fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / netfilter / ip_tables.c
index a560c55..fc1f153 100644 (file)
@@ -180,8 +180,7 @@ ipt_error(struct sk_buff **pskb,
          const struct net_device *out,
          unsigned int hooknum,
          const struct xt_target *target,
-         const void *targinfo,
-         void *userinfo)
+         const void *targinfo)
 {
        if (net_ratelimit())
                printk("ip_tables: error: `%s'\n", (char *)targinfo);
@@ -217,8 +216,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,
-            void *userdata)
+            struct ipt_table *table)
 {
        static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
        u_int16_t offset;
@@ -308,8 +306,7 @@ ipt_do_table(struct sk_buff **pskb,
                                                                     in, out,
                                                                     hook,
                                                                     t->u.kernel.target,
-                                                                    t->data,
-                                                                    userdata);
+                                                                    t->data);
 
 #ifdef CONFIG_NETFILTER_DEBUG
                                if (((struct ipt_entry *)table_base)->comefrom
@@ -387,6 +384,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",
@@ -397,11 +395,11 @@ 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) {
@@ -481,14 +479,53 @@ 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
-check_match(struct ipt_entry_match *m,
+check_entry(struct ipt_entry *e, const char *name)
+{
+       struct ipt_entry_target *t;
+
+       if (!ip_checkentry(&e->ip)) {
+               duprintf("ip_tables: ip check failed %p %s.\n", e, name);
+               return -EINVAL;
+       }
+
+       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 ipt_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 ret;
+}
+
+static inline int
+find_check_match(struct ipt_entry_match *m,
            const char *name,
            const struct ipt_ip *ip,
            unsigned int hookmask,
@@ -501,27 +538,15 @@ check_match(struct ipt_entry_match *m,
                                                   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:
@@ -529,10 +554,29 @@ 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 ipt_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;
@@ -540,51 +584,32 @@ check_entry(struct ipt_entry *e, const char *name, unsigned int size,
        int ret;
        unsigned int j;
 
-       if (!ip_checkentry(&e->ip)) {
-               duprintf("ip_tables: ip check failed %p %s.\n", e, name);
-               return -EINVAL;
-       }
-
-       if (e->target_offset + sizeof(struct ipt_entry_target) > e->next_offset)
-               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;
 
        t = ipt_get_target(e);
-       ret = -EINVAL;
-       if (e->target_offset + t->u.target_size > e->next_offset)
-                       goto cleanup_matches;
        target = try_then_request_module(xt_find_target(AF_INET,
                                                     t->u.user.name,
                                                     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->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:
@@ -649,8 +674,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;
 }
@@ -720,7 +744,7 @@ 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,
@@ -895,13 +919,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;
 
@@ -933,7 +957,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;
@@ -944,73 +968,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;
-};
+       int v = *(compat_int_t *)src;
 
-struct compat_ipt_standard
-{
-       struct compat_ipt_entry entry;
-       struct compat_ipt_standard_target target;
-};
-
-#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;
 }
 
@@ -1018,17 +997,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)
@@ -1416,17 +1392,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;
@@ -1444,11 +1416,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;
@@ -1480,11 +1448,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;
@@ -1503,7 +1467,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
 {
        struct ipt_entry_target *t;
        struct ipt_target *target;
-       u_int16_t entry_offset;
+       unsigned int entry_offset;
        int ret, off, h, j;
 
        duprintf("check_compat_entry_size_and_hooks %p\n", e);
@@ -1520,14 +1484,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;
-       }
-
-       if (e->target_offset + sizeof(struct compat_xt_entry_target) >
-                                                               e->next_offset)
-               return -EINVAL;
+       ret = check_entry(e, name);
+       if (ret)
+               return ret;
 
        off = 0;
        entry_offset = (void *)e - (void *)base;
@@ -1538,24 +1497,19 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
                goto cleanup_matches;
 
        t = ipt_get_target(e);
-       ret = -EINVAL;
-       if (e->target_offset + t->u.target_size > e->next_offset)
-                       goto cleanup_matches;
        target = try_then_request_module(xt_find_target(AF_INET,
                                                     t->u.user.name,
                                                     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 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)
@@ -1587,30 +1541,8 @@ 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 && 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);
-               ret = -EINVAL;
-       }
-       return ret;
+       xt_compat_match_from_user(m, dstptr, size);
+       return 0;
 }
 
 static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
@@ -1632,14 +1564,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 err;
+               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++) {
@@ -1648,26 +1577,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 err;
+               return ret;
 
-       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);
-               ret = -EINVAL;
-               goto err;
-       }
- err:
-       return ret;
+       return check_target(e, name);
 }
 
 static int
@@ -1758,6 +1679,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)
@@ -2189,7 +2115,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;
@@ -2204,7 +2129,9 @@ static struct ipt_target 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
 };