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.
40 EXPORT_SYMBOL(nf_ct_ext_destroy);
42 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
44 unsigned int newlen, newoff, oldlen, alloc;
45 struct nf_ct_ext_type *t;
46 struct nf_ct_ext *new;
48 /* Conntrack must not be confirmed to avoid races on reallocation. */
49 WARN_ON(nf_ct_is_confirmed(ct));
53 const struct nf_ct_ext *old = ct->ext;
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(ct->ext, alloc, gfp);
79 memset(new->offset, 0, sizeof(new->offset));
81 new->offset[id] = newoff;
83 memset((void *)new + newoff, 0, newlen - newoff);
86 return (void *)new + newoff;
88 EXPORT_SYMBOL(nf_ct_ext_add);
90 /* This MUST be called in process context. */
91 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
95 mutex_lock(&nf_ct_ext_type_mutex);
96 if (nf_ct_ext_types[type->id]) {
101 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
103 mutex_unlock(&nf_ct_ext_type_mutex);
106 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
108 /* This MUST be called in process context. */
109 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
111 mutex_lock(&nf_ct_ext_type_mutex);
112 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
113 mutex_unlock(&nf_ct_ext_type_mutex);
116 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);