summaryrefslogtreecommitdiff
path: root/usr/src/lib/libtsnet/common/misc.c
blob: 7b3d1f142c18fc8425d54c1cc8285db8a9b727f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * From "misc.c	5.15	00/05/31 SMI; TSOL 2.x"
 */

/*
 *	Miscellaneous user interfaces to trusted label functions.
 */


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#include <libintl.h>
#include <libtsnet.h>
#include <tsol/label.h>

#include <net/route.h>

#define	MAX_ATTR_LEN	1024

/*
 * Parse off an entry from a line.  Entry is stored in 'outbuf'.  Returned
 * value is a pointer to the first unprocessed input character from 'instr'.
 */
const char *
parse_entry(char *outbuf, size_t outlen, const char *instr,
    const char *delimit)
{
	boolean_t escape_state = B_FALSE;
	boolean_t any_white;
	char chr;

	any_white = strchr(delimit, '\n') != NULL;

	/*
	 * User may specify outlen as 0 to skip over a field without storing
	 * it anywhere.  Otherwise, we need at least one byte for the
	 * terminating NUL plus one byte to store another byte from instr.
	 */
	while (outlen != 1 && (chr = *instr++) != '\0') {
		if (!escape_state) {
			if (chr == '\\') {
				escape_state = B_TRUE;
				continue;
			}
			if (strchr(delimit, chr) != NULL)
				break;
			if (any_white && isspace(chr))
				break;
		}
		escape_state = B_FALSE;
		if (outlen > 0) {
			*outbuf++ = chr;
			outlen--;
		}
	}
	if (outlen != 1)
		instr--;
	if (escape_state)
		instr--;
	if (outlen > 0)
		*outbuf = '\0';
	return (instr);
}

char *
sl_to_str(const m_label_t *sl)
{
	char *sl_str = NULL;
	static char unknown_str[] = "UNKNOWN";

	if (sl == NULL)
		return (strdup(unknown_str));

	if ((label_to_str(sl, &sl_str, M_LABEL, DEF_NAMES) != 0) &&
	    (label_to_str(sl, &sl_str, M_INTERNAL, DEF_NAMES) != 0))
		return (strdup(unknown_str));

	return (sl_str);
}

static const char *rtsa_keywords[] = {
#define	SAK_MINSL	0
	"min_sl",
#define	SAK_MAXSL	1
	"max_sl",
#define	SAK_DOI		2
	"doi",
#define	SAK_CIPSO	3
	"cipso",
#define	SAK_SL		4
	"sl",
#define	SAK_INVAL	5
	NULL
};

const char *
rtsa_to_str(const struct rtsa_s *rtsa, char *line, size_t len)
{
	size_t slen;
	uint32_t mask, i;
	char *sl_str = NULL;

	slen = 0;
	*line = '\0';
	mask = rtsa->rtsa_mask;

	for (i = 1; mask != 0 && i != 0 && slen < len - 1; i <<= 1) {
		if (!(i & (RTSA_MINSL|RTSA_MAXSL|RTSA_DOI|RTSA_CIPSO)))
			continue;
		if (!(i & mask))
			continue;
		if (slen != 0)
			line[slen++] = ',';
		switch (i & mask) {
		case RTSA_MINSL:
			if ((mask & RTSA_MAXSL) &&
			    blequal(&rtsa->rtsa_slrange.lower_bound,
			    &rtsa->rtsa_slrange.upper_bound)) {

				sl_str =
				    sl_to_str(&rtsa->rtsa_slrange.lower_bound);
				slen += snprintf(line + slen, len - slen,
				    "sl=%s", sl_str);
				free(sl_str);
				sl_str = NULL;
				mask ^= RTSA_MAXSL;
				break;
			}
			sl_str = sl_to_str(&rtsa->rtsa_slrange.lower_bound);
			slen += snprintf(line + slen, len - slen, "min_sl=%s",
			    sl_str);
			free(sl_str);
			sl_str = NULL;
			break;
		case RTSA_MAXSL:
			sl_str = sl_to_str(&rtsa->rtsa_slrange.upper_bound);
			slen += snprintf(line + slen, len - slen, "max_sl=%s",
			    sl_str);
			free(sl_str);
			sl_str = NULL;
			break;
		case RTSA_DOI:
			slen += snprintf(line + slen, len - slen, "doi=%d",
			    rtsa->rtsa_doi);
			break;
		case RTSA_CIPSO:
			slen += snprintf(line + slen, len - slen, "cipso");
			break;
		}
	}

	return (line);
}

boolean_t
rtsa_keyword(const char *options, struct rtsa_s *sp, int *errp, char **errstrp)
{
	const char *valptr, *nxtopt;
	uint32_t mask = 0, doi;
	int key;
	m_label_t *min_sl = NULL, *max_sl = NULL;
	char attrbuf[MAX_ATTR_LEN];
	const char **keyword;
	int err;
	char *errstr, *cp;

	if (errp == NULL)
		errp = &err;
	if (errstrp == NULL)
		errstrp = &errstr;

	*errstrp = (char *)options;

	while (*options != '\0') {
		valptr = parse_entry(attrbuf, sizeof (attrbuf), options, ",=");

		if (attrbuf[0] == '\0') {
			*errstrp = (char *)options;
			*errp = LTSNET_ILL_ENTRY;
			goto out_err;
		}
		for (keyword = rtsa_keywords; *keyword != NULL; keyword++)
			if (strcmp(*keyword, attrbuf) == 0)
				break;
		if ((key = keyword - rtsa_keywords) == SAK_INVAL) {
			*errstrp = (char *)options;
			*errp = LTSNET_ILL_KEY;
			goto out_err;
		}
		if ((key == SAK_CIPSO && *valptr == '=') ||
		    (key != SAK_CIPSO && *valptr != '=')) {
			*errstrp = (char *)valptr;
			*errp = LTSNET_ILL_VALDELIM;
			goto out_err;
		}

		nxtopt = valptr;
		if (*valptr == '=') {
			valptr++;
			nxtopt = parse_entry(attrbuf, sizeof (attrbuf),
			    valptr, ",=");
			if (*nxtopt == '=') {
				*errstrp = (char *)nxtopt;
				*errp = LTSNET_ILL_KEYDELIM;
				goto out_err;
			}
		}
		if (*nxtopt == ',')
			nxtopt++;

		switch (key) {
		case SAK_MINSL:
			if (mask & RTSA_MINSL) {
				*errstrp = (char *)options;
				*errp = LTSNET_DUP_KEY;
				goto out_err;
			}
			m_label_free(min_sl);		/* in case of duplicate */
			min_sl = NULL;
			if (str_to_label(attrbuf, &min_sl, MAC_LABEL,
			    L_NO_CORRECTION, NULL) != 0) {
				*errstrp = (char *)valptr;
				*errp = LTSNET_ILL_LOWERBOUND;
				goto out_err;
			}
			mask |= RTSA_MINSL;
			break;

		case SAK_MAXSL:
			if (mask & RTSA_MAXSL) {
				*errstrp = (char *)options;
				*errp = LTSNET_DUP_KEY;
				goto out_err;
			}
			m_label_free(max_sl);		/* in case of duplicate */
			max_sl = NULL;
			if (str_to_label(attrbuf, &max_sl, MAC_LABEL,
			    L_NO_CORRECTION, NULL) != 0) {
				*errstrp = (char *)valptr;
				*errp = LTSNET_ILL_UPPERBOUND;
				goto out_err;
			}
			mask |= RTSA_MAXSL;
			break;

		case SAK_SL:
			if (mask & (RTSA_MAXSL|RTSA_MINSL)) {
				*errstrp = (char *)options;
				*errp = LTSNET_DUP_KEY;
				goto out_err;
			}
			m_label_free(min_sl);		/* in case of duplicate */
			min_sl = NULL;
			if (str_to_label(attrbuf, &min_sl, MAC_LABEL,
			    L_NO_CORRECTION, NULL) != 0) {
				*errstrp = (char *)valptr;
				*errp = LTSNET_ILL_LABEL;
				goto out_err;
			}
			*max_sl = *min_sl;
			mask |= (RTSA_MINSL | RTSA_MAXSL);
			break;

		case SAK_DOI:
			if (mask & RTSA_DOI) {
				*errstrp = (char *)options;
				*errp = LTSNET_DUP_KEY;
				goto out_err;
			}
			errno = 0;
			doi = strtoul(attrbuf, &cp, 0);
			if (doi == 0 || errno != 0 || *cp != '\0') {
				*errstrp = (char *)valptr;
				*errp = LTSNET_ILL_DOI;
				goto out_err;
			}
			mask |= RTSA_DOI;
			break;

		case SAK_CIPSO:
			if (mask & RTSA_CIPSO) {
				*errstrp = (char *)options;
				*errp = LTSNET_DUP_KEY;
				goto out_err;
			}
			mask |= RTSA_CIPSO;
			break;
		}

		options = nxtopt;
	}

	/* Defaults to CIPSO if not specified */
	mask |= RTSA_CIPSO;

	/* If RTSA_CIPSO is specified, RTSA_DOI must be specified */
	if (!(mask & RTSA_DOI)) {
		*errp = LTSNET_NO_DOI;
		goto out_err;
	}

	/* SL range must be specified */
	if (!(mask & (RTSA_MINSL|RTSA_MAXSL))) {
		*errp = LTSNET_NO_RANGE;
		goto out_err;
	}
	if (!(mask & RTSA_MINSL)) {
		*errp = LTSNET_NO_LOWERBOUND;
		goto out_err;
	}
	if (!(mask & RTSA_MAXSL)) {
		*errp = LTSNET_NO_UPPERBOUND;
		goto out_err;
	}

	/* SL range must have upper bound dominating lower bound */
	if (!bldominates(max_sl, min_sl)) {
		*errp = LTSNET_ILL_RANGE;
		goto out_err;
	}

	if (mask & RTSA_MINSL)
		sp->rtsa_slrange.lower_bound = *min_sl;
	if (mask & RTSA_MAXSL)
		sp->rtsa_slrange.upper_bound = *max_sl;
	if (mask & RTSA_DOI)
		sp->rtsa_doi = doi;
	sp->rtsa_mask = mask;

	m_label_free(min_sl);
	m_label_free(max_sl);

	return (B_TRUE);

out_err:
	m_label_free(min_sl);
	m_label_free(max_sl);

	return (B_FALSE);
}

/* Keep in sync with libtsnet.h */
static const char *tsol_errlist[] = {
	"No error",
	"System error",
	"Empty string or end of list",
	"Entry is malformed",
	"Missing name",
	"Missing attributes",
	"Illegal name",
	"Illegal keyword delimiter",
	"Unknown keyword",
	"Duplicate keyword",
	"Illegal value delimiter",
	"Missing host type",
	"Illegal host type",
	"Missing label",
	"Illegal label",
	"Missing label range",
	"Illegal label range",
	"No lower bound in range",
	"Illegal lower bound in range",
	"No upper bound in range",
	"Illegal upper bound in range",
	"Missing DOI",
	"Illegal DOI",
	"Too many entries in set",
	"Missing address/network",
	"Illegal address/network",
	"Illegal flag",
	"Illegal MLP specification",
	"Unacceptable keyword for type"
};
static const int tsol_nerr = sizeof (tsol_errlist) / sizeof (*tsol_errlist);

const char *
tsol_strerror(int libtserr, int errnoval)
{
	if (libtserr == LTSNET_SYSERR)
		return (strerror(errnoval));
	if (libtserr >= 0 && libtserr < tsol_nerr)
		return (gettext(tsol_errlist[libtserr]));
	return (gettext("Unknown error"));
}