ip6table_nat.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Based on Rusty Russell's IPv4 NAT code. Development of IPv6 NAT
  9. * funded by Astaro.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter_ipv6.h>
  14. #include <linux/netfilter_ipv6/ip6_tables.h>
  15. #include <linux/ipv6.h>
  16. #include <net/ipv6.h>
  17. #include <net/netfilter/nf_nat.h>
  18. #include <net/netfilter/nf_nat_core.h>
  19. #include <net/netfilter/nf_nat_l3proto.h>
  20. static const struct xt_table nf_nat_ipv6_table = {
  21. .name = "nat",
  22. .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
  23. (1 << NF_INET_POST_ROUTING) |
  24. (1 << NF_INET_LOCAL_OUT) |
  25. (1 << NF_INET_LOCAL_IN),
  26. .me = THIS_MODULE,
  27. .af = NFPROTO_IPV6,
  28. };
  29. static unsigned int ip6table_nat_do_chain(void *priv,
  30. struct sk_buff *skb,
  31. const struct nf_hook_state *state,
  32. struct nf_conn *ct)
  33. {
  34. return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
  35. }
  36. static unsigned int ip6table_nat_fn(void *priv,
  37. struct sk_buff *skb,
  38. const struct nf_hook_state *state)
  39. {
  40. return nf_nat_ipv6_fn(priv, skb, state, ip6table_nat_do_chain);
  41. }
  42. static unsigned int ip6table_nat_in(void *priv,
  43. struct sk_buff *skb,
  44. const struct nf_hook_state *state)
  45. {
  46. return nf_nat_ipv6_in(priv, skb, state, ip6table_nat_do_chain);
  47. }
  48. static unsigned int ip6table_nat_out(void *priv,
  49. struct sk_buff *skb,
  50. const struct nf_hook_state *state)
  51. {
  52. return nf_nat_ipv6_out(priv, skb, state, ip6table_nat_do_chain);
  53. }
  54. static unsigned int ip6table_nat_local_fn(void *priv,
  55. struct sk_buff *skb,
  56. const struct nf_hook_state *state)
  57. {
  58. return nf_nat_ipv6_local_fn(priv, skb, state, ip6table_nat_do_chain);
  59. }
  60. static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
  61. /* Before packet filtering, change destination */
  62. {
  63. .hook = ip6table_nat_in,
  64. .pf = NFPROTO_IPV6,
  65. .hooknum = NF_INET_PRE_ROUTING,
  66. .priority = NF_IP6_PRI_NAT_DST,
  67. },
  68. /* After packet filtering, change source */
  69. {
  70. .hook = ip6table_nat_out,
  71. .pf = NFPROTO_IPV6,
  72. .hooknum = NF_INET_POST_ROUTING,
  73. .priority = NF_IP6_PRI_NAT_SRC,
  74. },
  75. /* Before packet filtering, change destination */
  76. {
  77. .hook = ip6table_nat_local_fn,
  78. .pf = NFPROTO_IPV6,
  79. .hooknum = NF_INET_LOCAL_OUT,
  80. .priority = NF_IP6_PRI_NAT_DST,
  81. },
  82. /* After packet filtering, change source */
  83. {
  84. .hook = ip6table_nat_fn,
  85. .pf = NFPROTO_IPV6,
  86. .hooknum = NF_INET_LOCAL_IN,
  87. .priority = NF_IP6_PRI_NAT_SRC,
  88. },
  89. };
  90. static int __net_init ip6table_nat_net_init(struct net *net)
  91. {
  92. struct ip6t_replace *repl;
  93. repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
  94. if (repl == NULL)
  95. return -ENOMEM;
  96. net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl);
  97. kfree(repl);
  98. return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat);
  99. }
  100. static void __net_exit ip6table_nat_net_exit(struct net *net)
  101. {
  102. ip6t_unregister_table(net, net->ipv6.ip6table_nat);
  103. }
  104. static struct pernet_operations ip6table_nat_net_ops = {
  105. .init = ip6table_nat_net_init,
  106. .exit = ip6table_nat_net_exit,
  107. };
  108. static int __init ip6table_nat_init(void)
  109. {
  110. int err;
  111. err = register_pernet_subsys(&ip6table_nat_net_ops);
  112. if (err < 0)
  113. goto err1;
  114. err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
  115. if (err < 0)
  116. goto err2;
  117. return 0;
  118. err2:
  119. unregister_pernet_subsys(&ip6table_nat_net_ops);
  120. err1:
  121. return err;
  122. }
  123. static void __exit ip6table_nat_exit(void)
  124. {
  125. nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
  126. unregister_pernet_subsys(&ip6table_nat_net_ops);
  127. }
  128. module_init(ip6table_nat_init);
  129. module_exit(ip6table_nat_exit);
  130. MODULE_LICENSE("GPL");