Merge tag 'ipvs-fixes-for-v4.5' of https://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.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 SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
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 match = xt_find_match(nfproto, name, revision);
213 if (IS_ERR(match)) {
214 request_module("%st_%s", xt_prefix[nfproto], name);
215 match = xt_find_match(nfproto, name, revision);
216 }
217
218 return match;
219 }
220 EXPORT_SYMBOL_GPL(xt_request_find_match);
221
222 /* Find target, grabs ref. Returns ERR_PTR() on error. */
223 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
224 {
225 struct xt_target *t;
226 int err = -ENOENT;
227
228 mutex_lock(&xt[af].mutex);
229 list_for_each_entry(t, &xt[af].target, list) {
230 if (strcmp(t->name, name) == 0) {
231 if (t->revision == revision) {
232 if (try_module_get(t->me)) {
233 mutex_unlock(&xt[af].mutex);
234 return t;
235 }
236 } else
237 err = -EPROTOTYPE; /* Found something. */
238 }
239 }
240 mutex_unlock(&xt[af].mutex);
241
242 if (af != NFPROTO_UNSPEC)
243 /* Try searching again in the family-independent list */
244 return xt_find_target(NFPROTO_UNSPEC, name, revision);
245
246 return ERR_PTR(err);
247 }
248 EXPORT_SYMBOL(xt_find_target);
249
250 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
251 {
252 struct xt_target *target;
253
254 target = xt_find_target(af, name, revision);
255 if (IS_ERR(target)) {
256 request_module("%st_%s", xt_prefix[af], name);
257 target = xt_find_target(af, name, revision);
258 }
259
260 return target;
261 }
262 EXPORT_SYMBOL_GPL(xt_request_find_target);
263
264 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
265 {
266 const struct xt_match *m;
267 int have_rev = 0;
268
269 list_for_each_entry(m, &xt[af].match, list) {
270 if (strcmp(m->name, name) == 0) {
271 if (m->revision > *bestp)
272 *bestp = m->revision;
273 if (m->revision == revision)
274 have_rev = 1;
275 }
276 }
277
278 if (af != NFPROTO_UNSPEC && !have_rev)
279 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
280
281 return have_rev;
282 }
283
284 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
285 {
286 const struct xt_target *t;
287 int have_rev = 0;
288
289 list_for_each_entry(t, &xt[af].target, list) {
290 if (strcmp(t->name, name) == 0) {
291 if (t->revision > *bestp)
292 *bestp = t->revision;
293 if (t->revision == revision)
294 have_rev = 1;
295 }
296 }
297
298 if (af != NFPROTO_UNSPEC && !have_rev)
299 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
300
301 return have_rev;
302 }
303
304 /* Returns true or false (if no such extension at all) */
305 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
306 int *err)
307 {
308 int have_rev, best = -1;
309
310 mutex_lock(&xt[af].mutex);
311 if (target == 1)
312 have_rev = target_revfn(af, name, revision, &best);
313 else
314 have_rev = match_revfn(af, name, revision, &best);
315 mutex_unlock(&xt[af].mutex);
316
317 /* Nothing at all? Return 0 to try loading module. */
318 if (best == -1) {
319 *err = -ENOENT;
320 return 0;
321 }
322
323 *err = best;
324 if (!have_rev)
325 *err = -EPROTONOSUPPORT;
326 return 1;
327 }
328 EXPORT_SYMBOL_GPL(xt_find_revision);
329
330 static char *
331 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
332 {
333 static const char *const inetbr_names[] = {
334 "PREROUTING", "INPUT", "FORWARD",
335 "OUTPUT", "POSTROUTING", "BROUTING",
336 };
337 static const char *const arp_names[] = {
338 "INPUT", "FORWARD", "OUTPUT",
339 };
340 const char *const *names;
341 unsigned int i, max;
342 char *p = buf;
343 bool np = false;
344 int res;
345
346 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
347 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
348 ARRAY_SIZE(inetbr_names);
349 *p = '\0';
350 for (i = 0; i < max; ++i) {
351 if (!(mask & (1 << i)))
352 continue;
353 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
354 if (res > 0) {
355 size -= res;
356 p += res;
357 }
358 np = true;
359 }
360
361 return buf;
362 }
363
364 int xt_check_match(struct xt_mtchk_param *par,
365 unsigned int size, u_int8_t proto, bool inv_proto)
366 {
367 int ret;
368
369 if (XT_ALIGN(par->match->matchsize) != size &&
370 par->match->matchsize != -1) {
371 /*
372 * ebt_among is exempt from centralized matchsize checking
373 * because it uses a dynamic-size data set.
374 */
375 pr_err("%s_tables: %s.%u match: invalid size "
376 "%u (kernel) != (user) %u\n",
377 xt_prefix[par->family], par->match->name,
378 par->match->revision,
379 XT_ALIGN(par->match->matchsize), size);
380 return -EINVAL;
381 }
382 if (par->match->table != NULL &&
383 strcmp(par->match->table, par->table) != 0) {
384 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
385 xt_prefix[par->family], par->match->name,
386 par->match->table, par->table);
387 return -EINVAL;
388 }
389 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
390 char used[64], allow[64];
391
392 pr_err("%s_tables: %s match: used from hooks %s, but only "
393 "valid from %s\n",
394 xt_prefix[par->family], par->match->name,
395 textify_hooks(used, sizeof(used), par->hook_mask,
396 par->family),
397 textify_hooks(allow, sizeof(allow), par->match->hooks,
398 par->family));
399 return -EINVAL;
400 }
401 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
402 pr_err("%s_tables: %s match: only valid for protocol %u\n",
403 xt_prefix[par->family], par->match->name,
404 par->match->proto);
405 return -EINVAL;
406 }
407 if (par->match->checkentry != NULL) {
408 ret = par->match->checkentry(par);
409 if (ret < 0)
410 return ret;
411 else if (ret > 0)
412 /* Flag up potential errors. */
413 return -EIO;
414 }
415 return 0;
416 }
417 EXPORT_SYMBOL_GPL(xt_check_match);
418
419 #ifdef CONFIG_COMPAT
420 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
421 {
422 struct xt_af *xp = &xt[af];
423
424 if (!xp->compat_tab) {
425 if (!xp->number)
426 return -EINVAL;
427 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
428 if (!xp->compat_tab)
429 return -ENOMEM;
430 xp->cur = 0;
431 }
432
433 if (xp->cur >= xp->number)
434 return -EINVAL;
435
436 if (xp->cur)
437 delta += xp->compat_tab[xp->cur - 1].delta;
438 xp->compat_tab[xp->cur].offset = offset;
439 xp->compat_tab[xp->cur].delta = delta;
440 xp->cur++;
441 return 0;
442 }
443 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
444
445 void xt_compat_flush_offsets(u_int8_t af)
446 {
447 if (xt[af].compat_tab) {
448 vfree(xt[af].compat_tab);
449 xt[af].compat_tab = NULL;
450 xt[af].number = 0;
451 xt[af].cur = 0;
452 }
453 }
454 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
455
456 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
457 {
458 struct compat_delta *tmp = xt[af].compat_tab;
459 int mid, left = 0, right = xt[af].cur - 1;
460
461 while (left <= right) {
462 mid = (left + right) >> 1;
463 if (offset > tmp[mid].offset)
464 left = mid + 1;
465 else if (offset < tmp[mid].offset)
466 right = mid - 1;
467 else
468 return mid ? tmp[mid - 1].delta : 0;
469 }
470 return left ? tmp[left - 1].delta : 0;
471 }
472 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
473
474 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
475 {
476 xt[af].number = number;
477 xt[af].cur = 0;
478 }
479 EXPORT_SYMBOL(xt_compat_init_offsets);
480
481 int xt_compat_match_offset(const struct xt_match *match)
482 {
483 u_int16_t csize = match->compatsize ? : match->matchsize;
484 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
485 }
486 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
487
488 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
489 unsigned int *size)
490 {
491 const struct xt_match *match = m->u.kernel.match;
492 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
493 int pad, off = xt_compat_match_offset(match);
494 u_int16_t msize = cm->u.user.match_size;
495
496 m = *dstptr;
497 memcpy(m, cm, sizeof(*cm));
498 if (match->compat_from_user)
499 match->compat_from_user(m->data, cm->data);
500 else
501 memcpy(m->data, cm->data, msize - sizeof(*cm));
502 pad = XT_ALIGN(match->matchsize) - match->matchsize;
503 if (pad > 0)
504 memset(m->data + match->matchsize, 0, pad);
505
506 msize += off;
507 m->u.user.match_size = msize;
508
509 *size += off;
510 *dstptr += msize;
511 return 0;
512 }
513 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
514
515 int xt_compat_match_to_user(const struct xt_entry_match *m,
516 void __user **dstptr, unsigned int *size)
517 {
518 const struct xt_match *match = m->u.kernel.match;
519 struct compat_xt_entry_match __user *cm = *dstptr;
520 int off = xt_compat_match_offset(match);
521 u_int16_t msize = m->u.user.match_size - off;
522
523 if (copy_to_user(cm, m, sizeof(*cm)) ||
524 put_user(msize, &cm->u.user.match_size) ||
525 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
526 strlen(m->u.kernel.match->name) + 1))
527 return -EFAULT;
528
529 if (match->compat_to_user) {
530 if (match->compat_to_user((void __user *)cm->data, m->data))
531 return -EFAULT;
532 } else {
533 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
534 return -EFAULT;
535 }
536
537 *size -= off;
538 *dstptr += msize;
539 return 0;
540 }
541 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
542 #endif /* CONFIG_COMPAT */
543
544 int xt_check_target(struct xt_tgchk_param *par,
545 unsigned int size, u_int8_t proto, bool inv_proto)
546 {
547 int ret;
548
549 if (XT_ALIGN(par->target->targetsize) != size) {
550 pr_err("%s_tables: %s.%u target: invalid size "
551 "%u (kernel) != (user) %u\n",
552 xt_prefix[par->family], par->target->name,
553 par->target->revision,
554 XT_ALIGN(par->target->targetsize), size);
555 return -EINVAL;
556 }
557 if (par->target->table != NULL &&
558 strcmp(par->target->table, par->table) != 0) {
559 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
560 xt_prefix[par->family], par->target->name,
561 par->target->table, par->table);
562 return -EINVAL;
563 }
564 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
565 char used[64], allow[64];
566
567 pr_err("%s_tables: %s target: used from hooks %s, but only "
568 "usable from %s\n",
569 xt_prefix[par->family], par->target->name,
570 textify_hooks(used, sizeof(used), par->hook_mask,
571 par->family),
572 textify_hooks(allow, sizeof(allow), par->target->hooks,
573 par->family));
574 return -EINVAL;
575 }
576 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
577 pr_err("%s_tables: %s target: only valid for protocol %u\n",
578 xt_prefix[par->family], par->target->name,
579 par->target->proto);
580 return -EINVAL;
581 }
582 if (par->target->checkentry != NULL) {
583 ret = par->target->checkentry(par);
584 if (ret < 0)
585 return ret;
586 else if (ret > 0)
587 /* Flag up potential errors. */
588 return -EIO;
589 }
590 return 0;
591 }
592 EXPORT_SYMBOL_GPL(xt_check_target);
593
594 #ifdef CONFIG_COMPAT
595 int xt_compat_target_offset(const struct xt_target *target)
596 {
597 u_int16_t csize = target->compatsize ? : target->targetsize;
598 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
599 }
600 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
601
602 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
603 unsigned int *size)
604 {
605 const struct xt_target *target = t->u.kernel.target;
606 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
607 int pad, off = xt_compat_target_offset(target);
608 u_int16_t tsize = ct->u.user.target_size;
609
610 t = *dstptr;
611 memcpy(t, ct, sizeof(*ct));
612 if (target->compat_from_user)
613 target->compat_from_user(t->data, ct->data);
614 else
615 memcpy(t->data, ct->data, tsize - sizeof(*ct));
616 pad = XT_ALIGN(target->targetsize) - target->targetsize;
617 if (pad > 0)
618 memset(t->data + target->targetsize, 0, pad);
619
620 tsize += off;
621 t->u.user.target_size = tsize;
622
623 *size += off;
624 *dstptr += tsize;
625 }
626 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
627
628 int xt_compat_target_to_user(const struct xt_entry_target *t,
629 void __user **dstptr, unsigned int *size)
630 {
631 const struct xt_target *target = t->u.kernel.target;
632 struct compat_xt_entry_target __user *ct = *dstptr;
633 int off = xt_compat_target_offset(target);
634 u_int16_t tsize = t->u.user.target_size - off;
635
636 if (copy_to_user(ct, t, sizeof(*ct)) ||
637 put_user(tsize, &ct->u.user.target_size) ||
638 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
639 strlen(t->u.kernel.target->name) + 1))
640 return -EFAULT;
641
642 if (target->compat_to_user) {
643 if (target->compat_to_user((void __user *)ct->data, t->data))
644 return -EFAULT;
645 } else {
646 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
647 return -EFAULT;
648 }
649
650 *size -= off;
651 *dstptr += tsize;
652 return 0;
653 }
654 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
655 #endif
656
657 struct xt_table_info *xt_alloc_table_info(unsigned int size)
658 {
659 struct xt_table_info *info = NULL;
660 size_t sz = sizeof(*info) + size;
661
662 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
663 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
664 return NULL;
665
666 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
667 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
668 if (!info) {
669 info = vmalloc(sz);
670 if (!info)
671 return NULL;
672 }
673 memset(info, 0, sizeof(*info));
674 info->size = size;
675 return info;
676 }
677 EXPORT_SYMBOL(xt_alloc_table_info);
678
679 void xt_free_table_info(struct xt_table_info *info)
680 {
681 int cpu;
682
683 if (info->jumpstack != NULL) {
684 for_each_possible_cpu(cpu)
685 kvfree(info->jumpstack[cpu]);
686 kvfree(info->jumpstack);
687 }
688
689 kvfree(info);
690 }
691 EXPORT_SYMBOL(xt_free_table_info);
692
693 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
694 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
695 const char *name)
696 {
697 struct xt_table *t, *found = NULL;
698
699 mutex_lock(&xt[af].mutex);
700 list_for_each_entry(t, &net->xt.tables[af], list)
701 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
702 return t;
703
704 if (net == &init_net)
705 goto out;
706
707 /* Table doesn't exist in this netns, re-try init */
708 list_for_each_entry(t, &init_net.xt.tables[af], list) {
709 if (strcmp(t->name, name))
710 continue;
711 if (!try_module_get(t->me))
712 return NULL;
713
714 mutex_unlock(&xt[af].mutex);
715 if (t->table_init(net) != 0) {
716 module_put(t->me);
717 return NULL;
718 }
719
720 found = t;
721
722 mutex_lock(&xt[af].mutex);
723 break;
724 }
725
726 if (!found)
727 goto out;
728
729 /* and once again: */
730 list_for_each_entry(t, &net->xt.tables[af], list)
731 if (strcmp(t->name, name) == 0)
732 return t;
733
734 module_put(found->me);
735 out:
736 mutex_unlock(&xt[af].mutex);
737 return NULL;
738 }
739 EXPORT_SYMBOL_GPL(xt_find_table_lock);
740
741 void xt_table_unlock(struct xt_table *table)
742 {
743 mutex_unlock(&xt[table->af].mutex);
744 }
745 EXPORT_SYMBOL_GPL(xt_table_unlock);
746
747 #ifdef CONFIG_COMPAT
748 void xt_compat_lock(u_int8_t af)
749 {
750 mutex_lock(&xt[af].compat_mutex);
751 }
752 EXPORT_SYMBOL_GPL(xt_compat_lock);
753
754 void xt_compat_unlock(u_int8_t af)
755 {
756 mutex_unlock(&xt[af].compat_mutex);
757 }
758 EXPORT_SYMBOL_GPL(xt_compat_unlock);
759 #endif
760
761 DEFINE_PER_CPU(seqcount_t, xt_recseq);
762 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
763
764 struct static_key xt_tee_enabled __read_mostly;
765 EXPORT_SYMBOL_GPL(xt_tee_enabled);
766
767 static int xt_jumpstack_alloc(struct xt_table_info *i)
768 {
769 unsigned int size;
770 int cpu;
771
772 size = sizeof(void **) * nr_cpu_ids;
773 if (size > PAGE_SIZE)
774 i->jumpstack = vzalloc(size);
775 else
776 i->jumpstack = kzalloc(size, GFP_KERNEL);
777 if (i->jumpstack == NULL)
778 return -ENOMEM;
779
780 /* ruleset without jumps -- no stack needed */
781 if (i->stacksize == 0)
782 return 0;
783
784 /* Jumpstack needs to be able to record two full callchains, one
785 * from the first rule set traversal, plus one table reentrancy
786 * via -j TEE without clobbering the callchain that brought us to
787 * TEE target.
788 *
789 * This is done by allocating two jumpstacks per cpu, on reentry
790 * the upper half of the stack is used.
791 *
792 * see the jumpstack setup in ipt_do_table() for more details.
793 */
794 size = sizeof(void *) * i->stacksize * 2u;
795 for_each_possible_cpu(cpu) {
796 if (size > PAGE_SIZE)
797 i->jumpstack[cpu] = vmalloc_node(size,
798 cpu_to_node(cpu));
799 else
800 i->jumpstack[cpu] = kmalloc_node(size,
801 GFP_KERNEL, cpu_to_node(cpu));
802 if (i->jumpstack[cpu] == NULL)
803 /*
804 * Freeing will be done later on by the callers. The
805 * chain is: xt_replace_table -> __do_replace ->
806 * do_replace -> xt_free_table_info.
807 */
808 return -ENOMEM;
809 }
810
811 return 0;
812 }
813
814 struct xt_table_info *
815 xt_replace_table(struct xt_table *table,
816 unsigned int num_counters,
817 struct xt_table_info *newinfo,
818 int *error)
819 {
820 struct xt_table_info *private;
821 int ret;
822
823 ret = xt_jumpstack_alloc(newinfo);
824 if (ret < 0) {
825 *error = ret;
826 return NULL;
827 }
828
829 /* Do the substitution. */
830 local_bh_disable();
831 private = table->private;
832
833 /* Check inside lock: is the old number correct? */
834 if (num_counters != private->number) {
835 pr_debug("num_counters != table->private->number (%u/%u)\n",
836 num_counters, private->number);
837 local_bh_enable();
838 *error = -EAGAIN;
839 return NULL;
840 }
841
842 newinfo->initial_entries = private->initial_entries;
843 /*
844 * Ensure contents of newinfo are visible before assigning to
845 * private.
846 */
847 smp_wmb();
848 table->private = newinfo;
849
850 /*
851 * Even though table entries have now been swapped, other CPU's
852 * may still be using the old entries. This is okay, because
853 * resynchronization happens because of the locking done
854 * during the get_counters() routine.
855 */
856 local_bh_enable();
857
858 #ifdef CONFIG_AUDIT
859 if (audit_enabled) {
860 struct audit_buffer *ab;
861
862 ab = audit_log_start(current->audit_context, GFP_KERNEL,
863 AUDIT_NETFILTER_CFG);
864 if (ab) {
865 audit_log_format(ab, "table=%s family=%u entries=%u",
866 table->name, table->af,
867 private->number);
868 audit_log_end(ab);
869 }
870 }
871 #endif
872
873 return private;
874 }
875 EXPORT_SYMBOL_GPL(xt_replace_table);
876
877 struct xt_table *xt_register_table(struct net *net,
878 const struct xt_table *input_table,
879 struct xt_table_info *bootstrap,
880 struct xt_table_info *newinfo)
881 {
882 int ret;
883 struct xt_table_info *private;
884 struct xt_table *t, *table;
885
886 /* Don't add one object to multiple lists. */
887 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
888 if (!table) {
889 ret = -ENOMEM;
890 goto out;
891 }
892
893 mutex_lock(&xt[table->af].mutex);
894 /* Don't autoload: we'd eat our tail... */
895 list_for_each_entry(t, &net->xt.tables[table->af], list) {
896 if (strcmp(t->name, table->name) == 0) {
897 ret = -EEXIST;
898 goto unlock;
899 }
900 }
901
902 /* Simplifies replace_table code. */
903 table->private = bootstrap;
904
905 if (!xt_replace_table(table, 0, newinfo, &ret))
906 goto unlock;
907
908 private = table->private;
909 pr_debug("table->private->number = %u\n", private->number);
910
911 /* save number of initial entries */
912 private->initial_entries = private->number;
913
914 list_add(&table->list, &net->xt.tables[table->af]);
915 mutex_unlock(&xt[table->af].mutex);
916 return table;
917
918 unlock:
919 mutex_unlock(&xt[table->af].mutex);
920 kfree(table);
921 out:
922 return ERR_PTR(ret);
923 }
924 EXPORT_SYMBOL_GPL(xt_register_table);
925
926 void *xt_unregister_table(struct xt_table *table)
927 {
928 struct xt_table_info *private;
929
930 mutex_lock(&xt[table->af].mutex);
931 private = table->private;
932 list_del(&table->list);
933 mutex_unlock(&xt[table->af].mutex);
934 kfree(table);
935
936 return private;
937 }
938 EXPORT_SYMBOL_GPL(xt_unregister_table);
939
940 #ifdef CONFIG_PROC_FS
941 struct xt_names_priv {
942 struct seq_net_private p;
943 u_int8_t af;
944 };
945 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
946 {
947 struct xt_names_priv *priv = seq->private;
948 struct net *net = seq_file_net(seq);
949 u_int8_t af = priv->af;
950
951 mutex_lock(&xt[af].mutex);
952 return seq_list_start(&net->xt.tables[af], *pos);
953 }
954
955 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
956 {
957 struct xt_names_priv *priv = seq->private;
958 struct net *net = seq_file_net(seq);
959 u_int8_t af = priv->af;
960
961 return seq_list_next(v, &net->xt.tables[af], pos);
962 }
963
964 static void xt_table_seq_stop(struct seq_file *seq, void *v)
965 {
966 struct xt_names_priv *priv = seq->private;
967 u_int8_t af = priv->af;
968
969 mutex_unlock(&xt[af].mutex);
970 }
971
972 static int xt_table_seq_show(struct seq_file *seq, void *v)
973 {
974 struct xt_table *table = list_entry(v, struct xt_table, list);
975
976 if (*table->name)
977 seq_printf(seq, "%s\n", table->name);
978 return 0;
979 }
980
981 static const struct seq_operations xt_table_seq_ops = {
982 .start = xt_table_seq_start,
983 .next = xt_table_seq_next,
984 .stop = xt_table_seq_stop,
985 .show = xt_table_seq_show,
986 };
987
988 static int xt_table_open(struct inode *inode, struct file *file)
989 {
990 int ret;
991 struct xt_names_priv *priv;
992
993 ret = seq_open_net(inode, file, &xt_table_seq_ops,
994 sizeof(struct xt_names_priv));
995 if (!ret) {
996 priv = ((struct seq_file *)file->private_data)->private;
997 priv->af = (unsigned long)PDE_DATA(inode);
998 }
999 return ret;
1000 }
1001
1002 static const struct file_operations xt_table_ops = {
1003 .owner = THIS_MODULE,
1004 .open = xt_table_open,
1005 .read = seq_read,
1006 .llseek = seq_lseek,
1007 .release = seq_release_net,
1008 };
1009
1010 /*
1011 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1012 * the multi-AF mutexes.
1013 */
1014 struct nf_mttg_trav {
1015 struct list_head *head, *curr;
1016 uint8_t class, nfproto;
1017 };
1018
1019 enum {
1020 MTTG_TRAV_INIT,
1021 MTTG_TRAV_NFP_UNSPEC,
1022 MTTG_TRAV_NFP_SPEC,
1023 MTTG_TRAV_DONE,
1024 };
1025
1026 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1027 bool is_target)
1028 {
1029 static const uint8_t next_class[] = {
1030 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1031 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1032 };
1033 struct nf_mttg_trav *trav = seq->private;
1034
1035 switch (trav->class) {
1036 case MTTG_TRAV_INIT:
1037 trav->class = MTTG_TRAV_NFP_UNSPEC;
1038 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1039 trav->head = trav->curr = is_target ?
1040 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1041 break;
1042 case MTTG_TRAV_NFP_UNSPEC:
1043 trav->curr = trav->curr->next;
1044 if (trav->curr != trav->head)
1045 break;
1046 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1047 mutex_lock(&xt[trav->nfproto].mutex);
1048 trav->head = trav->curr = is_target ?
1049 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1050 trav->class = next_class[trav->class];
1051 break;
1052 case MTTG_TRAV_NFP_SPEC:
1053 trav->curr = trav->curr->next;
1054 if (trav->curr != trav->head)
1055 break;
1056 /* fallthru, _stop will unlock */
1057 default:
1058 return NULL;
1059 }
1060
1061 if (ppos != NULL)
1062 ++*ppos;
1063 return trav;
1064 }
1065
1066 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1067 bool is_target)
1068 {
1069 struct nf_mttg_trav *trav = seq->private;
1070 unsigned int j;
1071
1072 trav->class = MTTG_TRAV_INIT;
1073 for (j = 0; j < *pos; ++j)
1074 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1075 return NULL;
1076 return trav;
1077 }
1078
1079 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1080 {
1081 struct nf_mttg_trav *trav = seq->private;
1082
1083 switch (trav->class) {
1084 case MTTG_TRAV_NFP_UNSPEC:
1085 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1086 break;
1087 case MTTG_TRAV_NFP_SPEC:
1088 mutex_unlock(&xt[trav->nfproto].mutex);
1089 break;
1090 }
1091 }
1092
1093 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1094 {
1095 return xt_mttg_seq_start(seq, pos, false);
1096 }
1097
1098 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1099 {
1100 return xt_mttg_seq_next(seq, v, ppos, false);
1101 }
1102
1103 static int xt_match_seq_show(struct seq_file *seq, void *v)
1104 {
1105 const struct nf_mttg_trav *trav = seq->private;
1106 const struct xt_match *match;
1107
1108 switch (trav->class) {
1109 case MTTG_TRAV_NFP_UNSPEC:
1110 case MTTG_TRAV_NFP_SPEC:
1111 if (trav->curr == trav->head)
1112 return 0;
1113 match = list_entry(trav->curr, struct xt_match, list);
1114 if (*match->name)
1115 seq_printf(seq, "%s\n", match->name);
1116 }
1117 return 0;
1118 }
1119
1120 static const struct seq_operations xt_match_seq_ops = {
1121 .start = xt_match_seq_start,
1122 .next = xt_match_seq_next,
1123 .stop = xt_mttg_seq_stop,
1124 .show = xt_match_seq_show,
1125 };
1126
1127 static int xt_match_open(struct inode *inode, struct file *file)
1128 {
1129 struct nf_mttg_trav *trav;
1130 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1131 if (!trav)
1132 return -ENOMEM;
1133
1134 trav->nfproto = (unsigned long)PDE_DATA(inode);
1135 return 0;
1136 }
1137
1138 static const struct file_operations xt_match_ops = {
1139 .owner = THIS_MODULE,
1140 .open = xt_match_open,
1141 .read = seq_read,
1142 .llseek = seq_lseek,
1143 .release = seq_release_private,
1144 };
1145
1146 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1147 {
1148 return xt_mttg_seq_start(seq, pos, true);
1149 }
1150
1151 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1152 {
1153 return xt_mttg_seq_next(seq, v, ppos, true);
1154 }
1155
1156 static int xt_target_seq_show(struct seq_file *seq, void *v)
1157 {
1158 const struct nf_mttg_trav *trav = seq->private;
1159 const struct xt_target *target;
1160
1161 switch (trav->class) {
1162 case MTTG_TRAV_NFP_UNSPEC:
1163 case MTTG_TRAV_NFP_SPEC:
1164 if (trav->curr == trav->head)
1165 return 0;
1166 target = list_entry(trav->curr, struct xt_target, list);
1167 if (*target->name)
1168 seq_printf(seq, "%s\n", target->name);
1169 }
1170 return 0;
1171 }
1172
1173 static const struct seq_operations xt_target_seq_ops = {
1174 .start = xt_target_seq_start,
1175 .next = xt_target_seq_next,
1176 .stop = xt_mttg_seq_stop,
1177 .show = xt_target_seq_show,
1178 };
1179
1180 static int xt_target_open(struct inode *inode, struct file *file)
1181 {
1182 struct nf_mttg_trav *trav;
1183 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1184 if (!trav)
1185 return -ENOMEM;
1186
1187 trav->nfproto = (unsigned long)PDE_DATA(inode);
1188 return 0;
1189 }
1190
1191 static const struct file_operations xt_target_ops = {
1192 .owner = THIS_MODULE,
1193 .open = xt_target_open,
1194 .read = seq_read,
1195 .llseek = seq_lseek,
1196 .release = seq_release_private,
1197 };
1198
1199 #define FORMAT_TABLES "_tables_names"
1200 #define FORMAT_MATCHES "_tables_matches"
1201 #define FORMAT_TARGETS "_tables_targets"
1202
1203 #endif /* CONFIG_PROC_FS */
1204
1205 /**
1206 * xt_hook_ops_alloc - set up hooks for a new table
1207 * @table: table with metadata needed to set up hooks
1208 * @fn: Hook function
1209 *
1210 * This function will create the nf_hook_ops that the x_table needs
1211 * to hand to xt_hook_link_net().
1212 */
1213 struct nf_hook_ops *
1214 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1215 {
1216 unsigned int hook_mask = table->valid_hooks;
1217 uint8_t i, num_hooks = hweight32(hook_mask);
1218 uint8_t hooknum;
1219 struct nf_hook_ops *ops;
1220
1221 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1222 if (ops == NULL)
1223 return ERR_PTR(-ENOMEM);
1224
1225 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1226 hook_mask >>= 1, ++hooknum) {
1227 if (!(hook_mask & 1))
1228 continue;
1229 ops[i].hook = fn;
1230 ops[i].pf = table->af;
1231 ops[i].hooknum = hooknum;
1232 ops[i].priority = table->priority;
1233 ++i;
1234 }
1235
1236 return ops;
1237 }
1238 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1239
1240 int xt_proto_init(struct net *net, u_int8_t af)
1241 {
1242 #ifdef CONFIG_PROC_FS
1243 char buf[XT_FUNCTION_MAXNAMELEN];
1244 struct proc_dir_entry *proc;
1245 kuid_t root_uid;
1246 kgid_t root_gid;
1247 #endif
1248
1249 if (af >= ARRAY_SIZE(xt_prefix))
1250 return -EINVAL;
1251
1252
1253 #ifdef CONFIG_PROC_FS
1254 root_uid = make_kuid(net->user_ns, 0);
1255 root_gid = make_kgid(net->user_ns, 0);
1256
1257 strlcpy(buf, xt_prefix[af], sizeof(buf));
1258 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1259 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1260 (void *)(unsigned long)af);
1261 if (!proc)
1262 goto out;
1263 if (uid_valid(root_uid) && gid_valid(root_gid))
1264 proc_set_user(proc, root_uid, root_gid);
1265
1266 strlcpy(buf, xt_prefix[af], sizeof(buf));
1267 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1268 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1269 (void *)(unsigned long)af);
1270 if (!proc)
1271 goto out_remove_tables;
1272 if (uid_valid(root_uid) && gid_valid(root_gid))
1273 proc_set_user(proc, root_uid, root_gid);
1274
1275 strlcpy(buf, xt_prefix[af], sizeof(buf));
1276 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1277 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1278 (void *)(unsigned long)af);
1279 if (!proc)
1280 goto out_remove_matches;
1281 if (uid_valid(root_uid) && gid_valid(root_gid))
1282 proc_set_user(proc, root_uid, root_gid);
1283 #endif
1284
1285 return 0;
1286
1287 #ifdef CONFIG_PROC_FS
1288 out_remove_matches:
1289 strlcpy(buf, xt_prefix[af], sizeof(buf));
1290 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1291 remove_proc_entry(buf, net->proc_net);
1292
1293 out_remove_tables:
1294 strlcpy(buf, xt_prefix[af], sizeof(buf));
1295 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1296 remove_proc_entry(buf, net->proc_net);
1297 out:
1298 return -1;
1299 #endif
1300 }
1301 EXPORT_SYMBOL_GPL(xt_proto_init);
1302
1303 void xt_proto_fini(struct net *net, u_int8_t af)
1304 {
1305 #ifdef CONFIG_PROC_FS
1306 char buf[XT_FUNCTION_MAXNAMELEN];
1307
1308 strlcpy(buf, xt_prefix[af], sizeof(buf));
1309 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1310 remove_proc_entry(buf, net->proc_net);
1311
1312 strlcpy(buf, xt_prefix[af], sizeof(buf));
1313 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1314 remove_proc_entry(buf, net->proc_net);
1315
1316 strlcpy(buf, xt_prefix[af], sizeof(buf));
1317 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1318 remove_proc_entry(buf, net->proc_net);
1319 #endif /*CONFIG_PROC_FS*/
1320 }
1321 EXPORT_SYMBOL_GPL(xt_proto_fini);
1322
1323 static int __net_init xt_net_init(struct net *net)
1324 {
1325 int i;
1326
1327 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1328 INIT_LIST_HEAD(&net->xt.tables[i]);
1329 return 0;
1330 }
1331
1332 static struct pernet_operations xt_net_ops = {
1333 .init = xt_net_init,
1334 };
1335
1336 static int __init xt_init(void)
1337 {
1338 unsigned int i;
1339 int rv;
1340
1341 for_each_possible_cpu(i) {
1342 seqcount_init(&per_cpu(xt_recseq, i));
1343 }
1344
1345 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1346 if (!xt)
1347 return -ENOMEM;
1348
1349 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1350 mutex_init(&xt[i].mutex);
1351 #ifdef CONFIG_COMPAT
1352 mutex_init(&xt[i].compat_mutex);
1353 xt[i].compat_tab = NULL;
1354 #endif
1355 INIT_LIST_HEAD(&xt[i].target);
1356 INIT_LIST_HEAD(&xt[i].match);
1357 }
1358 rv = register_pernet_subsys(&xt_net_ops);
1359 if (rv < 0)
1360 kfree(xt);
1361 return rv;
1362 }
1363
1364 static void __exit xt_fini(void)
1365 {
1366 unregister_pernet_subsys(&xt_net_ops);
1367 kfree(xt);
1368 }
1369
1370 module_init(xt_init);
1371 module_exit(xt_fini);
1372
This page took 0.058583 seconds and 6 git commands to generate.