2 * Copyright (c) 2007-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999-2002 Robert N. M. Watson
30 * Copyright (c) 2001 Ilmar S. Habibulin
31 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
32 * Copyright (c) 2006-2007 SPARTA, Inc.
33 * All rights reserved.
35 * This software was developed by Robert Watson and Ilmar Habibulin for the
38 * This software was developed for the FreeBSD Project in part by Network
39 * Associates Laboratories, the Security Research Division of Network
40 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
41 * as part of the DARPA CHATS research program.
43 * This software was enhanced by SPARTA ISSO under SPAWAR contract
44 * N66001-04-C-6019 ("SEFOS").
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 #include <sys/param.h>
69 #include <sys/kernel.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/ip_var.h>
80 #include <security/mac_internal.h>
83 mac_inpcb_label_alloc(int flag
)
88 label
= mac_labelzone_alloc(flag
);
92 MAC_CHECK(inpcb_label_init
, label
, flag
);
94 MAC_PERFORM(inpcb_label_destroy
, label
);
95 mac_labelzone_free(label
);
102 mac_inpcb_label_init(struct inpcb
*inp
, int flag
)
104 inp
->inp_label
= mac_inpcb_label_alloc(flag
);
105 if (inp
->inp_label
== NULL
) {
111 static struct label
*
112 mac_ipq_label_alloc(int flag
)
117 label
= mac_labelzone_alloc(flag
);
122 MAC_CHECK(ipq_label_init
, label
, flag
);
124 MAC_PERFORM(ipq_label_destroy
, label
);
125 mac_labelzone_free(label
);
132 mac_ipq_label_init(struct ipq
*ipq
, int flag
)
134 ipq
->ipq_label
= mac_ipq_label_alloc(flag
);
135 if (ipq
->ipq_label
== NULL
) {
142 mac_inpcb_label_free(struct label
*label
)
144 MAC_PERFORM(inpcb_label_destroy
, label
);
145 mac_labelzone_free(label
);
149 mac_inpcb_label_destroy(struct inpcb
*inp
)
151 mac_inpcb_label_free(inp
->inp_label
);
152 inp
->inp_label
= NULL
;
156 mac_inpcb_label_recycle(struct inpcb
*inp
)
158 MAC_PERFORM(inpcb_label_recycle
, inp
->inp_label
);
162 mac_ipq_label_free(struct label
*label
)
164 MAC_PERFORM(ipq_label_destroy
, label
);
165 mac_labelzone_free(label
);
169 mac_ipq_label_destroy(struct ipq
*ipq
)
171 mac_ipq_label_free(ipq
->ipq_label
);
172 ipq
->ipq_label
= NULL
;
176 mac_inpcb_label_associate(struct socket
*so
, struct inpcb
*inp
)
178 MAC_PERFORM(inpcb_label_associate
, so
, so
->so_label
, inp
,
183 mac_mbuf_label_associate_ipq(struct ipq
*ipq
, struct mbuf
*m
)
187 label
= mac_mbuf_to_label(m
);
189 MAC_PERFORM(mbuf_label_associate_ipq
, ipq
, ipq
->ipq_label
, m
, label
);
193 mac_netinet_fragment(struct mbuf
*datagram
, struct mbuf
*fragment
)
195 struct label
*datagramlabel
, *fragmentlabel
;
197 datagramlabel
= mac_mbuf_to_label(datagram
);
198 fragmentlabel
= mac_mbuf_to_label(fragment
);
200 MAC_PERFORM(netinet_fragment
, datagram
, datagramlabel
, fragment
,
205 mac_ipq_label_associate(struct mbuf
*fragment
, struct ipq
*ipq
)
209 label
= mac_mbuf_to_label(fragment
);
211 MAC_PERFORM(ipq_label_associate
, fragment
, label
, ipq
, ipq
->ipq_label
);
215 mac_mbuf_label_associate_inpcb(struct inpcb
*inp
, struct mbuf
*m
)
217 struct label
*mlabel
;
219 /* INP_LOCK_ASSERT(inp); */
220 mlabel
= mac_mbuf_to_label(m
);
222 MAC_PERFORM(mbuf_label_associate_inpcb
, inp
, inp
->inp_label
, m
, mlabel
);
226 mac_ipq_label_compare(struct mbuf
*fragment
, struct ipq
*ipq
)
231 label
= mac_mbuf_to_label(fragment
);
234 MAC_BOOLEAN(ipq_label_compare
, &&, fragment
, label
, ipq
, ipq
->ipq_label
);
240 mac_netinet_icmp_reply(struct mbuf
*m
)
244 label
= mac_mbuf_to_label(m
);
246 MAC_PERFORM(netinet_icmp_reply
, m
, label
);
250 mac_netinet_tcp_reply(struct mbuf
*m
)
254 label
= mac_mbuf_to_label(m
);
256 MAC_PERFORM(netinet_tcp_reply
, m
, label
);
260 mac_ipq_label_update(struct mbuf
*fragment
, struct ipq
*ipq
)
264 label
= mac_mbuf_to_label(fragment
);
266 MAC_PERFORM(ipq_label_update
, fragment
, label
, ipq
, ipq
->ipq_label
);
270 mac_inpcb_check_deliver(struct inpcb
*inp
, struct mbuf
*m
, int family
, int type
)
275 if ((m
->m_flags
& M_PKTHDR
) == 0) {
276 panic("%s: no mbuf packet header!", __func__
);
279 label
= mac_mbuf_to_label(m
);
281 MAC_CHECK(inpcb_check_deliver
, inp
, inp
->inp_label
, m
, label
,
288 * Propagate a change in the socket label to the inpcb label.
291 mac_inpcb_label_update(struct socket
*so
)
295 /* XXX: assert socket lock. */
296 inp
= sotoinpcb(so
); /* XXX: inp locking */
299 /* INP_LOCK_ASSERT(inp); */
300 MAC_PERFORM(inpcb_label_update
, so
, so
->so_label
, inp
,