📄 ip_tables.c
字号:
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); j = 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, hook_entries, underflows, &j, name); if (ret != 0) goto out_unlock; ret = -EINVAL; if (j != number) { duprintf("translate_compat_table: %u not %u entries\n", j, number); goto out_unlock; } /* Check hooks all assigned */ for (i = 0; i < NF_IP_NUMHOOKS; i++) { /* Only hooks which are valid */ if (!(valid_hooks & (1 << i))) continue; if (info->hook_entry[i] == 0xFFFFFFFF) { duprintf("Invalid hook entry %u %u\n", i, hook_entries[i]); goto out_unlock; } if (info->underflow[i] == 0xFFFFFFFF) { duprintf("Invalid underflow %u %u\n", i, underflows[i]); goto out_unlock; } } ret = -ENOMEM; newinfo = xt_alloc_table_info(size); if (!newinfo) goto out_unlock; newinfo->number = number; for (i = 0; i < NF_IP_NUMHOOKS; i++) { newinfo->hook_entry[i] = info->hook_entry[i]; newinfo->underflow[i] = info->underflow[i]; } entry1 = newinfo->entries[raw_smp_processor_id()]; pos = entry1; size = total_size; ret = IPT_ENTRY_ITERATE(entry0, total_size, compat_copy_entry_from_user, &pos, &size, name, newinfo, entry1); compat_flush_offsets(); xt_compat_unlock(AF_INET); if (ret) goto free_newinfo; ret = -ELOOP; if (!mark_source_chains(newinfo, valid_hooks, entry1)) goto free_newinfo; i = 0; ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry, name, &i); if (ret) { j -= i; IPT_ENTRY_ITERATE_CONTINUE(entry1, newinfo->size, i, compat_release_entry, &j); IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i); xt_free_table_info(newinfo); return ret; } /* And one copy for every other CPU */ for_each_possible_cpu(i) if (newinfo->entries[i] && newinfo->entries[i] != entry1) memcpy(newinfo->entries[i], entry1, newinfo->size); *pinfo = newinfo; *pentry0 = entry1; xt_free_table_info(info); return 0;free_newinfo: xt_free_table_info(newinfo);out: IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j); return ret;out_unlock: compat_flush_offsets(); xt_compat_unlock(AF_INET); goto out;}static intcompat_do_replace(void __user *user, unsigned int len){ int ret; struct compat_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_compat_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("compat_do_replace: Translated table\n"); ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo, tmp.num_counters, compat_ptr(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;}static intcompat_do_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 = compat_do_replace(user, len); break; case IPT_SO_SET_ADD_COUNTERS: ret = do_add_counters(user, len, 1); break; default: duprintf("do_ipt_set_ctl: unknown request %i\n", cmd); ret = -EINVAL; } return ret;}struct compat_ipt_get_entries{ char name[IPT_TABLE_MAXNAMELEN]; compat_uint_t size; struct compat_ipt_entry entrytable[0];};static int compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, void __user *userptr){ struct xt_counters *counters; struct xt_table_info *private = table->private; void __user *pos; unsigned int size; int ret = 0; void *loc_cpu_entry; unsigned int i = 0; counters = alloc_counters(table); if (IS_ERR(counters)) return PTR_ERR(counters); /* choose the copy that is on our node/cpu, ... * This choice is lazy (because current thread is * allowed to migrate to another cpu) */ loc_cpu_entry = private->entries[raw_smp_processor_id()]; pos = userptr; size = total_size; ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size, compat_copy_entry_to_user, &pos, &size, counters, &i); vfree(counters); return ret;}static intcompat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len){ int ret; struct compat_ipt_get_entries get; struct xt_table *t; if (*len < sizeof(get)) { duprintf("compat_get_entries: %u < %u\n", *len, (unsigned int)sizeof(get)); return -EINVAL; } if (copy_from_user(&get, uptr, sizeof(get)) != 0) return -EFAULT; if (*len != sizeof(struct compat_ipt_get_entries) + get.size) { duprintf("compat_get_entries: %u != %u\n", *len, (unsigned int)(sizeof(struct compat_ipt_get_entries) + get.size)); return -EINVAL; } xt_compat_lock(AF_INET); t = xt_find_table_lock(AF_INET, get.name); if (t && !IS_ERR(t)) { struct xt_table_info *private = t->private; struct xt_table_info info; duprintf("t->private->number = %u\n", private->number); ret = compat_table_info(private, &info); if (!ret && get.size == info.size) { ret = compat_copy_entries_to_user(private->size, t, uptr->entrytable); } else if (!ret) { duprintf("compat_get_entries: I've got %u not %u!\n", private->size, get.size); ret = -EINVAL; } compat_flush_offsets(); module_put(t->me); xt_table_unlock(t); } else ret = t ? PTR_ERR(t) : -ENOENT; xt_compat_unlock(AF_INET); return ret;}static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);static intcompat_do_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: ret = get_info(user, len, 1); break; case IPT_SO_GET_ENTRIES: ret = compat_get_entries(user, len); break; default: ret = do_ipt_get_ctl(sk, cmd, user, len); } return ret;}#endifstatic 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, 0); 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: ret = get_info(user, len, 0); break; case IPT_SO_GET_ENTRIES: ret = get_entries(user, len); 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; } ret = xt_register_table(table, &bootstrap, newinfo); if (ret != 0) { xt_free_table_info(newinfo); return ret; } return 0;}void ipt_unregister_table(struct xt_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 boolicmp_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, bool invert){ return ((test_type == 0xFF) || (type == test_type && code >= min_code && code <= max_code)) ^ invert;}static boolicmp_match(const struct sk_buff *skb, const struct net_device *in, const struct net_device *out, const struct xt_match *match, const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop){ struct icmphdr _icmph, *ic; const struct ipt_icmp *icmpinfo = matchinfo; /* Must not be a fragment. */ if (offset) return false; 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 = true; return false; } 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 boolicmp_checkentry(const char *tablename, const void *info, const struct xt_match *match, void *matchinfo, unsigned int hook_mask){ const struct ipt_icmp *icmpinfo = matchinfo; /* Must specify no unknown invflags */ return !(icmpinfo->invflags & ~IPT_ICMP_INV);}/* The built-in targets: standard (NULL) and error. */static struct xt_target ipt_standard_target __read_mostly = { .name = IPT_STANDARD_TARGET, .targetsize = sizeof(int), .family = AF_INET,#ifdef CONFIG_COMPAT .compatsize = sizeof(compat_int_t), .compat_from_user = compat_standard_from_user, .compat_to_user = compat_standard_to_user,#endif};static struct xt_target ipt_error_target __read_mostly = { .name = IPT_ERROR_TARGET, .target = ipt_error, .targetsize = IPT_FUNCTION_MAXNAMELEN, .family = AF_INET,};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,#ifdef CONFIG_COMPAT .compat_set = compat_do_ipt_set_ctl,#endif .get_optmin = IPT_BASE_CTL, .get_optmax = IPT_SO_GET_MAX+1, .get = do_ipt_get_ctl,#ifdef CONFIG_COMPAT .compat_get = compat_do_ipt_get_ctl,#endif .owner = THIS_MODULE,};static struct xt_match icmp_matchstruct __read_mostly = { .name = "icmp", .match = icmp_match, .matchsize = sizeof(struct ipt_icmp), .proto = IPPROTO_ICMP, .family = AF_INET, .checkentry = icmp_checkentry,};static int __init ip_tables_init(void){ int ret; ret = xt_proto_init(AF_INET); if (ret < 0) goto err1; /* Noone else will be downing sem now, so we won't sleep */ ret = xt_register_target(&ipt_standard_target); if (ret < 0) goto err2; ret = xt_register_target(&ipt_error_target); if (ret < 0) goto err3; ret = xt_register_match(&icmp_matchstruct); if (ret < 0) goto err4; /* Register setsockopt */ ret = nf_register_sockopt(&ipt_sockopts); if (ret < 0) goto err5; printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n"); return 0;err5: xt_unregister_match(&icmp_matchstruct);err4: xt_unregister_target(&ipt_error_target);err3: xt_unregister_target(&ipt_standard_target);err2: xt_proto_fini(AF_INET);err1: return ret;}static void __exit ip_tables_fini(void){ nf_unregister_sockopt(&ipt_sockopts); xt_unregister_match(&icmp_matchstruct); xt_unregister_target(&ipt_error_target); xt_unregister_target(&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(ip_tables_init);module_exit(ip_tables_fini);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -