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/config.h>
53 #include <linux/types.h>
54 #include <linux/sched.h>
56 #include <linux/capability.h>
57 #include <linux/fcntl.h>
58 #include <linux/socket.h>
60 #include <linux/inet.h>
61 #include <linux/netdevice.h>
62 #include <linux/if_packet.h>
63 #include <linux/wireless.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>
76 #include <linux/proc_fs.h>
77 #include <linux/seq_file.h>
78 #include <linux/poll.h>
79 #include <linux/module.h>
80 #include <linux/init.h>
83 #include <net/inet_common.h>
86 #define CONFIG_SOCK_PACKET 1
89 Proposed replacement for SIOC{ADD,DEL}MULTI and
90 IFF_PROMISC, IFF_ALLMULTI flags.
92 It is more expensive, but I believe,
93 it is really correct solution: reentereble, safe and fault tolerant.
95 IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
96 reference count and global flag, so that real status is
97 (gflag|(count != 0)), so that we can use obsolete faulty interface
98 not harming clever users.
100 #define CONFIG_PACKET_MULTICAST 1
104 - if device has no dev->hard_header routine, it adds and removes ll header
105 inside itself. In this case ll header is invisible outside of device,
106 but higher levels still should reserve dev->hard_header_len.
107 Some devices are enough clever to reallocate skb, when header
108 will not fit to reserved space (tunnel), another ones are silly
110 - packet socket receives packets with pulled ll header,
111 so that SOCK_RAW should push it back.
116 Incoming, dev->hard_header!=NULL
120 Outgoing, dev->hard_header!=NULL
124 Incoming, dev->hard_header==NULL
125 mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
126 PPP makes it, that is wrong, because introduce assymetry
127 between rx and tx paths.
130 Outgoing, dev->hard_header==NULL
131 mac.raw -> data. ll header is still not built!
135 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
141 dev->hard_header != NULL
145 dev->hard_header == NULL (ll header is added by device, we cannot control it)
149 We should set nh.raw on output to correct posistion,
150 packet classifier depends on it.
153 /* List of all packet sockets. */
154 static HLIST_HEAD(packet_sklist
);
155 static DEFINE_RWLOCK(packet_sklist_lock
);
157 static atomic_t packet_socks_nr
;
160 /* Private packet socket structures. */
162 #ifdef CONFIG_PACKET_MULTICAST
165 struct packet_mclist
*next
;
170 unsigned char addr
[MAX_ADDR_LEN
];
172 /* identical to struct packet_mreq except it has
173 * a longer address field.
175 struct packet_mreq_max
178 unsigned short mr_type
;
179 unsigned short mr_alen
;
180 unsigned char mr_address
[MAX_ADDR_LEN
];
183 #ifdef CONFIG_PACKET_MMAP
184 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
);
187 static void packet_flush_mclist(struct sock
*sk
);
190 /* struct sock has to be the first member of packet_sock */
192 struct tpacket_stats stats
;
193 #ifdef CONFIG_PACKET_MMAP
196 unsigned int frames_per_block
;
197 unsigned int frame_size
;
198 unsigned int frame_max
;
201 struct packet_type prot_hook
;
202 spinlock_t bind_lock
;
203 char running
; /* prot_hook is attached*/
204 int ifindex
; /* bound device */
206 #ifdef CONFIG_PACKET_MULTICAST
207 struct packet_mclist
*mclist
;
209 #ifdef CONFIG_PACKET_MMAP
211 unsigned int pg_vec_order
;
212 unsigned int pg_vec_pages
;
213 unsigned int pg_vec_len
;
217 #ifdef CONFIG_PACKET_MMAP
219 static inline char *packet_lookup_frame(struct packet_sock
*po
, unsigned int position
)
221 unsigned int pg_vec_pos
, frame_offset
;
224 pg_vec_pos
= position
/ po
->frames_per_block
;
225 frame_offset
= position
% po
->frames_per_block
;
227 frame
= po
->pg_vec
[pg_vec_pos
] + (frame_offset
* po
->frame_size
);
233 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
235 return (struct packet_sock
*)sk
;
238 static void packet_sock_destruct(struct sock
*sk
)
240 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
241 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
243 if (!sock_flag(sk
, SOCK_DEAD
)) {
244 printk("Attempt to release alive packet socket: %p\n", sk
);
248 atomic_dec(&packet_socks_nr
);
249 #ifdef PACKET_REFCNT_DEBUG
250 printk(KERN_DEBUG
"PACKET socket %p is free, %d are alive\n", sk
, atomic_read(&packet_socks_nr
));
255 static const struct proto_ops packet_ops
;
257 #ifdef CONFIG_SOCK_PACKET
258 static const struct proto_ops packet_ops_spkt
;
260 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
263 struct sockaddr_pkt
*spkt
;
266 * When we registered the protocol we saved the socket in the data
267 * field for just this event.
270 sk
= pt
->af_packet_priv
;
273 * Yank back the headers [hope the device set this
274 * right or kerboom...]
276 * Incoming packets have ll header pulled,
279 * For outgoing ones skb->data == skb->mac.raw
280 * so that this procedure is noop.
283 if (skb
->pkt_type
== PACKET_LOOPBACK
)
286 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
289 /* drop any routing info */
290 dst_release(skb
->dst
);
293 /* drop conntrack reference */
296 spkt
= (struct sockaddr_pkt
*)skb
->cb
;
298 skb_push(skb
, skb
->data
-skb
->mac
.raw
);
301 * The SOCK_PACKET socket receives _all_ frames.
304 spkt
->spkt_family
= dev
->type
;
305 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
306 spkt
->spkt_protocol
= skb
->protocol
;
309 * Charge the memory to the socket. This is done specifically
310 * to prevent sockets using all the memory up.
313 if (sock_queue_rcv_skb(sk
,skb
) == 0)
324 * Output a raw packet to a device layer. This bypasses all the other
325 * protocol layers and you must therefore supply it with a complete frame
328 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
329 struct msghdr
*msg
, size_t len
)
331 struct sock
*sk
= sock
->sk
;
332 struct sockaddr_pkt
*saddr
=(struct sockaddr_pkt
*)msg
->msg_name
;
334 struct net_device
*dev
;
335 unsigned short proto
=0;
339 * Get and verify the address.
344 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
346 if (msg
->msg_namelen
==sizeof(struct sockaddr_pkt
))
347 proto
=saddr
->spkt_protocol
;
350 return(-ENOTCONN
); /* SOCK_PACKET must be sent giving an address */
353 * Find the device first to size check it
356 saddr
->spkt_device
[13] = 0;
357 dev
= dev_get_by_name(saddr
->spkt_device
);
363 * You may not queue a frame bigger than the mtu. This is the lowest level
364 * raw protocol and you must do your own fragmentation at this level.
368 if (len
> dev
->mtu
+ dev
->hard_header_len
)
372 skb
= sock_wmalloc(sk
, len
+ LL_RESERVED_SPACE(dev
), 0, GFP_KERNEL
);
375 * If the write buffer is full, then tough. At this level the user gets to
376 * deal with the problem - do your own algorithmic backoffs. That's far
387 /* FIXME: Save some space for broken drivers that write a
388 * hard header at transmission time by themselves. PPP is the
389 * notable one here. This should really be fixed at the driver level.
391 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
392 skb
->nh
.raw
= skb
->data
;
394 /* Try to align data part correctly */
395 if (dev
->hard_header
) {
396 skb
->data
-= dev
->hard_header_len
;
397 skb
->tail
-= dev
->hard_header_len
;
398 if (len
< dev
->hard_header_len
)
399 skb
->nh
.raw
= skb
->data
;
402 /* Returns -EFAULT on error */
403 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
404 skb
->protocol
= proto
;
406 skb
->priority
= sk
->sk_priority
;
411 if (!(dev
->flags
& IFF_UP
))
431 static inline unsigned run_filter(struct sk_buff
*skb
, struct sock
*sk
, unsigned res
)
433 struct sk_filter
*filter
;
436 filter
= sk
->sk_filter
;
438 * Our caller already checked that filter != NULL but we need to
439 * verify that under bh_lock_sock() to be safe
441 if (likely(filter
!= NULL
))
442 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
449 This function makes lazy skb cloning in hope that most of packets
450 are discarded by BPF.
452 Note tricky part: we DO mangle shared skb! skb->data, skb->len
453 and skb->cb are mangled. It works because (and until) packets
454 falling here are owned by current CPU. Output packets are cloned
455 by dev_queue_xmit_nit(), input packets are processed by net_bh
456 sequencially, so that if we return skb to original state on exit,
457 we will not harm anyone.
460 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
463 struct sockaddr_ll
*sll
;
464 struct packet_sock
*po
;
465 u8
* skb_head
= skb
->data
;
466 int skb_len
= skb
->len
;
469 if (skb
->pkt_type
== PACKET_LOOPBACK
)
472 sk
= pt
->af_packet_priv
;
477 if (dev
->hard_header
) {
478 /* The device has an explicit notion of ll header,
479 exported to higher levels.
481 Otherwise, the device hides datails of it frame
482 structure, so that corresponding packet head
483 never delivered to user.
485 if (sk
->sk_type
!= SOCK_DGRAM
)
486 skb_push(skb
, skb
->data
- skb
->mac
.raw
);
487 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
488 /* Special case: outgoing packets have ll header at head */
489 skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
496 unsigned res
= run_filter(skb
, sk
, snaplen
);
503 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
504 (unsigned)sk
->sk_rcvbuf
)
507 if (skb_shared(skb
)) {
508 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
512 if (skb_head
!= skb
->data
) {
513 skb
->data
= skb_head
;
520 sll
= (struct sockaddr_ll
*)skb
->cb
;
521 sll
->sll_family
= AF_PACKET
;
522 sll
->sll_hatype
= dev
->type
;
523 sll
->sll_protocol
= skb
->protocol
;
524 sll
->sll_pkttype
= skb
->pkt_type
;
525 sll
->sll_ifindex
= dev
->ifindex
;
528 if (dev
->hard_header_parse
)
529 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
531 if (pskb_trim(skb
, snaplen
))
534 skb_set_owner_r(skb
, sk
);
536 dst_release(skb
->dst
);
539 /* drop conntrack reference */
542 spin_lock(&sk
->sk_receive_queue
.lock
);
543 po
->stats
.tp_packets
++;
544 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
545 spin_unlock(&sk
->sk_receive_queue
.lock
);
546 sk
->sk_data_ready(sk
, skb
->len
);
550 spin_lock(&sk
->sk_receive_queue
.lock
);
551 po
->stats
.tp_drops
++;
552 spin_unlock(&sk
->sk_receive_queue
.lock
);
555 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
556 skb
->data
= skb_head
;
564 #ifdef CONFIG_PACKET_MMAP
565 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
568 struct packet_sock
*po
;
569 struct sockaddr_ll
*sll
;
570 struct tpacket_hdr
*h
;
571 u8
* skb_head
= skb
->data
;
572 int skb_len
= skb
->len
;
574 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
575 unsigned short macoff
, netoff
;
576 struct sk_buff
*copy_skb
= NULL
;
578 if (skb
->pkt_type
== PACKET_LOOPBACK
)
581 sk
= pt
->af_packet_priv
;
584 if (dev
->hard_header
) {
585 if (sk
->sk_type
!= SOCK_DGRAM
)
586 skb_push(skb
, skb
->data
- skb
->mac
.raw
);
587 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
588 /* Special case: outgoing packets have ll header at head */
589 skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
590 if (skb
->ip_summed
== CHECKSUM_HW
)
591 status
|= TP_STATUS_CSUMNOTREADY
;
598 unsigned res
= run_filter(skb
, sk
, snaplen
);
605 if (sk
->sk_type
== SOCK_DGRAM
) {
606 macoff
= netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
) + 16;
608 unsigned maclen
= skb
->nh
.raw
- skb
->data
;
609 netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
+ (maclen
< 16 ? 16 : maclen
));
610 macoff
= netoff
- maclen
;
613 if (macoff
+ snaplen
> po
->frame_size
) {
614 if (po
->copy_thresh
&&
615 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
616 (unsigned)sk
->sk_rcvbuf
) {
617 if (skb_shared(skb
)) {
618 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
620 copy_skb
= skb_get(skb
);
621 skb_head
= skb
->data
;
624 skb_set_owner_r(copy_skb
, sk
);
626 snaplen
= po
->frame_size
- macoff
;
627 if ((int)snaplen
< 0)
630 if (snaplen
> skb
->len
-skb
->data_len
)
631 snaplen
= skb
->len
-skb
->data_len
;
633 spin_lock(&sk
->sk_receive_queue
.lock
);
634 h
= (struct tpacket_hdr
*)packet_lookup_frame(po
, po
->head
);
638 po
->head
= po
->head
!= po
->frame_max
? po
->head
+1 : 0;
639 po
->stats
.tp_packets
++;
641 status
|= TP_STATUS_COPY
;
642 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
644 if (!po
->stats
.tp_drops
)
645 status
&= ~TP_STATUS_LOSING
;
646 spin_unlock(&sk
->sk_receive_queue
.lock
);
648 memcpy((u8
*)h
+ macoff
, skb
->data
, snaplen
);
650 h
->tp_len
= skb
->len
;
651 h
->tp_snaplen
= snaplen
;
654 if (skb
->tstamp
.off_sec
== 0) {
655 __net_timestamp(skb
);
656 sock_enable_timestamp(sk
);
658 h
->tp_sec
= skb
->tstamp
.off_sec
;
659 h
->tp_usec
= skb
->tstamp
.off_usec
;
661 sll
= (struct sockaddr_ll
*)((u8
*)h
+ TPACKET_ALIGN(sizeof(*h
)));
663 if (dev
->hard_header_parse
)
664 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
665 sll
->sll_family
= AF_PACKET
;
666 sll
->sll_hatype
= dev
->type
;
667 sll
->sll_protocol
= skb
->protocol
;
668 sll
->sll_pkttype
= skb
->pkt_type
;
669 sll
->sll_ifindex
= dev
->ifindex
;
671 h
->tp_status
= status
;
675 struct page
*p_start
, *p_end
;
676 u8
*h_end
= (u8
*)h
+ macoff
+ snaplen
- 1;
678 p_start
= virt_to_page(h
);
679 p_end
= virt_to_page(h_end
);
680 while (p_start
<= p_end
) {
681 flush_dcache_page(p_start
);
686 sk
->sk_data_ready(sk
, 0);
689 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
690 skb
->data
= skb_head
;
698 po
->stats
.tp_drops
++;
699 spin_unlock(&sk
->sk_receive_queue
.lock
);
701 sk
->sk_data_ready(sk
, 0);
710 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
711 struct msghdr
*msg
, size_t len
)
713 struct sock
*sk
= sock
->sk
;
714 struct sockaddr_ll
*saddr
=(struct sockaddr_ll
*)msg
->msg_name
;
716 struct net_device
*dev
;
717 unsigned short proto
;
719 int ifindex
, err
, reserve
= 0;
722 * Get and verify the address.
726 struct packet_sock
*po
= pkt_sk(sk
);
728 ifindex
= po
->ifindex
;
733 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
735 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
737 ifindex
= saddr
->sll_ifindex
;
738 proto
= saddr
->sll_protocol
;
739 addr
= saddr
->sll_addr
;
743 dev
= dev_get_by_index(ifindex
);
747 if (sock
->type
== SOCK_RAW
)
748 reserve
= dev
->hard_header_len
;
751 if (len
> dev
->mtu
+reserve
)
754 skb
= sock_alloc_send_skb(sk
, len
+ LL_RESERVED_SPACE(dev
),
755 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
759 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
760 skb
->nh
.raw
= skb
->data
;
762 if (dev
->hard_header
) {
765 res
= dev
->hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
);
766 if (sock
->type
!= SOCK_DGRAM
) {
767 skb
->tail
= skb
->data
;
773 /* Returns -EFAULT on error */
774 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
778 skb
->protocol
= proto
;
780 skb
->priority
= sk
->sk_priority
;
783 if (!(dev
->flags
& IFF_UP
))
790 err
= dev_queue_xmit(skb
);
791 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
808 * Close a PACKET socket. This is fairly simple. We immediately go
809 * to 'closed' state and remove our protocol entry in the device list.
812 static int packet_release(struct socket
*sock
)
814 struct sock
*sk
= sock
->sk
;
815 struct packet_sock
*po
;
822 write_lock_bh(&packet_sklist_lock
);
823 sk_del_node_init(sk
);
824 write_unlock_bh(&packet_sklist_lock
);
827 * Unhook packet receive handler.
832 * Remove the protocol hook
834 dev_remove_pack(&po
->prot_hook
);
840 #ifdef CONFIG_PACKET_MULTICAST
841 packet_flush_mclist(sk
);
844 #ifdef CONFIG_PACKET_MMAP
846 struct tpacket_req req
;
847 memset(&req
, 0, sizeof(req
));
848 packet_set_ring(sk
, &req
, 1);
853 * Now the socket is dead. No more input will appear.
861 skb_queue_purge(&sk
->sk_receive_queue
);
868 * Attach a packet hook.
871 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, int protocol
)
873 struct packet_sock
*po
= pkt_sk(sk
);
875 * Detach an existing hook if present.
880 spin_lock(&po
->bind_lock
);
885 spin_unlock(&po
->bind_lock
);
886 dev_remove_pack(&po
->prot_hook
);
887 spin_lock(&po
->bind_lock
);
891 po
->prot_hook
.type
= protocol
;
892 po
->prot_hook
.dev
= dev
;
894 po
->ifindex
= dev
? dev
->ifindex
: 0;
900 if (dev
->flags
&IFF_UP
) {
901 dev_add_pack(&po
->prot_hook
);
905 sk
->sk_err
= ENETDOWN
;
906 if (!sock_flag(sk
, SOCK_DEAD
))
907 sk
->sk_error_report(sk
);
910 dev_add_pack(&po
->prot_hook
);
916 spin_unlock(&po
->bind_lock
);
922 * Bind a packet socket to a device
925 #ifdef CONFIG_SOCK_PACKET
927 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
929 struct sock
*sk
=sock
->sk
;
931 struct net_device
*dev
;
938 if (addr_len
!= sizeof(struct sockaddr
))
940 strlcpy(name
,uaddr
->sa_data
,sizeof(name
));
942 dev
= dev_get_by_name(name
);
944 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
951 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
953 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
954 struct sock
*sk
=sock
->sk
;
955 struct net_device
*dev
= NULL
;
963 if (addr_len
< sizeof(struct sockaddr_ll
))
965 if (sll
->sll_family
!= AF_PACKET
)
968 if (sll
->sll_ifindex
) {
970 dev
= dev_get_by_index(sll
->sll_ifindex
);
974 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
982 static struct proto packet_proto
= {
984 .owner
= THIS_MODULE
,
985 .obj_size
= sizeof(struct packet_sock
),
989 * Create a packet of type SOCK_PACKET.
992 static int packet_create(struct socket
*sock
, int protocol
)
995 struct packet_sock
*po
;
998 if (!capable(CAP_NET_RAW
))
1000 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
1001 #ifdef CONFIG_SOCK_PACKET
1002 && sock
->type
!= SOCK_PACKET
1005 return -ESOCKTNOSUPPORT
;
1007 sock
->state
= SS_UNCONNECTED
;
1010 sk
= sk_alloc(PF_PACKET
, GFP_KERNEL
, &packet_proto
, 1);
1014 sock
->ops
= &packet_ops
;
1015 #ifdef CONFIG_SOCK_PACKET
1016 if (sock
->type
== SOCK_PACKET
)
1017 sock
->ops
= &packet_ops_spkt
;
1019 sock_init_data(sock
, sk
);
1022 sk
->sk_family
= PF_PACKET
;
1025 sk
->sk_destruct
= packet_sock_destruct
;
1026 atomic_inc(&packet_socks_nr
);
1029 * Attach a protocol block
1032 spin_lock_init(&po
->bind_lock
);
1033 po
->prot_hook
.func
= packet_rcv
;
1034 #ifdef CONFIG_SOCK_PACKET
1035 if (sock
->type
== SOCK_PACKET
)
1036 po
->prot_hook
.func
= packet_rcv_spkt
;
1038 po
->prot_hook
.af_packet_priv
= sk
;
1041 po
->prot_hook
.type
= protocol
;
1042 dev_add_pack(&po
->prot_hook
);
1047 write_lock_bh(&packet_sklist_lock
);
1048 sk_add_node(sk
, &packet_sklist
);
1049 write_unlock_bh(&packet_sklist_lock
);
1056 * Pull a packet from our receive queue and hand it to the user.
1057 * If necessary we block.
1060 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1061 struct msghdr
*msg
, size_t len
, int flags
)
1063 struct sock
*sk
= sock
->sk
;
1064 struct sk_buff
*skb
;
1066 struct sockaddr_ll
*sll
;
1069 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1073 /* What error should we return now? EUNATTACH? */
1074 if (pkt_sk(sk
)->ifindex
< 0)
1079 * Call the generic datagram receiver. This handles all sorts
1080 * of horrible races and re-entrancy so we can forget about it
1081 * in the protocol layers.
1083 * Now it will return ENETDOWN, if device have just gone down,
1084 * but then it will block.
1087 skb
=skb_recv_datagram(sk
,flags
,flags
&MSG_DONTWAIT
,&err
);
1090 * An error occurred so return it. Because skb_recv_datagram()
1091 * handles the blocking we don't see and worry about blocking
1099 * If the address length field is there to be filled in, we fill
1103 sll
= (struct sockaddr_ll
*)skb
->cb
;
1104 if (sock
->type
== SOCK_PACKET
)
1105 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1107 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1110 * You lose any data beyond the buffer you gave. If it worries a
1111 * user program they can ask the device for its MTU anyway.
1118 msg
->msg_flags
|=MSG_TRUNC
;
1121 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1125 sock_recv_timestamp(msg
, sk
, skb
);
1128 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
1131 * Free or return the buffer as appropriate. Again this
1132 * hides all the races and re-entrancy issues from us.
1134 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1137 skb_free_datagram(sk
, skb
);
1142 #ifdef CONFIG_SOCK_PACKET
1143 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1144 int *uaddr_len
, int peer
)
1146 struct net_device
*dev
;
1147 struct sock
*sk
= sock
->sk
;
1152 uaddr
->sa_family
= AF_PACKET
;
1153 dev
= dev_get_by_index(pkt_sk(sk
)->ifindex
);
1155 strlcpy(uaddr
->sa_data
, dev
->name
, 15);
1158 memset(uaddr
->sa_data
, 0, 14);
1159 *uaddr_len
= sizeof(*uaddr
);
1165 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1166 int *uaddr_len
, int peer
)
1168 struct net_device
*dev
;
1169 struct sock
*sk
= sock
->sk
;
1170 struct packet_sock
*po
= pkt_sk(sk
);
1171 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1176 sll
->sll_family
= AF_PACKET
;
1177 sll
->sll_ifindex
= po
->ifindex
;
1178 sll
->sll_protocol
= po
->num
;
1179 dev
= dev_get_by_index(po
->ifindex
);
1181 sll
->sll_hatype
= dev
->type
;
1182 sll
->sll_halen
= dev
->addr_len
;
1183 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1186 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1189 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1194 #ifdef CONFIG_PACKET_MULTICAST
1195 static void packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1198 case PACKET_MR_MULTICAST
:
1200 dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1202 dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1204 case PACKET_MR_PROMISC
:
1205 dev_set_promiscuity(dev
, what
);
1207 case PACKET_MR_ALLMULTI
:
1208 dev_set_allmulti(dev
, what
);
1214 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1216 for ( ; i
; i
=i
->next
) {
1217 if (i
->ifindex
== dev
->ifindex
)
1218 packet_dev_mc(dev
, i
, what
);
1222 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1224 struct packet_sock
*po
= pkt_sk(sk
);
1225 struct packet_mclist
*ml
, *i
;
1226 struct net_device
*dev
;
1232 dev
= __dev_get_by_index(mreq
->mr_ifindex
);
1237 if (mreq
->mr_alen
> dev
->addr_len
)
1241 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1246 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1247 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1248 ml
->type
== mreq
->mr_type
&&
1249 ml
->alen
== mreq
->mr_alen
&&
1250 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1252 /* Free the new element ... */
1258 i
->type
= mreq
->mr_type
;
1259 i
->ifindex
= mreq
->mr_ifindex
;
1260 i
->alen
= mreq
->mr_alen
;
1261 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1263 i
->next
= po
->mclist
;
1265 packet_dev_mc(dev
, i
, +1);
1272 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1274 struct packet_mclist
*ml
, **mlp
;
1278 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1279 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1280 ml
->type
== mreq
->mr_type
&&
1281 ml
->alen
== mreq
->mr_alen
&&
1282 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1283 if (--ml
->count
== 0) {
1284 struct net_device
*dev
;
1286 dev
= dev_get_by_index(ml
->ifindex
);
1288 packet_dev_mc(dev
, ml
, -1);
1298 return -EADDRNOTAVAIL
;
1301 static void packet_flush_mclist(struct sock
*sk
)
1303 struct packet_sock
*po
= pkt_sk(sk
);
1304 struct packet_mclist
*ml
;
1310 while ((ml
= po
->mclist
) != NULL
) {
1311 struct net_device
*dev
;
1313 po
->mclist
= ml
->next
;
1314 if ((dev
= dev_get_by_index(ml
->ifindex
)) != NULL
) {
1315 packet_dev_mc(dev
, ml
, -1);
1325 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1327 struct sock
*sk
= sock
->sk
;
1330 if (level
!= SOL_PACKET
)
1331 return -ENOPROTOOPT
;
1334 #ifdef CONFIG_PACKET_MULTICAST
1335 case PACKET_ADD_MEMBERSHIP
:
1336 case PACKET_DROP_MEMBERSHIP
:
1338 struct packet_mreq_max mreq
;
1340 memset(&mreq
, 0, sizeof(mreq
));
1341 if (len
< sizeof(struct packet_mreq
))
1343 if (len
> sizeof(mreq
))
1345 if (copy_from_user(&mreq
,optval
,len
))
1347 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1349 if (optname
== PACKET_ADD_MEMBERSHIP
)
1350 ret
= packet_mc_add(sk
, &mreq
);
1352 ret
= packet_mc_drop(sk
, &mreq
);
1356 #ifdef CONFIG_PACKET_MMAP
1357 case PACKET_RX_RING
:
1359 struct tpacket_req req
;
1361 if (optlen
<sizeof(req
))
1363 if (copy_from_user(&req
,optval
,sizeof(req
)))
1365 return packet_set_ring(sk
, &req
, 0);
1367 case PACKET_COPY_THRESH
:
1371 if (optlen
!=sizeof(val
))
1373 if (copy_from_user(&val
,optval
,sizeof(val
)))
1376 pkt_sk(sk
)->copy_thresh
= val
;
1381 return -ENOPROTOOPT
;
1385 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1386 char __user
*optval
, int __user
*optlen
)
1389 struct sock
*sk
= sock
->sk
;
1390 struct packet_sock
*po
= pkt_sk(sk
);
1392 if (level
!= SOL_PACKET
)
1393 return -ENOPROTOOPT
;
1395 if (get_user(len
, optlen
))
1402 case PACKET_STATISTICS
:
1404 struct tpacket_stats st
;
1406 if (len
> sizeof(struct tpacket_stats
))
1407 len
= sizeof(struct tpacket_stats
);
1408 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1410 memset(&po
->stats
, 0, sizeof(st
));
1411 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1412 st
.tp_packets
+= st
.tp_drops
;
1414 if (copy_to_user(optval
, &st
, len
))
1419 return -ENOPROTOOPT
;
1422 if (put_user(len
, optlen
))
1428 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1431 struct hlist_node
*node
;
1432 struct net_device
*dev
= (struct net_device
*)data
;
1434 read_lock(&packet_sklist_lock
);
1435 sk_for_each(sk
, node
, &packet_sklist
) {
1436 struct packet_sock
*po
= pkt_sk(sk
);
1439 case NETDEV_UNREGISTER
:
1440 #ifdef CONFIG_PACKET_MULTICAST
1442 packet_dev_mclist(dev
, po
->mclist
, -1);
1446 if (dev
->ifindex
== po
->ifindex
) {
1447 spin_lock(&po
->bind_lock
);
1449 __dev_remove_pack(&po
->prot_hook
);
1452 sk
->sk_err
= ENETDOWN
;
1453 if (!sock_flag(sk
, SOCK_DEAD
))
1454 sk
->sk_error_report(sk
);
1456 if (msg
== NETDEV_UNREGISTER
) {
1458 po
->prot_hook
.dev
= NULL
;
1460 spin_unlock(&po
->bind_lock
);
1464 spin_lock(&po
->bind_lock
);
1465 if (dev
->ifindex
== po
->ifindex
&& po
->num
&&
1467 dev_add_pack(&po
->prot_hook
);
1471 spin_unlock(&po
->bind_lock
);
1475 read_unlock(&packet_sklist_lock
);
1480 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
1483 struct sock
*sk
= sock
->sk
;
1488 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1489 return put_user(amount
, (int __user
*)arg
);
1493 struct sk_buff
*skb
;
1496 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1497 skb
= skb_peek(&sk
->sk_receive_queue
);
1500 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1501 return put_user(amount
, (int __user
*)arg
);
1504 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1514 case SIOCGIFBRDADDR
:
1515 case SIOCSIFBRDADDR
:
1516 case SIOCGIFNETMASK
:
1517 case SIOCSIFNETMASK
:
1518 case SIOCGIFDSTADDR
:
1519 case SIOCSIFDSTADDR
:
1521 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1525 return -ENOIOCTLCMD
;
1530 #ifndef CONFIG_PACKET_MMAP
1531 #define packet_mmap sock_no_mmap
1532 #define packet_poll datagram_poll
1535 static unsigned int packet_poll(struct file
* file
, struct socket
*sock
,
1538 struct sock
*sk
= sock
->sk
;
1539 struct packet_sock
*po
= pkt_sk(sk
);
1540 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1542 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1544 unsigned last
= po
->head
? po
->head
-1 : po
->frame_max
;
1545 struct tpacket_hdr
*h
;
1547 h
= (struct tpacket_hdr
*)packet_lookup_frame(po
, last
);
1550 mask
|= POLLIN
| POLLRDNORM
;
1552 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1557 /* Dirty? Well, I still did not learn better way to account
1561 static void packet_mm_open(struct vm_area_struct
*vma
)
1563 struct file
*file
= vma
->vm_file
;
1564 struct socket
* sock
= file
->private_data
;
1565 struct sock
*sk
= sock
->sk
;
1568 atomic_inc(&pkt_sk(sk
)->mapped
);
1571 static void packet_mm_close(struct vm_area_struct
*vma
)
1573 struct file
*file
= vma
->vm_file
;
1574 struct socket
* sock
= file
->private_data
;
1575 struct sock
*sk
= sock
->sk
;
1578 atomic_dec(&pkt_sk(sk
)->mapped
);
1581 static struct vm_operations_struct packet_mmap_ops
= {
1582 .open
= packet_mm_open
,
1583 .close
=packet_mm_close
,
1586 static inline struct page
*pg_vec_endpage(char *one_pg_vec
, unsigned int order
)
1588 return virt_to_page(one_pg_vec
+ (PAGE_SIZE
<< order
) - 1);
1591 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
1595 for (i
= 0; i
< len
; i
++) {
1596 if (likely(pg_vec
[i
]))
1597 free_pages((unsigned long) pg_vec
[i
], order
);
1602 static inline char *alloc_one_pg_vec_page(unsigned long order
)
1604 return (char *) __get_free_pages(GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
,
1608 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
1610 unsigned int block_nr
= req
->tp_block_nr
;
1614 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
1615 if (unlikely(!pg_vec
))
1618 for (i
= 0; i
< block_nr
; i
++) {
1619 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
1620 if (unlikely(!pg_vec
[i
]))
1621 goto out_free_pgvec
;
1628 free_pg_vec(pg_vec
, order
, block_nr
);
1633 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
)
1635 char **pg_vec
= NULL
;
1636 struct packet_sock
*po
= pkt_sk(sk
);
1637 int was_running
, num
, order
= 0;
1640 if (req
->tp_block_nr
) {
1643 /* Sanity tests and some calculations */
1645 if (unlikely(po
->pg_vec
))
1648 if (unlikely((int)req
->tp_block_size
<= 0))
1650 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
1652 if (unlikely(req
->tp_frame_size
< TPACKET_HDRLEN
))
1654 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
1657 po
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
1658 if (unlikely(po
->frames_per_block
<= 0))
1660 if (unlikely((po
->frames_per_block
* req
->tp_block_nr
) !=
1665 order
= get_order(req
->tp_block_size
);
1666 pg_vec
= alloc_pg_vec(req
, order
);
1667 if (unlikely(!pg_vec
))
1671 for (i
= 0; i
< req
->tp_block_nr
; i
++) {
1672 char *ptr
= pg_vec
[i
];
1673 struct tpacket_hdr
*header
;
1676 for (k
= 0; k
< po
->frames_per_block
; k
++) {
1677 header
= (struct tpacket_hdr
*) ptr
;
1678 header
->tp_status
= TP_STATUS_KERNEL
;
1679 ptr
+= req
->tp_frame_size
;
1684 if (unlikely(req
->tp_frame_nr
))
1690 /* Detach socket from network */
1691 spin_lock(&po
->bind_lock
);
1692 was_running
= po
->running
;
1695 __dev_remove_pack(&po
->prot_hook
);
1700 spin_unlock(&po
->bind_lock
);
1705 if (closing
|| atomic_read(&po
->mapped
) == 0) {
1707 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1709 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1710 pg_vec
= XC(po
->pg_vec
, pg_vec
);
1711 po
->frame_max
= (req
->tp_frame_nr
- 1);
1713 po
->frame_size
= req
->tp_frame_size
;
1714 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1716 order
= XC(po
->pg_vec_order
, order
);
1717 req
->tp_block_nr
= XC(po
->pg_vec_len
, req
->tp_block_nr
);
1719 po
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
1720 po
->prot_hook
.func
= po
->pg_vec
? tpacket_rcv
: packet_rcv
;
1721 skb_queue_purge(&sk
->sk_receive_queue
);
1723 if (atomic_read(&po
->mapped
))
1724 printk(KERN_DEBUG
"packet_mmap: vma is busy: %d\n", atomic_read(&po
->mapped
));
1727 spin_lock(&po
->bind_lock
);
1728 if (was_running
&& !po
->running
) {
1732 dev_add_pack(&po
->prot_hook
);
1734 spin_unlock(&po
->bind_lock
);
1739 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
1744 static int packet_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1746 struct sock
*sk
= sock
->sk
;
1747 struct packet_sock
*po
= pkt_sk(sk
);
1749 unsigned long start
;
1756 size
= vma
->vm_end
- vma
->vm_start
;
1759 if (po
->pg_vec
== NULL
)
1761 if (size
!= po
->pg_vec_len
*po
->pg_vec_pages
*PAGE_SIZE
)
1764 start
= vma
->vm_start
;
1765 for (i
= 0; i
< po
->pg_vec_len
; i
++) {
1766 struct page
*page
= virt_to_page(po
->pg_vec
[i
]);
1769 for (pg_num
= 0; pg_num
< po
->pg_vec_pages
; pg_num
++, page
++) {
1770 err
= vm_insert_page(vma
, start
, page
);
1776 atomic_inc(&po
->mapped
);
1777 vma
->vm_ops
= &packet_mmap_ops
;
1787 #ifdef CONFIG_SOCK_PACKET
1788 static const struct proto_ops packet_ops_spkt
= {
1789 .family
= PF_PACKET
,
1790 .owner
= THIS_MODULE
,
1791 .release
= packet_release
,
1792 .bind
= packet_bind_spkt
,
1793 .connect
= sock_no_connect
,
1794 .socketpair
= sock_no_socketpair
,
1795 .accept
= sock_no_accept
,
1796 .getname
= packet_getname_spkt
,
1797 .poll
= datagram_poll
,
1798 .ioctl
= packet_ioctl
,
1799 .listen
= sock_no_listen
,
1800 .shutdown
= sock_no_shutdown
,
1801 .setsockopt
= sock_no_setsockopt
,
1802 .getsockopt
= sock_no_getsockopt
,
1803 .sendmsg
= packet_sendmsg_spkt
,
1804 .recvmsg
= packet_recvmsg
,
1805 .mmap
= sock_no_mmap
,
1806 .sendpage
= sock_no_sendpage
,
1810 static const struct proto_ops packet_ops
= {
1811 .family
= PF_PACKET
,
1812 .owner
= THIS_MODULE
,
1813 .release
= packet_release
,
1814 .bind
= packet_bind
,
1815 .connect
= sock_no_connect
,
1816 .socketpair
= sock_no_socketpair
,
1817 .accept
= sock_no_accept
,
1818 .getname
= packet_getname
,
1819 .poll
= packet_poll
,
1820 .ioctl
= packet_ioctl
,
1821 .listen
= sock_no_listen
,
1822 .shutdown
= sock_no_shutdown
,
1823 .setsockopt
= packet_setsockopt
,
1824 .getsockopt
= packet_getsockopt
,
1825 .sendmsg
= packet_sendmsg
,
1826 .recvmsg
= packet_recvmsg
,
1827 .mmap
= packet_mmap
,
1828 .sendpage
= sock_no_sendpage
,
1831 static struct net_proto_family packet_family_ops
= {
1832 .family
= PF_PACKET
,
1833 .create
= packet_create
,
1834 .owner
= THIS_MODULE
,
1837 static struct notifier_block packet_netdev_notifier
= {
1838 .notifier_call
=packet_notifier
,
1841 #ifdef CONFIG_PROC_FS
1842 static inline struct sock
*packet_seq_idx(loff_t off
)
1845 struct hlist_node
*node
;
1847 sk_for_each(s
, node
, &packet_sklist
) {
1854 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1856 read_lock(&packet_sklist_lock
);
1857 return *pos
? packet_seq_idx(*pos
- 1) : SEQ_START_TOKEN
;
1860 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1863 return (v
== SEQ_START_TOKEN
)
1864 ? sk_head(&packet_sklist
)
1865 : sk_next((struct sock
*)v
) ;
1868 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
1870 read_unlock(&packet_sklist_lock
);
1873 static int packet_seq_show(struct seq_file
*seq
, void *v
)
1875 if (v
== SEQ_START_TOKEN
)
1876 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1879 const struct packet_sock
*po
= pkt_sk(s
);
1882 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1884 atomic_read(&s
->sk_refcnt
),
1889 atomic_read(&s
->sk_rmem_alloc
),
1897 static struct seq_operations packet_seq_ops
= {
1898 .start
= packet_seq_start
,
1899 .next
= packet_seq_next
,
1900 .stop
= packet_seq_stop
,
1901 .show
= packet_seq_show
,
1904 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
1906 return seq_open(file
, &packet_seq_ops
);
1909 static struct file_operations packet_seq_fops
= {
1910 .owner
= THIS_MODULE
,
1911 .open
= packet_seq_open
,
1913 .llseek
= seq_lseek
,
1914 .release
= seq_release
,
1919 static void __exit
packet_exit(void)
1921 proc_net_remove("packet");
1922 unregister_netdevice_notifier(&packet_netdev_notifier
);
1923 sock_unregister(PF_PACKET
);
1924 proto_unregister(&packet_proto
);
1927 static int __init
packet_init(void)
1929 int rc
= proto_register(&packet_proto
, 0);
1934 sock_register(&packet_family_ops
);
1935 register_netdevice_notifier(&packet_netdev_notifier
);
1936 proc_net_fops_create("packet", 0, &packet_seq_fops
);
1941 module_init(packet_init
);
1942 module_exit(packet_exit
);
1943 MODULE_LICENSE("GPL");
1944 MODULE_ALIAS_NETPROTO(PF_PACKET
);