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

TOMOYO Linux Cross Reference
Linux/net/netfilter/x_tables.c

Version: ~ [ linux-6.0-rc6 ] ~ [ linux-5.19.10 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.69 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.144 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.214 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.259 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.294 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.329 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 /**
427  * xt_check_proc_name - check that name is suitable for /proc file creation
428  *
429  * @name: file name candidate
430  * @size: length of buffer
431  *
432  * some x_tables modules wish to create a file in /proc.
433  * This function makes sure that the name is suitable for this
434  * purpose, it checks that name is NUL terminated and isn't a 'special'
435  * name, like "..".
436  *
437  * returns negative number on error or 0 if name is useable.
438  */
439 int xt_check_proc_name(const char *name, unsigned int size)
440 {
441         if (name[0] == '\0')
442                 return -EINVAL;
443 
444         if (strnlen(name, size) == size)
445                 return -ENAMETOOLONG;
446 
447         if (strcmp(name, ".") == 0 ||
448             strcmp(name, "..") == 0 ||
449             strchr(name, '/'))
450                 return -EINVAL;
451 
452         return 0;
453 }
454 EXPORT_SYMBOL(xt_check_proc_name);
455 
456 int xt_check_match(struct xt_mtchk_param *par,
457                    unsigned int size, u_int8_t proto, bool inv_proto)
458 {
459         int ret;
460 
461         if (XT_ALIGN(par->match->matchsize) != size &&
462             par->match->matchsize != -1) {
463                 /*
464                  * ebt_among is exempt from centralized matchsize checking
465                  * because it uses a dynamic-size data set.
466                  */
467                 pr_err("%s_tables: %s.%u match: invalid size "
468                        "%u (kernel) != (user) %u\n",
469                        xt_prefix[par->family], par->match->name,
470                        par->match->revision,
471                        XT_ALIGN(par->match->matchsize), size);
472                 return -EINVAL;
473         }
474         if (par->match->table != NULL &&
475             strcmp(par->match->table, par->table) != 0) {
476                 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
477                        xt_prefix[par->family], par->match->name,
478                        par->match->table, par->table);
479                 return -EINVAL;
480         }
481         if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
482                 char used[64], allow[64];
483 
484                 pr_err("%s_tables: %s match: used from hooks %s, but only "
485                        "valid from %s\n",
486                        xt_prefix[par->family], par->match->name,
487                        textify_hooks(used, sizeof(used), par->hook_mask,
488                                      par->family),
489                        textify_hooks(allow, sizeof(allow), par->match->hooks,
490                                      par->family));
491                 return -EINVAL;
492         }
493         if (par->match->proto && (par->match->proto != proto || inv_proto)) {
494                 pr_err("%s_tables: %s match: only valid for protocol %u\n",
495                        xt_prefix[par->family], par->match->name,
496                        par->match->proto);
497                 return -EINVAL;
498         }
499         if (par->match->checkentry != NULL) {
500                 ret = par->match->checkentry(par);
501                 if (ret < 0)
502                         return ret;
503                 else if (ret > 0)
504                         /* Flag up potential errors. */
505                         return -EIO;
506         }
507         return 0;
508 }
509 EXPORT_SYMBOL_GPL(xt_check_match);
510 
511 /** xt_check_entry_match - check that matches end before start of target
512  *
513  * @match: beginning of xt_entry_match
514  * @target: beginning of this rules target (alleged end of matches)
515  * @alignment: alignment requirement of match structures
516  *
517  * Validates that all matches add up to the beginning of the target,
518  * and that each match covers at least the base structure size.
519  *
520  * Return: 0 on success, negative errno on failure.
521  */
522 static int xt_check_entry_match(const char *match, const char *target,
523                                 const size_t alignment)
524 {
525         const struct xt_entry_match *pos;
526         int length = target - match;
527 
528         if (length == 0) /* no matches */
529                 return 0;
530 
531         pos = (struct xt_entry_match *)match;
532         do {
533                 if ((unsigned long)pos % alignment)
534                         return -EINVAL;
535 
536                 if (length < (int)sizeof(struct xt_entry_match))
537                         return -EINVAL;
538 
539                 if (pos->u.match_size < sizeof(struct xt_entry_match))
540                         return -EINVAL;
541 
542                 if (pos->u.match_size > length)
543                         return -EINVAL;
544 
545                 length -= pos->u.match_size;
546                 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
547         } while (length > 0);
548 
549         return 0;
550 }
551 
552 #ifdef CONFIG_COMPAT
553 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
554 {
555         struct xt_af *xp = &xt[af];
556 
557         if (!xp->compat_tab) {
558                 if (!xp->number)
559                         return -EINVAL;
560                 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
561                 if (!xp->compat_tab)
562                         return -ENOMEM;
563                 xp->cur = 0;
564         }
565 
566         if (xp->cur >= xp->number)
567                 return -EINVAL;
568 
569         if (xp->cur)
570                 delta += xp->compat_tab[xp->cur - 1].delta;
571         xp->compat_tab[xp->cur].offset = offset;
572         xp->compat_tab[xp->cur].delta = delta;
573         xp->cur++;
574         return 0;
575 }
576 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
577 
578 void xt_compat_flush_offsets(u_int8_t af)
579 {
580         if (xt[af].compat_tab) {
581                 vfree(xt[af].compat_tab);
582                 xt[af].compat_tab = NULL;
583                 xt[af].number = 0;
584                 xt[af].cur = 0;
585         }
586 }
587 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
588 
589 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
590 {
591         struct compat_delta *tmp = xt[af].compat_tab;
592         int mid, left = 0, right = xt[af].cur - 1;
593 
594         while (left <= right) {
595                 mid = (left + right) >> 1;
596                 if (offset > tmp[mid].offset)
597                         left = mid + 1;
598                 else if (offset < tmp[mid].offset)
599                         right = mid - 1;
600                 else
601                         return mid ? tmp[mid - 1].delta : 0;
602         }
603         return left ? tmp[left - 1].delta : 0;
604 }
605 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
606 
607 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
608 {
609         xt[af].number = number;
610         xt[af].cur = 0;
611 }
612 EXPORT_SYMBOL(xt_compat_init_offsets);
613 
614 int xt_compat_match_offset(const struct xt_match *match)
615 {
616         u_int16_t csize = match->compatsize ? : match->matchsize;
617         return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
618 }
619 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
620 
621 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
622                                unsigned int *size)
623 {
624         const struct xt_match *match = m->u.kernel.match;
625         struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
626         int pad, off = xt_compat_match_offset(match);
627         u_int16_t msize = cm->u.user.match_size;
628         char name[sizeof(m->u.user.name)];
629 
630         m = *dstptr;
631         memcpy(m, cm, sizeof(*cm));
632         if (match->compat_from_user)
633                 match->compat_from_user(m->data, cm->data);
634         else
635                 memcpy(m->data, cm->data, msize - sizeof(*cm));
636         pad = XT_ALIGN(match->matchsize) - match->matchsize;
637         if (pad > 0)
638                 memset(m->data + match->matchsize, 0, pad);
639 
640         msize += off;
641         m->u.user.match_size = msize;
642         strlcpy(name, match->name, sizeof(name));
643         module_put(match->me);
644         strncpy(m->u.user.name, name, sizeof(m->u.user.name));
645 
646         *size += off;
647         *dstptr += msize;
648 }
649 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
650 
651 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE)                      \
652         xt_data_to_user(U->data, K->data,                               \
653                         K->u.kernel.TYPE->usersize,                     \
654                         C_SIZE,                                         \
655                         COMPAT_XT_ALIGN(C_SIZE))
656 
657 int xt_compat_match_to_user(const struct xt_entry_match *m,
658                             void __user **dstptr, unsigned int *size)
659 {
660         const struct xt_match *match = m->u.kernel.match;
661         struct compat_xt_entry_match __user *cm = *dstptr;
662         int off = xt_compat_match_offset(match);
663         u_int16_t msize = m->u.user.match_size - off;
664 
665         if (XT_OBJ_TO_USER(cm, m, match, msize))
666                 return -EFAULT;
667 
668         if (match->compat_to_user) {
669                 if (match->compat_to_user((void __user *)cm->data, m->data))
670                         return -EFAULT;
671         } else {
672                 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
673                         return -EFAULT;
674         }
675 
676         *size -= off;
677         *dstptr += msize;
678         return 0;
679 }
680 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
681 
682 /* non-compat version may have padding after verdict */
683 struct compat_xt_standard_target {
684         struct compat_xt_entry_target t;
685         compat_uint_t verdict;
686 };
687 
688 int xt_compat_check_entry_offsets(const void *base, const char *elems,
689                                   unsigned int target_offset,
690                                   unsigned int next_offset)
691 {
692         long size_of_base_struct = elems - (const char *)base;
693         const struct compat_xt_entry_target *t;
694         const char *e = base;
695 
696         if (target_offset < size_of_base_struct)
697                 return -EINVAL;
698 
699         if (target_offset + sizeof(*t) > next_offset)
700                 return -EINVAL;
701 
702         t = (void *)(e + target_offset);
703         if (t->u.target_size < sizeof(*t))
704                 return -EINVAL;
705 
706         if (target_offset + t->u.target_size > next_offset)
707                 return -EINVAL;
708 
709         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
710             COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
711                 return -EINVAL;
712 
713         /* compat_xt_entry match has less strict alignment requirements,
714          * otherwise they are identical.  In case of padding differences
715          * we need to add compat version of xt_check_entry_match.
716          */
717         BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
718 
719         return xt_check_entry_match(elems, base + target_offset,
720                                     __alignof__(struct compat_xt_entry_match));
721 }
722 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
723 #endif /* CONFIG_COMPAT */
724 
725 /**
726  * xt_check_entry_offsets - validate arp/ip/ip6t_entry
727  *
728  * @base: pointer to arp/ip/ip6t_entry
729  * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
730  * @target_offset: the arp/ip/ip6_t->target_offset
731  * @next_offset: the arp/ip/ip6_t->next_offset
732  *
733  * validates that target_offset and next_offset are sane and that all
734  * match sizes (if any) align with the target offset.
735  *
736  * This function does not validate the targets or matches themselves, it
737  * only tests that all the offsets and sizes are correct, that all
738  * match structures are aligned, and that the last structure ends where
739  * the target structure begins.
740  *
741  * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
742  *
743  * The arp/ip/ip6t_entry structure @base must have passed following tests:
744  * - it must point to a valid memory location
745  * - base to base + next_offset must be accessible, i.e. not exceed allocated
746  *   length.
747  *
748  * A well-formed entry looks like this:
749  *
750  * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
751  * e->elems[]-----'                              |               |
752  *                matchsize                      |               |
753  *                                matchsize      |               |
754  *                                               |               |
755  * target_offset---------------------------------'               |
756  * next_offset---------------------------------------------------'
757  *
758  * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
759  *          This is where matches (if any) and the target reside.
760  * target_offset: beginning of target.
761  * next_offset: start of the next rule; also: size of this rule.
762  * Since targets have a minimum size, target_offset + minlen <= next_offset.
763  *
764  * Every match stores its size, sum of sizes must not exceed target_offset.
765  *
766  * Return: 0 on success, negative errno on failure.
767  */
768 int xt_check_entry_offsets(const void *base,
769                            const char *elems,
770                            unsigned int target_offset,
771                            unsigned int next_offset)
772 {
773         long size_of_base_struct = elems - (const char *)base;
774         const struct xt_entry_target *t;
775         const char *e = base;
776 
777         /* target start is within the ip/ip6/arpt_entry struct */
778         if (target_offset < size_of_base_struct)
779                 return -EINVAL;
780 
781         if (target_offset + sizeof(*t) > next_offset)
782                 return -EINVAL;
783 
784         t = (void *)(e + target_offset);
785         if (t->u.target_size < sizeof(*t))
786                 return -EINVAL;
787 
788         if (target_offset + t->u.target_size > next_offset)
789                 return -EINVAL;
790 
791         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
792             XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
793                 return -EINVAL;
794 
795         return xt_check_entry_match(elems, base + target_offset,
796                                     __alignof__(struct xt_entry_match));
797 }
798 EXPORT_SYMBOL(xt_check_entry_offsets);
799 
800 /**
801  * xt_alloc_entry_offsets - allocate array to store rule head offsets
802  *
803  * @size: number of entries
804  *
805  * Return: NULL or kmalloc'd or vmalloc'd array
806  */
807 unsigned int *xt_alloc_entry_offsets(unsigned int size)
808 {
809         return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
810 
811 }
812 EXPORT_SYMBOL(xt_alloc_entry_offsets);
813 
814 /**
815  * xt_find_jump_offset - check if target is a valid jump offset
816  *
817  * @offsets: array containing all valid rule start offsets of a rule blob
818  * @target: the jump target to search for
819  * @size: entries in @offset
820  */
821 bool xt_find_jump_offset(const unsigned int *offsets,
822                          unsigned int target, unsigned int size)
823 {
824         int m, low = 0, hi = size;
825 
826         while (hi > low) {
827                 m = (low + hi) / 2u;
828 
829                 if (offsets[m] > target)
830                         hi = m;
831                 else if (offsets[m] < target)
832                         low = m + 1;
833                 else
834                         return true;
835         }
836 
837         return false;
838 }
839 EXPORT_SYMBOL(xt_find_jump_offset);
840 
841 int xt_check_target(struct xt_tgchk_param *par,
842                     unsigned int size, u_int8_t proto, bool inv_proto)
843 {
844         int ret;
845 
846         if (XT_ALIGN(par->target->targetsize) != size) {
847                 pr_err("%s_tables: %s.%u target: invalid size "
848                        "%u (kernel) != (user) %u\n",
849                        xt_prefix[par->family], par->target->name,
850                        par->target->revision,
851                        XT_ALIGN(par->target->targetsize), size);
852                 return -EINVAL;
853         }
854         if (par->target->table != NULL &&
855             strcmp(par->target->table, par->table) != 0) {
856                 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
857                        xt_prefix[par->family], par->target->name,
858                        par->target->table, par->table);
859                 return -EINVAL;
860         }
861         if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
862                 char used[64], allow[64];
863 
864                 pr_err("%s_tables: %s target: used from hooks %s, but only "
865                        "usable from %s\n",
866                        xt_prefix[par->family], par->target->name,
867                        textify_hooks(used, sizeof(used), par->hook_mask,
868                                      par->family),
869                        textify_hooks(allow, sizeof(allow), par->target->hooks,
870                                      par->family));
871                 return -EINVAL;
872         }
873         if (par->target->proto && (par->target->proto != proto || inv_proto)) {
874                 pr_err("%s_tables: %s target: only valid for protocol %u\n",
875                        xt_prefix[par->family], par->target->name,
876                        par->target->proto);
877                 return -EINVAL;
878         }
879         if (par->target->checkentry != NULL) {
880                 ret = par->target->checkentry(par);
881                 if (ret < 0)
882                         return ret;
883                 else if (ret > 0)
884                         /* Flag up potential errors. */
885                         return -EIO;
886         }
887         return 0;
888 }
889 EXPORT_SYMBOL_GPL(xt_check_target);
890 
891 /**
892  * xt_copy_counters_from_user - copy counters and metadata from userspace
893  *
894  * @user: src pointer to userspace memory
895  * @len: alleged size of userspace memory
896  * @info: where to store the xt_counters_info metadata
897  * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
898  *
899  * Copies counter meta data from @user and stores it in @info.
900  *
901  * vmallocs memory to hold the counters, then copies the counter data
902  * from @user to the new memory and returns a pointer to it.
903  *
904  * If @compat is true, @info gets converted automatically to the 64bit
905  * representation.
906  *
907  * The metadata associated with the counters is stored in @info.
908  *
909  * Return: returns pointer that caller has to test via IS_ERR().
910  * If IS_ERR is false, caller has to vfree the pointer.
911  */
912 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
913                                  struct xt_counters_info *info, bool compat)
914 {
915         void *mem;
916         u64 size;
917 
918 #ifdef CONFIG_COMPAT
919         if (compat) {
920                 /* structures only differ in size due to alignment */
921                 struct compat_xt_counters_info compat_tmp;
922 
923                 if (len <= sizeof(compat_tmp))
924                         return ERR_PTR(-EINVAL);
925 
926                 len -= sizeof(compat_tmp);
927                 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
928                         return ERR_PTR(-EFAULT);
929 
930                 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
931                 info->num_counters = compat_tmp.num_counters;
932                 user += sizeof(compat_tmp);
933         } else
934 #endif
935         {
936                 if (len <= sizeof(*info))
937                         return ERR_PTR(-EINVAL);
938 
939                 len -= sizeof(*info);
940                 if (copy_from_user(info, user, sizeof(*info)) != 0)
941                         return ERR_PTR(-EFAULT);
942 
943                 user += sizeof(*info);
944         }
945         info->name[sizeof(info->name) - 1] = '\0';
946 
947         size = sizeof(struct xt_counters);
948         size *= info->num_counters;
949 
950         if (size != (u64)len)
951                 return ERR_PTR(-EINVAL);
952 
953         mem = vmalloc(len);
954         if (!mem)
955                 return ERR_PTR(-ENOMEM);
956 
957         if (copy_from_user(mem, user, len) == 0)
958                 return mem;
959 
960         vfree(mem);
961         return ERR_PTR(-EFAULT);
962 }
963 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
964 
965 #ifdef CONFIG_COMPAT
966 int xt_compat_target_offset(const struct xt_target *target)
967 {
968         u_int16_t csize = target->compatsize ? : target->targetsize;
969         return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
970 }
971 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
972 
973 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
974                                 unsigned int *size)
975 {
976         const struct xt_target *target = t->u.kernel.target;
977         struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
978         int pad, off = xt_compat_target_offset(target);
979         u_int16_t tsize = ct->u.user.target_size;
980         char name[sizeof(t->u.user.name)];
981 
982         t = *dstptr;
983         memcpy(t, ct, sizeof(*ct));
984         if (target->compat_from_user)
985                 target->compat_from_user(t->data, ct->data);
986         else
987                 memcpy(t->data, ct->data, tsize - sizeof(*ct));
988         pad = XT_ALIGN(target->targetsize) - target->targetsize;
989         if (pad > 0)
990                 memset(t->data + target->targetsize, 0, pad);
991 
992         tsize += off;
993         t->u.user.target_size = tsize;
994         strlcpy(name, target->name, sizeof(name));
995         module_put(target->me);
996         strncpy(t->u.user.name, name, sizeof(t->u.user.name));
997 
998         *size += off;
999         *dstptr += tsize;
1000 }
1001 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1002 
1003 int xt_compat_target_to_user(const struct xt_entry_target *t,
1004                              void __user **dstptr, unsigned int *size)
1005 {
1006         const struct xt_target *target = t->u.kernel.target;
1007         struct compat_xt_entry_target __user *ct = *dstptr;
1008         int off = xt_compat_target_offset(target);
1009         u_int16_t tsize = t->u.user.target_size - off;
1010 
1011         if (XT_OBJ_TO_USER(ct, t, target, tsize))
1012                 return -EFAULT;
1013 
1014         if (target->compat_to_user) {
1015                 if (target->compat_to_user((void __user *)ct->data, t->data))
1016                         return -EFAULT;
1017         } else {
1018                 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1019                         return -EFAULT;
1020         }
1021 
1022         *size -= off;
1023         *dstptr += tsize;
1024         return 0;
1025 }
1026 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1027 #endif
1028 
1029 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1030 {
1031         struct xt_table_info *info = NULL;
1032         size_t sz = sizeof(*info) + size;
1033 
1034         if (sz < sizeof(*info))
1035                 return NULL;
1036 
1037         /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
1038         if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
1039                 return NULL;
1040 
1041         /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1042          * work reasonably well if sz is too large and bail out rather
1043          * than shoot all processes down before realizing there is nothing
1044          * more to reclaim.
1045          */
1046         info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
1047         if (!info)
1048                 return NULL;
1049 
1050         memset(info, 0, sizeof(*info));
1051         info->size = size;
1052         return info;
1053 }
1054 EXPORT_SYMBOL(xt_alloc_table_info);
1055 
1056 void xt_free_table_info(struct xt_table_info *info)
1057 {
1058         int cpu;
1059 
1060         if (info->jumpstack != NULL) {
1061                 for_each_possible_cpu(cpu)
1062                         kvfree(info->jumpstack[cpu]);
1063                 kvfree(info->jumpstack);
1064         }
1065 
1066         kvfree(info);
1067 }
1068 EXPORT_SYMBOL(xt_free_table_info);
1069 
1070 /* Find table by name, grabs mutex & ref.  Returns NULL on error. */
1071 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1072                                     const char *name)
1073 {
1074         struct xt_table *t, *found = NULL;
1075 
1076         mutex_lock(&xt[af].mutex);
1077         list_for_each_entry(t, &net->xt.tables[af], list)
1078                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1079                         return t;
1080 
1081         if (net == &init_net)
1082                 goto out;
1083 
1084         /* Table doesn't exist in this netns, re-try init */
1085         list_for_each_entry(t, &init_net.xt.tables[af], list) {
1086                 if (strcmp(t->name, name))
1087                         continue;
1088                 if (!try_module_get(t->me)) {
1089                         mutex_unlock(&xt[af].mutex);
1090                         return NULL;
1091                 }
1092 
1093                 mutex_unlock(&xt[af].mutex);
1094                 if (t->table_init(net) != 0) {
1095                         module_put(t->me);
1096                         return NULL;
1097                 }
1098 
1099                 found = t;
1100 
1101                 mutex_lock(&xt[af].mutex);
1102                 break;
1103         }
1104 
1105         if (!found)
1106                 goto out;
1107 
1108         /* and once again: */
1109         list_for_each_entry(t, &net->xt.tables[af], list)
1110                 if (strcmp(t->name, name) == 0)
1111                         return t;
1112 
1113         module_put(found->me);
1114  out:
1115         mutex_unlock(&xt[af].mutex);
1116         return NULL;
1117 }
1118 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1119 
1120 void xt_table_unlock(struct xt_table *table)
1121 {
1122         mutex_unlock(&xt[table->af].mutex);
1123 }
1124 EXPORT_SYMBOL_GPL(xt_table_unlock);
1125 
1126 #ifdef CONFIG_COMPAT
1127 void xt_compat_lock(u_int8_t af)
1128 {
1129         mutex_lock(&xt[af].compat_mutex);
1130 }
1131 EXPORT_SYMBOL_GPL(xt_compat_lock);
1132 
1133 void xt_compat_unlock(u_int8_t af)
1134 {
1135         mutex_unlock(&xt[af].compat_mutex);
1136 }
1137 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1138 #endif
1139 
1140 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1141 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1142 
1143 struct static_key xt_tee_enabled __read_mostly;
1144 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1145 
1146 static int xt_jumpstack_alloc(struct xt_table_info *i)
1147 {
1148         unsigned int size;
1149         int cpu;
1150 
1151         size = sizeof(void **) * nr_cpu_ids;
1152         if (size > PAGE_SIZE)
1153                 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1154         else
1155                 i->jumpstack = kzalloc(size, GFP_KERNEL);
1156         if (i->jumpstack == NULL)
1157                 return -ENOMEM;
1158 
1159         /* ruleset without jumps -- no stack needed */
1160         if (i->stacksize == 0)
1161                 return 0;
1162 
1163         /* Jumpstack needs to be able to record two full callchains, one
1164          * from the first rule set traversal, plus one table reentrancy
1165          * via -j TEE without clobbering the callchain that brought us to
1166          * TEE target.
1167          *
1168          * This is done by allocating two jumpstacks per cpu, on reentry
1169          * the upper half of the stack is used.
1170          *
1171          * see the jumpstack setup in ipt_do_table() for more details.
1172          */
1173         size = sizeof(void *) * i->stacksize * 2u;
1174         for_each_possible_cpu(cpu) {
1175                 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1176                         cpu_to_node(cpu));
1177                 if (i->jumpstack[cpu] == NULL)
1178                         /*
1179                          * Freeing will be done later on by the callers. The
1180                          * chain is: xt_replace_table -> __do_replace ->
1181                          * do_replace -> xt_free_table_info.
1182                          */
1183                         return -ENOMEM;
1184         }
1185 
1186         return 0;
1187 }
1188 
1189 struct xt_table_info *
1190 xt_replace_table(struct xt_table *table,
1191               unsigned int num_counters,
1192               struct xt_table_info *newinfo,
1193               int *error)
1194 {
1195         struct xt_table_info *private;
1196         unsigned int cpu;
1197         int ret;
1198 
1199         ret = xt_jumpstack_alloc(newinfo);
1200         if (ret < 0) {
1201                 *error = ret;
1202                 return NULL;
1203         }
1204 
1205         /* Do the substitution. */
1206         local_bh_disable();
1207         private = table->private;
1208 
1209         /* Check inside lock: is the old number correct? */
1210         if (num_counters != private->number) {
1211                 pr_debug("num_counters != table->private->number (%u/%u)\n",
1212                          num_counters, private->number);
1213                 local_bh_enable();
1214                 *error = -EAGAIN;
1215                 return NULL;
1216         }
1217 
1218         newinfo->initial_entries = private->initial_entries;
1219         /*
1220          * Ensure contents of newinfo are visible before assigning to
1221          * private.
1222          */
1223         smp_wmb();
1224         table->private = newinfo;
1225 
1226         /* make sure all cpus see new ->private value */
1227         smp_wmb();
1228 
1229         /*
1230          * Even though table entries have now been swapped, other CPU's
1231          * may still be using the old entries...
1232          */
1233         local_bh_enable();
1234 
1235         /* ... so wait for even xt_recseq on all cpus */
1236         for_each_possible_cpu(cpu) {
1237                 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1238                 u32 seq = raw_read_seqcount(s);
1239 
1240                 if (seq & 1) {
1241                         do {
1242                                 cond_resched();
1243                                 cpu_relax();
1244                         } while (seq == raw_read_seqcount(s));
1245                 }
1246         }
1247 
1248 #ifdef CONFIG_AUDIT
1249         if (audit_enabled) {
1250                 audit_log(current->audit_context, GFP_KERNEL,
1251                           AUDIT_NETFILTER_CFG,
1252                           "table=%s family=%u entries=%u",
1253                           table->name, table->af, private->number);
1254         }
1255 #endif
1256 
1257         return private;
1258 }
1259 EXPORT_SYMBOL_GPL(xt_replace_table);
1260 
1261 struct xt_table *xt_register_table(struct net *net,
1262                                    const struct xt_table *input_table,
1263                                    struct xt_table_info *bootstrap,
1264                                    struct xt_table_info *newinfo)
1265 {
1266         int ret;
1267         struct xt_table_info *private;
1268         struct xt_table *t, *table;
1269 
1270         /* Don't add one object to multiple lists. */
1271         table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1272         if (!table) {
1273                 ret = -ENOMEM;
1274                 goto out;
1275         }
1276 
1277         mutex_lock(&xt[table->af].mutex);
1278         /* Don't autoload: we'd eat our tail... */
1279         list_for_each_entry(t, &net->xt.tables[table->af], list) {
1280                 if (strcmp(t->name, table->name) == 0) {
1281                         ret = -EEXIST;
1282                         goto unlock;
1283                 }
1284         }
1285 
1286         /* Simplifies replace_table code. */
1287         table->private = bootstrap;
1288 
1289         if (!xt_replace_table(table, 0, newinfo, &ret))
1290                 goto unlock;
1291 
1292         private = table->private;
1293         pr_debug("table->private->number = %u\n", private->number);
1294 
1295         /* save number of initial entries */
1296         private->initial_entries = private->number;
1297 
1298         list_add(&table->list, &net->xt.tables[table->af]);
1299         mutex_unlock(&xt[table->af].mutex);
1300         return table;
1301 
1302 unlock:
1303         mutex_unlock(&xt[table->af].mutex);
1304         kfree(table);
1305 out:
1306         return ERR_PTR(ret);
1307 }
1308 EXPORT_SYMBOL_GPL(xt_register_table);
1309 
1310 void *xt_unregister_table(struct xt_table *table)
1311 {
1312         struct xt_table_info *private;
1313 
1314         mutex_lock(&xt[table->af].mutex);
1315         private = table->private;
1316         list_del(&table->list);
1317         mutex_unlock(&xt[table->af].mutex);
1318         kfree(table);
1319 
1320         return private;
1321 }
1322 EXPORT_SYMBOL_GPL(xt_unregister_table);
1323 
1324 #ifdef CONFIG_PROC_FS
1325 struct xt_names_priv {
1326         struct seq_net_private p;
1327         u_int8_t af;
1328 };
1329 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1330 {
1331         struct xt_names_priv *priv = seq->private;
1332         struct net *net = seq_file_net(seq);
1333         u_int8_t af = priv->af;
1334 
1335         mutex_lock(&xt[af].mutex);
1336         return seq_list_start(&net->xt.tables[af], *pos);
1337 }
1338 
1339 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1340 {
1341         struct xt_names_priv *priv = seq->private;
1342         struct net *net = seq_file_net(seq);
1343         u_int8_t af = priv->af;
1344 
1345         return seq_list_next(v, &net->xt.tables[af], pos);
1346 }
1347 
1348 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1349 {
1350         struct xt_names_priv *priv = seq->private;
1351         u_int8_t af = priv->af;
1352 
1353         mutex_unlock(&xt[af].mutex);
1354 }
1355 
1356 static int xt_table_seq_show(struct seq_file *seq, void *v)
1357 {
1358         struct xt_table *table = list_entry(v, struct xt_table, list);
1359 
1360         if (*table->name)
1361                 seq_printf(seq, "%s\n", table->name);
1362         return 0;
1363 }
1364 
1365 static const struct seq_operations xt_table_seq_ops = {
1366         .start  = xt_table_seq_start,
1367         .next   = xt_table_seq_next,
1368         .stop   = xt_table_seq_stop,
1369         .show   = xt_table_seq_show,
1370 };
1371 
1372 static int xt_table_open(struct inode *inode, struct file *file)
1373 {
1374         int ret;
1375         struct xt_names_priv *priv;
1376 
1377         ret = seq_open_net(inode, file, &xt_table_seq_ops,
1378                            sizeof(struct xt_names_priv));
1379         if (!ret) {
1380                 priv = ((struct seq_file *)file->private_data)->private;
1381                 priv->af = (unsigned long)PDE_DATA(inode);
1382         }
1383         return ret;
1384 }
1385 
1386 static const struct file_operations xt_table_ops = {
1387         .owner   = THIS_MODULE,
1388         .open    = xt_table_open,
1389         .read    = seq_read,
1390         .llseek  = seq_lseek,
1391         .release = seq_release_net,
1392 };
1393 
1394 /*
1395  * Traverse state for ip{,6}_{tables,matches} for helping crossing
1396  * the multi-AF mutexes.
1397  */
1398 struct nf_mttg_trav {
1399         struct list_head *head, *curr;
1400         uint8_t class, nfproto;
1401 };
1402 
1403 enum {
1404         MTTG_TRAV_INIT,
1405         MTTG_TRAV_NFP_UNSPEC,
1406         MTTG_TRAV_NFP_SPEC,
1407         MTTG_TRAV_DONE,
1408 };
1409 
1410 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1411     bool is_target)
1412 {
1413         static const uint8_t next_class[] = {
1414                 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1415                 [MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1416         };
1417         struct nf_mttg_trav *trav = seq->private;
1418 
1419         switch (trav->class) {
1420         case MTTG_TRAV_INIT:
1421                 trav->class = MTTG_TRAV_NFP_UNSPEC;
1422                 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1423                 trav->head = trav->curr = is_target ?
1424                         &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1425                 break;
1426         case MTTG_TRAV_NFP_UNSPEC:
1427                 trav->curr = trav->curr->next;
1428                 if (trav->curr != trav->head)
1429                         break;
1430                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1431                 mutex_lock(&xt[trav->nfproto].mutex);
1432                 trav->head = trav->curr = is_target ?
1433                         &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1434                 trav->class = next_class[trav->class];
1435                 break;
1436         case MTTG_TRAV_NFP_SPEC:
1437                 trav->curr = trav->curr->next;
1438                 if (trav->curr != trav->head)
1439                         break;
1440                 /* fallthru, _stop will unlock */
1441         default:
1442                 return NULL;
1443         }
1444 
1445         if (ppos != NULL)
1446                 ++*ppos;
1447         return trav;
1448 }
1449 
1450 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1451     bool is_target)
1452 {
1453         struct nf_mttg_trav *trav = seq->private;
1454         unsigned int j;
1455 
1456         trav->class = MTTG_TRAV_INIT;
1457         for (j = 0; j < *pos; ++j)
1458                 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1459                         return NULL;
1460         return trav;
1461 }
1462 
1463 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1464 {
1465         struct nf_mttg_trav *trav = seq->private;
1466 
1467         switch (trav->class) {
1468         case MTTG_TRAV_NFP_UNSPEC:
1469                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1470                 break;
1471         case MTTG_TRAV_NFP_SPEC:
1472                 mutex_unlock(&xt[trav->nfproto].mutex);
1473                 break;
1474         }
1475 }
1476 
1477 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1478 {
1479         return xt_mttg_seq_start(seq, pos, false);
1480 }
1481 
1482 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1483 {
1484         return xt_mttg_seq_next(seq, v, ppos, false);
1485 }
1486 
1487 static int xt_match_seq_show(struct seq_file *seq, void *v)
1488 {
1489         const struct nf_mttg_trav *trav = seq->private;
1490         const struct xt_match *match;
1491 
1492         switch (trav->class) {
1493         case MTTG_TRAV_NFP_UNSPEC:
1494         case MTTG_TRAV_NFP_SPEC:
1495                 if (trav->curr == trav->head)
1496                         return 0;
1497                 match = list_entry(trav->curr, struct xt_match, list);
1498                 if (*match->name)
1499                         seq_printf(seq, "%s\n", match->name);
1500         }
1501         return 0;
1502 }
1503 
1504 static const struct seq_operations xt_match_seq_ops = {
1505         .start  = xt_match_seq_start,
1506         .next   = xt_match_seq_next,
1507         .stop   = xt_mttg_seq_stop,
1508         .show   = xt_match_seq_show,
1509 };
1510 
1511 static int xt_match_open(struct inode *inode, struct file *file)
1512 {
1513         struct nf_mttg_trav *trav;
1514         trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1515         if (!trav)
1516                 return -ENOMEM;
1517 
1518         trav->nfproto = (unsigned long)PDE_DATA(inode);
1519         return 0;
1520 }
1521 
1522 static const struct file_operations xt_match_ops = {
1523         .owner   = THIS_MODULE,
1524         .open    = xt_match_open,
1525         .read    = seq_read,
1526         .llseek  = seq_lseek,
1527         .release = seq_release_private,
1528 };
1529 
1530 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1531 {
1532         return xt_mttg_seq_start(seq, pos, true);
1533 }
1534 
1535 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1536 {
1537         return xt_mttg_seq_next(seq, v, ppos, true);
1538 }
1539 
1540 static int xt_target_seq_show(struct seq_file *seq, void *v)
1541 {
1542         const struct nf_mttg_trav *trav = seq->private;
1543         const struct xt_target *target;
1544 
1545         switch (trav->class) {
1546         case MTTG_TRAV_NFP_UNSPEC:
1547         case MTTG_TRAV_NFP_SPEC:
1548                 if (trav->curr == trav->head)
1549                         return 0;
1550                 target = list_entry(trav->curr, struct xt_target, list);
1551                 if (*target->name)
1552                         seq_printf(seq, "%s\n", target->name);
1553         }
1554         return 0;
1555 }
1556 
1557 static const struct seq_operations xt_target_seq_ops = {
1558         .start  = xt_target_seq_start,
1559         .next   = xt_target_seq_next,
1560         .stop   = xt_mttg_seq_stop,
1561         .show   = xt_target_seq_show,
1562 };
1563 
1564 static int xt_target_open(struct inode *inode, struct file *file)
1565 {
1566         struct nf_mttg_trav *trav;
1567         trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1568         if (!trav)
1569                 return -ENOMEM;
1570 
1571         trav->nfproto = (unsigned long)PDE_DATA(inode);
1572         return 0;
1573 }
1574 
1575 static const struct file_operations xt_target_ops = {
1576         .owner   = THIS_MODULE,
1577         .open    = xt_target_open,
1578         .read    = seq_read,
1579         .llseek  = seq_lseek,
1580         .release = seq_release_private,
1581 };
1582 
1583 #define FORMAT_TABLES   "_tables_names"
1584 #define FORMAT_MATCHES  "_tables_matches"
1585 #define FORMAT_TARGETS  "_tables_targets"
1586 
1587 #endif /* CONFIG_PROC_FS */
1588 
1589 /**
1590  * xt_hook_ops_alloc - set up hooks for a new table
1591  * @table:      table with metadata needed to set up hooks
1592  * @fn:         Hook function
1593  *
1594  * This function will create the nf_hook_ops that the x_table needs
1595  * to hand to xt_hook_link_net().
1596  */
1597 struct nf_hook_ops *
1598 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1599 {
1600         unsigned int hook_mask = table->valid_hooks;
1601         uint8_t i, num_hooks = hweight32(hook_mask);
1602         uint8_t hooknum;
1603         struct nf_hook_ops *ops;
1604 
1605         if (!num_hooks)
1606                 return ERR_PTR(-EINVAL);
1607 
1608         ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1609         if (ops == NULL)
1610                 return ERR_PTR(-ENOMEM);
1611 
1612         for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1613              hook_mask >>= 1, ++hooknum) {
1614                 if (!(hook_mask & 1))
1615                         continue;
1616                 ops[i].hook     = fn;
1617                 ops[i].pf       = table->af;
1618                 ops[i].hooknum  = hooknum;
1619                 ops[i].priority = table->priority;
1620                 ++i;
1621         }
1622 
1623         return ops;
1624 }
1625 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1626 
1627 int xt_proto_init(struct net *net, u_int8_t af)
1628 {
1629 #ifdef CONFIG_PROC_FS
1630         char buf[XT_FUNCTION_MAXNAMELEN];
1631         struct proc_dir_entry *proc;
1632         kuid_t root_uid;
1633         kgid_t root_gid;
1634 #endif
1635 
1636         if (af >= ARRAY_SIZE(xt_prefix))
1637                 return -EINVAL;
1638 
1639 
1640 #ifdef CONFIG_PROC_FS
1641         root_uid = make_kuid(net->user_ns, 0);
1642         root_gid = make_kgid(net->user_ns, 0);
1643 
1644         strlcpy(buf, xt_prefix[af], sizeof(buf));
1645         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1646         proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1647                                 (void *)(unsigned long)af);
1648         if (!proc)
1649                 goto out;
1650         if (uid_valid(root_uid) && gid_valid(root_gid))
1651                 proc_set_user(proc, root_uid, root_gid);
1652 
1653         strlcpy(buf, xt_prefix[af], sizeof(buf));
1654         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1655         proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1656                                 (void *)(unsigned long)af);
1657         if (!proc)
1658                 goto out_remove_tables;
1659         if (uid_valid(root_uid) && gid_valid(root_gid))
1660                 proc_set_user(proc, root_uid, root_gid);
1661 
1662         strlcpy(buf, xt_prefix[af], sizeof(buf));
1663         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1664         proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1665                                 (void *)(unsigned long)af);
1666         if (!proc)
1667                 goto out_remove_matches;
1668         if (uid_valid(root_uid) && gid_valid(root_gid))
1669                 proc_set_user(proc, root_uid, root_gid);
1670 #endif
1671 
1672         return 0;
1673 
1674 #ifdef CONFIG_PROC_FS
1675 out_remove_matches:
1676         strlcpy(buf, xt_prefix[af], sizeof(buf));
1677         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1678         remove_proc_entry(buf, net->proc_net);
1679 
1680 out_remove_tables:
1681         strlcpy(buf, xt_prefix[af], sizeof(buf));
1682         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1683         remove_proc_entry(buf, net->proc_net);
1684 out:
1685         return -1;
1686 #endif
1687 }
1688 EXPORT_SYMBOL_GPL(xt_proto_init);
1689 
1690 void xt_proto_fini(struct net *net, u_int8_t af)
1691 {
1692 #ifdef CONFIG_PROC_FS
1693         char buf[XT_FUNCTION_MAXNAMELEN];
1694 
1695         strlcpy(buf, xt_prefix[af], sizeof(buf));
1696         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1697         remove_proc_entry(buf, net->proc_net);
1698 
1699         strlcpy(buf, xt_prefix[af], sizeof(buf));
1700         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1701         remove_proc_entry(buf, net->proc_net);
1702 
1703         strlcpy(buf, xt_prefix[af], sizeof(buf));
1704         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1705         remove_proc_entry(buf, net->proc_net);
1706 #endif /*CONFIG_PROC_FS*/
1707 }
1708 EXPORT_SYMBOL_GPL(xt_proto_fini);
1709 
1710 /**
1711  * xt_percpu_counter_alloc - allocate x_tables rule counter
1712  *
1713  * @state: pointer to xt_percpu allocation state
1714  * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1715  *
1716  * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1717  * contain the address of the real (percpu) counter.
1718  *
1719  * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1720  * to fetch the real percpu counter.
1721  *
1722  * To speed up allocation and improve data locality, a 4kb block is
1723  * allocated.
1724  *
1725  * xt_percpu_counter_alloc_state contains the base address of the
1726  * allocated page and the current sub-offset.
1727  *
1728  * returns false on error.
1729  */
1730 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1731                              struct xt_counters *counter)
1732 {
1733         BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1734 
1735         if (nr_cpu_ids <= 1)
1736                 return true;
1737 
1738         if (!state->mem) {
1739                 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1740                                             XT_PCPU_BLOCK_SIZE);
1741                 if (!state->mem)
1742                         return false;
1743         }
1744         counter->pcnt = (__force unsigned long)(state->mem + state->off);
1745         state->off += sizeof(*counter);
1746         if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1747                 state->mem = NULL;
1748                 state->off = 0;
1749         }
1750         return true;
1751 }
1752 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1753 
1754 void xt_percpu_counter_free(struct xt_counters *counters)
1755 {
1756         unsigned long pcnt = counters->pcnt;
1757 
1758         if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1759                 free_percpu((void __percpu *)pcnt);
1760 }
1761 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1762 
1763 static int __net_init xt_net_init(struct net *net)
1764 {
1765         int i;
1766 
1767         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1768                 INIT_LIST_HEAD(&net->xt.tables[i]);
1769         return 0;
1770 }
1771 
1772 static void __net_exit xt_net_exit(struct net *net)
1773 {
1774         int i;
1775 
1776         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1777                 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1778 }
1779 
1780 static struct pernet_operations xt_net_ops = {
1781         .init = xt_net_init,
1782         .exit = xt_net_exit,
1783 };
1784 
1785 static int __init xt_init(void)
1786 {
1787         unsigned int i;
1788         int rv;
1789 
1790         for_each_possible_cpu(i) {
1791                 seqcount_init(&per_cpu(xt_recseq, i));
1792         }
1793 
1794         xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1795         if (!xt)
1796                 return -ENOMEM;
1797 
1798         for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1799                 mutex_init(&xt[i].mutex);
1800 #ifdef CONFIG_COMPAT
1801                 mutex_init(&xt[i].compat_mutex);
1802                 xt[i].compat_tab = NULL;
1803 #endif
1804                 INIT_LIST_HEAD(&xt[i].target);
1805                 INIT_LIST_HEAD(&xt[i].match);
1806         }
1807         rv = register_pernet_subsys(&xt_net_ops);
1808         if (rv < 0)
1809                 kfree(xt);
1810         return rv;
1811 }
1812 
1813 static void __exit xt_fini(void)
1814 {
1815         unregister_pernet_subsys(&xt_net_ops);
1816         kfree(xt);
1817 }
1818 
1819 module_init(xt_init);
1820 module_exit(xt_fini);
1821 
1822 

~ [ 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