allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / nf_conntrack_expect.c
blob48aa71ea742432fe0f7e214ef4cf1f0e41780a54
1 /* Expectation handling for nf_conntrack. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/netfilter.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/stddef.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/kernel.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_tuple.h>
29 LIST_HEAD(nf_conntrack_expect_list);
30 EXPORT_SYMBOL_GPL(nf_conntrack_expect_list);
32 struct kmem_cache *nf_conntrack_expect_cachep __read_mostly;
33 static unsigned int nf_conntrack_expect_next_id;
35 /* nf_conntrack_expect helper functions */
36 void nf_ct_unlink_expect(struct nf_conntrack_expect *exp)
38 struct nf_conn_help *master_help = nfct_help(exp->master);
40 NF_CT_ASSERT(master_help);
41 NF_CT_ASSERT(!timer_pending(&exp->timeout));
43 list_del(&exp->list);
44 NF_CT_STAT_INC(expect_delete);
45 master_help->expecting[exp->class]--;
46 nf_conntrack_expect_put(exp);
48 EXPORT_SYMBOL_GPL(nf_ct_unlink_expect);
50 static void expectation_timed_out(unsigned long ul_expect)
52 struct nf_conntrack_expect *exp = (void *)ul_expect;
54 write_lock_bh(&nf_conntrack_lock);
55 nf_ct_unlink_expect(exp);
56 write_unlock_bh(&nf_conntrack_lock);
57 nf_conntrack_expect_put(exp);
60 struct nf_conntrack_expect *
61 __nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple)
63 struct nf_conntrack_expect *i;
65 list_for_each_entry(i, &nf_conntrack_expect_list, list) {
66 if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask))
67 return i;
69 return NULL;
71 EXPORT_SYMBOL_GPL(__nf_conntrack_expect_find);
73 /* Just find a expectation corresponding to a tuple. */
74 struct nf_conntrack_expect *
75 nf_conntrack_expect_find_get(const struct nf_conntrack_tuple *tuple)
77 struct nf_conntrack_expect *i;
79 read_lock_bh(&nf_conntrack_lock);
80 i = __nf_conntrack_expect_find(tuple);
81 if (i)
82 atomic_inc(&i->use);
83 read_unlock_bh(&nf_conntrack_lock);
85 return i;
87 EXPORT_SYMBOL_GPL(nf_conntrack_expect_find_get);
89 /* If an expectation for this connection is found, it gets delete from
90 * global list then returned. */
91 struct nf_conntrack_expect *
92 find_expectation(const struct nf_conntrack_tuple *tuple)
94 struct nf_conntrack_expect *i, *exp = NULL;
96 list_for_each_entry(i, &nf_conntrack_expect_list, list) {
97 if (!(i->flags & NF_CT_EXPECT_INACTIVE) &&
98 nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask)) {
99 exp = i;
100 break;
103 if (!exp)
104 return NULL;
106 /* If master is not in hash table yet (ie. packet hasn't left
107 this machine yet), how can other end know about expected?
108 Hence these are not the droids you are looking for (if
109 master ct never got confirmed, we'd hold a reference to it
110 and weird things would happen to future packets). */
111 if (!nf_ct_is_confirmed(exp->master))
112 return NULL;
114 if (exp->flags & NF_CT_EXPECT_PERMANENT) {
115 atomic_inc(&exp->use);
116 return exp;
117 } else if (del_timer(&exp->timeout)) {
118 nf_ct_unlink_expect(exp);
119 return exp;
122 return NULL;
125 /* delete all expectations for this conntrack */
126 void nf_ct_remove_expectations(struct nf_conn *ct)
128 struct nf_conntrack_expect *i, *tmp;
129 struct nf_conn_help *help = nfct_help(ct);
131 /* Optimization: most connection never expect any others. */
132 if (!help)
133 return;
135 list_for_each_entry_safe(i, tmp, &nf_conntrack_expect_list, list) {
136 if (i->master == ct && del_timer(&i->timeout)) {
137 nf_ct_unlink_expect(i);
138 nf_conntrack_expect_put(i);
142 EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
144 /* Would two expected things clash? */
145 static inline int expect_clash(const struct nf_conntrack_expect *a,
146 const struct nf_conntrack_expect *b)
148 /* Part covered by intersection of masks must be unequal,
149 otherwise they clash */
150 struct nf_conntrack_tuple intersect_mask;
151 int count;
153 intersect_mask.src.l3num = a->mask.src.l3num & b->mask.src.l3num;
154 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
155 intersect_mask.dst.u.all = a->mask.dst.u.all & b->mask.dst.u.all;
156 intersect_mask.dst.protonum = a->mask.dst.protonum
157 & b->mask.dst.protonum;
159 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
160 intersect_mask.src.u3.all[count] =
161 a->mask.src.u3.all[count] & b->mask.src.u3.all[count];
164 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
165 intersect_mask.dst.u3.all[count] =
166 a->mask.dst.u3.all[count] & b->mask.dst.u3.all[count];
169 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);
172 static inline int expect_matches(const struct nf_conntrack_expect *a,
173 const struct nf_conntrack_expect *b)
175 return a->master == b->master && a->class == b->class
176 && nf_ct_tuple_equal(&a->tuple, &b->tuple)
177 && nf_ct_tuple_equal(&a->mask, &b->mask);
180 /* Generally a bad idea to call this: could have matched already. */
181 void nf_conntrack_unexpect_related(struct nf_conntrack_expect *exp)
183 write_lock_bh(&nf_conntrack_lock);
184 if (del_timer(&exp->timeout)) {
185 nf_ct_unlink_expect(exp);
186 nf_conntrack_expect_put(exp);
188 write_unlock_bh(&nf_conntrack_lock);
190 EXPORT_SYMBOL_GPL(nf_conntrack_unexpect_related);
192 /* We don't increase the master conntrack refcount for non-fulfilled
193 * conntracks. During the conntrack destruction, the expectations are
194 * always killed before the conntrack itself */
195 struct nf_conntrack_expect *nf_conntrack_expect_alloc(struct nf_conn *me)
197 struct nf_conntrack_expect *new;
199 new = kmem_cache_alloc(nf_conntrack_expect_cachep, GFP_ATOMIC);
200 if (!new)
201 return NULL;
203 new->master = me;
204 atomic_set(&new->use, 1);
205 return new;
207 EXPORT_SYMBOL_GPL(nf_conntrack_expect_alloc);
209 void nf_conntrack_expect_init(struct nf_conntrack_expect *exp, unsigned int class,
210 int family,
211 union nf_inet_addr *saddr,
212 union nf_inet_addr *daddr,
213 u_int8_t proto, __be16 *src, __be16 *dst)
215 int len;
217 if (family == AF_INET)
218 len = 4;
219 else
220 len = 16;
222 exp->flags = 0;
223 exp->class = class;
224 exp->expectfn = NULL;
225 exp->helper = NULL;
226 exp->tuple.src.l3num = family;
227 exp->tuple.dst.protonum = proto;
228 exp->mask.src.l3num = 0xFFFF;
229 exp->mask.dst.protonum = 0xFF;
231 if (saddr) {
232 memcpy(&exp->tuple.src.u3, saddr, len);
233 if (sizeof(exp->tuple.src.u3) > len)
234 /* address needs to be cleared for nf_ct_tuple_equal */
235 memset((void *)&exp->tuple.src.u3 + len, 0x00,
236 sizeof(exp->tuple.src.u3) - len);
237 memset(&exp->mask.src.u3, 0xFF, len);
238 if (sizeof(exp->mask.src.u3) > len)
239 memset((void *)&exp->mask.src.u3 + len, 0x00,
240 sizeof(exp->mask.src.u3) - len);
241 } else {
242 memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
243 memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
246 if (daddr) {
247 memcpy(&exp->tuple.dst.u3, daddr, len);
248 if (sizeof(exp->tuple.dst.u3) > len)
249 /* address needs to be cleared for nf_ct_tuple_equal */
250 memset((void *)&exp->tuple.dst.u3 + len, 0x00,
251 sizeof(exp->tuple.dst.u3) - len);
252 memset(&exp->mask.dst.u3, 0xFF, len);
253 if (sizeof(exp->mask.dst.u3) > len)
254 memset((void *)&exp->mask.dst.u3 + len, 0x00,
255 sizeof(exp->mask.dst.u3) - len);
256 } else {
257 memset(&exp->tuple.dst.u3, 0x00, sizeof(exp->tuple.dst.u3));
258 memset(&exp->mask.dst.u3, 0x00, sizeof(exp->mask.dst.u3));
261 if (src) {
262 exp->tuple.src.u.all = (__force u16)*src;
263 exp->mask.src.u.all = 0xFFFF;
264 } else {
265 exp->tuple.src.u.all = 0;
266 exp->mask.src.u.all = 0;
269 if (dst) {
270 exp->tuple.dst.u.all = (__force u16)*dst;
271 exp->mask.dst.u.all = 0xFFFF;
272 } else {
273 exp->tuple.dst.u.all = 0;
274 exp->mask.dst.u.all = 0;
277 EXPORT_SYMBOL_GPL(nf_conntrack_expect_init);
279 void nf_conntrack_expect_put(struct nf_conntrack_expect *exp)
281 if (atomic_dec_and_test(&exp->use))
282 kmem_cache_free(nf_conntrack_expect_cachep, exp);
284 EXPORT_SYMBOL_GPL(nf_conntrack_expect_put);
286 static void nf_conntrack_expect_insert(struct nf_conntrack_expect *exp)
288 struct nf_conn_help *master_help = nfct_help(exp->master);
289 const struct nf_conntrack_expect_policy *p;
291 atomic_inc(&exp->use);
292 master_help->expecting[exp->class]++;
293 list_add(&exp->list, &nf_conntrack_expect_list);
295 setup_timer(&exp->timeout, expectation_timed_out, (unsigned long)exp);
296 p = &master_help->helper->expect_policy[exp->class];
297 exp->timeout.expires = jiffies + p->timeout * HZ;
298 add_timer(&exp->timeout);
300 exp->id = ++nf_conntrack_expect_next_id;
301 atomic_inc(&exp->use);
302 NF_CT_STAT_INC(expect_create);
305 /* Race with expectations being used means we could have none to find; OK. */
306 static void evict_oldest_expect(struct nf_conn *master,
307 struct nf_conntrack_expect *new)
309 struct nf_conntrack_expect *i;
311 list_for_each_entry_reverse(i, &nf_conntrack_expect_list, list) {
312 if (i->master == master && i->class == new->class) {
313 if (del_timer(&i->timeout)) {
314 nf_ct_unlink_expect(i);
315 nf_conntrack_expect_put(i);
317 break;
322 static inline int refresh_timer(struct nf_conntrack_expect *i)
324 struct nf_conn_help *master_help = nfct_help(i->master);
325 const struct nf_conntrack_expect_policy *p;
327 if (!del_timer(&i->timeout))
328 return 0;
330 p = &master_help->helper->expect_policy[i->class];
331 i->timeout.expires = jiffies + p->timeout * HZ;
332 add_timer(&i->timeout);
333 return 1;
336 int nf_conntrack_expect_related(struct nf_conntrack_expect *expect)
338 const struct nf_conntrack_expect_policy *p;
339 struct nf_conntrack_expect *i;
340 struct nf_conn *master = expect->master;
341 struct nf_conn_help *master_help = nfct_help(master);
342 int ret;
344 NF_CT_ASSERT(master_help);
346 write_lock_bh(&nf_conntrack_lock);
347 if (!master_help->helper) {
348 ret = -ESHUTDOWN;
349 goto out;
351 list_for_each_entry(i, &nf_conntrack_expect_list, list) {
352 if (expect_matches(i, expect)) {
353 /* Refresh timer: if it's dying, ignore.. */
354 if (refresh_timer(i)) {
355 ret = 0;
356 goto out;
358 } else if (expect_clash(i, expect)) {
359 ret = -EBUSY;
360 goto out;
363 /* Will be over limit? */
364 p = &master_help->helper->expect_policy[expect->class];
365 if (p->max_expected &&
366 master_help->expecting[expect->class] >= p->max_expected) {
367 evict_oldest_expect(master, expect);
368 if (master_help->expecting[expect->class] >= p->max_expected) {
369 ret = -EMFILE;
370 goto out;
374 nf_conntrack_expect_insert(expect);
375 nf_conntrack_expect_event(IPEXP_NEW, expect);
376 ret = 0;
377 out:
378 write_unlock_bh(&nf_conntrack_lock);
379 return ret;
381 EXPORT_SYMBOL_GPL(nf_conntrack_expect_related);
383 #ifdef CONFIG_PROC_FS
384 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
386 struct list_head *e = &nf_conntrack_expect_list;
387 loff_t i;
389 /* strange seq_file api calls stop even if we fail,
390 * thus we need to grab lock since stop unlocks */
391 read_lock_bh(&nf_conntrack_lock);
393 if (list_empty(e))
394 return NULL;
396 for (i = 0; i <= *pos; i++) {
397 e = e->next;
398 if (e == &nf_conntrack_expect_list)
399 return NULL;
401 return e;
404 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
406 struct list_head *e = v;
408 ++*pos;
409 e = e->next;
411 if (e == &nf_conntrack_expect_list)
412 return NULL;
414 return e;
417 static void exp_seq_stop(struct seq_file *s, void *v)
419 read_unlock_bh(&nf_conntrack_lock);
422 static int exp_seq_show(struct seq_file *s, void *v)
424 struct nf_conntrack_expect *expect = v;
425 struct nf_conntrack_helper *helper;
426 char *delim = "";
428 if (expect->timeout.function)
429 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
430 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
431 else
432 seq_printf(s, "- ");
433 seq_printf(s, "l3proto = %u proto=%u ",
434 expect->tuple.src.l3num,
435 expect->tuple.dst.protonum);
436 print_tuple(s, &expect->tuple,
437 __nf_ct_l3proto_find(expect->tuple.src.l3num),
438 __nf_ct_l4proto_find(expect->tuple.src.l3num,
439 expect->tuple.dst.protonum));
441 if (expect->flags & NF_CT_EXPECT_PERMANENT) {
442 seq_printf(s, "PERMANENT");
443 delim = ",";
445 if (expect->flags & NF_CT_EXPECT_INACTIVE)
446 seq_printf(s, "%sINACTIVE", delim);
448 helper = rcu_dereference(nfct_help(expect->master)->helper);
449 if (helper) {
450 seq_printf(s, "%s%s", expect->flags ? " " : "", helper->name);
451 if (helper->expect_policy[expect->class].name)
452 seq_printf(s, "/%s",
453 helper->expect_policy[expect->class].name);
456 return seq_putc(s, '\n');
459 static struct seq_operations exp_seq_ops = {
460 .start = exp_seq_start,
461 .next = exp_seq_next,
462 .stop = exp_seq_stop,
463 .show = exp_seq_show
466 static int exp_open(struct inode *inode, struct file *file)
468 return seq_open(file, &exp_seq_ops);
471 const struct file_operations exp_file_ops = {
472 .owner = THIS_MODULE,
473 .open = exp_open,
474 .read = seq_read,
475 .llseek = seq_lseek,
476 .release = seq_release
478 #endif /* CONFIG_PROC_FS */