xref: /onnv-gate/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision 10475:1f599611bc1f)
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 /*
229914Samw@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235772Sas200622  * Use is subject to license terms.
245772Sas200622  */
255772Sas200622 
265772Sas200622 /*
278334SJose.Borrego@Sun.COM  * Server-side NDR stream (PDU) operations. Stream operations should
288334SJose.Borrego@Sun.COM  * return TRUE (non-zero) on success or FALSE (zero or a null pointer)
298334SJose.Borrego@Sun.COM  * on failure. When an operation returns FALSE, including ndo_malloc()
308334SJose.Borrego@Sun.COM  * returning NULL, it should set the nds->error to indicate what went
318334SJose.Borrego@Sun.COM  * wrong.
325772Sas200622  *
338334SJose.Borrego@Sun.COM  * When available, the relevant ndr reference is passed to the
345772Sas200622  * operation but keep in mind that it may be a null pointer.
355772Sas200622  *
368334SJose.Borrego@Sun.COM  * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu()
375772Sas200622  * must never grow the PDU data. A request for out-of-bounds data is
385772Sas200622  * an error. The swap_bytes flag is 1 if NDR knows that the byte-
395772Sas200622  * order in the PDU is different from the local system.
405772Sas200622  */
415772Sas200622 
425772Sas200622 #include <sys/types.h>
435772Sas200622 #include <stdarg.h>
445772Sas200622 #include <ctype.h>
455772Sas200622 #include <stdio.h>
465772Sas200622 #include <stdlib.h>
475772Sas200622 #include <strings.h>
485772Sas200622 #include <string.h>
495772Sas200622 #include <assert.h>
505772Sas200622 
515772Sas200622 #include <smbsrv/libsmb.h>
528334SJose.Borrego@Sun.COM #include <smbsrv/libmlrpc.h>
535772Sas200622 #include <smbsrv/ntstatus.h>
545772Sas200622 
555772Sas200622 #define	NDOBUFSZ		128
565772Sas200622 
575772Sas200622 #define	NDR_PDU_BLOCK_SIZE	(4*1024)
585772Sas200622 #define	NDR_PDU_BLOCK_MASK	(NDR_PDU_BLOCK_SIZE - 1)
595772Sas200622 #define	NDR_PDU_ALIGN(N) \
605772Sas200622 	(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
615772Sas200622 #define	NDR_PDU_MAX_SIZE		(64*1024*1024)
625772Sas200622 
638334SJose.Borrego@Sun.COM static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
648334SJose.Borrego@Sun.COM static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
658334SJose.Borrego@Sun.COM static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
668334SJose.Borrego@Sun.COM static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
678334SJose.Borrego@Sun.COM     ndr_ref_t *);
688334SJose.Borrego@Sun.COM static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
698334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
708334SJose.Borrego@Sun.COM static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
718334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
728334SJose.Borrego@Sun.COM static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
738334SJose.Borrego@Sun.COM static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
748334SJose.Borrego@Sun.COM static int ndo_reset(ndr_stream_t *);
758334SJose.Borrego@Sun.COM static void ndo_destruct(ndr_stream_t *);
768334SJose.Borrego@Sun.COM static void ndo_hexfmt(uint8_t *, int, int, char *, int);
775772Sas200622 
785772Sas200622 /*
798334SJose.Borrego@Sun.COM  * The ndr stream operations table.
805772Sas200622  */
818334SJose.Borrego@Sun.COM static ndr_stream_ops_t nds_ops = {
828334SJose.Borrego@Sun.COM     ndo_malloc,
838334SJose.Borrego@Sun.COM     ndo_free,
848334SJose.Borrego@Sun.COM     ndo_grow_pdu,
858334SJose.Borrego@Sun.COM     ndo_pad_pdu,
868334SJose.Borrego@Sun.COM     ndo_get_pdu,
878334SJose.Borrego@Sun.COM     ndo_put_pdu,
888334SJose.Borrego@Sun.COM     ndo_tattle,
898334SJose.Borrego@Sun.COM     ndo_tattle_error,
908334SJose.Borrego@Sun.COM     ndo_reset,
918334SJose.Borrego@Sun.COM     ndo_destruct
925772Sas200622 };
935772Sas200622 
945772Sas200622 /*
958334SJose.Borrego@Sun.COM  * nds_bswap
965772Sas200622  *
975772Sas200622  * Copies len bytes from src to dst such that dst contains the bytes
985772Sas200622  * from src in reverse order.
995772Sas200622  *
1005772Sas200622  * We expect to be dealing with bytes, words, dwords etc. So the
1015772Sas200622  * length must be non-zero and a power of 2.
1025772Sas200622  */
1035772Sas200622 void
1048334SJose.Borrego@Sun.COM nds_bswap(void *srcbuf, void *dstbuf, size_t len)
1055772Sas200622 {
1065772Sas200622 	uint8_t *src = (uint8_t *)srcbuf;
1075772Sas200622 	uint8_t *dst = (uint8_t *)dstbuf;
1085772Sas200622 
1095772Sas200622 	if ((len != 0) && ((len & (len - 1)) == 0)) {
1105772Sas200622 		src += len;
1115772Sas200622 
1125772Sas200622 		while (len--)
1135772Sas200622 			*dst++ = *(--src);
1145772Sas200622 	}
1155772Sas200622 }
1165772Sas200622 
1175772Sas200622 /*
1188334SJose.Borrego@Sun.COM  * nds_initialize
1195772Sas200622  *
1205772Sas200622  * Initialize a stream. Sets up the PDU parameters and assigns the stream
1215772Sas200622  * operations and the reference to the heap. An external heap is provided
1225772Sas200622  * to the stream, rather than each stream creating its own heap.
1235772Sas200622  */
1247052Samw void
1258334SJose.Borrego@Sun.COM nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
1268334SJose.Borrego@Sun.COM     int composite_op, ndr_heap_t *heap)
1275772Sas200622 {
1285772Sas200622 	unsigned size;
1295772Sas200622 
1308334SJose.Borrego@Sun.COM 	assert(nds);
1315772Sas200622 	assert(heap);
1325772Sas200622 
1338334SJose.Borrego@Sun.COM 	bzero(nds, sizeof (*nds));
1345772Sas200622 
1355772Sas200622 	if (pdu_size_hint > NDR_PDU_MAX_SIZE)
1367052Samw 		return;
1375772Sas200622 
1385772Sas200622 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
1398334SJose.Borrego@Sun.COM 	nds->pdu_base_addr = malloc(size);
1408334SJose.Borrego@Sun.COM 	assert(nds->pdu_base_addr);
1415772Sas200622 
1428334SJose.Borrego@Sun.COM 	nds->pdu_max_size = size;
1438334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
1448334SJose.Borrego@Sun.COM 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
1455772Sas200622 
1468334SJose.Borrego@Sun.COM 	nds->ndo = &nds_ops;
1478334SJose.Borrego@Sun.COM 	nds->heap = (struct ndr_heap *)heap;
1485772Sas200622 
1498334SJose.Borrego@Sun.COM 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
1508334SJose.Borrego@Sun.COM 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
1515772Sas200622 
1528334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
1535772Sas200622 }
1545772Sas200622 
1557052Samw void
1568334SJose.Borrego@Sun.COM nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
1576482Samw {
1587052Samw 	iovec_t *iov;
1596482Samw 	ndr_frag_t *frag;
1606482Samw 	uint32_t size = 0;
1616482Samw 
1627052Samw 	bzero(frags, sizeof (ndr_fraglist_t));
1637052Samw 
1648334SJose.Borrego@Sun.COM 	for (frag = nds->frags.head; frag; frag = frag->next)
1656482Samw 		size += frag->len;
1666482Samw 
1677052Samw 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
1687052Samw 		return;
1697052Samw 
1708334SJose.Borrego@Sun.COM 	frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
1717052Samw 	if (frags->iov == NULL)
1727052Samw 		return;
1737052Samw 
1748334SJose.Borrego@Sun.COM 	frags->head = nds->frags.head;
1758334SJose.Borrego@Sun.COM 	frags->tail = nds->frags.tail;
1768334SJose.Borrego@Sun.COM 	frags->nfrag = nds->frags.nfrag;
1778334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
1786482Samw 
1797052Samw 	frags->uio.uio_iov = frags->iov;
1807052Samw 	frags->uio.uio_iovcnt = frags->nfrag;
1817052Samw 	frags->uio.uio_offset = 0;
1827052Samw 	frags->uio.uio_segflg = UIO_USERSPACE;
1837052Samw 	frags->uio.uio_resid = size;
1847052Samw 
1857052Samw 	iov = frags->uio.uio_iov;
1867052Samw 	for (frag = frags->head; frag; frag = frag->next) {
1877052Samw 		iov->iov_base = (caddr_t)frag->buf;
1887052Samw 		iov->iov_len = frag->len;
1897052Samw 		++iov;
1906482Samw 	}
1916482Samw }
1926482Samw 
1935772Sas200622 /*
1948334SJose.Borrego@Sun.COM  * nds_destruct
1955772Sas200622  *
1965772Sas200622  * Destroy a stream. This is an external interface to provide access to
1975772Sas200622  * the stream's destruct operation.
1985772Sas200622  */
1995772Sas200622 void
2008334SJose.Borrego@Sun.COM nds_destruct(ndr_stream_t *nds)
2015772Sas200622 {
202*10475Samw@Sun.COM 	if ((nds == NULL) || (nds->ndo == NULL))
203*10475Samw@Sun.COM 		return;
204*10475Samw@Sun.COM 
2058334SJose.Borrego@Sun.COM 	NDS_DESTRUCT(nds);
2065772Sas200622 }
2075772Sas200622 
2085772Sas200622 /*
209*10475Samw@Sun.COM  * Print NDR stream state.
210*10475Samw@Sun.COM  */
211*10475Samw@Sun.COM void
212*10475Samw@Sun.COM nds_show_state(ndr_stream_t *nds)
213*10475Samw@Sun.COM {
214*10475Samw@Sun.COM 	if (nds == NULL) {
215*10475Samw@Sun.COM 		ndo_printf(NULL, NULL, "nds: <null");
216*10475Samw@Sun.COM 		return;
217*10475Samw@Sun.COM 	}
218*10475Samw@Sun.COM 
219*10475Samw@Sun.COM 	ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d",
220*10475Samw@Sun.COM 	    nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
221*10475Samw@Sun.COM 	    nds->pdu_scan_offset);
222*10475Samw@Sun.COM }
223*10475Samw@Sun.COM 
224*10475Samw@Sun.COM /*
2258334SJose.Borrego@Sun.COM  * ndo_malloc
2265772Sas200622  *
2275772Sas200622  * Allocate memory from the stream heap.
2285772Sas200622  */
2295772Sas200622 /*ARGSUSED*/
2305772Sas200622 static char *
2318334SJose.Borrego@Sun.COM ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
2325772Sas200622 {
2338334SJose.Borrego@Sun.COM 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
2345772Sas200622 }
2355772Sas200622 
2365772Sas200622 /*
2378334SJose.Borrego@Sun.COM  * ndo_free
2385772Sas200622  *
2395772Sas200622  * Always succeeds: cannot free individual stream allocations.
2405772Sas200622  */
2415772Sas200622 /*ARGSUSED*/
2425772Sas200622 static int
2438334SJose.Borrego@Sun.COM ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
2445772Sas200622 {
2455772Sas200622 	return (1);
2465772Sas200622 }
2475772Sas200622 
2485772Sas200622 /*
2498334SJose.Borrego@Sun.COM  * ndo_grow_pdu
2505772Sas200622  *
2515772Sas200622  * This is the only place that should change the size of the PDU. If the
2525772Sas200622  * desired offset is beyond the current PDU size, we realloc the PDU
2535772Sas200622  * buffer to accommodate the request. For efficiency, the PDU is always
2545772Sas200622  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2555772Sas200622  * beyond NDR_PDU_MAX_SIZE are rejected.
2565772Sas200622  *
2575772Sas200622  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2585772Sas200622  */
2595772Sas200622 static int
2608334SJose.Borrego@Sun.COM ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
2615772Sas200622 {
2625772Sas200622 	unsigned char *pdu_addr;
2635772Sas200622 	unsigned pdu_max_size;
2645772Sas200622 
2658334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "grow %d", want_end_offset);
2665772Sas200622 
2678334SJose.Borrego@Sun.COM 	pdu_max_size = nds->pdu_max_size;
2685772Sas200622 
2695772Sas200622 	if (want_end_offset > pdu_max_size) {
2705772Sas200622 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2715772Sas200622 
2725772Sas200622 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2735772Sas200622 			return (0);
2745772Sas200622 
2758334SJose.Borrego@Sun.COM 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
2765772Sas200622 		if (pdu_addr == 0)
2775772Sas200622 			return (0);
2785772Sas200622 
2798334SJose.Borrego@Sun.COM 		nds->pdu_max_size = pdu_max_size;
2808334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = pdu_addr;
2818334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = (unsigned long)pdu_addr;
2825772Sas200622 	}
2835772Sas200622 
2848334SJose.Borrego@Sun.COM 	nds->pdu_size = want_end_offset;
2855772Sas200622 	return (1);
2865772Sas200622 }
2875772Sas200622 
2885772Sas200622 static int
2898334SJose.Borrego@Sun.COM ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
2908334SJose.Borrego@Sun.COM     unsigned long n_bytes, ndr_ref_t *ref)
2915772Sas200622 {
2925772Sas200622 	unsigned char *data;
2935772Sas200622 
2948334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
2955772Sas200622 	data += pdu_offset;
2965772Sas200622 
2978334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
2985772Sas200622 
2995772Sas200622 	bzero(data, n_bytes);
3005772Sas200622 	return (1);
3015772Sas200622 }
3025772Sas200622 
3035772Sas200622 /*
3048334SJose.Borrego@Sun.COM  * ndo_get_pdu
3055772Sas200622  *
3065772Sas200622  * The swap flag is 1 if NDR knows that the byte-order in the PDU
3075772Sas200622  * is different from the local system.
3085772Sas200622  *
3095772Sas200622  * Returns 1 on success or 0 to indicate failure.
3105772Sas200622  */
3115772Sas200622 static int
3128334SJose.Borrego@Sun.COM ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3138334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3145772Sas200622 {
3155772Sas200622 	unsigned char *data;
3165772Sas200622 	char hexbuf[NDOBUFSZ];
3175772Sas200622 
3188334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3195772Sas200622 	data += pdu_offset;
3205772Sas200622 
3218334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
3225772Sas200622 
3238334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "get %d@%-3d = %s",
3245772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3255772Sas200622 
3265772Sas200622 	if (!swap_bytes)
3275772Sas200622 		bcopy(data, buf, n_bytes);
3285772Sas200622 	else
3298334SJose.Borrego@Sun.COM 		nds_bswap(data, (unsigned char *)buf, n_bytes);
3305772Sas200622 
3315772Sas200622 	return (1);
3325772Sas200622 }
3335772Sas200622 
3345772Sas200622 /*
3358334SJose.Borrego@Sun.COM  * ndo_put_pdu
3365772Sas200622  *
3375772Sas200622  * This is a receiver makes right protocol. So we do not need
3385772Sas200622  * to be concerned about the byte-order of an outgoing PDU.
3395772Sas200622  */
3405772Sas200622 /*ARGSUSED*/
3415772Sas200622 static int
3428334SJose.Borrego@Sun.COM ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3438334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3445772Sas200622 {
3455772Sas200622 	unsigned char *data;
3465772Sas200622 	char hexbuf[NDOBUFSZ];
3475772Sas200622 
3488334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3495772Sas200622 	data += pdu_offset;
3505772Sas200622 
3518334SJose.Borrego@Sun.COM 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3525772Sas200622 
3538334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "put %d@%-3d = %s",
3545772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3555772Sas200622 
3565772Sas200622 	bcopy(buf, data, n_bytes);
3575772Sas200622 	return (1);
3585772Sas200622 }
3595772Sas200622 
3605772Sas200622 static void
3618334SJose.Borrego@Sun.COM ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
3625772Sas200622 {
3638334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, what);
3645772Sas200622 }
3655772Sas200622 
3665772Sas200622 static void
3678334SJose.Borrego@Sun.COM ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
3685772Sas200622 {
3695772Sas200622 	unsigned char *data;
3705772Sas200622 	char hexbuf[NDOBUFSZ];
3715772Sas200622 
3728334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3735772Sas200622 	if (ref)
3745772Sas200622 		data += ref->pdu_offset;
3755772Sas200622 	else
3768334SJose.Borrego@Sun.COM 		data += nds->pdu_scan_offset;
3775772Sas200622 
3788334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
3795772Sas200622 
3808334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
3818334SJose.Borrego@Sun.COM 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
3828334SJose.Borrego@Sun.COM 	    nds->pdu_size, nds->pdu_max_size);
3838334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "      %s", hexbuf);
3845772Sas200622 }
3855772Sas200622 
3865772Sas200622 /*
3878334SJose.Borrego@Sun.COM  * ndo_reset
3885772Sas200622  *
3895772Sas200622  * Reset a stream: zap the outer_queue. We don't need to tamper
3905772Sas200622  * with the stream heap: it's handled externally to the stream.
3915772Sas200622  */
3925772Sas200622 static int
3938334SJose.Borrego@Sun.COM ndo_reset(ndr_stream_t *nds)
3945772Sas200622 {
3958334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "reset");
3965772Sas200622 
3978334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
3988334SJose.Borrego@Sun.COM 	nds->pdu_scan_offset = 0;
3998334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
4008334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
4018334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
4025772Sas200622 
4035772Sas200622 	return (1);
4045772Sas200622 }
4055772Sas200622 
4065772Sas200622 /*
4078334SJose.Borrego@Sun.COM  * ndo_destruct
4085772Sas200622  *
4096482Samw  * Destruct a stream: zap the outer_queue.
4106482Samw  * Note: heap management (creation/destruction) is external to the stream.
4115772Sas200622  */
4125772Sas200622 static void
4138334SJose.Borrego@Sun.COM ndo_destruct(ndr_stream_t *nds)
4145772Sas200622 {
4156482Samw 	ndr_frag_t *frag;
4166482Samw 
4178334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "destruct");
4188334SJose.Borrego@Sun.COM 
4198334SJose.Borrego@Sun.COM 	if (nds == NULL)
4208334SJose.Borrego@Sun.COM 		return;
4215772Sas200622 
4228334SJose.Borrego@Sun.COM 	if (nds->pdu_base_addr != NULL) {
4238334SJose.Borrego@Sun.COM 		free(nds->pdu_base_addr);
4248334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = NULL;
4258334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = 0;
4265772Sas200622 	}
4275772Sas200622 
4288334SJose.Borrego@Sun.COM 	while ((frag = nds->frags.head) != NULL) {
4298334SJose.Borrego@Sun.COM 		nds->frags.head = frag->next;
4306482Samw 		free(frag);
4316482Samw 	}
4326482Samw 
4338334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
4347052Samw 
4358334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
4368334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
4378334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
4385772Sas200622 }
4395772Sas200622 
4405772Sas200622 /*
4415772Sas200622  * Printf style formatting for NDR operations.
4425772Sas200622  */
4435772Sas200622 void
4448334SJose.Borrego@Sun.COM ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
4455772Sas200622 {
4465772Sas200622 	va_list ap;
4475772Sas200622 	char buf[NDOBUFSZ];
4485772Sas200622 
4495772Sas200622 	va_start(ap, fmt);
4505772Sas200622 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4515772Sas200622 	va_end(ap);
4525772Sas200622 
4538334SJose.Borrego@Sun.COM 	if (nds)
4548334SJose.Borrego@Sun.COM 		ndo_fmt(nds, ref, buf);
4555772Sas200622 	else
4568334SJose.Borrego@Sun.COM 		ndo_trace(buf);
4575772Sas200622 }
4585772Sas200622 
4595772Sas200622 /*
4605772Sas200622  * Main output formatter for NDR operations.
4615772Sas200622  *
4625772Sas200622  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
4635772Sas200622  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
4645772Sas200622  *
4655772Sas200622  *	U       Marshalling flag (M=marshal, U=unmarshal)
4665772Sas200622  *	I       Direction flag (I=in, O=out)
4675772Sas200622  *	...     Field name
4685772Sas200622  *	get     PDU operation (get or put)
4695772Sas200622  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
4705772Sas200622  *	{05}    Value
4715772Sas200622  */
4725772Sas200622 void
4738334SJose.Borrego@Sun.COM ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
4745772Sas200622 {
4758334SJose.Borrego@Sun.COM 	ndr_ref_t	*p;
4768334SJose.Borrego@Sun.COM 	int		indent;
4778334SJose.Borrego@Sun.COM 	char		ref_name[NDOBUFSZ];
4788334SJose.Borrego@Sun.COM 	char		buf[NDOBUFSZ];
4798334SJose.Borrego@Sun.COM 	int		m_op_c = '?', dir_c = '?';
4805772Sas200622 
4818334SJose.Borrego@Sun.COM 	switch (nds->m_op) {
4825772Sas200622 	case 0:				m_op_c = '-';	break;
4835772Sas200622 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
4845772Sas200622 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
4855772Sas200622 	default:			m_op_c = '?';	break;
4865772Sas200622 	}
4875772Sas200622 
4888334SJose.Borrego@Sun.COM 	switch (nds->dir) {
4895772Sas200622 	case 0:				dir_c = '-';	break;
4905772Sas200622 	case NDR_DIR_IN:		dir_c = 'I';	break;
4915772Sas200622 	case NDR_DIR_OUT:		dir_c = 'O';	break;
4925772Sas200622 	default:			dir_c = '?';	break;
4935772Sas200622 	}
4945772Sas200622 
4955772Sas200622 	for (indent = 0, p = ref; p; p = p->enclosing)
4965772Sas200622 		indent++;
4975772Sas200622 
4985772Sas200622 	if (ref && ref->name) {
4995772Sas200622 		if (*ref->name == '[' && ref->enclosing) {
5005772Sas200622 			indent--;
5015772Sas200622 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
5025772Sas200622 			    ref->enclosing->name, ref->name);
5035772Sas200622 		} else {
5045772Sas200622 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
5055772Sas200622 		}
5065772Sas200622 	} else {
5075772Sas200622 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
5085772Sas200622 	}
5095772Sas200622 
5109914Samw@Sun.COM 	(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s  %s",
5119914Samw@Sun.COM 	    m_op_c, dir_c, indent,
5125772Sas200622 	    "....+....+....+....+....+....",
5135772Sas200622 	    20 - indent, ref_name, note);
5145772Sas200622 
5158334SJose.Borrego@Sun.COM 	ndo_trace(buf);
5165772Sas200622 }
5175772Sas200622 
5185772Sas200622 /*ARGSUSED*/
5195772Sas200622 void
5208334SJose.Borrego@Sun.COM ndo_trace(const char *s)
5215772Sas200622 {
5225772Sas200622 	/*
5235772Sas200622 	 * Temporary fbt for dtrace until user space sdt enabled.
5245772Sas200622 	 */
5255772Sas200622 }
5265772Sas200622 
5275772Sas200622 /*
5285772Sas200622  * Format data as hex bytes (limit is 10 bytes):
5295772Sas200622  *
5305772Sas200622  *	1188689424 {10 f6 d9 46}
5315772Sas200622  *
5325772Sas200622  * If the input data is greater than 10 bytes, an ellipsis will
5335772Sas200622  * be inserted before the closing brace.
5345772Sas200622  */
5355772Sas200622 static void
5368334SJose.Borrego@Sun.COM ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5375772Sas200622 {
5385772Sas200622 	char *p = buf;
5395772Sas200622 	int interp = 1;
5405772Sas200622 	uint32_t c;
5415772Sas200622 	int n;
5425772Sas200622 	int i;
5435772Sas200622 
5445772Sas200622 	n = (size > 10) ? 10 : size;
5455772Sas200622 	if (n > len-1)
5465772Sas200622 		n = len-1;
5475772Sas200622 
5485772Sas200622 	switch (size) {
5495772Sas200622 	case 1:
5505772Sas200622 		c = *(uint8_t *)data;
5515772Sas200622 		break;
5525772Sas200622 	case 2:
5535772Sas200622 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5545772Sas200622 			c = *(uint16_t *)data;
5555772Sas200622 		else
5565772Sas200622 			c = (data[0] << 8) | data[1];
5575772Sas200622 		break;
5585772Sas200622 	case 4:
5595772Sas200622 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5605772Sas200622 			c = *(uint32_t *)data;
5615772Sas200622 		} else {
5625772Sas200622 			c = (data[0] << 24) | (data[1] << 16)
5635772Sas200622 			    | (data[2] << 8) | data[3];
5645772Sas200622 		}
5655772Sas200622 		break;
5665772Sas200622 	default:
5675772Sas200622 		c = 0;
5685772Sas200622 		interp = 0;
5695772Sas200622 		break;
5705772Sas200622 	}
5715772Sas200622 
5725772Sas200622 	if (interp)
5735772Sas200622 		p += sprintf(p, "%4u {", c);
5745772Sas200622 	else
5755772Sas200622 		p += sprintf(p, " {");
5765772Sas200622 
5775772Sas200622 	p += sprintf(p, "%02x", data[0]);
5785772Sas200622 	for (i = 1; i < n; i++)
5795772Sas200622 		p += sprintf(p, " %02x", data[i]);
5805772Sas200622 	if (size > 10)
5815772Sas200622 		p += sprintf(p, " ...}");
5825772Sas200622 	else
5835772Sas200622 		p += sprintf(p, "}");
5845772Sas200622 
5855772Sas200622 	/*
5865772Sas200622 	 * Show c if it's a printable character or wide-char.
5875772Sas200622 	 */
5885772Sas200622 	if (size < 4 && isprint((uint8_t)c))
5895772Sas200622 		(void) sprintf(p, " %c", (uint8_t)c);
5905772Sas200622 }
591