2 * Simplified MAC Kernel (smack) security module
4 * This file contains the Smack netfilter implementation
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2014 Casey Schaufler <casey@schaufler-ca.com>
10 * Copyright (C) 2014 Intel Corporation.
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.
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/netfilter_ipv6.h>
19 #include <linux/netdevice.h>
20 #include <net/inet_sock.h>
23 #if IS_ENABLED(CONFIG_IPV6)
25 static unsigned int smack_ipv6_output(void *priv
,
27 const struct nf_hook_state
*state
)
29 struct sock
*sk
= skb_to_full_sk(skb
);
30 struct socket_smack
*ssp
;
31 struct smack_known
*skp
;
33 if (sk
&& sk
->sk_security
) {
34 ssp
= sk
->sk_security
;
36 skb
->secmark
= skp
->smk_secid
;
43 static unsigned int smack_ipv4_output(void *priv
,
45 const struct nf_hook_state
*state
)
47 struct sock
*sk
= skb_to_full_sk(skb
);
48 struct socket_smack
*ssp
;
49 struct smack_known
*skp
;
51 if (sk
&& sk
->sk_security
) {
52 ssp
= sk
->sk_security
;
54 skb
->secmark
= skp
->smk_secid
;
60 static struct nf_hook_ops smack_nf_ops
[] = {
62 .hook
= smack_ipv4_output
,
64 .hooknum
= NF_INET_LOCAL_OUT
,
65 .priority
= NF_IP_PRI_SELINUX_FIRST
,
67 #if IS_ENABLED(CONFIG_IPV6)
69 .hook
= smack_ipv6_output
,
71 .hooknum
= NF_INET_LOCAL_OUT
,
72 .priority
= NF_IP6_PRI_SELINUX_FIRST
,
77 static int __init
smack_nf_ip_init(void)
81 if (smack_enabled
== 0)
84 printk(KERN_DEBUG
"Smack: Registering netfilter hooks\n");
86 err
= nf_register_hooks(smack_nf_ops
, ARRAY_SIZE(smack_nf_ops
));
88 pr_info("Smack: nf_register_hooks: error %d\n", err
);
93 __initcall(smack_nf_ip_init
);