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