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

📄 ip_tables.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Packet matching code. * * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling */#include <linux/config.h>#include <linux/skbuff.h>#include <linux/kmod.h>#include <linux/vmalloc.h>#include <linux/netdevice.h>#include <linux/module.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/icmp.h>#include <net/ip.h>#include <asm/uaccess.h>#include <asm/semaphore.h>#include <linux/proc_fs.h>#include <linux/netfilter_ipv4/ip_tables.h>/*#define DEBUG_IP_FIREWALL*//*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging *//*#define DEBUG_IP_FIREWALL_USER*/#ifdef DEBUG_IP_FIREWALL#define dprintf(format, args...)  printk(format , ## args)#else#define dprintf(format, args...)#endif#ifdef DEBUG_IP_FIREWALL_USER#define duprintf(format, args...) printk(format , ## args)#else#define duprintf(format, args...)#endif#ifdef CONFIG_NETFILTER_DEBUG#define IP_NF_ASSERT(x)						\do {								\	if (!(x))						\		printk("IP_NF_ASSERT: %s:%s:%u\n",		\		       __FUNCTION__, __FILE__, __LINE__);	\} while(0)#else#define IP_NF_ASSERT(x)#endif#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))/* Mutex protects lists (only traversed in user context). */static DECLARE_MUTEX(ipt_mutex);/* Must have mutex */#define ASSERT_READ_LOCK(x) IP_NF_ASSERT(down_trylock(&ipt_mutex) != 0)#define ASSERT_WRITE_LOCK(x) IP_NF_ASSERT(down_trylock(&ipt_mutex) != 0)#include <linux/netfilter_ipv4/lockhelp.h>#include <linux/netfilter_ipv4/listhelp.h>#if 0/* All the better to debug you with... */#define static#define inline#endif/* Locking is simple: we assume at worst case there will be one packet   in user context and one from bottom halves (or soft irq if Alexey's   softnet patch was applied).   We keep a set of rules for each CPU, so we can avoid write-locking   them; doing a readlock_bh() stops packets coming through if we're   in user context.   To be cache friendly on SMP, we arrange them like so:   [ n-entries ]   ... cache-align padding ...   [ n-entries ]   Hence the start of any table is given by get_table() below.  *//* The table itself */struct ipt_table_info{	/* Size per table */	unsigned int size;	/* Number of entries: FIXME. --RR */	unsigned int number;	/* Entry points and underflows */	unsigned int hook_entry[NF_IP_NUMHOOKS];	unsigned int underflow[NF_IP_NUMHOOKS];	/* ipt_entry tables: one per CPU */	char entries[0] __attribute__((aligned(SMP_CACHE_BYTES)));};static LIST_HEAD(ipt_target);static LIST_HEAD(ipt_match);static LIST_HEAD(ipt_tables);#define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)#ifdef CONFIG_SMP#define TABLE_OFFSET(t,p) (SMP_ALIGN((t)->size)*(p))#else#define TABLE_OFFSET(t,p) 0#endif#if 0#define down(x) do { printk("DOWN:%u:" #x "\n", __LINE__); down(x); } while(0)#define down_interruptible(x) ({ int __r; printk("DOWNi:%u:" #x "\n", __LINE__); __r = down_interruptible(x); if (__r != 0) printk("ABORT-DOWNi:%u\n", __LINE__); __r; })#define up(x) do { printk("UP:%u:" #x "\n", __LINE__); up(x); } while(0)#endif/* Returns whether matches rule or not. */static inline intip_packet_match(const struct iphdr *ip,		const char *indev,		const char *outdev,		const struct ipt_ip *ipinfo,		int isfrag){	size_t i;	unsigned long ret;#define FWINV(bool,invflg) ((bool) ^ !!(ipinfo->invflags & invflg))	if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,		  IPT_INV_SRCIP)	    || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,		     IPT_INV_DSTIP)) {		dprintf("Source or dest mismatch.\n");		dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",			NIPQUAD(ip->saddr),			NIPQUAD(ipinfo->smsk.s_addr),			NIPQUAD(ipinfo->src.s_addr),			ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");		dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",			NIPQUAD(ip->daddr),			NIPQUAD(ipinfo->dmsk.s_addr),			NIPQUAD(ipinfo->dst.s_addr),			ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");		return 0;	}	/* Look for ifname matches; this should unroll nicely. */	for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {		ret |= (((const unsigned long *)indev)[i]			^ ((const unsigned long *)ipinfo->iniface)[i])			& ((const unsigned long *)ipinfo->iniface_mask)[i];	}	if (FWINV(ret != 0, IPT_INV_VIA_IN)) {		dprintf("VIA in mismatch (%s vs %s).%s\n",			indev, ipinfo->iniface,			ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");		return 0;	}	for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {		ret |= (((const unsigned long *)outdev)[i]			^ ((const unsigned long *)ipinfo->outiface)[i])			& ((const unsigned long *)ipinfo->outiface_mask)[i];	}	if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {		dprintf("VIA out mismatch (%s vs %s).%s\n",			outdev, ipinfo->outiface,			ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");		return 0;	}	/* Check specific protocol */	if (ipinfo->proto	    && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {		dprintf("Packet protocol %hi does not match %hi.%s\n",			ip->protocol, ipinfo->proto,			ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");		return 0;	}	/* If we have a fragment rule but the packet is not a fragment	 * then we return zero */	if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {		dprintf("Fragment rule but not fragment.%s\n",			ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");		return 0;	}	return 1;}static inline intip_checkentry(const struct ipt_ip *ip){	if (ip->flags & ~IPT_F_MASK) {		duprintf("Unknown flag bits set: %08X\n",			 ip->flags & ~IPT_F_MASK);		return 0;	}	if (ip->invflags & ~IPT_INV_MASK) {		duprintf("Unknown invflag bits set: %08X\n",			 ip->invflags & ~IPT_INV_MASK);		return 0;	}	return 1;}static unsigned intipt_error(struct sk_buff **pskb,	  unsigned int hooknum,	  const struct net_device *in,	  const struct net_device *out,	  const void *targinfo,	  void *userinfo){	if (net_ratelimit())		printk("ip_tables: error: `%s'\n", (char *)targinfo);	return NF_DROP;}static inlineint do_match(struct ipt_entry_match *m,	     const struct sk_buff *skb,	     const struct net_device *in,	     const struct net_device *out,	     int offset,	     const void *hdr,	     u_int16_t datalen,	     int *hotdrop){	/* Stop iteration if it doesn't match */	if (!m->u.kernel.match->match(skb, in, out, m->data,				      offset, hdr, datalen, hotdrop))		return 1;	else		return 0;}static inline struct ipt_entry *get_entry(void *base, unsigned int offset){	return (struct ipt_entry *)(base + offset);}/* Returns one of the generic firewall policies, like NF_ACCEPT. */unsigned intipt_do_table(struct sk_buff **pskb,	     unsigned int hook,	     const struct net_device *in,	     const struct net_device *out,	     struct ipt_table *table,	     void *userdata){	static const char nulldevname[IFNAMSIZ] = { 0 };	u_int16_t offset;	struct iphdr *ip;	void *protohdr;	u_int16_t datalen;	int hotdrop = 0;	/* Initializing verdict to NF_DROP keeps gcc happy. */	unsigned int verdict = NF_DROP;	const char *indev, *outdev;	void *table_base;	struct ipt_entry *e, *back;	/* Initialization */	ip = (*pskb)->nh.iph;	protohdr = (u_int32_t *)ip + ip->ihl;	datalen = (*pskb)->len - ip->ihl * 4;	indev = in ? in->name : nulldevname;	outdev = out ? out->name : nulldevname;	/* We handle fragments by dealing with the first fragment as	 * if it was a normal packet.  All other fragments are treated	 * normally, except that they will NEVER match rules that ask	 * things we don't know, ie. tcp syn flag or ports).  If the	 * rule is also a fragment-specific rule, non-fragments won't	 * match it. */	offset = ntohs(ip->frag_off) & IP_OFFSET;	read_lock_bh(&table->lock);	IP_NF_ASSERT(table->valid_hooks & (1 << hook));	table_base = (void *)table->private->entries		+ TABLE_OFFSET(table->private,			       cpu_number_map(smp_processor_id()));	e = get_entry(table_base, table->private->hook_entry[hook]);#ifdef CONFIG_NETFILTER_DEBUG	/* Check noone else using our table */	if (((struct ipt_entry *)table_base)->comefrom != 0xdead57ac	    && ((struct ipt_entry *)table_base)->comefrom != 0xeeeeeeec) {		printk("ASSERT: CPU #%u, %s comefrom(%p) = %X\n",		       smp_processor_id(),		       table->name,		       &((struct ipt_entry *)table_base)->comefrom,		       ((struct ipt_entry *)table_base)->comefrom);	}	((struct ipt_entry *)table_base)->comefrom = 0x57acc001;#endif	/* For return from builtin chain */	back = get_entry(table_base, table->private->underflow[hook]);	do {		IP_NF_ASSERT(e);		IP_NF_ASSERT(back);		(*pskb)->nfcache |= e->nfcache;		if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {			struct ipt_entry_target *t;			if (IPT_MATCH_ITERATE(e, do_match,					      *pskb, in, out,					      offset, protohdr,					      datalen, &hotdrop) != 0)				goto no_match;			ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);			t = ipt_get_target(e);			IP_NF_ASSERT(t->u.kernel.target);			/* Standard target? */			if (!t->u.kernel.target->target) {				int v;				v = ((struct ipt_standard_target *)t)->verdict;				if (v < 0) {					/* Pop from stack? */					if (v != IPT_RETURN) {						verdict = (unsigned)(-v) - 1;						break;					}					e = back;					back = get_entry(table_base,							 back->comefrom);					continue;				}				if (table_base + v				    != (void *)e + e->next_offset) {					/* Save old back ptr in next entry */					struct ipt_entry *next						= (void *)e + e->next_offset;					next->comefrom						= (void *)back - table_base;					/* set back pointer to next entry */					back = next;				}				e = get_entry(table_base, v);			} else {				/* Targets which reenter must return                                   abs. verdicts */#ifdef CONFIG_NETFILTER_DEBUG				((struct ipt_entry *)table_base)->comefrom					= 0xeeeeeeec;#endif				verdict = t->u.kernel.target->target(pskb,								     hook,								     in, out,								     t->data,								     userdata);#ifdef CONFIG_NETFILTER_DEBUG				if (((struct ipt_entry *)table_base)->comefrom				    != 0xeeeeeeec				    && verdict == IPT_CONTINUE) {					printk("Target %s reentered!\n",					       t->u.kernel.target->name);					verdict = NF_DROP;				}				((struct ipt_entry *)table_base)->comefrom					= 0x57acc001;#endif				/* Target might have changed stuff. */				ip = (*pskb)->nh.iph;				protohdr = (u_int32_t *)ip + ip->ihl;				datalen = (*pskb)->len - ip->ihl * 4;				if (verdict == IPT_CONTINUE)					e = (void *)e + e->next_offset;				else					/* Verdict */					break;			}		} else {		no_match:			e = (void *)e + e->next_offset;		}	} while (!hotdrop);#ifdef CONFIG_NETFILTER_DEBUG	((struct ipt_entry *)table_base)->comefrom = 0xdead57ac;#endif	read_unlock_bh(&table->lock);#ifdef DEBUG_ALLOW_ALL	return NF_ACCEPT;#else	if (hotdrop)		return NF_DROP;	else return verdict;#endif}/* If it succeeds, returns element and locks mutex */static inline void *find_inlist_lock_noload(struct list_head *head,			const char *name,			int *error,			struct semaphore *mutex){	void *ret;#if 0	duprintf("find_inlist: searching for `%s' in %s.\n",		 name, head == &ipt_target ? "ipt_target"		 : head == &ipt_match ? "ipt_match"		 : head == &ipt_tables ? "ipt_tables" : "UNKNOWN");#endif	*error = down_interruptible(mutex);	if (*error != 0)		return NULL;	ret = list_named_find(head, name);	if (!ret) {		*error = -ENOENT;		up(mutex);	}	return ret;}#ifndef CONFIG_KMOD#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))#elsestatic void *find_inlist_lock(struct list_head *head,		 const char *name,		 const char *prefix,		 int *error,		 struct semaphore *mutex){	void *ret;	ret = find_inlist_lock_noload(head, name, error, mutex);	if (!ret) {		char modulename[IPT_FUNCTION_MAXNAMELEN + strlen(prefix) + 1];		strcpy(modulename, prefix);		strcat(modulename, name);		duprintf("find_inlist: loading `%s'.\n", modulename);		request_module(modulename);		ret = find_inlist_lock_noload(head, name, error, mutex);	}	return ret;}#endifstatic inline struct ipt_table *find_table_lock(const char *name, int *error, struct semaphore *mutex){	return find_inlist_lock(&ipt_tables, name, "iptable_", error, mutex);}static inline struct ipt_match *find_match_lock(const char *name, int *error, struct semaphore *mutex){	return find_inlist_lock(&ipt_match, name, "ipt_", error, mutex);}static inline struct ipt_target *find_target_lock(const char *name, int *error, struct semaphore *mutex){	return find_inlist_lock(&ipt_target, name, "ipt_", error, mutex);}/* All zeroes == unconditional rule. */static inline intunconditional(const struct ipt_ip *ip){	unsigned int i;	for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)		if (((__u32 *)ip)[i])			return 0;	return 1;}/* Figures out from what hook each rule can be called: returns 0 if   there are loops.  Puts hook bitmask in comefrom. */static intmark_source_chains(struct ipt_table_info *newinfo, unsigned int valid_hooks){	unsigned int hook;	/* No recursion; use packet counter to save back ptrs (reset	   to 0 as we leave), and comefrom to save source hook bitmask */	for (hook = 0; hook < NF_IP_NUMHOOKS; hook++) {		unsigned int pos = newinfo->hook_entry[hook];		struct ipt_entry *e			= (struct ipt_entry *)(newinfo->entries + pos);		if (!(valid_hooks & (1 << hook)))			continue;		/* Set initial back pointer. */		e->counters.pcnt = pos;		for (;;) {			struct ipt_standard_target *t				= (void *)ipt_get_target(e);			if (e->comefrom & (1 << NF_IP_NUMHOOKS)) {				printk("iptables: loop hook %u pos %u %08X.\n",				       hook, pos, e->comefrom);				return 0;			}			e->comefrom				|= ((1 << hook) | (1 << NF_IP_NUMHOOKS));			/* Unconditional return/END. */			if (e->target_offset == sizeof(struct ipt_entry)			    && (strcmp(t->target.u.user.name,				       IPT_STANDARD_TARGET) == 0)			    && t->verdict < 0			    && unconditional(&e->ip)) {				unsigned int oldpos, size;				/* Return: backtrack through the last				   big jump. */				do {					e->comefrom ^= (1<<NF_IP_NUMHOOKS);#ifdef DEBUG_IP_FIREWALL_USER					if (e->comefrom					    & (1 << NF_IP_NUMHOOKS)) {						duprintf("Back unset "							 "on hook %u "							 "rule %u\n",							 hook, pos);					}#endif					oldpos = pos;					pos = e->counters.pcnt;					e->counters.pcnt = 0;					/* We're at the start. */					if (pos == oldpos)						goto next;					e = (struct ipt_entry *)						(newinfo->entries + pos);				} while (oldpos == pos + e->next_offset);				/* Move along one */				size = e->next_offset;				e = (struct ipt_entry *)					(newinfo->entries + pos + size);				e->counters.pcnt = pos;				pos += size;			} else {				int newpos = t->verdict;				if (strcmp(t->target.u.user.name,					   IPT_STANDARD_TARGET) == 0				    && newpos >= 0) {					/* This a jump; chase it. */					duprintf("Jump rule %u -> %u\n",						 pos, newpos);				} else {					/* ... this is a fallthru */					newpos = pos + e->next_offset;				}				e = (struct ipt_entry *)					(newinfo->entries + newpos);				e->counters.pcnt = pos;				pos = newpos;			}		}		next:		duprintf("Finished chain %u\n", hook);	}	return 1;}static inline intcleanup_match(struct ipt_entry_match *m, unsigned int *i)

⌨️ 快捷键说明

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