Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 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 *nf_ct_ext_types[NF_CT_EXT_NUM]; |
| 20 | static DEFINE_MUTEX(nf_ct_ext_type_mutex); |
| 21 | |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 22 | void __nf_ct_ext_destroy(struct nf_conn *ct) |
| 23 | { |
| 24 | unsigned int i; |
| 25 | struct nf_ct_ext_type *t; |
| 26 | |
| 27 | for (i = 0; i < NF_CT_EXT_NUM; i++) { |
| 28 | if (!nf_ct_ext_exist(ct, i)) |
| 29 | continue; |
| 30 | |
| 31 | rcu_read_lock(); |
| 32 | t = rcu_dereference(nf_ct_ext_types[i]); |
| 33 | |
| 34 | /* Here the nf_ct_ext_type might have been unregisterd. |
| 35 | * I.e., it has responsible to cleanup private |
| 36 | * area in all conntracks when it is unregisterd. |
| 37 | */ |
| 38 | if (t && t->destroy) |
| 39 | t->destroy(ct); |
| 40 | rcu_read_unlock(); |
| 41 | } |
| 42 | } |
| 43 | EXPORT_SYMBOL(__nf_ct_ext_destroy); |
| 44 | |
| 45 | static void * |
| 46 | nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id, gfp_t gfp) |
| 47 | { |
Pekka Enberg | 019f692 | 2008-03-10 16:43:41 -0700 | [diff] [blame] | 48 | unsigned int off, len; |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 49 | struct nf_ct_ext_type *t; |
| 50 | |
| 51 | rcu_read_lock(); |
| 52 | t = rcu_dereference(nf_ct_ext_types[id]); |
| 53 | BUG_ON(t == NULL); |
| 54 | off = ALIGN(sizeof(struct nf_ct_ext), t->align); |
| 55 | len = off + t->len; |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 56 | rcu_read_unlock(); |
| 57 | |
Pekka Enberg | 019f692 | 2008-03-10 16:43:41 -0700 | [diff] [blame] | 58 | *ext = kzalloc(t->alloc_size, gfp); |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 59 | if (!*ext) |
| 60 | return NULL; |
| 61 | |
Patrick McHardy | 68b80f1 | 2008-06-17 15:51:47 -0700 | [diff] [blame] | 62 | INIT_RCU_HEAD(&(*ext)->rcu); |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 63 | (*ext)->offset[id] = off; |
| 64 | (*ext)->len = len; |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 65 | |
| 66 | return (void *)(*ext) + off; |
| 67 | } |
| 68 | |
Patrick McHardy | 68b80f1 | 2008-06-17 15:51:47 -0700 | [diff] [blame] | 69 | static 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 Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 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 *new; |
| 78 | int i, newlen, newoff; |
| 79 | struct nf_ct_ext_type *t; |
| 80 | |
Patrick McHardy | 55871d0 | 2008-04-14 11:15:51 +0200 | [diff] [blame] | 81 | /* Conntrack must not be confirmed to avoid races on reallocation. */ |
| 82 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
| 83 | |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 84 | if (!ct->ext) |
| 85 | return nf_ct_ext_create(&ct->ext, id, gfp); |
| 86 | |
| 87 | if (nf_ct_ext_exist(ct, id)) |
| 88 | return NULL; |
| 89 | |
| 90 | rcu_read_lock(); |
| 91 | t = rcu_dereference(nf_ct_ext_types[id]); |
| 92 | BUG_ON(t == NULL); |
| 93 | |
| 94 | newoff = ALIGN(ct->ext->len, t->align); |
| 95 | newlen = newoff + t->len; |
| 96 | rcu_read_unlock(); |
| 97 | |
Pekka Enberg | 93bc4e8 | 2008-07-26 17:49:33 -0700 | [diff] [blame] | 98 | new = __krealloc(ct->ext, newlen, gfp); |
Pekka Enberg | 31d8519 | 2008-06-09 15:58:39 -0700 | [diff] [blame] | 99 | if (!new) |
| 100 | return NULL; |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 101 | |
Pekka Enberg | 31d8519 | 2008-06-09 15:58:39 -0700 | [diff] [blame] | 102 | if (new != ct->ext) { |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 103 | for (i = 0; i < NF_CT_EXT_NUM; i++) { |
| 104 | if (!nf_ct_ext_exist(ct, i)) |
| 105 | continue; |
| 106 | |
| 107 | rcu_read_lock(); |
| 108 | t = rcu_dereference(nf_ct_ext_types[i]); |
| 109 | if (t && t->move) |
Patrick McHardy | 86577c6 | 2008-02-07 17:56:34 -0800 | [diff] [blame] | 110 | t->move((void *)new + new->offset[i], |
| 111 | (void *)ct->ext + ct->ext->offset[i]); |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 112 | rcu_read_unlock(); |
| 113 | } |
Patrick McHardy | 68b80f1 | 2008-06-17 15:51:47 -0700 | [diff] [blame] | 114 | call_rcu(&ct->ext->rcu, __nf_ct_ext_free_rcu); |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 115 | ct->ext = new; |
| 116 | } |
| 117 | |
Patrick McHardy | 6c64825 | 2008-07-26 17:50:05 -0700 | [diff] [blame] | 118 | new->offset[id] = newoff; |
| 119 | new->len = newlen; |
| 120 | memset((void *)new + newoff, 0, newlen - newoff); |
| 121 | return (void *)new + newoff; |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL(__nf_ct_ext_add); |
| 124 | |
| 125 | static void update_alloc_size(struct nf_ct_ext_type *type) |
| 126 | { |
| 127 | int i, j; |
| 128 | struct nf_ct_ext_type *t1, *t2; |
| 129 | enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1; |
| 130 | |
| 131 | /* unnecessary to update all types */ |
| 132 | if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) { |
| 133 | min = type->id; |
| 134 | max = type->id; |
| 135 | } |
| 136 | |
| 137 | /* This assumes that extended areas in conntrack for the types |
| 138 | whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */ |
| 139 | for (i = min; i <= max; i++) { |
| 140 | t1 = nf_ct_ext_types[i]; |
| 141 | if (!t1) |
| 142 | continue; |
| 143 | |
| 144 | t1->alloc_size = sizeof(struct nf_ct_ext) |
| 145 | + ALIGN(sizeof(struct nf_ct_ext), t1->align) |
| 146 | + t1->len; |
| 147 | for (j = 0; j < NF_CT_EXT_NUM; j++) { |
| 148 | t2 = nf_ct_ext_types[j]; |
| 149 | if (t2 == NULL || t2 == t1 || |
| 150 | (t2->flags & NF_CT_EXT_F_PREALLOC) == 0) |
| 151 | continue; |
| 152 | |
| 153 | t1->alloc_size = ALIGN(t1->alloc_size, t2->align) |
| 154 | + t2->len; |
| 155 | } |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /* This MUST be called in process context. */ |
| 160 | int nf_ct_extend_register(struct nf_ct_ext_type *type) |
| 161 | { |
| 162 | int ret = 0; |
| 163 | |
| 164 | mutex_lock(&nf_ct_ext_type_mutex); |
| 165 | if (nf_ct_ext_types[type->id]) { |
| 166 | ret = -EBUSY; |
| 167 | goto out; |
| 168 | } |
| 169 | |
| 170 | /* This ensures that nf_ct_ext_create() can allocate enough area |
| 171 | before updating alloc_size */ |
| 172 | type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align) |
| 173 | + type->len; |
| 174 | rcu_assign_pointer(nf_ct_ext_types[type->id], type); |
| 175 | update_alloc_size(type); |
| 176 | out: |
| 177 | mutex_unlock(&nf_ct_ext_type_mutex); |
| 178 | return ret; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(nf_ct_extend_register); |
| 181 | |
| 182 | /* This MUST be called in process context. */ |
| 183 | void nf_ct_extend_unregister(struct nf_ct_ext_type *type) |
| 184 | { |
| 185 | mutex_lock(&nf_ct_ext_type_mutex); |
| 186 | rcu_assign_pointer(nf_ct_ext_types[type->id], NULL); |
| 187 | update_alloc_size(type); |
| 188 | mutex_unlock(&nf_ct_ext_type_mutex); |
Jesper Dangaard Brouer | 308ff82 | 2009-06-25 16:32:52 +0200 | [diff] [blame] | 189 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 190 | } |
| 191 | EXPORT_SYMBOL_GPL(nf_ct_extend_unregister); |