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