xref: /onnv-gate/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision 6482:45eb7438d352)
15772Sas200622 /*
25772Sas200622  * CDDL HEADER START
35772Sas200622  *
45772Sas200622  * The contents of this file are subject to the terms of the
55772Sas200622  * Common Development and Distribution License (the "License").
65772Sas200622  * You may not use this file except in compliance with the License.
75772Sas200622  *
85772Sas200622  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95772Sas200622  * or http://www.opensolaris.org/os/licensing.
105772Sas200622  * See the License for the specific language governing permissions
115772Sas200622  * and limitations under the License.
125772Sas200622  *
135772Sas200622  * When distributing Covered Code, include this CDDL HEADER in each
145772Sas200622  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155772Sas200622  * If applicable, add the following below this CDDL HEADER, with the
165772Sas200622  * fields enclosed by brackets "[]" replaced with your own identifying
175772Sas200622  * information: Portions Copyright [yyyy] [name of copyright owner]
185772Sas200622  *
195772Sas200622  * CDDL HEADER END
205772Sas200622  */
215772Sas200622 /*
22*6482Samw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235772Sas200622  * Use is subject to license terms.
245772Sas200622  */
255772Sas200622 
265772Sas200622 #pragma ident	"%Z%%M%	%I%	%E% SMI"
275772Sas200622 
285772Sas200622 /*
295772Sas200622  * MLRPC server-side NDR stream (PDU) operations. Stream operations
305772Sas200622  * should return TRUE (non-zero) on success or FALSE (zero or a null
315772Sas200622  * pointer) on failure. When an operation returns FALSE, including
325772Sas200622  * mlndo_malloc() returning NULL, it should set the mlnds->error to
335772Sas200622  * indicate what went wrong.
345772Sas200622  *
355772Sas200622  * When available, the relevant ndr_reference is passed to the
365772Sas200622  * operation but keep in mind that it may be a null pointer.
375772Sas200622  *
385772Sas200622  * Functions mlndo_get_pdu(), mlndo_put_pdu(), and mlndo_pad_pdu()
395772Sas200622  * must never grow the PDU data. A request for out-of-bounds data is
405772Sas200622  * an error. The swap_bytes flag is 1 if NDR knows that the byte-
415772Sas200622  * order in the PDU is different from the local system.
425772Sas200622  */
435772Sas200622 
445772Sas200622 #include <sys/types.h>
455772Sas200622 #include <stdarg.h>
465772Sas200622 #include <ctype.h>
475772Sas200622 #include <stdio.h>
485772Sas200622 #include <stdlib.h>
495772Sas200622 #include <strings.h>
505772Sas200622 #include <string.h>
515772Sas200622 #include <assert.h>
525772Sas200622 
535772Sas200622 #include <smbsrv/libsmb.h>
545772Sas200622 #include <smbsrv/mlrpc.h>
555772Sas200622 #include <smbsrv/ndr.h>
565772Sas200622 #include <smbsrv/ntstatus.h>
575772Sas200622 
585772Sas200622 #define	NDOBUFSZ		128
595772Sas200622 
605772Sas200622 #define	NDR_PDU_BLOCK_SIZE	(4*1024)
615772Sas200622 #define	NDR_PDU_BLOCK_MASK	(NDR_PDU_BLOCK_SIZE - 1)
625772Sas200622 #define	NDR_PDU_ALIGN(N) \
635772Sas200622 	(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
645772Sas200622 #define	NDR_PDU_MAX_SIZE		(64*1024*1024)
655772Sas200622 
665772Sas200622 static char *mlndo_malloc(struct mlndr_stream *, unsigned,
675772Sas200622     struct ndr_reference *);
685772Sas200622 static int mlndo_free(struct mlndr_stream *, char *, struct ndr_reference *);
695772Sas200622 static int mlndo_grow_pdu(struct mlndr_stream *, unsigned long,
705772Sas200622     struct ndr_reference *);
715772Sas200622 static int mlndo_pad_pdu(struct mlndr_stream *, unsigned long, unsigned long,
725772Sas200622     struct ndr_reference *);
735772Sas200622 static int mlndo_get_pdu(struct mlndr_stream *, unsigned long, unsigned long,
745772Sas200622     char *, int, struct ndr_reference *);
755772Sas200622 static int mlndo_put_pdu(struct mlndr_stream *, unsigned long, unsigned long,
765772Sas200622     char *, int, struct ndr_reference *);
775772Sas200622 static void mlndo_tattle(struct mlndr_stream *, char *, struct ndr_reference *);
785772Sas200622 static void mlndo_tattle_error(struct mlndr_stream *, struct ndr_reference *);
795772Sas200622 static int mlndo_reset(struct mlndr_stream *);
805772Sas200622 static void mlndo_destruct(struct mlndr_stream *);
815772Sas200622 static void mlndo_hexfmt(uint8_t *, int, int, char *, int);
825772Sas200622 
835772Sas200622 /*
845772Sas200622  * The mlndr stream operations table.
855772Sas200622  */
865772Sas200622 static struct mlndr_stream_ops mlnds_ops = {
875772Sas200622     mlndo_malloc,
885772Sas200622     mlndo_free,
895772Sas200622     mlndo_grow_pdu,
905772Sas200622     mlndo_pad_pdu,
915772Sas200622     mlndo_get_pdu,
925772Sas200622     mlndo_put_pdu,
935772Sas200622     mlndo_tattle,
945772Sas200622     mlndo_tattle_error,
955772Sas200622     mlndo_reset,
965772Sas200622     mlndo_destruct
975772Sas200622 };
985772Sas200622 
995772Sas200622 /*
1005772Sas200622  * mlnds_bswap
1015772Sas200622  *
1025772Sas200622  * Copies len bytes from src to dst such that dst contains the bytes
1035772Sas200622  * from src in reverse order.
1045772Sas200622  *
1055772Sas200622  * We expect to be dealing with bytes, words, dwords etc. So the
1065772Sas200622  * length must be non-zero and a power of 2.
1075772Sas200622  */
1085772Sas200622 void
1095772Sas200622 mlnds_bswap(void *srcbuf, void *dstbuf, size_t len)
1105772Sas200622 {
1115772Sas200622 	uint8_t *src = (uint8_t *)srcbuf;
1125772Sas200622 	uint8_t *dst = (uint8_t *)dstbuf;
1135772Sas200622 
1145772Sas200622 	if ((len != 0) && ((len & (len - 1)) == 0)) {
1155772Sas200622 		src += len;
1165772Sas200622 
1175772Sas200622 		while (len--)
1185772Sas200622 			*dst++ = *(--src);
1195772Sas200622 	}
1205772Sas200622 }
1215772Sas200622 
1225772Sas200622 /*
1235772Sas200622  * mlnds_initialize
1245772Sas200622  *
1255772Sas200622  * Initialize a stream. Sets up the PDU parameters and assigns the stream
1265772Sas200622  * operations and the reference to the heap. An external heap is provided
1275772Sas200622  * to the stream, rather than each stream creating its own heap.
1285772Sas200622  */
1295772Sas200622 int
1305772Sas200622 mlnds_initialize(struct mlndr_stream *mlnds, unsigned pdu_size_hint,
1315772Sas200622     int composite_op, mlrpc_heap_t *heap)
1325772Sas200622 {
1335772Sas200622 	unsigned size;
1345772Sas200622 
1355772Sas200622 	assert(mlnds);
1365772Sas200622 	assert(heap);
1375772Sas200622 
1385772Sas200622 	bzero(mlnds, sizeof (*mlnds));
1395772Sas200622 
1405772Sas200622 	if (pdu_size_hint > NDR_PDU_MAX_SIZE)
1415772Sas200622 		return (0);
1425772Sas200622 
1435772Sas200622 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
1445772Sas200622 	mlnds->pdu_base_addr = malloc(size);
1455772Sas200622 	assert(mlnds->pdu_base_addr);
1465772Sas200622 
1475772Sas200622 	mlnds->pdu_max_size = size;
1485772Sas200622 	mlnds->pdu_size = 0;
1495772Sas200622 	mlnds->pdu_base_offset = (unsigned long)mlnds->pdu_base_addr;
1505772Sas200622 
1515772Sas200622 	mlnds->mlndo = &mlnds_ops;
1525772Sas200622 	mlnds->heap = (struct mlrpc_heap *)heap;
1535772Sas200622 
1545772Sas200622 	mlnds->m_op = composite_op & 0x0F;
1555772Sas200622 	mlnds->dir  = composite_op & 0xF0;
1565772Sas200622 
1575772Sas200622 	mlnds->outer_queue_tailp = &mlnds->outer_queue_head;
1585772Sas200622 	return (1);
1595772Sas200622 }
1605772Sas200622 
161*6482Samw int
162*6482Samw mlnds_finalize(struct mlndr_stream *mlnds, uint8_t *buf, uint32_t buflen)
163*6482Samw {
164*6482Samw 	ndr_frag_t *frag;
165*6482Samw 	uint8_t *pdu = mlnds->pdu_base_addr;
166*6482Samw 	uint32_t size = 0;
167*6482Samw 
168*6482Samw 	for (frag = mlnds->head; frag; frag = frag->next)
169*6482Samw 		size += frag->len;
170*6482Samw 
171*6482Samw 	if (size == 0 || size >= NDR_PDU_MAX_SIZE || size > buflen)
172*6482Samw 		return (0);
173*6482Samw 
174*6482Samw 	for (frag = mlnds->head; frag; frag = frag->next) {
175*6482Samw 		bcopy(frag->buf, buf, frag->len);
176*6482Samw 		buf += frag->len;
177*6482Samw 	}
178*6482Samw 
179*6482Samw 	return (size);
180*6482Samw }
181*6482Samw 
1825772Sas200622 /*
1835772Sas200622  * mlnds_destruct
1845772Sas200622  *
1855772Sas200622  * Destroy a stream. This is an external interface to provide access to
1865772Sas200622  * the stream's destruct operation.
1875772Sas200622  */
1885772Sas200622 void
1895772Sas200622 mlnds_destruct(struct mlndr_stream *mlnds)
1905772Sas200622 {
1915772Sas200622 	MLNDS_DESTRUCT(mlnds);
1925772Sas200622 }
1935772Sas200622 
1945772Sas200622 /*
1955772Sas200622  * mlndo_malloc
1965772Sas200622  *
1975772Sas200622  * Allocate memory from the stream heap.
1985772Sas200622  */
1995772Sas200622 /*ARGSUSED*/
2005772Sas200622 static char *
2015772Sas200622 mlndo_malloc(struct mlndr_stream *mlnds, unsigned len,
2025772Sas200622     struct ndr_reference *ref)
2035772Sas200622 {
2045772Sas200622 	return (mlrpc_heap_malloc((mlrpc_heap_t *)mlnds->heap, len));
2055772Sas200622 }
2065772Sas200622 
2075772Sas200622 /*
2085772Sas200622  * mlndo_free
2095772Sas200622  *
2105772Sas200622  * Always succeeds: cannot free individual stream allocations.
2115772Sas200622  */
2125772Sas200622 /*ARGSUSED*/
2135772Sas200622 static int
2145772Sas200622 mlndo_free(struct mlndr_stream *mlnds, char *p, struct ndr_reference *ref)
2155772Sas200622 {
2165772Sas200622 	return (1);
2175772Sas200622 }
2185772Sas200622 
2195772Sas200622 /*
2205772Sas200622  * mlndo_grow_pdu
2215772Sas200622  *
2225772Sas200622  * This is the only place that should change the size of the PDU. If the
2235772Sas200622  * desired offset is beyond the current PDU size, we realloc the PDU
2245772Sas200622  * buffer to accommodate the request. For efficiency, the PDU is always
2255772Sas200622  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2265772Sas200622  * beyond NDR_PDU_MAX_SIZE are rejected.
2275772Sas200622  *
2285772Sas200622  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2295772Sas200622  */
2305772Sas200622 static int
2315772Sas200622 mlndo_grow_pdu(struct mlndr_stream *mlnds, unsigned long want_end_offset,
2325772Sas200622     struct ndr_reference *ref)
2335772Sas200622 {
2345772Sas200622 	unsigned char *pdu_addr;
2355772Sas200622 	unsigned pdu_max_size;
2365772Sas200622 
2375772Sas200622 	mlndo_printf(mlnds, ref, "grow %d", want_end_offset);
2385772Sas200622 
2395772Sas200622 	pdu_max_size = mlnds->pdu_max_size;
2405772Sas200622 
2415772Sas200622 	if (want_end_offset > pdu_max_size) {
2425772Sas200622 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2435772Sas200622 
2445772Sas200622 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2455772Sas200622 			return (0);
2465772Sas200622 
2475772Sas200622 		pdu_addr = realloc(mlnds->pdu_base_addr, pdu_max_size);
2485772Sas200622 		if (pdu_addr == 0)
2495772Sas200622 			return (0);
2505772Sas200622 
2515772Sas200622 		mlnds->pdu_max_size = pdu_max_size;
2525772Sas200622 		mlnds->pdu_base_addr = pdu_addr;
2535772Sas200622 		mlnds->pdu_base_offset = (unsigned long)pdu_addr;
2545772Sas200622 	}
2555772Sas200622 
2565772Sas200622 	mlnds->pdu_size = want_end_offset;
2575772Sas200622 	return (1);
2585772Sas200622 }
2595772Sas200622 
2605772Sas200622 static int
2615772Sas200622 mlndo_pad_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
2625772Sas200622     unsigned long n_bytes, struct ndr_reference *ref)
2635772Sas200622 {
2645772Sas200622 	unsigned char *data;
2655772Sas200622 
2665772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
2675772Sas200622 	data += pdu_offset;
2685772Sas200622 
2695772Sas200622 	mlndo_printf(mlnds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
2705772Sas200622 
2715772Sas200622 	bzero(data, n_bytes);
2725772Sas200622 	return (1);
2735772Sas200622 }
2745772Sas200622 
2755772Sas200622 /*
2765772Sas200622  * mlndo_get_pdu
2775772Sas200622  *
2785772Sas200622  * The swap flag is 1 if NDR knows that the byte-order in the PDU
2795772Sas200622  * is different from the local system.
2805772Sas200622  *
2815772Sas200622  * Returns 1 on success or 0 to indicate failure.
2825772Sas200622  */
2835772Sas200622 static int
2845772Sas200622 mlndo_get_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
2855772Sas200622     unsigned long n_bytes, char *buf, int swap_bytes,
2865772Sas200622     struct ndr_reference *ref)
2875772Sas200622 {
2885772Sas200622 	unsigned char *data;
2895772Sas200622 	char hexbuf[NDOBUFSZ];
2905772Sas200622 
2915772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
2925772Sas200622 	data += pdu_offset;
2935772Sas200622 
2945772Sas200622 	mlndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
2955772Sas200622 
2965772Sas200622 	mlndo_printf(mlnds, ref, "get %d@%-3d = %s",
2975772Sas200622 	    n_bytes, pdu_offset, hexbuf);
2985772Sas200622 
2995772Sas200622 	if (!swap_bytes)
3005772Sas200622 		bcopy(data, buf, n_bytes);
3015772Sas200622 	else
3025772Sas200622 		mlnds_bswap(data, (unsigned char *)buf, n_bytes);
3035772Sas200622 
3045772Sas200622 	return (1);
3055772Sas200622 }
3065772Sas200622 
3075772Sas200622 /*
3085772Sas200622  * mlndo_put_pdu
3095772Sas200622  *
3105772Sas200622  * This is a receiver makes right protocol. So we do not need
3115772Sas200622  * to be concerned about the byte-order of an outgoing PDU.
3125772Sas200622  */
3135772Sas200622 /*ARGSUSED*/
3145772Sas200622 static int
3155772Sas200622 mlndo_put_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
3165772Sas200622     unsigned long n_bytes, char *buf, int swap_bytes,
3175772Sas200622     struct ndr_reference *ref)
3185772Sas200622 {
3195772Sas200622 	unsigned char *data;
3205772Sas200622 	char hexbuf[NDOBUFSZ];
3215772Sas200622 
3225772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
3235772Sas200622 	data += pdu_offset;
3245772Sas200622 
3255772Sas200622 	mlndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3265772Sas200622 
3275772Sas200622 	mlndo_printf(mlnds, ref, "put %d@%-3d = %s",
3285772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3295772Sas200622 
3305772Sas200622 	bcopy(buf, data, n_bytes);
3315772Sas200622 	return (1);
3325772Sas200622 }
3335772Sas200622 
3345772Sas200622 static void
3355772Sas200622 mlndo_tattle(struct mlndr_stream *mlnds, char *what,
3365772Sas200622     struct ndr_reference *ref)
3375772Sas200622 {
3385772Sas200622 	mlndo_printf(mlnds, ref, what);
3395772Sas200622 }
3405772Sas200622 
3415772Sas200622 static void
3425772Sas200622 mlndo_tattle_error(struct mlndr_stream *mlnds, struct ndr_reference *ref)
3435772Sas200622 {
3445772Sas200622 	unsigned char *data;
3455772Sas200622 	char hexbuf[NDOBUFSZ];
3465772Sas200622 
3475772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
3485772Sas200622 	if (ref)
3495772Sas200622 		data += ref->pdu_offset;
3505772Sas200622 	else
3515772Sas200622 		data += mlnds->pdu_scan_offset;
3525772Sas200622 
3535772Sas200622 	mlndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
3545772Sas200622 
3555772Sas200622 	mlndo_printf(mlnds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
3565772Sas200622 	    mlnds->error, mlnds->error_ref, mlnds->pdu_scan_offset,
3575772Sas200622 	    mlnds->pdu_size, mlnds->pdu_max_size);
3585772Sas200622 	mlndo_printf(mlnds, ref, "      %s", hexbuf);
3595772Sas200622 }
3605772Sas200622 
3615772Sas200622 /*
3625772Sas200622  * mlndo_reset
3635772Sas200622  *
3645772Sas200622  * Reset a stream: zap the outer_queue. We don't need to tamper
3655772Sas200622  * with the stream heap: it's handled externally to the stream.
3665772Sas200622  */
3675772Sas200622 static int
3685772Sas200622 mlndo_reset(struct mlndr_stream *mlnds)
3695772Sas200622 {
3705772Sas200622 	mlndo_printf(mlnds, 0, "reset");
3715772Sas200622 
3725772Sas200622 	mlnds->pdu_size = 0;
3735772Sas200622 	mlnds->pdu_scan_offset = 0;
3745772Sas200622 	mlnds->outer_queue_head = 0;
3755772Sas200622 	mlnds->outer_current = 0;
3765772Sas200622 	mlnds->outer_queue_tailp = &mlnds->outer_queue_head;
3775772Sas200622 
3785772Sas200622 	return (1);
3795772Sas200622 }
3805772Sas200622 
3815772Sas200622 /*
3825772Sas200622  * mlndo_destruct
3835772Sas200622  *
384*6482Samw  * Destruct a stream: zap the outer_queue.
385*6482Samw  * Note: heap management (creation/destruction) is external to the stream.
3865772Sas200622  */
3875772Sas200622 static void
3885772Sas200622 mlndo_destruct(struct mlndr_stream *mlnds)
3895772Sas200622 {
390*6482Samw 	ndr_frag_t *frag;
391*6482Samw 
3925772Sas200622 	mlndo_printf(mlnds, 0, "destruct");
3935772Sas200622 
394*6482Samw 	if (mlnds->pdu_base_addr != NULL) {
3955772Sas200622 		free(mlnds->pdu_base_addr);
396*6482Samw 		mlnds->pdu_base_addr = NULL;
3975772Sas200622 		mlnds->pdu_base_offset = 0;
3985772Sas200622 	}
3995772Sas200622 
400*6482Samw 	while ((frag = mlnds->head) != NULL) {
401*6482Samw 		mlnds->head = frag->next;
402*6482Samw 		free(frag);
403*6482Samw 	}
404*6482Samw 
405*6482Samw 	mlnds->head = NULL;
406*6482Samw 	mlnds->tail = NULL;
4075772Sas200622 	mlnds->outer_queue_head = 0;
4085772Sas200622 	mlnds->outer_current = 0;
4095772Sas200622 	mlnds->outer_queue_tailp = &mlnds->outer_queue_head;
4105772Sas200622 }
4115772Sas200622 
4125772Sas200622 /*
4135772Sas200622  * Printf style formatting for NDR operations.
4145772Sas200622  */
4155772Sas200622 void
4165772Sas200622 mlndo_printf(struct mlndr_stream *mlnds, struct ndr_reference *ref,
4175772Sas200622     const char *fmt, ...)
4185772Sas200622 {
4195772Sas200622 	va_list ap;
4205772Sas200622 	char buf[NDOBUFSZ];
4215772Sas200622 
4225772Sas200622 	va_start(ap, fmt);
4235772Sas200622 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4245772Sas200622 	va_end(ap);
4255772Sas200622 
4265772Sas200622 	if (mlnds)
4275772Sas200622 		mlndo_fmt(mlnds, ref, buf);
4285772Sas200622 	else
4295772Sas200622 		mlndo_trace(buf);
4305772Sas200622 }
4315772Sas200622 
4325772Sas200622 /*
4335772Sas200622  * Main output formatter for NDR operations.
4345772Sas200622  *
4355772Sas200622  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
4365772Sas200622  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
4375772Sas200622  *
4385772Sas200622  *	U       Marshalling flag (M=marshal, U=unmarshal)
4395772Sas200622  *	I       Direction flag (I=in, O=out)
4405772Sas200622  *	...     Field name
4415772Sas200622  *	get     PDU operation (get or put)
4425772Sas200622  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
4435772Sas200622  *	{05}    Value
4445772Sas200622  */
4455772Sas200622 void
4465772Sas200622 mlndo_fmt(struct mlndr_stream *mlnds, struct ndr_reference *ref, char *note)
4475772Sas200622 {
4485772Sas200622 	struct ndr_reference *p;
4495772Sas200622 	int			indent;
4505772Sas200622 	char			ref_name[NDOBUFSZ];
4515772Sas200622 	char			buf[NDOBUFSZ];
4525772Sas200622 	int			m_op_c = '?', dir_c = '?';
4535772Sas200622 
4545772Sas200622 	switch (mlnds->m_op) {
4555772Sas200622 	case 0:				m_op_c = '-';	break;
4565772Sas200622 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
4575772Sas200622 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
4585772Sas200622 	default:			m_op_c = '?';	break;
4595772Sas200622 	}
4605772Sas200622 
4615772Sas200622 	switch (mlnds->dir) {
4625772Sas200622 	case 0:				dir_c = '-';	break;
4635772Sas200622 	case NDR_DIR_IN:		dir_c = 'I';	break;
4645772Sas200622 	case NDR_DIR_OUT:		dir_c = 'O';	break;
4655772Sas200622 	default:			dir_c = '?';	break;
4665772Sas200622 	}
4675772Sas200622 
4685772Sas200622 	for (indent = 0, p = ref; p; p = p->enclosing)
4695772Sas200622 		indent++;
4705772Sas200622 
4715772Sas200622 	if (ref && ref->name) {
4725772Sas200622 		if (*ref->name == '[' && ref->enclosing) {
4735772Sas200622 			indent--;
4745772Sas200622 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
4755772Sas200622 			    ref->enclosing->name, ref->name);
4765772Sas200622 		} else {
4775772Sas200622 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
4785772Sas200622 		}
4795772Sas200622 	} else {
4805772Sas200622 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
4815772Sas200622 	}
4825772Sas200622 
4835772Sas200622 	(void) snprintf(buf, NDOBUFSZ, "%c%c %02d %-.*s %-*s  %s",
4845772Sas200622 	    m_op_c, dir_c, indent, indent,
4855772Sas200622 	    "....+....+....+....+....+....",
4865772Sas200622 	    20 - indent, ref_name, note);
4875772Sas200622 
4885772Sas200622 	mlndo_trace(buf);
4895772Sas200622 }
4905772Sas200622 
4915772Sas200622 /*ARGSUSED*/
4925772Sas200622 void
4935772Sas200622 mlndo_trace(const char *s)
4945772Sas200622 {
4955772Sas200622 	/*
4965772Sas200622 	 * Temporary fbt for dtrace until user space sdt enabled.
4975772Sas200622 	 */
4985772Sas200622 }
4995772Sas200622 
5005772Sas200622 /*
5015772Sas200622  * Format data as hex bytes (limit is 10 bytes):
5025772Sas200622  *
5035772Sas200622  *	1188689424 {10 f6 d9 46}
5045772Sas200622  *
5055772Sas200622  * If the input data is greater than 10 bytes, an ellipsis will
5065772Sas200622  * be inserted before the closing brace.
5075772Sas200622  */
5085772Sas200622 static void
5095772Sas200622 mlndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5105772Sas200622 {
5115772Sas200622 	char *p = buf;
5125772Sas200622 	int interp = 1;
5135772Sas200622 	uint32_t c;
5145772Sas200622 	int n;
5155772Sas200622 	int i;
5165772Sas200622 
5175772Sas200622 	n = (size > 10) ? 10 : size;
5185772Sas200622 	if (n > len-1)
5195772Sas200622 		n = len-1;
5205772Sas200622 
5215772Sas200622 	switch (size) {
5225772Sas200622 	case 1:
5235772Sas200622 		c = *(uint8_t *)data;
5245772Sas200622 		break;
5255772Sas200622 	case 2:
5265772Sas200622 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5275772Sas200622 			c = *(uint16_t *)data;
5285772Sas200622 		else
5295772Sas200622 			c = (data[0] << 8) | data[1];
5305772Sas200622 		break;
5315772Sas200622 	case 4:
5325772Sas200622 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5335772Sas200622 			c = *(uint32_t *)data;
5345772Sas200622 		} else {
5355772Sas200622 			c = (data[0] << 24) | (data[1] << 16)
5365772Sas200622 			    | (data[2] << 8) | data[3];
5375772Sas200622 		}
5385772Sas200622 		break;
5395772Sas200622 	default:
5405772Sas200622 		c = 0;
5415772Sas200622 		interp = 0;
5425772Sas200622 		break;
5435772Sas200622 	}
5445772Sas200622 
5455772Sas200622 	if (interp)
5465772Sas200622 		p += sprintf(p, "%4u {", c);
5475772Sas200622 	else
5485772Sas200622 		p += sprintf(p, " {");
5495772Sas200622 
5505772Sas200622 	p += sprintf(p, "%02x", data[0]);
5515772Sas200622 	for (i = 1; i < n; i++)
5525772Sas200622 		p += sprintf(p, " %02x", data[i]);
5535772Sas200622 	if (size > 10)
5545772Sas200622 		p += sprintf(p, " ...}");
5555772Sas200622 	else
5565772Sas200622 		p += sprintf(p, "}");
5575772Sas200622 
5585772Sas200622 	/*
5595772Sas200622 	 * Show c if it's a printable character or wide-char.
5605772Sas200622 	 */
5615772Sas200622 	if (size < 4 && isprint((uint8_t)c))
5625772Sas200622 		(void) sprintf(p, " %c", (uint8_t)c);
5635772Sas200622 }
564