Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / netfilter / nf_conntrack_extend.c
blob025aeb4cf959d3de69d81d0957d92a495e778467
1 /* Structure dynamic extension infrastructure
2 * Copyright (C) 2004 Rusty Russell IBM Corporation
3 * Copyright (C) 2007 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (C) 2007 USAGI/WIDE Project <http://www.linux-ipv6.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/rcupdate.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <net/netfilter/nf_conntrack_extend.h>
19 static struct nf_ct_ext_type *nf_ct_ext_types[NF_CT_EXT_NUM];
20 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
22 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
23 /* Horrible trick to figure out smallest amount worth kmallocing. */
24 #define CACHE(x) (x) + 0 *
25 enum {
26 NF_CT_EXT_MIN_SIZE =
27 #include <linux/kmalloc_sizes.h>
28 1 };
29 #undef CACHE
31 =======
32 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
33 void __nf_ct_ext_destroy(struct nf_conn *ct)
35 unsigned int i;
36 struct nf_ct_ext_type *t;
38 for (i = 0; i < NF_CT_EXT_NUM; i++) {
39 if (!nf_ct_ext_exist(ct, i))
40 continue;
42 rcu_read_lock();
43 t = rcu_dereference(nf_ct_ext_types[i]);
45 /* Here the nf_ct_ext_type might have been unregisterd.
46 * I.e., it has responsible to cleanup private
47 * area in all conntracks when it is unregisterd.
49 if (t && t->destroy)
50 t->destroy(ct);
51 rcu_read_unlock();
54 EXPORT_SYMBOL(__nf_ct_ext_destroy);
56 static void *
57 nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
59 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
60 unsigned int off, len, real_len;
61 =======
62 unsigned int off, len;
63 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
64 struct nf_ct_ext_type *t;
66 rcu_read_lock();
67 t = rcu_dereference(nf_ct_ext_types[id]);
68 BUG_ON(t == NULL);
69 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
70 len = off + t->len;
71 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
72 real_len = t->alloc_size;
73 =======
74 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
75 rcu_read_unlock();
77 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
78 *ext = kzalloc(real_len, gfp);
79 =======
80 *ext = kzalloc(t->alloc_size, gfp);
81 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
82 if (!*ext)
83 return NULL;
85 (*ext)->offset[id] = off;
86 (*ext)->len = len;
87 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
88 (*ext)->real_len = real_len;
89 =======
90 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
92 return (void *)(*ext) + off;
95 void *__nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
97 struct nf_ct_ext *new;
98 int i, newlen, newoff;
99 struct nf_ct_ext_type *t;
101 if (!ct->ext)
102 return nf_ct_ext_create(&ct->ext, id, gfp);
104 if (nf_ct_ext_exist(ct, id))
105 return NULL;
107 rcu_read_lock();
108 t = rcu_dereference(nf_ct_ext_types[id]);
109 BUG_ON(t == NULL);
111 newoff = ALIGN(ct->ext->len, t->align);
112 newlen = newoff + t->len;
113 rcu_read_unlock();
115 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
116 if (newlen >= ct->ext->real_len) {
117 =======
118 if (newlen >= ksize(ct->ext)) {
119 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
120 new = kmalloc(newlen, gfp);
121 if (!new)
122 return NULL;
124 memcpy(new, ct->ext, ct->ext->len);
126 for (i = 0; i < NF_CT_EXT_NUM; i++) {
127 if (!nf_ct_ext_exist(ct, i))
128 continue;
130 rcu_read_lock();
131 t = rcu_dereference(nf_ct_ext_types[i]);
132 if (t && t->move)
133 t->move((void *)new + new->offset[i],
134 (void *)ct->ext + ct->ext->offset[i]);
135 rcu_read_unlock();
137 kfree(ct->ext);
138 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
139 new->real_len = newlen;
140 =======
141 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
142 ct->ext = new;
145 ct->ext->offset[id] = newoff;
146 ct->ext->len = newlen;
147 memset((void *)ct->ext + newoff, 0, newlen - newoff);
148 return (void *)ct->ext + newoff;
150 EXPORT_SYMBOL(__nf_ct_ext_add);
152 static void update_alloc_size(struct nf_ct_ext_type *type)
154 int i, j;
155 struct nf_ct_ext_type *t1, *t2;
156 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
158 /* unnecessary to update all types */
159 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
160 min = type->id;
161 max = type->id;
164 /* This assumes that extended areas in conntrack for the types
165 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
166 for (i = min; i <= max; i++) {
167 t1 = nf_ct_ext_types[i];
168 if (!t1)
169 continue;
171 t1->alloc_size = sizeof(struct nf_ct_ext)
172 + ALIGN(sizeof(struct nf_ct_ext), t1->align)
173 + t1->len;
174 for (j = 0; j < NF_CT_EXT_NUM; j++) {
175 t2 = nf_ct_ext_types[j];
176 if (t2 == NULL || t2 == t1 ||
177 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
178 continue;
180 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
181 + t2->len;
183 <<<<<<< HEAD:net/netfilter/nf_conntrack_extend.c
184 if (t1->alloc_size < NF_CT_EXT_MIN_SIZE)
185 t1->alloc_size = NF_CT_EXT_MIN_SIZE;
186 =======
187 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nf_conntrack_extend.c
191 /* This MUST be called in process context. */
192 int nf_ct_extend_register(struct nf_ct_ext_type *type)
194 int ret = 0;
196 mutex_lock(&nf_ct_ext_type_mutex);
197 if (nf_ct_ext_types[type->id]) {
198 ret = -EBUSY;
199 goto out;
202 /* This ensures that nf_ct_ext_create() can allocate enough area
203 before updating alloc_size */
204 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
205 + type->len;
206 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
207 update_alloc_size(type);
208 out:
209 mutex_unlock(&nf_ct_ext_type_mutex);
210 return ret;
212 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
214 /* This MUST be called in process context. */
215 void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
217 mutex_lock(&nf_ct_ext_type_mutex);
218 rcu_assign_pointer(nf_ct_ext_types[type->id], NULL);
219 update_alloc_size(type);
220 mutex_unlock(&nf_ct_ext_type_mutex);
221 synchronize_rcu();
223 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);