]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nf_conntrack_extend.c
Merge tag 'trace-v5.12-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nf_conntrack_extend.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
ecfab2c9
YK
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>
ecfab2c9
YK
6 */
7#include <linux/kernel.h>
765cca91 8#include <linux/kmemleak.h>
ecfab2c9
YK
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>
15
0906a372 16static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
ecfab2c9 17static DEFINE_MUTEX(nf_ct_ext_type_mutex);
54044b1f 18#define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
ecfab2c9 19
8eeef235 20void nf_ct_ext_destroy(struct nf_conn *ct)
ecfab2c9
YK
21{
22 unsigned int i;
23 struct nf_ct_ext_type *t;
24
25 for (i = 0; i < NF_CT_EXT_NUM; i++) {
ecfab2c9
YK
26 rcu_read_lock();
27 t = rcu_dereference(nf_ct_ext_types[i]);
28
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.
32 */
33 if (t && t->destroy)
34 t->destroy(ct);
35 rcu_read_unlock();
36 }
2ad9d774
FW
37
38 kfree(ct->ext);
ecfab2c9 39}
ecfab2c9 40
faec865d 41void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
ecfab2c9 42{
22d4536d 43 unsigned int newlen, newoff, oldlen, alloc;
ecfab2c9 44 struct nf_ct_ext_type *t;
2ad9d774 45 struct nf_ct_ext *new;
ecfab2c9 46
55871d04 47 /* Conntrack must not be confirmed to avoid races on reallocation. */
44d6e2f2 48 WARN_ON(nf_ct_is_confirmed(ct));
55871d04 49
ecfab2c9 50
2ad9d774
FW
51 if (ct->ext) {
52 const struct nf_ct_ext *old = ct->ext;
53
22d4536d
FW
54 if (__nf_ct_ext_exist(old, id))
55 return NULL;
56 oldlen = old->len;
57 } else {
58 oldlen = sizeof(*new);
59 }
ecfab2c9
YK
60
61 rcu_read_lock();
62 t = rcu_dereference(nf_ct_ext_types[id]);
9c3f3794
LZ
63 if (!t) {
64 rcu_read_unlock();
65 return NULL;
66 }
ecfab2c9 67
22d4536d 68 newoff = ALIGN(oldlen, t->align);
faec865d 69 newlen = newoff + t->len;
ecfab2c9
YK
70 rcu_read_unlock();
71
22d4536d 72 alloc = max(newlen, NF_CT_EXT_PREALLOC);
2ad9d774 73 new = krealloc(ct->ext, alloc, gfp);
31d8519c
PE
74 if (!new)
75 return NULL;
ecfab2c9 76
2ad9d774 77 if (!ct->ext)
22d4536d 78 memset(new->offset, 0, sizeof(new->offset));
ecfab2c9 79
6c64825b
PM
80 new->offset[id] = newoff;
81 new->len = newlen;
82 memset((void *)new + newoff, 0, newlen - newoff);
2ad9d774
FW
83
84 ct->ext = new;
6c64825b 85 return (void *)new + newoff;
ecfab2c9 86}
faec865d 87EXPORT_SYMBOL(nf_ct_ext_add);
ecfab2c9 88
ecfab2c9 89/* This MUST be called in process context. */
23f671a1 90int nf_ct_extend_register(const struct nf_ct_ext_type *type)
ecfab2c9
YK
91{
92 int ret = 0;
93
94 mutex_lock(&nf_ct_ext_type_mutex);
95 if (nf_ct_ext_types[type->id]) {
96 ret = -EBUSY;
97 goto out;
98 }
99
cf778b00 100 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
ecfab2c9
YK
101out:
102 mutex_unlock(&nf_ct_ext_type_mutex);
103 return ret;
104}
105EXPORT_SYMBOL_GPL(nf_ct_extend_register);
106
107/* This MUST be called in process context. */
23f671a1 108void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
ecfab2c9
YK
109{
110 mutex_lock(&nf_ct_ext_type_mutex);
a9b3cd7f 111 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
ecfab2c9 112 mutex_unlock(&nf_ct_ext_type_mutex);
9c3f3794 113 synchronize_rcu();
ecfab2c9
YK
114}
115EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);