nft_chain_nat_ipv4.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3. * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
  4. * Copyright (c) 2012 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/list.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/ip.h>
  17. #include <linux/netfilter.h>
  18. #include <linux/netfilter_ipv4.h>
  19. #include <linux/netfilter/nf_tables.h>
  20. #include <net/netfilter/nf_conntrack.h>
  21. #include <net/netfilter/nf_nat.h>
  22. #include <net/netfilter/nf_nat_core.h>
  23. #include <net/netfilter/nf_tables.h>
  24. #include <net/netfilter/nf_tables_ipv4.h>
  25. #include <net/netfilter/nf_nat_l3proto.h>
  26. #include <net/ip.h>
  27. static unsigned int nft_nat_do_chain(void *priv,
  28. struct sk_buff *skb,
  29. const struct nf_hook_state *state,
  30. struct nf_conn *ct)
  31. {
  32. struct nft_pktinfo pkt;
  33. nft_set_pktinfo_ipv4(&pkt, skb, state);
  34. return nft_do_chain(&pkt, priv);
  35. }
  36. static unsigned int nft_nat_ipv4_fn(void *priv,
  37. struct sk_buff *skb,
  38. const struct nf_hook_state *state)
  39. {
  40. return nf_nat_ipv4_fn(priv, skb, state, nft_nat_do_chain);
  41. }
  42. static unsigned int nft_nat_ipv4_in(void *priv,
  43. struct sk_buff *skb,
  44. const struct nf_hook_state *state)
  45. {
  46. return nf_nat_ipv4_in(priv, skb, state, nft_nat_do_chain);
  47. }
  48. static unsigned int nft_nat_ipv4_out(void *priv,
  49. struct sk_buff *skb,
  50. const struct nf_hook_state *state)
  51. {
  52. return nf_nat_ipv4_out(priv, skb, state, nft_nat_do_chain);
  53. }
  54. static unsigned int nft_nat_ipv4_local_fn(void *priv,
  55. struct sk_buff *skb,
  56. const struct nf_hook_state *state)
  57. {
  58. return nf_nat_ipv4_local_fn(priv, skb, state, nft_nat_do_chain);
  59. }
  60. static const struct nf_chain_type nft_chain_nat_ipv4 = {
  61. .name = "nat",
  62. .type = NFT_CHAIN_T_NAT,
  63. .family = NFPROTO_IPV4,
  64. .owner = THIS_MODULE,
  65. .hook_mask = (1 << NF_INET_PRE_ROUTING) |
  66. (1 << NF_INET_POST_ROUTING) |
  67. (1 << NF_INET_LOCAL_OUT) |
  68. (1 << NF_INET_LOCAL_IN),
  69. .hooks = {
  70. [NF_INET_PRE_ROUTING] = nft_nat_ipv4_in,
  71. [NF_INET_POST_ROUTING] = nft_nat_ipv4_out,
  72. [NF_INET_LOCAL_OUT] = nft_nat_ipv4_local_fn,
  73. [NF_INET_LOCAL_IN] = nft_nat_ipv4_fn,
  74. },
  75. };
  76. static int __init nft_chain_nat_init(void)
  77. {
  78. int err;
  79. err = nft_register_chain_type(&nft_chain_nat_ipv4);
  80. if (err < 0)
  81. return err;
  82. return 0;
  83. }
  84. static void __exit nft_chain_nat_exit(void)
  85. {
  86. nft_unregister_chain_type(&nft_chain_nat_ipv4);
  87. }
  88. module_init(nft_chain_nat_init);
  89. module_exit(nft_chain_nat_exit);
  90. MODULE_LICENSE("GPL");
  91. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  92. MODULE_ALIAS_NFT_CHAIN(AF_INET, "nat");