Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_extend.c
CommitLineData
ecfab2c9
YK
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
0906a372 19static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
ecfab2c9
YK
20static DEFINE_MUTEX(nf_ct_ext_type_mutex);
21
ecfab2c9
YK
22void __nf_ct_ext_destroy(struct nf_conn *ct)
23{
24 unsigned int i;
25 struct nf_ct_ext_type *t;
ee92d378 26 struct nf_ct_ext *ext = ct->ext;
ecfab2c9
YK
27
28 for (i = 0; i < NF_CT_EXT_NUM; i++) {
ee92d378 29 if (!__nf_ct_ext_exist(ext, i))
ecfab2c9
YK
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}
44EXPORT_SYMBOL(__nf_ct_ext_destroy);
45
46static void *
3cf4c7e3
PNA
47nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id,
48 size_t var_alloc_len, gfp_t gfp)
ecfab2c9 49{
019f692e 50 unsigned int off, len;
ecfab2c9 51 struct nf_ct_ext_type *t;
15cdeada 52 size_t alloc_size;
ecfab2c9
YK
53
54 rcu_read_lock();
55 t = rcu_dereference(nf_ct_ext_types[id]);
159c95bd
LZ
56 if (!t) {
57 rcu_read_unlock();
58 return NULL;
59 }
60
ecfab2c9 61 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
3cf4c7e3
PNA
62 len = off + t->len + var_alloc_len;
63 alloc_size = t->alloc_size + var_alloc_len;
ecfab2c9
YK
64 rcu_read_unlock();
65
15cdeada 66 *ext = kzalloc(alloc_size, gfp);
ecfab2c9
YK
67 if (!*ext)
68 return NULL;
69
70 (*ext)->offset[id] = off;
71 (*ext)->len = len;
ecfab2c9
YK
72
73 return (void *)(*ext) + off;
74}
75
3cf4c7e3
PNA
76void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
77 size_t var_alloc_len, gfp_t gfp)
ecfab2c9 78{
ee92d378 79 struct nf_ct_ext *old, *new;
ecfab2c9
YK
80 int i, newlen, newoff;
81 struct nf_ct_ext_type *t;
82
55871d04
PM
83 /* Conntrack must not be confirmed to avoid races on reallocation. */
84 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
85
ee92d378
CG
86 old = ct->ext;
87 if (!old)
3cf4c7e3 88 return nf_ct_ext_create(&ct->ext, id, var_alloc_len, gfp);
ecfab2c9 89
ee92d378 90 if (__nf_ct_ext_exist(old, id))
ecfab2c9
YK
91 return NULL;
92
93 rcu_read_lock();
94 t = rcu_dereference(nf_ct_ext_types[id]);
159c95bd
LZ
95 if (!t) {
96 rcu_read_unlock();
97 return NULL;
98 }
ecfab2c9 99
ee92d378 100 newoff = ALIGN(old->len, t->align);
3cf4c7e3 101 newlen = newoff + t->len + var_alloc_len;
ecfab2c9
YK
102 rcu_read_unlock();
103
ee92d378 104 new = __krealloc(old, newlen, gfp);
31d8519c
PE
105 if (!new)
106 return NULL;
ecfab2c9 107
ee92d378 108 if (new != old) {
ecfab2c9 109 for (i = 0; i < NF_CT_EXT_NUM; i++) {
ee92d378 110 if (!__nf_ct_ext_exist(old, i))
ecfab2c9
YK
111 continue;
112
113 rcu_read_lock();
114 t = rcu_dereference(nf_ct_ext_types[i]);
115 if (t && t->move)
86577c66 116 t->move((void *)new + new->offset[i],
ee92d378 117 (void *)old + old->offset[i]);
ecfab2c9
YK
118 rcu_read_unlock();
119 }
1f8d36a1 120 kfree_rcu(old, rcu);
ecfab2c9
YK
121 ct->ext = new;
122 }
123
6c64825b
PM
124 new->offset[id] = newoff;
125 new->len = newlen;
126 memset((void *)new + newoff, 0, newlen - newoff);
127 return (void *)new + newoff;
ecfab2c9 128}
3cf4c7e3 129EXPORT_SYMBOL(__nf_ct_ext_add_length);
ecfab2c9
YK
130
131static void update_alloc_size(struct nf_ct_ext_type *type)
132{
133 int i, j;
134 struct nf_ct_ext_type *t1, *t2;
135 enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
136
137 /* unnecessary to update all types */
138 if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
139 min = type->id;
140 max = type->id;
141 }
142
143 /* This assumes that extended areas in conntrack for the types
144 whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
145 for (i = min; i <= max; i++) {
c5d277d2
ED
146 t1 = rcu_dereference_protected(nf_ct_ext_types[i],
147 lockdep_is_held(&nf_ct_ext_type_mutex));
ecfab2c9
YK
148 if (!t1)
149 continue;
150
3b236880
CG
151 t1->alloc_size = ALIGN(sizeof(struct nf_ct_ext), t1->align) +
152 t1->len;
ecfab2c9 153 for (j = 0; j < NF_CT_EXT_NUM; j++) {
c5d277d2
ED
154 t2 = rcu_dereference_protected(nf_ct_ext_types[j],
155 lockdep_is_held(&nf_ct_ext_type_mutex));
ecfab2c9
YK
156 if (t2 == NULL || t2 == t1 ||
157 (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
158 continue;
159
160 t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
161 + t2->len;
162 }
ecfab2c9
YK
163 }
164}
165
166/* This MUST be called in process context. */
167int nf_ct_extend_register(struct nf_ct_ext_type *type)
168{
169 int ret = 0;
170
171 mutex_lock(&nf_ct_ext_type_mutex);
172 if (nf_ct_ext_types[type->id]) {
173 ret = -EBUSY;
174 goto out;
175 }
176
177 /* This ensures that nf_ct_ext_create() can allocate enough area
178 before updating alloc_size */
179 type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
180 + type->len;
cf778b00 181 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
ecfab2c9
YK
182 update_alloc_size(type);
183out:
184 mutex_unlock(&nf_ct_ext_type_mutex);
185 return ret;
186}
187EXPORT_SYMBOL_GPL(nf_ct_extend_register);
188
189/* This MUST be called in process context. */
190void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
191{
192 mutex_lock(&nf_ct_ext_type_mutex);
a9b3cd7f 193 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
ecfab2c9
YK
194 update_alloc_size(type);
195 mutex_unlock(&nf_ct_ext_type_mutex);
159c95bd 196 synchronize_rcu();
ecfab2c9
YK
197}
198EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);