Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / packet / af_packet.c
blobb5362e96022b84259739770ff87114ccbe743bae
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.
6 * PACKET - implements raw packet sockets.
8 * Authors: Ross Biro
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
12 * Fixes:
13 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
35 * Ulises Alonso : Frame number limit removal and
36 * packet_set_ring memory leak.
37 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
40 * byte arrays at the end of sockaddr_ll
41 * and packet_mreq.
42 * Johann Baudy : Added TX RING.
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
51 #include <linux/types.h>
52 #include <linux/mm.h>
53 #include <linux/capability.h>
54 #include <linux/fcntl.h>
55 #include <linux/socket.h>
56 #include <linux/in.h>
57 #include <linux/inet.h>
58 #include <linux/netdevice.h>
59 #include <linux/if_packet.h>
60 #include <linux/wireless.h>
61 #include <linux/kernel.h>
62 #include <linux/kmod.h>
63 #include <linux/slab.h>
64 #include <linux/vmalloc.h>
65 #include <net/net_namespace.h>
66 #include <net/ip.h>
67 #include <net/protocol.h>
68 #include <linux/skbuff.h>
69 #include <net/sock.h>
70 #include <linux/errno.h>
71 #include <linux/timer.h>
72 #include <asm/system.h>
73 #include <asm/uaccess.h>
74 #include <asm/ioctls.h>
75 #include <asm/page.h>
76 #include <asm/cacheflush.h>
77 #include <asm/io.h>
78 #include <linux/proc_fs.h>
79 #include <linux/seq_file.h>
80 #include <linux/poll.h>
81 #include <linux/module.h>
82 #include <linux/init.h>
83 #include <linux/mutex.h>
84 #include <linux/if_vlan.h>
85 #include <linux/virtio_net.h>
86 #include <linux/errqueue.h>
87 #include <linux/net_tstamp.h>
89 #ifdef CONFIG_INET
90 #include <net/inet_common.h>
91 #endif
94 Assumptions:
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
100 (PPP).
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
104 On receive:
105 -----------
107 Incoming, dev->hard_header!=NULL
108 mac_header -> ll header
109 data -> data
111 Outgoing, dev->hard_header!=NULL
112 mac_header -> ll header
113 data -> ll header
115 Incoming, dev->hard_header==NULL
116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
118 assymetry between rx and tx paths.
119 data -> data
121 Outgoing, dev->hard_header==NULL
122 mac_header -> data. ll header is still not built!
123 data -> data
125 Resume
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
129 On transmit:
130 ------------
132 dev->hard_header != NULL
133 mac_header -> ll header
134 data -> ll header
136 dev->hard_header == NULL (ll header is added by device, we cannot control it)
137 mac_header -> data
138 data -> data
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
144 /* Private packet socket structures. */
146 struct packet_mclist {
147 struct packet_mclist *next;
148 int ifindex;
149 int count;
150 unsigned short type;
151 unsigned short alen;
152 unsigned char addr[MAX_ADDR_LEN];
154 /* identical to struct packet_mreq except it has
155 * a longer address field.
157 struct packet_mreq_max {
158 int mr_ifindex;
159 unsigned short mr_type;
160 unsigned short mr_alen;
161 unsigned char mr_address[MAX_ADDR_LEN];
164 static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
165 int closing, int tx_ring);
167 struct pgv {
168 char *buffer;
171 struct packet_ring_buffer {
172 struct pgv *pg_vec;
173 unsigned int head;
174 unsigned int frames_per_block;
175 unsigned int frame_size;
176 unsigned int frame_max;
178 unsigned int pg_vec_order;
179 unsigned int pg_vec_pages;
180 unsigned int pg_vec_len;
182 atomic_t pending;
185 struct packet_sock;
186 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
188 static void packet_flush_mclist(struct sock *sk);
190 struct packet_sock {
191 /* struct sock has to be the first member of packet_sock */
192 struct sock sk;
193 struct tpacket_stats stats;
194 struct packet_ring_buffer rx_ring;
195 struct packet_ring_buffer tx_ring;
196 int copy_thresh;
197 spinlock_t bind_lock;
198 struct mutex pg_vec_lock;
199 unsigned int running:1, /* prot_hook is attached*/
200 auxdata:1,
201 origdev:1,
202 has_vnet_hdr:1;
203 int ifindex; /* bound device */
204 __be16 num;
205 struct packet_mclist *mclist;
206 atomic_t mapped;
207 enum tpacket_versions tp_version;
208 unsigned int tp_hdrlen;
209 unsigned int tp_reserve;
210 unsigned int tp_loss:1;
211 unsigned int tp_tstamp;
212 struct packet_type prot_hook ____cacheline_aligned_in_smp;
215 struct packet_skb_cb {
216 unsigned int origlen;
217 union {
218 struct sockaddr_pkt pkt;
219 struct sockaddr_ll ll;
220 } sa;
223 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
225 static inline __pure struct page *pgv_to_page(void *addr)
227 if (is_vmalloc_addr(addr))
228 return vmalloc_to_page(addr);
229 return virt_to_page(addr);
232 static void __packet_set_status(struct packet_sock *po, void *frame, int status)
234 union {
235 struct tpacket_hdr *h1;
236 struct tpacket2_hdr *h2;
237 void *raw;
238 } h;
240 h.raw = frame;
241 switch (po->tp_version) {
242 case TPACKET_V1:
243 h.h1->tp_status = status;
244 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
245 break;
246 case TPACKET_V2:
247 h.h2->tp_status = status;
248 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
249 break;
250 default:
251 pr_err("TPACKET version not supported\n");
252 BUG();
255 smp_wmb();
258 static int __packet_get_status(struct packet_sock *po, void *frame)
260 union {
261 struct tpacket_hdr *h1;
262 struct tpacket2_hdr *h2;
263 void *raw;
264 } h;
266 smp_rmb();
268 h.raw = frame;
269 switch (po->tp_version) {
270 case TPACKET_V1:
271 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
272 return h.h1->tp_status;
273 case TPACKET_V2:
274 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
275 return h.h2->tp_status;
276 default:
277 pr_err("TPACKET version not supported\n");
278 BUG();
279 return 0;
283 static void *packet_lookup_frame(struct packet_sock *po,
284 struct packet_ring_buffer *rb,
285 unsigned int position,
286 int status)
288 unsigned int pg_vec_pos, frame_offset;
289 union {
290 struct tpacket_hdr *h1;
291 struct tpacket2_hdr *h2;
292 void *raw;
293 } h;
295 pg_vec_pos = position / rb->frames_per_block;
296 frame_offset = position % rb->frames_per_block;
298 h.raw = rb->pg_vec[pg_vec_pos].buffer +
299 (frame_offset * rb->frame_size);
301 if (status != __packet_get_status(po, h.raw))
302 return NULL;
304 return h.raw;
307 static inline void *packet_current_frame(struct packet_sock *po,
308 struct packet_ring_buffer *rb,
309 int status)
311 return packet_lookup_frame(po, rb, rb->head, status);
314 static inline void *packet_previous_frame(struct packet_sock *po,
315 struct packet_ring_buffer *rb,
316 int status)
318 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
319 return packet_lookup_frame(po, rb, previous, status);
322 static inline void packet_increment_head(struct packet_ring_buffer *buff)
324 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
327 static inline struct packet_sock *pkt_sk(struct sock *sk)
329 return (struct packet_sock *)sk;
332 static void packet_sock_destruct(struct sock *sk)
334 skb_queue_purge(&sk->sk_error_queue);
336 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
337 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
339 if (!sock_flag(sk, SOCK_DEAD)) {
340 pr_err("Attempt to release alive packet socket: %p\n", sk);
341 return;
344 sk_refcnt_debug_dec(sk);
348 static const struct proto_ops packet_ops;
350 static const struct proto_ops packet_ops_spkt;
352 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
353 struct packet_type *pt, struct net_device *orig_dev)
355 struct sock *sk;
356 struct sockaddr_pkt *spkt;
359 * When we registered the protocol we saved the socket in the data
360 * field for just this event.
363 sk = pt->af_packet_priv;
366 * Yank back the headers [hope the device set this
367 * right or kerboom...]
369 * Incoming packets have ll header pulled,
370 * push it back.
372 * For outgoing ones skb->data == skb_mac_header(skb)
373 * so that this procedure is noop.
376 if (skb->pkt_type == PACKET_LOOPBACK)
377 goto out;
379 if (!net_eq(dev_net(dev), sock_net(sk)))
380 goto out;
382 skb = skb_share_check(skb, GFP_ATOMIC);
383 if (skb == NULL)
384 goto oom;
386 /* drop any routing info */
387 skb_dst_drop(skb);
389 /* drop conntrack reference */
390 nf_reset(skb);
392 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
394 skb_push(skb, skb->data - skb_mac_header(skb));
397 * The SOCK_PACKET socket receives _all_ frames.
400 spkt->spkt_family = dev->type;
401 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
402 spkt->spkt_protocol = skb->protocol;
405 * Charge the memory to the socket. This is done specifically
406 * to prevent sockets using all the memory up.
409 if (sock_queue_rcv_skb(sk, skb) == 0)
410 return 0;
412 out:
413 kfree_skb(skb);
414 oom:
415 return 0;
420 * Output a raw packet to a device layer. This bypasses all the other
421 * protocol layers and you must therefore supply it with a complete frame
424 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
425 struct msghdr *msg, size_t len)
427 struct sock *sk = sock->sk;
428 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
429 struct sk_buff *skb = NULL;
430 struct net_device *dev;
431 __be16 proto = 0;
432 int err;
435 * Get and verify the address.
438 if (saddr) {
439 if (msg->msg_namelen < sizeof(struct sockaddr))
440 return -EINVAL;
441 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
442 proto = saddr->spkt_protocol;
443 } else
444 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
447 * Find the device first to size check it
450 saddr->spkt_device[13] = 0;
451 retry:
452 rcu_read_lock();
453 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
454 err = -ENODEV;
455 if (dev == NULL)
456 goto out_unlock;
458 err = -ENETDOWN;
459 if (!(dev->flags & IFF_UP))
460 goto out_unlock;
463 * You may not queue a frame bigger than the mtu. This is the lowest level
464 * raw protocol and you must do your own fragmentation at this level.
467 err = -EMSGSIZE;
468 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
469 goto out_unlock;
471 if (!skb) {
472 size_t reserved = LL_RESERVED_SPACE(dev);
473 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
475 rcu_read_unlock();
476 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
477 if (skb == NULL)
478 return -ENOBUFS;
479 /* FIXME: Save some space for broken drivers that write a hard
480 * header at transmission time by themselves. PPP is the notable
481 * one here. This should really be fixed at the driver level.
483 skb_reserve(skb, reserved);
484 skb_reset_network_header(skb);
486 /* Try to align data part correctly */
487 if (hhlen) {
488 skb->data -= hhlen;
489 skb->tail -= hhlen;
490 if (len < hhlen)
491 skb_reset_network_header(skb);
493 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
494 if (err)
495 goto out_free;
496 goto retry;
499 if (len > (dev->mtu + dev->hard_header_len)) {
500 /* Earlier code assumed this would be a VLAN pkt,
501 * double-check this now that we have the actual
502 * packet in hand.
504 struct ethhdr *ehdr;
505 skb_reset_mac_header(skb);
506 ehdr = eth_hdr(skb);
507 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
508 err = -EMSGSIZE;
509 goto out_unlock;
513 skb->protocol = proto;
514 skb->dev = dev;
515 skb->priority = sk->sk_priority;
516 skb->mark = sk->sk_mark;
517 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
518 if (err < 0)
519 goto out_unlock;
521 dev_queue_xmit(skb);
522 rcu_read_unlock();
523 return len;
525 out_unlock:
526 rcu_read_unlock();
527 out_free:
528 kfree_skb(skb);
529 return err;
532 static inline unsigned int run_filter(const struct sk_buff *skb,
533 const struct sock *sk,
534 unsigned int res)
536 struct sk_filter *filter;
538 rcu_read_lock();
539 filter = rcu_dereference(sk->sk_filter);
540 if (filter != NULL)
541 res = sk_run_filter(skb, filter->insns);
542 rcu_read_unlock();
544 return res;
548 * This function makes lazy skb cloning in hope that most of packets
549 * are discarded by BPF.
551 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
552 * and skb->cb are mangled. It works because (and until) packets
553 * falling here are owned by current CPU. Output packets are cloned
554 * by dev_queue_xmit_nit(), input packets are processed by net_bh
555 * sequencially, so that if we return skb to original state on exit,
556 * we will not harm anyone.
559 static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
560 struct packet_type *pt, struct net_device *orig_dev)
562 struct sock *sk;
563 struct sockaddr_ll *sll;
564 struct packet_sock *po;
565 u8 *skb_head = skb->data;
566 int skb_len = skb->len;
567 unsigned int snaplen, res;
569 if (skb->pkt_type == PACKET_LOOPBACK)
570 goto drop;
572 sk = pt->af_packet_priv;
573 po = pkt_sk(sk);
575 if (!net_eq(dev_net(dev), sock_net(sk)))
576 goto drop;
578 skb->dev = dev;
580 if (dev->header_ops) {
581 /* The device has an explicit notion of ll header,
582 * exported to higher levels.
584 * Otherwise, the device hides details of its frame
585 * structure, so that corresponding packet head is
586 * never delivered to user.
588 if (sk->sk_type != SOCK_DGRAM)
589 skb_push(skb, skb->data - skb_mac_header(skb));
590 else if (skb->pkt_type == PACKET_OUTGOING) {
591 /* Special case: outgoing packets have ll header at head */
592 skb_pull(skb, skb_network_offset(skb));
596 snaplen = skb->len;
598 res = run_filter(skb, sk, snaplen);
599 if (!res)
600 goto drop_n_restore;
601 if (snaplen > res)
602 snaplen = res;
604 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
605 (unsigned)sk->sk_rcvbuf)
606 goto drop_n_acct;
608 if (skb_shared(skb)) {
609 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
610 if (nskb == NULL)
611 goto drop_n_acct;
613 if (skb_head != skb->data) {
614 skb->data = skb_head;
615 skb->len = skb_len;
617 kfree_skb(skb);
618 skb = nskb;
621 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
622 sizeof(skb->cb));
624 sll = &PACKET_SKB_CB(skb)->sa.ll;
625 sll->sll_family = AF_PACKET;
626 sll->sll_hatype = dev->type;
627 sll->sll_protocol = skb->protocol;
628 sll->sll_pkttype = skb->pkt_type;
629 if (unlikely(po->origdev))
630 sll->sll_ifindex = orig_dev->ifindex;
631 else
632 sll->sll_ifindex = dev->ifindex;
634 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
636 PACKET_SKB_CB(skb)->origlen = skb->len;
638 if (pskb_trim(skb, snaplen))
639 goto drop_n_acct;
641 skb_set_owner_r(skb, sk);
642 skb->dev = NULL;
643 skb_dst_drop(skb);
645 /* drop conntrack reference */
646 nf_reset(skb);
648 spin_lock(&sk->sk_receive_queue.lock);
649 po->stats.tp_packets++;
650 skb->dropcount = atomic_read(&sk->sk_drops);
651 __skb_queue_tail(&sk->sk_receive_queue, skb);
652 spin_unlock(&sk->sk_receive_queue.lock);
653 sk->sk_data_ready(sk, skb->len);
654 return 0;
656 drop_n_acct:
657 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
659 drop_n_restore:
660 if (skb_head != skb->data && skb_shared(skb)) {
661 skb->data = skb_head;
662 skb->len = skb_len;
664 drop:
665 consume_skb(skb);
666 return 0;
669 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
670 struct packet_type *pt, struct net_device *orig_dev)
672 struct sock *sk;
673 struct packet_sock *po;
674 struct sockaddr_ll *sll;
675 union {
676 struct tpacket_hdr *h1;
677 struct tpacket2_hdr *h2;
678 void *raw;
679 } h;
680 u8 *skb_head = skb->data;
681 int skb_len = skb->len;
682 unsigned int snaplen, res;
683 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
684 unsigned short macoff, netoff, hdrlen;
685 struct sk_buff *copy_skb = NULL;
686 struct timeval tv;
687 struct timespec ts;
688 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
690 if (skb->pkt_type == PACKET_LOOPBACK)
691 goto drop;
693 sk = pt->af_packet_priv;
694 po = pkt_sk(sk);
696 if (!net_eq(dev_net(dev), sock_net(sk)))
697 goto drop;
699 if (dev->header_ops) {
700 if (sk->sk_type != SOCK_DGRAM)
701 skb_push(skb, skb->data - skb_mac_header(skb));
702 else if (skb->pkt_type == PACKET_OUTGOING) {
703 /* Special case: outgoing packets have ll header at head */
704 skb_pull(skb, skb_network_offset(skb));
708 if (skb->ip_summed == CHECKSUM_PARTIAL)
709 status |= TP_STATUS_CSUMNOTREADY;
711 snaplen = skb->len;
713 res = run_filter(skb, sk, snaplen);
714 if (!res)
715 goto drop_n_restore;
716 if (snaplen > res)
717 snaplen = res;
719 if (sk->sk_type == SOCK_DGRAM) {
720 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
721 po->tp_reserve;
722 } else {
723 unsigned maclen = skb_network_offset(skb);
724 netoff = TPACKET_ALIGN(po->tp_hdrlen +
725 (maclen < 16 ? 16 : maclen)) +
726 po->tp_reserve;
727 macoff = netoff - maclen;
730 if (macoff + snaplen > po->rx_ring.frame_size) {
731 if (po->copy_thresh &&
732 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
733 (unsigned)sk->sk_rcvbuf) {
734 if (skb_shared(skb)) {
735 copy_skb = skb_clone(skb, GFP_ATOMIC);
736 } else {
737 copy_skb = skb_get(skb);
738 skb_head = skb->data;
740 if (copy_skb)
741 skb_set_owner_r(copy_skb, sk);
743 snaplen = po->rx_ring.frame_size - macoff;
744 if ((int)snaplen < 0)
745 snaplen = 0;
748 spin_lock(&sk->sk_receive_queue.lock);
749 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
750 if (!h.raw)
751 goto ring_is_full;
752 packet_increment_head(&po->rx_ring);
753 po->stats.tp_packets++;
754 if (copy_skb) {
755 status |= TP_STATUS_COPY;
756 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
758 if (!po->stats.tp_drops)
759 status &= ~TP_STATUS_LOSING;
760 spin_unlock(&sk->sk_receive_queue.lock);
762 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
764 switch (po->tp_version) {
765 case TPACKET_V1:
766 h.h1->tp_len = skb->len;
767 h.h1->tp_snaplen = snaplen;
768 h.h1->tp_mac = macoff;
769 h.h1->tp_net = netoff;
770 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
771 && shhwtstamps->syststamp.tv64)
772 tv = ktime_to_timeval(shhwtstamps->syststamp);
773 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
774 && shhwtstamps->hwtstamp.tv64)
775 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
776 else if (skb->tstamp.tv64)
777 tv = ktime_to_timeval(skb->tstamp);
778 else
779 do_gettimeofday(&tv);
780 h.h1->tp_sec = tv.tv_sec;
781 h.h1->tp_usec = tv.tv_usec;
782 hdrlen = sizeof(*h.h1);
783 break;
784 case TPACKET_V2:
785 h.h2->tp_len = skb->len;
786 h.h2->tp_snaplen = snaplen;
787 h.h2->tp_mac = macoff;
788 h.h2->tp_net = netoff;
789 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
790 && shhwtstamps->syststamp.tv64)
791 ts = ktime_to_timespec(shhwtstamps->syststamp);
792 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
793 && shhwtstamps->hwtstamp.tv64)
794 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
795 else if (skb->tstamp.tv64)
796 ts = ktime_to_timespec(skb->tstamp);
797 else
798 getnstimeofday(&ts);
799 h.h2->tp_sec = ts.tv_sec;
800 h.h2->tp_nsec = ts.tv_nsec;
801 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
802 hdrlen = sizeof(*h.h2);
803 break;
804 default:
805 BUG();
808 sll = h.raw + TPACKET_ALIGN(hdrlen);
809 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
810 sll->sll_family = AF_PACKET;
811 sll->sll_hatype = dev->type;
812 sll->sll_protocol = skb->protocol;
813 sll->sll_pkttype = skb->pkt_type;
814 if (unlikely(po->origdev))
815 sll->sll_ifindex = orig_dev->ifindex;
816 else
817 sll->sll_ifindex = dev->ifindex;
819 __packet_set_status(po, h.raw, status);
820 smp_mb();
821 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
823 u8 *start, *end;
825 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
826 for (start = h.raw; start < end; start += PAGE_SIZE)
827 flush_dcache_page(pgv_to_page(start));
829 #endif
831 sk->sk_data_ready(sk, 0);
833 drop_n_restore:
834 if (skb_head != skb->data && skb_shared(skb)) {
835 skb->data = skb_head;
836 skb->len = skb_len;
838 drop:
839 kfree_skb(skb);
840 return 0;
842 ring_is_full:
843 po->stats.tp_drops++;
844 spin_unlock(&sk->sk_receive_queue.lock);
846 sk->sk_data_ready(sk, 0);
847 kfree_skb(copy_skb);
848 goto drop_n_restore;
851 static void tpacket_destruct_skb(struct sk_buff *skb)
853 struct packet_sock *po = pkt_sk(skb->sk);
854 void *ph;
856 BUG_ON(skb == NULL);
858 if (likely(po->tx_ring.pg_vec)) {
859 ph = skb_shinfo(skb)->destructor_arg;
860 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
861 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
862 atomic_dec(&po->tx_ring.pending);
863 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
866 sock_wfree(skb);
869 static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
870 void *frame, struct net_device *dev, int size_max,
871 __be16 proto, unsigned char *addr)
873 union {
874 struct tpacket_hdr *h1;
875 struct tpacket2_hdr *h2;
876 void *raw;
877 } ph;
878 int to_write, offset, len, tp_len, nr_frags, len_max;
879 struct socket *sock = po->sk.sk_socket;
880 struct page *page;
881 void *data;
882 int err;
884 ph.raw = frame;
886 skb->protocol = proto;
887 skb->dev = dev;
888 skb->priority = po->sk.sk_priority;
889 skb->mark = po->sk.sk_mark;
890 skb_shinfo(skb)->destructor_arg = ph.raw;
892 switch (po->tp_version) {
893 case TPACKET_V2:
894 tp_len = ph.h2->tp_len;
895 break;
896 default:
897 tp_len = ph.h1->tp_len;
898 break;
900 if (unlikely(tp_len > size_max)) {
901 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
902 return -EMSGSIZE;
905 skb_reserve(skb, LL_RESERVED_SPACE(dev));
906 skb_reset_network_header(skb);
908 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
909 to_write = tp_len;
911 if (sock->type == SOCK_DGRAM) {
912 err = dev_hard_header(skb, dev, ntohs(proto), addr,
913 NULL, tp_len);
914 if (unlikely(err < 0))
915 return -EINVAL;
916 } else if (dev->hard_header_len) {
917 /* net device doesn't like empty head */
918 if (unlikely(tp_len <= dev->hard_header_len)) {
919 pr_err("packet size is too short (%d < %d)\n",
920 tp_len, dev->hard_header_len);
921 return -EINVAL;
924 skb_push(skb, dev->hard_header_len);
925 err = skb_store_bits(skb, 0, data,
926 dev->hard_header_len);
927 if (unlikely(err))
928 return err;
930 data += dev->hard_header_len;
931 to_write -= dev->hard_header_len;
934 err = -EFAULT;
935 offset = offset_in_page(data);
936 len_max = PAGE_SIZE - offset;
937 len = ((to_write > len_max) ? len_max : to_write);
939 skb->data_len = to_write;
940 skb->len += to_write;
941 skb->truesize += to_write;
942 atomic_add(to_write, &po->sk.sk_wmem_alloc);
944 while (likely(to_write)) {
945 nr_frags = skb_shinfo(skb)->nr_frags;
947 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
948 pr_err("Packet exceed the number of skb frags(%lu)\n",
949 MAX_SKB_FRAGS);
950 return -EFAULT;
953 page = pgv_to_page(data);
954 data += len;
955 flush_dcache_page(page);
956 get_page(page);
957 skb_fill_page_desc(skb, nr_frags, page, offset, len);
958 to_write -= len;
959 offset = 0;
960 len_max = PAGE_SIZE;
961 len = ((to_write > len_max) ? len_max : to_write);
964 return tp_len;
967 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
969 struct sk_buff *skb;
970 struct net_device *dev;
971 __be16 proto;
972 int ifindex, err, reserve = 0;
973 void *ph;
974 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
975 int tp_len, size_max;
976 unsigned char *addr;
977 int len_sum = 0;
978 int status = 0;
980 mutex_lock(&po->pg_vec_lock);
982 err = -EBUSY;
983 if (saddr == NULL) {
984 ifindex = po->ifindex;
985 proto = po->num;
986 addr = NULL;
987 } else {
988 err = -EINVAL;
989 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
990 goto out;
991 if (msg->msg_namelen < (saddr->sll_halen
992 + offsetof(struct sockaddr_ll,
993 sll_addr)))
994 goto out;
995 ifindex = saddr->sll_ifindex;
996 proto = saddr->sll_protocol;
997 addr = saddr->sll_addr;
1000 dev = dev_get_by_index(sock_net(&po->sk), ifindex);
1001 err = -ENXIO;
1002 if (unlikely(dev == NULL))
1003 goto out;
1005 reserve = dev->hard_header_len;
1007 err = -ENETDOWN;
1008 if (unlikely(!(dev->flags & IFF_UP)))
1009 goto out_put;
1011 size_max = po->tx_ring.frame_size
1012 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
1014 if (size_max > dev->mtu + reserve)
1015 size_max = dev->mtu + reserve;
1017 do {
1018 ph = packet_current_frame(po, &po->tx_ring,
1019 TP_STATUS_SEND_REQUEST);
1021 if (unlikely(ph == NULL)) {
1022 schedule();
1023 continue;
1026 status = TP_STATUS_SEND_REQUEST;
1027 skb = sock_alloc_send_skb(&po->sk,
1028 LL_ALLOCATED_SPACE(dev)
1029 + sizeof(struct sockaddr_ll),
1030 0, &err);
1032 if (unlikely(skb == NULL))
1033 goto out_status;
1035 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1036 addr);
1038 if (unlikely(tp_len < 0)) {
1039 if (po->tp_loss) {
1040 __packet_set_status(po, ph,
1041 TP_STATUS_AVAILABLE);
1042 packet_increment_head(&po->tx_ring);
1043 kfree_skb(skb);
1044 continue;
1045 } else {
1046 status = TP_STATUS_WRONG_FORMAT;
1047 err = tp_len;
1048 goto out_status;
1052 skb->destructor = tpacket_destruct_skb;
1053 __packet_set_status(po, ph, TP_STATUS_SENDING);
1054 atomic_inc(&po->tx_ring.pending);
1056 status = TP_STATUS_SEND_REQUEST;
1057 err = dev_queue_xmit(skb);
1058 if (unlikely(err > 0)) {
1059 err = net_xmit_errno(err);
1060 if (err && __packet_get_status(po, ph) ==
1061 TP_STATUS_AVAILABLE) {
1062 /* skb was destructed already */
1063 skb = NULL;
1064 goto out_status;
1067 * skb was dropped but not destructed yet;
1068 * let's treat it like congestion or err < 0
1070 err = 0;
1072 packet_increment_head(&po->tx_ring);
1073 len_sum += tp_len;
1074 } while (likely((ph != NULL) ||
1075 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1076 (atomic_read(&po->tx_ring.pending))))
1079 err = len_sum;
1080 goto out_put;
1082 out_status:
1083 __packet_set_status(po, ph, status);
1084 kfree_skb(skb);
1085 out_put:
1086 dev_put(dev);
1087 out:
1088 mutex_unlock(&po->pg_vec_lock);
1089 return err;
1092 static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1093 size_t reserve, size_t len,
1094 size_t linear, int noblock,
1095 int *err)
1097 struct sk_buff *skb;
1099 /* Under a page? Don't bother with paged skb. */
1100 if (prepad + len < PAGE_SIZE || !linear)
1101 linear = len;
1103 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1104 err);
1105 if (!skb)
1106 return NULL;
1108 skb_reserve(skb, reserve);
1109 skb_put(skb, linear);
1110 skb->data_len = len - linear;
1111 skb->len += len - linear;
1113 return skb;
1116 static int packet_snd(struct socket *sock,
1117 struct msghdr *msg, size_t len)
1119 struct sock *sk = sock->sk;
1120 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
1121 struct sk_buff *skb;
1122 struct net_device *dev;
1123 __be16 proto;
1124 unsigned char *addr;
1125 int ifindex, err, reserve = 0;
1126 struct virtio_net_hdr vnet_hdr = { 0 };
1127 int offset = 0;
1128 int vnet_hdr_len;
1129 struct packet_sock *po = pkt_sk(sk);
1130 unsigned short gso_type = 0;
1133 * Get and verify the address.
1136 if (saddr == NULL) {
1137 ifindex = po->ifindex;
1138 proto = po->num;
1139 addr = NULL;
1140 } else {
1141 err = -EINVAL;
1142 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1143 goto out;
1144 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1145 goto out;
1146 ifindex = saddr->sll_ifindex;
1147 proto = saddr->sll_protocol;
1148 addr = saddr->sll_addr;
1152 dev = dev_get_by_index(sock_net(sk), ifindex);
1153 err = -ENXIO;
1154 if (dev == NULL)
1155 goto out_unlock;
1156 if (sock->type == SOCK_RAW)
1157 reserve = dev->hard_header_len;
1159 err = -ENETDOWN;
1160 if (!(dev->flags & IFF_UP))
1161 goto out_unlock;
1163 if (po->has_vnet_hdr) {
1164 vnet_hdr_len = sizeof(vnet_hdr);
1166 err = -EINVAL;
1167 if (len < vnet_hdr_len)
1168 goto out_unlock;
1170 len -= vnet_hdr_len;
1172 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1173 vnet_hdr_len);
1174 if (err < 0)
1175 goto out_unlock;
1177 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1178 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1179 vnet_hdr.hdr_len))
1180 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1181 vnet_hdr.csum_offset + 2;
1183 err = -EINVAL;
1184 if (vnet_hdr.hdr_len > len)
1185 goto out_unlock;
1187 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1188 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1189 case VIRTIO_NET_HDR_GSO_TCPV4:
1190 gso_type = SKB_GSO_TCPV4;
1191 break;
1192 case VIRTIO_NET_HDR_GSO_TCPV6:
1193 gso_type = SKB_GSO_TCPV6;
1194 break;
1195 case VIRTIO_NET_HDR_GSO_UDP:
1196 gso_type = SKB_GSO_UDP;
1197 break;
1198 default:
1199 goto out_unlock;
1202 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1203 gso_type |= SKB_GSO_TCP_ECN;
1205 if (vnet_hdr.gso_size == 0)
1206 goto out_unlock;
1211 err = -EMSGSIZE;
1212 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
1213 goto out_unlock;
1215 err = -ENOBUFS;
1216 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1217 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1218 msg->msg_flags & MSG_DONTWAIT, &err);
1219 if (skb == NULL)
1220 goto out_unlock;
1222 skb_set_network_header(skb, reserve);
1224 err = -EINVAL;
1225 if (sock->type == SOCK_DGRAM &&
1226 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
1227 goto out_free;
1229 /* Returns -EFAULT on error */
1230 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
1231 if (err)
1232 goto out_free;
1233 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
1234 if (err < 0)
1235 goto out_free;
1237 if (!gso_type && (len > dev->mtu + reserve)) {
1238 /* Earlier code assumed this would be a VLAN pkt,
1239 * double-check this now that we have the actual
1240 * packet in hand.
1242 struct ethhdr *ehdr;
1243 skb_reset_mac_header(skb);
1244 ehdr = eth_hdr(skb);
1245 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1246 err = -EMSGSIZE;
1247 goto out_free;
1251 skb->protocol = proto;
1252 skb->dev = dev;
1253 skb->priority = sk->sk_priority;
1254 skb->mark = sk->sk_mark;
1256 if (po->has_vnet_hdr) {
1257 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1258 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1259 vnet_hdr.csum_offset)) {
1260 err = -EINVAL;
1261 goto out_free;
1265 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1266 skb_shinfo(skb)->gso_type = gso_type;
1268 /* Header must be checked, and gso_segs computed. */
1269 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1270 skb_shinfo(skb)->gso_segs = 0;
1272 len += vnet_hdr_len;
1276 * Now send it
1279 err = dev_queue_xmit(skb);
1280 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1281 goto out_unlock;
1283 dev_put(dev);
1285 return len;
1287 out_free:
1288 kfree_skb(skb);
1289 out_unlock:
1290 if (dev)
1291 dev_put(dev);
1292 out:
1293 return err;
1296 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1297 struct msghdr *msg, size_t len)
1299 struct sock *sk = sock->sk;
1300 struct packet_sock *po = pkt_sk(sk);
1301 if (po->tx_ring.pg_vec)
1302 return tpacket_snd(po, msg);
1303 else
1304 return packet_snd(sock, msg, len);
1308 * Close a PACKET socket. This is fairly simple. We immediately go
1309 * to 'closed' state and remove our protocol entry in the device list.
1312 static int packet_release(struct socket *sock)
1314 struct sock *sk = sock->sk;
1315 struct packet_sock *po;
1316 struct net *net;
1317 struct tpacket_req req;
1319 if (!sk)
1320 return 0;
1322 net = sock_net(sk);
1323 po = pkt_sk(sk);
1325 spin_lock_bh(&net->packet.sklist_lock);
1326 sk_del_node_init_rcu(sk);
1327 sock_prot_inuse_add(net, sk->sk_prot, -1);
1328 spin_unlock_bh(&net->packet.sklist_lock);
1330 spin_lock(&po->bind_lock);
1331 if (po->running) {
1333 * Remove from protocol table
1335 po->running = 0;
1336 po->num = 0;
1337 __dev_remove_pack(&po->prot_hook);
1338 __sock_put(sk);
1340 spin_unlock(&po->bind_lock);
1342 packet_flush_mclist(sk);
1344 memset(&req, 0, sizeof(req));
1346 if (po->rx_ring.pg_vec)
1347 packet_set_ring(sk, &req, 1, 0);
1349 if (po->tx_ring.pg_vec)
1350 packet_set_ring(sk, &req, 1, 1);
1352 synchronize_net();
1354 * Now the socket is dead. No more input will appear.
1356 sock_orphan(sk);
1357 sock->sk = NULL;
1359 /* Purge queues */
1361 skb_queue_purge(&sk->sk_receive_queue);
1362 sk_refcnt_debug_release(sk);
1364 sock_put(sk);
1365 return 0;
1369 * Attach a packet hook.
1372 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
1374 struct packet_sock *po = pkt_sk(sk);
1376 * Detach an existing hook if present.
1379 lock_sock(sk);
1381 spin_lock(&po->bind_lock);
1382 if (po->running) {
1383 __sock_put(sk);
1384 po->running = 0;
1385 po->num = 0;
1386 spin_unlock(&po->bind_lock);
1387 dev_remove_pack(&po->prot_hook);
1388 spin_lock(&po->bind_lock);
1391 po->num = protocol;
1392 po->prot_hook.type = protocol;
1393 po->prot_hook.dev = dev;
1395 po->ifindex = dev ? dev->ifindex : 0;
1397 if (protocol == 0)
1398 goto out_unlock;
1400 if (!dev || (dev->flags & IFF_UP)) {
1401 dev_add_pack(&po->prot_hook);
1402 sock_hold(sk);
1403 po->running = 1;
1404 } else {
1405 sk->sk_err = ENETDOWN;
1406 if (!sock_flag(sk, SOCK_DEAD))
1407 sk->sk_error_report(sk);
1410 out_unlock:
1411 spin_unlock(&po->bind_lock);
1412 release_sock(sk);
1413 return 0;
1417 * Bind a packet socket to a device
1420 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1421 int addr_len)
1423 struct sock *sk = sock->sk;
1424 char name[15];
1425 struct net_device *dev;
1426 int err = -ENODEV;
1429 * Check legality
1432 if (addr_len != sizeof(struct sockaddr))
1433 return -EINVAL;
1434 strlcpy(name, uaddr->sa_data, sizeof(name));
1436 dev = dev_get_by_name(sock_net(sk), name);
1437 if (dev) {
1438 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
1439 dev_put(dev);
1441 return err;
1444 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1446 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1447 struct sock *sk = sock->sk;
1448 struct net_device *dev = NULL;
1449 int err;
1453 * Check legality
1456 if (addr_len < sizeof(struct sockaddr_ll))
1457 return -EINVAL;
1458 if (sll->sll_family != AF_PACKET)
1459 return -EINVAL;
1461 if (sll->sll_ifindex) {
1462 err = -ENODEV;
1463 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
1464 if (dev == NULL)
1465 goto out;
1467 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
1468 if (dev)
1469 dev_put(dev);
1471 out:
1472 return err;
1475 static struct proto packet_proto = {
1476 .name = "PACKET",
1477 .owner = THIS_MODULE,
1478 .obj_size = sizeof(struct packet_sock),
1482 * Create a packet of type SOCK_PACKET.
1485 static int packet_create(struct net *net, struct socket *sock, int protocol,
1486 int kern)
1488 struct sock *sk;
1489 struct packet_sock *po;
1490 __be16 proto = (__force __be16)protocol; /* weird, but documented */
1491 int err;
1493 if (!capable(CAP_NET_RAW))
1494 return -EPERM;
1495 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1496 sock->type != SOCK_PACKET)
1497 return -ESOCKTNOSUPPORT;
1499 sock->state = SS_UNCONNECTED;
1501 err = -ENOBUFS;
1502 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
1503 if (sk == NULL)
1504 goto out;
1506 sock->ops = &packet_ops;
1507 if (sock->type == SOCK_PACKET)
1508 sock->ops = &packet_ops_spkt;
1510 sock_init_data(sock, sk);
1512 po = pkt_sk(sk);
1513 sk->sk_family = PF_PACKET;
1514 po->num = proto;
1516 sk->sk_destruct = packet_sock_destruct;
1517 sk_refcnt_debug_inc(sk);
1520 * Attach a protocol block
1523 spin_lock_init(&po->bind_lock);
1524 mutex_init(&po->pg_vec_lock);
1525 po->prot_hook.func = packet_rcv;
1527 if (sock->type == SOCK_PACKET)
1528 po->prot_hook.func = packet_rcv_spkt;
1530 po->prot_hook.af_packet_priv = sk;
1532 if (proto) {
1533 po->prot_hook.type = proto;
1534 dev_add_pack(&po->prot_hook);
1535 sock_hold(sk);
1536 po->running = 1;
1539 spin_lock_bh(&net->packet.sklist_lock);
1540 sk_add_node_rcu(sk, &net->packet.sklist);
1541 sock_prot_inuse_add(net, &packet_proto, 1);
1542 spin_unlock_bh(&net->packet.sklist_lock);
1544 return 0;
1545 out:
1546 return err;
1549 static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1551 struct sock_exterr_skb *serr;
1552 struct sk_buff *skb, *skb2;
1553 int copied, err;
1555 err = -EAGAIN;
1556 skb = skb_dequeue(&sk->sk_error_queue);
1557 if (skb == NULL)
1558 goto out;
1560 copied = skb->len;
1561 if (copied > len) {
1562 msg->msg_flags |= MSG_TRUNC;
1563 copied = len;
1565 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1566 if (err)
1567 goto out_free_skb;
1569 sock_recv_timestamp(msg, sk, skb);
1571 serr = SKB_EXT_ERR(skb);
1572 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1573 sizeof(serr->ee), &serr->ee);
1575 msg->msg_flags |= MSG_ERRQUEUE;
1576 err = copied;
1578 /* Reset and regenerate socket error */
1579 spin_lock_bh(&sk->sk_error_queue.lock);
1580 sk->sk_err = 0;
1581 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1582 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1583 spin_unlock_bh(&sk->sk_error_queue.lock);
1584 sk->sk_error_report(sk);
1585 } else
1586 spin_unlock_bh(&sk->sk_error_queue.lock);
1588 out_free_skb:
1589 kfree_skb(skb);
1590 out:
1591 return err;
1595 * Pull a packet from our receive queue and hand it to the user.
1596 * If necessary we block.
1599 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1600 struct msghdr *msg, size_t len, int flags)
1602 struct sock *sk = sock->sk;
1603 struct sk_buff *skb;
1604 int copied, err;
1605 struct sockaddr_ll *sll;
1606 int vnet_hdr_len = 0;
1608 err = -EINVAL;
1609 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
1610 goto out;
1612 #if 0
1613 /* What error should we return now? EUNATTACH? */
1614 if (pkt_sk(sk)->ifindex < 0)
1615 return -ENODEV;
1616 #endif
1618 if (flags & MSG_ERRQUEUE) {
1619 err = packet_recv_error(sk, msg, len);
1620 goto out;
1624 * Call the generic datagram receiver. This handles all sorts
1625 * of horrible races and re-entrancy so we can forget about it
1626 * in the protocol layers.
1628 * Now it will return ENETDOWN, if device have just gone down,
1629 * but then it will block.
1632 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
1635 * An error occurred so return it. Because skb_recv_datagram()
1636 * handles the blocking we don't see and worry about blocking
1637 * retries.
1640 if (skb == NULL)
1641 goto out;
1643 if (pkt_sk(sk)->has_vnet_hdr) {
1644 struct virtio_net_hdr vnet_hdr = { 0 };
1646 err = -EINVAL;
1647 vnet_hdr_len = sizeof(vnet_hdr);
1648 if (len < vnet_hdr_len)
1649 goto out_free;
1651 len -= vnet_hdr_len;
1653 if (skb_is_gso(skb)) {
1654 struct skb_shared_info *sinfo = skb_shinfo(skb);
1656 /* This is a hint as to how much should be linear. */
1657 vnet_hdr.hdr_len = skb_headlen(skb);
1658 vnet_hdr.gso_size = sinfo->gso_size;
1659 if (sinfo->gso_type & SKB_GSO_TCPV4)
1660 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1661 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1662 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1663 else if (sinfo->gso_type & SKB_GSO_UDP)
1664 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1665 else if (sinfo->gso_type & SKB_GSO_FCOE)
1666 goto out_free;
1667 else
1668 BUG();
1669 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1670 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1671 } else
1672 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1674 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1675 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1676 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
1677 vnet_hdr.csum_offset = skb->csum_offset;
1678 } /* else everything is zero */
1680 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1681 vnet_hdr_len);
1682 if (err < 0)
1683 goto out_free;
1687 * If the address length field is there to be filled in, we fill
1688 * it in now.
1691 sll = &PACKET_SKB_CB(skb)->sa.ll;
1692 if (sock->type == SOCK_PACKET)
1693 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1694 else
1695 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1698 * You lose any data beyond the buffer you gave. If it worries a
1699 * user program they can ask the device for its MTU anyway.
1702 copied = skb->len;
1703 if (copied > len) {
1704 copied = len;
1705 msg->msg_flags |= MSG_TRUNC;
1708 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1709 if (err)
1710 goto out_free;
1712 sock_recv_ts_and_drops(msg, sk, skb);
1714 if (msg->msg_name)
1715 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1716 msg->msg_namelen);
1718 if (pkt_sk(sk)->auxdata) {
1719 struct tpacket_auxdata aux;
1721 aux.tp_status = TP_STATUS_USER;
1722 if (skb->ip_summed == CHECKSUM_PARTIAL)
1723 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1724 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1725 aux.tp_snaplen = skb->len;
1726 aux.tp_mac = 0;
1727 aux.tp_net = skb_network_offset(skb);
1728 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
1730 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
1734 * Free or return the buffer as appropriate. Again this
1735 * hides all the races and re-entrancy issues from us.
1737 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
1739 out_free:
1740 skb_free_datagram(sk, skb);
1741 out:
1742 return err;
1745 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1746 int *uaddr_len, int peer)
1748 struct net_device *dev;
1749 struct sock *sk = sock->sk;
1751 if (peer)
1752 return -EOPNOTSUPP;
1754 uaddr->sa_family = AF_PACKET;
1755 rcu_read_lock();
1756 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1757 if (dev)
1758 strncpy(uaddr->sa_data, dev->name, 14);
1759 else
1760 memset(uaddr->sa_data, 0, 14);
1761 rcu_read_unlock();
1762 *uaddr_len = sizeof(*uaddr);
1764 return 0;
1767 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1768 int *uaddr_len, int peer)
1770 struct net_device *dev;
1771 struct sock *sk = sock->sk;
1772 struct packet_sock *po = pkt_sk(sk);
1773 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
1775 if (peer)
1776 return -EOPNOTSUPP;
1778 sll->sll_family = AF_PACKET;
1779 sll->sll_ifindex = po->ifindex;
1780 sll->sll_protocol = po->num;
1781 sll->sll_pkttype = 0;
1782 rcu_read_lock();
1783 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
1784 if (dev) {
1785 sll->sll_hatype = dev->type;
1786 sll->sll_halen = dev->addr_len;
1787 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1788 } else {
1789 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1790 sll->sll_halen = 0;
1792 rcu_read_unlock();
1793 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
1795 return 0;
1798 static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1799 int what)
1801 switch (i->type) {
1802 case PACKET_MR_MULTICAST:
1803 if (i->alen != dev->addr_len)
1804 return -EINVAL;
1805 if (what > 0)
1806 return dev_mc_add(dev, i->addr);
1807 else
1808 return dev_mc_del(dev, i->addr);
1809 break;
1810 case PACKET_MR_PROMISC:
1811 return dev_set_promiscuity(dev, what);
1812 break;
1813 case PACKET_MR_ALLMULTI:
1814 return dev_set_allmulti(dev, what);
1815 break;
1816 case PACKET_MR_UNICAST:
1817 if (i->alen != dev->addr_len)
1818 return -EINVAL;
1819 if (what > 0)
1820 return dev_uc_add(dev, i->addr);
1821 else
1822 return dev_uc_del(dev, i->addr);
1823 break;
1824 default:
1825 break;
1827 return 0;
1830 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1832 for ( ; i; i = i->next) {
1833 if (i->ifindex == dev->ifindex)
1834 packet_dev_mc(dev, i, what);
1838 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
1840 struct packet_sock *po = pkt_sk(sk);
1841 struct packet_mclist *ml, *i;
1842 struct net_device *dev;
1843 int err;
1845 rtnl_lock();
1847 err = -ENODEV;
1848 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
1849 if (!dev)
1850 goto done;
1852 err = -EINVAL;
1853 if (mreq->mr_alen > dev->addr_len)
1854 goto done;
1856 err = -ENOBUFS;
1857 i = kmalloc(sizeof(*i), GFP_KERNEL);
1858 if (i == NULL)
1859 goto done;
1861 err = 0;
1862 for (ml = po->mclist; ml; ml = ml->next) {
1863 if (ml->ifindex == mreq->mr_ifindex &&
1864 ml->type == mreq->mr_type &&
1865 ml->alen == mreq->mr_alen &&
1866 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1867 ml->count++;
1868 /* Free the new element ... */
1869 kfree(i);
1870 goto done;
1874 i->type = mreq->mr_type;
1875 i->ifindex = mreq->mr_ifindex;
1876 i->alen = mreq->mr_alen;
1877 memcpy(i->addr, mreq->mr_address, i->alen);
1878 i->count = 1;
1879 i->next = po->mclist;
1880 po->mclist = i;
1881 err = packet_dev_mc(dev, i, 1);
1882 if (err) {
1883 po->mclist = i->next;
1884 kfree(i);
1887 done:
1888 rtnl_unlock();
1889 return err;
1892 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
1894 struct packet_mclist *ml, **mlp;
1896 rtnl_lock();
1898 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1899 if (ml->ifindex == mreq->mr_ifindex &&
1900 ml->type == mreq->mr_type &&
1901 ml->alen == mreq->mr_alen &&
1902 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1903 if (--ml->count == 0) {
1904 struct net_device *dev;
1905 *mlp = ml->next;
1906 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1907 if (dev)
1908 packet_dev_mc(dev, ml, -1);
1909 kfree(ml);
1911 rtnl_unlock();
1912 return 0;
1915 rtnl_unlock();
1916 return -EADDRNOTAVAIL;
1919 static void packet_flush_mclist(struct sock *sk)
1921 struct packet_sock *po = pkt_sk(sk);
1922 struct packet_mclist *ml;
1924 if (!po->mclist)
1925 return;
1927 rtnl_lock();
1928 while ((ml = po->mclist) != NULL) {
1929 struct net_device *dev;
1931 po->mclist = ml->next;
1932 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1933 if (dev != NULL)
1934 packet_dev_mc(dev, ml, -1);
1935 kfree(ml);
1937 rtnl_unlock();
1940 static int
1941 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
1943 struct sock *sk = sock->sk;
1944 struct packet_sock *po = pkt_sk(sk);
1945 int ret;
1947 if (level != SOL_PACKET)
1948 return -ENOPROTOOPT;
1950 switch (optname) {
1951 case PACKET_ADD_MEMBERSHIP:
1952 case PACKET_DROP_MEMBERSHIP:
1954 struct packet_mreq_max mreq;
1955 int len = optlen;
1956 memset(&mreq, 0, sizeof(mreq));
1957 if (len < sizeof(struct packet_mreq))
1958 return -EINVAL;
1959 if (len > sizeof(mreq))
1960 len = sizeof(mreq);
1961 if (copy_from_user(&mreq, optval, len))
1962 return -EFAULT;
1963 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1964 return -EINVAL;
1965 if (optname == PACKET_ADD_MEMBERSHIP)
1966 ret = packet_mc_add(sk, &mreq);
1967 else
1968 ret = packet_mc_drop(sk, &mreq);
1969 return ret;
1972 case PACKET_RX_RING:
1973 case PACKET_TX_RING:
1975 struct tpacket_req req;
1977 if (optlen < sizeof(req))
1978 return -EINVAL;
1979 if (pkt_sk(sk)->has_vnet_hdr)
1980 return -EINVAL;
1981 if (copy_from_user(&req, optval, sizeof(req)))
1982 return -EFAULT;
1983 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
1985 case PACKET_COPY_THRESH:
1987 int val;
1989 if (optlen != sizeof(val))
1990 return -EINVAL;
1991 if (copy_from_user(&val, optval, sizeof(val)))
1992 return -EFAULT;
1994 pkt_sk(sk)->copy_thresh = val;
1995 return 0;
1997 case PACKET_VERSION:
1999 int val;
2001 if (optlen != sizeof(val))
2002 return -EINVAL;
2003 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2004 return -EBUSY;
2005 if (copy_from_user(&val, optval, sizeof(val)))
2006 return -EFAULT;
2007 switch (val) {
2008 case TPACKET_V1:
2009 case TPACKET_V2:
2010 po->tp_version = val;
2011 return 0;
2012 default:
2013 return -EINVAL;
2016 case PACKET_RESERVE:
2018 unsigned int val;
2020 if (optlen != sizeof(val))
2021 return -EINVAL;
2022 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2023 return -EBUSY;
2024 if (copy_from_user(&val, optval, sizeof(val)))
2025 return -EFAULT;
2026 po->tp_reserve = val;
2027 return 0;
2029 case PACKET_LOSS:
2031 unsigned int val;
2033 if (optlen != sizeof(val))
2034 return -EINVAL;
2035 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2036 return -EBUSY;
2037 if (copy_from_user(&val, optval, sizeof(val)))
2038 return -EFAULT;
2039 po->tp_loss = !!val;
2040 return 0;
2042 case PACKET_AUXDATA:
2044 int val;
2046 if (optlen < sizeof(val))
2047 return -EINVAL;
2048 if (copy_from_user(&val, optval, sizeof(val)))
2049 return -EFAULT;
2051 po->auxdata = !!val;
2052 return 0;
2054 case PACKET_ORIGDEV:
2056 int val;
2058 if (optlen < sizeof(val))
2059 return -EINVAL;
2060 if (copy_from_user(&val, optval, sizeof(val)))
2061 return -EFAULT;
2063 po->origdev = !!val;
2064 return 0;
2066 case PACKET_VNET_HDR:
2068 int val;
2070 if (sock->type != SOCK_RAW)
2071 return -EINVAL;
2072 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2073 return -EBUSY;
2074 if (optlen < sizeof(val))
2075 return -EINVAL;
2076 if (copy_from_user(&val, optval, sizeof(val)))
2077 return -EFAULT;
2079 po->has_vnet_hdr = !!val;
2080 return 0;
2082 case PACKET_TIMESTAMP:
2084 int val;
2086 if (optlen != sizeof(val))
2087 return -EINVAL;
2088 if (copy_from_user(&val, optval, sizeof(val)))
2089 return -EFAULT;
2091 po->tp_tstamp = val;
2092 return 0;
2094 default:
2095 return -ENOPROTOOPT;
2099 static int packet_getsockopt(struct socket *sock, int level, int optname,
2100 char __user *optval, int __user *optlen)
2102 int len;
2103 int val;
2104 struct sock *sk = sock->sk;
2105 struct packet_sock *po = pkt_sk(sk);
2106 void *data;
2107 struct tpacket_stats st;
2109 if (level != SOL_PACKET)
2110 return -ENOPROTOOPT;
2112 if (get_user(len, optlen))
2113 return -EFAULT;
2115 if (len < 0)
2116 return -EINVAL;
2118 switch (optname) {
2119 case PACKET_STATISTICS:
2120 if (len > sizeof(struct tpacket_stats))
2121 len = sizeof(struct tpacket_stats);
2122 spin_lock_bh(&sk->sk_receive_queue.lock);
2123 st = po->stats;
2124 memset(&po->stats, 0, sizeof(st));
2125 spin_unlock_bh(&sk->sk_receive_queue.lock);
2126 st.tp_packets += st.tp_drops;
2128 data = &st;
2129 break;
2130 case PACKET_AUXDATA:
2131 if (len > sizeof(int))
2132 len = sizeof(int);
2133 val = po->auxdata;
2135 data = &val;
2136 break;
2137 case PACKET_ORIGDEV:
2138 if (len > sizeof(int))
2139 len = sizeof(int);
2140 val = po->origdev;
2142 data = &val;
2143 break;
2144 case PACKET_VNET_HDR:
2145 if (len > sizeof(int))
2146 len = sizeof(int);
2147 val = po->has_vnet_hdr;
2149 data = &val;
2150 break;
2151 case PACKET_VERSION:
2152 if (len > sizeof(int))
2153 len = sizeof(int);
2154 val = po->tp_version;
2155 data = &val;
2156 break;
2157 case PACKET_HDRLEN:
2158 if (len > sizeof(int))
2159 len = sizeof(int);
2160 if (copy_from_user(&val, optval, len))
2161 return -EFAULT;
2162 switch (val) {
2163 case TPACKET_V1:
2164 val = sizeof(struct tpacket_hdr);
2165 break;
2166 case TPACKET_V2:
2167 val = sizeof(struct tpacket2_hdr);
2168 break;
2169 default:
2170 return -EINVAL;
2172 data = &val;
2173 break;
2174 case PACKET_RESERVE:
2175 if (len > sizeof(unsigned int))
2176 len = sizeof(unsigned int);
2177 val = po->tp_reserve;
2178 data = &val;
2179 break;
2180 case PACKET_LOSS:
2181 if (len > sizeof(unsigned int))
2182 len = sizeof(unsigned int);
2183 val = po->tp_loss;
2184 data = &val;
2185 break;
2186 case PACKET_TIMESTAMP:
2187 if (len > sizeof(int))
2188 len = sizeof(int);
2189 val = po->tp_tstamp;
2190 data = &val;
2191 break;
2192 default:
2193 return -ENOPROTOOPT;
2196 if (put_user(len, optlen))
2197 return -EFAULT;
2198 if (copy_to_user(optval, data, len))
2199 return -EFAULT;
2200 return 0;
2204 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2206 struct sock *sk;
2207 struct hlist_node *node;
2208 struct net_device *dev = data;
2209 struct net *net = dev_net(dev);
2211 rcu_read_lock();
2212 sk_for_each_rcu(sk, node, &net->packet.sklist) {
2213 struct packet_sock *po = pkt_sk(sk);
2215 switch (msg) {
2216 case NETDEV_UNREGISTER:
2217 if (po->mclist)
2218 packet_dev_mclist(dev, po->mclist, -1);
2219 /* fallthrough */
2221 case NETDEV_DOWN:
2222 if (dev->ifindex == po->ifindex) {
2223 spin_lock(&po->bind_lock);
2224 if (po->running) {
2225 __dev_remove_pack(&po->prot_hook);
2226 __sock_put(sk);
2227 po->running = 0;
2228 sk->sk_err = ENETDOWN;
2229 if (!sock_flag(sk, SOCK_DEAD))
2230 sk->sk_error_report(sk);
2232 if (msg == NETDEV_UNREGISTER) {
2233 po->ifindex = -1;
2234 po->prot_hook.dev = NULL;
2236 spin_unlock(&po->bind_lock);
2238 break;
2239 case NETDEV_UP:
2240 if (dev->ifindex == po->ifindex) {
2241 spin_lock(&po->bind_lock);
2242 if (po->num && !po->running) {
2243 dev_add_pack(&po->prot_hook);
2244 sock_hold(sk);
2245 po->running = 1;
2247 spin_unlock(&po->bind_lock);
2249 break;
2252 rcu_read_unlock();
2253 return NOTIFY_DONE;
2257 static int packet_ioctl(struct socket *sock, unsigned int cmd,
2258 unsigned long arg)
2260 struct sock *sk = sock->sk;
2262 switch (cmd) {
2263 case SIOCOUTQ:
2265 int amount = sk_wmem_alloc_get(sk);
2267 return put_user(amount, (int __user *)arg);
2269 case SIOCINQ:
2271 struct sk_buff *skb;
2272 int amount = 0;
2274 spin_lock_bh(&sk->sk_receive_queue.lock);
2275 skb = skb_peek(&sk->sk_receive_queue);
2276 if (skb)
2277 amount = skb->len;
2278 spin_unlock_bh(&sk->sk_receive_queue.lock);
2279 return put_user(amount, (int __user *)arg);
2281 case SIOCGSTAMP:
2282 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2283 case SIOCGSTAMPNS:
2284 return sock_get_timestampns(sk, (struct timespec __user *)arg);
2286 #ifdef CONFIG_INET
2287 case SIOCADDRT:
2288 case SIOCDELRT:
2289 case SIOCDARP:
2290 case SIOCGARP:
2291 case SIOCSARP:
2292 case SIOCGIFADDR:
2293 case SIOCSIFADDR:
2294 case SIOCGIFBRDADDR:
2295 case SIOCSIFBRDADDR:
2296 case SIOCGIFNETMASK:
2297 case SIOCSIFNETMASK:
2298 case SIOCGIFDSTADDR:
2299 case SIOCSIFDSTADDR:
2300 case SIOCSIFFLAGS:
2301 return inet_dgram_ops.ioctl(sock, cmd, arg);
2302 #endif
2304 default:
2305 return -ENOIOCTLCMD;
2307 return 0;
2310 static unsigned int packet_poll(struct file *file, struct socket *sock,
2311 poll_table *wait)
2313 struct sock *sk = sock->sk;
2314 struct packet_sock *po = pkt_sk(sk);
2315 unsigned int mask = datagram_poll(file, sock, wait);
2317 spin_lock_bh(&sk->sk_receive_queue.lock);
2318 if (po->rx_ring.pg_vec) {
2319 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
2320 mask |= POLLIN | POLLRDNORM;
2322 spin_unlock_bh(&sk->sk_receive_queue.lock);
2323 spin_lock_bh(&sk->sk_write_queue.lock);
2324 if (po->tx_ring.pg_vec) {
2325 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2326 mask |= POLLOUT | POLLWRNORM;
2328 spin_unlock_bh(&sk->sk_write_queue.lock);
2329 return mask;
2333 /* Dirty? Well, I still did not learn better way to account
2334 * for user mmaps.
2337 static void packet_mm_open(struct vm_area_struct *vma)
2339 struct file *file = vma->vm_file;
2340 struct socket *sock = file->private_data;
2341 struct sock *sk = sock->sk;
2343 if (sk)
2344 atomic_inc(&pkt_sk(sk)->mapped);
2347 static void packet_mm_close(struct vm_area_struct *vma)
2349 struct file *file = vma->vm_file;
2350 struct socket *sock = file->private_data;
2351 struct sock *sk = sock->sk;
2353 if (sk)
2354 atomic_dec(&pkt_sk(sk)->mapped);
2357 static const struct vm_operations_struct packet_mmap_ops = {
2358 .open = packet_mm_open,
2359 .close = packet_mm_close,
2362 static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2363 unsigned int len)
2365 int i;
2367 for (i = 0; i < len; i++) {
2368 if (likely(pg_vec[i].buffer)) {
2369 if (is_vmalloc_addr(pg_vec[i].buffer))
2370 vfree(pg_vec[i].buffer);
2371 else
2372 free_pages((unsigned long)pg_vec[i].buffer,
2373 order);
2374 pg_vec[i].buffer = NULL;
2377 kfree(pg_vec);
2380 static inline char *alloc_one_pg_vec_page(unsigned long order)
2382 char *buffer = NULL;
2383 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2384 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
2386 buffer = (char *) __get_free_pages(gfp_flags, order);
2388 if (buffer)
2389 return buffer;
2392 * __get_free_pages failed, fall back to vmalloc
2394 buffer = vzalloc((1 << order) * PAGE_SIZE);
2396 if (buffer)
2397 return buffer;
2400 * vmalloc failed, lets dig into swap here
2402 gfp_flags &= ~__GFP_NORETRY;
2403 buffer = (char *)__get_free_pages(gfp_flags, order);
2404 if (buffer)
2405 return buffer;
2408 * complete and utter failure
2410 return NULL;
2413 static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
2415 unsigned int block_nr = req->tp_block_nr;
2416 struct pgv *pg_vec;
2417 int i;
2419 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
2420 if (unlikely(!pg_vec))
2421 goto out;
2423 for (i = 0; i < block_nr; i++) {
2424 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
2425 if (unlikely(!pg_vec[i].buffer))
2426 goto out_free_pgvec;
2429 out:
2430 return pg_vec;
2432 out_free_pgvec:
2433 free_pg_vec(pg_vec, order, block_nr);
2434 pg_vec = NULL;
2435 goto out;
2438 static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2439 int closing, int tx_ring)
2441 struct pgv *pg_vec = NULL;
2442 struct packet_sock *po = pkt_sk(sk);
2443 int was_running, order = 0;
2444 struct packet_ring_buffer *rb;
2445 struct sk_buff_head *rb_queue;
2446 __be16 num;
2447 int err;
2449 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2450 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2452 err = -EBUSY;
2453 if (!closing) {
2454 if (atomic_read(&po->mapped))
2455 goto out;
2456 if (atomic_read(&rb->pending))
2457 goto out;
2460 if (req->tp_block_nr) {
2461 /* Sanity tests and some calculations */
2462 err = -EBUSY;
2463 if (unlikely(rb->pg_vec))
2464 goto out;
2466 switch (po->tp_version) {
2467 case TPACKET_V1:
2468 po->tp_hdrlen = TPACKET_HDRLEN;
2469 break;
2470 case TPACKET_V2:
2471 po->tp_hdrlen = TPACKET2_HDRLEN;
2472 break;
2475 err = -EINVAL;
2476 if (unlikely((int)req->tp_block_size <= 0))
2477 goto out;
2478 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
2479 goto out;
2480 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
2481 po->tp_reserve))
2482 goto out;
2483 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
2484 goto out;
2486 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2487 if (unlikely(rb->frames_per_block <= 0))
2488 goto out;
2489 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2490 req->tp_frame_nr))
2491 goto out;
2493 err = -ENOMEM;
2494 order = get_order(req->tp_block_size);
2495 pg_vec = alloc_pg_vec(req, order);
2496 if (unlikely(!pg_vec))
2497 goto out;
2499 /* Done */
2500 else {
2501 err = -EINVAL;
2502 if (unlikely(req->tp_frame_nr))
2503 goto out;
2506 lock_sock(sk);
2508 /* Detach socket from network */
2509 spin_lock(&po->bind_lock);
2510 was_running = po->running;
2511 num = po->num;
2512 if (was_running) {
2513 __dev_remove_pack(&po->prot_hook);
2514 po->num = 0;
2515 po->running = 0;
2516 __sock_put(sk);
2518 spin_unlock(&po->bind_lock);
2520 synchronize_net();
2522 err = -EBUSY;
2523 mutex_lock(&po->pg_vec_lock);
2524 if (closing || atomic_read(&po->mapped) == 0) {
2525 err = 0;
2526 spin_lock_bh(&rb_queue->lock);
2527 swap(rb->pg_vec, pg_vec);
2528 rb->frame_max = (req->tp_frame_nr - 1);
2529 rb->head = 0;
2530 rb->frame_size = req->tp_frame_size;
2531 spin_unlock_bh(&rb_queue->lock);
2533 swap(rb->pg_vec_order, order);
2534 swap(rb->pg_vec_len, req->tp_block_nr);
2536 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2537 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2538 tpacket_rcv : packet_rcv;
2539 skb_queue_purge(rb_queue);
2540 if (atomic_read(&po->mapped))
2541 pr_err("packet_mmap: vma is busy: %d\n",
2542 atomic_read(&po->mapped));
2544 mutex_unlock(&po->pg_vec_lock);
2546 spin_lock(&po->bind_lock);
2547 if (was_running && !po->running) {
2548 sock_hold(sk);
2549 po->running = 1;
2550 po->num = num;
2551 dev_add_pack(&po->prot_hook);
2553 spin_unlock(&po->bind_lock);
2555 release_sock(sk);
2557 if (pg_vec)
2558 free_pg_vec(pg_vec, order, req->tp_block_nr);
2559 out:
2560 return err;
2563 static int packet_mmap(struct file *file, struct socket *sock,
2564 struct vm_area_struct *vma)
2566 struct sock *sk = sock->sk;
2567 struct packet_sock *po = pkt_sk(sk);
2568 unsigned long size, expected_size;
2569 struct packet_ring_buffer *rb;
2570 unsigned long start;
2571 int err = -EINVAL;
2572 int i;
2574 if (vma->vm_pgoff)
2575 return -EINVAL;
2577 mutex_lock(&po->pg_vec_lock);
2579 expected_size = 0;
2580 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2581 if (rb->pg_vec) {
2582 expected_size += rb->pg_vec_len
2583 * rb->pg_vec_pages
2584 * PAGE_SIZE;
2588 if (expected_size == 0)
2589 goto out;
2591 size = vma->vm_end - vma->vm_start;
2592 if (size != expected_size)
2593 goto out;
2595 start = vma->vm_start;
2596 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2597 if (rb->pg_vec == NULL)
2598 continue;
2600 for (i = 0; i < rb->pg_vec_len; i++) {
2601 struct page *page;
2602 void *kaddr = rb->pg_vec[i].buffer;
2603 int pg_num;
2605 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2606 page = pgv_to_page(kaddr);
2607 err = vm_insert_page(vma, start, page);
2608 if (unlikely(err))
2609 goto out;
2610 start += PAGE_SIZE;
2611 kaddr += PAGE_SIZE;
2616 atomic_inc(&po->mapped);
2617 vma->vm_ops = &packet_mmap_ops;
2618 err = 0;
2620 out:
2621 mutex_unlock(&po->pg_vec_lock);
2622 return err;
2625 static const struct proto_ops packet_ops_spkt = {
2626 .family = PF_PACKET,
2627 .owner = THIS_MODULE,
2628 .release = packet_release,
2629 .bind = packet_bind_spkt,
2630 .connect = sock_no_connect,
2631 .socketpair = sock_no_socketpair,
2632 .accept = sock_no_accept,
2633 .getname = packet_getname_spkt,
2634 .poll = datagram_poll,
2635 .ioctl = packet_ioctl,
2636 .listen = sock_no_listen,
2637 .shutdown = sock_no_shutdown,
2638 .setsockopt = sock_no_setsockopt,
2639 .getsockopt = sock_no_getsockopt,
2640 .sendmsg = packet_sendmsg_spkt,
2641 .recvmsg = packet_recvmsg,
2642 .mmap = sock_no_mmap,
2643 .sendpage = sock_no_sendpage,
2646 static const struct proto_ops packet_ops = {
2647 .family = PF_PACKET,
2648 .owner = THIS_MODULE,
2649 .release = packet_release,
2650 .bind = packet_bind,
2651 .connect = sock_no_connect,
2652 .socketpair = sock_no_socketpair,
2653 .accept = sock_no_accept,
2654 .getname = packet_getname,
2655 .poll = packet_poll,
2656 .ioctl = packet_ioctl,
2657 .listen = sock_no_listen,
2658 .shutdown = sock_no_shutdown,
2659 .setsockopt = packet_setsockopt,
2660 .getsockopt = packet_getsockopt,
2661 .sendmsg = packet_sendmsg,
2662 .recvmsg = packet_recvmsg,
2663 .mmap = packet_mmap,
2664 .sendpage = sock_no_sendpage,
2667 static const struct net_proto_family packet_family_ops = {
2668 .family = PF_PACKET,
2669 .create = packet_create,
2670 .owner = THIS_MODULE,
2673 static struct notifier_block packet_netdev_notifier = {
2674 .notifier_call = packet_notifier,
2677 #ifdef CONFIG_PROC_FS
2679 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
2680 __acquires(RCU)
2682 struct net *net = seq_file_net(seq);
2684 rcu_read_lock();
2685 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
2688 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2690 struct net *net = seq_file_net(seq);
2691 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
2694 static void packet_seq_stop(struct seq_file *seq, void *v)
2695 __releases(RCU)
2697 rcu_read_unlock();
2700 static int packet_seq_show(struct seq_file *seq, void *v)
2702 if (v == SEQ_START_TOKEN)
2703 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2704 else {
2705 struct sock *s = sk_entry(v);
2706 const struct packet_sock *po = pkt_sk(s);
2708 seq_printf(seq,
2709 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2711 atomic_read(&s->sk_refcnt),
2712 s->sk_type,
2713 ntohs(po->num),
2714 po->ifindex,
2715 po->running,
2716 atomic_read(&s->sk_rmem_alloc),
2717 sock_i_uid(s),
2718 sock_i_ino(s));
2721 return 0;
2724 static const struct seq_operations packet_seq_ops = {
2725 .start = packet_seq_start,
2726 .next = packet_seq_next,
2727 .stop = packet_seq_stop,
2728 .show = packet_seq_show,
2731 static int packet_seq_open(struct inode *inode, struct file *file)
2733 return seq_open_net(inode, file, &packet_seq_ops,
2734 sizeof(struct seq_net_private));
2737 static const struct file_operations packet_seq_fops = {
2738 .owner = THIS_MODULE,
2739 .open = packet_seq_open,
2740 .read = seq_read,
2741 .llseek = seq_lseek,
2742 .release = seq_release_net,
2745 #endif
2747 static int __net_init packet_net_init(struct net *net)
2749 spin_lock_init(&net->packet.sklist_lock);
2750 INIT_HLIST_HEAD(&net->packet.sklist);
2752 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2753 return -ENOMEM;
2755 return 0;
2758 static void __net_exit packet_net_exit(struct net *net)
2760 proc_net_remove(net, "packet");
2763 static struct pernet_operations packet_net_ops = {
2764 .init = packet_net_init,
2765 .exit = packet_net_exit,
2769 static void __exit packet_exit(void)
2771 unregister_netdevice_notifier(&packet_netdev_notifier);
2772 unregister_pernet_subsys(&packet_net_ops);
2773 sock_unregister(PF_PACKET);
2774 proto_unregister(&packet_proto);
2777 static int __init packet_init(void)
2779 int rc = proto_register(&packet_proto, 0);
2781 if (rc != 0)
2782 goto out;
2784 sock_register(&packet_family_ops);
2785 register_pernet_subsys(&packet_net_ops);
2786 register_netdevice_notifier(&packet_netdev_notifier);
2787 out:
2788 return rc;
2791 module_init(packet_init);
2792 module_exit(packet_exit);
2793 MODULE_LICENSE("GPL");
2794 MODULE_ALIAS_NETPROTO(PF_PACKET);