]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_extend.c
b5879a9c748d0c392234312152455bafef1ed92a
[mirror_ubuntu-artful-kernel.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
22 void __nf_ct_ext_destroy(struct nf_conn *ct)
23 {
24 unsigned int i;
25 struct nf_ct_ext_type *t;
26 struct nf_ct_ext *ext = ct->ext;
27
28 for (i = 0; i < NF_CT_EXT_NUM; i++) {
29 if (!__nf_ct_ext_exist(ext, i))
30 continue;
31
32 rcu_read_lock();
33 t = rcu_dereference(nf_ct_ext_types[i]);
34
35 /* Here the nf_ct_ext_type might have been unregisterd.
36 * I.e., it has responsible to cleanup private
37 * area in all conntracks when it is unregisterd.
38 */
39 if (t && t->destroy)
40 t->destroy(ct);
41 rcu_read_unlock();
42 }
43 }
44 EXPORT_SYMBOL(__nf_ct_ext_destroy);
45
46 static void *
47 nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
48 {
49 unsigned int off, len;
50 struct nf_ct_ext_type *t;
51 size_t alloc_size;
52
53 rcu_read_lock();
54 t = rcu_dereference(nf_ct_ext_types[id]);
55 if (!t) {
56 rcu_read_unlock();
57 return NULL;
58 }
59
60 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
61 len = off + t->len;
62 alloc_size = t->alloc_size;
63 rcu_read_unlock();
64
65 *ext = kzalloc(alloc_size, gfp);
66 if (!*ext)
67 return NULL;
68
69 (*ext)->offset[id] = off;
70 (*ext)->len = len;
71
72 return (void *)(*ext) + off;
73 }
74
75 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
76 {
77 struct nf_ct_ext *old, *new;
78 int newlen, newoff;
79 struct nf_ct_ext_type *t;
80
81 /* Conntrack must not be confirmed to avoid races on reallocation. */
82 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
83
84 old = ct->ext;
85 if (!old)
86 return nf_ct_ext_create(&ct->ext, id, gfp);
87
88 if (__nf_ct_ext_exist(old, id))
89 return NULL;
90
91 rcu_read_lock();
92 t = rcu_dereference(nf_ct_ext_types[id]);
93 if (!t) {
94 rcu_read_unlock();
95 return NULL;
96 }
97
98 newoff = ALIGN(old->len, t->align);
99 newlen = newoff + t->len;
100 rcu_read_unlock();
101
102 new = __krealloc(old, newlen, gfp);
103 if (!new)
104 return NULL;
105
106 if (new != old) {
107 kfree_rcu(old, rcu);
108 rcu_assign_pointer(ct->ext, new);
109 }
110
111 new->offset[id] = newoff;
112 new->len = newlen;
113 memset((void *)new + newoff, 0, newlen - newoff);
114 return (void *)new + newoff;
115 }
116 EXPORT_SYMBOL(nf_ct_ext_add);
117
118 static void update_alloc_size(struct nf_ct_ext_type *type)
119 {
120 int i, j;
121 struct nf_ct_ext_type *t1, *t2;
122 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
123
124 /* unnecessary to update all types */
125 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
126 min = type->id;
127 max = type->id;
128 }
129
130 /* This assumes that extended areas in conntrack for the types
131 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
132 for (i = min; i <= max; i++) {
133 t1 = rcu_dereference_protected(nf_ct_ext_types[i],
134 lockdep_is_held(&nf_ct_ext_type_mutex));
135 if (!t1)
136 continue;
137
138 t1->alloc_size = ALIGN(sizeof(struct nf_ct_ext), t1->align) +
139 t1->len;
140 for (j = 0; j < NF_CT_EXT_NUM; j++) {
141 t2 = rcu_dereference_protected(nf_ct_ext_types[j],
142 lockdep_is_held(&nf_ct_ext_type_mutex));
143 if (t2 == NULL || t2 == t1 ||
144 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
145 continue;
146
147 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
148 + t2->len;
149 }
150 }
151 }
152
153 /* This MUST be called in process context. */
154 int nf_ct_extend_register(struct nf_ct_ext_type *type)
155 {
156 int ret = 0;
157
158 mutex_lock(&nf_ct_ext_type_mutex);
159 if (nf_ct_ext_types[type->id]) {
160 ret = -EBUSY;
161 goto out;
162 }
163
164 /* This ensures that nf_ct_ext_create() can allocate enough area
165 before updating alloc_size */
166 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
167 + type->len;
168 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
169 update_alloc_size(type);
170 out:
171 mutex_unlock(&nf_ct_ext_type_mutex);
172 return ret;
173 }
174 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
175
176 /* This MUST be called in process context. */
177 void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
178 {
179 mutex_lock(&nf_ct_ext_type_mutex);
180 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
181 update_alloc_size(type);
182 mutex_unlock(&nf_ct_ext_type_mutex);
183 synchronize_rcu();
184 }
185 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);