iptable_security.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * "security" table
  3. *
  4. * This is for use by Mandatory Access Control (MAC) security models,
  5. * which need to be able to manage security policy in separate context
  6. * to DAC.
  7. *
  8. * Based on iptable_mangle.c
  9. *
  10. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  11. * Copyright (C) 2000-2004 Netfilter Core Team <coreteam <at> netfilter.org>
  12. * Copyright (C) 2008 Red Hat, Inc., James Morris <jmorris <at> redhat.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/netfilter_ipv4/ip_tables.h>
  20. #include <linux/slab.h>
  21. #include <net/ip.h>
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("James Morris <jmorris <at> redhat.com>");
  24. MODULE_DESCRIPTION("iptables security table, for MAC rules");
  25. #define SECURITY_VALID_HOOKS (1 << NF_INET_LOCAL_IN) | \
  26. (1 << NF_INET_FORWARD) | \
  27. (1 << NF_INET_LOCAL_OUT)
  28. static const struct xt_table security_table = {
  29. .name = "security",
  30. .valid_hooks = SECURITY_VALID_HOOKS,
  31. .me = THIS_MODULE,
  32. .af = NFPROTO_IPV4,
  33. .priority = NF_IP_PRI_SECURITY,
  34. };
  35. static unsigned int
  36. iptable_security_hook(void *priv, struct sk_buff *skb,
  37. const struct nf_hook_state *state)
  38. {
  39. if (state->hook == NF_INET_LOCAL_OUT &&
  40. (skb->len < sizeof(struct iphdr) ||
  41. ip_hdrlen(skb) < sizeof(struct iphdr)))
  42. /* Somebody is playing with raw sockets. */
  43. return NF_ACCEPT;
  44. return ipt_do_table(skb, state, state->net->ipv4.iptable_security);
  45. }
  46. static struct nf_hook_ops *sectbl_ops __read_mostly;
  47. static int __net_init iptable_security_net_init(struct net *net)
  48. {
  49. struct ipt_replace *repl;
  50. repl = ipt_alloc_initial_table(&security_table);
  51. if (repl == NULL)
  52. return -ENOMEM;
  53. net->ipv4.iptable_security =
  54. ipt_register_table(net, &security_table, repl);
  55. kfree(repl);
  56. return PTR_ERR_OR_ZERO(net->ipv4.iptable_security);
  57. }
  58. static void __net_exit iptable_security_net_exit(struct net *net)
  59. {
  60. ipt_unregister_table(net, net->ipv4.iptable_security);
  61. }
  62. static struct pernet_operations iptable_security_net_ops = {
  63. .init = iptable_security_net_init,
  64. .exit = iptable_security_net_exit,
  65. };
  66. static int __init iptable_security_init(void)
  67. {
  68. int ret;
  69. ret = register_pernet_subsys(&iptable_security_net_ops);
  70. if (ret < 0)
  71. return ret;
  72. sectbl_ops = xt_hook_link(&security_table, iptable_security_hook);
  73. if (IS_ERR(sectbl_ops)) {
  74. ret = PTR_ERR(sectbl_ops);
  75. goto cleanup_table;
  76. }
  77. return ret;
  78. cleanup_table:
  79. unregister_pernet_subsys(&iptable_security_net_ops);
  80. return ret;
  81. }
  82. static void __exit iptable_security_fini(void)
  83. {
  84. xt_hook_unlink(&security_table, sectbl_ops);
  85. unregister_pernet_subsys(&iptable_security_net_ops);
  86. }
  87. module_init(iptable_security_init);
  88. module_exit(iptable_security_fini);