iptable_security.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * "security" table
  4. *
  5. * This is for use by Mandatory Access Control (MAC) security models,
  6. * which need to be able to manage security policy in separate context
  7. * to DAC.
  8. *
  9. * Based on iptable_mangle.c
  10. *
  11. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  12. * Copyright (C) 2000-2004 Netfilter Core Team <coreteam <at> netfilter.org>
  13. * Copyright (C) 2008 Red Hat, Inc., James Morris <jmorris <at> redhat.com>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/netfilter_ipv4/ip_tables.h>
  17. #include <linux/slab.h>
  18. #include <net/ip.h>
  19. MODULE_LICENSE("GPL");
  20. MODULE_AUTHOR("James Morris <jmorris <at> redhat.com>");
  21. MODULE_DESCRIPTION("iptables security table, for MAC rules");
  22. #define SECURITY_VALID_HOOKS (1 << NF_INET_LOCAL_IN) | \
  23. (1 << NF_INET_FORWARD) | \
  24. (1 << NF_INET_LOCAL_OUT)
  25. static int __net_init iptable_security_table_init(struct net *net);
  26. static const struct xt_table security_table = {
  27. .name = "security",
  28. .valid_hooks = SECURITY_VALID_HOOKS,
  29. .me = THIS_MODULE,
  30. .af = NFPROTO_IPV4,
  31. .priority = NF_IP_PRI_SECURITY,
  32. .table_init = iptable_security_table_init,
  33. };
  34. static unsigned int
  35. iptable_security_hook(void *priv, struct sk_buff *skb,
  36. const struct nf_hook_state *state)
  37. {
  38. return ipt_do_table(skb, state, state->net->ipv4.iptable_security);
  39. }
  40. static struct nf_hook_ops *sectbl_ops __read_mostly;
  41. static int __net_init iptable_security_table_init(struct net *net)
  42. {
  43. struct ipt_replace *repl;
  44. int ret;
  45. if (net->ipv4.iptable_security)
  46. return 0;
  47. repl = ipt_alloc_initial_table(&security_table);
  48. if (repl == NULL)
  49. return -ENOMEM;
  50. ret = ipt_register_table(net, &security_table, repl, sectbl_ops,
  51. &net->ipv4.iptable_security);
  52. kfree(repl);
  53. return ret;
  54. }
  55. static void __net_exit iptable_security_net_pre_exit(struct net *net)
  56. {
  57. if (net->ipv4.iptable_security)
  58. ipt_unregister_table_pre_exit(net, net->ipv4.iptable_security,
  59. sectbl_ops);
  60. }
  61. static void __net_exit iptable_security_net_exit(struct net *net)
  62. {
  63. if (!net->ipv4.iptable_security)
  64. return;
  65. ipt_unregister_table_exit(net, net->ipv4.iptable_security);
  66. net->ipv4.iptable_security = NULL;
  67. }
  68. static struct pernet_operations iptable_security_net_ops = {
  69. .pre_exit = iptable_security_net_pre_exit,
  70. .exit = iptable_security_net_exit,
  71. };
  72. static int __init iptable_security_init(void)
  73. {
  74. int ret;
  75. sectbl_ops = xt_hook_ops_alloc(&security_table, iptable_security_hook);
  76. if (IS_ERR(sectbl_ops))
  77. return PTR_ERR(sectbl_ops);
  78. ret = register_pernet_subsys(&iptable_security_net_ops);
  79. if (ret < 0) {
  80. kfree(sectbl_ops);
  81. return ret;
  82. }
  83. ret = iptable_security_table_init(&init_net);
  84. if (ret) {
  85. unregister_pernet_subsys(&iptable_security_net_ops);
  86. kfree(sectbl_ops);
  87. }
  88. return ret;
  89. }
  90. static void __exit iptable_security_fini(void)
  91. {
  92. unregister_pernet_subsys(&iptable_security_net_ops);
  93. kfree(sectbl_ops);
  94. }
  95. module_init(iptable_security_init);
  96. module_exit(iptable_security_fini);