xref: /freebsd/sys/security/mac/mac_net.c (revision e678cce9402932e815e26b29f6d43e9ae92ae138)
17bc82500SRobert Watson /*-
2f6a41092SRobert Watson  * Copyright (c) 1999-2002 Robert N. M. Watson
37bc82500SRobert Watson  * Copyright (c) 2001 Ilmar S. Habibulin
4c66b4d8dSRobert Watson  * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
57bc82500SRobert Watson  * All rights reserved.
67bc82500SRobert Watson  *
77bc82500SRobert Watson  * This software was developed by Robert Watson and Ilmar Habibulin for the
87bc82500SRobert Watson  * TrustedBSD Project.
97bc82500SRobert Watson  *
106201265bSRobert Watson  * This software was developed for the FreeBSD Project in part by Network
116201265bSRobert Watson  * Associates Laboratories, the Security Research Division of Network
126201265bSRobert Watson  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
136201265bSRobert Watson  * as part of the DARPA CHATS research program.
147bc82500SRobert Watson  *
157bc82500SRobert Watson  * Redistribution and use in source and binary forms, with or without
167bc82500SRobert Watson  * modification, are permitted provided that the following conditions
177bc82500SRobert Watson  * are met:
187bc82500SRobert Watson  * 1. Redistributions of source code must retain the above copyright
197bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer.
207bc82500SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
217bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
227bc82500SRobert Watson  *    documentation and/or other materials provided with the distribution.
237bc82500SRobert Watson  *
247bc82500SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
257bc82500SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
267bc82500SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277bc82500SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
287bc82500SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
297bc82500SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
307bc82500SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
317bc82500SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327bc82500SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
337bc82500SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
347bc82500SRobert Watson  * SUCH DAMAGE.
357bc82500SRobert Watson  */
36677b542eSDavid E. O'Brien 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
407bc82500SRobert Watson #include "opt_mac.h"
41f9d0d524SRobert Watson 
427bc82500SRobert Watson #include <sys/param.h>
4395fab37eSRobert Watson #include <sys/kernel.h>
4495fab37eSRobert Watson #include <sys/lock.h>
45b656366bSBruce Evans #include <sys/malloc.h>
4695fab37eSRobert Watson #include <sys/mutex.h>
4795fab37eSRobert Watson #include <sys/mac.h>
48acd3428bSRobert Watson #include <sys/priv.h>
49f51e5803SRobert Watson #include <sys/sbuf.h>
5095fab37eSRobert Watson #include <sys/systm.h>
5195fab37eSRobert Watson #include <sys/mount.h>
5295fab37eSRobert Watson #include <sys/file.h>
5395fab37eSRobert Watson #include <sys/namei.h>
54a557af22SRobert Watson #include <sys/protosw.h>
5595fab37eSRobert Watson #include <sys/socket.h>
5695fab37eSRobert Watson #include <sys/socketvar.h>
5795fab37eSRobert Watson #include <sys/sysctl.h>
5895fab37eSRobert Watson 
5995fab37eSRobert Watson #include <sys/mac_policy.h>
6095fab37eSRobert Watson 
6195fab37eSRobert Watson #include <net/bpfdesc.h>
6295fab37eSRobert Watson #include <net/if.h>
6395fab37eSRobert Watson #include <net/if_var.h>
6495fab37eSRobert Watson 
65aed55708SRobert Watson #include <security/mac/mac_framework.h>
6628e65e3dSRobert Watson #include <security/mac/mac_internal.h>
67a3df768bSRobert Watson 
68c66b4d8dSRobert Watson /*
69e678cce9SRobert Watson  * mac_enforce_network is used by IPv4 and IPv6 checks, and so must be
70e678cce9SRobert Watson  * non-static for now.
71c66b4d8dSRobert Watson  */
72c66b4d8dSRobert Watson int	mac_enforce_network = 1;
7395fab37eSRobert Watson SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
7495fab37eSRobert Watson     &mac_enforce_network, 0, "Enforce MAC policy on network packets");
7595fab37eSRobert Watson TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
7695fab37eSRobert Watson 
772220907bSRobert Watson /*
78e678cce9SRobert Watson  * XXXRW: struct ifnet locking is incomplete in the network code, so we use
79e678cce9SRobert Watson  * our own global mutex for struct ifnet.  Non-ideal, but should help in the
80e678cce9SRobert Watson  * SMP environment.
812220907bSRobert Watson  */
822220907bSRobert Watson static struct mtx mac_ifnet_mtx;
832220907bSRobert Watson MTX_SYSINIT(mac_ifnet_mtx, &mac_ifnet_mtx, "mac_ifnet", MTX_DEF);
842220907bSRobert Watson #define	MAC_IFNET_LOCK(ifp)	mtx_lock(&mac_ifnet_mtx)
852220907bSRobert Watson #define	MAC_IFNET_UNLOCK(ifp)	mtx_unlock(&mac_ifnet_mtx)
862220907bSRobert Watson 
87e678cce9SRobert Watson /*
88e678cce9SRobert Watson  * XXXRW: In order to use the MAC label UMA zone for all label allocations,
89e678cce9SRobert Watson  * we simply store a pointer to a UMA-allocated label in the mbuf tag.  This
90e678cce9SRobert Watson  * is inefficient and should likely change to using a label embedded in the
91e678cce9SRobert Watson  * tag.
92e678cce9SRobert Watson  */
93e678cce9SRobert Watson 
94e678cce9SRobert Watson /*
95e678cce9SRobert Watson  * Retrieve the label associated with an mbuf by searching for the tag.
96e678cce9SRobert Watson  * Depending on the value of mac_labelmbufs, it's possible that a label will
97e678cce9SRobert Watson  * not be present, in which case NULL is returned.  Policies must handle the
98e678cce9SRobert Watson  * possibility of an mbuf not having label storage if they do not enforce
99e678cce9SRobert Watson  * early loading.
100e678cce9SRobert Watson  */
101c66b4d8dSRobert Watson struct label *
102c66b4d8dSRobert Watson mac_mbuf_to_label(struct mbuf *mbuf)
10310eeb10cSRobert Watson {
104225bff6fSRobert Watson 	struct m_tag *tag;
10510eeb10cSRobert Watson 	struct label *label;
10610eeb10cSRobert Watson 
107583284e1SRobert Watson 	if (mbuf == NULL)
108583284e1SRobert Watson 		return (NULL);
109225bff6fSRobert Watson 	tag = m_tag_find(mbuf, PACKET_TAG_MACLABEL, NULL);
110583284e1SRobert Watson 	if (tag == NULL)
111583284e1SRobert Watson 		return (NULL);
112225bff6fSRobert Watson 	label = (struct label *)(tag+1);
11310eeb10cSRobert Watson 	return (label);
11410eeb10cSRobert Watson }
11510eeb10cSRobert Watson 
116eca8a663SRobert Watson static struct label *
117eca8a663SRobert Watson mac_bpfdesc_label_alloc(void)
118eca8a663SRobert Watson {
119eca8a663SRobert Watson 	struct label *label;
120eca8a663SRobert Watson 
121eca8a663SRobert Watson 	label = mac_labelzone_alloc(M_WAITOK);
122eca8a663SRobert Watson 	MAC_PERFORM(init_bpfdesc_label, label);
123eca8a663SRobert Watson 	return (label);
124eca8a663SRobert Watson }
125eca8a663SRobert Watson 
12608bcdc58SRobert Watson void
12787807196SRobert Watson mac_init_bpfdesc(struct bpf_d *bpf_d)
12808bcdc58SRobert Watson {
12908bcdc58SRobert Watson 
130eca8a663SRobert Watson 	bpf_d->bd_label = mac_bpfdesc_label_alloc();
13108bcdc58SRobert Watson }
13208bcdc58SRobert Watson 
133eca8a663SRobert Watson static struct label *
134eca8a663SRobert Watson mac_ifnet_label_alloc(void)
135f7b951a8SRobert Watson {
136eca8a663SRobert Watson 	struct label *label;
137f7b951a8SRobert Watson 
138eca8a663SRobert Watson 	label = mac_labelzone_alloc(M_WAITOK);
139f7b951a8SRobert Watson 	MAC_PERFORM(init_ifnet_label, label);
140eca8a663SRobert Watson 	return (label);
141f7b951a8SRobert Watson }
142f7b951a8SRobert Watson 
14308bcdc58SRobert Watson void
14408bcdc58SRobert Watson mac_init_ifnet(struct ifnet *ifp)
14508bcdc58SRobert Watson {
14608bcdc58SRobert Watson 
147eca8a663SRobert Watson 	ifp->if_label = mac_ifnet_label_alloc();
148eca8a663SRobert Watson }
149eca8a663SRobert Watson 
15087807196SRobert Watson int
151225bff6fSRobert Watson mac_init_mbuf_tag(struct m_tag *tag, int flag)
15208bcdc58SRobert Watson {
153225bff6fSRobert Watson 	struct label *label;
1546d1a6a9aSRobert Watson 	int error;
15556c15412SRobert Watson 
156225bff6fSRobert Watson 	label = (struct label *) (tag + 1);
157225bff6fSRobert Watson 	mac_init_label(label);
15808bcdc58SRobert Watson 
1596d1a6a9aSRobert Watson 	MAC_CHECK(init_mbuf_label, label, flag);
16056c15412SRobert Watson 	if (error) {
161225bff6fSRobert Watson 		MAC_PERFORM(destroy_mbuf_label, label);
162225bff6fSRobert Watson 		mac_destroy_label(label);
16356c15412SRobert Watson 	}
16456c15412SRobert Watson 	return (error);
16508bcdc58SRobert Watson }
16608bcdc58SRobert Watson 
167225bff6fSRobert Watson int
168225bff6fSRobert Watson mac_init_mbuf(struct mbuf *m, int flag)
169225bff6fSRobert Watson {
170225bff6fSRobert Watson 	struct m_tag *tag;
171225bff6fSRobert Watson 	int error;
172225bff6fSRobert Watson 
173225bff6fSRobert Watson 	M_ASSERTPKTHDR(m);
174225bff6fSRobert Watson 
175225bff6fSRobert Watson #ifndef MAC_ALWAYS_LABEL_MBUF
176225bff6fSRobert Watson 	/*
17719c3e120SRobert Watson 	 * If conditionally allocating mbuf labels, don't allocate unless
17819c3e120SRobert Watson 	 * they are required.
179225bff6fSRobert Watson 	 */
18019c3e120SRobert Watson 	if (!mac_labelmbufs)
18119c3e120SRobert Watson 		return (0);
182225bff6fSRobert Watson #endif
183225bff6fSRobert Watson 	tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label),
184225bff6fSRobert Watson 	    flag);
185225bff6fSRobert Watson 	if (tag == NULL)
186225bff6fSRobert Watson 		return (ENOMEM);
187225bff6fSRobert Watson 	error = mac_init_mbuf_tag(tag, flag);
188225bff6fSRobert Watson 	if (error) {
189225bff6fSRobert Watson 		m_tag_free(tag);
190225bff6fSRobert Watson 		return (error);
191225bff6fSRobert Watson 	}
192225bff6fSRobert Watson 	m_tag_prepend(m, tag);
193225bff6fSRobert Watson 	return (0);
194225bff6fSRobert Watson }
195225bff6fSRobert Watson 
196eca8a663SRobert Watson static void
197eca8a663SRobert Watson mac_bpfdesc_label_free(struct label *label)
198eca8a663SRobert Watson {
19983985c26SRobert Watson 
200eca8a663SRobert Watson 	MAC_PERFORM(destroy_bpfdesc_label, label);
201eca8a663SRobert Watson 	mac_labelzone_free(label);
20287807196SRobert Watson }
20387807196SRobert Watson 
204763bbd2fSRobert Watson void
20508bcdc58SRobert Watson mac_destroy_bpfdesc(struct bpf_d *bpf_d)
20608bcdc58SRobert Watson {
20708bcdc58SRobert Watson 
208eca8a663SRobert Watson 	mac_bpfdesc_label_free(bpf_d->bd_label);
209eca8a663SRobert Watson 	bpf_d->bd_label = NULL;
21008bcdc58SRobert Watson }
21108bcdc58SRobert Watson 
212f7b951a8SRobert Watson static void
213eca8a663SRobert Watson mac_ifnet_label_free(struct label *label)
214f7b951a8SRobert Watson {
215f7b951a8SRobert Watson 
216f7b951a8SRobert Watson 	MAC_PERFORM(destroy_ifnet_label, label);
217eca8a663SRobert Watson 	mac_labelzone_free(label);
218f7b951a8SRobert Watson }
219f7b951a8SRobert Watson 
22087807196SRobert Watson void
22187807196SRobert Watson mac_destroy_ifnet(struct ifnet *ifp)
22287807196SRobert Watson {
22387807196SRobert Watson 
224eca8a663SRobert Watson 	mac_ifnet_label_free(ifp->if_label);
225eca8a663SRobert Watson 	ifp->if_label = NULL;
226eca8a663SRobert Watson }
227eca8a663SRobert Watson 
22887807196SRobert Watson void
229225bff6fSRobert Watson mac_destroy_mbuf_tag(struct m_tag *tag)
23087807196SRobert Watson {
231225bff6fSRobert Watson 	struct label *label;
23287807196SRobert Watson 
233225bff6fSRobert Watson 	label = (struct label *)(tag+1);
234225bff6fSRobert Watson 
235225bff6fSRobert Watson 	MAC_PERFORM(destroy_mbuf_label, label);
236225bff6fSRobert Watson 	mac_destroy_label(label);
23708bcdc58SRobert Watson }
23808bcdc58SRobert Watson 
239e678cce9SRobert Watson /*
240e678cce9SRobert Watson  * mac_copy_mbuf_tag is called when an mbuf header is duplicated, in which
241e678cce9SRobert Watson  * case the labels must also be duplicated.
242e678cce9SRobert Watson  */
243b0323ea3SRobert Watson void
244225bff6fSRobert Watson mac_copy_mbuf_tag(struct m_tag *src, struct m_tag *dest)
245225bff6fSRobert Watson {
246225bff6fSRobert Watson 	struct label *src_label, *dest_label;
247225bff6fSRobert Watson 
248225bff6fSRobert Watson 	src_label = (struct label *)(src+1);
249225bff6fSRobert Watson 	dest_label = (struct label *)(dest+1);
250225bff6fSRobert Watson 
251225bff6fSRobert Watson 	/*
252e678cce9SRobert Watson 	 * mac_init_mbuf_tag() is called on the target tag in m_tag_copy(),
253e678cce9SRobert Watson 	 * so we don't need to call it here.
254225bff6fSRobert Watson 	 */
255225bff6fSRobert Watson 	MAC_PERFORM(copy_mbuf_label, src_label, dest_label);
256225bff6fSRobert Watson }
257225bff6fSRobert Watson 
2583c308b09SRobert Watson void
2593c308b09SRobert Watson mac_copy_mbuf(struct mbuf *m_from, struct mbuf *m_to)
2603c308b09SRobert Watson {
2613c308b09SRobert Watson 	struct label *src_label, *dest_label;
2623c308b09SRobert Watson 
2633c308b09SRobert Watson 	src_label = mac_mbuf_to_label(m_from);
2643c308b09SRobert Watson 	dest_label = mac_mbuf_to_label(m_to);
2653c308b09SRobert Watson 
2663c308b09SRobert Watson 	MAC_PERFORM(copy_mbuf_label, src_label, dest_label);
2673c308b09SRobert Watson }
2683c308b09SRobert Watson 
2692220907bSRobert Watson static void
2702220907bSRobert Watson mac_copy_ifnet_label(struct label *src, struct label *dest)
2712220907bSRobert Watson {
2722220907bSRobert Watson 
2732220907bSRobert Watson 	MAC_PERFORM(copy_ifnet_label, src, dest);
2742220907bSRobert Watson }
2752220907bSRobert Watson 
27669bbb5b1SRobert Watson static int
277f7b951a8SRobert Watson mac_externalize_ifnet_label(struct label *label, char *elements,
27883b7b0edSRobert Watson     char *outbuf, size_t outbuflen)
27969bbb5b1SRobert Watson {
28069bbb5b1SRobert Watson 	int error;
28169bbb5b1SRobert Watson 
282da77b2faSRobert Watson 	MAC_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
283f7b951a8SRobert Watson 
284f7b951a8SRobert Watson 	return (error);
285f7b951a8SRobert Watson }
286f7b951a8SRobert Watson 
287f7b951a8SRobert Watson static int
288f7b951a8SRobert Watson mac_internalize_ifnet_label(struct label *label, char *string)
289f7b951a8SRobert Watson {
290f7b951a8SRobert Watson 	int error;
291f7b951a8SRobert Watson 
292da77b2faSRobert Watson 	MAC_INTERNALIZE(ifnet, label, string);
293f7b951a8SRobert Watson 
294f7b951a8SRobert Watson 	return (error);
295f7b951a8SRobert Watson }
296f7b951a8SRobert Watson 
29795fab37eSRobert Watson void
29895fab37eSRobert Watson mac_create_ifnet(struct ifnet *ifnet)
29995fab37eSRobert Watson {
30095fab37eSRobert Watson 
3012220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
302eca8a663SRobert Watson 	MAC_PERFORM(create_ifnet, ifnet, ifnet->if_label);
3032220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
30495fab37eSRobert Watson }
30595fab37eSRobert Watson 
30695fab37eSRobert Watson void
30795fab37eSRobert Watson mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
30895fab37eSRobert Watson {
30995fab37eSRobert Watson 
310eca8a663SRobert Watson 	MAC_PERFORM(create_bpfdesc, cred, bpf_d, bpf_d->bd_label);
31195fab37eSRobert Watson }
31295fab37eSRobert Watson 
31395fab37eSRobert Watson void
31495fab37eSRobert Watson mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
31595fab37eSRobert Watson {
31610eeb10cSRobert Watson 	struct label *label;
31710eeb10cSRobert Watson 
318e33d9f29SRobert Watson 	BPFD_LOCK_ASSERT(bpf_d);
319e33d9f29SRobert Watson 
320c66b4d8dSRobert Watson 	label = mac_mbuf_to_label(mbuf);
32195fab37eSRobert Watson 
322eca8a663SRobert Watson 	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, bpf_d->bd_label, mbuf,
32310eeb10cSRobert Watson 	    label);
32495fab37eSRobert Watson }
32595fab37eSRobert Watson 
32695fab37eSRobert Watson void
32795fab37eSRobert Watson mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
32895fab37eSRobert Watson {
32910eeb10cSRobert Watson 	struct label *label;
33010eeb10cSRobert Watson 
331c66b4d8dSRobert Watson 	label = mac_mbuf_to_label(mbuf);
33295fab37eSRobert Watson 
3332220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
334eca8a663SRobert Watson 	MAC_PERFORM(create_mbuf_linklayer, ifnet, ifnet->if_label, mbuf,
33510eeb10cSRobert Watson 	    label);
3362220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
33795fab37eSRobert Watson }
33895fab37eSRobert Watson 
33995fab37eSRobert Watson void
34095fab37eSRobert Watson mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
34195fab37eSRobert Watson {
34210eeb10cSRobert Watson 	struct label *label;
34310eeb10cSRobert Watson 
344c66b4d8dSRobert Watson 	label = mac_mbuf_to_label(mbuf);
34595fab37eSRobert Watson 
3462220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
347eca8a663SRobert Watson 	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, ifnet->if_label, mbuf,
34810eeb10cSRobert Watson 	    label);
3492220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
35095fab37eSRobert Watson }
35195fab37eSRobert Watson 
35295fab37eSRobert Watson void
35395fab37eSRobert Watson mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
35495fab37eSRobert Watson     struct mbuf *newmbuf)
35595fab37eSRobert Watson {
35610eeb10cSRobert Watson 	struct label *oldmbuflabel, *newmbuflabel;
35795fab37eSRobert Watson 
358c66b4d8dSRobert Watson 	oldmbuflabel = mac_mbuf_to_label(oldmbuf);
359c66b4d8dSRobert Watson 	newmbuflabel = mac_mbuf_to_label(newmbuf);
36010eeb10cSRobert Watson 
3612220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
36210eeb10cSRobert Watson 	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf, oldmbuflabel,
363eca8a663SRobert Watson 	    ifnet, ifnet->if_label, newmbuf, newmbuflabel);
3642220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
36595fab37eSRobert Watson }
36695fab37eSRobert Watson 
36795fab37eSRobert Watson void
36895fab37eSRobert Watson mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
36995fab37eSRobert Watson {
37010eeb10cSRobert Watson 	struct label *oldmbuflabel, *newmbuflabel;
37195fab37eSRobert Watson 
372c66b4d8dSRobert Watson 	oldmbuflabel = mac_mbuf_to_label(oldmbuf);
373c66b4d8dSRobert Watson 	newmbuflabel = mac_mbuf_to_label(newmbuf);
37410eeb10cSRobert Watson 
37510eeb10cSRobert Watson 	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, oldmbuflabel, newmbuf,
37610eeb10cSRobert Watson 	    newmbuflabel);
37795fab37eSRobert Watson }
37895fab37eSRobert Watson 
37995fab37eSRobert Watson int
38095fab37eSRobert Watson mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
38195fab37eSRobert Watson {
38295fab37eSRobert Watson 	int error;
38395fab37eSRobert Watson 
384e33d9f29SRobert Watson 	BPFD_LOCK_ASSERT(bpf_d);
385e33d9f29SRobert Watson 
38695fab37eSRobert Watson 	if (!mac_enforce_network)
38795fab37eSRobert Watson 		return (0);
38895fab37eSRobert Watson 
3892220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
390eca8a663SRobert Watson 	MAC_CHECK(check_bpfdesc_receive, bpf_d, bpf_d->bd_label, ifnet,
391eca8a663SRobert Watson 	    ifnet->if_label);
3922220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
39395fab37eSRobert Watson 
39495fab37eSRobert Watson 	return (error);
39595fab37eSRobert Watson }
39695fab37eSRobert Watson 
39795fab37eSRobert Watson int
39895fab37eSRobert Watson mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
39995fab37eSRobert Watson {
40010eeb10cSRobert Watson 	struct label *label;
40195fab37eSRobert Watson 	int error;
40295fab37eSRobert Watson 
403225bff6fSRobert Watson 	M_ASSERTPKTHDR(mbuf);
404225bff6fSRobert Watson 
40595fab37eSRobert Watson 	if (!mac_enforce_network)
40695fab37eSRobert Watson 		return (0);
40795fab37eSRobert Watson 
408c66b4d8dSRobert Watson 	label = mac_mbuf_to_label(mbuf);
40995fab37eSRobert Watson 
4102220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
411eca8a663SRobert Watson 	MAC_CHECK(check_ifnet_transmit, ifnet, ifnet->if_label, mbuf,
41210eeb10cSRobert Watson 	    label);
4132220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
41495fab37eSRobert Watson 
41595fab37eSRobert Watson 	return (error);
41695fab37eSRobert Watson }
41795fab37eSRobert Watson 
41895fab37eSRobert Watson int
41995fab37eSRobert Watson mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
42095fab37eSRobert Watson     struct ifnet *ifnet)
42195fab37eSRobert Watson {
422f7b951a8SRobert Watson 	char *elements, *buffer;
4232220907bSRobert Watson 	struct label *intlabel;
424f7b951a8SRobert Watson 	struct mac mac;
42595fab37eSRobert Watson 	int error;
42695fab37eSRobert Watson 
427f7b951a8SRobert Watson 	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
42895fab37eSRobert Watson 	if (error)
42995fab37eSRobert Watson 		return (error);
43095fab37eSRobert Watson 
431f7b951a8SRobert Watson 	error = mac_check_structmac_consistent(&mac);
432f7b951a8SRobert Watson 	if (error)
433f7b951a8SRobert Watson 		return (error);
434f7b951a8SRobert Watson 
435a163d034SWarner Losh 	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
436f7b951a8SRobert Watson 	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
437f7b951a8SRobert Watson 	if (error) {
438f7b951a8SRobert Watson 		free(elements, M_MACTEMP);
439f7b951a8SRobert Watson 		return (error);
440f7b951a8SRobert Watson 	}
441f7b951a8SRobert Watson 
442a163d034SWarner Losh 	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
4432220907bSRobert Watson 	intlabel = mac_ifnet_label_alloc();
4442220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
4452220907bSRobert Watson 	mac_copy_ifnet_label(ifnet->if_label, intlabel);
4462220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
4478f3476b3SRobert Watson 	error = mac_externalize_ifnet_label(intlabel, elements, buffer,
4488f3476b3SRobert Watson 	    mac.m_buflen);
4492220907bSRobert Watson 	mac_ifnet_label_free(intlabel);
450f7b951a8SRobert Watson 	if (error == 0)
451f7b951a8SRobert Watson 		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
452f7b951a8SRobert Watson 
453f7b951a8SRobert Watson 	free(buffer, M_MACTEMP);
454f7b951a8SRobert Watson 	free(elements, M_MACTEMP);
455f7b951a8SRobert Watson 
456f7b951a8SRobert Watson 	return (error);
45795fab37eSRobert Watson }
45895fab37eSRobert Watson 
45995fab37eSRobert Watson int
46095fab37eSRobert Watson mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
46195fab37eSRobert Watson     struct ifnet *ifnet)
46295fab37eSRobert Watson {
463eca8a663SRobert Watson 	struct label *intlabel;
464f7b951a8SRobert Watson 	struct mac mac;
465f7b951a8SRobert Watson 	char *buffer;
46695fab37eSRobert Watson 	int error;
46795fab37eSRobert Watson 
468f7b951a8SRobert Watson 	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
46995fab37eSRobert Watson 	if (error)
47095fab37eSRobert Watson 		return (error);
47195fab37eSRobert Watson 
472f7b951a8SRobert Watson 	error = mac_check_structmac_consistent(&mac);
47395fab37eSRobert Watson 	if (error)
47495fab37eSRobert Watson 		return (error);
47595fab37eSRobert Watson 
476a163d034SWarner Losh 	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
477f7b951a8SRobert Watson 	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
478f7b951a8SRobert Watson 	if (error) {
479f7b951a8SRobert Watson 		free(buffer, M_MACTEMP);
480f7b951a8SRobert Watson 		return (error);
481f7b951a8SRobert Watson 	}
482f7b951a8SRobert Watson 
483eca8a663SRobert Watson 	intlabel = mac_ifnet_label_alloc();
484eca8a663SRobert Watson 	error = mac_internalize_ifnet_label(intlabel, buffer);
485f7b951a8SRobert Watson 	free(buffer, M_MACTEMP);
486f7b951a8SRobert Watson 	if (error) {
487eca8a663SRobert Watson 		mac_ifnet_label_free(intlabel);
488f7b951a8SRobert Watson 		return (error);
489f7b951a8SRobert Watson 	}
490f7b951a8SRobert Watson 
49195fab37eSRobert Watson 	/*
492acd3428bSRobert Watson 	 * XXX: Note that this is a redundant privilege check, since policies
493e678cce9SRobert Watson 	 * impose this check themselves if required by the policy
494acd3428bSRobert Watson 	 * Eventually, this should go away.
49595fab37eSRobert Watson 	 */
496acd3428bSRobert Watson 	error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
497f7b951a8SRobert Watson 	if (error) {
498eca8a663SRobert Watson 		mac_ifnet_label_free(intlabel);
499f7b951a8SRobert Watson 		return (error);
500f7b951a8SRobert Watson 	}
50195fab37eSRobert Watson 
5022220907bSRobert Watson 	MAC_IFNET_LOCK(ifnet);
503eca8a663SRobert Watson 	MAC_CHECK(check_ifnet_relabel, cred, ifnet, ifnet->if_label,
504eca8a663SRobert Watson 	    intlabel);
505f7b951a8SRobert Watson 	if (error) {
5062220907bSRobert Watson 		MAC_IFNET_UNLOCK(ifnet);
507eca8a663SRobert Watson 		mac_ifnet_label_free(intlabel);
508f7b951a8SRobert Watson 		return (error);
509f7b951a8SRobert Watson 	}
51095fab37eSRobert Watson 
511eca8a663SRobert Watson 	MAC_PERFORM(relabel_ifnet, cred, ifnet, ifnet->if_label, intlabel);
5122220907bSRobert Watson 	MAC_IFNET_UNLOCK(ifnet);
51395fab37eSRobert Watson 
514eca8a663SRobert Watson 	mac_ifnet_label_free(intlabel);
515f7b951a8SRobert Watson 	return (0);
51695fab37eSRobert Watson }
517