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>
85 #include <linux/errqueue.h>
86 #include <linux/net_tstamp.h>
89 #include <net/inet_common.h>
94 - if device has no dev->hard_header routine, it adds and removes ll header
95 inside itself. In this case ll header is invisible outside of device,
96 but higher levels still should reserve dev->hard_header_len.
97 Some devices are enough clever to reallocate skb, when header
98 will not fit to reserved space (tunnel), another ones are silly
100 - packet socket receives packets with pulled ll header,
101 so that SOCK_RAW should push it back.
106 Incoming, dev->hard_header!=NULL
107 mac_header -> ll header
110 Outgoing, dev->hard_header!=NULL
111 mac_header -> ll header
114 Incoming, dev->hard_header==NULL
115 mac_header -> UNKNOWN position. It is very likely, that it points to ll
116 header. PPP makes it, that is wrong, because introduce
117 assymetry between rx and tx paths.
120 Outgoing, dev->hard_header==NULL
121 mac_header -> data. ll header is still not built!
125 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
131 dev->hard_header != NULL
132 mac_header -> ll header
135 dev->hard_header == NULL (ll header is added by device, we cannot control it)
139 We should set nh.raw on output to correct posistion,
140 packet classifier depends on it.
143 /* Private packet socket structures. */
145 struct packet_mclist
{
146 struct packet_mclist
*next
;
151 unsigned char addr
[MAX_ADDR_LEN
];
153 /* identical to struct packet_mreq except it has
154 * a longer address field.
156 struct packet_mreq_max
{
158 unsigned short mr_type
;
159 unsigned short mr_alen
;
160 unsigned char mr_address
[MAX_ADDR_LEN
];
163 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
164 int closing
, int tx_ring
);
166 struct packet_ring_buffer
{
169 unsigned int frames_per_block
;
170 unsigned int frame_size
;
171 unsigned int frame_max
;
173 unsigned int pg_vec_order
;
174 unsigned int pg_vec_pages
;
175 unsigned int pg_vec_len
;
181 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
);
183 static void packet_flush_mclist(struct sock
*sk
);
186 /* struct sock has to be the first member of packet_sock */
188 struct tpacket_stats stats
;
189 struct packet_ring_buffer rx_ring
;
190 struct packet_ring_buffer tx_ring
;
192 spinlock_t bind_lock
;
193 struct mutex pg_vec_lock
;
194 unsigned int running
:1, /* prot_hook is attached*/
198 int ifindex
; /* bound device */
200 struct packet_mclist
*mclist
;
202 enum tpacket_versions tp_version
;
203 unsigned int tp_hdrlen
;
204 unsigned int tp_reserve
;
205 unsigned int tp_loss
:1;
206 unsigned int tp_tstamp
;
207 struct packet_type prot_hook ____cacheline_aligned_in_smp
;
210 struct packet_skb_cb
{
211 unsigned int origlen
;
213 struct sockaddr_pkt pkt
;
214 struct sockaddr_ll ll
;
218 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
220 static void __packet_set_status(struct packet_sock
*po
, void *frame
, int status
)
223 struct tpacket_hdr
*h1
;
224 struct tpacket2_hdr
*h2
;
229 switch (po
->tp_version
) {
231 h
.h1
->tp_status
= status
;
232 flush_dcache_page(virt_to_page(&h
.h1
->tp_status
));
235 h
.h2
->tp_status
= status
;
236 flush_dcache_page(virt_to_page(&h
.h2
->tp_status
));
239 pr_err("TPACKET version not supported\n");
246 static int __packet_get_status(struct packet_sock
*po
, void *frame
)
249 struct tpacket_hdr
*h1
;
250 struct tpacket2_hdr
*h2
;
257 switch (po
->tp_version
) {
259 flush_dcache_page(virt_to_page(&h
.h1
->tp_status
));
260 return h
.h1
->tp_status
;
262 flush_dcache_page(virt_to_page(&h
.h2
->tp_status
));
263 return h
.h2
->tp_status
;
265 pr_err("TPACKET version not supported\n");
271 static void *packet_lookup_frame(struct packet_sock
*po
,
272 struct packet_ring_buffer
*rb
,
273 unsigned int position
,
276 unsigned int pg_vec_pos
, frame_offset
;
278 struct tpacket_hdr
*h1
;
279 struct tpacket2_hdr
*h2
;
283 pg_vec_pos
= position
/ rb
->frames_per_block
;
284 frame_offset
= position
% rb
->frames_per_block
;
286 h
.raw
= rb
->pg_vec
[pg_vec_pos
] + (frame_offset
* rb
->frame_size
);
288 if (status
!= __packet_get_status(po
, h
.raw
))
294 static inline void *packet_current_frame(struct packet_sock
*po
,
295 struct packet_ring_buffer
*rb
,
298 return packet_lookup_frame(po
, rb
, rb
->head
, status
);
301 static inline void *packet_previous_frame(struct packet_sock
*po
,
302 struct packet_ring_buffer
*rb
,
305 unsigned int previous
= rb
->head
? rb
->head
- 1 : rb
->frame_max
;
306 return packet_lookup_frame(po
, rb
, previous
, status
);
309 static inline void packet_increment_head(struct packet_ring_buffer
*buff
)
311 buff
->head
= buff
->head
!= buff
->frame_max
? buff
->head
+1 : 0;
314 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
316 return (struct packet_sock
*)sk
;
319 static void packet_sock_destruct(struct sock
*sk
)
321 skb_queue_purge(&sk
->sk_error_queue
);
323 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
324 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
326 if (!sock_flag(sk
, SOCK_DEAD
)) {
327 pr_err("Attempt to release alive packet socket: %p\n", sk
);
331 sk_refcnt_debug_dec(sk
);
335 static const struct proto_ops packet_ops
;
337 static const struct proto_ops packet_ops_spkt
;
339 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
,
340 struct packet_type
*pt
, struct net_device
*orig_dev
)
343 struct sockaddr_pkt
*spkt
;
346 * When we registered the protocol we saved the socket in the data
347 * field for just this event.
350 sk
= pt
->af_packet_priv
;
353 * Yank back the headers [hope the device set this
354 * right or kerboom...]
356 * Incoming packets have ll header pulled,
359 * For outgoing ones skb->data == skb_mac_header(skb)
360 * so that this procedure is noop.
363 if (skb
->pkt_type
== PACKET_LOOPBACK
)
366 if (!net_eq(dev_net(dev
), sock_net(sk
)))
369 skb
= skb_share_check(skb
, GFP_ATOMIC
);
373 /* drop any routing info */
376 /* drop conntrack reference */
379 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
381 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
384 * The SOCK_PACKET socket receives _all_ frames.
387 spkt
->spkt_family
= dev
->type
;
388 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
389 spkt
->spkt_protocol
= skb
->protocol
;
392 * Charge the memory to the socket. This is done specifically
393 * to prevent sockets using all the memory up.
396 if (sock_queue_rcv_skb(sk
, skb
) == 0)
407 * Output a raw packet to a device layer. This bypasses all the other
408 * protocol layers and you must therefore supply it with a complete frame
411 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
412 struct msghdr
*msg
, size_t len
)
414 struct sock
*sk
= sock
->sk
;
415 struct sockaddr_pkt
*saddr
= (struct sockaddr_pkt
*)msg
->msg_name
;
416 struct sk_buff
*skb
= NULL
;
417 struct net_device
*dev
;
422 * Get and verify the address.
426 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
428 if (msg
->msg_namelen
== sizeof(struct sockaddr_pkt
))
429 proto
= saddr
->spkt_protocol
;
431 return -ENOTCONN
; /* SOCK_PACKET must be sent giving an address */
434 * Find the device first to size check it
437 saddr
->spkt_device
[13] = 0;
440 dev
= dev_get_by_name_rcu(sock_net(sk
), saddr
->spkt_device
);
446 if (!(dev
->flags
& IFF_UP
))
450 * You may not queue a frame bigger than the mtu. This is the lowest level
451 * raw protocol and you must do your own fragmentation at this level.
455 if (len
> dev
->mtu
+ dev
->hard_header_len
)
459 size_t reserved
= LL_RESERVED_SPACE(dev
);
460 unsigned int hhlen
= dev
->header_ops
? dev
->hard_header_len
: 0;
463 skb
= sock_wmalloc(sk
, len
+ reserved
, 0, GFP_KERNEL
);
466 skb_reserve(skb
, reserved
);
467 skb_reset_network_header(skb
);
469 /* Try to align data part correctly */
474 skb_reset_network_header(skb
);
476 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
483 skb
->protocol
= proto
;
485 skb
->priority
= sk
->sk_priority
;
486 skb
->mark
= sk
->sk_mark
;
487 err
= sock_tx_timestamp(msg
, sk
, skb_tx(skb
));
502 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
505 struct sk_filter
*filter
;
508 filter
= rcu_dereference_bh(sk
->sk_filter
);
510 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
511 rcu_read_unlock_bh();
517 This function makes lazy skb cloning in hope that most of packets
518 are discarded by BPF.
520 Note tricky part: we DO mangle shared skb! skb->data, skb->len
521 and skb->cb are mangled. It works because (and until) packets
522 falling here are owned by current CPU. Output packets are cloned
523 by dev_queue_xmit_nit(), input packets are processed by net_bh
524 sequencially, so that if we return skb to original state on exit,
525 we will not harm anyone.
528 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
529 struct packet_type
*pt
, struct net_device
*orig_dev
)
532 struct sockaddr_ll
*sll
;
533 struct packet_sock
*po
;
534 u8
*skb_head
= skb
->data
;
535 int skb_len
= skb
->len
;
536 unsigned int snaplen
, res
;
538 if (skb
->pkt_type
== PACKET_LOOPBACK
)
541 sk
= pt
->af_packet_priv
;
544 if (!net_eq(dev_net(dev
), sock_net(sk
)))
549 if (dev
->header_ops
) {
550 /* The device has an explicit notion of ll header,
551 exported to higher levels.
553 Otherwise, the device hides datails of it frame
554 structure, so that corresponding packet head
555 never delivered to user.
557 if (sk
->sk_type
!= SOCK_DGRAM
)
558 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
559 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
560 /* Special case: outgoing packets have ll header at head */
561 skb_pull(skb
, skb_network_offset(skb
));
567 res
= run_filter(skb
, sk
, snaplen
);
573 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
574 (unsigned)sk
->sk_rcvbuf
)
577 if (skb_shared(skb
)) {
578 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
582 if (skb_head
!= skb
->data
) {
583 skb
->data
= skb_head
;
590 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
593 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
594 sll
->sll_family
= AF_PACKET
;
595 sll
->sll_hatype
= dev
->type
;
596 sll
->sll_protocol
= skb
->protocol
;
597 sll
->sll_pkttype
= skb
->pkt_type
;
598 if (unlikely(po
->origdev
))
599 sll
->sll_ifindex
= orig_dev
->ifindex
;
601 sll
->sll_ifindex
= dev
->ifindex
;
603 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
605 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
607 if (pskb_trim(skb
, snaplen
))
610 skb_set_owner_r(skb
, sk
);
614 /* drop conntrack reference */
617 spin_lock(&sk
->sk_receive_queue
.lock
);
618 po
->stats
.tp_packets
++;
619 skb
->dropcount
= atomic_read(&sk
->sk_drops
);
620 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
621 spin_unlock(&sk
->sk_receive_queue
.lock
);
622 sk
->sk_data_ready(sk
, skb
->len
);
626 po
->stats
.tp_drops
= atomic_inc_return(&sk
->sk_drops
);
629 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
630 skb
->data
= skb_head
;
638 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
639 struct packet_type
*pt
, struct net_device
*orig_dev
)
642 struct packet_sock
*po
;
643 struct sockaddr_ll
*sll
;
645 struct tpacket_hdr
*h1
;
646 struct tpacket2_hdr
*h2
;
649 u8
*skb_head
= skb
->data
;
650 int skb_len
= skb
->len
;
651 unsigned int snaplen
, res
;
652 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
653 unsigned short macoff
, netoff
, hdrlen
;
654 struct sk_buff
*copy_skb
= NULL
;
657 struct skb_shared_hwtstamps
*shhwtstamps
= skb_hwtstamps(skb
);
659 if (skb
->pkt_type
== PACKET_LOOPBACK
)
662 sk
= pt
->af_packet_priv
;
665 if (!net_eq(dev_net(dev
), sock_net(sk
)))
668 if (dev
->header_ops
) {
669 if (sk
->sk_type
!= SOCK_DGRAM
)
670 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
671 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
672 /* Special case: outgoing packets have ll header at head */
673 skb_pull(skb
, skb_network_offset(skb
));
677 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
678 status
|= TP_STATUS_CSUMNOTREADY
;
682 res
= run_filter(skb
, sk
, snaplen
);
688 if (sk
->sk_type
== SOCK_DGRAM
) {
689 macoff
= netoff
= TPACKET_ALIGN(po
->tp_hdrlen
) + 16 +
692 unsigned maclen
= skb_network_offset(skb
);
693 netoff
= TPACKET_ALIGN(po
->tp_hdrlen
+
694 (maclen
< 16 ? 16 : maclen
)) +
696 macoff
= netoff
- maclen
;
699 if (macoff
+ snaplen
> po
->rx_ring
.frame_size
) {
700 if (po
->copy_thresh
&&
701 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
702 (unsigned)sk
->sk_rcvbuf
) {
703 if (skb_shared(skb
)) {
704 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
706 copy_skb
= skb_get(skb
);
707 skb_head
= skb
->data
;
710 skb_set_owner_r(copy_skb
, sk
);
712 snaplen
= po
->rx_ring
.frame_size
- macoff
;
713 if ((int)snaplen
< 0)
717 spin_lock(&sk
->sk_receive_queue
.lock
);
718 h
.raw
= packet_current_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
);
721 packet_increment_head(&po
->rx_ring
);
722 po
->stats
.tp_packets
++;
724 status
|= TP_STATUS_COPY
;
725 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
727 if (!po
->stats
.tp_drops
)
728 status
&= ~TP_STATUS_LOSING
;
729 spin_unlock(&sk
->sk_receive_queue
.lock
);
731 skb_copy_bits(skb
, 0, h
.raw
+ macoff
, snaplen
);
733 switch (po
->tp_version
) {
735 h
.h1
->tp_len
= skb
->len
;
736 h
.h1
->tp_snaplen
= snaplen
;
737 h
.h1
->tp_mac
= macoff
;
738 h
.h1
->tp_net
= netoff
;
739 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
740 && shhwtstamps
->syststamp
.tv64
)
741 tv
= ktime_to_timeval(shhwtstamps
->syststamp
);
742 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
743 && shhwtstamps
->hwtstamp
.tv64
)
744 tv
= ktime_to_timeval(shhwtstamps
->hwtstamp
);
745 else if (skb
->tstamp
.tv64
)
746 tv
= ktime_to_timeval(skb
->tstamp
);
748 do_gettimeofday(&tv
);
749 h
.h1
->tp_sec
= tv
.tv_sec
;
750 h
.h1
->tp_usec
= tv
.tv_usec
;
751 hdrlen
= sizeof(*h
.h1
);
754 h
.h2
->tp_len
= skb
->len
;
755 h
.h2
->tp_snaplen
= snaplen
;
756 h
.h2
->tp_mac
= macoff
;
757 h
.h2
->tp_net
= netoff
;
758 if ((po
->tp_tstamp
& SOF_TIMESTAMPING_SYS_HARDWARE
)
759 && shhwtstamps
->syststamp
.tv64
)
760 ts
= ktime_to_timespec(shhwtstamps
->syststamp
);
761 else if ((po
->tp_tstamp
& SOF_TIMESTAMPING_RAW_HARDWARE
)
762 && shhwtstamps
->hwtstamp
.tv64
)
763 ts
= ktime_to_timespec(shhwtstamps
->hwtstamp
);
764 else if (skb
->tstamp
.tv64
)
765 ts
= ktime_to_timespec(skb
->tstamp
);
768 h
.h2
->tp_sec
= ts
.tv_sec
;
769 h
.h2
->tp_nsec
= ts
.tv_nsec
;
770 h
.h2
->tp_vlan_tci
= vlan_tx_tag_get(skb
);
771 hdrlen
= sizeof(*h
.h2
);
777 sll
= h
.raw
+ TPACKET_ALIGN(hdrlen
);
778 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
779 sll
->sll_family
= AF_PACKET
;
780 sll
->sll_hatype
= dev
->type
;
781 sll
->sll_protocol
= skb
->protocol
;
782 sll
->sll_pkttype
= skb
->pkt_type
;
783 if (unlikely(po
->origdev
))
784 sll
->sll_ifindex
= orig_dev
->ifindex
;
786 sll
->sll_ifindex
= dev
->ifindex
;
788 __packet_set_status(po
, h
.raw
, status
);
791 struct page
*p_start
, *p_end
;
792 u8
*h_end
= h
.raw
+ macoff
+ snaplen
- 1;
794 p_start
= virt_to_page(h
.raw
);
795 p_end
= virt_to_page(h_end
);
796 while (p_start
<= p_end
) {
797 flush_dcache_page(p_start
);
802 sk
->sk_data_ready(sk
, 0);
805 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
806 skb
->data
= skb_head
;
814 po
->stats
.tp_drops
++;
815 spin_unlock(&sk
->sk_receive_queue
.lock
);
817 sk
->sk_data_ready(sk
, 0);
822 static void tpacket_destruct_skb(struct sk_buff
*skb
)
824 struct packet_sock
*po
= pkt_sk(skb
->sk
);
829 if (likely(po
->tx_ring
.pg_vec
)) {
830 ph
= skb_shinfo(skb
)->destructor_arg
;
831 BUG_ON(__packet_get_status(po
, ph
) != TP_STATUS_SENDING
);
832 BUG_ON(atomic_read(&po
->tx_ring
.pending
) == 0);
833 atomic_dec(&po
->tx_ring
.pending
);
834 __packet_set_status(po
, ph
, TP_STATUS_AVAILABLE
);
840 static int tpacket_fill_skb(struct packet_sock
*po
, struct sk_buff
*skb
,
841 void *frame
, struct net_device
*dev
, int size_max
,
842 __be16 proto
, unsigned char *addr
)
845 struct tpacket_hdr
*h1
;
846 struct tpacket2_hdr
*h2
;
849 int to_write
, offset
, len
, tp_len
, nr_frags
, len_max
;
850 struct socket
*sock
= po
->sk
.sk_socket
;
857 skb
->protocol
= proto
;
859 skb
->priority
= po
->sk
.sk_priority
;
860 skb
->mark
= po
->sk
.sk_mark
;
861 skb_shinfo(skb
)->destructor_arg
= ph
.raw
;
863 switch (po
->tp_version
) {
865 tp_len
= ph
.h2
->tp_len
;
868 tp_len
= ph
.h1
->tp_len
;
871 if (unlikely(tp_len
> size_max
)) {
872 pr_err("packet size is too long (%d > %d)\n", tp_len
, size_max
);
876 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
877 skb_reset_network_header(skb
);
879 data
= ph
.raw
+ po
->tp_hdrlen
- sizeof(struct sockaddr_ll
);
882 if (sock
->type
== SOCK_DGRAM
) {
883 err
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
,
885 if (unlikely(err
< 0))
887 } else if (dev
->hard_header_len
) {
888 /* net device doesn't like empty head */
889 if (unlikely(tp_len
<= dev
->hard_header_len
)) {
890 pr_err("packet size is too short (%d < %d)\n",
891 tp_len
, dev
->hard_header_len
);
895 skb_push(skb
, dev
->hard_header_len
);
896 err
= skb_store_bits(skb
, 0, data
,
897 dev
->hard_header_len
);
901 data
+= dev
->hard_header_len
;
902 to_write
-= dev
->hard_header_len
;
906 page
= virt_to_page(data
);
907 offset
= offset_in_page(data
);
908 len_max
= PAGE_SIZE
- offset
;
909 len
= ((to_write
> len_max
) ? len_max
: to_write
);
911 skb
->data_len
= to_write
;
912 skb
->len
+= to_write
;
913 skb
->truesize
+= to_write
;
914 atomic_add(to_write
, &po
->sk
.sk_wmem_alloc
);
916 while (likely(to_write
)) {
917 nr_frags
= skb_shinfo(skb
)->nr_frags
;
919 if (unlikely(nr_frags
>= MAX_SKB_FRAGS
)) {
920 pr_err("Packet exceed the number of skb frags(%lu)\n",
925 flush_dcache_page(page
);
927 skb_fill_page_desc(skb
,
929 page
++, offset
, len
);
933 len
= ((to_write
> len_max
) ? len_max
: to_write
);
939 static int tpacket_snd(struct packet_sock
*po
, struct msghdr
*msg
)
943 struct net_device
*dev
;
945 int ifindex
, err
, reserve
= 0;
947 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
948 int tp_len
, size_max
;
953 sock
= po
->sk
.sk_socket
;
955 mutex_lock(&po
->pg_vec_lock
);
959 ifindex
= po
->ifindex
;
964 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
966 if (msg
->msg_namelen
< (saddr
->sll_halen
967 + offsetof(struct sockaddr_ll
,
970 ifindex
= saddr
->sll_ifindex
;
971 proto
= saddr
->sll_protocol
;
972 addr
= saddr
->sll_addr
;
975 dev
= dev_get_by_index(sock_net(&po
->sk
), ifindex
);
977 if (unlikely(dev
== NULL
))
980 reserve
= dev
->hard_header_len
;
983 if (unlikely(!(dev
->flags
& IFF_UP
)))
986 size_max
= po
->tx_ring
.frame_size
987 - (po
->tp_hdrlen
- sizeof(struct sockaddr_ll
));
989 if (size_max
> dev
->mtu
+ reserve
)
990 size_max
= dev
->mtu
+ reserve
;
993 ph
= packet_current_frame(po
, &po
->tx_ring
,
994 TP_STATUS_SEND_REQUEST
);
996 if (unlikely(ph
== NULL
)) {
1001 status
= TP_STATUS_SEND_REQUEST
;
1002 skb
= sock_alloc_send_skb(&po
->sk
,
1003 LL_ALLOCATED_SPACE(dev
)
1004 + sizeof(struct sockaddr_ll
),
1007 if (unlikely(skb
== NULL
))
1010 tp_len
= tpacket_fill_skb(po
, skb
, ph
, dev
, size_max
, proto
,
1013 if (unlikely(tp_len
< 0)) {
1015 __packet_set_status(po
, ph
,
1016 TP_STATUS_AVAILABLE
);
1017 packet_increment_head(&po
->tx_ring
);
1021 status
= TP_STATUS_WRONG_FORMAT
;
1027 skb
->destructor
= tpacket_destruct_skb
;
1028 __packet_set_status(po
, ph
, TP_STATUS_SENDING
);
1029 atomic_inc(&po
->tx_ring
.pending
);
1031 status
= TP_STATUS_SEND_REQUEST
;
1032 err
= dev_queue_xmit(skb
);
1033 if (unlikely(err
> 0)) {
1034 err
= net_xmit_errno(err
);
1035 if (err
&& __packet_get_status(po
, ph
) ==
1036 TP_STATUS_AVAILABLE
) {
1037 /* skb was destructed already */
1042 * skb was dropped but not destructed yet;
1043 * let's treat it like congestion or err < 0
1047 packet_increment_head(&po
->tx_ring
);
1049 } while (likely((ph
!= NULL
) ||
1050 ((!(msg
->msg_flags
& MSG_DONTWAIT
)) &&
1051 (atomic_read(&po
->tx_ring
.pending
))))
1058 __packet_set_status(po
, ph
, status
);
1063 mutex_unlock(&po
->pg_vec_lock
);
1067 static inline struct sk_buff
*packet_alloc_skb(struct sock
*sk
, size_t prepad
,
1068 size_t reserve
, size_t len
,
1069 size_t linear
, int noblock
,
1072 struct sk_buff
*skb
;
1074 /* Under a page? Don't bother with paged skb. */
1075 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
1078 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
1083 skb_reserve(skb
, reserve
);
1084 skb_put(skb
, linear
);
1085 skb
->data_len
= len
- linear
;
1086 skb
->len
+= len
- linear
;
1091 static int packet_snd(struct socket
*sock
,
1092 struct msghdr
*msg
, size_t len
)
1094 struct sock
*sk
= sock
->sk
;
1095 struct sockaddr_ll
*saddr
= (struct sockaddr_ll
*)msg
->msg_name
;
1096 struct sk_buff
*skb
;
1097 struct net_device
*dev
;
1099 unsigned char *addr
;
1100 int ifindex
, err
, reserve
= 0;
1101 struct virtio_net_hdr vnet_hdr
= { 0 };
1104 struct packet_sock
*po
= pkt_sk(sk
);
1105 unsigned short gso_type
= 0;
1108 * Get and verify the address.
1111 if (saddr
== NULL
) {
1112 ifindex
= po
->ifindex
;
1117 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
1119 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
1121 ifindex
= saddr
->sll_ifindex
;
1122 proto
= saddr
->sll_protocol
;
1123 addr
= saddr
->sll_addr
;
1127 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
1131 if (sock
->type
== SOCK_RAW
)
1132 reserve
= dev
->hard_header_len
;
1135 if (!(dev
->flags
& IFF_UP
))
1138 if (po
->has_vnet_hdr
) {
1139 vnet_hdr_len
= sizeof(vnet_hdr
);
1142 if (len
< vnet_hdr_len
)
1145 len
-= vnet_hdr_len
;
1147 err
= memcpy_fromiovec((void *)&vnet_hdr
, msg
->msg_iov
,
1152 if ((vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
1153 (vnet_hdr
.csum_start
+ vnet_hdr
.csum_offset
+ 2 >
1155 vnet_hdr
.hdr_len
= vnet_hdr
.csum_start
+
1156 vnet_hdr
.csum_offset
+ 2;
1159 if (vnet_hdr
.hdr_len
> len
)
1162 if (vnet_hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
1163 switch (vnet_hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
1164 case VIRTIO_NET_HDR_GSO_TCPV4
:
1165 gso_type
= SKB_GSO_TCPV4
;
1167 case VIRTIO_NET_HDR_GSO_TCPV6
:
1168 gso_type
= SKB_GSO_TCPV6
;
1170 case VIRTIO_NET_HDR_GSO_UDP
:
1171 gso_type
= SKB_GSO_UDP
;
1177 if (vnet_hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
1178 gso_type
|= SKB_GSO_TCP_ECN
;
1180 if (vnet_hdr
.gso_size
== 0)
1187 if (!gso_type
&& (len
> dev
->mtu
+reserve
))
1191 skb
= packet_alloc_skb(sk
, LL_ALLOCATED_SPACE(dev
),
1192 LL_RESERVED_SPACE(dev
), len
, vnet_hdr
.hdr_len
,
1193 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1197 skb_set_network_header(skb
, reserve
);
1200 if (sock
->type
== SOCK_DGRAM
&&
1201 (offset
= dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
)) < 0)
1204 /* Returns -EFAULT on error */
1205 err
= skb_copy_datagram_from_iovec(skb
, offset
, msg
->msg_iov
, 0, len
);
1208 err
= sock_tx_timestamp(msg
, sk
, skb_tx(skb
));
1212 skb
->protocol
= proto
;
1214 skb
->priority
= sk
->sk_priority
;
1215 skb
->mark
= sk
->sk_mark
;
1217 if (po
->has_vnet_hdr
) {
1218 if (vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
1219 if (!skb_partial_csum_set(skb
, vnet_hdr
.csum_start
,
1220 vnet_hdr
.csum_offset
)) {
1226 skb_shinfo(skb
)->gso_size
= vnet_hdr
.gso_size
;
1227 skb_shinfo(skb
)->gso_type
= gso_type
;
1229 /* Header must be checked, and gso_segs computed. */
1230 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
1231 skb_shinfo(skb
)->gso_segs
= 0;
1233 len
+= vnet_hdr_len
;
1240 err
= dev_queue_xmit(skb
);
1241 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
1257 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1258 struct msghdr
*msg
, size_t len
)
1260 struct sock
*sk
= sock
->sk
;
1261 struct packet_sock
*po
= pkt_sk(sk
);
1262 if (po
->tx_ring
.pg_vec
)
1263 return tpacket_snd(po
, msg
);
1265 return packet_snd(sock
, msg
, len
);
1269 * Close a PACKET socket. This is fairly simple. We immediately go
1270 * to 'closed' state and remove our protocol entry in the device list.
1273 static int packet_release(struct socket
*sock
)
1275 struct sock
*sk
= sock
->sk
;
1276 struct packet_sock
*po
;
1278 struct tpacket_req req
;
1286 spin_lock_bh(&net
->packet
.sklist_lock
);
1287 sk_del_node_init_rcu(sk
);
1288 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
1289 spin_unlock_bh(&net
->packet
.sklist_lock
);
1291 spin_lock(&po
->bind_lock
);
1294 * Remove from protocol table
1298 __dev_remove_pack(&po
->prot_hook
);
1301 spin_unlock(&po
->bind_lock
);
1303 packet_flush_mclist(sk
);
1305 memset(&req
, 0, sizeof(req
));
1307 if (po
->rx_ring
.pg_vec
)
1308 packet_set_ring(sk
, &req
, 1, 0);
1310 if (po
->tx_ring
.pg_vec
)
1311 packet_set_ring(sk
, &req
, 1, 1);
1315 * Now the socket is dead. No more input will appear.
1322 skb_queue_purge(&sk
->sk_receive_queue
);
1323 sk_refcnt_debug_release(sk
);
1330 * Attach a packet hook.
1333 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
1335 struct packet_sock
*po
= pkt_sk(sk
);
1337 * Detach an existing hook if present.
1342 spin_lock(&po
->bind_lock
);
1347 spin_unlock(&po
->bind_lock
);
1348 dev_remove_pack(&po
->prot_hook
);
1349 spin_lock(&po
->bind_lock
);
1353 po
->prot_hook
.type
= protocol
;
1354 po
->prot_hook
.dev
= dev
;
1356 po
->ifindex
= dev
? dev
->ifindex
: 0;
1361 if (!dev
|| (dev
->flags
& IFF_UP
)) {
1362 dev_add_pack(&po
->prot_hook
);
1366 sk
->sk_err
= ENETDOWN
;
1367 if (!sock_flag(sk
, SOCK_DEAD
))
1368 sk
->sk_error_report(sk
);
1372 spin_unlock(&po
->bind_lock
);
1378 * Bind a packet socket to a device
1381 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1384 struct sock
*sk
= sock
->sk
;
1386 struct net_device
*dev
;
1393 if (addr_len
!= sizeof(struct sockaddr
))
1395 strlcpy(name
, uaddr
->sa_data
, sizeof(name
));
1397 dev
= dev_get_by_name(sock_net(sk
), name
);
1399 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
1405 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
1407 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1408 struct sock
*sk
= sock
->sk
;
1409 struct net_device
*dev
= NULL
;
1417 if (addr_len
< sizeof(struct sockaddr_ll
))
1419 if (sll
->sll_family
!= AF_PACKET
)
1422 if (sll
->sll_ifindex
) {
1424 dev
= dev_get_by_index(sock_net(sk
), sll
->sll_ifindex
);
1428 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
1436 static struct proto packet_proto
= {
1438 .owner
= THIS_MODULE
,
1439 .obj_size
= sizeof(struct packet_sock
),
1443 * Create a packet of type SOCK_PACKET.
1446 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
,
1450 struct packet_sock
*po
;
1451 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1454 if (!capable(CAP_NET_RAW
))
1456 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
1457 sock
->type
!= SOCK_PACKET
)
1458 return -ESOCKTNOSUPPORT
;
1460 sock
->state
= SS_UNCONNECTED
;
1463 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
1467 sock
->ops
= &packet_ops
;
1468 if (sock
->type
== SOCK_PACKET
)
1469 sock
->ops
= &packet_ops_spkt
;
1471 sock_init_data(sock
, sk
);
1474 sk
->sk_family
= PF_PACKET
;
1477 sk
->sk_destruct
= packet_sock_destruct
;
1478 sk_refcnt_debug_inc(sk
);
1481 * Attach a protocol block
1484 spin_lock_init(&po
->bind_lock
);
1485 mutex_init(&po
->pg_vec_lock
);
1486 po
->prot_hook
.func
= packet_rcv
;
1488 if (sock
->type
== SOCK_PACKET
)
1489 po
->prot_hook
.func
= packet_rcv_spkt
;
1491 po
->prot_hook
.af_packet_priv
= sk
;
1494 po
->prot_hook
.type
= proto
;
1495 dev_add_pack(&po
->prot_hook
);
1500 spin_lock_bh(&net
->packet
.sklist_lock
);
1501 sk_add_node_rcu(sk
, &net
->packet
.sklist
);
1502 sock_prot_inuse_add(net
, &packet_proto
, 1);
1503 spin_unlock_bh(&net
->packet
.sklist_lock
);
1510 static int packet_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
1512 struct sock_exterr_skb
*serr
;
1513 struct sk_buff
*skb
, *skb2
;
1517 skb
= skb_dequeue(&sk
->sk_error_queue
);
1523 msg
->msg_flags
|= MSG_TRUNC
;
1526 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1530 sock_recv_timestamp(msg
, sk
, skb
);
1532 serr
= SKB_EXT_ERR(skb
);
1533 put_cmsg(msg
, SOL_PACKET
, PACKET_TX_TIMESTAMP
,
1534 sizeof(serr
->ee
), &serr
->ee
);
1536 msg
->msg_flags
|= MSG_ERRQUEUE
;
1539 /* Reset and regenerate socket error */
1540 spin_lock_bh(&sk
->sk_error_queue
.lock
);
1542 if ((skb2
= skb_peek(&sk
->sk_error_queue
)) != NULL
) {
1543 sk
->sk_err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
1544 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
1545 sk
->sk_error_report(sk
);
1547 spin_unlock_bh(&sk
->sk_error_queue
.lock
);
1556 * Pull a packet from our receive queue and hand it to the user.
1557 * If necessary we block.
1560 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1561 struct msghdr
*msg
, size_t len
, int flags
)
1563 struct sock
*sk
= sock
->sk
;
1564 struct sk_buff
*skb
;
1566 struct sockaddr_ll
*sll
;
1567 int vnet_hdr_len
= 0;
1570 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
|MSG_ERRQUEUE
))
1574 if (flags
& MSG_ERRQUEUE
) {
1575 err
= packet_recv_error(sk
, msg
, len
);
1580 * Call the generic datagram receiver. This handles all sorts
1581 * of horrible races and re-entrancy so we can forget about it
1582 * in the protocol layers.
1584 * Now it will return ENETDOWN, if device have just gone down,
1585 * but then it will block.
1588 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
1591 * An error occurred so return it. Because skb_recv_datagram()
1592 * handles the blocking we don't see and worry about blocking
1599 if (pkt_sk(sk
)->has_vnet_hdr
) {
1600 struct virtio_net_hdr vnet_hdr
= { 0 };
1603 vnet_hdr_len
= sizeof(vnet_hdr
);
1604 if (len
< vnet_hdr_len
)
1607 len
-= vnet_hdr_len
;
1609 if (skb_is_gso(skb
)) {
1610 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
1612 /* This is a hint as to how much should be linear. */
1613 vnet_hdr
.hdr_len
= skb_headlen(skb
);
1614 vnet_hdr
.gso_size
= sinfo
->gso_size
;
1615 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
1616 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1617 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
1618 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1619 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
1620 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
1621 else if (sinfo
->gso_type
& SKB_GSO_FCOE
)
1625 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
1626 vnet_hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1628 vnet_hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1630 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1631 vnet_hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1632 vnet_hdr
.csum_start
= skb
->csum_start
-
1634 vnet_hdr
.csum_offset
= skb
->csum_offset
;
1635 } /* else everything is zero */
1637 err
= memcpy_toiovec(msg
->msg_iov
, (void *)&vnet_hdr
,
1644 * If the address length field is there to be filled in, we fill
1648 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1649 if (sock
->type
== SOCK_PACKET
)
1650 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1652 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1655 * You lose any data beyond the buffer you gave. If it worries a
1656 * user program they can ask the device for its MTU anyway.
1662 msg
->msg_flags
|= MSG_TRUNC
;
1665 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1669 sock_recv_ts_and_drops(msg
, sk
, skb
);
1672 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1675 if (pkt_sk(sk
)->auxdata
) {
1676 struct tpacket_auxdata aux
;
1678 aux
.tp_status
= TP_STATUS_USER
;
1679 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1680 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1681 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1682 aux
.tp_snaplen
= skb
->len
;
1684 aux
.tp_net
= skb_network_offset(skb
);
1685 aux
.tp_vlan_tci
= vlan_tx_tag_get(skb
);
1687 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1691 * Free or return the buffer as appropriate. Again this
1692 * hides all the races and re-entrancy issues from us.
1694 err
= vnet_hdr_len
+ ((flags
&MSG_TRUNC
) ? skb
->len
: copied
);
1697 skb_free_datagram(sk
, skb
);
1702 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1703 int *uaddr_len
, int peer
)
1705 struct net_device
*dev
;
1706 struct sock
*sk
= sock
->sk
;
1711 uaddr
->sa_family
= AF_PACKET
;
1713 dev
= dev_get_by_index_rcu(sock_net(sk
), pkt_sk(sk
)->ifindex
);
1715 strncpy(uaddr
->sa_data
, dev
->name
, 14);
1717 memset(uaddr
->sa_data
, 0, 14);
1719 *uaddr_len
= sizeof(*uaddr
);
1724 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1725 int *uaddr_len
, int peer
)
1727 struct net_device
*dev
;
1728 struct sock
*sk
= sock
->sk
;
1729 struct packet_sock
*po
= pkt_sk(sk
);
1730 DECLARE_SOCKADDR(struct sockaddr_ll
*, sll
, uaddr
);
1735 sll
->sll_family
= AF_PACKET
;
1736 sll
->sll_ifindex
= po
->ifindex
;
1737 sll
->sll_protocol
= po
->num
;
1738 sll
->sll_pkttype
= 0;
1740 dev
= dev_get_by_index_rcu(sock_net(sk
), po
->ifindex
);
1742 sll
->sll_hatype
= dev
->type
;
1743 sll
->sll_halen
= dev
->addr_len
;
1744 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1746 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1750 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1755 static int packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
,
1759 case PACKET_MR_MULTICAST
:
1760 if (i
->alen
!= dev
->addr_len
)
1763 return dev_mc_add(dev
, i
->addr
);
1765 return dev_mc_del(dev
, i
->addr
);
1767 case PACKET_MR_PROMISC
:
1768 return dev_set_promiscuity(dev
, what
);
1770 case PACKET_MR_ALLMULTI
:
1771 return dev_set_allmulti(dev
, what
);
1773 case PACKET_MR_UNICAST
:
1774 if (i
->alen
!= dev
->addr_len
)
1777 return dev_uc_add(dev
, i
->addr
);
1779 return dev_uc_del(dev
, i
->addr
);
1787 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1789 for ( ; i
; i
= i
->next
) {
1790 if (i
->ifindex
== dev
->ifindex
)
1791 packet_dev_mc(dev
, i
, what
);
1795 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1797 struct packet_sock
*po
= pkt_sk(sk
);
1798 struct packet_mclist
*ml
, *i
;
1799 struct net_device
*dev
;
1805 dev
= __dev_get_by_index(sock_net(sk
), mreq
->mr_ifindex
);
1810 if (mreq
->mr_alen
> dev
->addr_len
)
1814 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1819 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1820 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1821 ml
->type
== mreq
->mr_type
&&
1822 ml
->alen
== mreq
->mr_alen
&&
1823 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1825 /* Free the new element ... */
1831 i
->type
= mreq
->mr_type
;
1832 i
->ifindex
= mreq
->mr_ifindex
;
1833 i
->alen
= mreq
->mr_alen
;
1834 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1836 i
->next
= po
->mclist
;
1838 err
= packet_dev_mc(dev
, i
, 1);
1840 po
->mclist
= i
->next
;
1849 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1851 struct packet_mclist
*ml
, **mlp
;
1855 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1856 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1857 ml
->type
== mreq
->mr_type
&&
1858 ml
->alen
== mreq
->mr_alen
&&
1859 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1860 if (--ml
->count
== 0) {
1861 struct net_device
*dev
;
1863 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1865 packet_dev_mc(dev
, ml
, -1);
1873 return -EADDRNOTAVAIL
;
1876 static void packet_flush_mclist(struct sock
*sk
)
1878 struct packet_sock
*po
= pkt_sk(sk
);
1879 struct packet_mclist
*ml
;
1885 while ((ml
= po
->mclist
) != NULL
) {
1886 struct net_device
*dev
;
1888 po
->mclist
= ml
->next
;
1889 dev
= __dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1891 packet_dev_mc(dev
, ml
, -1);
1898 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
1900 struct sock
*sk
= sock
->sk
;
1901 struct packet_sock
*po
= pkt_sk(sk
);
1904 if (level
!= SOL_PACKET
)
1905 return -ENOPROTOOPT
;
1908 case PACKET_ADD_MEMBERSHIP
:
1909 case PACKET_DROP_MEMBERSHIP
:
1911 struct packet_mreq_max mreq
;
1913 memset(&mreq
, 0, sizeof(mreq
));
1914 if (len
< sizeof(struct packet_mreq
))
1916 if (len
> sizeof(mreq
))
1918 if (copy_from_user(&mreq
, optval
, len
))
1920 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1922 if (optname
== PACKET_ADD_MEMBERSHIP
)
1923 ret
= packet_mc_add(sk
, &mreq
);
1925 ret
= packet_mc_drop(sk
, &mreq
);
1929 case PACKET_RX_RING
:
1930 case PACKET_TX_RING
:
1932 struct tpacket_req req
;
1934 if (optlen
< sizeof(req
))
1936 if (pkt_sk(sk
)->has_vnet_hdr
)
1938 if (copy_from_user(&req
, optval
, sizeof(req
)))
1940 return packet_set_ring(sk
, &req
, 0, optname
== PACKET_TX_RING
);
1942 case PACKET_COPY_THRESH
:
1946 if (optlen
!= sizeof(val
))
1948 if (copy_from_user(&val
, optval
, sizeof(val
)))
1951 pkt_sk(sk
)->copy_thresh
= val
;
1954 case PACKET_VERSION
:
1958 if (optlen
!= sizeof(val
))
1960 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1962 if (copy_from_user(&val
, optval
, sizeof(val
)))
1967 po
->tp_version
= val
;
1973 case PACKET_RESERVE
:
1977 if (optlen
!= sizeof(val
))
1979 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1981 if (copy_from_user(&val
, optval
, sizeof(val
)))
1983 po
->tp_reserve
= val
;
1990 if (optlen
!= sizeof(val
))
1992 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
1994 if (copy_from_user(&val
, optval
, sizeof(val
)))
1996 po
->tp_loss
= !!val
;
1999 case PACKET_AUXDATA
:
2003 if (optlen
< sizeof(val
))
2005 if (copy_from_user(&val
, optval
, sizeof(val
)))
2008 po
->auxdata
= !!val
;
2011 case PACKET_ORIGDEV
:
2015 if (optlen
< sizeof(val
))
2017 if (copy_from_user(&val
, optval
, sizeof(val
)))
2020 po
->origdev
= !!val
;
2023 case PACKET_VNET_HDR
:
2027 if (sock
->type
!= SOCK_RAW
)
2029 if (po
->rx_ring
.pg_vec
|| po
->tx_ring
.pg_vec
)
2031 if (optlen
< sizeof(val
))
2033 if (copy_from_user(&val
, optval
, sizeof(val
)))
2036 po
->has_vnet_hdr
= !!val
;
2039 case PACKET_TIMESTAMP
:
2043 if (optlen
!= sizeof(val
))
2045 if (copy_from_user(&val
, optval
, sizeof(val
)))
2048 po
->tp_tstamp
= val
;
2052 return -ENOPROTOOPT
;
2056 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
2057 char __user
*optval
, int __user
*optlen
)
2061 struct sock
*sk
= sock
->sk
;
2062 struct packet_sock
*po
= pkt_sk(sk
);
2064 struct tpacket_stats st
;
2066 if (level
!= SOL_PACKET
)
2067 return -ENOPROTOOPT
;
2069 if (get_user(len
, optlen
))
2076 case PACKET_STATISTICS
:
2077 if (len
> sizeof(struct tpacket_stats
))
2078 len
= sizeof(struct tpacket_stats
);
2079 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2081 memset(&po
->stats
, 0, sizeof(st
));
2082 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2083 st
.tp_packets
+= st
.tp_drops
;
2087 case PACKET_AUXDATA
:
2088 if (len
> sizeof(int))
2094 case PACKET_ORIGDEV
:
2095 if (len
> sizeof(int))
2101 case PACKET_VNET_HDR
:
2102 if (len
> sizeof(int))
2104 val
= po
->has_vnet_hdr
;
2108 case PACKET_VERSION
:
2109 if (len
> sizeof(int))
2111 val
= po
->tp_version
;
2115 if (len
> sizeof(int))
2117 if (copy_from_user(&val
, optval
, len
))
2121 val
= sizeof(struct tpacket_hdr
);
2124 val
= sizeof(struct tpacket2_hdr
);
2131 case PACKET_RESERVE
:
2132 if (len
> sizeof(unsigned int))
2133 len
= sizeof(unsigned int);
2134 val
= po
->tp_reserve
;
2138 if (len
> sizeof(unsigned int))
2139 len
= sizeof(unsigned int);
2143 case PACKET_TIMESTAMP
:
2144 if (len
> sizeof(int))
2146 val
= po
->tp_tstamp
;
2150 return -ENOPROTOOPT
;
2153 if (put_user(len
, optlen
))
2155 if (copy_to_user(optval
, data
, len
))
2161 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
2164 struct hlist_node
*node
;
2165 struct net_device
*dev
= data
;
2166 struct net
*net
= dev_net(dev
);
2169 sk_for_each_rcu(sk
, node
, &net
->packet
.sklist
) {
2170 struct packet_sock
*po
= pkt_sk(sk
);
2173 case NETDEV_UNREGISTER
:
2175 packet_dev_mclist(dev
, po
->mclist
, -1);
2179 if (dev
->ifindex
== po
->ifindex
) {
2180 spin_lock(&po
->bind_lock
);
2182 __dev_remove_pack(&po
->prot_hook
);
2185 sk
->sk_err
= ENETDOWN
;
2186 if (!sock_flag(sk
, SOCK_DEAD
))
2187 sk
->sk_error_report(sk
);
2189 if (msg
== NETDEV_UNREGISTER
) {
2191 po
->prot_hook
.dev
= NULL
;
2193 spin_unlock(&po
->bind_lock
);
2197 if (dev
->ifindex
== po
->ifindex
) {
2198 spin_lock(&po
->bind_lock
);
2199 if (po
->num
&& !po
->running
) {
2200 dev_add_pack(&po
->prot_hook
);
2204 spin_unlock(&po
->bind_lock
);
2214 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
2217 struct sock
*sk
= sock
->sk
;
2222 int amount
= sk_wmem_alloc_get(sk
);
2224 return put_user(amount
, (int __user
*)arg
);
2228 struct sk_buff
*skb
;
2231 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2232 skb
= skb_peek(&sk
->sk_receive_queue
);
2235 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2236 return put_user(amount
, (int __user
*)arg
);
2239 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
2241 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
2251 case SIOCGIFBRDADDR
:
2252 case SIOCSIFBRDADDR
:
2253 case SIOCGIFNETMASK
:
2254 case SIOCSIFNETMASK
:
2255 case SIOCGIFDSTADDR
:
2256 case SIOCSIFDSTADDR
:
2258 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
2262 return -ENOIOCTLCMD
;
2267 static unsigned int packet_poll(struct file
*file
, struct socket
*sock
,
2270 struct sock
*sk
= sock
->sk
;
2271 struct packet_sock
*po
= pkt_sk(sk
);
2272 unsigned int mask
= datagram_poll(file
, sock
, wait
);
2274 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
2275 if (po
->rx_ring
.pg_vec
) {
2276 if (!packet_previous_frame(po
, &po
->rx_ring
, TP_STATUS_KERNEL
))
2277 mask
|= POLLIN
| POLLRDNORM
;
2279 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
2280 spin_lock_bh(&sk
->sk_write_queue
.lock
);
2281 if (po
->tx_ring
.pg_vec
) {
2282 if (packet_current_frame(po
, &po
->tx_ring
, TP_STATUS_AVAILABLE
))
2283 mask
|= POLLOUT
| POLLWRNORM
;
2285 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
2290 /* Dirty? Well, I still did not learn better way to account
2294 static void packet_mm_open(struct vm_area_struct
*vma
)
2296 struct file
*file
= vma
->vm_file
;
2297 struct socket
*sock
= file
->private_data
;
2298 struct sock
*sk
= sock
->sk
;
2301 atomic_inc(&pkt_sk(sk
)->mapped
);
2304 static void packet_mm_close(struct vm_area_struct
*vma
)
2306 struct file
*file
= vma
->vm_file
;
2307 struct socket
*sock
= file
->private_data
;
2308 struct sock
*sk
= sock
->sk
;
2311 atomic_dec(&pkt_sk(sk
)->mapped
);
2314 static const struct vm_operations_struct packet_mmap_ops
= {
2315 .open
= packet_mm_open
,
2316 .close
= packet_mm_close
,
2319 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
2323 for (i
= 0; i
< len
; i
++) {
2324 if (likely(pg_vec
[i
]))
2325 free_pages((unsigned long) pg_vec
[i
], order
);
2330 static inline char *alloc_one_pg_vec_page(unsigned long order
)
2332 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
| __GFP_NOWARN
;
2334 return (char *) __get_free_pages(gfp_flags
, order
);
2337 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
2339 unsigned int block_nr
= req
->tp_block_nr
;
2343 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
2344 if (unlikely(!pg_vec
))
2347 for (i
= 0; i
< block_nr
; i
++) {
2348 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
2349 if (unlikely(!pg_vec
[i
]))
2350 goto out_free_pgvec
;
2357 free_pg_vec(pg_vec
, order
, block_nr
);
2362 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
,
2363 int closing
, int tx_ring
)
2365 char **pg_vec
= NULL
;
2366 struct packet_sock
*po
= pkt_sk(sk
);
2367 int was_running
, order
= 0;
2368 struct packet_ring_buffer
*rb
;
2369 struct sk_buff_head
*rb_queue
;
2373 rb
= tx_ring
? &po
->tx_ring
: &po
->rx_ring
;
2374 rb_queue
= tx_ring
? &sk
->sk_write_queue
: &sk
->sk_receive_queue
;
2378 if (atomic_read(&po
->mapped
))
2380 if (atomic_read(&rb
->pending
))
2384 if (req
->tp_block_nr
) {
2385 /* Sanity tests and some calculations */
2387 if (unlikely(rb
->pg_vec
))
2390 switch (po
->tp_version
) {
2392 po
->tp_hdrlen
= TPACKET_HDRLEN
;
2395 po
->tp_hdrlen
= TPACKET2_HDRLEN
;
2400 if (unlikely((int)req
->tp_block_size
<= 0))
2402 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
2404 if (unlikely(req
->tp_frame_size
< po
->tp_hdrlen
+
2407 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
2410 rb
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
2411 if (unlikely(rb
->frames_per_block
<= 0))
2413 if (unlikely((rb
->frames_per_block
* req
->tp_block_nr
) !=
2418 order
= get_order(req
->tp_block_size
);
2419 pg_vec
= alloc_pg_vec(req
, order
);
2420 if (unlikely(!pg_vec
))
2426 if (unlikely(req
->tp_frame_nr
))
2432 /* Detach socket from network */
2433 spin_lock(&po
->bind_lock
);
2434 was_running
= po
->running
;
2437 __dev_remove_pack(&po
->prot_hook
);
2442 spin_unlock(&po
->bind_lock
);
2447 mutex_lock(&po
->pg_vec_lock
);
2448 if (closing
|| atomic_read(&po
->mapped
) == 0) {
2450 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
2451 spin_lock_bh(&rb_queue
->lock
);
2452 pg_vec
= XC(rb
->pg_vec
, pg_vec
);
2453 rb
->frame_max
= (req
->tp_frame_nr
- 1);
2455 rb
->frame_size
= req
->tp_frame_size
;
2456 spin_unlock_bh(&rb_queue
->lock
);
2458 order
= XC(rb
->pg_vec_order
, order
);
2459 req
->tp_block_nr
= XC(rb
->pg_vec_len
, req
->tp_block_nr
);
2461 rb
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
2462 po
->prot_hook
.func
= (po
->rx_ring
.pg_vec
) ?
2463 tpacket_rcv
: packet_rcv
;
2464 skb_queue_purge(rb_queue
);
2466 if (atomic_read(&po
->mapped
))
2467 pr_err("packet_mmap: vma is busy: %d\n",
2468 atomic_read(&po
->mapped
));
2470 mutex_unlock(&po
->pg_vec_lock
);
2472 spin_lock(&po
->bind_lock
);
2473 if (was_running
&& !po
->running
) {
2477 dev_add_pack(&po
->prot_hook
);
2479 spin_unlock(&po
->bind_lock
);
2484 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
2489 static int packet_mmap(struct file
*file
, struct socket
*sock
,
2490 struct vm_area_struct
*vma
)
2492 struct sock
*sk
= sock
->sk
;
2493 struct packet_sock
*po
= pkt_sk(sk
);
2494 unsigned long size
, expected_size
;
2495 struct packet_ring_buffer
*rb
;
2496 unsigned long start
;
2503 mutex_lock(&po
->pg_vec_lock
);
2506 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2508 expected_size
+= rb
->pg_vec_len
2514 if (expected_size
== 0)
2517 size
= vma
->vm_end
- vma
->vm_start
;
2518 if (size
!= expected_size
)
2521 start
= vma
->vm_start
;
2522 for (rb
= &po
->rx_ring
; rb
<= &po
->tx_ring
; rb
++) {
2523 if (rb
->pg_vec
== NULL
)
2526 for (i
= 0; i
< rb
->pg_vec_len
; i
++) {
2527 struct page
*page
= virt_to_page(rb
->pg_vec
[i
]);
2530 for (pg_num
= 0; pg_num
< rb
->pg_vec_pages
;
2532 err
= vm_insert_page(vma
, start
, page
);
2540 atomic_inc(&po
->mapped
);
2541 vma
->vm_ops
= &packet_mmap_ops
;
2545 mutex_unlock(&po
->pg_vec_lock
);
2549 static const struct proto_ops packet_ops_spkt
= {
2550 .family
= PF_PACKET
,
2551 .owner
= THIS_MODULE
,
2552 .release
= packet_release
,
2553 .bind
= packet_bind_spkt
,
2554 .connect
= sock_no_connect
,
2555 .socketpair
= sock_no_socketpair
,
2556 .accept
= sock_no_accept
,
2557 .getname
= packet_getname_spkt
,
2558 .poll
= datagram_poll
,
2559 .ioctl
= packet_ioctl
,
2560 .listen
= sock_no_listen
,
2561 .shutdown
= sock_no_shutdown
,
2562 .setsockopt
= sock_no_setsockopt
,
2563 .getsockopt
= sock_no_getsockopt
,
2564 .sendmsg
= packet_sendmsg_spkt
,
2565 .recvmsg
= packet_recvmsg
,
2566 .mmap
= sock_no_mmap
,
2567 .sendpage
= sock_no_sendpage
,
2570 static const struct proto_ops packet_ops
= {
2571 .family
= PF_PACKET
,
2572 .owner
= THIS_MODULE
,
2573 .release
= packet_release
,
2574 .bind
= packet_bind
,
2575 .connect
= sock_no_connect
,
2576 .socketpair
= sock_no_socketpair
,
2577 .accept
= sock_no_accept
,
2578 .getname
= packet_getname
,
2579 .poll
= packet_poll
,
2580 .ioctl
= packet_ioctl
,
2581 .listen
= sock_no_listen
,
2582 .shutdown
= sock_no_shutdown
,
2583 .setsockopt
= packet_setsockopt
,
2584 .getsockopt
= packet_getsockopt
,
2585 .sendmsg
= packet_sendmsg
,
2586 .recvmsg
= packet_recvmsg
,
2587 .mmap
= packet_mmap
,
2588 .sendpage
= sock_no_sendpage
,
2591 static const struct net_proto_family packet_family_ops
= {
2592 .family
= PF_PACKET
,
2593 .create
= packet_create
,
2594 .owner
= THIS_MODULE
,
2597 static struct notifier_block packet_netdev_notifier
= {
2598 .notifier_call
= packet_notifier
,
2601 #ifdef CONFIG_PROC_FS
2603 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2606 struct net
*net
= seq_file_net(seq
);
2609 return seq_hlist_start_head_rcu(&net
->packet
.sklist
, *pos
);
2612 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2614 struct net
*net
= seq_file_net(seq
);
2615 return seq_hlist_next_rcu(v
, &net
->packet
.sklist
, pos
);
2618 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
2624 static int packet_seq_show(struct seq_file
*seq
, void *v
)
2626 if (v
== SEQ_START_TOKEN
)
2627 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2629 struct sock
*s
= sk_entry(v
);
2630 const struct packet_sock
*po
= pkt_sk(s
);
2633 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2635 atomic_read(&s
->sk_refcnt
),
2640 atomic_read(&s
->sk_rmem_alloc
),
2648 static const struct seq_operations packet_seq_ops
= {
2649 .start
= packet_seq_start
,
2650 .next
= packet_seq_next
,
2651 .stop
= packet_seq_stop
,
2652 .show
= packet_seq_show
,
2655 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
2657 return seq_open_net(inode
, file
, &packet_seq_ops
,
2658 sizeof(struct seq_net_private
));
2661 static const struct file_operations packet_seq_fops
= {
2662 .owner
= THIS_MODULE
,
2663 .open
= packet_seq_open
,
2665 .llseek
= seq_lseek
,
2666 .release
= seq_release_net
,
2671 static int __net_init
packet_net_init(struct net
*net
)
2673 spin_lock_init(&net
->packet
.sklist_lock
);
2674 INIT_HLIST_HEAD(&net
->packet
.sklist
);
2676 if (!proc_net_fops_create(net
, "packet", 0, &packet_seq_fops
))
2682 static void __net_exit
packet_net_exit(struct net
*net
)
2684 proc_net_remove(net
, "packet");
2687 static struct pernet_operations packet_net_ops
= {
2688 .init
= packet_net_init
,
2689 .exit
= packet_net_exit
,
2693 static void __exit
packet_exit(void)
2695 unregister_netdevice_notifier(&packet_netdev_notifier
);
2696 unregister_pernet_subsys(&packet_net_ops
);
2697 sock_unregister(PF_PACKET
);
2698 proto_unregister(&packet_proto
);
2701 static int __init
packet_init(void)
2703 int rc
= proto_register(&packet_proto
, 0);
2708 sock_register(&packet_family_ops
);
2709 register_pernet_subsys(&packet_net_ops
);
2710 register_netdevice_notifier(&packet_netdev_notifier
);
2715 module_init(packet_init
);
2716 module_exit(packet_exit
);
2717 MODULE_LICENSE("GPL");
2718 MODULE_ALIAS_NETPROTO(PF_PACKET
);