Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 6 | #include <osmocom/core/msgb.h> |
Harald Welte | dec201a | 2020-12-08 20:32:12 +0100 | [diff] [blame] | 7 | #include <osmocom/core/byteswap.h> |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 8 | #include <osmocom/core/bit16gen.h> |
| 9 | #include <osmocom/core/bit32gen.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 10 | |
Harald Welte | 9325d86 | 2017-10-16 15:32:43 +0200 | [diff] [blame] | 11 | /*! \defgroup tlv TLV parser |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 12 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 13 | * \file tlv.h */ |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 14 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 15 | /* Terminology / wording |
| 16 | tag length value (in bits) |
| 17 | |
| 18 | V - - 8 |
| 19 | LV - 8 N * 8 |
| 20 | TLV 8 8 N * 8 |
| 21 | TL16V 8 16 N * 8 |
| 22 | TLV16 8 8 N * 16 |
| 23 | TvLV 8 8/16 N * 8 |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 24 | vTvLV 8/16 8/16 N * 8 |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 25 | T16LV 16 8 N * 8 |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 28 | /*! gross length of a LV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 29 | #define LV_GROSS_LEN(x) (x+1) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 30 | /*! gross length of a TLV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 31 | #define TLV_GROSS_LEN(x) (x+2) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 32 | /*! gross length of a TLV16 type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 33 | #define TLV16_GROSS_LEN(x) ((2*x)+2) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 34 | /*! gross length of a TL16V type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | #define TL16V_GROSS_LEN(x) (x+3) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | /*! gross length of a L16TV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | #define L16TV_GROSS_LEN(x) (x+3) |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 38 | /*! gross length of a T16LV type field */ |
| 39 | #define T16LV_GROSS_LEN(x) (x+3) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 41 | /*! maximum length of TLV of one byte length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | #define TVLV_MAX_ONEBYTE 0x7f |
| 43 | |
Harald Welte | 30a9294 | 2020-12-04 14:09:22 +0100 | [diff] [blame] | 44 | /*! error return codes of various TLV parser functions */ |
| 45 | enum osmo_tlv_parser_error { |
| 46 | OSMO_TLVP_ERR_OFS_BEYOND_BUFFER = -1, |
| 47 | OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER = -2, |
| 48 | OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE = -3, |
| 49 | |
| 50 | OSMO_TLVP_ERR_MAND_IE_MISSING = -50, |
| 51 | OSMO_TLVP_ERR_IE_TOO_SHORT = -51, |
| 52 | }; |
| 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! gross length of a TVLV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 55 | static inline uint16_t TVLV_GROSS_LEN(uint16_t len) |
| 56 | { |
| 57 | if (len <= TVLV_MAX_ONEBYTE) |
| 58 | return TLV_GROSS_LEN(len); |
| 59 | else |
| 60 | return TL16V_GROSS_LEN(len); |
| 61 | } |
| 62 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 63 | /*! gross length of vTvL header (tag+len) */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 64 | static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 65 | { |
| 66 | uint16_t ret = 2; |
| 67 | |
| 68 | if (tag > TVLV_MAX_ONEBYTE) |
| 69 | ret++; |
| 70 | |
| 71 | if (len > TVLV_MAX_ONEBYTE) |
| 72 | ret++; |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | /*! gross length of vTvLV (tag+len+val) */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 78 | static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 79 | { |
| 80 | uint16_t ret; |
| 81 | |
| 82 | if (len <= TVLV_MAX_ONEBYTE) |
Vadim Yanitskiy | ac9e2d8 | 2017-05-14 20:52:46 +0300 | [diff] [blame] | 83 | ret = TLV_GROSS_LEN(len); |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 84 | else |
Vadim Yanitskiy | ac9e2d8 | 2017-05-14 20:52:46 +0300 | [diff] [blame] | 85 | ret = TL16V_GROSS_LEN(len); |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 86 | |
| 87 | if (tag > TVLV_MAX_ONEBYTE) |
| 88 | ret += 1; |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 93 | /* TLV generation */ |
| 94 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 95 | /*! put (append) a LV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 96 | static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, |
| 97 | const uint8_t *val) |
| 98 | { |
| 99 | *buf++ = len; |
| 100 | memcpy(buf, val, len); |
| 101 | return buf + len; |
| 102 | } |
| 103 | |
Neels Hofmeyr | e750980 | 2017-11-16 23:34:33 +0100 | [diff] [blame] | 104 | /*! Append a TLV field, a Tag-Length-Value field. |
| 105 | * \param[out] buf Location in a buffer to append TLV at. |
| 106 | * \param[in] tag Tag id to write. |
| 107 | * \param[in] len Length field to write and amount of bytes to append. |
| 108 | * \param[in] val Pointer to data to append, or NULL to append zero data. |
| 109 | * Always append tag and length. Append \a len bytes read from \a val. If val is NULL, append \a len zero |
| 110 | * bytes instead. If \a len is zero, do not append any data apart from tag and length. */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 111 | static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 112 | const uint8_t *val) |
| 113 | { |
| 114 | *buf++ = tag; |
| 115 | *buf++ = len; |
Neels Hofmeyr | e750980 | 2017-11-16 23:34:33 +0100 | [diff] [blame] | 116 | if (len) { |
| 117 | if (val) |
| 118 | memcpy(buf, val, len); |
| 119 | else |
| 120 | memset(buf, 0, len); |
| 121 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 122 | return buf + len; |
| 123 | } |
| 124 | |
Neels Hofmeyr | c71f771 | 2020-05-29 23:58:01 +0200 | [diff] [blame] | 125 | /*! put (append) a TL field (a TLV field but omitting the value part). */ |
| 126 | static inline uint8_t *tl_put(uint8_t *buf, uint8_t tag, uint8_t len) |
| 127 | { |
| 128 | *buf++ = tag; |
| 129 | *buf++ = len; |
| 130 | return buf; |
| 131 | } |
| 132 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 133 | /*! put (append) a TLV16 field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 134 | static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 135 | const uint16_t *val) |
| 136 | { |
| 137 | *buf++ = tag; |
| 138 | *buf++ = len; |
| 139 | memcpy(buf, val, len*2); |
| 140 | return buf + len*2; |
| 141 | } |
| 142 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 143 | /*! put (append) a TL16V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 144 | static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 145 | const uint8_t *val) |
| 146 | { |
| 147 | *buf++ = tag; |
| 148 | *buf++ = len >> 8; |
| 149 | *buf++ = len & 0xff; |
| 150 | memcpy(buf, val, len); |
| 151 | return buf + len*2; |
| 152 | } |
| 153 | |
Neels Hofmeyr | c71f771 | 2020-05-29 23:58:01 +0200 | [diff] [blame] | 154 | /*! put (append) a TL16 field. */ |
| 155 | static inline uint8_t *tl16_put(uint8_t *buf, uint8_t tag, uint16_t len) |
| 156 | { |
| 157 | *buf++ = tag; |
| 158 | *buf++ = len >> 8; |
| 159 | *buf++ = len & 0xff; |
| 160 | return buf; |
| 161 | } |
| 162 | |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 163 | /*! put (append) a TL16V field */ |
| 164 | static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len, |
| 165 | const uint8_t *val) |
| 166 | { |
| 167 | *buf++ = tag >> 8; |
| 168 | *buf++ = tag & 0xff; |
| 169 | *buf++ = len; |
| 170 | memcpy(buf, val, len); |
| 171 | return buf + len + 2; |
| 172 | } |
| 173 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 174 | /*! put (append) a TvLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 175 | static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 176 | const uint8_t *val) |
| 177 | { |
| 178 | uint8_t *ret; |
| 179 | |
| 180 | if (len <= TVLV_MAX_ONEBYTE) { |
| 181 | ret = tlv_put(buf, tag, len, val); |
| 182 | buf[1] |= 0x80; |
| 183 | } else |
| 184 | ret = tl16v_put(buf, tag, len, val); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
Neels Hofmeyr | c71f771 | 2020-05-29 23:58:01 +0200 | [diff] [blame] | 189 | /*! put (append) a TvL field (a TvLV with variable-size length, where the value part's length is already known, but will |
| 190 | * be put() later). |
| 191 | * \returns pointer to the value's start position. |
| 192 | */ |
| 193 | static inline uint8_t *tvl_put(uint8_t *buf, uint8_t tag, uint16_t len) |
| 194 | { |
| 195 | uint8_t *ret; |
| 196 | |
| 197 | if (len <= TVLV_MAX_ONEBYTE) { |
| 198 | ret = tl_put(buf, tag, len); |
| 199 | buf[1] |= 0x80; |
| 200 | } else |
| 201 | ret = tl16_put(buf, tag, len); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 206 | /*! put (append) a variable-length tag or variable-length length * */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 207 | static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag) |
| 208 | { |
| 209 | if (tag > TVLV_MAX_ONEBYTE) { |
| 210 | /* two-byte TAG */ |
| 211 | *buf++ = 0x80 | (tag >> 8); |
| 212 | *buf++ = (tag & 0xff); |
| 213 | } else |
| 214 | *buf++ = tag; |
| 215 | |
| 216 | return buf; |
| 217 | } |
| 218 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 219 | /* put (append) vTvL (GAN) field (tag + length)*/ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 220 | static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len) |
| 221 | { |
| 222 | uint8_t *ret; |
| 223 | |
| 224 | ret = vt_gan_put(buf, tag); |
| 225 | return vt_gan_put(ret, len); |
| 226 | } |
| 227 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 228 | /* put (append) vTvLV (GAN) field (tag + length + val) */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 229 | static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len, |
| 230 | const uint8_t *val) |
| 231 | { |
| 232 | uint8_t *ret; |
| 233 | |
| 234 | ret = vtvl_gan_put(buf, tag, len ); |
| 235 | |
| 236 | memcpy(ret, val, len); |
| 237 | ret = buf + len; |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 242 | /*! put (append) a TLV16 field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 243 | static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) |
| 244 | { |
| 245 | uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 246 | return tlv16_put(buf, tag, len, val); |
| 247 | } |
| 248 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 249 | /*! put (append) a TL16V field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 250 | static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 251 | const uint8_t *val) |
| 252 | { |
| 253 | uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 254 | return tl16v_put(buf, tag, len, val); |
| 255 | } |
| 256 | |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 257 | static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val) |
| 258 | { |
| 259 | uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len)); |
| 260 | return t16lv_put(buf, tag, len, val); |
| 261 | } |
| 262 | |
Neels Hofmeyr | c71f771 | 2020-05-29 23:58:01 +0200 | [diff] [blame] | 263 | /*! put (append) a TvL field to \ref msgb, i.e. a TvLV with variable-size length, where the value's length is already |
| 264 | * known, but will be put() later. The value section is not yet reserved, only tag and variable-length are put in the |
| 265 | * msgb. |
| 266 | * \returns pointer to the value's start position and end of the msgb. |
| 267 | */ |
| 268 | static inline uint8_t *msgb_tvl_put(struct msgb *msg, uint8_t tag, uint16_t len) |
| 269 | { |
| 270 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 271 | return tvl_put(buf, tag, len); |
| 272 | } |
| 273 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 274 | /*! put (append) a TvLV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 275 | static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 276 | const uint8_t *val) |
| 277 | { |
| 278 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 279 | return tvlv_put(buf, tag, len, val); |
| 280 | } |
| 281 | |
Harald Welte | dec201a | 2020-12-08 20:32:12 +0100 | [diff] [blame] | 282 | /*! put (append) a TvLV field containing a big-endian 16bit value to msgb. */ |
| 283 | static inline uint8_t *msgb_tvlv_put_16be(struct msgb *msg, uint8_t tag, uint16_t val) |
| 284 | { |
| 285 | uint16_t val_be = osmo_htons(val); |
| 286 | return msgb_tvlv_put(msg, tag, 2, (const uint8_t *)&val_be); |
| 287 | } |
| 288 | |
| 289 | /*! put (append) a TvLV field containing a big-endian 16bit value to msgb. */ |
| 290 | static inline uint8_t *msgb_tvlv_put_32be(struct msgb *msg, uint8_t tag, uint32_t val) |
| 291 | { |
| 292 | uint32_t val_be = osmo_htonl(val); |
| 293 | return msgb_tvlv_put(msg, tag, 4, (const uint8_t *)&val_be); |
| 294 | } |
| 295 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 296 | /*! put (append) a vTvLV field to \ref msgb */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 297 | static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag, |
| 298 | uint16_t len, const uint8_t *val) |
| 299 | { |
| 300 | uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 301 | return vtvlv_gan_put(buf, tag, len, val); |
| 302 | } |
| 303 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 304 | /*! put (append) a L16TV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 305 | static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, |
| 306 | const uint8_t *val) |
| 307 | { |
| 308 | uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len)); |
| 309 | |
| 310 | *buf++ = len >> 8; |
| 311 | *buf++ = len & 0xff; |
| 312 | *buf++ = tag; |
| 313 | memcpy(buf, val, len); |
| 314 | return buf + len; |
| 315 | } |
| 316 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 317 | /*! put (append) a V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 318 | static inline uint8_t *v_put(uint8_t *buf, uint8_t val) |
| 319 | { |
| 320 | *buf++ = val; |
| 321 | return buf; |
| 322 | } |
| 323 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 324 | /*! put (append) a TV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 325 | static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, |
| 326 | uint8_t val) |
| 327 | { |
| 328 | *buf++ = tag; |
| 329 | *buf++ = val; |
| 330 | return buf; |
| 331 | } |
| 332 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 333 | /*! put (append) a TVfixed field */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 334 | static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, |
| 335 | unsigned int len, const uint8_t *val) |
| 336 | { |
| 337 | *buf++ = tag; |
| 338 | memcpy(buf, val, len); |
| 339 | return buf + len; |
| 340 | } |
| 341 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 342 | /*! put (append) a TV16 field |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 343 | * \param[in,out] buf data buffer |
| 344 | * \param[in] tag Tag value |
| 345 | * \param[in] val Value (in host byte order!) |
| 346 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 347 | static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, |
| 348 | uint16_t val) |
| 349 | { |
| 350 | *buf++ = tag; |
| 351 | *buf++ = val >> 8; |
| 352 | *buf++ = val & 0xff; |
| 353 | return buf; |
| 354 | } |
| 355 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 356 | /*! put (append) a LV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 357 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 358 | static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) |
| 359 | { |
| 360 | uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); |
| 361 | return lv_put(buf, len, val); |
| 362 | } |
| 363 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 364 | /*! put (append) a TLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 365 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 366 | static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 367 | { |
| 368 | uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 369 | return tlv_put(buf, tag, len, val); |
| 370 | } |
| 371 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 372 | /*! put (append) a TV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 373 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 374 | static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) |
| 375 | { |
| 376 | uint8_t *buf = msgb_put(msg, 2); |
| 377 | return tv_put(buf, tag, val); |
| 378 | } |
| 379 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 380 | /*! put (append) a TVfixed field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 381 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 382 | static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, |
| 383 | unsigned int len, const uint8_t *val) |
| 384 | { |
| 385 | uint8_t *buf = msgb_put(msg, 1+len); |
| 386 | return tv_fixed_put(buf, tag, len, val); |
| 387 | } |
| 388 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 389 | /*! put (append) a V field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 390 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 391 | static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) |
| 392 | { |
| 393 | uint8_t *buf = msgb_put(msg, 1); |
| 394 | return v_put(buf, val); |
| 395 | } |
| 396 | |
Max | d82070c | 2018-12-03 15:33:52 +0100 | [diff] [blame] | 397 | /*! put (append) a TL fields to a \ref msgb |
| 398 | * \returns pointer to the length field so it can be updated after adding new information under specified tag */ |
| 399 | static inline uint8_t *msgb_tl_put(struct msgb *msg, uint8_t tag) |
| 400 | { |
| 401 | uint8_t *len = msgb_v_put(msg, tag); |
| 402 | |
| 403 | /* reserve space for length, len points to this reserved space already */ |
| 404 | msgb_v_put(msg, 0); |
| 405 | |
| 406 | return len; |
| 407 | } |
| 408 | |
Vadim Yanitskiy | 53aff13 | 2021-02-06 16:50:34 +0100 | [diff] [blame] | 409 | /*! put (append) a TV16 field (network order) to the given msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 410 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 411 | static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) |
| 412 | { |
| 413 | uint8_t *buf = msgb_put(msg, 3); |
| 414 | return tv16_put(buf, tag, val); |
| 415 | } |
| 416 | |
Vadim Yanitskiy | 53aff13 | 2021-02-06 16:50:34 +0100 | [diff] [blame] | 417 | /*! put (append) a TV32 field (network order) to the given msgb |
Vadim Yanitskiy | d397a53 | 2021-02-06 16:46:03 +0100 | [diff] [blame] | 418 | * \returns pointer to first byte after newly-put information */ |
| 419 | static inline uint8_t *msgb_tv32_put(struct msgb *msg, uint8_t tag, uint32_t val) |
| 420 | { |
| 421 | uint8_t *buf = msgb_put(msg, 1 + 4); |
| 422 | *buf++ = tag; |
| 423 | osmo_store32be(val, buf); |
| 424 | return msg->tail; |
| 425 | } |
| 426 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 427 | /*! push (prepend) a TLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 428 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 429 | static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 430 | { |
| 431 | uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 432 | tlv_put(buf, tag, len, val); |
| 433 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Max | 84fb5bb | 2018-11-08 13:23:59 +0100 | [diff] [blame] | 436 | /*! push 1-byte tagged value */ |
| 437 | static inline uint8_t *msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 438 | { |
| 439 | return msgb_tlv_push(msg, tag, 1, &val); |
| 440 | } |
| 441 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 442 | /*! push (prepend) a TV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 443 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 444 | static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 445 | { |
| 446 | uint8_t *buf = msgb_push(msg, 2); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 447 | tv_put(buf, tag, val); |
| 448 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 451 | /*! push (prepend) a TV16 field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 452 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 453 | static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) |
| 454 | { |
| 455 | uint8_t *buf = msgb_push(msg, 3); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 456 | tv16_put(buf, tag, val); |
| 457 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 460 | /*! push (prepend) a TvLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 461 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 462 | static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, |
| 463 | const uint8_t *val) |
| 464 | { |
| 465 | uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 466 | tvlv_put(buf, tag, len, val); |
| 467 | return buf; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 470 | /* push (prepend) a vTvL header to a \ref msgb |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 471 | */ |
| 472 | static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag, |
| 473 | uint16_t len) |
| 474 | { |
| 475 | uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len)); |
| 476 | vtvl_gan_put(buf, tag, len); |
| 477 | return buf; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag, |
| 482 | uint16_t len, const uint8_t *val) |
| 483 | { |
| 484 | uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 485 | vtvlv_gan_put(buf, tag, len, val); |
| 486 | return buf; |
| 487 | } |
| 488 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 489 | /* TLV parsing */ |
| 490 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 491 | /*! Entry in a TLV parser array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 492 | struct tlv_p_entry { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 493 | uint16_t len; /*!< length */ |
| 494 | const uint8_t *val; /*!< pointer to value */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 495 | }; |
| 496 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 497 | /*! TLV type */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 498 | enum tlv_type { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 499 | TLV_TYPE_NONE, /*!< no type */ |
| 500 | TLV_TYPE_FIXED, /*!< fixed-length value-only */ |
| 501 | TLV_TYPE_T, /*!< tag-only */ |
| 502 | TLV_TYPE_TV, /*!< tag-value (8bit) */ |
| 503 | TLV_TYPE_TLV, /*!< tag-length-value */ |
| 504 | TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */ |
| 505 | TLV_TYPE_TvLV, /*!< tag, variable length, value */ |
| 506 | TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */ |
| 507 | TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 508 | }; |
| 509 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 510 | /*! Definition of a single IE (Information Element) */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 511 | struct tlv_def { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 512 | enum tlv_type type; /*!< TLV type */ |
| 513 | uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 514 | }; |
| 515 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 516 | /*! Definition of All 256 IE / TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 517 | struct tlv_definition { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 518 | struct tlv_def def[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 519 | }; |
| 520 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 521 | /*! result of the TLV parser */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 522 | struct tlv_parsed { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 523 | struct tlv_p_entry lv[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | extern struct tlv_definition tvlv_att_def; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 527 | extern struct tlv_definition vtvlv_gan_att_def; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 528 | |
| 529 | int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, |
| 530 | const struct tlv_definition *def, |
| 531 | const uint8_t *buf, int buf_len); |
| 532 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 533 | const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2); |
Neels Hofmeyr | a78b22b | 2018-04-13 03:36:49 +0200 | [diff] [blame] | 534 | int tlv_parse2(struct tlv_parsed *dec, int dec_multiples, |
| 535 | const struct tlv_definition *def, const uint8_t *buf, int buf_len, |
| 536 | uint8_t lv_tag, uint8_t lv_tag2); |
Neels Hofmeyr | 9e57a5a | 2015-12-21 11:20:14 +0100 | [diff] [blame] | 537 | /* take a master (src) tlv def and fill up all empty slots in 'dst' */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 538 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src); |
| 539 | |
Harald Welte | 8006f53 | 2019-02-13 22:23:13 +0100 | [diff] [blame] | 540 | int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag, |
| 541 | unsigned int len, const uint8_t *val); |
| 542 | int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp); |
| 543 | int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp, |
| 544 | const uint8_t *tag_order, unsigned int tag_order_len); |
| 545 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 546 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 547 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 548 | #define TLVP_VAL(x, y) (x)->lv[y].val |
| 549 | |
Harald Welte | cc27fa6 | 2014-08-18 15:31:04 +0200 | [diff] [blame] | 550 | #define TLVP_PRES_LEN(tp, tag, min_len) \ |
| 551 | (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len) |
| 552 | |
Neels Hofmeyr | 74e4ed6 | 2018-04-13 03:55:06 +0200 | [diff] [blame] | 553 | /*! Return pointer to a TLV element if it is present. |
| 554 | * Usage: |
| 555 | * struct tlv_p_entry *e = TVLP_GET(&tp, TAG); |
| 556 | * if (!e) |
| 557 | * return -ENOENT; |
| 558 | * hexdump(e->val, e->len); |
| 559 | * \param[in] _tp pointer to \ref tlv_parsed. |
| 560 | * \param[in] tag IE tag to return. |
| 561 | * \returns struct tlv_p_entry pointer, or NULL if not present. |
| 562 | */ |
| 563 | #define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL) |
| 564 | |
| 565 | /*! Like TLVP_GET(), but enforcing a minimum val length. |
| 566 | * \param[in] _tp pointer to \ref tlv_parsed. |
| 567 | * \param[in] tag IE tag to return. |
| 568 | * \param[in] min_len Minimum value length in bytes. |
| 569 | * \returns struct tlv_p_entry pointer, or NULL if not present or too short. |
| 570 | */ |
| 571 | #define TLVP_GET_MINLEN(_tp, tag, min_len) \ |
| 572 | (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL) |
| 573 | |
Harald Welte | 1fbe3eb | 2018-06-02 13:34:06 +0200 | [diff] [blame] | 574 | /*! Like TLVP_VAL(), but enforcing a minimum val length. |
| 575 | * \param[in] _tp pointer to \ref tlv_parsed. |
| 576 | * \param[in] tag IE tag to return. |
| 577 | * \param[in] min_len Minimum value length in bytes. |
Max | e6f63f2 | 2018-12-19 19:00:23 +0100 | [diff] [blame] | 578 | * \returns const uint8_t pointer to value, or NULL if not present or too short. |
Harald Welte | 1fbe3eb | 2018-06-02 13:34:06 +0200 | [diff] [blame] | 579 | */ |
| 580 | #define TLVP_VAL_MINLEN(_tp, tag, min_len) \ |
| 581 | (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL) |
| 582 | |
| 583 | |
Max | bd6d5c1 | 2018-12-19 19:01:03 +0100 | [diff] [blame] | 584 | /*! Obtain 1-byte TLV element. |
| 585 | * \param[in] tp pointer to \ref tlv_parsed |
| 586 | * \param[in] tag the Tag to look for |
| 587 | * \param[in] default_val default value to use if tag not available |
| 588 | * \returns the 1st byte of value with a given tag or default_val if tag was not found |
| 589 | */ |
| 590 | static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val) |
| 591 | { |
| 592 | const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1); |
| 593 | |
| 594 | if (res) |
| 595 | return res[0]; |
| 596 | |
| 597 | return default_val; |
| 598 | } |
| 599 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 600 | /*! Align given TLV element with 16 bit value to an even address |
Andreas Eversberg | 0167596 | 2012-11-06 12:02:59 +0100 | [diff] [blame] | 601 | * \param[in] tp pointer to \ref tlv_parsed |
| 602 | * \param[in] pos element to return |
| 603 | * \returns aligned 16 bit value |
| 604 | */ |
| 605 | static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos) |
| 606 | { |
| 607 | uint16_t res; |
| 608 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 609 | return res; |
| 610 | } |
| 611 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 612 | /*! Align given TLV element with 32 bit value to an address that is a multiple of 4 |
Andreas Eversberg | 0167596 | 2012-11-06 12:02:59 +0100 | [diff] [blame] | 613 | * \param[in] tp pointer to \ref tlv_parsed |
| 614 | * \param[in] pos element to return |
| 615 | * \returns aligned 32 bit value |
| 616 | */ |
| 617 | static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos) |
| 618 | { |
| 619 | uint32_t res; |
| 620 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 621 | return res; |
| 622 | } |
| 623 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 624 | /*! Retrieve (possibly unaligned) TLV element and convert to host byte order |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 625 | * \param[in] tp pointer to \ref tlv_parsed |
| 626 | * \param[in] pos element to return |
| 627 | * \returns aligned 16 bit value in host byte order |
| 628 | */ |
| 629 | static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos) |
| 630 | { |
| 631 | return osmo_load16be(TLVP_VAL(tp, pos)); |
| 632 | } |
| 633 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 634 | /*! Retrieve (possibly unaligned) TLV element and convert to host byte order |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 635 | * \param[in] tp pointer to \ref tlv_parsed |
| 636 | * \param[in] pos element to return |
| 637 | * \returns aligned 32 bit value in host byte order |
| 638 | */ |
| 639 | static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos) |
| 640 | { |
| 641 | return osmo_load32be(TLVP_VAL(tp, pos)); |
| 642 | } |
| 643 | |
| 644 | |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 645 | struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx); |
| 646 | int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src); |
Harald Welte | fbd02fa | 2016-04-25 15:19:35 +0200 | [diff] [blame] | 647 | int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, |
| 648 | size_t len, uint8_t **value); |
| 649 | int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, |
| 650 | uint8_t tag, size_t len, uint8_t **value); |
| 651 | int osmo_shift_tlv(uint8_t **data, size_t *data_len, |
| 652 | uint8_t *tag, uint8_t **value, size_t *value_len); |
| 653 | int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, |
| 654 | uint8_t tag, uint8_t **value, size_t *value_len); |
| 655 | int osmo_shift_lv(uint8_t **data, size_t *data_len, |
| 656 | uint8_t **value, size_t *value_len); |
| 657 | |
Harald Welte | 9510992 | 2020-12-04 13:55:38 +0100 | [diff] [blame] | 658 | #define MSG_DEF(name, mand_ies, flags) { name, mand_ies, ARRAY_SIZE(mand_ies), flags } |
| 659 | |
| 660 | struct osmo_tlv_prot_msg_def { |
| 661 | /*! human-readable name of message type (optional) */ |
| 662 | const char *name; |
| 663 | /*! array of mandatory IEs */ |
| 664 | const uint8_t *mand_ies; |
| 665 | /*! number of entries in 'mand_ies' above */ |
| 666 | uint8_t mand_count; |
| 667 | /*! user-defined flags (like uplink/downlink/...) */ |
| 668 | uint32_t flags; |
| 669 | }; |
| 670 | struct osmo_tlv_prot_ie_def { |
| 671 | /*! minimum length of IE value part, in octets */ |
| 672 | uint16_t min_len; |
| 673 | /*! huamn-readable name (optional) */ |
| 674 | const char *name; |
| 675 | }; |
| 676 | |
| 677 | /*! Osmocom TLV protocol definition */ |
| 678 | struct osmo_tlv_prot_def { |
| 679 | /*! human-readable name of protocol */ |
| 680 | const char *name; |
| 681 | /*! TLV parser definition (optional) */ |
| 682 | const struct tlv_definition *tlv_def; |
| 683 | /*! definition of each message (8-bit message type) */ |
| 684 | struct osmo_tlv_prot_msg_def msg_def[256]; |
| 685 | /*! definition of IE for each 8-bit tag */ |
| 686 | struct osmo_tlv_prot_ie_def ie_def[256]; |
| 687 | /*! value_string array of message type names (legacy, if not populated in msg_def) */ |
| 688 | const struct value_string *msgt_names; |
| 689 | }; |
| 690 | |
| 691 | const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type); |
| 692 | const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei); |
| 693 | |
| 694 | int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type, |
| 695 | const struct tlv_parsed *tp, int log_subsys, const char *log_pfx); |
| 696 | |
| 697 | int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef, |
| 698 | struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type, |
| 699 | const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2, |
| 700 | int log_subsys, const char *log_pfx); |
| 701 | |
| 702 | static inline uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type) |
| 703 | { |
| 704 | return pdef->msg_def[msg_type].flags; |
| 705 | } |
| 706 | |
| 707 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 708 | /*! @} */ |