blob: 7dcf7a404190e6aa3fa06e642f54279e2f30fba9 [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
19static struct nf_ct_ext_type *nf_ct_ext_types[NF_CT_EXT_NUM];
20static 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 *
47nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
48{
Pekka Enberg019f6922008-03-10 16:43:41 -070049 unsigned int off, len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070050 struct nf_ct_ext_type *t;
51
52 rcu_read_lock();
53 t = rcu_dereference(nf_ct_ext_types[id]);
54 BUG_ON(t == NULL);
55 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
56 len = off + t->len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070057 rcu_read_unlock();
58
Pekka Enberg019f6922008-03-10 16:43:41 -070059 *ext = kzalloc(t->alloc_size, gfp);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070060 if (!*ext)
61 return NULL;
62
63 (*ext)->offset[id] = off;
64 (*ext)->len = len;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070065
66 return (void *)(*ext) + off;
67}
68
Patrick McHardy68b80f12008-06-17 15:51:47 -070069static void __nf_ct_ext_free_rcu(struct rcu_head *head)
70{
71 struct nf_ct_ext *ext = container_of(head, struct nf_ct_ext, rcu);
72 kfree(ext);
73}
74
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070075void *__nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
76{
Changli Gaoee92d372010-08-02 17:06:19 +020077 struct nf_ct_ext *old, *new;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070078 int i, newlen, newoff;
79 struct nf_ct_ext_type *t;
80
Patrick McHardy55871d02008-04-14 11:15:51 +020081 /* Conntrack must not be confirmed to avoid races on reallocation. */
82 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
83
Changli Gaoee92d372010-08-02 17:06:19 +020084 old = ct->ext;
85 if (!old)
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070086 return nf_ct_ext_create(&ct->ext, id, gfp);
87
Changli Gaoee92d372010-08-02 17:06:19 +020088 if (__nf_ct_ext_exist(old, id))
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070089 return NULL;
90
91 rcu_read_lock();
92 t = rcu_dereference(nf_ct_ext_types[id]);
93 BUG_ON(t == NULL);
94
Changli Gaoee92d372010-08-02 17:06:19 +020095 newoff = ALIGN(old->len, t->align);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070096 newlen = newoff + t->len;
97 rcu_read_unlock();
98
Changli Gaoee92d372010-08-02 17:06:19 +020099 new = __krealloc(old, newlen, gfp);
Pekka Enberg31d85192008-06-09 15:58:39 -0700100 if (!new)
101 return NULL;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700102
Changli Gaoee92d372010-08-02 17:06:19 +0200103 if (new != old) {
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700104 for (i = 0; i < NF_CT_EXT_NUM; i++) {
Changli Gaoee92d372010-08-02 17:06:19 +0200105 if (!__nf_ct_ext_exist(old, i))
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700106 continue;
107
108 rcu_read_lock();
109 t = rcu_dereference(nf_ct_ext_types[i]);
110 if (t && t->move)
Patrick McHardy86577c62008-02-07 17:56:34 -0800111 t->move((void *)new + new->offset[i],
Changli Gaoee92d372010-08-02 17:06:19 +0200112 (void *)old + old->offset[i]);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700113 rcu_read_unlock();
114 }
Changli Gaoee92d372010-08-02 17:06:19 +0200115 call_rcu(&old->rcu, __nf_ct_ext_free_rcu);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700116 ct->ext = new;
117 }
118
Patrick McHardy6c648252008-07-26 17:50:05 -0700119 new->offset[id] = newoff;
120 new->len = newlen;
121 memset((void *)new + newoff, 0, newlen - newoff);
122 return (void *)new + newoff;
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700123}
124EXPORT_SYMBOL(__nf_ct_ext_add);
125
126static void update_alloc_size(struct nf_ct_ext_type *type)
127{
128 int i, j;
129 struct nf_ct_ext_type *t1, *t2;
130 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
131
132 /* unnecessary to update all types */
133 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
134 min = type->id;
135 max = type->id;
136 }
137
138 /* This assumes that extended areas in conntrack for the types
139 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
140 for (i = min; i <= max; i++) {
141 t1 = nf_ct_ext_types[i];
142 if (!t1)
143 continue;
144
145 t1->alloc_size = sizeof(struct nf_ct_ext)
146 + ALIGN(sizeof(struct nf_ct_ext), t1->align)
147 + t1->len;
148 for (j = 0; j < NF_CT_EXT_NUM; j++) {
149 t2 = nf_ct_ext_types[j];
150 if (t2 == NULL || t2 == t1 ||
151 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
152 continue;
153
154 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
155 + t2->len;
156 }
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700157 }
158}
159
160/* This MUST be called in process context. */
161int nf_ct_extend_register(struct nf_ct_ext_type *type)
162{
163 int ret = 0;
164
165 mutex_lock(&nf_ct_ext_type_mutex);
166 if (nf_ct_ext_types[type->id]) {
167 ret = -EBUSY;
168 goto out;
169 }
170
171 /* This ensures that nf_ct_ext_create() can allocate enough area
172 before updating alloc_size */
173 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
174 + type->len;
175 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
176 update_alloc_size(type);
177out:
178 mutex_unlock(&nf_ct_ext_type_mutex);
179 return ret;
180}
181EXPORT_SYMBOL_GPL(nf_ct_extend_register);
182
183/* This MUST be called in process context. */
184void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
185{
186 mutex_lock(&nf_ct_ext_type_mutex);
187 rcu_assign_pointer(nf_ct_ext_types[type->id], NULL);
188 update_alloc_size(type);
189 mutex_unlock(&nf_ct_ext_type_mutex);
Jesper Dangaard Brouer308ff822009-06-25 16:32:52 +0200190 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700191}
192EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);