netfilter: implement NFPROTO_UNSPEC as a wildcard for extensions
[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
30
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
33 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
34
35 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
36
37 struct compat_delta {
38         struct compat_delta *next;
39         unsigned int offset;
40         short delta;
41 };
42
43 struct xt_af {
44         struct mutex mutex;
45         struct list_head match;
46         struct list_head target;
47 #ifdef CONFIG_COMPAT
48         struct mutex compat_mutex;
49         struct compat_delta *compat_offsets;
50 #endif
51 };
52
53 static struct xt_af *xt;
54
55 #ifdef DEBUG_IP_FIREWALL_USER
56 #define duprintf(format, args...) printk(format , ## args)
57 #else
58 #define duprintf(format, args...)
59 #endif
60
61 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
62         [NFPROTO_UNSPEC] = "x",
63         [NFPROTO_IPV4]   = "ip",
64         [NFPROTO_ARP]    = "arp",
65         [NFPROTO_BRIDGE] = "eb",
66         [NFPROTO_IPV6]   = "ip6",
67 };
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 /* Find target, grabs ref.  Returns ERR_PTR() on error. */
222 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
223 {
224         struct xt_target *t;
225         int err = 0;
226
227         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
228                 return ERR_PTR(-EINTR);
229
230         list_for_each_entry(t, &xt[af].target, list) {
231                 if (strcmp(t->name, name) == 0) {
232                         if (t->revision == revision) {
233                                 if (try_module_get(t->me)) {
234                                         mutex_unlock(&xt[af].mutex);
235                                         return t;
236                                 }
237                         } else
238                                 err = -EPROTOTYPE; /* Found something. */
239                 }
240         }
241         mutex_unlock(&xt[af].mutex);
242
243         if (af != NFPROTO_UNSPEC)
244                 /* Try searching again in the family-independent list */
245                 return xt_find_target(NFPROTO_UNSPEC, name, revision);
246
247         return ERR_PTR(err);
248 }
249 EXPORT_SYMBOL(xt_find_target);
250
251 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
252 {
253         struct xt_target *target;
254
255         target = try_then_request_module(xt_find_target(af, name, revision),
256                                          "%st_%s", xt_prefix[af], name);
257         if (IS_ERR(target) || !target)
258                 return NULL;
259         return target;
260 }
261 EXPORT_SYMBOL_GPL(xt_request_find_target);
262
263 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
264 {
265         const struct xt_match *m;
266         int have_rev = 0;
267
268         list_for_each_entry(m, &xt[af].match, list) {
269                 if (strcmp(m->name, name) == 0) {
270                         if (m->revision > *bestp)
271                                 *bestp = m->revision;
272                         if (m->revision == revision)
273                                 have_rev = 1;
274                 }
275         }
276         return have_rev;
277 }
278
279 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
280 {
281         const struct xt_target *t;
282         int have_rev = 0;
283
284         list_for_each_entry(t, &xt[af].target, list) {
285                 if (strcmp(t->name, name) == 0) {
286                         if (t->revision > *bestp)
287                                 *bestp = t->revision;
288                         if (t->revision == revision)
289                                 have_rev = 1;
290                 }
291         }
292         return have_rev;
293 }
294
295 /* Returns true or false (if no such extension at all) */
296 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
297                      int *err)
298 {
299         int have_rev, best = -1;
300
301         if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
302                 *err = -EINTR;
303                 return 1;
304         }
305         if (target == 1)
306                 have_rev = target_revfn(af, name, revision, &best);
307         else
308                 have_rev = match_revfn(af, name, revision, &best);
309         mutex_unlock(&xt[af].mutex);
310
311         /* Nothing at all?  Return 0 to try loading module. */
312         if (best == -1) {
313                 *err = -ENOENT;
314                 return 0;
315         }
316
317         *err = best;
318         if (!have_rev)
319                 *err = -EPROTONOSUPPORT;
320         return 1;
321 }
322 EXPORT_SYMBOL_GPL(xt_find_revision);
323
324 int xt_check_match(const struct xt_match *match, unsigned short family,
325                    unsigned int size, const char *table, unsigned int hook_mask,
326                    unsigned short proto, int inv_proto)
327 {
328         if (XT_ALIGN(match->matchsize) != size) {
329                 printk("%s_tables: %s match: invalid size %Zu != %u\n",
330                        xt_prefix[family], match->name,
331                        XT_ALIGN(match->matchsize), size);
332                 return -EINVAL;
333         }
334         if (match->table && strcmp(match->table, table)) {
335                 printk("%s_tables: %s match: only valid in %s table, not %s\n",
336                        xt_prefix[family], match->name, match->table, table);
337                 return -EINVAL;
338         }
339         if (match->hooks && (hook_mask & ~match->hooks) != 0) {
340                 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
341                        xt_prefix[family], match->name, hook_mask, match->hooks);
342                 return -EINVAL;
343         }
344         if (match->proto && (match->proto != proto || inv_proto)) {
345                 printk("%s_tables: %s match: only valid for protocol %u\n",
346                        xt_prefix[family], match->name, match->proto);
347                 return -EINVAL;
348         }
349         return 0;
350 }
351 EXPORT_SYMBOL_GPL(xt_check_match);
352
353 #ifdef CONFIG_COMPAT
354 int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta)
355 {
356         struct compat_delta *tmp;
357
358         tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
359         if (!tmp)
360                 return -ENOMEM;
361
362         tmp->offset = offset;
363         tmp->delta = delta;
364
365         if (xt[af].compat_offsets) {
366                 tmp->next = xt[af].compat_offsets->next;
367                 xt[af].compat_offsets->next = tmp;
368         } else {
369                 xt[af].compat_offsets = tmp;
370                 tmp->next = NULL;
371         }
372         return 0;
373 }
374 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
375
376 void xt_compat_flush_offsets(u_int8_t af)
377 {
378         struct compat_delta *tmp, *next;
379
380         if (xt[af].compat_offsets) {
381                 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
382                         next = tmp->next;
383                         kfree(tmp);
384                 }
385                 xt[af].compat_offsets = NULL;
386         }
387 }
388 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
389
390 short xt_compat_calc_jump(u_int8_t af, unsigned int offset)
391 {
392         struct compat_delta *tmp;
393         short delta;
394
395         for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
396                 if (tmp->offset < offset)
397                         delta += tmp->delta;
398         return delta;
399 }
400 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
401
402 int xt_compat_match_offset(const struct xt_match *match)
403 {
404         u_int16_t csize = match->compatsize ? : match->matchsize;
405         return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
406 }
407 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
408
409 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
410                               unsigned int *size)
411 {
412         const struct xt_match *match = m->u.kernel.match;
413         struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
414         int pad, off = xt_compat_match_offset(match);
415         u_int16_t msize = cm->u.user.match_size;
416
417         m = *dstptr;
418         memcpy(m, cm, sizeof(*cm));
419         if (match->compat_from_user)
420                 match->compat_from_user(m->data, cm->data);
421         else
422                 memcpy(m->data, cm->data, msize - sizeof(*cm));
423         pad = XT_ALIGN(match->matchsize) - match->matchsize;
424         if (pad > 0)
425                 memset(m->data + match->matchsize, 0, pad);
426
427         msize += off;
428         m->u.user.match_size = msize;
429
430         *size += off;
431         *dstptr += msize;
432         return 0;
433 }
434 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
435
436 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
437                             unsigned int *size)
438 {
439         const struct xt_match *match = m->u.kernel.match;
440         struct compat_xt_entry_match __user *cm = *dstptr;
441         int off = xt_compat_match_offset(match);
442         u_int16_t msize = m->u.user.match_size - off;
443
444         if (copy_to_user(cm, m, sizeof(*cm)) ||
445             put_user(msize, &cm->u.user.match_size) ||
446             copy_to_user(cm->u.user.name, m->u.kernel.match->name,
447                          strlen(m->u.kernel.match->name) + 1))
448                 return -EFAULT;
449
450         if (match->compat_to_user) {
451                 if (match->compat_to_user((void __user *)cm->data, m->data))
452                         return -EFAULT;
453         } else {
454                 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
455                         return -EFAULT;
456         }
457
458         *size -= off;
459         *dstptr += msize;
460         return 0;
461 }
462 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
463 #endif /* CONFIG_COMPAT */
464
465 int xt_check_target(const struct xt_target *target, unsigned short family,
466                     unsigned int size, const char *table, unsigned int hook_mask,
467                     unsigned short proto, int inv_proto)
468 {
469         if (XT_ALIGN(target->targetsize) != size) {
470                 printk("%s_tables: %s target: invalid size %Zu != %u\n",
471                        xt_prefix[family], target->name,
472                        XT_ALIGN(target->targetsize), size);
473                 return -EINVAL;
474         }
475         if (target->table && strcmp(target->table, table)) {
476                 printk("%s_tables: %s target: only valid in %s table, not %s\n",
477                        xt_prefix[family], target->name, target->table, table);
478                 return -EINVAL;
479         }
480         if (target->hooks && (hook_mask & ~target->hooks) != 0) {
481                 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
482                        xt_prefix[family], target->name, hook_mask,
483                        target->hooks);
484                 return -EINVAL;
485         }
486         if (target->proto && (target->proto != proto || inv_proto)) {
487                 printk("%s_tables: %s target: only valid for protocol %u\n",
488                        xt_prefix[family], target->name, target->proto);
489                 return -EINVAL;
490         }
491         return 0;
492 }
493 EXPORT_SYMBOL_GPL(xt_check_target);
494
495 #ifdef CONFIG_COMPAT
496 int xt_compat_target_offset(const struct xt_target *target)
497 {
498         u_int16_t csize = target->compatsize ? : target->targetsize;
499         return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
500 }
501 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
502
503 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
504                                 unsigned int *size)
505 {
506         const struct xt_target *target = t->u.kernel.target;
507         struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
508         int pad, off = xt_compat_target_offset(target);
509         u_int16_t tsize = ct->u.user.target_size;
510
511         t = *dstptr;
512         memcpy(t, ct, sizeof(*ct));
513         if (target->compat_from_user)
514                 target->compat_from_user(t->data, ct->data);
515         else
516                 memcpy(t->data, ct->data, tsize - sizeof(*ct));
517         pad = XT_ALIGN(target->targetsize) - target->targetsize;
518         if (pad > 0)
519                 memset(t->data + target->targetsize, 0, pad);
520
521         tsize += off;
522         t->u.user.target_size = tsize;
523
524         *size += off;
525         *dstptr += tsize;
526 }
527 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
528
529 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
530                              unsigned int *size)
531 {
532         const struct xt_target *target = t->u.kernel.target;
533         struct compat_xt_entry_target __user *ct = *dstptr;
534         int off = xt_compat_target_offset(target);
535         u_int16_t tsize = t->u.user.target_size - off;
536
537         if (copy_to_user(ct, t, sizeof(*ct)) ||
538             put_user(tsize, &ct->u.user.target_size) ||
539             copy_to_user(ct->u.user.name, t->u.kernel.target->name,
540                          strlen(t->u.kernel.target->name) + 1))
541                 return -EFAULT;
542
543         if (target->compat_to_user) {
544                 if (target->compat_to_user((void __user *)ct->data, t->data))
545                         return -EFAULT;
546         } else {
547                 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
548                         return -EFAULT;
549         }
550
551         *size -= off;
552         *dstptr += tsize;
553         return 0;
554 }
555 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
556 #endif
557
558 struct xt_table_info *xt_alloc_table_info(unsigned int size)
559 {
560         struct xt_table_info *newinfo;
561         int cpu;
562
563         /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
564         if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
565                 return NULL;
566
567         newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
568         if (!newinfo)
569                 return NULL;
570
571         newinfo->size = size;
572
573         for_each_possible_cpu(cpu) {
574                 if (size <= PAGE_SIZE)
575                         newinfo->entries[cpu] = kmalloc_node(size,
576                                                         GFP_KERNEL,
577                                                         cpu_to_node(cpu));
578                 else
579                         newinfo->entries[cpu] = vmalloc_node(size,
580                                                         cpu_to_node(cpu));
581
582                 if (newinfo->entries[cpu] == NULL) {
583                         xt_free_table_info(newinfo);
584                         return NULL;
585                 }
586         }
587
588         return newinfo;
589 }
590 EXPORT_SYMBOL(xt_alloc_table_info);
591
592 void xt_free_table_info(struct xt_table_info *info)
593 {
594         int cpu;
595
596         for_each_possible_cpu(cpu) {
597                 if (info->size <= PAGE_SIZE)
598                         kfree(info->entries[cpu]);
599                 else
600                         vfree(info->entries[cpu]);
601         }
602         kfree(info);
603 }
604 EXPORT_SYMBOL(xt_free_table_info);
605
606 /* Find table by name, grabs mutex & ref.  Returns ERR_PTR() on error. */
607 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
608                                     const char *name)
609 {
610         struct xt_table *t;
611
612         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
613                 return ERR_PTR(-EINTR);
614
615         list_for_each_entry(t, &net->xt.tables[af], list)
616                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
617                         return t;
618         mutex_unlock(&xt[af].mutex);
619         return NULL;
620 }
621 EXPORT_SYMBOL_GPL(xt_find_table_lock);
622
623 void xt_table_unlock(struct xt_table *table)
624 {
625         mutex_unlock(&xt[table->af].mutex);
626 }
627 EXPORT_SYMBOL_GPL(xt_table_unlock);
628
629 #ifdef CONFIG_COMPAT
630 void xt_compat_lock(u_int8_t af)
631 {
632         mutex_lock(&xt[af].compat_mutex);
633 }
634 EXPORT_SYMBOL_GPL(xt_compat_lock);
635
636 void xt_compat_unlock(u_int8_t af)
637 {
638         mutex_unlock(&xt[af].compat_mutex);
639 }
640 EXPORT_SYMBOL_GPL(xt_compat_unlock);
641 #endif
642
643 struct xt_table_info *
644 xt_replace_table(struct xt_table *table,
645               unsigned int num_counters,
646               struct xt_table_info *newinfo,
647               int *error)
648 {
649         struct xt_table_info *oldinfo, *private;
650
651         /* Do the substitution. */
652         write_lock_bh(&table->lock);
653         private = table->private;
654         /* Check inside lock: is the old number correct? */
655         if (num_counters != private->number) {
656                 duprintf("num_counters != table->private->number (%u/%u)\n",
657                          num_counters, private->number);
658                 write_unlock_bh(&table->lock);
659                 *error = -EAGAIN;
660                 return NULL;
661         }
662         oldinfo = private;
663         table->private = newinfo;
664         newinfo->initial_entries = oldinfo->initial_entries;
665         write_unlock_bh(&table->lock);
666
667         return oldinfo;
668 }
669 EXPORT_SYMBOL_GPL(xt_replace_table);
670
671 struct xt_table *xt_register_table(struct net *net, struct xt_table *table,
672                                    struct xt_table_info *bootstrap,
673                                    struct xt_table_info *newinfo)
674 {
675         int ret;
676         struct xt_table_info *private;
677         struct xt_table *t;
678
679         /* Don't add one object to multiple lists. */
680         table = kmemdup(table, sizeof(struct xt_table), GFP_KERNEL);
681         if (!table) {
682                 ret = -ENOMEM;
683                 goto out;
684         }
685
686         ret = mutex_lock_interruptible(&xt[table->af].mutex);
687         if (ret != 0)
688                 goto out_free;
689
690         /* Don't autoload: we'd eat our tail... */
691         list_for_each_entry(t, &net->xt.tables[table->af], list) {
692                 if (strcmp(t->name, table->name) == 0) {
693                         ret = -EEXIST;
694                         goto unlock;
695                 }
696         }
697
698         /* Simplifies replace_table code. */
699         table->private = bootstrap;
700         rwlock_init(&table->lock);
701         if (!xt_replace_table(table, 0, newinfo, &ret))
702                 goto unlock;
703
704         private = table->private;
705         duprintf("table->private->number = %u\n", private->number);
706
707         /* save number of initial entries */
708         private->initial_entries = private->number;
709
710         list_add(&table->list, &net->xt.tables[table->af]);
711         mutex_unlock(&xt[table->af].mutex);
712         return table;
713
714  unlock:
715         mutex_unlock(&xt[table->af].mutex);
716 out_free:
717         kfree(table);
718 out:
719         return ERR_PTR(ret);
720 }
721 EXPORT_SYMBOL_GPL(xt_register_table);
722
723 void *xt_unregister_table(struct xt_table *table)
724 {
725         struct xt_table_info *private;
726
727         mutex_lock(&xt[table->af].mutex);
728         private = table->private;
729         list_del(&table->list);
730         mutex_unlock(&xt[table->af].mutex);
731         kfree(table);
732
733         return private;
734 }
735 EXPORT_SYMBOL_GPL(xt_unregister_table);
736
737 #ifdef CONFIG_PROC_FS
738 struct xt_names_priv {
739         struct seq_net_private p;
740         u_int8_t af;
741 };
742 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
743 {
744         struct xt_names_priv *priv = seq->private;
745         struct net *net = seq_file_net(seq);
746         u_int8_t af = priv->af;
747
748         mutex_lock(&xt[af].mutex);
749         return seq_list_start(&net->xt.tables[af], *pos);
750 }
751
752 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
753 {
754         struct xt_names_priv *priv = seq->private;
755         struct net *net = seq_file_net(seq);
756         u_int8_t af = priv->af;
757
758         return seq_list_next(v, &net->xt.tables[af], pos);
759 }
760
761 static void xt_table_seq_stop(struct seq_file *seq, void *v)
762 {
763         struct xt_names_priv *priv = seq->private;
764         u_int8_t af = priv->af;
765
766         mutex_unlock(&xt[af].mutex);
767 }
768
769 static int xt_table_seq_show(struct seq_file *seq, void *v)
770 {
771         struct xt_table *table = list_entry(v, struct xt_table, list);
772
773         if (strlen(table->name))
774                 return seq_printf(seq, "%s\n", table->name);
775         else
776                 return 0;
777 }
778
779 static const struct seq_operations xt_table_seq_ops = {
780         .start  = xt_table_seq_start,
781         .next   = xt_table_seq_next,
782         .stop   = xt_table_seq_stop,
783         .show   = xt_table_seq_show,
784 };
785
786 static int xt_table_open(struct inode *inode, struct file *file)
787 {
788         int ret;
789         struct xt_names_priv *priv;
790
791         ret = seq_open_net(inode, file, &xt_table_seq_ops,
792                            sizeof(struct xt_names_priv));
793         if (!ret) {
794                 priv = ((struct seq_file *)file->private_data)->private;
795                 priv->af = (unsigned long)PDE(inode)->data;
796         }
797         return ret;
798 }
799
800 static const struct file_operations xt_table_ops = {
801         .owner   = THIS_MODULE,
802         .open    = xt_table_open,
803         .read    = seq_read,
804         .llseek  = seq_lseek,
805         .release = seq_release_net,
806 };
807
808 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
809 {
810         struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
811         u_int16_t af = (unsigned long)pde->data;
812
813         mutex_lock(&xt[af].mutex);
814         return seq_list_start(&xt[af].match, *pos);
815 }
816
817 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *pos)
818 {
819         struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
820         u_int16_t af = (unsigned long)pde->data;
821
822         return seq_list_next(v, &xt[af].match, pos);
823 }
824
825 static void xt_match_seq_stop(struct seq_file *seq, void *v)
826 {
827         struct proc_dir_entry *pde = seq->private;
828         u_int16_t af = (unsigned long)pde->data;
829
830         mutex_unlock(&xt[af].mutex);
831 }
832
833 static int xt_match_seq_show(struct seq_file *seq, void *v)
834 {
835         struct xt_match *match = list_entry(v, struct xt_match, list);
836
837         if (strlen(match->name))
838                 return seq_printf(seq, "%s\n", match->name);
839         else
840                 return 0;
841 }
842
843 static const struct seq_operations xt_match_seq_ops = {
844         .start  = xt_match_seq_start,
845         .next   = xt_match_seq_next,
846         .stop   = xt_match_seq_stop,
847         .show   = xt_match_seq_show,
848 };
849
850 static int xt_match_open(struct inode *inode, struct file *file)
851 {
852         int ret;
853
854         ret = seq_open(file, &xt_match_seq_ops);
855         if (!ret) {
856                 struct seq_file *seq = file->private_data;
857
858                 seq->private = PDE(inode);
859         }
860         return ret;
861 }
862
863 static const struct file_operations xt_match_ops = {
864         .owner   = THIS_MODULE,
865         .open    = xt_match_open,
866         .read    = seq_read,
867         .llseek  = seq_lseek,
868         .release = seq_release,
869 };
870
871 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
872 {
873         struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
874         u_int16_t af = (unsigned long)pde->data;
875
876         mutex_lock(&xt[af].mutex);
877         return seq_list_start(&xt[af].target, *pos);
878 }
879
880 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *pos)
881 {
882         struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
883         u_int16_t af = (unsigned long)pde->data;
884
885         return seq_list_next(v, &xt[af].target, pos);
886 }
887
888 static void xt_target_seq_stop(struct seq_file *seq, void *v)
889 {
890         struct proc_dir_entry *pde = seq->private;
891         u_int16_t af = (unsigned long)pde->data;
892
893         mutex_unlock(&xt[af].mutex);
894 }
895
896 static int xt_target_seq_show(struct seq_file *seq, void *v)
897 {
898         struct xt_target *target = list_entry(v, struct xt_target, list);
899
900         if (strlen(target->name))
901                 return seq_printf(seq, "%s\n", target->name);
902         else
903                 return 0;
904 }
905
906 static const struct seq_operations xt_target_seq_ops = {
907         .start  = xt_target_seq_start,
908         .next   = xt_target_seq_next,
909         .stop   = xt_target_seq_stop,
910         .show   = xt_target_seq_show,
911 };
912
913 static int xt_target_open(struct inode *inode, struct file *file)
914 {
915         int ret;
916
917         ret = seq_open(file, &xt_target_seq_ops);
918         if (!ret) {
919                 struct seq_file *seq = file->private_data;
920
921                 seq->private = PDE(inode);
922         }
923         return ret;
924 }
925
926 static const struct file_operations xt_target_ops = {
927         .owner   = THIS_MODULE,
928         .open    = xt_target_open,
929         .read    = seq_read,
930         .llseek  = seq_lseek,
931         .release = seq_release,
932 };
933
934 #define FORMAT_TABLES   "_tables_names"
935 #define FORMAT_MATCHES  "_tables_matches"
936 #define FORMAT_TARGETS  "_tables_targets"
937
938 #endif /* CONFIG_PROC_FS */
939
940 int xt_proto_init(struct net *net, u_int8_t af)
941 {
942 #ifdef CONFIG_PROC_FS
943         char buf[XT_FUNCTION_MAXNAMELEN];
944         struct proc_dir_entry *proc;
945 #endif
946
947         if (af >= ARRAY_SIZE(xt_prefix))
948                 return -EINVAL;
949
950
951 #ifdef CONFIG_PROC_FS
952         strlcpy(buf, xt_prefix[af], sizeof(buf));
953         strlcat(buf, FORMAT_TABLES, sizeof(buf));
954         proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
955                                 (void *)(unsigned long)af);
956         if (!proc)
957                 goto out;
958
959         strlcpy(buf, xt_prefix[af], sizeof(buf));
960         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
961         proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
962                                 (void *)(unsigned long)af);
963         if (!proc)
964                 goto out_remove_tables;
965
966         strlcpy(buf, xt_prefix[af], sizeof(buf));
967         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
968         proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
969                                 (void *)(unsigned long)af);
970         if (!proc)
971                 goto out_remove_matches;
972 #endif
973
974         return 0;
975
976 #ifdef CONFIG_PROC_FS
977 out_remove_matches:
978         strlcpy(buf, xt_prefix[af], sizeof(buf));
979         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
980         proc_net_remove(net, buf);
981
982 out_remove_tables:
983         strlcpy(buf, xt_prefix[af], sizeof(buf));
984         strlcat(buf, FORMAT_TABLES, sizeof(buf));
985         proc_net_remove(net, buf);
986 out:
987         return -1;
988 #endif
989 }
990 EXPORT_SYMBOL_GPL(xt_proto_init);
991
992 void xt_proto_fini(struct net *net, u_int8_t af)
993 {
994 #ifdef CONFIG_PROC_FS
995         char buf[XT_FUNCTION_MAXNAMELEN];
996
997         strlcpy(buf, xt_prefix[af], sizeof(buf));
998         strlcat(buf, FORMAT_TABLES, sizeof(buf));
999         proc_net_remove(net, buf);
1000
1001         strlcpy(buf, xt_prefix[af], sizeof(buf));
1002         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1003         proc_net_remove(net, buf);
1004
1005         strlcpy(buf, xt_prefix[af], sizeof(buf));
1006         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1007         proc_net_remove(net, buf);
1008 #endif /*CONFIG_PROC_FS*/
1009 }
1010 EXPORT_SYMBOL_GPL(xt_proto_fini);
1011
1012 static int __net_init xt_net_init(struct net *net)
1013 {
1014         int i;
1015
1016         for (i = 0; i < NFPROTO_NUMPROTO; i++)
1017                 INIT_LIST_HEAD(&net->xt.tables[i]);
1018         return 0;
1019 }
1020
1021 static struct pernet_operations xt_net_ops = {
1022         .init = xt_net_init,
1023 };
1024
1025 static int __init xt_init(void)
1026 {
1027         int i, rv;
1028
1029         xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1030         if (!xt)
1031                 return -ENOMEM;
1032
1033         for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1034                 mutex_init(&xt[i].mutex);
1035 #ifdef CONFIG_COMPAT
1036                 mutex_init(&xt[i].compat_mutex);
1037                 xt[i].compat_offsets = NULL;
1038 #endif
1039                 INIT_LIST_HEAD(&xt[i].target);
1040                 INIT_LIST_HEAD(&xt[i].match);
1041         }
1042         rv = register_pernet_subsys(&xt_net_ops);
1043         if (rv < 0)
1044                 kfree(xt);
1045         return rv;
1046 }
1047
1048 static void __exit xt_fini(void)
1049 {
1050         unregister_pernet_subsys(&xt_net_ops);
1051         kfree(xt);
1052 }
1053
1054 module_init(xt_init);
1055 module_exit(xt_fini);
1056