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