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 *
27 #include <linux/kmalloc_sizes.h>
32 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
33 void __nf_ct_ext_destroy(struct nf_conn
*ct
)
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
))
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.
54 EXPORT_SYMBOL(__nf_ct_ext_destroy
);
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
;
62 unsigned int off
, len
;
63 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
64 struct nf_ct_ext_type
*t
;
67 t
= rcu_dereference(nf_ct_ext_types
[id
]);
69 off
= ALIGN(sizeof(struct nf_ct_ext
), t
->align
);
71 <<<<<<< HEAD
:net
/netfilter
/nf_conntrack_extend
.c
72 real_len
= t
->alloc_size
;
74 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
77 <<<<<<< HEAD
:net
/netfilter
/nf_conntrack_extend
.c
78 *ext
= kzalloc(real_len
, gfp
);
80 *ext
= kzalloc(t
->alloc_size
, gfp
);
81 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
85 (*ext
)->offset
[id
] = off
;
87 <<<<<<< HEAD
:net
/netfilter
/nf_conntrack_extend
.c
88 (*ext
)->real_len
= real_len
;
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
;
102 return nf_ct_ext_create(&ct
->ext
, id
, gfp
);
104 if (nf_ct_ext_exist(ct
, id
))
108 t
= rcu_dereference(nf_ct_ext_types
[id
]);
111 newoff
= ALIGN(ct
->ext
->len
, t
->align
);
112 newlen
= newoff
+ t
->len
;
115 <<<<<<< HEAD
:net
/netfilter
/nf_conntrack_extend
.c
116 if (newlen
>= ct
->ext
->real_len
) {
118 if (newlen
>= ksize(ct
->ext
)) {
119 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
120 new = kmalloc(newlen
, gfp
);
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
))
131 t
= rcu_dereference(nf_ct_ext_types
[i
]);
133 t
->move((void *)new + new->offset
[i
],
134 (void *)ct
->ext
+ ct
->ext
->offset
[i
]);
138 <<<<<<< HEAD
:net
/netfilter
/nf_conntrack_extend
.c
139 new->real_len
= newlen
;
141 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/netfilter
/nf_conntrack_extend
.c
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
)
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) {
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
];
171 t1
->alloc_size
= sizeof(struct nf_ct_ext
)
172 + ALIGN(sizeof(struct nf_ct_ext
), t1
->align
)
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)
180 t1
->alloc_size
= ALIGN(t1
->alloc_size
, t2
->align
)
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
;
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
)
196 mutex_lock(&nf_ct_ext_type_mutex
);
197 if (nf_ct_ext_types
[type
->id
]) {
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
)
206 rcu_assign_pointer(nf_ct_ext_types
[type
->id
], type
);
207 update_alloc_size(type
);
209 mutex_unlock(&nf_ct_ext_type_mutex
);
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
);
223 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister
);