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>
64 #include <net/net_namespace.h>
66 #include <net/protocol.h>
67 #include <linux/skbuff.h>
69 #include <linux/errno.h>
70 #include <linux/timer.h>
71 #include <asm/system.h>
72 #include <asm/uaccess.h>
73 #include <asm/ioctls.h>
75 #include <asm/cacheflush.h>
77 #include <linux/proc_fs.h>
78 #include <linux/seq_file.h>
79 #include <linux/poll.h>
80 #include <linux/module.h>
81 #include <linux/init.h>
84 #include <net/inet_common.h>
89 - if device has no dev->hard_header routine, it adds and removes ll header
90 inside itself. In this case ll header is invisible outside of device,
91 but higher levels still should reserve dev->hard_header_len.
92 Some devices are enough clever to reallocate skb, when header
93 will not fit to reserved space (tunnel), another ones are silly
95 - packet socket receives packets with pulled ll header,
96 so that SOCK_RAW should push it back.
101 Incoming, dev->hard_header!=NULL
102 mac_header -> ll header
105 Outgoing, dev->hard_header!=NULL
106 mac_header -> ll header
109 Incoming, dev->hard_header==NULL
110 mac_header -> UNKNOWN position. It is very likely, that it points to ll
111 header. PPP makes it, that is wrong, because introduce
112 assymetry between rx and tx paths.
115 Outgoing, dev->hard_header==NULL
116 mac_header -> data. ll header is still not built!
120 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
126 dev->hard_header != NULL
127 mac_header -> ll header
130 dev->hard_header == NULL (ll header is added by device, we cannot control it)
134 We should set nh.raw on output to correct posistion,
135 packet classifier depends on it.
138 /* Private packet socket structures. */
142 struct packet_mclist *next;
147 unsigned char addr[MAX_ADDR_LEN];
149 /* identical to struct packet_mreq except it has
150 * a longer address field.
152 struct packet_mreq_max
155 unsigned short mr_type;
156 unsigned short mr_alen;
157 unsigned char mr_address[MAX_ADDR_LEN];
160 #ifdef CONFIG_PACKET_MMAP
161 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
164 static void packet_flush_mclist(struct sock *sk);
167 /* struct sock has to be the first member of packet_sock */
169 struct tpacket_stats stats;
170 #ifdef CONFIG_PACKET_MMAP
173 unsigned int frames_per_block;
174 unsigned int frame_size;
175 unsigned int frame_max;
178 struct packet_type prot_hook;
179 spinlock_t bind_lock;
180 unsigned int running:1, /* prot_hook is attached*/
183 int ifindex; /* bound device */
185 struct packet_mclist *mclist;
186 #ifdef CONFIG_PACKET_MMAP
188 unsigned int pg_vec_order;
189 unsigned int pg_vec_pages;
190 unsigned int pg_vec_len;
194 struct packet_skb_cb {
195 unsigned int origlen;
197 struct sockaddr_pkt pkt;
198 struct sockaddr_ll ll;
202 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
204 #ifdef CONFIG_PACKET_MMAP
206 static inline struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position)
208 unsigned int pg_vec_pos, frame_offset;
210 pg_vec_pos = position / po->frames_per_block;
211 frame_offset = position % po->frames_per_block;
213 return (struct tpacket_hdr *)(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 sk_refcnt_debug_dec(sk);
236 static const struct proto_ops packet_ops;
238 static const struct proto_ops packet_ops_spkt;
240 static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
243 struct sockaddr_pkt *spkt;
246 * When we registered the protocol we saved the socket in the data
247 * field for just this event.
250 sk = pt->af_packet_priv;
253 * Yank back the headers [hope the device set this
254 * right or kerboom...]
256 * Incoming packets have ll header pulled,
259 * For outgoing ones skb->data == skb_mac_header(skb)
260 * so that this procedure is noop.
263 if (skb->pkt_type == PACKET_LOOPBACK)
266 if (dev_net(dev) != sock_net(sk))
269 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
272 /* drop any routing info */
273 dst_release(skb->dst);
276 /* drop conntrack reference */
279 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
281 skb_push(skb, skb->data - skb_mac_header(skb));
284 * The SOCK_PACKET socket receives _all_ frames.
287 spkt->spkt_family = dev->type;
288 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
289 spkt->spkt_protocol = skb->protocol;
292 * Charge the memory to the socket. This is done specifically
293 * to prevent sockets using all the memory up.
296 if (sock_queue_rcv_skb(sk,skb) == 0)
307 * Output a raw packet to a device layer. This bypasses all the other
308 * protocol layers and you must therefore supply it with a complete frame
311 static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
312 struct msghdr *msg, size_t len)
314 struct sock *sk = sock->sk;
315 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
317 struct net_device *dev;
322 * Get and verify the address.
327 if (msg->msg_namelen < sizeof(struct sockaddr))
329 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
330 proto=saddr->spkt_protocol;
333 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
336 * Find the device first to size check it
339 saddr->spkt_device[13] = 0;
340 dev = dev_get_by_name(sock_net(sk), saddr->spkt_device);
346 if (!(dev->flags & IFF_UP))
350 * You may not queue a frame bigger than the mtu. This is the lowest level
351 * raw protocol and you must do your own fragmentation at this level.
355 if (len > dev->mtu + dev->hard_header_len)
359 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
362 * If the write buffer is full, then tough. At this level the user gets to
363 * deal with the problem - do your own algorithmic backoffs. That's far
374 /* FIXME: Save some space for broken drivers that write a
375 * hard header at transmission time by themselves. PPP is the
376 * notable one here. This should really be fixed at the driver level.
378 skb_reserve(skb, LL_RESERVED_SPACE(dev));
379 skb_reset_network_header(skb);
381 /* Try to align data part correctly */
382 if (dev->header_ops) {
383 skb->data -= dev->hard_header_len;
384 skb->tail -= dev->hard_header_len;
385 if (len < dev->hard_header_len)
386 skb_reset_network_header(skb);
389 /* Returns -EFAULT on error */
390 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
391 skb->protocol = proto;
393 skb->priority = sk->sk_priority;
413 static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
416 struct sk_filter *filter;
419 filter = rcu_dereference(sk->sk_filter);
421 res = sk_run_filter(skb, filter->insns, filter->len);
422 rcu_read_unlock_bh();
428 This function makes lazy skb cloning in hope that most of packets
429 are discarded by BPF.
431 Note tricky part: we DO mangle shared skb! skb->data, skb->len
432 and skb->cb are mangled. It works because (and until) packets
433 falling here are owned by current CPU. Output packets are cloned
434 by dev_queue_xmit_nit(), input packets are processed by net_bh
435 sequencially, so that if we return skb to original state on exit,
436 we will not harm anyone.
439 static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
442 struct sockaddr_ll *sll;
443 struct packet_sock *po;
444 u8 * skb_head = skb->data;
445 int skb_len = skb->len;
446 unsigned int snaplen, res;
448 if (skb->pkt_type == PACKET_LOOPBACK)
451 sk = pt->af_packet_priv;
454 if (dev_net(dev) != sock_net(sk))
459 if (dev->header_ops) {
460 /* The device has an explicit notion of ll header,
461 exported to higher levels.
463 Otherwise, the device hides datails of it frame
464 structure, so that corresponding packet head
465 never delivered to user.
467 if (sk->sk_type != SOCK_DGRAM)
468 skb_push(skb, skb->data - skb_mac_header(skb));
469 else if (skb->pkt_type == PACKET_OUTGOING) {
470 /* Special case: outgoing packets have ll header at head */
471 skb_pull(skb, skb_network_offset(skb));
477 res = run_filter(skb, sk, snaplen);
483 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
484 (unsigned)sk->sk_rcvbuf)
487 if (skb_shared(skb)) {
488 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
492 if (skb_head != skb->data) {
493 skb->data = skb_head;
500 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
503 sll = &PACKET_SKB_CB(skb)->sa.ll;
504 sll->sll_family = AF_PACKET;
505 sll->sll_hatype = dev->type;
506 sll->sll_protocol = skb->protocol;
507 sll->sll_pkttype = skb->pkt_type;
508 if (unlikely(po->origdev))
509 sll->sll_ifindex = orig_dev->ifindex;
511 sll->sll_ifindex = dev->ifindex;
513 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
515 PACKET_SKB_CB(skb)->origlen = skb->len;
517 if (pskb_trim(skb, snaplen))
520 skb_set_owner_r(skb, sk);
522 dst_release(skb->dst);
525 /* drop conntrack reference */
528 spin_lock(&sk->sk_receive_queue.lock);
529 po->stats.tp_packets++;
530 __skb_queue_tail(&sk->sk_receive_queue, skb);
531 spin_unlock(&sk->sk_receive_queue.lock);
532 sk->sk_data_ready(sk, skb->len);
536 spin_lock(&sk->sk_receive_queue.lock);
537 po->stats.tp_drops++;
538 spin_unlock(&sk->sk_receive_queue.lock);
541 if (skb_head != skb->data && skb_shared(skb)) {
542 skb->data = skb_head;
550 #ifdef CONFIG_PACKET_MMAP
551 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
554 struct packet_sock *po;
555 struct sockaddr_ll *sll;
556 struct tpacket_hdr *h;
557 u8 * skb_head = skb->data;
558 int skb_len = skb->len;
559 unsigned int snaplen, res;
560 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
561 unsigned short macoff, netoff;
562 struct sk_buff *copy_skb = NULL;
565 if (skb->pkt_type == PACKET_LOOPBACK)
568 sk = pt->af_packet_priv;
571 if (dev_net(dev) != sock_net(sk))
574 if (dev->header_ops) {
575 if (sk->sk_type != SOCK_DGRAM)
576 skb_push(skb, skb->data - skb_mac_header(skb));
577 else if (skb->pkt_type == PACKET_OUTGOING) {
578 /* Special case: outgoing packets have ll header at head */
579 skb_pull(skb, skb_network_offset(skb));
583 if (skb->ip_summed == CHECKSUM_PARTIAL)
584 status |= TP_STATUS_CSUMNOTREADY;
588 res = run_filter(skb, sk, snaplen);
594 if (sk->sk_type == SOCK_DGRAM) {
595 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
597 unsigned maclen = skb_network_offset(skb);
598 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
599 macoff = netoff - maclen;
602 if (macoff + snaplen > po->frame_size) {
603 if (po->copy_thresh &&
604 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
605 (unsigned)sk->sk_rcvbuf) {
606 if (skb_shared(skb)) {
607 copy_skb = skb_clone(skb, GFP_ATOMIC);
609 copy_skb = skb_get(skb);
610 skb_head = skb->data;
613 skb_set_owner_r(copy_skb, sk);
615 snaplen = po->frame_size - macoff;
616 if ((int)snaplen < 0)
620 spin_lock(&sk->sk_receive_queue.lock);
621 h = packet_lookup_frame(po, po->head);
625 po->head = po->head != po->frame_max ? po->head+1 : 0;
626 po->stats.tp_packets++;
628 status |= TP_STATUS_COPY;
629 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
631 if (!po->stats.tp_drops)
632 status &= ~TP_STATUS_LOSING;
633 spin_unlock(&sk->sk_receive_queue.lock);
635 skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
637 h->tp_len = skb->len;
638 h->tp_snaplen = snaplen;
641 if (skb->tstamp.tv64)
642 tv = ktime_to_timeval(skb->tstamp);
644 do_gettimeofday(&tv);
645 h->tp_sec = tv.tv_sec;
646 h->tp_usec = tv.tv_usec;
648 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
649 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
650 sll->sll_family = AF_PACKET;
651 sll->sll_hatype = dev->type;
652 sll->sll_protocol = skb->protocol;
653 sll->sll_pkttype = skb->pkt_type;
654 if (unlikely(po->origdev))
655 sll->sll_ifindex = orig_dev->ifindex;
657 sll->sll_ifindex = dev->ifindex;
659 h->tp_status = status;
663 struct page *p_start, *p_end;
664 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
666 p_start = virt_to_page(h);
667 p_end = virt_to_page(h_end);
668 while (p_start <= p_end) {
669 flush_dcache_page(p_start);
674 sk->sk_data_ready(sk, 0);
677 if (skb_head != skb->data && skb_shared(skb)) {
678 skb->data = skb_head;
686 po->stats.tp_drops++;
687 spin_unlock(&sk->sk_receive_queue.lock);
689 sk->sk_data_ready(sk, 0);
698 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
699 struct msghdr *msg, size_t len)
701 struct sock *sk = sock->sk;
702 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
704 struct net_device *dev;
707 int ifindex, err, reserve = 0;
710 * Get and verify the address.
714 struct packet_sock *po = pkt_sk(sk);
716 ifindex = po->ifindex;
721 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
723 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
725 ifindex = saddr->sll_ifindex;
726 proto = saddr->sll_protocol;
727 addr = saddr->sll_addr;
731 dev = dev_get_by_index(sock_net(sk), ifindex);
735 if (sock->type == SOCK_RAW)
736 reserve = dev->hard_header_len;
739 if (!(dev->flags & IFF_UP))
743 if (len > dev->mtu+reserve)
746 skb = sock_alloc_send_skb(sk, len + LL_ALLOCATED_SPACE(dev),
747 msg->msg_flags & MSG_DONTWAIT, &err);
751 skb_reserve(skb, LL_RESERVED_SPACE(dev));
752 skb_reset_network_header(skb);
755 if (sock->type == SOCK_DGRAM &&
756 dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len) < 0)
759 /* Returns -EFAULT on error */
760 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
764 skb->protocol = proto;
766 skb->priority = sk->sk_priority;
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;
806 write_lock_bh(&net->packet.sklist_lock);
807 sk_del_node_init(sk);
808 write_unlock_bh(&net->packet.sklist_lock);
811 * Unhook packet receive handler.
816 * Remove the protocol hook
818 dev_remove_pack(&po->prot_hook);
824 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);
844 sk_refcnt_debug_release(sk);
851 * Attach a packet hook.
854 static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
856 struct packet_sock *po = pkt_sk(sk);
858 * Detach an existing hook if present.
863 spin_lock(&po->bind_lock);
868 spin_unlock(&po->bind_lock);
869 dev_remove_pack(&po->prot_hook);
870 spin_lock(&po->bind_lock);
874 po->prot_hook.type = protocol;
875 po->prot_hook.dev = dev;
877 po->ifindex = dev ? dev->ifindex : 0;
882 if (!dev || (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);
893 spin_unlock(&po->bind_lock);
899 * Bind a packet socket to a device
902 static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
904 struct sock *sk=sock->sk;
906 struct net_device *dev;
913 if (addr_len != sizeof(struct sockaddr))
915 strlcpy(name,uaddr->sa_data,sizeof(name));
917 dev = dev_get_by_name(sock_net(sk), name);
919 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
925 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
927 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
928 struct sock *sk=sock->sk;
929 struct net_device *dev = NULL;
937 if (addr_len < sizeof(struct sockaddr_ll))
939 if (sll->sll_family != AF_PACKET)
942 if (sll->sll_ifindex) {
944 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
948 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
956 static struct proto packet_proto = {
958 .owner = THIS_MODULE,
959 .obj_size = sizeof(struct packet_sock),
963 * Create a packet of type SOCK_PACKET.
966 static int packet_create(struct net *net, struct socket *sock, int protocol)
969 struct packet_sock *po;
970 __be16 proto = (__force __be16)protocol; /* weird, but documented */
973 if (!capable(CAP_NET_RAW))
975 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
976 sock->type != SOCK_PACKET)
977 return -ESOCKTNOSUPPORT;
979 sock->state = SS_UNCONNECTED;
982 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
986 sock->ops = &packet_ops;
987 if (sock->type == SOCK_PACKET)
988 sock->ops = &packet_ops_spkt;
990 sock_init_data(sock, sk);
993 sk->sk_family = PF_PACKET;
996 sk->sk_destruct = packet_sock_destruct;
997 sk_refcnt_debug_inc(sk);
1000 * Attach a protocol block
1003 spin_lock_init(&po->bind_lock);
1004 po->prot_hook.func = packet_rcv;
1006 if (sock->type == SOCK_PACKET)
1007 po->prot_hook.func = packet_rcv_spkt;
1009 po->prot_hook.af_packet_priv = sk;
1012 po->prot_hook.type = proto;
1013 dev_add_pack(&po->prot_hook);
1018 write_lock_bh(&net->packet.sklist_lock);
1019 sk_add_node(sk, &net->packet.sklist);
1020 write_unlock_bh(&net->packet.sklist_lock);
1027 * Pull a packet from our receive queue and hand it to the user.
1028 * If necessary we block.
1031 static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1032 struct msghdr *msg, size_t len, int flags)
1034 struct sock *sk = sock->sk;
1035 struct sk_buff *skb;
1037 struct sockaddr_ll *sll;
1040 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1044 /* What error should we return now? EUNATTACH? */
1045 if (pkt_sk(sk)->ifindex < 0)
1050 * Call the generic datagram receiver. This handles all sorts
1051 * of horrible races and re-entrancy so we can forget about it
1052 * in the protocol layers.
1054 * Now it will return ENETDOWN, if device have just gone down,
1055 * but then it will block.
1058 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1061 * An error occurred so return it. Because skb_recv_datagram()
1062 * handles the blocking we don't see and worry about blocking
1070 * If the address length field is there to be filled in, we fill
1074 sll = &PACKET_SKB_CB(skb)->sa.ll;
1075 if (sock->type == SOCK_PACKET)
1076 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1078 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1081 * You lose any data beyond the buffer you gave. If it worries a
1082 * user program they can ask the device for its MTU anyway.
1089 msg->msg_flags|=MSG_TRUNC;
1092 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1096 sock_recv_timestamp(msg, sk, skb);
1099 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1102 if (pkt_sk(sk)->auxdata) {
1103 struct tpacket_auxdata aux;
1105 aux.tp_status = TP_STATUS_USER;
1106 if (skb->ip_summed == CHECKSUM_PARTIAL)
1107 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1108 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1109 aux.tp_snaplen = skb->len;
1111 aux.tp_net = skb_network_offset(skb);
1113 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
1117 * Free or return the buffer as appropriate. Again this
1118 * hides all the races and re-entrancy issues from us.
1120 err = (flags&MSG_TRUNC) ? skb->len : copied;
1123 skb_free_datagram(sk, skb);
1128 static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1129 int *uaddr_len, int peer)
1131 struct net_device *dev;
1132 struct sock *sk = sock->sk;
1137 uaddr->sa_family = AF_PACKET;
1138 dev = dev_get_by_index(sock_net(sk), pkt_sk(sk)->ifindex);
1140 strlcpy(uaddr->sa_data, dev->name, 15);
1143 memset(uaddr->sa_data, 0, 14);
1144 *uaddr_len = sizeof(*uaddr);
1149 static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1150 int *uaddr_len, int peer)
1152 struct net_device *dev;
1153 struct sock *sk = sock->sk;
1154 struct packet_sock *po = pkt_sk(sk);
1155 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1160 sll->sll_family = AF_PACKET;
1161 sll->sll_ifindex = po->ifindex;
1162 sll->sll_protocol = po->num;
1163 dev = dev_get_by_index(sock_net(sk), po->ifindex);
1165 sll->sll_hatype = dev->type;
1166 sll->sll_halen = dev->addr_len;
1167 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1170 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1173 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
1178 static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1181 case PACKET_MR_MULTICAST:
1183 dev_mc_add(dev, i->addr, i->alen, 0);
1185 dev_mc_delete(dev, i->addr, i->alen, 0);
1187 case PACKET_MR_PROMISC:
1188 dev_set_promiscuity(dev, what);
1190 case PACKET_MR_ALLMULTI:
1191 dev_set_allmulti(dev, what);
1197 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1199 for ( ; i; i=i->next) {
1200 if (i->ifindex == dev->ifindex)
1201 packet_dev_mc(dev, i, what);
1205 static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
1207 struct packet_sock *po = pkt_sk(sk);
1208 struct packet_mclist *ml, *i;
1209 struct net_device *dev;
1215 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
1220 if (mreq->mr_alen > dev->addr_len)
1224 i = kmalloc(sizeof(*i), GFP_KERNEL);
1229 for (ml = po->mclist; ml; ml = ml->next) {
1230 if (ml->ifindex == mreq->mr_ifindex &&
1231 ml->type == mreq->mr_type &&
1232 ml->alen == mreq->mr_alen &&
1233 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1235 /* Free the new element ... */
1241 i->type = mreq->mr_type;
1242 i->ifindex = mreq->mr_ifindex;
1243 i->alen = mreq->mr_alen;
1244 memcpy(i->addr, mreq->mr_address, i->alen);
1246 i->next = po->mclist;
1248 packet_dev_mc(dev, i, +1);
1255 static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
1257 struct packet_mclist *ml, **mlp;
1261 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1262 if (ml->ifindex == mreq->mr_ifindex &&
1263 ml->type == mreq->mr_type &&
1264 ml->alen == mreq->mr_alen &&
1265 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1266 if (--ml->count == 0) {
1267 struct net_device *dev;
1269 dev = dev_get_by_index(sock_net(sk), ml->ifindex);
1271 packet_dev_mc(dev, ml, -1);
1281 return -EADDRNOTAVAIL;
1284 static void packet_flush_mclist(struct sock *sk)
1286 struct packet_sock *po = pkt_sk(sk);
1287 struct packet_mclist *ml;
1293 while ((ml = po->mclist) != NULL) {
1294 struct net_device *dev;
1296 po->mclist = ml->next;
1297 if ((dev = dev_get_by_index(sock_net(sk), ml->ifindex)) != NULL) {
1298 packet_dev_mc(dev, ml, -1);
1307 packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1309 struct sock *sk = sock->sk;
1310 struct packet_sock *po = pkt_sk(sk);
1313 if (level != SOL_PACKET)
1314 return -ENOPROTOOPT;
1317 case PACKET_ADD_MEMBERSHIP:
1318 case PACKET_DROP_MEMBERSHIP:
1320 struct packet_mreq_max mreq;
1322 memset(&mreq, 0, sizeof(mreq));
1323 if (len < sizeof(struct packet_mreq))
1325 if (len > sizeof(mreq))
1327 if (copy_from_user(&mreq,optval,len))
1329 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1331 if (optname == PACKET_ADD_MEMBERSHIP)
1332 ret = packet_mc_add(sk, &mreq);
1334 ret = packet_mc_drop(sk, &mreq);
1338 #ifdef CONFIG_PACKET_MMAP
1339 case PACKET_RX_RING:
1341 struct tpacket_req req;
1343 if (optlen<sizeof(req))
1345 if (copy_from_user(&req,optval,sizeof(req)))
1347 return packet_set_ring(sk, &req, 0);
1349 case PACKET_COPY_THRESH:
1353 if (optlen!=sizeof(val))
1355 if (copy_from_user(&val,optval,sizeof(val)))
1358 pkt_sk(sk)->copy_thresh = val;
1362 case PACKET_AUXDATA:
1366 if (optlen < sizeof(val))
1368 if (copy_from_user(&val, optval, sizeof(val)))
1371 po->auxdata = !!val;
1374 case PACKET_ORIGDEV:
1378 if (optlen < sizeof(val))
1380 if (copy_from_user(&val, optval, sizeof(val)))
1383 po->origdev = !!val;
1387 return -ENOPROTOOPT;
1391 static int packet_getsockopt(struct socket *sock, int level, int optname,
1392 char __user *optval, int __user *optlen)
1396 struct sock *sk = sock->sk;
1397 struct packet_sock *po = pkt_sk(sk);
1399 struct tpacket_stats st;
1401 if (level != SOL_PACKET)
1402 return -ENOPROTOOPT;
1404 if (get_user(len, optlen))
1411 case PACKET_STATISTICS:
1412 if (len > sizeof(struct tpacket_stats))
1413 len = sizeof(struct tpacket_stats);
1414 spin_lock_bh(&sk->sk_receive_queue.lock);
1416 memset(&po->stats, 0, sizeof(st));
1417 spin_unlock_bh(&sk->sk_receive_queue.lock);
1418 st.tp_packets += st.tp_drops;
1422 case PACKET_AUXDATA:
1423 if (len > sizeof(int))
1429 case PACKET_ORIGDEV:
1430 if (len > sizeof(int))
1437 return -ENOPROTOOPT;
1440 if (put_user(len, optlen))
1442 if (copy_to_user(optval, data, len))
1448 static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1451 struct hlist_node *node;
1452 struct net_device *dev = data;
1453 struct net *net = dev_net(dev);
1455 read_lock(&net->packet.sklist_lock);
1456 sk_for_each(sk, node, &net->packet.sklist) {
1457 struct packet_sock *po = pkt_sk(sk);
1460 case NETDEV_UNREGISTER:
1462 packet_dev_mclist(dev, po->mclist, -1);
1466 if (dev->ifindex == po->ifindex) {
1467 spin_lock(&po->bind_lock);
1469 __dev_remove_pack(&po->prot_hook);
1472 sk->sk_err = ENETDOWN;
1473 if (!sock_flag(sk, SOCK_DEAD))
1474 sk->sk_error_report(sk);
1476 if (msg == NETDEV_UNREGISTER) {
1478 po->prot_hook.dev = NULL;
1480 spin_unlock(&po->bind_lock);
1484 spin_lock(&po->bind_lock);
1485 if (dev->ifindex == po->ifindex && po->num &&
1487 dev_add_pack(&po->prot_hook);
1491 spin_unlock(&po->bind_lock);
1495 read_unlock(&net->packet.sklist_lock);
1500 static int packet_ioctl(struct socket *sock, unsigned int cmd,
1503 struct sock *sk = sock->sk;
1508 int amount = atomic_read(&sk->sk_wmem_alloc);
1509 return put_user(amount, (int __user *)arg);
1513 struct sk_buff *skb;
1516 spin_lock_bh(&sk->sk_receive_queue.lock);
1517 skb = skb_peek(&sk->sk_receive_queue);
1520 spin_unlock_bh(&sk->sk_receive_queue.lock);
1521 return put_user(amount, (int __user *)arg);
1524 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1526 return sock_get_timestampns(sk, (struct timespec __user *)arg);
1536 case SIOCGIFBRDADDR:
1537 case SIOCSIFBRDADDR:
1538 case SIOCGIFNETMASK:
1539 case SIOCSIFNETMASK:
1540 case SIOCGIFDSTADDR:
1541 case SIOCSIFDSTADDR:
1543 if (sock_net(sk) != &init_net)
1544 return -ENOIOCTLCMD;
1545 return inet_dgram_ops.ioctl(sock, cmd, arg);
1549 return -ENOIOCTLCMD;
1554 #ifndef CONFIG_PACKET_MMAP
1555 #define packet_mmap sock_no_mmap
1556 #define packet_poll datagram_poll
1559 static unsigned int packet_poll(struct file * file, struct socket *sock,
1562 struct sock *sk = sock->sk;
1563 struct packet_sock *po = pkt_sk(sk);
1564 unsigned int mask = datagram_poll(file, sock, wait);
1566 spin_lock_bh(&sk->sk_receive_queue.lock);
1568 unsigned last = po->head ? po->head-1 : po->frame_max;
1569 struct tpacket_hdr *h;
1571 h = packet_lookup_frame(po, last);
1574 mask |= POLLIN | POLLRDNORM;
1576 spin_unlock_bh(&sk->sk_receive_queue.lock);
1581 /* Dirty? Well, I still did not learn better way to account
1585 static void packet_mm_open(struct vm_area_struct *vma)
1587 struct file *file = vma->vm_file;
1588 struct socket * sock = file->private_data;
1589 struct sock *sk = sock->sk;
1592 atomic_inc(&pkt_sk(sk)->mapped);
1595 static void packet_mm_close(struct vm_area_struct *vma)
1597 struct file *file = vma->vm_file;
1598 struct socket * sock = file->private_data;
1599 struct sock *sk = sock->sk;
1602 atomic_dec(&pkt_sk(sk)->mapped);
1605 static struct vm_operations_struct packet_mmap_ops = {
1606 .open = packet_mm_open,
1607 .close =packet_mm_close,
1610 static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
1614 for (i = 0; i < len; i++) {
1615 if (likely(pg_vec[i]))
1616 free_pages((unsigned long) pg_vec[i], order);
1621 static inline char *alloc_one_pg_vec_page(unsigned long order)
1623 return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
1627 static char **alloc_pg_vec(struct tpacket_req *req, int order)
1629 unsigned int block_nr = req->tp_block_nr;
1633 pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
1634 if (unlikely(!pg_vec))
1637 for (i = 0; i < block_nr; i++) {
1638 pg_vec[i] = alloc_one_pg_vec_page(order);
1639 if (unlikely(!pg_vec[i]))
1640 goto out_free_pgvec;
1647 free_pg_vec(pg_vec, order, block_nr);
1652 static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1654 char **pg_vec = NULL;
1655 struct packet_sock *po = pkt_sk(sk);
1656 int was_running, order = 0;
1660 if (req->tp_block_nr) {
1663 /* Sanity tests and some calculations */
1665 if (unlikely(po->pg_vec))
1668 if (unlikely((int)req->tp_block_size <= 0))
1670 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
1672 if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
1674 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
1677 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1678 if (unlikely(po->frames_per_block <= 0))
1680 if (unlikely((po->frames_per_block * req->tp_block_nr) !=
1685 order = get_order(req->tp_block_size);
1686 pg_vec = alloc_pg_vec(req, order);
1687 if (unlikely(!pg_vec))
1690 for (i = 0; i < req->tp_block_nr; i++) {
1691 char *ptr = pg_vec[i];
1692 struct tpacket_hdr *header;
1695 for (k = 0; k < po->frames_per_block; k++) {
1696 header = (struct tpacket_hdr *) ptr;
1697 header->tp_status = TP_STATUS_KERNEL;
1698 ptr += req->tp_frame_size;
1703 if (unlikely(req->tp_frame_nr))
1709 /* Detach socket from network */
1710 spin_lock(&po->bind_lock);
1711 was_running = po->running;
1714 __dev_remove_pack(&po->prot_hook);
1719 spin_unlock(&po->bind_lock);
1724 if (closing || atomic_read(&po->mapped) == 0) {
1726 #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1728 spin_lock_bh(&sk->sk_receive_queue.lock);
1729 pg_vec = XC(po->pg_vec, pg_vec);
1730 po->frame_max = (req->tp_frame_nr - 1);
1732 po->frame_size = req->tp_frame_size;
1733 spin_unlock_bh(&sk->sk_receive_queue.lock);
1735 order = XC(po->pg_vec_order, order);
1736 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1738 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1739 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1740 skb_queue_purge(&sk->sk_receive_queue);
1742 if (atomic_read(&po->mapped))
1743 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1746 spin_lock(&po->bind_lock);
1747 if (was_running && !po->running) {
1751 dev_add_pack(&po->prot_hook);
1753 spin_unlock(&po->bind_lock);
1758 free_pg_vec(pg_vec, order, req->tp_block_nr);
1763 static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1765 struct sock *sk = sock->sk;
1766 struct packet_sock *po = pkt_sk(sk);
1768 unsigned long start;
1775 size = vma->vm_end - vma->vm_start;
1778 if (po->pg_vec == NULL)
1780 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1783 start = vma->vm_start;
1784 for (i = 0; i < po->pg_vec_len; i++) {
1785 struct page *page = virt_to_page(po->pg_vec[i]);
1788 for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
1789 err = vm_insert_page(vma, start, page);
1795 atomic_inc(&po->mapped);
1796 vma->vm_ops = &packet_mmap_ops;
1806 static const struct proto_ops packet_ops_spkt = {
1807 .family = PF_PACKET,
1808 .owner = THIS_MODULE,
1809 .release = packet_release,
1810 .bind = packet_bind_spkt,
1811 .connect = sock_no_connect,
1812 .socketpair = sock_no_socketpair,
1813 .accept = sock_no_accept,
1814 .getname = packet_getname_spkt,
1815 .poll = datagram_poll,
1816 .ioctl = packet_ioctl,
1817 .listen = sock_no_listen,
1818 .shutdown = sock_no_shutdown,
1819 .setsockopt = sock_no_setsockopt,
1820 .getsockopt = sock_no_getsockopt,
1821 .sendmsg = packet_sendmsg_spkt,
1822 .recvmsg = packet_recvmsg,
1823 .mmap = sock_no_mmap,
1824 .sendpage = sock_no_sendpage,
1827 static const struct proto_ops packet_ops = {
1828 .family = PF_PACKET,
1829 .owner = THIS_MODULE,
1830 .release = packet_release,
1831 .bind = packet_bind,
1832 .connect = sock_no_connect,
1833 .socketpair = sock_no_socketpair,
1834 .accept = sock_no_accept,
1835 .getname = packet_getname,
1836 .poll = packet_poll,
1837 .ioctl = packet_ioctl,
1838 .listen = sock_no_listen,
1839 .shutdown = sock_no_shutdown,
1840 .setsockopt = packet_setsockopt,
1841 .getsockopt = packet_getsockopt,
1842 .sendmsg = packet_sendmsg,
1843 .recvmsg = packet_recvmsg,
1844 .mmap = packet_mmap,
1845 .sendpage = sock_no_sendpage,
1848 static struct net_proto_family packet_family_ops = {
1849 .family = PF_PACKET,
1850 .create = packet_create,
1851 .owner = THIS_MODULE,
1854 static struct notifier_block packet_netdev_notifier = {
1855 .notifier_call =packet_notifier,
1858 #ifdef CONFIG_PROC_FS
1859 static inline struct sock *packet_seq_idx(struct net *net, loff_t off)
1862 struct hlist_node *node;
1864 sk_for_each(s, node, &net->packet.sklist) {
1871 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1872 __acquires(seq_file_net(seq)->packet.sklist_lock)
1874 struct net *net = seq_file_net(seq);
1875 read_lock(&net->packet.sklist_lock);
1876 return *pos ? packet_seq_idx(net, *pos - 1) : SEQ_START_TOKEN;
1879 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1881 struct net *net = seq_file_net(seq);
1883 return (v == SEQ_START_TOKEN)
1884 ? sk_head(&net->packet.sklist)
1885 : sk_next((struct sock*)v) ;
1888 static void packet_seq_stop(struct seq_file *seq, void *v)
1889 __releases(seq_file_net(seq)->packet.sklist_lock)
1891 struct net *net = seq_file_net(seq);
1892 read_unlock(&net->packet.sklist_lock);
1895 static int packet_seq_show(struct seq_file *seq, void *v)
1897 if (v == SEQ_START_TOKEN)
1898 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1901 const struct packet_sock *po = pkt_sk(s);
1904 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1906 atomic_read(&s->sk_refcnt),
1911 atomic_read(&s->sk_rmem_alloc),
1919 static const struct seq_operations packet_seq_ops = {
1920 .start = packet_seq_start,
1921 .next = packet_seq_next,
1922 .stop = packet_seq_stop,
1923 .show = packet_seq_show,
1926 static int packet_seq_open(struct inode *inode, struct file *file)
1928 return seq_open_net(inode, file, &packet_seq_ops,
1929 sizeof(struct seq_net_private));
1932 static const struct file_operations packet_seq_fops = {
1933 .owner = THIS_MODULE,
1934 .open = packet_seq_open,
1936 .llseek = seq_lseek,
1937 .release = seq_release_net,
1942 static int packet_net_init(struct net *net)
1944 rwlock_init(&net->packet.sklist_lock);
1945 INIT_HLIST_HEAD(&net->packet.sklist);
1947 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
1953 static void packet_net_exit(struct net *net)
1955 proc_net_remove(net, "packet");
1958 static struct pernet_operations packet_net_ops = {
1959 .init = packet_net_init,
1960 .exit = packet_net_exit,
1964 static void __exit packet_exit(void)
1966 unregister_netdevice_notifier(&packet_netdev_notifier);
1967 unregister_pernet_subsys(&packet_net_ops);
1968 sock_unregister(PF_PACKET);
1969 proto_unregister(&packet_proto);
1972 static int __init packet_init(void)
1974 int rc = proto_register(&packet_proto, 0);
1979 sock_register(&packet_family_ops);
1980 register_pernet_subsys(&packet_net_ops);
1981 register_netdevice_notifier(&packet_netdev_notifier);
1986 module_init(packet_init);
1987 module_exit(packet_exit);
1988 MODULE_LICENSE("GPL");
1989 MODULE_ALIAS_NETPROTO(PF_PACKET);