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