~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/sunrpc/xdr.h

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp