1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * XDR standard data types and function declarations 4 * 5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de> 6 * 7 * Based on: 8 * RFC 4506 "XDR: External Data Representation Standard", May 2006 9 */ 10 11 #ifndef _SUNRPC_XDR_H_ 12 #define _SUNRPC_XDR_H_ 13 14 #include <linux/uio.h> 15 #include <asm/byteorder.h> 16 #include <linux/unaligned.h> 17 #include <linux/scatterlist.h> 18 19 struct bio_vec; 20 struct rpc_rqst; 21 22 /* 23 * Size of an XDR encoding unit in bytes, i.e. 32 bits, 24 * as defined in Section 3 of RFC 4506. All encoded 25 * XDR data items are aligned on a boundary of 32 bits. 26 */ 27 #define XDR_UNIT sizeof(__be32) 28 29 /* 30 * Buffer adjustment 31 */ 32 #define XDR_QUADLEN(l) (((l) + 3) >> 2) 33 34 /* 35 * Generic opaque `network object.' 36 */ 37 #define XDR_MAX_NETOBJ 1024 38 struct xdr_netobj { 39 unsigned int len; 40 u8 * data; 41 }; 42 43 /* 44 * Basic structure for transmission/reception of a client XDR message. 45 * Features a header (for a linear buffer containing RPC headers 46 * and the data payload for short messages), and then an array of 47 * pages. 48 * The tail iovec allows you to append data after the page array. Its 49 * main interest is for appending padding to the pages in order to 50 * satisfy the int_32-alignment requirements in RFC1832. 51 * 52 * For the future, we might want to string several of these together 53 * in a list if anybody wants to make use of NFSv4 COMPOUND 54 * operations and/or has a need for scatter/gather involving pages. 55 */ 56 struct xdr_buf { 57 struct kvec head[1], /* RPC header + non-page data */ 58 tail[1]; /* Appended after page data */ 59 60 struct bio_vec *bvec; 61 struct page ** pages; /* Array of pages */ 62 unsigned int page_base, /* Start of page data */ 63 page_len, /* Length of page data */ 64 flags; /* Flags for data disposition */ 65 #define XDRBUF_READ 0x01 /* target of file read */ 66 #define XDRBUF_WRITE 0x02 /* source of file write */ 67 #define XDRBUF_SPARSE_PAGES 0x04 /* Page array is sparse */ 68 69 unsigned int buflen, /* Total length of storage buffer */ 70 len; /* Length of XDR encoded message */ 71 }; 72 73 static inline void 74 xdr_buf_init(struct xdr_buf *buf, void *start, size_t len) 75 { 76 buf->head[0].iov_base = start; 77 buf->head[0].iov_len = len; 78 buf->tail[0].iov_len = 0; 79 buf->pages = NULL; 80 buf->page_len = 0; 81 buf->flags = 0; 82 buf->len = 0; 83 buf->buflen = len; 84 } 85 86 /* 87 * pre-xdr'ed macros. 88 */ 89 90 #define xdr_zero cpu_to_be32(0) 91 #define xdr_one cpu_to_be32(1) 92 #define xdr_two cpu_to_be32(2) 93 94 #define rpc_auth_null cpu_to_be32(RPC_AUTH_NULL) 95 #define rpc_auth_unix cpu_to_be32(RPC_AUTH_UNIX) 96 #define rpc_auth_short cpu_to_be32(RPC_AUTH_SHORT) 97 #define rpc_auth_gss cpu_to_be32(RPC_AUTH_GSS) 98 #define rpc_auth_tls cpu_to_be32(RPC_AUTH_TLS) 99 100 #define rpc_call cpu_to_be32(RPC_CALL) 101 #define rpc_reply cpu_to_be32(RPC_REPLY) 102 103 #define rpc_msg_accepted cpu_to_be32(RPC_MSG_ACCEPTED) 104 105 #define rpc_success cpu_to_be32(RPC_SUCCESS) 106 #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL) 107 #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH) 108 #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL) 109 #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS) 110 #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR) 111 #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY) 112 113 #define rpc_mismatch cpu_to_be32(RPC_MISMATCH) 114 #define rpc_auth_error cpu_to_be32(RPC_AUTH_ERROR) 115 116 #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK) 117 #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED) 118 #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED) 119 #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF) 120 #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF) 121 #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK) 122 #define rpc_autherr_invalidresp cpu_to_be32(RPC_AUTH_INVALIDRESP) 123 #define rpc_autherr_failed cpu_to_be32(RPC_AUTH_FAILED) 124 #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM) 125 #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM) 126 127 /* 128 * Miscellaneous XDR helper functions 129 */ 130 __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len); 131 __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len); 132 __be32 *xdr_encode_string(__be32 *p, const char *s); 133 __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *); 134 135 void xdr_inline_pages(struct xdr_buf *, unsigned int, 136 struct page **, unsigned int, unsigned int); 137 void xdr_terminate_string(const struct xdr_buf *, const u32); 138 size_t xdr_buf_pagecount(const struct xdr_buf *buf); 139 int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp); 140 void xdr_free_bvec(struct xdr_buf *buf); 141 unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size, 142 const struct xdr_buf *xdr); 143 144 static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len) 145 { 146 return xdr_encode_opaque(p, s, len); 147 } 148 149 /* 150 * Decode 64bit quantities (NFSv3 support) 151 */ 152 static inline __be32 * 153 xdr_encode_hyper(__be32 *p, __u64 val) 154 { 155 put_unaligned_be64(val, p); 156 return p + 2; 157 } 158 159 static inline __be32 * 160 xdr_decode_hyper(__be32 *p, __u64 *valp) 161 { 162 *valp = get_unaligned_be64(p); 163 return p + 2; 164 } 165 166 static inline __be32 * 167 xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len) 168 { 169 memcpy(ptr, p, len); 170 return p + XDR_QUADLEN(len); 171 } 172 173 static inline void xdr_netobj_dup(struct xdr_netobj *dst, 174 struct xdr_netobj *src, gfp_t gfp_mask) 175 { 176 dst->data = kmemdup(src->data, src->len, gfp_mask); 177 dst->len = src->len; 178 } 179 180 /* 181 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR) 182 */ 183 static inline int 184 xdr_adjust_iovec(struct kvec *iov, __be32 *p) 185 { 186 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base); 187 } 188 189 /* 190 * XDR buffer helper functions 191 */ 192 extern void xdr_buf_from_iov(const struct kvec *, struct xdr_buf *); 193 extern int xdr_buf_subsegment(const struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int); 194 extern void xdr_buf_trim(struct xdr_buf *, unsigned int); 195 extern int read_bytes_from_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int); 196 extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int); 197 198 extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32); 199 extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *); 200 201 struct xdr_array2_desc; 202 typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem); 203 struct xdr_array2_desc { 204 unsigned int elem_size; 205 unsigned int array_len; 206 unsigned int array_maxlen; 207 xdr_xcode_elem_t xcode; 208 }; 209 210 extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base, 211 struct xdr_array2_desc *desc); 212 extern int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base, 213 struct xdr_array2_desc *desc); 214 extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase, 215 size_t len); 216 217 /* 218 * Provide some simple tools for XDR buffer overflow-checking etc. 219 */ 220 struct xdr_stream { 221 __be32 *p; /* start of available buffer */ 222 struct xdr_buf *buf; /* XDR buffer to read/write */ 223 224 __be32 *end; /* end of available buffer space */ 225 struct kvec *iov; /* pointer to the current kvec */ 226 struct kvec scratch; /* Scratch buffer */ 227 struct page **page_ptr; /* pointer to the current page */ 228 void *page_kaddr; /* kmapped address of the current page */ 229 unsigned int nwords; /* Remaining decode buffer length */ 230 231 struct rpc_rqst *rqst; /* For debugging */ 232 }; 233 234 /* 235 * These are the xdr_stream style generic XDR encode and decode functions. 236 */ 237 typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 238 const void *obj); 239 typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 240 void *obj); 241 242 extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, 243 __be32 *p, struct rpc_rqst *rqst); 244 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf); 245 extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes); 246 extern int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes); 247 extern void __xdr_commit_encode(struct xdr_stream *xdr); 248 extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len); 249 extern void xdr_truncate_decode(struct xdr_stream *xdr, size_t len); 250 extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen); 251 extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, 252 unsigned int base, unsigned int len); 253 extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr); 254 extern unsigned int xdr_page_pos(const struct xdr_stream *xdr); 255 extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, 256 __be32 *p, struct rpc_rqst *rqst); 257 extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf, 258 struct page **pages, unsigned int len); 259 extern void xdr_finish_decode(struct xdr_stream *xdr); 260 extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes); 261 extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len); 262 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len); 263 extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data); 264 extern void xdr_set_pagelen(struct xdr_stream *, unsigned int len); 265 extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf, 266 unsigned int len); 267 extern unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset, 268 unsigned int target, unsigned int length); 269 extern unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset, 270 unsigned int length); 271 272 /** 273 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data. 274 * @xdr: pointer to xdr_stream struct 275 * @buf: pointer to an empty buffer 276 * @buflen: size of 'buf' 277 * 278 * The scratch buffer is used when decoding from an array of pages. 279 * If an xdr_inline_decode() call spans across page boundaries, then 280 * we copy the data into the scratch buffer in order to allow linear 281 * access. 282 */ 283 static inline void 284 xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen) 285 { 286 xdr->scratch.iov_base = buf; 287 xdr->scratch.iov_len = buflen; 288 } 289 290 /** 291 * xdr_set_scratch_folio - Attach a scratch buffer for decoding data 292 * @xdr: pointer to xdr_stream struct 293 * @folio: an anonymous folio 294 * 295 * See xdr_set_scratch_buffer(). 296 */ 297 static inline void 298 xdr_set_scratch_folio(struct xdr_stream *xdr, struct folio *folio) 299 { 300 xdr_set_scratch_buffer(xdr, folio_address(folio), folio_size(folio)); 301 } 302 303 /** 304 * xdr_reset_scratch_buffer - Clear scratch buffer information 305 * @xdr: pointer to xdr_stream struct 306 * 307 * See xdr_set_scratch_buffer(). 308 */ 309 static inline void 310 xdr_reset_scratch_buffer(struct xdr_stream *xdr) 311 { 312 xdr_set_scratch_buffer(xdr, NULL, 0); 313 } 314 315 /** 316 * xdr_commit_encode - Ensure all data is written to xdr->buf 317 * @xdr: pointer to xdr_stream 318 * 319 * Handle encoding across page boundaries by giving the caller a 320 * temporary location to write to, then later copying the data into 321 * place. __xdr_commit_encode() does that copying. 322 */ 323 static inline void xdr_commit_encode(struct xdr_stream *xdr) 324 { 325 if (unlikely(xdr->scratch.iov_len)) 326 __xdr_commit_encode(xdr); 327 } 328 329 /** 330 * xdr_stream_remaining - Return the number of bytes remaining in the stream 331 * @xdr: pointer to struct xdr_stream 332 * 333 * Returns: 334 * Number of bytes remaining in @xdr before xdr->end 335 */ 336 static inline size_t 337 xdr_stream_remaining(const struct xdr_stream *xdr) 338 { 339 return xdr->nwords << 2; 340 } 341 342 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str, 343 size_t maxlen, gfp_t gfp_flags); 344 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor, 345 void **body, unsigned int *body_len); 346 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor, 347 void *body, unsigned int body_len); 348 349 /** 350 * xdr_align_size - Calculate padded size of an object 351 * @n: Size of an object being XDR encoded (in bytes) 352 * 353 * Returns: 354 * Size (in bytes) of the object including xdr padding 355 */ 356 static inline size_t 357 xdr_align_size(size_t n) 358 { 359 const size_t mask = XDR_UNIT - 1; 360 361 return (n + mask) & ~mask; 362 } 363 364 /** 365 * xdr_pad_size - Calculate size of an object's pad 366 * @n: Size of an object being XDR encoded (in bytes) 367 * 368 * This implementation avoids the need for conditional 369 * branches or modulo division. 370 * 371 * Returns: 372 * Size (in bytes) of the needed XDR pad 373 */ 374 static inline size_t xdr_pad_size(size_t n) 375 { 376 return xdr_align_size(n) - n; 377 } 378 379 /** 380 * xdr_stream_encode_item_present - Encode a "present" list item 381 * @xdr: pointer to xdr_stream 382 * 383 * Returns: 384 * On success, returns length in bytes of XDR buffer consumed 385 * %-EMSGSIZE on XDR buffer overflow 386 */ 387 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr) 388 { 389 const size_t len = XDR_UNIT; 390 __be32 *p = xdr_reserve_space(xdr, len); 391 392 if (unlikely(!p)) 393 return -EMSGSIZE; 394 *p = xdr_one; 395 return len; 396 } 397 398 /** 399 * xdr_stream_encode_item_absent - Encode a "not present" list item 400 * @xdr: pointer to xdr_stream 401 * 402 * Returns: 403 * On success, returns length in bytes of XDR buffer consumed 404 * %-EMSGSIZE on XDR buffer overflow 405 */ 406 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr) 407 { 408 const size_t len = XDR_UNIT; 409 __be32 *p = xdr_reserve_space(xdr, len); 410 411 if (unlikely(!p)) 412 return -EMSGSIZE; 413 *p = xdr_zero; 414 return len; 415 } 416 417 /** 418 * xdr_encode_bool - Encode a boolean item 419 * @p: address in a buffer into which to encode 420 * @n: boolean value to encode 421 * 422 * Returns: 423 * Address of item following the encoded boolean 424 */ 425 static inline __be32 *xdr_encode_bool(__be32 *p, u32 n) 426 { 427 *p++ = n ? xdr_one : xdr_zero; 428 return p; 429 } 430 431 /** 432 * xdr_stream_encode_bool - Encode a boolean item 433 * @xdr: pointer to xdr_stream 434 * @n: boolean value to encode 435 * 436 * Returns: 437 * On success, returns length in bytes of XDR buffer consumed 438 * %-EMSGSIZE on XDR buffer overflow 439 */ 440 static inline int xdr_stream_encode_bool(struct xdr_stream *xdr, __u32 n) 441 { 442 const size_t len = XDR_UNIT; 443 __be32 *p = xdr_reserve_space(xdr, len); 444 445 if (unlikely(!p)) 446 return -EMSGSIZE; 447 xdr_encode_bool(p, n); 448 return len; 449 } 450 451 /** 452 * xdr_stream_encode_u32 - Encode a 32-bit integer 453 * @xdr: pointer to xdr_stream 454 * @n: integer to encode 455 * 456 * Returns: 457 * On success, returns length in bytes of XDR buffer consumed 458 * %-EMSGSIZE on XDR buffer overflow 459 */ 460 static inline ssize_t 461 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n) 462 { 463 const size_t len = sizeof(n); 464 __be32 *p = xdr_reserve_space(xdr, len); 465 466 if (unlikely(!p)) 467 return -EMSGSIZE; 468 *p = cpu_to_be32(n); 469 return len; 470 } 471 472 /** 473 * xdr_stream_encode_be32 - Encode a big-endian 32-bit integer 474 * @xdr: pointer to xdr_stream 475 * @n: integer to encode 476 * 477 * Returns: 478 * On success, returns length in bytes of XDR buffer consumed 479 * %-EMSGSIZE on XDR buffer overflow 480 */ 481 static inline ssize_t 482 xdr_stream_encode_be32(struct xdr_stream *xdr, __be32 n) 483 { 484 const size_t len = sizeof(n); 485 __be32 *p = xdr_reserve_space(xdr, len); 486 487 if (unlikely(!p)) 488 return -EMSGSIZE; 489 *p = n; 490 return len; 491 } 492 493 /** 494 * xdr_stream_encode_u64 - Encode a 64-bit integer 495 * @xdr: pointer to xdr_stream 496 * @n: 64-bit integer to encode 497 * 498 * Returns: 499 * On success, returns length in bytes of XDR buffer consumed 500 * %-EMSGSIZE on XDR buffer overflow 501 */ 502 static inline ssize_t 503 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n) 504 { 505 const size_t len = sizeof(n); 506 __be32 *p = xdr_reserve_space(xdr, len); 507 508 if (unlikely(!p)) 509 return -EMSGSIZE; 510 xdr_encode_hyper(p, n); 511 return len; 512 } 513 514 /** 515 * xdr_stream_encode_opaque_inline - Encode opaque xdr data 516 * @xdr: pointer to xdr_stream 517 * @ptr: pointer to void pointer 518 * @len: size of object 519 * 520 * Returns: 521 * On success, returns length in bytes of XDR buffer consumed 522 * %-EMSGSIZE on XDR buffer overflow 523 */ 524 static inline ssize_t 525 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len) 526 { 527 size_t count = sizeof(__u32) + xdr_align_size(len); 528 __be32 *p = xdr_reserve_space(xdr, count); 529 530 if (unlikely(!p)) { 531 *ptr = NULL; 532 return -EMSGSIZE; 533 } 534 xdr_encode_opaque(p, NULL, len); 535 *ptr = ++p; 536 return count; 537 } 538 539 /** 540 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data 541 * @xdr: pointer to xdr_stream 542 * @ptr: pointer to opaque data object 543 * @len: size of object pointed to by @ptr 544 * 545 * Returns: 546 * On success, returns length in bytes of XDR buffer consumed 547 * %-EMSGSIZE on XDR buffer overflow 548 */ 549 static inline ssize_t 550 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len) 551 { 552 __be32 *p = xdr_reserve_space(xdr, len); 553 554 if (unlikely(!p)) 555 return -EMSGSIZE; 556 xdr_encode_opaque_fixed(p, ptr, len); 557 return xdr_align_size(len); 558 } 559 560 /** 561 * xdr_stream_encode_opaque - Encode variable length opaque xdr data 562 * @xdr: pointer to xdr_stream 563 * @ptr: pointer to opaque data object 564 * @len: size of object pointed to by @ptr 565 * 566 * Returns: 567 * On success, returns length in bytes of XDR buffer consumed 568 * %-EMSGSIZE on XDR buffer overflow 569 */ 570 static inline ssize_t 571 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len) 572 { 573 size_t count = sizeof(__u32) + xdr_align_size(len); 574 __be32 *p = xdr_reserve_space(xdr, count); 575 576 if (unlikely(!p)) 577 return -EMSGSIZE; 578 xdr_encode_opaque(p, ptr, len); 579 return count; 580 } 581 582 /** 583 * xdr_stream_encode_uint32_array - Encode variable length array of integers 584 * @xdr: pointer to xdr_stream 585 * @array: array of integers 586 * @array_size: number of elements in @array 587 * 588 * Returns: 589 * On success, returns length in bytes of XDR buffer consumed 590 * %-EMSGSIZE on XDR buffer overflow 591 */ 592 static inline ssize_t 593 xdr_stream_encode_uint32_array(struct xdr_stream *xdr, 594 const __u32 *array, size_t array_size) 595 { 596 ssize_t ret = (array_size+1) * sizeof(__u32); 597 __be32 *p = xdr_reserve_space(xdr, ret); 598 599 if (unlikely(!p)) 600 return -EMSGSIZE; 601 *p++ = cpu_to_be32(array_size); 602 for (; array_size > 0; p++, array++, array_size--) 603 *p = cpu_to_be32p(array); 604 return ret; 605 } 606 607 /** 608 * xdr_item_is_absent - symbolically handle XDR discriminators 609 * @p: pointer to undecoded discriminator 610 * 611 * Returns: 612 * %true if the following XDR item is absent 613 * %false if the following XDR item is present 614 */ 615 static inline bool xdr_item_is_absent(const __be32 *p) 616 { 617 return *p == xdr_zero; 618 } 619 620 /** 621 * xdr_item_is_present - symbolically handle XDR discriminators 622 * @p: pointer to undecoded discriminator 623 * 624 * Returns: 625 * %true if the following XDR item is present 626 * %false if the following XDR item is absent 627 */ 628 static inline bool xdr_item_is_present(const __be32 *p) 629 { 630 return *p != xdr_zero; 631 } 632 633 /** 634 * xdr_stream_decode_bool - Decode a boolean 635 * @xdr: pointer to xdr_stream 636 * @ptr: pointer to a u32 in which to store the result 637 * 638 * Returns: 639 * %0 on success 640 * %-EBADMSG on XDR buffer overflow 641 */ 642 static inline ssize_t 643 xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr) 644 { 645 const size_t count = sizeof(*ptr); 646 __be32 *p = xdr_inline_decode(xdr, count); 647 648 if (unlikely(!p)) 649 return -EBADMSG; 650 *ptr = (*p != xdr_zero); 651 return 0; 652 } 653 654 /** 655 * xdr_stream_decode_u32 - Decode a 32-bit integer 656 * @xdr: pointer to xdr_stream 657 * @ptr: location to store integer 658 * 659 * Returns: 660 * %0 on success 661 * %-EBADMSG on XDR buffer overflow 662 */ 663 static inline ssize_t 664 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr) 665 { 666 const size_t count = sizeof(*ptr); 667 __be32 *p = xdr_inline_decode(xdr, count); 668 669 if (unlikely(!p)) 670 return -EBADMSG; 671 *ptr = be32_to_cpup(p); 672 return 0; 673 } 674 675 /** 676 * xdr_stream_decode_be32 - Decode a big-endian 32-bit integer 677 * @xdr: pointer to xdr_stream 678 * @ptr: location to store integer 679 * 680 * Returns: 681 * %0 on success 682 * %-EBADMSG on XDR buffer overflow 683 */ 684 static inline ssize_t 685 xdr_stream_decode_be32(struct xdr_stream *xdr, __be32 *ptr) 686 { 687 const size_t count = sizeof(*ptr); 688 __be32 *p = xdr_inline_decode(xdr, count); 689 690 if (unlikely(!p)) 691 return -EBADMSG; 692 *ptr = *p; 693 return 0; 694 } 695 696 /** 697 * xdr_stream_decode_u64 - Decode a 64-bit integer 698 * @xdr: pointer to xdr_stream 699 * @ptr: location to store 64-bit integer 700 * 701 * Returns: 702 * %0 on success 703 * %-EBADMSG on XDR buffer overflow 704 */ 705 static inline ssize_t 706 xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr) 707 { 708 const size_t count = sizeof(*ptr); 709 __be32 *p = xdr_inline_decode(xdr, count); 710 711 if (unlikely(!p)) 712 return -EBADMSG; 713 xdr_decode_hyper(p, ptr); 714 return 0; 715 } 716 717 /** 718 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data 719 * @xdr: pointer to xdr_stream 720 * @ptr: location to store data 721 * @len: size of buffer pointed to by @ptr 722 * 723 * Returns: 724 * %0 on success 725 * %-EBADMSG on XDR buffer overflow 726 */ 727 static inline ssize_t 728 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len) 729 { 730 __be32 *p = xdr_inline_decode(xdr, len); 731 732 if (unlikely(!p)) 733 return -EBADMSG; 734 xdr_decode_opaque_fixed(p, ptr, len); 735 return 0; 736 } 737 738 /** 739 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data 740 * @xdr: pointer to xdr_stream 741 * @ptr: location to store pointer to opaque data 742 * @maxlen: maximum acceptable object size 743 * 744 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR 745 * buffer has been destroyed, or even after calling xdr_inline_decode() 746 * on @xdr. It is therefore expected that the object it points to should 747 * be processed immediately. 748 * 749 * Returns: 750 * On success, returns size of object stored in *@ptr 751 * %-EBADMSG on XDR buffer overflow 752 * %-EMSGSIZE if the size of the object would exceed @maxlen 753 */ 754 static inline ssize_t 755 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen) 756 { 757 __be32 *p; 758 __u32 len; 759 760 *ptr = NULL; 761 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0)) 762 return -EBADMSG; 763 if (len != 0) { 764 p = xdr_inline_decode(xdr, len); 765 if (unlikely(!p)) 766 return -EBADMSG; 767 if (unlikely(len > maxlen)) 768 return -EMSGSIZE; 769 *ptr = p; 770 } 771 return len; 772 } 773 774 /** 775 * xdr_stream_decode_uint32_array - Decode variable length array of integers 776 * @xdr: pointer to xdr_stream 777 * @array: location to store the integer array or NULL 778 * @array_size: number of elements to store 779 * 780 * Returns: 781 * On success, returns number of elements stored in @array 782 * %-EBADMSG on XDR buffer overflow 783 * %-EMSGSIZE if the size of the array exceeds @array_size 784 */ 785 static inline ssize_t 786 xdr_stream_decode_uint32_array(struct xdr_stream *xdr, 787 __u32 *array, size_t array_size) 788 { 789 __be32 *p; 790 __u32 len; 791 ssize_t retval; 792 793 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0)) 794 return -EBADMSG; 795 if (U32_MAX >= SIZE_MAX / sizeof(*p) && len > SIZE_MAX / sizeof(*p)) 796 return -EBADMSG; 797 p = xdr_inline_decode(xdr, len * sizeof(*p)); 798 if (unlikely(!p)) 799 return -EBADMSG; 800 if (array == NULL) 801 return len; 802 if (len <= array_size) { 803 if (len < array_size) 804 memset(array+len, 0, (array_size-len)*sizeof(*array)); 805 array_size = len; 806 retval = len; 807 } else 808 retval = -EMSGSIZE; 809 for (; array_size > 0; p++, array++, array_size--) 810 *array = be32_to_cpup(p); 811 return retval; 812 } 813 814 #endif /* _SUNRPC_XDR_H_ */ 815