]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/netfilter/x_tables.c
netfilter: x_tables: check for bogus target offset
[karo-tx-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 <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. */
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
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
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
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
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
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
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
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. */
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 *
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. */
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
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
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
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) */
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 *
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
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 #ifdef CONFIG_COMPAT
419 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
420 {
421         struct xt_af *xp = &xt[af];
422
423         if (!xp->compat_tab) {
424                 if (!xp->number)
425                         return -EINVAL;
426                 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
427                 if (!xp->compat_tab)
428                         return -ENOMEM;
429                 xp->cur = 0;
430         }
431
432         if (xp->cur >= xp->number)
433                 return -EINVAL;
434
435         if (xp->cur)
436                 delta += xp->compat_tab[xp->cur - 1].delta;
437         xp->compat_tab[xp->cur].offset = offset;
438         xp->compat_tab[xp->cur].delta = delta;
439         xp->cur++;
440         return 0;
441 }
442 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
443
444 void xt_compat_flush_offsets(u_int8_t af)
445 {
446         if (xt[af].compat_tab) {
447                 vfree(xt[af].compat_tab);
448                 xt[af].compat_tab = NULL;
449                 xt[af].number = 0;
450                 xt[af].cur = 0;
451         }
452 }
453 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
454
455 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
456 {
457         struct compat_delta *tmp = xt[af].compat_tab;
458         int mid, left = 0, right = xt[af].cur - 1;
459
460         while (left <= right) {
461                 mid = (left + right) >> 1;
462                 if (offset > tmp[mid].offset)
463                         left = mid + 1;
464                 else if (offset < tmp[mid].offset)
465                         right = mid - 1;
466                 else
467                         return mid ? tmp[mid - 1].delta : 0;
468         }
469         return left ? tmp[left - 1].delta : 0;
470 }
471 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
472
473 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
474 {
475         xt[af].number = number;
476         xt[af].cur = 0;
477 }
478 EXPORT_SYMBOL(xt_compat_init_offsets);
479
480 int xt_compat_match_offset(const struct xt_match *match)
481 {
482         u_int16_t csize = match->compatsize ? : match->matchsize;
483         return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
484 }
485 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
486
487 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
488                               unsigned int *size)
489 {
490         const struct xt_match *match = m->u.kernel.match;
491         struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
492         int pad, off = xt_compat_match_offset(match);
493         u_int16_t msize = cm->u.user.match_size;
494
495         m = *dstptr;
496         memcpy(m, cm, sizeof(*cm));
497         if (match->compat_from_user)
498                 match->compat_from_user(m->data, cm->data);
499         else
500                 memcpy(m->data, cm->data, msize - sizeof(*cm));
501         pad = XT_ALIGN(match->matchsize) - match->matchsize;
502         if (pad > 0)
503                 memset(m->data + match->matchsize, 0, pad);
504
505         msize += off;
506         m->u.user.match_size = msize;
507
508         *size += off;
509         *dstptr += msize;
510         return 0;
511 }
512 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
513
514 int xt_compat_match_to_user(const struct xt_entry_match *m,
515                             void __user **dstptr, unsigned int *size)
516 {
517         const struct xt_match *match = m->u.kernel.match;
518         struct compat_xt_entry_match __user *cm = *dstptr;
519         int off = xt_compat_match_offset(match);
520         u_int16_t msize = m->u.user.match_size - off;
521
522         if (copy_to_user(cm, m, sizeof(*cm)) ||
523             put_user(msize, &cm->u.user.match_size) ||
524             copy_to_user(cm->u.user.name, m->u.kernel.match->name,
525                          strlen(m->u.kernel.match->name) + 1))
526                 return -EFAULT;
527
528         if (match->compat_to_user) {
529                 if (match->compat_to_user((void __user *)cm->data, m->data))
530                         return -EFAULT;
531         } else {
532                 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
533                         return -EFAULT;
534         }
535
536         *size -= off;
537         *dstptr += msize;
538         return 0;
539 }
540 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
541
542 /* non-compat version may have padding after verdict */
543 struct compat_xt_standard_target {
544         struct compat_xt_entry_target t;
545         compat_uint_t verdict;
546 };
547
548 int xt_compat_check_entry_offsets(const void *base, const char *elems,
549                                   unsigned int target_offset,
550                                   unsigned int next_offset)
551 {
552         long size_of_base_struct = elems - (const char *)base;
553         const struct compat_xt_entry_target *t;
554         const char *e = base;
555
556         if (target_offset < size_of_base_struct)
557                 return -EINVAL;
558
559         if (target_offset + sizeof(*t) > next_offset)
560                 return -EINVAL;
561
562         t = (void *)(e + target_offset);
563         if (t->u.target_size < sizeof(*t))
564                 return -EINVAL;
565
566         if (target_offset + t->u.target_size > next_offset)
567                 return -EINVAL;
568
569         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
570             target_offset + sizeof(struct compat_xt_standard_target) != next_offset)
571                 return -EINVAL;
572
573         return 0;
574 }
575 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
576 #endif /* CONFIG_COMPAT */
577
578 /**
579  * xt_check_entry_offsets - validate arp/ip/ip6t_entry
580  *
581  * @base: pointer to arp/ip/ip6t_entry
582  * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
583  * @target_offset: the arp/ip/ip6_t->target_offset
584  * @next_offset: the arp/ip/ip6_t->next_offset
585  *
586  * validates that target_offset and next_offset are sane.
587  * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
588  *
589  * This function does not validate the targets or matches themselves, it
590  * only tests that all the offsets and sizes are correct.
591  *
592  * The arp/ip/ip6t_entry structure @base must have passed following tests:
593  * - it must point to a valid memory location
594  * - base to base + next_offset must be accessible, i.e. not exceed allocated
595  *   length.
596  *
597  * Return: 0 on success, negative errno on failure.
598  */
599 int xt_check_entry_offsets(const void *base,
600                            const char *elems,
601                            unsigned int target_offset,
602                            unsigned int next_offset)
603 {
604         long size_of_base_struct = elems - (const char *)base;
605         const struct xt_entry_target *t;
606         const char *e = base;
607
608         /* target start is within the ip/ip6/arpt_entry struct */
609         if (target_offset < size_of_base_struct)
610                 return -EINVAL;
611
612         if (target_offset + sizeof(*t) > next_offset)
613                 return -EINVAL;
614
615         t = (void *)(e + target_offset);
616         if (t->u.target_size < sizeof(*t))
617                 return -EINVAL;
618
619         if (target_offset + t->u.target_size > next_offset)
620                 return -EINVAL;
621
622         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
623             target_offset + sizeof(struct xt_standard_target) != next_offset)
624                 return -EINVAL;
625
626         return 0;
627 }
628 EXPORT_SYMBOL(xt_check_entry_offsets);
629
630 int xt_check_target(struct xt_tgchk_param *par,
631                     unsigned int size, u_int8_t proto, bool inv_proto)
632 {
633         int ret;
634
635         if (XT_ALIGN(par->target->targetsize) != size) {
636                 pr_err("%s_tables: %s.%u target: invalid size "
637                        "%u (kernel) != (user) %u\n",
638                        xt_prefix[par->family], par->target->name,
639                        par->target->revision,
640                        XT_ALIGN(par->target->targetsize), size);
641                 return -EINVAL;
642         }
643         if (par->target->table != NULL &&
644             strcmp(par->target->table, par->table) != 0) {
645                 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
646                        xt_prefix[par->family], par->target->name,
647                        par->target->table, par->table);
648                 return -EINVAL;
649         }
650         if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
651                 char used[64], allow[64];
652
653                 pr_err("%s_tables: %s target: used from hooks %s, but only "
654                        "usable from %s\n",
655                        xt_prefix[par->family], par->target->name,
656                        textify_hooks(used, sizeof(used), par->hook_mask,
657                                      par->family),
658                        textify_hooks(allow, sizeof(allow), par->target->hooks,
659                                      par->family));
660                 return -EINVAL;
661         }
662         if (par->target->proto && (par->target->proto != proto || inv_proto)) {
663                 pr_err("%s_tables: %s target: only valid for protocol %u\n",
664                        xt_prefix[par->family], par->target->name,
665                        par->target->proto);
666                 return -EINVAL;
667         }
668         if (par->target->checkentry != NULL) {
669                 ret = par->target->checkentry(par);
670                 if (ret < 0)
671                         return ret;
672                 else if (ret > 0)
673                         /* Flag up potential errors. */
674                         return -EIO;
675         }
676         return 0;
677 }
678 EXPORT_SYMBOL_GPL(xt_check_target);
679
680 #ifdef CONFIG_COMPAT
681 int xt_compat_target_offset(const struct xt_target *target)
682 {
683         u_int16_t csize = target->compatsize ? : target->targetsize;
684         return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
685 }
686 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
687
688 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
689                                 unsigned int *size)
690 {
691         const struct xt_target *target = t->u.kernel.target;
692         struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
693         int pad, off = xt_compat_target_offset(target);
694         u_int16_t tsize = ct->u.user.target_size;
695
696         t = *dstptr;
697         memcpy(t, ct, sizeof(*ct));
698         if (target->compat_from_user)
699                 target->compat_from_user(t->data, ct->data);
700         else
701                 memcpy(t->data, ct->data, tsize - sizeof(*ct));
702         pad = XT_ALIGN(target->targetsize) - target->targetsize;
703         if (pad > 0)
704                 memset(t->data + target->targetsize, 0, pad);
705
706         tsize += off;
707         t->u.user.target_size = tsize;
708
709         *size += off;
710         *dstptr += tsize;
711 }
712 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
713
714 int xt_compat_target_to_user(const struct xt_entry_target *t,
715                              void __user **dstptr, unsigned int *size)
716 {
717         const struct xt_target *target = t->u.kernel.target;
718         struct compat_xt_entry_target __user *ct = *dstptr;
719         int off = xt_compat_target_offset(target);
720         u_int16_t tsize = t->u.user.target_size - off;
721
722         if (copy_to_user(ct, t, sizeof(*ct)) ||
723             put_user(tsize, &ct->u.user.target_size) ||
724             copy_to_user(ct->u.user.name, t->u.kernel.target->name,
725                          strlen(t->u.kernel.target->name) + 1))
726                 return -EFAULT;
727
728         if (target->compat_to_user) {
729                 if (target->compat_to_user((void __user *)ct->data, t->data))
730                         return -EFAULT;
731         } else {
732                 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
733                         return -EFAULT;
734         }
735
736         *size -= off;
737         *dstptr += tsize;
738         return 0;
739 }
740 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
741 #endif
742
743 struct xt_table_info *xt_alloc_table_info(unsigned int size)
744 {
745         struct xt_table_info *info = NULL;
746         size_t sz = sizeof(*info) + size;
747
748         /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
749         if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
750                 return NULL;
751
752         if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
753                 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
754         if (!info) {
755                 info = vmalloc(sz);
756                 if (!info)
757                         return NULL;
758         }
759         memset(info, 0, sizeof(*info));
760         info->size = size;
761         return info;
762 }
763 EXPORT_SYMBOL(xt_alloc_table_info);
764
765 void xt_free_table_info(struct xt_table_info *info)
766 {
767         int cpu;
768
769         if (info->jumpstack != NULL) {
770                 for_each_possible_cpu(cpu)
771                         kvfree(info->jumpstack[cpu]);
772                 kvfree(info->jumpstack);
773         }
774
775         kvfree(info);
776 }
777 EXPORT_SYMBOL(xt_free_table_info);
778
779 /* Find table by name, grabs mutex & ref.  Returns ERR_PTR() on error. */
780 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
781                                     const char *name)
782 {
783         struct xt_table *t;
784
785         mutex_lock(&xt[af].mutex);
786         list_for_each_entry(t, &net->xt.tables[af], list)
787                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
788                         return t;
789         mutex_unlock(&xt[af].mutex);
790         return NULL;
791 }
792 EXPORT_SYMBOL_GPL(xt_find_table_lock);
793
794 void xt_table_unlock(struct xt_table *table)
795 {
796         mutex_unlock(&xt[table->af].mutex);
797 }
798 EXPORT_SYMBOL_GPL(xt_table_unlock);
799
800 #ifdef CONFIG_COMPAT
801 void xt_compat_lock(u_int8_t af)
802 {
803         mutex_lock(&xt[af].compat_mutex);
804 }
805 EXPORT_SYMBOL_GPL(xt_compat_lock);
806
807 void xt_compat_unlock(u_int8_t af)
808 {
809         mutex_unlock(&xt[af].compat_mutex);
810 }
811 EXPORT_SYMBOL_GPL(xt_compat_unlock);
812 #endif
813
814 DEFINE_PER_CPU(seqcount_t, xt_recseq);
815 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
816
817 struct static_key xt_tee_enabled __read_mostly;
818 EXPORT_SYMBOL_GPL(xt_tee_enabled);
819
820 static int xt_jumpstack_alloc(struct xt_table_info *i)
821 {
822         unsigned int size;
823         int cpu;
824
825         size = sizeof(void **) * nr_cpu_ids;
826         if (size > PAGE_SIZE)
827                 i->jumpstack = vzalloc(size);
828         else
829                 i->jumpstack = kzalloc(size, GFP_KERNEL);
830         if (i->jumpstack == NULL)
831                 return -ENOMEM;
832
833         /* ruleset without jumps -- no stack needed */
834         if (i->stacksize == 0)
835                 return 0;
836
837         /* Jumpstack needs to be able to record two full callchains, one
838          * from the first rule set traversal, plus one table reentrancy
839          * via -j TEE without clobbering the callchain that brought us to
840          * TEE target.
841          *
842          * This is done by allocating two jumpstacks per cpu, on reentry
843          * the upper half of the stack is used.
844          *
845          * see the jumpstack setup in ipt_do_table() for more details.
846          */
847         size = sizeof(void *) * i->stacksize * 2u;
848         for_each_possible_cpu(cpu) {
849                 if (size > PAGE_SIZE)
850                         i->jumpstack[cpu] = vmalloc_node(size,
851                                 cpu_to_node(cpu));
852                 else
853                         i->jumpstack[cpu] = kmalloc_node(size,
854                                 GFP_KERNEL, cpu_to_node(cpu));
855                 if (i->jumpstack[cpu] == NULL)
856                         /*
857                          * Freeing will be done later on by the callers. The
858                          * chain is: xt_replace_table -> __do_replace ->
859                          * do_replace -> xt_free_table_info.
860                          */
861                         return -ENOMEM;
862         }
863
864         return 0;
865 }
866
867 struct xt_table_info *
868 xt_replace_table(struct xt_table *table,
869               unsigned int num_counters,
870               struct xt_table_info *newinfo,
871               int *error)
872 {
873         struct xt_table_info *private;
874         int ret;
875
876         ret = xt_jumpstack_alloc(newinfo);
877         if (ret < 0) {
878                 *error = ret;
879                 return NULL;
880         }
881
882         /* Do the substitution. */
883         local_bh_disable();
884         private = table->private;
885
886         /* Check inside lock: is the old number correct? */
887         if (num_counters != private->number) {
888                 pr_debug("num_counters != table->private->number (%u/%u)\n",
889                          num_counters, private->number);
890                 local_bh_enable();
891                 *error = -EAGAIN;
892                 return NULL;
893         }
894
895         newinfo->initial_entries = private->initial_entries;
896         /*
897          * Ensure contents of newinfo are visible before assigning to
898          * private.
899          */
900         smp_wmb();
901         table->private = newinfo;
902
903         /*
904          * Even though table entries have now been swapped, other CPU's
905          * may still be using the old entries. This is okay, because
906          * resynchronization happens because of the locking done
907          * during the get_counters() routine.
908          */
909         local_bh_enable();
910
911 #ifdef CONFIG_AUDIT
912         if (audit_enabled) {
913                 struct audit_buffer *ab;
914
915                 ab = audit_log_start(current->audit_context, GFP_KERNEL,
916                                      AUDIT_NETFILTER_CFG);
917                 if (ab) {
918                         audit_log_format(ab, "table=%s family=%u entries=%u",
919                                          table->name, table->af,
920                                          private->number);
921                         audit_log_end(ab);
922                 }
923         }
924 #endif
925
926         return private;
927 }
928 EXPORT_SYMBOL_GPL(xt_replace_table);
929
930 struct xt_table *xt_register_table(struct net *net,
931                                    const struct xt_table *input_table,
932                                    struct xt_table_info *bootstrap,
933                                    struct xt_table_info *newinfo)
934 {
935         int ret;
936         struct xt_table_info *private;
937         struct xt_table *t, *table;
938
939         /* Don't add one object to multiple lists. */
940         table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
941         if (!table) {
942                 ret = -ENOMEM;
943                 goto out;
944         }
945
946         mutex_lock(&xt[table->af].mutex);
947         /* Don't autoload: we'd eat our tail... */
948         list_for_each_entry(t, &net->xt.tables[table->af], list) {
949                 if (strcmp(t->name, table->name) == 0) {
950                         ret = -EEXIST;
951                         goto unlock;
952                 }
953         }
954
955         /* Simplifies replace_table code. */
956         table->private = bootstrap;
957
958         if (!xt_replace_table(table, 0, newinfo, &ret))
959                 goto unlock;
960
961         private = table->private;
962         pr_debug("table->private->number = %u\n", private->number);
963
964         /* save number of initial entries */
965         private->initial_entries = private->number;
966
967         list_add(&table->list, &net->xt.tables[table->af]);
968         mutex_unlock(&xt[table->af].mutex);
969         return table;
970
971 unlock:
972         mutex_unlock(&xt[table->af].mutex);
973         kfree(table);
974 out:
975         return ERR_PTR(ret);
976 }
977 EXPORT_SYMBOL_GPL(xt_register_table);
978
979 void *xt_unregister_table(struct xt_table *table)
980 {
981         struct xt_table_info *private;
982
983         mutex_lock(&xt[table->af].mutex);
984         private = table->private;
985         list_del(&table->list);
986         mutex_unlock(&xt[table->af].mutex);
987         kfree(table);
988
989         return private;
990 }
991 EXPORT_SYMBOL_GPL(xt_unregister_table);
992
993 #ifdef CONFIG_PROC_FS
994 struct xt_names_priv {
995         struct seq_net_private p;
996         u_int8_t af;
997 };
998 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
999 {
1000         struct xt_names_priv *priv = seq->private;
1001         struct net *net = seq_file_net(seq);
1002         u_int8_t af = priv->af;
1003
1004         mutex_lock(&xt[af].mutex);
1005         return seq_list_start(&net->xt.tables[af], *pos);
1006 }
1007
1008 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1009 {
1010         struct xt_names_priv *priv = seq->private;
1011         struct net *net = seq_file_net(seq);
1012         u_int8_t af = priv->af;
1013
1014         return seq_list_next(v, &net->xt.tables[af], pos);
1015 }
1016
1017 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1018 {
1019         struct xt_names_priv *priv = seq->private;
1020         u_int8_t af = priv->af;
1021
1022         mutex_unlock(&xt[af].mutex);
1023 }
1024
1025 static int xt_table_seq_show(struct seq_file *seq, void *v)
1026 {
1027         struct xt_table *table = list_entry(v, struct xt_table, list);
1028
1029         if (*table->name)
1030                 seq_printf(seq, "%s\n", table->name);
1031         return 0;
1032 }
1033
1034 static const struct seq_operations xt_table_seq_ops = {
1035         .start  = xt_table_seq_start,
1036         .next   = xt_table_seq_next,
1037         .stop   = xt_table_seq_stop,
1038         .show   = xt_table_seq_show,
1039 };
1040
1041 static int xt_table_open(struct inode *inode, struct file *file)
1042 {
1043         int ret;
1044         struct xt_names_priv *priv;
1045
1046         ret = seq_open_net(inode, file, &xt_table_seq_ops,
1047                            sizeof(struct xt_names_priv));
1048         if (!ret) {
1049                 priv = ((struct seq_file *)file->private_data)->private;
1050                 priv->af = (unsigned long)PDE_DATA(inode);
1051         }
1052         return ret;
1053 }
1054
1055 static const struct file_operations xt_table_ops = {
1056         .owner   = THIS_MODULE,
1057         .open    = xt_table_open,
1058         .read    = seq_read,
1059         .llseek  = seq_lseek,
1060         .release = seq_release_net,
1061 };
1062
1063 /*
1064  * Traverse state for ip{,6}_{tables,matches} for helping crossing
1065  * the multi-AF mutexes.
1066  */
1067 struct nf_mttg_trav {
1068         struct list_head *head, *curr;
1069         uint8_t class, nfproto;
1070 };
1071
1072 enum {
1073         MTTG_TRAV_INIT,
1074         MTTG_TRAV_NFP_UNSPEC,
1075         MTTG_TRAV_NFP_SPEC,
1076         MTTG_TRAV_DONE,
1077 };
1078
1079 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1080     bool is_target)
1081 {
1082         static const uint8_t next_class[] = {
1083                 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1084                 [MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1085         };
1086         struct nf_mttg_trav *trav = seq->private;
1087
1088         switch (trav->class) {
1089         case MTTG_TRAV_INIT:
1090                 trav->class = MTTG_TRAV_NFP_UNSPEC;
1091                 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1092                 trav->head = trav->curr = is_target ?
1093                         &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1094                 break;
1095         case MTTG_TRAV_NFP_UNSPEC:
1096                 trav->curr = trav->curr->next;
1097                 if (trav->curr != trav->head)
1098                         break;
1099                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1100                 mutex_lock(&xt[trav->nfproto].mutex);
1101                 trav->head = trav->curr = is_target ?
1102                         &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1103                 trav->class = next_class[trav->class];
1104                 break;
1105         case MTTG_TRAV_NFP_SPEC:
1106                 trav->curr = trav->curr->next;
1107                 if (trav->curr != trav->head)
1108                         break;
1109                 /* fallthru, _stop will unlock */
1110         default:
1111                 return NULL;
1112         }
1113
1114         if (ppos != NULL)
1115                 ++*ppos;
1116         return trav;
1117 }
1118
1119 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1120     bool is_target)
1121 {
1122         struct nf_mttg_trav *trav = seq->private;
1123         unsigned int j;
1124
1125         trav->class = MTTG_TRAV_INIT;
1126         for (j = 0; j < *pos; ++j)
1127                 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1128                         return NULL;
1129         return trav;
1130 }
1131
1132 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1133 {
1134         struct nf_mttg_trav *trav = seq->private;
1135
1136         switch (trav->class) {
1137         case MTTG_TRAV_NFP_UNSPEC:
1138                 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1139                 break;
1140         case MTTG_TRAV_NFP_SPEC:
1141                 mutex_unlock(&xt[trav->nfproto].mutex);
1142                 break;
1143         }
1144 }
1145
1146 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1147 {
1148         return xt_mttg_seq_start(seq, pos, false);
1149 }
1150
1151 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1152 {
1153         return xt_mttg_seq_next(seq, v, ppos, false);
1154 }
1155
1156 static int xt_match_seq_show(struct seq_file *seq, void *v)
1157 {
1158         const struct nf_mttg_trav *trav = seq->private;
1159         const struct xt_match *match;
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                 match = list_entry(trav->curr, struct xt_match, list);
1167                 if (*match->name)
1168                         seq_printf(seq, "%s\n", match->name);
1169         }
1170         return 0;
1171 }
1172
1173 static const struct seq_operations xt_match_seq_ops = {
1174         .start  = xt_match_seq_start,
1175         .next   = xt_match_seq_next,
1176         .stop   = xt_mttg_seq_stop,
1177         .show   = xt_match_seq_show,
1178 };
1179
1180 static int xt_match_open(struct inode *inode, struct file *file)
1181 {
1182         struct nf_mttg_trav *trav;
1183         trav = __seq_open_private(file, &xt_match_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_match_ops = {
1192         .owner   = THIS_MODULE,
1193         .open    = xt_match_open,
1194         .read    = seq_read,
1195         .llseek  = seq_lseek,
1196         .release = seq_release_private,
1197 };
1198
1199 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1200 {
1201         return xt_mttg_seq_start(seq, pos, true);
1202 }
1203
1204 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1205 {
1206         return xt_mttg_seq_next(seq, v, ppos, true);
1207 }
1208
1209 static int xt_target_seq_show(struct seq_file *seq, void *v)
1210 {
1211         const struct nf_mttg_trav *trav = seq->private;
1212         const struct xt_target *target;
1213
1214         switch (trav->class) {
1215         case MTTG_TRAV_NFP_UNSPEC:
1216         case MTTG_TRAV_NFP_SPEC:
1217                 if (trav->curr == trav->head)
1218                         return 0;
1219                 target = list_entry(trav->curr, struct xt_target, list);
1220                 if (*target->name)
1221                         seq_printf(seq, "%s\n", target->name);
1222         }
1223         return 0;
1224 }
1225
1226 static const struct seq_operations xt_target_seq_ops = {
1227         .start  = xt_target_seq_start,
1228         .next   = xt_target_seq_next,
1229         .stop   = xt_mttg_seq_stop,
1230         .show   = xt_target_seq_show,
1231 };
1232
1233 static int xt_target_open(struct inode *inode, struct file *file)
1234 {
1235         struct nf_mttg_trav *trav;
1236         trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1237         if (!trav)
1238                 return -ENOMEM;
1239
1240         trav->nfproto = (unsigned long)PDE_DATA(inode);
1241         return 0;
1242 }
1243
1244 static const struct file_operations xt_target_ops = {
1245         .owner   = THIS_MODULE,
1246         .open    = xt_target_open,
1247         .read    = seq_read,
1248         .llseek  = seq_lseek,
1249         .release = seq_release_private,
1250 };
1251
1252 #define FORMAT_TABLES   "_tables_names"
1253 #define FORMAT_MATCHES  "_tables_matches"
1254 #define FORMAT_TARGETS  "_tables_targets"
1255
1256 #endif /* CONFIG_PROC_FS */
1257
1258 /**
1259  * xt_hook_link - set up hooks for a new table
1260  * @table:      table with metadata needed to set up hooks
1261  * @fn:         Hook function
1262  *
1263  * This function will take care of creating and registering the necessary
1264  * Netfilter hooks for XT tables.
1265  */
1266 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1267 {
1268         unsigned int hook_mask = table->valid_hooks;
1269         uint8_t i, num_hooks = hweight32(hook_mask);
1270         uint8_t hooknum;
1271         struct nf_hook_ops *ops;
1272         int ret;
1273
1274         ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1275         if (ops == NULL)
1276                 return ERR_PTR(-ENOMEM);
1277
1278         for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1279              hook_mask >>= 1, ++hooknum) {
1280                 if (!(hook_mask & 1))
1281                         continue;
1282                 ops[i].hook     = fn;
1283                 ops[i].pf       = table->af;
1284                 ops[i].hooknum  = hooknum;
1285                 ops[i].priority = table->priority;
1286                 ++i;
1287         }
1288
1289         ret = nf_register_hooks(ops, num_hooks);
1290         if (ret < 0) {
1291                 kfree(ops);
1292                 return ERR_PTR(ret);
1293         }
1294
1295         return ops;
1296 }
1297 EXPORT_SYMBOL_GPL(xt_hook_link);
1298
1299 /**
1300  * xt_hook_unlink - remove hooks for a table
1301  * @ops:        nf_hook_ops array as returned by nf_hook_link
1302  * @hook_mask:  the very same mask that was passed to nf_hook_link
1303  */
1304 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1305 {
1306         nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1307         kfree(ops);
1308 }
1309 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1310
1311 int xt_proto_init(struct net *net, u_int8_t af)
1312 {
1313 #ifdef CONFIG_PROC_FS
1314         char buf[XT_FUNCTION_MAXNAMELEN];
1315         struct proc_dir_entry *proc;
1316 #endif
1317
1318         if (af >= ARRAY_SIZE(xt_prefix))
1319                 return -EINVAL;
1320
1321
1322 #ifdef CONFIG_PROC_FS
1323         strlcpy(buf, xt_prefix[af], sizeof(buf));
1324         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1325         proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1326                                 (void *)(unsigned long)af);
1327         if (!proc)
1328                 goto out;
1329
1330         strlcpy(buf, xt_prefix[af], sizeof(buf));
1331         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1332         proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1333                                 (void *)(unsigned long)af);
1334         if (!proc)
1335                 goto out_remove_tables;
1336
1337         strlcpy(buf, xt_prefix[af], sizeof(buf));
1338         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1339         proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1340                                 (void *)(unsigned long)af);
1341         if (!proc)
1342                 goto out_remove_matches;
1343 #endif
1344
1345         return 0;
1346
1347 #ifdef CONFIG_PROC_FS
1348 out_remove_matches:
1349         strlcpy(buf, xt_prefix[af], sizeof(buf));
1350         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1351         remove_proc_entry(buf, net->proc_net);
1352
1353 out_remove_tables:
1354         strlcpy(buf, xt_prefix[af], sizeof(buf));
1355         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1356         remove_proc_entry(buf, net->proc_net);
1357 out:
1358         return -1;
1359 #endif
1360 }
1361 EXPORT_SYMBOL_GPL(xt_proto_init);
1362
1363 void xt_proto_fini(struct net *net, u_int8_t af)
1364 {
1365 #ifdef CONFIG_PROC_FS
1366         char buf[XT_FUNCTION_MAXNAMELEN];
1367
1368         strlcpy(buf, xt_prefix[af], sizeof(buf));
1369         strlcat(buf, FORMAT_TABLES, sizeof(buf));
1370         remove_proc_entry(buf, net->proc_net);
1371
1372         strlcpy(buf, xt_prefix[af], sizeof(buf));
1373         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1374         remove_proc_entry(buf, net->proc_net);
1375
1376         strlcpy(buf, xt_prefix[af], sizeof(buf));
1377         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1378         remove_proc_entry(buf, net->proc_net);
1379 #endif /*CONFIG_PROC_FS*/
1380 }
1381 EXPORT_SYMBOL_GPL(xt_proto_fini);
1382
1383 static int __net_init xt_net_init(struct net *net)
1384 {
1385         int i;
1386
1387         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1388                 INIT_LIST_HEAD(&net->xt.tables[i]);
1389         return 0;
1390 }
1391
1392 static struct pernet_operations xt_net_ops = {
1393         .init = xt_net_init,
1394 };
1395
1396 static int __init xt_init(void)
1397 {
1398         unsigned int i;
1399         int rv;
1400
1401         for_each_possible_cpu(i) {
1402                 seqcount_init(&per_cpu(xt_recseq, i));
1403         }
1404
1405         xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1406         if (!xt)
1407                 return -ENOMEM;
1408
1409         for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1410                 mutex_init(&xt[i].mutex);
1411 #ifdef CONFIG_COMPAT
1412                 mutex_init(&xt[i].compat_mutex);
1413                 xt[i].compat_tab = NULL;
1414 #endif
1415                 INIT_LIST_HEAD(&xt[i].target);
1416                 INIT_LIST_HEAD(&xt[i].match);
1417         }
1418         rv = register_pernet_subsys(&xt_net_ops);
1419         if (rv < 0)
1420                 kfree(xt);
1421         return rv;
1422 }
1423
1424 static void __exit xt_fini(void)
1425 {
1426         unregister_pernet_subsys(&xt_net_ops);
1427         kfree(xt);
1428 }
1429
1430 module_init(xt_init);
1431 module_exit(xt_fini);
1432