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