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.
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
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
42 * Johann Baudy : Added TX RING.
43 * Chetan Loke : Implemented TPACKET_V3 block abstraction
45 * Copyright (C) 2011, <lokec@ccs.neu.edu>
48 * This program is free software; you can redistribute it and/or
49 * modify it under the terms of the GNU General Public License
50 * as published by the Free Software Foundation; either version
51 * 2 of the License, or (at your option) any later version.
55 #include <linux/types.h>
57 #include <linux/capability.h>
58 #include <linux/fcntl.h>
59 #include <linux/socket.h>
61 #include <linux/inet.h>
62 #include <linux/netdevice.h>
63 #include <linux/if_packet.h>
64 #include <linux/wireless.h>
65 #include <linux/kernel.h>
66 #include <linux/kmod.h>
67 #include <linux/slab.h>
68 #include <linux/vmalloc.h>
69 #include <net/net_namespace.h>
71 #include <net/protocol.h>
72 #include <linux/skbuff.h>
74 #include <linux/errno.h>
75 #include <linux/timer.h>
76 #include <asm/uaccess.h>
77 #include <asm/ioctls.h>
79 #include <asm/cacheflush.h>
81 #include <linux/proc_fs.h>
82 #include <linux/seq_file.h>
83 #include <linux/poll.h>
84 #include <linux/module.h>
85 #include <linux/init.h>
86 #include <linux/mutex.h>
87 #include <linux/if_vlan.h>
88 #include <linux/virtio_net.h>
89 #include <linux/errqueue.h>
90 #include <linux/net_tstamp.h>
91 #include <linux/reciprocal_div.h>
92 #include <linux/percpu.h>
94 #include <net/inet_common.h>
101 - if device has no dev->hard_header routine, it adds and removes ll header
102 inside itself. In this case ll header is invisible outside of device,
103 but higher levels still should reserve dev->hard_header_len.
104 Some devices are enough clever to reallocate skb, when header
105 will not fit to reserved space (tunnel), another ones are silly
107 - packet socket receives packets with pulled ll header,
108 so that SOCK_RAW should push it back.
113 Incoming, dev->hard_header!=NULL
114 mac_header -> ll header
117 Outgoing, dev->hard_header!=NULL
118 mac_header -> ll header
121 Incoming, dev->hard_header==NULL
122 mac_header -> UNKNOWN position. It is very likely, that it points to ll
123 header. PPP makes it, that is wrong, because introduce
124 assymetry between rx and tx paths.
127 Outgoing, dev->hard_header==NULL
128 mac_header -> data. ll header is still not built!
132 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
138 dev->hard_header != NULL
139 mac_header -> ll header
142 dev->hard_header == NULL (ll header is added by device, we cannot control it)
146 We should set nh.raw on output to correct posistion,
147 packet classifier depends on it.
150 /* Private packet socket structures. */
152 /* identical to struct packet_mreq except it has
153 * a longer address field.
155 struct packet_mreq_max {
157 unsigned short mr_type;
158 unsigned short mr_alen;
159 unsigned char mr_address[MAX_ADDR_LEN];
163 struct tpacket_hdr *h1;
164 struct tpacket2_hdr *h2;
165 struct tpacket3_hdr *h3;
169 static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
170 int closing, int tx_ring);
172 #define V3_ALIGNMENT (8)
174 #define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
176 #define BLK_PLUS_PRIV(sz_of_priv) \
177 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
179 #define PGV_FROM_VMALLOC 1
181 #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
182 #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
183 #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
184 #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
185 #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
186 #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
187 #define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
190 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
191 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
192 struct packet_type *pt, struct net_device *orig_dev);
194 static void *packet_previous_frame(struct packet_sock *po,
195 struct packet_ring_buffer *rb,
197 static void packet_increment_head(struct packet_ring_buffer *buff);
198 static int prb_curr_blk_in_use(struct tpacket_kbdq_core *,
199 struct tpacket_block_desc *);
200 static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
201 struct packet_sock *);
202 static void prb_retire_current_block(struct tpacket_kbdq_core *,
203 struct packet_sock *, unsigned int status);
204 static int prb_queue_frozen(struct tpacket_kbdq_core *);
205 static void prb_open_block(struct tpacket_kbdq_core *,
206 struct tpacket_block_desc *);
207 static void prb_retire_rx_blk_timer_expired(unsigned long);
208 static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
209 static void prb_init_blk_timer(struct packet_sock *,
210 struct tpacket_kbdq_core *,
211 void (*func) (unsigned long));
212 static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
213 static void prb_clear_rxhash(struct tpacket_kbdq_core *,
214 struct tpacket3_hdr *);
215 static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
216 struct tpacket3_hdr *);
217 static void packet_flush_mclist(struct sock *sk);
219 struct packet_skb_cb {
220 unsigned int origlen;
222 struct sockaddr_pkt pkt;
223 struct sockaddr_ll ll;
227 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
229 #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
230 #define GET_PBLOCK_DESC(x, bid) \
231 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
232 #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
233 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
234 #define GET_NEXT_PRB_BLK_NUM(x) \
235 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
236 ((x)->kactive_blk_num+1) : 0)
238 static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
239 static void __fanout_link(struct sock *sk, struct packet_sock *po);
241 static int packet_direct_xmit(struct sk_buff *skb)
243 struct net_device *dev = skb->dev;
244 const struct net_device_ops *ops = dev->netdev_ops;
245 netdev_features_t features;
246 struct netdev_queue *txq;
250 if (unlikely(!netif_running(dev) ||
251 !netif_carrier_ok(dev))) {
253 return NET_XMIT_DROP;
256 features = netif_skb_features(skb);
257 if (skb_needs_linearize(skb, features) &&
258 __skb_linearize(skb)) {
260 return NET_XMIT_DROP;
263 queue_map = skb_get_queue_mapping(skb);
264 txq = netdev_get_tx_queue(dev, queue_map);
266 __netif_tx_lock_bh(txq);
267 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
268 ret = NETDEV_TX_BUSY;
273 ret = ops->ndo_start_xmit(skb, dev);
274 if (likely(dev_xmit_complete(ret)))
275 txq_trans_update(txq);
279 __netif_tx_unlock_bh(txq);
283 static struct net_device *packet_cached_dev_get(struct packet_sock *po)
285 struct net_device *dev;
288 dev = rcu_dereference(po->cached_dev);
296 static void packet_cached_dev_assign(struct packet_sock *po,
297 struct net_device *dev)
299 rcu_assign_pointer(po->cached_dev, dev);
302 static void packet_cached_dev_reset(struct packet_sock *po)
304 RCU_INIT_POINTER(po->cached_dev, NULL);
307 static bool packet_use_direct_xmit(const struct packet_sock *po)
309 return po->xmit == packet_direct_xmit;
312 static u16 packet_pick_tx_queue(struct net_device *dev)
314 return (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
317 /* register_prot_hook must be invoked with the po->bind_lock held,
318 * or from a context in which asynchronous accesses to the packet
319 * socket is not possible (packet_create()).
321 static void register_prot_hook(struct sock *sk)
323 struct packet_sock *po = pkt_sk(sk);
327 __fanout_link(sk, po);
329 dev_add_pack(&po->prot_hook);
336 /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
337 * held. If the sync parameter is true, we will temporarily drop
338 * the po->bind_lock and do a synchronize_net to make sure no
339 * asynchronous packet processing paths still refer to the elements
340 * of po->prot_hook. If the sync parameter is false, it is the
341 * callers responsibility to take care of this.
343 static void __unregister_prot_hook(struct sock *sk, bool sync)
345 struct packet_sock *po = pkt_sk(sk);
350 __fanout_unlink(sk, po);
352 __dev_remove_pack(&po->prot_hook);
357 spin_unlock(&po->bind_lock);
359 spin_lock(&po->bind_lock);
363 static void unregister_prot_hook(struct sock *sk, bool sync)
365 struct packet_sock *po = pkt_sk(sk);
368 __unregister_prot_hook(sk, sync);
371 static inline __pure struct page *pgv_to_page(void *addr)
373 if (is_vmalloc_addr(addr))
374 return vmalloc_to_page(addr);
375 return virt_to_page(addr);
378 static void __packet_set_status(struct packet_sock *po, void *frame, int status)
380 union tpacket_uhdr h;
383 switch (po->tp_version) {
385 h.h1->tp_status = status;
386 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
389 h.h2->tp_status = status;
390 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
394 WARN(1, "TPACKET version not supported.\n");
401 static int __packet_get_status(struct packet_sock *po, void *frame)
403 union tpacket_uhdr h;
408 switch (po->tp_version) {
410 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
411 return h.h1->tp_status;
413 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
414 return h.h2->tp_status;
417 WARN(1, "TPACKET version not supported.\n");
423 static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
426 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
429 if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
430 ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
431 return TP_STATUS_TS_SYS_HARDWARE;
432 if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
433 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
434 return TP_STATUS_TS_RAW_HARDWARE;
437 if (ktime_to_timespec_cond(skb->tstamp, ts))
438 return TP_STATUS_TS_SOFTWARE;
443 static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
446 union tpacket_uhdr h;
450 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
454 switch (po->tp_version) {
456 h.h1->tp_sec = ts.tv_sec;
457 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
460 h.h2->tp_sec = ts.tv_sec;
461 h.h2->tp_nsec = ts.tv_nsec;
465 WARN(1, "TPACKET version not supported.\n");
469 /* one flush is safe, as both fields always lie on the same cacheline */
470 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
476 static void *packet_lookup_frame(struct packet_sock *po,
477 struct packet_ring_buffer *rb,
478 unsigned int position,
481 unsigned int pg_vec_pos, frame_offset;
482 union tpacket_uhdr h;
484 pg_vec_pos = position / rb->frames_per_block;
485 frame_offset = position % rb->frames_per_block;
487 h.raw = rb->pg_vec[pg_vec_pos].buffer +
488 (frame_offset * rb->frame_size);
490 if (status != __packet_get_status(po, h.raw))
496 static void *packet_current_frame(struct packet_sock *po,
497 struct packet_ring_buffer *rb,
500 return packet_lookup_frame(po, rb, rb->head, status);
503 static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
505 del_timer_sync(&pkc->retire_blk_timer);
508 static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
510 struct sk_buff_head *rb_queue)
512 struct tpacket_kbdq_core *pkc;
514 pkc = tx_ring ? GET_PBDQC_FROM_RB(&po->tx_ring) :
515 GET_PBDQC_FROM_RB(&po->rx_ring);
517 spin_lock_bh(&rb_queue->lock);
518 pkc->delete_blk_timer = 1;
519 spin_unlock_bh(&rb_queue->lock);
521 prb_del_retire_blk_timer(pkc);
524 static void prb_init_blk_timer(struct packet_sock *po,
525 struct tpacket_kbdq_core *pkc,
526 void (*func) (unsigned long))
528 init_timer(&pkc->retire_blk_timer);
529 pkc->retire_blk_timer.data = (long)po;
530 pkc->retire_blk_timer.function = func;
531 pkc->retire_blk_timer.expires = jiffies;
534 static void prb_setup_retire_blk_timer(struct packet_sock *po, int tx_ring)
536 struct tpacket_kbdq_core *pkc;
541 pkc = tx_ring ? GET_PBDQC_FROM_RB(&po->tx_ring) :
542 GET_PBDQC_FROM_RB(&po->rx_ring);
543 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
546 static int prb_calc_retire_blk_tmo(struct packet_sock *po,
547 int blk_size_in_bytes)
549 struct net_device *dev;
550 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
551 struct ethtool_cmd ecmd;
556 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
557 if (unlikely(!dev)) {
559 return DEFAULT_PRB_RETIRE_TOV;
561 err = __ethtool_get_settings(dev, &ecmd);
562 speed = ethtool_cmd_speed(&ecmd);
566 * If the link speed is so slow you don't really
567 * need to worry about perf anyways
569 if (speed < SPEED_1000 || speed == SPEED_UNKNOWN) {
570 return DEFAULT_PRB_RETIRE_TOV;
577 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
589 static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
590 union tpacket_req_u *req_u)
592 p1->feature_req_word = req_u->req3.tp_feature_req_word;
595 static void init_prb_bdqc(struct packet_sock *po,
596 struct packet_ring_buffer *rb,
598 union tpacket_req_u *req_u, int tx_ring)
600 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
601 struct tpacket_block_desc *pbd;
603 memset(p1, 0x0, sizeof(*p1));
605 p1->knxt_seq_num = 1;
607 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
608 p1->pkblk_start = pg_vec[0].buffer;
609 p1->kblk_size = req_u->req3.tp_block_size;
610 p1->knum_blocks = req_u->req3.tp_block_nr;
611 p1->hdrlen = po->tp_hdrlen;
612 p1->version = po->tp_version;
613 p1->last_kactive_blk_num = 0;
614 po->stats.stats3.tp_freeze_q_cnt = 0;
615 if (req_u->req3.tp_retire_blk_tov)
616 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
618 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
619 req_u->req3.tp_block_size);
620 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
621 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
623 prb_init_ft_ops(p1, req_u);
624 prb_setup_retire_blk_timer(po, tx_ring);
625 prb_open_block(p1, pbd);
628 /* Do NOT update the last_blk_num first.
629 * Assumes sk_buff_head lock is held.
631 static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
633 mod_timer(&pkc->retire_blk_timer,
634 jiffies + pkc->tov_in_jiffies);
635 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
640 * 1) We refresh the timer only when we open a block.
641 * By doing this we don't waste cycles refreshing the timer
642 * on packet-by-packet basis.
644 * With a 1MB block-size, on a 1Gbps line, it will take
645 * i) ~8 ms to fill a block + ii) memcpy etc.
646 * In this cut we are not accounting for the memcpy time.
648 * So, if the user sets the 'tmo' to 10ms then the timer
649 * will never fire while the block is still getting filled
650 * (which is what we want). However, the user could choose
651 * to close a block early and that's fine.
653 * But when the timer does fire, we check whether or not to refresh it.
654 * Since the tmo granularity is in msecs, it is not too expensive
655 * to refresh the timer, lets say every '8' msecs.
656 * Either the user can set the 'tmo' or we can derive it based on
657 * a) line-speed and b) block-size.
658 * prb_calc_retire_blk_tmo() calculates the tmo.
661 static void prb_retire_rx_blk_timer_expired(unsigned long data)
663 struct packet_sock *po = (struct packet_sock *)data;
664 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
666 struct tpacket_block_desc *pbd;
668 spin_lock(&po->sk.sk_receive_queue.lock);
670 frozen = prb_queue_frozen(pkc);
671 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
673 if (unlikely(pkc->delete_blk_timer))
676 /* We only need to plug the race when the block is partially filled.
678 * lock(); increment BLOCK_NUM_PKTS; unlock()
679 * copy_bits() is in progress ...
680 * timer fires on other cpu:
681 * we can't retire the current block because copy_bits
685 if (BLOCK_NUM_PKTS(pbd)) {
686 while (atomic_read(&pkc->blk_fill_in_prog)) {
687 /* Waiting for skb_copy_bits to finish... */
692 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
694 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
695 if (!prb_dispatch_next_block(pkc, po))
700 /* Case 1. Queue was frozen because user-space was
703 if (prb_curr_blk_in_use(pkc, pbd)) {
705 * Ok, user-space is still behind.
706 * So just refresh the timer.
710 /* Case 2. queue was frozen,user-space caught up,
711 * now the link went idle && the timer fired.
712 * We don't have a block to close.So we open this
713 * block and restart the timer.
714 * opening a block thaws the queue,restarts timer
715 * Thawing/timer-refresh is a side effect.
717 prb_open_block(pkc, pbd);
724 _prb_refresh_rx_retire_blk_timer(pkc);
727 spin_unlock(&po->sk.sk_receive_queue.lock);
730 static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
731 struct tpacket_block_desc *pbd1, __u32 status)
733 /* Flush everything minus the block header */
735 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
740 /* Skip the block header(we know header WILL fit in 4K) */
743 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
744 for (; start < end; start += PAGE_SIZE)
745 flush_dcache_page(pgv_to_page(start));
750 /* Now update the block status. */
752 BLOCK_STATUS(pbd1) = status;
754 /* Flush the block header */
756 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
758 flush_dcache_page(pgv_to_page(start));
768 * 2) Increment active_blk_num
770 * Note:We DONT refresh the timer on purpose.
771 * Because almost always the next block will be opened.
773 static void prb_close_block(struct tpacket_kbdq_core *pkc1,
774 struct tpacket_block_desc *pbd1,
775 struct packet_sock *po, unsigned int stat)
777 __u32 status = TP_STATUS_USER | stat;
779 struct tpacket3_hdr *last_pkt;
780 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
782 if (po->stats.stats3.tp_drops)
783 status |= TP_STATUS_LOSING;
785 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
786 last_pkt->tp_next_offset = 0;
788 /* Get the ts of the last pkt */
789 if (BLOCK_NUM_PKTS(pbd1)) {
790 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
791 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
793 /* Ok, we tmo'd - so get the current time */
796 h1->ts_last_pkt.ts_sec = ts.tv_sec;
797 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
802 /* Flush the block */
803 prb_flush_block(pkc1, pbd1, status);
805 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
808 static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
810 pkc->reset_pending_on_curr_blk = 0;
814 * Side effect of opening a block:
816 * 1) prb_queue is thawed.
817 * 2) retire_blk_timer is refreshed.
820 static void prb_open_block(struct tpacket_kbdq_core *pkc1,
821 struct tpacket_block_desc *pbd1)
824 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
828 /* We could have just memset this but we will lose the
829 * flexibility of making the priv area sticky
832 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
833 BLOCK_NUM_PKTS(pbd1) = 0;
834 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
838 h1->ts_first_pkt.ts_sec = ts.tv_sec;
839 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
841 pkc1->pkblk_start = (char *)pbd1;
842 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
844 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
845 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
847 pbd1->version = pkc1->version;
848 pkc1->prev = pkc1->nxt_offset;
849 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
851 prb_thaw_queue(pkc1);
852 _prb_refresh_rx_retire_blk_timer(pkc1);
858 * Queue freeze logic:
859 * 1) Assume tp_block_nr = 8 blocks.
860 * 2) At time 't0', user opens Rx ring.
861 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
862 * 4) user-space is either sleeping or processing block '0'.
863 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
864 * it will close block-7,loop around and try to fill block '0'.
866 * __packet_lookup_frame_in_block
867 * prb_retire_current_block()
868 * prb_dispatch_next_block()
869 * |->(BLOCK_STATUS == USER) evaluates to true
870 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
871 * 6) Now there are two cases:
872 * 6.1) Link goes idle right after the queue is frozen.
873 * But remember, the last open_block() refreshed the timer.
874 * When this timer expires,it will refresh itself so that we can
875 * re-open block-0 in near future.
876 * 6.2) Link is busy and keeps on receiving packets. This is a simple
877 * case and __packet_lookup_frame_in_block will check if block-0
878 * is free and can now be re-used.
880 static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
881 struct packet_sock *po)
883 pkc->reset_pending_on_curr_blk = 1;
884 po->stats.stats3.tp_freeze_q_cnt++;
887 #define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
890 * If the next block is free then we will dispatch it
891 * and return a good offset.
892 * Else, we will freeze the queue.
893 * So, caller must check the return value.
895 static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
896 struct packet_sock *po)
898 struct tpacket_block_desc *pbd;
902 /* 1. Get current block num */
903 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
905 /* 2. If this block is currently in_use then freeze the queue */
906 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
907 prb_freeze_queue(pkc, po);
913 * open this block and return the offset where the first packet
914 * needs to get stored.
916 prb_open_block(pkc, pbd);
917 return (void *)pkc->nxt_offset;
920 static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
921 struct packet_sock *po, unsigned int status)
923 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
925 /* retire/close the current block */
926 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
928 * Plug the case where copy_bits() is in progress on
929 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
930 * have space to copy the pkt in the current block and
931 * called prb_retire_current_block()
933 * We don't need to worry about the TMO case because
934 * the timer-handler already handled this case.
936 if (!(status & TP_STATUS_BLK_TMO)) {
937 while (atomic_read(&pkc->blk_fill_in_prog)) {
938 /* Waiting for skb_copy_bits to finish... */
942 prb_close_block(pkc, pbd, po, status);
947 static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
948 struct tpacket_block_desc *pbd)
950 return TP_STATUS_USER & BLOCK_STATUS(pbd);
953 static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
955 return pkc->reset_pending_on_curr_blk;
958 static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
960 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
961 atomic_dec(&pkc->blk_fill_in_prog);
964 static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
965 struct tpacket3_hdr *ppd)
967 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
970 static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
971 struct tpacket3_hdr *ppd)
973 ppd->hv1.tp_rxhash = 0;
976 static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
977 struct tpacket3_hdr *ppd)
979 if (vlan_tx_tag_present(pkc->skb)) {
980 ppd->hv1.tp_vlan_tci = vlan_tx_tag_get(pkc->skb);
981 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
982 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
984 ppd->hv1.tp_vlan_tci = 0;
985 ppd->hv1.tp_vlan_tpid = 0;
986 ppd->tp_status = TP_STATUS_AVAILABLE;
990 static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
991 struct tpacket3_hdr *ppd)
993 ppd->hv1.tp_padding = 0;
994 prb_fill_vlan_info(pkc, ppd);
996 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
997 prb_fill_rxhash(pkc, ppd);
999 prb_clear_rxhash(pkc, ppd);
1002 static void prb_fill_curr_block(char *curr,
1003 struct tpacket_kbdq_core *pkc,
1004 struct tpacket_block_desc *pbd,
1007 struct tpacket3_hdr *ppd;
1009 ppd = (struct tpacket3_hdr *)curr;
1010 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1012 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1013 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1014 BLOCK_NUM_PKTS(pbd) += 1;
1015 atomic_inc(&pkc->blk_fill_in_prog);
1016 prb_run_all_ft_ops(pkc, ppd);
1019 /* Assumes caller has the sk->rx_queue.lock */
1020 static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1021 struct sk_buff *skb,
1026 struct tpacket_kbdq_core *pkc;
1027 struct tpacket_block_desc *pbd;
1030 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
1031 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1033 /* Queue is frozen when user space is lagging behind */
1034 if (prb_queue_frozen(pkc)) {
1036 * Check if that last block which caused the queue to freeze,
1037 * is still in_use by user-space.
1039 if (prb_curr_blk_in_use(pkc, pbd)) {
1040 /* Can't record this packet */
1044 * Ok, the block was released by user-space.
1045 * Now let's open that block.
1046 * opening a block also thaws the queue.
1047 * Thawing is a side effect.
1049 prb_open_block(pkc, pbd);
1054 curr = pkc->nxt_offset;
1056 end = (char *)pbd + pkc->kblk_size;
1058 /* first try the current block */
1059 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1060 prb_fill_curr_block(curr, pkc, pbd, len);
1061 return (void *)curr;
1064 /* Ok, close the current block */
1065 prb_retire_current_block(pkc, po, 0);
1067 /* Now, try to dispatch the next block */
1068 curr = (char *)prb_dispatch_next_block(pkc, po);
1070 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1071 prb_fill_curr_block(curr, pkc, pbd, len);
1072 return (void *)curr;
1076 * No free blocks are available.user_space hasn't caught up yet.
1077 * Queue was just frozen and now this packet will get dropped.
1082 static void *packet_current_rx_frame(struct packet_sock *po,
1083 struct sk_buff *skb,
1084 int status, unsigned int len)
1087 switch (po->tp_version) {
1090 curr = packet_lookup_frame(po, &po->rx_ring,
1091 po->rx_ring.head, status);
1094 return __packet_lookup_frame_in_block(po, skb, status, len);
1096 WARN(1, "TPACKET version not supported\n");
1102 static void *prb_lookup_block(struct packet_sock *po,
1103 struct packet_ring_buffer *rb,
1107 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
1108 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
1110 if (status != BLOCK_STATUS(pbd))
1115 static int prb_previous_blk_num(struct packet_ring_buffer *rb)
1118 if (rb->prb_bdqc.kactive_blk_num)
1119 prev = rb->prb_bdqc.kactive_blk_num-1;
1121 prev = rb->prb_bdqc.knum_blocks-1;
1125 /* Assumes caller has held the rx_queue.lock */
1126 static void *__prb_previous_block(struct packet_sock *po,
1127 struct packet_ring_buffer *rb,
1130 unsigned int previous = prb_previous_blk_num(rb);
1131 return prb_lookup_block(po, rb, previous, status);
1134 static void *packet_previous_rx_frame(struct packet_sock *po,
1135 struct packet_ring_buffer *rb,
1138 if (po->tp_version <= TPACKET_V2)
1139 return packet_previous_frame(po, rb, status);
1141 return __prb_previous_block(po, rb, status);
1144 static void packet_increment_rx_head(struct packet_sock *po,
1145 struct packet_ring_buffer *rb)
1147 switch (po->tp_version) {
1150 return packet_increment_head(rb);
1153 WARN(1, "TPACKET version not supported.\n");
1159 static void *packet_previous_frame(struct packet_sock *po,
1160 struct packet_ring_buffer *rb,
1163 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1164 return packet_lookup_frame(po, rb, previous, status);
1167 static void packet_increment_head(struct packet_ring_buffer *buff)
1169 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1172 static void packet_inc_pending(struct packet_ring_buffer *rb)
1174 this_cpu_inc(*rb->pending_refcnt);
1177 static void packet_dec_pending(struct packet_ring_buffer *rb)
1179 this_cpu_dec(*rb->pending_refcnt);
1182 static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1184 unsigned int refcnt = 0;
1187 /* We don't use pending refcount in rx_ring. */
1188 if (rb->pending_refcnt == NULL)
1191 for_each_possible_cpu(cpu)
1192 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1197 static int packet_alloc_pending(struct packet_sock *po)
1199 po->rx_ring.pending_refcnt = NULL;
1201 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1202 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1208 static void packet_free_pending(struct packet_sock *po)
1210 free_percpu(po->tx_ring.pending_refcnt);
1213 static bool packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1215 struct sock *sk = &po->sk;
1218 if (po->prot_hook.func != tpacket_rcv)
1219 return (atomic_read(&sk->sk_rmem_alloc) + skb->truesize)
1222 spin_lock(&sk->sk_receive_queue.lock);
1223 if (po->tp_version == TPACKET_V3)
1224 has_room = prb_lookup_block(po, &po->rx_ring,
1225 po->rx_ring.prb_bdqc.kactive_blk_num,
1228 has_room = packet_lookup_frame(po, &po->rx_ring,
1231 spin_unlock(&sk->sk_receive_queue.lock);
1236 static void packet_sock_destruct(struct sock *sk)
1238 skb_queue_purge(&sk->sk_error_queue);
1240 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1241 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
1243 if (!sock_flag(sk, SOCK_DEAD)) {
1244 pr_err("Attempt to release alive packet socket: %p\n", sk);
1248 sk_refcnt_debug_dec(sk);
1251 static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
1253 int x = atomic_read(&f->rr_cur) + 1;
1261 static unsigned int fanout_demux_hash(struct packet_fanout *f,
1262 struct sk_buff *skb,
1265 return reciprocal_divide(skb->rxhash, num);
1268 static unsigned int fanout_demux_lb(struct packet_fanout *f,
1269 struct sk_buff *skb,
1274 cur = atomic_read(&f->rr_cur);
1275 while ((old = atomic_cmpxchg(&f->rr_cur, cur,
1276 fanout_rr_next(f, num))) != cur)
1281 static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1282 struct sk_buff *skb,
1285 return smp_processor_id() % num;
1288 static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1289 struct sk_buff *skb,
1292 return reciprocal_divide(prandom_u32(), num);
1295 static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1296 struct sk_buff *skb,
1297 unsigned int idx, unsigned int skip,
1302 i = j = min_t(int, f->next[idx], num - 1);
1304 if (i != skip && packet_rcv_has_room(pkt_sk(f->arr[i]), skb)) {
1316 static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1318 return f->flags & (flag >> 8);
1321 static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1322 struct packet_type *pt, struct net_device *orig_dev)
1324 struct packet_fanout *f = pt->af_packet_priv;
1325 unsigned int num = f->num_members;
1326 struct packet_sock *po;
1329 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
1336 case PACKET_FANOUT_HASH:
1338 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
1339 skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
1344 idx = fanout_demux_hash(f, skb, num);
1346 case PACKET_FANOUT_LB:
1347 idx = fanout_demux_lb(f, skb, num);
1349 case PACKET_FANOUT_CPU:
1350 idx = fanout_demux_cpu(f, skb, num);
1352 case PACKET_FANOUT_RND:
1353 idx = fanout_demux_rnd(f, skb, num);
1355 case PACKET_FANOUT_ROLLOVER:
1356 idx = fanout_demux_rollover(f, skb, 0, (unsigned int) -1, num);
1360 po = pkt_sk(f->arr[idx]);
1361 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER) &&
1362 unlikely(!packet_rcv_has_room(po, skb))) {
1363 idx = fanout_demux_rollover(f, skb, idx, idx, num);
1364 po = pkt_sk(f->arr[idx]);
1367 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1370 DEFINE_MUTEX(fanout_mutex);
1371 EXPORT_SYMBOL_GPL(fanout_mutex);
1372 static LIST_HEAD(fanout_list);
1374 static void __fanout_link(struct sock *sk, struct packet_sock *po)
1376 struct packet_fanout *f = po->fanout;
1378 spin_lock(&f->lock);
1379 f->arr[f->num_members] = sk;
1382 spin_unlock(&f->lock);
1385 static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1387 struct packet_fanout *f = po->fanout;
1390 spin_lock(&f->lock);
1391 for (i = 0; i < f->num_members; i++) {
1392 if (f->arr[i] == sk)
1395 BUG_ON(i >= f->num_members);
1396 f->arr[i] = f->arr[f->num_members - 1];
1398 spin_unlock(&f->lock);
1401 static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
1403 if (ptype->af_packet_priv == (void *)((struct packet_sock *)sk)->fanout)
1409 static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
1411 struct packet_sock *po = pkt_sk(sk);
1412 struct packet_fanout *f, *match;
1413 u8 type = type_flags & 0xff;
1414 u8 flags = type_flags >> 8;
1418 case PACKET_FANOUT_ROLLOVER:
1419 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1421 case PACKET_FANOUT_HASH:
1422 case PACKET_FANOUT_LB:
1423 case PACKET_FANOUT_CPU:
1424 case PACKET_FANOUT_RND:
1436 mutex_lock(&fanout_mutex);
1438 list_for_each_entry(f, &fanout_list, list) {
1440 read_pnet(&f->net) == sock_net(sk)) {
1446 if (match && match->flags != flags)
1450 match = kzalloc(sizeof(*match), GFP_KERNEL);
1453 write_pnet(&match->net, sock_net(sk));
1456 match->flags = flags;
1457 atomic_set(&match->rr_cur, 0);
1458 INIT_LIST_HEAD(&match->list);
1459 spin_lock_init(&match->lock);
1460 atomic_set(&match->sk_ref, 0);
1461 match->prot_hook.type = po->prot_hook.type;
1462 match->prot_hook.dev = po->prot_hook.dev;
1463 match->prot_hook.func = packet_rcv_fanout;
1464 match->prot_hook.af_packet_priv = match;
1465 match->prot_hook.id_match = match_fanout_group;
1466 dev_add_pack(&match->prot_hook);
1467 list_add(&match->list, &fanout_list);
1470 if (match->type == type &&
1471 match->prot_hook.type == po->prot_hook.type &&
1472 match->prot_hook.dev == po->prot_hook.dev) {
1474 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1475 __dev_remove_pack(&po->prot_hook);
1477 atomic_inc(&match->sk_ref);
1478 __fanout_link(sk, po);
1483 mutex_unlock(&fanout_mutex);
1487 static void fanout_release(struct sock *sk)
1489 struct packet_sock *po = pkt_sk(sk);
1490 struct packet_fanout *f;
1496 mutex_lock(&fanout_mutex);
1499 if (atomic_dec_and_test(&f->sk_ref)) {
1501 dev_remove_pack(&f->prot_hook);
1504 mutex_unlock(&fanout_mutex);
1507 static const struct proto_ops packet_ops;
1509 static const struct proto_ops packet_ops_spkt;
1511 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1512 struct packet_type *pt, struct net_device *orig_dev)
1515 struct sockaddr_pkt *spkt;
1518 * When we registered the protocol we saved the socket in the data
1519 * field for just this event.
1522 sk = pt->af_packet_priv;
1525 * Yank back the headers [hope the device set this
1526 * right or kerboom...]
1528 * Incoming packets have ll header pulled,
1531 * For outgoing ones skb->data == skb_mac_header(skb)
1532 * so that this procedure is noop.
1535 if (skb->pkt_type == PACKET_LOOPBACK)
1538 if (!net_eq(dev_net(dev), sock_net(sk)))
1541 skb = skb_share_check(skb, GFP_ATOMIC);
1545 /* drop any routing info */
1548 /* drop conntrack reference */
1551 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
1553 skb_push(skb, skb->data - skb_mac_header(skb));
1556 * The SOCK_PACKET socket receives _all_ frames.
1559 spkt->spkt_family = dev->type;
1560 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1561 spkt->spkt_protocol = skb->protocol;
1564 * Charge the memory to the socket. This is done specifically
1565 * to prevent sockets using all the memory up.
1568 if (sock_queue_rcv_skb(sk, skb) == 0)
1579 * Output a raw packet to a device layer. This bypasses all the other
1580 * protocol layers and you must therefore supply it with a complete frame
1583 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
1584 struct msghdr *msg, size_t len)
1586 struct sock *sk = sock->sk;
1587 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
1588 struct sk_buff *skb = NULL;
1589 struct net_device *dev;
1595 * Get and verify the address.
1599 if (msg->msg_namelen < sizeof(struct sockaddr))
1601 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1602 proto = saddr->spkt_protocol;
1604 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
1607 * Find the device first to size check it
1610 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
1613 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
1619 if (!(dev->flags & IFF_UP))
1623 * You may not queue a frame bigger than the mtu. This is the lowest level
1624 * raw protocol and you must do your own fragmentation at this level.
1627 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1628 if (!netif_supports_nofcs(dev)) {
1629 err = -EPROTONOSUPPORT;
1632 extra_len = 4; /* We're doing our own CRC */
1636 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
1640 size_t reserved = LL_RESERVED_SPACE(dev);
1641 int tlen = dev->needed_tailroom;
1642 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
1645 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
1648 /* FIXME: Save some space for broken drivers that write a hard
1649 * header at transmission time by themselves. PPP is the notable
1650 * one here. This should really be fixed at the driver level.
1652 skb_reserve(skb, reserved);
1653 skb_reset_network_header(skb);
1655 /* Try to align data part correctly */
1660 skb_reset_network_header(skb);
1662 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1668 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
1669 /* Earlier code assumed this would be a VLAN pkt,
1670 * double-check this now that we have the actual
1673 struct ethhdr *ehdr;
1674 skb_reset_mac_header(skb);
1675 ehdr = eth_hdr(skb);
1676 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1682 skb->protocol = proto;
1684 skb->priority = sk->sk_priority;
1685 skb->mark = sk->sk_mark;
1687 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
1689 if (unlikely(extra_len == 4))
1692 skb_probe_transport_header(skb, 0);
1694 dev_queue_xmit(skb);
1705 static unsigned int run_filter(const struct sk_buff *skb,
1706 const struct sock *sk,
1709 struct sk_filter *filter;
1712 filter = rcu_dereference(sk->sk_filter);
1714 res = SK_RUN_FILTER(filter, skb);
1721 * This function makes lazy skb cloning in hope that most of packets
1722 * are discarded by BPF.
1724 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1725 * and skb->cb are mangled. It works because (and until) packets
1726 * falling here are owned by current CPU. Output packets are cloned
1727 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1728 * sequencially, so that if we return skb to original state on exit,
1729 * we will not harm anyone.
1732 static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1733 struct packet_type *pt, struct net_device *orig_dev)
1736 struct sockaddr_ll *sll;
1737 struct packet_sock *po;
1738 u8 *skb_head = skb->data;
1739 int skb_len = skb->len;
1740 unsigned int snaplen, res;
1742 if (skb->pkt_type == PACKET_LOOPBACK)
1745 sk = pt->af_packet_priv;
1748 if (!net_eq(dev_net(dev), sock_net(sk)))
1753 if (dev->header_ops) {
1754 /* The device has an explicit notion of ll header,
1755 * exported to higher levels.
1757 * Otherwise, the device hides details of its frame
1758 * structure, so that corresponding packet head is
1759 * never delivered to user.
1761 if (sk->sk_type != SOCK_DGRAM)
1762 skb_push(skb, skb->data - skb_mac_header(skb));
1763 else if (skb->pkt_type == PACKET_OUTGOING) {
1764 /* Special case: outgoing packets have ll header at head */
1765 skb_pull(skb, skb_network_offset(skb));
1771 res = run_filter(skb, sk, snaplen);
1773 goto drop_n_restore;
1777 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
1780 if (skb_shared(skb)) {
1781 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1785 if (skb_head != skb->data) {
1786 skb->data = skb_head;
1793 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
1796 sll = &PACKET_SKB_CB(skb)->sa.ll;
1797 sll->sll_family = AF_PACKET;
1798 sll->sll_hatype = dev->type;
1799 sll->sll_protocol = skb->protocol;
1800 sll->sll_pkttype = skb->pkt_type;
1801 if (unlikely(po->origdev))
1802 sll->sll_ifindex = orig_dev->ifindex;
1804 sll->sll_ifindex = dev->ifindex;
1806 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
1808 PACKET_SKB_CB(skb)->origlen = skb->len;
1810 if (pskb_trim(skb, snaplen))
1813 skb_set_owner_r(skb, sk);
1817 /* drop conntrack reference */
1820 spin_lock(&sk->sk_receive_queue.lock);
1821 po->stats.stats1.tp_packets++;
1822 skb->dropcount = atomic_read(&sk->sk_drops);
1823 __skb_queue_tail(&sk->sk_receive_queue, skb);
1824 spin_unlock(&sk->sk_receive_queue.lock);
1825 sk->sk_data_ready(sk, skb->len);
1829 spin_lock(&sk->sk_receive_queue.lock);
1830 po->stats.stats1.tp_drops++;
1831 atomic_inc(&sk->sk_drops);
1832 spin_unlock(&sk->sk_receive_queue.lock);
1835 if (skb_head != skb->data && skb_shared(skb)) {
1836 skb->data = skb_head;
1844 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
1845 struct packet_type *pt, struct net_device *orig_dev)
1848 struct packet_sock *po;
1849 struct sockaddr_ll *sll;
1850 union tpacket_uhdr h;
1851 u8 *skb_head = skb->data;
1852 int skb_len = skb->len;
1853 unsigned int snaplen, res;
1854 unsigned long status = TP_STATUS_USER;
1855 unsigned short macoff, netoff, hdrlen;
1856 struct sk_buff *copy_skb = NULL;
1860 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
1861 * We may add members to them until current aligned size without forcing
1862 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
1864 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
1865 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
1867 if (skb->pkt_type == PACKET_LOOPBACK)
1870 sk = pt->af_packet_priv;
1873 if (!net_eq(dev_net(dev), sock_net(sk)))
1876 if (dev->header_ops) {
1877 if (sk->sk_type != SOCK_DGRAM)
1878 skb_push(skb, skb->data - skb_mac_header(skb));
1879 else if (skb->pkt_type == PACKET_OUTGOING) {
1880 /* Special case: outgoing packets have ll header at head */
1881 skb_pull(skb, skb_network_offset(skb));
1885 if (skb->ip_summed == CHECKSUM_PARTIAL)
1886 status |= TP_STATUS_CSUMNOTREADY;
1890 res = run_filter(skb, sk, snaplen);
1892 goto drop_n_restore;
1896 if (sk->sk_type == SOCK_DGRAM) {
1897 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1900 unsigned int maclen = skb_network_offset(skb);
1901 netoff = TPACKET_ALIGN(po->tp_hdrlen +
1902 (maclen < 16 ? 16 : maclen)) +
1904 macoff = netoff - maclen;
1906 if (po->tp_version <= TPACKET_V2) {
1907 if (macoff + snaplen > po->rx_ring.frame_size) {
1908 if (po->copy_thresh &&
1909 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
1910 if (skb_shared(skb)) {
1911 copy_skb = skb_clone(skb, GFP_ATOMIC);
1913 copy_skb = skb_get(skb);
1914 skb_head = skb->data;
1917 skb_set_owner_r(copy_skb, sk);
1919 snaplen = po->rx_ring.frame_size - macoff;
1920 if ((int)snaplen < 0)
1924 spin_lock(&sk->sk_receive_queue.lock);
1925 h.raw = packet_current_rx_frame(po, skb,
1926 TP_STATUS_KERNEL, (macoff+snaplen));
1929 if (po->tp_version <= TPACKET_V2) {
1930 packet_increment_rx_head(po, &po->rx_ring);
1932 * LOSING will be reported till you read the stats,
1933 * because it's COR - Clear On Read.
1934 * Anyways, moving it for V1/V2 only as V3 doesn't need this
1937 if (po->stats.stats1.tp_drops)
1938 status |= TP_STATUS_LOSING;
1940 po->stats.stats1.tp_packets++;
1942 status |= TP_STATUS_COPY;
1943 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1945 spin_unlock(&sk->sk_receive_queue.lock);
1947 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
1949 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
1950 getnstimeofday(&ts);
1952 status |= ts_status;
1954 switch (po->tp_version) {
1956 h.h1->tp_len = skb->len;
1957 h.h1->tp_snaplen = snaplen;
1958 h.h1->tp_mac = macoff;
1959 h.h1->tp_net = netoff;
1960 h.h1->tp_sec = ts.tv_sec;
1961 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
1962 hdrlen = sizeof(*h.h1);
1965 h.h2->tp_len = skb->len;
1966 h.h2->tp_snaplen = snaplen;
1967 h.h2->tp_mac = macoff;
1968 h.h2->tp_net = netoff;
1969 h.h2->tp_sec = ts.tv_sec;
1970 h.h2->tp_nsec = ts.tv_nsec;
1971 if (vlan_tx_tag_present(skb)) {
1972 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1973 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
1974 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
1976 h.h2->tp_vlan_tci = 0;
1977 h.h2->tp_vlan_tpid = 0;
1979 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
1980 hdrlen = sizeof(*h.h2);
1983 /* tp_nxt_offset,vlan are already populated above.
1984 * So DONT clear those fields here
1986 h.h3->tp_status |= status;
1987 h.h3->tp_len = skb->len;
1988 h.h3->tp_snaplen = snaplen;
1989 h.h3->tp_mac = macoff;
1990 h.h3->tp_net = netoff;
1991 h.h3->tp_sec = ts.tv_sec;
1992 h.h3->tp_nsec = ts.tv_nsec;
1993 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
1994 hdrlen = sizeof(*h.h3);
2000 sll = h.raw + TPACKET_ALIGN(hdrlen);
2001 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
2002 sll->sll_family = AF_PACKET;
2003 sll->sll_hatype = dev->type;
2004 sll->sll_protocol = skb->protocol;
2005 sll->sll_pkttype = skb->pkt_type;
2006 if (unlikely(po->origdev))
2007 sll->sll_ifindex = orig_dev->ifindex;
2009 sll->sll_ifindex = dev->ifindex;
2012 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
2016 if (po->tp_version <= TPACKET_V2) {
2017 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw
2018 + macoff + snaplen);
2019 for (start = h.raw; start < end; start += PAGE_SIZE)
2020 flush_dcache_page(pgv_to_page(start));
2025 if (po->tp_version <= TPACKET_V2)
2026 __packet_set_status(po, h.raw, status);
2028 prb_clear_blk_fill_status(&po->rx_ring);
2030 sk->sk_data_ready(sk, 0);
2033 if (skb_head != skb->data && skb_shared(skb)) {
2034 skb->data = skb_head;
2042 po->stats.stats1.tp_drops++;
2043 spin_unlock(&sk->sk_receive_queue.lock);
2045 sk->sk_data_ready(sk, 0);
2046 kfree_skb(copy_skb);
2047 goto drop_n_restore;
2050 static void tpacket_destruct_skb(struct sk_buff *skb)
2052 struct packet_sock *po = pkt_sk(skb->sk);
2055 if (likely(po->tx_ring.pg_vec)) {
2058 ph = skb_shinfo(skb)->destructor_arg;
2059 packet_dec_pending(&po->tx_ring);
2061 ts = __packet_set_timestamp(po, ph, skb);
2062 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
2068 static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
2069 void *frame, struct net_device *dev, int size_max,
2070 __be16 proto, unsigned char *addr, int hlen)
2072 union tpacket_uhdr ph;
2073 int to_write, offset, len, tp_len, nr_frags, len_max;
2074 struct socket *sock = po->sk.sk_socket;
2081 skb->protocol = proto;
2083 skb->priority = po->sk.sk_priority;
2084 skb->mark = po->sk.sk_mark;
2085 sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
2086 skb_shinfo(skb)->destructor_arg = ph.raw;
2088 switch (po->tp_version) {
2090 tp_len = ph.h2->tp_len;
2093 tp_len = ph.h1->tp_len;
2096 if (unlikely(tp_len > size_max)) {
2097 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2101 skb_reserve(skb, hlen);
2102 skb_reset_network_header(skb);
2104 if (!packet_use_direct_xmit(po))
2105 skb_probe_transport_header(skb, 0);
2106 if (unlikely(po->tp_tx_has_off)) {
2107 int off_min, off_max, off;
2108 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2109 off_max = po->tx_ring.frame_size - tp_len;
2110 if (sock->type == SOCK_DGRAM) {
2111 switch (po->tp_version) {
2113 off = ph.h2->tp_net;
2116 off = ph.h1->tp_net;
2120 switch (po->tp_version) {
2122 off = ph.h2->tp_mac;
2125 off = ph.h1->tp_mac;
2129 if (unlikely((off < off_min) || (off_max < off)))
2131 data = ph.raw + off;
2133 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
2137 if (sock->type == SOCK_DGRAM) {
2138 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2140 if (unlikely(err < 0))
2142 } else if (dev->hard_header_len) {
2143 /* net device doesn't like empty head */
2144 if (unlikely(tp_len <= dev->hard_header_len)) {
2145 pr_err("packet size is too short (%d < %d)\n",
2146 tp_len, dev->hard_header_len);
2150 skb_push(skb, dev->hard_header_len);
2151 err = skb_store_bits(skb, 0, data,
2152 dev->hard_header_len);
2156 data += dev->hard_header_len;
2157 to_write -= dev->hard_header_len;
2160 offset = offset_in_page(data);
2161 len_max = PAGE_SIZE - offset;
2162 len = ((to_write > len_max) ? len_max : to_write);
2164 skb->data_len = to_write;
2165 skb->len += to_write;
2166 skb->truesize += to_write;
2167 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2169 while (likely(to_write)) {
2170 nr_frags = skb_shinfo(skb)->nr_frags;
2172 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
2173 pr_err("Packet exceed the number of skb frags(%lu)\n",
2178 page = pgv_to_page(data);
2180 flush_dcache_page(page);
2182 skb_fill_page_desc(skb, nr_frags, page, offset, len);
2185 len_max = PAGE_SIZE;
2186 len = ((to_write > len_max) ? len_max : to_write);
2192 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2194 struct sk_buff *skb;
2195 struct net_device *dev;
2197 int err, reserve = 0;
2199 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
2200 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
2201 int tp_len, size_max;
2202 unsigned char *addr;
2204 int status = TP_STATUS_AVAILABLE;
2207 mutex_lock(&po->pg_vec_lock);
2209 if (likely(saddr == NULL)) {
2210 dev = packet_cached_dev_get(po);
2215 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2217 if (msg->msg_namelen < (saddr->sll_halen
2218 + offsetof(struct sockaddr_ll,
2221 proto = saddr->sll_protocol;
2222 addr = saddr->sll_addr;
2223 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
2227 if (unlikely(dev == NULL))
2230 if (unlikely(!(dev->flags & IFF_UP)))
2233 reserve = dev->hard_header_len;
2235 size_max = po->tx_ring.frame_size
2236 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
2238 if (size_max > dev->mtu + reserve)
2239 size_max = dev->mtu + reserve;
2242 ph = packet_current_frame(po, &po->tx_ring,
2243 TP_STATUS_SEND_REQUEST);
2244 if (unlikely(ph == NULL)) {
2245 if (need_wait && need_resched())
2250 status = TP_STATUS_SEND_REQUEST;
2251 hlen = LL_RESERVED_SPACE(dev);
2252 tlen = dev->needed_tailroom;
2253 skb = sock_alloc_send_skb(&po->sk,
2254 hlen + tlen + sizeof(struct sockaddr_ll),
2257 if (unlikely(skb == NULL))
2260 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
2263 if (unlikely(tp_len < 0)) {
2265 __packet_set_status(po, ph,
2266 TP_STATUS_AVAILABLE);
2267 packet_increment_head(&po->tx_ring);
2271 status = TP_STATUS_WRONG_FORMAT;
2277 skb_set_queue_mapping(skb, packet_pick_tx_queue(dev));
2278 skb->destructor = tpacket_destruct_skb;
2279 __packet_set_status(po, ph, TP_STATUS_SENDING);
2280 packet_inc_pending(&po->tx_ring);
2282 status = TP_STATUS_SEND_REQUEST;
2283 err = po->xmit(skb);
2284 if (unlikely(err > 0)) {
2285 err = net_xmit_errno(err);
2286 if (err && __packet_get_status(po, ph) ==
2287 TP_STATUS_AVAILABLE) {
2288 /* skb was destructed already */
2293 * skb was dropped but not destructed yet;
2294 * let's treat it like congestion or err < 0
2298 packet_increment_head(&po->tx_ring);
2300 } while (likely((ph != NULL) ||
2301 /* Note: packet_read_pending() might be slow if we have
2302 * to call it as it's per_cpu variable, but in fast-path
2303 * we already short-circuit the loop with the first
2304 * condition, and luckily don't have to go that path
2307 (need_wait && packet_read_pending(&po->tx_ring))));
2313 __packet_set_status(po, ph, status);
2318 mutex_unlock(&po->pg_vec_lock);
2322 static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2323 size_t reserve, size_t len,
2324 size_t linear, int noblock,
2327 struct sk_buff *skb;
2329 /* Under a page? Don't bother with paged skb. */
2330 if (prepad + len < PAGE_SIZE || !linear)
2333 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
2338 skb_reserve(skb, reserve);
2339 skb_put(skb, linear);
2340 skb->data_len = len - linear;
2341 skb->len += len - linear;
2346 static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
2348 struct sock *sk = sock->sk;
2349 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
2350 struct sk_buff *skb;
2351 struct net_device *dev;
2353 unsigned char *addr;
2354 int err, reserve = 0;
2355 struct virtio_net_hdr vnet_hdr = { 0 };
2358 struct packet_sock *po = pkt_sk(sk);
2359 unsigned short gso_type = 0;
2364 * Get and verify the address.
2367 if (likely(saddr == NULL)) {
2368 dev = packet_cached_dev_get(po);
2373 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2375 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2377 proto = saddr->sll_protocol;
2378 addr = saddr->sll_addr;
2379 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
2383 if (unlikely(dev == NULL))
2386 if (unlikely(!(dev->flags & IFF_UP)))
2389 if (sock->type == SOCK_RAW)
2390 reserve = dev->hard_header_len;
2391 if (po->has_vnet_hdr) {
2392 vnet_hdr_len = sizeof(vnet_hdr);
2395 if (len < vnet_hdr_len)
2398 len -= vnet_hdr_len;
2400 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
2405 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2406 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
2408 vnet_hdr.hdr_len = vnet_hdr.csum_start +
2409 vnet_hdr.csum_offset + 2;
2412 if (vnet_hdr.hdr_len > len)
2415 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2416 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2417 case VIRTIO_NET_HDR_GSO_TCPV4:
2418 gso_type = SKB_GSO_TCPV4;
2420 case VIRTIO_NET_HDR_GSO_TCPV6:
2421 gso_type = SKB_GSO_TCPV6;
2423 case VIRTIO_NET_HDR_GSO_UDP:
2424 gso_type = SKB_GSO_UDP;
2430 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
2431 gso_type |= SKB_GSO_TCP_ECN;
2433 if (vnet_hdr.gso_size == 0)
2439 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2440 if (!netif_supports_nofcs(dev)) {
2441 err = -EPROTONOSUPPORT;
2444 extra_len = 4; /* We're doing our own CRC */
2448 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
2452 hlen = LL_RESERVED_SPACE(dev);
2453 tlen = dev->needed_tailroom;
2454 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, vnet_hdr.hdr_len,
2455 msg->msg_flags & MSG_DONTWAIT, &err);
2459 skb_set_network_header(skb, reserve);
2462 if (sock->type == SOCK_DGRAM &&
2463 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
2466 /* Returns -EFAULT on error */
2467 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
2471 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
2473 if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
2474 /* Earlier code assumed this would be a VLAN pkt,
2475 * double-check this now that we have the actual
2478 struct ethhdr *ehdr;
2479 skb_reset_mac_header(skb);
2480 ehdr = eth_hdr(skb);
2481 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
2487 skb->protocol = proto;
2489 skb->priority = sk->sk_priority;
2490 skb->mark = sk->sk_mark;
2491 skb_set_queue_mapping(skb, packet_pick_tx_queue(dev));
2493 if (po->has_vnet_hdr) {
2494 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2495 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
2496 vnet_hdr.csum_offset)) {
2502 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
2503 skb_shinfo(skb)->gso_type = gso_type;
2505 /* Header must be checked, and gso_segs computed. */
2506 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2507 skb_shinfo(skb)->gso_segs = 0;
2509 len += vnet_hdr_len;
2512 if (!packet_use_direct_xmit(po))
2513 skb_probe_transport_header(skb, reserve);
2514 if (unlikely(extra_len == 4))
2517 err = po->xmit(skb);
2518 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2534 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
2535 struct msghdr *msg, size_t len)
2537 struct sock *sk = sock->sk;
2538 struct packet_sock *po = pkt_sk(sk);
2540 if (po->tx_ring.pg_vec)
2541 return tpacket_snd(po, msg);
2543 return packet_snd(sock, msg, len);
2547 * Close a PACKET socket. This is fairly simple. We immediately go
2548 * to 'closed' state and remove our protocol entry in the device list.
2551 static int packet_release(struct socket *sock)
2553 struct sock *sk = sock->sk;
2554 struct packet_sock *po;
2556 union tpacket_req_u req_u;
2564 mutex_lock(&net->packet.sklist_lock);
2565 sk_del_node_init_rcu(sk);
2566 mutex_unlock(&net->packet.sklist_lock);
2569 sock_prot_inuse_add(net, sk->sk_prot, -1);
2572 spin_lock(&po->bind_lock);
2573 unregister_prot_hook(sk, false);
2574 packet_cached_dev_reset(po);
2576 if (po->prot_hook.dev) {
2577 dev_put(po->prot_hook.dev);
2578 po->prot_hook.dev = NULL;
2580 spin_unlock(&po->bind_lock);
2582 packet_flush_mclist(sk);
2584 if (po->rx_ring.pg_vec) {
2585 memset(&req_u, 0, sizeof(req_u));
2586 packet_set_ring(sk, &req_u, 1, 0);
2589 if (po->tx_ring.pg_vec) {
2590 memset(&req_u, 0, sizeof(req_u));
2591 packet_set_ring(sk, &req_u, 1, 1);
2598 * Now the socket is dead. No more input will appear.
2605 skb_queue_purge(&sk->sk_receive_queue);
2606 packet_free_pending(po);
2607 sk_refcnt_debug_release(sk);
2614 * Attach a packet hook.
2617 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 proto)
2619 struct packet_sock *po = pkt_sk(sk);
2620 const struct net_device *dev_curr;
2632 spin_lock(&po->bind_lock);
2634 proto_curr = po->prot_hook.type;
2635 dev_curr = po->prot_hook.dev;
2637 need_rehook = proto_curr != proto || dev_curr != dev;
2640 unregister_prot_hook(sk, true);
2643 po->prot_hook.type = proto;
2645 if (po->prot_hook.dev)
2646 dev_put(po->prot_hook.dev);
2648 po->prot_hook.dev = dev;
2650 po->ifindex = dev ? dev->ifindex : 0;
2651 packet_cached_dev_assign(po, dev);
2654 if (proto == 0 || !need_rehook)
2657 if (!dev || (dev->flags & IFF_UP)) {
2658 register_prot_hook(sk);
2660 sk->sk_err = ENETDOWN;
2661 if (!sock_flag(sk, SOCK_DEAD))
2662 sk->sk_error_report(sk);
2666 spin_unlock(&po->bind_lock);
2672 * Bind a packet socket to a device
2675 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
2678 struct sock *sk = sock->sk;
2680 struct net_device *dev;
2687 if (addr_len != sizeof(struct sockaddr))
2689 strlcpy(name, uaddr->sa_data, sizeof(name));
2691 dev = dev_get_by_name(sock_net(sk), name);
2693 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
2697 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2699 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
2700 struct sock *sk = sock->sk;
2701 struct net_device *dev = NULL;
2709 if (addr_len < sizeof(struct sockaddr_ll))
2711 if (sll->sll_family != AF_PACKET)
2714 if (sll->sll_ifindex) {
2716 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
2720 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
2726 static struct proto packet_proto = {
2728 .owner = THIS_MODULE,
2729 .obj_size = sizeof(struct packet_sock),
2733 * Create a packet of type SOCK_PACKET.
2736 static int packet_create(struct net *net, struct socket *sock, int protocol,
2740 struct packet_sock *po;
2741 __be16 proto = (__force __be16)protocol; /* weird, but documented */
2744 if (!ns_capable(net->user_ns, CAP_NET_RAW))
2746 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
2747 sock->type != SOCK_PACKET)
2748 return -ESOCKTNOSUPPORT;
2750 sock->state = SS_UNCONNECTED;
2753 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
2757 sock->ops = &packet_ops;
2758 if (sock->type == SOCK_PACKET)
2759 sock->ops = &packet_ops_spkt;
2761 sock_init_data(sock, sk);
2764 sk->sk_family = PF_PACKET;
2766 po->xmit = dev_queue_xmit;
2768 err = packet_alloc_pending(po);
2772 packet_cached_dev_reset(po);
2774 sk->sk_destruct = packet_sock_destruct;
2775 sk_refcnt_debug_inc(sk);
2778 * Attach a protocol block
2781 spin_lock_init(&po->bind_lock);
2782 mutex_init(&po->pg_vec_lock);
2783 po->prot_hook.func = packet_rcv;
2785 if (sock->type == SOCK_PACKET)
2786 po->prot_hook.func = packet_rcv_spkt;
2788 po->prot_hook.af_packet_priv = sk;
2791 po->prot_hook.type = proto;
2792 register_prot_hook(sk);
2795 mutex_lock(&net->packet.sklist_lock);
2796 sk_add_node_rcu(sk, &net->packet.sklist);
2797 mutex_unlock(&net->packet.sklist_lock);
2800 sock_prot_inuse_add(net, &packet_proto, 1);
2811 * Pull a packet from our receive queue and hand it to the user.
2812 * If necessary we block.
2815 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2816 struct msghdr *msg, size_t len, int flags)
2818 struct sock *sk = sock->sk;
2819 struct sk_buff *skb;
2821 int vnet_hdr_len = 0;
2824 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
2828 /* What error should we return now? EUNATTACH? */
2829 if (pkt_sk(sk)->ifindex < 0)
2833 if (flags & MSG_ERRQUEUE) {
2834 err = sock_recv_errqueue(sk, msg, len,
2835 SOL_PACKET, PACKET_TX_TIMESTAMP);
2840 * Call the generic datagram receiver. This handles all sorts
2841 * of horrible races and re-entrancy so we can forget about it
2842 * in the protocol layers.
2844 * Now it will return ENETDOWN, if device have just gone down,
2845 * but then it will block.
2848 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2851 * An error occurred so return it. Because skb_recv_datagram()
2852 * handles the blocking we don't see and worry about blocking
2859 if (pkt_sk(sk)->has_vnet_hdr) {
2860 struct virtio_net_hdr vnet_hdr = { 0 };
2863 vnet_hdr_len = sizeof(vnet_hdr);
2864 if (len < vnet_hdr_len)
2867 len -= vnet_hdr_len;
2869 if (skb_is_gso(skb)) {
2870 struct skb_shared_info *sinfo = skb_shinfo(skb);
2872 /* This is a hint as to how much should be linear. */
2873 vnet_hdr.hdr_len = skb_headlen(skb);
2874 vnet_hdr.gso_size = sinfo->gso_size;
2875 if (sinfo->gso_type & SKB_GSO_TCPV4)
2876 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2877 else if (sinfo->gso_type & SKB_GSO_TCPV6)
2878 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2879 else if (sinfo->gso_type & SKB_GSO_UDP)
2880 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
2881 else if (sinfo->gso_type & SKB_GSO_FCOE)
2885 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
2886 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2888 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
2890 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2891 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
2892 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
2893 vnet_hdr.csum_offset = skb->csum_offset;
2894 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
2895 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
2896 } /* else everything is zero */
2898 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
2904 /* You lose any data beyond the buffer you gave. If it worries
2905 * a user program they can ask the device for its MTU
2911 msg->msg_flags |= MSG_TRUNC;
2914 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2918 sock_recv_ts_and_drops(msg, sk, skb);
2920 if (msg->msg_name) {
2921 /* If the address length field is there to be filled
2922 * in, we fill it in now.
2924 if (sock->type == SOCK_PACKET) {
2925 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2927 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
2928 msg->msg_namelen = sll->sll_halen +
2929 offsetof(struct sockaddr_ll, sll_addr);
2931 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2935 if (pkt_sk(sk)->auxdata) {
2936 struct tpacket_auxdata aux;
2938 aux.tp_status = TP_STATUS_USER;
2939 if (skb->ip_summed == CHECKSUM_PARTIAL)
2940 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
2941 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
2942 aux.tp_snaplen = skb->len;
2944 aux.tp_net = skb_network_offset(skb);
2945 if (vlan_tx_tag_present(skb)) {
2946 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
2947 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
2948 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
2950 aux.tp_vlan_tci = 0;
2951 aux.tp_vlan_tpid = 0;
2953 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
2957 * Free or return the buffer as appropriate. Again this
2958 * hides all the races and re-entrancy issues from us.
2960 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
2963 skb_free_datagram(sk, skb);
2968 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2969 int *uaddr_len, int peer)
2971 struct net_device *dev;
2972 struct sock *sk = sock->sk;
2977 uaddr->sa_family = AF_PACKET;
2978 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
2980 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2982 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
2984 *uaddr_len = sizeof(*uaddr);
2989 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2990 int *uaddr_len, int peer)
2992 struct net_device *dev;
2993 struct sock *sk = sock->sk;
2994 struct packet_sock *po = pkt_sk(sk);
2995 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
3000 sll->sll_family = AF_PACKET;
3001 sll->sll_ifindex = po->ifindex;
3002 sll->sll_protocol = po->num;
3003 sll->sll_pkttype = 0;
3005 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
3007 sll->sll_hatype = dev->type;
3008 sll->sll_halen = dev->addr_len;
3009 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
3011 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3015 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
3020 static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3024 case PACKET_MR_MULTICAST:
3025 if (i->alen != dev->addr_len)
3028 return dev_mc_add(dev, i->addr);
3030 return dev_mc_del(dev, i->addr);
3032 case PACKET_MR_PROMISC:
3033 return dev_set_promiscuity(dev, what);
3035 case PACKET_MR_ALLMULTI:
3036 return dev_set_allmulti(dev, what);
3038 case PACKET_MR_UNICAST:
3039 if (i->alen != dev->addr_len)
3042 return dev_uc_add(dev, i->addr);
3044 return dev_uc_del(dev, i->addr);
3052 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
3054 for ( ; i; i = i->next) {
3055 if (i->ifindex == dev->ifindex)
3056 packet_dev_mc(dev, i, what);
3060 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
3062 struct packet_sock *po = pkt_sk(sk);
3063 struct packet_mclist *ml, *i;
3064 struct net_device *dev;
3070 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
3075 if (mreq->mr_alen > dev->addr_len)
3079 i = kmalloc(sizeof(*i), GFP_KERNEL);
3084 for (ml = po->mclist; ml; ml = ml->next) {
3085 if (ml->ifindex == mreq->mr_ifindex &&
3086 ml->type == mreq->mr_type &&
3087 ml->alen == mreq->mr_alen &&
3088 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3090 /* Free the new element ... */
3096 i->type = mreq->mr_type;
3097 i->ifindex = mreq->mr_ifindex;
3098 i->alen = mreq->mr_alen;
3099 memcpy(i->addr, mreq->mr_address, i->alen);
3101 i->next = po->mclist;
3103 err = packet_dev_mc(dev, i, 1);
3105 po->mclist = i->next;
3114 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
3116 struct packet_mclist *ml, **mlp;
3120 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3121 if (ml->ifindex == mreq->mr_ifindex &&
3122 ml->type == mreq->mr_type &&
3123 ml->alen == mreq->mr_alen &&
3124 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3125 if (--ml->count == 0) {
3126 struct net_device *dev;
3128 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3130 packet_dev_mc(dev, ml, -1);
3138 return -EADDRNOTAVAIL;
3141 static void packet_flush_mclist(struct sock *sk)
3143 struct packet_sock *po = pkt_sk(sk);
3144 struct packet_mclist *ml;
3150 while ((ml = po->mclist) != NULL) {
3151 struct net_device *dev;
3153 po->mclist = ml->next;
3154 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3156 packet_dev_mc(dev, ml, -1);
3163 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
3165 struct sock *sk = sock->sk;
3166 struct packet_sock *po = pkt_sk(sk);
3169 if (level != SOL_PACKET)
3170 return -ENOPROTOOPT;
3173 case PACKET_ADD_MEMBERSHIP:
3174 case PACKET_DROP_MEMBERSHIP:
3176 struct packet_mreq_max mreq;
3178 memset(&mreq, 0, sizeof(mreq));
3179 if (len < sizeof(struct packet_mreq))
3181 if (len > sizeof(mreq))
3183 if (copy_from_user(&mreq, optval, len))
3185 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3187 if (optname == PACKET_ADD_MEMBERSHIP)
3188 ret = packet_mc_add(sk, &mreq);
3190 ret = packet_mc_drop(sk, &mreq);
3194 case PACKET_RX_RING:
3195 case PACKET_TX_RING:
3197 union tpacket_req_u req_u;
3200 switch (po->tp_version) {
3203 len = sizeof(req_u.req);
3207 len = sizeof(req_u.req3);
3212 if (pkt_sk(sk)->has_vnet_hdr)
3214 if (copy_from_user(&req_u.req, optval, len))
3216 return packet_set_ring(sk, &req_u, 0,
3217 optname == PACKET_TX_RING);
3219 case PACKET_COPY_THRESH:
3223 if (optlen != sizeof(val))
3225 if (copy_from_user(&val, optval, sizeof(val)))
3228 pkt_sk(sk)->copy_thresh = val;
3231 case PACKET_VERSION:
3235 if (optlen != sizeof(val))
3237 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3239 if (copy_from_user(&val, optval, sizeof(val)))
3245 po->tp_version = val;
3251 case PACKET_RESERVE:
3255 if (optlen != sizeof(val))
3257 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3259 if (copy_from_user(&val, optval, sizeof(val)))
3261 po->tp_reserve = val;
3268 if (optlen != sizeof(val))
3270 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3272 if (copy_from_user(&val, optval, sizeof(val)))
3274 po->tp_loss = !!val;
3277 case PACKET_AUXDATA:
3281 if (optlen < sizeof(val))
3283 if (copy_from_user(&val, optval, sizeof(val)))
3286 po->auxdata = !!val;
3289 case PACKET_ORIGDEV:
3293 if (optlen < sizeof(val))
3295 if (copy_from_user(&val, optval, sizeof(val)))
3298 po->origdev = !!val;
3301 case PACKET_VNET_HDR:
3305 if (sock->type != SOCK_RAW)
3307 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3309 if (optlen < sizeof(val))
3311 if (copy_from_user(&val, optval, sizeof(val)))
3314 po->has_vnet_hdr = !!val;
3317 case PACKET_TIMESTAMP:
3321 if (optlen != sizeof(val))
3323 if (copy_from_user(&val, optval, sizeof(val)))
3326 po->tp_tstamp = val;
3333 if (optlen != sizeof(val))
3335 if (copy_from_user(&val, optval, sizeof(val)))
3338 return fanout_add(sk, val & 0xffff, val >> 16);
3340 case PACKET_TX_HAS_OFF:
3344 if (optlen != sizeof(val))
3346 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3348 if (copy_from_user(&val, optval, sizeof(val)))
3350 po->tp_tx_has_off = !!val;
3353 case PACKET_QDISC_BYPASS:
3357 if (optlen != sizeof(val))
3359 if (copy_from_user(&val, optval, sizeof(val)))
3362 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3366 return -ENOPROTOOPT;
3370 static int packet_getsockopt(struct socket *sock, int level, int optname,
3371 char __user *optval, int __user *optlen)
3374 int val, lv = sizeof(val);
3375 struct sock *sk = sock->sk;
3376 struct packet_sock *po = pkt_sk(sk);
3378 union tpacket_stats_u st;
3380 if (level != SOL_PACKET)
3381 return -ENOPROTOOPT;
3383 if (get_user(len, optlen))
3390 case PACKET_STATISTICS:
3391 spin_lock_bh(&sk->sk_receive_queue.lock);
3392 memcpy(&st, &po->stats, sizeof(st));
3393 memset(&po->stats, 0, sizeof(po->stats));
3394 spin_unlock_bh(&sk->sk_receive_queue.lock);
3396 if (po->tp_version == TPACKET_V3) {
3397 lv = sizeof(struct tpacket_stats_v3);
3398 st.stats3.tp_packets += st.stats3.tp_drops;
3401 lv = sizeof(struct tpacket_stats);
3402 st.stats1.tp_packets += st.stats1.tp_drops;
3407 case PACKET_AUXDATA:
3410 case PACKET_ORIGDEV:
3413 case PACKET_VNET_HDR:
3414 val = po->has_vnet_hdr;
3416 case PACKET_VERSION:
3417 val = po->tp_version;
3420 if (len > sizeof(int))
3422 if (copy_from_user(&val, optval, len))
3426 val = sizeof(struct tpacket_hdr);
3429 val = sizeof(struct tpacket2_hdr);
3432 val = sizeof(struct tpacket3_hdr);
3438 case PACKET_RESERVE:
3439 val = po->tp_reserve;
3444 case PACKET_TIMESTAMP:
3445 val = po->tp_tstamp;
3449 ((u32)po->fanout->id |
3450 ((u32)po->fanout->type << 16) |
3451 ((u32)po->fanout->flags << 24)) :
3454 case PACKET_TX_HAS_OFF:
3455 val = po->tp_tx_has_off;
3457 case PACKET_QDISC_BYPASS:
3458 val = packet_use_direct_xmit(po);
3461 return -ENOPROTOOPT;
3466 if (put_user(len, optlen))
3468 if (copy_to_user(optval, data, len))
3474 static int packet_notifier(struct notifier_block *this,
3475 unsigned long msg, void *ptr)
3478 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3479 struct net *net = dev_net(dev);
3482 sk_for_each_rcu(sk, &net->packet.sklist) {
3483 struct packet_sock *po = pkt_sk(sk);
3486 case NETDEV_UNREGISTER:
3488 packet_dev_mclist(dev, po->mclist, -1);
3492 if (dev->ifindex == po->ifindex) {
3493 spin_lock(&po->bind_lock);
3495 __unregister_prot_hook(sk, false);
3496 sk->sk_err = ENETDOWN;
3497 if (!sock_flag(sk, SOCK_DEAD))
3498 sk->sk_error_report(sk);
3500 if (msg == NETDEV_UNREGISTER) {
3501 packet_cached_dev_reset(po);
3503 if (po->prot_hook.dev)
3504 dev_put(po->prot_hook.dev);
3505 po->prot_hook.dev = NULL;
3507 spin_unlock(&po->bind_lock);
3511 if (dev->ifindex == po->ifindex) {
3512 spin_lock(&po->bind_lock);
3514 register_prot_hook(sk);
3515 spin_unlock(&po->bind_lock);
3525 static int packet_ioctl(struct socket *sock, unsigned int cmd,
3528 struct sock *sk = sock->sk;
3533 int amount = sk_wmem_alloc_get(sk);
3535 return put_user(amount, (int __user *)arg);
3539 struct sk_buff *skb;
3542 spin_lock_bh(&sk->sk_receive_queue.lock);
3543 skb = skb_peek(&sk->sk_receive_queue);
3546 spin_unlock_bh(&sk->sk_receive_queue.lock);
3547 return put_user(amount, (int __user *)arg);
3550 return sock_get_timestamp(sk, (struct timeval __user *)arg);
3552 return sock_get_timestampns(sk, (struct timespec __user *)arg);
3562 case SIOCGIFBRDADDR:
3563 case SIOCSIFBRDADDR:
3564 case SIOCGIFNETMASK:
3565 case SIOCSIFNETMASK:
3566 case SIOCGIFDSTADDR:
3567 case SIOCSIFDSTADDR:
3569 return inet_dgram_ops.ioctl(sock, cmd, arg);
3573 return -ENOIOCTLCMD;
3578 static unsigned int packet_poll(struct file *file, struct socket *sock,
3581 struct sock *sk = sock->sk;
3582 struct packet_sock *po = pkt_sk(sk);
3583 unsigned int mask = datagram_poll(file, sock, wait);
3585 spin_lock_bh(&sk->sk_receive_queue.lock);
3586 if (po->rx_ring.pg_vec) {
3587 if (!packet_previous_rx_frame(po, &po->rx_ring,
3589 mask |= POLLIN | POLLRDNORM;
3591 spin_unlock_bh(&sk->sk_receive_queue.lock);
3592 spin_lock_bh(&sk->sk_write_queue.lock);
3593 if (po->tx_ring.pg_vec) {
3594 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
3595 mask |= POLLOUT | POLLWRNORM;
3597 spin_unlock_bh(&sk->sk_write_queue.lock);
3602 /* Dirty? Well, I still did not learn better way to account
3606 static void packet_mm_open(struct vm_area_struct *vma)
3608 struct file *file = vma->vm_file;
3609 struct socket *sock = file->private_data;
3610 struct sock *sk = sock->sk;
3613 atomic_inc(&pkt_sk(sk)->mapped);
3616 static void packet_mm_close(struct vm_area_struct *vma)
3618 struct file *file = vma->vm_file;
3619 struct socket *sock = file->private_data;
3620 struct sock *sk = sock->sk;
3623 atomic_dec(&pkt_sk(sk)->mapped);
3626 static const struct vm_operations_struct packet_mmap_ops = {
3627 .open = packet_mm_open,
3628 .close = packet_mm_close,
3631 static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
3636 for (i = 0; i < len; i++) {
3637 if (likely(pg_vec[i].buffer)) {
3638 if (is_vmalloc_addr(pg_vec[i].buffer))
3639 vfree(pg_vec[i].buffer);
3641 free_pages((unsigned long)pg_vec[i].buffer,
3643 pg_vec[i].buffer = NULL;
3649 static char *alloc_one_pg_vec_page(unsigned long order)
3651 char *buffer = NULL;
3652 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
3653 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
3655 buffer = (char *) __get_free_pages(gfp_flags, order);
3661 * __get_free_pages failed, fall back to vmalloc
3663 buffer = vzalloc((1 << order) * PAGE_SIZE);
3669 * vmalloc failed, lets dig into swap here
3671 gfp_flags &= ~__GFP_NORETRY;
3672 buffer = (char *)__get_free_pages(gfp_flags, order);
3677 * complete and utter failure
3682 static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
3684 unsigned int block_nr = req->tp_block_nr;
3688 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
3689 if (unlikely(!pg_vec))
3692 for (i = 0; i < block_nr; i++) {
3693 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
3694 if (unlikely(!pg_vec[i].buffer))
3695 goto out_free_pgvec;
3702 free_pg_vec(pg_vec, order, block_nr);
3707 static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
3708 int closing, int tx_ring)
3710 struct pgv *pg_vec = NULL;
3711 struct packet_sock *po = pkt_sk(sk);
3712 int was_running, order = 0;
3713 struct packet_ring_buffer *rb;
3714 struct sk_buff_head *rb_queue;
3717 /* Added to avoid minimal code churn */
3718 struct tpacket_req *req = &req_u->req;
3720 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
3721 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
3722 WARN(1, "Tx-ring is not supported.\n");
3726 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
3727 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
3731 if (atomic_read(&po->mapped))
3733 if (packet_read_pending(rb))
3737 if (req->tp_block_nr) {
3738 /* Sanity tests and some calculations */
3740 if (unlikely(rb->pg_vec))
3743 switch (po->tp_version) {
3745 po->tp_hdrlen = TPACKET_HDRLEN;
3748 po->tp_hdrlen = TPACKET2_HDRLEN;
3751 po->tp_hdrlen = TPACKET3_HDRLEN;
3756 if (unlikely((int)req->tp_block_size <= 0))
3758 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
3760 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
3763 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
3766 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
3767 if (unlikely(rb->frames_per_block <= 0))
3769 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
3774 order = get_order(req->tp_block_size);
3775 pg_vec = alloc_pg_vec(req, order);
3776 if (unlikely(!pg_vec))
3778 switch (po->tp_version) {
3780 /* Transmit path is not supported. We checked
3781 * it above but just being paranoid
3784 init_prb_bdqc(po, rb, pg_vec, req_u, tx_ring);
3793 if (unlikely(req->tp_frame_nr))
3799 /* Detach socket from network */
3800 spin_lock(&po->bind_lock);
3801 was_running = po->running;
3805 __unregister_prot_hook(sk, false);
3807 spin_unlock(&po->bind_lock);
3812 mutex_lock(&po->pg_vec_lock);
3813 if (closing || atomic_read(&po->mapped) == 0) {
3815 spin_lock_bh(&rb_queue->lock);
3816 swap(rb->pg_vec, pg_vec);
3817 rb->frame_max = (req->tp_frame_nr - 1);
3819 rb->frame_size = req->tp_frame_size;
3820 spin_unlock_bh(&rb_queue->lock);
3822 swap(rb->pg_vec_order, order);
3823 swap(rb->pg_vec_len, req->tp_block_nr);
3825 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
3826 po->prot_hook.func = (po->rx_ring.pg_vec) ?
3827 tpacket_rcv : packet_rcv;
3828 skb_queue_purge(rb_queue);
3829 if (atomic_read(&po->mapped))
3830 pr_err("packet_mmap: vma is busy: %d\n",
3831 atomic_read(&po->mapped));
3833 mutex_unlock(&po->pg_vec_lock);
3835 spin_lock(&po->bind_lock);
3838 register_prot_hook(sk);
3840 spin_unlock(&po->bind_lock);
3841 if (closing && (po->tp_version > TPACKET_V2)) {
3842 /* Because we don't support block-based V3 on tx-ring */
3844 prb_shutdown_retire_blk_timer(po, tx_ring, rb_queue);
3849 free_pg_vec(pg_vec, order, req->tp_block_nr);
3854 static int packet_mmap(struct file *file, struct socket *sock,
3855 struct vm_area_struct *vma)
3857 struct sock *sk = sock->sk;
3858 struct packet_sock *po = pkt_sk(sk);
3859 unsigned long size, expected_size;
3860 struct packet_ring_buffer *rb;
3861 unsigned long start;
3868 mutex_lock(&po->pg_vec_lock);
3871 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3873 expected_size += rb->pg_vec_len
3879 if (expected_size == 0)
3882 size = vma->vm_end - vma->vm_start;
3883 if (size != expected_size)
3886 start = vma->vm_start;
3887 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3888 if (rb->pg_vec == NULL)
3891 for (i = 0; i < rb->pg_vec_len; i++) {
3893 void *kaddr = rb->pg_vec[i].buffer;
3896 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
3897 page = pgv_to_page(kaddr);
3898 err = vm_insert_page(vma, start, page);
3907 atomic_inc(&po->mapped);
3908 vma->vm_ops = &packet_mmap_ops;
3912 mutex_unlock(&po->pg_vec_lock);
3916 static const struct proto_ops packet_ops_spkt = {
3917 .family = PF_PACKET,
3918 .owner = THIS_MODULE,
3919 .release = packet_release,
3920 .bind = packet_bind_spkt,
3921 .connect = sock_no_connect,
3922 .socketpair = sock_no_socketpair,
3923 .accept = sock_no_accept,
3924 .getname = packet_getname_spkt,
3925 .poll = datagram_poll,
3926 .ioctl = packet_ioctl,
3927 .listen = sock_no_listen,
3928 .shutdown = sock_no_shutdown,
3929 .setsockopt = sock_no_setsockopt,
3930 .getsockopt = sock_no_getsockopt,
3931 .sendmsg = packet_sendmsg_spkt,
3932 .recvmsg = packet_recvmsg,
3933 .mmap = sock_no_mmap,
3934 .sendpage = sock_no_sendpage,
3937 static const struct proto_ops packet_ops = {
3938 .family = PF_PACKET,
3939 .owner = THIS_MODULE,
3940 .release = packet_release,
3941 .bind = packet_bind,
3942 .connect = sock_no_connect,
3943 .socketpair = sock_no_socketpair,
3944 .accept = sock_no_accept,
3945 .getname = packet_getname,
3946 .poll = packet_poll,
3947 .ioctl = packet_ioctl,
3948 .listen = sock_no_listen,
3949 .shutdown = sock_no_shutdown,
3950 .setsockopt = packet_setsockopt,
3951 .getsockopt = packet_getsockopt,
3952 .sendmsg = packet_sendmsg,
3953 .recvmsg = packet_recvmsg,
3954 .mmap = packet_mmap,
3955 .sendpage = sock_no_sendpage,
3958 static const struct net_proto_family packet_family_ops = {
3959 .family = PF_PACKET,
3960 .create = packet_create,
3961 .owner = THIS_MODULE,
3964 static struct notifier_block packet_netdev_notifier = {
3965 .notifier_call = packet_notifier,
3968 #ifdef CONFIG_PROC_FS
3970 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
3973 struct net *net = seq_file_net(seq);
3976 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
3979 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3981 struct net *net = seq_file_net(seq);
3982 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
3985 static void packet_seq_stop(struct seq_file *seq, void *v)
3991 static int packet_seq_show(struct seq_file *seq, void *v)
3993 if (v == SEQ_START_TOKEN)
3994 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3996 struct sock *s = sk_entry(v);
3997 const struct packet_sock *po = pkt_sk(s);
4000 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
4002 atomic_read(&s->sk_refcnt),
4007 atomic_read(&s->sk_rmem_alloc),
4008 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
4015 static const struct seq_operations packet_seq_ops = {
4016 .start = packet_seq_start,
4017 .next = packet_seq_next,
4018 .stop = packet_seq_stop,
4019 .show = packet_seq_show,
4022 static int packet_seq_open(struct inode *inode, struct file *file)
4024 return seq_open_net(inode, file, &packet_seq_ops,
4025 sizeof(struct seq_net_private));
4028 static const struct file_operations packet_seq_fops = {
4029 .owner = THIS_MODULE,
4030 .open = packet_seq_open,
4032 .llseek = seq_lseek,
4033 .release = seq_release_net,
4038 static int __net_init packet_net_init(struct net *net)
4040 mutex_init(&net->packet.sklist_lock);
4041 INIT_HLIST_HEAD(&net->packet.sklist);
4043 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
4049 static void __net_exit packet_net_exit(struct net *net)
4051 remove_proc_entry("packet", net->proc_net);
4054 static struct pernet_operations packet_net_ops = {
4055 .init = packet_net_init,
4056 .exit = packet_net_exit,
4060 static void __exit packet_exit(void)
4062 unregister_netdevice_notifier(&packet_netdev_notifier);
4063 unregister_pernet_subsys(&packet_net_ops);
4064 sock_unregister(PF_PACKET);
4065 proto_unregister(&packet_proto);
4068 static int __init packet_init(void)
4070 int rc = proto_register(&packet_proto, 0);
4075 sock_register(&packet_family_ops);
4076 register_pernet_subsys(&packet_net_ops);
4077 register_netdevice_notifier(&packet_netdev_notifier);
4082 module_init(packet_init);
4083 module_exit(packet_exit);
4084 MODULE_LICENSE("GPL");
4085 MODULE_ALIAS_NETPROTO(PF_PACKET);