Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / attrs.c @ 4847a894

History | View | Annotate | Download (33.5 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);
39
};
40

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

    
49
static void
50
bgp_format_origin(eattr *a, byte *buf)
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
bgp_check_path(byte *a, int len, int bs, int errcode)
59
{
60
  while (len)
61
    {
62
      DBG("Path segment %02x %02x\n", a[0], a[1]);
63
      if (len < 2 ||
64
          (a[0] != AS_PATH_SET && a[0] != AS_PATH_SEQUENCE) ||
65
          bs * a[1] + 2 > len)
66
        return errcode;
67
      len -= bs * a[1] + 2;
68
      a += bs * a[1] + 2;
69
    }
70
  return 0;
71
}
72

    
73
static int
74
bgp_check_as_path(struct bgp_proto *p, byte *a, int len)
75
{
76
  return bgp_check_path(a, len, (bgp_as4_support && p->as4_support) ? 4 : 2, 11);
77
}
78

    
79
static int
80
bgp_check_as4_path(struct bgp_proto *p, byte *a, int len)
81
{
82
  if (bgp_as4_support && (! p->as4_support))
83
    return bgp_check_path(a, len, 4, 9);
84
  else 
85
    return 0;
86
}
87

    
88

    
89
static int
90
bgp_check_next_hop(struct bgp_proto *p UNUSED, byte *a, int len)
91
{
92
#ifdef IPV6
93
  return -1;
94
#else
95
  ip_addr addr;
96

    
97
  memcpy(&addr, a, len);
98
  ipa_ntoh(addr);
99
  if (ipa_classify(addr) & IADDR_HOST)
100
    return 0;
101
  else
102
    return 8;
103
#endif
104
}
105

    
106
static int
107
bgp_check_aggregator(struct bgp_proto *p, UNUSED byte *a, int len)
108
{
109
  int exp_len = (bgp_as4_support && p->as4_support) ? 8 : 6;
110
  
111
  return (len == exp_len) ? 0 : 5;
112
}
113

    
114
static int
115
bgp_check_cluster_list(struct bgp_proto *p UNUSED, UNUSED byte *a, int len)
116
{
117
  return ((len % 4) == 0) ? 0 : 5;
118
}
119

    
120
static int
121
bgp_check_reach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
122
{
123
#ifdef IPV6
124
  p->mp_reach_start = a;
125
  p->mp_reach_len = len;
126
#endif
127
  return -1;
128
}
129

    
130
static int
131
bgp_check_unreach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
132
{
133
#ifdef IPV6
134
  p->mp_unreach_start = a;
135
  p->mp_unreach_len = len;
136
#endif
137
  return -1;
138
}
139

    
140
static struct attr_desc bgp_attr_table[] = {
141
  { NULL, -1, 0, 0, 0,                                                                /* Undefined */
142
    NULL, NULL },
143
  { "origin", 1, BAF_TRANSITIVE, EAF_TYPE_INT, 1,                                /* BA_ORIGIN */
144
    bgp_check_origin, bgp_format_origin },
145
  { "as_path", -1, BAF_TRANSITIVE, EAF_TYPE_AS_PATH, 1,                                /* BA_AS_PATH */
146
    bgp_check_as_path, NULL },
147
  { "next_hop", 4, BAF_TRANSITIVE, EAF_TYPE_IP_ADDRESS, 1,                        /* BA_NEXT_HOP */
148
    bgp_check_next_hop, NULL },
149
  { "med", 4, BAF_OPTIONAL, EAF_TYPE_INT, 0,                                        /* BA_MULTI_EXIT_DISC */
150
    NULL, NULL },
151
  { "local_pref", 4, BAF_TRANSITIVE, EAF_TYPE_INT, 0,                                /* BA_LOCAL_PREF */
152
    NULL, NULL },
153
  { "atomic_aggr", 0, BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,                        /* BA_ATOMIC_AGGR */
154
    NULL, NULL },
155
  { "aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,        /* BA_AGGREGATOR */
156
    bgp_check_aggregator, NULL },
157
  { "community", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_INT_SET, 1,        /* BA_COMMUNITY */
158
    NULL, NULL },
159
  { "originator_id", 4, BAF_OPTIONAL, EAF_TYPE_INT, 0,                                /* BA_ORIGINATOR_ID */
160
    NULL, NULL },
161
  { "cluster_list", -1, BAF_OPTIONAL, EAF_TYPE_INT_SET, 0,                        /* BA_CLUSTER_LIST */
162
    bgp_check_cluster_list, NULL }, 
163
  { NULL, },                                                                        /* BA_DPA */
164
  { NULL, },                                                                        /* BA_ADVERTISER */
165
  { NULL, },                                                                        /* BA_RCID_PATH */
166
  { "mp_reach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1,                        /* BA_MP_REACH_NLRI */
167
    bgp_check_reach_nlri, NULL },
168
  { "mp_unreach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1,                        /* BA_MP_UNREACH_NLRI */
169
    bgp_check_unreach_nlri, NULL },
170
  { NULL, },                                                                        /* BA_EXTENDED_COMM */
171
  { "as4_path", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,                /* BA_AS4_PATH */
172
    bgp_check_as4_path, NULL },
173
  { "as4_aggregator", 8, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1,        /* BA_AS4_PATH */
174
    NULL, NULL }
175
};
176

    
177
/* BA_AS4_PATH is type EAF_TYPE_OPAQUE and not type EAF_TYPE_AS_PATH because
178
 * EAF_TYPE_AS_PATH is supposed to have different format (2 or 4 B for each ASN)
179
 * depending on bgp_as4_support variable.
180
 */
181

    
182
#define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
183

    
184
static inline struct adata *
185
bgp_alloc_adata(struct linpool *pool, unsigned len)
186
{
187
  struct adata *ad = lp_alloc(pool, sizeof(struct adata) + len);
188
  ad->length = len;
189
  return ad;
190
}
191

    
192
static void
193
bgp_set_attr(eattr *e, unsigned attr, uintptr_t val)
194
{
195
  ASSERT(ATTR_KNOWN(attr));
196
  e->id = EA_CODE(EAP_BGP, attr);
197
  e->type = bgp_attr_table[attr].type;
198
  e->flags = bgp_attr_table[attr].expected_flags;
199
  if (e->type & EAF_EMBEDDED)
200
    e->u.data = val;
201
  else
202
    e->u.ptr = (struct adata *) val;
203
}
204

    
205
static byte *
206
bgp_set_attr_wa(eattr *e, struct linpool *pool, unsigned attr, unsigned len)
207
{
208
  struct adata *ad = bgp_alloc_adata(pool, len);
209
  bgp_set_attr(e, attr, (uintptr_t) ad);
210
  return ad->data;
211
}
212

    
213
void
214
bgp_attach_attr(ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val)
215
{
216
  ea_list *a = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
217
  a->next = *to;
218
  *to = a;
219
  a->flags = EALF_SORTED;
220
  a->count = 1;
221
  bgp_set_attr(a->attrs, attr, val);
222
}
223

    
224
byte *
225
bgp_attach_attr_wa(ea_list **to, struct linpool *pool, unsigned attr, unsigned len)
226
{
227
  struct adata *ad = bgp_alloc_adata(pool, len);
228
  bgp_attach_attr(to, pool, attr, (uintptr_t) ad);
229
  return ad->data;
230
}
231

    
232
static int
233
bgp_encode_attr_hdr(byte *dst, unsigned int flags, unsigned code, int len)
234
{
235
  int wlen;
236

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

    
239
  if (len < 256)
240
    {
241
      *dst++ = flags;
242
      *dst++ = code;
243
      *dst++ = len;
244
      wlen = 3;
245
    }
246
  else
247
    {
248
      *dst++ = flags | BAF_EXT_LEN;
249
      *dst++ = code;
250
      put_u16(dst, len);
251
      wlen = 4;
252
    }
253

    
254
  return wlen;
255
}
256

    
257
static void
258
aggregator_convert_to_old(struct adata *aggr, byte *dst, int *new_used)
259
{
260
  byte *src = aggr->data;
261
  *new_used = 0;
262

    
263
  u32 as = get_u32(src);
264
  if (as > 0xFFFF) 
265
    {
266
      as = AS_TRANS;
267
      *new_used = 1;
268
    }
269
  put_u16(dst, as);
270

    
271
  /* Copy IPv4 address */
272
  memcpy(dst + 2, src + 4, 4);
273
}
274

    
275
static void
276
aggregator_convert_to_new(struct adata *aggr, byte *dst)
277
{
278
  byte *src = aggr->data;
279

    
280
  u32 as   = get_u16(src);
281
  put_u32(dst, as);
282

    
283
  /* Copy IPv4 address */
284
  memcpy(dst + 4, src + 2, 4);
285
}
286

    
287
static int
288
bgp_get_attr_len(eattr *a)
289
{
290
  int len;
291
  if (ATTR_KNOWN(EA_ID(a->id)))
292
    {
293
      int code = EA_ID(a->id);
294
      struct attr_desc *desc = &bgp_attr_table[code];
295
      len = desc->expected_length;
296
      if (len < 0)
297
        {
298
          ASSERT(!(a->type & EAF_EMBEDDED));
299
          len = a->u.ptr->length;
300
        }
301
    }
302
  else
303
    {
304
      ASSERT((a->type & EAF_TYPE_MASK) == EAF_TYPE_OPAQUE);
305
      len = a->u.ptr->length;
306
    }
307
  
308
  return len;
309
}
310

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

    
313
/**
314
 * bgp_encode_attrs - encode BGP attributes
315
 * @p: BGP instance
316
 * @w: buffer
317
 * @attrs: a list of extended attributes
318
 * @remains: remaining space in the buffer
319
 *
320
 * The bgp_encode_attrs() function takes a list of extended attributes
321
 * and converts it to its BGP representation (a part of an Update message).
322
 *
323
 * Result: Length of the attribute block generated.
324
 */
325
unsigned int
326
bgp_encode_attrs(struct bgp_proto *p, byte *w, ea_list *attrs, int remains)
327
{
328
  unsigned int i, code, flags;
329
  byte *start = w;
330
  int len, rv;
331

    
332
  for(i=0; i<attrs->count; i++)
333
    {
334
      eattr *a = &attrs->attrs[i];
335
      ASSERT(EA_PROTO(a->id) == EAP_BGP);
336
      code = EA_ID(a->id);
337
#ifdef IPV6
338
      /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
339
      if (code == BA_NEXT_HOP)
340
        continue;
341
#endif
342

    
343
      /* When AS4-aware BGP speaker is talking to non-AS4-aware BGP speaker,
344
       * we have to convert our 4B AS_PATH to 2B AS_PATH and send our AS_PATH 
345
       * as optional AS4_PATH attribute.
346
       */
347
      if ((code == BA_AS_PATH) && bgp_as4_support && (! p->as4_support))
348
        {
349
          len = a->u.ptr->length;
350

    
351
          if (remains < (len + 4))
352
            goto err_no_buffer;
353

    
354
          /* Using temporary buffer because don't know a length of created attr
355
           * and therefore a length of a header. Perhaps i should better always
356
           * use BAF_EXT_LEN. */
357
          
358
          byte buf[len];
359
          int new_used;
360
          int nl = as_path_convert_to_old(a->u.ptr, buf, &new_used);
361

    
362
          rv = bgp_encode_attr_hdr(w, BAF_TRANSITIVE, BA_AS_PATH, nl);
363
          ADVANCE(w, remains, rv);
364
          memcpy(w, buf, nl);
365
          ADVANCE(w, remains, nl);
366

    
367
          if (! new_used)
368
            continue;
369

    
370
          if (remains < (len + 4))
371
            goto err_no_buffer;
372

    
373
          /* We should discard AS_CONFED_SEQUENCE or AS_CONFED_SET path segments 
374
           * here but we don't support confederations and such paths we already
375
           * discarded in bgp_check_as_path().
376
           */
377

    
378
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_PATH, len);
379
          ADVANCE(w, remains, rv);
380
          memcpy(w, a->u.ptr->data, len);
381
          ADVANCE(w, remains, len);
382

    
383
          continue;
384
        }
385

    
386
      /* The same issue with AGGREGATOR attribute */
387
      if ((code == BA_AGGREGATOR) && bgp_as4_support && (! p->as4_support))
388
        {
389
          int new_used;
390

    
391
          len = 6;
392
          if (remains < (len + 3))
393
            goto err_no_buffer;
394

    
395
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AGGREGATOR, len);
396
          ADVANCE(w, remains, rv);
397
          aggregator_convert_to_old(a->u.ptr, w, &new_used);
398
          ADVANCE(w, remains, len);
399

    
400
          if (! new_used)
401
            continue;
402

    
403
          len = 8;
404
          if (remains < (len + 3))
405
            goto err_no_buffer;
406

    
407
          rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_AGGREGATOR, len);
408
          ADVANCE(w, remains, rv);
409
          memcpy(w, a->u.ptr->data, len);
410
          ADVANCE(w, remains, len);
411

    
412
          continue;
413
        }
414

    
415
      /* Standard path continues here ... */
416

    
417
      flags = a->flags & (BAF_OPTIONAL | BAF_TRANSITIVE | BAF_PARTIAL);
418
      len = bgp_get_attr_len(a);
419

    
420
      if (remains < len + 4)
421
        goto err_no_buffer;
422

    
423
      rv = bgp_encode_attr_hdr(w, flags, code, len);
424
      ADVANCE(w, remains, rv);
425

    
426
      switch (a->type & EAF_TYPE_MASK)
427
        {
428
        case EAF_TYPE_INT:
429
        case EAF_TYPE_ROUTER_ID:
430
          if (len == 4)
431
            put_u32(w, a->u.data);
432
          else
433
            *w = a->u.data;
434
          break;
435
        case EAF_TYPE_IP_ADDRESS:
436
          {
437
            ip_addr ip = *(ip_addr *)a->u.ptr->data;
438
            ipa_hton(ip);
439
            memcpy(w, &ip, len);
440
            break;
441
          }
442
        case EAF_TYPE_INT_SET:
443
          {
444
            u32 *z = (u32 *)a->u.ptr->data;
445
            int i;
446
            for(i=0; i<len; i+=4)
447
              put_u32(w+i, *z++);
448
            break;
449
          }
450
        case EAF_TYPE_OPAQUE:
451
        case EAF_TYPE_AS_PATH:
452
          memcpy(w, a->u.ptr->data, len);
453
          break;
454
        default:
455
          bug("bgp_encode_attrs: unknown attribute type %02x", a->type);
456
        }
457
      ADVANCE(w, remains, len);
458
    }
459
  return w - start;
460

    
461
 err_no_buffer:
462
  log(L_ERR "BGP: attribute list too long, ignoring the remaining attributes");
463
  return w - start;
464
}
465

    
466
static void
467
bgp_init_prefix(struct fib_node *N)
468
{
469
  struct bgp_prefix *p = (struct bgp_prefix *) N;
470
  p->bucket_node.next = NULL;
471
}
472

    
473
static int
474
bgp_compare_u32(const u32 *x, const u32 *y)
475
{
476
  return (*x < *y) ? -1 : (*x > *y) ? 1 : 0;
477
}
478

    
479
static void
480
bgp_normalize_set(u32 *dest, u32 *src, unsigned cnt)
481
{
482
  memcpy(dest, src, sizeof(u32) * cnt);
483
  qsort(dest, cnt, sizeof(u32), (int(*)(const void *, const void *)) bgp_compare_u32);
484
}
485

    
486
static void
487
bgp_rehash_buckets(struct bgp_proto *p)
488
{
489
  struct bgp_bucket **old = p->bucket_hash;
490
  struct bgp_bucket **new;
491
  unsigned oldn = p->hash_size;
492
  unsigned i, e, mask;
493
  struct bgp_bucket *b;
494

    
495
  p->hash_size = p->hash_limit;
496
  DBG("BGP: Rehashing bucket table from %d to %d\n", oldn, p->hash_size);
497
  p->hash_limit *= 4;
498
  if (p->hash_limit >= 65536)
499
    p->hash_limit = ~0;
500
  new = p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
501
  mask = p->hash_size - 1;
502
  for (i=0; i<oldn; i++)
503
    while (b = old[i])
504
      {
505
        old[i] = b->hash_next;
506
        e = b->hash & mask;
507
        b->hash_next = new[e];
508
        if (b->hash_next)
509
          b->hash_next->hash_prev = b;
510
        b->hash_prev = NULL;
511
        new[e] = b;
512
      }
513
  mb_free(old);
514
}
515

    
516
static struct bgp_bucket *
517
bgp_new_bucket(struct bgp_proto *p, ea_list *new, unsigned hash)
518
{
519
  struct bgp_bucket *b;
520
  unsigned ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
521
  unsigned ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
522
  unsigned size = sizeof(struct bgp_bucket) + ea_size;
523
  unsigned i;
524
  byte *dest;
525
  unsigned index = hash & (p->hash_size - 1);
526

    
527
  /* Gather total size of non-inline attributes */
528
  for (i=0; i<new->count; i++)
529
    {
530
      eattr *a = &new->attrs[i];
531
      if (!(a->type & EAF_EMBEDDED))
532
        size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
533
    }
534

    
535
  /* Create the bucket and hash it */
536
  b = mb_alloc(p->p.pool, size);
537
  b->hash_next = p->bucket_hash[index];
538
  if (b->hash_next)
539
    b->hash_next->hash_prev = b;
540
  p->bucket_hash[index] = b;
541
  b->hash_prev = NULL;
542
  b->hash = hash;
543
  add_tail(&p->bucket_queue, &b->send_node);
544
  init_list(&b->prefixes);
545
  memcpy(b->eattrs, new, ea_size);
546
  dest = ((byte *)b->eattrs) + ea_size_aligned;
547

    
548
  /* Copy values of non-inline attributes */
549
  for (i=0; i<new->count; i++)
550
    {
551
      eattr *a = &b->eattrs->attrs[i];
552
      if (!(a->type & EAF_EMBEDDED))
553
        {
554
          struct adata *oa = a->u.ptr;
555
          struct adata *na = (struct adata *) dest;
556
          memcpy(na, oa, sizeof(struct adata) + oa->length);
557
          a->u.ptr = na;
558
          dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
559
        }
560
    }
561

    
562
  /* If needed, rehash */
563
  p->hash_count++;
564
  if (p->hash_count > p->hash_limit)
565
    bgp_rehash_buckets(p);
566

    
567
  return b;
568
}
569

    
570
static int
571
bgp_export_check(struct bgp_proto *p, ea_list *new)
572
{
573
  eattr *a;
574
  struct adata *d;
575

    
576
  /* Check if next hop is valid */
577
  a = ea_find(new, EA_CODE(EAP_BGP, BA_NEXT_HOP));
578
  if (!a || ipa_equal(p->next_hop, *(ip_addr *)a->u.ptr))
579
    {
580
      DBG("\tInvalid NEXT_HOP\n");
581
      return 0;
582
    }
583

    
584
  /* Check if we aren't forbidden to export the route by communities */
585
  a = ea_find(new, EA_CODE(EAP_BGP, BA_COMMUNITY));
586
  if (a)
587
    {
588
      d = a->u.ptr;
589
      if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
590
        {
591
          DBG("\tNO_ADVERTISE\n");
592
          return 0;
593
        }
594
      if (!p->is_internal &&
595
          (int_set_contains(d, BGP_COMM_NO_EXPORT) ||
596
           int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED)))
597
        {
598
          DBG("\tNO_EXPORT\n");
599
          return 0;
600
        }
601
    }
602

    
603
  return 1;
604
}
605

    
606
static struct bgp_bucket *
607
bgp_get_bucket(struct bgp_proto *p, ea_list *attrs, int originate)
608
{
609
  ea_list *new;
610
  unsigned i, cnt, hash, code;
611
  eattr *a, *d;
612
  u32 seen = 0;
613
  struct bgp_bucket *b;
614

    
615
  /* Merge the attribute list */
616
  new = alloca(ea_scan(attrs));
617
  ea_merge(attrs, new);
618
  ea_sort(new);
619

    
620
  /* Normalize attributes */
621
  d = new->attrs;
622
  cnt = new->count;
623
  new->count = 0;
624
  for(i=0; i<cnt; i++)
625
    {
626
      a = &new->attrs[i];
627
#ifdef LOCAL_DEBUG
628
      {
629
        byte buf[EA_FORMAT_BUF_SIZE];
630
        ea_format(a, buf);
631
        DBG("\t%s\n", buf);
632
      }
633
#endif
634
      if (EA_PROTO(a->id) != EAP_BGP)
635
        continue;
636
      code = EA_ID(a->id);
637
      if (ATTR_KNOWN(code))
638
        {
639
          if (!bgp_attr_table[code].allow_in_ebgp && !p->is_internal)
640
            continue;
641
          /* The flags might have been zero if the attr was added by filters */
642
          a->flags = (a->flags & BAF_PARTIAL) | bgp_attr_table[code].expected_flags;
643
          if (code < 32)
644
            seen |= 1 << code;
645
        }
646
      else
647
        {
648
          /* Don't re-export unknown non-transitive attributes */
649
          if (!(a->flags & BAF_TRANSITIVE))
650
            continue;
651
        }
652
      *d = *a;
653
      if ((d->type & EAF_ORIGINATED) && !originate && (d->flags & BAF_TRANSITIVE) && (d->flags & BAF_OPTIONAL))
654
        d->flags |= BAF_PARTIAL;
655
      switch (d->type & EAF_TYPE_MASK)
656
        {
657
        case EAF_TYPE_INT_SET:
658
          {
659
            struct adata *z = alloca(sizeof(struct adata) + d->u.ptr->length);
660
            z->length = d->u.ptr->length;
661
            bgp_normalize_set((u32 *) z->data, (u32 *) d->u.ptr->data, z->length / 4);
662
            d->u.ptr = z;
663
            break;
664
          }
665
        default: ;
666
        }
667
      d++;
668
      new->count++;
669
    }
670

    
671
  /* Hash */
672
  hash = ea_hash(new);
673
  for(b=p->bucket_hash[hash & (p->hash_size - 1)]; b; b=b->hash_next)
674
    if (b->hash == hash && ea_same(b->eattrs, new))
675
      {
676
        DBG("Found bucket.\n");
677
        return b;
678
      }
679

    
680
  /* Ensure that there are all mandatory attributes */
681
  for(i=0; i<ARRAY_SIZE(bgp_mandatory_attrs); i++)
682
    if (!(seen & (1 << bgp_mandatory_attrs[i])))
683
      {
684
        log(L_ERR "%s: Mandatory attribute %s missing", p->p.name, bgp_attr_table[bgp_mandatory_attrs[i]].name);
685
        return NULL;
686
      }
687

    
688
  if (!bgp_export_check(p, new))
689
    return NULL;
690

    
691
  /* Create new bucket */
692
  DBG("Creating bucket.\n");
693
  return bgp_new_bucket(p, new, hash);
694
}
695

    
696
void
697
bgp_free_bucket(struct bgp_proto *p, struct bgp_bucket *buck)
698
{
699
  if (buck->hash_next)
700
    buck->hash_next->hash_prev = buck->hash_prev;
701
  if (buck->hash_prev)
702
    buck->hash_prev->hash_next = buck->hash_next;
703
  else
704
    p->bucket_hash[buck->hash & (p->hash_size-1)] = buck->hash_next;
705
  mb_free(buck);
706
}
707

    
708
void
709
bgp_rt_notify(struct proto *P, net *n, rte *new, rte *old UNUSED, ea_list *attrs)
710
{
711
  struct bgp_proto *p = (struct bgp_proto *) P;
712
  struct bgp_bucket *buck;
713
  struct bgp_prefix *px;
714

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

    
717
  if (new)
718
    {
719
      buck = bgp_get_bucket(p, attrs, new->attrs->source != RTS_BGP);
720
      if (!buck)                        /* Inconsistent attribute list */
721
        return;
722
    }
723
  else
724
    {
725
      if (!(buck = p->withdraw_bucket))
726
        {
727
          buck = p->withdraw_bucket = mb_alloc(P->pool, sizeof(struct bgp_bucket));
728
          init_list(&buck->prefixes);
729
        }
730
    }
731
  px = fib_get(&p->prefix_fib, &n->n.prefix, n->n.pxlen);
732
  if (px->bucket_node.next)
733
    {
734
      DBG("\tRemoving old entry.\n");
735
      rem_node(&px->bucket_node);
736
    }
737
  add_tail(&buck->prefixes, &px->bucket_node);
738
  bgp_schedule_packet(p->conn, PKT_UPDATE);
739
}
740

    
741

    
742
static int
743
bgp_create_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool)
744
{
745
  ea_list *ea = lp_alloc(pool, sizeof(ea_list) + 4*sizeof(eattr));
746
  rta *rta = e->attrs;
747
  byte *z;
748

    
749
  ea->next = *attrs;
750
  *attrs = ea;
751
  ea->flags = EALF_SORTED;
752
  ea->count = 4;
753

    
754
  bgp_set_attr(ea->attrs, BA_ORIGIN,
755
       ((rta->source == RTS_OSPF_EXT1) || (rta->source == RTS_OSPF_EXT2)) ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
756

    
757
  if (p->is_internal)
758
    bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 0);
759
  else
760
    {
761
      z = bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, bgp_as4_support ? 6 : 4);
762
      z[0] = AS_PATH_SEQUENCE;
763
      z[1] = 1;                                /* 1 AS */
764

    
765
      if (bgp_as4_support)
766
        put_u32(z+2, p->local_as);
767
      else
768
        put_u16(z+2, p->local_as);
769
    }
770

    
771
  z = bgp_set_attr_wa(ea->attrs+2, pool, BA_NEXT_HOP, sizeof(ip_addr));
772
  if (p->cf->next_hop_self ||
773
      !p->is_internal ||
774
      rta->dest != RTD_ROUTER)
775
    {
776
      if (ipa_nonzero(p->cf->source_addr))
777
        *(ip_addr *)z = p->cf->source_addr;
778
      else
779
        *(ip_addr *)z = p->local_addr;
780
    }
781
  else
782
    *(ip_addr *)z = e->attrs->gw;
783

    
784
  bgp_set_attr(ea->attrs+3, BA_LOCAL_PREF, 0);
785

    
786
  return 0;                                /* Leave decision to the filters */
787
}
788

    
789

    
790
static inline int
791
bgp_as_path_loopy(struct bgp_proto *p, rta *a)
792
{
793
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
794
  return (e && as_path_is_member(e->u.ptr, p->local_as));
795
}
796

    
797
static inline int
798
bgp_originator_id_loopy(struct bgp_proto *p, rta *a)
799
{
800
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
801
  return (e && (e->u.data == p->local_id));
802
}
803

    
804
static inline int
805
bgp_cluster_list_loopy(struct bgp_proto *p, rta *a)
806
{
807
  eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
808
  return (e && p->rr_client && int_set_contains(e->u.ptr, p->rr_cluster_id));
809
}
810

    
811

    
812
static inline void
813
bgp_path_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 as)
814
{
815
  eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
816
  bgp_attach_attr(attrs, pool, BA_AS_PATH, (uintptr_t) as_path_prepend(pool, a->u.ptr, as));
817
}
818

    
819
static inline void
820
bgp_cluster_list_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 cid)
821
{
822
  eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
823
  bgp_attach_attr(attrs, pool, BA_CLUSTER_LIST, (uintptr_t) int_set_add(pool, a ? a->u.ptr : NULL, cid));
824
}
825

    
826
static int
827
bgp_update_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool, int rr)
828
{
829
  eattr *a;
830

    
831
  if (!p->is_internal)
832
    bgp_path_prepend(e, attrs, pool, p->local_as);
833

    
834
  a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_NEXT_HOP));
835
  if (a && (p->is_internal || (!p->is_internal && e->attrs->iface == p->neigh->iface)))
836
    {
837
      /* Leave the original next hop attribute, will check later where does it point */
838
    }
839
  else
840
    {
841
      /* Need to create new one */
842
      bgp_attach_attr_ip(attrs, pool, BA_NEXT_HOP, p->local_addr);
843
    }
844

    
845
  if (rr)
846
    {
847
      /* Handling route reflection, RFC 4456 */
848
      struct bgp_proto *src = (struct bgp_proto *) e->attrs->proto;
849

    
850
      a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
851
      if (!a)
852
        bgp_attach_attr(attrs, pool, BA_ORIGINATOR_ID, src->remote_id);
853

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

    
857
      /* Two RR clients with different cluster ID, hmmm */
858
      if (src->rr_client && p->rr_client && (src->rr_cluster_id != p->rr_cluster_id))
859
        bgp_cluster_list_prepend(e, attrs, pool, p->rr_cluster_id);
860
    }
861

    
862
  return 0;                                /* Leave decision to the filters */
863
}
864

    
865
int
866
bgp_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
867
{
868
  rte *e = *new;
869
  struct bgp_proto *p = (struct bgp_proto *) P;
870
  struct bgp_proto *new_bgp = (e->attrs->proto->proto == &proto_bgp) ? (struct bgp_proto *) e->attrs->proto : NULL;
871

    
872
  if (p == new_bgp)                        /* Poison reverse updates */
873
    return -1;
874
  if (new_bgp)
875
    {
876
      /* We should check here for cluster list loop, because the receiving BGP instance
877
         might have different cluster ID  */
878
      if (bgp_cluster_list_loopy(p, e->attrs))
879
        return -1;
880

    
881
      if (p->local_as == new_bgp->local_as && p->is_internal && new_bgp->is_internal)
882
        {
883
          /* Redistribution of internal routes with IBGP */
884
          if (p->rr_client || new_bgp->rr_client)
885
            /* Route reflection, RFC 4456 */
886
            return bgp_update_attrs(p, e, attrs, pool, 1);
887
          else
888
            return -1;
889
        }
890
      else
891
        return bgp_update_attrs(p, e, attrs, pool, 0);
892
    }
893
  else
894
    return bgp_create_attrs(p, e, attrs, pool);
895
}
896

    
897
int
898
bgp_rte_better(rte *new, rte *old)
899
{
900
  struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->proto;
901
  struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->proto;
902
  eattr *x, *y;
903
  u32 n, o;
904

    
905
  /* Start with local preferences */
906
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
907
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
908
  n = x ? x->u.data : new_bgp->cf->default_local_pref;
909
  o = y ? y->u.data : old_bgp->cf->default_local_pref;
910
  if (n > o)
911
    return 1;
912
  if (n < o)
913
    return 0;
914

    
915
  /* RFC 4271 9.1.2.2. a)  Use AS path lengths */
916
  if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
917
    {
918
      x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
919
      y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
920
      n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
921
      o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
922
      if (n < o)
923
        return 1;
924
      if (n > o)
925
        return 0;
926
    }
927

    
928
  /* RFC 4271 9.1.2.2. b) Use origins */
929
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
930
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
931
  n = x ? x->u.data : ORIGIN_INCOMPLETE;
932
  o = y ? y->u.data : ORIGIN_INCOMPLETE;
933
  if (n < o)
934
    return 1;
935
  if (n > o)
936
    return 0;
937

    
938
  /* RFC 4271 9.1.2.2. c) Compare MED's */
939
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
940
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
941
  n = x ? x->u.data : new_bgp->cf->default_med;
942
  o = y ? y->u.data : old_bgp->cf->default_med;
943
  if (n < o)
944
    return 1;
945
  if (n > o)
946
    return 0;
947

    
948
  /* RFC 4271 9.1.2.2. d) Prefer external peers */
949
  if (new_bgp->is_internal > old_bgp->is_internal)
950
    return 0;
951
  if (new_bgp->is_internal < old_bgp->is_internal)
952
    return 1;
953

    
954
  /* Skipping RFC 4271 9.1.2.2. e) */
955
  /* We don't have interior distances */
956

    
957
  /* RFC 4456 9. b) Compare cluster list lengths */
958
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
959
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
960
  n = x ? int_set_get_size(x->u.ptr) : 0;
961
  o = y ? int_set_get_size(y->u.ptr) : 0;
962
  if (n < o)
963
    return 1;
964
  if (n > o)
965
    return 0;
966

    
967
  /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
968
  /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighor ID */
969
  x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
970
  y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
971
  n = x ? x->u.data : new_bgp->remote_id;
972
  o = y ? y->u.data : old_bgp->remote_id;
973
  if (n < o)
974
    return 1;
975
  if (n > o)
976
    return 0;
977

    
978

    
979
  /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
980
  return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
981
}
982

    
983
static struct adata *
984
bgp_aggregator_convert_to_new(struct adata *old, struct linpool *pool)
985
{
986
  struct adata *newa = lp_alloc(pool, sizeof(struct adata) + 8);
987
  newa->length = 8;
988
  aggregator_convert_to_new(old, newa->data);
989
  return newa;
990
}
991

    
992

    
993
/* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
994
 * and append path old4 (in 4B format).
995
 */
996
static struct adata *
997
bgp_merge_as_paths(struct adata *old2, struct adata *old4, int req_as, struct linpool *pool)
998
{
999
  byte buf[old2->length * 2];
1000

    
1001
  int ol = as_path_convert_to_new(old2, buf, req_as);
1002
  int nl = ol + (old4 ? old4->length : 0);
1003

    
1004
  struct adata *newa = lp_alloc(pool, sizeof(struct adata) + nl);
1005
  newa->length = nl;
1006
  memcpy(newa->data, buf, ol);
1007
  if (old4) memcpy(newa->data + ol, old4->data, old4->length);
1008

    
1009
  return newa;
1010
}
1011

    
1012

    
1013
/* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1014
static void
1015
bgp_reconstruct_4b_atts(struct bgp_proto *p, rta *a, struct linpool *pool)
1016
{
1017
  eattr *p2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1018
  eattr *p4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_PATH));
1019
  eattr *a2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AGGREGATOR));
1020
  eattr *a4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR));
1021

    
1022
  if (a2)
1023
    {
1024
      u32 a2_as = get_u16(a2->u.ptr->data);
1025

    
1026
      if (a4)
1027
        {
1028
          if (a2_as != AS_TRANS)
1029
            {
1030
              /* Routes were aggregated by old router and therefore AS4_PATH
1031
               * and AS4_AGGREGATOR is invalid
1032
               *
1033
               * Convert AS_PATH and AGGREGATOR to 4B format and finish.
1034
               */
1035

    
1036
              a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1037
              p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1038

    
1039
              return;
1040
            }
1041
          else
1042
            {
1043
              /* Common case, use AS4_AGGREGATOR attribute */
1044
              a2->u.ptr = a4->u.ptr;
1045
            }
1046
        }
1047
      else
1048
        {
1049
          /* Common case, use old AGGREGATOR attribute */
1050
          a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1051

    
1052
          if (a2_as == AS_TRANS)
1053
            log(L_WARN "BGP: AGGREGATOR attribute contain AS_TRANS, but AS4_AGGREGATOR is missing");
1054
        }
1055
    }
1056
  else
1057
    if (a4)
1058
      log(L_WARN "BGP: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing");
1059

    
1060
  int p2_len = as_path_getlen(p2->u.ptr);
1061
  int p4_len = p4 ? as_path_getlen(p4->u.ptr) : AS_PATH_MAXLEN;
1062

    
1063
  if (p2_len < p4_len)
1064
    p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1065
  else
1066
    p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, p4->u.ptr, p2_len - p4_len, pool);
1067

    
1068
}
1069

    
1070
static void
1071
bgp_remove_as4_attrs(struct bgp_proto *p, rta *a)
1072
{
1073
  unsigned id1 = EA_CODE(EAP_BGP, BA_AS4_PATH);
1074
  unsigned id2 = EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR);
1075
  ea_list **el = &(a->eattrs);
1076

    
1077
  /* We know that ea_lists constructed in bgp_decode_attrs have one attribute per ea_list struct */
1078
  while (*el != NULL)
1079
    {
1080
      unsigned fid = (*el)->attrs[0].id;
1081

    
1082
      if ((fid == id1) || (fid == id2))
1083
        {
1084
          *el = (*el)->next;
1085
          if (p->as4_support)
1086
            log(L_WARN "BGP: Unexpected AS4_* attributes received");
1087
        }
1088
      else
1089
        el = &((*el)->next);
1090
    }
1091
}
1092

    
1093
/**
1094
 * bgp_decode_attrs - check and decode BGP attributes
1095
 * @conn: connection
1096
 * @attr: start of attribute block
1097
 * @len: length of attribute block
1098
 * @pool: linear pool to make all the allocations in
1099
 * @mandatory: 1 iff presence of mandatory attributes has to be checked
1100
 *
1101
 * This function takes a BGP attribute block (a part of an Update message), checks
1102
 * its consistency and converts it to a list of BIRD route attributes represented
1103
 * by a &rta.
1104
 */
1105
struct rta *
1106
bgp_decode_attrs(struct bgp_conn *conn, byte *attr, unsigned int len, struct linpool *pool, int mandatory)
1107
{
1108
  struct bgp_proto *bgp = conn->bgp;
1109
  rta *a = lp_alloc(pool, sizeof(struct rta));
1110
  unsigned int flags, code, l, i, type;
1111
  int errcode;
1112
  byte *z, *attr_start;
1113
  byte seen[256/8];
1114
  eattr *e;
1115
  ea_list *ea;
1116
  struct adata *ad;
1117

    
1118
  a->proto = &bgp->p;
1119
  a->source = RTS_BGP;
1120
  a->scope = SCOPE_UNIVERSE;
1121
  a->cast = RTC_UNICAST;
1122
  a->dest = RTD_ROUTER;
1123
  a->flags = 0;
1124
  a->aflags = 0;
1125
  a->from = bgp->cf->remote_ip;
1126
  a->eattrs = NULL;
1127

    
1128
  /* Parse the attributes */
1129
  bzero(seen, sizeof(seen));
1130
  DBG("BGP: Parsing attributes\n");
1131
  while (len)
1132
    {
1133
      if (len < 2)
1134
        goto malformed;
1135
      attr_start = attr;
1136
      flags = *attr++;
1137
      code = *attr++;
1138
      len -= 2;
1139
      if (flags & BAF_EXT_LEN)
1140
        {
1141
          if (len < 2)
1142
            goto malformed;
1143
          l = get_u16(attr);
1144
          attr += 2;
1145
          len -= 2;
1146
        }
1147
      else
1148
        {
1149
          if (len < 1)
1150
            goto malformed;
1151
          l = *attr++;
1152
          len--;
1153
        }
1154
      if (l > len)
1155
        goto malformed;
1156
      len -= l;
1157
      z = attr;
1158
      attr += l;
1159
      DBG("Attr %02x %02x %d\n", code, flags, l);
1160
      if (seen[code/8] & (1 << (code%8)))
1161
        goto malformed;
1162
      if (ATTR_KNOWN(code))
1163
        {
1164
          struct attr_desc *desc = &bgp_attr_table[code];
1165
          if (desc->expected_length >= 0 && desc->expected_length != (int) l)
1166
            { errcode = 5; goto err; }
1167
          if ((desc->expected_flags ^ flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
1168
            { errcode = 4; goto err; }
1169
          if (!desc->allow_in_ebgp && !bgp->is_internal)
1170
            continue;
1171
          if (desc->validate)
1172
            {
1173
              errcode = desc->validate(bgp, z, l);
1174
              if (errcode > 0)
1175
                goto err;
1176
              if (errcode < 0)
1177
                continue;
1178
            }
1179
          type = desc->type;
1180
        }
1181
      else                                /* Unknown attribute */
1182
        {
1183
          if (!(flags & BAF_OPTIONAL))
1184
            { errcode = 2; goto err; }
1185
          type = EAF_TYPE_OPAQUE;
1186
        }
1187
      seen[code/8] |= (1 << (code%8));
1188
      ea = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
1189
      ea->next = a->eattrs;
1190
      a->eattrs = ea;
1191
      ea->flags = 0;
1192
      ea->count = 1;
1193
      ea->attrs[0].id = EA_CODE(EAP_BGP, code);
1194
      ea->attrs[0].flags = flags;
1195
      ea->attrs[0].type = type;
1196
      if (type & EAF_EMBEDDED)
1197
        ad = NULL;
1198
      else
1199
        {
1200
          ad = lp_alloc(pool, sizeof(struct adata) + l);
1201
          ea->attrs[0].u.ptr = ad;
1202
          ad->length = l;
1203
          memcpy(ad->data, z, l);
1204
        }
1205
      switch (type)
1206
        {
1207
        case EAF_TYPE_ROUTER_ID:
1208
        case EAF_TYPE_INT:
1209
          if (l == 1)
1210
            ea->attrs[0].u.data = *z;
1211
          else
1212
            ea->attrs[0].u.data = get_u32(z);
1213
          break;
1214
        case EAF_TYPE_IP_ADDRESS:
1215
          ipa_ntoh(*(ip_addr *)ad->data);
1216
          break;
1217
        case EAF_TYPE_INT_SET:
1218
          {
1219
            u32 *z = (u32 *) ad->data;
1220
            for(i=0; i<ad->length/4; i++)
1221
              z[i] = ntohl(z[i]);
1222
            break;
1223
          }
1224
        }
1225
    }
1226

    
1227
#ifdef IPV6
1228
  if (seen[BA_MP_REACH_NLRI / 8] & (1 << (BA_MP_REACH_NLRI % 8)))
1229
    mandatory = 1;
1230
#endif
1231

    
1232
  /* Check if all mandatory attributes are present */
1233
  if (mandatory)
1234
    {
1235
      for(i=0; i < ARRAY_SIZE(bgp_mandatory_attrs); i++)
1236
        {
1237
          code = bgp_mandatory_attrs[i];
1238
          if (!(seen[code/8] & (1 << (code%8))))
1239
            {
1240
              bgp_error(conn, 3, 3, &bgp_mandatory_attrs[i], 1);
1241
              return NULL;
1242
            }
1243
        }
1244
    }
1245
  
1246
  /* When receiving attributes from non-AS4-aware BGP speaker,
1247
   * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1248
   */
1249
  if (bgp_as4_support && (! bgp->as4_support))
1250
    bgp_reconstruct_4b_atts(bgp, a, pool);
1251

    
1252
  if (bgp_as4_support)
1253
    bgp_remove_as4_attrs(bgp, a);
1254

    
1255
  /* If the AS path attribute contains our AS, reject the routes */
1256
  if (bgp_as_path_loopy(bgp, a))
1257
    goto loop;
1258

    
1259
  /* Two checks for IBGP loops caused by route reflection, RFC 4456 */ 
1260
  if (bgp_originator_id_loopy(bgp, a) ||
1261
      bgp_cluster_list_loopy(bgp, a))
1262
    goto loop;
1263

    
1264
  /* If there's no local preference, define one */
1265
  if (!(seen[0] && (1 << BA_LOCAL_PREF)))
1266
    bgp_attach_attr(&a->eattrs, pool, BA_LOCAL_PREF, 0);
1267
  return a;
1268

    
1269
loop:
1270
  DBG("BGP: Path loop!\n");
1271
  return NULL;
1272

    
1273
malformed:
1274
  bgp_error(conn, 3, 1, NULL, 0);
1275
  return NULL;
1276

    
1277
err:
1278
  bgp_error(conn, 3, errcode, attr_start, z+l-attr_start);
1279
  return NULL;
1280
}
1281

    
1282
int
1283
bgp_get_attr(eattr *a, byte *buf)
1284
{
1285
  unsigned int i = EA_ID(a->id);
1286
  struct attr_desc *d;
1287

    
1288
  if (ATTR_KNOWN(i))
1289
    {
1290
      d = &bgp_attr_table[i];
1291
      buf += bsprintf(buf, "%s", d->name);
1292
      if (d->format)
1293
        {
1294
          *buf++ = ':';
1295
          *buf++ = ' ';
1296
          d->format(a, buf);
1297
          return GA_FULL;
1298
        }
1299
      return GA_NAME;
1300
    }
1301
  bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1302
  return GA_NAME;
1303
}
1304

    
1305
void
1306
bgp_attr_init(struct bgp_proto *p)
1307
{
1308
  p->hash_size = 256;
1309
  p->hash_limit = p->hash_size * 4;
1310
  p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
1311
  init_list(&p->bucket_queue);
1312
  p->withdraw_bucket = NULL;
1313
  fib_init(&p->prefix_fib, p->p.pool, sizeof(struct bgp_prefix), 0, bgp_init_prefix);
1314
}
1315

    
1316
void
1317
bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1318
{
1319
  eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1320
  eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1321
  u32 origas;
1322

    
1323
  buf += bsprintf(buf, " (%d) [", e->pref);
1324
  if (p && as_path_get_first(p->u.ptr, &origas))
1325
    buf += bsprintf(buf, "AS%u", origas);
1326
  if (o)
1327
    buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
1328
  strcpy(buf, "]");
1329
}