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

📄 ip_tables.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
#ifdef CONFIG_COMPAT		if (compat) {			struct xt_table_info tmp;			ret = compat_table_info(private, &tmp);			compat_flush_offsets();			private =  &tmp;		}#endif		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;		strcpy(info.name, 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;#ifdef CONFIG_COMPAT	if (compat)		xt_compat_unlock(AF_INET);#endif	return ret;}static intget_entries(struct ipt_get_entries __user *uptr, int *len){	int ret;	struct ipt_get_entries get;	struct xt_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 xt_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 xt_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){	return xt_compat_match_to_user(m, dstptr, size);}static intcompat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,			  compat_uint_t *size, struct xt_counters *counters,			  unsigned int *i){	struct ipt_entry_target *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;	if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))		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);	ret = xt_compat_target_to_user(t, dstptr, size);	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;	(*i)++;	return 0;out:	return ret;}static inline intcompat_find_calc_match(struct ipt_entry_match *m,	    const char *name,	    const struct ipt_ip *ip,	    unsigned int hookmask,	    int *size, int *i){	struct xt_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;	*size += xt_compat_match_offset(match);	(*i)++;	return 0;}static inline intcompat_release_match(struct ipt_entry_match *m, unsigned int *i){	if (i && (*i)-- == 0)		return 1;	module_put(m->u.kernel.match->me);	return 0;}static inline intcompat_release_entry(struct ipt_entry *e, unsigned int *i){	struct ipt_entry_target *t;	if (i && (*i)-- == 0)		return 1;	/* Cleanup all matches */	IPT_MATCH_ITERATE(e, compat_release_match, NULL);	t = ipt_get_target(e);	module_put(t->u.kernel.target->me);	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 xt_target *target;	unsigned int 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;	}	ret = check_entry(e, name);	if (ret)		return ret;	off = 0;	entry_offset = (void *)e - (void *)base;	j = 0;	ret = IPT_MATCH_ITERATE(e, compat_find_calc_match, name, &e->ip,			e->comefrom, &off, &j);	if (ret != 0)		goto release_matches;	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_compat_entry_size_and_hooks: `%s' not found\n",							t->u.user.name);		ret = target ? PTR_ERR(target) : -ENOENT;		goto release_matches;	}	t->u.kernel.target = target;	off += xt_compat_target_offset(target);	*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:	module_put(t->u.kernel.target->me);release_matches:	IPT_MATCH_ITERATE(e, compat_release_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){	xt_compat_match_from_user(m, dstptr, size);	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 xt_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)		return ret;	de->target_offset = e->target_offset - (origsize - *size);	t = ipt_get_target(e);	target = t->u.kernel.target;	xt_compat_target_from_user(t, dstptr, size);	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;	}	return ret;}static inline int compat_check_entry(struct ipt_entry *e, const char *name,						unsigned int *i){	int j, ret;	j = 0;	ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip, e->comefrom, &j);	if (ret)		goto cleanup_matches;	ret = check_target(e, name);	if (ret)		goto cleanup_matches;	(*i)++;	return 0; cleanup_matches:	IPT_MATCH_ITERATE(e, cleanup_match, &j);	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, j;	struct xt_table_info *newinfo, *info;	void *pos, *entry0, *entry1;	unsigned int size;	int ret;	info = *pinfo;	entry0 = *pentry0;	size = total_size;

⌨️ 快捷键说明

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