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