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