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

📄 ip6_tables.c

📁 Linux Kernel 2.6.9 for OMAP1710
💻 C
📖 第 1 页 / 共 4 页
字号:
	}	/* 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;	table->lock = RW_LOCK_UNLOCKED;	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 tcphdr *tcp,		u_int16_t datalen,		int invert,		int *hotdrop){	unsigned int i = sizeof(struct tcphdr);	const u_int8_t *opt = (u_int8_t *)tcp;	duprintf("tcp_match: finding option\n");	/* If we don't have the whole header, drop packet. */	if (tcp->doff * 4 < sizeof(struct tcphdr) ||	    tcp->doff * 4 > datalen) {		*hotdrop = 1;		return 0;	}	while (i < tcp->doff * 4) {		if (opt[i] == option) return !invert;		if (opt[i] < 2) i++;		else i += opt[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,	  const void *hdr,	  u_int16_t datalen,	  int *hotdrop){	const struct tcphdr *tcp;	const struct ip6t_tcp *tcpinfo = matchinfo;	int tcpoff;	u8 nexthdr = skb->nh.ipv6h->nexthdr;	/* 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;		return 0;	} else if (offset == 0 && datalen < sizeof(struct tcphdr)) {		/* 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;	}	tcpoff = (u8*)(skb->nh.ipv6h + 1) - skb->data;	tcpoff = ipv6_skip_exthdr(skb, tcpoff, &nexthdr, skb->len - tcpoff);	if (tcpoff < 0 || tcpoff > skb->len) {		duprintf("tcp_match: cannot skip exthdr. Dropping.\n");		*hotdrop = 1;		return 0;	} else if (nexthdr == IPPROTO_FRAGMENT)		return 0;	else if (nexthdr != IPPROTO_TCP ||		 skb->len - tcpoff < sizeof(struct tcphdr)) {		/* cannot be occured */		duprintf("tcp_match: cannot get TCP header. Dropping.\n");		*hotdrop = 1;		return 0;	}	tcp = (struct tcphdr *)(skb->data + tcpoff);	/* FIXME: Try tcp doff >> packet len against various stacks --RR */#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))	/* Must not be a fragment. */	return !offset		&& port_match(tcpinfo->spts[0], tcpinfo->spts[1],			      ntohs(tcp->source),			      !!(tcpinfo->invflags & IP6T_TCP_INV_SRCPT))		&& port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],			      ntohs(tcp->dest),			      !!(tcpinfo->invflags & IP6T_TCP_INV_DSTPT))		&& FWINVTCP((((unsigned char *)tcp)[13]			     & tcpinfo->flg_mask)			    == tcpinfo->flg_cmp,			    IP6T_TCP_INV_FLAGS)		&& (!tcpinfo->option		    || tcp_find_option(tcpinfo->option, tcp, datalen,				       tcpinfo->invflags				       & IP6T_TCP_INV_OPTION,				       hotdrop));}/* 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,	  const void *hdr,	  u_int16_t datalen,	  int *hotdrop){	const struct udphdr *udp;	const struct ip6t_udp *udpinfo = matchinfo;	int udpoff;	u8 nexthdr = skb->nh.ipv6h->nexthdr;	if (offset == 0 && datalen < sizeof(struct udphdr)) {		/* 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;	}	udpoff = (u8*)(skb->nh.ipv6h + 1) - skb->data;	udpoff = ipv6_skip_exthdr(skb, udpoff, &nexthdr, skb->len - udpoff);	if (udpoff < 0 || udpoff > skb->len) {		duprintf("udp_match: cannot skip exthdr. Dropping.\n");		*hotdrop = 1;		return 0;	} else if (nexthdr == IPPROTO_FRAGMENT)		return 0;	else if (nexthdr != IPPROTO_UDP ||		 skb->len - udpoff < sizeof(struct udphdr)) {		duprintf("udp_match: cannot get UDP header. Dropping.\n");		*hotdrop = 1;		return 0;	}	udp = (struct udphdr *)(skb->data + udpoff);	/* Must not be a fragment. */	return !offset		&& port_match(udpinfo->spts[0], udpinfo->spts[1],			      ntohs(udp->source),			      !!(udpinfo->invflags & IP6T_UDP_INV_SRCPT))		&& port_match(udpinfo->dpts[0], udpinfo->dpts[1],			      ntohs(udp->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,	   const void *hdr,	   u_int16_t datalen,	   int *hotdrop){	const struct icmp6hdr *icmp = hdr;	const struct ip6t_icmp *icmpinfo = matchinfo;	if (offset == 0 && datalen < 2) {		/* 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;	}	/* Must not be a fragment. */	return !offset		&& icmp6_type_code_match(icmpinfo->type,					icmpinfo->code[0],					icmpinfo->code[1],					icmp->icmp6_type, icmp->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_find_target_lock);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 + -