nf_tables_ipv4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
  3. * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/ip.h>
  14. #include <linux/netfilter_ipv4.h>
  15. #include <net/netfilter/nf_tables.h>
  16. #include <net/net_namespace.h>
  17. #include <net/ip.h>
  18. #include <net/netfilter/nf_tables_ipv4.h>
  19. static unsigned int nft_do_chain_ipv4(void *priv,
  20. struct sk_buff *skb,
  21. const struct nf_hook_state *state)
  22. {
  23. struct nft_pktinfo pkt;
  24. nft_set_pktinfo_ipv4(&pkt, skb, state);
  25. return nft_do_chain(&pkt, priv);
  26. }
  27. static unsigned int nft_ipv4_output(void *priv,
  28. struct sk_buff *skb,
  29. const struct nf_hook_state *state)
  30. {
  31. if (unlikely(skb->len < sizeof(struct iphdr) ||
  32. ip_hdr(skb)->ihl < sizeof(struct iphdr) / 4)) {
  33. if (net_ratelimit())
  34. pr_info("nf_tables_ipv4: ignoring short SOCK_RAW "
  35. "packet\n");
  36. return NF_ACCEPT;
  37. }
  38. return nft_do_chain_ipv4(priv, skb, state);
  39. }
  40. struct nft_af_info nft_af_ipv4 __read_mostly = {
  41. .family = NFPROTO_IPV4,
  42. .nhooks = NF_INET_NUMHOOKS,
  43. .owner = THIS_MODULE,
  44. .nops = 1,
  45. .hooks = {
  46. [NF_INET_LOCAL_IN] = nft_do_chain_ipv4,
  47. [NF_INET_LOCAL_OUT] = nft_ipv4_output,
  48. [NF_INET_FORWARD] = nft_do_chain_ipv4,
  49. [NF_INET_PRE_ROUTING] = nft_do_chain_ipv4,
  50. [NF_INET_POST_ROUTING] = nft_do_chain_ipv4,
  51. },
  52. };
  53. EXPORT_SYMBOL_GPL(nft_af_ipv4);
  54. static int nf_tables_ipv4_init_net(struct net *net)
  55. {
  56. net->nft.ipv4 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
  57. if (net->nft.ipv4 == NULL)
  58. return -ENOMEM;
  59. memcpy(net->nft.ipv4, &nft_af_ipv4, sizeof(nft_af_ipv4));
  60. if (nft_register_afinfo(net, net->nft.ipv4) < 0)
  61. goto err;
  62. return 0;
  63. err:
  64. kfree(net->nft.ipv4);
  65. return -ENOMEM;
  66. }
  67. static void nf_tables_ipv4_exit_net(struct net *net)
  68. {
  69. nft_unregister_afinfo(net->nft.ipv4);
  70. kfree(net->nft.ipv4);
  71. }
  72. static struct pernet_operations nf_tables_ipv4_net_ops = {
  73. .init = nf_tables_ipv4_init_net,
  74. .exit = nf_tables_ipv4_exit_net,
  75. };
  76. static const struct nf_chain_type filter_ipv4 = {
  77. .name = "filter",
  78. .type = NFT_CHAIN_T_DEFAULT,
  79. .family = NFPROTO_IPV4,
  80. .owner = THIS_MODULE,
  81. .hook_mask = (1 << NF_INET_LOCAL_IN) |
  82. (1 << NF_INET_LOCAL_OUT) |
  83. (1 << NF_INET_FORWARD) |
  84. (1 << NF_INET_PRE_ROUTING) |
  85. (1 << NF_INET_POST_ROUTING),
  86. };
  87. static int __init nf_tables_ipv4_init(void)
  88. {
  89. int ret;
  90. nft_register_chain_type(&filter_ipv4);
  91. ret = register_pernet_subsys(&nf_tables_ipv4_net_ops);
  92. if (ret < 0)
  93. nft_unregister_chain_type(&filter_ipv4);
  94. return ret;
  95. }
  96. static void __exit nf_tables_ipv4_exit(void)
  97. {
  98. unregister_pernet_subsys(&nf_tables_ipv4_net_ops);
  99. nft_unregister_chain_type(&filter_ipv4);
  100. }
  101. module_init(nf_tables_ipv4_init);
  102. module_exit(nf_tables_ipv4_exit);
  103. MODULE_LICENSE("GPL");
  104. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  105. MODULE_ALIAS_NFT_FAMILY(AF_INET);