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
xdr_buf_init(struct xdr_buf * buf,void * start,size_t len)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_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
134 unsigned int maxlen);
135 __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
136 __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
137
138 void xdr_inline_pages(struct xdr_buf *, unsigned int,
139 struct page **, unsigned int, unsigned int);
140 void xdr_terminate_string(const struct xdr_buf *, const u32);
141 size_t xdr_buf_pagecount(const struct xdr_buf *buf);
142 int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp);
143 void xdr_free_bvec(struct xdr_buf *buf);
144 unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
145 const struct xdr_buf *xdr);
146
xdr_encode_array(__be32 * p,const void * s,unsigned int len)147 static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
148 {
149 return xdr_encode_opaque(p, s, len);
150 }
151
152 /*
153 * Decode 64bit quantities (NFSv3 support)
154 */
155 static inline __be32 *
xdr_encode_hyper(__be32 * p,__u64 val)156 xdr_encode_hyper(__be32 *p, __u64 val)
157 {
158 put_unaligned_be64(val, p);
159 return p + 2;
160 }
161
162 static inline __be32 *
xdr_decode_hyper(__be32 * p,__u64 * valp)163 xdr_decode_hyper(__be32 *p, __u64 *valp)
164 {
165 *valp = get_unaligned_be64(p);
166 return p + 2;
167 }
168
169 static inline __be32 *
xdr_decode_opaque_fixed(__be32 * p,void * ptr,unsigned int len)170 xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
171 {
172 memcpy(ptr, p, len);
173 return p + XDR_QUADLEN(len);
174 }
175
xdr_netobj_dup(struct xdr_netobj * dst,struct xdr_netobj * src,gfp_t gfp_mask)176 static inline void xdr_netobj_dup(struct xdr_netobj *dst,
177 struct xdr_netobj *src, gfp_t gfp_mask)
178 {
179 dst->data = kmemdup(src->data, src->len, gfp_mask);
180 dst->len = src->len;
181 }
182
183 /*
184 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
185 */
186 static inline int
xdr_adjust_iovec(struct kvec * iov,__be32 * p)187 xdr_adjust_iovec(struct kvec *iov, __be32 *p)
188 {
189 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
190 }
191
192 /*
193 * XDR buffer helper functions
194 */
195 extern void xdr_buf_from_iov(const struct kvec *, struct xdr_buf *);
196 extern int xdr_buf_subsegment(const struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
197 extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
198 extern int read_bytes_from_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
199 extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
200
201 extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32);
202 extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *);
203
204 struct xdr_array2_desc;
205 typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
206 struct xdr_array2_desc {
207 unsigned int elem_size;
208 unsigned int array_len;
209 unsigned int array_maxlen;
210 xdr_xcode_elem_t xcode;
211 };
212
213 extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
214 struct xdr_array2_desc *desc);
215 extern int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
216 struct xdr_array2_desc *desc);
217 extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
218 size_t len);
219
220 /*
221 * Provide some simple tools for XDR buffer overflow-checking etc.
222 */
223 struct xdr_stream {
224 __be32 *p; /* start of available buffer */
225 struct xdr_buf *buf; /* XDR buffer to read/write */
226
227 __be32 *end; /* end of available buffer space */
228 struct kvec *iov; /* pointer to the current kvec */
229 struct kvec scratch; /* Scratch buffer */
230 struct page **page_ptr; /* pointer to the current page */
231 void *page_kaddr; /* kmapped address of the current page */
232 unsigned int nwords; /* Remaining decode buffer length */
233
234 struct rpc_rqst *rqst; /* For debugging */
235 };
236
237 /*
238 * These are the xdr_stream style generic XDR encode and decode functions.
239 */
240 typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
241 const void *obj);
242 typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
243 void *obj);
244
245 extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf,
246 __be32 *p, struct rpc_rqst *rqst);
247 void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf);
248 extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
249 extern int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes);
250 extern void __xdr_commit_encode(struct xdr_stream *xdr);
251 extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
252 extern void xdr_truncate_decode(struct xdr_stream *xdr, size_t len);
253 extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
254 extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
255 unsigned int base, unsigned int len);
256 extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
257 extern unsigned int xdr_page_pos(const struct xdr_stream *xdr);
258 extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf,
259 __be32 *p, struct rpc_rqst *rqst);
260 extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
261 struct page **pages, unsigned int len);
262 extern void xdr_finish_decode(struct xdr_stream *xdr);
263 extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
264 extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
265 extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
266 extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
267 extern void xdr_set_pagelen(struct xdr_stream *, unsigned int len);
268 extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
269 unsigned int len);
270 extern unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
271 unsigned int target, unsigned int length);
272 extern unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
273 unsigned int length);
274
275 /**
276 * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
277 * @xdr: pointer to xdr_stream struct
278 * @buf: pointer to an empty buffer
279 * @buflen: size of 'buf'
280 *
281 * The scratch buffer is used when decoding from an array of pages.
282 * If an xdr_inline_decode() call spans across page boundaries, then
283 * we copy the data into the scratch buffer in order to allow linear
284 * access.
285 */
286 static inline void
xdr_set_scratch_buffer(struct xdr_stream * xdr,void * buf,size_t buflen)287 xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
288 {
289 xdr->scratch.iov_base = buf;
290 xdr->scratch.iov_len = buflen;
291 }
292
293 /**
294 * xdr_set_scratch_page - Attach a scratch buffer for decoding data
295 * @xdr: pointer to xdr_stream struct
296 * @page: an anonymous page
297 *
298 * See xdr_set_scratch_buffer().
299 */
300 static inline void
xdr_set_scratch_page(struct xdr_stream * xdr,struct page * page)301 xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page)
302 {
303 xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE);
304 }
305
306 /**
307 * xdr_reset_scratch_buffer - Clear scratch buffer information
308 * @xdr: pointer to xdr_stream struct
309 *
310 * See xdr_set_scratch_buffer().
311 */
312 static inline void
xdr_reset_scratch_buffer(struct xdr_stream * xdr)313 xdr_reset_scratch_buffer(struct xdr_stream *xdr)
314 {
315 xdr_set_scratch_buffer(xdr, NULL, 0);
316 }
317
318 /**
319 * xdr_commit_encode - Ensure all data is written to xdr->buf
320 * @xdr: pointer to xdr_stream
321 *
322 * Handle encoding across page boundaries by giving the caller a
323 * temporary location to write to, then later copying the data into
324 * place. __xdr_commit_encode() does that copying.
325 */
xdr_commit_encode(struct xdr_stream * xdr)326 static inline void xdr_commit_encode(struct xdr_stream *xdr)
327 {
328 if (unlikely(xdr->scratch.iov_len))
329 __xdr_commit_encode(xdr);
330 }
331
332 /**
333 * xdr_stream_remaining - Return the number of bytes remaining in the stream
334 * @xdr: pointer to struct xdr_stream
335 *
336 * Return value:
337 * Number of bytes remaining in @xdr before xdr->end
338 */
339 static inline size_t
xdr_stream_remaining(const struct xdr_stream * xdr)340 xdr_stream_remaining(const struct xdr_stream *xdr)
341 {
342 return xdr->nwords << 2;
343 }
344
345 ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
346 size_t size);
347 ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
348 size_t maxlen, gfp_t gfp_flags);
349 ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
350 size_t size);
351 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
352 size_t maxlen, gfp_t gfp_flags);
353 ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
354 void **body, unsigned int *body_len);
355 ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
356 void *body, unsigned int body_len);
357
358 /**
359 * xdr_align_size - Calculate padded size of an object
360 * @n: Size of an object being XDR encoded (in bytes)
361 *
362 * Return value:
363 * Size (in bytes) of the object including xdr padding
364 */
365 static inline size_t
xdr_align_size(size_t n)366 xdr_align_size(size_t n)
367 {
368 const size_t mask = XDR_UNIT - 1;
369
370 return (n + mask) & ~mask;
371 }
372
373 /**
374 * xdr_pad_size - Calculate size of an object's pad
375 * @n: Size of an object being XDR encoded (in bytes)
376 *
377 * This implementation avoids the need for conditional
378 * branches or modulo division.
379 *
380 * Return value:
381 * Size (in bytes) of the needed XDR pad
382 */
xdr_pad_size(size_t n)383 static inline size_t xdr_pad_size(size_t n)
384 {
385 return xdr_align_size(n) - n;
386 }
387
388 /**
389 * xdr_stream_encode_item_present - Encode a "present" list item
390 * @xdr: pointer to xdr_stream
391 *
392 * Return values:
393 * On success, returns length in bytes of XDR buffer consumed
394 * %-EMSGSIZE on XDR buffer overflow
395 */
xdr_stream_encode_item_present(struct xdr_stream * xdr)396 static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
397 {
398 const size_t len = XDR_UNIT;
399 __be32 *p = xdr_reserve_space(xdr, len);
400
401 if (unlikely(!p))
402 return -EMSGSIZE;
403 *p = xdr_one;
404 return len;
405 }
406
407 /**
408 * xdr_stream_encode_item_absent - Encode a "not present" list item
409 * @xdr: pointer to xdr_stream
410 *
411 * Return values:
412 * On success, returns length in bytes of XDR buffer consumed
413 * %-EMSGSIZE on XDR buffer overflow
414 */
xdr_stream_encode_item_absent(struct xdr_stream * xdr)415 static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
416 {
417 const size_t len = XDR_UNIT;
418 __be32 *p = xdr_reserve_space(xdr, len);
419
420 if (unlikely(!p))
421 return -EMSGSIZE;
422 *p = xdr_zero;
423 return len;
424 }
425
426 /**
427 * xdr_encode_bool - Encode a boolean item
428 * @p: address in a buffer into which to encode
429 * @n: boolean value to encode
430 *
431 * Return value:
432 * Address of item following the encoded boolean
433 */
xdr_encode_bool(__be32 * p,u32 n)434 static inline __be32 *xdr_encode_bool(__be32 *p, u32 n)
435 {
436 *p++ = n ? xdr_one : xdr_zero;
437 return p;
438 }
439
440 /**
441 * xdr_stream_encode_bool - Encode a boolean item
442 * @xdr: pointer to xdr_stream
443 * @n: boolean value to encode
444 *
445 * Return values:
446 * On success, returns length in bytes of XDR buffer consumed
447 * %-EMSGSIZE on XDR buffer overflow
448 */
xdr_stream_encode_bool(struct xdr_stream * xdr,__u32 n)449 static inline int xdr_stream_encode_bool(struct xdr_stream *xdr, __u32 n)
450 {
451 const size_t len = XDR_UNIT;
452 __be32 *p = xdr_reserve_space(xdr, len);
453
454 if (unlikely(!p))
455 return -EMSGSIZE;
456 xdr_encode_bool(p, n);
457 return len;
458 }
459
460 /**
461 * xdr_stream_encode_u32 - Encode a 32-bit integer
462 * @xdr: pointer to xdr_stream
463 * @n: integer to encode
464 *
465 * Return values:
466 * On success, returns length in bytes of XDR buffer consumed
467 * %-EMSGSIZE on XDR buffer overflow
468 */
469 static inline ssize_t
xdr_stream_encode_u32(struct xdr_stream * xdr,__u32 n)470 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
471 {
472 const size_t len = sizeof(n);
473 __be32 *p = xdr_reserve_space(xdr, len);
474
475 if (unlikely(!p))
476 return -EMSGSIZE;
477 *p = cpu_to_be32(n);
478 return len;
479 }
480
481 /**
482 * xdr_stream_encode_be32 - Encode a big-endian 32-bit integer
483 * @xdr: pointer to xdr_stream
484 * @n: integer to encode
485 *
486 * Return values:
487 * On success, returns length in bytes of XDR buffer consumed
488 * %-EMSGSIZE on XDR buffer overflow
489 */
490 static inline ssize_t
xdr_stream_encode_be32(struct xdr_stream * xdr,__be32 n)491 xdr_stream_encode_be32(struct xdr_stream *xdr, __be32 n)
492 {
493 const size_t len = sizeof(n);
494 __be32 *p = xdr_reserve_space(xdr, len);
495
496 if (unlikely(!p))
497 return -EMSGSIZE;
498 *p = n;
499 return len;
500 }
501
502 /**
503 * xdr_stream_encode_u64 - Encode a 64-bit integer
504 * @xdr: pointer to xdr_stream
505 * @n: 64-bit integer to encode
506 *
507 * Return values:
508 * On success, returns length in bytes of XDR buffer consumed
509 * %-EMSGSIZE on XDR buffer overflow
510 */
511 static inline ssize_t
xdr_stream_encode_u64(struct xdr_stream * xdr,__u64 n)512 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
513 {
514 const size_t len = sizeof(n);
515 __be32 *p = xdr_reserve_space(xdr, len);
516
517 if (unlikely(!p))
518 return -EMSGSIZE;
519 xdr_encode_hyper(p, n);
520 return len;
521 }
522
523 /**
524 * xdr_stream_encode_opaque_inline - Encode opaque xdr data
525 * @xdr: pointer to xdr_stream
526 * @ptr: pointer to void pointer
527 * @len: size of object
528 *
529 * Return values:
530 * On success, returns length in bytes of XDR buffer consumed
531 * %-EMSGSIZE on XDR buffer overflow
532 */
533 static inline ssize_t
xdr_stream_encode_opaque_inline(struct xdr_stream * xdr,void ** ptr,size_t len)534 xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
535 {
536 size_t count = sizeof(__u32) + xdr_align_size(len);
537 __be32 *p = xdr_reserve_space(xdr, count);
538
539 if (unlikely(!p)) {
540 *ptr = NULL;
541 return -EMSGSIZE;
542 }
543 xdr_encode_opaque(p, NULL, len);
544 *ptr = ++p;
545 return count;
546 }
547
548 /**
549 * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
550 * @xdr: pointer to xdr_stream
551 * @ptr: pointer to opaque data object
552 * @len: size of object pointed to by @ptr
553 *
554 * Return values:
555 * On success, returns length in bytes of XDR buffer consumed
556 * %-EMSGSIZE on XDR buffer overflow
557 */
558 static inline ssize_t
xdr_stream_encode_opaque_fixed(struct xdr_stream * xdr,const void * ptr,size_t len)559 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
560 {
561 __be32 *p = xdr_reserve_space(xdr, len);
562
563 if (unlikely(!p))
564 return -EMSGSIZE;
565 xdr_encode_opaque_fixed(p, ptr, len);
566 return xdr_align_size(len);
567 }
568
569 /**
570 * xdr_stream_encode_opaque - Encode variable length opaque xdr data
571 * @xdr: pointer to xdr_stream
572 * @ptr: pointer to opaque data object
573 * @len: size of object pointed to by @ptr
574 *
575 * Return values:
576 * On success, returns length in bytes of XDR buffer consumed
577 * %-EMSGSIZE on XDR buffer overflow
578 */
579 static inline ssize_t
xdr_stream_encode_opaque(struct xdr_stream * xdr,const void * ptr,size_t len)580 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
581 {
582 size_t count = sizeof(__u32) + xdr_align_size(len);
583 __be32 *p = xdr_reserve_space(xdr, count);
584
585 if (unlikely(!p))
586 return -EMSGSIZE;
587 xdr_encode_opaque(p, ptr, len);
588 return count;
589 }
590
591 /**
592 * xdr_stream_encode_uint32_array - Encode variable length array of integers
593 * @xdr: pointer to xdr_stream
594 * @array: array of integers
595 * @array_size: number of elements in @array
596 *
597 * Return values:
598 * On success, returns length in bytes of XDR buffer consumed
599 * %-EMSGSIZE on XDR buffer overflow
600 */
601 static inline ssize_t
xdr_stream_encode_uint32_array(struct xdr_stream * xdr,const __u32 * array,size_t array_size)602 xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
603 const __u32 *array, size_t array_size)
604 {
605 ssize_t ret = (array_size+1) * sizeof(__u32);
606 __be32 *p = xdr_reserve_space(xdr, ret);
607
608 if (unlikely(!p))
609 return -EMSGSIZE;
610 *p++ = cpu_to_be32(array_size);
611 for (; array_size > 0; p++, array++, array_size--)
612 *p = cpu_to_be32p(array);
613 return ret;
614 }
615
616 /**
617 * xdr_item_is_absent - symbolically handle XDR discriminators
618 * @p: pointer to undecoded discriminator
619 *
620 * Return values:
621 * %true if the following XDR item is absent
622 * %false if the following XDR item is present
623 */
xdr_item_is_absent(const __be32 * p)624 static inline bool xdr_item_is_absent(const __be32 *p)
625 {
626 return *p == xdr_zero;
627 }
628
629 /**
630 * xdr_item_is_present - symbolically handle XDR discriminators
631 * @p: pointer to undecoded discriminator
632 *
633 * Return values:
634 * %true if the following XDR item is present
635 * %false if the following XDR item is absent
636 */
xdr_item_is_present(const __be32 * p)637 static inline bool xdr_item_is_present(const __be32 *p)
638 {
639 return *p != xdr_zero;
640 }
641
642 /**
643 * xdr_stream_decode_bool - Decode a boolean
644 * @xdr: pointer to xdr_stream
645 * @ptr: pointer to a u32 in which to store the result
646 *
647 * Return values:
648 * %0 on success
649 * %-EBADMSG on XDR buffer overflow
650 */
651 static inline ssize_t
xdr_stream_decode_bool(struct xdr_stream * xdr,__u32 * ptr)652 xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr)
653 {
654 const size_t count = sizeof(*ptr);
655 __be32 *p = xdr_inline_decode(xdr, count);
656
657 if (unlikely(!p))
658 return -EBADMSG;
659 *ptr = (*p != xdr_zero);
660 return 0;
661 }
662
663 /**
664 * xdr_stream_decode_u32 - Decode a 32-bit integer
665 * @xdr: pointer to xdr_stream
666 * @ptr: location to store integer
667 *
668 * Return values:
669 * %0 on success
670 * %-EBADMSG on XDR buffer overflow
671 */
672 static inline ssize_t
xdr_stream_decode_u32(struct xdr_stream * xdr,__u32 * ptr)673 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
674 {
675 const size_t count = sizeof(*ptr);
676 __be32 *p = xdr_inline_decode(xdr, count);
677
678 if (unlikely(!p))
679 return -EBADMSG;
680 *ptr = be32_to_cpup(p);
681 return 0;
682 }
683
684 /**
685 * xdr_stream_decode_be32 - Decode a big-endian 32-bit integer
686 * @xdr: pointer to xdr_stream
687 * @ptr: location to store integer
688 *
689 * Return values:
690 * %0 on success
691 * %-EBADMSG on XDR buffer overflow
692 */
693 static inline ssize_t
xdr_stream_decode_be32(struct xdr_stream * xdr,__be32 * ptr)694 xdr_stream_decode_be32(struct xdr_stream *xdr, __be32 *ptr)
695 {
696 const size_t count = sizeof(*ptr);
697 __be32 *p = xdr_inline_decode(xdr, count);
698
699 if (unlikely(!p))
700 return -EBADMSG;
701 *ptr = *p;
702 return 0;
703 }
704
705 /**
706 * xdr_stream_decode_u64 - Decode a 64-bit integer
707 * @xdr: pointer to xdr_stream
708 * @ptr: location to store 64-bit integer
709 *
710 * Return values:
711 * %0 on success
712 * %-EBADMSG on XDR buffer overflow
713 */
714 static inline ssize_t
xdr_stream_decode_u64(struct xdr_stream * xdr,__u64 * ptr)715 xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
716 {
717 const size_t count = sizeof(*ptr);
718 __be32 *p = xdr_inline_decode(xdr, count);
719
720 if (unlikely(!p))
721 return -EBADMSG;
722 xdr_decode_hyper(p, ptr);
723 return 0;
724 }
725
726 /**
727 * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
728 * @xdr: pointer to xdr_stream
729 * @ptr: location to store data
730 * @len: size of buffer pointed to by @ptr
731 *
732 * Return values:
733 * On success, returns size of object stored in @ptr
734 * %-EBADMSG on XDR buffer overflow
735 */
736 static inline ssize_t
xdr_stream_decode_opaque_fixed(struct xdr_stream * xdr,void * ptr,size_t len)737 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
738 {
739 __be32 *p = xdr_inline_decode(xdr, len);
740
741 if (unlikely(!p))
742 return -EBADMSG;
743 xdr_decode_opaque_fixed(p, ptr, len);
744 return len;
745 }
746
747 /**
748 * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
749 * @xdr: pointer to xdr_stream
750 * @ptr: location to store pointer to opaque data
751 * @maxlen: maximum acceptable object size
752 *
753 * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
754 * buffer has been destroyed, or even after calling xdr_inline_decode()
755 * on @xdr. It is therefore expected that the object it points to should
756 * be processed immediately.
757 *
758 * Return values:
759 * On success, returns size of object stored in *@ptr
760 * %-EBADMSG on XDR buffer overflow
761 * %-EMSGSIZE if the size of the object would exceed @maxlen
762 */
763 static inline ssize_t
xdr_stream_decode_opaque_inline(struct xdr_stream * xdr,void ** ptr,size_t maxlen)764 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
765 {
766 __be32 *p;
767 __u32 len;
768
769 *ptr = NULL;
770 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
771 return -EBADMSG;
772 if (len != 0) {
773 p = xdr_inline_decode(xdr, len);
774 if (unlikely(!p))
775 return -EBADMSG;
776 if (unlikely(len > maxlen))
777 return -EMSGSIZE;
778 *ptr = p;
779 }
780 return len;
781 }
782
783 /**
784 * xdr_stream_decode_uint32_array - Decode variable length array of integers
785 * @xdr: pointer to xdr_stream
786 * @array: location to store the integer array or NULL
787 * @array_size: number of elements to store
788 *
789 * Return values:
790 * On success, returns number of elements stored in @array
791 * %-EBADMSG on XDR buffer overflow
792 * %-EMSGSIZE if the size of the array exceeds @array_size
793 */
794 static inline ssize_t
xdr_stream_decode_uint32_array(struct xdr_stream * xdr,__u32 * array,size_t array_size)795 xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
796 __u32 *array, size_t array_size)
797 {
798 __be32 *p;
799 __u32 len;
800 ssize_t retval;
801
802 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
803 return -EBADMSG;
804 if (U32_MAX >= SIZE_MAX / sizeof(*p) && len > SIZE_MAX / sizeof(*p))
805 return -EBADMSG;
806 p = xdr_inline_decode(xdr, len * sizeof(*p));
807 if (unlikely(!p))
808 return -EBADMSG;
809 if (array == NULL)
810 return len;
811 if (len <= array_size) {
812 if (len < array_size)
813 memset(array+len, 0, (array_size-len)*sizeof(*array));
814 array_size = len;
815 retval = len;
816 } else
817 retval = -EMSGSIZE;
818 for (; array_size > 0; p++, array++, array_size--)
819 *array = be32_to_cpup(p);
820 return retval;
821 }
822
823 #endif /* _SUNRPC_XDR_H_ */
824