2008-12-23 20:25:15 +00:00
|
|
|
#ifndef _TLV_H
|
|
|
|
#define _TLV_H
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-02-06 12:51:39 +00:00
|
|
|
#include <openbsc/msgb.h>
|
|
|
|
|
2009-10-24 08:04:02 +00:00
|
|
|
/* Terminology / wording
|
|
|
|
tag length value (in bits)
|
|
|
|
|
|
|
|
V - - 8
|
|
|
|
LV - 8 N * 8
|
|
|
|
TLV 8 8 N * 8
|
|
|
|
TL16V 8 16 N * 8
|
|
|
|
TLV16 8 8 N * 16
|
|
|
|
TvLV 8 8/16 N * 8
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-05-23 06:39:58 +00:00
|
|
|
#define LV_GROSS_LEN(x) (x+1)
|
2008-12-23 20:25:15 +00:00
|
|
|
#define TLV_GROSS_LEN(x) (x+2)
|
|
|
|
#define TLV16_GROSS_LEN(x) ((2*x)+2)
|
2008-12-26 20:25:35 +00:00
|
|
|
#define TL16V_GROSS_LEN(x) (x+3)
|
2010-02-09 15:26:49 +00:00
|
|
|
#define L16TV_GROSS_LEN(x) (x+3)
|
2008-12-23 20:25:15 +00:00
|
|
|
|
2009-10-24 08:04:02 +00:00
|
|
|
#define TVLV_MAX_ONEBYTE 0x7f
|
|
|
|
|
|
|
|
static inline u_int16_t TVLV_GROSS_LEN(u_int16_t len)
|
|
|
|
{
|
|
|
|
if (len <= TVLV_MAX_ONEBYTE)
|
|
|
|
return TLV_GROSS_LEN(len);
|
|
|
|
else
|
|
|
|
return TL16V_GROSS_LEN(len);
|
|
|
|
}
|
|
|
|
|
2009-02-06 12:51:39 +00:00
|
|
|
/* TLV generation */
|
|
|
|
|
2009-05-23 06:39:58 +00:00
|
|
|
static inline u_int8_t *lv_put(u_int8_t *buf, u_int8_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
*buf++ = len;
|
|
|
|
memcpy(buf, val, len);
|
|
|
|
return buf + len;
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
static inline u_int8_t *tlv_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
*buf++ = tag;
|
|
|
|
*buf++ = len;
|
|
|
|
memcpy(buf, val, len);
|
|
|
|
return buf + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u_int8_t *tlv16_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
|
|
|
|
const u_int16_t *val)
|
|
|
|
{
|
|
|
|
*buf++ = tag;
|
|
|
|
*buf++ = len;
|
|
|
|
memcpy(buf, val, len*2);
|
|
|
|
return buf + len*2;
|
|
|
|
}
|
|
|
|
|
2008-12-26 20:25:35 +00:00
|
|
|
static inline u_int8_t *tl16v_put(u_int8_t *buf, u_int8_t tag, u_int16_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
*buf++ = tag;
|
|
|
|
*buf++ = len >> 8;
|
|
|
|
*buf++ = len & 0xff;
|
|
|
|
memcpy(buf, val, len);
|
|
|
|
return buf + len*2;
|
|
|
|
}
|
|
|
|
|
2009-10-24 08:04:02 +00:00
|
|
|
static inline u_int8_t *tvlv_put(u_int8_t *buf, u_int8_t tag, u_int16_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *ret;
|
|
|
|
|
|
|
|
if (len <= TVLV_MAX_ONEBYTE) {
|
|
|
|
ret = tlv_put(buf, tag, len, val);
|
|
|
|
buf[1] |= 0x80;
|
|
|
|
} else
|
|
|
|
ret = tl16v_put(buf, tag, len, val);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
static inline u_int8_t *msgb_tlv16_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int16_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
|
|
|
|
return tlv16_put(buf, tag, len, val);
|
|
|
|
}
|
|
|
|
|
2008-12-26 20:25:35 +00:00
|
|
|
static inline u_int8_t *msgb_tl16v_put(struct msgb *msg, u_int8_t tag, u_int16_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
|
|
|
|
return tl16v_put(buf, tag, len, val);
|
|
|
|
}
|
|
|
|
|
2009-10-24 08:04:02 +00:00
|
|
|
static inline u_int8_t *msgb_tvlv_put(struct msgb *msg, u_int8_t tag, u_int16_t len,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
|
|
|
|
return tvlv_put(buf, tag, len, val);
|
|
|
|
}
|
|
|
|
|
2010-02-09 15:26:49 +00:00
|
|
|
static inline u_int8_t *msgb_l16tv_put(struct msgb *msg, u_int16_t len, u_int8_t tag,
|
|
|
|
const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
|
|
|
|
|
|
|
|
*buf++ = len >> 8;
|
|
|
|
*buf++ = len & 0xff;
|
|
|
|
*buf++ = tag;
|
|
|
|
memcpy(buf, val, len);
|
|
|
|
return buf + len;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:39:58 +00:00
|
|
|
static inline u_int8_t *v_put(u_int8_t *buf, u_int8_t val)
|
|
|
|
{
|
|
|
|
*buf++ = val;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
static inline u_int8_t *tv_put(u_int8_t *buf, u_int8_t tag,
|
|
|
|
u_int8_t val)
|
|
|
|
{
|
|
|
|
*buf++ = tag;
|
|
|
|
*buf++ = val;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-12-19 15:42:06 +00:00
|
|
|
/* 'val' is still in host byte order! */
|
2008-12-27 01:55:51 +00:00
|
|
|
static inline u_int8_t *tv16_put(u_int8_t *buf, u_int8_t tag,
|
|
|
|
u_int16_t val)
|
|
|
|
{
|
|
|
|
*buf++ = tag;
|
|
|
|
*buf++ = val >> 8;
|
|
|
|
*buf++ = val & 0xff;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:39:58 +00:00
|
|
|
static inline u_int8_t *msgb_lv_put(struct msgb *msg, u_int8_t len, const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
|
|
|
|
return lv_put(buf, len, val);
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
static inline u_int8_t *msgb_tlv_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
|
|
|
|
return tlv_put(buf, tag, len, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u_int8_t *msgb_tv_put(struct msgb *msg, u_int8_t tag, u_int8_t val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, 2);
|
|
|
|
return tv_put(buf, tag, val);
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:39:58 +00:00
|
|
|
static inline u_int8_t *msgb_v_put(struct msgb *msg, u_int8_t val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, 1);
|
|
|
|
return v_put(buf, val);
|
|
|
|
}
|
|
|
|
|
2008-12-27 01:55:51 +00:00
|
|
|
static inline u_int8_t *msgb_tv16_put(struct msgb *msg, u_int8_t tag, u_int16_t val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_put(msg, 3);
|
|
|
|
return tv16_put(buf, tag, val);
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
static inline u_int8_t *msgb_tlv_push(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
|
|
|
|
return tlv_put(buf, tag, len, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u_int8_t *msgb_tv_push(struct msgb *msg, u_int8_t tag, u_int8_t val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_push(msg, 2);
|
|
|
|
return tv_put(buf, tag, val);
|
|
|
|
}
|
|
|
|
|
2008-12-27 01:55:51 +00:00
|
|
|
static inline u_int8_t *msgb_tv16_push(struct msgb *msg, u_int8_t tag, u_int16_t val)
|
|
|
|
{
|
|
|
|
u_int8_t *buf = msgb_push(msg, 3);
|
|
|
|
return tv16_put(buf, tag, val);
|
|
|
|
}
|
|
|
|
|
2009-02-06 12:51:39 +00:00
|
|
|
/* TLV parsing */
|
|
|
|
|
|
|
|
struct tlv_p_entry {
|
2009-02-15 03:34:15 +00:00
|
|
|
u_int16_t len;
|
2009-02-21 12:59:22 +00:00
|
|
|
const u_int8_t *val;
|
2009-02-06 12:51:39 +00:00
|
|
|
};
|
|
|
|
|
2009-02-15 03:34:15 +00:00
|
|
|
enum tlv_type {
|
2010-01-10 16:45:23 +00:00
|
|
|
TLV_TYPE_NONE,
|
2009-02-15 03:34:15 +00:00
|
|
|
TLV_TYPE_FIXED,
|
|
|
|
TLV_TYPE_T,
|
|
|
|
TLV_TYPE_TV,
|
|
|
|
TLV_TYPE_TLV,
|
|
|
|
TLV_TYPE_TL16V,
|
2009-10-24 08:04:02 +00:00
|
|
|
TLV_TYPE_TvLV,
|
2009-02-15 03:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tlv_def {
|
|
|
|
enum tlv_type type;
|
|
|
|
u_int8_t fixed_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tlv_definition {
|
|
|
|
struct tlv_def def[0xff];
|
|
|
|
};
|
|
|
|
|
2009-02-14 19:07:10 +00:00
|
|
|
struct tlv_parsed {
|
2009-02-06 12:51:39 +00:00
|
|
|
struct tlv_p_entry lv[0xff];
|
|
|
|
};
|
|
|
|
|
2009-10-24 08:04:02 +00:00
|
|
|
extern struct tlv_definition tvlv_att_def;
|
|
|
|
|
2009-10-26 19:19:59 +00:00
|
|
|
int tlv_parse_one(u_int8_t *o_tag, u_int16_t *o_len, const u_int8_t **o_val,
|
|
|
|
const struct tlv_definition *def,
|
|
|
|
const u_int8_t *buf, int buf_len);
|
2009-02-21 12:59:22 +00:00
|
|
|
int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
|
2009-05-23 06:39:58 +00:00
|
|
|
const u_int8_t *buf, int buf_len, u_int8_t lv_tag, u_int8_t lv_tag2);
|
2010-01-10 16:45:23 +00:00
|
|
|
/* take a master (src) tlvdev and fill up all empty slots in 'dst' */
|
|
|
|
void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
|
2009-02-14 19:07:10 +00:00
|
|
|
|
|
|
|
#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
|
|
|
|
#define TLVP_LEN(x, y) (x)->lv[y].len
|
|
|
|
#define TLVP_VAL(x, y) (x)->lv[y].val
|
2008-12-27 01:55:51 +00:00
|
|
|
|
2008-12-23 20:25:15 +00:00
|
|
|
#endif /* _TLV_H */
|