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 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _SMBSRV_NDR_H 27 #define _SMBSRV_NDR_H 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Network Data Representation (NDR) is a compatible subset of DCE RPC 33 * and MSRPC NDR. NDR is used to move parameters consisting of 34 * complicated trees of data constructs between an RPC client and server. 35 * 36 * CAE Specification (1997) 37 * DCE 1.1: Remote Procedure Call 38 * Document Number: C706 39 * The Open Group 40 * ogspecs@opengroup.org 41 */ 42 43 #ifndef _KERNEL 44 #include <syslog.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #endif 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 /* 54 * Normal sequence: 55 * - Application calls client-side stub w/ TOP-MOST arg structure 56 * - client stub performs NDR_M_OP_MARSHALL+NDR_DIR_IN 57 * - PDU conveyed (request, aka call, aka query) 58 * - server stub performs NDR_M_OP_UNMARSHALL+NDR_DIR_IN 59 * - server function called w/ TOP-MOST arg structure 60 * - server function returns w/ TOP-MOST arg structure modified 61 * - server stub performs NDR_M_OP_MARSHALL+NDR_DIR_OUT 62 * - PDU conveyed (reply, aka result, aka response) 63 * - client stub performs NDR_M_OP_UNMARSHALL+NDR_DIR_OUT 64 * - return to Application w/ TOP-MOST arg structure modified 65 * 66 * An interface is a sequence of top-most constructs. Each top-most 67 * construct corresponds to one parameter, either argument or return 68 * value. 69 * 70 * A top-most construct is a sequence of outer constructs. The first 71 * outer construct is the referent of the argument, and the subsequent 72 * outer constructs are descendents referenced by pointers from prior 73 * constructs. 74 * 75 * An outer construct is a sequence of variable-sized info, fixed-sized 76 * data, and variable-sized data. 77 */ 78 79 /* 80 * Terminology 81 * 82 * The ALL UPPER CASE terms recur in the DCE/RPC documentation. 83 * The mixed-case names have been introduced as a reading aid. 84 * 85 * Size The size of an array in elements. Think of this 86 * as the amount to malloc(). 87 * 88 * Length The number of elements of an array which are significant 89 * Think of this as the amount to bcopy(). 90 * 91 * Known Size/length is known at build time. 92 * 93 * Determined Size/length is determined at run time. 94 * 95 * FIXED The Size and Length are Known. 96 * Think of this as a string constant or a DOS 8.3 file name. 97 * char array[] = "A Constant Size/Length"; 98 * 99 * CONFORMANT The Size is Determined. Length is the same as Size. 100 * Think of this as strdup(). 101 * char *array = strdup("Something"); 102 * 103 * VARYING The Size is Known. The Length is determined. 104 * Think of this as a strcpy() of a variable length string 105 * into a fixed length buffer: 106 * char array[100]; 107 * strcpy(array, "very short string"); 108 * 109 * VARYING/CONFORMANT 110 * The Size is Determined. The Length is separately Determined. 111 * Think of this like: 112 * char *array = malloc(size); 113 * strcpy(array, "short string"); 114 * 115 * STRING Strings can be CONFORMANT, VARYING, or CONFORMANT/VARYING. 116 * A string is fundamentally an array with the last 117 * significant element some sort of NULL. 118 */ 119 120 #define NDR_F_NONE 0x0000 /* no flags */ 121 #define NDR_F_PARAMS_MASK 0x00FF 122 #define NDR_F_SIZE_IS 0x0001 /* [size_is(X)] required/given */ 123 #define NDR_F_LENGTH_IS 0x0002 /* not implemented */ 124 #define NDR_F_SWITCH_IS 0x0004 /* [switch_is(X)] req./given */ 125 #define NDR_F_IS_STRING 0x0008 /* [string] req./given */ 126 #define NDR_F_IS_POINTER 0x0010 /* TYPE * ... req./given */ 127 #define NDR_F_IS_REFERENCE 0x0020 /* TYPE & ... req./given */ 128 #define NDR_F_DIMENSION_IS 0x0040 /* TYPE [N] req./given */ 129 130 #define NDR_F_WHENCE_MASK 0x00F0 131 #define NDR_F_BACKPTR 0x0010 /* ref cause by pointer */ 132 #define NDR_F_OUTER 0x0020 /* ref caused by outer */ 133 #define NDR_F_TOPMOST 0x0040 /* ref caused by topmost */ 134 135 #define NDR_F_TYPEOP_MASK 0x0F00 136 #define NDR_F_ARRAY 0x0100 /* type is array of somethings */ 137 #define NDR_F_POINTER 0x0200 /* type is pointer to something(s) */ 138 #define NDR_F_STRING 0x0300 /* type is string of somethings */ 139 #define NDR_F_UNION 0x0400 /* type is a union */ 140 #define NDR_F_STRUCT 0x0500 /* type is a structure */ 141 #define NDR_F_OPERATION 0x0600 /* type is a structure, special */ 142 #define NDR_F_INTERFACE 0x0700 /* type is a union, special */ 143 #define NDR_F_CONFORMANT 0x1000 /* struct conforming (var-size tail) */ 144 #define NDR_F_VARYING 0x2000 /* not implemented */ 145 146 struct mlrpc_heap; 147 struct mlndr_stream; 148 struct ndr_reference; 149 struct ndr_typeinfo; 150 151 struct ndr_typeinfo { 152 unsigned char version; /* sanity check */ 153 unsigned char alignment; /* mask */ 154 unsigned short type_flags; /* NDR_F_... */ 155 int (*ndr_func)(struct ndr_reference *encl_ref); 156 unsigned short pdu_size_fixed_part; 157 unsigned short pdu_size_variable_part; 158 unsigned short c_size_fixed_part; 159 unsigned short c_size_variable_part; 160 }; 161 162 struct ndr_reference { 163 struct ndr_reference *next; /* queue list (outer only) */ 164 struct ndr_reference *enclosing; /* e.g. struct for this memb */ 165 struct mlndr_stream *stream; /* root of NDR */ 166 struct ndr_typeinfo *ti; /* type of data referenced */ 167 char *name; /* name of this member */ 168 unsigned long pdu_offset; /* referent in stub data */ 169 char *datum; /* referent in local memory */ 170 char **backptr; /* referer to set */ 171 unsigned short outer_flags; /* XXX_is() from top level */ 172 unsigned short inner_flags; /* XXX_is() in encapsulated */ 173 unsigned short type_flags; /* "requires" */ 174 unsigned short packed_alignment; 175 unsigned long size_is; /* conforming constructs */ 176 unsigned long strlen_is; /* strings */ 177 unsigned long switch_is; /* union arg selector */ 178 unsigned long dimension_is; /* fixed-len array size */ 179 unsigned long pdu_end_offset; /* offset for limit of PDU */ 180 }; 181 182 /* 183 * For all operations, the mlndr_stream, which is the root of NDR processing, 184 * is the primary object. When available, the appropriate ndr_reference 185 * is passed, NULL otherwise. Functions that return 'int' should return 186 * TRUE (!0) or FALSE (0). When functions return FALSE, including 187 * mlndo_malloc() returning NULL, they should set the stream->error to an 188 * appropriate indicator of what went wrong. 189 * 190 * Functions mlndo_get_pdu(), mlndo_put_pdu(), and mlndo_pad_pdu() must 191 * never grow the PDU data. A request for out-of-bounds data is an error. 192 * The swap_bytes flag is 1 if NDR knows that the byte-order in the PDU 193 * is different from the local system. mlndo_pad_pdu() advised that the 194 * affected bytes should be zero filled. 195 */ 196 struct mlndr_stream_ops { 197 char *(*mlndo_malloc)(struct mlndr_stream *, unsigned, 198 struct ndr_reference *); 199 200 int (*mlndo_free)(struct mlndr_stream *, char *, 201 struct ndr_reference *); 202 203 int (*mlndo_grow_pdu)(struct mlndr_stream *, unsigned long, 204 struct ndr_reference *); 205 206 int (*mlndo_pad_pdu)(struct mlndr_stream *, unsigned long, 207 unsigned long, struct ndr_reference *); 208 209 int (*mlndo_get_pdu)(struct mlndr_stream *, unsigned long, 210 unsigned long, char *, int, struct ndr_reference *); 211 212 int (*mlndo_put_pdu)(struct mlndr_stream *, unsigned long, 213 unsigned long, char *, int, struct ndr_reference *); 214 215 void (*mlndo_tattle)(struct mlndr_stream *, char *, 216 struct ndr_reference *); 217 218 void (*mlndo_tattle_error)(struct mlndr_stream *, 219 struct ndr_reference *); 220 221 int (*mlndo_reset)(struct mlndr_stream *); 222 void (*mlndo_destruct)(struct mlndr_stream *); 223 }; 224 225 #define MLNDS_MALLOC(MLNDS, LEN, REF) \ 226 (*(MLNDS)->mlndo->mlndo_malloc)(MLNDS, LEN, REF) 227 228 #define MLNDS_GROW_PDU(MLNDS, WANT_END_OFF, REF) \ 229 (*(MLNDS)->mlndo->mlndo_grow_pdu)(MLNDS, WANT_END_OFF, REF) 230 #define MLNDS_PAD_PDU(MLNDS, PDU_OFFSET, N_BYTES, REF) \ 231 (*(MLNDS)->mlndo->mlndo_pad_pdu)(MLNDS, PDU_OFFSET, N_BYTES, REF) 232 #define MLNDS_GET_PDU(MLNDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF) \ 233 (*(MLNDS)->mlndo->mlndo_get_pdu)(MLNDS, PDU_OFFSET, N_BYTES, BUF, \ 234 SWAP, REF) 235 #define MLNDS_PUT_PDU(MLNDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF) \ 236 (*(MLNDS)->mlndo->mlndo_put_pdu)(MLNDS, PDU_OFFSET, N_BYTES, BUF, \ 237 SWAP, REF) 238 239 #define MLNDS_TATTLE(MLNDS, WHAT, REF) \ 240 (*(MLNDS)->mlndo->mlndo_tattle)(MLNDS, WHAT, REF) 241 #define MLNDS_TATTLE_ERROR(MLNDS, WHAT, REF) \ 242 (*(MLNDS)->mlndo->mlndo_tattle_error)(MLNDS, REF) 243 #define MLNDS_RESET(MLNDS) \ 244 (*(MLNDS)->mlndo->mlndo_reset)(MLNDS) 245 #define MLNDS_DESTRUCT(MLNDS) \ 246 (*(MLNDS)->mlndo->mlndo_destruct)(MLNDS) 247 248 struct mlndr_stream { 249 unsigned long pdu_size; 250 unsigned long pdu_size_with_rpc_hdrs; 251 unsigned long pdu_max_size; 252 unsigned long pdu_base_offset; 253 unsigned long pdu_scan_offset; 254 unsigned char *pdu_base_addr; 255 unsigned char *pdu_base_addr_with_rpc_hdrs; 256 257 struct mlndr_stream_ops *mlndo; 258 259 unsigned char m_op; 260 unsigned char dir; 261 unsigned char swap; /* native/net endian swap */ 262 short error; 263 short error_ref; 264 265 struct ndr_reference *outer_queue_head; 266 struct ndr_reference **outer_queue_tailp; 267 struct ndr_reference *outer_current; 268 struct mlrpc_heap *heap; 269 }; 270 271 272 #define NDR_M_OP_NONE 0x00 273 #define NDR_M_OP_MARSHALL 0x01 /* data moving from datum to PDU */ 274 #define NDR_M_OP_UNMARSHALL 0x02 /* data moving from PDU to datum */ 275 276 #define NDR_DIR_NONE 0x00 277 #define NDR_DIR_IN 0x10 /* data moving from caller to callee */ 278 #define NDR_DIR_OUT 0x20 /* data moving from callee to caller */ 279 280 #define NDR_MODE_CALL_SEND (NDR_M_OP_MARSHALL + NDR_DIR_IN) 281 #define NDR_MODE_CALL_RECV (NDR_M_OP_UNMARSHALL + NDR_DIR_IN) 282 #define NDR_MODE_RETURN_SEND (NDR_M_OP_MARSHALL + NDR_DIR_OUT) 283 #define NDR_MODE_RETURN_RECV (NDR_M_OP_UNMARSHALL + NDR_DIR_OUT) 284 285 #define NDR_MODE_TO_M_OP(MODE) ((MODE)&0x0F) 286 #define NDR_MODE_TO_DIR(MODE) ((MODE)&0xF0) 287 #define NDR_M_OP_AND_DIR_TO_MODE(M_OP, DIR) ((M_OP)|(DIR)) 288 289 #define NDR_MODE_MATCH(MLNDS, MODE) \ 290 (NDR_M_OP_AND_DIR_TO_MODE((MLNDS)->m_op, (MLNDS)->dir) == (MODE)) 291 292 293 #define NDR_ERR_MALLOC_FAILED -1 294 #define NDR_ERR_M_OP_INVALID -2 295 #define NDR_ERR_UNDERFLOW -3 296 #define NDR_ERR_GROW_FAILED -4 /* overflow */ 297 #define NDR_ERR_PAD_FAILED -5 /* couldn't possibly happen */ 298 #define NDR_ERR_OUTER_HEADER_BAD -6 299 #define NDR_ERR_SWITCH_VALUE_ILLEGAL -7 300 #define NDR_ERR_SWITCH_VALUE_INVALID -8 301 #define NDR_ERR_SWITCH_VALUE_MISSING -9 302 #define NDR_ERR_SIZE_IS_MISMATCH_PDU -10 303 #define NDR_ERR_SIZE_IS_MISMATCH_AFTER -11 304 #define NDR_ERR_SIZE_IS_UNEXPECTED -12 305 #define NDR_ERR_SIZE_IS_DUPLICATED -13 306 #define NDR_ERR_OUTER_PARAMS_MISMATCH -14 307 #define NDR_ERR_ARRAY_VARLEN_ILLEGAL -15 308 #define NDR_ERR_ARRAY_UNION_ILLEGAL -16 309 #define NDR_ERR_OUTER_PARAMS_BAD -17 310 #define NDR_ERR_OUTER_UNION_ILLEGAL -18 311 #define NDR_ERR_TOPMOST_UNION_ILLEGAL -19 312 #define NDR_ERR_TOPMOST_VARLEN_ILLEGAL -20 313 #define NDR_ERR_INNER_PARAMS_BAD -21 314 #define NDR_ERR_UNIMPLEMENTED -22 315 #define NDR_ERR_NOT_AN_INTERFACE -23 316 #define NDR_ERR_STRLEN -24 317 #define NDR_ERR_STRING_SIZING -25 318 #define NDR_ERR_BOUNDS_CHECK -26 319 320 #define NDR_SET_ERROR(REF, ERROR) \ 321 ((REF)->stream->error = (ERROR), \ 322 (REF)->stream->error_ref = __LINE__, \ 323 MLNDS_TATTLE_ERROR((REF)->stream, 0, REF)) 324 325 #define NDR_TATTLE(REF, WHAT) \ 326 (*(REF)->stream->mlndo->mlndo_tattle)((REF)->stream, WHAT, REF) 327 328 #define MEMBER_STR(MEMBER) #MEMBER 329 330 #define NDR_DIR_IS_IN (encl_ref->stream->dir == NDR_DIR_IN) 331 #define NDR_DIR_IS_OUT (encl_ref->stream->dir == NDR_DIR_OUT) 332 333 #define NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 334 ARGFLAGS, ARGMEM, ARGVAL) { \ 335 myref.pdu_offset = encl_ref->pdu_offset + (OFFSET); \ 336 myref.name = MEMBER_STR(MEMBER); \ 337 myref.datum = (char *)&val->MEMBER; \ 338 myref.inner_flags = ARGFLAGS; \ 339 myref.ti = &ndt_##TYPE; \ 340 myref.ARGMEM = ARGVAL; \ 341 if (!mlndr_inner(&myref)) \ 342 return (0); \ 343 } 344 345 #define NDR_MEMBER(TYPE, MEMBER, OFFSET) \ 346 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 347 NDR_F_NONE, size_is, 0) 348 349 #define NDR_MEMBER_ARR_WITH_SIZE_IS(TYPE, MEMBER, OFFSET, SIZE_IS) \ 350 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 351 NDR_F_SIZE_IS, size_is, SIZE_IS) 352 353 #define NDR_MEMBER_ARR_WITH_DIMENSION(TYPE, MEMBER, OFFSET, SIZE_IS) \ 354 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 355 NDR_F_DIMENSION_IS, dimension_is, SIZE_IS) 356 357 #define NDR_MEMBER_PTR_WITH_SIZE_IS(TYPE, MEMBER, OFFSET, SIZE_IS) \ 358 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 359 NDR_F_SIZE_IS+NDR_F_IS_POINTER, size_is, SIZE_IS) 360 361 #define NDR_MEMBER_PTR(TYPE, MEMBER, OFFSET) \ 362 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 363 NDR_F_IS_POINTER, size_is, 0) 364 365 #define NDR_MEMBER_WITH_SWITCH_IS(TYPE, MEMBER, OFFSET, SWITCH_IS) \ 366 NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \ 367 NDR_F_SWITCH_IS, switch_is, SWITCH_IS) 368 369 370 #define NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 371 ARGFLAGS, ARGMEM, ARGVAL) { \ 372 myref.pdu_offset = -1; \ 373 myref.name = MEMBER_STR(MEMBER); \ 374 myref.datum = (char *)&val->MEMBER; \ 375 myref.inner_flags = ARGFLAGS; \ 376 myref.ti = &ndt_##TYPE; \ 377 myref.ARGMEM = ARGVAL; \ 378 if (!mlndr_topmost(&myref)) \ 379 return (0); \ 380 } 381 382 #define NDR_TOPMOST_MEMBER(TYPE, MEMBER) \ 383 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 384 NDR_F_NONE, size_is, 0) 385 386 #define NDR_TOPMOST_MEMBER_ARR_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \ 387 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 388 NDR_F_SIZE_IS, size_is, SIZE_IS) 389 390 #define NDR_TOPMOST_MEMBER_ARR_WITH_DIMENSION(TYPE, MEMBER, SIZE_IS) \ 391 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 392 NDR_F_DIMENSION_IS, dimension_is, SIZE_IS) 393 394 #define NDR_TOPMOST_MEMBER_PTR_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \ 395 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 396 NDR_F_SIZE_IS+NDR_F_IS_POINTER, size_is, SIZE_IS) 397 398 #define NDR_TOPMOST_MEMBER_PTR(TYPE, MEMBER) \ 399 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 400 NDR_F_IS_POINTER, size_is, 0) 401 402 #define NDR_TOPMOST_MEMBER_REF(TYPE, MEMBER) \ 403 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 404 NDR_F_IS_REFERENCE, size_is, 0) 405 406 #define NDR_TOPMOST_MEMBER_REF_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \ 407 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 408 NDR_F_SIZE_IS+NDR_F_IS_REFERENCE, size_is, SIZE_IS) 409 410 #define NDR_TOPMOST_MEMBER_WITH_SWITCH_IS(TYPE, MEMBER, SWITCH_IS) \ 411 NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \ 412 NDR_F_SWITCH_IS, switch_is, SWITCH_IS) 413 414 /* this is assuming offset+0 */ 415 #define NDR_PARAMS_MEMBER_WITH_ARG(TYPE, MEMBER, ARGFLAGS, \ 416 ARGMEM, ARGVAL) { \ 417 myref.pdu_offset = encl_ref->pdu_offset; \ 418 myref.name = MEMBER_STR(MEMBER); \ 419 myref.datum = (char *)&val->MEMBER; \ 420 myref.inner_flags = ARGFLAGS; \ 421 myref.ti = &ndt_##TYPE; \ 422 myref.ARGMEM = ARGVAL; \ 423 if (!mlndr_params(&myref)) \ 424 return (0); \ 425 } 426 427 #define NDR_PARAMS_MEMBER(TYPE, MEMBER) \ 428 NDR_PARAMS_MEMBER_WITH_ARG(TYPE, MEMBER, \ 429 NDR_F_NONE, size_is, 0) 430 431 #define NDR_STRING_DIM 1 432 #define NDR_ANYSIZE_DIM 1 433 434 int mlndo_process(struct mlndr_stream *, struct ndr_typeinfo *, char *); 435 int mlndo_operation(struct mlndr_stream *, struct ndr_typeinfo *, 436 int opnum, char *); 437 void mlndo_printf(struct mlndr_stream *, struct ndr_reference *, 438 const char *, ...); 439 void mlndo_trace(const char *); 440 void mlndo_fmt(struct mlndr_stream *, struct ndr_reference *, char *); 441 442 int mlndr_params(struct ndr_reference *); 443 int mlndr_topmost(struct ndr_reference *); 444 int mlndr_run_outer_queue(struct mlndr_stream *); 445 int mlndr_outer(struct ndr_reference *); 446 int mlndr_outer_fixed(struct ndr_reference *); 447 int mlndr_outer_fixed_array(struct ndr_reference *); 448 int mlndr_outer_conformant_array(struct ndr_reference *); 449 int mlndr_outer_conformant_construct(struct ndr_reference *); 450 int mlndr_size_is(struct ndr_reference *); 451 int mlndr_outer_string(struct ndr_reference *); 452 int mlndr_outer_peek_sizing(struct ndr_reference *, unsigned, 453 unsigned long *); 454 int mlndr_outer_poke_sizing(struct ndr_reference *, unsigned, 455 unsigned long *); 456 int mlndr_outer_align(struct ndr_reference *); 457 int mlndr_outer_grow(struct ndr_reference *, unsigned); 458 int mlndr_inner(struct ndr_reference *); 459 int mlndr_inner_pointer(struct ndr_reference *); 460 int mlndr_inner_reference(struct ndr_reference *); 461 int mlndr_inner_array(struct ndr_reference *); 462 void mlnds_bswap(void *src, void *dst, size_t len); 463 464 #ifdef __cplusplus 465 } 466 #endif 467 468 #endif /* _SMBSRV_NDR_H */ 469