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