1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Structure dynamic extension infrastructure
3 * Copyright (C) 2004 Rusty Russell IBM Corporation
4 * Copyright (C) 2007 Netfilter Core Team <coreteam@netfilter.org>
5 * Copyright (C) 2007 USAGI/WIDE Project <http://www.linux-ipv6.org>
7 #include <linux/kernel.h>
8 #include <linux/kmemleak.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/rcupdate.h>
12 #include <linux/slab.h>
13 #include <linux/skbuff.h>
14 #include <net/netfilter/nf_conntrack_extend.h>
16 static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
17 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
18 #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
20 void nf_ct_ext_destroy(struct nf_conn *ct)
23 struct nf_ct_ext_type *t;
25 for (i = 0; i < NF_CT_EXT_NUM; i++) {
27 t = rcu_dereference(nf_ct_ext_types[i]);
29 /* Here the nf_ct_ext_type might have been unregisterd.
30 * I.e., it has responsible to cleanup private
31 * area in all conntracks when it is unregisterd.
38 EXPORT_SYMBOL(nf_ct_ext_destroy);
40 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
42 unsigned int newlen, newoff, oldlen, alloc;
43 struct nf_ct_ext *old, *new;
44 struct nf_ct_ext_type *t;
46 /* Conntrack must not be confirmed to avoid races on reallocation. */
47 WARN_ON(nf_ct_is_confirmed(ct));
52 if (__nf_ct_ext_exist(old, id))
56 oldlen = sizeof(*new);
60 t = rcu_dereference(nf_ct_ext_types[id]);
66 newoff = ALIGN(oldlen, t->align);
67 newlen = newoff + t->len;
70 alloc = max(newlen, NF_CT_EXT_PREALLOC);
71 kmemleak_not_leak(old);
72 new = __krealloc(old, alloc, gfp);
77 memset(new->offset, 0, sizeof(new->offset));
79 } else if (new != old) {
81 rcu_assign_pointer(ct->ext, new);
84 new->offset[id] = newoff;
86 memset((void *)new + newoff, 0, newlen - newoff);
87 return (void *)new + newoff;
89 EXPORT_SYMBOL(nf_ct_ext_add);
91 /* This MUST be called in process context. */
92 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
96 mutex_lock(&nf_ct_ext_type_mutex);
97 if (nf_ct_ext_types[type->id]) {
102 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
104 mutex_unlock(&nf_ct_ext_type_mutex);
107 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
109 /* This MUST be called in process context. */
110 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
112 mutex_lock(&nf_ct_ext_type_mutex);
113 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
114 mutex_unlock(&nf_ct_ext_type_mutex);
117 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);