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