Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / attrs.c @ 7e95c05d

History | View | Annotate | Download (38.6 KB)

1
/*
2
 *        BIRD -- BGP Attributes
3
 *
4
 *        (c) 2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#undef LOCAL_DEBUG
10

    
11
#include <stdlib.h>
12

    
13
#include "nest/bird.h"
14
#include "nest/iface.h"
15
#include "nest/protocol.h"
16
#include "nest/route.h"
17
#include "nest/attrs.h"
18
#include "conf/conf.h"
19
#include "lib/resource.h"
20
#include "lib/string.h"
21
#include "lib/unaligned.h"
22

    
23
#include "bgp.h"
24

    
25
static byte bgp_mandatory_attrs[] = { BA_ORIGIN, BA_AS_PATH
26
#ifndef IPV6
27
,BA_NEXT_HOP
28
#endif
29
};
30

    
31
struct attr_desc {
32
  char *name;
33
  int expected_length;
34
  int expected_flags;
35
  int type;
36
  int allow_in_ebgp;
37
  int (*validate)(struct bgp_proto *p, byte *attr, int len);
38
  void (*format)(eattr *ea, byte *buf, int buflen);
39
};
40

    
41
static int
42
bgp_check_origin(struct bgp_proto *p UNUSED, byte *a, int len UNUSED)
43
{
44
  if (*a > 2)
45
    return 6;
46
  return 0;
47
}
48

    
49
static void
50
bgp_format_origin(eattr *a, byte *buf, int buflen UNUSED)
51
{
52
  static char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" };
53

    
54
  bsprintf(buf, bgp_origin_names[a->u.data]);
55
}
56

    
57
static int
58
path_segment_contains(byte *p, int bs, u32 asn)
59
{
60
  int i;
61
  int len = p[1];
62
  p += 2;
63

    
64
  for(i=0; i<len; i++)
65
    {
66
      u32 asn2 = (bs == 4) ? get_u32(p) : get_u16(p);
67
      if (asn2 == asn)
68
        return 1;
69
      p += bs;
70
    }
71

    
72
  return 0;
73
}
74

    
75
/* Validates path attribute, removes AS_CONFED_* segments, and also returns path length */
76
static int
77
validate_path(struct bgp_proto *p, int as_path, int bs, byte *idata, unsigned int *ilength)
78
{
79
  int res = 0;
80
  u8 *a, *dst;
81
  int len, plen, copy;
82

    
83
  dst = a = idata;
84
  len = *ilength;
85

    
86
  while (len)
87
    {
88
      if (len < 2)
89
        return -1;
90

    
91
      plen = 2 + bs * a[1];
92
      if (len < plen)
93
        return -1;
94

    
95
      switch (a[0])
96
        {
97
        case AS_PATH_SET:
98
          copy = 1;
99
          res++;
100
          break;
101

    
102
        case AS_PATH_SEQUENCE:
103
          copy = 1;
104
          res += a[1];
105
          break;
106

    
107
        case AS_PATH_CONFED_SEQUENCE:
108
        case AS_PATH_CONFED_SET:
109
          if (as_path && path_segment_contains(a, bs, p->remote_as))
110
            {
111
              log(L_WARN "%s: AS_CONFED_* segment with peer ASN found, misconfigured confederation?", p->p.name);
112
              return -1;
113
            }
114

    
115
          log(L_WARN "%s: %s_PATH attribute contains AS_CONFED_* segment, skipping segment",
116
              p->p.name, as_path ? "AS" : "AS4");
117
          copy = 0;
118
          break;
119

    
120
        default:
121
          return -1;
122
        }
123

    
124
      if (copy)
125
        {
126
          if (dst != a)
127
            memmove(dst, a, plen);
128
          dst += plen;
129
        }
130

    
131
      len -= plen;
132
      a += plen;
133
    }
134

    
135
  *ilength = dst - idata;
136
  return res;
137
}
138

    
139
static inline int
140
validate_as_path(struct bgp_proto *p, byte *a, int *len)
141
{
142
  return validate_path(p, 1, p->as4_session ? 4 : 2, a, len);
143
}
144

    
145
static inline int
146
validate_as4_path(struct bgp_proto *p, struct adata *path)
147
{
148
  return validate_path(p, 0, 4, path->data, &path->length);
149
}
150

    
151
static int
152
bgp_check_next_hop(struct bgp_proto *p UNUSED, byte *a, int len)
153
{
154
#ifdef IPV6
155
  return -1;
156
#else
157
  ip_addr addr;
158

    
159
  memcpy(&addr, a, len);
160
  ipa_ntoh(addr);
161
  if (ipa_classify(addr) & IADDR_HOST)
162
    return 0;
163
  else
164
    return 8;
165
#endif
166
}
167

    
168
static void
169
bgp_format_next_hop(eattr *a, byte *buf, int buflen UNUSED)
170
{
171
  ip_addr *ipp = (ip_addr *) a->u.ptr->data;
172
#ifdef IPV6
173
  /* in IPv6, we might have two addresses in NEXT HOP */
174
  if ((a->u.ptr->length == NEXT_HOP_LENGTH) && ipa_nonzero(ipp[1]))
175
    {
176
      bsprintf(buf, "%I %I", ipp[0], ipp[1]);
177
      return;
178
    }
179
#endif
180

    
181
  bsprintf(buf, "%I", ipp[0]);
182
}
183

    
184
static int
185
bgp_check_aggregator(struct bgp_proto *p, byte *a UNUSED, int len)
186
{
187
  int exp_len = p->as4_session ? 8 : 6;
188
  
189
  return (len == exp_len) ? 0 : 5;
190
}
191

    
192
static void
193
bgp_format_aggregator(eattr *a, byte *buf, int buflen UNUSED)
194
{
195
  struct adata *ad =  a->u.ptr;
196
  byte *data = ad->data;
197
  u32 as;
198

    
199
  as = get_u32(data);
200
  data += 4;
201

    
202
  bsprintf(buf, "%d.%d.%d.%d AS%d", data[0], data[1], data[2], data[3], as);
203
}
204

    
205
static int
206
bgp_check_cluster_list(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
207
{
208
  return ((len % 4) == 0) ? 0 : 5;
209
}
210

    
211
static void
212
bgp_format_cluster_list(eattr *a, byte *buf, int buflen UNUSED)
213
{
214
  int_set_format(a->u.ptr, 0, buf, buflen);
215
}
216

    
217
static int
218
bgp_check_reach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
219
{
220
#ifdef IPV6
221
  p->mp_reach_start = a;
222
  p->mp_reach_len = len;
223
#endif
224
  return -1;
225
}
226

    
227
static int
228
bgp_check_unreach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
229
{
230
#ifdef IPV6
231
  p->mp_unreach_start = a;
232
  p->mp_unreach_len = len;
233
#endif
234
  return -1;
235
}
236

    
237
static struct attr_desc bgp_attr_table[] = {
238
  { NULL, -1, 0, 0, 0,                                                                /* Undefined */
239
    NULL, NULL },
240
  { "origin", 1, BAF_TRANSITIVE, EAF_TYPE_INT, 1,                                /* BA_ORIGIN */
241
    bgp_check_origin, bgp_format_origin },
242
  { "as_path", -1, BAF_TRANSITIVE, EAF_TYPE_AS_PATH, 1,                                /* BA_AS_PATH */
243
    NULL, NULL }, /* is checked by validate_as_path() as a special case */
244
  { "next_hop", 4, BAF_TRANSITIVE, EAF_TYPE_IP_ADDRESS, 1,                        /* BA_NEXT_HOP */
245
    bgp_check_next_hop, bgp_format_next_hop },
246
  { "med", 4, BAF_OPTIONAL, EAF_TYPE_INT, 1,                                        /* BA_MULTI_EXIT_DISC */
247
    NULL, NULL },
248
  { "local_pref", 4, BAF_TRANSITIVE, EAF_TYPE_INT, 0,                                /* BA_LOCAL_PREF */
249
    NULL, NULL },
250
  { "atomic_aggr", 0, BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,                        /* BA_ATOMIC_AGGR */
251
    NULL, NULL },
252
  { "aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,        /* BA_AGGREGATOR */
253
    bgp_check_aggregator, bgp_format_aggregator },
254
  { "community", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_INT_SET, 1,        /* BA_COMMUNITY */
255
    NULL, NULL },
256
  { "originator_id", 4, BAF_OPTIONAL, EAF_TYPE_ROUTER_ID, 0,                        /* BA_ORIGINATOR_ID */
257
    NULL, NULL },
258
  { "cluster_list", -1, BAF_OPTIONAL, EAF_TYPE_INT_SET, 0,                        /* BA_CLUSTER_LIST */
259
    bgp_check_cluster_list, bgp_format_cluster_list }, 
260
  { .name = NULL },                                                                /* BA_DPA */
261
  { .name = NULL },                                                                        /* BA_ADVERTISER */
262
  { .name = NULL },                                                                        /* BA_RCID_PATH */
263
  { "mp_reach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1,                        /* BA_MP_REACH_NLRI */
264
    bgp_check_reach_nlri, NULL },
265
  { "mp_unreach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1,                        /* BA_MP_UNREACH_NLRI */
266
    bgp_check_unreach_nlri, NULL },
267
  {  .name = NULL },                                                                        /* BA_EXTENDED_COMM */
268
  { "as4_path", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,                /* BA_AS4_PATH */
269
    NULL, NULL },
270
  { "as4_aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,        /* BA_AS4_PATH */
271
    NULL, NULL }
272
};
273

    
274
/* BA_AS4_PATH is type EAF_TYPE_OPAQUE and not type EAF_TYPE_AS_PATH.
275
 * It does not matter as this attribute does not appear on routes in the routing table.
276
 */
277

    
278
#define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
279

    
280
static inline struct adata *
281
bgp_alloc_adata(struct linpool *pool, unsigned len)
282
{
283
  struct adata *ad = lp_alloc(pool, sizeof(struct adata) + len);
284
  ad->length = len;
285
  return ad;
286
}
287

    
288
static void
289
bgp_set_attr(eattr *e, unsigned attr, uintptr_t val)
290
{
291
  ASSERT(ATTR_KNOWN(attr));
292
  e->id = EA_CODE(EAP_BGP, attr);
293
  e->type = bgp_attr_table[attr].type;
294
  e->flags = bgp_attr_table[attr].expected_flags;
295
  if (e->type & EAF_EMBEDDED)
296
    e->u.data = val;
297
  else
298
    e->u.ptr = (struct adata *) val;
299
}
300

    
301
static byte *
302
bgp_set_attr_wa(eattr *e, struct linpool *pool, unsigned attr, unsigned len)
303
{
304
  struct adata *ad = bgp_alloc_adata(pool, len);
305
  bgp_set_attr(e, attr, (uintptr_t) ad);
306
  return ad->data;
307
}
308

    
309
void
310
bgp_attach_attr(ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val)
311
{
312
  ea_list *a = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
313
  a->next = *to;
314
  *to = a;
315
  a->flags = EALF_SORTED;
316
  a->count = 1;
317
  bgp_set_attr(a->attrs, attr, val);
318
}
319

    
320
byte *
321
bgp_attach_attr_wa(ea_list **to, struct linpool *pool, unsigned attr, unsigned len)
322
{
323
  struct adata *ad = bgp_alloc_adata(pool, len);
324
  bgp_attach_attr(to, pool, attr, (uintptr_t) ad);
325
  return ad->data;
326
}
327

    
328
static int
329
bgp_encode_attr_hdr(byte *dst, unsigned int flags, unsigned code, int len)
330
{
331
  int wlen;
332

    
333
  DBG("\tAttribute %02x (%d bytes, flags %02x)\n", code, len, flags);
334

    
335
  if (len < 256)
336
    {
337
      *dst++ = flags;
338
      *dst++ = code;
339
      *dst++ = len;
340
      wlen = 3;
341
    }
342
  else
343
    {
344
      *dst++ = flags | BAF_EXT_LEN;
345
      *dst++ = code;
346
      put_u16(dst, len);
347
      wlen = 4;
348
    }
349

    
350
  return wlen;
351
}
352

    
353
static void
354
aggregator_convert_to_old(struct adata *aggr, byte *dst, int *new_used)
355
{
356
  byte *src = aggr->data;
357
  *new_used = 0;
358

    
359
  u32 as = get_u32(src);
360
  if (as > 0xFFFF) 
361
    {
362
      as = AS_TRANS;
363
      *new_used = 1;
364
    }
365
  put_u16(dst, as);
366

    
367
  /* Copy IPv4 address */
368
  memcpy(dst + 2, src + 4, 4);
369
}
370

    
371
static void
372
aggregator_convert_to_new(struct adata *aggr, byte *dst)
373
{
374
  byte *src = aggr->data;
375

    
376
  u32 as   = get_u16(src);
377
  put_u32(dst, as);
378

    
379
  /* Copy IPv4 address */
380
  memcpy(dst + 4, src + 2, 4);
381
}
382

    
383
static int
384
bgp_get_attr_len(eattr *a)
385
{
386
  int len;
387
  if (ATTR_KNOWN(EA_ID(a->id)))
388
    {
389
      int code = EA_ID(a->id);
390
      struct attr_desc *desc = &bgp_attr_table[code];
391
      len = desc->expected_length;
392
      if (len < 0)
393
        {
394
          ASSERT(!(a->type & EAF_EMBEDDED));
395
          len = a->u.ptr->length;
396
        }
397
    }
398
  else
399
    {
400
      ASSERT((a->type & EAF_TYPE_MASK) == EAF_TYPE_OPAQUE);
401
      len = a->u.ptr->length;
402
    }
403
  
404
  return len;
405
}
406

    
407
#define ADVANCE(w, r, l) do { r -= l; w += l; } while (0)
408

    
409
/**
410
 * bgp_encode_attrs - encode BGP attributes
411
 * @p: BGP instance
412
 * @w: buffer
413
 * @attrs: a list of extended attributes
414
 * @remains: remaining space in the buffer
415
 *
416
 * The bgp_encode_attrs() function takes a list of extended attributes
417
 * and converts it to its BGP representation (a part of an Update message).
418
 *
419
 * Result: Length of the attribute block generated or -1 if not enough space.
420
 */
421
unsigned int
422
bgp_encode_attrs(struct bgp_proto *p, byte *w, ea_list *attrs, int remains)
423
{
424
  unsigned int i, code, flags;
425
  byte *start = w;
426
  int len, rv;
427

    
428
  for(i=0; i<attrs->count; i++)
429
    {
430
      eattr *a = &attrs->attrs[i];
431
      ASSERT(EA_PROTO(a->id) == EAP_BGP);
432
      code = EA_ID(a->id);
433
#ifdef IPV6
434
      /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
435
      if (code == BA_NEXT_HOP)
436
        continue;
437
#endif
438

    
439
      /* When AS4-aware BGP speaker is talking to non-AS4-aware BGP speaker,
440
       * we have to convert our 4B AS_PATH to 2B AS_PATH and send our AS_PATH 
441
       * as optional AS4_PATH attribute.
442
       */
443
      if ((code == BA_AS_PATH) && (! p->as4_session))
444
        {
445
          len = a->u.ptr->length;
446

    
447
          if (remains < (len + 4))
448
            goto err_no_buffer;
449

    
450
          /* Using temporary buffer because don't know a length of created attr
451
           * and therefore a length of a header. Perhaps i should better always
452
           * use BAF_EXT_LEN. */
453
          
454
          byte buf[len];
455
          int new_used;
456
          int nl = as_path_convert_to_old(a->u.ptr, buf, &new_used);
457

    
458
          DBG("BGP: Encoding old AS_PATH\n");
459
          rv = bgp_encode_attr_hdr(w, BAF_TRANSITIVE, BA_AS_PATH, nl);
460
          ADVANCE(w, remains, rv);
461
          memcpy(w, buf, nl);
462
          ADVANCE(w, remains, nl);
463

    
464
          if (! new_used)
465
            continue;
466

    
467
          if (remains < (len + 4))
468
            goto err_no_buffer;
469

    
470
          /* We should discard AS_CONFED_SEQUENCE or AS_CONFED_SET path segments 
471
           * here but we don't support confederations and such paths we already
472
           * discarded in bgp_check_as_path().
473
           */
474

    
475
          DBG("BGP: Encoding AS4_PATH\n");
476
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_PATH, len);
477
          ADVANCE(w, remains, rv);
478
          memcpy(w, a->u.ptr->data, len);
479
          ADVANCE(w, remains, len);
480

    
481
          continue;
482
        }
483

    
484
      /* The same issue with AGGREGATOR attribute */
485
      if ((code == BA_AGGREGATOR) && (! p->as4_session))
486
        {
487
          int new_used;
488

    
489
          len = 6;
490
          if (remains < (len + 3))
491
            goto err_no_buffer;
492

    
493
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AGGREGATOR, len);
494
          ADVANCE(w, remains, rv);
495
          aggregator_convert_to_old(a->u.ptr, w, &new_used);
496
          ADVANCE(w, remains, len);
497

    
498
          if (! new_used)
499
            continue;
500

    
501
          len = 8;
502
          if (remains < (len + 3))
503
            goto err_no_buffer;
504

    
505
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_AGGREGATOR, len);
506
          ADVANCE(w, remains, rv);
507
          memcpy(w, a->u.ptr->data, len);
508
          ADVANCE(w, remains, len);
509

    
510
          continue;
511
        }
512

    
513
      /* Standard path continues here ... */
514

    
515
      flags = a->flags & (BAF_OPTIONAL | BAF_TRANSITIVE | BAF_PARTIAL);
516
      len = bgp_get_attr_len(a);
517

    
518
      /* Skip empty int sets */ 
519
      if (((a->type & EAF_TYPE_MASK) == EAF_TYPE_INT_SET) && (len == 0))
520
        continue; 
521

    
522
      if (remains < len + 4)
523
        goto err_no_buffer;
524

    
525
      rv = bgp_encode_attr_hdr(w, flags, code, len);
526
      ADVANCE(w, remains, rv);
527

    
528
      switch (a->type & EAF_TYPE_MASK)
529
        {
530
        case EAF_TYPE_INT:
531
        case EAF_TYPE_ROUTER_ID:
532
          if (len == 4)
533
            put_u32(w, a->u.data);
534
          else
535
            *w = a->u.data;
536
          break;
537
        case EAF_TYPE_IP_ADDRESS:
538
          {
539
            ip_addr ip = *(ip_addr *)a->u.ptr->data;
540
            ipa_hton(ip);
541
            memcpy(w, &ip, len);
542
            break;
543
          }
544
        case EAF_TYPE_INT_SET:
545
          {
546
            u32 *z = (u32 *)a->u.ptr->data;
547
            int i;
548
            for(i=0; i<len; i+=4)
549
              put_u32(w+i, *z++);
550
            break;
551
          }
552
        case EAF_TYPE_OPAQUE:
553
        case EAF_TYPE_AS_PATH:
554
          memcpy(w, a->u.ptr->data, len);
555
          break;
556
        default:
557
          bug("bgp_encode_attrs: unknown attribute type %02x", a->type);
558
        }
559
      ADVANCE(w, remains, len);
560
    }
561
  return w - start;
562

    
563
 err_no_buffer:
564
  return -1;
565
}
566

    
567
static void
568
bgp_init_prefix(struct fib_node *N)
569
{
570
  struct bgp_prefix *p = (struct bgp_prefix *) N;
571
  p->bucket_node.next = NULL;
572
}
573

    
574
static int
575
bgp_compare_u32(const u32 *x, const u32 *y)
576
{
577
  return (*x < *y) ? -1 : (*x > *y) ? 1 : 0;
578
}
579

    
580
static void
581
bgp_normalize_set(u32 *dest, u32 *src, unsigned cnt)
582
{
583
  memcpy(dest, src, sizeof(u32) * cnt);
584
  qsort(dest, cnt, sizeof(u32), (int(*)(const void *, const void *)) bgp_compare_u32);
585
}
586

    
587
static void
588
bgp_rehash_buckets(struct bgp_proto *p)
589
{
590
  struct bgp_bucket **old = p->bucket_hash;
591
  struct bgp_bucket **new;
592
  unsigned oldn = p->hash_size;
593
  unsigned i, e, mask;
594
  struct bgp_bucket *b;
595

    
596
  p->hash_size = p->hash_limit;
597
  DBG("BGP: Rehashing bucket table from %d to %d\n", oldn, p->hash_size);
598
  p->hash_limit *= 4;
599
  if (p->hash_limit >= 65536)
600
    p->hash_limit = ~0;
601
  new = p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
602
  mask = p->hash_size - 1;
603
  for (i=0; i<oldn; i++)
604
    while (b = old[i])
605
      {
606
        old[i] = b->hash_next;
607
        e = b->hash & mask;
608
        b->hash_next = new[e];
609
        if (b->hash_next)
610
          b->hash_next->hash_prev = b;
611
        b->hash_prev = NULL;
612
        new[e] = b;
613
      }
614
  mb_free(old);
615
}
616

    
617
static struct bgp_bucket *
618
bgp_new_bucket(struct bgp_proto *p, ea_list *new, unsigned hash)
619
{
620
  struct bgp_bucket *b;
621
  unsigned ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
622
  unsigned ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
623
  unsigned size = sizeof(struct bgp_bucket) + ea_size;
624
  unsigned i;
625
  byte *dest;
626
  unsigned index = hash & (p->hash_size - 1);
627

    
628
  /* Gather total size of non-inline attributes */
629
  for (i=0; i<new->count; i++)
630
    {
631
      eattr *a = &new->attrs[i];
632
      if (!(a->type & EAF_EMBEDDED))
633
        size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
634
    }
635

    
636
  /* Create the bucket and hash it */
637
  b = mb_alloc(p->p.pool, size);
638
  b->hash_next = p->bucket_hash[index];
639
  if (b->hash_next)
640
    b->hash_next->hash_prev = b;
641
  p->bucket_hash[index] = b;
642
  b->hash_prev = NULL;
643
  b->hash = hash;
644
  add_tail(&p->bucket_queue, &b->send_node);
645
  init_list(&b->prefixes);
646
  memcpy(b->eattrs, new, ea_size);
647
  dest = ((byte *)b->eattrs) + ea_size_aligned;
648

    
649
  /* Copy values of non-inline attributes */
650
  for (i=0; i<new->count; i++)
651
    {
652
      eattr *a = &b->eattrs->attrs[i];
653
      if (!(a->type & EAF_EMBEDDED))
654
        {
655
          struct adata *oa = a->u.ptr;
656
          struct adata *na = (struct adata *) dest;
657
          memcpy(na, oa, sizeof(struct adata) + oa->length);
658
          a->u.ptr = na;
659
          dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
660
        }
661
    }
662

    
663
  /* If needed, rehash */
664
  p->hash_count++;
665
  if (p->hash_count > p->hash_limit)
666
    bgp_rehash_buckets(p);
667

    
668
  return b;
669
}
670

    
671
static struct bgp_bucket *
672
bgp_get_bucket(struct bgp_proto *p, net *n, ea_list *attrs, int originate)
673
{
674
  ea_list *new;
675
  unsigned i, cnt, hash, code;
676
  eattr *a, *d;
677
  u32 seen = 0;
678
  struct bgp_bucket *b;
679

    
680
  /* Merge the attribute list */
681
  new = alloca(ea_scan(attrs));
682
  ea_merge(attrs, new);
683
  ea_sort(new);
684

    
685
  /* Normalize attributes */
686
  d = new->attrs;
687
  cnt = new->count;
688
  new->count = 0;
689
  for(i=0; i<cnt; i++)
690
    {
691
      a = &new->attrs[i];
692
#ifdef LOCAL_DEBUG
693
      {
694
        byte buf[EA_FORMAT_BUF_SIZE];
695
        ea_format(a, buf);
696
        DBG("\t%s\n", buf);
697
      }
698
#endif
699
      if (EA_PROTO(a->id) != EAP_BGP)
700
        continue;
701
      code = EA_ID(a->id);
702
      if (ATTR_KNOWN(code))
703
        {
704
          if (!bgp_attr_table[code].allow_in_ebgp && !p->is_internal)
705
            continue;
706
          /* The flags might have been zero if the attr was added by filters */
707
          a->flags = (a->flags & BAF_PARTIAL) | bgp_attr_table[code].expected_flags;
708
          if (code < 32)
709
            seen |= 1 << code;
710
        }
711
      else
712
        {
713
          /* Don't re-export unknown non-transitive attributes */
714
          if (!(a->flags & BAF_TRANSITIVE))
715
            continue;
716
        }
717
      *d = *a;
718
      if ((d->type & EAF_ORIGINATED) && !originate && (d->flags & BAF_TRANSITIVE) && (d->flags & BAF_OPTIONAL))
719
        d->flags |= BAF_PARTIAL;
720
      switch (d->type & EAF_TYPE_MASK)
721
        {
722
        case EAF_TYPE_INT_SET:
723
          {
724
            struct adata *z = alloca(sizeof(struct adata) + d->u.ptr->length);
725
            z->length = d->u.ptr->length;
726
            bgp_normalize_set((u32 *) z->data, (u32 *) d->u.ptr->data, z->length / 4);
727
            d->u.ptr = z;
728
            break;
729
          }
730
        default: ;
731
        }
732
      d++;
733
      new->count++;
734
    }
735

    
736
  /* Hash */
737
  hash = ea_hash(new);
738
  for(b=p->bucket_hash[hash & (p->hash_size - 1)]; b; b=b->hash_next)
739
    if (b->hash == hash && ea_same(b->eattrs, new))
740
      {
741
        DBG("Found bucket.\n");
742
        return b;
743
      }
744

    
745
  /* Ensure that there are all mandatory attributes */
746
  for(i=0; i<ARRAY_SIZE(bgp_mandatory_attrs); i++)
747
    if (!(seen & (1 << bgp_mandatory_attrs[i])))
748
      {
749
        log(L_ERR "%s: Mandatory attribute %s missing in route %I/%d", p->p.name, bgp_attr_table[bgp_mandatory_attrs[i]].name, n->n.prefix, n->n.pxlen);
750
        return NULL;
751
      }
752

    
753
  /* Check if next hop is valid */
754
  a = ea_find(new, EA_CODE(EAP_BGP, BA_NEXT_HOP));
755
  if (!a || ipa_equal(p->cf->remote_ip, *(ip_addr *)a->u.ptr->data))
756
    {
757
      log(L_ERR "%s: Invalid NEXT_HOP attribute in route %I/%d", p->p.name, n->n.prefix, n->n.pxlen);
758
      return NULL;
759
    }
760

    
761
  /* Create new bucket */
762
  DBG("Creating bucket.\n");
763
  return bgp_new_bucket(p, new, hash);
764
}
765

    
766
void
767
bgp_free_bucket(struct bgp_proto *p, struct bgp_bucket *buck)
768
{
769
  if (buck->hash_next)
770
    buck->hash_next->hash_prev = buck->hash_prev;
771
  if (buck->hash_prev)
772
    buck->hash_prev->hash_next = buck->hash_next;
773
  else
774
    p->bucket_hash[buck->hash & (p->hash_size-1)] = buck->hash_next;
775
  mb_free(buck);
776
}
777

    
778
void
779
bgp_rt_notify(struct proto *P, rtable *tbl UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs)
780
{
781
  struct bgp_proto *p = (struct bgp_proto *) P;
782
  struct bgp_bucket *buck;
783
  struct bgp_prefix *px;
784

    
785
  DBG("BGP: Got route %I/%d %s\n", n->n.prefix, n->n.pxlen, new ? "up" : "down");
786

    
787
  if (new)
788
    {
789
      buck = bgp_get_bucket(p, n, attrs, new->attrs->source != RTS_BGP);
790
      if (!buck)                        /* Inconsistent attribute list */
791
        return;
792
    }
793
  else
794
    {
795
      if (!(buck = p->withdraw_bucket))
796
        {
797
          buck = p->withdraw_bucket = mb_alloc(P->pool, sizeof(struct bgp_bucket));
798
          init_list(&buck->prefixes);
799
        }
800
    }
801
  px = fib_get(&p->prefix_fib, &n->n.prefix, n->n.pxlen);
802
  if (px->bucket_node.next)
803
    {
804
      DBG("\tRemoving old entry.\n");
805
      rem_node(&px->bucket_node);
806
    }
807
  add_tail(&buck->prefixes, &px->bucket_node);
808
  bgp_schedule_packet(p->conn, PKT_UPDATE);
809
}
810

    
811
static int
812
bgp_create_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool)
813
{
814
  ea_list *ea = lp_alloc(pool, sizeof(ea_list) + 4*sizeof(eattr));
815
  rta *rta = e->attrs;
816
  byte *z;
817

    
818
  ea->next = *attrs;
819
  *attrs = ea;
820
  ea->flags = EALF_SORTED;
821
  ea->count = 4;
822

    
823
  bgp_set_attr(ea->attrs, BA_ORIGIN,
824
       ((rta->source == RTS_OSPF_EXT1) || (rta->source == RTS_OSPF_EXT2)) ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
825

    
826
  if (p->is_internal)
827
    bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 0);
828
  else
829
    {
830
      z = bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 6);
831
      z[0] = AS_PATH_SEQUENCE;
832
      z[1] = 1;                                /* 1 AS */
833
      put_u32(z+2, p->local_as);
834
    }
835

    
836
  /* iBGP -> use gw, eBGP multi-hop -> use source_addr,
837
     eBGP single-hop -> use gw if on the same iface */
838
  z = bgp_set_attr_wa(ea->attrs+2, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
839
  if (p->cf->next_hop_self ||
840
      rta->dest != RTD_ROUTER ||
841
      ipa_equal(rta->gw, IPA_NONE) ||
842
      ipa_has_link_scope(rta->gw) ||
843
      (!p->is_internal && (!p->neigh || (rta->iface != p->neigh->iface))))
844
    set_next_hop(z, p->source_addr);
845
  else
846
    set_next_hop(z, rta->gw);
847

    
848
  bgp_set_attr(ea->attrs+3, BA_LOCAL_PREF, p->cf->default_local_pref);
849

    
850
  return 0;                                /* Leave decision to the filters */
851
}
852

    
853

    
854
static inline int
855
bgp_as_path_loopy(struct bgp_proto *p, rta *a)
856
{
857
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
858
  return (e && as_path_is_member(e->u.ptr, p->local_as));
859
}
860

    
861
static inline int
862
bgp_originator_id_loopy(struct bgp_proto *p, rta *a)
863
{
864
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
865
  return (e && (e->u.data == p->local_id));
866
}
867

    
868
static inline int
869
bgp_cluster_list_loopy(struct bgp_proto *p, rta *a)
870
{
871
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
872
  return (e && p->rr_client && int_set_contains(e->u.ptr, p->rr_cluster_id));
873
}
874

    
875

    
876
static inline void
877
bgp_path_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 as)
878
{
879
  eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
880
  bgp_attach_attr(attrs, pool, BA_AS_PATH, (uintptr_t) as_path_prepend(pool, a->u.ptr, as));
881
}
882

    
883
static inline void
884
bgp_cluster_list_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 cid)
885
{
886
  eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
887
  bgp_attach_attr(attrs, pool, BA_CLUSTER_LIST, (uintptr_t) int_set_add(pool, a ? a->u.ptr : NULL, cid));
888
}
889

    
890
static int
891
bgp_update_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool, int rr)
892
{
893
  eattr *a;
894

    
895
  if (!p->is_internal && !p->rs_client)
896
    {
897
      bgp_path_prepend(e, attrs, pool, p->local_as);
898

    
899
      /* The MULTI_EXIT_DISC attribute received from a neighboring AS MUST NOT be
900
       * propagated to other neighboring ASes.
901
       * Perhaps it would be better to undefine it.
902
       */
903
      a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
904
      if (a)
905
        bgp_attach_attr(attrs, pool, BA_MULTI_EXIT_DISC, 0);
906
    }
907

    
908
  /* iBGP -> keep next_hop, eBGP multi-hop -> use source_addr,
909
     eBGP single-hop -> keep next_hop if on the same iface */
910
  a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_NEXT_HOP));
911
  if (a && !p->cf->next_hop_self && 
912
      (p->is_internal || (p->neigh && (e->attrs->iface == p->neigh->iface))))
913
    {
914
      /* Leave the original next hop attribute, will check later where does it point */
915
    }
916
  else
917
    {
918
      /* Need to create new one */
919
      byte *b = bgp_attach_attr_wa(attrs, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
920
      set_next_hop(b, p->source_addr);
921
    }
922

    
923
  if (rr)
924
    {
925
      /* Handling route reflection, RFC 4456 */
926
      struct bgp_proto *src = (struct bgp_proto *) e->attrs->proto;
927

    
928
      a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
929
      if (!a)
930
        bgp_attach_attr(attrs, pool, BA_ORIGINATOR_ID, src->remote_id);
931

    
932
      /* We attach proper cluster ID according to whether the route is entering or leaving the cluster */
933
      bgp_cluster_list_prepend(e, attrs, pool, src->rr_client ? src->rr_cluster_id : p->rr_cluster_id);
934

    
935
      /* Two RR clients with different cluster ID, hmmm */
936
      if (src->rr_client && p->rr_client && (src->rr_cluster_id != p->rr_cluster_id))
937
        bgp_cluster_list_prepend(e, attrs, pool, p->rr_cluster_id);
938
    }
939

    
940
  return 0;                                /* Leave decision to the filters */
941
}
942

    
943
static int
944
bgp_community_filter(struct bgp_proto *p, rte *e)
945
{
946
  eattr *a;
947
  struct adata *d;
948

    
949
  /* Check if we aren't forbidden to export the route by communities */
950
  a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_COMMUNITY));
951
  if (a)
952
    {
953
      d = a->u.ptr;
954
      if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
955
        {
956
          DBG("\tNO_ADVERTISE\n");
957
          return 1;
958
        }
959
      if (!p->is_internal &&
960
          (int_set_contains(d, BGP_COMM_NO_EXPORT) ||
961
           int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED)))
962
        {
963
          DBG("\tNO_EXPORT\n");
964
          return 1;
965
        }
966
    }
967

    
968
  return 0;
969
}
970

    
971
int
972
bgp_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
973
{
974
  rte *e = *new;
975
  struct bgp_proto *p = (struct bgp_proto *) P;
976
  struct bgp_proto *new_bgp = (e->attrs->proto->proto == &proto_bgp) ? (struct bgp_proto *) e->attrs->proto : NULL;
977

    
978
  if (p == new_bgp)                        /* Poison reverse updates */
979
    return -1;
980
  if (new_bgp)
981
    {
982
      /* We should check here for cluster list loop, because the receiving BGP instance
983
         might have different cluster ID  */
984
      if (bgp_cluster_list_loopy(p, e->attrs))
985
        return -1;
986

    
987
      if (p->cf->interpret_communities && bgp_community_filter(p, e))
988
        return -1;
989

    
990
      if (p->local_as == new_bgp->local_as && p->is_internal && new_bgp->is_internal)
991
        {
992
          /* Redistribution of internal routes with IBGP */
993
          if (p->rr_client || new_bgp->rr_client)
994
            /* Route reflection, RFC 4456 */
995
            return bgp_update_attrs(p, e, attrs, pool, 1);
996
          else
997
            return -1;
998
        }
999
      else
1000
        return bgp_update_attrs(p, e, attrs, pool, 0);
1001
    }
1002
  else
1003
    return bgp_create_attrs(p, e, attrs, pool);
1004
}
1005

    
1006
static inline u32
1007
bgp_get_neighbor(rte *r)
1008
{
1009
  eattr *e = ea_find(r->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1010
  u32 as;
1011

    
1012
  if (e && as_path_get_first(e->u.ptr, &as))
1013
    return as;
1014
  else
1015
    return ((struct bgp_proto *) r->attrs->proto)->remote_as;
1016
}
1017

    
1018
static inline int
1019
rte_resolvable(rte *rt)
1020
{
1021
  int rd = rt->attrs->dest;  
1022
  return (rd == RTD_ROUTER) || (rd == RTD_DEVICE) || (rd == RTD_MULTIPATH);
1023
}
1024

    
1025
int
1026
bgp_rte_better(rte *new, rte *old)
1027
{
1028
  struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->proto;
1029
  struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->proto;
1030
  eattr *x, *y;
1031
  u32 n, o;
1032

    
1033
  /* RFC 4271 9.1.2.1. Route resolvability test */
1034
  n = rte_resolvable(new);
1035
  o = rte_resolvable(old);
1036
  if (n > o)
1037
    return 1;
1038
  if (n < o)
1039
    return 0;
1040

    
1041
  /* Start with local preferences */
1042
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1043
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1044
  n = x ? x->u.data : new_bgp->cf->default_local_pref;
1045
  o = y ? y->u.data : old_bgp->cf->default_local_pref;
1046
  if (n > o)
1047
    return 1;
1048
  if (n < o)
1049
    return 0;
1050

    
1051
  /* RFC 4271 9.1.2.2. a)  Use AS path lengths */
1052
  if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
1053
    {
1054
      x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1055
      y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1056
      n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1057
      o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1058
      if (n < o)
1059
        return 1;
1060
      if (n > o)
1061
        return 0;
1062
    }
1063

    
1064
  /* RFC 4271 9.1.2.2. b) Use origins */
1065
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1066
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1067
  n = x ? x->u.data : ORIGIN_INCOMPLETE;
1068
  o = y ? y->u.data : ORIGIN_INCOMPLETE;
1069
  if (n < o)
1070
    return 1;
1071
  if (n > o)
1072
    return 0;
1073

    
1074
  /* RFC 4271 9.1.2.2. c) Compare MED's */
1075
  if (bgp_get_neighbor(new) == bgp_get_neighbor(old))
1076
    {
1077
      x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1078
      y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1079
      n = x ? x->u.data : new_bgp->cf->default_med;
1080
      o = y ? y->u.data : old_bgp->cf->default_med;
1081
      if (n < o)
1082
        return 1;
1083
      if (n > o)
1084
        return 0;
1085
    }
1086

    
1087
  /* RFC 4271 9.1.2.2. d) Prefer external peers */
1088
  if (new_bgp->is_internal > old_bgp->is_internal)
1089
    return 0;
1090
  if (new_bgp->is_internal < old_bgp->is_internal)
1091
    return 1;
1092

    
1093
  /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1094
  n = new_bgp->cf->igp_metric ? new->attrs->igp_metric : 0;
1095
  o = old_bgp->cf->igp_metric ? old->attrs->igp_metric : 0;
1096
  if (n < o)
1097
    return 1;
1098
  if (n > o)
1099
    return 0;
1100

    
1101
  /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1102
  /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighor ID */
1103
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1104
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1105
  n = x ? x->u.data : new_bgp->remote_id;
1106
  o = y ? y->u.data : old_bgp->remote_id;
1107

    
1108
  /* RFC 5004 - prefer older routes */
1109
  /* (if both are external and from different peer) */
1110
  if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) &&
1111
      !new_bgp->is_internal && n != o)
1112
    return 0;
1113

    
1114
  /* rest of RFC 4271 9.1.2.2. f) */
1115
  if (n < o)
1116
    return 1;
1117
  if (n > o)
1118
    return 0;
1119

    
1120
  /* RFC 4456 9. b) Compare cluster list lengths */
1121
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1122
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1123
  n = x ? int_set_get_size(x->u.ptr) : 0;
1124
  o = y ? int_set_get_size(y->u.ptr) : 0;
1125
  if (n < o)
1126
    return 1;
1127
  if (n > o)
1128
    return 0;
1129

    
1130
  /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1131
  return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
1132
}
1133

    
1134
static struct adata *
1135
bgp_aggregator_convert_to_new(struct adata *old, struct linpool *pool)
1136
{
1137
  struct adata *newa = lp_alloc(pool, sizeof(struct adata) + 8);
1138
  newa->length = 8;
1139
  aggregator_convert_to_new(old, newa->data);
1140
  return newa;
1141
}
1142

    
1143

    
1144
/* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
1145
 * and append path old4 (in 4B format).
1146
 */
1147
static struct adata *
1148
bgp_merge_as_paths(struct adata *old2, struct adata *old4, int req_as, struct linpool *pool)
1149
{
1150
  byte buf[old2->length * 2];
1151

    
1152
  int ol = as_path_convert_to_new(old2, buf, req_as);
1153
  int nl = ol + (old4 ? old4->length : 0);
1154

    
1155
  struct adata *newa = lp_alloc(pool, sizeof(struct adata) + nl);
1156
  newa->length = nl;
1157
  memcpy(newa->data, buf, ol);
1158
  if (old4) memcpy(newa->data + ol, old4->data, old4->length);
1159

    
1160
  return newa;
1161
}
1162

    
1163
static int
1164
as4_aggregator_valid(struct adata *aggr)
1165
{
1166
  if (aggr->length != 8)
1167
    return 0;
1168

    
1169
  u32 *a = (u32 *) aggr->data;
1170

    
1171
  if ((a[0] == 0) || (a[1] == 0))
1172
    return 0;
1173

    
1174
  return 1;
1175
}
1176

    
1177

    
1178
/* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1179
static void
1180
bgp_reconstruct_4b_atts(struct bgp_proto *p, rta *a, struct linpool *pool)
1181
{
1182
  eattr *p2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1183
  eattr *p4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_PATH));
1184
  eattr *a2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AGGREGATOR));
1185
  eattr *a4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR));
1186
  int a4_removed = 0;
1187

    
1188
  if (a4 && !as4_aggregator_valid(a4->u.ptr))
1189
    {
1190
      log(L_WARN "%s: AS4_AGGREGATOR attribute is invalid, skipping attribute", p->p.name);
1191
      a4 = NULL;
1192
      a4_removed = 1;
1193
    }
1194

    
1195
  if (a2)
1196
    {
1197
      u32 a2_as = get_u16(a2->u.ptr->data);
1198

    
1199
      if (a4)
1200
        {
1201
          if (a2_as != AS_TRANS)
1202
            {
1203
              /* Routes were aggregated by old router and therefore AS4_PATH
1204
               * and AS4_AGGREGATOR is invalid
1205
               *
1206
               * Convert AS_PATH and AGGREGATOR to 4B format and finish.
1207
               */
1208

    
1209
              a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1210
              p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1211

    
1212
              return;
1213
            }
1214
          else
1215
            {
1216
              /* Common case, use AS4_AGGREGATOR attribute */
1217
              a2->u.ptr = a4->u.ptr;
1218
            }
1219
        }
1220
      else
1221
        {
1222
          /* Common case, use old AGGREGATOR attribute */
1223
          a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1224

    
1225
          if ((a2_as == AS_TRANS) && !a4_removed)
1226
            log(L_WARN "%s: AGGREGATOR attribute contain AS_TRANS, but AS4_AGGREGATOR is missing", p->p.name);
1227
        }
1228
    }
1229
  else
1230
    if (a4)
1231
      log(L_WARN "%s: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing", p->p.name);
1232

    
1233
  int p2_len = as_path_getlen_int(p2->u.ptr, 2);
1234
  int p4_len = p4 ? validate_as4_path(p, p4->u.ptr) : -1;
1235

    
1236
  if (p4 && (p4_len < 0))
1237
    log(L_WARN "%s: AS4_PATH attribute is malformed, skipping attribute", p->p.name);
1238

    
1239
  if ((p4_len <= 0) || (p2_len < p4_len))
1240
    p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1241
  else
1242
    p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, p4->u.ptr, p2_len - p4_len, pool);
1243
}
1244

    
1245
static void
1246
bgp_remove_as4_attrs(struct bgp_proto *p, rta *a)
1247
{
1248
  unsigned id1 = EA_CODE(EAP_BGP, BA_AS4_PATH);
1249
  unsigned id2 = EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR);
1250
  ea_list **el = &(a->eattrs);
1251

    
1252
  /* We know that ea_lists constructed in bgp_decode attrs have one attribute per ea_list struct */
1253
  while (*el != NULL)
1254
    {
1255
      unsigned fid = (*el)->attrs[0].id;
1256

    
1257
      if ((fid == id1) || (fid == id2))
1258
        {
1259
          *el = (*el)->next;
1260
          if (p->as4_session)
1261
            log(L_WARN "BGP: Unexpected AS4_* attributes received");
1262
        }
1263
      else
1264
        el = &((*el)->next);
1265
    }
1266
}
1267

    
1268
/**
1269
 * bgp_decode_attrs - check and decode BGP attributes
1270
 * @conn: connection
1271
 * @attr: start of attribute block
1272
 * @len: length of attribute block
1273
 * @pool: linear pool to make all the allocations in
1274
 * @mandatory: 1 iff presence of mandatory attributes has to be checked
1275
 *
1276
 * This function takes a BGP attribute block (a part of an Update message), checks
1277
 * its consistency and converts it to a list of BIRD route attributes represented
1278
 * by a &rta.
1279
 */
1280
struct rta *
1281
bgp_decode_attrs(struct bgp_conn *conn, byte *attr, unsigned int len, struct linpool *pool, int mandatory)
1282
{
1283
  struct bgp_proto *bgp = conn->bgp;
1284
  rta *a = lp_alloc(pool, sizeof(struct rta));
1285
  unsigned int flags, code, l, i, type;
1286
  int errcode;
1287
  byte *z, *attr_start;
1288
  byte seen[256/8];
1289
  ea_list *ea;
1290
  struct adata *ad;
1291

    
1292
  bzero(a, sizeof(rta));
1293
  a->proto = &bgp->p;
1294
  a->source = RTS_BGP;
1295
  a->scope = SCOPE_UNIVERSE;
1296
  a->cast = RTC_UNICAST;
1297
  /* a->dest = RTD_ROUTER;  -- set in bgp_set_next_hop() */
1298
  a->from = bgp->cf->remote_ip;
1299

    
1300
  /* Parse the attributes */
1301
  bzero(seen, sizeof(seen));
1302
  DBG("BGP: Parsing attributes\n");
1303
  while (len)
1304
    {
1305
      if (len < 2)
1306
        goto malformed;
1307
      attr_start = attr;
1308
      flags = *attr++;
1309
      code = *attr++;
1310
      len -= 2;
1311
      if (flags & BAF_EXT_LEN)
1312
        {
1313
          if (len < 2)
1314
            goto malformed;
1315
          l = get_u16(attr);
1316
          attr += 2;
1317
          len -= 2;
1318
        }
1319
      else
1320
        {
1321
          if (len < 1)
1322
            goto malformed;
1323
          l = *attr++;
1324
          len--;
1325
        }
1326
      if (l > len)
1327
        goto malformed;
1328
      len -= l;
1329
      z = attr;
1330
      attr += l;
1331
      DBG("Attr %02x %02x %d\n", code, flags, l);
1332
      if (seen[code/8] & (1 << (code%8)))
1333
        goto malformed;
1334
      if (ATTR_KNOWN(code))
1335
        {
1336
          struct attr_desc *desc = &bgp_attr_table[code];
1337
          if (desc->expected_length >= 0 && desc->expected_length != (int) l)
1338
            { errcode = 5; goto err; }
1339
          if ((desc->expected_flags ^ flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
1340
            { errcode = 4; goto err; }
1341
          if (!desc->allow_in_ebgp && !bgp->is_internal)
1342
            continue;
1343
          if (desc->validate)
1344
            {
1345
              errcode = desc->validate(bgp, z, l);
1346
              if (errcode > 0)
1347
                goto err;
1348
              if (errcode < 0)
1349
                continue;
1350
            }
1351
          else if (code == BA_AS_PATH)
1352
            {
1353
              /* Special case as it might also trim the attribute */
1354
              if (validate_as_path(bgp, z, &l) < 0)
1355
                { errcode = 11; goto err; }
1356
            }
1357
          type = desc->type;
1358
        }
1359
      else                                /* Unknown attribute */
1360
        {
1361
          if (!(flags & BAF_OPTIONAL))
1362
            { errcode = 2; goto err; }
1363
          type = EAF_TYPE_OPAQUE;
1364
        }
1365
      
1366
      // Only OPTIONAL and TRANSITIVE attributes may have non-zero PARTIAL flag
1367
      // if (!((flags & BAF_OPTIONAL) && (flags & BAF_TRANSITIVE)) && (flags & BAF_PARTIAL))
1368
      //   { errcode = 4; goto err; }
1369

    
1370
      seen[code/8] |= (1 << (code%8));
1371
      ea = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
1372
      ea->next = a->eattrs;
1373
      a->eattrs = ea;
1374
      ea->flags = 0;
1375
      ea->count = 1;
1376
      ea->attrs[0].id = EA_CODE(EAP_BGP, code);
1377
      ea->attrs[0].flags = flags;
1378
      ea->attrs[0].type = type;
1379
      if (type & EAF_EMBEDDED)
1380
        ad = NULL;
1381
      else
1382
        {
1383
          ad = lp_alloc(pool, sizeof(struct adata) + l);
1384
          ea->attrs[0].u.ptr = ad;
1385
          ad->length = l;
1386
          memcpy(ad->data, z, l);
1387
        }
1388
      switch (type)
1389
        {
1390
        case EAF_TYPE_ROUTER_ID:
1391
        case EAF_TYPE_INT:
1392
          if (l == 1)
1393
            ea->attrs[0].u.data = *z;
1394
          else
1395
            ea->attrs[0].u.data = get_u32(z);
1396
          break;
1397
        case EAF_TYPE_IP_ADDRESS:
1398
          ipa_ntoh(*(ip_addr *)ad->data);
1399
          break;
1400
        case EAF_TYPE_INT_SET:
1401
          {
1402
            u32 *z = (u32 *) ad->data;
1403
            for(i=0; i<ad->length/4; i++)
1404
              z[i] = ntohl(z[i]);
1405
            break;
1406
          }
1407
        }
1408
    }
1409

    
1410
#ifdef IPV6
1411
  /* If we received MP_REACH_NLRI we should check mandatory attributes */
1412
  if (bgp->mp_reach_len != 0)
1413
    mandatory = 1;
1414
#endif
1415

    
1416
  /* If there is no (reachability) NLRI, we should exit now */
1417
  if (! mandatory)
1418
    return a;
1419

    
1420
  /* Check if all mandatory attributes are present */
1421
  for(i=0; i < ARRAY_SIZE(bgp_mandatory_attrs); i++)
1422
    {
1423
      code = bgp_mandatory_attrs[i];
1424
      if (!(seen[code/8] & (1 << (code%8))))
1425
        {
1426
          bgp_error(conn, 3, 3, &bgp_mandatory_attrs[i], 1);
1427
          return NULL;
1428
        }
1429
    }
1430

    
1431
  /* When receiving attributes from non-AS4-aware BGP speaker,
1432
   * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1433
   */
1434
  if (! bgp->as4_session)
1435
    bgp_reconstruct_4b_atts(bgp, a, pool);
1436

    
1437
  bgp_remove_as4_attrs(bgp, a);
1438

    
1439
  /* If the AS path attribute contains our AS, reject the routes */
1440
  if (bgp_as_path_loopy(bgp, a))
1441
    goto loop;
1442

    
1443
  /* Two checks for IBGP loops caused by route reflection, RFC 4456 */ 
1444
  if (bgp_originator_id_loopy(bgp, a) ||
1445
      bgp_cluster_list_loopy(bgp, a))
1446
    goto loop;
1447

    
1448
  /* If there's no local preference, define one */
1449
  if (!(seen[0] & (1 << BA_LOCAL_PREF)))
1450
    bgp_attach_attr(&a->eattrs, pool, BA_LOCAL_PREF, bgp->cf->default_local_pref);
1451

    
1452
  return a;
1453

    
1454
loop:
1455
  DBG("BGP: Path loop!\n");
1456
  return NULL;
1457

    
1458
malformed:
1459
  bgp_error(conn, 3, 1, NULL, 0);
1460
  return NULL;
1461

    
1462
err:
1463
  bgp_error(conn, 3, errcode, attr_start, z+l-attr_start);
1464
  return NULL;
1465
}
1466

    
1467
int
1468
bgp_get_attr(eattr *a, byte *buf, int buflen)
1469
{
1470
  unsigned int i = EA_ID(a->id);
1471
  struct attr_desc *d;
1472

    
1473
  if (ATTR_KNOWN(i))
1474
    {
1475
      d = &bgp_attr_table[i];
1476
      buf += bsprintf(buf, "%s", d->name);
1477
      if (d->format)
1478
        {
1479
          *buf++ = ':';
1480
          *buf++ = ' ';
1481
          d->format(a, buf, buflen);
1482
          return GA_FULL;
1483
        }
1484
      return GA_NAME;
1485
    }
1486
  bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1487
  return GA_NAME;
1488
}
1489

    
1490
void
1491
bgp_attr_init(struct bgp_proto *p)
1492
{
1493
  p->hash_size = 256;
1494
  p->hash_limit = p->hash_size * 4;
1495
  p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
1496
  init_list(&p->bucket_queue);
1497
  p->withdraw_bucket = NULL;
1498
  fib_init(&p->prefix_fib, p->p.pool, sizeof(struct bgp_prefix), 0, bgp_init_prefix);
1499
}
1500

    
1501
void
1502
bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1503
{
1504
  eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1505
  eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1506
  u32 origas;
1507

    
1508
  buf += bsprintf(buf, " (%d", e->pref);
1509
  if (e->attrs->hostentry)
1510
    {
1511
      if (!rte_resolvable(e))
1512
        buf += bsprintf(buf, "/-");
1513
      else if (e->attrs->igp_metric >= IGP_METRIC_UNKNOWN)
1514
        buf += bsprintf(buf, "/?");
1515
      else
1516
        buf += bsprintf(buf, "/%d", e->attrs->igp_metric);
1517
    }
1518
  buf += bsprintf(buf, ") [");
1519

    
1520
  if (p && as_path_get_last(p->u.ptr, &origas))
1521
    buf += bsprintf(buf, "AS%u", origas);
1522
  if (o)
1523
    buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
1524
  strcpy(buf, "]");
1525
}