nf_tables_inet.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2012-2014 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. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/ip.h>
  11. #include <linux/netfilter_ipv4.h>
  12. #include <linux/netfilter_ipv6.h>
  13. #include <net/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables_ipv4.h>
  15. #include <net/netfilter/nf_tables_ipv6.h>
  16. #include <net/ip.h>
  17. static void nft_inet_hook_ops_init(struct nf_hook_ops *ops, unsigned int n)
  18. {
  19. struct nft_af_info *afi;
  20. if (n == 1)
  21. afi = &nft_af_ipv4;
  22. else
  23. afi = &nft_af_ipv6;
  24. ops->pf = afi->family;
  25. if (afi->hooks[ops->hooknum])
  26. ops->hook = afi->hooks[ops->hooknum];
  27. }
  28. static struct nft_af_info nft_af_inet __read_mostly = {
  29. .family = NFPROTO_INET,
  30. .nhooks = NF_INET_NUMHOOKS,
  31. .owner = THIS_MODULE,
  32. .nops = 2,
  33. .hook_ops_init = nft_inet_hook_ops_init,
  34. };
  35. static int __net_init nf_tables_inet_init_net(struct net *net)
  36. {
  37. net->nft.inet = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
  38. if (net->nft.inet == NULL)
  39. return -ENOMEM;
  40. memcpy(net->nft.inet, &nft_af_inet, sizeof(nft_af_inet));
  41. if (nft_register_afinfo(net, net->nft.inet) < 0)
  42. goto err;
  43. return 0;
  44. err:
  45. kfree(net->nft.inet);
  46. return -ENOMEM;
  47. }
  48. static void __net_exit nf_tables_inet_exit_net(struct net *net)
  49. {
  50. nft_unregister_afinfo(net->nft.inet);
  51. kfree(net->nft.inet);
  52. }
  53. static struct pernet_operations nf_tables_inet_net_ops = {
  54. .init = nf_tables_inet_init_net,
  55. .exit = nf_tables_inet_exit_net,
  56. };
  57. static const struct nf_chain_type filter_inet = {
  58. .name = "filter",
  59. .type = NFT_CHAIN_T_DEFAULT,
  60. .family = NFPROTO_INET,
  61. .owner = THIS_MODULE,
  62. .hook_mask = (1 << NF_INET_LOCAL_IN) |
  63. (1 << NF_INET_LOCAL_OUT) |
  64. (1 << NF_INET_FORWARD) |
  65. (1 << NF_INET_PRE_ROUTING) |
  66. (1 << NF_INET_POST_ROUTING),
  67. };
  68. static int __init nf_tables_inet_init(void)
  69. {
  70. int ret;
  71. nft_register_chain_type(&filter_inet);
  72. ret = register_pernet_subsys(&nf_tables_inet_net_ops);
  73. if (ret < 0)
  74. nft_unregister_chain_type(&filter_inet);
  75. return ret;
  76. }
  77. static void __exit nf_tables_inet_exit(void)
  78. {
  79. unregister_pernet_subsys(&nf_tables_inet_net_ops);
  80. nft_unregister_chain_type(&filter_inet);
  81. }
  82. module_init(nf_tables_inet_init);
  83. module_exit(nf_tables_inet_exit);
  84. MODULE_LICENSE("GPL");
  85. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  86. MODULE_ALIAS_NFT_FAMILY(1);