]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/inet_hashtables.c
[INET]: Generalise tcp_bind_hash & tcp_inherit_port
[linux-2.6-omap-h63xx.git] / net / ipv4 / inet_hashtables.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              Generic INET transport hashtables
7  *
8  * Authors:     Lotsa people, from code originally in tcp
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19
20 #include <net/inet_hashtables.h>
21
22 /*
23  * Allocate and initialize a new local port bind bucket.
24  * The bindhash mutex for snum's hash chain must be held here.
25  */
26 struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
27                                                  struct inet_bind_hashbucket *head,
28                                                  const unsigned short snum)
29 {
30         struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
31
32         if (tb != NULL) {
33                 tb->port      = snum;
34                 tb->fastreuse = 0;
35                 INIT_HLIST_HEAD(&tb->owners);
36                 hlist_add_head(&tb->node, &head->chain);
37         }
38         return tb;
39 }
40
41 EXPORT_SYMBOL(inet_bind_bucket_create);
42
43 /*
44  * Caller must hold hashbucket lock for this tb with local BH disabled
45  */
46 void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
47 {
48         if (hlist_empty(&tb->owners)) {
49                 __hlist_del(&tb->node);
50                 kmem_cache_free(cachep, tb);
51         }
52 }
53
54 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
55                     const unsigned short snum)
56 {
57         struct inet_sock *inet = inet_sk(sk);
58         inet->num       = snum;
59         sk_add_bind_node(sk, &tb->owners);
60         inet->bind_hash = tb;
61 }
62
63 EXPORT_SYMBOL(inet_bind_hash);
64
65 /*
66  * Get rid of any references to a local port held by the given sock.
67  */
68 static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
69 {
70         struct inet_sock *inet = inet_sk(sk);
71         const int bhash = inet_bhashfn(inet->num, hashinfo->bhash_size);
72         struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
73         struct inet_bind_bucket *tb;
74
75         spin_lock(&head->lock);
76         tb = inet->bind_hash;
77         __sk_del_bind_node(sk);
78         inet->bind_hash = NULL;
79         inet->num = 0;
80         inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
81         spin_unlock(&head->lock);
82 }
83
84 void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
85 {
86         local_bh_disable();
87         __inet_put_port(hashinfo, sk);
88         local_bh_enable();
89 }
90
91 EXPORT_SYMBOL(inet_put_port);