Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * x_tables core - Backend for {ip,ip6,arp}_tables |
| 3 | * |
| 4 | * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 6 | * |
| 7 | * Based on existing ip_tables code which is |
| 8 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 9 | * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 19 | #include <linux/socket.h> |
| 20 | #include <linux/net.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/vmalloc.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 26 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Thomas Graf | fbabf31 | 2011-01-16 18:12:59 +0100 | [diff] [blame] | 28 | #include <linux/audit.h> |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 29 | #include <linux/user_namespace.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 30 | #include <net/net_namespace.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | |
| 32 | #include <linux/netfilter/x_tables.h> |
| 33 | #include <linux/netfilter_arp.h> |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 34 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 35 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 36 | #include <linux/netfilter_arp/arp_tables.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 37 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 38 | MODULE_LICENSE("GPL"); |
| 39 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 40 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | |
| 42 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) |
| 43 | |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 44 | struct compat_delta { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 45 | unsigned int offset; /* offset in kernel */ |
| 46 | int delta; /* delta in 32bit user land */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 49 | struct xt_af { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 50 | struct mutex mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 51 | struct list_head match; |
| 52 | struct list_head target; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 53 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 54 | struct mutex compat_mutex; |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 55 | struct compat_delta *compat_tab; |
| 56 | unsigned int number; /* number of slots in compat_tab[] */ |
| 57 | unsigned int cur; /* number of used slots in compat_tab[] */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 58 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static struct xt_af *xt; |
| 62 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 63 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
| 64 | [NFPROTO_UNSPEC] = "x", |
| 65 | [NFPROTO_IPV4] = "ip", |
| 66 | [NFPROTO_ARP] = "arp", |
| 67 | [NFPROTO_BRIDGE] = "eb", |
| 68 | [NFPROTO_IPV6] = "ip6", |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | /* Registration hooks for targets. */ |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 72 | int xt_register_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 73 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 74 | u_int8_t af = target->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 76 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 77 | list_add(&target->list, &xt[af].target); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 78 | mutex_unlock(&xt[af].mutex); |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 79 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | } |
| 81 | EXPORT_SYMBOL(xt_register_target); |
| 82 | |
| 83 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 84 | xt_unregister_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 85 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 86 | u_int8_t af = target->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 87 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 88 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 89 | list_del(&target->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 90 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 91 | } |
| 92 | EXPORT_SYMBOL(xt_unregister_target); |
| 93 | |
| 94 | int |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 95 | xt_register_targets(struct xt_target *target, unsigned int n) |
| 96 | { |
| 97 | unsigned int i; |
| 98 | int err = 0; |
| 99 | |
| 100 | for (i = 0; i < n; i++) { |
| 101 | err = xt_register_target(&target[i]); |
| 102 | if (err) |
| 103 | goto err; |
| 104 | } |
| 105 | return err; |
| 106 | |
| 107 | err: |
| 108 | if (i > 0) |
| 109 | xt_unregister_targets(target, i); |
| 110 | return err; |
| 111 | } |
| 112 | EXPORT_SYMBOL(xt_register_targets); |
| 113 | |
| 114 | void |
| 115 | xt_unregister_targets(struct xt_target *target, unsigned int n) |
| 116 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 117 | while (n-- > 0) |
| 118 | xt_unregister_target(&target[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL(xt_unregister_targets); |
| 121 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 122 | int xt_register_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 123 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 124 | u_int8_t af = match->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 126 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | list_add(&match->list, &xt[af].match); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 128 | mutex_unlock(&xt[af].mutex); |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 129 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL(xt_register_match); |
| 132 | |
| 133 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 134 | xt_unregister_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 136 | u_int8_t af = match->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 137 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 138 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 139 | list_del(&match->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 140 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 141 | } |
| 142 | EXPORT_SYMBOL(xt_unregister_match); |
| 143 | |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 144 | int |
| 145 | xt_register_matches(struct xt_match *match, unsigned int n) |
| 146 | { |
| 147 | unsigned int i; |
| 148 | int err = 0; |
| 149 | |
| 150 | for (i = 0; i < n; i++) { |
| 151 | err = xt_register_match(&match[i]); |
| 152 | if (err) |
| 153 | goto err; |
| 154 | } |
| 155 | return err; |
| 156 | |
| 157 | err: |
| 158 | if (i > 0) |
| 159 | xt_unregister_matches(match, i); |
| 160 | return err; |
| 161 | } |
| 162 | EXPORT_SYMBOL(xt_register_matches); |
| 163 | |
| 164 | void |
| 165 | xt_unregister_matches(struct xt_match *match, unsigned int n) |
| 166 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 167 | while (n-- > 0) |
| 168 | xt_unregister_match(&match[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL(xt_unregister_matches); |
| 171 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * These are weird, but module loading must not be done with mutex |
| 175 | * held (since they will register), and we have to have a single |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 176 | * function to use. |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 177 | */ |
| 178 | |
| 179 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 180 | struct xt_match *xt_find_match(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 181 | { |
| 182 | struct xt_match *m; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 183 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 184 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 185 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 186 | list_for_each_entry(m, &xt[af].match, list) { |
| 187 | if (strcmp(m->name, name) == 0) { |
| 188 | if (m->revision == revision) { |
| 189 | if (try_module_get(m->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 190 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 191 | return m; |
| 192 | } |
| 193 | } else |
| 194 | err = -EPROTOTYPE; /* Found something. */ |
| 195 | } |
| 196 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 197 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 198 | |
| 199 | if (af != NFPROTO_UNSPEC) |
| 200 | /* Try searching again in the family-independent list */ |
| 201 | return xt_find_match(NFPROTO_UNSPEC, name, revision); |
| 202 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 203 | return ERR_PTR(err); |
| 204 | } |
| 205 | EXPORT_SYMBOL(xt_find_match); |
| 206 | |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 207 | struct xt_match * |
| 208 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) |
| 209 | { |
| 210 | struct xt_match *match; |
| 211 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 212 | match = xt_find_match(nfproto, name, revision); |
| 213 | if (IS_ERR(match)) { |
| 214 | request_module("%st_%s", xt_prefix[nfproto], name); |
| 215 | match = xt_find_match(nfproto, name, revision); |
| 216 | } |
| 217 | |
| 218 | return match; |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 219 | } |
| 220 | EXPORT_SYMBOL_GPL(xt_request_find_match); |
| 221 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 222 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 223 | struct xt_target *xt_find_target(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 224 | { |
| 225 | struct xt_target *t; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 226 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 227 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 228 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 229 | list_for_each_entry(t, &xt[af].target, list) { |
| 230 | if (strcmp(t->name, name) == 0) { |
| 231 | if (t->revision == revision) { |
| 232 | if (try_module_get(t->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 233 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 234 | return t; |
| 235 | } |
| 236 | } else |
| 237 | err = -EPROTOTYPE; /* Found something. */ |
| 238 | } |
| 239 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 240 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 241 | |
| 242 | if (af != NFPROTO_UNSPEC) |
| 243 | /* Try searching again in the family-independent list */ |
| 244 | return xt_find_target(NFPROTO_UNSPEC, name, revision); |
| 245 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 246 | return ERR_PTR(err); |
| 247 | } |
| 248 | EXPORT_SYMBOL(xt_find_target); |
| 249 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 250 | struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 251 | { |
| 252 | struct xt_target *target; |
| 253 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 254 | target = xt_find_target(af, name, revision); |
| 255 | if (IS_ERR(target)) { |
| 256 | request_module("%st_%s", xt_prefix[af], name); |
| 257 | target = xt_find_target(af, name, revision); |
| 258 | } |
| 259 | |
| 260 | return target; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 261 | } |
| 262 | EXPORT_SYMBOL_GPL(xt_request_find_target); |
| 263 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 264 | static int match_revfn(u8 af, const char *name, u8 revision, int *bestp) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 265 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 266 | const struct xt_match *m; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 267 | int have_rev = 0; |
| 268 | |
| 269 | list_for_each_entry(m, &xt[af].match, list) { |
| 270 | if (strcmp(m->name, name) == 0) { |
| 271 | if (m->revision > *bestp) |
| 272 | *bestp = m->revision; |
| 273 | if (m->revision == revision) |
| 274 | have_rev = 1; |
| 275 | } |
| 276 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 277 | |
| 278 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 279 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 280 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 281 | return have_rev; |
| 282 | } |
| 283 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 284 | static int target_revfn(u8 af, const char *name, u8 revision, int *bestp) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 285 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 286 | const struct xt_target *t; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 287 | int have_rev = 0; |
| 288 | |
| 289 | list_for_each_entry(t, &xt[af].target, list) { |
| 290 | if (strcmp(t->name, name) == 0) { |
| 291 | if (t->revision > *bestp) |
| 292 | *bestp = t->revision; |
| 293 | if (t->revision == revision) |
| 294 | have_rev = 1; |
| 295 | } |
| 296 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 297 | |
| 298 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 299 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 300 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 301 | return have_rev; |
| 302 | } |
| 303 | |
| 304 | /* Returns true or false (if no such extension at all) */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 305 | int xt_find_revision(u8 af, const char *name, u8 revision, int target, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 306 | int *err) |
| 307 | { |
| 308 | int have_rev, best = -1; |
| 309 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 310 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 311 | if (target == 1) |
| 312 | have_rev = target_revfn(af, name, revision, &best); |
| 313 | else |
| 314 | have_rev = match_revfn(af, name, revision, &best); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 315 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 316 | |
| 317 | /* Nothing at all? Return 0 to try loading module. */ |
| 318 | if (best == -1) { |
| 319 | *err = -ENOENT; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | *err = best; |
| 324 | if (!have_rev) |
| 325 | *err = -EPROTONOSUPPORT; |
| 326 | return 1; |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(xt_find_revision); |
| 329 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 330 | static char * |
| 331 | textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto) |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 332 | { |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 333 | static const char *const inetbr_names[] = { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 334 | "PREROUTING", "INPUT", "FORWARD", |
| 335 | "OUTPUT", "POSTROUTING", "BROUTING", |
| 336 | }; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 337 | static const char *const arp_names[] = { |
| 338 | "INPUT", "FORWARD", "OUTPUT", |
| 339 | }; |
| 340 | const char *const *names; |
| 341 | unsigned int i, max; |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 342 | char *p = buf; |
| 343 | bool np = false; |
| 344 | int res; |
| 345 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 346 | names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names; |
| 347 | max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) : |
| 348 | ARRAY_SIZE(inetbr_names); |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 349 | *p = '\0'; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 350 | for (i = 0; i < max; ++i) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 351 | if (!(mask & (1 << i))) |
| 352 | continue; |
| 353 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); |
| 354 | if (res > 0) { |
| 355 | size -= res; |
| 356 | p += res; |
| 357 | } |
| 358 | np = true; |
| 359 | } |
| 360 | |
| 361 | return buf; |
| 362 | } |
| 363 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 364 | int xt_check_match(struct xt_mtchk_param *par, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 365 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 366 | { |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 367 | int ret; |
| 368 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 369 | if (XT_ALIGN(par->match->matchsize) != size && |
| 370 | par->match->matchsize != -1) { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 371 | /* |
| 372 | * ebt_among is exempt from centralized matchsize checking |
| 373 | * because it uses a dynamic-size data set. |
| 374 | */ |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 375 | pr_err("%s_tables: %s.%u match: invalid size " |
| 376 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 377 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 378 | par->match->revision, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 379 | XT_ALIGN(par->match->matchsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 382 | if (par->match->table != NULL && |
| 383 | strcmp(par->match->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 384 | pr_err("%s_tables: %s match: only valid in %s table, not %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 385 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 386 | par->match->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 387 | return -EINVAL; |
| 388 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 389 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 390 | char used[64], allow[64]; |
| 391 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 392 | pr_err("%s_tables: %s match: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 393 | "valid from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 394 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 395 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 396 | par->family), |
| 397 | textify_hooks(allow, sizeof(allow), par->match->hooks, |
| 398 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 401 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 402 | pr_err("%s_tables: %s match: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 403 | xt_prefix[par->family], par->match->name, |
| 404 | par->match->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 405 | return -EINVAL; |
| 406 | } |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 407 | if (par->match->checkentry != NULL) { |
| 408 | ret = par->match->checkentry(par); |
| 409 | if (ret < 0) |
| 410 | return ret; |
| 411 | else if (ret > 0) |
| 412 | /* Flag up potential errors. */ |
| 413 | return -EIO; |
| 414 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(xt_check_match); |
| 418 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 419 | /** xt_check_entry_match - check that matches end before start of target |
| 420 | * |
| 421 | * @match: beginning of xt_entry_match |
| 422 | * @target: beginning of this rules target (alleged end of matches) |
| 423 | * @alignment: alignment requirement of match structures |
| 424 | * |
| 425 | * Validates that all matches add up to the beginning of the target, |
| 426 | * and that each match covers at least the base structure size. |
| 427 | * |
| 428 | * Return: 0 on success, negative errno on failure. |
| 429 | */ |
| 430 | static int xt_check_entry_match(const char *match, const char *target, |
| 431 | const size_t alignment) |
| 432 | { |
| 433 | const struct xt_entry_match *pos; |
| 434 | int length = target - match; |
| 435 | |
| 436 | if (length == 0) /* no matches */ |
| 437 | return 0; |
| 438 | |
| 439 | pos = (struct xt_entry_match *)match; |
| 440 | do { |
| 441 | if ((unsigned long)pos % alignment) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | if (length < (int)sizeof(struct xt_entry_match)) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | if (pos->u.match_size < sizeof(struct xt_entry_match)) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | if (pos->u.match_size > length) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | length -= pos->u.match_size; |
| 454 | pos = ((void *)((char *)(pos) + (pos)->u.match_size)); |
| 455 | } while (length > 0); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 460 | #ifdef CONFIG_COMPAT |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 461 | int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 462 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 463 | struct xt_af *xp = &xt[af]; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 464 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 465 | if (!xp->compat_tab) { |
| 466 | if (!xp->number) |
| 467 | return -EINVAL; |
| 468 | xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number); |
| 469 | if (!xp->compat_tab) |
| 470 | return -ENOMEM; |
| 471 | xp->cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 472 | } |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 473 | |
| 474 | if (xp->cur >= xp->number) |
| 475 | return -EINVAL; |
| 476 | |
| 477 | if (xp->cur) |
| 478 | delta += xp->compat_tab[xp->cur - 1].delta; |
| 479 | xp->compat_tab[xp->cur].offset = offset; |
| 480 | xp->compat_tab[xp->cur].delta = delta; |
| 481 | xp->cur++; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); |
| 485 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 486 | void xt_compat_flush_offsets(u_int8_t af) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 487 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 488 | if (xt[af].compat_tab) { |
| 489 | vfree(xt[af].compat_tab); |
| 490 | xt[af].compat_tab = NULL; |
| 491 | xt[af].number = 0; |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 492 | xt[af].cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); |
| 496 | |
Florian Westphal | 3e5e524 | 2010-02-15 18:17:10 +0100 | [diff] [blame] | 497 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 498 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 499 | struct compat_delta *tmp = xt[af].compat_tab; |
| 500 | int mid, left = 0, right = xt[af].cur - 1; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 501 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 502 | while (left <= right) { |
| 503 | mid = (left + right) >> 1; |
| 504 | if (offset > tmp[mid].offset) |
| 505 | left = mid + 1; |
| 506 | else if (offset < tmp[mid].offset) |
| 507 | right = mid - 1; |
| 508 | else |
| 509 | return mid ? tmp[mid - 1].delta : 0; |
| 510 | } |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 511 | return left ? tmp[left - 1].delta : 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 512 | } |
| 513 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); |
| 514 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 515 | void xt_compat_init_offsets(u_int8_t af, unsigned int number) |
| 516 | { |
| 517 | xt[af].number = number; |
| 518 | xt[af].cur = 0; |
| 519 | } |
| 520 | EXPORT_SYMBOL(xt_compat_init_offsets); |
| 521 | |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 522 | int xt_compat_match_offset(const struct xt_match *match) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 523 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 524 | u_int16_t csize = match->compatsize ? : match->matchsize; |
| 525 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 526 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 527 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); |
| 528 | |
Florian Westphal | 0188346 | 2016-04-01 14:17:33 +0200 | [diff] [blame] | 529 | void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
| 530 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 531 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 532 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 533 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
| 534 | int pad, off = xt_compat_match_offset(match); |
| 535 | u_int16_t msize = cm->u.user.match_size; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 536 | char name[sizeof(m->u.user.name)]; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 537 | |
| 538 | m = *dstptr; |
| 539 | memcpy(m, cm, sizeof(*cm)); |
| 540 | if (match->compat_from_user) |
| 541 | match->compat_from_user(m->data, cm->data); |
| 542 | else |
| 543 | memcpy(m->data, cm->data, msize - sizeof(*cm)); |
| 544 | pad = XT_ALIGN(match->matchsize) - match->matchsize; |
| 545 | if (pad > 0) |
| 546 | memset(m->data + match->matchsize, 0, pad); |
| 547 | |
| 548 | msize += off; |
| 549 | m->u.user.match_size = msize; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 550 | strlcpy(name, match->name, sizeof(name)); |
| 551 | module_put(match->me); |
| 552 | strncpy(m->u.user.name, name, sizeof(m->u.user.name)); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 553 | |
| 554 | *size += off; |
| 555 | *dstptr += msize; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); |
| 558 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 559 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
| 560 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 561 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 562 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 563 | struct compat_xt_entry_match __user *cm = *dstptr; |
| 564 | int off = xt_compat_match_offset(match); |
| 565 | u_int16_t msize = m->u.user.match_size - off; |
| 566 | |
| 567 | if (copy_to_user(cm, m, sizeof(*cm)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 568 | put_user(msize, &cm->u.user.match_size) || |
| 569 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, |
| 570 | strlen(m->u.kernel.match->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 571 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 572 | |
| 573 | if (match->compat_to_user) { |
| 574 | if (match->compat_to_user((void __user *)cm->data, m->data)) |
| 575 | return -EFAULT; |
| 576 | } else { |
| 577 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) |
| 578 | return -EFAULT; |
| 579 | } |
| 580 | |
| 581 | *size -= off; |
| 582 | *dstptr += msize; |
| 583 | return 0; |
| 584 | } |
| 585 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 586 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 587 | /* non-compat version may have padding after verdict */ |
| 588 | struct compat_xt_standard_target { |
| 589 | struct compat_xt_entry_target t; |
| 590 | compat_uint_t verdict; |
| 591 | }; |
| 592 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 593 | int xt_compat_check_entry_offsets(const void *base, const char *elems, |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 594 | unsigned int target_offset, |
| 595 | unsigned int next_offset) |
| 596 | { |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 597 | long size_of_base_struct = elems - (const char *)base; |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 598 | const struct compat_xt_entry_target *t; |
| 599 | const char *e = base; |
| 600 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 601 | if (target_offset < size_of_base_struct) |
| 602 | return -EINVAL; |
| 603 | |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 604 | if (target_offset + sizeof(*t) > next_offset) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | t = (void *)(e + target_offset); |
| 608 | if (t->u.target_size < sizeof(*t)) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | if (target_offset + t->u.target_size > next_offset) |
| 612 | return -EINVAL; |
| 613 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 614 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
Florian Westphal | 7b7eba0 | 2016-06-01 02:04:44 +0200 | [diff] [blame] | 615 | COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset) |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 616 | return -EINVAL; |
| 617 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 618 | /* compat_xt_entry match has less strict aligment requirements, |
| 619 | * otherwise they are identical. In case of padding differences |
| 620 | * we need to add compat version of xt_check_entry_match. |
| 621 | */ |
| 622 | BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match)); |
| 623 | |
| 624 | return xt_check_entry_match(elems, base + target_offset, |
| 625 | __alignof__(struct compat_xt_entry_match)); |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 626 | } |
| 627 | EXPORT_SYMBOL(xt_compat_check_entry_offsets); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 628 | #endif /* CONFIG_COMPAT */ |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 629 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 630 | /** |
| 631 | * xt_check_entry_offsets - validate arp/ip/ip6t_entry |
| 632 | * |
| 633 | * @base: pointer to arp/ip/ip6t_entry |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 634 | * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 635 | * @target_offset: the arp/ip/ip6_t->target_offset |
| 636 | * @next_offset: the arp/ip/ip6_t->next_offset |
| 637 | * |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 638 | * validates that target_offset and next_offset are sane and that all |
| 639 | * match sizes (if any) align with the target offset. |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 640 | * |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 641 | * This function does not validate the targets or matches themselves, it |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 642 | * only tests that all the offsets and sizes are correct, that all |
| 643 | * match structures are aligned, and that the last structure ends where |
| 644 | * the target structure begins. |
| 645 | * |
| 646 | * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 647 | * |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 648 | * The arp/ip/ip6t_entry structure @base must have passed following tests: |
| 649 | * - it must point to a valid memory location |
| 650 | * - base to base + next_offset must be accessible, i.e. not exceed allocated |
| 651 | * length. |
| 652 | * |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 653 | * A well-formed entry looks like this: |
| 654 | * |
| 655 | * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry |
| 656 | * e->elems[]-----' | | |
| 657 | * matchsize | | |
| 658 | * matchsize | | |
| 659 | * | | |
| 660 | * target_offset---------------------------------' | |
| 661 | * next_offset---------------------------------------------------' |
| 662 | * |
| 663 | * elems[]: flexible array member at end of ip(6)/arpt_entry struct. |
| 664 | * This is where matches (if any) and the target reside. |
| 665 | * target_offset: beginning of target. |
| 666 | * next_offset: start of the next rule; also: size of this rule. |
| 667 | * Since targets have a minimum size, target_offset + minlen <= next_offset. |
| 668 | * |
| 669 | * Every match stores its size, sum of sizes must not exceed target_offset. |
| 670 | * |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 671 | * Return: 0 on success, negative errno on failure. |
| 672 | */ |
| 673 | int xt_check_entry_offsets(const void *base, |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 674 | const char *elems, |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 675 | unsigned int target_offset, |
| 676 | unsigned int next_offset) |
| 677 | { |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 678 | long size_of_base_struct = elems - (const char *)base; |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 679 | const struct xt_entry_target *t; |
| 680 | const char *e = base; |
| 681 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 682 | /* target start is within the ip/ip6/arpt_entry struct */ |
| 683 | if (target_offset < size_of_base_struct) |
| 684 | return -EINVAL; |
| 685 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 686 | if (target_offset + sizeof(*t) > next_offset) |
| 687 | return -EINVAL; |
| 688 | |
| 689 | t = (void *)(e + target_offset); |
Florian Westphal | a08e4e19 | 2016-04-01 14:17:25 +0200 | [diff] [blame] | 690 | if (t->u.target_size < sizeof(*t)) |
| 691 | return -EINVAL; |
| 692 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 693 | if (target_offset + t->u.target_size > next_offset) |
| 694 | return -EINVAL; |
| 695 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 696 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
Florian Westphal | 7b7eba0 | 2016-06-01 02:04:44 +0200 | [diff] [blame] | 697 | XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset) |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 698 | return -EINVAL; |
| 699 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 700 | return xt_check_entry_match(elems, base + target_offset, |
| 701 | __alignof__(struct xt_entry_match)); |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 702 | } |
| 703 | EXPORT_SYMBOL(xt_check_entry_offsets); |
| 704 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 705 | int xt_check_target(struct xt_tgchk_param *par, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 706 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 707 | { |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 708 | int ret; |
| 709 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 710 | if (XT_ALIGN(par->target->targetsize) != size) { |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 711 | pr_err("%s_tables: %s.%u target: invalid size " |
| 712 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 713 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 714 | par->target->revision, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 715 | XT_ALIGN(par->target->targetsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 716 | return -EINVAL; |
| 717 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 718 | if (par->target->table != NULL && |
| 719 | strcmp(par->target->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 720 | pr_err("%s_tables: %s target: only valid in %s table, not %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 721 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 722 | par->target->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 723 | return -EINVAL; |
| 724 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 725 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 726 | char used[64], allow[64]; |
| 727 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 728 | pr_err("%s_tables: %s target: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 729 | "usable from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 730 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 731 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 732 | par->family), |
| 733 | textify_hooks(allow, sizeof(allow), par->target->hooks, |
| 734 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 737 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 738 | pr_err("%s_tables: %s target: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 739 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 740 | par->target->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 741 | return -EINVAL; |
| 742 | } |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 743 | if (par->target->checkentry != NULL) { |
| 744 | ret = par->target->checkentry(par); |
| 745 | if (ret < 0) |
| 746 | return ret; |
| 747 | else if (ret > 0) |
| 748 | /* Flag up potential errors. */ |
| 749 | return -EIO; |
| 750 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | EXPORT_SYMBOL_GPL(xt_check_target); |
| 754 | |
Florian Westphal | d7591f0 | 2016-04-01 15:37:59 +0200 | [diff] [blame] | 755 | /** |
| 756 | * xt_copy_counters_from_user - copy counters and metadata from userspace |
| 757 | * |
| 758 | * @user: src pointer to userspace memory |
| 759 | * @len: alleged size of userspace memory |
| 760 | * @info: where to store the xt_counters_info metadata |
| 761 | * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel |
| 762 | * |
| 763 | * Copies counter meta data from @user and stores it in @info. |
| 764 | * |
| 765 | * vmallocs memory to hold the counters, then copies the counter data |
| 766 | * from @user to the new memory and returns a pointer to it. |
| 767 | * |
| 768 | * If @compat is true, @info gets converted automatically to the 64bit |
| 769 | * representation. |
| 770 | * |
| 771 | * The metadata associated with the counters is stored in @info. |
| 772 | * |
| 773 | * Return: returns pointer that caller has to test via IS_ERR(). |
| 774 | * If IS_ERR is false, caller has to vfree the pointer. |
| 775 | */ |
| 776 | void *xt_copy_counters_from_user(const void __user *user, unsigned int len, |
| 777 | struct xt_counters_info *info, bool compat) |
| 778 | { |
| 779 | void *mem; |
| 780 | u64 size; |
| 781 | |
| 782 | #ifdef CONFIG_COMPAT |
| 783 | if (compat) { |
| 784 | /* structures only differ in size due to alignment */ |
| 785 | struct compat_xt_counters_info compat_tmp; |
| 786 | |
| 787 | if (len <= sizeof(compat_tmp)) |
| 788 | return ERR_PTR(-EINVAL); |
| 789 | |
| 790 | len -= sizeof(compat_tmp); |
| 791 | if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0) |
| 792 | return ERR_PTR(-EFAULT); |
| 793 | |
| 794 | strlcpy(info->name, compat_tmp.name, sizeof(info->name)); |
| 795 | info->num_counters = compat_tmp.num_counters; |
| 796 | user += sizeof(compat_tmp); |
| 797 | } else |
| 798 | #endif |
| 799 | { |
| 800 | if (len <= sizeof(*info)) |
| 801 | return ERR_PTR(-EINVAL); |
| 802 | |
| 803 | len -= sizeof(*info); |
| 804 | if (copy_from_user(info, user, sizeof(*info)) != 0) |
| 805 | return ERR_PTR(-EFAULT); |
| 806 | |
| 807 | info->name[sizeof(info->name) - 1] = '\0'; |
| 808 | user += sizeof(*info); |
| 809 | } |
| 810 | |
| 811 | size = sizeof(struct xt_counters); |
| 812 | size *= info->num_counters; |
| 813 | |
| 814 | if (size != (u64)len) |
| 815 | return ERR_PTR(-EINVAL); |
| 816 | |
| 817 | mem = vmalloc(len); |
| 818 | if (!mem) |
| 819 | return ERR_PTR(-ENOMEM); |
| 820 | |
| 821 | if (copy_from_user(mem, user, len) == 0) |
| 822 | return mem; |
| 823 | |
| 824 | vfree(mem); |
| 825 | return ERR_PTR(-EFAULT); |
| 826 | } |
| 827 | EXPORT_SYMBOL_GPL(xt_copy_counters_from_user); |
| 828 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 829 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 830 | int xt_compat_target_offset(const struct xt_target *target) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 831 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 832 | u_int16_t csize = target->compatsize ? : target->targetsize; |
| 833 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 834 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 835 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); |
| 836 | |
| 837 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, |
Patrick McHardy | b0a6363 | 2008-01-31 04:10:18 -0800 | [diff] [blame] | 838 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 839 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 840 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 841 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
| 842 | int pad, off = xt_compat_target_offset(target); |
| 843 | u_int16_t tsize = ct->u.user.target_size; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 844 | char name[sizeof(t->u.user.name)]; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 845 | |
| 846 | t = *dstptr; |
| 847 | memcpy(t, ct, sizeof(*ct)); |
| 848 | if (target->compat_from_user) |
| 849 | target->compat_from_user(t->data, ct->data); |
| 850 | else |
| 851 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); |
| 852 | pad = XT_ALIGN(target->targetsize) - target->targetsize; |
| 853 | if (pad > 0) |
| 854 | memset(t->data + target->targetsize, 0, pad); |
| 855 | |
| 856 | tsize += off; |
| 857 | t->u.user.target_size = tsize; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 858 | strlcpy(name, target->name, sizeof(name)); |
| 859 | module_put(target->me); |
| 860 | strncpy(t->u.user.name, name, sizeof(t->u.user.name)); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 861 | |
| 862 | *size += off; |
| 863 | *dstptr += tsize; |
| 864 | } |
| 865 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); |
| 866 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 867 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
| 868 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 869 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 870 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 871 | struct compat_xt_entry_target __user *ct = *dstptr; |
| 872 | int off = xt_compat_target_offset(target); |
| 873 | u_int16_t tsize = t->u.user.target_size - off; |
| 874 | |
| 875 | if (copy_to_user(ct, t, sizeof(*ct)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 876 | put_user(tsize, &ct->u.user.target_size) || |
| 877 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, |
| 878 | strlen(t->u.kernel.target->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 879 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 880 | |
| 881 | if (target->compat_to_user) { |
| 882 | if (target->compat_to_user((void __user *)ct->data, t->data)) |
| 883 | return -EFAULT; |
| 884 | } else { |
| 885 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) |
| 886 | return -EFAULT; |
| 887 | } |
| 888 | |
| 889 | *size -= off; |
| 890 | *dstptr += tsize; |
| 891 | return 0; |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 894 | #endif |
| 895 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 896 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
| 897 | { |
Eric Dumazet | 711bdde6 | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 898 | struct xt_table_info *info = NULL; |
| 899 | size_t sz = sizeof(*info) + size; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 900 | |
Florian Westphal | d157bd7 | 2016-03-10 01:56:23 +0100 | [diff] [blame] | 901 | if (sz < sizeof(*info)) |
| 902 | return NULL; |
| 903 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 904 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 905 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 906 | return NULL; |
| 907 | |
Eric Dumazet | 711bdde6 | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 908 | if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) |
| 909 | info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 910 | if (!info) { |
| 911 | info = vmalloc(sz); |
| 912 | if (!info) |
| 913 | return NULL; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 914 | } |
Eric Dumazet | 711bdde6 | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 915 | memset(info, 0, sizeof(*info)); |
| 916 | info->size = size; |
| 917 | return info; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 918 | } |
| 919 | EXPORT_SYMBOL(xt_alloc_table_info); |
| 920 | |
| 921 | void xt_free_table_info(struct xt_table_info *info) |
| 922 | { |
| 923 | int cpu; |
| 924 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 925 | if (info->jumpstack != NULL) { |
Eric Dumazet | f6b5082 | 2014-06-24 02:15:35 -0700 | [diff] [blame] | 926 | for_each_possible_cpu(cpu) |
| 927 | kvfree(info->jumpstack[cpu]); |
| 928 | kvfree(info->jumpstack); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Eric Dumazet | 711bdde6 | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 931 | kvfree(info); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 932 | } |
| 933 | EXPORT_SYMBOL(xt_free_table_info); |
| 934 | |
| 935 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 936 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
| 937 | const char *name) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 938 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 939 | struct xt_table *t, *found = NULL; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 940 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 941 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 942 | list_for_each_entry(t, &net->xt.tables[af], list) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 943 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
| 944 | return t; |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 945 | |
| 946 | if (net == &init_net) |
| 947 | goto out; |
| 948 | |
| 949 | /* Table doesn't exist in this netns, re-try init */ |
| 950 | list_for_each_entry(t, &init_net.xt.tables[af], list) { |
| 951 | if (strcmp(t->name, name)) |
| 952 | continue; |
| 953 | if (!try_module_get(t->me)) |
| 954 | return NULL; |
| 955 | |
| 956 | mutex_unlock(&xt[af].mutex); |
| 957 | if (t->table_init(net) != 0) { |
| 958 | module_put(t->me); |
| 959 | return NULL; |
| 960 | } |
| 961 | |
| 962 | found = t; |
| 963 | |
| 964 | mutex_lock(&xt[af].mutex); |
| 965 | break; |
| 966 | } |
| 967 | |
| 968 | if (!found) |
| 969 | goto out; |
| 970 | |
| 971 | /* and once again: */ |
| 972 | list_for_each_entry(t, &net->xt.tables[af], list) |
| 973 | if (strcmp(t->name, name) == 0) |
| 974 | return t; |
| 975 | |
| 976 | module_put(found->me); |
| 977 | out: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 978 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 979 | return NULL; |
| 980 | } |
| 981 | EXPORT_SYMBOL_GPL(xt_find_table_lock); |
| 982 | |
| 983 | void xt_table_unlock(struct xt_table *table) |
| 984 | { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 985 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 986 | } |
| 987 | EXPORT_SYMBOL_GPL(xt_table_unlock); |
| 988 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 989 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 990 | void xt_compat_lock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 991 | { |
| 992 | mutex_lock(&xt[af].compat_mutex); |
| 993 | } |
| 994 | EXPORT_SYMBOL_GPL(xt_compat_lock); |
| 995 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 996 | void xt_compat_unlock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 997 | { |
| 998 | mutex_unlock(&xt[af].compat_mutex); |
| 999 | } |
| 1000 | EXPORT_SYMBOL_GPL(xt_compat_unlock); |
| 1001 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1002 | |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 1003 | DEFINE_PER_CPU(seqcount_t, xt_recseq); |
| 1004 | EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1005 | |
Florian Westphal | dcebd31 | 2015-07-14 17:51:09 +0200 | [diff] [blame] | 1006 | struct static_key xt_tee_enabled __read_mostly; |
| 1007 | EXPORT_SYMBOL_GPL(xt_tee_enabled); |
| 1008 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1009 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
| 1010 | { |
| 1011 | unsigned int size; |
| 1012 | int cpu; |
| 1013 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1014 | size = sizeof(void **) * nr_cpu_ids; |
| 1015 | if (size > PAGE_SIZE) |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 1016 | i->jumpstack = vzalloc(size); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1017 | else |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 1018 | i->jumpstack = kzalloc(size, GFP_KERNEL); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1019 | if (i->jumpstack == NULL) |
| 1020 | return -ENOMEM; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1021 | |
Florian Westphal | 98d1bd8 | 2015-07-14 17:51:06 +0200 | [diff] [blame] | 1022 | /* ruleset without jumps -- no stack needed */ |
| 1023 | if (i->stacksize == 0) |
| 1024 | return 0; |
| 1025 | |
Florian Westphal | 7814b6e | 2015-07-14 17:51:08 +0200 | [diff] [blame] | 1026 | /* Jumpstack needs to be able to record two full callchains, one |
| 1027 | * from the first rule set traversal, plus one table reentrancy |
| 1028 | * via -j TEE without clobbering the callchain that brought us to |
| 1029 | * TEE target. |
| 1030 | * |
| 1031 | * This is done by allocating two jumpstacks per cpu, on reentry |
| 1032 | * the upper half of the stack is used. |
| 1033 | * |
| 1034 | * see the jumpstack setup in ipt_do_table() for more details. |
| 1035 | */ |
| 1036 | size = sizeof(void *) * i->stacksize * 2u; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1037 | for_each_possible_cpu(cpu) { |
| 1038 | if (size > PAGE_SIZE) |
| 1039 | i->jumpstack[cpu] = vmalloc_node(size, |
| 1040 | cpu_to_node(cpu)); |
| 1041 | else |
| 1042 | i->jumpstack[cpu] = kmalloc_node(size, |
| 1043 | GFP_KERNEL, cpu_to_node(cpu)); |
| 1044 | if (i->jumpstack[cpu] == NULL) |
| 1045 | /* |
| 1046 | * Freeing will be done later on by the callers. The |
| 1047 | * chain is: xt_replace_table -> __do_replace -> |
| 1048 | * do_replace -> xt_free_table_info. |
| 1049 | */ |
| 1050 | return -ENOMEM; |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1055 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1056 | struct xt_table_info * |
| 1057 | xt_replace_table(struct xt_table *table, |
| 1058 | unsigned int num_counters, |
| 1059 | struct xt_table_info *newinfo, |
| 1060 | int *error) |
| 1061 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1062 | struct xt_table_info *private; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1063 | int ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1064 | |
Jan Engelhardt | d97a9e4 | 2010-04-21 14:45:51 +0200 | [diff] [blame] | 1065 | ret = xt_jumpstack_alloc(newinfo); |
| 1066 | if (ret < 0) { |
| 1067 | *error = ret; |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1071 | /* Do the substitution. */ |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1072 | local_bh_disable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1073 | private = table->private; |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1074 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1075 | /* Check inside lock: is the old number correct? */ |
| 1076 | if (num_counters != private->number) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 1077 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1078 | num_counters, private->number); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1079 | local_bh_enable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1080 | *error = -EAGAIN; |
| 1081 | return NULL; |
| 1082 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1083 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1084 | newinfo->initial_entries = private->initial_entries; |
Will Deacon | b416c14 | 2013-10-21 13:14:53 +0100 | [diff] [blame] | 1085 | /* |
| 1086 | * Ensure contents of newinfo are visible before assigning to |
| 1087 | * private. |
| 1088 | */ |
| 1089 | smp_wmb(); |
| 1090 | table->private = newinfo; |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* |
| 1093 | * Even though table entries have now been swapped, other CPU's |
| 1094 | * may still be using the old entries. This is okay, because |
| 1095 | * resynchronization happens because of the locking done |
| 1096 | * during the get_counters() routine. |
| 1097 | */ |
| 1098 | local_bh_enable(); |
| 1099 | |
Thomas Graf | fbabf31 | 2011-01-16 18:12:59 +0100 | [diff] [blame] | 1100 | #ifdef CONFIG_AUDIT |
| 1101 | if (audit_enabled) { |
| 1102 | struct audit_buffer *ab; |
| 1103 | |
| 1104 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
| 1105 | AUDIT_NETFILTER_CFG); |
| 1106 | if (ab) { |
| 1107 | audit_log_format(ab, "table=%s family=%u entries=%u", |
| 1108 | table->name, table->af, |
| 1109 | private->number); |
| 1110 | audit_log_end(ab); |
| 1111 | } |
| 1112 | } |
| 1113 | #endif |
| 1114 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1115 | return private; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1116 | } |
| 1117 | EXPORT_SYMBOL_GPL(xt_replace_table); |
| 1118 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1119 | struct xt_table *xt_register_table(struct net *net, |
| 1120 | const struct xt_table *input_table, |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1121 | struct xt_table_info *bootstrap, |
| 1122 | struct xt_table_info *newinfo) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1123 | { |
| 1124 | int ret; |
| 1125 | struct xt_table_info *private; |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1126 | struct xt_table *t, *table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1127 | |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1128 | /* Don't add one object to multiple lists. */ |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1129 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1130 | if (!table) { |
| 1131 | ret = -ENOMEM; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 1135 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1136 | /* Don't autoload: we'd eat our tail... */ |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1137 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1138 | if (strcmp(t->name, table->name) == 0) { |
| 1139 | ret = -EEXIST; |
| 1140 | goto unlock; |
| 1141 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* Simplifies replace_table code. */ |
| 1145 | table->private = bootstrap; |
Stephen Hemminger | 7845447 | 2009-02-20 10:35:32 +0100 | [diff] [blame] | 1146 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1147 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
| 1148 | goto unlock; |
| 1149 | |
| 1150 | private = table->private; |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 1151 | pr_debug("table->private->number = %u\n", private->number); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1152 | |
| 1153 | /* save number of initial entries */ |
| 1154 | private->initial_entries = private->number; |
| 1155 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1156 | list_add(&table->list, &net->xt.tables[table->af]); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1157 | mutex_unlock(&xt[table->af].mutex); |
| 1158 | return table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1159 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 1160 | unlock: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1161 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1162 | kfree(table); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1163 | out: |
| 1164 | return ERR_PTR(ret); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1165 | } |
| 1166 | EXPORT_SYMBOL_GPL(xt_register_table); |
| 1167 | |
| 1168 | void *xt_unregister_table(struct xt_table *table) |
| 1169 | { |
| 1170 | struct xt_table_info *private; |
| 1171 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1172 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1173 | private = table->private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1174 | list_del(&table->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1175 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1176 | kfree(table); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1177 | |
| 1178 | return private; |
| 1179 | } |
| 1180 | EXPORT_SYMBOL_GPL(xt_unregister_table); |
| 1181 | |
| 1182 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1183 | struct xt_names_priv { |
| 1184 | struct seq_net_private p; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1185 | u_int8_t af; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1186 | }; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1187 | static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1188 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1189 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 1190 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1191 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1192 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1193 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1194 | return seq_list_start(&net->xt.tables[af], *pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1197 | static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1198 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1199 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 1200 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1201 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1202 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1203 | return seq_list_next(v, &net->xt.tables[af], pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1206 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1207 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1208 | struct xt_names_priv *priv = seq->private; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1209 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1210 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1211 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1214 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1215 | { |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1216 | struct xt_table *table = list_entry(v, struct xt_table, list); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1217 | |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1218 | if (*table->name) |
Steven Rostedt (Red Hat) | e71456ae | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 1219 | seq_printf(seq, "%s\n", table->name); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1220 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1223 | static const struct seq_operations xt_table_seq_ops = { |
| 1224 | .start = xt_table_seq_start, |
| 1225 | .next = xt_table_seq_next, |
| 1226 | .stop = xt_table_seq_stop, |
| 1227 | .show = xt_table_seq_show, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1228 | }; |
| 1229 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1230 | static int xt_table_open(struct inode *inode, struct file *file) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1231 | { |
| 1232 | int ret; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1233 | struct xt_names_priv *priv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1234 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1235 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
| 1236 | sizeof(struct xt_names_priv)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1237 | if (!ret) { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1238 | priv = ((struct seq_file *)file->private_data)->private; |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1239 | priv->af = (unsigned long)PDE_DATA(inode); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1240 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1241 | return ret; |
| 1242 | } |
| 1243 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1244 | static const struct file_operations xt_table_ops = { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1245 | .owner = THIS_MODULE, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1246 | .open = xt_table_open, |
| 1247 | .read = seq_read, |
| 1248 | .llseek = seq_lseek, |
Pavel Emelyanov | 0e93bb94 | 2008-04-29 03:15:35 -0700 | [diff] [blame] | 1249 | .release = seq_release_net, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1250 | }; |
| 1251 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1252 | /* |
| 1253 | * Traverse state for ip{,6}_{tables,matches} for helping crossing |
| 1254 | * the multi-AF mutexes. |
| 1255 | */ |
| 1256 | struct nf_mttg_trav { |
| 1257 | struct list_head *head, *curr; |
| 1258 | uint8_t class, nfproto; |
| 1259 | }; |
| 1260 | |
| 1261 | enum { |
| 1262 | MTTG_TRAV_INIT, |
| 1263 | MTTG_TRAV_NFP_UNSPEC, |
| 1264 | MTTG_TRAV_NFP_SPEC, |
| 1265 | MTTG_TRAV_DONE, |
| 1266 | }; |
| 1267 | |
| 1268 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, |
| 1269 | bool is_target) |
| 1270 | { |
| 1271 | static const uint8_t next_class[] = { |
| 1272 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, |
| 1273 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, |
| 1274 | }; |
| 1275 | struct nf_mttg_trav *trav = seq->private; |
| 1276 | |
| 1277 | switch (trav->class) { |
| 1278 | case MTTG_TRAV_INIT: |
| 1279 | trav->class = MTTG_TRAV_NFP_UNSPEC; |
| 1280 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); |
| 1281 | trav->head = trav->curr = is_target ? |
| 1282 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; |
| 1283 | break; |
| 1284 | case MTTG_TRAV_NFP_UNSPEC: |
| 1285 | trav->curr = trav->curr->next; |
| 1286 | if (trav->curr != trav->head) |
| 1287 | break; |
| 1288 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1289 | mutex_lock(&xt[trav->nfproto].mutex); |
| 1290 | trav->head = trav->curr = is_target ? |
| 1291 | &xt[trav->nfproto].target : &xt[trav->nfproto].match; |
| 1292 | trav->class = next_class[trav->class]; |
| 1293 | break; |
| 1294 | case MTTG_TRAV_NFP_SPEC: |
| 1295 | trav->curr = trav->curr->next; |
| 1296 | if (trav->curr != trav->head) |
| 1297 | break; |
| 1298 | /* fallthru, _stop will unlock */ |
| 1299 | default: |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | |
| 1303 | if (ppos != NULL) |
| 1304 | ++*ppos; |
| 1305 | return trav; |
| 1306 | } |
| 1307 | |
| 1308 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
| 1309 | bool is_target) |
| 1310 | { |
| 1311 | struct nf_mttg_trav *trav = seq->private; |
| 1312 | unsigned int j; |
| 1313 | |
| 1314 | trav->class = MTTG_TRAV_INIT; |
| 1315 | for (j = 0; j < *pos; ++j) |
| 1316 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) |
| 1317 | return NULL; |
| 1318 | return trav; |
| 1319 | } |
| 1320 | |
| 1321 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
| 1322 | { |
| 1323 | struct nf_mttg_trav *trav = seq->private; |
| 1324 | |
| 1325 | switch (trav->class) { |
| 1326 | case MTTG_TRAV_NFP_UNSPEC: |
| 1327 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1328 | break; |
| 1329 | case MTTG_TRAV_NFP_SPEC: |
| 1330 | mutex_unlock(&xt[trav->nfproto].mutex); |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1335 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
| 1336 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1337 | return xt_mttg_seq_start(seq, pos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1340 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1341 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1342 | return xt_mttg_seq_next(seq, v, ppos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
| 1346 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1347 | const struct nf_mttg_trav *trav = seq->private; |
| 1348 | const struct xt_match *match; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1349 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1350 | switch (trav->class) { |
| 1351 | case MTTG_TRAV_NFP_UNSPEC: |
| 1352 | case MTTG_TRAV_NFP_SPEC: |
| 1353 | if (trav->curr == trav->head) |
| 1354 | return 0; |
| 1355 | match = list_entry(trav->curr, struct xt_match, list); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1356 | if (*match->name) |
| 1357 | seq_printf(seq, "%s\n", match->name); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1358 | } |
| 1359 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static const struct seq_operations xt_match_seq_ops = { |
| 1363 | .start = xt_match_seq_start, |
| 1364 | .next = xt_match_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1365 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1366 | .show = xt_match_seq_show, |
| 1367 | }; |
| 1368 | |
| 1369 | static int xt_match_open(struct inode *inode, struct file *file) |
| 1370 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1371 | struct nf_mttg_trav *trav; |
Rob Jones | 772476d | 2014-09-19 11:27:51 +0100 | [diff] [blame] | 1372 | trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav)); |
| 1373 | if (!trav) |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1374 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1375 | |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1376 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1377 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static const struct file_operations xt_match_ops = { |
| 1381 | .owner = THIS_MODULE, |
| 1382 | .open = xt_match_open, |
| 1383 | .read = seq_read, |
| 1384 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1385 | .release = seq_release_private, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1386 | }; |
| 1387 | |
| 1388 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
| 1389 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1390 | return xt_mttg_seq_start(seq, pos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1393 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1394 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1395 | return xt_mttg_seq_next(seq, v, ppos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static int xt_target_seq_show(struct seq_file *seq, void *v) |
| 1399 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1400 | const struct nf_mttg_trav *trav = seq->private; |
| 1401 | const struct xt_target *target; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1402 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1403 | switch (trav->class) { |
| 1404 | case MTTG_TRAV_NFP_UNSPEC: |
| 1405 | case MTTG_TRAV_NFP_SPEC: |
| 1406 | if (trav->curr == trav->head) |
| 1407 | return 0; |
| 1408 | target = list_entry(trav->curr, struct xt_target, list); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1409 | if (*target->name) |
| 1410 | seq_printf(seq, "%s\n", target->name); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1411 | } |
| 1412 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | static const struct seq_operations xt_target_seq_ops = { |
| 1416 | .start = xt_target_seq_start, |
| 1417 | .next = xt_target_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1418 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1419 | .show = xt_target_seq_show, |
| 1420 | }; |
| 1421 | |
| 1422 | static int xt_target_open(struct inode *inode, struct file *file) |
| 1423 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1424 | struct nf_mttg_trav *trav; |
Rob Jones | 772476d | 2014-09-19 11:27:51 +0100 | [diff] [blame] | 1425 | trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav)); |
| 1426 | if (!trav) |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1427 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1428 | |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1429 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1430 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | static const struct file_operations xt_target_ops = { |
| 1434 | .owner = THIS_MODULE, |
| 1435 | .open = xt_target_open, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1436 | .read = seq_read, |
| 1437 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1438 | .release = seq_release_private, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1439 | }; |
| 1440 | |
| 1441 | #define FORMAT_TABLES "_tables_names" |
| 1442 | #define FORMAT_MATCHES "_tables_matches" |
| 1443 | #define FORMAT_TARGETS "_tables_targets" |
| 1444 | |
| 1445 | #endif /* CONFIG_PROC_FS */ |
| 1446 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1447 | /** |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1448 | * xt_hook_ops_alloc - set up hooks for a new table |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1449 | * @table: table with metadata needed to set up hooks |
| 1450 | * @fn: Hook function |
| 1451 | * |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1452 | * This function will create the nf_hook_ops that the x_table needs |
| 1453 | * to hand to xt_hook_link_net(). |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1454 | */ |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1455 | struct nf_hook_ops * |
| 1456 | xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn) |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1457 | { |
| 1458 | unsigned int hook_mask = table->valid_hooks; |
| 1459 | uint8_t i, num_hooks = hweight32(hook_mask); |
| 1460 | uint8_t hooknum; |
| 1461 | struct nf_hook_ops *ops; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1462 | |
| 1463 | ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL); |
| 1464 | if (ops == NULL) |
| 1465 | return ERR_PTR(-ENOMEM); |
| 1466 | |
| 1467 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; |
| 1468 | hook_mask >>= 1, ++hooknum) { |
| 1469 | if (!(hook_mask & 1)) |
| 1470 | continue; |
| 1471 | ops[i].hook = fn; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1472 | ops[i].pf = table->af; |
| 1473 | ops[i].hooknum = hooknum; |
| 1474 | ops[i].priority = table->priority; |
| 1475 | ++i; |
| 1476 | } |
| 1477 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1478 | return ops; |
| 1479 | } |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1480 | EXPORT_SYMBOL_GPL(xt_hook_ops_alloc); |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1481 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1482 | int xt_proto_init(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1483 | { |
| 1484 | #ifdef CONFIG_PROC_FS |
| 1485 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1486 | struct proc_dir_entry *proc; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1487 | kuid_t root_uid; |
| 1488 | kgid_t root_gid; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1489 | #endif |
| 1490 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1491 | if (af >= ARRAY_SIZE(xt_prefix)) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1492 | return -EINVAL; |
| 1493 | |
| 1494 | |
| 1495 | #ifdef CONFIG_PROC_FS |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1496 | root_uid = make_kuid(net->user_ns, 0); |
| 1497 | root_gid = make_kgid(net->user_ns, 0); |
| 1498 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1499 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1500 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1501 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops, |
| 1502 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1503 | if (!proc) |
| 1504 | goto out; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1505 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1506 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1507 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1508 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1509 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1510 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops, |
| 1511 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1512 | if (!proc) |
| 1513 | goto out_remove_tables; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1514 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1515 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1516 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1517 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1518 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1519 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops, |
| 1520 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1521 | if (!proc) |
| 1522 | goto out_remove_matches; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1523 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1524 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1525 | #endif |
| 1526 | |
| 1527 | return 0; |
| 1528 | |
| 1529 | #ifdef CONFIG_PROC_FS |
| 1530 | out_remove_matches: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1531 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1532 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1533 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1534 | |
| 1535 | out_remove_tables: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1536 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1537 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1538 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1539 | out: |
| 1540 | return -1; |
| 1541 | #endif |
| 1542 | } |
| 1543 | EXPORT_SYMBOL_GPL(xt_proto_init); |
| 1544 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1545 | void xt_proto_fini(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1546 | { |
| 1547 | #ifdef CONFIG_PROC_FS |
| 1548 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1549 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1550 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1551 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1552 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1553 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1554 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1555 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1556 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1557 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1558 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1559 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1560 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1561 | #endif /*CONFIG_PROC_FS*/ |
| 1562 | } |
| 1563 | EXPORT_SYMBOL_GPL(xt_proto_fini); |
| 1564 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1565 | static int __net_init xt_net_init(struct net *net) |
| 1566 | { |
| 1567 | int i; |
| 1568 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1569 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1570 | INIT_LIST_HEAD(&net->xt.tables[i]); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | static struct pernet_operations xt_net_ops = { |
| 1575 | .init = xt_net_init, |
| 1576 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1577 | |
| 1578 | static int __init xt_init(void) |
| 1579 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1580 | unsigned int i; |
| 1581 | int rv; |
| 1582 | |
| 1583 | for_each_possible_cpu(i) { |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 1584 | seqcount_init(&per_cpu(xt_recseq, i)); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1585 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1586 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1587 | xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1588 | if (!xt) |
| 1589 | return -ENOMEM; |
| 1590 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1591 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1592 | mutex_init(&xt[i].mutex); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1593 | #ifdef CONFIG_COMPAT |
| 1594 | mutex_init(&xt[i].compat_mutex); |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 1595 | xt[i].compat_tab = NULL; |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1596 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1597 | INIT_LIST_HEAD(&xt[i].target); |
| 1598 | INIT_LIST_HEAD(&xt[i].match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1599 | } |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1600 | rv = register_pernet_subsys(&xt_net_ops); |
| 1601 | if (rv < 0) |
| 1602 | kfree(xt); |
| 1603 | return rv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | static void __exit xt_fini(void) |
| 1607 | { |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1608 | unregister_pernet_subsys(&xt_net_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1609 | kfree(xt); |
| 1610 | } |
| 1611 | |
| 1612 | module_init(xt_init); |
| 1613 | module_exit(xt_fini); |
| 1614 | |