]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_extend.c
netfilter: conntrack: remove prealloc support
[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 #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
22
23 void __nf_ct_ext_destroy(struct nf_conn *ct)
24 {
25 unsigned int i;
26 struct nf_ct_ext_type *t;
27 struct nf_ct_ext *ext = ct->ext;
28
29 for (i = 0; i < NF_CT_EXT_NUM; i++) {
30 if (!__nf_ct_ext_exist(ext, i))
31 continue;
32
33 rcu_read_lock();
34 t = rcu_dereference(nf_ct_ext_types[i]);
35
36 /* Here the nf_ct_ext_type might have been unregisterd.
37 * I.e., it has responsible to cleanup private
38 * area in all conntracks when it is unregisterd.
39 */
40 if (t && t->destroy)
41 t->destroy(ct);
42 rcu_read_unlock();
43 }
44 }
45 EXPORT_SYMBOL(__nf_ct_ext_destroy);
46
47 static void *
48 nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
49 {
50 unsigned int off, len, alloc;
51 struct nf_ct_ext_type *t;
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 rcu_read_unlock();
63
64 alloc = max(len, NF_CT_EXT_PREALLOC);
65 *ext = kzalloc(alloc, 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 /* This MUST be called in process context. */
119 int nf_ct_extend_register(struct nf_ct_ext_type *type)
120 {
121 int ret = 0;
122
123 mutex_lock(&nf_ct_ext_type_mutex);
124 if (nf_ct_ext_types[type->id]) {
125 ret = -EBUSY;
126 goto out;
127 }
128
129 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
130 out:
131 mutex_unlock(&nf_ct_ext_type_mutex);
132 return ret;
133 }
134 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
135
136 /* This MUST be called in process context. */
137 void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
138 {
139 mutex_lock(&nf_ct_ext_type_mutex);
140 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
141 mutex_unlock(&nf_ct_ext_type_mutex);
142 synchronize_rcu();
143 }
144 EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);