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.
40 * This program is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU General Public License
42 * as published by the Free Software Foundation; either version
43 * 2 of the License, or (at your option) any later version.
47 #include <linux/config.h>
48 #include <linux/types.h>
49 #include <linux/sched.h>
51 #include <linux/fcntl.h>
52 #include <linux/socket.h>
54 #include <linux/inet.h>
55 #include <linux/netdevice.h>
56 #include <linux/if_packet.h>
57 #include <linux/wireless.h>
58 #include <linux/kmod.h>
60 #include <net/protocol.h>
61 #include <linux/skbuff.h>
63 #include <linux/errno.h>
64 #include <linux/timer.h>
65 #include <asm/system.h>
66 #include <asm/uaccess.h>
67 #include <asm/ioctls.h>
70 #include <linux/proc_fs.h>
71 #include <linux/seq_file.h>
72 #include <linux/poll.h>
73 #include <linux/module.h>
74 #include <linux/init.h>
77 #include <net/inet_common.h>
80 #define CONFIG_SOCK_PACKET 1
83 Proposed replacement for SIOC{ADD,DEL}MULTI and
84 IFF_PROMISC, IFF_ALLMULTI flags.
86 It is more expensive, but I believe,
87 it is really correct solution: reentereble, safe and fault tolerant.
89 IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
90 reference count and global flag, so that real status is
91 (gflag|(count != 0)), so that we can use obsolete faulty interface
92 not harming clever users.
94 #define CONFIG_PACKET_MULTICAST 1
98 - if device has no dev->hard_header routine, it adds and removes ll header
99 inside itself. In this case ll header is invisible outside of device,
100 but higher levels still should reserve dev->hard_header_len.
101 Some devices are enough clever to reallocate skb, when header
102 will not fit to reserved space (tunnel), another ones are silly
104 - packet socket receives packets with pulled ll header,
105 so that SOCK_RAW should push it back.
110 Incoming, dev->hard_header!=NULL
114 Outgoing, dev->hard_header!=NULL
118 Incoming, dev->hard_header==NULL
119 mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
120 PPP makes it, that is wrong, because introduce assymetry
121 between rx and tx paths.
124 Outgoing, dev->hard_header==NULL
125 mac.raw -> data. ll header is still not built!
129 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
135 dev->hard_header != NULL
139 dev->hard_header == NULL (ll header is added by device, we cannot control it)
143 We should set nh.raw on output to correct posistion,
144 packet classifier depends on it.
147 /* List of all packet sockets. */
148 static HLIST_HEAD(packet_sklist);
149 static DEFINE_RWLOCK(packet_sklist_lock);
151 static atomic_t packet_socks_nr;
154 /* Private packet socket structures. */
156 #ifdef CONFIG_PACKET_MULTICAST
159 struct packet_mclist *next;
164 unsigned char addr[8];
167 #ifdef CONFIG_PACKET_MMAP
168 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
171 static void packet_flush_mclist(struct sock *sk);
174 /* struct sock has to be the first member of packet_sock */
176 struct tpacket_stats stats;
177 #ifdef CONFIG_PACKET_MMAP
180 unsigned int frames_per_block;
181 unsigned int frame_size;
182 unsigned int frame_max;
185 struct packet_type prot_hook;
186 spinlock_t bind_lock;
187 char running; /* prot_hook is attached*/
188 int ifindex; /* bound device */
190 #ifdef CONFIG_PACKET_MULTICAST
191 struct packet_mclist *mclist;
193 #ifdef CONFIG_PACKET_MMAP
195 unsigned int pg_vec_order;
196 unsigned int pg_vec_pages;
197 unsigned int pg_vec_len;
201 #ifdef CONFIG_PACKET_MMAP
203 static inline char *packet_lookup_frame(struct packet_sock *po, unsigned int position)
205 unsigned int pg_vec_pos, frame_offset;
208 pg_vec_pos = position / po->frames_per_block;
209 frame_offset = position % po->frames_per_block;
211 frame = po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size);
217 static inline struct packet_sock *pkt_sk(struct sock *sk)
219 return (struct packet_sock *)sk;
222 static void packet_sock_destruct(struct sock *sk)
224 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
225 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
227 if (!sock_flag(sk, SOCK_DEAD)) {
228 printk("Attempt to release alive packet socket: %p\n", sk);
232 atomic_dec(&packet_socks_nr);
233 #ifdef PACKET_REFCNT_DEBUG
234 printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
239 static struct proto_ops packet_ops;
241 #ifdef CONFIG_SOCK_PACKET
242 static struct proto_ops packet_ops_spkt;
244 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
247 struct sockaddr_pkt *spkt;
250 * When we registered the protocol we saved the socket in the data
251 * field for just this event.
254 sk = pt->af_packet_priv;
257 * Yank back the headers [hope the device set this
258 * right or kerboom...]
260 * Incoming packets have ll header pulled,
263 * For outgoing ones skb->data == skb->mac.raw
264 * so that this procedure is noop.
267 if (skb->pkt_type == PACKET_LOOPBACK)
270 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
273 /* drop any routing info */
274 dst_release(skb->dst);
277 /* drop conntrack reference */
280 spkt = (struct sockaddr_pkt*)skb->cb;
282 skb_push(skb, skb->data-skb->mac.raw);
285 * The SOCK_PACKET socket receives _all_ frames.
288 spkt->spkt_family = dev->type;
289 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
290 spkt->spkt_protocol = skb->protocol;
293 * Charge the memory to the socket. This is done specifically
294 * to prevent sockets using all the memory up.
297 if (sock_queue_rcv_skb(sk,skb) == 0)
308 * Output a raw packet to a device layer. This bypasses all the other
309 * protocol layers and you must therefore supply it with a complete frame
312 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
313 struct msghdr *msg, size_t len)
315 struct sock *sk = sock->sk;
316 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
318 struct net_device *dev;
319 unsigned short proto=0;
323 * Get and verify the address.
328 if (msg->msg_namelen < sizeof(struct sockaddr))
330 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
331 proto=saddr->spkt_protocol;
334 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
337 * Find the device first to size check it
340 saddr->spkt_device[13] = 0;
341 dev = dev_get_by_name(saddr->spkt_device);
347 * You may not queue a frame bigger than the mtu. This is the lowest level
348 * raw protocol and you must do your own fragmentation at this level.
352 if(len>dev->mtu+dev->hard_header_len)
356 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
359 * If the write buffer is full, then tough. At this level the user gets to
360 * deal with the problem - do your own algorithmic backoffs. That's far
371 /* FIXME: Save some space for broken drivers that write a
372 * hard header at transmission time by themselves. PPP is the
373 * notable one here. This should really be fixed at the driver level.
375 skb_reserve(skb, LL_RESERVED_SPACE(dev));
376 skb->nh.raw = skb->data;
378 /* Try to align data part correctly */
379 if (dev->hard_header) {
380 skb->data -= dev->hard_header_len;
381 skb->tail -= dev->hard_header_len;
382 if (len < dev->hard_header_len)
383 skb->nh.raw = skb->data;
386 /* Returns -EFAULT on error */
387 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
388 skb->protocol = proto;
390 skb->priority = sk->sk_priority;
395 if (!(dev->flags & IFF_UP))
415 static inline unsigned run_filter(struct sk_buff *skb, struct sock *sk, unsigned res)
417 struct sk_filter *filter;
420 filter = sk->sk_filter;
422 * Our caller already checked that filter != NULL but we need to
423 * verify that under bh_lock_sock() to be safe
425 if (likely(filter != NULL))
426 res = sk_run_filter(skb, filter->insns, filter->len);
433 This function makes lazy skb cloning in hope that most of packets
434 are discarded by BPF.
436 Note tricky part: we DO mangle shared skb! skb->data, skb->len
437 and skb->cb are mangled. It works because (and until) packets
438 falling here are owned by current CPU. Output packets are cloned
439 by dev_queue_xmit_nit(), input packets are processed by net_bh
440 sequencially, so that if we return skb to original state on exit,
441 we will not harm anyone.
444 static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
447 struct sockaddr_ll *sll;
448 struct packet_sock *po;
449 u8 * skb_head = skb->data;
450 int skb_len = skb->len;
453 if (skb->pkt_type == PACKET_LOOPBACK)
456 sk = pt->af_packet_priv;
461 if (dev->hard_header) {
462 /* The device has an explicit notion of ll header,
463 exported to higher levels.
465 Otherwise, the device hides datails of it frame
466 structure, so that corresponding packet head
467 never delivered to user.
469 if (sk->sk_type != SOCK_DGRAM)
470 skb_push(skb, skb->data - skb->mac.raw);
471 else if (skb->pkt_type == PACKET_OUTGOING) {
472 /* Special case: outgoing packets have ll header at head */
473 skb_pull(skb, skb->nh.raw - skb->data);
480 unsigned res = run_filter(skb, sk, snaplen);
487 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
488 (unsigned)sk->sk_rcvbuf)
491 if (skb_shared(skb)) {
492 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
496 if (skb_head != skb->data) {
497 skb->data = skb_head;
504 sll = (struct sockaddr_ll*)skb->cb;
505 sll->sll_family = AF_PACKET;
506 sll->sll_hatype = dev->type;
507 sll->sll_protocol = skb->protocol;
508 sll->sll_pkttype = skb->pkt_type;
509 sll->sll_ifindex = dev->ifindex;
512 if (dev->hard_header_parse)
513 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
515 if (pskb_trim(skb, snaplen))
518 skb_set_owner_r(skb, sk);
520 dst_release(skb->dst);
523 /* drop conntrack reference */
526 spin_lock(&sk->sk_receive_queue.lock);
527 po->stats.tp_packets++;
528 __skb_queue_tail(&sk->sk_receive_queue, skb);
529 spin_unlock(&sk->sk_receive_queue.lock);
530 sk->sk_data_ready(sk, skb->len);
534 spin_lock(&sk->sk_receive_queue.lock);
535 po->stats.tp_drops++;
536 spin_unlock(&sk->sk_receive_queue.lock);
539 if (skb_head != skb->data && skb_shared(skb)) {
540 skb->data = skb_head;
548 #ifdef CONFIG_PACKET_MMAP
549 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
552 struct packet_sock *po;
553 struct sockaddr_ll *sll;
554 struct tpacket_hdr *h;
555 u8 * skb_head = skb->data;
556 int skb_len = skb->len;
558 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
559 unsigned short macoff, netoff;
560 struct sk_buff *copy_skb = NULL;
562 if (skb->pkt_type == PACKET_LOOPBACK)
565 sk = pt->af_packet_priv;
568 if (dev->hard_header) {
569 if (sk->sk_type != SOCK_DGRAM)
570 skb_push(skb, skb->data - skb->mac.raw);
571 else if (skb->pkt_type == PACKET_OUTGOING) {
572 /* Special case: outgoing packets have ll header at head */
573 skb_pull(skb, skb->nh.raw - skb->data);
574 if (skb->ip_summed == CHECKSUM_HW)
575 status |= TP_STATUS_CSUMNOTREADY;
582 unsigned res = run_filter(skb, sk, snaplen);
589 if (sk->sk_type == SOCK_DGRAM) {
590 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
592 unsigned maclen = skb->nh.raw - skb->data;
593 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
594 macoff = netoff - maclen;
597 if (macoff + snaplen > po->frame_size) {
598 if (po->copy_thresh &&
599 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
600 (unsigned)sk->sk_rcvbuf) {
601 if (skb_shared(skb)) {
602 copy_skb = skb_clone(skb, GFP_ATOMIC);
604 copy_skb = skb_get(skb);
605 skb_head = skb->data;
608 skb_set_owner_r(copy_skb, sk);
610 snaplen = po->frame_size - macoff;
611 if ((int)snaplen < 0)
614 if (snaplen > skb->len-skb->data_len)
615 snaplen = skb->len-skb->data_len;
617 spin_lock(&sk->sk_receive_queue.lock);
618 h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
622 po->head = po->head != po->frame_max ? po->head+1 : 0;
623 po->stats.tp_packets++;
625 status |= TP_STATUS_COPY;
626 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
628 if (!po->stats.tp_drops)
629 status &= ~TP_STATUS_LOSING;
630 spin_unlock(&sk->sk_receive_queue.lock);
632 memcpy((u8*)h + macoff, skb->data, snaplen);
634 h->tp_len = skb->len;
635 h->tp_snaplen = snaplen;
638 if (skb->tstamp.off_sec == 0) {
639 __net_timestamp(skb);
640 sock_enable_timestamp(sk);
642 h->tp_sec = skb_tv_base.tv_sec + skb->tstamp.off_sec;
643 h->tp_usec = skb_tv_base.tv_usec + skb->tstamp.off_usec;
645 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
647 if (dev->hard_header_parse)
648 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
649 sll->sll_family = AF_PACKET;
650 sll->sll_hatype = dev->type;
651 sll->sll_protocol = skb->protocol;
652 sll->sll_pkttype = skb->pkt_type;
653 sll->sll_ifindex = dev->ifindex;
655 h->tp_status = status;
659 struct page *p_start, *p_end;
660 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
662 p_start = virt_to_page(h);
663 p_end = virt_to_page(h_end);
664 while (p_start <= p_end) {
665 flush_dcache_page(p_start);
670 sk->sk_data_ready(sk, 0);
673 if (skb_head != skb->data && skb_shared(skb)) {
674 skb->data = skb_head;
682 po->stats.tp_drops++;
683 spin_unlock(&sk->sk_receive_queue.lock);
685 sk->sk_data_ready(sk, 0);
694 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
695 struct msghdr *msg, size_t len)
697 struct sock *sk = sock->sk;
698 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
700 struct net_device *dev;
701 unsigned short proto;
703 int ifindex, err, reserve = 0;
706 * Get and verify the address.
710 struct packet_sock *po = pkt_sk(sk);
712 ifindex = po->ifindex;
717 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
719 ifindex = saddr->sll_ifindex;
720 proto = saddr->sll_protocol;
721 addr = saddr->sll_addr;
725 dev = dev_get_by_index(ifindex);
729 if (sock->type == SOCK_RAW)
730 reserve = dev->hard_header_len;
733 if (len > dev->mtu+reserve)
736 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
737 msg->msg_flags & MSG_DONTWAIT, &err);
741 skb_reserve(skb, LL_RESERVED_SPACE(dev));
742 skb->nh.raw = skb->data;
744 if (dev->hard_header) {
747 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
748 if (sock->type != SOCK_DGRAM) {
749 skb->tail = skb->data;
755 /* Returns -EFAULT on error */
756 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
760 skb->protocol = proto;
762 skb->priority = sk->sk_priority;
765 if (!(dev->flags & IFF_UP))
772 err = dev_queue_xmit(skb);
773 if (err > 0 && (err = net_xmit_errno(err)) != 0)
790 * Close a PACKET socket. This is fairly simple. We immediately go
791 * to 'closed' state and remove our protocol entry in the device list.
794 static int packet_release(struct socket *sock)
796 struct sock *sk = sock->sk;
797 struct packet_sock *po;
804 write_lock_bh(&packet_sklist_lock);
805 sk_del_node_init(sk);
806 write_unlock_bh(&packet_sklist_lock);
809 * Unhook packet receive handler.
814 * Remove the protocol hook
816 dev_remove_pack(&po->prot_hook);
822 #ifdef CONFIG_PACKET_MULTICAST
823 packet_flush_mclist(sk);
826 #ifdef CONFIG_PACKET_MMAP
828 struct tpacket_req req;
829 memset(&req, 0, sizeof(req));
830 packet_set_ring(sk, &req, 1);
835 * Now the socket is dead. No more input will appear.
843 skb_queue_purge(&sk->sk_receive_queue);
850 * Attach a packet hook.
853 static int packet_do_bind(struct sock *sk, struct net_device *dev, int protocol)
855 struct packet_sock *po = pkt_sk(sk);
857 * Detach an existing hook if present.
862 spin_lock(&po->bind_lock);
867 spin_unlock(&po->bind_lock);
868 dev_remove_pack(&po->prot_hook);
869 spin_lock(&po->bind_lock);
873 po->prot_hook.type = protocol;
874 po->prot_hook.dev = dev;
876 po->ifindex = dev ? dev->ifindex : 0;
882 if (dev->flags&IFF_UP) {
883 dev_add_pack(&po->prot_hook);
887 sk->sk_err = ENETDOWN;
888 if (!sock_flag(sk, SOCK_DEAD))
889 sk->sk_error_report(sk);
892 dev_add_pack(&po->prot_hook);
898 spin_unlock(&po->bind_lock);
904 * Bind a packet socket to a device
907 #ifdef CONFIG_SOCK_PACKET
909 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
911 struct sock *sk=sock->sk;
913 struct net_device *dev;
920 if(addr_len!=sizeof(struct sockaddr))
922 strlcpy(name,uaddr->sa_data,sizeof(name));
924 dev = dev_get_by_name(name);
926 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
933 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
935 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
936 struct sock *sk=sock->sk;
937 struct net_device *dev = NULL;
945 if (addr_len < sizeof(struct sockaddr_ll))
947 if (sll->sll_family != AF_PACKET)
950 if (sll->sll_ifindex) {
952 dev = dev_get_by_index(sll->sll_ifindex);
956 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
964 static struct proto packet_proto = {
966 .owner = THIS_MODULE,
967 .obj_size = sizeof(struct packet_sock),
971 * Create a packet of type SOCK_PACKET.
974 static int packet_create(struct socket *sock, int protocol)
977 struct packet_sock *po;
980 if (!capable(CAP_NET_RAW))
982 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
983 #ifdef CONFIG_SOCK_PACKET
984 && sock->type != SOCK_PACKET
987 return -ESOCKTNOSUPPORT;
989 sock->state = SS_UNCONNECTED;
992 sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
996 sock->ops = &packet_ops;
997 #ifdef CONFIG_SOCK_PACKET
998 if (sock->type == SOCK_PACKET)
999 sock->ops = &packet_ops_spkt;
1001 sock_init_data(sock, sk);
1004 sk->sk_family = PF_PACKET;
1007 sk->sk_destruct = packet_sock_destruct;
1008 atomic_inc(&packet_socks_nr);
1011 * Attach a protocol block
1014 spin_lock_init(&po->bind_lock);
1015 po->prot_hook.func = packet_rcv;
1016 #ifdef CONFIG_SOCK_PACKET
1017 if (sock->type == SOCK_PACKET)
1018 po->prot_hook.func = packet_rcv_spkt;
1020 po->prot_hook.af_packet_priv = sk;
1023 po->prot_hook.type = protocol;
1024 dev_add_pack(&po->prot_hook);
1029 write_lock_bh(&packet_sklist_lock);
1030 sk_add_node(sk, &packet_sklist);
1031 write_unlock_bh(&packet_sklist_lock);
1038 * Pull a packet from our receive queue and hand it to the user.
1039 * If necessary we block.
1042 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1043 struct msghdr *msg, size_t len, int flags)
1045 struct sock *sk = sock->sk;
1046 struct sk_buff *skb;
1050 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1054 /* What error should we return now? EUNATTACH? */
1055 if (pkt_sk(sk)->ifindex < 0)
1060 * If the address length field is there to be filled in, we fill
1064 if (sock->type == SOCK_PACKET)
1065 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1067 msg->msg_namelen = sizeof(struct sockaddr_ll);
1070 * Call the generic datagram receiver. This handles all sorts
1071 * of horrible races and re-entrancy so we can forget about it
1072 * in the protocol layers.
1074 * Now it will return ENETDOWN, if device have just gone down,
1075 * but then it will block.
1078 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1081 * An error occurred so return it. Because skb_recv_datagram()
1082 * handles the blocking we don't see and worry about blocking
1090 * You lose any data beyond the buffer you gave. If it worries a
1091 * user program they can ask the device for its MTU anyway.
1098 msg->msg_flags|=MSG_TRUNC;
1101 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1105 sock_recv_timestamp(msg, sk, skb);
1108 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1111 * Free or return the buffer as appropriate. Again this
1112 * hides all the races and re-entrancy issues from us.
1114 err = (flags&MSG_TRUNC) ? skb->len : copied;
1117 skb_free_datagram(sk, skb);
1122 #ifdef CONFIG_SOCK_PACKET
1123 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1124 int *uaddr_len, int peer)
1126 struct net_device *dev;
1127 struct sock *sk = sock->sk;
1132 uaddr->sa_family = AF_PACKET;
1133 dev = dev_get_by_index(pkt_sk(sk)->ifindex);
1135 strlcpy(uaddr->sa_data, dev->name, 15);
1138 memset(uaddr->sa_data, 0, 14);
1139 *uaddr_len = sizeof(*uaddr);
1145 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1146 int *uaddr_len, int peer)
1148 struct net_device *dev;
1149 struct sock *sk = sock->sk;
1150 struct packet_sock *po = pkt_sk(sk);
1151 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1156 sll->sll_family = AF_PACKET;
1157 sll->sll_ifindex = po->ifindex;
1158 sll->sll_protocol = po->num;
1159 dev = dev_get_by_index(po->ifindex);
1161 sll->sll_hatype = dev->type;
1162 sll->sll_halen = dev->addr_len;
1163 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1166 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1169 *uaddr_len = sizeof(*sll);
1174 #ifdef CONFIG_PACKET_MULTICAST
1175 static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1178 case PACKET_MR_MULTICAST:
1180 dev_mc_add(dev, i->addr, i->alen, 0);
1182 dev_mc_delete(dev, i->addr, i->alen, 0);
1184 case PACKET_MR_PROMISC:
1185 dev_set_promiscuity(dev, what);
1187 case PACKET_MR_ALLMULTI:
1188 dev_set_allmulti(dev, what);
1194 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1196 for ( ; i; i=i->next) {
1197 if (i->ifindex == dev->ifindex)
1198 packet_dev_mc(dev, i, what);
1202 static int packet_mc_add(struct sock *sk, struct packet_mreq *mreq)
1204 struct packet_sock *po = pkt_sk(sk);
1205 struct packet_mclist *ml, *i;
1206 struct net_device *dev;
1212 dev = __dev_get_by_index(mreq->mr_ifindex);
1217 if (mreq->mr_alen > dev->addr_len)
1221 i = (struct packet_mclist *)kmalloc(sizeof(*i), GFP_KERNEL);
1226 for (ml = po->mclist; ml; ml = ml->next) {
1227 if (ml->ifindex == mreq->mr_ifindex &&
1228 ml->type == mreq->mr_type &&
1229 ml->alen == mreq->mr_alen &&
1230 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1232 /* Free the new element ... */
1238 i->type = mreq->mr_type;
1239 i->ifindex = mreq->mr_ifindex;
1240 i->alen = mreq->mr_alen;
1241 memcpy(i->addr, mreq->mr_address, i->alen);
1243 i->next = po->mclist;
1245 packet_dev_mc(dev, i, +1);
1252 static int packet_mc_drop(struct sock *sk, struct packet_mreq *mreq)
1254 struct packet_mclist *ml, **mlp;
1258 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1259 if (ml->ifindex == mreq->mr_ifindex &&
1260 ml->type == mreq->mr_type &&
1261 ml->alen == mreq->mr_alen &&
1262 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1263 if (--ml->count == 0) {
1264 struct net_device *dev;
1266 dev = dev_get_by_index(ml->ifindex);
1268 packet_dev_mc(dev, ml, -1);
1278 return -EADDRNOTAVAIL;
1281 static void packet_flush_mclist(struct sock *sk)
1283 struct packet_sock *po = pkt_sk(sk);
1284 struct packet_mclist *ml;
1290 while ((ml = po->mclist) != NULL) {
1291 struct net_device *dev;
1293 po->mclist = ml->next;
1294 if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1295 packet_dev_mc(dev, ml, -1);
1305 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1307 struct sock *sk = sock->sk;
1310 if (level != SOL_PACKET)
1311 return -ENOPROTOOPT;
1314 #ifdef CONFIG_PACKET_MULTICAST
1315 case PACKET_ADD_MEMBERSHIP:
1316 case PACKET_DROP_MEMBERSHIP:
1318 struct packet_mreq mreq;
1319 if (optlen<sizeof(mreq))
1321 if (copy_from_user(&mreq,optval,sizeof(mreq)))
1323 if (optname == PACKET_ADD_MEMBERSHIP)
1324 ret = packet_mc_add(sk, &mreq);
1326 ret = packet_mc_drop(sk, &mreq);
1330 #ifdef CONFIG_PACKET_MMAP
1331 case PACKET_RX_RING:
1333 struct tpacket_req req;
1335 if (optlen<sizeof(req))
1337 if (copy_from_user(&req,optval,sizeof(req)))
1339 return packet_set_ring(sk, &req, 0);
1341 case PACKET_COPY_THRESH:
1345 if (optlen!=sizeof(val))
1347 if (copy_from_user(&val,optval,sizeof(val)))
1350 pkt_sk(sk)->copy_thresh = val;
1355 return -ENOPROTOOPT;
1359 static int packet_getsockopt(struct socket *sock, int level, int optname,
1360 char __user *optval, int __user *optlen)
1363 struct sock *sk = sock->sk;
1364 struct packet_sock *po = pkt_sk(sk);
1366 if (level != SOL_PACKET)
1367 return -ENOPROTOOPT;
1369 if (get_user(len,optlen))
1376 case PACKET_STATISTICS:
1378 struct tpacket_stats st;
1380 if (len > sizeof(struct tpacket_stats))
1381 len = sizeof(struct tpacket_stats);
1382 spin_lock_bh(&sk->sk_receive_queue.lock);
1384 memset(&po->stats, 0, sizeof(st));
1385 spin_unlock_bh(&sk->sk_receive_queue.lock);
1386 st.tp_packets += st.tp_drops;
1388 if (copy_to_user(optval, &st, len))
1393 return -ENOPROTOOPT;
1396 if (put_user(len, optlen))
1402 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1405 struct hlist_node *node;
1406 struct net_device *dev = (struct net_device*)data;
1408 read_lock(&packet_sklist_lock);
1409 sk_for_each(sk, node, &packet_sklist) {
1410 struct packet_sock *po = pkt_sk(sk);
1413 case NETDEV_UNREGISTER:
1414 #ifdef CONFIG_PACKET_MULTICAST
1416 packet_dev_mclist(dev, po->mclist, -1);
1420 if (dev->ifindex == po->ifindex) {
1421 spin_lock(&po->bind_lock);
1423 __dev_remove_pack(&po->prot_hook);
1426 sk->sk_err = ENETDOWN;
1427 if (!sock_flag(sk, SOCK_DEAD))
1428 sk->sk_error_report(sk);
1430 if (msg == NETDEV_UNREGISTER) {
1432 po->prot_hook.dev = NULL;
1434 spin_unlock(&po->bind_lock);
1438 spin_lock(&po->bind_lock);
1439 if (dev->ifindex == po->ifindex && po->num &&
1441 dev_add_pack(&po->prot_hook);
1445 spin_unlock(&po->bind_lock);
1449 read_unlock(&packet_sklist_lock);
1454 static int packet_ioctl(struct socket *sock, unsigned int cmd,
1457 struct sock *sk = sock->sk;
1462 int amount = atomic_read(&sk->sk_wmem_alloc);
1463 return put_user(amount, (int __user *)arg);
1467 struct sk_buff *skb;
1470 spin_lock_bh(&sk->sk_receive_queue.lock);
1471 skb = skb_peek(&sk->sk_receive_queue);
1474 spin_unlock_bh(&sk->sk_receive_queue.lock);
1475 return put_user(amount, (int __user *)arg);
1478 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1488 case SIOCGIFBRDADDR:
1489 case SIOCSIFBRDADDR:
1490 case SIOCGIFNETMASK:
1491 case SIOCSIFNETMASK:
1492 case SIOCGIFDSTADDR:
1493 case SIOCSIFDSTADDR:
1495 return inet_dgram_ops.ioctl(sock, cmd, arg);
1499 return dev_ioctl(cmd, (void __user *)arg);
1504 #ifndef CONFIG_PACKET_MMAP
1505 #define packet_mmap sock_no_mmap
1506 #define packet_poll datagram_poll
1509 static unsigned int packet_poll(struct file * file, struct socket *sock,
1512 struct sock *sk = sock->sk;
1513 struct packet_sock *po = pkt_sk(sk);
1514 unsigned int mask = datagram_poll(file, sock, wait);
1516 spin_lock_bh(&sk->sk_receive_queue.lock);
1518 unsigned last = po->head ? po->head-1 : po->frame_max;
1519 struct tpacket_hdr *h;
1521 h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
1524 mask |= POLLIN | POLLRDNORM;
1526 spin_unlock_bh(&sk->sk_receive_queue.lock);
1531 /* Dirty? Well, I still did not learn better way to account
1535 static void packet_mm_open(struct vm_area_struct *vma)
1537 struct file *file = vma->vm_file;
1538 struct inode *inode = file->f_dentry->d_inode;
1539 struct socket * sock = SOCKET_I(inode);
1540 struct sock *sk = sock->sk;
1543 atomic_inc(&pkt_sk(sk)->mapped);
1546 static void packet_mm_close(struct vm_area_struct *vma)
1548 struct file *file = vma->vm_file;
1549 struct inode *inode = file->f_dentry->d_inode;
1550 struct socket * sock = SOCKET_I(inode);
1551 struct sock *sk = sock->sk;
1554 atomic_dec(&pkt_sk(sk)->mapped);
1557 static struct vm_operations_struct packet_mmap_ops = {
1558 .open = packet_mm_open,
1559 .close =packet_mm_close,
1562 static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
1564 return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
1567 static void free_pg_vec(char **pg_vec, unsigned order, unsigned len)
1571 for (i=0; i<len; i++) {
1573 struct page *page, *pend;
1575 pend = pg_vec_endpage(pg_vec[i], order);
1576 for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1577 ClearPageReserved(page);
1578 free_pages((unsigned long)pg_vec[i], order);
1585 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1587 char **pg_vec = NULL;
1588 struct packet_sock *po = pkt_sk(sk);
1589 int was_running, num, order = 0;
1592 if (req->tp_block_nr) {
1595 /* Sanity tests and some calculations */
1600 if ((int)req->tp_block_size <= 0)
1602 if (req->tp_block_size&(PAGE_SIZE-1))
1604 if (req->tp_frame_size < TPACKET_HDRLEN)
1606 if (req->tp_frame_size&(TPACKET_ALIGNMENT-1))
1609 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1610 if (po->frames_per_block <= 0)
1612 if (po->frames_per_block*req->tp_block_nr != req->tp_frame_nr)
1616 /* Allocate page vector */
1617 while ((PAGE_SIZE<<order) < req->tp_block_size)
1622 pg_vec = kmalloc(req->tp_block_nr*sizeof(char *), GFP_KERNEL);
1625 memset(pg_vec, 0, req->tp_block_nr*sizeof(char **));
1627 for (i=0; i<req->tp_block_nr; i++) {
1628 struct page *page, *pend;
1629 pg_vec[i] = (char *)__get_free_pages(GFP_KERNEL, order);
1631 goto out_free_pgvec;
1633 pend = pg_vec_endpage(pg_vec[i], order);
1634 for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1635 SetPageReserved(page);
1637 /* Page vector is allocated */
1640 for (i=0; i<req->tp_block_nr; i++) {
1641 char *ptr = pg_vec[i];
1642 struct tpacket_hdr *header;
1645 for (k=0; k<po->frames_per_block; k++) {
1647 header = (struct tpacket_hdr*)ptr;
1648 header->tp_status = TP_STATUS_KERNEL;
1649 ptr += req->tp_frame_size;
1654 if (req->tp_frame_nr)
1660 /* Detach socket from network */
1661 spin_lock(&po->bind_lock);
1662 was_running = po->running;
1665 __dev_remove_pack(&po->prot_hook);
1670 spin_unlock(&po->bind_lock);
1675 if (closing || atomic_read(&po->mapped) == 0) {
1677 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1679 spin_lock_bh(&sk->sk_receive_queue.lock);
1680 pg_vec = XC(po->pg_vec, pg_vec);
1681 po->frame_max = req->tp_frame_nr-1;
1683 po->frame_size = req->tp_frame_size;
1684 spin_unlock_bh(&sk->sk_receive_queue.lock);
1686 order = XC(po->pg_vec_order, order);
1687 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1689 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1690 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1691 skb_queue_purge(&sk->sk_receive_queue);
1693 if (atomic_read(&po->mapped))
1694 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1697 spin_lock(&po->bind_lock);
1698 if (was_running && !po->running) {
1702 dev_add_pack(&po->prot_hook);
1704 spin_unlock(&po->bind_lock);
1710 free_pg_vec(pg_vec, order, req->tp_block_nr);
1715 static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1717 struct sock *sk = sock->sk;
1718 struct packet_sock *po = pkt_sk(sk);
1720 unsigned long start;
1727 size = vma->vm_end - vma->vm_start;
1730 if (po->pg_vec == NULL)
1732 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1735 atomic_inc(&po->mapped);
1736 start = vma->vm_start;
1738 for (i=0; i<po->pg_vec_len; i++) {
1739 if (remap_pfn_range(vma, start,
1740 __pa(po->pg_vec[i]) >> PAGE_SHIFT,
1741 po->pg_vec_pages*PAGE_SIZE,
1744 start += po->pg_vec_pages*PAGE_SIZE;
1746 vma->vm_ops = &packet_mmap_ops;
1756 #ifdef CONFIG_SOCK_PACKET
1757 static struct proto_ops packet_ops_spkt = {
1758 .family = PF_PACKET,
1759 .owner = THIS_MODULE,
1760 .release = packet_release,
1761 .bind = packet_bind_spkt,
1762 .connect = sock_no_connect,
1763 .socketpair = sock_no_socketpair,
1764 .accept = sock_no_accept,
1765 .getname = packet_getname_spkt,
1766 .poll = datagram_poll,
1767 .ioctl = packet_ioctl,
1768 .listen = sock_no_listen,
1769 .shutdown = sock_no_shutdown,
1770 .setsockopt = sock_no_setsockopt,
1771 .getsockopt = sock_no_getsockopt,
1772 .sendmsg = packet_sendmsg_spkt,
1773 .recvmsg = packet_recvmsg,
1774 .mmap = sock_no_mmap,
1775 .sendpage = sock_no_sendpage,
1779 static struct proto_ops packet_ops = {
1780 .family = PF_PACKET,
1781 .owner = THIS_MODULE,
1782 .release = packet_release,
1783 .bind = packet_bind,
1784 .connect = sock_no_connect,
1785 .socketpair = sock_no_socketpair,
1786 .accept = sock_no_accept,
1787 .getname = packet_getname,
1788 .poll = packet_poll,
1789 .ioctl = packet_ioctl,
1790 .listen = sock_no_listen,
1791 .shutdown = sock_no_shutdown,
1792 .setsockopt = packet_setsockopt,
1793 .getsockopt = packet_getsockopt,
1794 .sendmsg = packet_sendmsg,
1795 .recvmsg = packet_recvmsg,
1796 .mmap = packet_mmap,
1797 .sendpage = sock_no_sendpage,
1800 static struct net_proto_family packet_family_ops = {
1801 .family = PF_PACKET,
1802 .create = packet_create,
1803 .owner = THIS_MODULE,
1806 static struct notifier_block packet_netdev_notifier = {
1807 .notifier_call =packet_notifier,
1810 #ifdef CONFIG_PROC_FS
1811 static inline struct sock *packet_seq_idx(loff_t off)
1814 struct hlist_node *node;
1816 sk_for_each(s, node, &packet_sklist) {
1823 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1825 read_lock(&packet_sklist_lock);
1826 return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1829 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1832 return (v == SEQ_START_TOKEN)
1833 ? sk_head(&packet_sklist)
1834 : sk_next((struct sock*)v) ;
1837 static void packet_seq_stop(struct seq_file *seq, void *v)
1839 read_unlock(&packet_sklist_lock);
1842 static int packet_seq_show(struct seq_file *seq, void *v)
1844 if (v == SEQ_START_TOKEN)
1845 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1848 const struct packet_sock *po = pkt_sk(s);
1851 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1853 atomic_read(&s->sk_refcnt),
1858 atomic_read(&s->sk_rmem_alloc),
1866 static struct seq_operations packet_seq_ops = {
1867 .start = packet_seq_start,
1868 .next = packet_seq_next,
1869 .stop = packet_seq_stop,
1870 .show = packet_seq_show,
1873 static int packet_seq_open(struct inode *inode, struct file *file)
1875 return seq_open(file, &packet_seq_ops);
1878 static struct file_operations packet_seq_fops = {
1879 .owner = THIS_MODULE,
1880 .open = packet_seq_open,
1882 .llseek = seq_lseek,
1883 .release = seq_release,
1888 static void __exit packet_exit(void)
1890 proc_net_remove("packet");
1891 unregister_netdevice_notifier(&packet_netdev_notifier);
1892 sock_unregister(PF_PACKET);
1893 proto_unregister(&packet_proto);
1896 static int __init packet_init(void)
1898 int rc = proto_register(&packet_proto, 0);
1903 sock_register(&packet_family_ops);
1904 register_netdevice_notifier(&packet_netdev_notifier);
1905 proc_net_fops_create("packet", 0, &packet_seq_fops);
1910 module_init(packet_init);
1911 module_exit(packet_exit);
1912 MODULE_LICENSE("GPL");
1913 MODULE_ALIAS_NETPROTO(PF_PACKET);