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

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

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ 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 struct rpc_rqst;
 21 
 22 /*
 23  * Buffer adjustment
 24  */
 25 #define XDR_QUADLEN(l)          (((l) + 3) >> 2)
 26 
 27 /*
 28  * Generic opaque `network object.' At the kernel level, this type
 29  * is used only by lockd.
 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
 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 
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 *
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 *
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 *
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
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 ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
243                 size_t maxlen, gfp_t gfp_flags);
244 /**
245  * xdr_align_size - Calculate padded size of an object
246  * @n: Size of an object being XDR encoded (in bytes)
247  *
248  * Return value:
249  *   Size (in bytes) of the object including xdr padding
250  */
251 static inline size_t
252 xdr_align_size(size_t n)
253 {
254         const size_t mask = sizeof(__u32) - 1;
255 
256         return (n + mask) & ~mask;
257 }
258 
259 /**
260  * xdr_stream_encode_u32 - Encode a 32-bit integer
261  * @xdr: pointer to xdr_stream
262  * @n: integer to encode
263  *
264  * Return values:
265  *   On success, returns length in bytes of XDR buffer consumed
266  *   %-EMSGSIZE on XDR buffer overflow
267  */
268 static inline ssize_t
269 xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
270 {
271         const size_t len = sizeof(n);
272         __be32 *p = xdr_reserve_space(xdr, len);
273 
274         if (unlikely(!p))
275                 return -EMSGSIZE;
276         *p = cpu_to_be32(n);
277         return len;
278 }
279 
280 /**
281  * xdr_stream_encode_u64 - Encode a 64-bit integer
282  * @xdr: pointer to xdr_stream
283  * @n: 64-bit integer to encode
284  *
285  * Return values:
286  *   On success, returns length in bytes of XDR buffer consumed
287  *   %-EMSGSIZE on XDR buffer overflow
288  */
289 static inline ssize_t
290 xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
291 {
292         const size_t len = sizeof(n);
293         __be32 *p = xdr_reserve_space(xdr, len);
294 
295         if (unlikely(!p))
296                 return -EMSGSIZE;
297         xdr_encode_hyper(p, n);
298         return len;
299 }
300 
301 /**
302  * xdr_stream_encode_opaque_fixed - Encode fixed length opaque xdr data
303  * @xdr: pointer to xdr_stream
304  * @ptr: pointer to opaque data object
305  * @len: size of object pointed to by @ptr
306  *
307  * Return values:
308  *   On success, returns length in bytes of XDR buffer consumed
309  *   %-EMSGSIZE on XDR buffer overflow
310  */
311 static inline ssize_t
312 xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
313 {
314         __be32 *p = xdr_reserve_space(xdr, len);
315 
316         if (unlikely(!p))
317                 return -EMSGSIZE;
318         xdr_encode_opaque_fixed(p, ptr, len);
319         return xdr_align_size(len);
320 }
321 
322 /**
323  * xdr_stream_encode_opaque - Encode variable length opaque xdr data
324  * @xdr: pointer to xdr_stream
325  * @ptr: pointer to opaque data object
326  * @len: size of object pointed to by @ptr
327  *
328  * Return values:
329  *   On success, returns length in bytes of XDR buffer consumed
330  *   %-EMSGSIZE on XDR buffer overflow
331  */
332 static inline ssize_t
333 xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
334 {
335         size_t count = sizeof(__u32) + xdr_align_size(len);
336         __be32 *p = xdr_reserve_space(xdr, count);
337 
338         if (unlikely(!p))
339                 return -EMSGSIZE;
340         xdr_encode_opaque(p, ptr, len);
341         return count;
342 }
343 
344 /**
345  * xdr_stream_decode_u32 - Decode a 32-bit integer
346  * @xdr: pointer to xdr_stream
347  * @ptr: location to store integer
348  *
349  * Return values:
350  *   %0 on success
351  *   %-EBADMSG on XDR buffer overflow
352  */
353 static inline ssize_t
354 xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
355 {
356         const size_t count = sizeof(*ptr);
357         __be32 *p = xdr_inline_decode(xdr, count);
358 
359         if (unlikely(!p))
360                 return -EBADMSG;
361         *ptr = be32_to_cpup(p);
362         return 0;
363 }
364 
365 /**
366  * xdr_stream_decode_opaque_fixed - Decode fixed length opaque xdr data
367  * @xdr: pointer to xdr_stream
368  * @ptr: location to store data
369  * @len: size of buffer pointed to by @ptr
370  *
371  * Return values:
372  *   On success, returns size of object stored in @ptr
373  *   %-EBADMSG on XDR buffer overflow
374  */
375 static inline ssize_t
376 xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
377 {
378         __be32 *p = xdr_inline_decode(xdr, len);
379 
380         if (unlikely(!p))
381                 return -EBADMSG;
382         xdr_decode_opaque_fixed(p, ptr, len);
383         return len;
384 }
385 
386 /**
387  * xdr_stream_decode_opaque_inline - Decode variable length opaque xdr data
388  * @xdr: pointer to xdr_stream
389  * @ptr: location to store pointer to opaque data
390  * @maxlen: maximum acceptable object size
391  *
392  * Note: the pointer stored in @ptr cannot be assumed valid after the XDR
393  * buffer has been destroyed, or even after calling xdr_inline_decode()
394  * on @xdr. It is therefore expected that the object it points to should
395  * be processed immediately.
396  *
397  * Return values:
398  *   On success, returns size of object stored in *@ptr
399  *   %-EBADMSG on XDR buffer overflow
400  *   %-EMSGSIZE if the size of the object would exceed @maxlen
401  */
402 static inline ssize_t
403 xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
404 {
405         __be32 *p;
406         __u32 len;
407 
408         *ptr = NULL;
409         if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
410                 return -EBADMSG;
411         if (len != 0) {
412                 p = xdr_inline_decode(xdr, len);
413                 if (unlikely(!p))
414                         return -EBADMSG;
415                 if (unlikely(len > maxlen))
416                         return -EMSGSIZE;
417                 *ptr = p;
418         }
419         return len;
420 }
421 #endif /* __KERNEL__ */
422 
423 #endif /* _SUNRPC_XDR_H_ */
424 

~ [ 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