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