1dc20a302Sas200622 /* 2dc20a302Sas200622 * CDDL HEADER START 3dc20a302Sas200622 * 4dc20a302Sas200622 * The contents of this file are subject to the terms of the 5dc20a302Sas200622 * Common Development and Distribution License (the "License"). 6dc20a302Sas200622 * You may not use this file except in compliance with the License. 7dc20a302Sas200622 * 8dc20a302Sas200622 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9dc20a302Sas200622 * or http://www.opensolaris.org/os/licensing. 10dc20a302Sas200622 * See the License for the specific language governing permissions 11dc20a302Sas200622 * and limitations under the License. 12dc20a302Sas200622 * 13dc20a302Sas200622 * When distributing Covered Code, include this CDDL HEADER in each 14dc20a302Sas200622 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15dc20a302Sas200622 * If applicable, add the following below this CDDL HEADER, with the 16dc20a302Sas200622 * fields enclosed by brackets "[]" replaced with your own identifying 17dc20a302Sas200622 * information: Portions Copyright [yyyy] [name of copyright owner] 18dc20a302Sas200622 * 19dc20a302Sas200622 * CDDL HEADER END 20dc20a302Sas200622 */ 21148c5f43SAlan Wright 22dc20a302Sas200622 /* 23148c5f43SAlan Wright * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 24*68b2bbf2SGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 25dc20a302Sas200622 */ 26dc20a302Sas200622 27dc20a302Sas200622 /* 288d7e4166Sjose borrego * Server-side NDR stream (PDU) operations. Stream operations should 298d7e4166Sjose borrego * return TRUE (non-zero) on success or FALSE (zero or a null pointer) 308d7e4166Sjose borrego * on failure. When an operation returns FALSE, including ndo_malloc() 318d7e4166Sjose borrego * returning NULL, it should set the nds->error to indicate what went 328d7e4166Sjose borrego * wrong. 33dc20a302Sas200622 * 348d7e4166Sjose borrego * When available, the relevant ndr reference is passed to the 35dc20a302Sas200622 * operation but keep in mind that it may be a null pointer. 36dc20a302Sas200622 * 378d7e4166Sjose borrego * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu() 38dc20a302Sas200622 * must never grow the PDU data. A request for out-of-bounds data is 39dc20a302Sas200622 * an error. The swap_bytes flag is 1 if NDR knows that the byte- 40dc20a302Sas200622 * order in the PDU is different from the local system. 41dc20a302Sas200622 */ 42dc20a302Sas200622 43dc20a302Sas200622 #include <sys/types.h> 44dc20a302Sas200622 #include <stdarg.h> 45dc20a302Sas200622 #include <ctype.h> 46dc20a302Sas200622 #include <stdio.h> 47dc20a302Sas200622 #include <stdlib.h> 48dc20a302Sas200622 #include <strings.h> 49dc20a302Sas200622 #include <string.h> 50dc20a302Sas200622 #include <assert.h> 51dc20a302Sas200622 52dc20a302Sas200622 #include <smbsrv/libsmb.h> 538d7e4166Sjose borrego #include <smbsrv/libmlrpc.h> 54dc20a302Sas200622 55dc20a302Sas200622 #define NDOBUFSZ 128 56dc20a302Sas200622 57dc20a302Sas200622 #define NDR_PDU_BLOCK_SIZE (4*1024) 58dc20a302Sas200622 #define NDR_PDU_BLOCK_MASK (NDR_PDU_BLOCK_SIZE - 1) 59dc20a302Sas200622 #define NDR_PDU_ALIGN(N) \ 60dc20a302Sas200622 (((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK) 61dc20a302Sas200622 #define NDR_PDU_MAX_SIZE (64*1024*1024) 62dc20a302Sas200622 638d7e4166Sjose borrego static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *); 648d7e4166Sjose borrego static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *); 658d7e4166Sjose borrego static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *); 668d7e4166Sjose borrego static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long, 678d7e4166Sjose borrego ndr_ref_t *); 688d7e4166Sjose borrego static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long, 698d7e4166Sjose borrego char *, int, ndr_ref_t *); 708d7e4166Sjose borrego static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long, 718d7e4166Sjose borrego char *, int, ndr_ref_t *); 728d7e4166Sjose borrego static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *); 738d7e4166Sjose borrego static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *); 748d7e4166Sjose borrego static int ndo_reset(ndr_stream_t *); 758d7e4166Sjose borrego static void ndo_destruct(ndr_stream_t *); 768d7e4166Sjose borrego static void ndo_hexfmt(uint8_t *, int, int, char *, int); 77dc20a302Sas200622 78dc20a302Sas200622 /* 798d7e4166Sjose borrego * The ndr stream operations table. 80dc20a302Sas200622 */ 818d7e4166Sjose borrego static ndr_stream_ops_t nds_ops = { 828d7e4166Sjose borrego ndo_malloc, 838d7e4166Sjose borrego ndo_free, 848d7e4166Sjose borrego ndo_grow_pdu, 858d7e4166Sjose borrego ndo_pad_pdu, 868d7e4166Sjose borrego ndo_get_pdu, 878d7e4166Sjose borrego ndo_put_pdu, 888d7e4166Sjose borrego ndo_tattle, 898d7e4166Sjose borrego ndo_tattle_error, 908d7e4166Sjose borrego ndo_reset, 918d7e4166Sjose borrego ndo_destruct 92dc20a302Sas200622 }; 93dc20a302Sas200622 94dc20a302Sas200622 /* 958d7e4166Sjose borrego * nds_bswap 96dc20a302Sas200622 * 97dc20a302Sas200622 * Copies len bytes from src to dst such that dst contains the bytes 98dc20a302Sas200622 * from src in reverse order. 99dc20a302Sas200622 * 100dc20a302Sas200622 * We expect to be dealing with bytes, words, dwords etc. So the 101dc20a302Sas200622 * length must be non-zero and a power of 2. 102dc20a302Sas200622 */ 103dc20a302Sas200622 void 1048d7e4166Sjose borrego nds_bswap(void *srcbuf, void *dstbuf, size_t len) 105dc20a302Sas200622 { 106dc20a302Sas200622 uint8_t *src = (uint8_t *)srcbuf; 107dc20a302Sas200622 uint8_t *dst = (uint8_t *)dstbuf; 108dc20a302Sas200622 109dc20a302Sas200622 if ((len != 0) && ((len & (len - 1)) == 0)) { 110dc20a302Sas200622 src += len; 111dc20a302Sas200622 112dc20a302Sas200622 while (len--) 113dc20a302Sas200622 *dst++ = *(--src); 114dc20a302Sas200622 } 115dc20a302Sas200622 } 116dc20a302Sas200622 117dc20a302Sas200622 /* 1188d7e4166Sjose borrego * nds_initialize 119dc20a302Sas200622 * 120dc20a302Sas200622 * Initialize a stream. Sets up the PDU parameters and assigns the stream 121dc20a302Sas200622 * operations and the reference to the heap. An external heap is provided 122dc20a302Sas200622 * to the stream, rather than each stream creating its own heap. 123dc20a302Sas200622 */ 124fe1c642dSBill Krier int 1258d7e4166Sjose borrego nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint, 1268d7e4166Sjose borrego int composite_op, ndr_heap_t *heap) 127dc20a302Sas200622 { 128dc20a302Sas200622 unsigned size; 129dc20a302Sas200622 1308d7e4166Sjose borrego assert(nds); 131dc20a302Sas200622 assert(heap); 132dc20a302Sas200622 1338d7e4166Sjose borrego bzero(nds, sizeof (*nds)); 1349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States nds->ndo = &nds_ops; 1359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States nds->heap = (struct ndr_heap *)heap; 136dc20a302Sas200622 1379fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (pdu_size_hint > NDR_PDU_MAX_SIZE) { 1389fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States nds->error = NDR_ERR_BOUNDS_CHECK; 1399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States nds->error_ref = __LINE__; 1409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States NDS_TATTLE_ERROR(nds, NULL, NULL); 1419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (NDR_DRC_FAULT_RESOURCE_1); 1429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States } 143dc20a302Sas200622 144dc20a302Sas200622 size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint; 145fe1c642dSBill Krier 146fe1c642dSBill Krier if ((nds->pdu_base_addr = malloc(size)) == NULL) { 147fe1c642dSBill Krier nds->error = NDR_ERR_MALLOC_FAILED; 148fe1c642dSBill Krier nds->error_ref = __LINE__; 149fe1c642dSBill Krier NDS_TATTLE_ERROR(nds, NULL, NULL); 150fe1c642dSBill Krier return (NDR_DRC_FAULT_OUT_OF_MEMORY); 151fe1c642dSBill Krier } 152dc20a302Sas200622 1538d7e4166Sjose borrego nds->pdu_max_size = size; 1548d7e4166Sjose borrego nds->pdu_size = 0; 1558d7e4166Sjose borrego nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr; 156dc20a302Sas200622 1578d7e4166Sjose borrego nds->m_op = NDR_MODE_TO_M_OP(composite_op); 1588d7e4166Sjose borrego nds->dir = NDR_MODE_TO_DIR(composite_op); 159dc20a302Sas200622 1608d7e4166Sjose borrego nds->outer_queue_tailp = &nds->outer_queue_head; 161fe1c642dSBill Krier return (0); 162dc20a302Sas200622 } 163dc20a302Sas200622 164dc20a302Sas200622 /* 1658d7e4166Sjose borrego * nds_destruct 166dc20a302Sas200622 * 167dc20a302Sas200622 * Destroy a stream. This is an external interface to provide access to 168dc20a302Sas200622 * the stream's destruct operation. 169dc20a302Sas200622 */ 170dc20a302Sas200622 void 1718d7e4166Sjose borrego nds_destruct(ndr_stream_t *nds) 172dc20a302Sas200622 { 1730658b32dSAlan Wright if ((nds == NULL) || (nds->ndo == NULL)) 1740658b32dSAlan Wright return; 1750658b32dSAlan Wright 1768d7e4166Sjose borrego NDS_DESTRUCT(nds); 177dc20a302Sas200622 } 178dc20a302Sas200622 179dc20a302Sas200622 /* 1800658b32dSAlan Wright * Print NDR stream state. 1810658b32dSAlan Wright */ 1820658b32dSAlan Wright void 1830658b32dSAlan Wright nds_show_state(ndr_stream_t *nds) 1840658b32dSAlan Wright { 1850658b32dSAlan Wright if (nds == NULL) { 1860658b32dSAlan Wright ndo_printf(NULL, NULL, "nds: <null"); 1870658b32dSAlan Wright return; 1880658b32dSAlan Wright } 1890658b32dSAlan Wright 1900658b32dSAlan Wright ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d", 1910658b32dSAlan Wright nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size, 1920658b32dSAlan Wright nds->pdu_scan_offset); 1930658b32dSAlan Wright } 1940658b32dSAlan Wright 1950658b32dSAlan Wright /* 1968d7e4166Sjose borrego * ndo_malloc 197dc20a302Sas200622 * 198dc20a302Sas200622 * Allocate memory from the stream heap. 199dc20a302Sas200622 */ 200dc20a302Sas200622 /*ARGSUSED*/ 201dc20a302Sas200622 static char * 2028d7e4166Sjose borrego ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref) 203dc20a302Sas200622 { 2048d7e4166Sjose borrego return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len)); 205dc20a302Sas200622 } 206dc20a302Sas200622 207dc20a302Sas200622 /* 2088d7e4166Sjose borrego * ndo_free 209dc20a302Sas200622 * 210dc20a302Sas200622 * Always succeeds: cannot free individual stream allocations. 211dc20a302Sas200622 */ 212dc20a302Sas200622 /*ARGSUSED*/ 213dc20a302Sas200622 static int 2148d7e4166Sjose borrego ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref) 215dc20a302Sas200622 { 216dc20a302Sas200622 return (1); 217dc20a302Sas200622 } 218dc20a302Sas200622 219dc20a302Sas200622 /* 2208d7e4166Sjose borrego * ndo_grow_pdu 221dc20a302Sas200622 * 222dc20a302Sas200622 * This is the only place that should change the size of the PDU. If the 223dc20a302Sas200622 * desired offset is beyond the current PDU size, we realloc the PDU 224dc20a302Sas200622 * buffer to accommodate the request. For efficiency, the PDU is always 225dc20a302Sas200622 * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU 226dc20a302Sas200622 * beyond NDR_PDU_MAX_SIZE are rejected. 227dc20a302Sas200622 * 228dc20a302Sas200622 * Returns 1 to indicate success. Otherwise 0 to indicate failure. 229dc20a302Sas200622 */ 230dc20a302Sas200622 static int 2318d7e4166Sjose borrego ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref) 232dc20a302Sas200622 { 233dc20a302Sas200622 unsigned char *pdu_addr; 234dc20a302Sas200622 unsigned pdu_max_size; 235dc20a302Sas200622 2368d7e4166Sjose borrego ndo_printf(nds, ref, "grow %d", want_end_offset); 237dc20a302Sas200622 2388d7e4166Sjose borrego pdu_max_size = nds->pdu_max_size; 239dc20a302Sas200622 240dc20a302Sas200622 if (want_end_offset > pdu_max_size) { 241dc20a302Sas200622 pdu_max_size = NDR_PDU_ALIGN(want_end_offset); 242dc20a302Sas200622 243dc20a302Sas200622 if (pdu_max_size >= NDR_PDU_MAX_SIZE) 244dc20a302Sas200622 return (0); 245dc20a302Sas200622 2468d7e4166Sjose borrego pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size); 247dc20a302Sas200622 if (pdu_addr == 0) 248dc20a302Sas200622 return (0); 249dc20a302Sas200622 2508d7e4166Sjose borrego nds->pdu_max_size = pdu_max_size; 2518d7e4166Sjose borrego nds->pdu_base_addr = pdu_addr; 2528d7e4166Sjose borrego nds->pdu_base_offset = (unsigned long)pdu_addr; 253dc20a302Sas200622 } 254dc20a302Sas200622 2558d7e4166Sjose borrego nds->pdu_size = want_end_offset; 256dc20a302Sas200622 return (1); 257dc20a302Sas200622 } 258dc20a302Sas200622 259dc20a302Sas200622 static int 2608d7e4166Sjose borrego ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 2618d7e4166Sjose borrego unsigned long n_bytes, ndr_ref_t *ref) 262dc20a302Sas200622 { 263dc20a302Sas200622 unsigned char *data; 264dc20a302Sas200622 2658d7e4166Sjose borrego data = (unsigned char *)nds->pdu_base_offset; 266dc20a302Sas200622 data += pdu_offset; 267dc20a302Sas200622 2688d7e4166Sjose borrego ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset); 269dc20a302Sas200622 270dc20a302Sas200622 bzero(data, n_bytes); 271dc20a302Sas200622 return (1); 272dc20a302Sas200622 } 273dc20a302Sas200622 274dc20a302Sas200622 /* 2758d7e4166Sjose borrego * ndo_get_pdu 276dc20a302Sas200622 * 277dc20a302Sas200622 * The swap flag is 1 if NDR knows that the byte-order in the PDU 278dc20a302Sas200622 * is different from the local system. 279dc20a302Sas200622 * 280dc20a302Sas200622 * Returns 1 on success or 0 to indicate failure. 281dc20a302Sas200622 */ 282dc20a302Sas200622 static int 2838d7e4166Sjose borrego ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 2848d7e4166Sjose borrego unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref) 285dc20a302Sas200622 { 286dc20a302Sas200622 unsigned char *data; 287dc20a302Sas200622 char hexbuf[NDOBUFSZ]; 288dc20a302Sas200622 2898d7e4166Sjose borrego data = (unsigned char *)nds->pdu_base_offset; 290dc20a302Sas200622 data += pdu_offset; 291dc20a302Sas200622 2928d7e4166Sjose borrego ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ); 293dc20a302Sas200622 2948d7e4166Sjose borrego ndo_printf(nds, ref, "get %d@%-3d = %s", 295dc20a302Sas200622 n_bytes, pdu_offset, hexbuf); 296dc20a302Sas200622 297dc20a302Sas200622 if (!swap_bytes) 298dc20a302Sas200622 bcopy(data, buf, n_bytes); 299dc20a302Sas200622 else 3008d7e4166Sjose borrego nds_bswap(data, (unsigned char *)buf, n_bytes); 301dc20a302Sas200622 302dc20a302Sas200622 return (1); 303dc20a302Sas200622 } 304dc20a302Sas200622 305dc20a302Sas200622 /* 3068d7e4166Sjose borrego * ndo_put_pdu 307dc20a302Sas200622 * 308dc20a302Sas200622 * This is a receiver makes right protocol. So we do not need 309dc20a302Sas200622 * to be concerned about the byte-order of an outgoing PDU. 310dc20a302Sas200622 */ 311dc20a302Sas200622 /*ARGSUSED*/ 312dc20a302Sas200622 static int 3138d7e4166Sjose borrego ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 3148d7e4166Sjose borrego unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref) 315dc20a302Sas200622 { 316dc20a302Sas200622 unsigned char *data; 317dc20a302Sas200622 char hexbuf[NDOBUFSZ]; 318dc20a302Sas200622 3198d7e4166Sjose borrego data = (unsigned char *)nds->pdu_base_offset; 320dc20a302Sas200622 data += pdu_offset; 321dc20a302Sas200622 3228d7e4166Sjose borrego ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ); 323dc20a302Sas200622 3248d7e4166Sjose borrego ndo_printf(nds, ref, "put %d@%-3d = %s", 325dc20a302Sas200622 n_bytes, pdu_offset, hexbuf); 326dc20a302Sas200622 327dc20a302Sas200622 bcopy(buf, data, n_bytes); 328dc20a302Sas200622 return (1); 329dc20a302Sas200622 } 330dc20a302Sas200622 331dc20a302Sas200622 static void 3328d7e4166Sjose borrego ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref) 333dc20a302Sas200622 { 3348d7e4166Sjose borrego ndo_printf(nds, ref, what); 335dc20a302Sas200622 } 336dc20a302Sas200622 337dc20a302Sas200622 static void 3388d7e4166Sjose borrego ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref) 339dc20a302Sas200622 { 340dc20a302Sas200622 unsigned char *data; 341dc20a302Sas200622 char hexbuf[NDOBUFSZ]; 342dc20a302Sas200622 3439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (nds->pdu_base_addr != NULL) { 3448d7e4166Sjose borrego data = (unsigned char *)nds->pdu_base_offset; 345dc20a302Sas200622 if (ref) 346dc20a302Sas200622 data += ref->pdu_offset; 347dc20a302Sas200622 else 3488d7e4166Sjose borrego data += nds->pdu_scan_offset; 349dc20a302Sas200622 3508d7e4166Sjose borrego ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ); 3519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States } else { 3529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States bzero(hexbuf, NDOBUFSZ); 3539fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States } 354dc20a302Sas200622 3558d7e4166Sjose borrego ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d", 3568d7e4166Sjose borrego nds->error, nds->error_ref, nds->pdu_scan_offset, 3578d7e4166Sjose borrego nds->pdu_size, nds->pdu_max_size); 3588d7e4166Sjose borrego ndo_printf(nds, ref, " %s", hexbuf); 359dc20a302Sas200622 } 360dc20a302Sas200622 361dc20a302Sas200622 /* 3628d7e4166Sjose borrego * ndo_reset 363dc20a302Sas200622 * 364dc20a302Sas200622 * Reset a stream: zap the outer_queue. We don't need to tamper 365dc20a302Sas200622 * with the stream heap: it's handled externally to the stream. 366dc20a302Sas200622 */ 367dc20a302Sas200622 static int 3688d7e4166Sjose borrego ndo_reset(ndr_stream_t *nds) 369dc20a302Sas200622 { 3708d7e4166Sjose borrego ndo_printf(nds, 0, "reset"); 371dc20a302Sas200622 3728d7e4166Sjose borrego nds->pdu_size = 0; 3738d7e4166Sjose borrego nds->pdu_scan_offset = 0; 3748d7e4166Sjose borrego nds->outer_queue_head = 0; 3758d7e4166Sjose borrego nds->outer_current = 0; 3768d7e4166Sjose borrego nds->outer_queue_tailp = &nds->outer_queue_head; 377dc20a302Sas200622 378dc20a302Sas200622 return (1); 379dc20a302Sas200622 } 380dc20a302Sas200622 381dc20a302Sas200622 /* 3828d7e4166Sjose borrego * ndo_destruct 383dc20a302Sas200622 * 38419d41fccSamw * Destruct a stream: zap the outer_queue. 38519d41fccSamw * Note: heap management (creation/destruction) is external to the stream. 386dc20a302Sas200622 */ 387dc20a302Sas200622 static void 3888d7e4166Sjose borrego ndo_destruct(ndr_stream_t *nds) 389dc20a302Sas200622 { 39019d41fccSamw 3918d7e4166Sjose borrego ndo_printf(nds, 0, "destruct"); 392dc20a302Sas200622 3938d7e4166Sjose borrego if (nds == NULL) 3948d7e4166Sjose borrego return; 3958d7e4166Sjose borrego 3968d7e4166Sjose borrego if (nds->pdu_base_addr != NULL) { 3978d7e4166Sjose borrego free(nds->pdu_base_addr); 3988d7e4166Sjose borrego nds->pdu_base_addr = NULL; 3998d7e4166Sjose borrego nds->pdu_base_offset = 0; 400dc20a302Sas200622 } 401dc20a302Sas200622 4028d7e4166Sjose borrego nds->outer_queue_head = 0; 4038d7e4166Sjose borrego nds->outer_current = 0; 4048d7e4166Sjose borrego nds->outer_queue_tailp = &nds->outer_queue_head; 405dc20a302Sas200622 } 406dc20a302Sas200622 407dc20a302Sas200622 /* 408dc20a302Sas200622 * Printf style formatting for NDR operations. 409dc20a302Sas200622 */ 410dc20a302Sas200622 void 4118d7e4166Sjose borrego ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...) 412dc20a302Sas200622 { 413dc20a302Sas200622 va_list ap; 414dc20a302Sas200622 char buf[NDOBUFSZ]; 415dc20a302Sas200622 416dc20a302Sas200622 va_start(ap, fmt); 417dc20a302Sas200622 (void) vsnprintf(buf, NDOBUFSZ, fmt, ap); 418dc20a302Sas200622 va_end(ap); 419dc20a302Sas200622 4208d7e4166Sjose borrego if (nds) 4218d7e4166Sjose borrego ndo_fmt(nds, ref, buf); 422dc20a302Sas200622 else 4238d7e4166Sjose borrego ndo_trace(buf); 424dc20a302Sas200622 } 425dc20a302Sas200622 426dc20a302Sas200622 /* 427dc20a302Sas200622 * Main output formatter for NDR operations. 428dc20a302Sas200622 * 429dc20a302Sas200622 * UI 03 ... rpc_vers get 1@0 = 5 {05} 430dc20a302Sas200622 * UI 03 ... rpc_vers_minor get 1@1 = 0 {00} 431dc20a302Sas200622 * 432dc20a302Sas200622 * U Marshalling flag (M=marshal, U=unmarshal) 433dc20a302Sas200622 * I Direction flag (I=in, O=out) 434dc20a302Sas200622 * ... Field name 435dc20a302Sas200622 * get PDU operation (get or put) 436dc20a302Sas200622 * 1@0 Bytes @ offset (i.e. 1 byte at offset 0) 437dc20a302Sas200622 * {05} Value 438dc20a302Sas200622 */ 439dc20a302Sas200622 void 4408d7e4166Sjose borrego ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note) 441dc20a302Sas200622 { 4428d7e4166Sjose borrego ndr_ref_t *p; 443dc20a302Sas200622 int indent; 444dc20a302Sas200622 char ref_name[NDOBUFSZ]; 445dc20a302Sas200622 char buf[NDOBUFSZ]; 446dc20a302Sas200622 int m_op_c = '?', dir_c = '?'; 447dc20a302Sas200622 4488d7e4166Sjose borrego switch (nds->m_op) { 449dc20a302Sas200622 case 0: m_op_c = '-'; break; 450dc20a302Sas200622 case NDR_M_OP_MARSHALL: m_op_c = 'M'; break; 451dc20a302Sas200622 case NDR_M_OP_UNMARSHALL: m_op_c = 'U'; break; 452dc20a302Sas200622 default: m_op_c = '?'; break; 453dc20a302Sas200622 } 454dc20a302Sas200622 4558d7e4166Sjose borrego switch (nds->dir) { 456dc20a302Sas200622 case 0: dir_c = '-'; break; 457dc20a302Sas200622 case NDR_DIR_IN: dir_c = 'I'; break; 458dc20a302Sas200622 case NDR_DIR_OUT: dir_c = 'O'; break; 459dc20a302Sas200622 default: dir_c = '?'; break; 460dc20a302Sas200622 } 461dc20a302Sas200622 462dc20a302Sas200622 for (indent = 0, p = ref; p; p = p->enclosing) 463dc20a302Sas200622 indent++; 464dc20a302Sas200622 465dc20a302Sas200622 if (ref && ref->name) { 466dc20a302Sas200622 if (*ref->name == '[' && ref->enclosing) { 467dc20a302Sas200622 indent--; 468dc20a302Sas200622 (void) snprintf(ref_name, NDOBUFSZ, "%s%s", 469dc20a302Sas200622 ref->enclosing->name, ref->name); 470dc20a302Sas200622 } else { 471dc20a302Sas200622 (void) strlcpy(ref_name, ref->name, NDOBUFSZ); 472dc20a302Sas200622 } 473dc20a302Sas200622 } else { 474dc20a302Sas200622 (void) strlcpy(ref_name, "----", NDOBUFSZ); 475dc20a302Sas200622 } 476dc20a302Sas200622 477b1352070SAlan Wright (void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s %s", 478b1352070SAlan Wright m_op_c, dir_c, indent, 479dc20a302Sas200622 "....+....+....+....+....+....", 480dc20a302Sas200622 20 - indent, ref_name, note); 481dc20a302Sas200622 4828d7e4166Sjose borrego ndo_trace(buf); 483dc20a302Sas200622 } 484dc20a302Sas200622 485dc20a302Sas200622 /*ARGSUSED*/ 486dc20a302Sas200622 void 4878d7e4166Sjose borrego ndo_trace(const char *s) 488dc20a302Sas200622 { 489dc20a302Sas200622 /* 490dc20a302Sas200622 * Temporary fbt for dtrace until user space sdt enabled. 491dc20a302Sas200622 */ 492dc20a302Sas200622 } 493dc20a302Sas200622 494dc20a302Sas200622 /* 495dc20a302Sas200622 * Format data as hex bytes (limit is 10 bytes): 496dc20a302Sas200622 * 497dc20a302Sas200622 * 1188689424 {10 f6 d9 46} 498dc20a302Sas200622 * 499dc20a302Sas200622 * If the input data is greater than 10 bytes, an ellipsis will 500dc20a302Sas200622 * be inserted before the closing brace. 501dc20a302Sas200622 */ 502dc20a302Sas200622 static void 5038d7e4166Sjose borrego ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len) 504dc20a302Sas200622 { 505dc20a302Sas200622 char *p = buf; 506dc20a302Sas200622 int interp = 1; 507dc20a302Sas200622 uint32_t c; 508dc20a302Sas200622 int n; 509dc20a302Sas200622 int i; 510dc20a302Sas200622 511dc20a302Sas200622 n = (size > 10) ? 10 : size; 512dc20a302Sas200622 if (n > len-1) 513dc20a302Sas200622 n = len-1; 514dc20a302Sas200622 515dc20a302Sas200622 switch (size) { 516dc20a302Sas200622 case 1: 517dc20a302Sas200622 c = *(uint8_t *)data; 518dc20a302Sas200622 break; 519dc20a302Sas200622 case 2: 520dc20a302Sas200622 if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/ 521dc20a302Sas200622 c = *(uint16_t *)data; 522dc20a302Sas200622 else 523dc20a302Sas200622 c = (data[0] << 8) | data[1]; 524dc20a302Sas200622 break; 525dc20a302Sas200622 case 4: 526dc20a302Sas200622 if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/ 527dc20a302Sas200622 c = *(uint32_t *)data; 528dc20a302Sas200622 } else { 529dc20a302Sas200622 c = (data[0] << 24) | (data[1] << 16) 530dc20a302Sas200622 | (data[2] << 8) | data[3]; 531dc20a302Sas200622 } 532dc20a302Sas200622 break; 533dc20a302Sas200622 default: 534dc20a302Sas200622 c = 0; 535dc20a302Sas200622 interp = 0; 536dc20a302Sas200622 break; 537dc20a302Sas200622 } 538dc20a302Sas200622 539dc20a302Sas200622 if (interp) 540dc20a302Sas200622 p += sprintf(p, "%4u {", c); 541dc20a302Sas200622 else 542dc20a302Sas200622 p += sprintf(p, " {"); 543dc20a302Sas200622 544dc20a302Sas200622 p += sprintf(p, "%02x", data[0]); 545dc20a302Sas200622 for (i = 1; i < n; i++) 546dc20a302Sas200622 p += sprintf(p, " %02x", data[i]); 547dc20a302Sas200622 if (size > 10) 548dc20a302Sas200622 p += sprintf(p, " ...}"); 549dc20a302Sas200622 else 550dc20a302Sas200622 p += sprintf(p, "}"); 551dc20a302Sas200622 552dc20a302Sas200622 /* 553dc20a302Sas200622 * Show c if it's a printable character or wide-char. 554dc20a302Sas200622 */ 555dc20a302Sas200622 if (size < 4 && isprint((uint8_t)c)) 556dc20a302Sas200622 (void) sprintf(p, " %c", (uint8_t)c); 557dc20a302Sas200622 } 558