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