1028 lines · 24.2 KB · C
RawDownload
/* * This is a module which is used for queueing packets and communicating with * userspace via nfnetlink. * * (C) 2005 by Harald Welte <laforge@netfilter.org> * (C) 2007 by Patrick McHardy <kaber@trash.net> * * Based on the old ipv4-only ip_queue.c: * (C) 2000-2002 James Morris <jmorris@intercode.com.au> * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */#include <linux/module.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/slab.h>#include <linux/notifier.h>#include <linux/netdevice.h>#include <linux/netfilter.h>#include <linux/proc_fs.h>#include <linux/netfilter_ipv4.h>#include <linux/netfilter_ipv6.h>#include <linux/netfilter/nfnetlink.h>#include <linux/netfilter/nfnetlink_queue.h>#include <linux/list.h>#include <net/sock.h>#include <net/netfilter/nf_queue.h> #include <linux/atomic.h> #ifdef CONFIG_BRIDGE_NETFILTER#include "../bridge/br_private.h"#endif #define NFQNL_QMAX_DEFAULT 1024 struct nfqnl_instance {	struct hlist_node hlist;		/* global list of queues */	struct rcu_head rcu; 	int peer_pid;	unsigned int queue_maxlen;	unsigned int copy_range;	unsigned int queue_dropped;	unsigned int queue_user_dropped;  	u_int16_t queue_num;			/* number of this queue */	u_int8_t copy_mode;/* * Following fields are dirtied for each queued packet, * keep them in same cache line if possible. */	spinlock_t	lock;	unsigned int	queue_total;	unsigned int	id_sequence;		/* 'sequence' of pkt ids */	struct list_head queue_list;		/* packets in queue */}; typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); static DEFINE_SPINLOCK(instances_lock); #define INSTANCE_BUCKETS	16static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly; static inline u_int8_t instance_hashfn(u_int16_t queue_num){	return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;} static struct nfqnl_instance *instance_lookup(u_int16_t queue_num){	struct hlist_head *head;	struct hlist_node *pos;	struct nfqnl_instance *inst; 	head = &instance_table[instance_hashfn(queue_num)];	hlist_for_each_entry_rcu(inst, pos, head, hlist) {		if (inst->queue_num == queue_num)			return inst;	}	return NULL;} static struct nfqnl_instance *instance_create(u_int16_t queue_num, int pid){	struct nfqnl_instance *inst;	unsigned int h;	int err; 	spin_lock(&instances_lock);	if (instance_lookup(queue_num)) {		err = -EEXIST;		goto out_unlock;	} 	inst = kzalloc(sizeof(*inst), GFP_ATOMIC);	if (!inst) {		err = -ENOMEM;		goto out_unlock;	} 	inst->queue_num = queue_num;	inst->peer_pid = pid;	inst->queue_maxlen = NFQNL_QMAX_DEFAULT;	inst->copy_range = 0xfffff;	inst->copy_mode = NFQNL_COPY_NONE;	spin_lock_init(&inst->lock);	INIT_LIST_HEAD(&inst->queue_list); 	if (!try_module_get(THIS_MODULE)) {		err = -EAGAIN;		goto out_free;	} 	h = instance_hashfn(queue_num);	hlist_add_head_rcu(&inst->hlist, &instance_table[h]); 	spin_unlock(&instances_lock); 	return inst; out_free:	kfree(inst);out_unlock:	spin_unlock(&instances_lock);	return ERR_PTR(err);} static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,			unsigned long data); static voidinstance_destroy_rcu(struct rcu_head *head){	struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,						   rcu); 	nfqnl_flush(inst, NULL, 0);	kfree(inst);	module_put(THIS_MODULE);} static void__instance_destroy(struct nfqnl_instance *inst){	hlist_del_rcu(&inst->hlist);	call_rcu(&inst->rcu, instance_destroy_rcu);} static voidinstance_destroy(struct nfqnl_instance *inst){	spin_lock(&instances_lock);	__instance_destroy(inst);	spin_unlock(&instances_lock);} static inline void__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry){       list_add_tail(&entry->list, &queue->queue_list);       queue->queue_total++;} static void__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry){	list_del(&entry->list);	queue->queue_total--;} static struct nf_queue_entry *find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id){	struct nf_queue_entry *entry = NULL, *i; 	spin_lock_bh(&queue->lock); 	list_for_each_entry(i, &queue->queue_list, list) {		if (i->id == id) {			entry = i;			break;		}	} 	if (entry)		__dequeue_entry(queue, entry); 	spin_unlock_bh(&queue->lock); 	return entry;} static voidnfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data){	struct nf_queue_entry *entry, *next; 	spin_lock_bh(&queue->lock);	list_for_each_entry_safe(entry, next, &queue->queue_list, list) {		if (!cmpfn || cmpfn(entry, data)) {			list_del(&entry->list);			queue->queue_total--;			nf_reinject(entry, NF_DROP);		}	}	spin_unlock_bh(&queue->lock);} static struct sk_buff *nfqnl_build_packet_message(struct nfqnl_instance *queue,			   struct nf_queue_entry *entry,			   __be32 **packet_id_ptr){	sk_buff_data_t old_tail;	size_t size;	size_t data_len = 0;	struct sk_buff *skb;	struct nlattr *nla;	struct nfqnl_msg_packet_hdr *pmsg;	struct nlmsghdr *nlh;	struct nfgenmsg *nfmsg;	struct sk_buff *entskb = entry->skb;	struct net_device *indev;	struct net_device *outdev; 	size =    NLMSG_SPACE(sizeof(struct nfgenmsg))		+ nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */#ifdef CONFIG_BRIDGE_NETFILTER		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */#endif		+ nla_total_size(sizeof(u_int32_t))	/* mark */		+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))		+ nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); 	outdev = entry->outdev; 	switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {	case NFQNL_COPY_META:	case NFQNL_COPY_NONE:		break; 	case NFQNL_COPY_PACKET:		if (entskb->ip_summed == CHECKSUM_PARTIAL &&		    skb_checksum_help(entskb))			return NULL; 		data_len = ACCESS_ONCE(queue->copy_range);		if (data_len == 0 || data_len > entskb->len)			data_len = entskb->len; 		size += nla_total_size(data_len);		break;	}  	skb = alloc_skb(size, GFP_ATOMIC);	if (!skb)		goto nlmsg_failure; 	old_tail = skb->tail;	nlh = NLMSG_PUT(skb, 0, 0,			NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,			sizeof(struct nfgenmsg));	nfmsg = NLMSG_DATA(nlh);	nfmsg->nfgen_family = entry->pf;	nfmsg->version = NFNETLINK_V0;	nfmsg->res_id = htons(queue->queue_num); 	nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));	pmsg = nla_data(nla);	pmsg->hw_protocol	= entskb->protocol;	pmsg->hook		= entry->hook;	*packet_id_ptr		= &pmsg->packet_id; 	indev = entry->indev;	if (indev) {#ifndef CONFIG_BRIDGE_NETFILTER		NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));#else		if (entry->pf == PF_BRIDGE) {			/* Case 1: indev is physical input device, we need to			 * look for bridge group (when called from			 * netfilter_bridge) */			NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,				     htonl(indev->ifindex));			/* this is the bridge group "brX" */			/* rcu_read_lock()ed by __nf_queue */			NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,				     htonl(br_port_get_rcu(indev)->br->dev->ifindex));		} else {			/* Case 2: indev is bridge group, we need to look for			 * physical device (when called from ipv4) */			NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,				     htonl(indev->ifindex));			if (entskb->nf_bridge && entskb->nf_bridge->physindev)				NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,					     htonl(entskb->nf_bridge->physindev->ifindex));		}#endif	} 	if (outdev) {#ifndef CONFIG_BRIDGE_NETFILTER		NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));#else		if (entry->pf == PF_BRIDGE) {			/* Case 1: outdev is physical output device, we need to			 * look for bridge group (when called from			 * netfilter_bridge) */			NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,				     htonl(outdev->ifindex));			/* this is the bridge group "brX" */			/* rcu_read_lock()ed by __nf_queue */			NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,				     htonl(br_port_get_rcu(outdev)->br->dev->ifindex));		} else {			/* Case 2: outdev is bridge group, we need to look for			 * physical output device (when called from ipv4) */			NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,				     htonl(outdev->ifindex));			if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)				NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,					     htonl(entskb->nf_bridge->physoutdev->ifindex));		}#endif	} 	if (entskb->mark)		NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark)); 	if (indev && entskb->dev &&	    entskb->mac_header != entskb->network_header) {		struct nfqnl_msg_packet_hw phw;		int len = dev_parse_header(entskb, phw.hw_addr);		if (len) {			phw.hw_addrlen = htons(len);			NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);		}	} 	if (entskb->tstamp.tv64) {		struct nfqnl_msg_packet_timestamp ts;		struct timeval tv = ktime_to_timeval(entskb->tstamp);		ts.sec = cpu_to_be64(tv.tv_sec);		ts.usec = cpu_to_be64(tv.tv_usec); 		NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);	} 	if (data_len) {		struct nlattr *nla;		int sz = nla_attr_size(data_len); 		if (skb_tailroom(skb) < nla_total_size(data_len)) {			printk(KERN_WARNING "nf_queue: no tailroom!\n");			goto nlmsg_failure;		} 		nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));		nla->nla_type = NFQA_PAYLOAD;		nla->nla_len = sz; 		if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))			BUG();	} 	nlh->nlmsg_len = skb->tail - old_tail;	return skb; nlmsg_failure:nla_put_failure:	if (skb)		kfree_skb(skb);	if (net_ratelimit())		printk(KERN_ERR "nf_queue: error creating packet message\n");	return NULL;} static intnfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum){	struct sk_buff *nskb;	struct nfqnl_instance *queue;	int err = -ENOBUFS;	__be32 *packet_id_ptr; 	/* rcu_read_lock()ed by nf_hook_slow() */	queue = instance_lookup(queuenum);	if (!queue) {		err = -ESRCH;		goto err_out;	} 	if (queue->copy_mode == NFQNL_COPY_NONE) {		err = -EINVAL;		goto err_out;	} 	nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);	if (nskb == NULL) {		err = -ENOMEM;		goto err_out;	}	spin_lock_bh(&queue->lock); 	if (!queue->peer_pid) {		err = -EINVAL;		goto err_out_free_nskb;	}	if (queue->queue_total >= queue->queue_maxlen) {		queue->queue_dropped++;		if (net_ratelimit())			  printk(KERN_WARNING "nf_queue: full at %d entries, "				 "dropping packets(s).\n",				 queue->queue_total);		goto err_out_free_nskb;	}	entry->id = ++queue->id_sequence;	*packet_id_ptr = htonl(entry->id); 	/* nfnetlink_unicast will either free the nskb or add it to a socket */	err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);	if (err < 0) {		queue->queue_user_dropped++;		goto err_out_unlock;	} 	__enqueue_entry(queue, entry); 	spin_unlock_bh(&queue->lock);	return 0; err_out_free_nskb:	kfree_skb(nskb);err_out_unlock:	spin_unlock_bh(&queue->lock);err_out:	return err;} static intnfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e){	struct sk_buff *nskb;	int diff; 	diff = data_len - e->skb->len;	if (diff < 0) {		if (pskb_trim(e->skb, data_len))			return -ENOMEM;	} else if (diff > 0) {		if (data_len > 0xFFFF)			return -EINVAL;		if (diff > skb_tailroom(e->skb)) {			nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),					       diff, GFP_ATOMIC);			if (!nskb) {				printk(KERN_WARNING "nf_queue: OOM "				      "in mangle, dropping packet\n");				return -ENOMEM;			}			kfree_skb(e->skb);			e->skb = nskb;		}		skb_put(e->skb, diff);	}	if (!skb_make_writable(e->skb, data_len))		return -ENOMEM;	skb_copy_to_linear_data(e->skb, data, data_len);	e->skb->ip_summed = CHECKSUM_NONE;	return 0;} static intnfqnl_set_mode(struct nfqnl_instance *queue,	       unsigned char mode, unsigned int range){	int status = 0; 	spin_lock_bh(&queue->lock);	switch (mode) {	case NFQNL_COPY_NONE:	case NFQNL_COPY_META:		queue->copy_mode = mode;		queue->copy_range = 0;		break; 

Lines 1–500 of 1028.