Commit | Line | Data |
---|---|---|
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> | |
765cca91 | 12 | #include <linux/kmemleak.h> |
ecfab2c9 YK |
13 | #include <linux/module.h> |
14 | #include <linux/mutex.h> | |
15 | #include <linux/rcupdate.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/skbuff.h> | |
18 | #include <net/netfilter/nf_conntrack_extend.h> | |
19 | ||
0906a372 | 20 | static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM]; |
ecfab2c9 | 21 | static DEFINE_MUTEX(nf_ct_ext_type_mutex); |
54044b1f | 22 | #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */ |
ecfab2c9 | 23 | |
8eeef235 | 24 | void nf_ct_ext_destroy(struct nf_conn *ct) |
ecfab2c9 YK |
25 | { |
26 | unsigned int i; | |
27 | struct nf_ct_ext_type *t; | |
28 | ||
29 | for (i = 0; i < NF_CT_EXT_NUM; i++) { | |
ecfab2c9 YK |
30 | rcu_read_lock(); |
31 | t = rcu_dereference(nf_ct_ext_types[i]); | |
32 | ||
33 | /* Here the nf_ct_ext_type might have been unregisterd. | |
34 | * I.e., it has responsible to cleanup private | |
35 | * area in all conntracks when it is unregisterd. | |
36 | */ | |
37 | if (t && t->destroy) | |
38 | t->destroy(ct); | |
39 | rcu_read_unlock(); | |
40 | } | |
41 | } | |
8eeef235 | 42 | EXPORT_SYMBOL(nf_ct_ext_destroy); |
ecfab2c9 | 43 | |
faec865d | 44 | void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp) |
ecfab2c9 | 45 | { |
22d4536d | 46 | unsigned int newlen, newoff, oldlen, alloc; |
ee92d378 | 47 | struct nf_ct_ext *old, *new; |
ecfab2c9 YK |
48 | struct nf_ct_ext_type *t; |
49 | ||
55871d04 | 50 | /* Conntrack must not be confirmed to avoid races on reallocation. */ |
44d6e2f2 | 51 | WARN_ON(nf_ct_is_confirmed(ct)); |
55871d04 | 52 | |
ee92d378 | 53 | old = ct->ext; |
ecfab2c9 | 54 | |
22d4536d FW |
55 | if (old) { |
56 | if (__nf_ct_ext_exist(old, id)) | |
57 | return NULL; | |
58 | oldlen = old->len; | |
59 | } else { | |
60 | oldlen = sizeof(*new); | |
61 | } | |
ecfab2c9 YK |
62 | |
63 | rcu_read_lock(); | |
64 | t = rcu_dereference(nf_ct_ext_types[id]); | |
9c3f3794 LZ |
65 | if (!t) { |
66 | rcu_read_unlock(); | |
67 | return NULL; | |
68 | } | |
ecfab2c9 | 69 | |
22d4536d | 70 | newoff = ALIGN(oldlen, t->align); |
faec865d | 71 | newlen = newoff + t->len; |
ecfab2c9 YK |
72 | rcu_read_unlock(); |
73 | ||
22d4536d | 74 | alloc = max(newlen, NF_CT_EXT_PREALLOC); |
114aa35d | 75 | kmemleak_not_leak(old); |
22d4536d | 76 | new = __krealloc(old, alloc, gfp); |
31d8519c PE |
77 | if (!new) |
78 | return NULL; | |
ecfab2c9 | 79 | |
22d4536d FW |
80 | if (!old) { |
81 | memset(new->offset, 0, sizeof(new->offset)); | |
82 | ct->ext = new; | |
83 | } else if (new != old) { | |
1f8d36a1 | 84 | kfree_rcu(old, rcu); |
7c966435 | 85 | rcu_assign_pointer(ct->ext, new); |
ecfab2c9 YK |
86 | } |
87 | ||
6c64825b PM |
88 | new->offset[id] = newoff; |
89 | new->len = newlen; | |
90 | memset((void *)new + newoff, 0, newlen - newoff); | |
91 | return (void *)new + newoff; | |
ecfab2c9 | 92 | } |
faec865d | 93 | EXPORT_SYMBOL(nf_ct_ext_add); |
ecfab2c9 | 94 | |
ecfab2c9 | 95 | /* This MUST be called in process context. */ |
23f671a1 | 96 | int nf_ct_extend_register(const struct nf_ct_ext_type *type) |
ecfab2c9 YK |
97 | { |
98 | int ret = 0; | |
99 | ||
100 | mutex_lock(&nf_ct_ext_type_mutex); | |
101 | if (nf_ct_ext_types[type->id]) { | |
102 | ret = -EBUSY; | |
103 | goto out; | |
104 | } | |
105 | ||
cf778b00 | 106 | rcu_assign_pointer(nf_ct_ext_types[type->id], type); |
ecfab2c9 YK |
107 | out: |
108 | mutex_unlock(&nf_ct_ext_type_mutex); | |
109 | return ret; | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(nf_ct_extend_register); | |
112 | ||
113 | /* This MUST be called in process context. */ | |
23f671a1 | 114 | void nf_ct_extend_unregister(const struct nf_ct_ext_type *type) |
ecfab2c9 YK |
115 | { |
116 | mutex_lock(&nf_ct_ext_type_mutex); | |
a9b3cd7f | 117 | RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL); |
ecfab2c9 | 118 | mutex_unlock(&nf_ct_ext_type_mutex); |
9c3f3794 | 119 | synchronize_rcu(); |
ecfab2c9 YK |
120 | } |
121 | EXPORT_SYMBOL_GPL(nf_ct_extend_unregister); |