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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
/* $OpenBSD: rde_attr.c,v 1.11 2004/02/19 13:54:58 claudio Exp $ */
/*
* Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bgpd.h"
#include "ensure.h"
#include "rde.h"
/* attribute specific functions */
#define UPD_READ(t, p, plen, n) \
do { \
memcpy(t, p, n); \
p += n; \
plen += n; \
} while (0)
#define CHECK_FLAGS(s, t) \
(((s) & ~ATTR_EXTLEN) == (t))
#define F_ATTR_ORIGIN 0x01
#define F_ATTR_ASPATH 0x02
#define F_ATTR_NEXTHOP 0x04
#define F_ATTR_LOCALPREF 0x08
#define F_ATTR_MED 0x10
#define WFLAG(s, t) \
do { \
if ((s) & (t)) \
return (-1); \
(s) |= (t); \
} while (0)
void
attr_init(struct attr_flags *a)
{
bzero(a, sizeof(struct attr_flags));
a->origin = ORIGIN_INCOMPLETE;
TAILQ_INIT(&a->others);
}
int
attr_parse(u_char *p, u_int16_t len, struct attr_flags *a, int ebgp)
{
u_int32_t tmp32;
u_int16_t attr_len;
u_int16_t plen = 0;
u_int8_t flags;
u_int8_t type;
u_int8_t tmp8;
if (len < 3)
return (-1);
UPD_READ(&flags, p, plen, 1);
UPD_READ(&type, p, plen, 1);
if (flags & ATTR_EXTLEN) {
if (len - plen < 2)
return (-1);
UPD_READ(&attr_len, p, plen, 2);
} else {
UPD_READ(&tmp8, p, plen, 1);
attr_len = tmp8;
}
if (len - plen < attr_len)
return (-1);
switch (type) {
case ATTR_UNDEF:
/* error! */
return (-1);
case ATTR_ORIGIN:
if (attr_len != 1)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN))
return (-1);
UPD_READ(&a->origin, p, plen, 1);
if (a->origin > ORIGIN_INCOMPLETE)
return (-1);
WFLAG(a->wflags, F_ATTR_ORIGIN);
break;
case ATTR_ASPATH:
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN))
return (-1);
if (aspath_verify(p, attr_len) != 0)
return (-1);
WFLAG(a->wflags, F_ATTR_ASPATH);
a->aspath = aspath_create(p, attr_len);
/* XXX enforce remote-as == left most AS if not disabled */
plen += attr_len;
break;
case ATTR_NEXTHOP:
if (attr_len != 4)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN))
return (-1);
WFLAG(a->wflags, F_ATTR_NEXTHOP);
UPD_READ(&a->nexthop, p, plen, 4); /* network byte order */
/* XXX check if the nexthop is a valid IP address */
break;
case ATTR_MED:
if (attr_len != 4)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL))
return (-1);
WFLAG(a->wflags, F_ATTR_MED);
UPD_READ(&tmp32, p, plen, 4);
a->med = ntohl(tmp32);
break;
case ATTR_LOCALPREF:
if (attr_len != 4)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN))
return (-1);
if (ebgp) {
/* ignore local-pref attr for non ibgp peers */
a->lpref = 0; /* set a default value */
break;
}
WFLAG(a->wflags, F_ATTR_LOCALPREF);
UPD_READ(&tmp32, p, plen, 4);
a->lpref = ntohl(tmp32);
break;
case ATTR_ATOMIC_AGGREGATE:
if (attr_len != 0)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN))
return (-1);
goto optattr;
case ATTR_AGGREGATOR:
if (attr_len != 6)
return (-1);
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE))
return (-1);
goto optattr;
default:
optattr:
if (attr_optadd(a, flags, type, p, attr_len) == -1)
return (-1);
plen += attr_len;
break;
}
return (plen);
}
u_char *
attr_error(u_char *p, u_int16_t len, struct attr_flags *attr,
u_int8_t *suberr, u_int16_t *size)
{
struct attr *a;
u_int16_t attr_len;
u_int16_t plen = 0;
u_int8_t flags;
u_int8_t type;
u_int8_t tmp8;
*suberr = ERR_UPD_ATTRLEN;
*size = len;
if (len < 3)
return (p);
UPD_READ(&flags, p, plen, 1);
UPD_READ(&type, p, plen, 1);
if (flags & ATTR_EXTLEN) {
if (len - plen < 2)
return (p);
UPD_READ(&attr_len, p, plen, 2);
} else {
UPD_READ(&tmp8, p, plen, 1);
attr_len = tmp8;
}
if (len - plen < attr_len)
return (p);
*size = attr_len;
switch (type) {
case ATTR_UNDEF:
/* error! */
*suberr = ERR_UPD_UNSPECIFIC;
*size = 0;
return (NULL);
case ATTR_ORIGIN:
if (attr_len != 1)
return (p);
if (attr->wflags & F_ATTR_ORIGIN) {
*suberr = ERR_UPD_ATTRLIST;
*size = 0;
return (NULL);
}
UPD_READ(&tmp8, p, plen, 1);
if (tmp8 > ORIGIN_INCOMPLETE) {
*suberr = ERR_UPD_ORIGIN;
return (p);
}
break;
case ATTR_ASPATH:
if (attr->wflags & F_ATTR_ASPATH) {
*suberr = ERR_UPD_ATTRLIST;
*size = 0;
return (NULL);
}
if (CHECK_FLAGS(flags, ATTR_WELL_KNOWN)) {
/* malformed aspath detected by exclusion method */
*size = 0;
*suberr = ERR_UPD_ASPATH;
return (NULL);
}
break;
case ATTR_NEXTHOP:
if (attr_len != 4)
return (p);
if (attr->wflags & F_ATTR_NEXTHOP) {
*suberr = ERR_UPD_ATTRLIST;
*size = 0;
return (NULL);
}
if (CHECK_FLAGS(flags, ATTR_WELL_KNOWN)) {
/* malformed nexthop detected by exclusion method */
*suberr = ERR_UPD_NETWORK;
return (p);
}
break;
case ATTR_MED:
if (attr_len != 4)
return (p);
if (attr->wflags & F_ATTR_MED) {
*suberr = ERR_UPD_ATTRLIST;
*size = 0;
return (NULL);
}
break;
case ATTR_LOCALPREF:
if (attr_len != 4)
return (p);
if (attr->wflags & F_ATTR_LOCALPREF) {
*suberr = ERR_UPD_ATTRLIST;
*size = 0;
return (NULL);
}
break;
case ATTR_ATOMIC_AGGREGATE:
if (attr_len != 0)
return (p);
break;
case ATTR_AGGREGATOR:
if (attr_len != 6)
return (p);
break;
default:
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN)) {
*suberr = ERR_UPD_UNKNWN_WK_ATTR;
return (p);
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL)) {
*suberr = ERR_UPD_ATTRFLAGS;
return (p);
}
TAILQ_FOREACH(a, &attr->others, attr_l)
if (type == a->type) {
*size = NULL;
*suberr = ERR_UPD_ATTRLIST;
return (NULL);
}
*suberr = ERR_UPD_OPTATTR;
return (p);
}
/* can only be a attribute flag error */
*suberr = ERR_UPD_ATTRFLAGS;
return (p);
}
#undef UPD_READ
#undef WFLAG
u_int8_t
attr_missing(struct attr_flags *a, int ebgp)
{
if ((a->wflags & F_ATTR_ORIGIN) == 0)
return ATTR_ORIGIN;
if ((a->wflags & F_ATTR_ASPATH) == 0)
return ATTR_ASPATH;
if ((a->wflags & F_ATTR_NEXTHOP) == 0)
return ATTR_NEXTHOP;
if (!ebgp)
if ((a->wflags & F_ATTR_LOCALPREF) == 0)
return ATTR_LOCALPREF;
return 0;
}
int
attr_compare(struct attr_flags *a, struct attr_flags *b)
{
struct attr *oa, *ob;
int r;
if (a->origin > b->origin)
return (1);
if (a->origin < b->origin)
return (-1);
if (a->nexthop > b->nexthop)
return (1);
if (a->nexthop < b->nexthop)
return (-1);
if (a->med > b->med)
return (1);
if (a->med < b->med)
return (-1);
if (a->lpref > b->lpref)
return (1);
if (a->lpref < b->lpref)
return (-1);
r = aspath_compare(a->aspath, b->aspath);
if (r > 0)
return (1);
if (r < 0)
return (-1);
for (oa = TAILQ_FIRST(&a->others), ob = TAILQ_FIRST(&b->others);
oa != NULL && ob != NULL;
oa = TAILQ_NEXT(oa, attr_l), ob = TAILQ_NEXT(ob, attr_l)) {
if (oa->type > ob->type)
return (1);
if (oa->type < ob->type)
return (-1);
if (oa->len > ob->len)
return (1);
if (oa->len < ob->len)
return (-1);
r = memcmp(oa->data, ob->data, oa->len);
if (r > 0)
return (1);
if (r < 0)
return (-1);
}
if (oa != NULL)
return (1);
if (ob != NULL)
return (-1);
return (0);
}
void
attr_copy(struct attr_flags *t, struct attr_flags *s)
{
struct attr *os;
/*
* first copy the full struct, then replace the path and tags with
* a own copy.
*/
memcpy(t, s, sizeof(struct attr_flags));
t->aspath = aspath_create(s->aspath->data, s->aspath->hdr.len);
TAILQ_INIT(&t->others);
TAILQ_FOREACH(os, &s->others, attr_l)
attr_optadd(t, os->flags, os->type, os->data, os->len);
}
int
attr_write(void *p, u_int16_t p_len, u_int8_t flags, u_int8_t type,
void *data, u_int16_t data_len)
{
u_char *b = p;
u_int16_t tmp, tot_len = 2; /* attribute header (without len) */
if (data_len > 255) {
tot_len += 2 + data_len;
flags |= ATTR_EXTLEN;
} else
tot_len += 1 + data_len;
if (tot_len > p_len)
return (-1);
*b++ = flags;
*b++ = type;
if (data_len > 255) {
tmp = htons(data_len);
memcpy(b, &tmp, 2);
b += 2;
} else
*b++ = (u_char)(data_len & 0xff);
if (data_len != 0)
memcpy(b, data, data_len);
return (tot_len);
}
int
attr_optadd(struct attr_flags *attr, u_int8_t flags, u_int8_t type,
u_char *data, u_int16_t len)
{
struct attr *a, *p;
/* we need validate known optional attributes */
if (flags & ATTR_OPTIONAL && ! flags & ATTR_TRANSITIVE)
/*
* We already know that we're not intrested in this attribute.
* Currently only the MED is optional and non-transitive but
* MED is directly stored in struct attr_flags.
*/
return (0);
a = calloc(1, sizeof(struct attr));
if (a == NULL)
fatal("attr_optadd");
a->flags = flags;
a->type = type;
a->len = len;
if (len != 0) {
a->data = malloc(len);
if (a->data == NULL)
fatal("attr_optadd");
memcpy(a->data, data, len);
} else
a->data = NULL;
/* keep a sorted list */
TAILQ_FOREACH_REVERSE(p, &attr->others, attr_l, attr_list) {
if (type == p->type)
/* attribute only once allowed */
return (-1);
if (type > p->type) {
TAILQ_INSERT_AFTER(&attr->others, p, a, attr_l);
return (0);
}
}
TAILQ_INSERT_HEAD(&attr->others, a, attr_l);
return (0);
}
void
attr_optfree(struct attr_flags *attr)
{
struct attr *a;
while ((a = TAILQ_FIRST(&attr->others)) != NULL) {
TAILQ_REMOVE(&attr->others, a, attr_l);
free(a->data);
free(a);
}
}
/* aspath specific functions */
/* TODO
* aspath regexp search,
* aspath to string converter
*/
static u_int16_t aspath_extract(void *, int);
/*
* Extract the asnum out of the as segment at the specified position.
* Direct access is not possible because of non-aligned reads.
* ATTENTION: no bounds check are done.
*/
static u_int16_t
aspath_extract(void *seg, int pos)
{
u_char *ptr = seg;
u_int16_t as = 0;
ENSURE(0 <= pos && pos < 255);
ptr += 2 + 2 * pos;
as = *ptr++;
as <<= 8;
as |= *ptr;
return as;
}
int
aspath_verify(void *data, u_int16_t len)
{
u_int8_t *seg = data;
u_int16_t seg_size;
u_int8_t seg_len, seg_type;
if (len & 1)
/* odd lenght aspath are invalid */
return AS_ERR_BAD;
for (; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
if (seg_type != AS_SET && seg_type != AS_SEQUENCE) {
return AS_ERR_TYPE;
}
seg_size = 2 + 2 * seg_len;
if (seg_size > len)
return AS_ERR_LEN;
if (seg_size == 0)
/* empty aspath segment are not allowed */
return AS_ERR_BAD;
}
return 0; /* aspath is valid but probably not loop free */
}
struct aspath *
aspath_create(void *data, u_int16_t len)
{
struct aspath *aspath;
/* The aspath must already have been checked for correctness. */
aspath = malloc(ASPATH_HEADER_SIZE + len);
if (aspath == NULL)
fatal("aspath_create");
aspath->hdr.len = len;
memcpy(aspath->data, data, len);
aspath->hdr.as_cnt = aspath_count(aspath);
return aspath;
}
int
aspath_write(void *p, u_int16_t len, struct aspath *aspath, u_int16_t myAS,
int prepend)
{
u_char *b = p;
int tot_len, as_len, size, wpos = 0;
u_int16_t tmp;
u_int8_t type, attr_flag = ATTR_WELL_KNOWN;
if (prepend > 255)
/* lunatic prepends need to be blocked in the parser */
return (-1);
/* first calculate new size */
if (aspath->hdr.len > 0) {
ENSURE(aspath->hdr.len > 2);
type = aspath->data[0];
size = aspath->data[1];
} else {
/* empty as path */
type = AS_SET;
size = 0;
}
if (prepend == 0)
as_len = aspath->hdr.len;
else if (type == AS_SET || size + prepend > 255)
/* need to attach a new AS_SEQUENCE */
as_len = 2 + prepend * 2 + aspath->hdr.len;
else
as_len = prepend * 2 + aspath->hdr.len;
/* check buffer size */
tot_len = 2 + as_len;
if (as_len > 255) {
attr_flag |= ATTR_EXTLEN;
tot_len += 2;
} else
tot_len += 1;
if (tot_len > len)
return (-1);
/* header */
b[wpos++] = attr_flag;
b[wpos++] = ATTR_ASPATH;
if (as_len > 255) {
tmp = as_len;
tmp = htons(tmp);
memcpy(b, &tmp, 2);
wpos += 2;
} else
b[wpos++] = (u_char)(as_len & 0xff);
/* first prepends */
myAS = htons(myAS);
if (type == AS_SET) {
b[wpos++] = AS_SEQUENCE;
b[wpos++] = prepend;
for (; prepend > 0; prepend--) {
memcpy(b + wpos, &myAS, 2);
wpos += 2;
}
memcpy(b + wpos, aspath->data, aspath->hdr.len);
} else {
if (size + prepend > 255) {
b[wpos++] = AS_SEQUENCE;
b[wpos++] = size + prepend - 255;
for (; prepend + size > 255; prepend--) {
memcpy(b + wpos, &myAS, 2);
wpos += 2;
}
}
b[wpos++] = AS_SEQUENCE;
b[wpos++] = size + prepend;
for (; prepend > 0; prepend--) {
memcpy(b + wpos, &myAS, 2);
wpos += 2;
}
memcpy(b + wpos, aspath->data + 2, aspath->hdr.len - 2);
}
return (tot_len);
}
void
aspath_destroy(struct aspath *aspath)
{
/* only the aspath needs to be freed */
free(aspath);
}
u_char *
aspath_dump(struct aspath *aspath)
{
return aspath->data;
}
u_int16_t
aspath_length(struct aspath *aspath)
{
return aspath->hdr.len;
}
u_int16_t
aspath_count(struct aspath *aspath)
{
u_int8_t *seg;
u_int16_t cnt, len, seg_size;
u_int8_t seg_type, seg_len;
cnt = 0;
seg = aspath->data;
for (len = aspath->hdr.len; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
if (seg_type == AS_SET)
cnt += 1;
else
cnt += seg_len;
}
return cnt;
}
u_int16_t
aspath_neighbour(struct aspath *aspath)
{
/*
* Empty aspath is OK -- internal as route.
* But what is the neighbour? For now let's return 0 that
* should not break anything.
*/
if (aspath->hdr.len == 0)
return 0;
ENSURE(aspath->hdr.len > 2);
return aspath_extract(aspath->data, 0);
}
int
aspath_loopfree(struct aspath *aspath, u_int16_t myAS)
{
u_int8_t *seg;
u_int16_t len, seg_size;
u_int8_t i, seg_len, seg_type;
seg = aspath->data;
for (len = aspath->hdr.len; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
ENSURE(seg_size <= len);
for (i = 0; i < seg_len; i++) {
if (myAS == aspath_extract(seg, i))
return 0;
}
}
return 1;
}
#define AS_HASH_INITIAL 8271
u_int32_t
aspath_hash(struct aspath *aspath)
{
u_int8_t *seg;
u_int32_t hash;
u_int16_t len, seg_size;
u_int8_t i, seg_len, seg_type;
hash = AS_HASH_INITIAL;
seg = aspath->data;
for (len = aspath->hdr.len; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
ENSURE(seg_size <= len);
for (i = 0; i < seg_len; i++) {
hash += (hash << 5);
hash ^= aspath_extract(seg, i);
}
}
return hash;
}
int
aspath_compare(struct aspath *a1, struct aspath *a2)
{
int r;
if (a1->hdr.len > a2->hdr.len)
return (1);
if (a1->hdr.len < a2->hdr.len)
return (-1);
r = memcmp(a1->data, a2->data, a1->hdr.len);
if (r > 0)
return (1);
if (r < 0)
return (-1);
return 0;
}
int
aspath_snprint(char *buf, size_t size, void *data, u_int16_t len)
{
#define UPDATE() \
do { \
if (r == -1) \
return (1); \
total_size += r; \
if ((unsigned int)r < size) { \
size -= r; \
buf += r; \
} else { \
buf += size; \
size = 0; \
} \
} while (0)
u_int8_t *seg;
int r, total_size;
u_int16_t seg_size;
u_int8_t i, seg_type, seg_len;
total_size = 0;
seg = data;
for (; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
if (seg_type == AS_SET) {
r = snprintf(buf, size, "{ ");
UPDATE();
} else if (total_size != 0) {
r = snprintf(buf, size, " ");
UPDATE();
}
ENSURE(seg_size <= len);
for (i = 0; i < seg_len; i++) {
r = snprintf(buf, size, "%hu", aspath_extract(seg, i));
UPDATE();
if (i + 1 < seg_len) {
r = snprintf(buf, size, " ");
UPDATE();
}
}
if (seg_type == AS_SET) {
r = snprintf(buf, size, " }");
UPDATE();
}
}
return total_size;
#undef UPDATE
}
int
aspath_asprint(char **ret, void *data, u_int16_t len)
{
size_t slen, plen;
slen = aspath_strlen(data, len) + 1;
*ret = malloc(slen);
if (*ret == NULL)
return (-1);
plen = aspath_snprint(*ret, slen, data, len);
ENSURE(plen < slen);
return (plen);
}
size_t
aspath_strlen(void *data, u_int16_t len)
{
u_int8_t *seg;
int total_size;
u_int16_t as, seg_size;
u_int8_t i, seg_type, seg_len;
total_size = 0;
seg = data;
for (; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
if (seg_type == AS_SET)
total_size += 2;
else if (total_size != 0)
total_size += 1;
ENSURE(seg_size <= len);
for (i = 0; i < seg_len; i++) {
as = aspath_extract(seg, i);
if (as >= 10000)
total_size += 5;
else if (as >= 1000)
total_size += 4;
else if (as >= 100)
total_size += 3;
else if (as >= 10)
total_size += 2;
else
total_size += 1;
if (i + 1 < seg_len)
total_size += 1;
}
if (seg_type == AS_SET)
total_size += 2;
}
return total_size;
}
/* we need to be able to search more than one as */
int
aspath_match(struct aspath *a, enum as_spec type, u_int16_t as)
{
u_int8_t *seg;
int final;
u_int16_t len, seg_size;
u_int8_t i, seg_type, seg_len;
final = 0;
seg = a->data;
for (len = a->hdr.len; len > 0; len -= seg_size, seg += seg_size) {
seg_type = seg[0];
seg_len = seg[1];
ENSURE(seg_type == AS_SET || seg_type == AS_SEQUENCE);
seg_size = 2 + 2 * seg_len;
final = (len == seg_size);
if (type == AS_SOURCE && !final)
/* not yet in the final segment */
continue;
ENSURE(seg_size <= len);
for (i = 0; i < seg_len; i++)
if (as == aspath_extract(seg, i)) {
if (final && i + 1 >= seg_len)
/* the final (rightmost) as */
if (type == AS_TRANSIT)
return (0);
else
return (1);
else if (type != AS_SOURCE)
return (1);
}
}
return (0);
}
|