Broadcom SDK and wireless driver: another attempt to update to ver. 5.10.147.0
[tomato.git] / release / src-rt / linux / linux-2.6 / net / netfilter / nf_conntrack_core.c
blob3478d1d2864b5cb371c298a6538131cf19222c3a
1 /* Connection state tracking for netfilter. This is separated from,
2 but required by, the NAT layer; it can also be used by an iptables
3 extension. */
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
7 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/types.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/proc_fs.h>
19 #include <linux/vmalloc.h>
20 #include <linux/stddef.h>
21 #include <linux/slab.h>
22 #include <linux/random.h>
23 #include <linux/jhash.h>
24 #include <linux/err.h>
25 #include <linux/percpu.h>
26 #include <linux/moduleparam.h>
27 #include <linux/notifier.h>
28 #include <linux/kernel.h>
29 #include <linux/netdevice.h>
30 #include <linux/socket.h>
31 #include <linux/mm.h>
32 //#ifdef CONFIG_BCM_NAT
33 #include <net/ip.h>
34 //#endif
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_core.h>
43 #define NF_CONNTRACK_VERSION "0.5.0"
45 #if 0
46 #define DEBUGP printk
47 #else
48 #define DEBUGP(format, args...)
49 #endif
51 DEFINE_RWLOCK(nf_conntrack_lock);
52 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
54 /* nf_conntrack_standalone needs this */
55 atomic_t nf_conntrack_count = ATOMIC_INIT(0);
56 EXPORT_SYMBOL_GPL(nf_conntrack_count);
58 void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
59 EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
61 unsigned int nf_conntrack_htable_size __read_mostly;
62 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
64 int nf_conntrack_max __read_mostly;
65 EXPORT_SYMBOL_GPL(nf_conntrack_max);
67 struct list_head *nf_conntrack_hash __read_mostly;
68 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
70 struct nf_conn nf_conntrack_untracked __read_mostly;
71 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
73 unsigned int nf_ct_log_invalid __read_mostly;
74 LIST_HEAD(unconfirmed);
75 static int nf_conntrack_vmalloc __read_mostly;
77 static unsigned int nf_conntrack_next_id;
79 DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
80 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
82 #ifdef HNDCTF
83 extern int ip_conntrack_ipct_delete(struct nf_conn *ct, int ct_timeout);
84 #endif /* HNDCTF */
86 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
87 #define BCM_FASTNAT_DENY 1
88 extern int ipv4_conntrack_fastnat;
90 typedef int (*bcmNatBindHook)(struct nf_conn *ct,
91 enum ip_conntrack_info ctinfo,
92 struct sk_buff **pskb,
93 struct nf_conntrack_l3proto *l3proto,
94 struct nf_conntrack_l4proto *l4proto);
96 static bcmNatBindHook bcm_nat_bind_hook = NULL;
97 int bcm_nat_bind_hook_func(bcmNatBindHook hook_func) {
98 bcm_nat_bind_hook = hook_func;
99 return 1;
102 typedef int (*bcmNatHitHook)(struct sk_buff *skb);
103 bcmNatHitHook bcm_nat_hit_hook = NULL;
104 int bcm_nat_hit_hook_func(bcmNatHitHook hook_func) {
105 bcm_nat_hit_hook = hook_func;
106 return 1;
108 #endif
111 * This scheme offers various size of "struct nf_conn" dependent on
112 * features(helper, nat, ...)
115 #define NF_CT_FEATURES_NAMELEN 256
116 static struct {
117 /* name of slab cache. printed in /proc/slabinfo */
118 char *name;
120 /* size of slab cache */
121 size_t size;
123 /* slab cache pointer */
124 struct kmem_cache *cachep;
126 /* allocated slab cache + modules which uses this slab cache */
127 int use;
129 } nf_ct_cache[NF_CT_F_NUM];
131 /* protect members of nf_ct_cache except of "use" */
132 DEFINE_RWLOCK(nf_ct_cache_lock);
134 /* This avoids calling kmem_cache_create() with same name simultaneously */
135 static DEFINE_MUTEX(nf_ct_cache_mutex);
137 /*--SZ angela 09.03 { */
138 #define IP_TRACK_SMALL 0x01
139 #define IP_TRACK_PORT 0x02
140 #define IP_TRACK_DATA 0x04
141 #define IP_TRACK_UDP 0x08
143 extern int track_flag;
144 extern ulong ipaddr;
145 u_int8_t port_num_udp[65536];
147 extern unsigned char mbss_prio_1; /*SZ-Angela Add for Multiple SSID */
148 extern unsigned char mbss_prio_2;
149 extern unsigned char mbss_prio_3;
151 //--SZ angela 09.03 }
153 static int nf_conntrack_hash_rnd_initted;
154 static unsigned int nf_conntrack_hash_rnd;
157 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
158 unsigned int size, unsigned int rnd)
160 unsigned int a, b;
162 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
163 (tuple->src.l3num << 16) | tuple->dst.protonum);
164 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
165 (tuple->src.u.all << 16) | tuple->dst.u.all);
167 return jhash_2words(a, b, rnd) % size;
170 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
172 return __hash_conntrack(tuple, nf_conntrack_htable_size,
173 nf_conntrack_hash_rnd);
176 int nf_conntrack_register_cache(u_int32_t features, const char *name,
177 size_t size)
179 int ret = 0;
180 char *cache_name;
181 struct kmem_cache *cachep;
183 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
184 features, name, size);
186 if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) {
187 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
188 features);
189 return -EINVAL;
192 mutex_lock(&nf_ct_cache_mutex);
194 write_lock_bh(&nf_ct_cache_lock);
195 /* e.g: multiple helpers are loaded */
196 if (nf_ct_cache[features].use > 0) {
197 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
198 if ((!strncmp(nf_ct_cache[features].name, name,
199 NF_CT_FEATURES_NAMELEN))
200 && nf_ct_cache[features].size == size) {
201 DEBUGP("nf_conntrack_register_cache: reusing.\n");
202 nf_ct_cache[features].use++;
203 ret = 0;
204 } else
205 ret = -EBUSY;
207 write_unlock_bh(&nf_ct_cache_lock);
208 mutex_unlock(&nf_ct_cache_mutex);
209 return ret;
211 write_unlock_bh(&nf_ct_cache_lock);
214 * The memory space for name of slab cache must be alive until
215 * cache is destroyed.
217 cache_name = kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN, GFP_ATOMIC);
218 if (cache_name == NULL) {
219 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
220 ret = -ENOMEM;
221 goto out_up_mutex;
224 if (strlcpy(cache_name, name, NF_CT_FEATURES_NAMELEN)
225 >= NF_CT_FEATURES_NAMELEN) {
226 printk("nf_conntrack_register_cache: name too long\n");
227 ret = -EINVAL;
228 goto out_free_name;
231 cachep = kmem_cache_create(cache_name, size, 0, 0,
232 NULL, NULL);
233 if (!cachep) {
234 printk("nf_conntrack_register_cache: Can't create slab cache "
235 "for the features = 0x%x\n", features);
236 ret = -ENOMEM;
237 goto out_free_name;
240 write_lock_bh(&nf_ct_cache_lock);
241 nf_ct_cache[features].use = 1;
242 nf_ct_cache[features].size = size;
243 nf_ct_cache[features].cachep = cachep;
244 nf_ct_cache[features].name = cache_name;
245 write_unlock_bh(&nf_ct_cache_lock);
247 goto out_up_mutex;
249 out_free_name:
250 kfree(cache_name);
251 out_up_mutex:
252 mutex_unlock(&nf_ct_cache_mutex);
253 return ret;
255 EXPORT_SYMBOL_GPL(nf_conntrack_register_cache);
257 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
258 void nf_conntrack_unregister_cache(u_int32_t features)
260 struct kmem_cache *cachep;
261 char *name;
264 * This assures that kmem_cache_create() isn't called before destroying
265 * slab cache.
267 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features);
268 mutex_lock(&nf_ct_cache_mutex);
270 write_lock_bh(&nf_ct_cache_lock);
271 if (--nf_ct_cache[features].use > 0) {
272 write_unlock_bh(&nf_ct_cache_lock);
273 mutex_unlock(&nf_ct_cache_mutex);
274 return;
276 cachep = nf_ct_cache[features].cachep;
277 name = nf_ct_cache[features].name;
278 nf_ct_cache[features].cachep = NULL;
279 nf_ct_cache[features].name = NULL;
280 nf_ct_cache[features].size = 0;
281 write_unlock_bh(&nf_ct_cache_lock);
283 synchronize_net();
285 kmem_cache_destroy(cachep);
286 kfree(name);
288 mutex_unlock(&nf_ct_cache_mutex);
290 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache);
293 nf_ct_get_tuple(const struct sk_buff *skb,
294 unsigned int nhoff,
295 unsigned int dataoff,
296 u_int16_t l3num,
297 u_int8_t protonum,
298 struct nf_conntrack_tuple *tuple,
299 const struct nf_conntrack_l3proto *l3proto,
300 const struct nf_conntrack_l4proto *l4proto)
302 NF_CT_TUPLE_U_BLANK(tuple);
304 tuple->src.l3num = l3num;
305 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
306 return 0;
307 tuple->dst.protonum = protonum;
308 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
310 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
312 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
315 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
316 const struct nf_conntrack_tuple *orig,
317 const struct nf_conntrack_l3proto *l3proto,
318 const struct nf_conntrack_l4proto *l4proto)
320 NF_CT_TUPLE_U_BLANK(inverse);
322 inverse->src.l3num = orig->src.l3num;
323 if (l3proto->invert_tuple(inverse, orig) == 0)
324 return 0;
326 inverse->dst.dir = !orig->dst.dir;
328 inverse->dst.protonum = orig->dst.protonum;
329 return l4proto->invert_tuple(inverse, orig);
331 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
333 static void
334 clean_from_lists(struct nf_conn *ct)
336 DEBUGP("clean_from_lists(%p)\n", ct);
337 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
338 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
340 /* Destroy all pending expectations */
341 nf_ct_remove_expectations(ct);
344 static void
345 destroy_conntrack(struct nf_conntrack *nfct)
347 struct nf_conn *ct = (struct nf_conn *)nfct;
348 struct nf_conntrack_l4proto *l4proto;
349 typeof(nf_conntrack_destroyed) destroyed;
351 DEBUGP("destroy_conntrack(%p)\n", ct);
352 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
353 NF_CT_ASSERT(!timer_pending(&ct->timeout));
355 #ifdef HNDCTF
356 ip_conntrack_ipct_delete(ct, 0);
357 #endif /* HNDCTF*/
359 nf_conntrack_event(IPCT_DESTROY, ct);
360 set_bit(IPS_DYING_BIT, &ct->status);
362 /* To make sure we don't get any weird locking issues here:
363 * destroy_conntrack() MUST NOT be called with a write lock
364 * to nf_conntrack_lock!!! -HW */
365 rcu_read_lock();
366 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
367 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
368 if (l4proto && l4proto->destroy)
369 l4proto->destroy(ct);
371 destroyed = rcu_dereference(nf_conntrack_destroyed);
372 if (destroyed)
373 destroyed(ct);
375 rcu_read_unlock();
377 write_lock_bh(&nf_conntrack_lock);
378 /* Expectations will have been removed in clean_from_lists,
379 * except TFTP can create an expectation on the first packet,
380 * before connection is in the list, so we need to clean here,
381 * too. */
382 nf_ct_remove_expectations(ct);
384 #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
385 if(ct->layer7.app_proto)
386 kfree(ct->layer7.app_proto);
387 if(ct->layer7.app_data)
388 kfree(ct->layer7.app_data);
389 #endif
392 /* We overload first tuple to link into unconfirmed list. */
393 if (!nf_ct_is_confirmed(ct)) {
394 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
395 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
398 NF_CT_STAT_INC(delete);
399 write_unlock_bh(&nf_conntrack_lock);
401 if (ct->master)
402 nf_ct_put(ct->master);
404 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
405 nf_conntrack_free(ct);
408 static void death_by_timeout(unsigned long ul_conntrack)
410 struct nf_conn *ct = (void *)ul_conntrack;
411 struct nf_conn_help *help = nfct_help(ct);
412 struct nf_conntrack_helper *helper;
414 #ifdef HNDCTF
415 /* If negative error is returned it means the entry hasn't
416 * timed out yet.
418 if (ip_conntrack_ipct_delete(ct, jiffies >= ct->timeout.expires ? 1 : 0) != 0)
419 return;
420 #endif /* HNDCTF */
422 if (help) {
423 rcu_read_lock();
424 helper = rcu_dereference(help->helper);
425 if (helper && helper->destroy)
426 helper->destroy(ct);
427 rcu_read_unlock();
430 write_lock_bh(&nf_conntrack_lock);
431 /* Inside lock so preempt is disabled on module removal path.
432 * Otherwise we can get spurious warnings. */
433 NF_CT_STAT_INC(delete_list);
434 clean_from_lists(ct);
435 write_unlock_bh(&nf_conntrack_lock);
436 nf_ct_put(ct);
439 struct nf_conntrack_tuple_hash *
440 __nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
441 const struct nf_conn *ignored_conntrack)
443 struct nf_conntrack_tuple_hash *h;
444 unsigned int hash = hash_conntrack(tuple);
446 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
447 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
448 nf_ct_tuple_equal(tuple, &h->tuple)) {
449 NF_CT_STAT_INC(found);
450 return h;
452 NF_CT_STAT_INC(searched);
455 return NULL;
457 EXPORT_SYMBOL_GPL(__nf_conntrack_find);
459 /* Find a connection corresponding to a tuple. */
460 struct nf_conntrack_tuple_hash *
461 nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
462 const struct nf_conn *ignored_conntrack)
464 struct nf_conntrack_tuple_hash *h;
466 read_lock_bh(&nf_conntrack_lock);
467 h = __nf_conntrack_find(tuple, ignored_conntrack);
468 if (h)
469 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
470 read_unlock_bh(&nf_conntrack_lock);
472 return h;
474 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
476 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
477 unsigned int hash,
478 unsigned int repl_hash)
480 ct->id = ++nf_conntrack_next_id;
481 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
482 &nf_conntrack_hash[hash]);
483 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
484 &nf_conntrack_hash[repl_hash]);
487 void nf_conntrack_hash_insert(struct nf_conn *ct)
489 unsigned int hash, repl_hash;
491 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
492 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
494 write_lock_bh(&nf_conntrack_lock);
495 __nf_conntrack_hash_insert(ct, hash, repl_hash);
496 write_unlock_bh(&nf_conntrack_lock);
498 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
500 /* Confirm a connection given skb; places it in hash table */
502 __nf_conntrack_confirm(struct sk_buff **pskb)
504 unsigned int hash, repl_hash;
505 struct nf_conntrack_tuple_hash *h;
506 struct nf_conn *ct;
507 struct nf_conn_help *help;
508 enum ip_conntrack_info ctinfo;
510 ct = nf_ct_get(*pskb, &ctinfo);
512 /* ipt_REJECT uses nf_conntrack_attach to attach related
513 ICMP/TCP RST packets in other direction. Actual packet
514 which created connection will be IP_CT_NEW or for an
515 expected connection, IP_CT_RELATED. */
516 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
517 return NF_ACCEPT;
519 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
520 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
522 /* We're not in hash table, and we refuse to set up related
523 connections for unconfirmed conns. But packet copies and
524 REJECT will give spurious warnings here. */
525 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
527 /* No external references means noone else could have
528 confirmed us. */
529 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
530 DEBUGP("Confirming conntrack %p\n", ct);
532 write_lock_bh(&nf_conntrack_lock);
534 /* See if there's one in the list already, including reverse:
535 NAT could have grabbed it without realizing, since we're
536 not in the hash. If there is, we lost race. */
537 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
538 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
539 &h->tuple))
540 goto out;
541 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
542 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
543 &h->tuple))
544 goto out;
546 /* Remove from unconfirmed list */
547 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
549 __nf_conntrack_hash_insert(ct, hash, repl_hash);
550 /* Timer relative to confirmation time, not original
551 setting time, otherwise we'd get timer wrap in
552 weird delay cases. */
553 ct->timeout.expires += jiffies;
554 add_timer(&ct->timeout);
555 atomic_inc(&ct->ct_general.use);
556 set_bit(IPS_CONFIRMED_BIT, &ct->status);
557 NF_CT_STAT_INC(insert);
558 write_unlock_bh(&nf_conntrack_lock);
559 help = nfct_help(ct);
560 if (help && help->helper)
561 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
562 #ifdef CONFIG_NF_NAT_NEEDED
563 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
564 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
565 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
566 #endif
567 nf_conntrack_event_cache(master_ct(ct) ?
568 IPCT_RELATED : IPCT_NEW, *pskb);
569 return NF_ACCEPT;
571 out:
572 NF_CT_STAT_INC(insert_failed);
573 write_unlock_bh(&nf_conntrack_lock);
574 return NF_DROP;
576 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
578 /* Returns true if a connection correspondings to the tuple (required
579 for NAT). */
581 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
582 const struct nf_conn *ignored_conntrack)
584 struct nf_conntrack_tuple_hash *h;
586 read_lock_bh(&nf_conntrack_lock);
587 h = __nf_conntrack_find(tuple, ignored_conntrack);
588 read_unlock_bh(&nf_conntrack_lock);
590 return h != NULL;
592 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
594 /* There's a small race here where we may free a just-assured
595 connection. Too bad: we're in trouble anyway. */
596 static int early_drop(struct list_head *chain)
598 /* Traverse backwards: gives us oldest, which is roughly LRU */
599 struct nf_conntrack_tuple_hash *h;
600 struct nf_conn *ct = NULL, *tmp;
601 int dropped = 0;
603 read_lock_bh(&nf_conntrack_lock);
604 list_for_each_entry_reverse(h, chain, list) {
605 tmp = nf_ct_tuplehash_to_ctrack(h);
606 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
607 ct = tmp;
608 atomic_inc(&ct->ct_general.use);
609 break;
612 read_unlock_bh(&nf_conntrack_lock);
614 if (!ct)
615 return dropped;
617 #ifdef HNDCTF
618 ip_conntrack_ipct_delete(ct, 0);
619 #endif /* HNDCTF */
621 if (del_timer(&ct->timeout)) {
622 death_by_timeout((unsigned long)ct);
623 dropped = 1;
624 NF_CT_STAT_INC_ATOMIC(early_drop);
626 nf_ct_put(ct);
627 return dropped;
630 static struct nf_conn *
631 __nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
632 const struct nf_conntrack_tuple *repl,
633 const struct nf_conntrack_l3proto *l3proto,
634 u_int32_t features)
636 struct nf_conn *conntrack = NULL;
637 struct nf_conntrack_helper *helper;
639 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
640 get_random_bytes(&nf_conntrack_hash_rnd, 4);
641 nf_conntrack_hash_rnd_initted = 1;
644 /* We don't want any race condition at early drop stage */
645 atomic_inc(&nf_conntrack_count);
647 if (nf_conntrack_max
648 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
649 unsigned int hash = hash_conntrack(orig);
650 /* Try dropping from this hash chain. */
651 if (!early_drop(&nf_conntrack_hash[hash])) {
652 atomic_dec(&nf_conntrack_count);
653 if (net_ratelimit())
654 printk(KERN_WARNING
655 "nf_conntrack: table full, dropping"
656 " packet.\n");
657 return ERR_PTR(-ENOMEM);
661 /* find features needed by this conntrack. */
662 features |= l3proto->get_features(orig);
664 /* FIXME: protect helper list per RCU */
665 read_lock_bh(&nf_conntrack_lock);
666 helper = __nf_ct_helper_find(repl);
667 /* NAT might want to assign a helper later */
668 if (helper || features & NF_CT_F_NAT)
669 features |= NF_CT_F_HELP;
670 read_unlock_bh(&nf_conntrack_lock);
672 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
674 read_lock_bh(&nf_ct_cache_lock);
676 if (unlikely(!nf_ct_cache[features].use)) {
677 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
678 features);
679 goto out;
682 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
683 if (conntrack == NULL) {
684 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
685 goto out;
688 memset(conntrack, 0, nf_ct_cache[features].size);
689 conntrack->features = features;
690 atomic_set(&conntrack->ct_general.use, 1);
691 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
692 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
693 /* Don't set timer yet: wait for confirmation */
694 setup_timer(&conntrack->timeout, death_by_timeout,
695 (unsigned long)conntrack);
696 read_unlock_bh(&nf_ct_cache_lock);
698 return conntrack;
699 out:
700 read_unlock_bh(&nf_ct_cache_lock);
701 atomic_dec(&nf_conntrack_count);
702 return conntrack;
705 struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
706 const struct nf_conntrack_tuple *repl)
708 struct nf_conntrack_l3proto *l3proto;
709 struct nf_conn *ct;
711 rcu_read_lock();
712 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
713 ct = __nf_conntrack_alloc(orig, repl, l3proto, 0);
714 rcu_read_unlock();
716 return ct;
718 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
720 void nf_conntrack_free(struct nf_conn *conntrack)
722 u_int32_t features = conntrack->features;
723 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
724 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
725 conntrack);
726 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
727 atomic_dec(&nf_conntrack_count);
729 EXPORT_SYMBOL_GPL(nf_conntrack_free);
731 /* Allocate a new conntrack: we return -ENOMEM if classification
732 failed due to stress. Otherwise it really is unclassifiable. */
733 static struct nf_conntrack_tuple_hash *
734 init_conntrack(const struct nf_conntrack_tuple *tuple,
735 struct nf_conntrack_l3proto *l3proto,
736 struct nf_conntrack_l4proto *l4proto,
737 struct sk_buff *skb,
738 unsigned int dataoff)
740 struct nf_conn *conntrack;
741 struct nf_conn_help *help;
742 struct nf_conntrack_tuple repl_tuple;
743 struct nf_conntrack_expect *exp;
744 u_int32_t features = 0;
746 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
747 DEBUGP("Can't invert tuple.\n");
748 return NULL;
751 read_lock_bh(&nf_conntrack_lock);
752 exp = __nf_conntrack_expect_find(tuple);
753 if (exp && exp->helper)
754 features = NF_CT_F_HELP;
755 read_unlock_bh(&nf_conntrack_lock);
757 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features);
758 if (conntrack == NULL || IS_ERR(conntrack)) {
759 DEBUGP("Can't allocate conntrack.\n");
760 return (struct nf_conntrack_tuple_hash *)conntrack;
763 if (!l4proto->new(conntrack, skb, dataoff)) {
764 nf_conntrack_free(conntrack);
765 DEBUGP("init conntrack: can't track with proto module\n");
766 return NULL;
769 write_lock_bh(&nf_conntrack_lock);
770 //--SZ angela 09.03 {
771 /* if the qos enable and the layer 3 protocol is ipv4 */
772 if((track_flag == 1) && (strcmp(l3proto->name, "ipv4") == 0)) {
773 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].track.flag = 0;
774 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].track.number =1;
776 if(strcmp(l4proto->name, "udp") == 0) {
777 if(conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip == ipaddr)
778 port_num_udp[conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all]++;
779 else
780 port_num_udp[conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all]++;
783 //--SZ angela 09.03 }
785 exp = find_expectation(tuple);
787 help = nfct_help(conntrack);
788 if (exp) {
789 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
790 conntrack, exp);
791 /* Welcome, Mr. Bond. We've been expecting you... */
792 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
793 conntrack->master = exp->master;
794 if (exp->helper)
795 rcu_assign_pointer(help->helper, exp->helper);
796 #ifdef CONFIG_NF_CONNTRACK_MARK
797 conntrack->mark = exp->master->mark;
798 #endif
799 #ifdef CONFIG_NF_CONNTRACK_SECMARK
800 conntrack->secmark = exp->master->secmark;
801 #endif
802 nf_conntrack_get(&conntrack->master->ct_general);
803 NF_CT_STAT_INC(expect_new);
804 } else {
805 if (help) {
806 /* not in hash table yet, so not strictly necessary */
807 rcu_assign_pointer(help->helper,
808 __nf_ct_helper_find(&repl_tuple));
810 NF_CT_STAT_INC(new);
813 /* Overload tuple linked list to put us in unconfirmed list. */
814 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
816 write_unlock_bh(&nf_conntrack_lock);
818 if (exp) {
819 if (exp->expectfn)
820 exp->expectfn(conntrack, exp);
821 nf_conntrack_expect_put(exp);
824 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
827 //--SZ angela 09.03 {
828 //On success, returns h->track.flags & IP_TRACK_MARK
829 inline int deal_track(struct nf_conntrack_tuple_hash *h, int len)
831 struct nf_conntrack_tuple_hash *rep_h;
832 unsigned int port=0, dport=0;
833 struct nf_conn *ct = NULL;
835 // Add the packet number of this connect track and record the length of the packet
836 h->track.number ++;
837 // if((h->track.flag & IP_TRACK_FULL) != IP_TRACK_FULL)
838 // {
839 if(len > 512)
840 h->track.large_packet++;
841 // }
843 // The download packet set the IP_TRACK_DOWN flag
844 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
846 port = h->tuple.dst.u.all;
847 dport = h->tuple.src.u.all;
849 else
851 port = h->tuple.src.u.all;
852 dport = h->tuple.dst.u.all;
855 // if the connect track is data connect ,we return IP_TRACK_DATA
856 if((h->track.flag & IP_TRACK_DATA) == IP_TRACK_DATA)
857 return IP_TRACK_DATA;
859 // if the destination port of this connect track is one of 80,8080,443.We return IP_TRACK_PORT
860 if((h->track.flag & IP_TRACK_PORT) == IP_TRACK_PORT)
861 return IP_TRACK_PORT;
863 if((h->track.flag & IP_TRACK_SMALL) == IP_TRACK_SMALL)
864 return IP_TRACK_SMALL;
866 if((h->track.flag & IP_TRACK_UDP) == IP_TRACK_UDP)
867 return IP_TRACK_UDP;
869 ct = nf_ct_tuplehash_to_ctrack(h);
871 //start compare
872 if(NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
873 rep_h = &ct->tuplehash[IP_CT_DIR_ORIGINAL];
874 else
875 rep_h = &ct->tuplehash[IP_CT_DIR_REPLY];
876 if(!rep_h)
877 return 0;
879 if(ntohs(dport) == 80 || ntohs(dport) == 8080 || ntohs(dport) == 443)
881 h->track.flag |= IP_TRACK_PORT;
882 rep_h->track.flag |= IP_TRACK_PORT;
883 return IP_TRACK_PORT;
886 // if the port has connections more than 30, we mark it and return IP_TRACK_UDP
887 // h->tuple.dst.protonum == 17 &&
888 if((port_num_udp[port] > 30 || port_num_udp[dport] >30))
890 h->track.flag |= IP_TRACK_UDP;
891 rep_h->track.flag |= IP_TRACK_UDP;
892 return IP_TRACK_UDP;
895 if(h->track.number == 250)
897 if(h->track.large_packet<70)
899 if((rep_h->track.flag & IP_TRACK_DATA) == IP_TRACK_DATA)
901 h->track.flag |= IP_TRACK_DATA;
902 return IP_TRACK_DATA;
904 h->track.flag |= IP_TRACK_SMALL;
905 return IP_TRACK_SMALL;
908 if((rep_h->track.flag & IP_TRACK_SMALL) == IP_TRACK_SMALL)
910 rep_h->track.flag |= IP_TRACK_DATA;
911 rep_h->track.flag &= ~IP_TRACK_SMALL;
913 h->track.flag |= IP_TRACK_DATA;
914 return IP_TRACK_DATA;
917 return 0;
919 //--SZ angela 09.03 }
921 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
922 static inline struct nf_conn *
923 resolve_normal_ct(struct sk_buff *skb,
924 unsigned int dataoff,
925 u_int16_t l3num,
926 u_int8_t protonum,
927 struct nf_conntrack_l3proto *l3proto,
928 struct nf_conntrack_l4proto *l4proto,
929 int *set_reply,
930 enum ip_conntrack_info *ctinfo)
932 struct nf_conntrack_tuple tuple;
933 struct nf_conntrack_tuple_hash *h;
934 struct nf_conn *ct;
936 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
937 dataoff, l3num, protonum, &tuple, l3proto,
938 l4proto)) {
939 DEBUGP("resolve_normal_ct: Can't get tuple\n");
940 return NULL;
943 /* look for tuple match */
944 h = nf_conntrack_find_get(&tuple, NULL);
945 if (!h) {
946 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
947 if (!h)
948 return NULL;
949 if (IS_ERR(h))
950 return (void *)h;
952 /*/--SZ angela 09.03 {*/
953 else if((track_flag == 1) && (strcmp(l3proto->name, "ipv4") == 0)) {
954 write_lock_bh(&nf_conntrack_lock);
956 switch(deal_track(h, ntohs(ip_hdr(skb)->tot_len))) {
957 case IP_TRACK_UDP:
958 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
959 skb->mark = 91;
960 else
961 skb->mark = 51;
962 break;
963 case IP_TRACK_DATA:
964 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
965 skb->mark = 90;
966 else
967 skb->mark = 50;
968 break;
969 case IP_TRACK_PORT:
970 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
971 skb->mark = 80;
972 else
973 skb->mark = 20;
974 break;
975 case IP_TRACK_SMALL:
976 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
977 skb->mark = 70;
978 else
979 skb->mark = 10;
980 break;
981 default:
982 if(ntohl(h->tuple.dst.u3.ip) == ipaddr)
983 skb->mark = 90;
984 else
985 skb->mark = 50;
986 break;
990 switch(skb->wl_idx) /*/SZ-Angela Add for Multiple SSID*/
992 case 2:
993 if(mbss_prio_1 == 0)
994 skb->mark = 60;
995 break;
996 case 4:
997 if(mbss_prio_2 == 0)
998 skb->mark = 60;
999 break;
1000 case 8:
1001 if(mbss_prio_3 == 0)
1002 skb->mark = 60;
1003 break;
1004 default:
1005 break;
1008 write_unlock_bh(&nf_conntrack_lock);
1009 } //--SZ angela 09.03 }
1010 ct = nf_ct_tuplehash_to_ctrack(h);
1012 /* It exists; we have (non-exclusive) reference. */
1013 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1014 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
1015 /* Please set reply bit if this packet OK */
1016 *set_reply = 1;
1017 } else {
1018 /* Once we've had two way comms, always ESTABLISHED. */
1019 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1020 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
1021 *ctinfo = IP_CT_ESTABLISHED;
1022 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1023 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
1024 *ctinfo = IP_CT_RELATED;
1025 } else {
1026 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
1027 *ctinfo = IP_CT_NEW;
1029 *set_reply = 0;
1031 skb->nfct = &ct->ct_general;
1032 skb->nfctinfo = *ctinfo;
1033 return ct;
1036 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1037 extern struct sk_buff * nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user);
1038 #endif
1040 unsigned int
1041 nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
1043 struct nf_conn *ct;
1044 enum ip_conntrack_info ctinfo;
1045 struct nf_conntrack_l3proto *l3proto;
1046 struct nf_conntrack_l4proto *l4proto;
1047 unsigned int dataoff;
1048 u_int8_t protonum;
1049 int set_reply = 0;
1050 int ret;
1051 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1052 struct nf_conn_nat *nat;
1053 struct nf_conn_help *help;
1054 #endif
1056 /* Previously seen (loopback or untracked)? Ignore. */
1057 if ((*pskb)->nfct) {
1058 NF_CT_STAT_INC_ATOMIC(ignore);
1059 return NF_ACCEPT;
1062 /* rcu_read_lock()ed by nf_hook_slow */
1063 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
1065 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
1066 DEBUGP("not prepared to track yet or error occured\n");
1067 return -ret;
1070 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1071 if (pf == PF_INET) {
1072 /* Gather fragments. */
1073 if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1074 *pskb = nf_ct_ipv4_gather_frags(*pskb,
1075 hooknum == NF_IP_PRE_ROUTING ?
1076 IP_DEFRAG_CONNTRACK_IN :
1077 IP_DEFRAG_CONNTRACK_OUT);
1078 if (!*pskb)
1079 return NF_STOLEN;
1082 #endif
1084 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
1086 /* It may be an special packet, error, unclean...
1087 * inverse of the return code tells to the netfilter
1088 * core what to do with the packet. */
1089 if (l4proto->error != NULL &&
1090 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
1091 NF_CT_STAT_INC_ATOMIC(error);
1092 NF_CT_STAT_INC_ATOMIC(invalid);
1093 return -ret;
1096 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
1097 &set_reply, &ctinfo);
1098 if (!ct) {
1099 /* Not valid part of a connection */
1100 NF_CT_STAT_INC_ATOMIC(invalid);
1101 return NF_ACCEPT;
1104 if (IS_ERR(ct)) {
1105 /* Too stressed to deal. */
1106 NF_CT_STAT_INC_ATOMIC(drop);
1107 return NF_DROP;
1110 NF_CT_ASSERT((*pskb)->nfct);
1112 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
1113 if (ret <= 0) {
1114 /* Invalid: inverse of the return code tells
1115 * the netfilter core what to do */
1116 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
1117 nf_conntrack_put((*pskb)->nfct);
1118 (*pskb)->nfct = NULL;
1119 NF_CT_STAT_INC_ATOMIC(invalid);
1120 return -ret;
1123 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1124 help = nfct_help(ct);
1125 nat = nfct_nat(ct);
1126 if (pf == PF_INET && ipv4_conntrack_fastnat && bcm_nat_bind_hook
1127 && !(nat->info.nat_type & BCM_FASTNAT_DENY)
1128 && !help->helper
1129 && (ctinfo == IP_CT_ESTABLISHED || ctinfo == IP_CT_IS_REPLY)
1130 && (hooknum == NF_IP_PRE_ROUTING) &&
1131 (protonum == IPPROTO_TCP || protonum == IPPROTO_UDP)) {
1133 struct nf_conntrack_tuple *t1, *t2;
1134 t1 = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
1135 t2 = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
1136 if (!(t1->dst.u3.ip == t2->src.u3.ip &&
1137 t1->src.u3.ip == t2->dst.u3.ip &&
1138 t1->dst.u.all == t2->src.u.all &&
1139 t1->src.u.all == t2->dst.u.all)) {
1140 ret = bcm_nat_bind_hook(ct, ctinfo, pskb, l3proto, l4proto);
1143 #endif
1145 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1146 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1147 if (pf == PF_INET && hooknum == NF_IP_LOCAL_OUT)
1148 nat->info.nat_type |= BCM_FASTNAT_DENY;
1149 #endif
1150 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
1152 return ret;
1154 EXPORT_SYMBOL_GPL(nf_conntrack_in);
1156 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1157 const struct nf_conntrack_tuple *orig)
1159 int ret;
1161 rcu_read_lock();
1162 ret = nf_ct_invert_tuple(inverse, orig,
1163 __nf_ct_l3proto_find(orig->src.l3num),
1164 __nf_ct_l4proto_find(orig->src.l3num,
1165 orig->dst.protonum));
1166 rcu_read_unlock();
1167 return ret;
1169 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
1171 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
1172 implicitly racy: see __nf_conntrack_confirm */
1173 void nf_conntrack_alter_reply(struct nf_conn *ct,
1174 const struct nf_conntrack_tuple *newreply)
1176 struct nf_conn_help *help = nfct_help(ct);
1178 write_lock_bh(&nf_conntrack_lock);
1179 /* Should be unconfirmed, so not in hash table yet */
1180 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1182 DEBUGP("Altering reply tuple of %p to ", ct);
1183 NF_CT_DUMP_TUPLE(newreply);
1185 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
1186 if (!ct->master && help && help->expecting == 0) {
1187 struct nf_conntrack_helper *helper;
1188 helper = __nf_ct_helper_find(newreply);
1189 if (helper)
1190 memset(&help->help, 0, sizeof(help->help));
1191 /* not in hash table yet, so not strictly necessary */
1192 rcu_assign_pointer(help->helper, helper);
1194 write_unlock_bh(&nf_conntrack_lock);
1196 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
1198 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1199 void __nf_ct_refresh_acct(struct nf_conn *ct,
1200 enum ip_conntrack_info ctinfo,
1201 const struct sk_buff *skb,
1202 unsigned long extra_jiffies,
1203 int do_acct)
1205 int event = 0;
1207 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
1208 NF_CT_ASSERT(skb);
1210 write_lock_bh(&nf_conntrack_lock);
1212 /* Only update if this is not a fixed timeout */
1213 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
1214 write_unlock_bh(&nf_conntrack_lock);
1215 return;
1218 /* If not in hash table, timer will not be active yet */
1219 if (!nf_ct_is_confirmed(ct)) {
1220 #ifdef HNDCTF
1221 ct->expire_jiffies = extra_jiffies;
1222 #endif /* HNDCTF */
1223 ct->timeout.expires = extra_jiffies;
1224 event = IPCT_REFRESH;
1225 } else {
1226 unsigned long newtime = jiffies + extra_jiffies;
1228 /* Only update the timeout if the new timeout is at least
1229 HZ jiffies from the old timeout. Need del_timer for race
1230 avoidance (may already be dying). */
1231 if (newtime - ct->timeout.expires >= HZ
1232 && del_timer(&ct->timeout)) {
1233 #ifdef HNDCTF
1234 ct->expire_jiffies = extra_jiffies;
1235 #endif /* HNDCTF */
1236 ct->timeout.expires = newtime;
1237 add_timer(&ct->timeout);
1238 event = IPCT_REFRESH;
1242 #ifdef CONFIG_NF_CT_ACCT
1243 if (do_acct) {
1244 ct->counters[CTINFO2DIR(ctinfo)].packets++;
1245 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
1246 skb->len - skb_network_offset(skb);
1248 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
1249 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
1250 event |= IPCT_COUNTER_FILLING;
1252 #endif
1254 write_unlock_bh(&nf_conntrack_lock);
1256 /* must be unlocked when calling event cache */
1257 if (event)
1258 nf_conntrack_event_cache(event, skb);
1260 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
1262 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1264 #include <linux/netfilter/nfnetlink.h>
1265 #include <linux/netfilter/nfnetlink_conntrack.h>
1266 #include <linux/mutex.h>
1269 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1270 * in ip_conntrack_core, since we don't want the protocols to autoload
1271 * or depend on ctnetlink */
1272 int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
1273 const struct nf_conntrack_tuple *tuple)
1275 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
1276 &tuple->src.u.tcp.port);
1277 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
1278 &tuple->dst.u.tcp.port);
1279 return 0;
1281 nfattr_failure:
1282 return -1;
1284 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
1286 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
1287 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
1288 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
1291 int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
1292 struct nf_conntrack_tuple *t)
1294 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
1295 return -EINVAL;
1297 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
1298 return -EINVAL;
1300 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
1301 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
1303 return 0;
1305 EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
1306 #endif
1308 /* Used by ipt_REJECT and ip6t_REJECT. */
1309 void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
1311 struct nf_conn *ct;
1312 enum ip_conntrack_info ctinfo;
1314 /* This ICMP is in reverse direction to the packet which caused it */
1315 ct = nf_ct_get(skb, &ctinfo);
1316 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1317 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
1318 else
1319 ctinfo = IP_CT_RELATED;
1321 /* Attach to new skbuff, and increment count */
1322 nskb->nfct = &ct->ct_general;
1323 nskb->nfctinfo = ctinfo;
1324 nf_conntrack_get(nskb->nfct);
1326 EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
1328 static inline int
1329 do_iter(const struct nf_conntrack_tuple_hash *i,
1330 int (*iter)(struct nf_conn *i, void *data),
1331 void *data)
1333 return iter(nf_ct_tuplehash_to_ctrack(i), data);
1336 /* Bring out ya dead! */
1337 static struct nf_conn *
1338 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1339 void *data, unsigned int *bucket)
1341 struct nf_conntrack_tuple_hash *h;
1342 struct nf_conn *ct;
1344 write_lock_bh(&nf_conntrack_lock);
1345 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1346 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
1347 ct = nf_ct_tuplehash_to_ctrack(h);
1348 if (iter(ct, data))
1349 goto found;
1352 list_for_each_entry(h, &unconfirmed, list) {
1353 ct = nf_ct_tuplehash_to_ctrack(h);
1354 if (iter(ct, data))
1355 set_bit(IPS_DYING_BIT, &ct->status);
1357 write_unlock_bh(&nf_conntrack_lock);
1358 return NULL;
1359 found:
1360 atomic_inc(&ct->ct_general.use);
1361 write_unlock_bh(&nf_conntrack_lock);
1362 return ct;
1365 void
1366 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1368 struct nf_conn *ct;
1369 unsigned int bucket = 0;
1371 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
1372 #ifdef HNDCTF
1373 ip_conntrack_ipct_delete(ct, 0);
1374 #endif /* HNDCTF */
1375 /* Time to push up daises... */
1376 if (del_timer(&ct->timeout))
1377 death_by_timeout((unsigned long)ct);
1378 /* ... else the timer will get him soon. */
1380 nf_ct_put(ct);
1383 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1385 static int kill_all(struct nf_conn *i, void *data)
1387 return 1;
1390 static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1392 if (vmalloced)
1393 vfree(hash);
1394 else
1395 free_pages((unsigned long)hash,
1396 get_order(sizeof(struct list_head) * size));
1399 void nf_conntrack_flush(void)
1401 nf_ct_iterate_cleanup(kill_all, NULL);
1403 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
1405 /* Mishearing the voices in his head, our hero wonders how he's
1406 supposed to kill the mall. */
1407 void nf_conntrack_cleanup(void)
1409 int i;
1411 rcu_assign_pointer(ip_ct_attach, NULL);
1413 /* This makes sure all current packets have passed through
1414 netfilter framework. Roll on, two-stage module
1415 delete... */
1416 synchronize_net();
1418 nf_ct_event_cache_flush();
1419 i_see_dead_people:
1420 nf_conntrack_flush();
1421 if (atomic_read(&nf_conntrack_count) != 0) {
1422 schedule();
1423 goto i_see_dead_people;
1425 /* wait until all references to nf_conntrack_untracked are dropped */
1426 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1427 schedule();
1429 rcu_assign_pointer(nf_ct_destroy, NULL);
1431 for (i = 0; i < NF_CT_F_NUM; i++) {
1432 if (nf_ct_cache[i].use == 0)
1433 continue;
1435 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1436 nf_ct_cache[i].use = 1;
1437 nf_conntrack_unregister_cache(i);
1439 kmem_cache_destroy(nf_conntrack_expect_cachep);
1440 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1441 nf_conntrack_htable_size);
1443 nf_conntrack_proto_fini();
1446 static struct list_head *alloc_hashtable(int size, int *vmalloced)
1448 struct list_head *hash;
1449 unsigned int i;
1451 *vmalloced = 0;
1452 hash = (void*)__get_free_pages(GFP_KERNEL,
1453 get_order(sizeof(struct list_head)
1454 * size));
1455 if (!hash) {
1456 *vmalloced = 1;
1457 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1458 hash = vmalloc(sizeof(struct list_head) * size);
1461 if (hash)
1462 for (i = 0; i < size; i++)
1463 INIT_LIST_HEAD(&hash[i]);
1465 return hash;
1468 int set_hashsize(const char *val, struct kernel_param *kp)
1470 int i, bucket, hashsize, vmalloced;
1471 int old_vmalloced, old_size;
1472 int rnd;
1473 struct list_head *hash, *old_hash;
1474 struct nf_conntrack_tuple_hash *h;
1476 /* On boot, we can set this without any fancy locking. */
1477 if (!nf_conntrack_htable_size)
1478 return param_set_uint(val, kp);
1480 hashsize = simple_strtol(val, NULL, 0);
1481 if (!hashsize)
1482 return -EINVAL;
1484 hash = alloc_hashtable(hashsize, &vmalloced);
1485 if (!hash)
1486 return -ENOMEM;
1488 /* We have to rehahs for the new table anyway, so we also can
1489 * use a newrandom seed */
1490 get_random_bytes(&rnd, 4);
1492 write_lock_bh(&nf_conntrack_lock);
1493 for (i = 0; i < nf_conntrack_htable_size; i++) {
1494 while (!list_empty(&nf_conntrack_hash[i])) {
1495 h = list_entry(nf_conntrack_hash[i].next,
1496 struct nf_conntrack_tuple_hash, list);
1497 list_del(&h->list);
1498 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1499 list_add_tail(&h->list, &hash[bucket]);
1502 old_size = nf_conntrack_htable_size;
1503 old_vmalloced = nf_conntrack_vmalloc;
1504 old_hash = nf_conntrack_hash;
1506 nf_conntrack_htable_size = hashsize;
1507 nf_conntrack_vmalloc = vmalloced;
1508 nf_conntrack_hash = hash;
1509 nf_conntrack_hash_rnd = rnd;
1510 write_unlock_bh(&nf_conntrack_lock);
1512 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1513 return 0;
1516 module_param_call(hashsize, set_hashsize, param_get_uint,
1517 &nf_conntrack_htable_size, 0600);
1519 int __init nf_conntrack_init(void)
1521 int ret;
1523 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1524 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1525 if (!nf_conntrack_htable_size) {
1526 nf_conntrack_htable_size
1527 = (((num_physpages << PAGE_SHIFT) / 16384)
1528 / sizeof(struct list_head));
1529 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1530 nf_conntrack_htable_size = 8192;
1531 if (nf_conntrack_htable_size < 16)
1532 nf_conntrack_htable_size = 16;
1534 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1536 for(ret=0; ret<65535; ret++) //--SZ Angela 09.03 QOS Initialization
1537 port_num_udp[ret]=0;
1539 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1540 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1541 nf_conntrack_max);
1543 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1544 &nf_conntrack_vmalloc);
1545 if (!nf_conntrack_hash) {
1546 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1547 goto err_out;
1550 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
1551 sizeof(struct nf_conn));
1552 if (ret < 0) {
1553 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1554 goto err_free_hash;
1557 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1558 sizeof(struct nf_conntrack_expect),
1559 0, 0, NULL, NULL);
1560 if (!nf_conntrack_expect_cachep) {
1561 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1562 goto err_free_conntrack_slab;
1565 ret = nf_conntrack_proto_init();
1566 if (ret < 0)
1567 goto out_free_expect_slab;
1569 /* For use by REJECT target */
1570 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
1571 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
1573 /* Set up fake conntrack:
1574 - to never be deleted, not in any hashes */
1575 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1576 /* - and look it like as a confirmed connection */
1577 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1579 return ret;
1581 out_free_expect_slab:
1582 kmem_cache_destroy(nf_conntrack_expect_cachep);
1583 err_free_conntrack_slab:
1584 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1585 err_free_hash:
1586 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1587 nf_conntrack_htable_size);
1588 err_out:
1589 return -ENOMEM;