1 /* BGP attributes management routines.
2 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_aspath.h"
40 #include "bgpd/bgp_community.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_errors.h"
43 #include "bgpd/bgp_label.h"
44 #include "bgpd/bgp_packet.h"
45 #include "bgpd/bgp_ecommunity.h"
46 #include "bgpd/bgp_lcommunity.h"
47 #include "bgpd/bgp_updgrp.h"
48 #include "bgpd/bgp_encap_types.h"
50 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
51 #include "bgp_encap_types.h"
52 #include "bgp_vnc_types.h"
54 #include "bgp_encap_types.h"
56 #include "bgp_flowspec_private.h"
58 /* Attribute strings for logging. */
59 static const struct message attr_str
[] = {
60 {BGP_ATTR_ORIGIN
, "ORIGIN"},
61 {BGP_ATTR_AS_PATH
, "AS_PATH"},
62 {BGP_ATTR_NEXT_HOP
, "NEXT_HOP"},
63 {BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC"},
64 {BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF"},
65 {BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE"},
66 {BGP_ATTR_AGGREGATOR
, "AGGREGATOR"},
67 {BGP_ATTR_COMMUNITIES
, "COMMUNITY"},
68 {BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID"},
69 {BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST"},
70 {BGP_ATTR_DPA
, "DPA"},
71 {BGP_ATTR_ADVERTISER
, "ADVERTISER"},
72 {BGP_ATTR_RCID_PATH
, "RCID_PATH"},
73 {BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI"},
74 {BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI"},
75 {BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES"},
76 {BGP_ATTR_AS4_PATH
, "AS4_PATH"},
77 {BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR"},
78 {BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT"},
79 {BGP_ATTR_PMSI_TUNNEL
, "PMSI_TUNNEL_ATTRIBUTE"},
80 {BGP_ATTR_ENCAP
, "ENCAP"},
82 {BGP_ATTR_VNC
, "VNC"},
84 {BGP_ATTR_LARGE_COMMUNITIES
, "LARGE_COMMUNITY"},
85 {BGP_ATTR_PREFIX_SID
, "PREFIX_SID"},
88 static const struct message attr_flag_str
[] = {
89 {BGP_ATTR_FLAG_OPTIONAL
, "Optional"},
90 {BGP_ATTR_FLAG_TRANS
, "Transitive"},
91 {BGP_ATTR_FLAG_PARTIAL
, "Partial"},
92 /* bgp_attr_flags_diagnose() relies on this bit being last in
94 {BGP_ATTR_FLAG_EXTLEN
, "Extended Length"},
97 static struct hash
*cluster_hash
;
99 static void *cluster_hash_alloc(void *p
)
101 const struct cluster_list
*val
= (const struct cluster_list
*)p
;
102 struct cluster_list
*cluster
;
104 cluster
= XMALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
105 cluster
->length
= val
->length
;
107 if (cluster
->length
) {
108 cluster
->list
= XMALLOC(MTYPE_CLUSTER_VAL
, val
->length
);
109 memcpy(cluster
->list
, val
->list
, val
->length
);
111 cluster
->list
= NULL
;
118 /* Cluster list related functions. */
119 static struct cluster_list
*cluster_parse(struct in_addr
*pnt
, int length
)
121 struct cluster_list tmp
;
122 struct cluster_list
*cluster
;
127 cluster
= hash_get(cluster_hash
, &tmp
, cluster_hash_alloc
);
132 int cluster_loop_check(struct cluster_list
*cluster
, struct in_addr originator
)
136 for (i
= 0; i
< cluster
->length
/ 4; i
++)
137 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
142 static unsigned int cluster_hash_key_make(void *p
)
144 const struct cluster_list
*cluster
= p
;
146 return jhash(cluster
->list
, cluster
->length
, 0);
149 static int cluster_hash_cmp(const void *p1
, const void *p2
)
151 const struct cluster_list
*cluster1
= p1
;
152 const struct cluster_list
*cluster2
= p2
;
154 return (cluster1
->length
== cluster2
->length
155 && memcmp(cluster1
->list
, cluster2
->list
, cluster1
->length
)
159 static void cluster_free(struct cluster_list
*cluster
)
162 XFREE(MTYPE_CLUSTER_VAL
, cluster
->list
);
163 XFREE(MTYPE_CLUSTER
, cluster
);
166 static struct cluster_list
*cluster_intern(struct cluster_list
*cluster
)
168 struct cluster_list
*find
;
170 find
= hash_get(cluster_hash
, cluster
, cluster_hash_alloc
);
176 void cluster_unintern(struct cluster_list
*cluster
)
181 if (cluster
->refcnt
== 0) {
182 hash_release(cluster_hash
, cluster
);
183 cluster_free(cluster
);
187 static void cluster_init(void)
189 cluster_hash
= hash_create(cluster_hash_key_make
, cluster_hash_cmp
,
193 static void cluster_finish(void)
195 hash_clean(cluster_hash
, (void (*)(void *))cluster_free
);
196 hash_free(cluster_hash
);
200 static struct hash
*encap_hash
= NULL
;
202 static struct hash
*vnc_hash
= NULL
;
205 struct bgp_attr_encap_subtlv
*encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
207 struct bgp_attr_encap_subtlv
*new;
208 struct bgp_attr_encap_subtlv
*tail
;
209 struct bgp_attr_encap_subtlv
*p
;
211 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
212 int size
= sizeof(struct bgp_attr_encap_subtlv
) + p
->length
;
214 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
217 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
220 memcpy(tail
, p
, size
);
227 static void encap_free(struct bgp_attr_encap_subtlv
*p
)
229 struct bgp_attr_encap_subtlv
*next
;
233 XFREE(MTYPE_ENCAP_TLV
, p
);
238 void bgp_attr_flush_encap(struct attr
*attr
)
243 if (attr
->encap_subtlvs
) {
244 encap_free(attr
->encap_subtlvs
);
245 attr
->encap_subtlvs
= NULL
;
248 if (attr
->vnc_subtlvs
) {
249 encap_free(attr
->vnc_subtlvs
);
250 attr
->vnc_subtlvs
= NULL
;
256 * Compare encap sub-tlv chains
261 * This algorithm could be made faster if needed
263 static int encap_same(const struct bgp_attr_encap_subtlv
*h1
,
264 const struct bgp_attr_encap_subtlv
*h2
)
266 const struct bgp_attr_encap_subtlv
*p
;
267 const struct bgp_attr_encap_subtlv
*q
;
271 if (h1
== NULL
|| h2
== NULL
)
274 for (p
= h1
; p
; p
= p
->next
) {
275 for (q
= h2
; q
; q
= q
->next
) {
276 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
277 && !memcmp(p
->value
, q
->value
, p
->length
)) {
286 for (p
= h2
; p
; p
= p
->next
) {
287 for (q
= h1
; q
; q
= q
->next
) {
288 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
289 && !memcmp(p
->value
, q
->value
, p
->length
)) {
301 static void *encap_hash_alloc(void *p
)
303 /* Encap structure is already allocated. */
314 static struct bgp_attr_encap_subtlv
*
315 encap_intern(struct bgp_attr_encap_subtlv
*encap
, encap_subtlv_type type
)
317 struct bgp_attr_encap_subtlv
*find
;
318 struct hash
*hash
= encap_hash
;
320 if (type
== VNC_SUBTLV_TYPE
)
324 find
= hash_get(hash
, encap
, encap_hash_alloc
);
332 static void encap_unintern(struct bgp_attr_encap_subtlv
**encapp
,
333 encap_subtlv_type type
)
335 struct bgp_attr_encap_subtlv
*encap
= *encapp
;
339 if (encap
->refcnt
== 0) {
340 struct hash
*hash
= encap_hash
;
342 if (type
== VNC_SUBTLV_TYPE
)
345 hash_release(hash
, encap
);
351 static unsigned int encap_hash_key_make(void *p
)
353 const struct bgp_attr_encap_subtlv
*encap
= p
;
355 return jhash(encap
->value
, encap
->length
, 0);
358 static int encap_hash_cmp(const void *p1
, const void *p2
)
360 return encap_same((const struct bgp_attr_encap_subtlv
*)p1
,
361 (const struct bgp_attr_encap_subtlv
*)p2
);
364 static void encap_init(void)
366 encap_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
,
369 vnc_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
,
374 static void encap_finish(void)
376 hash_clean(encap_hash
, (void (*)(void *))encap_free
);
377 hash_free(encap_hash
);
380 hash_clean(vnc_hash
, (void (*)(void *))encap_free
);
386 static bool overlay_index_same(const struct attr
*a1
, const struct attr
*a2
)
394 return !memcmp(&(a1
->evpn_overlay
), &(a2
->evpn_overlay
),
395 sizeof(struct overlay_index
));
398 /* Unknown transit attribute. */
399 static struct hash
*transit_hash
;
401 static void transit_free(struct transit
*transit
)
404 XFREE(MTYPE_TRANSIT_VAL
, transit
->val
);
405 XFREE(MTYPE_TRANSIT
, transit
);
408 static void *transit_hash_alloc(void *p
)
410 /* Transit structure is already allocated. */
414 static struct transit
*transit_intern(struct transit
*transit
)
416 struct transit
*find
;
418 find
= hash_get(transit_hash
, transit
, transit_hash_alloc
);
420 transit_free(transit
);
426 void transit_unintern(struct transit
*transit
)
431 if (transit
->refcnt
== 0) {
432 hash_release(transit_hash
, transit
);
433 transit_free(transit
);
437 static unsigned int transit_hash_key_make(void *p
)
439 const struct transit
*transit
= p
;
441 return jhash(transit
->val
, transit
->length
, 0);
444 static int transit_hash_cmp(const void *p1
, const void *p2
)
446 const struct transit
*transit1
= p1
;
447 const struct transit
*transit2
= p2
;
449 return (transit1
->length
== transit2
->length
450 && memcmp(transit1
->val
, transit2
->val
, transit1
->length
) == 0);
453 static void transit_init(void)
455 transit_hash
= hash_create(transit_hash_key_make
, transit_hash_cmp
,
459 static void transit_finish(void)
461 hash_clean(transit_hash
, (void (*)(void *))transit_free
);
462 hash_free(transit_hash
);
466 /* Attribute hash routines. */
467 static struct hash
*attrhash
;
469 /* Shallow copy of an attribute
470 * Though, not so shallow that it doesn't copy the contents
471 * of the attr_extra pointed to by 'extra'
473 void bgp_attr_dup(struct attr
*new, struct attr
*orig
)
478 unsigned long int attr_count(void)
480 return attrhash
->count
;
483 unsigned long int attr_unknown_count(void)
485 return transit_hash
->count
;
488 unsigned int attrhash_key_make(void *p
)
490 const struct attr
*attr
= (struct attr
*)p
;
492 #define MIX(val) key = jhash_1word(val, key)
493 #define MIX3(a, b, c) key = jhash_3words((a), (b), (c), key)
495 MIX3(attr
->origin
, attr
->nexthop
.s_addr
, attr
->med
);
496 MIX3(attr
->local_pref
, attr
->aggregator_as
,
497 attr
->aggregator_addr
.s_addr
);
498 MIX3(attr
->weight
, attr
->mp_nexthop_global_in
.s_addr
,
499 attr
->originator_id
.s_addr
);
500 MIX3(attr
->tag
, attr
->label
, attr
->label_index
);
503 MIX(aspath_key_make(attr
->aspath
));
505 MIX(community_hash_make(attr
->community
));
507 if (attr
->lcommunity
)
508 MIX(lcommunity_hash_make(attr
->lcommunity
));
509 if (attr
->ecommunity
)
510 MIX(ecommunity_hash_make(attr
->ecommunity
));
512 MIX(cluster_hash_key_make(attr
->cluster
));
514 MIX(transit_hash_key_make(attr
->transit
));
515 if (attr
->encap_subtlvs
)
516 MIX(encap_hash_key_make(attr
->encap_subtlvs
));
518 if (attr
->vnc_subtlvs
)
519 MIX(encap_hash_key_make(attr
->vnc_subtlvs
));
521 MIX(attr
->mp_nexthop_len
);
522 key
= jhash(attr
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
523 key
= jhash(attr
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
524 MIX(attr
->nh_ifindex
);
525 MIX(attr
->nh_lla_ifindex
);
530 int attrhash_cmp(const void *p1
, const void *p2
)
532 const struct attr
*attr1
= p1
;
533 const struct attr
*attr2
= p2
;
535 if (attr1
->flag
== attr2
->flag
&& attr1
->origin
== attr2
->origin
536 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
537 && attr1
->aspath
== attr2
->aspath
538 && attr1
->community
== attr2
->community
&& attr1
->med
== attr2
->med
539 && attr1
->local_pref
== attr2
->local_pref
540 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
) {
541 if (attr1
->aggregator_as
== attr2
->aggregator_as
542 && attr1
->aggregator_addr
.s_addr
543 == attr2
->aggregator_addr
.s_addr
544 && attr1
->weight
== attr2
->weight
545 && attr1
->tag
== attr2
->tag
546 && attr1
->label_index
== attr2
->label_index
547 && attr1
->mp_nexthop_len
== attr2
->mp_nexthop_len
548 && attr1
->ecommunity
== attr2
->ecommunity
549 && attr1
->lcommunity
== attr2
->lcommunity
550 && attr1
->cluster
== attr2
->cluster
551 && attr1
->transit
== attr2
->transit
552 && (attr1
->encap_tunneltype
== attr2
->encap_tunneltype
)
553 && encap_same(attr1
->encap_subtlvs
, attr2
->encap_subtlvs
)
555 && encap_same(attr1
->vnc_subtlvs
, attr2
->vnc_subtlvs
)
557 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_global
,
558 &attr2
->mp_nexthop_global
)
559 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_local
,
560 &attr2
->mp_nexthop_local
)
561 && IPV4_ADDR_SAME(&attr1
->mp_nexthop_global_in
,
562 &attr2
->mp_nexthop_global_in
)
563 && IPV4_ADDR_SAME(&attr1
->originator_id
,
564 &attr2
->originator_id
)
565 && overlay_index_same(attr1
, attr2
)
566 && attr1
->nh_ifindex
== attr2
->nh_ifindex
567 && attr1
->nh_lla_ifindex
== attr2
->nh_lla_ifindex
)
574 static void attrhash_init(void)
577 hash_create(attrhash_key_make
, attrhash_cmp
, "BGP Attributes");
581 * special for hash_clean below
583 static void attr_vfree(void *attr
)
585 XFREE(MTYPE_ATTR
, attr
);
588 static void attrhash_finish(void)
590 hash_clean(attrhash
, attr_vfree
);
595 static void attr_show_all_iterator(struct hash_backet
*backet
, struct vty
*vty
)
597 struct attr
*attr
= backet
->data
;
599 vty_out(vty
, "attr[%ld] nexthop %s\n", attr
->refcnt
,
600 inet_ntoa(attr
->nexthop
));
601 vty_out(vty
, "\tflags: %" PRIu64
" med: %u local_pref: %u origin: %u weight: %u label: %u\n",
602 attr
->flag
, attr
->med
, attr
->local_pref
, attr
->origin
,
603 attr
->weight
, attr
->label
);
606 void attr_show_all(struct vty
*vty
)
608 hash_iterate(attrhash
, (void (*)(struct hash_backet
*,
609 void *))attr_show_all_iterator
,
613 static void *bgp_attr_hash_alloc(void *p
)
615 struct attr
*val
= (struct attr
*)p
;
618 attr
= XMALLOC(MTYPE_ATTR
, sizeof(struct attr
));
620 if (val
->encap_subtlvs
) {
621 val
->encap_subtlvs
= NULL
;
624 if (val
->vnc_subtlvs
) {
625 val
->vnc_subtlvs
= NULL
;
632 /* Internet argument attribute. */
633 struct attr
*bgp_attr_intern(struct attr
*attr
)
637 /* Intern referenced strucutre. */
639 if (!attr
->aspath
->refcnt
)
640 attr
->aspath
= aspath_intern(attr
->aspath
);
642 attr
->aspath
->refcnt
++;
644 if (attr
->community
) {
645 if (!attr
->community
->refcnt
)
646 attr
->community
= community_intern(attr
->community
);
648 attr
->community
->refcnt
++;
651 if (attr
->ecommunity
) {
652 if (!attr
->ecommunity
->refcnt
)
653 attr
->ecommunity
= ecommunity_intern(attr
->ecommunity
);
655 attr
->ecommunity
->refcnt
++;
657 if (attr
->lcommunity
) {
658 if (!attr
->lcommunity
->refcnt
)
659 attr
->lcommunity
= lcommunity_intern(attr
->lcommunity
);
661 attr
->lcommunity
->refcnt
++;
664 if (!attr
->cluster
->refcnt
)
665 attr
->cluster
= cluster_intern(attr
->cluster
);
667 attr
->cluster
->refcnt
++;
670 if (!attr
->transit
->refcnt
)
671 attr
->transit
= transit_intern(attr
->transit
);
673 attr
->transit
->refcnt
++;
675 if (attr
->encap_subtlvs
) {
676 if (!attr
->encap_subtlvs
->refcnt
)
677 attr
->encap_subtlvs
= encap_intern(attr
->encap_subtlvs
,
680 attr
->encap_subtlvs
->refcnt
++;
683 if (attr
->vnc_subtlvs
) {
684 if (!attr
->vnc_subtlvs
->refcnt
)
685 attr
->vnc_subtlvs
= encap_intern(attr
->vnc_subtlvs
,
688 attr
->vnc_subtlvs
->refcnt
++;
692 /* At this point, attr only contains intern'd pointers. that means
693 * if we find it in attrhash, it has all the same pointers and we
694 * correctly updated the refcounts on these.
695 * If we don't find it, we need to allocate a one because in all
696 * cases this returns a new reference to a hashed attr, but the input
698 find
= (struct attr
*)hash_get(attrhash
, attr
, bgp_attr_hash_alloc
);
704 /* Make network statement's attribute. */
705 struct attr
*bgp_attr_default_set(struct attr
*attr
, uint8_t origin
)
707 memset(attr
, 0, sizeof(struct attr
));
709 attr
->origin
= origin
;
710 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
711 attr
->aspath
= aspath_empty();
712 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
713 attr
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
715 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
716 attr
->label
= MPLS_INVALID_LABEL
;
717 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
718 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
723 /* Create the attributes for an aggregate */
724 struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, uint8_t origin
,
725 struct aspath
*aspath
,
726 struct community
*community
, int as_set
,
727 uint8_t atomic_aggregate
)
732 memset(&attr
, 0, sizeof(struct attr
));
734 /* Origin attribute. */
735 attr
.origin
= origin
;
736 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
738 /* AS path attribute. */
740 attr
.aspath
= aspath_intern(aspath
);
742 attr
.aspath
= aspath_empty();
743 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
745 /* Next hop attribute. */
746 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
749 uint32_t gshut
= COMMUNITY_GSHUT
;
751 /* If we are not shutting down ourselves and we are
752 * aggregating a route that contains the GSHUT community we
753 * need to remove that community when creating the aggregate */
754 if (!bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)
755 && community_include(community
, gshut
)) {
756 community_del_val(community
, &gshut
);
759 attr
.community
= community
;
760 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
763 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
764 bgp_attr_add_gshut_community(&attr
);
767 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
768 attr
.label
= MPLS_INVALID_LABEL
;
769 attr
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
770 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
771 if (!as_set
|| atomic_aggregate
)
772 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
773 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
774 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
775 attr
.aggregator_as
= bgp
->confed_id
;
777 attr
.aggregator_as
= bgp
->as
;
778 attr
.aggregator_addr
= bgp
->router_id
;
779 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
780 attr
.label
= MPLS_INVALID_LABEL
;
782 new = bgp_attr_intern(&attr
);
784 aspath_unintern(&new->aspath
);
788 /* Unintern just the sub-components of the attr, but not the attr */
789 void bgp_attr_unintern_sub(struct attr
*attr
)
791 /* aspath refcount shoud be decrement. */
793 aspath_unintern(&attr
->aspath
);
794 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
));
797 community_unintern(&attr
->community
);
798 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
));
800 if (attr
->ecommunity
)
801 ecommunity_unintern(&attr
->ecommunity
);
802 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
));
804 if (attr
->lcommunity
)
805 lcommunity_unintern(&attr
->lcommunity
);
806 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
));
809 cluster_unintern(attr
->cluster
);
810 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
));
813 transit_unintern(attr
->transit
);
815 if (attr
->encap_subtlvs
)
816 encap_unintern(&attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
819 if (attr
->vnc_subtlvs
)
820 encap_unintern(&attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
825 * We have some show commands that let you experimentally
826 * apply a route-map. When we apply the route-map
827 * we are reseting values but not saving them for
828 * posterity via intern'ing( because route-maps don't
829 * do that) but at this point in time we need
830 * to compare the new attr to the old and if the
831 * routemap has changed it we need to, as Snoop Dog says,
832 * Drop it like it's hot
834 void bgp_attr_undup(struct attr
*new, struct attr
*old
)
836 if (new->aspath
!= old
->aspath
)
837 aspath_free(new->aspath
);
839 if (new->community
!= old
->community
)
840 community_free(new->community
);
842 if (new->ecommunity
!= old
->ecommunity
)
843 ecommunity_free(&new->ecommunity
);
845 if (new->lcommunity
!= old
->lcommunity
)
846 lcommunity_free(&new->lcommunity
);
849 /* Free bgp attribute and aspath. */
850 void bgp_attr_unintern(struct attr
**pattr
)
852 struct attr
*attr
= *pattr
;
856 /* Decrement attribute reference. */
861 /* If reference becomes zero then free attribute object. */
862 if (attr
->refcnt
== 0) {
863 ret
= hash_release(attrhash
, attr
);
865 XFREE(MTYPE_ATTR
, attr
);
869 bgp_attr_unintern_sub(&tmp
);
872 void bgp_attr_flush(struct attr
*attr
)
874 if (attr
->aspath
&& !attr
->aspath
->refcnt
) {
875 aspath_free(attr
->aspath
);
878 if (attr
->community
&& !attr
->community
->refcnt
) {
879 community_free(attr
->community
);
880 attr
->community
= NULL
;
883 if (attr
->ecommunity
&& !attr
->ecommunity
->refcnt
)
884 ecommunity_free(&attr
->ecommunity
);
885 if (attr
->lcommunity
&& !attr
->lcommunity
->refcnt
)
886 lcommunity_free(&attr
->lcommunity
);
887 if (attr
->cluster
&& !attr
->cluster
->refcnt
) {
888 cluster_free(attr
->cluster
);
889 attr
->cluster
= NULL
;
891 if (attr
->transit
&& !attr
->transit
->refcnt
) {
892 transit_free(attr
->transit
);
893 attr
->transit
= NULL
;
895 if (attr
->encap_subtlvs
&& !attr
->encap_subtlvs
->refcnt
) {
896 encap_free(attr
->encap_subtlvs
);
897 attr
->encap_subtlvs
= NULL
;
900 if (attr
->vnc_subtlvs
&& !attr
->vnc_subtlvs
->refcnt
) {
901 encap_free(attr
->vnc_subtlvs
);
902 attr
->vnc_subtlvs
= NULL
;
907 /* Implement draft-scudder-idr-optional-transitive behaviour and
908 * avoid resetting sessions for malformed attributes which are
909 * are partial/optional and hence where the error likely was not
910 * introduced by the sending neighbour.
912 static bgp_attr_parse_ret_t
913 bgp_attr_malformed(struct bgp_attr_parser_args
*args
, uint8_t subcode
,
916 struct peer
*const peer
= args
->peer
;
917 const uint8_t flags
= args
->flags
;
918 /* startp and length must be special-cased, as whether or not to
919 * send the attribute data with the NOTIFY depends on the error,
920 * the caller therefore signals this with the seperate length argument
922 uint8_t *notify_datap
= (length
> 0 ? args
->startp
: NULL
);
924 /* Only relax error handling for eBGP peers */
925 if (peer
->sort
!= BGP_PEER_EBGP
) {
926 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
927 notify_datap
, length
);
928 return BGP_ATTR_PARSE_ERROR
;
931 /* Adjust the stream getp to the end of the attribute, in case we can
932 * still proceed but the caller hasn't read all the attribute.
934 stream_set_getp(BGP_INPUT(peer
),
935 (args
->startp
- STREAM_DATA(BGP_INPUT(peer
)))
938 switch (args
->type
) {
939 /* where an attribute is relatively inconsequential, e.g. it does not
940 * affect route selection, and can be safely ignored, then any such
941 * attributes which are malformed should just be ignored and the route
942 * processed as normal.
944 case BGP_ATTR_AS4_AGGREGATOR
:
945 case BGP_ATTR_AGGREGATOR
:
946 case BGP_ATTR_ATOMIC_AGGREGATE
:
947 return BGP_ATTR_PARSE_PROCEED
;
949 /* Core attributes, particularly ones which may influence route
950 * selection, should always cause session resets
952 case BGP_ATTR_ORIGIN
:
953 case BGP_ATTR_AS_PATH
:
954 case BGP_ATTR_NEXT_HOP
:
955 case BGP_ATTR_MULTI_EXIT_DISC
:
956 case BGP_ATTR_LOCAL_PREF
:
957 case BGP_ATTR_COMMUNITIES
:
958 case BGP_ATTR_ORIGINATOR_ID
:
959 case BGP_ATTR_CLUSTER_LIST
:
960 case BGP_ATTR_MP_REACH_NLRI
:
961 case BGP_ATTR_MP_UNREACH_NLRI
:
962 case BGP_ATTR_EXT_COMMUNITIES
:
963 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
964 notify_datap
, length
);
965 return BGP_ATTR_PARSE_ERROR
;
968 /* Partial optional attributes that are malformed should not cause
969 * the whole session to be reset. Instead treat it as a withdrawal
970 * of the routes, if possible.
972 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)
973 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
974 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
))
975 return BGP_ATTR_PARSE_WITHDRAW
;
977 /* default to reset */
978 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
981 /* Find out what is wrong with the path attribute flag bits and log the error.
982 "Flag bits" here stand for Optional, Transitive and Partial, but not for
983 Extended Length. Checking O/T/P bits at once implies, that the attribute
984 being diagnosed is defined by RFC as either a "well-known" or an "optional,
985 non-transitive" attribute. */
987 bgp_attr_flags_diagnose(struct bgp_attr_parser_args
*args
,
988 uint8_t desired_flags
/* how RFC says it must be */
992 uint8_t real_flags
= args
->flags
;
993 const uint8_t attr_code
= args
->type
;
995 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
996 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
997 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
998 if (CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
999 != CHECK_FLAG(real_flags
, attr_flag_str
[i
].key
)) {
1000 flog_err(EC_BGP_ATTR_FLAG
,
1001 "%s attribute must%s be flagged as \"%s\"",
1002 lookup_msg(attr_str
, attr_code
, NULL
),
1003 CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1006 attr_flag_str
[i
].str
);
1011 "Strange, %s called for attr %s, but no problem found with flags"
1012 " (real flags 0x%x, desired 0x%x)",
1013 __func__
, lookup_msg(attr_str
, attr_code
, NULL
),
1014 real_flags
, desired_flags
);
1018 /* Required flags for attributes. EXTLEN will be masked off when testing,
1019 * as will PARTIAL for optional+transitive attributes.
1021 const uint8_t attr_flags_values
[] = {
1022 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1023 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1024 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1025 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1026 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1027 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1028 [BGP_ATTR_AGGREGATOR
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1029 [BGP_ATTR_COMMUNITIES
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1030 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1031 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1032 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1033 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1034 [BGP_ATTR_EXT_COMMUNITIES
] =
1035 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1036 [BGP_ATTR_AS4_PATH
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1037 [BGP_ATTR_AS4_AGGREGATOR
] =
1038 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1039 [BGP_ATTR_PMSI_TUNNEL
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1040 [BGP_ATTR_LARGE_COMMUNITIES
] =
1041 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1042 [BGP_ATTR_PREFIX_SID
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1044 static const size_t attr_flags_values_max
= array_size(attr_flags_values
) - 1;
1046 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args
*args
)
1048 uint8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1049 const uint8_t flags
= args
->flags
;
1050 const uint8_t attr_code
= args
->type
;
1052 /* there may be attributes we don't know about */
1053 if (attr_code
> attr_flags_values_max
)
1055 if (attr_flags_values
[attr_code
] == 0)
1058 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1062 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL
, flags
)
1063 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS
, flags
)) {
1066 "%s well-known attributes must have transitive flag set (%x)",
1067 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1071 /* "For well-known attributes and for optional non-transitive
1073 * the Partial bit MUST be set to 0."
1075 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
)) {
1076 if (!CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)) {
1077 flog_err(EC_BGP_ATTR_FLAG
,
1078 "%s well-known attribute "
1079 "must NOT have the partial flag set (%x)",
1080 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1083 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1084 && !CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)) {
1085 flog_err(EC_BGP_ATTR_FLAG
,
1086 "%s optional + transitive attribute "
1087 "must NOT have the partial flag set (%x)",
1088 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1093 /* Optional transitive attributes may go through speakers that don't
1094 * reocgnise them and set the Partial bit.
1096 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1097 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
))
1098 SET_FLAG(mask
, BGP_ATTR_FLAG_PARTIAL
);
1100 if ((flags
& ~mask
) == attr_flags_values
[attr_code
])
1103 bgp_attr_flags_diagnose(args
, attr_flags_values
[attr_code
]);
1107 /* Get origin attribute of the update message. */
1108 static bgp_attr_parse_ret_t
bgp_attr_origin(struct bgp_attr_parser_args
*args
)
1110 struct peer
*const peer
= args
->peer
;
1111 struct attr
*const attr
= args
->attr
;
1112 const bgp_size_t length
= args
->length
;
1114 /* If any recognized attribute has Attribute Length that conflicts
1115 with the expected length (based on the attribute type code), then
1116 the Error Subcode is set to Attribute Length Error. The Data
1117 field contains the erroneous attribute (type, length and
1120 flog_err(EC_BGP_ATTR_LEN
,
1121 "Origin attribute length is not one %d", length
);
1122 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1126 /* Fetch origin attribute. */
1127 attr
->origin
= stream_getc(BGP_INPUT(peer
));
1129 /* If the ORIGIN attribute has an undefined value, then the Error
1130 Subcode is set to Invalid Origin Attribute. The Data field
1131 contains the unrecognized attribute (type, length and value). */
1132 if ((attr
->origin
!= BGP_ORIGIN_IGP
) && (attr
->origin
!= BGP_ORIGIN_EGP
)
1133 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
)) {
1134 flog_err(EC_BGP_ATTR_ORIGIN
,
1135 "Origin attribute value is invalid %d", attr
->origin
);
1136 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1140 /* Set oring attribute flag. */
1141 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
1146 /* Parse AS path information. This function is wrapper of
1148 static int bgp_attr_aspath(struct bgp_attr_parser_args
*args
)
1150 struct attr
*const attr
= args
->attr
;
1151 struct peer
*const peer
= args
->peer
;
1152 const bgp_size_t length
= args
->length
;
1155 * peer with AS4 => will get 4Byte ASnums
1156 * otherwise, will get 16 Bit
1158 attr
->aspath
= aspath_parse(peer
->curr
, length
,
1159 CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
));
1161 /* In case of IBGP, length will be zero. */
1162 if (!attr
->aspath
) {
1163 flog_err(EC_BGP_ATTR_MAL_AS_PATH
,
1164 "Malformed AS path from %s, length is %d", peer
->host
,
1166 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1170 /* Set aspath attribute flag. */
1171 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
1173 return BGP_ATTR_PARSE_PROCEED
;
1176 static bgp_attr_parse_ret_t
bgp_attr_aspath_check(struct peer
*const peer
,
1177 struct attr
*const attr
)
1179 /* These checks were part of bgp_attr_aspath, but with
1180 * as4 we should to check aspath things when
1181 * aspath synthesizing with as4_path has already taken place.
1182 * Otherwise we check ASPATH and use the synthesized thing, and that is
1184 * So do the checks later, i.e. here
1186 struct aspath
*aspath
;
1188 /* Confederation sanity check. */
1189 if ((peer
->sort
== BGP_PEER_CONFED
1190 && !aspath_left_confed_check(attr
->aspath
))
1191 || (peer
->sort
== BGP_PEER_EBGP
1192 && aspath_confed_check(attr
->aspath
))) {
1193 flog_err(EC_BGP_ATTR_MAL_AS_PATH
, "Malformed AS path from %s",
1195 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1196 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1197 return BGP_ATTR_PARSE_ERROR
;
1200 /* First AS check for EBGP. */
1201 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_ENFORCE_FIRST_AS
)) {
1202 if (peer
->sort
== BGP_PEER_EBGP
1203 && !aspath_firstas_check(attr
->aspath
, peer
->as
)) {
1204 flog_err(EC_BGP_ATTR_FIRST_AS
,
1205 "%s incorrect first AS (must be %u)",
1206 peer
->host
, peer
->as
);
1207 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1208 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1209 return BGP_ATTR_PARSE_ERROR
;
1213 /* local-as prepend */
1214 if (peer
->change_local_as
1215 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)) {
1216 aspath
= aspath_dup(attr
->aspath
);
1217 aspath
= aspath_add_seq(aspath
, peer
->change_local_as
);
1218 aspath_unintern(&attr
->aspath
);
1219 attr
->aspath
= aspath_intern(aspath
);
1222 return BGP_ATTR_PARSE_PROCEED
;
1225 /* Parse AS4 path information. This function is another wrapper of
1227 static int bgp_attr_as4_path(struct bgp_attr_parser_args
*args
,
1228 struct aspath
**as4_path
)
1230 struct peer
*const peer
= args
->peer
;
1231 struct attr
*const attr
= args
->attr
;
1232 const bgp_size_t length
= args
->length
;
1234 *as4_path
= aspath_parse(peer
->curr
, length
, 1);
1236 /* In case of IBGP, length will be zero. */
1238 flog_err(EC_BGP_ATTR_MAL_AS_PATH
,
1239 "Malformed AS4 path from %s, length is %d", peer
->host
,
1241 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1245 /* Set aspath attribute flag. */
1246 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
);
1248 return BGP_ATTR_PARSE_PROCEED
;
1251 /* Nexthop attribute. */
1252 static bgp_attr_parse_ret_t
bgp_attr_nexthop(struct bgp_attr_parser_args
*args
)
1254 struct peer
*const peer
= args
->peer
;
1255 struct attr
*const attr
= args
->attr
;
1256 const bgp_size_t length
= args
->length
;
1258 in_addr_t nexthop_h
, nexthop_n
;
1260 /* Check nexthop attribute length. */
1262 flog_err(EC_BGP_ATTR_LEN
,
1263 "Nexthop attribute length isn't four [%d]", length
);
1265 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1269 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1270 attribute must result in a NOTIFICATION message (this is implemented
1272 At the same time, semantically incorrect NEXT_HOP is more likely to
1274 logged locally (this is implemented somewhere else). The UPDATE
1276 gets ignored in any of these cases. */
1277 nexthop_n
= stream_get_ipv4(peer
->curr
);
1278 nexthop_h
= ntohl(nexthop_n
);
1279 if ((IPV4_NET0(nexthop_h
) || IPV4_NET127(nexthop_h
)
1280 || IPV4_CLASS_DE(nexthop_h
))
1283 ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1285 char buf
[INET_ADDRSTRLEN
];
1286 inet_ntop(AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1287 flog_err(EC_BGP_ATTR_MARTIAN_NH
, "Martian nexthop %s", buf
);
1288 return bgp_attr_malformed(
1289 args
, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
, args
->total
);
1292 attr
->nexthop
.s_addr
= nexthop_n
;
1293 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1295 return BGP_ATTR_PARSE_PROCEED
;
1298 /* MED atrribute. */
1299 static bgp_attr_parse_ret_t
bgp_attr_med(struct bgp_attr_parser_args
*args
)
1301 struct peer
*const peer
= args
->peer
;
1302 struct attr
*const attr
= args
->attr
;
1303 const bgp_size_t length
= args
->length
;
1307 flog_err(EC_BGP_ATTR_LEN
,
1308 "MED attribute length isn't four [%d]", length
);
1310 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1314 attr
->med
= stream_getl(peer
->curr
);
1316 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
1318 return BGP_ATTR_PARSE_PROCEED
;
1321 /* Local preference attribute. */
1322 static bgp_attr_parse_ret_t
1323 bgp_attr_local_pref(struct bgp_attr_parser_args
*args
)
1325 struct peer
*const peer
= args
->peer
;
1326 struct attr
*const attr
= args
->attr
;
1327 const bgp_size_t length
= args
->length
;
1331 flog_err(EC_BGP_ATTR_LEN
,
1332 "LOCAL_PREF attribute length isn't 4 [%u]", length
);
1333 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1337 /* If it is contained in an UPDATE message that is received from an
1338 external peer, then this attribute MUST be ignored by the
1339 receiving speaker. */
1340 if (peer
->sort
== BGP_PEER_EBGP
) {
1341 stream_forward_getp(peer
->curr
, length
);
1342 return BGP_ATTR_PARSE_PROCEED
;
1345 attr
->local_pref
= stream_getl(peer
->curr
);
1347 /* Set the local-pref flag. */
1348 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1350 return BGP_ATTR_PARSE_PROCEED
;
1353 /* Atomic aggregate. */
1354 static int bgp_attr_atomic(struct bgp_attr_parser_args
*args
)
1356 struct attr
*const attr
= args
->attr
;
1357 const bgp_size_t length
= args
->length
;
1361 flog_err(EC_BGP_ATTR_LEN
,
1362 "ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1364 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1368 /* Set atomic aggregate flag. */
1369 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
1371 return BGP_ATTR_PARSE_PROCEED
;
1374 /* Aggregator attribute */
1375 static int bgp_attr_aggregator(struct bgp_attr_parser_args
*args
)
1377 struct peer
*const peer
= args
->peer
;
1378 struct attr
*const attr
= args
->attr
;
1379 const bgp_size_t length
= args
->length
;
1383 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1384 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1387 if (length
!= wantedlen
) {
1388 flog_err(EC_BGP_ATTR_LEN
,
1389 "AGGREGATOR attribute length isn't %u [%u]", wantedlen
,
1391 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1395 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1396 attr
->aggregator_as
= stream_getl(peer
->curr
);
1398 attr
->aggregator_as
= stream_getw(peer
->curr
);
1399 attr
->aggregator_addr
.s_addr
= stream_get_ipv4(peer
->curr
);
1401 /* Set atomic aggregate flag. */
1402 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
1404 return BGP_ATTR_PARSE_PROCEED
;
1407 /* New Aggregator attribute */
1408 static bgp_attr_parse_ret_t
1409 bgp_attr_as4_aggregator(struct bgp_attr_parser_args
*args
,
1410 as_t
*as4_aggregator_as
,
1411 struct in_addr
*as4_aggregator_addr
)
1413 struct peer
*const peer
= args
->peer
;
1414 struct attr
*const attr
= args
->attr
;
1415 const bgp_size_t length
= args
->length
;
1418 flog_err(EC_BGP_ATTR_LEN
, "New Aggregator length is not 8 [%d]",
1420 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1424 *as4_aggregator_as
= stream_getl(peer
->curr
);
1425 as4_aggregator_addr
->s_addr
= stream_get_ipv4(peer
->curr
);
1427 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
);
1429 return BGP_ATTR_PARSE_PROCEED
;
1432 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1434 static bgp_attr_parse_ret_t
1435 bgp_attr_munge_as4_attrs(struct peer
*const peer
, struct attr
*const attr
,
1436 struct aspath
*as4_path
, as_t as4_aggregator
,
1437 struct in_addr
*as4_aggregator_addr
)
1439 int ignore_as4_path
= 0;
1440 struct aspath
*newpath
;
1442 if (!attr
->aspath
) {
1443 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1445 * checked that all well-known, mandatory attributes were
1448 * Can only be a problem with peer itself - hard error
1450 return BGP_ATTR_PARSE_ERROR
;
1453 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) {
1454 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1456 * It is worth a warning though, because the peer really
1457 * should not send them
1459 if (BGP_DEBUG(as4
, AS4
)) {
1460 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1461 zlog_debug("[AS4] %s %s AS4_PATH", peer
->host
,
1462 "AS4 capable peer, yet it sent");
1465 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1466 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1468 "AS4 capable peer, yet it sent");
1471 return BGP_ATTR_PARSE_PROCEED
;
1474 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1475 * because that may override AS4_PATH
1477 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
))) {
1478 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
1480 * if the as_number in aggregator is not AS_TRANS,
1481 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1482 * and the Aggregator shall be taken as
1483 * info on the aggregating node, and the AS_PATH
1484 * shall be taken as the AS_PATH
1486 * the Aggregator shall be ignored and the
1487 * AS4_AGGREGATOR shall be taken as the
1488 * Aggregating node and the AS_PATH is to be
1489 * constructed "as in all other cases"
1491 if (attr
->aggregator_as
!= BGP_AS_TRANS
) {
1493 if (BGP_DEBUG(as4
, AS4
))
1495 "[AS4] %s BGP not AS4 capable peer"
1496 " send AGGREGATOR != AS_TRANS and"
1497 " AS4_AGGREGATOR, so ignore"
1498 " AS4_AGGREGATOR and AS4_PATH",
1500 ignore_as4_path
= 1;
1502 /* "New_aggregator shall be taken as aggregator"
1504 attr
->aggregator_as
= as4_aggregator
;
1505 attr
->aggregator_addr
.s_addr
=
1506 as4_aggregator_addr
->s_addr
;
1509 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1510 * That is bogus - but reading the conditions
1511 * we have to handle AS4_AGGREGATOR as if it were
1512 * AGGREGATOR in that case
1514 if (BGP_DEBUG(as4
, AS4
))
1516 "[AS4] %s BGP not AS4 capable peer send"
1517 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1518 " it as if AGGREGATOR with AS_TRANS had been there",
1520 attr
->aggregator_as
= as4_aggregator
;
1521 /* sweep it under the carpet and simulate a "good"
1523 attr
->flag
|= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
));
1527 /* need to reconcile NEW_AS_PATH and AS_PATH */
1528 if (!ignore_as4_path
1529 && (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))) {
1530 newpath
= aspath_reconcile_as4(attr
->aspath
, as4_path
);
1532 return BGP_ATTR_PARSE_ERROR
;
1534 aspath_unintern(&attr
->aspath
);
1535 attr
->aspath
= aspath_intern(newpath
);
1537 return BGP_ATTR_PARSE_PROCEED
;
1540 /* Community attribute. */
1541 static bgp_attr_parse_ret_t
1542 bgp_attr_community(struct bgp_attr_parser_args
*args
)
1544 struct peer
*const peer
= args
->peer
;
1545 struct attr
*const attr
= args
->attr
;
1546 const bgp_size_t length
= args
->length
;
1549 attr
->community
= NULL
;
1550 return BGP_ATTR_PARSE_PROCEED
;
1554 community_parse((uint32_t *)stream_pnt(peer
->curr
), length
);
1556 /* XXX: fix community_parse to use stream API and remove this */
1557 stream_forward_getp(peer
->curr
, length
);
1559 if (!attr
->community
)
1560 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1563 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1565 return BGP_ATTR_PARSE_PROCEED
;
1568 /* Originator ID attribute. */
1569 static bgp_attr_parse_ret_t
1570 bgp_attr_originator_id(struct bgp_attr_parser_args
*args
)
1572 struct peer
*const peer
= args
->peer
;
1573 struct attr
*const attr
= args
->attr
;
1574 const bgp_size_t length
= args
->length
;
1578 flog_err(EC_BGP_ATTR_LEN
, "Bad originator ID length %d",
1581 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1585 attr
->originator_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1587 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
);
1589 return BGP_ATTR_PARSE_PROCEED
;
1592 /* Cluster list attribute. */
1593 static bgp_attr_parse_ret_t
1594 bgp_attr_cluster_list(struct bgp_attr_parser_args
*args
)
1596 struct peer
*const peer
= args
->peer
;
1597 struct attr
*const attr
= args
->attr
;
1598 const bgp_size_t length
= args
->length
;
1602 flog_err(EC_BGP_ATTR_LEN
, "Bad cluster list length %d", length
);
1604 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1609 cluster_parse((struct in_addr
*)stream_pnt(peer
->curr
), length
);
1611 /* XXX: Fix cluster_parse to use stream API and then remove this */
1612 stream_forward_getp(peer
->curr
, length
);
1614 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
);
1616 return BGP_ATTR_PARSE_PROCEED
;
1619 /* Multiprotocol reachability information parse. */
1620 int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
1621 struct bgp_nlri
*mp_update
)
1625 iana_safi_t pkt_safi
;
1627 bgp_size_t nlri_len
;
1630 struct peer
*const peer
= args
->peer
;
1631 struct attr
*const attr
= args
->attr
;
1632 const bgp_size_t length
= args
->length
;
1634 /* Set end of packet. */
1635 s
= BGP_INPUT(peer
);
1636 start
= stream_get_getp(s
);
1638 /* safe to read statically sized header? */
1639 #define BGP_MP_REACH_MIN_SIZE 5
1640 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1641 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
)) {
1642 zlog_info("%s: %s sent invalid length, %lu", __func__
,
1643 peer
->host
, (unsigned long)length
);
1644 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1647 /* Load AFI, SAFI. */
1648 pkt_afi
= stream_getw(s
);
1649 pkt_safi
= stream_getc(s
);
1651 /* Convert AFI, SAFI to internal values, check. */
1652 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1653 /* Log if AFI or SAFI is unrecognized. This is not an error
1655 * the attribute is otherwise malformed.
1657 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1659 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1660 peer
->host
, pkt_afi
, pkt_safi
);
1661 return BGP_ATTR_PARSE_ERROR
;
1664 /* Get nexthop length. */
1665 attr
->mp_nexthop_len
= stream_getc(s
);
1667 if (LEN_LEFT
< attr
->mp_nexthop_len
) {
1669 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1670 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1671 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1674 /* Nexthop length check. */
1675 switch (attr
->mp_nexthop_len
) {
1677 if (safi
!= SAFI_FLOWSPEC
) {
1678 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1679 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1680 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1683 case BGP_ATTR_NHLEN_VPNV4
:
1684 stream_getl(s
); /* RD high */
1685 stream_getl(s
); /* RD low */
1687 * NOTE: intentional fall through
1688 * - for consistency in rx processing
1690 * The following comment is to signal GCC this intention
1691 * and supress the warning
1694 case BGP_ATTR_NHLEN_IPV4
:
1695 stream_get(&attr
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1696 /* Probably needed for RFC 2283 */
1697 if (attr
->nexthop
.s_addr
== 0)
1698 memcpy(&attr
->nexthop
.s_addr
,
1699 &attr
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1701 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1702 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1703 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1704 stream_getl(s
); /* RD high */
1705 stream_getl(s
); /* RD low */
1707 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1708 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
)) {
1709 if (!peer
->nexthop
.ifp
) {
1710 zlog_warn("%s: interface not set appropriately to handle some attributes",
1712 return BGP_ATTR_PARSE_WITHDRAW
;
1714 attr
->nh_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1717 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1718 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1719 if (attr
->mp_nexthop_len
1720 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1721 stream_getl(s
); /* RD high */
1722 stream_getl(s
); /* RD low */
1724 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1725 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
)) {
1726 if (!peer
->nexthop
.ifp
) {
1727 zlog_warn("%s: interface not set appropriately to handle some attributes",
1729 return BGP_ATTR_PARSE_WITHDRAW
;
1731 attr
->nh_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1733 if (attr
->mp_nexthop_len
1734 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1735 stream_getl(s
); /* RD high */
1736 stream_getl(s
); /* RD low */
1738 stream_get(&attr
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1739 if (!IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
)) {
1740 char buf1
[INET6_ADDRSTRLEN
];
1741 char buf2
[INET6_ADDRSTRLEN
];
1743 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1745 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1748 &attr
->mp_nexthop_global
,
1749 buf1
, INET6_ADDRSTRLEN
),
1751 &attr
->mp_nexthop_local
, buf2
,
1754 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1756 if (!peer
->nexthop
.ifp
) {
1757 zlog_warn("%s: Interface not set appropriately to handle this some attributes",
1759 return BGP_ATTR_PARSE_WITHDRAW
;
1761 attr
->nh_lla_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1764 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1765 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1766 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1770 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__
,
1772 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1777 if ((val
= stream_getc(s
)))
1779 EC_BGP_DEFUNCT_SNPA_LEN
,
1780 "%s sent non-zero value, %u, for defunct SNPA-length field",
1784 /* must have nrli_len, what is left of the attribute */
1785 nlri_len
= LEN_LEFT
;
1786 if (nlri_len
> STREAM_READABLE(s
)) {
1787 zlog_info("%s: (%s) Failed to read NLRI", __func__
, peer
->host
);
1788 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1792 zlog_info("%s: (%s) No Reachability, Treating as a EOR marker",
1793 __func__
, peer
->host
);
1795 mp_update
->afi
= afi
;
1796 mp_update
->safi
= safi
;
1797 return BGP_ATTR_PARSE_EOR
;
1800 mp_update
->afi
= afi
;
1801 mp_update
->safi
= safi
;
1802 mp_update
->nlri
= stream_pnt(s
);
1803 mp_update
->length
= nlri_len
;
1805 stream_forward_getp(s
, nlri_len
);
1807 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
);
1809 return BGP_ATTR_PARSE_PROCEED
;
1813 /* Multiprotocol unreachable parse */
1814 int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
1815 struct bgp_nlri
*mp_withdraw
)
1820 iana_safi_t pkt_safi
;
1822 uint16_t withdraw_len
;
1823 struct peer
*const peer
= args
->peer
;
1824 struct attr
*const attr
= args
->attr
;
1825 const bgp_size_t length
= args
->length
;
1829 #define BGP_MP_UNREACH_MIN_SIZE 3
1830 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1831 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1833 pkt_afi
= stream_getw(s
);
1834 pkt_safi
= stream_getc(s
);
1836 /* Convert AFI, SAFI to internal values, check. */
1837 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1838 /* Log if AFI or SAFI is unrecognized. This is not an error
1840 * the attribute is otherwise malformed.
1842 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1844 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1845 peer
->host
, pkt_afi
, pkt_safi
);
1846 return BGP_ATTR_PARSE_ERROR
;
1849 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1851 mp_withdraw
->afi
= afi
;
1852 mp_withdraw
->safi
= safi
;
1853 mp_withdraw
->nlri
= stream_pnt(s
);
1854 mp_withdraw
->length
= withdraw_len
;
1856 stream_forward_getp(s
, withdraw_len
);
1858 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
);
1860 return BGP_ATTR_PARSE_PROCEED
;
1863 /* Large Community attribute. */
1864 static bgp_attr_parse_ret_t
1865 bgp_attr_large_community(struct bgp_attr_parser_args
*args
)
1867 struct peer
*const peer
= args
->peer
;
1868 struct attr
*const attr
= args
->attr
;
1869 const bgp_size_t length
= args
->length
;
1872 * Large community follows new attribute format.
1875 attr
->lcommunity
= NULL
;
1876 /* Empty extcomm doesn't seem to be invalid per se */
1877 return BGP_ATTR_PARSE_PROCEED
;
1881 lcommunity_parse((uint8_t *)stream_pnt(peer
->curr
), length
);
1882 /* XXX: fix ecommunity_parse to use stream API */
1883 stream_forward_getp(peer
->curr
, length
);
1885 if (!attr
->lcommunity
)
1886 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1889 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
1891 return BGP_ATTR_PARSE_PROCEED
;
1894 /* Extended Community attribute. */
1895 static bgp_attr_parse_ret_t
1896 bgp_attr_ext_communities(struct bgp_attr_parser_args
*args
)
1898 struct peer
*const peer
= args
->peer
;
1899 struct attr
*const attr
= args
->attr
;
1900 const bgp_size_t length
= args
->length
;
1904 attr
->ecommunity
= NULL
;
1905 /* Empty extcomm doesn't seem to be invalid per se */
1906 return BGP_ATTR_PARSE_PROCEED
;
1910 ecommunity_parse((uint8_t *)stream_pnt(peer
->curr
), length
);
1911 /* XXX: fix ecommunity_parse to use stream API */
1912 stream_forward_getp(peer
->curr
, length
);
1914 if (!attr
->ecommunity
)
1915 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1918 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
1920 /* Extract MAC mobility sequence number, if any. */
1921 attr
->mm_seqnum
= bgp_attr_mac_mobility_seqnum(attr
, &sticky
);
1922 attr
->sticky
= sticky
;
1924 /* Check if this is a Gateway MAC-IP advertisement */
1925 attr
->default_gw
= bgp_attr_default_gw(attr
);
1927 /* Handle scenario where router flag ecommunity is not
1928 * set but default gw ext community is present.
1929 * Use default gateway, set and propogate R-bit.
1931 if (attr
->default_gw
)
1932 attr
->router_flag
= 1;
1934 /* Check EVPN Neighbor advertisement flags, R-bit */
1935 bgp_attr_evpn_na_flag(attr
, &attr
->router_flag
);
1937 /* Extract the Rmac, if any */
1938 bgp_attr_rmac(attr
, &attr
->rmac
);
1940 return BGP_ATTR_PARSE_PROCEED
;
1943 /* Parse Tunnel Encap attribute in an UPDATE */
1944 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
1945 bgp_size_t length
, /* IN: attr's length field */
1946 struct attr
*attr
, /* IN: caller already allocated */
1947 uint8_t flag
, /* IN: attr's flags field */
1951 uint16_t tunneltype
= 0;
1953 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1955 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1956 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
1958 "Tunnel Encap attribute flag isn't optional and transitive %d",
1960 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
1961 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1966 if (BGP_ATTR_ENCAP
== type
) {
1967 /* read outer TLV type and length */
1968 uint16_t tlv_length
;
1972 "Tunnel Encap attribute not long enough to contain outer T,L");
1973 bgp_notify_send_with_data(
1974 peer
, BGP_NOTIFY_UPDATE_ERR
,
1975 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1978 tunneltype
= stream_getw(BGP_INPUT(peer
));
1979 tlv_length
= stream_getw(BGP_INPUT(peer
));
1982 if (tlv_length
!= length
) {
1983 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
1984 tlv_length
, length
);
1988 while (length
>= 4) {
1989 uint16_t subtype
= 0;
1990 uint16_t sublength
= 0;
1991 struct bgp_attr_encap_subtlv
*tlv
;
1993 if (BGP_ATTR_ENCAP
== type
) {
1994 subtype
= stream_getc(BGP_INPUT(peer
));
1995 sublength
= stream_getc(BGP_INPUT(peer
));
1999 subtype
= stream_getw(BGP_INPUT(peer
));
2000 sublength
= stream_getw(BGP_INPUT(peer
));
2005 if (sublength
> length
) {
2007 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
2009 bgp_notify_send_with_data(
2010 peer
, BGP_NOTIFY_UPDATE_ERR
,
2011 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
2015 /* alloc and copy sub-tlv */
2016 /* TBD make sure these are freed when attributes are released */
2017 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
2018 sizeof(struct bgp_attr_encap_subtlv
) + sublength
);
2019 tlv
->type
= subtype
;
2020 tlv
->length
= sublength
;
2021 stream_get(tlv
->value
, peer
->curr
, sublength
);
2022 length
-= sublength
;
2024 /* attach tlv to encap chain */
2025 if (BGP_ATTR_ENCAP
== type
) {
2026 struct bgp_attr_encap_subtlv
*stlv_last
;
2027 for (stlv_last
= attr
->encap_subtlvs
;
2028 stlv_last
&& stlv_last
->next
;
2029 stlv_last
= stlv_last
->next
)
2032 stlv_last
->next
= tlv
;
2034 attr
->encap_subtlvs
= tlv
;
2038 struct bgp_attr_encap_subtlv
*stlv_last
;
2039 for (stlv_last
= attr
->vnc_subtlvs
;
2040 stlv_last
&& stlv_last
->next
;
2041 stlv_last
= stlv_last
->next
)
2044 stlv_last
->next
= tlv
;
2046 attr
->vnc_subtlvs
= tlv
;
2052 if (BGP_ATTR_ENCAP
== type
) {
2053 attr
->encap_tunneltype
= tunneltype
;
2057 /* spurious leftover data */
2059 "Tunnel Encap attribute length is bad: %d leftover octets",
2061 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2062 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2071 * Read an individual SID value returning how much data we have read
2072 * Returns 0 if there was an error that needs to be passed up the stack
2074 static bgp_attr_parse_ret_t
bgp_attr_psid_sub(int32_t type
,
2076 struct bgp_attr_parser_args
*args
,
2077 struct bgp_nlri
*mp_update
)
2079 struct peer
*const peer
= args
->peer
;
2080 struct attr
*const attr
= args
->attr
;
2081 uint32_t label_index
;
2082 struct in6_addr ipv6_sid
;
2084 uint32_t srgb_range
;
2087 if (type
== BGP_PREFIX_SID_LABEL_INDEX
) {
2088 if (length
!= BGP_PREFIX_SID_LABEL_INDEX_LENGTH
) {
2091 "Prefix SID label index length is %d instead of %d",
2092 length
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
2093 return bgp_attr_malformed(args
,
2094 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2098 /* Ignore flags and reserved */
2099 stream_getc(peer
->curr
);
2100 stream_getw(peer
->curr
);
2102 /* Fetch the label index and see if it is valid. */
2103 label_index
= stream_getl(peer
->curr
);
2104 if (label_index
== BGP_INVALID_LABEL_INDEX
)
2105 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2108 /* Store label index; subsequently, we'll check on
2110 attr
->label_index
= label_index
;
2113 * Ignore the Label index attribute unless received for
2117 if (!mp_update
->length
2118 || mp_update
->safi
!= SAFI_LABELED_UNICAST
)
2119 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
2122 /* Placeholder code for the IPv6 SID type */
2123 else if (type
== BGP_PREFIX_SID_IPV6
) {
2124 if (length
!= BGP_PREFIX_SID_IPV6_LENGTH
) {
2125 flog_err(EC_BGP_ATTR_LEN
,
2126 "Prefix SID IPv6 length is %d instead of %d",
2127 length
, BGP_PREFIX_SID_IPV6_LENGTH
);
2128 return bgp_attr_malformed(args
,
2129 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2133 /* Ignore reserved */
2134 stream_getc(peer
->curr
);
2135 stream_getw(peer
->curr
);
2137 stream_get(&ipv6_sid
, peer
->curr
, 16);
2140 /* Placeholder code for the Originator SRGB type */
2141 else if (type
== BGP_PREFIX_SID_ORIGINATOR_SRGB
) {
2143 stream_getw(peer
->curr
);
2147 if (length
% BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
) {
2150 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2151 length
, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
);
2152 return bgp_attr_malformed(
2153 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2157 srgb_count
= length
/ BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
;
2159 for (int i
= 0; i
< srgb_count
; i
++) {
2160 stream_get(&srgb_base
, peer
->curr
, 3);
2161 stream_get(&srgb_range
, peer
->curr
, 3);
2165 return BGP_ATTR_PARSE_PROCEED
;
2168 /* Prefix SID attribute
2169 * draft-ietf-idr-bgp-prefix-sid-05
2171 bgp_attr_parse_ret_t
2172 bgp_attr_prefix_sid(int32_t tlength
, struct bgp_attr_parser_args
*args
,
2173 struct bgp_nlri
*mp_update
)
2175 struct peer
*const peer
= args
->peer
;
2176 struct attr
*const attr
= args
->attr
;
2177 bgp_attr_parse_ret_t ret
;
2179 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
2182 int32_t type
, length
;
2184 type
= stream_getc(peer
->curr
);
2185 length
= stream_getw(peer
->curr
);
2187 ret
= bgp_attr_psid_sub(type
, length
, args
, mp_update
);
2189 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2192 * Subtract length + the T and the L
2193 * since length is the Vector portion
2195 tlength
-= length
+ 3;
2200 "Prefix SID internal length %d causes us to read beyond the total Prefix SID length",
2202 return bgp_attr_malformed(args
,
2203 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2208 return BGP_ATTR_PARSE_PROCEED
;
2211 /* PMSI tunnel attribute (RFC 6514)
2212 * Basic validation checks done here.
2214 static bgp_attr_parse_ret_t
2215 bgp_attr_pmsi_tunnel(struct bgp_attr_parser_args
*args
)
2217 struct peer
*const peer
= args
->peer
;
2218 struct attr
*const attr
= args
->attr
;
2219 const bgp_size_t length
= args
->length
;
2222 /* Verify that the receiver is expecting "ingress replication" as we
2223 * can only support that.
2226 flog_err(EC_BGP_ATTR_LEN
, "Bad PMSI tunnel attribute length %d",
2228 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2231 stream_getc(peer
->curr
); /* Flags */
2232 tnl_type
= stream_getc(peer
->curr
);
2233 if (tnl_type
> PMSI_TNLTYPE_MAX
) {
2234 flog_err(EC_BGP_ATTR_PMSI_TYPE
,
2235 "Invalid PMSI tunnel attribute type %d", tnl_type
);
2236 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2239 if (tnl_type
== PMSI_TNLTYPE_INGR_REPL
) {
2241 flog_err(EC_BGP_ATTR_PMSI_LEN
,
2242 "Bad PMSI tunnel attribute length %d for IR",
2244 return bgp_attr_malformed(
2245 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2250 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
2251 attr
->pmsi_tnl_type
= tnl_type
;
2253 /* Forward read pointer of input stream. */
2254 stream_forward_getp(peer
->curr
, length
- 2);
2256 return BGP_ATTR_PARSE_PROCEED
;
2259 /* BGP unknown attribute treatment. */
2260 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2262 bgp_size_t total
= args
->total
;
2263 struct transit
*transit
;
2264 struct peer
*const peer
= args
->peer
;
2265 struct attr
*const attr
= args
->attr
;
2266 uint8_t *const startp
= args
->startp
;
2267 const uint8_t type
= args
->type
;
2268 const uint8_t flag
= args
->flags
;
2269 const bgp_size_t length
= args
->length
;
2271 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2273 "%s Unknown attribute is received (type %d, length %d)",
2274 peer
->host
, type
, length
);
2276 /* Forward read pointer of input stream. */
2277 stream_forward_getp(peer
->curr
, length
);
2279 /* If any of the mandatory well-known attributes are not recognized,
2280 then the Error Subcode is set to Unrecognized Well-known
2281 Attribute. The Data field contains the unrecognized attribute
2282 (type, length and value). */
2283 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2284 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2288 /* Unrecognized non-transitive optional attributes must be quietly
2289 ignored and not passed along to other BGP peers. */
2290 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2291 return BGP_ATTR_PARSE_PROCEED
;
2293 /* If a path with recognized transitive optional attribute is
2294 accepted and passed along to other BGP peers and the Partial bit
2295 in the Attribute Flags octet is set to 1 by some previous AS, it
2296 is not set back to 0 by the current AS. */
2297 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2299 /* Store transitive attribute to the end of attr->transit. */
2301 attr
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2303 transit
= attr
->transit
;
2306 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2307 transit
->length
+ total
);
2309 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2311 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2312 transit
->length
+= total
;
2314 return BGP_ATTR_PARSE_PROCEED
;
2317 /* Well-known attribute check. */
2318 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2322 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2324 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2325 return BGP_ATTR_PARSE_PROCEED
;
2327 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2328 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2329 are present, it should. Check for any other attribute being present
2332 if ((!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)) &&
2333 CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))))
2334 return BGP_ATTR_PARSE_PROCEED
;
2336 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2337 type
= BGP_ATTR_ORIGIN
;
2339 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2340 type
= BGP_ATTR_AS_PATH
;
2342 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2344 * NLRI is empty. We can't easily check NLRI empty here though.
2346 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2347 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2348 type
= BGP_ATTR_NEXT_HOP
;
2350 if (peer
->sort
== BGP_PEER_IBGP
2351 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2352 type
= BGP_ATTR_LOCAL_PREF
;
2355 flog_warn(EC_BGP_MISSING_ATTRIBUTE
,
2356 "%s Missing well-known attribute %s.", peer
->host
,
2357 lookup_msg(attr_str
, type
, NULL
));
2358 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2359 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2361 return BGP_ATTR_PARSE_ERROR
;
2363 return BGP_ATTR_PARSE_PROCEED
;
2366 /* Read attribute of update packet. This function is called from
2367 bgp_update_receive() in bgp_packet.c. */
2368 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2369 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2370 struct bgp_nlri
*mp_withdraw
)
2372 bgp_attr_parse_ret_t ret
;
2376 uint8_t *startp
, *endp
;
2378 uint8_t seen
[BGP_ATTR_BITMAP_SIZE
];
2379 /* we need the as4_path only until we have synthesized the as_path with
2381 /* same goes for as4_aggregator */
2382 struct aspath
*as4_path
= NULL
;
2383 as_t as4_aggregator
= 0;
2384 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2386 /* Initialize bitmap. */
2387 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2389 /* End pointer of BGP attribute. */
2390 endp
= BGP_INPUT_PNT(peer
) + size
;
2392 /* Get attributes to the end of attribute length. */
2393 while (BGP_INPUT_PNT(peer
) < endp
) {
2394 /* Check remaining length check.*/
2395 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2396 /* XXX warning: long int format, int arg (arg 5) */
2398 EC_BGP_ATTRIBUTE_TOO_SMALL
,
2399 "%s: error BGP attribute length %lu is smaller than min len",
2401 (unsigned long)(endp
2402 - stream_pnt(BGP_INPUT(peer
))));
2404 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2405 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2406 return BGP_ATTR_PARSE_ERROR
;
2409 /* Fetch attribute flag and type. */
2410 startp
= BGP_INPUT_PNT(peer
);
2411 /* "The lower-order four bits of the Attribute Flags octet are
2412 unused. They MUST be zero when sent and MUST be ignored when
2414 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2415 type
= stream_getc(BGP_INPUT(peer
));
2417 /* Check whether Extended-Length applies and is in bounds */
2418 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2419 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2421 EC_BGP_EXT_ATTRIBUTE_TOO_SMALL
,
2422 "%s: Extended length set, but just %lu bytes of attr header",
2424 (unsigned long)(endp
2425 - stream_pnt(BGP_INPUT(peer
))));
2427 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2428 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2429 return BGP_ATTR_PARSE_ERROR
;
2432 /* Check extended attribue length bit. */
2433 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2434 length
= stream_getw(BGP_INPUT(peer
));
2436 length
= stream_getc(BGP_INPUT(peer
));
2438 /* If any attribute appears more than once in the UPDATE
2439 message, then the Error Subcode is set to Malformed Attribute
2442 if (CHECK_BITMAP(seen
, type
)) {
2444 EC_BGP_ATTRIBUTE_REPEATED
,
2445 "%s: error BGP attribute type %d appears twice in a message",
2448 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2449 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2450 return BGP_ATTR_PARSE_ERROR
;
2453 /* Set type to bitmap to check duplicate attribute. `type' is
2454 unsigned char so it never overflow bitmap range. */
2456 SET_BITMAP(seen
, type
);
2458 /* Overflow check. */
2459 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2461 if (attr_endp
> endp
) {
2463 EC_BGP_ATTRIBUTE_TOO_LARGE
,
2464 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2465 peer
->host
, type
, length
, size
, attr_endp
,
2469 * If any recognized attribute has an Attribute
2470 * Length that conflicts with the expected length
2471 * (based on the attribute type code), then the
2472 * Error Subcode MUST be set to Attribute Length
2473 * Error. The Data field MUST contain the erroneous
2474 * attribute (type, length, and value).
2476 * We do not currently have a good way to determine the
2477 * length of the attribute independent of the length
2478 * received in the message. Instead we send the
2479 * minimum between the amount of data we have and the
2480 * amount specified by the attribute length field.
2482 * Instead of directly passing in the packet buffer and
2483 * offset we use the stream_get* functions to read into
2484 * a stack buffer, since they perform bounds checking
2485 * and we are working with untrusted data.
2487 unsigned char ndata
[BGP_MAX_PACKET_SIZE
];
2488 memset(ndata
, 0x00, sizeof(ndata
));
2490 CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 2 : 1;
2491 /* Rewind to end of flag field */
2492 stream_forward_getp(BGP_INPUT(peer
), -(1 + lfl
));
2494 stream_get(&ndata
[0], BGP_INPUT(peer
), 1);
2496 stream_get(&ndata
[1], BGP_INPUT(peer
), lfl
);
2498 size_t atl
= attr_endp
- startp
;
2499 size_t ndl
= MIN(atl
, STREAM_READABLE(BGP_INPUT(peer
)));
2500 stream_get(&ndata
[lfl
+ 1], BGP_INPUT(peer
), ndl
);
2502 bgp_notify_send_with_data(
2503 peer
, BGP_NOTIFY_UPDATE_ERR
,
2504 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, ndata
,
2507 return BGP_ATTR_PARSE_ERROR
;
2510 struct bgp_attr_parser_args attr_args
= {
2517 .total
= attr_endp
- startp
,
2521 /* If any recognized attribute has Attribute Flags that conflict
2522 with the Attribute Type Code, then the Error Subcode is set
2524 Attribute Flags Error. The Data field contains the erroneous
2525 attribute (type, length and value). */
2526 if (bgp_attr_flag_invalid(&attr_args
)) {
2527 ret
= bgp_attr_malformed(
2528 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2530 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2535 /* OK check attribute and store it's value. */
2537 case BGP_ATTR_ORIGIN
:
2538 ret
= bgp_attr_origin(&attr_args
);
2540 case BGP_ATTR_AS_PATH
:
2541 ret
= bgp_attr_aspath(&attr_args
);
2543 case BGP_ATTR_AS4_PATH
:
2544 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2546 case BGP_ATTR_NEXT_HOP
:
2547 ret
= bgp_attr_nexthop(&attr_args
);
2549 case BGP_ATTR_MULTI_EXIT_DISC
:
2550 ret
= bgp_attr_med(&attr_args
);
2552 case BGP_ATTR_LOCAL_PREF
:
2553 ret
= bgp_attr_local_pref(&attr_args
);
2555 case BGP_ATTR_ATOMIC_AGGREGATE
:
2556 ret
= bgp_attr_atomic(&attr_args
);
2558 case BGP_ATTR_AGGREGATOR
:
2559 ret
= bgp_attr_aggregator(&attr_args
);
2561 case BGP_ATTR_AS4_AGGREGATOR
:
2562 ret
= bgp_attr_as4_aggregator(&attr_args
,
2564 &as4_aggregator_addr
);
2566 case BGP_ATTR_COMMUNITIES
:
2567 ret
= bgp_attr_community(&attr_args
);
2569 case BGP_ATTR_LARGE_COMMUNITIES
:
2570 ret
= bgp_attr_large_community(&attr_args
);
2572 case BGP_ATTR_ORIGINATOR_ID
:
2573 ret
= bgp_attr_originator_id(&attr_args
);
2575 case BGP_ATTR_CLUSTER_LIST
:
2576 ret
= bgp_attr_cluster_list(&attr_args
);
2578 case BGP_ATTR_MP_REACH_NLRI
:
2579 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2581 case BGP_ATTR_MP_UNREACH_NLRI
:
2582 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2584 case BGP_ATTR_EXT_COMMUNITIES
:
2585 ret
= bgp_attr_ext_communities(&attr_args
);
2590 case BGP_ATTR_ENCAP
:
2591 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2594 case BGP_ATTR_PREFIX_SID
:
2595 ret
= bgp_attr_prefix_sid(length
,
2596 &attr_args
, mp_update
);
2598 case BGP_ATTR_PMSI_TUNNEL
:
2599 ret
= bgp_attr_pmsi_tunnel(&attr_args
);
2602 ret
= bgp_attr_unknown(&attr_args
);
2606 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2607 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2608 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2609 ret
= BGP_ATTR_PARSE_ERROR
;
2612 if (ret
== BGP_ATTR_PARSE_EOR
) {
2614 aspath_unintern(&as4_path
);
2618 /* If hard error occured immediately return to the caller. */
2619 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2620 flog_warn(EC_BGP_ATTRIBUTE_PARSE_ERROR
,
2621 "%s: Attribute %s, parse error", peer
->host
,
2622 lookup_msg(attr_str
, type
, NULL
));
2624 aspath_unintern(&as4_path
);
2627 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2630 EC_BGP_ATTRIBUTE_PARSE_WITHDRAW
,
2631 "%s: Attribute %s, parse error - treating as withdrawal",
2632 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2634 aspath_unintern(&as4_path
);
2638 /* Check the fetched length. */
2639 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2640 flog_warn(EC_BGP_ATTRIBUTE_FETCH_ERROR
,
2641 "%s: BGP attribute %s, fetch error",
2642 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2643 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2644 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2646 aspath_unintern(&as4_path
);
2647 return BGP_ATTR_PARSE_ERROR
;
2651 /* Check final read pointer is same as end pointer. */
2652 if (BGP_INPUT_PNT(peer
) != endp
) {
2653 flog_warn(EC_BGP_ATTRIBUTES_MISMATCH
,
2654 "%s: BGP attribute %s, length mismatch", peer
->host
,
2655 lookup_msg(attr_str
, type
, NULL
));
2656 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2657 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2659 aspath_unintern(&as4_path
);
2660 return BGP_ATTR_PARSE_ERROR
;
2663 /* Check all mandatory well-known attributes are present */
2664 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2666 aspath_unintern(&as4_path
);
2671 * At this place we can see whether we got AS4_PATH and/or
2672 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2673 * We can not do this before we've read all attributes because
2674 * the as4 handling does not say whether AS4_PATH has to be sent
2675 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2676 * in relationship to AGGREGATOR.
2677 * So, to be defensive, we are not relying on any order and read
2678 * all attributes first, including these 32bit ones, and now,
2679 * afterwards, we look what and if something is to be done for as4.
2681 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2684 /* actually... this doesn't ever return failure currently, but
2685 * better safe than sorry */
2686 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2687 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2688 &as4_aggregator_addr
)) {
2689 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2690 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2692 aspath_unintern(&as4_path
);
2693 return BGP_ATTR_PARSE_ERROR
;
2696 /* At this stage, we have done all fiddling with as4, and the
2697 * resulting info is in attr->aggregator resp. attr->aspath
2698 * so we can chuck as4_aggregator and as4_path alltogether in
2699 * order to save memory
2702 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2703 /* The flag that we got this is still there, but that does not
2708 * The "rest" of the code does nothing with as4_aggregator.
2709 * there is no memory attached specifically which is not part
2711 * so ignoring just means do nothing.
2714 * Finally do the checks on the aspath we did not do yet
2715 * because we waited for a potentially synthesized aspath.
2717 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2718 ret
= bgp_attr_aspath_check(peer
, attr
);
2719 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2722 /* Finally intern unknown attribute. */
2724 attr
->transit
= transit_intern(attr
->transit
);
2725 if (attr
->encap_subtlvs
)
2726 attr
->encap_subtlvs
=
2727 encap_intern(attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2729 if (attr
->vnc_subtlvs
)
2731 encap_intern(attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2734 return BGP_ATTR_PARSE_PROCEED
;
2737 size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
, afi_t afi
,
2738 safi_t safi
, struct bpacket_attr_vec_arr
*vecarr
,
2743 iana_safi_t pkt_safi
;
2746 /* Set extended bit always to encode the attribute length as 2 bytes */
2747 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2748 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2749 sizep
= stream_get_endp(s
);
2750 stream_putw(s
, 0); /* Marker: Attribute length. */
2753 /* Convert AFI, SAFI to values for packet. */
2754 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2756 stream_putw(s
, pkt_afi
); /* AFI */
2757 stream_putc(s
, pkt_safi
); /* SAFI */
2761 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
))
2762 nh_afi
= peer_cap_enhe(peer
, afi
, safi
) ? AFI_IP6
: AFI_IP
;
2764 nh_afi
= BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->mp_nexthop_len
);
2767 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2772 case SAFI_MULTICAST
:
2773 case SAFI_LABELED_UNICAST
:
2775 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2779 stream_putl(s
, 0); /* RD = 0, per RFC */
2781 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2786 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2789 stream_putc(s
, 0); /* no nexthop for flowspec */
2797 case SAFI_MULTICAST
:
2798 case SAFI_LABELED_UNICAST
:
2800 if (attr
->mp_nexthop_len
2801 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2803 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
);
2804 stream_put(s
, &attr
->mp_nexthop_global
,
2806 stream_put(s
, &attr
->mp_nexthop_local
,
2809 stream_putc(s
, IPV6_MAX_BYTELEN
);
2810 stream_put(s
, &attr
->mp_nexthop_global
,
2814 case SAFI_MPLS_VPN
: {
2815 if (attr
->mp_nexthop_len
2816 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2818 stream_putl(s
, 0); /* RD = 0, per RFC */
2820 stream_put(s
, &attr
->mp_nexthop_global
,
2822 } else if (attr
->mp_nexthop_len
2823 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2825 stream_putl(s
, 0); /* RD = 0, per RFC */
2827 stream_put(s
, &attr
->mp_nexthop_global
,
2829 stream_putl(s
, 0); /* RD = 0, per RFC */
2831 stream_put(s
, &attr
->mp_nexthop_local
,
2836 stream_putc(s
, IPV6_MAX_BYTELEN
);
2837 stream_put(s
, &attr
->mp_nexthop_global
,
2841 stream_putc(s
, 0); /* no nexthop for flowspec */
2847 if (safi
!= SAFI_FLOWSPEC
)
2849 EC_BGP_ATTR_NH_SEND_LEN
,
2850 "Bad nexthop when sending to %s, AFI %u SAFI %u nhlen %d",
2851 peer
->host
, afi
, safi
, attr
->mp_nexthop_len
);
2860 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2861 struct prefix
*p
, struct prefix_rd
*prd
,
2862 mpls_label_t
*label
, uint32_t num_labels
,
2863 int addpath_encode
, uint32_t addpath_tx_id
,
2866 if (safi
== SAFI_MPLS_VPN
) {
2868 stream_putl(s
, addpath_tx_id
);
2869 /* Label, RD, Prefix write. */
2870 stream_putc(s
, p
->prefixlen
+ 88);
2871 stream_put(s
, label
, BGP_LABEL_BYTES
);
2872 stream_put(s
, prd
->val
, 8);
2873 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2874 } else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
2875 /* EVPN prefix - contents depend on type */
2876 bgp_evpn_encode_prefix(s
, p
, prd
, label
, num_labels
, attr
,
2877 addpath_encode
, addpath_tx_id
);
2878 } else if (safi
== SAFI_LABELED_UNICAST
) {
2879 /* Prefix write with label. */
2880 stream_put_labeled_prefix(s
, p
, label
);
2881 } else if (safi
== SAFI_FLOWSPEC
) {
2882 if (PSIZE (p
->prefixlen
)+2 < FLOWSPEC_NLRI_SIZELIMIT
)
2883 stream_putc(s
, PSIZE (p
->prefixlen
)+2);
2885 stream_putw(s
, (PSIZE (p
->prefixlen
)+2)|(0xf<<12));
2886 stream_putc(s
, 2);/* Filter type */
2887 stream_putc(s
, p
->prefixlen
);/* Prefix length */
2888 stream_put(s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
2890 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2893 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2895 int size
= PSIZE(p
->prefixlen
);
2896 if (safi
== SAFI_MPLS_VPN
)
2898 else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2899 size
+= 232; // TODO: Maximum possible for type-2, type-3 and
2905 * Encodes the tunnel encapsulation attribute,
2906 * and with ENABLE_BGP_VNC the VNC attribute which uses
2907 * almost the same TLV format
2909 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2910 struct stream
*s
, struct attr
*attr
,
2913 unsigned int attrlenfield
= 0;
2914 unsigned int attrhdrlen
= 0;
2915 struct bgp_attr_encap_subtlv
*subtlvs
;
2916 struct bgp_attr_encap_subtlv
*st
;
2917 const char *attrname
;
2919 if (!attr
|| (attrtype
== BGP_ATTR_ENCAP
2920 && (!attr
->encap_tunneltype
2921 || attr
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2925 case BGP_ATTR_ENCAP
:
2926 attrname
= "Tunnel Encap";
2927 subtlvs
= attr
->encap_subtlvs
;
2928 if (subtlvs
== NULL
) /* nothing to do */
2931 * The tunnel encap attr has an "outer" tlv.
2933 * L = total length of subtlvs,
2934 * V = concatenated subtlvs.
2936 attrlenfield
= 2 + 2; /* T + L */
2937 attrhdrlen
= 1 + 1; /* subTLV T + L */
2943 subtlvs
= attr
->vnc_subtlvs
;
2944 if (subtlvs
== NULL
) /* nothing to do */
2946 attrlenfield
= 0; /* no outer T + L */
2947 attrhdrlen
= 2 + 2; /* subTLV T + L */
2955 /* compute attr length */
2956 for (st
= subtlvs
; st
; st
= st
->next
) {
2957 attrlenfield
+= (attrhdrlen
+ st
->length
);
2960 if (attrlenfield
> 0xffff) {
2961 zlog_info("%s attribute is too long (length=%d), can't send it",
2962 attrname
, attrlenfield
);
2966 if (attrlenfield
> 0xff) {
2967 /* 2-octet length field */
2969 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2970 | BGP_ATTR_FLAG_EXTLEN
);
2971 stream_putc(s
, attrtype
);
2972 stream_putw(s
, attrlenfield
& 0xffff);
2974 /* 1-octet length field */
2975 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2976 stream_putc(s
, attrtype
);
2977 stream_putc(s
, attrlenfield
& 0xff);
2980 if (attrtype
== BGP_ATTR_ENCAP
) {
2981 /* write outer T+L */
2982 stream_putw(s
, attr
->encap_tunneltype
);
2983 stream_putw(s
, attrlenfield
- 4);
2986 /* write each sub-tlv */
2987 for (st
= subtlvs
; st
; st
= st
->next
) {
2988 if (attrtype
== BGP_ATTR_ENCAP
) {
2989 stream_putc(s
, st
->type
);
2990 stream_putc(s
, st
->length
);
2993 stream_putw(s
, st
->type
);
2994 stream_putw(s
, st
->length
);
2997 stream_put(s
, st
->value
, st
->length
);
3001 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
3003 /* Set MP attribute length. Don't count the (2) bytes used to encode
3005 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
3008 /* Make attribute packet. */
3009 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
3010 struct stream
*s
, struct attr
*attr
,
3011 struct bpacket_attr_vec_arr
*vecarr
,
3012 struct prefix
*p
, afi_t afi
, safi_t safi
,
3013 struct peer
*from
, struct prefix_rd
*prd
,
3014 mpls_label_t
*label
, uint32_t num_labels
,
3015 int addpath_encode
, uint32_t addpath_tx_id
)
3018 size_t aspath_sizep
;
3019 struct aspath
*aspath
;
3020 int send_as4_path
= 0;
3021 int send_as4_aggregator
= 0;
3022 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
3027 /* Remember current pointer. */
3028 cp
= stream_get_endp(s
);
3031 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
3032 && !peer_cap_enhe(peer
, afi
, safi
))) {
3033 size_t mpattrlen_pos
= 0;
3035 mpattrlen_pos
= bgp_packet_mpattr_start(s
, peer
, afi
, safi
,
3037 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
3038 num_labels
, addpath_encode
,
3039 addpath_tx_id
, attr
);
3040 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
3043 /* Origin attribute. */
3044 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3045 stream_putc(s
, BGP_ATTR_ORIGIN
);
3047 stream_putc(s
, attr
->origin
);
3049 /* AS path attribute. */
3051 /* If remote-peer is EBGP */
3052 if (peer
->sort
== BGP_PEER_EBGP
3053 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3054 PEER_FLAG_AS_PATH_UNCHANGED
)
3055 || attr
->aspath
->segments
== NULL
)
3056 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3057 PEER_FLAG_RSERVER_CLIENT
))) {
3058 aspath
= aspath_dup(attr
->aspath
);
3060 /* Even though we may not be configured for confederations we
3062 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
3063 aspath
= aspath_delete_confed_seq(aspath
);
3065 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
3066 /* Stuff our path CONFED_ID on the front */
3067 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
3069 if (peer
->change_local_as
) {
3070 /* If replace-as is specified, we only use the
3071 change_local_as when
3072 advertising routes. */
3075 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
3076 aspath
= aspath_add_seq(aspath
,
3079 aspath
= aspath_add_seq(aspath
,
3080 peer
->change_local_as
);
3082 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
3085 } else if (peer
->sort
== BGP_PEER_CONFED
) {
3086 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
3088 aspath
= aspath_dup(attr
->aspath
);
3089 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
3091 aspath
= attr
->aspath
;
3093 /* If peer is not AS4 capable, then:
3094 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
3095 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
3097 * types are in it (i.e. exclude them if they are there)
3098 * AND do this only if there is at least one asnum > 65535 in the
3100 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
3102 * all ASnums > 65535 to BGP_AS_TRANS
3105 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3106 stream_putc(s
, BGP_ATTR_AS_PATH
);
3107 aspath_sizep
= stream_get_endp(s
);
3109 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
3111 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
3114 if (!use32bit
&& aspath_has_as4(aspath
))
3116 1; /* we'll do this later, at the correct place */
3118 /* Nexthop attribute. */
3119 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
3120 && !peer_cap_enhe(peer
, afi
, safi
)) {
3121 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
3122 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3123 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3124 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
3127 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3128 } else if (peer_cap_enhe(from
, afi
, safi
)) {
3130 * Likely this is the case when an IPv4 prefix was
3132 * Extended Next-hop capability and now being advertised
3135 * Setting the mandatory (ipv4) next-hop attribute here
3137 * implicit next-hop self with correct (ipv4 address
3140 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3141 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3142 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
3145 stream_put_ipv4(s
, 0);
3149 /* MED attribute. */
3150 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
3151 || bgp
->maxmed_active
) {
3152 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3153 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3155 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
3159 /* Local preference. */
3160 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
3161 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3162 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3164 stream_putl(s
, attr
->local_pref
);
3167 /* Atomic aggregate. */
3168 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3169 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3170 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3175 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3176 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3177 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3178 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3181 /* AS4 capable peer */
3183 stream_putl(s
, attr
->aggregator_as
);
3185 /* 2-byte AS peer */
3188 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3190 if (attr
->aggregator_as
> 65535) {
3191 stream_putw(s
, BGP_AS_TRANS
);
3193 /* we have to send AS4_AGGREGATOR, too.
3194 * we'll do that later in order to send
3195 * attributes in ascending
3198 send_as4_aggregator
= 1;
3200 stream_putw(s
, (uint16_t)attr
->aggregator_as
);
3202 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3205 /* Community attribute. */
3206 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3207 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3208 if (attr
->community
->size
* 4 > 255) {
3210 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3211 | BGP_ATTR_FLAG_EXTLEN
);
3212 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3213 stream_putw(s
, attr
->community
->size
* 4);
3216 BGP_ATTR_FLAG_OPTIONAL
3217 | BGP_ATTR_FLAG_TRANS
);
3218 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3219 stream_putc(s
, attr
->community
->size
* 4);
3221 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3225 * Large Community attribute.
3227 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3228 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3229 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3230 if (lcom_length(attr
->lcommunity
) > 255) {
3232 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3233 | BGP_ATTR_FLAG_EXTLEN
);
3234 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3235 stream_putw(s
, lcom_length(attr
->lcommunity
));
3238 BGP_ATTR_FLAG_OPTIONAL
3239 | BGP_ATTR_FLAG_TRANS
);
3240 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3241 stream_putc(s
, lcom_length(attr
->lcommunity
));
3243 stream_put(s
, attr
->lcommunity
->val
,
3244 lcom_length(attr
->lcommunity
));
3247 /* Route Reflector. */
3248 if (peer
->sort
== BGP_PEER_IBGP
&& from
3249 && from
->sort
== BGP_PEER_IBGP
) {
3250 /* Originator ID. */
3251 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3252 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3255 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3256 stream_put_in_addr(s
, &attr
->originator_id
);
3258 stream_put_in_addr(s
, &from
->remote_id
);
3261 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3262 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3264 if (attr
->cluster
) {
3265 stream_putc(s
, attr
->cluster
->length
+ 4);
3266 /* If this peer configuration's parent BGP has
3268 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3269 stream_put_in_addr(s
, &bgp
->cluster_id
);
3271 stream_put_in_addr(s
, &bgp
->router_id
);
3272 stream_put(s
, attr
->cluster
->list
,
3273 attr
->cluster
->length
);
3276 /* If this peer configuration's parent BGP has
3278 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3279 stream_put_in_addr(s
, &bgp
->cluster_id
);
3281 stream_put_in_addr(s
, &bgp
->router_id
);
3285 /* Extended Communities attribute. */
3286 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3287 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3288 if (peer
->sort
== BGP_PEER_IBGP
3289 || peer
->sort
== BGP_PEER_CONFED
) {
3290 if (attr
->ecommunity
->size
* 8 > 255) {
3292 BGP_ATTR_FLAG_OPTIONAL
3293 | BGP_ATTR_FLAG_TRANS
3294 | BGP_ATTR_FLAG_EXTLEN
);
3295 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3296 stream_putw(s
, attr
->ecommunity
->size
* 8);
3299 BGP_ATTR_FLAG_OPTIONAL
3300 | BGP_ATTR_FLAG_TRANS
);
3301 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3302 stream_putc(s
, attr
->ecommunity
->size
* 8);
3304 stream_put(s
, attr
->ecommunity
->val
,
3305 attr
->ecommunity
->size
* 8);
3309 int ecom_tr_size
= 0;
3312 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3313 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3316 if (CHECK_FLAG(tbit
,
3317 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3324 if (ecom_tr_size
* 8 > 255) {
3327 BGP_ATTR_FLAG_OPTIONAL
3328 | BGP_ATTR_FLAG_TRANS
3329 | BGP_ATTR_FLAG_EXTLEN
);
3331 BGP_ATTR_EXT_COMMUNITIES
);
3332 stream_putw(s
, ecom_tr_size
* 8);
3336 BGP_ATTR_FLAG_OPTIONAL
3337 | BGP_ATTR_FLAG_TRANS
);
3339 BGP_ATTR_EXT_COMMUNITIES
);
3340 stream_putc(s
, ecom_tr_size
* 8);
3343 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3344 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3349 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3352 stream_put(s
, pnt
, 8);
3358 /* Label index attribute. */
3359 if (safi
== SAFI_LABELED_UNICAST
) {
3360 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3361 uint32_t label_index
;
3363 label_index
= attr
->label_index
;
3365 if (label_index
!= BGP_INVALID_LABEL_INDEX
) {
3367 BGP_ATTR_FLAG_OPTIONAL
3368 | BGP_ATTR_FLAG_TRANS
);
3369 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3371 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3373 BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3374 stream_putc(s
, 0); // reserved
3375 stream_putw(s
, 0); // flags
3376 stream_putl(s
, label_index
);
3381 if (send_as4_path
) {
3382 /* If the peer is NOT As4 capable, AND */
3383 /* there are ASnums > 65535 in path THEN
3384 * give out AS4_PATH */
3386 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3388 * Hm, I wonder... confederation things *should* only be at
3389 * the beginning of an aspath, right? Then we should use
3390 * aspath_delete_confed_seq for this, because it is already
3392 * Folks, talk to me: what is reasonable here!?
3394 aspath
= aspath_delete_confed_seq(aspath
);
3397 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3398 | BGP_ATTR_FLAG_EXTLEN
);
3399 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3400 aspath_sizep
= stream_get_endp(s
);
3402 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3405 if (aspath
!= attr
->aspath
)
3406 aspath_free(aspath
);
3408 if (send_as4_aggregator
) {
3409 /* send AS4_AGGREGATOR, at this place */
3410 /* this section of code moved here in order to ensure the
3412 * *ascending* order of attributes
3414 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3415 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3417 stream_putl(s
, attr
->aggregator_as
);
3418 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3421 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3422 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3423 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3424 /* Tunnel Encap attribute */
3425 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3429 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3434 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
3435 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3436 stream_putc(s
, BGP_ATTR_PMSI_TUNNEL
);
3437 stream_putc(s
, 9); // Length
3438 stream_putc(s
, 0); // Flags
3439 stream_putc(s
, PMSI_TNLTYPE_INGR_REPL
); // IR (6)
3440 stream_put(s
, &(attr
->label
),
3441 BGP_LABEL_BYTES
); // MPLS Label / VXLAN VNI
3442 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3443 // Unicast tunnel endpoint IP address
3446 /* Unknown transit attribute. */
3448 stream_put(s
, attr
->transit
->val
, attr
->transit
->length
);
3450 /* Return total size of attribute. */
3451 return stream_get_endp(s
) - cp
;
3454 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3456 unsigned long attrlen_pnt
;
3458 iana_safi_t pkt_safi
;
3460 /* Set extended bit always to encode the attribute length as 2 bytes */
3461 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3462 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3464 attrlen_pnt
= stream_get_endp(s
);
3465 stream_putw(s
, 0); /* Length of this attribute. */
3467 /* Convert AFI, SAFI to values for packet. */
3468 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3470 stream_putw(s
, pkt_afi
);
3471 stream_putc(s
, pkt_safi
);
3476 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3477 safi_t safi
, struct prefix_rd
*prd
,
3478 mpls_label_t
*label
, uint32_t num_labels
,
3479 int addpath_encode
, uint32_t addpath_tx_id
,
3482 uint8_t wlabel
[3] = {0x80, 0x00, 0x00};
3484 if (safi
== SAFI_LABELED_UNICAST
) {
3485 label
= (mpls_label_t
*)wlabel
;
3489 return bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
, num_labels
,
3490 addpath_encode
, addpath_tx_id
, attr
);
3493 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3495 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3498 /* Initialization of attribute. */
3499 void bgp_attr_init(void)
3511 void bgp_attr_finish(void)
3516 ecommunity_finish();
3517 lcommunity_finish();
3523 /* Make attribute packet. */
3524 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3525 struct prefix
*prefix
)
3530 struct aspath
*aspath
;
3531 int addpath_encode
= 0;
3532 uint32_t addpath_tx_id
= 0;
3534 /* Remember current pointer. */
3535 cp
= stream_get_endp(s
);
3537 /* Place holder of length. */
3540 /* Origin attribute. */
3541 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3542 stream_putc(s
, BGP_ATTR_ORIGIN
);
3544 stream_putc(s
, attr
->origin
);
3546 aspath
= attr
->aspath
;
3548 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3549 stream_putc(s
, BGP_ATTR_AS_PATH
);
3550 aspath_lenp
= stream_get_endp(s
);
3553 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3555 /* Nexthop attribute. */
3556 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3557 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3558 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3559 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3561 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3564 /* MED attribute. */
3565 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3566 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3567 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3569 stream_putl(s
, attr
->med
);
3572 /* Local preference. */
3573 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3574 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3575 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3577 stream_putl(s
, attr
->local_pref
);
3580 /* Atomic aggregate. */
3581 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3582 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3583 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3588 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3589 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3590 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3592 stream_putl(s
, attr
->aggregator_as
);
3593 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3596 /* Community attribute. */
3597 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3598 if (attr
->community
->size
* 4 > 255) {
3600 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3601 | BGP_ATTR_FLAG_EXTLEN
);
3602 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3603 stream_putw(s
, attr
->community
->size
* 4);
3606 BGP_ATTR_FLAG_OPTIONAL
3607 | BGP_ATTR_FLAG_TRANS
);
3608 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3609 stream_putc(s
, attr
->community
->size
* 4);
3611 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3614 /* Large Community attribute. */
3615 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3616 if (lcom_length(attr
->lcommunity
) > 255) {
3618 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3619 | BGP_ATTR_FLAG_EXTLEN
);
3620 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3621 stream_putw(s
, lcom_length(attr
->lcommunity
));
3624 BGP_ATTR_FLAG_OPTIONAL
3625 | BGP_ATTR_FLAG_TRANS
);
3626 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3627 stream_putc(s
, lcom_length(attr
->lcommunity
));
3630 stream_put(s
, attr
->lcommunity
->val
,
3631 lcom_length(attr
->lcommunity
));
3634 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3635 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
3636 && (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3637 || attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3640 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3641 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3642 sizep
= stream_get_endp(s
);
3645 stream_putc(s
, 0); /* Marker: Attribute length. */
3646 stream_putw(s
, AFI_IP6
); /* AFI */
3647 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3650 stream_putc(s
, attr
->mp_nexthop_len
);
3651 stream_put(s
, &attr
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3652 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3653 stream_put(s
, &attr
->mp_nexthop_local
,
3660 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3663 /* Set MP attribute length. */
3664 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3668 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3669 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
3671 BGP_ATTR_FLAG_OPTIONAL
3672 | BGP_ATTR_FLAG_TRANS
);
3673 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3675 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3676 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3677 stream_putc(s
, 0); // reserved
3678 stream_putw(s
, 0); // flags
3679 stream_putl(s
, attr
->label_index
);
3683 /* Return total size of attribute. */
3684 len
= stream_get_endp(s
) - cp
- 2;
3685 stream_putw_at(s
, cp
, len
);