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"
59 /* Attribute strings for logging. */
60 static const struct message attr_str
[] = {
61 {BGP_ATTR_ORIGIN
, "ORIGIN"},
62 {BGP_ATTR_AS_PATH
, "AS_PATH"},
63 {BGP_ATTR_NEXT_HOP
, "NEXT_HOP"},
64 {BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC"},
65 {BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF"},
66 {BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE"},
67 {BGP_ATTR_AGGREGATOR
, "AGGREGATOR"},
68 {BGP_ATTR_COMMUNITIES
, "COMMUNITY"},
69 {BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID"},
70 {BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST"},
71 {BGP_ATTR_DPA
, "DPA"},
72 {BGP_ATTR_ADVERTISER
, "ADVERTISER"},
73 {BGP_ATTR_RCID_PATH
, "RCID_PATH"},
74 {BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI"},
75 {BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI"},
76 {BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES"},
77 {BGP_ATTR_AS4_PATH
, "AS4_PATH"},
78 {BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR"},
79 {BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT"},
80 {BGP_ATTR_PMSI_TUNNEL
, "PMSI_TUNNEL_ATTRIBUTE"},
81 {BGP_ATTR_ENCAP
, "ENCAP"},
82 #if ENABLE_BGP_VNC_ATTR
83 {BGP_ATTR_VNC
, "VNC"},
85 {BGP_ATTR_LARGE_COMMUNITIES
, "LARGE_COMMUNITY"},
86 {BGP_ATTR_PREFIX_SID
, "PREFIX_SID"},
89 static const struct message attr_flag_str
[] = {
90 {BGP_ATTR_FLAG_OPTIONAL
, "Optional"},
91 {BGP_ATTR_FLAG_TRANS
, "Transitive"},
92 {BGP_ATTR_FLAG_PARTIAL
, "Partial"},
93 /* bgp_attr_flags_diagnose() relies on this bit being last in
95 {BGP_ATTR_FLAG_EXTLEN
, "Extended Length"},
98 static struct hash
*cluster_hash
;
100 static void *cluster_hash_alloc(void *p
)
102 const struct cluster_list
*val
= (const struct cluster_list
*)p
;
103 struct cluster_list
*cluster
;
105 cluster
= XMALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
106 cluster
->length
= val
->length
;
108 if (cluster
->length
) {
109 cluster
->list
= XMALLOC(MTYPE_CLUSTER_VAL
, val
->length
);
110 memcpy(cluster
->list
, val
->list
, val
->length
);
112 cluster
->list
= NULL
;
119 /* Cluster list related functions. */
120 static struct cluster_list
*cluster_parse(struct in_addr
*pnt
, int length
)
122 struct cluster_list tmp
;
123 struct cluster_list
*cluster
;
128 cluster
= hash_get(cluster_hash
, &tmp
, cluster_hash_alloc
);
133 int cluster_loop_check(struct cluster_list
*cluster
, struct in_addr originator
)
137 for (i
= 0; i
< cluster
->length
/ 4; i
++)
138 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
143 static unsigned int cluster_hash_key_make(void *p
)
145 const struct cluster_list
*cluster
= p
;
147 return jhash(cluster
->list
, cluster
->length
, 0);
150 static bool cluster_hash_cmp(const void *p1
, const void *p2
)
152 const struct cluster_list
*cluster1
= p1
;
153 const struct cluster_list
*cluster2
= p2
;
155 return (cluster1
->length
== cluster2
->length
156 && memcmp(cluster1
->list
, cluster2
->list
, cluster1
->length
)
160 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 bool 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 bgp_route_evpn
));
398 /* Unknown transit attribute. */
399 static struct hash
*transit_hash
;
401 static void transit_free(struct transit
*transit
)
403 XFREE(MTYPE_TRANSIT_VAL
, transit
->val
);
404 XFREE(MTYPE_TRANSIT
, transit
);
407 static void *transit_hash_alloc(void *p
)
409 /* Transit structure is already allocated. */
413 static struct transit
*transit_intern(struct transit
*transit
)
415 struct transit
*find
;
417 find
= hash_get(transit_hash
, transit
, transit_hash_alloc
);
419 transit_free(transit
);
425 void transit_unintern(struct transit
*transit
)
430 if (transit
->refcnt
== 0) {
431 hash_release(transit_hash
, transit
);
432 transit_free(transit
);
436 static unsigned int transit_hash_key_make(void *p
)
438 const struct transit
*transit
= p
;
440 return jhash(transit
->val
, transit
->length
, 0);
443 static bool transit_hash_cmp(const void *p1
, const void *p2
)
445 const struct transit
*transit1
= p1
;
446 const struct transit
*transit2
= p2
;
448 return (transit1
->length
== transit2
->length
449 && memcmp(transit1
->val
, transit2
->val
, transit1
->length
) == 0);
452 static void transit_init(void)
454 transit_hash
= hash_create(transit_hash_key_make
, transit_hash_cmp
,
458 static void transit_finish(void)
460 hash_clean(transit_hash
, (void (*)(void *))transit_free
);
461 hash_free(transit_hash
);
465 /* Attribute hash routines. */
466 static struct hash
*attrhash
;
468 /* Shallow copy of an attribute
469 * Though, not so shallow that it doesn't copy the contents
470 * of the attr_extra pointed to by 'extra'
472 void bgp_attr_dup(struct attr
*new, struct attr
*orig
)
477 unsigned long int attr_count(void)
479 return attrhash
->count
;
482 unsigned long int attr_unknown_count(void)
484 return transit_hash
->count
;
487 unsigned int attrhash_key_make(void *p
)
489 const struct attr
*attr
= (struct attr
*)p
;
491 #define MIX(val) key = jhash_1word(val, key)
492 #define MIX3(a, b, c) key = jhash_3words((a), (b), (c), key)
494 MIX3(attr
->origin
, attr
->nexthop
.s_addr
, attr
->med
);
495 MIX3(attr
->local_pref
, attr
->aggregator_as
,
496 attr
->aggregator_addr
.s_addr
);
497 MIX3(attr
->weight
, attr
->mp_nexthop_global_in
.s_addr
,
498 attr
->originator_id
.s_addr
);
499 MIX3(attr
->tag
, attr
->label
, attr
->label_index
);
502 MIX(aspath_key_make(attr
->aspath
));
504 MIX(community_hash_make(attr
->community
));
506 if (attr
->lcommunity
)
507 MIX(lcommunity_hash_make(attr
->lcommunity
));
508 if (attr
->ecommunity
)
509 MIX(ecommunity_hash_make(attr
->ecommunity
));
511 MIX(cluster_hash_key_make(attr
->cluster
));
513 MIX(transit_hash_key_make(attr
->transit
));
514 if (attr
->encap_subtlvs
)
515 MIX(encap_hash_key_make(attr
->encap_subtlvs
));
517 if (attr
->vnc_subtlvs
)
518 MIX(encap_hash_key_make(attr
->vnc_subtlvs
));
520 MIX(attr
->mp_nexthop_len
);
521 key
= jhash(attr
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
522 key
= jhash(attr
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
523 MIX(attr
->nh_ifindex
);
524 MIX(attr
->nh_lla_ifindex
);
529 bool attrhash_cmp(const void *p1
, const void *p2
)
531 const struct attr
*attr1
= p1
;
532 const struct attr
*attr2
= p2
;
534 if (attr1
->flag
== attr2
->flag
&& attr1
->origin
== attr2
->origin
535 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
536 && attr1
->aspath
== attr2
->aspath
537 && attr1
->community
== attr2
->community
&& attr1
->med
== attr2
->med
538 && attr1
->local_pref
== attr2
->local_pref
539 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
) {
540 if (attr1
->aggregator_as
== attr2
->aggregator_as
541 && attr1
->aggregator_addr
.s_addr
542 == attr2
->aggregator_addr
.s_addr
543 && attr1
->weight
== attr2
->weight
544 && attr1
->tag
== attr2
->tag
545 && attr1
->label_index
== attr2
->label_index
546 && attr1
->mp_nexthop_len
== attr2
->mp_nexthop_len
547 && attr1
->ecommunity
== attr2
->ecommunity
548 && attr1
->lcommunity
== attr2
->lcommunity
549 && attr1
->cluster
== attr2
->cluster
550 && attr1
->transit
== attr2
->transit
551 && (attr1
->encap_tunneltype
== attr2
->encap_tunneltype
)
552 && encap_same(attr1
->encap_subtlvs
, attr2
->encap_subtlvs
)
554 && encap_same(attr1
->vnc_subtlvs
, attr2
->vnc_subtlvs
)
556 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_global
,
557 &attr2
->mp_nexthop_global
)
558 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_local
,
559 &attr2
->mp_nexthop_local
)
560 && IPV4_ADDR_SAME(&attr1
->mp_nexthop_global_in
,
561 &attr2
->mp_nexthop_global_in
)
562 && IPV4_ADDR_SAME(&attr1
->originator_id
,
563 &attr2
->originator_id
)
564 && overlay_index_same(attr1
, attr2
)
565 && attr1
->nh_ifindex
== attr2
->nh_ifindex
566 && attr1
->nh_lla_ifindex
== attr2
->nh_lla_ifindex
)
573 static void attrhash_init(void)
576 hash_create(attrhash_key_make
, attrhash_cmp
, "BGP Attributes");
580 * special for hash_clean below
582 static void attr_vfree(void *attr
)
584 XFREE(MTYPE_ATTR
, attr
);
587 static void attrhash_finish(void)
589 hash_clean(attrhash
, attr_vfree
);
594 static void attr_show_all_iterator(struct hash_bucket
*bucket
, struct vty
*vty
)
596 struct attr
*attr
= bucket
->data
;
598 vty_out(vty
, "attr[%ld] nexthop %s\n", attr
->refcnt
,
599 inet_ntoa(attr
->nexthop
));
600 vty_out(vty
, "\tflags: %" PRIu64
" med: %u local_pref: %u origin: %u weight: %u label: %u\n",
601 attr
->flag
, attr
->med
, attr
->local_pref
, attr
->origin
,
602 attr
->weight
, attr
->label
);
605 void attr_show_all(struct vty
*vty
)
607 hash_iterate(attrhash
, (void (*)(struct hash_bucket
*,
608 void *))attr_show_all_iterator
,
612 static void *bgp_attr_hash_alloc(void *p
)
614 struct attr
*val
= (struct attr
*)p
;
617 attr
= XMALLOC(MTYPE_ATTR
, sizeof(struct attr
));
619 if (val
->encap_subtlvs
) {
620 val
->encap_subtlvs
= NULL
;
623 if (val
->vnc_subtlvs
) {
624 val
->vnc_subtlvs
= NULL
;
631 /* Internet argument attribute. */
632 struct attr
*bgp_attr_intern(struct attr
*attr
)
636 /* Intern referenced strucutre. */
638 if (!attr
->aspath
->refcnt
)
639 attr
->aspath
= aspath_intern(attr
->aspath
);
641 attr
->aspath
->refcnt
++;
643 if (attr
->community
) {
644 if (!attr
->community
->refcnt
)
645 attr
->community
= community_intern(attr
->community
);
647 attr
->community
->refcnt
++;
650 if (attr
->ecommunity
) {
651 if (!attr
->ecommunity
->refcnt
)
652 attr
->ecommunity
= ecommunity_intern(attr
->ecommunity
);
654 attr
->ecommunity
->refcnt
++;
656 if (attr
->lcommunity
) {
657 if (!attr
->lcommunity
->refcnt
)
658 attr
->lcommunity
= lcommunity_intern(attr
->lcommunity
);
660 attr
->lcommunity
->refcnt
++;
663 if (!attr
->cluster
->refcnt
)
664 attr
->cluster
= cluster_intern(attr
->cluster
);
666 attr
->cluster
->refcnt
++;
669 if (!attr
->transit
->refcnt
)
670 attr
->transit
= transit_intern(attr
->transit
);
672 attr
->transit
->refcnt
++;
674 if (attr
->encap_subtlvs
) {
675 if (!attr
->encap_subtlvs
->refcnt
)
676 attr
->encap_subtlvs
= encap_intern(attr
->encap_subtlvs
,
679 attr
->encap_subtlvs
->refcnt
++;
682 if (attr
->vnc_subtlvs
) {
683 if (!attr
->vnc_subtlvs
->refcnt
)
684 attr
->vnc_subtlvs
= encap_intern(attr
->vnc_subtlvs
,
687 attr
->vnc_subtlvs
->refcnt
++;
691 /* At this point, attr only contains intern'd pointers. that means
692 * if we find it in attrhash, it has all the same pointers and we
693 * correctly updated the refcounts on these.
694 * If we don't find it, we need to allocate a one because in all
695 * cases this returns a new reference to a hashed attr, but the input
697 find
= (struct attr
*)hash_get(attrhash
, attr
, bgp_attr_hash_alloc
);
703 /* Make network statement's attribute. */
704 struct attr
*bgp_attr_default_set(struct attr
*attr
, uint8_t origin
)
706 memset(attr
, 0, sizeof(struct attr
));
708 attr
->origin
= origin
;
709 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
710 attr
->aspath
= aspath_empty();
711 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
712 attr
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
714 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
715 attr
->label
= MPLS_INVALID_LABEL
;
716 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
717 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
722 /* Create the attributes for an aggregate */
723 struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, uint8_t origin
,
724 struct aspath
*aspath
,
725 struct community
*community
,
726 struct ecommunity
*ecommunity
,
727 struct lcommunity
*lcommunity
,
728 int as_set
, uint8_t atomic_aggregate
)
733 memset(&attr
, 0, sizeof(struct attr
));
735 /* Origin attribute. */
736 attr
.origin
= origin
;
737 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
739 /* AS path attribute. */
741 attr
.aspath
= aspath_intern(aspath
);
743 attr
.aspath
= aspath_empty();
744 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
746 /* Next hop attribute. */
747 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
750 uint32_t gshut
= COMMUNITY_GSHUT
;
752 /* If we are not shutting down ourselves and we are
753 * aggregating a route that contains the GSHUT community we
754 * need to remove that community when creating the aggregate */
755 if (!bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)
756 && community_include(community
, gshut
)) {
757 community_del_val(community
, &gshut
);
760 attr
.community
= community
;
761 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
765 attr
.ecommunity
= ecommunity
;
766 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
770 attr
.lcommunity
= lcommunity
;
771 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
774 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
775 bgp_attr_add_gshut_community(&attr
);
778 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
779 attr
.label
= MPLS_INVALID_LABEL
;
780 attr
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
781 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
782 if (!as_set
|| atomic_aggregate
)
783 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
784 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
785 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
786 attr
.aggregator_as
= bgp
->confed_id
;
788 attr
.aggregator_as
= bgp
->as
;
789 attr
.aggregator_addr
= bgp
->router_id
;
790 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
791 attr
.label
= MPLS_INVALID_LABEL
;
793 new = bgp_attr_intern(&attr
);
795 aspath_unintern(&new->aspath
);
799 /* Unintern just the sub-components of the attr, but not the attr */
800 void bgp_attr_unintern_sub(struct attr
*attr
)
802 /* aspath refcount shoud be decrement. */
804 aspath_unintern(&attr
->aspath
);
805 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
));
808 community_unintern(&attr
->community
);
809 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
));
811 if (attr
->ecommunity
)
812 ecommunity_unintern(&attr
->ecommunity
);
813 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
));
815 if (attr
->lcommunity
)
816 lcommunity_unintern(&attr
->lcommunity
);
817 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
));
820 cluster_unintern(attr
->cluster
);
821 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
));
824 transit_unintern(attr
->transit
);
826 if (attr
->encap_subtlvs
)
827 encap_unintern(&attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
830 if (attr
->vnc_subtlvs
)
831 encap_unintern(&attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
836 * We have some show commands that let you experimentally
837 * apply a route-map. When we apply the route-map
838 * we are reseting values but not saving them for
839 * posterity via intern'ing( because route-maps don't
840 * do that) but at this point in time we need
841 * to compare the new attr to the old and if the
842 * routemap has changed it we need to, as Snoop Dog says,
843 * Drop it like it's hot
845 void bgp_attr_undup(struct attr
*new, struct attr
*old
)
847 if (new->aspath
!= old
->aspath
)
848 aspath_free(new->aspath
);
850 if (new->community
!= old
->community
)
851 community_free(&new->community
);
853 if (new->ecommunity
!= old
->ecommunity
)
854 ecommunity_free(&new->ecommunity
);
856 if (new->lcommunity
!= old
->lcommunity
)
857 lcommunity_free(&new->lcommunity
);
860 /* Free bgp attribute and aspath. */
861 void bgp_attr_unintern(struct attr
**pattr
)
863 struct attr
*attr
= *pattr
;
867 /* Decrement attribute reference. */
872 /* If reference becomes zero then free attribute object. */
873 if (attr
->refcnt
== 0) {
874 ret
= hash_release(attrhash
, attr
);
876 XFREE(MTYPE_ATTR
, attr
);
880 bgp_attr_unintern_sub(&tmp
);
883 void bgp_attr_flush(struct attr
*attr
)
885 if (attr
->aspath
&& !attr
->aspath
->refcnt
) {
886 aspath_free(attr
->aspath
);
889 if (attr
->community
&& !attr
->community
->refcnt
)
890 community_free(&attr
->community
);
891 if (attr
->ecommunity
&& !attr
->ecommunity
->refcnt
)
892 ecommunity_free(&attr
->ecommunity
);
893 if (attr
->lcommunity
&& !attr
->lcommunity
->refcnt
)
894 lcommunity_free(&attr
->lcommunity
);
895 if (attr
->cluster
&& !attr
->cluster
->refcnt
) {
896 cluster_free(attr
->cluster
);
897 attr
->cluster
= NULL
;
899 if (attr
->transit
&& !attr
->transit
->refcnt
) {
900 transit_free(attr
->transit
);
901 attr
->transit
= NULL
;
903 if (attr
->encap_subtlvs
&& !attr
->encap_subtlvs
->refcnt
) {
904 encap_free(attr
->encap_subtlvs
);
905 attr
->encap_subtlvs
= NULL
;
908 if (attr
->vnc_subtlvs
&& !attr
->vnc_subtlvs
->refcnt
) {
909 encap_free(attr
->vnc_subtlvs
);
910 attr
->vnc_subtlvs
= NULL
;
915 /* Implement draft-scudder-idr-optional-transitive behaviour and
916 * avoid resetting sessions for malformed attributes which are
917 * are partial/optional and hence where the error likely was not
918 * introduced by the sending neighbour.
920 static bgp_attr_parse_ret_t
921 bgp_attr_malformed(struct bgp_attr_parser_args
*args
, uint8_t subcode
,
924 struct peer
*const peer
= args
->peer
;
925 const uint8_t flags
= args
->flags
;
926 /* startp and length must be special-cased, as whether or not to
927 * send the attribute data with the NOTIFY depends on the error,
928 * the caller therefore signals this with the seperate length argument
930 uint8_t *notify_datap
= (length
> 0 ? args
->startp
: NULL
);
932 /* Only relax error handling for eBGP peers */
933 if (peer
->sort
!= BGP_PEER_EBGP
) {
934 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
935 notify_datap
, length
);
936 return BGP_ATTR_PARSE_ERROR
;
939 /* Adjust the stream getp to the end of the attribute, in case we can
940 * still proceed but the caller hasn't read all the attribute.
942 stream_set_getp(BGP_INPUT(peer
),
943 (args
->startp
- STREAM_DATA(BGP_INPUT(peer
)))
946 switch (args
->type
) {
947 /* where an attribute is relatively inconsequential, e.g. it does not
948 * affect route selection, and can be safely ignored, then any such
949 * attributes which are malformed should just be ignored and the route
950 * processed as normal.
952 case BGP_ATTR_AS4_AGGREGATOR
:
953 case BGP_ATTR_AGGREGATOR
:
954 case BGP_ATTR_ATOMIC_AGGREGATE
:
955 return BGP_ATTR_PARSE_PROCEED
;
957 /* Core attributes, particularly ones which may influence route
958 * selection, should always cause session resets
960 case BGP_ATTR_ORIGIN
:
961 case BGP_ATTR_AS_PATH
:
962 case BGP_ATTR_NEXT_HOP
:
963 case BGP_ATTR_MULTI_EXIT_DISC
:
964 case BGP_ATTR_LOCAL_PREF
:
965 case BGP_ATTR_COMMUNITIES
:
966 case BGP_ATTR_ORIGINATOR_ID
:
967 case BGP_ATTR_CLUSTER_LIST
:
968 case BGP_ATTR_MP_REACH_NLRI
:
969 case BGP_ATTR_MP_UNREACH_NLRI
:
970 case BGP_ATTR_EXT_COMMUNITIES
:
971 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
972 notify_datap
, length
);
973 return BGP_ATTR_PARSE_ERROR
;
976 /* Partial optional attributes that are malformed should not cause
977 * the whole session to be reset. Instead treat it as a withdrawal
978 * of the routes, if possible.
980 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)
981 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
982 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
))
983 return BGP_ATTR_PARSE_WITHDRAW
;
985 /* default to reset */
986 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
989 /* Find out what is wrong with the path attribute flag bits and log the error.
990 "Flag bits" here stand for Optional, Transitive and Partial, but not for
991 Extended Length. Checking O/T/P bits at once implies, that the attribute
992 being diagnosed is defined by RFC as either a "well-known" or an "optional,
993 non-transitive" attribute. */
995 bgp_attr_flags_diagnose(struct bgp_attr_parser_args
*args
,
996 uint8_t desired_flags
/* how RFC says it must be */
1000 uint8_t real_flags
= args
->flags
;
1001 const uint8_t attr_code
= args
->type
;
1003 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1004 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1005 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
1006 if (CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1007 != CHECK_FLAG(real_flags
, attr_flag_str
[i
].key
)) {
1008 flog_err(EC_BGP_ATTR_FLAG
,
1009 "%s attribute must%s be flagged as \"%s\"",
1010 lookup_msg(attr_str
, attr_code
, NULL
),
1011 CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1014 attr_flag_str
[i
].str
);
1019 "Strange, %s called for attr %s, but no problem found with flags"
1020 " (real flags 0x%x, desired 0x%x)",
1021 __func__
, lookup_msg(attr_str
, attr_code
, NULL
),
1022 real_flags
, desired_flags
);
1026 /* Required flags for attributes. EXTLEN will be masked off when testing,
1027 * as will PARTIAL for optional+transitive attributes.
1029 const uint8_t attr_flags_values
[] = {
1030 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1031 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1032 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1033 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1034 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1035 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1036 [BGP_ATTR_AGGREGATOR
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1037 [BGP_ATTR_COMMUNITIES
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1038 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1039 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1040 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1041 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1042 [BGP_ATTR_EXT_COMMUNITIES
] =
1043 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1044 [BGP_ATTR_AS4_PATH
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1045 [BGP_ATTR_AS4_AGGREGATOR
] =
1046 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1047 [BGP_ATTR_PMSI_TUNNEL
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1048 [BGP_ATTR_LARGE_COMMUNITIES
] =
1049 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1050 [BGP_ATTR_PREFIX_SID
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1052 static const size_t attr_flags_values_max
= array_size(attr_flags_values
) - 1;
1054 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args
*args
)
1056 uint8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1057 const uint8_t flags
= args
->flags
;
1058 const uint8_t attr_code
= args
->type
;
1060 /* there may be attributes we don't know about */
1061 if (attr_code
> attr_flags_values_max
)
1063 if (attr_flags_values
[attr_code
] == 0)
1066 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1070 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL
, flags
)
1071 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS
, flags
)) {
1074 "%s well-known attributes must have transitive flag set (%x)",
1075 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1079 /* "For well-known attributes and for optional non-transitive
1081 * the Partial bit MUST be set to 0."
1083 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
)) {
1084 if (!CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)) {
1085 flog_err(EC_BGP_ATTR_FLAG
,
1086 "%s well-known attribute "
1087 "must NOT have the partial flag set (%x)",
1088 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1091 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1092 && !CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)) {
1093 flog_err(EC_BGP_ATTR_FLAG
,
1094 "%s optional + transitive attribute "
1095 "must NOT have the partial flag set (%x)",
1096 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1101 /* Optional transitive attributes may go through speakers that don't
1102 * reocgnise them and set the Partial bit.
1104 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1105 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
))
1106 SET_FLAG(mask
, BGP_ATTR_FLAG_PARTIAL
);
1108 if ((flags
& ~mask
) == attr_flags_values
[attr_code
])
1111 bgp_attr_flags_diagnose(args
, attr_flags_values
[attr_code
]);
1115 /* Get origin attribute of the update message. */
1116 static bgp_attr_parse_ret_t
bgp_attr_origin(struct bgp_attr_parser_args
*args
)
1118 struct peer
*const peer
= args
->peer
;
1119 struct attr
*const attr
= args
->attr
;
1120 const bgp_size_t length
= args
->length
;
1122 /* If any recognized attribute has Attribute Length that conflicts
1123 with the expected length (based on the attribute type code), then
1124 the Error Subcode is set to Attribute Length Error. The Data
1125 field contains the erroneous attribute (type, length and
1128 flog_err(EC_BGP_ATTR_LEN
,
1129 "Origin attribute length is not one %d", length
);
1130 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1134 /* Fetch origin attribute. */
1135 attr
->origin
= stream_getc(BGP_INPUT(peer
));
1137 /* If the ORIGIN attribute has an undefined value, then the Error
1138 Subcode is set to Invalid Origin Attribute. The Data field
1139 contains the unrecognized attribute (type, length and value). */
1140 if ((attr
->origin
!= BGP_ORIGIN_IGP
) && (attr
->origin
!= BGP_ORIGIN_EGP
)
1141 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
)) {
1142 flog_err(EC_BGP_ATTR_ORIGIN
,
1143 "Origin attribute value is invalid %d", attr
->origin
);
1144 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1148 /* Set oring attribute flag. */
1149 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
1154 /* Parse AS path information. This function is wrapper of
1156 static int bgp_attr_aspath(struct bgp_attr_parser_args
*args
)
1158 struct attr
*const attr
= args
->attr
;
1159 struct peer
*const peer
= args
->peer
;
1160 const bgp_size_t length
= args
->length
;
1163 * peer with AS4 => will get 4Byte ASnums
1164 * otherwise, will get 16 Bit
1166 attr
->aspath
= aspath_parse(peer
->curr
, length
,
1167 CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
));
1169 /* In case of IBGP, length will be zero. */
1170 if (!attr
->aspath
) {
1171 flog_err(EC_BGP_ATTR_MAL_AS_PATH
,
1172 "Malformed AS path from %s, length is %d", peer
->host
,
1174 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1178 /* Set aspath attribute flag. */
1179 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
1181 return BGP_ATTR_PARSE_PROCEED
;
1184 static bgp_attr_parse_ret_t
bgp_attr_aspath_check(struct peer
*const peer
,
1185 struct attr
*const attr
)
1187 /* These checks were part of bgp_attr_aspath, but with
1188 * as4 we should to check aspath things when
1189 * aspath synthesizing with as4_path has already taken place.
1190 * Otherwise we check ASPATH and use the synthesized thing, and that is
1192 * So do the checks later, i.e. here
1194 struct aspath
*aspath
;
1196 /* Confederation sanity check. */
1197 if ((peer
->sort
== BGP_PEER_CONFED
1198 && !aspath_left_confed_check(attr
->aspath
))
1199 || (peer
->sort
== BGP_PEER_EBGP
1200 && aspath_confed_check(attr
->aspath
))) {
1201 flog_err(EC_BGP_ATTR_MAL_AS_PATH
, "Malformed AS path from %s",
1203 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1204 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1205 return BGP_ATTR_PARSE_ERROR
;
1208 /* First AS check for EBGP. */
1209 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_ENFORCE_FIRST_AS
)) {
1210 if (peer
->sort
== BGP_PEER_EBGP
1211 && !aspath_firstas_check(attr
->aspath
, peer
->as
)) {
1212 flog_err(EC_BGP_ATTR_FIRST_AS
,
1213 "%s incorrect first AS (must be %u)",
1214 peer
->host
, peer
->as
);
1215 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1216 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1217 return BGP_ATTR_PARSE_ERROR
;
1221 /* local-as prepend */
1222 if (peer
->change_local_as
1223 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)) {
1224 aspath
= aspath_dup(attr
->aspath
);
1225 aspath
= aspath_add_seq(aspath
, peer
->change_local_as
);
1226 aspath_unintern(&attr
->aspath
);
1227 attr
->aspath
= aspath_intern(aspath
);
1230 return BGP_ATTR_PARSE_PROCEED
;
1233 /* Parse AS4 path information. This function is another wrapper of
1235 static int bgp_attr_as4_path(struct bgp_attr_parser_args
*args
,
1236 struct aspath
**as4_path
)
1238 struct peer
*const peer
= args
->peer
;
1239 struct attr
*const attr
= args
->attr
;
1240 const bgp_size_t length
= args
->length
;
1242 *as4_path
= aspath_parse(peer
->curr
, length
, 1);
1244 /* In case of IBGP, length will be zero. */
1246 flog_err(EC_BGP_ATTR_MAL_AS_PATH
,
1247 "Malformed AS4 path from %s, length is %d", peer
->host
,
1249 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1253 /* Set aspath attribute flag. */
1254 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
);
1256 return BGP_ATTR_PARSE_PROCEED
;
1259 /* Nexthop attribute. */
1260 static bgp_attr_parse_ret_t
bgp_attr_nexthop(struct bgp_attr_parser_args
*args
)
1262 struct peer
*const peer
= args
->peer
;
1263 struct attr
*const attr
= args
->attr
;
1264 const bgp_size_t length
= args
->length
;
1266 in_addr_t nexthop_h
, nexthop_n
;
1268 /* Check nexthop attribute length. */
1270 flog_err(EC_BGP_ATTR_LEN
,
1271 "Nexthop attribute length isn't four [%d]", length
);
1273 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1277 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1278 attribute must result in a NOTIFICATION message (this is implemented
1280 At the same time, semantically incorrect NEXT_HOP is more likely to
1282 logged locally (this is implemented somewhere else). The UPDATE
1284 gets ignored in any of these cases. */
1285 nexthop_n
= stream_get_ipv4(peer
->curr
);
1286 nexthop_h
= ntohl(nexthop_n
);
1287 if ((IPV4_NET0(nexthop_h
) || IPV4_NET127(nexthop_h
)
1288 || IPV4_CLASS_DE(nexthop_h
))
1291 ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1293 char buf
[INET_ADDRSTRLEN
];
1294 inet_ntop(AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1295 flog_err(EC_BGP_ATTR_MARTIAN_NH
, "Martian nexthop %s", buf
);
1296 return bgp_attr_malformed(
1297 args
, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
, args
->total
);
1300 attr
->nexthop
.s_addr
= nexthop_n
;
1301 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1303 return BGP_ATTR_PARSE_PROCEED
;
1306 /* MED atrribute. */
1307 static bgp_attr_parse_ret_t
bgp_attr_med(struct bgp_attr_parser_args
*args
)
1309 struct peer
*const peer
= args
->peer
;
1310 struct attr
*const attr
= args
->attr
;
1311 const bgp_size_t length
= args
->length
;
1315 flog_err(EC_BGP_ATTR_LEN
,
1316 "MED attribute length isn't four [%d]", length
);
1318 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1322 attr
->med
= stream_getl(peer
->curr
);
1324 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
1326 return BGP_ATTR_PARSE_PROCEED
;
1329 /* Local preference attribute. */
1330 static bgp_attr_parse_ret_t
1331 bgp_attr_local_pref(struct bgp_attr_parser_args
*args
)
1333 struct peer
*const peer
= args
->peer
;
1334 struct attr
*const attr
= args
->attr
;
1335 const bgp_size_t length
= args
->length
;
1339 flog_err(EC_BGP_ATTR_LEN
,
1340 "LOCAL_PREF attribute length isn't 4 [%u]", length
);
1341 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1345 /* If it is contained in an UPDATE message that is received from an
1346 external peer, then this attribute MUST be ignored by the
1347 receiving speaker. */
1348 if (peer
->sort
== BGP_PEER_EBGP
) {
1349 stream_forward_getp(peer
->curr
, length
);
1350 return BGP_ATTR_PARSE_PROCEED
;
1353 attr
->local_pref
= stream_getl(peer
->curr
);
1355 /* Set the local-pref flag. */
1356 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1358 return BGP_ATTR_PARSE_PROCEED
;
1361 /* Atomic aggregate. */
1362 static int bgp_attr_atomic(struct bgp_attr_parser_args
*args
)
1364 struct attr
*const attr
= args
->attr
;
1365 const bgp_size_t length
= args
->length
;
1369 flog_err(EC_BGP_ATTR_LEN
,
1370 "ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1372 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1376 /* Set atomic aggregate flag. */
1377 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
1379 return BGP_ATTR_PARSE_PROCEED
;
1382 /* Aggregator attribute */
1383 static int bgp_attr_aggregator(struct bgp_attr_parser_args
*args
)
1385 struct peer
*const peer
= args
->peer
;
1386 struct attr
*const attr
= args
->attr
;
1387 const bgp_size_t length
= args
->length
;
1391 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1392 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1395 if (length
!= wantedlen
) {
1396 flog_err(EC_BGP_ATTR_LEN
,
1397 "AGGREGATOR attribute length isn't %u [%u]", wantedlen
,
1399 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1403 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1404 attr
->aggregator_as
= stream_getl(peer
->curr
);
1406 attr
->aggregator_as
= stream_getw(peer
->curr
);
1407 attr
->aggregator_addr
.s_addr
= stream_get_ipv4(peer
->curr
);
1409 /* Set atomic aggregate flag. */
1410 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
1412 return BGP_ATTR_PARSE_PROCEED
;
1415 /* New Aggregator attribute */
1416 static bgp_attr_parse_ret_t
1417 bgp_attr_as4_aggregator(struct bgp_attr_parser_args
*args
,
1418 as_t
*as4_aggregator_as
,
1419 struct in_addr
*as4_aggregator_addr
)
1421 struct peer
*const peer
= args
->peer
;
1422 struct attr
*const attr
= args
->attr
;
1423 const bgp_size_t length
= args
->length
;
1426 flog_err(EC_BGP_ATTR_LEN
, "New Aggregator length is not 8 [%d]",
1428 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1432 *as4_aggregator_as
= stream_getl(peer
->curr
);
1433 as4_aggregator_addr
->s_addr
= stream_get_ipv4(peer
->curr
);
1435 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
);
1437 return BGP_ATTR_PARSE_PROCEED
;
1440 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1442 static bgp_attr_parse_ret_t
1443 bgp_attr_munge_as4_attrs(struct peer
*const peer
, struct attr
*const attr
,
1444 struct aspath
*as4_path
, as_t as4_aggregator
,
1445 struct in_addr
*as4_aggregator_addr
)
1447 int ignore_as4_path
= 0;
1448 struct aspath
*newpath
;
1450 if (!attr
->aspath
) {
1451 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1453 * checked that all well-known, mandatory attributes were
1456 * Can only be a problem with peer itself - hard error
1458 return BGP_ATTR_PARSE_ERROR
;
1461 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) {
1462 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1464 * It is worth a warning though, because the peer really
1465 * should not send them
1467 if (BGP_DEBUG(as4
, AS4
)) {
1468 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1469 zlog_debug("[AS4] %s %s AS4_PATH", peer
->host
,
1470 "AS4 capable peer, yet it sent");
1473 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1474 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1476 "AS4 capable peer, yet it sent");
1479 return BGP_ATTR_PARSE_PROCEED
;
1482 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1483 * because that may override AS4_PATH
1485 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
))) {
1486 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
1488 * if the as_number in aggregator is not AS_TRANS,
1489 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1490 * and the Aggregator shall be taken as
1491 * info on the aggregating node, and the AS_PATH
1492 * shall be taken as the AS_PATH
1494 * the Aggregator shall be ignored and the
1495 * AS4_AGGREGATOR shall be taken as the
1496 * Aggregating node and the AS_PATH is to be
1497 * constructed "as in all other cases"
1499 if (attr
->aggregator_as
!= BGP_AS_TRANS
) {
1501 if (BGP_DEBUG(as4
, AS4
))
1503 "[AS4] %s BGP not AS4 capable peer"
1504 " send AGGREGATOR != AS_TRANS and"
1505 " AS4_AGGREGATOR, so ignore"
1506 " AS4_AGGREGATOR and AS4_PATH",
1508 ignore_as4_path
= 1;
1510 /* "New_aggregator shall be taken as aggregator"
1512 attr
->aggregator_as
= as4_aggregator
;
1513 attr
->aggregator_addr
.s_addr
=
1514 as4_aggregator_addr
->s_addr
;
1517 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1518 * That is bogus - but reading the conditions
1519 * we have to handle AS4_AGGREGATOR as if it were
1520 * AGGREGATOR in that case
1522 if (BGP_DEBUG(as4
, AS4
))
1524 "[AS4] %s BGP not AS4 capable peer send"
1525 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1526 " it as if AGGREGATOR with AS_TRANS had been there",
1528 attr
->aggregator_as
= as4_aggregator
;
1529 /* sweep it under the carpet and simulate a "good"
1531 attr
->flag
|= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
));
1535 /* need to reconcile NEW_AS_PATH and AS_PATH */
1536 if (!ignore_as4_path
1537 && (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))) {
1538 newpath
= aspath_reconcile_as4(attr
->aspath
, as4_path
);
1540 return BGP_ATTR_PARSE_ERROR
;
1542 aspath_unintern(&attr
->aspath
);
1543 attr
->aspath
= aspath_intern(newpath
);
1545 return BGP_ATTR_PARSE_PROCEED
;
1548 /* Community attribute. */
1549 static bgp_attr_parse_ret_t
1550 bgp_attr_community(struct bgp_attr_parser_args
*args
)
1552 struct peer
*const peer
= args
->peer
;
1553 struct attr
*const attr
= args
->attr
;
1554 const bgp_size_t length
= args
->length
;
1557 attr
->community
= NULL
;
1558 return BGP_ATTR_PARSE_PROCEED
;
1562 community_parse((uint32_t *)stream_pnt(peer
->curr
), length
);
1564 /* XXX: fix community_parse to use stream API and remove this */
1565 stream_forward_getp(peer
->curr
, length
);
1567 if (!attr
->community
)
1568 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1571 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1573 return BGP_ATTR_PARSE_PROCEED
;
1576 /* Originator ID attribute. */
1577 static bgp_attr_parse_ret_t
1578 bgp_attr_originator_id(struct bgp_attr_parser_args
*args
)
1580 struct peer
*const peer
= args
->peer
;
1581 struct attr
*const attr
= args
->attr
;
1582 const bgp_size_t length
= args
->length
;
1586 flog_err(EC_BGP_ATTR_LEN
, "Bad originator ID length %d",
1589 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1593 attr
->originator_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1595 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
);
1597 return BGP_ATTR_PARSE_PROCEED
;
1600 /* Cluster list attribute. */
1601 static bgp_attr_parse_ret_t
1602 bgp_attr_cluster_list(struct bgp_attr_parser_args
*args
)
1604 struct peer
*const peer
= args
->peer
;
1605 struct attr
*const attr
= args
->attr
;
1606 const bgp_size_t length
= args
->length
;
1610 flog_err(EC_BGP_ATTR_LEN
, "Bad cluster list length %d", length
);
1612 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1617 cluster_parse((struct in_addr
*)stream_pnt(peer
->curr
), length
);
1619 /* XXX: Fix cluster_parse to use stream API and then remove this */
1620 stream_forward_getp(peer
->curr
, length
);
1622 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
);
1624 return BGP_ATTR_PARSE_PROCEED
;
1627 /* Multiprotocol reachability information parse. */
1628 int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
1629 struct bgp_nlri
*mp_update
)
1633 iana_safi_t pkt_safi
;
1635 bgp_size_t nlri_len
;
1638 struct peer
*const peer
= args
->peer
;
1639 struct attr
*const attr
= args
->attr
;
1640 const bgp_size_t length
= args
->length
;
1642 /* Set end of packet. */
1643 s
= BGP_INPUT(peer
);
1644 start
= stream_get_getp(s
);
1646 /* safe to read statically sized header? */
1647 #define BGP_MP_REACH_MIN_SIZE 5
1648 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1649 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
)) {
1650 zlog_info("%s: %s sent invalid length, %lu", __func__
,
1651 peer
->host
, (unsigned long)length
);
1652 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1655 /* Load AFI, SAFI. */
1656 pkt_afi
= stream_getw(s
);
1657 pkt_safi
= stream_getc(s
);
1659 /* Convert AFI, SAFI to internal values, check. */
1660 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1661 /* Log if AFI or SAFI is unrecognized. This is not an error
1663 * the attribute is otherwise malformed.
1665 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1667 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1668 peer
->host
, pkt_afi
, pkt_safi
);
1669 return BGP_ATTR_PARSE_ERROR
;
1672 /* Get nexthop length. */
1673 attr
->mp_nexthop_len
= stream_getc(s
);
1675 if (LEN_LEFT
< attr
->mp_nexthop_len
) {
1677 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1678 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1679 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1682 /* Nexthop length check. */
1683 switch (attr
->mp_nexthop_len
) {
1685 if (safi
!= SAFI_FLOWSPEC
) {
1686 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1687 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1688 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1691 case BGP_ATTR_NHLEN_VPNV4
:
1692 stream_getl(s
); /* RD high */
1693 stream_getl(s
); /* RD low */
1695 * NOTE: intentional fall through
1696 * - for consistency in rx processing
1698 * The following comment is to signal GCC this intention
1699 * and suppress the warning
1702 case BGP_ATTR_NHLEN_IPV4
:
1703 stream_get(&attr
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1704 /* Probably needed for RFC 2283 */
1705 if (attr
->nexthop
.s_addr
== 0)
1706 memcpy(&attr
->nexthop
.s_addr
,
1707 &attr
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1709 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1710 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1711 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1712 stream_getl(s
); /* RD high */
1713 stream_getl(s
); /* RD low */
1715 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1716 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
)) {
1717 if (!peer
->nexthop
.ifp
) {
1718 zlog_warn("%s: Received a V6/VPNV6 Global attribute but address is a V6 LL and we have no peer interface information, withdrawing",
1720 return BGP_ATTR_PARSE_WITHDRAW
;
1722 attr
->nh_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1725 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1726 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1727 if (attr
->mp_nexthop_len
1728 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1729 stream_getl(s
); /* RD high */
1730 stream_getl(s
); /* RD low */
1732 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1733 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
)) {
1734 if (!peer
->nexthop
.ifp
) {
1735 zlog_warn("%s: Received V6/VPNV6 Global and LL attribute but global address is a V6 LL and we have no peer interface information, withdrawing",
1737 return BGP_ATTR_PARSE_WITHDRAW
;
1739 attr
->nh_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1741 if (attr
->mp_nexthop_len
1742 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1743 stream_getl(s
); /* RD high */
1744 stream_getl(s
); /* RD low */
1746 stream_get(&attr
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1747 if (!IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
)) {
1748 char buf1
[INET6_ADDRSTRLEN
];
1749 char buf2
[INET6_ADDRSTRLEN
];
1751 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1753 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1756 &attr
->mp_nexthop_global
,
1757 buf1
, INET6_ADDRSTRLEN
),
1759 &attr
->mp_nexthop_local
, buf2
,
1762 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1764 if (!peer
->nexthop
.ifp
) {
1765 zlog_warn("%s: Received a V6 LL nexthop and we have no peer interface information, withdrawing",
1767 return BGP_ATTR_PARSE_WITHDRAW
;
1769 attr
->nh_lla_ifindex
= peer
->nexthop
.ifp
->ifindex
;
1772 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1773 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1774 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1778 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__
,
1780 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1785 if ((val
= stream_getc(s
)))
1787 EC_BGP_DEFUNCT_SNPA_LEN
,
1788 "%s sent non-zero value, %u, for defunct SNPA-length field",
1792 /* must have nrli_len, what is left of the attribute */
1793 nlri_len
= LEN_LEFT
;
1794 if (nlri_len
> STREAM_READABLE(s
)) {
1795 zlog_info("%s: (%s) Failed to read NLRI", __func__
, peer
->host
);
1796 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1800 zlog_info("%s: (%s) No Reachability, Treating as a EOR marker",
1801 __func__
, peer
->host
);
1803 mp_update
->afi
= afi
;
1804 mp_update
->safi
= safi
;
1805 return BGP_ATTR_PARSE_EOR
;
1808 mp_update
->afi
= afi
;
1809 mp_update
->safi
= safi
;
1810 mp_update
->nlri
= stream_pnt(s
);
1811 mp_update
->length
= nlri_len
;
1813 stream_forward_getp(s
, nlri_len
);
1815 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
);
1817 return BGP_ATTR_PARSE_PROCEED
;
1821 /* Multiprotocol unreachable parse */
1822 int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
1823 struct bgp_nlri
*mp_withdraw
)
1828 iana_safi_t pkt_safi
;
1830 uint16_t withdraw_len
;
1831 struct peer
*const peer
= args
->peer
;
1832 struct attr
*const attr
= args
->attr
;
1833 const bgp_size_t length
= args
->length
;
1837 #define BGP_MP_UNREACH_MIN_SIZE 3
1838 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1839 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1841 pkt_afi
= stream_getw(s
);
1842 pkt_safi
= stream_getc(s
);
1844 /* Convert AFI, SAFI to internal values, check. */
1845 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1846 /* Log if AFI or SAFI is unrecognized. This is not an error
1848 * the attribute is otherwise malformed.
1850 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1852 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1853 peer
->host
, pkt_afi
, pkt_safi
);
1854 return BGP_ATTR_PARSE_ERROR
;
1857 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1859 mp_withdraw
->afi
= afi
;
1860 mp_withdraw
->safi
= safi
;
1861 mp_withdraw
->nlri
= stream_pnt(s
);
1862 mp_withdraw
->length
= withdraw_len
;
1864 stream_forward_getp(s
, withdraw_len
);
1866 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
);
1868 return BGP_ATTR_PARSE_PROCEED
;
1871 /* Large Community attribute. */
1872 static bgp_attr_parse_ret_t
1873 bgp_attr_large_community(struct bgp_attr_parser_args
*args
)
1875 struct peer
*const peer
= args
->peer
;
1876 struct attr
*const attr
= args
->attr
;
1877 const bgp_size_t length
= args
->length
;
1880 * Large community follows new attribute format.
1883 attr
->lcommunity
= NULL
;
1884 /* Empty extcomm doesn't seem to be invalid per se */
1885 return BGP_ATTR_PARSE_PROCEED
;
1889 lcommunity_parse((uint8_t *)stream_pnt(peer
->curr
), length
);
1890 /* XXX: fix ecommunity_parse to use stream API */
1891 stream_forward_getp(peer
->curr
, length
);
1893 if (!attr
->lcommunity
)
1894 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1897 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
1899 return BGP_ATTR_PARSE_PROCEED
;
1902 /* Extended Community attribute. */
1903 static bgp_attr_parse_ret_t
1904 bgp_attr_ext_communities(struct bgp_attr_parser_args
*args
)
1906 struct peer
*const peer
= args
->peer
;
1907 struct attr
*const attr
= args
->attr
;
1908 const bgp_size_t length
= args
->length
;
1912 attr
->ecommunity
= NULL
;
1913 /* Empty extcomm doesn't seem to be invalid per se */
1914 return BGP_ATTR_PARSE_PROCEED
;
1918 ecommunity_parse((uint8_t *)stream_pnt(peer
->curr
), length
);
1919 /* XXX: fix ecommunity_parse to use stream API */
1920 stream_forward_getp(peer
->curr
, length
);
1922 if (!attr
->ecommunity
)
1923 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1926 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
1928 /* Extract MAC mobility sequence number, if any. */
1929 attr
->mm_seqnum
= bgp_attr_mac_mobility_seqnum(attr
, &sticky
);
1930 attr
->sticky
= sticky
;
1932 /* Check if this is a Gateway MAC-IP advertisement */
1933 attr
->default_gw
= bgp_attr_default_gw(attr
);
1935 /* Handle scenario where router flag ecommunity is not
1936 * set but default gw ext community is present.
1937 * Use default gateway, set and propogate R-bit.
1939 if (attr
->default_gw
)
1940 attr
->router_flag
= 1;
1942 /* Check EVPN Neighbor advertisement flags, R-bit */
1943 bgp_attr_evpn_na_flag(attr
, &attr
->router_flag
);
1945 /* Extract the Rmac, if any */
1946 if (bgp_attr_rmac(attr
, &attr
->rmac
)) {
1947 if (bgp_debug_update(peer
, NULL
, NULL
, 1) &&
1948 bgp_mac_exist(&attr
->rmac
)) {
1949 char buf1
[ETHER_ADDR_STRLEN
];
1951 zlog_debug("%s: router mac %s is self mac",
1953 prefix_mac2str(&attr
->rmac
, buf1
,
1959 return BGP_ATTR_PARSE_PROCEED
;
1962 /* Parse Tunnel Encap attribute in an UPDATE */
1963 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
1964 bgp_size_t length
, /* IN: attr's length field */
1965 struct attr
*attr
, /* IN: caller already allocated */
1966 uint8_t flag
, /* IN: attr's flags field */
1970 uint16_t tunneltype
= 0;
1972 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1974 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1975 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
1977 "Tunnel Encap attribute flag isn't optional and transitive %d",
1979 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
1980 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1985 if (BGP_ATTR_ENCAP
== type
) {
1986 /* read outer TLV type and length */
1987 uint16_t tlv_length
;
1991 "Tunnel Encap attribute not long enough to contain outer T,L");
1992 bgp_notify_send_with_data(
1993 peer
, BGP_NOTIFY_UPDATE_ERR
,
1994 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1997 tunneltype
= stream_getw(BGP_INPUT(peer
));
1998 tlv_length
= stream_getw(BGP_INPUT(peer
));
2001 if (tlv_length
!= length
) {
2002 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
2003 tlv_length
, length
);
2007 while (length
>= 4) {
2008 uint16_t subtype
= 0;
2009 uint16_t sublength
= 0;
2010 struct bgp_attr_encap_subtlv
*tlv
;
2012 if (BGP_ATTR_ENCAP
== type
) {
2013 subtype
= stream_getc(BGP_INPUT(peer
));
2014 sublength
= stream_getc(BGP_INPUT(peer
));
2018 subtype
= stream_getw(BGP_INPUT(peer
));
2019 sublength
= stream_getw(BGP_INPUT(peer
));
2024 if (sublength
> length
) {
2026 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
2028 bgp_notify_send_with_data(
2029 peer
, BGP_NOTIFY_UPDATE_ERR
,
2030 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
2034 /* alloc and copy sub-tlv */
2035 /* TBD make sure these are freed when attributes are released */
2036 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
2037 sizeof(struct bgp_attr_encap_subtlv
) + sublength
);
2038 tlv
->type
= subtype
;
2039 tlv
->length
= sublength
;
2040 stream_get(tlv
->value
, peer
->curr
, sublength
);
2041 length
-= sublength
;
2043 /* attach tlv to encap chain */
2044 if (BGP_ATTR_ENCAP
== type
) {
2045 struct bgp_attr_encap_subtlv
*stlv_last
;
2046 for (stlv_last
= attr
->encap_subtlvs
;
2047 stlv_last
&& stlv_last
->next
;
2048 stlv_last
= stlv_last
->next
)
2051 stlv_last
->next
= tlv
;
2053 attr
->encap_subtlvs
= tlv
;
2057 struct bgp_attr_encap_subtlv
*stlv_last
;
2058 for (stlv_last
= attr
->vnc_subtlvs
;
2059 stlv_last
&& stlv_last
->next
;
2060 stlv_last
= stlv_last
->next
)
2063 stlv_last
->next
= tlv
;
2065 attr
->vnc_subtlvs
= tlv
;
2071 if (BGP_ATTR_ENCAP
== type
) {
2072 attr
->encap_tunneltype
= tunneltype
;
2076 /* spurious leftover data */
2078 "Tunnel Encap attribute length is bad: %d leftover octets",
2080 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2081 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2090 * Read an individual SID value returning how much data we have read
2091 * Returns 0 if there was an error that needs to be passed up the stack
2093 static bgp_attr_parse_ret_t
bgp_attr_psid_sub(int32_t type
,
2095 struct bgp_attr_parser_args
*args
,
2096 struct bgp_nlri
*mp_update
)
2098 struct peer
*const peer
= args
->peer
;
2099 struct attr
*const attr
= args
->attr
;
2100 uint32_t label_index
;
2101 struct in6_addr ipv6_sid
;
2103 uint32_t srgb_range
;
2106 if (type
== BGP_PREFIX_SID_LABEL_INDEX
) {
2107 if (length
!= BGP_PREFIX_SID_LABEL_INDEX_LENGTH
) {
2110 "Prefix SID label index length is %d instead of %d",
2111 length
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
2112 return bgp_attr_malformed(args
,
2113 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2117 /* Ignore flags and reserved */
2118 stream_getc(peer
->curr
);
2119 stream_getw(peer
->curr
);
2121 /* Fetch the label index and see if it is valid. */
2122 label_index
= stream_getl(peer
->curr
);
2123 if (label_index
== BGP_INVALID_LABEL_INDEX
)
2124 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2127 /* Store label index; subsequently, we'll check on
2129 attr
->label_index
= label_index
;
2132 * Ignore the Label index attribute unless received for
2136 if (!mp_update
->length
2137 || mp_update
->safi
!= SAFI_LABELED_UNICAST
)
2138 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
2141 /* Placeholder code for the IPv6 SID type */
2142 else if (type
== BGP_PREFIX_SID_IPV6
) {
2143 if (length
!= BGP_PREFIX_SID_IPV6_LENGTH
) {
2144 flog_err(EC_BGP_ATTR_LEN
,
2145 "Prefix SID IPv6 length is %d instead of %d",
2146 length
, BGP_PREFIX_SID_IPV6_LENGTH
);
2147 return bgp_attr_malformed(args
,
2148 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2152 /* Ignore reserved */
2153 stream_getc(peer
->curr
);
2154 stream_getw(peer
->curr
);
2156 stream_get(&ipv6_sid
, peer
->curr
, 16);
2159 /* Placeholder code for the Originator SRGB type */
2160 else if (type
== BGP_PREFIX_SID_ORIGINATOR_SRGB
) {
2162 stream_getw(peer
->curr
);
2166 if (length
% BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
) {
2169 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2170 length
, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
);
2171 return bgp_attr_malformed(
2172 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2176 srgb_count
= length
/ BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
;
2178 for (int i
= 0; i
< srgb_count
; i
++) {
2179 stream_get(&srgb_base
, peer
->curr
, 3);
2180 stream_get(&srgb_range
, peer
->curr
, 3);
2184 return BGP_ATTR_PARSE_PROCEED
;
2187 /* Prefix SID attribute
2188 * draft-ietf-idr-bgp-prefix-sid-05
2190 bgp_attr_parse_ret_t
2191 bgp_attr_prefix_sid(int32_t tlength
, struct bgp_attr_parser_args
*args
,
2192 struct bgp_nlri
*mp_update
)
2194 struct peer
*const peer
= args
->peer
;
2195 struct attr
*const attr
= args
->attr
;
2196 bgp_attr_parse_ret_t ret
;
2198 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
2201 int32_t type
, length
;
2203 type
= stream_getc(peer
->curr
);
2204 length
= stream_getw(peer
->curr
);
2206 ret
= bgp_attr_psid_sub(type
, length
, args
, mp_update
);
2208 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2211 * Subtract length + the T and the L
2212 * since length is the Vector portion
2214 tlength
-= length
+ 3;
2219 "Prefix SID internal length %d causes us to read beyond the total Prefix SID length",
2221 return bgp_attr_malformed(args
,
2222 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2227 return BGP_ATTR_PARSE_PROCEED
;
2230 /* PMSI tunnel attribute (RFC 6514)
2231 * Basic validation checks done here.
2233 static bgp_attr_parse_ret_t
2234 bgp_attr_pmsi_tunnel(struct bgp_attr_parser_args
*args
)
2236 struct peer
*const peer
= args
->peer
;
2237 struct attr
*const attr
= args
->attr
;
2238 const bgp_size_t length
= args
->length
;
2240 int attr_parse_len
= 2 + BGP_LABEL_BYTES
;
2242 /* Verify that the receiver is expecting "ingress replication" as we
2243 * can only support that.
2245 if (length
< attr_parse_len
) {
2246 flog_err(EC_BGP_ATTR_LEN
, "Bad PMSI tunnel attribute length %d",
2248 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2251 stream_getc(peer
->curr
); /* Flags */
2252 tnl_type
= stream_getc(peer
->curr
);
2253 if (tnl_type
> PMSI_TNLTYPE_MAX
) {
2254 flog_err(EC_BGP_ATTR_PMSI_TYPE
,
2255 "Invalid PMSI tunnel attribute type %d", tnl_type
);
2256 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2259 if (tnl_type
== PMSI_TNLTYPE_INGR_REPL
) {
2261 flog_err(EC_BGP_ATTR_PMSI_LEN
,
2262 "Bad PMSI tunnel attribute length %d for IR",
2264 return bgp_attr_malformed(
2265 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2270 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
2271 attr
->pmsi_tnl_type
= tnl_type
;
2272 stream_get(&attr
->label
, peer
->curr
, BGP_LABEL_BYTES
);
2274 /* Forward read pointer of input stream. */
2275 stream_forward_getp(peer
->curr
, length
- attr_parse_len
);
2277 return BGP_ATTR_PARSE_PROCEED
;
2280 /* BGP unknown attribute treatment. */
2281 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2283 bgp_size_t total
= args
->total
;
2284 struct transit
*transit
;
2285 struct peer
*const peer
= args
->peer
;
2286 struct attr
*const attr
= args
->attr
;
2287 uint8_t *const startp
= args
->startp
;
2288 const uint8_t type
= args
->type
;
2289 const uint8_t flag
= args
->flags
;
2290 const bgp_size_t length
= args
->length
;
2292 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2294 "%s Unknown attribute is received (type %d, length %d)",
2295 peer
->host
, type
, length
);
2297 /* Forward read pointer of input stream. */
2298 stream_forward_getp(peer
->curr
, length
);
2300 /* If any of the mandatory well-known attributes are not recognized,
2301 then the Error Subcode is set to Unrecognized Well-known
2302 Attribute. The Data field contains the unrecognized attribute
2303 (type, length and value). */
2304 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2305 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2309 /* Unrecognized non-transitive optional attributes must be quietly
2310 ignored and not passed along to other BGP peers. */
2311 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2312 return BGP_ATTR_PARSE_PROCEED
;
2314 /* If a path with recognized transitive optional attribute is
2315 accepted and passed along to other BGP peers and the Partial bit
2316 in the Attribute Flags octet is set to 1 by some previous AS, it
2317 is not set back to 0 by the current AS. */
2318 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2320 /* Store transitive attribute to the end of attr->transit. */
2322 attr
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2324 transit
= attr
->transit
;
2327 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2328 transit
->length
+ total
);
2330 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2332 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2333 transit
->length
+= total
;
2335 return BGP_ATTR_PARSE_PROCEED
;
2338 /* Well-known attribute check. */
2339 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2343 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2345 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2346 return BGP_ATTR_PARSE_PROCEED
;
2348 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2349 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2350 are present, it should. Check for any other attribute being present
2353 if ((!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)) &&
2354 CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))))
2355 return BGP_ATTR_PARSE_PROCEED
;
2357 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2358 type
= BGP_ATTR_ORIGIN
;
2360 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2361 type
= BGP_ATTR_AS_PATH
;
2363 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2365 * NLRI is empty. We can't easily check NLRI empty here though.
2367 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2368 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2369 type
= BGP_ATTR_NEXT_HOP
;
2371 if (peer
->sort
== BGP_PEER_IBGP
2372 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2373 type
= BGP_ATTR_LOCAL_PREF
;
2376 flog_warn(EC_BGP_MISSING_ATTRIBUTE
,
2377 "%s Missing well-known attribute %s.", peer
->host
,
2378 lookup_msg(attr_str
, type
, NULL
));
2379 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2380 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2382 return BGP_ATTR_PARSE_ERROR
;
2384 return BGP_ATTR_PARSE_PROCEED
;
2387 /* Read attribute of update packet. This function is called from
2388 bgp_update_receive() in bgp_packet.c. */
2389 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2390 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2391 struct bgp_nlri
*mp_withdraw
)
2393 bgp_attr_parse_ret_t ret
;
2397 uint8_t *startp
, *endp
;
2399 uint8_t seen
[BGP_ATTR_BITMAP_SIZE
];
2400 /* we need the as4_path only until we have synthesized the as_path with
2402 /* same goes for as4_aggregator */
2403 struct aspath
*as4_path
= NULL
;
2404 as_t as4_aggregator
= 0;
2405 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2407 /* Initialize bitmap. */
2408 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2410 /* End pointer of BGP attribute. */
2411 endp
= BGP_INPUT_PNT(peer
) + size
;
2413 /* Get attributes to the end of attribute length. */
2414 while (BGP_INPUT_PNT(peer
) < endp
) {
2415 /* Check remaining length check.*/
2416 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2417 /* XXX warning: long int format, int arg (arg 5) */
2419 EC_BGP_ATTRIBUTE_TOO_SMALL
,
2420 "%s: error BGP attribute length %lu is smaller than min len",
2422 (unsigned long)(endp
2423 - stream_pnt(BGP_INPUT(peer
))));
2425 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2426 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2427 return BGP_ATTR_PARSE_ERROR
;
2430 /* Fetch attribute flag and type. */
2431 startp
= BGP_INPUT_PNT(peer
);
2432 /* "The lower-order four bits of the Attribute Flags octet are
2433 unused. They MUST be zero when sent and MUST be ignored when
2435 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2436 type
= stream_getc(BGP_INPUT(peer
));
2438 /* Check whether Extended-Length applies and is in bounds */
2439 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2440 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2442 EC_BGP_EXT_ATTRIBUTE_TOO_SMALL
,
2443 "%s: Extended length set, but just %lu bytes of attr header",
2445 (unsigned long)(endp
2446 - stream_pnt(BGP_INPUT(peer
))));
2448 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2449 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2450 return BGP_ATTR_PARSE_ERROR
;
2453 /* Check extended attribue length bit. */
2454 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2455 length
= stream_getw(BGP_INPUT(peer
));
2457 length
= stream_getc(BGP_INPUT(peer
));
2459 /* If any attribute appears more than once in the UPDATE
2460 message, then the Error Subcode is set to Malformed Attribute
2463 if (CHECK_BITMAP(seen
, type
)) {
2465 EC_BGP_ATTRIBUTE_REPEATED
,
2466 "%s: error BGP attribute type %d appears twice in a message",
2469 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2470 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2471 return BGP_ATTR_PARSE_ERROR
;
2474 /* Set type to bitmap to check duplicate attribute. `type' is
2475 unsigned char so it never overflow bitmap range. */
2477 SET_BITMAP(seen
, type
);
2479 /* Overflow check. */
2480 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2482 if (attr_endp
> endp
) {
2484 EC_BGP_ATTRIBUTE_TOO_LARGE
,
2485 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2486 peer
->host
, type
, length
, size
, attr_endp
,
2490 * If any recognized attribute has an Attribute
2491 * Length that conflicts with the expected length
2492 * (based on the attribute type code), then the
2493 * Error Subcode MUST be set to Attribute Length
2494 * Error. The Data field MUST contain the erroneous
2495 * attribute (type, length, and value).
2497 * We do not currently have a good way to determine the
2498 * length of the attribute independent of the length
2499 * received in the message. Instead we send the
2500 * minimum between the amount of data we have and the
2501 * amount specified by the attribute length field.
2503 * Instead of directly passing in the packet buffer and
2504 * offset we use the stream_get* functions to read into
2505 * a stack buffer, since they perform bounds checking
2506 * and we are working with untrusted data.
2508 unsigned char ndata
[BGP_MAX_PACKET_SIZE
];
2509 memset(ndata
, 0x00, sizeof(ndata
));
2511 CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 2 : 1;
2512 /* Rewind to end of flag field */
2513 stream_forward_getp(BGP_INPUT(peer
), -(1 + lfl
));
2515 stream_get(&ndata
[0], BGP_INPUT(peer
), 1);
2517 stream_get(&ndata
[1], BGP_INPUT(peer
), lfl
);
2519 size_t atl
= attr_endp
- startp
;
2520 size_t ndl
= MIN(atl
, STREAM_READABLE(BGP_INPUT(peer
)));
2521 stream_get(&ndata
[lfl
+ 1], BGP_INPUT(peer
), ndl
);
2523 bgp_notify_send_with_data(
2524 peer
, BGP_NOTIFY_UPDATE_ERR
,
2525 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, ndata
,
2528 return BGP_ATTR_PARSE_ERROR
;
2531 struct bgp_attr_parser_args attr_args
= {
2538 .total
= attr_endp
- startp
,
2542 /* If any recognized attribute has Attribute Flags that conflict
2543 with the Attribute Type Code, then the Error Subcode is set
2545 Attribute Flags Error. The Data field contains the erroneous
2546 attribute (type, length and value). */
2547 if (bgp_attr_flag_invalid(&attr_args
)) {
2548 ret
= bgp_attr_malformed(
2549 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2551 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2556 /* OK check attribute and store it's value. */
2558 case BGP_ATTR_ORIGIN
:
2559 ret
= bgp_attr_origin(&attr_args
);
2561 case BGP_ATTR_AS_PATH
:
2562 ret
= bgp_attr_aspath(&attr_args
);
2564 case BGP_ATTR_AS4_PATH
:
2565 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2567 case BGP_ATTR_NEXT_HOP
:
2568 ret
= bgp_attr_nexthop(&attr_args
);
2570 case BGP_ATTR_MULTI_EXIT_DISC
:
2571 ret
= bgp_attr_med(&attr_args
);
2573 case BGP_ATTR_LOCAL_PREF
:
2574 ret
= bgp_attr_local_pref(&attr_args
);
2576 case BGP_ATTR_ATOMIC_AGGREGATE
:
2577 ret
= bgp_attr_atomic(&attr_args
);
2579 case BGP_ATTR_AGGREGATOR
:
2580 ret
= bgp_attr_aggregator(&attr_args
);
2582 case BGP_ATTR_AS4_AGGREGATOR
:
2583 ret
= bgp_attr_as4_aggregator(&attr_args
,
2585 &as4_aggregator_addr
);
2587 case BGP_ATTR_COMMUNITIES
:
2588 ret
= bgp_attr_community(&attr_args
);
2590 case BGP_ATTR_LARGE_COMMUNITIES
:
2591 ret
= bgp_attr_large_community(&attr_args
);
2593 case BGP_ATTR_ORIGINATOR_ID
:
2594 ret
= bgp_attr_originator_id(&attr_args
);
2596 case BGP_ATTR_CLUSTER_LIST
:
2597 ret
= bgp_attr_cluster_list(&attr_args
);
2599 case BGP_ATTR_MP_REACH_NLRI
:
2600 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2602 case BGP_ATTR_MP_UNREACH_NLRI
:
2603 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2605 case BGP_ATTR_EXT_COMMUNITIES
:
2606 ret
= bgp_attr_ext_communities(&attr_args
);
2608 #if ENABLE_BGP_VNC_ATTR
2611 case BGP_ATTR_ENCAP
:
2612 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2615 case BGP_ATTR_PREFIX_SID
:
2616 ret
= bgp_attr_prefix_sid(length
,
2617 &attr_args
, mp_update
);
2619 case BGP_ATTR_PMSI_TUNNEL
:
2620 ret
= bgp_attr_pmsi_tunnel(&attr_args
);
2623 ret
= bgp_attr_unknown(&attr_args
);
2627 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2628 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2629 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2630 ret
= BGP_ATTR_PARSE_ERROR
;
2633 if (ret
== BGP_ATTR_PARSE_EOR
) {
2635 aspath_unintern(&as4_path
);
2639 /* If hard error occurred immediately return to the caller. */
2640 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2641 flog_warn(EC_BGP_ATTRIBUTE_PARSE_ERROR
,
2642 "%s: Attribute %s, parse error", peer
->host
,
2643 lookup_msg(attr_str
, type
, NULL
));
2645 aspath_unintern(&as4_path
);
2648 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2651 EC_BGP_ATTRIBUTE_PARSE_WITHDRAW
,
2652 "%s: Attribute %s, parse error - treating as withdrawal",
2653 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2655 aspath_unintern(&as4_path
);
2659 /* Check the fetched length. */
2660 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2661 flog_warn(EC_BGP_ATTRIBUTE_FETCH_ERROR
,
2662 "%s: BGP attribute %s, fetch error",
2663 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2664 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2665 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2667 aspath_unintern(&as4_path
);
2668 return BGP_ATTR_PARSE_ERROR
;
2672 /* Check final read pointer is same as end pointer. */
2673 if (BGP_INPUT_PNT(peer
) != endp
) {
2674 flog_warn(EC_BGP_ATTRIBUTES_MISMATCH
,
2675 "%s: BGP attribute %s, length mismatch", peer
->host
,
2676 lookup_msg(attr_str
, type
, NULL
));
2677 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2678 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2680 aspath_unintern(&as4_path
);
2681 return BGP_ATTR_PARSE_ERROR
;
2684 /* Check all mandatory well-known attributes are present */
2685 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2687 aspath_unintern(&as4_path
);
2692 * At this place we can see whether we got AS4_PATH and/or
2693 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2694 * We can not do this before we've read all attributes because
2695 * the as4 handling does not say whether AS4_PATH has to be sent
2696 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2697 * in relationship to AGGREGATOR.
2698 * So, to be defensive, we are not relying on any order and read
2699 * all attributes first, including these 32bit ones, and now,
2700 * afterwards, we look what and if something is to be done for as4.
2702 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2705 /* actually... this doesn't ever return failure currently, but
2706 * better safe than sorry */
2707 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2708 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2709 &as4_aggregator_addr
)) {
2710 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2711 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2713 aspath_unintern(&as4_path
);
2714 return BGP_ATTR_PARSE_ERROR
;
2717 /* At this stage, we have done all fiddling with as4, and the
2718 * resulting info is in attr->aggregator resp. attr->aspath
2719 * so we can chuck as4_aggregator and as4_path alltogether in
2720 * order to save memory
2723 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2724 /* The flag that we got this is still there, but that does not
2729 * The "rest" of the code does nothing with as4_aggregator.
2730 * there is no memory attached specifically which is not part
2732 * so ignoring just means do nothing.
2735 * Finally do the checks on the aspath we did not do yet
2736 * because we waited for a potentially synthesized aspath.
2738 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2739 ret
= bgp_attr_aspath_check(peer
, attr
);
2740 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2743 /* Finally intern unknown attribute. */
2745 attr
->transit
= transit_intern(attr
->transit
);
2746 if (attr
->encap_subtlvs
)
2747 attr
->encap_subtlvs
=
2748 encap_intern(attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2750 if (attr
->vnc_subtlvs
)
2752 encap_intern(attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2755 return BGP_ATTR_PARSE_PROCEED
;
2758 size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
, afi_t afi
,
2759 safi_t safi
, struct bpacket_attr_vec_arr
*vecarr
,
2764 iana_safi_t pkt_safi
;
2767 /* Set extended bit always to encode the attribute length as 2 bytes */
2768 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2769 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2770 sizep
= stream_get_endp(s
);
2771 stream_putw(s
, 0); /* Marker: Attribute length. */
2774 /* Convert AFI, SAFI to values for packet. */
2775 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2777 stream_putw(s
, pkt_afi
); /* AFI */
2778 stream_putc(s
, pkt_safi
); /* SAFI */
2782 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
))
2783 nh_afi
= peer_cap_enhe(peer
, afi
, safi
) ? AFI_IP6
: AFI_IP
;
2785 nh_afi
= BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->mp_nexthop_len
);
2788 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2793 case SAFI_MULTICAST
:
2794 case SAFI_LABELED_UNICAST
:
2796 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2800 stream_putl(s
, 0); /* RD = 0, per RFC */
2802 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2807 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2810 stream_putc(s
, 0); /* no nexthop for flowspec */
2818 case SAFI_MULTICAST
:
2819 case SAFI_LABELED_UNICAST
:
2821 if (attr
->mp_nexthop_len
2822 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2824 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
);
2825 stream_put(s
, &attr
->mp_nexthop_global
,
2827 stream_put(s
, &attr
->mp_nexthop_local
,
2830 stream_putc(s
, IPV6_MAX_BYTELEN
);
2831 stream_put(s
, &attr
->mp_nexthop_global
,
2835 case SAFI_MPLS_VPN
: {
2836 if (attr
->mp_nexthop_len
2837 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2839 stream_putl(s
, 0); /* RD = 0, per RFC */
2841 stream_put(s
, &attr
->mp_nexthop_global
,
2843 } else if (attr
->mp_nexthop_len
2844 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2846 stream_putl(s
, 0); /* RD = 0, per RFC */
2848 stream_put(s
, &attr
->mp_nexthop_global
,
2850 stream_putl(s
, 0); /* RD = 0, per RFC */
2852 stream_put(s
, &attr
->mp_nexthop_local
,
2857 stream_putc(s
, IPV6_MAX_BYTELEN
);
2858 stream_put(s
, &attr
->mp_nexthop_global
,
2862 stream_putc(s
, 0); /* no nexthop for flowspec */
2868 if (safi
!= SAFI_FLOWSPEC
)
2870 EC_BGP_ATTR_NH_SEND_LEN
,
2871 "Bad nexthop when sending to %s, AFI %u SAFI %u nhlen %d",
2872 peer
->host
, afi
, safi
, attr
->mp_nexthop_len
);
2881 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2882 struct prefix
*p
, struct prefix_rd
*prd
,
2883 mpls_label_t
*label
, uint32_t num_labels
,
2884 int addpath_encode
, uint32_t addpath_tx_id
,
2887 if (safi
== SAFI_MPLS_VPN
) {
2889 stream_putl(s
, addpath_tx_id
);
2890 /* Label, RD, Prefix write. */
2891 stream_putc(s
, p
->prefixlen
+ 88);
2892 stream_put(s
, label
, BGP_LABEL_BYTES
);
2893 stream_put(s
, prd
->val
, 8);
2894 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2895 } else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
2896 /* EVPN prefix - contents depend on type */
2897 bgp_evpn_encode_prefix(s
, p
, prd
, label
, num_labels
, attr
,
2898 addpath_encode
, addpath_tx_id
);
2899 } else if (safi
== SAFI_LABELED_UNICAST
) {
2900 /* Prefix write with label. */
2901 stream_put_labeled_prefix(s
, p
, label
);
2902 } else if (safi
== SAFI_FLOWSPEC
) {
2903 if (PSIZE (p
->prefixlen
)+2 < FLOWSPEC_NLRI_SIZELIMIT
)
2904 stream_putc(s
, PSIZE (p
->prefixlen
)+2);
2906 stream_putw(s
, (PSIZE (p
->prefixlen
)+2)|(0xf<<12));
2907 stream_putc(s
, 2);/* Filter type */
2908 stream_putc(s
, p
->prefixlen
);/* Prefix length */
2909 stream_put(s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
2911 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2914 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2916 int size
= PSIZE(p
->prefixlen
);
2917 if (safi
== SAFI_MPLS_VPN
)
2919 else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2920 size
+= 232; // TODO: Maximum possible for type-2, type-3 and
2926 * Encodes the tunnel encapsulation attribute,
2927 * and with ENABLE_BGP_VNC the VNC attribute which uses
2928 * almost the same TLV format
2930 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2931 struct stream
*s
, struct attr
*attr
,
2934 unsigned int attrlenfield
= 0;
2935 unsigned int attrhdrlen
= 0;
2936 struct bgp_attr_encap_subtlv
*subtlvs
;
2937 struct bgp_attr_encap_subtlv
*st
;
2938 const char *attrname
;
2940 if (!attr
|| (attrtype
== BGP_ATTR_ENCAP
2941 && (!attr
->encap_tunneltype
2942 || attr
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2946 case BGP_ATTR_ENCAP
:
2947 attrname
= "Tunnel Encap";
2948 subtlvs
= attr
->encap_subtlvs
;
2949 if (subtlvs
== NULL
) /* nothing to do */
2952 * The tunnel encap attr has an "outer" tlv.
2954 * L = total length of subtlvs,
2955 * V = concatenated subtlvs.
2957 attrlenfield
= 2 + 2; /* T + L */
2958 attrhdrlen
= 1 + 1; /* subTLV T + L */
2961 #if ENABLE_BGP_VNC_ATTR
2964 subtlvs
= attr
->vnc_subtlvs
;
2965 if (subtlvs
== NULL
) /* nothing to do */
2967 attrlenfield
= 0; /* no outer T + L */
2968 attrhdrlen
= 2 + 2; /* subTLV T + L */
2976 /* compute attr length */
2977 for (st
= subtlvs
; st
; st
= st
->next
) {
2978 attrlenfield
+= (attrhdrlen
+ st
->length
);
2981 if (attrlenfield
> 0xffff) {
2982 zlog_info("%s attribute is too long (length=%d), can't send it",
2983 attrname
, attrlenfield
);
2987 if (attrlenfield
> 0xff) {
2988 /* 2-octet length field */
2990 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2991 | BGP_ATTR_FLAG_EXTLEN
);
2992 stream_putc(s
, attrtype
);
2993 stream_putw(s
, attrlenfield
& 0xffff);
2995 /* 1-octet length field */
2996 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2997 stream_putc(s
, attrtype
);
2998 stream_putc(s
, attrlenfield
& 0xff);
3001 if (attrtype
== BGP_ATTR_ENCAP
) {
3002 /* write outer T+L */
3003 stream_putw(s
, attr
->encap_tunneltype
);
3004 stream_putw(s
, attrlenfield
- 4);
3007 /* write each sub-tlv */
3008 for (st
= subtlvs
; st
; st
= st
->next
) {
3009 if (attrtype
== BGP_ATTR_ENCAP
) {
3010 stream_putc(s
, st
->type
);
3011 stream_putc(s
, st
->length
);
3014 stream_putw(s
, st
->type
);
3015 stream_putw(s
, st
->length
);
3018 stream_put(s
, st
->value
, st
->length
);
3022 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
3024 /* Set MP attribute length. Don't count the (2) bytes used to encode
3026 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
3029 /* Make attribute packet. */
3030 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
3031 struct stream
*s
, struct attr
*attr
,
3032 struct bpacket_attr_vec_arr
*vecarr
,
3033 struct prefix
*p
, afi_t afi
, safi_t safi
,
3034 struct peer
*from
, struct prefix_rd
*prd
,
3035 mpls_label_t
*label
, uint32_t num_labels
,
3036 int addpath_encode
, uint32_t addpath_tx_id
)
3039 size_t aspath_sizep
;
3040 struct aspath
*aspath
;
3041 int send_as4_path
= 0;
3042 int send_as4_aggregator
= 0;
3043 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
3048 /* Remember current pointer. */
3049 cp
= stream_get_endp(s
);
3052 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
3053 && !peer_cap_enhe(peer
, afi
, safi
))) {
3054 size_t mpattrlen_pos
= 0;
3056 mpattrlen_pos
= bgp_packet_mpattr_start(s
, peer
, afi
, safi
,
3058 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
3059 num_labels
, addpath_encode
,
3060 addpath_tx_id
, attr
);
3061 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
3064 /* Origin attribute. */
3065 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3066 stream_putc(s
, BGP_ATTR_ORIGIN
);
3068 stream_putc(s
, attr
->origin
);
3070 /* AS path attribute. */
3072 /* If remote-peer is EBGP */
3073 if (peer
->sort
== BGP_PEER_EBGP
3074 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3075 PEER_FLAG_AS_PATH_UNCHANGED
)
3076 || attr
->aspath
->segments
== NULL
)
3077 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3078 PEER_FLAG_RSERVER_CLIENT
))) {
3079 aspath
= aspath_dup(attr
->aspath
);
3081 /* Even though we may not be configured for confederations we
3083 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
3084 aspath
= aspath_delete_confed_seq(aspath
);
3086 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
3087 /* Stuff our path CONFED_ID on the front */
3088 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
3090 if (peer
->change_local_as
) {
3091 /* If replace-as is specified, we only use the
3092 change_local_as when
3093 advertising routes. */
3096 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
3097 aspath
= aspath_add_seq(aspath
,
3100 aspath
= aspath_add_seq(aspath
,
3101 peer
->change_local_as
);
3103 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
3106 } else if (peer
->sort
== BGP_PEER_CONFED
) {
3107 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
3109 aspath
= aspath_dup(attr
->aspath
);
3110 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
3112 aspath
= attr
->aspath
;
3114 /* If peer is not AS4 capable, then:
3115 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
3116 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
3118 * types are in it (i.e. exclude them if they are there)
3119 * AND do this only if there is at least one asnum > 65535 in the
3121 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
3123 * all ASnums > 65535 to BGP_AS_TRANS
3126 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3127 stream_putc(s
, BGP_ATTR_AS_PATH
);
3128 aspath_sizep
= stream_get_endp(s
);
3130 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
3132 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
3135 if (!use32bit
&& aspath_has_as4(aspath
))
3137 1; /* we'll do this later, at the correct place */
3139 /* Nexthop attribute. */
3140 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
3141 && !peer_cap_enhe(peer
, afi
, safi
)) {
3142 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
3143 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3144 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3145 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
3148 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3149 } else if (peer_cap_enhe(from
, afi
, safi
)) {
3151 * Likely this is the case when an IPv4 prefix was
3153 * Extended Next-hop capability and now being advertised
3156 * Setting the mandatory (ipv4) next-hop attribute here
3158 * implicit next-hop self with correct (ipv4 address
3161 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3162 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3163 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
3166 stream_put_ipv4(s
, 0);
3170 /* MED attribute. */
3171 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
3172 || bgp
->maxmed_active
) {
3173 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3174 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3176 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
3180 /* Local preference. */
3181 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
3182 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3183 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3185 stream_putl(s
, attr
->local_pref
);
3188 /* Atomic aggregate. */
3189 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3190 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3191 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3196 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3197 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3198 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3199 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3202 /* AS4 capable peer */
3204 stream_putl(s
, attr
->aggregator_as
);
3206 /* 2-byte AS peer */
3209 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3211 if (attr
->aggregator_as
> 65535) {
3212 stream_putw(s
, BGP_AS_TRANS
);
3214 /* we have to send AS4_AGGREGATOR, too.
3215 * we'll do that later in order to send
3216 * attributes in ascending
3219 send_as4_aggregator
= 1;
3221 stream_putw(s
, (uint16_t)attr
->aggregator_as
);
3223 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3226 /* Community attribute. */
3227 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3228 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3229 if (attr
->community
->size
* 4 > 255) {
3231 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3232 | BGP_ATTR_FLAG_EXTLEN
);
3233 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3234 stream_putw(s
, attr
->community
->size
* 4);
3237 BGP_ATTR_FLAG_OPTIONAL
3238 | BGP_ATTR_FLAG_TRANS
);
3239 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3240 stream_putc(s
, attr
->community
->size
* 4);
3242 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3246 * Large Community attribute.
3248 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3249 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3250 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3251 if (lcom_length(attr
->lcommunity
) > 255) {
3253 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3254 | BGP_ATTR_FLAG_EXTLEN
);
3255 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3256 stream_putw(s
, lcom_length(attr
->lcommunity
));
3259 BGP_ATTR_FLAG_OPTIONAL
3260 | BGP_ATTR_FLAG_TRANS
);
3261 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3262 stream_putc(s
, lcom_length(attr
->lcommunity
));
3264 stream_put(s
, attr
->lcommunity
->val
,
3265 lcom_length(attr
->lcommunity
));
3268 /* Route Reflector. */
3269 if (peer
->sort
== BGP_PEER_IBGP
&& from
3270 && from
->sort
== BGP_PEER_IBGP
) {
3271 /* Originator ID. */
3272 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3273 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3276 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3277 stream_put_in_addr(s
, &attr
->originator_id
);
3279 stream_put_in_addr(s
, &from
->remote_id
);
3282 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3283 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3285 if (attr
->cluster
) {
3286 stream_putc(s
, attr
->cluster
->length
+ 4);
3287 /* If this peer configuration's parent BGP has
3289 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3290 stream_put_in_addr(s
, &bgp
->cluster_id
);
3292 stream_put_in_addr(s
, &bgp
->router_id
);
3293 stream_put(s
, attr
->cluster
->list
,
3294 attr
->cluster
->length
);
3297 /* If this peer configuration's parent BGP has
3299 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3300 stream_put_in_addr(s
, &bgp
->cluster_id
);
3302 stream_put_in_addr(s
, &bgp
->router_id
);
3306 /* Extended Communities attribute. */
3307 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3308 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3309 if (peer
->sort
== BGP_PEER_IBGP
3310 || peer
->sort
== BGP_PEER_CONFED
) {
3311 if (attr
->ecommunity
->size
* 8 > 255) {
3313 BGP_ATTR_FLAG_OPTIONAL
3314 | BGP_ATTR_FLAG_TRANS
3315 | BGP_ATTR_FLAG_EXTLEN
);
3316 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3317 stream_putw(s
, attr
->ecommunity
->size
* 8);
3320 BGP_ATTR_FLAG_OPTIONAL
3321 | BGP_ATTR_FLAG_TRANS
);
3322 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3323 stream_putc(s
, attr
->ecommunity
->size
* 8);
3325 stream_put(s
, attr
->ecommunity
->val
,
3326 attr
->ecommunity
->size
* 8);
3330 int ecom_tr_size
= 0;
3333 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3334 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3337 if (CHECK_FLAG(tbit
,
3338 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3345 if (ecom_tr_size
* 8 > 255) {
3348 BGP_ATTR_FLAG_OPTIONAL
3349 | BGP_ATTR_FLAG_TRANS
3350 | BGP_ATTR_FLAG_EXTLEN
);
3352 BGP_ATTR_EXT_COMMUNITIES
);
3353 stream_putw(s
, ecom_tr_size
* 8);
3357 BGP_ATTR_FLAG_OPTIONAL
3358 | BGP_ATTR_FLAG_TRANS
);
3360 BGP_ATTR_EXT_COMMUNITIES
);
3361 stream_putc(s
, ecom_tr_size
* 8);
3364 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3365 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3370 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3373 stream_put(s
, pnt
, 8);
3379 /* Label index attribute. */
3380 if (safi
== SAFI_LABELED_UNICAST
) {
3381 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3382 uint32_t label_index
;
3384 label_index
= attr
->label_index
;
3386 if (label_index
!= BGP_INVALID_LABEL_INDEX
) {
3388 BGP_ATTR_FLAG_OPTIONAL
3389 | BGP_ATTR_FLAG_TRANS
);
3390 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3392 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3394 BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3395 stream_putc(s
, 0); // reserved
3396 stream_putw(s
, 0); // flags
3397 stream_putl(s
, label_index
);
3402 if (send_as4_path
) {
3403 /* If the peer is NOT As4 capable, AND */
3404 /* there are ASnums > 65535 in path THEN
3405 * give out AS4_PATH */
3407 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3409 * Hm, I wonder... confederation things *should* only be at
3410 * the beginning of an aspath, right? Then we should use
3411 * aspath_delete_confed_seq for this, because it is already
3413 * Folks, talk to me: what is reasonable here!?
3415 aspath
= aspath_delete_confed_seq(aspath
);
3418 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3419 | BGP_ATTR_FLAG_EXTLEN
);
3420 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3421 aspath_sizep
= stream_get_endp(s
);
3423 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3426 if (aspath
!= attr
->aspath
)
3427 aspath_free(aspath
);
3429 if (send_as4_aggregator
) {
3430 /* send AS4_AGGREGATOR, at this place */
3431 /* this section of code moved here in order to ensure the
3433 * *ascending* order of attributes
3435 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3436 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3438 stream_putl(s
, attr
->aggregator_as
);
3439 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3442 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3443 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3444 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3445 /* Tunnel Encap attribute */
3446 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3448 #if ENABLE_BGP_VNC_ATTR
3450 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3455 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
3456 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3457 stream_putc(s
, BGP_ATTR_PMSI_TUNNEL
);
3458 stream_putc(s
, 9); // Length
3459 stream_putc(s
, 0); // Flags
3460 stream_putc(s
, attr
->pmsi_tnl_type
);
3461 stream_put(s
, &(attr
->label
),
3462 BGP_LABEL_BYTES
); // MPLS Label / VXLAN VNI
3463 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3464 // Unicast tunnel endpoint IP address
3467 /* Unknown transit attribute. */
3469 stream_put(s
, attr
->transit
->val
, attr
->transit
->length
);
3471 /* Return total size of attribute. */
3472 return stream_get_endp(s
) - cp
;
3475 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3477 unsigned long attrlen_pnt
;
3479 iana_safi_t pkt_safi
;
3481 /* Set extended bit always to encode the attribute length as 2 bytes */
3482 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3483 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3485 attrlen_pnt
= stream_get_endp(s
);
3486 stream_putw(s
, 0); /* Length of this attribute. */
3488 /* Convert AFI, SAFI to values for packet. */
3489 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3491 stream_putw(s
, pkt_afi
);
3492 stream_putc(s
, pkt_safi
);
3497 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3498 safi_t safi
, struct prefix_rd
*prd
,
3499 mpls_label_t
*label
, uint32_t num_labels
,
3500 int addpath_encode
, uint32_t addpath_tx_id
,
3503 uint8_t wlabel
[3] = {0x80, 0x00, 0x00};
3505 if (safi
== SAFI_LABELED_UNICAST
) {
3506 label
= (mpls_label_t
*)wlabel
;
3510 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
, num_labels
,
3511 addpath_encode
, addpath_tx_id
, attr
);
3514 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3516 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3519 /* Initialization of attribute. */
3520 void bgp_attr_init(void)
3532 void bgp_attr_finish(void)
3537 ecommunity_finish();
3538 lcommunity_finish();
3544 /* Make attribute packet. */
3545 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3546 struct prefix
*prefix
)
3551 struct aspath
*aspath
;
3552 int addpath_encode
= 0;
3553 uint32_t addpath_tx_id
= 0;
3555 /* Remember current pointer. */
3556 cp
= stream_get_endp(s
);
3558 /* Place holder of length. */
3561 /* Origin attribute. */
3562 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3563 stream_putc(s
, BGP_ATTR_ORIGIN
);
3565 stream_putc(s
, attr
->origin
);
3567 aspath
= attr
->aspath
;
3569 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3570 stream_putc(s
, BGP_ATTR_AS_PATH
);
3571 aspath_lenp
= stream_get_endp(s
);
3574 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3576 /* Nexthop attribute. */
3577 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3578 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3579 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3580 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3582 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3585 /* MED attribute. */
3586 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3587 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3588 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3590 stream_putl(s
, attr
->med
);
3593 /* Local preference. */
3594 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3595 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3596 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3598 stream_putl(s
, attr
->local_pref
);
3601 /* Atomic aggregate. */
3602 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3603 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3604 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3609 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3610 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3611 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3613 stream_putl(s
, attr
->aggregator_as
);
3614 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3617 /* Community attribute. */
3618 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3619 if (attr
->community
->size
* 4 > 255) {
3621 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3622 | BGP_ATTR_FLAG_EXTLEN
);
3623 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3624 stream_putw(s
, attr
->community
->size
* 4);
3627 BGP_ATTR_FLAG_OPTIONAL
3628 | BGP_ATTR_FLAG_TRANS
);
3629 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3630 stream_putc(s
, attr
->community
->size
* 4);
3632 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3635 /* Large Community attribute. */
3636 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3637 if (lcom_length(attr
->lcommunity
) > 255) {
3639 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3640 | BGP_ATTR_FLAG_EXTLEN
);
3641 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3642 stream_putw(s
, lcom_length(attr
->lcommunity
));
3645 BGP_ATTR_FLAG_OPTIONAL
3646 | BGP_ATTR_FLAG_TRANS
);
3647 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3648 stream_putc(s
, lcom_length(attr
->lcommunity
));
3651 stream_put(s
, attr
->lcommunity
->val
,
3652 lcom_length(attr
->lcommunity
));
3655 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3656 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
3657 && (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3658 || attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3661 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3662 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3663 sizep
= stream_get_endp(s
);
3666 stream_putc(s
, 0); /* Marker: Attribute length. */
3667 stream_putw(s
, AFI_IP6
); /* AFI */
3668 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3671 stream_putc(s
, attr
->mp_nexthop_len
);
3672 stream_put(s
, &attr
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3673 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3674 stream_put(s
, &attr
->mp_nexthop_local
,
3681 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3684 /* Set MP attribute length. */
3685 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3689 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3690 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
3692 BGP_ATTR_FLAG_OPTIONAL
3693 | BGP_ATTR_FLAG_TRANS
);
3694 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3696 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3697 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3698 stream_putc(s
, 0); // reserved
3699 stream_putw(s
, 0); // flags
3700 stream_putl(s
, attr
->label_index
);
3704 /* Return total size of attribute. */
3705 len
= stream_get_endp(s
) - cp
- 2;
3706 stream_putw_at(s
, cp
, len
);