/net/ipv6/netfilter/ip6table_security.c

http://github.com/mirrors/linux · C · 105 lines · 74 code · 17 blank · 14 comment · 7 complexity · 0835af5aa73492f772191744bad2736b MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * "security" table for IPv6
  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_ipv6/ip6_tables.h>
  17. #include <linux/slab.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("James Morris <jmorris <at> redhat.com>");
  20. MODULE_DESCRIPTION("ip6tables security table, for MAC rules");
  21. #define SECURITY_VALID_HOOKS (1 << NF_INET_LOCAL_IN) | \
  22. (1 << NF_INET_FORWARD) | \
  23. (1 << NF_INET_LOCAL_OUT)
  24. static int __net_init ip6table_security_table_init(struct net *net);
  25. static const struct xt_table security_table = {
  26. .name = "security",
  27. .valid_hooks = SECURITY_VALID_HOOKS,
  28. .me = THIS_MODULE,
  29. .af = NFPROTO_IPV6,
  30. .priority = NF_IP6_PRI_SECURITY,
  31. .table_init = ip6table_security_table_init,
  32. };
  33. static unsigned int
  34. ip6table_security_hook(void *priv, struct sk_buff *skb,
  35. const struct nf_hook_state *state)
  36. {
  37. return ip6t_do_table(skb, state, state->net->ipv6.ip6table_security);
  38. }
  39. static struct nf_hook_ops *sectbl_ops __read_mostly;
  40. static int __net_init ip6table_security_table_init(struct net *net)
  41. {
  42. struct ip6t_replace *repl;
  43. int ret;
  44. if (net->ipv6.ip6table_security)
  45. return 0;
  46. repl = ip6t_alloc_initial_table(&security_table);
  47. if (repl == NULL)
  48. return -ENOMEM;
  49. ret = ip6t_register_table(net, &security_table, repl, sectbl_ops,
  50. &net->ipv6.ip6table_security);
  51. kfree(repl);
  52. return ret;
  53. }
  54. static void __net_exit ip6table_security_net_exit(struct net *net)
  55. {
  56. if (!net->ipv6.ip6table_security)
  57. return;
  58. ip6t_unregister_table(net, net->ipv6.ip6table_security, sectbl_ops);
  59. net->ipv6.ip6table_security = NULL;
  60. }
  61. static struct pernet_operations ip6table_security_net_ops = {
  62. .exit = ip6table_security_net_exit,
  63. };
  64. static int __init ip6table_security_init(void)
  65. {
  66. int ret;
  67. sectbl_ops = xt_hook_ops_alloc(&security_table, ip6table_security_hook);
  68. if (IS_ERR(sectbl_ops))
  69. return PTR_ERR(sectbl_ops);
  70. ret = register_pernet_subsys(&ip6table_security_net_ops);
  71. if (ret < 0) {
  72. kfree(sectbl_ops);
  73. return ret;
  74. }
  75. ret = ip6table_security_table_init(&init_net);
  76. if (ret) {
  77. unregister_pernet_subsys(&ip6table_security_net_ops);
  78. kfree(sectbl_ops);
  79. }
  80. return ret;
  81. }
  82. static void __exit ip6table_security_fini(void)
  83. {
  84. unregister_pernet_subsys(&ip6table_security_net_ops);
  85. kfree(sectbl_ops);
  86. }
  87. module_init(ip6table_security_init);
  88. module_exit(ip6table_security_fini);