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