xref: /illumos-gate/usr/src/uts/common/sys/tsol/label_macro.h (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_LABEL_MACRO_H
27 #define	_LABEL_MACRO_H
28 
29 #include <sys/types.h>
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 /* PRIVATE ONLY TO THE LABEL LIBRARY.  DO NOT USE ELSEWHERE */
36 
37 /* Actual Binary Label Structure Definitions */
38 
39 typedef int16_t	_Classification;
40 typedef struct {
41 	union {
42 		uint8_t		class_ar[2];
43 		_Classification	class_chunk;
44 	} class_u;
45 } Classification_t;
46 
47 typedef struct {
48 	uint32_t c1;
49 	uint32_t c2;
50 	uint32_t c3;
51 	uint32_t c4;
52 	uint32_t c5;
53 	uint32_t c6;
54 	uint32_t c7;
55 	uint32_t c8;
56 } Compartments_t;
57 
58 typedef struct {
59 	uint32_t m1;
60 	uint32_t m2;
61 	uint32_t m3;
62 	uint32_t m4;
63 	uint32_t m5;
64 	uint32_t m6;
65 	uint32_t m7;
66 	uint32_t m8;
67 } Markings_t;
68 
69 typedef struct _mac_label_impl {
70 	uint8_t id;		/* Magic to say label type */
71 	uint8_t _c_len;		/* Number of Compartment words */
72 	Classification_t classification;
73 	Compartments_t compartments;
74 } _mac_label_impl_t;
75 
76 typedef _mac_label_impl_t	_blevel_impl_t,		/* compatibility */
77 				_bslabel_impl_t,	/* Sensitivity Label */
78 				_bclear_impl_t;		/* Clearance */
79 
80 typedef struct _binary_information_label_impl {	/* Information Label */
81 	_mac_label_impl_t	binformation_level;
82 	Markings_t markings;
83 } _bilabel_impl_t;
84 
85 typedef struct _binary_cmw_label_impl {		/* CMW Label */
86 	_bslabel_impl_t bcl_sensitivity_label;
87 	_bilabel_impl_t bcl_information_label;
88 } _bclabel_impl_t;
89 
90 typedef struct _binary_level_range_impl {	/* Level Range */
91 	_mac_label_impl_t lower_bound;
92 	_mac_label_impl_t upper_bound;
93 } _brange_impl_t, brange_t;
94 
95 #define	NMLP_MAX	0x10
96 #define	NSLS_MAX	0x4
97 
98 typedef _mac_label_impl_t blset_t[NSLS_MAX];
99 
100 /* Label Identifier Types */
101 
102 #define	SUN_MAC_ID	0x41	/* MAC label, legacy SUN_SL_ID */
103 #define	SUN_UCLR_ID	0x49	/* User Clearance, legacy SUN_CLR_ID */
104 
105 #define	_C_LEN		8	/* number of compartments words */
106 
107 /* m_label_t macros */
108 #define	_MTYPE(l, t) \
109 	(((_mac_label_impl_t *)(l))->id == (t))
110 
111 #define	_MSETTYPE(l, t) \
112 	(((_mac_label_impl_t *)(l))->id = (t))
113 
114 #define	_MGETTYPE(l)	(((_mac_label_impl_t *)(l))->id)
115 
116 #define	_MEQUAL(l1, l2) \
117 	(LCLASS(l1) == LCLASS(l2) && \
118 	(l1)->_comps.c1 == (l2)->_comps.c1 && \
119 	(l1)->_comps.c2 == (l2)->_comps.c2 && \
120 	(l1)->_comps.c3 == (l2)->_comps.c3 && \
121 	(l1)->_comps.c4 == (l2)->_comps.c4 && \
122 	(l1)->_comps.c5 == (l2)->_comps.c5 && \
123 	(l1)->_comps.c6 == (l2)->_comps.c6 && \
124 	(l1)->_comps.c7 == (l2)->_comps.c7 && \
125 	(l1)->_comps.c8 == (l2)->_comps.c8)
126 
127 #define	SUN_INVALID_ID	0	/* uninitialized label */
128 #define	SUN_CMW_ID	0x83	/* 104 - total bytes in CMW Label */
129 #define	SUN_SL_ID	0x41	/* 36 - total bytes in Sensitivity Label */
130 #define	SUN_SL_UN	0xF1	/* undefined Sensitivity Label */
131 #define	SUN_IL_ID	0x42	/* 68 - total bytes in Information Label */
132 #define	SUN_IL_UN	0x73	/* undefined Information Label */
133 #define	SUN_CLR_ID	0x49	/* 36 - total bytes in Clearance */
134 #define	SUN_CLR_UN	0xF9	/* undefined Clearance */
135 
136 #define	_bcl_sl		bcl_sensitivity_label
137 #define	_bcl_il		bcl_information_label
138 #define	_bslev_il	binformation_level
139 
140 #define	_lclass		classification
141 #ifdef	_BIG_ENDIAN
142 #define	LCLASS(slp)	((slp)->_lclass.class_u.class_chunk)
143 #define	LCLASS_SET(slp, l)	((slp)->_lclass.class_u.class_chunk = (l))
144 #else
145 #define	LCLASS(slp)	\
146 	((_Classification)(((slp)->_lclass.class_u.class_ar[0] << 8) | \
147 	(slp)->_lclass.class_u.class_ar[1]))
148 #define	LCLASS_SET(slp, l)	\
149 	((slp)->_lclass.class_u.class_ar[0] = (uint8_t)((l)>> 8), \
150 	(slp)->_lclass.class_u.class_ar[1] = (uint8_t)(l))
151 #endif	/* _BIG_ENDIAN */
152 #define	_comps		compartments
153 
154 #define	_iid		_bslev_il.id
155 #define	_i_c_len		_bslev_il._c_len
156 #define	_iclass		_bslev_il._lclass
157 #ifdef	_BIG_ENDIAN
158 #define	ICLASS(ilp)	((ilp)->_iclass.class_u.class_chunk)
159 #define	ICLASS_SET(ilp, l)	((ilp)->_iclass.class_u.class_chunk = (l))
160 #else
161 #define	ICLASS(ilp)	\
162 	((_Classification)(((ilp)->_iclass.class_u.class_ar[0] << 8) | \
163 	(ilp)->_iclass.class_u.class_ar[1]))
164 #define	ICLASS_SET(ilp, l)	\
165 	((ilp)->_iclass.class_u.class_ar[0] = (uint8_t)((l)>> 8), \
166 	(ilp)->_iclass.class_u.class_ar[1] = (uint8_t)(l))
167 #endif	/* _BIG_ENDIAN */
168 #define	_icomps		_bslev_il._comps
169 #define	_imarks		markings
170 
171 /* Manifest Constant Values */
172 
173 #define	LOW_CLASS	0	/* Admin_Low classification value */
174 #define	HIGH_CLASS	0x7FFF	/* Admin_High classification value */
175 #define	EMPTY_SET	0	/* Empty compartments and markings set */
176 #define	UNIVERSAL_SET	0xFFFFFFFFU	/* Universal compartments and */
177 					/* markings set */
178 
179 /* Construct initial labels */
180 
181 #define	_LOW_LABEL(l, t) \
182 	((l)->id = t, (l)->_c_len = _C_LEN, LCLASS_SET(l, LOW_CLASS), \
183 	(l)->_comps.c1 = (l)->_comps.c2 = (l)->_comps.c3 = (l)->_comps.c4 = \
184 	(l)->_comps.c5 = (l)->_comps.c6 = (l)->_comps.c7 = (l)->_comps.c8 = \
185 	EMPTY_SET)
186 
187 #define	_HIGH_LABEL(l, t) \
188 	((l)->id = t, (l)->_c_len = _C_LEN, LCLASS_SET(l, HIGH_CLASS), \
189 	(l)->_comps.c1 = (l)->_comps.c2 = (l)->_comps.c3 = (l)->_comps.c4 = \
190 	(l)->_comps.c5 = (l)->_comps.c6 = (l)->_comps.c7 = (l)->_comps.c8 = \
191 	UNIVERSAL_SET)
192 
193 /* Macro equivalents */
194 
195 /* Is this memory a properly formatted label of type t? */
196 #define	BLTYPE(l, t) \
197 	((t) == SUN_CMW_ID ? \
198 	(((_bclabel_impl_t *)(l))->_bcl_sl.id == SUN_SL_ID || \
199 	((_bclabel_impl_t *)(l))->_bcl_sl.id == SUN_SL_UN) && \
200 	(((_bclabel_impl_t *)(l))->_bcl_il._iid == SUN_IL_ID || \
201 	((_bclabel_impl_t *)(l))->_bcl_il._iid == SUN_IL_UN) : \
202 	((_mac_label_impl_t *)(l))->id == (t))
203 
204 /* Are the levels of these labels equal? */
205 #define	BLEQUAL(l1, l2) \
206 	_BLEQUAL((_mac_label_impl_t *)(l1), (_mac_label_impl_t *)(l2))
207 
208 #define	_BLEQUAL(l1, l2) \
209 	(LCLASS(l1) == LCLASS(l2) && \
210 	(l1)->_comps.c1 == (l2)->_comps.c1 && \
211 	(l1)->_comps.c2 == (l2)->_comps.c2 && \
212 	(l1)->_comps.c3 == (l2)->_comps.c3 && \
213 	(l1)->_comps.c4 == (l2)->_comps.c4 && \
214 	(l1)->_comps.c5 == (l2)->_comps.c5 && \
215 	(l1)->_comps.c6 == (l2)->_comps.c6 && \
216 	(l1)->_comps.c7 == (l2)->_comps.c7 && \
217 	(l1)->_comps.c8 == (l2)->_comps.c8)
218 
219 /* Does the level of l1 dominate that of l2? */
220 #define	BLDOMINATES(l1, l2) \
221 	_BLDOMINATES((_mac_label_impl_t *)(l1), (_mac_label_impl_t *)(l2))
222 
223 #define	_BLDOMINATES(l1, l2) (LCLASS(l1) >= LCLASS(l2) && \
224 	(l2)->_comps.c1 == ((l1)->_comps.c1 & (l2)->_comps.c1) && \
225 	(l2)->_comps.c2 == ((l1)->_comps.c2 & (l2)->_comps.c2) && \
226 	(l2)->_comps.c3 == ((l1)->_comps.c3 & (l2)->_comps.c3) && \
227 	(l2)->_comps.c4 == ((l1)->_comps.c4 & (l2)->_comps.c4) && \
228 	(l2)->_comps.c5 == ((l1)->_comps.c5 & (l2)->_comps.c5) && \
229 	(l2)->_comps.c6 == ((l1)->_comps.c6 & (l2)->_comps.c6) && \
230 	(l2)->_comps.c7 == ((l1)->_comps.c7 & (l2)->_comps.c7) && \
231 	(l2)->_comps.c8 == ((l1)->_comps.c8 & (l2)->_comps.c8))
232 
233 /* Does the level of l1 strictly dominate that of l2? */
234 #define	BLSTRICTDOM(l1, l2) (!BLEQUAL(l1, l2) && BLDOMINATES(l1, l2))
235 
236 /* Is the level of l within the range r? */
237 #define	BLINRANGE(l, r)\
238 	(BLDOMINATES((l), &((r)->lower_bound)) && \
239 	BLDOMINATES(&((r)->upper_bound), (l)))
240 
241 /* Least Upper Bound level l1 and l2 replacing l1 with the result. */
242 #define	BLMAXIMUM(l1, l2) \
243 	_BLMAXIMUM((_mac_label_impl_t *)(l1), (_mac_label_impl_t *)(l2))
244 
245 #define	_BLMAXIMUM(l1, l2)\
246 	(((l1)->_lclass = (LCLASS(l1) < LCLASS(l2)) ? \
247 	(l2)->_lclass : (l1)->_lclass), \
248 	(l1)->_comps.c1 |= (l2)->_comps.c1, \
249 	(l1)->_comps.c2 |= (l2)->_comps.c2, \
250 	(l1)->_comps.c3 |= (l2)->_comps.c3, \
251 	(l1)->_comps.c4 |= (l2)->_comps.c4, \
252 	(l1)->_comps.c5 |= (l2)->_comps.c5, \
253 	(l1)->_comps.c6 |= (l2)->_comps.c6, \
254 	(l1)->_comps.c7 |= (l2)->_comps.c7, \
255 	(l1)->_comps.c8 |= (l2)->_comps.c8)
256 
257 /* Greatest Lower Bound level l1 and l2 replacing l1 with the result. */
258 #define	BLMINIMUM(l1, l2) \
259 	_BLMINIMUM((_mac_label_impl_t *)(l1), (_mac_label_impl_t *)(l2))
260 
261 #define	_BLMINIMUM(l1, l2)\
262 	(((l1)->_lclass = (LCLASS(l1) > LCLASS(l2)) ? \
263 	(l2)->_lclass : (l1)->_lclass), \
264 	(l1)->_comps.c1 &= (l2)->_comps.c1, \
265 	(l1)->_comps.c2 &= (l2)->_comps.c2, \
266 	(l1)->_comps.c3 &= (l2)->_comps.c3, \
267 	(l1)->_comps.c4 &= (l2)->_comps.c4, \
268 	(l1)->_comps.c5 &= (l2)->_comps.c5, \
269 	(l1)->_comps.c6 &= (l2)->_comps.c6, \
270 	(l1)->_comps.c7 &= (l2)->_comps.c7, \
271 	(l1)->_comps.c8 &= (l2)->_comps.c8)
272 
273 /* Create Manifest Labels */
274 
275 /* Write a System_Low CMW Label into this memory. */
276 #define	BCLLOW(l) (BSLLOW(BCLTOSL(l)), BILLOW(BCLTOIL(l)))
277 
278 /* Write a System_Low Sensitivity Label into this memory. */
279 #define	BSLLOW(l) _BSLLOW((_bslabel_impl_t *)(l))
280 
281 #define	_BSLLOW(l) \
282 	((l)->id = SUN_SL_ID, (l)->_c_len = _C_LEN, LCLASS_SET(l, LOW_CLASS), \
283 	(l)->_comps.c1 = (l)->_comps.c2 = (l)->_comps.c3 = (l)->_comps.c4 = \
284 	(l)->_comps.c5 = (l)->_comps.c6 = (l)->_comps.c7 = (l)->_comps.c8 = \
285 	EMPTY_SET)
286 
287 /* Write a System_High Sensitivity Label into this memory. */
288 #define	BSLHIGH(l) _BSLHIGH((_bslabel_impl_t *)(l))
289 
290 #define	_BSLHIGH(l) \
291 	((l)->id = SUN_SL_ID, (l)->_c_len = _C_LEN, LCLASS_SET(l, HIGH_CLASS), \
292 	(l)->_comps.c1 = (l)->_comps.c2 = (l)->_comps.c3 = (l)->_comps.c4 = \
293 	(l)->_comps.c5 = (l)->_comps.c6 = (l)->_comps.c7 = (l)->_comps.c8 = \
294 	UNIVERSAL_SET)
295 
296 /* Write a System_Low Information Label into this memory. */
297 #define	BILLOW(l) _BILLOW((_bilabel_impl_t *)(l))
298 
299 #define	_BILLOW(l) \
300 	((l)->_iid = SUN_IL_ID, (l)->_i_c_len = _C_LEN, \
301 	ICLASS_SET(l, LOW_CLASS), \
302 	(l)->_icomps.c1 = (l)->_icomps.c2 = (l)->_icomps.c3 = \
303 	(l)->_icomps.c4 = (l)->_icomps.c5 = (l)->_icomps.c6 = \
304 	(l)->_icomps.c7 = (l)->_icomps.c8 = EMPTY_SET, \
305 	(l)->_imarks.m1 = (l)->_imarks.m2 = (l)->_imarks.m3 = \
306 	(l)->_imarks.m4 = (l)->_imarks.m5 = (l)->_imarks.m6 = \
307 	(l)->_imarks.m7 = (l)->_imarks.m8 = EMPTY_SET)
308 
309 
310 /* Write a System_Low Sensitivity Label into this memory. */
311 #define	BCLEARLOW(l) _BCLEARLOW((_bclear_impl_t *)(l))
312 
313 #define	_BCLEARLOW(c) \
314 	((c)->id = SUN_CLR_ID, (c)->_c_len = _C_LEN, \
315 	LCLASS_SET(c, LOW_CLASS), \
316 	(c)->_comps.c1 = (c)->_comps.c2 = (c)->_comps.c3 = (c)->_comps.c4 = \
317 	(c)->_comps.c5 = (c)->_comps.c6 = (c)->_comps.c7 = (c)->_comps.c8 = \
318 	EMPTY_SET)
319 
320 /* Write a System_High Sensitivity Label into this memory. */
321 #define	BCLEARHIGH(l) _BCLEARHIGH((_bclear_impl_t *)(l))
322 
323 #define	_BCLEARHIGH(c) \
324 	((c)->id = SUN_CLR_ID, (c)->_c_len = _C_LEN, \
325 	LCLASS_SET(c, HIGH_CLASS), \
326 	(c)->_comps.c1 = (c)->_comps.c2 = (c)->_comps.c3 = (c)->_comps.c4 = \
327 	(c)->_comps.c5 = (c)->_comps.c6 = (c)->_comps.c7 = (c)->_comps.c8 = \
328 	UNIVERSAL_SET)
329 
330 /* Write an undefined Sensitivity Label into this memory. */
331 #define	BSLUNDEF(l) (((_bslabel_impl_t *)(l))->id = SUN_SL_UN)
332 
333 /* Write an undefined Clearance into this memory. */
334 #define	BCLEARUNDEF(c) (((_bclear_impl_t *)(c))->id = SUN_CLR_UN)
335 
336 /* Retrieve the Sensitivity Label portion of a CMW Label */
337 #define	BCLTOSL(l) ((bslabel_t *)&((_bclabel_impl_t *)(l))->_bcl_sl)
338 
339 /* Retrieve the Information Label portion of a CMW Label */
340 #define	BCLTOIL(l) ((_bilabel_impl_t *)&((_bclabel_impl_t *)(l))->_bcl_il)
341 
342 /* Copy the Sensitivity Label portion from a CMW Label */
343 #define	GETCSL(l1, l2) \
344 	(*((_bslabel_impl_t *)(l1)) = ((_bclabel_impl_t *)(l2))->_bcl_sl)
345 
346 /* Replace the Sensitivity Label portion of a CMW Label */
347 #define	SETCSL(l1, l2) \
348 	(((_bclabel_impl_t *)(l1))->_bcl_sl = *((_bslabel_impl_t *)(l2)))
349 
350 /* Set type of this memory to the label type 't' */
351 #define	SETBLTYPE(l, t) (((_bclabel_impl_t *)(l))->_bcl_sl.id = (t))
352 
353 #define	GETBLTYPE(l)	(((const _bclabel_impl_t *)(l))->_bcl_sl.id)
354 
355 #ifdef	__cplusplus
356 }
357 #endif
358 
359 #endif	/* !_LABEL_MACRO_H */
360