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

📄 ip6_tables.c

📁 上传linux-jx2410的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	struct ip6t_table *t;	int ret;	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)		return -EFAULT;	if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct ip6t_counters))		return -EINVAL;	paddc = vmalloc(len);	if (!paddc)		return -ENOMEM;	if (copy_from_user(paddc, user, len) != 0) {		ret = -EFAULT;		goto free;	}	t = find_table_lock(tmp.name, &ret, &ip6t_mutex);	if (!t)		goto free;	write_lock_bh(&t->lock);	if (t->private->number != paddc->num_counters) {		ret = -EINVAL;		goto unlock_up_free;	}	i = 0;	IP6T_ENTRY_ITERATE(t->private->entries,			  t->private->size,			  add_counter_to_entry,			  paddc->counters,			  &i); unlock_up_free:	write_unlock_bh(&t->lock);	up(&ip6t_mutex); free:	vfree(paddc);	return ret;}static intdo_ip6t_set_ctl(struct sock *sk,	int cmd, void *user, unsigned int len){	int ret;	if (!capable(CAP_NET_ADMIN))		return -EPERM;	switch (cmd) {	case IP6T_SO_SET_REPLACE:		ret = do_replace(user, len);		break;	case IP6T_SO_SET_ADD_COUNTERS:		ret = do_add_counters(user, len);		break;	default:		duprintf("do_ip6t_set_ctl:  unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}static intdo_ip6t_get_ctl(struct sock *sk, int cmd, void *user, int *len){	int ret;	if (!capable(CAP_NET_ADMIN))		return -EPERM;	switch (cmd) {	case IP6T_SO_GET_INFO: {		char name[IP6T_TABLE_MAXNAMELEN];		struct ip6t_table *t;		if (*len != sizeof(struct ip6t_getinfo)) {			duprintf("length %u != %u\n", *len,				 sizeof(struct ip6t_getinfo));			ret = -EINVAL;			break;		}		if (copy_from_user(name, user, sizeof(name)) != 0) {			ret = -EFAULT;			break;		}		name[IP6T_TABLE_MAXNAMELEN-1] = '\0';		t = find_table_lock(name, &ret, &ip6t_mutex);		if (t) {			struct ip6t_getinfo info;			info.valid_hooks = t->valid_hooks;			memcpy(info.hook_entry, t->private->hook_entry,			       sizeof(info.hook_entry));			memcpy(info.underflow, t->private->underflow,			       sizeof(info.underflow));			info.num_entries = t->private->number;			info.size = t->private->size;			strcpy(info.name, name);			if (copy_to_user(user, &info, *len) != 0)				ret = -EFAULT;			else				ret = 0;			up(&ip6t_mutex);		}	}	break;	case IP6T_SO_GET_ENTRIES: {		struct ip6t_get_entries get;		if (*len < sizeof(get)) {			duprintf("get_entries: %u < %u\n", *len, sizeof(get));			ret = -EINVAL;		} else if (copy_from_user(&get, user, sizeof(get)) != 0) {			ret = -EFAULT;		} else if (*len != sizeof(struct ip6t_get_entries) + get.size) {			duprintf("get_entries: %u != %u\n", *len,				 sizeof(struct ip6t_get_entries) + get.size);			ret = -EINVAL;		} else			ret = get_entries(&get, user);		break;	}	default:		duprintf("do_ip6t_get_ctl: unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}/* Registration hooks for targets. */intip6t_register_target(struct ip6t_target *target){	int ret;	MOD_INC_USE_COUNT;	ret = down_interruptible(&ip6t_mutex);	if (ret != 0) {		MOD_DEC_USE_COUNT;		return ret;	}	if (!list_named_insert(&ip6t_target, target)) {		duprintf("ip6t_register_target: `%s' already in list!\n",			 target->name);		ret = -EINVAL;		MOD_DEC_USE_COUNT;	}	up(&ip6t_mutex);	return ret;}voidip6t_unregister_target(struct ip6t_target *target){	down(&ip6t_mutex);	LIST_DELETE(&ip6t_target, target);	up(&ip6t_mutex);	MOD_DEC_USE_COUNT;}intip6t_register_match(struct ip6t_match *match){	int ret;	MOD_INC_USE_COUNT;	ret = down_interruptible(&ip6t_mutex);	if (ret != 0) {		MOD_DEC_USE_COUNT;		return ret;	}	if (!list_named_insert(&ip6t_match, match)) {		duprintf("ip6t_register_match: `%s' already in list!\n",			 match->name);		MOD_DEC_USE_COUNT;		ret = -EINVAL;	}	up(&ip6t_mutex);	return ret;}voidip6t_unregister_match(struct ip6t_match *match){	down(&ip6t_mutex);	LIST_DELETE(&ip6t_match, match);	up(&ip6t_mutex);	MOD_DEC_USE_COUNT;}int ip6t_register_table(struct ip6t_table *table){	int ret;	struct ip6t_table_info *newinfo;	static struct ip6t_table_info bootstrap		= { 0, 0, 0, { 0 }, { 0 }, { }, { } };	MOD_INC_USE_COUNT;	newinfo = vmalloc(sizeof(struct ip6t_table_info)			  + SMP_ALIGN(table->table->size) * smp_num_cpus);	if (!newinfo) {		ret = -ENOMEM;		MOD_DEC_USE_COUNT;		return ret;	}	memcpy(newinfo->entries, table->table->entries, table->table->size);	ret = translate_table(table->name, table->valid_hooks,			      newinfo, table->table->size,			      table->table->num_entries,			      table->table->hook_entry,			      table->table->underflow);	if (ret != 0) {		vfree(newinfo);		MOD_DEC_USE_COUNT;		return ret;	}	ret = down_interruptible(&ip6t_mutex);	if (ret != 0) {		vfree(newinfo);		MOD_DEC_USE_COUNT;		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);	table->lock = RW_LOCK_UNLOCKED;	list_prepend(&ip6t_tables, table); unlock:	up(&ip6t_mutex);	return ret; free_unlock:	vfree(newinfo);	MOD_DEC_USE_COUNT;	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);	MOD_DEC_USE_COUNT;}/* 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 > 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 = hdr;	const struct ip6t_tcp *tcpinfo = matchinfo;	/* 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;	}	/* 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 = hdr;	const struct ip6t_udp *udpinfo = matchinfo;	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;	}	/* 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= { { NULL, NULL }, IP6T_STANDARD_TARGET, NULL, NULL, NULL };static struct ip6t_target ip6t_error_target= { { NULL, NULL }, IP6T_ERROR_TARGET, ip6t_error, NULL, NULL };static struct nf_sockopt_ops ip6t_sockopts= { { NULL, NULL }, PF_INET6, IP6T_BASE_CTL, IP6T_SO_SET_MAX+1, do_ip6t_set_ctl,    IP6T_BASE_CTL, IP6T_SO_GET_MAX+1, do_ip6t_get_ctl, 0, NULL  };static struct ip6t_match tcp_matchstruct= { { NULL, NULL }, "tcp", &tcp_match, &tcp_checkentry, NULL };static struct ip6t_match udp_matchstruct= { { NULL, NULL }, "udp", &udp_match, &udp_checkentry, NULL };static struct ip6t_match icmp6_matchstruct= { { NULL, NULL }, "icmp6", &icmp6_match, &icmp6_checkentry, NULL };#ifdef CONFIG_PROC_FSstatic inline int print_name(const struct ip6t_table *t,			     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", t->name);		if (*pos + namelen > length) {			/* Stop iterating */			return 1;		}		*pos += namelen;	}	return 0;}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, struct ip6t_table *,		  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;}#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	if (!proc_net_create("ip6_tables_names", 0, ip6t_get_tables)) {		nf_unregister_sockopt(&ip6t_sockopts);		return -ENOMEM;	}#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	proc_net_remove("ip6_tables_names");#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);module_init(init);module_exit(fini);MODULE_LICENSE("GPL");

⌨️ 快捷键说明

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