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
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 unsigned int running:1, /* prot_hook is attached*/
205 int ifindex; /* bound device */
207 #ifdef CONFIG_PACKET_MULTICAST
208 struct packet_mclist *mclist;
210 #ifdef CONFIG_PACKET_MMAP
212 unsigned int pg_vec_order;
213 unsigned int pg_vec_pages;
214 unsigned int pg_vec_len;
218 struct packet_skb_cb {
219 unsigned int origlen;
221 struct sockaddr_pkt pkt;
222 struct sockaddr_ll ll;
226 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
228 #ifdef CONFIG_PACKET_MMAP
230 static inline char *packet_lookup_frame(struct packet_sock *po, unsigned int position)
232 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 frame = po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size);
244 static inline struct packet_sock *pkt_sk(struct sock *sk)
246 return (struct packet_sock *)sk;
249 static void packet_sock_destruct(struct sock *sk)
251 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
252 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
254 if (!sock_flag(sk, SOCK_DEAD)) {
255 printk("Attempt to release alive packet socket: %p\n", sk);
259 atomic_dec(&packet_socks_nr);
260 #ifdef PACKET_REFCNT_DEBUG
261 printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
266 static const struct proto_ops packet_ops;
268 #ifdef CONFIG_SOCK_PACKET
269 static const struct proto_ops packet_ops_spkt;
271 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
274 struct sockaddr_pkt *spkt;
277 * When we registered the protocol we saved the socket in the data
278 * field for just this event.
281 sk = pt->af_packet_priv;
284 * Yank back the headers [hope the device set this
285 * right or kerboom...]
287 * Incoming packets have ll header pulled,
290 * For outgoing ones skb->data == skb->mac.raw
291 * so that this procedure is noop.
294 if (skb->pkt_type == PACKET_LOOPBACK)
297 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
300 /* drop any routing info */
301 dst_release(skb->dst);
304 /* drop conntrack reference */
307 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
309 skb_push(skb, skb->data-skb->mac.raw);
312 * The SOCK_PACKET socket receives _all_ frames.
315 spkt->spkt_family = dev->type;
316 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
317 spkt->spkt_protocol = skb->protocol;
320 * Charge the memory to the socket. This is done specifically
321 * to prevent sockets using all the memory up.
324 if (sock_queue_rcv_skb(sk,skb) == 0)
335 * Output a raw packet to a device layer. This bypasses all the other
336 * protocol layers and you must therefore supply it with a complete frame
339 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
340 struct msghdr *msg, size_t len)
342 struct sock *sk = sock->sk;
343 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
345 struct net_device *dev;
350 * Get and verify the address.
355 if (msg->msg_namelen < sizeof(struct sockaddr))
357 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
358 proto=saddr->spkt_protocol;
361 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
364 * Find the device first to size check it
367 saddr->spkt_device[13] = 0;
368 dev = dev_get_by_name(saddr->spkt_device);
374 if (!(dev->flags & IFF_UP))
378 * You may not queue a frame bigger than the mtu. This is the lowest level
379 * raw protocol and you must do your own fragmentation at this level.
383 if (len > dev->mtu + dev->hard_header_len)
387 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
390 * If the write buffer is full, then tough. At this level the user gets to
391 * deal with the problem - do your own algorithmic backoffs. That's far
402 /* FIXME: Save some space for broken drivers that write a
403 * hard header at transmission time by themselves. PPP is the
404 * notable one here. This should really be fixed at the driver level.
406 skb_reserve(skb, LL_RESERVED_SPACE(dev));
407 skb->nh.raw = skb->data;
409 /* Try to align data part correctly */
410 if (dev->hard_header) {
411 skb->data -= dev->hard_header_len;
412 skb->tail -= dev->hard_header_len;
413 if (len < dev->hard_header_len)
414 skb->nh.raw = skb->data;
417 /* Returns -EFAULT on error */
418 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
419 skb->protocol = proto;
421 skb->priority = sk->sk_priority;
442 static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
445 struct sk_filter *filter;
448 filter = rcu_dereference(sk->sk_filter);
450 res = sk_run_filter(skb, filter->insns, filter->len);
451 rcu_read_unlock_bh();
457 This function makes lazy skb cloning in hope that most of packets
458 are discarded by BPF.
460 Note tricky part: we DO mangle shared skb! skb->data, skb->len
461 and skb->cb are mangled. It works because (and until) packets
462 falling here are owned by current CPU. Output packets are cloned
463 by dev_queue_xmit_nit(), input packets are processed by net_bh
464 sequencially, so that if we return skb to original state on exit,
465 we will not harm anyone.
468 static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
471 struct sockaddr_ll *sll;
472 struct packet_sock *po;
473 u8 * skb_head = skb->data;
474 int skb_len = skb->len;
475 unsigned int snaplen, res;
477 if (skb->pkt_type == PACKET_LOOPBACK)
480 sk = pt->af_packet_priv;
485 if (dev->hard_header) {
486 /* The device has an explicit notion of ll header,
487 exported to higher levels.
489 Otherwise, the device hides datails of it frame
490 structure, so that corresponding packet head
491 never delivered to user.
493 if (sk->sk_type != SOCK_DGRAM)
494 skb_push(skb, skb->data - skb->mac.raw);
495 else if (skb->pkt_type == PACKET_OUTGOING) {
496 /* Special case: outgoing packets have ll header at head */
497 skb_pull(skb, skb->nh.raw - skb->data);
503 res = run_filter(skb, sk, snaplen);
509 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
510 (unsigned)sk->sk_rcvbuf)
513 if (skb_shared(skb)) {
514 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
518 if (skb_head != skb->data) {
519 skb->data = skb_head;
526 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
529 sll = &PACKET_SKB_CB(skb)->sa.ll;
530 sll->sll_family = AF_PACKET;
531 sll->sll_hatype = dev->type;
532 sll->sll_protocol = skb->protocol;
533 sll->sll_pkttype = skb->pkt_type;
534 sll->sll_ifindex = dev->ifindex;
537 if (dev->hard_header_parse)
538 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
540 PACKET_SKB_CB(skb)->origlen = skb->len;
542 if (pskb_trim(skb, snaplen))
545 skb_set_owner_r(skb, sk);
547 dst_release(skb->dst);
550 /* drop conntrack reference */
553 spin_lock(&sk->sk_receive_queue.lock);
554 po->stats.tp_packets++;
555 __skb_queue_tail(&sk->sk_receive_queue, skb);
556 spin_unlock(&sk->sk_receive_queue.lock);
557 sk->sk_data_ready(sk, skb->len);
561 spin_lock(&sk->sk_receive_queue.lock);
562 po->stats.tp_drops++;
563 spin_unlock(&sk->sk_receive_queue.lock);
566 if (skb_head != skb->data && skb_shared(skb)) {
567 skb->data = skb_head;
575 #ifdef CONFIG_PACKET_MMAP
576 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
579 struct packet_sock *po;
580 struct sockaddr_ll *sll;
581 struct tpacket_hdr *h;
582 u8 * skb_head = skb->data;
583 int skb_len = skb->len;
584 unsigned int snaplen, res;
585 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
586 unsigned short macoff, netoff;
587 struct sk_buff *copy_skb = NULL;
589 if (skb->pkt_type == PACKET_LOOPBACK)
592 sk = pt->af_packet_priv;
595 if (dev->hard_header) {
596 if (sk->sk_type != SOCK_DGRAM)
597 skb_push(skb, skb->data - skb->mac.raw);
598 else if (skb->pkt_type == PACKET_OUTGOING) {
599 /* Special case: outgoing packets have ll header at head */
600 skb_pull(skb, skb->nh.raw - skb->data);
604 if (skb->ip_summed == CHECKSUM_PARTIAL)
605 status |= TP_STATUS_CSUMNOTREADY;
609 res = run_filter(skb, sk, snaplen);
615 if (sk->sk_type == SOCK_DGRAM) {
616 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
618 unsigned maclen = skb->nh.raw - skb->data;
619 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
620 macoff = netoff - maclen;
623 if (macoff + snaplen > po->frame_size) {
624 if (po->copy_thresh &&
625 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
626 (unsigned)sk->sk_rcvbuf) {
627 if (skb_shared(skb)) {
628 copy_skb = skb_clone(skb, GFP_ATOMIC);
630 copy_skb = skb_get(skb);
631 skb_head = skb->data;
634 skb_set_owner_r(copy_skb, sk);
636 snaplen = po->frame_size - macoff;
637 if ((int)snaplen < 0)
641 spin_lock(&sk->sk_receive_queue.lock);
642 h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
646 po->head = po->head != po->frame_max ? po->head+1 : 0;
647 po->stats.tp_packets++;
649 status |= TP_STATUS_COPY;
650 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
652 if (!po->stats.tp_drops)
653 status &= ~TP_STATUS_LOSING;
654 spin_unlock(&sk->sk_receive_queue.lock);
656 skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
658 h->tp_len = skb->len;
659 h->tp_snaplen = snaplen;
662 if (skb->tstamp.off_sec == 0) {
663 __net_timestamp(skb);
664 sock_enable_timestamp(sk);
666 h->tp_sec = skb->tstamp.off_sec;
667 h->tp_usec = skb->tstamp.off_usec;
669 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
671 if (dev->hard_header_parse)
672 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
673 sll->sll_family = AF_PACKET;
674 sll->sll_hatype = dev->type;
675 sll->sll_protocol = skb->protocol;
676 sll->sll_pkttype = skb->pkt_type;
677 sll->sll_ifindex = dev->ifindex;
679 h->tp_status = status;
683 struct page *p_start, *p_end;
684 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
686 p_start = virt_to_page(h);
687 p_end = virt_to_page(h_end);
688 while (p_start <= p_end) {
689 flush_dcache_page(p_start);
694 sk->sk_data_ready(sk, 0);
697 if (skb_head != skb->data && skb_shared(skb)) {
698 skb->data = skb_head;
706 po->stats.tp_drops++;
707 spin_unlock(&sk->sk_receive_queue.lock);
709 sk->sk_data_ready(sk, 0);
718 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
719 struct msghdr *msg, size_t len)
721 struct sock *sk = sock->sk;
722 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
724 struct net_device *dev;
727 int ifindex, err, reserve = 0;
730 * Get and verify the address.
734 struct packet_sock *po = pkt_sk(sk);
736 ifindex = po->ifindex;
741 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
743 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
745 ifindex = saddr->sll_ifindex;
746 proto = saddr->sll_protocol;
747 addr = saddr->sll_addr;
751 dev = dev_get_by_index(ifindex);
755 if (sock->type == SOCK_RAW)
756 reserve = dev->hard_header_len;
759 if (!(dev->flags & IFF_UP))
763 if (len > dev->mtu+reserve)
766 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
767 msg->msg_flags & MSG_DONTWAIT, &err);
771 skb_reserve(skb, LL_RESERVED_SPACE(dev));
772 skb->nh.raw = skb->data;
774 if (dev->hard_header) {
777 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
778 if (sock->type != SOCK_DGRAM) {
779 skb->tail = skb->data;
785 /* Returns -EFAULT on error */
786 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
790 skb->protocol = proto;
792 skb->priority = sk->sk_priority;
798 err = dev_queue_xmit(skb);
799 if (err > 0 && (err = net_xmit_errno(err)) != 0)
816 * Close a PACKET socket. This is fairly simple. We immediately go
817 * to 'closed' state and remove our protocol entry in the device list.
820 static int packet_release(struct socket *sock)
822 struct sock *sk = sock->sk;
823 struct packet_sock *po;
830 write_lock_bh(&packet_sklist_lock);
831 sk_del_node_init(sk);
832 write_unlock_bh(&packet_sklist_lock);
835 * Unhook packet receive handler.
840 * Remove the protocol hook
842 dev_remove_pack(&po->prot_hook);
848 #ifdef CONFIG_PACKET_MULTICAST
849 packet_flush_mclist(sk);
852 #ifdef CONFIG_PACKET_MMAP
854 struct tpacket_req req;
855 memset(&req, 0, sizeof(req));
856 packet_set_ring(sk, &req, 1);
861 * Now the socket is dead. No more input will appear.
869 skb_queue_purge(&sk->sk_receive_queue);
876 * Attach a packet hook.
879 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
881 struct packet_sock *po = pkt_sk(sk);
883 * Detach an existing hook if present.
888 spin_lock(&po->bind_lock);
893 spin_unlock(&po->bind_lock);
894 dev_remove_pack(&po->prot_hook);
895 spin_lock(&po->bind_lock);
899 po->prot_hook.type = protocol;
900 po->prot_hook.dev = dev;
902 po->ifindex = dev ? dev->ifindex : 0;
908 if (dev->flags&IFF_UP) {
909 dev_add_pack(&po->prot_hook);
913 sk->sk_err = ENETDOWN;
914 if (!sock_flag(sk, SOCK_DEAD))
915 sk->sk_error_report(sk);
918 dev_add_pack(&po->prot_hook);
924 spin_unlock(&po->bind_lock);
930 * Bind a packet socket to a device
933 #ifdef CONFIG_SOCK_PACKET
935 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
937 struct sock *sk=sock->sk;
939 struct net_device *dev;
946 if (addr_len != sizeof(struct sockaddr))
948 strlcpy(name,uaddr->sa_data,sizeof(name));
950 dev = dev_get_by_name(name);
952 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
959 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
961 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
962 struct sock *sk=sock->sk;
963 struct net_device *dev = NULL;
971 if (addr_len < sizeof(struct sockaddr_ll))
973 if (sll->sll_family != AF_PACKET)
976 if (sll->sll_ifindex) {
978 dev = dev_get_by_index(sll->sll_ifindex);
982 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
990 static struct proto packet_proto = {
992 .owner = THIS_MODULE,
993 .obj_size = sizeof(struct packet_sock),
997 * Create a packet of type SOCK_PACKET.
1000 static int packet_create(struct socket *sock, int protocol)
1003 struct packet_sock *po;
1004 __be16 proto = (__force __be16)protocol; /* weird, but documented */
1007 if (!capable(CAP_NET_RAW))
1009 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
1010 #ifdef CONFIG_SOCK_PACKET
1011 && sock->type != SOCK_PACKET
1014 return -ESOCKTNOSUPPORT;
1016 sock->state = SS_UNCONNECTED;
1019 sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
1023 sock->ops = &packet_ops;
1024 #ifdef CONFIG_SOCK_PACKET
1025 if (sock->type == SOCK_PACKET)
1026 sock->ops = &packet_ops_spkt;
1028 sock_init_data(sock, sk);
1031 sk->sk_family = PF_PACKET;
1034 sk->sk_destruct = packet_sock_destruct;
1035 atomic_inc(&packet_socks_nr);
1038 * Attach a protocol block
1041 spin_lock_init(&po->bind_lock);
1042 po->prot_hook.func = packet_rcv;
1043 #ifdef CONFIG_SOCK_PACKET
1044 if (sock->type == SOCK_PACKET)
1045 po->prot_hook.func = packet_rcv_spkt;
1047 po->prot_hook.af_packet_priv = sk;
1050 po->prot_hook.type = proto;
1051 dev_add_pack(&po->prot_hook);
1056 write_lock_bh(&packet_sklist_lock);
1057 sk_add_node(sk, &packet_sklist);
1058 write_unlock_bh(&packet_sklist_lock);
1065 * Pull a packet from our receive queue and hand it to the user.
1066 * If necessary we block.
1069 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1070 struct msghdr *msg, size_t len, int flags)
1072 struct sock *sk = sock->sk;
1073 struct sk_buff *skb;
1075 struct sockaddr_ll *sll;
1078 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1082 /* What error should we return now? EUNATTACH? */
1083 if (pkt_sk(sk)->ifindex < 0)
1088 * Call the generic datagram receiver. This handles all sorts
1089 * of horrible races and re-entrancy so we can forget about it
1090 * in the protocol layers.
1092 * Now it will return ENETDOWN, if device have just gone down,
1093 * but then it will block.
1096 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1099 * An error occurred so return it. Because skb_recv_datagram()
1100 * handles the blocking we don't see and worry about blocking
1108 * If the address length field is there to be filled in, we fill
1112 sll = &PACKET_SKB_CB(skb)->sa.ll;
1113 if (sock->type == SOCK_PACKET)
1114 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1116 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1119 * You lose any data beyond the buffer you gave. If it worries a
1120 * user program they can ask the device for its MTU anyway.
1127 msg->msg_flags|=MSG_TRUNC;
1130 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1134 sock_recv_timestamp(msg, sk, skb);
1137 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1140 if (pkt_sk(sk)->auxdata) {
1141 struct tpacket_auxdata aux;
1143 aux.tp_status = TP_STATUS_USER;
1144 if (skb->ip_summed == CHECKSUM_PARTIAL)
1145 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1146 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1147 aux.tp_snaplen = skb->len;
1149 aux.tp_net = skb->nh.raw - skb->data;
1151 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
1155 * Free or return the buffer as appropriate. Again this
1156 * hides all the races and re-entrancy issues from us.
1158 err = (flags&MSG_TRUNC) ? skb->len : copied;
1161 skb_free_datagram(sk, skb);
1166 #ifdef CONFIG_SOCK_PACKET
1167 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1168 int *uaddr_len, int peer)
1170 struct net_device *dev;
1171 struct sock *sk = sock->sk;
1176 uaddr->sa_family = AF_PACKET;
1177 dev = dev_get_by_index(pkt_sk(sk)->ifindex);
1179 strlcpy(uaddr->sa_data, dev->name, 15);
1182 memset(uaddr->sa_data, 0, 14);
1183 *uaddr_len = sizeof(*uaddr);
1189 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1190 int *uaddr_len, int peer)
1192 struct net_device *dev;
1193 struct sock *sk = sock->sk;
1194 struct packet_sock *po = pkt_sk(sk);
1195 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1200 sll->sll_family = AF_PACKET;
1201 sll->sll_ifindex = po->ifindex;
1202 sll->sll_protocol = po->num;
1203 dev = dev_get_by_index(po->ifindex);
1205 sll->sll_hatype = dev->type;
1206 sll->sll_halen = dev->addr_len;
1207 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1210 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1213 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
1218 #ifdef CONFIG_PACKET_MULTICAST
1219 static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1222 case PACKET_MR_MULTICAST:
1224 dev_mc_add(dev, i->addr, i->alen, 0);
1226 dev_mc_delete(dev, i->addr, i->alen, 0);
1228 case PACKET_MR_PROMISC:
1229 dev_set_promiscuity(dev, what);
1231 case PACKET_MR_ALLMULTI:
1232 dev_set_allmulti(dev, what);
1238 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1240 for ( ; i; i=i->next) {
1241 if (i->ifindex == dev->ifindex)
1242 packet_dev_mc(dev, i, what);
1246 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
1248 struct packet_sock *po = pkt_sk(sk);
1249 struct packet_mclist *ml, *i;
1250 struct net_device *dev;
1256 dev = __dev_get_by_index(mreq->mr_ifindex);
1261 if (mreq->mr_alen > dev->addr_len)
1265 i = kmalloc(sizeof(*i), GFP_KERNEL);
1270 for (ml = po->mclist; ml; ml = ml->next) {
1271 if (ml->ifindex == mreq->mr_ifindex &&
1272 ml->type == mreq->mr_type &&
1273 ml->alen == mreq->mr_alen &&
1274 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1276 /* Free the new element ... */
1282 i->type = mreq->mr_type;
1283 i->ifindex = mreq->mr_ifindex;
1284 i->alen = mreq->mr_alen;
1285 memcpy(i->addr, mreq->mr_address, i->alen);
1287 i->next = po->mclist;
1289 packet_dev_mc(dev, i, +1);
1296 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
1298 struct packet_mclist *ml, **mlp;
1302 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1303 if (ml->ifindex == mreq->mr_ifindex &&
1304 ml->type == mreq->mr_type &&
1305 ml->alen == mreq->mr_alen &&
1306 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1307 if (--ml->count == 0) {
1308 struct net_device *dev;
1310 dev = dev_get_by_index(ml->ifindex);
1312 packet_dev_mc(dev, ml, -1);
1322 return -EADDRNOTAVAIL;
1325 static void packet_flush_mclist(struct sock *sk)
1327 struct packet_sock *po = pkt_sk(sk);
1328 struct packet_mclist *ml;
1334 while ((ml = po->mclist) != NULL) {
1335 struct net_device *dev;
1337 po->mclist = ml->next;
1338 if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1339 packet_dev_mc(dev, ml, -1);
1349 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1351 struct sock *sk = sock->sk;
1352 struct packet_sock *po = pkt_sk(sk);
1355 if (level != SOL_PACKET)
1356 return -ENOPROTOOPT;
1359 #ifdef CONFIG_PACKET_MULTICAST
1360 case PACKET_ADD_MEMBERSHIP:
1361 case PACKET_DROP_MEMBERSHIP:
1363 struct packet_mreq_max mreq;
1365 memset(&mreq, 0, sizeof(mreq));
1366 if (len < sizeof(struct packet_mreq))
1368 if (len > sizeof(mreq))
1370 if (copy_from_user(&mreq,optval,len))
1372 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1374 if (optname == PACKET_ADD_MEMBERSHIP)
1375 ret = packet_mc_add(sk, &mreq);
1377 ret = packet_mc_drop(sk, &mreq);
1381 #ifdef CONFIG_PACKET_MMAP
1382 case PACKET_RX_RING:
1384 struct tpacket_req req;
1386 if (optlen<sizeof(req))
1388 if (copy_from_user(&req,optval,sizeof(req)))
1390 return packet_set_ring(sk, &req, 0);
1392 case PACKET_COPY_THRESH:
1396 if (optlen!=sizeof(val))
1398 if (copy_from_user(&val,optval,sizeof(val)))
1401 pkt_sk(sk)->copy_thresh = val;
1405 case PACKET_AUXDATA:
1409 if (optlen < sizeof(val))
1411 if (copy_from_user(&val, optval, sizeof(val)))
1414 po->auxdata = !!val;
1418 return -ENOPROTOOPT;
1422 static int packet_getsockopt(struct socket *sock, int level, int optname,
1423 char __user *optval, int __user *optlen)
1427 struct sock *sk = sock->sk;
1428 struct packet_sock *po = pkt_sk(sk);
1430 struct tpacket_stats st;
1432 if (level != SOL_PACKET)
1433 return -ENOPROTOOPT;
1435 if (get_user(len, optlen))
1442 case PACKET_STATISTICS:
1443 if (len > sizeof(struct tpacket_stats))
1444 len = sizeof(struct tpacket_stats);
1445 spin_lock_bh(&sk->sk_receive_queue.lock);
1447 memset(&po->stats, 0, sizeof(st));
1448 spin_unlock_bh(&sk->sk_receive_queue.lock);
1449 st.tp_packets += st.tp_drops;
1453 case PACKET_AUXDATA:
1454 if (len > sizeof(int))
1461 return -ENOPROTOOPT;
1464 if (put_user(len, optlen))
1466 if (copy_to_user(optval, data, len))
1472 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1475 struct hlist_node *node;
1476 struct net_device *dev = (struct net_device*)data;
1478 read_lock(&packet_sklist_lock);
1479 sk_for_each(sk, node, &packet_sklist) {
1480 struct packet_sock *po = pkt_sk(sk);
1483 case NETDEV_UNREGISTER:
1484 #ifdef CONFIG_PACKET_MULTICAST
1486 packet_dev_mclist(dev, po->mclist, -1);
1490 if (dev->ifindex == po->ifindex) {
1491 spin_lock(&po->bind_lock);
1493 __dev_remove_pack(&po->prot_hook);
1496 sk->sk_err = ENETDOWN;
1497 if (!sock_flag(sk, SOCK_DEAD))
1498 sk->sk_error_report(sk);
1500 if (msg == NETDEV_UNREGISTER) {
1502 po->prot_hook.dev = NULL;
1504 spin_unlock(&po->bind_lock);
1508 spin_lock(&po->bind_lock);
1509 if (dev->ifindex == po->ifindex && po->num &&
1511 dev_add_pack(&po->prot_hook);
1515 spin_unlock(&po->bind_lock);
1519 read_unlock(&packet_sklist_lock);
1524 static int packet_ioctl(struct socket *sock, unsigned int cmd,
1527 struct sock *sk = sock->sk;
1532 int amount = atomic_read(&sk->sk_wmem_alloc);
1533 return put_user(amount, (int __user *)arg);
1537 struct sk_buff *skb;
1540 spin_lock_bh(&sk->sk_receive_queue.lock);
1541 skb = skb_peek(&sk->sk_receive_queue);
1544 spin_unlock_bh(&sk->sk_receive_queue.lock);
1545 return put_user(amount, (int __user *)arg);
1548 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1558 case SIOCGIFBRDADDR:
1559 case SIOCSIFBRDADDR:
1560 case SIOCGIFNETMASK:
1561 case SIOCSIFNETMASK:
1562 case SIOCGIFDSTADDR:
1563 case SIOCSIFDSTADDR:
1565 return inet_dgram_ops.ioctl(sock, cmd, arg);
1569 return -ENOIOCTLCMD;
1574 #ifndef CONFIG_PACKET_MMAP
1575 #define packet_mmap sock_no_mmap
1576 #define packet_poll datagram_poll
1579 static unsigned int packet_poll(struct file * file, struct socket *sock,
1582 struct sock *sk = sock->sk;
1583 struct packet_sock *po = pkt_sk(sk);
1584 unsigned int mask = datagram_poll(file, sock, wait);
1586 spin_lock_bh(&sk->sk_receive_queue.lock);
1588 unsigned last = po->head ? po->head-1 : po->frame_max;
1589 struct tpacket_hdr *h;
1591 h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
1594 mask |= POLLIN | POLLRDNORM;
1596 spin_unlock_bh(&sk->sk_receive_queue.lock);
1601 /* Dirty? Well, I still did not learn better way to account
1605 static void packet_mm_open(struct vm_area_struct *vma)
1607 struct file *file = vma->vm_file;
1608 struct socket * sock = file->private_data;
1609 struct sock *sk = sock->sk;
1612 atomic_inc(&pkt_sk(sk)->mapped);
1615 static void packet_mm_close(struct vm_area_struct *vma)
1617 struct file *file = vma->vm_file;
1618 struct socket * sock = file->private_data;
1619 struct sock *sk = sock->sk;
1622 atomic_dec(&pkt_sk(sk)->mapped);
1625 static struct vm_operations_struct packet_mmap_ops = {
1626 .open = packet_mm_open,
1627 .close =packet_mm_close,
1630 static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
1632 return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
1635 static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
1639 for (i = 0; i < len; i++) {
1640 if (likely(pg_vec[i]))
1641 free_pages((unsigned long) pg_vec[i], order);
1646 static inline char *alloc_one_pg_vec_page(unsigned long order)
1648 return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
1652 static char **alloc_pg_vec(struct tpacket_req *req, int order)
1654 unsigned int block_nr = req->tp_block_nr;
1658 pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
1659 if (unlikely(!pg_vec))
1662 for (i = 0; i < block_nr; i++) {
1663 pg_vec[i] = alloc_one_pg_vec_page(order);
1664 if (unlikely(!pg_vec[i]))
1665 goto out_free_pgvec;
1672 free_pg_vec(pg_vec, order, block_nr);
1677 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1679 char **pg_vec = NULL;
1680 struct packet_sock *po = pkt_sk(sk);
1681 int was_running, order = 0;
1685 if (req->tp_block_nr) {
1688 /* Sanity tests and some calculations */
1690 if (unlikely(po->pg_vec))
1693 if (unlikely((int)req->tp_block_size <= 0))
1695 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
1697 if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
1699 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
1702 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1703 if (unlikely(po->frames_per_block <= 0))
1705 if (unlikely((po->frames_per_block * req->tp_block_nr) !=
1710 order = get_order(req->tp_block_size);
1711 pg_vec = alloc_pg_vec(req, order);
1712 if (unlikely(!pg_vec))
1716 for (i = 0; i < req->tp_block_nr; i++) {
1717 char *ptr = pg_vec[i];
1718 struct tpacket_hdr *header;
1721 for (k = 0; k < po->frames_per_block; k++) {
1722 header = (struct tpacket_hdr *) ptr;
1723 header->tp_status = TP_STATUS_KERNEL;
1724 ptr += req->tp_frame_size;
1729 if (unlikely(req->tp_frame_nr))
1735 /* Detach socket from network */
1736 spin_lock(&po->bind_lock);
1737 was_running = po->running;
1740 __dev_remove_pack(&po->prot_hook);
1745 spin_unlock(&po->bind_lock);
1750 if (closing || atomic_read(&po->mapped) == 0) {
1752 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1754 spin_lock_bh(&sk->sk_receive_queue.lock);
1755 pg_vec = XC(po->pg_vec, pg_vec);
1756 po->frame_max = (req->tp_frame_nr - 1);
1758 po->frame_size = req->tp_frame_size;
1759 spin_unlock_bh(&sk->sk_receive_queue.lock);
1761 order = XC(po->pg_vec_order, order);
1762 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1764 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1765 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1766 skb_queue_purge(&sk->sk_receive_queue);
1768 if (atomic_read(&po->mapped))
1769 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1772 spin_lock(&po->bind_lock);
1773 if (was_running && !po->running) {
1777 dev_add_pack(&po->prot_hook);
1779 spin_unlock(&po->bind_lock);
1784 free_pg_vec(pg_vec, order, req->tp_block_nr);
1789 static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1791 struct sock *sk = sock->sk;
1792 struct packet_sock *po = pkt_sk(sk);
1794 unsigned long start;
1801 size = vma->vm_end - vma->vm_start;
1804 if (po->pg_vec == NULL)
1806 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1809 start = vma->vm_start;
1810 for (i = 0; i < po->pg_vec_len; i++) {
1811 struct page *page = virt_to_page(po->pg_vec[i]);
1814 for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
1815 err = vm_insert_page(vma, start, page);
1821 atomic_inc(&po->mapped);
1822 vma->vm_ops = &packet_mmap_ops;
1832 #ifdef CONFIG_SOCK_PACKET
1833 static const struct proto_ops packet_ops_spkt = {
1834 .family = PF_PACKET,
1835 .owner = THIS_MODULE,
1836 .release = packet_release,
1837 .bind = packet_bind_spkt,
1838 .connect = sock_no_connect,
1839 .socketpair = sock_no_socketpair,
1840 .accept = sock_no_accept,
1841 .getname = packet_getname_spkt,
1842 .poll = datagram_poll,
1843 .ioctl = packet_ioctl,
1844 .listen = sock_no_listen,
1845 .shutdown = sock_no_shutdown,
1846 .setsockopt = sock_no_setsockopt,
1847 .getsockopt = sock_no_getsockopt,
1848 .sendmsg = packet_sendmsg_spkt,
1849 .recvmsg = packet_recvmsg,
1850 .mmap = sock_no_mmap,
1851 .sendpage = sock_no_sendpage,
1855 static const struct proto_ops packet_ops = {
1856 .family = PF_PACKET,
1857 .owner = THIS_MODULE,
1858 .release = packet_release,
1859 .bind = packet_bind,
1860 .connect = sock_no_connect,
1861 .socketpair = sock_no_socketpair,
1862 .accept = sock_no_accept,
1863 .getname = packet_getname,
1864 .poll = packet_poll,
1865 .ioctl = packet_ioctl,
1866 .listen = sock_no_listen,
1867 .shutdown = sock_no_shutdown,
1868 .setsockopt = packet_setsockopt,
1869 .getsockopt = packet_getsockopt,
1870 .sendmsg = packet_sendmsg,
1871 .recvmsg = packet_recvmsg,
1872 .mmap = packet_mmap,
1873 .sendpage = sock_no_sendpage,
1876 static struct net_proto_family packet_family_ops = {
1877 .family = PF_PACKET,
1878 .create = packet_create,
1879 .owner = THIS_MODULE,
1882 static struct notifier_block packet_netdev_notifier = {
1883 .notifier_call =packet_notifier,
1886 #ifdef CONFIG_PROC_FS
1887 static inline struct sock *packet_seq_idx(loff_t off)
1890 struct hlist_node *node;
1892 sk_for_each(s, node, &packet_sklist) {
1899 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1901 read_lock(&packet_sklist_lock);
1902 return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1905 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1908 return (v == SEQ_START_TOKEN)
1909 ? sk_head(&packet_sklist)
1910 : sk_next((struct sock*)v) ;
1913 static void packet_seq_stop(struct seq_file *seq, void *v)
1915 read_unlock(&packet_sklist_lock);
1918 static int packet_seq_show(struct seq_file *seq, void *v)
1920 if (v == SEQ_START_TOKEN)
1921 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1924 const struct packet_sock *po = pkt_sk(s);
1927 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1929 atomic_read(&s->sk_refcnt),
1934 atomic_read(&s->sk_rmem_alloc),
1942 static struct seq_operations packet_seq_ops = {
1943 .start = packet_seq_start,
1944 .next = packet_seq_next,
1945 .stop = packet_seq_stop,
1946 .show = packet_seq_show,
1949 static int packet_seq_open(struct inode *inode, struct file *file)
1951 return seq_open(file, &packet_seq_ops);
1954 static const struct file_operations packet_seq_fops = {
1955 .owner = THIS_MODULE,
1956 .open = packet_seq_open,
1958 .llseek = seq_lseek,
1959 .release = seq_release,
1964 static void __exit packet_exit(void)
1966 proc_net_remove("packet");
1967 unregister_netdevice_notifier(&packet_netdev_notifier);
1968 sock_unregister(PF_PACKET);
1969 proto_unregister(&packet_proto);
1972 static int __init packet_init(void)
1974 int rc = proto_register(&packet_proto, 0);
1979 sock_register(&packet_family_ops);
1980 register_netdevice_notifier(&packet_netdev_notifier);
1981 proc_net_fops_create("packet", 0, &packet_seq_fops);
1986 module_init(packet_init);
1987 module_exit(packet_exit);
1988 MODULE_LICENSE("GPL");
1989 MODULE_ALIAS_NETPROTO(PF_PACKET);