[NETFILTER]: Remove changelogs and CVS IDs
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / ip_tables.c
index 8ce5b6f..e3f83bf 100644 (file)
@@ -7,12 +7,6 @@
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
- *
- * 19 Jan 2002 Harald Welte <laforge@gnumonks.org>
- *     - increase module usage count as soon as we have rules inside
- *       a table
- * 08 Oct 2005 Harald Welte <lafore@netfilter.org>
- *     - Generalize into "x_tables" layer and "{ip,ip6,arp}_tables"
  */
 #include <linux/cache.h>
 #include <linux/capability.h>
@@ -198,7 +192,7 @@ int do_match(struct ipt_entry_match *m,
 {
        /* Stop iteration if it doesn't match */
        if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,
-                                     offset, skb->nh.iph->ihl*4, hotdrop))
+                                     offset, ip_hdrlen(skb), hotdrop))
                return 1;
        else
                return 0;
@@ -216,7 +210,7 @@ ipt_do_table(struct sk_buff **pskb,
             unsigned int hook,
             const struct net_device *in,
             const struct net_device *out,
-            struct ipt_table *table)
+            struct xt_table *table)
 {
        static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
        u_int16_t offset;
@@ -231,7 +225,7 @@ ipt_do_table(struct sk_buff **pskb,
        struct xt_table_info *private;
 
        /* Initialization */
-       ip = (*pskb)->nh.iph;
+       ip = ip_hdr(*pskb);
        datalen = (*pskb)->len - ip->ihl * 4;
        indev = in ? in->name : nulldevname;
        outdev = out ? out->name : nulldevname;
@@ -297,7 +291,7 @@ ipt_do_table(struct sk_buff **pskb,
                                e = get_entry(table_base, v);
                        } else {
                                /* Targets which reenter must return
-                                   abs. verdicts */
+                                  abs. verdicts */
 #ifdef CONFIG_NETFILTER_DEBUG
                                ((struct ipt_entry *)table_base)->comefrom
                                        = 0xeeeeeeec;
@@ -320,7 +314,7 @@ ipt_do_table(struct sk_buff **pskb,
                                        = 0x57acc001;
 #endif
                                /* Target might have changed stuff. */
-                               ip = (*pskb)->nh.iph;
+                               ip = ip_hdr(*pskb);
                                datalen = (*pskb)->len - ip->ihl * 4;
 
                                if (verdict == IPT_CONTINUE)
@@ -384,6 +378,7 @@ mark_source_chains(struct xt_table_info *newinfo,
                for (;;) {
                        struct ipt_standard_target *t
                                = (void *)ipt_get_target(e);
+                       int visited = e->comefrom & (1 << hook);
 
                        if (e->comefrom & (1 << NF_IP_NUMHOOKS)) {
                                printk("iptables: loop hook %u pos %u %08X.\n",
@@ -394,13 +389,20 @@ mark_source_chains(struct xt_table_info *newinfo,
                                |= ((1 << hook) | (1 << NF_IP_NUMHOOKS));
 
                        /* Unconditional return/END. */
-                       if (e->target_offset == sizeof(struct ipt_entry)
+                       if ((e->target_offset == sizeof(struct ipt_entry)
                            && (strcmp(t->target.u.user.name,
                                       IPT_STANDARD_TARGET) == 0)
                            && t->verdict < 0
-                           && unconditional(&e->ip)) {
+                           && unconditional(&e->ip)) || visited) {
                                unsigned int oldpos, size;
 
+                               if (t->verdict < -NF_MAX_VERDICT - 1) {
+                                       duprintf("mark_source_chains: bad "
+                                               "negative verdict (%i)\n",
+                                                               t->verdict);
+                                       return 0;
+                               }
+
                                /* Return: backtrack through the last
                                   big jump. */
                                do {
@@ -438,6 +440,13 @@ mark_source_chains(struct xt_table_info *newinfo,
                                if (strcmp(t->target.u.user.name,
                                           IPT_STANDARD_TARGET) == 0
                                    && newpos >= 0) {
+                                       if (newpos > newinfo->size -
+                                               sizeof(struct ipt_entry)) {
+                                               duprintf("mark_source_chains: "
+                                                       "bad verdict (%i)\n",
+                                                               newpos);
+                                               return 0;
+                                       }
                                        /* This a jump; chase it. */
                                        duprintf("Jump rule %u -> %u\n",
                                                 pos, newpos);
@@ -464,68 +473,74 @@ cleanup_match(struct ipt_entry_match *m, unsigned int *i)
                return 1;
 
        if (m->u.kernel.match->destroy)
-               m->u.kernel.match->destroy(m->u.kernel.match, m->data,
-                                          m->u.match_size - sizeof(*m));
+               m->u.kernel.match->destroy(m->u.kernel.match, m->data);
        module_put(m->u.kernel.match->me);
        return 0;
 }
 
 static inline int
-standard_check(const struct ipt_entry_target *t,
-              unsigned int max_offset)
+check_entry(struct ipt_entry *e, const char *name)
 {
-       struct ipt_standard_target *targ = (void *)t;
+       struct ipt_entry_target *t;
 
-       /* Check standard info. */
-       if (targ->verdict >= 0
-           && targ->verdict > max_offset - sizeof(struct ipt_entry)) {
-               duprintf("ipt_standard_check: bad verdict (%i)\n",
-                        targ->verdict);
-               return 0;
+       if (!ip_checkentry(&e->ip)) {
+               duprintf("ip_tables: ip check failed %p %s.\n", e, name);
+               return -EINVAL;
        }
-       if (targ->verdict < -NF_MAX_VERDICT - 1) {
-               duprintf("ipt_standard_check: bad negative verdict (%i)\n",
-                        targ->verdict);
-               return 0;
+
+       if (e->target_offset + sizeof(struct ipt_entry_target) > e->next_offset)
+               return -EINVAL;
+
+       t = ipt_get_target(e);
+       if (e->target_offset + t->u.target_size > e->next_offset)
+               return -EINVAL;
+
+       return 0;
+}
+
+static inline int check_match(struct ipt_entry_match *m, const char *name,
+                               const struct ipt_ip *ip, unsigned int hookmask)
+{
+       struct xt_match *match;
+       int ret;
+
+       match = m->u.kernel.match;
+       ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
+                            name, hookmask, ip->proto,
+                            ip->invflags & IPT_INV_PROTO);
+       if (!ret && m->u.kernel.match->checkentry
+           && !m->u.kernel.match->checkentry(name, ip, match, m->data,
+                                             hookmask)) {
+               duprintf("ip_tables: check failed for `%s'.\n",
+                        m->u.kernel.match->name);
+               ret = -EINVAL;
        }
-       return 1;
+       return ret;
 }
 
 static inline int
-check_match(struct ipt_entry_match *m,
+find_check_match(struct ipt_entry_match *m,
            const char *name,
            const struct ipt_ip *ip,
            unsigned int hookmask,
            unsigned int *i)
 {
-       struct ipt_match *match;
+       struct xt_match *match;
        int ret;
 
        match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
                                                   m->u.user.revision),
                                        "ipt_%s", m->u.user.name);
        if (IS_ERR(match) || !match) {
-               duprintf("check_match: `%s' not found\n", m->u.user.name);
+               duprintf("find_check_match: `%s' not found\n", m->u.user.name);
                return match ? PTR_ERR(match) : -ENOENT;
        }
        m->u.kernel.match = match;
 
-       ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
-                            name, hookmask, ip->proto,
-                            ip->invflags & IPT_INV_PROTO);
+       ret = check_match(m, name, ip, hookmask);
        if (ret)
                goto err;
 
-       if (m->u.kernel.match->checkentry
-           && !m->u.kernel.match->checkentry(name, ip, match, m->data,
-                                             m->u.match_size - sizeof(*m),
-                                             hookmask)) {
-               duprintf("ip_tables: check failed for `%s'.\n",
-                        m->u.kernel.match->name);
-               ret = -EINVAL;
-               goto err;
-       }
-
        (*i)++;
        return 0;
 err:
@@ -533,24 +548,43 @@ err:
        return ret;
 }
 
-static struct ipt_target ipt_standard_target;
+static inline int check_target(struct ipt_entry *e, const char *name)
+{
+       struct ipt_entry_target *t;
+       struct xt_target *target;
+       int ret;
+
+       t = ipt_get_target(e);
+       target = t->u.kernel.target;
+       ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
+                             name, e->comefrom, e->ip.proto,
+                             e->ip.invflags & IPT_INV_PROTO);
+       if (!ret && t->u.kernel.target->checkentry
+                  && !t->u.kernel.target->checkentry(name, e, target,
+                                                     t->data, e->comefrom)) {
+               duprintf("ip_tables: check failed for `%s'.\n",
+                        t->u.kernel.target->name);
+               ret = -EINVAL;
+       }
+       return ret;
+}
 
 static inline int
-check_entry(struct ipt_entry *e, const char *name, unsigned int size,
+find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
            unsigned int *i)
 {
        struct ipt_entry_target *t;
-       struct ipt_target *target;
+       struct xt_target *target;
        int ret;
        unsigned int j;
 
-       if (!ip_checkentry(&e->ip)) {
-               duprintf("ip_tables: ip check failed %p %s.\n", e, name);
-               return -EINVAL;
-       }
+       ret = check_entry(e, name);
+       if (ret)
+               return ret;
 
        j = 0;
-       ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip, e->comefrom, &j);
+       ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
+                                                       e->comefrom, &j);
        if (ret != 0)
                goto cleanup_matches;
 
@@ -560,34 +594,16 @@ check_entry(struct ipt_entry *e, const char *name, unsigned int size,
                                                     t->u.user.revision),
                                         "ipt_%s", t->u.user.name);
        if (IS_ERR(target) || !target) {
-               duprintf("check_entry: `%s' not found\n", t->u.user.name);
+               duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
                ret = target ? PTR_ERR(target) : -ENOENT;
                goto cleanup_matches;
        }
        t->u.kernel.target = target;
 
-       ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
-                             name, e->comefrom, e->ip.proto,
-                             e->ip.invflags & IPT_INV_PROTO);
+       ret = check_target(e, name);
        if (ret)
                goto err;
 
-       if (t->u.kernel.target == &ipt_standard_target) {
-               if (!standard_check(t, size)) {
-                       ret = -EINVAL;
-                       goto cleanup_matches;
-               }
-       } else if (t->u.kernel.target->checkentry
-                  && !t->u.kernel.target->checkentry(name, e, target, t->data,
-                                                     t->u.target_size
-                                                     - sizeof(*t),
-                                                     e->comefrom)) {
-               duprintf("ip_tables: check failed for `%s'.\n",
-                        t->u.kernel.target->name);
-               ret = -EINVAL;
-               goto err;
-       }
-
        (*i)++;
        return 0;
  err:
@@ -630,7 +646,7 @@ check_entry_size_and_hooks(struct ipt_entry *e,
        }
 
        /* FIXME: underflows must be unconditional, standard verdicts
-           < 0 (not IPT_RETURN). --RR */
+          < 0 (not IPT_RETURN). --RR */
 
        /* Clear counters and comefrom */
        e->counters = ((struct xt_counters) { 0, 0 });
@@ -652,8 +668,7 @@ cleanup_entry(struct ipt_entry *e, unsigned int *i)
        IPT_MATCH_ITERATE(e, cleanup_match, NULL);
        t = ipt_get_target(e);
        if (t->u.kernel.target->destroy)
-               t->u.kernel.target->destroy(t->u.kernel.target, t->data,
-                                           t->u.target_size - sizeof(*t));
+               t->u.kernel.target->destroy(t->u.kernel.target, t->data);
        module_put(t->u.kernel.target->me);
        return 0;
 }
@@ -723,11 +738,11 @@ translate_table(const char *name,
        /* Finally, each sanity check must pass */
        i = 0;
        ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
-                               check_entry, name, size, &i);
+                               find_check_entry, name, size, &i);
 
        if (ret != 0) {
                IPT_ENTRY_ITERATE(entry0, newinfo->size,
-                                 cleanup_entry, &i);
+                               cleanup_entry, &i);
                return ret;
        }
 
@@ -797,7 +812,7 @@ get_counters(const struct xt_table_info *t,
        }
 }
 
-static inline struct xt_counters * alloc_counters(struct ipt_table *table)
+static inline struct xt_counters * alloc_counters(struct xt_table *table)
 {
        unsigned int countersize;
        struct xt_counters *counters;
@@ -822,7 +837,7 @@ static inline struct xt_counters * alloc_counters(struct ipt_table *table)
 
 static int
 copy_entries_to_user(unsigned int total_size,
-                    struct ipt_table *table,
+                    struct xt_table *table,
                     void __user *userptr)
 {
        unsigned int off, num;
@@ -898,13 +913,13 @@ copy_entries_to_user(unsigned int total_size,
 #ifdef CONFIG_COMPAT
 struct compat_delta {
        struct compat_delta *next;
-       u_int16_t offset;
+       unsigned int offset;
        short delta;
 };
 
 static struct compat_delta *compat_offsets = NULL;
 
-static int compat_add_offset(u_int16_t offset, short delta)
+static int compat_add_offset(unsigned int offset, short delta)
 {
        struct compat_delta *tmp;
 
@@ -936,7 +951,7 @@ static void compat_flush_offsets(void)
        }
 }
 
-static short compat_calc_jump(u_int16_t offset)
+static short compat_calc_jump(unsigned int offset)
 {
        struct compat_delta *tmp;
        short delta;
@@ -947,73 +962,28 @@ static short compat_calc_jump(u_int16_t offset)
        return delta;
 }
 
-struct compat_ipt_standard_target
+static void compat_standard_from_user(void *dst, void *src)
 {
-       struct compat_xt_entry_target target;
-       compat_int_t verdict;
-};
-
-struct compat_ipt_standard
-{
-       struct compat_ipt_entry entry;
-       struct compat_ipt_standard_target target;
-};
+       int v = *(compat_int_t *)src;
 
-#define IPT_ST_LEN             XT_ALIGN(sizeof(struct ipt_standard_target))
-#define IPT_ST_COMPAT_LEN      COMPAT_XT_ALIGN(sizeof(struct compat_ipt_standard_target))
-#define IPT_ST_OFFSET          (IPT_ST_LEN - IPT_ST_COMPAT_LEN)
+       if (v > 0)
+               v += compat_calc_jump(v);
+       memcpy(dst, &v, sizeof(v));
+}
 
-static int compat_ipt_standard_fn(void *target,
-               void **dstptr, int *size, int convert)
+static int compat_standard_to_user(void __user *dst, void *src)
 {
-       struct compat_ipt_standard_target compat_st, *pcompat_st;
-       struct ipt_standard_target st, *pst;
-       int ret;
+       compat_int_t cv = *(int *)src;
 
-       ret = 0;
-       switch (convert) {
-               case COMPAT_TO_USER:
-                       pst = target;
-                       memcpy(&compat_st.target, &pst->target,
-                               sizeof(compat_st.target));
-                       compat_st.verdict = pst->verdict;
-                       if (compat_st.verdict > 0)
-                               compat_st.verdict -=
-                                       compat_calc_jump(compat_st.verdict);
-                       compat_st.target.u.user.target_size = IPT_ST_COMPAT_LEN;
-                       if (copy_to_user(*dstptr, &compat_st, IPT_ST_COMPAT_LEN))
-                               ret = -EFAULT;
-                       *size -= IPT_ST_OFFSET;
-                       *dstptr += IPT_ST_COMPAT_LEN;
-                       break;
-               case COMPAT_FROM_USER:
-                       pcompat_st = target;
-                       memcpy(&st.target, &pcompat_st->target, IPT_ST_COMPAT_LEN);
-                       st.verdict = pcompat_st->verdict;
-                       if (st.verdict > 0)
-                               st.verdict += compat_calc_jump(st.verdict);
-                       st.target.u.user.target_size = IPT_ST_LEN;
-                       memcpy(*dstptr, &st, IPT_ST_LEN);
-                       *size += IPT_ST_OFFSET;
-                       *dstptr += IPT_ST_LEN;
-                       break;
-               case COMPAT_CALC_SIZE:
-                       *size += IPT_ST_OFFSET;
-                       break;
-               default:
-                       ret = -ENOPROTOOPT;
-                       break;
-       }
-       return ret;
+       if (cv > 0)
+               cv -= compat_calc_jump(cv);
+       return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
 }
 
 static inline int
 compat_calc_match(struct ipt_entry_match *m, int * size)
 {
-       if (m->u.kernel.match->compat)
-               m->u.kernel.match->compat(m, NULL, size, COMPAT_CALC_SIZE);
-       else
-               xt_compat_match(m, NULL, size, COMPAT_CALC_SIZE);
+       *size += xt_compat_match_offset(m->u.kernel.match);
        return 0;
 }
 
@@ -1021,17 +991,14 @@ static int compat_calc_entry(struct ipt_entry *e, struct xt_table_info *info,
                void *base, struct xt_table_info *newinfo)
 {
        struct ipt_entry_target *t;
-       u_int16_t entry_offset;
+       unsigned int entry_offset;
        int off, i, ret;
 
        off = 0;
        entry_offset = (void *)e - base;
        IPT_MATCH_ITERATE(e, compat_calc_match, &off);
        t = ipt_get_target(e);
-       if (t->u.kernel.target->compat)
-               t->u.kernel.target->compat(t, NULL, &off, COMPAT_CALC_SIZE);
-       else
-               xt_compat_target(t, NULL, &off, COMPAT_CALC_SIZE);
+       off += xt_compat_target_offset(t->u.kernel.target);
        newinfo->size -= off;
        ret = compat_add_offset(entry_offset, off);
        if (ret)
@@ -1073,7 +1040,7 @@ static int compat_table_info(struct xt_table_info *info,
 static int get_info(void __user *user, int *len, int compat)
 {
        char name[IPT_TABLE_MAXNAMELEN];
-       struct ipt_table *t;
+       struct xt_table *t;
        int ret;
 
        if (*len != sizeof(struct ipt_getinfo)) {
@@ -1134,7 +1101,7 @@ get_entries(struct ipt_get_entries __user *uptr, int *len)
 {
        int ret;
        struct ipt_get_entries get;
-       struct ipt_table *t;
+       struct xt_table *t;
 
        if (*len < sizeof(get)) {
                duprintf("get_entries: %u < %d\n", *len,
@@ -1178,7 +1145,7 @@ __do_replace(const char *name, unsigned int valid_hooks,
                void __user *counters_ptr)
 {
        int ret;
-       struct ipt_table *t;
+       struct xt_table *t;
        struct xt_table_info *oldinfo;
        struct xt_counters *counters;
        void *loc_cpu_old_entry;
@@ -1329,7 +1296,7 @@ do_add_counters(void __user *user, unsigned int len, int compat)
        char *name;
        int size;
        void *ptmp;
-       struct ipt_table *t;
+       struct xt_table *t;
        struct xt_table_info *private;
        int ret = 0;
        void *loc_cpu_entry;
@@ -1419,17 +1386,13 @@ struct compat_ipt_replace {
 static inline int compat_copy_match_to_user(struct ipt_entry_match *m,
                void __user **dstptr, compat_uint_t *size)
 {
-       if (m->u.kernel.match->compat)
-               return m->u.kernel.match->compat(m, dstptr, size,
-                               COMPAT_TO_USER);
-       else
-               return xt_compat_match(m, dstptr, size, COMPAT_TO_USER);
+       return xt_compat_match_to_user(m, dstptr, size);
 }
 
 static int compat_copy_entry_to_user(struct ipt_entry *e,
                void __user **dstptr, compat_uint_t *size)
 {
-       struct ipt_entry_target __user *t;
+       struct ipt_entry_target *t;
        struct compat_ipt_entry __user *ce;
        u_int16_t target_offset, next_offset;
        compat_uint_t origsize;
@@ -1447,11 +1410,7 @@ static int compat_copy_entry_to_user(struct ipt_entry *e,
        if (ret)
                goto out;
        t = ipt_get_target(e);
-       if (t->u.kernel.target->compat)
-               ret = t->u.kernel.target->compat(t, dstptr, size,
-                               COMPAT_TO_USER);
-       else
-               ret = xt_compat_target(t, dstptr, size, COMPAT_TO_USER);
+       ret = xt_compat_target_to_user(t, dstptr, size);
        if (ret)
                goto out;
        ret = -EFAULT;
@@ -1472,7 +1431,7 @@ compat_check_calc_match(struct ipt_entry_match *m,
            unsigned int hookmask,
            int *size, int *i)
 {
-       struct ipt_match *match;
+       struct xt_match *match;
 
        match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
                                                   m->u.user.revision),
@@ -1483,11 +1442,7 @@ compat_check_calc_match(struct ipt_entry_match *m,
                return match ? PTR_ERR(match) : -ENOENT;
        }
        m->u.kernel.match = match;
-
-       if (m->u.kernel.match->compat)
-               m->u.kernel.match->compat(m, NULL, size, COMPAT_CALC_SIZE);
-       else
-               xt_compat_match(m, NULL, size, COMPAT_CALC_SIZE);
+       *size += xt_compat_match_offset(match);
 
        (*i)++;
        return 0;
@@ -1505,8 +1460,8 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
                           const char *name)
 {
        struct ipt_entry_target *t;
-       struct ipt_target *target;
-       u_int16_t entry_offset;
+       struct xt_target *target;
+       unsigned int entry_offset;
        int ret, off, h, j;
 
        duprintf("check_compat_entry_size_and_hooks %p\n", e);
@@ -1523,10 +1478,9 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
                return -EINVAL;
        }
 
-       if (!ip_checkentry(&e->ip)) {
-               duprintf("ip_tables: ip check failed %p %s.\n", e, name);
-               return -EINVAL;
-       }
+       ret = check_entry(e, name);
+       if (ret)
+               return ret;
 
        off = 0;
        entry_offset = (void *)e - (void *)base;
@@ -1534,7 +1488,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
        ret = IPT_MATCH_ITERATE(e, compat_check_calc_match, name, &e->ip,
                        e->comefrom, &off, &j);
        if (ret != 0)
-               goto out;
+               goto cleanup_matches;
 
        t = ipt_get_target(e);
        target = try_then_request_module(xt_find_target(AF_INET,
@@ -1542,16 +1496,14 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
                                                     t->u.user.revision),
                                         "ipt_%s", t->u.user.name);
        if (IS_ERR(target) || !target) {
-               duprintf("check_entry: `%s' not found\n", t->u.user.name);
+               duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
+                                                       t->u.user.name);
                ret = target ? PTR_ERR(target) : -ENOENT;
-               goto out;
+               goto cleanup_matches;
        }
        t->u.kernel.target = target;
 
-       if (t->u.kernel.target->compat)
-               t->u.kernel.target->compat(t, NULL, &off, COMPAT_CALC_SIZE);
-       else
-               xt_compat_target(t, NULL, &off, COMPAT_CALC_SIZE);
+       off += xt_compat_target_offset(target);
        *size += off;
        ret = compat_add_offset(entry_offset, off);
        if (ret)
@@ -1571,7 +1523,10 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
 
        (*i)++;
        return 0;
+
 out:
+       module_put(t->u.kernel.target->me);
+cleanup_matches:
        IPT_MATCH_ITERATE(e, cleanup_match, &j);
        return ret;
 }
@@ -1580,31 +1535,7 @@ static inline int compat_copy_match_from_user(struct ipt_entry_match *m,
        void **dstptr, compat_uint_t *size, const char *name,
        const struct ipt_ip *ip, unsigned int hookmask)
 {
-       struct ipt_entry_match *dm;
-       struct ipt_match *match;
-       int ret;
-
-       dm = (struct ipt_entry_match *)*dstptr;
-       match = m->u.kernel.match;
-       if (match->compat)
-               match->compat(m, dstptr, size, COMPAT_FROM_USER);
-       else
-               xt_compat_match(m, dstptr, size, COMPAT_FROM_USER);
-
-       ret = xt_check_match(match, AF_INET, dm->u.match_size - sizeof(*dm),
-                            name, hookmask, ip->proto,
-                            ip->invflags & IPT_INV_PROTO);
-       if (ret)
-               return ret;
-
-       if (m->u.kernel.match->checkentry
-           && !m->u.kernel.match->checkentry(name, ip, match, dm->data,
-                                             dm->u.match_size - sizeof(*dm),
-                                             hookmask)) {
-               duprintf("ip_tables: check failed for `%s'.\n",
-                        m->u.kernel.match->name);
-               return -EINVAL;
-       }
+       xt_compat_match_from_user(m, dstptr, size);
        return 0;
 }
 
@@ -1613,7 +1544,7 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
        struct xt_table_info *newinfo, unsigned char *base)
 {
        struct ipt_entry_target *t;
-       struct ipt_target *target;
+       struct xt_target *target;
        struct ipt_entry *de;
        unsigned int origsize;
        int ret, h;
@@ -1627,14 +1558,11 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
        ret = IPT_MATCH_ITERATE(e, compat_copy_match_from_user, dstptr, size,
                        name, &de->ip, de->comefrom);
        if (ret)
-               goto out;
+               return ret;
        de->target_offset = e->target_offset - (origsize - *size);
        t = ipt_get_target(e);
        target = t->u.kernel.target;
-       if (target->compat)
-               target->compat(t, dstptr, size, COMPAT_FROM_USER);
-       else
-               xt_compat_target(t, dstptr, size, COMPAT_FROM_USER);
+       xt_compat_target_from_user(t, dstptr, size);
 
        de->next_offset = e->next_offset - (origsize - *size);
        for (h = 0; h < NF_IP_NUMHOOKS; h++) {
@@ -1643,30 +1571,18 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
                if ((unsigned char *)de - base < newinfo->underflow[h])
                        newinfo->underflow[h] -= origsize - *size;
        }
+       return ret;
+}
 
-       t = ipt_get_target(de);
-       target = t->u.kernel.target;
-       ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
-                             name, e->comefrom, e->ip.proto,
-                             e->ip.invflags & IPT_INV_PROTO);
+static inline int compat_check_entry(struct ipt_entry *e, const char *name)
+{
+       int ret;
+
+       ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip, e->comefrom);
        if (ret)
-               goto out;
+               return ret;
 
-       ret = -EINVAL;
-       if (t->u.kernel.target == &ipt_standard_target) {
-               if (!standard_check(t, *size))
-                       goto out;
-       } else if (t->u.kernel.target->checkentry
-                  && !t->u.kernel.target->checkentry(name, de, target,
-                               t->data, t->u.target_size - sizeof(*t),
-                               de->comefrom)) {
-               duprintf("ip_tables: compat: check failed for `%s'.\n",
-                        t->u.kernel.target->name);
-               goto out;
-       }
-       ret = 0;
-out:
-       return ret;
+       return check_target(e, name);
 }
 
 static int
@@ -1679,7 +1595,7 @@ translate_compat_table(const char *name,
                unsigned int *hook_entries,
                unsigned int *underflows)
 {
-       unsigned int i;
+       unsigned int i, j;
        struct xt_table_info *newinfo, *info;
        void *pos, *entry0, *entry1;
        unsigned int size;
@@ -1697,21 +1613,21 @@ translate_compat_table(const char *name,
        }
 
        duprintf("translate_compat_table: size %u\n", info->size);
-       i = 0;
+       j = 0;
        xt_compat_lock(AF_INET);
        /* Walk through entries, checking offsets. */
        ret = IPT_ENTRY_ITERATE(entry0, total_size,
                                check_compat_entry_size_and_hooks,
                                info, &size, entry0,
                                entry0 + total_size,
-                               hook_entries, underflows, &i, name);
+                               hook_entries, underflows, &j, name);
        if (ret != 0)
                goto out_unlock;
 
        ret = -EINVAL;
-       if (i != number) {
+       if (j != number) {
                duprintf("translate_compat_table: %u not %u entries\n",
-                        i, number);
+                        j, number);
                goto out_unlock;
        }
 
@@ -1757,6 +1673,11 @@ translate_compat_table(const char *name,
        if (!mark_source_chains(newinfo, valid_hooks, entry1))
                goto free_newinfo;
 
+       ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
+                                                                       name);
+       if (ret)
+               goto free_newinfo;
+
        /* And one copy for every other CPU */
        for_each_possible_cpu(i)
                if (newinfo->entries[i] && newinfo->entries[i] != entry1)
@@ -1770,8 +1691,10 @@ translate_compat_table(const char *name,
 free_newinfo:
        xt_free_table_info(newinfo);
 out:
+       IPT_ENTRY_ITERATE(entry0, total_size, cleanup_entry, &j);
        return ret;
 out_unlock:
+       compat_flush_offsets();
        xt_compat_unlock(AF_INET);
        goto out;
 }
@@ -1866,7 +1789,7 @@ struct compat_ipt_get_entries
 };
 
 static int compat_copy_entries_to_user(unsigned int total_size,
-                    struct ipt_table *table, void __user *userptr)
+                    struct xt_table *table, void __user *userptr)
 {
        unsigned int off, num;
        struct compat_ipt_entry e;
@@ -1940,7 +1863,7 @@ compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
 {
        int ret;
        struct compat_ipt_get_entries get;
-       struct ipt_table *t;
+       struct xt_table *t;
 
 
        if (*len < sizeof(get)) {
@@ -1986,11 +1909,16 @@ compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
        return ret;
 }
 
+static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
+
 static int
 compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
 {
        int ret;
 
+       if (!capable(CAP_NET_ADMIN))
+               return -EPERM;
+
        switch (cmd) {
        case IPT_SO_GET_INFO:
                ret = get_info(user, len, 1);
@@ -1999,8 +1927,7 @@ compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
                ret = compat_get_entries(user, len);
                break;
        default:
-               duprintf("compat_do_ipt_get_ctl: unknown request %i\n", cmd);
-               ret = -EINVAL;
+               ret = do_ipt_get_ctl(sk, cmd, user, len);
        }
        return ret;
 }
@@ -2119,12 +2046,12 @@ int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
        return 0;
 }
 
-void ipt_unregister_table(struct ipt_table *table)
+void ipt_unregister_table(struct xt_table *table)
 {
        struct xt_table_info *private;
        void *loc_cpu_entry;
 
-       private = xt_unregister_table(table);
+       private = xt_unregister_table(table);
 
        /* Decrease module usage counts and free resources */
        loc_cpu_entry = private->entries[raw_smp_processor_id()];
@@ -2182,7 +2109,6 @@ icmp_checkentry(const char *tablename,
           const void *info,
           const struct xt_match *match,
           void *matchinfo,
-          unsigned int matchsize,
           unsigned int hook_mask)
 {
        const struct ipt_icmp *icmpinfo = matchinfo;
@@ -2192,16 +2118,18 @@ icmp_checkentry(const char *tablename,
 }
 
 /* The built-in targets: standard (NULL) and error. */
-static struct ipt_target ipt_standard_target = {
+static struct xt_target ipt_standard_target = {
        .name           = IPT_STANDARD_TARGET,
        .targetsize     = sizeof(int),
        .family         = AF_INET,
 #ifdef CONFIG_COMPAT
-       .compat         = &compat_ipt_standard_fn,
+       .compatsize     = sizeof(compat_int_t),
+       .compat_from_user = compat_standard_from_user,
+       .compat_to_user = compat_standard_to_user,
 #endif
 };
 
-static struct ipt_target ipt_error_target = {
+static struct xt_target ipt_error_target = {
        .name           = IPT_ERROR_TARGET,
        .target         = ipt_error,
        .targetsize     = IPT_FUNCTION_MAXNAMELEN,
@@ -2224,7 +2152,7 @@ static struct nf_sockopt_ops ipt_sockopts = {
 #endif
 };
 
-static struct ipt_match icmp_matchstruct = {
+static struct xt_match icmp_matchstruct = {
        .name           = "icmp",
        .match          = icmp_match,
        .matchsize      = sizeof(struct ipt_icmp),