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

📄 arp_tables.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
	newinfo = vmalloc(sizeof(struct arpt_table_info)			  + SMP_ALIGN(tmp.size) *			  		(highest_possible_processor_id()+1));	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 arpt_counters));	if (!counters) {		ret = -ENOMEM;		goto free_newinfo;	}	memset(counters, 0, tmp.num_counters * sizeof(struct arpt_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("arp_tables: Translated table\n");	t = try_then_request_module(find_table_lock(tmp.name),				    "arptable_%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 = 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 */	ARPT_ENTRY_ITERATE(oldinfo->entries, oldinfo->size, cleanup_entry,NULL);	vfree(oldinfo);	if (copy_to_user(tmp.counters, counters,			 sizeof(struct arpt_counters) * tmp.num_counters) != 0)		ret = -EFAULT;	vfree(counters);	up(&arpt_mutex);	return ret; put_module:	module_put(t->me);	up(&arpt_mutex); free_newinfo_counters_untrans:	ARPT_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 int add_counter_to_entry(struct arpt_entry *e,				       const struct arpt_counters addme[],				       unsigned int *i){	ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);	(*i)++;	return 0;}static int do_add_counters(void __user *user, unsigned int len){	unsigned int i;	struct arpt_counters_info tmp, *paddc;	struct arpt_table *t;	int ret = 0;	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)		return -EFAULT;	if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct arpt_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);	if (!t || IS_ERR(t)) {		ret = t ? PTR_ERR(t) : -ENOENT;		goto free;	}	write_lock_bh(&t->lock);	if (t->private->number != paddc->num_counters) {		ret = -EINVAL;		goto unlock_up_free;	}	i = 0;	ARPT_ENTRY_ITERATE(t->private->entries,			   t->private->size,			   add_counter_to_entry,			   paddc->counters,			   &i); unlock_up_free:	write_unlock_bh(&t->lock);	up(&arpt_mutex);	module_put(t->me); free:	vfree(paddc);	return ret;}static int do_arpt_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 ARPT_SO_SET_REPLACE:		ret = do_replace(user, len);		break;	case ARPT_SO_SET_ADD_COUNTERS:		ret = do_add_counters(user, len);		break;	default:		duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len){	int ret;	if (!capable(CAP_NET_ADMIN))		return -EPERM;	switch (cmd) {	case ARPT_SO_GET_INFO: {		char name[ARPT_TABLE_MAXNAMELEN];		struct arpt_table *t;		if (*len != sizeof(struct arpt_getinfo)) {			duprintf("length %u != %Zu\n", *len,				 sizeof(struct arpt_getinfo));			ret = -EINVAL;			break;		}		if (copy_from_user(name, user, sizeof(name)) != 0) {			ret = -EFAULT;			break;		}		name[ARPT_TABLE_MAXNAMELEN-1] = '\0';		t = try_then_request_module(find_table_lock(name),					    "arptable_%s", name);		if (t && !IS_ERR(t)) {			struct arpt_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(&arpt_mutex);			module_put(t->me);		} else			ret = t ? PTR_ERR(t) : -ENOENT;	}	break;	case ARPT_SO_GET_ENTRIES: {		struct arpt_get_entries get;		if (*len < sizeof(get)) {			duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));			ret = -EINVAL;		} else if (copy_from_user(&get, user, sizeof(get)) != 0) {			ret = -EFAULT;		} else if (*len != sizeof(struct arpt_get_entries) + get.size) {			duprintf("get_entries: %u != %Zu\n", *len,				 sizeof(struct arpt_get_entries) + get.size);			ret = -EINVAL;		} else			ret = get_entries(&get, user);		break;	}	case ARPT_SO_GET_REVISION_TARGET: {		struct arpt_get_revision rev;		if (*len != sizeof(rev)) {			ret = -EINVAL;			break;		}		if (copy_from_user(&rev, user, sizeof(rev)) != 0) {			ret = -EFAULT;			break;		}		try_then_request_module(find_revision(rev.name, rev.revision,						      target_revfn, &ret),					"arpt_%s", rev.name);		break;	}	default:		duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);		ret = -EINVAL;	}	return ret;}/* Registration hooks for targets. */int arpt_register_target(struct arpt_target *target){	int ret;	ret = down_interruptible(&arpt_mutex);	if (ret != 0)		return ret;	list_add(&target->list, &arpt_target);	up(&arpt_mutex);	return ret;}void arpt_unregister_target(struct arpt_target *target){	down(&arpt_mutex);	LIST_DELETE(&arpt_target, target);	up(&arpt_mutex);}int arpt_register_table(struct arpt_table *table,			const struct arpt_replace *repl){	int ret;	struct arpt_table_info *newinfo;	static struct arpt_table_info bootstrap		= { 0, 0, 0, { 0 }, { 0 }, { } };	newinfo = vmalloc(sizeof(struct arpt_table_info)			  + SMP_ALIGN(repl->size) *			  		(highest_possible_processor_id()+1));	if (!newinfo) {		ret = -ENOMEM;		return ret;	}	memcpy(newinfo->entries, repl->entries, repl->size);	ret = translate_table(table->name, table->valid_hooks,			      newinfo, repl->size,			      repl->num_entries,			      repl->hook_entry,			      repl->underflow);	duprintf("arpt_register_table: translate table gives %d\n", ret);	if (ret != 0) {		vfree(newinfo);		return ret;	}	ret = down_interruptible(&arpt_mutex);	if (ret != 0) {		vfree(newinfo);		return ret;	}	/* Don't autoload: we'd eat our tail... */	if (list_named_find(&arpt_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);		/* save number of initial entries */	table->private->initial_entries = table->private->number;	rwlock_init(&table->lock);	list_prepend(&arpt_tables, table); unlock:	up(&arpt_mutex);	return ret; free_unlock:	vfree(newinfo);	goto unlock;}void arpt_unregister_table(struct arpt_table *table){	down(&arpt_mutex);	LIST_DELETE(&arpt_tables, table);	up(&arpt_mutex);	/* Decrease module usage counts and free resources */	ARPT_ENTRY_ITERATE(table->private->entries, table->private->size,			   cleanup_entry, NULL);	vfree(table->private);}/* The built-in targets: standard (NULL) and error. */static struct arpt_target arpt_standard_target = {	.name		= ARPT_STANDARD_TARGET,};static struct arpt_target arpt_error_target = {	.name		= ARPT_ERROR_TARGET,	.target		= arpt_error,};static struct nf_sockopt_ops arpt_sockopts = {	.pf		= PF_INET,	.set_optmin	= ARPT_BASE_CTL,	.set_optmax	= ARPT_SO_SET_MAX+1,	.set		= do_arpt_set_ctl,	.get_optmin	= ARPT_BASE_CTL,	.get_optmax	= ARPT_SO_GET_MAX+1,	.get		= do_arpt_get_ctl,};#ifdef CONFIG_PROC_FSstatic inline int print_name(const struct arpt_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 arpt_get_tables(char *buffer, char **start, off_t offset, int length){	off_t pos = 0;	unsigned int count = 0;	if (down_interruptible(&arpt_mutex) != 0)		return 0;	LIST_FIND(&arpt_tables, print_name, struct arpt_table *,		  offset, buffer, length, &pos, &count);	up(&arpt_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(&arpt_mutex);	list_append(&arpt_target, &arpt_standard_target);	list_append(&arpt_target, &arpt_error_target);	up(&arpt_mutex);	/* Register setsockopt */	ret = nf_register_sockopt(&arpt_sockopts);	if (ret < 0) {		duprintf("Unable to register sockopts.\n");		return ret;	}#ifdef CONFIG_PROC_FS	{		struct proc_dir_entry *proc;		proc = proc_net_create("arp_tables_names", 0, arpt_get_tables);		if (!proc) {			nf_unregister_sockopt(&arpt_sockopts);			return -ENOMEM;		}		proc->owner = THIS_MODULE;	}#endif	printk("arp_tables: (C) 2002 David S. Miller\n");	return 0;}static void __exit fini(void){	nf_unregister_sockopt(&arpt_sockopts);#ifdef CONFIG_PROC_FS	proc_net_remove("arp_tables_names");#endif}EXPORT_SYMBOL(arpt_register_table);EXPORT_SYMBOL(arpt_unregister_table);EXPORT_SYMBOL(arpt_do_table);EXPORT_SYMBOL(arpt_register_target);EXPORT_SYMBOL(arpt_unregister_target);module_init(init);module_exit(fini);

⌨️ 快捷键说明

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