blob: ee865d88183bb930d4d05faf01e584263a80f51f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $
9 *
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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 * Alan Cox : Protocol setting support
34 * Alexey Kuznetsov : Untied from IPv4 stack.
35 * Cyrus Durgin : Fixed kerneld for kmod.
36 * Michal Ostrowski : Module initialization cleanup.
37 * Ulises Alonso : Frame number limit removal and
38 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070039 * Eric Biederman : Allow for > 8 byte hardware addresses.
40 * The convention is that longer addresses
41 * will simply extend the hardware address
42 * byte arrays at the end of sockaddr_ll
43 * and packet_mreq.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * This program is free software; you can redistribute it and/or
46 * modify it under the terms of the GNU General Public License
47 * as published by the Free Software Foundation; either version
48 * 2 of the License, or (at your option) any later version.
49 *
50 */
51
52#include <linux/config.h>
53#include <linux/types.h>
54#include <linux/sched.h>
55#include <linux/mm.h>
56#include <linux/fcntl.h>
57#include <linux/socket.h>
58#include <linux/in.h>
59#include <linux/inet.h>
60#include <linux/netdevice.h>
61#include <linux/if_packet.h>
62#include <linux/wireless.h>
63#include <linux/kmod.h>
64#include <net/ip.h>
65#include <net/protocol.h>
66#include <linux/skbuff.h>
67#include <net/sock.h>
68#include <linux/errno.h>
69#include <linux/timer.h>
70#include <asm/system.h>
71#include <asm/uaccess.h>
72#include <asm/ioctls.h>
73#include <asm/page.h>
74#include <asm/io.h>
75#include <linux/proc_fs.h>
76#include <linux/seq_file.h>
77#include <linux/poll.h>
78#include <linux/module.h>
79#include <linux/init.h>
80
81#ifdef CONFIG_INET
82#include <net/inet_common.h>
83#endif
84
85#define CONFIG_SOCK_PACKET 1
86
87/*
88 Proposed replacement for SIOC{ADD,DEL}MULTI and
89 IFF_PROMISC, IFF_ALLMULTI flags.
90
91 It is more expensive, but I believe,
92 it is really correct solution: reentereble, safe and fault tolerant.
93
94 IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
95 reference count and global flag, so that real status is
96 (gflag|(count != 0)), so that we can use obsolete faulty interface
97 not harming clever users.
98 */
99#define CONFIG_PACKET_MULTICAST 1
100
101/*
102 Assumptions:
103 - if device has no dev->hard_header routine, it adds and removes ll header
104 inside itself. In this case ll header is invisible outside of device,
105 but higher levels still should reserve dev->hard_header_len.
106 Some devices are enough clever to reallocate skb, when header
107 will not fit to reserved space (tunnel), another ones are silly
108 (PPP).
109 - packet socket receives packets with pulled ll header,
110 so that SOCK_RAW should push it back.
111
112On receive:
113-----------
114
115Incoming, dev->hard_header!=NULL
116 mac.raw -> ll header
117 data -> data
118
119Outgoing, dev->hard_header!=NULL
120 mac.raw -> ll header
121 data -> ll header
122
123Incoming, dev->hard_header==NULL
124 mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
125 PPP makes it, that is wrong, because introduce assymetry
126 between rx and tx paths.
127 data -> data
128
129Outgoing, dev->hard_header==NULL
130 mac.raw -> data. ll header is still not built!
131 data -> data
132
133Resume
134 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
135
136
137On transmit:
138------------
139
140dev->hard_header != NULL
141 mac.raw -> ll header
142 data -> ll header
143
144dev->hard_header == NULL (ll header is added by device, we cannot control it)
145 mac.raw -> data
146 data -> data
147
148 We should set nh.raw on output to correct posistion,
149 packet classifier depends on it.
150 */
151
152/* List of all packet sockets. */
153static HLIST_HEAD(packet_sklist);
154static DEFINE_RWLOCK(packet_sklist_lock);
155
156static atomic_t packet_socks_nr;
157
158
159/* Private packet socket structures. */
160
161#ifdef CONFIG_PACKET_MULTICAST
162struct packet_mclist
163{
164 struct packet_mclist *next;
165 int ifindex;
166 int count;
167 unsigned short type;
168 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700169 unsigned char addr[MAX_ADDR_LEN];
170};
171/* identical to struct packet_mreq except it has
172 * a longer address field.
173 */
174struct packet_mreq_max
175{
176 int mr_ifindex;
177 unsigned short mr_type;
178 unsigned short mr_alen;
179 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181#endif
182#ifdef CONFIG_PACKET_MMAP
183static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
184#endif
185
186static void packet_flush_mclist(struct sock *sk);
187
188struct packet_sock {
189 /* struct sock has to be the first member of packet_sock */
190 struct sock sk;
191 struct tpacket_stats stats;
192#ifdef CONFIG_PACKET_MMAP
193 char * *pg_vec;
194 unsigned int head;
195 unsigned int frames_per_block;
196 unsigned int frame_size;
197 unsigned int frame_max;
198 int copy_thresh;
199#endif
200 struct packet_type prot_hook;
201 spinlock_t bind_lock;
202 char running; /* prot_hook is attached*/
203 int ifindex; /* bound device */
204 unsigned short num;
205#ifdef CONFIG_PACKET_MULTICAST
206 struct packet_mclist *mclist;
207#endif
208#ifdef CONFIG_PACKET_MMAP
209 atomic_t mapped;
210 unsigned int pg_vec_order;
211 unsigned int pg_vec_pages;
212 unsigned int pg_vec_len;
213#endif
214};
215
216#ifdef CONFIG_PACKET_MMAP
217
218static inline char *packet_lookup_frame(struct packet_sock *po, unsigned int position)
219{
220 unsigned int pg_vec_pos, frame_offset;
221 char *frame;
222
223 pg_vec_pos = position / po->frames_per_block;
224 frame_offset = position % po->frames_per_block;
225
226 frame = po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size);
227
228 return frame;
229}
230#endif
231
232static inline struct packet_sock *pkt_sk(struct sock *sk)
233{
234 return (struct packet_sock *)sk;
235}
236
237static void packet_sock_destruct(struct sock *sk)
238{
239 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
240 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
241
242 if (!sock_flag(sk, SOCK_DEAD)) {
243 printk("Attempt to release alive packet socket: %p\n", sk);
244 return;
245 }
246
247 atomic_dec(&packet_socks_nr);
248#ifdef PACKET_REFCNT_DEBUG
249 printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
250#endif
251}
252
253
254static struct proto_ops packet_ops;
255
256#ifdef CONFIG_SOCK_PACKET
257static struct proto_ops packet_ops_spkt;
258
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700259static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct sock *sk;
262 struct sockaddr_pkt *spkt;
263
264 /*
265 * When we registered the protocol we saved the socket in the data
266 * field for just this event.
267 */
268
269 sk = pt->af_packet_priv;
270
271 /*
272 * Yank back the headers [hope the device set this
273 * right or kerboom...]
274 *
275 * Incoming packets have ll header pulled,
276 * push it back.
277 *
278 * For outgoing ones skb->data == skb->mac.raw
279 * so that this procedure is noop.
280 */
281
282 if (skb->pkt_type == PACKET_LOOPBACK)
283 goto out;
284
285 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
286 goto oom;
287
288 /* drop any routing info */
289 dst_release(skb->dst);
290 skb->dst = NULL;
291
Phil Oester84531c22005-07-12 11:57:52 -0700292 /* drop conntrack reference */
293 nf_reset(skb);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 spkt = (struct sockaddr_pkt*)skb->cb;
296
297 skb_push(skb, skb->data-skb->mac.raw);
298
299 /*
300 * The SOCK_PACKET socket receives _all_ frames.
301 */
302
303 spkt->spkt_family = dev->type;
304 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
305 spkt->spkt_protocol = skb->protocol;
306
307 /*
308 * Charge the memory to the socket. This is done specifically
309 * to prevent sockets using all the memory up.
310 */
311
312 if (sock_queue_rcv_skb(sk,skb) == 0)
313 return 0;
314
315out:
316 kfree_skb(skb);
317oom:
318 return 0;
319}
320
321
322/*
323 * Output a raw packet to a device layer. This bypasses all the other
324 * protocol layers and you must therefore supply it with a complete frame
325 */
326
327static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
328 struct msghdr *msg, size_t len)
329{
330 struct sock *sk = sock->sk;
331 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
332 struct sk_buff *skb;
333 struct net_device *dev;
334 unsigned short proto=0;
335 int err;
336
337 /*
338 * Get and verify the address.
339 */
340
341 if (saddr)
342 {
343 if (msg->msg_namelen < sizeof(struct sockaddr))
344 return(-EINVAL);
345 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
346 proto=saddr->spkt_protocol;
347 }
348 else
349 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
350
351 /*
352 * Find the device first to size check it
353 */
354
355 saddr->spkt_device[13] = 0;
356 dev = dev_get_by_name(saddr->spkt_device);
357 err = -ENODEV;
358 if (dev == NULL)
359 goto out_unlock;
360
361 /*
362 * You may not queue a frame bigger than the mtu. This is the lowest level
363 * raw protocol and you must do your own fragmentation at this level.
364 */
365
366 err = -EMSGSIZE;
367 if(len>dev->mtu+dev->hard_header_len)
368 goto out_unlock;
369
370 err = -ENOBUFS;
371 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
372
373 /*
374 * If the write buffer is full, then tough. At this level the user gets to
375 * deal with the problem - do your own algorithmic backoffs. That's far
376 * more flexible.
377 */
378
379 if (skb == NULL)
380 goto out_unlock;
381
382 /*
383 * Fill it in
384 */
385
386 /* FIXME: Save some space for broken drivers that write a
387 * hard header at transmission time by themselves. PPP is the
388 * notable one here. This should really be fixed at the driver level.
389 */
390 skb_reserve(skb, LL_RESERVED_SPACE(dev));
391 skb->nh.raw = skb->data;
392
393 /* Try to align data part correctly */
394 if (dev->hard_header) {
395 skb->data -= dev->hard_header_len;
396 skb->tail -= dev->hard_header_len;
397 if (len < dev->hard_header_len)
398 skb->nh.raw = skb->data;
399 }
400
401 /* Returns -EFAULT on error */
402 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
403 skb->protocol = proto;
404 skb->dev = dev;
405 skb->priority = sk->sk_priority;
406 if (err)
407 goto out_free;
408
409 err = -ENETDOWN;
410 if (!(dev->flags & IFF_UP))
411 goto out_free;
412
413 /*
414 * Now send it
415 */
416
417 dev_queue_xmit(skb);
418 dev_put(dev);
419 return(len);
420
421out_free:
422 kfree_skb(skb);
423out_unlock:
424 if (dev)
425 dev_put(dev);
426 return err;
427}
428#endif
429
430static inline unsigned run_filter(struct sk_buff *skb, struct sock *sk, unsigned res)
431{
432 struct sk_filter *filter;
433
434 bh_lock_sock(sk);
435 filter = sk->sk_filter;
436 /*
437 * Our caller already checked that filter != NULL but we need to
438 * verify that under bh_lock_sock() to be safe
439 */
440 if (likely(filter != NULL))
441 res = sk_run_filter(skb, filter->insns, filter->len);
442 bh_unlock_sock(sk);
443
444 return res;
445}
446
447/*
448 This function makes lazy skb cloning in hope that most of packets
449 are discarded by BPF.
450
451 Note tricky part: we DO mangle shared skb! skb->data, skb->len
452 and skb->cb are mangled. It works because (and until) packets
453 falling here are owned by current CPU. Output packets are cloned
454 by dev_queue_xmit_nit(), input packets are processed by net_bh
455 sequencially, so that if we return skb to original state on exit,
456 we will not harm anyone.
457 */
458
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700459static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct sock *sk;
462 struct sockaddr_ll *sll;
463 struct packet_sock *po;
464 u8 * skb_head = skb->data;
465 int skb_len = skb->len;
466 unsigned snaplen;
467
468 if (skb->pkt_type == PACKET_LOOPBACK)
469 goto drop;
470
471 sk = pt->af_packet_priv;
472 po = pkt_sk(sk);
473
474 skb->dev = dev;
475
476 if (dev->hard_header) {
477 /* The device has an explicit notion of ll header,
478 exported to higher levels.
479
480 Otherwise, the device hides datails of it frame
481 structure, so that corresponding packet head
482 never delivered to user.
483 */
484 if (sk->sk_type != SOCK_DGRAM)
485 skb_push(skb, skb->data - skb->mac.raw);
486 else if (skb->pkt_type == PACKET_OUTGOING) {
487 /* Special case: outgoing packets have ll header at head */
488 skb_pull(skb, skb->nh.raw - skb->data);
489 }
490 }
491
492 snaplen = skb->len;
493
494 if (sk->sk_filter) {
495 unsigned res = run_filter(skb, sk, snaplen);
496 if (res == 0)
497 goto drop_n_restore;
498 if (snaplen > res)
499 snaplen = res;
500 }
501
502 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
503 (unsigned)sk->sk_rcvbuf)
504 goto drop_n_acct;
505
506 if (skb_shared(skb)) {
507 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
508 if (nskb == NULL)
509 goto drop_n_acct;
510
511 if (skb_head != skb->data) {
512 skb->data = skb_head;
513 skb->len = skb_len;
514 }
515 kfree_skb(skb);
516 skb = nskb;
517 }
518
519 sll = (struct sockaddr_ll*)skb->cb;
520 sll->sll_family = AF_PACKET;
521 sll->sll_hatype = dev->type;
522 sll->sll_protocol = skb->protocol;
523 sll->sll_pkttype = skb->pkt_type;
524 sll->sll_ifindex = dev->ifindex;
525 sll->sll_halen = 0;
526
527 if (dev->hard_header_parse)
528 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
529
530 if (pskb_trim(skb, snaplen))
531 goto drop_n_acct;
532
533 skb_set_owner_r(skb, sk);
534 skb->dev = NULL;
535 dst_release(skb->dst);
536 skb->dst = NULL;
537
Phil Oester84531c22005-07-12 11:57:52 -0700538 /* drop conntrack reference */
539 nf_reset(skb);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 spin_lock(&sk->sk_receive_queue.lock);
542 po->stats.tp_packets++;
543 __skb_queue_tail(&sk->sk_receive_queue, skb);
544 spin_unlock(&sk->sk_receive_queue.lock);
545 sk->sk_data_ready(sk, skb->len);
546 return 0;
547
548drop_n_acct:
549 spin_lock(&sk->sk_receive_queue.lock);
550 po->stats.tp_drops++;
551 spin_unlock(&sk->sk_receive_queue.lock);
552
553drop_n_restore:
554 if (skb_head != skb->data && skb_shared(skb)) {
555 skb->data = skb_head;
556 skb->len = skb_len;
557 }
558drop:
559 kfree_skb(skb);
560 return 0;
561}
562
563#ifdef CONFIG_PACKET_MMAP
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700564static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct sock *sk;
567 struct packet_sock *po;
568 struct sockaddr_ll *sll;
569 struct tpacket_hdr *h;
570 u8 * skb_head = skb->data;
571 int skb_len = skb->len;
572 unsigned snaplen;
573 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
574 unsigned short macoff, netoff;
575 struct sk_buff *copy_skb = NULL;
576
577 if (skb->pkt_type == PACKET_LOOPBACK)
578 goto drop;
579
580 sk = pt->af_packet_priv;
581 po = pkt_sk(sk);
582
583 if (dev->hard_header) {
584 if (sk->sk_type != SOCK_DGRAM)
585 skb_push(skb, skb->data - skb->mac.raw);
586 else if (skb->pkt_type == PACKET_OUTGOING) {
587 /* Special case: outgoing packets have ll header at head */
588 skb_pull(skb, skb->nh.raw - skb->data);
589 if (skb->ip_summed == CHECKSUM_HW)
590 status |= TP_STATUS_CSUMNOTREADY;
591 }
592 }
593
594 snaplen = skb->len;
595
596 if (sk->sk_filter) {
597 unsigned res = run_filter(skb, sk, snaplen);
598 if (res == 0)
599 goto drop_n_restore;
600 if (snaplen > res)
601 snaplen = res;
602 }
603
604 if (sk->sk_type == SOCK_DGRAM) {
605 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
606 } else {
607 unsigned maclen = skb->nh.raw - skb->data;
608 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
609 macoff = netoff - maclen;
610 }
611
612 if (macoff + snaplen > po->frame_size) {
613 if (po->copy_thresh &&
614 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
615 (unsigned)sk->sk_rcvbuf) {
616 if (skb_shared(skb)) {
617 copy_skb = skb_clone(skb, GFP_ATOMIC);
618 } else {
619 copy_skb = skb_get(skb);
620 skb_head = skb->data;
621 }
622 if (copy_skb)
623 skb_set_owner_r(copy_skb, sk);
624 }
625 snaplen = po->frame_size - macoff;
626 if ((int)snaplen < 0)
627 snaplen = 0;
628 }
629 if (snaplen > skb->len-skb->data_len)
630 snaplen = skb->len-skb->data_len;
631
632 spin_lock(&sk->sk_receive_queue.lock);
633 h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
634
635 if (h->tp_status)
636 goto ring_is_full;
637 po->head = po->head != po->frame_max ? po->head+1 : 0;
638 po->stats.tp_packets++;
639 if (copy_skb) {
640 status |= TP_STATUS_COPY;
641 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
642 }
643 if (!po->stats.tp_drops)
644 status &= ~TP_STATUS_LOSING;
645 spin_unlock(&sk->sk_receive_queue.lock);
646
647 memcpy((u8*)h + macoff, skb->data, snaplen);
648
649 h->tp_len = skb->len;
650 h->tp_snaplen = snaplen;
651 h->tp_mac = macoff;
652 h->tp_net = netoff;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700653 if (skb->tstamp.off_sec == 0) {
654 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 sock_enable_timestamp(sk);
656 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700657 h->tp_sec = skb_tv_base.tv_sec + skb->tstamp.off_sec;
658 h->tp_usec = skb_tv_base.tv_usec + skb->tstamp.off_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
661 sll->sll_halen = 0;
662 if (dev->hard_header_parse)
663 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
664 sll->sll_family = AF_PACKET;
665 sll->sll_hatype = dev->type;
666 sll->sll_protocol = skb->protocol;
667 sll->sll_pkttype = skb->pkt_type;
668 sll->sll_ifindex = dev->ifindex;
669
670 h->tp_status = status;
671 mb();
672
673 {
674 struct page *p_start, *p_end;
675 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
676
677 p_start = virt_to_page(h);
678 p_end = virt_to_page(h_end);
679 while (p_start <= p_end) {
680 flush_dcache_page(p_start);
681 p_start++;
682 }
683 }
684
685 sk->sk_data_ready(sk, 0);
686
687drop_n_restore:
688 if (skb_head != skb->data && skb_shared(skb)) {
689 skb->data = skb_head;
690 skb->len = skb_len;
691 }
692drop:
693 kfree_skb(skb);
694 return 0;
695
696ring_is_full:
697 po->stats.tp_drops++;
698 spin_unlock(&sk->sk_receive_queue.lock);
699
700 sk->sk_data_ready(sk, 0);
701 if (copy_skb)
702 kfree_skb(copy_skb);
703 goto drop_n_restore;
704}
705
706#endif
707
708
709static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
710 struct msghdr *msg, size_t len)
711{
712 struct sock *sk = sock->sk;
713 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
714 struct sk_buff *skb;
715 struct net_device *dev;
716 unsigned short proto;
717 unsigned char *addr;
718 int ifindex, err, reserve = 0;
719
720 /*
721 * Get and verify the address.
722 */
723
724 if (saddr == NULL) {
725 struct packet_sock *po = pkt_sk(sk);
726
727 ifindex = po->ifindex;
728 proto = po->num;
729 addr = NULL;
730 } else {
731 err = -EINVAL;
732 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
733 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700734 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ifindex = saddr->sll_ifindex;
737 proto = saddr->sll_protocol;
738 addr = saddr->sll_addr;
739 }
740
741
742 dev = dev_get_by_index(ifindex);
743 err = -ENXIO;
744 if (dev == NULL)
745 goto out_unlock;
746 if (sock->type == SOCK_RAW)
747 reserve = dev->hard_header_len;
748
749 err = -EMSGSIZE;
750 if (len > dev->mtu+reserve)
751 goto out_unlock;
752
753 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
754 msg->msg_flags & MSG_DONTWAIT, &err);
755 if (skb==NULL)
756 goto out_unlock;
757
758 skb_reserve(skb, LL_RESERVED_SPACE(dev));
759 skb->nh.raw = skb->data;
760
761 if (dev->hard_header) {
762 int res;
763 err = -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700764 if (saddr) {
765 if (saddr->sll_halen != dev->addr_len)
766 goto out_free;
767 if (saddr->sll_hatype != dev->type)
768 goto out_free;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
771 if (sock->type != SOCK_DGRAM) {
772 skb->tail = skb->data;
773 skb->len = 0;
774 } else if (res < 0)
775 goto out_free;
776 }
777
778 /* Returns -EFAULT on error */
779 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
780 if (err)
781 goto out_free;
782
783 skb->protocol = proto;
784 skb->dev = dev;
785 skb->priority = sk->sk_priority;
786
787 err = -ENETDOWN;
788 if (!(dev->flags & IFF_UP))
789 goto out_free;
790
791 /*
792 * Now send it
793 */
794
795 err = dev_queue_xmit(skb);
796 if (err > 0 && (err = net_xmit_errno(err)) != 0)
797 goto out_unlock;
798
799 dev_put(dev);
800
801 return(len);
802
803out_free:
804 kfree_skb(skb);
805out_unlock:
806 if (dev)
807 dev_put(dev);
808out:
809 return err;
810}
811
812/*
813 * Close a PACKET socket. This is fairly simple. We immediately go
814 * to 'closed' state and remove our protocol entry in the device list.
815 */
816
817static int packet_release(struct socket *sock)
818{
819 struct sock *sk = sock->sk;
820 struct packet_sock *po;
821
822 if (!sk)
823 return 0;
824
825 po = pkt_sk(sk);
826
827 write_lock_bh(&packet_sklist_lock);
828 sk_del_node_init(sk);
829 write_unlock_bh(&packet_sklist_lock);
830
831 /*
832 * Unhook packet receive handler.
833 */
834
835 if (po->running) {
836 /*
837 * Remove the protocol hook
838 */
839 dev_remove_pack(&po->prot_hook);
840 po->running = 0;
841 po->num = 0;
842 __sock_put(sk);
843 }
844
845#ifdef CONFIG_PACKET_MULTICAST
846 packet_flush_mclist(sk);
847#endif
848
849#ifdef CONFIG_PACKET_MMAP
850 if (po->pg_vec) {
851 struct tpacket_req req;
852 memset(&req, 0, sizeof(req));
853 packet_set_ring(sk, &req, 1);
854 }
855#endif
856
857 /*
858 * Now the socket is dead. No more input will appear.
859 */
860
861 sock_orphan(sk);
862 sock->sk = NULL;
863
864 /* Purge queues */
865
866 skb_queue_purge(&sk->sk_receive_queue);
867
868 sock_put(sk);
869 return 0;
870}
871
872/*
873 * Attach a packet hook.
874 */
875
876static int packet_do_bind(struct sock *sk, struct net_device *dev, int protocol)
877{
878 struct packet_sock *po = pkt_sk(sk);
879 /*
880 * Detach an existing hook if present.
881 */
882
883 lock_sock(sk);
884
885 spin_lock(&po->bind_lock);
886 if (po->running) {
887 __sock_put(sk);
888 po->running = 0;
889 po->num = 0;
890 spin_unlock(&po->bind_lock);
891 dev_remove_pack(&po->prot_hook);
892 spin_lock(&po->bind_lock);
893 }
894
895 po->num = protocol;
896 po->prot_hook.type = protocol;
897 po->prot_hook.dev = dev;
898
899 po->ifindex = dev ? dev->ifindex : 0;
900
901 if (protocol == 0)
902 goto out_unlock;
903
904 if (dev) {
905 if (dev->flags&IFF_UP) {
906 dev_add_pack(&po->prot_hook);
907 sock_hold(sk);
908 po->running = 1;
909 } else {
910 sk->sk_err = ENETDOWN;
911 if (!sock_flag(sk, SOCK_DEAD))
912 sk->sk_error_report(sk);
913 }
914 } else {
915 dev_add_pack(&po->prot_hook);
916 sock_hold(sk);
917 po->running = 1;
918 }
919
920out_unlock:
921 spin_unlock(&po->bind_lock);
922 release_sock(sk);
923 return 0;
924}
925
926/*
927 * Bind a packet socket to a device
928 */
929
930#ifdef CONFIG_SOCK_PACKET
931
932static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
933{
934 struct sock *sk=sock->sk;
935 char name[15];
936 struct net_device *dev;
937 int err = -ENODEV;
938
939 /*
940 * Check legality
941 */
942
943 if(addr_len!=sizeof(struct sockaddr))
944 return -EINVAL;
945 strlcpy(name,uaddr->sa_data,sizeof(name));
946
947 dev = dev_get_by_name(name);
948 if (dev) {
949 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
950 dev_put(dev);
951 }
952 return err;
953}
954#endif
955
956static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
957{
958 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
959 struct sock *sk=sock->sk;
960 struct net_device *dev = NULL;
961 int err;
962
963
964 /*
965 * Check legality
966 */
967
968 if (addr_len < sizeof(struct sockaddr_ll))
969 return -EINVAL;
970 if (sll->sll_family != AF_PACKET)
971 return -EINVAL;
972
973 if (sll->sll_ifindex) {
974 err = -ENODEV;
975 dev = dev_get_by_index(sll->sll_ifindex);
976 if (dev == NULL)
977 goto out;
978 }
979 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
980 if (dev)
981 dev_put(dev);
982
983out:
984 return err;
985}
986
987static struct proto packet_proto = {
988 .name = "PACKET",
989 .owner = THIS_MODULE,
990 .obj_size = sizeof(struct packet_sock),
991};
992
993/*
994 * Create a packet of type SOCK_PACKET.
995 */
996
997static int packet_create(struct socket *sock, int protocol)
998{
999 struct sock *sk;
1000 struct packet_sock *po;
1001 int err;
1002
1003 if (!capable(CAP_NET_RAW))
1004 return -EPERM;
1005 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
1006#ifdef CONFIG_SOCK_PACKET
1007 && sock->type != SOCK_PACKET
1008#endif
1009 )
1010 return -ESOCKTNOSUPPORT;
1011
1012 sock->state = SS_UNCONNECTED;
1013
1014 err = -ENOBUFS;
1015 sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
1016 if (sk == NULL)
1017 goto out;
1018
1019 sock->ops = &packet_ops;
1020#ifdef CONFIG_SOCK_PACKET
1021 if (sock->type == SOCK_PACKET)
1022 sock->ops = &packet_ops_spkt;
1023#endif
1024 sock_init_data(sock, sk);
1025
1026 po = pkt_sk(sk);
1027 sk->sk_family = PF_PACKET;
1028 po->num = protocol;
1029
1030 sk->sk_destruct = packet_sock_destruct;
1031 atomic_inc(&packet_socks_nr);
1032
1033 /*
1034 * Attach a protocol block
1035 */
1036
1037 spin_lock_init(&po->bind_lock);
1038 po->prot_hook.func = packet_rcv;
1039#ifdef CONFIG_SOCK_PACKET
1040 if (sock->type == SOCK_PACKET)
1041 po->prot_hook.func = packet_rcv_spkt;
1042#endif
1043 po->prot_hook.af_packet_priv = sk;
1044
1045 if (protocol) {
1046 po->prot_hook.type = protocol;
1047 dev_add_pack(&po->prot_hook);
1048 sock_hold(sk);
1049 po->running = 1;
1050 }
1051
1052 write_lock_bh(&packet_sklist_lock);
1053 sk_add_node(sk, &packet_sklist);
1054 write_unlock_bh(&packet_sklist_lock);
1055 return(0);
1056out:
1057 return err;
1058}
1059
1060/*
1061 * Pull a packet from our receive queue and hand it to the user.
1062 * If necessary we block.
1063 */
1064
1065static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1066 struct msghdr *msg, size_t len, int flags)
1067{
1068 struct sock *sk = sock->sk;
1069 struct sk_buff *skb;
1070 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001071 struct sockaddr_ll *sll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 err = -EINVAL;
1074 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1075 goto out;
1076
1077#if 0
1078 /* What error should we return now? EUNATTACH? */
1079 if (pkt_sk(sk)->ifindex < 0)
1080 return -ENODEV;
1081#endif
1082
1083 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * Call the generic datagram receiver. This handles all sorts
1085 * of horrible races and re-entrancy so we can forget about it
1086 * in the protocol layers.
1087 *
1088 * Now it will return ENETDOWN, if device have just gone down,
1089 * but then it will block.
1090 */
1091
1092 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1093
1094 /*
1095 * An error occurred so return it. Because skb_recv_datagram()
1096 * handles the blocking we don't see and worry about blocking
1097 * retries.
1098 */
1099
1100 if(skb==NULL)
1101 goto out;
1102
1103 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001104 * If the address length field is there to be filled in, we fill
1105 * it in now.
1106 */
1107
1108 sll = (struct sockaddr_ll*)skb->cb;
1109 if (sock->type == SOCK_PACKET)
1110 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1111 else
1112 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1113
1114 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * You lose any data beyond the buffer you gave. If it worries a
1116 * user program they can ask the device for its MTU anyway.
1117 */
1118
1119 copied = skb->len;
1120 if (copied > len)
1121 {
1122 copied=len;
1123 msg->msg_flags|=MSG_TRUNC;
1124 }
1125
1126 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1127 if (err)
1128 goto out_free;
1129
1130 sock_recv_timestamp(msg, sk, skb);
1131
1132 if (msg->msg_name)
1133 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1134
1135 /*
1136 * Free or return the buffer as appropriate. Again this
1137 * hides all the races and re-entrancy issues from us.
1138 */
1139 err = (flags&MSG_TRUNC) ? skb->len : copied;
1140
1141out_free:
1142 skb_free_datagram(sk, skb);
1143out:
1144 return err;
1145}
1146
1147#ifdef CONFIG_SOCK_PACKET
1148static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1149 int *uaddr_len, int peer)
1150{
1151 struct net_device *dev;
1152 struct sock *sk = sock->sk;
1153
1154 if (peer)
1155 return -EOPNOTSUPP;
1156
1157 uaddr->sa_family = AF_PACKET;
1158 dev = dev_get_by_index(pkt_sk(sk)->ifindex);
1159 if (dev) {
1160 strlcpy(uaddr->sa_data, dev->name, 15);
1161 dev_put(dev);
1162 } else
1163 memset(uaddr->sa_data, 0, 14);
1164 *uaddr_len = sizeof(*uaddr);
1165
1166 return 0;
1167}
1168#endif
1169
1170static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1171 int *uaddr_len, int peer)
1172{
1173 struct net_device *dev;
1174 struct sock *sk = sock->sk;
1175 struct packet_sock *po = pkt_sk(sk);
1176 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1177
1178 if (peer)
1179 return -EOPNOTSUPP;
1180
1181 sll->sll_family = AF_PACKET;
1182 sll->sll_ifindex = po->ifindex;
1183 sll->sll_protocol = po->num;
1184 dev = dev_get_by_index(po->ifindex);
1185 if (dev) {
1186 sll->sll_hatype = dev->type;
1187 sll->sll_halen = dev->addr_len;
1188 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1189 dev_put(dev);
1190 } else {
1191 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1192 sll->sll_halen = 0;
1193 }
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001194 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 return 0;
1197}
1198
1199#ifdef CONFIG_PACKET_MULTICAST
1200static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1201{
1202 switch (i->type) {
1203 case PACKET_MR_MULTICAST:
1204 if (what > 0)
1205 dev_mc_add(dev, i->addr, i->alen, 0);
1206 else
1207 dev_mc_delete(dev, i->addr, i->alen, 0);
1208 break;
1209 case PACKET_MR_PROMISC:
1210 dev_set_promiscuity(dev, what);
1211 break;
1212 case PACKET_MR_ALLMULTI:
1213 dev_set_allmulti(dev, what);
1214 break;
1215 default:;
1216 }
1217}
1218
1219static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1220{
1221 for ( ; i; i=i->next) {
1222 if (i->ifindex == dev->ifindex)
1223 packet_dev_mc(dev, i, what);
1224 }
1225}
1226
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001227static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct packet_sock *po = pkt_sk(sk);
1230 struct packet_mclist *ml, *i;
1231 struct net_device *dev;
1232 int err;
1233
1234 rtnl_lock();
1235
1236 err = -ENODEV;
1237 dev = __dev_get_by_index(mreq->mr_ifindex);
1238 if (!dev)
1239 goto done;
1240
1241 err = -EINVAL;
1242 if (mreq->mr_alen > dev->addr_len)
1243 goto done;
1244
1245 err = -ENOBUFS;
1246 i = (struct packet_mclist *)kmalloc(sizeof(*i), GFP_KERNEL);
1247 if (i == NULL)
1248 goto done;
1249
1250 err = 0;
1251 for (ml = po->mclist; ml; ml = ml->next) {
1252 if (ml->ifindex == mreq->mr_ifindex &&
1253 ml->type == mreq->mr_type &&
1254 ml->alen == mreq->mr_alen &&
1255 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1256 ml->count++;
1257 /* Free the new element ... */
1258 kfree(i);
1259 goto done;
1260 }
1261 }
1262
1263 i->type = mreq->mr_type;
1264 i->ifindex = mreq->mr_ifindex;
1265 i->alen = mreq->mr_alen;
1266 memcpy(i->addr, mreq->mr_address, i->alen);
1267 i->count = 1;
1268 i->next = po->mclist;
1269 po->mclist = i;
1270 packet_dev_mc(dev, i, +1);
1271
1272done:
1273 rtnl_unlock();
1274 return err;
1275}
1276
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001277static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct packet_mclist *ml, **mlp;
1280
1281 rtnl_lock();
1282
1283 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1284 if (ml->ifindex == mreq->mr_ifindex &&
1285 ml->type == mreq->mr_type &&
1286 ml->alen == mreq->mr_alen &&
1287 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1288 if (--ml->count == 0) {
1289 struct net_device *dev;
1290 *mlp = ml->next;
1291 dev = dev_get_by_index(ml->ifindex);
1292 if (dev) {
1293 packet_dev_mc(dev, ml, -1);
1294 dev_put(dev);
1295 }
1296 kfree(ml);
1297 }
1298 rtnl_unlock();
1299 return 0;
1300 }
1301 }
1302 rtnl_unlock();
1303 return -EADDRNOTAVAIL;
1304}
1305
1306static void packet_flush_mclist(struct sock *sk)
1307{
1308 struct packet_sock *po = pkt_sk(sk);
1309 struct packet_mclist *ml;
1310
1311 if (!po->mclist)
1312 return;
1313
1314 rtnl_lock();
1315 while ((ml = po->mclist) != NULL) {
1316 struct net_device *dev;
1317
1318 po->mclist = ml->next;
1319 if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1320 packet_dev_mc(dev, ml, -1);
1321 dev_put(dev);
1322 }
1323 kfree(ml);
1324 }
1325 rtnl_unlock();
1326}
1327#endif
1328
1329static int
1330packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1331{
1332 struct sock *sk = sock->sk;
1333 int ret;
1334
1335 if (level != SOL_PACKET)
1336 return -ENOPROTOOPT;
1337
1338 switch(optname) {
1339#ifdef CONFIG_PACKET_MULTICAST
1340 case PACKET_ADD_MEMBERSHIP:
1341 case PACKET_DROP_MEMBERSHIP:
1342 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001343 struct packet_mreq_max mreq;
1344 int len = optlen;
1345 memset(&mreq, 0, sizeof(mreq));
1346 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001348 if (len > sizeof(mreq))
1349 len = sizeof(mreq);
1350 if (copy_from_user(&mreq,optval,len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001352 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (optname == PACKET_ADD_MEMBERSHIP)
1355 ret = packet_mc_add(sk, &mreq);
1356 else
1357 ret = packet_mc_drop(sk, &mreq);
1358 return ret;
1359 }
1360#endif
1361#ifdef CONFIG_PACKET_MMAP
1362 case PACKET_RX_RING:
1363 {
1364 struct tpacket_req req;
1365
1366 if (optlen<sizeof(req))
1367 return -EINVAL;
1368 if (copy_from_user(&req,optval,sizeof(req)))
1369 return -EFAULT;
1370 return packet_set_ring(sk, &req, 0);
1371 }
1372 case PACKET_COPY_THRESH:
1373 {
1374 int val;
1375
1376 if (optlen!=sizeof(val))
1377 return -EINVAL;
1378 if (copy_from_user(&val,optval,sizeof(val)))
1379 return -EFAULT;
1380
1381 pkt_sk(sk)->copy_thresh = val;
1382 return 0;
1383 }
1384#endif
1385 default:
1386 return -ENOPROTOOPT;
1387 }
1388}
1389
1390static int packet_getsockopt(struct socket *sock, int level, int optname,
1391 char __user *optval, int __user *optlen)
1392{
1393 int len;
1394 struct sock *sk = sock->sk;
1395 struct packet_sock *po = pkt_sk(sk);
1396
1397 if (level != SOL_PACKET)
1398 return -ENOPROTOOPT;
1399
1400 if (get_user(len,optlen))
1401 return -EFAULT;
1402
1403 if (len < 0)
1404 return -EINVAL;
1405
1406 switch(optname) {
1407 case PACKET_STATISTICS:
1408 {
1409 struct tpacket_stats st;
1410
1411 if (len > sizeof(struct tpacket_stats))
1412 len = sizeof(struct tpacket_stats);
1413 spin_lock_bh(&sk->sk_receive_queue.lock);
1414 st = po->stats;
1415 memset(&po->stats, 0, sizeof(st));
1416 spin_unlock_bh(&sk->sk_receive_queue.lock);
1417 st.tp_packets += st.tp_drops;
1418
1419 if (copy_to_user(optval, &st, len))
1420 return -EFAULT;
1421 break;
1422 }
1423 default:
1424 return -ENOPROTOOPT;
1425 }
1426
1427 if (put_user(len, optlen))
1428 return -EFAULT;
1429 return 0;
1430}
1431
1432
1433static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1434{
1435 struct sock *sk;
1436 struct hlist_node *node;
1437 struct net_device *dev = (struct net_device*)data;
1438
1439 read_lock(&packet_sklist_lock);
1440 sk_for_each(sk, node, &packet_sklist) {
1441 struct packet_sock *po = pkt_sk(sk);
1442
1443 switch (msg) {
1444 case NETDEV_UNREGISTER:
1445#ifdef CONFIG_PACKET_MULTICAST
1446 if (po->mclist)
1447 packet_dev_mclist(dev, po->mclist, -1);
1448 // fallthrough
1449#endif
1450 case NETDEV_DOWN:
1451 if (dev->ifindex == po->ifindex) {
1452 spin_lock(&po->bind_lock);
1453 if (po->running) {
1454 __dev_remove_pack(&po->prot_hook);
1455 __sock_put(sk);
1456 po->running = 0;
1457 sk->sk_err = ENETDOWN;
1458 if (!sock_flag(sk, SOCK_DEAD))
1459 sk->sk_error_report(sk);
1460 }
1461 if (msg == NETDEV_UNREGISTER) {
1462 po->ifindex = -1;
1463 po->prot_hook.dev = NULL;
1464 }
1465 spin_unlock(&po->bind_lock);
1466 }
1467 break;
1468 case NETDEV_UP:
1469 spin_lock(&po->bind_lock);
1470 if (dev->ifindex == po->ifindex && po->num &&
1471 !po->running) {
1472 dev_add_pack(&po->prot_hook);
1473 sock_hold(sk);
1474 po->running = 1;
1475 }
1476 spin_unlock(&po->bind_lock);
1477 break;
1478 }
1479 }
1480 read_unlock(&packet_sklist_lock);
1481 return NOTIFY_DONE;
1482}
1483
1484
1485static int packet_ioctl(struct socket *sock, unsigned int cmd,
1486 unsigned long arg)
1487{
1488 struct sock *sk = sock->sk;
1489
1490 switch(cmd) {
1491 case SIOCOUTQ:
1492 {
1493 int amount = atomic_read(&sk->sk_wmem_alloc);
1494 return put_user(amount, (int __user *)arg);
1495 }
1496 case SIOCINQ:
1497 {
1498 struct sk_buff *skb;
1499 int amount = 0;
1500
1501 spin_lock_bh(&sk->sk_receive_queue.lock);
1502 skb = skb_peek(&sk->sk_receive_queue);
1503 if (skb)
1504 amount = skb->len;
1505 spin_unlock_bh(&sk->sk_receive_queue.lock);
1506 return put_user(amount, (int __user *)arg);
1507 }
1508 case SIOCGSTAMP:
1509 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1510
1511#ifdef CONFIG_INET
1512 case SIOCADDRT:
1513 case SIOCDELRT:
1514 case SIOCDARP:
1515 case SIOCGARP:
1516 case SIOCSARP:
1517 case SIOCGIFADDR:
1518 case SIOCSIFADDR:
1519 case SIOCGIFBRDADDR:
1520 case SIOCSIFBRDADDR:
1521 case SIOCGIFNETMASK:
1522 case SIOCSIFNETMASK:
1523 case SIOCGIFDSTADDR:
1524 case SIOCSIFDSTADDR:
1525 case SIOCSIFFLAGS:
1526 return inet_dgram_ops.ioctl(sock, cmd, arg);
1527#endif
1528
1529 default:
1530 return dev_ioctl(cmd, (void __user *)arg);
1531 }
1532 return 0;
1533}
1534
1535#ifndef CONFIG_PACKET_MMAP
1536#define packet_mmap sock_no_mmap
1537#define packet_poll datagram_poll
1538#else
1539
1540static unsigned int packet_poll(struct file * file, struct socket *sock,
1541 poll_table *wait)
1542{
1543 struct sock *sk = sock->sk;
1544 struct packet_sock *po = pkt_sk(sk);
1545 unsigned int mask = datagram_poll(file, sock, wait);
1546
1547 spin_lock_bh(&sk->sk_receive_queue.lock);
1548 if (po->pg_vec) {
1549 unsigned last = po->head ? po->head-1 : po->frame_max;
1550 struct tpacket_hdr *h;
1551
1552 h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
1553
1554 if (h->tp_status)
1555 mask |= POLLIN | POLLRDNORM;
1556 }
1557 spin_unlock_bh(&sk->sk_receive_queue.lock);
1558 return mask;
1559}
1560
1561
1562/* Dirty? Well, I still did not learn better way to account
1563 * for user mmaps.
1564 */
1565
1566static void packet_mm_open(struct vm_area_struct *vma)
1567{
1568 struct file *file = vma->vm_file;
Eric Dumazetb69aee02005-09-06 14:42:45 -07001569 struct socket * sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct sock *sk = sock->sk;
1571
1572 if (sk)
1573 atomic_inc(&pkt_sk(sk)->mapped);
1574}
1575
1576static void packet_mm_close(struct vm_area_struct *vma)
1577{
1578 struct file *file = vma->vm_file;
Eric Dumazetb69aee02005-09-06 14:42:45 -07001579 struct socket * sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 struct sock *sk = sock->sk;
1581
1582 if (sk)
1583 atomic_dec(&pkt_sk(sk)->mapped);
1584}
1585
1586static struct vm_operations_struct packet_mmap_ops = {
1587 .open = packet_mm_open,
1588 .close =packet_mm_close,
1589};
1590
1591static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
1592{
1593 return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
1594}
1595
1596static void free_pg_vec(char **pg_vec, unsigned order, unsigned len)
1597{
1598 int i;
1599
1600 for (i=0; i<len; i++) {
1601 if (pg_vec[i]) {
1602 struct page *page, *pend;
1603
1604 pend = pg_vec_endpage(pg_vec[i], order);
1605 for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1606 ClearPageReserved(page);
1607 free_pages((unsigned long)pg_vec[i], order);
1608 }
1609 }
1610 kfree(pg_vec);
1611}
1612
1613
1614static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1615{
1616 char **pg_vec = NULL;
1617 struct packet_sock *po = pkt_sk(sk);
1618 int was_running, num, order = 0;
1619 int err = 0;
1620
1621 if (req->tp_block_nr) {
1622 int i, l;
1623
1624 /* Sanity tests and some calculations */
1625
1626 if (po->pg_vec)
1627 return -EBUSY;
1628
1629 if ((int)req->tp_block_size <= 0)
1630 return -EINVAL;
1631 if (req->tp_block_size&(PAGE_SIZE-1))
1632 return -EINVAL;
1633 if (req->tp_frame_size < TPACKET_HDRLEN)
1634 return -EINVAL;
1635 if (req->tp_frame_size&(TPACKET_ALIGNMENT-1))
1636 return -EINVAL;
1637
1638 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1639 if (po->frames_per_block <= 0)
1640 return -EINVAL;
1641 if (po->frames_per_block*req->tp_block_nr != req->tp_frame_nr)
1642 return -EINVAL;
1643 /* OK! */
1644
1645 /* Allocate page vector */
1646 while ((PAGE_SIZE<<order) < req->tp_block_size)
1647 order++;
1648
1649 err = -ENOMEM;
1650
1651 pg_vec = kmalloc(req->tp_block_nr*sizeof(char *), GFP_KERNEL);
1652 if (pg_vec == NULL)
1653 goto out;
1654 memset(pg_vec, 0, req->tp_block_nr*sizeof(char **));
1655
1656 for (i=0; i<req->tp_block_nr; i++) {
1657 struct page *page, *pend;
1658 pg_vec[i] = (char *)__get_free_pages(GFP_KERNEL, order);
1659 if (!pg_vec[i])
1660 goto out_free_pgvec;
1661
1662 pend = pg_vec_endpage(pg_vec[i], order);
1663 for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1664 SetPageReserved(page);
1665 }
1666 /* Page vector is allocated */
1667
1668 l = 0;
1669 for (i=0; i<req->tp_block_nr; i++) {
1670 char *ptr = pg_vec[i];
1671 struct tpacket_hdr *header;
1672 int k;
1673
1674 for (k=0; k<po->frames_per_block; k++) {
1675
1676 header = (struct tpacket_hdr*)ptr;
1677 header->tp_status = TP_STATUS_KERNEL;
1678 ptr += req->tp_frame_size;
1679 }
1680 }
1681 /* Done */
1682 } else {
1683 if (req->tp_frame_nr)
1684 return -EINVAL;
1685 }
1686
1687 lock_sock(sk);
1688
1689 /* Detach socket from network */
1690 spin_lock(&po->bind_lock);
1691 was_running = po->running;
1692 num = po->num;
1693 if (was_running) {
1694 __dev_remove_pack(&po->prot_hook);
1695 po->num = 0;
1696 po->running = 0;
1697 __sock_put(sk);
1698 }
1699 spin_unlock(&po->bind_lock);
1700
1701 synchronize_net();
1702
1703 err = -EBUSY;
1704 if (closing || atomic_read(&po->mapped) == 0) {
1705 err = 0;
1706#define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1707
1708 spin_lock_bh(&sk->sk_receive_queue.lock);
1709 pg_vec = XC(po->pg_vec, pg_vec);
1710 po->frame_max = req->tp_frame_nr-1;
1711 po->head = 0;
1712 po->frame_size = req->tp_frame_size;
1713 spin_unlock_bh(&sk->sk_receive_queue.lock);
1714
1715 order = XC(po->pg_vec_order, order);
1716 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1717
1718 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1719 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1720 skb_queue_purge(&sk->sk_receive_queue);
1721#undef XC
1722 if (atomic_read(&po->mapped))
1723 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1724 }
1725
1726 spin_lock(&po->bind_lock);
1727 if (was_running && !po->running) {
1728 sock_hold(sk);
1729 po->running = 1;
1730 po->num = num;
1731 dev_add_pack(&po->prot_hook);
1732 }
1733 spin_unlock(&po->bind_lock);
1734
1735 release_sock(sk);
1736
1737out_free_pgvec:
1738 if (pg_vec)
1739 free_pg_vec(pg_vec, order, req->tp_block_nr);
1740out:
1741 return err;
1742}
1743
1744static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1745{
1746 struct sock *sk = sock->sk;
1747 struct packet_sock *po = pkt_sk(sk);
1748 unsigned long size;
1749 unsigned long start;
1750 int err = -EINVAL;
1751 int i;
1752
1753 if (vma->vm_pgoff)
1754 return -EINVAL;
1755
1756 size = vma->vm_end - vma->vm_start;
1757
1758 lock_sock(sk);
1759 if (po->pg_vec == NULL)
1760 goto out;
1761 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1762 goto out;
1763
1764 atomic_inc(&po->mapped);
1765 start = vma->vm_start;
1766 err = -EAGAIN;
1767 for (i=0; i<po->pg_vec_len; i++) {
1768 if (remap_pfn_range(vma, start,
1769 __pa(po->pg_vec[i]) >> PAGE_SHIFT,
1770 po->pg_vec_pages*PAGE_SIZE,
1771 vma->vm_page_prot))
1772 goto out;
1773 start += po->pg_vec_pages*PAGE_SIZE;
1774 }
1775 vma->vm_ops = &packet_mmap_ops;
1776 err = 0;
1777
1778out:
1779 release_sock(sk);
1780 return err;
1781}
1782#endif
1783
1784
1785#ifdef CONFIG_SOCK_PACKET
1786static struct proto_ops packet_ops_spkt = {
1787 .family = PF_PACKET,
1788 .owner = THIS_MODULE,
1789 .release = packet_release,
1790 .bind = packet_bind_spkt,
1791 .connect = sock_no_connect,
1792 .socketpair = sock_no_socketpair,
1793 .accept = sock_no_accept,
1794 .getname = packet_getname_spkt,
1795 .poll = datagram_poll,
1796 .ioctl = packet_ioctl,
1797 .listen = sock_no_listen,
1798 .shutdown = sock_no_shutdown,
1799 .setsockopt = sock_no_setsockopt,
1800 .getsockopt = sock_no_getsockopt,
1801 .sendmsg = packet_sendmsg_spkt,
1802 .recvmsg = packet_recvmsg,
1803 .mmap = sock_no_mmap,
1804 .sendpage = sock_no_sendpage,
1805};
1806#endif
1807
1808static struct proto_ops packet_ops = {
1809 .family = PF_PACKET,
1810 .owner = THIS_MODULE,
1811 .release = packet_release,
1812 .bind = packet_bind,
1813 .connect = sock_no_connect,
1814 .socketpair = sock_no_socketpair,
1815 .accept = sock_no_accept,
1816 .getname = packet_getname,
1817 .poll = packet_poll,
1818 .ioctl = packet_ioctl,
1819 .listen = sock_no_listen,
1820 .shutdown = sock_no_shutdown,
1821 .setsockopt = packet_setsockopt,
1822 .getsockopt = packet_getsockopt,
1823 .sendmsg = packet_sendmsg,
1824 .recvmsg = packet_recvmsg,
1825 .mmap = packet_mmap,
1826 .sendpage = sock_no_sendpage,
1827};
1828
1829static struct net_proto_family packet_family_ops = {
1830 .family = PF_PACKET,
1831 .create = packet_create,
1832 .owner = THIS_MODULE,
1833};
1834
1835static struct notifier_block packet_netdev_notifier = {
1836 .notifier_call =packet_notifier,
1837};
1838
1839#ifdef CONFIG_PROC_FS
1840static inline struct sock *packet_seq_idx(loff_t off)
1841{
1842 struct sock *s;
1843 struct hlist_node *node;
1844
1845 sk_for_each(s, node, &packet_sklist) {
1846 if (!off--)
1847 return s;
1848 }
1849 return NULL;
1850}
1851
1852static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1853{
1854 read_lock(&packet_sklist_lock);
1855 return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1856}
1857
1858static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1859{
1860 ++*pos;
1861 return (v == SEQ_START_TOKEN)
1862 ? sk_head(&packet_sklist)
1863 : sk_next((struct sock*)v) ;
1864}
1865
1866static void packet_seq_stop(struct seq_file *seq, void *v)
1867{
1868 read_unlock(&packet_sklist_lock);
1869}
1870
1871static int packet_seq_show(struct seq_file *seq, void *v)
1872{
1873 if (v == SEQ_START_TOKEN)
1874 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1875 else {
1876 struct sock *s = v;
1877 const struct packet_sock *po = pkt_sk(s);
1878
1879 seq_printf(seq,
1880 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1881 s,
1882 atomic_read(&s->sk_refcnt),
1883 s->sk_type,
1884 ntohs(po->num),
1885 po->ifindex,
1886 po->running,
1887 atomic_read(&s->sk_rmem_alloc),
1888 sock_i_uid(s),
1889 sock_i_ino(s) );
1890 }
1891
1892 return 0;
1893}
1894
1895static struct seq_operations packet_seq_ops = {
1896 .start = packet_seq_start,
1897 .next = packet_seq_next,
1898 .stop = packet_seq_stop,
1899 .show = packet_seq_show,
1900};
1901
1902static int packet_seq_open(struct inode *inode, struct file *file)
1903{
1904 return seq_open(file, &packet_seq_ops);
1905}
1906
1907static struct file_operations packet_seq_fops = {
1908 .owner = THIS_MODULE,
1909 .open = packet_seq_open,
1910 .read = seq_read,
1911 .llseek = seq_lseek,
1912 .release = seq_release,
1913};
1914
1915#endif
1916
1917static void __exit packet_exit(void)
1918{
1919 proc_net_remove("packet");
1920 unregister_netdevice_notifier(&packet_netdev_notifier);
1921 sock_unregister(PF_PACKET);
1922 proto_unregister(&packet_proto);
1923}
1924
1925static int __init packet_init(void)
1926{
1927 int rc = proto_register(&packet_proto, 0);
1928
1929 if (rc != 0)
1930 goto out;
1931
1932 sock_register(&packet_family_ops);
1933 register_netdevice_notifier(&packet_netdev_notifier);
1934 proc_net_fops_create("packet", 0, &packet_seq_fops);
1935out:
1936 return rc;
1937}
1938
1939module_init(packet_init);
1940module_exit(packet_exit);
1941MODULE_LICENSE("GPL");
1942MODULE_ALIAS_NETPROTO(PF_PACKET);