Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / net / netfilter / x_tables.c
1 /*
2  * x_tables core - Backend for {ip,ip6,arp}_tables
3  *
4  * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5  * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
6  *
7  * Based on existing ip_tables code which is
8  *   Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
9  *   Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/socket.h>
20 #include <linux/net.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mutex.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/audit.h>
29 #include <linux/user_namespace.h>
30 #include <net/net_namespace.h>
31
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter_arp.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #include <linux/netfilter_arp/arp_tables.h>
37
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
41
42 #define XT_PCPU_BLOCK_SIZE 4096
43
44 struct compat_delta {
45         unsigned int offset; /* offset in kernel */
46         int delta; /* delta in 32bit user land */
47 };
48
49 struct xt_af {
50         struct mutex mutex;
51         struct list_head match;
52         struct list_head target;
53 #ifdef CONFIG_COMPAT
54         struct mutex compat_mutex;
55         struct compat_delta *compat_tab;
56         unsigned int number; /* number of slots in compat_tab[] */
57         unsigned int cur; /* number of used slots in compat_tab[] */
58 #endif
59 };
60
61 static struct xt_af *xt;
62
63 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
64         [NFPROTO_UNSPEC] = "x",
65         [NFPROTO_IPV4]   = "ip",
66         [NFPROTO_ARP]    = "arp",
67         [NFPROTO_BRIDGE] = "eb",
68         [NFPROTO_IPV6]   = "ip6",
69 };
70
71 /* Registration hooks for targets. */
72 int xt_register_target(struct xt_target *target)
73 {
74         u_int8_t af = target->family;
75
76         mutex_lock(&xt[af].mutex);
77         list_add(&target->list, &xt[af].target);
78         mutex_unlock(&xt[af].mutex);
79         return 0;
80 }
81 EXPORT_SYMBOL(xt_register_target);
82
83 void
84 xt_unregister_target(struct xt_target *target)
85 {
86         u_int8_t af = target->family;
87
88         mutex_lock(&xt[af].mutex);
89         list_del(&target->list);
90         mutex_unlock(&xt[af].mutex);
91 }
92 EXPORT_SYMBOL(xt_unregister_target);
93
94 int
95 xt_register_targets(struct xt_target *target, unsigned int n)
96 {
97         unsigned int i;
98         int err = 0;
99
100         for (i = 0; i < n; i++) {
101                 err = xt_register_target(&target[i]);
102                 if (err)
103                         goto err;
104         }
105         return err;
106
107 err:
108         if (i > 0)
109                 xt_unregister_targets(target, i);
110         return err;
111 }
112 EXPORT_SYMBOL(xt_register_targets);
113
114 void
115 xt_unregister_targets(struct xt_target *target, unsigned int n)
116 {
117         while (n-- > 0)
118                 xt_unregister_target(&target[n]);
119 }
120 EXPORT_SYMBOL(xt_unregister_targets);
121
122 int xt_register_match(struct xt_match *match)
123 {
124         u_int8_t af = match->family;
125
126         mutex_lock(&xt[af].mutex);
127         list_add(&match->list, &xt[af].match);
128         mutex_unlock(&xt[af].mutex);
129         return 0;
130 }
131 EXPORT_SYMBOL(xt_register_match);
132
133 void
134 xt_unregister_match(struct xt_match *match)
135 {
136         u_int8_t af = match->family;
137
138         mutex_lock(&xt[af].mutex);
139         list_del(&match->list);
140         mutex_unlock(&xt[af].mutex);
141 }
142 EXPORT_SYMBOL(xt_unregister_match);
143
144 int
145 xt_register_matches(struct xt_match *match, unsigned int n)
146 {
147         unsigned int i;
148         int err = 0;
149
150         for (i = 0; i < n; i++) {
151                 err = xt_register_match(&match[i]);
152                 if (err)
153                         goto err;
154         }
155         return err;
156
157 err:
158         if (i > 0)
159                 xt_unregister_matches(match, i);
160         return err;
161 }
162 EXPORT_SYMBOL(xt_register_matches);
163
164 void
165 xt_unregister_matches(struct xt_match *match, unsigned int n)
166 {
167         while (n-- > 0)
168                 xt_unregister_match(&match[n]);
169 }
170 EXPORT_SYMBOL(xt_unregister_matches);
171
172
173 /*
174  * These are weird, but module loading must not be done with mutex
175  * held (since they will register), and we have to have a single
176  * function to use.
177  */
178
179 /* Find match, grabs ref.  Returns ERR_PTR() on error. */
180 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
181 {
182         struct xt_match *m;
183         int err = -ENOENT;
184
185         mutex_lock(&xt[af].mutex);
186         list_for_each_entry(m, &xt[af].match, list) {
187                 if (strcmp(m->name, name) == 0) {
188                         if (m->revision == revision) {
189                                 if (try_module_get(m->me)) {
190                                         mutex_unlock(&xt[af].mutex);
191                                         return m;
192                                 }
193                         } else
194                                 err = -EPROTOTYPE; /* Found something. */
195                 }
196         }
197         mutex_unlock(&xt[af].mutex);
198
199         if (af != NFPROTO_UNSPEC)
200                 /* Try searching again in the family-independent list */
201                 return xt_find_match(NFPROTO_UNSPEC, name, revision);
202
203         return ERR_PTR(err);
204 }
205 EXPORT_SYMBOL(xt_find_match);
206
207 struct xt_match *
208 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209 {
210         struct xt_match *match;
211
212         if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
213                 return ERR_PTR(-EINVAL);
214
215         match = xt_find_match(nfproto, name, revision);
216         if (IS_ERR(match)) {
217                 request_module("%st_%s", xt_prefix[nfproto], name);
218                 match = xt_find_match(nfproto, name, revision);
219         }
220
221         return match;
222 }
223 EXPORT_SYMBOL_GPL(xt_request_find_match);
224
225 /* Find target, grabs ref.  Returns ERR_PTR() on error. */
226 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
227 {
228         struct xt_target *t;
229         int err = -ENOENT;
230
231         mutex_lock(&xt[af].mutex);
232         list_for_each_entry(t, &xt[af].target, list) {
233                 if (strcmp(t->name, name) == 0) {
234                         if (t->revision == revision) {
235                                 if (try_module_get(t->me)) {
236                                         mutex_unlock(&xt[af].mutex);
237                                         return t;
238                                 }
239                         } else
240                                 err = -EPROTOTYPE; /* Found something. */
241                 }
242         }
243         mutex_unlock(&xt[af].mutex);
244
245         if (af != NFPROTO_UNSPEC)
246                 /* Try searching again in the family-independent list */
247                 return xt_find_target(NFPROTO_UNSPEC, name, revision);
248
249         return ERR_PTR(err);
250 }
251 EXPORT_SYMBOL(xt_find_target);
252
253 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
254 {
255         struct xt_target *target;
256
257         if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
258                 return ERR_PTR(-EINVAL);
259
260         target = xt_find_target(af, name, revision);
261         if (IS_ERR(target)) {
262                 request_module("%st_%s", xt_prefix[af], name);
263                 target = xt_find_target(af, name, revision);
264         }
265
266         return target;
267 }
268 EXPORT_SYMBOL_GPL(xt_request_find_target);
269
270
271 static int xt_obj_to_user(u16 __user *psize, u16 size,
272                           void __user *pname, const char *name,
273                           u8 __user *prev, u8 rev)
274 {
275         if (put_user(size, psize))
276                 return -EFAULT;
277         if (copy_to_user(pname, name, strlen(name) + 1))
278                 return -EFAULT;
279         if (put_user(rev, prev))
280                 return -EFAULT;
281
282         return 0;
283 }
284
285 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE)                              \
286         xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size,  \
287                        U->u.user.name, K->u.kernel.TYPE->name,          \
288                        &U->u.user.revision, K->u.kernel.TYPE->revision)
289
290 int xt_data_to_user(void __user *dst, const void *src,
291                     int usersize, int size, int aligned_size)
292 {
293         usersize = usersize ? : size;
294         if (copy_to_user(dst, src, usersize))
295                 return -EFAULT;
296         if (usersize != aligned_size &&
297             clear_user(dst + usersize, aligned_size - usersize))
298                 return -EFAULT;
299
300         return 0;
301 }
302 EXPORT_SYMBOL_GPL(xt_data_to_user);
303
304 #define XT_DATA_TO_USER(U, K, TYPE)                                     \
305         xt_data_to_user(U->data, K->data,                               \
306                         K->u.kernel.TYPE->usersize,                     \
307                         K->u.kernel.TYPE->TYPE##size,                   \
308                         XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
309
310 int xt_match_to_user(const struct xt_entry_match *m,
311                      struct xt_entry_match __user *u)
312 {
313         return XT_OBJ_TO_USER(u, m, match, 0) ||
314                XT_DATA_TO_USER(u, m, match);
315 }
316 EXPORT_SYMBOL_GPL(xt_match_to_user);
317
318 int xt_target_to_user(const struct xt_entry_target *t,
319                       struct xt_entry_target __user *u)
320 {
321         return XT_OBJ_TO_USER(u, t, target, 0) ||
322                XT_DATA_TO_USER(u, t, target);
323 }
324 EXPORT_SYMBOL_GPL(xt_target_to_user);
325
326 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
327 {
328         const struct xt_match *m;
329         int have_rev = 0;
330
331         list_for_each_entry(m, &xt[af].match, list) {
332                 if (strcmp(m->name, name) == 0) {
333                         if (m->revision > *bestp)
334                                 *bestp = m->revision;
335                         if (m->revision == revision)
336                                 have_rev = 1;
337                 }
338         }
339
340         if (af != NFPROTO_UNSPEC && !have_rev)
341                 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
342
343         return have_rev;
344 }
345
346 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
347 {
348         const struct xt_target *t;
349         int have_rev = 0;
350
351         list_for_each_entry(t, &xt[af].target, list) {
352                 if (strcmp(t->name, name) == 0) {
353                         if (t->revision > *bestp)
354                                 *bestp = t->revision;
355                         if (t->revision == revision)
356                                 have_rev = 1;
357                 }
358         }
359
360         if (af != NFPROTO_UNSPEC && !have_rev)
361                 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
362
363         return have_rev;
364 }
365
366 /* Returns true or false (if no such extension at all) */
367 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
368                      int *err)
369 {
370         int have_rev, best = -1;
371
372         mutex_lock(&xt[af].mutex);
373         if (target == 1)
374                 have_rev = target_revfn(af, name, revision, &best);
375         else
376                 have_rev = match_revfn(af, name, revision, &best);
377         mutex_unlock(&xt[af].mutex);
378
379         /* Nothing at all?  Return 0 to try loading module. */
380         if (best == -1) {
381                 *err = -ENOENT;
382                 return 0;
383         }
384
385         *err = best;
386         if (!have_rev)
387                 *err = -EPROTONOSUPPORT;
388         return 1;
389 }
390 EXPORT_SYMBOL_GPL(xt_find_revision);
391
392 static char *
393 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
394 {
395         static const char *const inetbr_names[] = {
396                 "PREROUTING", "INPUT", "FORWARD",
397                 "OUTPUT", "POSTROUTING", "BROUTING",
398         };
399         static const char *const arp_names[] = {
400                 "INPUT", "FORWARD", "OUTPUT",
401         };
402         const char *const *names;
403         unsigned int i, max;
404         char *p = buf;
405         bool np = false;
406         int res;
407
408         names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
409         max   = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
410                                            ARRAY_SIZE(inetbr_names);
411         *p = '\0';
412         for (i = 0; i < max; ++i) {
413                 if (!(mask & (1 << i)))
414                         continue;
415                 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
416                 if (res > 0) {
417                         size -= res;
418                         p += res;
419                 }
420                 np = true;
421         }
422
423         return buf;
424 }
425
426 int xt_check_match(struct xt_mtchk_param *par,
427                    unsigned int size, u_int8_t proto, bool inv_proto)
428 {
429         int ret;
430
431         if (XT_ALIGN(par->match->matchsize) != size &&
432             par->match->matchsize != -1) {
433                 /*
434                  * ebt_among is exempt from centralized matchsize checking
435                  * because it uses a dynamic-size data set.
436                  */
437                 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
438                                    xt_prefix[par->family], par->match->name,
439                                    par->match->revision,
440                                    XT_ALIGN(par->match->matchsize), size);
441                 return -EINVAL;
442         }
443         if (par->match->table != NULL &&
444             strcmp(par->match->table, par->table) != 0) {
445                 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
446                                     xt_prefix[par->family], par->match->name,
447                                     par->match->table, par->table);
448                 return -EINVAL;
449         }
450         if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
451                 char used[64], allow[64];
452
453                 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
454                                     xt_prefix[par->family], par->match->name,
455                                     textify_hooks(used, sizeof(used),
456                                                   par->hook_mask, par->family),
457                                     textify_hooks(allow, sizeof(allow),
458                                                   par->match->hooks,
459                                                   par->family));
460                 return -EINVAL;
461         }
462         if (par->match->proto && (par->match->proto != proto || inv_proto)) {
463                 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
464                                     xt_prefix[par->family], par->match->name,
465                                     par->match->proto);
466                 return -EINVAL;
467         }
468         if (par->match->checkentry != NULL) {
469                 ret = par->match->checkentry(par);
470                 if (ret < 0)
471                         return ret;
472                 else if (ret > 0)
473                         /* Flag up potential errors. */
474                         return -EIO;
475         }
476         return 0;
477 }
478 EXPORT_SYMBOL_GPL(xt_check_match);
479
480 /** xt_check_entry_match - check that matches end before start of target
481  *
482  * @match: beginning of xt_entry_match
483  * @target: beginning of this rules target (alleged end of matches)
484  * @alignment: alignment requirement of match structures
485  *
486  * Validates that all matches add up to the beginning of the target,
487  * and that each match covers at least the base structure size.
488  *
489  * Return: 0 on success, negative errno on failure.
490  */
491 static int xt_check_entry_match(const char *match, const char *target,
492                                 const size_t alignment)
493 {
494         const struct xt_entry_match *pos;
495         int length = target - match;
496
497         if (length == 0) /* no matches */
498                 return 0;
499
500         pos = (struct xt_entry_match *)match;
501         do {
502                 if ((unsigned long)pos % alignment)
503                         return -EINVAL;
504
505                 if (length < (int)sizeof(struct xt_entry_match))
506                         return -EINVAL;
507
508                 if (pos->u.match_size < sizeof(struct xt_entry_match))
509                         return -EINVAL;
510
511                 if (pos->u.match_size > length)
512                         return -EINVAL;
513
514                 length -= pos->u.match_size;
515                 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
516         } while (length > 0);
517
518         return 0;
519 }
520
521 #ifdef CONFIG_COMPAT
522 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
523 {
524         struct xt_af *xp = &xt[af];
525
526         if (!xp->compat_tab) {
527                 if (!xp->number)
528                         return -EINVAL;
529                 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
530                 if (!xp->compat_tab)
531                         return -ENOMEM;
532                 xp->cur = 0;
533         }
534
535         if (xp->cur >= xp->number)
536                 return -EINVAL;
537
538         if (xp->cur)
539                 delta += xp->compat_tab[xp->cur - 1].delta;
540         xp->compat_tab[xp->cur].offset = offset;
541         xp->compat_tab[xp->cur].delta = delta;
542         xp->cur++;
543         return 0;
544 }
545 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
546
547 void xt_compat_flush_offsets(u_int8_t af)
548 {
549         if (xt[af].compat_tab) {
550                 vfree(xt[af].compat_tab);
551                 xt[af].compat_tab = NULL;
552                 xt[af].number = 0;
553                 xt[af].cur = 0;
554         }
555 }
556 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
557
558 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
559 {
560         struct compat_delta *tmp = xt[af].compat_tab;
561         int mid, left = 0, right = xt[af].cur - 1;
562
563         while (left <= right) {
564                 mid = (left + right) >> 1;
565                 if (offset > tmp[mid].offset)
566                         left = mid + 1;
567                 else if (offset < tmp[mid].offset)
568                         right = mid - 1;
569                 else
570                         return mid ? tmp[mid - 1].delta : 0;
571         }
572         return left ? tmp[left - 1].delta : 0;
573 }
574 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
575
576 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
577 {
578         xt[af].number = number;
579         xt[af].cur = 0;
580 }
581 EXPORT_SYMBOL(xt_compat_init_offsets);
582
583 int xt_compat_match_offset(const struct xt_match *match)
584 {
585         u_int16_t csize = match->compatsize ? : match->matchsize;
586         return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
587 }
588 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
589
590 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
591                                unsigned int *size)
592 {
593         const struct xt_match *match = m->u.kernel.match;
594         struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
595         int pad, off = xt_compat_match_offset(match);
596         u_int16_t msize = cm->u.user.match_size;
597         char name[sizeof(m->u.user.name)];
598
599         m = *dstptr;
600         memcpy(m, cm, sizeof(*cm));
601         if (match->compat_from_user)
602                 match->compat_from_user(m->data, cm->data);
603         else
604                 memcpy(m->data, cm->data, msize - sizeof(*cm));
605         pad = XT_ALIGN(match->matchsize) - match->matchsize;
606         if (pad > 0)
607                 memset(m->data + match->matchsize, 0, pad);
608
609         msize += off;
610         m->u.user.match_size = msize;
611         strlcpy(name, match->name, sizeof(name));
612         module_put(match->me);
613         strncpy(m->u.user.name, name, sizeof(m->u.user.name));
614
615         *size += off;
616         *dstptr += msize;
617 }
618 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
619
620 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE)                      \
621         xt_data_to_user(U->data, K->data,                               \
622                         K->u.kernel.TYPE->usersize,                     \
623                         C_SIZE,                                         \
624                         COMPAT_XT_ALIGN(C_SIZE))
625
626 int xt_compat_match_to_user(const struct xt_entry_match *m,
627                             void __user **dstptr, unsigned int *size)
628 {
629         const struct xt_match *match = m->u.kernel.match;
630         struct compat_xt_entry_match __user *cm = *dstptr;
631         int off = xt_compat_match_offset(match);
632         u_int16_t msize = m->u.user.match_size - off;
633
634         if (XT_OBJ_TO_USER(cm, m, match, msize))
635                 return -EFAULT;
636
637         if (match->compat_to_user) {
638                 if (match->compat_to_user((void __user *)cm->data, m->data))
639                         return -EFAULT;
640         } else {
641                 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
642                         return -EFAULT;
643         }
644
645         *size -= off;
646         *dstptr += msize;
647         return 0;
648 }
649 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
650
651 /* non-compat version may have padding after verdict */
652 struct compat_xt_standard_target {
653         struct compat_xt_entry_target t;
654         compat_uint_t verdict;
655 };
656
657 int xt_compat_check_entry_offsets(const void *base, const char *elems,
658                                   unsigned int target_offset,
659                                   unsigned int next_offset)
660 {
661         long size_of_base_struct = elems - (const char *)base;
662         const struct compat_xt_entry_target *t;
663         const char *e = base;
664
665         if (target_offset < size_of_base_struct)
666                 return -EINVAL;
667
668         if (target_offset + sizeof(*t) > next_offset)
669                 return -EINVAL;
670
671         t = (void *)(e + target_offset);
672         if (t->u.target_size < sizeof(*t))
673                 return -EINVAL;
674
675         if (target_offset + t->u.target_size > next_offset)
676                 return -EINVAL;
677
678         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
679             COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
680                 return -EINVAL;
681
682         /* compat_xt_entry match has less strict alignment requirements,
683          * otherwise they are identical.  In case of padding differences
684          * we need to add compat version of xt_check_entry_match.
685          */
686         BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
687
688         return xt_check_entry_match(elems, base + target_offset,
689                                     __alignof__(struct compat_xt_entry_match));
690 }
691 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
692 #endif /* CONFIG_COMPAT */
693
694 /**
695  * xt_check_entry_offsets - validate arp/ip/ip6t_entry
696  *
697  * @base: pointer to arp/ip/ip6t_entry
698  * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
699  * @target_offset: the arp/ip/ip6_t->target_offset
700  * @next_offset: the arp/ip/ip6_t->next_offset
701  *
702  * validates that target_offset and next_offset are sane and that all
703  * match sizes (if any) align with the target offset.
704  *
705  * This function does not validate the targets or matches themselves, it
706  * only tests that all the offsets and sizes are correct, that all
707  * match structures are aligned, and that the last structure ends where
708  * the target structure begins.
709  *
710  * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
711  *
712  * The arp/ip/ip6t_entry structure @base must have passed following tests:
713  * - it must point to a valid memory location
714  * - base to base + next_offset must be accessible, i.e. not exceed allocated
715  *   length.
716  *
717  * A well-formed entry looks like this:
718  *
719  * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
720  * e->elems[]-----'                              |               |
721  *                matchsize                      |               |
722  *                                matchsize      |               |
723  *                                               |               |
724  * target_offset---------------------------------'               |
725  * next_offset---------------------------------------------------'
726  *
727  * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
728  *          This is where matches (if any) and the target reside.
729  * target_offset: beginning of target.
730  * next_offset: start of the next rule; also: size of this rule.
731  * Since targets have a minimum size, target_offset + minlen <= next_offset.
732  *
733  * Every match stores its size, sum of sizes must not exceed target_offset.
734  *
735  * Return: 0 on success, negative errno on failure.
736  */
737 int xt_check_entry_offsets(const void *base,
738                            const char *elems,
739                            unsigned int target_offset,
740                            unsigned int next_offset)
741 {
742         long size_of_base_struct = elems - (const char *)base;
743         const struct xt_entry_target *t;
744         const char *e = base;
745
746         /* target start is within the ip/ip6/arpt_entry struct */
747         if (target_offset < size_of_base_struct)
748                 return -EINVAL;
749
750         if (target_offset + sizeof(*t) > next_offset)
751                 return -EINVAL;
752
753         t = (void *)(e + target_offset);
754         if (t->u.target_size < sizeof(*t))
755                 return -EINVAL;
756
757         if (target_offset + t->u.target_size > next_offset)
758                 return -EINVAL;
759
760         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
761             XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
762                 return -EINVAL;
763
764         return xt_check_entry_match(elems, base + target_offset,
765                                     __alignof__(struct xt_entry_match));
766 }
767 EXPORT_SYMBOL(xt_check_entry_offsets);
768
769 /**
770  * xt_alloc_entry_offsets - allocate array to store rule head offsets
771  *
772  * @size: number of entries
773  *
774  * Return: NULL or kmalloc'd or vmalloc'd array
775  */
776 unsigned int *xt_alloc_entry_offsets(unsigned int size)
777 {
778         return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
779
780 }
781 EXPORT_SYMBOL(xt_alloc_entry_offsets);
782
783 /**
784  * xt_find_jump_offset - check if target is a valid jump offset
785  *
786  * @offsets: array containing all valid rule start offsets of a rule blob
787  * @target: the jump target to search for
788  * @size: entries in @offset
789  */
790 bool xt_find_jump_offset(const unsigned int *offsets,
791                          unsigned int target, unsigned int size)
792 {
793         int m, low = 0, hi = size;
794
795         while (hi > low) {
796                 m = (low + hi) / 2u;
797
798                 if (offsets[m] > target)
799                         hi = m;
800                 else if (offsets[m] < target)
801                         low = m + 1;
802                 else
803                         return true;
804         }
805
806         return false;
807 }
808 EXPORT_SYMBOL(xt_find_jump_offset);
809
810 int xt_check_target(struct xt_tgchk_param *par,
811                     unsigned int size, u_int8_t proto, bool inv_proto)
812 {
813         int ret;
814
815         if (XT_ALIGN(par->target->targetsize) != size) {
816                 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
817                                    xt_prefix[par->family], par->target->name,
818                                    par->target->revision,
819                                    XT_ALIGN(par->target->targetsize), size);
820                 return -EINVAL;
821         }
822         if (par->target->table != NULL &&
823             strcmp(par->target->table, par->table) != 0) {
824                 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
825                                     xt_prefix[par->family], par->target->name,
826                                     par->target->table, par->table);
827                 return -EINVAL;
828         }
829         if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
830                 char used[64], allow[64];
831
832                 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
833                                     xt_prefix[par->family], par->target->name,
834                                     textify_hooks(used, sizeof(used),
835                                                   par->hook_mask, par->family),
836                                     textify_hooks(allow, sizeof(allow),
837                                                   par->target->hooks,
838                                                   par->family));
839                 return -EINVAL;
840         }
841         if (par->target->proto && (par->target->proto != proto || inv_proto)) {
842                 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
843                                     xt_prefix[par->family], par->target->name,
844                                     par->target->proto);
845                 return -EINVAL;
846         }
847         if (par->target->checkentry != NULL) {
848                 ret = par->target->checkentry(par);
849                 if (ret < 0)
850                         return ret;
851                 else if (ret > 0)
852                         /* Flag up potential errors. */
853                         return -EIO;
854         }
855         return 0;
856 }
857 EXPORT_SYMBOL_GPL(xt_check_target);
858
859 /**
860  * xt_copy_counters_from_user - copy counters and metadata from userspace
861  *
862  * @user: src pointer to userspace memory
863  * @len: alleged size of userspace memory
864  * @info: where to store the xt_counters_info metadata
865  * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
866  *
867  * Copies counter meta data from @user and stores it in @info.
868  *
869  * vmallocs memory to hold the counters, then copies the counter data
870  * from @user to the new memory and returns a pointer to it.
871  *
872  * If @compat is true, @info gets converted automatically to the 64bit
873  * representation.
874  *
875  * The metadata associated with the counters is stored in @info.
876  *
877  * Return: returns pointer that caller has to test via IS_ERR().
878  * If IS_ERR is false, caller has to vfree the pointer.
879  */
880 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
881                                  struct xt_counters_info *info, bool compat)
882 {
883         void *mem;
884         u64 size;
885
886 #ifdef CONFIG_COMPAT
887         if (compat) {
888                 /* structures only differ in size due to alignment */
889                 struct compat_xt_counters_info compat_tmp;
890
891                 if (len <= sizeof(compat_tmp))
892                         return ERR_PTR(-EINVAL);
893
894                 len -= sizeof(compat_tmp);
895                 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
896                         return ERR_PTR(-EFAULT);
897
898                 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
899                 info->num_counters = compat_tmp.num_counters;
900                 user += sizeof(compat_tmp);
901         } else
902 #endif
903         {
904                 if (len <= sizeof(*info))
905                         return ERR_PTR(-EINVAL);
906
907                 len -= sizeof(*info);
908                 if (copy_from_user(info, user, sizeof(*info)) != 0)
909                         return ERR_PTR(-EFAULT);
910
911                 user += sizeof(*info);
912         }
913         info->name[sizeof(info->name) - 1] = '\0';
914
915         size = sizeof(struct xt_counters);
916         size *= info->num_counters;
917
918         if (size != (u64)len)
919                 return ERR_PTR(-EINVAL);
920
921         mem = vmalloc(len);
922         if (!mem)
923                 return ERR_PTR(-ENOMEM);
924
925         if (copy_from_user(mem, user, len) == 0)
926                 return mem;
927
928         vfree(mem);
929         return ERR_PTR(-EFAULT);
930 }
931 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
932
933 #ifdef CONFIG_COMPAT
934 int xt_compat_target_offset(const struct xt_target *target)
935 {
936         u_int16_t csize = target->compatsize ? : target->targetsize;
937         return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
938 }
939 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
940
941 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
942                                 unsigned int *size)
943 {
944         const struct xt_target *target = t->u.kernel.target;
945         struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
946         int pad, off = xt_compat_target_offset(target);
947         u_int16_t tsize = ct->u.user.target_size;
948         char name[sizeof(t->u.user.name)];
949
950         t = *dstptr;
951         memcpy(t, ct, sizeof(*ct));
952         if (target->compat_from_user)
953                 target->compat_from_user(t->data, ct->data);
954         else
955                 memcpy(t->data, ct->data, tsize - sizeof(*ct));
956         pad = XT_ALIGN(target->targetsize) - target->targetsize;
957         if (pad > 0)
958                 memset(t->data + target->targetsize, 0, pad);
959
960         tsize += off;
961         t->u.user.target_size = tsize;
962         strlcpy(name, target->name, sizeof(name));
963         module_put(target->me);
964         strncpy(t->u.user.name, name, sizeof(t->u.user.name));
965
966         *size += off;
967         *dstptr += tsize;
968 }
969 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
970
971 int xt_compat_target_to_user(const struct xt_entry_target *t,
972                              void __user **dstptr, unsigned int *size)
973 {
974         const struct xt_target *target = t->u.kernel.target;
975         struct compat_xt_entry_target __user *ct = *dstptr;
976         int off = xt_compat_target_offset(target);
977         u_int16_t tsize = t->u.user.target_size - off;
978
979         if (XT_OBJ_TO_USER(ct, t, target, tsize))
980                 return -EFAULT;
981
982         if (target->compat_to_user) {
983                 if (target->compat_to_user((void __user *)ct->data, t->data))
984                         return -EFAULT;
985         } else {
986                 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
987                         return -EFAULT;
988         }
989
990         *size -= off;
991         *dstptr += tsize;
992         return 0;
993 }
994 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
995 #endif
996
997 struct xt_table_info *xt_alloc_table_info(unsigned int size)
998 {
999         struct xt_table_info *info = NULL;
1000         size_t sz = sizeof(*info) + size;
1001
1002         if (sz < sizeof(*info))
1003                 return NULL;
1004
1005         /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1006          * work reasonably well if sz is too large and bail out rather
1007          * than shoot all processes down before realizing there is nothing
1008          * more to reclaim.
1009          */
1010         info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
1011         if (!info)
1012                 return NULL;
1013
1014         memset(info, 0, sizeof(*info));
1015         info->size = size;
1016         return info;
1017 }
1018 EXPORT_SYMBOL(xt_alloc_table_info);
1019
1020 void xt_free_table_info(struct xt_table_info *info)
1021 {
1022         int cpu;
1023
1024         if (info->jumpstack != NULL) {
1025                 for_each_possible_cpu(cpu)
1026                         kvfree(info->jumpstack[cpu]);
1027                 kvfree(info->jumpstack);
1028         }
1029
1030         kvfree(info);
1031 }
1032 EXPORT_SYMBOL(xt_free_table_info);
1033
1034 /* Find table by name, grabs mutex & ref.  Returns ERR_PTR on error. */
1035 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1036                                     const char *name)
1037 {
1038         struct xt_table *t, *found = NULL;
1039
1040         mutex_lock(&xt[af].mutex);
1041         list_for_each_entry(t, &net->xt.tables[af], list)
1042                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1043                         return t;
1044
1045         if (net == &init_net)
1046                 goto out;
1047
1048         /* Table doesn't exist in this netns, re-try init */
1049         list_for_each_entry(t, &init_net.xt.tables[af], list) {
1050                 int err;
1051
1052                 if (strcmp(t->name, name))
1053                         continue;
1054                 if (!try_module_get(t->me))
1055                         goto out;
1056                 mutex_unlock(&xt[af].mutex);
1057                 err = t->table_init(net);
1058                 if (err < 0) {
1059                         module_put(t->me);
1060                         return ERR_PTR(err);
1061                 }
1062
1063                 found = t;
1064
1065                 mutex_lock(&xt[af].mutex);
1066                 break;
1067         }
1068
1069         if (!found)
1070                 goto out;
1071
1072         /* and once again: */
1073         list_for_each_entry(t, &net->xt.tables[af], list)
1074                 if (strcmp(t->name, name) == 0)
1075                         return t;
1076
1077         module_put(found->me);
1078  out:
1079         mutex_unlock(&xt[af].mutex);
1080         return ERR_PTR(-ENOENT);
1081 }
1082 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1083
1084 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1085                                             const char *name)
1086 {
1087         struct xt_table *t = xt_find_table_lock(net, af, name);
1088
1089 #ifdef CONFIG_MODULES
1090         if (IS_ERR(t)) {
1091                 int err = request_module("%stable_%s", xt_prefix[af], name);
1092                 if (err < 0)
1093                         return ERR_PTR(err);
1094                 t = xt_find_table_lock(net, af, name);
1095         }
1096 #endif
1097
1098         return t;
1099 }
1100 EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1101
1102 void xt_table_unlock(struct xt_table *table)
1103 {
1104         mutex_unlock(&xt[table->af].mutex);
1105 }
1106 EXPORT_SYMBOL_GPL(xt_table_unlock);
1107
1108 #ifdef CONFIG_COMPAT
1109 void xt_compat_lock(u_int8_t af)
1110 {
1111         mutex_lock(&xt[af].compat_mutex);
1112 }
1113 EXPORT_SYMBOL_GPL(xt_compat_lock);
1114
1115 void xt_compat_unlock(u_int8_t af)
1116 {
1117         mutex_unlock(&xt[af].compat_mutex);
1118 }
1119 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1120 #endif
1121
1122 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1123 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1124
1125 struct static_key xt_tee_enabled __read_mostly;
1126 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1127
1128 static int xt_jumpstack_alloc(struct xt_table_info *i)
1129 {
1130         unsigned int size;
1131         int cpu;
1132
1133         size = sizeof(void **) * nr_cpu_ids;
1134         if (size > PAGE_SIZE)
1135                 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1136         else
1137                 i->jumpstack = kzalloc(size, GFP_KERNEL);
1138         if (i->jumpstack == NULL)
1139                 return -ENOMEM;
1140
1141         /* ruleset without jumps -- no stack needed */
1142         if (i->stacksize == 0)
1143                 return 0;
1144
1145         /* Jumpstack needs to be able to record two full callchains, one
1146          * from the first rule set traversal, plus one table reentrancy
1147          * via -j TEE without clobbering the callchain that brought us to
1148          * TEE target.
1149          *
1150          * This is done by allocating two jumpstacks per cpu, on reentry
1151          * the upper half of the stack is used.
1152          *
1153          * see the jumpstack setup in ipt_do_table() for more details.
1154          */
1155         size = sizeof(void *) * i->stacksize * 2u;
1156         for_each_possible_cpu(cpu) {
1157                 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1158                         cpu_to_node(cpu));
1159                 if (i->jumpstack[cpu] == NULL)
1160                         /*
1161                          * Freeing will be done later on by the callers. The
1162                          * chain is: xt_replace_table -> __do_replace ->
1163                          * do_replace -> xt_free_table_info.
1164                          */
1165                         return -ENOMEM;
1166         }
1167
1168         return 0;
1169 }
1170
1171 struct xt_table_info *
1172 xt_replace_table(struct xt_table *table,
1173               unsigned int num_counters,
1174               struct xt_table_info *newinfo,
1175               int *error)
1176 {
1177         struct xt_table_info *private;
1178         unsigned int cpu;
1179         int ret;
1180
1181         ret = xt_jumpstack_alloc(newinfo);
1182         if (ret < 0) {
1183                 *error = ret;
1184                 return NULL;
1185         }
1186
1187         /* Do the substitution. */
1188         local_bh_disable();
1189         private = table->private;
1190
1191         /* Check inside lock: is the old number correct? */
1192         if (num_counters != private->number) {
1193                 pr_debug("num_counters != table->private->number (%u/%u)\n",
1194                          num_counters, private->number);
1195                 local_bh_enable();
1196                 *error = -EAGAIN;
1197                 return NULL;
1198         }
1199
1200         newinfo->initial_entries = private->initial_entries;
1201         /*
1202          * Ensure contents of newinfo are visible before assigning to
1203          * private.
1204          */
1205         smp_wmb();
1206         table->private = newinfo;
1207
1208         /* make sure all cpus see new ->private value */
1209         smp_wmb();
1210
1211         /*
1212          * Even though table entries have now been swapped, other CPU's
1213          * may still be using the old entries...
1214          */
1215         local_bh_enable();
1216
1217         /* ... so wait for even xt_recseq on all cpus */
1218         for_each_possible_cpu(cpu) {
1219                 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1220                 u32 seq = raw_read_seqcount(s);
1221
1222                 if (seq & 1) {
1223                         do {
1224                                 cond_resched();
1225                                 cpu_relax();
1226                         } while (seq == raw_read_seqcount(s));
1227                 }
1228         }
1229
1230 #ifdef CONFIG_AUDIT
1231         if (audit_enabled) {
1232                 audit_log(current->audit_context, GFP_KERNEL,
1233                           AUDIT_NETFILTER_CFG,
1234                           "table=%s family=%u entries=%u",
1235                           table->name, table->af, private->number);
1236         }
1237 #endif
1238
1239         return private;
1240 }
1241 EXPORT_SYMBOL_GPL(xt_replace_table);
1242
1243 struct xt_table *xt_register_table(struct net *net,
1244                                    const struct xt_table *input_table,
1245                                    struct xt_table_info *bootstrap,
1246                                    struct xt_table_info *newinfo)
1247 {
1248         int ret;
1249         struct xt_table_info *private;
1250         struct xt_table *t, *table;
1251
1252         /* Don't add one object to multiple lists. */
1253         table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1254         if (!table) {
1255                 ret = -ENOMEM;
1256                 goto out;
1257         }
1258
1259         mutex_lock(&xt[table->af].mutex);
1260         /* Don't autoload: we'd eat our tail... */
1261         list_for_each_entry(t, &net->xt.tables[table->af], list) {
1262                 if (strcmp(t->name, table->name) == 0) {
1263                         ret = -EEXIST;
1264                         goto unlock;
1265                 }
1266         }
1267
1268         /* Simplifies replace_table code. */
1269         table->private = bootstrap;
1270
1271         if (!xt_replace_table(table, 0, newinfo, &ret))
1272                 goto unlock;
1273
1274         private = table->private;
1275         pr_debug("table->private->number = %u\n", private->number);
1276
1277         /* save number of initial entries */
1278         private->initial_entries = private->number;
1279
1280         list_add(&table->list, &net->xt.tables[table->af]);
1281         mutex_unlock(&xt[table->af].mutex);
1282         return table;
1283
1284 unlock:
1285         mutex_unlock(&xt[table->af].mutex);
1286         kfree(table);
1287 out:
1288         return ERR_PTR(ret);
1289 }
1290 EXPORT_SYMBOL_GPL(xt_register_table);
1291
1292 void *xt_unregister_table(struct xt_table *table)
1293 {
1294         struct xt_table_info *private;
1295
1296         mutex_lock(&xt[table->af].mutex);
1297         private = table->private;
1298         list_del(&table->list);
1299         mutex_unlock(&xt[table->af].mutex);
1300         kfree(table);
1301
1302         return private;
1303 }
1304 EXPORT_SYMBOL_GPL(xt_unregister_table);
1305
1306 #ifdef CONFIG_PROC_FS
1307 struct xt_names_priv {
1308         struct seq_net_private p;
1309         u_int8_t af;
1310 };
1311 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1312 {
1313         struct xt_names_priv *priv = seq->private;
1314         struct net *net = seq_file_net(seq);
1315         u_int8_t af = priv->af;
1316
1317         mutex_lock(&xt[af].mutex);
1318         return seq_list_start(&net->xt.tables[af], *pos);
1319 }
1320
1321 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1322 {
1323         struct xt_names_priv *priv = seq->private;
1324         struct net *net = seq_file_net(seq);
1325         u_int8_t af = priv->af;
1326
1327         return seq_list_next(v, &net->xt.tables[af], pos);
1328 }
1329
1330 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1331 {
1332         struct xt_names_priv *priv = seq->private;
1333         u_int8_t af = priv->af;
1334
1335         mutex_unlock(&xt[af].mutex);
1336 }
1337
1338 static int xt_table_seq_show(struct seq_file *seq, void *v)
1339 {
1340         struct xt_table *table = list_entry(v, struct xt_table, list);
1341
1342         if (*table->name)
1343                 seq_printf(seq, "%s\n", table->name);
1344         return 0;
1345 }
1346
1347 static const struct seq_operations xt_table_seq_ops = {
1348         .start  = xt_table_seq_start,
1349         .next   = xt_table_seq_next,
1350         .stop   = xt_table_seq_stop,
1351         .show   = xt_table_seq_show,
1352 };
1353
1354 static int xt_table_open(struct inode *inode, struct file *file)
1355 {
1356         int ret;
1357         struct xt_names_priv *priv;
1358
1359         ret = seq_open_net(inode, file, &xt_table_seq_ops,
1360                            sizeof(struct xt_names_priv));
1361         if (!ret) {
1362                 priv = ((struct seq_file *)file->private_data)->private;
1363                 priv->af = (unsigned long)PDE_DATA(inode);
1364         }
1365         return ret;
1366 }
1367
1368 static const struct file_operations xt_table_ops = {
1369         .open    = xt_table_open,
1370         .read    = seq_read,
1371         .llseek  = seq_lseek,
1372         .release = seq_release_net,
1373 };
1374
1375 /*
1376  * Traverse state for ip{,6}_{tables,matches} for helping crossing
1377  * the multi-AF mutexes.
1378  */
1379 struct nf_mttg_trav {
1380         struct list_head *head, *curr;
1381         uint8_t class, nfproto;
1382 };
1383
1384 enum {
1385         MTTG_TRAV_INIT,
1386         MTTG_TRAV_NFP_UNSPEC,
1387         MTTG_TRAV_NFP_SPEC,
1388         MTTG_TRAV_DONE,
1389 };
1390
1391 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1392     bool is_target)
1393 {
1394         static const uint8_t next_class[] = {
1395                 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1396                 [MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1397         };
1398         struct nf_mttg_trav *trav = seq->private;
1399
1400         switch (trav->class) {
1401         case MTTG_TRAV_INIT:
1402                 trav->class = MTTG_TRAV_NFP_UNSPEC;
1403                 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1404                 trav->head = trav->curr = is_target ?
1405                         &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1406                 break;
1407         case MTTG_TRAV_NFP_UNSPEC:
1408                 trav->curr = trav->curr->next;
1409                 if (trav->curr != trav->head)
1410                         break;
1411                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1412                 mutex_lock(&xt[trav->nfproto].mutex);
1413                 trav->head = trav->curr = is_target ?
1414                         &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1415                 trav->class = next_class[trav->class];
1416                 break;
1417         case MTTG_TRAV_NFP_SPEC:
1418                 trav->curr = trav->curr->next;
1419                 if (trav->curr != trav->head)
1420                         break;
1421                 /* fall through */
1422         default:
1423                 return NULL;
1424         }
1425
1426         if (ppos != NULL)
1427                 ++*ppos;
1428         return trav;
1429 }
1430
1431 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1432     bool is_target)
1433 {
1434         struct nf_mttg_trav *trav = seq->private;
1435         unsigned int j;
1436
1437         trav->class = MTTG_TRAV_INIT;
1438         for (j = 0; j < *pos; ++j)
1439                 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1440                         return NULL;
1441         return trav;
1442 }
1443
1444 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1445 {
1446         struct nf_mttg_trav *trav = seq->private;
1447
1448         switch (trav->class) {
1449         case MTTG_TRAV_NFP_UNSPEC:
1450                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1451                 break;
1452         case MTTG_TRAV_NFP_SPEC:
1453                 mutex_unlock(&xt[trav->nfproto].mutex);
1454                 break;
1455         }
1456 }
1457
1458 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1459 {
1460         return xt_mttg_seq_start(seq, pos, false);
1461 }
1462
1463 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1464 {
1465         return xt_mttg_seq_next(seq, v, ppos, false);
1466 }
1467
1468 static int xt_match_seq_show(struct seq_file *seq, void *v)
1469 {
1470         const struct nf_mttg_trav *trav = seq->private;
1471         const struct xt_match *match;
1472
1473         switch (trav->class) {
1474         case MTTG_TRAV_NFP_UNSPEC:
1475         case MTTG_TRAV_NFP_SPEC:
1476                 if (trav->curr == trav->head)
1477                         return 0;
1478                 match = list_entry(trav->curr, struct xt_match, list);
1479                 if (*match->name)
1480                         seq_printf(seq, "%s\n", match->name);
1481         }
1482         return 0;
1483 }
1484
1485 static const struct seq_operations xt_match_seq_ops = {
1486         .start  = xt_match_seq_start,
1487         .next   = xt_match_seq_next,
1488         .stop   = xt_mttg_seq_stop,
1489         .show   = xt_match_seq_show,
1490 };
1491
1492 static int xt_match_open(struct inode *inode, struct file *file)
1493 {
1494         struct nf_mttg_trav *trav;
1495         trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1496         if (!trav)
1497                 return -ENOMEM;
1498
1499         trav->nfproto = (unsigned long)PDE_DATA(inode);
1500         return 0;
1501 }
1502
1503 static const struct file_operations xt_match_ops = {
1504         .open    = xt_match_open,
1505         .read    = seq_read,
1506         .llseek  = seq_lseek,
1507         .release = seq_release_private,
1508 };
1509
1510 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1511 {
1512         return xt_mttg_seq_start(seq, pos, true);
1513 }
1514
1515 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1516 {
1517         return xt_mttg_seq_next(seq, v, ppos, true);
1518 }
1519
1520 static int xt_target_seq_show(struct seq_file *seq, void *v)
1521 {
1522         const struct nf_mttg_trav *trav = seq->private;
1523         const struct xt_target *target;
1524
1525         switch (trav->class) {
1526         case MTTG_TRAV_NFP_UNSPEC:
1527         case MTTG_TRAV_NFP_SPEC:
1528                 if (trav->curr == trav->head)
1529                         return 0;
1530                 target = list_entry(trav->curr, struct xt_target, list);
1531                 if (*target->name)
1532                         seq_printf(seq, "%s\n", target->name);
1533         }
1534         return 0;
1535 }
1536
1537 static const struct seq_operations xt_target_seq_ops = {
1538         .start  = xt_target_seq_start,
1539         .next   = xt_target_seq_next,
1540         .stop   = xt_mttg_seq_stop,
1541         .show   = xt_target_seq_show,
1542 };
1543
1544 static int xt_target_open(struct inode *inode, struct file *file)
1545 {
1546         struct nf_mttg_trav *trav;
1547         trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1548         if (!trav)
1549                 return -ENOMEM;
1550
1551         trav->nfproto = (unsigned long)PDE_DATA(inode);
1552         return 0;
1553 }
1554
1555 static const struct file_operations xt_target_ops = {
1556         .open    = xt_target_open,
1557         .read    = seq_read,
1558         .llseek  = seq_lseek,
1559         .release = seq_release_private,
1560 };
1561
1562 #define FORMAT_TABLES   "_tables_names"
1563 #define FORMAT_MATCHES  "_tables_matches"
1564 #define FORMAT_TARGETS  "_tables_targets"
1565
1566 #endif /* CONFIG_PROC_FS */
1567
1568 /**
1569  * xt_hook_ops_alloc - set up hooks for a new table
1570  * @table:      table with metadata needed to set up hooks
1571  * @fn:         Hook function
1572  *
1573  * This function will create the nf_hook_ops that the x_table needs
1574  * to hand to xt_hook_link_net().
1575  */
1576 struct nf_hook_ops *
1577 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1578 {
1579         unsigned int hook_mask = table->valid_hooks;
1580         uint8_t i, num_hooks = hweight32(hook_mask);
1581         uint8_t hooknum;
1582         struct nf_hook_ops *ops;
1583
1584         if (!num_hooks)
1585                 return ERR_PTR(-EINVAL);
1586
1587         ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1588         if (ops == NULL)
1589                 return ERR_PTR(-ENOMEM);
1590
1591         for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1592              hook_mask >>= 1, ++hooknum) {
1593                 if (!(hook_mask & 1))
1594                         continue;
1595                 ops[i].hook     = fn;
1596                 ops[i].pf       = table->af;
1597                 ops[i].hooknum  = hooknum;
1598                 ops[i].priority = table->priority;
1599                 ++i;
1600         }
1601
1602         return ops;
1603 }
1604 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1605
1606 int xt_proto_init(struct net *net, u_int8_t af)
1607 {
1608 #ifdef CONFIG_PROC_FS
1609         char buf[XT_FUNCTION_MAXNAMELEN];
1610         struct proc_dir_entry *proc;
1611         kuid_t root_uid;
1612         kgid_t root_gid;
1613 #endif
1614
1615         if (af >= ARRAY_SIZE(xt_prefix))
1616                 return -EINVAL;
1617
1618
1619 #ifdef CONFIG_PROC_FS
1620         root_uid = make_kuid(net->user_ns, 0);
1621         root_gid = make_kgid(net->user_ns, 0);
1622
1623         strlcpy(buf, xt_prefix[af], sizeof(buf));
1624         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1625         proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1626                                 (void *)(unsigned long)af);
1627         if (!proc)
1628                 goto out;
1629         if (uid_valid(root_uid) && gid_valid(root_gid))
1630                 proc_set_user(proc, root_uid, root_gid);
1631
1632         strlcpy(buf, xt_prefix[af], sizeof(buf));
1633         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1634         proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1635                                 (void *)(unsigned long)af);
1636         if (!proc)
1637                 goto out_remove_tables;
1638         if (uid_valid(root_uid) && gid_valid(root_gid))
1639                 proc_set_user(proc, root_uid, root_gid);
1640
1641         strlcpy(buf, xt_prefix[af], sizeof(buf));
1642         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1643         proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1644                                 (void *)(unsigned long)af);
1645         if (!proc)
1646                 goto out_remove_matches;
1647         if (uid_valid(root_uid) && gid_valid(root_gid))
1648                 proc_set_user(proc, root_uid, root_gid);
1649 #endif
1650
1651         return 0;
1652
1653 #ifdef CONFIG_PROC_FS
1654 out_remove_matches:
1655         strlcpy(buf, xt_prefix[af], sizeof(buf));
1656         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1657         remove_proc_entry(buf, net->proc_net);
1658
1659 out_remove_tables:
1660         strlcpy(buf, xt_prefix[af], sizeof(buf));
1661         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1662         remove_proc_entry(buf, net->proc_net);
1663 out:
1664         return -1;
1665 #endif
1666 }
1667 EXPORT_SYMBOL_GPL(xt_proto_init);
1668
1669 void xt_proto_fini(struct net *net, u_int8_t af)
1670 {
1671 #ifdef CONFIG_PROC_FS
1672         char buf[XT_FUNCTION_MAXNAMELEN];
1673
1674         strlcpy(buf, xt_prefix[af], sizeof(buf));
1675         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1676         remove_proc_entry(buf, net->proc_net);
1677
1678         strlcpy(buf, xt_prefix[af], sizeof(buf));
1679         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1680         remove_proc_entry(buf, net->proc_net);
1681
1682         strlcpy(buf, xt_prefix[af], sizeof(buf));
1683         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1684         remove_proc_entry(buf, net->proc_net);
1685 #endif /*CONFIG_PROC_FS*/
1686 }
1687 EXPORT_SYMBOL_GPL(xt_proto_fini);
1688
1689 /**
1690  * xt_percpu_counter_alloc - allocate x_tables rule counter
1691  *
1692  * @state: pointer to xt_percpu allocation state
1693  * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1694  *
1695  * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1696  * contain the address of the real (percpu) counter.
1697  *
1698  * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1699  * to fetch the real percpu counter.
1700  *
1701  * To speed up allocation and improve data locality, a 4kb block is
1702  * allocated.
1703  *
1704  * xt_percpu_counter_alloc_state contains the base address of the
1705  * allocated page and the current sub-offset.
1706  *
1707  * returns false on error.
1708  */
1709 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1710                              struct xt_counters *counter)
1711 {
1712         BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1713
1714         if (nr_cpu_ids <= 1)
1715                 return true;
1716
1717         if (!state->mem) {
1718                 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1719                                             XT_PCPU_BLOCK_SIZE);
1720                 if (!state->mem)
1721                         return false;
1722         }
1723         counter->pcnt = (__force unsigned long)(state->mem + state->off);
1724         state->off += sizeof(*counter);
1725         if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1726                 state->mem = NULL;
1727                 state->off = 0;
1728         }
1729         return true;
1730 }
1731 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1732
1733 void xt_percpu_counter_free(struct xt_counters *counters)
1734 {
1735         unsigned long pcnt = counters->pcnt;
1736
1737         if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1738                 free_percpu((void __percpu *)pcnt);
1739 }
1740 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1741
1742 static int __net_init xt_net_init(struct net *net)
1743 {
1744         int i;
1745
1746         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1747                 INIT_LIST_HEAD(&net->xt.tables[i]);
1748         return 0;
1749 }
1750
1751 static void __net_exit xt_net_exit(struct net *net)
1752 {
1753         int i;
1754
1755         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1756                 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1757 }
1758
1759 static struct pernet_operations xt_net_ops = {
1760         .init = xt_net_init,
1761         .exit = xt_net_exit,
1762 };
1763
1764 static int __init xt_init(void)
1765 {
1766         unsigned int i;
1767         int rv;
1768
1769         for_each_possible_cpu(i) {
1770                 seqcount_init(&per_cpu(xt_recseq, i));
1771         }
1772
1773         xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1774         if (!xt)
1775                 return -ENOMEM;
1776
1777         for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1778                 mutex_init(&xt[i].mutex);
1779 #ifdef CONFIG_COMPAT
1780                 mutex_init(&xt[i].compat_mutex);
1781                 xt[i].compat_tab = NULL;
1782 #endif
1783                 INIT_LIST_HEAD(&xt[i].target);
1784                 INIT_LIST_HEAD(&xt[i].match);
1785         }
1786         rv = register_pernet_subsys(&xt_net_ops);
1787         if (rv < 0)
1788                 kfree(xt);
1789         return rv;
1790 }
1791
1792 static void __exit xt_fini(void)
1793 {
1794         unregister_pernet_subsys(&xt_net_ops);
1795         kfree(xt);
1796 }
1797
1798 module_init(xt_init);
1799 module_exit(xt_fini);
1800