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