📄 af_packet.c
字号:
/* * INET An implementation of the TCP/IP protocol suite for the LINUX * operating system. INET is implemented using the BSD Socket * interface as the means of communication with the user level. * * PACKET - implements raw packet sockets. * * Version: $Id: af_packet.c,v 1.58 2001/11/28 21:02:10 davem Exp $ * * Authors: Ross Biro, <bir7@leland.Stanford.Edu> * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> * Alan Cox, <gw4pts@gw4pts.ampr.org> * * Fixes: * Alan Cox : verify_area() now used correctly * Alan Cox : new skbuff lists, look ma no backlogs! * Alan Cox : tidied skbuff lists. * Alan Cox : Now uses generic datagram routines I * added. Also fixed the peek/read crash * from all old Linux datagram code. * Alan Cox : Uses the improved datagram code. * Alan Cox : Added NULL's for socket options. * Alan Cox : Re-commented the code. * Alan Cox : Use new kernel side addressing * Rob Janssen : Correct MTU usage. * Dave Platt : Counter leaks caused by incorrect * interrupt locking and some slightly * dubious gcc output. Can you read * compiler: it said _VOLATILE_ * Richard Kooijman : Timestamp fixes. * Alan Cox : New buffers. Use sk->mac.raw. * Alan Cox : sendmsg/recvmsg support. * Alan Cox : Protocol setting support * Alexey Kuznetsov : Untied from IPv4 stack. * Cyrus Durgin : Fixed kerneld for kmod. * Michal Ostrowski : Module initialization cleanup. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * */ #include <linux/config.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/fcntl.h>#include <linux/socket.h>#include <linux/in.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <linux/if_packet.h>#include <linux/wireless.h>#include <linux/kmod.h>#include <net/ip.h>#include <net/protocol.h>#include <linux/skbuff.h>#include <net/sock.h>#include <linux/errno.h>#include <linux/timer.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/ioctls.h>#include <linux/proc_fs.h>#include <linux/poll.h>#include <linux/module.h>#include <linux/init.h>#include <linux/if_bridge.h>#ifdef CONFIG_NET_DIVERT#include <linux/divert.h>#endif /* CONFIG_NET_DIVERT */#ifdef CONFIG_INET#include <net/inet_common.h>#endif#ifdef CONFIG_DLCIextern int dlci_ioctl(unsigned int, void*);#endif#define CONFIG_SOCK_PACKET 1/* Proposed replacement for SIOC{ADD,DEL}MULTI and IFF_PROMISC, IFF_ALLMULTI flags. It is more expensive, but I believe, it is really correct solution: reentereble, safe and fault tolerant. IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping reference count and global flag, so that real status is (gflag|(count != 0)), so that we can use obsolete faulty interface not harming clever users. */#define CONFIG_PACKET_MULTICAST 1/* Assumptions: - if device has no dev->hard_header routine, it adds and removes ll header inside itself. In this case ll header is invisible outside of device, but higher levels still should reserve dev->hard_header_len. Some devices are enough clever to reallocate skb, when header will not fit to reserved space (tunnel), another ones are silly (PPP). - packet socket receives packets with pulled ll header, so that SOCK_RAW should push it back.On receive:-----------Incoming, dev->hard_header!=NULL mac.raw -> ll header data -> dataOutgoing, dev->hard_header!=NULL mac.raw -> ll header data -> ll headerIncoming, dev->hard_header==NULL mac.raw -> UNKNOWN position. It is very likely, that it points to ll header. PPP makes it, that is wrong, because introduce assymetry between rx and tx paths. data -> dataOutgoing, dev->hard_header==NULL mac.raw -> data. ll header is still not built! data -> dataResume If dev->hard_header==NULL we are unlikely to restore sensible ll header.On transmit:------------dev->hard_header != NULL mac.raw -> ll header data -> ll headerdev->hard_header == NULL (ll header is added by device, we cannot control it) mac.raw -> data data -> data We should set nh.raw on output to correct posistion, packet classifier depends on it. *//* List of all packet sockets. */static struct sock * packet_sklist;static rwlock_t packet_sklist_lock = RW_LOCK_UNLOCKED;atomic_t packet_socks_nr;/* Private packet socket structures. */#ifdef CONFIG_PACKET_MULTICASTstruct packet_mclist{ struct packet_mclist *next; int ifindex; int count; unsigned short type; unsigned short alen; unsigned char addr[8];};#endif#ifdef CONFIG_PACKET_MMAPstatic int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);#endifstatic void packet_flush_mclist(struct sock *sk);struct packet_opt{ struct packet_type prot_hook; spinlock_t bind_lock; char running; /* prot_hook is attached*/ int ifindex; /* bound device */ struct tpacket_stats stats;#ifdef CONFIG_PACKET_MULTICAST struct packet_mclist *mclist;#endif#ifdef CONFIG_PACKET_MMAP atomic_t mapped; unsigned long *pg_vec; unsigned int pg_vec_order; unsigned int pg_vec_pages; unsigned int pg_vec_len; struct tpacket_hdr **iovec; unsigned int frame_size; unsigned int iovmax; unsigned int head; int copy_thresh;#endif};void packet_sock_destruct(struct sock *sk){ BUG_TRAP(atomic_read(&sk->rmem_alloc)==0); BUG_TRAP(atomic_read(&sk->wmem_alloc)==0); if (!sk->dead) { printk("Attempt to release alive packet socket: %p\n", sk); return; } if (sk->protinfo.destruct_hook) kfree(sk->protinfo.destruct_hook); atomic_dec(&packet_socks_nr);#ifdef PACKET_REFCNT_DEBUG printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));#endif MOD_DEC_USE_COUNT;}extern struct proto_ops packet_ops;#ifdef CONFIG_SOCK_PACKETextern struct proto_ops packet_ops_spkt;static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt){ struct sock *sk; struct sockaddr_pkt *spkt; /* * When we registered the protocol we saved the socket in the data * field for just this event. */ sk = (struct sock *) pt->data; /* * Yank back the headers [hope the device set this * right or kerboom...] * * Incoming packets have ll header pulled, * push it back. * * For outgoing ones skb->data == skb->mac.raw * so that this procedure is noop. */ if (skb->pkt_type == PACKET_LOOPBACK) goto out; if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) goto oom; spkt = (struct sockaddr_pkt*)skb->cb; skb_push(skb, skb->data-skb->mac.raw); /* * The SOCK_PACKET socket receives _all_ frames. */ spkt->spkt_family = dev->type; strncpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); spkt->spkt_protocol = skb->protocol; /* * Charge the memory to the socket. This is done specifically * to prevent sockets using all the memory up. */ if (sock_queue_rcv_skb(sk,skb) == 0) return 0;out: kfree_skb(skb);oom: return 0;}/* * Output a raw packet to a device layer. This bypasses all the other * protocol layers and you must therefore supply it with a complete frame */ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm){ struct sock *sk = sock->sk; struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name; struct sk_buff *skb; struct net_device *dev; unsigned short proto=0; int err; /* * Get and verify the address. */ if (saddr) { if (msg->msg_namelen < sizeof(struct sockaddr)) return(-EINVAL); if (msg->msg_namelen==sizeof(struct sockaddr_pkt)) proto=saddr->spkt_protocol; } else return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */ /* * Find the device first to size check it */ saddr->spkt_device[13] = 0; dev = dev_get_by_name(saddr->spkt_device); err = -ENODEV; if (dev == NULL) goto out_unlock; /* * You may not queue a frame bigger than the mtu. This is the lowest level * raw protocol and you must do your own fragmentation at this level. */ err = -EMSGSIZE; if(len>dev->mtu+dev->hard_header_len) goto out_unlock; err = -ENOBUFS; skb = sock_wmalloc(sk, len+dev->hard_header_len+15, 0, GFP_KERNEL); /* * If the write buffer is full, then tough. At this level the user gets to * deal with the problem - do your own algorithmic backoffs. That's far * more flexible. */ if (skb == NULL) goto out_unlock; /* * Fill it in */ /* FIXME: Save some space for broken drivers that write a * hard header at transmission time by themselves. PPP is the * notable one here. This should really be fixed at the driver level. */ skb_reserve(skb,(dev->hard_header_len+15)&~15); skb->nh.raw = skb->data; /* Try to align data part correctly */ if (dev->hard_header) { skb->data -= dev->hard_header_len; skb->tail -= dev->hard_header_len; } /* Returns -EFAULT on error */ err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); skb->protocol = proto; skb->dev = dev; skb->priority = sk->priority; if (err) goto out_free; err = -ENETDOWN; if (!(dev->flags & IFF_UP)) goto out_free; /* * Now send it */ dev_queue_xmit(skb); dev_put(dev); return(len);out_free: kfree_skb(skb);out_unlock: if (dev) dev_put(dev); return err;}#endif/* This function makes lazy skb cloning in hope that most of packets are discarded by BPF. Note tricky part: we DO mangle shared skb! skb->data, skb->len and skb->cb are mangled. It works because (and until) packets falling here are owned by current CPU. Output packets are cloned by dev_queue_xmit_nit(), input packets are processed by net_bh sequencially, so that if we return skb to original state on exit, we will not harm anyone. */static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt){ struct sock *sk; struct sockaddr_ll *sll; struct packet_opt *po; u8 * skb_head = skb->data; int skb_len = skb->len;#ifdef CONFIG_FILTER unsigned snaplen;#endif if (skb->pkt_type == PACKET_LOOPBACK) goto drop; sk = (struct sock *) pt->data; po = sk->protinfo.af_packet; skb->dev = dev; if (dev->hard_header) { /* The device has an explicit notion of ll header, exported to higher levels. Otherwise, the device hides datails of it frame structure, so that corresponding packet head never delivered to user. */ if (sk->type != SOCK_DGRAM) skb_push(skb, skb->data - skb->mac.raw); else if (skb->pkt_type == PACKET_OUTGOING) { /* Special case: outgoing packets have ll header at head */ skb_pull(skb, skb->nh.raw - skb->data); } }#ifdef CONFIG_FILTER snaplen = skb->len; if (sk->filter) { unsigned res = snaplen; struct sk_filter *filter; bh_lock_sock(sk); if ((filter = sk->filter) != NULL) res = sk_run_filter(skb, sk->filter->insns, sk->filter->len); bh_unlock_sock(sk); if (res == 0) goto drop_n_restore; if (snaplen > res) snaplen = res; }#endif /* CONFIG_FILTER */ if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf) goto drop_n_acct; if (skb_shared(skb)) { struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); if (nskb == NULL) goto drop_n_acct; if (skb_head != skb->data) { skb->data = skb_head; skb->len = skb_len; } kfree_skb(skb); skb = nskb; } sll = (struct sockaddr_ll*)skb->cb; sll->sll_family = AF_PACKET; sll->sll_hatype = dev->type; sll->sll_protocol = skb->protocol; sll->sll_pkttype = skb->pkt_type; sll->sll_ifindex = dev->ifindex; sll->sll_halen = 0; if (dev->hard_header_parse) sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);#ifdef CONFIG_FILTER if (pskb_trim(skb, snaplen)) goto drop_n_acct;#endif skb_set_owner_r(skb, sk); skb->dev = NULL; spin_lock(&sk->receive_queue.lock); po->stats.tp_packets++; __skb_queue_tail(&sk->receive_queue, skb); spin_unlock(&sk->receive_queue.lock); sk->data_ready(sk,skb->len); return 0;drop_n_acct: spin_lock(&sk->receive_queue.lock); po->stats.tp_drops++; spin_unlock(&sk->receive_queue.lock);#ifdef CONFIG_FILTERdrop_n_restore:#endif if (skb_head != skb->data && skb_shared(skb)) { skb->data = skb_head; skb->len = skb_len; }drop: kfree_skb(skb); return 0;}#ifdef CONFIG_PACKET_MMAPstatic int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt){ struct sock *sk; struct packet_opt *po; struct sockaddr_ll *sll; struct tpacket_hdr *h; u8 * skb_head = skb->data; int skb_len = skb->len; unsigned snaplen; unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER; unsigned short macoff, netoff; struct sk_buff *copy_skb = NULL; if (skb->pkt_type == PACKET_LOOPBACK) goto drop; sk = (struct sock *) pt->data; po = sk->protinfo.af_packet; if (dev->hard_header) { if (sk->type != SOCK_DGRAM) skb_push(skb, skb->data - skb->mac.raw); else if (skb->pkt_type == PACKET_OUTGOING) { /* Special case: outgoing packets have ll header at head */ skb_pull(skb, skb->nh.raw - skb->data); if (skb->ip_summed == CHECKSUM_HW) status |= TP_STATUS_CSUMNOTREADY; } } snaplen = skb->len;#ifdef CONFIG_FILTER if (sk->filter) { unsigned res = snaplen; struct sk_filter *filter; bh_lock_sock(sk); if ((filter = sk->filter) != NULL) res = sk_run_filter(skb, sk->filter->insns, sk->filter->len); bh_unlock_sock(sk); if (res == 0) goto drop_n_restore; if (snaplen > res) snaplen = res; }#endif if (sk->type == SOCK_DGRAM) { macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16; } else { unsigned maclen = skb->nh.raw - skb->data; netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen)); macoff = netoff - maclen; } if (macoff + snaplen > po->frame_size) { if (po->copy_thresh && atomic_read(&sk->rmem_alloc) + skb->truesize < (unsigned)sk->rcvbuf) { if (skb_shared(skb)) { copy_skb = skb_clone(skb, GFP_ATOMIC); } else { copy_skb = skb_get(skb); skb_head = skb->data; } if (copy_skb) skb_set_owner_r(copy_skb, sk); } snaplen = po->frame_size - macoff; if ((int)snaplen < 0) snaplen = 0; } if (snaplen > skb->len-skb->data_len) snaplen = skb->len-skb->data_len; spin_lock(&sk->receive_queue.lock); h = po->iovec[po->head]; if (h->tp_status) goto ring_is_full; po->head = po->head != po->iovmax ? po->head+1 : 0; po->stats.tp_packets++; if (copy_skb) { status |= TP_STATUS_COPY; __skb_queue_tail(&sk->receive_queue, copy_skb); } if (!po->stats.tp_drops) status &= ~TP_STATUS_LOSING; spin_unlock(&sk->receive_queue.lock); memcpy((u8*)h + macoff, skb->data, snaplen); h->tp_len = skb->len; h->tp_snaplen = snaplen; h->tp_mac = macoff; h->tp_net = netoff; h->tp_sec = skb->stamp.tv_sec; h->tp_usec = skb->stamp.tv_usec; sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h))); sll->sll_halen = 0; if (dev->hard_header_parse) sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr); sll->sll_family = AF_PACKET; sll->sll_hatype = dev->type; sll->sll_protocol = skb->protocol; sll->sll_pkttype = skb->pkt_type; sll->sll_ifindex = dev->ifindex; h->tp_status = status; mb(); { struct page *p_start, *p_end; u8 *h_end = (u8 *)h + macoff + snaplen - 1; p_start = virt_to_page(h); p_end = virt_to_page(h_end); while (p_start <= p_end) { flush_dcache_page(p_start); p_start++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -