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