Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __CEPH_DECODE_H
0003 #define __CEPH_DECODE_H
0004 
0005 #include <linux/err.h>
0006 #include <linux/bug.h>
0007 #include <linux/slab.h>
0008 #include <linux/time.h>
0009 #include <asm/unaligned.h>
0010 
0011 #include <linux/ceph/types.h>
0012 
0013 /*
0014  * in all cases,
0015  *   void **p     pointer to position pointer
0016  *   void *end    pointer to end of buffer (last byte + 1)
0017  */
0018 
0019 static inline u64 ceph_decode_64(void **p)
0020 {
0021     u64 v = get_unaligned_le64(*p);
0022     *p += sizeof(u64);
0023     return v;
0024 }
0025 static inline u32 ceph_decode_32(void **p)
0026 {
0027     u32 v = get_unaligned_le32(*p);
0028     *p += sizeof(u32);
0029     return v;
0030 }
0031 static inline u16 ceph_decode_16(void **p)
0032 {
0033     u16 v = get_unaligned_le16(*p);
0034     *p += sizeof(u16);
0035     return v;
0036 }
0037 static inline u8 ceph_decode_8(void **p)
0038 {
0039     u8 v = *(u8 *)*p;
0040     (*p)++;
0041     return v;
0042 }
0043 static inline void ceph_decode_copy(void **p, void *pv, size_t n)
0044 {
0045     memcpy(pv, *p, n);
0046     *p += n;
0047 }
0048 
0049 /*
0050  * bounds check input.
0051  */
0052 static inline bool ceph_has_room(void **p, void *end, size_t n)
0053 {
0054     return end >= *p && n <= end - *p;
0055 }
0056 
0057 #define ceph_decode_need(p, end, n, bad)            \
0058     do {                            \
0059         if (!likely(ceph_has_room(p, end, n)))      \
0060             goto bad;               \
0061     } while (0)
0062 
0063 #define ceph_decode_64_safe(p, end, v, bad)         \
0064     do {                            \
0065         ceph_decode_need(p, end, sizeof(u64), bad); \
0066         v = ceph_decode_64(p);              \
0067     } while (0)
0068 #define ceph_decode_32_safe(p, end, v, bad)         \
0069     do {                            \
0070         ceph_decode_need(p, end, sizeof(u32), bad); \
0071         v = ceph_decode_32(p);              \
0072     } while (0)
0073 #define ceph_decode_16_safe(p, end, v, bad)         \
0074     do {                            \
0075         ceph_decode_need(p, end, sizeof(u16), bad); \
0076         v = ceph_decode_16(p);              \
0077     } while (0)
0078 #define ceph_decode_8_safe(p, end, v, bad)          \
0079     do {                            \
0080         ceph_decode_need(p, end, sizeof(u8), bad);  \
0081         v = ceph_decode_8(p);               \
0082     } while (0)
0083 
0084 #define ceph_decode_copy_safe(p, end, pv, n, bad)       \
0085     do {                            \
0086         ceph_decode_need(p, end, n, bad);       \
0087         ceph_decode_copy(p, pv, n);         \
0088     } while (0)
0089 
0090 /*
0091  * Allocate a buffer big enough to hold the wire-encoded string, and
0092  * decode the string into it.  The resulting string will always be
0093  * terminated with '\0'.  If successful, *p will be advanced
0094  * past the decoded data.  Also, if lenp is not a null pointer, the
0095  * length (not including the terminating '\0') will be recorded in
0096  * *lenp.  Note that a zero-length string is a valid return value.
0097  *
0098  * Returns a pointer to the newly-allocated string buffer, or a
0099  * pointer-coded errno if an error occurs.  Neither *p nor *lenp
0100  * will have been updated if an error is returned.
0101  *
0102  * There are two possible failures:
0103  *   - converting the string would require accessing memory at or
0104  *     beyond the "end" pointer provided (-ERANGE)
0105  *   - memory could not be allocated for the result (-ENOMEM)
0106  */
0107 static inline char *ceph_extract_encoded_string(void **p, void *end,
0108                         size_t *lenp, gfp_t gfp)
0109 {
0110     u32 len;
0111     void *sp = *p;
0112     char *buf;
0113 
0114     ceph_decode_32_safe(&sp, end, len, bad);
0115     if (!ceph_has_room(&sp, end, len))
0116         goto bad;
0117 
0118     buf = kmalloc(len + 1, gfp);
0119     if (!buf)
0120         return ERR_PTR(-ENOMEM);
0121 
0122     if (len)
0123         memcpy(buf, sp, len);
0124     buf[len] = '\0';
0125 
0126     *p = (char *) *p + sizeof (u32) + len;
0127     if (lenp)
0128         *lenp = (size_t) len;
0129 
0130     return buf;
0131 
0132 bad:
0133     return ERR_PTR(-ERANGE);
0134 }
0135 
0136 /*
0137  * skip helpers
0138  */
0139 #define ceph_decode_skip_n(p, end, n, bad)          \
0140     do {                            \
0141         ceph_decode_need(p, end, n, bad);       \
0142                 *p += n;                    \
0143     } while (0)
0144 
0145 #define ceph_decode_skip_64(p, end, bad)            \
0146 ceph_decode_skip_n(p, end, sizeof(u64), bad)
0147 
0148 #define ceph_decode_skip_32(p, end, bad)            \
0149 ceph_decode_skip_n(p, end, sizeof(u32), bad)
0150 
0151 #define ceph_decode_skip_16(p, end, bad)            \
0152 ceph_decode_skip_n(p, end, sizeof(u16), bad)
0153 
0154 #define ceph_decode_skip_8(p, end, bad)             \
0155 ceph_decode_skip_n(p, end, sizeof(u8), bad)
0156 
0157 #define ceph_decode_skip_string(p, end, bad)            \
0158     do {                            \
0159         u32 len;                    \
0160                                 \
0161         ceph_decode_32_safe(p, end, len, bad);      \
0162         ceph_decode_skip_n(p, end, len, bad);       \
0163     } while (0)
0164 
0165 #define ceph_decode_skip_set(p, end, type, bad)         \
0166     do {                            \
0167         u32 len;                    \
0168                                 \
0169         ceph_decode_32_safe(p, end, len, bad);      \
0170         while (len--)                   \
0171             ceph_decode_skip_##type(p, end, bad);   \
0172     } while (0)
0173 
0174 #define ceph_decode_skip_map(p, end, ktype, vtype, bad)     \
0175     do {                            \
0176         u32 len;                    \
0177                                 \
0178         ceph_decode_32_safe(p, end, len, bad);      \
0179         while (len--) {                 \
0180             ceph_decode_skip_##ktype(p, end, bad);  \
0181             ceph_decode_skip_##vtype(p, end, bad);  \
0182         }                       \
0183     } while (0)
0184 
0185 #define ceph_decode_skip_map_of_map(p, end, ktype1, ktype2, vtype2, bad) \
0186     do {                            \
0187         u32 len;                    \
0188                                 \
0189         ceph_decode_32_safe(p, end, len, bad);      \
0190         while (len--) {                 \
0191             ceph_decode_skip_##ktype1(p, end, bad); \
0192             ceph_decode_skip_map(p, end, ktype2, vtype2, bad); \
0193         }                       \
0194     } while (0)
0195 
0196 /*
0197  * struct ceph_timespec <-> struct timespec64
0198  */
0199 static inline void ceph_decode_timespec64(struct timespec64 *ts,
0200                       const struct ceph_timespec *tv)
0201 {
0202     /*
0203      * This will still overflow in year 2106.  We could extend
0204      * the protocol to steal two more bits from tv_nsec to
0205      * add three more 136 year epochs after that the way ext4
0206      * does if necessary.
0207      */
0208     ts->tv_sec = (time64_t)le32_to_cpu(tv->tv_sec);
0209     ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
0210 }
0211 static inline void ceph_encode_timespec64(struct ceph_timespec *tv,
0212                       const struct timespec64 *ts)
0213 {
0214     tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
0215     tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
0216 }
0217 
0218 /*
0219  * sockaddr_storage <-> ceph_sockaddr
0220  */
0221 #define CEPH_ENTITY_ADDR_TYPE_NONE  0
0222 #define CEPH_ENTITY_ADDR_TYPE_LEGACY    __cpu_to_le32(1)
0223 #define CEPH_ENTITY_ADDR_TYPE_MSGR2 __cpu_to_le32(2)
0224 #define CEPH_ENTITY_ADDR_TYPE_ANY   __cpu_to_le32(3)
0225 
0226 static inline void ceph_encode_banner_addr(struct ceph_entity_addr *a)
0227 {
0228     __be16 ss_family = htons(a->in_addr.ss_family);
0229     a->in_addr.ss_family = *(__u16 *)&ss_family;
0230 
0231     /* Banner addresses require TYPE_NONE */
0232     a->type = CEPH_ENTITY_ADDR_TYPE_NONE;
0233 }
0234 static inline void ceph_decode_banner_addr(struct ceph_entity_addr *a)
0235 {
0236     __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
0237     a->in_addr.ss_family = ntohs(ss_family);
0238     WARN_ON(a->in_addr.ss_family == 512);
0239     a->type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
0240 }
0241 
0242 extern int ceph_decode_entity_addr(void **p, void *end,
0243                    struct ceph_entity_addr *addr);
0244 int ceph_decode_entity_addrvec(void **p, void *end, bool msgr2,
0245                    struct ceph_entity_addr *addr);
0246 
0247 int ceph_entity_addr_encoding_len(const struct ceph_entity_addr *addr);
0248 void ceph_encode_entity_addr(void **p, const struct ceph_entity_addr *addr);
0249 
0250 /*
0251  * encoders
0252  */
0253 static inline void ceph_encode_64(void **p, u64 v)
0254 {
0255     put_unaligned_le64(v, (__le64 *)*p);
0256     *p += sizeof(u64);
0257 }
0258 static inline void ceph_encode_32(void **p, u32 v)
0259 {
0260     put_unaligned_le32(v, (__le32 *)*p);
0261     *p += sizeof(u32);
0262 }
0263 static inline void ceph_encode_16(void **p, u16 v)
0264 {
0265     put_unaligned_le16(v, (__le16 *)*p);
0266     *p += sizeof(u16);
0267 }
0268 static inline void ceph_encode_8(void **p, u8 v)
0269 {
0270     *(u8 *)*p = v;
0271     (*p)++;
0272 }
0273 static inline void ceph_encode_copy(void **p, const void *s, int len)
0274 {
0275     memcpy(*p, s, len);
0276     *p += len;
0277 }
0278 
0279 /*
0280  * filepath, string encoders
0281  */
0282 static inline void ceph_encode_filepath(void **p, void *end,
0283                     u64 ino, const char *path)
0284 {
0285     u32 len = path ? strlen(path) : 0;
0286     BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
0287     ceph_encode_8(p, 1);
0288     ceph_encode_64(p, ino);
0289     ceph_encode_32(p, len);
0290     if (len)
0291         memcpy(*p, path, len);
0292     *p += len;
0293 }
0294 
0295 static inline void ceph_encode_string(void **p, void *end,
0296                       const char *s, u32 len)
0297 {
0298     BUG_ON(*p + sizeof(len) + len > end);
0299     ceph_encode_32(p, len);
0300     if (len)
0301         memcpy(*p, s, len);
0302     *p += len;
0303 }
0304 
0305 /*
0306  * version and length starting block encoders/decoders
0307  */
0308 
0309 /* current code version (u8) + compat code version (u8) + len of struct (u32) */
0310 #define CEPH_ENCODING_START_BLK_LEN 6
0311 
0312 /**
0313  * ceph_start_encoding - start encoding block
0314  * @struct_v: current (code) version of the encoding
0315  * @struct_compat: oldest code version that can decode it
0316  * @struct_len: length of struct encoding
0317  */
0318 static inline void ceph_start_encoding(void **p, u8 struct_v, u8 struct_compat,
0319                        u32 struct_len)
0320 {
0321     ceph_encode_8(p, struct_v);
0322     ceph_encode_8(p, struct_compat);
0323     ceph_encode_32(p, struct_len);
0324 }
0325 
0326 /**
0327  * ceph_start_decoding - start decoding block
0328  * @v: current version of the encoding that the code supports
0329  * @name: name of the struct (free-form)
0330  * @struct_v: out param for the encoding version
0331  * @struct_len: out param for the length of struct encoding
0332  *
0333  * Validates the length of struct encoding, so unsafe ceph_decode_*
0334  * variants can be used for decoding.
0335  */
0336 static inline int ceph_start_decoding(void **p, void *end, u8 v,
0337                       const char *name, u8 *struct_v,
0338                       u32 *struct_len)
0339 {
0340     u8 struct_compat;
0341 
0342     ceph_decode_need(p, end, CEPH_ENCODING_START_BLK_LEN, bad);
0343     *struct_v = ceph_decode_8(p);
0344     struct_compat = ceph_decode_8(p);
0345     if (v < struct_compat) {
0346         pr_warn("got struct_v %d struct_compat %d > %d of %s\n",
0347             *struct_v, struct_compat, v, name);
0348         return -EINVAL;
0349     }
0350 
0351     *struct_len = ceph_decode_32(p);
0352     ceph_decode_need(p, end, *struct_len, bad);
0353     return 0;
0354 
0355 bad:
0356     return -ERANGE;
0357 }
0358 
0359 #define ceph_encode_need(p, end, n, bad)            \
0360     do {                            \
0361         if (!likely(ceph_has_room(p, end, n)))      \
0362             goto bad;               \
0363     } while (0)
0364 
0365 #define ceph_encode_64_safe(p, end, v, bad)         \
0366     do {                            \
0367         ceph_encode_need(p, end, sizeof(u64), bad); \
0368         ceph_encode_64(p, v);               \
0369     } while (0)
0370 #define ceph_encode_32_safe(p, end, v, bad)         \
0371     do {                            \
0372         ceph_encode_need(p, end, sizeof(u32), bad); \
0373         ceph_encode_32(p, v);               \
0374     } while (0)
0375 #define ceph_encode_16_safe(p, end, v, bad)         \
0376     do {                            \
0377         ceph_encode_need(p, end, sizeof(u16), bad); \
0378         ceph_encode_16(p, v);               \
0379     } while (0)
0380 #define ceph_encode_8_safe(p, end, v, bad)          \
0381     do {                            \
0382         ceph_encode_need(p, end, sizeof(u8), bad);  \
0383         ceph_encode_8(p, v);                \
0384     } while (0)
0385 
0386 #define ceph_encode_copy_safe(p, end, pv, n, bad)       \
0387     do {                            \
0388         ceph_encode_need(p, end, n, bad);       \
0389         ceph_encode_copy(p, pv, n);         \
0390     } while (0)
0391 #define ceph_encode_string_safe(p, end, s, n, bad)      \
0392     do {                            \
0393         ceph_encode_need(p, end, n, bad);       \
0394         ceph_encode_string(p, end, s, n);       \
0395     } while (0)
0396 
0397 
0398 #endif