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/kmemleak.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/rcupdate.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <net/netfilter/nf_conntrack_extend.h>
20 static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
21 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
22 #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
24 void nf_ct_ext_destroy(struct nf_conn *ct)
27 struct nf_ct_ext_type *t;
29 for (i = 0; i < NF_CT_EXT_NUM; i++) {
31 t = rcu_dereference(nf_ct_ext_types[i]);
33 /* Here the nf_ct_ext_type might have been unregisterd.
34 * I.e., it has responsible to cleanup private
35 * area in all conntracks when it is unregisterd.
42 EXPORT_SYMBOL(nf_ct_ext_destroy);
44 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
46 unsigned int newlen, newoff, oldlen, alloc;
47 struct nf_ct_ext *old, *new;
48 struct nf_ct_ext_type *t;
50 /* Conntrack must not be confirmed to avoid races on reallocation. */
51 WARN_ON(nf_ct_is_confirmed(ct));
56 if (__nf_ct_ext_exist(old, id))
60 oldlen = sizeof(*new);
64 t = rcu_dereference(nf_ct_ext_types[id]);
70 newoff = ALIGN(oldlen, t->align);
71 newlen = newoff + t->len;
74 alloc = max(newlen, NF_CT_EXT_PREALLOC);
75 kmemleak_not_leak(old);
76 new = __krealloc(old, alloc, gfp);
81 memset(new->offset, 0, sizeof(new->offset));
83 } else if (new != old) {
85 rcu_assign_pointer(ct->ext, new);
88 new->offset[id] = newoff;
90 memset((void *)new + newoff, 0, newlen - newoff);
91 return (void *)new + newoff;
93 EXPORT_SYMBOL(nf_ct_ext_add);
95 /* This MUST be called in process context. */
96 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
100 mutex_lock(&nf_ct_ext_type_mutex);
101 if (nf_ct_ext_types[type->id]) {
106 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
108 mutex_unlock(&nf_ct_ext_type_mutex);
111 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
113 /* This MUST be called in process context. */
114 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
116 mutex_lock(&nf_ct_ext_type_mutex);
117 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
118 mutex_unlock(&nf_ct_ext_type_mutex);
121 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);