]>
Commit | Line | Data |
---|---|---|
2e4e6a17 HW |
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 | /* Registration hooks for targets. */ | |
56 | int | |
57 | xt_register_target(int af, struct xt_target *target) | |
58 | { | |
59 | int ret; | |
60 | ||
61 | ret = down_interruptible(&xt[af].mutex); | |
62 | if (ret != 0) | |
63 | return ret; | |
64 | list_add(&target->list, &xt[af].target); | |
65 | up(&xt[af].mutex); | |
66 | return ret; | |
67 | } | |
68 | EXPORT_SYMBOL(xt_register_target); | |
69 | ||
70 | void | |
71 | xt_unregister_target(int af, struct xt_target *target) | |
72 | { | |
73 | down(&xt[af].mutex); | |
74 | LIST_DELETE(&xt[af].target, target); | |
75 | up(&xt[af].mutex); | |
76 | } | |
77 | EXPORT_SYMBOL(xt_unregister_target); | |
78 | ||
79 | int | |
80 | xt_register_match(int af, struct xt_match *match) | |
81 | { | |
82 | int ret; | |
83 | ||
84 | ret = down_interruptible(&xt[af].mutex); | |
85 | if (ret != 0) | |
86 | return ret; | |
87 | ||
88 | list_add(&match->list, &xt[af].match); | |
89 | up(&xt[af].mutex); | |
90 | ||
91 | return ret; | |
92 | } | |
93 | EXPORT_SYMBOL(xt_register_match); | |
94 | ||
95 | void | |
96 | xt_unregister_match(int af, struct xt_match *match) | |
97 | { | |
98 | down(&xt[af].mutex); | |
99 | LIST_DELETE(&xt[af].match, match); | |
100 | up(&xt[af].mutex); | |
101 | } | |
102 | EXPORT_SYMBOL(xt_unregister_match); | |
103 | ||
104 | ||
105 | /* | |
106 | * These are weird, but module loading must not be done with mutex | |
107 | * held (since they will register), and we have to have a single | |
108 | * function to use try_then_request_module(). | |
109 | */ | |
110 | ||
111 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ | |
112 | struct xt_match *xt_find_match(int af, const char *name, u8 revision) | |
113 | { | |
114 | struct xt_match *m; | |
115 | int err = 0; | |
116 | ||
117 | if (down_interruptible(&xt[af].mutex) != 0) | |
118 | return ERR_PTR(-EINTR); | |
119 | ||
120 | list_for_each_entry(m, &xt[af].match, list) { | |
121 | if (strcmp(m->name, name) == 0) { | |
122 | if (m->revision == revision) { | |
123 | if (try_module_get(m->me)) { | |
124 | up(&xt[af].mutex); | |
125 | return m; | |
126 | } | |
127 | } else | |
128 | err = -EPROTOTYPE; /* Found something. */ | |
129 | } | |
130 | } | |
131 | up(&xt[af].mutex); | |
132 | return ERR_PTR(err); | |
133 | } | |
134 | EXPORT_SYMBOL(xt_find_match); | |
135 | ||
136 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ | |
137 | struct xt_target *xt_find_target(int af, const char *name, u8 revision) | |
138 | { | |
139 | struct xt_target *t; | |
140 | int err = 0; | |
141 | ||
142 | if (down_interruptible(&xt[af].mutex) != 0) | |
143 | return ERR_PTR(-EINTR); | |
144 | ||
145 | list_for_each_entry(t, &xt[af].target, list) { | |
146 | if (strcmp(t->name, name) == 0) { | |
147 | if (t->revision == revision) { | |
148 | if (try_module_get(t->me)) { | |
149 | up(&xt[af].mutex); | |
150 | return t; | |
151 | } | |
152 | } else | |
153 | err = -EPROTOTYPE; /* Found something. */ | |
154 | } | |
155 | } | |
156 | up(&xt[af].mutex); | |
157 | return ERR_PTR(err); | |
158 | } | |
159 | EXPORT_SYMBOL(xt_find_target); | |
160 | ||
161 | static const char *xt_prefix[NPROTO] = { | |
162 | [AF_INET] = "ipt_%s", | |
163 | [AF_INET6] = "ip6t_%s", | |
164 | [NF_ARP] = "arpt_%s", | |
165 | }; | |
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 | 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 | struct xt_table_info *xt_alloc_table_info(unsigned int size) | |
241 | { | |
242 | struct xt_table_info *newinfo; | |
243 | int cpu; | |
244 | ||
245 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ | |
246 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages) | |
247 | return NULL; | |
248 | ||
249 | newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL); | |
250 | if (!newinfo) | |
251 | return NULL; | |
252 | ||
253 | newinfo->size = size; | |
254 | ||
255 | for_each_cpu(cpu) { | |
256 | if (size <= PAGE_SIZE) | |
257 | newinfo->entries[cpu] = kmalloc_node(size, | |
258 | GFP_KERNEL, | |
259 | cpu_to_node(cpu)); | |
260 | else | |
261 | newinfo->entries[cpu] = vmalloc_node(size, | |
262 | cpu_to_node(cpu)); | |
263 | ||
264 | if (newinfo->entries[cpu] == NULL) { | |
265 | xt_free_table_info(newinfo); | |
266 | return NULL; | |
267 | } | |
268 | } | |
269 | ||
270 | return newinfo; | |
271 | } | |
272 | EXPORT_SYMBOL(xt_alloc_table_info); | |
273 | ||
274 | void xt_free_table_info(struct xt_table_info *info) | |
275 | { | |
276 | int cpu; | |
277 | ||
278 | for_each_cpu(cpu) { | |
279 | if (info->size <= PAGE_SIZE) | |
280 | kfree(info->entries[cpu]); | |
281 | else | |
282 | vfree(info->entries[cpu]); | |
283 | } | |
284 | kfree(info); | |
285 | } | |
286 | EXPORT_SYMBOL(xt_free_table_info); | |
287 | ||
288 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ | |
289 | struct xt_table *xt_find_table_lock(int af, const char *name) | |
290 | { | |
291 | struct xt_table *t; | |
292 | ||
293 | if (down_interruptible(&xt[af].mutex) != 0) | |
294 | return ERR_PTR(-EINTR); | |
295 | ||
296 | list_for_each_entry(t, &xt[af].tables, list) | |
297 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) | |
298 | return t; | |
299 | up(&xt[af].mutex); | |
300 | return NULL; | |
301 | } | |
302 | EXPORT_SYMBOL_GPL(xt_find_table_lock); | |
303 | ||
304 | void xt_table_unlock(struct xt_table *table) | |
305 | { | |
306 | up(&xt[table->af].mutex); | |
307 | } | |
308 | EXPORT_SYMBOL_GPL(xt_table_unlock); | |
309 | ||
310 | ||
311 | struct xt_table_info * | |
312 | xt_replace_table(struct xt_table *table, | |
313 | unsigned int num_counters, | |
314 | struct xt_table_info *newinfo, | |
315 | int *error) | |
316 | { | |
317 | struct xt_table_info *oldinfo, *private; | |
318 | ||
319 | /* Do the substitution. */ | |
320 | write_lock_bh(&table->lock); | |
321 | private = table->private; | |
322 | /* Check inside lock: is the old number correct? */ | |
323 | if (num_counters != private->number) { | |
324 | duprintf("num_counters != table->private->number (%u/%u)\n", | |
325 | num_counters, private->number); | |
326 | write_unlock_bh(&table->lock); | |
327 | *error = -EAGAIN; | |
328 | return NULL; | |
329 | } | |
330 | oldinfo = private; | |
331 | table->private = newinfo; | |
332 | newinfo->initial_entries = oldinfo->initial_entries; | |
333 | write_unlock_bh(&table->lock); | |
334 | ||
335 | return oldinfo; | |
336 | } | |
337 | EXPORT_SYMBOL_GPL(xt_replace_table); | |
338 | ||
339 | int xt_register_table(struct xt_table *table, | |
340 | struct xt_table_info *bootstrap, | |
341 | struct xt_table_info *newinfo) | |
342 | { | |
343 | int ret; | |
344 | struct xt_table_info *private; | |
345 | ||
346 | ret = down_interruptible(&xt[table->af].mutex); | |
347 | if (ret != 0) | |
348 | return ret; | |
349 | ||
350 | /* Don't autoload: we'd eat our tail... */ | |
351 | if (list_named_find(&xt[table->af].tables, table->name)) { | |
352 | ret = -EEXIST; | |
353 | goto unlock; | |
354 | } | |
355 | ||
356 | /* Simplifies replace_table code. */ | |
357 | table->private = bootstrap; | |
358 | if (!xt_replace_table(table, 0, newinfo, &ret)) | |
359 | goto unlock; | |
360 | ||
361 | private = table->private; | |
362 | duprintf("table->private->number = %u\n", private->number); | |
363 | ||
364 | /* save number of initial entries */ | |
365 | private->initial_entries = private->number; | |
366 | ||
367 | rwlock_init(&table->lock); | |
368 | list_prepend(&xt[table->af].tables, table); | |
369 | ||
370 | ret = 0; | |
371 | unlock: | |
372 | up(&xt[table->af].mutex); | |
373 | return ret; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(xt_register_table); | |
376 | ||
377 | void *xt_unregister_table(struct xt_table *table) | |
378 | { | |
379 | struct xt_table_info *private; | |
380 | ||
381 | down(&xt[table->af].mutex); | |
382 | private = table->private; | |
383 | LIST_DELETE(&xt[table->af].tables, table); | |
384 | up(&xt[table->af].mutex); | |
385 | ||
386 | return private; | |
387 | } | |
388 | EXPORT_SYMBOL_GPL(xt_unregister_table); | |
389 | ||
390 | #ifdef CONFIG_PROC_FS | |
391 | static char *xt_proto_prefix[NPROTO] = { | |
392 | [AF_INET] = "ip", | |
393 | [AF_INET6] = "ip6", | |
394 | [NF_ARP] = "arp", | |
395 | }; | |
396 | ||
397 | static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos) | |
398 | { | |
399 | struct list_head *head = list->next; | |
400 | ||
401 | if (!head || list_empty(list)) | |
402 | return NULL; | |
403 | ||
404 | while (pos && (head = head->next)) { | |
405 | if (head == list) | |
406 | return NULL; | |
407 | pos--; | |
408 | } | |
409 | return pos ? NULL : head; | |
410 | } | |
411 | ||
412 | static struct list_head *type2list(u_int16_t af, u_int16_t type) | |
413 | { | |
414 | struct list_head *list; | |
415 | ||
416 | switch (type) { | |
417 | case TARGET: | |
418 | list = &xt[af].target; | |
419 | break; | |
420 | case MATCH: | |
421 | list = &xt[af].match; | |
422 | break; | |
423 | case TABLE: | |
424 | list = &xt[af].tables; | |
425 | break; | |
426 | default: | |
427 | list = NULL; | |
428 | break; | |
429 | } | |
430 | ||
431 | return list; | |
432 | } | |
433 | ||
434 | static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos) | |
435 | { | |
436 | struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private; | |
437 | u_int16_t af = (unsigned long)pde->data & 0xffff; | |
438 | u_int16_t type = (unsigned long)pde->data >> 16; | |
439 | struct list_head *list; | |
440 | ||
441 | if (af >= NPROTO) | |
442 | return NULL; | |
443 | ||
444 | list = type2list(af, type); | |
445 | if (!list) | |
446 | return NULL; | |
447 | ||
448 | if (down_interruptible(&xt[af].mutex) != 0) | |
449 | return NULL; | |
450 | ||
451 | return xt_get_idx(list, seq, *pos); | |
452 | } | |
453 | ||
454 | static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
455 | { | |
456 | struct proc_dir_entry *pde = seq->private; | |
457 | u_int16_t af = (unsigned long)pde->data & 0xffff; | |
458 | u_int16_t type = (unsigned long)pde->data >> 16; | |
459 | struct list_head *list; | |
460 | ||
461 | if (af >= NPROTO) | |
462 | return NULL; | |
463 | ||
464 | list = type2list(af, type); | |
465 | if (!list) | |
466 | return NULL; | |
467 | ||
468 | (*pos)++; | |
469 | return xt_get_idx(list, seq, *pos); | |
470 | } | |
471 | ||
472 | static void xt_tgt_seq_stop(struct seq_file *seq, void *v) | |
473 | { | |
474 | struct proc_dir_entry *pde = seq->private; | |
475 | u_int16_t af = (unsigned long)pde->data & 0xffff; | |
476 | ||
477 | up(&xt[af].mutex); | |
478 | } | |
479 | ||
480 | static int xt_name_seq_show(struct seq_file *seq, void *v) | |
481 | { | |
482 | char *name = (char *)v + sizeof(struct list_head); | |
483 | ||
484 | if (strlen(name)) | |
485 | return seq_printf(seq, "%s\n", name); | |
486 | else | |
487 | return 0; | |
488 | } | |
489 | ||
490 | static struct seq_operations xt_tgt_seq_ops = { | |
491 | .start = xt_tgt_seq_start, | |
492 | .next = xt_tgt_seq_next, | |
493 | .stop = xt_tgt_seq_stop, | |
494 | .show = xt_name_seq_show, | |
495 | }; | |
496 | ||
497 | static int xt_tgt_open(struct inode *inode, struct file *file) | |
498 | { | |
499 | int ret; | |
500 | ||
501 | ret = seq_open(file, &xt_tgt_seq_ops); | |
502 | if (!ret) { | |
503 | struct seq_file *seq = file->private_data; | |
504 | struct proc_dir_entry *pde = PDE(inode); | |
505 | ||
506 | seq->private = pde; | |
507 | } | |
508 | ||
509 | return ret; | |
510 | } | |
511 | ||
512 | static struct file_operations xt_file_ops = { | |
513 | .owner = THIS_MODULE, | |
514 | .open = xt_tgt_open, | |
515 | .read = seq_read, | |
516 | .llseek = seq_lseek, | |
517 | .release = seq_release, | |
518 | }; | |
519 | ||
520 | #define FORMAT_TABLES "_tables_names" | |
521 | #define FORMAT_MATCHES "_tables_matches" | |
522 | #define FORMAT_TARGETS "_tables_targets" | |
523 | ||
524 | #endif /* CONFIG_PROC_FS */ | |
525 | ||
526 | int xt_proto_init(int af) | |
527 | { | |
528 | #ifdef CONFIG_PROC_FS | |
529 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
530 | struct proc_dir_entry *proc; | |
531 | #endif | |
532 | ||
533 | if (af >= NPROTO) | |
534 | return -EINVAL; | |
535 | ||
536 | ||
537 | #ifdef CONFIG_PROC_FS | |
538 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
539 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); | |
540 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); | |
541 | if (!proc) | |
542 | goto out; | |
543 | proc->data = (void *) ((unsigned long) af | (TABLE << 16)); | |
544 | ||
545 | ||
546 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
547 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); | |
548 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); | |
549 | if (!proc) | |
550 | goto out_remove_tables; | |
551 | proc->data = (void *) ((unsigned long) af | (MATCH << 16)); | |
552 | ||
553 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
554 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); | |
555 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); | |
556 | if (!proc) | |
557 | goto out_remove_matches; | |
558 | proc->data = (void *) ((unsigned long) af | (TARGET << 16)); | |
559 | #endif | |
560 | ||
561 | return 0; | |
562 | ||
563 | #ifdef CONFIG_PROC_FS | |
564 | out_remove_matches: | |
565 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
566 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); | |
567 | proc_net_remove(buf); | |
568 | ||
569 | out_remove_tables: | |
570 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
571 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); | |
572 | proc_net_remove(buf); | |
573 | out: | |
574 | return -1; | |
575 | #endif | |
576 | } | |
577 | EXPORT_SYMBOL_GPL(xt_proto_init); | |
578 | ||
579 | void xt_proto_fini(int af) | |
580 | { | |
581 | #ifdef CONFIG_PROC_FS | |
582 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
583 | ||
584 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
585 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); | |
586 | proc_net_remove(buf); | |
587 | ||
588 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
589 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); | |
590 | proc_net_remove(buf); | |
591 | ||
592 | strlcpy(buf, xt_proto_prefix[af], sizeof(buf)); | |
593 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); | |
594 | proc_net_remove(buf); | |
595 | #endif /*CONFIG_PROC_FS*/ | |
596 | } | |
597 | EXPORT_SYMBOL_GPL(xt_proto_fini); | |
598 | ||
599 | ||
600 | static int __init xt_init(void) | |
601 | { | |
602 | int i; | |
603 | ||
604 | xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL); | |
605 | if (!xt) | |
606 | return -ENOMEM; | |
607 | ||
608 | for (i = 0; i < NPROTO; i++) { | |
609 | init_MUTEX(&xt[i].mutex); | |
610 | INIT_LIST_HEAD(&xt[i].target); | |
611 | INIT_LIST_HEAD(&xt[i].match); | |
612 | INIT_LIST_HEAD(&xt[i].tables); | |
613 | } | |
614 | return 0; | |
615 | } | |
616 | ||
617 | static void __exit xt_fini(void) | |
618 | { | |
619 | kfree(xt); | |
620 | } | |
621 | ||
622 | module_init(xt_init); | |
623 | module_exit(xt_fini); | |
624 |