]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/x_tables.c
netfilter: change Ebtables function signatures to match Xtables's
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / x_tables.c
CommitLineData
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
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
33MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
34
35#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
36
b386d9f5
PM
37struct compat_delta {
38 struct compat_delta *next;
39 unsigned int offset;
40 short delta;
41};
42
2e4e6a17 43struct xt_af {
9e19bb6d 44 struct mutex mutex;
2e4e6a17
HW
45 struct list_head match;
46 struct list_head target;
b386d9f5 47#ifdef CONFIG_COMPAT
2722971c 48 struct mutex compat_mutex;
b386d9f5
PM
49 struct compat_delta *compat_offsets;
50#endif
2e4e6a17
HW
51};
52
53static struct xt_af *xt;
54
55#ifdef DEBUG_IP_FIREWALL_USER
56#define duprintf(format, args...) printk(format , ## args)
57#else
58#define duprintf(format, args...)
59#endif
60
7e9c6eeb
JE
61static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
62 [NFPROTO_UNSPEC] = "x",
63 [NFPROTO_IPV4] = "ip",
64 [NFPROTO_ARP] = "arp",
65 [NFPROTO_BRIDGE] = "eb",
66 [NFPROTO_IPV6] = "ip6",
37f9f733
PM
67};
68
2e4e6a17
HW
69/* Registration hooks for targets. */
70int
a45049c5 71xt_register_target(struct xt_target *target)
2e4e6a17 72{
76108cea
JE
73 u_int8_t af = target->family;
74 int ret;
2e4e6a17 75
9e19bb6d 76 ret = mutex_lock_interruptible(&xt[af].mutex);
2e4e6a17
HW
77 if (ret != 0)
78 return ret;
79 list_add(&target->list, &xt[af].target);
9e19bb6d 80 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
81 return ret;
82}
83EXPORT_SYMBOL(xt_register_target);
84
85void
a45049c5 86xt_unregister_target(struct xt_target *target)
2e4e6a17 87{
76108cea 88 u_int8_t af = target->family;
a45049c5 89
9e19bb6d 90 mutex_lock(&xt[af].mutex);
df0933dc 91 list_del(&target->list);
9e19bb6d 92 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
93}
94EXPORT_SYMBOL(xt_unregister_target);
95
52d9c42e
PM
96int
97xt_register_targets(struct xt_target *target, unsigned int n)
98{
99 unsigned int i;
100 int err = 0;
101
102 for (i = 0; i < n; i++) {
103 err = xt_register_target(&target[i]);
104 if (err)
105 goto err;
106 }
107 return err;
108
109err:
110 if (i > 0)
111 xt_unregister_targets(target, i);
112 return err;
113}
114EXPORT_SYMBOL(xt_register_targets);
115
116void
117xt_unregister_targets(struct xt_target *target, unsigned int n)
118{
119 unsigned int i;
120
121 for (i = 0; i < n; i++)
122 xt_unregister_target(&target[i]);
123}
124EXPORT_SYMBOL(xt_unregister_targets);
125
2e4e6a17 126int
a45049c5 127xt_register_match(struct xt_match *match)
2e4e6a17 128{
76108cea
JE
129 u_int8_t af = match->family;
130 int ret;
2e4e6a17 131
9e19bb6d 132 ret = mutex_lock_interruptible(&xt[af].mutex);
2e4e6a17
HW
133 if (ret != 0)
134 return ret;
135
136 list_add(&match->list, &xt[af].match);
9e19bb6d 137 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
138
139 return ret;
140}
141EXPORT_SYMBOL(xt_register_match);
142
143void
a45049c5 144xt_unregister_match(struct xt_match *match)
2e4e6a17 145{
76108cea 146 u_int8_t af = match->family;
a45049c5 147
9e19bb6d 148 mutex_lock(&xt[af].mutex);
df0933dc 149 list_del(&match->list);
9e19bb6d 150 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
151}
152EXPORT_SYMBOL(xt_unregister_match);
153
52d9c42e
PM
154int
155xt_register_matches(struct xt_match *match, unsigned int n)
156{
157 unsigned int i;
158 int err = 0;
159
160 for (i = 0; i < n; i++) {
161 err = xt_register_match(&match[i]);
162 if (err)
163 goto err;
164 }
165 return err;
166
167err:
168 if (i > 0)
169 xt_unregister_matches(match, i);
170 return err;
171}
172EXPORT_SYMBOL(xt_register_matches);
173
174void
175xt_unregister_matches(struct xt_match *match, unsigned int n)
176{
177 unsigned int i;
178
179 for (i = 0; i < n; i++)
180 xt_unregister_match(&match[i]);
181}
182EXPORT_SYMBOL(xt_unregister_matches);
183
2e4e6a17
HW
184
185/*
186 * These are weird, but module loading must not be done with mutex
187 * held (since they will register), and we have to have a single
188 * function to use try_then_request_module().
189 */
190
191/* Find match, grabs ref. Returns ERR_PTR() on error. */
76108cea 192struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
2e4e6a17
HW
193{
194 struct xt_match *m;
195 int err = 0;
196
9e19bb6d 197 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
2e4e6a17
HW
198 return ERR_PTR(-EINTR);
199
200 list_for_each_entry(m, &xt[af].match, list) {
201 if (strcmp(m->name, name) == 0) {
202 if (m->revision == revision) {
203 if (try_module_get(m->me)) {
9e19bb6d 204 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
205 return m;
206 }
207 } else
208 err = -EPROTOTYPE; /* Found something. */
209 }
210 }
9e19bb6d 211 mutex_unlock(&xt[af].mutex);
55b69e91
JE
212
213 if (af != NFPROTO_UNSPEC)
214 /* Try searching again in the family-independent list */
215 return xt_find_match(NFPROTO_UNSPEC, name, revision);
216
2e4e6a17
HW
217 return ERR_PTR(err);
218}
219EXPORT_SYMBOL(xt_find_match);
220
221/* Find target, grabs ref. Returns ERR_PTR() on error. */
76108cea 222struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
2e4e6a17
HW
223{
224 struct xt_target *t;
225 int err = 0;
226
9e19bb6d 227 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
2e4e6a17
HW
228 return ERR_PTR(-EINTR);
229
230 list_for_each_entry(t, &xt[af].target, list) {
231 if (strcmp(t->name, name) == 0) {
232 if (t->revision == revision) {
233 if (try_module_get(t->me)) {
9e19bb6d 234 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
235 return t;
236 }
237 } else
238 err = -EPROTOTYPE; /* Found something. */
239 }
240 }
9e19bb6d 241 mutex_unlock(&xt[af].mutex);
55b69e91
JE
242
243 if (af != NFPROTO_UNSPEC)
244 /* Try searching again in the family-independent list */
245 return xt_find_target(NFPROTO_UNSPEC, name, revision);
246
2e4e6a17
HW
247 return ERR_PTR(err);
248}
249EXPORT_SYMBOL(xt_find_target);
250
76108cea 251struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
2e4e6a17
HW
252{
253 struct xt_target *target;
254
255 target = try_then_request_module(xt_find_target(af, name, revision),
37f9f733 256 "%st_%s", xt_prefix[af], name);
2e4e6a17
HW
257 if (IS_ERR(target) || !target)
258 return NULL;
259 return target;
260}
261EXPORT_SYMBOL_GPL(xt_request_find_target);
262
76108cea 263static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
2e4e6a17 264{
5452e425 265 const struct xt_match *m;
2e4e6a17
HW
266 int have_rev = 0;
267
268 list_for_each_entry(m, &xt[af].match, list) {
269 if (strcmp(m->name, name) == 0) {
270 if (m->revision > *bestp)
271 *bestp = m->revision;
272 if (m->revision == revision)
273 have_rev = 1;
274 }
275 }
276 return have_rev;
277}
278
76108cea 279static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
2e4e6a17 280{
5452e425 281 const struct xt_target *t;
2e4e6a17
HW
282 int have_rev = 0;
283
284 list_for_each_entry(t, &xt[af].target, list) {
285 if (strcmp(t->name, name) == 0) {
286 if (t->revision > *bestp)
287 *bestp = t->revision;
288 if (t->revision == revision)
289 have_rev = 1;
290 }
291 }
292 return have_rev;
293}
294
295/* Returns true or false (if no such extension at all) */
76108cea 296int xt_find_revision(u8 af, const char *name, u8 revision, int target,
2e4e6a17
HW
297 int *err)
298{
299 int have_rev, best = -1;
300
9e19bb6d 301 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
2e4e6a17
HW
302 *err = -EINTR;
303 return 1;
304 }
305 if (target == 1)
306 have_rev = target_revfn(af, name, revision, &best);
307 else
308 have_rev = match_revfn(af, name, revision, &best);
9e19bb6d 309 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
310
311 /* Nothing at all? Return 0 to try loading module. */
312 if (best == -1) {
313 *err = -ENOENT;
314 return 0;
315 }
316
317 *err = best;
318 if (!have_rev)
319 *err = -EPROTONOSUPPORT;
320 return 1;
321}
322EXPORT_SYMBOL_GPL(xt_find_revision);
323
37f9f733 324int xt_check_match(const struct xt_match *match, unsigned short family,
601e68e1 325 unsigned int size, const char *table, unsigned int hook_mask,
37f9f733
PM
326 unsigned short proto, int inv_proto)
327{
328 if (XT_ALIGN(match->matchsize) != size) {
329 printk("%s_tables: %s match: invalid size %Zu != %u\n",
330 xt_prefix[family], match->name,
331 XT_ALIGN(match->matchsize), size);
332 return -EINVAL;
333 }
334 if (match->table && strcmp(match->table, table)) {
335 printk("%s_tables: %s match: only valid in %s table, not %s\n",
336 xt_prefix[family], match->name, match->table, table);
337 return -EINVAL;
338 }
339 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
5faf4153
BS
340 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
341 xt_prefix[family], match->name, hook_mask, match->hooks);
37f9f733
PM
342 return -EINVAL;
343 }
344 if (match->proto && (match->proto != proto || inv_proto)) {
345 printk("%s_tables: %s match: only valid for protocol %u\n",
346 xt_prefix[family], match->name, match->proto);
347 return -EINVAL;
348 }
349 return 0;
350}
351EXPORT_SYMBOL_GPL(xt_check_match);
352
2722971c 353#ifdef CONFIG_COMPAT
76108cea 354int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta)
b386d9f5
PM
355{
356 struct compat_delta *tmp;
357
358 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
359 if (!tmp)
360 return -ENOMEM;
361
362 tmp->offset = offset;
363 tmp->delta = delta;
364
365 if (xt[af].compat_offsets) {
366 tmp->next = xt[af].compat_offsets->next;
367 xt[af].compat_offsets->next = tmp;
368 } else {
369 xt[af].compat_offsets = tmp;
370 tmp->next = NULL;
371 }
372 return 0;
373}
374EXPORT_SYMBOL_GPL(xt_compat_add_offset);
375
76108cea 376void xt_compat_flush_offsets(u_int8_t af)
b386d9f5
PM
377{
378 struct compat_delta *tmp, *next;
379
380 if (xt[af].compat_offsets) {
381 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
382 next = tmp->next;
383 kfree(tmp);
384 }
385 xt[af].compat_offsets = NULL;
386 }
387}
388EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
389
76108cea 390short xt_compat_calc_jump(u_int8_t af, unsigned int offset)
b386d9f5
PM
391{
392 struct compat_delta *tmp;
393 short delta;
394
395 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
396 if (tmp->offset < offset)
397 delta += tmp->delta;
398 return delta;
399}
400EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
401
5452e425 402int xt_compat_match_offset(const struct xt_match *match)
2722971c 403{
9fa492cd
PM
404 u_int16_t csize = match->compatsize ? : match->matchsize;
405 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
406}
407EXPORT_SYMBOL_GPL(xt_compat_match_offset);
408
89566951 409int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
b0a6363c 410 unsigned int *size)
9fa492cd 411{
5452e425 412 const struct xt_match *match = m->u.kernel.match;
9fa492cd
PM
413 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
414 int pad, off = xt_compat_match_offset(match);
415 u_int16_t msize = cm->u.user.match_size;
416
417 m = *dstptr;
418 memcpy(m, cm, sizeof(*cm));
419 if (match->compat_from_user)
420 match->compat_from_user(m->data, cm->data);
421 else
422 memcpy(m->data, cm->data, msize - sizeof(*cm));
423 pad = XT_ALIGN(match->matchsize) - match->matchsize;
424 if (pad > 0)
425 memset(m->data + match->matchsize, 0, pad);
426
427 msize += off;
428 m->u.user.match_size = msize;
429
430 *size += off;
431 *dstptr += msize;
89566951 432 return 0;
9fa492cd
PM
433}
434EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
435
436int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
b0a6363c 437 unsigned int *size)
9fa492cd 438{
5452e425 439 const struct xt_match *match = m->u.kernel.match;
9fa492cd
PM
440 struct compat_xt_entry_match __user *cm = *dstptr;
441 int off = xt_compat_match_offset(match);
442 u_int16_t msize = m->u.user.match_size - off;
443
444 if (copy_to_user(cm, m, sizeof(*cm)) ||
a18aa31b
PM
445 put_user(msize, &cm->u.user.match_size) ||
446 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
447 strlen(m->u.kernel.match->name) + 1))
601e68e1 448 return -EFAULT;
9fa492cd
PM
449
450 if (match->compat_to_user) {
451 if (match->compat_to_user((void __user *)cm->data, m->data))
452 return -EFAULT;
453 } else {
454 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
455 return -EFAULT;
2722971c 456 }
9fa492cd
PM
457
458 *size -= off;
459 *dstptr += msize;
460 return 0;
2722971c 461}
9fa492cd
PM
462EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
463#endif /* CONFIG_COMPAT */
2722971c 464
37f9f733
PM
465int xt_check_target(const struct xt_target *target, unsigned short family,
466 unsigned int size, const char *table, unsigned int hook_mask,
467 unsigned short proto, int inv_proto)
468{
469 if (XT_ALIGN(target->targetsize) != size) {
470 printk("%s_tables: %s target: invalid size %Zu != %u\n",
471 xt_prefix[family], target->name,
472 XT_ALIGN(target->targetsize), size);
473 return -EINVAL;
474 }
475 if (target->table && strcmp(target->table, table)) {
476 printk("%s_tables: %s target: only valid in %s table, not %s\n",
477 xt_prefix[family], target->name, target->table, table);
478 return -EINVAL;
479 }
480 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
5faf4153
BS
481 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
482 xt_prefix[family], target->name, hook_mask,
483 target->hooks);
37f9f733
PM
484 return -EINVAL;
485 }
486 if (target->proto && (target->proto != proto || inv_proto)) {
487 printk("%s_tables: %s target: only valid for protocol %u\n",
488 xt_prefix[family], target->name, target->proto);
489 return -EINVAL;
490 }
491 return 0;
492}
493EXPORT_SYMBOL_GPL(xt_check_target);
494
2722971c 495#ifdef CONFIG_COMPAT
5452e425 496int xt_compat_target_offset(const struct xt_target *target)
2722971c 497{
9fa492cd
PM
498 u_int16_t csize = target->compatsize ? : target->targetsize;
499 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
500}
501EXPORT_SYMBOL_GPL(xt_compat_target_offset);
502
503void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
b0a6363c 504 unsigned int *size)
9fa492cd 505{
5452e425 506 const struct xt_target *target = t->u.kernel.target;
9fa492cd
PM
507 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
508 int pad, off = xt_compat_target_offset(target);
509 u_int16_t tsize = ct->u.user.target_size;
510
511 t = *dstptr;
512 memcpy(t, ct, sizeof(*ct));
513 if (target->compat_from_user)
514 target->compat_from_user(t->data, ct->data);
515 else
516 memcpy(t->data, ct->data, tsize - sizeof(*ct));
517 pad = XT_ALIGN(target->targetsize) - target->targetsize;
518 if (pad > 0)
519 memset(t->data + target->targetsize, 0, pad);
520
521 tsize += off;
522 t->u.user.target_size = tsize;
523
524 *size += off;
525 *dstptr += tsize;
526}
527EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
528
529int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
b0a6363c 530 unsigned int *size)
9fa492cd 531{
5452e425 532 const struct xt_target *target = t->u.kernel.target;
9fa492cd
PM
533 struct compat_xt_entry_target __user *ct = *dstptr;
534 int off = xt_compat_target_offset(target);
535 u_int16_t tsize = t->u.user.target_size - off;
536
537 if (copy_to_user(ct, t, sizeof(*ct)) ||
a18aa31b
PM
538 put_user(tsize, &ct->u.user.target_size) ||
539 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
540 strlen(t->u.kernel.target->name) + 1))
601e68e1 541 return -EFAULT;
9fa492cd
PM
542
543 if (target->compat_to_user) {
544 if (target->compat_to_user((void __user *)ct->data, t->data))
545 return -EFAULT;
546 } else {
547 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
548 return -EFAULT;
2722971c 549 }
9fa492cd
PM
550
551 *size -= off;
552 *dstptr += tsize;
553 return 0;
2722971c 554}
9fa492cd 555EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
2722971c
DM
556#endif
557
2e4e6a17
HW
558struct xt_table_info *xt_alloc_table_info(unsigned int size)
559{
560 struct xt_table_info *newinfo;
561 int cpu;
562
563 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
564 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
565 return NULL;
566
259d4e41 567 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
2e4e6a17
HW
568 if (!newinfo)
569 return NULL;
570
571 newinfo->size = size;
572
6f912042 573 for_each_possible_cpu(cpu) {
2e4e6a17
HW
574 if (size <= PAGE_SIZE)
575 newinfo->entries[cpu] = kmalloc_node(size,
576 GFP_KERNEL,
577 cpu_to_node(cpu));
578 else
579 newinfo->entries[cpu] = vmalloc_node(size,
580 cpu_to_node(cpu));
581
582 if (newinfo->entries[cpu] == NULL) {
583 xt_free_table_info(newinfo);
584 return NULL;
585 }
586 }
587
588 return newinfo;
589}
590EXPORT_SYMBOL(xt_alloc_table_info);
591
592void xt_free_table_info(struct xt_table_info *info)
593{
594 int cpu;
595
6f912042 596 for_each_possible_cpu(cpu) {
2e4e6a17
HW
597 if (info->size <= PAGE_SIZE)
598 kfree(info->entries[cpu]);
599 else
600 vfree(info->entries[cpu]);
601 }
602 kfree(info);
603}
604EXPORT_SYMBOL(xt_free_table_info);
605
606/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
76108cea
JE
607struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
608 const char *name)
2e4e6a17
HW
609{
610 struct xt_table *t;
611
9e19bb6d 612 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
2e4e6a17
HW
613 return ERR_PTR(-EINTR);
614
8d870052 615 list_for_each_entry(t, &net->xt.tables[af], list)
2e4e6a17
HW
616 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
617 return t;
9e19bb6d 618 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
619 return NULL;
620}
621EXPORT_SYMBOL_GPL(xt_find_table_lock);
622
623void xt_table_unlock(struct xt_table *table)
624{
9e19bb6d 625 mutex_unlock(&xt[table->af].mutex);
2e4e6a17
HW
626}
627EXPORT_SYMBOL_GPL(xt_table_unlock);
628
2722971c 629#ifdef CONFIG_COMPAT
76108cea 630void xt_compat_lock(u_int8_t af)
2722971c
DM
631{
632 mutex_lock(&xt[af].compat_mutex);
633}
634EXPORT_SYMBOL_GPL(xt_compat_lock);
635
76108cea 636void xt_compat_unlock(u_int8_t af)
2722971c
DM
637{
638 mutex_unlock(&xt[af].compat_mutex);
639}
640EXPORT_SYMBOL_GPL(xt_compat_unlock);
641#endif
2e4e6a17
HW
642
643struct xt_table_info *
644xt_replace_table(struct xt_table *table,
645 unsigned int num_counters,
646 struct xt_table_info *newinfo,
647 int *error)
648{
649 struct xt_table_info *oldinfo, *private;
650
651 /* Do the substitution. */
652 write_lock_bh(&table->lock);
653 private = table->private;
654 /* Check inside lock: is the old number correct? */
655 if (num_counters != private->number) {
656 duprintf("num_counters != table->private->number (%u/%u)\n",
657 num_counters, private->number);
658 write_unlock_bh(&table->lock);
659 *error = -EAGAIN;
660 return NULL;
661 }
662 oldinfo = private;
663 table->private = newinfo;
664 newinfo->initial_entries = oldinfo->initial_entries;
665 write_unlock_bh(&table->lock);
666
667 return oldinfo;
668}
669EXPORT_SYMBOL_GPL(xt_replace_table);
670
8d870052 671struct xt_table *xt_register_table(struct net *net, struct xt_table *table,
a98da11d
AD
672 struct xt_table_info *bootstrap,
673 struct xt_table_info *newinfo)
2e4e6a17
HW
674{
675 int ret;
676 struct xt_table_info *private;
df0933dc 677 struct xt_table *t;
2e4e6a17 678
44d34e72
AD
679 /* Don't add one object to multiple lists. */
680 table = kmemdup(table, sizeof(struct xt_table), GFP_KERNEL);
681 if (!table) {
682 ret = -ENOMEM;
683 goto out;
684 }
685
9e19bb6d 686 ret = mutex_lock_interruptible(&xt[table->af].mutex);
2e4e6a17 687 if (ret != 0)
44d34e72 688 goto out_free;
2e4e6a17
HW
689
690 /* Don't autoload: we'd eat our tail... */
8d870052 691 list_for_each_entry(t, &net->xt.tables[table->af], list) {
df0933dc
PM
692 if (strcmp(t->name, table->name) == 0) {
693 ret = -EEXIST;
694 goto unlock;
695 }
2e4e6a17
HW
696 }
697
698 /* Simplifies replace_table code. */
699 table->private = bootstrap;
91536b7a 700 rwlock_init(&table->lock);
2e4e6a17
HW
701 if (!xt_replace_table(table, 0, newinfo, &ret))
702 goto unlock;
703
704 private = table->private;
705 duprintf("table->private->number = %u\n", private->number);
706
707 /* save number of initial entries */
708 private->initial_entries = private->number;
709
8d870052 710 list_add(&table->list, &net->xt.tables[table->af]);
a98da11d
AD
711 mutex_unlock(&xt[table->af].mutex);
712 return table;
2e4e6a17 713
2e4e6a17 714 unlock:
9e19bb6d 715 mutex_unlock(&xt[table->af].mutex);
44d34e72
AD
716out_free:
717 kfree(table);
a98da11d
AD
718out:
719 return ERR_PTR(ret);
2e4e6a17
HW
720}
721EXPORT_SYMBOL_GPL(xt_register_table);
722
723void *xt_unregister_table(struct xt_table *table)
724{
725 struct xt_table_info *private;
726
9e19bb6d 727 mutex_lock(&xt[table->af].mutex);
2e4e6a17 728 private = table->private;
df0933dc 729 list_del(&table->list);
9e19bb6d 730 mutex_unlock(&xt[table->af].mutex);
44d34e72 731 kfree(table);
2e4e6a17
HW
732
733 return private;
734}
735EXPORT_SYMBOL_GPL(xt_unregister_table);
736
737#ifdef CONFIG_PROC_FS
715cf35a
AD
738struct xt_names_priv {
739 struct seq_net_private p;
76108cea 740 u_int8_t af;
715cf35a 741};
025d93d1 742static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
2e4e6a17 743{
715cf35a 744 struct xt_names_priv *priv = seq->private;
1218854a 745 struct net *net = seq_file_net(seq);
76108cea 746 u_int8_t af = priv->af;
2e4e6a17 747
025d93d1 748 mutex_lock(&xt[af].mutex);
715cf35a 749 return seq_list_start(&net->xt.tables[af], *pos);
025d93d1 750}
2e4e6a17 751
025d93d1
AD
752static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
753{
715cf35a 754 struct xt_names_priv *priv = seq->private;
1218854a 755 struct net *net = seq_file_net(seq);
76108cea 756 u_int8_t af = priv->af;
2e4e6a17 757
715cf35a 758 return seq_list_next(v, &net->xt.tables[af], pos);
2e4e6a17
HW
759}
760
025d93d1 761static void xt_table_seq_stop(struct seq_file *seq, void *v)
2e4e6a17 762{
715cf35a 763 struct xt_names_priv *priv = seq->private;
76108cea 764 u_int8_t af = priv->af;
2e4e6a17 765
025d93d1
AD
766 mutex_unlock(&xt[af].mutex);
767}
2e4e6a17 768
025d93d1
AD
769static int xt_table_seq_show(struct seq_file *seq, void *v)
770{
771 struct xt_table *table = list_entry(v, struct xt_table, list);
2e4e6a17 772
025d93d1
AD
773 if (strlen(table->name))
774 return seq_printf(seq, "%s\n", table->name);
775 else
776 return 0;
777}
601e68e1 778
025d93d1
AD
779static const struct seq_operations xt_table_seq_ops = {
780 .start = xt_table_seq_start,
781 .next = xt_table_seq_next,
782 .stop = xt_table_seq_stop,
783 .show = xt_table_seq_show,
784};
785
786static int xt_table_open(struct inode *inode, struct file *file)
787{
788 int ret;
715cf35a 789 struct xt_names_priv *priv;
025d93d1 790
715cf35a
AD
791 ret = seq_open_net(inode, file, &xt_table_seq_ops,
792 sizeof(struct xt_names_priv));
025d93d1 793 if (!ret) {
715cf35a
AD
794 priv = ((struct seq_file *)file->private_data)->private;
795 priv->af = (unsigned long)PDE(inode)->data;
025d93d1
AD
796 }
797 return ret;
2e4e6a17
HW
798}
799
025d93d1
AD
800static const struct file_operations xt_table_ops = {
801 .owner = THIS_MODULE,
802 .open = xt_table_open,
803 .read = seq_read,
804 .llseek = seq_lseek,
0e93bb94 805 .release = seq_release_net,
025d93d1
AD
806};
807
808static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
2e4e6a17 809{
025d93d1
AD
810 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
811 u_int16_t af = (unsigned long)pde->data;
2e4e6a17 812
025d93d1
AD
813 mutex_lock(&xt[af].mutex);
814 return seq_list_start(&xt[af].match, *pos);
815}
601e68e1 816
025d93d1
AD
817static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *pos)
818{
819 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
820 u_int16_t af = (unsigned long)pde->data;
2e4e6a17 821
025d93d1 822 return seq_list_next(v, &xt[af].match, pos);
2e4e6a17
HW
823}
824
025d93d1 825static void xt_match_seq_stop(struct seq_file *seq, void *v)
2e4e6a17
HW
826{
827 struct proc_dir_entry *pde = seq->private;
025d93d1 828 u_int16_t af = (unsigned long)pde->data;
2e4e6a17 829
9e19bb6d 830 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
831}
832
025d93d1 833static int xt_match_seq_show(struct seq_file *seq, void *v)
2e4e6a17 834{
025d93d1 835 struct xt_match *match = list_entry(v, struct xt_match, list);
2e4e6a17 836
025d93d1
AD
837 if (strlen(match->name))
838 return seq_printf(seq, "%s\n", match->name);
2e4e6a17
HW
839 else
840 return 0;
841}
842
025d93d1
AD
843static const struct seq_operations xt_match_seq_ops = {
844 .start = xt_match_seq_start,
845 .next = xt_match_seq_next,
846 .stop = xt_match_seq_stop,
847 .show = xt_match_seq_show,
2e4e6a17
HW
848};
849
025d93d1 850static int xt_match_open(struct inode *inode, struct file *file)
2e4e6a17
HW
851{
852 int ret;
853
025d93d1 854 ret = seq_open(file, &xt_match_seq_ops);
2e4e6a17
HW
855 if (!ret) {
856 struct seq_file *seq = file->private_data;
2e4e6a17 857
025d93d1 858 seq->private = PDE(inode);
2e4e6a17 859 }
025d93d1
AD
860 return ret;
861}
862
863static const struct file_operations xt_match_ops = {
864 .owner = THIS_MODULE,
865 .open = xt_match_open,
866 .read = seq_read,
867 .llseek = seq_lseek,
868 .release = seq_release,
869};
2e4e6a17 870
025d93d1
AD
871static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
872{
873 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
874 u_int16_t af = (unsigned long)pde->data;
875
876 mutex_lock(&xt[af].mutex);
877 return seq_list_start(&xt[af].target, *pos);
878}
879
880static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *pos)
881{
882 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
883 u_int16_t af = (unsigned long)pde->data;
884
885 return seq_list_next(v, &xt[af].target, pos);
886}
887
888static void xt_target_seq_stop(struct seq_file *seq, void *v)
889{
890 struct proc_dir_entry *pde = seq->private;
891 u_int16_t af = (unsigned long)pde->data;
892
893 mutex_unlock(&xt[af].mutex);
894}
895
896static int xt_target_seq_show(struct seq_file *seq, void *v)
897{
898 struct xt_target *target = list_entry(v, struct xt_target, list);
899
900 if (strlen(target->name))
901 return seq_printf(seq, "%s\n", target->name);
902 else
903 return 0;
904}
905
906static const struct seq_operations xt_target_seq_ops = {
907 .start = xt_target_seq_start,
908 .next = xt_target_seq_next,
909 .stop = xt_target_seq_stop,
910 .show = xt_target_seq_show,
911};
912
913static int xt_target_open(struct inode *inode, struct file *file)
914{
915 int ret;
916
917 ret = seq_open(file, &xt_target_seq_ops);
918 if (!ret) {
919 struct seq_file *seq = file->private_data;
920
921 seq->private = PDE(inode);
922 }
2e4e6a17
HW
923 return ret;
924}
925
025d93d1 926static const struct file_operations xt_target_ops = {
2e4e6a17 927 .owner = THIS_MODULE,
025d93d1 928 .open = xt_target_open,
2e4e6a17
HW
929 .read = seq_read,
930 .llseek = seq_lseek,
931 .release = seq_release,
932};
933
934#define FORMAT_TABLES "_tables_names"
935#define FORMAT_MATCHES "_tables_matches"
936#define FORMAT_TARGETS "_tables_targets"
937
938#endif /* CONFIG_PROC_FS */
939
76108cea 940int xt_proto_init(struct net *net, u_int8_t af)
2e4e6a17
HW
941{
942#ifdef CONFIG_PROC_FS
943 char buf[XT_FUNCTION_MAXNAMELEN];
944 struct proc_dir_entry *proc;
945#endif
946
7e9c6eeb 947 if (af >= ARRAY_SIZE(xt_prefix))
2e4e6a17
HW
948 return -EINVAL;
949
950
951#ifdef CONFIG_PROC_FS
ce18afe5 952 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 953 strlcat(buf, FORMAT_TABLES, sizeof(buf));
8b169240
DL
954 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
955 (void *)(unsigned long)af);
2e4e6a17
HW
956 if (!proc)
957 goto out;
2e4e6a17 958
ce18afe5 959 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 960 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
8b169240
DL
961 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
962 (void *)(unsigned long)af);
2e4e6a17
HW
963 if (!proc)
964 goto out_remove_tables;
2e4e6a17 965
ce18afe5 966 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 967 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
8b169240
DL
968 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
969 (void *)(unsigned long)af);
2e4e6a17
HW
970 if (!proc)
971 goto out_remove_matches;
2e4e6a17
HW
972#endif
973
974 return 0;
975
976#ifdef CONFIG_PROC_FS
977out_remove_matches:
ce18afe5 978 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 979 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
3cb609d5 980 proc_net_remove(net, buf);
2e4e6a17
HW
981
982out_remove_tables:
ce18afe5 983 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 984 strlcat(buf, FORMAT_TABLES, sizeof(buf));
3cb609d5 985 proc_net_remove(net, buf);
2e4e6a17
HW
986out:
987 return -1;
988#endif
989}
990EXPORT_SYMBOL_GPL(xt_proto_init);
991
76108cea 992void xt_proto_fini(struct net *net, u_int8_t af)
2e4e6a17
HW
993{
994#ifdef CONFIG_PROC_FS
995 char buf[XT_FUNCTION_MAXNAMELEN];
996
ce18afe5 997 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 998 strlcat(buf, FORMAT_TABLES, sizeof(buf));
3cb609d5 999 proc_net_remove(net, buf);
2e4e6a17 1000
ce18afe5 1001 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1002 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
3cb609d5 1003 proc_net_remove(net, buf);
2e4e6a17 1004
ce18afe5 1005 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1006 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
3cb609d5 1007 proc_net_remove(net, buf);
2e4e6a17
HW
1008#endif /*CONFIG_PROC_FS*/
1009}
1010EXPORT_SYMBOL_GPL(xt_proto_fini);
1011
8d870052
AD
1012static int __net_init xt_net_init(struct net *net)
1013{
1014 int i;
1015
7e9c6eeb 1016 for (i = 0; i < NFPROTO_NUMPROTO; i++)
8d870052
AD
1017 INIT_LIST_HEAD(&net->xt.tables[i]);
1018 return 0;
1019}
1020
1021static struct pernet_operations xt_net_ops = {
1022 .init = xt_net_init,
1023};
2e4e6a17
HW
1024
1025static int __init xt_init(void)
1026{
8d870052 1027 int i, rv;
2e4e6a17 1028
7e9c6eeb 1029 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
2e4e6a17
HW
1030 if (!xt)
1031 return -ENOMEM;
1032
7e9c6eeb 1033 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
9e19bb6d 1034 mutex_init(&xt[i].mutex);
2722971c
DM
1035#ifdef CONFIG_COMPAT
1036 mutex_init(&xt[i].compat_mutex);
b386d9f5 1037 xt[i].compat_offsets = NULL;
2722971c 1038#endif
2e4e6a17
HW
1039 INIT_LIST_HEAD(&xt[i].target);
1040 INIT_LIST_HEAD(&xt[i].match);
2e4e6a17 1041 }
8d870052
AD
1042 rv = register_pernet_subsys(&xt_net_ops);
1043 if (rv < 0)
1044 kfree(xt);
1045 return rv;
2e4e6a17
HW
1046}
1047
1048static void __exit xt_fini(void)
1049{
8d870052 1050 unregister_pernet_subsys(&xt_net_ops);
2e4e6a17
HW
1051 kfree(xt);
1052}
1053
1054module_init(xt_init);
1055module_exit(xt_fini);
1056