libosmogsm  1.9.0.158-3938d.202402282026
Osmocom GSM library
tlv.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <string.h>
5 
6 #include <osmocom/core/msgb.h>
10 
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
24  vTvLV 8/16 8/16 N * 8
25  T16LV 16 8 N * 8
26 */
27 
29 #define LV_GROSS_LEN(x) (x+1)
31 #define TLV_GROSS_LEN(x) (x+2)
33 #define TLV16_GROSS_LEN(x) ((2*x)+2)
35 #define TL16V_GROSS_LEN(x) (x+3)
37 #define L16TV_GROSS_LEN(x) (x+3)
39 #define T16LV_GROSS_LEN(x) (x+3)
40 
42 #define TVLV_MAX_ONEBYTE 0x7f
43 
49 
52 };
53 
55 static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
56 {
57  if (len <= TVLV_MAX_ONEBYTE)
58  return TLV_GROSS_LEN(len);
59  else
60  return TL16V_GROSS_LEN(len);
61 }
62 
64 static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
65 {
66  uint16_t ret = 2;
67 
68  if (tag > TVLV_MAX_ONEBYTE)
69  ret++;
70 
71  if (len > TVLV_MAX_ONEBYTE)
72  ret++;
73 
74  return ret;
75 }
76 
78 static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
79 {
80  uint16_t ret;
81 
82  if (len <= TVLV_MAX_ONEBYTE)
83  ret = TLV_GROSS_LEN(len);
84  else
85  ret = TL16V_GROSS_LEN(len);
86 
87  if (tag > TVLV_MAX_ONEBYTE)
88  ret += 1;
89 
90  return ret;
91 }
92 
93 /* TLV generation */
94 
96 static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
97  const uint8_t *val)
98 {
99  *buf++ = len;
100  memcpy(buf, val, len);
101  return buf + len;
102 }
103 
111 static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
112  const uint8_t *val)
113 {
114  *buf++ = tag;
115  *buf++ = len;
116  if (len) {
117  if (val)
118  memcpy(buf, val, len);
119  else
120  memset(buf, 0, len);
121  }
122  return buf + len;
123 }
124 
126 static inline uint8_t *tl_put(uint8_t *buf, uint8_t tag, uint8_t len)
127 {
128  *buf++ = tag;
129  *buf++ = len;
130  return buf;
131 }
132 
134 static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
135  const uint16_t *val)
136 {
137  *buf++ = tag;
138  *buf++ = len;
139  memcpy(buf, val, len*2);
140  return buf + len*2;
141 }
142 
144 static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
145  const uint8_t *val)
146 {
147  *buf++ = tag;
148  *buf++ = len >> 8;
149  *buf++ = len & 0xff;
150  memcpy(buf, val, len);
151  return buf + len;
152 }
153 
155 static inline uint8_t *tl16_put(uint8_t *buf, uint8_t tag, uint16_t len)
156 {
157  *buf++ = tag;
158  *buf++ = len >> 8;
159  *buf++ = len & 0xff;
160  return buf;
161 }
162 
164 static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len,
165  const uint8_t *val)
166 {
167  *buf++ = tag >> 8;
168  *buf++ = tag & 0xff;
169  *buf++ = len;
170  memcpy(buf, val, len);
171  return buf + len;
172 }
173 
175 static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
176  const uint8_t *val)
177 {
178  uint8_t *ret;
179 
180  if (len <= TVLV_MAX_ONEBYTE) {
181  ret = tlv_put(buf, tag, len, val);
182  buf[1] |= 0x80;
183  } else
184  ret = tl16v_put(buf, tag, len, val);
185 
186  return ret;
187 }
188 
193 static inline uint8_t *tvl_put(uint8_t *buf, uint8_t tag, uint16_t len)
194 {
195  uint8_t *ret;
196 
197  if (len <= TVLV_MAX_ONEBYTE) {
198  ret = tl_put(buf, tag, len);
199  buf[1] |= 0x80;
200  } else
201  ret = tl16_put(buf, tag, len);
202 
203  return ret;
204 }
205 
207 static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag)
208 {
209  if (tag > TVLV_MAX_ONEBYTE) {
210  /* two-byte TAG */
211  *buf++ = 0x80 | (tag >> 8);
212  *buf++ = (tag & 0xff);
213  } else
214  *buf++ = tag;
215 
216  return buf;
217 }
218 
219 /* put (append) vTvL (GAN) field (tag + length)*/
220 static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
221 {
222  uint8_t *ret;
223 
224  ret = vt_gan_put(buf, tag);
225  return vt_gan_put(ret, len);
226 }
227 
228 /* put (append) vTvLV (GAN) field (tag + length + val) */
229 static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len,
230  const uint8_t *val)
231 {
232  uint8_t *ret;
233 
234  ret = vtvl_gan_put(buf, tag, len );
235 
236  memcpy(ret, val, len);
237  ret = buf + len;
238 
239  return ret;
240 }
241 
243 static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
244 {
245  uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
246  return tlv16_put(buf, tag, len, val);
247 }
248 
250 static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
251  const uint8_t *val)
252 {
253  uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
254  return tl16v_put(buf, tag, len, val);
255 }
256 
257 static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val)
258 {
259  uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len));
260  return t16lv_put(buf, tag, len, val);
261 }
262 
268 static inline uint8_t *msgb_tvl_put(struct msgb *msg, uint8_t tag, uint16_t len)
269 {
270  uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
271  return tvl_put(buf, tag, len);
272 }
273 
275 static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
276  const uint8_t *val)
277 {
278  uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
279  return tvlv_put(buf, tag, len, val);
280 }
281 
283 static inline uint8_t *msgb_tvlv_put_16be(struct msgb *msg, uint8_t tag, uint16_t val)
284 {
285  uint16_t val_be = osmo_htons(val);
286  return msgb_tvlv_put(msg, tag, 2, (const uint8_t *)&val_be);
287 }
288 
290 static inline uint8_t *msgb_tvlv_put_32be(struct msgb *msg, uint8_t tag, uint32_t val)
291 {
292  uint32_t val_be = osmo_htonl(val);
293  return msgb_tvlv_put(msg, tag, 4, (const uint8_t *)&val_be);
294 }
295 
297 static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag,
298  uint16_t len, const uint8_t *val)
299 {
300  uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len));
301  return vtvlv_gan_put(buf, tag, len, val);
302 }
303 
305 static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
306  const uint8_t *val)
307 {
308  uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
309 
310  *buf++ = len >> 8;
311  *buf++ = len & 0xff;
312  *buf++ = tag;
313  memcpy(buf, val, len);
314  return buf + len;
315 }
316 
318 static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
319 {
320  *buf++ = val;
321  return buf;
322 }
323 
325 static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
326  uint8_t val)
327 {
328  *buf++ = tag;
329  *buf++ = val;
330  return buf;
331 }
332 
334 static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
335  unsigned int len, const uint8_t *val)
336 {
337  *buf++ = tag;
338  memcpy(buf, val, len);
339  return buf + len;
340 }
341 
347 static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
348  uint16_t val)
349 {
350  *buf++ = tag;
351  *buf++ = val >> 8;
352  *buf++ = val & 0xff;
353  return buf;
354 }
355 
358 static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
359 {
360  uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
361  return lv_put(buf, len, val);
362 }
363 
366 static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
367 {
368  uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
369  return tlv_put(buf, tag, len, val);
370 }
371 
374 static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
375 {
376  uint8_t *buf = msgb_put(msg, 2);
377  return tv_put(buf, tag, val);
378 }
379 
382 static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
383  unsigned int len, const uint8_t *val)
384 {
385  uint8_t *buf = msgb_put(msg, 1+len);
386  return tv_fixed_put(buf, tag, len, val);
387 }
388 
391 static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
392 {
393  uint8_t *buf = msgb_put(msg, 1);
394  return v_put(buf, val);
395 }
396 
399 static inline uint8_t *msgb_tl_put(struct msgb *msg, uint8_t tag)
400 {
401  uint8_t *len = msgb_v_put(msg, tag);
402 
403  /* reserve space for length, len points to this reserved space already */
404  msgb_v_put(msg, 0);
405 
406  return len;
407 }
408 
411 static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
412 {
413  uint8_t *buf = msgb_put(msg, 3);
414  return tv16_put(buf, tag, val);
415 }
416 
419 static inline uint8_t *msgb_tv32_put(struct msgb *msg, uint8_t tag, uint32_t val)
420 {
421  uint8_t *buf = msgb_put(msg, 1 + 4);
422  *buf++ = tag;
423  osmo_store32be(val, buf);
424  return msg->tail;
425 }
426 
429 static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
430 {
431  uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
432  tlv_put(buf, tag, len, val);
433  return buf;
434 }
435 
437 static inline uint8_t *msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val)
438 {
439  return msgb_tlv_push(msg, tag, 1, &val);
440 }
441 
444 static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
445 {
446  uint8_t *buf = msgb_push(msg, 2);
447  tv_put(buf, tag, val);
448  return buf;
449 }
450 
453 static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
454 {
455  uint8_t *buf = msgb_push(msg, 3);
456  tv16_put(buf, tag, val);
457  return buf;
458 }
459 
462 static inline uint8_t *msgb_tv32_push(struct msgb *msg, uint8_t tag, uint32_t val)
463 {
464  uint8_t *buf = msgb_push(msg, 5);
465  *buf++ = tag;
466  osmo_store32be(val, buf);
467  return buf;
468 }
469 
472 static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
473  const uint8_t *val)
474 {
475  uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
476  tvlv_put(buf, tag, len, val);
477  return buf;
478 }
479 
480 /* push (prepend) a vTvL header to a \ref msgb
481  */
482 static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
483  uint16_t len)
484 {
485  uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
486  vtvl_gan_put(buf, tag, len);
487  return buf;
488 }
489 
490 
491 static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
492  uint16_t len, const uint8_t *val)
493 {
494  uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
495  vtvlv_gan_put(buf, tag, len, val);
496  return buf;
497 }
498 
499 /* TLV parsing */
500 
502 struct tlv_p_entry {
503  uint16_t len;
504  const uint8_t *val;
505 };
506 
508 enum tlv_type {
518 };
519 
521 struct tlv_def {
522  enum tlv_type type;
523  uint8_t fixed_len;
524 };
525 
528  struct tlv_def def[256];
529 };
530 
532 struct tlv_parsed {
533  struct tlv_p_entry lv[256];
534 };
535 
536 extern struct tlv_definition tvlv_att_def;
537 extern struct tlv_definition vtvlv_gan_att_def;
538 
539 int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
540  const struct tlv_definition *def,
541  const uint8_t *buf, int buf_len);
542 int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
543  const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
544 int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
545  const struct tlv_definition *def, const uint8_t *buf, int buf_len,
546  uint8_t lv_tag, uint8_t lv_tag2);
547 /* take a master (src) tlv def and fill up all empty slots in 'dst' */
548 void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
549 
550 int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
551  unsigned int len, const uint8_t *val);
552 int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp);
553 int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
554  const uint8_t *tag_order, unsigned int tag_order_len);
555 
556 #define TLVP_PRESENT(x, y) (!!((x)->lv[y].val))
557 #define TLVP_LEN(x, y) (x)->lv[y].len
558 #define TLVP_VAL(x, y) (x)->lv[y].val
559 
560 #define TLVP_PRES_LEN(tp, tag, min_len) \
561  (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
562 
573 #define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
574 
581 #define TLVP_GET_MINLEN(_tp, tag, min_len) \
582  (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
583 
590 #define TLVP_VAL_MINLEN(_tp, tag, min_len) \
591  (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL)
592 
593 
600 static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
601 {
602  const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1);
603 
604  if (res)
605  return res[0];
606 
607  return default_val;
608 }
609 
615 static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
616 {
617  uint16_t res;
618  memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
619  return res;
620 }
621 
627 static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
628 {
629  uint32_t res;
630  memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
631  return res;
632 }
633 
639 static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
640 {
641  return osmo_load16be(TLVP_VAL(tp, pos));
642 }
643 
649 static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
650 {
651  return osmo_load32be(TLVP_VAL(tp, pos));
652 }
653 
654 
655 struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
656 int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
657 int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
658  size_t len, uint8_t **value);
659 int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
660  uint8_t tag, size_t len, uint8_t **value);
661 int osmo_shift_tlv(uint8_t **data, size_t *data_len,
662  uint8_t *tag, uint8_t **value, size_t *value_len);
663 int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
664  uint8_t tag, uint8_t **value, size_t *value_len);
665 int osmo_shift_lv(uint8_t **data, size_t *data_len,
666  uint8_t **value, size_t *value_len);
667 
668 #define MSG_DEF(name, mand_ies, flags) { name, mand_ies, ARRAY_SIZE(mand_ies), flags }
669 
672  const char *name;
674  const uint8_t *mand_ies;
676  uint8_t mand_count;
678  uint32_t flags;
679 };
682  uint16_t min_len;
684  const char *name;
685 };
686 
690  const char *name;
692  const struct tlv_definition *tlv_def;
694  struct osmo_tlv_prot_msg_def msg_def[256];
696  struct osmo_tlv_prot_ie_def ie_def[256];
698  const struct value_string *msgt_names;
699 };
700 
701 const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type);
702 const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei);
703 
704 int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
705  const struct tlv_parsed *tp, int log_subsys, const char *log_pfx);
706 
707 int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
708  struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
709  const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
710  int log_subsys, const char *log_pfx);
711 
712 static inline uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
713 {
714  return pdef->msg_def[msg_type].flags;
715 }
716 
717 
#define osmo_htons(x)
#define osmo_htonl(x)
uint8_t data[0]
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
static uint8_t * tl_put(uint8_t *buf, uint8_t tag, uint8_t len)
put (append) a TL field (a TLV field but omitting the value part).
Definition: tlv.h:126
static uint8_t * msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
put (append) a TV field to a Message buffers
Definition: tlv.h:374
int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, size_t len, uint8_t **value)
Advance the data pointer, subtract length and assign value pointer.
Definition: tlv_parser.c:478
#define TL16V_GROSS_LEN(x)
gross length of a TL16V type field
Definition: tlv.h:35
static uint8_t * tvl_put(uint8_t *buf, uint8_t tag, uint16_t len)
put (append) a TvL field (a TvLV with variable-size length, where the value part's length is already ...
Definition: tlv.h:193
static uint8_t * tl16_put(uint8_t *buf, uint8_t tag, uint16_t len)
put (append) a TL16 field.
Definition: tlv.h:155
static uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
Retrieve (possibly unaligned) TLV element and convert to host byte order.
Definition: tlv.h:639
static uint8_t * msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
put (append) a LV field to a Message buffers
Definition: tlv.h:358
static uint8_t * msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
put (append) a TLV field to a Message buffers
Definition: tlv.h:366
#define TLVP_VAL(x, y)
Definition: tlv.h:558
static uint8_t * msgb_tvlv_put_32be(struct msgb *msg, uint8_t tag, uint32_t val)
put (append) a TvLV field containing a big-endian 16bit value to msgb.
Definition: tlv.h:290
#define TVLV_MAX_ONEBYTE
maximum length of TLV of one byte length
Definition: tlv.h:42
static uint8_t * vt_gan_put(uint8_t *buf, uint16_t tag)
put (append) a variable-length tag or variable-length length *
Definition: tlv.h:207
int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, const struct tlv_definition *def, const uint8_t *buf, int buf_len)
Parse a single TLV encoded IE.
Definition: tlv_parser.c:234
int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp, const uint8_t *tag_order, unsigned int tag_order_len)
Encode a set of decoded TLVs according to a given definition and IE order into a message buffer.
Definition: tlv_parser.c:199
tlv_type
TLV type.
Definition: tlv.h:508
int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, uint8_t tag, size_t len, uint8_t **value)
Match tag, check length and assign value pointer.
Definition: tlv_parser.c:506
#define TLVP_VAL_MINLEN(_tp, tag, min_len)
Like TLVP_VAL(), but enforcing a minimum val length.
Definition: tlv.h:590
struct tlv_definition vtvlv_gan_att_def
Definition: tlv_parser.c:50
static uint8_t * msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val)
push 1-byte tagged value
Definition: tlv.h:437
static uint8_t * vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
Definition: tlv.h:220
int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp)
Encode a set of decoded TLVs according to a given definition into a message buffer.
Definition: tlv_parser.c:173
static uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
Align given TLV element with 16 bit value to an even address.
Definition: tlv.h:615
static uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
Align given TLV element with 32 bit value to an address that is a multiple of 4.
Definition: tlv.h:627
#define LV_GROSS_LEN(x)
gross length of a LV type field
Definition: tlv.h:29
static uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
gross length of vTvLV (tag+len+val)
Definition: tlv.h:78
int osmo_shift_lv(uint8_t **data, size_t *data_len, uint8_t **value, size_t *value_len)
Extract LV and advance data pointer + subtract length.
Definition: tlv_parser.c:612
static uint8_t * msgb_tvl_put(struct msgb *msg, uint8_t tag, uint16_t len)
put (append) a TvL field to Message buffers, i.e.
Definition: tlv.h:268
int tlv_parse2(struct tlv_parsed *dec, int dec_multiples, const struct tlv_definition *def, const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2)
Like tlv_parse(), but capable of decoding multiple occurences of the same IE.
Definition: tlv_parser.c:369
static uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
Obtain 1-byte TLV element.
Definition: tlv.h:600
static uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
Retrieve (possibly unaligned) TLV element and convert to host byte order.
Definition: tlv.h:649
const char * osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei)
get the IE name for given IEI in protocol pdef
Definition: tlv_parser.c:659
int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef, struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type, const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2, int log_subsys, const char *log_pfx)
Parse + Validate a TLV-encoded message against the protocol definition.
Definition: tlv_parser.c:729
static uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
gross length of vTvL header (tag+len)
Definition: tlv.h:64
static uint16_t TVLV_GROSS_LEN(uint16_t len)
gross length of a TVLV type field
Definition: tlv.h:55
static uint8_t * msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val)
Definition: tlv.h:257
void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src)
take a master (src) tlv_definition and fill up all empty slots in 'dst'
Definition: tlv_parser.c:449
static uint8_t * tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, const uint8_t *val)
put (append) a TvLV field
Definition: tlv.h:175
static uint8_t * msgb_tl_put(struct msgb *msg, uint8_t tag)
put (append) a TL fields to a Message buffers
Definition: tlv.h:399
static uint8_t * msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag, uint16_t len, const uint8_t *val)
put (append) a vTvLV field to Message buffers
Definition: tlv.h:297
static uint8_t * msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
put (append) a TV16 field (network order) to the given msgb
Definition: tlv.h:411
static uint8_t * msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
push (prepend) a TV field to a Message buffers
Definition: tlv.h:444
static uint8_t * msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag, uint16_t len)
Definition: tlv.h:482
static uint8_t * msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val)
put (append) a TL16V field to Message buffers
Definition: tlv.h:250
static uint8_t * msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val)
put (append) a TvLV field to Message buffers
Definition: tlv.h:275
static uint8_t * tv_put(uint8_t *buf, uint8_t tag, uint8_t val)
put (append) a TV field
Definition: tlv.h:325
#define TLV16_GROSS_LEN(x)
gross length of a TLV16 type field
Definition: tlv.h:33
#define L16TV_GROSS_LEN(x)
gross length of a L16TV type field
Definition: tlv.h:37
static uint8_t * msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, const uint8_t *val)
put (append) a L16TV field to Message buffers
Definition: tlv.h:305
static uint8_t * msgb_tv32_put(struct msgb *msg, uint8_t tag, uint32_t val)
put (append) a TV32 field (network order) to the given msgb
Definition: tlv.h:419
int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src)
Merge all tlv_parsed attributes of 'src' into 'dst'.
Definition: tlv_parser.c:105
static uint8_t * t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len, const uint8_t *val)
put (append) a TL16V field
Definition: tlv.h:164
static uint8_t * v_put(uint8_t *buf, uint8_t val)
put (append) a V field
Definition: tlv.h:318
int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2)
Parse an entire buffer of TLV encoded Information Elements.
Definition: tlv_parser.c:346
static uint8_t * msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag, uint16_t len, const uint8_t *val)
Definition: tlv.h:491
int osmo_shift_tlv(uint8_t **data, size_t *data_len, uint8_t *tag, uint8_t **value, size_t *value_len)
Extract TLV and advance data pointer + subtract length.
Definition: tlv_parser.c:572
struct tlv_definition tvlv_att_def
Definition: tlv_parser.c:49
static uint8_t * msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val)
push (prepend) a TvLV field to a Message buffers
Definition: tlv.h:472
static uint8_t * tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, const uint8_t *val)
Append a TLV field, a Tag-Length-Value field.
Definition: tlv.h:111
static uint8_t * lv_put(uint8_t *buf, uint8_t len, const uint8_t *val)
put (append) a LV field
Definition: tlv.h:96
static uint8_t * msgb_tv32_push(struct msgb *msg, uint8_t tag, uint32_t val)
push (prepend) a TV32 field to a Message buffers
Definition: tlv.h:462
int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type, const struct tlv_parsed *tp, int log_subsys, const char *log_pfx)
Validate an already TLV-decoded message against the protocol definition.
Definition: tlv_parser.c:677
static uint8_t * msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
push (prepend) a TLV field to a Message buffers
Definition: tlv.h:429
osmo_tlv_parser_error
error return codes of various TLV parser functions
Definition: tlv.h:45
int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, uint8_t tag, uint8_t **value, size_t *value_len)
Verify TLV header and advance data / subtract length.
Definition: tlv_parser.c:544
#define T16LV_GROSS_LEN(x)
gross length of a T16LV type field
Definition: tlv.h:39
const char * osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
get the message name for given msg_type in protocol pdef
Definition: tlv_parser.c:646
static uint8_t * vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len, const uint8_t *val)
Definition: tlv.h:229
static uint8_t * msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
put (append) a TLV16 field to Message buffers
Definition: tlv.h:243
#define TLV_GROSS_LEN(x)
gross length of a TLV type field
Definition: tlv.h:31
static uint8_t * msgb_v_put(struct msgb *msg, uint8_t val)
put (append) a V field to a Message buffers
Definition: tlv.h:391
static uint8_t * tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, const uint16_t *val)
put (append) a TLV16 field
Definition: tlv.h:134
static uint8_t * tv_fixed_put(uint8_t *buf, uint8_t tag, unsigned int len, const uint8_t *val)
put (append) a TVfixed field
Definition: tlv.h:334
struct tlv_parsed * osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx)
Copy tlv_parsed using given talloc context.
Definition: tlv_parser.c:70
int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag, unsigned int len, const uint8_t *val)
Encode a single TLV into given message buffer.
Definition: tlv_parser.c:132
static uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
Definition: tlv.h:712
static uint8_t * msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
push (prepend) a TV16 field to a Message buffers
Definition: tlv.h:453
static uint8_t * tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, const uint8_t *val)
put (append) a TL16V field
Definition: tlv.h:144
static uint8_t * tv16_put(uint8_t *buf, uint8_t tag, uint16_t val)
put (append) a TV16 field
Definition: tlv.h:347
static uint8_t * msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, unsigned int len, const uint8_t *val)
put (append) a TVfixed field to a Message buffers
Definition: tlv.h:382
static uint8_t * msgb_tvlv_put_16be(struct msgb *msg, uint8_t tag, uint16_t val)
put (append) a TvLV field containing a big-endian 16bit value to msgb.
Definition: tlv.h:283
@ TLV_TYPE_TLV
tag-length-value
Definition: tlv.h:513
@ TLV_TYPE_TV
tag-value (8bit)
Definition: tlv.h:512
@ TLV_TYPE_FIXED
fixed-length value-only
Definition: tlv.h:510
@ TLV_TYPE_TvLV
tag, variable length, value
Definition: tlv.h:515
@ TLV_TYPE_NONE
no type
Definition: tlv.h:509
@ TLV_TYPE_TL16V
tag, 16 bit length, value
Definition: tlv.h:514
@ TLV_TYPE_SINGLE_TV
tag and value (both 4 bit) in 1 byte
Definition: tlv.h:516
@ TLV_TYPE_T
tag-only
Definition: tlv.h:511
@ TLV_TYPE_vTvLV_GAN
variable-length tag, variable-length length
Definition: tlv.h:517
@ OSMO_TLVP_ERR_MAND_IE_MISSING
Definition: tlv.h:50
@ OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE
Definition: tlv.h:48
@ OSMO_TLVP_ERR_OFS_BEYOND_BUFFER
Definition: tlv.h:46
@ OSMO_TLVP_ERR_IE_TOO_SHORT
Definition: tlv.h:51
@ OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER
Definition: tlv.h:47
uint8_t msg_type
Definition: gsm_04_08.h:2
uint8_t type
Definition: gsm_04_08_gprs.h:7
uint8_t len
Definition: gsm_04_11.h:0
uint8_t msg[0]
Definition: gsm_08_08.h:8
uint8_t iei
Definition: gsm_08_58.h:2
uint8_t res
Osmocom TLV protocol definition.
Definition: tlv.h:688
struct osmo_tlv_prot_ie_def ie_def[256]
definition of IE for each 8-bit tag
Definition: tlv.h:696
const struct tlv_definition * tlv_def
TLV parser definition (optional)
Definition: tlv.h:692
struct osmo_tlv_prot_msg_def msg_def[256]
definition of each message (8-bit message type)
Definition: tlv.h:694
const struct value_string * msgt_names
value_string array of message type names (legacy, if not populated in msg_def)
Definition: tlv.h:698
const char * name
human-readable name of protocol
Definition: tlv.h:690
Definition: tlv.h:680
const char * name
huamn-readable name (optional)
Definition: tlv.h:684
uint16_t min_len
minimum length of IE value part, in octets
Definition: tlv.h:682
Definition: tlv.h:670
const uint8_t * mand_ies
array of mandatory IEs
Definition: tlv.h:674
const char * name
human-readable name of message type (optional)
Definition: tlv.h:672
uint32_t flags
user-defined flags (like uplink/downlink/...)
Definition: tlv.h:678
uint8_t mand_count
number of entries in 'mand_ies' above
Definition: tlv.h:676
Definition of a single IE (Information Element)
Definition: tlv.h:521
uint8_t fixed_len
length in case of TLV_TYPE_FIXED
Definition: tlv.h:523
enum tlv_type type
TLV type.
Definition: tlv.h:522
Definition of All 256 IE / TLV.
Definition: tlv.h:527
struct tlv_def def[256]
Definition: tlv.h:528
Entry in a TLV parser array.
Definition: tlv.h:502
uint16_t len
length
Definition: tlv.h:503
const uint8_t * val
pointer to value
Definition: tlv.h:504
result of the TLV parser
Definition: tlv.h:532
struct tlv_p_entry lv[256]
Definition: tlv.h:533