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 */
21*12508Samw@Sun.COM
225772Sas200622 /*
23*12508Samw@Sun.COM * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
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
545772Sas200622 #define NDOBUFSZ 128
555772Sas200622
565772Sas200622 #define NDR_PDU_BLOCK_SIZE (4*1024)
575772Sas200622 #define NDR_PDU_BLOCK_MASK (NDR_PDU_BLOCK_SIZE - 1)
585772Sas200622 #define NDR_PDU_ALIGN(N) \
595772Sas200622 (((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
605772Sas200622 #define NDR_PDU_MAX_SIZE (64*1024*1024)
615772Sas200622
628334SJose.Borrego@Sun.COM static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
638334SJose.Borrego@Sun.COM static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
648334SJose.Borrego@Sun.COM static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
658334SJose.Borrego@Sun.COM static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
668334SJose.Borrego@Sun.COM ndr_ref_t *);
678334SJose.Borrego@Sun.COM static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
688334SJose.Borrego@Sun.COM char *, int, ndr_ref_t *);
698334SJose.Borrego@Sun.COM static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
708334SJose.Borrego@Sun.COM char *, int, ndr_ref_t *);
718334SJose.Borrego@Sun.COM static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
728334SJose.Borrego@Sun.COM static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
738334SJose.Borrego@Sun.COM static int ndo_reset(ndr_stream_t *);
748334SJose.Borrego@Sun.COM static void ndo_destruct(ndr_stream_t *);
758334SJose.Borrego@Sun.COM static void ndo_hexfmt(uint8_t *, int, int, char *, int);
765772Sas200622
775772Sas200622 /*
788334SJose.Borrego@Sun.COM * The ndr stream operations table.
795772Sas200622 */
808334SJose.Borrego@Sun.COM static ndr_stream_ops_t nds_ops = {
818334SJose.Borrego@Sun.COM ndo_malloc,
828334SJose.Borrego@Sun.COM ndo_free,
838334SJose.Borrego@Sun.COM ndo_grow_pdu,
848334SJose.Borrego@Sun.COM ndo_pad_pdu,
858334SJose.Borrego@Sun.COM ndo_get_pdu,
868334SJose.Borrego@Sun.COM ndo_put_pdu,
878334SJose.Borrego@Sun.COM ndo_tattle,
888334SJose.Borrego@Sun.COM ndo_tattle_error,
898334SJose.Borrego@Sun.COM ndo_reset,
908334SJose.Borrego@Sun.COM ndo_destruct
915772Sas200622 };
925772Sas200622
935772Sas200622 /*
948334SJose.Borrego@Sun.COM * nds_bswap
955772Sas200622 *
965772Sas200622 * Copies len bytes from src to dst such that dst contains the bytes
975772Sas200622 * from src in reverse order.
985772Sas200622 *
995772Sas200622 * We expect to be dealing with bytes, words, dwords etc. So the
1005772Sas200622 * length must be non-zero and a power of 2.
1015772Sas200622 */
1025772Sas200622 void
nds_bswap(void * srcbuf,void * dstbuf,size_t len)1038334SJose.Borrego@Sun.COM nds_bswap(void *srcbuf, void *dstbuf, size_t len)
1045772Sas200622 {
1055772Sas200622 uint8_t *src = (uint8_t *)srcbuf;
1065772Sas200622 uint8_t *dst = (uint8_t *)dstbuf;
1075772Sas200622
1085772Sas200622 if ((len != 0) && ((len & (len - 1)) == 0)) {
1095772Sas200622 src += len;
1105772Sas200622
1115772Sas200622 while (len--)
1125772Sas200622 *dst++ = *(--src);
1135772Sas200622 }
1145772Sas200622 }
1155772Sas200622
1165772Sas200622 /*
1178334SJose.Borrego@Sun.COM * nds_initialize
1185772Sas200622 *
1195772Sas200622 * Initialize a stream. Sets up the PDU parameters and assigns the stream
1205772Sas200622 * operations and the reference to the heap. An external heap is provided
1215772Sas200622 * to the stream, rather than each stream creating its own heap.
1225772Sas200622 */
12311337SWilliam.Krier@Sun.COM int
nds_initialize(ndr_stream_t * nds,unsigned pdu_size_hint,int composite_op,ndr_heap_t * heap)1248334SJose.Borrego@Sun.COM nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
1258334SJose.Borrego@Sun.COM int composite_op, ndr_heap_t *heap)
1265772Sas200622 {
1275772Sas200622 unsigned size;
1285772Sas200622
1298334SJose.Borrego@Sun.COM assert(nds);
1305772Sas200622 assert(heap);
1315772Sas200622
1328334SJose.Borrego@Sun.COM bzero(nds, sizeof (*nds));
13311963SAfshin.Ardakani@Sun.COM nds->ndo = &nds_ops;
13411963SAfshin.Ardakani@Sun.COM nds->heap = (struct ndr_heap *)heap;
1355772Sas200622
13611963SAfshin.Ardakani@Sun.COM if (pdu_size_hint > NDR_PDU_MAX_SIZE) {
13711963SAfshin.Ardakani@Sun.COM nds->error = NDR_ERR_BOUNDS_CHECK;
13811963SAfshin.Ardakani@Sun.COM nds->error_ref = __LINE__;
13911963SAfshin.Ardakani@Sun.COM NDS_TATTLE_ERROR(nds, NULL, NULL);
14011963SAfshin.Ardakani@Sun.COM return (NDR_DRC_FAULT_RESOURCE_1);
14111963SAfshin.Ardakani@Sun.COM }
1425772Sas200622
1435772Sas200622 size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
14411337SWilliam.Krier@Sun.COM
14511337SWilliam.Krier@Sun.COM if ((nds->pdu_base_addr = malloc(size)) == NULL) {
14611337SWilliam.Krier@Sun.COM nds->error = NDR_ERR_MALLOC_FAILED;
14711337SWilliam.Krier@Sun.COM nds->error_ref = __LINE__;
14811337SWilliam.Krier@Sun.COM NDS_TATTLE_ERROR(nds, NULL, NULL);
14911337SWilliam.Krier@Sun.COM return (NDR_DRC_FAULT_OUT_OF_MEMORY);
15011337SWilliam.Krier@Sun.COM }
1515772Sas200622
1528334SJose.Borrego@Sun.COM nds->pdu_max_size = size;
1538334SJose.Borrego@Sun.COM nds->pdu_size = 0;
1548334SJose.Borrego@Sun.COM nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
1555772Sas200622
1568334SJose.Borrego@Sun.COM nds->m_op = NDR_MODE_TO_M_OP(composite_op);
1578334SJose.Borrego@Sun.COM nds->dir = NDR_MODE_TO_DIR(composite_op);
1585772Sas200622
1598334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head;
16011337SWilliam.Krier@Sun.COM return (0);
1615772Sas200622 }
1625772Sas200622
1637052Samw void
nds_finalize(ndr_stream_t * nds,ndr_fraglist_t * frags)1648334SJose.Borrego@Sun.COM nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
1656482Samw {
1667052Samw iovec_t *iov;
1676482Samw ndr_frag_t *frag;
1686482Samw uint32_t size = 0;
1696482Samw
1707052Samw bzero(frags, sizeof (ndr_fraglist_t));
1717052Samw
1728334SJose.Borrego@Sun.COM for (frag = nds->frags.head; frag; frag = frag->next)
1736482Samw size += frag->len;
1746482Samw
1757052Samw if (size == 0 || size >= NDR_PDU_MAX_SIZE)
1767052Samw return;
1777052Samw
1788334SJose.Borrego@Sun.COM frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
1797052Samw if (frags->iov == NULL)
1807052Samw return;
1817052Samw
1828334SJose.Borrego@Sun.COM frags->head = nds->frags.head;
1838334SJose.Borrego@Sun.COM frags->tail = nds->frags.tail;
1848334SJose.Borrego@Sun.COM frags->nfrag = nds->frags.nfrag;
1858334SJose.Borrego@Sun.COM bzero(&nds->frags, sizeof (ndr_fraglist_t));
1866482Samw
1877052Samw frags->uio.uio_iov = frags->iov;
1887052Samw frags->uio.uio_iovcnt = frags->nfrag;
1897052Samw frags->uio.uio_offset = 0;
1907052Samw frags->uio.uio_segflg = UIO_USERSPACE;
1917052Samw frags->uio.uio_resid = size;
1927052Samw
1937052Samw iov = frags->uio.uio_iov;
1947052Samw for (frag = frags->head; frag; frag = frag->next) {
1957052Samw iov->iov_base = (caddr_t)frag->buf;
1967052Samw iov->iov_len = frag->len;
1977052Samw ++iov;
1986482Samw }
1996482Samw }
2006482Samw
2015772Sas200622 /*
2028334SJose.Borrego@Sun.COM * nds_destruct
2035772Sas200622 *
2045772Sas200622 * Destroy a stream. This is an external interface to provide access to
2055772Sas200622 * the stream's destruct operation.
2065772Sas200622 */
2075772Sas200622 void
nds_destruct(ndr_stream_t * nds)2088334SJose.Borrego@Sun.COM nds_destruct(ndr_stream_t *nds)
2095772Sas200622 {
21010475Samw@Sun.COM if ((nds == NULL) || (nds->ndo == NULL))
21110475Samw@Sun.COM return;
21210475Samw@Sun.COM
2138334SJose.Borrego@Sun.COM NDS_DESTRUCT(nds);
2145772Sas200622 }
2155772Sas200622
2165772Sas200622 /*
21710475Samw@Sun.COM * Print NDR stream state.
21810475Samw@Sun.COM */
21910475Samw@Sun.COM void
nds_show_state(ndr_stream_t * nds)22010475Samw@Sun.COM nds_show_state(ndr_stream_t *nds)
22110475Samw@Sun.COM {
22210475Samw@Sun.COM if (nds == NULL) {
22310475Samw@Sun.COM ndo_printf(NULL, NULL, "nds: <null");
22410475Samw@Sun.COM return;
22510475Samw@Sun.COM }
22610475Samw@Sun.COM
22710475Samw@Sun.COM ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d",
22810475Samw@Sun.COM nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
22910475Samw@Sun.COM nds->pdu_scan_offset);
23010475Samw@Sun.COM }
23110475Samw@Sun.COM
23210475Samw@Sun.COM /*
2338334SJose.Borrego@Sun.COM * ndo_malloc
2345772Sas200622 *
2355772Sas200622 * Allocate memory from the stream heap.
2365772Sas200622 */
2375772Sas200622 /*ARGSUSED*/
2385772Sas200622 static char *
ndo_malloc(ndr_stream_t * nds,unsigned len,ndr_ref_t * ref)2398334SJose.Borrego@Sun.COM ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
2405772Sas200622 {
2418334SJose.Borrego@Sun.COM return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
2425772Sas200622 }
2435772Sas200622
2445772Sas200622 /*
2458334SJose.Borrego@Sun.COM * ndo_free
2465772Sas200622 *
2475772Sas200622 * Always succeeds: cannot free individual stream allocations.
2485772Sas200622 */
2495772Sas200622 /*ARGSUSED*/
2505772Sas200622 static int
ndo_free(ndr_stream_t * nds,char * p,ndr_ref_t * ref)2518334SJose.Borrego@Sun.COM ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
2525772Sas200622 {
2535772Sas200622 return (1);
2545772Sas200622 }
2555772Sas200622
2565772Sas200622 /*
2578334SJose.Borrego@Sun.COM * ndo_grow_pdu
2585772Sas200622 *
2595772Sas200622 * This is the only place that should change the size of the PDU. If the
2605772Sas200622 * desired offset is beyond the current PDU size, we realloc the PDU
2615772Sas200622 * buffer to accommodate the request. For efficiency, the PDU is always
2625772Sas200622 * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2635772Sas200622 * beyond NDR_PDU_MAX_SIZE are rejected.
2645772Sas200622 *
2655772Sas200622 * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2665772Sas200622 */
2675772Sas200622 static int
ndo_grow_pdu(ndr_stream_t * nds,unsigned long want_end_offset,ndr_ref_t * ref)2688334SJose.Borrego@Sun.COM ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
2695772Sas200622 {
2705772Sas200622 unsigned char *pdu_addr;
2715772Sas200622 unsigned pdu_max_size;
2725772Sas200622
2738334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "grow %d", want_end_offset);
2745772Sas200622
2758334SJose.Borrego@Sun.COM pdu_max_size = nds->pdu_max_size;
2765772Sas200622
2775772Sas200622 if (want_end_offset > pdu_max_size) {
2785772Sas200622 pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2795772Sas200622
2805772Sas200622 if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2815772Sas200622 return (0);
2825772Sas200622
2838334SJose.Borrego@Sun.COM pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
2845772Sas200622 if (pdu_addr == 0)
2855772Sas200622 return (0);
2865772Sas200622
2878334SJose.Borrego@Sun.COM nds->pdu_max_size = pdu_max_size;
2888334SJose.Borrego@Sun.COM nds->pdu_base_addr = pdu_addr;
2898334SJose.Borrego@Sun.COM nds->pdu_base_offset = (unsigned long)pdu_addr;
2905772Sas200622 }
2915772Sas200622
2928334SJose.Borrego@Sun.COM nds->pdu_size = want_end_offset;
2935772Sas200622 return (1);
2945772Sas200622 }
2955772Sas200622
2965772Sas200622 static int
ndo_pad_pdu(ndr_stream_t * nds,unsigned long pdu_offset,unsigned long n_bytes,ndr_ref_t * ref)2978334SJose.Borrego@Sun.COM ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
2988334SJose.Borrego@Sun.COM unsigned long n_bytes, ndr_ref_t *ref)
2995772Sas200622 {
3005772Sas200622 unsigned char *data;
3015772Sas200622
3028334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset;
3035772Sas200622 data += pdu_offset;
3045772Sas200622
3058334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
3065772Sas200622
3075772Sas200622 bzero(data, n_bytes);
3085772Sas200622 return (1);
3095772Sas200622 }
3105772Sas200622
3115772Sas200622 /*
3128334SJose.Borrego@Sun.COM * ndo_get_pdu
3135772Sas200622 *
3145772Sas200622 * The swap flag is 1 if NDR knows that the byte-order in the PDU
3155772Sas200622 * is different from the local system.
3165772Sas200622 *
3175772Sas200622 * Returns 1 on success or 0 to indicate failure.
3185772Sas200622 */
3195772Sas200622 static int
ndo_get_pdu(ndr_stream_t * nds,unsigned long pdu_offset,unsigned long n_bytes,char * buf,int swap_bytes,ndr_ref_t * ref)3208334SJose.Borrego@Sun.COM ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3218334SJose.Borrego@Sun.COM unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3225772Sas200622 {
3235772Sas200622 unsigned char *data;
3245772Sas200622 char hexbuf[NDOBUFSZ];
3255772Sas200622
3268334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset;
3275772Sas200622 data += pdu_offset;
3285772Sas200622
3298334SJose.Borrego@Sun.COM ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
3305772Sas200622
3318334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "get %d@%-3d = %s",
3325772Sas200622 n_bytes, pdu_offset, hexbuf);
3335772Sas200622
3345772Sas200622 if (!swap_bytes)
3355772Sas200622 bcopy(data, buf, n_bytes);
3365772Sas200622 else
3378334SJose.Borrego@Sun.COM nds_bswap(data, (unsigned char *)buf, n_bytes);
3385772Sas200622
3395772Sas200622 return (1);
3405772Sas200622 }
3415772Sas200622
3425772Sas200622 /*
3438334SJose.Borrego@Sun.COM * ndo_put_pdu
3445772Sas200622 *
3455772Sas200622 * This is a receiver makes right protocol. So we do not need
3465772Sas200622 * to be concerned about the byte-order of an outgoing PDU.
3475772Sas200622 */
3485772Sas200622 /*ARGSUSED*/
3495772Sas200622 static int
ndo_put_pdu(ndr_stream_t * nds,unsigned long pdu_offset,unsigned long n_bytes,char * buf,int swap_bytes,ndr_ref_t * ref)3508334SJose.Borrego@Sun.COM ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3518334SJose.Borrego@Sun.COM unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3525772Sas200622 {
3535772Sas200622 unsigned char *data;
3545772Sas200622 char hexbuf[NDOBUFSZ];
3555772Sas200622
3568334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset;
3575772Sas200622 data += pdu_offset;
3585772Sas200622
3598334SJose.Borrego@Sun.COM ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3605772Sas200622
3618334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "put %d@%-3d = %s",
3625772Sas200622 n_bytes, pdu_offset, hexbuf);
3635772Sas200622
3645772Sas200622 bcopy(buf, data, n_bytes);
3655772Sas200622 return (1);
3665772Sas200622 }
3675772Sas200622
3685772Sas200622 static void
ndo_tattle(ndr_stream_t * nds,char * what,ndr_ref_t * ref)3698334SJose.Borrego@Sun.COM ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
3705772Sas200622 {
3718334SJose.Borrego@Sun.COM ndo_printf(nds, ref, what);
3725772Sas200622 }
3735772Sas200622
3745772Sas200622 static void
ndo_tattle_error(ndr_stream_t * nds,ndr_ref_t * ref)3758334SJose.Borrego@Sun.COM ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
3765772Sas200622 {
3775772Sas200622 unsigned char *data;
3785772Sas200622 char hexbuf[NDOBUFSZ];
3795772Sas200622
38011963SAfshin.Ardakani@Sun.COM if (nds->pdu_base_addr != NULL) {
38111963SAfshin.Ardakani@Sun.COM data = (unsigned char *)nds->pdu_base_offset;
38211963SAfshin.Ardakani@Sun.COM if (ref)
38311963SAfshin.Ardakani@Sun.COM data += ref->pdu_offset;
38411963SAfshin.Ardakani@Sun.COM else
38511963SAfshin.Ardakani@Sun.COM data += nds->pdu_scan_offset;
3865772Sas200622
38711963SAfshin.Ardakani@Sun.COM ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
38811963SAfshin.Ardakani@Sun.COM } else {
38911963SAfshin.Ardakani@Sun.COM bzero(hexbuf, NDOBUFSZ);
39011963SAfshin.Ardakani@Sun.COM }
3915772Sas200622
3928334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
3938334SJose.Borrego@Sun.COM nds->error, nds->error_ref, nds->pdu_scan_offset,
3948334SJose.Borrego@Sun.COM nds->pdu_size, nds->pdu_max_size);
3958334SJose.Borrego@Sun.COM ndo_printf(nds, ref, " %s", hexbuf);
3965772Sas200622 }
3975772Sas200622
3985772Sas200622 /*
3998334SJose.Borrego@Sun.COM * ndo_reset
4005772Sas200622 *
4015772Sas200622 * Reset a stream: zap the outer_queue. We don't need to tamper
4025772Sas200622 * with the stream heap: it's handled externally to the stream.
4035772Sas200622 */
4045772Sas200622 static int
ndo_reset(ndr_stream_t * nds)4058334SJose.Borrego@Sun.COM ndo_reset(ndr_stream_t *nds)
4065772Sas200622 {
4078334SJose.Borrego@Sun.COM ndo_printf(nds, 0, "reset");
4085772Sas200622
4098334SJose.Borrego@Sun.COM nds->pdu_size = 0;
4108334SJose.Borrego@Sun.COM nds->pdu_scan_offset = 0;
4118334SJose.Borrego@Sun.COM nds->outer_queue_head = 0;
4128334SJose.Borrego@Sun.COM nds->outer_current = 0;
4138334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head;
4145772Sas200622
4155772Sas200622 return (1);
4165772Sas200622 }
4175772Sas200622
4185772Sas200622 /*
4198334SJose.Borrego@Sun.COM * ndo_destruct
4205772Sas200622 *
4216482Samw * Destruct a stream: zap the outer_queue.
4226482Samw * Note: heap management (creation/destruction) is external to the stream.
4235772Sas200622 */
4245772Sas200622 static void
ndo_destruct(ndr_stream_t * nds)4258334SJose.Borrego@Sun.COM ndo_destruct(ndr_stream_t *nds)
4265772Sas200622 {
4276482Samw ndr_frag_t *frag;
4286482Samw
4298334SJose.Borrego@Sun.COM ndo_printf(nds, 0, "destruct");
4308334SJose.Borrego@Sun.COM
4318334SJose.Borrego@Sun.COM if (nds == NULL)
4328334SJose.Borrego@Sun.COM return;
4335772Sas200622
4348334SJose.Borrego@Sun.COM if (nds->pdu_base_addr != NULL) {
4358334SJose.Borrego@Sun.COM free(nds->pdu_base_addr);
4368334SJose.Borrego@Sun.COM nds->pdu_base_addr = NULL;
4378334SJose.Borrego@Sun.COM nds->pdu_base_offset = 0;
4385772Sas200622 }
4395772Sas200622
4408334SJose.Borrego@Sun.COM while ((frag = nds->frags.head) != NULL) {
4418334SJose.Borrego@Sun.COM nds->frags.head = frag->next;
4426482Samw free(frag);
4436482Samw }
4446482Samw
4458334SJose.Borrego@Sun.COM bzero(&nds->frags, sizeof (ndr_fraglist_t));
4467052Samw
4478334SJose.Borrego@Sun.COM nds->outer_queue_head = 0;
4488334SJose.Borrego@Sun.COM nds->outer_current = 0;
4498334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head;
4505772Sas200622 }
4515772Sas200622
4525772Sas200622 /*
4535772Sas200622 * Printf style formatting for NDR operations.
4545772Sas200622 */
4555772Sas200622 void
ndo_printf(ndr_stream_t * nds,ndr_ref_t * ref,const char * fmt,...)4568334SJose.Borrego@Sun.COM ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
4575772Sas200622 {
4585772Sas200622 va_list ap;
4595772Sas200622 char buf[NDOBUFSZ];
4605772Sas200622
4615772Sas200622 va_start(ap, fmt);
4625772Sas200622 (void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4635772Sas200622 va_end(ap);
4645772Sas200622
4658334SJose.Borrego@Sun.COM if (nds)
4668334SJose.Borrego@Sun.COM ndo_fmt(nds, ref, buf);
4675772Sas200622 else
4688334SJose.Borrego@Sun.COM ndo_trace(buf);
4695772Sas200622 }
4705772Sas200622
4715772Sas200622 /*
4725772Sas200622 * Main output formatter for NDR operations.
4735772Sas200622 *
4745772Sas200622 * UI 03 ... rpc_vers get 1@0 = 5 {05}
4755772Sas200622 * UI 03 ... rpc_vers_minor get 1@1 = 0 {00}
4765772Sas200622 *
4775772Sas200622 * U Marshalling flag (M=marshal, U=unmarshal)
4785772Sas200622 * I Direction flag (I=in, O=out)
4795772Sas200622 * ... Field name
4805772Sas200622 * get PDU operation (get or put)
4815772Sas200622 * 1@0 Bytes @ offset (i.e. 1 byte at offset 0)
4825772Sas200622 * {05} Value
4835772Sas200622 */
4845772Sas200622 void
ndo_fmt(ndr_stream_t * nds,ndr_ref_t * ref,char * note)4858334SJose.Borrego@Sun.COM ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
4865772Sas200622 {
4878334SJose.Borrego@Sun.COM ndr_ref_t *p;
4888334SJose.Borrego@Sun.COM int indent;
4898334SJose.Borrego@Sun.COM char ref_name[NDOBUFSZ];
4908334SJose.Borrego@Sun.COM char buf[NDOBUFSZ];
4918334SJose.Borrego@Sun.COM int m_op_c = '?', dir_c = '?';
4925772Sas200622
4938334SJose.Borrego@Sun.COM switch (nds->m_op) {
4945772Sas200622 case 0: m_op_c = '-'; break;
4955772Sas200622 case NDR_M_OP_MARSHALL: m_op_c = 'M'; break;
4965772Sas200622 case NDR_M_OP_UNMARSHALL: m_op_c = 'U'; break;
4975772Sas200622 default: m_op_c = '?'; break;
4985772Sas200622 }
4995772Sas200622
5008334SJose.Borrego@Sun.COM switch (nds->dir) {
5015772Sas200622 case 0: dir_c = '-'; break;
5025772Sas200622 case NDR_DIR_IN: dir_c = 'I'; break;
5035772Sas200622 case NDR_DIR_OUT: dir_c = 'O'; break;
5045772Sas200622 default: dir_c = '?'; break;
5055772Sas200622 }
5065772Sas200622
5075772Sas200622 for (indent = 0, p = ref; p; p = p->enclosing)
5085772Sas200622 indent++;
5095772Sas200622
5105772Sas200622 if (ref && ref->name) {
5115772Sas200622 if (*ref->name == '[' && ref->enclosing) {
5125772Sas200622 indent--;
5135772Sas200622 (void) snprintf(ref_name, NDOBUFSZ, "%s%s",
5145772Sas200622 ref->enclosing->name, ref->name);
5155772Sas200622 } else {
5165772Sas200622 (void) strlcpy(ref_name, ref->name, NDOBUFSZ);
5175772Sas200622 }
5185772Sas200622 } else {
5195772Sas200622 (void) strlcpy(ref_name, "----", NDOBUFSZ);
5205772Sas200622 }
5215772Sas200622
5229914Samw@Sun.COM (void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s %s",
5239914Samw@Sun.COM m_op_c, dir_c, indent,
5245772Sas200622 "....+....+....+....+....+....",
5255772Sas200622 20 - indent, ref_name, note);
5265772Sas200622
5278334SJose.Borrego@Sun.COM ndo_trace(buf);
5285772Sas200622 }
5295772Sas200622
5305772Sas200622 /*ARGSUSED*/
5315772Sas200622 void
ndo_trace(const char * s)5328334SJose.Borrego@Sun.COM ndo_trace(const char *s)
5335772Sas200622 {
5345772Sas200622 /*
5355772Sas200622 * Temporary fbt for dtrace until user space sdt enabled.
5365772Sas200622 */
5375772Sas200622 }
5385772Sas200622
5395772Sas200622 /*
5405772Sas200622 * Format data as hex bytes (limit is 10 bytes):
5415772Sas200622 *
5425772Sas200622 * 1188689424 {10 f6 d9 46}
5435772Sas200622 *
5445772Sas200622 * If the input data is greater than 10 bytes, an ellipsis will
5455772Sas200622 * be inserted before the closing brace.
5465772Sas200622 */
5475772Sas200622 static void
ndo_hexfmt(uint8_t * data,int size,int swap_bytes,char * buf,int len)5488334SJose.Borrego@Sun.COM ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5495772Sas200622 {
5505772Sas200622 char *p = buf;
5515772Sas200622 int interp = 1;
5525772Sas200622 uint32_t c;
5535772Sas200622 int n;
5545772Sas200622 int i;
5555772Sas200622
5565772Sas200622 n = (size > 10) ? 10 : size;
5575772Sas200622 if (n > len-1)
5585772Sas200622 n = len-1;
5595772Sas200622
5605772Sas200622 switch (size) {
5615772Sas200622 case 1:
5625772Sas200622 c = *(uint8_t *)data;
5635772Sas200622 break;
5645772Sas200622 case 2:
5655772Sas200622 if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5665772Sas200622 c = *(uint16_t *)data;
5675772Sas200622 else
5685772Sas200622 c = (data[0] << 8) | data[1];
5695772Sas200622 break;
5705772Sas200622 case 4:
5715772Sas200622 if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5725772Sas200622 c = *(uint32_t *)data;
5735772Sas200622 } else {
5745772Sas200622 c = (data[0] << 24) | (data[1] << 16)
5755772Sas200622 | (data[2] << 8) | data[3];
5765772Sas200622 }
5775772Sas200622 break;
5785772Sas200622 default:
5795772Sas200622 c = 0;
5805772Sas200622 interp = 0;
5815772Sas200622 break;
5825772Sas200622 }
5835772Sas200622
5845772Sas200622 if (interp)
5855772Sas200622 p += sprintf(p, "%4u {", c);
5865772Sas200622 else
5875772Sas200622 p += sprintf(p, " {");
5885772Sas200622
5895772Sas200622 p += sprintf(p, "%02x", data[0]);
5905772Sas200622 for (i = 1; i < n; i++)
5915772Sas200622 p += sprintf(p, " %02x", data[i]);
5925772Sas200622 if (size > 10)
5935772Sas200622 p += sprintf(p, " ...}");
5945772Sas200622 else
5955772Sas200622 p += sprintf(p, "}");
5965772Sas200622
5975772Sas200622 /*
5985772Sas200622 * Show c if it's a printable character or wide-char.
5995772Sas200622 */
6005772Sas200622 if (size < 4 && isprint((uint8_t)c))
6015772Sas200622 (void) sprintf(p, " %c", (uint8_t)c);
6025772Sas200622 }
603