gre: fix hard header destination address checking
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / arp_tables.c
index 03e83a6..f07d77f 100644 (file)
@@ -8,7 +8,7 @@
  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  *
  */
-
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 #include <linux/kernel.h>
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
@@ -27,6 +27,7 @@
 
 #include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_arp/arp_tables.h>
+#include "../../netfilter/xt_repldata.h"
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
@@ -58,6 +59,12 @@ do {                                                         \
 #define ARP_NF_ASSERT(x)
 #endif
 
+void *arpt_alloc_initial_table(const struct xt_table *info)
+{
+       return xt_alloc_initial_table(arpt, ARPT);
+}
+EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
+
 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
                                      const char *hdr_addr, int len)
 {
@@ -73,6 +80,28 @@ static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
        return (ret != 0);
 }
 
+/*
+ * Unfortunatly, _b and _mask are not aligned to an int (or long int)
+ * Some arches dont care, unrolling the loop is a win on them.
+ * For other arches, we only have a 16bit alignement.
+ */
+static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
+{
+#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
+       unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
+#else
+       unsigned long ret = 0;
+       const u16 *a = (const u16 *)_a;
+       const u16 *b = (const u16 *)_b;
+       const u16 *mask = (const u16 *)_mask;
+       int i;
+
+       for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
+               ret |= (a[i] ^ b[i]) & mask[i];
+#endif
+       return ret;
+}
+
 /* Returns whether packet matches rule or not. */
 static inline int arp_packet_match(const struct arphdr *arphdr,
                                   struct net_device *dev,
@@ -83,7 +112,7 @@ static inline int arp_packet_match(const struct arphdr *arphdr,
        const char *arpptr = (char *)(arphdr + 1);
        const char *src_devaddr, *tgt_devaddr;
        __be32 src_ipaddr, tgt_ipaddr;
-       int i, ret;
+       long ret;
 
 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
 
@@ -142,24 +171,21 @@ static inline int arp_packet_match(const struct arphdr *arphdr,
                  ARPT_INV_TGTIP)) {
                dprintf("Source or target IP address mismatch.\n");
 
-               dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
-                       NIPQUAD(src_ipaddr),
-                       NIPQUAD(arpinfo->smsk.s_addr),
-                       NIPQUAD(arpinfo->src.s_addr),
+               dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
+                       &src_ipaddr,
+                       &arpinfo->smsk.s_addr,
+                       &arpinfo->src.s_addr,
                        arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
-               dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
-                       NIPQUAD(tgt_ipaddr),
-                       NIPQUAD(arpinfo->tmsk.s_addr),
-                       NIPQUAD(arpinfo->tgt.s_addr),
+               dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
+                       &tgt_ipaddr,
+                       &arpinfo->tmsk.s_addr,
+                       &arpinfo->tgt.s_addr,
                        arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
                return 0;
        }
 
        /* Look for ifname matches.  */
-       for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
-               ret |= (indev[i] ^ arpinfo->iniface[i])
-                       & arpinfo->iniface_mask[i];
-       }
+       ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
 
        if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
                dprintf("VIA in mismatch (%s vs %s).%s\n",
@@ -168,10 +194,7 @@ static inline int arp_packet_match(const struct arphdr *arphdr,
                return 0;
        }
 
-       for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
-               ret |= (outdev[i] ^ arpinfo->outiface[i])
-                       & arpinfo->outiface_mask[i];
-       }
+       ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
 
        if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
                dprintf("VIA out mismatch (%s vs %s).%s\n",
@@ -200,31 +223,41 @@ static inline int arp_checkentry(const struct arpt_arp *arp)
        return 1;
 }
 
-static unsigned int arpt_error(struct sk_buff *skb,
-                              const struct net_device *in,
-                              const struct net_device *out,
-                              unsigned int hooknum,
-                              const struct xt_target *target,
-                              const void *targinfo)
+static unsigned int
+arpt_error(struct sk_buff *skb, const struct xt_target_param *par)
 {
        if (net_ratelimit())
-               printk("arp_tables: error: '%s'\n", (char *)targinfo);
+               printk("arp_tables: error: '%s'\n",
+                      (const char *)par->targinfo);
 
        return NF_DROP;
 }
 
-static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
+static inline const struct arpt_entry_target *
+arpt_get_target_c(const struct arpt_entry *e)
+{
+       return arpt_get_target((struct arpt_entry *)e);
+}
+
+static inline struct arpt_entry *
+get_entry(const void *base, unsigned int offset)
 {
        return (struct arpt_entry *)(base + offset);
 }
 
+static inline __pure
+struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
+{
+       return (void *)entry + entry->next_offset;
+}
+
 unsigned int arpt_do_table(struct sk_buff *skb,
                           unsigned int hook,
                           const struct net_device *in,
                           const struct net_device *out,
                           struct xt_table *table)
 {
-       static const char nulldevname[IFNAMSIZ];
+       static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
        unsigned int verdict = NF_DROP;
        const struct arphdr *arp;
        bool hotdrop = false;
@@ -232,6 +265,7 @@ unsigned int arpt_do_table(struct sk_buff *skb,
        const char *indev, *outdev;
        void *table_base;
        const struct xt_table_info *private;
+       struct xt_target_param tgpar;
 
        if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
                return NF_DROP;
@@ -239,77 +273,80 @@ unsigned int arpt_do_table(struct sk_buff *skb,
        indev = in ? in->name : nulldevname;
        outdev = out ? out->name : nulldevname;
 
-       read_lock_bh(&table->lock);
+       xt_info_rdlock_bh();
        private = table->private;
-       table_base = (void *)private->entries[smp_processor_id()];
+       table_base = private->entries[smp_processor_id()];
+
        e = get_entry(table_base, private->hook_entry[hook]);
        back = get_entry(table_base, private->underflow[hook]);
 
+       tgpar.in      = in;
+       tgpar.out     = out;
+       tgpar.hooknum = hook;
+       tgpar.family  = NFPROTO_ARP;
+
        arp = arp_hdr(skb);
        do {
-               if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
-                       struct arpt_entry_target *t;
-                       int hdr_len;
-
-                       hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
-                               (2 * skb->dev->addr_len);
-                       ADD_COUNTER(e->counters, hdr_len, 1);
-
-                       t = arpt_get_target(e);
-
-                       /* Standard target? */
-                       if (!t->u.kernel.target->target) {
-                               int v;
-
-                               v = ((struct arpt_standard_target *)t)->verdict;
-                               if (v < 0) {
-                                       /* Pop from stack? */
-                                       if (v != ARPT_RETURN) {
-                                               verdict = (unsigned)(-v) - 1;
-                                               break;
-                                       }
-                                       e = back;
-                                       back = get_entry(table_base,
-                                                        back->comefrom);
-                                       continue;
-                               }
-                               if (table_base + v
-                                   != (void *)e + e->next_offset) {
-                                       /* Save old back ptr in next entry */
-                                       struct arpt_entry *next
-                                               = (void *)e + e->next_offset;
-                                       next->comefrom =
-                                               (void *)back - table_base;
-
-                                       /* set back pointer to next entry */
-                                       back = next;
-                               }
+               const struct arpt_entry_target *t;
+               int hdr_len;
 
-                               e = get_entry(table_base, v);
-                       } else {
-                               /* Targets which reenter must return
-                                * abs. verdicts
-                                */
-                               verdict = t->u.kernel.target->target(skb,
-                                                                    in, out,
-                                                                    hook,
-                                                                    t->u.kernel.target,
-                                                                    t->data);
-
-                               /* Target might have changed stuff. */
-                               arp = arp_hdr(skb);
-
-                               if (verdict == ARPT_CONTINUE)
-                                       e = (void *)e + e->next_offset;
-                               else
-                                       /* Verdict */
+               if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
+                       e = arpt_next_entry(e);
+                       continue;
+               }
+
+               hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
+                       (2 * skb->dev->addr_len);
+               ADD_COUNTER(e->counters, hdr_len, 1);
+
+               t = arpt_get_target_c(e);
+
+               /* Standard target? */
+               if (!t->u.kernel.target->target) {
+                       int v;
+
+                       v = ((struct arpt_standard_target *)t)->verdict;
+                       if (v < 0) {
+                               /* Pop from stack? */
+                               if (v != ARPT_RETURN) {
+                                       verdict = (unsigned)(-v) - 1;
                                        break;
+                               }
+                               e = back;
+                               back = get_entry(table_base, back->comefrom);
+                               continue;
                        }
-               } else {
-                       e = (void *)e + e->next_offset;
+                       if (table_base + v
+                           != arpt_next_entry(e)) {
+                               /* Save old back ptr in next entry */
+                               struct arpt_entry *next = arpt_next_entry(e);
+                               next->comefrom = (void *)back - table_base;
+
+                               /* set back pointer to next entry */
+                               back = next;
+                       }
+
+                       e = get_entry(table_base, v);
+                       continue;
                }
+
+               /* Targets which reenter must return
+                * abs. verdicts
+                */
+               tgpar.target   = t->u.kernel.target;
+               tgpar.targinfo = t->data;
+               verdict = t->u.kernel.target->target(skb, &tgpar);
+
+               /* Target might have changed stuff. */
+               arp = arp_hdr(skb);
+
+               if (verdict == ARPT_CONTINUE)
+                       e = arpt_next_entry(e);
+               else
+                       /* Verdict */
+                       break;
        } while (!hotdrop);
-       read_unlock_bh(&table->lock);
+       xt_info_rdunlock_bh();
 
        if (hotdrop)
                return NF_DROP;
@@ -318,21 +355,17 @@ unsigned int arpt_do_table(struct sk_buff *skb,
 }
 
 /* All zeroes == unconditional rule. */
-static inline int unconditional(const struct arpt_arp *arp)
+static inline bool unconditional(const struct arpt_arp *arp)
 {
-       unsigned int i;
-
-       for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
-               if (((__u32 *)arp)[i])
-                       return 0;
+       static const struct arpt_arp uncond;
 
-       return 1;
+       return memcmp(arp, &uncond, sizeof(uncond)) == 0;
 }
 
 /* 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,
+static int mark_source_chains(const struct xt_table_info *newinfo,
                              unsigned int valid_hooks, void *entry0)
 {
        unsigned int hook;
@@ -353,7 +386,7 @@ static int mark_source_chains(struct xt_table_info *newinfo,
 
                for (;;) {
                        const struct arpt_standard_target *t
-                               = (void *)arpt_get_target(e);
+                               = (void *)arpt_get_target_c(e);
                        int visited = e->comefrom & (1 << hook);
 
                        if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
@@ -365,14 +398,16 @@ static int mark_source_chains(struct xt_table_info *newinfo,
                                |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
 
                        /* Unconditional return/END. */
-                       if ((e->target_offset == sizeof(struct arpt_entry)
-                           && (strcmp(t->target.u.user.name,
-                                      ARPT_STANDARD_TARGET) == 0)
-                           && t->verdict < 0
-                           && unconditional(&e->arp)) || visited) {
+                       if ((e->target_offset == sizeof(struct arpt_entry) &&
+                            (strcmp(t->target.u.user.name,
+                                    ARPT_STANDARD_TARGET) == 0) &&
+                            t->verdict < 0 && unconditional(&e->arp)) ||
+                           visited) {
                                unsigned int oldpos, size;
 
-                               if (t->verdict < -NF_MAX_VERDICT - 1) {
+                               if ((strcmp(t->target.u.user.name,
+                                           ARPT_STANDARD_TARGET) == 0) &&
+                                   t->verdict < -NF_MAX_VERDICT - 1) {
                                        duprintf("mark_source_chains: bad "
                                                "negative verdict (%i)\n",
                                                                t->verdict);
@@ -406,8 +441,8 @@ static int mark_source_chains(struct xt_table_info *newinfo,
                                int newpos = t->verdict;
 
                                if (strcmp(t->target.u.user.name,
-                                          ARPT_STANDARD_TARGET) == 0
-                                   && newpos >= 0) {
+                                          ARPT_STANDARD_TARGET) == 0 &&
+                                   newpos >= 0) {
                                        if (newpos > newinfo->size -
                                                sizeof(struct arpt_entry)) {
                                                duprintf("mark_source_chains: "
@@ -435,7 +470,7 @@ static int mark_source_chains(struct xt_table_info *newinfo,
        return 1;
 }
 
-static inline int check_entry(struct arpt_entry *e, const char *name)
+static inline int check_entry(const struct arpt_entry *e, const char *name)
 {
        const struct arpt_entry_target *t;
 
@@ -447,7 +482,7 @@ static inline int check_entry(struct arpt_entry *e, const char *name)
        if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
                return -EINVAL;
 
-       t = arpt_get_target(e);
+       t = arpt_get_target_c(e);
        if (e->target_offset + t->u.target_size > e->next_offset)
                return -EINVAL;
 
@@ -456,28 +491,28 @@ static inline int check_entry(struct arpt_entry *e, const char *name)
 
 static inline int check_target(struct arpt_entry *e, const char *name)
 {
-       struct arpt_entry_target *t;
-       struct xt_target *target;
+       struct arpt_entry_target *t = arpt_get_target(e);
        int ret;
-
-       t = arpt_get_target(e);
-       target = t->u.kernel.target;
-
-       ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
-                             name, e->comefrom, 0, 0);
-       if (!ret && t->u.kernel.target->checkentry
-           && !t->u.kernel.target->checkentry(name, e, target, t->data,
-                                              e->comefrom)) {
+       struct xt_tgchk_param par = {
+               .table     = name,
+               .entryinfo = e,
+               .target    = t->u.kernel.target,
+               .targinfo  = t->data,
+               .hook_mask = e->comefrom,
+               .family    = NFPROTO_ARP,
+       };
+
+       ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
+       if (ret < 0) {
                duprintf("arp_tables: check failed for `%s'.\n",
                         t->u.kernel.target->name);
-               ret = -EINVAL;
+               return ret;
        }
-       return ret;
+       return 0;
 }
 
 static inline int
-find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
-                unsigned int *i)
+find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
 {
        struct arpt_entry_target *t;
        struct xt_target *target;
@@ -488,7 +523,8 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
                return ret;
 
        t = arpt_get_target(e);
-       target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
+       target = try_then_request_module(xt_find_target(NFPROTO_ARP,
+                                                       t->u.user.name,
                                                        t->u.user.revision),
                                         "arpt_%s", t->u.user.name);
        if (IS_ERR(target) || !target) {
@@ -501,8 +537,6 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
        ret = check_target(e, name);
        if (ret)
                goto err;
-
-       (*i)++;
        return 0;
 err:
        module_put(t->u.kernel.target->me);
@@ -510,18 +544,33 @@ out:
        return ret;
 }
 
+static bool check_underflow(const struct arpt_entry *e)
+{
+       const struct arpt_entry_target *t;
+       unsigned int verdict;
+
+       if (!unconditional(&e->arp))
+               return false;
+       t = arpt_get_target_c(e);
+       if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
+               return false;
+       verdict = ((struct arpt_standard_target *)t)->verdict;
+       verdict = -verdict - 1;
+       return verdict == NF_DROP || verdict == NF_ACCEPT;
+}
+
 static inline int check_entry_size_and_hooks(struct arpt_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 *i)
+                                            unsigned int valid_hooks)
 {
        unsigned int h;
 
-       if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
-           || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
+       if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
+           (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
                duprintf("Bad offset %p\n", e);
                return -EINVAL;
        }
@@ -535,54 +584,53 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,
 
        /* Check hooks & underflows */
        for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
+               if (!(valid_hooks & (1 << h)))
+                       continue;
                if ((unsigned char *)e - base == hook_entries[h])
                        newinfo->hook_entry[h] = hook_entries[h];
-               if ((unsigned char *)e - base == underflows[h])
+               if ((unsigned char *)e - base == underflows[h]) {
+                       if (!check_underflow(e)) {
+                               pr_err("Underflows must be unconditional and "
+                                      "use the STANDARD target with "
+                                      "ACCEPT/DROP\n");
+                               return -EINVAL;
+                       }
                        newinfo->underflow[h] = underflows[h];
+               }
        }
 
-       /* FIXME: underflows must be unconditional, standard verdicts
-          < 0 (not ARPT_RETURN). --RR */
-
        /* Clear counters and comefrom */
        e->counters = ((struct xt_counters) { 0, 0 });
        e->comefrom = 0;
-
-       (*i)++;
        return 0;
 }
 
-static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
+static inline void cleanup_entry(struct arpt_entry *e)
 {
+       struct xt_tgdtor_param par;
        struct arpt_entry_target *t;
 
-       if (i && (*i)-- == 0)
-               return 1;
-
        t = arpt_get_target(e);
-       if (t->u.kernel.target->destroy)
-               t->u.kernel.target->destroy(t->u.kernel.target, t->data);
-       module_put(t->u.kernel.target->me);
-       return 0;
+       par.target   = t->u.kernel.target;
+       par.targinfo = t->data;
+       par.family   = NFPROTO_ARP;
+       if (par.target->destroy != NULL)
+               par.target->destroy(&par);
+       module_put(par.target->me);
 }
 
 /* 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)
+static int translate_table(struct xt_table_info *newinfo, void *entry0,
+                           const struct arpt_replace *repl)
 {
+       struct arpt_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_ARP_NUMHOOKS; i++) {
@@ -594,52 +642,63 @@ static int translate_table(const char *name,
        i = 0;
 
        /* Walk through entries, checking offsets. */
-       ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
-                                check_entry_size_and_hooks,
-                                newinfo,
-                                entry0,
-                                entry0 + size,
-                                hook_entries, underflows, &i);
+       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)
+                       break;
+               ++i;
+       }
        duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
        if (ret != 0)
                return ret;
 
-       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_ARP_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)) {
                duprintf("Looping hook\n");
                return -ELOOP;
        }
 
        /* Finally, each sanity check must pass */
        i = 0;
-       ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
-                                find_check_entry, name, size, &i);
+       xt_entry_foreach(iter, entry0, newinfo->size) {
+               ret = find_check_entry(iter, repl->name, repl->size);
+               if (ret != 0)
+                       break;
+               ++i;
+       }
 
        if (ret != 0) {
-               ARPT_ENTRY_ITERATE(entry0, newinfo->size,
-                               cleanup_entry, &i);
+               xt_entry_foreach(iter, entry0, newinfo->size) {
+                       if (i-- == 0)
+                               break;
+                       cleanup_entry(iter);
+               }
                return ret;
        }
 
@@ -652,30 +711,10 @@ static int translate_table(const char *name,
        return ret;
 }
 
-/* Gets counters. */
-static inline int add_entry_to_counter(const struct arpt_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 arpt_entry *e,
-                                      struct xt_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 arpt_entry *iter;
        unsigned int cpu;
        unsigned int i;
        unsigned int curcpu;
@@ -683,30 +722,36 @@ static void get_counters(const struct xt_table_info *t,
        /* Instead of clearing (by a previous call to memset())
         * the counters and using adds, we set the counters
         * with data used by 'current' CPU
-        * We dont care about preemption here.
+        *
+        * Bottom half has to be disabled to prevent deadlock
+        * if new softirq were to run and call ipt_do_table
         */
-       curcpu = raw_smp_processor_id();
+       local_bh_disable();
+       curcpu = smp_processor_id();
 
        i = 0;
-       ARPT_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;
-               ARPT_ENTRY_ITERATE(t->entries[cpu],
-                                  t->size,
-                                  add_entry_to_counter,
-                                  counters,
-                                  &i);
+               xt_info_wrlock(cpu);
+               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 inline 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;
@@ -722,20 +767,17 @@ static inline struct xt_counters *alloc_counters(struct xt_table *table)
        if (counters == NULL)
                return ERR_PTR(-ENOMEM);
 
-       /* First, sum counters... */
-       write_lock_bh(&table->lock);
        get_counters(private, counters);
-       write_unlock_bh(&table->lock);
 
        return counters;
 }
 
 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 arpt_entry *e;
+       const struct arpt_entry *e;
        struct xt_counters *counters;
        struct xt_table_info *private = table->private;
        int ret = 0;
@@ -755,7 +797,7 @@ static int copy_entries_to_user(unsigned int total_size,
        /* FIXME: use iterator macros --RR */
        /* ... then go back and fix counters and names */
        for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
-               struct arpt_entry_target *t;
+               const struct arpt_entry_target *t;
 
                e = (struct arpt_entry *)(loc_cpu_entry + off);
                if (copy_to_user(userptr + off
@@ -766,7 +808,7 @@ static int copy_entries_to_user(unsigned int total_size,
                        goto free_counters;
                }
 
-               t = arpt_get_target(e);
+               t = arpt_get_target_c(e);
                if (copy_to_user(userptr + off + e->target_offset
                                 + offsetof(struct arpt_entry_target,
                                            u.user.name),
@@ -783,39 +825,39 @@ static int 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;
 
        if (v > 0)
-               v += xt_compat_calc_jump(NF_ARP, v);
+               v += xt_compat_calc_jump(NFPROTO_ARP, v);
        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;
 
        if (cv > 0)
-               cv -= xt_compat_calc_jump(NF_ARP, cv);
+               cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
        return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
 }
 
-static int compat_calc_entry(struct arpt_entry *e,
+static int compat_calc_entry(const struct arpt_entry *e,
                             const struct xt_table_info *info,
-                            void *base, struct xt_table_info *newinfo)
+                            const void *base, struct xt_table_info *newinfo)
 {
-       struct arpt_entry_target *t;
+       const struct arpt_entry_target *t;
        unsigned int entry_offset;
        int off, i, ret;
 
        off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
        entry_offset = (void *)e - base;
 
-       t = arpt_get_target(e);
+       t = arpt_get_target_c(e);
        off += xt_compat_target_offset(t->u.kernel.target);
        newinfo->size -= off;
-       ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
+       ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
        if (ret)
                return ret;
 
@@ -833,7 +875,9 @@ static int compat_calc_entry(struct arpt_entry *e,
 static int compat_table_info(const struct xt_table_info *info,
                             struct xt_table_info *newinfo)
 {
+       struct arpt_entry *iter;
        void *loc_cpu_entry;
+       int ret;
 
        if (!newinfo || !info)
                return -EINVAL;
@@ -842,13 +886,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 ARPT_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[ARPT_TABLE_MAXNAMELEN];
        struct xt_table *t;
@@ -866,19 +914,19 @@ static int get_info(struct net *net, void __user *user, int *len, int compat)
        name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
 #ifdef CONFIG_COMPAT
        if (compat)
-               xt_compat_lock(NF_ARP);
+               xt_compat_lock(NFPROTO_ARP);
 #endif
-       t = try_then_request_module(xt_find_table_lock(net, NF_ARP, name),
+       t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
                                    "arptable_%s", name);
        if (t && !IS_ERR(t)) {
                struct arpt_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(NF_ARP);
+                       xt_compat_flush_offsets(NFPROTO_ARP);
                        private = &tmp;
                }
 #endif
@@ -901,13 +949,13 @@ static int get_info(struct net *net, void __user *user, int *len, int compat)
                ret = t ? PTR_ERR(t) : -ENOENT;
 #ifdef CONFIG_COMPAT
        if (compat)
-               xt_compat_unlock(NF_ARP);
+               xt_compat_unlock(NFPROTO_ARP);
 #endif
        return ret;
 }
 
 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
-                      int *len)
+                      const int *len)
 {
        int ret;
        struct arpt_get_entries get;
@@ -925,7 +973,7 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
                return -EINVAL;
        }
 
-       t = xt_find_table_lock(net, NF_ARP, get.name);
+       t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
        if (t && !IS_ERR(t)) {
                const struct xt_table_info *private = t->private;
 
@@ -958,6 +1006,7 @@ static int __do_replace(struct net *net, const char *name,
        struct xt_table_info *oldinfo;
        struct xt_counters *counters;
        void *loc_cpu_old_entry;
+       struct arpt_entry *iter;
 
        ret = 0;
        counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
@@ -967,7 +1016,7 @@ static int __do_replace(struct net *net, const char *name,
                goto out;
        }
 
-       t = try_then_request_module(xt_find_table_lock(net, NF_ARP, name),
+       t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
                                    "arptable_%s", name);
        if (!t || IS_ERR(t)) {
                ret = t ? PTR_ERR(t) : -ENOENT;
@@ -996,12 +1045,13 @@ static int __do_replace(struct net *net, const char *name,
            (newinfo->number <= oldinfo->initial_entries))
                module_put(t->me);
 
-       /* Get the old counters. */
+       /* Get the old counters, and synchronize with replace */
        get_counters(oldinfo, counters);
+
        /* Decrease module usage counts and free resource */
        loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
-       ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
-                          NULL);
+       xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
+               cleanup_entry(iter);
 
        xt_free_table_info(oldinfo);
        if (copy_to_user(counters_ptr, counters,
@@ -1020,12 +1070,14 @@ static int __do_replace(struct net *net, const char *name,
        return ret;
 }
 
-static int do_replace(struct net *net, void __user *user, unsigned int len)
+static int do_replace(struct net *net, const void __user *user,
+                      unsigned int len)
 {
        int ret;
        struct arpt_replace tmp;
        struct xt_table_info *newinfo;
        void *loc_cpu_entry;
+       struct arpt_entry *iter;
 
        if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
                return -EFAULT;
@@ -1046,9 +1098,7 @@ static int 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(newinfo, loc_cpu_entry, &tmp);
        if (ret != 0)
                goto free_newinfo;
 
@@ -1061,30 +1111,17 @@ static int do_replace(struct net *net, void __user *user, unsigned int len)
        return 0;
 
  free_newinfo_untrans:
-       ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
+               cleanup_entry(iter);
  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 inline int add_counter_to_entry(struct arpt_entry *e,
-                                      const struct xt_counters addme[],
-                                      unsigned int *i)
+static int do_add_counters(struct net *net, const void __user *user,
+                          unsigned int len, int compat)
 {
-
-       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,
-                          int compat)
-{
-       unsigned int i;
+       unsigned int i, curcpu;
        struct xt_counters_info tmp;
        struct xt_counters *paddc;
        unsigned int num_counters;
@@ -1095,6 +1132,7 @@ static int do_add_counters(struct net *net, void __user *user, unsigned int len,
        const struct xt_table_info *private;
        int ret = 0;
        void *loc_cpu_entry;
+       struct arpt_entry *iter;
 #ifdef CONFIG_COMPAT
        struct compat_xt_counters_info compat_tmp;
 
@@ -1134,13 +1172,13 @@ static int do_add_counters(struct net *net, void __user *user, unsigned int len,
                goto free;
        }
 
-       t = xt_find_table_lock(net, NF_ARP, name);
+       t = xt_find_table_lock(net, NFPROTO_ARP, name);
        if (!t || IS_ERR(t)) {
                ret = t ? PTR_ERR(t) : -ENOENT;
                goto free;
        }
 
-       write_lock_bh(&t->lock);
+       local_bh_disable();
        private = t->private;
        if (private->number != num_counters) {
                ret = -EINVAL;
@@ -1149,14 +1187,16 @@ static int do_add_counters(struct net *net, void __user *user, unsigned int len,
 
        i = 0;
        /* Choose the copy that is on our node */
-       loc_cpu_entry = private->entries[smp_processor_id()];
-       ARPT_ENTRY_ITERATE(loc_cpu_entry,
-                          private->size,
-                          add_counter_to_entry,
-                          paddc,
-                          &i);
+       curcpu = smp_processor_id();
+       loc_cpu_entry = private->entries[curcpu];
+       xt_info_wrlock(curcpu);
+       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:
-       write_unlock_bh(&t->lock);
+       local_bh_enable();
        xt_table_unlock(t);
        module_put(t->me);
  free:
@@ -1166,28 +1206,22 @@ static int do_add_counters(struct net *net, void __user *user, unsigned int len,
 }
 
 #ifdef CONFIG_COMPAT
-static inline int
-compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
+static inline void compat_release_entry(struct compat_arpt_entry *e)
 {
        struct arpt_entry_target *t;
 
-       if (i && (*i)-- == 0)
-               return 1;
-
        t = compat_arpt_get_target(e);
        module_put(t->u.kernel.target->me);
-       return 0;
 }
 
 static inline int
 check_compat_entry_size_and_hooks(struct compat_arpt_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 arpt_entry_target *t;
@@ -1196,8 +1230,8 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
        int ret, off, h;
 
        duprintf("check_compat_entry_size_and_hooks %p\n", e);
-       if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
-           || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
+       if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
+           (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
                duprintf("Bad offset %p, limit = %p\n", e, limit);
                return -EINVAL;
        }
@@ -1218,7 +1252,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
        entry_offset = (void *)e - (void *)base;
 
        t = compat_arpt_get_target(e);
-       target = try_then_request_module(xt_find_target(NF_ARP,
+       target = try_then_request_module(xt_find_target(NFPROTO_ARP,
                                                        t->u.user.name,
                                                        t->u.user.revision),
                                         "arpt_%s", t->u.user.name);
@@ -1232,7 +1266,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
 
        off += xt_compat_target_offset(target);
        *size += off;
-       ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
+       ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
        if (ret)
                goto release_target;
 
@@ -1247,8 +1281,6 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
        /* Clear counters and comefrom */
        memset(&e->counters, 0, sizeof(e->counters));
        e->comefrom = 0;
-
-       (*i)++;
        return 0;
 
 release_target:
@@ -1292,19 +1324,6 @@ compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
        return ret;
 }
 
-static inline int compat_check_entry(struct arpt_entry *e, const char *name,
-                                    unsigned int *i)
-{
-       int ret;
-
-       ret = check_target(e, name);
-       if (ret)
-               return ret;
-
-       (*i)++;
-       return 0;
-}
-
 static int translate_compat_table(const char *name,
                                  unsigned int valid_hooks,
                                  struct xt_table_info **pinfo,
@@ -1317,8 +1336,10 @@ static int translate_compat_table(const char *name,
        unsigned int i, j;
        struct xt_table_info *newinfo, *info;
        void *pos, *entry0, *entry1;
+       struct compat_arpt_entry *iter0;
+       struct arpt_entry *iter1;
        unsigned int size;
-       int ret;
+       int ret = 0;
 
        info = *pinfo;
        entry0 = *pentry0;
@@ -1333,15 +1354,19 @@ static int translate_compat_table(const char *name,
 
        duprintf("translate_compat_table: size %u\n", info->size);
        j = 0;
-       xt_compat_lock(NF_ARP);
+       xt_compat_lock(NFPROTO_ARP);
        /* Walk through entries, checking offsets. */
-       ret = COMPAT_ARPT_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) {
@@ -1380,11 +1405,14 @@ static int translate_compat_table(const char *name,
        entry1 = newinfo->entries[raw_smp_processor_id()];
        pos = entry1;
        size = total_size;
-       ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
-                                       compat_copy_entry_from_user,
-                                       &pos, &size, name, newinfo, entry1);
-       xt_compat_flush_offsets(NF_ARP);
-       xt_compat_unlock(NF_ARP);
+       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(NFPROTO_ARP);
+       xt_compat_unlock(NFPROTO_ARP);
        if (ret)
                goto free_newinfo;
 
@@ -1393,13 +1421,32 @@ static int translate_compat_table(const char *name,
                goto free_newinfo;
 
        i = 0;
-       ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
-                                name, &i);
+       xt_entry_foreach(iter1, entry1, newinfo->size) {
+               ret = check_target(iter1, 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_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
-                                                  compat_release_entry, &j);
-               ARPT_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);
+               }
                xt_free_table_info(newinfo);
                return ret;
        }
@@ -1417,11 +1464,15 @@ static int translate_compat_table(const char *name,
 free_newinfo:
        xt_free_table_info(newinfo);
 out:
-       COMPAT_ARPT_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(NF_ARP);
-       xt_compat_unlock(NF_ARP);
+       xt_compat_flush_offsets(NFPROTO_ARP);
+       xt_compat_unlock(NFPROTO_ARP);
        goto out;
 }
 
@@ -1444,6 +1495,7 @@ static int compat_do_replace(struct net *net, void __user *user,
        struct compat_arpt_replace tmp;
        struct xt_table_info *newinfo;
        void *loc_cpu_entry;
+       struct arpt_entry *iter;
 
        if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
                return -EFAULT;
@@ -1481,7 +1533,8 @@ static int compat_do_replace(struct net *net, void __user *user,
        return 0;
 
  free_newinfo_untrans:
-       ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
+               cleanup_entry(iter);
  free_newinfo:
        xt_free_table_info(newinfo);
        return ret;
@@ -1515,7 +1568,7 @@ static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
                                     compat_uint_t *size,
                                     struct xt_counters *counters,
-                                    unsigned int *i)
+                                    unsigned int i)
 {
        struct arpt_entry_target *t;
        struct compat_arpt_entry __user *ce;
@@ -1523,14 +1576,12 @@ static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
        compat_uint_t origsize;
        int ret;
 
-       ret = -EFAULT;
        origsize = *size;
        ce = (struct compat_arpt_entry __user *)*dstptr;
-       if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
-               goto out;
-
-       if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
-               goto out;
+       if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
+           copy_to_user(&ce->counters, &counters[i],
+           sizeof(counters[i])) != 0)
+               return -EFAULT;
 
        *dstptr += sizeof(struct compat_arpt_entry);
        *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
@@ -1540,18 +1591,12 @@ static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
        t = arpt_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 compat_copy_entries_to_user(unsigned int total_size,
@@ -1565,6 +1610,7 @@ static int compat_copy_entries_to_user(unsigned int total_size,
        int ret = 0;
        void *loc_cpu_entry;
        unsigned int i = 0;
+       struct arpt_entry *iter;
 
        counters = alloc_counters(table);
        if (IS_ERR(counters))
@@ -1574,9 +1620,12 @@ static int compat_copy_entries_to_user(unsigned int total_size,
        loc_cpu_entry = private->entries[raw_smp_processor_id()];
        pos = userptr;
        size = total_size;
-       ret = ARPT_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;
 }
@@ -1607,8 +1656,8 @@ static int compat_get_entries(struct net *net,
                return -EINVAL;
        }
 
-       xt_compat_lock(NF_ARP);
-       t = xt_find_table_lock(net, NF_ARP, get.name);
+       xt_compat_lock(NFPROTO_ARP);
+       t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
        if (t && !IS_ERR(t)) {
                const struct xt_table_info *private = t->private;
                struct xt_table_info info;
@@ -1623,13 +1672,13 @@ static int compat_get_entries(struct net *net,
                                 private->size, get.size);
                        ret = -EAGAIN;
                }
-               xt_compat_flush_offsets(NF_ARP);
+               xt_compat_flush_offsets(NFPROTO_ARP);
                module_put(t->me);
                xt_table_unlock(t);
        } else
                ret = t ? PTR_ERR(t) : -ENOENT;
 
-       xt_compat_unlock(NF_ARP);
+       xt_compat_unlock(NFPROTO_ARP);
        return ret;
 }
 
@@ -1709,7 +1758,7 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
                        break;
                }
 
-               try_then_request_module(xt_find_revision(NF_ARP, rev.name,
+               try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
                                                         rev.revision, 1, &ret),
                                        "arpt_%s", rev.name);
                break;
@@ -1723,7 +1772,8 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
        return ret;
 }
 
-struct xt_table *arpt_register_table(struct net *net, struct xt_table *table,
+struct xt_table *arpt_register_table(struct net *net,
+                                    const struct xt_table *table,
                                     const struct arpt_replace *repl)
 {
        int ret;
@@ -1743,12 +1793,7 @@ struct xt_table *arpt_register_table(struct net *net, struct xt_table *table,
        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(newinfo, loc_cpu_entry, repl);
        duprintf("arpt_register_table: translate table gives %d\n", ret);
        if (ret != 0)
                goto out_free;
@@ -1771,13 +1816,14 @@ void arpt_unregister_table(struct xt_table *table)
        struct xt_table_info *private;
        void *loc_cpu_entry;
        struct module *table_owner = table->me;
+       struct arpt_entry *iter;
 
        private = xt_unregister_table(table);
 
        /* Decrease module usage counts and free resources */
        loc_cpu_entry = private->entries[raw_smp_processor_id()];
-       ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
-                          cleanup_entry, NULL);
+       xt_entry_foreach(iter, loc_cpu_entry, private->size)
+               cleanup_entry(iter);
        if (private->number > private->initial_entries)
                module_put(table_owner);
        xt_free_table_info(private);
@@ -1787,7 +1833,7 @@ void arpt_unregister_table(struct xt_table *table)
 static struct xt_target arpt_standard_target __read_mostly = {
        .name           = ARPT_STANDARD_TARGET,
        .targetsize     = sizeof(int),
-       .family         = NF_ARP,
+       .family         = NFPROTO_ARP,
 #ifdef CONFIG_COMPAT
        .compatsize     = sizeof(compat_int_t),
        .compat_from_user = compat_standard_from_user,
@@ -1799,7 +1845,7 @@ static struct xt_target arpt_error_target __read_mostly = {
        .name           = ARPT_ERROR_TARGET,
        .target         = arpt_error,
        .targetsize     = ARPT_FUNCTION_MAXNAMELEN,
-       .family         = NF_ARP,
+       .family         = NFPROTO_ARP,
 };
 
 static struct nf_sockopt_ops arpt_sockopts = {
@@ -1821,12 +1867,12 @@ static struct nf_sockopt_ops arpt_sockopts = {
 
 static int __net_init arp_tables_net_init(struct net *net)
 {
-       return xt_proto_init(net, NF_ARP);
+       return xt_proto_init(net, NFPROTO_ARP);
 }
 
 static void __net_exit arp_tables_net_exit(struct net *net)
 {
-       xt_proto_fini(net, NF_ARP);
+       xt_proto_fini(net, NFPROTO_ARP);
 }
 
 static struct pernet_operations arp_tables_net_ops = {