Merge tag 'sh-for-5.9' of git://git.libc.org/linux-sh
[linux/fpc-iii.git] / net / ceph / decode.c
blobeea529595a7a1128e774262b114d9f5491a10f49
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/decode.h>
5 static int
6 ceph_decode_entity_addr_versioned(void **p, void *end,
7 struct ceph_entity_addr *addr)
9 int ret;
10 u8 struct_v;
11 u32 struct_len, addr_len;
12 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;
19 ret = -EINVAL;
20 struct_end = *p + struct_len;
22 ceph_decode_copy_safe(p, end, &addr->type, sizeof(addr->type), bad);
24 ceph_decode_copy_safe(p, end, &addr->nonce, sizeof(addr->nonce), bad);
26 ceph_decode_32_safe(p, end, addr_len, bad);
27 if (addr_len > sizeof(addr->in_addr))
28 goto bad;
30 memset(&addr->in_addr, 0, sizeof(addr->in_addr));
31 if (addr_len) {
32 ceph_decode_copy_safe(p, end, &addr->in_addr, addr_len, bad);
34 addr->in_addr.ss_family =
35 le16_to_cpu((__force __le16)addr->in_addr.ss_family);
38 /* Advance past anything the client doesn't yet understand */
39 *p = struct_end;
40 ret = 0;
41 bad:
42 return ret;
45 static int
46 ceph_decode_entity_addr_legacy(void **p, void *end,
47 struct ceph_entity_addr *addr)
49 int ret = -EINVAL;
51 /* Skip rest of type field */
52 ceph_decode_skip_n(p, end, 3, bad);
55 * Clients that don't support ADDR2 always send TYPE_NONE, change it
56 * to TYPE_LEGACY for forward compatibility.
58 addr->type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
59 ceph_decode_copy_safe(p, end, &addr->nonce, sizeof(addr->nonce), bad);
60 memset(&addr->in_addr, 0, sizeof(addr->in_addr));
61 ceph_decode_copy_safe(p, end, &addr->in_addr,
62 sizeof(addr->in_addr), bad);
63 addr->in_addr.ss_family =
64 be16_to_cpu((__force __be16)addr->in_addr.ss_family);
65 ret = 0;
66 bad:
67 return ret;
70 int
71 ceph_decode_entity_addr(void **p, void *end, struct ceph_entity_addr *addr)
73 u8 marker;
75 ceph_decode_8_safe(p, end, marker, bad);
76 if (marker == 1)
77 return ceph_decode_entity_addr_versioned(p, end, addr);
78 else if (marker == 0)
79 return ceph_decode_entity_addr_legacy(p, end, addr);
80 bad:
81 return -EINVAL;
83 EXPORT_SYMBOL(ceph_decode_entity_addr);