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