PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv6/netfilter/ip6table_filter.c

https://bitbucket.org/androidarmv6/android_kernel_huawei_msm7x25
C | 177 lines | 139 code | 22 blank | 16 comment | 8 complexity | f70101add1cf8302925f35827daf1cc4 MD5 | raw file
  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. MODULE_LICENSE("GPL");
  15. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  16. MODULE_DESCRIPTION("ip6tables filter table");
  17. #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
  18. (1 << NF_INET_FORWARD) | \
  19. (1 << NF_INET_LOCAL_OUT))
  20. static struct
  21. {
  22. struct ip6t_replace repl;
  23. struct ip6t_standard entries[3];
  24. struct ip6t_error term;
  25. } initial_table __net_initdata = {
  26. .repl = {
  27. .name = "filter",
  28. .valid_hooks = FILTER_VALID_HOOKS,
  29. .num_entries = 4,
  30. .size = sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error),
  31. .hook_entry = {
  32. [NF_INET_LOCAL_IN] = 0,
  33. [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
  34. [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
  35. },
  36. .underflow = {
  37. [NF_INET_LOCAL_IN] = 0,
  38. [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
  39. [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
  40. },
  41. },
  42. .entries = {
  43. IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
  44. IP6T_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
  45. IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
  46. },
  47. .term = IP6T_ERROR_INIT, /* ERROR */
  48. };
  49. static const struct xt_table packet_filter = {
  50. .name = "filter",
  51. .valid_hooks = FILTER_VALID_HOOKS,
  52. .me = THIS_MODULE,
  53. .af = NFPROTO_IPV6,
  54. };
  55. /* The work comes in here from netfilter.c. */
  56. static unsigned int
  57. ip6t_in_hook(unsigned int hook,
  58. struct sk_buff *skb,
  59. const struct net_device *in,
  60. const struct net_device *out,
  61. int (*okfn)(struct sk_buff *))
  62. {
  63. return ip6t_do_table(skb, hook, in, out,
  64. dev_net(in)->ipv6.ip6table_filter);
  65. }
  66. static unsigned int
  67. ip6t_local_out_hook(unsigned int hook,
  68. struct sk_buff *skb,
  69. const struct net_device *in,
  70. const struct net_device *out,
  71. int (*okfn)(struct sk_buff *))
  72. {
  73. #if 0
  74. /* root is playing with raw sockets. */
  75. if (skb->len < sizeof(struct iphdr)
  76. || ip_hdrlen(skb) < sizeof(struct iphdr)) {
  77. if (net_ratelimit())
  78. printk("ip6t_hook: happy cracking.\n");
  79. return NF_ACCEPT;
  80. }
  81. #endif
  82. return ip6t_do_table(skb, hook, in, out,
  83. dev_net(out)->ipv6.ip6table_filter);
  84. }
  85. static struct nf_hook_ops ip6t_ops[] __read_mostly = {
  86. {
  87. .hook = ip6t_in_hook,
  88. .owner = THIS_MODULE,
  89. .pf = NFPROTO_IPV6,
  90. .hooknum = NF_INET_LOCAL_IN,
  91. .priority = NF_IP6_PRI_FILTER,
  92. },
  93. {
  94. .hook = ip6t_in_hook,
  95. .owner = THIS_MODULE,
  96. .pf = NFPROTO_IPV6,
  97. .hooknum = NF_INET_FORWARD,
  98. .priority = NF_IP6_PRI_FILTER,
  99. },
  100. {
  101. .hook = ip6t_local_out_hook,
  102. .owner = THIS_MODULE,
  103. .pf = NFPROTO_IPV6,
  104. .hooknum = NF_INET_LOCAL_OUT,
  105. .priority = NF_IP6_PRI_FILTER,
  106. },
  107. };
  108. /* Default to forward because I got too much mail already. */
  109. static int forward = NF_ACCEPT;
  110. module_param(forward, bool, 0000);
  111. static int __net_init ip6table_filter_net_init(struct net *net)
  112. {
  113. /* Register table */
  114. net->ipv6.ip6table_filter =
  115. ip6t_register_table(net, &packet_filter, &initial_table.repl);
  116. if (IS_ERR(net->ipv6.ip6table_filter))
  117. return PTR_ERR(net->ipv6.ip6table_filter);
  118. return 0;
  119. }
  120. static void __net_exit ip6table_filter_net_exit(struct net *net)
  121. {
  122. ip6t_unregister_table(net->ipv6.ip6table_filter);
  123. }
  124. static struct pernet_operations ip6table_filter_net_ops = {
  125. .init = ip6table_filter_net_init,
  126. .exit = ip6table_filter_net_exit,
  127. };
  128. static int __init ip6table_filter_init(void)
  129. {
  130. int ret;
  131. if (forward < 0 || forward > NF_MAX_VERDICT) {
  132. printk("iptables forward must be 0 or 1\n");
  133. return -EINVAL;
  134. }
  135. /* Entry 1 is the FORWARD hook */
  136. initial_table.entries[1].target.verdict = -forward - 1;
  137. ret = register_pernet_subsys(&ip6table_filter_net_ops);
  138. if (ret < 0)
  139. return ret;
  140. /* Register hooks */
  141. ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
  142. if (ret < 0)
  143. goto cleanup_table;
  144. return ret;
  145. cleanup_table:
  146. unregister_pernet_subsys(&ip6table_filter_net_ops);
  147. return ret;
  148. }
  149. static void __exit ip6table_filter_fini(void)
  150. {
  151. nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
  152. unregister_pernet_subsys(&ip6table_filter_net_ops);
  153. }
  154. module_init(ip6table_filter_init);
  155. module_exit(ip6table_filter_fini);