Merge branch 'master' of /repos/git/net-next-2.6
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / netfilter / x_tables.c
1 /*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
3 *
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5 *
6 * Based on existing ip_tables code which is
7 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
8 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
23 #include <linux/mutex.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/audit.h>
27 #include <net/net_namespace.h>
28
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_arp.h>
31 #include <linux/netfilter_ipv4/ip_tables.h>
32 #include <linux/netfilter_ipv6/ip6_tables.h>
33 #include <linux/netfilter_arp/arp_tables.h>
34
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
37 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
38
39 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
40
41 struct compat_delta {
42 unsigned int offset; /* offset in kernel */
43 int delta; /* delta in 32bit user land */
44 };
45
46 struct xt_af {
47 struct mutex mutex;
48 struct list_head match;
49 struct list_head target;
50 #ifdef CONFIG_COMPAT
51 struct mutex compat_mutex;
52 struct compat_delta *compat_tab;
53 unsigned int number; /* number of slots in compat_tab[] */
54 unsigned int cur; /* number of used slots in compat_tab[] */
55 #endif
56 };
57
58 static struct xt_af *xt;
59
60 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
61 [NFPROTO_UNSPEC] = "x",
62 [NFPROTO_IPV4] = "ip",
63 [NFPROTO_ARP] = "arp",
64 [NFPROTO_BRIDGE] = "eb",
65 [NFPROTO_IPV6] = "ip6",
66 };
67
68 /* Allow this many total (re)entries. */
69 static const unsigned int xt_jumpstack_multiplier = 2;
70
71 /* Registration hooks for targets. */
72 int
73 xt_register_target(struct xt_target *target)
74 {
75 u_int8_t af = target->family;
76 int ret;
77
78 ret = mutex_lock_interruptible(&xt[af].mutex);
79 if (ret != 0)
80 return ret;
81 list_add(&target->list, &xt[af].target);
82 mutex_unlock(&xt[af].mutex);
83 return ret;
84 }
85 EXPORT_SYMBOL(xt_register_target);
86
87 void
88 xt_unregister_target(struct xt_target *target)
89 {
90 u_int8_t af = target->family;
91
92 mutex_lock(&xt[af].mutex);
93 list_del(&target->list);
94 mutex_unlock(&xt[af].mutex);
95 }
96 EXPORT_SYMBOL(xt_unregister_target);
97
98 int
99 xt_register_targets(struct xt_target *target, unsigned int n)
100 {
101 unsigned int i;
102 int err = 0;
103
104 for (i = 0; i < n; i++) {
105 err = xt_register_target(&target[i]);
106 if (err)
107 goto err;
108 }
109 return err;
110
111 err:
112 if (i > 0)
113 xt_unregister_targets(target, i);
114 return err;
115 }
116 EXPORT_SYMBOL(xt_register_targets);
117
118 void
119 xt_unregister_targets(struct xt_target *target, unsigned int n)
120 {
121 while (n-- > 0)
122 xt_unregister_target(&target[n]);
123 }
124 EXPORT_SYMBOL(xt_unregister_targets);
125
126 int
127 xt_register_match(struct xt_match *match)
128 {
129 u_int8_t af = match->family;
130 int ret;
131
132 ret = mutex_lock_interruptible(&xt[af].mutex);
133 if (ret != 0)
134 return ret;
135
136 list_add(&match->list, &xt[af].match);
137 mutex_unlock(&xt[af].mutex);
138
139 return ret;
140 }
141 EXPORT_SYMBOL(xt_register_match);
142
143 void
144 xt_unregister_match(struct xt_match *match)
145 {
146 u_int8_t af = match->family;
147
148 mutex_lock(&xt[af].mutex);
149 list_del(&match->list);
150 mutex_unlock(&xt[af].mutex);
151 }
152 EXPORT_SYMBOL(xt_unregister_match);
153
154 int
155 xt_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
167 err:
168 if (i > 0)
169 xt_unregister_matches(match, i);
170 return err;
171 }
172 EXPORT_SYMBOL(xt_register_matches);
173
174 void
175 xt_unregister_matches(struct xt_match *match, unsigned int n)
176 {
177 while (n-- > 0)
178 xt_unregister_match(&match[n]);
179 }
180 EXPORT_SYMBOL(xt_unregister_matches);
181
182
183 /*
184 * These are weird, but module loading must not be done with mutex
185 * held (since they will register), and we have to have a single
186 * function to use try_then_request_module().
187 */
188
189 /* Find match, grabs ref. Returns ERR_PTR() on error. */
190 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
191 {
192 struct xt_match *m;
193 int err = 0;
194
195 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
196 return ERR_PTR(-EINTR);
197
198 list_for_each_entry(m, &xt[af].match, list) {
199 if (strcmp(m->name, name) == 0) {
200 if (m->revision == revision) {
201 if (try_module_get(m->me)) {
202 mutex_unlock(&xt[af].mutex);
203 return m;
204 }
205 } else
206 err = -EPROTOTYPE; /* Found something. */
207 }
208 }
209 mutex_unlock(&xt[af].mutex);
210
211 if (af != NFPROTO_UNSPEC)
212 /* Try searching again in the family-independent list */
213 return xt_find_match(NFPROTO_UNSPEC, name, revision);
214
215 return ERR_PTR(err);
216 }
217 EXPORT_SYMBOL(xt_find_match);
218
219 struct xt_match *
220 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
221 {
222 struct xt_match *match;
223
224 match = try_then_request_module(xt_find_match(nfproto, name, revision),
225 "%st_%s", xt_prefix[nfproto], name);
226 return (match != NULL) ? match : ERR_PTR(-ENOENT);
227 }
228 EXPORT_SYMBOL_GPL(xt_request_find_match);
229
230 /* Find target, grabs ref. Returns ERR_PTR() on error. */
231 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
232 {
233 struct xt_target *t;
234 int err = 0;
235
236 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
237 return ERR_PTR(-EINTR);
238
239 list_for_each_entry(t, &xt[af].target, list) {
240 if (strcmp(t->name, name) == 0) {
241 if (t->revision == revision) {
242 if (try_module_get(t->me)) {
243 mutex_unlock(&xt[af].mutex);
244 return t;
245 }
246 } else
247 err = -EPROTOTYPE; /* Found something. */
248 }
249 }
250 mutex_unlock(&xt[af].mutex);
251
252 if (af != NFPROTO_UNSPEC)
253 /* Try searching again in the family-independent list */
254 return xt_find_target(NFPROTO_UNSPEC, name, revision);
255
256 return ERR_PTR(err);
257 }
258 EXPORT_SYMBOL(xt_find_target);
259
260 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
261 {
262 struct xt_target *target;
263
264 target = try_then_request_module(xt_find_target(af, name, revision),
265 "%st_%s", xt_prefix[af], name);
266 return (target != NULL) ? target : ERR_PTR(-ENOENT);
267 }
268 EXPORT_SYMBOL_GPL(xt_request_find_target);
269
270 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
271 {
272 const struct xt_match *m;
273 int have_rev = 0;
274
275 list_for_each_entry(m, &xt[af].match, list) {
276 if (strcmp(m->name, name) == 0) {
277 if (m->revision > *bestp)
278 *bestp = m->revision;
279 if (m->revision == revision)
280 have_rev = 1;
281 }
282 }
283
284 if (af != NFPROTO_UNSPEC && !have_rev)
285 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
286
287 return have_rev;
288 }
289
290 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
291 {
292 const struct xt_target *t;
293 int have_rev = 0;
294
295 list_for_each_entry(t, &xt[af].target, list) {
296 if (strcmp(t->name, name) == 0) {
297 if (t->revision > *bestp)
298 *bestp = t->revision;
299 if (t->revision == revision)
300 have_rev = 1;
301 }
302 }
303
304 if (af != NFPROTO_UNSPEC && !have_rev)
305 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
306
307 return have_rev;
308 }
309
310 /* Returns true or false (if no such extension at all) */
311 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
312 int *err)
313 {
314 int have_rev, best = -1;
315
316 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
317 *err = -EINTR;
318 return 1;
319 }
320 if (target == 1)
321 have_rev = target_revfn(af, name, revision, &best);
322 else
323 have_rev = match_revfn(af, name, revision, &best);
324 mutex_unlock(&xt[af].mutex);
325
326 /* Nothing at all? Return 0 to try loading module. */
327 if (best == -1) {
328 *err = -ENOENT;
329 return 0;
330 }
331
332 *err = best;
333 if (!have_rev)
334 *err = -EPROTONOSUPPORT;
335 return 1;
336 }
337 EXPORT_SYMBOL_GPL(xt_find_revision);
338
339 static char *textify_hooks(char *buf, size_t size, unsigned int mask)
340 {
341 static const char *const names[] = {
342 "PREROUTING", "INPUT", "FORWARD",
343 "OUTPUT", "POSTROUTING", "BROUTING",
344 };
345 unsigned int i;
346 char *p = buf;
347 bool np = false;
348 int res;
349
350 *p = '\0';
351 for (i = 0; i < ARRAY_SIZE(names); ++i) {
352 if (!(mask & (1 << i)))
353 continue;
354 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
355 if (res > 0) {
356 size -= res;
357 p += res;
358 }
359 np = true;
360 }
361
362 return buf;
363 }
364
365 int xt_check_match(struct xt_mtchk_param *par,
366 unsigned int size, u_int8_t proto, bool inv_proto)
367 {
368 int ret;
369
370 if (XT_ALIGN(par->match->matchsize) != size &&
371 par->match->matchsize != -1) {
372 /*
373 * ebt_among is exempt from centralized matchsize checking
374 * because it uses a dynamic-size data set.
375 */
376 pr_err("%s_tables: %s.%u match: invalid size "
377 "%u (kernel) != (user) %u\n",
378 xt_prefix[par->family], par->match->name,
379 par->match->revision,
380 XT_ALIGN(par->match->matchsize), size);
381 return -EINVAL;
382 }
383 if (par->match->table != NULL &&
384 strcmp(par->match->table, par->table) != 0) {
385 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
386 xt_prefix[par->family], par->match->name,
387 par->match->table, par->table);
388 return -EINVAL;
389 }
390 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
391 char used[64], allow[64];
392
393 pr_err("%s_tables: %s match: used from hooks %s, but only "
394 "valid from %s\n",
395 xt_prefix[par->family], par->match->name,
396 textify_hooks(used, sizeof(used), par->hook_mask),
397 textify_hooks(allow, sizeof(allow), par->match->hooks));
398 return -EINVAL;
399 }
400 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
401 pr_err("%s_tables: %s match: only valid for protocol %u\n",
402 xt_prefix[par->family], par->match->name,
403 par->match->proto);
404 return -EINVAL;
405 }
406 if (par->match->checkentry != NULL) {
407 ret = par->match->checkentry(par);
408 if (ret < 0)
409 return ret;
410 else if (ret > 0)
411 /* Flag up potential errors. */
412 return -EIO;
413 }
414 return 0;
415 }
416 EXPORT_SYMBOL_GPL(xt_check_match);
417
418 #ifdef CONFIG_COMPAT
419 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
420 {
421 struct xt_af *xp = &xt[af];
422
423 if (!xp->compat_tab) {
424 if (!xp->number)
425 return -EINVAL;
426 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
427 if (!xp->compat_tab)
428 return -ENOMEM;
429 xp->cur = 0;
430 }
431
432 if (xp->cur >= xp->number)
433 return -EINVAL;
434
435 if (xp->cur)
436 delta += xp->compat_tab[xp->cur - 1].delta;
437 xp->compat_tab[xp->cur].offset = offset;
438 xp->compat_tab[xp->cur].delta = delta;
439 xp->cur++;
440 return 0;
441 }
442 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
443
444 void xt_compat_flush_offsets(u_int8_t af)
445 {
446 if (xt[af].compat_tab) {
447 vfree(xt[af].compat_tab);
448 xt[af].compat_tab = NULL;
449 xt[af].number = 0;
450 }
451 }
452 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
453
454 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
455 {
456 struct compat_delta *tmp = xt[af].compat_tab;
457 int mid, left = 0, right = xt[af].cur - 1;
458
459 while (left <= right) {
460 mid = (left + right) >> 1;
461 if (offset > tmp[mid].offset)
462 left = mid + 1;
463 else if (offset < tmp[mid].offset)
464 right = mid - 1;
465 else
466 return mid ? tmp[mid - 1].delta : 0;
467 }
468 WARN_ON_ONCE(1);
469 return 0;
470 }
471 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
472
473 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
474 {
475 xt[af].number = number;
476 xt[af].cur = 0;
477 }
478 EXPORT_SYMBOL(xt_compat_init_offsets);
479
480 int xt_compat_match_offset(const struct xt_match *match)
481 {
482 u_int16_t csize = match->compatsize ? : match->matchsize;
483 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
484 }
485 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
486
487 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
488 unsigned int *size)
489 {
490 const struct xt_match *match = m->u.kernel.match;
491 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
492 int pad, off = xt_compat_match_offset(match);
493 u_int16_t msize = cm->u.user.match_size;
494
495 m = *dstptr;
496 memcpy(m, cm, sizeof(*cm));
497 if (match->compat_from_user)
498 match->compat_from_user(m->data, cm->data);
499 else
500 memcpy(m->data, cm->data, msize - sizeof(*cm));
501 pad = XT_ALIGN(match->matchsize) - match->matchsize;
502 if (pad > 0)
503 memset(m->data + match->matchsize, 0, pad);
504
505 msize += off;
506 m->u.user.match_size = msize;
507
508 *size += off;
509 *dstptr += msize;
510 return 0;
511 }
512 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
513
514 int xt_compat_match_to_user(const struct xt_entry_match *m,
515 void __user **dstptr, unsigned int *size)
516 {
517 const struct xt_match *match = m->u.kernel.match;
518 struct compat_xt_entry_match __user *cm = *dstptr;
519 int off = xt_compat_match_offset(match);
520 u_int16_t msize = m->u.user.match_size - off;
521
522 if (copy_to_user(cm, m, sizeof(*cm)) ||
523 put_user(msize, &cm->u.user.match_size) ||
524 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
525 strlen(m->u.kernel.match->name) + 1))
526 return -EFAULT;
527
528 if (match->compat_to_user) {
529 if (match->compat_to_user((void __user *)cm->data, m->data))
530 return -EFAULT;
531 } else {
532 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
533 return -EFAULT;
534 }
535
536 *size -= off;
537 *dstptr += msize;
538 return 0;
539 }
540 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
541 #endif /* CONFIG_COMPAT */
542
543 int xt_check_target(struct xt_tgchk_param *par,
544 unsigned int size, u_int8_t proto, bool inv_proto)
545 {
546 int ret;
547
548 if (XT_ALIGN(par->target->targetsize) != size) {
549 pr_err("%s_tables: %s.%u target: invalid size "
550 "%u (kernel) != (user) %u\n",
551 xt_prefix[par->family], par->target->name,
552 par->target->revision,
553 XT_ALIGN(par->target->targetsize), size);
554 return -EINVAL;
555 }
556 if (par->target->table != NULL &&
557 strcmp(par->target->table, par->table) != 0) {
558 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
559 xt_prefix[par->family], par->target->name,
560 par->target->table, par->table);
561 return -EINVAL;
562 }
563 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
564 char used[64], allow[64];
565
566 pr_err("%s_tables: %s target: used from hooks %s, but only "
567 "usable from %s\n",
568 xt_prefix[par->family], par->target->name,
569 textify_hooks(used, sizeof(used), par->hook_mask),
570 textify_hooks(allow, sizeof(allow), par->target->hooks));
571 return -EINVAL;
572 }
573 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
574 pr_err("%s_tables: %s target: only valid for protocol %u\n",
575 xt_prefix[par->family], par->target->name,
576 par->target->proto);
577 return -EINVAL;
578 }
579 if (par->target->checkentry != NULL) {
580 ret = par->target->checkentry(par);
581 if (ret < 0)
582 return ret;
583 else if (ret > 0)
584 /* Flag up potential errors. */
585 return -EIO;
586 }
587 return 0;
588 }
589 EXPORT_SYMBOL_GPL(xt_check_target);
590
591 #ifdef CONFIG_COMPAT
592 int xt_compat_target_offset(const struct xt_target *target)
593 {
594 u_int16_t csize = target->compatsize ? : target->targetsize;
595 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
596 }
597 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
598
599 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
600 unsigned int *size)
601 {
602 const struct xt_target *target = t->u.kernel.target;
603 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
604 int pad, off = xt_compat_target_offset(target);
605 u_int16_t tsize = ct->u.user.target_size;
606
607 t = *dstptr;
608 memcpy(t, ct, sizeof(*ct));
609 if (target->compat_from_user)
610 target->compat_from_user(t->data, ct->data);
611 else
612 memcpy(t->data, ct->data, tsize - sizeof(*ct));
613 pad = XT_ALIGN(target->targetsize) - target->targetsize;
614 if (pad > 0)
615 memset(t->data + target->targetsize, 0, pad);
616
617 tsize += off;
618 t->u.user.target_size = tsize;
619
620 *size += off;
621 *dstptr += tsize;
622 }
623 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
624
625 int xt_compat_target_to_user(const struct xt_entry_target *t,
626 void __user **dstptr, unsigned int *size)
627 {
628 const struct xt_target *target = t->u.kernel.target;
629 struct compat_xt_entry_target __user *ct = *dstptr;
630 int off = xt_compat_target_offset(target);
631 u_int16_t tsize = t->u.user.target_size - off;
632
633 if (copy_to_user(ct, t, sizeof(*ct)) ||
634 put_user(tsize, &ct->u.user.target_size) ||
635 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
636 strlen(t->u.kernel.target->name) + 1))
637 return -EFAULT;
638
639 if (target->compat_to_user) {
640 if (target->compat_to_user((void __user *)ct->data, t->data))
641 return -EFAULT;
642 } else {
643 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
644 return -EFAULT;
645 }
646
647 *size -= off;
648 *dstptr += tsize;
649 return 0;
650 }
651 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
652 #endif
653
654 struct xt_table_info *xt_alloc_table_info(unsigned int size)
655 {
656 struct xt_table_info *newinfo;
657 int cpu;
658
659 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
660 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
661 return NULL;
662
663 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
664 if (!newinfo)
665 return NULL;
666
667 newinfo->size = size;
668
669 for_each_possible_cpu(cpu) {
670 if (size <= PAGE_SIZE)
671 newinfo->entries[cpu] = kmalloc_node(size,
672 GFP_KERNEL,
673 cpu_to_node(cpu));
674 else
675 newinfo->entries[cpu] = vmalloc_node(size,
676 cpu_to_node(cpu));
677
678 if (newinfo->entries[cpu] == NULL) {
679 xt_free_table_info(newinfo);
680 return NULL;
681 }
682 }
683
684 return newinfo;
685 }
686 EXPORT_SYMBOL(xt_alloc_table_info);
687
688 void xt_free_table_info(struct xt_table_info *info)
689 {
690 int cpu;
691
692 for_each_possible_cpu(cpu) {
693 if (info->size <= PAGE_SIZE)
694 kfree(info->entries[cpu]);
695 else
696 vfree(info->entries[cpu]);
697 }
698
699 if (info->jumpstack != NULL) {
700 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
701 for_each_possible_cpu(cpu)
702 vfree(info->jumpstack[cpu]);
703 } else {
704 for_each_possible_cpu(cpu)
705 kfree(info->jumpstack[cpu]);
706 }
707 }
708
709 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
710 vfree(info->jumpstack);
711 else
712 kfree(info->jumpstack);
713
714 free_percpu(info->stackptr);
715
716 kfree(info);
717 }
718 EXPORT_SYMBOL(xt_free_table_info);
719
720 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
721 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
722 const char *name)
723 {
724 struct xt_table *t;
725
726 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
727 return ERR_PTR(-EINTR);
728
729 list_for_each_entry(t, &net->xt.tables[af], list)
730 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
731 return t;
732 mutex_unlock(&xt[af].mutex);
733 return NULL;
734 }
735 EXPORT_SYMBOL_GPL(xt_find_table_lock);
736
737 void xt_table_unlock(struct xt_table *table)
738 {
739 mutex_unlock(&xt[table->af].mutex);
740 }
741 EXPORT_SYMBOL_GPL(xt_table_unlock);
742
743 #ifdef CONFIG_COMPAT
744 void xt_compat_lock(u_int8_t af)
745 {
746 mutex_lock(&xt[af].compat_mutex);
747 }
748 EXPORT_SYMBOL_GPL(xt_compat_lock);
749
750 void xt_compat_unlock(u_int8_t af)
751 {
752 mutex_unlock(&xt[af].compat_mutex);
753 }
754 EXPORT_SYMBOL_GPL(xt_compat_unlock);
755 #endif
756
757 DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks);
758 EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks);
759
760 static int xt_jumpstack_alloc(struct xt_table_info *i)
761 {
762 unsigned int size;
763 int cpu;
764
765 i->stackptr = alloc_percpu(unsigned int);
766 if (i->stackptr == NULL)
767 return -ENOMEM;
768
769 size = sizeof(void **) * nr_cpu_ids;
770 if (size > PAGE_SIZE)
771 i->jumpstack = vmalloc(size);
772 else
773 i->jumpstack = kmalloc(size, GFP_KERNEL);
774 if (i->jumpstack == NULL)
775 return -ENOMEM;
776 memset(i->jumpstack, 0, size);
777
778 i->stacksize *= xt_jumpstack_multiplier;
779 size = sizeof(void *) * i->stacksize;
780 for_each_possible_cpu(cpu) {
781 if (size > PAGE_SIZE)
782 i->jumpstack[cpu] = vmalloc_node(size,
783 cpu_to_node(cpu));
784 else
785 i->jumpstack[cpu] = kmalloc_node(size,
786 GFP_KERNEL, cpu_to_node(cpu));
787 if (i->jumpstack[cpu] == NULL)
788 /*
789 * Freeing will be done later on by the callers. The
790 * chain is: xt_replace_table -> __do_replace ->
791 * do_replace -> xt_free_table_info.
792 */
793 return -ENOMEM;
794 }
795
796 return 0;
797 }
798
799 struct xt_table_info *
800 xt_replace_table(struct xt_table *table,
801 unsigned int num_counters,
802 struct xt_table_info *newinfo,
803 int *error)
804 {
805 struct xt_table_info *private;
806 int ret;
807
808 ret = xt_jumpstack_alloc(newinfo);
809 if (ret < 0) {
810 *error = ret;
811 return NULL;
812 }
813
814 /* Do the substitution. */
815 local_bh_disable();
816 private = table->private;
817
818 /* Check inside lock: is the old number correct? */
819 if (num_counters != private->number) {
820 pr_debug("num_counters != table->private->number (%u/%u)\n",
821 num_counters, private->number);
822 local_bh_enable();
823 *error = -EAGAIN;
824 return NULL;
825 }
826
827 table->private = newinfo;
828 newinfo->initial_entries = private->initial_entries;
829
830 /*
831 * Even though table entries have now been swapped, other CPU's
832 * may still be using the old entries. This is okay, because
833 * resynchronization happens because of the locking done
834 * during the get_counters() routine.
835 */
836 local_bh_enable();
837
838 #ifdef CONFIG_AUDIT
839 if (audit_enabled) {
840 struct audit_buffer *ab;
841
842 ab = audit_log_start(current->audit_context, GFP_KERNEL,
843 AUDIT_NETFILTER_CFG);
844 if (ab) {
845 audit_log_format(ab, "table=%s family=%u entries=%u",
846 table->name, table->af,
847 private->number);
848 audit_log_end(ab);
849 }
850 }
851 #endif
852
853 return private;
854 }
855 EXPORT_SYMBOL_GPL(xt_replace_table);
856
857 struct xt_table *xt_register_table(struct net *net,
858 const struct xt_table *input_table,
859 struct xt_table_info *bootstrap,
860 struct xt_table_info *newinfo)
861 {
862 int ret;
863 struct xt_table_info *private;
864 struct xt_table *t, *table;
865
866 /* Don't add one object to multiple lists. */
867 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
868 if (!table) {
869 ret = -ENOMEM;
870 goto out;
871 }
872
873 ret = mutex_lock_interruptible(&xt[table->af].mutex);
874 if (ret != 0)
875 goto out_free;
876
877 /* Don't autoload: we'd eat our tail... */
878 list_for_each_entry(t, &net->xt.tables[table->af], list) {
879 if (strcmp(t->name, table->name) == 0) {
880 ret = -EEXIST;
881 goto unlock;
882 }
883 }
884
885 /* Simplifies replace_table code. */
886 table->private = bootstrap;
887
888 if (!xt_replace_table(table, 0, newinfo, &ret))
889 goto unlock;
890
891 private = table->private;
892 pr_debug("table->private->number = %u\n", private->number);
893
894 /* save number of initial entries */
895 private->initial_entries = private->number;
896
897 list_add(&table->list, &net->xt.tables[table->af]);
898 mutex_unlock(&xt[table->af].mutex);
899 return table;
900
901 unlock:
902 mutex_unlock(&xt[table->af].mutex);
903 out_free:
904 kfree(table);
905 out:
906 return ERR_PTR(ret);
907 }
908 EXPORT_SYMBOL_GPL(xt_register_table);
909
910 void *xt_unregister_table(struct xt_table *table)
911 {
912 struct xt_table_info *private;
913
914 mutex_lock(&xt[table->af].mutex);
915 private = table->private;
916 list_del(&table->list);
917 mutex_unlock(&xt[table->af].mutex);
918 kfree(table);
919
920 return private;
921 }
922 EXPORT_SYMBOL_GPL(xt_unregister_table);
923
924 #ifdef CONFIG_PROC_FS
925 struct xt_names_priv {
926 struct seq_net_private p;
927 u_int8_t af;
928 };
929 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
930 {
931 struct xt_names_priv *priv = seq->private;
932 struct net *net = seq_file_net(seq);
933 u_int8_t af = priv->af;
934
935 mutex_lock(&xt[af].mutex);
936 return seq_list_start(&net->xt.tables[af], *pos);
937 }
938
939 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
940 {
941 struct xt_names_priv *priv = seq->private;
942 struct net *net = seq_file_net(seq);
943 u_int8_t af = priv->af;
944
945 return seq_list_next(v, &net->xt.tables[af], pos);
946 }
947
948 static void xt_table_seq_stop(struct seq_file *seq, void *v)
949 {
950 struct xt_names_priv *priv = seq->private;
951 u_int8_t af = priv->af;
952
953 mutex_unlock(&xt[af].mutex);
954 }
955
956 static int xt_table_seq_show(struct seq_file *seq, void *v)
957 {
958 struct xt_table *table = list_entry(v, struct xt_table, list);
959
960 if (strlen(table->name))
961 return seq_printf(seq, "%s\n", table->name);
962 else
963 return 0;
964 }
965
966 static const struct seq_operations xt_table_seq_ops = {
967 .start = xt_table_seq_start,
968 .next = xt_table_seq_next,
969 .stop = xt_table_seq_stop,
970 .show = xt_table_seq_show,
971 };
972
973 static int xt_table_open(struct inode *inode, struct file *file)
974 {
975 int ret;
976 struct xt_names_priv *priv;
977
978 ret = seq_open_net(inode, file, &xt_table_seq_ops,
979 sizeof(struct xt_names_priv));
980 if (!ret) {
981 priv = ((struct seq_file *)file->private_data)->private;
982 priv->af = (unsigned long)PDE(inode)->data;
983 }
984 return ret;
985 }
986
987 static const struct file_operations xt_table_ops = {
988 .owner = THIS_MODULE,
989 .open = xt_table_open,
990 .read = seq_read,
991 .llseek = seq_lseek,
992 .release = seq_release_net,
993 };
994
995 /*
996 * Traverse state for ip{,6}_{tables,matches} for helping crossing
997 * the multi-AF mutexes.
998 */
999 struct nf_mttg_trav {
1000 struct list_head *head, *curr;
1001 uint8_t class, nfproto;
1002 };
1003
1004 enum {
1005 MTTG_TRAV_INIT,
1006 MTTG_TRAV_NFP_UNSPEC,
1007 MTTG_TRAV_NFP_SPEC,
1008 MTTG_TRAV_DONE,
1009 };
1010
1011 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1012 bool is_target)
1013 {
1014 static const uint8_t next_class[] = {
1015 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1016 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1017 };
1018 struct nf_mttg_trav *trav = seq->private;
1019
1020 switch (trav->class) {
1021 case MTTG_TRAV_INIT:
1022 trav->class = MTTG_TRAV_NFP_UNSPEC;
1023 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1024 trav->head = trav->curr = is_target ?
1025 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1026 break;
1027 case MTTG_TRAV_NFP_UNSPEC:
1028 trav->curr = trav->curr->next;
1029 if (trav->curr != trav->head)
1030 break;
1031 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1032 mutex_lock(&xt[trav->nfproto].mutex);
1033 trav->head = trav->curr = is_target ?
1034 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1035 trav->class = next_class[trav->class];
1036 break;
1037 case MTTG_TRAV_NFP_SPEC:
1038 trav->curr = trav->curr->next;
1039 if (trav->curr != trav->head)
1040 break;
1041 /* fallthru, _stop will unlock */
1042 default:
1043 return NULL;
1044 }
1045
1046 if (ppos != NULL)
1047 ++*ppos;
1048 return trav;
1049 }
1050
1051 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1052 bool is_target)
1053 {
1054 struct nf_mttg_trav *trav = seq->private;
1055 unsigned int j;
1056
1057 trav->class = MTTG_TRAV_INIT;
1058 for (j = 0; j < *pos; ++j)
1059 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1060 return NULL;
1061 return trav;
1062 }
1063
1064 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1065 {
1066 struct nf_mttg_trav *trav = seq->private;
1067
1068 switch (trav->class) {
1069 case MTTG_TRAV_NFP_UNSPEC:
1070 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1071 break;
1072 case MTTG_TRAV_NFP_SPEC:
1073 mutex_unlock(&xt[trav->nfproto].mutex);
1074 break;
1075 }
1076 }
1077
1078 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1079 {
1080 return xt_mttg_seq_start(seq, pos, false);
1081 }
1082
1083 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1084 {
1085 return xt_mttg_seq_next(seq, v, ppos, false);
1086 }
1087
1088 static int xt_match_seq_show(struct seq_file *seq, void *v)
1089 {
1090 const struct nf_mttg_trav *trav = seq->private;
1091 const struct xt_match *match;
1092
1093 switch (trav->class) {
1094 case MTTG_TRAV_NFP_UNSPEC:
1095 case MTTG_TRAV_NFP_SPEC:
1096 if (trav->curr == trav->head)
1097 return 0;
1098 match = list_entry(trav->curr, struct xt_match, list);
1099 return (*match->name == '\0') ? 0 :
1100 seq_printf(seq, "%s\n", match->name);
1101 }
1102 return 0;
1103 }
1104
1105 static const struct seq_operations xt_match_seq_ops = {
1106 .start = xt_match_seq_start,
1107 .next = xt_match_seq_next,
1108 .stop = xt_mttg_seq_stop,
1109 .show = xt_match_seq_show,
1110 };
1111
1112 static int xt_match_open(struct inode *inode, struct file *file)
1113 {
1114 struct seq_file *seq;
1115 struct nf_mttg_trav *trav;
1116 int ret;
1117
1118 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1119 if (trav == NULL)
1120 return -ENOMEM;
1121
1122 ret = seq_open(file, &xt_match_seq_ops);
1123 if (ret < 0) {
1124 kfree(trav);
1125 return ret;
1126 }
1127
1128 seq = file->private_data;
1129 seq->private = trav;
1130 trav->nfproto = (unsigned long)PDE(inode)->data;
1131 return 0;
1132 }
1133
1134 static const struct file_operations xt_match_ops = {
1135 .owner = THIS_MODULE,
1136 .open = xt_match_open,
1137 .read = seq_read,
1138 .llseek = seq_lseek,
1139 .release = seq_release_private,
1140 };
1141
1142 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1143 {
1144 return xt_mttg_seq_start(seq, pos, true);
1145 }
1146
1147 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1148 {
1149 return xt_mttg_seq_next(seq, v, ppos, true);
1150 }
1151
1152 static int xt_target_seq_show(struct seq_file *seq, void *v)
1153 {
1154 const struct nf_mttg_trav *trav = seq->private;
1155 const struct xt_target *target;
1156
1157 switch (trav->class) {
1158 case MTTG_TRAV_NFP_UNSPEC:
1159 case MTTG_TRAV_NFP_SPEC:
1160 if (trav->curr == trav->head)
1161 return 0;
1162 target = list_entry(trav->curr, struct xt_target, list);
1163 return (*target->name == '\0') ? 0 :
1164 seq_printf(seq, "%s\n", target->name);
1165 }
1166 return 0;
1167 }
1168
1169 static const struct seq_operations xt_target_seq_ops = {
1170 .start = xt_target_seq_start,
1171 .next = xt_target_seq_next,
1172 .stop = xt_mttg_seq_stop,
1173 .show = xt_target_seq_show,
1174 };
1175
1176 static int xt_target_open(struct inode *inode, struct file *file)
1177 {
1178 struct seq_file *seq;
1179 struct nf_mttg_trav *trav;
1180 int ret;
1181
1182 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1183 if (trav == NULL)
1184 return -ENOMEM;
1185
1186 ret = seq_open(file, &xt_target_seq_ops);
1187 if (ret < 0) {
1188 kfree(trav);
1189 return ret;
1190 }
1191
1192 seq = file->private_data;
1193 seq->private = trav;
1194 trav->nfproto = (unsigned long)PDE(inode)->data;
1195 return 0;
1196 }
1197
1198 static const struct file_operations xt_target_ops = {
1199 .owner = THIS_MODULE,
1200 .open = xt_target_open,
1201 .read = seq_read,
1202 .llseek = seq_lseek,
1203 .release = seq_release_private,
1204 };
1205
1206 #define FORMAT_TABLES "_tables_names"
1207 #define FORMAT_MATCHES "_tables_matches"
1208 #define FORMAT_TARGETS "_tables_targets"
1209
1210 #endif /* CONFIG_PROC_FS */
1211
1212 /**
1213 * xt_hook_link - set up hooks for a new table
1214 * @table: table with metadata needed to set up hooks
1215 * @fn: Hook function
1216 *
1217 * This function will take care of creating and registering the necessary
1218 * Netfilter hooks for XT tables.
1219 */
1220 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1221 {
1222 unsigned int hook_mask = table->valid_hooks;
1223 uint8_t i, num_hooks = hweight32(hook_mask);
1224 uint8_t hooknum;
1225 struct nf_hook_ops *ops;
1226 int ret;
1227
1228 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1229 if (ops == NULL)
1230 return ERR_PTR(-ENOMEM);
1231
1232 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1233 hook_mask >>= 1, ++hooknum) {
1234 if (!(hook_mask & 1))
1235 continue;
1236 ops[i].hook = fn;
1237 ops[i].owner = table->me;
1238 ops[i].pf = table->af;
1239 ops[i].hooknum = hooknum;
1240 ops[i].priority = table->priority;
1241 ++i;
1242 }
1243
1244 ret = nf_register_hooks(ops, num_hooks);
1245 if (ret < 0) {
1246 kfree(ops);
1247 return ERR_PTR(ret);
1248 }
1249
1250 return ops;
1251 }
1252 EXPORT_SYMBOL_GPL(xt_hook_link);
1253
1254 /**
1255 * xt_hook_unlink - remove hooks for a table
1256 * @ops: nf_hook_ops array as returned by nf_hook_link
1257 * @hook_mask: the very same mask that was passed to nf_hook_link
1258 */
1259 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1260 {
1261 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1262 kfree(ops);
1263 }
1264 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1265
1266 int xt_proto_init(struct net *net, u_int8_t af)
1267 {
1268 #ifdef CONFIG_PROC_FS
1269 char buf[XT_FUNCTION_MAXNAMELEN];
1270 struct proc_dir_entry *proc;
1271 #endif
1272
1273 if (af >= ARRAY_SIZE(xt_prefix))
1274 return -EINVAL;
1275
1276
1277 #ifdef CONFIG_PROC_FS
1278 strlcpy(buf, xt_prefix[af], sizeof(buf));
1279 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1280 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1281 (void *)(unsigned long)af);
1282 if (!proc)
1283 goto out;
1284
1285 strlcpy(buf, xt_prefix[af], sizeof(buf));
1286 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1287 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1288 (void *)(unsigned long)af);
1289 if (!proc)
1290 goto out_remove_tables;
1291
1292 strlcpy(buf, xt_prefix[af], sizeof(buf));
1293 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1294 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1295 (void *)(unsigned long)af);
1296 if (!proc)
1297 goto out_remove_matches;
1298 #endif
1299
1300 return 0;
1301
1302 #ifdef CONFIG_PROC_FS
1303 out_remove_matches:
1304 strlcpy(buf, xt_prefix[af], sizeof(buf));
1305 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1306 proc_net_remove(net, buf);
1307
1308 out_remove_tables:
1309 strlcpy(buf, xt_prefix[af], sizeof(buf));
1310 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1311 proc_net_remove(net, buf);
1312 out:
1313 return -1;
1314 #endif
1315 }
1316 EXPORT_SYMBOL_GPL(xt_proto_init);
1317
1318 void xt_proto_fini(struct net *net, u_int8_t af)
1319 {
1320 #ifdef CONFIG_PROC_FS
1321 char buf[XT_FUNCTION_MAXNAMELEN];
1322
1323 strlcpy(buf, xt_prefix[af], sizeof(buf));
1324 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1325 proc_net_remove(net, buf);
1326
1327 strlcpy(buf, xt_prefix[af], sizeof(buf));
1328 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1329 proc_net_remove(net, buf);
1330
1331 strlcpy(buf, xt_prefix[af], sizeof(buf));
1332 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1333 proc_net_remove(net, buf);
1334 #endif /*CONFIG_PROC_FS*/
1335 }
1336 EXPORT_SYMBOL_GPL(xt_proto_fini);
1337
1338 static int __net_init xt_net_init(struct net *net)
1339 {
1340 int i;
1341
1342 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1343 INIT_LIST_HEAD(&net->xt.tables[i]);
1344 return 0;
1345 }
1346
1347 static struct pernet_operations xt_net_ops = {
1348 .init = xt_net_init,
1349 };
1350
1351 static int __init xt_init(void)
1352 {
1353 unsigned int i;
1354 int rv;
1355
1356 for_each_possible_cpu(i) {
1357 struct xt_info_lock *lock = &per_cpu(xt_info_locks, i);
1358
1359 seqlock_init(&lock->lock);
1360 lock->readers = 0;
1361 }
1362
1363 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1364 if (!xt)
1365 return -ENOMEM;
1366
1367 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1368 mutex_init(&xt[i].mutex);
1369 #ifdef CONFIG_COMPAT
1370 mutex_init(&xt[i].compat_mutex);
1371 xt[i].compat_tab = NULL;
1372 #endif
1373 INIT_LIST_HEAD(&xt[i].target);
1374 INIT_LIST_HEAD(&xt[i].match);
1375 }
1376 rv = register_pernet_subsys(&xt_net_ops);
1377 if (rv < 0)
1378 kfree(xt);
1379 return rv;
1380 }
1381
1382 static void __exit xt_fini(void)
1383 {
1384 unregister_pernet_subsys(&xt_net_ops);
1385 kfree(xt);
1386 }
1387
1388 module_init(xt_init);
1389 module_exit(xt_fini);
1390