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 struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position)
232 unsigned int pg_vec_pos, frame_offset;
234 pg_vec_pos = position / po->frames_per_block;
235 frame_offset = position % po->frames_per_block;
237 return (struct tpacket_hdr *)(po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size));
241 static inline struct packet_sock *pkt_sk(struct sock *sk)
243 return (struct packet_sock *)sk;
246 static void packet_sock_destruct(struct sock *sk)
248 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
249 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
251 if (!sock_flag(sk, SOCK_DEAD)) {
252 printk("Attempt to release alive packet socket: %p\n", sk);
256 atomic_dec(&packet_socks_nr);
257 #ifdef PACKET_REFCNT_DEBUG
258 printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
263 static const struct proto_ops packet_ops;
265 #ifdef CONFIG_SOCK_PACKET
266 static const struct proto_ops packet_ops_spkt;
268 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
271 struct sockaddr_pkt *spkt;
274 * When we registered the protocol we saved the socket in the data
275 * field for just this event.
278 sk = pt->af_packet_priv;
281 * Yank back the headers [hope the device set this
282 * right or kerboom...]
284 * Incoming packets have ll header pulled,
287 * For outgoing ones skb->data == skb_mac_header(skb)
288 * so that this procedure is noop.
291 if (skb->pkt_type == PACKET_LOOPBACK)
294 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
297 /* drop any routing info */
298 dst_release(skb->dst);
301 /* drop conntrack reference */
304 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
306 skb_push(skb, skb->data - skb_mac_header(skb));
309 * The SOCK_PACKET socket receives _all_ frames.
312 spkt->spkt_family = dev->type;
313 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
314 spkt->spkt_protocol = skb->protocol;
317 * Charge the memory to the socket. This is done specifically
318 * to prevent sockets using all the memory up.
321 if (sock_queue_rcv_skb(sk,skb) == 0)
332 * Output a raw packet to a device layer. This bypasses all the other
333 * protocol layers and you must therefore supply it with a complete frame
336 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
337 struct msghdr *msg, size_t len)
339 struct sock *sk = sock->sk;
340 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
342 struct net_device *dev;
347 * Get and verify the address.
352 if (msg->msg_namelen < sizeof(struct sockaddr))
354 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
355 proto=saddr->spkt_protocol;
358 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
361 * Find the device first to size check it
364 saddr->spkt_device[13] = 0;
365 dev = dev_get_by_name(saddr->spkt_device);
371 if (!(dev->flags & IFF_UP))
375 * You may not queue a frame bigger than the mtu. This is the lowest level
376 * raw protocol and you must do your own fragmentation at this level.
380 if (len > dev->mtu + dev->hard_header_len)
384 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
387 * If the write buffer is full, then tough. At this level the user gets to
388 * deal with the problem - do your own algorithmic backoffs. That's far
399 /* FIXME: Save some space for broken drivers that write a
400 * hard header at transmission time by themselves. PPP is the
401 * notable one here. This should really be fixed at the driver level.
403 skb_reserve(skb, LL_RESERVED_SPACE(dev));
404 skb_reset_network_header(skb);
406 /* Try to align data part correctly */
407 if (dev->hard_header) {
408 skb->data -= dev->hard_header_len;
409 skb->tail -= dev->hard_header_len;
410 if (len < dev->hard_header_len)
411 skb_reset_network_header(skb);
414 /* Returns -EFAULT on error */
415 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
416 skb->protocol = proto;
418 skb->priority = sk->sk_priority;
439 static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
442 struct sk_filter *filter;
445 filter = rcu_dereference(sk->sk_filter);
447 res = sk_run_filter(skb, filter->insns, filter->len);
448 rcu_read_unlock_bh();
454 This function makes lazy skb cloning in hope that most of packets
455 are discarded by BPF.
457 Note tricky part: we DO mangle shared skb! skb->data, skb->len
458 and skb->cb are mangled. It works because (and until) packets
459 falling here are owned by current CPU. Output packets are cloned
460 by dev_queue_xmit_nit(), input packets are processed by net_bh
461 sequencially, so that if we return skb to original state on exit,
462 we will not harm anyone.
465 static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
468 struct sockaddr_ll *sll;
469 struct packet_sock *po;
470 u8 * skb_head = skb->data;
471 int skb_len = skb->len;
472 unsigned int snaplen, res;
474 if (skb->pkt_type == PACKET_LOOPBACK)
477 sk = pt->af_packet_priv;
482 if (dev->hard_header) {
483 /* The device has an explicit notion of ll header,
484 exported to higher levels.
486 Otherwise, the device hides datails of it frame
487 structure, so that corresponding packet head
488 never delivered to user.
490 if (sk->sk_type != SOCK_DGRAM)
491 skb_push(skb, skb->data - skb_mac_header(skb));
492 else if (skb->pkt_type == PACKET_OUTGOING) {
493 /* Special case: outgoing packets have ll header at head */
494 skb_pull(skb, skb->nh.raw - skb->data);
500 res = run_filter(skb, sk, snaplen);
506 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
507 (unsigned)sk->sk_rcvbuf)
510 if (skb_shared(skb)) {
511 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
515 if (skb_head != skb->data) {
516 skb->data = skb_head;
523 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
526 sll = &PACKET_SKB_CB(skb)->sa.ll;
527 sll->sll_family = AF_PACKET;
528 sll->sll_hatype = dev->type;
529 sll->sll_protocol = skb->protocol;
530 sll->sll_pkttype = skb->pkt_type;
531 sll->sll_ifindex = dev->ifindex;
534 if (dev->hard_header_parse)
535 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
537 PACKET_SKB_CB(skb)->origlen = skb->len;
539 if (pskb_trim(skb, snaplen))
542 skb_set_owner_r(skb, sk);
544 dst_release(skb->dst);
547 /* drop conntrack reference */
550 spin_lock(&sk->sk_receive_queue.lock);
551 po->stats.tp_packets++;
552 __skb_queue_tail(&sk->sk_receive_queue, skb);
553 spin_unlock(&sk->sk_receive_queue.lock);
554 sk->sk_data_ready(sk, skb->len);
558 spin_lock(&sk->sk_receive_queue.lock);
559 po->stats.tp_drops++;
560 spin_unlock(&sk->sk_receive_queue.lock);
563 if (skb_head != skb->data && skb_shared(skb)) {
564 skb->data = skb_head;
572 #ifdef CONFIG_PACKET_MMAP
573 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
576 struct packet_sock *po;
577 struct sockaddr_ll *sll;
578 struct tpacket_hdr *h;
579 u8 * skb_head = skb->data;
580 int skb_len = skb->len;
581 unsigned int snaplen, res;
582 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
583 unsigned short macoff, netoff;
584 struct sk_buff *copy_skb = NULL;
587 if (skb->pkt_type == PACKET_LOOPBACK)
590 sk = pt->af_packet_priv;
593 if (dev->hard_header) {
594 if (sk->sk_type != SOCK_DGRAM)
595 skb_push(skb, skb->data - skb_mac_header(skb));
596 else if (skb->pkt_type == PACKET_OUTGOING) {
597 /* Special case: outgoing packets have ll header at head */
598 skb_pull(skb, skb->nh.raw - skb->data);
602 if (skb->ip_summed == CHECKSUM_PARTIAL)
603 status |= TP_STATUS_CSUMNOTREADY;
607 res = run_filter(skb, sk, snaplen);
613 if (sk->sk_type == SOCK_DGRAM) {
614 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
616 unsigned maclen = skb->nh.raw - skb->data;
617 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
618 macoff = netoff - maclen;
621 if (macoff + snaplen > po->frame_size) {
622 if (po->copy_thresh &&
623 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
624 (unsigned)sk->sk_rcvbuf) {
625 if (skb_shared(skb)) {
626 copy_skb = skb_clone(skb, GFP_ATOMIC);
628 copy_skb = skb_get(skb);
629 skb_head = skb->data;
632 skb_set_owner_r(copy_skb, sk);
634 snaplen = po->frame_size - macoff;
635 if ((int)snaplen < 0)
639 spin_lock(&sk->sk_receive_queue.lock);
640 h = packet_lookup_frame(po, po->head);
644 po->head = po->head != po->frame_max ? po->head+1 : 0;
645 po->stats.tp_packets++;
647 status |= TP_STATUS_COPY;
648 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
650 if (!po->stats.tp_drops)
651 status &= ~TP_STATUS_LOSING;
652 spin_unlock(&sk->sk_receive_queue.lock);
654 skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
656 h->tp_len = skb->len;
657 h->tp_snaplen = snaplen;
660 if (skb->tstamp.tv64 == 0) {
661 __net_timestamp(skb);
662 sock_enable_timestamp(sk);
664 tv = ktime_to_timeval(skb->tstamp);
665 h->tp_sec = tv.tv_sec;
666 h->tp_usec = tv.tv_usec;
668 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
670 if (dev->hard_header_parse)
671 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
672 sll->sll_family = AF_PACKET;
673 sll->sll_hatype = dev->type;
674 sll->sll_protocol = skb->protocol;
675 sll->sll_pkttype = skb->pkt_type;
676 sll->sll_ifindex = dev->ifindex;
678 h->tp_status = status;
682 struct page *p_start, *p_end;
683 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
685 p_start = virt_to_page(h);
686 p_end = virt_to_page(h_end);
687 while (p_start <= p_end) {
688 flush_dcache_page(p_start);
693 sk->sk_data_ready(sk, 0);
696 if (skb_head != skb->data && skb_shared(skb)) {
697 skb->data = skb_head;
705 po->stats.tp_drops++;
706 spin_unlock(&sk->sk_receive_queue.lock);
708 sk->sk_data_ready(sk, 0);
717 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
718 struct msghdr *msg, size_t len)
720 struct sock *sk = sock->sk;
721 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
723 struct net_device *dev;
726 int ifindex, err, reserve = 0;
729 * Get and verify the address.
733 struct packet_sock *po = pkt_sk(sk);
735 ifindex = po->ifindex;
740 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
742 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
744 ifindex = saddr->sll_ifindex;
745 proto = saddr->sll_protocol;
746 addr = saddr->sll_addr;
750 dev = dev_get_by_index(ifindex);
754 if (sock->type == SOCK_RAW)
755 reserve = dev->hard_header_len;
758 if (!(dev->flags & IFF_UP))
762 if (len > dev->mtu+reserve)
765 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
766 msg->msg_flags & MSG_DONTWAIT, &err);
770 skb_reserve(skb, LL_RESERVED_SPACE(dev));
771 skb_reset_network_header(skb);
773 if (dev->hard_header) {
776 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
777 if (sock->type != SOCK_DGRAM) {
778 skb->tail = skb->data;
784 /* Returns -EFAULT on error */
785 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
789 skb->protocol = proto;
791 skb->priority = sk->sk_priority;
797 err = dev_queue_xmit(skb);
798 if (err > 0 && (err = net_xmit_errno(err)) != 0)
815 * Close a PACKET socket. This is fairly simple. We immediately go
816 * to 'closed' state and remove our protocol entry in the device list.
819 static int packet_release(struct socket *sock)
821 struct sock *sk = sock->sk;
822 struct packet_sock *po;
829 write_lock_bh(&packet_sklist_lock);
830 sk_del_node_init(sk);
831 write_unlock_bh(&packet_sklist_lock);
834 * Unhook packet receive handler.
839 * Remove the protocol hook
841 dev_remove_pack(&po->prot_hook);
847 #ifdef CONFIG_PACKET_MULTICAST
848 packet_flush_mclist(sk);
851 #ifdef CONFIG_PACKET_MMAP
853 struct tpacket_req req;
854 memset(&req, 0, sizeof(req));
855 packet_set_ring(sk, &req, 1);
860 * Now the socket is dead. No more input will appear.
868 skb_queue_purge(&sk->sk_receive_queue);
875 * Attach a packet hook.
878 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
880 struct packet_sock *po = pkt_sk(sk);
882 * Detach an existing hook if present.
887 spin_lock(&po->bind_lock);
892 spin_unlock(&po->bind_lock);
893 dev_remove_pack(&po->prot_hook);
894 spin_lock(&po->bind_lock);
898 po->prot_hook.type = protocol;
899 po->prot_hook.dev = dev;
901 po->ifindex = dev ? dev->ifindex : 0;
907 if (dev->flags&IFF_UP) {
908 dev_add_pack(&po->prot_hook);
912 sk->sk_err = ENETDOWN;
913 if (!sock_flag(sk, SOCK_DEAD))
914 sk->sk_error_report(sk);
917 dev_add_pack(&po->prot_hook);
923 spin_unlock(&po->bind_lock);
929 * Bind a packet socket to a device
932 #ifdef CONFIG_SOCK_PACKET
934 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
936 struct sock *sk=sock->sk;
938 struct net_device *dev;
945 if (addr_len != sizeof(struct sockaddr))
947 strlcpy(name,uaddr->sa_data,sizeof(name));
949 dev = dev_get_by_name(name);
951 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
958 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
960 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
961 struct sock *sk=sock->sk;
962 struct net_device *dev = NULL;
970 if (addr_len < sizeof(struct sockaddr_ll))
972 if (sll->sll_family != AF_PACKET)
975 if (sll->sll_ifindex) {
977 dev = dev_get_by_index(sll->sll_ifindex);
981 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
989 static struct proto packet_proto = {
991 .owner = THIS_MODULE,
992 .obj_size = sizeof(struct packet_sock),
996 * Create a packet of type SOCK_PACKET.
999 static int packet_create(struct socket *sock, int protocol)
1002 struct packet_sock *po;
1003 __be16 proto = (__force __be16)protocol; /* weird, but documented */
1006 if (!capable(CAP_NET_RAW))
1008 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
1009 #ifdef CONFIG_SOCK_PACKET
1010 && sock->type != SOCK_PACKET
1013 return -ESOCKTNOSUPPORT;
1015 sock->state = SS_UNCONNECTED;
1018 sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
1022 sock->ops = &packet_ops;
1023 #ifdef CONFIG_SOCK_PACKET
1024 if (sock->type == SOCK_PACKET)
1025 sock->ops = &packet_ops_spkt;
1027 sock_init_data(sock, sk);
1030 sk->sk_family = PF_PACKET;
1033 sk->sk_destruct = packet_sock_destruct;
1034 atomic_inc(&packet_socks_nr);
1037 * Attach a protocol block
1040 spin_lock_init(&po->bind_lock);
1041 po->prot_hook.func = packet_rcv;
1042 #ifdef CONFIG_SOCK_PACKET
1043 if (sock->type == SOCK_PACKET)
1044 po->prot_hook.func = packet_rcv_spkt;
1046 po->prot_hook.af_packet_priv = sk;
1049 po->prot_hook.type = proto;
1050 dev_add_pack(&po->prot_hook);
1055 write_lock_bh(&packet_sklist_lock);
1056 sk_add_node(sk, &packet_sklist);
1057 write_unlock_bh(&packet_sklist_lock);
1064 * Pull a packet from our receive queue and hand it to the user.
1065 * If necessary we block.
1068 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1069 struct msghdr *msg, size_t len, int flags)
1071 struct sock *sk = sock->sk;
1072 struct sk_buff *skb;
1074 struct sockaddr_ll *sll;
1077 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1081 /* What error should we return now? EUNATTACH? */
1082 if (pkt_sk(sk)->ifindex < 0)
1087 * Call the generic datagram receiver. This handles all sorts
1088 * of horrible races and re-entrancy so we can forget about it
1089 * in the protocol layers.
1091 * Now it will return ENETDOWN, if device have just gone down,
1092 * but then it will block.
1095 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1098 * An error occurred so return it. Because skb_recv_datagram()
1099 * handles the blocking we don't see and worry about blocking
1107 * If the address length field is there to be filled in, we fill
1111 sll = &PACKET_SKB_CB(skb)->sa.ll;
1112 if (sock->type == SOCK_PACKET)
1113 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1115 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1118 * You lose any data beyond the buffer you gave. If it worries a
1119 * user program they can ask the device for its MTU anyway.
1126 msg->msg_flags|=MSG_TRUNC;
1129 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1133 sock_recv_timestamp(msg, sk, skb);
1136 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1139 if (pkt_sk(sk)->auxdata) {
1140 struct tpacket_auxdata aux;
1142 aux.tp_status = TP_STATUS_USER;
1143 if (skb->ip_summed == CHECKSUM_PARTIAL)
1144 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1145 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1146 aux.tp_snaplen = skb->len;
1148 aux.tp_net = skb->nh.raw - skb->data;
1150 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
1154 * Free or return the buffer as appropriate. Again this
1155 * hides all the races and re-entrancy issues from us.
1157 err = (flags&MSG_TRUNC) ? skb->len : copied;
1160 skb_free_datagram(sk, skb);
1165 #ifdef CONFIG_SOCK_PACKET
1166 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1167 int *uaddr_len, int peer)
1169 struct net_device *dev;
1170 struct sock *sk = sock->sk;
1175 uaddr->sa_family = AF_PACKET;
1176 dev = dev_get_by_index(pkt_sk(sk)->ifindex);
1178 strlcpy(uaddr->sa_data, dev->name, 15);
1181 memset(uaddr->sa_data, 0, 14);
1182 *uaddr_len = sizeof(*uaddr);
1188 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1189 int *uaddr_len, int peer)
1191 struct net_device *dev;
1192 struct sock *sk = sock->sk;
1193 struct packet_sock *po = pkt_sk(sk);
1194 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1199 sll->sll_family = AF_PACKET;
1200 sll->sll_ifindex = po->ifindex;
1201 sll->sll_protocol = po->num;
1202 dev = dev_get_by_index(po->ifindex);
1204 sll->sll_hatype = dev->type;
1205 sll->sll_halen = dev->addr_len;
1206 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1209 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1212 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
1217 #ifdef CONFIG_PACKET_MULTICAST
1218 static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1221 case PACKET_MR_MULTICAST:
1223 dev_mc_add(dev, i->addr, i->alen, 0);
1225 dev_mc_delete(dev, i->addr, i->alen, 0);
1227 case PACKET_MR_PROMISC:
1228 dev_set_promiscuity(dev, what);
1230 case PACKET_MR_ALLMULTI:
1231 dev_set_allmulti(dev, what);
1237 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1239 for ( ; i; i=i->next) {
1240 if (i->ifindex == dev->ifindex)
1241 packet_dev_mc(dev, i, what);
1245 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
1247 struct packet_sock *po = pkt_sk(sk);
1248 struct packet_mclist *ml, *i;
1249 struct net_device *dev;
1255 dev = __dev_get_by_index(mreq->mr_ifindex);
1260 if (mreq->mr_alen > dev->addr_len)
1264 i = kmalloc(sizeof(*i), GFP_KERNEL);
1269 for (ml = po->mclist; ml; ml = ml->next) {
1270 if (ml->ifindex == mreq->mr_ifindex &&
1271 ml->type == mreq->mr_type &&
1272 ml->alen == mreq->mr_alen &&
1273 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1275 /* Free the new element ... */
1281 i->type = mreq->mr_type;
1282 i->ifindex = mreq->mr_ifindex;
1283 i->alen = mreq->mr_alen;
1284 memcpy(i->addr, mreq->mr_address, i->alen);
1286 i->next = po->mclist;
1288 packet_dev_mc(dev, i, +1);
1295 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
1297 struct packet_mclist *ml, **mlp;
1301 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1302 if (ml->ifindex == mreq->mr_ifindex &&
1303 ml->type == mreq->mr_type &&
1304 ml->alen == mreq->mr_alen &&
1305 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1306 if (--ml->count == 0) {
1307 struct net_device *dev;
1309 dev = dev_get_by_index(ml->ifindex);
1311 packet_dev_mc(dev, ml, -1);
1321 return -EADDRNOTAVAIL;
1324 static void packet_flush_mclist(struct sock *sk)
1326 struct packet_sock *po = pkt_sk(sk);
1327 struct packet_mclist *ml;
1333 while ((ml = po->mclist) != NULL) {
1334 struct net_device *dev;
1336 po->mclist = ml->next;
1337 if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1338 packet_dev_mc(dev, ml, -1);
1348 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1350 struct sock *sk = sock->sk;
1351 struct packet_sock *po = pkt_sk(sk);
1354 if (level != SOL_PACKET)
1355 return -ENOPROTOOPT;
1358 #ifdef CONFIG_PACKET_MULTICAST
1359 case PACKET_ADD_MEMBERSHIP:
1360 case PACKET_DROP_MEMBERSHIP:
1362 struct packet_mreq_max mreq;
1364 memset(&mreq, 0, sizeof(mreq));
1365 if (len < sizeof(struct packet_mreq))
1367 if (len > sizeof(mreq))
1369 if (copy_from_user(&mreq,optval,len))
1371 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1373 if (optname == PACKET_ADD_MEMBERSHIP)
1374 ret = packet_mc_add(sk, &mreq);
1376 ret = packet_mc_drop(sk, &mreq);
1380 #ifdef CONFIG_PACKET_MMAP
1381 case PACKET_RX_RING:
1383 struct tpacket_req req;
1385 if (optlen<sizeof(req))
1387 if (copy_from_user(&req,optval,sizeof(req)))
1389 return packet_set_ring(sk, &req, 0);
1391 case PACKET_COPY_THRESH:
1395 if (optlen!=sizeof(val))
1397 if (copy_from_user(&val,optval,sizeof(val)))
1400 pkt_sk(sk)->copy_thresh = val;
1404 case PACKET_AUXDATA:
1408 if (optlen < sizeof(val))
1410 if (copy_from_user(&val, optval, sizeof(val)))
1413 po->auxdata = !!val;
1417 return -ENOPROTOOPT;
1421 static int packet_getsockopt(struct socket *sock, int level, int optname,
1422 char __user *optval, int __user *optlen)
1426 struct sock *sk = sock->sk;
1427 struct packet_sock *po = pkt_sk(sk);
1429 struct tpacket_stats st;
1431 if (level != SOL_PACKET)
1432 return -ENOPROTOOPT;
1434 if (get_user(len, optlen))
1441 case PACKET_STATISTICS:
1442 if (len > sizeof(struct tpacket_stats))
1443 len = sizeof(struct tpacket_stats);
1444 spin_lock_bh(&sk->sk_receive_queue.lock);
1446 memset(&po->stats, 0, sizeof(st));
1447 spin_unlock_bh(&sk->sk_receive_queue.lock);
1448 st.tp_packets += st.tp_drops;
1452 case PACKET_AUXDATA:
1453 if (len > sizeof(int))
1460 return -ENOPROTOOPT;
1463 if (put_user(len, optlen))
1465 if (copy_to_user(optval, data, len))
1471 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1474 struct hlist_node *node;
1475 struct net_device *dev = data;
1477 read_lock(&packet_sklist_lock);
1478 sk_for_each(sk, node, &packet_sklist) {
1479 struct packet_sock *po = pkt_sk(sk);
1482 case NETDEV_UNREGISTER:
1483 #ifdef CONFIG_PACKET_MULTICAST
1485 packet_dev_mclist(dev, po->mclist, -1);
1489 if (dev->ifindex == po->ifindex) {
1490 spin_lock(&po->bind_lock);
1492 __dev_remove_pack(&po->prot_hook);
1495 sk->sk_err = ENETDOWN;
1496 if (!sock_flag(sk, SOCK_DEAD))
1497 sk->sk_error_report(sk);
1499 if (msg == NETDEV_UNREGISTER) {
1501 po->prot_hook.dev = NULL;
1503 spin_unlock(&po->bind_lock);
1507 spin_lock(&po->bind_lock);
1508 if (dev->ifindex == po->ifindex && po->num &&
1510 dev_add_pack(&po->prot_hook);
1514 spin_unlock(&po->bind_lock);
1518 read_unlock(&packet_sklist_lock);
1523 static int packet_ioctl(struct socket *sock, unsigned int cmd,
1526 struct sock *sk = sock->sk;
1531 int amount = atomic_read(&sk->sk_wmem_alloc);
1532 return put_user(amount, (int __user *)arg);
1536 struct sk_buff *skb;
1539 spin_lock_bh(&sk->sk_receive_queue.lock);
1540 skb = skb_peek(&sk->sk_receive_queue);
1543 spin_unlock_bh(&sk->sk_receive_queue.lock);
1544 return put_user(amount, (int __user *)arg);
1547 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1549 return sock_get_timestampns(sk, (struct timespec __user *)arg);
1559 case SIOCGIFBRDADDR:
1560 case SIOCSIFBRDADDR:
1561 case SIOCGIFNETMASK:
1562 case SIOCSIFNETMASK:
1563 case SIOCGIFDSTADDR:
1564 case SIOCSIFDSTADDR:
1566 return inet_dgram_ops.ioctl(sock, cmd, arg);
1570 return -ENOIOCTLCMD;
1575 #ifndef CONFIG_PACKET_MMAP
1576 #define packet_mmap sock_no_mmap
1577 #define packet_poll datagram_poll
1580 static unsigned int packet_poll(struct file * file, struct socket *sock,
1583 struct sock *sk = sock->sk;
1584 struct packet_sock *po = pkt_sk(sk);
1585 unsigned int mask = datagram_poll(file, sock, wait);
1587 spin_lock_bh(&sk->sk_receive_queue.lock);
1589 unsigned last = po->head ? po->head-1 : po->frame_max;
1590 struct tpacket_hdr *h;
1592 h = packet_lookup_frame(po, last);
1595 mask |= POLLIN | POLLRDNORM;
1597 spin_unlock_bh(&sk->sk_receive_queue.lock);
1602 /* Dirty? Well, I still did not learn better way to account
1606 static void packet_mm_open(struct vm_area_struct *vma)
1608 struct file *file = vma->vm_file;
1609 struct socket * sock = file->private_data;
1610 struct sock *sk = sock->sk;
1613 atomic_inc(&pkt_sk(sk)->mapped);
1616 static void packet_mm_close(struct vm_area_struct *vma)
1618 struct file *file = vma->vm_file;
1619 struct socket * sock = file->private_data;
1620 struct sock *sk = sock->sk;
1623 atomic_dec(&pkt_sk(sk)->mapped);
1626 static struct vm_operations_struct packet_mmap_ops = {
1627 .open = packet_mm_open,
1628 .close =packet_mm_close,
1631 static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
1633 return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
1636 static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
1640 for (i = 0; i < len; i++) {
1641 if (likely(pg_vec[i]))
1642 free_pages((unsigned long) pg_vec[i], order);
1647 static inline char *alloc_one_pg_vec_page(unsigned long order)
1649 return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
1653 static char **alloc_pg_vec(struct tpacket_req *req, int order)
1655 unsigned int block_nr = req->tp_block_nr;
1659 pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
1660 if (unlikely(!pg_vec))
1663 for (i = 0; i < block_nr; i++) {
1664 pg_vec[i] = alloc_one_pg_vec_page(order);
1665 if (unlikely(!pg_vec[i]))
1666 goto out_free_pgvec;
1673 free_pg_vec(pg_vec, order, block_nr);
1678 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1680 char **pg_vec = NULL;
1681 struct packet_sock *po = pkt_sk(sk);
1682 int was_running, order = 0;
1686 if (req->tp_block_nr) {
1689 /* Sanity tests and some calculations */
1691 if (unlikely(po->pg_vec))
1694 if (unlikely((int)req->tp_block_size <= 0))
1696 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
1698 if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
1700 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
1703 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1704 if (unlikely(po->frames_per_block <= 0))
1706 if (unlikely((po->frames_per_block * req->tp_block_nr) !=
1711 order = get_order(req->tp_block_size);
1712 pg_vec = alloc_pg_vec(req, order);
1713 if (unlikely(!pg_vec))
1717 for (i = 0; i < req->tp_block_nr; i++) {
1718 char *ptr = pg_vec[i];
1719 struct tpacket_hdr *header;
1722 for (k = 0; k < po->frames_per_block; k++) {
1723 header = (struct tpacket_hdr *) ptr;
1724 header->tp_status = TP_STATUS_KERNEL;
1725 ptr += req->tp_frame_size;
1730 if (unlikely(req->tp_frame_nr))
1736 /* Detach socket from network */
1737 spin_lock(&po->bind_lock);
1738 was_running = po->running;
1741 __dev_remove_pack(&po->prot_hook);
1746 spin_unlock(&po->bind_lock);
1751 if (closing || atomic_read(&po->mapped) == 0) {
1753 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1755 spin_lock_bh(&sk->sk_receive_queue.lock);
1756 pg_vec = XC(po->pg_vec, pg_vec);
1757 po->frame_max = (req->tp_frame_nr - 1);
1759 po->frame_size = req->tp_frame_size;
1760 spin_unlock_bh(&sk->sk_receive_queue.lock);
1762 order = XC(po->pg_vec_order, order);
1763 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1765 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1766 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1767 skb_queue_purge(&sk->sk_receive_queue);
1769 if (atomic_read(&po->mapped))
1770 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1773 spin_lock(&po->bind_lock);
1774 if (was_running && !po->running) {
1778 dev_add_pack(&po->prot_hook);
1780 spin_unlock(&po->bind_lock);
1785 free_pg_vec(pg_vec, order, req->tp_block_nr);
1790 static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1792 struct sock *sk = sock->sk;
1793 struct packet_sock *po = pkt_sk(sk);
1795 unsigned long start;
1802 size = vma->vm_end - vma->vm_start;
1805 if (po->pg_vec == NULL)
1807 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1810 start = vma->vm_start;
1811 for (i = 0; i < po->pg_vec_len; i++) {
1812 struct page *page = virt_to_page(po->pg_vec[i]);
1815 for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
1816 err = vm_insert_page(vma, start, page);
1822 atomic_inc(&po->mapped);
1823 vma->vm_ops = &packet_mmap_ops;
1833 #ifdef CONFIG_SOCK_PACKET
1834 static const struct proto_ops packet_ops_spkt = {
1835 .family = PF_PACKET,
1836 .owner = THIS_MODULE,
1837 .release = packet_release,
1838 .bind = packet_bind_spkt,
1839 .connect = sock_no_connect,
1840 .socketpair = sock_no_socketpair,
1841 .accept = sock_no_accept,
1842 .getname = packet_getname_spkt,
1843 .poll = datagram_poll,
1844 .ioctl = packet_ioctl,
1845 .listen = sock_no_listen,
1846 .shutdown = sock_no_shutdown,
1847 .setsockopt = sock_no_setsockopt,
1848 .getsockopt = sock_no_getsockopt,
1849 .sendmsg = packet_sendmsg_spkt,
1850 .recvmsg = packet_recvmsg,
1851 .mmap = sock_no_mmap,
1852 .sendpage = sock_no_sendpage,
1856 static const struct proto_ops packet_ops = {
1857 .family = PF_PACKET,
1858 .owner = THIS_MODULE,
1859 .release = packet_release,
1860 .bind = packet_bind,
1861 .connect = sock_no_connect,
1862 .socketpair = sock_no_socketpair,
1863 .accept = sock_no_accept,
1864 .getname = packet_getname,
1865 .poll = packet_poll,
1866 .ioctl = packet_ioctl,
1867 .listen = sock_no_listen,
1868 .shutdown = sock_no_shutdown,
1869 .setsockopt = packet_setsockopt,
1870 .getsockopt = packet_getsockopt,
1871 .sendmsg = packet_sendmsg,
1872 .recvmsg = packet_recvmsg,
1873 .mmap = packet_mmap,
1874 .sendpage = sock_no_sendpage,
1877 static struct net_proto_family packet_family_ops = {
1878 .family = PF_PACKET,
1879 .create = packet_create,
1880 .owner = THIS_MODULE,
1883 static struct notifier_block packet_netdev_notifier = {
1884 .notifier_call =packet_notifier,
1887 #ifdef CONFIG_PROC_FS
1888 static inline struct sock *packet_seq_idx(loff_t off)
1891 struct hlist_node *node;
1893 sk_for_each(s, node, &packet_sklist) {
1900 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1902 read_lock(&packet_sklist_lock);
1903 return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1906 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1909 return (v == SEQ_START_TOKEN)
1910 ? sk_head(&packet_sklist)
1911 : sk_next((struct sock*)v) ;
1914 static void packet_seq_stop(struct seq_file *seq, void *v)
1916 read_unlock(&packet_sklist_lock);
1919 static int packet_seq_show(struct seq_file *seq, void *v)
1921 if (v == SEQ_START_TOKEN)
1922 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1925 const struct packet_sock *po = pkt_sk(s);
1928 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1930 atomic_read(&s->sk_refcnt),
1935 atomic_read(&s->sk_rmem_alloc),
1943 static struct seq_operations packet_seq_ops = {
1944 .start = packet_seq_start,
1945 .next = packet_seq_next,
1946 .stop = packet_seq_stop,
1947 .show = packet_seq_show,
1950 static int packet_seq_open(struct inode *inode, struct file *file)
1952 return seq_open(file, &packet_seq_ops);
1955 static const struct file_operations packet_seq_fops = {
1956 .owner = THIS_MODULE,
1957 .open = packet_seq_open,
1959 .llseek = seq_lseek,
1960 .release = seq_release,
1965 static void __exit packet_exit(void)
1967 proc_net_remove("packet");
1968 unregister_netdevice_notifier(&packet_netdev_notifier);
1969 sock_unregister(PF_PACKET);
1970 proto_unregister(&packet_proto);
1973 static int __init packet_init(void)
1975 int rc = proto_register(&packet_proto, 0);
1980 sock_register(&packet_family_ops);
1981 register_netdevice_notifier(&packet_netdev_notifier);
1982 proc_net_fops_create("packet", 0, &packet_seq_fops);
1987 module_init(packet_init);
1988 module_exit(packet_exit);
1989 MODULE_LICENSE("GPL");
1990 MODULE_ALIAS_NETPROTO(PF_PACKET);