GNU Linux-libre 4.14.290-gnu1
[releases.git] / net / netfilter / nf_conntrack_extend.c
1 /* Structure dynamic extension infrastructure
2  * Copyright (C) 2004 Rusty Russell IBM Corporation
3  * Copyright (C) 2007 Netfilter Core Team <coreteam@netfilter.org>
4  * Copyright (C) 2007 USAGI/WIDE Project <http://www.linux-ipv6.org>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/rcupdate.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <net/netfilter/nf_conntrack_extend.h>
18
19 static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
20 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
21 #define NF_CT_EXT_PREALLOC      128u /* conntrack events are on by default */
22
23 void nf_ct_ext_destroy(struct nf_conn *ct)
24 {
25         unsigned int i;
26         struct nf_ct_ext_type *t;
27
28         for (i = 0; i < NF_CT_EXT_NUM; i++) {
29                 rcu_read_lock();
30                 t = rcu_dereference(nf_ct_ext_types[i]);
31
32                 /* Here the nf_ct_ext_type might have been unregisterd.
33                  * I.e., it has responsible to cleanup private
34                  * area in all conntracks when it is unregisterd.
35                  */
36                 if (t && t->destroy)
37                         t->destroy(ct);
38                 rcu_read_unlock();
39         }
40 }
41 EXPORT_SYMBOL(nf_ct_ext_destroy);
42
43 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
44 {
45         unsigned int newlen, newoff, oldlen, alloc;
46         struct nf_ct_ext *old, *new;
47         struct nf_ct_ext_type *t;
48
49         /* Conntrack must not be confirmed to avoid races on reallocation. */
50         WARN_ON(nf_ct_is_confirmed(ct));
51
52         old = ct->ext;
53
54         if (old) {
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(old, alloc, gfp);
75         if (!new)
76                 return NULL;
77
78         if (!old) {
79                 memset(new->offset, 0, sizeof(new->offset));
80                 ct->ext = new;
81         } else if (new != old) {
82                 kfree_rcu(old, rcu);
83                 rcu_assign_pointer(ct->ext, new);
84         }
85
86         new->offset[id] = newoff;
87         new->len = newlen;
88         memset((void *)new + newoff, 0, newlen - newoff);
89         return (void *)new + newoff;
90 }
91 EXPORT_SYMBOL(nf_ct_ext_add);
92
93 /* This MUST be called in process context. */
94 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
95 {
96         int ret = 0;
97
98         mutex_lock(&nf_ct_ext_type_mutex);
99         if (nf_ct_ext_types[type->id]) {
100                 ret = -EBUSY;
101                 goto out;
102         }
103
104         rcu_assign_pointer(nf_ct_ext_types[type->id], type);
105 out:
106         mutex_unlock(&nf_ct_ext_type_mutex);
107         return ret;
108 }
109 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
110
111 /* This MUST be called in process context. */
112 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
113 {
114         mutex_lock(&nf_ct_ext_type_mutex);
115         RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
116         mutex_unlock(&nf_ct_ext_type_mutex);
117         synchronize_rcu();
118 }
119 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);