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 void __nf_ct_ext_destroy(struct nf_conn *ct)
25 struct nf_ct_ext_type *t;
27 for (i = 0; i < NF_CT_EXT_NUM; i++) {
28 if (!nf_ct_ext_exist(ct, i))
32 t = rcu_dereference(nf_ct_ext_types[i]);
34 /* Here the nf_ct_ext_type might have been unregisterd.
35 * I.e., it has responsible to cleanup private
36 * area in all conntracks when it is unregisterd.
43 EXPORT_SYMBOL(__nf_ct_ext_destroy);
46 nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
48 unsigned int off, len;
49 struct nf_ct_ext_type *t;
52 t = rcu_dereference(nf_ct_ext_types[id]);
54 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
58 *ext = kzalloc(t->alloc_size, gfp);
62 (*ext)->offset[id] = off;
65 return (void *)(*ext) + off;
68 void *__nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
70 struct nf_ct_ext *new;
71 int i, newlen, newoff;
72 struct nf_ct_ext_type *t;
74 /* Conntrack must not be confirmed to avoid races on reallocation. */
75 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
78 return nf_ct_ext_create(&ct->ext, id, gfp);
80 if (nf_ct_ext_exist(ct, id))
84 t = rcu_dereference(nf_ct_ext_types[id]);
87 newoff = ALIGN(ct->ext->len, t->align);
88 newlen = newoff + t->len;
91 if (newlen >= ksize(ct->ext)) {
92 new = kmalloc(newlen, gfp);
96 memcpy(new, ct->ext, ct->ext->len);
98 for (i = 0; i < NF_CT_EXT_NUM; i++) {
99 if (!nf_ct_ext_exist(ct, i))
103 t = rcu_dereference(nf_ct_ext_types[i]);
105 t->move((void *)new + new->offset[i],
106 (void *)ct->ext + ct->ext->offset[i]);
113 ct->ext->offset[id] = newoff;
114 ct->ext->len = newlen;
115 memset((void *)ct->ext + newoff, 0, newlen - newoff);
116 return (void *)ct->ext + newoff;
118 EXPORT_SYMBOL(__nf_ct_ext_add);
120 static void update_alloc_size(struct nf_ct_ext_type *type)
123 struct nf_ct_ext_type *t1, *t2;
124 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
126 /* unnecessary to update all types */
127 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
132 /* This assumes that extended areas in conntrack for the types
133 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
134 for (i = min; i <= max; i++) {
135 t1 = nf_ct_ext_types[i];
139 t1->alloc_size = sizeof(struct nf_ct_ext)
140 + ALIGN(sizeof(struct nf_ct_ext), t1->align)
142 for (j = 0; j < NF_CT_EXT_NUM; j++) {
143 t2 = nf_ct_ext_types[j];
144 if (t2 == NULL || t2 == t1 ||
145 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
148 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
154 /* This MUST be called in process context. */
155 int nf_ct_extend_register(struct nf_ct_ext_type *type)
159 mutex_lock(&nf_ct_ext_type_mutex);
160 if (nf_ct_ext_types[type->id]) {
165 /* This ensures that nf_ct_ext_create() can allocate enough area
166 before updating alloc_size */
167 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
169 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
170 update_alloc_size(type);
172 mutex_unlock(&nf_ct_ext_type_mutex);
175 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
177 /* This MUST be called in process context. */
178 void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
180 mutex_lock(&nf_ct_ext_type_mutex);
181 rcu_assign_pointer(nf_ct_ext_types[type->id], NULL);
182 update_alloc_size(type);
183 mutex_unlock(&nf_ct_ext_type_mutex);
186 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);