2 * BIRD -- BGP Attributes
4 * (c) 2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
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"
25 static byte bgp_mandatory_attrs
[] = { BA_ORIGIN
, BA_AS_PATH
, BA_NEXT_HOP
};
28 char *name
; /* FIXME: Use the same names as in filters */
33 int (*validate
)(struct bgp_proto
*p
, byte
*attr
, int len
);
34 void (*format
)(eattr
*ea
, byte
*buf
);
38 bgp_check_origin(struct bgp_proto
*p
, byte
*a
, int len
)
46 bgp_format_origin(eattr
*a
, byte
*buf
)
48 static char *bgp_origin_names
[] = { "IGP", "EGP", "Incomplete" };
50 bsprintf(buf
, bgp_origin_names
[a
->u
.data
]);
54 bgp_check_path(struct bgp_proto
*p
, byte
*a
, int len
)
58 DBG("Path segment %02x %02x\n", a
[0], a
[1]);
60 a
[0] != AS_PATH_SET
&& a
[0] != AS_PATH_SEQUENCE
||
70 bgp_check_next_hop(struct bgp_proto
*p
, byte
*a
, int len
)
74 memcpy(&addr
, a
, len
);
76 if (ipa_classify(addr
) & IADDR_HOST
)
82 static struct attr_desc bgp_attr_table
[] = {
83 { NULL
, -1, 0, 0, 0, /* Undefined */
85 { "origin", 1, BAF_TRANSITIVE
, EAF_TYPE_INT
, 1, /* BA_ORIGIN */
86 bgp_check_origin
, bgp_format_origin
},
87 { "as_path", -1, BAF_TRANSITIVE
, EAF_TYPE_AS_PATH
, 1, /* BA_AS_PATH */
88 bgp_check_path
, NULL
},
89 { "next_hop", 4, BAF_TRANSITIVE
, EAF_TYPE_IP_ADDRESS
, 1, /* BA_NEXT_HOP */
90 bgp_check_next_hop
, NULL
},
91 { "MED", 4, BAF_OPTIONAL
, EAF_TYPE_INT
, 0, /* BA_MULTI_EXIT_DISC */
93 { "local_pref", 4, BAF_OPTIONAL
, EAF_TYPE_INT
, 0, /* BA_LOCAL_PREF */
95 { "atomic_aggr", 0, BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_ATOMIC_AGGR */
97 { "aggregator", 6, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_AGGREGATOR */
99 { "community", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_INT_SET
, 1, /* BA_COMMUNITY */
102 { 0, 0 }, /* BA_ORIGINATOR_ID */
103 { 0, 0 }, /* BA_CLUSTER_LIST */
108 bgp_encode_attrs(byte
*w
, struct bgp_bucket
*buck
)
111 unsigned int i
, code
, flags
;
116 for(i
=0; i
<buck
->eattrs
->count
; i
++)
118 eattr
*a
= &buck
->eattrs
->attrs
[i
];
119 ASSERT(EA_PROTO(a
->id
) == EAP_BGP
);
121 flags
= a
->flags
& (BAF_OPTIONAL
| BAF_TRANSITIVE
| BAF_PARTIAL
);
122 if (code
&& code
< ARRAY_SIZE(bgp_attr_table
))
124 struct attr_desc
*desc
= &bgp_attr_table
[code
];
125 len
= desc
->expected_length
;
128 ASSERT(!(a
->type
& EAF_EMBEDDED
));
129 len
= a
->u
.ptr
->length
;
134 ASSERT((a
->type
& EAF_TYPE_MASK
) == EAF_TYPE_OPAQUE
);
135 len
= a
->u
.ptr
->length
;
137 DBG("\tAttribute %02x (type %02x, %d bytes, flags %02x)\n", code
, a
->type
, len
, flags
);
138 if (remains
< len
+ 4)
140 log(L_ERR
"BGP: attribute list too long, ignoring the remaining attributes");
152 *w
++ = flags
| BAF_EXT_LEN
;
158 switch (a
->type
& EAF_TYPE_MASK
)
161 case EAF_TYPE_ROUTER_ID
:
163 put_u32(w
, a
->u
.data
);
167 case EAF_TYPE_IP_ADDRESS
:
169 ip_addr ip
= *(ip_addr
*)a
->u
.ptr
->data
;
174 case EAF_TYPE_INT_SET
:
176 u32
*z
= (u32
*)a
->u
.ptr
->data
;
178 for(i
=0; i
<len
; i
+=4)
182 case EAF_TYPE_OPAQUE
:
183 case EAF_TYPE_AS_PATH
:
184 memcpy(w
, a
->u
.ptr
->data
, len
);
187 bug("bgp_encode_attrs: unknown attribute type %02x", a
->type
);
192 put_u16(start
, w
-start
-2);
197 bgp_init_prefix(struct fib_node
*N
)
199 struct bgp_prefix
*p
= (struct bgp_prefix
*) N
;
200 p
->bucket_node
.next
= NULL
;
204 bgp_compare_u32(const u32
*x
, const u32
*y
)
206 return (*x
< *y
) ? -1 : (*x
> *y
) ? 1 : 0;
210 bgp_normalize_set(u32
*dest
, u32
*src
, unsigned cnt
)
212 memcpy(dest
, src
, sizeof(u32
) * cnt
);
213 qsort(dest
, cnt
, sizeof(u32
), (int(*)(const void *, const void *)) bgp_compare_u32
);
217 bgp_rehash_buckets(struct bgp_proto
*p
)
219 struct bgp_bucket
**old
= p
->bucket_hash
;
220 struct bgp_bucket
**new;
221 unsigned oldn
= p
->hash_size
;
223 struct bgp_bucket
*b
;
225 p
->hash_size
= p
->hash_limit
;
226 DBG("BGP: Rehashing bucket table from %d to %d\n", oldn
, p
->hash_size
);
228 if (p
->hash_limit
>= 65536)
230 new = p
->bucket_hash
= mb_allocz(p
->p
.pool
, p
->hash_size
* sizeof(struct bgp_bucket
*));
231 mask
= p
->hash_size
- 1;
232 for (i
=0; i
<oldn
; i
++)
235 old
[i
] = b
->hash_next
;
237 b
->hash_next
= new[e
];
239 b
->hash_next
->hash_prev
= b
;
246 static struct bgp_bucket
*
247 bgp_new_bucket(struct bgp_proto
*p
, ea_list
*new, unsigned hash
)
249 struct bgp_bucket
*b
;
250 unsigned ea_size
= sizeof(ea_list
) + new->count
* sizeof(eattr
);
251 unsigned ea_size_aligned
= ALIGN(ea_size
, CPU_STRUCT_ALIGN
);
252 unsigned size
= sizeof(struct bgp_bucket
) + ea_size
;
255 unsigned index
= hash
& (p
->hash_size
- 1);
257 /* Gather total size of non-inline attributes */
258 for (i
=0; i
<new->count
; i
++)
260 eattr
*a
= &new->attrs
[i
];
261 if (!(a
->type
& EAF_EMBEDDED
))
262 size
+= ALIGN(sizeof(struct adata
) + a
->u
.ptr
->length
, CPU_STRUCT_ALIGN
);
265 /* Create the bucket and hash it */
266 b
= mb_alloc(p
->p
.pool
, size
);
267 b
->hash_next
= p
->bucket_hash
[index
];
269 b
->hash_next
->hash_prev
= b
;
270 p
->bucket_hash
[index
] = b
;
273 add_tail(&p
->bucket_queue
, &b
->send_node
);
274 init_list(&b
->prefixes
);
275 memcpy(b
->eattrs
, new, ea_size
);
276 dest
= ((byte
*)b
->eattrs
) + ea_size_aligned
;
278 /* Copy values of non-inline attributes */
279 for (i
=0; i
<new->count
; i
++)
281 eattr
*a
= &new->attrs
[i
];
282 if (!(a
->type
& EAF_EMBEDDED
))
284 struct adata
*oa
= a
->u
.ptr
;
285 struct adata
*na
= (struct adata
*) dest
;
286 memcpy(na
, oa
, sizeof(struct adata
) + oa
->length
);
288 dest
+= ALIGN(na
->length
, CPU_STRUCT_ALIGN
);
292 /* If needed, rehash */
294 if (p
->hash_count
> p
->hash_limit
)
295 bgp_rehash_buckets(p
);
301 bgp_export_check(struct bgp_proto
*p
, ea_list
*new)
306 /* Check if next hop is valid */
307 a
= ea_find(new, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
308 if (!a
|| ipa_equal(p
->next_hop
, *(ip_addr
*)a
->u
.ptr
))
310 DBG("\tInvalid NEXT_HOP\n");
314 /* Check if we aren't forbidden to export the route by communities */
315 a
= ea_find(new, EA_CODE(EAP_BGP
, BA_COMMUNITY
));
319 if (int_set_contains(d
, BGP_COMM_NO_ADVERTISE
))
321 DBG("\tNO_ADVERTISE\n");
324 if (!p
->is_internal
&&
325 (int_set_contains(d
, BGP_COMM_NO_EXPORT
) ||
326 int_set_contains(d
, BGP_COMM_NO_EXPORT_SUBCONFED
)))
328 DBG("\tNO_EXPORT\n");
336 static struct bgp_bucket
*
337 bgp_get_bucket(struct bgp_proto
*p
, ea_list
*old
, ea_list
*tmp
, int originate
)
340 unsigned i
, cnt
, hash
, code
;
343 struct bgp_bucket
*b
;
345 /* Merge the attribute lists */
346 for(t
=tmp
; t
->next
; t
=t
->next
)
349 new = alloca(ea_scan(tmp
));
354 /* Normalize attributes */
363 byte buf
[EA_FORMAT_BUF_SIZE
];
368 if (EA_PROTO(a
->id
) != EAP_BGP
)
371 if (code
< ARRAY_SIZE(bgp_attr_table
))
373 if (!bgp_attr_table
[code
].allow_in_ebgp
&& !p
->is_internal
)
375 /* The flags might have been zero if the attr was added by filters */
376 a
->flags
= (a
->flags
& BAF_PARTIAL
) | bgp_attr_table
[code
].expected_flags
;
381 if ((d
->type
& EAF_ORIGINATED
) && !originate
&& (d
->flags
& BAF_TRANSITIVE
) && (d
->flags
& BAF_OPTIONAL
))
382 d
->flags
|= BAF_PARTIAL
;
383 switch (d
->type
& EAF_TYPE_MASK
)
385 case EAF_TYPE_INT_SET
:
387 struct adata
*z
= alloca(sizeof(struct adata
) + d
->u
.ptr
->length
);
388 z
->length
= d
->u
.ptr
->length
;
389 bgp_normalize_set((u32
*) z
->data
, (u32
*) d
->u
.ptr
->data
, z
->length
/ 4);
401 for(b
=p
->bucket_hash
[hash
& (p
->hash_size
- 1)]; b
; b
=b
->hash_next
)
402 if (b
->hash
== hash
&& ea_same(b
->eattrs
, new))
404 DBG("Found bucket.\n");
408 /* Ensure that there are all mandatory attributes */
409 for(i
=0; i
<ARRAY_SIZE(bgp_mandatory_attrs
); i
++)
410 if (!(seen
& (1 << bgp_mandatory_attrs
[i
])))
412 log(L_ERR
"%s: Mandatory attribute %s missing", p
->p
.name
, bgp_attr_table
[bgp_mandatory_attrs
[i
]].name
);
416 if (!bgp_export_check(p
, new))
419 /* Create new bucket */
420 DBG("Creating bucket.\n");
421 return bgp_new_bucket(p
, new, hash
);
425 bgp_free_bucket(struct bgp_proto
*p
, struct bgp_bucket
*buck
)
428 buck
->hash_next
->hash_prev
= buck
->hash_prev
;
430 buck
->hash_prev
->hash_next
= buck
->hash_next
;
432 p
->bucket_hash
[buck
->hash
& (p
->hash_size
-1)] = buck
->hash_next
;
437 bgp_rt_notify(struct proto
*P
, net
*n
, rte
*new, rte
*old
, ea_list
*tmpa
)
439 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
440 struct bgp_bucket
*buck
;
441 struct bgp_prefix
*px
;
443 DBG("BGP: Got route %I/%d %s\n", n
->n
.prefix
, n
->n
.pxlen
, new ? "up" : "down");
447 buck
= bgp_get_bucket(p
, new->attrs
->eattrs
, tmpa
, new->attrs
->source
!= RTS_BGP
);
448 if (!buck
) /* Inconsistent attribute list */
453 if (!(buck
= p
->withdraw_bucket
))
455 buck
= p
->withdraw_bucket
= mb_alloc(P
->pool
, sizeof(struct bgp_bucket
));
456 init_list(&buck
->prefixes
);
459 px
= fib_get(&p
->prefix_fib
, &n
->n
.prefix
, n
->n
.pxlen
);
460 if (px
->bucket_node
.next
)
462 DBG("\tRemoving old entry.\n");
463 rem_node(&px
->bucket_node
);
465 add_tail(&buck
->prefixes
, &px
->bucket_node
);
466 bgp_schedule_packet(p
->conn
, PKT_UPDATE
);
470 bgp_create_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
)
472 ea_list
*ea
= lp_alloc(pool
, sizeof(ea_list
) + 4*sizeof(eattr
));
473 eattr
*a
= ea
->attrs
;
478 ea
->flags
= EALF_SORTED
;
481 a
->id
= EA_CODE(EAP_BGP
, BA_ORIGIN
);
482 a
->flags
= BAF_TRANSITIVE
;
483 a
->type
= EAF_TYPE_INT
;
484 if (rta
->source
== RTS_RIP_EXT
|| rta
->source
== RTS_OSPF_EXT
)
485 a
->u
.data
= 2; /* Incomplete */
487 a
->u
.data
= 0; /* IGP */
490 a
->id
= EA_CODE(EAP_BGP
, BA_AS_PATH
);
491 a
->flags
= BAF_TRANSITIVE
;
492 a
->type
= EAF_TYPE_AS_PATH
;
495 a
->u
.ptr
= lp_alloc(pool
, sizeof(struct adata
));
496 a
->u
.ptr
->length
= 0;
501 a
->u
.ptr
= lp_alloc(pool
, sizeof(struct adata
) + 4);
502 a
->u
.ptr
->length
= 4;
504 z
[0] = AS_PATH_SEQUENCE
;
506 put_u16(z
+2, p
->local_as
);
510 a
->id
= EA_CODE(EAP_BGP
, BA_NEXT_HOP
);
511 a
->flags
= BAF_TRANSITIVE
;
512 a
->type
= EAF_TYPE_IP_ADDRESS
;
513 a
->u
.ptr
= lp_alloc(pool
, sizeof(struct adata
) + sizeof(ip_addr
));
514 a
->u
.ptr
->length
= sizeof(ip_addr
);
515 if (p
->cf
->next_hop_self
||
517 rta
->dest
!= RTD_ROUTER
)
518 *(ip_addr
*)a
->u
.ptr
->data
= p
->local_addr
;
520 *(ip_addr
*)a
->u
.ptr
->data
= e
->attrs
->gw
;
523 a
->id
= EA_CODE(EAP_BGP
, BA_LOCAL_PREF
);
524 a
->flags
= BAF_OPTIONAL
;
525 a
->type
= EAF_TYPE_INT
;
528 return 0; /* Leave decision to the filters */
532 bgp_path_prepend(struct linpool
*pool
, eattr
*a
, ea_list
*old
, int as
)
534 struct ea_list
*e
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
535 struct adata
*olda
= a
->u
.ptr
;
538 e
->flags
= EALF_SORTED
;
540 e
->attrs
[0].id
= EA_CODE(EAP_BGP
, BA_AS_PATH
);
541 e
->attrs
[0].flags
= BAF_TRANSITIVE
;
542 e
->attrs
[0].type
= EAF_TYPE_AS_PATH
;
543 e
->attrs
[0].u
.ptr
= as_path_prepend(pool
, olda
, as
);
548 bgp_update_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
)
553 *attrs
= bgp_path_prepend(pool
, ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
)), *attrs
, p
->local_as
);
555 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
556 if (a
&& (p
->is_internal
|| (!p
->is_internal
&& e
->attrs
->iface
== p
->neigh
->iface
)))
558 /* Leave the original next hop attribute, will check later where does it point */
562 /* Need to create new one */
563 ea_list
*ea
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
566 ea
->flags
= EALF_SORTED
;
569 a
->id
= EA_CODE(EAP_BGP
, BA_NEXT_HOP
);
570 a
->flags
= BAF_TRANSITIVE
;
571 a
->type
= EAF_TYPE_IP_ADDRESS
;
572 a
->u
.ptr
= lp_alloc(pool
, sizeof(struct adata
) + sizeof(ip_addr
));
573 a
->u
.ptr
->length
= sizeof(ip_addr
);
574 *(ip_addr
*)a
->u
.ptr
->data
= p
->local_addr
;
577 return 0; /* Leave decision to the filters */
581 bgp_import_control(struct proto
*P
, rte
**new, ea_list
**attrs
, struct linpool
*pool
)
584 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
585 struct bgp_proto
*new_bgp
= (e
->attrs
->proto
->proto
== &proto_bgp
) ? (struct bgp_proto
*) e
->attrs
->proto
: NULL
;
587 if (p
== new_bgp
) /* Poison reverse updates */
591 if (p
->local_as
== new_bgp
->local_as
&& p
->is_internal
&& new_bgp
->is_internal
)
592 return -1; /* Don't redistribute internal routes with IBGP */
593 return bgp_update_attrs(p
, e
, attrs
, pool
);
596 return bgp_create_attrs(p
, e
, attrs
, pool
);
600 bgp_rte_better(rte
*new, rte
*old
)
602 struct bgp_proto
*new_bgp
= (struct bgp_proto
*) new->attrs
->proto
;
603 struct bgp_proto
*old_bgp
= (struct bgp_proto
*) old
->attrs
->proto
;
607 /* Start with local preferences */
608 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
609 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
610 n
= x
? x
->u
.data
: new_bgp
->cf
->default_local_pref
;
611 o
= y
? y
->u
.data
: old_bgp
->cf
->default_local_pref
;
617 /* Use AS path lengths */
618 if (new_bgp
->cf
->compare_path_lengths
|| old_bgp
->cf
->compare_path_lengths
)
620 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
621 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
622 n
= x
? as_path_getlen(x
->u
.ptr
) : 100000;
623 o
= y
? as_path_getlen(y
->u
.ptr
) : 100000;
631 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
632 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
633 n
= x
? x
->u
.data
: 2;
634 o
= y
? y
->u
.data
: 2;
641 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
642 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
643 n
= x
? x
->u
.data
: new_bgp
->cf
->default_med
;
644 o
= y
? y
->u
.data
: old_bgp
->cf
->default_med
;
650 /* A tie breaking procedure according to RFC 1771, section 9.1.2.1 */
651 /* We don't have interior distances */
652 /* We prefer external peers */
653 if (new_bgp
->is_internal
> old_bgp
->is_internal
)
655 if (new_bgp
->is_internal
< old_bgp
->is_internal
)
657 /* Finally we compare BGP identifiers */
658 return (new_bgp
->remote_id
< old_bgp
->remote_id
);
662 bgp_path_loopy(struct bgp_proto
*p
, eattr
*a
)
664 byte
*path
= a
->u
.ptr
->data
;
665 int len
= a
->u
.ptr
->length
;
675 if (get_u16(path
) == p
->local_as
)
684 bgp_decode_attrs(struct bgp_conn
*conn
, byte
*attr
, unsigned int len
, struct linpool
*pool
, int mandatory
)
686 struct bgp_proto
*bgp
= conn
->bgp
;
687 rta
*a
= lp_alloc(pool
, sizeof(struct rta
));
688 unsigned int flags
, code
, l
, i
, type
;
690 byte
*z
, *attr_start
;
700 a
->scope
= SCOPE_UNIVERSE
;
701 a
->cast
= RTC_UNICAST
;
702 a
->dest
= RTD_ROUTER
;
705 a
->from
= bgp
->cf
->remote_ip
;
708 /* Parse the attributes */
709 bzero(seen
, sizeof(seen
));
710 DBG("BGP: Parsing attributes\n");
719 if (flags
& BAF_EXT_LEN
)
739 DBG("Attr %02x %02x %d\n", code
, flags
, l
);
740 if (seen
[code
/8] & (1 << (code
%8)))
742 if (code
&& code
< ARRAY_SIZE(bgp_attr_table
))
744 struct attr_desc
*desc
= &bgp_attr_table
[code
];
745 if (desc
->expected_length
>= 0 && desc
->expected_length
!= (int) l
)
746 { errcode
= 5; goto err
; }
747 if ((desc
->expected_flags
^ flags
) & (BAF_OPTIONAL
| BAF_TRANSITIVE
))
748 { errcode
= 4; goto err
; }
749 if (!desc
->allow_in_ebgp
&& !bgp
->is_internal
)
753 errcode
= desc
->validate(bgp
, z
, l
);
761 else /* Unknown attribute */
763 if (!(flags
& BAF_OPTIONAL
))
764 { errcode
= 2; goto err
; }
765 type
= EAF_TYPE_OPAQUE
;
767 seen
[code
/8] |= (1 << (code
%8));
768 ea
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
769 ea
->next
= a
->eattrs
;
773 ea
->attrs
[0].id
= EA_CODE(EAP_BGP
, code
);
774 ea
->attrs
[0].flags
= flags
;
775 ea
->attrs
[0].type
= type
;
776 if (type
& EAF_EMBEDDED
)
780 ad
= lp_alloc(pool
, sizeof(struct adata
) + l
);
781 ea
->attrs
[0].u
.ptr
= ad
;
783 memcpy(ad
->data
, z
, l
);
787 case EAF_TYPE_ROUTER_ID
:
790 ea
->attrs
[0].u
.data
= *z
;
792 ea
->attrs
[0].u
.data
= get_u32(z
);
794 case EAF_TYPE_IP_ADDRESS
:
795 ipa_ntoh(*(ip_addr
*)ad
->data
);
797 case EAF_TYPE_INT_SET
:
799 u32
*z
= (u32
*) ad
->data
;
800 for(i
=0; i
<ad
->length
/4; i
++)
807 /* Check if all mandatory attributes are present */
810 for(i
=0; i
< ARRAY_SIZE(bgp_mandatory_attrs
); i
++)
812 code
= bgp_mandatory_attrs
[i
];
813 if (!(seen
[code
/8] & (1 << (code
%8))))
815 bgp_error(conn
, 3, 3, &bgp_mandatory_attrs
[i
], 1);
821 /* If the AS path attribute contains our AS, reject the routes */
822 e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
824 if (bgp_path_loopy(bgp
, e
))
826 DBG("BGP: Path loop!\n");
830 /* If there's no local preference, define one */
831 if (!(seen
[0] && (1 << BA_LOCAL_PREF
)))
833 ea
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
834 ea
->next
= a
->eattrs
;
838 ea
->attrs
[0].id
= EA_CODE(EAP_BGP
, BA_LOCAL_PREF
);
839 ea
->attrs
[0].flags
= BAF_OPTIONAL
;
840 ea
->attrs
[0].type
= EAF_TYPE_INT
;
841 ea
->attrs
[0].u
.data
= 0;
844 /* Fill in the remaining rta fields */
845 e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
847 nexthop
= *(ip_addr
*) e
->u
.ptr
->data
;
848 if (ipa_equal(nexthop
, bgp
->local_addr
))
853 neigh
= neigh_find(&bgp
->p
, &nexthop
, 0) ? : bgp
->neigh
;
855 a
->iface
= neigh
->iface
;
859 bgp_error(conn
, 3, 1, NULL
, 0);
863 bgp_error(conn
, 3, errcode
, z
-2, l
+2);
868 bgp_get_attr(eattr
*a
, byte
*buf
)
870 unsigned int i
= EA_ID(a
->id
);
873 if (i
&& i
< ARRAY_SIZE(bgp_attr_table
))
875 d
= &bgp_attr_table
[i
];
876 buf
+= bsprintf(buf
, "%s", d
->name
);
886 bsprintf(buf
, "%02x%s", i
, (a
->flags
& BAF_TRANSITIVE
) ? "[t]" : "");
891 bgp_attr_init(struct bgp_proto
*p
)
894 p
->hash_limit
= p
->hash_size
* 4;
895 p
->bucket_hash
= mb_allocz(p
->p
.pool
, p
->hash_size
* sizeof(struct bgp_bucket
*));
896 init_list(&p
->bucket_queue
);
897 p
->withdraw_bucket
= NULL
;
898 fib_init(&p
->prefix_fib
, p
->p
.pool
, sizeof(struct bgp_prefix
), 0, bgp_init_prefix
);