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
;
26 struct nf_ct_ext
*ext
= ct
->ext
;
28 for (i
= 0; i
< NF_CT_EXT_NUM
; i
++) {
29 if (!__nf_ct_ext_exist(ext
, i
))
33 t
= rcu_dereference(nf_ct_ext_types
[i
]);
35 /* Here the nf_ct_ext_type might have been unregisterd.
36 * I.e., it has responsible to cleanup private
37 * area in all conntracks when it is unregisterd.
44 EXPORT_SYMBOL(__nf_ct_ext_destroy
);
47 nf_ct_ext_create(struct nf_ct_ext
**ext
, enum nf_ct_ext_id id
, gfp_t gfp
)
49 unsigned int off
, len
;
50 struct nf_ct_ext_type
*t
;
53 t
= rcu_dereference(nf_ct_ext_types
[id
]);
55 off
= ALIGN(sizeof(struct nf_ct_ext
), t
->align
);
59 *ext
= kzalloc(t
->alloc_size
, gfp
);
63 (*ext
)->offset
[id
] = off
;
66 return (void *)(*ext
) + off
;
69 static void __nf_ct_ext_free_rcu(struct rcu_head
*head
)
71 struct nf_ct_ext
*ext
= container_of(head
, struct nf_ct_ext
, rcu
);
75 void *__nf_ct_ext_add(struct nf_conn
*ct
, enum nf_ct_ext_id id
, gfp_t gfp
)
77 struct nf_ct_ext
*old
, *new;
78 int i
, newlen
, newoff
;
79 struct nf_ct_ext_type
*t
;
81 /* Conntrack must not be confirmed to avoid races on reallocation. */
82 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
86 return nf_ct_ext_create(&ct
->ext
, id
, gfp
);
88 if (__nf_ct_ext_exist(old
, id
))
92 t
= rcu_dereference(nf_ct_ext_types
[id
]);
95 newoff
= ALIGN(old
->len
, t
->align
);
96 newlen
= newoff
+ t
->len
;
99 new = __krealloc(old
, newlen
, gfp
);
104 for (i
= 0; i
< NF_CT_EXT_NUM
; i
++) {
105 if (!__nf_ct_ext_exist(old
, i
))
109 t
= rcu_dereference(nf_ct_ext_types
[i
]);
111 t
->move((void *)new + new->offset
[i
],
112 (void *)old
+ old
->offset
[i
]);
115 call_rcu(&old
->rcu
, __nf_ct_ext_free_rcu
);
119 new->offset
[id
] = newoff
;
121 memset((void *)new + newoff
, 0, newlen
- newoff
);
122 return (void *)new + newoff
;
124 EXPORT_SYMBOL(__nf_ct_ext_add
);
126 static void update_alloc_size(struct nf_ct_ext_type
*type
)
129 struct nf_ct_ext_type
*t1
, *t2
;
130 enum nf_ct_ext_id min
= 0, max
= NF_CT_EXT_NUM
- 1;
132 /* unnecessary to update all types */
133 if ((type
->flags
& NF_CT_EXT_F_PREALLOC
) == 0) {
138 /* This assumes that extended areas in conntrack for the types
139 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
140 for (i
= min
; i
<= max
; i
++) {
141 t1
= nf_ct_ext_types
[i
];
145 t1
->alloc_size
= sizeof(struct nf_ct_ext
)
146 + ALIGN(sizeof(struct nf_ct_ext
), t1
->align
)
148 for (j
= 0; j
< NF_CT_EXT_NUM
; j
++) {
149 t2
= nf_ct_ext_types
[j
];
150 if (t2
== NULL
|| t2
== t1
||
151 (t2
->flags
& NF_CT_EXT_F_PREALLOC
) == 0)
154 t1
->alloc_size
= ALIGN(t1
->alloc_size
, t2
->align
)
160 /* This MUST be called in process context. */
161 int nf_ct_extend_register(struct nf_ct_ext_type
*type
)
165 mutex_lock(&nf_ct_ext_type_mutex
);
166 if (nf_ct_ext_types
[type
->id
]) {
171 /* This ensures that nf_ct_ext_create() can allocate enough area
172 before updating alloc_size */
173 type
->alloc_size
= ALIGN(sizeof(struct nf_ct_ext
), type
->align
)
175 rcu_assign_pointer(nf_ct_ext_types
[type
->id
], type
);
176 update_alloc_size(type
);
178 mutex_unlock(&nf_ct_ext_type_mutex
);
181 EXPORT_SYMBOL_GPL(nf_ct_extend_register
);
183 /* This MUST be called in process context. */
184 void nf_ct_extend_unregister(struct nf_ct_ext_type
*type
)
186 mutex_lock(&nf_ct_ext_type_mutex
);
187 rcu_assign_pointer(nf_ct_ext_types
[type
->id
], NULL
);
188 update_alloc_size(type
);
189 mutex_unlock(&nf_ct_ext_type_mutex
);
190 rcu_barrier(); /* Wait for completion of call_rcu()'s */
192 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister
);