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