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