svcxdr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Encode/decode NLM basic data types
  4. *
  5. * Basic NLMv3 XDR data types are not defined in an IETF standards
  6. * document. X/Open has a description of these data types that
  7. * is useful. See Chapter 10 of "Protocols for Interworking:
  8. * XNFS, Version 3W".
  9. *
  10. * Basic NLMv4 XDR data types are defined in Appendix II.1.4 of
  11. * RFC 1813: "NFS Version 3 Protocol Specification".
  12. *
  13. * Author: Chuck Lever <chuck.lever@oracle.com>
  14. *
  15. * Copyright (c) 2020, Oracle and/or its affiliates.
  16. */
  17. #ifndef _LOCKD_SVCXDR_H_
  18. #define _LOCKD_SVCXDR_H_
  19. static inline bool
  20. svcxdr_decode_stats(struct xdr_stream *xdr, __be32 *status)
  21. {
  22. __be32 *p;
  23. p = xdr_inline_decode(xdr, XDR_UNIT);
  24. if (!p)
  25. return false;
  26. *status = *p;
  27. return true;
  28. }
  29. static inline bool
  30. svcxdr_encode_stats(struct xdr_stream *xdr, __be32 status)
  31. {
  32. __be32 *p;
  33. p = xdr_reserve_space(xdr, XDR_UNIT);
  34. if (!p)
  35. return false;
  36. *p = status;
  37. return true;
  38. }
  39. static inline bool
  40. svcxdr_decode_string(struct xdr_stream *xdr, char **data, unsigned int *data_len)
  41. {
  42. __be32 *p;
  43. u32 len;
  44. if (xdr_stream_decode_u32(xdr, &len) < 0)
  45. return false;
  46. if (len > NLM_MAXSTRLEN)
  47. return false;
  48. p = xdr_inline_decode(xdr, len);
  49. if (!p)
  50. return false;
  51. *data_len = len;
  52. *data = (char *)p;
  53. return true;
  54. }
  55. /*
  56. * NLM cookies are defined by specification to be a variable-length
  57. * XDR opaque no longer than 1024 bytes. However, this implementation
  58. * limits their length to 32 bytes, and treats zero-length cookies
  59. * specially.
  60. */
  61. static inline bool
  62. svcxdr_decode_cookie(struct xdr_stream *xdr, struct nlm_cookie *cookie)
  63. {
  64. __be32 *p;
  65. u32 len;
  66. if (xdr_stream_decode_u32(xdr, &len) < 0)
  67. return false;
  68. if (len > NLM_MAXCOOKIELEN)
  69. return false;
  70. if (!len)
  71. goto out_hpux;
  72. p = xdr_inline_decode(xdr, len);
  73. if (!p)
  74. return false;
  75. cookie->len = len;
  76. memcpy(cookie->data, p, len);
  77. return true;
  78. /* apparently HPUX can return empty cookies */
  79. out_hpux:
  80. cookie->len = 4;
  81. memset(cookie->data, 0, 4);
  82. return true;
  83. }
  84. static inline bool
  85. svcxdr_encode_cookie(struct xdr_stream *xdr, const struct nlm_cookie *cookie)
  86. {
  87. __be32 *p;
  88. if (xdr_stream_encode_u32(xdr, cookie->len) < 0)
  89. return false;
  90. p = xdr_reserve_space(xdr, cookie->len);
  91. if (!p)
  92. return false;
  93. memcpy(p, cookie->data, cookie->len);
  94. return true;
  95. }
  96. static inline bool
  97. svcxdr_decode_owner(struct xdr_stream *xdr, struct xdr_netobj *obj)
  98. {
  99. __be32 *p;
  100. u32 len;
  101. if (xdr_stream_decode_u32(xdr, &len) < 0)
  102. return false;
  103. if (len > XDR_MAX_NETOBJ)
  104. return false;
  105. p = xdr_inline_decode(xdr, len);
  106. if (!p)
  107. return false;
  108. obj->len = len;
  109. obj->data = (u8 *)p;
  110. return true;
  111. }
  112. static inline bool
  113. svcxdr_encode_owner(struct xdr_stream *xdr, const struct xdr_netobj *obj)
  114. {
  115. if (obj->len > XDR_MAX_NETOBJ)
  116. return false;
  117. return xdr_stream_encode_opaque(xdr, obj->data, obj->len) > 0;
  118. }
  119. #endif /* _LOCKD_SVCXDR_H_ */