[NETFILTER]: ip_tables: move compat offset calculation to x_tables
[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         struct list_head tables;
48 #ifdef CONFIG_COMPAT
49         struct mutex compat_mutex;
50         struct compat_delta *compat_offsets;
51 #endif
52 };
53
54 static struct xt_af *xt;
55
56 #ifdef DEBUG_IP_FIREWALL_USER
57 #define duprintf(format, args...) printk(format , ## args)
58 #else
59 #define duprintf(format, args...)
60 #endif
61
62 enum {
63         TABLE,
64         TARGET,
65         MATCH,
66 };
67
68 static const char *xt_prefix[NPROTO] = {
69         [AF_INET]       = "ip",
70         [AF_INET6]      = "ip6",
71         [NF_ARP]        = "arp",
72 };
73
74 /* Registration hooks for targets. */
75 int
76 xt_register_target(struct xt_target *target)
77 {
78         int ret, af = target->family;
79
80         ret = mutex_lock_interruptible(&xt[af].mutex);
81         if (ret != 0)
82                 return ret;
83         list_add(&target->list, &xt[af].target);
84         mutex_unlock(&xt[af].mutex);
85         return ret;
86 }
87 EXPORT_SYMBOL(xt_register_target);
88
89 void
90 xt_unregister_target(struct xt_target *target)
91 {
92         int af = target->family;
93
94         mutex_lock(&xt[af].mutex);
95         list_del(&target->list);
96         mutex_unlock(&xt[af].mutex);
97 }
98 EXPORT_SYMBOL(xt_unregister_target);
99
100 int
101 xt_register_targets(struct xt_target *target, unsigned int n)
102 {
103         unsigned int i;
104         int err = 0;
105
106         for (i = 0; i < n; i++) {
107                 err = xt_register_target(&target[i]);
108                 if (err)
109                         goto err;
110         }
111         return err;
112
113 err:
114         if (i > 0)
115                 xt_unregister_targets(target, i);
116         return err;
117 }
118 EXPORT_SYMBOL(xt_register_targets);
119
120 void
121 xt_unregister_targets(struct xt_target *target, unsigned int n)
122 {
123         unsigned int i;
124
125         for (i = 0; i < n; i++)
126                 xt_unregister_target(&target[i]);
127 }
128 EXPORT_SYMBOL(xt_unregister_targets);
129
130 int
131 xt_register_match(struct xt_match *match)
132 {
133         int ret, af = match->family;
134
135         ret = mutex_lock_interruptible(&xt[af].mutex);
136         if (ret != 0)
137                 return ret;
138
139         list_add(&match->list, &xt[af].match);
140         mutex_unlock(&xt[af].mutex);
141
142         return ret;
143 }
144 EXPORT_SYMBOL(xt_register_match);
145
146 void
147 xt_unregister_match(struct xt_match *match)
148 {
149         int af =  match->family;
150
151         mutex_lock(&xt[af].mutex);
152         list_del(&match->list);
153         mutex_unlock(&xt[af].mutex);
154 }
155 EXPORT_SYMBOL(xt_unregister_match);
156
157 int
158 xt_register_matches(struct xt_match *match, unsigned int n)
159 {
160         unsigned int i;
161         int err = 0;
162
163         for (i = 0; i < n; i++) {
164                 err = xt_register_match(&match[i]);
165                 if (err)
166                         goto err;
167         }
168         return err;
169
170 err:
171         if (i > 0)
172                 xt_unregister_matches(match, i);
173         return err;
174 }
175 EXPORT_SYMBOL(xt_register_matches);
176
177 void
178 xt_unregister_matches(struct xt_match *match, unsigned int n)
179 {
180         unsigned int i;
181
182         for (i = 0; i < n; i++)
183                 xt_unregister_match(&match[i]);
184 }
185 EXPORT_SYMBOL(xt_unregister_matches);
186
187
188 /*
189  * These are weird, but module loading must not be done with mutex
190  * held (since they will register), and we have to have a single
191  * function to use try_then_request_module().
192  */
193
194 /* Find match, grabs ref.  Returns ERR_PTR() on error. */
195 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
196 {
197         struct xt_match *m;
198         int err = 0;
199
200         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
201                 return ERR_PTR(-EINTR);
202
203         list_for_each_entry(m, &xt[af].match, list) {
204                 if (strcmp(m->name, name) == 0) {
205                         if (m->revision == revision) {
206                                 if (try_module_get(m->me)) {
207                                         mutex_unlock(&xt[af].mutex);
208                                         return m;
209                                 }
210                         } else
211                                 err = -EPROTOTYPE; /* Found something. */
212                 }
213         }
214         mutex_unlock(&xt[af].mutex);
215         return ERR_PTR(err);
216 }
217 EXPORT_SYMBOL(xt_find_match);
218
219 /* Find target, grabs ref.  Returns ERR_PTR() on error. */
220 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
221 {
222         struct xt_target *t;
223         int err = 0;
224
225         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
226                 return ERR_PTR(-EINTR);
227
228         list_for_each_entry(t, &xt[af].target, list) {
229                 if (strcmp(t->name, name) == 0) {
230                         if (t->revision == revision) {
231                                 if (try_module_get(t->me)) {
232                                         mutex_unlock(&xt[af].mutex);
233                                         return t;
234                                 }
235                         } else
236                                 err = -EPROTOTYPE; /* Found something. */
237                 }
238         }
239         mutex_unlock(&xt[af].mutex);
240         return ERR_PTR(err);
241 }
242 EXPORT_SYMBOL(xt_find_target);
243
244 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
245 {
246         struct xt_target *target;
247
248         target = try_then_request_module(xt_find_target(af, name, revision),
249                                          "%st_%s", xt_prefix[af], name);
250         if (IS_ERR(target) || !target)
251                 return NULL;
252         return target;
253 }
254 EXPORT_SYMBOL_GPL(xt_request_find_target);
255
256 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
257 {
258         struct xt_match *m;
259         int have_rev = 0;
260
261         list_for_each_entry(m, &xt[af].match, list) {
262                 if (strcmp(m->name, name) == 0) {
263                         if (m->revision > *bestp)
264                                 *bestp = m->revision;
265                         if (m->revision == revision)
266                                 have_rev = 1;
267                 }
268         }
269         return have_rev;
270 }
271
272 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
273 {
274         struct xt_target *t;
275         int have_rev = 0;
276
277         list_for_each_entry(t, &xt[af].target, list) {
278                 if (strcmp(t->name, name) == 0) {
279                         if (t->revision > *bestp)
280                                 *bestp = t->revision;
281                         if (t->revision == revision)
282                                 have_rev = 1;
283                 }
284         }
285         return have_rev;
286 }
287
288 /* Returns true or false (if no such extension at all) */
289 int xt_find_revision(int af, const char *name, u8 revision, int target,
290                      int *err)
291 {
292         int have_rev, best = -1;
293
294         if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
295                 *err = -EINTR;
296                 return 1;
297         }
298         if (target == 1)
299                 have_rev = target_revfn(af, name, revision, &best);
300         else
301                 have_rev = match_revfn(af, name, revision, &best);
302         mutex_unlock(&xt[af].mutex);
303
304         /* Nothing at all?  Return 0 to try loading module. */
305         if (best == -1) {
306                 *err = -ENOENT;
307                 return 0;
308         }
309
310         *err = best;
311         if (!have_rev)
312                 *err = -EPROTONOSUPPORT;
313         return 1;
314 }
315 EXPORT_SYMBOL_GPL(xt_find_revision);
316
317 int xt_check_match(const struct xt_match *match, unsigned short family,
318                    unsigned int size, const char *table, unsigned int hook_mask,
319                    unsigned short proto, int inv_proto)
320 {
321         if (XT_ALIGN(match->matchsize) != size) {
322                 printk("%s_tables: %s match: invalid size %Zu != %u\n",
323                        xt_prefix[family], match->name,
324                        XT_ALIGN(match->matchsize), size);
325                 return -EINVAL;
326         }
327         if (match->table && strcmp(match->table, table)) {
328                 printk("%s_tables: %s match: only valid in %s table, not %s\n",
329                        xt_prefix[family], match->name, match->table, table);
330                 return -EINVAL;
331         }
332         if (match->hooks && (hook_mask & ~match->hooks) != 0) {
333                 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
334                        xt_prefix[family], match->name, hook_mask, match->hooks);
335                 return -EINVAL;
336         }
337         if (match->proto && (match->proto != proto || inv_proto)) {
338                 printk("%s_tables: %s match: only valid for protocol %u\n",
339                        xt_prefix[family], match->name, match->proto);
340                 return -EINVAL;
341         }
342         return 0;
343 }
344 EXPORT_SYMBOL_GPL(xt_check_match);
345
346 #ifdef CONFIG_COMPAT
347 int xt_compat_add_offset(int af, unsigned int offset, short delta)
348 {
349         struct compat_delta *tmp;
350
351         tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
352         if (!tmp)
353                 return -ENOMEM;
354
355         tmp->offset = offset;
356         tmp->delta = delta;
357
358         if (xt[af].compat_offsets) {
359                 tmp->next = xt[af].compat_offsets->next;
360                 xt[af].compat_offsets->next = tmp;
361         } else {
362                 xt[af].compat_offsets = tmp;
363                 tmp->next = NULL;
364         }
365         return 0;
366 }
367 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
368
369 void xt_compat_flush_offsets(int af)
370 {
371         struct compat_delta *tmp, *next;
372
373         if (xt[af].compat_offsets) {
374                 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
375                         next = tmp->next;
376                         kfree(tmp);
377                 }
378                 xt[af].compat_offsets = NULL;
379         }
380 }
381 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
382
383 short xt_compat_calc_jump(int af, unsigned int offset)
384 {
385         struct compat_delta *tmp;
386         short delta;
387
388         for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
389                 if (tmp->offset < offset)
390                         delta += tmp->delta;
391         return delta;
392 }
393 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
394
395 int xt_compat_match_offset(struct xt_match *match)
396 {
397         u_int16_t csize = match->compatsize ? : match->matchsize;
398         return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
399 }
400 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
401
402 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
403                               int *size)
404 {
405         struct xt_match *match = m->u.kernel.match;
406         struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
407         int pad, off = xt_compat_match_offset(match);
408         u_int16_t msize = cm->u.user.match_size;
409
410         m = *dstptr;
411         memcpy(m, cm, sizeof(*cm));
412         if (match->compat_from_user)
413                 match->compat_from_user(m->data, cm->data);
414         else
415                 memcpy(m->data, cm->data, msize - sizeof(*cm));
416         pad = XT_ALIGN(match->matchsize) - match->matchsize;
417         if (pad > 0)
418                 memset(m->data + match->matchsize, 0, pad);
419
420         msize += off;
421         m->u.user.match_size = msize;
422
423         *size += off;
424         *dstptr += msize;
425         return 0;
426 }
427 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
428
429 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
430                             int *size)
431 {
432         struct xt_match *match = m->u.kernel.match;
433         struct compat_xt_entry_match __user *cm = *dstptr;
434         int off = xt_compat_match_offset(match);
435         u_int16_t msize = m->u.user.match_size - off;
436
437         if (copy_to_user(cm, m, sizeof(*cm)) ||
438             put_user(msize, &cm->u.user.match_size) ||
439             copy_to_user(cm->u.user.name, m->u.kernel.match->name,
440                          strlen(m->u.kernel.match->name) + 1))
441                 return -EFAULT;
442
443         if (match->compat_to_user) {
444                 if (match->compat_to_user((void __user *)cm->data, m->data))
445                         return -EFAULT;
446         } else {
447                 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
448                         return -EFAULT;
449         }
450
451         *size -= off;
452         *dstptr += msize;
453         return 0;
454 }
455 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
456 #endif /* CONFIG_COMPAT */
457
458 int xt_check_target(const struct xt_target *target, unsigned short family,
459                     unsigned int size, const char *table, unsigned int hook_mask,
460                     unsigned short proto, int inv_proto)
461 {
462         if (XT_ALIGN(target->targetsize) != size) {
463                 printk("%s_tables: %s target: invalid size %Zu != %u\n",
464                        xt_prefix[family], target->name,
465                        XT_ALIGN(target->targetsize), size);
466                 return -EINVAL;
467         }
468         if (target->table && strcmp(target->table, table)) {
469                 printk("%s_tables: %s target: only valid in %s table, not %s\n",
470                        xt_prefix[family], target->name, target->table, table);
471                 return -EINVAL;
472         }
473         if (target->hooks && (hook_mask & ~target->hooks) != 0) {
474                 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
475                        xt_prefix[family], target->name, hook_mask,
476                        target->hooks);
477                 return -EINVAL;
478         }
479         if (target->proto && (target->proto != proto || inv_proto)) {
480                 printk("%s_tables: %s target: only valid for protocol %u\n",
481                        xt_prefix[family], target->name, target->proto);
482                 return -EINVAL;
483         }
484         return 0;
485 }
486 EXPORT_SYMBOL_GPL(xt_check_target);
487
488 #ifdef CONFIG_COMPAT
489 int xt_compat_target_offset(struct xt_target *target)
490 {
491         u_int16_t csize = target->compatsize ? : target->targetsize;
492         return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
493 }
494 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
495
496 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
497                                 int *size)
498 {
499         struct xt_target *target = t->u.kernel.target;
500         struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
501         int pad, off = xt_compat_target_offset(target);
502         u_int16_t tsize = ct->u.user.target_size;
503
504         t = *dstptr;
505         memcpy(t, ct, sizeof(*ct));
506         if (target->compat_from_user)
507                 target->compat_from_user(t->data, ct->data);
508         else
509                 memcpy(t->data, ct->data, tsize - sizeof(*ct));
510         pad = XT_ALIGN(target->targetsize) - target->targetsize;
511         if (pad > 0)
512                 memset(t->data + target->targetsize, 0, pad);
513
514         tsize += off;
515         t->u.user.target_size = tsize;
516
517         *size += off;
518         *dstptr += tsize;
519 }
520 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
521
522 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
523                              int *size)
524 {
525         struct xt_target *target = t->u.kernel.target;
526         struct compat_xt_entry_target __user *ct = *dstptr;
527         int off = xt_compat_target_offset(target);
528         u_int16_t tsize = t->u.user.target_size - off;
529
530         if (copy_to_user(ct, t, sizeof(*ct)) ||
531             put_user(tsize, &ct->u.user.target_size) ||
532             copy_to_user(ct->u.user.name, t->u.kernel.target->name,
533                          strlen(t->u.kernel.target->name) + 1))
534                 return -EFAULT;
535
536         if (target->compat_to_user) {
537                 if (target->compat_to_user((void __user *)ct->data, t->data))
538                         return -EFAULT;
539         } else {
540                 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
541                         return -EFAULT;
542         }
543
544         *size -= off;
545         *dstptr += tsize;
546         return 0;
547 }
548 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
549 #endif
550
551 struct xt_table_info *xt_alloc_table_info(unsigned int size)
552 {
553         struct xt_table_info *newinfo;
554         int cpu;
555
556         /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
557         if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
558                 return NULL;
559
560         newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
561         if (!newinfo)
562                 return NULL;
563
564         newinfo->size = size;
565
566         for_each_possible_cpu(cpu) {
567                 if (size <= PAGE_SIZE)
568                         newinfo->entries[cpu] = kmalloc_node(size,
569                                                         GFP_KERNEL,
570                                                         cpu_to_node(cpu));
571                 else
572                         newinfo->entries[cpu] = vmalloc_node(size,
573                                                         cpu_to_node(cpu));
574
575                 if (newinfo->entries[cpu] == NULL) {
576                         xt_free_table_info(newinfo);
577                         return NULL;
578                 }
579         }
580
581         return newinfo;
582 }
583 EXPORT_SYMBOL(xt_alloc_table_info);
584
585 void xt_free_table_info(struct xt_table_info *info)
586 {
587         int cpu;
588
589         for_each_possible_cpu(cpu) {
590                 if (info->size <= PAGE_SIZE)
591                         kfree(info->entries[cpu]);
592                 else
593                         vfree(info->entries[cpu]);
594         }
595         kfree(info);
596 }
597 EXPORT_SYMBOL(xt_free_table_info);
598
599 /* Find table by name, grabs mutex & ref.  Returns ERR_PTR() on error. */
600 struct xt_table *xt_find_table_lock(int af, const char *name)
601 {
602         struct xt_table *t;
603
604         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
605                 return ERR_PTR(-EINTR);
606
607         list_for_each_entry(t, &xt[af].tables, list)
608                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
609                         return t;
610         mutex_unlock(&xt[af].mutex);
611         return NULL;
612 }
613 EXPORT_SYMBOL_GPL(xt_find_table_lock);
614
615 void xt_table_unlock(struct xt_table *table)
616 {
617         mutex_unlock(&xt[table->af].mutex);
618 }
619 EXPORT_SYMBOL_GPL(xt_table_unlock);
620
621 #ifdef CONFIG_COMPAT
622 void xt_compat_lock(int af)
623 {
624         mutex_lock(&xt[af].compat_mutex);
625 }
626 EXPORT_SYMBOL_GPL(xt_compat_lock);
627
628 void xt_compat_unlock(int af)
629 {
630         mutex_unlock(&xt[af].compat_mutex);
631 }
632 EXPORT_SYMBOL_GPL(xt_compat_unlock);
633 #endif
634
635 struct xt_table_info *
636 xt_replace_table(struct xt_table *table,
637               unsigned int num_counters,
638               struct xt_table_info *newinfo,
639               int *error)
640 {
641         struct xt_table_info *oldinfo, *private;
642
643         /* Do the substitution. */
644         write_lock_bh(&table->lock);
645         private = table->private;
646         /* Check inside lock: is the old number correct? */
647         if (num_counters != private->number) {
648                 duprintf("num_counters != table->private->number (%u/%u)\n",
649                          num_counters, private->number);
650                 write_unlock_bh(&table->lock);
651                 *error = -EAGAIN;
652                 return NULL;
653         }
654         oldinfo = private;
655         table->private = newinfo;
656         newinfo->initial_entries = oldinfo->initial_entries;
657         write_unlock_bh(&table->lock);
658
659         return oldinfo;
660 }
661 EXPORT_SYMBOL_GPL(xt_replace_table);
662
663 int xt_register_table(struct xt_table *table,
664                       struct xt_table_info *bootstrap,
665                       struct xt_table_info *newinfo)
666 {
667         int ret;
668         struct xt_table_info *private;
669         struct xt_table *t;
670
671         ret = mutex_lock_interruptible(&xt[table->af].mutex);
672         if (ret != 0)
673                 return ret;
674
675         /* Don't autoload: we'd eat our tail... */
676         list_for_each_entry(t, &xt[table->af].tables, list) {
677                 if (strcmp(t->name, table->name) == 0) {
678                         ret = -EEXIST;
679                         goto unlock;
680                 }
681         }
682
683         /* Simplifies replace_table code. */
684         table->private = bootstrap;
685         rwlock_init(&table->lock);
686         if (!xt_replace_table(table, 0, newinfo, &ret))
687                 goto unlock;
688
689         private = table->private;
690         duprintf("table->private->number = %u\n", private->number);
691
692         /* save number of initial entries */
693         private->initial_entries = private->number;
694
695         list_add(&table->list, &xt[table->af].tables);
696
697         ret = 0;
698  unlock:
699         mutex_unlock(&xt[table->af].mutex);
700         return ret;
701 }
702 EXPORT_SYMBOL_GPL(xt_register_table);
703
704 void *xt_unregister_table(struct xt_table *table)
705 {
706         struct xt_table_info *private;
707
708         mutex_lock(&xt[table->af].mutex);
709         private = table->private;
710         list_del(&table->list);
711         mutex_unlock(&xt[table->af].mutex);
712
713         return private;
714 }
715 EXPORT_SYMBOL_GPL(xt_unregister_table);
716
717 #ifdef CONFIG_PROC_FS
718 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
719 {
720         struct list_head *head = list->next;
721
722         if (!head || list_empty(list))
723                 return NULL;
724
725         while (pos && (head = head->next)) {
726                 if (head == list)
727                         return NULL;
728                 pos--;
729         }
730         return pos ? NULL : head;
731 }
732
733 static struct list_head *type2list(u_int16_t af, u_int16_t type)
734 {
735         struct list_head *list;
736
737         switch (type) {
738         case TARGET:
739                 list = &xt[af].target;
740                 break;
741         case MATCH:
742                 list = &xt[af].match;
743                 break;
744         case TABLE:
745                 list = &xt[af].tables;
746                 break;
747         default:
748                 list = NULL;
749                 break;
750         }
751
752         return list;
753 }
754
755 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
756 {
757         struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
758         u_int16_t af = (unsigned long)pde->data & 0xffff;
759         u_int16_t type = (unsigned long)pde->data >> 16;
760         struct list_head *list;
761
762         if (af >= NPROTO)
763                 return NULL;
764
765         list = type2list(af, type);
766         if (!list)
767                 return NULL;
768
769         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
770                 return NULL;
771
772         return xt_get_idx(list, seq, *pos);
773 }
774
775 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
776 {
777         struct proc_dir_entry *pde = seq->private;
778         u_int16_t af = (unsigned long)pde->data & 0xffff;
779         u_int16_t type = (unsigned long)pde->data >> 16;
780         struct list_head *list;
781
782         if (af >= NPROTO)
783                 return NULL;
784
785         list = type2list(af, type);
786         if (!list)
787                 return NULL;
788
789         (*pos)++;
790         return xt_get_idx(list, seq, *pos);
791 }
792
793 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
794 {
795         struct proc_dir_entry *pde = seq->private;
796         u_int16_t af = (unsigned long)pde->data & 0xffff;
797
798         mutex_unlock(&xt[af].mutex);
799 }
800
801 static int xt_name_seq_show(struct seq_file *seq, void *v)
802 {
803         char *name = (char *)v + sizeof(struct list_head);
804
805         if (strlen(name))
806                 return seq_printf(seq, "%s\n", name);
807         else
808                 return 0;
809 }
810
811 static const struct seq_operations xt_tgt_seq_ops = {
812         .start  = xt_tgt_seq_start,
813         .next   = xt_tgt_seq_next,
814         .stop   = xt_tgt_seq_stop,
815         .show   = xt_name_seq_show,
816 };
817
818 static int xt_tgt_open(struct inode *inode, struct file *file)
819 {
820         int ret;
821
822         ret = seq_open(file, &xt_tgt_seq_ops);
823         if (!ret) {
824                 struct seq_file *seq = file->private_data;
825                 struct proc_dir_entry *pde = PDE(inode);
826
827                 seq->private = pde;
828         }
829
830         return ret;
831 }
832
833 static const struct file_operations xt_file_ops = {
834         .owner   = THIS_MODULE,
835         .open    = xt_tgt_open,
836         .read    = seq_read,
837         .llseek  = seq_lseek,
838         .release = seq_release,
839 };
840
841 #define FORMAT_TABLES   "_tables_names"
842 #define FORMAT_MATCHES  "_tables_matches"
843 #define FORMAT_TARGETS  "_tables_targets"
844
845 #endif /* CONFIG_PROC_FS */
846
847 int xt_proto_init(int af)
848 {
849 #ifdef CONFIG_PROC_FS
850         char buf[XT_FUNCTION_MAXNAMELEN];
851         struct proc_dir_entry *proc;
852 #endif
853
854         if (af >= NPROTO)
855                 return -EINVAL;
856
857
858 #ifdef CONFIG_PROC_FS
859         strlcpy(buf, xt_prefix[af], sizeof(buf));
860         strlcat(buf, FORMAT_TABLES, sizeof(buf));
861         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
862         if (!proc)
863                 goto out;
864         proc->data = (void *) ((unsigned long) af | (TABLE << 16));
865
866
867         strlcpy(buf, xt_prefix[af], sizeof(buf));
868         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
869         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
870         if (!proc)
871                 goto out_remove_tables;
872         proc->data = (void *) ((unsigned long) af | (MATCH << 16));
873
874         strlcpy(buf, xt_prefix[af], sizeof(buf));
875         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
876         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
877         if (!proc)
878                 goto out_remove_matches;
879         proc->data = (void *) ((unsigned long) af | (TARGET << 16));
880 #endif
881
882         return 0;
883
884 #ifdef CONFIG_PROC_FS
885 out_remove_matches:
886         strlcpy(buf, xt_prefix[af], sizeof(buf));
887         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
888         proc_net_remove(&init_net, buf);
889
890 out_remove_tables:
891         strlcpy(buf, xt_prefix[af], sizeof(buf));
892         strlcat(buf, FORMAT_TABLES, sizeof(buf));
893         proc_net_remove(&init_net, buf);
894 out:
895         return -1;
896 #endif
897 }
898 EXPORT_SYMBOL_GPL(xt_proto_init);
899
900 void xt_proto_fini(int af)
901 {
902 #ifdef CONFIG_PROC_FS
903         char buf[XT_FUNCTION_MAXNAMELEN];
904
905         strlcpy(buf, xt_prefix[af], sizeof(buf));
906         strlcat(buf, FORMAT_TABLES, sizeof(buf));
907         proc_net_remove(&init_net, buf);
908
909         strlcpy(buf, xt_prefix[af], sizeof(buf));
910         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
911         proc_net_remove(&init_net, buf);
912
913         strlcpy(buf, xt_prefix[af], sizeof(buf));
914         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
915         proc_net_remove(&init_net, buf);
916 #endif /*CONFIG_PROC_FS*/
917 }
918 EXPORT_SYMBOL_GPL(xt_proto_fini);
919
920
921 static int __init xt_init(void)
922 {
923         int i;
924
925         xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
926         if (!xt)
927                 return -ENOMEM;
928
929         for (i = 0; i < NPROTO; i++) {
930                 mutex_init(&xt[i].mutex);
931 #ifdef CONFIG_COMPAT
932                 mutex_init(&xt[i].compat_mutex);
933                 xt[i].compat_offsets = NULL;
934 #endif
935                 INIT_LIST_HEAD(&xt[i].target);
936                 INIT_LIST_HEAD(&xt[i].match);
937                 INIT_LIST_HEAD(&xt[i].tables);
938         }
939         return 0;
940 }
941
942 static void __exit xt_fini(void)
943 {
944         kfree(xt);
945 }
946
947 module_init(xt_init);
948 module_exit(xt_fini);
949