]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_extend.c
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 __rcu
*nf_ct_ext_types
[NF_CT_EXT_NUM
];
20 static DEFINE_MUTEX(nf_ct_ext_type_mutex
);
21 #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
23 void nf_ct_ext_destroy(struct nf_conn
*ct
)
26 struct nf_ct_ext_type
*t
;
28 for (i
= 0; i
< NF_CT_EXT_NUM
; i
++) {
30 t
= rcu_dereference(nf_ct_ext_types
[i
]);
32 /* Here the nf_ct_ext_type might have been unregisterd.
33 * I.e., it has responsible to cleanup private
34 * area in all conntracks when it is unregisterd.
41 EXPORT_SYMBOL(nf_ct_ext_destroy
);
43 void *nf_ct_ext_add(struct nf_conn
*ct
, enum nf_ct_ext_id id
, gfp_t gfp
)
45 unsigned int newlen
, newoff
, oldlen
, alloc
;
46 struct nf_ct_ext
*old
, *new;
47 struct nf_ct_ext_type
*t
;
49 /* Conntrack must not be confirmed to avoid races on reallocation. */
50 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
55 if (__nf_ct_ext_exist(old
, id
))
59 oldlen
= sizeof(*new);
63 t
= rcu_dereference(nf_ct_ext_types
[id
]);
69 newoff
= ALIGN(oldlen
, t
->align
);
70 newlen
= newoff
+ t
->len
;
73 alloc
= max(newlen
, NF_CT_EXT_PREALLOC
);
74 new = __krealloc(old
, alloc
, gfp
);
79 memset(new->offset
, 0, sizeof(new->offset
));
81 } else if (new != old
) {
83 rcu_assign_pointer(ct
->ext
, new);
86 new->offset
[id
] = newoff
;
88 memset((void *)new + newoff
, 0, newlen
- newoff
);
89 return (void *)new + newoff
;
91 EXPORT_SYMBOL(nf_ct_ext_add
);
93 /* This MUST be called in process context. */
94 int nf_ct_extend_register(const struct nf_ct_ext_type
*type
)
98 mutex_lock(&nf_ct_ext_type_mutex
);
99 if (nf_ct_ext_types
[type
->id
]) {
104 rcu_assign_pointer(nf_ct_ext_types
[type
->id
], type
);
106 mutex_unlock(&nf_ct_ext_type_mutex
);
109 EXPORT_SYMBOL_GPL(nf_ct_extend_register
);
111 /* This MUST be called in process context. */
112 void nf_ct_extend_unregister(const struct nf_ct_ext_type
*type
)
114 mutex_lock(&nf_ct_ext_type_mutex
);
115 RCU_INIT_POINTER(nf_ct_ext_types
[type
->id
], NULL
);
116 mutex_unlock(&nf_ct_ext_type_mutex
);
119 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister
);