Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebtables |
| 3 | * |
| 4 | * Author: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
| 6 | * |
| 7 | * ebtables.c,v 2.0, July, 2002 |
| 8 | * |
| 9 | * This code is stongly inspired on the iptables code which is |
| 10 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kmod.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/vmalloc.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 21 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/netfilter_bridge/ebtables.h> |
| 23 | #include <linux/spinlock.h> |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> |
| 27 | #include <linux/smp.h> |
David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 28 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/sock.h> |
| 30 | /* needed for logical [in,out]-dev filtering */ |
| 31 | #include "../br_private.h" |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 34 | "report to author: "format, ## args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* #define BUGPRINT(format, args...) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Each cpu has its own set of counters, so there is no need for write_lock in |
| 39 | * the softirq |
| 40 | * For reading or updating the counters, the user context needs to |
| 41 | * get a write_lock |
| 42 | */ |
| 43 | |
| 44 | /* The size of each set of counters is altered to get cache alignment */ |
| 45 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) |
| 46 | #define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter))) |
| 47 | #define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \ |
| 48 | COUNTER_OFFSET(n) * cpu)) |
| 49 | |
| 50 | |
| 51 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 52 | static DEFINE_MUTEX(ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_COMPAT |
| 55 | static void ebt_standard_compat_from_user(void *dst, const void *src) |
| 56 | { |
| 57 | int v = *(compat_int_t *)src; |
| 58 | |
| 59 | if (v >= 0) |
| 60 | v += xt_compat_calc_jump(NFPROTO_BRIDGE, v); |
| 61 | memcpy(dst, &v, sizeof(v)); |
| 62 | } |
| 63 | |
| 64 | static int ebt_standard_compat_to_user(void __user *dst, const void *src) |
| 65 | { |
| 66 | compat_int_t cv = *(int *)src; |
| 67 | |
| 68 | if (cv >= 0) |
| 69 | cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv); |
| 70 | return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 75 | static struct xt_target ebt_standard_target = { |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 76 | .name = "standard", |
| 77 | .revision = 0, |
| 78 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 79 | .targetsize = sizeof(int), |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 80 | #ifdef CONFIG_COMPAT |
| 81 | .compatsize = sizeof(compat_int_t), |
| 82 | .compat_from_user = ebt_standard_compat_from_user, |
| 83 | .compat_to_user = ebt_standard_compat_to_user, |
| 84 | #endif |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 85 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 87 | static inline int |
| 88 | ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb, |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 89 | struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 91 | par->target = w->u.watcher; |
| 92 | par->targinfo = w->data; |
| 93 | w->u.watcher->target(skb, par); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* watchers don't give a verdict */ |
| 95 | return 0; |
| 96 | } |
| 97 | |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 98 | static inline int |
| 99 | ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb, |
| 100 | struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 102 | par->match = m->u.match; |
| 103 | par->matchinfo = m->data; |
Jan Engelhardt | d61ba9f | 2009-01-12 00:06:06 +0000 | [diff] [blame] | 104 | return m->u.match->match(skb, par) ? EBT_MATCH : EBT_NOMATCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 107 | static inline int |
| 108 | ebt_dev_check(const char *entry, const struct net_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | int i = 0; |
Julia Lawall | f3d8b2e | 2009-01-09 10:22:22 +0000 | [diff] [blame] | 111 | const char *devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | if (*entry == '\0') |
| 114 | return 0; |
| 115 | if (!device) |
| 116 | return 1; |
Julia Lawall | f3d8b2e | 2009-01-09 10:22:22 +0000 | [diff] [blame] | 117 | devname = device->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* 1 is the wildcard token */ |
| 119 | while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i]) |
| 120 | i++; |
| 121 | return (devname[i] != entry[i] && entry[i] != 1); |
| 122 | } |
| 123 | |
| 124 | #define FWINV2(bool,invflg) ((bool) ^ !!(e->invflags & invflg)) |
| 125 | /* process standard matches */ |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 126 | static inline int |
| 127 | ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h, |
| 128 | const struct net_device *in, const struct net_device *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | int verdict, i; |
| 131 | |
| 132 | if (e->bitmask & EBT_802_3) { |
| 133 | if (FWINV2(ntohs(h->h_proto) >= 1536, EBT_IPROTO)) |
| 134 | return 1; |
| 135 | } else if (!(e->bitmask & EBT_NOPROTO) && |
| 136 | FWINV2(e->ethproto != h->h_proto, EBT_IPROTO)) |
| 137 | return 1; |
| 138 | |
| 139 | if (FWINV2(ebt_dev_check(e->in, in), EBT_IIN)) |
| 140 | return 1; |
| 141 | if (FWINV2(ebt_dev_check(e->out, out), EBT_IOUT)) |
| 142 | return 1; |
| 143 | if ((!in || !in->br_port) ? 0 : FWINV2(ebt_dev_check( |
| 144 | e->logical_in, in->br_port->br->dev), EBT_ILOGICALIN)) |
| 145 | return 1; |
| 146 | if ((!out || !out->br_port) ? 0 : FWINV2(ebt_dev_check( |
| 147 | e->logical_out, out->br_port->br->dev), EBT_ILOGICALOUT)) |
| 148 | return 1; |
| 149 | |
| 150 | if (e->bitmask & EBT_SOURCEMAC) { |
| 151 | verdict = 0; |
| 152 | for (i = 0; i < 6; i++) |
| 153 | verdict |= (h->h_source[i] ^ e->sourcemac[i]) & |
| 154 | e->sourcemsk[i]; |
| 155 | if (FWINV2(verdict != 0, EBT_ISOURCE) ) |
| 156 | return 1; |
| 157 | } |
| 158 | if (e->bitmask & EBT_DESTMAC) { |
| 159 | verdict = 0; |
| 160 | for (i = 0; i < 6; i++) |
| 161 | verdict |= (h->h_dest[i] ^ e->destmac[i]) & |
| 162 | e->destmsk[i]; |
| 163 | if (FWINV2(verdict != 0, EBT_IDEST) ) |
| 164 | return 1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jan Engelhardt | 98e8640 | 2009-04-15 21:06:05 +0200 | [diff] [blame] | 169 | static inline __pure |
| 170 | struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry) |
| 171 | { |
| 172 | return (void *)entry + entry->next_offset; |
| 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* Do some firewalling */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 176 | unsigned int ebt_do_table (unsigned int hook, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | const struct net_device *in, const struct net_device *out, |
| 178 | struct ebt_table *table) |
| 179 | { |
| 180 | int i, nentries; |
| 181 | struct ebt_entry *point; |
| 182 | struct ebt_counter *counter_base, *cb_base; |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 183 | const struct ebt_entry_target *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int verdict, sp = 0; |
| 185 | struct ebt_chainstack *cs; |
| 186 | struct ebt_entries *chaininfo; |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 187 | const char *base; |
| 188 | const struct ebt_table_info *private; |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 189 | struct xt_action_param acpar; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 190 | |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 191 | acpar.family = NFPROTO_BRIDGE; |
| 192 | acpar.in = in; |
| 193 | acpar.out = out; |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame^] | 194 | acpar.hotdrop = false; |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 195 | acpar.hooknum = hook; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | read_lock_bh(&table->lock); |
| 198 | private = table->private; |
| 199 | cb_base = COUNTER_BASE(private->counters, private->nentries, |
| 200 | smp_processor_id()); |
| 201 | if (private->chainstack) |
| 202 | cs = private->chainstack[smp_processor_id()]; |
| 203 | else |
| 204 | cs = NULL; |
| 205 | chaininfo = private->hook_entry[hook]; |
| 206 | nentries = private->hook_entry[hook]->nentries; |
| 207 | point = (struct ebt_entry *)(private->hook_entry[hook]->data); |
| 208 | counter_base = cb_base + private->hook_entry[hook]->counter_offset; |
| 209 | /* base for chain jumps */ |
| 210 | base = private->entries; |
| 211 | i = 0; |
| 212 | while (i < nentries) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 213 | if (ebt_basic_match(point, eth_hdr(skb), in, out)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | goto letscontinue; |
| 215 | |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 216 | if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | goto letscontinue; |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame^] | 218 | if (acpar.hotdrop) { |
Jan Engelhardt | 5365f80 | 2008-10-08 11:35:16 +0200 | [diff] [blame] | 219 | read_unlock_bh(&table->lock); |
| 220 | return NF_DROP; |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | /* increase counter */ |
| 224 | (*(counter_base + i)).pcnt++; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 225 | (*(counter_base + i)).bcnt += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* these should only watch: not modify, nor tell us |
| 228 | what to do with the packet */ |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 229 | EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | t = (struct ebt_entry_target *) |
| 232 | (((char *)point) + point->target_offset); |
| 233 | /* standard target */ |
| 234 | if (!t->u.target->target) |
| 235 | verdict = ((struct ebt_standard_target *)t)->verdict; |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 236 | else { |
Jan Engelhardt | de74c16 | 2009-07-05 18:26:37 +0200 | [diff] [blame] | 237 | acpar.target = t->u.target; |
| 238 | acpar.targinfo = t->data; |
| 239 | verdict = t->u.target->target(skb, &acpar); |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (verdict == EBT_ACCEPT) { |
| 242 | read_unlock_bh(&table->lock); |
| 243 | return NF_ACCEPT; |
| 244 | } |
| 245 | if (verdict == EBT_DROP) { |
| 246 | read_unlock_bh(&table->lock); |
| 247 | return NF_DROP; |
| 248 | } |
| 249 | if (verdict == EBT_RETURN) { |
| 250 | letsreturn: |
| 251 | #ifdef CONFIG_NETFILTER_DEBUG |
| 252 | if (sp == 0) { |
| 253 | BUGPRINT("RETURN on base chain"); |
| 254 | /* act like this is EBT_CONTINUE */ |
| 255 | goto letscontinue; |
| 256 | } |
| 257 | #endif |
| 258 | sp--; |
| 259 | /* put all the local variables right */ |
| 260 | i = cs[sp].n; |
| 261 | chaininfo = cs[sp].chaininfo; |
| 262 | nentries = chaininfo->nentries; |
| 263 | point = cs[sp].e; |
| 264 | counter_base = cb_base + |
| 265 | chaininfo->counter_offset; |
| 266 | continue; |
| 267 | } |
| 268 | if (verdict == EBT_CONTINUE) |
| 269 | goto letscontinue; |
| 270 | #ifdef CONFIG_NETFILTER_DEBUG |
| 271 | if (verdict < 0) { |
| 272 | BUGPRINT("bogus standard verdict\n"); |
| 273 | read_unlock_bh(&table->lock); |
| 274 | return NF_DROP; |
| 275 | } |
| 276 | #endif |
| 277 | /* jump to a udc */ |
| 278 | cs[sp].n = i + 1; |
| 279 | cs[sp].chaininfo = chaininfo; |
Jan Engelhardt | 98e8640 | 2009-04-15 21:06:05 +0200 | [diff] [blame] | 280 | cs[sp].e = ebt_next_entry(point); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | i = 0; |
| 282 | chaininfo = (struct ebt_entries *) (base + verdict); |
| 283 | #ifdef CONFIG_NETFILTER_DEBUG |
| 284 | if (chaininfo->distinguisher) { |
| 285 | BUGPRINT("jump to non-chain\n"); |
| 286 | read_unlock_bh(&table->lock); |
| 287 | return NF_DROP; |
| 288 | } |
| 289 | #endif |
| 290 | nentries = chaininfo->nentries; |
| 291 | point = (struct ebt_entry *)chaininfo->data; |
| 292 | counter_base = cb_base + chaininfo->counter_offset; |
| 293 | sp++; |
| 294 | continue; |
| 295 | letscontinue: |
Jan Engelhardt | 98e8640 | 2009-04-15 21:06:05 +0200 | [diff] [blame] | 296 | point = ebt_next_entry(point); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | i++; |
| 298 | } |
| 299 | |
| 300 | /* I actually like this :) */ |
| 301 | if (chaininfo->policy == EBT_RETURN) |
| 302 | goto letsreturn; |
| 303 | if (chaininfo->policy == EBT_ACCEPT) { |
| 304 | read_unlock_bh(&table->lock); |
| 305 | return NF_ACCEPT; |
| 306 | } |
| 307 | read_unlock_bh(&table->lock); |
| 308 | return NF_DROP; |
| 309 | } |
| 310 | |
| 311 | /* If it succeeds, returns element and locks mutex */ |
| 312 | static inline void * |
| 313 | find_inlist_lock_noload(struct list_head *head, const char *name, int *error, |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 314 | struct mutex *mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 316 | struct { |
| 317 | struct list_head list; |
| 318 | char name[EBT_FUNCTION_MAXNAMELEN]; |
| 319 | } *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 321 | *error = mutex_lock_interruptible(mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (*error != 0) |
| 323 | return NULL; |
| 324 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 325 | list_for_each_entry(e, head, list) { |
| 326 | if (strcmp(e->name, name) == 0) |
| 327 | return e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 329 | *error = -ENOENT; |
| 330 | mutex_unlock(mutex); |
| 331 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static void * |
| 335 | find_inlist_lock(struct list_head *head, const char *name, const char *prefix, |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 336 | int *error, struct mutex *mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 338 | return try_then_request_module( |
| 339 | find_inlist_lock_noload(head, name, error, mutex), |
| 340 | "%s%s", prefix, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | static inline struct ebt_table * |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 344 | find_table_lock(struct net *net, const char *name, int *error, |
| 345 | struct mutex *mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 347 | return find_inlist_lock(&net->xt.tables[NFPROTO_BRIDGE], name, |
| 348 | "ebtable_", error, mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | static inline int |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 352 | ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par, |
| 353 | unsigned int *cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 355 | const struct ebt_entry *e = par->entryinfo; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 356 | struct xt_match *match; |
Al Viro | 14197d5 | 2006-11-30 19:25:21 -0800 | [diff] [blame] | 357 | size_t left = ((char *)e + e->watchers_offset) - (char *)m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int ret; |
| 359 | |
Al Viro | 14197d5 | 2006-11-30 19:25:21 -0800 | [diff] [blame] | 360 | if (left < sizeof(struct ebt_entry_match) || |
| 361 | left - sizeof(struct ebt_entry_match) < m->match_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return -EINVAL; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 363 | |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 364 | match = xt_request_find_match(NFPROTO_BRIDGE, m->u.name, 0); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 365 | if (IS_ERR(match)) |
| 366 | return PTR_ERR(match); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 367 | m->u.match = match; |
| 368 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 369 | par->match = match; |
| 370 | par->matchinfo = m->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 371 | ret = xt_check_match(par, m->match_size, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 372 | e->ethproto, e->invflags & EBT_IPROTO); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 373 | if (ret < 0) { |
| 374 | module_put(match->me); |
| 375 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | (*cnt)++; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static inline int |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 383 | ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par, |
| 384 | unsigned int *cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 386 | const struct ebt_entry *e = par->entryinfo; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 387 | struct xt_target *watcher; |
Al Viro | 14197d5 | 2006-11-30 19:25:21 -0800 | [diff] [blame] | 388 | size_t left = ((char *)e + e->target_offset) - (char *)w; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | int ret; |
| 390 | |
Al Viro | 14197d5 | 2006-11-30 19:25:21 -0800 | [diff] [blame] | 391 | if (left < sizeof(struct ebt_entry_watcher) || |
| 392 | left - sizeof(struct ebt_entry_watcher) < w->watcher_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return -EINVAL; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 394 | |
Jan Engelhardt | d2a7b6b | 2009-07-10 18:55:11 +0200 | [diff] [blame] | 395 | watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 396 | if (IS_ERR(watcher)) |
| 397 | return PTR_ERR(watcher); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 398 | w->u.watcher = watcher; |
| 399 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 400 | par->target = watcher; |
| 401 | par->targinfo = w->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 402 | ret = xt_check_target(par, w->watcher_size, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 403 | e->ethproto, e->invflags & EBT_IPROTO); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 404 | if (ret < 0) { |
| 405 | module_put(watcher->me); |
| 406 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | (*cnt)++; |
| 410 | return 0; |
| 411 | } |
| 412 | |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 413 | static int ebt_verify_pointers(const struct ebt_replace *repl, |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 414 | struct ebt_table_info *newinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 416 | unsigned int limit = repl->entries_size; |
| 417 | unsigned int valid_hooks = repl->valid_hooks; |
| 418 | unsigned int offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int i; |
| 420 | |
Al Viro | e4fd77d | 2006-11-30 19:26:35 -0800 | [diff] [blame] | 421 | for (i = 0; i < NF_BR_NUMHOOKS; i++) |
| 422 | newinfo->hook_entry[i] = NULL; |
| 423 | |
| 424 | newinfo->entries_size = repl->entries_size; |
| 425 | newinfo->nentries = repl->nentries; |
| 426 | |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 427 | while (offset < limit) { |
| 428 | size_t left = limit - offset; |
| 429 | struct ebt_entry *e = (void *)newinfo->entries + offset; |
Al Viro | bb2ef25 | 2006-11-30 19:22:42 -0800 | [diff] [blame] | 430 | |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 431 | if (left < sizeof(unsigned int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | break; |
Al Viro | 22b440b | 2006-11-30 19:25:51 -0800 | [diff] [blame] | 433 | |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 434 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 435 | if ((valid_hooks & (1 << i)) == 0) |
| 436 | continue; |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 437 | if ((char __user *)repl->hook_entry[i] == |
| 438 | repl->entries + offset) |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | |
| 442 | if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) { |
| 443 | if (e->bitmask != 0) { |
| 444 | /* we make userspace set this right, |
| 445 | so there is no misunderstanding */ |
| 446 | BUGPRINT("EBT_ENTRY_OR_ENTRIES shouldn't be set " |
| 447 | "in distinguisher\n"); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | if (i != NF_BR_NUMHOOKS) |
| 451 | newinfo->hook_entry[i] = (struct ebt_entries *)e; |
| 452 | if (left < sizeof(struct ebt_entries)) |
| 453 | break; |
| 454 | offset += sizeof(struct ebt_entries); |
| 455 | } else { |
| 456 | if (left < sizeof(struct ebt_entry)) |
| 457 | break; |
| 458 | if (left < e->next_offset) |
| 459 | break; |
Florian Westphal | 1756de2 | 2010-02-15 18:15:55 +0100 | [diff] [blame] | 460 | if (e->next_offset < sizeof(struct ebt_entry)) |
| 461 | return -EINVAL; |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 462 | offset += e->next_offset; |
| 463 | } |
| 464 | } |
| 465 | if (offset != limit) { |
| 466 | BUGPRINT("entries_size too small\n"); |
| 467 | return -EINVAL; |
| 468 | } |
Al Viro | e4fd77d | 2006-11-30 19:26:35 -0800 | [diff] [blame] | 469 | |
| 470 | /* check if all valid hooks have a chain */ |
| 471 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 472 | if (!newinfo->hook_entry[i] && |
| 473 | (valid_hooks & (1 << i))) { |
| 474 | BUGPRINT("Valid hook without chain\n"); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | } |
Al Viro | 70fe9af | 2006-11-30 19:26:14 -0800 | [diff] [blame] | 478 | return 0; |
Al Viro | 22b440b | 2006-11-30 19:25:51 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | * this one is very careful, as it is the first function |
| 483 | * to parse the userspace data |
| 484 | */ |
| 485 | static inline int |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 486 | ebt_check_entry_size_and_hooks(const struct ebt_entry *e, |
| 487 | const struct ebt_table_info *newinfo, |
Al Viro | 0e79553 | 2006-11-30 19:27:13 -0800 | [diff] [blame] | 488 | unsigned int *n, unsigned int *cnt, |
| 489 | unsigned int *totalcnt, unsigned int *udc_cnt) |
Al Viro | 22b440b | 2006-11-30 19:25:51 -0800 | [diff] [blame] | 490 | { |
Al Viro | 22b440b | 2006-11-30 19:25:51 -0800 | [diff] [blame] | 491 | int i; |
| 492 | |
| 493 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
Al Viro | 0e79553 | 2006-11-30 19:27:13 -0800 | [diff] [blame] | 494 | if ((void *)e == (void *)newinfo->hook_entry[i]) |
Al Viro | 22b440b | 2006-11-30 19:25:51 -0800 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | /* beginning of a new chain |
| 498 | if i == NF_BR_NUMHOOKS it must be a user defined chain */ |
| 499 | if (i != NF_BR_NUMHOOKS || !e->bitmask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | /* this checks if the previous chain has as many entries |
| 501 | as it said it has */ |
| 502 | if (*n != *cnt) { |
| 503 | BUGPRINT("nentries does not equal the nr of entries " |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 504 | "in the chain\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return -EINVAL; |
| 506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (((struct ebt_entries *)e)->policy != EBT_DROP && |
| 508 | ((struct ebt_entries *)e)->policy != EBT_ACCEPT) { |
| 509 | /* only RETURN from udc */ |
| 510 | if (i != NF_BR_NUMHOOKS || |
| 511 | ((struct ebt_entries *)e)->policy != EBT_RETURN) { |
| 512 | BUGPRINT("bad policy\n"); |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | } |
| 516 | if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */ |
| 517 | (*udc_cnt)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (((struct ebt_entries *)e)->counter_offset != *totalcnt) { |
| 519 | BUGPRINT("counter_offset != totalcnt"); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | *n = ((struct ebt_entries *)e)->nentries; |
| 523 | *cnt = 0; |
| 524 | return 0; |
| 525 | } |
| 526 | /* a plain old entry, heh */ |
| 527 | if (sizeof(struct ebt_entry) > e->watchers_offset || |
| 528 | e->watchers_offset > e->target_offset || |
| 529 | e->target_offset >= e->next_offset) { |
| 530 | BUGPRINT("entry offsets not in right order\n"); |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | /* this is not checked anywhere else */ |
| 534 | if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target)) { |
| 535 | BUGPRINT("target size too small\n"); |
| 536 | return -EINVAL; |
| 537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | (*cnt)++; |
| 539 | (*totalcnt)++; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | struct ebt_cl_stack |
| 544 | { |
| 545 | struct ebt_chainstack cs; |
| 546 | int from; |
| 547 | unsigned int hookmask; |
| 548 | }; |
| 549 | |
| 550 | /* |
| 551 | * we need these positions to check that the jumps to a different part of the |
| 552 | * entries is a jump to the beginning of a new chain. |
| 553 | */ |
| 554 | static inline int |
| 555 | ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo, |
Al Viro | 177abc3 | 2006-11-30 19:27:32 -0800 | [diff] [blame] | 556 | unsigned int *n, struct ebt_cl_stack *udc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | int i; |
| 559 | |
| 560 | /* we're only interested in chain starts */ |
Al Viro | 40642f9 | 2006-11-30 19:24:12 -0800 | [diff] [blame] | 561 | if (e->bitmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return 0; |
| 563 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (newinfo->hook_entry[i] == (struct ebt_entries *)e) |
| 565 | break; |
| 566 | } |
| 567 | /* only care about udc */ |
| 568 | if (i != NF_BR_NUMHOOKS) |
| 569 | return 0; |
| 570 | |
| 571 | udc[*n].cs.chaininfo = (struct ebt_entries *)e; |
| 572 | /* these initialisations are depended on later in check_chainloops() */ |
| 573 | udc[*n].cs.n = 0; |
| 574 | udc[*n].hookmask = 0; |
| 575 | |
| 576 | (*n)++; |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static inline int |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 581 | ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 583 | struct xt_mtdtor_param par; |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (i && (*i)-- == 0) |
| 586 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 588 | par.net = net; |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 589 | par.match = m->u.match; |
| 590 | par.matchinfo = m->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 591 | par.family = NFPROTO_BRIDGE; |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 592 | if (par.match->destroy != NULL) |
| 593 | par.match->destroy(&par); |
| 594 | module_put(par.match->me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static inline int |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 599 | ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | { |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 601 | struct xt_tgdtor_param par; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (i && (*i)-- == 0) |
| 604 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 606 | par.net = net; |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 607 | par.target = w->u.watcher; |
| 608 | par.targinfo = w->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 609 | par.family = NFPROTO_BRIDGE; |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 610 | if (par.target->destroy != NULL) |
| 611 | par.target->destroy(&par); |
| 612 | module_put(par.target->me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static inline int |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 617 | ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 619 | struct xt_tgdtor_param par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | struct ebt_entry_target *t; |
| 621 | |
Al Viro | 40642f9 | 2006-11-30 19:24:12 -0800 | [diff] [blame] | 622 | if (e->bitmask == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | /* we're done */ |
| 625 | if (cnt && (*cnt)-- == 0) |
| 626 | return 1; |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 627 | EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL); |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 628 | EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | t = (struct ebt_entry_target *)(((char *)e) + e->target_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 631 | par.net = net; |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 632 | par.target = t->u.target; |
| 633 | par.targinfo = t->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 634 | par.family = NFPROTO_BRIDGE; |
Jan Engelhardt | a2df164 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 635 | if (par.target->destroy != NULL) |
| 636 | par.target->destroy(&par); |
| 637 | module_put(par.target->me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static inline int |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 642 | ebt_check_entry(struct ebt_entry *e, struct net *net, |
| 643 | const struct ebt_table_info *newinfo, |
Al Viro | f7da79d | 2006-11-30 19:27:48 -0800 | [diff] [blame] | 644 | const char *name, unsigned int *cnt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | struct ebt_cl_stack *cl_s, unsigned int udc_cnt) |
| 646 | { |
| 647 | struct ebt_entry_target *t; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 648 | struct xt_target *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | unsigned int i, j, hook = 0, hookmask = 0; |
Chuck Ebbert | 44f9a2f | 2007-01-04 12:17:44 -0800 | [diff] [blame] | 650 | size_t gap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | int ret; |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 652 | struct xt_mtchk_param mtpar; |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 653 | struct xt_tgchk_param tgpar; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | /* don't mess with the struct ebt_entries */ |
Al Viro | 40642f9 | 2006-11-30 19:24:12 -0800 | [diff] [blame] | 656 | if (e->bitmask == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return 0; |
| 658 | |
| 659 | if (e->bitmask & ~EBT_F_MASK) { |
| 660 | BUGPRINT("Unknown flag for bitmask\n"); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | if (e->invflags & ~EBT_INV_MASK) { |
| 664 | BUGPRINT("Unknown flag for inv bitmask\n"); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | if ( (e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3) ) { |
| 668 | BUGPRINT("NOPROTO & 802_3 not allowed\n"); |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | /* what hook do we belong to? */ |
| 672 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
Al Viro | f7da79d | 2006-11-30 19:27:48 -0800 | [diff] [blame] | 673 | if (!newinfo->hook_entry[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | continue; |
| 675 | if ((char *)newinfo->hook_entry[i] < (char *)e) |
| 676 | hook = i; |
| 677 | else |
| 678 | break; |
| 679 | } |
| 680 | /* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on |
| 681 | a base chain */ |
| 682 | if (i < NF_BR_NUMHOOKS) |
| 683 | hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS); |
| 684 | else { |
| 685 | for (i = 0; i < udc_cnt; i++) |
| 686 | if ((char *)(cl_s[i].cs.chaininfo) > (char *)e) |
| 687 | break; |
| 688 | if (i == 0) |
| 689 | hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS); |
| 690 | else |
| 691 | hookmask = cl_s[i - 1].hookmask; |
| 692 | } |
| 693 | i = 0; |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 694 | |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 695 | mtpar.net = tgpar.net = net; |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 696 | mtpar.table = tgpar.table = name; |
| 697 | mtpar.entryinfo = tgpar.entryinfo = e; |
| 698 | mtpar.hook_mask = tgpar.hook_mask = hookmask; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 699 | mtpar.family = tgpar.family = NFPROTO_BRIDGE; |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 700 | ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (ret != 0) |
| 702 | goto cleanup_matches; |
| 703 | j = 0; |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 704 | ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (ret != 0) |
| 706 | goto cleanup_watchers; |
| 707 | t = (struct ebt_entry_target *)(((char *)e) + e->target_offset); |
Chuck Ebbert | 44f9a2f | 2007-01-04 12:17:44 -0800 | [diff] [blame] | 708 | gap = e->next_offset - e->target_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Jan Engelhardt | d2a7b6b | 2009-07-10 18:55:11 +0200 | [diff] [blame] | 710 | target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 711 | if (IS_ERR(target)) { |
| 712 | ret = PTR_ERR(target); |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 713 | goto cleanup_watchers; |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | t->u.target = target; |
| 717 | if (t->u.target == &ebt_standard_target) { |
Al Viro | 14197d5 | 2006-11-30 19:25:21 -0800 | [diff] [blame] | 718 | if (gap < sizeof(struct ebt_standard_target)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | BUGPRINT("Standard target size too big\n"); |
| 720 | ret = -EFAULT; |
| 721 | goto cleanup_watchers; |
| 722 | } |
| 723 | if (((struct ebt_standard_target *)t)->verdict < |
| 724 | -NUM_STANDARD_TARGETS) { |
| 725 | BUGPRINT("Invalid standard target\n"); |
| 726 | ret = -EFAULT; |
| 727 | goto cleanup_watchers; |
| 728 | } |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 729 | } else if (t->target_size > gap - sizeof(struct ebt_entry_target)) { |
| 730 | module_put(t->u.target->me); |
| 731 | ret = -EFAULT; |
| 732 | goto cleanup_watchers; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 735 | tgpar.target = target; |
| 736 | tgpar.targinfo = t->data; |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 737 | ret = xt_check_target(&tgpar, t->target_size, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 738 | e->ethproto, e->invflags & EBT_IPROTO); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 739 | if (ret < 0) { |
| 740 | module_put(target->me); |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 741 | goto cleanup_watchers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | (*cnt)++; |
| 744 | return 0; |
| 745 | cleanup_watchers: |
Patrick McHardy | add6746 | 2010-02-03 13:45:12 +0100 | [diff] [blame] | 746 | EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | cleanup_matches: |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 748 | EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * checks for loops and sets the hook mask for udc |
| 754 | * the hook mask for udc tells us from which base chains the udc can be |
| 755 | * accessed. This mask is a parameter to the check() functions of the extensions |
| 756 | */ |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 757 | static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | unsigned int udc_cnt, unsigned int hooknr, char *base) |
| 759 | { |
| 760 | int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict; |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 761 | const struct ebt_entry *e = (struct ebt_entry *)chain->data; |
| 762 | const struct ebt_entry_target *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | while (pos < nentries || chain_nr != -1) { |
| 765 | /* end of udc, go back one 'recursion' step */ |
| 766 | if (pos == nentries) { |
| 767 | /* put back values of the time when this chain was called */ |
| 768 | e = cl_s[chain_nr].cs.e; |
| 769 | if (cl_s[chain_nr].from != -1) |
| 770 | nentries = |
| 771 | cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries; |
| 772 | else |
| 773 | nentries = chain->nentries; |
| 774 | pos = cl_s[chain_nr].cs.n; |
| 775 | /* make sure we won't see a loop that isn't one */ |
| 776 | cl_s[chain_nr].cs.n = 0; |
| 777 | chain_nr = cl_s[chain_nr].from; |
| 778 | if (pos == nentries) |
| 779 | continue; |
| 780 | } |
| 781 | t = (struct ebt_entry_target *) |
| 782 | (((char *)e) + e->target_offset); |
| 783 | if (strcmp(t->u.name, EBT_STANDARD_TARGET)) |
| 784 | goto letscontinue; |
| 785 | if (e->target_offset + sizeof(struct ebt_standard_target) > |
| 786 | e->next_offset) { |
| 787 | BUGPRINT("Standard target size too big\n"); |
| 788 | return -1; |
| 789 | } |
| 790 | verdict = ((struct ebt_standard_target *)t)->verdict; |
| 791 | if (verdict >= 0) { /* jump to another chain */ |
| 792 | struct ebt_entries *hlp2 = |
| 793 | (struct ebt_entries *)(base + verdict); |
| 794 | for (i = 0; i < udc_cnt; i++) |
| 795 | if (hlp2 == cl_s[i].cs.chaininfo) |
| 796 | break; |
| 797 | /* bad destination or loop */ |
| 798 | if (i == udc_cnt) { |
| 799 | BUGPRINT("bad destination\n"); |
| 800 | return -1; |
| 801 | } |
| 802 | if (cl_s[i].cs.n) { |
| 803 | BUGPRINT("loop\n"); |
| 804 | return -1; |
| 805 | } |
Al Viro | 98a0824 | 2006-11-30 19:24:49 -0800 | [diff] [blame] | 806 | if (cl_s[i].hookmask & (1 << hooknr)) |
| 807 | goto letscontinue; |
| 808 | /* this can't be 0, so the loop test is correct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | cl_s[i].cs.n = pos + 1; |
| 810 | pos = 0; |
Jan Engelhardt | 98e8640 | 2009-04-15 21:06:05 +0200 | [diff] [blame] | 811 | cl_s[i].cs.e = ebt_next_entry(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | e = (struct ebt_entry *)(hlp2->data); |
| 813 | nentries = hlp2->nentries; |
| 814 | cl_s[i].from = chain_nr; |
| 815 | chain_nr = i; |
| 816 | /* this udc is accessible from the base chain for hooknr */ |
| 817 | cl_s[i].hookmask |= (1 << hooknr); |
| 818 | continue; |
| 819 | } |
| 820 | letscontinue: |
Jan Engelhardt | 98e8640 | 2009-04-15 21:06:05 +0200 | [diff] [blame] | 821 | e = ebt_next_entry(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | pos++; |
| 823 | } |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* do the parsing of the table/chains/entries/matches/watchers/targets, heh */ |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 828 | static int translate_table(struct net *net, const char *name, |
Alexey Dobriyan | a83d8e8 | 2010-01-18 08:21:13 +0100 | [diff] [blame] | 829 | struct ebt_table_info *newinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
| 831 | unsigned int i, j, k, udc_cnt; |
| 832 | int ret; |
| 833 | struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */ |
| 834 | |
| 835 | i = 0; |
Al Viro | 1f072c9 | 2006-11-30 19:26:53 -0800 | [diff] [blame] | 836 | while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | i++; |
| 838 | if (i == NF_BR_NUMHOOKS) { |
| 839 | BUGPRINT("No valid hooks specified\n"); |
| 840 | return -EINVAL; |
| 841 | } |
Al Viro | 1f072c9 | 2006-11-30 19:26:53 -0800 | [diff] [blame] | 842 | if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | BUGPRINT("Chains don't start at beginning\n"); |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | /* make sure chains are ordered after each other in same order |
| 847 | as their corresponding hooks */ |
| 848 | for (j = i + 1; j < NF_BR_NUMHOOKS; j++) { |
Al Viro | 1f072c9 | 2006-11-30 19:26:53 -0800 | [diff] [blame] | 849 | if (!newinfo->hook_entry[j]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | continue; |
Al Viro | 1f072c9 | 2006-11-30 19:26:53 -0800 | [diff] [blame] | 851 | if (newinfo->hook_entry[j] <= newinfo->hook_entry[i]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | BUGPRINT("Hook order must be followed\n"); |
| 853 | return -EINVAL; |
| 854 | } |
| 855 | i = j; |
| 856 | } |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | /* do some early checkings and initialize some things */ |
| 859 | i = 0; /* holds the expected nr. of entries for the chain */ |
| 860 | j = 0; /* holds the up to now counted entries for the chain */ |
| 861 | k = 0; /* holds the total nr. of entries, should equal |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 862 | newinfo->nentries afterwards */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */ |
| 864 | ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size, |
Al Viro | 0e79553 | 2006-11-30 19:27:13 -0800 | [diff] [blame] | 865 | ebt_check_entry_size_and_hooks, newinfo, |
| 866 | &i, &j, &k, &udc_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | if (ret != 0) |
| 869 | return ret; |
| 870 | |
| 871 | if (i != j) { |
| 872 | BUGPRINT("nentries does not equal the nr of entries in the " |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 873 | "(last) chain\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return -EINVAL; |
| 875 | } |
| 876 | if (k != newinfo->nentries) { |
| 877 | BUGPRINT("Total nentries is wrong\n"); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* get the location of the udc, put them in an array |
| 882 | while we're at it, allocate the chainstack */ |
| 883 | if (udc_cnt) { |
| 884 | /* this will get free'd in do_replace()/ebt_register_table() |
| 885 | if an error occurs */ |
Jayachandran C | 7ad4d2f | 2006-04-11 17:25:38 -0700 | [diff] [blame] | 886 | newinfo->chainstack = |
Christoph Lameter | 53b8a31 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 887 | vmalloc(nr_cpu_ids * sizeof(*(newinfo->chainstack))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (!newinfo->chainstack) |
| 889 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 890 | for_each_possible_cpu(i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | newinfo->chainstack[i] = |
Jayachandran C | 18bc89a | 2006-04-20 00:14:49 -0700 | [diff] [blame] | 892 | vmalloc(udc_cnt * sizeof(*(newinfo->chainstack[0]))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (!newinfo->chainstack[i]) { |
| 894 | while (i) |
| 895 | vfree(newinfo->chainstack[--i]); |
| 896 | vfree(newinfo->chainstack); |
| 897 | newinfo->chainstack = NULL; |
| 898 | return -ENOMEM; |
| 899 | } |
| 900 | } |
| 901 | |
Jayachandran C | 18bc89a | 2006-04-20 00:14:49 -0700 | [diff] [blame] | 902 | cl_s = vmalloc(udc_cnt * sizeof(*cl_s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (!cl_s) |
| 904 | return -ENOMEM; |
| 905 | i = 0; /* the i'th udc */ |
| 906 | EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size, |
Al Viro | 177abc3 | 2006-11-30 19:27:32 -0800 | [diff] [blame] | 907 | ebt_get_udc_positions, newinfo, &i, cl_s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | /* sanity check */ |
| 909 | if (i != udc_cnt) { |
| 910 | BUGPRINT("i != udc_cnt\n"); |
| 911 | vfree(cl_s); |
| 912 | return -EFAULT; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* Check for loops */ |
| 917 | for (i = 0; i < NF_BR_NUMHOOKS; i++) |
Al Viro | 1f072c9 | 2006-11-30 19:26:53 -0800 | [diff] [blame] | 918 | if (newinfo->hook_entry[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (check_chainloops(newinfo->hook_entry[i], |
| 920 | cl_s, udc_cnt, i, newinfo->entries)) { |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 921 | vfree(cl_s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | return -EINVAL; |
| 923 | } |
| 924 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 925 | /* we now know the following (along with E=mc²): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | - the nr of entries in each chain is right |
| 927 | - the size of the allocated space is right |
| 928 | - all valid hooks have a corresponding chain |
| 929 | - there are no loops |
| 930 | - wrong data can still be on the level of a single entry |
| 931 | - could be there are jumps to places that are not the |
| 932 | beginning of a chain. This can only occur in chains that |
| 933 | are not accessible from any base chains, so we don't care. */ |
| 934 | |
| 935 | /* used to know what we need to clean up if something goes wrong */ |
| 936 | i = 0; |
| 937 | ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size, |
Alexey Dobriyan | a83d8e8 | 2010-01-18 08:21:13 +0100 | [diff] [blame] | 938 | ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (ret != 0) { |
| 940 | EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size, |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 941 | ebt_cleanup_entry, net, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 943 | vfree(cl_s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | /* called under write_lock */ |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 948 | static void get_counters(const struct ebt_counter *oldcounters, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | struct ebt_counter *counters, unsigned int nentries) |
| 950 | { |
| 951 | int i, cpu; |
| 952 | struct ebt_counter *counter_base; |
| 953 | |
| 954 | /* counters of cpu 0 */ |
| 955 | memcpy(counters, oldcounters, |
David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 956 | sizeof(struct ebt_counter) * nentries); |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* add other counters to those of cpu 0 */ |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 959 | for_each_possible_cpu(cpu) { |
David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 960 | if (cpu == 0) |
| 961 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | counter_base = COUNTER_BASE(oldcounters, nentries, cpu); |
| 963 | for (i = 0; i < nentries; i++) { |
| 964 | counters[i].pcnt += counter_base[i].pcnt; |
| 965 | counters[i].bcnt += counter_base[i].bcnt; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 970 | static int do_replace_finish(struct net *net, struct ebt_replace *repl, |
| 971 | struct ebt_table_info *newinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 973 | int ret, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | struct ebt_counter *counterstmp = NULL; |
| 975 | /* used to be able to unlock earlier */ |
| 976 | struct ebt_table_info *table; |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 977 | struct ebt_table *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | /* the user wants counters back |
| 980 | the check on the size is done later, when we have the lock */ |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 981 | if (repl->num_counters) { |
| 982 | unsigned long size = repl->num_counters * sizeof(*counterstmp); |
| 983 | counterstmp = vmalloc(size); |
| 984 | if (!counterstmp) |
| 985 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | newinfo->chainstack = NULL; |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 989 | ret = ebt_verify_pointers(repl, newinfo); |
Al Viro | 1bc2326 | 2006-11-30 19:28:08 -0800 | [diff] [blame] | 990 | if (ret != 0) |
| 991 | goto free_counterstmp; |
| 992 | |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 993 | ret = translate_table(net, repl->name, newinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | if (ret != 0) |
| 996 | goto free_counterstmp; |
| 997 | |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 998 | t = find_table_lock(net, repl->name, &ret, &ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | if (!t) { |
| 1000 | ret = -ENOENT; |
| 1001 | goto free_iterate; |
| 1002 | } |
| 1003 | |
| 1004 | /* the table doesn't like it */ |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 1005 | if (t->check && (ret = t->check(newinfo, repl->valid_hooks))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | goto free_unlock; |
| 1007 | |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 1008 | if (repl->num_counters && repl->num_counters != t->private->nentries) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | BUGPRINT("Wrong nr. of counters requested\n"); |
| 1010 | ret = -EINVAL; |
| 1011 | goto free_unlock; |
| 1012 | } |
| 1013 | |
| 1014 | /* we have the mutex lock, so no danger in reading this pointer */ |
| 1015 | table = t->private; |
| 1016 | /* make sure the table can only be rmmod'ed if it contains no rules */ |
| 1017 | if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) { |
| 1018 | ret = -ENOENT; |
| 1019 | goto free_unlock; |
| 1020 | } else if (table->nentries && !newinfo->nentries) |
| 1021 | module_put(t->me); |
| 1022 | /* we need an atomic snapshot of the counters */ |
| 1023 | write_lock_bh(&t->lock); |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 1024 | if (repl->num_counters) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | get_counters(t->private->counters, counterstmp, |
| 1026 | t->private->nentries); |
| 1027 | |
| 1028 | t->private = newinfo; |
| 1029 | write_unlock_bh(&t->lock); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1030 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | /* so, a user can change the chains while having messed up her counter |
| 1032 | allocation. Only reason why this is done is because this way the lock |
| 1033 | is held only once, while this doesn't bring the kernel into a |
| 1034 | dangerous state. */ |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 1035 | if (repl->num_counters && |
| 1036 | copy_to_user(repl->counters, counterstmp, |
| 1037 | repl->num_counters * sizeof(struct ebt_counter))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | ret = -EFAULT; |
| 1039 | } |
| 1040 | else |
| 1041 | ret = 0; |
| 1042 | |
| 1043 | /* decrease module count and free resources */ |
| 1044 | EBT_ENTRY_ITERATE(table->entries, table->entries_size, |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 1045 | ebt_cleanup_entry, net, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | vfree(table->entries); |
| 1048 | if (table->chainstack) { |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 1049 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | vfree(table->chainstack[i]); |
| 1051 | vfree(table->chainstack); |
| 1052 | } |
| 1053 | vfree(table); |
| 1054 | |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 1055 | vfree(counterstmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return ret; |
| 1057 | |
| 1058 | free_unlock: |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1059 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | free_iterate: |
| 1061 | EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size, |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 1062 | ebt_cleanup_entry, net, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | free_counterstmp: |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 1064 | vfree(counterstmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | /* can be initialized in translate_table() */ |
| 1066 | if (newinfo->chainstack) { |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 1067 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | vfree(newinfo->chainstack[i]); |
| 1069 | vfree(newinfo->chainstack); |
| 1070 | } |
Florian Westphal | e788759 | 2010-02-04 18:38:53 +0100 | [diff] [blame] | 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | /* replace the table */ |
| 1075 | static int do_replace(struct net *net, const void __user *user, |
| 1076 | unsigned int len) |
| 1077 | { |
| 1078 | int ret, countersize; |
| 1079 | struct ebt_table_info *newinfo; |
| 1080 | struct ebt_replace tmp; |
| 1081 | |
| 1082 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) |
| 1083 | return -EFAULT; |
| 1084 | |
| 1085 | if (len != sizeof(tmp) + tmp.entries_size) { |
| 1086 | BUGPRINT("Wrong len argument\n"); |
| 1087 | return -EINVAL; |
| 1088 | } |
| 1089 | |
| 1090 | if (tmp.entries_size == 0) { |
| 1091 | BUGPRINT("Entries_size never zero\n"); |
| 1092 | return -EINVAL; |
| 1093 | } |
| 1094 | /* overflow check */ |
| 1095 | if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) / |
| 1096 | NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter)) |
| 1097 | return -ENOMEM; |
| 1098 | if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter)) |
| 1099 | return -ENOMEM; |
| 1100 | |
| 1101 | countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids; |
| 1102 | newinfo = vmalloc(sizeof(*newinfo) + countersize); |
| 1103 | if (!newinfo) |
| 1104 | return -ENOMEM; |
| 1105 | |
| 1106 | if (countersize) |
| 1107 | memset(newinfo->counters, 0, countersize); |
| 1108 | |
| 1109 | newinfo->entries = vmalloc(tmp.entries_size); |
| 1110 | if (!newinfo->entries) { |
| 1111 | ret = -ENOMEM; |
| 1112 | goto free_newinfo; |
| 1113 | } |
| 1114 | if (copy_from_user( |
| 1115 | newinfo->entries, tmp.entries, tmp.entries_size) != 0) { |
| 1116 | BUGPRINT("Couldn't copy entries from userspace\n"); |
| 1117 | ret = -EFAULT; |
| 1118 | goto free_entries; |
| 1119 | } |
| 1120 | |
| 1121 | ret = do_replace_finish(net, &tmp, newinfo); |
| 1122 | if (ret == 0) |
| 1123 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | free_entries: |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 1125 | vfree(newinfo->entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | free_newinfo: |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 1127 | vfree(newinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1131 | struct ebt_table * |
| 1132 | ebt_register_table(struct net *net, const struct ebt_table *input_table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | { |
| 1134 | struct ebt_table_info *newinfo; |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1135 | struct ebt_table *t, *table; |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 1136 | struct ebt_replace_kernel *repl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | int ret, i, countersize; |
Al Viro | df07a81 | 2006-11-30 19:28:25 -0800 | [diff] [blame] | 1138 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1140 | if (input_table == NULL || (repl = input_table->table) == NULL || |
| 1141 | repl->entries == 0 || repl->entries_size == 0 || |
| 1142 | repl->counters != NULL || input_table->private != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | BUGPRINT("Bad table data for ebt_register_table!!!\n"); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1144 | return ERR_PTR(-EINVAL); |
| 1145 | } |
| 1146 | |
| 1147 | /* Don't add one table to multiple lists. */ |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1148 | table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1149 | if (!table) { |
| 1150 | ret = -ENOMEM; |
| 1151 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Christoph Lameter | 53b8a31 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 1154 | countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids; |
Jayachandran C | 18bc89a | 2006-04-20 00:14:49 -0700 | [diff] [blame] | 1155 | newinfo = vmalloc(sizeof(*newinfo) + countersize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | ret = -ENOMEM; |
| 1157 | if (!newinfo) |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1158 | goto free_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Al Viro | df07a81 | 2006-11-30 19:28:25 -0800 | [diff] [blame] | 1160 | p = vmalloc(repl->entries_size); |
| 1161 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | goto free_newinfo; |
| 1163 | |
Al Viro | df07a81 | 2006-11-30 19:28:25 -0800 | [diff] [blame] | 1164 | memcpy(p, repl->entries, repl->entries_size); |
| 1165 | newinfo->entries = p; |
| 1166 | |
| 1167 | newinfo->entries_size = repl->entries_size; |
| 1168 | newinfo->nentries = repl->nentries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (countersize) |
| 1171 | memset(newinfo->counters, 0, countersize); |
| 1172 | |
| 1173 | /* fill in newinfo and parse the entries */ |
| 1174 | newinfo->chainstack = NULL; |
Al Viro | df07a81 | 2006-11-30 19:28:25 -0800 | [diff] [blame] | 1175 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 1176 | if ((repl->valid_hooks & (1 << i)) == 0) |
| 1177 | newinfo->hook_entry[i] = NULL; |
| 1178 | else |
| 1179 | newinfo->hook_entry[i] = p + |
| 1180 | ((char *)repl->hook_entry[i] - repl->entries); |
| 1181 | } |
Alexey Dobriyan | a83d8e8 | 2010-01-18 08:21:13 +0100 | [diff] [blame] | 1182 | ret = translate_table(net, repl->name, newinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (ret != 0) { |
| 1184 | BUGPRINT("Translate_table failed\n"); |
| 1185 | goto free_chainstack; |
| 1186 | } |
| 1187 | |
| 1188 | if (table->check && table->check(newinfo, table->valid_hooks)) { |
| 1189 | BUGPRINT("The table doesn't like its own initial data, lol\n"); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1190 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | table->private = newinfo; |
| 1194 | rwlock_init(&table->lock); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1195 | ret = mutex_lock_interruptible(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | if (ret != 0) |
| 1197 | goto free_chainstack; |
| 1198 | |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 1199 | list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1200 | if (strcmp(t->name, table->name) == 0) { |
| 1201 | ret = -EEXIST; |
| 1202 | BUGPRINT("Table name already exists\n"); |
| 1203 | goto free_unlock; |
| 1204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | /* Hold a reference count if the chains aren't empty */ |
| 1208 | if (newinfo->nentries && !try_module_get(table->me)) { |
| 1209 | ret = -ENOENT; |
| 1210 | goto free_unlock; |
| 1211 | } |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 1212 | list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1213 | mutex_unlock(&ebt_mutex); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1214 | return table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | free_unlock: |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1216 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | free_chainstack: |
| 1218 | if (newinfo->chainstack) { |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 1219 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | vfree(newinfo->chainstack[i]); |
| 1221 | vfree(newinfo->chainstack); |
| 1222 | } |
| 1223 | vfree(newinfo->entries); |
| 1224 | free_newinfo: |
| 1225 | vfree(newinfo); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1226 | free_table: |
| 1227 | kfree(table); |
| 1228 | out: |
| 1229 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 1232 | void ebt_unregister_table(struct net *net, struct ebt_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | { |
| 1234 | int i; |
| 1235 | |
| 1236 | if (!table) { |
| 1237 | BUGPRINT("Request to unregister NULL table!!!\n"); |
| 1238 | return; |
| 1239 | } |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1240 | mutex_lock(&ebt_mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1241 | list_del(&table->list); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1242 | mutex_unlock(&ebt_mutex); |
Alexey Dobriyan | dbcdf85 | 2008-11-04 14:28:04 +0100 | [diff] [blame] | 1243 | EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size, |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 1244 | ebt_cleanup_entry, net, NULL); |
Alexey Dobriyan | dbcdf85 | 2008-11-04 14:28:04 +0100 | [diff] [blame] | 1245 | if (table->private->nentries) |
| 1246 | module_put(table->me); |
James Lamanna | 68d3187 | 2005-06-22 22:12:57 -0700 | [diff] [blame] | 1247 | vfree(table->private->entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | if (table->private->chainstack) { |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 1249 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | vfree(table->private->chainstack[i]); |
| 1251 | vfree(table->private->chainstack); |
| 1252 | } |
| 1253 | vfree(table->private); |
Alexey Dobriyan | 6beceee | 2008-11-04 14:27:15 +0100 | [diff] [blame] | 1254 | kfree(table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /* userspace just supplied us with counters */ |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1258 | static int do_update_counters(struct net *net, const char *name, |
| 1259 | struct ebt_counter __user *counters, |
| 1260 | unsigned int num_counters, |
| 1261 | const void __user *user, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
| 1263 | int i, ret; |
| 1264 | struct ebt_counter *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | struct ebt_table *t; |
| 1266 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1267 | if (num_counters == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | return -EINVAL; |
| 1269 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1270 | tmp = vmalloc(num_counters * sizeof(*tmp)); |
| 1271 | if (!tmp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1274 | t = find_table_lock(net, name, &ret, &ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | if (!t) |
| 1276 | goto free_tmp; |
| 1277 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1278 | if (num_counters != t->private->nentries) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | BUGPRINT("Wrong nr of counters\n"); |
| 1280 | ret = -EINVAL; |
| 1281 | goto unlock_mutex; |
| 1282 | } |
| 1283 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1284 | if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | ret = -EFAULT; |
| 1286 | goto unlock_mutex; |
| 1287 | } |
| 1288 | |
| 1289 | /* we want an atomic add of the counters */ |
| 1290 | write_lock_bh(&t->lock); |
| 1291 | |
| 1292 | /* we add to the counters of the first cpu */ |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1293 | for (i = 0; i < num_counters; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | t->private->counters[i].pcnt += tmp[i].pcnt; |
| 1295 | t->private->counters[i].bcnt += tmp[i].bcnt; |
| 1296 | } |
| 1297 | |
| 1298 | write_unlock_bh(&t->lock); |
| 1299 | ret = 0; |
| 1300 | unlock_mutex: |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1301 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | free_tmp: |
| 1303 | vfree(tmp); |
| 1304 | return ret; |
| 1305 | } |
| 1306 | |
Florian Westphal | 49facff | 2010-02-07 02:48:47 +0100 | [diff] [blame] | 1307 | static int update_counters(struct net *net, const void __user *user, |
| 1308 | unsigned int len) |
| 1309 | { |
| 1310 | struct ebt_replace hlp; |
| 1311 | |
| 1312 | if (copy_from_user(&hlp, user, sizeof(hlp))) |
| 1313 | return -EFAULT; |
| 1314 | |
| 1315 | if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter)) |
| 1316 | return -EINVAL; |
| 1317 | |
| 1318 | return do_update_counters(net, hlp.name, hlp.counters, |
| 1319 | hlp.num_counters, user, len); |
| 1320 | } |
| 1321 | |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1322 | static inline int ebt_make_matchname(const struct ebt_entry_match *m, |
| 1323 | const char *base, char __user *ubase) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 1325 | char __user *hlp = ubase + ((char *)m - base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | if (copy_to_user(hlp, m->u.match->name, EBT_FUNCTION_MAXNAMELEN)) |
| 1327 | return -EFAULT; |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1331 | static inline int ebt_make_watchername(const struct ebt_entry_watcher *w, |
| 1332 | const char *base, char __user *ubase) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | { |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 1334 | char __user *hlp = ubase + ((char *)w - base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if (copy_to_user(hlp , w->u.watcher->name, EBT_FUNCTION_MAXNAMELEN)) |
| 1336 | return -EFAULT; |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1340 | static inline int |
| 1341 | ebt_make_names(struct ebt_entry *e, const char *base, char __user *ubase) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | { |
| 1343 | int ret; |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 1344 | char __user *hlp; |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1345 | const struct ebt_entry_target *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
Al Viro | 40642f9 | 2006-11-30 19:24:12 -0800 | [diff] [blame] | 1347 | if (e->bitmask == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | return 0; |
| 1349 | |
Al Viro | 1e419cd | 2006-11-30 19:28:48 -0800 | [diff] [blame] | 1350 | hlp = ubase + (((char *)e + e->target_offset) - base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | t = (struct ebt_entry_target *)(((char *)e) + e->target_offset); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | ret = EBT_MATCH_ITERATE(e, ebt_make_matchname, base, ubase); |
| 1354 | if (ret != 0) |
| 1355 | return ret; |
| 1356 | ret = EBT_WATCHER_ITERATE(e, ebt_make_watchername, base, ubase); |
| 1357 | if (ret != 0) |
| 1358 | return ret; |
| 1359 | if (copy_to_user(hlp, t->u.target->name, EBT_FUNCTION_MAXNAMELEN)) |
| 1360 | return -EFAULT; |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
Florian Westphal | 837395a | 2010-02-07 02:11:34 +0100 | [diff] [blame] | 1364 | static int copy_counters_to_user(struct ebt_table *t, |
| 1365 | const struct ebt_counter *oldcounters, |
| 1366 | void __user *user, unsigned int num_counters, |
| 1367 | unsigned int nentries) |
| 1368 | { |
| 1369 | struct ebt_counter *counterstmp; |
| 1370 | int ret = 0; |
| 1371 | |
| 1372 | /* userspace might not need the counters */ |
| 1373 | if (num_counters == 0) |
| 1374 | return 0; |
| 1375 | |
| 1376 | if (num_counters != nentries) { |
| 1377 | BUGPRINT("Num_counters wrong\n"); |
| 1378 | return -EINVAL; |
| 1379 | } |
| 1380 | |
| 1381 | counterstmp = vmalloc(nentries * sizeof(*counterstmp)); |
| 1382 | if (!counterstmp) |
| 1383 | return -ENOMEM; |
| 1384 | |
| 1385 | write_lock_bh(&t->lock); |
| 1386 | get_counters(oldcounters, counterstmp, nentries); |
| 1387 | write_unlock_bh(&t->lock); |
| 1388 | |
| 1389 | if (copy_to_user(user, counterstmp, |
| 1390 | nentries * sizeof(struct ebt_counter))) |
| 1391 | ret = -EFAULT; |
| 1392 | vfree(counterstmp); |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1396 | /* called with ebt_mutex locked */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | static int copy_everything_to_user(struct ebt_table *t, void __user *user, |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1398 | const int *len, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | { |
| 1400 | struct ebt_replace tmp; |
Jan Engelhardt | d5d1baa | 2009-06-26 07:51:59 +0200 | [diff] [blame] | 1401 | const struct ebt_counter *oldcounters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | unsigned int entries_size, nentries; |
Florian Westphal | 837395a | 2010-02-07 02:11:34 +0100 | [diff] [blame] | 1403 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | char *entries; |
| 1405 | |
| 1406 | if (cmd == EBT_SO_GET_ENTRIES) { |
| 1407 | entries_size = t->private->entries_size; |
| 1408 | nentries = t->private->nentries; |
| 1409 | entries = t->private->entries; |
| 1410 | oldcounters = t->private->counters; |
| 1411 | } else { |
| 1412 | entries_size = t->table->entries_size; |
| 1413 | nentries = t->table->nentries; |
| 1414 | entries = t->table->entries; |
| 1415 | oldcounters = t->table->counters; |
| 1416 | } |
| 1417 | |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 1418 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
| 1421 | if (*len != sizeof(struct ebt_replace) + entries_size + |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 1422 | (tmp.num_counters? nentries * sizeof(struct ebt_counter): 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | if (tmp.nentries != nentries) { |
| 1426 | BUGPRINT("Nentries wrong\n"); |
| 1427 | return -EINVAL; |
| 1428 | } |
| 1429 | |
| 1430 | if (tmp.entries_size != entries_size) { |
| 1431 | BUGPRINT("Wrong size\n"); |
| 1432 | return -EINVAL; |
| 1433 | } |
| 1434 | |
Florian Westphal | 837395a | 2010-02-07 02:11:34 +0100 | [diff] [blame] | 1435 | ret = copy_counters_to_user(t, oldcounters, tmp.counters, |
| 1436 | tmp.num_counters, nentries); |
| 1437 | if (ret) |
| 1438 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | if (copy_to_user(tmp.entries, entries, entries_size)) { |
| 1441 | BUGPRINT("Couldn't copy entries to userspace\n"); |
| 1442 | return -EFAULT; |
| 1443 | } |
| 1444 | /* set the match/watcher/target names right */ |
| 1445 | return EBT_ENTRY_ITERATE(entries, entries_size, |
| 1446 | ebt_make_names, entries, tmp.entries); |
| 1447 | } |
| 1448 | |
| 1449 | static int do_ebt_set_ctl(struct sock *sk, |
| 1450 | int cmd, void __user *user, unsigned int len) |
| 1451 | { |
| 1452 | int ret; |
| 1453 | |
Florian Westphal | dce766a | 2010-01-08 17:31:24 +0100 | [diff] [blame] | 1454 | if (!capable(CAP_NET_ADMIN)) |
| 1455 | return -EPERM; |
| 1456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | switch(cmd) { |
| 1458 | case EBT_SO_SET_ENTRIES: |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 1459 | ret = do_replace(sock_net(sk), user, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | break; |
| 1461 | case EBT_SO_SET_COUNTERS: |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 1462 | ret = update_counters(sock_net(sk), user, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | default: |
| 1465 | ret = -EINVAL; |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 1466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | return ret; |
| 1468 | } |
| 1469 | |
| 1470 | static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) |
| 1471 | { |
| 1472 | int ret; |
| 1473 | struct ebt_replace tmp; |
| 1474 | struct ebt_table *t; |
| 1475 | |
Florian Westphal | dce766a | 2010-01-08 17:31:24 +0100 | [diff] [blame] | 1476 | if (!capable(CAP_NET_ADMIN)) |
| 1477 | return -EPERM; |
| 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
| 1480 | return -EFAULT; |
| 1481 | |
Alexey Dobriyan | 511061e | 2008-11-04 14:22:55 +0100 | [diff] [blame] | 1482 | t = find_table_lock(sock_net(sk), tmp.name, &ret, &ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (!t) |
| 1484 | return ret; |
| 1485 | |
| 1486 | switch(cmd) { |
| 1487 | case EBT_SO_GET_INFO: |
| 1488 | case EBT_SO_GET_INIT_INFO: |
| 1489 | if (*len != sizeof(struct ebt_replace)){ |
| 1490 | ret = -EINVAL; |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1491 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | break; |
| 1493 | } |
| 1494 | if (cmd == EBT_SO_GET_INFO) { |
| 1495 | tmp.nentries = t->private->nentries; |
| 1496 | tmp.entries_size = t->private->entries_size; |
| 1497 | tmp.valid_hooks = t->valid_hooks; |
| 1498 | } else { |
| 1499 | tmp.nentries = t->table->nentries; |
| 1500 | tmp.entries_size = t->table->entries_size; |
| 1501 | tmp.valid_hooks = t->table->valid_hooks; |
| 1502 | } |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1503 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | if (copy_to_user(user, &tmp, *len) != 0){ |
| 1505 | BUGPRINT("c2u Didn't work\n"); |
| 1506 | ret = -EFAULT; |
| 1507 | break; |
| 1508 | } |
| 1509 | ret = 0; |
| 1510 | break; |
| 1511 | |
| 1512 | case EBT_SO_GET_ENTRIES: |
| 1513 | case EBT_SO_GET_INIT_ENTRIES: |
| 1514 | ret = copy_everything_to_user(t, user, len, cmd); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1515 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | break; |
| 1517 | |
| 1518 | default: |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1519 | mutex_unlock(&ebt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | ret = -EINVAL; |
| 1521 | } |
| 1522 | |
| 1523 | return ret; |
| 1524 | } |
| 1525 | |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 1526 | #ifdef CONFIG_COMPAT |
| 1527 | /* 32 bit-userspace compatibility definitions. */ |
| 1528 | struct compat_ebt_replace { |
| 1529 | char name[EBT_TABLE_MAXNAMELEN]; |
| 1530 | compat_uint_t valid_hooks; |
| 1531 | compat_uint_t nentries; |
| 1532 | compat_uint_t entries_size; |
| 1533 | /* start of the chains */ |
| 1534 | compat_uptr_t hook_entry[NF_BR_NUMHOOKS]; |
| 1535 | /* nr of counters userspace expects back */ |
| 1536 | compat_uint_t num_counters; |
| 1537 | /* where the kernel will put the old counters. */ |
| 1538 | compat_uptr_t counters; |
| 1539 | compat_uptr_t entries; |
| 1540 | }; |
| 1541 | |
| 1542 | /* struct ebt_entry_match, _target and _watcher have same layout */ |
| 1543 | struct compat_ebt_entry_mwt { |
| 1544 | union { |
| 1545 | char name[EBT_FUNCTION_MAXNAMELEN]; |
| 1546 | compat_uptr_t ptr; |
| 1547 | } u; |
| 1548 | compat_uint_t match_size; |
| 1549 | compat_uint_t data[0]; |
| 1550 | }; |
| 1551 | |
| 1552 | /* account for possible padding between match_size and ->data */ |
| 1553 | static int ebt_compat_entry_padsize(void) |
| 1554 | { |
| 1555 | BUILD_BUG_ON(XT_ALIGN(sizeof(struct ebt_entry_match)) < |
| 1556 | COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt))); |
| 1557 | return (int) XT_ALIGN(sizeof(struct ebt_entry_match)) - |
| 1558 | COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt)); |
| 1559 | } |
| 1560 | |
| 1561 | static int ebt_compat_match_offset(const struct xt_match *match, |
| 1562 | unsigned int userlen) |
| 1563 | { |
| 1564 | /* |
| 1565 | * ebt_among needs special handling. The kernel .matchsize is |
| 1566 | * set to -1 at registration time; at runtime an EBT_ALIGN()ed |
| 1567 | * value is expected. |
| 1568 | * Example: userspace sends 4500, ebt_among.c wants 4504. |
| 1569 | */ |
| 1570 | if (unlikely(match->matchsize == -1)) |
| 1571 | return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen); |
| 1572 | return xt_compat_match_offset(match); |
| 1573 | } |
| 1574 | |
| 1575 | static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr, |
| 1576 | unsigned int *size) |
| 1577 | { |
| 1578 | const struct xt_match *match = m->u.match; |
| 1579 | struct compat_ebt_entry_mwt __user *cm = *dstptr; |
| 1580 | int off = ebt_compat_match_offset(match, m->match_size); |
| 1581 | compat_uint_t msize = m->match_size - off; |
| 1582 | |
| 1583 | BUG_ON(off >= m->match_size); |
| 1584 | |
| 1585 | if (copy_to_user(cm->u.name, match->name, |
| 1586 | strlen(match->name) + 1) || put_user(msize, &cm->match_size)) |
| 1587 | return -EFAULT; |
| 1588 | |
| 1589 | if (match->compat_to_user) { |
| 1590 | if (match->compat_to_user(cm->data, m->data)) |
| 1591 | return -EFAULT; |
| 1592 | } else if (copy_to_user(cm->data, m->data, msize)) |
| 1593 | return -EFAULT; |
| 1594 | |
| 1595 | *size -= ebt_compat_entry_padsize() + off; |
| 1596 | *dstptr = cm->data; |
| 1597 | *dstptr += msize; |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static int compat_target_to_user(struct ebt_entry_target *t, |
| 1602 | void __user **dstptr, |
| 1603 | unsigned int *size) |
| 1604 | { |
| 1605 | const struct xt_target *target = t->u.target; |
| 1606 | struct compat_ebt_entry_mwt __user *cm = *dstptr; |
| 1607 | int off = xt_compat_target_offset(target); |
| 1608 | compat_uint_t tsize = t->target_size - off; |
| 1609 | |
| 1610 | BUG_ON(off >= t->target_size); |
| 1611 | |
| 1612 | if (copy_to_user(cm->u.name, target->name, |
| 1613 | strlen(target->name) + 1) || put_user(tsize, &cm->match_size)) |
| 1614 | return -EFAULT; |
| 1615 | |
| 1616 | if (target->compat_to_user) { |
| 1617 | if (target->compat_to_user(cm->data, t->data)) |
| 1618 | return -EFAULT; |
| 1619 | } else if (copy_to_user(cm->data, t->data, tsize)) |
| 1620 | return -EFAULT; |
| 1621 | |
| 1622 | *size -= ebt_compat_entry_padsize() + off; |
| 1623 | *dstptr = cm->data; |
| 1624 | *dstptr += tsize; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static int compat_watcher_to_user(struct ebt_entry_watcher *w, |
| 1629 | void __user **dstptr, |
| 1630 | unsigned int *size) |
| 1631 | { |
| 1632 | return compat_target_to_user((struct ebt_entry_target *)w, |
| 1633 | dstptr, size); |
| 1634 | } |
| 1635 | |
| 1636 | static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr, |
| 1637 | unsigned int *size) |
| 1638 | { |
| 1639 | struct ebt_entry_target *t; |
| 1640 | struct ebt_entry __user *ce; |
| 1641 | u32 watchers_offset, target_offset, next_offset; |
| 1642 | compat_uint_t origsize; |
| 1643 | int ret; |
| 1644 | |
| 1645 | if (e->bitmask == 0) { |
| 1646 | if (*size < sizeof(struct ebt_entries)) |
| 1647 | return -EINVAL; |
| 1648 | if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries))) |
| 1649 | return -EFAULT; |
| 1650 | |
| 1651 | *dstptr += sizeof(struct ebt_entries); |
| 1652 | *size -= sizeof(struct ebt_entries); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | if (*size < sizeof(*ce)) |
| 1657 | return -EINVAL; |
| 1658 | |
| 1659 | ce = (struct ebt_entry __user *)*dstptr; |
| 1660 | if (copy_to_user(ce, e, sizeof(*ce))) |
| 1661 | return -EFAULT; |
| 1662 | |
| 1663 | origsize = *size; |
| 1664 | *dstptr += sizeof(*ce); |
| 1665 | |
| 1666 | ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size); |
| 1667 | if (ret) |
| 1668 | return ret; |
| 1669 | watchers_offset = e->watchers_offset - (origsize - *size); |
| 1670 | |
| 1671 | ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size); |
| 1672 | if (ret) |
| 1673 | return ret; |
| 1674 | target_offset = e->target_offset - (origsize - *size); |
| 1675 | |
| 1676 | t = (struct ebt_entry_target *) ((char *) e + e->target_offset); |
| 1677 | |
| 1678 | ret = compat_target_to_user(t, dstptr, size); |
| 1679 | if (ret) |
| 1680 | return ret; |
| 1681 | next_offset = e->next_offset - (origsize - *size); |
| 1682 | |
| 1683 | if (put_user(watchers_offset, &ce->watchers_offset) || |
| 1684 | put_user(target_offset, &ce->target_offset) || |
| 1685 | put_user(next_offset, &ce->next_offset)) |
| 1686 | return -EFAULT; |
| 1687 | |
| 1688 | *size -= sizeof(*ce); |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | static int compat_calc_match(struct ebt_entry_match *m, int *off) |
| 1693 | { |
| 1694 | *off += ebt_compat_match_offset(m->u.match, m->match_size); |
| 1695 | *off += ebt_compat_entry_padsize(); |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off) |
| 1700 | { |
| 1701 | *off += xt_compat_target_offset(w->u.watcher); |
| 1702 | *off += ebt_compat_entry_padsize(); |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static int compat_calc_entry(const struct ebt_entry *e, |
| 1707 | const struct ebt_table_info *info, |
| 1708 | const void *base, |
| 1709 | struct compat_ebt_replace *newinfo) |
| 1710 | { |
| 1711 | const struct ebt_entry_target *t; |
| 1712 | unsigned int entry_offset; |
| 1713 | int off, ret, i; |
| 1714 | |
| 1715 | if (e->bitmask == 0) |
| 1716 | return 0; |
| 1717 | |
| 1718 | off = 0; |
| 1719 | entry_offset = (void *)e - base; |
| 1720 | |
| 1721 | EBT_MATCH_ITERATE(e, compat_calc_match, &off); |
| 1722 | EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off); |
| 1723 | |
| 1724 | t = (const struct ebt_entry_target *) ((char *) e + e->target_offset); |
| 1725 | |
| 1726 | off += xt_compat_target_offset(t->u.target); |
| 1727 | off += ebt_compat_entry_padsize(); |
| 1728 | |
| 1729 | newinfo->entries_size -= off; |
| 1730 | |
| 1731 | ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off); |
| 1732 | if (ret) |
| 1733 | return ret; |
| 1734 | |
| 1735 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 1736 | const void *hookptr = info->hook_entry[i]; |
| 1737 | if (info->hook_entry[i] && |
| 1738 | (e < (struct ebt_entry *)(base - hookptr))) { |
| 1739 | newinfo->hook_entry[i] -= off; |
| 1740 | pr_debug("0x%08X -> 0x%08X\n", |
| 1741 | newinfo->hook_entry[i] + off, |
| 1742 | newinfo->hook_entry[i]); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | static int compat_table_info(const struct ebt_table_info *info, |
| 1751 | struct compat_ebt_replace *newinfo) |
| 1752 | { |
| 1753 | unsigned int size = info->entries_size; |
| 1754 | const void *entries = info->entries; |
| 1755 | |
| 1756 | newinfo->entries_size = size; |
| 1757 | |
| 1758 | return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info, |
| 1759 | entries, newinfo); |
| 1760 | } |
| 1761 | |
| 1762 | static int compat_copy_everything_to_user(struct ebt_table *t, |
| 1763 | void __user *user, int *len, int cmd) |
| 1764 | { |
| 1765 | struct compat_ebt_replace repl, tmp; |
| 1766 | struct ebt_counter *oldcounters; |
| 1767 | struct ebt_table_info tinfo; |
| 1768 | int ret; |
| 1769 | void __user *pos; |
| 1770 | |
| 1771 | memset(&tinfo, 0, sizeof(tinfo)); |
| 1772 | |
| 1773 | if (cmd == EBT_SO_GET_ENTRIES) { |
| 1774 | tinfo.entries_size = t->private->entries_size; |
| 1775 | tinfo.nentries = t->private->nentries; |
| 1776 | tinfo.entries = t->private->entries; |
| 1777 | oldcounters = t->private->counters; |
| 1778 | } else { |
| 1779 | tinfo.entries_size = t->table->entries_size; |
| 1780 | tinfo.nentries = t->table->nentries; |
| 1781 | tinfo.entries = t->table->entries; |
| 1782 | oldcounters = t->table->counters; |
| 1783 | } |
| 1784 | |
| 1785 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
| 1786 | return -EFAULT; |
| 1787 | |
| 1788 | if (tmp.nentries != tinfo.nentries || |
| 1789 | (tmp.num_counters && tmp.num_counters != tinfo.nentries)) |
| 1790 | return -EINVAL; |
| 1791 | |
| 1792 | memcpy(&repl, &tmp, sizeof(repl)); |
| 1793 | if (cmd == EBT_SO_GET_ENTRIES) |
| 1794 | ret = compat_table_info(t->private, &repl); |
| 1795 | else |
| 1796 | ret = compat_table_info(&tinfo, &repl); |
| 1797 | if (ret) |
| 1798 | return ret; |
| 1799 | |
| 1800 | if (*len != sizeof(tmp) + repl.entries_size + |
| 1801 | (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) { |
| 1802 | pr_err("wrong size: *len %d, entries_size %u, replsz %d\n", |
| 1803 | *len, tinfo.entries_size, repl.entries_size); |
| 1804 | return -EINVAL; |
| 1805 | } |
| 1806 | |
| 1807 | /* userspace might not need the counters */ |
| 1808 | ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters), |
| 1809 | tmp.num_counters, tinfo.nentries); |
| 1810 | if (ret) |
| 1811 | return ret; |
| 1812 | |
| 1813 | pos = compat_ptr(tmp.entries); |
| 1814 | return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size, |
| 1815 | compat_copy_entry_to_user, &pos, &tmp.entries_size); |
| 1816 | } |
| 1817 | |
| 1818 | struct ebt_entries_buf_state { |
| 1819 | char *buf_kern_start; /* kernel buffer to copy (translated) data to */ |
| 1820 | u32 buf_kern_len; /* total size of kernel buffer */ |
| 1821 | u32 buf_kern_offset; /* amount of data copied so far */ |
| 1822 | u32 buf_user_offset; /* read position in userspace buffer */ |
| 1823 | }; |
| 1824 | |
| 1825 | static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz) |
| 1826 | { |
| 1827 | state->buf_kern_offset += sz; |
| 1828 | return state->buf_kern_offset >= sz ? 0 : -EINVAL; |
| 1829 | } |
| 1830 | |
| 1831 | static int ebt_buf_add(struct ebt_entries_buf_state *state, |
| 1832 | void *data, unsigned int sz) |
| 1833 | { |
| 1834 | if (state->buf_kern_start == NULL) |
| 1835 | goto count_only; |
| 1836 | |
| 1837 | BUG_ON(state->buf_kern_offset + sz > state->buf_kern_len); |
| 1838 | |
| 1839 | memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz); |
| 1840 | |
| 1841 | count_only: |
| 1842 | state->buf_user_offset += sz; |
| 1843 | return ebt_buf_count(state, sz); |
| 1844 | } |
| 1845 | |
| 1846 | static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz) |
| 1847 | { |
| 1848 | char *b = state->buf_kern_start; |
| 1849 | |
| 1850 | BUG_ON(b && state->buf_kern_offset > state->buf_kern_len); |
| 1851 | |
| 1852 | if (b != NULL && sz > 0) |
| 1853 | memset(b + state->buf_kern_offset, 0, sz); |
| 1854 | /* do not adjust ->buf_user_offset here, we added kernel-side padding */ |
| 1855 | return ebt_buf_count(state, sz); |
| 1856 | } |
| 1857 | |
| 1858 | enum compat_mwt { |
| 1859 | EBT_COMPAT_MATCH, |
| 1860 | EBT_COMPAT_WATCHER, |
| 1861 | EBT_COMPAT_TARGET, |
| 1862 | }; |
| 1863 | |
| 1864 | static int compat_mtw_from_user(struct compat_ebt_entry_mwt *mwt, |
| 1865 | enum compat_mwt compat_mwt, |
| 1866 | struct ebt_entries_buf_state *state, |
| 1867 | const unsigned char *base) |
| 1868 | { |
| 1869 | char name[EBT_FUNCTION_MAXNAMELEN]; |
| 1870 | struct xt_match *match; |
| 1871 | struct xt_target *wt; |
| 1872 | void *dst = NULL; |
| 1873 | int off, pad = 0, ret = 0; |
| 1874 | unsigned int size_kern, entry_offset, match_size = mwt->match_size; |
| 1875 | |
| 1876 | strlcpy(name, mwt->u.name, sizeof(name)); |
| 1877 | |
| 1878 | if (state->buf_kern_start) |
| 1879 | dst = state->buf_kern_start + state->buf_kern_offset; |
| 1880 | |
| 1881 | entry_offset = (unsigned char *) mwt - base; |
| 1882 | switch (compat_mwt) { |
| 1883 | case EBT_COMPAT_MATCH: |
| 1884 | match = try_then_request_module(xt_find_match(NFPROTO_BRIDGE, |
| 1885 | name, 0), "ebt_%s", name); |
| 1886 | if (match == NULL) |
| 1887 | return -ENOENT; |
| 1888 | if (IS_ERR(match)) |
| 1889 | return PTR_ERR(match); |
| 1890 | |
| 1891 | off = ebt_compat_match_offset(match, match_size); |
| 1892 | if (dst) { |
| 1893 | if (match->compat_from_user) |
| 1894 | match->compat_from_user(dst, mwt->data); |
| 1895 | else |
| 1896 | memcpy(dst, mwt->data, match_size); |
| 1897 | } |
| 1898 | |
| 1899 | size_kern = match->matchsize; |
| 1900 | if (unlikely(size_kern == -1)) |
| 1901 | size_kern = match_size; |
| 1902 | module_put(match->me); |
| 1903 | break; |
| 1904 | case EBT_COMPAT_WATCHER: /* fallthrough */ |
| 1905 | case EBT_COMPAT_TARGET: |
| 1906 | wt = try_then_request_module(xt_find_target(NFPROTO_BRIDGE, |
| 1907 | name, 0), "ebt_%s", name); |
| 1908 | if (wt == NULL) |
| 1909 | return -ENOENT; |
| 1910 | if (IS_ERR(wt)) |
| 1911 | return PTR_ERR(wt); |
| 1912 | off = xt_compat_target_offset(wt); |
| 1913 | |
| 1914 | if (dst) { |
| 1915 | if (wt->compat_from_user) |
| 1916 | wt->compat_from_user(dst, mwt->data); |
| 1917 | else |
| 1918 | memcpy(dst, mwt->data, match_size); |
| 1919 | } |
| 1920 | |
| 1921 | size_kern = wt->targetsize; |
| 1922 | module_put(wt->me); |
| 1923 | break; |
| 1924 | } |
| 1925 | |
| 1926 | if (!dst) { |
| 1927 | ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, |
| 1928 | off + ebt_compat_entry_padsize()); |
| 1929 | if (ret < 0) |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
| 1933 | state->buf_kern_offset += match_size + off; |
| 1934 | state->buf_user_offset += match_size; |
| 1935 | pad = XT_ALIGN(size_kern) - size_kern; |
| 1936 | |
| 1937 | if (pad > 0 && dst) { |
| 1938 | BUG_ON(state->buf_kern_len <= pad); |
| 1939 | BUG_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad); |
| 1940 | memset(dst + size_kern, 0, pad); |
| 1941 | } |
| 1942 | return off + match_size; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * return size of all matches, watchers or target, including necessary |
| 1947 | * alignment and padding. |
| 1948 | */ |
| 1949 | static int ebt_size_mwt(struct compat_ebt_entry_mwt *match32, |
| 1950 | unsigned int size_left, enum compat_mwt type, |
| 1951 | struct ebt_entries_buf_state *state, const void *base) |
| 1952 | { |
| 1953 | int growth = 0; |
| 1954 | char *buf; |
| 1955 | |
| 1956 | if (size_left == 0) |
| 1957 | return 0; |
| 1958 | |
| 1959 | buf = (char *) match32; |
| 1960 | |
| 1961 | while (size_left >= sizeof(*match32)) { |
| 1962 | struct ebt_entry_match *match_kern; |
| 1963 | int ret; |
| 1964 | |
| 1965 | match_kern = (struct ebt_entry_match *) state->buf_kern_start; |
| 1966 | if (match_kern) { |
| 1967 | char *tmp; |
| 1968 | tmp = state->buf_kern_start + state->buf_kern_offset; |
| 1969 | match_kern = (struct ebt_entry_match *) tmp; |
| 1970 | } |
| 1971 | ret = ebt_buf_add(state, buf, sizeof(*match32)); |
| 1972 | if (ret < 0) |
| 1973 | return ret; |
| 1974 | size_left -= sizeof(*match32); |
| 1975 | |
| 1976 | /* add padding before match->data (if any) */ |
| 1977 | ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize()); |
| 1978 | if (ret < 0) |
| 1979 | return ret; |
| 1980 | |
| 1981 | if (match32->match_size > size_left) |
| 1982 | return -EINVAL; |
| 1983 | |
| 1984 | size_left -= match32->match_size; |
| 1985 | |
| 1986 | ret = compat_mtw_from_user(match32, type, state, base); |
| 1987 | if (ret < 0) |
| 1988 | return ret; |
| 1989 | |
| 1990 | BUG_ON(ret < match32->match_size); |
| 1991 | growth += ret - match32->match_size; |
| 1992 | growth += ebt_compat_entry_padsize(); |
| 1993 | |
| 1994 | buf += sizeof(*match32); |
| 1995 | buf += match32->match_size; |
| 1996 | |
| 1997 | if (match_kern) |
| 1998 | match_kern->match_size = ret; |
| 1999 | |
| 2000 | WARN_ON(type == EBT_COMPAT_TARGET && size_left); |
| 2001 | match32 = (struct compat_ebt_entry_mwt *) buf; |
| 2002 | } |
| 2003 | |
| 2004 | return growth; |
| 2005 | } |
| 2006 | |
| 2007 | #define EBT_COMPAT_WATCHER_ITERATE(e, fn, args...) \ |
| 2008 | ({ \ |
| 2009 | unsigned int __i; \ |
| 2010 | int __ret = 0; \ |
| 2011 | struct compat_ebt_entry_mwt *__watcher; \ |
| 2012 | \ |
| 2013 | for (__i = e->watchers_offset; \ |
| 2014 | __i < (e)->target_offset; \ |
| 2015 | __i += __watcher->watcher_size + \ |
| 2016 | sizeof(struct compat_ebt_entry_mwt)) { \ |
| 2017 | __watcher = (void *)(e) + __i; \ |
| 2018 | __ret = fn(__watcher , ## args); \ |
| 2019 | if (__ret != 0) \ |
| 2020 | break; \ |
| 2021 | } \ |
| 2022 | if (__ret == 0) { \ |
| 2023 | if (__i != (e)->target_offset) \ |
| 2024 | __ret = -EINVAL; \ |
| 2025 | } \ |
| 2026 | __ret; \ |
| 2027 | }) |
| 2028 | |
| 2029 | #define EBT_COMPAT_MATCH_ITERATE(e, fn, args...) \ |
| 2030 | ({ \ |
| 2031 | unsigned int __i; \ |
| 2032 | int __ret = 0; \ |
| 2033 | struct compat_ebt_entry_mwt *__match; \ |
| 2034 | \ |
| 2035 | for (__i = sizeof(struct ebt_entry); \ |
| 2036 | __i < (e)->watchers_offset; \ |
| 2037 | __i += __match->match_size + \ |
| 2038 | sizeof(struct compat_ebt_entry_mwt)) { \ |
| 2039 | __match = (void *)(e) + __i; \ |
| 2040 | __ret = fn(__match , ## args); \ |
| 2041 | if (__ret != 0) \ |
| 2042 | break; \ |
| 2043 | } \ |
| 2044 | if (__ret == 0) { \ |
| 2045 | if (__i != (e)->watchers_offset) \ |
| 2046 | __ret = -EINVAL; \ |
| 2047 | } \ |
| 2048 | __ret; \ |
| 2049 | }) |
| 2050 | |
| 2051 | /* called for all ebt_entry structures. */ |
| 2052 | static int size_entry_mwt(struct ebt_entry *entry, const unsigned char *base, |
| 2053 | unsigned int *total, |
| 2054 | struct ebt_entries_buf_state *state) |
| 2055 | { |
| 2056 | unsigned int i, j, startoff, new_offset = 0; |
| 2057 | /* stores match/watchers/targets & offset of next struct ebt_entry: */ |
| 2058 | unsigned int offsets[4]; |
| 2059 | unsigned int *offsets_update = NULL; |
| 2060 | int ret; |
| 2061 | char *buf_start; |
| 2062 | |
| 2063 | if (*total < sizeof(struct ebt_entries)) |
| 2064 | return -EINVAL; |
| 2065 | |
| 2066 | if (!entry->bitmask) { |
| 2067 | *total -= sizeof(struct ebt_entries); |
| 2068 | return ebt_buf_add(state, entry, sizeof(struct ebt_entries)); |
| 2069 | } |
| 2070 | if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry)) |
| 2071 | return -EINVAL; |
| 2072 | |
| 2073 | startoff = state->buf_user_offset; |
| 2074 | /* pull in most part of ebt_entry, it does not need to be changed. */ |
| 2075 | ret = ebt_buf_add(state, entry, |
| 2076 | offsetof(struct ebt_entry, watchers_offset)); |
| 2077 | if (ret < 0) |
| 2078 | return ret; |
| 2079 | |
| 2080 | offsets[0] = sizeof(struct ebt_entry); /* matches come first */ |
| 2081 | memcpy(&offsets[1], &entry->watchers_offset, |
| 2082 | sizeof(offsets) - sizeof(offsets[0])); |
| 2083 | |
| 2084 | if (state->buf_kern_start) { |
| 2085 | buf_start = state->buf_kern_start + state->buf_kern_offset; |
| 2086 | offsets_update = (unsigned int *) buf_start; |
| 2087 | } |
| 2088 | ret = ebt_buf_add(state, &offsets[1], |
| 2089 | sizeof(offsets) - sizeof(offsets[0])); |
| 2090 | if (ret < 0) |
| 2091 | return ret; |
| 2092 | buf_start = (char *) entry; |
| 2093 | /* |
| 2094 | * 0: matches offset, always follows ebt_entry. |
| 2095 | * 1: watchers offset, from ebt_entry structure |
| 2096 | * 2: target offset, from ebt_entry structure |
| 2097 | * 3: next ebt_entry offset, from ebt_entry structure |
| 2098 | * |
| 2099 | * offsets are relative to beginning of struct ebt_entry (i.e., 0). |
| 2100 | */ |
| 2101 | for (i = 0, j = 1 ; j < 4 ; j++, i++) { |
| 2102 | struct compat_ebt_entry_mwt *match32; |
| 2103 | unsigned int size; |
| 2104 | char *buf = buf_start; |
| 2105 | |
| 2106 | buf = buf_start + offsets[i]; |
| 2107 | if (offsets[i] > offsets[j]) |
| 2108 | return -EINVAL; |
| 2109 | |
| 2110 | match32 = (struct compat_ebt_entry_mwt *) buf; |
| 2111 | size = offsets[j] - offsets[i]; |
| 2112 | ret = ebt_size_mwt(match32, size, i, state, base); |
| 2113 | if (ret < 0) |
| 2114 | return ret; |
| 2115 | new_offset += ret; |
| 2116 | if (offsets_update && new_offset) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 2117 | pr_debug("change offset %d to %d\n", |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2118 | offsets_update[i], offsets[j] + new_offset); |
| 2119 | offsets_update[i] = offsets[j] + new_offset; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | startoff = state->buf_user_offset - startoff; |
| 2124 | |
| 2125 | BUG_ON(*total < startoff); |
| 2126 | *total -= startoff; |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * repl->entries_size is the size of the ebt_entry blob in userspace. |
| 2132 | * It might need more memory when copied to a 64 bit kernel in case |
| 2133 | * userspace is 32-bit. So, first task: find out how much memory is needed. |
| 2134 | * |
| 2135 | * Called before validation is performed. |
| 2136 | */ |
| 2137 | static int compat_copy_entries(unsigned char *data, unsigned int size_user, |
| 2138 | struct ebt_entries_buf_state *state) |
| 2139 | { |
| 2140 | unsigned int size_remaining = size_user; |
| 2141 | int ret; |
| 2142 | |
| 2143 | ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data, |
| 2144 | &size_remaining, state); |
| 2145 | if (ret < 0) |
| 2146 | return ret; |
| 2147 | |
| 2148 | WARN_ON(size_remaining); |
| 2149 | return state->buf_kern_offset; |
| 2150 | } |
| 2151 | |
| 2152 | |
| 2153 | static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl, |
| 2154 | void __user *user, unsigned int len) |
| 2155 | { |
| 2156 | struct compat_ebt_replace tmp; |
| 2157 | int i; |
| 2158 | |
| 2159 | if (len < sizeof(tmp)) |
| 2160 | return -EINVAL; |
| 2161 | |
| 2162 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
| 2163 | return -EFAULT; |
| 2164 | |
| 2165 | if (len != sizeof(tmp) + tmp.entries_size) |
| 2166 | return -EINVAL; |
| 2167 | |
| 2168 | if (tmp.entries_size == 0) |
| 2169 | return -EINVAL; |
| 2170 | |
| 2171 | if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) / |
| 2172 | NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter)) |
| 2173 | return -ENOMEM; |
| 2174 | if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter)) |
| 2175 | return -ENOMEM; |
| 2176 | |
| 2177 | memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry)); |
| 2178 | |
| 2179 | /* starting with hook_entry, 32 vs. 64 bit structures are different */ |
| 2180 | for (i = 0; i < NF_BR_NUMHOOKS; i++) |
| 2181 | repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]); |
| 2182 | |
| 2183 | repl->num_counters = tmp.num_counters; |
| 2184 | repl->counters = compat_ptr(tmp.counters); |
| 2185 | repl->entries = compat_ptr(tmp.entries); |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | static int compat_do_replace(struct net *net, void __user *user, |
| 2190 | unsigned int len) |
| 2191 | { |
| 2192 | int ret, i, countersize, size64; |
| 2193 | struct ebt_table_info *newinfo; |
| 2194 | struct ebt_replace tmp; |
| 2195 | struct ebt_entries_buf_state state; |
| 2196 | void *entries_tmp; |
| 2197 | |
| 2198 | ret = compat_copy_ebt_replace_from_user(&tmp, user, len); |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2199 | if (ret) { |
| 2200 | /* try real handler in case userland supplied needed padding */ |
| 2201 | if (ret == -EINVAL && do_replace(net, user, len) == 0) |
| 2202 | ret = 0; |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2203 | return ret; |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2204 | } |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2205 | |
| 2206 | countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids; |
| 2207 | newinfo = vmalloc(sizeof(*newinfo) + countersize); |
| 2208 | if (!newinfo) |
| 2209 | return -ENOMEM; |
| 2210 | |
| 2211 | if (countersize) |
| 2212 | memset(newinfo->counters, 0, countersize); |
| 2213 | |
| 2214 | memset(&state, 0, sizeof(state)); |
| 2215 | |
| 2216 | newinfo->entries = vmalloc(tmp.entries_size); |
| 2217 | if (!newinfo->entries) { |
| 2218 | ret = -ENOMEM; |
| 2219 | goto free_newinfo; |
| 2220 | } |
| 2221 | if (copy_from_user( |
| 2222 | newinfo->entries, tmp.entries, tmp.entries_size) != 0) { |
| 2223 | ret = -EFAULT; |
| 2224 | goto free_entries; |
| 2225 | } |
| 2226 | |
| 2227 | entries_tmp = newinfo->entries; |
| 2228 | |
| 2229 | xt_compat_lock(NFPROTO_BRIDGE); |
| 2230 | |
| 2231 | ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state); |
| 2232 | if (ret < 0) |
| 2233 | goto out_unlock; |
| 2234 | |
| 2235 | pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n", |
| 2236 | tmp.entries_size, state.buf_kern_offset, state.buf_user_offset, |
| 2237 | xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size)); |
| 2238 | |
| 2239 | size64 = ret; |
| 2240 | newinfo->entries = vmalloc(size64); |
| 2241 | if (!newinfo->entries) { |
| 2242 | vfree(entries_tmp); |
| 2243 | ret = -ENOMEM; |
| 2244 | goto out_unlock; |
| 2245 | } |
| 2246 | |
| 2247 | memset(&state, 0, sizeof(state)); |
| 2248 | state.buf_kern_start = newinfo->entries; |
| 2249 | state.buf_kern_len = size64; |
| 2250 | |
| 2251 | ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state); |
| 2252 | BUG_ON(ret < 0); /* parses same data again */ |
| 2253 | |
| 2254 | vfree(entries_tmp); |
| 2255 | tmp.entries_size = size64; |
| 2256 | |
| 2257 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 2258 | char __user *usrptr; |
| 2259 | if (tmp.hook_entry[i]) { |
| 2260 | unsigned int delta; |
| 2261 | usrptr = (char __user *) tmp.hook_entry[i]; |
| 2262 | delta = usrptr - tmp.entries; |
| 2263 | usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta); |
| 2264 | tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | xt_compat_flush_offsets(NFPROTO_BRIDGE); |
| 2269 | xt_compat_unlock(NFPROTO_BRIDGE); |
| 2270 | |
| 2271 | ret = do_replace_finish(net, &tmp, newinfo); |
| 2272 | if (ret == 0) |
| 2273 | return ret; |
| 2274 | free_entries: |
| 2275 | vfree(newinfo->entries); |
| 2276 | free_newinfo: |
| 2277 | vfree(newinfo); |
| 2278 | return ret; |
| 2279 | out_unlock: |
| 2280 | xt_compat_flush_offsets(NFPROTO_BRIDGE); |
| 2281 | xt_compat_unlock(NFPROTO_BRIDGE); |
| 2282 | goto free_entries; |
| 2283 | } |
| 2284 | |
| 2285 | static int compat_update_counters(struct net *net, void __user *user, |
| 2286 | unsigned int len) |
| 2287 | { |
| 2288 | struct compat_ebt_replace hlp; |
| 2289 | |
| 2290 | if (copy_from_user(&hlp, user, sizeof(hlp))) |
| 2291 | return -EFAULT; |
| 2292 | |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2293 | /* try real handler in case userland supplied needed padding */ |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2294 | if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter)) |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2295 | return update_counters(net, user, len); |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2296 | |
| 2297 | return do_update_counters(net, hlp.name, compat_ptr(hlp.counters), |
| 2298 | hlp.num_counters, user, len); |
| 2299 | } |
| 2300 | |
| 2301 | static int compat_do_ebt_set_ctl(struct sock *sk, |
| 2302 | int cmd, void __user *user, unsigned int len) |
| 2303 | { |
| 2304 | int ret; |
| 2305 | |
| 2306 | if (!capable(CAP_NET_ADMIN)) |
| 2307 | return -EPERM; |
| 2308 | |
| 2309 | switch (cmd) { |
| 2310 | case EBT_SO_SET_ENTRIES: |
| 2311 | ret = compat_do_replace(sock_net(sk), user, len); |
| 2312 | break; |
| 2313 | case EBT_SO_SET_COUNTERS: |
| 2314 | ret = compat_update_counters(sock_net(sk), user, len); |
| 2315 | break; |
| 2316 | default: |
| 2317 | ret = -EINVAL; |
| 2318 | } |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | static int compat_do_ebt_get_ctl(struct sock *sk, int cmd, |
| 2323 | void __user *user, int *len) |
| 2324 | { |
| 2325 | int ret; |
| 2326 | struct compat_ebt_replace tmp; |
| 2327 | struct ebt_table *t; |
| 2328 | |
| 2329 | if (!capable(CAP_NET_ADMIN)) |
| 2330 | return -EPERM; |
| 2331 | |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2332 | /* try real handler in case userland supplied needed padding */ |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2333 | if ((cmd == EBT_SO_GET_INFO || |
| 2334 | cmd == EBT_SO_GET_INIT_INFO) && *len != sizeof(tmp)) |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2335 | return do_ebt_get_ctl(sk, cmd, user, len); |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2336 | |
| 2337 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
| 2338 | return -EFAULT; |
| 2339 | |
| 2340 | t = find_table_lock(sock_net(sk), tmp.name, &ret, &ebt_mutex); |
| 2341 | if (!t) |
| 2342 | return ret; |
| 2343 | |
| 2344 | xt_compat_lock(NFPROTO_BRIDGE); |
| 2345 | switch (cmd) { |
| 2346 | case EBT_SO_GET_INFO: |
| 2347 | tmp.nentries = t->private->nentries; |
| 2348 | ret = compat_table_info(t->private, &tmp); |
| 2349 | if (ret) |
| 2350 | goto out; |
| 2351 | tmp.valid_hooks = t->valid_hooks; |
| 2352 | |
| 2353 | if (copy_to_user(user, &tmp, *len) != 0) { |
| 2354 | ret = -EFAULT; |
| 2355 | break; |
| 2356 | } |
| 2357 | ret = 0; |
| 2358 | break; |
| 2359 | case EBT_SO_GET_INIT_INFO: |
| 2360 | tmp.nentries = t->table->nentries; |
| 2361 | tmp.entries_size = t->table->entries_size; |
| 2362 | tmp.valid_hooks = t->table->valid_hooks; |
| 2363 | |
| 2364 | if (copy_to_user(user, &tmp, *len) != 0) { |
| 2365 | ret = -EFAULT; |
| 2366 | break; |
| 2367 | } |
| 2368 | ret = 0; |
| 2369 | break; |
| 2370 | case EBT_SO_GET_ENTRIES: |
| 2371 | case EBT_SO_GET_INIT_ENTRIES: |
Florian Westphal | 90b89af | 2010-02-07 03:19:12 +0100 | [diff] [blame] | 2372 | /* |
| 2373 | * try real handler first in case of userland-side padding. |
| 2374 | * in case we are dealing with an 'ordinary' 32 bit binary |
| 2375 | * without 64bit compatibility padding, this will fail right |
| 2376 | * after copy_from_user when the *len argument is validated. |
| 2377 | * |
| 2378 | * the compat_ variant needs to do one pass over the kernel |
| 2379 | * data set to adjust for size differences before it the check. |
| 2380 | */ |
| 2381 | if (copy_everything_to_user(t, user, len, cmd) == 0) |
| 2382 | ret = 0; |
| 2383 | else |
| 2384 | ret = compat_copy_everything_to_user(t, user, len, cmd); |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2385 | break; |
| 2386 | default: |
| 2387 | ret = -EINVAL; |
| 2388 | } |
| 2389 | out: |
| 2390 | xt_compat_flush_offsets(NFPROTO_BRIDGE); |
| 2391 | xt_compat_unlock(NFPROTO_BRIDGE); |
| 2392 | mutex_unlock(&ebt_mutex); |
| 2393 | return ret; |
| 2394 | } |
| 2395 | #endif |
| 2396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | static struct nf_sockopt_ops ebt_sockopts = |
Andrew Morton | 74ca4e5a | 2006-03-20 22:55:02 -0800 | [diff] [blame] | 2398 | { |
| 2399 | .pf = PF_INET, |
| 2400 | .set_optmin = EBT_BASE_CTL, |
| 2401 | .set_optmax = EBT_SO_SET_MAX + 1, |
| 2402 | .set = do_ebt_set_ctl, |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2403 | #ifdef CONFIG_COMPAT |
| 2404 | .compat_set = compat_do_ebt_set_ctl, |
| 2405 | #endif |
Andrew Morton | 74ca4e5a | 2006-03-20 22:55:02 -0800 | [diff] [blame] | 2406 | .get_optmin = EBT_BASE_CTL, |
| 2407 | .get_optmax = EBT_SO_GET_MAX + 1, |
| 2408 | .get = do_ebt_get_ctl, |
Florian Westphal | 81e675c | 2010-01-05 16:09:46 +0100 | [diff] [blame] | 2409 | #ifdef CONFIG_COMPAT |
| 2410 | .compat_get = compat_do_ebt_get_ctl, |
| 2411 | #endif |
Neil Horman | 16fcec3 | 2007-09-11 11:28:26 +0200 | [diff] [blame] | 2412 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | }; |
| 2414 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 2415 | static int __init ebtables_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | { |
| 2417 | int ret; |
| 2418 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 2419 | ret = xt_register_target(&ebt_standard_target); |
| 2420 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | return ret; |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 2422 | ret = nf_register_sockopt(&ebt_sockopts); |
| 2423 | if (ret < 0) { |
| 2424 | xt_unregister_target(&ebt_standard_target); |
| 2425 | return ret; |
| 2426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | |
Patrick McHardy | a887c1c | 2007-07-14 20:46:15 -0700 | [diff] [blame] | 2428 | printk(KERN_INFO "Ebtables v2.0 registered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | return 0; |
| 2430 | } |
| 2431 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 2432 | static void __exit ebtables_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | { |
| 2434 | nf_unregister_sockopt(&ebt_sockopts); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 2435 | xt_unregister_target(&ebt_standard_target); |
Patrick McHardy | a887c1c | 2007-07-14 20:46:15 -0700 | [diff] [blame] | 2436 | printk(KERN_INFO "Ebtables v2.0 unregistered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | EXPORT_SYMBOL(ebt_register_table); |
| 2440 | EXPORT_SYMBOL(ebt_unregister_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | EXPORT_SYMBOL(ebt_do_table); |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 2442 | module_init(ebtables_init); |
| 2443 | module_exit(ebtables_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | MODULE_LICENSE("GPL"); |