xref: /freebsd/sys/security/mac/mac_inet6.c (revision 4b908c8bb4b13eccb45b0fdb4ffcb8abcb086814)
1b9b0dac3SRobert Watson /*-
24b908c8bSRobert Watson  * Copyright (c) 2007-2008 Robert N. M. Watson
3b9b0dac3SRobert Watson  * All rights reserved.
4b9b0dac3SRobert Watson  *
5b9b0dac3SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
6b9b0dac3SRobert Watson  *
7b9b0dac3SRobert Watson  * Redistribution and use in source and binary forms, with or without
8b9b0dac3SRobert Watson  * modification, are permitted provided that the following conditions
9b9b0dac3SRobert Watson  * are met:
10b9b0dac3SRobert Watson  * 1. Redistributions of source code must retain the above copyright
11b9b0dac3SRobert Watson  *    notice, this list of conditions and the following disclaimer.
12b9b0dac3SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
13b9b0dac3SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
14b9b0dac3SRobert Watson  *    documentation and/or other materials provided with the distribution.
15b9b0dac3SRobert Watson  *
16b9b0dac3SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b9b0dac3SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b9b0dac3SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b9b0dac3SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b9b0dac3SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b9b0dac3SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b9b0dac3SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b9b0dac3SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b9b0dac3SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b9b0dac3SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b9b0dac3SRobert Watson  * SUCH DAMAGE.
27b9b0dac3SRobert Watson  */
28b9b0dac3SRobert Watson 
29b9b0dac3SRobert Watson #include <sys/cdefs.h>
30b9b0dac3SRobert Watson __FBSDID("$FreeBSD$");
31b9b0dac3SRobert Watson 
32b9b0dac3SRobert Watson #include "opt_mac.h"
33b9b0dac3SRobert Watson 
34b9b0dac3SRobert Watson #include <sys/param.h>
35b9b0dac3SRobert Watson #include <sys/kernel.h>
36b9b0dac3SRobert Watson #include <sys/lock.h>
37b9b0dac3SRobert Watson #include <sys/malloc.h>
38b9b0dac3SRobert Watson #include <sys/mutex.h>
39b9b0dac3SRobert Watson #include <sys/sbuf.h>
40b9b0dac3SRobert Watson #include <sys/systm.h>
41b9b0dac3SRobert Watson #include <sys/mount.h>
42b9b0dac3SRobert Watson #include <sys/file.h>
43b9b0dac3SRobert Watson #include <sys/namei.h>
44b9b0dac3SRobert Watson #include <sys/protosw.h>
45b9b0dac3SRobert Watson #include <sys/socket.h>
46b9b0dac3SRobert Watson #include <sys/socketvar.h>
47b9b0dac3SRobert Watson #include <sys/sysctl.h>
48b9b0dac3SRobert Watson 
49b9b0dac3SRobert Watson #include <net/if.h>
50b9b0dac3SRobert Watson #include <net/if_var.h>
51b9b0dac3SRobert Watson 
524b908c8bSRobert Watson #include <netinet/in.h>
534b908c8bSRobert Watson #include <netinet/ip6.h>
544b908c8bSRobert Watson #include <netinet6/ip6_var.h>
554b908c8bSRobert Watson 
56b9b0dac3SRobert Watson #include <security/mac/mac_framework.h>
57b9b0dac3SRobert Watson #include <security/mac/mac_internal.h>
58b9b0dac3SRobert Watson #include <security/mac/mac_policy.h>
59b9b0dac3SRobert Watson 
604b908c8bSRobert Watson static struct label *
614b908c8bSRobert Watson mac_ip6q_label_alloc(int flag)
624b908c8bSRobert Watson {
634b908c8bSRobert Watson 	struct label *label;
644b908c8bSRobert Watson 	int error;
654b908c8bSRobert Watson 
664b908c8bSRobert Watson 	label = mac_labelzone_alloc(flag);
674b908c8bSRobert Watson 	if (label == NULL)
684b908c8bSRobert Watson 		return (NULL);
694b908c8bSRobert Watson 
704b908c8bSRobert Watson 	MAC_CHECK(ip6q_init_label, label, flag);
714b908c8bSRobert Watson 	if (error) {
724b908c8bSRobert Watson 		MAC_PERFORM(ip6q_destroy_label, label);
734b908c8bSRobert Watson 		mac_labelzone_free(label);
744b908c8bSRobert Watson 		return (NULL);
754b908c8bSRobert Watson 	}
764b908c8bSRobert Watson 	return (label);
774b908c8bSRobert Watson }
784b908c8bSRobert Watson 
794b908c8bSRobert Watson int
804b908c8bSRobert Watson mac_ip6q_init(struct ip6q *q6, int flag)
814b908c8bSRobert Watson {
824b908c8bSRobert Watson 
834b908c8bSRobert Watson 	if (mac_labeled & MPC_OBJECT_IPQ) {
844b908c8bSRobert Watson 		q6->ip6q_label = mac_ip6q_label_alloc(flag);
854b908c8bSRobert Watson 		if (q6->ip6q_label == NULL)
864b908c8bSRobert Watson 			return (ENOMEM);
874b908c8bSRobert Watson 	} else
884b908c8bSRobert Watson 		q6->ip6q_label = NULL;
894b908c8bSRobert Watson 	return (0);
904b908c8bSRobert Watson }
914b908c8bSRobert Watson 
924b908c8bSRobert Watson static void
934b908c8bSRobert Watson mac_ip6q_label_free(struct label *label)
944b908c8bSRobert Watson {
954b908c8bSRobert Watson 
964b908c8bSRobert Watson 	MAC_PERFORM(ip6q_destroy_label, label);
974b908c8bSRobert Watson 	mac_labelzone_free(label);
984b908c8bSRobert Watson }
994b908c8bSRobert Watson 
1004b908c8bSRobert Watson void
1014b908c8bSRobert Watson mac_ip6q_destroy(struct ip6q *q6)
1024b908c8bSRobert Watson {
1034b908c8bSRobert Watson 
1044b908c8bSRobert Watson 	if (q6->ip6q_label != NULL) {
1054b908c8bSRobert Watson 		mac_ip6q_label_free(q6->ip6q_label);
1064b908c8bSRobert Watson 		q6->ip6q_label = NULL;
1074b908c8bSRobert Watson 	}
1084b908c8bSRobert Watson }
1094b908c8bSRobert Watson 
1104b908c8bSRobert Watson void
1114b908c8bSRobert Watson mac_ip6q_reassemble(struct ip6q *q6, struct mbuf *m)
1124b908c8bSRobert Watson {
1134b908c8bSRobert Watson 	struct label *label;
1144b908c8bSRobert Watson 
1154b908c8bSRobert Watson 	label = mac_mbuf_to_label(m);
1164b908c8bSRobert Watson 
1174b908c8bSRobert Watson 	MAC_PERFORM(ip6q_reassemble, q6, q6->ip6q_label, m, label);
1184b908c8bSRobert Watson }
1194b908c8bSRobert Watson 
1204b908c8bSRobert Watson void
1214b908c8bSRobert Watson mac_ip6q_create(struct mbuf *m, struct ip6q *q6)
1224b908c8bSRobert Watson {
1234b908c8bSRobert Watson 	struct label *label;
1244b908c8bSRobert Watson 
1254b908c8bSRobert Watson 	label = mac_mbuf_to_label(m);
1264b908c8bSRobert Watson 
1274b908c8bSRobert Watson 	MAC_PERFORM(ip6q_create, m, label, q6, q6->ip6q_label);
1284b908c8bSRobert Watson }
1294b908c8bSRobert Watson 
1304b908c8bSRobert Watson int
1314b908c8bSRobert Watson mac_ip6q_match(struct mbuf *m, struct ip6q *q6)
1324b908c8bSRobert Watson {
1334b908c8bSRobert Watson 	struct label *label;
1344b908c8bSRobert Watson 	int result;
1354b908c8bSRobert Watson 
1364b908c8bSRobert Watson 	label = mac_mbuf_to_label(m);
1374b908c8bSRobert Watson 
1384b908c8bSRobert Watson 	result = 1;
1394b908c8bSRobert Watson 	MAC_BOOLEAN(ip6q_match, &&, m, label, q6, q6->ip6q_label);
1404b908c8bSRobert Watson 
1414b908c8bSRobert Watson 	return (result);
1424b908c8bSRobert Watson }
1434b908c8bSRobert Watson 
1444b908c8bSRobert Watson void
1454b908c8bSRobert Watson mac_ip6q_update(struct mbuf *m, struct ip6q *q6)
1464b908c8bSRobert Watson {
1474b908c8bSRobert Watson 	struct label *label;
1484b908c8bSRobert Watson 
1494b908c8bSRobert Watson 	label = mac_mbuf_to_label(m);
1504b908c8bSRobert Watson 
1514b908c8bSRobert Watson 	MAC_PERFORM(ip6q_update, m, label, q6, q6->ip6q_label);
1524b908c8bSRobert Watson }
1534b908c8bSRobert Watson 
154b9b0dac3SRobert Watson void
155b9b0dac3SRobert Watson mac_netinet6_nd6_send(struct ifnet *ifp, struct mbuf *m)
156b9b0dac3SRobert Watson {
157b9b0dac3SRobert Watson 	struct label *mlabel;
158b9b0dac3SRobert Watson 
159b9b0dac3SRobert Watson 	mlabel = mac_mbuf_to_label(m);
160b9b0dac3SRobert Watson 
161b9b0dac3SRobert Watson 	MAC_PERFORM(netinet6_nd6_send, ifp, ifp->if_label, m, mlabel);
162b9b0dac3SRobert Watson }
163