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