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

📄 ip6_tables.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Packet matching code. * * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling * Copyright (C) 2000-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/capability.h>#include <linux/in.h>#include <linux/skbuff.h>#include <linux/kmod.h>#include <linux/vmalloc.h>#include <linux/netdevice.h>#include <linux/module.h>#include <linux/poison.h>#include <linux/icmpv6.h>#include <net/ipv6.h>#include <asm/uaccess.h>#include <linux/mutex.h>#include <linux/proc_fs.h>#include <linux/cpumask.h>#include <linux/netfilter_ipv6/ip6_tables.h>#include <linux/netfilter/x_tables.h>MODULE_LICENSE("GPL");MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");MODULE_DESCRIPTION("IPv6 packet filter");#define IPV6_HDR_LEN	(sizeof(struct ipv6hdr))#define IPV6_OPTHDR_LEN	(sizeof(struct ipv6_opt_hdr))/*#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#if 0/* All the better to debug you with... */#define static#define inline#endif/*   We keep a set of rules for each CPU, so we can avoid write-locking   them in the softirq when updating the counters and therefore   only need to read-lock in the softirq; doing a write_lock_bh() in user   context stops packets coming through and allows user context to read   the counters or update the rules.   Hence the start of any table is given by get_table() below.  */#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/* Check for an extension */intip6t_ext_hdr(u8 nexthdr){	return ( (nexthdr == IPPROTO_HOPOPTS)   ||		 (nexthdr == IPPROTO_ROUTING)   ||		 (nexthdr == IPPROTO_FRAGMENT)  ||		 (nexthdr == IPPROTO_ESP)       ||		 (nexthdr == IPPROTO_AH)        ||		 (nexthdr == IPPROTO_NONE)      ||		 (nexthdr == IPPROTO_DSTOPTS) );}/* Returns whether matches rule or not. */static inline boolip6_packet_match(const struct sk_buff *skb,		 const char *indev,		 const char *outdev,		 const struct ip6t_ip6 *ip6info,		 unsigned int *protoff,		 int *fragoff, bool *hotdrop){	size_t i;	unsigned long ret;	const struct ipv6hdr *ipv6 = ipv6_hdr(skb);#define FWINV(bool,invflg) ((bool) ^ !!(ip6info->invflags & invflg))	if (FWINV(ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,				       &ip6info->src), IP6T_INV_SRCIP)	    || FWINV(ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,					  &ip6info->dst), IP6T_INV_DSTIP)) {		dprintf("Source or dest mismatch.\n");/*		dprintf("SRC: %u. Mask: %u. Target: %u.%s\n", ip->saddr,			ipinfo->smsk.s_addr, ipinfo->src.s_addr,			ipinfo->invflags & IP6T_INV_SRCIP ? " (INV)" : "");		dprintf("DST: %u. Mask: %u. Target: %u.%s\n", ip->daddr,			ipinfo->dmsk.s_addr, ipinfo->dst.s_addr,			ipinfo->invflags & IP6T_INV_DSTIP ? " (INV)" : "");*/		return false;	}	/* 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 *)ip6info->iniface)[i])			& ((const unsigned long *)ip6info->iniface_mask)[i];	}	if (FWINV(ret != 0, IP6T_INV_VIA_IN)) {		dprintf("VIA in mismatch (%s vs %s).%s\n",			indev, ip6info->iniface,			ip6info->invflags&IP6T_INV_VIA_IN ?" (INV)":"");		return false;	}	for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {		ret |= (((const unsigned long *)outdev)[i]			^ ((const unsigned long *)ip6info->outiface)[i])			& ((const unsigned long *)ip6info->outiface_mask)[i];	}	if (FWINV(ret != 0, IP6T_INV_VIA_OUT)) {		dprintf("VIA out mismatch (%s vs %s).%s\n",			outdev, ip6info->outiface,			ip6info->invflags&IP6T_INV_VIA_OUT ?" (INV)":"");		return false;	}/* ... might want to do something with class and flowlabel here ... */	/* look for the desired protocol header */	if((ip6info->flags & IP6T_F_PROTO)) {		int protohdr;		unsigned short _frag_off;		protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off);		if (protohdr < 0) {			if (_frag_off == 0)				*hotdrop = true;			return false;		}		*fragoff = _frag_off;		dprintf("Packet protocol %hi ?= %s%hi.\n",				protohdr,				ip6info->invflags & IP6T_INV_PROTO ? "!":"",				ip6info->proto);		if (ip6info->proto == protohdr) {			if(ip6info->invflags & IP6T_INV_PROTO) {				return false;			}			return true;		}		/* We need match for the '-p all', too! */		if ((ip6info->proto != 0) &&			!(ip6info->invflags & IP6T_INV_PROTO))			return false;	}	return true;}/* should be ip6 safe */static inline boolip6_checkentry(const struct ip6t_ip6 *ipv6){	if (ipv6->flags & ~IP6T_F_MASK) {		duprintf("Unknown flag bits set: %08X\n",			 ipv6->flags & ~IP6T_F_MASK);		return false;	}	if (ipv6->invflags & ~IP6T_INV_MASK) {		duprintf("Unknown invflag bits set: %08X\n",			 ipv6->invflags & ~IP6T_INV_MASK);		return false;	}	return true;}static unsigned intip6t_error(struct sk_buff *skb,	  const struct net_device *in,	  const struct net_device *out,	  unsigned int hooknum,	  const struct xt_target *target,	  const void *targinfo){	if (net_ratelimit())		printk("ip6_tables: error: `%s'\n", (char *)targinfo);	return NF_DROP;}static inlinebool do_match(struct ip6t_entry_match *m,	      const struct sk_buff *skb,	      const struct net_device *in,	      const struct net_device *out,	      int offset,	      unsigned int protoff,	      bool *hotdrop){	/* Stop iteration if it doesn't match */	if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,				      offset, protoff, hotdrop))		return true;	else		return false;}static inline struct ip6t_entry *get_entry(void *base, unsigned int offset){	return (struct ip6t_entry *)(base + offset);}/* All zeroes == unconditional rule. */static inline intunconditional(const struct ip6t_ip6 *ipv6){	unsigned int i;	for (i = 0; i < sizeof(*ipv6); i++)		if (((char *)ipv6)[i])			break;	return (i == sizeof(*ipv6));}#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \    defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)/* This cries for unification! */static const char *hooknames[] = {	[NF_IP6_PRE_ROUTING]		= "PREROUTING",	[NF_IP6_LOCAL_IN]		= "INPUT",	[NF_IP6_FORWARD]		= "FORWARD",	[NF_IP6_LOCAL_OUT]		= "OUTPUT",	[NF_IP6_POST_ROUTING]		= "POSTROUTING",};enum nf_ip_trace_comments {	NF_IP6_TRACE_COMMENT_RULE,	NF_IP6_TRACE_COMMENT_RETURN,	NF_IP6_TRACE_COMMENT_POLICY,};static const char *comments[] = {	[NF_IP6_TRACE_COMMENT_RULE]	= "rule",	[NF_IP6_TRACE_COMMENT_RETURN]	= "return",	[NF_IP6_TRACE_COMMENT_POLICY]	= "policy",};static struct nf_loginfo trace_loginfo = {	.type = NF_LOG_TYPE_LOG,	.u = {		.log = {			.level = 4,			.logflags = NF_LOG_MASK,		},	},};static inline intget_chainname_rulenum(struct ip6t_entry *s, struct ip6t_entry *e,		      char *hookname, char **chainname,		      char **comment, unsigned int *rulenum){	struct ip6t_standard_target *t = (void *)ip6t_get_target(s);	if (strcmp(t->target.u.kernel.target->name, IP6T_ERROR_TARGET) == 0) {		/* Head of user chain: ERROR target with chainname */		*chainname = t->target.data;		(*rulenum) = 0;	} else if (s == e) {		(*rulenum)++;		if (s->target_offset == sizeof(struct ip6t_entry)		   && strcmp(t->target.u.kernel.target->name,			     IP6T_STANDARD_TARGET) == 0		   && t->verdict < 0		   && unconditional(&s->ipv6)) {			/* Tail of chains: STANDARD target (return/policy) */			*comment = *chainname == hookname				? (char *)comments[NF_IP6_TRACE_COMMENT_POLICY]				: (char *)comments[NF_IP6_TRACE_COMMENT_RETURN];		}		return 1;	} else		(*rulenum)++;	return 0;}static void trace_packet(struct sk_buff *skb,			 unsigned int hook,			 const struct net_device *in,			 const struct net_device *out,			 char *tablename,			 struct xt_table_info *private,			 struct ip6t_entry *e){	void *table_base;	struct ip6t_entry *root;	char *hookname, *chainname, *comment;	unsigned int rulenum = 0;	table_base = (void *)private->entries[smp_processor_id()];	root = get_entry(table_base, private->hook_entry[hook]);	hookname = chainname = (char *)hooknames[hook];	comment = (char *)comments[NF_IP6_TRACE_COMMENT_RULE];	IP6T_ENTRY_ITERATE(root,			   private->size - private->hook_entry[hook],			   get_chainname_rulenum,			   e, hookname, &chainname, &comment, &rulenum);	nf_log_packet(AF_INET6, hook, skb, in, out, &trace_loginfo,		      "TRACE: %s:%s:%s:%u ",		      tablename, chainname, comment, rulenum);}#endif/* Returns one of the generic firewall policies, like NF_ACCEPT. */unsigned intip6t_do_table(struct sk_buff *skb,	      unsigned int hook,	      const struct net_device *in,	      const struct net_device *out,	      struct xt_table *table){	static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));	int offset = 0;	unsigned int protoff = 0;	bool hotdrop = false;	/* Initializing verdict to NF_DROP keeps gcc happy. */	unsigned int verdict = NF_DROP;	const char *indev, *outdev;	void *table_base;	struct ip6t_entry *e, *back;	struct xt_table_info *private;	/* Initialization */	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. */	read_lock_bh(&table->lock);	private = table->private;	IP_NF_ASSERT(table->valid_hooks & (1 << hook));	table_base = (void *)private->entries[smp_processor_id()];	e = get_entry(table_base, private->hook_entry[hook]);	/* For return from builtin chain */	back = get_entry(table_base, private->underflow[hook]);	do {		IP_NF_ASSERT(e);		IP_NF_ASSERT(back);		if (ip6_packet_match(skb, indev, outdev, &e->ipv6,			&protoff, &offset, &hotdrop)) {			struct ip6t_entry_target *t;			if (IP6T_MATCH_ITERATE(e, do_match,					       skb, in, out,					       offset, protoff, &hotdrop) != 0)				goto no_match;			ADD_COUNTER(e->counters,				    ntohs(ipv6_hdr(skb)->payload_len)				    + IPV6_HDR_LEN,				    1);			t = ip6t_get_target(e);			IP_NF_ASSERT(t->u.kernel.target);#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \    defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)			/* The packet is traced: log it */			if (unlikely(skb->nf_trace))				trace_packet(skb, hook, in, out,					     table->name, private, e);#endif			/* Standard target? */			if (!t->u.kernel.target->target) {				int v;				v = ((struct ip6t_standard_target *)t)->verdict;				if (v < 0) {					/* Pop from stack? */					if (v != IP6T_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				    && !(e->ipv6.flags & IP6T_F_GOTO)) {					/* Save old back ptr in next entry */					struct ip6t_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 ip6t_entry *)table_base)->comefrom					= 0xeeeeeeec;#endif				verdict = t->u.kernel.target->target(skb,								     in, out,								     hook,								     t->u.kernel.target,								     t->data);#ifdef CONFIG_NETFILTER_DEBUG				if (((struct ip6t_entry *)table_base)->comefrom				    != 0xeeeeeeec				    && verdict == IP6T_CONTINUE) {					printk("Target %s reentered!\n",					       t->u.kernel.target->name);					verdict = NF_DROP;				}				((struct ip6t_entry *)table_base)->comefrom					= 0x57acc001;#endif				if (verdict == IP6T_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 ip6t_entry *)table_base)->comefrom = NETFILTER_LINK_POISON;#endif	read_unlock_bh(&table->lock);#ifdef DEBUG_ALLOW_ALL	return NF_ACCEPT;#else	if (hotdrop)		return NF_DROP;	else return verdict;#endif}/* 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 xt_table_info *newinfo,		   unsigned int valid_hooks, void *entry0){	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_IP6_NUMHOOKS; hook++) {		unsigned int pos = newinfo->hook_entry[hook];		struct ip6t_entry *e			= (struct ip6t_entry *)(entry0 + pos);		int visited = e->comefrom & (1 << hook);		if (!(valid_hooks & (1 << hook)))			continue;		/* Set initial back pointer. */		e->counters.pcnt = pos;		for (;;) {			struct ip6t_standard_target *t				= (void *)ip6t_get_target(e);			if (e->comefrom & (1 << NF_IP6_NUMHOOKS)) {				printk("iptables: loop hook %u pos %u %08X.\n",				       hook, pos, e->comefrom);				return 0;			}			e->comefrom				|= ((1 << hook) | (1 << NF_IP6_NUMHOOKS));			/* Unconditional return/END. */			if ((e->target_offset == sizeof(struct ip6t_entry)			    && (strcmp(t->target.u.user.name,				       IP6T_STANDARD_TARGET) == 0)			    && t->verdict < 0			    && unconditional(&e->ipv6)) || visited) {				unsigned int oldpos, size;				if (t->verdict < -NF_MAX_VERDICT - 1) {

⌨️ 快捷键说明

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