2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * PACKET - implements raw packet sockets.
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
13 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
35 * Ulises Alonso : Frame number limit removal and
36 * packet_set_ring memory leak.
37 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
40 * byte arrays at the end of sockaddr_ll
43 * This program is free software; you can redistribute it and/or
44 * modify it under the terms of the GNU General Public License
45 * as published by the Free Software Foundation; either version
46 * 2 of the License, or (at your option) any later version.
50 #include <linux/types.h>
52 #include <linux/capability.h>
53 #include <linux/fcntl.h>
54 #include <linux/socket.h>
56 #include <linux/inet.h>
57 #include <linux/netdevice.h>
58 #include <linux/if_packet.h>
59 #include <linux/wireless.h>
60 #include <linux/kernel.h>
61 #include <linux/kmod.h>
62 #include <net/net_namespace.h>
64 #include <net/protocol.h>
65 #include <linux/skbuff.h>
67 #include <linux/errno.h>
68 #include <linux/timer.h>
69 #include <asm/system.h>
70 #include <asm/uaccess.h>
71 #include <asm/ioctls.h>
73 #include <asm/cacheflush.h>
75 #include <linux/proc_fs.h>
76 #include <linux/seq_file.h>
77 #include <linux/poll.h>
78 #include <linux/module.h>
79 #include <linux/init.h>
80 #include <linux/mutex.h>
83 #include <net/inet_common.h>
88 - if device has no dev->hard_header routine, it adds and removes ll header
89 inside itself. In this case ll header is invisible outside of device,
90 but higher levels still should reserve dev->hard_header_len.
91 Some devices are enough clever to reallocate skb, when header
92 will not fit to reserved space (tunnel), another ones are silly
94 - packet socket receives packets with pulled ll header,
95 so that SOCK_RAW should push it back.
100 Incoming, dev->hard_header!=NULL
101 mac_header -> ll header
104 Outgoing, dev->hard_header!=NULL
105 mac_header -> ll header
108 Incoming, dev->hard_header==NULL
109 mac_header -> UNKNOWN position. It is very likely, that it points to ll
110 header. PPP makes it, that is wrong, because introduce
111 assymetry between rx and tx paths.
114 Outgoing, dev->hard_header==NULL
115 mac_header -> data. ll header is still not built!
119 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
125 dev->hard_header != NULL
126 mac_header -> ll header
129 dev->hard_header == NULL (ll header is added by device, we cannot control it)
133 We should set nh.raw on output to correct posistion,
134 packet classifier depends on it.
137 /* Private packet socket structures. */
141 struct packet_mclist
*next
;
146 unsigned char addr
[MAX_ADDR_LEN
];
148 /* identical to struct packet_mreq except it has
149 * a longer address field.
151 struct packet_mreq_max
154 unsigned short mr_type
;
155 unsigned short mr_alen
;
156 unsigned char mr_address
[MAX_ADDR_LEN
];
159 #ifdef CONFIG_PACKET_MMAP
160 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
);
163 static void packet_flush_mclist(struct sock
*sk
);
166 /* struct sock has to be the first member of packet_sock */
168 struct tpacket_stats stats
;
169 #ifdef CONFIG_PACKET_MMAP
172 unsigned int frames_per_block
;
173 unsigned int frame_size
;
174 unsigned int frame_max
;
177 struct packet_type prot_hook
;
178 spinlock_t bind_lock
;
179 struct mutex pg_vec_lock
;
180 unsigned int running
:1, /* prot_hook is attached*/
183 int ifindex
; /* bound device */
185 struct packet_mclist
*mclist
;
186 #ifdef CONFIG_PACKET_MMAP
188 unsigned int pg_vec_order
;
189 unsigned int pg_vec_pages
;
190 unsigned int pg_vec_len
;
191 enum tpacket_versions tp_version
;
192 unsigned int tp_hdrlen
;
193 unsigned int tp_reserve
;
197 struct packet_skb_cb
{
198 unsigned int origlen
;
200 struct sockaddr_pkt pkt
;
201 struct sockaddr_ll ll
;
205 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
207 #ifdef CONFIG_PACKET_MMAP
209 static void *packet_lookup_frame(struct packet_sock
*po
, unsigned int position
,
212 unsigned int pg_vec_pos
, frame_offset
;
214 struct tpacket_hdr
*h1
;
215 struct tpacket2_hdr
*h2
;
219 pg_vec_pos
= position
/ po
->frames_per_block
;
220 frame_offset
= position
% po
->frames_per_block
;
222 h
.raw
= po
->pg_vec
[pg_vec_pos
] + (frame_offset
* po
->frame_size
);
223 switch (po
->tp_version
) {
225 if (status
!= (h
.h1
->tp_status
? TP_STATUS_USER
:
230 if (status
!= (h
.h2
->tp_status
? TP_STATUS_USER
:
238 static void __packet_set_status(struct packet_sock
*po
, void *frame
, int status
)
241 struct tpacket_hdr
*h1
;
242 struct tpacket2_hdr
*h2
;
247 switch (po
->tp_version
) {
249 h
.h1
->tp_status
= status
;
252 h
.h2
->tp_status
= status
;
258 static inline struct packet_sock
*pkt_sk(struct sock
*sk
)
260 return (struct packet_sock
*)sk
;
263 static void packet_sock_destruct(struct sock
*sk
)
265 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
266 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
268 if (!sock_flag(sk
, SOCK_DEAD
)) {
269 printk("Attempt to release alive packet socket: %p\n", sk
);
273 sk_refcnt_debug_dec(sk
);
277 static const struct proto_ops packet_ops
;
279 static const struct proto_ops packet_ops_spkt
;
281 static int packet_rcv_spkt(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
284 struct sockaddr_pkt
*spkt
;
287 * When we registered the protocol we saved the socket in the data
288 * field for just this event.
291 sk
= pt
->af_packet_priv
;
294 * Yank back the headers [hope the device set this
295 * right or kerboom...]
297 * Incoming packets have ll header pulled,
300 * For outgoing ones skb->data == skb_mac_header(skb)
301 * so that this procedure is noop.
304 if (skb
->pkt_type
== PACKET_LOOPBACK
)
307 if (dev_net(dev
) != sock_net(sk
))
310 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
313 /* drop any routing info */
314 dst_release(skb
->dst
);
317 /* drop conntrack reference */
320 spkt
= &PACKET_SKB_CB(skb
)->sa
.pkt
;
322 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
325 * The SOCK_PACKET socket receives _all_ frames.
328 spkt
->spkt_family
= dev
->type
;
329 strlcpy(spkt
->spkt_device
, dev
->name
, sizeof(spkt
->spkt_device
));
330 spkt
->spkt_protocol
= skb
->protocol
;
333 * Charge the memory to the socket. This is done specifically
334 * to prevent sockets using all the memory up.
337 if (sock_queue_rcv_skb(sk
,skb
) == 0)
348 * Output a raw packet to a device layer. This bypasses all the other
349 * protocol layers and you must therefore supply it with a complete frame
352 static int packet_sendmsg_spkt(struct kiocb
*iocb
, struct socket
*sock
,
353 struct msghdr
*msg
, size_t len
)
355 struct sock
*sk
= sock
->sk
;
356 struct sockaddr_pkt
*saddr
=(struct sockaddr_pkt
*)msg
->msg_name
;
358 struct net_device
*dev
;
363 * Get and verify the address.
368 if (msg
->msg_namelen
< sizeof(struct sockaddr
))
370 if (msg
->msg_namelen
==sizeof(struct sockaddr_pkt
))
371 proto
=saddr
->spkt_protocol
;
374 return(-ENOTCONN
); /* SOCK_PACKET must be sent giving an address */
377 * Find the device first to size check it
380 saddr
->spkt_device
[13] = 0;
381 dev
= dev_get_by_name(sock_net(sk
), saddr
->spkt_device
);
387 if (!(dev
->flags
& IFF_UP
))
391 * You may not queue a frame bigger than the mtu. This is the lowest level
392 * raw protocol and you must do your own fragmentation at this level.
396 if (len
> dev
->mtu
+ dev
->hard_header_len
)
400 skb
= sock_wmalloc(sk
, len
+ LL_RESERVED_SPACE(dev
), 0, GFP_KERNEL
);
403 * If the write buffer is full, then tough. At this level the user gets to
404 * deal with the problem - do your own algorithmic backoffs. That's far
415 /* FIXME: Save some space for broken drivers that write a
416 * hard header at transmission time by themselves. PPP is the
417 * notable one here. This should really be fixed at the driver level.
419 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
420 skb_reset_network_header(skb
);
422 /* Try to align data part correctly */
423 if (dev
->header_ops
) {
424 skb
->data
-= dev
->hard_header_len
;
425 skb
->tail
-= dev
->hard_header_len
;
426 if (len
< dev
->hard_header_len
)
427 skb_reset_network_header(skb
);
430 /* Returns -EFAULT on error */
431 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
432 skb
->protocol
= proto
;
434 skb
->priority
= sk
->sk_priority
;
454 static inline unsigned int run_filter(struct sk_buff
*skb
, struct sock
*sk
,
457 struct sk_filter
*filter
;
460 filter
= rcu_dereference(sk
->sk_filter
);
462 res
= sk_run_filter(skb
, filter
->insns
, filter
->len
);
463 rcu_read_unlock_bh();
469 This function makes lazy skb cloning in hope that most of packets
470 are discarded by BPF.
472 Note tricky part: we DO mangle shared skb! skb->data, skb->len
473 and skb->cb are mangled. It works because (and until) packets
474 falling here are owned by current CPU. Output packets are cloned
475 by dev_queue_xmit_nit(), input packets are processed by net_bh
476 sequencially, so that if we return skb to original state on exit,
477 we will not harm anyone.
480 static int packet_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
483 struct sockaddr_ll
*sll
;
484 struct packet_sock
*po
;
485 u8
* skb_head
= skb
->data
;
486 int skb_len
= skb
->len
;
487 unsigned int snaplen
, res
;
489 if (skb
->pkt_type
== PACKET_LOOPBACK
)
492 sk
= pt
->af_packet_priv
;
495 if (dev_net(dev
) != sock_net(sk
))
500 if (dev
->header_ops
) {
501 /* The device has an explicit notion of ll header,
502 exported to higher levels.
504 Otherwise, the device hides datails of it frame
505 structure, so that corresponding packet head
506 never delivered to user.
508 if (sk
->sk_type
!= SOCK_DGRAM
)
509 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
510 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
511 /* Special case: outgoing packets have ll header at head */
512 skb_pull(skb
, skb_network_offset(skb
));
518 res
= run_filter(skb
, sk
, snaplen
);
524 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
525 (unsigned)sk
->sk_rcvbuf
)
528 if (skb_shared(skb
)) {
529 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
533 if (skb_head
!= skb
->data
) {
534 skb
->data
= skb_head
;
541 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb
)) + MAX_ADDR_LEN
- 8 >
544 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
545 sll
->sll_family
= AF_PACKET
;
546 sll
->sll_hatype
= dev
->type
;
547 sll
->sll_protocol
= skb
->protocol
;
548 sll
->sll_pkttype
= skb
->pkt_type
;
549 if (unlikely(po
->origdev
))
550 sll
->sll_ifindex
= orig_dev
->ifindex
;
552 sll
->sll_ifindex
= dev
->ifindex
;
554 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
556 PACKET_SKB_CB(skb
)->origlen
= skb
->len
;
558 if (pskb_trim(skb
, snaplen
))
561 skb_set_owner_r(skb
, sk
);
563 dst_release(skb
->dst
);
566 /* drop conntrack reference */
569 spin_lock(&sk
->sk_receive_queue
.lock
);
570 po
->stats
.tp_packets
++;
571 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
572 spin_unlock(&sk
->sk_receive_queue
.lock
);
573 sk
->sk_data_ready(sk
, skb
->len
);
577 spin_lock(&sk
->sk_receive_queue
.lock
);
578 po
->stats
.tp_drops
++;
579 spin_unlock(&sk
->sk_receive_queue
.lock
);
582 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
583 skb
->data
= skb_head
;
591 #ifdef CONFIG_PACKET_MMAP
592 static int tpacket_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
595 struct packet_sock
*po
;
596 struct sockaddr_ll
*sll
;
598 struct tpacket_hdr
*h1
;
599 struct tpacket2_hdr
*h2
;
602 u8
* skb_head
= skb
->data
;
603 int skb_len
= skb
->len
;
604 unsigned int snaplen
, res
;
605 unsigned long status
= TP_STATUS_LOSING
|TP_STATUS_USER
;
606 unsigned short macoff
, netoff
, hdrlen
;
607 struct sk_buff
*copy_skb
= NULL
;
611 if (skb
->pkt_type
== PACKET_LOOPBACK
)
614 sk
= pt
->af_packet_priv
;
617 if (dev_net(dev
) != sock_net(sk
))
620 if (dev
->header_ops
) {
621 if (sk
->sk_type
!= SOCK_DGRAM
)
622 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
623 else if (skb
->pkt_type
== PACKET_OUTGOING
) {
624 /* Special case: outgoing packets have ll header at head */
625 skb_pull(skb
, skb_network_offset(skb
));
629 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
630 status
|= TP_STATUS_CSUMNOTREADY
;
634 res
= run_filter(skb
, sk
, snaplen
);
640 if (sk
->sk_type
== SOCK_DGRAM
) {
641 macoff
= netoff
= TPACKET_ALIGN(po
->tp_hdrlen
) + 16 +
644 unsigned maclen
= skb_network_offset(skb
);
645 netoff
= TPACKET_ALIGN(po
->tp_hdrlen
+
646 (maclen
< 16 ? 16 : maclen
)) +
648 macoff
= netoff
- maclen
;
651 if (macoff
+ snaplen
> po
->frame_size
) {
652 if (po
->copy_thresh
&&
653 atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
<
654 (unsigned)sk
->sk_rcvbuf
) {
655 if (skb_shared(skb
)) {
656 copy_skb
= skb_clone(skb
, GFP_ATOMIC
);
658 copy_skb
= skb_get(skb
);
659 skb_head
= skb
->data
;
662 skb_set_owner_r(copy_skb
, sk
);
664 snaplen
= po
->frame_size
- macoff
;
665 if ((int)snaplen
< 0)
669 spin_lock(&sk
->sk_receive_queue
.lock
);
670 h
.raw
= packet_lookup_frame(po
, po
->head
, TP_STATUS_KERNEL
);
673 po
->head
= po
->head
!= po
->frame_max
? po
->head
+1 : 0;
674 po
->stats
.tp_packets
++;
676 status
|= TP_STATUS_COPY
;
677 __skb_queue_tail(&sk
->sk_receive_queue
, copy_skb
);
679 if (!po
->stats
.tp_drops
)
680 status
&= ~TP_STATUS_LOSING
;
681 spin_unlock(&sk
->sk_receive_queue
.lock
);
683 skb_copy_bits(skb
, 0, h
.raw
+ macoff
, snaplen
);
685 switch (po
->tp_version
) {
687 h
.h1
->tp_len
= skb
->len
;
688 h
.h1
->tp_snaplen
= snaplen
;
689 h
.h1
->tp_mac
= macoff
;
690 h
.h1
->tp_net
= netoff
;
691 if (skb
->tstamp
.tv64
)
692 tv
= ktime_to_timeval(skb
->tstamp
);
694 do_gettimeofday(&tv
);
695 h
.h1
->tp_sec
= tv
.tv_sec
;
696 h
.h1
->tp_usec
= tv
.tv_usec
;
697 hdrlen
= sizeof(*h
.h1
);
700 h
.h2
->tp_len
= skb
->len
;
701 h
.h2
->tp_snaplen
= snaplen
;
702 h
.h2
->tp_mac
= macoff
;
703 h
.h2
->tp_net
= netoff
;
704 if (skb
->tstamp
.tv64
)
705 ts
= ktime_to_timespec(skb
->tstamp
);
708 h
.h2
->tp_sec
= ts
.tv_sec
;
709 h
.h2
->tp_nsec
= ts
.tv_nsec
;
710 h
.h2
->tp_vlan_tci
= skb
->vlan_tci
;
711 hdrlen
= sizeof(*h
.h2
);
717 sll
= h
.raw
+ TPACKET_ALIGN(hdrlen
);
718 sll
->sll_halen
= dev_parse_header(skb
, sll
->sll_addr
);
719 sll
->sll_family
= AF_PACKET
;
720 sll
->sll_hatype
= dev
->type
;
721 sll
->sll_protocol
= skb
->protocol
;
722 sll
->sll_pkttype
= skb
->pkt_type
;
723 if (unlikely(po
->origdev
))
724 sll
->sll_ifindex
= orig_dev
->ifindex
;
726 sll
->sll_ifindex
= dev
->ifindex
;
728 __packet_set_status(po
, h
.raw
, status
);
732 struct page
*p_start
, *p_end
;
733 u8
*h_end
= h
.raw
+ macoff
+ snaplen
- 1;
735 p_start
= virt_to_page(h
.raw
);
736 p_end
= virt_to_page(h_end
);
737 while (p_start
<= p_end
) {
738 flush_dcache_page(p_start
);
743 sk
->sk_data_ready(sk
, 0);
746 if (skb_head
!= skb
->data
&& skb_shared(skb
)) {
747 skb
->data
= skb_head
;
755 po
->stats
.tp_drops
++;
756 spin_unlock(&sk
->sk_receive_queue
.lock
);
758 sk
->sk_data_ready(sk
, 0);
766 static int packet_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
767 struct msghdr
*msg
, size_t len
)
769 struct sock
*sk
= sock
->sk
;
770 struct sockaddr_ll
*saddr
=(struct sockaddr_ll
*)msg
->msg_name
;
772 struct net_device
*dev
;
775 int ifindex
, err
, reserve
= 0;
778 * Get and verify the address.
782 struct packet_sock
*po
= pkt_sk(sk
);
784 ifindex
= po
->ifindex
;
789 if (msg
->msg_namelen
< sizeof(struct sockaddr_ll
))
791 if (msg
->msg_namelen
< (saddr
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
)))
793 ifindex
= saddr
->sll_ifindex
;
794 proto
= saddr
->sll_protocol
;
795 addr
= saddr
->sll_addr
;
799 dev
= dev_get_by_index(sock_net(sk
), ifindex
);
803 if (sock
->type
== SOCK_RAW
)
804 reserve
= dev
->hard_header_len
;
807 if (!(dev
->flags
& IFF_UP
))
811 if (len
> dev
->mtu
+reserve
)
814 skb
= sock_alloc_send_skb(sk
, len
+ LL_ALLOCATED_SPACE(dev
),
815 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
819 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
820 skb_reset_network_header(skb
);
823 if (sock
->type
== SOCK_DGRAM
&&
824 dev_hard_header(skb
, dev
, ntohs(proto
), addr
, NULL
, len
) < 0)
827 /* Returns -EFAULT on error */
828 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
832 skb
->protocol
= proto
;
834 skb
->priority
= sk
->sk_priority
;
840 err
= dev_queue_xmit(skb
);
841 if (err
> 0 && (err
= net_xmit_errno(err
)) != 0)
858 * Close a PACKET socket. This is fairly simple. We immediately go
859 * to 'closed' state and remove our protocol entry in the device list.
862 static int packet_release(struct socket
*sock
)
864 struct sock
*sk
= sock
->sk
;
865 struct packet_sock
*po
;
874 write_lock_bh(&net
->packet
.sklist_lock
);
875 sk_del_node_init(sk
);
876 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
877 write_unlock_bh(&net
->packet
.sklist_lock
);
880 * Unhook packet receive handler.
885 * Remove the protocol hook
887 dev_remove_pack(&po
->prot_hook
);
893 packet_flush_mclist(sk
);
895 #ifdef CONFIG_PACKET_MMAP
897 struct tpacket_req req
;
898 memset(&req
, 0, sizeof(req
));
899 packet_set_ring(sk
, &req
, 1);
904 * Now the socket is dead. No more input will appear.
912 skb_queue_purge(&sk
->sk_receive_queue
);
913 sk_refcnt_debug_release(sk
);
920 * Attach a packet hook.
923 static int packet_do_bind(struct sock
*sk
, struct net_device
*dev
, __be16 protocol
)
925 struct packet_sock
*po
= pkt_sk(sk
);
927 * Detach an existing hook if present.
932 spin_lock(&po
->bind_lock
);
937 spin_unlock(&po
->bind_lock
);
938 dev_remove_pack(&po
->prot_hook
);
939 spin_lock(&po
->bind_lock
);
943 po
->prot_hook
.type
= protocol
;
944 po
->prot_hook
.dev
= dev
;
946 po
->ifindex
= dev
? dev
->ifindex
: 0;
951 if (!dev
|| (dev
->flags
& IFF_UP
)) {
952 dev_add_pack(&po
->prot_hook
);
956 sk
->sk_err
= ENETDOWN
;
957 if (!sock_flag(sk
, SOCK_DEAD
))
958 sk
->sk_error_report(sk
);
962 spin_unlock(&po
->bind_lock
);
968 * Bind a packet socket to a device
971 static int packet_bind_spkt(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
973 struct sock
*sk
=sock
->sk
;
975 struct net_device
*dev
;
982 if (addr_len
!= sizeof(struct sockaddr
))
984 strlcpy(name
,uaddr
->sa_data
,sizeof(name
));
986 dev
= dev_get_by_name(sock_net(sk
), name
);
988 err
= packet_do_bind(sk
, dev
, pkt_sk(sk
)->num
);
994 static int packet_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
996 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
997 struct sock
*sk
=sock
->sk
;
998 struct net_device
*dev
= NULL
;
1006 if (addr_len
< sizeof(struct sockaddr_ll
))
1008 if (sll
->sll_family
!= AF_PACKET
)
1011 if (sll
->sll_ifindex
) {
1013 dev
= dev_get_by_index(sock_net(sk
), sll
->sll_ifindex
);
1017 err
= packet_do_bind(sk
, dev
, sll
->sll_protocol
? : pkt_sk(sk
)->num
);
1025 static struct proto packet_proto
= {
1027 .owner
= THIS_MODULE
,
1028 .obj_size
= sizeof(struct packet_sock
),
1032 * Create a packet of type SOCK_PACKET.
1035 static int packet_create(struct net
*net
, struct socket
*sock
, int protocol
)
1038 struct packet_sock
*po
;
1039 __be16 proto
= (__force __be16
)protocol
; /* weird, but documented */
1042 if (!capable(CAP_NET_RAW
))
1044 if (sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
&&
1045 sock
->type
!= SOCK_PACKET
)
1046 return -ESOCKTNOSUPPORT
;
1048 sock
->state
= SS_UNCONNECTED
;
1051 sk
= sk_alloc(net
, PF_PACKET
, GFP_KERNEL
, &packet_proto
);
1055 sock
->ops
= &packet_ops
;
1056 if (sock
->type
== SOCK_PACKET
)
1057 sock
->ops
= &packet_ops_spkt
;
1059 sock_init_data(sock
, sk
);
1062 sk
->sk_family
= PF_PACKET
;
1065 sk
->sk_destruct
= packet_sock_destruct
;
1066 sk_refcnt_debug_inc(sk
);
1069 * Attach a protocol block
1072 spin_lock_init(&po
->bind_lock
);
1073 mutex_init(&po
->pg_vec_lock
);
1074 po
->prot_hook
.func
= packet_rcv
;
1076 if (sock
->type
== SOCK_PACKET
)
1077 po
->prot_hook
.func
= packet_rcv_spkt
;
1079 po
->prot_hook
.af_packet_priv
= sk
;
1082 po
->prot_hook
.type
= proto
;
1083 dev_add_pack(&po
->prot_hook
);
1088 write_lock_bh(&net
->packet
.sklist_lock
);
1089 sk_add_node(sk
, &net
->packet
.sklist
);
1090 sock_prot_inuse_add(net
, &packet_proto
, 1);
1091 write_unlock_bh(&net
->packet
.sklist_lock
);
1098 * Pull a packet from our receive queue and hand it to the user.
1099 * If necessary we block.
1102 static int packet_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1103 struct msghdr
*msg
, size_t len
, int flags
)
1105 struct sock
*sk
= sock
->sk
;
1106 struct sk_buff
*skb
;
1108 struct sockaddr_ll
*sll
;
1111 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
1115 /* What error should we return now? EUNATTACH? */
1116 if (pkt_sk(sk
)->ifindex
< 0)
1121 * Call the generic datagram receiver. This handles all sorts
1122 * of horrible races and re-entrancy so we can forget about it
1123 * in the protocol layers.
1125 * Now it will return ENETDOWN, if device have just gone down,
1126 * but then it will block.
1129 skb
=skb_recv_datagram(sk
,flags
,flags
&MSG_DONTWAIT
,&err
);
1132 * An error occurred so return it. Because skb_recv_datagram()
1133 * handles the blocking we don't see and worry about blocking
1141 * If the address length field is there to be filled in, we fill
1145 sll
= &PACKET_SKB_CB(skb
)->sa
.ll
;
1146 if (sock
->type
== SOCK_PACKET
)
1147 msg
->msg_namelen
= sizeof(struct sockaddr_pkt
);
1149 msg
->msg_namelen
= sll
->sll_halen
+ offsetof(struct sockaddr_ll
, sll_addr
);
1152 * You lose any data beyond the buffer you gave. If it worries a
1153 * user program they can ask the device for its MTU anyway.
1160 msg
->msg_flags
|=MSG_TRUNC
;
1163 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1167 sock_recv_timestamp(msg
, sk
, skb
);
1170 memcpy(msg
->msg_name
, &PACKET_SKB_CB(skb
)->sa
,
1173 if (pkt_sk(sk
)->auxdata
) {
1174 struct tpacket_auxdata aux
;
1176 aux
.tp_status
= TP_STATUS_USER
;
1177 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1178 aux
.tp_status
|= TP_STATUS_CSUMNOTREADY
;
1179 aux
.tp_len
= PACKET_SKB_CB(skb
)->origlen
;
1180 aux
.tp_snaplen
= skb
->len
;
1182 aux
.tp_net
= skb_network_offset(skb
);
1183 aux
.tp_vlan_tci
= skb
->vlan_tci
;
1185 put_cmsg(msg
, SOL_PACKET
, PACKET_AUXDATA
, sizeof(aux
), &aux
);
1189 * Free or return the buffer as appropriate. Again this
1190 * hides all the races and re-entrancy issues from us.
1192 err
= (flags
&MSG_TRUNC
) ? skb
->len
: copied
;
1195 skb_free_datagram(sk
, skb
);
1200 static int packet_getname_spkt(struct socket
*sock
, struct sockaddr
*uaddr
,
1201 int *uaddr_len
, int peer
)
1203 struct net_device
*dev
;
1204 struct sock
*sk
= sock
->sk
;
1209 uaddr
->sa_family
= AF_PACKET
;
1210 dev
= dev_get_by_index(sock_net(sk
), pkt_sk(sk
)->ifindex
);
1212 strlcpy(uaddr
->sa_data
, dev
->name
, 15);
1215 memset(uaddr
->sa_data
, 0, 14);
1216 *uaddr_len
= sizeof(*uaddr
);
1221 static int packet_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1222 int *uaddr_len
, int peer
)
1224 struct net_device
*dev
;
1225 struct sock
*sk
= sock
->sk
;
1226 struct packet_sock
*po
= pkt_sk(sk
);
1227 struct sockaddr_ll
*sll
= (struct sockaddr_ll
*)uaddr
;
1232 sll
->sll_family
= AF_PACKET
;
1233 sll
->sll_ifindex
= po
->ifindex
;
1234 sll
->sll_protocol
= po
->num
;
1235 dev
= dev_get_by_index(sock_net(sk
), po
->ifindex
);
1237 sll
->sll_hatype
= dev
->type
;
1238 sll
->sll_halen
= dev
->addr_len
;
1239 memcpy(sll
->sll_addr
, dev
->dev_addr
, dev
->addr_len
);
1242 sll
->sll_hatype
= 0; /* Bad: we have no ARPHRD_UNSPEC */
1245 *uaddr_len
= offsetof(struct sockaddr_ll
, sll_addr
) + sll
->sll_halen
;
1250 static int packet_dev_mc(struct net_device
*dev
, struct packet_mclist
*i
,
1254 case PACKET_MR_MULTICAST
:
1256 dev_mc_add(dev
, i
->addr
, i
->alen
, 0);
1258 dev_mc_delete(dev
, i
->addr
, i
->alen
, 0);
1260 case PACKET_MR_PROMISC
:
1261 return dev_set_promiscuity(dev
, what
);
1263 case PACKET_MR_ALLMULTI
:
1264 return dev_set_allmulti(dev
, what
);
1271 static void packet_dev_mclist(struct net_device
*dev
, struct packet_mclist
*i
, int what
)
1273 for ( ; i
; i
=i
->next
) {
1274 if (i
->ifindex
== dev
->ifindex
)
1275 packet_dev_mc(dev
, i
, what
);
1279 static int packet_mc_add(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1281 struct packet_sock
*po
= pkt_sk(sk
);
1282 struct packet_mclist
*ml
, *i
;
1283 struct net_device
*dev
;
1289 dev
= __dev_get_by_index(sock_net(sk
), mreq
->mr_ifindex
);
1294 if (mreq
->mr_alen
> dev
->addr_len
)
1298 i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
1303 for (ml
= po
->mclist
; ml
; ml
= 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) {
1309 /* Free the new element ... */
1315 i
->type
= mreq
->mr_type
;
1316 i
->ifindex
= mreq
->mr_ifindex
;
1317 i
->alen
= mreq
->mr_alen
;
1318 memcpy(i
->addr
, mreq
->mr_address
, i
->alen
);
1320 i
->next
= po
->mclist
;
1322 err
= packet_dev_mc(dev
, i
, 1);
1324 po
->mclist
= i
->next
;
1333 static int packet_mc_drop(struct sock
*sk
, struct packet_mreq_max
*mreq
)
1335 struct packet_mclist
*ml
, **mlp
;
1339 for (mlp
= &pkt_sk(sk
)->mclist
; (ml
= *mlp
) != NULL
; mlp
= &ml
->next
) {
1340 if (ml
->ifindex
== mreq
->mr_ifindex
&&
1341 ml
->type
== mreq
->mr_type
&&
1342 ml
->alen
== mreq
->mr_alen
&&
1343 memcmp(ml
->addr
, mreq
->mr_address
, ml
->alen
) == 0) {
1344 if (--ml
->count
== 0) {
1345 struct net_device
*dev
;
1347 dev
= dev_get_by_index(sock_net(sk
), ml
->ifindex
);
1349 packet_dev_mc(dev
, ml
, -1);
1359 return -EADDRNOTAVAIL
;
1362 static void packet_flush_mclist(struct sock
*sk
)
1364 struct packet_sock
*po
= pkt_sk(sk
);
1365 struct packet_mclist
*ml
;
1371 while ((ml
= po
->mclist
) != NULL
) {
1372 struct net_device
*dev
;
1374 po
->mclist
= ml
->next
;
1375 if ((dev
= dev_get_by_index(sock_net(sk
), ml
->ifindex
)) != NULL
) {
1376 packet_dev_mc(dev
, ml
, -1);
1385 packet_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1387 struct sock
*sk
= sock
->sk
;
1388 struct packet_sock
*po
= pkt_sk(sk
);
1391 if (level
!= SOL_PACKET
)
1392 return -ENOPROTOOPT
;
1395 case PACKET_ADD_MEMBERSHIP
:
1396 case PACKET_DROP_MEMBERSHIP
:
1398 struct packet_mreq_max mreq
;
1400 memset(&mreq
, 0, sizeof(mreq
));
1401 if (len
< sizeof(struct packet_mreq
))
1403 if (len
> sizeof(mreq
))
1405 if (copy_from_user(&mreq
,optval
,len
))
1407 if (len
< (mreq
.mr_alen
+ offsetof(struct packet_mreq
, mr_address
)))
1409 if (optname
== PACKET_ADD_MEMBERSHIP
)
1410 ret
= packet_mc_add(sk
, &mreq
);
1412 ret
= packet_mc_drop(sk
, &mreq
);
1416 #ifdef CONFIG_PACKET_MMAP
1417 case PACKET_RX_RING
:
1419 struct tpacket_req req
;
1421 if (optlen
<sizeof(req
))
1423 if (copy_from_user(&req
,optval
,sizeof(req
)))
1425 return packet_set_ring(sk
, &req
, 0);
1427 case PACKET_COPY_THRESH
:
1431 if (optlen
!=sizeof(val
))
1433 if (copy_from_user(&val
,optval
,sizeof(val
)))
1436 pkt_sk(sk
)->copy_thresh
= val
;
1439 case PACKET_VERSION
:
1443 if (optlen
!= sizeof(val
))
1447 if (copy_from_user(&val
, optval
, sizeof(val
)))
1452 po
->tp_version
= val
;
1458 case PACKET_RESERVE
:
1462 if (optlen
!= sizeof(val
))
1466 if (copy_from_user(&val
, optval
, sizeof(val
)))
1468 po
->tp_reserve
= val
;
1472 case PACKET_AUXDATA
:
1476 if (optlen
< sizeof(val
))
1478 if (copy_from_user(&val
, optval
, sizeof(val
)))
1481 po
->auxdata
= !!val
;
1484 case PACKET_ORIGDEV
:
1488 if (optlen
< sizeof(val
))
1490 if (copy_from_user(&val
, optval
, sizeof(val
)))
1493 po
->origdev
= !!val
;
1497 return -ENOPROTOOPT
;
1501 static int packet_getsockopt(struct socket
*sock
, int level
, int optname
,
1502 char __user
*optval
, int __user
*optlen
)
1506 struct sock
*sk
= sock
->sk
;
1507 struct packet_sock
*po
= pkt_sk(sk
);
1509 struct tpacket_stats st
;
1511 if (level
!= SOL_PACKET
)
1512 return -ENOPROTOOPT
;
1514 if (get_user(len
, optlen
))
1521 case PACKET_STATISTICS
:
1522 if (len
> sizeof(struct tpacket_stats
))
1523 len
= sizeof(struct tpacket_stats
);
1524 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1526 memset(&po
->stats
, 0, sizeof(st
));
1527 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1528 st
.tp_packets
+= st
.tp_drops
;
1532 case PACKET_AUXDATA
:
1533 if (len
> sizeof(int))
1539 case PACKET_ORIGDEV
:
1540 if (len
> sizeof(int))
1546 #ifdef CONFIG_PACKET_MMAP
1547 case PACKET_VERSION
:
1548 if (len
> sizeof(int))
1550 val
= po
->tp_version
;
1554 if (len
> sizeof(int))
1556 if (copy_from_user(&val
, optval
, len
))
1560 val
= sizeof(struct tpacket_hdr
);
1563 val
= sizeof(struct tpacket2_hdr
);
1570 case PACKET_RESERVE
:
1571 if (len
> sizeof(unsigned int))
1572 len
= sizeof(unsigned int);
1573 val
= po
->tp_reserve
;
1578 return -ENOPROTOOPT
;
1581 if (put_user(len
, optlen
))
1583 if (copy_to_user(optval
, data
, len
))
1589 static int packet_notifier(struct notifier_block
*this, unsigned long msg
, void *data
)
1592 struct hlist_node
*node
;
1593 struct net_device
*dev
= data
;
1594 struct net
*net
= dev_net(dev
);
1596 read_lock(&net
->packet
.sklist_lock
);
1597 sk_for_each(sk
, node
, &net
->packet
.sklist
) {
1598 struct packet_sock
*po
= pkt_sk(sk
);
1601 case NETDEV_UNREGISTER
:
1603 packet_dev_mclist(dev
, po
->mclist
, -1);
1607 if (dev
->ifindex
== po
->ifindex
) {
1608 spin_lock(&po
->bind_lock
);
1610 __dev_remove_pack(&po
->prot_hook
);
1613 sk
->sk_err
= ENETDOWN
;
1614 if (!sock_flag(sk
, SOCK_DEAD
))
1615 sk
->sk_error_report(sk
);
1617 if (msg
== NETDEV_UNREGISTER
) {
1619 po
->prot_hook
.dev
= NULL
;
1621 spin_unlock(&po
->bind_lock
);
1625 spin_lock(&po
->bind_lock
);
1626 if (dev
->ifindex
== po
->ifindex
&& po
->num
&&
1628 dev_add_pack(&po
->prot_hook
);
1632 spin_unlock(&po
->bind_lock
);
1636 read_unlock(&net
->packet
.sklist_lock
);
1641 static int packet_ioctl(struct socket
*sock
, unsigned int cmd
,
1644 struct sock
*sk
= sock
->sk
;
1649 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1650 return put_user(amount
, (int __user
*)arg
);
1654 struct sk_buff
*skb
;
1657 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1658 skb
= skb_peek(&sk
->sk_receive_queue
);
1661 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1662 return put_user(amount
, (int __user
*)arg
);
1665 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
1667 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
1677 case SIOCGIFBRDADDR
:
1678 case SIOCSIFBRDADDR
:
1679 case SIOCGIFNETMASK
:
1680 case SIOCSIFNETMASK
:
1681 case SIOCGIFDSTADDR
:
1682 case SIOCSIFDSTADDR
:
1684 if (!net_eq(sock_net(sk
), &init_net
))
1685 return -ENOIOCTLCMD
;
1686 return inet_dgram_ops
.ioctl(sock
, cmd
, arg
);
1690 return -ENOIOCTLCMD
;
1695 #ifndef CONFIG_PACKET_MMAP
1696 #define packet_mmap sock_no_mmap
1697 #define packet_poll datagram_poll
1700 static unsigned int packet_poll(struct file
* file
, struct socket
*sock
,
1703 struct sock
*sk
= sock
->sk
;
1704 struct packet_sock
*po
= pkt_sk(sk
);
1705 unsigned int mask
= datagram_poll(file
, sock
, wait
);
1707 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1709 unsigned last
= po
->head
? po
->head
-1 : po
->frame_max
;
1711 if (packet_lookup_frame(po
, last
, TP_STATUS_USER
))
1712 mask
|= POLLIN
| POLLRDNORM
;
1714 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1719 /* Dirty? Well, I still did not learn better way to account
1723 static void packet_mm_open(struct vm_area_struct
*vma
)
1725 struct file
*file
= vma
->vm_file
;
1726 struct socket
* sock
= file
->private_data
;
1727 struct sock
*sk
= sock
->sk
;
1730 atomic_inc(&pkt_sk(sk
)->mapped
);
1733 static void packet_mm_close(struct vm_area_struct
*vma
)
1735 struct file
*file
= vma
->vm_file
;
1736 struct socket
* sock
= file
->private_data
;
1737 struct sock
*sk
= sock
->sk
;
1740 atomic_dec(&pkt_sk(sk
)->mapped
);
1743 static struct vm_operations_struct packet_mmap_ops
= {
1744 .open
= packet_mm_open
,
1745 .close
=packet_mm_close
,
1748 static void free_pg_vec(char **pg_vec
, unsigned int order
, unsigned int len
)
1752 for (i
= 0; i
< len
; i
++) {
1753 if (likely(pg_vec
[i
]))
1754 free_pages((unsigned long) pg_vec
[i
], order
);
1759 static inline char *alloc_one_pg_vec_page(unsigned long order
)
1761 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
| __GFP_NOWARN
;
1763 return (char *) __get_free_pages(gfp_flags
, order
);
1766 static char **alloc_pg_vec(struct tpacket_req
*req
, int order
)
1768 unsigned int block_nr
= req
->tp_block_nr
;
1772 pg_vec
= kzalloc(block_nr
* sizeof(char *), GFP_KERNEL
);
1773 if (unlikely(!pg_vec
))
1776 for (i
= 0; i
< block_nr
; i
++) {
1777 pg_vec
[i
] = alloc_one_pg_vec_page(order
);
1778 if (unlikely(!pg_vec
[i
]))
1779 goto out_free_pgvec
;
1786 free_pg_vec(pg_vec
, order
, block_nr
);
1791 static int packet_set_ring(struct sock
*sk
, struct tpacket_req
*req
, int closing
)
1793 char **pg_vec
= NULL
;
1794 struct packet_sock
*po
= pkt_sk(sk
);
1795 int was_running
, order
= 0;
1799 if (req
->tp_block_nr
) {
1802 /* Sanity tests and some calculations */
1804 if (unlikely(po
->pg_vec
))
1807 switch (po
->tp_version
) {
1809 po
->tp_hdrlen
= TPACKET_HDRLEN
;
1812 po
->tp_hdrlen
= TPACKET2_HDRLEN
;
1816 if (unlikely((int)req
->tp_block_size
<= 0))
1818 if (unlikely(req
->tp_block_size
& (PAGE_SIZE
- 1)))
1820 if (unlikely(req
->tp_frame_size
< po
->tp_hdrlen
+
1823 if (unlikely(req
->tp_frame_size
& (TPACKET_ALIGNMENT
- 1)))
1826 po
->frames_per_block
= req
->tp_block_size
/req
->tp_frame_size
;
1827 if (unlikely(po
->frames_per_block
<= 0))
1829 if (unlikely((po
->frames_per_block
* req
->tp_block_nr
) !=
1834 order
= get_order(req
->tp_block_size
);
1835 pg_vec
= alloc_pg_vec(req
, order
);
1836 if (unlikely(!pg_vec
))
1839 for (i
= 0; i
< req
->tp_block_nr
; i
++) {
1840 void *ptr
= pg_vec
[i
];
1843 for (k
= 0; k
< po
->frames_per_block
; k
++) {
1844 __packet_set_status(po
, ptr
, TP_STATUS_KERNEL
);
1845 ptr
+= req
->tp_frame_size
;
1850 if (unlikely(req
->tp_frame_nr
))
1856 /* Detach socket from network */
1857 spin_lock(&po
->bind_lock
);
1858 was_running
= po
->running
;
1861 __dev_remove_pack(&po
->prot_hook
);
1866 spin_unlock(&po
->bind_lock
);
1871 mutex_lock(&po
->pg_vec_lock
);
1872 if (closing
|| atomic_read(&po
->mapped
) == 0) {
1874 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1876 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1877 pg_vec
= XC(po
->pg_vec
, pg_vec
);
1878 po
->frame_max
= (req
->tp_frame_nr
- 1);
1880 po
->frame_size
= req
->tp_frame_size
;
1881 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1883 order
= XC(po
->pg_vec_order
, order
);
1884 req
->tp_block_nr
= XC(po
->pg_vec_len
, req
->tp_block_nr
);
1886 po
->pg_vec_pages
= req
->tp_block_size
/PAGE_SIZE
;
1887 po
->prot_hook
.func
= po
->pg_vec
? tpacket_rcv
: packet_rcv
;
1888 skb_queue_purge(&sk
->sk_receive_queue
);
1890 if (atomic_read(&po
->mapped
))
1891 printk(KERN_DEBUG
"packet_mmap: vma is busy: %d\n", atomic_read(&po
->mapped
));
1893 mutex_unlock(&po
->pg_vec_lock
);
1895 spin_lock(&po
->bind_lock
);
1896 if (was_running
&& !po
->running
) {
1900 dev_add_pack(&po
->prot_hook
);
1902 spin_unlock(&po
->bind_lock
);
1907 free_pg_vec(pg_vec
, order
, req
->tp_block_nr
);
1912 static int packet_mmap(struct file
*file
, struct socket
*sock
, struct vm_area_struct
*vma
)
1914 struct sock
*sk
= sock
->sk
;
1915 struct packet_sock
*po
= pkt_sk(sk
);
1917 unsigned long start
;
1924 size
= vma
->vm_end
- vma
->vm_start
;
1926 mutex_lock(&po
->pg_vec_lock
);
1927 if (po
->pg_vec
== NULL
)
1929 if (size
!= po
->pg_vec_len
*po
->pg_vec_pages
*PAGE_SIZE
)
1932 start
= vma
->vm_start
;
1933 for (i
= 0; i
< po
->pg_vec_len
; i
++) {
1934 struct page
*page
= virt_to_page(po
->pg_vec
[i
]);
1937 for (pg_num
= 0; pg_num
< po
->pg_vec_pages
; pg_num
++, page
++) {
1938 err
= vm_insert_page(vma
, start
, page
);
1944 atomic_inc(&po
->mapped
);
1945 vma
->vm_ops
= &packet_mmap_ops
;
1949 mutex_unlock(&po
->pg_vec_lock
);
1955 static const struct proto_ops packet_ops_spkt
= {
1956 .family
= PF_PACKET
,
1957 .owner
= THIS_MODULE
,
1958 .release
= packet_release
,
1959 .bind
= packet_bind_spkt
,
1960 .connect
= sock_no_connect
,
1961 .socketpair
= sock_no_socketpair
,
1962 .accept
= sock_no_accept
,
1963 .getname
= packet_getname_spkt
,
1964 .poll
= datagram_poll
,
1965 .ioctl
= packet_ioctl
,
1966 .listen
= sock_no_listen
,
1967 .shutdown
= sock_no_shutdown
,
1968 .setsockopt
= sock_no_setsockopt
,
1969 .getsockopt
= sock_no_getsockopt
,
1970 .sendmsg
= packet_sendmsg_spkt
,
1971 .recvmsg
= packet_recvmsg
,
1972 .mmap
= sock_no_mmap
,
1973 .sendpage
= sock_no_sendpage
,
1976 static const struct proto_ops packet_ops
= {
1977 .family
= PF_PACKET
,
1978 .owner
= THIS_MODULE
,
1979 .release
= packet_release
,
1980 .bind
= packet_bind
,
1981 .connect
= sock_no_connect
,
1982 .socketpair
= sock_no_socketpair
,
1983 .accept
= sock_no_accept
,
1984 .getname
= packet_getname
,
1985 .poll
= packet_poll
,
1986 .ioctl
= packet_ioctl
,
1987 .listen
= sock_no_listen
,
1988 .shutdown
= sock_no_shutdown
,
1989 .setsockopt
= packet_setsockopt
,
1990 .getsockopt
= packet_getsockopt
,
1991 .sendmsg
= packet_sendmsg
,
1992 .recvmsg
= packet_recvmsg
,
1993 .mmap
= packet_mmap
,
1994 .sendpage
= sock_no_sendpage
,
1997 static struct net_proto_family packet_family_ops
= {
1998 .family
= PF_PACKET
,
1999 .create
= packet_create
,
2000 .owner
= THIS_MODULE
,
2003 static struct notifier_block packet_netdev_notifier
= {
2004 .notifier_call
=packet_notifier
,
2007 #ifdef CONFIG_PROC_FS
2008 static inline struct sock
*packet_seq_idx(struct net
*net
, loff_t off
)
2011 struct hlist_node
*node
;
2013 sk_for_each(s
, node
, &net
->packet
.sklist
) {
2020 static void *packet_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2021 __acquires(seq_file_net(seq
)->packet
.sklist_lock
)
2023 struct net
*net
= seq_file_net(seq
);
2024 read_lock(&net
->packet
.sklist_lock
);
2025 return *pos
? packet_seq_idx(net
, *pos
- 1) : SEQ_START_TOKEN
;
2028 static void *packet_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2030 struct net
*net
= seq_file_net(seq
);
2032 return (v
== SEQ_START_TOKEN
)
2033 ? sk_head(&net
->packet
.sklist
)
2034 : sk_next((struct sock
*)v
) ;
2037 static void packet_seq_stop(struct seq_file
*seq
, void *v
)
2038 __releases(seq_file_net(seq
)->packet
.sklist_lock
)
2040 struct net
*net
= seq_file_net(seq
);
2041 read_unlock(&net
->packet
.sklist_lock
);
2044 static int packet_seq_show(struct seq_file
*seq
, void *v
)
2046 if (v
== SEQ_START_TOKEN
)
2047 seq_puts(seq
, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2050 const struct packet_sock
*po
= pkt_sk(s
);
2053 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2055 atomic_read(&s
->sk_refcnt
),
2060 atomic_read(&s
->sk_rmem_alloc
),
2068 static const struct seq_operations packet_seq_ops
= {
2069 .start
= packet_seq_start
,
2070 .next
= packet_seq_next
,
2071 .stop
= packet_seq_stop
,
2072 .show
= packet_seq_show
,
2075 static int packet_seq_open(struct inode
*inode
, struct file
*file
)
2077 return seq_open_net(inode
, file
, &packet_seq_ops
,
2078 sizeof(struct seq_net_private
));
2081 static const struct file_operations packet_seq_fops
= {
2082 .owner
= THIS_MODULE
,
2083 .open
= packet_seq_open
,
2085 .llseek
= seq_lseek
,
2086 .release
= seq_release_net
,
2091 static int packet_net_init(struct net
*net
)
2093 rwlock_init(&net
->packet
.sklist_lock
);
2094 INIT_HLIST_HEAD(&net
->packet
.sklist
);
2096 if (!proc_net_fops_create(net
, "packet", 0, &packet_seq_fops
))
2102 static void packet_net_exit(struct net
*net
)
2104 proc_net_remove(net
, "packet");
2107 static struct pernet_operations packet_net_ops
= {
2108 .init
= packet_net_init
,
2109 .exit
= packet_net_exit
,
2113 static void __exit
packet_exit(void)
2115 unregister_netdevice_notifier(&packet_netdev_notifier
);
2116 unregister_pernet_subsys(&packet_net_ops
);
2117 sock_unregister(PF_PACKET
);
2118 proto_unregister(&packet_proto
);
2121 static int __init
packet_init(void)
2123 int rc
= proto_register(&packet_proto
, 0);
2128 sock_register(&packet_family_ops
);
2129 register_pernet_subsys(&packet_net_ops
);
2130 register_netdevice_notifier(&packet_netdev_notifier
);
2135 module_init(packet_init
);
2136 module_exit(packet_exit
);
2137 MODULE_LICENSE("GPL");
2138 MODULE_ALIAS_NETPROTO(PF_PACKET
);