Root/target/linux/generic/patches-2.6.36/110-netfilter_match_speedup.patch

1--- a/include/linux/netfilter_ipv4/ip_tables.h
2+++ b/include/linux/netfilter_ipv4/ip_tables.h
3@@ -62,6 +62,7 @@ struct ipt_ip {
4 #define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */
5 #define IPT_F_GOTO 0x02 /* Set if jump is a goto */
6 #define IPT_F_MASK 0x03 /* All possible flag bits mask. */
7+#define IPT_F_NO_DEF_MATCH 0x80 /* Internal: no default match rules present */
8 
9 /* Values for "inv" field in struct ipt_ip. */
10 #define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
11--- a/net/ipv4/netfilter/ip_tables.c
12+++ b/net/ipv4/netfilter/ip_tables.c
13@@ -90,6 +90,9 @@ ip_packet_match(const struct iphdr *ip,
14 
15 #define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
16 
17+ if (ipinfo->flags & IPT_F_NO_DEF_MATCH)
18+ return true;
19+
20     if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
21           IPT_INV_SRCIP) ||
22         FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
23@@ -140,13 +143,35 @@ ip_packet_match(const struct iphdr *ip,
24         return false;
25     }
26 
27+#undef FWINV
28     return true;
29 }
30 
31 static bool
32-ip_checkentry(const struct ipt_ip *ip)
33+ip_checkentry(struct ipt_ip *ip)
34 {
35- if (ip->flags & ~IPT_F_MASK) {
36+#define FWINV(bool, invflg) ((bool) || (ip->invflags & (invflg)))
37+
38+ if (FWINV(ip->smsk.s_addr, IPT_INV_SRCIP) ||
39+ FWINV(ip->dmsk.s_addr, IPT_INV_DSTIP))
40+ goto has_match_rules;
41+
42+ if (FWINV(!!((const unsigned long *)ip->iniface_mask)[0],
43+ IPT_INV_VIA_IN) ||
44+ FWINV(!!((const unsigned long *)ip->outiface_mask)[0],
45+ IPT_INV_VIA_OUT))
46+ goto has_match_rules;
47+
48+ if (FWINV(ip->proto, IPT_INV_PROTO))
49+ goto has_match_rules;
50+
51+ if (FWINV(ip->flags&IPT_F_FRAG, IPT_INV_FRAG))
52+ goto has_match_rules;
53+
54+ ip->flags |= IPT_F_NO_DEF_MATCH;
55+
56+has_match_rules:
57+ if (ip->flags & ~(IPT_F_MASK|IPT_F_NO_DEF_MATCH)) {
58         duprintf("Unknown flag bits set: %08X\n",
59              ip->flags & ~IPT_F_MASK);
60         return false;
61@@ -156,6 +181,8 @@ ip_checkentry(const struct ipt_ip *ip)
62              ip->invflags & ~IPT_INV_MASK);
63         return false;
64     }
65+
66+#undef FWINV
67     return true;
68 }
69 
70@@ -182,7 +209,6 @@ static inline bool unconditional(const s
71     static const struct ipt_ip uncond;
72 
73     return memcmp(ip, &uncond, sizeof(uncond)) == 0;
74-#undef FWINV
75 }
76 
77 /* for const-correctness */
78@@ -312,8 +338,29 @@ ipt_do_table(struct sk_buff *skb,
79     const struct xt_table_info *private;
80     struct xt_action_param acpar;
81 
82- /* Initialization */
83     ip = ip_hdr(skb);
84+
85+ IP_NF_ASSERT(table->valid_hooks & (1 << hook));
86+ xt_info_rdlock_bh();
87+ private = table->private;
88+ cpu = smp_processor_id();
89+ table_base = private->entries[cpu];
90+ e = get_entry(table_base, private->hook_entry[hook]);
91+
92+ if (e->target_offset <= sizeof(struct ipt_entry) &&
93+ (e->ip.flags & IPT_F_NO_DEF_MATCH)) {
94+ struct ipt_entry_target *t = ipt_get_target(e);
95+ if (!t->u.kernel.target->target) {
96+ int v = ((struct ipt_standard_target *)t)->verdict;
97+ if ((v < 0) && (v != IPT_RETURN)) {
98+ ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
99+ xt_info_rdunlock_bh();
100+ return (unsigned)(-v) - 1;
101+ }
102+ }
103+ }
104+
105+ /* Initialization */
106     indev = in ? in->name : nulldevname;
107     outdev = out ? out->name : nulldevname;
108     /* We handle fragments by dealing with the first fragment as
109@@ -330,17 +377,10 @@ ipt_do_table(struct sk_buff *skb,
110     acpar.family = NFPROTO_IPV4;
111     acpar.hooknum = hook;
112 
113- IP_NF_ASSERT(table->valid_hooks & (1 << hook));
114- xt_info_rdlock_bh();
115- private = table->private;
116- cpu = smp_processor_id();
117- table_base = private->entries[cpu];
118     jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
119     stackptr = per_cpu_ptr(private->stackptr, cpu);
120     origptr = *stackptr;
121 
122- e = get_entry(table_base, private->hook_entry[hook]);
123-
124     pr_debug("Entering %s(hook %u); sp at %u (UF %p)\n",
125          table->name, hook, origptr,
126          get_entry(table_base, private->underflow[hook]));
127@@ -974,6 +1014,7 @@ copy_entries_to_user(unsigned int total_
128         unsigned int i;
129         const struct ipt_entry_match *m;
130         const struct ipt_entry_target *t;
131+ u8 flags;
132 
133         e = (struct ipt_entry *)(loc_cpu_entry + off);
134         if (copy_to_user(userptr + off
135@@ -983,6 +1024,14 @@ copy_entries_to_user(unsigned int total_
136             ret = -EFAULT;
137             goto free_counters;
138         }
139+
140+ flags = e->ip.flags & ~IPT_F_NO_DEF_MATCH;
141+ if (copy_to_user(userptr + off
142+ + offsetof(struct ipt_entry, ip.flags),
143+ &flags, sizeof(flags)) != 0) {
144+ ret = -EFAULT;
145+ goto free_counters;
146+ }
147 
148         for (i = sizeof(struct ipt_entry);
149              i < e->target_offset;
150

Archive Download this file



interactive