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