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