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/system.h>
77 #include <asm/uaccess.h>
78 #include <asm/ioctls.h>
80 #include <asm/cacheflush.h>
82 #include <linux/proc_fs.h>
83 #include <linux/seq_file.h>
84 #include <linux/poll.h>
85 #include <linux/module.h>
86 #include <linux/init.h>
87 #include <linux/mutex.h>
88 #include <linux/if_vlan.h>
89 #include <linux/virtio_net.h>
90 #include <linux/errqueue.h>
91 #include <linux/net_tstamp.h>
94 #include <net/inet_common.h>
99 - if device has no dev->hard_header routine, it adds and removes ll header
100 inside itself. In this case ll header is invisible outside of device,
101 but higher levels still should reserve dev->hard_header_len.
102 Some devices are enough clever to reallocate skb, when header
103 will not fit to reserved space (tunnel), another ones are silly
105 - packet socket receives packets with pulled ll header,
106 so that SOCK_RAW should push it back.
111 Incoming, dev->hard_header!=NULL
112 mac_header -> ll header
115 Outgoing, dev->hard_header!=NULL
116 mac_header -> ll header
119 Incoming, dev->hard_header==NULL
120 mac_header -> UNKNOWN position. It is very likely, that it points to ll
121 header. PPP makes it, that is wrong, because introduce
122 assymetry between rx and tx paths.
125 Outgoing, dev->hard_header==NULL
126 mac_header -> data. ll header is still not built!
130 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
136 dev->hard_header != NULL
137 mac_header -> ll header
140 dev->hard_header == NULL (ll header is added by device, we cannot control it)
144 We should set nh.raw on output to correct posistion,
145 packet classifier depends on it.
148 /* Private packet socket structures. */
150 struct packet_mclist
{
151 struct packet_mclist
*next
;
156 unsigned char addr
[MAX_ADDR_LEN
];
158 /* identical to struct packet_mreq except it has
159 * a longer address field.
161 struct packet_mreq_max
{
163 unsigned short mr_type
;
164 unsigned short mr_alen
;
165 unsigned char mr_address
[MAX_ADDR_LEN
];
168 static int packet_set_ring(struct sock
*sk
, union tpacket_req_u
*req_u
,
169 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 /* kbdq - kernel block descriptor queue */
180 struct tpacket_kbdq_core
{
182 unsigned int feature_req_word
;
184 unsigned char reset_pending_on_curr_blk
;
185 unsigned char delete_blk_timer
;
186 unsigned short kactive_blk_num
;
187 unsigned short blk_sizeof_priv
;
189 /* last_kactive_blk_num:
190 * trick to see if user-space has caught up
191 * in order to avoid refreshing timer when every single pkt arrives.
193 unsigned short last_kactive_blk_num
;
198 unsigned int knum_blocks
;
199 uint64_t knxt_seq_num
;
204 atomic_t blk_fill_in_prog
;
206 /* Default is set to 8ms */
207 #define DEFAULT_PRB_RETIRE_TOV (8)
209 unsigned short retire_blk_tov
;
210 unsigned short version
;
211 unsigned long tov_in_jiffies
;
213 /* timer to retire an outstanding block */
214 struct timer_list retire_blk_timer
;
217 #define PGV_FROM_VMALLOC 1
222 struct packet_ring_buffer
{
225 unsigned int frames_per_block
;
226 unsigned int frame_size
;
227 unsigned int frame_max
;
229 unsigned int pg_vec_order
;
230 unsigned int pg_vec_pages
;
231 unsigned int pg_vec_len
;
233 struct tpacket_kbdq_core prb_bdqc
;
237 #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
238 #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
239 #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
240 #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
241 #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
242 #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
243 #define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
246 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
);
248 static void *packet_previous_frame(struct packet_sock
*po
,
249 struct packet_ring_buffer
*rb
,
251 static void packet_increment_head(struct packet_ring_buffer
*buff
);
252 static int prb_curr_blk_in_use(struct tpacket_kbdq_core
*,
253 struct tpacket_block_desc
*);
254 static void *prb_dispatch_next_block(struct tpacket_kbdq_core
*,
255 struct packet_sock
*);
256 static void prb_retire_current_block(struct tpacket_kbdq_core
*,
257 struct packet_sock
*, unsigned int status
);
258 static int prb_queue_frozen(struct tpacket_kbdq_core
*);
259 static void prb_open_block(struct tpacket_kbdq_core
*,
260 struct tpacket_block_desc
*);
261 static void prb_retire_rx_blk_timer_expired(unsigned long);
262 static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core
*);
263 static void prb_init_blk_timer(struct packet_sock
*,
264 struct tpacket_kbdq_core
*,
265 void (*func
) (unsigned long));
266 static void prb_fill_rxhash(struct tpacket_kbdq_core
*, struct tpacket3_hdr
*);
267 static void prb_clear_rxhash(struct tpacket_kbdq_core
*,
268 struct tpacket3_hdr
*);
269 static void prb_fill_vlan_info(struct tpacket_kbdq_core
*,
270 struct tpacket3_hdr
*);
271 static void packet_flush_mclist(struct sock
*sk
);
273 struct packet_fanout
;
275 /* struct sock has to be the first member of packet_sock */
277 struct packet_fanout
*fanout
;
278 struct tpacket_stats stats
;
279 union tpacket_stats_u stats_u
;
280 struct packet_ring_buffer rx_ring
;
281 struct packet_ring_buffer tx_ring
;
283 spinlock_t bind_lock
;
284 struct mutex pg_vec_lock
;
285 unsigned int running
:1, /* prot_hook is attached*/
289 int ifindex
; /* bound device */
291 struct packet_mclist
*mclist
;
293 enum tpacket_versions tp_version
;
294 unsigned int tp_hdrlen
;
295 unsigned int tp_reserve
;
296 unsigned int tp_loss
:1;
297 unsigned int tp_tstamp
;
298 struct packet_type prot_hook ____cacheline_aligned_in_smp
;
301 #define PACKET_FANOUT_MAX 256
303 struct packet_fanout
{
307 unsigned int num_members
;
312 struct list_head list
;
313 struct sock
*arr
[PACKET_FANOUT_MAX
];
316 struct packet_type prot_hook ____cacheline_aligned_in_smp
;
319 struct packet_skb_cb
{
320 unsigned int origlen
;
322 struct sockaddr_pkt pkt
;
323 struct sockaddr_ll ll
;
327 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
329 #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
330 #define GET_PBLOCK_DESC(x, bid) \
331 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
332 #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
333 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
334 #define GET_NEXT_PRB_BLK_NUM(x) \
335 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
336 ((x)->kactive_blk_num+1) : 0)
338 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
340 return (struct packet_sock
*)sk
;
343 static void __fanout_unlink(struct sock
*sk
, struct packet_sock
*po
);
344 static void __fanout_link(struct sock
*sk
, struct packet_sock
*po
);
346 /* register_prot_hook must be invoked with the po->bind_lock held,
347 * or from a context in which asynchronous accesses to the packet
348 * socket is not possible (packet_create()).
350 static void register_prot_hook(struct sock
*sk
)
352 struct packet_sock
*po
= pkt_sk(sk
);
355 __fanout_link(sk
, po
);
357 dev_add_pack(&po
->prot_hook
);
363 /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
364 * held. If the sync parameter is true, we will temporarily drop
365 * the po->bind_lock and do a synchronize_net to make sure no
366 * asynchronous packet processing paths still refer to the elements
367 * of po->prot_hook. If the sync parameter is false, it is the
368 * callers responsibility to take care of this.
370 static void __unregister_prot_hook(struct sock
*sk
, bool sync
)
372 struct packet_sock
*po
= pkt_sk(sk
);
376 __fanout_unlink(sk
, po
);
378 __dev_remove_pack(&po
->prot_hook
);
382 spin_unlock(&po
->bind_lock
);
384 spin_lock(&po
->bind_lock
);
388 static void unregister_prot_hook(struct sock
*sk
, bool sync
)
390 struct packet_sock
*po
= pkt_sk(sk
);
393 __unregister_prot_hook(sk
, sync
);
396 static inline __pure
struct page
*pgv_to_page(void *addr
)
398 if (is_vmalloc_addr(addr
))
399 return vmalloc_to_page(addr
);
400 return virt_to_page(addr
);
403 static void __packet_set_status(struct packet_sock
*po
, void *frame
, int status
)
406 struct tpacket_hdr
*h1
;
407 struct tpacket2_hdr
*h2
;
412 switch (po
->tp_version
) {
414 h
.h1
->tp_status
= status
;
415 flush_dcache_page(pgv_to_page(&h
.h1
->tp_status
));
418 h
.h2
->tp_status
= status
;
419 flush_dcache_page(pgv_to_page(&h
.h2
->tp_status
));
423 WARN(1, "TPACKET version not supported.\n");
430 static int __packet_get_status(struct packet_sock
*po
, void *frame
)
433 struct tpacket_hdr
*h1
;
434 struct tpacket2_hdr
*h2
;
441 switch (po
->tp_version
) {
443 flush_dcache_page(pgv_to_page(&h
.h1
->tp_status
));
444 return h
.h1
->tp_status
;
446 flush_dcache_page(pgv_to_page(&h
.h2
->tp_status
));
447 return h
.h2
->tp_status
;
450 WARN(1, "TPACKET version not supported.\n");
456 static void *packet_lookup_frame(struct packet_sock
*po
,
457 struct packet_ring_buffer
*rb
,
458 unsigned int position
,
461 unsigned int pg_vec_pos
, frame_offset
;
463 struct tpacket_hdr
*h1
;
464 struct tpacket2_hdr
*h2
;
468 pg_vec_pos
= position
/ rb
->frames_per_block
;
469 frame_offset
= position
% rb
->frames_per_block
;
471 h
.raw
= rb
->pg_vec
[pg_vec_pos
].buffer
+
472 (frame_offset
* rb
->frame_size
);
474 if (status
!= __packet_get_status(po
, h
.raw
))
480 static inline void *packet_current_frame(struct packet_sock
*po
,
481 struct packet_ring_buffer
*rb
,
484 return packet_lookup_frame(po
, rb
, rb
->head
, status
);
487 static void prb_del_retire_blk_timer(struct tpacket_kbdq_core
*pkc
)
489 del_timer_sync(&pkc
->retire_blk_timer
);
492 static void prb_shutdown_retire_blk_timer(struct packet_sock
*po
,
494 struct sk_buff_head
*rb_queue
)
496 struct tpacket_kbdq_core
*pkc
;
498 pkc
= tx_ring
? &po
->tx_ring
.prb_bdqc
: &po
->rx_ring
.prb_bdqc
;
500 spin_lock(&rb_queue
->lock
);
501 pkc
->delete_blk_timer
= 1;
502 spin_unlock(&rb_queue
->lock
);
504 prb_del_retire_blk_timer(pkc
);
507 static void prb_init_blk_timer(struct packet_sock
*po
,
508 struct tpacket_kbdq_core
*pkc
,
509 void (*func
) (unsigned long))
511 init_timer(&pkc
->retire_blk_timer
);
512 pkc
->retire_blk_timer
.data
= (long)po
;
513 pkc
->retire_blk_timer
.function
= func
;
514 pkc
->retire_blk_timer
.expires
= jiffies
;
517 static void prb_setup_retire_blk_timer(struct packet_sock
*po
, int tx_ring
)
519 struct tpacket_kbdq_core
*pkc
;
524 pkc
= tx_ring
? &po
->tx_ring
.prb_bdqc
: &po
->rx_ring
.prb_bdqc
;
525 prb_init_blk_timer(po
, pkc
, prb_retire_rx_blk_timer_expired
);
528 static int prb_calc_retire_blk_tmo(struct packet_sock
*po
,
529 int blk_size_in_bytes
)
531 struct net_device
*dev
;
532 unsigned int mbits
= 0, msec
= 0, div
= 0, tmo
= 0;
534 dev
= dev_get_by_index(sock_net(&po
->sk
), po
->ifindex
);
535 if (unlikely(dev
== NULL
))
536 return DEFAULT_PRB_RETIRE_TOV
;
538 if (dev
->ethtool_ops
&& dev
->ethtool_ops
->get_settings
) {
539 struct ethtool_cmd ecmd
= { .cmd
= ETHTOOL_GSET
, };
541 if (!dev
->ethtool_ops
->get_settings(dev
, &ecmd
)) {
542 switch (ecmd
.speed
) {
552 * If the link speed is so slow you don't really
553 * need to worry about perf anyways
558 return DEFAULT_PRB_RETIRE_TOV
;
563 mbits
= (blk_size_in_bytes
* 8) / (1024 * 1024);
575 static void prb_init_ft_ops(struct tpacket_kbdq_core
*p1
,
576 union tpacket_req_u
*req_u
)
578 p1
->feature_req_word
= req_u
->req3
.tp_feature_req_word
;
581 static void init_prb_bdqc(struct packet_sock
*po
,
582 struct packet_ring_buffer
*rb
,
584 union tpacket_req_u
*req_u
, int tx_ring
)
586 struct tpacket_kbdq_core
*p1
= &rb
->prb_bdqc
;
587 struct tpacket_block_desc
*pbd
;
589 memset(p1
, 0x0, sizeof(*p1
));
591 p1
->knxt_seq_num
= 1;
593 pbd
= (struct tpacket_block_desc
*)pg_vec
[0].buffer
;
594 p1
->pkblk_start
= (char *)pg_vec
[0].buffer
;
595 p1
->kblk_size
= req_u
->req3
.tp_block_size
;
596 p1
->knum_blocks
= req_u
->req3
.tp_block_nr
;
597 p1
->hdrlen
= po
->tp_hdrlen
;
598 p1
->version
= po
->tp_version
;
599 p1
->last_kactive_blk_num
= 0;
600 po
->stats_u
.stats3
.tp_freeze_q_cnt
= 0;
601 if (req_u
->req3
.tp_retire_blk_tov
)
602 p1
->retire_blk_tov
= req_u
->req3
.tp_retire_blk_tov
;
604 p1
->retire_blk_tov
= prb_calc_retire_blk_tmo(po
,
605 req_u
->req3
.tp_block_size
);
606 p1
->tov_in_jiffies
= msecs_to_jiffies(p1
->retire_blk_tov
);
607 p1
->blk_sizeof_priv
= req_u
->req3
.tp_sizeof_priv
;
609 prb_init_ft_ops(p1
, req_u
);
610 prb_setup_retire_blk_timer(po
, tx_ring
);
611 prb_open_block(p1
, pbd
);
614 /* Do NOT update the last_blk_num first.
615 * Assumes sk_buff_head lock is held.
617 static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core
*pkc
)
619 mod_timer(&pkc
->retire_blk_timer
,
620 jiffies
+ pkc
->tov_in_jiffies
);
621 pkc
->last_kactive_blk_num
= pkc
->kactive_blk_num
;
626 * 1) We refresh the timer only when we open a block.
627 * By doing this we don't waste cycles refreshing the timer
628 * on packet-by-packet basis.
630 * With a 1MB block-size, on a 1Gbps line, it will take
631 * i) ~8 ms to fill a block + ii) memcpy etc.
632 * In this cut we are not accounting for the memcpy time.
634 * So, if the user sets the 'tmo' to 10ms then the timer
635 * will never fire while the block is still getting filled
636 * (which is what we want). However, the user could choose
637 * to close a block early and that's fine.
639 * But when the timer does fire, we check whether or not to refresh it.
640 * Since the tmo granularity is in msecs, it is not too expensive
641 * to refresh the timer, lets say every '8' msecs.
642 * Either the user can set the 'tmo' or we can derive it based on
643 * a) line-speed and b) block-size.
644 * prb_calc_retire_blk_tmo() calculates the tmo.
647 static void prb_retire_rx_blk_timer_expired(unsigned long data
)
649 struct packet_sock
*po
= (struct packet_sock
*)data
;
650 struct tpacket_kbdq_core
*pkc
= &po
->rx_ring
.prb_bdqc
;
652 struct tpacket_block_desc
*pbd
;
654 spin_lock(&po
->sk
.sk_receive_queue
.lock
);
656 frozen
= prb_queue_frozen(pkc
);
657 pbd
= GET_CURR_PBLOCK_DESC_FROM_CORE(pkc
);
659 if (unlikely(pkc
->delete_blk_timer
))
662 /* We only need to plug the race when the block is partially filled.
664 * lock(); increment BLOCK_NUM_PKTS; unlock()
665 * copy_bits() is in progress ...
666 * timer fires on other cpu:
667 * we can't retire the current block because copy_bits
671 if (BLOCK_NUM_PKTS(pbd
)) {
672 while (atomic_read(&pkc
->blk_fill_in_prog
)) {
673 /* Waiting for skb_copy_bits to finish... */
678 if (pkc
->last_kactive_blk_num
== pkc
->kactive_blk_num
) {
680 prb_retire_current_block(pkc
, po
, TP_STATUS_BLK_TMO
);
681 if (!prb_dispatch_next_block(pkc
, po
))
686 /* Case 1. Queue was frozen because user-space was
689 if (prb_curr_blk_in_use(pkc
, pbd
)) {
691 * Ok, user-space is still behind.
692 * So just refresh the timer.
696 /* Case 2. queue was frozen,user-space caught up,
697 * now the link went idle && the timer fired.
698 * We don't have a block to close.So we open this
699 * block and restart the timer.
700 * opening a block thaws the queue,restarts timer
701 * Thawing/timer-refresh is a side effect.
703 prb_open_block(pkc
, pbd
);
710 _prb_refresh_rx_retire_blk_timer(pkc
);
713 spin_unlock(&po
->sk
.sk_receive_queue
.lock
);
716 static inline void prb_flush_block(struct tpacket_kbdq_core
*pkc1
,
717 struct tpacket_block_desc
*pbd1
, __u32 status
)
719 /* Flush everything minus the block header */
721 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
726 /* Skip the block header(we know header WILL fit in 4K) */
729 end
= (u8
*)PAGE_ALIGN((unsigned long)pkc1
->pkblk_end
);
730 for (; start
< end
; start
+= PAGE_SIZE
)
731 flush_dcache_page(pgv_to_page(start
));
736 /* Now update the block status. */
738 BLOCK_STATUS(pbd1
) = status
;
740 /* Flush the block header */
742 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
744 flush_dcache_page(pgv_to_page(start
));
754 * 2) Increment active_blk_num
756 * Note:We DONT refresh the timer on purpose.
757 * Because almost always the next block will be opened.
759 static void prb_close_block(struct tpacket_kbdq_core
*pkc1
,
760 struct tpacket_block_desc
*pbd1
,
761 struct packet_sock
*po
, unsigned int stat
)
763 __u32 status
= TP_STATUS_USER
| stat
;
765 struct tpacket3_hdr
*last_pkt
;
766 struct tpacket_hdr_v1
*h1
= &pbd1
->hdr
.bh1
;
768 if (po
->stats
.tp_drops
)
769 status
|= TP_STATUS_LOSING
;
771 last_pkt
= (struct tpacket3_hdr
*)pkc1
->prev
;
772 last_pkt
->tp_next_offset
= 0;
774 /* Get the ts of the last pkt */
775 if (BLOCK_NUM_PKTS(pbd1
)) {
776 h1
->ts_last_pkt
.ts_sec
= last_pkt
->tp_sec
;
777 h1
->ts_last_pkt
.ts_nsec
= last_pkt
->tp_nsec
;
779 /* Ok, we tmo'd - so get the current time */
782 h1
->ts_last_pkt
.ts_sec
= ts
.tv_sec
;
783 h1
->ts_last_pkt
.ts_nsec
= ts
.tv_nsec
;
788 /* Flush the block */
789 prb_flush_block(pkc1
, pbd1
, status
);
791 pkc1
->kactive_blk_num
= GET_NEXT_PRB_BLK_NUM(pkc1
);
794 static inline void prb_thaw_queue(struct tpacket_kbdq_core
*pkc
)
796 pkc
->reset_pending_on_curr_blk
= 0;
800 * Side effect of opening a block:
802 * 1) prb_queue is thawed.
803 * 2) retire_blk_timer is refreshed.
806 static void prb_open_block(struct tpacket_kbdq_core
*pkc1
,
807 struct tpacket_block_desc
*pbd1
)
810 struct tpacket_hdr_v1
*h1
= &pbd1
->hdr
.bh1
;
814 if (likely(TP_STATUS_KERNEL
== BLOCK_STATUS(pbd1
))) {
816 /* We could have just memset this but we will lose the
817 * flexibility of making the priv area sticky
819 BLOCK_SNUM(pbd1
) = pkc1
->knxt_seq_num
++;
820 BLOCK_NUM_PKTS(pbd1
) = 0;
821 BLOCK_LEN(pbd1
) = BLK_PLUS_PRIV(pkc1
->blk_sizeof_priv
);
823 h1
->ts_first_pkt
.ts_sec
= ts
.tv_sec
;
824 h1
->ts_first_pkt
.ts_nsec
= ts
.tv_nsec
;
825 pkc1
->pkblk_start
= (char *)pbd1
;
826 pkc1
->nxt_offset
= (char *)(pkc1
->pkblk_start
+
827 BLK_PLUS_PRIV(pkc1
->blk_sizeof_priv
));
828 BLOCK_O2FP(pbd1
) = (__u32
)BLK_PLUS_PRIV(pkc1
->blk_sizeof_priv
);
829 BLOCK_O2PRIV(pbd1
) = BLK_HDR_LEN
;
830 pbd1
->version
= pkc1
->version
;
831 pkc1
->prev
= pkc1
->nxt_offset
;
832 pkc1
->pkblk_end
= pkc1
->pkblk_start
+ pkc1
->kblk_size
;
833 prb_thaw_queue(pkc1
);
834 _prb_refresh_rx_retire_blk_timer(pkc1
);
841 WARN(1, "ERROR block:%p is NOT FREE status:%d kactive_blk_num:%d\n",
842 pbd1
, BLOCK_STATUS(pbd1
), pkc1
->kactive_blk_num
);
848 * Queue freeze logic:
849 * 1) Assume tp_block_nr = 8 blocks.
850 * 2) At time 't0', user opens Rx ring.
851 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
852 * 4) user-space is either sleeping or processing block '0'.
853 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
854 * it will close block-7,loop around and try to fill block '0'.
856 * __packet_lookup_frame_in_block
857 * prb_retire_current_block()
858 * prb_dispatch_next_block()
859 * |->(BLOCK_STATUS == USER) evaluates to true
860 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
861 * 6) Now there are two cases:
862 * 6.1) Link goes idle right after the queue is frozen.
863 * But remember, the last open_block() refreshed the timer.
864 * When this timer expires,it will refresh itself so that we can
865 * re-open block-0 in near future.
866 * 6.2) Link is busy and keeps on receiving packets. This is a simple
867 * case and __packet_lookup_frame_in_block will check if block-0
868 * is free and can now be re-used.
870 static inline void prb_freeze_queue(struct tpacket_kbdq_core
*pkc
,
871 struct packet_sock
*po
)
873 pkc
->reset_pending_on_curr_blk
= 1;
874 po
->stats_u
.stats3
.tp_freeze_q_cnt
++;
877 #define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
880 * If the next block is free then we will dispatch it
881 * and return a good offset.
882 * Else, we will freeze the queue.
883 * So, caller must check the return value.
885 static void *prb_dispatch_next_block(struct tpacket_kbdq_core
*pkc
,
886 struct packet_sock
*po
)
888 struct tpacket_block_desc
*pbd
;
892 /* 1. Get current block num */
893 pbd
= GET_CURR_PBLOCK_DESC_FROM_CORE(pkc
);
895 /* 2. If this block is currently in_use then freeze the queue */
896 if (TP_STATUS_USER
& BLOCK_STATUS(pbd
)) {
897 prb_freeze_queue(pkc
, po
);
903 * open this block and return the offset where the first packet
904 * needs to get stored.
906 prb_open_block(pkc
, pbd
);
907 return (void *)pkc
->nxt_offset
;
910 static void prb_retire_current_block(struct tpacket_kbdq_core
*pkc
,
911 struct packet_sock
*po
, unsigned int status
)
913 struct tpacket_block_desc
*pbd
= GET_CURR_PBLOCK_DESC_FROM_CORE(pkc
);
915 /* retire/close the current block */
916 if (likely(TP_STATUS_KERNEL
== BLOCK_STATUS(pbd
))) {
918 * Plug the case where copy_bits() is in progress on
919 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
920 * have space to copy the pkt in the current block and
921 * called prb_retire_current_block()
923 * We don't need to worry about the TMO case because
924 * the timer-handler already handled this case.
926 if (!(status
& TP_STATUS_BLK_TMO
)) {
927 while (atomic_read(&pkc
->blk_fill_in_prog
)) {
928 /* Waiting for skb_copy_bits to finish... */
932 prb_close_block(pkc
, pbd
, po
, status
);
936 WARN(1, "ERROR-pbd[%d]:%p\n", pkc
->kactive_blk_num
, pbd
);
941 static inline int prb_curr_blk_in_use(struct tpacket_kbdq_core
*pkc
,
942 struct tpacket_block_desc
*pbd
)
944 return TP_STATUS_USER
& BLOCK_STATUS(pbd
);
947 static inline int prb_queue_frozen(struct tpacket_kbdq_core
*pkc
)
949 return pkc
->reset_pending_on_curr_blk
;
952 static inline void prb_clear_blk_fill_status(struct packet_ring_buffer
*rb
)
954 struct tpacket_kbdq_core
*pkc
= GET_PBDQC_FROM_RB(rb
);
955 atomic_dec(&pkc
->blk_fill_in_prog
);
958 static inline void prb_fill_rxhash(struct tpacket_kbdq_core
*pkc
,
959 struct tpacket3_hdr
*ppd
)
961 ppd
->hv1
.tp_rxhash
= skb_get_rxhash(pkc
->skb
);
964 static inline void prb_clear_rxhash(struct tpacket_kbdq_core
*pkc
,
965 struct tpacket3_hdr
*ppd
)
967 ppd
->hv1
.tp_rxhash
= 0;
970 static inline void prb_fill_vlan_info(struct tpacket_kbdq_core
*pkc
,
971 struct tpacket3_hdr
*ppd
)
973 if (vlan_tx_tag_present(pkc
->skb
)) {
974 ppd
->hv1
.tp_vlan_tci
= vlan_tx_tag_get(pkc
->skb
);
975 ppd
->tp_status
= TP_STATUS_VLAN_VALID
;
977 ppd
->hv1
.tp_vlan_tci
= ppd
->tp_status
= 0;
981 static void prb_run_all_ft_ops(struct tpacket_kbdq_core
*pkc
,
982 struct tpacket3_hdr
*ppd
)
984 prb_fill_vlan_info(pkc
, ppd
);
986 if (pkc
->feature_req_word
& TP_FT_REQ_FILL_RXHASH
)
987 prb_fill_rxhash(pkc
, ppd
);
989 prb_clear_rxhash(pkc
, ppd
);
992 static inline void prb_fill_curr_block(char *curr
,
993 struct tpacket_kbdq_core
*pkc
,
994 struct tpacket_block_desc
*pbd
,
997 struct tpacket3_hdr
*ppd
;
999 ppd
= (struct tpacket3_hdr
*)curr
;
1000 ppd
->tp_next_offset
= TOTAL_PKT_LEN_INCL_ALIGN(len
);
1002 pkc
->nxt_offset
+= TOTAL_PKT_LEN_INCL_ALIGN(len
);
1003 BLOCK_LEN(pbd
) += TOTAL_PKT_LEN_INCL_ALIGN(len
);
1004 BLOCK_NUM_PKTS(pbd
) += 1;
1005 atomic_inc(&pkc
->blk_fill_in_prog
);
1006 prb_run_all_ft_ops(pkc
, ppd
);
1009 /* Assumes caller has the sk->rx_queue.lock */
1010 static void *__packet_lookup_frame_in_block(struct packet_sock
*po
,
1011 struct sk_buff
*skb
,
1016 struct tpacket_kbdq_core
*pkc
;
1017 struct tpacket_block_desc
*pbd
;
1020 pkc
= GET_PBDQC_FROM_RB(((struct packet_ring_buffer
*)&po
->rx_ring
));
1021 pbd
= GET_CURR_PBLOCK_DESC_FROM_CORE(pkc
);
1023 /* Queue is frozen when user space is lagging behind */
1024 if (prb_queue_frozen(pkc
)) {
1026 * Check if that last block which caused the queue to freeze,
1027 * is still in_use by user-space.
1029 if (prb_curr_blk_in_use(pkc
, pbd
)) {
1030 /* Can't record this packet */
1034 * Ok, the block was released by user-space.
1035 * Now let's open that block.
1036 * opening a block also thaws the queue.
1037 * Thawing is a side effect.
1039 prb_open_block(pkc
, pbd
);
1044 curr
= pkc
->nxt_offset
;
1046 end
= (char *) ((char *)pbd
+ pkc
->kblk_size
);
1048 /* first try the current block */
1049 if (curr
+TOTAL_PKT_LEN_INCL_ALIGN(len
) < end
) {
1050 prb_fill_curr_block(curr
, pkc
, pbd
, len
);
1051 return (void *)curr
;
1054 /* Ok, close the current block */
1055 prb_retire_current_block(pkc
, po
, 0);
1057 /* Now, try to dispatch the next block */
1058 curr
= (char *)prb_dispatch_next_block(pkc
, po
);
1060 pbd
= GET_CURR_PBLOCK_DESC_FROM_CORE(pkc
);
1061 prb_fill_curr_block(curr
, pkc
, pbd
, len
);
1062 return (void *)curr
;
1066 * No free blocks are available.user_space hasn't caught up yet.
1067 * Queue was just frozen and now this packet will get dropped.
1072 static inline void *packet_current_rx_frame(struct packet_sock
*po
,
1073 struct sk_buff
*skb
,
1074 int status
, unsigned int len
)
1077 switch (po
->tp_version
) {
1080 curr
= packet_lookup_frame(po
, &po
->rx_ring
,
1081 po
->rx_ring
.head
, status
);
1084 return __packet_lookup_frame_in_block(po
, skb
, status
, len
);
1086 WARN(1, "TPACKET version not supported\n");
1092 static inline void *prb_lookup_block(struct packet_sock
*po
,
1093 struct packet_ring_buffer
*rb
,
1094 unsigned int previous
,
1097 struct tpacket_kbdq_core
*pkc
= GET_PBDQC_FROM_RB(rb
);
1098 struct tpacket_block_desc
*pbd
= GET_PBLOCK_DESC(pkc
, previous
);
1100 if (status
!= BLOCK_STATUS(pbd
))
1105 static inline int prb_previous_blk_num(struct packet_ring_buffer
*rb
)
1108 if (rb
->prb_bdqc
.kactive_blk_num
)
1109 prev
= rb
->prb_bdqc
.kactive_blk_num
-1;
1111 prev
= rb
->prb_bdqc
.knum_blocks
-1;
1115 /* Assumes caller has held the rx_queue.lock */
1116 static inline void *__prb_previous_block(struct packet_sock
*po
,
1117 struct packet_ring_buffer
*rb
,
1120 unsigned int previous
= prb_previous_blk_num(rb
);
1121 return prb_lookup_block(po
, rb
, previous
, status
);
1124 static inline void *packet_previous_rx_frame(struct packet_sock
*po
,
1125 struct packet_ring_buffer
*rb
,
1128 if (po
->tp_version
<= TPACKET_V2
)
1129 return packet_previous_frame(po
, rb
, status
);
1131 return __prb_previous_block(po
, rb
, status
);
1134 static inline void packet_increment_rx_head(struct packet_sock
*po
,
1135 struct packet_ring_buffer
*rb
)
1137 switch (po
->tp_version
) {
1140 return packet_increment_head(rb
);
1143 WARN(1, "TPACKET version not supported.\n");
1149 static inline void *packet_previous_frame(struct packet_sock
*po
,
1150 struct packet_ring_buffer
*rb
,
1153 unsigned int previous
= rb
->head
? rb
->head
- 1 : rb
->frame_max
;
1154 return packet_lookup_frame(po
, rb
, previous
, status
);
1157 static inline void packet_increment_head(struct packet_ring_buffer
*buff
)
1159 buff
->head
= buff
->head
!= buff
->frame_max
? buff
->head
+1 : 0;
1162 static void packet_sock_destruct(struct sock
*sk
)
1164 skb_queue_purge(&sk
->sk_error_queue
);
1166 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
1167 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
1169 if (!sock_flag(sk
, SOCK_DEAD
)) {
1170 pr_err("Attempt to release alive packet socket: %p\n", sk
);
1174 sk_refcnt_debug_dec(sk
);
1177 static int fanout_rr_next(struct packet_fanout
*f
, unsigned int num
)
1179 int x
= atomic_read(&f
->rr_cur
) + 1;
1187 static struct sock
*fanout_demux_hash(struct packet_fanout
*f
, struct sk_buff
*skb
, unsigned int num
)
1189 u32 idx
, hash
= skb
->rxhash
;
1191 idx
= ((u64
)hash
* num
) >> 32;
1196 static struct sock
*fanout_demux_lb(struct packet_fanout
*f
, struct sk_buff
*skb
, unsigned int num
)
1200 cur
= atomic_read(&f
->rr_cur
);
1201 while ((old
= atomic_cmpxchg(&f
->rr_cur
, cur
,
1202 fanout_rr_next(f
, num
))) != cur
)
1207 static struct sock
*fanout_demux_cpu(struct packet_fanout
*f
, struct sk_buff
*skb
, unsigned int num
)
1209 unsigned int cpu
= smp_processor_id();
1211 return f
->arr
[cpu
% num
];
1214 static struct sk_buff
*fanout_check_defrag(struct sk_buff
*skb
)
1217 const struct iphdr
*iph
;
1220 if (skb
->protocol
!= htons(ETH_P_IP
))
1223 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
1227 if (iph
->ihl
< 5 || iph
->version
!= 4)
1229 if (!pskb_may_pull(skb
, iph
->ihl
*4))
1232 len
= ntohs(iph
->tot_len
);
1233 if (skb
->len
< len
|| len
< (iph
->ihl
* 4))
1236 if (ip_is_fragment(ip_hdr(skb
))) {
1237 skb
= skb_share_check(skb
, GFP_ATOMIC
);
1239 if (pskb_trim_rcsum(skb
, len
))
1241 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
1242 if (ip_defrag(skb
, IP_DEFRAG_AF_PACKET
))
1251 static int packet_rcv_fanout(struct sk_buff
*skb
, struct net_device
*dev
,
1252 struct packet_type
*pt
, struct net_device
*orig_dev
)
1254 struct packet_fanout
*f
= pt
->af_packet_priv
;
1255 unsigned int num
= f
->num_members
;
1256 struct packet_sock
*po
;
1259 if (!net_eq(dev_net(dev
), read_pnet(&f
->net
)) ||
1266 case PACKET_FANOUT_HASH
:
1269 skb
= fanout_check_defrag(skb
);
1273 skb_get_rxhash(skb
);
1274 sk
= fanout_demux_hash(f
, skb
, num
);
1276 case PACKET_FANOUT_LB
:
1277 sk
= fanout_demux_lb(f
, skb
, num
);
1279 case PACKET_FANOUT_CPU
:
1280 sk
= fanout_demux_cpu(f
, skb
, num
);
1286 return po
->prot_hook
.func(skb
, dev
, &po
->prot_hook
, orig_dev
);
1289 static DEFINE_MUTEX(fanout_mutex
);
1290 static LIST_HEAD(fanout_list
);
1292 static void __fanout_link(struct sock
*sk
, struct packet_sock
*po
)
1294 struct packet_fanout
*f
= po
->fanout
;
1296 spin_lock(&f
->lock
);
1297 f
->arr
[f
->num_members
] = sk
;
1300 spin_unlock(&f
->lock
);
1303 static void __fanout_unlink(struct sock
*sk
, struct packet_sock
*po
)
1305 struct packet_fanout
*f
= po
->fanout
;
1308 spin_lock(&f
->lock
);
1309 for (i
= 0; i
< f
->num_members
; i
++) {
1310 if (f
->arr
[i
] == sk
)
1313 BUG_ON(i
>= f
->num_members
);
1314 f
->arr
[i
] = f
->arr
[f
->num_members
- 1];
1316 spin_unlock(&f
->lock
);
1319 static int fanout_add(struct sock
*sk
, u16 id
, u16 type_flags
)
1321 struct packet_sock
*po
= pkt_sk(sk
);
1322 struct packet_fanout
*f
, *match
;
1323 u8 type
= type_flags
& 0xff;
1324 u8 defrag
= (type_flags
& PACKET_FANOUT_FLAG_DEFRAG
) ? 1 : 0;
1328 case PACKET_FANOUT_HASH
:
1329 case PACKET_FANOUT_LB
:
1330 case PACKET_FANOUT_CPU
:
1342 mutex_lock(&fanout_mutex
);
1344 list_for_each_entry(f
, &fanout_list
, list
) {
1346 read_pnet(&f
->net
) == sock_net(sk
)) {
1352 if (match
&& match
->defrag
!= defrag
)
1356 match
= kzalloc(sizeof(*match
), GFP_KERNEL
);
1359 write_pnet(&match
->net
, sock_net(sk
));
1362 match
->defrag
= defrag
;
1363 atomic_set(&match
->rr_cur
, 0);
1364 INIT_LIST_HEAD(&match
->list
);
1365 spin_lock_init(&match
->lock
);
1366 atomic_set(&match
->sk_ref
, 0);
1367 match
->prot_hook
.type
= po
->prot_hook
.type
;
1368 match
->prot_hook
.dev
= po
->prot_hook
.dev
;
1369 match
->prot_hook
.func
= packet_rcv_fanout
;
1370 match
->prot_hook
.af_packet_priv
= match
;
1371 dev_add_pack(&match
->prot_hook
);
1372 list_add(&match
->list
, &fanout_list
);
1375 if (match
->type
== type
&&
1376 match
->prot_hook
.type
== po
->prot_hook
.type
&&
1377 match
->prot_hook
.dev
== po
->prot_hook
.dev
) {
1379 if (atomic_read(&match
->sk_ref
) < PACKET_FANOUT_MAX
) {
1380 __dev_remove_pack(&po
->prot_hook
);
1382 atomic_inc(&match
->sk_ref
);
1383 __fanout_link(sk
, po
);
1388 mutex_unlock(&fanout_mutex
);
1392 static void fanout_release(struct sock
*sk
)
1394 struct packet_sock
*po
= pkt_sk(sk
);
1395 struct packet_fanout
*f
;
1403 mutex_lock(&fanout_mutex
);
1404 if (atomic_dec_and_test(&f
->sk_ref
)) {
1406 dev_remove_pack(&f
->prot_hook
);
1409 mutex_unlock(&fanout_mutex
);
1412 static const struct proto_ops packet_ops
;
1414 static const struct proto_ops packet_ops_spkt
;
1416 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
,
1417 struct packet_type
*pt
, struct net_device
*orig_dev
)
1420 struct sockaddr_pkt
*spkt
;
1423 * When we registered the protocol we saved the socket in the data
1424 * field for just this event.
1427 sk
= pt
->af_packet_priv
;
1430 * Yank back the headers [hope the device set this
1431 * right or kerboom...]
1433 * Incoming packets have ll header pulled,
1436 * For outgoing ones skb->data == skb_mac_header(skb)
1437 * so that this procedure is noop.
1440 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1443 if (!net_eq(dev_net(dev
), sock_net(sk
)))
1446 skb
= skb_share_check(skb
, GFP_ATOMIC
);
1450 /* drop any routing info */
1453 /* drop conntrack reference */
1456 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
1458 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
1461 * The SOCK_PACKET socket receives _all_ frames.
1464 spkt
->spkt_family
= dev
->type
;
1465 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
1466 spkt
->spkt_protocol
= skb
->protocol
;
1469 * Charge the memory to the socket. This is done specifically
1470 * to prevent sockets using all the memory up.
1473 if (sock_queue_rcv_skb(sk
, skb
) == 0)
1484 * Output a raw packet to a device layer. This bypasses all the other
1485 * protocol layers and you must therefore supply it with a complete frame
1488 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
1489 struct msghdr
*msg
, size_t len
)
1491 struct sock
*sk
= sock
->sk
;
1492 struct sockaddr_pkt
*saddr
= (struct sockaddr_pkt
*)msg
->msg_name
;
1493 struct sk_buff
*skb
= NULL
;
1494 struct net_device
*dev
;
1499 * Get and verify the address.
1503 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
1505 if (msg
->msg_namelen
== sizeof(struct sockaddr_pkt
))
1506 proto
= saddr
->spkt_protocol
;
1508 return -ENOTCONN
; /* SOCK_PACKET must be sent giving an address */
1511 * Find the device first to size check it
1514 saddr
->spkt_device
[13] = 0;
1517 dev
= dev_get_by_name_rcu(sock_net(sk
), saddr
->spkt_device
);
1523 if (!(dev
->flags
& IFF_UP
))
1527 * You may not queue a frame bigger than the mtu. This is the lowest level
1528 * raw protocol and you must do your own fragmentation at this level.
1532 if (len
> dev
->mtu
+ dev
->hard_header_len
+ VLAN_HLEN
)
1536 size_t reserved
= LL_RESERVED_SPACE(dev
);
1537 unsigned int hhlen
= dev
->header_ops
? dev
->hard_header_len
: 0;
1540 skb
= sock_wmalloc(sk
, len
+ reserved
, 0, GFP_KERNEL
);
1543 /* FIXME: Save some space for broken drivers that write a hard
1544 * header at transmission time by themselves. PPP is the notable
1545 * one here. This should really be fixed at the driver level.
1547 skb_reserve(skb
, reserved
);
1548 skb_reset_network_header(skb
);
1550 /* Try to align data part correctly */
1555 skb_reset_network_header(skb
);
1557 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
1563 if (len
> (dev
->mtu
+ dev
->hard_header_len
)) {
1564 /* Earlier code assumed this would be a VLAN pkt,
1565 * double-check this now that we have the actual
1568 struct ethhdr
*ehdr
;
1569 skb_reset_mac_header(skb
);
1570 ehdr
= eth_hdr(skb
);
1571 if (ehdr
->h_proto
!= htons(ETH_P_8021Q
)) {
1577 skb
->protocol
= proto
;
1579 skb
->priority
= sk
->sk_priority
;
1580 skb
->mark
= sk
->sk_mark
;
1581 err
= sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
1585 dev_queue_xmit(skb
);
1596 static inline unsigned int run_filter(const struct sk_buff
*skb
,
1597 const struct sock
*sk
,
1600 struct sk_filter
*filter
;
1603 filter
= rcu_dereference(sk
->sk_filter
);
1605 res
= SK_RUN_FILTER(filter
, skb
);
1612 * This function makes lazy skb cloning in hope that most of packets
1613 * are discarded by BPF.
1615 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1616 * and skb->cb are mangled. It works because (and until) packets
1617 * falling here are owned by current CPU. Output packets are cloned
1618 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1619 * sequencially, so that if we return skb to original state on exit,
1620 * we will not harm anyone.
1623 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1624 struct packet_type
*pt
, struct net_device
*orig_dev
)
1627 struct sockaddr_ll
*sll
;
1628 struct packet_sock
*po
;
1629 u8
*skb_head
= skb
->data
;
1630 int skb_len
= skb
->len
;
1631 unsigned int snaplen
, res
;
1633 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1636 sk
= pt
->af_packet_priv
;
1639 if (!net_eq(dev_net(dev
), sock_net(sk
)))
1644 if (dev
->header_ops
) {
1645 /* The device has an explicit notion of ll header,
1646 * exported to higher levels.
1648 * Otherwise, the device hides details of its frame
1649 * structure, so that corresponding packet head is
1650 * never delivered to user.
1652 if (sk
->sk_type
!= SOCK_DGRAM
)
1653 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
1654 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
1655 /* Special case: outgoing packets have ll header at head */
1656 skb_pull(skb
, skb_network_offset(skb
));
1662 res
= run_filter(skb
, sk
, snaplen
);
1664 goto drop_n_restore
;
1668 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
1669 (unsigned)sk
->sk_rcvbuf
)
1672 if (skb_shared(skb
)) {
1673 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
1677 if (skb_head
!= skb
->data
) {
1678 skb
->data
= skb_head
;
1685 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
1688 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1689 sll
->sll_family
= AF_PACKET
;
1690 sll
->sll_hatype
= dev
->type
;
1691 sll
->sll_protocol
= skb
->protocol
;
1692 sll
->sll_pkttype
= skb
->pkt_type
;
1693 if (unlikely(po
->origdev
))
1694 sll
->sll_ifindex
= orig_dev
->ifindex
;
1696 sll
->sll_ifindex
= dev
->ifindex
;
1698 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
1700 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
1702 if (pskb_trim(skb
, snaplen
))
1705 skb_set_owner_r(skb
, sk
);
1709 /* drop conntrack reference */
1712 spin_lock(&sk
->sk_receive_queue
.lock
);
1713 po
->stats
.tp_packets
++;
1714 skb
->dropcount
= atomic_read(&sk
->sk_drops
);
1715 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
1716 spin_unlock(&sk
->sk_receive_queue
.lock
);
1717 sk
->sk_data_ready(sk
, skb
->len
);
1721 po
->stats
.tp_drops
= atomic_inc_return(&sk
->sk_drops
);
1724 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
1725 skb
->data
= skb_head
;
1733 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1734 struct packet_type
*pt
, struct net_device
*orig_dev
)
1737 struct packet_sock
*po
;
1738 struct sockaddr_ll
*sll
;
1740 struct tpacket_hdr
*h1
;
1741 struct tpacket2_hdr
*h2
;
1742 struct tpacket3_hdr
*h3
;
1745 u8
*skb_head
= skb
->data
;
1746 int skb_len
= skb
->len
;
1747 unsigned int snaplen
, res
;
1748 unsigned long status
= TP_STATUS_USER
;
1749 unsigned short macoff
, netoff
, hdrlen
;
1750 struct sk_buff
*copy_skb
= NULL
;
1753 struct skb_shared_hwtstamps
*shhwtstamps
= skb_hwtstamps(skb
);
1755 if (skb
->pkt_type
== PACKET_LOOPBACK
)
1758 sk
= pt
->af_packet_priv
;
1761 if (!net_eq(dev_net(dev
), sock_net(sk
)))
1764 if (dev
->header_ops
) {
1765 if (sk
->sk_type
!= SOCK_DGRAM
)
1766 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
1767 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
1768 /* Special case: outgoing packets have ll header at head */
1769 skb_pull(skb
, skb_network_offset(skb
));
1773 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1774 status
|= TP_STATUS_CSUMNOTREADY
;
1778 res
= run_filter(skb
, sk
, snaplen
);
1780 goto drop_n_restore
;
1784 if (sk
->sk_type
== SOCK_DGRAM
) {
1785 macoff
= netoff
= TPACKET_ALIGN(po
->tp_hdrlen
) + 16 +
1788 unsigned maclen
= skb_network_offset(skb
);
1789 netoff
= TPACKET_ALIGN(po
->tp_hdrlen
+
1790 (maclen
< 16 ? 16 : maclen
)) +
1792 macoff
= netoff
- maclen
;
1794 if (po
->tp_version
<= TPACKET_V2
) {
1795 if (macoff
+ snaplen
> po
->rx_ring
.frame_size
) {
1796 if (po
->copy_thresh
&&
1797 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
1798 < (unsigned)sk
->sk_rcvbuf
) {
1799 if (skb_shared(skb
)) {
1800 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
1802 copy_skb
= skb_get(skb
);
1803 skb_head
= skb
->data
;
1806 skb_set_owner_r(copy_skb
, sk
);
1808 snaplen
= po
->rx_ring
.frame_size
- macoff
;
1809 if ((int)snaplen
< 0)
1813 spin_lock(&sk
->sk_receive_queue
.lock
);
1814 h
.raw
= packet_current_rx_frame(po
, skb
,
1815 TP_STATUS_KERNEL
, (macoff
+snaplen
));
1818 if (po
->tp_version
<= TPACKET_V2
) {
1819 packet_increment_rx_head(po
, &po
->rx_ring
);
1821 * LOSING will be reported till you read the stats,
1822 * because it's COR - Clear On Read.
1823 * Anyways, moving it for V1/V2 only as V3 doesn't need this
1826 if (po
->stats
.tp_drops
)
1827 status
|= TP_STATUS_LOSING
;
1829 po
->stats
.tp_packets
++;
1831 status
|= TP_STATUS_COPY
;
1832 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
1834 spin_unlock(&sk
->sk_receive_queue
.lock
);
1836 skb_copy_bits(skb
, 0, h
.raw
+ macoff
, snaplen
);
1838 switch (po
->tp_version
) {
1840 h
.h1
->tp_len
= skb
->len
;
1841 h
.h1
->tp_snaplen
= snaplen
;
1842 h
.h1
->tp_mac
= macoff
;
1843 h
.h1
->tp_net
= netoff
;
1844 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
1845 && shhwtstamps
->syststamp
.tv64
)
1846 tv
= ktime_to_timeval(shhwtstamps
->syststamp
);
1847 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
1848 && shhwtstamps
->hwtstamp
.tv64
)
1849 tv
= ktime_to_timeval(shhwtstamps
->hwtstamp
);
1850 else if (skb
->tstamp
.tv64
)
1851 tv
= ktime_to_timeval(skb
->tstamp
);
1853 do_gettimeofday(&tv
);
1854 h
.h1
->tp_sec
= tv
.tv_sec
;
1855 h
.h1
->tp_usec
= tv
.tv_usec
;
1856 hdrlen
= sizeof(*h
.h1
);
1859 h
.h2
->tp_len
= skb
->len
;
1860 h
.h2
->tp_snaplen
= snaplen
;
1861 h
.h2
->tp_mac
= macoff
;
1862 h
.h2
->tp_net
= netoff
;
1863 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
1864 && shhwtstamps
->syststamp
.tv64
)
1865 ts
= ktime_to_timespec(shhwtstamps
->syststamp
);
1866 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
1867 && shhwtstamps
->hwtstamp
.tv64
)
1868 ts
= ktime_to_timespec(shhwtstamps
->hwtstamp
);
1869 else if (skb
->tstamp
.tv64
)
1870 ts
= ktime_to_timespec(skb
->tstamp
);
1872 getnstimeofday(&ts
);
1873 h
.h2
->tp_sec
= ts
.tv_sec
;
1874 h
.h2
->tp_nsec
= ts
.tv_nsec
;
1875 if (vlan_tx_tag_present(skb
)) {
1876 h
.h2
->tp_vlan_tci
= vlan_tx_tag_get(skb
);
1877 status
|= TP_STATUS_VLAN_VALID
;
1879 h
.h2
->tp_vlan_tci
= 0;
1881 h
.h2
->tp_padding
= 0;
1882 hdrlen
= sizeof(*h
.h2
);
1885 /* tp_nxt_offset,vlan are already populated above.
1886 * So DONT clear those fields here
1888 h
.h3
->tp_status
|= status
;
1889 h
.h3
->tp_len
= skb
->len
;
1890 h
.h3
->tp_snaplen
= snaplen
;
1891 h
.h3
->tp_mac
= macoff
;
1892 h
.h3
->tp_net
= netoff
;
1893 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
1894 && shhwtstamps
->syststamp
.tv64
)
1895 ts
= ktime_to_timespec(shhwtstamps
->syststamp
);
1896 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
1897 && shhwtstamps
->hwtstamp
.tv64
)
1898 ts
= ktime_to_timespec(shhwtstamps
->hwtstamp
);
1899 else if (skb
->tstamp
.tv64
)
1900 ts
= ktime_to_timespec(skb
->tstamp
);
1902 getnstimeofday(&ts
);
1903 h
.h3
->tp_sec
= ts
.tv_sec
;
1904 h
.h3
->tp_nsec
= ts
.tv_nsec
;
1905 hdrlen
= sizeof(*h
.h3
);
1911 sll
= h
.raw
+ TPACKET_ALIGN(hdrlen
);
1912 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
1913 sll
->sll_family
= AF_PACKET
;
1914 sll
->sll_hatype
= dev
->type
;
1915 sll
->sll_protocol
= skb
->protocol
;
1916 sll
->sll_pkttype
= skb
->pkt_type
;
1917 if (unlikely(po
->origdev
))
1918 sll
->sll_ifindex
= orig_dev
->ifindex
;
1920 sll
->sll_ifindex
= dev
->ifindex
;
1923 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
1927 if (po
->tp_version
<= TPACKET_V2
) {
1928 end
= (u8
*)PAGE_ALIGN((unsigned long)h
.raw
1929 + macoff
+ snaplen
);
1930 for (start
= h
.raw
; start
< end
; start
+= PAGE_SIZE
)
1931 flush_dcache_page(pgv_to_page(start
));
1936 if (po
->tp_version
<= TPACKET_V2
)
1937 __packet_set_status(po
, h
.raw
, status
);
1939 prb_clear_blk_fill_status(&po
->rx_ring
);
1941 sk
->sk_data_ready(sk
, 0);
1944 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
1945 skb
->data
= skb_head
;
1953 po
->stats
.tp_drops
++;
1954 spin_unlock(&sk
->sk_receive_queue
.lock
);
1956 sk
->sk_data_ready(sk
, 0);
1957 kfree_skb(copy_skb
);
1958 goto drop_n_restore
;
1961 static void tpacket_destruct_skb(struct sk_buff
*skb
)
1963 struct packet_sock
*po
= pkt_sk(skb
->sk
);
1966 BUG_ON(skb
== NULL
);
1968 if (likely(po
->tx_ring
.pg_vec
)) {
1969 ph
= skb_shinfo(skb
)->destructor_arg
;
1970 BUG_ON(__packet_get_status(po
, ph
) != TP_STATUS_SENDING
);
1971 BUG_ON(atomic_read(&po
->tx_ring
.pending
) == 0);
1972 atomic_dec(&po
->tx_ring
.pending
);
1973 __packet_set_status(po
, ph
, TP_STATUS_AVAILABLE
);
1979 static int tpacket_fill_skb(struct packet_sock
*po
, struct sk_buff
*skb
,
1980 void *frame
, struct net_device
*dev
, int size_max
,
1981 __be16 proto
, unsigned char *addr
)
1984 struct tpacket_hdr
*h1
;
1985 struct tpacket2_hdr
*h2
;
1988 int to_write
, offset
, len
, tp_len
, nr_frags
, len_max
;
1989 struct socket
*sock
= po
->sk
.sk_socket
;
1996 skb
->protocol
= proto
;
1998 skb
->priority
= po
->sk
.sk_priority
;
1999 skb
->mark
= po
->sk
.sk_mark
;
2000 skb_shinfo(skb
)->destructor_arg
= ph
.raw
;
2002 switch (po
->tp_version
) {
2004 tp_len
= ph
.h2
->tp_len
;
2007 tp_len
= ph
.h1
->tp_len
;
2010 if (unlikely(tp_len
> size_max
)) {
2011 pr_err("packet size is too long (%d > %d)\n", tp_len
, size_max
);
2015 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
2016 skb_reset_network_header(skb
);
2018 data
= ph
.raw
+ po
->tp_hdrlen
- sizeof(struct sockaddr_ll
);
2021 if (sock
->type
== SOCK_DGRAM
) {
2022 err
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
,
2024 if (unlikely(err
< 0))
2026 } else if (dev
->hard_header_len
) {
2027 /* net device doesn't like empty head */
2028 if (unlikely(tp_len
<= dev
->hard_header_len
)) {
2029 pr_err("packet size is too short (%d < %d)\n",
2030 tp_len
, dev
->hard_header_len
);
2034 skb_push(skb
, dev
->hard_header_len
);
2035 err
= skb_store_bits(skb
, 0, data
,
2036 dev
->hard_header_len
);
2040 data
+= dev
->hard_header_len
;
2041 to_write
-= dev
->hard_header_len
;
2045 offset
= offset_in_page(data
);
2046 len_max
= PAGE_SIZE
- offset
;
2047 len
= ((to_write
> len_max
) ? len_max
: to_write
);
2049 skb
->data_len
= to_write
;
2050 skb
->len
+= to_write
;
2051 skb
->truesize
+= to_write
;
2052 atomic_add(to_write
, &po
->sk
.sk_wmem_alloc
);
2054 while (likely(to_write
)) {
2055 nr_frags
= skb_shinfo(skb
)->nr_frags
;
2057 if (unlikely(nr_frags
>= MAX_SKB_FRAGS
)) {
2058 pr_err("Packet exceed the number of skb frags(%lu)\n",
2063 page
= pgv_to_page(data
);
2065 flush_dcache_page(page
);
2067 skb_fill_page_desc(skb
, nr_frags
, page
, offset
, len
);
2070 len_max
= PAGE_SIZE
;
2071 len
= ((to_write
> len_max
) ? len_max
: to_write
);
2077 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
)
2079 struct sk_buff
*skb
;
2080 struct net_device
*dev
;
2082 bool need_rls_dev
= false;
2083 int err
, reserve
= 0;
2085 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
2086 int tp_len
, size_max
;
2087 unsigned char *addr
;
2091 mutex_lock(&po
->pg_vec_lock
);
2094 if (saddr
== NULL
) {
2095 dev
= po
->prot_hook
.dev
;
2100 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
2102 if (msg
->msg_namelen
< (saddr
->sll_halen
2103 + offsetof(struct sockaddr_ll
,
2106 proto
= saddr
->sll_protocol
;
2107 addr
= saddr
->sll_addr
;
2108 dev
= dev_get_by_index(sock_net(&po
->sk
), saddr
->sll_ifindex
);
2109 need_rls_dev
= true;
2113 if (unlikely(dev
== NULL
))
2116 reserve
= dev
->hard_header_len
;
2119 if (unlikely(!(dev
->flags
& IFF_UP
)))
2122 size_max
= po
->tx_ring
.frame_size
2123 - (po
->tp_hdrlen
- sizeof(struct sockaddr_ll
));
2125 if (size_max
> dev
->mtu
+ reserve
)
2126 size_max
= dev
->mtu
+ reserve
;
2129 ph
= packet_current_frame(po
, &po
->tx_ring
,
2130 TP_STATUS_SEND_REQUEST
);
2132 if (unlikely(ph
== NULL
)) {
2137 status
= TP_STATUS_SEND_REQUEST
;
2138 skb
= sock_alloc_send_skb(&po
->sk
,
2139 LL_ALLOCATED_SPACE(dev
)
2140 + sizeof(struct sockaddr_ll
),
2143 if (unlikely(skb
== NULL
))
2146 tp_len
= tpacket_fill_skb(po
, skb
, ph
, dev
, size_max
, proto
,
2149 if (unlikely(tp_len
< 0)) {
2151 __packet_set_status(po
, ph
,
2152 TP_STATUS_AVAILABLE
);
2153 packet_increment_head(&po
->tx_ring
);
2157 status
= TP_STATUS_WRONG_FORMAT
;
2163 skb
->destructor
= tpacket_destruct_skb
;
2164 __packet_set_status(po
, ph
, TP_STATUS_SENDING
);
2165 atomic_inc(&po
->tx_ring
.pending
);
2167 status
= TP_STATUS_SEND_REQUEST
;
2168 err
= dev_queue_xmit(skb
);
2169 if (unlikely(err
> 0)) {
2170 err
= net_xmit_errno(err
);
2171 if (err
&& __packet_get_status(po
, ph
) ==
2172 TP_STATUS_AVAILABLE
) {
2173 /* skb was destructed already */
2178 * skb was dropped but not destructed yet;
2179 * let's treat it like congestion or err < 0
2183 packet_increment_head(&po
->tx_ring
);
2185 } while (likely((ph
!= NULL
) ||
2186 ((!(msg
->msg_flags
& MSG_DONTWAIT
)) &&
2187 (atomic_read(&po
->tx_ring
.pending
))))
2194 __packet_set_status(po
, ph
, status
);
2200 mutex_unlock(&po
->pg_vec_lock
);
2204 static inline struct sk_buff
*packet_alloc_skb(struct sock
*sk
, size_t prepad
,
2205 size_t reserve
, size_t len
,
2206 size_t linear
, int noblock
,
2209 struct sk_buff
*skb
;
2211 /* Under a page? Don't bother with paged skb. */
2212 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
2215 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
2220 skb_reserve(skb
, reserve
);
2221 skb_put(skb
, linear
);
2222 skb
->data_len
= len
- linear
;
2223 skb
->len
+= len
- linear
;
2228 static int packet_snd(struct socket
*sock
,
2229 struct msghdr
*msg
, size_t len
)
2231 struct sock
*sk
= sock
->sk
;
2232 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
2233 struct sk_buff
*skb
;
2234 struct net_device
*dev
;
2236 bool need_rls_dev
= false;
2237 unsigned char *addr
;
2238 int err
, reserve
= 0;
2239 struct virtio_net_hdr vnet_hdr
= { 0 };
2242 struct packet_sock
*po
= pkt_sk(sk
);
2243 unsigned short gso_type
= 0;
2246 * Get and verify the address.
2249 if (saddr
== NULL
) {
2250 dev
= po
->prot_hook
.dev
;
2255 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
2257 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
2259 proto
= saddr
->sll_protocol
;
2260 addr
= saddr
->sll_addr
;
2261 dev
= dev_get_by_index(sock_net(sk
), saddr
->sll_ifindex
);
2262 need_rls_dev
= true;
2268 if (sock
->type
== SOCK_RAW
)
2269 reserve
= dev
->hard_header_len
;
2272 if (!(dev
->flags
& IFF_UP
))
2275 if (po
->has_vnet_hdr
) {
2276 vnet_hdr_len
= sizeof(vnet_hdr
);
2279 if (len
< vnet_hdr_len
)
2282 len
-= vnet_hdr_len
;
2284 err
= memcpy_fromiovec((void *)&vnet_hdr
, msg
->msg_iov
,
2289 if ((vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
2290 (vnet_hdr
.csum_start
+ vnet_hdr
.csum_offset
+ 2 >
2292 vnet_hdr
.hdr_len
= vnet_hdr
.csum_start
+
2293 vnet_hdr
.csum_offset
+ 2;
2296 if (vnet_hdr
.hdr_len
> len
)
2299 if (vnet_hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
2300 switch (vnet_hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
2301 case VIRTIO_NET_HDR_GSO_TCPV4
:
2302 gso_type
= SKB_GSO_TCPV4
;
2304 case VIRTIO_NET_HDR_GSO_TCPV6
:
2305 gso_type
= SKB_GSO_TCPV6
;
2307 case VIRTIO_NET_HDR_GSO_UDP
:
2308 gso_type
= SKB_GSO_UDP
;
2314 if (vnet_hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
2315 gso_type
|= SKB_GSO_TCP_ECN
;
2317 if (vnet_hdr
.gso_size
== 0)
2324 if (!gso_type
&& (len
> dev
->mtu
+ reserve
+ VLAN_HLEN
))
2328 skb
= packet_alloc_skb(sk
, LL_ALLOCATED_SPACE(dev
),
2329 LL_RESERVED_SPACE(dev
), len
, vnet_hdr
.hdr_len
,
2330 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
2334 skb_set_network_header(skb
, reserve
);
2337 if (sock
->type
== SOCK_DGRAM
&&
2338 (offset
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
)) < 0)
2341 /* Returns -EFAULT on error */
2342 err
= skb_copy_datagram_from_iovec(skb
, offset
, msg
->msg_iov
, 0, len
);
2345 err
= sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
2349 if (!gso_type
&& (len
> dev
->mtu
+ reserve
)) {
2350 /* Earlier code assumed this would be a VLAN pkt,
2351 * double-check this now that we have the actual
2354 struct ethhdr
*ehdr
;
2355 skb_reset_mac_header(skb
);
2356 ehdr
= eth_hdr(skb
);
2357 if (ehdr
->h_proto
!= htons(ETH_P_8021Q
)) {
2363 skb
->protocol
= proto
;
2365 skb
->priority
= sk
->sk_priority
;
2366 skb
->mark
= sk
->sk_mark
;
2368 if (po
->has_vnet_hdr
) {
2369 if (vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
2370 if (!skb_partial_csum_set(skb
, vnet_hdr
.csum_start
,
2371 vnet_hdr
.csum_offset
)) {
2377 skb_shinfo(skb
)->gso_size
= vnet_hdr
.gso_size
;
2378 skb_shinfo(skb
)->gso_type
= gso_type
;
2380 /* Header must be checked, and gso_segs computed. */
2381 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
2382 skb_shinfo(skb
)->gso_segs
= 0;
2384 len
+= vnet_hdr_len
;
2391 err
= dev_queue_xmit(skb
);
2392 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
2403 if (dev
&& need_rls_dev
)
2409 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
2410 struct msghdr
*msg
, size_t len
)
2412 struct sock
*sk
= sock
->sk
;
2413 struct packet_sock
*po
= pkt_sk(sk
);
2414 if (po
->tx_ring
.pg_vec
)
2415 return tpacket_snd(po
, msg
);
2417 return packet_snd(sock
, msg
, len
);
2421 * Close a PACKET socket. This is fairly simple. We immediately go
2422 * to 'closed' state and remove our protocol entry in the device list.
2425 static int packet_release(struct socket
*sock
)
2427 struct sock
*sk
= sock
->sk
;
2428 struct packet_sock
*po
;
2430 union tpacket_req_u req_u
;
2438 spin_lock_bh(&net
->packet
.sklist_lock
);
2439 sk_del_node_init_rcu(sk
);
2440 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
2441 spin_unlock_bh(&net
->packet
.sklist_lock
);
2443 spin_lock(&po
->bind_lock
);
2444 unregister_prot_hook(sk
, false);
2445 if (po
->prot_hook
.dev
) {
2446 dev_put(po
->prot_hook
.dev
);
2447 po
->prot_hook
.dev
= NULL
;
2449 spin_unlock(&po
->bind_lock
);
2451 packet_flush_mclist(sk
);
2453 memset(&req_u
, 0, sizeof(req_u
));
2455 if (po
->rx_ring
.pg_vec
)
2456 packet_set_ring(sk
, &req_u
, 1, 0);
2458 if (po
->tx_ring
.pg_vec
)
2459 packet_set_ring(sk
, &req_u
, 1, 1);
2465 * Now the socket is dead. No more input will appear.
2472 skb_queue_purge(&sk
->sk_receive_queue
);
2473 sk_refcnt_debug_release(sk
);
2480 * Attach a packet hook.
2483 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
2485 struct packet_sock
*po
= pkt_sk(sk
);
2492 spin_lock(&po
->bind_lock
);
2493 unregister_prot_hook(sk
, true);
2495 po
->prot_hook
.type
= protocol
;
2496 if (po
->prot_hook
.dev
)
2497 dev_put(po
->prot_hook
.dev
);
2498 po
->prot_hook
.dev
= dev
;
2500 po
->ifindex
= dev
? dev
->ifindex
: 0;
2505 if (!dev
|| (dev
->flags
& IFF_UP
)) {
2506 register_prot_hook(sk
);
2508 sk
->sk_err
= ENETDOWN
;
2509 if (!sock_flag(sk
, SOCK_DEAD
))
2510 sk
->sk_error_report(sk
);
2514 spin_unlock(&po
->bind_lock
);
2520 * Bind a packet socket to a device
2523 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
2526 struct sock
*sk
= sock
->sk
;
2528 struct net_device
*dev
;
2535 if (addr_len
!= sizeof(struct sockaddr
))
2537 strlcpy(name
, uaddr
->sa_data
, sizeof(name
));
2539 dev
= dev_get_by_name(sock_net(sk
), name
);
2541 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
2545 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
2547 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
2548 struct sock
*sk
= sock
->sk
;
2549 struct net_device
*dev
= NULL
;
2557 if (addr_len
< sizeof(struct sockaddr_ll
))
2559 if (sll
->sll_family
!= AF_PACKET
)
2562 if (sll
->sll_ifindex
) {
2564 dev
= dev_get_by_index(sock_net(sk
), sll
->sll_ifindex
);
2568 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
2574 static struct proto packet_proto
= {
2576 .owner
= THIS_MODULE
,
2577 .obj_size
= sizeof(struct packet_sock
),
2581 * Create a packet of type SOCK_PACKET.
2584 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
,
2588 struct packet_sock
*po
;
2589 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
2592 if (!capable(CAP_NET_RAW
))
2594 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
2595 sock
->type
!= SOCK_PACKET
)
2596 return -ESOCKTNOSUPPORT
;
2598 sock
->state
= SS_UNCONNECTED
;
2601 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
2605 sock
->ops
= &packet_ops
;
2606 if (sock
->type
== SOCK_PACKET
)
2607 sock
->ops
= &packet_ops_spkt
;
2609 sock_init_data(sock
, sk
);
2612 sk
->sk_family
= PF_PACKET
;
2615 sk
->sk_destruct
= packet_sock_destruct
;
2616 sk_refcnt_debug_inc(sk
);
2619 * Attach a protocol block
2622 spin_lock_init(&po
->bind_lock
);
2623 mutex_init(&po
->pg_vec_lock
);
2624 po
->prot_hook
.func
= packet_rcv
;
2626 if (sock
->type
== SOCK_PACKET
)
2627 po
->prot_hook
.func
= packet_rcv_spkt
;
2629 po
->prot_hook
.af_packet_priv
= sk
;
2632 po
->prot_hook
.type
= proto
;
2633 register_prot_hook(sk
);
2636 spin_lock_bh(&net
->packet
.sklist_lock
);
2637 sk_add_node_rcu(sk
, &net
->packet
.sklist
);
2638 sock_prot_inuse_add(net
, &packet_proto
, 1);
2639 spin_unlock_bh(&net
->packet
.sklist_lock
);
2646 static int packet_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
2648 struct sock_exterr_skb
*serr
;
2649 struct sk_buff
*skb
, *skb2
;
2653 skb
= skb_dequeue(&sk
->sk_error_queue
);
2659 msg
->msg_flags
|= MSG_TRUNC
;
2662 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
2666 sock_recv_timestamp(msg
, sk
, skb
);
2668 serr
= SKB_EXT_ERR(skb
);
2669 put_cmsg(msg
, SOL_PACKET
, PACKET_TX_TIMESTAMP
,
2670 sizeof(serr
->ee
), &serr
->ee
);
2672 msg
->msg_flags
|= MSG_ERRQUEUE
;
2675 /* Reset and regenerate socket error */
2676 spin_lock_bh(&sk
->sk_error_queue
.lock
);
2678 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
2679 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
2680 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
2681 sk
->sk_error_report(sk
);
2683 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
2692 * Pull a packet from our receive queue and hand it to the user.
2693 * If necessary we block.
2696 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
2697 struct msghdr
*msg
, size_t len
, int flags
)
2699 struct sock
*sk
= sock
->sk
;
2700 struct sk_buff
*skb
;
2702 struct sockaddr_ll
*sll
;
2703 int vnet_hdr_len
= 0;
2706 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
|MSG_ERRQUEUE
))
2710 /* What error should we return now? EUNATTACH? */
2711 if (pkt_sk(sk
)->ifindex
< 0)
2715 if (flags
& MSG_ERRQUEUE
) {
2716 err
= packet_recv_error(sk
, msg
, len
);
2721 * Call the generic datagram receiver. This handles all sorts
2722 * of horrible races and re-entrancy so we can forget about it
2723 * in the protocol layers.
2725 * Now it will return ENETDOWN, if device have just gone down,
2726 * but then it will block.
2729 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
2732 * An error occurred so return it. Because skb_recv_datagram()
2733 * handles the blocking we don't see and worry about blocking
2740 if (pkt_sk(sk
)->has_vnet_hdr
) {
2741 struct virtio_net_hdr vnet_hdr
= { 0 };
2744 vnet_hdr_len
= sizeof(vnet_hdr
);
2745 if (len
< vnet_hdr_len
)
2748 len
-= vnet_hdr_len
;
2750 if (skb_is_gso(skb
)) {
2751 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
2753 /* This is a hint as to how much should be linear. */
2754 vnet_hdr
.hdr_len
= skb_headlen(skb
);
2755 vnet_hdr
.gso_size
= sinfo
->gso_size
;
2756 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
2757 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2758 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
2759 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2760 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
2761 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
2762 else if (sinfo
->gso_type
& SKB_GSO_FCOE
)
2766 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
2767 vnet_hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
2769 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
2771 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2772 vnet_hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
2773 vnet_hdr
.csum_start
= skb_checksum_start_offset(skb
);
2774 vnet_hdr
.csum_offset
= skb
->csum_offset
;
2775 } else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
) {
2776 vnet_hdr
.flags
= VIRTIO_NET_HDR_F_DATA_VALID
;
2777 } /* else everything is zero */
2779 err
= memcpy_toiovec(msg
->msg_iov
, (void *)&vnet_hdr
,
2786 * If the address length field is there to be filled in, we fill
2790 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
2791 if (sock
->type
== SOCK_PACKET
)
2792 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
2794 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
2797 * You lose any data beyond the buffer you gave. If it worries a
2798 * user program they can ask the device for its MTU anyway.
2804 msg
->msg_flags
|= MSG_TRUNC
;
2807 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
2811 sock_recv_ts_and_drops(msg
, sk
, skb
);
2814 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
2817 if (pkt_sk(sk
)->auxdata
) {
2818 struct tpacket_auxdata aux
;
2820 aux
.tp_status
= TP_STATUS_USER
;
2821 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2822 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
2823 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
2824 aux
.tp_snaplen
= skb
->len
;
2826 aux
.tp_net
= skb_network_offset(skb
);
2827 if (vlan_tx_tag_present(skb
)) {
2828 aux
.tp_vlan_tci
= vlan_tx_tag_get(skb
);
2829 aux
.tp_status
|= TP_STATUS_VLAN_VALID
;
2831 aux
.tp_vlan_tci
= 0;
2834 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
2838 * Free or return the buffer as appropriate. Again this
2839 * hides all the races and re-entrancy issues from us.
2841 err
= vnet_hdr_len
+ ((flags
&MSG_TRUNC
) ? skb
->len
: copied
);
2844 skb_free_datagram(sk
, skb
);
2849 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
2850 int *uaddr_len
, int peer
)
2852 struct net_device
*dev
;
2853 struct sock
*sk
= sock
->sk
;
2858 uaddr
->sa_family
= AF_PACKET
;
2860 dev
= dev_get_by_index_rcu(sock_net(sk
), pkt_sk(sk
)->ifindex
);
2862 strncpy(uaddr
->sa_data
, dev
->name
, 14);
2864 memset(uaddr
->sa_data
, 0, 14);
2866 *uaddr_len
= sizeof(*uaddr
);
2871 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
2872 int *uaddr_len
, int peer
)
2874 struct net_device
*dev
;
2875 struct sock
*sk
= sock
->sk
;
2876 struct packet_sock
*po
= pkt_sk(sk
);
2877 DECLARE_SOCKADDR(struct sockaddr_ll
*, sll
, uaddr
);
2882 sll
->sll_family
= AF_PACKET
;
2883 sll
->sll_ifindex
= po
->ifindex
;
2884 sll
->sll_protocol
= po
->num
;
2885 sll
->sll_pkttype
= 0;
2887 dev
= dev_get_by_index_rcu(sock_net(sk
), po
->ifindex
);
2889 sll
->sll_hatype
= dev
->type
;
2890 sll
->sll_halen
= dev
->addr_len
;
2891 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
2893 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
2897 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
2902 static int packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
,
2906 case PACKET_MR_MULTICAST
:
2907 if (i
->alen
!= dev
->addr_len
)
2910 return dev_mc_add(dev
, i
->addr
);
2912 return dev_mc_del(dev
, i
->addr
);
2914 case PACKET_MR_PROMISC
:
2915 return dev_set_promiscuity(dev
, what
);
2917 case PACKET_MR_ALLMULTI
:
2918 return dev_set_allmulti(dev
, what
);
2920 case PACKET_MR_UNICAST
:
2921 if (i
->alen
!= dev
->addr_len
)
2924 return dev_uc_add(dev
, i
->addr
);
2926 return dev_uc_del(dev
, i
->addr
);
2934 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
2936 for ( ; i
; i
= i
->next
) {
2937 if (i
->ifindex
== dev
->ifindex
)
2938 packet_dev_mc(dev
, i
, what
);
2942 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
2944 struct packet_sock
*po
= pkt_sk(sk
);
2945 struct packet_mclist
*ml
, *i
;
2946 struct net_device
*dev
;
2952 dev
= __dev_get_by_index(sock_net(sk
), mreq
->mr_ifindex
);
2957 if (mreq
->mr_alen
> dev
->addr_len
)
2961 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
2966 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
2967 if (ml
->ifindex
== mreq
->mr_ifindex
&&
2968 ml
->type
== mreq
->mr_type
&&
2969 ml
->alen
== mreq
->mr_alen
&&
2970 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
2972 /* Free the new element ... */
2978 i
->type
= mreq
->mr_type
;
2979 i
->ifindex
= mreq
->mr_ifindex
;
2980 i
->alen
= mreq
->mr_alen
;
2981 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
2983 i
->next
= po
->mclist
;
2985 err
= packet_dev_mc(dev
, i
, 1);
2987 po
->mclist
= i
->next
;
2996 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
2998 struct packet_mclist
*ml
, **mlp
;
3002 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
3003 if (ml
->ifindex
== mreq
->mr_ifindex
&&
3004 ml
->type
== mreq
->mr_type
&&
3005 ml
->alen
== mreq
->mr_alen
&&
3006 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
3007 if (--ml
->count
== 0) {
3008 struct net_device
*dev
;
3010 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
3012 packet_dev_mc(dev
, ml
, -1);
3020 return -EADDRNOTAVAIL
;
3023 static void packet_flush_mclist(struct sock
*sk
)
3025 struct packet_sock
*po
= pkt_sk(sk
);
3026 struct packet_mclist
*ml
;
3032 while ((ml
= po
->mclist
) != NULL
) {
3033 struct net_device
*dev
;
3035 po
->mclist
= ml
->next
;
3036 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
3038 packet_dev_mc(dev
, ml
, -1);
3045 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
3047 struct sock
*sk
= sock
->sk
;
3048 struct packet_sock
*po
= pkt_sk(sk
);
3051 if (level
!= SOL_PACKET
)
3052 return -ENOPROTOOPT
;
3055 case PACKET_ADD_MEMBERSHIP
:
3056 case PACKET_DROP_MEMBERSHIP
:
3058 struct packet_mreq_max mreq
;
3060 memset(&mreq
, 0, sizeof(mreq
));
3061 if (len
< sizeof(struct packet_mreq
))
3063 if (len
> sizeof(mreq
))
3065 if (copy_from_user(&mreq
, optval
, len
))
3067 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
3069 if (optname
== PACKET_ADD_MEMBERSHIP
)
3070 ret
= packet_mc_add(sk
, &mreq
);
3072 ret
= packet_mc_drop(sk
, &mreq
);
3076 case PACKET_RX_RING
:
3077 case PACKET_TX_RING
:
3079 union tpacket_req_u req_u
;
3082 switch (po
->tp_version
) {
3085 len
= sizeof(req_u
.req
);
3089 len
= sizeof(req_u
.req3
);
3094 if (pkt_sk(sk
)->has_vnet_hdr
)
3096 if (copy_from_user(&req_u
.req
, optval
, len
))
3098 return packet_set_ring(sk
, &req_u
, 0,
3099 optname
== PACKET_TX_RING
);
3101 case PACKET_COPY_THRESH
:
3105 if (optlen
!= sizeof(val
))
3107 if (copy_from_user(&val
, optval
, sizeof(val
)))
3110 pkt_sk(sk
)->copy_thresh
= val
;
3113 case PACKET_VERSION
:
3117 if (optlen
!= sizeof(val
))
3119 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
3121 if (copy_from_user(&val
, optval
, sizeof(val
)))
3127 po
->tp_version
= val
;
3133 case PACKET_RESERVE
:
3137 if (optlen
!= sizeof(val
))
3139 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
3141 if (copy_from_user(&val
, optval
, sizeof(val
)))
3143 po
->tp_reserve
= val
;
3150 if (optlen
!= sizeof(val
))
3152 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
3154 if (copy_from_user(&val
, optval
, sizeof(val
)))
3156 po
->tp_loss
= !!val
;
3159 case PACKET_AUXDATA
:
3163 if (optlen
< sizeof(val
))
3165 if (copy_from_user(&val
, optval
, sizeof(val
)))
3168 po
->auxdata
= !!val
;
3171 case PACKET_ORIGDEV
:
3175 if (optlen
< sizeof(val
))
3177 if (copy_from_user(&val
, optval
, sizeof(val
)))
3180 po
->origdev
= !!val
;
3183 case PACKET_VNET_HDR
:
3187 if (sock
->type
!= SOCK_RAW
)
3189 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
3191 if (optlen
< sizeof(val
))
3193 if (copy_from_user(&val
, optval
, sizeof(val
)))
3196 po
->has_vnet_hdr
= !!val
;
3199 case PACKET_TIMESTAMP
:
3203 if (optlen
!= sizeof(val
))
3205 if (copy_from_user(&val
, optval
, sizeof(val
)))
3208 po
->tp_tstamp
= val
;
3215 if (optlen
!= sizeof(val
))
3217 if (copy_from_user(&val
, optval
, sizeof(val
)))
3220 return fanout_add(sk
, val
& 0xffff, val
>> 16);
3223 return -ENOPROTOOPT
;
3227 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
3228 char __user
*optval
, int __user
*optlen
)
3232 struct sock
*sk
= sock
->sk
;
3233 struct packet_sock
*po
= pkt_sk(sk
);
3235 struct tpacket_stats st
;
3236 union tpacket_stats_u st_u
;
3238 if (level
!= SOL_PACKET
)
3239 return -ENOPROTOOPT
;
3241 if (get_user(len
, optlen
))
3248 case PACKET_STATISTICS
:
3249 if (po
->tp_version
== TPACKET_V3
) {
3250 len
= sizeof(struct tpacket_stats_v3
);
3252 if (len
> sizeof(struct tpacket_stats
))
3253 len
= sizeof(struct tpacket_stats
);
3255 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
3256 if (po
->tp_version
== TPACKET_V3
) {
3257 memcpy(&st_u
.stats3
, &po
->stats
,
3258 sizeof(struct tpacket_stats
));
3259 st_u
.stats3
.tp_freeze_q_cnt
=
3260 po
->stats_u
.stats3
.tp_freeze_q_cnt
;
3261 st_u
.stats3
.tp_packets
+= po
->stats
.tp_drops
;
3262 data
= &st_u
.stats3
;
3265 st
.tp_packets
+= st
.tp_drops
;
3268 memset(&po
->stats
, 0, sizeof(st
));
3269 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
3271 case PACKET_AUXDATA
:
3272 if (len
> sizeof(int))
3278 case PACKET_ORIGDEV
:
3279 if (len
> sizeof(int))
3285 case PACKET_VNET_HDR
:
3286 if (len
> sizeof(int))
3288 val
= po
->has_vnet_hdr
;
3292 case PACKET_VERSION
:
3293 if (len
> sizeof(int))
3295 val
= po
->tp_version
;
3299 if (len
> sizeof(int))
3301 if (copy_from_user(&val
, optval
, len
))
3305 val
= sizeof(struct tpacket_hdr
);
3308 val
= sizeof(struct tpacket2_hdr
);
3311 val
= sizeof(struct tpacket3_hdr
);
3318 case PACKET_RESERVE
:
3319 if (len
> sizeof(unsigned int))
3320 len
= sizeof(unsigned int);
3321 val
= po
->tp_reserve
;
3325 if (len
> sizeof(unsigned int))
3326 len
= sizeof(unsigned int);
3330 case PACKET_TIMESTAMP
:
3331 if (len
> sizeof(int))
3333 val
= po
->tp_tstamp
;
3337 if (len
> sizeof(int))
3340 ((u32
)po
->fanout
->id
|
3341 ((u32
)po
->fanout
->type
<< 16)) :
3346 return -ENOPROTOOPT
;
3349 if (put_user(len
, optlen
))
3351 if (copy_to_user(optval
, data
, len
))
3357 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
3360 struct hlist_node
*node
;
3361 struct net_device
*dev
= data
;
3362 struct net
*net
= dev_net(dev
);
3365 sk_for_each_rcu(sk
, node
, &net
->packet
.sklist
) {
3366 struct packet_sock
*po
= pkt_sk(sk
);
3369 case NETDEV_UNREGISTER
:
3371 packet_dev_mclist(dev
, po
->mclist
, -1);
3375 if (dev
->ifindex
== po
->ifindex
) {
3376 spin_lock(&po
->bind_lock
);
3378 __unregister_prot_hook(sk
, false);
3379 sk
->sk_err
= ENETDOWN
;
3380 if (!sock_flag(sk
, SOCK_DEAD
))
3381 sk
->sk_error_report(sk
);
3383 if (msg
== NETDEV_UNREGISTER
) {
3385 if (po
->prot_hook
.dev
)
3386 dev_put(po
->prot_hook
.dev
);
3387 po
->prot_hook
.dev
= NULL
;
3389 spin_unlock(&po
->bind_lock
);
3393 if (dev
->ifindex
== po
->ifindex
) {
3394 spin_lock(&po
->bind_lock
);
3396 register_prot_hook(sk
);
3397 spin_unlock(&po
->bind_lock
);
3407 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
3410 struct sock
*sk
= sock
->sk
;
3415 int amount
= sk_wmem_alloc_get(sk
);
3417 return put_user(amount
, (int __user
*)arg
);
3421 struct sk_buff
*skb
;
3424 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
3425 skb
= skb_peek(&sk
->sk_receive_queue
);
3428 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
3429 return put_user(amount
, (int __user
*)arg
);
3432 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
3434 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
3444 case SIOCGIFBRDADDR
:
3445 case SIOCSIFBRDADDR
:
3446 case SIOCGIFNETMASK
:
3447 case SIOCSIFNETMASK
:
3448 case SIOCGIFDSTADDR
:
3449 case SIOCSIFDSTADDR
:
3451 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
3455 return -ENOIOCTLCMD
;
3460 static unsigned int packet_poll(struct file
*file
, struct socket
*sock
,
3463 struct sock
*sk
= sock
->sk
;
3464 struct packet_sock
*po
= pkt_sk(sk
);
3465 unsigned int mask
= datagram_poll(file
, sock
, wait
);
3467 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
3468 if (po
->rx_ring
.pg_vec
) {
3469 if (!packet_previous_rx_frame(po
, &po
->rx_ring
,
3471 mask
|= POLLIN
| POLLRDNORM
;
3473 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
3474 spin_lock_bh(&sk
->sk_write_queue
.lock
);
3475 if (po
->tx_ring
.pg_vec
) {
3476 if (packet_current_frame(po
, &po
->tx_ring
, TP_STATUS_AVAILABLE
))
3477 mask
|= POLLOUT
| POLLWRNORM
;
3479 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
3484 /* Dirty? Well, I still did not learn better way to account
3488 static void packet_mm_open(struct vm_area_struct
*vma
)
3490 struct file
*file
= vma
->vm_file
;
3491 struct socket
*sock
= file
->private_data
;
3492 struct sock
*sk
= sock
->sk
;
3495 atomic_inc(&pkt_sk(sk
)->mapped
);
3498 static void packet_mm_close(struct vm_area_struct
*vma
)
3500 struct file
*file
= vma
->vm_file
;
3501 struct socket
*sock
= file
->private_data
;
3502 struct sock
*sk
= sock
->sk
;
3505 atomic_dec(&pkt_sk(sk
)->mapped
);
3508 static const struct vm_operations_struct packet_mmap_ops
= {
3509 .open
= packet_mm_open
,
3510 .close
= packet_mm_close
,
3513 static void free_pg_vec(struct pgv
*pg_vec
, unsigned int order
,
3518 for (i
= 0; i
< len
; i
++) {
3519 if (likely(pg_vec
[i
].buffer
)) {
3520 if (is_vmalloc_addr(pg_vec
[i
].buffer
))
3521 vfree(pg_vec
[i
].buffer
);
3523 free_pages((unsigned long)pg_vec
[i
].buffer
,
3525 pg_vec
[i
].buffer
= NULL
;
3531 static inline char *alloc_one_pg_vec_page(unsigned long order
)
3533 char *buffer
= NULL
;
3534 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
|
3535 __GFP_ZERO
| __GFP_NOWARN
| __GFP_NORETRY
;
3537 buffer
= (char *) __get_free_pages(gfp_flags
, order
);
3543 * __get_free_pages failed, fall back to vmalloc
3545 buffer
= vzalloc((1 << order
) * PAGE_SIZE
);
3551 * vmalloc failed, lets dig into swap here
3553 gfp_flags
&= ~__GFP_NORETRY
;
3554 buffer
= (char *)__get_free_pages(gfp_flags
, order
);
3559 * complete and utter failure
3564 static struct pgv
*alloc_pg_vec(struct tpacket_req
*req
, int order
)
3566 unsigned int block_nr
= req
->tp_block_nr
;
3570 pg_vec
= kcalloc(block_nr
, sizeof(struct pgv
), GFP_KERNEL
);
3571 if (unlikely(!pg_vec
))
3574 for (i
= 0; i
< block_nr
; i
++) {
3575 pg_vec
[i
].buffer
= alloc_one_pg_vec_page(order
);
3576 if (unlikely(!pg_vec
[i
].buffer
))
3577 goto out_free_pgvec
;
3584 free_pg_vec(pg_vec
, order
, block_nr
);
3589 static int packet_set_ring(struct sock
*sk
, union tpacket_req_u
*req_u
,
3590 int closing
, int tx_ring
)
3592 struct pgv
*pg_vec
= NULL
;
3593 struct packet_sock
*po
= pkt_sk(sk
);
3594 int was_running
, order
= 0;
3595 struct packet_ring_buffer
*rb
;
3596 struct sk_buff_head
*rb_queue
;
3599 /* Added to avoid minimal code churn */
3600 struct tpacket_req
*req
= &req_u
->req
;
3602 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
3603 if (!closing
&& tx_ring
&& (po
->tp_version
> TPACKET_V2
)) {
3604 WARN(1, "Tx-ring is not supported.\n");
3608 rb
= tx_ring
? &po
->tx_ring
: &po
->rx_ring
;
3609 rb_queue
= tx_ring
? &sk
->sk_write_queue
: &sk
->sk_receive_queue
;
3613 if (atomic_read(&po
->mapped
))
3615 if (atomic_read(&rb
->pending
))
3619 if (req
->tp_block_nr
) {
3620 /* Sanity tests and some calculations */
3622 if (unlikely(rb
->pg_vec
))
3625 switch (po
->tp_version
) {
3627 po
->tp_hdrlen
= TPACKET_HDRLEN
;
3630 po
->tp_hdrlen
= TPACKET2_HDRLEN
;
3633 po
->tp_hdrlen
= TPACKET3_HDRLEN
;
3638 if (unlikely((int)req
->tp_block_size
<= 0))
3640 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
3642 if (unlikely(req
->tp_frame_size
< po
->tp_hdrlen
+
3645 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
3648 rb
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
3649 if (unlikely(rb
->frames_per_block
<= 0))
3651 if (unlikely((rb
->frames_per_block
* req
->tp_block_nr
) !=
3656 order
= get_order(req
->tp_block_size
);
3657 pg_vec
= alloc_pg_vec(req
, order
);
3658 if (unlikely(!pg_vec
))
3660 switch (po
->tp_version
) {
3662 /* Transmit path is not supported. We checked
3663 * it above but just being paranoid
3666 init_prb_bdqc(po
, rb
, pg_vec
, req_u
, tx_ring
);
3675 if (unlikely(req
->tp_frame_nr
))
3681 /* Detach socket from network */
3682 spin_lock(&po
->bind_lock
);
3683 was_running
= po
->running
;
3687 __unregister_prot_hook(sk
, false);
3689 spin_unlock(&po
->bind_lock
);
3694 mutex_lock(&po
->pg_vec_lock
);
3695 if (closing
|| atomic_read(&po
->mapped
) == 0) {
3697 spin_lock_bh(&rb_queue
->lock
);
3698 swap(rb
->pg_vec
, pg_vec
);
3699 rb
->frame_max
= (req
->tp_frame_nr
- 1);
3701 rb
->frame_size
= req
->tp_frame_size
;
3702 spin_unlock_bh(&rb_queue
->lock
);
3704 swap(rb
->pg_vec_order
, order
);
3705 swap(rb
->pg_vec_len
, req
->tp_block_nr
);
3707 rb
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
3708 po
->prot_hook
.func
= (po
->rx_ring
.pg_vec
) ?
3709 tpacket_rcv
: packet_rcv
;
3710 skb_queue_purge(rb_queue
);
3711 if (atomic_read(&po
->mapped
))
3712 pr_err("packet_mmap: vma is busy: %d\n",
3713 atomic_read(&po
->mapped
));
3715 mutex_unlock(&po
->pg_vec_lock
);
3717 spin_lock(&po
->bind_lock
);
3720 register_prot_hook(sk
);
3722 spin_unlock(&po
->bind_lock
);
3723 if (closing
&& (po
->tp_version
> TPACKET_V2
)) {
3724 /* Because we don't support block-based V3 on tx-ring */
3726 prb_shutdown_retire_blk_timer(po
, tx_ring
, rb_queue
);
3731 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
3736 static int packet_mmap(struct file
*file
, struct socket
*sock
,
3737 struct vm_area_struct
*vma
)
3739 struct sock
*sk
= sock
->sk
;
3740 struct packet_sock
*po
= pkt_sk(sk
);
3741 unsigned long size
, expected_size
;
3742 struct packet_ring_buffer
*rb
;
3743 unsigned long start
;
3750 mutex_lock(&po
->pg_vec_lock
);
3753 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
3755 expected_size
+= rb
->pg_vec_len
3761 if (expected_size
== 0)
3764 size
= vma
->vm_end
- vma
->vm_start
;
3765 if (size
!= expected_size
)
3768 start
= vma
->vm_start
;
3769 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
3770 if (rb
->pg_vec
== NULL
)
3773 for (i
= 0; i
< rb
->pg_vec_len
; i
++) {
3775 void *kaddr
= rb
->pg_vec
[i
].buffer
;
3778 for (pg_num
= 0; pg_num
< rb
->pg_vec_pages
; pg_num
++) {
3779 page
= pgv_to_page(kaddr
);
3780 err
= vm_insert_page(vma
, start
, page
);
3789 atomic_inc(&po
->mapped
);
3790 vma
->vm_ops
= &packet_mmap_ops
;
3794 mutex_unlock(&po
->pg_vec_lock
);
3798 static const struct proto_ops packet_ops_spkt
= {
3799 .family
= PF_PACKET
,
3800 .owner
= THIS_MODULE
,
3801 .release
= packet_release
,
3802 .bind
= packet_bind_spkt
,
3803 .connect
= sock_no_connect
,
3804 .socketpair
= sock_no_socketpair
,
3805 .accept
= sock_no_accept
,
3806 .getname
= packet_getname_spkt
,
3807 .poll
= datagram_poll
,
3808 .ioctl
= packet_ioctl
,
3809 .listen
= sock_no_listen
,
3810 .shutdown
= sock_no_shutdown
,
3811 .setsockopt
= sock_no_setsockopt
,
3812 .getsockopt
= sock_no_getsockopt
,
3813 .sendmsg
= packet_sendmsg_spkt
,
3814 .recvmsg
= packet_recvmsg
,
3815 .mmap
= sock_no_mmap
,
3816 .sendpage
= sock_no_sendpage
,
3819 static const struct proto_ops packet_ops
= {
3820 .family
= PF_PACKET
,
3821 .owner
= THIS_MODULE
,
3822 .release
= packet_release
,
3823 .bind
= packet_bind
,
3824 .connect
= sock_no_connect
,
3825 .socketpair
= sock_no_socketpair
,
3826 .accept
= sock_no_accept
,
3827 .getname
= packet_getname
,
3828 .poll
= packet_poll
,
3829 .ioctl
= packet_ioctl
,
3830 .listen
= sock_no_listen
,
3831 .shutdown
= sock_no_shutdown
,
3832 .setsockopt
= packet_setsockopt
,
3833 .getsockopt
= packet_getsockopt
,
3834 .sendmsg
= packet_sendmsg
,
3835 .recvmsg
= packet_recvmsg
,
3836 .mmap
= packet_mmap
,
3837 .sendpage
= sock_no_sendpage
,
3840 static const struct net_proto_family packet_family_ops
= {
3841 .family
= PF_PACKET
,
3842 .create
= packet_create
,
3843 .owner
= THIS_MODULE
,
3846 static struct notifier_block packet_netdev_notifier
= {
3847 .notifier_call
= packet_notifier
,
3850 #ifdef CONFIG_PROC_FS
3852 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3855 struct net
*net
= seq_file_net(seq
);
3858 return seq_hlist_start_head_rcu(&net
->packet
.sklist
, *pos
);
3861 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3863 struct net
*net
= seq_file_net(seq
);
3864 return seq_hlist_next_rcu(v
, &net
->packet
.sklist
, pos
);
3867 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
3873 static int packet_seq_show(struct seq_file
*seq
, void *v
)
3875 if (v
== SEQ_START_TOKEN
)
3876 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3878 struct sock
*s
= sk_entry(v
);
3879 const struct packet_sock
*po
= pkt_sk(s
);
3882 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
3884 atomic_read(&s
->sk_refcnt
),
3889 atomic_read(&s
->sk_rmem_alloc
),
3897 static const struct seq_operations packet_seq_ops
= {
3898 .start
= packet_seq_start
,
3899 .next
= packet_seq_next
,
3900 .stop
= packet_seq_stop
,
3901 .show
= packet_seq_show
,
3904 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
3906 return seq_open_net(inode
, file
, &packet_seq_ops
,
3907 sizeof(struct seq_net_private
));
3910 static const struct file_operations packet_seq_fops
= {
3911 .owner
= THIS_MODULE
,
3912 .open
= packet_seq_open
,
3914 .llseek
= seq_lseek
,
3915 .release
= seq_release_net
,
3920 static int __net_init
packet_net_init(struct net
*net
)
3922 spin_lock_init(&net
->packet
.sklist_lock
);
3923 INIT_HLIST_HEAD(&net
->packet
.sklist
);
3925 if (!proc_net_fops_create(net
, "packet", 0, &packet_seq_fops
))
3931 static void __net_exit
packet_net_exit(struct net
*net
)
3933 proc_net_remove(net
, "packet");
3936 static struct pernet_operations packet_net_ops
= {
3937 .init
= packet_net_init
,
3938 .exit
= packet_net_exit
,
3942 static void __exit
packet_exit(void)
3944 unregister_netdevice_notifier(&packet_netdev_notifier
);
3945 unregister_pernet_subsys(&packet_net_ops
);
3946 sock_unregister(PF_PACKET
);
3947 proto_unregister(&packet_proto
);
3950 static int __init
packet_init(void)
3952 int rc
= proto_register(&packet_proto
, 0);
3957 sock_register(&packet_family_ops
);
3958 register_pernet_subsys(&packet_net_ops
);
3959 register_netdevice_notifier(&packet_netdev_notifier
);
3964 module_init(packet_init
);
3965 module_exit(packet_exit
);
3966 MODULE_LICENSE("GPL");
3967 MODULE_ALIAS_NETPROTO(PF_PACKET
);