objtool: Refactor sibling call detection logic
[linux-2.6-block.git] / security / smack / smack_netfilter.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
69f287ae
CS
2/*
3 * Simplified MAC Kernel (smack) security module
4 *
5 * This file contains the Smack netfilter implementation
6 *
7 * Author:
8 * Casey Schaufler <casey@schaufler-ca.com>
9 *
10 * Copyright (C) 2014 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2014 Intel Corporation.
69f287ae
CS
12 */
13
14#include <linux/netfilter_ipv4.h>
15#include <linux/netfilter_ipv6.h>
16#include <linux/netdevice.h>
8827d90e 17#include <net/inet_sock.h>
e661a582 18#include <net/net_namespace.h>
69f287ae
CS
19#include "smack.h"
20
1a93a6ea 21#if IS_ENABLED(CONFIG_IPV6)
69f287ae 22
06198b34 23static unsigned int smack_ipv6_output(void *priv,
69f287ae 24 struct sk_buff *skb,
238e54c9 25 const struct nf_hook_state *state)
69f287ae 26{
8827d90e 27 struct sock *sk = skb_to_full_sk(skb);
69f287ae
CS
28 struct socket_smack *ssp;
29 struct smack_known *skp;
30
8827d90e
ED
31 if (sk && sk->sk_security) {
32 ssp = sk->sk_security;
69f287ae
CS
33 skp = ssp->smk_out;
34 skb->secmark = skp->smk_secid;
35 }
36
37 return NF_ACCEPT;
38}
39#endif /* IPV6 */
40
06198b34 41static unsigned int smack_ipv4_output(void *priv,
69f287ae 42 struct sk_buff *skb,
238e54c9 43 const struct nf_hook_state *state)
69f287ae 44{
8827d90e 45 struct sock *sk = skb_to_full_sk(skb);
69f287ae
CS
46 struct socket_smack *ssp;
47 struct smack_known *skp;
48
8827d90e
ED
49 if (sk && sk->sk_security) {
50 ssp = sk->sk_security;
69f287ae
CS
51 skp = ssp->smk_out;
52 skb->secmark = skp->smk_secid;
53 }
54
55 return NF_ACCEPT;
56}
57
591bb278 58static const struct nf_hook_ops smack_nf_ops[] = {
69f287ae
CS
59 {
60 .hook = smack_ipv4_output,
69f287ae
CS
61 .pf = NFPROTO_IPV4,
62 .hooknum = NF_INET_LOCAL_OUT,
63 .priority = NF_IP_PRI_SELINUX_FIRST,
64 },
1a93a6ea 65#if IS_ENABLED(CONFIG_IPV6)
69f287ae
CS
66 {
67 .hook = smack_ipv6_output,
69f287ae
CS
68 .pf = NFPROTO_IPV6,
69 .hooknum = NF_INET_LOCAL_OUT,
70 .priority = NF_IP6_PRI_SELINUX_FIRST,
71 },
72#endif /* IPV6 */
73};
74
e661a582
FW
75static int __net_init smack_nf_register(struct net *net)
76{
77 return nf_register_net_hooks(net, smack_nf_ops,
78 ARRAY_SIZE(smack_nf_ops));
79}
80
81static void __net_exit smack_nf_unregister(struct net *net)
69f287ae 82{
e661a582
FW
83 nf_unregister_net_hooks(net, smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
84}
69f287ae 85
e661a582
FW
86static struct pernet_operations smack_net_ops = {
87 .init = smack_nf_register,
88 .exit = smack_nf_unregister,
89};
90
91static int __init smack_nf_ip_init(void)
92{
69f287ae
CS
93 if (smack_enabled == 0)
94 return 0;
95
96 printk(KERN_DEBUG "Smack: Registering netfilter hooks\n");
e661a582 97 return register_pernet_subsys(&smack_net_ops);
69f287ae
CS
98}
99
100__initcall(smack_nf_ip_init);