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 /* Horrible trick to figure out smallest amount worth kmallocing. */
23 #define CACHE(x) (x) + 0 *
26 #include <linux/kmalloc_sizes.h>
30 void __nf_ct_ext_destroy(struct nf_conn *ct)
33 struct nf_ct_ext_type *t;
35 for (i = 0; i < NF_CT_EXT_NUM; i++) {
36 if (!nf_ct_ext_exist(ct, i))
40 t = rcu_dereference(nf_ct_ext_types[i]);
42 /* Here the nf_ct_ext_type might have been unregisterd.
43 * I.e., it has responsible to cleanup private
44 * area in all conntracks when it is unregisterd.
51 EXPORT_SYMBOL(__nf_ct_ext_destroy);
54 nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
56 unsigned int off, len, real_len;
57 struct nf_ct_ext_type *t;
60 t = rcu_dereference(nf_ct_ext_types[id]);
62 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
64 real_len = t->alloc_size;
67 *ext = kzalloc(real_len, gfp);
71 (*ext)->offset[id] = off;
73 (*ext)->real_len = real_len;
75 return (void *)(*ext) + off;
78 void *__nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
80 struct nf_ct_ext *new;
81 int i, newlen, newoff;
82 struct nf_ct_ext_type *t;
85 return nf_ct_ext_create(&ct->ext, id, gfp);
87 if (nf_ct_ext_exist(ct, id))
91 t = rcu_dereference(nf_ct_ext_types[id]);
94 newoff = ALIGN(ct->ext->len, t->align);
95 newlen = newoff + t->len;
98 if (newlen >= ct->ext->real_len) {
99 new = kmalloc(newlen, gfp);
103 memcpy(new, ct->ext, ct->ext->len);
105 for (i = 0; i < NF_CT_EXT_NUM; i++) {
106 if (!nf_ct_ext_exist(ct, i))
110 t = rcu_dereference(nf_ct_ext_types[i]);
112 t->move((void *)new + new->offset[i],
113 (void *)ct->ext + ct->ext->offset[i]);
117 new->real_len = newlen;
121 ct->ext->offset[id] = newoff;
122 ct->ext->len = newlen;
123 memset((void *)ct->ext + newoff, 0, newlen - newoff);
124 return (void *)ct->ext + newoff;
126 EXPORT_SYMBOL(__nf_ct_ext_add);
128 static void update_alloc_size(struct nf_ct_ext_type *type)
131 struct nf_ct_ext_type *t1, *t2;
132 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
134 /* unnecessary to update all types */
135 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
140 /* This assumes that extended areas in conntrack for the types
141 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
142 for (i = min; i <= max; i++) {
143 t1 = nf_ct_ext_types[i];
147 t1->alloc_size = sizeof(struct nf_ct_ext)
148 + ALIGN(sizeof(struct nf_ct_ext), t1->align)
150 for (j = 0; j < NF_CT_EXT_NUM; j++) {
151 t2 = nf_ct_ext_types[j];
152 if (t2 == NULL || t2 == t1 ||
153 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
156 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
159 if (t1->alloc_size < NF_CT_EXT_MIN_SIZE)
160 t1->alloc_size = NF_CT_EXT_MIN_SIZE;
164 /* This MUST be called in process context. */
165 int nf_ct_extend_register(struct nf_ct_ext_type *type)
169 mutex_lock(&nf_ct_ext_type_mutex);
170 if (nf_ct_ext_types[type->id]) {
175 /* This ensures that nf_ct_ext_create() can allocate enough area
176 before updating alloc_size */
177 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
179 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
180 update_alloc_size(type);
182 mutex_unlock(&nf_ct_ext_type_mutex);
185 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
187 /* This MUST be called in process context. */
188 void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
190 mutex_lock(&nf_ct_ext_type_mutex);
191 rcu_assign_pointer(nf_ct_ext_types[type->id], NULL);
192 update_alloc_size(type);
193 mutex_unlock(&nf_ct_ext_type_mutex);
196 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);