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

📄 ip6_tables.c

📁 h内核
💻 C
📖 第 1 页 / 共 4 页
字号:
			      repl->num_entries,			      repl->hook_entry,			      repl->underflow);	if (ret != 0) {		vfree(newinfo);		return ret;	}	ret = down_interruptible(&ip6t_mutex);	if (ret != 0) {		vfree(newinfo);		return ret;	}	/* Don't autoload: we'd eat our tail... */	if (list_named_find(&ip6t_tables, table->name)) {		ret = -EEXIST;		goto free_unlock;	}	/* Simplifies replace_table code. */	table->private = &bootstrap;	if (!replace_table(table, 0, newinfo, &ret))		goto free_unlock;	duprintf("table->private->number = %u\n",		 table->private->number);	/* save number of initial entries */	table->private->initial_entries = table->private->number;	rwlock_init(&table->lock);	list_prepend(&ip6t_tables, table); unlock:	up(&ip6t_mutex);	return ret; free_unlock:	vfree(newinfo);	goto unlock;}void ip6t_unregister_table(struct ip6t_table *table){	down(&ip6t_mutex);	LIST_DELETE(&ip6t_tables, table);	up(&ip6t_mutex);	/* Decrease module usage counts and free resources */	IP6T_ENTRY_ITERATE(table->private->entries, table->private->size,			  cleanup_entry, NULL);	vfree(table->private);}/* Returns 1 if the port is matched by the range, 0 otherwise */static inline intport_match(u_int16_t min, u_int16_t max, u_int16_t port, int invert){	int ret;	ret = (port >= min && port <= max) ^ invert;	return ret;}static inttcp_find_option(u_int8_t option,		const struct sk_buff *skb,		unsigned int tcpoff,		unsigned int optlen,		int invert,		int *hotdrop){	/* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */	u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;	unsigned int i;	duprintf("tcp_match: finding option\n");	if (!optlen)		return invert;	/* If we don't have the whole header, drop packet. */	op = skb_header_pointer(skb, tcpoff + sizeof(struct tcphdr), optlen,				_opt);	if (op == NULL) {		*hotdrop = 1;		return 0;	}	for (i = 0; i < optlen; ) {		if (op[i] == option) return !invert;		if (op[i] < 2) i++;		else i += op[i+1]?:1;	}	return invert;}static inttcp_match(const struct sk_buff *skb,	  const struct net_device *in,	  const struct net_device *out,	  const void *matchinfo,	  int offset,	  unsigned int protoff,	  int *hotdrop){	struct tcphdr _tcph, *th;	const struct ip6t_tcp *tcpinfo = matchinfo;	if (offset) {		/* To quote Alan:		   Don't allow a fragment of TCP 8 bytes in. Nobody normal		   causes this. Its a cracker trying to break in by doing a		   flag overwrite to pass the direction checks.		*/		if (offset == 1) {			duprintf("Dropping evil TCP offset=1 frag.\n");			*hotdrop = 1;		}		/* Must not be a fragment. */		return 0;	}#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))	th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);	if (th == NULL) {		/* We've been asked to examine this packet, and we		   can't.  Hence, no choice but to drop. */		duprintf("Dropping evil TCP offset=0 tinygram.\n");		*hotdrop = 1;		return 0;	}	if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],			ntohs(th->source),			!!(tcpinfo->invflags & IP6T_TCP_INV_SRCPT)))		return 0;	if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],			ntohs(th->dest),			!!(tcpinfo->invflags & IP6T_TCP_INV_DSTPT)))		return 0;	if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)		      == tcpinfo->flg_cmp,		      IP6T_TCP_INV_FLAGS))		return 0;	if (tcpinfo->option) {		if (th->doff * 4 < sizeof(_tcph)) {			*hotdrop = 1;			return 0;		}		if (!tcp_find_option(tcpinfo->option, skb, protoff,				     th->doff*4 - sizeof(*th),				     tcpinfo->invflags & IP6T_TCP_INV_OPTION,				     hotdrop))			return 0;	}	return 1;}/* Called when user tries to insert an entry of this type. */static inttcp_checkentry(const char *tablename,	       const struct ip6t_ip6 *ipv6,	       void *matchinfo,	       unsigned int matchsize,	       unsigned int hook_mask){	const struct ip6t_tcp *tcpinfo = matchinfo;	/* Must specify proto == TCP, and no unknown invflags */	return ipv6->proto == IPPROTO_TCP		&& !(ipv6->invflags & IP6T_INV_PROTO)		&& matchsize == IP6T_ALIGN(sizeof(struct ip6t_tcp))		&& !(tcpinfo->invflags & ~IP6T_TCP_INV_MASK);}static intudp_match(const struct sk_buff *skb,	  const struct net_device *in,	  const struct net_device *out,	  const void *matchinfo,	  int offset,	  unsigned int protoff,	  int *hotdrop){	struct udphdr _udph, *uh;	const struct ip6t_udp *udpinfo = matchinfo;	/* Must not be a fragment. */	if (offset)		return 0;	uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);	if (uh == NULL) {		/* We've been asked to examine this packet, and we		   can't.  Hence, no choice but to drop. */		duprintf("Dropping evil UDP tinygram.\n");		*hotdrop = 1;		return 0;	}	return port_match(udpinfo->spts[0], udpinfo->spts[1],			  ntohs(uh->source),			  !!(udpinfo->invflags & IP6T_UDP_INV_SRCPT))		&& port_match(udpinfo->dpts[0], udpinfo->dpts[1],			      ntohs(uh->dest),			      !!(udpinfo->invflags & IP6T_UDP_INV_DSTPT));}/* Called when user tries to insert an entry of this type. */static intudp_checkentry(const char *tablename,	       const struct ip6t_ip6 *ipv6,	       void *matchinfo,	       unsigned int matchinfosize,	       unsigned int hook_mask){	const struct ip6t_udp *udpinfo = matchinfo;	/* Must specify proto == UDP, and no unknown invflags */	if (ipv6->proto != IPPROTO_UDP || (ipv6->invflags & IP6T_INV_PROTO)) {		duprintf("ip6t_udp: Protocol %u != %u\n", ipv6->proto,			 IPPROTO_UDP);		return 0;	}	if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_udp))) {		duprintf("ip6t_udp: matchsize %u != %u\n",			 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_udp)));		return 0;	}	if (udpinfo->invflags & ~IP6T_UDP_INV_MASK) {		duprintf("ip6t_udp: unknown flags %X\n",			 udpinfo->invflags);		return 0;	}	return 1;}/* Returns 1 if the type and code is matched by the range, 0 otherwise */static inline inticmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,		     u_int8_t type, u_int8_t code,		     int invert){	return (type == test_type && code >= min_code && code <= max_code)		^ invert;}static inticmp6_match(const struct sk_buff *skb,	   const struct net_device *in,	   const struct net_device *out,	   const void *matchinfo,	   int offset,	   unsigned int protoff,	   int *hotdrop){	struct icmp6hdr _icmp, *ic;	const struct ip6t_icmp *icmpinfo = matchinfo;	/* Must not be a fragment. */	if (offset)		return 0;	ic = skb_header_pointer(skb, protoff, sizeof(_icmp), &_icmp);	if (ic == NULL) {		/* We've been asked to examine this packet, and we		   can't.  Hence, no choice but to drop. */		duprintf("Dropping evil ICMP tinygram.\n");		*hotdrop = 1;		return 0;	}	return icmp6_type_code_match(icmpinfo->type,				     icmpinfo->code[0],				     icmpinfo->code[1],				     ic->icmp6_type, ic->icmp6_code,				     !!(icmpinfo->invflags&IP6T_ICMP_INV));}/* Called when user tries to insert an entry of this type. */static inticmp6_checkentry(const char *tablename,	   const struct ip6t_ip6 *ipv6,	   void *matchinfo,	   unsigned int matchsize,	   unsigned int hook_mask){	const struct ip6t_icmp *icmpinfo = matchinfo;	/* Must specify proto == ICMP, and no unknown invflags */	return ipv6->proto == IPPROTO_ICMPV6		&& !(ipv6->invflags & IP6T_INV_PROTO)		&& matchsize == IP6T_ALIGN(sizeof(struct ip6t_icmp))		&& !(icmpinfo->invflags & ~IP6T_ICMP_INV);}/* The built-in targets: standard (NULL) and error. */static struct ip6t_target ip6t_standard_target = {	.name		= IP6T_STANDARD_TARGET,};static struct ip6t_target ip6t_error_target = {	.name		= IP6T_ERROR_TARGET,	.target		= ip6t_error,};static struct nf_sockopt_ops ip6t_sockopts = {	.pf		= PF_INET6,	.set_optmin	= IP6T_BASE_CTL,	.set_optmax	= IP6T_SO_SET_MAX+1,	.set		= do_ip6t_set_ctl,	.get_optmin	= IP6T_BASE_CTL,	.get_optmax	= IP6T_SO_GET_MAX+1,	.get		= do_ip6t_get_ctl,};static struct ip6t_match tcp_matchstruct = {	.name		= "tcp",	.match		= &tcp_match,	.checkentry	= &tcp_checkentry,};static struct ip6t_match udp_matchstruct = {	.name		= "udp",	.match		= &udp_match,	.checkentry	= &udp_checkentry,};static struct ip6t_match icmp6_matchstruct = {	.name		= "icmp6",	.match		= &icmp6_match,	.checkentry	= &icmp6_checkentry,};#ifdef CONFIG_PROC_FSstatic inline int print_name(const char *i,			     off_t start_offset, char *buffer, int length,			     off_t *pos, unsigned int *count){	if ((*count)++ >= start_offset) {		unsigned int namelen;		namelen = sprintf(buffer + *pos, "%s\n",				  i + sizeof(struct list_head));		if (*pos + namelen > length) {			/* Stop iterating */			return 1;		}		*pos += namelen;	}	return 0;}static inline int print_target(const struct ip6t_target *t,                               off_t start_offset, char *buffer, int length,                               off_t *pos, unsigned int *count){	if (t == &ip6t_standard_target || t == &ip6t_error_target)		return 0;	return print_name((char *)t, start_offset, buffer, length, pos, count);}static int ip6t_get_tables(char *buffer, char **start, off_t offset, int length){	off_t pos = 0;	unsigned int count = 0;	if (down_interruptible(&ip6t_mutex) != 0)		return 0;	LIST_FIND(&ip6t_tables, print_name, char *,		  offset, buffer, length, &pos, &count);	up(&ip6t_mutex);	/* `start' hack - see fs/proc/generic.c line ~105 */	*start=(char *)((unsigned long)count-offset);	return pos;}static int ip6t_get_targets(char *buffer, char **start, off_t offset, int length){	off_t pos = 0;	unsigned int count = 0;	if (down_interruptible(&ip6t_mutex) != 0)		return 0;	LIST_FIND(&ip6t_target, print_target, struct ip6t_target *,		  offset, buffer, length, &pos, &count);	up(&ip6t_mutex);	*start = (char *)((unsigned long)count - offset);	return pos;}static int ip6t_get_matches(char *buffer, char **start, off_t offset, int length){	off_t pos = 0;	unsigned int count = 0;	if (down_interruptible(&ip6t_mutex) != 0)		return 0;	LIST_FIND(&ip6t_match, print_name, char *,		  offset, buffer, length, &pos, &count);	up(&ip6t_mutex);	*start = (char *)((unsigned long)count - offset);	return pos;}static struct { char *name; get_info_t *get_info; } ip6t_proc_entry[] ={ { "ip6_tables_names", ip6t_get_tables },  { "ip6_tables_targets", ip6t_get_targets },  { "ip6_tables_matches", ip6t_get_matches },  { NULL, NULL} };#endif /*CONFIG_PROC_FS*/static int __init init(void){	int ret;	/* Noone else will be downing sem now, so we won't sleep */	down(&ip6t_mutex);	list_append(&ip6t_target, &ip6t_standard_target);	list_append(&ip6t_target, &ip6t_error_target);	list_append(&ip6t_match, &tcp_matchstruct);	list_append(&ip6t_match, &udp_matchstruct);	list_append(&ip6t_match, &icmp6_matchstruct);	up(&ip6t_mutex);	/* Register setsockopt */	ret = nf_register_sockopt(&ip6t_sockopts);	if (ret < 0) {		duprintf("Unable to register sockopts.\n");		return ret;	}#ifdef CONFIG_PROC_FS	{		struct proc_dir_entry *proc;		int i;		for (i = 0; ip6t_proc_entry[i].name; i++) {			proc = proc_net_create(ip6t_proc_entry[i].name, 0,					       ip6t_proc_entry[i].get_info);			if (!proc) {				while (--i >= 0)				       proc_net_remove(ip6t_proc_entry[i].name);				nf_unregister_sockopt(&ip6t_sockopts);				return -ENOMEM;			}			proc->owner = THIS_MODULE;		}	}#endif	printk("ip6_tables: (C) 2000-2002 Netfilter core team\n");	return 0;}static void __exit fini(void){	nf_unregister_sockopt(&ip6t_sockopts);#ifdef CONFIG_PROC_FS	{		int i;		for (i = 0; ip6t_proc_entry[i].name; i++)			proc_net_remove(ip6t_proc_entry[i].name);	}#endif}EXPORT_SYMBOL(ip6t_register_table);EXPORT_SYMBOL(ip6t_unregister_table);EXPORT_SYMBOL(ip6t_do_table);EXPORT_SYMBOL(ip6t_register_match);EXPORT_SYMBOL(ip6t_unregister_match);EXPORT_SYMBOL(ip6t_register_target);EXPORT_SYMBOL(ip6t_unregister_target);EXPORT_SYMBOL(ip6t_ext_hdr);module_init(init);module_exit(fini);

⌨️ 快捷键说明

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