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 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)
*/
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 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)
*/
75 {
76 struct sock *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 = (struct sock *) 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 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)
*/
123 int noblock, int flags)
124 {
125 struct sk_buff *skb;
126 struct device *dev;
127 struct sockaddr *saddr=(struct sockaddr *)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 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)
*/
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((struct packet_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(struct packet_type));
235 sk->protinfo.af_packet.prot_hook = NULL;
236 }
237
238 release_sock(sk);
239 destroy_sock(sk);
240 }
241
242 /*
243 * Attach a packet hook to a device.
244 */
245
246 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)
*/
247 {
248 struct packet_type *p = (struct packet_type *) kmalloc(sizeof(*p), GFP_KERNEL);
249 if (p == NULL)
250 return(-ENOMEM);
251
252 p->func = packet_rcv;
253 p->type = sk->num;
254 p->data = (void *)sk;
255 p->dev = dev;
256 dev_add_pack(p);
257
258 /*
259 * We need to remember this somewhere.
260 */
261
262 sk->protinfo.af_packet.prot_hook = p;
263 sk->protinfo.af_packet.bound_dev = dev;
264 return 0;
265 }
266
267 /*
268 * Bind a packet socket to a device
269 */
270
271 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)
*/
272 {
273 char name[15];
274 struct device *dev;
275
276 /*
277 * Check legality
278 */
279
280 if(addr_len!=sizeof(struct sockaddr))
281 return -EINVAL;
282 strncpy(name,uaddr->sa_data,14);
283 name[14]=0;
284
285 /*
286 * Lock the device chain while we sanity check
287 * the bind request.
288 */
289
290 dev_lock_list();
291 dev=dev_get(name);
292 if(dev==NULL)
293 {
294 dev_unlock_list();
295 return -ENODEV;
296 }
297
298 if(!(dev->flags&IFF_UP))
299 {
300 dev_unlock_list();
301 return -ENETDOWN;
302 }
303
304 /*
305 * Perform the request.
306 */
307
308 memcpy(sk->protinfo.af_packet.device_name,name,15);
309
310 /*
311 * Rewrite an existing hook if present.
312 */
313
314 if(sk->protinfo.af_packet.prot_hook)
315 {
316 dev_remove_pack(sk->protinfo.af_packet.prot_hook);
317 sk->protinfo.af_packet.prot_hook->dev=dev;
318 sk->protinfo.af_packet.bound_dev=dev;
319 dev_add_pack(sk->protinfo.af_packet.prot_hook);
320 }
321 else
322 {
323 int err=packet_attach(sk, dev);
324 if(err)
325 {
326 dev_unlock_list();
327 return err;
328 }
329 }
330 /*
331 * Now the notifier is set up right this lot is safe.
332 */
333 dev_unlock_list();
334 return 0;
335 }
336
337 /*
338 * This hook is called when a device goes up or down so that
339 * SOCK_PACKET sockets can come unbound properly.
340 */
341
342 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)
*/
343 {
344 struct inet_packet_opt *ipo=(struct inet_packet_opt *)this;
345 if(msg==NETDEV_DOWN && data==ipo->bound_dev)
346 {
347 /*
348 * Our device has gone down.
349 */
350 ipo->bound_dev=NULL;
351 dev_remove_pack(ipo->prot_hook);
352 kfree(ipo->prot_hook);
353 ipo->prot_hook=NULL;
354 }
355 return NOTIFY_DONE;
356 }
357
358
359 /*
360 * Create a packet of type SOCK_PACKET.
361 */
362
363 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)
*/
364 {
365 /*
366 * Attach a protocol block
367 */
368
369 int err=packet_attach(sk, NULL);
370 if(err)
371 return err;
372
373 /*
374 * Set up the per socket notifier.
375 */
376
377 sk->protinfo.af_packet.notifier.notifier_call=packet_unbind;
378 sk->protinfo.af_packet.notifier.priority=0;
379
380 register_netdevice_notifier(&sk->protinfo.af_packet.notifier);
381
382 return(0);
383 }
384
385
386 /*
387 * Pull a packet from our receive queue and hand it to the user.
388 * If necessary we block.
389 */
390
391 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)
*/
392 int noblock, int flags,int *addr_len)
393 {
394 int copied=0;
395 struct sk_buff *skb;
396 struct sockaddr *saddr=(struct sockaddr *)msg->msg_name;
397 int err;
398
399 if (sk->shutdown & RCV_SHUTDOWN)
400 return(0);
401
402 /*
403 * If there is no protocol hook then the device is down.
404 */
405
406 if(sk->protinfo.af_packet.prot_hook==NULL)
407 return -ENETDOWN;
408
409 /*
410 * If the address length field is there to be filled in, we fill
411 * it in now.
412 */
413
414 if (addr_len)
415 *addr_len=sizeof(*saddr);
416
417 /*
418 * Call the generic datagram receiver. This handles all sorts
419 * of horrible races and re-entrancy so we can forget about it
420 * in the protocol layers.
421 */
422
423 skb=skb_recv_datagram(sk,flags,noblock,&err);
424
425 /*
426 * An error occurred so return it. Because skb_recv_datagram()
427 * handles the blocking we don't see and worry about blocking
428 * retries.
429 */
430
431 if(skb==NULL)
432 return err;
433
434 /*
435 * You lose any data beyond the buffer you gave. If it worries a
436 * user program they can ask the device for its MTU anyway.
437 */
438
439 copied = min(len, skb->len);
440
441 memcpy_toiovec(msg->msg_iov, skb->data, copied); /* We can't use skb_copy_datagram here */
442 sk->stamp=skb->stamp;
443
444 /*
445 * Copy the address.
446 */
447
448 if (saddr)
449 {
450 saddr->sa_family = skb->dev->type;
451 strncpy(saddr->sa_data,skb->dev->name, 15);
452 }
453
454 /*
455 * Free or return the buffer as appropriate. Again this hides all the
456 * races and re-entrancy issues from us.
457 */
458
459 skb_free_datagram(sk, skb);
460
461 return(copied);
462 }
463
464 /*
465 * This structure declares to the lower layer socket subsystem currently
466 * incorrectly embedded in the IP code how to behave. This interface needs
467 * a lot of work and will change.
468 */
469
470 struct proto packet_prot =
471 {
472 packet_close,
473 ip_build_header, /* Not actually used */
474 NULL,
475 NULL,
476 ip_queue_xmit, /* These two are not actually used */
477 NULL,
478 NULL,
479 NULL,
480 NULL,
481 datagram_select,
482 NULL, /* No ioctl */
483 packet_init,
484 NULL,
485 NULL, /* No set/get socket options */
486 NULL,
487 packet_sendmsg, /* Sendmsg */
488 packet_recvmsg, /* Recvmsg */
489 packet_bind, /* Bind */
490 128,
491 0,
492 "PACKET",
493 0, 0
494 };
495
496