ip6table_filter.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
  3. *
  4. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  5. * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/netfilter_ipv6/ip6_tables.h>
  14. #include <linux/slab.h>
  15. MODULE_LICENSE("GPL");
  16. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  17. MODULE_DESCRIPTION("ip6tables filter table");
  18. #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
  19. (1 << NF_INET_FORWARD) | \
  20. (1 << NF_INET_LOCAL_OUT))
  21. static const struct xt_table packet_filter = {
  22. .name = "filter",
  23. .valid_hooks = FILTER_VALID_HOOKS,
  24. .me = THIS_MODULE,
  25. .af = NFPROTO_IPV6,
  26. .priority = NF_IP6_PRI_FILTER,
  27. };
  28. /* The work comes in here from netfilter.c. */
  29. static unsigned int
  30. ip6table_filter_hook(void *priv, struct sk_buff *skb,
  31. const struct nf_hook_state *state)
  32. {
  33. return ip6t_do_table(skb, state, state->net->ipv6.ip6table_filter);
  34. }
  35. static struct nf_hook_ops *filter_ops __read_mostly;
  36. /* Default to forward because I got too much mail already. */
  37. static bool forward = true;
  38. module_param(forward, bool, 0000);
  39. static int __net_init ip6table_filter_net_init(struct net *net)
  40. {
  41. struct ip6t_replace *repl;
  42. repl = ip6t_alloc_initial_table(&packet_filter);
  43. if (repl == NULL)
  44. return -ENOMEM;
  45. /* Entry 1 is the FORWARD hook */
  46. ((struct ip6t_standard *)repl->entries)[1].target.verdict =
  47. forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
  48. net->ipv6.ip6table_filter =
  49. ip6t_register_table(net, &packet_filter, repl);
  50. kfree(repl);
  51. return PTR_ERR_OR_ZERO(net->ipv6.ip6table_filter);
  52. }
  53. static void __net_exit ip6table_filter_net_exit(struct net *net)
  54. {
  55. ip6t_unregister_table(net, net->ipv6.ip6table_filter);
  56. }
  57. static struct pernet_operations ip6table_filter_net_ops = {
  58. .init = ip6table_filter_net_init,
  59. .exit = ip6table_filter_net_exit,
  60. };
  61. static int __init ip6table_filter_init(void)
  62. {
  63. int ret;
  64. ret = register_pernet_subsys(&ip6table_filter_net_ops);
  65. if (ret < 0)
  66. return ret;
  67. /* Register hooks */
  68. filter_ops = xt_hook_link(&packet_filter, ip6table_filter_hook);
  69. if (IS_ERR(filter_ops)) {
  70. ret = PTR_ERR(filter_ops);
  71. goto cleanup_table;
  72. }
  73. return ret;
  74. cleanup_table:
  75. unregister_pernet_subsys(&ip6table_filter_net_ops);
  76. return ret;
  77. }
  78. static void __exit ip6table_filter_fini(void)
  79. {
  80. xt_hook_unlink(&packet_filter, filter_ops);
  81. unregister_pernet_subsys(&ip6table_filter_net_ops);
  82. }
  83. module_init(ip6table_filter_init);
  84. module_exit(ip6table_filter_fini);