Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #ifndef _TLV_H |
| 2 | #define _TLV_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 7 | #include <osmocom/core/msgb.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 8 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 9 | /*! \defgroup tlv GSM L3 compatible TLV parser |
| 10 | * @{ |
| 11 | */ |
| 12 | /*! \file tlv.h */ |
| 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 |
| 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 | |
| 49 | /* TLV generation */ |
| 50 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 51 | /*! \brief put (append) a LV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, |
| 53 | const uint8_t *val) |
| 54 | { |
| 55 | *buf++ = len; |
| 56 | memcpy(buf, val, len); |
| 57 | return buf + len; |
| 58 | } |
| 59 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 60 | /*! \brief put (append) a TLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 62 | const uint8_t *val) |
| 63 | { |
| 64 | *buf++ = tag; |
| 65 | *buf++ = len; |
| 66 | memcpy(buf, val, len); |
| 67 | return buf + len; |
| 68 | } |
| 69 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 70 | /*! \brief put (append) a TLV16 field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 71 | static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 72 | const uint16_t *val) |
| 73 | { |
| 74 | *buf++ = tag; |
| 75 | *buf++ = len; |
| 76 | memcpy(buf, val, len*2); |
| 77 | return buf + len*2; |
| 78 | } |
| 79 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 80 | /*! \brief put (append) a TL16V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 82 | const uint8_t *val) |
| 83 | { |
| 84 | *buf++ = tag; |
| 85 | *buf++ = len >> 8; |
| 86 | *buf++ = len & 0xff; |
| 87 | memcpy(buf, val, len); |
| 88 | return buf + len*2; |
| 89 | } |
| 90 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 91 | /*! \brief put (append) a TvLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 92 | static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 93 | const uint8_t *val) |
| 94 | { |
| 95 | uint8_t *ret; |
| 96 | |
| 97 | if (len <= TVLV_MAX_ONEBYTE) { |
| 98 | ret = tlv_put(buf, tag, len, val); |
| 99 | buf[1] |= 0x80; |
| 100 | } else |
| 101 | ret = tl16v_put(buf, tag, len, val); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 106 | /*! \brief put (append) a TLV16 field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 107 | static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) |
| 108 | { |
| 109 | uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 110 | return tlv16_put(buf, tag, len, val); |
| 111 | } |
| 112 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 113 | /*! \brief put (append) a TL16V field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 114 | static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 115 | const uint8_t *val) |
| 116 | { |
| 117 | uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 118 | return tl16v_put(buf, tag, len, val); |
| 119 | } |
| 120 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 121 | /*! \brief put (append) a TvLV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 122 | static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 123 | const uint8_t *val) |
| 124 | { |
| 125 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 126 | return tvlv_put(buf, tag, len, val); |
| 127 | } |
| 128 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 129 | /*! \brief put (append) a L16TV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 130 | static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, |
| 131 | const uint8_t *val) |
| 132 | { |
| 133 | uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len)); |
| 134 | |
| 135 | *buf++ = len >> 8; |
| 136 | *buf++ = len & 0xff; |
| 137 | *buf++ = tag; |
| 138 | memcpy(buf, val, len); |
| 139 | return buf + len; |
| 140 | } |
| 141 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 142 | /*! \brief put (append) a V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 143 | static inline uint8_t *v_put(uint8_t *buf, uint8_t val) |
| 144 | { |
| 145 | *buf++ = val; |
| 146 | return buf; |
| 147 | } |
| 148 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 149 | /*! \brief put (append) a TV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 150 | static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, |
| 151 | uint8_t val) |
| 152 | { |
| 153 | *buf++ = tag; |
| 154 | *buf++ = val; |
| 155 | return buf; |
| 156 | } |
| 157 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 158 | /*! \brief put (append) a TVfixed field */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 159 | static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, |
| 160 | unsigned int len, const uint8_t *val) |
| 161 | { |
| 162 | *buf++ = tag; |
| 163 | memcpy(buf, val, len); |
| 164 | return buf + len; |
| 165 | } |
| 166 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 167 | /*! \brief put (append) a TV16 field |
| 168 | * \param[in,out] buf data buffer |
| 169 | * \param[in] tag Tag value |
| 170 | * \param[in] val Value (in host byte order!) |
| 171 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 172 | static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, |
| 173 | uint16_t val) |
| 174 | { |
| 175 | *buf++ = tag; |
| 176 | *buf++ = val >> 8; |
| 177 | *buf++ = val & 0xff; |
| 178 | return buf; |
| 179 | } |
| 180 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 181 | /*! \brief put (append) a LV field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 182 | static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) |
| 183 | { |
| 184 | uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); |
| 185 | return lv_put(buf, len, val); |
| 186 | } |
| 187 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 188 | /*! \brief put (append) a TLV field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 189 | static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 190 | { |
| 191 | uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 192 | return tlv_put(buf, tag, len, val); |
| 193 | } |
| 194 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 195 | /*! \brief put (append) a TV field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 196 | static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) |
| 197 | { |
| 198 | uint8_t *buf = msgb_put(msg, 2); |
| 199 | return tv_put(buf, tag, val); |
| 200 | } |
| 201 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 202 | /*! \brief put (append) a TVfixed field to a \ref msgb */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 203 | static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, |
| 204 | unsigned int len, const uint8_t *val) |
| 205 | { |
| 206 | uint8_t *buf = msgb_put(msg, 1+len); |
| 207 | return tv_fixed_put(buf, tag, len, val); |
| 208 | } |
| 209 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 210 | /*! \brief put (append) a V field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 211 | static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) |
| 212 | { |
| 213 | uint8_t *buf = msgb_put(msg, 1); |
| 214 | return v_put(buf, val); |
| 215 | } |
| 216 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 217 | /*! \brief put (append) a TV16 field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 218 | static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) |
| 219 | { |
| 220 | uint8_t *buf = msgb_put(msg, 3); |
| 221 | return tv16_put(buf, tag, val); |
| 222 | } |
| 223 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 224 | /*! \brief push (prepend) a TLV field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 225 | static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 226 | { |
| 227 | uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
| 228 | return tlv_put(buf, tag, len, val); |
| 229 | } |
| 230 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 231 | /*! \brief push (prepend) a TV field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 232 | static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 233 | { |
| 234 | uint8_t *buf = msgb_push(msg, 2); |
| 235 | return tv_put(buf, tag, val); |
| 236 | } |
| 237 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 238 | /*! \brief push (prepend) a TV16 field to a \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 239 | static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) |
| 240 | { |
| 241 | uint8_t *buf = msgb_push(msg, 3); |
| 242 | return tv16_put(buf, tag, val); |
| 243 | } |
| 244 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 245 | /*! \brief push (prepend) a TvLV field to a \ref msgb */ |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 246 | static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, |
| 247 | const uint8_t *val) |
| 248 | { |
| 249 | uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len)); |
| 250 | return tvlv_put(buf, tag, len, val); |
| 251 | } |
| 252 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 253 | /* TLV parsing */ |
| 254 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 255 | /*! \brief Entry in a TLV parser array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 256 | struct tlv_p_entry { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 257 | uint16_t len; /*!< \brief length */ |
| 258 | const uint8_t *val; /*!< \brief pointer to value */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 259 | }; |
| 260 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 261 | /*! \brief TLV type */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 262 | enum tlv_type { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 263 | TLV_TYPE_NONE, /*!< \brief no type */ |
| 264 | TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */ |
| 265 | TLV_TYPE_T, /*!< \brief tag-only */ |
| 266 | TLV_TYPE_TV, /*!< \brief tag-value (8bit) */ |
| 267 | TLV_TYPE_TLV, /*!< \brief tag-length-value */ |
| 268 | TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */ |
| 269 | TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */ |
| 270 | TLV_TYPE_SINGLE_TV /*!< \brief tag and value (both 4 bit) in 1 byte */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 271 | }; |
| 272 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 273 | /*! \brief Definition of a single IE (Information Element) */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 274 | struct tlv_def { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 275 | enum tlv_type type; /*!< \brief TLV type */ |
| 276 | 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] | 277 | }; |
| 278 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 279 | /*! \brief Definition of All 256 IE / TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 280 | struct tlv_definition { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 281 | struct tlv_def def[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 282 | }; |
| 283 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 284 | /*! \brief result of the TLV parser */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 285 | struct tlv_parsed { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 286 | struct tlv_p_entry lv[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | extern struct tlv_definition tvlv_att_def; |
| 290 | |
| 291 | int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, |
| 292 | const struct tlv_definition *def, |
| 293 | const uint8_t *buf, int buf_len); |
| 294 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 295 | const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2); |
| 296 | /* take a master (src) tlvdev and fill up all empty slots in 'dst' */ |
| 297 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src); |
| 298 | |
| 299 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 300 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 301 | #define TLVP_VAL(x, y) (x)->lv[y].val |
| 302 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 303 | /*! }@ */ |
| 304 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 305 | #endif /* _TLV_H */ |