⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip6_queue.c

📁 上传linux-jx2410的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * This is a module which is used for queueing IPv6 packets and * communicating with userspace via netlink. * * (C) 2001 Fernando Anton, this code is GPL. *     IPv64 Project - Work based in IPv64 draft by Arturo Azcorra. *     Universidad Carlos III de Madrid - Leganes (Madrid) - Spain *     Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain *     email: fanton@it.uc3m.es * * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying *             to adapt it to IPv6 *             HEAVILY based in ipqueue.c by James Morris. It's just *             a little modified version of it, so he's nearly the *             real coder of this. *             Few changes needed, mainly the hard_routing code and *             the netlink socket protocol (we're NETLINK_IP6_FW). * */#include <linux/module.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/ipv6.h>#include <linux/notifier.h>#include <linux/netdevice.h>#include <linux/netfilter.h>#include <linux/netlink.h>#include <linux/spinlock.h>#include <linux/rtnetlink.h>#include <linux/sysctl.h>#include <linux/proc_fs.h>#include <net/sock.h>#include <net/ipv6.h>#include <net/ip6_route.h>/* We're still usign the following structs. No need to change them: *//*   ipq_packet_msg                                                 *//*   ipq_mode_msg                                                   *//*   ipq_verdict_msg                                                *//*   ipq_peer_msg                                                   */#include <linux/netfilter_ipv4/ip_queue.h>#include <linux/netfilter_ipv4/ip_tables.h>#include <linux/netfilter_ipv6/ip6_tables.h>#define IPQ_QMAX_DEFAULT 1024#define IPQ_PROC_FS_NAME "ip6_queue"#define NET_IPQ_QMAX 2088#define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"typedef struct ip6q_rt_info {	struct in6_addr daddr;	struct in6_addr saddr;} ip6q_rt_info_t;typedef struct ip6q_queue_element {	struct list_head list;		/* Links element into queue */	int verdict;			/* Current verdict */	struct nf_info *info;		/* Extra info from netfilter */	struct sk_buff *skb;		/* Packet inside */	ip6q_rt_info_t rt_info;		/* May need post-mangle routing */} ip6q_queue_element_t;typedef int (*ip6q_send_cb_t)(ip6q_queue_element_t *e);typedef struct ip6q_peer {	pid_t pid;			/* PID of userland peer */	unsigned char died;		/* We think the peer died */	unsigned char copy_mode;	/* Copy packet as well as metadata? */	size_t copy_range;		/* Range past metadata to copy */	ip6q_send_cb_t send;		/* Callback for sending data to peer */} ip6q_peer_t;typedef struct ip6q_queue { 	int len;			/* Current queue len */ 	int *maxlen;			/* Maximum queue len, via sysctl */ 	unsigned char flushing;		/* If queue is being flushed */ 	unsigned char terminate;	/* If the queue is being terminated */ 	struct list_head list;		/* Head of packet queue */ 	spinlock_t lock;		/* Queue spinlock */ 	ip6q_peer_t peer;		/* Userland peer */} ip6q_queue_t;/**************************************************************************** * * Packet queue * ****************************************************************************//* Dequeue a packet if matched by cmp, or the next available if cmp is NULL */static ip6q_queue_element_t *ip6q_dequeue(ip6q_queue_t *q,            int (*cmp)(ip6q_queue_element_t *, unsigned long),            unsigned long data){	struct list_head *i;	spin_lock_bh(&q->lock);	for (i = q->list.prev; i != &q->list; i = i->prev) {		ip6q_queue_element_t *e = (ip6q_queue_element_t *)i;				if (!cmp || cmp(e, data)) {			list_del(&e->list);			q->len--;			spin_unlock_bh(&q->lock);			return e;		}	}	spin_unlock_bh(&q->lock);	return NULL;}/* Flush all packets */static void ip6q_flush(ip6q_queue_t *q){	ip6q_queue_element_t *e;		spin_lock_bh(&q->lock);	q->flushing = 1;	spin_unlock_bh(&q->lock);	while ((e = ip6q_dequeue(q, NULL, 0))) {		e->verdict = NF_DROP;		nf_reinject(e->skb, e->info, e->verdict);		kfree(e);	}	spin_lock_bh(&q->lock);	q->flushing = 0;	spin_unlock_bh(&q->lock);}static ip6q_queue_t *ip6q_create_queue(nf_queue_outfn_t outfn,                                     ip6q_send_cb_t send_cb,                                     int *errp, int *sysctl_qmax){	int status;	ip6q_queue_t *q;	*errp = 0;	q = kmalloc(sizeof(ip6q_queue_t), GFP_KERNEL);	if (q == NULL) {		*errp = -ENOMEM;		return NULL;	}	q->peer.pid = 0;	q->peer.died = 0;	q->peer.copy_mode = IPQ_COPY_NONE;	q->peer.copy_range = 0;	q->peer.send = send_cb;	q->len = 0;	q->maxlen = sysctl_qmax;	q->flushing = 0;	q->terminate = 0;	INIT_LIST_HEAD(&q->list);	spin_lock_init(&q->lock);	status = nf_register_queue_handler(PF_INET6, outfn, q);	if (status < 0) {		*errp = -EBUSY;		kfree(q);		return NULL;	}	return q;}static int ip6q_enqueue(ip6q_queue_t *q,                       struct sk_buff *skb, struct nf_info *info){	ip6q_queue_element_t *e;	int status;		e = kmalloc(sizeof(*e), GFP_ATOMIC);	if (e == NULL) {		printk(KERN_ERR "ip6_queue: OOM in enqueue\n");		return -ENOMEM;	}	e->verdict = NF_DROP;	e->info = info;	e->skb = skb;	if (e->info->hook == NF_IP_LOCAL_OUT) {		struct ipv6hdr *iph = skb->nh.ipv6h;		e->rt_info.daddr = iph->daddr;		e->rt_info.saddr = iph->saddr;	}	spin_lock_bh(&q->lock);	if (q->len >= *q->maxlen) {		spin_unlock_bh(&q->lock);		if (net_ratelimit()) 			printk(KERN_WARNING "ip6_queue: full at %d entries, "			       "dropping packet(s).\n", q->len);		goto free_drop;	}	if (q->flushing || q->peer.copy_mode == IPQ_COPY_NONE	    || q->peer.pid == 0 || q->peer.died || q->terminate) {		spin_unlock_bh(&q->lock);		goto free_drop;	}	status = q->peer.send(e);	if (status > 0) {		list_add(&e->list, &q->list);		q->len++;		spin_unlock_bh(&q->lock);		return status;	}	spin_unlock_bh(&q->lock);	if (status == -ECONNREFUSED) {		printk(KERN_INFO "ip6_queue: peer %d died, "		       "resetting state and flushing queue\n", q->peer.pid);			q->peer.died = 1;			q->peer.pid = 0;			q->peer.copy_mode = IPQ_COPY_NONE;			q->peer.copy_range = 0;			ip6q_flush(q);	}free_drop:	kfree(e);	return -EBUSY;}static void ip6q_destroy_queue(ip6q_queue_t *q){	nf_unregister_queue_handler(PF_INET6);	spin_lock_bh(&q->lock);	q->terminate = 1;	spin_unlock_bh(&q->lock);	ip6q_flush(q);	kfree(q);}/* * Taken from net/ipv6/ip6_output.c * * We should use the one there, but is defined static * so we put this just here and let the things as * they are now. * * If that one is modified, this one should be modified too. */static int route6_me_harder(struct sk_buff *skb){	struct ipv6hdr *iph = skb->nh.ipv6h;	struct dst_entry *dst;	struct flowi fl;	fl.proto = iph->nexthdr;	fl.fl6_dst = &iph->daddr;	fl.fl6_src = &iph->saddr;	fl.oif = skb->sk ? skb->sk->bound_dev_if : 0;	fl.fl6_flowlabel = 0;	fl.uli_u.ports.dport = 0;	fl.uli_u.ports.sport = 0;	dst = ip6_route_output(skb->sk, &fl);	if (dst->error) {		if (net_ratelimit())			printk(KERN_DEBUG "route6_me_harder: No more route.\n");		return -EINVAL;	}	/* Drop old route. */	dst_release(skb->dst);	skb->dst = dst;	return 0;}static int ip6q_mangle_ipv6(ipq_verdict_msg_t *v, ip6q_queue_element_t *e){	int diff;	struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;	if (v->data_len < sizeof(*user_iph))		return 0;	diff = v->data_len - e->skb->len;	if (diff < 0)		skb_trim(e->skb, v->data_len);	else if (diff > 0) {		if (v->data_len > 0xFFFF)			return -EINVAL;		if (diff > skb_tailroom(e->skb)) {			struct sk_buff *newskb;						newskb = skb_copy_expand(e->skb,			                         skb_headroom(e->skb),			                         diff,			                         GFP_ATOMIC);			if (newskb == NULL) {				printk(KERN_WARNING "ip6_queue: OOM "				      "in mangle, dropping packet\n");				return -ENOMEM;			}			if (e->skb->sk)				skb_set_owner_w(newskb, e->skb->sk);			kfree_skb(e->skb);			e->skb = newskb;		}		skb_put(e->skb, diff);	}	memcpy(e->skb->data, v->payload, v->data_len);	e->skb->nfcache |= NFC_ALTERED;	/*	 * Extra routing may needed on local out, as the QUEUE target never	 * returns control to the table.         * Not a nice way to cmp, but works	 */	if (e->info->hook == NF_IP_LOCAL_OUT) {		struct ipv6hdr *iph = e->skb->nh.ipv6h;		if (!(   iph->daddr.in6_u.u6_addr32[0] == e->rt_info.daddr.in6_u.u6_addr32[0]                      && iph->daddr.in6_u.u6_addr32[1] == e->rt_info.daddr.in6_u.u6_addr32[1]                      && iph->daddr.in6_u.u6_addr32[2] == e->rt_info.daddr.in6_u.u6_addr32[2]                      && iph->daddr.in6_u.u6_addr32[3] == e->rt_info.daddr.in6_u.u6_addr32[3]		      && iph->saddr.in6_u.u6_addr32[0] == e->rt_info.saddr.in6_u.u6_addr32[0]		      && iph->saddr.in6_u.u6_addr32[1] == e->rt_info.saddr.in6_u.u6_addr32[1]		      && iph->saddr.in6_u.u6_addr32[2] == e->rt_info.saddr.in6_u.u6_addr32[2]		      && iph->saddr.in6_u.u6_addr32[3] == e->rt_info.saddr.in6_u.u6_addr32[3]))			return route6_me_harder(e->skb);	}	return 0;}static inline int id_cmp(ip6q_queue_element_t *e, unsigned long id){	return (id == (unsigned long )e);}static int ip6q_set_verdict(ip6q_queue_t *q,                           ipq_verdict_msg_t *v, unsigned int len){	ip6q_queue_element_t *e;	if (v->value > NF_MAX_VERDICT)		return -EINVAL;	e = ip6q_dequeue(q, id_cmp, v->id);	if (e == NULL)		return -ENOENT;	else {		e->verdict = v->value;		if (v->data_len && v->data_len == len)			if (ip6q_mangle_ipv6(v, e) < 0)				e->verdict = NF_DROP;		nf_reinject(e->skb, e->info, e->verdict);		kfree(e);		return 0;	}}static int ip6q_receive_peer(ip6q_queue_t* q, ipq_peer_msg_t *m,                            unsigned char type, unsigned int len){	int status = 0;	int busy;			spin_lock_bh(&q->lock);	busy = (q->terminate || q->flushing);	spin_unlock_bh(&q->lock);	if (busy)		return -EBUSY;	if (len < sizeof(ipq_peer_msg_t))		return -EINVAL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -