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