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