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

📄 ip_tables.c

📁 linux2.6.16版本
💻 C
📖 第 1 页 / 共 3 页
字号:
		return -ENOMEM;	newinfo = xt_alloc_table_info(tmp.size);	if (!newinfo)		return -ENOMEM;	/* choose the copy that is our node/cpu */	loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),			   tmp.size) != 0) {		ret = -EFAULT;		goto free_newinfo;	}	counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));	if (!counters) {		ret = -ENOMEM;		goto free_newinfo;	}	ret = translate_table(tmp.name, tmp.valid_hooks,			      newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,			      tmp.hook_entry, tmp.underflow);	if (ret != 0)		goto free_newinfo_counters;	duprintf("ip_tables: Translated table\n");	t = try_then_request_module(xt_find_table_lock(AF_INET, tmp.name),				    "iptable_%s", tmp.name);	if (!t || IS_ERR(t)) {		ret = t ? PTR_ERR(t) : -ENOENT;		goto free_newinfo_counters_untrans;	}	/* You lied! */	if (tmp.valid_hooks != t->valid_hooks) {		duprintf("Valid hook crap: %08X vs %08X\n",			 tmp.valid_hooks, t->valid_hooks);		ret = -EINVAL;		goto put_module;	}	oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);	if (!oldinfo)		goto put_module;	/* Update module usage count based on number of rules */	duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",		oldinfo->number, oldinfo->initial_entries, newinfo->number);	if ((oldinfo->number > oldinfo->initial_entries) || 	    (newinfo->number <= oldinfo->initial_entries)) 		module_put(t->me);	if ((oldinfo->number > oldinfo->initial_entries) &&	    (newinfo->number <= oldinfo->initial_entries))		module_put(t->me);	/* Get the old counters. */	get_counters(oldinfo, counters);	/* Decrease module usage counts and free resource */	loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];	IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);	xt_free_table_info(oldinfo);	if (copy_to_user(tmp.counters, counters,			 sizeof(struct xt_counters) * tmp.num_counters) != 0)		ret = -EFAULT;	vfree(counters);	xt_table_unlock(t);	return ret; put_module:	module_put(t->me);	xt_table_unlock(t); free_newinfo_counters_untrans:	IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL); free_newinfo_counters:	vfree(counters); free_newinfo:	xt_free_table_info(newinfo);	return ret;}/* We're lazy, and add to the first CPU; overflow works its fey magic * and everything is OK. */static inline intadd_counter_to_entry(struct ipt_entry *e,		     const struct xt_counters addme[],		     unsigned int *i){#if 0	duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",		 *i,		 (long unsigned int)e->counters.pcnt,		 (long unsigned int)e->counters.bcnt,		 (long unsigned int)addme[*i].pcnt,		 (long unsigned int)addme[*i].bcnt);#endif	ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);	(*i)++;	return 0;}static intdo_add_counters(void __user *user, unsigned int len){	unsigned int i;	struct xt_counters_info tmp, *paddc;	struct ipt_table *t;	struct xt_table_info *private;	int ret = 0;	void *loc_cpu_entry;	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)		return -EFAULT;	if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))		return -EINVAL;	paddc = vmalloc_node(len, numa_node_id());	if (!paddc)		return -ENOMEM;	if (copy_from_user(paddc, user, len) != 0) {		ret = -EFAULT;		goto free;	}	t = xt_find_table_lock(AF_INET, tmp.name);	if (!t || IS_ERR(t)) {		ret = t ? PTR_ERR(t) : -ENOENT;		goto free;	}	write_lock_bh(&t->lock);	private = t->private;	if (private->number != paddc->num_counters) {		ret = -EINVAL;		goto unlock_up_free;	}	i = 0;	/* Choose the copy that is on our node */	loc_cpu_entry = private->entries[raw_smp_processor_id()];	IPT_ENTRY_ITERATE(loc_cpu_entry,			  private->size,			  add_counter_to_entry,			  paddc->counters,			  &i); unlock_up_free:	write_unlock_bh(&t->lock);	xt_table_unlock(t);	module_put(t->me); free:	vfree(paddc);	return ret;}static intdo_ipt_set_ctl(struct sock *sk,	int cmd, void __user *user, unsigned int len){	int ret;	if (!capable(CAP_NET_ADMIN))		return -EPERM;	switch (cmd) {	case IPT_SO_SET_REPLACE:		ret = do_replace(user, len);		break;	case IPT_SO_SET_ADD_COUNTERS:		ret = do_add_counters(user, len);		break;	default:		duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}static intdo_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len){	int ret;	if (!capable(CAP_NET_ADMIN))		return -EPERM;	switch (cmd) {	case IPT_SO_GET_INFO: {		char name[IPT_TABLE_MAXNAMELEN];		struct ipt_table *t;		if (*len != sizeof(struct ipt_getinfo)) {			duprintf("length %u != %u\n", *len,				 sizeof(struct ipt_getinfo));			ret = -EINVAL;			break;		}		if (copy_from_user(name, user, sizeof(name)) != 0) {			ret = -EFAULT;			break;		}		name[IPT_TABLE_MAXNAMELEN-1] = '\0';		t = try_then_request_module(xt_find_table_lock(AF_INET, name),					    "iptable_%s", name);		if (t && !IS_ERR(t)) {			struct ipt_getinfo info;			struct xt_table_info *private = t->private;			info.valid_hooks = t->valid_hooks;			memcpy(info.hook_entry, private->hook_entry,			       sizeof(info.hook_entry));			memcpy(info.underflow, private->underflow,			       sizeof(info.underflow));			info.num_entries = private->number;			info.size = private->size;			memcpy(info.name, name, sizeof(info.name));			if (copy_to_user(user, &info, *len) != 0)				ret = -EFAULT;			else				ret = 0;			xt_table_unlock(t);			module_put(t->me);		} else			ret = t ? PTR_ERR(t) : -ENOENT;	}	break;	case IPT_SO_GET_ENTRIES: {		struct ipt_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 ipt_get_entries) + get.size) {			duprintf("get_entries: %u != %u\n", *len,				 sizeof(struct ipt_get_entries) + get.size);			ret = -EINVAL;		} else			ret = get_entries(&get, user);		break;	}	case IPT_SO_GET_REVISION_MATCH:	case IPT_SO_GET_REVISION_TARGET: {		struct ipt_get_revision rev;		int target;		if (*len != sizeof(rev)) {			ret = -EINVAL;			break;		}		if (copy_from_user(&rev, user, sizeof(rev)) != 0) {			ret = -EFAULT;			break;		}		if (cmd == IPT_SO_GET_REVISION_TARGET)			target = 1;		else			target = 0;		try_then_request_module(xt_find_revision(AF_INET, rev.name,							 rev.revision,							 target, &ret),					"ipt_%s", rev.name);		break;	}	default:		duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl){	int ret;	struct xt_table_info *newinfo;	static struct xt_table_info bootstrap		= { 0, 0, 0, { 0 }, { 0 }, { } };	void *loc_cpu_entry;	newinfo = xt_alloc_table_info(repl->size);	if (!newinfo)		return -ENOMEM;	/* choose the copy on our node/cpu	 * but dont care of preemption	 */	loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];	memcpy(loc_cpu_entry, repl->entries, repl->size);	ret = translate_table(table->name, table->valid_hooks,			      newinfo, loc_cpu_entry, repl->size,			      repl->num_entries,			      repl->hook_entry,			      repl->underflow);	if (ret != 0) {		xt_free_table_info(newinfo);		return ret;	}	if (xt_register_table(table, &bootstrap, newinfo) != 0) {		xt_free_table_info(newinfo);		return ret;	}	return 0;}void ipt_unregister_table(struct ipt_table *table){	struct xt_table_info *private;	void *loc_cpu_entry; 	private = xt_unregister_table(table);	/* Decrease module usage counts and free resources */	loc_cpu_entry = private->entries[raw_smp_processor_id()];	IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);	xt_free_table_info(private);}/* Returns 1 if the type and code is matched by the range, 0 otherwise */static inline inticmp_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 ((test_type == 0xFF) || (type == test_type && code >= min_code && code <= max_code))		^ invert;}static inticmp_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 icmphdr _icmph, *ic;	const struct ipt_icmp *icmpinfo = matchinfo;	/* Must not be a fragment. */	if (offset)		return 0;	ic = skb_header_pointer(skb, protoff, sizeof(_icmph), &_icmph);	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 icmp_type_code_match(icmpinfo->type,				    icmpinfo->code[0],				    icmpinfo->code[1],				    ic->type, ic->code,				    !!(icmpinfo->invflags&IPT_ICMP_INV));}/* Called when user tries to insert an entry of this type. */static inticmp_checkentry(const char *tablename,	   const void *info,	   void *matchinfo,	   unsigned int matchsize,	   unsigned int hook_mask){	const struct ipt_ip *ip = info;	const struct ipt_icmp *icmpinfo = matchinfo;	/* Must specify proto == ICMP, and no unknown invflags */	return ip->proto == IPPROTO_ICMP		&& !(ip->invflags & IPT_INV_PROTO)		&& matchsize == IPT_ALIGN(sizeof(struct ipt_icmp))		&& !(icmpinfo->invflags & ~IPT_ICMP_INV);}/* The built-in targets: standard (NULL) and error. */static struct ipt_target ipt_standard_target = {	.name		= IPT_STANDARD_TARGET,};static struct ipt_target ipt_error_target = {	.name		= IPT_ERROR_TARGET,	.target		= ipt_error,};static struct nf_sockopt_ops ipt_sockopts = {	.pf		= PF_INET,	.set_optmin	= IPT_BASE_CTL,	.set_optmax	= IPT_SO_SET_MAX+1,	.set		= do_ipt_set_ctl,	.get_optmin	= IPT_BASE_CTL,	.get_optmax	= IPT_SO_GET_MAX+1,	.get		= do_ipt_get_ctl,};static struct ipt_match icmp_matchstruct = {	.name		= "icmp",	.match		= &icmp_match,	.checkentry	= &icmp_checkentry,};static int __init init(void){	int ret;	xt_proto_init(AF_INET);	/* Noone else will be downing sem now, so we won't sleep */	xt_register_target(AF_INET, &ipt_standard_target);	xt_register_target(AF_INET, &ipt_error_target);	xt_register_match(AF_INET, &icmp_matchstruct);	/* Register setsockopt */	ret = nf_register_sockopt(&ipt_sockopts);	if (ret < 0) {		duprintf("Unable to register sockopts.\n");		return ret;	}	printk("ip_tables: (C) 2000-2006 Netfilter Core Team\n");	return 0;}static void __exit fini(void){	nf_unregister_sockopt(&ipt_sockopts);	xt_unregister_match(AF_INET, &icmp_matchstruct);	xt_unregister_target(AF_INET, &ipt_error_target);	xt_unregister_target(AF_INET, &ipt_standard_target);	xt_proto_fini(AF_INET);}EXPORT_SYMBOL(ipt_register_table);EXPORT_SYMBOL(ipt_unregister_table);EXPORT_SYMBOL(ipt_do_table);module_init(init);module_exit(fini);

⌨️ 快捷键说明

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