iptable_nat.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  4. * (C) 2011 Patrick McHardy <kaber@trash.net>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/netfilter.h>
  8. #include <linux/netfilter_ipv4.h>
  9. #include <linux/netfilter_ipv4/ip_tables.h>
  10. #include <linux/ip.h>
  11. #include <net/ip.h>
  12. #include <net/netfilter/nf_nat.h>
  13. static int __net_init iptable_nat_table_init(struct net *net);
  14. static const struct xt_table nf_nat_ipv4_table = {
  15. .name = "nat",
  16. .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
  17. (1 << NF_INET_POST_ROUTING) |
  18. (1 << NF_INET_LOCAL_OUT) |
  19. (1 << NF_INET_LOCAL_IN),
  20. .me = THIS_MODULE,
  21. .af = NFPROTO_IPV4,
  22. .table_init = iptable_nat_table_init,
  23. };
  24. static unsigned int iptable_nat_do_chain(void *priv,
  25. struct sk_buff *skb,
  26. const struct nf_hook_state *state)
  27. {
  28. return ipt_do_table(skb, state, state->net->ipv4.nat_table);
  29. }
  30. static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
  31. {
  32. .hook = iptable_nat_do_chain,
  33. .pf = NFPROTO_IPV4,
  34. .hooknum = NF_INET_PRE_ROUTING,
  35. .priority = NF_IP_PRI_NAT_DST,
  36. },
  37. {
  38. .hook = iptable_nat_do_chain,
  39. .pf = NFPROTO_IPV4,
  40. .hooknum = NF_INET_POST_ROUTING,
  41. .priority = NF_IP_PRI_NAT_SRC,
  42. },
  43. {
  44. .hook = iptable_nat_do_chain,
  45. .pf = NFPROTO_IPV4,
  46. .hooknum = NF_INET_LOCAL_OUT,
  47. .priority = NF_IP_PRI_NAT_DST,
  48. },
  49. {
  50. .hook = iptable_nat_do_chain,
  51. .pf = NFPROTO_IPV4,
  52. .hooknum = NF_INET_LOCAL_IN,
  53. .priority = NF_IP_PRI_NAT_SRC,
  54. },
  55. };
  56. static int ipt_nat_register_lookups(struct net *net)
  57. {
  58. int i, ret;
  59. for (i = 0; i < ARRAY_SIZE(nf_nat_ipv4_ops); i++) {
  60. ret = nf_nat_ipv4_register_fn(net, &nf_nat_ipv4_ops[i]);
  61. if (ret) {
  62. while (i)
  63. nf_nat_ipv4_unregister_fn(net, &nf_nat_ipv4_ops[--i]);
  64. return ret;
  65. }
  66. }
  67. return 0;
  68. }
  69. static void ipt_nat_unregister_lookups(struct net *net)
  70. {
  71. int i;
  72. for (i = 0; i < ARRAY_SIZE(nf_nat_ipv4_ops); i++)
  73. nf_nat_ipv4_unregister_fn(net, &nf_nat_ipv4_ops[i]);
  74. }
  75. static int __net_init iptable_nat_table_init(struct net *net)
  76. {
  77. struct ipt_replace *repl;
  78. int ret;
  79. if (net->ipv4.nat_table)
  80. return 0;
  81. repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
  82. if (repl == NULL)
  83. return -ENOMEM;
  84. ret = ipt_register_table(net, &nf_nat_ipv4_table, repl,
  85. NULL, &net->ipv4.nat_table);
  86. if (ret < 0) {
  87. kfree(repl);
  88. return ret;
  89. }
  90. ret = ipt_nat_register_lookups(net);
  91. if (ret < 0) {
  92. ipt_unregister_table(net, net->ipv4.nat_table, NULL);
  93. net->ipv4.nat_table = NULL;
  94. }
  95. kfree(repl);
  96. return ret;
  97. }
  98. static void __net_exit iptable_nat_net_pre_exit(struct net *net)
  99. {
  100. if (net->ipv4.nat_table)
  101. ipt_nat_unregister_lookups(net);
  102. }
  103. static void __net_exit iptable_nat_net_exit(struct net *net)
  104. {
  105. if (!net->ipv4.nat_table)
  106. return;
  107. ipt_unregister_table_exit(net, net->ipv4.nat_table);
  108. net->ipv4.nat_table = NULL;
  109. }
  110. static struct pernet_operations iptable_nat_net_ops = {
  111. .pre_exit = iptable_nat_net_pre_exit,
  112. .exit = iptable_nat_net_exit,
  113. };
  114. static int __init iptable_nat_init(void)
  115. {
  116. int ret = register_pernet_subsys(&iptable_nat_net_ops);
  117. if (ret)
  118. return ret;
  119. ret = iptable_nat_table_init(&init_net);
  120. if (ret)
  121. unregister_pernet_subsys(&iptable_nat_net_ops);
  122. return ret;
  123. }
  124. static void __exit iptable_nat_exit(void)
  125. {
  126. unregister_pernet_subsys(&iptable_nat_net_ops);
  127. }
  128. module_init(iptable_nat_init);
  129. module_exit(iptable_nat_exit);
  130. MODULE_LICENSE("GPL");