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