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/kmemleak.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/rcupdate.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <net/netfilter/nf_conntrack_extend.h>
19
20 static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
21 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
22 #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
23
nf_ct_ext_destroy(struct nf_conn * ct)24 void nf_ct_ext_destroy(struct nf_conn *ct)
25 {
26 unsigned int i;
27 struct nf_ct_ext_type *t;
28
29 for (i = 0; i < NF_CT_EXT_NUM; i++) {
30 rcu_read_lock();
31 t = rcu_dereference(nf_ct_ext_types[i]);
32
33 /* Here the nf_ct_ext_type might have been unregisterd.
34 * I.e., it has responsible to cleanup private
35 * area in all conntracks when it is unregisterd.
36 */
37 if (t && t->destroy)
38 t->destroy(ct);
39 rcu_read_unlock();
40 }
41 }
42 EXPORT_SYMBOL(nf_ct_ext_destroy);
43
nf_ct_ext_add(struct nf_conn * ct,enum nf_ct_ext_id id,gfp_t gfp)44 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
45 {
46 unsigned int newlen, newoff, oldlen, alloc;
47 struct nf_ct_ext *old, *new;
48 struct nf_ct_ext_type *t;
49
50 /* Conntrack must not be confirmed to avoid races on reallocation. */
51 WARN_ON(nf_ct_is_confirmed(ct));
52
53 old = ct->ext;
54
55 if (old) {
56 if (__nf_ct_ext_exist(old, id))
57 return NULL;
58 oldlen = old->len;
59 } else {
60 oldlen = sizeof(*new);
61 }
62
63 rcu_read_lock();
64 t = rcu_dereference(nf_ct_ext_types[id]);
65 if (!t) {
66 rcu_read_unlock();
67 return NULL;
68 }
69
70 newoff = ALIGN(oldlen, t->align);
71 newlen = newoff + t->len;
72 rcu_read_unlock();
73
74 alloc = max(newlen, NF_CT_EXT_PREALLOC);
75 kmemleak_not_leak(old);
76 new = __krealloc(old, alloc, gfp);
77 if (!new)
78 return NULL;
79
80 if (!old) {
81 memset(new->offset, 0, sizeof(new->offset));
82 ct->ext = new;
83 } else if (new != old) {
84 kfree_rcu(old, rcu);
85 rcu_assign_pointer(ct->ext, new);
86 }
87
88 new->offset[id] = newoff;
89 new->len = newlen;
90 memset((void *)new + newoff, 0, newlen - newoff);
91 return (void *)new + newoff;
92 }
93 EXPORT_SYMBOL(nf_ct_ext_add);
94
95 /* This MUST be called in process context. */
nf_ct_extend_register(const struct nf_ct_ext_type * type)96 int nf_ct_extend_register(const struct nf_ct_ext_type *type)
97 {
98 int ret = 0;
99
100 mutex_lock(&nf_ct_ext_type_mutex);
101 if (nf_ct_ext_types[type->id]) {
102 ret = -EBUSY;
103 goto out;
104 }
105
106 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
107 out:
108 mutex_unlock(&nf_ct_ext_type_mutex);
109 return ret;
110 }
111 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
112
113 /* This MUST be called in process context. */
nf_ct_extend_unregister(const struct nf_ct_ext_type * type)114 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
115 {
116 mutex_lock(&nf_ct_ext_type_mutex);
117 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
118 mutex_unlock(&nf_ct_ext_type_mutex);
119 synchronize_rcu();
120 }
121 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);
122