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 | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 10 | /*! \defgroup tlv GSM L3 compatible TLV parser |
| 11 | * @{ |
| 12 | */ |
| 13 | /*! \file tlv.h */ |
| 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 | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 25 | |
| 26 | */ |
| 27 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 28 | /*! \brief 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) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 30 | /*! \brief 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) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 32 | /*! \brief 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) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 34 | /*! \brief 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) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 36 | /*! \brief 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) |
| 38 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 39 | /*! \brief maximum length of TLV of one byte length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | #define TVLV_MAX_ONEBYTE 0x7f |
| 41 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 42 | /*! \brief gross length of a TVLV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | static inline uint16_t TVLV_GROSS_LEN(uint16_t len) |
| 44 | { |
| 45 | if (len <= TVLV_MAX_ONEBYTE) |
| 46 | return TLV_GROSS_LEN(len); |
| 47 | else |
| 48 | return TL16V_GROSS_LEN(len); |
| 49 | } |
| 50 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 51 | /*! \brief gross length of vTvL header (tag+len) */ |
| 52 | static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 53 | { |
| 54 | uint16_t ret = 2; |
| 55 | |
| 56 | if (tag > TVLV_MAX_ONEBYTE) |
| 57 | ret++; |
| 58 | |
| 59 | if (len > TVLV_MAX_ONEBYTE) |
| 60 | ret++; |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | /*! \brief gross length of vTvLV (tag+len+val) */ |
| 66 | static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 67 | { |
| 68 | uint16_t ret; |
| 69 | |
| 70 | if (len <= TVLV_MAX_ONEBYTE) |
Vadim Yanitskiy | ac9e2d8 | 2017-05-14 20:52:46 +0300 | [diff] [blame] | 71 | ret = TLV_GROSS_LEN(len); |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 72 | else |
Vadim Yanitskiy | ac9e2d8 | 2017-05-14 20:52:46 +0300 | [diff] [blame] | 73 | ret = TL16V_GROSS_LEN(len); |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 74 | |
| 75 | if (tag > TVLV_MAX_ONEBYTE) |
| 76 | ret += 1; |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | /* TLV generation */ |
| 82 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 83 | /*! \brief put (append) a LV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 84 | static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, |
| 85 | const uint8_t *val) |
| 86 | { |
| 87 | *buf++ = len; |
| 88 | memcpy(buf, val, len); |
| 89 | return buf + len; |
| 90 | } |
| 91 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 92 | /*! \brief put (append) a TLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 93 | static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 94 | const uint8_t *val) |
| 95 | { |
| 96 | *buf++ = tag; |
| 97 | *buf++ = len; |
| 98 | memcpy(buf, val, len); |
| 99 | return buf + len; |
| 100 | } |
| 101 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 102 | /*! \brief put (append) a TLV16 field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 103 | static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 104 | const uint16_t *val) |
| 105 | { |
| 106 | *buf++ = tag; |
| 107 | *buf++ = len; |
| 108 | memcpy(buf, val, len*2); |
| 109 | return buf + len*2; |
| 110 | } |
| 111 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 112 | /*! \brief put (append) a TL16V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 113 | static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 114 | const uint8_t *val) |
| 115 | { |
| 116 | *buf++ = tag; |
| 117 | *buf++ = len >> 8; |
| 118 | *buf++ = len & 0xff; |
| 119 | memcpy(buf, val, len); |
| 120 | return buf + len*2; |
| 121 | } |
| 122 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 123 | /*! \brief put (append) a TvLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 124 | static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 125 | const uint8_t *val) |
| 126 | { |
| 127 | uint8_t *ret; |
| 128 | |
| 129 | if (len <= TVLV_MAX_ONEBYTE) { |
| 130 | ret = tlv_put(buf, tag, len, val); |
| 131 | buf[1] |= 0x80; |
| 132 | } else |
| 133 | ret = tl16v_put(buf, tag, len, val); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 138 | /*! \brief put (append) a variable-length tag or variable-length length * */ |
| 139 | static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag) |
| 140 | { |
| 141 | if (tag > TVLV_MAX_ONEBYTE) { |
| 142 | /* two-byte TAG */ |
| 143 | *buf++ = 0x80 | (tag >> 8); |
| 144 | *buf++ = (tag & 0xff); |
| 145 | } else |
| 146 | *buf++ = tag; |
| 147 | |
| 148 | return buf; |
| 149 | } |
| 150 | |
| 151 | /* \brief put (append) vTvL (GAN) field (tag + length)*/ |
| 152 | static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len) |
| 153 | { |
| 154 | uint8_t *ret; |
| 155 | |
| 156 | ret = vt_gan_put(buf, tag); |
| 157 | return vt_gan_put(ret, len); |
| 158 | } |
| 159 | |
| 160 | /* \brief put (append) vTvLV (GAN) field (tag + length + val) */ |
| 161 | static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len, |
| 162 | const uint8_t *val) |
| 163 | { |
| 164 | uint8_t *ret; |
| 165 | |
| 166 | ret = vtvl_gan_put(buf, tag, len ); |
| 167 | |
| 168 | memcpy(ret, val, len); |
| 169 | ret = buf + len; |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 174 | /*! \brief put (append) a TLV16 field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 175 | static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) |
| 176 | { |
| 177 | uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 178 | return tlv16_put(buf, tag, len, val); |
| 179 | } |
| 180 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 181 | /*! \brief put (append) a TL16V field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 182 | static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 183 | const uint8_t *val) |
| 184 | { |
| 185 | uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 186 | return tl16v_put(buf, tag, len, val); |
| 187 | } |
| 188 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 189 | /*! \brief put (append) a TvLV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 190 | static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 191 | const uint8_t *val) |
| 192 | { |
| 193 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 194 | return tvlv_put(buf, tag, len, val); |
| 195 | } |
| 196 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 197 | /*! \brief put (append) a vTvLV field to \ref msgb */ |
| 198 | static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag, |
| 199 | uint16_t len, const uint8_t *val) |
| 200 | { |
| 201 | uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 202 | return vtvlv_gan_put(buf, tag, len, val); |
| 203 | } |
| 204 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 205 | /*! \brief put (append) a L16TV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 206 | static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, |
| 207 | const uint8_t *val) |
| 208 | { |
| 209 | uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len)); |
| 210 | |
| 211 | *buf++ = len >> 8; |
| 212 | *buf++ = len & 0xff; |
| 213 | *buf++ = tag; |
| 214 | memcpy(buf, val, len); |
| 215 | return buf + len; |
| 216 | } |
| 217 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 218 | /*! \brief put (append) a V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 219 | static inline uint8_t *v_put(uint8_t *buf, uint8_t val) |
| 220 | { |
| 221 | *buf++ = val; |
| 222 | return buf; |
| 223 | } |
| 224 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 225 | /*! \brief put (append) a TV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 226 | static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, |
| 227 | uint8_t val) |
| 228 | { |
| 229 | *buf++ = tag; |
| 230 | *buf++ = val; |
| 231 | return buf; |
| 232 | } |
| 233 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 234 | /*! \brief put (append) a TVfixed field */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 235 | static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, |
| 236 | unsigned int len, const uint8_t *val) |
| 237 | { |
| 238 | *buf++ = tag; |
| 239 | memcpy(buf, val, len); |
| 240 | return buf + len; |
| 241 | } |
| 242 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 243 | /*! \brief put (append) a TV16 field |
| 244 | * \param[in,out] buf data buffer |
| 245 | * \param[in] tag Tag value |
| 246 | * \param[in] val Value (in host byte order!) |
| 247 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 248 | static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, |
| 249 | uint16_t val) |
| 250 | { |
| 251 | *buf++ = tag; |
| 252 | *buf++ = val >> 8; |
| 253 | *buf++ = val & 0xff; |
| 254 | return buf; |
| 255 | } |
| 256 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 257 | /*! \brief put (append) a LV field to a \ref msgb |
| 258 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 259 | static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) |
| 260 | { |
| 261 | uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); |
| 262 | return lv_put(buf, len, val); |
| 263 | } |
| 264 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 265 | /*! \brief put (append) a TLV field to a \ref msgb |
| 266 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 267 | static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 268 | { |
| 269 | uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 270 | return tlv_put(buf, tag, len, val); |
| 271 | } |
| 272 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 273 | /*! \brief put (append) a TV field to a \ref msgb |
| 274 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 275 | static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) |
| 276 | { |
| 277 | uint8_t *buf = msgb_put(msg, 2); |
| 278 | return tv_put(buf, tag, val); |
| 279 | } |
| 280 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 281 | /*! \brief put (append) a TVfixed field to a \ref msgb |
| 282 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 283 | static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, |
| 284 | unsigned int len, const uint8_t *val) |
| 285 | { |
| 286 | uint8_t *buf = msgb_put(msg, 1+len); |
| 287 | return tv_fixed_put(buf, tag, len, val); |
| 288 | } |
| 289 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 290 | /*! \brief put (append) a V field to a \ref msgb |
| 291 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 292 | static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) |
| 293 | { |
| 294 | uint8_t *buf = msgb_put(msg, 1); |
| 295 | return v_put(buf, val); |
| 296 | } |
| 297 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 298 | /*! \brief put (append) a TV16 field to a \ref msgb |
| 299 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 300 | static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) |
| 301 | { |
| 302 | uint8_t *buf = msgb_put(msg, 3); |
| 303 | return tv16_put(buf, tag, val); |
| 304 | } |
| 305 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 306 | /*! \brief push (prepend) a TLV field to a \ref msgb |
| 307 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 308 | static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 309 | { |
| 310 | uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 311 | tlv_put(buf, tag, len, val); |
| 312 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 315 | /*! \brief push (prepend) a TV field to a \ref msgb |
| 316 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 317 | static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 318 | { |
| 319 | uint8_t *buf = msgb_push(msg, 2); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 320 | tv_put(buf, tag, val); |
| 321 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 324 | /*! \brief push (prepend) a TV16 field to a \ref msgb |
| 325 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 326 | static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) |
| 327 | { |
| 328 | uint8_t *buf = msgb_push(msg, 3); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 329 | tv16_put(buf, tag, val); |
| 330 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 333 | /*! \brief push (prepend) a TvLV field to a \ref msgb |
| 334 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 335 | static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, |
| 336 | const uint8_t *val) |
| 337 | { |
| 338 | uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 339 | tvlv_put(buf, tag, len, val); |
| 340 | return buf; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 343 | /* \brief push (prepend) a vTvL header to a \ref msgb |
| 344 | */ |
| 345 | static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag, |
| 346 | uint16_t len) |
| 347 | { |
| 348 | uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len)); |
| 349 | vtvl_gan_put(buf, tag, len); |
| 350 | return buf; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag, |
| 355 | uint16_t len, const uint8_t *val) |
| 356 | { |
| 357 | uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 358 | vtvlv_gan_put(buf, tag, len, val); |
| 359 | return buf; |
| 360 | } |
| 361 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 362 | /* TLV parsing */ |
| 363 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 364 | /*! \brief Entry in a TLV parser array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 365 | struct tlv_p_entry { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 366 | uint16_t len; /*!< \brief length */ |
| 367 | const uint8_t *val; /*!< \brief pointer to value */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 368 | }; |
| 369 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 370 | /*! \brief TLV type */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 371 | enum tlv_type { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 372 | TLV_TYPE_NONE, /*!< \brief no type */ |
| 373 | TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */ |
| 374 | TLV_TYPE_T, /*!< \brief tag-only */ |
| 375 | TLV_TYPE_TV, /*!< \brief tag-value (8bit) */ |
| 376 | TLV_TYPE_TLV, /*!< \brief tag-length-value */ |
| 377 | TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */ |
| 378 | TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 379 | TLV_TYPE_SINGLE_TV, /*!< \brief tag and value (both 4 bit) in 1 byte */ |
| 380 | TLV_TYPE_vTvLV_GAN, /*!< \brief variable-length tag, variable-length length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 381 | }; |
| 382 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 383 | /*! \brief Definition of a single IE (Information Element) */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 384 | struct tlv_def { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 385 | enum tlv_type type; /*!< \brief TLV type */ |
| 386 | uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 387 | }; |
| 388 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 389 | /*! \brief Definition of All 256 IE / TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 390 | struct tlv_definition { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 391 | struct tlv_def def[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 392 | }; |
| 393 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 394 | /*! \brief result of the TLV parser */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 395 | struct tlv_parsed { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 396 | struct tlv_p_entry lv[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | extern struct tlv_definition tvlv_att_def; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 400 | extern struct tlv_definition vtvlv_gan_att_def; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 401 | |
| 402 | int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, |
| 403 | const struct tlv_definition *def, |
| 404 | const uint8_t *buf, int buf_len); |
| 405 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 406 | const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2); |
Neels Hofmeyr | 9e57a5a | 2015-12-21 11:20:14 +0100 | [diff] [blame] | 407 | /* 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] | 408 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src); |
| 409 | |
| 410 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 411 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 412 | #define TLVP_VAL(x, y) (x)->lv[y].val |
| 413 | |
Harald Welte | cc27fa6 | 2014-08-18 15:31:04 +0200 | [diff] [blame] | 414 | #define TLVP_PRES_LEN(tp, tag, min_len) \ |
| 415 | (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len) |
| 416 | |
Andreas Eversberg | 0167596 | 2012-11-06 12:02:59 +0100 | [diff] [blame] | 417 | /*! \brief Align given TLV element with 16 bit value to an even address |
| 418 | * \param[in] tp pointer to \ref tlv_parsed |
| 419 | * \param[in] pos element to return |
| 420 | * \returns aligned 16 bit value |
| 421 | */ |
| 422 | static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos) |
| 423 | { |
| 424 | uint16_t res; |
| 425 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 426 | return res; |
| 427 | } |
| 428 | |
| 429 | /*! \brief Align given TLV element with 32 bit value to an address that is a multiple of 4 |
| 430 | * \param[in] tp pointer to \ref tlv_parsed |
| 431 | * \param[in] pos element to return |
| 432 | * \returns aligned 32 bit value |
| 433 | */ |
| 434 | static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos) |
| 435 | { |
| 436 | uint32_t res; |
| 437 | memcpy(&res, TLVP_VAL(tp, pos), sizeof(res)); |
| 438 | return res; |
| 439 | } |
| 440 | |
Harald Welte | 50ef733 | 2017-05-15 12:45:59 +0200 | [diff] [blame] | 441 | /*! \brief Retrieve (possibly unaligned) TLV element and convert to host byte order |
| 442 | * \param[in] tp pointer to \ref tlv_parsed |
| 443 | * \param[in] pos element to return |
| 444 | * \returns aligned 16 bit value in host byte order |
| 445 | */ |
| 446 | static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos) |
| 447 | { |
| 448 | return osmo_load16be(TLVP_VAL(tp, pos)); |
| 449 | } |
| 450 | |
| 451 | /*! \brief Retrieve (possibly unaligned) TLV element and convert to host byte order |
| 452 | * \param[in] tp pointer to \ref tlv_parsed |
| 453 | * \param[in] pos element to return |
| 454 | * \returns aligned 32 bit value in host byte order |
| 455 | */ |
| 456 | static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos) |
| 457 | { |
| 458 | return osmo_load32be(TLVP_VAL(tp, pos)); |
| 459 | } |
| 460 | |
| 461 | |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 462 | struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx); |
| 463 | 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] | 464 | int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, |
| 465 | size_t len, uint8_t **value); |
| 466 | int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, |
| 467 | uint8_t tag, size_t len, uint8_t **value); |
| 468 | int osmo_shift_tlv(uint8_t **data, size_t *data_len, |
| 469 | uint8_t *tag, uint8_t **value, size_t *value_len); |
| 470 | int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, |
| 471 | uint8_t tag, uint8_t **value, size_t *value_len); |
| 472 | int osmo_shift_lv(uint8_t **data, size_t *data_len, |
| 473 | uint8_t **value, size_t *value_len); |
| 474 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 475 | /*! @} */ |