smack_netfilter.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Simplified MAC Kernel (smack) security module
  3. *
  4. * This file contains the Smack netfilter implementation
  5. *
  6. * Author:
  7. * Casey Schaufler <casey@schaufler-ca.com>
  8. *
  9. * Copyright (C) 2014 Casey Schaufler <casey@schaufler-ca.com>
  10. * Copyright (C) 2014 Intel Corporation.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2,
  14. * as published by the Free Software Foundation.
  15. */
  16. #include <linux/netfilter_ipv4.h>
  17. #include <linux/netfilter_ipv6.h>
  18. #include <linux/netdevice.h>
  19. #include <net/inet_sock.h>
  20. #include "smack.h"
  21. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  22. static unsigned int smack_ipv6_output(void *priv,
  23. struct sk_buff *skb,
  24. const struct nf_hook_state *state)
  25. {
  26. struct sock *sk = skb_to_full_sk(skb);
  27. struct socket_smack *ssp;
  28. struct smack_known *skp;
  29. if (sk && sk->sk_security) {
  30. ssp = sk->sk_security;
  31. skp = ssp->smk_out;
  32. skb->secmark = skp->smk_secid;
  33. }
  34. return NF_ACCEPT;
  35. }
  36. #endif /* IPV6 */
  37. static unsigned int smack_ipv4_output(void *priv,
  38. struct sk_buff *skb,
  39. const struct nf_hook_state *state)
  40. {
  41. struct sock *sk = skb_to_full_sk(skb);
  42. struct socket_smack *ssp;
  43. struct smack_known *skp;
  44. if (sk && sk->sk_security) {
  45. ssp = sk->sk_security;
  46. skp = ssp->smk_out;
  47. skb->secmark = skp->smk_secid;
  48. }
  49. return NF_ACCEPT;
  50. }
  51. static struct nf_hook_ops smack_nf_ops[] = {
  52. {
  53. .hook = smack_ipv4_output,
  54. .pf = NFPROTO_IPV4,
  55. .hooknum = NF_INET_LOCAL_OUT,
  56. .priority = NF_IP_PRI_SELINUX_FIRST,
  57. },
  58. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  59. {
  60. .hook = smack_ipv6_output,
  61. .pf = NFPROTO_IPV6,
  62. .hooknum = NF_INET_LOCAL_OUT,
  63. .priority = NF_IP6_PRI_SELINUX_FIRST,
  64. },
  65. #endif /* IPV6 */
  66. };
  67. static int __init smack_nf_ip_init(void)
  68. {
  69. int err;
  70. if (smack_enabled == 0)
  71. return 0;
  72. printk(KERN_DEBUG "Smack: Registering netfilter hooks\n");
  73. err = nf_register_hooks(smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
  74. if (err)
  75. pr_info("Smack: nf_register_hooks: error %d\n", err);
  76. return 0;
  77. }
  78. __initcall(smack_nf_ip_init);