netfilter: iptables: remove unused datalen variable
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / ip_tables.c
index 82ee7c9..3856aa3 100644 (file)
@@ -238,8 +238,8 @@ static struct nf_loginfo trace_loginfo = {
 /* Mildly perf critical (only if packet tracing is on) */
 static inline int
 get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
-                     char *hookname, char **chainname,
-                     char **comment, unsigned int *rulenum)
+                     const char *hookname, const char **chainname,
+                     const char **comment, unsigned int *rulenum)
 {
        struct ipt_standard_target *t = (void *)ipt_get_target(s);
 
@@ -257,8 +257,8 @@ get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
                   && unconditional(&s->ip)) {
                        /* Tail of chains: STANDARD target (return/policy) */
                        *comment = *chainname == hookname
-                               ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
-                               : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
+                               ? comments[NF_IP_TRACE_COMMENT_POLICY]
+                               : comments[NF_IP_TRACE_COMMENT_RETURN];
                }
                return 1;
        } else
@@ -277,14 +277,14 @@ static void trace_packet(struct sk_buff *skb,
 {
        void *table_base;
        const struct ipt_entry *root;
-       char *hookname, *chainname, *comment;
+       const char *hookname, *chainname, *comment;
        unsigned int rulenum = 0;
 
-       table_base = (void *)private->entries[smp_processor_id()];
+       table_base = private->entries[smp_processor_id()];
        root = get_entry(table_base, private->hook_entry[hook]);
 
-       hookname = chainname = (char *)hooknames[hook];
-       comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
+       hookname = chainname = hooknames[hook];
+       comment = comments[NF_IP_TRACE_COMMENT_RULE];
 
        IPT_ENTRY_ITERATE(root,
                          private->size - private->hook_entry[hook],
@@ -297,6 +297,12 @@ static void trace_packet(struct sk_buff *skb,
 }
 #endif
 
+static inline __pure
+struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
+{
+       return (void *)entry + entry->next_offset;
+}
+
 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
 unsigned int
 ipt_do_table(struct sk_buff *skb,
@@ -305,9 +311,10 @@ ipt_do_table(struct sk_buff *skb,
             const struct net_device *out,
             struct xt_table *table)
 {
+#define tb_comefrom ((struct ipt_entry *)table_base)->comefrom
+
        static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
        const struct iphdr *ip;
-       u_int16_t datalen;
        bool hotdrop = false;
        /* Initializing verdict to NF_DROP keeps gcc happy. */
        unsigned int verdict = NF_DROP;
@@ -320,7 +327,6 @@ ipt_do_table(struct sk_buff *skb,
 
        /* Initialization */
        ip = ip_hdr(skb);
-       datalen = skb->len - ip->ihl * 4;
        indev = in ? in->name : nulldevname;
        outdev = out ? out->name : nulldevname;
        /* We handle fragments by dealing with the first fragment as
@@ -335,13 +341,12 @@ ipt_do_table(struct sk_buff *skb,
        mtpar.in      = tgpar.in  = in;
        mtpar.out     = tgpar.out = out;
        mtpar.family  = tgpar.family = NFPROTO_IPV4;
-       tgpar.hooknum = hook;
+       mtpar.hooknum = tgpar.hooknum = hook;
 
        IP_NF_ASSERT(table->valid_hooks & (1 << hook));
-
-       rcu_read_lock();
-       private = rcu_dereference(table->private);
-       table_base = rcu_dereference(private->entries[smp_processor_id()]);
+       xt_info_rdlock_bh();
+       private = table->private;
+       table_base = private->entries[smp_processor_id()];
 
        e = get_entry(table_base, private->hook_entry[hook]);
 
@@ -349,95 +354,84 @@ ipt_do_table(struct sk_buff *skb,
        back = get_entry(table_base, private->underflow[hook]);
 
        do {
+               struct ipt_entry_target *t;
+
                IP_NF_ASSERT(e);
                IP_NF_ASSERT(back);
-               if (ip_packet_match(ip, indev, outdev,
-                   &e->ip, mtpar.fragoff)) {
-                       struct ipt_entry_target *t;
-
-                       if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
-                               goto no_match;
+               if (!ip_packet_match(ip, indev, outdev,
+                   &e->ip, mtpar.fragoff) ||
+                   IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0) {
+                       e = ipt_next_entry(e);
+                       continue;
+               }
 
-                       ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
+               ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
 
-                       t = ipt_get_target(e);
-                       IP_NF_ASSERT(t->u.kernel.target);
+               t = ipt_get_target(e);
+               IP_NF_ASSERT(t->u.kernel.target);
 
 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
-                       /* The packet is traced: log it */
-                       if (unlikely(skb->nf_trace))
-                               trace_packet(skb, hook, in, out,
-                                            table->name, private, e);
+               /* The packet is traced: log it */
+               if (unlikely(skb->nf_trace))
+                       trace_packet(skb, hook, in, out,
+                                    table->name, private, e);
 #endif
-                       /* Standard target? */
-                       if (!t->u.kernel.target->target) {
-                               int v;
-
-                               v = ((struct ipt_standard_target *)t)->verdict;
-                               if (v < 0) {
-                                       /* Pop from stack? */
-                                       if (v != IPT_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
-                                   && !(e->ip.flags & IPT_F_GOTO)) {
-                                       /* Save old back ptr in next entry */
-                                       struct ipt_entry *next
-                                               = (void *)e + e->next_offset;
-                                       next->comefrom
-                                               = (void *)back - table_base;
-                                       /* set back pointer to next entry */
-                                       back = next;
+               /* Standard target? */
+               if (!t->u.kernel.target->target) {
+                       int v;
+
+                       v = ((struct ipt_standard_target *)t)->verdict;
+                       if (v < 0) {
+                               /* Pop from stack? */
+                               if (v != IPT_RETURN) {
+                                       verdict = (unsigned)(-v) - 1;
+                                       break;
                                }
+                               e = back;
+                               back = get_entry(table_base, back->comefrom);
+                               continue;
+                       }
+                       if (table_base + v != ipt_next_entry(e)
+                           && !(e->ip.flags & IPT_F_GOTO)) {
+                               /* Save old back ptr in next entry */
+                               struct ipt_entry *next = ipt_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;
+
 
-                               e = get_entry(table_base, v);
-                       } else {
-                               /* Targets which reenter must return
-                                  abs. verdicts */
-                               tgpar.target   = t->u.kernel.target;
-                               tgpar.targinfo = t->data;
 #ifdef CONFIG_NETFILTER_DEBUG
-                               ((struct ipt_entry *)table_base)->comefrom
-                                       = 0xeeeeeeec;
+               tb_comefrom = 0xeeeeeeec;
 #endif
-                               verdict = t->u.kernel.target->target(skb,
-                                                                    &tgpar);
+               verdict = t->u.kernel.target->target(skb, &tgpar);
 #ifdef CONFIG_NETFILTER_DEBUG
-                               if (((struct ipt_entry *)table_base)->comefrom
-                                   != 0xeeeeeeec
-                                   && verdict == IPT_CONTINUE) {
-                                       printk("Target %s reentered!\n",
-                                              t->u.kernel.target->name);
-                                       verdict = NF_DROP;
-                               }
-                               ((struct ipt_entry *)table_base)->comefrom
-                                       = 0x57acc001;
-#endif
-                               /* Target might have changed stuff. */
-                               ip = ip_hdr(skb);
-                               datalen = skb->len - ip->ihl * 4;
-
-                               if (verdict == IPT_CONTINUE)
-                                       e = (void *)e + e->next_offset;
-                               else
-                                       /* Verdict */
-                                       break;
-                       }
-               } else {
-
-               no_match:
-                       e = (void *)e + e->next_offset;
+               if (tb_comefrom != 0xeeeeeeec && verdict == IPT_CONTINUE) {
+                       printk("Target %s reentered!\n",
+                              t->u.kernel.target->name);
+                       verdict = NF_DROP;
                }
+               tb_comefrom = 0x57acc001;
+#endif
+               /* Target might have changed stuff. */
+               ip = ip_hdr(skb);
+               if (verdict == IPT_CONTINUE)
+                       e = ipt_next_entry(e);
+               else
+                       /* Verdict */
+                       break;
        } while (!hotdrop);
-
-       rcu_read_unlock();
+       xt_info_rdunlock_bh();
 
 #ifdef DEBUG_ALLOW_ALL
        return NF_ACCEPT;
@@ -446,6 +440,8 @@ ipt_do_table(struct sk_buff *skb,
                return NF_DROP;
        else return verdict;
 #endif
+
+#undef tb_comefrom
 }
 
 /* Figures out from what hook each rule can be called: returns 0 if
@@ -896,10 +892,13 @@ 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.
+        * with data used by 'current' CPU.
+        *
+        * 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;
        IPT_ENTRY_ITERATE(t->entries[curcpu],
@@ -912,74 +911,22 @@ get_counters(const struct xt_table_info *t,
                if (cpu == curcpu)
                        continue;
                i = 0;
+               xt_info_wrlock(cpu);
                IPT_ENTRY_ITERATE(t->entries[cpu],
                                  t->size,
                                  add_entry_to_counter,
                                  counters,
                                  &i);
+               xt_info_wrunlock(cpu);
        }
-
-}
-
-/* We're lazy, and add to the first CPU; overflow works its fey magic
- * and everything is OK. */
-static int
-add_counter_to_entry(struct ipt_entry *e,
-                    const struct xt_counters addme[],
-                    unsigned int *i)
-{
-       ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
-
-       (*i)++;
-       return 0;
-}
-
-/* Take values from counters and add them back onto the current cpu */
-static void put_counters(struct xt_table_info *t,
-                        const struct xt_counters counters[])
-{
-       unsigned int i, cpu;
-
-       local_bh_disable();
-       cpu = smp_processor_id();
-       i = 0;
-       IPT_ENTRY_ITERATE(t->entries[cpu],
-                         t->size,
-                         add_counter_to_entry,
-                         counters,
-                         &i);
        local_bh_enable();
 }
 
-
-static inline int
-zero_entry_counter(struct ipt_entry *e, void *arg)
-{
-       e->counters.bcnt = 0;
-       e->counters.pcnt = 0;
-       return 0;
-}
-
-static void
-clone_counters(struct xt_table_info *newinfo, const struct xt_table_info *info)
-{
-       unsigned int cpu;
-       const void *loc_cpu_entry = info->entries[raw_smp_processor_id()];
-
-       memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
-       for_each_possible_cpu(cpu) {
-               memcpy(newinfo->entries[cpu], loc_cpu_entry, info->size);
-               IPT_ENTRY_ITERATE(newinfo->entries[cpu], newinfo->size,
-                                 zero_entry_counter, NULL);
-       }
-}
-
 static struct xt_counters * alloc_counters(struct xt_table *table)
 {
        unsigned int countersize;
        struct xt_counters *counters;
        struct xt_table_info *private = table->private;
-       struct xt_table_info *info;
 
        /* We need atomic snapshot of counters: rest doesn't change
           (other than comefrom, which userspace doesn't care
@@ -988,30 +935,11 @@ static struct xt_counters * alloc_counters(struct xt_table *table)
        counters = vmalloc_node(countersize, numa_node_id());
 
        if (counters == NULL)
-               goto nomem;
-
-       info = xt_alloc_table_info(private->size);
-       if (!info)
-               goto free_counters;
+               return ERR_PTR(-ENOMEM);
 
-       clone_counters(info, private);
-
-       mutex_lock(&table->lock);
-       xt_table_entry_swap_rcu(private, info);
-       synchronize_net();      /* Wait until smoke has cleared */
-
-       get_counters(info, counters);
-       put_counters(private, counters);
-       mutex_unlock(&table->lock);
-
-       xt_free_table_info(info);
+       get_counters(private, counters);
 
        return counters;
-
- free_counters:
-       vfree(counters);
- nomem:
-       return ERR_PTR(-ENOMEM);
 }
 
 static int
@@ -1306,8 +1234,9 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
            (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()];
        IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
@@ -1377,11 +1306,23 @@ do_replace(struct net *net, void __user *user, unsigned int len)
        return ret;
 }
 
+/* We're lazy, and add to the first CPU; overflow works its fey magic
+ * and everything is OK. */
+static int
+add_counter_to_entry(struct ipt_entry *e,
+                    const struct xt_counters addme[],
+                    unsigned int *i)
+{
+       ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
+
+       (*i)++;
+       return 0;
+}
 
 static int
 do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
 {
-       unsigned int i;
+       unsigned int i, curcpu;
        struct xt_counters_info tmp;
        struct xt_counters *paddc;
        unsigned int num_counters;
@@ -1437,25 +1378,26 @@ do_add_counters(struct net *net, void __user *user, unsigned int len, int compat
                goto free;
        }
 
-       mutex_lock(&t->lock);
+       local_bh_disable();
        private = t->private;
        if (private->number != num_counters) {
                ret = -EINVAL;
                goto unlock_up_free;
        }
 
-       preempt_disable();
        i = 0;
        /* Choose the copy that is on our node */
-       loc_cpu_entry = private->entries[raw_smp_processor_id()];
+       curcpu = smp_processor_id();
+       loc_cpu_entry = private->entries[curcpu];
+       xt_info_wrlock(curcpu);
        IPT_ENTRY_ITERATE(loc_cpu_entry,
                          private->size,
                          add_counter_to_entry,
                          paddc,
                          &i);
-       preempt_enable();
+       xt_info_wrunlock(curcpu);
  unlock_up_free:
-       mutex_unlock(&t->lock);
+       local_bh_enable();
        xt_table_unlock(t);
        module_put(t->me);
  free:
@@ -2214,7 +2156,7 @@ static bool icmp_checkentry(const struct xt_mtchk_param *par)
 static struct xt_target ipt_standard_target __read_mostly = {
        .name           = IPT_STANDARD_TARGET,
        .targetsize     = sizeof(int),
-       .family         = AF_INET,
+       .family         = NFPROTO_IPV4,
 #ifdef CONFIG_COMPAT
        .compatsize     = sizeof(compat_int_t),
        .compat_from_user = compat_standard_from_user,
@@ -2226,7 +2168,7 @@ static struct xt_target ipt_error_target __read_mostly = {
        .name           = IPT_ERROR_TARGET,
        .target         = ipt_error,
        .targetsize     = IPT_FUNCTION_MAXNAMELEN,
-       .family         = AF_INET,
+       .family         = NFPROTO_IPV4,
 };
 
 static struct nf_sockopt_ops ipt_sockopts = {
@@ -2252,17 +2194,17 @@ static struct xt_match icmp_matchstruct __read_mostly = {
        .matchsize      = sizeof(struct ipt_icmp),
        .checkentry     = icmp_checkentry,
        .proto          = IPPROTO_ICMP,
-       .family         = AF_INET,
+       .family         = NFPROTO_IPV4,
 };
 
 static int __net_init ip_tables_net_init(struct net *net)
 {
-       return xt_proto_init(net, AF_INET);
+       return xt_proto_init(net, NFPROTO_IPV4);
 }
 
 static void __net_exit ip_tables_net_exit(struct net *net)
 {
-       xt_proto_fini(net, AF_INET);
+       xt_proto_fini(net, NFPROTO_IPV4);
 }
 
 static struct pernet_operations ip_tables_net_ops = {