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