netfilter: xtables: optimize call flow around xt_entry_foreach
[pandora-kernel.git] / net / ipv6 / netfilter / ip6_tables.c
index 480d7f8..b7e27c1 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>");
@@ -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
@@ -201,7 +208,7 @@ ip6t_error(struct sk_buff *skb, const struct xt_target_param *par)
 
 /* 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 +222,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 +236,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 +277,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 +307,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 +327,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 ",
@@ -345,9 +359,9 @@ ip6t_do_table(struct sk_buff *skb,
        /* Initializing verdict to NF_DROP keeps gcc happy. */
        unsigned int verdict = NF_DROP;
        const char *indev, *outdev;
-       void *table_base;
+       const void *table_base;
        struct ip6t_entry *e, *back;
-       struct xt_table_info *private;
+       const struct xt_table_info *private;
        struct xt_match_param mtpar;
        struct xt_target_param tgpar;
 
@@ -378,7 +392,7 @@ ip6t_do_table(struct sk_buff *skb,
        back = get_entry(table_base, private->underflow[hook]);
 
        do {
-               struct ip6t_entry_target *t;
+               const struct ip6t_entry_target *t;
 
                IP_NF_ASSERT(e);
                IP_NF_ASSERT(back);
@@ -393,7 +407,7 @@ ip6t_do_table(struct sk_buff *skb,
                            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) || \
@@ -475,7 +489,7 @@ ip6t_do_table(struct sk_buff *skb,
 /* 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 +507,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)) {
@@ -585,13 +599,14 @@ mark_source_chains(struct xt_table_info *newinfo,
 }
 
 static int
-cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
+cleanup_match(struct ip6t_entry_match *m, struct net *net, unsigned int *i)
 {
        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;
@@ -602,9 +617,9 @@ cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
 }
 
 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,7 +630,7 @@ 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;
 
@@ -668,10 +683,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,8 +709,8 @@ 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;
@@ -707,6 +723,7 @@ find_check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
                return ret;
 
        j = 0;
+       mtpar.net       = net;
        mtpar.table     = name;
        mtpar.entryinfo = &e->ipv6;
        mtpar.hook_mask = e->comefrom;
@@ -727,27 +744,25 @@ find_check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
        }
        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);
+       IP6T_MATCH_ITERATE(e, cleanup_match, net, &j);
        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 +773,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,37 +814,32 @@ 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;
-
        /* Cleanup all matches */
-       IP6T_MATCH_ITERATE(e, cleanup_match, NULL);
+       IP6T_MATCH_ITERATE(e, cleanup_match, net, NULL);
        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,
+translate_table(struct net *net,
+               const char *name,
                unsigned int valid_hooks,
                struct xt_table_info *newinfo,
                void *entry0,
@@ -839,8 +848,9 @@ translate_table(const char *name,
                const unsigned int *hook_entries,
                const unsigned int *underflows)
 {
+       struct ip6t_entry *iter;
        unsigned int i;
-       int ret;
+       int ret = 0;
 
        newinfo->size = size;
        newinfo->number = number;
@@ -854,14 +864,13 @@ 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 + size, hook_entries, underflows, valid_hooks);
+               if (ret != 0)
+                       return ret;
+               ++i;
+       }
 
        if (i != number) {
                duprintf("translate_table: %u not %u entries\n",
@@ -891,12 +900,19 @@ translate_table(const char *name,
 
        /* 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, name, 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 +925,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 +945,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 +988,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 +1044,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 +1061,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 +1070,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;
 
@@ -1086,24 +1080,24 @@ static int compat_standard_to_user(void __user *dst, void *src)
 }
 
 static inline int
-compat_calc_match(struct ip6t_entry_match *m, int *size)
+compat_calc_match(const 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 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);
+       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 +1118,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 +1129,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;
@@ -1164,10 +1164,10 @@ static int get_info(struct net *net, void __user *user, int *len, int compat)
        if (t && !IS_ERR(t)) {
                struct ip6t_getinfo info;
                const struct xt_table_info *private = t->private;
-
 #ifdef CONFIG_COMPAT
+               struct xt_table_info tmp;
+
                if (compat) {
-                       struct xt_table_info tmp;
                        ret = compat_table_info(private, &tmp);
                        xt_compat_flush_offsets(AF_INET6);
                        private = &tmp;
@@ -1199,7 +1199,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 +1248,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 +1292,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 +1313,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,7 +1340,7 @@ do_replace(struct net *net, void __user *user, unsigned int len)
                goto free_newinfo;
        }
 
-       ret = translate_table(tmp.name, tmp.valid_hooks,
+       ret = translate_table(net, tmp.name, tmp.valid_hooks,
                              newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
                              tmp.hook_entry, tmp.underflow);
        if (ret != 0)
@@ -1351,27 +1355,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 +1377,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 +1436,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,7 +1468,7 @@ 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;
@@ -1484,14 +1476,12 @@ compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
        compat_uint_t origsize;
        int ret;
 
-       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);
@@ -1499,22 +1489,16 @@ compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
        ret = IP6T_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
        target_offset = e->target_offset - (origsize - *size);
        if (ret)
-               goto out;
+               return ret;
        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
@@ -1551,30 +1535,24 @@ compat_release_match(struct ip6t_entry_match *m, unsigned int *i)
        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;
-
        /* Cleanup all matches */
        COMPAT_IP6T_MATCH_ITERATE(e, compat_release_match, NULL);
        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 ip6t_entry_target *t;
@@ -1640,8 +1618,6 @@ 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:
@@ -1690,14 +1666,15 @@ 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;
        struct xt_mtchk_param mtpar;
 
        j = 0;
+       mtpar.net       = net;
        mtpar.table     = name;
        mtpar.entryinfo = &e->ipv6;
        mtpar.hook_mask = e->comefrom;
@@ -1706,20 +1683,19 @@ static int compat_check_entry(struct ip6t_entry *e, const char *name,
        if (ret)
                goto cleanup_matches;
 
-       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);
+       IP6T_MATCH_ITERATE(e, cleanup_match, net, &j);
        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 +1707,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 +1727,14 @@ 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 +1773,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 +1789,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 +1832,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 +1851,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 +1874,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 +1890,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 +1940,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 +1953,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;
@@ -2121,7 +2132,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,
+       ret = translate_table(net, table->name, table->valid_hooks,
                              newinfo, loc_cpu_entry, repl->size,
                              repl->num_entries,
                              repl->hook_entry,
@@ -2142,17 +2153,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);