xref: /onnv-gate/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision 7052:efa04b030974)
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 /*
226482Samw  * 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  */
129*7052Samw void
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)
141*7052Samw 		return;
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 }
1595772Sas200622 
160*7052Samw void
161*7052Samw mlnds_finalize(struct mlndr_stream *mlnds, ndr_fraglist_t *frags)
1626482Samw {
163*7052Samw 	iovec_t *iov;
1646482Samw 	ndr_frag_t *frag;
1656482Samw 	uint32_t size = 0;
1666482Samw 
167*7052Samw 	bzero(frags, sizeof (ndr_fraglist_t));
168*7052Samw 
169*7052Samw 	for (frag = mlnds->frags.head; frag; frag = frag->next)
1706482Samw 		size += frag->len;
1716482Samw 
172*7052Samw 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
173*7052Samw 		return;
174*7052Samw 
175*7052Samw 	frags->iov = malloc(mlnds->frags.nfrag * sizeof (iovec_t));
176*7052Samw 	if (frags->iov == NULL)
177*7052Samw 		return;
178*7052Samw 
179*7052Samw 	frags->head = mlnds->frags.head;
180*7052Samw 	frags->tail = mlnds->frags.tail;
181*7052Samw 	frags->nfrag = mlnds->frags.nfrag;
182*7052Samw 	bzero(&mlnds->frags, sizeof (ndr_fraglist_t));
1836482Samw 
184*7052Samw 	frags->uio.uio_iov = frags->iov;
185*7052Samw 	frags->uio.uio_iovcnt = frags->nfrag;
186*7052Samw 	frags->uio.uio_offset = 0;
187*7052Samw 	frags->uio.uio_segflg = UIO_USERSPACE;
188*7052Samw 	frags->uio.uio_resid = size;
189*7052Samw 
190*7052Samw 	iov = frags->uio.uio_iov;
191*7052Samw 	for (frag = frags->head; frag; frag = frag->next) {
192*7052Samw 		iov->iov_base = (caddr_t)frag->buf;
193*7052Samw 		iov->iov_len = frag->len;
194*7052Samw 		++iov;
1956482Samw 	}
1966482Samw }
1976482Samw 
1985772Sas200622 /*
1995772Sas200622  * mlnds_destruct
2005772Sas200622  *
2015772Sas200622  * Destroy a stream. This is an external interface to provide access to
2025772Sas200622  * the stream's destruct operation.
2035772Sas200622  */
2045772Sas200622 void
2055772Sas200622 mlnds_destruct(struct mlndr_stream *mlnds)
2065772Sas200622 {
2075772Sas200622 	MLNDS_DESTRUCT(mlnds);
2085772Sas200622 }
2095772Sas200622 
2105772Sas200622 /*
2115772Sas200622  * mlndo_malloc
2125772Sas200622  *
2135772Sas200622  * Allocate memory from the stream heap.
2145772Sas200622  */
2155772Sas200622 /*ARGSUSED*/
2165772Sas200622 static char *
2175772Sas200622 mlndo_malloc(struct mlndr_stream *mlnds, unsigned len,
2185772Sas200622     struct ndr_reference *ref)
2195772Sas200622 {
2205772Sas200622 	return (mlrpc_heap_malloc((mlrpc_heap_t *)mlnds->heap, len));
2215772Sas200622 }
2225772Sas200622 
2235772Sas200622 /*
2245772Sas200622  * mlndo_free
2255772Sas200622  *
2265772Sas200622  * Always succeeds: cannot free individual stream allocations.
2275772Sas200622  */
2285772Sas200622 /*ARGSUSED*/
2295772Sas200622 static int
2305772Sas200622 mlndo_free(struct mlndr_stream *mlnds, char *p, struct ndr_reference *ref)
2315772Sas200622 {
2325772Sas200622 	return (1);
2335772Sas200622 }
2345772Sas200622 
2355772Sas200622 /*
2365772Sas200622  * mlndo_grow_pdu
2375772Sas200622  *
2385772Sas200622  * This is the only place that should change the size of the PDU. If the
2395772Sas200622  * desired offset is beyond the current PDU size, we realloc the PDU
2405772Sas200622  * buffer to accommodate the request. For efficiency, the PDU is always
2415772Sas200622  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2425772Sas200622  * beyond NDR_PDU_MAX_SIZE are rejected.
2435772Sas200622  *
2445772Sas200622  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2455772Sas200622  */
2465772Sas200622 static int
2475772Sas200622 mlndo_grow_pdu(struct mlndr_stream *mlnds, unsigned long want_end_offset,
2485772Sas200622     struct ndr_reference *ref)
2495772Sas200622 {
2505772Sas200622 	unsigned char *pdu_addr;
2515772Sas200622 	unsigned pdu_max_size;
2525772Sas200622 
2535772Sas200622 	mlndo_printf(mlnds, ref, "grow %d", want_end_offset);
2545772Sas200622 
2555772Sas200622 	pdu_max_size = mlnds->pdu_max_size;
2565772Sas200622 
2575772Sas200622 	if (want_end_offset > pdu_max_size) {
2585772Sas200622 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2595772Sas200622 
2605772Sas200622 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2615772Sas200622 			return (0);
2625772Sas200622 
2635772Sas200622 		pdu_addr = realloc(mlnds->pdu_base_addr, pdu_max_size);
2645772Sas200622 		if (pdu_addr == 0)
2655772Sas200622 			return (0);
2665772Sas200622 
2675772Sas200622 		mlnds->pdu_max_size = pdu_max_size;
2685772Sas200622 		mlnds->pdu_base_addr = pdu_addr;
2695772Sas200622 		mlnds->pdu_base_offset = (unsigned long)pdu_addr;
2705772Sas200622 	}
2715772Sas200622 
2725772Sas200622 	mlnds->pdu_size = want_end_offset;
2735772Sas200622 	return (1);
2745772Sas200622 }
2755772Sas200622 
2765772Sas200622 static int
2775772Sas200622 mlndo_pad_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
2785772Sas200622     unsigned long n_bytes, struct ndr_reference *ref)
2795772Sas200622 {
2805772Sas200622 	unsigned char *data;
2815772Sas200622 
2825772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
2835772Sas200622 	data += pdu_offset;
2845772Sas200622 
2855772Sas200622 	mlndo_printf(mlnds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
2865772Sas200622 
2875772Sas200622 	bzero(data, n_bytes);
2885772Sas200622 	return (1);
2895772Sas200622 }
2905772Sas200622 
2915772Sas200622 /*
2925772Sas200622  * mlndo_get_pdu
2935772Sas200622  *
2945772Sas200622  * The swap flag is 1 if NDR knows that the byte-order in the PDU
2955772Sas200622  * is different from the local system.
2965772Sas200622  *
2975772Sas200622  * Returns 1 on success or 0 to indicate failure.
2985772Sas200622  */
2995772Sas200622 static int
3005772Sas200622 mlndo_get_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
3015772Sas200622     unsigned long n_bytes, char *buf, int swap_bytes,
3025772Sas200622     struct ndr_reference *ref)
3035772Sas200622 {
3045772Sas200622 	unsigned char *data;
3055772Sas200622 	char hexbuf[NDOBUFSZ];
3065772Sas200622 
3075772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
3085772Sas200622 	data += pdu_offset;
3095772Sas200622 
3105772Sas200622 	mlndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
3115772Sas200622 
3125772Sas200622 	mlndo_printf(mlnds, ref, "get %d@%-3d = %s",
3135772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3145772Sas200622 
3155772Sas200622 	if (!swap_bytes)
3165772Sas200622 		bcopy(data, buf, n_bytes);
3175772Sas200622 	else
3185772Sas200622 		mlnds_bswap(data, (unsigned char *)buf, n_bytes);
3195772Sas200622 
3205772Sas200622 	return (1);
3215772Sas200622 }
3225772Sas200622 
3235772Sas200622 /*
3245772Sas200622  * mlndo_put_pdu
3255772Sas200622  *
3265772Sas200622  * This is a receiver makes right protocol. So we do not need
3275772Sas200622  * to be concerned about the byte-order of an outgoing PDU.
3285772Sas200622  */
3295772Sas200622 /*ARGSUSED*/
3305772Sas200622 static int
3315772Sas200622 mlndo_put_pdu(struct mlndr_stream *mlnds, unsigned long pdu_offset,
3325772Sas200622     unsigned long n_bytes, char *buf, int swap_bytes,
3335772Sas200622     struct ndr_reference *ref)
3345772Sas200622 {
3355772Sas200622 	unsigned char *data;
3365772Sas200622 	char hexbuf[NDOBUFSZ];
3375772Sas200622 
3385772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
3395772Sas200622 	data += pdu_offset;
3405772Sas200622 
3415772Sas200622 	mlndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3425772Sas200622 
3435772Sas200622 	mlndo_printf(mlnds, ref, "put %d@%-3d = %s",
3445772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3455772Sas200622 
3465772Sas200622 	bcopy(buf, data, n_bytes);
3475772Sas200622 	return (1);
3485772Sas200622 }
3495772Sas200622 
3505772Sas200622 static void
3515772Sas200622 mlndo_tattle(struct mlndr_stream *mlnds, char *what,
3525772Sas200622     struct ndr_reference *ref)
3535772Sas200622 {
3545772Sas200622 	mlndo_printf(mlnds, ref, what);
3555772Sas200622 }
3565772Sas200622 
3575772Sas200622 static void
3585772Sas200622 mlndo_tattle_error(struct mlndr_stream *mlnds, struct ndr_reference *ref)
3595772Sas200622 {
3605772Sas200622 	unsigned char *data;
3615772Sas200622 	char hexbuf[NDOBUFSZ];
3625772Sas200622 
3635772Sas200622 	data = (unsigned char *)mlnds->pdu_base_offset;
3645772Sas200622 	if (ref)
3655772Sas200622 		data += ref->pdu_offset;
3665772Sas200622 	else
3675772Sas200622 		data += mlnds->pdu_scan_offset;
3685772Sas200622 
3695772Sas200622 	mlndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
3705772Sas200622 
3715772Sas200622 	mlndo_printf(mlnds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
3725772Sas200622 	    mlnds->error, mlnds->error_ref, mlnds->pdu_scan_offset,
3735772Sas200622 	    mlnds->pdu_size, mlnds->pdu_max_size);
3745772Sas200622 	mlndo_printf(mlnds, ref, "      %s", hexbuf);
3755772Sas200622 }
3765772Sas200622 
3775772Sas200622 /*
3785772Sas200622  * mlndo_reset
3795772Sas200622  *
3805772Sas200622  * Reset a stream: zap the outer_queue. We don't need to tamper
3815772Sas200622  * with the stream heap: it's handled externally to the stream.
3825772Sas200622  */
3835772Sas200622 static int
3845772Sas200622 mlndo_reset(struct mlndr_stream *mlnds)
3855772Sas200622 {
3865772Sas200622 	mlndo_printf(mlnds, 0, "reset");
3875772Sas200622 
3885772Sas200622 	mlnds->pdu_size = 0;
3895772Sas200622 	mlnds->pdu_scan_offset = 0;
3905772Sas200622 	mlnds->outer_queue_head = 0;
3915772Sas200622 	mlnds->outer_current = 0;
3925772Sas200622 	mlnds->outer_queue_tailp = &mlnds->outer_queue_head;
3935772Sas200622 
3945772Sas200622 	return (1);
3955772Sas200622 }
3965772Sas200622 
3975772Sas200622 /*
3985772Sas200622  * mlndo_destruct
3995772Sas200622  *
4006482Samw  * Destruct a stream: zap the outer_queue.
4016482Samw  * Note: heap management (creation/destruction) is external to the stream.
4025772Sas200622  */
4035772Sas200622 static void
4045772Sas200622 mlndo_destruct(struct mlndr_stream *mlnds)
4055772Sas200622 {
4066482Samw 	ndr_frag_t *frag;
4076482Samw 
4085772Sas200622 	mlndo_printf(mlnds, 0, "destruct");
4095772Sas200622 
4106482Samw 	if (mlnds->pdu_base_addr != NULL) {
4115772Sas200622 		free(mlnds->pdu_base_addr);
4126482Samw 		mlnds->pdu_base_addr = NULL;
4135772Sas200622 		mlnds->pdu_base_offset = 0;
4145772Sas200622 	}
4155772Sas200622 
416*7052Samw 	while ((frag = mlnds->frags.head) != NULL) {
417*7052Samw 		mlnds->frags.head = frag->next;
4186482Samw 		free(frag);
4196482Samw 	}
4206482Samw 
421*7052Samw 	bzero(&mlnds->frags, sizeof (ndr_fraglist_t));
422*7052Samw 
4235772Sas200622 	mlnds->outer_queue_head = 0;
4245772Sas200622 	mlnds->outer_current = 0;
4255772Sas200622 	mlnds->outer_queue_tailp = &mlnds->outer_queue_head;
4265772Sas200622 }
4275772Sas200622 
4285772Sas200622 /*
4295772Sas200622  * Printf style formatting for NDR operations.
4305772Sas200622  */
4315772Sas200622 void
4325772Sas200622 mlndo_printf(struct mlndr_stream *mlnds, struct ndr_reference *ref,
4335772Sas200622     const char *fmt, ...)
4345772Sas200622 {
4355772Sas200622 	va_list ap;
4365772Sas200622 	char buf[NDOBUFSZ];
4375772Sas200622 
4385772Sas200622 	va_start(ap, fmt);
4395772Sas200622 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4405772Sas200622 	va_end(ap);
4415772Sas200622 
4425772Sas200622 	if (mlnds)
4435772Sas200622 		mlndo_fmt(mlnds, ref, buf);
4445772Sas200622 	else
4455772Sas200622 		mlndo_trace(buf);
4465772Sas200622 }
4475772Sas200622 
4485772Sas200622 /*
4495772Sas200622  * Main output formatter for NDR operations.
4505772Sas200622  *
4515772Sas200622  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
4525772Sas200622  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
4535772Sas200622  *
4545772Sas200622  *	U       Marshalling flag (M=marshal, U=unmarshal)
4555772Sas200622  *	I       Direction flag (I=in, O=out)
4565772Sas200622  *	...     Field name
4575772Sas200622  *	get     PDU operation (get or put)
4585772Sas200622  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
4595772Sas200622  *	{05}    Value
4605772Sas200622  */
4615772Sas200622 void
4625772Sas200622 mlndo_fmt(struct mlndr_stream *mlnds, struct ndr_reference *ref, char *note)
4635772Sas200622 {
4645772Sas200622 	struct ndr_reference *p;
4655772Sas200622 	int			indent;
4665772Sas200622 	char			ref_name[NDOBUFSZ];
4675772Sas200622 	char			buf[NDOBUFSZ];
4685772Sas200622 	int			m_op_c = '?', dir_c = '?';
4695772Sas200622 
4705772Sas200622 	switch (mlnds->m_op) {
4715772Sas200622 	case 0:				m_op_c = '-';	break;
4725772Sas200622 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
4735772Sas200622 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
4745772Sas200622 	default:			m_op_c = '?';	break;
4755772Sas200622 	}
4765772Sas200622 
4775772Sas200622 	switch (mlnds->dir) {
4785772Sas200622 	case 0:				dir_c = '-';	break;
4795772Sas200622 	case NDR_DIR_IN:		dir_c = 'I';	break;
4805772Sas200622 	case NDR_DIR_OUT:		dir_c = 'O';	break;
4815772Sas200622 	default:			dir_c = '?';	break;
4825772Sas200622 	}
4835772Sas200622 
4845772Sas200622 	for (indent = 0, p = ref; p; p = p->enclosing)
4855772Sas200622 		indent++;
4865772Sas200622 
4875772Sas200622 	if (ref && ref->name) {
4885772Sas200622 		if (*ref->name == '[' && ref->enclosing) {
4895772Sas200622 			indent--;
4905772Sas200622 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
4915772Sas200622 			    ref->enclosing->name, ref->name);
4925772Sas200622 		} else {
4935772Sas200622 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
4945772Sas200622 		}
4955772Sas200622 	} else {
4965772Sas200622 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
4975772Sas200622 	}
4985772Sas200622 
4995772Sas200622 	(void) snprintf(buf, NDOBUFSZ, "%c%c %02d %-.*s %-*s  %s",
5005772Sas200622 	    m_op_c, dir_c, indent, indent,
5015772Sas200622 	    "....+....+....+....+....+....",
5025772Sas200622 	    20 - indent, ref_name, note);
5035772Sas200622 
5045772Sas200622 	mlndo_trace(buf);
5055772Sas200622 }
5065772Sas200622 
5075772Sas200622 /*ARGSUSED*/
5085772Sas200622 void
5095772Sas200622 mlndo_trace(const char *s)
5105772Sas200622 {
5115772Sas200622 	/*
5125772Sas200622 	 * Temporary fbt for dtrace until user space sdt enabled.
5135772Sas200622 	 */
5145772Sas200622 }
5155772Sas200622 
5165772Sas200622 /*
5175772Sas200622  * Format data as hex bytes (limit is 10 bytes):
5185772Sas200622  *
5195772Sas200622  *	1188689424 {10 f6 d9 46}
5205772Sas200622  *
5215772Sas200622  * If the input data is greater than 10 bytes, an ellipsis will
5225772Sas200622  * be inserted before the closing brace.
5235772Sas200622  */
5245772Sas200622 static void
5255772Sas200622 mlndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5265772Sas200622 {
5275772Sas200622 	char *p = buf;
5285772Sas200622 	int interp = 1;
5295772Sas200622 	uint32_t c;
5305772Sas200622 	int n;
5315772Sas200622 	int i;
5325772Sas200622 
5335772Sas200622 	n = (size > 10) ? 10 : size;
5345772Sas200622 	if (n > len-1)
5355772Sas200622 		n = len-1;
5365772Sas200622 
5375772Sas200622 	switch (size) {
5385772Sas200622 	case 1:
5395772Sas200622 		c = *(uint8_t *)data;
5405772Sas200622 		break;
5415772Sas200622 	case 2:
5425772Sas200622 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5435772Sas200622 			c = *(uint16_t *)data;
5445772Sas200622 		else
5455772Sas200622 			c = (data[0] << 8) | data[1];
5465772Sas200622 		break;
5475772Sas200622 	case 4:
5485772Sas200622 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5495772Sas200622 			c = *(uint32_t *)data;
5505772Sas200622 		} else {
5515772Sas200622 			c = (data[0] << 24) | (data[1] << 16)
5525772Sas200622 			    | (data[2] << 8) | data[3];
5535772Sas200622 		}
5545772Sas200622 		break;
5555772Sas200622 	default:
5565772Sas200622 		c = 0;
5575772Sas200622 		interp = 0;
5585772Sas200622 		break;
5595772Sas200622 	}
5605772Sas200622 
5615772Sas200622 	if (interp)
5625772Sas200622 		p += sprintf(p, "%4u {", c);
5635772Sas200622 	else
5645772Sas200622 		p += sprintf(p, " {");
5655772Sas200622 
5665772Sas200622 	p += sprintf(p, "%02x", data[0]);
5675772Sas200622 	for (i = 1; i < n; i++)
5685772Sas200622 		p += sprintf(p, " %02x", data[i]);
5695772Sas200622 	if (size > 10)
5705772Sas200622 		p += sprintf(p, " ...}");
5715772Sas200622 	else
5725772Sas200622 		p += sprintf(p, "}");
5735772Sas200622 
5745772Sas200622 	/*
5755772Sas200622 	 * Show c if it's a printable character or wide-char.
5765772Sas200622 	 */
5775772Sas200622 	if (size < 4 && isprint((uint8_t)c))
5785772Sas200622 		(void) sprintf(p, " %c", (uint8_t)c);
5795772Sas200622 }
580