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>
53 #include <linux/sched.h>
55 #include <linux/capability.h>
56 #include <linux/fcntl.h>
57 #include <linux/socket.h>
59 #include <linux/inet.h>
60 #include <linux/netdevice.h>
61 #include <linux/if_packet.h>
62 #include <linux/wireless.h>
63 #include <linux/kernel.h>
64 #include <linux/kmod.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>
87 #define CONFIG_SOCK_PACKET 1
90 Proposed replacement for SIOC{ADD,DEL}MULTI and
91 IFF_PROMISC, IFF_ALLMULTI flags.
93 It is more expensive, but I believe,
94 it is really correct solution: reentereble, safe and fault tolerant.
96 IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
97 reference count and global flag, so that real status is
98 (gflag|(count != 0)), so that we can use obsolete faulty interface
99 not harming clever users.
101 #define CONFIG_PACKET_MULTICAST 1
105 - if device has no dev->hard_header routine, it adds and removes ll header
106 inside itself. In this case ll header is invisible outside of device,
107 but higher levels still should reserve dev->hard_header_len.
108 Some devices are enough clever to reallocate skb, when header
109 will not fit to reserved space (tunnel), another ones are silly
111 - packet socket receives packets with pulled ll header,
112 so that SOCK_RAW should push it back.
117 Incoming, dev->hard_header!=NULL
121 Outgoing, dev->hard_header!=NULL
125 Incoming, dev->hard_header==NULL
126 mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
127 PPP makes it, that is wrong, because introduce assymetry
128 between rx and tx paths.
131 Outgoing, dev->hard_header==NULL
132 mac.raw -> data. ll header is still not built!
136 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
142 dev->hard_header != NULL
146 dev->hard_header == NULL (ll header is added by device, we cannot control it)
150 We should set nh.raw on output to correct posistion,
151 packet classifier depends on it.
154 /* List of all packet sockets. */
155 static HLIST_HEAD(packet_sklist
);
156 static DEFINE_RWLOCK(packet_sklist_lock
);
158 static atomic_t packet_socks_nr
;
161 /* Private packet socket structures. */
163 #ifdef CONFIG_PACKET_MULTICAST
166 struct packet_mclist
*next
;
171 unsigned char addr
[MAX_ADDR_LEN
];
173 /* identical to struct packet_mreq except it has
174 * a longer address field.
176 struct packet_mreq_max
179 unsigned short mr_type
;
180 unsigned short mr_alen
;
181 unsigned char mr_address
[MAX_ADDR_LEN
];
184 #ifdef CONFIG_PACKET_MMAP
185 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
);
188 static void packet_flush_mclist(struct sock
*sk
);
191 /* struct sock has to be the first member of packet_sock */
193 struct tpacket_stats stats
;
194 #ifdef CONFIG_PACKET_MMAP
197 unsigned int frames_per_block
;
198 unsigned int frame_size
;
199 unsigned int frame_max
;
202 struct packet_type prot_hook
;
203 spinlock_t bind_lock
;
204 unsigned int running
:1, /* prot_hook is attached*/
206 int ifindex
; /* bound device */
208 #ifdef CONFIG_PACKET_MULTICAST
209 struct packet_mclist
*mclist
;
211 #ifdef CONFIG_PACKET_MMAP
213 unsigned int pg_vec_order
;
214 unsigned int pg_vec_pages
;
215 unsigned int pg_vec_len
;
219 struct packet_skb_cb
{
220 unsigned int origlen
;
222 struct sockaddr_pkt pkt
;
223 struct sockaddr_ll ll
;
227 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
229 #ifdef CONFIG_PACKET_MMAP
231 static inline char *packet_lookup_frame(struct packet_sock
*po
, unsigned int position
)
233 unsigned int pg_vec_pos
, frame_offset
;
236 pg_vec_pos
= position
/ po
->frames_per_block
;
237 frame_offset
= position
% po
->frames_per_block
;
239 frame
= po
->pg_vec
[pg_vec_pos
] + (frame_offset
* po
->frame_size
);
245 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
247 return (struct packet_sock
*)sk
;
250 static void packet_sock_destruct(struct sock
*sk
)
252 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
253 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
255 if (!sock_flag(sk
, SOCK_DEAD
)) {
256 printk("Attempt to release alive packet socket: %p\n", sk
);
260 atomic_dec(&packet_socks_nr
);
261 #ifdef PACKET_REFCNT_DEBUG
262 printk(KERN_DEBUG
"PACKET socket %p is free, %d are alive\n", sk
, atomic_read(&packet_socks_nr
));
267 static const struct proto_ops packet_ops
;
269 #ifdef CONFIG_SOCK_PACKET
270 static const struct proto_ops packet_ops_spkt
;
272 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
275 struct sockaddr_pkt
*spkt
;
278 * When we registered the protocol we saved the socket in the data
279 * field for just this event.
282 sk
= pt
->af_packet_priv
;
285 * Yank back the headers [hope the device set this
286 * right or kerboom...]
288 * Incoming packets have ll header pulled,
291 * For outgoing ones skb->data == skb->mac.raw
292 * so that this procedure is noop.
295 if (skb
->pkt_type
== PACKET_LOOPBACK
)
298 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
301 /* drop any routing info */
302 dst_release(skb
->dst
);
305 /* drop conntrack reference */
308 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
310 skb_push(skb
, skb
->data
-skb
->mac
.raw
);
313 * The SOCK_PACKET socket receives _all_ frames.
316 spkt
->spkt_family
= dev
->type
;
317 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
318 spkt
->spkt_protocol
= skb
->protocol
;
321 * Charge the memory to the socket. This is done specifically
322 * to prevent sockets using all the memory up.
325 if (sock_queue_rcv_skb(sk
,skb
) == 0)
336 * Output a raw packet to a device layer. This bypasses all the other
337 * protocol layers and you must therefore supply it with a complete frame
340 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
341 struct msghdr
*msg
, size_t len
)
343 struct sock
*sk
= sock
->sk
;
344 struct sockaddr_pkt
*saddr
=(struct sockaddr_pkt
*)msg
->msg_name
;
346 struct net_device
*dev
;
351 * Get and verify the address.
356 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
358 if (msg
->msg_namelen
==sizeof(struct sockaddr_pkt
))
359 proto
=saddr
->spkt_protocol
;
362 return(-ENOTCONN
); /* SOCK_PACKET must be sent giving an address */
365 * Find the device first to size check it
368 saddr
->spkt_device
[13] = 0;
369 dev
= dev_get_by_name(saddr
->spkt_device
);
375 if (!(dev
->flags
& IFF_UP
))
379 * You may not queue a frame bigger than the mtu. This is the lowest level
380 * raw protocol and you must do your own fragmentation at this level.
384 if (len
> dev
->mtu
+ dev
->hard_header_len
)
388 skb
= sock_wmalloc(sk
, len
+ LL_RESERVED_SPACE(dev
), 0, GFP_KERNEL
);
391 * If the write buffer is full, then tough. At this level the user gets to
392 * deal with the problem - do your own algorithmic backoffs. That's far
403 /* FIXME: Save some space for broken drivers that write a
404 * hard header at transmission time by themselves. PPP is the
405 * notable one here. This should really be fixed at the driver level.
407 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
408 skb
->nh
.raw
= skb
->data
;
410 /* Try to align data part correctly */
411 if (dev
->hard_header
) {
412 skb
->data
-= dev
->hard_header_len
;
413 skb
->tail
-= dev
->hard_header_len
;
414 if (len
< dev
->hard_header_len
)
415 skb
->nh
.raw
= skb
->data
;
418 /* Returns -EFAULT on error */
419 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
420 skb
->protocol
= proto
;
422 skb
->priority
= sk
->sk_priority
;
443 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
446 struct sk_filter
*filter
;
449 filter
= rcu_dereference(sk
->sk_filter
);
451 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
452 rcu_read_unlock_bh();
458 This function makes lazy skb cloning in hope that most of packets
459 are discarded by BPF.
461 Note tricky part: we DO mangle shared skb! skb->data, skb->len
462 and skb->cb are mangled. It works because (and until) packets
463 falling here are owned by current CPU. Output packets are cloned
464 by dev_queue_xmit_nit(), input packets are processed by net_bh
465 sequencially, so that if we return skb to original state on exit,
466 we will not harm anyone.
469 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
472 struct sockaddr_ll
*sll
;
473 struct packet_sock
*po
;
474 u8
* skb_head
= skb
->data
;
475 int skb_len
= skb
->len
;
476 unsigned int snaplen
, res
;
478 if (skb
->pkt_type
== PACKET_LOOPBACK
)
481 sk
= pt
->af_packet_priv
;
486 if (dev
->hard_header
) {
487 /* The device has an explicit notion of ll header,
488 exported to higher levels.
490 Otherwise, the device hides datails of it frame
491 structure, so that corresponding packet head
492 never delivered to user.
494 if (sk
->sk_type
!= SOCK_DGRAM
)
495 skb_push(skb
, skb
->data
- skb
->mac
.raw
);
496 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
497 /* Special case: outgoing packets have ll header at head */
498 skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
504 res
= run_filter(skb
, sk
, snaplen
);
510 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
511 (unsigned)sk
->sk_rcvbuf
)
514 if (skb_shared(skb
)) {
515 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
519 if (skb_head
!= skb
->data
) {
520 skb
->data
= skb_head
;
527 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
530 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
531 sll
->sll_family
= AF_PACKET
;
532 sll
->sll_hatype
= dev
->type
;
533 sll
->sll_protocol
= skb
->protocol
;
534 sll
->sll_pkttype
= skb
->pkt_type
;
535 sll
->sll_ifindex
= dev
->ifindex
;
538 if (dev
->hard_header_parse
)
539 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
541 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
543 if (pskb_trim(skb
, snaplen
))
546 skb_set_owner_r(skb
, sk
);
548 dst_release(skb
->dst
);
551 /* drop conntrack reference */
554 spin_lock(&sk
->sk_receive_queue
.lock
);
555 po
->stats
.tp_packets
++;
556 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
557 spin_unlock(&sk
->sk_receive_queue
.lock
);
558 sk
->sk_data_ready(sk
, skb
->len
);
562 spin_lock(&sk
->sk_receive_queue
.lock
);
563 po
->stats
.tp_drops
++;
564 spin_unlock(&sk
->sk_receive_queue
.lock
);
567 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
568 skb
->data
= skb_head
;
576 #ifdef CONFIG_PACKET_MMAP
577 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
580 struct packet_sock
*po
;
581 struct sockaddr_ll
*sll
;
582 struct tpacket_hdr
*h
;
583 u8
* skb_head
= skb
->data
;
584 int skb_len
= skb
->len
;
585 unsigned int snaplen
, res
;
586 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
587 unsigned short macoff
, netoff
;
588 struct sk_buff
*copy_skb
= NULL
;
590 if (skb
->pkt_type
== PACKET_LOOPBACK
)
593 sk
= pt
->af_packet_priv
;
596 if (dev
->hard_header
) {
597 if (sk
->sk_type
!= SOCK_DGRAM
)
598 skb_push(skb
, skb
->data
- skb
->mac
.raw
);
599 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
600 /* Special case: outgoing packets have ll header at head */
601 skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
605 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
606 status
|= TP_STATUS_CSUMNOTREADY
;
610 res
= run_filter(skb
, sk
, snaplen
);
616 if (sk
->sk_type
== SOCK_DGRAM
) {
617 macoff
= netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
) + 16;
619 unsigned maclen
= skb
->nh
.raw
- skb
->data
;
620 netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
+ (maclen
< 16 ? 16 : maclen
));
621 macoff
= netoff
- maclen
;
624 if (macoff
+ snaplen
> po
->frame_size
) {
625 if (po
->copy_thresh
&&
626 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
627 (unsigned)sk
->sk_rcvbuf
) {
628 if (skb_shared(skb
)) {
629 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
631 copy_skb
= skb_get(skb
);
632 skb_head
= skb
->data
;
635 skb_set_owner_r(copy_skb
, sk
);
637 snaplen
= po
->frame_size
- macoff
;
638 if ((int)snaplen
< 0)
642 spin_lock(&sk
->sk_receive_queue
.lock
);
643 h
= (struct tpacket_hdr
*)packet_lookup_frame(po
, po
->head
);
647 po
->head
= po
->head
!= po
->frame_max
? po
->head
+1 : 0;
648 po
->stats
.tp_packets
++;
650 status
|= TP_STATUS_COPY
;
651 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
653 if (!po
->stats
.tp_drops
)
654 status
&= ~TP_STATUS_LOSING
;
655 spin_unlock(&sk
->sk_receive_queue
.lock
);
657 skb_copy_bits(skb
, 0, (u8
*)h
+ macoff
, snaplen
);
659 h
->tp_len
= skb
->len
;
660 h
->tp_snaplen
= snaplen
;
663 if (skb
->tstamp
.off_sec
== 0) {
664 __net_timestamp(skb
);
665 sock_enable_timestamp(sk
);
667 h
->tp_sec
= skb
->tstamp
.off_sec
;
668 h
->tp_usec
= skb
->tstamp
.off_usec
;
670 sll
= (struct sockaddr_ll
*)((u8
*)h
+ TPACKET_ALIGN(sizeof(*h
)));
672 if (dev
->hard_header_parse
)
673 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
674 sll
->sll_family
= AF_PACKET
;
675 sll
->sll_hatype
= dev
->type
;
676 sll
->sll_protocol
= skb
->protocol
;
677 sll
->sll_pkttype
= skb
->pkt_type
;
678 sll
->sll_ifindex
= dev
->ifindex
;
680 h
->tp_status
= status
;
684 struct page
*p_start
, *p_end
;
685 u8
*h_end
= (u8
*)h
+ macoff
+ snaplen
- 1;
687 p_start
= virt_to_page(h
);
688 p_end
= virt_to_page(h_end
);
689 while (p_start
<= p_end
) {
690 flush_dcache_page(p_start
);
695 sk
->sk_data_ready(sk
, 0);
698 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
699 skb
->data
= skb_head
;
707 po
->stats
.tp_drops
++;
708 spin_unlock(&sk
->sk_receive_queue
.lock
);
710 sk
->sk_data_ready(sk
, 0);
719 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
720 struct msghdr
*msg
, size_t len
)
722 struct sock
*sk
= sock
->sk
;
723 struct sockaddr_ll
*saddr
=(struct sockaddr_ll
*)msg
->msg_name
;
725 struct net_device
*dev
;
728 int ifindex
, err
, reserve
= 0;
731 * Get and verify the address.
735 struct packet_sock
*po
= pkt_sk(sk
);
737 ifindex
= po
->ifindex
;
742 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
744 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
746 ifindex
= saddr
->sll_ifindex
;
747 proto
= saddr
->sll_protocol
;
748 addr
= saddr
->sll_addr
;
752 dev
= dev_get_by_index(ifindex
);
756 if (sock
->type
== SOCK_RAW
)
757 reserve
= dev
->hard_header_len
;
760 if (!(dev
->flags
& IFF_UP
))
764 if (len
> dev
->mtu
+reserve
)
767 skb
= sock_alloc_send_skb(sk
, len
+ LL_RESERVED_SPACE(dev
),
768 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
772 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
773 skb
->nh
.raw
= skb
->data
;
775 if (dev
->hard_header
) {
778 res
= dev
->hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
);
779 if (sock
->type
!= SOCK_DGRAM
) {
780 skb
->tail
= skb
->data
;
786 /* Returns -EFAULT on error */
787 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
791 skb
->protocol
= proto
;
793 skb
->priority
= sk
->sk_priority
;
799 err
= dev_queue_xmit(skb
);
800 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
817 * Close a PACKET socket. This is fairly simple. We immediately go
818 * to 'closed' state and remove our protocol entry in the device list.
821 static int packet_release(struct socket
*sock
)
823 struct sock
*sk
= sock
->sk
;
824 struct packet_sock
*po
;
831 write_lock_bh(&packet_sklist_lock
);
832 sk_del_node_init(sk
);
833 write_unlock_bh(&packet_sklist_lock
);
836 * Unhook packet receive handler.
841 * Remove the protocol hook
843 dev_remove_pack(&po
->prot_hook
);
849 #ifdef CONFIG_PACKET_MULTICAST
850 packet_flush_mclist(sk
);
853 #ifdef CONFIG_PACKET_MMAP
855 struct tpacket_req req
;
856 memset(&req
, 0, sizeof(req
));
857 packet_set_ring(sk
, &req
, 1);
862 * Now the socket is dead. No more input will appear.
870 skb_queue_purge(&sk
->sk_receive_queue
);
877 * Attach a packet hook.
880 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
882 struct packet_sock
*po
= pkt_sk(sk
);
884 * Detach an existing hook if present.
889 spin_lock(&po
->bind_lock
);
894 spin_unlock(&po
->bind_lock
);
895 dev_remove_pack(&po
->prot_hook
);
896 spin_lock(&po
->bind_lock
);
900 po
->prot_hook
.type
= protocol
;
901 po
->prot_hook
.dev
= dev
;
903 po
->ifindex
= dev
? dev
->ifindex
: 0;
909 if (dev
->flags
&IFF_UP
) {
910 dev_add_pack(&po
->prot_hook
);
914 sk
->sk_err
= ENETDOWN
;
915 if (!sock_flag(sk
, SOCK_DEAD
))
916 sk
->sk_error_report(sk
);
919 dev_add_pack(&po
->prot_hook
);
925 spin_unlock(&po
->bind_lock
);
931 * Bind a packet socket to a device
934 #ifdef CONFIG_SOCK_PACKET
936 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
938 struct sock
*sk
=sock
->sk
;
940 struct net_device
*dev
;
947 if (addr_len
!= sizeof(struct sockaddr
))
949 strlcpy(name
,uaddr
->sa_data
,sizeof(name
));
951 dev
= dev_get_by_name(name
);
953 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
960 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
962 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
963 struct sock
*sk
=sock
->sk
;
964 struct net_device
*dev
= NULL
;
972 if (addr_len
< sizeof(struct sockaddr_ll
))
974 if (sll
->sll_family
!= AF_PACKET
)
977 if (sll
->sll_ifindex
) {
979 dev
= dev_get_by_index(sll
->sll_ifindex
);
983 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
991 static struct proto packet_proto
= {
993 .owner
= THIS_MODULE
,
994 .obj_size
= sizeof(struct packet_sock
),
998 * Create a packet of type SOCK_PACKET.
1001 static int packet_create(struct socket
*sock
, int protocol
)
1004 struct packet_sock
*po
;
1005 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1008 if (!capable(CAP_NET_RAW
))
1010 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
1011 #ifdef CONFIG_SOCK_PACKET
1012 && sock
->type
!= SOCK_PACKET
1015 return -ESOCKTNOSUPPORT
;
1017 sock
->state
= SS_UNCONNECTED
;
1020 sk
= sk_alloc(PF_PACKET
, GFP_KERNEL
, &packet_proto
, 1);
1024 sock
->ops
= &packet_ops
;
1025 #ifdef CONFIG_SOCK_PACKET
1026 if (sock
->type
== SOCK_PACKET
)
1027 sock
->ops
= &packet_ops_spkt
;
1029 sock_init_data(sock
, sk
);
1032 sk
->sk_family
= PF_PACKET
;
1035 sk
->sk_destruct
= packet_sock_destruct
;
1036 atomic_inc(&packet_socks_nr
);
1039 * Attach a protocol block
1042 spin_lock_init(&po
->bind_lock
);
1043 po
->prot_hook
.func
= packet_rcv
;
1044 #ifdef CONFIG_SOCK_PACKET
1045 if (sock
->type
== SOCK_PACKET
)
1046 po
->prot_hook
.func
= packet_rcv_spkt
;
1048 po
->prot_hook
.af_packet_priv
= sk
;
1051 po
->prot_hook
.type
= proto
;
1052 dev_add_pack(&po
->prot_hook
);
1057 write_lock_bh(&packet_sklist_lock
);
1058 sk_add_node(sk
, &packet_sklist
);
1059 write_unlock_bh(&packet_sklist_lock
);
1066 * Pull a packet from our receive queue and hand it to the user.
1067 * If necessary we block.
1070 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1071 struct msghdr
*msg
, size_t len
, int flags
)
1073 struct sock
*sk
= sock
->sk
;
1074 struct sk_buff
*skb
;
1076 struct sockaddr_ll
*sll
;
1079 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1083 /* What error should we return now? EUNATTACH? */
1084 if (pkt_sk(sk
)->ifindex
< 0)
1089 * Call the generic datagram receiver. This handles all sorts
1090 * of horrible races and re-entrancy so we can forget about it
1091 * in the protocol layers.
1093 * Now it will return ENETDOWN, if device have just gone down,
1094 * but then it will block.
1097 skb
=skb_recv_datagram(sk
,flags
,flags
&MSG_DONTWAIT
,&err
);
1100 * An error occurred so return it. Because skb_recv_datagram()
1101 * handles the blocking we don't see and worry about blocking
1109 * If the address length field is there to be filled in, we fill
1113 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1114 if (sock
->type
== SOCK_PACKET
)
1115 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1117 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1120 * You lose any data beyond the buffer you gave. If it worries a
1121 * user program they can ask the device for its MTU anyway.
1128 msg
->msg_flags
|=MSG_TRUNC
;
1131 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1135 sock_recv_timestamp(msg
, sk
, skb
);
1138 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1141 if (pkt_sk(sk
)->auxdata
) {
1142 struct tpacket_auxdata aux
;
1144 aux
.tp_status
= TP_STATUS_USER
;
1145 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1146 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1147 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1148 aux
.tp_snaplen
= skb
->len
;
1150 aux
.tp_net
= skb
->nh
.raw
- skb
->data
;
1152 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1156 * Free or return the buffer as appropriate. Again this
1157 * hides all the races and re-entrancy issues from us.
1159 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1162 skb_free_datagram(sk
, skb
);
1167 #ifdef CONFIG_SOCK_PACKET
1168 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1169 int *uaddr_len
, int peer
)
1171 struct net_device
*dev
;
1172 struct sock
*sk
= sock
->sk
;
1177 uaddr
->sa_family
= AF_PACKET
;
1178 dev
= dev_get_by_index(pkt_sk(sk
)->ifindex
);
1180 strlcpy(uaddr
->sa_data
, dev
->name
, 15);
1183 memset(uaddr
->sa_data
, 0, 14);
1184 *uaddr_len
= sizeof(*uaddr
);
1190 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1191 int *uaddr_len
, int peer
)
1193 struct net_device
*dev
;
1194 struct sock
*sk
= sock
->sk
;
1195 struct packet_sock
*po
= pkt_sk(sk
);
1196 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1201 sll
->sll_family
= AF_PACKET
;
1202 sll
->sll_ifindex
= po
->ifindex
;
1203 sll
->sll_protocol
= po
->num
;
1204 dev
= dev_get_by_index(po
->ifindex
);
1206 sll
->sll_hatype
= dev
->type
;
1207 sll
->sll_halen
= dev
->addr_len
;
1208 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1211 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1214 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1219 #ifdef CONFIG_PACKET_MULTICAST
1220 static void packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1223 case PACKET_MR_MULTICAST
:
1225 dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1227 dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1229 case PACKET_MR_PROMISC
:
1230 dev_set_promiscuity(dev
, what
);
1232 case PACKET_MR_ALLMULTI
:
1233 dev_set_allmulti(dev
, what
);
1239 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1241 for ( ; i
; i
=i
->next
) {
1242 if (i
->ifindex
== dev
->ifindex
)
1243 packet_dev_mc(dev
, i
, what
);
1247 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1249 struct packet_sock
*po
= pkt_sk(sk
);
1250 struct packet_mclist
*ml
, *i
;
1251 struct net_device
*dev
;
1257 dev
= __dev_get_by_index(mreq
->mr_ifindex
);
1262 if (mreq
->mr_alen
> dev
->addr_len
)
1266 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1271 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1272 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1273 ml
->type
== mreq
->mr_type
&&
1274 ml
->alen
== mreq
->mr_alen
&&
1275 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1277 /* Free the new element ... */
1283 i
->type
= mreq
->mr_type
;
1284 i
->ifindex
= mreq
->mr_ifindex
;
1285 i
->alen
= mreq
->mr_alen
;
1286 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1288 i
->next
= po
->mclist
;
1290 packet_dev_mc(dev
, i
, +1);
1297 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1299 struct packet_mclist
*ml
, **mlp
;
1303 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1304 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1305 ml
->type
== mreq
->mr_type
&&
1306 ml
->alen
== mreq
->mr_alen
&&
1307 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1308 if (--ml
->count
== 0) {
1309 struct net_device
*dev
;
1311 dev
= dev_get_by_index(ml
->ifindex
);
1313 packet_dev_mc(dev
, ml
, -1);
1323 return -EADDRNOTAVAIL
;
1326 static void packet_flush_mclist(struct sock
*sk
)
1328 struct packet_sock
*po
= pkt_sk(sk
);
1329 struct packet_mclist
*ml
;
1335 while ((ml
= po
->mclist
) != NULL
) {
1336 struct net_device
*dev
;
1338 po
->mclist
= ml
->next
;
1339 if ((dev
= dev_get_by_index(ml
->ifindex
)) != NULL
) {
1340 packet_dev_mc(dev
, ml
, -1);
1350 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1352 struct sock
*sk
= sock
->sk
;
1353 struct packet_sock
*po
= pkt_sk(sk
);
1356 if (level
!= SOL_PACKET
)
1357 return -ENOPROTOOPT
;
1360 #ifdef CONFIG_PACKET_MULTICAST
1361 case PACKET_ADD_MEMBERSHIP
:
1362 case PACKET_DROP_MEMBERSHIP
:
1364 struct packet_mreq_max mreq
;
1366 memset(&mreq
, 0, sizeof(mreq
));
1367 if (len
< sizeof(struct packet_mreq
))
1369 if (len
> sizeof(mreq
))
1371 if (copy_from_user(&mreq
,optval
,len
))
1373 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1375 if (optname
== PACKET_ADD_MEMBERSHIP
)
1376 ret
= packet_mc_add(sk
, &mreq
);
1378 ret
= packet_mc_drop(sk
, &mreq
);
1382 #ifdef CONFIG_PACKET_MMAP
1383 case PACKET_RX_RING
:
1385 struct tpacket_req req
;
1387 if (optlen
<sizeof(req
))
1389 if (copy_from_user(&req
,optval
,sizeof(req
)))
1391 return packet_set_ring(sk
, &req
, 0);
1393 case PACKET_COPY_THRESH
:
1397 if (optlen
!=sizeof(val
))
1399 if (copy_from_user(&val
,optval
,sizeof(val
)))
1402 pkt_sk(sk
)->copy_thresh
= val
;
1406 case PACKET_AUXDATA
:
1410 if (optlen
< sizeof(val
))
1412 if (copy_from_user(&val
, optval
, sizeof(val
)))
1415 po
->auxdata
= !!val
;
1419 return -ENOPROTOOPT
;
1423 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1424 char __user
*optval
, int __user
*optlen
)
1428 struct sock
*sk
= sock
->sk
;
1429 struct packet_sock
*po
= pkt_sk(sk
);
1431 struct tpacket_stats st
;
1433 if (level
!= SOL_PACKET
)
1434 return -ENOPROTOOPT
;
1436 if (get_user(len
, optlen
))
1443 case PACKET_STATISTICS
:
1444 if (len
> sizeof(struct tpacket_stats
))
1445 len
= sizeof(struct tpacket_stats
);
1446 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1448 memset(&po
->stats
, 0, sizeof(st
));
1449 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1450 st
.tp_packets
+= st
.tp_drops
;
1454 case PACKET_AUXDATA
:
1455 if (len
> sizeof(int))
1462 return -ENOPROTOOPT
;
1465 if (put_user(len
, optlen
))
1467 if (copy_to_user(optval
, data
, len
))
1473 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1476 struct hlist_node
*node
;
1477 struct net_device
*dev
= (struct net_device
*)data
;
1479 read_lock(&packet_sklist_lock
);
1480 sk_for_each(sk
, node
, &packet_sklist
) {
1481 struct packet_sock
*po
= pkt_sk(sk
);
1484 case NETDEV_UNREGISTER
:
1485 #ifdef CONFIG_PACKET_MULTICAST
1487 packet_dev_mclist(dev
, po
->mclist
, -1);
1491 if (dev
->ifindex
== po
->ifindex
) {
1492 spin_lock(&po
->bind_lock
);
1494 __dev_remove_pack(&po
->prot_hook
);
1497 sk
->sk_err
= ENETDOWN
;
1498 if (!sock_flag(sk
, SOCK_DEAD
))
1499 sk
->sk_error_report(sk
);
1501 if (msg
== NETDEV_UNREGISTER
) {
1503 po
->prot_hook
.dev
= NULL
;
1505 spin_unlock(&po
->bind_lock
);
1509 spin_lock(&po
->bind_lock
);
1510 if (dev
->ifindex
== po
->ifindex
&& po
->num
&&
1512 dev_add_pack(&po
->prot_hook
);
1516 spin_unlock(&po
->bind_lock
);
1520 read_unlock(&packet_sklist_lock
);
1525 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
1528 struct sock
*sk
= sock
->sk
;
1533 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1534 return put_user(amount
, (int __user
*)arg
);
1538 struct sk_buff
*skb
;
1541 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1542 skb
= skb_peek(&sk
->sk_receive_queue
);
1545 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1546 return put_user(amount
, (int __user
*)arg
);
1549 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1559 case SIOCGIFBRDADDR
:
1560 case SIOCSIFBRDADDR
:
1561 case SIOCGIFNETMASK
:
1562 case SIOCSIFNETMASK
:
1563 case SIOCGIFDSTADDR
:
1564 case SIOCSIFDSTADDR
:
1566 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1570 return -ENOIOCTLCMD
;
1575 #ifndef CONFIG_PACKET_MMAP
1576 #define packet_mmap sock_no_mmap
1577 #define packet_poll datagram_poll
1580 static unsigned int packet_poll(struct file
* file
, struct socket
*sock
,
1583 struct sock
*sk
= sock
->sk
;
1584 struct packet_sock
*po
= pkt_sk(sk
);
1585 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1587 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1589 unsigned last
= po
->head
? po
->head
-1 : po
->frame_max
;
1590 struct tpacket_hdr
*h
;
1592 h
= (struct tpacket_hdr
*)packet_lookup_frame(po
, last
);
1595 mask
|= POLLIN
| POLLRDNORM
;
1597 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1602 /* Dirty? Well, I still did not learn better way to account
1606 static void packet_mm_open(struct vm_area_struct
*vma
)
1608 struct file
*file
= vma
->vm_file
;
1609 struct socket
* sock
= file
->private_data
;
1610 struct sock
*sk
= sock
->sk
;
1613 atomic_inc(&pkt_sk(sk
)->mapped
);
1616 static void packet_mm_close(struct vm_area_struct
*vma
)
1618 struct file
*file
= vma
->vm_file
;
1619 struct socket
* sock
= file
->private_data
;
1620 struct sock
*sk
= sock
->sk
;
1623 atomic_dec(&pkt_sk(sk
)->mapped
);
1626 static struct vm_operations_struct packet_mmap_ops
= {
1627 .open
= packet_mm_open
,
1628 .close
=packet_mm_close
,
1631 static inline struct page
*pg_vec_endpage(char *one_pg_vec
, unsigned int order
)
1633 return virt_to_page(one_pg_vec
+ (PAGE_SIZE
<< order
) - 1);
1636 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
1640 for (i
= 0; i
< len
; i
++) {
1641 if (likely(pg_vec
[i
]))
1642 free_pages((unsigned long) pg_vec
[i
], order
);
1647 static inline char *alloc_one_pg_vec_page(unsigned long order
)
1649 return (char *) __get_free_pages(GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
,
1653 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
1655 unsigned int block_nr
= req
->tp_block_nr
;
1659 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
1660 if (unlikely(!pg_vec
))
1663 for (i
= 0; i
< block_nr
; i
++) {
1664 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
1665 if (unlikely(!pg_vec
[i
]))
1666 goto out_free_pgvec
;
1673 free_pg_vec(pg_vec
, order
, block_nr
);
1678 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
)
1680 char **pg_vec
= NULL
;
1681 struct packet_sock
*po
= pkt_sk(sk
);
1682 int was_running
, order
= 0;
1686 if (req
->tp_block_nr
) {
1689 /* Sanity tests and some calculations */
1691 if (unlikely(po
->pg_vec
))
1694 if (unlikely((int)req
->tp_block_size
<= 0))
1696 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
1698 if (unlikely(req
->tp_frame_size
< TPACKET_HDRLEN
))
1700 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
1703 po
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
1704 if (unlikely(po
->frames_per_block
<= 0))
1706 if (unlikely((po
->frames_per_block
* req
->tp_block_nr
) !=
1711 order
= get_order(req
->tp_block_size
);
1712 pg_vec
= alloc_pg_vec(req
, order
);
1713 if (unlikely(!pg_vec
))
1717 for (i
= 0; i
< req
->tp_block_nr
; i
++) {
1718 char *ptr
= pg_vec
[i
];
1719 struct tpacket_hdr
*header
;
1722 for (k
= 0; k
< po
->frames_per_block
; k
++) {
1723 header
= (struct tpacket_hdr
*) ptr
;
1724 header
->tp_status
= TP_STATUS_KERNEL
;
1725 ptr
+= req
->tp_frame_size
;
1730 if (unlikely(req
->tp_frame_nr
))
1736 /* Detach socket from network */
1737 spin_lock(&po
->bind_lock
);
1738 was_running
= po
->running
;
1741 __dev_remove_pack(&po
->prot_hook
);
1746 spin_unlock(&po
->bind_lock
);
1751 if (closing
|| atomic_read(&po
->mapped
) == 0) {
1753 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1755 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1756 pg_vec
= XC(po
->pg_vec
, pg_vec
);
1757 po
->frame_max
= (req
->tp_frame_nr
- 1);
1759 po
->frame_size
= req
->tp_frame_size
;
1760 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1762 order
= XC(po
->pg_vec_order
, order
);
1763 req
->tp_block_nr
= XC(po
->pg_vec_len
, req
->tp_block_nr
);
1765 po
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
1766 po
->prot_hook
.func
= po
->pg_vec
? tpacket_rcv
: packet_rcv
;
1767 skb_queue_purge(&sk
->sk_receive_queue
);
1769 if (atomic_read(&po
->mapped
))
1770 printk(KERN_DEBUG
"packet_mmap: vma is busy: %d\n", atomic_read(&po
->mapped
));
1773 spin_lock(&po
->bind_lock
);
1774 if (was_running
&& !po
->running
) {
1778 dev_add_pack(&po
->prot_hook
);
1780 spin_unlock(&po
->bind_lock
);
1785 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
1790 static int packet_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1792 struct sock
*sk
= sock
->sk
;
1793 struct packet_sock
*po
= pkt_sk(sk
);
1795 unsigned long start
;
1802 size
= vma
->vm_end
- vma
->vm_start
;
1805 if (po
->pg_vec
== NULL
)
1807 if (size
!= po
->pg_vec_len
*po
->pg_vec_pages
*PAGE_SIZE
)
1810 start
= vma
->vm_start
;
1811 for (i
= 0; i
< po
->pg_vec_len
; i
++) {
1812 struct page
*page
= virt_to_page(po
->pg_vec
[i
]);
1815 for (pg_num
= 0; pg_num
< po
->pg_vec_pages
; pg_num
++, page
++) {
1816 err
= vm_insert_page(vma
, start
, page
);
1822 atomic_inc(&po
->mapped
);
1823 vma
->vm_ops
= &packet_mmap_ops
;
1833 #ifdef CONFIG_SOCK_PACKET
1834 static const struct proto_ops packet_ops_spkt
= {
1835 .family
= PF_PACKET
,
1836 .owner
= THIS_MODULE
,
1837 .release
= packet_release
,
1838 .bind
= packet_bind_spkt
,
1839 .connect
= sock_no_connect
,
1840 .socketpair
= sock_no_socketpair
,
1841 .accept
= sock_no_accept
,
1842 .getname
= packet_getname_spkt
,
1843 .poll
= datagram_poll
,
1844 .ioctl
= packet_ioctl
,
1845 .listen
= sock_no_listen
,
1846 .shutdown
= sock_no_shutdown
,
1847 .setsockopt
= sock_no_setsockopt
,
1848 .getsockopt
= sock_no_getsockopt
,
1849 .sendmsg
= packet_sendmsg_spkt
,
1850 .recvmsg
= packet_recvmsg
,
1851 .mmap
= sock_no_mmap
,
1852 .sendpage
= sock_no_sendpage
,
1856 static const struct proto_ops packet_ops
= {
1857 .family
= PF_PACKET
,
1858 .owner
= THIS_MODULE
,
1859 .release
= packet_release
,
1860 .bind
= packet_bind
,
1861 .connect
= sock_no_connect
,
1862 .socketpair
= sock_no_socketpair
,
1863 .accept
= sock_no_accept
,
1864 .getname
= packet_getname
,
1865 .poll
= packet_poll
,
1866 .ioctl
= packet_ioctl
,
1867 .listen
= sock_no_listen
,
1868 .shutdown
= sock_no_shutdown
,
1869 .setsockopt
= packet_setsockopt
,
1870 .getsockopt
= packet_getsockopt
,
1871 .sendmsg
= packet_sendmsg
,
1872 .recvmsg
= packet_recvmsg
,
1873 .mmap
= packet_mmap
,
1874 .sendpage
= sock_no_sendpage
,
1877 static struct net_proto_family packet_family_ops
= {
1878 .family
= PF_PACKET
,
1879 .create
= packet_create
,
1880 .owner
= THIS_MODULE
,
1883 static struct notifier_block packet_netdev_notifier
= {
1884 .notifier_call
=packet_notifier
,
1887 #ifdef CONFIG_PROC_FS
1888 static inline struct sock
*packet_seq_idx(loff_t off
)
1891 struct hlist_node
*node
;
1893 sk_for_each(s
, node
, &packet_sklist
) {
1900 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1902 read_lock(&packet_sklist_lock
);
1903 return *pos
? packet_seq_idx(*pos
- 1) : SEQ_START_TOKEN
;
1906 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1909 return (v
== SEQ_START_TOKEN
)
1910 ? sk_head(&packet_sklist
)
1911 : sk_next((struct sock
*)v
) ;
1914 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
1916 read_unlock(&packet_sklist_lock
);
1919 static int packet_seq_show(struct seq_file
*seq
, void *v
)
1921 if (v
== SEQ_START_TOKEN
)
1922 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1925 const struct packet_sock
*po
= pkt_sk(s
);
1928 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1930 atomic_read(&s
->sk_refcnt
),
1935 atomic_read(&s
->sk_rmem_alloc
),
1943 static struct seq_operations packet_seq_ops
= {
1944 .start
= packet_seq_start
,
1945 .next
= packet_seq_next
,
1946 .stop
= packet_seq_stop
,
1947 .show
= packet_seq_show
,
1950 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
1952 return seq_open(file
, &packet_seq_ops
);
1955 static struct file_operations packet_seq_fops
= {
1956 .owner
= THIS_MODULE
,
1957 .open
= packet_seq_open
,
1959 .llseek
= seq_lseek
,
1960 .release
= seq_release
,
1965 static void __exit
packet_exit(void)
1967 proc_net_remove("packet");
1968 unregister_netdevice_notifier(&packet_netdev_notifier
);
1969 sock_unregister(PF_PACKET
);
1970 proto_unregister(&packet_proto
);
1973 static int __init
packet_init(void)
1975 int rc
= proto_register(&packet_proto
, 0);
1980 sock_register(&packet_family_ops
);
1981 register_netdevice_notifier(&packet_netdev_notifier
);
1982 proc_net_fops_create("packet", 0, &packet_seq_fops
);
1987 module_init(packet_init
);
1988 module_exit(packet_exit
);
1989 MODULE_LICENSE("GPL");
1990 MODULE_ALIAS_NETPROTO(PF_PACKET
);