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