📄 ip_tables.c
字号:
(*i)++; return 0;}static voidget_counters(const struct ipt_table_info *t, struct ipt_counters counters[]){ unsigned int cpu; unsigned int i; for_each_cpu(cpu) { i = 0; IPT_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 ipt_table *table, void __user *userptr){ unsigned int off, num, countersize; struct ipt_entry *e; struct ipt_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 ipt_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 ipt_entry_match *m; struct ipt_entry_target *t; e = (struct ipt_entry *)(table->private->entries + off); if (copy_to_user(userptr + off + offsetof(struct ipt_entry, counters), &counters[num], sizeof(counters[num])) != 0) { ret = -EFAULT; goto free_counters; } for (i = sizeof(struct ipt_entry); i < e->target_offset; i += m->u.match_size) { m = (void *)e + i; if (copy_to_user(userptr + off + i + offsetof(struct ipt_entry_match, u.user.name), m->u.kernel.match->name, strlen(m->u.kernel.match->name)+1) != 0) { ret = -EFAULT; goto free_counters; } } t = ipt_get_target(e); if (copy_to_user(userptr + off + e->target_offset + offsetof(struct ipt_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 ipt_get_entries *entries, struct ipt_get_entries __user *uptr){ int ret; struct ipt_table *t; t = find_table_lock(entries->name); if (t && !IS_ERR(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; } module_put(t->me); up(&ipt_mutex); } else ret = t ? PTR_ERR(t) : -ENOENT; return ret;}static intdo_replace(void __user *user, unsigned int len){ int ret; struct ipt_replace tmp; struct ipt_table *t; struct ipt_table_info *newinfo, *oldinfo; struct ipt_counters *counters; 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; /* 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 ipt_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 ipt_counters)); if (!counters) { ret = -ENOMEM; goto free_newinfo; } memset(counters, 0, tmp.num_counters * sizeof(struct ipt_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 = try_then_request_module(find_table_lock(tmp.name), "iptable_%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 */ IPT_ENTRY_ITERATE(oldinfo->entries, oldinfo->size, cleanup_entry,NULL); vfree(oldinfo); if (copy_to_user(tmp.counters, counters, sizeof(struct ipt_counters) * tmp.num_counters) != 0) ret = -EFAULT; vfree(counters); up(&ipt_mutex); return ret; put_module: module_put(t->me); up(&ipt_mutex); free_newinfo_counters_untrans: IPT_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 ipt_entry *e, const struct ipt_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){ unsigned int i; struct ipt_counters_info tmp, *paddc; struct ipt_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 ipt_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; IPT_ENTRY_ITERATE(t->private->entries, t->private->size, add_counter_to_entry, paddc->counters, &i); unlock_up_free: write_unlock_bh(&t->lock); up(&ipt_mutex); module_put(t->me); free: vfree(paddc); return ret;}static 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); 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: { char name[IPT_TABLE_MAXNAMELEN]; struct ipt_table *t; if (*len != sizeof(struct ipt_getinfo)) { duprintf("length %u != %u\n", *len, sizeof(struct ipt_getinfo)); ret = -EINVAL; break; } if (copy_from_user(name, user, sizeof(name)) != 0) { ret = -EFAULT; break; } name[IPT_TABLE_MAXNAMELEN-1] = '\0'; t = try_then_request_module(find_table_lock(name), "iptable_%s", name); if (t && !IS_ERR(t)) { struct ipt_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; memcpy(info.name, name, sizeof(info.name)); if (copy_to_user(user, &info, *len) != 0) ret = -EFAULT; else ret = 0; up(&ipt_mutex); module_put(t->me); } else ret = t ? PTR_ERR(t) : -ENOENT; } break; case IPT_SO_GET_ENTRIES: { struct ipt_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 ipt_get_entries) + get.size) { duprintf("get_entries: %u != %u\n", *len, sizeof(struct ipt_get_entries) + get.size); ret = -EINVAL; } else ret = get_entries(&get, user); break; } case IPT_SO_GET_REVISION_MATCH: case IPT_SO_GET_REVISION_TARGET: { struct ipt_get_revision rev; int (*revfn)(const char *, u8, int *); 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) revfn = target_revfn; else revfn = match_revfn; try_then_request_module(find_revision(rev.name, rev.revision, revfn, &ret), "ipt_%s", rev.name); break; } default: duprintf("do_ipt_get_ctl: unknown request %i\n", cmd); ret = -EINVAL; } return ret;}/* Registration hooks for targets. */intipt_register_target(struct ipt_target *target){ int ret; ret = down_interruptible(&ipt_mutex); if (ret != 0) return ret; list_add(&target->list, &ipt_target); up(&ipt_mutex); return ret;}voidipt_unregister_target(struct ipt_target *target){ down(&ipt_mutex); LIST_DELETE(&ipt_target, target); up(&ipt_mutex);}intipt_register_match(struct ipt_match *match){ int ret; ret = down_interruptible(&ipt_mutex); if (ret != 0) return ret; list_add(&match->list, &ipt_match); up(&ipt_mutex); return ret;}voidipt_unregister_match(struct ipt_match *match){ down(&ipt_mutex); LIST_DELETE(&ipt_match, match); up(&ipt_mutex);}int ipt_register_table(struct ipt_table *table, const struct ipt_replace *repl){ int ret; struct ipt_table_info *newinfo; static struct ipt_table_info bootstrap = { 0, 0, 0, { 0 }, { 0 }, { } }; newinfo = vmalloc(sizeof(struct ipt_table_info) + SMP_ALIGN(repl->size) * (highest_possible_processor_id()+1)); if (!newinfo) return -ENOMEM; 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -