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>
53 #include <linux/sched.h>
55 #include <linux/capability.h>
56 #include <linux/fcntl.h>
57 #include <linux/socket.h>
59 #include <linux/inet.h>
60 #include <linux/netdevice.h>
61 #include <linux/if_packet.h>
62 #include <linux/wireless.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 char running; /* prot_hook is attached*/
204 int ifindex; /* bound device */
206 #ifdef CONFIG_PACKET_MULTICAST
207 struct packet_mclist *mclist;
209 #ifdef CONFIG_PACKET_MMAP
211 unsigned int pg_vec_order;
212 unsigned int pg_vec_pages;
213 unsigned int pg_vec_len;
217 #ifdef CONFIG_PACKET_MMAP
219 static inline char *packet_lookup_frame(struct packet_sock *po, unsigned int position)
221 unsigned int pg_vec_pos, frame_offset;
224 pg_vec_pos = position / po->frames_per_block;
225 frame_offset = position % po->frames_per_block;
227 frame = po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size);
233 static inline struct packet_sock *pkt_sk(struct sock *sk)
235 return (struct packet_sock *)sk;
238 static void packet_sock_destruct(struct sock *sk)
240 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
241 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
243 if (!sock_flag(sk, SOCK_DEAD)) {
244 printk("Attempt to release alive packet socket: %p\n", sk);
248 atomic_dec(&packet_socks_nr);
249 #ifdef PACKET_REFCNT_DEBUG
250 printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
255 static const struct proto_ops packet_ops;
257 #ifdef CONFIG_SOCK_PACKET
258 static const struct proto_ops packet_ops_spkt;
260 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
263 struct sockaddr_pkt *spkt;
266 * When we registered the protocol we saved the socket in the data
267 * field for just this event.
270 sk = pt->af_packet_priv;
273 * Yank back the headers [hope the device set this
274 * right or kerboom...]
276 * Incoming packets have ll header pulled,
279 * For outgoing ones skb->data == skb->mac.raw
280 * so that this procedure is noop.
283 if (skb->pkt_type == PACKET_LOOPBACK)
286 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
289 /* drop any routing info */
290 dst_release(skb->dst);
293 /* drop conntrack reference */
296 spkt = (struct sockaddr_pkt*)skb->cb;
298 skb_push(skb, skb->data-skb->mac.raw);
301 * The SOCK_PACKET socket receives _all_ frames.
304 spkt->spkt_family = dev->type;
305 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
306 spkt->spkt_protocol = skb->protocol;
309 * Charge the memory to the socket. This is done specifically
310 * to prevent sockets using all the memory up.
313 if (sock_queue_rcv_skb(sk,skb) == 0)
324 * Output a raw packet to a device layer. This bypasses all the other
325 * protocol layers and you must therefore supply it with a complete frame
328 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
329 struct msghdr *msg, size_t len)
331 struct sock *sk = sock->sk;
332 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
334 struct net_device *dev;
339 * Get and verify the address.
344 if (msg->msg_namelen < sizeof(struct sockaddr))
346 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
347 proto=saddr->spkt_protocol;
350 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
353 * Find the device first to size check it
356 saddr->spkt_device[13] = 0;
357 dev = dev_get_by_name(saddr->spkt_device);
363 if (!(dev->flags & IFF_UP))
367 * You may not queue a frame bigger than the mtu. This is the lowest level
368 * raw protocol and you must do your own fragmentation at this level.
372 if (len > dev->mtu + dev->hard_header_len)
376 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
379 * If the write buffer is full, then tough. At this level the user gets to
380 * deal with the problem - do your own algorithmic backoffs. That's far
391 /* FIXME: Save some space for broken drivers that write a
392 * hard header at transmission time by themselves. PPP is the
393 * notable one here. This should really be fixed at the driver level.
395 skb_reserve(skb, LL_RESERVED_SPACE(dev));
396 skb->nh.raw = skb->data;
398 /* Try to align data part correctly */
399 if (dev->hard_header) {
400 skb->data -= dev->hard_header_len;
401 skb->tail -= dev->hard_header_len;
402 if (len < dev->hard_header_len)
403 skb->nh.raw = skb->data;
406 /* Returns -EFAULT on error */
407 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
408 skb->protocol = proto;
410 skb->priority = sk->sk_priority;
431 static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
434 struct sk_filter *filter;
437 filter = rcu_dereference(sk->sk_filter);
439 res = sk_run_filter(skb, filter->insns, filter->len);
440 rcu_read_unlock_bh();
446 This function makes lazy skb cloning in hope that most of packets
447 are discarded by BPF.
449 Note tricky part: we DO mangle shared skb! skb->data, skb->len
450 and skb->cb are mangled. It works because (and until) packets
451 falling here are owned by current CPU. Output packets are cloned
452 by dev_queue_xmit_nit(), input packets are processed by net_bh
453 sequencially, so that if we return skb to original state on exit,
454 we will not harm anyone.
457 static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
460 struct sockaddr_ll *sll;
461 struct packet_sock *po;
462 u8 * skb_head = skb->data;
463 int skb_len = skb->len;
464 unsigned int snaplen, res;
466 if (skb->pkt_type == PACKET_LOOPBACK)
469 sk = pt->af_packet_priv;
474 if (dev->hard_header) {
475 /* The device has an explicit notion of ll header,
476 exported to higher levels.
478 Otherwise, the device hides datails of it frame
479 structure, so that corresponding packet head
480 never delivered to user.
482 if (sk->sk_type != SOCK_DGRAM)
483 skb_push(skb, skb->data - skb->mac.raw);
484 else if (skb->pkt_type == PACKET_OUTGOING) {
485 /* Special case: outgoing packets have ll header at head */
486 skb_pull(skb, skb->nh.raw - skb->data);
492 res = run_filter(skb, sk, snaplen);
498 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
499 (unsigned)sk->sk_rcvbuf)
502 if (skb_shared(skb)) {
503 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
507 if (skb_head != skb->data) {
508 skb->data = skb_head;
515 sll = (struct sockaddr_ll*)skb->cb;
516 sll->sll_family = AF_PACKET;
517 sll->sll_hatype = dev->type;
518 sll->sll_protocol = skb->protocol;
519 sll->sll_pkttype = skb->pkt_type;
520 sll->sll_ifindex = dev->ifindex;
523 if (dev->hard_header_parse)
524 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
526 if (pskb_trim(skb, snaplen))
529 skb_set_owner_r(skb, sk);
531 dst_release(skb->dst);
534 /* drop conntrack reference */
537 spin_lock(&sk->sk_receive_queue.lock);
538 po->stats.tp_packets++;
539 __skb_queue_tail(&sk->sk_receive_queue, skb);
540 spin_unlock(&sk->sk_receive_queue.lock);
541 sk->sk_data_ready(sk, skb->len);
545 spin_lock(&sk->sk_receive_queue.lock);
546 po->stats.tp_drops++;
547 spin_unlock(&sk->sk_receive_queue.lock);
550 if (skb_head != skb->data && skb_shared(skb)) {
551 skb->data = skb_head;
559 #ifdef CONFIG_PACKET_MMAP
560 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
563 struct packet_sock *po;
564 struct sockaddr_ll *sll;
565 struct tpacket_hdr *h;
566 u8 * skb_head = skb->data;
567 int skb_len = skb->len;
568 unsigned int snaplen, res;
569 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
570 unsigned short macoff, netoff;
571 struct sk_buff *copy_skb = NULL;
573 if (skb->pkt_type == PACKET_LOOPBACK)
576 sk = pt->af_packet_priv;
579 if (dev->hard_header) {
580 if (sk->sk_type != SOCK_DGRAM)
581 skb_push(skb, skb->data - skb->mac.raw);
582 else if (skb->pkt_type == PACKET_OUTGOING) {
583 /* Special case: outgoing packets have ll header at head */
584 skb_pull(skb, skb->nh.raw - skb->data);
585 if (skb->ip_summed == CHECKSUM_PARTIAL)
586 status |= TP_STATUS_CSUMNOTREADY;
592 res = run_filter(skb, sk, snaplen);
598 if (sk->sk_type == SOCK_DGRAM) {
599 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
601 unsigned maclen = skb->nh.raw - skb->data;
602 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
603 macoff = netoff - maclen;
606 if (macoff + snaplen > po->frame_size) {
607 if (po->copy_thresh &&
608 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
609 (unsigned)sk->sk_rcvbuf) {
610 if (skb_shared(skb)) {
611 copy_skb = skb_clone(skb, GFP_ATOMIC);
613 copy_skb = skb_get(skb);
614 skb_head = skb->data;
617 skb_set_owner_r(copy_skb, sk);
619 snaplen = po->frame_size - macoff;
620 if ((int)snaplen < 0)
624 spin_lock(&sk->sk_receive_queue.lock);
625 h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
629 po->head = po->head != po->frame_max ? po->head+1 : 0;
630 po->stats.tp_packets++;
632 status |= TP_STATUS_COPY;
633 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
635 if (!po->stats.tp_drops)
636 status &= ~TP_STATUS_LOSING;
637 spin_unlock(&sk->sk_receive_queue.lock);
639 skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
641 h->tp_len = skb->len;
642 h->tp_snaplen = snaplen;
645 if (skb->tstamp.off_sec == 0) {
646 __net_timestamp(skb);
647 sock_enable_timestamp(sk);
649 h->tp_sec = skb->tstamp.off_sec;
650 h->tp_usec = skb->tstamp.off_usec;
652 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
654 if (dev->hard_header_parse)
655 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
656 sll->sll_family = AF_PACKET;
657 sll->sll_hatype = dev->type;
658 sll->sll_protocol = skb->protocol;
659 sll->sll_pkttype = skb->pkt_type;
660 sll->sll_ifindex = dev->ifindex;
662 h->tp_status = status;
666 struct page *p_start, *p_end;
667 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
669 p_start = virt_to_page(h);
670 p_end = virt_to_page(h_end);
671 while (p_start <= p_end) {
672 flush_dcache_page(p_start);
677 sk->sk_data_ready(sk, 0);
680 if (skb_head != skb->data && skb_shared(skb)) {
681 skb->data = skb_head;
689 po->stats.tp_drops++;
690 spin_unlock(&sk->sk_receive_queue.lock);
692 sk->sk_data_ready(sk, 0);
701 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
702 struct msghdr *msg, size_t len)
704 struct sock *sk = sock->sk;
705 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
707 struct net_device *dev;
710 int ifindex, err, reserve = 0;
713 * Get and verify the address.
717 struct packet_sock *po = pkt_sk(sk);
719 ifindex = po->ifindex;
724 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
726 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
728 ifindex = saddr->sll_ifindex;
729 proto = saddr->sll_protocol;
730 addr = saddr->sll_addr;
734 dev = dev_get_by_index(ifindex);
738 if (sock->type == SOCK_RAW)
739 reserve = dev->hard_header_len;
742 if (!(dev->flags & IFF_UP))
746 if (len > dev->mtu+reserve)
749 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
750 msg->msg_flags & MSG_DONTWAIT, &err);
754 skb_reserve(skb, LL_RESERVED_SPACE(dev));
755 skb->nh.raw = skb->data;
757 if (dev->hard_header) {
760 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
761 if (sock->type != SOCK_DGRAM) {
762 skb->tail = skb->data;
768 /* Returns -EFAULT on error */
769 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
773 skb->protocol = proto;
775 skb->priority = sk->sk_priority;
781 err = dev_queue_xmit(skb);
782 if (err > 0 && (err = net_xmit_errno(err)) != 0)
799 * Close a PACKET socket. This is fairly simple. We immediately go
800 * to 'closed' state and remove our protocol entry in the device list.
803 static int packet_release(struct socket *sock)
805 struct sock *sk = sock->sk;
806 struct packet_sock *po;
813 write_lock_bh(&packet_sklist_lock);
814 sk_del_node_init(sk);
815 write_unlock_bh(&packet_sklist_lock);
818 * Unhook packet receive handler.
823 * Remove the protocol hook
825 dev_remove_pack(&po->prot_hook);
831 #ifdef CONFIG_PACKET_MULTICAST
832 packet_flush_mclist(sk);
835 #ifdef CONFIG_PACKET_MMAP
837 struct tpacket_req req;
838 memset(&req, 0, sizeof(req));
839 packet_set_ring(sk, &req, 1);
844 * Now the socket is dead. No more input will appear.
852 skb_queue_purge(&sk->sk_receive_queue);
859 * Attach a packet hook.
862 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
864 struct packet_sock *po = pkt_sk(sk);
866 * Detach an existing hook if present.
871 spin_lock(&po->bind_lock);
876 spin_unlock(&po->bind_lock);
877 dev_remove_pack(&po->prot_hook);
878 spin_lock(&po->bind_lock);
882 po->prot_hook.type = protocol;
883 po->prot_hook.dev = dev;
885 po->ifindex = dev ? dev->ifindex : 0;
891 if (dev->flags&IFF_UP) {
892 dev_add_pack(&po->prot_hook);
896 sk->sk_err = ENETDOWN;
897 if (!sock_flag(sk, SOCK_DEAD))
898 sk->sk_error_report(sk);
901 dev_add_pack(&po->prot_hook);
907 spin_unlock(&po->bind_lock);
913 * Bind a packet socket to a device
916 #ifdef CONFIG_SOCK_PACKET
918 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
920 struct sock *sk=sock->sk;
922 struct net_device *dev;
929 if (addr_len != sizeof(struct sockaddr))
931 strlcpy(name,uaddr->sa_data,sizeof(name));
933 dev = dev_get_by_name(name);
935 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
942 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
944 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
945 struct sock *sk=sock->sk;
946 struct net_device *dev = NULL;
954 if (addr_len < sizeof(struct sockaddr_ll))
956 if (sll->sll_family != AF_PACKET)
959 if (sll->sll_ifindex) {
961 dev = dev_get_by_index(sll->sll_ifindex);
965 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
973 static struct proto packet_proto = {
975 .owner = THIS_MODULE,
976 .obj_size = sizeof(struct packet_sock),
980 * Create a packet of type SOCK_PACKET.
983 static int packet_create(struct socket *sock, int protocol)
986 struct packet_sock *po;
987 __be16 proto = (__force __be16)protocol; /* weird, but documented */
990 if (!capable(CAP_NET_RAW))
992 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
993 #ifdef CONFIG_SOCK_PACKET
994 && sock->type != SOCK_PACKET
997 return -ESOCKTNOSUPPORT;
999 sock->state = SS_UNCONNECTED;
1002 sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
1006 sock->ops = &packet_ops;
1007 #ifdef CONFIG_SOCK_PACKET
1008 if (sock->type == SOCK_PACKET)
1009 sock->ops = &packet_ops_spkt;
1011 sock_init_data(sock, sk);
1014 sk->sk_family = PF_PACKET;
1017 sk->sk_destruct = packet_sock_destruct;
1018 atomic_inc(&packet_socks_nr);
1021 * Attach a protocol block
1024 spin_lock_init(&po->bind_lock);
1025 po->prot_hook.func = packet_rcv;
1026 #ifdef CONFIG_SOCK_PACKET
1027 if (sock->type == SOCK_PACKET)
1028 po->prot_hook.func = packet_rcv_spkt;
1030 po->prot_hook.af_packet_priv = sk;
1033 po->prot_hook.type = proto;
1034 dev_add_pack(&po->prot_hook);
1039 write_lock_bh(&packet_sklist_lock);
1040 sk_add_node(sk, &packet_sklist);
1041 write_unlock_bh(&packet_sklist_lock);
1048 * Pull a packet from our receive queue and hand it to the user.
1049 * If necessary we block.
1052 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1053 struct msghdr *msg, size_t len, int flags)
1055 struct sock *sk = sock->sk;
1056 struct sk_buff *skb;
1058 struct sockaddr_ll *sll;
1061 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1065 /* What error should we return now? EUNATTACH? */
1066 if (pkt_sk(sk)->ifindex < 0)
1071 * Call the generic datagram receiver. This handles all sorts
1072 * of horrible races and re-entrancy so we can forget about it
1073 * in the protocol layers.
1075 * Now it will return ENETDOWN, if device have just gone down,
1076 * but then it will block.
1079 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1082 * An error occurred so return it. Because skb_recv_datagram()
1083 * handles the blocking we don't see and worry about blocking
1091 * If the address length field is there to be filled in, we fill
1095 sll = (struct sockaddr_ll*)skb->cb;
1096 if (sock->type == SOCK_PACKET)
1097 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1099 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1102 * You lose any data beyond the buffer you gave. If it worries a
1103 * user program they can ask the device for its MTU anyway.
1110 msg->msg_flags|=MSG_TRUNC;
1113 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1117 sock_recv_timestamp(msg, sk, skb);
1120 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1123 * Free or return the buffer as appropriate. Again this
1124 * hides all the races and re-entrancy issues from us.
1126 err = (flags&MSG_TRUNC) ? skb->len : copied;
1129 skb_free_datagram(sk, skb);
1134 #ifdef CONFIG_SOCK_PACKET
1135 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1136 int *uaddr_len, int peer)
1138 struct net_device *dev;
1139 struct sock *sk = sock->sk;
1144 uaddr->sa_family = AF_PACKET;
1145 dev = dev_get_by_index(pkt_sk(sk)->ifindex);
1147 strlcpy(uaddr->sa_data, dev->name, 15);
1150 memset(uaddr->sa_data, 0, 14);
1151 *uaddr_len = sizeof(*uaddr);
1157 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1158 int *uaddr_len, int peer)
1160 struct net_device *dev;
1161 struct sock *sk = sock->sk;
1162 struct packet_sock *po = pkt_sk(sk);
1163 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1168 sll->sll_family = AF_PACKET;
1169 sll->sll_ifindex = po->ifindex;
1170 sll->sll_protocol = po->num;
1171 dev = dev_get_by_index(po->ifindex);
1173 sll->sll_hatype = dev->type;
1174 sll->sll_halen = dev->addr_len;
1175 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1178 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1181 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
1186 #ifdef CONFIG_PACKET_MULTICAST
1187 static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1190 case PACKET_MR_MULTICAST:
1192 dev_mc_add(dev, i->addr, i->alen, 0);
1194 dev_mc_delete(dev, i->addr, i->alen, 0);
1196 case PACKET_MR_PROMISC:
1197 dev_set_promiscuity(dev, what);
1199 case PACKET_MR_ALLMULTI:
1200 dev_set_allmulti(dev, what);
1206 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1208 for ( ; i; i=i->next) {
1209 if (i->ifindex == dev->ifindex)
1210 packet_dev_mc(dev, i, what);
1214 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
1216 struct packet_sock *po = pkt_sk(sk);
1217 struct packet_mclist *ml, *i;
1218 struct net_device *dev;
1224 dev = __dev_get_by_index(mreq->mr_ifindex);
1229 if (mreq->mr_alen > dev->addr_len)
1233 i = kmalloc(sizeof(*i), GFP_KERNEL);
1238 for (ml = po->mclist; ml; ml = ml->next) {
1239 if (ml->ifindex == mreq->mr_ifindex &&
1240 ml->type == mreq->mr_type &&
1241 ml->alen == mreq->mr_alen &&
1242 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1244 /* Free the new element ... */
1250 i->type = mreq->mr_type;
1251 i->ifindex = mreq->mr_ifindex;
1252 i->alen = mreq->mr_alen;
1253 memcpy(i->addr, mreq->mr_address, i->alen);
1255 i->next = po->mclist;
1257 packet_dev_mc(dev, i, +1);
1264 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
1266 struct packet_mclist *ml, **mlp;
1270 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &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) {
1275 if (--ml->count == 0) {
1276 struct net_device *dev;
1278 dev = dev_get_by_index(ml->ifindex);
1280 packet_dev_mc(dev, ml, -1);
1290 return -EADDRNOTAVAIL;
1293 static void packet_flush_mclist(struct sock *sk)
1295 struct packet_sock *po = pkt_sk(sk);
1296 struct packet_mclist *ml;
1302 while ((ml = po->mclist) != NULL) {
1303 struct net_device *dev;
1305 po->mclist = ml->next;
1306 if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1307 packet_dev_mc(dev, ml, -1);
1317 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1319 struct sock *sk = sock->sk;
1322 if (level != SOL_PACKET)
1323 return -ENOPROTOOPT;
1326 #ifdef CONFIG_PACKET_MULTICAST
1327 case PACKET_ADD_MEMBERSHIP:
1328 case PACKET_DROP_MEMBERSHIP:
1330 struct packet_mreq_max mreq;
1332 memset(&mreq, 0, sizeof(mreq));
1333 if (len < sizeof(struct packet_mreq))
1335 if (len > sizeof(mreq))
1337 if (copy_from_user(&mreq,optval,len))
1339 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1341 if (optname == PACKET_ADD_MEMBERSHIP)
1342 ret = packet_mc_add(sk, &mreq);
1344 ret = packet_mc_drop(sk, &mreq);
1348 #ifdef CONFIG_PACKET_MMAP
1349 case PACKET_RX_RING:
1351 struct tpacket_req req;
1353 if (optlen<sizeof(req))
1355 if (copy_from_user(&req,optval,sizeof(req)))
1357 return packet_set_ring(sk, &req, 0);
1359 case PACKET_COPY_THRESH:
1363 if (optlen!=sizeof(val))
1365 if (copy_from_user(&val,optval,sizeof(val)))
1368 pkt_sk(sk)->copy_thresh = val;
1373 return -ENOPROTOOPT;
1377 static int packet_getsockopt(struct socket *sock, int level, int optname,
1378 char __user *optval, int __user *optlen)
1381 struct sock *sk = sock->sk;
1382 struct packet_sock *po = pkt_sk(sk);
1384 if (level != SOL_PACKET)
1385 return -ENOPROTOOPT;
1387 if (get_user(len, optlen))
1394 case PACKET_STATISTICS:
1396 struct tpacket_stats st;
1398 if (len > sizeof(struct tpacket_stats))
1399 len = sizeof(struct tpacket_stats);
1400 spin_lock_bh(&sk->sk_receive_queue.lock);
1402 memset(&po->stats, 0, sizeof(st));
1403 spin_unlock_bh(&sk->sk_receive_queue.lock);
1404 st.tp_packets += st.tp_drops;
1406 if (copy_to_user(optval, &st, len))
1411 return -ENOPROTOOPT;
1414 if (put_user(len, optlen))
1420 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1423 struct hlist_node *node;
1424 struct net_device *dev = (struct net_device*)data;
1426 read_lock(&packet_sklist_lock);
1427 sk_for_each(sk, node, &packet_sklist) {
1428 struct packet_sock *po = pkt_sk(sk);
1431 case NETDEV_UNREGISTER:
1432 #ifdef CONFIG_PACKET_MULTICAST
1434 packet_dev_mclist(dev, po->mclist, -1);
1438 if (dev->ifindex == po->ifindex) {
1439 spin_lock(&po->bind_lock);
1441 __dev_remove_pack(&po->prot_hook);
1444 sk->sk_err = ENETDOWN;
1445 if (!sock_flag(sk, SOCK_DEAD))
1446 sk->sk_error_report(sk);
1448 if (msg == NETDEV_UNREGISTER) {
1450 po->prot_hook.dev = NULL;
1452 spin_unlock(&po->bind_lock);
1456 spin_lock(&po->bind_lock);
1457 if (dev->ifindex == po->ifindex && po->num &&
1459 dev_add_pack(&po->prot_hook);
1463 spin_unlock(&po->bind_lock);
1467 read_unlock(&packet_sklist_lock);
1472 static int packet_ioctl(struct socket *sock, unsigned int cmd,
1475 struct sock *sk = sock->sk;
1480 int amount = atomic_read(&sk->sk_wmem_alloc);
1481 return put_user(amount, (int __user *)arg);
1485 struct sk_buff *skb;
1488 spin_lock_bh(&sk->sk_receive_queue.lock);
1489 skb = skb_peek(&sk->sk_receive_queue);
1492 spin_unlock_bh(&sk->sk_receive_queue.lock);
1493 return put_user(amount, (int __user *)arg);
1496 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1506 case SIOCGIFBRDADDR:
1507 case SIOCSIFBRDADDR:
1508 case SIOCGIFNETMASK:
1509 case SIOCSIFNETMASK:
1510 case SIOCGIFDSTADDR:
1511 case SIOCSIFDSTADDR:
1513 return inet_dgram_ops.ioctl(sock, cmd, arg);
1517 return -ENOIOCTLCMD;
1522 #ifndef CONFIG_PACKET_MMAP
1523 #define packet_mmap sock_no_mmap
1524 #define packet_poll datagram_poll
1527 static unsigned int packet_poll(struct file * file, struct socket *sock,
1530 struct sock *sk = sock->sk;
1531 struct packet_sock *po = pkt_sk(sk);
1532 unsigned int mask = datagram_poll(file, sock, wait);
1534 spin_lock_bh(&sk->sk_receive_queue.lock);
1536 unsigned last = po->head ? po->head-1 : po->frame_max;
1537 struct tpacket_hdr *h;
1539 h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
1542 mask |= POLLIN | POLLRDNORM;
1544 spin_unlock_bh(&sk->sk_receive_queue.lock);
1549 /* Dirty? Well, I still did not learn better way to account
1553 static void packet_mm_open(struct vm_area_struct *vma)
1555 struct file *file = vma->vm_file;
1556 struct socket * sock = file->private_data;
1557 struct sock *sk = sock->sk;
1560 atomic_inc(&pkt_sk(sk)->mapped);
1563 static void packet_mm_close(struct vm_area_struct *vma)
1565 struct file *file = vma->vm_file;
1566 struct socket * sock = file->private_data;
1567 struct sock *sk = sock->sk;
1570 atomic_dec(&pkt_sk(sk)->mapped);
1573 static struct vm_operations_struct packet_mmap_ops = {
1574 .open = packet_mm_open,
1575 .close =packet_mm_close,
1578 static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
1580 return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
1583 static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
1587 for (i = 0; i < len; i++) {
1588 if (likely(pg_vec[i]))
1589 free_pages((unsigned long) pg_vec[i], order);
1594 static inline char *alloc_one_pg_vec_page(unsigned long order)
1596 return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
1600 static char **alloc_pg_vec(struct tpacket_req *req, int order)
1602 unsigned int block_nr = req->tp_block_nr;
1606 pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
1607 if (unlikely(!pg_vec))
1610 for (i = 0; i < block_nr; i++) {
1611 pg_vec[i] = alloc_one_pg_vec_page(order);
1612 if (unlikely(!pg_vec[i]))
1613 goto out_free_pgvec;
1620 free_pg_vec(pg_vec, order, block_nr);
1625 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1627 char **pg_vec = NULL;
1628 struct packet_sock *po = pkt_sk(sk);
1629 int was_running, order = 0;
1633 if (req->tp_block_nr) {
1636 /* Sanity tests and some calculations */
1638 if (unlikely(po->pg_vec))
1641 if (unlikely((int)req->tp_block_size <= 0))
1643 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
1645 if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
1647 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
1650 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1651 if (unlikely(po->frames_per_block <= 0))
1653 if (unlikely((po->frames_per_block * req->tp_block_nr) !=
1658 order = get_order(req->tp_block_size);
1659 pg_vec = alloc_pg_vec(req, order);
1660 if (unlikely(!pg_vec))
1664 for (i = 0; i < req->tp_block_nr; i++) {
1665 char *ptr = pg_vec[i];
1666 struct tpacket_hdr *header;
1669 for (k = 0; k < po->frames_per_block; k++) {
1670 header = (struct tpacket_hdr *) ptr;
1671 header->tp_status = TP_STATUS_KERNEL;
1672 ptr += req->tp_frame_size;
1677 if (unlikely(req->tp_frame_nr))
1683 /* Detach socket from network */
1684 spin_lock(&po->bind_lock);
1685 was_running = po->running;
1688 __dev_remove_pack(&po->prot_hook);
1693 spin_unlock(&po->bind_lock);
1698 if (closing || atomic_read(&po->mapped) == 0) {
1700 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1702 spin_lock_bh(&sk->sk_receive_queue.lock);
1703 pg_vec = XC(po->pg_vec, pg_vec);
1704 po->frame_max = (req->tp_frame_nr - 1);
1706 po->frame_size = req->tp_frame_size;
1707 spin_unlock_bh(&sk->sk_receive_queue.lock);
1709 order = XC(po->pg_vec_order, order);
1710 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1712 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1713 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1714 skb_queue_purge(&sk->sk_receive_queue);
1716 if (atomic_read(&po->mapped))
1717 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1720 spin_lock(&po->bind_lock);
1721 if (was_running && !po->running) {
1725 dev_add_pack(&po->prot_hook);
1727 spin_unlock(&po->bind_lock);
1732 free_pg_vec(pg_vec, order, req->tp_block_nr);
1737 static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1739 struct sock *sk = sock->sk;
1740 struct packet_sock *po = pkt_sk(sk);
1742 unsigned long start;
1749 size = vma->vm_end - vma->vm_start;
1752 if (po->pg_vec == NULL)
1754 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1757 start = vma->vm_start;
1758 for (i = 0; i < po->pg_vec_len; i++) {
1759 struct page *page = virt_to_page(po->pg_vec[i]);
1762 for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
1763 err = vm_insert_page(vma, start, page);
1769 atomic_inc(&po->mapped);
1770 vma->vm_ops = &packet_mmap_ops;
1780 #ifdef CONFIG_SOCK_PACKET
1781 static const struct proto_ops packet_ops_spkt = {
1782 .family = PF_PACKET,
1783 .owner = THIS_MODULE,
1784 .release = packet_release,
1785 .bind = packet_bind_spkt,
1786 .connect = sock_no_connect,
1787 .socketpair = sock_no_socketpair,
1788 .accept = sock_no_accept,
1789 .getname = packet_getname_spkt,
1790 .poll = datagram_poll,
1791 .ioctl = packet_ioctl,
1792 .listen = sock_no_listen,
1793 .shutdown = sock_no_shutdown,
1794 .setsockopt = sock_no_setsockopt,
1795 .getsockopt = sock_no_getsockopt,
1796 .sendmsg = packet_sendmsg_spkt,
1797 .recvmsg = packet_recvmsg,
1798 .mmap = sock_no_mmap,
1799 .sendpage = sock_no_sendpage,
1803 static const struct proto_ops packet_ops = {
1804 .family = PF_PACKET,
1805 .owner = THIS_MODULE,
1806 .release = packet_release,
1807 .bind = packet_bind,
1808 .connect = sock_no_connect,
1809 .socketpair = sock_no_socketpair,
1810 .accept = sock_no_accept,
1811 .getname = packet_getname,
1812 .poll = packet_poll,
1813 .ioctl = packet_ioctl,
1814 .listen = sock_no_listen,
1815 .shutdown = sock_no_shutdown,
1816 .setsockopt = packet_setsockopt,
1817 .getsockopt = packet_getsockopt,
1818 .sendmsg = packet_sendmsg,
1819 .recvmsg = packet_recvmsg,
1820 .mmap = packet_mmap,
1821 .sendpage = sock_no_sendpage,
1824 static struct net_proto_family packet_family_ops = {
1825 .family = PF_PACKET,
1826 .create = packet_create,
1827 .owner = THIS_MODULE,
1830 static struct notifier_block packet_netdev_notifier = {
1831 .notifier_call =packet_notifier,
1834 #ifdef CONFIG_PROC_FS
1835 static inline struct sock *packet_seq_idx(loff_t off)
1838 struct hlist_node *node;
1840 sk_for_each(s, node, &packet_sklist) {
1847 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1849 read_lock(&packet_sklist_lock);
1850 return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1853 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1856 return (v == SEQ_START_TOKEN)
1857 ? sk_head(&packet_sklist)
1858 : sk_next((struct sock*)v) ;
1861 static void packet_seq_stop(struct seq_file *seq, void *v)
1863 read_unlock(&packet_sklist_lock);
1866 static int packet_seq_show(struct seq_file *seq, void *v)
1868 if (v == SEQ_START_TOKEN)
1869 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1872 const struct packet_sock *po = pkt_sk(s);
1875 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1877 atomic_read(&s->sk_refcnt),
1882 atomic_read(&s->sk_rmem_alloc),
1890 static struct seq_operations packet_seq_ops = {
1891 .start = packet_seq_start,
1892 .next = packet_seq_next,
1893 .stop = packet_seq_stop,
1894 .show = packet_seq_show,
1897 static int packet_seq_open(struct inode *inode, struct file *file)
1899 return seq_open(file, &packet_seq_ops);
1902 static struct file_operations packet_seq_fops = {
1903 .owner = THIS_MODULE,
1904 .open = packet_seq_open,
1906 .llseek = seq_lseek,
1907 .release = seq_release,
1912 static void __exit packet_exit(void)
1914 proc_net_remove("packet");
1915 unregister_netdevice_notifier(&packet_netdev_notifier);
1916 sock_unregister(PF_PACKET);
1917 proto_unregister(&packet_proto);
1920 static int __init packet_init(void)
1922 int rc = proto_register(&packet_proto, 0);
1927 sock_register(&packet_family_ops);
1928 register_netdevice_notifier(&packet_netdev_notifier);
1929 proc_net_fops_create("packet", 0, &packet_seq_fops);
1934 module_init(packet_init);
1935 module_exit(packet_exit);
1936 MODULE_LICENSE("GPL");
1937 MODULE_ALIAS_NETPROTO(PF_PACKET);