]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nf_conntrack_extend.c
netfilter: conntrack: mark extension structs as const
[mirror_ubuntu-artful-kernel.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 20static DEFINE_MUTEX(nf_ct_ext_type_mutex);
54044b1f 21#define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
ecfab2c9 22
ecfab2c9
YK
23void __nf_ct_ext_destroy(struct nf_conn *ct)
24{
25 unsigned int i;
26 struct nf_ct_ext_type *t;
ee92d378 27 struct nf_ct_ext *ext = ct->ext;
ecfab2c9
YK
28
29 for (i = 0; i < NF_CT_EXT_NUM; i++) {
ee92d378 30 if (!__nf_ct_ext_exist(ext, i))
ecfab2c9
YK
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}
45EXPORT_SYMBOL(__nf_ct_ext_destroy);
46
47static void *
faec865d 48nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp)
ecfab2c9 49{
54044b1f 50 unsigned int off, len, alloc;
ecfab2c9
YK
51 struct nf_ct_ext_type *t;
52
53 rcu_read_lock();
54 t = rcu_dereference(nf_ct_ext_types[id]);
9c3f3794
LZ
55 if (!t) {
56 rcu_read_unlock();
57 return NULL;
58 }
59
ecfab2c9 60 off = ALIGN(sizeof(struct nf_ct_ext), t->align);
faec865d 61 len = off + t->len;
ecfab2c9
YK
62 rcu_read_unlock();
63
54044b1f
FW
64 alloc = max(len, NF_CT_EXT_PREALLOC);
65 *ext = kzalloc(alloc, gfp);
ecfab2c9
YK
66 if (!*ext)
67 return NULL;
68
69 (*ext)->offset[id] = off;
70 (*ext)->len = len;
ecfab2c9
YK
71
72 return (void *)(*ext) + off;
73}
74
faec865d 75void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
ecfab2c9 76{
ee92d378 77 struct nf_ct_ext *old, *new;
7c966435 78 int newlen, newoff;
ecfab2c9
YK
79 struct nf_ct_ext_type *t;
80
55871d04
PM
81 /* Conntrack must not be confirmed to avoid races on reallocation. */
82 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
83
ee92d378
CG
84 old = ct->ext;
85 if (!old)
faec865d 86 return nf_ct_ext_create(&ct->ext, id, gfp);
ecfab2c9 87
ee92d378 88 if (__nf_ct_ext_exist(old, id))
ecfab2c9
YK
89 return NULL;
90
91 rcu_read_lock();
92 t = rcu_dereference(nf_ct_ext_types[id]);
9c3f3794
LZ
93 if (!t) {
94 rcu_read_unlock();
95 return NULL;
96 }
ecfab2c9 97
ee92d378 98 newoff = ALIGN(old->len, t->align);
faec865d 99 newlen = newoff + t->len;
ecfab2c9
YK
100 rcu_read_unlock();
101
ee92d378 102 new = __krealloc(old, newlen, gfp);
31d8519c
PE
103 if (!new)
104 return NULL;
ecfab2c9 105
ee92d378 106 if (new != old) {
1f8d36a1 107 kfree_rcu(old, rcu);
7c966435 108 rcu_assign_pointer(ct->ext, new);
ecfab2c9
YK
109 }
110
6c64825b
PM
111 new->offset[id] = newoff;
112 new->len = newlen;
113 memset((void *)new + newoff, 0, newlen - newoff);
114 return (void *)new + newoff;
ecfab2c9 115}
faec865d 116EXPORT_SYMBOL(nf_ct_ext_add);
ecfab2c9 117
ecfab2c9 118/* This MUST be called in process context. */
23f671a1 119int nf_ct_extend_register(const struct nf_ct_ext_type *type)
ecfab2c9
YK
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
cf778b00 129 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
ecfab2c9
YK
130out:
131 mutex_unlock(&nf_ct_ext_type_mutex);
132 return ret;
133}
134EXPORT_SYMBOL_GPL(nf_ct_extend_register);
135
136/* This MUST be called in process context. */
23f671a1 137void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
ecfab2c9
YK
138{
139 mutex_lock(&nf_ct_ext_type_mutex);
a9b3cd7f 140 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
ecfab2c9 141 mutex_unlock(&nf_ct_ext_type_mutex);
9c3f3794 142 synchronize_rcu();
ecfab2c9
YK
143}
144EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);