]> asedeno.scripts.mit.edu Git - linux.git/blob - net/netfilter/nf_conntrack_extend.c
Merge tag 'armsoc-drivers' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux.git] / net / netfilter / nf_conntrack_extend.c
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>
6  */
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>
15
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 */
19
20 void nf_ct_ext_destroy(struct nf_conn *ct)
21 {
22         unsigned int i;
23         struct nf_ct_ext_type *t;
24
25         for (i = 0; i < NF_CT_EXT_NUM; i++) {
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
38         kfree(ct->ext);
39 }
40 EXPORT_SYMBOL(nf_ct_ext_destroy);
41
42 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
43 {
44         unsigned int newlen, newoff, oldlen, alloc;
45         struct nf_ct_ext_type *t;
46         struct nf_ct_ext *new;
47
48         /* Conntrack must not be confirmed to avoid races on reallocation. */
49         WARN_ON(nf_ct_is_confirmed(ct));
50
51
52         if (ct->ext) {
53                 const struct nf_ct_ext *old = ct->ext;
54
55                 if (__nf_ct_ext_exist(old, id))
56                         return NULL;
57                 oldlen = old->len;
58         } else {
59                 oldlen = sizeof(*new);
60         }
61
62         rcu_read_lock();
63         t = rcu_dereference(nf_ct_ext_types[id]);
64         if (!t) {
65                 rcu_read_unlock();
66                 return NULL;
67         }
68
69         newoff = ALIGN(oldlen, t->align);
70         newlen = newoff + t->len;
71         rcu_read_unlock();
72
73         alloc = max(newlen, NF_CT_EXT_PREALLOC);
74         new = krealloc(ct->ext, alloc, gfp);
75         if (!new)
76                 return NULL;
77
78         if (!ct->ext)
79                 memset(new->offset, 0, sizeof(new->offset));
80
81         new->offset[id] = newoff;
82         new->len = newlen;
83         memset((void *)new + newoff, 0, newlen - newoff);
84
85         ct->ext = new;
86         return (void *)new + newoff;
87 }
88 EXPORT_SYMBOL(nf_ct_ext_add);
89
90 /* This MUST be called in process context. */
91 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
92 {
93         int ret = 0;
94
95         mutex_lock(&nf_ct_ext_type_mutex);
96         if (nf_ct_ext_types[type->id]) {
97                 ret = -EBUSY;
98                 goto out;
99         }
100
101         rcu_assign_pointer(nf_ct_ext_types[type->id], type);
102 out:
103         mutex_unlock(&nf_ct_ext_type_mutex);
104         return ret;
105 }
106 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
107
108 /* This MUST be called in process context. */
109 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
110 {
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);
114         synchronize_rcu();
115 }
116 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);