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.
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
51 #include <linux/types.h>
53 #include <linux/capability.h>
54 #include <linux/fcntl.h>
55 #include <linux/socket.h>
57 #include <linux/inet.h>
58 #include <linux/netdevice.h>
59 #include <linux/if_packet.h>
60 #include <linux/wireless.h>
61 #include <linux/kernel.h>
62 #include <linux/kmod.h>
63 #include <linux/slab.h>
64 #include <linux/vmalloc.h>
65 #include <net/net_namespace.h>
67 #include <net/protocol.h>
68 #include <linux/skbuff.h>
70 #include <linux/errno.h>
71 #include <linux/timer.h>
72 #include <asm/system.h>
73 #include <asm/uaccess.h>
74 #include <asm/ioctls.h>
76 #include <asm/cacheflush.h>
78 #include <linux/proc_fs.h>
79 #include <linux/seq_file.h>
80 #include <linux/poll.h>
81 #include <linux/module.h>
82 #include <linux/init.h>
83 #include <linux/mutex.h>
84 #include <linux/if_vlan.h>
85 #include <linux/virtio_net.h>
86 #include <linux/errqueue.h>
87 #include <linux/net_tstamp.h>
90 #include <net/inet_common.h>
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
107 Incoming, dev->hard_header!=NULL
108 mac_header -> ll header
111 Outgoing, dev->hard_header!=NULL
112 mac_header -> ll header
115 Incoming, dev->hard_header==NULL
116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
118 assymetry between rx and tx paths.
121 Outgoing, dev->hard_header==NULL
122 mac_header -> data. ll header is still not built!
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
132 dev->hard_header != NULL
133 mac_header -> ll header
136 dev->hard_header == NULL (ll header is added by device, we cannot control it)
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
144 /* Private packet socket structures. */
146 struct packet_mclist
{
147 struct packet_mclist
*next
;
152 unsigned char addr
[MAX_ADDR_LEN
];
154 /* identical to struct packet_mreq except it has
155 * a longer address field.
157 struct packet_mreq_max
{
159 unsigned short mr_type
;
160 unsigned short mr_alen
;
161 unsigned char mr_address
[MAX_ADDR_LEN
];
164 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
165 int closing
, int tx_ring
);
171 struct packet_ring_buffer
{
174 unsigned int frames_per_block
;
175 unsigned int frame_size
;
176 unsigned int frame_max
;
178 unsigned int pg_vec_order
;
179 unsigned int pg_vec_pages
;
180 unsigned int pg_vec_len
;
186 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
);
188 static void packet_flush_mclist(struct sock
*sk
);
191 /* struct sock has to be the first member of packet_sock */
193 struct tpacket_stats stats
;
194 struct packet_ring_buffer rx_ring
;
195 struct packet_ring_buffer tx_ring
;
197 spinlock_t bind_lock
;
198 struct mutex pg_vec_lock
;
199 unsigned int running
:1, /* prot_hook is attached*/
203 int ifindex
; /* bound device */
205 struct packet_mclist
*mclist
;
207 enum tpacket_versions tp_version
;
208 unsigned int tp_hdrlen
;
209 unsigned int tp_reserve
;
210 unsigned int tp_loss
:1;
211 unsigned int tp_tstamp
;
212 struct packet_type prot_hook ____cacheline_aligned_in_smp
;
215 struct packet_skb_cb
{
216 unsigned int origlen
;
218 struct sockaddr_pkt pkt
;
219 struct sockaddr_ll ll
;
223 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
225 static inline __pure
struct page
*pgv_to_page(void *addr
)
227 if (is_vmalloc_addr(addr
))
228 return vmalloc_to_page(addr
);
229 return virt_to_page(addr
);
232 static void __packet_set_status(struct packet_sock
*po
, void *frame
, int status
)
235 struct tpacket_hdr
*h1
;
236 struct tpacket2_hdr
*h2
;
241 switch (po
->tp_version
) {
243 h
.h1
->tp_status
= status
;
244 flush_dcache_page(pgv_to_page(&h
.h1
->tp_status
));
247 h
.h2
->tp_status
= status
;
248 flush_dcache_page(pgv_to_page(&h
.h2
->tp_status
));
251 pr_err("TPACKET version not supported\n");
258 static int __packet_get_status(struct packet_sock
*po
, void *frame
)
261 struct tpacket_hdr
*h1
;
262 struct tpacket2_hdr
*h2
;
269 switch (po
->tp_version
) {
271 flush_dcache_page(pgv_to_page(&h
.h1
->tp_status
));
272 return h
.h1
->tp_status
;
274 flush_dcache_page(pgv_to_page(&h
.h2
->tp_status
));
275 return h
.h2
->tp_status
;
277 pr_err("TPACKET version not supported\n");
283 static void *packet_lookup_frame(struct packet_sock
*po
,
284 struct packet_ring_buffer
*rb
,
285 unsigned int position
,
288 unsigned int pg_vec_pos
, frame_offset
;
290 struct tpacket_hdr
*h1
;
291 struct tpacket2_hdr
*h2
;
295 pg_vec_pos
= position
/ rb
->frames_per_block
;
296 frame_offset
= position
% rb
->frames_per_block
;
298 h
.raw
= rb
->pg_vec
[pg_vec_pos
].buffer
+
299 (frame_offset
* rb
->frame_size
);
301 if (status
!= __packet_get_status(po
, h
.raw
))
307 static inline void *packet_current_frame(struct packet_sock
*po
,
308 struct packet_ring_buffer
*rb
,
311 return packet_lookup_frame(po
, rb
, rb
->head
, status
);
314 static inline void *packet_previous_frame(struct packet_sock
*po
,
315 struct packet_ring_buffer
*rb
,
318 unsigned int previous
= rb
->head
? rb
->head
- 1 : rb
->frame_max
;
319 return packet_lookup_frame(po
, rb
, previous
, status
);
322 static inline void packet_increment_head(struct packet_ring_buffer
*buff
)
324 buff
->head
= buff
->head
!= buff
->frame_max
? buff
->head
+1 : 0;
327 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
329 return (struct packet_sock
*)sk
;
332 static void packet_sock_destruct(struct sock
*sk
)
334 skb_queue_purge(&sk
->sk_error_queue
);
336 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
337 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
339 if (!sock_flag(sk
, SOCK_DEAD
)) {
340 pr_err("Attempt to release alive packet socket: %p\n", sk
);
344 sk_refcnt_debug_dec(sk
);
348 static const struct proto_ops packet_ops
;
350 static const struct proto_ops packet_ops_spkt
;
352 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
,
353 struct packet_type
*pt
, struct net_device
*orig_dev
)
356 struct sockaddr_pkt
*spkt
;
359 * When we registered the protocol we saved the socket in the data
360 * field for just this event.
363 sk
= pt
->af_packet_priv
;
366 * Yank back the headers [hope the device set this
367 * right or kerboom...]
369 * Incoming packets have ll header pulled,
372 * For outgoing ones skb->data == skb_mac_header(skb)
373 * so that this procedure is noop.
376 if (skb
->pkt_type
== PACKET_LOOPBACK
)
379 if (!net_eq(dev_net(dev
), sock_net(sk
)))
382 skb
= skb_share_check(skb
, GFP_ATOMIC
);
386 /* drop any routing info */
389 /* drop conntrack reference */
392 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
394 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
397 * The SOCK_PACKET socket receives _all_ frames.
400 spkt
->spkt_family
= dev
->type
;
401 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
402 spkt
->spkt_protocol
= skb
->protocol
;
405 * Charge the memory to the socket. This is done specifically
406 * to prevent sockets using all the memory up.
409 if (sock_queue_rcv_skb(sk
, skb
) == 0)
420 * Output a raw packet to a device layer. This bypasses all the other
421 * protocol layers and you must therefore supply it with a complete frame
424 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
425 struct msghdr
*msg
, size_t len
)
427 struct sock
*sk
= sock
->sk
;
428 struct sockaddr_pkt
*saddr
= (struct sockaddr_pkt
*)msg
->msg_name
;
429 struct sk_buff
*skb
= NULL
;
430 struct net_device
*dev
;
435 * Get and verify the address.
439 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
441 if (msg
->msg_namelen
== sizeof(struct sockaddr_pkt
))
442 proto
= saddr
->spkt_protocol
;
444 return -ENOTCONN
; /* SOCK_PACKET must be sent giving an address */
447 * Find the device first to size check it
450 saddr
->spkt_device
[13] = 0;
453 dev
= dev_get_by_name_rcu(sock_net(sk
), saddr
->spkt_device
);
459 if (!(dev
->flags
& IFF_UP
))
463 * You may not queue a frame bigger than the mtu. This is the lowest level
464 * raw protocol and you must do your own fragmentation at this level.
468 if (len
> dev
->mtu
+ dev
->hard_header_len
+ VLAN_HLEN
)
472 size_t reserved
= LL_RESERVED_SPACE(dev
);
473 unsigned int hhlen
= dev
->header_ops
? dev
->hard_header_len
: 0;
476 skb
= sock_wmalloc(sk
, len
+ reserved
, 0, GFP_KERNEL
);
479 /* FIXME: Save some space for broken drivers that write a hard
480 * header at transmission time by themselves. PPP is the notable
481 * one here. This should really be fixed at the driver level.
483 skb_reserve(skb
, reserved
);
484 skb_reset_network_header(skb
);
486 /* Try to align data part correctly */
491 skb_reset_network_header(skb
);
493 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
499 if (len
> (dev
->mtu
+ dev
->hard_header_len
)) {
500 /* Earlier code assumed this would be a VLAN pkt,
501 * double-check this now that we have the actual
505 skb_reset_mac_header(skb
);
507 if (ehdr
->h_proto
!= htons(ETH_P_8021Q
)) {
513 skb
->protocol
= proto
;
515 skb
->priority
= sk
->sk_priority
;
516 skb
->mark
= sk
->sk_mark
;
517 err
= sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
532 static inline unsigned int run_filter(const struct sk_buff
*skb
,
533 const struct sock
*sk
,
536 struct sk_filter
*filter
;
539 filter
= rcu_dereference(sk
->sk_filter
);
541 res
= SK_RUN_FILTER(filter
, skb
);
548 * This function makes lazy skb cloning in hope that most of packets
549 * are discarded by BPF.
551 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
552 * and skb->cb are mangled. It works because (and until) packets
553 * falling here are owned by current CPU. Output packets are cloned
554 * by dev_queue_xmit_nit(), input packets are processed by net_bh
555 * sequencially, so that if we return skb to original state on exit,
556 * we will not harm anyone.
559 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
560 struct packet_type
*pt
, struct net_device
*orig_dev
)
563 struct sockaddr_ll
*sll
;
564 struct packet_sock
*po
;
565 u8
*skb_head
= skb
->data
;
566 int skb_len
= skb
->len
;
567 unsigned int snaplen
, res
;
569 if (skb
->pkt_type
== PACKET_LOOPBACK
)
572 sk
= pt
->af_packet_priv
;
575 if (!net_eq(dev_net(dev
), sock_net(sk
)))
580 if (dev
->header_ops
) {
581 /* The device has an explicit notion of ll header,
582 * exported to higher levels.
584 * Otherwise, the device hides details of its frame
585 * structure, so that corresponding packet head is
586 * never delivered to user.
588 if (sk
->sk_type
!= SOCK_DGRAM
)
589 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
590 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
591 /* Special case: outgoing packets have ll header at head */
592 skb_pull(skb
, skb_network_offset(skb
));
598 res
= run_filter(skb
, sk
, snaplen
);
604 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
605 (unsigned)sk
->sk_rcvbuf
)
608 if (skb_shared(skb
)) {
609 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
613 if (skb_head
!= skb
->data
) {
614 skb
->data
= skb_head
;
621 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
624 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
625 sll
->sll_family
= AF_PACKET
;
626 sll
->sll_hatype
= dev
->type
;
627 sll
->sll_protocol
= skb
->protocol
;
628 sll
->sll_pkttype
= skb
->pkt_type
;
629 if (unlikely(po
->origdev
))
630 sll
->sll_ifindex
= orig_dev
->ifindex
;
632 sll
->sll_ifindex
= dev
->ifindex
;
634 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
636 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
638 if (pskb_trim(skb
, snaplen
))
641 skb_set_owner_r(skb
, sk
);
645 /* drop conntrack reference */
648 spin_lock(&sk
->sk_receive_queue
.lock
);
649 po
->stats
.tp_packets
++;
650 skb
->dropcount
= atomic_read(&sk
->sk_drops
);
651 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
652 spin_unlock(&sk
->sk_receive_queue
.lock
);
653 sk
->sk_data_ready(sk
, skb
->len
);
657 spin_lock(&sk
->sk_receive_queue
.lock
);
658 po
->stats
.tp_drops
++;
659 atomic_inc(&sk
->sk_drops
);
660 spin_unlock(&sk
->sk_receive_queue
.lock
);
663 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
664 skb
->data
= skb_head
;
672 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
673 struct packet_type
*pt
, struct net_device
*orig_dev
)
676 struct packet_sock
*po
;
677 struct sockaddr_ll
*sll
;
679 struct tpacket_hdr
*h1
;
680 struct tpacket2_hdr
*h2
;
683 u8
*skb_head
= skb
->data
;
684 int skb_len
= skb
->len
;
685 unsigned int snaplen
, res
;
686 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
687 unsigned short macoff
, netoff
, hdrlen
;
688 struct sk_buff
*copy_skb
= NULL
;
691 struct skb_shared_hwtstamps
*shhwtstamps
= skb_hwtstamps(skb
);
693 if (skb
->pkt_type
== PACKET_LOOPBACK
)
696 sk
= pt
->af_packet_priv
;
699 if (!net_eq(dev_net(dev
), sock_net(sk
)))
702 if (dev
->header_ops
) {
703 if (sk
->sk_type
!= SOCK_DGRAM
)
704 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
705 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
706 /* Special case: outgoing packets have ll header at head */
707 skb_pull(skb
, skb_network_offset(skb
));
711 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
712 status
|= TP_STATUS_CSUMNOTREADY
;
716 res
= run_filter(skb
, sk
, snaplen
);
722 if (sk
->sk_type
== SOCK_DGRAM
) {
723 macoff
= netoff
= TPACKET_ALIGN(po
->tp_hdrlen
) + 16 +
726 unsigned maclen
= skb_network_offset(skb
);
727 netoff
= TPACKET_ALIGN(po
->tp_hdrlen
+
728 (maclen
< 16 ? 16 : maclen
)) +
730 macoff
= netoff
- maclen
;
733 if (macoff
+ snaplen
> po
->rx_ring
.frame_size
) {
734 if (po
->copy_thresh
&&
735 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
736 (unsigned)sk
->sk_rcvbuf
) {
737 if (skb_shared(skb
)) {
738 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
740 copy_skb
= skb_get(skb
);
741 skb_head
= skb
->data
;
744 skb_set_owner_r(copy_skb
, sk
);
746 snaplen
= po
->rx_ring
.frame_size
- macoff
;
747 if ((int)snaplen
< 0)
751 spin_lock(&sk
->sk_receive_queue
.lock
);
752 h
.raw
= packet_current_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
);
755 packet_increment_head(&po
->rx_ring
);
756 po
->stats
.tp_packets
++;
758 status
|= TP_STATUS_COPY
;
759 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
761 if (!po
->stats
.tp_drops
)
762 status
&= ~TP_STATUS_LOSING
;
763 spin_unlock(&sk
->sk_receive_queue
.lock
);
765 skb_copy_bits(skb
, 0, h
.raw
+ macoff
, snaplen
);
767 switch (po
->tp_version
) {
769 h
.h1
->tp_len
= skb
->len
;
770 h
.h1
->tp_snaplen
= snaplen
;
771 h
.h1
->tp_mac
= macoff
;
772 h
.h1
->tp_net
= netoff
;
773 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
774 && shhwtstamps
->syststamp
.tv64
)
775 tv
= ktime_to_timeval(shhwtstamps
->syststamp
);
776 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
777 && shhwtstamps
->hwtstamp
.tv64
)
778 tv
= ktime_to_timeval(shhwtstamps
->hwtstamp
);
779 else if (skb
->tstamp
.tv64
)
780 tv
= ktime_to_timeval(skb
->tstamp
);
782 do_gettimeofday(&tv
);
783 h
.h1
->tp_sec
= tv
.tv_sec
;
784 h
.h1
->tp_usec
= tv
.tv_usec
;
785 hdrlen
= sizeof(*h
.h1
);
788 h
.h2
->tp_len
= skb
->len
;
789 h
.h2
->tp_snaplen
= snaplen
;
790 h
.h2
->tp_mac
= macoff
;
791 h
.h2
->tp_net
= netoff
;
792 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
793 && shhwtstamps
->syststamp
.tv64
)
794 ts
= ktime_to_timespec(shhwtstamps
->syststamp
);
795 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
796 && shhwtstamps
->hwtstamp
.tv64
)
797 ts
= ktime_to_timespec(shhwtstamps
->hwtstamp
);
798 else if (skb
->tstamp
.tv64
)
799 ts
= ktime_to_timespec(skb
->tstamp
);
802 h
.h2
->tp_sec
= ts
.tv_sec
;
803 h
.h2
->tp_nsec
= ts
.tv_nsec
;
804 if (vlan_tx_tag_present(skb
)) {
805 h
.h2
->tp_vlan_tci
= vlan_tx_tag_get(skb
);
806 status
|= TP_STATUS_VLAN_VALID
;
808 h
.h2
->tp_vlan_tci
= 0;
810 h
.h2
->tp_padding
= 0;
811 hdrlen
= sizeof(*h
.h2
);
817 sll
= h
.raw
+ TPACKET_ALIGN(hdrlen
);
818 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
819 sll
->sll_family
= AF_PACKET
;
820 sll
->sll_hatype
= dev
->type
;
821 sll
->sll_protocol
= skb
->protocol
;
822 sll
->sll_pkttype
= skb
->pkt_type
;
823 if (unlikely(po
->origdev
))
824 sll
->sll_ifindex
= orig_dev
->ifindex
;
826 sll
->sll_ifindex
= dev
->ifindex
;
828 __packet_set_status(po
, h
.raw
, status
);
830 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
834 end
= (u8
*)PAGE_ALIGN((unsigned long)h
.raw
+ macoff
+ snaplen
);
835 for (start
= h
.raw
; start
< end
; start
+= PAGE_SIZE
)
836 flush_dcache_page(pgv_to_page(start
));
840 sk
->sk_data_ready(sk
, 0);
843 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
844 skb
->data
= skb_head
;
852 po
->stats
.tp_drops
++;
853 spin_unlock(&sk
->sk_receive_queue
.lock
);
855 sk
->sk_data_ready(sk
, 0);
860 static void tpacket_destruct_skb(struct sk_buff
*skb
)
862 struct packet_sock
*po
= pkt_sk(skb
->sk
);
867 if (likely(po
->tx_ring
.pg_vec
)) {
868 ph
= skb_shinfo(skb
)->destructor_arg
;
869 BUG_ON(__packet_get_status(po
, ph
) != TP_STATUS_SENDING
);
870 BUG_ON(atomic_read(&po
->tx_ring
.pending
) == 0);
871 atomic_dec(&po
->tx_ring
.pending
);
872 __packet_set_status(po
, ph
, TP_STATUS_AVAILABLE
);
878 static int tpacket_fill_skb(struct packet_sock
*po
, struct sk_buff
*skb
,
879 void *frame
, struct net_device
*dev
, int size_max
,
880 __be16 proto
, unsigned char *addr
)
883 struct tpacket_hdr
*h1
;
884 struct tpacket2_hdr
*h2
;
887 int to_write
, offset
, len
, tp_len
, nr_frags
, len_max
;
888 struct socket
*sock
= po
->sk
.sk_socket
;
895 skb
->protocol
= proto
;
897 skb
->priority
= po
->sk
.sk_priority
;
898 skb
->mark
= po
->sk
.sk_mark
;
899 skb_shinfo(skb
)->destructor_arg
= ph
.raw
;
901 switch (po
->tp_version
) {
903 tp_len
= ph
.h2
->tp_len
;
906 tp_len
= ph
.h1
->tp_len
;
909 if (unlikely(tp_len
> size_max
)) {
910 pr_err("packet size is too long (%d > %d)\n", tp_len
, size_max
);
914 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
915 skb_reset_network_header(skb
);
917 data
= ph
.raw
+ po
->tp_hdrlen
- sizeof(struct sockaddr_ll
);
920 if (sock
->type
== SOCK_DGRAM
) {
921 err
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
,
923 if (unlikely(err
< 0))
925 } else if (dev
->hard_header_len
) {
926 /* net device doesn't like empty head */
927 if (unlikely(tp_len
<= dev
->hard_header_len
)) {
928 pr_err("packet size is too short (%d < %d)\n",
929 tp_len
, dev
->hard_header_len
);
933 skb_push(skb
, dev
->hard_header_len
);
934 err
= skb_store_bits(skb
, 0, data
,
935 dev
->hard_header_len
);
939 data
+= dev
->hard_header_len
;
940 to_write
-= dev
->hard_header_len
;
944 offset
= offset_in_page(data
);
945 len_max
= PAGE_SIZE
- offset
;
946 len
= ((to_write
> len_max
) ? len_max
: to_write
);
948 skb
->data_len
= to_write
;
949 skb
->len
+= to_write
;
950 skb
->truesize
+= to_write
;
951 atomic_add(to_write
, &po
->sk
.sk_wmem_alloc
);
953 while (likely(to_write
)) {
954 nr_frags
= skb_shinfo(skb
)->nr_frags
;
956 if (unlikely(nr_frags
>= MAX_SKB_FRAGS
)) {
957 pr_err("Packet exceed the number of skb frags(%lu)\n",
962 page
= pgv_to_page(data
);
964 flush_dcache_page(page
);
966 skb_fill_page_desc(skb
, nr_frags
, page
, offset
, len
);
970 len
= ((to_write
> len_max
) ? len_max
: to_write
);
976 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
)
979 struct net_device
*dev
;
981 int ifindex
, err
, reserve
= 0;
983 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
984 int tp_len
, size_max
;
989 mutex_lock(&po
->pg_vec_lock
);
993 ifindex
= po
->ifindex
;
998 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
1000 if (msg
->msg_namelen
< (saddr
->sll_halen
1001 + offsetof(struct sockaddr_ll
,
1004 ifindex
= saddr
->sll_ifindex
;
1005 proto
= saddr
->sll_protocol
;
1006 addr
= saddr
->sll_addr
;
1009 dev
= dev_get_by_index(sock_net(&po
->sk
), ifindex
);
1011 if (unlikely(dev
== NULL
))
1014 reserve
= dev
->hard_header_len
;
1017 if (unlikely(!(dev
->flags
& IFF_UP
)))
1020 size_max
= po
->tx_ring
.frame_size
1021 - (po
->tp_hdrlen
- sizeof(struct sockaddr_ll
));
1023 if (size_max
> dev
->mtu
+ reserve
)
1024 size_max
= dev
->mtu
+ reserve
;
1027 ph
= packet_current_frame(po
, &po
->tx_ring
,
1028 TP_STATUS_SEND_REQUEST
);
1030 if (unlikely(ph
== NULL
)) {
1035 status
= TP_STATUS_SEND_REQUEST
;
1036 skb
= sock_alloc_send_skb(&po
->sk
,
1037 LL_ALLOCATED_SPACE(dev
)
1038 + sizeof(struct sockaddr_ll
),
1041 if (unlikely(skb
== NULL
))
1044 tp_len
= tpacket_fill_skb(po
, skb
, ph
, dev
, size_max
, proto
,
1047 if (unlikely(tp_len
< 0)) {
1049 __packet_set_status(po
, ph
,
1050 TP_STATUS_AVAILABLE
);
1051 packet_increment_head(&po
->tx_ring
);
1055 status
= TP_STATUS_WRONG_FORMAT
;
1061 skb
->destructor
= tpacket_destruct_skb
;
1062 __packet_set_status(po
, ph
, TP_STATUS_SENDING
);
1063 atomic_inc(&po
->tx_ring
.pending
);
1065 status
= TP_STATUS_SEND_REQUEST
;
1066 err
= dev_queue_xmit(skb
);
1067 if (unlikely(err
> 0)) {
1068 err
= net_xmit_errno(err
);
1069 if (err
&& __packet_get_status(po
, ph
) ==
1070 TP_STATUS_AVAILABLE
) {
1071 /* skb was destructed already */
1076 * skb was dropped but not destructed yet;
1077 * let's treat it like congestion or err < 0
1081 packet_increment_head(&po
->tx_ring
);
1083 } while (likely((ph
!= NULL
) ||
1084 ((!(msg
->msg_flags
& MSG_DONTWAIT
)) &&
1085 (atomic_read(&po
->tx_ring
.pending
))))
1092 __packet_set_status(po
, ph
, status
);
1097 mutex_unlock(&po
->pg_vec_lock
);
1101 static inline struct sk_buff
*packet_alloc_skb(struct sock
*sk
, size_t prepad
,
1102 size_t reserve
, size_t len
,
1103 size_t linear
, int noblock
,
1106 struct sk_buff
*skb
;
1108 /* Under a page? Don't bother with paged skb. */
1109 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
1112 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
1117 skb_reserve(skb
, reserve
);
1118 skb_put(skb
, linear
);
1119 skb
->data_len
= len
- linear
;
1120 skb
->len
+= len
- linear
;
1125 static int packet_snd(struct socket
*sock
,
1126 struct msghdr
*msg
, size_t len
)
1128 struct sock
*sk
= sock
->sk
;
1129 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
1130 struct sk_buff
*skb
;
1131 struct net_device
*dev
;
1133 unsigned char *addr
;
1134 int ifindex
, err
, reserve
= 0;
1135 struct virtio_net_hdr vnet_hdr
= { 0 };
1138 struct packet_sock
*po
= pkt_sk(sk
);
1139 unsigned short gso_type
= 0;
1142 * Get and verify the address.
1145 if (saddr
== NULL
) {
1146 ifindex
= po
->ifindex
;
1151 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
1153 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
1155 ifindex
= saddr
->sll_ifindex
;
1156 proto
= saddr
->sll_protocol
;
1157 addr
= saddr
->sll_addr
;
1161 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
1165 if (sock
->type
== SOCK_RAW
)
1166 reserve
= dev
->hard_header_len
;
1169 if (!(dev
->flags
& IFF_UP
))
1172 if (po
->has_vnet_hdr
) {
1173 vnet_hdr_len
= sizeof(vnet_hdr
);
1176 if (len
< vnet_hdr_len
)
1179 len
-= vnet_hdr_len
;
1181 err
= memcpy_fromiovec((void *)&vnet_hdr
, msg
->msg_iov
,
1186 if ((vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
1187 (vnet_hdr
.csum_start
+ vnet_hdr
.csum_offset
+ 2 >
1189 vnet_hdr
.hdr_len
= vnet_hdr
.csum_start
+
1190 vnet_hdr
.csum_offset
+ 2;
1193 if (vnet_hdr
.hdr_len
> len
)
1196 if (vnet_hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
1197 switch (vnet_hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
1198 case VIRTIO_NET_HDR_GSO_TCPV4
:
1199 gso_type
= SKB_GSO_TCPV4
;
1201 case VIRTIO_NET_HDR_GSO_TCPV6
:
1202 gso_type
= SKB_GSO_TCPV6
;
1204 case VIRTIO_NET_HDR_GSO_UDP
:
1205 gso_type
= SKB_GSO_UDP
;
1211 if (vnet_hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
1212 gso_type
|= SKB_GSO_TCP_ECN
;
1214 if (vnet_hdr
.gso_size
== 0)
1221 if (!gso_type
&& (len
> dev
->mtu
+ reserve
+ VLAN_HLEN
))
1225 skb
= packet_alloc_skb(sk
, LL_ALLOCATED_SPACE(dev
),
1226 LL_RESERVED_SPACE(dev
), len
, vnet_hdr
.hdr_len
,
1227 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1231 skb_set_network_header(skb
, reserve
);
1234 if (sock
->type
== SOCK_DGRAM
&&
1235 (offset
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
)) < 0)
1238 /* Returns -EFAULT on error */
1239 err
= skb_copy_datagram_from_iovec(skb
, offset
, msg
->msg_iov
, 0, len
);
1242 err
= sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
1246 if (!gso_type
&& (len
> dev
->mtu
+ reserve
)) {
1247 /* Earlier code assumed this would be a VLAN pkt,
1248 * double-check this now that we have the actual
1251 struct ethhdr
*ehdr
;
1252 skb_reset_mac_header(skb
);
1253 ehdr
= eth_hdr(skb
);
1254 if (ehdr
->h_proto
!= htons(ETH_P_8021Q
)) {
1260 skb
->protocol
= proto
;
1262 skb
->priority
= sk
->sk_priority
;
1263 skb
->mark
= sk
->sk_mark
;
1265 if (po
->has_vnet_hdr
) {
1266 if (vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
1267 if (!skb_partial_csum_set(skb
, vnet_hdr
.csum_start
,
1268 vnet_hdr
.csum_offset
)) {
1274 skb_shinfo(skb
)->gso_size
= vnet_hdr
.gso_size
;
1275 skb_shinfo(skb
)->gso_type
= gso_type
;
1277 /* Header must be checked, and gso_segs computed. */
1278 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
1279 skb_shinfo(skb
)->gso_segs
= 0;
1281 len
+= vnet_hdr_len
;
1288 err
= dev_queue_xmit(skb
);
1289 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
1305 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1306 struct msghdr
*msg
, size_t len
)
1308 struct sock
*sk
= sock
->sk
;
1309 struct packet_sock
*po
= pkt_sk(sk
);
1310 if (po
->tx_ring
.pg_vec
)
1311 return tpacket_snd(po
, msg
);
1313 return packet_snd(sock
, msg
, len
);
1317 * Close a PACKET socket. This is fairly simple. We immediately go
1318 * to 'closed' state and remove our protocol entry in the device list.
1321 static int packet_release(struct socket
*sock
)
1323 struct sock
*sk
= sock
->sk
;
1324 struct packet_sock
*po
;
1326 struct tpacket_req req
;
1334 spin_lock_bh(&net
->packet
.sklist_lock
);
1335 sk_del_node_init_rcu(sk
);
1336 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
1337 spin_unlock_bh(&net
->packet
.sklist_lock
);
1339 spin_lock(&po
->bind_lock
);
1342 * Remove from protocol table
1346 __dev_remove_pack(&po
->prot_hook
);
1349 spin_unlock(&po
->bind_lock
);
1351 packet_flush_mclist(sk
);
1353 memset(&req
, 0, sizeof(req
));
1355 if (po
->rx_ring
.pg_vec
)
1356 packet_set_ring(sk
, &req
, 1, 0);
1358 if (po
->tx_ring
.pg_vec
)
1359 packet_set_ring(sk
, &req
, 1, 1);
1363 * Now the socket is dead. No more input will appear.
1370 skb_queue_purge(&sk
->sk_receive_queue
);
1371 sk_refcnt_debug_release(sk
);
1378 * Attach a packet hook.
1381 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
1383 struct packet_sock
*po
= pkt_sk(sk
);
1385 * Detach an existing hook if present.
1390 spin_lock(&po
->bind_lock
);
1395 spin_unlock(&po
->bind_lock
);
1396 dev_remove_pack(&po
->prot_hook
);
1397 spin_lock(&po
->bind_lock
);
1401 po
->prot_hook
.type
= protocol
;
1402 po
->prot_hook
.dev
= dev
;
1404 po
->ifindex
= dev
? dev
->ifindex
: 0;
1409 if (!dev
|| (dev
->flags
& IFF_UP
)) {
1410 dev_add_pack(&po
->prot_hook
);
1414 sk
->sk_err
= ENETDOWN
;
1415 if (!sock_flag(sk
, SOCK_DEAD
))
1416 sk
->sk_error_report(sk
);
1420 spin_unlock(&po
->bind_lock
);
1426 * Bind a packet socket to a device
1429 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1432 struct sock
*sk
= sock
->sk
;
1434 struct net_device
*dev
;
1441 if (addr_len
!= sizeof(struct sockaddr
))
1443 strlcpy(name
, uaddr
->sa_data
, sizeof(name
));
1445 dev
= dev_get_by_name(sock_net(sk
), name
);
1447 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
1453 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1455 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1456 struct sock
*sk
= sock
->sk
;
1457 struct net_device
*dev
= NULL
;
1465 if (addr_len
< sizeof(struct sockaddr_ll
))
1467 if (sll
->sll_family
!= AF_PACKET
)
1470 if (sll
->sll_ifindex
) {
1472 dev
= dev_get_by_index(sock_net(sk
), sll
->sll_ifindex
);
1476 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
1484 static struct proto packet_proto
= {
1486 .owner
= THIS_MODULE
,
1487 .obj_size
= sizeof(struct packet_sock
),
1491 * Create a packet of type SOCK_PACKET.
1494 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
,
1498 struct packet_sock
*po
;
1499 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1502 if (!capable(CAP_NET_RAW
))
1504 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
1505 sock
->type
!= SOCK_PACKET
)
1506 return -ESOCKTNOSUPPORT
;
1508 sock
->state
= SS_UNCONNECTED
;
1511 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
1515 sock
->ops
= &packet_ops
;
1516 if (sock
->type
== SOCK_PACKET
)
1517 sock
->ops
= &packet_ops_spkt
;
1519 sock_init_data(sock
, sk
);
1522 sk
->sk_family
= PF_PACKET
;
1525 sk
->sk_destruct
= packet_sock_destruct
;
1526 sk_refcnt_debug_inc(sk
);
1529 * Attach a protocol block
1532 spin_lock_init(&po
->bind_lock
);
1533 mutex_init(&po
->pg_vec_lock
);
1534 po
->prot_hook
.func
= packet_rcv
;
1536 if (sock
->type
== SOCK_PACKET
)
1537 po
->prot_hook
.func
= packet_rcv_spkt
;
1539 po
->prot_hook
.af_packet_priv
= sk
;
1542 po
->prot_hook
.type
= proto
;
1543 dev_add_pack(&po
->prot_hook
);
1548 spin_lock_bh(&net
->packet
.sklist_lock
);
1549 sk_add_node_rcu(sk
, &net
->packet
.sklist
);
1550 sock_prot_inuse_add(net
, &packet_proto
, 1);
1551 spin_unlock_bh(&net
->packet
.sklist_lock
);
1558 static int packet_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
1560 struct sock_exterr_skb
*serr
;
1561 struct sk_buff
*skb
, *skb2
;
1565 skb
= skb_dequeue(&sk
->sk_error_queue
);
1571 msg
->msg_flags
|= MSG_TRUNC
;
1574 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1578 sock_recv_timestamp(msg
, sk
, skb
);
1580 serr
= SKB_EXT_ERR(skb
);
1581 put_cmsg(msg
, SOL_PACKET
, PACKET_TX_TIMESTAMP
,
1582 sizeof(serr
->ee
), &serr
->ee
);
1584 msg
->msg_flags
|= MSG_ERRQUEUE
;
1587 /* Reset and regenerate socket error */
1588 spin_lock_bh(&sk
->sk_error_queue
.lock
);
1590 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
1591 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
1592 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
1593 sk
->sk_error_report(sk
);
1595 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
1604 * Pull a packet from our receive queue and hand it to the user.
1605 * If necessary we block.
1608 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1609 struct msghdr
*msg
, size_t len
, int flags
)
1611 struct sock
*sk
= sock
->sk
;
1612 struct sk_buff
*skb
;
1614 struct sockaddr_ll
*sll
;
1615 int vnet_hdr_len
= 0;
1618 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
|MSG_ERRQUEUE
))
1622 /* What error should we return now? EUNATTACH? */
1623 if (pkt_sk(sk
)->ifindex
< 0)
1627 if (flags
& MSG_ERRQUEUE
) {
1628 err
= packet_recv_error(sk
, msg
, len
);
1633 * Call the generic datagram receiver. This handles all sorts
1634 * of horrible races and re-entrancy so we can forget about it
1635 * in the protocol layers.
1637 * Now it will return ENETDOWN, if device have just gone down,
1638 * but then it will block.
1641 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
1644 * An error occurred so return it. Because skb_recv_datagram()
1645 * handles the blocking we don't see and worry about blocking
1652 if (pkt_sk(sk
)->has_vnet_hdr
) {
1653 struct virtio_net_hdr vnet_hdr
= { 0 };
1656 vnet_hdr_len
= sizeof(vnet_hdr
);
1657 if (len
< vnet_hdr_len
)
1660 len
-= vnet_hdr_len
;
1662 if (skb_is_gso(skb
)) {
1663 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
1665 /* This is a hint as to how much should be linear. */
1666 vnet_hdr
.hdr_len
= skb_headlen(skb
);
1667 vnet_hdr
.gso_size
= sinfo
->gso_size
;
1668 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
1669 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1670 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
1671 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1672 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
1673 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
1674 else if (sinfo
->gso_type
& SKB_GSO_FCOE
)
1678 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
1679 vnet_hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1681 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1683 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1684 vnet_hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1685 vnet_hdr
.csum_start
= skb_checksum_start_offset(skb
);
1686 vnet_hdr
.csum_offset
= skb
->csum_offset
;
1687 } /* else everything is zero */
1689 err
= memcpy_toiovec(msg
->msg_iov
, (void *)&vnet_hdr
,
1696 * If the address length field is there to be filled in, we fill
1700 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1701 if (sock
->type
== SOCK_PACKET
)
1702 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1704 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1707 * You lose any data beyond the buffer you gave. If it worries a
1708 * user program they can ask the device for its MTU anyway.
1714 msg
->msg_flags
|= MSG_TRUNC
;
1717 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1721 sock_recv_ts_and_drops(msg
, sk
, skb
);
1724 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1727 if (pkt_sk(sk
)->auxdata
) {
1728 struct tpacket_auxdata aux
;
1730 aux
.tp_status
= TP_STATUS_USER
;
1731 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1732 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1733 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1734 aux
.tp_snaplen
= skb
->len
;
1736 aux
.tp_net
= skb_network_offset(skb
);
1737 if (vlan_tx_tag_present(skb
)) {
1738 aux
.tp_vlan_tci
= vlan_tx_tag_get(skb
);
1739 aux
.tp_status
|= TP_STATUS_VLAN_VALID
;
1741 aux
.tp_vlan_tci
= 0;
1744 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1748 * Free or return the buffer as appropriate. Again this
1749 * hides all the races and re-entrancy issues from us.
1751 err
= vnet_hdr_len
+ ((flags
&MSG_TRUNC
) ? skb
->len
: copied
);
1754 skb_free_datagram(sk
, skb
);
1759 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1760 int *uaddr_len
, int peer
)
1762 struct net_device
*dev
;
1763 struct sock
*sk
= sock
->sk
;
1768 uaddr
->sa_family
= AF_PACKET
;
1770 dev
= dev_get_by_index_rcu(sock_net(sk
), pkt_sk(sk
)->ifindex
);
1772 strncpy(uaddr
->sa_data
, dev
->name
, 14);
1774 memset(uaddr
->sa_data
, 0, 14);
1776 *uaddr_len
= sizeof(*uaddr
);
1781 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1782 int *uaddr_len
, int peer
)
1784 struct net_device
*dev
;
1785 struct sock
*sk
= sock
->sk
;
1786 struct packet_sock
*po
= pkt_sk(sk
);
1787 DECLARE_SOCKADDR(struct sockaddr_ll
*, sll
, uaddr
);
1792 sll
->sll_family
= AF_PACKET
;
1793 sll
->sll_ifindex
= po
->ifindex
;
1794 sll
->sll_protocol
= po
->num
;
1795 sll
->sll_pkttype
= 0;
1797 dev
= dev_get_by_index_rcu(sock_net(sk
), po
->ifindex
);
1799 sll
->sll_hatype
= dev
->type
;
1800 sll
->sll_halen
= dev
->addr_len
;
1801 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1803 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1807 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1812 static int packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
,
1816 case PACKET_MR_MULTICAST
:
1817 if (i
->alen
!= dev
->addr_len
)
1820 return dev_mc_add(dev
, i
->addr
);
1822 return dev_mc_del(dev
, i
->addr
);
1824 case PACKET_MR_PROMISC
:
1825 return dev_set_promiscuity(dev
, what
);
1827 case PACKET_MR_ALLMULTI
:
1828 return dev_set_allmulti(dev
, what
);
1830 case PACKET_MR_UNICAST
:
1831 if (i
->alen
!= dev
->addr_len
)
1834 return dev_uc_add(dev
, i
->addr
);
1836 return dev_uc_del(dev
, i
->addr
);
1844 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1846 for ( ; i
; i
= i
->next
) {
1847 if (i
->ifindex
== dev
->ifindex
)
1848 packet_dev_mc(dev
, i
, what
);
1852 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1854 struct packet_sock
*po
= pkt_sk(sk
);
1855 struct packet_mclist
*ml
, *i
;
1856 struct net_device
*dev
;
1862 dev
= __dev_get_by_index(sock_net(sk
), mreq
->mr_ifindex
);
1867 if (mreq
->mr_alen
> dev
->addr_len
)
1871 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1876 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1877 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1878 ml
->type
== mreq
->mr_type
&&
1879 ml
->alen
== mreq
->mr_alen
&&
1880 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1882 /* Free the new element ... */
1888 i
->type
= mreq
->mr_type
;
1889 i
->ifindex
= mreq
->mr_ifindex
;
1890 i
->alen
= mreq
->mr_alen
;
1891 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1893 i
->next
= po
->mclist
;
1895 err
= packet_dev_mc(dev
, i
, 1);
1897 po
->mclist
= i
->next
;
1906 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1908 struct packet_mclist
*ml
, **mlp
;
1912 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1913 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1914 ml
->type
== mreq
->mr_type
&&
1915 ml
->alen
== mreq
->mr_alen
&&
1916 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1917 if (--ml
->count
== 0) {
1918 struct net_device
*dev
;
1920 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1922 packet_dev_mc(dev
, ml
, -1);
1930 return -EADDRNOTAVAIL
;
1933 static void packet_flush_mclist(struct sock
*sk
)
1935 struct packet_sock
*po
= pkt_sk(sk
);
1936 struct packet_mclist
*ml
;
1942 while ((ml
= po
->mclist
) != NULL
) {
1943 struct net_device
*dev
;
1945 po
->mclist
= ml
->next
;
1946 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1948 packet_dev_mc(dev
, ml
, -1);
1955 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
1957 struct sock
*sk
= sock
->sk
;
1958 struct packet_sock
*po
= pkt_sk(sk
);
1961 if (level
!= SOL_PACKET
)
1962 return -ENOPROTOOPT
;
1965 case PACKET_ADD_MEMBERSHIP
:
1966 case PACKET_DROP_MEMBERSHIP
:
1968 struct packet_mreq_max mreq
;
1970 memset(&mreq
, 0, sizeof(mreq
));
1971 if (len
< sizeof(struct packet_mreq
))
1973 if (len
> sizeof(mreq
))
1975 if (copy_from_user(&mreq
, optval
, len
))
1977 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1979 if (optname
== PACKET_ADD_MEMBERSHIP
)
1980 ret
= packet_mc_add(sk
, &mreq
);
1982 ret
= packet_mc_drop(sk
, &mreq
);
1986 case PACKET_RX_RING
:
1987 case PACKET_TX_RING
:
1989 struct tpacket_req req
;
1991 if (optlen
< sizeof(req
))
1993 if (pkt_sk(sk
)->has_vnet_hdr
)
1995 if (copy_from_user(&req
, optval
, sizeof(req
)))
1997 return packet_set_ring(sk
, &req
, 0, optname
== PACKET_TX_RING
);
1999 case PACKET_COPY_THRESH
:
2003 if (optlen
!= sizeof(val
))
2005 if (copy_from_user(&val
, optval
, sizeof(val
)))
2008 pkt_sk(sk
)->copy_thresh
= val
;
2011 case PACKET_VERSION
:
2015 if (optlen
!= sizeof(val
))
2017 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
2019 if (copy_from_user(&val
, optval
, sizeof(val
)))
2024 po
->tp_version
= val
;
2030 case PACKET_RESERVE
:
2034 if (optlen
!= sizeof(val
))
2036 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
2038 if (copy_from_user(&val
, optval
, sizeof(val
)))
2040 po
->tp_reserve
= val
;
2047 if (optlen
!= sizeof(val
))
2049 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
2051 if (copy_from_user(&val
, optval
, sizeof(val
)))
2053 po
->tp_loss
= !!val
;
2056 case PACKET_AUXDATA
:
2060 if (optlen
< sizeof(val
))
2062 if (copy_from_user(&val
, optval
, sizeof(val
)))
2065 po
->auxdata
= !!val
;
2068 case PACKET_ORIGDEV
:
2072 if (optlen
< sizeof(val
))
2074 if (copy_from_user(&val
, optval
, sizeof(val
)))
2077 po
->origdev
= !!val
;
2080 case PACKET_VNET_HDR
:
2084 if (sock
->type
!= SOCK_RAW
)
2086 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
2088 if (optlen
< sizeof(val
))
2090 if (copy_from_user(&val
, optval
, sizeof(val
)))
2093 po
->has_vnet_hdr
= !!val
;
2096 case PACKET_TIMESTAMP
:
2100 if (optlen
!= sizeof(val
))
2102 if (copy_from_user(&val
, optval
, sizeof(val
)))
2105 po
->tp_tstamp
= val
;
2109 return -ENOPROTOOPT
;
2113 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
2114 char __user
*optval
, int __user
*optlen
)
2118 struct sock
*sk
= sock
->sk
;
2119 struct packet_sock
*po
= pkt_sk(sk
);
2121 struct tpacket_stats st
;
2123 if (level
!= SOL_PACKET
)
2124 return -ENOPROTOOPT
;
2126 if (get_user(len
, optlen
))
2133 case PACKET_STATISTICS
:
2134 if (len
> sizeof(struct tpacket_stats
))
2135 len
= sizeof(struct tpacket_stats
);
2136 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2138 memset(&po
->stats
, 0, sizeof(st
));
2139 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2140 st
.tp_packets
+= st
.tp_drops
;
2144 case PACKET_AUXDATA
:
2145 if (len
> sizeof(int))
2151 case PACKET_ORIGDEV
:
2152 if (len
> sizeof(int))
2158 case PACKET_VNET_HDR
:
2159 if (len
> sizeof(int))
2161 val
= po
->has_vnet_hdr
;
2165 case PACKET_VERSION
:
2166 if (len
> sizeof(int))
2168 val
= po
->tp_version
;
2172 if (len
> sizeof(int))
2174 if (copy_from_user(&val
, optval
, len
))
2178 val
= sizeof(struct tpacket_hdr
);
2181 val
= sizeof(struct tpacket2_hdr
);
2188 case PACKET_RESERVE
:
2189 if (len
> sizeof(unsigned int))
2190 len
= sizeof(unsigned int);
2191 val
= po
->tp_reserve
;
2195 if (len
> sizeof(unsigned int))
2196 len
= sizeof(unsigned int);
2200 case PACKET_TIMESTAMP
:
2201 if (len
> sizeof(int))
2203 val
= po
->tp_tstamp
;
2207 return -ENOPROTOOPT
;
2210 if (put_user(len
, optlen
))
2212 if (copy_to_user(optval
, data
, len
))
2218 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
2221 struct hlist_node
*node
;
2222 struct net_device
*dev
= data
;
2223 struct net
*net
= dev_net(dev
);
2226 sk_for_each_rcu(sk
, node
, &net
->packet
.sklist
) {
2227 struct packet_sock
*po
= pkt_sk(sk
);
2230 case NETDEV_UNREGISTER
:
2232 packet_dev_mclist(dev
, po
->mclist
, -1);
2236 if (dev
->ifindex
== po
->ifindex
) {
2237 spin_lock(&po
->bind_lock
);
2239 __dev_remove_pack(&po
->prot_hook
);
2242 sk
->sk_err
= ENETDOWN
;
2243 if (!sock_flag(sk
, SOCK_DEAD
))
2244 sk
->sk_error_report(sk
);
2246 if (msg
== NETDEV_UNREGISTER
) {
2248 po
->prot_hook
.dev
= NULL
;
2250 spin_unlock(&po
->bind_lock
);
2254 if (dev
->ifindex
== po
->ifindex
) {
2255 spin_lock(&po
->bind_lock
);
2256 if (po
->num
&& !po
->running
) {
2257 dev_add_pack(&po
->prot_hook
);
2261 spin_unlock(&po
->bind_lock
);
2271 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
2274 struct sock
*sk
= sock
->sk
;
2279 int amount
= sk_wmem_alloc_get(sk
);
2281 return put_user(amount
, (int __user
*)arg
);
2285 struct sk_buff
*skb
;
2288 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2289 skb
= skb_peek(&sk
->sk_receive_queue
);
2292 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2293 return put_user(amount
, (int __user
*)arg
);
2296 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
2298 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
2308 case SIOCGIFBRDADDR
:
2309 case SIOCSIFBRDADDR
:
2310 case SIOCGIFNETMASK
:
2311 case SIOCSIFNETMASK
:
2312 case SIOCGIFDSTADDR
:
2313 case SIOCSIFDSTADDR
:
2315 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
2319 return -ENOIOCTLCMD
;
2324 static unsigned int packet_poll(struct file
*file
, struct socket
*sock
,
2327 struct sock
*sk
= sock
->sk
;
2328 struct packet_sock
*po
= pkt_sk(sk
);
2329 unsigned int mask
= datagram_poll(file
, sock
, wait
);
2331 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2332 if (po
->rx_ring
.pg_vec
) {
2333 if (!packet_previous_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
))
2334 mask
|= POLLIN
| POLLRDNORM
;
2336 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2337 spin_lock_bh(&sk
->sk_write_queue
.lock
);
2338 if (po
->tx_ring
.pg_vec
) {
2339 if (packet_current_frame(po
, &po
->tx_ring
, TP_STATUS_AVAILABLE
))
2340 mask
|= POLLOUT
| POLLWRNORM
;
2342 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
2347 /* Dirty? Well, I still did not learn better way to account
2351 static void packet_mm_open(struct vm_area_struct
*vma
)
2353 struct file
*file
= vma
->vm_file
;
2354 struct socket
*sock
= file
->private_data
;
2355 struct sock
*sk
= sock
->sk
;
2358 atomic_inc(&pkt_sk(sk
)->mapped
);
2361 static void packet_mm_close(struct vm_area_struct
*vma
)
2363 struct file
*file
= vma
->vm_file
;
2364 struct socket
*sock
= file
->private_data
;
2365 struct sock
*sk
= sock
->sk
;
2368 atomic_dec(&pkt_sk(sk
)->mapped
);
2371 static const struct vm_operations_struct packet_mmap_ops
= {
2372 .open
= packet_mm_open
,
2373 .close
= packet_mm_close
,
2376 static void free_pg_vec(struct pgv
*pg_vec
, unsigned int order
,
2381 for (i
= 0; i
< len
; i
++) {
2382 if (likely(pg_vec
[i
].buffer
)) {
2383 if (is_vmalloc_addr(pg_vec
[i
].buffer
))
2384 vfree(pg_vec
[i
].buffer
);
2386 free_pages((unsigned long)pg_vec
[i
].buffer
,
2388 pg_vec
[i
].buffer
= NULL
;
2394 static inline char *alloc_one_pg_vec_page(unsigned long order
)
2396 char *buffer
= NULL
;
2397 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
|
2398 __GFP_ZERO
| __GFP_NOWARN
| __GFP_NORETRY
;
2400 buffer
= (char *) __get_free_pages(gfp_flags
, order
);
2406 * __get_free_pages failed, fall back to vmalloc
2408 buffer
= vzalloc((1 << order
) * PAGE_SIZE
);
2414 * vmalloc failed, lets dig into swap here
2416 gfp_flags
&= ~__GFP_NORETRY
;
2417 buffer
= (char *)__get_free_pages(gfp_flags
, order
);
2422 * complete and utter failure
2427 static struct pgv
*alloc_pg_vec(struct tpacket_req
*req
, int order
)
2429 unsigned int block_nr
= req
->tp_block_nr
;
2433 pg_vec
= kcalloc(block_nr
, sizeof(struct pgv
), GFP_KERNEL
);
2434 if (unlikely(!pg_vec
))
2437 for (i
= 0; i
< block_nr
; i
++) {
2438 pg_vec
[i
].buffer
= alloc_one_pg_vec_page(order
);
2439 if (unlikely(!pg_vec
[i
].buffer
))
2440 goto out_free_pgvec
;
2447 free_pg_vec(pg_vec
, order
, block_nr
);
2452 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
2453 int closing
, int tx_ring
)
2455 struct pgv
*pg_vec
= NULL
;
2456 struct packet_sock
*po
= pkt_sk(sk
);
2457 int was_running
, order
= 0;
2458 struct packet_ring_buffer
*rb
;
2459 struct sk_buff_head
*rb_queue
;
2463 rb
= tx_ring
? &po
->tx_ring
: &po
->rx_ring
;
2464 rb_queue
= tx_ring
? &sk
->sk_write_queue
: &sk
->sk_receive_queue
;
2468 if (atomic_read(&po
->mapped
))
2470 if (atomic_read(&rb
->pending
))
2474 if (req
->tp_block_nr
) {
2475 /* Sanity tests and some calculations */
2477 if (unlikely(rb
->pg_vec
))
2480 switch (po
->tp_version
) {
2482 po
->tp_hdrlen
= TPACKET_HDRLEN
;
2485 po
->tp_hdrlen
= TPACKET2_HDRLEN
;
2490 if (unlikely((int)req
->tp_block_size
<= 0))
2492 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
2494 if (unlikely(req
->tp_frame_size
< po
->tp_hdrlen
+
2497 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
2500 rb
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
2501 if (unlikely(rb
->frames_per_block
<= 0))
2503 if (unlikely((rb
->frames_per_block
* req
->tp_block_nr
) !=
2508 order
= get_order(req
->tp_block_size
);
2509 pg_vec
= alloc_pg_vec(req
, order
);
2510 if (unlikely(!pg_vec
))
2516 if (unlikely(req
->tp_frame_nr
))
2522 /* Detach socket from network */
2523 spin_lock(&po
->bind_lock
);
2524 was_running
= po
->running
;
2527 __dev_remove_pack(&po
->prot_hook
);
2532 spin_unlock(&po
->bind_lock
);
2537 mutex_lock(&po
->pg_vec_lock
);
2538 if (closing
|| atomic_read(&po
->mapped
) == 0) {
2540 spin_lock_bh(&rb_queue
->lock
);
2541 swap(rb
->pg_vec
, pg_vec
);
2542 rb
->frame_max
= (req
->tp_frame_nr
- 1);
2544 rb
->frame_size
= req
->tp_frame_size
;
2545 spin_unlock_bh(&rb_queue
->lock
);
2547 swap(rb
->pg_vec_order
, order
);
2548 swap(rb
->pg_vec_len
, req
->tp_block_nr
);
2550 rb
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
2551 po
->prot_hook
.func
= (po
->rx_ring
.pg_vec
) ?
2552 tpacket_rcv
: packet_rcv
;
2553 skb_queue_purge(rb_queue
);
2554 if (atomic_read(&po
->mapped
))
2555 pr_err("packet_mmap: vma is busy: %d\n",
2556 atomic_read(&po
->mapped
));
2558 mutex_unlock(&po
->pg_vec_lock
);
2560 spin_lock(&po
->bind_lock
);
2561 if (was_running
&& !po
->running
) {
2565 dev_add_pack(&po
->prot_hook
);
2567 spin_unlock(&po
->bind_lock
);
2572 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
2577 static int packet_mmap(struct file
*file
, struct socket
*sock
,
2578 struct vm_area_struct
*vma
)
2580 struct sock
*sk
= sock
->sk
;
2581 struct packet_sock
*po
= pkt_sk(sk
);
2582 unsigned long size
, expected_size
;
2583 struct packet_ring_buffer
*rb
;
2584 unsigned long start
;
2591 mutex_lock(&po
->pg_vec_lock
);
2594 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2596 expected_size
+= rb
->pg_vec_len
2602 if (expected_size
== 0)
2605 size
= vma
->vm_end
- vma
->vm_start
;
2606 if (size
!= expected_size
)
2609 start
= vma
->vm_start
;
2610 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2611 if (rb
->pg_vec
== NULL
)
2614 for (i
= 0; i
< rb
->pg_vec_len
; i
++) {
2616 void *kaddr
= rb
->pg_vec
[i
].buffer
;
2619 for (pg_num
= 0; pg_num
< rb
->pg_vec_pages
; pg_num
++) {
2620 page
= pgv_to_page(kaddr
);
2621 err
= vm_insert_page(vma
, start
, page
);
2630 atomic_inc(&po
->mapped
);
2631 vma
->vm_ops
= &packet_mmap_ops
;
2635 mutex_unlock(&po
->pg_vec_lock
);
2639 static const struct proto_ops packet_ops_spkt
= {
2640 .family
= PF_PACKET
,
2641 .owner
= THIS_MODULE
,
2642 .release
= packet_release
,
2643 .bind
= packet_bind_spkt
,
2644 .connect
= sock_no_connect
,
2645 .socketpair
= sock_no_socketpair
,
2646 .accept
= sock_no_accept
,
2647 .getname
= packet_getname_spkt
,
2648 .poll
= datagram_poll
,
2649 .ioctl
= packet_ioctl
,
2650 .listen
= sock_no_listen
,
2651 .shutdown
= sock_no_shutdown
,
2652 .setsockopt
= sock_no_setsockopt
,
2653 .getsockopt
= sock_no_getsockopt
,
2654 .sendmsg
= packet_sendmsg_spkt
,
2655 .recvmsg
= packet_recvmsg
,
2656 .mmap
= sock_no_mmap
,
2657 .sendpage
= sock_no_sendpage
,
2660 static const struct proto_ops packet_ops
= {
2661 .family
= PF_PACKET
,
2662 .owner
= THIS_MODULE
,
2663 .release
= packet_release
,
2664 .bind
= packet_bind
,
2665 .connect
= sock_no_connect
,
2666 .socketpair
= sock_no_socketpair
,
2667 .accept
= sock_no_accept
,
2668 .getname
= packet_getname
,
2669 .poll
= packet_poll
,
2670 .ioctl
= packet_ioctl
,
2671 .listen
= sock_no_listen
,
2672 .shutdown
= sock_no_shutdown
,
2673 .setsockopt
= packet_setsockopt
,
2674 .getsockopt
= packet_getsockopt
,
2675 .sendmsg
= packet_sendmsg
,
2676 .recvmsg
= packet_recvmsg
,
2677 .mmap
= packet_mmap
,
2678 .sendpage
= sock_no_sendpage
,
2681 static const struct net_proto_family packet_family_ops
= {
2682 .family
= PF_PACKET
,
2683 .create
= packet_create
,
2684 .owner
= THIS_MODULE
,
2687 static struct notifier_block packet_netdev_notifier
= {
2688 .notifier_call
= packet_notifier
,
2691 #ifdef CONFIG_PROC_FS
2693 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2696 struct net
*net
= seq_file_net(seq
);
2699 return seq_hlist_start_head_rcu(&net
->packet
.sklist
, *pos
);
2702 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2704 struct net
*net
= seq_file_net(seq
);
2705 return seq_hlist_next_rcu(v
, &net
->packet
.sklist
, pos
);
2708 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
2714 static int packet_seq_show(struct seq_file
*seq
, void *v
)
2716 if (v
== SEQ_START_TOKEN
)
2717 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2719 struct sock
*s
= sk_entry(v
);
2720 const struct packet_sock
*po
= pkt_sk(s
);
2723 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2725 atomic_read(&s
->sk_refcnt
),
2730 atomic_read(&s
->sk_rmem_alloc
),
2738 static const struct seq_operations packet_seq_ops
= {
2739 .start
= packet_seq_start
,
2740 .next
= packet_seq_next
,
2741 .stop
= packet_seq_stop
,
2742 .show
= packet_seq_show
,
2745 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
2747 return seq_open_net(inode
, file
, &packet_seq_ops
,
2748 sizeof(struct seq_net_private
));
2751 static const struct file_operations packet_seq_fops
= {
2752 .owner
= THIS_MODULE
,
2753 .open
= packet_seq_open
,
2755 .llseek
= seq_lseek
,
2756 .release
= seq_release_net
,
2761 static int __net_init
packet_net_init(struct net
*net
)
2763 spin_lock_init(&net
->packet
.sklist_lock
);
2764 INIT_HLIST_HEAD(&net
->packet
.sklist
);
2766 if (!proc_net_fops_create(net
, "packet", 0, &packet_seq_fops
))
2772 static void __net_exit
packet_net_exit(struct net
*net
)
2774 proc_net_remove(net
, "packet");
2777 static struct pernet_operations packet_net_ops
= {
2778 .init
= packet_net_init
,
2779 .exit
= packet_net_exit
,
2783 static void __exit
packet_exit(void)
2785 unregister_netdevice_notifier(&packet_netdev_notifier
);
2786 unregister_pernet_subsys(&packet_net_ops
);
2787 sock_unregister(PF_PACKET
);
2788 proto_unregister(&packet_proto
);
2791 static int __init
packet_init(void)
2793 int rc
= proto_register(&packet_proto
, 0);
2798 sock_register(&packet_family_ops
);
2799 register_pernet_subsys(&packet_net_ops
);
2800 register_netdevice_notifier(&packet_netdev_notifier
);
2805 module_init(packet_init
);
2806 module_exit(packet_exit
);
2807 MODULE_LICENSE("GPL");
2808 MODULE_ALIAS_NETPROTO(PF_PACKET
);