Merge tag 'platform-drivers-x86-v6.9-3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / net / ipv4 / netfilter / ip_tables.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * Packet matching code.
4 *
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
2e4e6a17 6 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
f229f6ce 7 * Copyright (C) 2006-2010 Patrick McHardy <kaber@trash.net>
1da177e4 8 */
90e7d4ab 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4 10#include <linux/cache.h>
4fc268d2 11#include <linux/capability.h>
1da177e4
LT
12#include <linux/skbuff.h>
13#include <linux/kmod.h>
14#include <linux/vmalloc.h>
15#include <linux/netdevice.h>
16#include <linux/module.h>
1da177e4 17#include <net/ip.h>
2722971c 18#include <net/compat.h>
7c0f6ba6 19#include <linux/uaccess.h>
57b47a53 20#include <linux/mutex.h>
1da177e4
LT
21#include <linux/proc_fs.h>
22#include <linux/err.h>
c8923c6b 23#include <linux/cpumask.h>
1da177e4 24
2e4e6a17 25#include <linux/netfilter/x_tables.h>
1da177e4 26#include <linux/netfilter_ipv4/ip_tables.h>
f01ffbd6 27#include <net/netfilter/nf_log.h>
e3eaa991 28#include "../../netfilter/xt_repldata.h"
1da177e4
LT
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32MODULE_DESCRIPTION("IPv4 packet filter");
33
e3eaa991
JE
34void *ipt_alloc_initial_table(const struct xt_table *info)
35{
36 return xt_alloc_initial_table(ipt, IPT);
37}
38EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
39
1da177e4 40/* Returns whether matches rule or not. */
022748a9 41/* Performance critical - called for every packet */
9c547959 42static inline bool
1da177e4
LT
43ip_packet_match(const struct iphdr *ip,
44 const char *indev,
45 const char *outdev,
46 const struct ipt_ip *ipinfo,
47 int isfrag)
48{
1da177e4
LT
49 unsigned long ret;
50
c37a2dfa
JP
51 if (NF_INVF(ipinfo, IPT_INV_SRCIP,
52 (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
53 NF_INVF(ipinfo, IPT_INV_DSTIP,
54 (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
9c547959 55 return false;
1da177e4 56
b8dfe498 57 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
1da177e4 58
c37a2dfa 59 if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
9c547959 60 return false;
1da177e4 61
b8dfe498 62 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
1da177e4 63
c37a2dfa 64 if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
9c547959 65 return false;
1da177e4
LT
66
67 /* Check specific protocol */
3666ed1c 68 if (ipinfo->proto &&
c37a2dfa 69 NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
9c547959 70 return false;
1da177e4
LT
71
72 /* If we have a fragment rule but the packet is not a fragment
73 * then we return zero */
c37a2dfa
JP
74 if (NF_INVF(ipinfo, IPT_INV_FRAG,
75 (ipinfo->flags & IPT_F_FRAG) && !isfrag))
9c547959 76 return false;
1da177e4 77
9c547959 78 return true;
1da177e4
LT
79}
80
022748a9 81static bool
1da177e4
LT
82ip_checkentry(const struct ipt_ip *ip)
83{
d7cdf816 84 if (ip->flags & ~IPT_F_MASK)
ccb79bdc 85 return false;
d7cdf816 86 if (ip->invflags & ~IPT_INV_MASK)
ccb79bdc 87 return false;
ccb79bdc 88 return true;
1da177e4
LT
89}
90
91static unsigned int
4b560b44 92ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 93{
e87cc472 94 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
1da177e4
LT
95
96 return NF_DROP;
97}
98
022748a9 99/* Performance critical */
1da177e4 100static inline struct ipt_entry *
d5d1baa1 101get_entry(const void *base, unsigned int offset)
1da177e4
LT
102{
103 return (struct ipt_entry *)(base + offset);
104}
105
ba9dda3a 106/* All zeroes == unconditional rule. */
022748a9 107/* Mildly perf critical (only if packet tracing is on) */
54d83fc7 108static inline bool unconditional(const struct ipt_entry *e)
ba9dda3a 109{
47901dc2 110 static const struct ipt_ip uncond;
ba9dda3a 111
54d83fc7
FW
112 return e->target_offset == sizeof(struct ipt_entry) &&
113 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
ba9dda3a
JK
114}
115
d5d1baa1 116/* for const-correctness */
87a2e70d 117static inline const struct xt_entry_target *
d5d1baa1
JE
118ipt_get_target_c(const struct ipt_entry *e)
119{
120 return ipt_get_target((struct ipt_entry *)e);
121}
122
f0165888 123#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
022748a9 124static const char *const hooknames[] = {
6e23ae2a
PM
125 [NF_INET_PRE_ROUTING] = "PREROUTING",
126 [NF_INET_LOCAL_IN] = "INPUT",
9c547959 127 [NF_INET_FORWARD] = "FORWARD",
6e23ae2a
PM
128 [NF_INET_LOCAL_OUT] = "OUTPUT",
129 [NF_INET_POST_ROUTING] = "POSTROUTING",
ba9dda3a
JK
130};
131
132enum nf_ip_trace_comments {
133 NF_IP_TRACE_COMMENT_RULE,
134 NF_IP_TRACE_COMMENT_RETURN,
135 NF_IP_TRACE_COMMENT_POLICY,
136};
137
022748a9 138static const char *const comments[] = {
ba9dda3a
JK
139 [NF_IP_TRACE_COMMENT_RULE] = "rule",
140 [NF_IP_TRACE_COMMENT_RETURN] = "return",
141 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
142};
143
549d2d41 144static const struct nf_loginfo trace_loginfo = {
ba9dda3a
JK
145 .type = NF_LOG_TYPE_LOG,
146 .u = {
147 .log = {
148 .level = 4,
ff107d27 149 .logflags = NF_LOG_DEFAULT_MASK,
ba9dda3a
JK
150 },
151 },
152};
153
022748a9 154/* Mildly perf critical (only if packet tracing is on) */
ba9dda3a 155static inline int
d5d1baa1 156get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
4f2f6f23
JE
157 const char *hookname, const char **chainname,
158 const char **comment, unsigned int *rulenum)
ba9dda3a 159{
87a2e70d 160 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
ba9dda3a 161
243bf6e2 162 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
ba9dda3a
JK
163 /* Head of user chain: ERROR target with chainname */
164 *chainname = t->target.data;
165 (*rulenum) = 0;
166 } else if (s == e) {
167 (*rulenum)++;
168
54d83fc7 169 if (unconditional(s) &&
3666ed1c 170 strcmp(t->target.u.kernel.target->name,
243bf6e2 171 XT_STANDARD_TARGET) == 0 &&
54d83fc7 172 t->verdict < 0) {
ba9dda3a
JK
173 /* Tail of chains: STANDARD target (return/policy) */
174 *comment = *chainname == hookname
4f2f6f23
JE
175 ? comments[NF_IP_TRACE_COMMENT_POLICY]
176 : comments[NF_IP_TRACE_COMMENT_RETURN];
ba9dda3a
JK
177 }
178 return 1;
179 } else
180 (*rulenum)++;
181
182 return 0;
183}
184
9dff2c96
EB
185static void trace_packet(struct net *net,
186 const struct sk_buff *skb,
ba9dda3a
JK
187 unsigned int hook,
188 const struct net_device *in,
189 const struct net_device *out,
ecb6f85e 190 const char *tablename,
d5d1baa1
JE
191 const struct xt_table_info *private,
192 const struct ipt_entry *e)
ba9dda3a 193{
5452e425 194 const struct ipt_entry *root;
4f2f6f23 195 const char *hookname, *chainname, *comment;
72b2b1dd 196 const struct ipt_entry *iter;
ba9dda3a
JK
197 unsigned int rulenum = 0;
198
482cfc31 199 root = get_entry(private->entries, private->hook_entry[hook]);
ba9dda3a 200
4f2f6f23
JE
201 hookname = chainname = hooknames[hook];
202 comment = comments[NF_IP_TRACE_COMMENT_RULE];
ba9dda3a 203
72b2b1dd
JE
204 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
205 if (get_chainname_rulenum(iter, e, hookname,
206 &chainname, &comment, &rulenum) != 0)
207 break;
ba9dda3a 208
4017a7ee
PNA
209 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
210 "TRACE: %s:%s:%s:%u ",
211 tablename, chainname, comment, rulenum);
ba9dda3a
JK
212}
213#endif
214
6c7941de 215static inline
98e86403
JE
216struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
217{
218 return (void *)entry + entry->next_offset;
219}
220
1da177e4
LT
221/* Returns one of the generic firewall policies, like NF_ACCEPT. */
222unsigned int
8844e010
FW
223ipt_do_table(void *priv,
224 struct sk_buff *skb,
225 const struct nf_hook_state *state)
1da177e4 226{
8844e010 227 const struct xt_table *table = priv;
6cb8ff3f 228 unsigned int hook = state->hook;
1da177e4 229 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
5452e425 230 const struct iphdr *ip;
1da177e4
LT
231 /* Initializing verdict to NF_DROP keeps gcc happy. */
232 unsigned int verdict = NF_DROP;
233 const char *indev, *outdev;
d5d1baa1 234 const void *table_base;
f3c5c1bf 235 struct ipt_entry *e, **jumpstack;
7814b6ec 236 unsigned int stackidx, cpu;
d5d1baa1 237 const struct xt_table_info *private;
de74c169 238 struct xt_action_param acpar;
7f5c6d4f 239 unsigned int addend;
1da177e4
LT
240
241 /* Initialization */
7814b6ec 242 stackidx = 0;
3db05fea 243 ip = ip_hdr(skb);
1c491ba2
DM
244 indev = state->in ? state->in->name : nulldevname;
245 outdev = state->out ? state->out->name : nulldevname;
1da177e4
LT
246 /* We handle fragments by dealing with the first fragment as
247 * if it was a normal packet. All other fragments are treated
248 * normally, except that they will NEVER match rules that ask
249 * things we don't know, ie. tcp syn flag or ports). If the
250 * rule is also a fragment-specific rule, non-fragments won't
251 * match it. */
de74c169
JE
252 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
253 acpar.thoff = ip_hdrlen(skb);
b4ba2611 254 acpar.hotdrop = false;
613dbd95 255 acpar.state = state;
1da177e4 256
9efdb14f 257 WARN_ON(!(table->valid_hooks & (1 << hook)));
7f5c6d4f
ED
258 local_bh_disable();
259 addend = xt_write_recseq_begin();
d3d40f23 260 private = READ_ONCE(table->private); /* Address dependency. */
f3c5c1bf 261 cpu = smp_processor_id();
482cfc31 262 table_base = private->entries;
f3c5c1bf 263 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
7814b6ec
FW
264
265 /* Switch to alternate jumpstack if we're being invoked via TEE.
266 * TEE issues XT_CONTINUE verdict on original skb so we must not
267 * clobber the jumpstack.
268 *
269 * For recursion via REJECT or SYNPROXY the stack will be clobbered
270 * but it is no problem since absolute verdict is issued by these.
271 */
dcebd315
FW
272 if (static_key_false(&xt_tee_enabled))
273 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
78454473 274
2e4e6a17 275 e = get_entry(table_base, private->hook_entry[hook]);
1da177e4 276
1da177e4 277 do {
87a2e70d 278 const struct xt_entry_target *t;
dcea992a 279 const struct xt_entry_match *ematch;
71ae0dff 280 struct xt_counters *counter;
a1ff4ac8 281
9efdb14f 282 WARN_ON(!e);
a1ff4ac8 283 if (!ip_packet_match(ip, indev, outdev,
de74c169 284 &e->ip, acpar.fragoff)) {
dcea992a 285 no_match:
a1ff4ac8
JE
286 e = ipt_next_entry(e);
287 continue;
288 }
1da177e4 289
ef53d702 290 xt_ematch_foreach(ematch, e) {
de74c169
JE
291 acpar.match = ematch->u.kernel.match;
292 acpar.matchinfo = ematch->data;
293 if (!acpar.match->match(skb, &acpar))
dcea992a 294 goto no_match;
ef53d702 295 }
dcea992a 296
71ae0dff
FW
297 counter = xt_get_this_cpu_counter(&e->counters);
298 ADD_COUNTER(*counter, skb->len, 1);
1da177e4 299
dc3c09d3 300 t = ipt_get_target_c(e);
9efdb14f 301 WARN_ON(!t->u.kernel.target);
ba9dda3a 302
f0165888 303#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
a1ff4ac8
JE
304 /* The packet is traced: log it */
305 if (unlikely(skb->nf_trace))
9dff2c96
EB
306 trace_packet(state->net, skb, hook, state->in,
307 state->out, table->name, private, e);
ba9dda3a 308#endif
a1ff4ac8
JE
309 /* Standard target? */
310 if (!t->u.kernel.target->target) {
311 int v;
312
87a2e70d 313 v = ((struct xt_standard_target *)t)->verdict;
a1ff4ac8
JE
314 if (v < 0) {
315 /* Pop from stack? */
243bf6e2 316 if (v != XT_RETURN) {
95c96174 317 verdict = (unsigned int)(-v) - 1;
a1ff4ac8 318 break;
1da177e4 319 }
7814b6ec 320 if (stackidx == 0) {
f3c5c1bf
JE
321 e = get_entry(table_base,
322 private->underflow[hook]);
f3c5c1bf 323 } else {
7814b6ec 324 e = jumpstack[--stackidx];
f3c5c1bf
JE
325 e = ipt_next_entry(e);
326 }
a1ff4ac8
JE
327 continue;
328 }
3666ed1c 329 if (table_base + v != ipt_next_entry(e) &&
57ebd808
FW
330 !(e->ip.flags & IPT_F_GOTO)) {
331 if (unlikely(stackidx >= private->stacksize)) {
332 verdict = NF_DROP;
333 break;
334 }
7814b6ec 335 jumpstack[stackidx++] = e;
57ebd808 336 }
1da177e4 337
a1ff4ac8 338 e = get_entry(table_base, v);
7a6b1c46
JE
339 continue;
340 }
341
de74c169
JE
342 acpar.target = t->u.kernel.target;
343 acpar.targinfo = t->data;
bb70dfa5 344
de74c169 345 verdict = t->u.kernel.target->target(skb, &acpar);
9beceb54
TY
346 if (verdict == XT_CONTINUE) {
347 /* Target might have changed stuff. */
348 ip = ip_hdr(skb);
7a6b1c46 349 e = ipt_next_entry(e);
9beceb54 350 } else {
7a6b1c46
JE
351 /* Verdict */
352 break;
9beceb54 353 }
b4ba2611 354 } while (!acpar.hotdrop);
7814b6ec 355
24cebe3f
IM
356 xt_write_recseq_end(addend);
357 local_bh_enable();
7f5c6d4f 358
b4ba2611 359 if (acpar.hotdrop)
1da177e4
LT
360 return NF_DROP;
361 else return verdict;
1da177e4
LT
362}
363
1da177e4 364/* Figures out from what hook each rule can be called: returns 0 if
98dbbfc3 365 there are loops. Puts hook bitmask in comefrom. */
1da177e4 366static int
98dbbfc3 367mark_source_chains(const struct xt_table_info *newinfo,
f4dc7771
FW
368 unsigned int valid_hooks, void *entry0,
369 unsigned int *offsets)
1da177e4
LT
370{
371 unsigned int hook;
372
373 /* No recursion; use packet counter to save back ptrs (reset
374 to 0 as we leave), and comefrom to save source hook bitmask */
6e23ae2a 375 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
1da177e4 376 unsigned int pos = newinfo->hook_entry[hook];
68ad546a 377 struct ipt_entry *e = entry0 + pos;
1da177e4
LT
378
379 if (!(valid_hooks & (1 << hook)))
380 continue;
381
382 /* Set initial back pointer. */
383 e->counters.pcnt = pos;
384
385 for (;;) {
87a2e70d 386 const struct xt_standard_target *t
d5d1baa1 387 = (void *)ipt_get_target_c(e);
e1b4b9f3 388 int visited = e->comefrom & (1 << hook);
1da177e4 389
d7cdf816 390 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
1da177e4 391 return 0;
d7cdf816 392
9c547959 393 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
1da177e4
LT
394
395 /* Unconditional return/END. */
54d83fc7 396 if ((unconditional(e) &&
3666ed1c 397 (strcmp(t->target.u.user.name,
243bf6e2 398 XT_STANDARD_TARGET) == 0) &&
54d83fc7 399 t->verdict < 0) || visited) {
1da177e4
LT
400 unsigned int oldpos, size;
401
402 /* Return: backtrack through the last
403 big jump. */
404 do {
6e23ae2a 405 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
1da177e4
LT
406 oldpos = pos;
407 pos = e->counters.pcnt;
408 e->counters.pcnt = 0;
409
410 /* We're at the start. */
411 if (pos == oldpos)
412 goto next;
413
68ad546a 414 e = entry0 + pos;
1da177e4
LT
415 } while (oldpos == pos + e->next_offset);
416
417 /* Move along one */
418 size = e->next_offset;
68ad546a 419 e = entry0 + pos + size;
f24e230d
FW
420 if (pos + size >= newinfo->size)
421 return 0;
1da177e4
LT
422 e->counters.pcnt = pos;
423 pos += size;
424 } else {
425 int newpos = t->verdict;
426
427 if (strcmp(t->target.u.user.name,
243bf6e2 428 XT_STANDARD_TARGET) == 0 &&
3666ed1c 429 newpos >= 0) {
1da177e4 430 /* This a jump; chase it. */
f4dc7771
FW
431 if (!xt_find_jump_offset(offsets, newpos,
432 newinfo->number))
433 return 0;
1da177e4
LT
434 } else {
435 /* ... this is a fallthru */
436 newpos = pos + e->next_offset;
f24e230d
FW
437 if (newpos >= newinfo->size)
438 return 0;
1da177e4 439 }
68ad546a 440 e = entry0 + newpos;
1da177e4
LT
441 e->counters.pcnt = pos;
442 pos = newpos;
443 }
444 }
d7cdf816 445next: ;
1da177e4
LT
446 }
447 return 1;
448}
449
87a2e70d 450static void cleanup_match(struct xt_entry_match *m, struct net *net)
1da177e4 451{
6be3d859
JE
452 struct xt_mtdtor_param par;
453
f54e9367 454 par.net = net;
6be3d859
JE
455 par.match = m->u.kernel.match;
456 par.matchinfo = m->data;
916a917d 457 par.family = NFPROTO_IPV4;
6be3d859
JE
458 if (par.match->destroy != NULL)
459 par.match->destroy(&par);
460 module_put(par.match->me);
1da177e4
LT
461}
462
022748a9 463static int
87a2e70d 464check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
a96be246 465{
9b4fce7a 466 const struct ipt_ip *ip = par->entryinfo;
a96be246 467
9b4fce7a
JE
468 par->match = m->u.kernel.match;
469 par->matchinfo = m->data;
470
d7cdf816
PNA
471 return xt_check_match(par, m->u.match_size - sizeof(*m),
472 ip->proto, ip->invflags & IPT_INV_PROTO);
a96be246
DM
473}
474
022748a9 475static int
87a2e70d 476find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
1da177e4 477{
6709dbbb 478 struct xt_match *match;
3cdc7c95 479 int ret;
1da177e4 480
fd0ec0e6
JE
481 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
482 m->u.user.revision);
d7cdf816 483 if (IS_ERR(match))
fd0ec0e6 484 return PTR_ERR(match);
1da177e4
LT
485 m->u.kernel.match = match;
486
6bdb331b 487 ret = check_match(m, par);
3cdc7c95
PM
488 if (ret)
489 goto err;
490
1da177e4 491 return 0;
3cdc7c95
PM
492err:
493 module_put(m->u.kernel.match->me);
494 return ret;
1da177e4
LT
495}
496
add67461 497static int check_target(struct ipt_entry *e, struct net *net, const char *name)
a96be246 498{
87a2e70d 499 struct xt_entry_target *t = ipt_get_target(e);
af5d6dc2 500 struct xt_tgchk_param par = {
add67461 501 .net = net,
af5d6dc2
JE
502 .table = name,
503 .entryinfo = e,
504 .target = t->u.kernel.target,
505 .targinfo = t->data,
506 .hook_mask = e->comefrom,
916a917d 507 .family = NFPROTO_IPV4,
af5d6dc2 508 };
a96be246 509
d7cdf816
PNA
510 return xt_check_target(&par, t->u.target_size - sizeof(*t),
511 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
a96be246 512}
1da177e4 513
022748a9 514static int
a83d8e8d 515find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
ae0ac0ed
FW
516 unsigned int size,
517 struct xt_percpu_counter_alloc_state *alloc_state)
1da177e4 518{
87a2e70d 519 struct xt_entry_target *t;
6709dbbb 520 struct xt_target *target;
1da177e4
LT
521 int ret;
522 unsigned int j;
9b4fce7a 523 struct xt_mtchk_param mtpar;
dcea992a 524 struct xt_entry_match *ematch;
1da177e4 525
ae0ac0ed 526 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
71ae0dff
FW
527 return -ENOMEM;
528
1da177e4 529 j = 0;
c568503e 530 memset(&mtpar, 0, sizeof(mtpar));
a83d8e8d 531 mtpar.net = net;
9b4fce7a
JE
532 mtpar.table = name;
533 mtpar.entryinfo = &e->ip;
534 mtpar.hook_mask = e->comefrom;
916a917d 535 mtpar.family = NFPROTO_IPV4;
dcea992a 536 xt_ematch_foreach(ematch, e) {
6bdb331b 537 ret = find_check_match(ematch, &mtpar);
dcea992a 538 if (ret != 0)
6bdb331b
JE
539 goto cleanup_matches;
540 ++j;
dcea992a 541 }
1da177e4
LT
542
543 t = ipt_get_target(e);
d2a7b6ba
JE
544 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
545 t->u.user.revision);
546 if (IS_ERR(target)) {
d2a7b6ba 547 ret = PTR_ERR(target);
1da177e4
LT
548 goto cleanup_matches;
549 }
550 t->u.kernel.target = target;
551
add67461 552 ret = check_target(e, net, name);
3cdc7c95
PM
553 if (ret)
554 goto err;
71ae0dff 555
1da177e4 556 return 0;
3cdc7c95
PM
557 err:
558 module_put(t->u.kernel.target->me);
1da177e4 559 cleanup_matches:
6bdb331b
JE
560 xt_ematch_foreach(ematch, e) {
561 if (j-- == 0)
dcea992a 562 break;
6bdb331b
JE
563 cleanup_match(ematch, net);
564 }
71ae0dff 565
4d31eef5 566 xt_percpu_counter_free(&e->counters);
71ae0dff 567
1da177e4
LT
568 return ret;
569}
570
d5d1baa1 571static bool check_underflow(const struct ipt_entry *e)
e2fe35c1 572{
87a2e70d 573 const struct xt_entry_target *t;
e2fe35c1
JE
574 unsigned int verdict;
575
54d83fc7 576 if (!unconditional(e))
e2fe35c1 577 return false;
d5d1baa1 578 t = ipt_get_target_c(e);
e2fe35c1
JE
579 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
580 return false;
87a2e70d 581 verdict = ((struct xt_standard_target *)t)->verdict;
e2fe35c1
JE
582 verdict = -verdict - 1;
583 return verdict == NF_DROP || verdict == NF_ACCEPT;
584}
585
022748a9 586static int
1da177e4 587check_entry_size_and_hooks(struct ipt_entry *e,
2e4e6a17 588 struct xt_table_info *newinfo,
d5d1baa1
JE
589 const unsigned char *base,
590 const unsigned char *limit,
1da177e4
LT
591 const unsigned int *hook_entries,
592 const unsigned int *underflows,
0559518b 593 unsigned int valid_hooks)
1da177e4
LT
594{
595 unsigned int h;
bdf533de 596 int err;
1da177e4 597
3666ed1c 598 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
6e94e0cf 599 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
d7cdf816 600 (unsigned char *)e + e->next_offset > limit)
1da177e4 601 return -EINVAL;
1da177e4
LT
602
603 if (e->next_offset
d7cdf816 604 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
1da177e4 605 return -EINVAL;
1da177e4 606
aa412ba2
FW
607 if (!ip_checkentry(&e->ip))
608 return -EINVAL;
609
ce683e5f
FW
610 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
611 e->next_offset);
bdf533de
FW
612 if (err)
613 return err;
614
1da177e4 615 /* Check hooks & underflows */
6e23ae2a 616 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
a7d51738
JE
617 if (!(valid_hooks & (1 << h)))
618 continue;
1da177e4
LT
619 if ((unsigned char *)e - base == hook_entries[h])
620 newinfo->hook_entry[h] = hook_entries[h];
90e7d4ab 621 if ((unsigned char *)e - base == underflows[h]) {
d7cdf816 622 if (!check_underflow(e))
90e7d4ab 623 return -EINVAL;
d7cdf816 624
1da177e4 625 newinfo->underflow[h] = underflows[h];
90e7d4ab 626 }
1da177e4
LT
627 }
628
1da177e4 629 /* Clear counters and comefrom */
2e4e6a17 630 e->counters = ((struct xt_counters) { 0, 0 });
1da177e4 631 e->comefrom = 0;
1da177e4
LT
632 return 0;
633}
634
0559518b
JE
635static void
636cleanup_entry(struct ipt_entry *e, struct net *net)
1da177e4 637{
a2df1648 638 struct xt_tgdtor_param par;
87a2e70d 639 struct xt_entry_target *t;
dcea992a 640 struct xt_entry_match *ematch;
1da177e4 641
1da177e4 642 /* Cleanup all matches */
dcea992a 643 xt_ematch_foreach(ematch, e)
6bdb331b 644 cleanup_match(ematch, net);
1da177e4 645 t = ipt_get_target(e);
a2df1648 646
add67461 647 par.net = net;
a2df1648
JE
648 par.target = t->u.kernel.target;
649 par.targinfo = t->data;
916a917d 650 par.family = NFPROTO_IPV4;
a2df1648
JE
651 if (par.target->destroy != NULL)
652 par.target->destroy(&par);
653 module_put(par.target->me);
4d31eef5 654 xt_percpu_counter_free(&e->counters);
1da177e4
LT
655}
656
657/* Checks and translates the user-supplied table segment (held in
658 newinfo) */
659static int
0f234214 660translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
6c28255b 661 const struct ipt_replace *repl)
1da177e4 662{
ae0ac0ed 663 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
72b2b1dd 664 struct ipt_entry *iter;
f4dc7771 665 unsigned int *offsets;
1da177e4 666 unsigned int i;
72b2b1dd 667 int ret = 0;
1da177e4 668
0f234214
JE
669 newinfo->size = repl->size;
670 newinfo->number = repl->num_entries;
1da177e4
LT
671
672 /* Init all hooks to impossible value. */
6e23ae2a 673 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1da177e4
LT
674 newinfo->hook_entry[i] = 0xFFFFFFFF;
675 newinfo->underflow[i] = 0xFFFFFFFF;
676 }
677
f4dc7771
FW
678 offsets = xt_alloc_entry_offsets(newinfo->number);
679 if (!offsets)
680 return -ENOMEM;
1da177e4
LT
681 i = 0;
682 /* Walk through entries, checking offsets. */
72b2b1dd
JE
683 xt_entry_foreach(iter, entry0, newinfo->size) {
684 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
6b4ff2d7
JE
685 entry0 + repl->size,
686 repl->hook_entry,
687 repl->underflow,
688 repl->valid_hooks);
72b2b1dd 689 if (ret != 0)
f4dc7771
FW
690 goto out_free;
691 if (i < repl->num_entries)
692 offsets[i] = (void *)iter - entry0;
0559518b 693 ++i;
98dbbfc3
FW
694 if (strcmp(ipt_get_target(iter)->u.user.name,
695 XT_ERROR_TARGET) == 0)
696 ++newinfo->stacksize;
72b2b1dd 697 }
1da177e4 698
f4dc7771 699 ret = -EINVAL;
d7cdf816 700 if (i != repl->num_entries)
f4dc7771 701 goto out_free;
1da177e4 702
1b293e30
FW
703 ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
704 if (ret)
705 goto out_free;
1da177e4 706
f4dc7771
FW
707 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
708 ret = -ELOOP;
709 goto out_free;
710 }
711 kvfree(offsets);
74c9c0c1 712
1da177e4
LT
713 /* Finally, each sanity check must pass */
714 i = 0;
72b2b1dd 715 xt_entry_foreach(iter, entry0, newinfo->size) {
ae0ac0ed
FW
716 ret = find_check_entry(iter, net, repl->name, repl->size,
717 &alloc_state);
72b2b1dd
JE
718 if (ret != 0)
719 break;
0559518b 720 ++i;
72b2b1dd 721 }
1da177e4 722
74c9c0c1 723 if (ret != 0) {
0559518b
JE
724 xt_entry_foreach(iter, entry0, newinfo->size) {
725 if (i-- == 0)
72b2b1dd 726 break;
0559518b
JE
727 cleanup_entry(iter, net);
728 }
74c9c0c1
DM
729 return ret;
730 }
1da177e4 731
f4dc7771
FW
732 return ret;
733 out_free:
734 kvfree(offsets);
1da177e4
LT
735 return ret;
736}
737
1da177e4 738static void
2e4e6a17
HW
739get_counters(const struct xt_table_info *t,
740 struct xt_counters counters[])
1da177e4 741{
72b2b1dd 742 struct ipt_entry *iter;
1da177e4
LT
743 unsigned int cpu;
744 unsigned int i;
745
6f912042 746 for_each_possible_cpu(cpu) {
7f5c6d4f 747 seqcount_t *s = &per_cpu(xt_recseq, cpu);
83723d60 748
1da177e4 749 i = 0;
482cfc31 750 xt_entry_foreach(iter, t->entries, t->size) {
71ae0dff 751 struct xt_counters *tmp;
83723d60
ED
752 u64 bcnt, pcnt;
753 unsigned int start;
754
71ae0dff 755 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
83723d60 756 do {
7f5c6d4f 757 start = read_seqcount_begin(s);
71ae0dff
FW
758 bcnt = tmp->bcnt;
759 pcnt = tmp->pcnt;
7f5c6d4f 760 } while (read_seqcount_retry(s, start));
83723d60
ED
761
762 ADD_COUNTER(counters[i], bcnt, pcnt);
0559518b 763 ++i; /* macro does multi eval of i */
a5d7a714 764 cond_resched();
0559518b 765 }
1da177e4 766 }
78454473
SH
767}
768
d13e7b2e
FW
769static void get_old_counters(const struct xt_table_info *t,
770 struct xt_counters counters[])
771{
772 struct ipt_entry *iter;
773 unsigned int cpu, i;
774
775 for_each_possible_cpu(cpu) {
776 i = 0;
777 xt_entry_foreach(iter, t->entries, t->size) {
778 const struct xt_counters *tmp;
779
780 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
781 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
782 ++i; /* macro does multi eval of i */
783 }
784
785 cond_resched();
786 }
787}
788
d5d1baa1 789static struct xt_counters *alloc_counters(const struct xt_table *table)
1da177e4 790{
2722971c 791 unsigned int countersize;
2e4e6a17 792 struct xt_counters *counters;
d3d40f23 793 const struct xt_table_info *private = table->private;
1da177e4
LT
794
795 /* We need atomic snapshot of counters: rest doesn't change
796 (other than comefrom, which userspace doesn't care
797 about). */
2e4e6a17 798 countersize = sizeof(struct xt_counters) * private->number;
83723d60 799 counters = vzalloc(countersize);
1da177e4
LT
800
801 if (counters == NULL)
942e4a2b 802 return ERR_PTR(-ENOMEM);
78454473 803
942e4a2b 804 get_counters(private, counters);
1da177e4 805
2722971c
DM
806 return counters;
807}
808
809static int
810copy_entries_to_user(unsigned int total_size,
d5d1baa1 811 const struct xt_table *table,
2722971c
DM
812 void __user *userptr)
813{
814 unsigned int off, num;
d5d1baa1 815 const struct ipt_entry *e;
2722971c 816 struct xt_counters *counters;
d3d40f23 817 const struct xt_table_info *private = table->private;
2722971c 818 int ret = 0;
711bdde6 819 const void *loc_cpu_entry;
2722971c
DM
820
821 counters = alloc_counters(table);
822 if (IS_ERR(counters))
823 return PTR_ERR(counters);
824
482cfc31 825 loc_cpu_entry = private->entries;
1da177e4
LT
826
827 /* FIXME: use iterator macros --RR */
828 /* ... then go back and fix counters and names */
829 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
830 unsigned int i;
87a2e70d
JE
831 const struct xt_entry_match *m;
832 const struct xt_entry_target *t;
1da177e4 833
68ad546a 834 e = loc_cpu_entry + off;
f77bc5b2
WB
835 if (copy_to_user(userptr + off, e, sizeof(*e))) {
836 ret = -EFAULT;
837 goto free_counters;
838 }
1da177e4
LT
839 if (copy_to_user(userptr + off
840 + offsetof(struct ipt_entry, counters),
841 &counters[num],
842 sizeof(counters[num])) != 0) {
843 ret = -EFAULT;
844 goto free_counters;
845 }
846
847 for (i = sizeof(struct ipt_entry);
848 i < e->target_offset;
849 i += m->u.match_size) {
850 m = (void *)e + i;
851
f77bc5b2 852 if (xt_match_to_user(m, userptr + off + i)) {
1da177e4
LT
853 ret = -EFAULT;
854 goto free_counters;
855 }
856 }
857
d5d1baa1 858 t = ipt_get_target_c(e);
f77bc5b2 859 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
1da177e4
LT
860 ret = -EFAULT;
861 goto free_counters;
862 }
863 }
864
865 free_counters:
866 vfree(counters);
867 return ret;
868}
869
47a6959f 870#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
739674fb 871static void compat_standard_from_user(void *dst, const void *src)
2722971c 872{
9fa492cd 873 int v = *(compat_int_t *)src;
2722971c 874
9fa492cd 875 if (v > 0)
b386d9f5 876 v += xt_compat_calc_jump(AF_INET, v);
9fa492cd
PM
877 memcpy(dst, &v, sizeof(v));
878}
46c5ea3c 879
739674fb 880static int compat_standard_to_user(void __user *dst, const void *src)
2722971c 881{
9fa492cd 882 compat_int_t cv = *(int *)src;
2722971c 883
9fa492cd 884 if (cv > 0)
b386d9f5 885 cv -= xt_compat_calc_jump(AF_INET, cv);
9fa492cd 886 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
2722971c
DM
887}
888
d5d1baa1 889static int compat_calc_entry(const struct ipt_entry *e,
4b478248 890 const struct xt_table_info *info,
d5d1baa1 891 const void *base, struct xt_table_info *newinfo)
2722971c 892{
dcea992a 893 const struct xt_entry_match *ematch;
87a2e70d 894 const struct xt_entry_target *t;
e5b5ef7d 895 unsigned int entry_offset;
2722971c
DM
896 int off, i, ret;
897
30c08c41 898 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
2722971c 899 entry_offset = (void *)e - base;
dcea992a 900 xt_ematch_foreach(ematch, e)
6bdb331b 901 off += xt_compat_match_offset(ematch->u.kernel.match);
d5d1baa1 902 t = ipt_get_target_c(e);
9fa492cd 903 off += xt_compat_target_offset(t->u.kernel.target);
2722971c 904 newinfo->size -= off;
b386d9f5 905 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
2722971c
DM
906 if (ret)
907 return ret;
908
6e23ae2a 909 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
4b478248
PM
910 if (info->hook_entry[i] &&
911 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
2722971c 912 newinfo->hook_entry[i] -= off;
4b478248
PM
913 if (info->underflow[i] &&
914 (e < (struct ipt_entry *)(base + info->underflow[i])))
2722971c
DM
915 newinfo->underflow[i] -= off;
916 }
917 return 0;
918}
919
259d4e41 920static int compat_table_info(const struct xt_table_info *info,
4b478248 921 struct xt_table_info *newinfo)
2722971c 922{
72b2b1dd 923 struct ipt_entry *iter;
711bdde6 924 const void *loc_cpu_entry;
0559518b 925 int ret;
2722971c
DM
926
927 if (!newinfo || !info)
928 return -EINVAL;
929
482cfc31 930 /* we dont care about newinfo->entries */
259d4e41
ED
931 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
932 newinfo->initial_entries = 0;
482cfc31 933 loc_cpu_entry = info->entries;
9782a11e
FW
934 ret = xt_compat_init_offsets(AF_INET, info->number);
935 if (ret)
936 return ret;
72b2b1dd
JE
937 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
938 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
939 if (ret != 0)
0559518b 940 return ret;
72b2b1dd 941 }
0559518b 942 return 0;
2722971c
DM
943}
944#endif
945
89c53c14 946static int get_info(struct net *net, void __user *user, const int *len)
2722971c 947{
12b00c2c 948 char name[XT_TABLE_MAXNAMELEN];
e60a13e0 949 struct xt_table *t;
2722971c
DM
950 int ret;
951
d7cdf816 952 if (*len != sizeof(struct ipt_getinfo))
2722971c 953 return -EINVAL;
2722971c
DM
954
955 if (copy_from_user(name, user, sizeof(name)) != 0)
956 return -EFAULT;
957
12b00c2c 958 name[XT_TABLE_MAXNAMELEN-1] = '\0';
47a6959f 959#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
89c53c14 960 if (in_compat_syscall())
2722971c
DM
961 xt_compat_lock(AF_INET);
962#endif
03d13b68
FW
963 t = xt_request_find_table_lock(net, AF_INET, name);
964 if (!IS_ERR(t)) {
2722971c 965 struct ipt_getinfo info;
d3d40f23 966 const struct xt_table_info *private = t->private;
47a6959f 967#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
14c7dbe0
AD
968 struct xt_table_info tmp;
969
89c53c14 970 if (in_compat_syscall()) {
2722971c 971 ret = compat_table_info(private, &tmp);
b386d9f5 972 xt_compat_flush_offsets(AF_INET);
4b478248 973 private = &tmp;
2722971c
DM
974 }
975#endif
b5f15ac4 976 memset(&info, 0, sizeof(info));
2722971c
DM
977 info.valid_hooks = t->valid_hooks;
978 memcpy(info.hook_entry, private->hook_entry,
4b478248 979 sizeof(info.hook_entry));
2722971c 980 memcpy(info.underflow, private->underflow,
4b478248 981 sizeof(info.underflow));
2722971c
DM
982 info.num_entries = private->number;
983 info.size = private->size;
984 strcpy(info.name, name);
985
986 if (copy_to_user(user, &info, *len) != 0)
987 ret = -EFAULT;
988 else
989 ret = 0;
990
991 xt_table_unlock(t);
992 module_put(t->me);
993 } else
03d13b68 994 ret = PTR_ERR(t);
47a6959f 995#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
89c53c14 996 if (in_compat_syscall())
2722971c
DM
997 xt_compat_unlock(AF_INET);
998#endif
999 return ret;
1000}
1001
1002static int
d5d1baa1
JE
1003get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1004 const int *len)
2722971c
DM
1005{
1006 int ret;
1007 struct ipt_get_entries get;
e60a13e0 1008 struct xt_table *t;
2722971c 1009
d7cdf816 1010 if (*len < sizeof(get))
2722971c 1011 return -EINVAL;
2722971c
DM
1012 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1013 return -EFAULT;
d7cdf816 1014 if (*len != sizeof(struct ipt_get_entries) + get.size)
2722971c 1015 return -EINVAL;
b301f253 1016 get.name[sizeof(get.name) - 1] = '\0';
2722971c 1017
34bd137b 1018 t = xt_find_table_lock(net, AF_INET, get.name);
03d13b68 1019 if (!IS_ERR(t)) {
d3d40f23 1020 const struct xt_table_info *private = t->private;
2722971c
DM
1021 if (get.size == private->size)
1022 ret = copy_entries_to_user(private->size,
1023 t, uptr->entrytable);
d7cdf816 1024 else
544473c1 1025 ret = -EAGAIN;
d7cdf816 1026
2722971c
DM
1027 module_put(t->me);
1028 xt_table_unlock(t);
1029 } else
03d13b68 1030 ret = PTR_ERR(t);
2722971c
DM
1031
1032 return ret;
1033}
1034
1035static int
34bd137b 1036__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
4b478248
PM
1037 struct xt_table_info *newinfo, unsigned int num_counters,
1038 void __user *counters_ptr)
2722971c
DM
1039{
1040 int ret;
e60a13e0 1041 struct xt_table *t;
2722971c
DM
1042 struct xt_table_info *oldinfo;
1043 struct xt_counters *counters;
72b2b1dd 1044 struct ipt_entry *iter;
2722971c 1045
c84ca954 1046 counters = xt_counters_alloc(num_counters);
2722971c
DM
1047 if (!counters) {
1048 ret = -ENOMEM;
1049 goto out;
1050 }
1051
03d13b68
FW
1052 t = xt_request_find_table_lock(net, AF_INET, name);
1053 if (IS_ERR(t)) {
1054 ret = PTR_ERR(t);
2722971c
DM
1055 goto free_newinfo_counters_untrans;
1056 }
1057
1058 /* You lied! */
1059 if (valid_hooks != t->valid_hooks) {
2722971c
DM
1060 ret = -EINVAL;
1061 goto put_module;
1062 }
1063
1064 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1065 if (!oldinfo)
1066 goto put_module;
1067
1068 /* Update module usage count based on number of rules */
2722971c
DM
1069 if ((oldinfo->number > oldinfo->initial_entries) ||
1070 (newinfo->number <= oldinfo->initial_entries))
1071 module_put(t->me);
1072 if ((oldinfo->number > oldinfo->initial_entries) &&
1073 (newinfo->number <= oldinfo->initial_entries))
1074 module_put(t->me);
1075
f31e5f1a
XL
1076 xt_table_unlock(t);
1077
d13e7b2e 1078 get_old_counters(oldinfo, counters);
942e4a2b 1079
2722971c 1080 /* Decrease module usage counts and free resource */
482cfc31 1081 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
0559518b 1082 cleanup_entry(iter, net);
72b2b1dd 1083
2722971c
DM
1084 xt_free_table_info(oldinfo);
1085 if (copy_to_user(counters_ptr, counters,
c58dd2dd
TG
1086 sizeof(struct xt_counters) * num_counters) != 0) {
1087 /* Silent error, can't fail, new table is already in place */
1088 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1089 }
2722971c 1090 vfree(counters);
e58a171d 1091 return 0;
2722971c
DM
1092
1093 put_module:
1094 module_put(t->me);
1095 xt_table_unlock(t);
1096 free_newinfo_counters_untrans:
1097 vfree(counters);
1098 out:
1099 return ret;
1100}
1101
1102static int
c2f12630 1103do_replace(struct net *net, sockptr_t arg, unsigned int len)
2722971c
DM
1104{
1105 int ret;
1106 struct ipt_replace tmp;
1107 struct xt_table_info *newinfo;
1108 void *loc_cpu_entry;
72b2b1dd 1109 struct ipt_entry *iter;
2722971c 1110
0c83842d
ED
1111 if (len < sizeof(tmp))
1112 return -EINVAL;
c2f12630 1113 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
2722971c
DM
1114 return -EFAULT;
1115
2722971c 1116 /* overflow check */
2722971c
DM
1117 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1118 return -ENOMEM;
1086bbe9
DJ
1119 if (tmp.num_counters == 0)
1120 return -EINVAL;
65acf6e0
ED
1121 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1122 return -EINVAL;
1086bbe9 1123
78b79876 1124 tmp.name[sizeof(tmp.name)-1] = 0;
2722971c
DM
1125
1126 newinfo = xt_alloc_table_info(tmp.size);
1127 if (!newinfo)
1128 return -ENOMEM;
1129
482cfc31 1130 loc_cpu_entry = newinfo->entries;
d3c48151
CH
1131 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1132 tmp.size) != 0) {
2722971c
DM
1133 ret = -EFAULT;
1134 goto free_newinfo;
1135 }
1136
0f234214 1137 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
2722971c
DM
1138 if (ret != 0)
1139 goto free_newinfo;
1140
34bd137b 1141 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
4b478248 1142 tmp.num_counters, tmp.counters);
2722971c
DM
1143 if (ret)
1144 goto free_newinfo_untrans;
1145 return 0;
1146
1147 free_newinfo_untrans:
72b2b1dd 1148 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1149 cleanup_entry(iter, net);
2722971c
DM
1150 free_newinfo:
1151 xt_free_table_info(newinfo);
1152 return ret;
1153}
1154
2722971c 1155static int
ab214d1b 1156do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
2722971c 1157{
482cfc31 1158 unsigned int i;
2722971c
DM
1159 struct xt_counters_info tmp;
1160 struct xt_counters *paddc;
e60a13e0 1161 struct xt_table *t;
5452e425 1162 const struct xt_table_info *private;
2722971c 1163 int ret = 0;
72b2b1dd 1164 struct ipt_entry *iter;
7f5c6d4f 1165 unsigned int addend;
2722971c 1166
ab214d1b 1167 paddc = xt_copy_counters(arg, len, &tmp);
d7591f0c
FW
1168 if (IS_ERR(paddc))
1169 return PTR_ERR(paddc);
2722971c 1170
d7591f0c 1171 t = xt_find_table_lock(net, AF_INET, tmp.name);
03d13b68
FW
1172 if (IS_ERR(t)) {
1173 ret = PTR_ERR(t);
2722971c
DM
1174 goto free;
1175 }
1176
942e4a2b 1177 local_bh_disable();
d3d40f23 1178 private = t->private;
d7591f0c 1179 if (private->number != tmp.num_counters) {
2722971c
DM
1180 ret = -EINVAL;
1181 goto unlock_up_free;
1182 }
1183
1184 i = 0;
7f5c6d4f 1185 addend = xt_write_recseq_begin();
482cfc31 1186 xt_entry_foreach(iter, private->entries, private->size) {
71ae0dff
FW
1187 struct xt_counters *tmp;
1188
1189 tmp = xt_get_this_cpu_counter(&iter->counters);
1190 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
0559518b
JE
1191 ++i;
1192 }
7f5c6d4f 1193 xt_write_recseq_end(addend);
2722971c 1194 unlock_up_free:
942e4a2b 1195 local_bh_enable();
2722971c
DM
1196 xt_table_unlock(t);
1197 module_put(t->me);
1198 free:
1199 vfree(paddc);
1200
1201 return ret;
1202}
1203
47a6959f 1204#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2722971c 1205struct compat_ipt_replace {
12b00c2c 1206 char name[XT_TABLE_MAXNAMELEN];
2722971c
DM
1207 u32 valid_hooks;
1208 u32 num_entries;
1209 u32 size;
6e23ae2a
PM
1210 u32 hook_entry[NF_INET_NUMHOOKS];
1211 u32 underflow[NF_INET_NUMHOOKS];
2722971c 1212 u32 num_counters;
87a2e70d 1213 compat_uptr_t counters; /* struct xt_counters * */
6daf1414 1214 struct compat_ipt_entry entries[];
2722971c
DM
1215};
1216
a18aa31b
PM
1217static int
1218compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
b0a6363c 1219 unsigned int *size, struct xt_counters *counters,
0559518b 1220 unsigned int i)
2722971c 1221{
87a2e70d 1222 struct xt_entry_target *t;
2722971c
DM
1223 struct compat_ipt_entry __user *ce;
1224 u_int16_t target_offset, next_offset;
1225 compat_uint_t origsize;
dcea992a
JE
1226 const struct xt_entry_match *ematch;
1227 int ret = 0;
2722971c 1228
2722971c 1229 origsize = *size;
68ad546a 1230 ce = *dstptr;
0559518b
JE
1231 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1232 copy_to_user(&ce->counters, &counters[i],
1233 sizeof(counters[i])) != 0)
1234 return -EFAULT;
a18aa31b 1235
2722971c 1236 *dstptr += sizeof(struct compat_ipt_entry);
30c08c41
PM
1237 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1238
dcea992a
JE
1239 xt_ematch_foreach(ematch, e) {
1240 ret = xt_compat_match_to_user(ematch, dstptr, size);
1241 if (ret != 0)
6bdb331b 1242 return ret;
dcea992a 1243 }
2722971c 1244 target_offset = e->target_offset - (origsize - *size);
2722971c 1245 t = ipt_get_target(e);
9fa492cd 1246 ret = xt_compat_target_to_user(t, dstptr, size);
2722971c 1247 if (ret)
0559518b 1248 return ret;
2722971c 1249 next_offset = e->next_offset - (origsize - *size);
0559518b
JE
1250 if (put_user(target_offset, &ce->target_offset) != 0 ||
1251 put_user(next_offset, &ce->next_offset) != 0)
1252 return -EFAULT;
2722971c 1253 return 0;
2722971c
DM
1254}
1255
022748a9 1256static int
87a2e70d 1257compat_find_calc_match(struct xt_entry_match *m,
4b478248 1258 const struct ipt_ip *ip,
6bdb331b 1259 int *size)
2722971c 1260{
6709dbbb 1261 struct xt_match *match;
2722971c 1262
fd0ec0e6
JE
1263 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1264 m->u.user.revision);
d7cdf816 1265 if (IS_ERR(match))
fd0ec0e6 1266 return PTR_ERR(match);
d7cdf816 1267
2722971c 1268 m->u.kernel.match = match;
9fa492cd 1269 *size += xt_compat_match_offset(match);
4c1b52bc
DM
1270 return 0;
1271}
1272
0559518b 1273static void compat_release_entry(struct compat_ipt_entry *e)
4c1b52bc 1274{
87a2e70d 1275 struct xt_entry_target *t;
dcea992a 1276 struct xt_entry_match *ematch;
4c1b52bc 1277
4c1b52bc 1278 /* Cleanup all matches */
dcea992a 1279 xt_ematch_foreach(ematch, e)
6bdb331b 1280 module_put(ematch->u.kernel.match->me);
73cd598d 1281 t = compat_ipt_get_target(e);
4c1b52bc 1282 module_put(t->u.kernel.target->me);
4c1b52bc
DM
1283}
1284
022748a9 1285static int
73cd598d 1286check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
4b478248
PM
1287 struct xt_table_info *newinfo,
1288 unsigned int *size,
d5d1baa1 1289 const unsigned char *base,
09d96860 1290 const unsigned char *limit)
2722971c 1291{
dcea992a 1292 struct xt_entry_match *ematch;
87a2e70d 1293 struct xt_entry_target *t;
6709dbbb 1294 struct xt_target *target;
e5b5ef7d 1295 unsigned int entry_offset;
b0a6363c 1296 unsigned int j;
09d96860 1297 int ret, off;
2722971c 1298
3666ed1c 1299 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
6e94e0cf 1300 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
d7cdf816 1301 (unsigned char *)e + e->next_offset > limit)
2722971c 1302 return -EINVAL;
2722971c
DM
1303
1304 if (e->next_offset < sizeof(struct compat_ipt_entry) +
d7cdf816 1305 sizeof(struct compat_xt_entry_target))
2722971c 1306 return -EINVAL;
2722971c 1307
aa412ba2
FW
1308 if (!ip_checkentry(&e->ip))
1309 return -EINVAL;
1310
ce683e5f 1311 ret = xt_compat_check_entry_offsets(e, e->elems,
fc1221b3 1312 e->target_offset, e->next_offset);
a96be246
DM
1313 if (ret)
1314 return ret;
590bdf7f 1315
30c08c41 1316 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
2722971c
DM
1317 entry_offset = (void *)e - (void *)base;
1318 j = 0;
dcea992a 1319 xt_ematch_foreach(ematch, e) {
7d3f843e 1320 ret = compat_find_calc_match(ematch, &e->ip, &off);
dcea992a 1321 if (ret != 0)
6bdb331b
JE
1322 goto release_matches;
1323 ++j;
dcea992a 1324 }
2722971c 1325
73cd598d 1326 t = compat_ipt_get_target(e);
d2a7b6ba
JE
1327 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1328 t->u.user.revision);
1329 if (IS_ERR(target)) {
d2a7b6ba 1330 ret = PTR_ERR(target);
4c1b52bc 1331 goto release_matches;
2722971c
DM
1332 }
1333 t->u.kernel.target = target;
1334
9fa492cd 1335 off += xt_compat_target_offset(target);
2722971c 1336 *size += off;
b386d9f5 1337 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
2722971c
DM
1338 if (ret)
1339 goto out;
1340
2722971c 1341 return 0;
bec71b16 1342
2722971c 1343out:
bec71b16 1344 module_put(t->u.kernel.target->me);
4c1b52bc 1345release_matches:
6bdb331b
JE
1346 xt_ematch_foreach(ematch, e) {
1347 if (j-- == 0)
dcea992a 1348 break;
6bdb331b
JE
1349 module_put(ematch->u.kernel.match->me);
1350 }
2722971c
DM
1351 return ret;
1352}
1353
0188346f 1354static void
73cd598d 1355compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
7d3f843e 1356 unsigned int *size,
4b478248 1357 struct xt_table_info *newinfo, unsigned char *base)
2722971c 1358{
87a2e70d 1359 struct xt_entry_target *t;
2722971c
DM
1360 struct ipt_entry *de;
1361 unsigned int origsize;
0188346f 1362 int h;
dcea992a 1363 struct xt_entry_match *ematch;
2722971c 1364
2722971c 1365 origsize = *size;
68ad546a 1366 de = *dstptr;
2722971c 1367 memcpy(de, e, sizeof(struct ipt_entry));
73cd598d 1368 memcpy(&de->counters, &e->counters, sizeof(e->counters));
2722971c 1369
73cd598d 1370 *dstptr += sizeof(struct ipt_entry);
30c08c41
PM
1371 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1372
0188346f
FW
1373 xt_ematch_foreach(ematch, e)
1374 xt_compat_match_from_user(ematch, dstptr, size);
1375
2722971c 1376 de->target_offset = e->target_offset - (origsize - *size);
73cd598d 1377 t = compat_ipt_get_target(e);
9fa492cd 1378 xt_compat_target_from_user(t, dstptr, size);
2722971c
DM
1379
1380 de->next_offset = e->next_offset - (origsize - *size);
09d96860 1381
6e23ae2a 1382 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
2722971c
DM
1383 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1384 newinfo->hook_entry[h] -= origsize - *size;
1385 if ((unsigned char *)de - base < newinfo->underflow[h])
1386 newinfo->underflow[h] -= origsize - *size;
1387 }
f6677f43
DM
1388}
1389
1da177e4 1390static int
a83d8e8d 1391translate_compat_table(struct net *net,
4b478248
PM
1392 struct xt_table_info **pinfo,
1393 void **pentry0,
7d3f843e 1394 const struct compat_ipt_replace *compatr)
1da177e4 1395{
920b868a 1396 unsigned int i, j;
2722971c
DM
1397 struct xt_table_info *newinfo, *info;
1398 void *pos, *entry0, *entry1;
72b2b1dd 1399 struct compat_ipt_entry *iter0;
09d96860 1400 struct ipt_replace repl;
2722971c 1401 unsigned int size;
0559518b 1402 int ret;
1da177e4 1403
2722971c
DM
1404 info = *pinfo;
1405 entry0 = *pentry0;
7d3f843e
FW
1406 size = compatr->size;
1407 info->number = compatr->num_entries;
2722971c 1408
920b868a 1409 j = 0;
2722971c 1410 xt_compat_lock(AF_INET);
9782a11e
FW
1411 ret = xt_compat_init_offsets(AF_INET, compatr->num_entries);
1412 if (ret)
1413 goto out_unlock;
2722971c 1414 /* Walk through entries, checking offsets. */
7d3f843e 1415 xt_entry_foreach(iter0, entry0, compatr->size) {
72b2b1dd 1416 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
6b4ff2d7 1417 entry0,
09d96860 1418 entry0 + compatr->size);
72b2b1dd 1419 if (ret != 0)
0559518b
JE
1420 goto out_unlock;
1421 ++j;
72b2b1dd 1422 }
2722971c
DM
1423
1424 ret = -EINVAL;
d7cdf816 1425 if (j != compatr->num_entries)
2722971c 1426 goto out_unlock;
2722971c 1427
2722971c
DM
1428 ret = -ENOMEM;
1429 newinfo = xt_alloc_table_info(size);
1430 if (!newinfo)
1431 goto out_unlock;
1432
b29c457a
FW
1433 memset(newinfo->entries, 0, size);
1434
7d3f843e 1435 newinfo->number = compatr->num_entries;
6e23ae2a 1436 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
09d96860
FW
1437 newinfo->hook_entry[i] = compatr->hook_entry[i];
1438 newinfo->underflow[i] = compatr->underflow[i];
2722971c 1439 }
482cfc31 1440 entry1 = newinfo->entries;
2722971c 1441 pos = entry1;
7d3f843e 1442 size = compatr->size;
0188346f
FW
1443 xt_entry_foreach(iter0, entry0, compatr->size)
1444 compat_copy_entry_from_user(iter0, &pos, &size,
1445 newinfo, entry1);
1446
09d96860
FW
1447 /* all module references in entry0 are now gone.
1448 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1449 * generated by 64bit userspace.
1450 *
1451 * Call standard translate_table() to validate all hook_entrys,
1452 * underflows, check for loops, etc.
1453 */
b386d9f5 1454 xt_compat_flush_offsets(AF_INET);
2722971c 1455 xt_compat_unlock(AF_INET);
2722971c 1456
09d96860 1457 memcpy(&repl, compatr, sizeof(*compatr));
2722971c 1458
09d96860
FW
1459 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1460 repl.hook_entry[i] = newinfo->hook_entry[i];
1461 repl.underflow[i] = newinfo->underflow[i];
4c1b52bc 1462 }
f6677f43 1463
09d96860
FW
1464 repl.num_counters = 0;
1465 repl.counters = NULL;
1466 repl.size = newinfo->size;
1467 ret = translate_table(net, newinfo, entry1, &repl);
1468 if (ret)
1469 goto free_newinfo;
1470
2722971c
DM
1471 *pinfo = newinfo;
1472 *pentry0 = entry1;
1473 xt_free_table_info(info);
1474 return 0;
1da177e4 1475
2722971c
DM
1476free_newinfo:
1477 xt_free_table_info(newinfo);
09d96860
FW
1478 return ret;
1479out_unlock:
1480 xt_compat_flush_offsets(AF_INET);
1481 xt_compat_unlock(AF_INET);
7d3f843e 1482 xt_entry_foreach(iter0, entry0, compatr->size) {
0559518b 1483 if (j-- == 0)
72b2b1dd 1484 break;
0559518b
JE
1485 compat_release_entry(iter0);
1486 }
1da177e4
LT
1487 return ret;
1488}
1489
1490static int
c2f12630 1491compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
1da177e4
LT
1492{
1493 int ret;
2722971c
DM
1494 struct compat_ipt_replace tmp;
1495 struct xt_table_info *newinfo;
1496 void *loc_cpu_entry;
72b2b1dd 1497 struct ipt_entry *iter;
1da177e4 1498
0c83842d
ED
1499 if (len < sizeof(tmp))
1500 return -EINVAL;
c2f12630 1501 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1da177e4
LT
1502 return -EFAULT;
1503
ee4bb818 1504 /* overflow check */
ee4bb818
KK
1505 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1506 return -ENOMEM;
1086bbe9
DJ
1507 if (tmp.num_counters == 0)
1508 return -EINVAL;
65acf6e0
ED
1509 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1510 return -EINVAL;
1086bbe9 1511
78b79876 1512 tmp.name[sizeof(tmp.name)-1] = 0;
ee4bb818 1513
2e4e6a17 1514 newinfo = xt_alloc_table_info(tmp.size);
1da177e4
LT
1515 if (!newinfo)
1516 return -ENOMEM;
1517
482cfc31 1518 loc_cpu_entry = newinfo->entries;
d3c48151
CH
1519 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1520 tmp.size) != 0) {
1da177e4
LT
1521 ret = -EFAULT;
1522 goto free_newinfo;
1523 }
1524
7d3f843e 1525 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
2722971c 1526 if (ret != 0)
1da177e4 1527 goto free_newinfo;
1da177e4 1528
34bd137b 1529 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
4b478248 1530 tmp.num_counters, compat_ptr(tmp.counters));
2722971c
DM
1531 if (ret)
1532 goto free_newinfo_untrans;
1533 return 0;
1da177e4 1534
2722971c 1535 free_newinfo_untrans:
72b2b1dd 1536 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1537 cleanup_entry(iter, net);
2722971c
DM
1538 free_newinfo:
1539 xt_free_table_info(newinfo);
1540 return ret;
1541}
1da177e4 1542
4b478248 1543struct compat_ipt_get_entries {
12b00c2c 1544 char name[XT_TABLE_MAXNAMELEN];
2722971c 1545 compat_uint_t size;
6daf1414 1546 struct compat_ipt_entry entrytable[];
2722971c 1547};
1da177e4 1548
4b478248
PM
1549static int
1550compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1551 void __user *userptr)
2722971c 1552{
2722971c 1553 struct xt_counters *counters;
d3d40f23 1554 const struct xt_table_info *private = table->private;
2722971c
DM
1555 void __user *pos;
1556 unsigned int size;
1557 int ret = 0;
a18aa31b 1558 unsigned int i = 0;
72b2b1dd 1559 struct ipt_entry *iter;
1da177e4 1560
2722971c
DM
1561 counters = alloc_counters(table);
1562 if (IS_ERR(counters))
1563 return PTR_ERR(counters);
1564
2722971c
DM
1565 pos = userptr;
1566 size = total_size;
482cfc31 1567 xt_entry_foreach(iter, private->entries, total_size) {
72b2b1dd 1568 ret = compat_copy_entry_to_user(iter, &pos,
6b4ff2d7 1569 &size, counters, i++);
72b2b1dd
JE
1570 if (ret != 0)
1571 break;
1572 }
2722971c 1573
2722971c
DM
1574 vfree(counters);
1575 return ret;
1da177e4
LT
1576}
1577
1578static int
34bd137b
AD
1579compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1580 int *len)
1da177e4 1581{
2722971c
DM
1582 int ret;
1583 struct compat_ipt_get_entries get;
e60a13e0 1584 struct xt_table *t;
1da177e4 1585
d7cdf816 1586 if (*len < sizeof(get))
1da177e4
LT
1587 return -EINVAL;
1588
2722971c
DM
1589 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1590 return -EFAULT;
1da177e4 1591
d7cdf816 1592 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
2722971c 1593 return -EINVAL;
d7cdf816 1594
b301f253 1595 get.name[sizeof(get.name) - 1] = '\0';
1da177e4 1596
2722971c 1597 xt_compat_lock(AF_INET);
34bd137b 1598 t = xt_find_table_lock(net, AF_INET, get.name);
03d13b68 1599 if (!IS_ERR(t)) {
abe7034b 1600 const struct xt_table_info *private = t->private;
2722971c 1601 struct xt_table_info info;
2722971c 1602 ret = compat_table_info(private, &info);
d7cdf816 1603 if (!ret && get.size == info.size)
2722971c 1604 ret = compat_copy_entries_to_user(private->size,
4b478248 1605 t, uptr->entrytable);
d7cdf816 1606 else if (!ret)
544473c1 1607 ret = -EAGAIN;
d7cdf816 1608
b386d9f5 1609 xt_compat_flush_offsets(AF_INET);
2722971c
DM
1610 module_put(t->me);
1611 xt_table_unlock(t);
1612 } else
03d13b68 1613 ret = PTR_ERR(t);
1da177e4 1614
2722971c
DM
1615 xt_compat_unlock(AF_INET);
1616 return ret;
1617}
2722971c 1618#endif
1da177e4
LT
1619
1620static int
c2f12630 1621do_ipt_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len)
1da177e4
LT
1622{
1623 int ret;
1624
52e804c6 1625 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1626 return -EPERM;
1627
1628 switch (cmd) {
1629 case IPT_SO_SET_REPLACE:
47a6959f 1630#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
89c53c14 1631 if (in_compat_syscall())
c2f12630 1632 ret = compat_do_replace(sock_net(sk), arg, len);
89c53c14
CH
1633 else
1634#endif
c2f12630 1635 ret = do_replace(sock_net(sk), arg, len);
1da177e4
LT
1636 break;
1637
1638 case IPT_SO_SET_ADD_COUNTERS:
c2f12630 1639 ret = do_add_counters(sock_net(sk), arg, len);
1da177e4
LT
1640 break;
1641
1642 default:
1da177e4
LT
1643 ret = -EINVAL;
1644 }
1645
1646 return ret;
1647}
1648
1649static int
1650do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1651{
1652 int ret;
1653
52e804c6 1654 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1655 return -EPERM;
1656
1657 switch (cmd) {
2722971c 1658 case IPT_SO_GET_INFO:
89c53c14 1659 ret = get_info(sock_net(sk), user, len);
2722971c 1660 break;
1da177e4 1661
2722971c 1662 case IPT_SO_GET_ENTRIES:
47a6959f 1663#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
89c53c14
CH
1664 if (in_compat_syscall())
1665 ret = compat_get_entries(sock_net(sk), user, len);
1666 else
1667#endif
1668 ret = get_entries(sock_net(sk), user, len);
1da177e4 1669 break;
1da177e4
LT
1670
1671 case IPT_SO_GET_REVISION_MATCH:
1672 case IPT_SO_GET_REVISION_TARGET: {
12b00c2c 1673 struct xt_get_revision rev;
2e4e6a17 1674 int target;
1da177e4
LT
1675
1676 if (*len != sizeof(rev)) {
1677 ret = -EINVAL;
1678 break;
1679 }
1680 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1681 ret = -EFAULT;
1682 break;
1683 }
78b79876 1684 rev.name[sizeof(rev.name)-1] = 0;
1da177e4
LT
1685
1686 if (cmd == IPT_SO_GET_REVISION_TARGET)
2e4e6a17 1687 target = 1;
1da177e4 1688 else
2e4e6a17 1689 target = 0;
1da177e4 1690
2e4e6a17
HW
1691 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1692 rev.revision,
1693 target, &ret),
1da177e4
LT
1694 "ipt_%s", rev.name);
1695 break;
1696 }
1697
1698 default:
1da177e4
LT
1699 ret = -EINVAL;
1700 }
1701
1702 return ret;
1703}
1704
b9e69e12
FW
1705static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1706{
1707 struct xt_table_info *private;
1708 void *loc_cpu_entry;
1709 struct module *table_owner = table->me;
1710 struct ipt_entry *iter;
1711
1712 private = xt_unregister_table(table);
1713
1714 /* Decrease module usage counts and free resources */
1715 loc_cpu_entry = private->entries;
1716 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1717 cleanup_entry(iter, net);
1718 if (private->number > private->initial_entries)
1719 module_put(table_owner);
1720 xt_free_table_info(private);
1721}
1722
a67dd266
FW
1723int ipt_register_table(struct net *net, const struct xt_table *table,
1724 const struct ipt_replace *repl,
ae689334 1725 const struct nf_hook_ops *template_ops)
1da177e4 1726{
ae689334
FW
1727 struct nf_hook_ops *ops;
1728 unsigned int num_ops;
1729 int ret, i;
2e4e6a17 1730 struct xt_table_info *newinfo;
f3c5c1bf 1731 struct xt_table_info bootstrap = {0};
31836064 1732 void *loc_cpu_entry;
a98da11d 1733 struct xt_table *new_table;
1da177e4 1734
2e4e6a17 1735 newinfo = xt_alloc_table_info(repl->size);
a67dd266
FW
1736 if (!newinfo)
1737 return -ENOMEM;
1da177e4 1738
482cfc31 1739 loc_cpu_entry = newinfo->entries;
31836064 1740 memcpy(loc_cpu_entry, repl->entries, repl->size);
1da177e4 1741
0f234214 1742 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
ae689334
FW
1743 if (ret != 0) {
1744 xt_free_table_info(newinfo);
1745 return ret;
1746 }
1da177e4 1747
44d34e72 1748 new_table = xt_register_table(net, table, &bootstrap, newinfo);
a98da11d 1749 if (IS_ERR(new_table)) {
0af8c09c
PT
1750 struct ipt_entry *iter;
1751
1752 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1753 cleanup_entry(iter, net);
ae689334
FW
1754 xt_free_table_info(newinfo);
1755 return PTR_ERR(new_table);
1da177e4
LT
1756 }
1757
ae689334
FW
1758 /* No template? No need to do anything. This is used by 'nat' table, it registers
1759 * with the nat core instead of the netfilter core.
1760 */
1761 if (!template_ops)
ba7d284a 1762 return 0;
b9e69e12 1763
ae689334
FW
1764 num_ops = hweight32(table->valid_hooks);
1765 if (num_ops == 0) {
1766 ret = -EINVAL;
1767 goto out_free;
b9e69e12
FW
1768 }
1769
ae689334
FW
1770 ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
1771 if (!ops) {
1772 ret = -ENOMEM;
1773 goto out_free;
1774 }
1775
1776 for (i = 0; i < num_ops; i++)
1777 ops[i].priv = new_table;
1778
1779 new_table->ops = ops;
1780
1781 ret = nf_register_net_hooks(net, ops, num_ops);
1782 if (ret != 0)
1783 goto out_free;
1784
a67dd266 1785 return ret;
44d34e72
AD
1786
1787out_free:
ae689334 1788 __ipt_unregister_table(net, new_table);
a67dd266 1789 return ret;
1da177e4
LT
1790}
1791
ae689334 1792void ipt_unregister_table_pre_exit(struct net *net, const char *name)
1cbf9098 1793{
20a9df33
FW
1794 struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
1795
1796 if (table)
ae689334 1797 nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
1cbf9098
DW
1798}
1799
20a9df33 1800void ipt_unregister_table_exit(struct net *net, const char *name)
1cbf9098 1801{
20a9df33
FW
1802 struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
1803
1804 if (table)
1805 __ipt_unregister_table(net, table);
1cbf9098
DW
1806}
1807
4538506b
JE
1808static struct xt_target ipt_builtin_tg[] __read_mostly = {
1809 {
243bf6e2 1810 .name = XT_STANDARD_TARGET,
4538506b
JE
1811 .targetsize = sizeof(int),
1812 .family = NFPROTO_IPV4,
47a6959f 1813#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
4538506b
JE
1814 .compatsize = sizeof(compat_int_t),
1815 .compat_from_user = compat_standard_from_user,
1816 .compat_to_user = compat_standard_to_user,
2722971c 1817#endif
4538506b
JE
1818 },
1819 {
243bf6e2 1820 .name = XT_ERROR_TARGET,
4538506b 1821 .target = ipt_error,
12b00c2c 1822 .targetsize = XT_FUNCTION_MAXNAMELEN,
4538506b
JE
1823 .family = NFPROTO_IPV4,
1824 },
1da177e4
LT
1825};
1826
1827static struct nf_sockopt_ops ipt_sockopts = {
1828 .pf = PF_INET,
1829 .set_optmin = IPT_BASE_CTL,
1830 .set_optmax = IPT_SO_SET_MAX+1,
1831 .set = do_ipt_set_ctl,
1832 .get_optmin = IPT_BASE_CTL,
1833 .get_optmax = IPT_SO_GET_MAX+1,
1834 .get = do_ipt_get_ctl,
16fcec35 1835 .owner = THIS_MODULE,
1da177e4
LT
1836};
1837
3cb609d5
AD
1838static int __net_init ip_tables_net_init(struct net *net)
1839{
383ca5b8 1840 return xt_proto_init(net, NFPROTO_IPV4);
3cb609d5
AD
1841}
1842
1843static void __net_exit ip_tables_net_exit(struct net *net)
1844{
383ca5b8 1845 xt_proto_fini(net, NFPROTO_IPV4);
3cb609d5
AD
1846}
1847
1848static struct pernet_operations ip_tables_net_ops = {
1849 .init = ip_tables_net_init,
1850 .exit = ip_tables_net_exit,
1851};
1852
65b4b4e8 1853static int __init ip_tables_init(void)
1da177e4
LT
1854{
1855 int ret;
1856
3cb609d5 1857 ret = register_pernet_subsys(&ip_tables_net_ops);
0eff66e6
PM
1858 if (ret < 0)
1859 goto err1;
2e4e6a17 1860
25985edc 1861 /* No one else will be downing sem now, so we won't sleep */
4538506b 1862 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
0eff66e6
PM
1863 if (ret < 0)
1864 goto err2;
1da177e4
LT
1865
1866 /* Register setsockopt */
1867 ret = nf_register_sockopt(&ipt_sockopts);
0eff66e6 1868 if (ret < 0)
36ce9982 1869 goto err4;
1da177e4 1870
1da177e4 1871 return 0;
0eff66e6 1872
0eff66e6 1873err4:
4538506b 1874 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
0eff66e6 1875err2:
3cb609d5 1876 unregister_pernet_subsys(&ip_tables_net_ops);
0eff66e6
PM
1877err1:
1878 return ret;
1da177e4
LT
1879}
1880
65b4b4e8 1881static void __exit ip_tables_fini(void)
1da177e4
LT
1882{
1883 nf_unregister_sockopt(&ipt_sockopts);
2e4e6a17 1884
4538506b 1885 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
3cb609d5 1886 unregister_pernet_subsys(&ip_tables_net_ops);
1da177e4
LT
1887}
1888
1889EXPORT_SYMBOL(ipt_register_table);
1cbf9098
DW
1890EXPORT_SYMBOL(ipt_unregister_table_pre_exit);
1891EXPORT_SYMBOL(ipt_unregister_table_exit);
1da177e4 1892EXPORT_SYMBOL(ipt_do_table);
65b4b4e8
AM
1893module_init(ip_tables_init);
1894module_exit(ip_tables_fini);