1 /* 2 * INET An implementation of the TCP/IP protocol suite for the LINUX 3 * operating system. INET is implemented using the BSD Socket 4 * interface as the means of communication with the user level. 5 * 6 * PACKET - implements raw packet sockets. 7 * 8 * Version: @(#)packet.c 1.0.6 05/25/93 9 * 10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu> 11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 12 * Alan Cox, <gw4pts@gw4pts.ampr.org> 13 * 14 * Fixes: 15 * Alan Cox : verify_area() now used correctly 16 * Alan Cox : new skbuff lists, look ma no backlogs! 17 * Alan Cox : tidied skbuff lists. 18 * Alan Cox : Now uses generic datagram routines I 19 * added. Also fixed the peek/read crash 20 * from all old Linux datagram code. 21 * Alan Cox : Uses the improved datagram code. 22 * Alan Cox : Added NULL's for socket options. 23 * Alan Cox : Re-commented the code. 24 * Alan Cox : Use new kernel side addressing 25 * Rob Janssen : Correct MTU usage. 26 * Dave Platt : Counter leaks caused by incorrect 27 * interrupt locking and some slightly 28 * dubious gcc output. Can you read 29 * compiler: it said _VOLATILE_ 30 * Richard Kooijman : Timestamp fixes. 31 * Alan Cox : New buffers. Use sk->mac.raw. 32 * Alan Cox : sendmsg/recvmsg support. 33 * 34 * This program is free software; you can redistribute it and/or 35 * modify it under the terms of the GNU General Public License 36 * as published by the Free Software Foundation; either version 37 * 2 of the License, or (at your option) any later version. 38 * 39 */ 40
41 #include <linux/types.h>
42 #include <linux/sched.h>
43 #include <linux/mm.h>
44 #include <linux/fcntl.h>
45 #include <linux/socket.h>
46 #include <linux/in.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/ip.h>
50 #include <net/protocol.h>
51 #include <linux/skbuff.h>
52 #include <net/sock.h>
53 #include <linux/errno.h>
54 #include <linux/timer.h>
55 #include <asm/system.h>
56 #include <asm/segment.h>
57
58 /* 59 * We really ought to have a single public _inline_ min function! 60 */ 61
62 staticunsignedlongmin(unsignedlonga, unsignedlongb)
/* */ 63 { 64 if (a < b)
65 return(a);
66 return(b);
67 } 68
69
70 /* 71 * This should be the easiest of all, all we do is copy it into a buffer. 72 */ 73
74 intpacket_rcv(structsk_buff *skb, structdevice *dev, structpacket_type *pt)
/* */ 75 { 76 structsock *sk;
77
78 /* 79 * When we registered the protocol we saved the socket in the data 80 * field for just this event. 81 */ 82
83 sk = (structsock *) pt->data;
84
85 /* 86 * Yank back the headers [hope the device set this 87 * right or kerboom...] 88 */ 89
90 skb_push(skb,skb->data-skb->mac.raw);
91
92 /* 93 * The SOCK_PACKET socket receives _all_ frames. 94 */ 95
96 skb->dev = dev;
97
98 /* 99 * Charge the memory to the socket. This is done specifically 100 * to prevent sockets using all the memory up. 101 */ 102
103 if(sock_queue_rcv_skb(sk,skb)<0)
104 { 105 skb->sk = NULL;
106 kfree_skb(skb, FREE_READ);
107 return 0;
108 } 109 /* 110 * Processing complete. 111 */ 112
113 return(0);
114 } 115
116
117 /* 118 * Output a raw packet to a device layer. This bypasses all the other 119 * protocol layers and you must therefore supply it with a complete frame 120 */ 121
122 staticintpacket_sendmsg(structsock *sk, structmsghdr *msg, intlen,
/* */ 123 intnoblock, intflags)
124 { 125 structsk_buff *skb;
126 structdevice *dev;
127 structsockaddr *saddr=(structsockaddr *)msg->msg_name;
128
129 /* 130 * Check the flags. 131 */ 132
133 if (flags)
134 return(-EINVAL);
135
136 /* 137 * Get and verify the address. 138 */ 139
140 if (saddr)
141 { 142 if (msg->msg_namelen < sizeof(*saddr))
143 return(-EINVAL);
144 } 145 else 146 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */ 147
148 /* 149 * Find the device first to size check it 150 */ 151
152 saddr->sa_data[13] = 0;
153 dev = dev_get(saddr->sa_data);
154 if (dev == NULL)
155 { 156 return(-ENODEV);
157 } 158
159 /* 160 * You may not queue a frame bigger than the mtu. This is the lowest level 161 * raw protocol and you must do your own fragmentation at this level. 162 */ 163
164 if(len>dev->mtu+dev->hard_header_len)
165 return -EMSGSIZE;
166
167 skb = sock_wmalloc(sk, len, 0, GFP_KERNEL);
168
169 /* 170 * If the write buffer is full, then tough. At this level the user gets to 171 * deal with the problem - do your own algorithmic backoffs. Thats far 172 * more flexible. 173 */ 174
175 if (skb == NULL)
176 { 177 return(-ENOBUFS);
178 } 179
180 /* 181 * Fill it in 182 */ 183
184 skb->sk = sk;
185 skb->free = 1;
186 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
187 skb->arp = 1; /* No ARP needs doing on this (complete) frame */ 188
189 /* 190 * Now send it 191 */ 192
193 if (dev->flags & IFF_UP)
194 dev_queue_xmit(skb, dev, sk->priority);
195 else 196 kfree_skb(skb, FREE_WRITE);
197 return(len);
198 } 199
200 /* 201 * Close a SOCK_PACKET socket. This is fairly simple. We immediately go 202 * to 'closed' state and remove our protocol entry in the device list. 203 * The release_sock() will destroy the socket if a user has closed the 204 * file side of the object. 205 */ 206
207 staticvoidpacket_close(structsock *sk, inttimeout)
/* */ 208 { 209 /* 210 * Stop more data and kill the socket off. 211 */ 212
213 lock_sock(sk);
214 sk->state = TCP_CLOSE;
215
216 /* 217 * Unhook the notifier 218 */ 219
220 unregister_netdevice_notifier(&sk->protinfo.af_packet.notifier);
221
222 if(sk->protinfo.af_packet.prot_hook)
223 { 224 /* 225 * Remove the protocol hook 226 */ 227
228 dev_remove_pack((structpacket_type *)sk->protinfo.af_packet.prot_hook);
229
230 /* 231 * Dispose of litter carefully. 232 */ 233
234 kfree_s((void *)sk->protinfo.af_packet.prot_hook, sizeof(structpacket_type));
235 sk->protinfo.af_packet.prot_hook = NULL;
236 } 237
238 release_sock(sk);
239 } 240
241 /* 242 * Attach a packer hook to a device. 243 */ 244
245 intpacket_attach(structsock *sk)
/* */ 246 { 247 structpacket_type *p = (structpacket_type *) kmalloc(sizeof(*p), GFP_KERNEL);
248 if (p == NULL)
249 return(-ENOMEM);
250
251 p->func = packet_rcv;
252 p->type = sk->num;
253 p->data = (void *)sk;
254 p->dev = NULL;
255 dev_add_pack(p);
256
257 /* 258 * We need to remember this somewhere. 259 */ 260
261 sk->protinfo.af_packet.prot_hook = p;
262 return 0;
263 } 264
265 /* 266 * Bind a packet socket to a device 267 */ 268
269 staticintpacket_bind(structsock *sk, structsockaddr *uaddr, intaddr_len)
/* */ 270 { 271 chardev[15];
272
273 /* 274 * Check legality 275 */ 276
277 if(addr_len!=sizeof(structsockaddr))
278 return -EINVAL;
279 strncpy(dev,uaddr->sa_data,14);
280 dev[14]=0;
281
282 /* 283 * Lock the device chain while we sanity check 284 * the bind request. 285 */ 286
287 dev_lock_list();
288 if((sk->protinfo.af_packet.bound_dev=dev_get(dev))==NULL)
289 { 290 dev_unlock_list();
291 return -ENODEV;
292 } 293 if(!(sk->protinfo.af_packet.bound_dev->flags&IFF_UP))
294 { 295 dev_unlock_list();
296 return -ENETDOWN;
297 } 298
299 /* 300 * Perform the request. 301 */ 302
303 memcpy(sk->protinfo.af_packet.device_name,dev,15);
304 if(sk->protinfo.af_packet.prot_hook)
305 dev_remove_pack(sk->protinfo.af_packet.prot_hook);
306 else 307 { 308 interr=packet_attach(sk);
309 if(err)
310 { 311 dev_unlock_list();
312 returnerr;
313 } 314 } 315 sk->protinfo.af_packet.prot_hook->dev=sk->protinfo.af_packet.bound_dev;
316 dev_add_pack(sk->protinfo.af_packet.prot_hook);
317 /* 318 * Now the notifier is set up right this lot is safe. 319 */ 320 dev_unlock_list();
321 return 0;
322 } 323
324 /* 325 * This hook is called when a device goes up or down so that 326 * SOCK_PACKET sockets can come unbound properly. 327 */ 328
329 staticintpacket_unbind(structnotifier_block *this, unsignedlongmsg, void *data)
/* */ 330 { 331 structinet_packet_opt *ipo=(structinet_packet_opt *)this;
332 if(msg==NETDEV_DOWN && data==ipo->bound_dev)
333 { 334 /* 335 * Our device has gone down. 336 */ 337 ipo->bound_dev=NULL;
338 dev_remove_pack(ipo->prot_hook);
339 kfree(ipo->prot_hook);
340 ipo->prot_hook=NULL;
341 } 342 returnNOTIFY_DONE;
343 } 344
345
346 /* 347 * Create a packet of type SOCK_PACKET. 348 */ 349
350 staticintpacket_init(structsock *sk)
/* */ 351 { 352 /* 353 * Attach a protocol block 354 */ 355
356 interr=packet_attach(sk);
357 if(err)
358 returnerr;
359
360 /* 361 * Set up the per socket notifier. 362 */ 363
364 sk->protinfo.af_packet.notifier.notifier_call=packet_unbind;
365 sk->protinfo.af_packet.notifier.priority=0;
366
367 register_netdevice_notifier(&sk->protinfo.af_packet.notifier);
368
369 return(0);
370 } 371
372
373 /* 374 * Pull a packet from our receive queue and hand it to the user. 375 * If necessary we block. 376 */ 377
378 intpacket_recvmsg(structsock *sk, structmsghdr *msg, intlen,
/* */ 379 intnoblock, intflags,int *addr_len)
380 { 381 intcopied=0;
382 structsk_buff *skb;
383 structsockaddr *saddr=(structsockaddr *)msg->msg_name;
384 interr;
385
386 if (sk->shutdown & RCV_SHUTDOWN)
387 return(0);
388
389 /* 390 * If there is no protocol hook then the device is down. 391 */ 392
393 if(sk->protinfo.af_packet.prot_hook==NULL)
394 return -ENETDOWN;
395
396 /* 397 * If the address length field is there to be filled in, we fill 398 * it in now. 399 */ 400
401 if (addr_len)
402 *addr_len=sizeof(*saddr);
403
404 /* 405 * Call the generic datagram receiver. This handles all sorts 406 * of horrible races and re-entrancy so we can forget about it 407 * in the protocol layers. 408 */ 409
410 skb=skb_recv_datagram(sk,flags,noblock,&err);
411
412 /* 413 * An error occurred so return it. Because skb_recv_datagram() 414 * handles the blocking we don't see and worry about blocking 415 * retries. 416 */ 417
418 if(skb==NULL)
419 returnerr;
420
421 /* 422 * You lose any data beyond the buffer you gave. If it worries a 423 * user program they can ask the device for its MTU anyway. 424 */ 425
426 copied = min(len, skb->len);
427
428 memcpy_toiovec(msg->msg_iov, skb->data, copied); /* We can't use skb_copy_datagram here */ 429 sk->stamp=skb->stamp;
430
431 /* 432 * Copy the address. 433 */ 434
435 if (saddr)
436 { 437 saddr->sa_family = skb->dev->type;
438 strncpy(saddr->sa_data,skb->dev->name, 15);
439 } 440
441 /* 442 * Free or return the buffer as appropriate. Again this hides all the 443 * races and re-entrancy issues from us. 444 */ 445
446 skb_free_datagram(sk, skb);
447
448 return(copied);
449 } 450
451 /* 452 * This structure declares to the lower layer socket subsystem currently 453 * incorrectly embedded in the IP code how to behave. This interface needs 454 * a lot of work and will change. 455 */ 456
457 structprotopacket_prot =
458 { 459 packet_close,
460 ip_build_header, /* Not actually used */ 461 NULL,
462 NULL,
463 ip_queue_xmit, /* These two are not actually used */ 464 NULL,
465 NULL,
466 NULL,
467 NULL,
468 datagram_select,
469 NULL, /* No ioctl */ 470 packet_init,
471 NULL,
472 NULL, /* No set/get socket options */ 473 NULL,
474 packet_sendmsg, /* Sendmsg */ 475 packet_recvmsg, /* Recvmsg */ 476 packet_bind, /* Bind */ 477 128,
478 0,
479 "PACKET",
480 0, 0
481 };
482
483