| 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 | #ifdef __KERNEL__ |
| 15 | |
| 16 | #include <linux/uio.h> |
| 17 | #include <asm/byteorder.h> |
| 18 | #include <asm/unaligned.h> |
| 19 | #include <linux/scatterlist.h> |
| 20 | |
| 21 | struct rpc_rqst; |
| 22 | |
| 23 | /* |
| 24 | * Buffer adjustment |
| 25 | */ |
| 26 | #define XDR_QUADLEN(l) (((l) + 3) >> 2) |
| 27 | |
| 28 | /* |
| 29 | * Generic opaque `network object.' At the kernel level, this type |
| 30 | * is used only by lockd. |
| 31 | */ |
| 32 | #define XDR_MAX_NETOBJ 1024 |
| 33 | struct xdr_netobj { |
| 34 | unsigned int len; |
| 35 | u8 * data; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * Basic structure for transmission/reception of a client XDR message. |
| 40 | * Features a header (for a linear buffer containing RPC headers |
| 41 | * and the data payload for short messages), and then an array of |
| 42 | * pages. |
| 43 | * The tail iovec allows you to append data after the page array. Its |
| 44 | * main interest is for appending padding to the pages in order to |
| 45 | * satisfy the int_32-alignment requirements in RFC1832. |
| 46 | * |
| 47 | * For the future, we might want to string several of these together |
| 48 | * in a list if anybody wants to make use of NFSv4 COMPOUND |
| 49 | * operations and/or has a need for scatter/gather involving pages. |
| 50 | */ |
| 51 | struct xdr_buf { |
| 52 | struct kvec head[1], /* RPC header + non-page data */ |
| 53 | tail[1]; /* Appended after page data */ |
| 54 | |
| 55 | struct page ** pages; /* Array of pages */ |
| 56 | unsigned int page_base, /* Start of page data */ |
| 57 | page_len, /* Length of page data */ |
| 58 | flags; /* Flags for data disposition */ |
| 59 | #define XDRBUF_READ 0x01 /* target of file read */ |
| 60 | #define XDRBUF_WRITE 0x02 /* source of file write */ |
| 61 | |
| 62 | unsigned int buflen, /* Total length of storage buffer */ |
| 63 | len; /* Length of XDR encoded message */ |
| 64 | }; |
| 65 | |
| 66 | static inline void |
| 67 | xdr_buf_init(struct xdr_buf *buf, void *start, size_t len) |
| 68 | { |
| 69 | buf->head[0].iov_base = start; |
| 70 | buf->head[0].iov_len = len; |
| 71 | buf->tail[0].iov_len = 0; |
| 72 | buf->page_len = 0; |
| 73 | buf->flags = 0; |
| 74 | buf->len = 0; |
| 75 | buf->buflen = len; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * pre-xdr'ed macros. |
| 80 | */ |
| 81 | |
| 82 | #define xdr_zero cpu_to_be32(0) |
| 83 | #define xdr_one cpu_to_be32(1) |
| 84 | #define xdr_two cpu_to_be32(2) |
| 85 | |
| 86 | #define rpc_success cpu_to_be32(RPC_SUCCESS) |
| 87 | #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL) |
| 88 | #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH) |
| 89 | #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL) |
| 90 | #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS) |
| 91 | #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR) |
| 92 | #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY) |
| 93 | |
| 94 | #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK) |
| 95 | #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED) |
| 96 | #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED) |
| 97 | #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF) |
| 98 | #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF) |
| 99 | #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK) |
| 100 | #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM) |
| 101 | #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM) |
| 102 | #define rpc_autherr_oldseqnum cpu_to_be32(101) |
| 103 | |
| 104 | /* |
| 105 | * Miscellaneous XDR helper functions |
| 106 | */ |
| 107 | __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len); |
| 108 | __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len); |
| 109 | __be32 *xdr_encode_string(__be32 *p, const char *s); |
| 110 | __be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp, |
| 111 | unsigned int maxlen); |
| 112 | __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *); |
| 113 | __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *); |
| 114 | |
| 115 | void xdr_inline_pages(struct xdr_buf *, unsigned int, |
| 116 | struct page **, unsigned int, unsigned int); |
| 117 | void xdr_terminate_string(struct xdr_buf *, const u32); |
| 118 | |
| 119 | static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len) |
| 120 | { |
| 121 | return xdr_encode_opaque(p, s, len); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Decode 64bit quantities (NFSv3 support) |
| 126 | */ |
| 127 | static inline __be32 * |
| 128 | xdr_encode_hyper(__be32 *p, __u64 val) |
| 129 | { |
| 130 | put_unaligned_be64(val, p); |
| 131 | return p + 2; |
| 132 | } |
| 133 | |
| 134 | static inline __be32 * |
| 135 | xdr_decode_hyper(__be32 *p, __u64 *valp) |
| 136 | { |
| 137 | *valp = get_unaligned_be64(p); |
| 138 | return p + 2; |
| 139 | } |
| 140 | |
| 141 | static inline __be32 * |
| 142 | xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len) |
| 143 | { |
| 144 | memcpy(ptr, p, len); |
| 145 | return p + XDR_QUADLEN(len); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Adjust kvec to reflect end of xdr'ed data (RPC client XDR) |
| 150 | */ |
| 151 | static inline int |
| 152 | xdr_adjust_iovec(struct kvec *iov, __be32 *p) |
| 153 | { |
| 154 | return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * XDR buffer helper functions |
| 159 | */ |
| 160 | extern void xdr_shift_buf(struct xdr_buf *, size_t); |
| 161 | extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *); |
| 162 | extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int); |
| 163 | extern void xdr_buf_trim(struct xdr_buf *, unsigned int); |
| 164 | extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int); |
| 165 | extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int); |
| 166 | extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int); |
| 167 | |
| 168 | /* |
| 169 | * Helper structure for copying from an sk_buff. |
| 170 | */ |
| 171 | struct xdr_skb_reader { |
| 172 | struct sk_buff *skb; |
| 173 | unsigned int offset; |
| 174 | size_t count; |
| 175 | __wsum csum; |
| 176 | }; |
| 177 | |
| 178 | typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len); |
| 179 | |
| 180 | size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len); |
| 181 | extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *); |
| 182 | extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int, |
| 183 | struct xdr_skb_reader *, xdr_skb_read_actor); |
| 184 | |
| 185 | extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32); |
| 186 | extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *); |
| 187 | |
| 188 | struct xdr_array2_desc; |
| 189 | typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem); |
| 190 | struct xdr_array2_desc { |
| 191 | unsigned int elem_size; |
| 192 | unsigned int array_len; |
| 193 | unsigned int array_maxlen; |
| 194 | xdr_xcode_elem_t xcode; |
| 195 | }; |
| 196 | |
| 197 | extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base, |
| 198 | struct xdr_array2_desc *desc); |
| 199 | extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base, |
| 200 | struct xdr_array2_desc *desc); |
| 201 | extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase, |
| 202 | size_t len); |
| 203 | |
| 204 | /* |
| 205 | * Provide some simple tools for XDR buffer overflow-checking etc. |
| 206 | */ |
| 207 | struct xdr_stream { |
| 208 | __be32 *p; /* start of available buffer */ |
| 209 | struct xdr_buf *buf; /* XDR buffer to read/write */ |
| 210 | |
| 211 | __be32 *end; /* end of available buffer space */ |
| 212 | struct kvec *iov; /* pointer to the current kvec */ |
| 213 | struct kvec scratch; /* Scratch buffer */ |
| 214 | struct page **page_ptr; /* pointer to the current page */ |
| 215 | unsigned int nwords; /* Remaining decode buffer length */ |
| 216 | }; |
| 217 | |
| 218 | /* |
| 219 | * These are the xdr_stream style generic XDR encode and decode functions. |
| 220 | */ |
| 221 | typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, |
| 222 | const void *obj); |
| 223 | typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr, |
| 224 | void *obj); |
| 225 | |
| 226 | extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p); |
| 227 | extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes); |
| 228 | extern void xdr_commit_encode(struct xdr_stream *xdr); |
| 229 | extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len); |
| 230 | extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen); |
| 231 | extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, |
| 232 | unsigned int base, unsigned int len); |
| 233 | extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr); |
| 234 | extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p); |
| 235 | extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf, |
| 236 | struct page **pages, unsigned int len); |
| 237 | extern void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen); |
| 238 | extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes); |
| 239 | extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len); |
| 240 | extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len); |
| 241 | extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data); |
| 242 | |
| 243 | /** |
| 244 | * xdr_stream_remaining - Return the number of bytes remaining in the stream |
| 245 | * @xdr: pointer to struct xdr_stream |
| 246 | * |
| 247 | * Return value: |
| 248 | * Number of bytes remaining in @xdr before xdr->end |
| 249 | */ |
| 250 | static inline size_t |
| 251 | xdr_stream_remaining(const struct xdr_stream *xdr) |
| 252 | { |
| 253 | return xdr->nwords << 2; |
| 254 | } |
| 255 | |
| 256 | ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str, |
| 257 | size_t maxlen, gfp_t gfp_flags); |
| 258 | /** |
| 259 | * xdr_align_size - Calculate padded size of an object |
| 260 | * @n: Size of an object being XDR encoded (in bytes) |
| 261 | * |
| 262 | * Return value: |
| 263 | * Size (in bytes) of the object including xdr padding |
| 264 | */ |
| 265 | static inline size_t |
| 266 | xdr_align_size(size_t n) |
| 267 | { |
| 268 | const size_t mask = sizeof(__u32) - 1; |
| 269 | |
| 270 | return (n + mask) & ~mask; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * xdr_stream_encode_u32 - Encode a 32-bit integer |
| 275 | * @xdr: pointer to xdr_stream |
| 276 | * @n: integer to encode |
| 277 | * |
| 278 | * Return values: |
| 279 | * On success, returns length in bytes of XDR buffer consumed |
| 280 | * %-EMSGSIZE on XDR buffer overflow |
| 281 | */ |
| 282 | static inline ssize_t |
| 283 | xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n) |
| 284 | { |
| 285 | const size_t len = sizeof(n); |
| 286 | __be32 *p = xdr_reserve_space(xdr, len); |
| 287 | |
| 288 | if (unlikely(!p)) |
| 289 | return -EMSGSIZE; |
| 290 | *p = cpu_to_be32(n); |
| 291 | return len; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * xdr_stream_encode_u64 - Encode a 64-bit integer |
| 296 | * @xdr: pointer to xdr_stream |
| 297 | * @n: 64-bit integer to encode |
| 298 | * |
| 299 | * Return values: |
| 300 | * On success, returns length in bytes of XDR buffer consumed |
| 301 | * %-EMSGSIZE on XDR buffer overflow |
| 302 | */ |
| 303 | static inline ssize_t |
| 304 | xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n) |
| 305 | { |
| 306 | const size_t len = sizeof(n); |
| 307 | __be32 *p = xdr_reserve_space(xdr, len); |
| 308 | |
| 309 | if (unlikely(!p)) |
| 310 | return -EMSGSIZE; |
| 311 | xdr_encode_hyper(p, n); |
| 312 | return len; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data |
| 317 | * @xdr: pointer to xdr_stream |
| 318 | * @ptr: pointer to opaque data object |
| 319 | * @len: size of object pointed to by @ptr |
| 320 | * |
| 321 | * Return values: |
| 322 | * On success, returns length in bytes of XDR buffer consumed |
| 323 | * %-EMSGSIZE on XDR buffer overflow |
| 324 | */ |
| 325 | static inline ssize_t |
| 326 | xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len) |
| 327 | { |
| 328 | __be32 *p = xdr_reserve_space(xdr, len); |
| 329 | |
| 330 | if (unlikely(!p)) |
| 331 | return -EMSGSIZE; |
| 332 | xdr_encode_opaque_fixed(p, ptr, len); |
| 333 | return xdr_align_size(len); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * xdr_stream_encode_opaque - Encode variable length opaque xdr data |
| 338 | * @xdr: pointer to xdr_stream |
| 339 | * @ptr: pointer to opaque data object |
| 340 | * @len: size of object pointed to by @ptr |
| 341 | * |
| 342 | * Return values: |
| 343 | * On success, returns length in bytes of XDR buffer consumed |
| 344 | * %-EMSGSIZE on XDR buffer overflow |
| 345 | */ |
| 346 | static inline ssize_t |
| 347 | xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len) |
| 348 | { |
| 349 | size_t count = sizeof(__u32) + xdr_align_size(len); |
| 350 | __be32 *p = xdr_reserve_space(xdr, count); |
| 351 | |
| 352 | if (unlikely(!p)) |
| 353 | return -EMSGSIZE; |
| 354 | xdr_encode_opaque(p, ptr, len); |
| 355 | return count; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * xdr_stream_decode_u32 - Decode a 32-bit integer |
| 360 | * @xdr: pointer to xdr_stream |
| 361 | * @ptr: location to store integer |
| 362 | * |
| 363 | * Return values: |
| 364 | * %0 on success |
| 365 | * %-EBADMSG on XDR buffer overflow |
| 366 | */ |
| 367 | static inline ssize_t |
| 368 | xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr) |
| 369 | { |
| 370 | const size_t count = sizeof(*ptr); |
| 371 | __be32 *p = xdr_inline_decode(xdr, count); |
| 372 | |
| 373 | if (unlikely(!p)) |
| 374 | return -EBADMSG; |
| 375 | *ptr = be32_to_cpup(p); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data |
| 381 | * @xdr: pointer to xdr_stream |
| 382 | * @ptr: location to store data |
| 383 | * @len: size of buffer pointed to by @ptr |
| 384 | * |
| 385 | * Return values: |
| 386 | * On success, returns size of object stored in @ptr |
| 387 | * %-EBADMSG on XDR buffer overflow |
| 388 | */ |
| 389 | static inline ssize_t |
| 390 | xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len) |
| 391 | { |
| 392 | __be32 *p = xdr_inline_decode(xdr, len); |
| 393 | |
| 394 | if (unlikely(!p)) |
| 395 | return -EBADMSG; |
| 396 | xdr_decode_opaque_fixed(p, ptr, len); |
| 397 | return len; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data |
| 402 | * @xdr: pointer to xdr_stream |
| 403 | * @ptr: location to store pointer to opaque data |
| 404 | * @maxlen: maximum acceptable object size |
| 405 | * |
| 406 | * Note: the pointer stored in @ptr cannot be assumed valid after the XDR |
| 407 | * buffer has been destroyed, or even after calling xdr_inline_decode() |
| 408 | * on @xdr. It is therefore expected that the object it points to should |
| 409 | * be processed immediately. |
| 410 | * |
| 411 | * Return values: |
| 412 | * On success, returns size of object stored in *@ptr |
| 413 | * %-EBADMSG on XDR buffer overflow |
| 414 | * %-EMSGSIZE if the size of the object would exceed @maxlen |
| 415 | */ |
| 416 | static inline ssize_t |
| 417 | xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen) |
| 418 | { |
| 419 | __be32 *p; |
| 420 | __u32 len; |
| 421 | |
| 422 | *ptr = NULL; |
| 423 | if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0)) |
| 424 | return -EBADMSG; |
| 425 | if (len != 0) { |
| 426 | p = xdr_inline_decode(xdr, len); |
| 427 | if (unlikely(!p)) |
| 428 | return -EBADMSG; |
| 429 | if (unlikely(len > maxlen)) |
| 430 | return -EMSGSIZE; |
| 431 | *ptr = p; |
| 432 | } |
| 433 | return len; |
| 434 | } |
| 435 | #endif /* __KERNEL__ */ |
| 436 | |
| 437 | #endif /* _SUNRPC_XDR_H_ */ |