xref: /illumos-gate/usr/src/lib/libmlrpc/common/ndr_ops.c (revision fd6d41c5025e9fb45a115fc82d86e9983d1e9fd6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * Server-side NDR stream (PDU) operations. Stream operations should
29  * return TRUE (non-zero) on success or FALSE (zero or a null pointer)
30  * on failure. When an operation returns FALSE, including ndo_malloc()
31  * returning NULL, it should set the nds->error to indicate what went
32  * wrong.
33  *
34  * When available, the relevant ndr reference is passed to the
35  * operation but keep in mind that it may be a null pointer.
36  *
37  * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu()
38  * must never grow the PDU data. A request for out-of-bounds data is
39  * an error. The swap_bytes flag is 1 if NDR knows that the byte-
40  * order in the PDU is different from the local system.
41  */
42 
43 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <strings.h>
49 #include <string.h>
50 #include <assert.h>
51 
52 #include <libmlrpc.h>
53 
54 #define	NDOBUFSZ		128
55 
56 #define	NDR_PDU_BLOCK_SIZE	(4*1024)
57 #define	NDR_PDU_BLOCK_MASK	(NDR_PDU_BLOCK_SIZE - 1)
58 #define	NDR_PDU_ALIGN(N) \
59 	(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
60 #define	NDR_PDU_MAX_SIZE		(64*1024*1024)
61 
62 static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
63 static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
64 static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
65 static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
66     ndr_ref_t *);
67 static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
68     char *, int, ndr_ref_t *);
69 static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
70     char *, int, ndr_ref_t *);
71 static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
72 static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
73 static int ndo_reset(ndr_stream_t *);
74 static void ndo_destruct(ndr_stream_t *);
75 static void ndo_hexfmt(uint8_t *, int, int, char *, int);
76 
77 /*
78  * The ndr stream operations table.
79  */
80 static ndr_stream_ops_t nds_ops = {
81     ndo_malloc,
82     ndo_free,
83     ndo_grow_pdu,
84     ndo_pad_pdu,
85     ndo_get_pdu,
86     ndo_put_pdu,
87     ndo_tattle,
88     ndo_tattle_error,
89     ndo_reset,
90     ndo_destruct
91 };
92 
93 /*
94  * nds_bswap
95  *
96  * Copies len bytes from src to dst such that dst contains the bytes
97  * from src in reverse order.
98  *
99  * We expect to be dealing with bytes, words, dwords etc. So the
100  * length must be non-zero and a power of 2.
101  */
102 void
103 nds_bswap(void *srcbuf, void *dstbuf, size_t len)
104 {
105 	uint8_t *src = (uint8_t *)srcbuf;
106 	uint8_t *dst = (uint8_t *)dstbuf;
107 
108 	if ((len != 0) && ((len & (len - 1)) == 0)) {
109 		src += len;
110 
111 		while (len--)
112 			*dst++ = *(--src);
113 	}
114 }
115 
116 /*
117  * nds_initialize
118  *
119  * Initialize a stream. Sets up the PDU parameters and assigns the stream
120  * operations and the reference to the heap. An external heap is provided
121  * to the stream, rather than each stream creating its own heap.
122  */
123 int
124 nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
125     int composite_op, ndr_heap_t *heap)
126 {
127 	unsigned size;
128 
129 	assert(nds);
130 	assert(heap);
131 
132 	bzero(nds, sizeof (*nds));
133 	nds->ndo = &nds_ops;
134 	nds->heap = (struct ndr_heap *)heap;
135 
136 	if (pdu_size_hint > NDR_PDU_MAX_SIZE) {
137 		nds->error = NDR_ERR_BOUNDS_CHECK;
138 		nds->error_ref = __LINE__;
139 		NDS_TATTLE_ERROR(nds, NULL, NULL);
140 		return (NDR_DRC_FAULT_RESOURCE_1);
141 	}
142 
143 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
144 
145 	if ((nds->pdu_base_addr = malloc(size)) == NULL) {
146 		nds->error = NDR_ERR_MALLOC_FAILED;
147 		nds->error_ref = __LINE__;
148 		NDS_TATTLE_ERROR(nds, NULL, NULL);
149 		return (NDR_DRC_FAULT_OUT_OF_MEMORY);
150 	}
151 
152 	nds->pdu_max_size = size;
153 	nds->pdu_size = 0;
154 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
155 
156 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
157 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
158 
159 	nds->outer_queue_tailp = &nds->outer_queue_head;
160 	return (0);
161 }
162 
163 /*
164  * nds_destruct
165  *
166  * Destroy a stream. This is an external interface to provide access to
167  * the stream's destruct operation.
168  */
169 void
170 nds_destruct(ndr_stream_t *nds)
171 {
172 	if ((nds == NULL) || (nds->ndo == NULL))
173 		return;
174 
175 	NDS_DESTRUCT(nds);
176 }
177 
178 /*
179  * Print NDR stream state.
180  */
181 void
182 nds_show_state(ndr_stream_t *nds)
183 {
184 	if (nds == NULL) {
185 		ndo_printf(NULL, NULL, "nds: <null");
186 		return;
187 	}
188 
189 	ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d",
190 	    nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
191 	    nds->pdu_scan_offset);
192 }
193 
194 /*
195  * ndo_malloc
196  *
197  * Allocate memory from the stream heap.
198  */
199 /*ARGSUSED*/
200 static char *
201 ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
202 {
203 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
204 }
205 
206 /*
207  * ndo_free
208  *
209  * Always succeeds: cannot free individual stream allocations.
210  */
211 /*ARGSUSED*/
212 static int
213 ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
214 {
215 	return (1);
216 }
217 
218 /*
219  * ndo_grow_pdu
220  *
221  * This is the only place that should change the size of the PDU. If the
222  * desired offset is beyond the current PDU size, we realloc the PDU
223  * buffer to accommodate the request. For efficiency, the PDU is always
224  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
225  * beyond NDR_PDU_MAX_SIZE are rejected.
226  *
227  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
228  */
229 static int
230 ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
231 {
232 	unsigned char *pdu_addr;
233 	unsigned pdu_max_size;
234 
235 	ndo_printf(nds, ref, "grow %d", want_end_offset);
236 
237 	pdu_max_size = nds->pdu_max_size;
238 
239 	if (want_end_offset > pdu_max_size) {
240 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
241 
242 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
243 			return (0);
244 
245 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
246 		if (pdu_addr == 0)
247 			return (0);
248 
249 		nds->pdu_max_size = pdu_max_size;
250 		nds->pdu_base_addr = pdu_addr;
251 		nds->pdu_base_offset = (unsigned long)pdu_addr;
252 	}
253 
254 	nds->pdu_size = want_end_offset;
255 	return (1);
256 }
257 
258 static int
259 ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
260     unsigned long n_bytes, ndr_ref_t *ref)
261 {
262 	unsigned char *data;
263 
264 	data = (unsigned char *)nds->pdu_base_offset;
265 	data += pdu_offset;
266 
267 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
268 
269 	bzero(data, n_bytes);
270 	return (1);
271 }
272 
273 /*
274  * ndo_get_pdu
275  *
276  * The swap flag is 1 if NDR knows that the byte-order in the PDU
277  * is different from the local system.
278  *
279  * Returns 1 on success or 0 to indicate failure.
280  */
281 static int
282 ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
283     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
284 {
285 	unsigned char *data;
286 	char hexbuf[NDOBUFSZ];
287 
288 	data = (unsigned char *)nds->pdu_base_offset;
289 	data += pdu_offset;
290 
291 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
292 
293 	ndo_printf(nds, ref, "get %d@%-3d = %s",
294 	    n_bytes, pdu_offset, hexbuf);
295 
296 	if (!swap_bytes)
297 		bcopy(data, buf, n_bytes);
298 	else
299 		nds_bswap(data, (unsigned char *)buf, n_bytes);
300 
301 	return (1);
302 }
303 
304 /*
305  * ndo_put_pdu
306  *
307  * This is a receiver makes right protocol. So we do not need
308  * to be concerned about the byte-order of an outgoing PDU.
309  */
310 /*ARGSUSED*/
311 static int
312 ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
313     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
314 {
315 	unsigned char *data;
316 	char hexbuf[NDOBUFSZ];
317 
318 	data = (unsigned char *)nds->pdu_base_offset;
319 	data += pdu_offset;
320 
321 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
322 
323 	ndo_printf(nds, ref, "put %d@%-3d = %s",
324 	    n_bytes, pdu_offset, hexbuf);
325 
326 	bcopy(buf, data, n_bytes);
327 	return (1);
328 }
329 
330 static void
331 ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
332 {
333 	ndo_printf(nds, ref, what);
334 }
335 
336 static void
337 ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
338 {
339 	unsigned char *data;
340 	char hexbuf[NDOBUFSZ];
341 
342 	if (nds->pdu_base_addr != NULL) {
343 		data = (unsigned char *)nds->pdu_base_offset;
344 		if (ref)
345 			data += ref->pdu_offset;
346 		else
347 			data += nds->pdu_scan_offset;
348 
349 		ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
350 	} else {
351 		bzero(hexbuf, NDOBUFSZ);
352 	}
353 
354 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
355 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
356 	    nds->pdu_size, nds->pdu_max_size);
357 	ndo_printf(nds, ref, "      %s", hexbuf);
358 }
359 
360 /*
361  * ndo_reset
362  *
363  * Reset a stream: zap the outer_queue. We don't need to tamper
364  * with the stream heap: it's handled externally to the stream.
365  */
366 static int
367 ndo_reset(ndr_stream_t *nds)
368 {
369 	ndo_printf(nds, 0, "reset");
370 
371 	nds->pdu_size = 0;
372 	nds->pdu_scan_offset = 0;
373 	nds->outer_queue_head = 0;
374 	nds->outer_current = 0;
375 	nds->outer_queue_tailp = &nds->outer_queue_head;
376 
377 	return (1);
378 }
379 
380 /*
381  * ndo_destruct
382  *
383  * Destruct a stream: zap the outer_queue.
384  * Note: heap management (creation/destruction) is external to the stream.
385  */
386 static void
387 ndo_destruct(ndr_stream_t *nds)
388 {
389 
390 	ndo_printf(nds, 0, "destruct");
391 
392 	if (nds == NULL)
393 		return;
394 
395 	if (nds->pdu_base_addr != NULL) {
396 		free(nds->pdu_base_addr);
397 		nds->pdu_base_addr = NULL;
398 		nds->pdu_base_offset = 0;
399 	}
400 
401 	nds->outer_queue_head = 0;
402 	nds->outer_current = 0;
403 	nds->outer_queue_tailp = &nds->outer_queue_head;
404 }
405 
406 /*
407  * Printf style formatting for NDR operations.
408  */
409 void
410 ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
411 {
412 	va_list ap;
413 	char buf[NDOBUFSZ];
414 
415 	va_start(ap, fmt);
416 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
417 	va_end(ap);
418 
419 	if (nds)
420 		ndo_fmt(nds, ref, buf);
421 	else
422 		ndo_trace(buf);
423 }
424 
425 /*
426  * Main output formatter for NDR operations.
427  *
428  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
429  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
430  *
431  *	U       Marshalling flag (M=marshal, U=unmarshal)
432  *	I       Direction flag (I=in, O=out)
433  *	...     Field name
434  *	get     PDU operation (get or put)
435  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
436  *	{05}    Value
437  */
438 void
439 ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
440 {
441 	ndr_ref_t	*p;
442 	int		indent;
443 	char		ref_name[NDOBUFSZ];
444 	char		buf[NDOBUFSZ];
445 	int		m_op_c = '?', dir_c = '?';
446 
447 	switch (nds->m_op) {
448 	case 0:				m_op_c = '-';	break;
449 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
450 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
451 	default:			m_op_c = '?';	break;
452 	}
453 
454 	switch (nds->dir) {
455 	case 0:				dir_c = '-';	break;
456 	case NDR_DIR_IN:		dir_c = 'I';	break;
457 	case NDR_DIR_OUT:		dir_c = 'O';	break;
458 	default:			dir_c = '?';	break;
459 	}
460 
461 	for (indent = 0, p = ref; p; p = p->enclosing)
462 		indent++;
463 
464 	if (ref && ref->name) {
465 		if (*ref->name == '[' && ref->enclosing) {
466 			indent--;
467 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
468 			    ref->enclosing->name, ref->name);
469 		} else {
470 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
471 		}
472 	} else {
473 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
474 	}
475 
476 	(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s  %s",
477 	    m_op_c, dir_c, indent,
478 	    "....+....+....+....+....+....",
479 	    20 - indent, ref_name, note);
480 
481 	ndo_trace(buf);
482 }
483 
484 /*ARGSUSED*/
485 void
486 ndo_trace(const char *s)
487 {
488 	/*
489 	 * Temporary fbt for dtrace until user space sdt enabled.
490 	 */
491 }
492 
493 /*
494  * Format data as hex bytes (limit is 10 bytes):
495  *
496  *	1188689424 {10 f6 d9 46}
497  *
498  * If the input data is greater than 10 bytes, an ellipsis will
499  * be inserted before the closing brace.
500  */
501 static void
502 ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
503 {
504 	char *p = buf;
505 	int interp = 1;
506 	uint32_t c;
507 	int n;
508 	int i;
509 
510 	n = (size > 10) ? 10 : size;
511 	if (n > len-1)
512 		n = len-1;
513 
514 	switch (size) {
515 	case 1:
516 		c = *(uint8_t *)data;
517 		break;
518 	case 2:
519 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
520 			c = *(uint16_t *)data;
521 		else
522 			c = (data[0] << 8) | data[1];
523 		break;
524 	case 4:
525 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
526 			c = *(uint32_t *)data;
527 		} else {
528 			c = (data[0] << 24) | (data[1] << 16)
529 			    | (data[2] << 8) | data[3];
530 		}
531 		break;
532 	default:
533 		c = 0;
534 		interp = 0;
535 		break;
536 	}
537 
538 	if (interp)
539 		p += sprintf(p, "%4u {", c);
540 	else
541 		p += sprintf(p, " {");
542 
543 	p += sprintf(p, "%02x", data[0]);
544 	for (i = 1; i < n; i++)
545 		p += sprintf(p, " %02x", data[i]);
546 	if (size > 10)
547 		p += sprintf(p, " ...}");
548 	else
549 		p += sprintf(p, "}");
550 
551 	/*
552 	 * Show c if it's a printable character or wide-char.
553 	 */
554 	if (size < 4 && isprint((uint8_t)c))
555 		(void) sprintf(p, " %c", (uint8_t)c);
556 }
557