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 <net/net_namespace.h>
66 #include <net/protocol.h>
67 #include <linux/skbuff.h>
69 #include <linux/errno.h>
70 #include <linux/timer.h>
71 #include <asm/system.h>
72 #include <asm/uaccess.h>
73 #include <asm/ioctls.h>
75 #include <asm/cacheflush.h>
77 #include <linux/proc_fs.h>
78 #include <linux/seq_file.h>
79 #include <linux/poll.h>
80 #include <linux/module.h>
81 #include <linux/init.h>
82 #include <linux/mutex.h>
83 #include <linux/if_vlan.h>
84 #include <linux/virtio_net.h>
87 #include <net/inet_common.h>
92 - if device has no dev->hard_header routine, it adds and removes ll header
93 inside itself. In this case ll header is invisible outside of device,
94 but higher levels still should reserve dev->hard_header_len.
95 Some devices are enough clever to reallocate skb, when header
96 will not fit to reserved space (tunnel), another ones are silly
98 - packet socket receives packets with pulled ll header,
99 so that SOCK_RAW should push it back.
104 Incoming, dev->hard_header!=NULL
105 mac_header -> ll header
108 Outgoing, dev->hard_header!=NULL
109 mac_header -> ll header
112 Incoming, dev->hard_header==NULL
113 mac_header -> UNKNOWN position. It is very likely, that it points to ll
114 header. PPP makes it, that is wrong, because introduce
115 assymetry between rx and tx paths.
118 Outgoing, dev->hard_header==NULL
119 mac_header -> data. ll header is still not built!
123 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
129 dev->hard_header != NULL
130 mac_header -> ll header
133 dev->hard_header == NULL (ll header is added by device, we cannot control it)
137 We should set nh.raw on output to correct posistion,
138 packet classifier depends on it.
141 /* Private packet socket structures. */
143 struct packet_mclist
{
144 struct packet_mclist
*next
;
149 unsigned char addr
[MAX_ADDR_LEN
];
151 /* identical to struct packet_mreq except it has
152 * a longer address field.
154 struct packet_mreq_max
{
156 unsigned short mr_type
;
157 unsigned short mr_alen
;
158 unsigned char mr_address
[MAX_ADDR_LEN
];
161 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
162 int closing
, int tx_ring
);
164 struct packet_ring_buffer
{
167 unsigned int frames_per_block
;
168 unsigned int frame_size
;
169 unsigned int frame_max
;
171 unsigned int pg_vec_order
;
172 unsigned int pg_vec_pages
;
173 unsigned int pg_vec_len
;
179 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
);
181 static void packet_flush_mclist(struct sock
*sk
);
184 /* struct sock has to be the first member of packet_sock */
186 struct tpacket_stats stats
;
187 struct packet_ring_buffer rx_ring
;
188 struct packet_ring_buffer tx_ring
;
190 spinlock_t bind_lock
;
191 struct mutex pg_vec_lock
;
192 unsigned int running
:1, /* prot_hook is attached*/
196 int ifindex
; /* bound device */
198 struct packet_mclist
*mclist
;
200 enum tpacket_versions tp_version
;
201 unsigned int tp_hdrlen
;
202 unsigned int tp_reserve
;
203 unsigned int tp_loss
:1;
204 struct packet_type prot_hook ____cacheline_aligned_in_smp
;
207 struct packet_skb_cb
{
208 unsigned int origlen
;
210 struct sockaddr_pkt pkt
;
211 struct sockaddr_ll ll
;
215 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
217 static void __packet_set_status(struct packet_sock
*po
, void *frame
, int status
)
220 struct tpacket_hdr
*h1
;
221 struct tpacket2_hdr
*h2
;
226 switch (po
->tp_version
) {
228 h
.h1
->tp_status
= status
;
229 flush_dcache_page(virt_to_page(&h
.h1
->tp_status
));
232 h
.h2
->tp_status
= status
;
233 flush_dcache_page(virt_to_page(&h
.h2
->tp_status
));
236 pr_err("TPACKET version not supported\n");
243 static int __packet_get_status(struct packet_sock
*po
, void *frame
)
246 struct tpacket_hdr
*h1
;
247 struct tpacket2_hdr
*h2
;
254 switch (po
->tp_version
) {
256 flush_dcache_page(virt_to_page(&h
.h1
->tp_status
));
257 return h
.h1
->tp_status
;
259 flush_dcache_page(virt_to_page(&h
.h2
->tp_status
));
260 return h
.h2
->tp_status
;
262 pr_err("TPACKET version not supported\n");
268 static void *packet_lookup_frame(struct packet_sock
*po
,
269 struct packet_ring_buffer
*rb
,
270 unsigned int position
,
273 unsigned int pg_vec_pos
, frame_offset
;
275 struct tpacket_hdr
*h1
;
276 struct tpacket2_hdr
*h2
;
280 pg_vec_pos
= position
/ rb
->frames_per_block
;
281 frame_offset
= position
% rb
->frames_per_block
;
283 h
.raw
= rb
->pg_vec
[pg_vec_pos
] + (frame_offset
* rb
->frame_size
);
285 if (status
!= __packet_get_status(po
, h
.raw
))
291 static inline void *packet_current_frame(struct packet_sock
*po
,
292 struct packet_ring_buffer
*rb
,
295 return packet_lookup_frame(po
, rb
, rb
->head
, status
);
298 static inline void *packet_previous_frame(struct packet_sock
*po
,
299 struct packet_ring_buffer
*rb
,
302 unsigned int previous
= rb
->head
? rb
->head
- 1 : rb
->frame_max
;
303 return packet_lookup_frame(po
, rb
, previous
, status
);
306 static inline void packet_increment_head(struct packet_ring_buffer
*buff
)
308 buff
->head
= buff
->head
!= buff
->frame_max
? buff
->head
+1 : 0;
311 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
313 return (struct packet_sock
*)sk
;
316 static void packet_sock_destruct(struct sock
*sk
)
318 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
319 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
321 if (!sock_flag(sk
, SOCK_DEAD
)) {
322 pr_err("Attempt to release alive packet socket: %p\n", sk
);
326 sk_refcnt_debug_dec(sk
);
330 static const struct proto_ops packet_ops
;
332 static const struct proto_ops packet_ops_spkt
;
334 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
,
335 struct packet_type
*pt
, struct net_device
*orig_dev
)
338 struct sockaddr_pkt
*spkt
;
341 * When we registered the protocol we saved the socket in the data
342 * field for just this event.
345 sk
= pt
->af_packet_priv
;
348 * Yank back the headers [hope the device set this
349 * right or kerboom...]
351 * Incoming packets have ll header pulled,
354 * For outgoing ones skb->data == skb_mac_header(skb)
355 * so that this procedure is noop.
358 if (skb
->pkt_type
== PACKET_LOOPBACK
)
361 if (!net_eq(dev_net(dev
), sock_net(sk
)))
364 skb
= skb_share_check(skb
, GFP_ATOMIC
);
368 /* drop any routing info */
371 /* drop conntrack reference */
374 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
376 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
379 * The SOCK_PACKET socket receives _all_ frames.
382 spkt
->spkt_family
= dev
->type
;
383 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
384 spkt
->spkt_protocol
= skb
->protocol
;
387 * Charge the memory to the socket. This is done specifically
388 * to prevent sockets using all the memory up.
391 if (sock_queue_rcv_skb(sk
, skb
) == 0)
402 * Output a raw packet to a device layer. This bypasses all the other
403 * protocol layers and you must therefore supply it with a complete frame
406 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
407 struct msghdr
*msg
, size_t len
)
409 struct sock
*sk
= sock
->sk
;
410 struct sockaddr_pkt
*saddr
= (struct sockaddr_pkt
*)msg
->msg_name
;
411 struct sk_buff
*skb
= NULL
;
412 struct net_device
*dev
;
417 * Get and verify the address.
421 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
423 if (msg
->msg_namelen
== sizeof(struct sockaddr_pkt
))
424 proto
= saddr
->spkt_protocol
;
426 return -ENOTCONN
; /* SOCK_PACKET must be sent giving an address */
429 * Find the device first to size check it
432 saddr
->spkt_device
[13] = 0;
435 dev
= dev_get_by_name_rcu(sock_net(sk
), saddr
->spkt_device
);
441 if (!(dev
->flags
& IFF_UP
))
445 * You may not queue a frame bigger than the mtu. This is the lowest level
446 * raw protocol and you must do your own fragmentation at this level.
450 if (len
> dev
->mtu
+ dev
->hard_header_len
)
454 size_t reserved
= LL_RESERVED_SPACE(dev
);
455 unsigned int hhlen
= dev
->header_ops
? dev
->hard_header_len
: 0;
458 skb
= sock_wmalloc(sk
, len
+ reserved
, 0, GFP_KERNEL
);
461 /* FIXME: Save some space for broken drivers that write a hard
462 * header at transmission time by themselves. PPP is the notable
463 * one here. This should really be fixed at the driver level.
465 skb_reserve(skb
, reserved
);
466 skb_reset_network_header(skb
);
468 /* Try to align data part correctly */
473 skb_reset_network_header(skb
);
475 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
482 skb
->protocol
= proto
;
484 skb
->priority
= sk
->sk_priority
;
485 skb
->mark
= sk
->sk_mark
;
498 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
501 struct sk_filter
*filter
;
504 filter
= rcu_dereference_bh(sk
->sk_filter
);
506 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
507 rcu_read_unlock_bh();
513 This function makes lazy skb cloning in hope that most of packets
514 are discarded by BPF.
516 Note tricky part: we DO mangle shared skb! skb->data, skb->len
517 and skb->cb are mangled. It works because (and until) packets
518 falling here are owned by current CPU. Output packets are cloned
519 by dev_queue_xmit_nit(), input packets are processed by net_bh
520 sequencially, so that if we return skb to original state on exit,
521 we will not harm anyone.
524 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
525 struct packet_type
*pt
, struct net_device
*orig_dev
)
528 struct sockaddr_ll
*sll
;
529 struct packet_sock
*po
;
530 u8
*skb_head
= skb
->data
;
531 int skb_len
= skb
->len
;
532 unsigned int snaplen
, res
;
534 if (skb
->pkt_type
== PACKET_LOOPBACK
)
537 sk
= pt
->af_packet_priv
;
540 if (!net_eq(dev_net(dev
), sock_net(sk
)))
545 if (dev
->header_ops
) {
546 /* The device has an explicit notion of ll header,
547 exported to higher levels.
549 Otherwise, the device hides datails of it frame
550 structure, so that corresponding packet head
551 never delivered to user.
553 if (sk
->sk_type
!= SOCK_DGRAM
)
554 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
555 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
556 /* Special case: outgoing packets have ll header at head */
557 skb_pull(skb
, skb_network_offset(skb
));
563 res
= run_filter(skb
, sk
, snaplen
);
569 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
570 (unsigned)sk
->sk_rcvbuf
)
573 if (skb_shared(skb
)) {
574 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
578 if (skb_head
!= skb
->data
) {
579 skb
->data
= skb_head
;
586 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
589 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
590 sll
->sll_family
= AF_PACKET
;
591 sll
->sll_hatype
= dev
->type
;
592 sll
->sll_protocol
= skb
->protocol
;
593 sll
->sll_pkttype
= skb
->pkt_type
;
594 if (unlikely(po
->origdev
))
595 sll
->sll_ifindex
= orig_dev
->ifindex
;
597 sll
->sll_ifindex
= dev
->ifindex
;
599 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
601 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
603 if (pskb_trim(skb
, snaplen
))
606 skb_set_owner_r(skb
, sk
);
610 /* drop conntrack reference */
613 spin_lock(&sk
->sk_receive_queue
.lock
);
614 po
->stats
.tp_packets
++;
615 skb
->dropcount
= atomic_read(&sk
->sk_drops
);
616 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
617 spin_unlock(&sk
->sk_receive_queue
.lock
);
618 sk
->sk_data_ready(sk
, skb
->len
);
622 po
->stats
.tp_drops
= atomic_inc_return(&sk
->sk_drops
);
625 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
626 skb
->data
= skb_head
;
634 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
635 struct packet_type
*pt
, struct net_device
*orig_dev
)
638 struct packet_sock
*po
;
639 struct sockaddr_ll
*sll
;
641 struct tpacket_hdr
*h1
;
642 struct tpacket2_hdr
*h2
;
645 u8
*skb_head
= skb
->data
;
646 int skb_len
= skb
->len
;
647 unsigned int snaplen
, res
;
648 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
649 unsigned short macoff
, netoff
, hdrlen
;
650 struct sk_buff
*copy_skb
= NULL
;
654 if (skb
->pkt_type
== PACKET_LOOPBACK
)
657 sk
= pt
->af_packet_priv
;
660 if (!net_eq(dev_net(dev
), sock_net(sk
)))
663 if (dev
->header_ops
) {
664 if (sk
->sk_type
!= SOCK_DGRAM
)
665 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
666 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
667 /* Special case: outgoing packets have ll header at head */
668 skb_pull(skb
, skb_network_offset(skb
));
672 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
673 status
|= TP_STATUS_CSUMNOTREADY
;
677 res
= run_filter(skb
, sk
, snaplen
);
683 if (sk
->sk_type
== SOCK_DGRAM
) {
684 macoff
= netoff
= TPACKET_ALIGN(po
->tp_hdrlen
) + 16 +
687 unsigned maclen
= skb_network_offset(skb
);
688 netoff
= TPACKET_ALIGN(po
->tp_hdrlen
+
689 (maclen
< 16 ? 16 : maclen
)) +
691 macoff
= netoff
- maclen
;
694 if (macoff
+ snaplen
> po
->rx_ring
.frame_size
) {
695 if (po
->copy_thresh
&&
696 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
697 (unsigned)sk
->sk_rcvbuf
) {
698 if (skb_shared(skb
)) {
699 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
701 copy_skb
= skb_get(skb
);
702 skb_head
= skb
->data
;
705 skb_set_owner_r(copy_skb
, sk
);
707 snaplen
= po
->rx_ring
.frame_size
- macoff
;
708 if ((int)snaplen
< 0)
712 spin_lock(&sk
->sk_receive_queue
.lock
);
713 h
.raw
= packet_current_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
);
716 packet_increment_head(&po
->rx_ring
);
717 po
->stats
.tp_packets
++;
719 status
|= TP_STATUS_COPY
;
720 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
722 if (!po
->stats
.tp_drops
)
723 status
&= ~TP_STATUS_LOSING
;
724 spin_unlock(&sk
->sk_receive_queue
.lock
);
726 skb_copy_bits(skb
, 0, h
.raw
+ macoff
, snaplen
);
728 switch (po
->tp_version
) {
730 h
.h1
->tp_len
= skb
->len
;
731 h
.h1
->tp_snaplen
= snaplen
;
732 h
.h1
->tp_mac
= macoff
;
733 h
.h1
->tp_net
= netoff
;
734 if (skb
->tstamp
.tv64
)
735 tv
= ktime_to_timeval(skb
->tstamp
);
737 do_gettimeofday(&tv
);
738 h
.h1
->tp_sec
= tv
.tv_sec
;
739 h
.h1
->tp_usec
= tv
.tv_usec
;
740 hdrlen
= sizeof(*h
.h1
);
743 h
.h2
->tp_len
= skb
->len
;
744 h
.h2
->tp_snaplen
= snaplen
;
745 h
.h2
->tp_mac
= macoff
;
746 h
.h2
->tp_net
= netoff
;
747 if (skb
->tstamp
.tv64
)
748 ts
= ktime_to_timespec(skb
->tstamp
);
751 h
.h2
->tp_sec
= ts
.tv_sec
;
752 h
.h2
->tp_nsec
= ts
.tv_nsec
;
753 h
.h2
->tp_vlan_tci
= vlan_tx_tag_get(skb
);
754 h
.h2
->tp_padding
= 0;
755 hdrlen
= sizeof(*h
.h2
);
761 sll
= h
.raw
+ TPACKET_ALIGN(hdrlen
);
762 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
763 sll
->sll_family
= AF_PACKET
;
764 sll
->sll_hatype
= dev
->type
;
765 sll
->sll_protocol
= skb
->protocol
;
766 sll
->sll_pkttype
= skb
->pkt_type
;
767 if (unlikely(po
->origdev
))
768 sll
->sll_ifindex
= orig_dev
->ifindex
;
770 sll
->sll_ifindex
= dev
->ifindex
;
772 __packet_set_status(po
, h
.raw
, status
);
775 struct page
*p_start
, *p_end
;
776 u8
*h_end
= h
.raw
+ macoff
+ snaplen
- 1;
778 p_start
= virt_to_page(h
.raw
);
779 p_end
= virt_to_page(h_end
);
780 while (p_start
<= p_end
) {
781 flush_dcache_page(p_start
);
786 sk
->sk_data_ready(sk
, 0);
789 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
790 skb
->data
= skb_head
;
798 po
->stats
.tp_drops
++;
799 spin_unlock(&sk
->sk_receive_queue
.lock
);
801 sk
->sk_data_ready(sk
, 0);
806 static void tpacket_destruct_skb(struct sk_buff
*skb
)
808 struct packet_sock
*po
= pkt_sk(skb
->sk
);
813 if (likely(po
->tx_ring
.pg_vec
)) {
814 ph
= skb_shinfo(skb
)->destructor_arg
;
815 BUG_ON(__packet_get_status(po
, ph
) != TP_STATUS_SENDING
);
816 BUG_ON(atomic_read(&po
->tx_ring
.pending
) == 0);
817 atomic_dec(&po
->tx_ring
.pending
);
818 __packet_set_status(po
, ph
, TP_STATUS_AVAILABLE
);
824 static int tpacket_fill_skb(struct packet_sock
*po
, struct sk_buff
*skb
,
825 void *frame
, struct net_device
*dev
, int size_max
,
826 __be16 proto
, unsigned char *addr
)
829 struct tpacket_hdr
*h1
;
830 struct tpacket2_hdr
*h2
;
833 int to_write
, offset
, len
, tp_len
, nr_frags
, len_max
;
834 struct socket
*sock
= po
->sk
.sk_socket
;
841 skb
->protocol
= proto
;
843 skb
->priority
= po
->sk
.sk_priority
;
844 skb
->mark
= po
->sk
.sk_mark
;
845 skb_shinfo(skb
)->destructor_arg
= ph
.raw
;
847 switch (po
->tp_version
) {
849 tp_len
= ph
.h2
->tp_len
;
852 tp_len
= ph
.h1
->tp_len
;
855 if (unlikely(tp_len
> size_max
)) {
856 pr_err("packet size is too long (%d > %d)\n", tp_len
, size_max
);
860 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
861 skb_reset_network_header(skb
);
863 data
= ph
.raw
+ po
->tp_hdrlen
- sizeof(struct sockaddr_ll
);
866 if (sock
->type
== SOCK_DGRAM
) {
867 err
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
,
869 if (unlikely(err
< 0))
871 } else if (dev
->hard_header_len
) {
872 /* net device doesn't like empty head */
873 if (unlikely(tp_len
<= dev
->hard_header_len
)) {
874 pr_err("packet size is too short (%d < %d)\n",
875 tp_len
, dev
->hard_header_len
);
879 skb_push(skb
, dev
->hard_header_len
);
880 err
= skb_store_bits(skb
, 0, data
,
881 dev
->hard_header_len
);
885 data
+= dev
->hard_header_len
;
886 to_write
-= dev
->hard_header_len
;
890 page
= virt_to_page(data
);
891 offset
= offset_in_page(data
);
892 len_max
= PAGE_SIZE
- offset
;
893 len
= ((to_write
> len_max
) ? len_max
: to_write
);
895 skb
->data_len
= to_write
;
896 skb
->len
+= to_write
;
897 skb
->truesize
+= to_write
;
898 atomic_add(to_write
, &po
->sk
.sk_wmem_alloc
);
900 while (likely(to_write
)) {
901 nr_frags
= skb_shinfo(skb
)->nr_frags
;
903 if (unlikely(nr_frags
>= MAX_SKB_FRAGS
)) {
904 pr_err("Packet exceed the number of skb frags(%lu)\n",
909 flush_dcache_page(page
);
911 skb_fill_page_desc(skb
,
913 page
++, offset
, len
);
917 len
= ((to_write
> len_max
) ? len_max
: to_write
);
923 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
)
927 struct net_device
*dev
;
929 int ifindex
, err
, reserve
= 0;
931 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
932 int tp_len
, size_max
;
937 sock
= po
->sk
.sk_socket
;
939 mutex_lock(&po
->pg_vec_lock
);
943 ifindex
= po
->ifindex
;
948 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
950 if (msg
->msg_namelen
< (saddr
->sll_halen
951 + offsetof(struct sockaddr_ll
,
954 ifindex
= saddr
->sll_ifindex
;
955 proto
= saddr
->sll_protocol
;
956 addr
= saddr
->sll_addr
;
959 dev
= dev_get_by_index(sock_net(&po
->sk
), ifindex
);
961 if (unlikely(dev
== NULL
))
964 reserve
= dev
->hard_header_len
;
967 if (unlikely(!(dev
->flags
& IFF_UP
)))
970 size_max
= po
->tx_ring
.frame_size
971 - (po
->tp_hdrlen
- sizeof(struct sockaddr_ll
));
973 if (size_max
> dev
->mtu
+ reserve
)
974 size_max
= dev
->mtu
+ reserve
;
977 ph
= packet_current_frame(po
, &po
->tx_ring
,
978 TP_STATUS_SEND_REQUEST
);
980 if (unlikely(ph
== NULL
)) {
985 status
= TP_STATUS_SEND_REQUEST
;
986 skb
= sock_alloc_send_skb(&po
->sk
,
987 LL_ALLOCATED_SPACE(dev
)
988 + sizeof(struct sockaddr_ll
),
991 if (unlikely(skb
== NULL
))
994 tp_len
= tpacket_fill_skb(po
, skb
, ph
, dev
, size_max
, proto
,
997 if (unlikely(tp_len
< 0)) {
999 __packet_set_status(po
, ph
,
1000 TP_STATUS_AVAILABLE
);
1001 packet_increment_head(&po
->tx_ring
);
1005 status
= TP_STATUS_WRONG_FORMAT
;
1011 skb
->destructor
= tpacket_destruct_skb
;
1012 __packet_set_status(po
, ph
, TP_STATUS_SENDING
);
1013 atomic_inc(&po
->tx_ring
.pending
);
1015 status
= TP_STATUS_SEND_REQUEST
;
1016 err
= dev_queue_xmit(skb
);
1017 if (unlikely(err
> 0)) {
1018 err
= net_xmit_errno(err
);
1019 if (err
&& __packet_get_status(po
, ph
) ==
1020 TP_STATUS_AVAILABLE
) {
1021 /* skb was destructed already */
1026 * skb was dropped but not destructed yet;
1027 * let's treat it like congestion or err < 0
1031 packet_increment_head(&po
->tx_ring
);
1033 } while (likely((ph
!= NULL
) ||
1034 ((!(msg
->msg_flags
& MSG_DONTWAIT
)) &&
1035 (atomic_read(&po
->tx_ring
.pending
))))
1042 __packet_set_status(po
, ph
, status
);
1047 mutex_unlock(&po
->pg_vec_lock
);
1051 static inline struct sk_buff
*packet_alloc_skb(struct sock
*sk
, size_t prepad
,
1052 size_t reserve
, size_t len
,
1053 size_t linear
, int noblock
,
1056 struct sk_buff
*skb
;
1058 /* Under a page? Don't bother with paged skb. */
1059 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
1062 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
1067 skb_reserve(skb
, reserve
);
1068 skb_put(skb
, linear
);
1069 skb
->data_len
= len
- linear
;
1070 skb
->len
+= len
- linear
;
1075 static int packet_snd(struct socket
*sock
,
1076 struct msghdr
*msg
, size_t len
)
1078 struct sock
*sk
= sock
->sk
;
1079 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
1080 struct sk_buff
*skb
;
1081 struct net_device
*dev
;
1083 unsigned char *addr
;
1084 int ifindex
, err
, reserve
= 0;
1085 struct virtio_net_hdr vnet_hdr
= { 0 };
1088 struct packet_sock
*po
= pkt_sk(sk
);
1089 unsigned short gso_type
= 0;
1092 * Get and verify the address.
1095 if (saddr
== NULL
) {
1096 ifindex
= po
->ifindex
;
1101 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
1103 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
1105 ifindex
= saddr
->sll_ifindex
;
1106 proto
= saddr
->sll_protocol
;
1107 addr
= saddr
->sll_addr
;
1111 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
1115 if (sock
->type
== SOCK_RAW
)
1116 reserve
= dev
->hard_header_len
;
1119 if (!(dev
->flags
& IFF_UP
))
1122 if (po
->has_vnet_hdr
) {
1123 vnet_hdr_len
= sizeof(vnet_hdr
);
1126 if (len
< vnet_hdr_len
)
1129 len
-= vnet_hdr_len
;
1131 err
= memcpy_fromiovec((void *)&vnet_hdr
, msg
->msg_iov
,
1136 if ((vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
1137 (vnet_hdr
.csum_start
+ vnet_hdr
.csum_offset
+ 2 >
1139 vnet_hdr
.hdr_len
= vnet_hdr
.csum_start
+
1140 vnet_hdr
.csum_offset
+ 2;
1143 if (vnet_hdr
.hdr_len
> len
)
1146 if (vnet_hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
1147 switch (vnet_hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
1148 case VIRTIO_NET_HDR_GSO_TCPV4
:
1149 gso_type
= SKB_GSO_TCPV4
;
1151 case VIRTIO_NET_HDR_GSO_TCPV6
:
1152 gso_type
= SKB_GSO_TCPV6
;
1154 case VIRTIO_NET_HDR_GSO_UDP
:
1155 gso_type
= SKB_GSO_UDP
;
1161 if (vnet_hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
1162 gso_type
|= SKB_GSO_TCP_ECN
;
1164 if (vnet_hdr
.gso_size
== 0)
1171 if (!gso_type
&& (len
> dev
->mtu
+reserve
))
1175 skb
= packet_alloc_skb(sk
, LL_ALLOCATED_SPACE(dev
),
1176 LL_RESERVED_SPACE(dev
), len
, vnet_hdr
.hdr_len
,
1177 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1181 skb_set_network_header(skb
, reserve
);
1184 if (sock
->type
== SOCK_DGRAM
&&
1185 (offset
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
)) < 0)
1188 /* Returns -EFAULT on error */
1189 err
= skb_copy_datagram_from_iovec(skb
, offset
, msg
->msg_iov
, 0, len
);
1193 skb
->protocol
= proto
;
1195 skb
->priority
= sk
->sk_priority
;
1196 skb
->mark
= sk
->sk_mark
;
1198 if (po
->has_vnet_hdr
) {
1199 if (vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
1200 if (!skb_partial_csum_set(skb
, vnet_hdr
.csum_start
,
1201 vnet_hdr
.csum_offset
)) {
1207 skb_shinfo(skb
)->gso_size
= vnet_hdr
.gso_size
;
1208 skb_shinfo(skb
)->gso_type
= gso_type
;
1210 /* Header must be checked, and gso_segs computed. */
1211 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
1212 skb_shinfo(skb
)->gso_segs
= 0;
1214 len
+= vnet_hdr_len
;
1221 err
= dev_queue_xmit(skb
);
1222 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
1238 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1239 struct msghdr
*msg
, size_t len
)
1241 struct sock
*sk
= sock
->sk
;
1242 struct packet_sock
*po
= pkt_sk(sk
);
1243 if (po
->tx_ring
.pg_vec
)
1244 return tpacket_snd(po
, msg
);
1246 return packet_snd(sock
, msg
, len
);
1250 * Close a PACKET socket. This is fairly simple. We immediately go
1251 * to 'closed' state and remove our protocol entry in the device list.
1254 static int packet_release(struct socket
*sock
)
1256 struct sock
*sk
= sock
->sk
;
1257 struct packet_sock
*po
;
1259 struct tpacket_req req
;
1267 spin_lock_bh(&net
->packet
.sklist_lock
);
1268 sk_del_node_init_rcu(sk
);
1269 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
1270 spin_unlock_bh(&net
->packet
.sklist_lock
);
1272 spin_lock(&po
->bind_lock
);
1275 * Remove from protocol table
1279 __dev_remove_pack(&po
->prot_hook
);
1282 spin_unlock(&po
->bind_lock
);
1284 packet_flush_mclist(sk
);
1286 memset(&req
, 0, sizeof(req
));
1288 if (po
->rx_ring
.pg_vec
)
1289 packet_set_ring(sk
, &req
, 1, 0);
1291 if (po
->tx_ring
.pg_vec
)
1292 packet_set_ring(sk
, &req
, 1, 1);
1296 * Now the socket is dead. No more input will appear.
1303 skb_queue_purge(&sk
->sk_receive_queue
);
1304 sk_refcnt_debug_release(sk
);
1311 * Attach a packet hook.
1314 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
1316 struct packet_sock
*po
= pkt_sk(sk
);
1318 * Detach an existing hook if present.
1323 spin_lock(&po
->bind_lock
);
1328 spin_unlock(&po
->bind_lock
);
1329 dev_remove_pack(&po
->prot_hook
);
1330 spin_lock(&po
->bind_lock
);
1334 po
->prot_hook
.type
= protocol
;
1335 po
->prot_hook
.dev
= dev
;
1337 po
->ifindex
= dev
? dev
->ifindex
: 0;
1342 if (!dev
|| (dev
->flags
& IFF_UP
)) {
1343 dev_add_pack(&po
->prot_hook
);
1347 sk
->sk_err
= ENETDOWN
;
1348 if (!sock_flag(sk
, SOCK_DEAD
))
1349 sk
->sk_error_report(sk
);
1353 spin_unlock(&po
->bind_lock
);
1359 * Bind a packet socket to a device
1362 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1365 struct sock
*sk
= sock
->sk
;
1367 struct net_device
*dev
;
1374 if (addr_len
!= sizeof(struct sockaddr
))
1376 strlcpy(name
, uaddr
->sa_data
, sizeof(name
));
1378 dev
= dev_get_by_name(sock_net(sk
), name
);
1380 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
1386 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1388 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1389 struct sock
*sk
= sock
->sk
;
1390 struct net_device
*dev
= NULL
;
1398 if (addr_len
< sizeof(struct sockaddr_ll
))
1400 if (sll
->sll_family
!= AF_PACKET
)
1403 if (sll
->sll_ifindex
) {
1405 dev
= dev_get_by_index(sock_net(sk
), sll
->sll_ifindex
);
1409 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
1417 static struct proto packet_proto
= {
1419 .owner
= THIS_MODULE
,
1420 .obj_size
= sizeof(struct packet_sock
),
1424 * Create a packet of type SOCK_PACKET.
1427 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
,
1431 struct packet_sock
*po
;
1432 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1435 if (!capable(CAP_NET_RAW
))
1437 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
1438 sock
->type
!= SOCK_PACKET
)
1439 return -ESOCKTNOSUPPORT
;
1441 sock
->state
= SS_UNCONNECTED
;
1444 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
1448 sock
->ops
= &packet_ops
;
1449 if (sock
->type
== SOCK_PACKET
)
1450 sock
->ops
= &packet_ops_spkt
;
1452 sock_init_data(sock
, sk
);
1455 sk
->sk_family
= PF_PACKET
;
1458 sk
->sk_destruct
= packet_sock_destruct
;
1459 sk_refcnt_debug_inc(sk
);
1462 * Attach a protocol block
1465 spin_lock_init(&po
->bind_lock
);
1466 mutex_init(&po
->pg_vec_lock
);
1467 po
->prot_hook
.func
= packet_rcv
;
1469 if (sock
->type
== SOCK_PACKET
)
1470 po
->prot_hook
.func
= packet_rcv_spkt
;
1472 po
->prot_hook
.af_packet_priv
= sk
;
1475 po
->prot_hook
.type
= proto
;
1476 dev_add_pack(&po
->prot_hook
);
1481 spin_lock_bh(&net
->packet
.sklist_lock
);
1482 sk_add_node_rcu(sk
, &net
->packet
.sklist
);
1483 sock_prot_inuse_add(net
, &packet_proto
, 1);
1484 spin_unlock_bh(&net
->packet
.sklist_lock
);
1492 * Pull a packet from our receive queue and hand it to the user.
1493 * If necessary we block.
1496 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1497 struct msghdr
*msg
, size_t len
, int flags
)
1499 struct sock
*sk
= sock
->sk
;
1500 struct sk_buff
*skb
;
1502 struct sockaddr_ll
*sll
;
1503 int vnet_hdr_len
= 0;
1506 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1510 /* What error should we return now? EUNATTACH? */
1511 if (pkt_sk(sk
)->ifindex
< 0)
1516 * Call the generic datagram receiver. This handles all sorts
1517 * of horrible races and re-entrancy so we can forget about it
1518 * in the protocol layers.
1520 * Now it will return ENETDOWN, if device have just gone down,
1521 * but then it will block.
1524 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
1527 * An error occurred so return it. Because skb_recv_datagram()
1528 * handles the blocking we don't see and worry about blocking
1535 if (pkt_sk(sk
)->has_vnet_hdr
) {
1536 struct virtio_net_hdr vnet_hdr
= { 0 };
1539 vnet_hdr_len
= sizeof(vnet_hdr
);
1540 if ((len
-= vnet_hdr_len
) < 0)
1543 if (skb_is_gso(skb
)) {
1544 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
1546 /* This is a hint as to how much should be linear. */
1547 vnet_hdr
.hdr_len
= skb_headlen(skb
);
1548 vnet_hdr
.gso_size
= sinfo
->gso_size
;
1549 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
1550 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1551 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
1552 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1553 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
1554 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
1555 else if (sinfo
->gso_type
& SKB_GSO_FCOE
)
1559 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
1560 vnet_hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1562 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1564 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1565 vnet_hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1566 vnet_hdr
.csum_start
= skb
->csum_start
-
1568 vnet_hdr
.csum_offset
= skb
->csum_offset
;
1569 } /* else everything is zero */
1571 err
= memcpy_toiovec(msg
->msg_iov
, (void *)&vnet_hdr
,
1578 * If the address length field is there to be filled in, we fill
1582 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1583 if (sock
->type
== SOCK_PACKET
)
1584 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1586 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1589 * You lose any data beyond the buffer you gave. If it worries a
1590 * user program they can ask the device for its MTU anyway.
1596 msg
->msg_flags
|= MSG_TRUNC
;
1599 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1603 sock_recv_ts_and_drops(msg
, sk
, skb
);
1606 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1609 if (pkt_sk(sk
)->auxdata
) {
1610 struct tpacket_auxdata aux
;
1612 aux
.tp_status
= TP_STATUS_USER
;
1613 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1614 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1615 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1616 aux
.tp_snaplen
= skb
->len
;
1618 aux
.tp_net
= skb_network_offset(skb
);
1619 aux
.tp_vlan_tci
= vlan_tx_tag_get(skb
);
1622 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1626 * Free or return the buffer as appropriate. Again this
1627 * hides all the races and re-entrancy issues from us.
1629 err
= vnet_hdr_len
+ ((flags
&MSG_TRUNC
) ? skb
->len
: copied
);
1632 skb_free_datagram(sk
, skb
);
1637 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1638 int *uaddr_len
, int peer
)
1640 struct net_device
*dev
;
1641 struct sock
*sk
= sock
->sk
;
1646 uaddr
->sa_family
= AF_PACKET
;
1648 dev
= dev_get_by_index_rcu(sock_net(sk
), pkt_sk(sk
)->ifindex
);
1650 strncpy(uaddr
->sa_data
, dev
->name
, 14);
1652 memset(uaddr
->sa_data
, 0, 14);
1654 *uaddr_len
= sizeof(*uaddr
);
1659 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1660 int *uaddr_len
, int peer
)
1662 struct net_device
*dev
;
1663 struct sock
*sk
= sock
->sk
;
1664 struct packet_sock
*po
= pkt_sk(sk
);
1665 DECLARE_SOCKADDR(struct sockaddr_ll
*, sll
, uaddr
);
1670 sll
->sll_family
= AF_PACKET
;
1671 sll
->sll_ifindex
= po
->ifindex
;
1672 sll
->sll_protocol
= po
->num
;
1673 sll
->sll_pkttype
= 0;
1675 dev
= dev_get_by_index_rcu(sock_net(sk
), po
->ifindex
);
1677 sll
->sll_hatype
= dev
->type
;
1678 sll
->sll_halen
= dev
->addr_len
;
1679 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1681 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1685 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1690 static int packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
,
1694 case PACKET_MR_MULTICAST
:
1695 if (i
->alen
!= dev
->addr_len
)
1698 return dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1700 return dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1702 case PACKET_MR_PROMISC
:
1703 return dev_set_promiscuity(dev
, what
);
1705 case PACKET_MR_ALLMULTI
:
1706 return dev_set_allmulti(dev
, what
);
1708 case PACKET_MR_UNICAST
:
1709 if (i
->alen
!= dev
->addr_len
)
1712 return dev_unicast_add(dev
, i
->addr
);
1714 return dev_unicast_delete(dev
, i
->addr
);
1722 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1724 for ( ; i
; i
= i
->next
) {
1725 if (i
->ifindex
== dev
->ifindex
)
1726 packet_dev_mc(dev
, i
, what
);
1730 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1732 struct packet_sock
*po
= pkt_sk(sk
);
1733 struct packet_mclist
*ml
, *i
;
1734 struct net_device
*dev
;
1740 dev
= __dev_get_by_index(sock_net(sk
), mreq
->mr_ifindex
);
1745 if (mreq
->mr_alen
> dev
->addr_len
)
1749 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1754 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1755 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1756 ml
->type
== mreq
->mr_type
&&
1757 ml
->alen
== mreq
->mr_alen
&&
1758 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1760 /* Free the new element ... */
1766 i
->type
= mreq
->mr_type
;
1767 i
->ifindex
= mreq
->mr_ifindex
;
1768 i
->alen
= mreq
->mr_alen
;
1769 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1771 i
->next
= po
->mclist
;
1773 err
= packet_dev_mc(dev
, i
, 1);
1775 po
->mclist
= i
->next
;
1784 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1786 struct packet_mclist
*ml
, **mlp
;
1790 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1791 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1792 ml
->type
== mreq
->mr_type
&&
1793 ml
->alen
== mreq
->mr_alen
&&
1794 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1795 if (--ml
->count
== 0) {
1796 struct net_device
*dev
;
1798 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1800 packet_dev_mc(dev
, ml
, -1);
1808 return -EADDRNOTAVAIL
;
1811 static void packet_flush_mclist(struct sock
*sk
)
1813 struct packet_sock
*po
= pkt_sk(sk
);
1814 struct packet_mclist
*ml
;
1820 while ((ml
= po
->mclist
) != NULL
) {
1821 struct net_device
*dev
;
1823 po
->mclist
= ml
->next
;
1824 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1826 packet_dev_mc(dev
, ml
, -1);
1833 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
1835 struct sock
*sk
= sock
->sk
;
1836 struct packet_sock
*po
= pkt_sk(sk
);
1839 if (level
!= SOL_PACKET
)
1840 return -ENOPROTOOPT
;
1843 case PACKET_ADD_MEMBERSHIP
:
1844 case PACKET_DROP_MEMBERSHIP
:
1846 struct packet_mreq_max mreq
;
1848 memset(&mreq
, 0, sizeof(mreq
));
1849 if (len
< sizeof(struct packet_mreq
))
1851 if (len
> sizeof(mreq
))
1853 if (copy_from_user(&mreq
, optval
, len
))
1855 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1857 if (optname
== PACKET_ADD_MEMBERSHIP
)
1858 ret
= packet_mc_add(sk
, &mreq
);
1860 ret
= packet_mc_drop(sk
, &mreq
);
1864 case PACKET_RX_RING
:
1865 case PACKET_TX_RING
:
1867 struct tpacket_req req
;
1869 if (optlen
< sizeof(req
))
1871 if (pkt_sk(sk
)->has_vnet_hdr
)
1873 if (copy_from_user(&req
, optval
, sizeof(req
)))
1875 return packet_set_ring(sk
, &req
, 0, optname
== PACKET_TX_RING
);
1877 case PACKET_COPY_THRESH
:
1881 if (optlen
!= sizeof(val
))
1883 if (copy_from_user(&val
, optval
, sizeof(val
)))
1886 pkt_sk(sk
)->copy_thresh
= val
;
1889 case PACKET_VERSION
:
1893 if (optlen
!= sizeof(val
))
1895 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1897 if (copy_from_user(&val
, optval
, sizeof(val
)))
1902 po
->tp_version
= val
;
1908 case PACKET_RESERVE
:
1912 if (optlen
!= sizeof(val
))
1914 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1916 if (copy_from_user(&val
, optval
, sizeof(val
)))
1918 po
->tp_reserve
= val
;
1925 if (optlen
!= sizeof(val
))
1927 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1929 if (copy_from_user(&val
, optval
, sizeof(val
)))
1931 po
->tp_loss
= !!val
;
1934 case PACKET_AUXDATA
:
1938 if (optlen
< sizeof(val
))
1940 if (copy_from_user(&val
, optval
, sizeof(val
)))
1943 po
->auxdata
= !!val
;
1946 case PACKET_ORIGDEV
:
1950 if (optlen
< sizeof(val
))
1952 if (copy_from_user(&val
, optval
, sizeof(val
)))
1955 po
->origdev
= !!val
;
1958 case PACKET_VNET_HDR
:
1962 if (sock
->type
!= SOCK_RAW
)
1964 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1966 if (optlen
< sizeof(val
))
1968 if (copy_from_user(&val
, optval
, sizeof(val
)))
1971 po
->has_vnet_hdr
= !!val
;
1975 return -ENOPROTOOPT
;
1979 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1980 char __user
*optval
, int __user
*optlen
)
1984 struct sock
*sk
= sock
->sk
;
1985 struct packet_sock
*po
= pkt_sk(sk
);
1987 struct tpacket_stats st
;
1989 if (level
!= SOL_PACKET
)
1990 return -ENOPROTOOPT
;
1992 if (get_user(len
, optlen
))
1999 case PACKET_STATISTICS
:
2000 if (len
> sizeof(struct tpacket_stats
))
2001 len
= sizeof(struct tpacket_stats
);
2002 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2004 memset(&po
->stats
, 0, sizeof(st
));
2005 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2006 st
.tp_packets
+= st
.tp_drops
;
2010 case PACKET_AUXDATA
:
2011 if (len
> sizeof(int))
2017 case PACKET_ORIGDEV
:
2018 if (len
> sizeof(int))
2024 case PACKET_VNET_HDR
:
2025 if (len
> sizeof(int))
2027 val
= po
->has_vnet_hdr
;
2031 case PACKET_VERSION
:
2032 if (len
> sizeof(int))
2034 val
= po
->tp_version
;
2038 if (len
> sizeof(int))
2040 if (copy_from_user(&val
, optval
, len
))
2044 val
= sizeof(struct tpacket_hdr
);
2047 val
= sizeof(struct tpacket2_hdr
);
2054 case PACKET_RESERVE
:
2055 if (len
> sizeof(unsigned int))
2056 len
= sizeof(unsigned int);
2057 val
= po
->tp_reserve
;
2061 if (len
> sizeof(unsigned int))
2062 len
= sizeof(unsigned int);
2067 return -ENOPROTOOPT
;
2070 if (put_user(len
, optlen
))
2072 if (copy_to_user(optval
, data
, len
))
2078 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
2081 struct hlist_node
*node
;
2082 struct net_device
*dev
= data
;
2083 struct net
*net
= dev_net(dev
);
2086 sk_for_each_rcu(sk
, node
, &net
->packet
.sklist
) {
2087 struct packet_sock
*po
= pkt_sk(sk
);
2090 case NETDEV_UNREGISTER
:
2092 packet_dev_mclist(dev
, po
->mclist
, -1);
2096 if (dev
->ifindex
== po
->ifindex
) {
2097 spin_lock(&po
->bind_lock
);
2099 __dev_remove_pack(&po
->prot_hook
);
2102 sk
->sk_err
= ENETDOWN
;
2103 if (!sock_flag(sk
, SOCK_DEAD
))
2104 sk
->sk_error_report(sk
);
2106 if (msg
== NETDEV_UNREGISTER
) {
2108 po
->prot_hook
.dev
= NULL
;
2110 spin_unlock(&po
->bind_lock
);
2114 if (dev
->ifindex
== po
->ifindex
) {
2115 spin_lock(&po
->bind_lock
);
2116 if (po
->num
&& !po
->running
) {
2117 dev_add_pack(&po
->prot_hook
);
2121 spin_unlock(&po
->bind_lock
);
2131 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
2134 struct sock
*sk
= sock
->sk
;
2139 int amount
= sk_wmem_alloc_get(sk
);
2141 return put_user(amount
, (int __user
*)arg
);
2145 struct sk_buff
*skb
;
2148 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2149 skb
= skb_peek(&sk
->sk_receive_queue
);
2152 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2153 return put_user(amount
, (int __user
*)arg
);
2156 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
2158 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
2168 case SIOCGIFBRDADDR
:
2169 case SIOCSIFBRDADDR
:
2170 case SIOCGIFNETMASK
:
2171 case SIOCSIFNETMASK
:
2172 case SIOCGIFDSTADDR
:
2173 case SIOCSIFDSTADDR
:
2175 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
2179 return -ENOIOCTLCMD
;
2184 static unsigned int packet_poll(struct file
*file
, struct socket
*sock
,
2187 struct sock
*sk
= sock
->sk
;
2188 struct packet_sock
*po
= pkt_sk(sk
);
2189 unsigned int mask
= datagram_poll(file
, sock
, wait
);
2191 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2192 if (po
->rx_ring
.pg_vec
) {
2193 if (!packet_previous_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
))
2194 mask
|= POLLIN
| POLLRDNORM
;
2196 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2197 spin_lock_bh(&sk
->sk_write_queue
.lock
);
2198 if (po
->tx_ring
.pg_vec
) {
2199 if (packet_current_frame(po
, &po
->tx_ring
, TP_STATUS_AVAILABLE
))
2200 mask
|= POLLOUT
| POLLWRNORM
;
2202 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
2207 /* Dirty? Well, I still did not learn better way to account
2211 static void packet_mm_open(struct vm_area_struct
*vma
)
2213 struct file
*file
= vma
->vm_file
;
2214 struct socket
*sock
= file
->private_data
;
2215 struct sock
*sk
= sock
->sk
;
2218 atomic_inc(&pkt_sk(sk
)->mapped
);
2221 static void packet_mm_close(struct vm_area_struct
*vma
)
2223 struct file
*file
= vma
->vm_file
;
2224 struct socket
*sock
= file
->private_data
;
2225 struct sock
*sk
= sock
->sk
;
2228 atomic_dec(&pkt_sk(sk
)->mapped
);
2231 static const struct vm_operations_struct packet_mmap_ops
= {
2232 .open
= packet_mm_open
,
2233 .close
= packet_mm_close
,
2236 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
2240 for (i
= 0; i
< len
; i
++) {
2241 if (likely(pg_vec
[i
]))
2242 free_pages((unsigned long) pg_vec
[i
], order
);
2247 static inline char *alloc_one_pg_vec_page(unsigned long order
)
2249 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
| __GFP_NOWARN
;
2251 return (char *) __get_free_pages(gfp_flags
, order
);
2254 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
2256 unsigned int block_nr
= req
->tp_block_nr
;
2260 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
2261 if (unlikely(!pg_vec
))
2264 for (i
= 0; i
< block_nr
; i
++) {
2265 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
2266 if (unlikely(!pg_vec
[i
]))
2267 goto out_free_pgvec
;
2274 free_pg_vec(pg_vec
, order
, block_nr
);
2279 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
2280 int closing
, int tx_ring
)
2282 char **pg_vec
= NULL
;
2283 struct packet_sock
*po
= pkt_sk(sk
);
2284 int was_running
, order
= 0;
2285 struct packet_ring_buffer
*rb
;
2286 struct sk_buff_head
*rb_queue
;
2290 rb
= tx_ring
? &po
->tx_ring
: &po
->rx_ring
;
2291 rb_queue
= tx_ring
? &sk
->sk_write_queue
: &sk
->sk_receive_queue
;
2295 if (atomic_read(&po
->mapped
))
2297 if (atomic_read(&rb
->pending
))
2301 if (req
->tp_block_nr
) {
2302 /* Sanity tests and some calculations */
2304 if (unlikely(rb
->pg_vec
))
2307 switch (po
->tp_version
) {
2309 po
->tp_hdrlen
= TPACKET_HDRLEN
;
2312 po
->tp_hdrlen
= TPACKET2_HDRLEN
;
2317 if (unlikely((int)req
->tp_block_size
<= 0))
2319 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
2321 if (unlikely(req
->tp_frame_size
< po
->tp_hdrlen
+
2324 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
2327 rb
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
2328 if (unlikely(rb
->frames_per_block
<= 0))
2330 if (unlikely((rb
->frames_per_block
* req
->tp_block_nr
) !=
2335 order
= get_order(req
->tp_block_size
);
2336 pg_vec
= alloc_pg_vec(req
, order
);
2337 if (unlikely(!pg_vec
))
2343 if (unlikely(req
->tp_frame_nr
))
2349 /* Detach socket from network */
2350 spin_lock(&po
->bind_lock
);
2351 was_running
= po
->running
;
2354 __dev_remove_pack(&po
->prot_hook
);
2359 spin_unlock(&po
->bind_lock
);
2364 mutex_lock(&po
->pg_vec_lock
);
2365 if (closing
|| atomic_read(&po
->mapped
) == 0) {
2367 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
2368 spin_lock_bh(&rb_queue
->lock
);
2369 pg_vec
= XC(rb
->pg_vec
, pg_vec
);
2370 rb
->frame_max
= (req
->tp_frame_nr
- 1);
2372 rb
->frame_size
= req
->tp_frame_size
;
2373 spin_unlock_bh(&rb_queue
->lock
);
2375 order
= XC(rb
->pg_vec_order
, order
);
2376 req
->tp_block_nr
= XC(rb
->pg_vec_len
, req
->tp_block_nr
);
2378 rb
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
2379 po
->prot_hook
.func
= (po
->rx_ring
.pg_vec
) ?
2380 tpacket_rcv
: packet_rcv
;
2381 skb_queue_purge(rb_queue
);
2383 if (atomic_read(&po
->mapped
))
2384 pr_err("packet_mmap: vma is busy: %d\n",
2385 atomic_read(&po
->mapped
));
2387 mutex_unlock(&po
->pg_vec_lock
);
2389 spin_lock(&po
->bind_lock
);
2390 if (was_running
&& !po
->running
) {
2394 dev_add_pack(&po
->prot_hook
);
2396 spin_unlock(&po
->bind_lock
);
2401 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
2406 static int packet_mmap(struct file
*file
, struct socket
*sock
,
2407 struct vm_area_struct
*vma
)
2409 struct sock
*sk
= sock
->sk
;
2410 struct packet_sock
*po
= pkt_sk(sk
);
2411 unsigned long size
, expected_size
;
2412 struct packet_ring_buffer
*rb
;
2413 unsigned long start
;
2420 mutex_lock(&po
->pg_vec_lock
);
2423 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2425 expected_size
+= rb
->pg_vec_len
2431 if (expected_size
== 0)
2434 size
= vma
->vm_end
- vma
->vm_start
;
2435 if (size
!= expected_size
)
2438 start
= vma
->vm_start
;
2439 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2440 if (rb
->pg_vec
== NULL
)
2443 for (i
= 0; i
< rb
->pg_vec_len
; i
++) {
2444 struct page
*page
= virt_to_page(rb
->pg_vec
[i
]);
2447 for (pg_num
= 0; pg_num
< rb
->pg_vec_pages
;
2449 err
= vm_insert_page(vma
, start
, page
);
2457 atomic_inc(&po
->mapped
);
2458 vma
->vm_ops
= &packet_mmap_ops
;
2462 mutex_unlock(&po
->pg_vec_lock
);
2466 static const struct proto_ops packet_ops_spkt
= {
2467 .family
= PF_PACKET
,
2468 .owner
= THIS_MODULE
,
2469 .release
= packet_release
,
2470 .bind
= packet_bind_spkt
,
2471 .connect
= sock_no_connect
,
2472 .socketpair
= sock_no_socketpair
,
2473 .accept
= sock_no_accept
,
2474 .getname
= packet_getname_spkt
,
2475 .poll
= datagram_poll
,
2476 .ioctl
= packet_ioctl
,
2477 .listen
= sock_no_listen
,
2478 .shutdown
= sock_no_shutdown
,
2479 .setsockopt
= sock_no_setsockopt
,
2480 .getsockopt
= sock_no_getsockopt
,
2481 .sendmsg
= packet_sendmsg_spkt
,
2482 .recvmsg
= packet_recvmsg
,
2483 .mmap
= sock_no_mmap
,
2484 .sendpage
= sock_no_sendpage
,
2487 static const struct proto_ops packet_ops
= {
2488 .family
= PF_PACKET
,
2489 .owner
= THIS_MODULE
,
2490 .release
= packet_release
,
2491 .bind
= packet_bind
,
2492 .connect
= sock_no_connect
,
2493 .socketpair
= sock_no_socketpair
,
2494 .accept
= sock_no_accept
,
2495 .getname
= packet_getname
,
2496 .poll
= packet_poll
,
2497 .ioctl
= packet_ioctl
,
2498 .listen
= sock_no_listen
,
2499 .shutdown
= sock_no_shutdown
,
2500 .setsockopt
= packet_setsockopt
,
2501 .getsockopt
= packet_getsockopt
,
2502 .sendmsg
= packet_sendmsg
,
2503 .recvmsg
= packet_recvmsg
,
2504 .mmap
= packet_mmap
,
2505 .sendpage
= sock_no_sendpage
,
2508 static const struct net_proto_family packet_family_ops
= {
2509 .family
= PF_PACKET
,
2510 .create
= packet_create
,
2511 .owner
= THIS_MODULE
,
2514 static struct notifier_block packet_netdev_notifier
= {
2515 .notifier_call
= packet_notifier
,
2518 #ifdef CONFIG_PROC_FS
2520 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2523 struct net
*net
= seq_file_net(seq
);
2526 return seq_hlist_start_head_rcu(&net
->packet
.sklist
, *pos
);
2529 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2531 struct net
*net
= seq_file_net(seq
);
2532 return seq_hlist_next_rcu(v
, &net
->packet
.sklist
, pos
);
2535 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
2541 static int packet_seq_show(struct seq_file
*seq
, void *v
)
2543 if (v
== SEQ_START_TOKEN
)
2544 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2546 struct sock
*s
= sk_entry(v
);
2547 const struct packet_sock
*po
= pkt_sk(s
);
2550 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2552 atomic_read(&s
->sk_refcnt
),
2557 atomic_read(&s
->sk_rmem_alloc
),
2565 static const struct seq_operations packet_seq_ops
= {
2566 .start
= packet_seq_start
,
2567 .next
= packet_seq_next
,
2568 .stop
= packet_seq_stop
,
2569 .show
= packet_seq_show
,
2572 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
2574 return seq_open_net(inode
, file
, &packet_seq_ops
,
2575 sizeof(struct seq_net_private
));
2578 static const struct file_operations packet_seq_fops
= {
2579 .owner
= THIS_MODULE
,
2580 .open
= packet_seq_open
,
2582 .llseek
= seq_lseek
,
2583 .release
= seq_release_net
,
2588 static int __net_init
packet_net_init(struct net
*net
)
2590 spin_lock_init(&net
->packet
.sklist_lock
);
2591 INIT_HLIST_HEAD(&net
->packet
.sklist
);
2593 if (!proc_net_fops_create(net
, "packet", 0, &packet_seq_fops
))
2599 static void __net_exit
packet_net_exit(struct net
*net
)
2601 proc_net_remove(net
, "packet");
2604 static struct pernet_operations packet_net_ops
= {
2605 .init
= packet_net_init
,
2606 .exit
= packet_net_exit
,
2610 static void __exit
packet_exit(void)
2612 unregister_netdevice_notifier(&packet_netdev_notifier
);
2613 unregister_pernet_subsys(&packet_net_ops
);
2614 sock_unregister(PF_PACKET
);
2615 proto_unregister(&packet_proto
);
2618 static int __init
packet_init(void)
2620 int rc
= proto_register(&packet_proto
, 0);
2625 sock_register(&packet_family_ops
);
2626 register_pernet_subsys(&packet_net_ops
);
2627 register_netdevice_notifier(&packet_netdev_notifier
);
2632 module_init(packet_init
);
2633 module_exit(packet_exit
);
2634 MODULE_LICENSE("GPL");
2635 MODULE_ALIAS_NETPROTO(PF_PACKET
);