blob: 6d1706c9777e64fc69ca305e6ef55a2d563bb29d [file] [log] [blame]
Casey Schaufler69f287a2014-12-12 17:08:40 -08001/*
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
17#include <linux/netfilter_ipv4.h>
18#include <linux/netfilter_ipv6.h>
19#include <linux/netdevice.h>
20#include "smack.h"
21
22#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
23
Eric W. Biederman06198b32015-09-18 14:33:06 -050024static unsigned int smack_ipv6_output(void *priv,
Casey Schaufler69f287a2014-12-12 17:08:40 -080025 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040026 const struct nf_hook_state *state)
Casey Schaufler69f287a2014-12-12 17:08:40 -080027{
28 struct socket_smack *ssp;
29 struct smack_known *skp;
30
31 if (skb && skb->sk && skb->sk->sk_security) {
32 ssp = skb->sk->sk_security;
33 skp = ssp->smk_out;
34 skb->secmark = skp->smk_secid;
35 }
36
37 return NF_ACCEPT;
38}
39#endif /* IPV6 */
40
Eric W. Biederman06198b32015-09-18 14:33:06 -050041static unsigned int smack_ipv4_output(void *priv,
Casey Schaufler69f287a2014-12-12 17:08:40 -080042 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040043 const struct nf_hook_state *state)
Casey Schaufler69f287a2014-12-12 17:08:40 -080044{
45 struct socket_smack *ssp;
46 struct smack_known *skp;
47
48 if (skb && skb->sk && skb->sk->sk_security) {
49 ssp = skb->sk->sk_security;
50 skp = ssp->smk_out;
51 skb->secmark = skp->smk_secid;
52 }
53
54 return NF_ACCEPT;
55}
56
57static struct nf_hook_ops smack_nf_ops[] = {
58 {
59 .hook = smack_ipv4_output,
Casey Schaufler69f287a2014-12-12 17:08:40 -080060 .pf = NFPROTO_IPV4,
61 .hooknum = NF_INET_LOCAL_OUT,
62 .priority = NF_IP_PRI_SELINUX_FIRST,
63 },
64#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
65 {
66 .hook = smack_ipv6_output,
Casey Schaufler69f287a2014-12-12 17:08:40 -080067 .pf = NFPROTO_IPV6,
68 .hooknum = NF_INET_LOCAL_OUT,
69 .priority = NF_IP6_PRI_SELINUX_FIRST,
70 },
71#endif /* IPV6 */
72};
73
74static int __init smack_nf_ip_init(void)
75{
76 int err;
77
78 if (smack_enabled == 0)
79 return 0;
80
81 printk(KERN_DEBUG "Smack: Registering netfilter hooks\n");
82
83 err = nf_register_hooks(smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
84 if (err)
85 pr_info("Smack: nf_register_hooks: error %d\n", err);
86
87 return 0;
88}
89
90__initcall(smack_nf_ip_init);