]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blobdiff - net/ipv4/netfilter/ip_tables.c
Merge tag 'nfc-next-4.7-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo...
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / netfilter / ip_tables.c
index 631c100a13384203c06ec531ef901cd3aac74f90..54906e0e8e0c093a8680e6b1b0b45fc5dff9387d 100644 (file)
@@ -35,34 +35,12 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 MODULE_DESCRIPTION("IPv4 packet filter");
 
-/*#define DEBUG_IP_FIREWALL*/
-/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
-/*#define DEBUG_IP_FIREWALL_USER*/
-
-#ifdef DEBUG_IP_FIREWALL
-#define dprintf(format, args...) pr_info(format , ## args)
-#else
-#define dprintf(format, args...)
-#endif
-
-#ifdef DEBUG_IP_FIREWALL_USER
-#define duprintf(format, args...) pr_info(format , ## args)
-#else
-#define duprintf(format, args...)
-#endif
-
 #ifdef CONFIG_NETFILTER_DEBUG
 #define IP_NF_ASSERT(x)                WARN_ON(!(x))
 #else
 #define IP_NF_ASSERT(x)
 #endif
 
-#if 0
-/* All the better to debug you with... */
-#define static
-#define inline
-#endif
-
 void *ipt_alloc_initial_table(const struct xt_table *info)
 {
        return xt_alloc_initial_table(ipt, IPT);
@@ -85,52 +63,28 @@ ip_packet_match(const struct iphdr *ip,
        if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
                  IPT_INV_SRCIP) ||
            FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
-                 IPT_INV_DSTIP)) {
-               dprintf("Source or dest mismatch.\n");
-
-               dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
-                       &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
-                       ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
-               dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
-                       &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
-                       ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
+                 IPT_INV_DSTIP))
                return false;
-       }
 
        ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
 
-       if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
-               dprintf("VIA in mismatch (%s vs %s).%s\n",
-                       indev, ipinfo->iniface,
-                       ipinfo->invflags & IPT_INV_VIA_IN ? " (INV)" : "");
+       if (FWINV(ret != 0, IPT_INV_VIA_IN))
                return false;
-       }
 
        ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
 
-       if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
-               dprintf("VIA out mismatch (%s vs %s).%s\n",
-                       outdev, ipinfo->outiface,
-                       ipinfo->invflags & IPT_INV_VIA_OUT ? " (INV)" : "");
+       if (FWINV(ret != 0, IPT_INV_VIA_OUT))
                return false;
-       }
 
        /* Check specific protocol */
        if (ipinfo->proto &&
-           FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
-               dprintf("Packet protocol %hi does not match %hi.%s\n",
-                       ip->protocol, ipinfo->proto,
-                       ipinfo->invflags & IPT_INV_PROTO ? " (INV)" : "");
+           FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO))
                return false;
-       }
 
        /* If we have a fragment rule but the packet is not a fragment
         * then we return zero */
-       if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
-               dprintf("Fragment rule but not fragment.%s\n",
-                       ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
+       if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG))
                return false;
-       }
 
        return true;
 }
@@ -138,16 +92,10 @@ ip_packet_match(const struct iphdr *ip,
 static bool
 ip_checkentry(const struct ipt_ip *ip)
 {
-       if (ip->flags & ~IPT_F_MASK) {
-               duprintf("Unknown flag bits set: %08X\n",
-                        ip->flags & ~IPT_F_MASK);
+       if (ip->flags & ~IPT_F_MASK)
                return false;
-       }
-       if (ip->invflags & ~IPT_INV_MASK) {
-               duprintf("Unknown invflag bits set: %08X\n",
-                        ip->invflags & ~IPT_INV_MASK);
+       if (ip->invflags & ~IPT_INV_MASK)
                return false;
-       }
        return true;
 }
 
@@ -346,10 +294,6 @@ ipt_do_table(struct sk_buff *skb,
 
        e = get_entry(table_base, private->hook_entry[hook]);
 
-       pr_debug("Entering %s(hook %u), UF %p\n",
-                table->name, hook,
-                get_entry(table_base, private->underflow[hook]));
-
        do {
                const struct xt_entry_target *t;
                const struct xt_entry_match *ematch;
@@ -396,22 +340,15 @@ ipt_do_table(struct sk_buff *skb,
                                if (stackidx == 0) {
                                        e = get_entry(table_base,
                                            private->underflow[hook]);
-                                       pr_debug("Underflow (this is normal) "
-                                                "to %p\n", e);
                                } else {
                                        e = jumpstack[--stackidx];
-                                       pr_debug("Pulled %p out from pos %u\n",
-                                                e, stackidx);
                                        e = ipt_next_entry(e);
                                }
                                continue;
                        }
                        if (table_base + v != ipt_next_entry(e) &&
-                           !(e->ip.flags & IPT_F_GOTO)) {
+                           !(e->ip.flags & IPT_F_GOTO))
                                jumpstack[stackidx++] = e;
-                               pr_debug("Pushed %p into pos %u\n",
-                                        e, stackidx - 1);
-                       }
 
                        e = get_entry(table_base, v);
                        continue;
@@ -429,18 +366,25 @@ ipt_do_table(struct sk_buff *skb,
                        /* Verdict */
                        break;
        } while (!acpar.hotdrop);
-       pr_debug("Exiting %s; sp at %u\n", __func__, stackidx);
 
        xt_write_recseq_end(addend);
        local_bh_enable();
 
-#ifdef DEBUG_ALLOW_ALL
-       return NF_ACCEPT;
-#else
        if (acpar.hotdrop)
                return NF_DROP;
        else return verdict;
-#endif
+}
+
+static bool find_jump_target(const struct xt_table_info *t,
+                            const struct ipt_entry *target)
+{
+       struct ipt_entry *iter;
+
+       xt_entry_foreach(iter, t->entries, t->size) {
+                if (iter == target)
+                       return true;
+       }
+       return false;
 }
 
 /* Figures out from what hook each rule can be called: returns 0 if
@@ -468,11 +412,9 @@ mark_source_chains(const struct xt_table_info *newinfo,
                                = (void *)ipt_get_target_c(e);
                        int visited = e->comefrom & (1 << hook);
 
-                       if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
-                               pr_err("iptables: loop hook %u pos %u %08X.\n",
-                                      hook, pos, e->comefrom);
+                       if (e->comefrom & (1 << NF_INET_NUMHOOKS))
                                return 0;
-                       }
+
                        e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
 
                        /* Unconditional return/END. */
@@ -484,26 +426,13 @@ mark_source_chains(const struct xt_table_info *newinfo,
 
                                if ((strcmp(t->target.u.user.name,
                                            XT_STANDARD_TARGET) == 0) &&
-                                   t->verdict < -NF_MAX_VERDICT - 1) {
-                                       duprintf("mark_source_chains: bad "
-                                               "negative verdict (%i)\n",
-                                                               t->verdict);
+                                   t->verdict < -NF_MAX_VERDICT - 1)
                                        return 0;
-                               }
 
                                /* Return: backtrack through the last
                                   big jump. */
                                do {
                                        e->comefrom ^= (1<<NF_INET_NUMHOOKS);
-#ifdef DEBUG_IP_FIREWALL_USER
-                                       if (e->comefrom
-                                           & (1 << NF_INET_NUMHOOKS)) {
-                                               duprintf("Back unset "
-                                                        "on hook %u "
-                                                        "rule %u\n",
-                                                        hook, pos);
-                                       }
-#endif
                                        oldpos = pos;
                                        pos = e->counters.pcnt;
                                        e->counters.pcnt = 0;
@@ -520,6 +449,8 @@ mark_source_chains(const struct xt_table_info *newinfo,
                                size = e->next_offset;
                                e = (struct ipt_entry *)
                                        (entry0 + pos + size);
+                               if (pos + size >= newinfo->size)
+                                       return 0;
                                e->counters.pcnt = pos;
                                pos += size;
                        } else {
@@ -528,19 +459,16 @@ mark_source_chains(const struct xt_table_info *newinfo,
                                if (strcmp(t->target.u.user.name,
                                           XT_STANDARD_TARGET) == 0 &&
                                    newpos >= 0) {
-                                       if (newpos > newinfo->size -
-                                               sizeof(struct ipt_entry)) {
-                                               duprintf("mark_source_chains: "
-                                                       "bad verdict (%i)\n",
-                                                               newpos);
-                                               return 0;
-                                       }
                                        /* This a jump; chase it. */
-                                       duprintf("Jump rule %u -> %u\n",
-                                                pos, newpos);
+                                       e = (struct ipt_entry *)
+                                               (entry0 + newpos);
+                                       if (!find_jump_target(newinfo, e))
+                                               return 0;
                                } else {
                                        /* ... this is a fallthru */
                                        newpos = pos + e->next_offset;
+                                       if (newpos >= newinfo->size)
+                                               return 0;
                                }
                                e = (struct ipt_entry *)
                                        (entry0 + newpos);
@@ -548,8 +476,7 @@ mark_source_chains(const struct xt_table_info *newinfo,
                                pos = newpos;
                        }
                }
-next:
-               duprintf("Finished chain %u\n", hook);
+next:          ;
        }
        return 1;
 }
@@ -567,41 +494,16 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)
        module_put(par.match->me);
 }
 
-static int
-check_entry(const struct ipt_entry *e)
-{
-       const struct xt_entry_target *t;
-
-       if (!ip_checkentry(&e->ip))
-               return -EINVAL;
-
-       if (e->target_offset + sizeof(struct xt_entry_target) >
-           e->next_offset)
-               return -EINVAL;
-
-       t = ipt_get_target_c(e);
-       if (e->target_offset + t->u.target_size > e->next_offset)
-               return -EINVAL;
-
-       return 0;
-}
-
 static int
 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
 {
        const struct ipt_ip *ip = par->entryinfo;
-       int ret;
 
        par->match     = m->u.kernel.match;
        par->matchinfo = m->data;
 
-       ret = xt_check_match(par, m->u.match_size - sizeof(*m),
-             ip->proto, ip->invflags & IPT_INV_PROTO);
-       if (ret < 0) {
-               duprintf("check failed for `%s'.\n", par->match->name);
-               return ret;
-       }
-       return 0;
+       return xt_check_match(par, m->u.match_size - sizeof(*m),
+                             ip->proto, ip->invflags & IPT_INV_PROTO);
 }
 
 static int
@@ -612,10 +514,8 @@ find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
 
        match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
                                      m->u.user.revision);
-       if (IS_ERR(match)) {
-               duprintf("find_check_match: `%s' not found\n", m->u.user.name);
+       if (IS_ERR(match))
                return PTR_ERR(match);
-       }
        m->u.kernel.match = match;
 
        ret = check_match(m, par);
@@ -640,16 +540,9 @@ static int check_target(struct ipt_entry *e, struct net *net, const char *name)
                .hook_mask = e->comefrom,
                .family    = NFPROTO_IPV4,
        };
-       int ret;
 
-       ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
-             e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
-       if (ret < 0) {
-               duprintf("check failed for `%s'.\n",
-                        t->u.kernel.target->name);
-               return ret;
-       }
-       return 0;
+       return xt_check_target(&par, t->u.target_size - sizeof(*t),
+                              e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
 }
 
 static int
@@ -662,10 +555,12 @@ find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
        unsigned int j;
        struct xt_mtchk_param mtpar;
        struct xt_entry_match *ematch;
+       unsigned long pcnt;
 
-       e->counters.pcnt = xt_percpu_counter_alloc();
-       if (IS_ERR_VALUE(e->counters.pcnt))
+       pcnt = xt_percpu_counter_alloc();
+       if (IS_ERR_VALUE(pcnt))
                return -ENOMEM;
+       e->counters.pcnt = pcnt;
 
        j = 0;
        mtpar.net       = net;
@@ -684,7 +579,6 @@ find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
        target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
                                        t->u.user.revision);
        if (IS_ERR(target)) {
-               duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
                ret = PTR_ERR(target);
                goto cleanup_matches;
        }
@@ -738,19 +632,18 @@ check_entry_size_and_hooks(struct ipt_entry *e,
 
        if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
            (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
-           (unsigned char *)e + e->next_offset > limit) {
-               duprintf("Bad offset %p\n", e);
+           (unsigned char *)e + e->next_offset > limit)
                return -EINVAL;
-       }
 
        if (e->next_offset
-           < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
-               duprintf("checking: element %p size %u\n",
-                        e, e->next_offset);
+           < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
+               return -EINVAL;
+
+       if (!ip_checkentry(&e->ip))
                return -EINVAL;
-       }
 
-       err = check_entry(e);
+       err = xt_check_entry_offsets(e, e->elems, e->target_offset,
+                                    e->next_offset);
        if (err)
                return err;
 
@@ -761,12 +654,9 @@ check_entry_size_and_hooks(struct ipt_entry *e,
                if ((unsigned char *)e - base == hook_entries[h])
                        newinfo->hook_entry[h] = hook_entries[h];
                if ((unsigned char *)e - base == underflows[h]) {
-                       if (!check_underflow(e)) {
-                               pr_debug("Underflows must be unconditional and "
-                                        "use the STANDARD target with "
-                                        "ACCEPT/DROP\n");
+                       if (!check_underflow(e))
                                return -EINVAL;
-                       }
+
                        newinfo->underflow[h] = underflows[h];
                }
        }
@@ -818,7 +708,6 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
                newinfo->underflow[i] = 0xFFFFFFFF;
        }
 
-       duprintf("translate_table: size %u\n", newinfo->size);
        i = 0;
        /* Walk through entries, checking offsets. */
        xt_entry_foreach(iter, entry0, newinfo->size) {
@@ -835,27 +724,18 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
                        ++newinfo->stacksize;
        }
 
-       if (i != repl->num_entries) {
-               duprintf("translate_table: %u not %u entries\n",
-                        i, repl->num_entries);
+       if (i != repl->num_entries)
                return -EINVAL;
-       }
 
        /* Check hooks all assigned */
        for (i = 0; i < NF_INET_NUMHOOKS; i++) {
                /* Only hooks which are valid */
                if (!(repl->valid_hooks & (1 << i)))
                        continue;
-               if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
-                       duprintf("Invalid hook entry %u %u\n",
-                                i, repl->hook_entry[i]);
+               if (newinfo->hook_entry[i] == 0xFFFFFFFF)
                        return -EINVAL;
-               }
-               if (newinfo->underflow[i] == 0xFFFFFFFF) {
-                       duprintf("Invalid underflow %u %u\n",
-                                i, repl->underflow[i]);
+               if (newinfo->underflow[i] == 0xFFFFFFFF)
                        return -EINVAL;
-               }
        }
 
        if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
@@ -1083,11 +963,8 @@ static int get_info(struct net *net, void __user *user,
        struct xt_table *t;
        int ret;
 
-       if (*len != sizeof(struct ipt_getinfo)) {
-               duprintf("length %u != %zu\n", *len,
-                        sizeof(struct ipt_getinfo));
+       if (*len != sizeof(struct ipt_getinfo))
                return -EINVAL;
-       }
 
        if (copy_from_user(name, user, sizeof(name)) != 0)
                return -EFAULT;
@@ -1145,31 +1022,23 @@ get_entries(struct net *net, struct ipt_get_entries __user *uptr,
        struct ipt_get_entries get;
        struct xt_table *t;
 
-       if (*len < sizeof(get)) {
-               duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
+       if (*len < sizeof(get))
                return -EINVAL;
-       }
        if (copy_from_user(&get, uptr, sizeof(get)) != 0)
                return -EFAULT;
-       if (*len != sizeof(struct ipt_get_entries) + get.size) {
-               duprintf("get_entries: %u != %zu\n",
-                        *len, sizeof(get) + get.size);
+       if (*len != sizeof(struct ipt_get_entries) + get.size)
                return -EINVAL;
-       }
        get.name[sizeof(get.name) - 1] = '\0';
 
        t = xt_find_table_lock(net, AF_INET, get.name);
        if (!IS_ERR_OR_NULL(t)) {
                const struct xt_table_info *private = t->private;
-               duprintf("t->private->number = %u\n", private->number);
                if (get.size == private->size)
                        ret = copy_entries_to_user(private->size,
                                                   t, uptr->entrytable);
-               else {
-                       duprintf("get_entries: I've got %u not %u!\n",
-                                private->size, get.size);
+               else
                        ret = -EAGAIN;
-               }
+
                module_put(t->me);
                xt_table_unlock(t);
        } else
@@ -1205,8 +1074,6 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 
        /* You lied! */
        if (valid_hooks != t->valid_hooks) {
-               duprintf("Valid hook crap: %08X vs %08X\n",
-                        valid_hooks, t->valid_hooks);
                ret = -EINVAL;
                goto put_module;
        }
@@ -1216,8 +1083,6 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
                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);
@@ -1286,8 +1151,6 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
        if (ret != 0)
                goto free_newinfo;
 
-       duprintf("Translated table\n");
-
        ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
                           tmp.num_counters, tmp.counters);
        if (ret)
@@ -1309,55 +1172,17 @@ do_add_counters(struct net *net, const void __user *user,
        unsigned int i;
        struct xt_counters_info tmp;
        struct xt_counters *paddc;
-       unsigned int num_counters;
-       const char *name;
-       int size;
-       void *ptmp;
        struct xt_table *t;
        const struct xt_table_info *private;
        int ret = 0;
        struct ipt_entry *iter;
        unsigned int addend;
-#ifdef CONFIG_COMPAT
-       struct compat_xt_counters_info compat_tmp;
 
-       if (compat) {
-               ptmp = &compat_tmp;
-               size = sizeof(struct compat_xt_counters_info);
-       } else
-#endif
-       {
-               ptmp = &tmp;
-               size = sizeof(struct xt_counters_info);
-       }
+       paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
+       if (IS_ERR(paddc))
+               return PTR_ERR(paddc);
 
-       if (copy_from_user(ptmp, user, size) != 0)
-               return -EFAULT;
-
-#ifdef CONFIG_COMPAT
-       if (compat) {
-               num_counters = compat_tmp.num_counters;
-               name = compat_tmp.name;
-       } else
-#endif
-       {
-               num_counters = tmp.num_counters;
-               name = tmp.name;
-       }
-
-       if (len != size + num_counters * sizeof(struct xt_counters))
-               return -EINVAL;
-
-       paddc = vmalloc(len - size);
-       if (!paddc)
-               return -ENOMEM;
-
-       if (copy_from_user(paddc, user + size, len - size) != 0) {
-               ret = -EFAULT;
-               goto free;
-       }
-
-       t = xt_find_table_lock(net, AF_INET, name);
+       t = xt_find_table_lock(net, AF_INET, tmp.name);
        if (IS_ERR_OR_NULL(t)) {
                ret = t ? PTR_ERR(t) : -ENOENT;
                goto free;
@@ -1365,7 +1190,7 @@ do_add_counters(struct net *net, const void __user *user,
 
        local_bh_disable();
        private = t->private;
-       if (private->number != num_counters) {
+       if (private->number != tmp.num_counters) {
                ret = -EINVAL;
                goto unlock_up_free;
        }
@@ -1444,7 +1269,6 @@ compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
 
 static int
 compat_find_calc_match(struct xt_entry_match *m,
-                      const char *name,
                       const struct ipt_ip *ip,
                       int *size)
 {
@@ -1452,11 +1276,9 @@ compat_find_calc_match(struct xt_entry_match *m,
 
        match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
                                      m->u.user.revision);
-       if (IS_ERR(match)) {
-               duprintf("compat_check_calc_match: `%s' not found\n",
-                        m->u.user.name);
+       if (IS_ERR(match))
                return PTR_ERR(match);
-       }
+
        m->u.kernel.match = match;
        *size += xt_compat_match_offset(match);
        return 0;
@@ -1479,35 +1301,29 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
                                  struct xt_table_info *newinfo,
                                  unsigned int *size,
                                  const unsigned char *base,
-                                 const unsigned char *limit,
-                                 const unsigned int *hook_entries,
-                                 const unsigned int *underflows,
-                                 const char *name)
+                                 const unsigned char *limit)
 {
        struct xt_entry_match *ematch;
        struct xt_entry_target *t;
        struct xt_target *target;
        unsigned int entry_offset;
        unsigned int j;
-       int ret, off, h;
+       int ret, off;
 
-       duprintf("check_compat_entry_size_and_hooks %p\n", e);
        if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
            (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
-           (unsigned char *)e + e->next_offset > limit) {
-               duprintf("Bad offset %p, limit = %p\n", e, limit);
+           (unsigned char *)e + e->next_offset > limit)
                return -EINVAL;
-       }
 
        if (e->next_offset < sizeof(struct compat_ipt_entry) +
-                            sizeof(struct compat_xt_entry_target)) {
-               duprintf("checking: element %p size %u\n",
-                        e, e->next_offset);
+                            sizeof(struct compat_xt_entry_target))
                return -EINVAL;
-       }
 
-       /* For purposes of check_entry casting the compat entry is fine */
-       ret = check_entry((struct ipt_entry *)e);
+       if (!ip_checkentry(&e->ip))
+               return -EINVAL;
+
+       ret = xt_compat_check_entry_offsets(e, e->elems,
+                                           e->target_offset, e->next_offset);
        if (ret)
                return ret;
 
@@ -1515,7 +1331,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
        entry_offset = (void *)e - (void *)base;
        j = 0;
        xt_ematch_foreach(ematch, e) {
-               ret = compat_find_calc_match(ematch, name, &e->ip, &off);
+               ret = compat_find_calc_match(ematch, &e->ip, &off);
                if (ret != 0)
                        goto release_matches;
                ++j;
@@ -1525,8 +1341,6 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
        target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
                                        t->u.user.revision);
        if (IS_ERR(target)) {
-               duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
-                        t->u.user.name);
                ret = PTR_ERR(target);
                goto release_matches;
        }
@@ -1538,17 +1352,6 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
        if (ret)
                goto out;
 
-       /* Check hooks & underflows */
-       for (h = 0; h < NF_INET_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];
-       }
-
-       /* Clear counters and comefrom */
-       memset(&e->counters, 0, sizeof(e->counters));
-       e->comefrom = 0;
        return 0;
 
 out:
@@ -1562,19 +1365,18 @@ release_matches:
        return ret;
 }
 
-static int
+static void
 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
-                           unsigned int *size, const char *name,
+                           unsigned int *size,
                            struct xt_table_info *newinfo, unsigned char *base)
 {
        struct xt_entry_target *t;
        struct xt_target *target;
        struct ipt_entry *de;
        unsigned int origsize;
-       int ret, h;
+       int h;
        struct xt_entry_match *ematch;
 
-       ret = 0;
        origsize = *size;
        de = (struct ipt_entry *)*dstptr;
        memcpy(de, e, sizeof(struct ipt_entry));
@@ -1583,201 +1385,101 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
        *dstptr += sizeof(struct ipt_entry);
        *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
 
-       xt_ematch_foreach(ematch, e) {
-               ret = xt_compat_match_from_user(ematch, dstptr, size);
-               if (ret != 0)
-                       return ret;
-       }
+       xt_ematch_foreach(ematch, e)
+               xt_compat_match_from_user(ematch, dstptr, size);
+
        de->target_offset = e->target_offset - (origsize - *size);
        t = compat_ipt_get_target(e);
        target = t->u.kernel.target;
        xt_compat_target_from_user(t, dstptr, size);
 
        de->next_offset = e->next_offset - (origsize - *size);
+
        for (h = 0; h < NF_INET_NUMHOOKS; h++) {
                if ((unsigned char *)de - base < newinfo->hook_entry[h])
                        newinfo->hook_entry[h] -= origsize - *size;
                if ((unsigned char *)de - base < newinfo->underflow[h])
                        newinfo->underflow[h] -= origsize - *size;
        }
-       return ret;
-}
-
-static int
-compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
-{
-       struct xt_entry_match *ematch;
-       struct xt_mtchk_param mtpar;
-       unsigned int j;
-       int ret = 0;
-
-       e->counters.pcnt = xt_percpu_counter_alloc();
-       if (IS_ERR_VALUE(e->counters.pcnt))
-               return -ENOMEM;
-
-       j = 0;
-       mtpar.net       = net;
-       mtpar.table     = name;
-       mtpar.entryinfo = &e->ip;
-       mtpar.hook_mask = e->comefrom;
-       mtpar.family    = NFPROTO_IPV4;
-       xt_ematch_foreach(ematch, e) {
-               ret = check_match(ematch, &mtpar);
-               if (ret != 0)
-                       goto cleanup_matches;
-               ++j;
-       }
-
-       ret = check_target(e, net, name);
-       if (ret)
-               goto cleanup_matches;
-       return 0;
-
- cleanup_matches:
-       xt_ematch_foreach(ematch, e) {
-               if (j-- == 0)
-                       break;
-               cleanup_match(ematch, net);
-       }
-
-       xt_percpu_counter_free(e->counters.pcnt);
-
-       return ret;
 }
 
 static int
 translate_compat_table(struct net *net,
-                      const char *name,
-                      unsigned int valid_hooks,
                       struct xt_table_info **pinfo,
                       void **pentry0,
-                      unsigned int total_size,
-                      unsigned int number,
-                      unsigned int *hook_entries,
-                      unsigned int *underflows)
+                      const struct compat_ipt_replace *compatr)
 {
        unsigned int i, j;
        struct xt_table_info *newinfo, *info;
        void *pos, *entry0, *entry1;
        struct compat_ipt_entry *iter0;
-       struct ipt_entry *iter1;
+       struct ipt_replace repl;
        unsigned int size;
        int ret;
 
        info = *pinfo;
        entry0 = *pentry0;
-       size = total_size;
-       info->number = number;
-
-       /* Init all hooks to impossible value. */
-       for (i = 0; i < NF_INET_NUMHOOKS; i++) {
-               info->hook_entry[i] = 0xFFFFFFFF;
-               info->underflow[i] = 0xFFFFFFFF;
-       }
+       size = compatr->size;
+       info->number = compatr->num_entries;
 
-       duprintf("translate_compat_table: size %u\n", info->size);
        j = 0;
        xt_compat_lock(AF_INET);
-       xt_compat_init_offsets(AF_INET, number);
+       xt_compat_init_offsets(AF_INET, compatr->num_entries);
        /* Walk through entries, checking offsets. */
-       xt_entry_foreach(iter0, entry0, total_size) {
+       xt_entry_foreach(iter0, entry0, compatr->size) {
                ret = check_compat_entry_size_and_hooks(iter0, info, &size,
                                                        entry0,
-                                                       entry0 + total_size,
-                                                       hook_entries,
-                                                       underflows,
-                                                       name);
+                                                       entry0 + compatr->size);
                if (ret != 0)
                        goto out_unlock;
                ++j;
        }
 
        ret = -EINVAL;
-       if (j != number) {
-               duprintf("translate_compat_table: %u not %u entries\n",
-                        j, number);
+       if (j != compatr->num_entries)
                goto out_unlock;
-       }
-
-       /* Check hooks all assigned */
-       for (i = 0; i < NF_INET_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;
+       newinfo->number = compatr->num_entries;
        for (i = 0; i < NF_INET_NUMHOOKS; i++) {
-               newinfo->hook_entry[i] = info->hook_entry[i];
-               newinfo->underflow[i] = info->underflow[i];
+               newinfo->hook_entry[i] = compatr->hook_entry[i];
+               newinfo->underflow[i] = compatr->underflow[i];
        }
        entry1 = newinfo->entries;
        pos = entry1;
-       size = total_size;
-       xt_entry_foreach(iter0, entry0, total_size) {
-               ret = compat_copy_entry_from_user(iter0, &pos, &size,
-                                                 name, newinfo, entry1);
-               if (ret != 0)
-                       break;
-       }
+       size = compatr->size;
+       xt_entry_foreach(iter0, entry0, compatr->size)
+               compat_copy_entry_from_user(iter0, &pos, &size,
+                                           newinfo, entry1);
+
+       /* all module references in entry0 are now gone.
+        * entry1/newinfo contains a 64bit ruleset that looks exactly as
+        * generated by 64bit userspace.
+        *
+        * Call standard translate_table() to validate all hook_entrys,
+        * underflows, check for loops, etc.
+        */
        xt_compat_flush_offsets(AF_INET);
        xt_compat_unlock(AF_INET);
-       if (ret)
-               goto free_newinfo;
 
-       ret = -ELOOP;
-       if (!mark_source_chains(newinfo, valid_hooks, entry1))
-               goto free_newinfo;
+       memcpy(&repl, compatr, sizeof(*compatr));
 
-       i = 0;
-       xt_entry_foreach(iter1, entry1, newinfo->size) {
-               ret = compat_check_entry(iter1, net, name);
-               if (ret != 0)
-                       break;
-               ++i;
-               if (strcmp(ipt_get_target(iter1)->u.user.name,
-                   XT_ERROR_TARGET) == 0)
-                       ++newinfo->stacksize;
-       }
-       if (ret) {
-               /*
-                * The first i matches need cleanup_entry (calls ->destroy)
-                * because they had called ->check already. The other j-i
-                * entries need only release.
-                */
-               int skip = i;
-               j -= i;
-               xt_entry_foreach(iter0, entry0, newinfo->size) {
-                       if (skip-- > 0)
-                               continue;
-                       if (j-- == 0)
-                               break;
-                       compat_release_entry(iter0);
-               }
-               xt_entry_foreach(iter1, entry1, newinfo->size) {
-                       if (i-- == 0)
-                               break;
-                       cleanup_entry(iter1, net);
-               }
-               xt_free_table_info(newinfo);
-               return ret;
+       for (i = 0; i < NF_INET_NUMHOOKS; i++) {
+               repl.hook_entry[i] = newinfo->hook_entry[i];
+               repl.underflow[i] = newinfo->underflow[i];
        }
 
+       repl.num_counters = 0;
+       repl.counters = NULL;
+       repl.size = newinfo->size;
+       ret = translate_table(net, newinfo, entry1, &repl);
+       if (ret)
+               goto free_newinfo;
+
        *pinfo = newinfo;
        *pentry0 = entry1;
        xt_free_table_info(info);
@@ -1785,17 +1487,16 @@ translate_compat_table(struct net *net,
 
 free_newinfo:
        xt_free_table_info(newinfo);
-out:
-       xt_entry_foreach(iter0, entry0, total_size) {
+       return ret;
+out_unlock:
+       xt_compat_flush_offsets(AF_INET);
+       xt_compat_unlock(AF_INET);
+       xt_entry_foreach(iter0, entry0, compatr->size) {
                if (j-- == 0)
                        break;
                compat_release_entry(iter0);
        }
        return ret;
-out_unlock:
-       xt_compat_flush_offsets(AF_INET);
-       xt_compat_unlock(AF_INET);
-       goto out;
 }
 
 static int
@@ -1811,8 +1512,6 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
                return -EFAULT;
 
        /* overflow check */
-       if (tmp.size >= INT_MAX / num_possible_cpus())
-               return -ENOMEM;
        if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
                return -ENOMEM;
        if (tmp.num_counters == 0)
@@ -1831,15 +1530,10 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
                goto free_newinfo;
        }
 
-       ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
-                                    &newinfo, &loc_cpu_entry, tmp.size,
-                                    tmp.num_entries, tmp.hook_entry,
-                                    tmp.underflow);
+       ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
        if (ret != 0)
                goto free_newinfo;
 
-       duprintf("compat_do_replace: Translated table\n");
-
        ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
                           tmp.num_counters, compat_ptr(tmp.counters));
        if (ret)
@@ -1873,7 +1567,6 @@ compat_do_ipt_set_ctl(struct sock *sk,    int cmd, void __user *user,
                break;
 
        default:
-               duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
                ret = -EINVAL;
        }
 
@@ -1923,19 +1616,15 @@ compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
        struct compat_ipt_get_entries get;
        struct xt_table *t;
 
-       if (*len < sizeof(get)) {
-               duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
+       if (*len < 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 != %zu\n",
-                        *len, sizeof(get) + get.size);
+       if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
                return -EINVAL;
-       }
+
        get.name[sizeof(get.name) - 1] = '\0';
 
        xt_compat_lock(AF_INET);
@@ -1943,16 +1632,13 @@ compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
        if (!IS_ERR_OR_NULL(t)) {
                const 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) {
+               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);
+               else if (!ret)
                        ret = -EAGAIN;
-               }
+
                xt_compat_flush_offsets(AF_INET);
                module_put(t->me);
                xt_table_unlock(t);
@@ -2005,7 +1691,6 @@ do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
                break;
 
        default:
-               duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
                ret = -EINVAL;
        }
 
@@ -2057,7 +1742,6 @@ do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
        }
 
        default:
-               duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
                ret = -EINVAL;
        }
 
@@ -2159,7 +1843,6 @@ icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
                /* We've been asked to examine this packet, and we
                 * can't.  Hence, no choice but to drop.
                 */
-               duprintf("Dropping evil ICMP tinygram.\n");
                par->hotdrop = true;
                return false;
        }