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