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