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>
65 #include <net/protocol.h>
66 #include <linux/skbuff.h>
68 #include <linux/errno.h>
69 #include <linux/timer.h>
70 #include <asm/system.h>
71 #include <asm/uaccess.h>
72 #include <asm/ioctls.h>
74 #include <asm/cacheflush.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
117 mac_header -> ll header
120 Outgoing, dev->hard_header!=NULL
121 mac_header -> ll header
124 Incoming, dev->hard_header==NULL
125 mac_header -> UNKNOWN position. It is very likely, that it points to ll
126 header. PPP makes it, that is wrong, because introduce
127 assymetry between rx and tx paths.
130 Outgoing, dev->hard_header==NULL
131 mac_header -> 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
142 mac_header -> ll header
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 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 struct tpacket_hdr
*packet_lookup_frame(struct packet_sock
*po
, unsigned int position
)
233 unsigned int pg_vec_pos
, frame_offset
;
235 pg_vec_pos
= position
/ po
->frames_per_block
;
236 frame_offset
= position
% po
->frames_per_block
;
238 return (struct tpacket_hdr
*)(po
->pg_vec
[pg_vec_pos
] + (frame_offset
* po
->frame_size
));
242 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
244 return (struct packet_sock
*)sk
;
247 static void packet_sock_destruct(struct sock
*sk
)
249 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
250 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
252 if (!sock_flag(sk
, SOCK_DEAD
)) {
253 printk("Attempt to release alive packet socket: %p\n", sk
);
257 atomic_dec(&packet_socks_nr
);
258 #ifdef PACKET_REFCNT_DEBUG
259 printk(KERN_DEBUG
"PACKET socket %p is free, %d are alive\n", sk
, atomic_read(&packet_socks_nr
));
264 static const struct proto_ops packet_ops
;
266 #ifdef CONFIG_SOCK_PACKET
267 static const struct proto_ops packet_ops_spkt
;
269 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
272 struct sockaddr_pkt
*spkt
;
275 * When we registered the protocol we saved the socket in the data
276 * field for just this event.
279 sk
= pt
->af_packet_priv
;
282 * Yank back the headers [hope the device set this
283 * right or kerboom...]
285 * Incoming packets have ll header pulled,
288 * For outgoing ones skb->data == skb_mac_header(skb)
289 * so that this procedure is noop.
292 if (skb
->pkt_type
== PACKET_LOOPBACK
)
295 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
298 /* drop any routing info */
299 dst_release(skb
->dst
);
302 /* drop conntrack reference */
305 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
307 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
310 * The SOCK_PACKET socket receives _all_ frames.
313 spkt
->spkt_family
= dev
->type
;
314 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
315 spkt
->spkt_protocol
= skb
->protocol
;
318 * Charge the memory to the socket. This is done specifically
319 * to prevent sockets using all the memory up.
322 if (sock_queue_rcv_skb(sk
,skb
) == 0)
333 * Output a raw packet to a device layer. This bypasses all the other
334 * protocol layers and you must therefore supply it with a complete frame
337 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
338 struct msghdr
*msg
, size_t len
)
340 struct sock
*sk
= sock
->sk
;
341 struct sockaddr_pkt
*saddr
=(struct sockaddr_pkt
*)msg
->msg_name
;
343 struct net_device
*dev
;
348 * Get and verify the address.
353 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
355 if (msg
->msg_namelen
==sizeof(struct sockaddr_pkt
))
356 proto
=saddr
->spkt_protocol
;
359 return(-ENOTCONN
); /* SOCK_PACKET must be sent giving an address */
362 * Find the device first to size check it
365 saddr
->spkt_device
[13] = 0;
366 dev
= dev_get_by_name(saddr
->spkt_device
);
372 if (!(dev
->flags
& IFF_UP
))
376 * You may not queue a frame bigger than the mtu. This is the lowest level
377 * raw protocol and you must do your own fragmentation at this level.
381 if (len
> dev
->mtu
+ dev
->hard_header_len
)
385 skb
= sock_wmalloc(sk
, len
+ LL_RESERVED_SPACE(dev
), 0, GFP_KERNEL
);
388 * If the write buffer is full, then tough. At this level the user gets to
389 * deal with the problem - do your own algorithmic backoffs. That's far
400 /* FIXME: Save some space for broken drivers that write a
401 * hard header at transmission time by themselves. PPP is the
402 * notable one here. This should really be fixed at the driver level.
404 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
405 skb_reset_network_header(skb
);
407 /* Try to align data part correctly */
408 if (dev
->hard_header
) {
409 skb
->data
-= dev
->hard_header_len
;
410 skb
->tail
-= dev
->hard_header_len
;
411 if (len
< dev
->hard_header_len
)
412 skb_reset_network_header(skb
);
415 /* Returns -EFAULT on error */
416 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
417 skb
->protocol
= proto
;
419 skb
->priority
= sk
->sk_priority
;
440 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
443 struct sk_filter
*filter
;
446 filter
= rcu_dereference(sk
->sk_filter
);
448 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
449 rcu_read_unlock_bh();
455 This function makes lazy skb cloning in hope that most of packets
456 are discarded by BPF.
458 Note tricky part: we DO mangle shared skb! skb->data, skb->len
459 and skb->cb are mangled. It works because (and until) packets
460 falling here are owned by current CPU. Output packets are cloned
461 by dev_queue_xmit_nit(), input packets are processed by net_bh
462 sequencially, so that if we return skb to original state on exit,
463 we will not harm anyone.
466 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
469 struct sockaddr_ll
*sll
;
470 struct packet_sock
*po
;
471 u8
* skb_head
= skb
->data
;
472 int skb_len
= skb
->len
;
473 unsigned int snaplen
, res
;
475 if (skb
->pkt_type
== PACKET_LOOPBACK
)
478 sk
= pt
->af_packet_priv
;
483 if (dev
->hard_header
) {
484 /* The device has an explicit notion of ll header,
485 exported to higher levels.
487 Otherwise, the device hides datails of it frame
488 structure, so that corresponding packet head
489 never delivered to user.
491 if (sk
->sk_type
!= SOCK_DGRAM
)
492 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
493 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
494 /* Special case: outgoing packets have ll header at head */
495 skb_pull(skb
, skb_network_offset(skb
));
501 res
= run_filter(skb
, sk
, snaplen
);
507 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
508 (unsigned)sk
->sk_rcvbuf
)
511 if (skb_shared(skb
)) {
512 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
516 if (skb_head
!= skb
->data
) {
517 skb
->data
= skb_head
;
524 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
527 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
528 sll
->sll_family
= AF_PACKET
;
529 sll
->sll_hatype
= dev
->type
;
530 sll
->sll_protocol
= skb
->protocol
;
531 sll
->sll_pkttype
= skb
->pkt_type
;
532 if (unlikely(po
->origdev
) && skb
->pkt_type
== PACKET_HOST
)
533 sll
->sll_ifindex
= orig_dev
->ifindex
;
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
;
591 if (skb
->pkt_type
== PACKET_LOOPBACK
)
594 sk
= pt
->af_packet_priv
;
597 if (dev
->hard_header
) {
598 if (sk
->sk_type
!= SOCK_DGRAM
)
599 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
600 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
601 /* Special case: outgoing packets have ll header at head */
602 skb_pull(skb
, skb_network_offset(skb
));
606 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
607 status
|= TP_STATUS_CSUMNOTREADY
;
611 res
= run_filter(skb
, sk
, snaplen
);
617 if (sk
->sk_type
== SOCK_DGRAM
) {
618 macoff
= netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
) + 16;
620 unsigned maclen
= skb_network_offset(skb
);
621 netoff
= TPACKET_ALIGN(TPACKET_HDRLEN
+ (maclen
< 16 ? 16 : maclen
));
622 macoff
= netoff
- maclen
;
625 if (macoff
+ snaplen
> po
->frame_size
) {
626 if (po
->copy_thresh
&&
627 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
628 (unsigned)sk
->sk_rcvbuf
) {
629 if (skb_shared(skb
)) {
630 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
632 copy_skb
= skb_get(skb
);
633 skb_head
= skb
->data
;
636 skb_set_owner_r(copy_skb
, sk
);
638 snaplen
= po
->frame_size
- macoff
;
639 if ((int)snaplen
< 0)
643 spin_lock(&sk
->sk_receive_queue
.lock
);
644 h
= packet_lookup_frame(po
, po
->head
);
648 po
->head
= po
->head
!= po
->frame_max
? po
->head
+1 : 0;
649 po
->stats
.tp_packets
++;
651 status
|= TP_STATUS_COPY
;
652 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
654 if (!po
->stats
.tp_drops
)
655 status
&= ~TP_STATUS_LOSING
;
656 spin_unlock(&sk
->sk_receive_queue
.lock
);
658 skb_copy_bits(skb
, 0, (u8
*)h
+ macoff
, snaplen
);
660 h
->tp_len
= skb
->len
;
661 h
->tp_snaplen
= snaplen
;
664 if (skb
->tstamp
.tv64
== 0) {
665 __net_timestamp(skb
);
666 sock_enable_timestamp(sk
);
668 tv
= ktime_to_timeval(skb
->tstamp
);
669 h
->tp_sec
= tv
.tv_sec
;
670 h
->tp_usec
= tv
.tv_usec
;
672 sll
= (struct sockaddr_ll
*)((u8
*)h
+ TPACKET_ALIGN(sizeof(*h
)));
674 if (dev
->hard_header_parse
)
675 sll
->sll_halen
= dev
->hard_header_parse(skb
, sll
->sll_addr
);
676 sll
->sll_family
= AF_PACKET
;
677 sll
->sll_hatype
= dev
->type
;
678 sll
->sll_protocol
= skb
->protocol
;
679 sll
->sll_pkttype
= skb
->pkt_type
;
680 if (unlikely(po
->origdev
) && skb
->pkt_type
== PACKET_HOST
)
681 sll
->sll_ifindex
= orig_dev
->ifindex
;
683 sll
->sll_ifindex
= dev
->ifindex
;
685 h
->tp_status
= status
;
689 struct page
*p_start
, *p_end
;
690 u8
*h_end
= (u8
*)h
+ macoff
+ snaplen
- 1;
692 p_start
= virt_to_page(h
);
693 p_end
= virt_to_page(h_end
);
694 while (p_start
<= p_end
) {
695 flush_dcache_page(p_start
);
700 sk
->sk_data_ready(sk
, 0);
703 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
704 skb
->data
= skb_head
;
712 po
->stats
.tp_drops
++;
713 spin_unlock(&sk
->sk_receive_queue
.lock
);
715 sk
->sk_data_ready(sk
, 0);
724 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
725 struct msghdr
*msg
, size_t len
)
727 struct sock
*sk
= sock
->sk
;
728 struct sockaddr_ll
*saddr
=(struct sockaddr_ll
*)msg
->msg_name
;
730 struct net_device
*dev
;
733 int ifindex
, err
, reserve
= 0;
736 * Get and verify the address.
740 struct packet_sock
*po
= pkt_sk(sk
);
742 ifindex
= po
->ifindex
;
747 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
749 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
751 ifindex
= saddr
->sll_ifindex
;
752 proto
= saddr
->sll_protocol
;
753 addr
= saddr
->sll_addr
;
757 dev
= dev_get_by_index(ifindex
);
761 if (sock
->type
== SOCK_RAW
)
762 reserve
= dev
->hard_header_len
;
765 if (!(dev
->flags
& IFF_UP
))
769 if (len
> dev
->mtu
+reserve
)
772 skb
= sock_alloc_send_skb(sk
, len
+ LL_RESERVED_SPACE(dev
),
773 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
777 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
778 skb_reset_network_header(skb
);
780 if (dev
->hard_header
) {
783 res
= dev
->hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
);
784 if (sock
->type
!= SOCK_DGRAM
) {
785 skb_reset_tail_pointer(skb
);
791 /* Returns -EFAULT on error */
792 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
796 skb
->protocol
= proto
;
798 skb
->priority
= sk
->sk_priority
;
804 err
= dev_queue_xmit(skb
);
805 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
822 * Close a PACKET socket. This is fairly simple. We immediately go
823 * to 'closed' state and remove our protocol entry in the device list.
826 static int packet_release(struct socket
*sock
)
828 struct sock
*sk
= sock
->sk
;
829 struct packet_sock
*po
;
836 write_lock_bh(&packet_sklist_lock
);
837 sk_del_node_init(sk
);
838 write_unlock_bh(&packet_sklist_lock
);
841 * Unhook packet receive handler.
846 * Remove the protocol hook
848 dev_remove_pack(&po
->prot_hook
);
854 #ifdef CONFIG_PACKET_MULTICAST
855 packet_flush_mclist(sk
);
858 #ifdef CONFIG_PACKET_MMAP
860 struct tpacket_req req
;
861 memset(&req
, 0, sizeof(req
));
862 packet_set_ring(sk
, &req
, 1);
867 * Now the socket is dead. No more input will appear.
875 skb_queue_purge(&sk
->sk_receive_queue
);
882 * Attach a packet hook.
885 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
887 struct packet_sock
*po
= pkt_sk(sk
);
889 * Detach an existing hook if present.
894 spin_lock(&po
->bind_lock
);
899 spin_unlock(&po
->bind_lock
);
900 dev_remove_pack(&po
->prot_hook
);
901 spin_lock(&po
->bind_lock
);
905 po
->prot_hook
.type
= protocol
;
906 po
->prot_hook
.dev
= dev
;
908 po
->ifindex
= dev
? dev
->ifindex
: 0;
914 if (dev
->flags
&IFF_UP
) {
915 dev_add_pack(&po
->prot_hook
);
919 sk
->sk_err
= ENETDOWN
;
920 if (!sock_flag(sk
, SOCK_DEAD
))
921 sk
->sk_error_report(sk
);
924 dev_add_pack(&po
->prot_hook
);
930 spin_unlock(&po
->bind_lock
);
936 * Bind a packet socket to a device
939 #ifdef CONFIG_SOCK_PACKET
941 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
943 struct sock
*sk
=sock
->sk
;
945 struct net_device
*dev
;
952 if (addr_len
!= sizeof(struct sockaddr
))
954 strlcpy(name
,uaddr
->sa_data
,sizeof(name
));
956 dev
= dev_get_by_name(name
);
958 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
965 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
967 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
968 struct sock
*sk
=sock
->sk
;
969 struct net_device
*dev
= NULL
;
977 if (addr_len
< sizeof(struct sockaddr_ll
))
979 if (sll
->sll_family
!= AF_PACKET
)
982 if (sll
->sll_ifindex
) {
984 dev
= dev_get_by_index(sll
->sll_ifindex
);
988 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
996 static struct proto packet_proto
= {
998 .owner
= THIS_MODULE
,
999 .obj_size
= sizeof(struct packet_sock
),
1003 * Create a packet of type SOCK_PACKET.
1006 static int packet_create(struct socket
*sock
, int protocol
)
1009 struct packet_sock
*po
;
1010 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1013 if (!capable(CAP_NET_RAW
))
1015 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
1016 #ifdef CONFIG_SOCK_PACKET
1017 && sock
->type
!= SOCK_PACKET
1020 return -ESOCKTNOSUPPORT
;
1022 sock
->state
= SS_UNCONNECTED
;
1025 sk
= sk_alloc(PF_PACKET
, GFP_KERNEL
, &packet_proto
, 1);
1029 sock
->ops
= &packet_ops
;
1030 #ifdef CONFIG_SOCK_PACKET
1031 if (sock
->type
== SOCK_PACKET
)
1032 sock
->ops
= &packet_ops_spkt
;
1034 sock_init_data(sock
, sk
);
1037 sk
->sk_family
= PF_PACKET
;
1040 sk
->sk_destruct
= packet_sock_destruct
;
1041 atomic_inc(&packet_socks_nr
);
1044 * Attach a protocol block
1047 spin_lock_init(&po
->bind_lock
);
1048 po
->prot_hook
.func
= packet_rcv
;
1049 #ifdef CONFIG_SOCK_PACKET
1050 if (sock
->type
== SOCK_PACKET
)
1051 po
->prot_hook
.func
= packet_rcv_spkt
;
1053 po
->prot_hook
.af_packet_priv
= sk
;
1056 po
->prot_hook
.type
= proto
;
1057 dev_add_pack(&po
->prot_hook
);
1062 write_lock_bh(&packet_sklist_lock
);
1063 sk_add_node(sk
, &packet_sklist
);
1064 write_unlock_bh(&packet_sklist_lock
);
1071 * Pull a packet from our receive queue and hand it to the user.
1072 * If necessary we block.
1075 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1076 struct msghdr
*msg
, size_t len
, int flags
)
1078 struct sock
*sk
= sock
->sk
;
1079 struct sk_buff
*skb
;
1081 struct sockaddr_ll
*sll
;
1084 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1088 /* What error should we return now? EUNATTACH? */
1089 if (pkt_sk(sk
)->ifindex
< 0)
1094 * Call the generic datagram receiver. This handles all sorts
1095 * of horrible races and re-entrancy so we can forget about it
1096 * in the protocol layers.
1098 * Now it will return ENETDOWN, if device have just gone down,
1099 * but then it will block.
1102 skb
=skb_recv_datagram(sk
,flags
,flags
&MSG_DONTWAIT
,&err
);
1105 * An error occurred so return it. Because skb_recv_datagram()
1106 * handles the blocking we don't see and worry about blocking
1114 * If the address length field is there to be filled in, we fill
1118 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1119 if (sock
->type
== SOCK_PACKET
)
1120 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1122 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1125 * You lose any data beyond the buffer you gave. If it worries a
1126 * user program they can ask the device for its MTU anyway.
1133 msg
->msg_flags
|=MSG_TRUNC
;
1136 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1140 sock_recv_timestamp(msg
, sk
, skb
);
1143 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1146 if (pkt_sk(sk
)->auxdata
) {
1147 struct tpacket_auxdata aux
;
1149 aux
.tp_status
= TP_STATUS_USER
;
1150 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1151 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1152 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1153 aux
.tp_snaplen
= skb
->len
;
1155 aux
.tp_net
= skb_network_offset(skb
);
1157 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1161 * Free or return the buffer as appropriate. Again this
1162 * hides all the races and re-entrancy issues from us.
1164 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1167 skb_free_datagram(sk
, skb
);
1172 #ifdef CONFIG_SOCK_PACKET
1173 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1174 int *uaddr_len
, int peer
)
1176 struct net_device
*dev
;
1177 struct sock
*sk
= sock
->sk
;
1182 uaddr
->sa_family
= AF_PACKET
;
1183 dev
= dev_get_by_index(pkt_sk(sk
)->ifindex
);
1185 strlcpy(uaddr
->sa_data
, dev
->name
, 15);
1188 memset(uaddr
->sa_data
, 0, 14);
1189 *uaddr_len
= sizeof(*uaddr
);
1195 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1196 int *uaddr_len
, int peer
)
1198 struct net_device
*dev
;
1199 struct sock
*sk
= sock
->sk
;
1200 struct packet_sock
*po
= pkt_sk(sk
);
1201 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1206 sll
->sll_family
= AF_PACKET
;
1207 sll
->sll_ifindex
= po
->ifindex
;
1208 sll
->sll_protocol
= po
->num
;
1209 dev
= dev_get_by_index(po
->ifindex
);
1211 sll
->sll_hatype
= dev
->type
;
1212 sll
->sll_halen
= dev
->addr_len
;
1213 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1216 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1219 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1224 #ifdef CONFIG_PACKET_MULTICAST
1225 static void packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1228 case PACKET_MR_MULTICAST
:
1230 dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1232 dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1234 case PACKET_MR_PROMISC
:
1235 dev_set_promiscuity(dev
, what
);
1237 case PACKET_MR_ALLMULTI
:
1238 dev_set_allmulti(dev
, what
);
1244 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1246 for ( ; i
; i
=i
->next
) {
1247 if (i
->ifindex
== dev
->ifindex
)
1248 packet_dev_mc(dev
, i
, what
);
1252 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1254 struct packet_sock
*po
= pkt_sk(sk
);
1255 struct packet_mclist
*ml
, *i
;
1256 struct net_device
*dev
;
1262 dev
= __dev_get_by_index(mreq
->mr_ifindex
);
1267 if (mreq
->mr_alen
> dev
->addr_len
)
1271 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1276 for (ml
= po
->mclist
; ml
; ml
= ml
->next
) {
1277 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1278 ml
->type
== mreq
->mr_type
&&
1279 ml
->alen
== mreq
->mr_alen
&&
1280 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1282 /* Free the new element ... */
1288 i
->type
= mreq
->mr_type
;
1289 i
->ifindex
= mreq
->mr_ifindex
;
1290 i
->alen
= mreq
->mr_alen
;
1291 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1293 i
->next
= po
->mclist
;
1295 packet_dev_mc(dev
, i
, +1);
1302 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1304 struct packet_mclist
*ml
, **mlp
;
1308 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1309 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1310 ml
->type
== mreq
->mr_type
&&
1311 ml
->alen
== mreq
->mr_alen
&&
1312 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1313 if (--ml
->count
== 0) {
1314 struct net_device
*dev
;
1316 dev
= dev_get_by_index(ml
->ifindex
);
1318 packet_dev_mc(dev
, ml
, -1);
1328 return -EADDRNOTAVAIL
;
1331 static void packet_flush_mclist(struct sock
*sk
)
1333 struct packet_sock
*po
= pkt_sk(sk
);
1334 struct packet_mclist
*ml
;
1340 while ((ml
= po
->mclist
) != NULL
) {
1341 struct net_device
*dev
;
1343 po
->mclist
= ml
->next
;
1344 if ((dev
= dev_get_by_index(ml
->ifindex
)) != NULL
) {
1345 packet_dev_mc(dev
, ml
, -1);
1355 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1357 struct sock
*sk
= sock
->sk
;
1358 struct packet_sock
*po
= pkt_sk(sk
);
1361 if (level
!= SOL_PACKET
)
1362 return -ENOPROTOOPT
;
1365 #ifdef CONFIG_PACKET_MULTICAST
1366 case PACKET_ADD_MEMBERSHIP
:
1367 case PACKET_DROP_MEMBERSHIP
:
1369 struct packet_mreq_max mreq
;
1371 memset(&mreq
, 0, sizeof(mreq
));
1372 if (len
< sizeof(struct packet_mreq
))
1374 if (len
> sizeof(mreq
))
1376 if (copy_from_user(&mreq
,optval
,len
))
1378 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1380 if (optname
== PACKET_ADD_MEMBERSHIP
)
1381 ret
= packet_mc_add(sk
, &mreq
);
1383 ret
= packet_mc_drop(sk
, &mreq
);
1387 #ifdef CONFIG_PACKET_MMAP
1388 case PACKET_RX_RING
:
1390 struct tpacket_req req
;
1392 if (optlen
<sizeof(req
))
1394 if (copy_from_user(&req
,optval
,sizeof(req
)))
1396 return packet_set_ring(sk
, &req
, 0);
1398 case PACKET_COPY_THRESH
:
1402 if (optlen
!=sizeof(val
))
1404 if (copy_from_user(&val
,optval
,sizeof(val
)))
1407 pkt_sk(sk
)->copy_thresh
= val
;
1411 case PACKET_AUXDATA
:
1415 if (optlen
< sizeof(val
))
1417 if (copy_from_user(&val
, optval
, sizeof(val
)))
1420 po
->auxdata
= !!val
;
1423 case PACKET_ORIGDEV
:
1427 if (optlen
< sizeof(val
))
1429 if (copy_from_user(&val
, optval
, sizeof(val
)))
1432 po
->origdev
= !!val
;
1436 return -ENOPROTOOPT
;
1440 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1441 char __user
*optval
, int __user
*optlen
)
1445 struct sock
*sk
= sock
->sk
;
1446 struct packet_sock
*po
= pkt_sk(sk
);
1448 struct tpacket_stats st
;
1450 if (level
!= SOL_PACKET
)
1451 return -ENOPROTOOPT
;
1453 if (get_user(len
, optlen
))
1460 case PACKET_STATISTICS
:
1461 if (len
> sizeof(struct tpacket_stats
))
1462 len
= sizeof(struct tpacket_stats
);
1463 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1465 memset(&po
->stats
, 0, sizeof(st
));
1466 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1467 st
.tp_packets
+= st
.tp_drops
;
1471 case PACKET_AUXDATA
:
1472 if (len
> sizeof(int))
1478 case PACKET_ORIGDEV
:
1479 if (len
> sizeof(int))
1486 return -ENOPROTOOPT
;
1489 if (put_user(len
, optlen
))
1491 if (copy_to_user(optval
, data
, len
))
1497 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1500 struct hlist_node
*node
;
1501 struct net_device
*dev
= data
;
1503 read_lock(&packet_sklist_lock
);
1504 sk_for_each(sk
, node
, &packet_sklist
) {
1505 struct packet_sock
*po
= pkt_sk(sk
);
1508 case NETDEV_UNREGISTER
:
1509 #ifdef CONFIG_PACKET_MULTICAST
1511 packet_dev_mclist(dev
, po
->mclist
, -1);
1515 if (dev
->ifindex
== po
->ifindex
) {
1516 spin_lock(&po
->bind_lock
);
1518 __dev_remove_pack(&po
->prot_hook
);
1521 sk
->sk_err
= ENETDOWN
;
1522 if (!sock_flag(sk
, SOCK_DEAD
))
1523 sk
->sk_error_report(sk
);
1525 if (msg
== NETDEV_UNREGISTER
) {
1527 po
->prot_hook
.dev
= NULL
;
1529 spin_unlock(&po
->bind_lock
);
1533 spin_lock(&po
->bind_lock
);
1534 if (dev
->ifindex
== po
->ifindex
&& po
->num
&&
1536 dev_add_pack(&po
->prot_hook
);
1540 spin_unlock(&po
->bind_lock
);
1544 read_unlock(&packet_sklist_lock
);
1549 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
1552 struct sock
*sk
= sock
->sk
;
1557 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1558 return put_user(amount
, (int __user
*)arg
);
1562 struct sk_buff
*skb
;
1565 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1566 skb
= skb_peek(&sk
->sk_receive_queue
);
1569 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1570 return put_user(amount
, (int __user
*)arg
);
1573 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1575 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
1585 case SIOCGIFBRDADDR
:
1586 case SIOCSIFBRDADDR
:
1587 case SIOCGIFNETMASK
:
1588 case SIOCSIFNETMASK
:
1589 case SIOCGIFDSTADDR
:
1590 case SIOCSIFDSTADDR
:
1592 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1596 return -ENOIOCTLCMD
;
1601 #ifndef CONFIG_PACKET_MMAP
1602 #define packet_mmap sock_no_mmap
1603 #define packet_poll datagram_poll
1606 static unsigned int packet_poll(struct file
* file
, struct socket
*sock
,
1609 struct sock
*sk
= sock
->sk
;
1610 struct packet_sock
*po
= pkt_sk(sk
);
1611 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1613 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1615 unsigned last
= po
->head
? po
->head
-1 : po
->frame_max
;
1616 struct tpacket_hdr
*h
;
1618 h
= packet_lookup_frame(po
, last
);
1621 mask
|= POLLIN
| POLLRDNORM
;
1623 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1628 /* Dirty? Well, I still did not learn better way to account
1632 static void packet_mm_open(struct vm_area_struct
*vma
)
1634 struct file
*file
= vma
->vm_file
;
1635 struct socket
* sock
= file
->private_data
;
1636 struct sock
*sk
= sock
->sk
;
1639 atomic_inc(&pkt_sk(sk
)->mapped
);
1642 static void packet_mm_close(struct vm_area_struct
*vma
)
1644 struct file
*file
= vma
->vm_file
;
1645 struct socket
* sock
= file
->private_data
;
1646 struct sock
*sk
= sock
->sk
;
1649 atomic_dec(&pkt_sk(sk
)->mapped
);
1652 static struct vm_operations_struct packet_mmap_ops
= {
1653 .open
= packet_mm_open
,
1654 .close
=packet_mm_close
,
1657 static inline struct page
*pg_vec_endpage(char *one_pg_vec
, unsigned int order
)
1659 return virt_to_page(one_pg_vec
+ (PAGE_SIZE
<< order
) - 1);
1662 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
1666 for (i
= 0; i
< len
; i
++) {
1667 if (likely(pg_vec
[i
]))
1668 free_pages((unsigned long) pg_vec
[i
], order
);
1673 static inline char *alloc_one_pg_vec_page(unsigned long order
)
1675 return (char *) __get_free_pages(GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
,
1679 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
1681 unsigned int block_nr
= req
->tp_block_nr
;
1685 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
1686 if (unlikely(!pg_vec
))
1689 for (i
= 0; i
< block_nr
; i
++) {
1690 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
1691 if (unlikely(!pg_vec
[i
]))
1692 goto out_free_pgvec
;
1699 free_pg_vec(pg_vec
, order
, block_nr
);
1704 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
)
1706 char **pg_vec
= NULL
;
1707 struct packet_sock
*po
= pkt_sk(sk
);
1708 int was_running
, order
= 0;
1712 if (req
->tp_block_nr
) {
1715 /* Sanity tests and some calculations */
1717 if (unlikely(po
->pg_vec
))
1720 if (unlikely((int)req
->tp_block_size
<= 0))
1722 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
1724 if (unlikely(req
->tp_frame_size
< TPACKET_HDRLEN
))
1726 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
1729 po
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
1730 if (unlikely(po
->frames_per_block
<= 0))
1732 if (unlikely((po
->frames_per_block
* req
->tp_block_nr
) !=
1737 order
= get_order(req
->tp_block_size
);
1738 pg_vec
= alloc_pg_vec(req
, order
);
1739 if (unlikely(!pg_vec
))
1743 for (i
= 0; i
< req
->tp_block_nr
; i
++) {
1744 char *ptr
= pg_vec
[i
];
1745 struct tpacket_hdr
*header
;
1748 for (k
= 0; k
< po
->frames_per_block
; k
++) {
1749 header
= (struct tpacket_hdr
*) ptr
;
1750 header
->tp_status
= TP_STATUS_KERNEL
;
1751 ptr
+= req
->tp_frame_size
;
1756 if (unlikely(req
->tp_frame_nr
))
1762 /* Detach socket from network */
1763 spin_lock(&po
->bind_lock
);
1764 was_running
= po
->running
;
1767 __dev_remove_pack(&po
->prot_hook
);
1772 spin_unlock(&po
->bind_lock
);
1777 if (closing
|| atomic_read(&po
->mapped
) == 0) {
1779 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1781 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1782 pg_vec
= XC(po
->pg_vec
, pg_vec
);
1783 po
->frame_max
= (req
->tp_frame_nr
- 1);
1785 po
->frame_size
= req
->tp_frame_size
;
1786 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1788 order
= XC(po
->pg_vec_order
, order
);
1789 req
->tp_block_nr
= XC(po
->pg_vec_len
, req
->tp_block_nr
);
1791 po
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
1792 po
->prot_hook
.func
= po
->pg_vec
? tpacket_rcv
: packet_rcv
;
1793 skb_queue_purge(&sk
->sk_receive_queue
);
1795 if (atomic_read(&po
->mapped
))
1796 printk(KERN_DEBUG
"packet_mmap: vma is busy: %d\n", atomic_read(&po
->mapped
));
1799 spin_lock(&po
->bind_lock
);
1800 if (was_running
&& !po
->running
) {
1804 dev_add_pack(&po
->prot_hook
);
1806 spin_unlock(&po
->bind_lock
);
1811 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
1816 static int packet_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1818 struct sock
*sk
= sock
->sk
;
1819 struct packet_sock
*po
= pkt_sk(sk
);
1821 unsigned long start
;
1828 size
= vma
->vm_end
- vma
->vm_start
;
1831 if (po
->pg_vec
== NULL
)
1833 if (size
!= po
->pg_vec_len
*po
->pg_vec_pages
*PAGE_SIZE
)
1836 start
= vma
->vm_start
;
1837 for (i
= 0; i
< po
->pg_vec_len
; i
++) {
1838 struct page
*page
= virt_to_page(po
->pg_vec
[i
]);
1841 for (pg_num
= 0; pg_num
< po
->pg_vec_pages
; pg_num
++, page
++) {
1842 err
= vm_insert_page(vma
, start
, page
);
1848 atomic_inc(&po
->mapped
);
1849 vma
->vm_ops
= &packet_mmap_ops
;
1859 #ifdef CONFIG_SOCK_PACKET
1860 static const struct proto_ops packet_ops_spkt
= {
1861 .family
= PF_PACKET
,
1862 .owner
= THIS_MODULE
,
1863 .release
= packet_release
,
1864 .bind
= packet_bind_spkt
,
1865 .connect
= sock_no_connect
,
1866 .socketpair
= sock_no_socketpair
,
1867 .accept
= sock_no_accept
,
1868 .getname
= packet_getname_spkt
,
1869 .poll
= datagram_poll
,
1870 .ioctl
= packet_ioctl
,
1871 .listen
= sock_no_listen
,
1872 .shutdown
= sock_no_shutdown
,
1873 .setsockopt
= sock_no_setsockopt
,
1874 .getsockopt
= sock_no_getsockopt
,
1875 .sendmsg
= packet_sendmsg_spkt
,
1876 .recvmsg
= packet_recvmsg
,
1877 .mmap
= sock_no_mmap
,
1878 .sendpage
= sock_no_sendpage
,
1882 static const struct proto_ops packet_ops
= {
1883 .family
= PF_PACKET
,
1884 .owner
= THIS_MODULE
,
1885 .release
= packet_release
,
1886 .bind
= packet_bind
,
1887 .connect
= sock_no_connect
,
1888 .socketpair
= sock_no_socketpair
,
1889 .accept
= sock_no_accept
,
1890 .getname
= packet_getname
,
1891 .poll
= packet_poll
,
1892 .ioctl
= packet_ioctl
,
1893 .listen
= sock_no_listen
,
1894 .shutdown
= sock_no_shutdown
,
1895 .setsockopt
= packet_setsockopt
,
1896 .getsockopt
= packet_getsockopt
,
1897 .sendmsg
= packet_sendmsg
,
1898 .recvmsg
= packet_recvmsg
,
1899 .mmap
= packet_mmap
,
1900 .sendpage
= sock_no_sendpage
,
1903 static struct net_proto_family packet_family_ops
= {
1904 .family
= PF_PACKET
,
1905 .create
= packet_create
,
1906 .owner
= THIS_MODULE
,
1909 static struct notifier_block packet_netdev_notifier
= {
1910 .notifier_call
=packet_notifier
,
1913 #ifdef CONFIG_PROC_FS
1914 static inline struct sock
*packet_seq_idx(loff_t off
)
1917 struct hlist_node
*node
;
1919 sk_for_each(s
, node
, &packet_sklist
) {
1926 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1928 read_lock(&packet_sklist_lock
);
1929 return *pos
? packet_seq_idx(*pos
- 1) : SEQ_START_TOKEN
;
1932 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1935 return (v
== SEQ_START_TOKEN
)
1936 ? sk_head(&packet_sklist
)
1937 : sk_next((struct sock
*)v
) ;
1940 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
1942 read_unlock(&packet_sklist_lock
);
1945 static int packet_seq_show(struct seq_file
*seq
, void *v
)
1947 if (v
== SEQ_START_TOKEN
)
1948 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1951 const struct packet_sock
*po
= pkt_sk(s
);
1954 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1956 atomic_read(&s
->sk_refcnt
),
1961 atomic_read(&s
->sk_rmem_alloc
),
1969 static struct seq_operations packet_seq_ops
= {
1970 .start
= packet_seq_start
,
1971 .next
= packet_seq_next
,
1972 .stop
= packet_seq_stop
,
1973 .show
= packet_seq_show
,
1976 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
1978 return seq_open(file
, &packet_seq_ops
);
1981 static const struct file_operations packet_seq_fops
= {
1982 .owner
= THIS_MODULE
,
1983 .open
= packet_seq_open
,
1985 .llseek
= seq_lseek
,
1986 .release
= seq_release
,
1991 static void __exit
packet_exit(void)
1993 proc_net_remove("packet");
1994 unregister_netdevice_notifier(&packet_netdev_notifier
);
1995 sock_unregister(PF_PACKET
);
1996 proto_unregister(&packet_proto
);
1999 static int __init
packet_init(void)
2001 int rc
= proto_register(&packet_proto
, 0);
2006 sock_register(&packet_family_ops
);
2007 register_netdevice_notifier(&packet_netdev_notifier
);
2008 proc_net_fops_create("packet", 0, &packet_seq_fops
);
2013 module_init(packet_init
);
2014 module_exit(packet_exit
);
2015 MODULE_LICENSE("GPL");
2016 MODULE_ALIAS_NETPROTO(PF_PACKET
);