netfilter: xtables: make ip_tables reentrant
[pandora-kernel.git] / net / ipv6 / netfilter / ip6_tables.c
index 8a7e0f5..2a2770b 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/netfilter_ipv6/ip6_tables.h>
 #include <linux/netfilter/x_tables.h>
 #include <net/netfilter/nf_log.h>
+#include "../../netfilter/xt_repldata.h"
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
@@ -39,13 +40,13 @@ MODULE_DESCRIPTION("IPv6 packet filter");
 /*#define DEBUG_IP_FIREWALL_USER*/
 
 #ifdef DEBUG_IP_FIREWALL
-#define dprintf(format, args...)  printk(format , ## args)
+#define dprintf(format, args...) pr_info(format , ## args)
 #else
 #define dprintf(format, args...)
 #endif
 
 #ifdef DEBUG_IP_FIREWALL_USER
-#define duprintf(format, args...) printk(format , ## args)
+#define duprintf(format, args...) pr_info(format , ## args)
 #else
 #define duprintf(format, args...)
 #endif
@@ -67,6 +68,12 @@ do {                                                         \
 #define inline
 #endif
 
+void *ip6t_alloc_initial_table(const struct xt_table *info)
+{
+       return xt_alloc_initial_table(ip6t, IP6T);
+}
+EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table);
+
 /*
    We keep a set of rules for each CPU, so we can avoid write-locking
    them in the softirq when updating the counters and therefore
@@ -193,15 +200,14 @@ static unsigned int
 ip6t_error(struct sk_buff *skb, const struct xt_target_param *par)
 {
        if (net_ratelimit())
-               printk("ip6_tables: error: `%s'\n",
-                      (const char *)par->targinfo);
+               pr_info("error: `%s'\n", (const char *)par->targinfo);
 
        return NF_DROP;
 }
 
 /* Performance critical - called for every packet */
 static inline bool
-do_match(struct ip6t_entry_match *m, const struct sk_buff *skb,
+do_match(const struct ip6t_entry_match *m, const struct sk_buff *skb,
         struct xt_match_param *par)
 {
        par->match     = m->u.kernel.match;
@@ -215,7 +221,7 @@ do_match(struct ip6t_entry_match *m, const struct sk_buff *skb,
 }
 
 static inline struct ip6t_entry *
-get_entry(void *base, unsigned int offset)
+get_entry(const void *base, unsigned int offset)
 {
        return (struct ip6t_entry *)(base + offset);
 }
@@ -229,6 +235,12 @@ static inline bool unconditional(const struct ip6t_ip6 *ipv6)
        return memcmp(ipv6, &uncond, sizeof(uncond)) == 0;
 }
 
+static inline const struct ip6t_entry_target *
+ip6t_get_target_c(const struct ip6t_entry *e)
+{
+       return ip6t_get_target((struct ip6t_entry *)e);
+}
+
 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
 /* This cries for unification! */
@@ -264,11 +276,11 @@ static struct nf_loginfo trace_loginfo = {
 
 /* Mildly perf critical (only if packet tracing is on) */
 static inline int
-get_chainname_rulenum(struct ip6t_entry *s, struct ip6t_entry *e,
+get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
                      const char *hookname, const char **chainname,
                      const char **comment, unsigned int *rulenum)
 {
-       struct ip6t_standard_target *t = (void *)ip6t_get_target(s);
+       const struct ip6t_standard_target *t = (void *)ip6t_get_target_c(s);
 
        if (strcmp(t->target.u.kernel.target->name, IP6T_ERROR_TARGET) == 0) {
                /* Head of user chain: ERROR target with chainname */
@@ -294,17 +306,18 @@ get_chainname_rulenum(struct ip6t_entry *s, struct ip6t_entry *e,
        return 0;
 }
 
-static void trace_packet(struct sk_buff *skb,
+static void trace_packet(const struct sk_buff *skb,
                         unsigned int hook,
                         const struct net_device *in,
                         const struct net_device *out,
                         const char *tablename,
-                        struct xt_table_info *private,
-                        struct ip6t_entry *e)
+                        const struct xt_table_info *private,
+                        const struct ip6t_entry *e)
 {
-       void *table_base;
+       const void *table_base;
        const struct ip6t_entry *root;
        const char *hookname, *chainname, *comment;
+       const struct ip6t_entry *iter;
        unsigned int rulenum = 0;
 
        table_base = private->entries[smp_processor_id()];
@@ -313,10 +326,10 @@ static void trace_packet(struct sk_buff *skb,
        hookname = chainname = hooknames[hook];
        comment = comments[NF_IP6_TRACE_COMMENT_RULE];
 
-       IP6T_ENTRY_ITERATE(root,
-                          private->size - private->hook_entry[hook],
-                          get_chainname_rulenum,
-                          e, hookname, &chainname, &comment, &rulenum);
+       xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
+               if (get_chainname_rulenum(iter, e, hookname,
+                   &chainname, &comment, &rulenum) != 0)
+                       break;
 
        nf_log_packet(AF_INET6, hook, skb, in, out, &trace_loginfo,
                      "TRACE: %s:%s:%s:%u ",
@@ -338,16 +351,15 @@ ip6t_do_table(struct sk_buff *skb,
              const struct net_device *out,
              struct xt_table *table)
 {
-#define tb_comefrom ((struct ip6t_entry *)table_base)->comefrom
-
        static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
        bool hotdrop = false;
        /* Initializing verdict to NF_DROP keeps gcc happy. */
        unsigned int verdict = NF_DROP;
        const char *indev, *outdev;
-       void *table_base;
-       struct ip6t_entry *e, *back;
-       struct xt_table_info *private;
+       const void *table_base;
+       struct ip6t_entry *e, **jumpstack;
+       unsigned int *stackptr, origptr, cpu;
+       const struct xt_table_info *private;
        struct xt_match_param mtpar;
        struct xt_target_param tgpar;
 
@@ -370,30 +382,35 @@ ip6t_do_table(struct sk_buff *skb,
 
        xt_info_rdlock_bh();
        private = table->private;
-       table_base = private->entries[smp_processor_id()];
+       cpu        = smp_processor_id();
+       table_base = private->entries[cpu];
+       jumpstack  = (struct ip6t_entry **)private->jumpstack[cpu];
+       stackptr   = &private->stackptr[cpu];
+       origptr    = *stackptr;
 
        e = get_entry(table_base, private->hook_entry[hook]);
 
-       /* For return from builtin chain */
-       back = get_entry(table_base, private->underflow[hook]);
-
        do {
-               struct ip6t_entry_target *t;
+               const struct ip6t_entry_target *t;
+               const struct xt_entry_match *ematch;
 
                IP_NF_ASSERT(e);
-               IP_NF_ASSERT(back);
                if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
-                   &mtpar.thoff, &mtpar.fragoff, &hotdrop) ||
-                   IP6T_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0) {
+                   &mtpar.thoff, &mtpar.fragoff, &hotdrop)) {
+ no_match:
                        e = ip6t_next_entry(e);
                        continue;
                }
 
+               xt_ematch_foreach(ematch, e)
+                       if (do_match(ematch, skb, &mtpar) != 0)
+                               goto no_match;
+
                ADD_COUNTER(e->counters,
                            ntohs(ipv6_hdr(skb)->payload_len) +
                            sizeof(struct ipv6hdr), 1);
 
-               t = ip6t_get_target(e);
+               t = ip6t_get_target_c(e);
                IP_NF_ASSERT(t->u.kernel.target);
 
 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
@@ -414,17 +431,20 @@ ip6t_do_table(struct sk_buff *skb,
                                        verdict = (unsigned)(-v) - 1;
                                        break;
                                }
-                               e = back;
-                               back = get_entry(table_base, back->comefrom);
+                               if (*stackptr == 0)
+                                       e = get_entry(table_base,
+                                           private->underflow[hook]);
+                               else
+                                       e = ip6t_next_entry(jumpstack[--*stackptr]);
                                continue;
                        }
                        if (table_base + v != ip6t_next_entry(e) &&
                            !(e->ipv6.flags & IP6T_F_GOTO)) {
-                               /* Save old back ptr in next entry */
-                               struct ip6t_entry *next = ip6t_next_entry(e);
-                               next->comefrom = (void *)back - table_base;
-                               /* set back pointer to next entry */
-                               back = next;
+                               if (*stackptr >= private->stacksize) {
+                                       verdict = NF_DROP;
+                                       break;
+                               }
+                               jumpstack[(*stackptr)++] = e;
                        }
 
                        e = get_entry(table_base, v);
@@ -436,19 +456,7 @@ ip6t_do_table(struct sk_buff *skb,
                tgpar.target   = t->u.kernel.target;
                tgpar.targinfo = t->data;
 
-#ifdef CONFIG_NETFILTER_DEBUG
-               tb_comefrom = 0xeeeeeeec;
-#endif
                verdict = t->u.kernel.target->target(skb, &tgpar);
-
-#ifdef CONFIG_NETFILTER_DEBUG
-               if (tb_comefrom != 0xeeeeeeec && verdict == IP6T_CONTINUE) {
-                       printk("Target %s reentered!\n",
-                              t->u.kernel.target->name);
-                       verdict = NF_DROP;
-               }
-               tb_comefrom = 0x57acc001;
-#endif
                if (verdict == IP6T_CONTINUE)
                        e = ip6t_next_entry(e);
                else
@@ -456,10 +464,8 @@ ip6t_do_table(struct sk_buff *skb,
                        break;
        } while (!hotdrop);
 
-#ifdef CONFIG_NETFILTER_DEBUG
-       tb_comefrom = NETFILTER_LINK_POISON;
-#endif
        xt_info_rdunlock_bh();
+       *stackptr = origptr;
 
 #ifdef DEBUG_ALLOW_ALL
        return NF_ACCEPT;
@@ -468,14 +474,12 @@ ip6t_do_table(struct sk_buff *skb,
                return NF_DROP;
        else return verdict;
 #endif
-
-#undef tb_comefrom
 }
 
 /* Figures out from what hook each rule can be called: returns 0 if
    there are loops.  Puts hook bitmask in comefrom. */
 static int
-mark_source_chains(struct xt_table_info *newinfo,
+mark_source_chains(const struct xt_table_info *newinfo,
                   unsigned int valid_hooks, void *entry0)
 {
        unsigned int hook;
@@ -493,8 +497,8 @@ mark_source_chains(struct xt_table_info *newinfo,
                e->counters.pcnt = pos;
 
                for (;;) {
-                       struct ip6t_standard_target *t
-                               = (void *)ip6t_get_target(e);
+                       const struct ip6t_standard_target *t
+                               = (void *)ip6t_get_target_c(e);
                        int visited = e->comefrom & (1 << hook);
 
                        if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
@@ -584,27 +588,23 @@ mark_source_chains(struct xt_table_info *newinfo,
        return 1;
 }
 
-static int
-cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
+static void cleanup_match(struct ip6t_entry_match *m, struct net *net)
 {
        struct xt_mtdtor_param par;
 
-       if (i && (*i)-- == 0)
-               return 1;
-
+       par.net       = net;
        par.match     = m->u.kernel.match;
        par.matchinfo = m->data;
        par.family    = NFPROTO_IPV6;
        if (par.match->destroy != NULL)
                par.match->destroy(&par);
        module_put(par.match->me);
-       return 0;
 }
 
 static int
-check_entry(struct ip6t_entry *e, const char *name)
+check_entry(const struct ip6t_entry *e, const char *name)
 {
-       struct ip6t_entry_target *t;
+       const struct ip6t_entry_target *t;
 
        if (!ip6_checkentry(&e->ipv6)) {
                duprintf("ip_tables: ip check failed %p %s.\n", e, name);
@@ -615,15 +615,14 @@ check_entry(struct ip6t_entry *e, const char *name)
            e->next_offset)
                return -EINVAL;
 
-       t = ip6t_get_target(e);
+       t = ip6t_get_target_c(e);
        if (e->target_offset + t->u.target_size > e->next_offset)
                return -EINVAL;
 
        return 0;
 }
 
-static int check_match(struct ip6t_entry_match *m, struct xt_mtchk_param *par,
-                      unsigned int *i)
+static int check_match(struct ip6t_entry_match *m, struct xt_mtchk_param *par)
 {
        const struct ip6t_ip6 *ipv6 = par->entryinfo;
        int ret;
@@ -638,27 +637,24 @@ static int check_match(struct ip6t_entry_match *m, struct xt_mtchk_param *par,
                         par.match->name);
                return ret;
        }
-       ++*i;
        return 0;
 }
 
 static int
-find_check_match(struct ip6t_entry_match *m, struct xt_mtchk_param *par,
-                unsigned int *i)
+find_check_match(struct ip6t_entry_match *m, struct xt_mtchk_param *par)
 {
        struct xt_match *match;
        int ret;
 
-       match = try_then_request_module(xt_find_match(AF_INET6, m->u.user.name,
-                                                     m->u.user.revision),
-                                       "ip6t_%s", m->u.user.name);
-       if (IS_ERR(match) || !match) {
+       match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
+                                     m->u.user.revision);
+       if (IS_ERR(match)) {
                duprintf("find_check_match: `%s' not found\n", m->u.user.name);
-               return match ? PTR_ERR(match) : -ENOENT;
+               return PTR_ERR(match);
        }
        m->u.kernel.match = match;
 
-       ret = check_match(m, par, i);
+       ret = check_match(m, par);
        if (ret)
                goto err;
 
@@ -668,10 +664,11 @@ err:
        return ret;
 }
 
-static int check_target(struct ip6t_entry *e, const char *name)
+static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
 {
        struct ip6t_entry_target *t = ip6t_get_target(e);
        struct xt_tgchk_param par = {
+               .net       = net,
                .table     = name,
                .entryinfo = e,
                .target    = t->u.kernel.target,
@@ -693,61 +690,66 @@ static int check_target(struct ip6t_entry *e, const char *name)
 }
 
 static int
-find_check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
-                unsigned int *i)
+find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
+                unsigned int size)
 {
        struct ip6t_entry_target *t;
        struct xt_target *target;
        int ret;
        unsigned int j;
        struct xt_mtchk_param mtpar;
+       struct xt_entry_match *ematch;
 
        ret = check_entry(e, name);
        if (ret)
                return ret;
 
        j = 0;
+       mtpar.net       = net;
        mtpar.table     = name;
        mtpar.entryinfo = &e->ipv6;
        mtpar.hook_mask = e->comefrom;
        mtpar.family    = NFPROTO_IPV6;
-       ret = IP6T_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
-       if (ret != 0)
-               goto cleanup_matches;
+       xt_ematch_foreach(ematch, e) {
+               ret = find_check_match(ematch, &mtpar);
+               if (ret != 0)
+                       goto cleanup_matches;
+               ++j;
+       }
 
        t = ip6t_get_target(e);
-       target = try_then_request_module(xt_find_target(AF_INET6,
-                                                       t->u.user.name,
-                                                       t->u.user.revision),
-                                        "ip6t_%s", t->u.user.name);
-       if (IS_ERR(target) || !target) {
+       target = xt_request_find_target(NFPROTO_IPV6, 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 = target ? PTR_ERR(target) : -ENOENT;
+               ret = PTR_ERR(target);
                goto cleanup_matches;
        }
        t->u.kernel.target = target;
 
-       ret = check_target(e, name);
+       ret = check_target(e, net, name);
        if (ret)
                goto err;
-
-       (*i)++;
        return 0;
  err:
        module_put(t->u.kernel.target->me);
  cleanup_matches:
-       IP6T_MATCH_ITERATE(e, cleanup_match, &j);
+       xt_ematch_foreach(ematch, e) {
+               if (j-- == 0)
+                       break;
+               cleanup_match(ematch, net);
+       }
        return ret;
 }
 
-static bool check_underflow(struct ip6t_entry *e)
+static bool check_underflow(const struct ip6t_entry *e)
 {
        const struct ip6t_entry_target *t;
        unsigned int verdict;
 
        if (!unconditional(&e->ipv6))
                return false;
-       t = ip6t_get_target(e);
+       t = ip6t_get_target_c(e);
        if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
                return false;
        verdict = ((struct ip6t_standard_target *)t)->verdict;
@@ -758,12 +760,11 @@ static bool check_underflow(struct ip6t_entry *e)
 static int
 check_entry_size_and_hooks(struct ip6t_entry *e,
                           struct xt_table_info *newinfo,
-                          unsigned char *base,
-                          unsigned char *limit,
+                          const unsigned char *base,
+                          const unsigned char *limit,
                           const unsigned int *hook_entries,
                           const unsigned int *underflows,
-                          unsigned int valid_hooks,
-                          unsigned int *i)
+                          unsigned int valid_hooks)
 {
        unsigned int h;
 
@@ -800,50 +801,41 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
        /* Clear counters and comefrom */
        e->counters = ((struct xt_counters) { 0, 0 });
        e->comefrom = 0;
-
-       (*i)++;
        return 0;
 }
 
-static int
-cleanup_entry(struct ip6t_entry *e, unsigned int *i)
+static void cleanup_entry(struct ip6t_entry *e, struct net *net)
 {
        struct xt_tgdtor_param par;
        struct ip6t_entry_target *t;
-
-       if (i && (*i)-- == 0)
-               return 1;
+       struct xt_entry_match *ematch;
 
        /* Cleanup all matches */
-       IP6T_MATCH_ITERATE(e, cleanup_match, NULL);
+       xt_ematch_foreach(ematch, e)
+               cleanup_match(ematch, net);
        t = ip6t_get_target(e);
 
+       par.net      = net;
        par.target   = t->u.kernel.target;
        par.targinfo = t->data;
        par.family   = NFPROTO_IPV6;
        if (par.target->destroy != NULL)
                par.target->destroy(&par);
        module_put(par.target->me);
-       return 0;
 }
 
 /* Checks and translates the user-supplied table segment (held in
    newinfo) */
 static int
-translate_table(const char *name,
-               unsigned int valid_hooks,
-               struct xt_table_info *newinfo,
-               void *entry0,
-               unsigned int size,
-               unsigned int number,
-               const unsigned int *hook_entries,
-               const unsigned int *underflows)
+translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
+                const struct ip6t_replace *repl)
 {
+       struct ip6t_entry *iter;
        unsigned int i;
-       int ret;
+       int ret = 0;
 
-       newinfo->size = size;
-       newinfo->number = number;
+       newinfo->size = repl->size;
+       newinfo->number = repl->num_entries;
 
        /* Init all hooks to impossible value. */
        for (i = 0; i < NF_INET_NUMHOOKS; i++) {
@@ -854,49 +846,61 @@ translate_table(const char *name,
        duprintf("translate_table: size %u\n", newinfo->size);
        i = 0;
        /* Walk through entries, checking offsets. */
-       ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
-                               check_entry_size_and_hooks,
-                               newinfo,
-                               entry0,
-                               entry0 + size,
-                               hook_entries, underflows, valid_hooks, &i);
-       if (ret != 0)
-               return ret;
+       xt_entry_foreach(iter, entry0, newinfo->size) {
+               ret = check_entry_size_and_hooks(iter, newinfo, entry0,
+                                                entry0 + repl->size,
+                                                repl->hook_entry,
+                                                repl->underflow,
+                                                repl->valid_hooks);
+               if (ret != 0)
+                       return ret;
+               ++i;
+               if (strcmp(ip6t_get_target(iter)->u.user.name,
+                   XT_ERROR_TARGET) == 0)
+                       ++newinfo->stacksize;
+       }
 
-       if (i != number) {
+       if (i != repl->num_entries) {
                duprintf("translate_table: %u not %u entries\n",
-                        i, number);
+                        i, repl->num_entries);
                return -EINVAL;
        }
 
        /* Check hooks all assigned */
        for (i = 0; i < NF_INET_NUMHOOKS; i++) {
                /* Only hooks which are valid */
-               if (!(valid_hooks & (1 << i)))
+               if (!(repl->valid_hooks & (1 << i)))
                        continue;
                if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
                        duprintf("Invalid hook entry %u %u\n",
-                                i, hook_entries[i]);
+                                i, repl->hook_entry[i]);
                        return -EINVAL;
                }
                if (newinfo->underflow[i] == 0xFFFFFFFF) {
                        duprintf("Invalid underflow %u %u\n",
-                                i, underflows[i]);
+                                i, repl->underflow[i]);
                        return -EINVAL;
                }
        }
 
-       if (!mark_source_chains(newinfo, valid_hooks, entry0))
+       if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
                return -ELOOP;
 
        /* Finally, each sanity check must pass */
        i = 0;
-       ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
-                               find_check_entry, name, size, &i);
+       xt_entry_foreach(iter, entry0, newinfo->size) {
+               ret = find_check_entry(iter, net, repl->name, repl->size);
+               if (ret != 0)
+                       break;
+               ++i;
+       }
 
        if (ret != 0) {
-               IP6T_ENTRY_ITERATE(entry0, newinfo->size,
-                                  cleanup_entry, &i);
+               xt_entry_foreach(iter, entry0, newinfo->size) {
+                       if (i-- == 0)
+                               break;
+                       cleanup_entry(iter, net);
+               }
                return ret;
        }
 
@@ -909,33 +913,11 @@ translate_table(const char *name,
        return ret;
 }
 
-/* Gets counters. */
-static inline int
-add_entry_to_counter(const struct ip6t_entry *e,
-                    struct xt_counters total[],
-                    unsigned int *i)
-{
-       ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
-
-       (*i)++;
-       return 0;
-}
-
-static inline int
-set_entry_to_counter(const struct ip6t_entry *e,
-                    struct ip6t_counters total[],
-                    unsigned int *i)
-{
-       SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
-
-       (*i)++;
-       return 0;
-}
-
 static void
 get_counters(const struct xt_table_info *t,
             struct xt_counters counters[])
 {
+       struct ip6t_entry *iter;
        unsigned int cpu;
        unsigned int i;
        unsigned int curcpu;
@@ -951,32 +933,32 @@ get_counters(const struct xt_table_info *t,
        curcpu = smp_processor_id();
 
        i = 0;
-       IP6T_ENTRY_ITERATE(t->entries[curcpu],
-                          t->size,
-                          set_entry_to_counter,
-                          counters,
-                          &i);
+       xt_entry_foreach(iter, t->entries[curcpu], t->size) {
+               SET_COUNTER(counters[i], iter->counters.bcnt,
+                           iter->counters.pcnt);
+               ++i;
+       }
 
        for_each_possible_cpu(cpu) {
                if (cpu == curcpu)
                        continue;
                i = 0;
                xt_info_wrlock(cpu);
-               IP6T_ENTRY_ITERATE(t->entries[cpu],
-                                 t->size,
-                                 add_entry_to_counter,
-                                 counters,
-                                 &i);
+               xt_entry_foreach(iter, t->entries[cpu], t->size) {
+                       ADD_COUNTER(counters[i], iter->counters.bcnt,
+                                   iter->counters.pcnt);
+                       ++i;
+               }
                xt_info_wrunlock(cpu);
        }
        local_bh_enable();
 }
 
-static struct xt_counters *alloc_counters(struct xt_table *table)
+static struct xt_counters *alloc_counters(const struct xt_table *table)
 {
        unsigned int countersize;
        struct xt_counters *counters;
-       struct xt_table_info *private = table->private;
+       const struct xt_table_info *private = table->private;
 
        /* We need atomic snapshot of counters: rest doesn't change
           (other than comefrom, which userspace doesn't care
@@ -994,11 +976,11 @@ static struct xt_counters *alloc_counters(struct xt_table *table)
 
 static int
 copy_entries_to_user(unsigned int total_size,
-                    struct xt_table *table,
+                    const struct xt_table *table,
                     void __user *userptr)
 {
        unsigned int off, num;
-       struct ip6t_entry *e;
+       const struct ip6t_entry *e;
        struct xt_counters *counters;
        const struct xt_table_info *private = table->private;
        int ret = 0;
@@ -1050,7 +1032,7 @@ copy_entries_to_user(unsigned int total_size,
                        }
                }
 
-               t = ip6t_get_target(e);
+               t = ip6t_get_target_c(e);
                if (copy_to_user(userptr + off + e->target_offset
                                 + offsetof(struct ip6t_entry_target,
                                            u.user.name),
@@ -1067,7 +1049,7 @@ copy_entries_to_user(unsigned int total_size,
 }
 
 #ifdef CONFIG_COMPAT
-static void compat_standard_from_user(void *dst, void *src)
+static void compat_standard_from_user(void *dst, const void *src)
 {
        int v = *(compat_int_t *)src;
 
@@ -1076,7 +1058,7 @@ static void compat_standard_from_user(void *dst, void *src)
        memcpy(dst, &v, sizeof(v));
 }
 
-static int compat_standard_to_user(void __user *dst, void *src)
+static int compat_standard_to_user(void __user *dst, const void *src)
 {
        compat_int_t cv = *(int *)src;
 
@@ -1085,25 +1067,20 @@ static int compat_standard_to_user(void __user *dst, void *src)
        return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
 }
 
-static inline int
-compat_calc_match(struct ip6t_entry_match *m, int *size)
-{
-       *size += xt_compat_match_offset(m->u.kernel.match);
-       return 0;
-}
-
-static int compat_calc_entry(struct ip6t_entry *e,
+static int compat_calc_entry(const struct ip6t_entry *e,
                             const struct xt_table_info *info,
-                            void *base, struct xt_table_info *newinfo)
+                            const void *base, struct xt_table_info *newinfo)
 {
-       struct ip6t_entry_target *t;
+       const struct xt_entry_match *ematch;
+       const struct ip6t_entry_target *t;
        unsigned int entry_offset;
        int off, i, ret;
 
        off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
        entry_offset = (void *)e - base;
-       IP6T_MATCH_ITERATE(e, compat_calc_match, &off);
-       t = ip6t_get_target(e);
+       xt_ematch_foreach(ematch, e)
+               off += xt_compat_match_offset(ematch->u.kernel.match);
+       t = ip6t_get_target_c(e);
        off += xt_compat_target_offset(t->u.kernel.target);
        newinfo->size -= off;
        ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
@@ -1124,7 +1101,9 @@ static int compat_calc_entry(struct ip6t_entry *e,
 static int compat_table_info(const struct xt_table_info *info,
                             struct xt_table_info *newinfo)
 {
+       struct ip6t_entry *iter;
        void *loc_cpu_entry;
+       int ret;
 
        if (!newinfo || !info)
                return -EINVAL;
@@ -1133,13 +1112,17 @@ static int compat_table_info(const struct xt_table_info *info,
        memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
        newinfo->initial_entries = 0;
        loc_cpu_entry = info->entries[raw_smp_processor_id()];
-       return IP6T_ENTRY_ITERATE(loc_cpu_entry, info->size,
-                                 compat_calc_entry, info, loc_cpu_entry,
-                                 newinfo);
+       xt_entry_foreach(iter, loc_cpu_entry, info->size) {
+               ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
+               if (ret != 0)
+                       return ret;
+       }
+       return 0;
 }
 #endif
 
-static int get_info(struct net *net, void __user *user, int *len, int compat)
+static int get_info(struct net *net, void __user *user,
+                    const int *len, int compat)
 {
        char name[IP6T_TABLE_MAXNAMELEN];
        struct xt_table *t;
@@ -1199,7 +1182,8 @@ static int get_info(struct net *net, void __user *user, int *len, int compat)
 }
 
 static int
-get_entries(struct net *net, struct ip6t_get_entries __user *uptr, int *len)
+get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
+            const int *len)
 {
        int ret;
        struct ip6t_get_entries get;
@@ -1247,6 +1231,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
        struct xt_table_info *oldinfo;
        struct xt_counters *counters;
        const void *loc_cpu_old_entry;
+       struct ip6t_entry *iter;
 
        ret = 0;
        counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
@@ -1290,8 +1275,9 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 
        /* Decrease module usage counts and free resource */
        loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
-       IP6T_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
-                          NULL);
+       xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
+               cleanup_entry(iter, net);
+
        xt_free_table_info(oldinfo);
        if (copy_to_user(counters_ptr, counters,
                         sizeof(struct xt_counters) * num_counters) != 0)
@@ -1310,12 +1296,13 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 }
 
 static int
-do_replace(struct net *net, void __user *user, unsigned int len)
+do_replace(struct net *net, const void __user *user, unsigned int len)
 {
        int ret;
        struct ip6t_replace tmp;
        struct xt_table_info *newinfo;
        void *loc_cpu_entry;
+       struct ip6t_entry *iter;
 
        if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
                return -EFAULT;
@@ -1336,9 +1323,7 @@ do_replace(struct net *net, void __user *user, unsigned int len)
                goto free_newinfo;
        }
 
-       ret = translate_table(tmp.name, tmp.valid_hooks,
-                             newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
-                             tmp.hook_entry, tmp.underflow);
+       ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
        if (ret != 0)
                goto free_newinfo;
 
@@ -1351,27 +1336,15 @@ do_replace(struct net *net, void __user *user, unsigned int len)
        return 0;
 
  free_newinfo_untrans:
-       IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
+               cleanup_entry(iter, net);
  free_newinfo:
        xt_free_table_info(newinfo);
        return ret;
 }
 
-/* We're lazy, and add to the first CPU; overflow works its fey magic
- * and everything is OK. */
-static int
-add_counter_to_entry(struct ip6t_entry *e,
-                    const struct xt_counters addme[],
-                    unsigned int *i)
-{
-       ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
-
-       (*i)++;
-       return 0;
-}
-
 static int
-do_add_counters(struct net *net, void __user *user, unsigned int len,
+do_add_counters(struct net *net, const void __user *user, unsigned int len,
                int compat)
 {
        unsigned int i, curcpu;
@@ -1385,6 +1358,7 @@ do_add_counters(struct net *net, void __user *user, unsigned int len,
        const struct xt_table_info *private;
        int ret = 0;
        const void *loc_cpu_entry;
+       struct ip6t_entry *iter;
 #ifdef CONFIG_COMPAT
        struct compat_xt_counters_info compat_tmp;
 
@@ -1443,11 +1417,10 @@ do_add_counters(struct net *net, void __user *user, unsigned int len,
        curcpu = smp_processor_id();
        xt_info_wrlock(curcpu);
        loc_cpu_entry = private->entries[curcpu];
-       IP6T_ENTRY_ITERATE(loc_cpu_entry,
-                         private->size,
-                         add_counter_to_entry,
-                         paddc,
-                         &i);
+       xt_entry_foreach(iter, loc_cpu_entry, private->size) {
+               ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
+               ++i;
+       }
        xt_info_wrunlock(curcpu);
 
  unlock_up_free:
@@ -1476,45 +1449,40 @@ struct compat_ip6t_replace {
 static int
 compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
                          unsigned int *size, struct xt_counters *counters,
-                         unsigned int *i)
+                         unsigned int i)
 {
        struct ip6t_entry_target *t;
        struct compat_ip6t_entry __user *ce;
        u_int16_t target_offset, next_offset;
        compat_uint_t origsize;
-       int ret;
+       const struct xt_entry_match *ematch;
+       int ret = 0;
 
-       ret = -EFAULT;
        origsize = *size;
        ce = (struct compat_ip6t_entry __user *)*dstptr;
-       if (copy_to_user(ce, e, sizeof(struct ip6t_entry)))
-               goto out;
-
-       if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
-               goto out;
+       if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
+           copy_to_user(&ce->counters, &counters[i],
+           sizeof(counters[i])) != 0)
+               return -EFAULT;
 
        *dstptr += sizeof(struct compat_ip6t_entry);
        *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
 
-       ret = IP6T_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
+       xt_ematch_foreach(ematch, e) {
+               ret = xt_compat_match_to_user(ematch, dstptr, size);
+               if (ret != 0)
+                       return ret;
+       }
        target_offset = e->target_offset - (origsize - *size);
-       if (ret)
-               goto out;
        t = ip6t_get_target(e);
        ret = xt_compat_target_to_user(t, dstptr, size);
        if (ret)
-               goto out;
-       ret = -EFAULT;
+               return ret;
        next_offset = e->next_offset - (origsize - *size);
-       if (put_user(target_offset, &ce->target_offset))
-               goto out;
-       if (put_user(next_offset, &ce->next_offset))
-               goto out;
-
-       (*i)++;
+       if (put_user(target_offset, &ce->target_offset) != 0 ||
+           put_user(next_offset, &ce->next_offset) != 0)
+               return -EFAULT;
        return 0;
-out:
-       return ret;
 }
 
 static int
@@ -1522,61 +1490,45 @@ compat_find_calc_match(struct ip6t_entry_match *m,
                       const char *name,
                       const struct ip6t_ip6 *ipv6,
                       unsigned int hookmask,
-                      int *size, unsigned int *i)
+                      int *size)
 {
        struct xt_match *match;
 
-       match = try_then_request_module(xt_find_match(AF_INET6, m->u.user.name,
-                                                     m->u.user.revision),
-                                       "ip6t_%s", m->u.user.name);
-       if (IS_ERR(match) || !match) {
+       match = xt_request_find_match(NFPROTO_IPV6, 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);
-               return match ? PTR_ERR(match) : -ENOENT;
+               return PTR_ERR(match);
        }
        m->u.kernel.match = match;
        *size += xt_compat_match_offset(match);
-
-       (*i)++;
-       return 0;
-}
-
-static int
-compat_release_match(struct ip6t_entry_match *m, unsigned int *i)
-{
-       if (i && (*i)-- == 0)
-               return 1;
-
-       module_put(m->u.kernel.match->me);
        return 0;
 }
 
-static int
-compat_release_entry(struct compat_ip6t_entry *e, unsigned int *i)
+static void compat_release_entry(struct compat_ip6t_entry *e)
 {
        struct ip6t_entry_target *t;
-
-       if (i && (*i)-- == 0)
-               return 1;
+       struct xt_entry_match *ematch;
 
        /* Cleanup all matches */
-       COMPAT_IP6T_MATCH_ITERATE(e, compat_release_match, NULL);
+       xt_ematch_foreach(ematch, e)
+               module_put(ematch->u.kernel.match->me);
        t = compat_ip6t_get_target(e);
        module_put(t->u.kernel.target->me);
-       return 0;
 }
 
 static int
 check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
                                  struct xt_table_info *newinfo,
                                  unsigned int *size,
-                                 unsigned char *base,
-                                 unsigned char *limit,
-                                 unsigned int *hook_entries,
-                                 unsigned int *underflows,
-                                 unsigned int *i,
+                                 const unsigned char *base,
+                                 const unsigned char *limit,
+                                 const unsigned int *hook_entries,
+                                 const unsigned int *underflows,
                                  const char *name)
 {
+       struct xt_entry_match *ematch;
        struct ip6t_entry_target *t;
        struct xt_target *target;
        unsigned int entry_offset;
@@ -1605,20 +1557,21 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
        off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
        entry_offset = (void *)e - (void *)base;
        j = 0;
-       ret = COMPAT_IP6T_MATCH_ITERATE(e, compat_find_calc_match, name,
-                                       &e->ipv6, e->comefrom, &off, &j);
-       if (ret != 0)
-               goto release_matches;
+       xt_ematch_foreach(ematch, e) {
+               ret = compat_find_calc_match(ematch, name,
+                                            &e->ipv6, e->comefrom, &off);
+               if (ret != 0)
+                       goto release_matches;
+               ++j;
+       }
 
        t = compat_ip6t_get_target(e);
-       target = try_then_request_module(xt_find_target(AF_INET6,
-                                                       t->u.user.name,
-                                                       t->u.user.revision),
-                                        "ip6t_%s", t->u.user.name);
-       if (IS_ERR(target) || !target) {
+       target = xt_request_find_target(NFPROTO_IPV6, 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 = target ? PTR_ERR(target) : -ENOENT;
+               ret = PTR_ERR(target);
                goto release_matches;
        }
        t->u.kernel.target = target;
@@ -1640,14 +1593,16 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
        /* Clear counters and comefrom */
        memset(&e->counters, 0, sizeof(e->counters));
        e->comefrom = 0;
-
-       (*i)++;
        return 0;
 
 out:
        module_put(t->u.kernel.target->me);
 release_matches:
-       IP6T_MATCH_ITERATE(e, compat_release_match, &j);
+       xt_ematch_foreach(ematch, e) {
+               if (j-- == 0)
+                       break;
+               module_put(ematch->u.kernel.match->me);
+       }
        return ret;
 }
 
@@ -1661,6 +1616,7 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
        struct ip6t_entry *de;
        unsigned int origsize;
        int ret, h;
+       struct xt_entry_match *ematch;
 
        ret = 0;
        origsize = *size;
@@ -1671,10 +1627,11 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
        *dstptr += sizeof(struct ip6t_entry);
        *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
 
-       ret = COMPAT_IP6T_MATCH_ITERATE(e, xt_compat_match_from_user,
-                                       dstptr, size);
-       if (ret)
-               return ret;
+       xt_ematch_foreach(ematch, e) {
+               ret = xt_compat_match_from_user(ematch, dstptr, size);
+               if (ret != 0)
+                       return ret;
+       }
        de->target_offset = e->target_offset - (origsize - *size);
        t = compat_ip6t_get_target(e);
        target = t->u.kernel.target;
@@ -1690,36 +1647,44 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
        return ret;
 }
 
-static int compat_check_entry(struct ip6t_entry *e, const char *name,
-                                    unsigned int *i)
+static int compat_check_entry(struct ip6t_entry *e, struct net *net,
+                             const char *name)
 {
        unsigned int j;
-       int ret;
+       int ret = 0;
        struct xt_mtchk_param mtpar;
+       struct xt_entry_match *ematch;
 
        j = 0;
+       mtpar.net       = net;
        mtpar.table     = name;
        mtpar.entryinfo = &e->ipv6;
        mtpar.hook_mask = e->comefrom;
        mtpar.family    = NFPROTO_IPV6;
-       ret = IP6T_MATCH_ITERATE(e, check_match, &mtpar, &j);
-       if (ret)
-               goto cleanup_matches;
+       xt_ematch_foreach(ematch, e) {
+               ret = check_match(ematch, &mtpar);
+               if (ret != 0)
+                       goto cleanup_matches;
+               ++j;
+       }
 
-       ret = check_target(e, name);
+       ret = check_target(e, net, name);
        if (ret)
                goto cleanup_matches;
-
-       (*i)++;
        return 0;
 
  cleanup_matches:
-       IP6T_MATCH_ITERATE(e, cleanup_match, &j);
+       xt_ematch_foreach(ematch, e) {
+               if (j-- == 0)
+                       break;
+               cleanup_match(ematch, net);
+       }
        return ret;
 }
 
 static int
-translate_compat_table(const char *name,
+translate_compat_table(struct net *net,
+                      const char *name,
                       unsigned int valid_hooks,
                       struct xt_table_info **pinfo,
                       void **pentry0,
@@ -1731,8 +1696,10 @@ translate_compat_table(const char *name,
        unsigned int i, j;
        struct xt_table_info *newinfo, *info;
        void *pos, *entry0, *entry1;
+       struct compat_ip6t_entry *iter0;
+       struct ip6t_entry *iter1;
        unsigned int size;
-       int ret;
+       int ret = 0;
 
        info = *pinfo;
        entry0 = *pentry0;
@@ -1749,13 +1716,17 @@ translate_compat_table(const char *name,
        j = 0;
        xt_compat_lock(AF_INET6);
        /* Walk through entries, checking offsets. */
-       ret = COMPAT_IP6T_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;
+       xt_entry_foreach(iter0, entry0, total_size) {
+               ret = check_compat_entry_size_and_hooks(iter0, info, &size,
+                                                       entry0,
+                                                       entry0 + total_size,
+                                                       hook_entries,
+                                                       underflows,
+                                                       name);
+               if (ret != 0)
+                       goto out_unlock;
+               ++j;
+       }
 
        ret = -EINVAL;
        if (j != number) {
@@ -1794,9 +1765,12 @@ translate_compat_table(const char *name,
        entry1 = newinfo->entries[raw_smp_processor_id()];
        pos = entry1;
        size = total_size;
-       ret = COMPAT_IP6T_ENTRY_ITERATE(entry0, total_size,
-                                       compat_copy_entry_from_user,
-                                       &pos, &size, name, newinfo, entry1);
+       xt_entry_foreach(iter0, entry0, total_size) {
+               ret = compat_copy_entry_from_user(iter0, &pos, &size,
+                                                 name, newinfo, entry1);
+               if (ret != 0)
+                       break;
+       }
        xt_compat_flush_offsets(AF_INET6);
        xt_compat_unlock(AF_INET6);
        if (ret)
@@ -1807,13 +1781,32 @@ translate_compat_table(const char *name,
                goto free_newinfo;
 
        i = 0;
-       ret = IP6T_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
-                                name, &i);
+       xt_entry_foreach(iter1, entry1, newinfo->size) {
+               ret = compat_check_entry(iter1, net, name);
+               if (ret != 0)
+                       break;
+               ++i;
+       }
        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;
-               COMPAT_IP6T_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
-                                                  compat_release_entry, &j);
-               IP6T_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &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;
        }
@@ -1831,7 +1824,11 @@ translate_compat_table(const char *name,
 free_newinfo:
        xt_free_table_info(newinfo);
 out:
-       COMPAT_IP6T_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
+       xt_entry_foreach(iter0, entry0, total_size) {
+               if (j-- == 0)
+                       break;
+               compat_release_entry(iter0);
+       }
        return ret;
 out_unlock:
        xt_compat_flush_offsets(AF_INET6);
@@ -1846,6 +1843,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
        struct compat_ip6t_replace tmp;
        struct xt_table_info *newinfo;
        void *loc_cpu_entry;
+       struct ip6t_entry *iter;
 
        if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
                return -EFAULT;
@@ -1868,7 +1866,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
                goto free_newinfo;
        }
 
-       ret = translate_compat_table(tmp.name, tmp.valid_hooks,
+       ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
                                     &newinfo, &loc_cpu_entry, tmp.size,
                                     tmp.num_entries, tmp.hook_entry,
                                     tmp.underflow);
@@ -1884,7 +1882,8 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
        return 0;
 
  free_newinfo_untrans:
-       IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
+               cleanup_entry(iter, net);
  free_newinfo:
        xt_free_table_info(newinfo);
        return ret;
@@ -1933,6 +1932,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
        int ret = 0;
        const void *loc_cpu_entry;
        unsigned int i = 0;
+       struct ip6t_entry *iter;
 
        counters = alloc_counters(table);
        if (IS_ERR(counters))
@@ -1945,9 +1945,12 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
        loc_cpu_entry = private->entries[raw_smp_processor_id()];
        pos = userptr;
        size = total_size;
-       ret = IP6T_ENTRY_ITERATE(loc_cpu_entry, total_size,
-                                compat_copy_entry_to_user,
-                                &pos, &size, counters, &i);
+       xt_entry_foreach(iter, loc_cpu_entry, total_size) {
+               ret = compat_copy_entry_to_user(iter, &pos,
+                                               &size, counters, i++);
+               if (ret != 0)
+                       break;
+       }
 
        vfree(counters);
        return ret;
@@ -2106,8 +2109,7 @@ struct xt_table *ip6t_register_table(struct net *net,
 {
        int ret;
        struct xt_table_info *newinfo;
-       struct xt_table_info bootstrap
-               = { 0, 0, 0, { 0 }, { 0 }, { } };
+       struct xt_table_info bootstrap = {0};
        void *loc_cpu_entry;
        struct xt_table *new_table;
 
@@ -2121,11 +2123,7 @@ struct xt_table *ip6t_register_table(struct net *net,
        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);
+       ret = translate_table(net, newinfo, loc_cpu_entry, repl);
        if (ret != 0)
                goto out_free;
 
@@ -2142,17 +2140,19 @@ out:
        return ERR_PTR(ret);
 }
 
-void ip6t_unregister_table(struct xt_table *table)
+void ip6t_unregister_table(struct net *net, struct xt_table *table)
 {
        struct xt_table_info *private;
        void *loc_cpu_entry;
        struct module *table_owner = table->me;
+       struct ip6t_entry *iter;
 
        private = xt_unregister_table(table);
 
        /* Decrease module usage counts and free resources */
        loc_cpu_entry = private->entries[raw_smp_processor_id()];
-       IP6T_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, private->size)
+               cleanup_entry(iter, net);
        if (private->number > private->initial_entries)
                module_put(table_owner);
        xt_free_table_info(private);
@@ -2197,12 +2197,12 @@ icmp6_match(const struct sk_buff *skb, const struct xt_match_param *par)
 }
 
 /* Called when user tries to insert an entry of this type. */
-static bool icmp6_checkentry(const struct xt_mtchk_param *par)
+static int icmp6_checkentry(const struct xt_mtchk_param *par)
 {
        const struct ip6t_icmp *icmpinfo = par->matchinfo;
 
        /* Must specify no unknown invflags */
-       return !(icmpinfo->invflags & ~IP6T_ICMP_INV);
+       return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
 }
 
 /* The built-in targets: standard (NULL) and error. */
@@ -2289,7 +2289,7 @@ static int __init ip6_tables_init(void)
        if (ret < 0)
                goto err5;
 
-       printk(KERN_INFO "ip6_tables: (C) 2000-2006 Netfilter Core Team\n");
+       pr_info("(C) 2000-2006 Netfilter Core Team\n");
        return 0;
 
 err5: