decode.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/inet.h>
  4. #include <linux/ceph/decode.h>
  5. #include <linux/ceph/messenger.h> /* for ceph_pr_addr() */
  6. static int
  7. ceph_decode_entity_addr_versioned(void **p, void *end,
  8. struct ceph_entity_addr *addr)
  9. {
  10. int ret;
  11. u8 struct_v;
  12. u32 struct_len, addr_len;
  13. void *struct_end;
  14. ret = ceph_start_decoding(p, end, 1, "entity_addr_t", &struct_v,
  15. &struct_len);
  16. if (ret)
  17. goto bad;
  18. ret = -EINVAL;
  19. struct_end = *p + struct_len;
  20. ceph_decode_copy_safe(p, end, &addr->type, sizeof(addr->type), bad);
  21. ceph_decode_copy_safe(p, end, &addr->nonce, sizeof(addr->nonce), bad);
  22. ceph_decode_32_safe(p, end, addr_len, bad);
  23. if (addr_len > sizeof(addr->in_addr))
  24. goto bad;
  25. memset(&addr->in_addr, 0, sizeof(addr->in_addr));
  26. if (addr_len) {
  27. ceph_decode_copy_safe(p, end, &addr->in_addr, addr_len, bad);
  28. addr->in_addr.ss_family =
  29. le16_to_cpu((__force __le16)addr->in_addr.ss_family);
  30. }
  31. /* Advance past anything the client doesn't yet understand */
  32. *p = struct_end;
  33. ret = 0;
  34. bad:
  35. return ret;
  36. }
  37. static int
  38. ceph_decode_entity_addr_legacy(void **p, void *end,
  39. struct ceph_entity_addr *addr)
  40. {
  41. int ret = -EINVAL;
  42. /* Skip rest of type field */
  43. ceph_decode_skip_n(p, end, 3, bad);
  44. /*
  45. * Clients that don't support ADDR2 always send TYPE_NONE, change it
  46. * to TYPE_LEGACY for forward compatibility.
  47. */
  48. addr->type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
  49. ceph_decode_copy_safe(p, end, &addr->nonce, sizeof(addr->nonce), bad);
  50. memset(&addr->in_addr, 0, sizeof(addr->in_addr));
  51. ceph_decode_copy_safe(p, end, &addr->in_addr,
  52. sizeof(addr->in_addr), bad);
  53. addr->in_addr.ss_family =
  54. be16_to_cpu((__force __be16)addr->in_addr.ss_family);
  55. ret = 0;
  56. bad:
  57. return ret;
  58. }
  59. int
  60. ceph_decode_entity_addr(void **p, void *end, struct ceph_entity_addr *addr)
  61. {
  62. u8 marker;
  63. ceph_decode_8_safe(p, end, marker, bad);
  64. if (marker == 1)
  65. return ceph_decode_entity_addr_versioned(p, end, addr);
  66. else if (marker == 0)
  67. return ceph_decode_entity_addr_legacy(p, end, addr);
  68. bad:
  69. return -EINVAL;
  70. }
  71. EXPORT_SYMBOL(ceph_decode_entity_addr);
  72. /*
  73. * Return addr of desired type (MSGR2 or LEGACY) or error.
  74. * Make sure there is only one match.
  75. *
  76. * Assume encoding with MSG_ADDR2.
  77. */
  78. int ceph_decode_entity_addrvec(void **p, void *end, bool msgr2,
  79. struct ceph_entity_addr *addr)
  80. {
  81. __le32 my_type = msgr2 ? CEPH_ENTITY_ADDR_TYPE_MSGR2 :
  82. CEPH_ENTITY_ADDR_TYPE_LEGACY;
  83. struct ceph_entity_addr tmp_addr;
  84. int addr_cnt;
  85. bool found;
  86. u8 marker;
  87. int ret;
  88. int i;
  89. ceph_decode_8_safe(p, end, marker, e_inval);
  90. if (marker != 2) {
  91. pr_err("bad addrvec marker %d\n", marker);
  92. return -EINVAL;
  93. }
  94. ceph_decode_32_safe(p, end, addr_cnt, e_inval);
  95. dout("%s addr_cnt %d\n", __func__, addr_cnt);
  96. found = false;
  97. for (i = 0; i < addr_cnt; i++) {
  98. ret = ceph_decode_entity_addr(p, end, &tmp_addr);
  99. if (ret)
  100. return ret;
  101. dout("%s i %d addr %s\n", __func__, i, ceph_pr_addr(&tmp_addr));
  102. if (tmp_addr.type == my_type) {
  103. if (found) {
  104. pr_err("another match of type %d in addrvec\n",
  105. le32_to_cpu(my_type));
  106. return -EINVAL;
  107. }
  108. memcpy(addr, &tmp_addr, sizeof(*addr));
  109. found = true;
  110. }
  111. }
  112. if (found)
  113. return 0;
  114. if (!addr_cnt)
  115. return 0; /* normal -- e.g. unused OSD id/slot */
  116. if (addr_cnt == 1 && !memchr_inv(&tmp_addr, 0, sizeof(tmp_addr)))
  117. return 0; /* weird but effectively the same as !addr_cnt */
  118. pr_err("no match of type %d in addrvec\n", le32_to_cpu(my_type));
  119. return -ENOENT;
  120. e_inval:
  121. return -EINVAL;
  122. }
  123. EXPORT_SYMBOL(ceph_decode_entity_addrvec);
  124. static int get_sockaddr_encoding_len(sa_family_t family)
  125. {
  126. union {
  127. struct sockaddr sa;
  128. struct sockaddr_in sin;
  129. struct sockaddr_in6 sin6;
  130. } u;
  131. switch (family) {
  132. case AF_INET:
  133. return sizeof(u.sin);
  134. case AF_INET6:
  135. return sizeof(u.sin6);
  136. default:
  137. return sizeof(u);
  138. }
  139. }
  140. int ceph_entity_addr_encoding_len(const struct ceph_entity_addr *addr)
  141. {
  142. sa_family_t family = get_unaligned(&addr->in_addr.ss_family);
  143. int addr_len = get_sockaddr_encoding_len(family);
  144. return 1 + CEPH_ENCODING_START_BLK_LEN + 4 + 4 + 4 + addr_len;
  145. }
  146. void ceph_encode_entity_addr(void **p, const struct ceph_entity_addr *addr)
  147. {
  148. sa_family_t family = get_unaligned(&addr->in_addr.ss_family);
  149. int addr_len = get_sockaddr_encoding_len(family);
  150. ceph_encode_8(p, 1); /* marker */
  151. ceph_start_encoding(p, 1, 1, sizeof(addr->type) +
  152. sizeof(addr->nonce) +
  153. sizeof(u32) + addr_len);
  154. ceph_encode_copy(p, &addr->type, sizeof(addr->type));
  155. ceph_encode_copy(p, &addr->nonce, sizeof(addr->nonce));
  156. ceph_encode_32(p, addr_len);
  157. ceph_encode_16(p, family);
  158. ceph_encode_copy(p, addr->in_addr.__data, addr_len - sizeof(family));
  159. }