📄 ip_tables.c
字号:
{ if (i && (*i)-- == 0) return 1; if (m->u.kernel.match->destroy) m->u.kernel.match->destroy(m->data, m->u.match_size - sizeof(*m)); if (m->u.kernel.match->me) __MOD_DEC_USE_COUNT(m->u.kernel.match->me); return 0;}static inline intstandard_check(const struct ipt_entry_target *t, unsigned int max_offset){ struct ipt_standard_target *targ = (void *)t; /* Check standard info. */ if (t->u.target_size != IPT_ALIGN(sizeof(struct ipt_standard_target))) { duprintf("standard_check: target size %u != %u\n", t->u.target_size, IPT_ALIGN(sizeof(struct ipt_standard_target))); return 0; } if (targ->verdict >= 0 && targ->verdict > max_offset - sizeof(struct ipt_entry)) { duprintf("ipt_standard_check: bad verdict (%i)\n", targ->verdict); return 0; } if (targ->verdict < -NF_MAX_VERDICT - 1) { duprintf("ipt_standard_check: bad negative verdict (%i)\n", targ->verdict); return 0; } return 1;}static inline intcheck_match(struct ipt_entry_match *m, const char *name, const struct ipt_ip *ip, unsigned int hookmask, unsigned int *i){ int ret; struct ipt_match *match; match = find_match_lock(m->u.user.name, &ret, &ipt_mutex); if (!match) { duprintf("check_match: `%s' not found\n", m->u.user.name); return ret; } if (match->me) __MOD_INC_USE_COUNT(match->me); m->u.kernel.match = match; up(&ipt_mutex); if (m->u.kernel.match->checkentry && !m->u.kernel.match->checkentry(name, ip, m->data, m->u.match_size - sizeof(*m), hookmask)) { if (m->u.kernel.match->me) __MOD_DEC_USE_COUNT(m->u.kernel.match->me); duprintf("ip_tables: check failed for `%s'.\n", m->u.kernel.match->name); return -EINVAL; } (*i)++; return 0;}static struct ipt_target ipt_standard_target;static inline intcheck_entry(struct ipt_entry *e, const char *name, unsigned int size, unsigned int *i){ struct ipt_entry_target *t; struct ipt_target *target; int ret; unsigned int j; if (!ip_checkentry(&e->ip)) { duprintf("ip_tables: ip check failed %p %s.\n", e, name); return -EINVAL; } j = 0; ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip, e->comefrom, &j); if (ret != 0) goto cleanup_matches; t = ipt_get_target(e); target = find_target_lock(t->u.user.name, &ret, &ipt_mutex); if (!target) { duprintf("check_entry: `%s' not found\n", t->u.user.name); goto cleanup_matches; } if (target->me) __MOD_INC_USE_COUNT(target->me); t->u.kernel.target = target; up(&ipt_mutex); if (t->u.kernel.target == &ipt_standard_target) { if (!standard_check(t, size)) { ret = -EINVAL; goto cleanup_matches; } } else if (t->u.kernel.target->checkentry && !t->u.kernel.target->checkentry(name, e, t->data, t->u.target_size - sizeof(*t), e->comefrom)) { if (t->u.kernel.target->me) __MOD_DEC_USE_COUNT(t->u.kernel.target->me); duprintf("ip_tables: check failed for `%s'.\n", t->u.kernel.target->name); ret = -EINVAL; goto cleanup_matches; } (*i)++; return 0; cleanup_matches: IPT_MATCH_ITERATE(e, cleanup_match, &j); return ret;}static inline intcheck_entry_size_and_hooks(struct ipt_entry *e, struct ipt_table_info *newinfo, unsigned char *base, unsigned char *limit, const unsigned int *hook_entries, const unsigned int *underflows, unsigned int *i){ unsigned int h; if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) { duprintf("Bad offset %p\n", e); return -EINVAL; } if (e->next_offset < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) { duprintf("checking: element %p size %u\n", e, e->next_offset); return -EINVAL; } /* 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]; } /* FIXME: underflows must be unconditional, standard verdicts < 0 (not IPT_RETURN). --RR */ /* Clear counters and comefrom */ e->counters = ((struct ipt_counters) { 0, 0 }); e->comefrom = 0; (*i)++; return 0;}static inline intcleanup_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, cleanup_match, NULL); t = ipt_get_target(e); if (t->u.kernel.target->destroy) t->u.kernel.target->destroy(t->data, t->u.target_size - sizeof(*t)); if (t->u.kernel.target->me) __MOD_DEC_USE_COUNT(t->u.kernel.target->me); return 0;}/* Checks and translates the user-supplied table segment (held in newinfo) */static inttranslate_table(const char *name, unsigned int valid_hooks, struct ipt_table_info *newinfo, unsigned int size, unsigned int number, const unsigned int *hook_entries, const unsigned int *underflows){ unsigned int i; int ret; newinfo->size = size; newinfo->number = number; /* Init all hooks to impossible value. */ for (i = 0; i < NF_IP_NUMHOOKS; i++) { newinfo->hook_entry[i] = 0xFFFFFFFF; newinfo->underflow[i] = 0xFFFFFFFF; } duprintf("translate_table: size %u\n", newinfo->size); i = 0; /* Walk through entries, checking offsets. */ ret = IPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, check_entry_size_and_hooks, newinfo, newinfo->entries, newinfo->entries + size, hook_entries, underflows, &i); if (ret != 0) return ret; if (i != number) { duprintf("translate_table: %u not %u entries\n", i, number); return -EINVAL; } /* Check hooks all assigned */ for (i = 0; i < NF_IP_NUMHOOKS; i++) { /* Only hooks which are valid */ if (!(valid_hooks & (1 << i))) continue; if (newinfo->hook_entry[i] == 0xFFFFFFFF) { duprintf("Invalid hook entry %u %u\n", i, hook_entries[i]); return -EINVAL; } if (newinfo->underflow[i] == 0xFFFFFFFF) { duprintf("Invalid underflow %u %u\n", i, underflows[i]); return -EINVAL; } } if (!mark_source_chains(newinfo, valid_hooks)) return -ELOOP; /* Finally, each sanity check must pass */ i = 0; ret = IPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, check_entry, name, size, &i); if (ret != 0) { IPT_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 ipt_table_info *replace_table(struct ipt_table *table, unsigned int num_counters, struct ipt_table_info *newinfo, int *error){ struct ipt_table_info *oldinfo;#ifdef CONFIG_NETFILTER_DEBUG { struct ipt_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; write_unlock_bh(&table->lock); return oldinfo;}/* Gets counters. */static inline intadd_entry_to_counter(const struct ipt_entry *e, struct ipt_counters total[], unsigned int *i){ ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt); (*i)++; return 0;}static voidget_counters(const struct ipt_table_info *t, struct ipt_counters counters[]){ unsigned int cpu; unsigned int i; for (cpu = 0; cpu < smp_num_cpus; 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 *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 *uptr){ int ret; struct ipt_table *t; t = find_table_lock(entries->name, &ret, &ipt_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(&ipt_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 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; newinfo = vmalloc(sizeof(struct ipt_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 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 = find_table_lock(tmp.name, &ret, &ipt_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; /* 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); /* Silent error: too late now. */ copy_to_user(tmp.counters, counters, sizeof(struct ipt_counters) * tmp.num_counters); vfree(counters); up(&ipt_mutex); return 0; free_newinfo_counters_untrans_unlock: 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, unsigned int len){ unsigned int i; struct ipt_counters_info tmp, *paddc; struct ipt_table *t; int ret; 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, &ret, &ipt_mutex); if (!t) goto free;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -