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

📄 ip_tables.c

📁 LINUX 2.6.17.4的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
	struct ipt_table *t;	if (*len < sizeof(get)) {		duprintf("get_entries: %u < %d\n", *len,				(unsigned int)sizeof(get));		return -EINVAL;	}	if (copy_from_user(&get, uptr, sizeof(get)) != 0)		return -EFAULT;	if (*len != sizeof(struct ipt_get_entries) + get.size) {		duprintf("get_entries: %u != %u\n", *len,				(unsigned int)(sizeof(struct ipt_get_entries) +				get.size));		return -EINVAL;	}	t = xt_find_table_lock(AF_INET, get.name);	if (t && !IS_ERR(t)) {		struct xt_table_info *private = t->private;		duprintf("t->private->number = %u\n",			 private->number);		if (get.size == private->size)			ret = copy_entries_to_user(private->size,						   t, uptr->entrytable);		else {			duprintf("get_entries: I've got %u not %u!\n",				 private->size,				 get.size);			ret = -EINVAL;		}		module_put(t->me);		xt_table_unlock(t);	} else		ret = t ? PTR_ERR(t) : -ENOENT;	return ret;}static int__do_replace(const char *name, unsigned int valid_hooks,		struct xt_table_info *newinfo, unsigned int num_counters,		void __user *counters_ptr){	int ret;	struct ipt_table *t;	struct xt_table_info *oldinfo;	struct xt_counters *counters;	void *loc_cpu_old_entry;	ret = 0;	counters = vmalloc(num_counters * sizeof(struct xt_counters));	if (!counters) {		ret = -ENOMEM;		goto out;	}	t = try_then_request_module(xt_find_table_lock(AF_INET, name),				    "iptable_%s", name);	if (!t || IS_ERR(t)) {		ret = t ? PTR_ERR(t) : -ENOENT;		goto free_newinfo_counters_untrans;	}	/* You lied! */	if (valid_hooks != t->valid_hooks) {		duprintf("Valid hook crap: %08X vs %08X\n",			 valid_hooks, t->valid_hooks);		ret = -EINVAL;		goto put_module;	}	oldinfo = xt_replace_table(t, 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(counters_ptr, counters,			 sizeof(struct xt_counters) * 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:	vfree(counters); out:	return ret;}static intdo_replace(void __user *user, unsigned int len){	int ret;	struct ipt_replace tmp;	struct xt_table_info *newinfo;	void *loc_cpu_entry;	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)		return -EFAULT;	/* Hack: Causes ipchains to give correct error msg --RR */	if (len != sizeof(tmp) + tmp.size)		return -ENOPROTOOPT;	/* overflow check */	if (tmp.size >= (INT_MAX - sizeof(struct xt_table_info)) / NR_CPUS -			SMP_CACHE_BYTES)		return -ENOMEM;	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))		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;	}	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;	duprintf("ip_tables: Translated table\n");	ret = __do_replace(tmp.name, tmp.valid_hooks,			      newinfo, tmp.num_counters,			      tmp.counters);	if (ret)		goto free_newinfo_untrans;	return 0; free_newinfo_untrans:	IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL); 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, int compat){	unsigned int i;	struct xt_counters_info tmp;	struct xt_counters *paddc;	unsigned int num_counters;	char *name;	int size;	void *ptmp;	struct ipt_table *t;	struct xt_table_info *private;	int ret = 0;	void *loc_cpu_entry;#ifdef CONFIG_COMPAT	struct compat_xt_counters_info compat_tmp;	if (compat) {		ptmp = &compat_tmp;		size = sizeof(struct compat_xt_counters_info);	} else#endif	{		ptmp = &tmp;		size = sizeof(struct xt_counters_info);	}	if (copy_from_user(ptmp, user, size) != 0)		return -EFAULT;#ifdef CONFIG_COMPAT	if (compat) {		num_counters = compat_tmp.num_counters;		name = compat_tmp.name;	} else#endif	{		num_counters = tmp.num_counters;		name = tmp.name;	}	if (len != size + num_counters * sizeof(struct xt_counters))		return -EINVAL;	paddc = vmalloc_node(len - size, numa_node_id());	if (!paddc)		return -ENOMEM;	if (copy_from_user(paddc, user + size, len - size) != 0) {		ret = -EFAULT;		goto free;	}	t = xt_find_table_lock(AF_INET, 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 != 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,			  &i); unlock_up_free:	write_unlock_bh(&t->lock);	xt_table_unlock(t);	module_put(t->me); free:	vfree(paddc);	return ret;}#ifdef CONFIG_COMPATstruct compat_ipt_replace {	char			name[IPT_TABLE_MAXNAMELEN];	u32			valid_hooks;	u32			num_entries;	u32			size;	u32			hook_entry[NF_IP_NUMHOOKS];	u32			underflow[NF_IP_NUMHOOKS];	u32			num_counters;	compat_uptr_t		counters;	/* struct ipt_counters * */	struct compat_ipt_entry	entries[0];};static inline int compat_copy_match_to_user(struct ipt_entry_match *m,		void __user **dstptr, compat_uint_t *size){	if (m->u.kernel.match->compat)		return m->u.kernel.match->compat(m, dstptr, size,				COMPAT_TO_USER);	else		return xt_compat_match(m, dstptr, size, COMPAT_TO_USER);}static int compat_copy_entry_to_user(struct ipt_entry *e,		void __user **dstptr, compat_uint_t *size){	struct ipt_entry_target __user *t;	struct compat_ipt_entry __user *ce;	u_int16_t target_offset, next_offset;	compat_uint_t origsize;	int ret;	ret = -EFAULT;	origsize = *size;	ce = (struct compat_ipt_entry __user *)*dstptr;	if (copy_to_user(ce, e, sizeof(struct ipt_entry)))		goto out;	*dstptr += sizeof(struct compat_ipt_entry);	ret = IPT_MATCH_ITERATE(e, compat_copy_match_to_user, dstptr, size);	target_offset = e->target_offset - (origsize - *size);	if (ret)		goto out;	t = ipt_get_target(e);	if (t->u.kernel.target->compat)		ret = t->u.kernel.target->compat(t, dstptr, size,				COMPAT_TO_USER);	else		ret = xt_compat_target(t, dstptr, size, COMPAT_TO_USER);	if (ret)		goto out;	ret = -EFAULT;	next_offset = e->next_offset - (origsize - *size);	if (put_user(target_offset, &ce->target_offset))		goto out;	if (put_user(next_offset, &ce->next_offset))		goto out;	return 0;out:	return ret;}static inline intcompat_check_calc_match(struct ipt_entry_match *m,	    const char *name,	    const struct ipt_ip *ip,	    unsigned int hookmask,	    int *size, int *i){	struct ipt_match *match;	match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,						   m->u.user.revision),					"ipt_%s", m->u.user.name);	if (IS_ERR(match) || !match) {		duprintf("compat_check_calc_match: `%s' not found\n",				m->u.user.name);		return match ? PTR_ERR(match) : -ENOENT;	}	m->u.kernel.match = match;	if (m->u.kernel.match->compat)		m->u.kernel.match->compat(m, NULL, size, COMPAT_CALC_SIZE);	else		xt_compat_match(m, NULL, size, COMPAT_CALC_SIZE);	(*i)++;	return 0;}static inline intcheck_compat_entry_size_and_hooks(struct ipt_entry *e,			   struct xt_table_info *newinfo,			   unsigned int *size,			   unsigned char *base,			   unsigned char *limit,			   unsigned int *hook_entries,			   unsigned int *underflows,			   unsigned int *i,			   const char *name){	struct ipt_entry_target *t;	struct ipt_target *target;	u_int16_t entry_offset;	int ret, off, h, j;	duprintf("check_compat_entry_size_and_hooks %p\n", e);	if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0	    || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {		duprintf("Bad offset %p, limit = %p\n", e, limit);		return -EINVAL;	}	if (e->next_offset < sizeof(struct compat_ipt_entry) +			sizeof(struct compat_xt_entry_target)) {		duprintf("checking: element %p size %u\n",			 e, e->next_offset);		return -EINVAL;	}	if (!ip_checkentry(&e->ip)) {		duprintf("ip_tables: ip check failed %p %s.\n", e, name);		return -EINVAL;	}	off = 0;	entry_offset = (void *)e - (void *)base;	j = 0;	ret = IPT_MATCH_ITERATE(e, compat_check_calc_match, name, &e->ip,			e->comefrom, &off, &j);	if (ret != 0)		goto out;	t = ipt_get_target(e);	target = try_then_request_module(xt_find_target(AF_INET,						     t->u.user.name,						     t->u.user.revision),					 "ipt_%s", t->u.user.name);	if (IS_ERR(target) || !target) {		duprintf("check_entry: `%s' not found\n", t->u.user.name);		ret = target ? PTR_ERR(target) : -ENOENT;		goto out;	}	t->u.kernel.target = target;	if (t->u.kernel.target->compat)		t->u.kernel.target->compat(t, NULL, &off, COMPAT_CALC_SIZE);	else		xt_compat_target(t, NULL, &off, COMPAT_CALC_SIZE);	*size += off;	ret = compat_add_offset(entry_offset, off);	if (ret)		goto out;	/* Check hooks & underflows */	for (h = 0; h < NF_IP_NUMHOOKS; h++) {		if ((unsigned char *)e - base == hook_entries[h])			newinfo->hook_entry[h] = hook_entries[h];		if ((unsigned char *)e - base == underflows[h])			newinfo->underflow[h] = underflows[h];	}	/* Clear counters and comefrom */	e->counters = ((struct ipt_counters) { 0, 0 });	e->comefrom = 0;	(*i)++;	return 0;out:	IPT_MATCH_ITERATE(e, cleanup_match, &j);	return ret;}static inline int compat_copy_match_from_user(struct ipt_entry_match *m,	void **dstptr, compat_uint_t *size, const char *name,	const struct ipt_ip *ip, unsigned int hookmask){	struct ipt_entry_match *dm;	struct ipt_match *match;	int ret;	dm = (struct ipt_entry_match *)*dstptr;	match = m->u.kernel.match;	if (match->compat)		match->compat(m, dstptr, size, COMPAT_FROM_USER);	else		xt_compat_match(m, dstptr, size, COMPAT_FROM_USER);	ret = xt_check_match(match, AF_INET, dm->u.match_size - sizeof(*dm),			     name, hookmask, ip->proto,			     ip->invflags & IPT_INV_PROTO);	if (ret)		return ret;	if (m->u.kernel.match->checkentry	    && !m->u.kernel.match->checkentry(name, ip, match, dm->data,					      dm->u.match_size - sizeof(*dm),					      hookmask)) {		duprintf("ip_tables: check failed for `%s'.\n",			 m->u.kernel.match->name);		return -EINVAL;	}	return 0;}static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,	unsigned int *size, const char *name,	struct xt_table_info *newinfo, unsigned char *base){	struct ipt_entry_target *t;	struct ipt_target *target;	struct ipt_entry *de;	unsigned int origsize;	int ret, h;	ret = 0;	origsize = *size;	de = (struct ipt_entry *)*dstptr;	memcpy(de, e, sizeof(struct ipt_entry));	*dstptr += sizeof(struct compat_ipt_entry);	ret = IPT_MATCH_ITERATE(e, compat_copy_match_from_user, dstptr, size,			name, &de->ip, de->comefrom);	if (ret)		goto out;	de->target_offset = e->target_offset - (origsize - *size);	t = ipt_get_target(e);	target = t->u.kernel.target;	if (target->compat)		target->compat(t, dstptr, size, COMPAT_FROM_USER);	else		xt_compat_target(t, dstptr, size, COMPAT_FROM_USER);	de->next_offset = e->next_offset - (origsize - *size);	for (h = 0; h < NF_IP_NUMHOOKS; h++) {		if ((unsigned char *)de - base < newinfo->hook_entry[h])			newinfo->hook_entry[h] -= origsize - *size;		if ((unsigned char *)de - base < newinfo->underflow[h])			newinfo->underflow[h] -= origsize - *size;	}	t = ipt_get_target(de);	target = t->u.kernel.target;	ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),			      name, e->comefrom, e->ip.proto,			      e->ip.invflags & IPT_INV_PROTO);	if (ret)		goto out;	ret = -EINVAL;	if (t->u.kernel.target == &ipt_standard_target) {		if (!standard_check(t, *size))			goto out;	} else if (t->u.kernel.target->checkentry		   && !t->u.kernel.target->checkentry(name, de, target,				t->data, t->u.target_size - sizeof(*t),				de->comefrom)) {		duprintf("ip_tables: compat: check failed for `%s'.\n",			 t->u.kernel.target->name);		goto out;	}	ret = 0;out:	return ret;}static inttranslate_compat_table(const char *name,		unsigned int valid_hooks,		struct xt_table_info **pinfo,		void **pentry0,		unsigned int total_size,		unsigned int number,		unsigned int *hook_entries,		unsigned int *underflows){	unsigned int i;	struct xt_table_info *newinfo, *info;	void *pos, *entry0, *entry1;	unsigned int size;	int ret;	info = *pinfo;	entry0 = *pentry0;	size = total_size;	info->number = number;	/* Init all hooks to impossible value. */	for (i = 0; i < NF_IP_NUMHOOKS; i++) {		info->hook_entry[i] = 0xFFFFFFFF;		info->underflow[i] = 0xFFFFFFFF;	}	duprintf("translate_compat_table: size %u\n", info->size);	i = 0;	xt_compat_lock(AF_INET);	/* Walk through entries, checking offsets. */	ret = IPT_ENTRY_ITERATE(entry0, total_size,				check_compat_entry_size_and_hooks,				info, &size, entry0,				entry0 + total_size,

⌨️ 快捷键说明

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