~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/netfilter/ip6_tables.c

Version: ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp