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

📄 ip6_tables.c

📁 该文件是rt_linux
💻 C
📖 第 1 页 / 共 4 页
字号:
	if (ret != 0) {		IP6T_ENTRY_ITERATE(newinfo->entries, newinfo->size,				  cleanup_entry, &i);		return ret;	}	/* And one copy for every other CPU */	for (i = 1; i < smp_num_cpus; i++) {		memcpy(newinfo->entries + SMP_ALIGN(newinfo->size)*i,		       newinfo->entries,		       SMP_ALIGN(newinfo->size));	}	return ret;}static struct ip6t_table_info *replace_table(struct ip6t_table *table,	      unsigned int num_counters,	      struct ip6t_table_info *newinfo,	      int *error){	struct ip6t_table_info *oldinfo;#ifdef CONFIG_NETFILTER_DEBUG	{		struct ip6t_entry *table_base;		unsigned int i;		for (i = 0; i < smp_num_cpus; i++) {			table_base =				(void *)newinfo->entries				+ TABLE_OFFSET(newinfo, i);			table_base->comefrom = 0xdead57ac;		}	}#endif	/* Do the substitution. */	write_lock_bh(&table->lock);	/* Check inside lock: is the old number correct? */	if (num_counters != table->private->number) {		duprintf("num_counters != table->private->number (%u/%u)\n",			 num_counters, table->private->number);		write_unlock_bh(&table->lock);		*error = -EAGAIN;		return NULL;	}	oldinfo = table->private;	table->private = newinfo;	newinfo->initial_entries = oldinfo->initial_entries;	write_unlock_bh(&table->lock);	return oldinfo;}/* Gets counters. */static inline intadd_entry_to_counter(const struct ip6t_entry *e,		     struct ip6t_counters total[],		     unsigned int *i){	ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);	(*i)++;	return 0;}static voidget_counters(const struct ip6t_table_info *t,	     struct ip6t_counters counters[]){	unsigned int cpu;	unsigned int i;	for (cpu = 0; cpu < smp_num_cpus; cpu++) {		i = 0;		IP6T_ENTRY_ITERATE(t->entries + TABLE_OFFSET(t, cpu),				  t->size,				  add_entry_to_counter,				  counters,				  &i);	}}static intcopy_entries_to_user(unsigned int total_size,		     struct ip6t_table *table,		     void *userptr){	unsigned int off, num, countersize;	struct ip6t_entry *e;	struct ip6t_counters *counters;	int ret = 0;	/* We need atomic snapshot of counters: rest doesn't change	   (other than comefrom, which userspace doesn't care	   about). */	countersize = sizeof(struct ip6t_counters) * table->private->number;	counters = vmalloc(countersize);	if (counters == NULL)		return -ENOMEM;	/* First, sum counters... */	memset(counters, 0, countersize);	write_lock_bh(&table->lock);	get_counters(table->private, counters);	write_unlock_bh(&table->lock);	/* ... then copy entire thing from CPU 0... */	if (copy_to_user(userptr, table->private->entries, total_size) != 0) {		ret = -EFAULT;		goto free_counters;	}	/* FIXME: use iterator macros --RR */	/* ... then go back and fix counters and names */	for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){		unsigned int i;		struct ip6t_entry_match *m;		struct ip6t_entry_target *t;		e = (struct ip6t_entry *)(table->private->entries + off);		if (copy_to_user(userptr + off				 + offsetof(struct ip6t_entry, counters),				 &counters[num],				 sizeof(counters[num])) != 0) {			ret = -EFAULT;			goto free_counters;		}		for (i = sizeof(struct ip6t_entry);		     i < e->target_offset;		     i += m->u.match_size) {			m = (void *)e + i;			if (copy_to_user(userptr + off + i					 + offsetof(struct ip6t_entry_match,						    u.user.name),					 m->u.kernel.match->name,					 strlen(m->u.kernel.match->name)+1)			    != 0) {				ret = -EFAULT;				goto free_counters;			}		}		t = ip6t_get_target(e);		if (copy_to_user(userptr + off + e->target_offset				 + offsetof(struct ip6t_entry_target,					    u.user.name),				 t->u.kernel.target->name,				 strlen(t->u.kernel.target->name)+1) != 0) {			ret = -EFAULT;			goto free_counters;		}	} free_counters:	vfree(counters);	return ret;}static intget_entries(const struct ip6t_get_entries *entries,	    struct ip6t_get_entries *uptr){	int ret;	struct ip6t_table *t;	t = find_table_lock(entries->name, &ret, &ip6t_mutex);	if (t) {		duprintf("t->private->number = %u\n",			 t->private->number);		if (entries->size == t->private->size)			ret = copy_entries_to_user(t->private->size,						   t, uptr->entrytable);		else {			duprintf("get_entries: I've got %u not %u!\n",				 t->private->size,				 entries->size);			ret = -EINVAL;		}		up(&ip6t_mutex);	} else		duprintf("get_entries: Can't find %s!\n",			 entries->name);	return ret;}static intdo_replace(void *user, unsigned int len){	int ret;	struct ip6t_replace tmp;	struct ip6t_table *t;	struct ip6t_table_info *newinfo, *oldinfo;	struct ip6t_counters *counters;	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)		return -EFAULT;	/* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */	if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)		return -ENOMEM;	newinfo = vmalloc(sizeof(struct ip6t_table_info)			  + SMP_ALIGN(tmp.size) * smp_num_cpus);	if (!newinfo)		return -ENOMEM;	if (copy_from_user(newinfo->entries, user + sizeof(tmp),			   tmp.size) != 0) {		ret = -EFAULT;		goto free_newinfo;	}	counters = vmalloc(tmp.num_counters * sizeof(struct ip6t_counters));	if (!counters) {		ret = -ENOMEM;		goto free_newinfo;	}	memset(counters, 0, tmp.num_counters * sizeof(struct ip6t_counters));	ret = translate_table(tmp.name, tmp.valid_hooks,			      newinfo, tmp.size, tmp.num_entries,			      tmp.hook_entry, tmp.underflow);	if (ret != 0)		goto free_newinfo_counters;	duprintf("ip_tables: Translated table\n");	t = find_table_lock(tmp.name, &ret, &ip6t_mutex);	if (!t)		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 free_newinfo_counters_untrans_unlock;	}	oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);	if (!oldinfo)		goto free_newinfo_counters_untrans_unlock;	/* 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 (t->me && (oldinfo->number <= oldinfo->initial_entries) && 	    (newinfo->number > oldinfo->initial_entries))		__MOD_INC_USE_COUNT(t->me);	else if (t->me && (oldinfo->number > oldinfo->initial_entries) &&	 	 (newinfo->number <= oldinfo->initial_entries))		__MOD_DEC_USE_COUNT(t->me);	/* Get the old counters. */	get_counters(oldinfo, counters);	/* Decrease module usage counts and free resource */	IP6T_ENTRY_ITERATE(oldinfo->entries, oldinfo->size, cleanup_entry,NULL);	vfree(oldinfo);	/* Silent error: too late now. */	copy_to_user(tmp.counters, counters,		     sizeof(struct ip6t_counters) * tmp.num_counters);	vfree(counters);	up(&ip6t_mutex);	return 0; free_newinfo_counters_untrans_unlock:	up(&ip6t_mutex); free_newinfo_counters_untrans:	IP6T_ENTRY_ITERATE(newinfo->entries, newinfo->size, cleanup_entry,NULL); free_newinfo_counters:	vfree(counters); free_newinfo:	vfree(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 ip6t_entry *e,		     const struct ip6t_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, unsigned int len){	unsigned int i;	struct ip6t_counters_info tmp, *paddc;	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)) {

⌨️ 快捷键说明

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