[NETFILTER]: x_tables: set the protocol family in x_tables targets/matches
[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/config.h>
17 #include <linux/kernel.h>
18 #include <linux/socket.h>
19 #include <linux/net.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/string.h>
23 #include <linux/vmalloc.h>
24
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter_arp.h>
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
30 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
31
32 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
33
34 struct xt_af {
35         struct semaphore mutex;
36         struct list_head match;
37         struct list_head target;
38         struct list_head tables;
39 };
40
41 static struct xt_af *xt;
42
43 #ifdef DEBUG_IP_FIREWALL_USER
44 #define duprintf(format, args...) printk(format , ## args)
45 #else
46 #define duprintf(format, args...)
47 #endif
48
49 enum {
50         TABLE,
51         TARGET,
52         MATCH,
53 };
54
55 static const char *xt_prefix[NPROTO] = {
56         [AF_INET]       = "ip",
57         [AF_INET6]      = "ip6",
58         [NF_ARP]        = "arp",
59 };
60
61 /* Registration hooks for targets. */
62 int
63 xt_register_target(struct xt_target *target)
64 {
65         int ret, af = target->family;
66
67         ret = down_interruptible(&xt[af].mutex);
68         if (ret != 0)
69                 return ret;
70         list_add(&target->list, &xt[af].target);
71         up(&xt[af].mutex);
72         return ret;
73 }
74 EXPORT_SYMBOL(xt_register_target);
75
76 void
77 xt_unregister_target(struct xt_target *target)
78 {
79         int af = target->family;
80
81         down(&xt[af].mutex);
82         LIST_DELETE(&xt[af].target, target);
83         up(&xt[af].mutex);
84 }
85 EXPORT_SYMBOL(xt_unregister_target);
86
87 int
88 xt_register_match(struct xt_match *match)
89 {
90         int ret, af = match->family;
91
92         ret = down_interruptible(&xt[af].mutex);
93         if (ret != 0)
94                 return ret;
95
96         list_add(&match->list, &xt[af].match);
97         up(&xt[af].mutex);
98
99         return ret;
100 }
101 EXPORT_SYMBOL(xt_register_match);
102
103 void
104 xt_unregister_match(struct xt_match *match)
105 {
106         int af =  match->family;
107
108         down(&xt[af].mutex);
109         LIST_DELETE(&xt[af].match, match);
110         up(&xt[af].mutex);
111 }
112 EXPORT_SYMBOL(xt_unregister_match);
113
114
115 /*
116  * These are weird, but module loading must not be done with mutex
117  * held (since they will register), and we have to have a single
118  * function to use try_then_request_module().
119  */
120
121 /* Find match, grabs ref.  Returns ERR_PTR() on error. */
122 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
123 {
124         struct xt_match *m;
125         int err = 0;
126
127         if (down_interruptible(&xt[af].mutex) != 0)
128                 return ERR_PTR(-EINTR);
129
130         list_for_each_entry(m, &xt[af].match, list) {
131                 if (strcmp(m->name, name) == 0) {
132                         if (m->revision == revision) {
133                                 if (try_module_get(m->me)) {
134                                         up(&xt[af].mutex);
135                                         return m;
136                                 }
137                         } else
138                                 err = -EPROTOTYPE; /* Found something. */
139                 }
140         }
141         up(&xt[af].mutex);
142         return ERR_PTR(err);
143 }
144 EXPORT_SYMBOL(xt_find_match);
145
146 /* Find target, grabs ref.  Returns ERR_PTR() on error. */
147 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
148 {
149         struct xt_target *t;
150         int err = 0;
151
152         if (down_interruptible(&xt[af].mutex) != 0)
153                 return ERR_PTR(-EINTR);
154
155         list_for_each_entry(t, &xt[af].target, list) {
156                 if (strcmp(t->name, name) == 0) {
157                         if (t->revision == revision) {
158                                 if (try_module_get(t->me)) {
159                                         up(&xt[af].mutex);
160                                         return t;
161                                 }
162                         } else
163                                 err = -EPROTOTYPE; /* Found something. */
164                 }
165         }
166         up(&xt[af].mutex);
167         return ERR_PTR(err);
168 }
169 EXPORT_SYMBOL(xt_find_target);
170
171 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
172 {
173         struct xt_target *target;
174
175         target = try_then_request_module(xt_find_target(af, name, revision),
176                                          "%st_%s", xt_prefix[af], name);
177         if (IS_ERR(target) || !target)
178                 return NULL;
179         return target;
180 }
181 EXPORT_SYMBOL_GPL(xt_request_find_target);
182
183 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
184 {
185         struct xt_match *m;
186         int have_rev = 0;
187
188         list_for_each_entry(m, &xt[af].match, list) {
189                 if (strcmp(m->name, name) == 0) {
190                         if (m->revision > *bestp)
191                                 *bestp = m->revision;
192                         if (m->revision == revision)
193                                 have_rev = 1;
194                 }
195         }
196         return have_rev;
197 }
198
199 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
200 {
201         struct xt_target *t;
202         int have_rev = 0;
203
204         list_for_each_entry(t, &xt[af].target, list) {
205                 if (strcmp(t->name, name) == 0) {
206                         if (t->revision > *bestp)
207                                 *bestp = t->revision;
208                         if (t->revision == revision)
209                                 have_rev = 1;
210                 }
211         }
212         return have_rev;
213 }
214
215 /* Returns true or false (if no such extension at all) */
216 int xt_find_revision(int af, const char *name, u8 revision, int target,
217                      int *err)
218 {
219         int have_rev, best = -1;
220
221         if (down_interruptible(&xt[af].mutex) != 0) {
222                 *err = -EINTR;
223                 return 1;
224         }
225         if (target == 1)
226                 have_rev = target_revfn(af, name, revision, &best);
227         else
228                 have_rev = match_revfn(af, name, revision, &best);
229         up(&xt[af].mutex);
230
231         /* Nothing at all?  Return 0 to try loading module. */
232         if (best == -1) {
233                 *err = -ENOENT;
234                 return 0;
235         }
236
237         *err = best;
238         if (!have_rev)
239                 *err = -EPROTONOSUPPORT;
240         return 1;
241 }
242 EXPORT_SYMBOL_GPL(xt_find_revision);
243
244 int xt_check_match(const struct xt_match *match, unsigned short family,
245                    unsigned int size, const char *table, unsigned int hook_mask,
246                    unsigned short proto, int inv_proto)
247 {
248         if (XT_ALIGN(match->matchsize) != size) {
249                 printk("%s_tables: %s match: invalid size %Zu != %u\n",
250                        xt_prefix[family], match->name,
251                        XT_ALIGN(match->matchsize), size);
252                 return -EINVAL;
253         }
254         if (match->table && strcmp(match->table, table)) {
255                 printk("%s_tables: %s match: only valid in %s table, not %s\n",
256                        xt_prefix[family], match->name, match->table, table);
257                 return -EINVAL;
258         }
259         if (match->hooks && (hook_mask & ~match->hooks) != 0) {
260                 printk("%s_tables: %s match: bad hook_mask %u\n",
261                        xt_prefix[family], match->name, hook_mask);
262                 return -EINVAL;
263         }
264         if (match->proto && (match->proto != proto || inv_proto)) {
265                 printk("%s_tables: %s match: only valid for protocol %u\n",
266                        xt_prefix[family], match->name, match->proto);
267                 return -EINVAL;
268         }
269         return 0;
270 }
271 EXPORT_SYMBOL_GPL(xt_check_match);
272
273 int xt_check_target(const struct xt_target *target, unsigned short family,
274                     unsigned int size, const char *table, unsigned int hook_mask,
275                     unsigned short proto, int inv_proto)
276 {
277         if (XT_ALIGN(target->targetsize) != size) {
278                 printk("%s_tables: %s target: invalid size %Zu != %u\n",
279                        xt_prefix[family], target->name,
280                        XT_ALIGN(target->targetsize), size);
281                 return -EINVAL;
282         }
283         if (target->table && strcmp(target->table, table)) {
284                 printk("%s_tables: %s target: only valid in %s table, not %s\n",
285                        xt_prefix[family], target->name, target->table, table);
286                 return -EINVAL;
287         }
288         if (target->hooks && (hook_mask & ~target->hooks) != 0) {
289                 printk("%s_tables: %s target: bad hook_mask %u\n",
290                        xt_prefix[family], target->name, hook_mask);
291                 return -EINVAL;
292         }
293         if (target->proto && (target->proto != proto || inv_proto)) {
294                 printk("%s_tables: %s target: only valid for protocol %u\n",
295                        xt_prefix[family], target->name, target->proto);
296                 return -EINVAL;
297         }
298         return 0;
299 }
300 EXPORT_SYMBOL_GPL(xt_check_target);
301
302 struct xt_table_info *xt_alloc_table_info(unsigned int size)
303 {
304         struct xt_table_info *newinfo;
305         int cpu;
306
307         /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
308         if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
309                 return NULL;
310
311         newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL);
312         if (!newinfo)
313                 return NULL;
314
315         newinfo->size = size;
316
317         for_each_cpu(cpu) {
318                 if (size <= PAGE_SIZE)
319                         newinfo->entries[cpu] = kmalloc_node(size,
320                                                         GFP_KERNEL,
321                                                         cpu_to_node(cpu));
322                 else
323                         newinfo->entries[cpu] = vmalloc_node(size,
324                                                         cpu_to_node(cpu));
325
326                 if (newinfo->entries[cpu] == NULL) {
327                         xt_free_table_info(newinfo);
328                         return NULL;
329                 }
330         }
331
332         return newinfo;
333 }
334 EXPORT_SYMBOL(xt_alloc_table_info);
335
336 void xt_free_table_info(struct xt_table_info *info)
337 {
338         int cpu;
339
340         for_each_cpu(cpu) {
341                 if (info->size <= PAGE_SIZE)
342                         kfree(info->entries[cpu]);
343                 else
344                         vfree(info->entries[cpu]);
345         }
346         kfree(info);
347 }
348 EXPORT_SYMBOL(xt_free_table_info);
349
350 /* Find table by name, grabs mutex & ref.  Returns ERR_PTR() on error. */
351 struct xt_table *xt_find_table_lock(int af, const char *name)
352 {
353         struct xt_table *t;
354
355         if (down_interruptible(&xt[af].mutex) != 0)
356                 return ERR_PTR(-EINTR);
357
358         list_for_each_entry(t, &xt[af].tables, list)
359                 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
360                         return t;
361         up(&xt[af].mutex);
362         return NULL;
363 }
364 EXPORT_SYMBOL_GPL(xt_find_table_lock);
365
366 void xt_table_unlock(struct xt_table *table)
367 {
368         up(&xt[table->af].mutex);
369 }
370 EXPORT_SYMBOL_GPL(xt_table_unlock);
371
372
373 struct xt_table_info *
374 xt_replace_table(struct xt_table *table,
375               unsigned int num_counters,
376               struct xt_table_info *newinfo,
377               int *error)
378 {
379         struct xt_table_info *oldinfo, *private;
380
381         /* Do the substitution. */
382         write_lock_bh(&table->lock);
383         private = table->private;
384         /* Check inside lock: is the old number correct? */
385         if (num_counters != private->number) {
386                 duprintf("num_counters != table->private->number (%u/%u)\n",
387                          num_counters, private->number);
388                 write_unlock_bh(&table->lock);
389                 *error = -EAGAIN;
390                 return NULL;
391         }
392         oldinfo = private;
393         table->private = newinfo;
394         newinfo->initial_entries = oldinfo->initial_entries;
395         write_unlock_bh(&table->lock);
396
397         return oldinfo;
398 }
399 EXPORT_SYMBOL_GPL(xt_replace_table);
400
401 int xt_register_table(struct xt_table *table,
402                       struct xt_table_info *bootstrap,
403                       struct xt_table_info *newinfo)
404 {
405         int ret;
406         struct xt_table_info *private;
407
408         ret = down_interruptible(&xt[table->af].mutex);
409         if (ret != 0)
410                 return ret;
411
412         /* Don't autoload: we'd eat our tail... */
413         if (list_named_find(&xt[table->af].tables, table->name)) {
414                 ret = -EEXIST;
415                 goto unlock;
416         }
417
418         /* Simplifies replace_table code. */
419         table->private = bootstrap;
420         if (!xt_replace_table(table, 0, newinfo, &ret))
421                 goto unlock;
422
423         private = table->private;
424         duprintf("table->private->number = %u\n", private->number);
425
426         /* save number of initial entries */
427         private->initial_entries = private->number;
428
429         rwlock_init(&table->lock);
430         list_prepend(&xt[table->af].tables, table);
431
432         ret = 0;
433  unlock:
434         up(&xt[table->af].mutex);
435         return ret;
436 }
437 EXPORT_SYMBOL_GPL(xt_register_table);
438
439 void *xt_unregister_table(struct xt_table *table)
440 {
441         struct xt_table_info *private;
442
443         down(&xt[table->af].mutex);
444         private = table->private;
445         LIST_DELETE(&xt[table->af].tables, table);
446         up(&xt[table->af].mutex);
447
448         return private;
449 }
450 EXPORT_SYMBOL_GPL(xt_unregister_table);
451
452 #ifdef CONFIG_PROC_FS
453 static char *xt_proto_prefix[NPROTO] = {
454         [AF_INET]       = "ip",
455         [AF_INET6]      = "ip6",
456         [NF_ARP]        = "arp",
457 };
458
459 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
460 {
461         struct list_head *head = list->next;
462
463         if (!head || list_empty(list))
464                 return NULL;
465
466         while (pos && (head = head->next)) {
467                 if (head == list)
468                         return NULL;
469                 pos--;
470         }
471         return pos ? NULL : head;
472 }
473
474 static struct list_head *type2list(u_int16_t af, u_int16_t type)
475 {
476         struct list_head *list;
477
478         switch (type) {
479         case TARGET:
480                 list = &xt[af].target;
481                 break;
482         case MATCH:
483                 list = &xt[af].match;
484                 break;
485         case TABLE:
486                 list = &xt[af].tables;
487                 break;
488         default:
489                 list = NULL;
490                 break;
491         }
492
493         return list;
494 }
495
496 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
497 {
498         struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
499         u_int16_t af = (unsigned long)pde->data & 0xffff;
500         u_int16_t type = (unsigned long)pde->data >> 16;
501         struct list_head *list;
502
503         if (af >= NPROTO)
504                 return NULL;
505
506         list = type2list(af, type);
507         if (!list)
508                 return NULL;
509
510         if (down_interruptible(&xt[af].mutex) != 0)
511                 return NULL;
512         
513         return xt_get_idx(list, seq, *pos);
514 }
515
516 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
517 {
518         struct proc_dir_entry *pde = seq->private;
519         u_int16_t af = (unsigned long)pde->data & 0xffff;
520         u_int16_t type = (unsigned long)pde->data >> 16;
521         struct list_head *list;
522
523         if (af >= NPROTO)
524                 return NULL;
525         
526         list = type2list(af, type);
527         if (!list)
528                 return NULL;
529
530         (*pos)++;
531         return xt_get_idx(list, seq, *pos);
532 }
533
534 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
535 {
536         struct proc_dir_entry *pde = seq->private;
537         u_int16_t af = (unsigned long)pde->data & 0xffff;
538
539         up(&xt[af].mutex);
540 }
541
542 static int xt_name_seq_show(struct seq_file *seq, void *v)
543 {
544         char *name = (char *)v + sizeof(struct list_head);
545
546         if (strlen(name))
547                 return seq_printf(seq, "%s\n", name);
548         else
549                 return 0;
550 }
551
552 static struct seq_operations xt_tgt_seq_ops = {
553         .start  = xt_tgt_seq_start,
554         .next   = xt_tgt_seq_next,
555         .stop   = xt_tgt_seq_stop,
556         .show   = xt_name_seq_show,
557 };
558
559 static int xt_tgt_open(struct inode *inode, struct file *file)
560 {
561         int ret;
562
563         ret = seq_open(file, &xt_tgt_seq_ops);
564         if (!ret) {
565                 struct seq_file *seq = file->private_data;
566                 struct proc_dir_entry *pde = PDE(inode);
567
568                 seq->private = pde;
569         }
570
571         return ret;
572 }
573
574 static struct file_operations xt_file_ops = {
575         .owner   = THIS_MODULE,
576         .open    = xt_tgt_open,
577         .read    = seq_read,
578         .llseek  = seq_lseek,
579         .release = seq_release,
580 };
581
582 #define FORMAT_TABLES   "_tables_names"
583 #define FORMAT_MATCHES  "_tables_matches"
584 #define FORMAT_TARGETS  "_tables_targets"
585
586 #endif /* CONFIG_PROC_FS */
587
588 int xt_proto_init(int af)
589 {
590 #ifdef CONFIG_PROC_FS
591         char buf[XT_FUNCTION_MAXNAMELEN];
592         struct proc_dir_entry *proc;
593 #endif
594
595         if (af >= NPROTO)
596                 return -EINVAL;
597
598
599 #ifdef CONFIG_PROC_FS
600         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
601         strlcat(buf, FORMAT_TABLES, sizeof(buf));
602         proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
603         if (!proc)
604                 goto out;
605         proc->data = (void *) ((unsigned long) af | (TABLE << 16));
606
607
608         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
609         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
610         proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
611         if (!proc)
612                 goto out_remove_tables;
613         proc->data = (void *) ((unsigned long) af | (MATCH << 16));
614
615         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
616         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
617         proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
618         if (!proc)
619                 goto out_remove_matches;
620         proc->data = (void *) ((unsigned long) af | (TARGET << 16));
621 #endif
622
623         return 0;
624
625 #ifdef CONFIG_PROC_FS
626 out_remove_matches:
627         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
628         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
629         proc_net_remove(buf);
630
631 out_remove_tables:
632         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
633         strlcat(buf, FORMAT_TABLES, sizeof(buf));
634         proc_net_remove(buf);
635 out:
636         return -1;
637 #endif
638 }
639 EXPORT_SYMBOL_GPL(xt_proto_init);
640
641 void xt_proto_fini(int af)
642 {
643 #ifdef CONFIG_PROC_FS
644         char buf[XT_FUNCTION_MAXNAMELEN];
645
646         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
647         strlcat(buf, FORMAT_TABLES, sizeof(buf));
648         proc_net_remove(buf);
649
650         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
651         strlcat(buf, FORMAT_TARGETS, sizeof(buf));
652         proc_net_remove(buf);
653
654         strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
655         strlcat(buf, FORMAT_MATCHES, sizeof(buf));
656         proc_net_remove(buf);
657 #endif /*CONFIG_PROC_FS*/
658 }
659 EXPORT_SYMBOL_GPL(xt_proto_fini);
660
661
662 static int __init xt_init(void)
663 {
664         int i;
665
666         xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
667         if (!xt)
668                 return -ENOMEM;
669
670         for (i = 0; i < NPROTO; i++) {
671                 init_MUTEX(&xt[i].mutex);
672                 INIT_LIST_HEAD(&xt[i].target);
673                 INIT_LIST_HEAD(&xt[i].match);
674                 INIT_LIST_HEAD(&xt[i].tables);
675         }
676         return 0;
677 }
678
679 static void __exit xt_fini(void)
680 {
681         kfree(xt);
682 }
683
684 module_init(xt_init);
685 module_exit(xt_fini);
686