blob: 008299b7f78fe3754946cf0a58029090234ad905 [file] [log] [blame]
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -07001/* 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
Arnd Bergmann0906a372010-03-09 20:59:15 +010019static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070020static DEFINE_MUTEX(nf_ct_ext_type_mutex);
21
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070022void __nf_ct_ext_destroy(struct nf_conn *ct)
23{
24 unsigned int i;
25 struct nf_ct_ext_type *t;
Changli Gaoee92d372010-08-02 17:06:19 +020026 struct nf_ct_ext *ext = ct->ext;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070027
28 for (i = 0; i < NF_CT_EXT_NUM; i++) {
Changli Gaoee92d372010-08-02 17:06:19 +020029 if (!__nf_ct_ext_exist(ext, i))
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070030 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}
44EXPORT_SYMBOL(__nf_ct_ext_destroy);
45
46static void *
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +010047nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id,
48 size_t var_alloc_len, gfp_t gfp)
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070049{
Pekka Enberg019f6922008-03-10 16:43:41 -070050 unsigned int off, len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070051 struct nf_ct_ext_type *t;
Eric Dumazet15cdead2010-09-21 21:17:32 +000052 size_t alloc_size;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070053
54 rcu_read_lock();
55 t = rcu_dereference(nf_ct_ext_types[id]);
Liping Zhang5170d212017-03-25 16:35:29 +080056 if (!t) {
57 rcu_read_unlock();
58 return NULL;
59 }
60
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070061 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +010062 len = off + t->len + var_alloc_len;
63 alloc_size = t->alloc_size + var_alloc_len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070064 rcu_read_unlock();
65
Eric Dumazet15cdead2010-09-21 21:17:32 +000066 *ext = kzalloc(alloc_size, gfp);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070067 if (!*ext)
68 return NULL;
69
70 (*ext)->offset[id] = off;
71 (*ext)->len = len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070072
73 return (void *)(*ext) + off;
74}
75
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +010076void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
77 size_t var_alloc_len, gfp_t gfp)
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070078{
Changli Gaoee92d372010-08-02 17:06:19 +020079 struct nf_ct_ext *old, *new;
Florian Westphal7c966432016-07-05 12:07:23 +020080 int newlen, newoff;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070081 struct nf_ct_ext_type *t;
82
Patrick McHardy55871d02008-04-14 11:15:51 +020083 /* Conntrack must not be confirmed to avoid races on reallocation. */
84 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
85
Changli Gaoee92d372010-08-02 17:06:19 +020086 old = ct->ext;
87 if (!old)
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +010088 return nf_ct_ext_create(&ct->ext, id, var_alloc_len, gfp);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070089
Changli Gaoee92d372010-08-02 17:06:19 +020090 if (__nf_ct_ext_exist(old, id))
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070091 return NULL;
92
93 rcu_read_lock();
94 t = rcu_dereference(nf_ct_ext_types[id]);
Liping Zhang5170d212017-03-25 16:35:29 +080095 if (!t) {
96 rcu_read_unlock();
97 return NULL;
98 }
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070099
Changli Gaoee92d372010-08-02 17:06:19 +0200100 newoff = ALIGN(old->len, t->align);
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +0100101 newlen = newoff + t->len + var_alloc_len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700102 rcu_read_unlock();
103
Changli Gaoee92d372010-08-02 17:06:19 +0200104 new = __krealloc(old, newlen, gfp);
Pekka Enberg31d85192008-06-09 15:58:39 -0700105 if (!new)
106 return NULL;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700107
Changli Gaoee92d372010-08-02 17:06:19 +0200108 if (new != old) {
Lai Jiangshan1f8d36a2011-03-18 12:07:09 +0800109 kfree_rcu(old, rcu);
Florian Westphal7c966432016-07-05 12:07:23 +0200110 rcu_assign_pointer(ct->ext, new);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700111 }
112
Patrick McHardy6c648252008-07-26 17:50:05 -0700113 new->offset[id] = newoff;
114 new->len = newlen;
115 memset((void *)new + newoff, 0, newlen - newoff);
116 return (void *)new + newoff;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700117}
Pablo Neira Ayuso3cf4c7e2012-02-01 16:18:31 +0100118EXPORT_SYMBOL(__nf_ct_ext_add_length);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700119
120static void update_alloc_size(struct nf_ct_ext_type *type)
121{
122 int i, j;
123 struct nf_ct_ext_type *t1, *t2;
124 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
125
126 /* unnecessary to update all types */
127 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
128 min = type->id;
129 max = type->id;
130 }
131
132 /* This assumes that extended areas in conntrack for the types
133 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
134 for (i = min; i <= max; i++) {
Eric Dumazetc5d277d2010-11-15 19:45:13 +0100135 t1 = rcu_dereference_protected(nf_ct_ext_types[i],
136 lockdep_is_held(&nf_ct_ext_type_mutex));
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700137 if (!t1)
138 continue;
139
Changli Gao3b236882010-11-15 11:47:52 +0100140 t1->alloc_size = ALIGN(sizeof(struct nf_ct_ext), t1->align) +
141 t1->len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700142 for (j = 0; j < NF_CT_EXT_NUM; j++) {
Eric Dumazetc5d277d2010-11-15 19:45:13 +0100143 t2 = rcu_dereference_protected(nf_ct_ext_types[j],
144 lockdep_is_held(&nf_ct_ext_type_mutex));
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700145 if (t2 == NULL || t2 == t1 ||
146 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
147 continue;
148
149 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
150 + t2->len;
151 }
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700152 }
153}
154
155/* This MUST be called in process context. */
156int nf_ct_extend_register(struct nf_ct_ext_type *type)
157{
158 int ret = 0;
159
160 mutex_lock(&nf_ct_ext_type_mutex);
161 if (nf_ct_ext_types[type->id]) {
162 ret = -EBUSY;
163 goto out;
164 }
165
166 /* This ensures that nf_ct_ext_create() can allocate enough area
167 before updating alloc_size */
168 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
169 + type->len;
Eric Dumazetcf778b02012-01-12 04:41:32 +0000170 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700171 update_alloc_size(type);
172out:
173 mutex_unlock(&nf_ct_ext_type_mutex);
174 return ret;
175}
176EXPORT_SYMBOL_GPL(nf_ct_extend_register);
177
178/* This MUST be called in process context. */
179void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
180{
181 mutex_lock(&nf_ct_ext_type_mutex);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000182 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700183 update_alloc_size(type);
184 mutex_unlock(&nf_ct_ext_type_mutex);
Liping Zhang5170d212017-03-25 16:35:29 +0800185 synchronize_rcu();
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700186}
187EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);