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