summaryrefslogtreecommitdiff
path: root/nest/a-tlv.c
blob: 13e05f431522d14be12d7318b12acdc541914d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "lib/tunnel_encaps.h"
#include "filter/data.h"

static int
walk_tlvlist(const struct te_tlvlist *set, const struct te_visitor *visitor, void *opaque)
{
  if (!set)
    return 0;

  struct te_tlv *t;
  struct te_context ctx = { .opaque = opaque, };

  WALK_LIST(t, set->tlv)
  {
    if (visitor->visit_tlv && visitor->visit_tlv(t->type, &ctx) < 0)
      continue;

    for (uint j=0; j < t->len; j++)
    {
      const struct te_subtlv *st = &t->st[j];

      if (visitor->visit_subtlv && visitor->visit_subtlv(st->type, &ctx) < 0)
        continue;

      switch (st->type)
      {
      case TLV_ENCAPSULATION:
        if (visitor->visit_encap && visitor->visit_encap(&st->u.tunnel_encap, &ctx) < 0)
          return -1;
        break;
      case TLV_COLOR:
        if (visitor->visit_color && visitor->visit_color(st->u.color, &ctx) < 0)
          return -1;
        break;
      case TLV_TUNNEL_ENDPOINT:
        if (visitor->visit_ep && visitor->visit_ep(&st->u.tunnel_endpoint, &ctx) < 0)
          return -1;
        break;
      case TLV_UDP_DEST_PORT:
        if (visitor->visit_udp_dest_port && visitor->visit_udp_dest_port(st->u.udp_dest_port, &ctx) < 0)
          return -1;
        break;
      default:
        if (visitor->visit_unknown && visitor->visit_unknown(st->type, &st->u.unknown, &ctx) < 0)
          return -1;
        break;
      }

      if (visitor->visit_subtlv_end)
        visitor->visit_subtlv_end(st->type, &ctx); /* TODO use return value? */
    }

    if (visitor->visit_tlv_end)
      visitor->visit_tlv_end(&ctx); /* TODO use return value? */
  }

  return 0;
}

static void calc_elems(const struct f_tree UNUSED *t, void *ptr)
{
  uint *len = ptr;
  (*len)++;
}

static void put_elems(const struct f_tree *t, void *ptr)
{
  struct te_tlv *tlv = ptr;
  const struct te_subtlv *st = &t->from.val.st;
  tlv->len--;
  tlv->st[tlv->len] = *st;
  if (st->type == TLV_ENCAPSULATION)
    tlv->type = st->u.tunnel_encap.type;
  DBG("put_elems: type %d\n", st->type);
}

const struct te_tlv *tlv_alloc(struct linpool *pool, const struct f_tree *set)
{
  uint len = 0;

  tree_walk(set, calc_elems, &len);

  if (!len)
    return NULL;

  struct te_tlv *res = lp_alloc(pool, sizeof(struct te_tlv) + len * sizeof(struct te_subtlv));

  res->len = len;
  tree_walk(set, put_elems, res);

  res->len = len;		/* Len was reset during tree_walk. */

  for (uint i=0; i < len; i++)
  {
    DBG("tlv_alloc: type[%d]: %d\n", i, res->st[i].type);
  }
  return res;
}

int tlv_set_format(const struct te_tlvlist *set, int UNUSED from, byte *buf, uint size)
{
  const struct te_visitor *visitor = te_get_format_visitor();
  struct te_buffer buffer = {.pos=buf, .size=size, .tlv=0, .subtlv=0};

  if (set)
    walk_tlvlist(set, visitor, &buffer);

  int l = bsnprintf(buffer.pos, buffer.size, "%s", buffer.tlv?"}":"<empty>");
  if (l < 0)
    return -1;
  ADVANCE(buffer.pos, buffer.size, l);
  return buffer.pos - buf;
}

int
tlv_set_contains(const struct te_tlvlist *list, const struct te_tlv UNUSED *val)
{
  if (!list)
    return 0;

  /* FIXME */
  return 0;
}

static void tlv_add_copy(struct linpool *pool, struct te_tlvlist *list, const struct te_tlv *t)
{
  uint size = sizeof(struct te_tlv) + t->len * sizeof(struct te_subtlv);
  struct te_tlv *tlv = lp_alloc(pool, size);
  memcpy(tlv, t, size);
  memset(&tlv->n, 0, sizeof(tlv->n));
  add_tail(&list->tlv, &tlv->n);
  DBG("tlv_add_copy: src type %d, dst type %d\n", t->st[0].type, tlv->st[0].type);
}

const struct te_tlvlist *
tlv_set_add(struct linpool *pool, const struct te_tlvlist *list, const struct te_tlv *val)
{
  if (tlv_set_contains(list, val))
    return list;

  struct te_tlvlist *res = lp_alloc(pool, sizeof(struct te_tlvlist));

  init_list(&res->tlv);

  DBG("tlv_set_add: first type %d, second type %d\n", list?((const struct te_tlv *)HEAD(list->tlv))->st[0].type:-1, val->st[0].type);

  if (list)
  {
    struct te_tlv *n;
    WALK_LIST(n, list->tlv)
    {
      tlv_add_copy(pool, res, n);
    }
  }

  tlv_add_copy(pool, res, val);
  return res;
}

const struct te_tlvlist *
tlv_set_union(struct linpool UNUSED *pool, const struct te_tlvlist *l1, const struct te_tlvlist *l2)
{
  if (!l1)
    return l2;
  if (!l2)
    return l1;

  /* FIXME */
  void *res = NULL;
  return res;
}

static int
subtlv_same(const struct te_subtlv *st1, const struct te_subtlv *st2)
{
  if (st1 == NULL && st2 == NULL)
    return 1;

  if (st1 == NULL || st2 == NULL)
    return 0;

  if (st1->type != st2->type)
    return 0;

  switch(st1->type)
  {
  case TLV_ENCAPSULATION:
    if (st1->u.tunnel_encap.type != st2->u.tunnel_encap.type ||
	st1->u.tunnel_encap.length != st2->u.tunnel_encap.length ||
	memcmp(st1->u.tunnel_encap.data, st2->u.tunnel_encap.data, st1->u.tunnel_encap.length))
      return 0;
    break;
  case TLV_COLOR:
    if (st1->u.color != st2->u.color)
      return 0;
    break;
  case TLV_TUNNEL_ENDPOINT:
    if (st1->u.tunnel_endpoint.reserved != st2->u.tunnel_endpoint.reserved ||
	st1->u.tunnel_endpoint.af != st2->u.tunnel_endpoint.af ||
	st1->u.tunnel_endpoint.af && ipa_compare(st1->u.tunnel_endpoint.ip, st2->u.tunnel_endpoint.ip))
      return 0;
    break;
  case TLV_UDP_DEST_PORT:
    if (st1->u.udp_dest_port != st2->u.udp_dest_port)
      return 0;
    break;
  default:
    if (st1->u.unknown.length != st2->u.unknown.length ||
	memcmp(st1->u.unknown.data, st2->u.unknown.data, st1->u.unknown.length))
      return 0;
    break;
  }

  return 1;
}

static int
tlv_same(const struct te_tlv *t1, const struct te_tlv *t2)
{
  if (t1 == NULL && t2 == NULL)
    return 1;

  if (t1 == NULL || t2 == NULL)
    return 0;

  if (t1->type != t2->type ||
      t1->len != t2->len)
    return 0;

  for (uint i=0; i < t1->len; i++)
    {
      if (!subtlv_same(&t1->st[i], &t2->st[i]))
	return 0;
    }

  return 1;
}

int
tlvlist_same(const struct te_tlvlist *tl1, const struct te_tlvlist *tl2)
{
  if (tl1 == NULL && tl2 == NULL)
    return 1;

  if (tl1 == NULL || tl2 == NULL)
    return 0;

  const struct te_tlv *t1 = HEAD(tl1->tlv);
  const struct te_tlv *t2 = HEAD(tl2->tlv);

  while (NODE_VALID(t1) || NODE_VALID(t2))
  {
    if (!NODE_VALID(t1) || !NODE_VALID(t2))
      return 0;

    /* Both are valid. */
    if (!tlv_same(t1, t2))
      return 0;
  }

  return 1;
}

static int
tlvlist_calc_tunnel_encap_new(const struct te_tlvlist *t, struct adata *ad)
{
  if (t == NULL)
    return 0;

  uint size = ad ? ad->length : 0;
  byte *p = ad ? ad->data : NULL;

  uint buf_size = te_init_encode_visitor(NULL, NULL, 0);
  struct tlv_buffer *buf = alloca(buf_size);

  if (te_init_encode_visitor(buf, p, size) < 0)
    return -1;

  if (walk_tlvlist(t, te_get_encode_visitor(), buf) < 0)
    return -1;

  return te_get_encode_length(buf);
}

struct adata *
tlvlist_encode_tunnel_encap(struct linpool *pool, const struct te_tlvlist *tl)
{
  int len = tlvlist_calc_tunnel_encap_new(tl, NULL);

  if (len < 0)
    return NULL;

  struct adata *ad = lp_alloc(pool, sizeof(struct adata) + len);
  ad->length = len;

  if (tlvlist_calc_tunnel_encap_new(tl, ad) < 0)
    return NULL;

  return ad;
}

struct decoder_buf
{
  struct linpool *pool;
  struct te_tlvlist *tl;
  struct te_tlv *cur_tlv;
  struct te_subtlv *cur_subtlv;
  int cur_subtlv_index;
};

static int visitor_decode_tlv(int type, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  int count = te_count_subtlvs(ctx);

  if (count < 0)
    return -1;

  buf->cur_tlv = lp_allocz(buf->pool, sizeof(struct te_tlv) + count * sizeof(struct te_subtlv));
  buf->cur_tlv->type = type;
  return 0;
}

static int visitor_decode_tlv_end(struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  add_tail(&buf->tl->tlv, &buf->cur_tlv->n);
  buf->cur_tlv->len = buf->cur_subtlv_index;
  buf->cur_tlv = NULL;
  return 0;
}

static int
visitor_decode_subtlv(int type, struct te_context *ctx){
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv = &buf->cur_tlv->st[buf->cur_subtlv_index];
  buf->cur_subtlv->type = type;
  return 0;
}

static int
visitor_decode_subtlv_end(int UNUSED type, struct te_context *ctx){
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv_index++;
  return 0;
}

static int
visitor_decode_encap(const struct te_encap *encap, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv->u.tunnel_encap = *encap;
  return 0;
}

static int
visitor_decode_ep(const struct te_endpoint *ep, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv->u.tunnel_endpoint = *ep;
  return 0;
}

static int
visitor_decode_color(u32 color, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv->u.color = color;
  return 0;
}

static int
visitor_decode_udp_dest_port(u16 port, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv->u.udp_dest_port = port;
  return 0;
}

static int
visitor_decode_unknown(int UNUSED type, const struct te_unknown *unknown, struct te_context *ctx)
{
  struct decoder_buf *buf = ctx->opaque;

  buf->cur_subtlv->u.unknown = *unknown;
  return 0;
}

struct te_tlvlist *tlvlist_decode_tunnel_encap(struct linpool *pool,
					       const struct adata *ad)
{
  struct te_tlvlist *tl = lp_alloc(pool, sizeof(struct te_tlvlist));
  struct decoder_buf buf = { .pool = pool, .tl = tl, };
  struct te_visitor visitor = {
    .visit_tlv = visitor_decode_tlv,
    .visit_tlv_end = visitor_decode_tlv_end,
    .visit_subtlv = visitor_decode_subtlv,
    .visit_subtlv_end = visitor_decode_subtlv_end,
    .visit_encap = visitor_decode_encap,
    .visit_ep = visitor_decode_ep,
    .visit_color = visitor_decode_color,
    .visit_udp_dest_port = visitor_decode_udp_dest_port,
    .visit_unknown = visitor_decode_unknown,
  };

  init_list(&tl->tlv);

  int res = walk_tunnel_encap(ad, &visitor, &buf);
  if (res < 0)
    return NULL;

  return tl;
}