xref: /dragonfly/sys/netgraph7/netgraph/ng_parse.c (revision fb151170)
1 /*
2  * ng_parse.c
3  */
4 
5 /*-
6  * Copyright (c) 1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41  * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.30 2007/06/23 00:02:20 mjacob Exp $
42  * $DragonFly: src/sys/netgraph7/ng_parse.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
43  */
44 
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/limits.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/ctype.h>
54 
55 #include <machine/stdarg.h>
56 
57 #include <net/ethernet.h>
58 
59 #include <netinet/in.h>
60 
61 #include <netgraph7/ng_message.h>
62 #include <netgraph7/netgraph.h>
63 #include <netgraph7/ng_parse.h>
64 
65 #ifdef NG_SEPARATE_MALLOC
66 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
67 #else
68 #define M_NETGRAPH_PARSE M_NETGRAPH
69 #endif
70 
71 /* Compute alignment for primitive integral types */
72 struct int16_temp {
73 	char	x;
74 	int16_t	y;
75 };
76 
77 struct int32_temp {
78 	char	x;
79 	int32_t	y;
80 };
81 
82 struct int64_temp {
83 	char	x;
84 	int64_t	y;
85 };
86 
87 #define INT8_ALIGNMENT		1
88 #define INT16_ALIGNMENT		((size_t)&((struct int16_temp *)0)->y)
89 #define INT32_ALIGNMENT		((size_t)&((struct int32_temp *)0)->y)
90 #define INT64_ALIGNMENT		((size_t)&((struct int64_temp *)0)->y)
91 
92 /* Output format for integral types */
93 #define INT_UNSIGNED		0
94 #define INT_SIGNED		1
95 #define INT_HEX			2
96 
97 /* Type of composite object: struct, array, or fixedarray */
98 enum comptype {
99 	CT_STRUCT,
100 	CT_ARRAY,
101 	CT_FIXEDARRAY,
102 };
103 
104 /* Composite types helper functions */
105 static int	ng_parse_composite(const struct ng_parse_type *type,
106 			const char *s, int *off, const u_char *start,
107 			u_char *const buf, int *buflen, enum comptype ctype);
108 static int	ng_unparse_composite(const struct ng_parse_type *type,
109 			const u_char *data, int *off, char *cbuf, int cbuflen,
110 			enum comptype ctype);
111 static int	ng_get_composite_elem_default(const struct ng_parse_type *type,
112 			int index, const u_char *start, u_char *buf,
113 			int *buflen, enum comptype ctype);
114 static int	ng_get_composite_len(const struct ng_parse_type *type,
115 			const u_char *start, const u_char *buf,
116 			enum comptype ctype);
117 static const	struct ng_parse_type *ng_get_composite_etype(const struct
118 			ng_parse_type *type, int index, enum comptype ctype);
119 static int	ng_parse_get_elem_pad(const struct ng_parse_type *type,
120 			int index, enum comptype ctype, int posn);
121 
122 /* Parsing helper functions */
123 static int	ng_parse_skip_value(const char *s, int off, int *lenp);
124 static int	ng_parse_append(char **cbufp, int *cbuflenp,
125 			const char *fmt, ...);
126 
127 /* Poor man's virtual method calls */
128 #define METHOD(t,m)	(ng_get_ ## m ## _method(t))
129 #define INVOKE(t,m)	(*METHOD(t,m))
130 
131 static ng_parse_t	*ng_get_parse_method(const struct ng_parse_type *t);
132 static ng_unparse_t	*ng_get_unparse_method(const struct ng_parse_type *t);
133 static ng_getDefault_t	*ng_get_getDefault_method(const
134 				struct ng_parse_type *t);
135 static ng_getAlign_t	*ng_get_getAlign_method(const struct ng_parse_type *t);
136 
137 #define ALIGNMENT(t)	(METHOD(t, getAlign) == NULL ? \
138 				0 : INVOKE(t, getAlign)(t))
139 
140 /************************************************************************
141 			PUBLIC FUNCTIONS
142  ************************************************************************/
143 
144 /*
145  * Convert an ASCII string to binary according to the supplied type descriptor
146  */
147 int
148 ng_parse(const struct ng_parse_type *type,
149 	const char *string, int *off, u_char *buf, int *buflen)
150 {
151 	return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
152 }
153 
154 /*
155  * Convert binary to an ASCII string according to the supplied type descriptor
156  */
157 int
158 ng_unparse(const struct ng_parse_type *type,
159 	const u_char *data, char *cbuf, int cbuflen)
160 {
161 	int off = 0;
162 
163 	return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
164 }
165 
166 /*
167  * Fill in the default value according to the supplied type descriptor
168  */
169 int
170 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
171 {
172 	ng_getDefault_t *const func = METHOD(type, getDefault);
173 
174 	if (func == NULL)
175 		return (EOPNOTSUPP);
176 	return (*func)(type, buf, buf, buflen);
177 }
178 
179 
180 /************************************************************************
181 			STRUCTURE TYPE
182  ************************************************************************/
183 
184 static int
185 ng_struct_parse(const struct ng_parse_type *type,
186 	const char *s, int *off, const u_char *const start,
187 	u_char *const buf, int *buflen)
188 {
189 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
190 }
191 
192 static int
193 ng_struct_unparse(const struct ng_parse_type *type,
194 	const u_char *data, int *off, char *cbuf, int cbuflen)
195 {
196 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
197 }
198 
199 static int
200 ng_struct_getDefault(const struct ng_parse_type *type,
201 	const u_char *const start, u_char *buf, int *buflen)
202 {
203 	int off = 0;
204 
205 	return ng_parse_composite(type,
206 	    "{}", &off, start, buf, buflen, CT_STRUCT);
207 }
208 
209 static int
210 ng_struct_getAlign(const struct ng_parse_type *type)
211 {
212 	const struct ng_parse_struct_field *field;
213 	int align = 0;
214 
215 	for (field = type->info; field->name != NULL; field++) {
216 		int falign = ALIGNMENT(field->type);
217 
218 		if (falign > align)
219 			align = falign;
220 	}
221 	return align;
222 }
223 
224 const struct ng_parse_type ng_parse_struct_type = {
225 	NULL,
226 	NULL,
227 	NULL,
228 	ng_struct_parse,
229 	ng_struct_unparse,
230 	ng_struct_getDefault,
231 	ng_struct_getAlign
232 };
233 
234 /************************************************************************
235 			FIXED LENGTH ARRAY TYPE
236  ************************************************************************/
237 
238 static int
239 ng_fixedarray_parse(const struct ng_parse_type *type,
240 	const char *s, int *off, const u_char *const start,
241 	u_char *const buf, int *buflen)
242 {
243 	return ng_parse_composite(type,
244 	    s, off, start, buf, buflen, CT_FIXEDARRAY);
245 }
246 
247 static int
248 ng_fixedarray_unparse(const struct ng_parse_type *type,
249 	const u_char *data, int *off, char *cbuf, int cbuflen)
250 {
251 	return ng_unparse_composite(type,
252 		data, off, cbuf, cbuflen, CT_FIXEDARRAY);
253 }
254 
255 static int
256 ng_fixedarray_getDefault(const struct ng_parse_type *type,
257 	const u_char *const start, u_char *buf, int *buflen)
258 {
259 	int off = 0;
260 
261 	return ng_parse_composite(type,
262 	    "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
263 }
264 
265 static int
266 ng_fixedarray_getAlign(const struct ng_parse_type *type)
267 {
268 	const struct ng_parse_fixedarray_info *fi = type->info;
269 
270 	return ALIGNMENT(fi->elementType);
271 }
272 
273 const struct ng_parse_type ng_parse_fixedarray_type = {
274 	NULL,
275 	NULL,
276 	NULL,
277 	ng_fixedarray_parse,
278 	ng_fixedarray_unparse,
279 	ng_fixedarray_getDefault,
280 	ng_fixedarray_getAlign
281 };
282 
283 /************************************************************************
284 			VARIABLE LENGTH ARRAY TYPE
285  ************************************************************************/
286 
287 static int
288 ng_array_parse(const struct ng_parse_type *type,
289 	const char *s, int *off, const u_char *const start,
290 	u_char *const buf, int *buflen)
291 {
292 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
293 }
294 
295 static int
296 ng_array_unparse(const struct ng_parse_type *type,
297 	const u_char *data, int *off, char *cbuf, int cbuflen)
298 {
299 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
300 }
301 
302 static int
303 ng_array_getDefault(const struct ng_parse_type *type,
304 	const u_char *const start, u_char *buf, int *buflen)
305 {
306 	int off = 0;
307 
308 	return ng_parse_composite(type,
309 	    "[]", &off, start, buf, buflen, CT_ARRAY);
310 }
311 
312 static int
313 ng_array_getAlign(const struct ng_parse_type *type)
314 {
315 	const struct ng_parse_array_info *ai = type->info;
316 
317 	return ALIGNMENT(ai->elementType);
318 }
319 
320 const struct ng_parse_type ng_parse_array_type = {
321 	NULL,
322 	NULL,
323 	NULL,
324 	ng_array_parse,
325 	ng_array_unparse,
326 	ng_array_getDefault,
327 	ng_array_getAlign
328 };
329 
330 /************************************************************************
331 				INT8 TYPE
332  ************************************************************************/
333 
334 static int
335 ng_int8_parse(const struct ng_parse_type *type,
336 	const char *s, int *off, const u_char *const start,
337 	u_char *const buf, int *buflen)
338 {
339 	long val;
340 	int8_t val8;
341 	char *eptr;
342 
343 	val = strtol(s + *off, &eptr, 0);
344 	if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
345 		return (EINVAL);
346 	*off = eptr - s;
347 	val8 = (int8_t)val;
348 	bcopy(&val8, buf, sizeof(int8_t));
349 	*buflen = sizeof(int8_t);
350 	return (0);
351 }
352 
353 static int
354 ng_int8_unparse(const struct ng_parse_type *type,
355 	const u_char *data, int *off, char *cbuf, int cbuflen)
356 {
357 	const char *fmt;
358 	int fval;
359 	int error;
360 	int8_t val;
361 
362 	bcopy(data + *off, &val, sizeof(int8_t));
363 	switch ((intptr_t)type->info) {
364 	case INT_SIGNED:
365 		fmt = "%d";
366 		fval = val;
367 		break;
368 	case INT_UNSIGNED:
369 		fmt = "%u";
370 		fval = (u_int8_t)val;
371 		break;
372 	case INT_HEX:
373 		fmt = "0x%x";
374 		fval = (u_int8_t)val;
375 		break;
376 	default:
377 		panic("%s: unknown type", __func__);
378 #ifdef	RESTARTABLE_PANICS
379 		return(0);
380 #endif
381 	}
382 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
383 		return (error);
384 	*off += sizeof(int8_t);
385 	return (0);
386 }
387 
388 static int
389 ng_int8_getDefault(const struct ng_parse_type *type,
390 	const u_char *const start, u_char *buf, int *buflen)
391 {
392 	int8_t val;
393 
394 	if (*buflen < sizeof(int8_t))
395 		return (ERANGE);
396 	val = 0;
397 	bcopy(&val, buf, sizeof(int8_t));
398 	*buflen = sizeof(int8_t);
399 	return (0);
400 }
401 
402 static int
403 ng_int8_getAlign(const struct ng_parse_type *type)
404 {
405 	return INT8_ALIGNMENT;
406 }
407 
408 const struct ng_parse_type ng_parse_int8_type = {
409 	NULL,
410 	(void *)INT_SIGNED,
411 	NULL,
412 	ng_int8_parse,
413 	ng_int8_unparse,
414 	ng_int8_getDefault,
415 	ng_int8_getAlign
416 };
417 
418 const struct ng_parse_type ng_parse_uint8_type = {
419 	&ng_parse_int8_type,
420 	(void *)INT_UNSIGNED
421 };
422 
423 const struct ng_parse_type ng_parse_hint8_type = {
424 	&ng_parse_int8_type,
425 	(void *)INT_HEX
426 };
427 
428 /************************************************************************
429 				INT16 TYPE
430  ************************************************************************/
431 
432 static int
433 ng_int16_parse(const struct ng_parse_type *type,
434 	const char *s, int *off, const u_char *const start,
435 	u_char *const buf, int *buflen)
436 {
437 	long val;
438 	int16_t val16;
439 	char *eptr;
440 
441 	val = strtol(s + *off, &eptr, 0);
442 	if (val < (int16_t)0x8000
443 	    || val > (u_int16_t)0xffff || eptr == s + *off)
444 		return (EINVAL);
445 	*off = eptr - s;
446 	val16 = (int16_t)val;
447 	bcopy(&val16, buf, sizeof(int16_t));
448 	*buflen = sizeof(int16_t);
449 	return (0);
450 }
451 
452 static int
453 ng_int16_unparse(const struct ng_parse_type *type,
454 	const u_char *data, int *off, char *cbuf, int cbuflen)
455 {
456 	const char *fmt;
457 	int fval;
458 	int error;
459 	int16_t val;
460 
461 	bcopy(data + *off, &val, sizeof(int16_t));
462 	switch ((intptr_t)type->info) {
463 	case INT_SIGNED:
464 		fmt = "%d";
465 		fval = val;
466 		break;
467 	case INT_UNSIGNED:
468 		fmt = "%u";
469 		fval = (u_int16_t)val;
470 		break;
471 	case INT_HEX:
472 		fmt = "0x%x";
473 		fval = (u_int16_t)val;
474 		break;
475 	default:
476 		panic("%s: unknown type", __func__);
477 #ifdef	RESTARTABLE_PANICS
478 		return(0);
479 #endif
480 	}
481 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
482 		return (error);
483 	*off += sizeof(int16_t);
484 	return (0);
485 }
486 
487 static int
488 ng_int16_getDefault(const struct ng_parse_type *type,
489 	const u_char *const start, u_char *buf, int *buflen)
490 {
491 	int16_t val;
492 
493 	if (*buflen < sizeof(int16_t))
494 		return (ERANGE);
495 	val = 0;
496 	bcopy(&val, buf, sizeof(int16_t));
497 	*buflen = sizeof(int16_t);
498 	return (0);
499 }
500 
501 static int
502 ng_int16_getAlign(const struct ng_parse_type *type)
503 {
504 	return INT16_ALIGNMENT;
505 }
506 
507 const struct ng_parse_type ng_parse_int16_type = {
508 	NULL,
509 	(void *)INT_SIGNED,
510 	NULL,
511 	ng_int16_parse,
512 	ng_int16_unparse,
513 	ng_int16_getDefault,
514 	ng_int16_getAlign
515 };
516 
517 const struct ng_parse_type ng_parse_uint16_type = {
518 	&ng_parse_int16_type,
519 	(void *)INT_UNSIGNED
520 };
521 
522 const struct ng_parse_type ng_parse_hint16_type = {
523 	&ng_parse_int16_type,
524 	(void *)INT_HEX
525 };
526 
527 /************************************************************************
528 				INT32 TYPE
529  ************************************************************************/
530 
531 static int
532 ng_int32_parse(const struct ng_parse_type *type,
533 	const char *s, int *off, const u_char *const start,
534 	u_char *const buf, int *buflen)
535 {
536 	long val;			/* assumes long is at least 32 bits */
537 	int32_t val32;
538 	char *eptr;
539 
540 	if ((intptr_t)type->info == INT_SIGNED)
541 		val = strtol(s + *off, &eptr, 0);
542 	else
543 		val = strtoul(s + *off, &eptr, 0);
544 	if (val < (int32_t)0x80000000
545 	    || val > (u_int32_t)0xffffffff || eptr == s + *off)
546 		return (EINVAL);
547 	*off = eptr - s;
548 	val32 = (int32_t)val;
549 	bcopy(&val32, buf, sizeof(int32_t));
550 	*buflen = sizeof(int32_t);
551 	return (0);
552 }
553 
554 static int
555 ng_int32_unparse(const struct ng_parse_type *type,
556 	const u_char *data, int *off, char *cbuf, int cbuflen)
557 {
558 	const char *fmt;
559 	long fval;
560 	int error;
561 	int32_t val;
562 
563 	bcopy(data + *off, &val, sizeof(int32_t));
564 	switch ((intptr_t)type->info) {
565 	case INT_SIGNED:
566 		fmt = "%ld";
567 		fval = val;
568 		break;
569 	case INT_UNSIGNED:
570 		fmt = "%lu";
571 		fval = (u_int32_t)val;
572 		break;
573 	case INT_HEX:
574 		fmt = "0x%lx";
575 		fval = (u_int32_t)val;
576 		break;
577 	default:
578 		panic("%s: unknown type", __func__);
579 #ifdef	RESTARTABLE_PANICS
580 		return(0);
581 #endif
582 	}
583 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
584 		return (error);
585 	*off += sizeof(int32_t);
586 	return (0);
587 }
588 
589 static int
590 ng_int32_getDefault(const struct ng_parse_type *type,
591 	const u_char *const start, u_char *buf, int *buflen)
592 {
593 	int32_t val;
594 
595 	if (*buflen < sizeof(int32_t))
596 		return (ERANGE);
597 	val = 0;
598 	bcopy(&val, buf, sizeof(int32_t));
599 	*buflen = sizeof(int32_t);
600 	return (0);
601 }
602 
603 static int
604 ng_int32_getAlign(const struct ng_parse_type *type)
605 {
606 	return INT32_ALIGNMENT;
607 }
608 
609 const struct ng_parse_type ng_parse_int32_type = {
610 	NULL,
611 	(void *)INT_SIGNED,
612 	NULL,
613 	ng_int32_parse,
614 	ng_int32_unparse,
615 	ng_int32_getDefault,
616 	ng_int32_getAlign
617 };
618 
619 const struct ng_parse_type ng_parse_uint32_type = {
620 	&ng_parse_int32_type,
621 	(void *)INT_UNSIGNED
622 };
623 
624 const struct ng_parse_type ng_parse_hint32_type = {
625 	&ng_parse_int32_type,
626 	(void *)INT_HEX
627 };
628 
629 /************************************************************************
630 				INT64 TYPE
631  ************************************************************************/
632 
633 static int
634 ng_int64_parse(const struct ng_parse_type *type,
635 	const char *s, int *off, const u_char *const start,
636 	u_char *const buf, int *buflen)
637 {
638 	quad_t val;
639 	int64_t val64;
640 	char *eptr;
641 
642 	val = strtoq(s + *off, &eptr, 0);
643 	if (eptr == s + *off)
644 		return (EINVAL);
645 	*off = eptr - s;
646 	val64 = (int64_t)val;
647 	bcopy(&val64, buf, sizeof(int64_t));
648 	*buflen = sizeof(int64_t);
649 	return (0);
650 }
651 
652 static int
653 ng_int64_unparse(const struct ng_parse_type *type,
654 	const u_char *data, int *off, char *cbuf, int cbuflen)
655 {
656 	const char *fmt;
657 	long long fval;
658 	int64_t val;
659 	int error;
660 
661 	bcopy(data + *off, &val, sizeof(int64_t));
662 	switch ((intptr_t)type->info) {
663 	case INT_SIGNED:
664 		fmt = "%lld";
665 		fval = val;
666 		break;
667 	case INT_UNSIGNED:
668 		fmt = "%llu";
669 		fval = (u_int64_t)val;
670 		break;
671 	case INT_HEX:
672 		fmt = "0x%llx";
673 		fval = (u_int64_t)val;
674 		break;
675 	default:
676 		panic("%s: unknown type", __func__);
677 #ifdef	RESTARTABLE_PANICS
678 		return(0);
679 #endif
680 	}
681 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
682 		return (error);
683 	*off += sizeof(int64_t);
684 	return (0);
685 }
686 
687 static int
688 ng_int64_getDefault(const struct ng_parse_type *type,
689 	const u_char *const start, u_char *buf, int *buflen)
690 {
691 	int64_t val;
692 
693 	if (*buflen < sizeof(int64_t))
694 		return (ERANGE);
695 	val = 0;
696 	bcopy(&val, buf, sizeof(int64_t));
697 	*buflen = sizeof(int64_t);
698 	return (0);
699 }
700 
701 static int
702 ng_int64_getAlign(const struct ng_parse_type *type)
703 {
704 	return INT64_ALIGNMENT;
705 }
706 
707 const struct ng_parse_type ng_parse_int64_type = {
708 	NULL,
709 	(void *)INT_SIGNED,
710 	NULL,
711 	ng_int64_parse,
712 	ng_int64_unparse,
713 	ng_int64_getDefault,
714 	ng_int64_getAlign
715 };
716 
717 const struct ng_parse_type ng_parse_uint64_type = {
718 	&ng_parse_int64_type,
719 	(void *)INT_UNSIGNED
720 };
721 
722 const struct ng_parse_type ng_parse_hint64_type = {
723 	&ng_parse_int64_type,
724 	(void *)INT_HEX
725 };
726 
727 /************************************************************************
728 				STRING TYPE
729  ************************************************************************/
730 
731 static int
732 ng_string_parse(const struct ng_parse_type *type,
733 	const char *s, int *off, const u_char *const start,
734 	u_char *const buf, int *buflen)
735 {
736 	char *sval;
737 	int len;
738 	int slen;
739 
740 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
741 		return (EINVAL);
742 	*off += len;
743 	bcopy(sval, buf, slen + 1);
744 	kfree(sval, M_NETGRAPH_PARSE);
745 	*buflen = slen + 1;
746 	return (0);
747 }
748 
749 static int
750 ng_string_unparse(const struct ng_parse_type *type,
751 	const u_char *data, int *off, char *cbuf, int cbuflen)
752 {
753 	const char *const raw = (const char *)data + *off;
754 	char *const s = ng_encode_string(raw, strlen(raw));
755 	int error;
756 
757 	if (s == NULL)
758 		return (ENOMEM);
759 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
760 		kfree(s, M_NETGRAPH_PARSE);
761 		return (error);
762 	}
763 	*off += strlen(raw) + 1;
764 	kfree(s, M_NETGRAPH_PARSE);
765 	return (0);
766 }
767 
768 static int
769 ng_string_getDefault(const struct ng_parse_type *type,
770 	const u_char *const start, u_char *buf, int *buflen)
771 {
772 
773 	if (*buflen < 1)
774 		return (ERANGE);
775 	buf[0] = (u_char)'\0';
776 	*buflen = 1;
777 	return (0);
778 }
779 
780 const struct ng_parse_type ng_parse_string_type = {
781 	NULL,
782 	NULL,
783 	NULL,
784 	ng_string_parse,
785 	ng_string_unparse,
786 	ng_string_getDefault,
787 	NULL
788 };
789 
790 /************************************************************************
791 			FIXED BUFFER STRING TYPE
792  ************************************************************************/
793 
794 static int
795 ng_fixedstring_parse(const struct ng_parse_type *type,
796 	const char *s, int *off, const u_char *const start,
797 	u_char *const buf, int *buflen)
798 {
799 	const struct ng_parse_fixedstring_info *const fi = type->info;
800 	char *sval;
801 	int len;
802 	int slen;
803 
804 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
805 		return (EINVAL);
806 	if (slen + 1 > fi->bufSize) {
807 		kfree(sval, M_NETGRAPH_PARSE);
808 		return (E2BIG);
809 	}
810 	*off += len;
811 	bcopy(sval, buf, slen);
812 	kfree(sval, M_NETGRAPH_PARSE);
813 	bzero(buf + slen, fi->bufSize - slen);
814 	*buflen = fi->bufSize;
815 	return (0);
816 }
817 
818 static int
819 ng_fixedstring_unparse(const struct ng_parse_type *type,
820 	const u_char *data, int *off, char *cbuf, int cbuflen)
821 {
822 	const struct ng_parse_fixedstring_info *const fi = type->info;
823 	int error, temp = *off;
824 
825 	if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
826 		return (error);
827 	*off += fi->bufSize;
828 	return (0);
829 }
830 
831 static int
832 ng_fixedstring_getDefault(const struct ng_parse_type *type,
833 	const u_char *const start, u_char *buf, int *buflen)
834 {
835 	const struct ng_parse_fixedstring_info *const fi = type->info;
836 
837 	if (*buflen < fi->bufSize)
838 		return (ERANGE);
839 	bzero(buf, fi->bufSize);
840 	*buflen = fi->bufSize;
841 	return (0);
842 }
843 
844 const struct ng_parse_type ng_parse_fixedstring_type = {
845 	NULL,
846 	NULL,
847 	NULL,
848 	ng_fixedstring_parse,
849 	ng_fixedstring_unparse,
850 	ng_fixedstring_getDefault,
851 	NULL
852 };
853 
854 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
855 	NG_NODESIZ
856 };
857 const struct ng_parse_type ng_parse_nodebuf_type = {
858 	&ng_parse_fixedstring_type,
859 	&ng_parse_nodebuf_info
860 };
861 
862 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
863 	NG_HOOKSIZ
864 };
865 const struct ng_parse_type ng_parse_hookbuf_type = {
866 	&ng_parse_fixedstring_type,
867 	&ng_parse_hookbuf_info
868 };
869 
870 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
871 	NG_PATHSIZ
872 };
873 const struct ng_parse_type ng_parse_pathbuf_type = {
874 	&ng_parse_fixedstring_type,
875 	&ng_parse_pathbuf_info
876 };
877 
878 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
879 	NG_TYPESIZ
880 };
881 const struct ng_parse_type ng_parse_typebuf_type = {
882 	&ng_parse_fixedstring_type,
883 	&ng_parse_typebuf_info
884 };
885 
886 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
887 	NG_CMDSTRSIZ
888 };
889 const struct ng_parse_type ng_parse_cmdbuf_type = {
890 	&ng_parse_fixedstring_type,
891 	&ng_parse_cmdbuf_info
892 };
893 
894 /************************************************************************
895 			EXPLICITLY SIZED STRING TYPE
896  ************************************************************************/
897 
898 static int
899 ng_sizedstring_parse(const struct ng_parse_type *type,
900 	const char *s, int *off, const u_char *const start,
901 	u_char *const buf, int *buflen)
902 {
903 	char *sval;
904 	int len;
905 	int slen;
906 
907 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
908 		return (EINVAL);
909 	if (slen > USHRT_MAX) {
910 		kfree(sval, M_NETGRAPH_PARSE);
911 		return (EINVAL);
912 	}
913 	*off += len;
914 	*((u_int16_t *)buf) = (u_int16_t)slen;
915 	bcopy(sval, buf + 2, slen);
916 	kfree(sval, M_NETGRAPH_PARSE);
917 	*buflen = 2 + slen;
918 	return (0);
919 }
920 
921 static int
922 ng_sizedstring_unparse(const struct ng_parse_type *type,
923 	const u_char *data, int *off, char *cbuf, int cbuflen)
924 {
925 	const char *const raw = (const char *)data + *off + 2;
926 	const int slen = *((const u_int16_t *)(data + *off));
927 	char *const s = ng_encode_string(raw, slen);
928 	int error;
929 
930 	if (s == NULL)
931 		return (ENOMEM);
932 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
933 		kfree(s, M_NETGRAPH_PARSE);
934 		return (error);
935 	}
936 	kfree(s, M_NETGRAPH_PARSE);
937 	*off += slen + 2;
938 	return (0);
939 }
940 
941 static int
942 ng_sizedstring_getDefault(const struct ng_parse_type *type,
943 	const u_char *const start, u_char *buf, int *buflen)
944 {
945 	if (*buflen < 2)
946 		return (ERANGE);
947 	bzero(buf, 2);
948 	*buflen = 2;
949 	return (0);
950 }
951 
952 const struct ng_parse_type ng_parse_sizedstring_type = {
953 	NULL,
954 	NULL,
955 	NULL,
956 	ng_sizedstring_parse,
957 	ng_sizedstring_unparse,
958 	ng_sizedstring_getDefault,
959 	NULL
960 };
961 
962 /************************************************************************
963 			IP ADDRESS TYPE
964  ************************************************************************/
965 
966 static int
967 ng_ipaddr_parse(const struct ng_parse_type *type,
968 	const char *s, int *off, const u_char *const start,
969 	u_char *const buf, int *buflen)
970 {
971 	int i, error;
972 
973 	for (i = 0; i < 4; i++) {
974 		if ((error = ng_int8_parse(&ng_parse_int8_type,
975 		    s, off, start, buf + i, buflen)) != 0)
976 			return (error);
977 		if (i < 3 && s[*off] != '.')
978 			return (EINVAL);
979 		(*off)++;
980 	}
981 	*buflen = 4;
982 	return (0);
983 }
984 
985 static int
986 ng_ipaddr_unparse(const struct ng_parse_type *type,
987 	const u_char *data, int *off, char *cbuf, int cbuflen)
988 {
989 	struct in_addr ip;
990 	int error;
991 
992 	bcopy(data + *off, &ip, sizeof(ip));
993 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
994 	    ((u_char *)&ip)[0], ((u_char *)&ip)[1],
995 	    ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
996 		return (error);
997 	*off += sizeof(ip);
998 	return (0);
999 }
1000 
1001 static int
1002 ng_ipaddr_getDefault(const struct ng_parse_type *type,
1003 	const u_char *const start, u_char *buf, int *buflen)
1004 {
1005 	struct in_addr ip = { 0 };
1006 
1007 	if (*buflen < sizeof(ip))
1008 		return (ERANGE);
1009 	bcopy(&ip, buf, sizeof(ip));
1010 	*buflen = sizeof(ip);
1011 	return (0);
1012 }
1013 
1014 const struct ng_parse_type ng_parse_ipaddr_type = {
1015 	NULL,
1016 	NULL,
1017 	NULL,
1018 	ng_ipaddr_parse,
1019 	ng_ipaddr_unparse,
1020 	ng_ipaddr_getDefault,
1021 	ng_int32_getAlign
1022 };
1023 
1024 /************************************************************************
1025 			ETHERNET ADDRESS TYPE
1026  ************************************************************************/
1027 
1028 static int
1029 ng_enaddr_parse(const struct ng_parse_type *type,
1030 	const char *s, int *const off, const u_char *const start,
1031 	u_char *const buf, int *const buflen)
1032 {
1033 	char *eptr;
1034 	u_long val;
1035 	int i;
1036 
1037 	if (*buflen < ETHER_ADDR_LEN)
1038 		return (ERANGE);
1039 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1040 		val = strtoul(s + *off, &eptr, 16);
1041 		if (val > 0xff || eptr == s + *off)
1042 			return (EINVAL);
1043 		buf[i] = (u_char)val;
1044 		*off = (eptr - s);
1045 		if (i < ETHER_ADDR_LEN - 1) {
1046 			if (*eptr != ':')
1047 				return (EINVAL);
1048 			(*off)++;
1049 		}
1050 	}
1051 	*buflen = ETHER_ADDR_LEN;
1052 	return (0);
1053 }
1054 
1055 static int
1056 ng_enaddr_unparse(const struct ng_parse_type *type,
1057 	const u_char *data, int *off, char *cbuf, int cbuflen)
1058 {
1059 	int len;
1060 
1061 	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1062 	    data[*off], data[*off + 1], data[*off + 2],
1063 	    data[*off + 3], data[*off + 4], data[*off + 5]);
1064 	if (len >= cbuflen)
1065 		return (ERANGE);
1066 	*off += ETHER_ADDR_LEN;
1067 	return (0);
1068 }
1069 
1070 const struct ng_parse_type ng_parse_enaddr_type = {
1071 	NULL,
1072 	NULL,
1073 	NULL,
1074 	ng_enaddr_parse,
1075 	ng_enaddr_unparse,
1076 	NULL,
1077 	0
1078 };
1079 
1080 /************************************************************************
1081 			BYTE ARRAY TYPE
1082  ************************************************************************/
1083 
1084 /* Get the length of a byte array */
1085 static int
1086 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1087 	const u_char *start, const u_char *buf)
1088 {
1089 	ng_parse_array_getLength_t *const getLength = type->private;
1090 
1091 	return (*getLength)(type, start, buf);
1092 }
1093 
1094 /* Byte array element type is hex int8 */
1095 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1096 	&ng_parse_hint8_type,
1097 	&ng_parse_bytearray_subtype_getLength,
1098 	NULL
1099 };
1100 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1101 	&ng_parse_array_type,
1102 	&ng_parse_bytearray_subtype_info
1103 };
1104 
1105 static int
1106 ng_bytearray_parse(const struct ng_parse_type *type,
1107 	const char *s, int *off, const u_char *const start,
1108 	u_char *const buf, int *buflen)
1109 {
1110 	char *str;
1111 	int toklen;
1112 	int slen;
1113 
1114 	/* We accept either an array of bytes or a string constant */
1115 	if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1116 		ng_parse_array_getLength_t *const getLength = type->info;
1117 		int arraylen;
1118 
1119 		arraylen = (*getLength)(type, start, buf);
1120 		if (arraylen > *buflen) {
1121 			kfree(str, M_NETGRAPH_PARSE);
1122 			return (ERANGE);
1123 		}
1124 		if (slen > arraylen) {
1125 			kfree(str, M_NETGRAPH_PARSE);
1126 			return (E2BIG);
1127 		}
1128 		bcopy(str, buf, slen);
1129 		bzero(buf + slen, arraylen - slen);
1130 		kfree(str, M_NETGRAPH_PARSE);
1131 		*off += toklen;
1132 		*buflen = arraylen;
1133 		return (0);
1134 	} else {
1135 		struct ng_parse_type subtype;
1136 
1137 		subtype = ng_parse_bytearray_subtype;
1138 		*(const void **)&subtype.private = type->info;
1139 		return ng_array_parse(&subtype, s, off, start, buf, buflen);
1140 	}
1141 }
1142 
1143 static int
1144 ng_bytearray_unparse(const struct ng_parse_type *type,
1145 	const u_char *data, int *off, char *cbuf, int cbuflen)
1146 {
1147 	struct ng_parse_type subtype;
1148 
1149 	subtype = ng_parse_bytearray_subtype;
1150 	*(const void **)&subtype.private = type->info;
1151 	return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1152 }
1153 
1154 static int
1155 ng_bytearray_getDefault(const struct ng_parse_type *type,
1156 	const u_char *const start, u_char *buf, int *buflen)
1157 {
1158 	struct ng_parse_type subtype;
1159 
1160 	subtype = ng_parse_bytearray_subtype;
1161 	*(const void **)&subtype.private = type->info;
1162 	return ng_array_getDefault(&subtype, start, buf, buflen);
1163 }
1164 
1165 const struct ng_parse_type ng_parse_bytearray_type = {
1166 	NULL,
1167 	NULL,
1168 	NULL,
1169 	ng_bytearray_parse,
1170 	ng_bytearray_unparse,
1171 	ng_bytearray_getDefault,
1172 	NULL
1173 };
1174 
1175 /************************************************************************
1176 			STRUCT NG_MESG TYPE
1177  ************************************************************************/
1178 
1179 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1180 static int
1181 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1182 	const u_char *start, const u_char *buf)
1183 {
1184 	const struct ng_mesg *msg;
1185 
1186 	msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1187 	return msg->header.arglen;
1188 }
1189 
1190 /* Type for the variable length data portion of a struct ng_mesg */
1191 static const struct ng_parse_type ng_msg_data_type = {
1192 	&ng_parse_bytearray_type,
1193 	&ng_parse_ng_mesg_getLength
1194 };
1195 
1196 /* Type for the entire struct ng_mesg header with data section */
1197 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1198 	= NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1199 const struct ng_parse_type ng_parse_ng_mesg_type = {
1200 	&ng_parse_struct_type,
1201 	&ng_parse_ng_mesg_type_fields,
1202 };
1203 
1204 /************************************************************************
1205 			COMPOSITE HELPER ROUTINES
1206  ************************************************************************/
1207 
1208 /*
1209  * Convert a structure or array from ASCII to binary
1210  */
1211 static int
1212 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1213 	int *off, const u_char *const start, u_char *const buf, int *buflen,
1214 	const enum comptype ctype)
1215 {
1216 	const int num = ng_get_composite_len(type, start, buf, ctype);
1217 	int nextIndex = 0;		/* next implicit array index */
1218 	u_int index;			/* field or element index */
1219 	int *foff;			/* field value offsets in string */
1220 	int align, len, blen, error = 0;
1221 
1222 	/* Initialize */
1223 	foff = kmalloc(num * sizeof(*foff), M_NETGRAPH_PARSE,
1224 		       M_WAITOK | M_NULLOK | M_ZERO);
1225 	if (foff == NULL) {
1226 		error = ENOMEM;
1227 		goto done;
1228 	}
1229 
1230 	/* Get opening brace/bracket */
1231 	if (ng_parse_get_token(s, off, &len)
1232 	    != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1233 		error = EINVAL;
1234 		goto done;
1235 	}
1236 	*off += len;
1237 
1238 	/* Get individual element value positions in the string */
1239 	for (;;) {
1240 		enum ng_parse_token tok;
1241 
1242 		/* Check for closing brace/bracket */
1243 		tok = ng_parse_get_token(s, off, &len);
1244 		if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1245 			*off += len;
1246 			break;
1247 		}
1248 
1249 		/* For arrays, the 'name' (ie, index) is optional, so
1250 		   distinguish name from values by seeing if the next
1251 		   token is an equals sign */
1252 		if (ctype != CT_STRUCT) {
1253 			int len2, off2;
1254 			char *eptr;
1255 
1256 			/* If an opening brace/bracket, index is implied */
1257 			if (tok == T_LBRACE || tok == T_LBRACKET) {
1258 				index = nextIndex++;
1259 				goto gotIndex;
1260 			}
1261 
1262 			/* Might be an index, might be a value, either way... */
1263 			if (tok != T_WORD) {
1264 				error = EINVAL;
1265 				goto done;
1266 			}
1267 
1268 			/* If no equals sign follows, index is implied */
1269 			off2 = *off + len;
1270 			if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1271 				index = nextIndex++;
1272 				goto gotIndex;
1273 			}
1274 
1275 			/* Index was specified explicitly; parse it */
1276 			index = (u_int)strtoul(s + *off, &eptr, 0);
1277 			if (index < 0 || eptr - (s + *off) != len) {
1278 				error = EINVAL;
1279 				goto done;
1280 			}
1281 			nextIndex = index + 1;
1282 			*off += len + len2;
1283 		} else {			/* a structure field */
1284 			const struct ng_parse_struct_field *const
1285 			    fields = type->info;
1286 
1287 			/* Find the field by name (required) in field list */
1288 			if (tok != T_WORD) {
1289 				error = EINVAL;
1290 				goto done;
1291 			}
1292 			for (index = 0; index < num; index++) {
1293 				const struct ng_parse_struct_field *const
1294 				    field = &fields[index];
1295 
1296 				if (strncmp(&s[*off], field->name, len) == 0
1297 				    && field->name[len] == '\0')
1298 					break;
1299 			}
1300 			if (index == num) {
1301 				error = ENOENT;
1302 				goto done;
1303 			}
1304 			*off += len;
1305 
1306 			/* Get equals sign */
1307 			if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1308 				error = EINVAL;
1309 				goto done;
1310 			}
1311 			*off += len;
1312 		}
1313 gotIndex:
1314 
1315 		/* Check array index */
1316 		if (index >= num) {
1317 			error = E2BIG;
1318 			goto done;
1319 		}
1320 
1321 		/* Save value's position and skip over it for now */
1322 		if (foff[index] != 0) {
1323 			error = EALREADY;		/* duplicate */
1324 			goto done;
1325 		}
1326 		while (isspace(s[*off]))
1327 			(*off)++;
1328 		foff[index] = *off;
1329 		if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1330 			goto done;
1331 		*off += len;
1332 	}
1333 
1334 	/* Now build binary structure from supplied values and defaults */
1335 	for (blen = index = 0; index < num; index++) {
1336 		const struct ng_parse_type *const
1337 		    etype = ng_get_composite_etype(type, index, ctype);
1338 		int k, pad, vlen;
1339 
1340 		/* Zero-pad any alignment bytes */
1341 		pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1342 		for (k = 0; k < pad; k++) {
1343 			if (blen >= *buflen) {
1344 				error = ERANGE;
1345 				goto done;
1346 			}
1347 			buf[blen++] = 0;
1348 		}
1349 
1350 		/* Get value */
1351 		vlen = *buflen - blen;
1352 		if (foff[index] == 0) {		/* use default value */
1353 			error = ng_get_composite_elem_default(type, index,
1354 			    start, buf + blen, &vlen, ctype);
1355 		} else {			/* parse given value */
1356 			*off = foff[index];
1357 			error = INVOKE(etype, parse)(etype,
1358 			    s, off, start, buf + blen, &vlen);
1359 		}
1360 		if (error != 0)
1361 			goto done;
1362 		blen += vlen;
1363 	}
1364 
1365 	/* Make total composite structure size a multiple of its alignment */
1366 	if ((align = ALIGNMENT(type)) != 0) {
1367 		while (blen % align != 0) {
1368 			if (blen >= *buflen) {
1369 				error = ERANGE;
1370 				goto done;
1371 			}
1372 			buf[blen++] = 0;
1373 		}
1374 	}
1375 
1376 	/* Done */
1377 	*buflen = blen;
1378 done:
1379 	if (foff != NULL)
1380 		kfree(foff, M_NETGRAPH_PARSE);
1381 	return (error);
1382 }
1383 
1384 /*
1385  * Convert an array or structure from binary to ASCII
1386  */
1387 static int
1388 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1389 	int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1390 {
1391 	const struct ng_mesg *const hdr
1392 	    = (const struct ng_mesg *)(data - sizeof(*hdr));
1393 	const int num = ng_get_composite_len(type, data, data + *off, ctype);
1394 	const int workSize = 20 * 1024;		/* XXX hard coded constant */
1395 	int nextIndex = 0, didOne = 0;
1396 	int error, index;
1397 	u_char *workBuf;
1398 
1399 	/* Get workspace for checking default values */
1400 	workBuf = kmalloc(workSize, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1401 	if (workBuf == NULL)
1402 		return (ENOMEM);
1403 
1404 	/* Opening brace/bracket */
1405 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1406 	    (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1407 		goto fail;
1408 
1409 	/* Do each item */
1410 	for (index = 0; index < num; index++) {
1411 		const struct ng_parse_type *const
1412 		    etype = ng_get_composite_etype(type, index, ctype);
1413 
1414 		/* Skip any alignment pad bytes */
1415 		*off += ng_parse_get_elem_pad(type, index, ctype, *off);
1416 
1417 		/*
1418 		 * See if element is equal to its default value; skip if so.
1419 		 * Copy struct ng_mesg header for types that peek into it.
1420 		 */
1421 		if (sizeof(*hdr) + *off < workSize) {
1422 			int tempsize = workSize - sizeof(*hdr) - *off;
1423 
1424 			bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1425 			if (ng_get_composite_elem_default(type, index, workBuf
1426 			      + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1427 			      &tempsize, ctype) == 0
1428 			    && bcmp(workBuf + sizeof(*hdr) + *off,
1429 			      data + *off, tempsize) == 0) {
1430 				*off += tempsize;
1431 				continue;
1432 			}
1433 		}
1434 
1435 		/* Print name= */
1436 		if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1437 			goto fail;
1438 		if (ctype != CT_STRUCT) {
1439 			if (index != nextIndex) {
1440 				nextIndex = index;
1441 				if ((error = ng_parse_append(&cbuf,
1442 				    &cbuflen, "%d=", index)) != 0)
1443 					goto fail;
1444 			}
1445 			nextIndex++;
1446 		} else {
1447 			const struct ng_parse_struct_field *const
1448 			    fields = type->info;
1449 
1450 			if ((error = ng_parse_append(&cbuf,
1451 			    &cbuflen, "%s=", fields[index].name)) != 0)
1452 				goto fail;
1453 		}
1454 
1455 		/* Print value */
1456 		if ((error = INVOKE(etype, unparse)
1457 		    (etype, data, off, cbuf, cbuflen)) != 0) {
1458 			kfree(workBuf, M_NETGRAPH_PARSE);
1459 			return (error);
1460 		}
1461 		cbuflen -= strlen(cbuf);
1462 		cbuf += strlen(cbuf);
1463 		didOne = 1;
1464 	}
1465 
1466 	/* Closing brace/bracket */
1467 	error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1468 	    didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1469 
1470 fail:
1471 	/* Clean up after failure */
1472 	kfree(workBuf, M_NETGRAPH_PARSE);
1473 	return (error);
1474 }
1475 
1476 /*
1477  * Generate the default value for an element of an array or structure
1478  * Returns EOPNOTSUPP if default value is unspecified.
1479  */
1480 static int
1481 ng_get_composite_elem_default(const struct ng_parse_type *type,
1482 	int index, const u_char *const start, u_char *buf, int *buflen,
1483 	const enum comptype ctype)
1484 {
1485 	const struct ng_parse_type *etype;
1486 	ng_getDefault_t *func;
1487 
1488 	switch (ctype) {
1489 	case CT_STRUCT:
1490 		break;
1491 	case CT_ARRAY:
1492 	    {
1493 		const struct ng_parse_array_info *const ai = type->info;
1494 
1495 		if (ai->getDefault != NULL) {
1496 			return (*ai->getDefault)(type,
1497 			    index, start, buf, buflen);
1498 		}
1499 		break;
1500 	    }
1501 	case CT_FIXEDARRAY:
1502 	    {
1503 		const struct ng_parse_fixedarray_info *const fi = type->info;
1504 
1505 		if (*fi->getDefault != NULL) {
1506 			return (*fi->getDefault)(type,
1507 			    index, start, buf, buflen);
1508 		}
1509 		break;
1510 	    }
1511 	default:
1512 	    panic("%s", __func__);
1513 	}
1514 
1515 	/* Default to element type default */
1516 	etype = ng_get_composite_etype(type, index, ctype);
1517 	func = METHOD(etype, getDefault);
1518 	if (func == NULL)
1519 		return (EOPNOTSUPP);
1520 	return (*func)(etype, start, buf, buflen);
1521 }
1522 
1523 /*
1524  * Get the number of elements in a struct, variable or fixed array.
1525  */
1526 static int
1527 ng_get_composite_len(const struct ng_parse_type *type,
1528 	const u_char *const start, const u_char *buf,
1529 	const enum comptype ctype)
1530 {
1531 	switch (ctype) {
1532 	case CT_STRUCT:
1533 	    {
1534 		const struct ng_parse_struct_field *const fields = type->info;
1535 		int numFields = 0;
1536 
1537 		for (numFields = 0; ; numFields++) {
1538 			const struct ng_parse_struct_field *const
1539 				fi = &fields[numFields];
1540 
1541 			if (fi->name == NULL)
1542 				break;
1543 		}
1544 		return (numFields);
1545 	    }
1546 	case CT_ARRAY:
1547 	    {
1548 		const struct ng_parse_array_info *const ai = type->info;
1549 
1550 		return (*ai->getLength)(type, start, buf);
1551 	    }
1552 	case CT_FIXEDARRAY:
1553 	    {
1554 		const struct ng_parse_fixedarray_info *const fi = type->info;
1555 
1556 		return fi->length;
1557 	    }
1558 	default:
1559 	    panic("%s", __func__);
1560 	}
1561 	return (0);
1562 }
1563 
1564 /*
1565  * Return the type of the index'th element of a composite structure
1566  */
1567 static const struct ng_parse_type *
1568 ng_get_composite_etype(const struct ng_parse_type *type,
1569 	int index, const enum comptype ctype)
1570 {
1571 	const struct ng_parse_type *etype = NULL;
1572 
1573 	switch (ctype) {
1574 	case CT_STRUCT:
1575 	    {
1576 		const struct ng_parse_struct_field *const fields = type->info;
1577 
1578 		etype = fields[index].type;
1579 		break;
1580 	    }
1581 	case CT_ARRAY:
1582 	    {
1583 		const struct ng_parse_array_info *const ai = type->info;
1584 
1585 		etype = ai->elementType;
1586 		break;
1587 	    }
1588 	case CT_FIXEDARRAY:
1589 	    {
1590 		const struct ng_parse_fixedarray_info *const fi = type->info;
1591 
1592 		etype = fi->elementType;
1593 		break;
1594 	    }
1595 	default:
1596 	    panic("%s", __func__);
1597 	}
1598 	return (etype);
1599 }
1600 
1601 /*
1602  * Get the number of bytes to skip to align for the next
1603  * element in a composite structure.
1604  */
1605 static int
1606 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1607 	int index, enum comptype ctype, int posn)
1608 {
1609 	const struct ng_parse_type *const
1610 	    etype = ng_get_composite_etype(type, index, ctype);
1611 	int align;
1612 
1613 	/* Get element's alignment, and possibly override */
1614 	align = ALIGNMENT(etype);
1615 	if (ctype == CT_STRUCT) {
1616 		const struct ng_parse_struct_field *const fields = type->info;
1617 
1618 		if (fields[index].alignment != 0)
1619 			align = fields[index].alignment;
1620 	}
1621 
1622 	/* Return number of bytes to skip to align */
1623 	return (align ? (align - (posn % align)) % align : 0);
1624 }
1625 
1626 /************************************************************************
1627 			PARSING HELPER ROUTINES
1628  ************************************************************************/
1629 
1630 /*
1631  * Append to a fixed length string buffer.
1632  */
1633 static int
1634 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1635 {
1636 	va_list args;
1637 	int len;
1638 
1639 	va_start(args, fmt);
1640 	len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1641 	va_end(args);
1642 	if (len >= *cbuflenp)
1643 		return ERANGE;
1644 	*cbufp += len;
1645 	*cbuflenp -= len;
1646 
1647 	return (0);
1648 }
1649 
1650 /*
1651  * Skip over a value
1652  */
1653 static int
1654 ng_parse_skip_value(const char *s, int off0, int *lenp)
1655 {
1656 	int len, nbracket, nbrace;
1657 	int off = off0;
1658 
1659 	len = nbracket = nbrace = 0;
1660 	do {
1661 		switch (ng_parse_get_token(s, &off, &len)) {
1662 		case T_LBRACKET:
1663 			nbracket++;
1664 			break;
1665 		case T_LBRACE:
1666 			nbrace++;
1667 			break;
1668 		case T_RBRACKET:
1669 			if (nbracket-- == 0)
1670 				return (EINVAL);
1671 			break;
1672 		case T_RBRACE:
1673 			if (nbrace-- == 0)
1674 				return (EINVAL);
1675 			break;
1676 		case T_EOF:
1677 			return (EINVAL);
1678 		default:
1679 			break;
1680 		}
1681 		off += len;
1682 	} while (nbracket > 0 || nbrace > 0);
1683 	*lenp = off - off0;
1684 	return (0);
1685 }
1686 
1687 /*
1688  * Find the next token in the string, starting at offset *startp.
1689  * Returns the token type, with *startp pointing to the first char
1690  * and *lenp the length.
1691  */
1692 enum ng_parse_token
1693 ng_parse_get_token(const char *s, int *startp, int *lenp)
1694 {
1695 	char *t;
1696 	int i;
1697 
1698 	while (isspace(s[*startp]))
1699 		(*startp)++;
1700 	switch (s[*startp]) {
1701 	case '\0':
1702 		*lenp = 0;
1703 		return T_EOF;
1704 	case '{':
1705 		*lenp = 1;
1706 		return T_LBRACE;
1707 	case '}':
1708 		*lenp = 1;
1709 		return T_RBRACE;
1710 	case '[':
1711 		*lenp = 1;
1712 		return T_LBRACKET;
1713 	case ']':
1714 		*lenp = 1;
1715 		return T_RBRACKET;
1716 	case '=':
1717 		*lenp = 1;
1718 		return T_EQUALS;
1719 	case '"':
1720 		if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1721 			return T_ERROR;
1722 		kfree(t, M_NETGRAPH_PARSE);
1723 		return T_STRING;
1724 	default:
1725 		for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1726 		    && s[i] != '{' && s[i] != '}' && s[i] != '['
1727 		    && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1728 			;
1729 		*lenp = i - *startp;
1730 		return T_WORD;
1731 	}
1732 }
1733 
1734 /*
1735  * Get a string token, which must be enclosed in double quotes.
1736  * The normal C backslash escapes are recognized.
1737  */
1738 char *
1739 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1740 {
1741 	char *cbuf, *p;
1742 	int start, off;
1743 	int slen;
1744 
1745 	while (isspace(s[*startp]))
1746 		(*startp)++;
1747 	start = *startp;
1748 	if (s[*startp] != '"')
1749 		return (NULL);
1750 	cbuf = kmalloc(strlen(s + start), M_NETGRAPH_PARSE,
1751 		       M_WAITOK | M_NULLOK);
1752 	if (cbuf == NULL)
1753 		return (NULL);
1754 	strcpy(cbuf, s + start + 1);
1755 	for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1756 		if (*p == '"') {
1757 			*p = '\0';
1758 			*lenp = off + 1;
1759 			if (slenp != NULL)
1760 				*slenp = slen;
1761 			return (cbuf);
1762 		} else if (p[0] == '\\' && p[1] != '\0') {
1763 			int x, k;
1764 			char *v;
1765 
1766 			strcpy(p, p + 1);
1767 			v = p;
1768 			switch (*p) {
1769 			case 't':
1770 				*v = '\t';
1771 				off++;
1772 				continue;
1773 			case 'n':
1774 				*v = '\n';
1775 				off++;
1776 				continue;
1777 			case 'r':
1778 				*v = '\r';
1779 				off++;
1780 				continue;
1781 			case 'v':
1782 				*v =  '\v';
1783 				off++;
1784 				continue;
1785 			case 'f':
1786 				*v =  '\f';
1787 				off++;
1788 				continue;
1789 			case '"':
1790 				*v =  '"';
1791 				off++;
1792 				continue;
1793 			case '0': case '1': case '2': case '3':
1794 			case '4': case '5': case '6': case '7':
1795 				for (x = k = 0;
1796 				    k < 3 && *v >= '0' && *v <= '7'; v++) {
1797 					x = (x << 3) + (*v - '0');
1798 					off++;
1799 				}
1800 				*--v = (char)x;
1801 				break;
1802 			case 'x':
1803 				for (v++, x = k = 0;
1804 				    k < 2 && isxdigit(*v); v++) {
1805 					x = (x << 4) + (isdigit(*v) ?
1806 					      (*v - '0') :
1807 					      (tolower(*v) - 'a' + 10));
1808 					off++;
1809 				}
1810 				*--v = (char)x;
1811 				break;
1812 			default:
1813 				continue;
1814 			}
1815 			strcpy(p, v);
1816 		}
1817 	}
1818 	kfree(cbuf, M_NETGRAPH_PARSE);
1819 	return (NULL);		/* no closing quote */
1820 }
1821 
1822 /*
1823  * Encode a string so it can be safely put in double quotes.
1824  * Caller must free the result. Exactly "slen" characters
1825  * are encoded.
1826  */
1827 char *
1828 ng_encode_string(const char *raw, int slen)
1829 {
1830 	char *cbuf;
1831 	int off = 0;
1832 	int i;
1833 
1834 	cbuf = kmalloc(strlen(raw) * 4 + 3, M_NETGRAPH_PARSE,
1835 		       M_WAITOK | M_NULLOK);
1836 	if (cbuf == NULL)
1837 		return (NULL);
1838 	cbuf[off++] = '"';
1839 	for (i = 0; i < slen; i++, raw++) {
1840 		switch (*raw) {
1841 		case '\t':
1842 			cbuf[off++] = '\\';
1843 			cbuf[off++] = 't';
1844 			break;
1845 		case '\f':
1846 			cbuf[off++] = '\\';
1847 			cbuf[off++] = 'f';
1848 			break;
1849 		case '\n':
1850 			cbuf[off++] = '\\';
1851 			cbuf[off++] = 'n';
1852 			break;
1853 		case '\r':
1854 			cbuf[off++] = '\\';
1855 			cbuf[off++] = 'r';
1856 			break;
1857 		case '\v':
1858 			cbuf[off++] = '\\';
1859 			cbuf[off++] = 'v';
1860 			break;
1861 		case '"':
1862 		case '\\':
1863 			cbuf[off++] = '\\';
1864 			cbuf[off++] = *raw;
1865 			break;
1866 		default:
1867 			if (*raw < 0x20 || *raw > 0x7e) {
1868 				off += sprintf(cbuf + off,
1869 				    "\\x%02x", (u_char)*raw);
1870 				break;
1871 			}
1872 			cbuf[off++] = *raw;
1873 			break;
1874 		}
1875 	}
1876 	cbuf[off++] = '"';
1877 	cbuf[off] = '\0';
1878 	return (cbuf);
1879 }
1880 
1881 /************************************************************************
1882 			VIRTUAL METHOD LOOKUP
1883  ************************************************************************/
1884 
1885 static ng_parse_t *
1886 ng_get_parse_method(const struct ng_parse_type *t)
1887 {
1888 	while (t != NULL && t->parse == NULL)
1889 		t = t->supertype;
1890 	return (t ? t->parse : NULL);
1891 }
1892 
1893 static ng_unparse_t *
1894 ng_get_unparse_method(const struct ng_parse_type *t)
1895 {
1896 	while (t != NULL && t->unparse == NULL)
1897 		t = t->supertype;
1898 	return (t ? t->unparse : NULL);
1899 }
1900 
1901 static ng_getDefault_t *
1902 ng_get_getDefault_method(const struct ng_parse_type *t)
1903 {
1904 	while (t != NULL && t->getDefault == NULL)
1905 		t = t->supertype;
1906 	return (t ? t->getDefault : NULL);
1907 }
1908 
1909 static ng_getAlign_t *
1910 ng_get_getAlign_method(const struct ng_parse_type *t)
1911 {
1912 	while (t != NULL && t->getAlign == NULL)
1913 		t = t->supertype;
1914 	return (t ? t->getAlign : NULL);
1915 }
1916 
1917