2 * Network interface table.
4 * Network interfaces (devices) do not have a security field, so we
5 * maintain a table associating each interface with a SID.
7 * Author: James Morris <jmorris@redhat.com>
9 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/rcupdate.h>
23 #include <net/net_namespace.h>
29 #define SEL_NETIF_HASH_SIZE 64
30 #define SEL_NETIF_HASH_MAX 1024
37 #define DEBUGP(format, args...)
42 struct list_head list
;
43 struct netif_security_struct nsec
;
44 struct rcu_head rcu_head
;
47 static u32 sel_netif_total
;
48 static LIST_HEAD(sel_netif_list
);
49 static DEFINE_SPINLOCK(sel_netif_lock
);
50 static struct list_head sel_netif_hash
[SEL_NETIF_HASH_SIZE
];
52 static inline u32
sel_netif_hasfn(struct net_device
*dev
)
54 return (dev
->ifindex
& (SEL_NETIF_HASH_SIZE
- 1));
58 * All of the devices should normally fit in the hash, so we optimize
61 static inline struct sel_netif
*sel_netif_find(struct net_device
*dev
)
63 struct list_head
*pos
;
64 int idx
= sel_netif_hasfn(dev
);
66 __list_for_each_rcu(pos
, &sel_netif_hash
[idx
]) {
67 struct sel_netif
*netif
= list_entry(pos
,
68 struct sel_netif
, list
);
69 if (likely(netif
->nsec
.dev
== dev
))
75 static int sel_netif_insert(struct sel_netif
*netif
)
79 if (sel_netif_total
>= SEL_NETIF_HASH_MAX
) {
84 idx
= sel_netif_hasfn(netif
->nsec
.dev
);
85 list_add_rcu(&netif
->list
, &sel_netif_hash
[idx
]);
91 static void sel_netif_free(struct rcu_head
*p
)
93 struct sel_netif
*netif
= container_of(p
, struct sel_netif
, rcu_head
);
95 DEBUGP("%s: %s\n", __FUNCTION__
, netif
->nsec
.dev
->name
);
99 static void sel_netif_destroy(struct sel_netif
*netif
)
101 DEBUGP("%s: %s\n", __FUNCTION__
, netif
->nsec
.dev
->name
);
103 list_del_rcu(&netif
->list
);
105 call_rcu(&netif
->rcu_head
, sel_netif_free
);
108 static struct sel_netif
*sel_netif_lookup(struct net_device
*dev
)
111 struct sel_netif
*netif
, *new;
112 struct netif_security_struct
*nsec
;
114 netif
= sel_netif_find(dev
);
115 if (likely(netif
!= NULL
))
118 new = kzalloc(sizeof(*new), GFP_ATOMIC
);
120 netif
= ERR_PTR(-ENOMEM
);
126 ret
= security_netif_sid(dev
->name
, &nsec
->if_sid
, &nsec
->msg_sid
);
129 netif
= ERR_PTR(ret
);
135 spin_lock_bh(&sel_netif_lock
);
137 netif
= sel_netif_find(dev
);
139 spin_unlock_bh(&sel_netif_lock
);
144 ret
= sel_netif_insert(new);
145 spin_unlock_bh(&sel_netif_lock
);
149 netif
= ERR_PTR(ret
);
155 DEBUGP("new: ifindex=%u name=%s if_sid=%u msg_sid=%u\n", dev
->ifindex
, dev
->name
,
156 nsec
->if_sid
, nsec
->msg_sid
);
161 static void sel_netif_assign_sids(u32 if_sid_in
, u32 msg_sid_in
, u32
*if_sid_out
, u32
*msg_sid_out
)
164 *if_sid_out
= if_sid_in
;
166 *msg_sid_out
= msg_sid_in
;
169 static int sel_netif_sids_slow(struct net_device
*dev
, u32
*if_sid
, u32
*msg_sid
)
172 u32 tmp_if_sid
, tmp_msg_sid
;
174 ret
= security_netif_sid(dev
->name
, &tmp_if_sid
, &tmp_msg_sid
);
176 sel_netif_assign_sids(tmp_if_sid
, tmp_msg_sid
, if_sid
, msg_sid
);
180 int sel_netif_sids(struct net_device
*dev
, u32
*if_sid
, u32
*msg_sid
)
183 struct sel_netif
*netif
;
186 netif
= sel_netif_lookup(dev
);
189 ret
= sel_netif_sids_slow(dev
, if_sid
, msg_sid
);
192 sel_netif_assign_sids(netif
->nsec
.if_sid
, netif
->nsec
.msg_sid
, if_sid
, msg_sid
);
198 static void sel_netif_kill(struct net_device
*dev
)
200 struct sel_netif
*netif
;
202 spin_lock_bh(&sel_netif_lock
);
203 netif
= sel_netif_find(dev
);
205 sel_netif_destroy(netif
);
206 spin_unlock_bh(&sel_netif_lock
);
209 static void sel_netif_flush(void)
213 spin_lock_bh(&sel_netif_lock
);
214 for (idx
= 0; idx
< SEL_NETIF_HASH_SIZE
; idx
++) {
215 struct sel_netif
*netif
;
217 list_for_each_entry(netif
, &sel_netif_hash
[idx
], list
)
218 sel_netif_destroy(netif
);
220 spin_unlock_bh(&sel_netif_lock
);
223 static int sel_netif_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
224 u16
class, u32 perms
, u32
*retained
)
226 if (event
== AVC_CALLBACK_RESET
) {
233 static int sel_netif_netdev_notifier_handler(struct notifier_block
*this,
234 unsigned long event
, void *ptr
)
236 struct net_device
*dev
= ptr
;
238 if (dev
->nd_net
!= &init_net
)
241 if (event
== NETDEV_DOWN
)
247 static struct notifier_block sel_netif_netdev_notifier
= {
248 .notifier_call
= sel_netif_netdev_notifier_handler
,
251 static __init
int sel_netif_init(void)
255 if (!selinux_enabled
)
258 for (i
= 0; i
< SEL_NETIF_HASH_SIZE
; i
++)
259 INIT_LIST_HEAD(&sel_netif_hash
[i
]);
261 register_netdevice_notifier(&sel_netif_netdev_notifier
);
263 err
= avc_add_callback(sel_netif_avc_callback
, AVC_CALLBACK_RESET
,
264 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
266 panic("avc_add_callback() failed, error %d\n", err
);
272 __initcall(sel_netif_init
);