[NETFILTER]: x_tables: change xt_table_register() return value convention
[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 struct xt_table *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                 goto out;
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         mutex_unlock(&xt[table->af].mutex);
697         return table;
698
699  unlock:
700         mutex_unlock(&xt[table->af].mutex);
701 out:
702         return ERR_PTR(ret);
703 }
704 EXPORT_SYMBOL_GPL(xt_register_table);
705
706 void *xt_unregister_table(struct xt_table *table)
707 {
708         struct xt_table_info *private;
709
710         mutex_lock(&xt[table->af].mutex);
711         private = table->private;
712         list_del(&table->list);
713         mutex_unlock(&xt[table->af].mutex);
714
715         return private;
716 }
717 EXPORT_SYMBOL_GPL(xt_unregister_table);
718
719 #ifdef CONFIG_PROC_FS
720 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
721 {
722         struct list_head *head = list->next;
723
724         if (!head || list_empty(list))
725                 return NULL;
726
727         while (pos && (head = head->next)) {
728                 if (head == list)
729                         return NULL;
730                 pos--;
731         }
732         return pos ? NULL : head;
733 }
734
735 static struct list_head *type2list(u_int16_t af, u_int16_t type)
736 {
737         struct list_head *list;
738
739         switch (type) {
740         case TARGET:
741                 list = &xt[af].target;
742                 break;
743         case MATCH:
744                 list = &xt[af].match;
745                 break;
746         case TABLE:
747                 list = &xt[af].tables;
748                 break;
749         default:
750                 list = NULL;
751                 break;
752         }
753
754         return list;
755 }
756
757 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
758 {
759         struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
760         u_int16_t af = (unsigned long)pde->data & 0xffff;
761         u_int16_t type = (unsigned long)pde->data >> 16;
762         struct list_head *list;
763
764         if (af >= NPROTO)
765                 return NULL;
766
767         list = type2list(af, type);
768         if (!list)
769                 return NULL;
770
771         if (mutex_lock_interruptible(&xt[af].mutex) != 0)
772                 return NULL;
773
774         return xt_get_idx(list, seq, *pos);
775 }
776
777 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
778 {
779         struct proc_dir_entry *pde = seq->private;
780         u_int16_t af = (unsigned long)pde->data & 0xffff;
781         u_int16_t type = (unsigned long)pde->data >> 16;
782         struct list_head *list;
783
784         if (af >= NPROTO)
785                 return NULL;
786
787         list = type2list(af, type);
788         if (!list)
789                 return NULL;
790
791         (*pos)++;
792         return xt_get_idx(list, seq, *pos);
793 }
794
795 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
796 {
797         struct proc_dir_entry *pde = seq->private;
798         u_int16_t af = (unsigned long)pde->data & 0xffff;
799
800         mutex_unlock(&xt[af].mutex);
801 }
802
803 static int xt_name_seq_show(struct seq_file *seq, void *v)
804 {
805         char *name = (char *)v + sizeof(struct list_head);
806
807         if (strlen(name))
808                 return seq_printf(seq, "%s\n", name);
809         else
810                 return 0;
811 }
812
813 static const struct seq_operations xt_tgt_seq_ops = {
814         .start  = xt_tgt_seq_start,
815         .next   = xt_tgt_seq_next,
816         .stop   = xt_tgt_seq_stop,
817         .show   = xt_name_seq_show,
818 };
819
820 static int xt_tgt_open(struct inode *inode, struct file *file)
821 {
822         int ret;
823
824         ret = seq_open(file, &xt_tgt_seq_ops);
825         if (!ret) {
826                 struct seq_file *seq = file->private_data;
827                 struct proc_dir_entry *pde = PDE(inode);
828
829                 seq->private = pde;
830         }
831
832         return ret;
833 }
834
835 static const struct file_operations xt_file_ops = {
836         .owner   = THIS_MODULE,
837         .open    = xt_tgt_open,
838         .read    = seq_read,
839         .llseek  = seq_lseek,
840         .release = seq_release,
841 };
842
843 #define FORMAT_TABLES   "_tables_names"
844 #define FORMAT_MATCHES  "_tables_matches"
845 #define FORMAT_TARGETS  "_tables_targets"
846
847 #endif /* CONFIG_PROC_FS */
848
849 int xt_proto_init(int af)
850 {
851 #ifdef CONFIG_PROC_FS
852         char buf[XT_FUNCTION_MAXNAMELEN];
853         struct proc_dir_entry *proc;
854 #endif
855
856         if (af >= NPROTO)
857                 return -EINVAL;
858
859
860 #ifdef CONFIG_PROC_FS
861         strlcpy(buf, xt_prefix[af], sizeof(buf));
862         strlcat(buf, FORMAT_TABLES, sizeof(buf));
863         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
864         if (!proc)
865                 goto out;
866         proc->data = (void *) ((unsigned long) af | (TABLE << 16));
867
868
869         strlcpy(buf, xt_prefix[af], sizeof(buf));
870         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
871         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
872         if (!proc)
873                 goto out_remove_tables;
874         proc->data = (void *) ((unsigned long) af | (MATCH << 16));
875
876         strlcpy(buf, xt_prefix[af], sizeof(buf));
877         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
878         proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
879         if (!proc)
880                 goto out_remove_matches;
881         proc->data = (void *) ((unsigned long) af | (TARGET << 16));
882 #endif
883
884         return 0;
885
886 #ifdef CONFIG_PROC_FS
887 out_remove_matches:
888         strlcpy(buf, xt_prefix[af], sizeof(buf));
889         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
890         proc_net_remove(&init_net, buf);
891
892 out_remove_tables:
893         strlcpy(buf, xt_prefix[af], sizeof(buf));
894         strlcat(buf, FORMAT_TABLES, sizeof(buf));
895         proc_net_remove(&init_net, buf);
896 out:
897         return -1;
898 #endif
899 }
900 EXPORT_SYMBOL_GPL(xt_proto_init);
901
902 void xt_proto_fini(int af)
903 {
904 #ifdef CONFIG_PROC_FS
905         char buf[XT_FUNCTION_MAXNAMELEN];
906
907         strlcpy(buf, xt_prefix[af], sizeof(buf));
908         strlcat(buf, FORMAT_TABLES, sizeof(buf));
909         proc_net_remove(&init_net, buf);
910
911         strlcpy(buf, xt_prefix[af], sizeof(buf));
912         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
913         proc_net_remove(&init_net, buf);
914
915         strlcpy(buf, xt_prefix[af], sizeof(buf));
916         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
917         proc_net_remove(&init_net, buf);
918 #endif /*CONFIG_PROC_FS*/
919 }
920 EXPORT_SYMBOL_GPL(xt_proto_fini);
921
922
923 static int __init xt_init(void)
924 {
925         int i;
926
927         xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
928         if (!xt)
929                 return -ENOMEM;
930
931         for (i = 0; i < NPROTO; i++) {
932                 mutex_init(&xt[i].mutex);
933 #ifdef CONFIG_COMPAT
934                 mutex_init(&xt[i].compat_mutex);
935                 xt[i].compat_offsets = NULL;
936 #endif
937                 INIT_LIST_HEAD(&xt[i].target);
938                 INIT_LIST_HEAD(&xt[i].match);
939                 INIT_LIST_HEAD(&xt[i].tables);
940         }
941         return 0;
942 }
943
944 static void __exit xt_fini(void)
945 {
946         kfree(xt);
947 }
948
949 module_init(xt_init);
950 module_exit(xt_fini);
951