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