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 | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 114 | /*! put (append) a TLV16 field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 115 | static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 116 | const uint16_t *val) |
| 117 | { |
| 118 | *buf++ = tag; |
| 119 | *buf++ = len; |
| 120 | memcpy(buf, val, len*2); |
| 121 | return buf + len*2; |
| 122 | } |
| 123 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 124 | /*! put (append) a TL16V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 125 | static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 126 | const uint8_t *val) |
| 127 | { |
| 128 | *buf++ = tag; |
| 129 | *buf++ = len >> 8; |
| 130 | *buf++ = len & 0xff; |
| 131 | memcpy(buf, val, len); |
| 132 | return buf + len*2; |
| 133 | } |
| 134 | |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 135 | /*! put (append) a TL16V field */ |
| 136 | static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len, |
| 137 | const uint8_t *val) |
| 138 | { |
| 139 | *buf++ = tag >> 8; |
| 140 | *buf++ = tag & 0xff; |
| 141 | *buf++ = len; |
| 142 | memcpy(buf, val, len); |
| 143 | return buf + len + 2; |
| 144 | } |
| 145 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 146 | /*! put (append) a TvLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 147 | static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 148 | const uint8_t *val) |
| 149 | { |
| 150 | uint8_t *ret; |
| 151 | |
| 152 | if (len <= TVLV_MAX_ONEBYTE) { |
| 153 | ret = tlv_put(buf, tag, len, val); |
| 154 | buf[1] |= 0x80; |
| 155 | } else |
| 156 | ret = tl16v_put(buf, tag, len, val); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 161 | /*! put (append) a variable-length tag or variable-length length * */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 162 | static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag) |
| 163 | { |
| 164 | if (tag > TVLV_MAX_ONEBYTE) { |
| 165 | /* two-byte TAG */ |
| 166 | *buf++ = 0x80 | (tag >> 8); |
| 167 | *buf++ = (tag & 0xff); |
| 168 | } else |
| 169 | *buf++ = tag; |
| 170 | |
| 171 | return buf; |
| 172 | } |
| 173 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 174 | /* put (append) vTvL (GAN) field (tag + length)*/ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 175 | static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len) |
| 176 | { |
| 177 | uint8_t *ret; |
| 178 | |
| 179 | ret = vt_gan_put(buf, tag); |
| 180 | return vt_gan_put(ret, len); |
| 181 | } |
| 182 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 183 | /* put (append) vTvLV (GAN) field (tag + length + val) */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 184 | static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len, |
| 185 | const uint8_t *val) |
| 186 | { |
| 187 | uint8_t *ret; |
| 188 | |
| 189 | ret = vtvl_gan_put(buf, tag, len ); |
| 190 | |
| 191 | memcpy(ret, val, len); |
| 192 | ret = buf + len; |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 197 | /*! put (append) a TLV16 field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 198 | static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) |
| 199 | { |
| 200 | uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 201 | return tlv16_put(buf, tag, len, val); |
| 202 | } |
| 203 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 204 | /*! put (append) a TL16V field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 205 | static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 206 | const uint8_t *val) |
| 207 | { |
| 208 | uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 209 | return tl16v_put(buf, tag, len, val); |
| 210 | } |
| 211 | |
Harald Welte | 4a29f34 | 2017-08-09 19:00:09 +0200 | [diff] [blame] | 212 | static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val) |
| 213 | { |
| 214 | uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len)); |
| 215 | return t16lv_put(buf, tag, len, val); |
| 216 | } |
| 217 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 218 | /*! put (append) a TvLV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 219 | static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 220 | const uint8_t *val) |
| 221 | { |
| 222 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 223 | return tvlv_put(buf, tag, len, val); |
| 224 | } |
| 225 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 226 | /*! put (append) a vTvLV field to \ref msgb */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 227 | static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag, |
| 228 | uint16_t len, const uint8_t *val) |
| 229 | { |
| 230 | uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 231 | return vtvlv_gan_put(buf, tag, len, val); |
| 232 | } |
| 233 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 234 | /*! put (append) a L16TV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 235 | static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, |
| 236 | const uint8_t *val) |
| 237 | { |
| 238 | uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len)); |
| 239 | |
| 240 | *buf++ = len >> 8; |
| 241 | *buf++ = len & 0xff; |
| 242 | *buf++ = tag; |
| 243 | memcpy(buf, val, len); |
| 244 | return buf + len; |
| 245 | } |
| 246 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 247 | /*! put (append) a V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 248 | static inline uint8_t *v_put(uint8_t *buf, uint8_t val) |
| 249 | { |
| 250 | *buf++ = val; |
| 251 | return buf; |
| 252 | } |
| 253 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 254 | /*! put (append) a TV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 255 | static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, |
| 256 | uint8_t val) |
| 257 | { |
| 258 | *buf++ = tag; |
| 259 | *buf++ = val; |
| 260 | return buf; |
| 261 | } |
| 262 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 263 | /*! put (append) a TVfixed field */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 264 | static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, |
| 265 | unsigned int len, const uint8_t *val) |
| 266 | { |
| 267 | *buf++ = tag; |
| 268 | memcpy(buf, val, len); |
| 269 | return buf + len; |
| 270 | } |
| 271 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 272 | /*! put (append) a TV16 field |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 273 | * \param[in,out] buf data buffer |
| 274 | * \param[in] tag Tag value |
| 275 | * \param[in] val Value (in host byte order!) |
| 276 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 277 | static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, |
| 278 | uint16_t val) |
| 279 | { |
| 280 | *buf++ = tag; |
| 281 | *buf++ = val >> 8; |
| 282 | *buf++ = val & 0xff; |
| 283 | return buf; |
| 284 | } |
| 285 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 286 | /*! put (append) a LV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 287 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 288 | static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) |
| 289 | { |
| 290 | uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); |
| 291 | return lv_put(buf, len, val); |
| 292 | } |
| 293 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 294 | /*! put (append) a TLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 295 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 296 | static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 297 | { |
| 298 | uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 299 | return tlv_put(buf, tag, len, val); |
| 300 | } |
| 301 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 302 | /*! put (append) a TV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 303 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 304 | static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) |
| 305 | { |
| 306 | uint8_t *buf = msgb_put(msg, 2); |
| 307 | return tv_put(buf, tag, val); |
| 308 | } |
| 309 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 310 | /*! put (append) a TVfixed field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 311 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 312 | static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, |
| 313 | unsigned int len, const uint8_t *val) |
| 314 | { |
| 315 | uint8_t *buf = msgb_put(msg, 1+len); |
| 316 | return tv_fixed_put(buf, tag, len, val); |
| 317 | } |
| 318 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 319 | /*! put (append) a V field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 320 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 321 | static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) |
| 322 | { |
| 323 | uint8_t *buf = msgb_put(msg, 1); |
| 324 | return v_put(buf, val); |
| 325 | } |
| 326 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 327 | /*! put (append) a TV16 field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 328 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 329 | static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) |
| 330 | { |
| 331 | uint8_t *buf = msgb_put(msg, 3); |
| 332 | return tv16_put(buf, tag, val); |
| 333 | } |
| 334 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 335 | /*! push (prepend) a TLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 336 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 337 | static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 338 | { |
| 339 | uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 340 | tlv_put(buf, tag, len, val); |
| 341 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 344 | /*! push (prepend) a TV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 345 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 346 | static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 347 | { |
| 348 | uint8_t *buf = msgb_push(msg, 2); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 349 | tv_put(buf, tag, val); |
| 350 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 353 | /*! push (prepend) a TV16 field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 354 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 355 | static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) |
| 356 | { |
| 357 | uint8_t *buf = msgb_push(msg, 3); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 358 | tv16_put(buf, tag, val); |
| 359 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 362 | /*! push (prepend) a TvLV field to a \ref msgb |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 363 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 364 | static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, |
| 365 | const uint8_t *val) |
| 366 | { |
| 367 | uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 368 | tvlv_put(buf, tag, len, val); |
| 369 | return buf; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 372 | /* push (prepend) a vTvL header to a \ref msgb |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 373 | */ |
| 374 | static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag, |
| 375 | uint16_t len) |
| 376 | { |
| 377 | uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len)); |
| 378 | vtvl_gan_put(buf, tag, len); |
| 379 | return buf; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag, |
| 384 | uint16_t len, const uint8_t *val) |
| 385 | { |
| 386 | uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 387 | vtvlv_gan_put(buf, tag, len, val); |
| 388 | return buf; |
| 389 | } |
| 390 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 391 | /* TLV parsing */ |
| 392 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 393 | /*! Entry in a TLV parser array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 394 | struct tlv_p_entry { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 395 | uint16_t len; /*!< length */ |
| 396 | const uint8_t *val; /*!< pointer to value */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 397 | }; |
| 398 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 399 | /*! TLV type */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 400 | enum tlv_type { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 401 | TLV_TYPE_NONE, /*!< no type */ |
| 402 | TLV_TYPE_FIXED, /*!< fixed-length value-only */ |
| 403 | TLV_TYPE_T, /*!< tag-only */ |
| 404 | TLV_TYPE_TV, /*!< tag-value (8bit) */ |
| 405 | TLV_TYPE_TLV, /*!< tag-length-value */ |
| 406 | TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */ |
| 407 | TLV_TYPE_TvLV, /*!< tag, variable length, value */ |
| 408 | TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */ |
| 409 | TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 410 | }; |
| 411 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 412 | /*! Definition of a single IE (Information Element) */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 413 | struct tlv_def { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 414 | enum tlv_type type; /*!< TLV type */ |
| 415 | uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 416 | }; |
| 417 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 418 | /*! Definition of All 256 IE / TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 419 | struct tlv_definition { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 420 | struct tlv_def def[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 421 | }; |
| 422 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 423 | /*! result of the TLV parser */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 424 | struct tlv_parsed { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 425 | struct tlv_p_entry lv[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | extern struct tlv_definition tvlv_att_def; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 429 | extern struct tlv_definition vtvlv_gan_att_def; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 430 | |
| 431 | int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, |
| 432 | const struct tlv_definition *def, |
| 433 | const uint8_t *buf, int buf_len); |
| 434 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 435 | 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] | 436 | int tlv_parse2(struct tlv_parsed *dec, int dec_multiples, |
| 437 | const struct tlv_definition *def, const uint8_t *buf, int buf_len, |
| 438 | uint8_t lv_tag, uint8_t lv_tag2); |
Neels Hofmeyr | 9e57a5a | 2015-12-21 11:20:14 +0100 | [diff] [blame] | 439 | /* 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] | 440 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src); |
| 441 | |
| 442 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 443 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 444 | #define TLVP_VAL(x, y) (x)->lv[y].val |
| 445 | |
Harald Welte | cc27fa6 | 2014-08-18 15:31:04 +0200 | [diff] [blame] | 446 | #define TLVP_PRES_LEN(tp, tag, min_len) \ |
| 447 | (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len) |
| 448 | |
Neels Hofmeyr | 74e4ed6 | 2018-04-13 03:55:06 +0200 | [diff] [blame] | 449 | /*! Return pointer to a TLV element if it is present. |
| 450 | * Usage: |
| 451 | * struct tlv_p_entry *e = TVLP_GET(&tp, TAG); |
| 452 | * if (!e) |
| 453 | * return -ENOENT; |
| 454 | * hexdump(e->val, e->len); |
| 455 | * \param[in] _tp pointer to \ref tlv_parsed. |
| 456 | * \param[in] tag IE tag to return. |
| 457 | * \returns struct tlv_p_entry pointer, or NULL if not present. |
| 458 | */ |
| 459 | #define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL) |
| 460 | |
| 461 | /*! Like TLVP_GET(), but enforcing a minimum val length. |
| 462 | * \param[in] _tp pointer to \ref tlv_parsed. |
| 463 | * \param[in] tag IE tag to return. |
| 464 | * \param[in] min_len Minimum value length in bytes. |
| 465 | * \returns struct tlv_p_entry pointer, or NULL if not present or too short. |
| 466 | */ |
| 467 | #define TLVP_GET_MINLEN(_tp, tag, min_len) \ |
| 468 | (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL) |
| 469 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 470 | /*! Align given TLV element with 16 bit value to an even address |
Andreas Eversberg | 0167596 | 2012-11-06 12:02:59 +0100 | [diff] [blame] | 471 | * \param[in] tp pointer to \ref tlv_parsed |
| 472 | * \param[in] pos element to return |
| 473 | * \returns aligned 16 bit value |
| 474 | */ |
| 475 | static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos) |
| 476 | { |
| 477 | uint16_t res; |
| 478 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 479 | return res; |
| 480 | } |
| 481 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 482 | /*! 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] | 483 | * \param[in] tp pointer to \ref tlv_parsed |
| 484 | * \param[in] pos element to return |
| 485 | * \returns aligned 32 bit value |
| 486 | */ |
| 487 | static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos) |
| 488 | { |
| 489 | uint32_t res; |
| 490 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 491 | return res; |
| 492 | } |
| 493 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 494 | /*! Retrieve (possibly unaligned) TLV element and convert to host byte order |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 495 | * \param[in] tp pointer to \ref tlv_parsed |
| 496 | * \param[in] pos element to return |
| 497 | * \returns aligned 16 bit value in host byte order |
| 498 | */ |
| 499 | static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos) |
| 500 | { |
| 501 | return osmo_load16be(TLVP_VAL(tp, pos)); |
| 502 | } |
| 503 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 504 | /*! Retrieve (possibly unaligned) TLV element and convert to host byte order |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 505 | * \param[in] tp pointer to \ref tlv_parsed |
| 506 | * \param[in] pos element to return |
| 507 | * \returns aligned 32 bit value in host byte order |
| 508 | */ |
| 509 | static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos) |
| 510 | { |
| 511 | return osmo_load32be(TLVP_VAL(tp, pos)); |
| 512 | } |
| 513 | |
| 514 | |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 515 | struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx); |
| 516 | 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] | 517 | int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, |
| 518 | size_t len, uint8_t **value); |
| 519 | int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, |
| 520 | uint8_t tag, size_t len, uint8_t **value); |
| 521 | int osmo_shift_tlv(uint8_t **data, size_t *data_len, |
| 522 | uint8_t *tag, uint8_t **value, size_t *value_len); |
| 523 | int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, |
| 524 | uint8_t tag, uint8_t **value, size_t *value_len); |
| 525 | int osmo_shift_lv(uint8_t **data, size_t *data_len, |
| 526 | uint8_t **value, size_t *value_len); |
| 527 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 528 | /*! @} */ |