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.
8 * Version: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Alan Cox, <gw4pts@gw4pts.ampr.org>
15 * Alan Cox : verify_area() now used correctly
16 * Alan Cox : new skbuff lists, look ma no backlogs!
17 * Alan Cox : tidied skbuff lists.
18 * Alan Cox : Now uses generic datagram routines I
19 * added. Also fixed the peek/read crash
20 * from all old Linux datagram code.
21 * Alan Cox : Uses the improved datagram code.
22 * Alan Cox : Added NULL's for socket options.
23 * Alan Cox : Re-commented the code.
24 * Alan Cox : Use new kernel side addressing
25 * Rob Janssen : Correct MTU usage.
26 * Dave Platt : Counter leaks caused by incorrect
27 * interrupt locking and some slightly
28 * dubious gcc output. Can you read
29 * compiler: it said _VOLATILE_
30 * Richard Kooijman : Timestamp fixes.
31 * Alan Cox : New buffers. Use sk->mac.raw.
32 * Alan Cox : sendmsg/recvmsg support.
33 * Alan Cox : Protocol setting support
34 * Alexey Kuznetsov : Untied from IPv4 stack.
35 * Cyrus Durgin : Fixed kerneld for kmod.
36 * Michal Ostrowski : Module initialization cleanup.
37 * Ulises Alonso : Frame number limit removal and
38 * packet_set_ring memory leak.
39 * Eric Biederman : Allow for > 8 byte hardware addresses.
40 * The convention is that longer addresses
41 * will simply extend the hardware address
42 * byte arrays at the end of sockaddr_ll
45 * This program is free software; you can redistribute it and/or
46 * modify it under the terms of the GNU General Public License
47 * as published by the Free Software Foundation; either version
48 * 2 of the License, or (at your option) any later version.
52 #include <linux/types.h>
54 #include <linux/capability.h>
55 #include <linux/fcntl.h>
56 #include <linux/socket.h>
58 #include <linux/inet.h>
59 #include <linux/netdevice.h>
60 #include <linux/if_packet.h>
61 #include <linux/wireless.h>
62 #include <linux/kernel.h>
63 #include <linux/kmod.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>
84 #include <net/inet_common.h>
89 - if device has no dev->hard_header routine, it adds and removes ll header
90 inside itself. In this case ll header is invisible outside of device,
91 but higher levels still should reserve dev->hard_header_len.
92 Some devices are enough clever to reallocate skb, when header
93 will not fit to reserved space (tunnel), another ones are silly
95 - packet socket receives packets with pulled ll header,
96 so that SOCK_RAW should push it back.
101 Incoming, dev->hard_header!=NULL
102 mac_header -> ll header
105 Outgoing, dev->hard_header!=NULL
106 mac_header -> ll header
109 Incoming, dev->hard_header==NULL
110 mac_header -> UNKNOWN position. It is very likely, that it points to ll
111 header. PPP makes it, that is wrong, because introduce
112 assymetry between rx and tx paths.
115 Outgoing, dev->hard_header==NULL
116 mac_header -> data. ll header is still not built!
120 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
126 dev->hard_header != NULL
127 mac_header -> ll header
130 dev->hard_header == NULL (ll header is added by device, we cannot control it)
134 We should set nh.raw on output to correct posistion,
135 packet classifier depends on it.
138 /* List of all packet sockets. */
139 static HLIST_HEAD(packet_sklist
);
140 static DEFINE_RWLOCK(packet_sklist_lock
);
142 /* Private packet socket structures. */
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
159 unsigned short mr_type
;
160 unsigned short mr_alen
;
161 unsigned char mr_address
[MAX_ADDR_LEN
];
164 #ifdef CONFIG_PACKET_MMAP
165 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
);
168 static void packet_flush_mclist(struct sock
*sk
);
171 /* struct sock has to be the first member of packet_sock */
173 struct tpacket_stats stats
;
174 #ifdef CONFIG_PACKET_MMAP
177 unsigned int frames_per_block
;
178 unsigned int frame_size
;
179 unsigned int frame_max
;
182 struct packet_type prot_hook
;
183 spinlock_t bind_lock
;
184 unsigned int running
:1, /* prot_hook is attached*/
187 int ifindex
; /* bound device */
189 struct packet_mclist
*mclist
;
190 #ifdef CONFIG_PACKET_MMAP
192 unsigned int pg_vec_order
;
193 unsigned int pg_vec_pages
;
194 unsigned int pg_vec_len
;
198 struct packet_skb_cb
{
199 unsigned int origlen
;
201 struct sockaddr_pkt pkt
;
202 struct sockaddr_ll ll
;
206 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
208 #ifdef CONFIG_PACKET_MMAP
210 static inline struct tpacket_hdr
*packet_lookup_frame(struct packet_sock
*po
, unsigned int position
)
212 unsigned int pg_vec_pos
, frame_offset
;
214 pg_vec_pos
= position
/ po
->frames_per_block
;
215 frame_offset
= position
% po
->frames_per_block
;
217 return (struct tpacket_hdr
*)(po
->pg_vec
[pg_vec_pos
] + (frame_offset
* po
->frame_size
));
221 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
223 return (struct packet_sock
*)sk
;
226 static void packet_sock_destruct(struct sock
*sk
)
228 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
229 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
231 if (!sock_flag(sk
, SOCK_DEAD
)) {
232 printk("Attempt to release alive packet socket: %p\n", sk
);
236 sk_refcnt_debug_dec(sk
);
240 static const struct proto_ops packet_ops
;
242 static const struct proto_ops packet_ops_spkt
;
244 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
247 struct sockaddr_pkt
*spkt
;
249 if (dev
->nd_net
!= &init_net
)
253 * When we registered the protocol we saved the socket in the data
254 * field for just this event.
257 sk
= pt
->af_packet_priv
;
260 * Yank back the headers [hope the device set this
261 * right or kerboom...]
263 * Incoming packets have ll header pulled,
266 * For outgoing ones skb->data == skb_mac_header(skb)
267 * so that this procedure is noop.
270 if (skb
->pkt_type
== PACKET_LOOPBACK
)
273 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
276 /* drop any routing info */
277 dst_release(skb
->dst
);
280 /* drop conntrack reference */
283 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
285 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
288 * The SOCK_PACKET socket receives _all_ frames.
291 spkt
->spkt_family
= dev
->type
;
292 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
293 spkt
->spkt_protocol
= skb
->protocol
;
296 * Charge the memory to the socket. This is done specifically
297 * to prevent sockets using all the memory up.
300 if (sock_queue_rcv_skb(sk
,skb
) == 0)
311 * Output a raw packet to a device layer. This bypasses all the other
312 * protocol layers and you must therefore supply it with a complete frame
315 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
316 struct msghdr
*msg
, size_t len
)
318 struct sock
*sk
= sock
->sk
;
319 struct sockaddr_pkt
*saddr
=(struct sockaddr_pkt
*)msg
->msg_name
;
321 struct net_device
*dev
;
326 * Get and verify the address.
331 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
333 if (msg
->msg_namelen
==sizeof(struct sockaddr_pkt
))
334 proto
=saddr
->spkt_protocol
;
337 return(-ENOTCONN
); /* SOCK_PACKET must be sent giving an address */
340 * Find the device first to size check it
343 saddr
->spkt_device
[13] = 0;
344 dev
= dev_get_by_name(&init_net
, saddr
->spkt_device
);
350 if (!(dev
->flags
& IFF_UP
))
354 * You may not queue a frame bigger than the mtu. This is the lowest level
355 * raw protocol and you must do your own fragmentation at this level.
359 if (len
> dev
->mtu
+ dev
->hard_header_len
)
363 skb
= sock_wmalloc(sk
, len
+ LL_RESERVED_SPACE(dev
), 0, GFP_KERNEL
);
366 * If the write buffer is full, then tough. At this level the user gets to
367 * deal with the problem - do your own algorithmic backoffs. That's far
378 /* FIXME: Save some space for broken drivers that write a
379 * hard header at transmission time by themselves. PPP is the
380 * notable one here. This should really be fixed at the driver level.
382 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
383 skb_reset_network_header(skb
);
385 /* Try to align data part correctly */
386 if (dev
->header_ops
) {
387 skb
->data
-= dev
->hard_header_len
;
388 skb
->tail
-= dev
->hard_header_len
;
389 if (len
< dev
->hard_header_len
)
390 skb_reset_network_header(skb
);
393 /* Returns -EFAULT on error */
394 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
395 skb
->protocol
= proto
;
397 skb
->priority
= sk
->sk_priority
;
417 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
420 struct sk_filter
*filter
;
423 filter
= rcu_dereference(sk
->sk_filter
);
425 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
426 rcu_read_unlock_bh();
432 This function makes lazy skb cloning in hope that most of packets
433 are discarded by BPF.
435 Note tricky part: we DO mangle shared skb! skb->data, skb->len
436 and skb->cb are mangled. It works because (and until) packets
437 falling here are owned by current CPU. Output packets are cloned
438 by dev_queue_xmit_nit(), input packets are processed by net_bh
439 sequencially, so that if we return skb to original state on exit,
440 we will not harm anyone.
443 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
446 struct sockaddr_ll
*sll
;
447 struct packet_sock
*po
;
448 u8
* skb_head
= skb
->data
;
449 int skb_len
= skb
->len
;
450 unsigned int snaplen
, res
;
452 if (dev
->nd_net
!= &init_net
)
455 if (skb
->pkt_type
== PACKET_LOOPBACK
)
458 sk
= pt
->af_packet_priv
;
463 if (dev
->header_ops
) {
464 /* The device has an explicit notion of ll header,
465 exported to higher levels.
467 Otherwise, the device hides datails of it frame
468 structure, so that corresponding packet head
469 never delivered to user.
471 if (sk
->sk_type
!= SOCK_DGRAM
)
472 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
473 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
474 /* Special case: outgoing packets have ll header at head */
475 skb_pull(skb
, skb_network_offset(skb
));
481 res
= run_filter(skb
, sk
, snaplen
);
487 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
488 (unsigned)sk
->sk_rcvbuf
)
491 if (skb_shared(skb
)) {
492 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
496 if (skb_head
!= skb
->data
) {
497 skb
->data
= skb_head
;
504 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
507 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
508 sll
->sll_family
= AF_PACKET
;
509 sll
->sll_hatype
= dev
->type
;
510 sll
->sll_protocol
= skb
->protocol
;
511 sll
->sll_pkttype
= skb
->pkt_type
;
512 if (unlikely(po
->origdev
))
513 sll
->sll_ifindex
= orig_dev
->ifindex
;
515 sll
->sll_ifindex
= dev
->ifindex
;
517 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
519 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
521 if (pskb_trim(skb
, snaplen
))
524 skb_set_owner_r(skb
, sk
);
526 dst_release(skb
->dst
);
529 /* drop conntrack reference */
532 spin_lock(&sk
->sk_receive_queue
.lock
);
533 po
->stats
.tp_packets
++;
534 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
535 spin_unlock(&sk
->sk_receive_queue
.lock
);
536 sk
->sk_data_ready(sk
, skb
->len
);
540 spin_lock(&sk
->sk_receive_queue
.lock
);
541 po
->stats
.tp_drops
++;
542 spin_unlock(&sk
->sk_receive_queue
.lock
);
545 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
546 skb
->data
= skb_head
;
554 #ifdef CONFIG_PACKET_MMAP
555 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
558 struct packet_sock
*po
;
559 struct sockaddr_ll
*sll
;
560 struct tpacket_hdr
*h
;
561 u8
* skb_head
= skb
->data
;
562 int skb_len
= skb
->len
;
563 unsigned int snaplen
, res
;
564 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
565 unsigned short macoff
, netoff
;
566 struct sk_buff
*copy_skb
= NULL
;
569 if (dev
->nd_net
!= &init_net
)
572 if (skb
->pkt_type
== PACKET_LOOPBACK
)
575 sk
= pt
->af_packet_priv
;
578 if (dev
->header_ops
) {
579 if (sk
->sk_type
!= SOCK_DGRAM
)
580 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
581 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
582 /* Special case: outgoing packets have ll header at head */
583 skb_pull(skb
, skb_network_offset(skb
));
587 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
588 status
|= TP_STATUS_CSUMNOTREADY
;
592 res
= run_filter(skb
, sk
, snaplen
);
598 if (sk
->sk_type
== SOCK_DGRAM
) {
599 macoff
= netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
) + 16;
601 unsigned maclen
= skb_network_offset(skb
);
602 netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
+ (maclen
< 16 ? 16 : maclen
));
603 macoff
= netoff
- maclen
;
606 if (macoff
+ snaplen
> po
->frame_size
) {
607 if (po
->copy_thresh
&&
608 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
609 (unsigned)sk
->sk_rcvbuf
) {
610 if (skb_shared(skb
)) {
611 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
613 copy_skb
= skb_get(skb
);
614 skb_head
= skb
->data
;
617 skb_set_owner_r(copy_skb
, sk
);
619 snaplen
= po
->frame_size
- macoff
;
620 if ((int)snaplen
< 0)
624 spin_lock(&sk
->sk_receive_queue
.lock
);
625 h
= packet_lookup_frame(po
, po
->head
);
629 po
->head
= po
->head
!= po
->frame_max
? po
->head
+1 : 0;
630 po
->stats
.tp_packets
++;
632 status
|= TP_STATUS_COPY
;
633 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
635 if (!po
->stats
.tp_drops
)
636 status
&= ~TP_STATUS_LOSING
;
637 spin_unlock(&sk
->sk_receive_queue
.lock
);
639 skb_copy_bits(skb
, 0, (u8
*)h
+ macoff
, snaplen
);
641 h
->tp_len
= skb
->len
;
642 h
->tp_snaplen
= snaplen
;
645 if (skb
->tstamp
.tv64
)
646 tv
= ktime_to_timeval(skb
->tstamp
);
648 do_gettimeofday(&tv
);
649 h
->tp_sec
= tv
.tv_sec
;
650 h
->tp_usec
= tv
.tv_usec
;
652 sll
= (struct sockaddr_ll
*)((u8
*)h
+ TPACKET_ALIGN(sizeof(*h
)));
653 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
654 sll
->sll_family
= AF_PACKET
;
655 sll
->sll_hatype
= dev
->type
;
656 sll
->sll_protocol
= skb
->protocol
;
657 sll
->sll_pkttype
= skb
->pkt_type
;
658 if (unlikely(po
->origdev
))
659 sll
->sll_ifindex
= orig_dev
->ifindex
;
661 sll
->sll_ifindex
= dev
->ifindex
;
663 h
->tp_status
= status
;
667 struct page
*p_start
, *p_end
;
668 u8
*h_end
= (u8
*)h
+ macoff
+ snaplen
- 1;
670 p_start
= virt_to_page(h
);
671 p_end
= virt_to_page(h_end
);
672 while (p_start
<= p_end
) {
673 flush_dcache_page(p_start
);
678 sk
->sk_data_ready(sk
, 0);
681 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
682 skb
->data
= skb_head
;
690 po
->stats
.tp_drops
++;
691 spin_unlock(&sk
->sk_receive_queue
.lock
);
693 sk
->sk_data_ready(sk
, 0);
702 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
703 struct msghdr
*msg
, size_t len
)
705 struct sock
*sk
= sock
->sk
;
706 struct sockaddr_ll
*saddr
=(struct sockaddr_ll
*)msg
->msg_name
;
708 struct net_device
*dev
;
711 int ifindex
, err
, reserve
= 0;
714 * Get and verify the address.
718 struct packet_sock
*po
= pkt_sk(sk
);
720 ifindex
= po
->ifindex
;
725 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
727 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
729 ifindex
= saddr
->sll_ifindex
;
730 proto
= saddr
->sll_protocol
;
731 addr
= saddr
->sll_addr
;
735 dev
= dev_get_by_index(&init_net
, ifindex
);
739 if (sock
->type
== SOCK_RAW
)
740 reserve
= dev
->hard_header_len
;
743 if (!(dev
->flags
& IFF_UP
))
747 if (len
> dev
->mtu
+reserve
)
750 skb
= sock_alloc_send_skb(sk
, len
+ LL_RESERVED_SPACE(dev
),
751 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
755 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
756 skb_reset_network_header(skb
);
759 if (sock
->type
== SOCK_DGRAM
&&
760 dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
) < 0)
763 /* Returns -EFAULT on error */
764 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
768 skb
->protocol
= proto
;
770 skb
->priority
= sk
->sk_priority
;
776 err
= dev_queue_xmit(skb
);
777 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
794 * Close a PACKET socket. This is fairly simple. We immediately go
795 * to 'closed' state and remove our protocol entry in the device list.
798 static int packet_release(struct socket
*sock
)
800 struct sock
*sk
= sock
->sk
;
801 struct packet_sock
*po
;
808 write_lock_bh(&packet_sklist_lock
);
809 sk_del_node_init(sk
);
810 write_unlock_bh(&packet_sklist_lock
);
813 * Unhook packet receive handler.
818 * Remove the protocol hook
820 dev_remove_pack(&po
->prot_hook
);
826 packet_flush_mclist(sk
);
828 #ifdef CONFIG_PACKET_MMAP
830 struct tpacket_req req
;
831 memset(&req
, 0, sizeof(req
));
832 packet_set_ring(sk
, &req
, 1);
837 * Now the socket is dead. No more input will appear.
845 skb_queue_purge(&sk
->sk_receive_queue
);
846 sk_refcnt_debug_release(sk
);
853 * Attach a packet hook.
856 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
858 struct packet_sock
*po
= pkt_sk(sk
);
860 * Detach an existing hook if present.
865 spin_lock(&po
->bind_lock
);
870 spin_unlock(&po
->bind_lock
);
871 dev_remove_pack(&po
->prot_hook
);
872 spin_lock(&po
->bind_lock
);
876 po
->prot_hook
.type
= protocol
;
877 po
->prot_hook
.dev
= dev
;
879 po
->ifindex
= dev
? dev
->ifindex
: 0;
884 if (!dev
|| (dev
->flags
& IFF_UP
)) {
885 dev_add_pack(&po
->prot_hook
);
889 sk
->sk_err
= ENETDOWN
;
890 if (!sock_flag(sk
, SOCK_DEAD
))
891 sk
->sk_error_report(sk
);
895 spin_unlock(&po
->bind_lock
);
901 * Bind a packet socket to a device
904 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
906 struct sock
*sk
=sock
->sk
;
908 struct net_device
*dev
;
915 if (addr_len
!= sizeof(struct sockaddr
))
917 strlcpy(name
,uaddr
->sa_data
,sizeof(name
));
919 dev
= dev_get_by_name(&init_net
, name
);
921 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
927 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
929 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
930 struct sock
*sk
=sock
->sk
;
931 struct net_device
*dev
= NULL
;
939 if (addr_len
< sizeof(struct sockaddr_ll
))
941 if (sll
->sll_family
!= AF_PACKET
)
944 if (sll
->sll_ifindex
) {
946 dev
= dev_get_by_index(&init_net
, sll
->sll_ifindex
);
950 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
958 static struct proto packet_proto
= {
960 .owner
= THIS_MODULE
,
961 .obj_size
= sizeof(struct packet_sock
),
965 * Create a packet of type SOCK_PACKET.
968 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
)
971 struct packet_sock
*po
;
972 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
975 if (net
!= &init_net
)
976 return -EAFNOSUPPORT
;
978 if (!capable(CAP_NET_RAW
))
980 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
981 sock
->type
!= SOCK_PACKET
)
982 return -ESOCKTNOSUPPORT
;
984 sock
->state
= SS_UNCONNECTED
;
987 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
991 sock
->ops
= &packet_ops
;
992 if (sock
->type
== SOCK_PACKET
)
993 sock
->ops
= &packet_ops_spkt
;
995 sock_init_data(sock
, sk
);
998 sk
->sk_family
= PF_PACKET
;
1001 sk
->sk_destruct
= packet_sock_destruct
;
1002 sk_refcnt_debug_inc(sk
);
1005 * Attach a protocol block
1008 spin_lock_init(&po
->bind_lock
);
1009 po
->prot_hook
.func
= packet_rcv
;
1011 if (sock
->type
== SOCK_PACKET
)
1012 po
->prot_hook
.func
= packet_rcv_spkt
;
1014 po
->prot_hook
.af_packet_priv
= sk
;
1017 po
->prot_hook
.type
= proto
;
1018 dev_add_pack(&po
->prot_hook
);
1023 write_lock_bh(&packet_sklist_lock
);
1024 sk_add_node(sk
, &packet_sklist
);
1025 write_unlock_bh(&packet_sklist_lock
);
1032 * Pull a packet from our receive queue and hand it to the user.
1033 * If necessary we block.
1036 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1037 struct msghdr
*msg
, size_t len
, int flags
)
1039 struct sock
*sk
= sock
->sk
;
1040 struct sk_buff
*skb
;
1042 struct sockaddr_ll
*sll
;
1045 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1049 /* What error should we return now? EUNATTACH? */
1050 if (pkt_sk(sk
)->ifindex
< 0)
1055 * Call the generic datagram receiver. This handles all sorts
1056 * of horrible races and re-entrancy so we can forget about it
1057 * in the protocol layers.
1059 * Now it will return ENETDOWN, if device have just gone down,
1060 * but then it will block.
1063 skb
=skb_recv_datagram(sk
,flags
,flags
&MSG_DONTWAIT
,&err
);
1066 * An error occurred so return it. Because skb_recv_datagram()
1067 * handles the blocking we don't see and worry about blocking
1075 * If the address length field is there to be filled in, we fill
1079 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1080 if (sock
->type
== SOCK_PACKET
)
1081 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1083 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1086 * You lose any data beyond the buffer you gave. If it worries a
1087 * user program they can ask the device for its MTU anyway.
1094 msg
->msg_flags
|=MSG_TRUNC
;
1097 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1101 sock_recv_timestamp(msg
, sk
, skb
);
1104 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1107 if (pkt_sk(sk
)->auxdata
) {
1108 struct tpacket_auxdata aux
;
1110 aux
.tp_status
= TP_STATUS_USER
;
1111 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1112 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1113 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1114 aux
.tp_snaplen
= skb
->len
;
1116 aux
.tp_net
= skb_network_offset(skb
);
1118 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1122 * Free or return the buffer as appropriate. Again this
1123 * hides all the races and re-entrancy issues from us.
1125 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1128 skb_free_datagram(sk
, skb
);
1133 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1134 int *uaddr_len
, int peer
)
1136 struct net_device
*dev
;
1137 struct sock
*sk
= sock
->sk
;
1142 uaddr
->sa_family
= AF_PACKET
;
1143 dev
= dev_get_by_index(&init_net
, pkt_sk(sk
)->ifindex
);
1145 strlcpy(uaddr
->sa_data
, dev
->name
, 15);
1148 memset(uaddr
->sa_data
, 0, 14);
1149 *uaddr_len
= sizeof(*uaddr
);
1154 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1155 int *uaddr_len
, int peer
)
1157 struct net_device
*dev
;
1158 struct sock
*sk
= sock
->sk
;
1159 struct packet_sock
*po
= pkt_sk(sk
);
1160 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1165 sll
->sll_family
= AF_PACKET
;
1166 sll
->sll_ifindex
= po
->ifindex
;
1167 sll
->sll_protocol
= po
->num
;
1168 dev
= dev_get_by_index(&init_net
, po
->ifindex
);
1170 sll
->sll_hatype
= dev
->type
;
1171 sll
->sll_halen
= dev
->addr_len
;
1172 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1175 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1178 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1183 static void packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1186 case PACKET_MR_MULTICAST
:
1188 dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1190 dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1192 case PACKET_MR_PROMISC
:
1193 dev_set_promiscuity(dev
, what
);
1195 case PACKET_MR_ALLMULTI
:
1196 dev_set_allmulti(dev
, what
);
1202 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1204 for ( ; i
; i
=i
->next
) {
1205 if (i
->ifindex
== dev
->ifindex
)
1206 packet_dev_mc(dev
, i
, what
);
1210 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1212 struct packet_sock
*po
= pkt_sk(sk
);
1213 struct packet_mclist
*ml
, *i
;
1214 struct net_device
*dev
;
1220 dev
= __dev_get_by_index(&init_net
, mreq
->mr_ifindex
);
1225 if (mreq
->mr_alen
> dev
->addr_len
)
1229 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1234 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1235 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1236 ml
->type
== mreq
->mr_type
&&
1237 ml
->alen
== mreq
->mr_alen
&&
1238 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1240 /* Free the new element ... */
1246 i
->type
= mreq
->mr_type
;
1247 i
->ifindex
= mreq
->mr_ifindex
;
1248 i
->alen
= mreq
->mr_alen
;
1249 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1251 i
->next
= po
->mclist
;
1253 packet_dev_mc(dev
, i
, +1);
1260 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1262 struct packet_mclist
*ml
, **mlp
;
1266 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1267 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1268 ml
->type
== mreq
->mr_type
&&
1269 ml
->alen
== mreq
->mr_alen
&&
1270 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1271 if (--ml
->count
== 0) {
1272 struct net_device
*dev
;
1274 dev
= dev_get_by_index(&init_net
, ml
->ifindex
);
1276 packet_dev_mc(dev
, ml
, -1);
1286 return -EADDRNOTAVAIL
;
1289 static void packet_flush_mclist(struct sock
*sk
)
1291 struct packet_sock
*po
= pkt_sk(sk
);
1292 struct packet_mclist
*ml
;
1298 while ((ml
= po
->mclist
) != NULL
) {
1299 struct net_device
*dev
;
1301 po
->mclist
= ml
->next
;
1302 if ((dev
= dev_get_by_index(&init_net
, ml
->ifindex
)) != NULL
) {
1303 packet_dev_mc(dev
, ml
, -1);
1312 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1314 struct sock
*sk
= sock
->sk
;
1315 struct packet_sock
*po
= pkt_sk(sk
);
1318 if (level
!= SOL_PACKET
)
1319 return -ENOPROTOOPT
;
1322 case PACKET_ADD_MEMBERSHIP
:
1323 case PACKET_DROP_MEMBERSHIP
:
1325 struct packet_mreq_max mreq
;
1327 memset(&mreq
, 0, sizeof(mreq
));
1328 if (len
< sizeof(struct packet_mreq
))
1330 if (len
> sizeof(mreq
))
1332 if (copy_from_user(&mreq
,optval
,len
))
1334 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1336 if (optname
== PACKET_ADD_MEMBERSHIP
)
1337 ret
= packet_mc_add(sk
, &mreq
);
1339 ret
= packet_mc_drop(sk
, &mreq
);
1343 #ifdef CONFIG_PACKET_MMAP
1344 case PACKET_RX_RING
:
1346 struct tpacket_req req
;
1348 if (optlen
<sizeof(req
))
1350 if (copy_from_user(&req
,optval
,sizeof(req
)))
1352 return packet_set_ring(sk
, &req
, 0);
1354 case PACKET_COPY_THRESH
:
1358 if (optlen
!=sizeof(val
))
1360 if (copy_from_user(&val
,optval
,sizeof(val
)))
1363 pkt_sk(sk
)->copy_thresh
= val
;
1367 case PACKET_AUXDATA
:
1371 if (optlen
< sizeof(val
))
1373 if (copy_from_user(&val
, optval
, sizeof(val
)))
1376 po
->auxdata
= !!val
;
1379 case PACKET_ORIGDEV
:
1383 if (optlen
< sizeof(val
))
1385 if (copy_from_user(&val
, optval
, sizeof(val
)))
1388 po
->origdev
= !!val
;
1392 return -ENOPROTOOPT
;
1396 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1397 char __user
*optval
, int __user
*optlen
)
1401 struct sock
*sk
= sock
->sk
;
1402 struct packet_sock
*po
= pkt_sk(sk
);
1404 struct tpacket_stats st
;
1406 if (level
!= SOL_PACKET
)
1407 return -ENOPROTOOPT
;
1409 if (get_user(len
, optlen
))
1416 case PACKET_STATISTICS
:
1417 if (len
> sizeof(struct tpacket_stats
))
1418 len
= sizeof(struct tpacket_stats
);
1419 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1421 memset(&po
->stats
, 0, sizeof(st
));
1422 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1423 st
.tp_packets
+= st
.tp_drops
;
1427 case PACKET_AUXDATA
:
1428 if (len
> sizeof(int))
1434 case PACKET_ORIGDEV
:
1435 if (len
> sizeof(int))
1442 return -ENOPROTOOPT
;
1445 if (put_user(len
, optlen
))
1447 if (copy_to_user(optval
, data
, len
))
1453 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1456 struct hlist_node
*node
;
1457 struct net_device
*dev
= data
;
1459 if (dev
->nd_net
!= &init_net
)
1462 read_lock(&packet_sklist_lock
);
1463 sk_for_each(sk
, node
, &packet_sklist
) {
1464 struct packet_sock
*po
= pkt_sk(sk
);
1467 case NETDEV_UNREGISTER
:
1469 packet_dev_mclist(dev
, po
->mclist
, -1);
1473 if (dev
->ifindex
== po
->ifindex
) {
1474 spin_lock(&po
->bind_lock
);
1476 __dev_remove_pack(&po
->prot_hook
);
1479 sk
->sk_err
= ENETDOWN
;
1480 if (!sock_flag(sk
, SOCK_DEAD
))
1481 sk
->sk_error_report(sk
);
1483 if (msg
== NETDEV_UNREGISTER
) {
1485 po
->prot_hook
.dev
= NULL
;
1487 spin_unlock(&po
->bind_lock
);
1491 spin_lock(&po
->bind_lock
);
1492 if (dev
->ifindex
== po
->ifindex
&& po
->num
&&
1494 dev_add_pack(&po
->prot_hook
);
1498 spin_unlock(&po
->bind_lock
);
1502 read_unlock(&packet_sklist_lock
);
1507 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
1510 struct sock
*sk
= sock
->sk
;
1515 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1516 return put_user(amount
, (int __user
*)arg
);
1520 struct sk_buff
*skb
;
1523 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1524 skb
= skb_peek(&sk
->sk_receive_queue
);
1527 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1528 return put_user(amount
, (int __user
*)arg
);
1531 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1533 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
1543 case SIOCGIFBRDADDR
:
1544 case SIOCSIFBRDADDR
:
1545 case SIOCGIFNETMASK
:
1546 case SIOCSIFNETMASK
:
1547 case SIOCGIFDSTADDR
:
1548 case SIOCSIFDSTADDR
:
1550 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1554 return -ENOIOCTLCMD
;
1559 #ifndef CONFIG_PACKET_MMAP
1560 #define packet_mmap sock_no_mmap
1561 #define packet_poll datagram_poll
1564 static unsigned int packet_poll(struct file
* file
, struct socket
*sock
,
1567 struct sock
*sk
= sock
->sk
;
1568 struct packet_sock
*po
= pkt_sk(sk
);
1569 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1571 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1573 unsigned last
= po
->head
? po
->head
-1 : po
->frame_max
;
1574 struct tpacket_hdr
*h
;
1576 h
= packet_lookup_frame(po
, last
);
1579 mask
|= POLLIN
| POLLRDNORM
;
1581 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1586 /* Dirty? Well, I still did not learn better way to account
1590 static void packet_mm_open(struct vm_area_struct
*vma
)
1592 struct file
*file
= vma
->vm_file
;
1593 struct socket
* sock
= file
->private_data
;
1594 struct sock
*sk
= sock
->sk
;
1597 atomic_inc(&pkt_sk(sk
)->mapped
);
1600 static void packet_mm_close(struct vm_area_struct
*vma
)
1602 struct file
*file
= vma
->vm_file
;
1603 struct socket
* sock
= file
->private_data
;
1604 struct sock
*sk
= sock
->sk
;
1607 atomic_dec(&pkt_sk(sk
)->mapped
);
1610 static struct vm_operations_struct packet_mmap_ops
= {
1611 .open
= packet_mm_open
,
1612 .close
=packet_mm_close
,
1615 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
1619 for (i
= 0; i
< len
; i
++) {
1620 if (likely(pg_vec
[i
]))
1621 free_pages((unsigned long) pg_vec
[i
], order
);
1626 static inline char *alloc_one_pg_vec_page(unsigned long order
)
1628 return (char *) __get_free_pages(GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
,
1632 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
1634 unsigned int block_nr
= req
->tp_block_nr
;
1638 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
1639 if (unlikely(!pg_vec
))
1642 for (i
= 0; i
< block_nr
; i
++) {
1643 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
1644 if (unlikely(!pg_vec
[i
]))
1645 goto out_free_pgvec
;
1652 free_pg_vec(pg_vec
, order
, block_nr
);
1657 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
)
1659 char **pg_vec
= NULL
;
1660 struct packet_sock
*po
= pkt_sk(sk
);
1661 int was_running
, order
= 0;
1665 if (req
->tp_block_nr
) {
1668 /* Sanity tests and some calculations */
1670 if (unlikely(po
->pg_vec
))
1673 if (unlikely((int)req
->tp_block_size
<= 0))
1675 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
1677 if (unlikely(req
->tp_frame_size
< TPACKET_HDRLEN
))
1679 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
1682 po
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
1683 if (unlikely(po
->frames_per_block
<= 0))
1685 if (unlikely((po
->frames_per_block
* req
->tp_block_nr
) !=
1690 order
= get_order(req
->tp_block_size
);
1691 pg_vec
= alloc_pg_vec(req
, order
);
1692 if (unlikely(!pg_vec
))
1696 for (i
= 0; i
< req
->tp_block_nr
; i
++) {
1697 char *ptr
= pg_vec
[i
];
1698 struct tpacket_hdr
*header
;
1701 for (k
= 0; k
< po
->frames_per_block
; k
++) {
1702 header
= (struct tpacket_hdr
*) ptr
;
1703 header
->tp_status
= TP_STATUS_KERNEL
;
1704 ptr
+= req
->tp_frame_size
;
1709 if (unlikely(req
->tp_frame_nr
))
1715 /* Detach socket from network */
1716 spin_lock(&po
->bind_lock
);
1717 was_running
= po
->running
;
1720 __dev_remove_pack(&po
->prot_hook
);
1725 spin_unlock(&po
->bind_lock
);
1730 if (closing
|| atomic_read(&po
->mapped
) == 0) {
1732 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1734 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1735 pg_vec
= XC(po
->pg_vec
, pg_vec
);
1736 po
->frame_max
= (req
->tp_frame_nr
- 1);
1738 po
->frame_size
= req
->tp_frame_size
;
1739 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1741 order
= XC(po
->pg_vec_order
, order
);
1742 req
->tp_block_nr
= XC(po
->pg_vec_len
, req
->tp_block_nr
);
1744 po
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
1745 po
->prot_hook
.func
= po
->pg_vec
? tpacket_rcv
: packet_rcv
;
1746 skb_queue_purge(&sk
->sk_receive_queue
);
1748 if (atomic_read(&po
->mapped
))
1749 printk(KERN_DEBUG
"packet_mmap: vma is busy: %d\n", atomic_read(&po
->mapped
));
1752 spin_lock(&po
->bind_lock
);
1753 if (was_running
&& !po
->running
) {
1757 dev_add_pack(&po
->prot_hook
);
1759 spin_unlock(&po
->bind_lock
);
1764 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
1769 static int packet_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1771 struct sock
*sk
= sock
->sk
;
1772 struct packet_sock
*po
= pkt_sk(sk
);
1774 unsigned long start
;
1781 size
= vma
->vm_end
- vma
->vm_start
;
1784 if (po
->pg_vec
== NULL
)
1786 if (size
!= po
->pg_vec_len
*po
->pg_vec_pages
*PAGE_SIZE
)
1789 start
= vma
->vm_start
;
1790 for (i
= 0; i
< po
->pg_vec_len
; i
++) {
1791 struct page
*page
= virt_to_page(po
->pg_vec
[i
]);
1794 for (pg_num
= 0; pg_num
< po
->pg_vec_pages
; pg_num
++, page
++) {
1795 err
= vm_insert_page(vma
, start
, page
);
1801 atomic_inc(&po
->mapped
);
1802 vma
->vm_ops
= &packet_mmap_ops
;
1812 static const struct proto_ops packet_ops_spkt
= {
1813 .family
= PF_PACKET
,
1814 .owner
= THIS_MODULE
,
1815 .release
= packet_release
,
1816 .bind
= packet_bind_spkt
,
1817 .connect
= sock_no_connect
,
1818 .socketpair
= sock_no_socketpair
,
1819 .accept
= sock_no_accept
,
1820 .getname
= packet_getname_spkt
,
1821 .poll
= datagram_poll
,
1822 .ioctl
= packet_ioctl
,
1823 .listen
= sock_no_listen
,
1824 .shutdown
= sock_no_shutdown
,
1825 .setsockopt
= sock_no_setsockopt
,
1826 .getsockopt
= sock_no_getsockopt
,
1827 .sendmsg
= packet_sendmsg_spkt
,
1828 .recvmsg
= packet_recvmsg
,
1829 .mmap
= sock_no_mmap
,
1830 .sendpage
= sock_no_sendpage
,
1833 static const struct proto_ops packet_ops
= {
1834 .family
= PF_PACKET
,
1835 .owner
= THIS_MODULE
,
1836 .release
= packet_release
,
1837 .bind
= packet_bind
,
1838 .connect
= sock_no_connect
,
1839 .socketpair
= sock_no_socketpair
,
1840 .accept
= sock_no_accept
,
1841 .getname
= packet_getname
,
1842 .poll
= packet_poll
,
1843 .ioctl
= packet_ioctl
,
1844 .listen
= sock_no_listen
,
1845 .shutdown
= sock_no_shutdown
,
1846 .setsockopt
= packet_setsockopt
,
1847 .getsockopt
= packet_getsockopt
,
1848 .sendmsg
= packet_sendmsg
,
1849 .recvmsg
= packet_recvmsg
,
1850 .mmap
= packet_mmap
,
1851 .sendpage
= sock_no_sendpage
,
1854 static struct net_proto_family packet_family_ops
= {
1855 .family
= PF_PACKET
,
1856 .create
= packet_create
,
1857 .owner
= THIS_MODULE
,
1860 static struct notifier_block packet_netdev_notifier
= {
1861 .notifier_call
=packet_notifier
,
1864 #ifdef CONFIG_PROC_FS
1865 static inline struct sock
*packet_seq_idx(loff_t off
)
1868 struct hlist_node
*node
;
1870 sk_for_each(s
, node
, &packet_sklist
) {
1877 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1879 read_lock(&packet_sklist_lock
);
1880 return *pos
? packet_seq_idx(*pos
- 1) : SEQ_START_TOKEN
;
1883 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1886 return (v
== SEQ_START_TOKEN
)
1887 ? sk_head(&packet_sklist
)
1888 : sk_next((struct sock
*)v
) ;
1891 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
1893 read_unlock(&packet_sklist_lock
);
1896 static int packet_seq_show(struct seq_file
*seq
, void *v
)
1898 if (v
== SEQ_START_TOKEN
)
1899 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1902 const struct packet_sock
*po
= pkt_sk(s
);
1905 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1907 atomic_read(&s
->sk_refcnt
),
1912 atomic_read(&s
->sk_rmem_alloc
),
1920 static const struct seq_operations packet_seq_ops
= {
1921 .start
= packet_seq_start
,
1922 .next
= packet_seq_next
,
1923 .stop
= packet_seq_stop
,
1924 .show
= packet_seq_show
,
1927 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
1929 return seq_open(file
, &packet_seq_ops
);
1932 static const struct file_operations packet_seq_fops
= {
1933 .owner
= THIS_MODULE
,
1934 .open
= packet_seq_open
,
1936 .llseek
= seq_lseek
,
1937 .release
= seq_release
,
1942 static void __exit
packet_exit(void)
1944 proc_net_remove(&init_net
, "packet");
1945 unregister_netdevice_notifier(&packet_netdev_notifier
);
1946 sock_unregister(PF_PACKET
);
1947 proto_unregister(&packet_proto
);
1950 static int __init
packet_init(void)
1952 int rc
= proto_register(&packet_proto
, 0);
1957 sock_register(&packet_family_ops
);
1958 register_netdevice_notifier(&packet_netdev_notifier
);
1959 proc_net_fops_create(&init_net
, "packet", 0, &packet_seq_fops
);
1964 module_init(packet_init
);
1965 module_exit(packet_exit
);
1966 MODULE_LICENSE("GPL");
1967 MODULE_ALIAS_NETPROTO(PF_PACKET
);