]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/netfilter/nf_conntrack_extend.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-eoan-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 }
37}
8eeef235 38EXPORT_SYMBOL(nf_ct_ext_destroy);
ecfab2c9 39
faec865d 40void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
ecfab2c9 41{
22d4536d 42 unsigned int newlen, newoff, oldlen, alloc;
ee92d378 43 struct nf_ct_ext *old, *new;
ecfab2c9
YK
44 struct nf_ct_ext_type *t;
45
55871d04 46 /* Conntrack must not be confirmed to avoid races on reallocation. */
44d6e2f2 47 WARN_ON(nf_ct_is_confirmed(ct));
55871d04 48
ee92d378 49 old = ct->ext;
ecfab2c9 50
22d4536d
FW
51 if (old) {
52 if (__nf_ct_ext_exist(old, id))
53 return NULL;
54 oldlen = old->len;
55 } else {
56 oldlen = sizeof(*new);
57 }
ecfab2c9
YK
58
59 rcu_read_lock();
60 t = rcu_dereference(nf_ct_ext_types[id]);
9c3f3794
LZ
61 if (!t) {
62 rcu_read_unlock();
63 return NULL;
64 }
ecfab2c9 65
22d4536d 66 newoff = ALIGN(oldlen, t->align);
faec865d 67 newlen = newoff + t->len;
ecfab2c9
YK
68 rcu_read_unlock();
69
22d4536d 70 alloc = max(newlen, NF_CT_EXT_PREALLOC);
114aa35d 71 kmemleak_not_leak(old);
22d4536d 72 new = __krealloc(old, alloc, gfp);
31d8519c
PE
73 if (!new)
74 return NULL;
ecfab2c9 75
22d4536d
FW
76 if (!old) {
77 memset(new->offset, 0, sizeof(new->offset));
78 ct->ext = new;
79 } else if (new != old) {
1f8d36a1 80 kfree_rcu(old, rcu);
7c966435 81 rcu_assign_pointer(ct->ext, new);
ecfab2c9
YK
82 }
83
6c64825b
PM
84 new->offset[id] = newoff;
85 new->len = newlen;
86 memset((void *)new + newoff, 0, newlen - newoff);
87 return (void *)new + newoff;
ecfab2c9 88}
faec865d 89EXPORT_SYMBOL(nf_ct_ext_add);
ecfab2c9 90
ecfab2c9 91/* This MUST be called in process context. */
23f671a1 92int nf_ct_extend_register(const struct nf_ct_ext_type *type)
ecfab2c9
YK
93{
94 int ret = 0;
95
96 mutex_lock(&nf_ct_ext_type_mutex);
97 if (nf_ct_ext_types[type->id]) {
98 ret = -EBUSY;
99 goto out;
100 }
101
cf778b00 102 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
ecfab2c9
YK
103out:
104 mutex_unlock(&nf_ct_ext_type_mutex);
105 return ret;
106}
107EXPORT_SYMBOL_GPL(nf_ct_extend_register);
108
109/* This MUST be called in process context. */
23f671a1 110void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
ecfab2c9
YK
111{
112 mutex_lock(&nf_ct_ext_type_mutex);
a9b3cd7f 113 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
ecfab2c9 114 mutex_unlock(&nf_ct_ext_type_mutex);
9c3f3794 115 synchronize_rcu();
ecfab2c9
YK
116}
117EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);