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_label.h"
43 #include "bgpd/bgp_packet.h"
44 #include "bgpd/bgp_ecommunity.h"
45 #include "bgpd/bgp_lcommunity.h"
46 #include "bgpd/bgp_updgrp.h"
47 #include "bgpd/bgp_encap_types.h"
49 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
50 #include "bgp_encap_types.h"
51 #include "bgp_vnc_types.h"
53 #include "bgp_encap_types.h"
56 /* Attribute strings for logging. */
57 static const struct message attr_str
[] = {
58 {BGP_ATTR_ORIGIN
, "ORIGIN"},
59 {BGP_ATTR_AS_PATH
, "AS_PATH"},
60 {BGP_ATTR_NEXT_HOP
, "NEXT_HOP"},
61 {BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC"},
62 {BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF"},
63 {BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE"},
64 {BGP_ATTR_AGGREGATOR
, "AGGREGATOR"},
65 {BGP_ATTR_COMMUNITIES
, "COMMUNITY"},
66 {BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID"},
67 {BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST"},
68 {BGP_ATTR_DPA
, "DPA"},
69 {BGP_ATTR_ADVERTISER
, "ADVERTISER"},
70 {BGP_ATTR_RCID_PATH
, "RCID_PATH"},
71 {BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI"},
72 {BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI"},
73 {BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES"},
74 {BGP_ATTR_AS4_PATH
, "AS4_PATH"},
75 {BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR"},
76 {BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT"},
77 {BGP_ATTR_ENCAP
, "ENCAP"},
79 {BGP_ATTR_VNC
, "VNC"},
81 {BGP_ATTR_LARGE_COMMUNITIES
, "LARGE_COMMUNITY"},
82 {BGP_ATTR_PREFIX_SID
, "PREFIX_SID"},
85 static const struct message attr_flag_str
[] =
87 {BGP_ATTR_FLAG_OPTIONAL
, "Optional"},
88 {BGP_ATTR_FLAG_TRANS
, "Transitive"},
89 {BGP_ATTR_FLAG_PARTIAL
, "Partial"},
90 /* bgp_attr_flags_diagnose() relies on this bit being last in
92 {BGP_ATTR_FLAG_EXTLEN
, "Extended Length"},
95 static struct hash
*cluster_hash
;
97 static void *cluster_hash_alloc(void *p
)
99 const struct cluster_list
*val
= (const struct cluster_list
*)p
;
100 struct cluster_list
*cluster
;
102 cluster
= XMALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
103 cluster
->length
= val
->length
;
105 if (cluster
->length
) {
106 cluster
->list
= XMALLOC(MTYPE_CLUSTER_VAL
, val
->length
);
107 memcpy(cluster
->list
, val
->list
, val
->length
);
109 cluster
->list
= NULL
;
116 /* Cluster list related functions. */
117 static struct cluster_list
*cluster_parse(struct in_addr
*pnt
, int length
)
119 struct cluster_list tmp
;
120 struct cluster_list
*cluster
;
125 cluster
= hash_get(cluster_hash
, &tmp
, cluster_hash_alloc
);
130 int cluster_loop_check(struct cluster_list
*cluster
, struct in_addr originator
)
134 for (i
= 0; i
< cluster
->length
/ 4; i
++)
135 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
140 static unsigned int cluster_hash_key_make(void *p
)
142 const struct cluster_list
*cluster
= p
;
144 return jhash(cluster
->list
, cluster
->length
, 0);
147 static int cluster_hash_cmp(const void *p1
, const void *p2
)
149 const struct cluster_list
*cluster1
= p1
;
150 const struct cluster_list
*cluster2
= p2
;
152 return (cluster1
->length
== cluster2
->length
153 && memcmp(cluster1
->list
, cluster2
->list
, cluster1
->length
)
157 static void cluster_free(struct cluster_list
*cluster
)
160 XFREE(MTYPE_CLUSTER_VAL
, cluster
->list
);
161 XFREE(MTYPE_CLUSTER
, cluster
);
164 static struct cluster_list
*cluster_dup(struct cluster_list
*cluster
)
166 struct cluster_list
*new;
168 new = XCALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
169 new->length
= cluster
->length
;
171 if (cluster
->length
) {
172 new->list
= XMALLOC(MTYPE_CLUSTER_VAL
, cluster
->length
);
173 memcpy(new->list
, cluster
->list
, cluster
->length
);
180 static struct cluster_list
*cluster_intern(struct cluster_list
*cluster
)
182 struct cluster_list
*find
;
184 find
= hash_get(cluster_hash
, cluster
, cluster_hash_alloc
);
190 void cluster_unintern(struct cluster_list
*cluster
)
195 if (cluster
->refcnt
== 0) {
196 hash_release(cluster_hash
, cluster
);
197 cluster_free(cluster
);
201 static void cluster_init(void)
204 hash_create(cluster_hash_key_make
, cluster_hash_cmp
, NULL
);
207 static void cluster_finish(void)
209 hash_clean(cluster_hash
, (void (*)(void *))cluster_free
);
210 hash_free(cluster_hash
);
214 static struct hash
*encap_hash
= NULL
;
216 static struct hash
*vnc_hash
= NULL
;
219 struct bgp_attr_encap_subtlv
*encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
221 struct bgp_attr_encap_subtlv
*new;
222 struct bgp_attr_encap_subtlv
*tail
;
223 struct bgp_attr_encap_subtlv
*p
;
225 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
226 int size
= sizeof(struct bgp_attr_encap_subtlv
) - 1 + p
->length
;
228 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
231 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
234 memcpy(tail
, p
, size
);
241 static void encap_free(struct bgp_attr_encap_subtlv
*p
)
243 struct bgp_attr_encap_subtlv
*next
;
247 XFREE(MTYPE_ENCAP_TLV
, p
);
252 void bgp_attr_flush_encap(struct attr
*attr
)
257 if (attr
->encap_subtlvs
) {
258 encap_free(attr
->encap_subtlvs
);
259 attr
->encap_subtlvs
= NULL
;
262 if (attr
->vnc_subtlvs
) {
263 encap_free(attr
->vnc_subtlvs
);
264 attr
->vnc_subtlvs
= NULL
;
270 * Compare encap sub-tlv chains
275 * This algorithm could be made faster if needed
277 static int encap_same(struct bgp_attr_encap_subtlv
*h1
,
278 struct bgp_attr_encap_subtlv
*h2
)
280 struct bgp_attr_encap_subtlv
*p
;
281 struct bgp_attr_encap_subtlv
*q
;
285 if (h1
== NULL
|| h2
== NULL
)
288 for (p
= h1
; p
; p
= p
->next
) {
289 for (q
= h2
; q
; q
= q
->next
) {
290 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
291 && !memcmp(p
->value
, q
->value
, p
->length
)) {
300 for (p
= h2
; p
; p
= p
->next
) {
301 for (q
= h1
; q
; q
= q
->next
) {
302 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
303 && !memcmp(p
->value
, q
->value
, p
->length
)) {
315 static void *encap_hash_alloc(void *p
)
317 /* Encap structure is already allocated. */
328 static struct bgp_attr_encap_subtlv
*
329 encap_intern(struct bgp_attr_encap_subtlv
*encap
, encap_subtlv_type type
)
331 struct bgp_attr_encap_subtlv
*find
;
332 struct hash
*hash
= encap_hash
;
334 if (type
== VNC_SUBTLV_TYPE
)
338 find
= hash_get(hash
, encap
, encap_hash_alloc
);
346 static void encap_unintern(struct bgp_attr_encap_subtlv
**encapp
,
347 encap_subtlv_type type
)
349 struct bgp_attr_encap_subtlv
*encap
= *encapp
;
353 if (encap
->refcnt
== 0) {
354 struct hash
*hash
= encap_hash
;
356 if (type
== VNC_SUBTLV_TYPE
)
359 hash_release(hash
, encap
);
365 static unsigned int encap_hash_key_make(void *p
)
367 const struct bgp_attr_encap_subtlv
*encap
= p
;
369 return jhash(encap
->value
, encap
->length
, 0);
372 static int encap_hash_cmp(const void *p1
, const void *p2
)
374 return encap_same((struct bgp_attr_encap_subtlv
*)p1
,
375 (struct bgp_attr_encap_subtlv
*)p2
);
378 static void encap_init(void)
380 encap_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
, NULL
);
382 vnc_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
, NULL
);
386 static void encap_finish(void)
388 hash_clean(encap_hash
, (void (*)(void *))encap_free
);
389 hash_free(encap_hash
);
392 hash_clean(vnc_hash
, (void (*)(void *))encap_free
);
398 static bool overlay_index_same(const struct attr
*a1
, const struct attr
*a2
)
406 return !memcmp(&(a1
->evpn_overlay
), &(a2
->evpn_overlay
),
407 sizeof(struct overlay_index
));
410 /* Unknown transit attribute. */
411 static struct hash
*transit_hash
;
413 static void transit_free(struct transit
*transit
)
416 XFREE(MTYPE_TRANSIT_VAL
, transit
->val
);
417 XFREE(MTYPE_TRANSIT
, transit
);
420 static struct transit
*transit_dup(struct transit
*transit
)
424 new = XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
425 new->length
= transit
->length
;
427 new->val
= XMALLOC(MTYPE_TRANSIT_VAL
, transit
->length
);
428 memcpy(new->val
, transit
->val
, transit
->length
);
435 static void *transit_hash_alloc(void *p
)
437 /* Transit structure is already allocated. */
441 static struct transit
*transit_intern(struct transit
*transit
)
443 struct transit
*find
;
445 find
= hash_get(transit_hash
, transit
, transit_hash_alloc
);
447 transit_free(transit
);
453 void transit_unintern(struct transit
*transit
)
458 if (transit
->refcnt
== 0) {
459 hash_release(transit_hash
, transit
);
460 transit_free(transit
);
464 static unsigned int transit_hash_key_make(void *p
)
466 const struct transit
*transit
= p
;
468 return jhash(transit
->val
, transit
->length
, 0);
471 static int transit_hash_cmp(const void *p1
, const void *p2
)
473 const struct transit
*transit1
= p1
;
474 const struct transit
*transit2
= p2
;
476 return (transit1
->length
== transit2
->length
477 && memcmp(transit1
->val
, transit2
->val
, transit1
->length
) == 0);
480 static void transit_init(void)
483 hash_create(transit_hash_key_make
, transit_hash_cmp
, NULL
);
486 static void transit_finish(void)
488 hash_clean(transit_hash
, (void (*)(void *))transit_free
);
489 hash_free(transit_hash
);
493 /* Attribute hash routines. */
494 static struct hash
*attrhash
;
496 /* Shallow copy of an attribute
497 * Though, not so shallow that it doesn't copy the contents
498 * of the attr_extra pointed to by 'extra'
500 void bgp_attr_dup(struct attr
*new, struct attr
*orig
)
505 void bgp_attr_deep_dup(struct attr
*new, struct attr
*orig
)
508 new->aspath
= aspath_dup(orig
->aspath
);
511 new->community
= community_dup(orig
->community
);
513 if (orig
->ecommunity
)
514 new->ecommunity
= ecommunity_dup(orig
->ecommunity
);
516 new->cluster
= cluster_dup(orig
->cluster
);
518 new->transit
= transit_dup(orig
->transit
);
519 if (orig
->encap_subtlvs
)
520 new->encap_subtlvs
= encap_tlv_dup(orig
->encap_subtlvs
);
522 if (orig
->vnc_subtlvs
)
523 new->vnc_subtlvs
= encap_tlv_dup(orig
->vnc_subtlvs
);
527 void bgp_attr_deep_free(struct attr
*attr
)
530 aspath_free(attr
->aspath
);
533 community_free(attr
->community
);
535 if (attr
->ecommunity
)
536 ecommunity_free(&attr
->ecommunity
);
538 cluster_free(attr
->cluster
);
540 transit_free(attr
->transit
);
541 if (attr
->encap_subtlvs
)
542 encap_free(attr
->encap_subtlvs
);
544 if (attr
->vnc_subtlvs
)
545 encap_free(attr
->vnc_subtlvs
);
549 unsigned long int attr_count(void)
551 return attrhash
->count
;
554 unsigned long int attr_unknown_count(void)
556 return transit_hash
->count
;
559 unsigned int attrhash_key_make(void *p
)
561 const struct attr
*attr
= (struct attr
*)p
;
563 #define MIX(val) key = jhash_1word(val, key)
566 MIX(attr
->nexthop
.s_addr
);
568 MIX(attr
->local_pref
);
569 MIX(attr
->aggregator_as
);
570 MIX(attr
->aggregator_addr
.s_addr
);
572 MIX(attr
->mp_nexthop_global_in
.s_addr
);
573 MIX(attr
->originator_id
.s_addr
);
576 MIX(attr
->label_index
);
579 MIX(aspath_key_make(attr
->aspath
));
581 MIX(community_hash_make(attr
->community
));
583 if (attr
->lcommunity
)
584 MIX(lcommunity_hash_make(attr
->lcommunity
));
585 if (attr
->ecommunity
)
586 MIX(ecommunity_hash_make(attr
->ecommunity
));
588 MIX(cluster_hash_key_make(attr
->cluster
));
590 MIX(transit_hash_key_make(attr
->transit
));
591 if (attr
->encap_subtlvs
)
592 MIX(encap_hash_key_make(attr
->encap_subtlvs
));
594 if (attr
->vnc_subtlvs
)
595 MIX(encap_hash_key_make(attr
->vnc_subtlvs
));
597 MIX(attr
->mp_nexthop_len
);
598 key
= jhash(attr
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
599 key
= jhash(attr
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
604 int attrhash_cmp(const void *p1
, const void *p2
)
606 const struct attr
*attr1
= p1
;
607 const struct attr
*attr2
= p2
;
609 if (attr1
->flag
== attr2
->flag
&& attr1
->origin
== attr2
->origin
610 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
611 && attr1
->aspath
== attr2
->aspath
612 && attr1
->community
== attr2
->community
&& attr1
->med
== attr2
->med
613 && attr1
->local_pref
== attr2
->local_pref
614 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
) {
615 if (attr1
->aggregator_as
== attr2
->aggregator_as
616 && attr1
->aggregator_addr
.s_addr
617 == attr2
->aggregator_addr
.s_addr
618 && attr1
->weight
== attr2
->weight
619 && attr1
->tag
== attr2
->tag
620 && attr1
->label_index
== attr2
->label_index
621 && attr1
->mp_nexthop_len
== attr2
->mp_nexthop_len
622 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_global
,
623 &attr2
->mp_nexthop_global
)
624 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_local
,
625 &attr2
->mp_nexthop_local
)
626 && IPV4_ADDR_SAME(&attr1
->mp_nexthop_global_in
,
627 &attr2
->mp_nexthop_global_in
)
628 && attr1
->ecommunity
== attr2
->ecommunity
629 && attr1
->lcommunity
== attr2
->lcommunity
630 && attr1
->cluster
== attr2
->cluster
631 && attr1
->transit
== attr2
->transit
632 && (attr1
->encap_tunneltype
== attr2
->encap_tunneltype
)
633 && encap_same(attr1
->encap_subtlvs
, attr2
->encap_subtlvs
)
635 && encap_same(attr1
->vnc_subtlvs
, attr2
->vnc_subtlvs
)
637 && IPV4_ADDR_SAME(&attr1
->originator_id
,
638 &attr2
->originator_id
)
639 && overlay_index_same(attr1
, attr2
))
646 static void attrhash_init(void)
649 hash_create(attrhash_key_make
, attrhash_cmp
, "BGP Attributes");
653 * special for hash_clean below
655 static void attr_vfree(void *attr
)
657 XFREE(MTYPE_ATTR
, attr
);
660 static void attrhash_finish(void)
662 hash_clean(attrhash
, attr_vfree
);
667 static void attr_show_all_iterator(struct hash_backet
*backet
, struct vty
*vty
)
669 struct attr
*attr
= backet
->data
;
671 vty_out(vty
, "attr[%ld] nexthop %s\n", attr
->refcnt
,
672 inet_ntoa(attr
->nexthop
));
675 void attr_show_all(struct vty
*vty
)
677 hash_iterate(attrhash
, (void (*)(struct hash_backet
*,
678 void *))attr_show_all_iterator
,
682 static void *bgp_attr_hash_alloc(void *p
)
684 struct attr
*val
= (struct attr
*)p
;
687 attr
= XMALLOC(MTYPE_ATTR
, sizeof(struct attr
));
689 if (val
->encap_subtlvs
) {
690 val
->encap_subtlvs
= NULL
;
693 if (val
->vnc_subtlvs
) {
694 val
->vnc_subtlvs
= NULL
;
701 /* Internet argument attribute. */
702 struct attr
*bgp_attr_intern(struct attr
*attr
)
706 /* Intern referenced strucutre. */
708 if (!attr
->aspath
->refcnt
)
709 attr
->aspath
= aspath_intern(attr
->aspath
);
711 attr
->aspath
->refcnt
++;
713 if (attr
->community
) {
714 if (!attr
->community
->refcnt
)
715 attr
->community
= community_intern(attr
->community
);
717 attr
->community
->refcnt
++;
720 if (attr
->ecommunity
) {
721 if (!attr
->ecommunity
->refcnt
)
722 attr
->ecommunity
= ecommunity_intern(attr
->ecommunity
);
724 attr
->ecommunity
->refcnt
++;
726 if (attr
->lcommunity
) {
727 if (!attr
->lcommunity
->refcnt
)
728 attr
->lcommunity
= lcommunity_intern(attr
->lcommunity
);
730 attr
->lcommunity
->refcnt
++;
733 if (!attr
->cluster
->refcnt
)
734 attr
->cluster
= cluster_intern(attr
->cluster
);
736 attr
->cluster
->refcnt
++;
739 if (!attr
->transit
->refcnt
)
740 attr
->transit
= transit_intern(attr
->transit
);
742 attr
->transit
->refcnt
++;
744 if (attr
->encap_subtlvs
) {
745 if (!attr
->encap_subtlvs
->refcnt
)
746 attr
->encap_subtlvs
= encap_intern(attr
->encap_subtlvs
,
749 attr
->encap_subtlvs
->refcnt
++;
752 if (attr
->vnc_subtlvs
) {
753 if (!attr
->vnc_subtlvs
->refcnt
)
754 attr
->vnc_subtlvs
= encap_intern(attr
->vnc_subtlvs
,
757 attr
->vnc_subtlvs
->refcnt
++;
761 find
= (struct attr
*)hash_get(attrhash
, attr
, bgp_attr_hash_alloc
);
768 * Increment the refcount on various structures that attr holds.
769 * Note on usage: call _only_ when the 'attr' object has already
770 * been 'intern'ed and exists in 'attrhash' table. The function
771 * serves to hold a reference to that (real) object.
772 * Note also that the caller can safely call bgp_attr_unintern()
773 * after calling bgp_attr_refcount(). That would release the
774 * reference and could result in a free() of the attr object.
776 struct attr
*bgp_attr_refcount(struct attr
*attr
)
778 /* Intern referenced strucutre. */
780 attr
->aspath
->refcnt
++;
783 attr
->community
->refcnt
++;
785 if (attr
->ecommunity
)
786 attr
->ecommunity
->refcnt
++;
789 attr
->cluster
->refcnt
++;
792 attr
->transit
->refcnt
++;
794 if (attr
->encap_subtlvs
)
795 attr
->encap_subtlvs
->refcnt
++;
798 if (attr
->vnc_subtlvs
)
799 attr
->vnc_subtlvs
->refcnt
++;
806 /* Make network statement's attribute. */
807 struct attr
*bgp_attr_default_set(struct attr
*attr
, u_char origin
)
809 memset(attr
, 0, sizeof(struct attr
));
811 attr
->origin
= origin
;
812 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
813 attr
->aspath
= aspath_empty();
814 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
815 attr
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
817 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
818 attr
->label
= MPLS_INVALID_LABEL
;
819 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
820 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
825 /* Create the attributes for an aggregate */
826 struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, u_char origin
,
827 struct aspath
*aspath
,
828 struct community
*community
, int as_set
,
829 u_char atomic_aggregate
)
834 memset(&attr
, 0, sizeof(struct attr
));
836 /* Origin attribute. */
837 attr
.origin
= origin
;
838 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
840 /* AS path attribute. */
842 attr
.aspath
= aspath_intern(aspath
);
844 attr
.aspath
= aspath_empty();
845 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
847 /* Next hop attribute. */
848 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
851 u_int32_t gshut
= COMMUNITY_GSHUT
;
853 /* If we are not shutting down ourselves and we are
854 * aggregating a route that contains the GSHUT community we
855 * need to remove that community when creating the aggregate */
856 if (!bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
) &&
857 community_include(community
, gshut
)) {
858 community_del_val(community
, &gshut
);
861 attr
.community
= community
;
862 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
865 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
866 bgp_attr_add_gshut_community(&attr
);
869 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
870 attr
.label
= MPLS_INVALID_LABEL
;
871 attr
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
872 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
873 if (!as_set
|| atomic_aggregate
)
874 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
875 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
876 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
877 attr
.aggregator_as
= bgp
->confed_id
;
879 attr
.aggregator_as
= bgp
->as
;
880 attr
.aggregator_addr
= bgp
->router_id
;
881 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
882 attr
.label
= MPLS_INVALID_LABEL
;
884 new = bgp_attr_intern(&attr
);
886 aspath_unintern(&new->aspath
);
890 /* Unintern just the sub-components of the attr, but not the attr */
891 void bgp_attr_unintern_sub(struct attr
*attr
)
893 /* aspath refcount shoud be decrement. */
895 aspath_unintern(&attr
->aspath
);
896 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
));
899 community_unintern(&attr
->community
);
900 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
));
902 if (attr
->ecommunity
)
903 ecommunity_unintern(&attr
->ecommunity
);
904 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
));
906 if (attr
->lcommunity
)
907 lcommunity_unintern(&attr
->lcommunity
);
908 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
));
911 cluster_unintern(attr
->cluster
);
912 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
));
915 transit_unintern(attr
->transit
);
917 if (attr
->encap_subtlvs
)
918 encap_unintern(&attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
921 if (attr
->vnc_subtlvs
)
922 encap_unintern(&attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
926 /* Free bgp attribute and aspath. */
927 void bgp_attr_unintern(struct attr
**pattr
)
929 struct attr
*attr
= *pattr
;
933 /* Decrement attribute reference. */
938 /* If reference becomes zero then free attribute object. */
939 if (attr
->refcnt
== 0) {
940 ret
= hash_release(attrhash
, attr
);
942 XFREE(MTYPE_ATTR
, attr
);
946 bgp_attr_unintern_sub(&tmp
);
949 void bgp_attr_flush(struct attr
*attr
)
951 if (attr
->aspath
&& !attr
->aspath
->refcnt
) {
952 aspath_free(attr
->aspath
);
955 if (attr
->community
&& !attr
->community
->refcnt
) {
956 community_free(attr
->community
);
957 attr
->community
= NULL
;
960 if (attr
->ecommunity
&& !attr
->ecommunity
->refcnt
)
961 ecommunity_free(&attr
->ecommunity
);
962 if (attr
->lcommunity
&& !attr
->lcommunity
->refcnt
)
963 lcommunity_free(&attr
->lcommunity
);
964 if (attr
->cluster
&& !attr
->cluster
->refcnt
) {
965 cluster_free(attr
->cluster
);
966 attr
->cluster
= NULL
;
968 if (attr
->transit
&& !attr
->transit
->refcnt
) {
969 transit_free(attr
->transit
);
970 attr
->transit
= NULL
;
972 if (attr
->encap_subtlvs
&& !attr
->encap_subtlvs
->refcnt
) {
973 encap_free(attr
->encap_subtlvs
);
974 attr
->encap_subtlvs
= NULL
;
977 if (attr
->vnc_subtlvs
&& !attr
->vnc_subtlvs
->refcnt
) {
978 encap_free(attr
->vnc_subtlvs
);
979 attr
->vnc_subtlvs
= NULL
;
984 /* Implement draft-scudder-idr-optional-transitive behaviour and
985 * avoid resetting sessions for malformed attributes which are
986 * are partial/optional and hence where the error likely was not
987 * introduced by the sending neighbour.
989 static bgp_attr_parse_ret_t
990 bgp_attr_malformed(struct bgp_attr_parser_args
*args
, u_char subcode
,
993 struct peer
*const peer
= args
->peer
;
994 const u_int8_t flags
= args
->flags
;
995 /* startp and length must be special-cased, as whether or not to
996 * send the attribute data with the NOTIFY depends on the error,
997 * the caller therefore signals this with the seperate length argument
999 u_char
*notify_datap
= (length
> 0 ? args
->startp
: NULL
);
1001 /* Only relax error handling for eBGP peers */
1002 if (peer
->sort
!= BGP_PEER_EBGP
) {
1003 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1004 notify_datap
, length
);
1005 return BGP_ATTR_PARSE_ERROR
;
1008 /* Adjust the stream getp to the end of the attribute, in case we can
1009 * still proceed but the caller hasn't read all the attribute.
1011 stream_set_getp(BGP_INPUT(peer
),
1012 (args
->startp
- STREAM_DATA(BGP_INPUT(peer
)))
1015 switch (args
->type
) {
1016 /* where an attribute is relatively inconsequential, e.g. it does not
1017 * affect route selection, and can be safely ignored, then any such
1018 * attributes which are malformed should just be ignored and the route
1019 * processed as normal.
1021 case BGP_ATTR_AS4_AGGREGATOR
:
1022 case BGP_ATTR_AGGREGATOR
:
1023 case BGP_ATTR_ATOMIC_AGGREGATE
:
1024 return BGP_ATTR_PARSE_PROCEED
;
1026 /* Core attributes, particularly ones which may influence route
1027 * selection, should always cause session resets
1029 case BGP_ATTR_ORIGIN
:
1030 case BGP_ATTR_AS_PATH
:
1031 case BGP_ATTR_NEXT_HOP
:
1032 case BGP_ATTR_MULTI_EXIT_DISC
:
1033 case BGP_ATTR_LOCAL_PREF
:
1034 case BGP_ATTR_COMMUNITIES
:
1035 case BGP_ATTR_ORIGINATOR_ID
:
1036 case BGP_ATTR_CLUSTER_LIST
:
1037 case BGP_ATTR_MP_REACH_NLRI
:
1038 case BGP_ATTR_MP_UNREACH_NLRI
:
1039 case BGP_ATTR_EXT_COMMUNITIES
:
1040 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1041 notify_datap
, length
);
1042 return BGP_ATTR_PARSE_ERROR
;
1045 /* Partial optional attributes that are malformed should not cause
1046 * the whole session to be reset. Instead treat it as a withdrawal
1047 * of the routes, if possible.
1049 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)
1050 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1051 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
))
1052 return BGP_ATTR_PARSE_WITHDRAW
;
1054 /* default to reset */
1055 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1058 /* Find out what is wrong with the path attribute flag bits and log the error.
1059 "Flag bits" here stand for Optional, Transitive and Partial, but not for
1060 Extended Length. Checking O/T/P bits at once implies, that the attribute
1061 being diagnosed is defined by RFC as either a "well-known" or an "optional,
1062 non-transitive" attribute. */
1064 bgp_attr_flags_diagnose(struct bgp_attr_parser_args
*args
,
1065 u_int8_t desired_flags
/* how RFC says it must be */
1069 u_char real_flags
= args
->flags
;
1070 const u_int8_t attr_code
= args
->type
;
1072 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1073 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1074 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
1075 if (CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1076 != CHECK_FLAG(real_flags
, attr_flag_str
[i
].key
)) {
1077 zlog_err("%s attribute must%s be flagged as \"%s\"",
1078 lookup_msg(attr_str
, attr_code
, NULL
),
1079 CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1082 attr_flag_str
[i
].str
);
1087 "Strange, %s called for attr %s, but no problem found with flags"
1088 " (real flags 0x%x, desired 0x%x)",
1089 __func__
, lookup_msg(attr_str
, attr_code
, NULL
),
1090 real_flags
, desired_flags
);
1094 /* Required flags for attributes. EXTLEN will be masked off when testing,
1095 * as will PARTIAL for optional+transitive attributes.
1097 const u_int8_t attr_flags_values
[] = {
1098 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1099 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1100 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1101 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1102 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1103 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1104 [BGP_ATTR_AGGREGATOR
] =
1105 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1106 [BGP_ATTR_COMMUNITIES
] =
1107 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1108 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1109 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1110 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1111 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1112 [BGP_ATTR_EXT_COMMUNITIES
] =
1113 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1114 [BGP_ATTR_AS4_PATH
] =
1115 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1116 [BGP_ATTR_AS4_AGGREGATOR
] =
1117 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1118 [BGP_ATTR_LARGE_COMMUNITIES
] =
1119 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1120 [BGP_ATTR_PREFIX_SID
] =
1121 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1123 static const size_t attr_flags_values_max
= array_size(attr_flags_values
) - 1;
1125 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args
*args
)
1127 u_int8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1128 const u_int8_t flags
= args
->flags
;
1129 const u_int8_t attr_code
= args
->type
;
1131 /* there may be attributes we don't know about */
1132 if (attr_code
> attr_flags_values_max
)
1134 if (attr_flags_values
[attr_code
] == 0)
1137 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1141 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL
, flags
)
1142 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS
, flags
)) {
1144 "%s well-known attributes must have transitive flag set (%x)",
1145 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1149 /* "For well-known attributes and for optional non-transitive
1151 * the Partial bit MUST be set to 0."
1153 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
)) {
1154 if (!CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)) {
1156 "%s well-known attribute "
1157 "must NOT have the partial flag set (%x)",
1158 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1161 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1162 && !CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)) {
1164 "%s optional + transitive attribute "
1165 "must NOT have the partial flag set (%x)",
1166 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1171 /* Optional transitive attributes may go through speakers that don't
1172 * reocgnise them and set the Partial bit.
1174 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1175 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
))
1176 SET_FLAG(mask
, BGP_ATTR_FLAG_PARTIAL
);
1178 if ((flags
& ~mask
) == attr_flags_values
[attr_code
])
1181 bgp_attr_flags_diagnose(args
, attr_flags_values
[attr_code
]);
1185 /* Get origin attribute of the update message. */
1186 static bgp_attr_parse_ret_t
bgp_attr_origin(struct bgp_attr_parser_args
*args
)
1188 struct peer
*const peer
= args
->peer
;
1189 struct attr
*const attr
= args
->attr
;
1190 const bgp_size_t length
= args
->length
;
1192 /* If any recognized attribute has Attribute Length that conflicts
1193 with the expected length (based on the attribute type code), then
1194 the Error Subcode is set to Attribute Length Error. The Data
1195 field contains the erroneous attribute (type, length and
1198 zlog_err("Origin attribute length is not one %d", length
);
1199 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1203 /* Fetch origin attribute. */
1204 attr
->origin
= stream_getc(BGP_INPUT(peer
));
1206 /* If the ORIGIN attribute has an undefined value, then the Error
1207 Subcode is set to Invalid Origin Attribute. The Data field
1208 contains the unrecognized attribute (type, length and value). */
1209 if ((attr
->origin
!= BGP_ORIGIN_IGP
) && (attr
->origin
!= BGP_ORIGIN_EGP
)
1210 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
)) {
1211 zlog_err("Origin attribute value is invalid %d", attr
->origin
);
1212 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1216 /* Set oring attribute flag. */
1217 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
1222 /* Parse AS path information. This function is wrapper of
1224 static int bgp_attr_aspath(struct bgp_attr_parser_args
*args
)
1226 struct attr
*const attr
= args
->attr
;
1227 struct peer
*const peer
= args
->peer
;
1228 const bgp_size_t length
= args
->length
;
1231 * peer with AS4 => will get 4Byte ASnums
1232 * otherwise, will get 16 Bit
1234 attr
->aspath
= aspath_parse(peer
->ibuf
, length
,
1235 CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
));
1237 /* In case of IBGP, length will be zero. */
1238 if (!attr
->aspath
) {
1239 zlog_err("Malformed AS path from %s, length is %d", peer
->host
,
1241 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1245 /* Set aspath attribute flag. */
1246 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
1248 return BGP_ATTR_PARSE_PROCEED
;
1251 static bgp_attr_parse_ret_t
bgp_attr_aspath_check(struct peer
*const peer
,
1252 struct attr
*const attr
)
1254 /* These checks were part of bgp_attr_aspath, but with
1255 * as4 we should to check aspath things when
1256 * aspath synthesizing with as4_path has already taken place.
1257 * Otherwise we check ASPATH and use the synthesized thing, and that is
1259 * So do the checks later, i.e. here
1261 struct bgp
*bgp
= peer
->bgp
;
1262 struct aspath
*aspath
;
1264 /* Confederation sanity check. */
1265 if ((peer
->sort
== BGP_PEER_CONFED
1266 && !aspath_left_confed_check(attr
->aspath
))
1267 || (peer
->sort
== BGP_PEER_EBGP
1268 && aspath_confed_check(attr
->aspath
))) {
1269 zlog_err("Malformed AS path from %s", peer
->host
);
1270 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1271 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1272 return BGP_ATTR_PARSE_ERROR
;
1275 /* First AS check for EBGP. */
1276 if (bgp
!= NULL
&& bgp_flag_check(bgp
, BGP_FLAG_ENFORCE_FIRST_AS
)) {
1277 if (peer
->sort
== BGP_PEER_EBGP
1278 && !aspath_firstas_check(attr
->aspath
, peer
->as
)) {
1279 zlog_err("%s incorrect first AS (must be %u)",
1280 peer
->host
, peer
->as
);
1281 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1282 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1283 return BGP_ATTR_PARSE_ERROR
;
1287 /* local-as prepend */
1288 if (peer
->change_local_as
1289 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)) {
1290 aspath
= aspath_dup(attr
->aspath
);
1291 aspath
= aspath_add_seq(aspath
, peer
->change_local_as
);
1292 aspath_unintern(&attr
->aspath
);
1293 attr
->aspath
= aspath_intern(aspath
);
1296 return BGP_ATTR_PARSE_PROCEED
;
1299 /* Parse AS4 path information. This function is another wrapper of
1301 static int bgp_attr_as4_path(struct bgp_attr_parser_args
*args
,
1302 struct aspath
**as4_path
)
1304 struct peer
*const peer
= args
->peer
;
1305 struct attr
*const attr
= args
->attr
;
1306 const bgp_size_t length
= args
->length
;
1308 *as4_path
= aspath_parse(peer
->ibuf
, length
, 1);
1310 /* In case of IBGP, length will be zero. */
1312 zlog_err("Malformed AS4 path from %s, length is %d", peer
->host
,
1314 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1318 /* Set aspath attribute flag. */
1319 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
);
1321 return BGP_ATTR_PARSE_PROCEED
;
1324 /* Nexthop attribute. */
1325 static bgp_attr_parse_ret_t
bgp_attr_nexthop(struct bgp_attr_parser_args
*args
)
1327 struct peer
*const peer
= args
->peer
;
1328 struct attr
*const attr
= args
->attr
;
1329 const bgp_size_t length
= args
->length
;
1331 in_addr_t nexthop_h
, nexthop_n
;
1333 /* Check nexthop attribute length. */
1335 zlog_err("Nexthop attribute length isn't four [%d]", length
);
1337 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1341 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1342 attribute must result in a NOTIFICATION message (this is implemented
1344 At the same time, semantically incorrect NEXT_HOP is more likely to
1346 logged locally (this is implemented somewhere else). The UPDATE
1348 gets ignored in any of these cases. */
1349 nexthop_n
= stream_get_ipv4(peer
->ibuf
);
1350 nexthop_h
= ntohl(nexthop_n
);
1351 if ((IPV4_NET0(nexthop_h
) || IPV4_NET127(nexthop_h
)
1352 || IPV4_CLASS_DE(nexthop_h
))
1355 ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1357 char buf
[INET_ADDRSTRLEN
];
1358 inet_ntop(AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1359 zlog_err("Martian nexthop %s", buf
);
1360 return bgp_attr_malformed(
1361 args
, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
, args
->total
);
1364 attr
->nexthop
.s_addr
= nexthop_n
;
1365 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1367 return BGP_ATTR_PARSE_PROCEED
;
1370 /* MED atrribute. */
1371 static bgp_attr_parse_ret_t
bgp_attr_med(struct bgp_attr_parser_args
*args
)
1373 struct peer
*const peer
= args
->peer
;
1374 struct attr
*const attr
= args
->attr
;
1375 const bgp_size_t length
= args
->length
;
1379 zlog_err("MED attribute length isn't four [%d]", length
);
1381 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1385 attr
->med
= stream_getl(peer
->ibuf
);
1387 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
1389 return BGP_ATTR_PARSE_PROCEED
;
1392 /* Local preference attribute. */
1393 static bgp_attr_parse_ret_t
1394 bgp_attr_local_pref(struct bgp_attr_parser_args
*args
)
1396 struct peer
*const peer
= args
->peer
;
1397 struct attr
*const attr
= args
->attr
;
1398 const bgp_size_t length
= args
->length
;
1402 zlog_err("LOCAL_PREF attribute length isn't 4 [%u]", length
);
1403 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1407 /* If it is contained in an UPDATE message that is received from an
1408 external peer, then this attribute MUST be ignored by the
1409 receiving speaker. */
1410 if (peer
->sort
== BGP_PEER_EBGP
) {
1411 stream_forward_getp(peer
->ibuf
, length
);
1412 return BGP_ATTR_PARSE_PROCEED
;
1415 attr
->local_pref
= stream_getl(peer
->ibuf
);
1417 /* Set the local-pref flag. */
1418 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1420 return BGP_ATTR_PARSE_PROCEED
;
1423 /* Atomic aggregate. */
1424 static int bgp_attr_atomic(struct bgp_attr_parser_args
*args
)
1426 struct attr
*const attr
= args
->attr
;
1427 const bgp_size_t length
= args
->length
;
1431 zlog_err("ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1433 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1437 /* Set atomic aggregate flag. */
1438 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
1440 return BGP_ATTR_PARSE_PROCEED
;
1443 /* Aggregator attribute */
1444 static int bgp_attr_aggregator(struct bgp_attr_parser_args
*args
)
1446 struct peer
*const peer
= args
->peer
;
1447 struct attr
*const attr
= args
->attr
;
1448 const bgp_size_t length
= args
->length
;
1452 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1453 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1456 if (length
!= wantedlen
) {
1457 zlog_err("AGGREGATOR attribute length isn't %u [%u]", wantedlen
,
1459 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1463 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1464 attr
->aggregator_as
= stream_getl(peer
->ibuf
);
1466 attr
->aggregator_as
= stream_getw(peer
->ibuf
);
1467 attr
->aggregator_addr
.s_addr
= stream_get_ipv4(peer
->ibuf
);
1469 /* Set atomic aggregate flag. */
1470 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
1472 return BGP_ATTR_PARSE_PROCEED
;
1475 /* New Aggregator attribute */
1476 static bgp_attr_parse_ret_t
1477 bgp_attr_as4_aggregator(struct bgp_attr_parser_args
*args
,
1478 as_t
*as4_aggregator_as
,
1479 struct in_addr
*as4_aggregator_addr
)
1481 struct peer
*const peer
= args
->peer
;
1482 struct attr
*const attr
= args
->attr
;
1483 const bgp_size_t length
= args
->length
;
1486 zlog_err("New Aggregator length is not 8 [%d]", length
);
1487 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1491 *as4_aggregator_as
= stream_getl(peer
->ibuf
);
1492 as4_aggregator_addr
->s_addr
= stream_get_ipv4(peer
->ibuf
);
1494 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
);
1496 return BGP_ATTR_PARSE_PROCEED
;
1499 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1501 static bgp_attr_parse_ret_t
1502 bgp_attr_munge_as4_attrs(struct peer
*const peer
, struct attr
*const attr
,
1503 struct aspath
*as4_path
, as_t as4_aggregator
,
1504 struct in_addr
*as4_aggregator_addr
)
1506 int ignore_as4_path
= 0;
1507 struct aspath
*newpath
;
1509 if (!attr
->aspath
) {
1510 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1512 * checked that all well-known, mandatory attributes were
1515 * Can only be a problem with peer itself - hard error
1517 return BGP_ATTR_PARSE_ERROR
;
1520 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) {
1521 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1523 * It is worth a warning though, because the peer really
1524 * should not send them
1526 if (BGP_DEBUG(as4
, AS4
)) {
1527 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1528 zlog_debug("[AS4] %s %s AS4_PATH", peer
->host
,
1529 "AS4 capable peer, yet it sent");
1532 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1533 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1535 "AS4 capable peer, yet it sent");
1538 return BGP_ATTR_PARSE_PROCEED
;
1541 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1542 * because that may override AS4_PATH
1544 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
))) {
1545 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
1547 * if the as_number in aggregator is not AS_TRANS,
1548 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1549 * and the Aggregator shall be taken as
1550 * info on the aggregating node, and the AS_PATH
1551 * shall be taken as the AS_PATH
1553 * the Aggregator shall be ignored and the
1554 * AS4_AGGREGATOR shall be taken as the
1555 * Aggregating node and the AS_PATH is to be
1556 * constructed "as in all other cases"
1558 if (attr
->aggregator_as
!= BGP_AS_TRANS
) {
1560 if (BGP_DEBUG(as4
, AS4
))
1562 "[AS4] %s BGP not AS4 capable peer"
1563 " send AGGREGATOR != AS_TRANS and"
1564 " AS4_AGGREGATOR, so ignore"
1565 " AS4_AGGREGATOR and AS4_PATH",
1567 ignore_as4_path
= 1;
1569 /* "New_aggregator shall be taken as aggregator"
1571 attr
->aggregator_as
= as4_aggregator
;
1572 attr
->aggregator_addr
.s_addr
=
1573 as4_aggregator_addr
->s_addr
;
1576 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1577 * That is bogus - but reading the conditions
1578 * we have to handle AS4_AGGREGATOR as if it were
1579 * AGGREGATOR in that case
1581 if (BGP_DEBUG(as4
, AS4
))
1583 "[AS4] %s BGP not AS4 capable peer send"
1584 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1585 " it as if AGGREGATOR with AS_TRANS had been there",
1587 attr
->aggregator_as
= as4_aggregator
;
1588 /* sweep it under the carpet and simulate a "good"
1590 attr
->flag
|= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
));
1594 /* need to reconcile NEW_AS_PATH and AS_PATH */
1595 if (!ignore_as4_path
1596 && (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))) {
1597 newpath
= aspath_reconcile_as4(attr
->aspath
, as4_path
);
1598 aspath_unintern(&attr
->aspath
);
1599 attr
->aspath
= aspath_intern(newpath
);
1601 return BGP_ATTR_PARSE_PROCEED
;
1604 /* Community attribute. */
1605 static bgp_attr_parse_ret_t
1606 bgp_attr_community(struct bgp_attr_parser_args
*args
)
1608 struct peer
*const peer
= args
->peer
;
1609 struct attr
*const attr
= args
->attr
;
1610 const bgp_size_t length
= args
->length
;
1613 attr
->community
= NULL
;
1614 return BGP_ATTR_PARSE_PROCEED
;
1618 community_parse((u_int32_t
*)stream_pnt(peer
->ibuf
), length
);
1620 /* XXX: fix community_parse to use stream API and remove this */
1621 stream_forward_getp(peer
->ibuf
, length
);
1623 if (!attr
->community
)
1624 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1627 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1629 return BGP_ATTR_PARSE_PROCEED
;
1632 /* Originator ID attribute. */
1633 static bgp_attr_parse_ret_t
1634 bgp_attr_originator_id(struct bgp_attr_parser_args
*args
)
1636 struct peer
*const peer
= args
->peer
;
1637 struct attr
*const attr
= args
->attr
;
1638 const bgp_size_t length
= args
->length
;
1642 zlog_err("Bad originator ID length %d", length
);
1644 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1648 attr
->originator_id
.s_addr
= stream_get_ipv4(peer
->ibuf
);
1650 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
);
1652 return BGP_ATTR_PARSE_PROCEED
;
1655 /* Cluster list attribute. */
1656 static bgp_attr_parse_ret_t
1657 bgp_attr_cluster_list(struct bgp_attr_parser_args
*args
)
1659 struct peer
*const peer
= args
->peer
;
1660 struct attr
*const attr
= args
->attr
;
1661 const bgp_size_t length
= args
->length
;
1665 zlog_err("Bad cluster list length %d", length
);
1667 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1672 cluster_parse((struct in_addr
*)stream_pnt(peer
->ibuf
), length
);
1674 /* XXX: Fix cluster_parse to use stream API and then remove this */
1675 stream_forward_getp(peer
->ibuf
, length
);
1677 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
);
1679 return BGP_ATTR_PARSE_PROCEED
;
1682 /* Multiprotocol reachability information parse. */
1683 int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
1684 struct bgp_nlri
*mp_update
)
1688 iana_safi_t pkt_safi
;
1690 bgp_size_t nlri_len
;
1693 struct peer
*const peer
= args
->peer
;
1694 struct attr
*const attr
= args
->attr
;
1695 const bgp_size_t length
= args
->length
;
1697 /* Set end of packet. */
1698 s
= BGP_INPUT(peer
);
1699 start
= stream_get_getp(s
);
1701 /* safe to read statically sized header? */
1702 #define BGP_MP_REACH_MIN_SIZE 5
1703 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1704 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
)) {
1705 zlog_info("%s: %s sent invalid length, %lu", __func__
,
1706 peer
->host
, (unsigned long)length
);
1707 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1710 /* Load AFI, SAFI. */
1711 pkt_afi
= stream_getw(s
);
1712 pkt_safi
= stream_getc(s
);
1714 /* Convert AFI, SAFI to internal values, check. */
1715 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1716 /* Log if AFI or SAFI is unrecognized. This is not an error
1718 * the attribute is otherwise malformed.
1720 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1722 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1723 peer
->host
, pkt_afi
, pkt_safi
);
1724 return BGP_ATTR_PARSE_ERROR
;
1727 /* Get nexthop length. */
1728 attr
->mp_nexthop_len
= stream_getc(s
);
1730 if (LEN_LEFT
< attr
->mp_nexthop_len
) {
1732 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1733 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1734 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1737 /* Nexthop length check. */
1738 switch (attr
->mp_nexthop_len
) {
1739 case BGP_ATTR_NHLEN_IPV4
:
1740 stream_get(&attr
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1741 /* Probably needed for RFC 2283 */
1742 if (attr
->nexthop
.s_addr
== 0)
1743 memcpy(&attr
->nexthop
.s_addr
,
1744 &attr
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1746 case BGP_ATTR_NHLEN_VPNV4
:
1747 stream_getl(s
); /* RD high */
1748 stream_getl(s
); /* RD low */
1749 stream_get(&attr
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1751 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1752 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1753 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1754 stream_getl(s
); /* RD high */
1755 stream_getl(s
); /* RD low */
1757 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1759 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1760 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1761 if (attr
->mp_nexthop_len
1762 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1763 stream_getl(s
); /* RD high */
1764 stream_getl(s
); /* RD low */
1766 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1767 if (attr
->mp_nexthop_len
1768 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1769 stream_getl(s
); /* RD high */
1770 stream_getl(s
); /* RD low */
1772 stream_get(&attr
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1773 if (!IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
)) {
1774 char buf1
[INET6_ADDRSTRLEN
];
1775 char buf2
[INET6_ADDRSTRLEN
];
1777 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1779 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1782 &attr
->mp_nexthop_global
,
1783 buf1
, INET6_ADDRSTRLEN
),
1785 &attr
->mp_nexthop_local
, buf2
,
1788 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1792 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1793 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1794 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1798 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__
,
1800 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1805 if ((val
= stream_getc(s
)))
1807 "%s sent non-zero value, %u, for defunct SNPA-length field",
1811 /* must have nrli_len, what is left of the attribute */
1812 nlri_len
= LEN_LEFT
;
1813 if ((!nlri_len
) || (nlri_len
> STREAM_READABLE(s
))) {
1814 zlog_info("%s: (%s) Failed to read NLRI", __func__
, peer
->host
);
1815 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1818 mp_update
->afi
= afi
;
1819 mp_update
->safi
= safi
;
1820 mp_update
->nlri
= stream_pnt(s
);
1821 mp_update
->length
= nlri_len
;
1823 stream_forward_getp(s
, nlri_len
);
1825 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
);
1827 return BGP_ATTR_PARSE_PROCEED
;
1831 /* Multiprotocol unreachable parse */
1832 int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
1833 struct bgp_nlri
*mp_withdraw
)
1838 iana_safi_t pkt_safi
;
1840 u_int16_t withdraw_len
;
1841 struct peer
*const peer
= args
->peer
;
1842 struct attr
*const attr
= args
->attr
;
1843 const bgp_size_t length
= args
->length
;
1847 #define BGP_MP_UNREACH_MIN_SIZE 3
1848 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1849 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1851 pkt_afi
= stream_getw(s
);
1852 pkt_safi
= stream_getc(s
);
1854 /* Convert AFI, SAFI to internal values, check. */
1855 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1856 /* Log if AFI or SAFI is unrecognized. This is not an error
1858 * the attribute is otherwise malformed.
1860 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1862 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1863 peer
->host
, pkt_afi
, pkt_safi
);
1864 return BGP_ATTR_PARSE_ERROR
;
1867 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1869 mp_withdraw
->afi
= afi
;
1870 mp_withdraw
->safi
= safi
;
1871 mp_withdraw
->nlri
= stream_pnt(s
);
1872 mp_withdraw
->length
= withdraw_len
;
1874 stream_forward_getp(s
, withdraw_len
);
1876 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
);
1878 return BGP_ATTR_PARSE_PROCEED
;
1881 /* Large Community attribute. */
1882 static bgp_attr_parse_ret_t
1883 bgp_attr_large_community(struct bgp_attr_parser_args
*args
)
1885 struct peer
*const peer
= args
->peer
;
1886 struct attr
*const attr
= args
->attr
;
1887 const bgp_size_t length
= args
->length
;
1890 * Large community follows new attribute format.
1893 attr
->lcommunity
= NULL
;
1894 /* Empty extcomm doesn't seem to be invalid per se */
1895 return BGP_ATTR_PARSE_PROCEED
;
1899 lcommunity_parse((u_int8_t
*)stream_pnt(peer
->ibuf
), length
);
1900 /* XXX: fix ecommunity_parse to use stream API */
1901 stream_forward_getp(peer
->ibuf
, length
);
1903 if (!attr
->lcommunity
)
1904 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1907 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
1909 return BGP_ATTR_PARSE_PROCEED
;
1912 /* Extended Community attribute. */
1913 static bgp_attr_parse_ret_t
1914 bgp_attr_ext_communities(struct bgp_attr_parser_args
*args
)
1916 struct peer
*const peer
= args
->peer
;
1917 struct attr
*const attr
= args
->attr
;
1918 const bgp_size_t length
= args
->length
;
1922 attr
->ecommunity
= NULL
;
1923 /* Empty extcomm doesn't seem to be invalid per se */
1924 return BGP_ATTR_PARSE_PROCEED
;
1928 ecommunity_parse((u_int8_t
*)stream_pnt(peer
->ibuf
), length
);
1929 /* XXX: fix ecommunity_parse to use stream API */
1930 stream_forward_getp(peer
->ibuf
, length
);
1932 if (!attr
->ecommunity
)
1933 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1936 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
1938 /* Extract MAC mobility sequence number, if any. */
1939 attr
->mm_seqnum
= bgp_attr_mac_mobility_seqnum(attr
, &sticky
);
1940 attr
->sticky
= sticky
;
1942 return BGP_ATTR_PARSE_PROCEED
;
1945 /* Parse Tunnel Encap attribute in an UPDATE */
1946 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
1947 bgp_size_t length
, /* IN: attr's length field */
1948 struct attr
*attr
, /* IN: caller already allocated */
1949 u_char flag
, /* IN: attr's flags field */
1953 struct bgp_attr_encap_subtlv
*stlv_last
= NULL
;
1954 uint16_t tunneltype
= 0;
1956 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1958 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1959 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
1961 "Tunnel Encap attribute flag isn't optional and transitive %d",
1963 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
1964 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1969 if (BGP_ATTR_ENCAP
== type
) {
1970 /* read outer TLV type and length */
1971 uint16_t tlv_length
;
1975 "Tunnel Encap attribute not long enough to contain outer T,L");
1976 bgp_notify_send_with_data(
1977 peer
, BGP_NOTIFY_UPDATE_ERR
,
1978 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1981 tunneltype
= stream_getw(BGP_INPUT(peer
));
1982 tlv_length
= stream_getw(BGP_INPUT(peer
));
1985 if (tlv_length
!= length
) {
1986 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
1987 tlv_length
, length
);
1991 while (length
>= 4) {
1992 uint16_t subtype
= 0;
1993 uint16_t sublength
= 0;
1994 struct bgp_attr_encap_subtlv
*tlv
;
1996 if (BGP_ATTR_ENCAP
== type
) {
1997 subtype
= stream_getc(BGP_INPUT(peer
));
1998 sublength
= stream_getc(BGP_INPUT(peer
));
2002 subtype
= stream_getw(BGP_INPUT(peer
));
2003 sublength
= stream_getw(BGP_INPUT(peer
));
2008 if (sublength
> length
) {
2010 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
2012 bgp_notify_send_with_data(
2013 peer
, BGP_NOTIFY_UPDATE_ERR
,
2014 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
2018 /* alloc and copy sub-tlv */
2019 /* TBD make sure these are freed when attributes are released */
2020 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
2021 sizeof(struct bgp_attr_encap_subtlv
) - 1
2023 tlv
->type
= subtype
;
2024 tlv
->length
= sublength
;
2025 stream_get(tlv
->value
, peer
->ibuf
, sublength
);
2026 length
-= sublength
;
2028 /* attach tlv to encap chain */
2029 if (BGP_ATTR_ENCAP
== type
) {
2030 for (stlv_last
= attr
->encap_subtlvs
;
2031 stlv_last
&& stlv_last
->next
;
2032 stlv_last
= stlv_last
->next
)
2035 stlv_last
->next
= tlv
;
2037 attr
->encap_subtlvs
= tlv
;
2041 for (stlv_last
= attr
->vnc_subtlvs
;
2042 stlv_last
&& stlv_last
->next
;
2043 stlv_last
= stlv_last
->next
)
2046 stlv_last
->next
= tlv
;
2048 attr
->vnc_subtlvs
= tlv
;
2055 if (BGP_ATTR_ENCAP
== type
) {
2056 attr
->encap_tunneltype
= tunneltype
;
2060 /* spurious leftover data */
2062 "Tunnel Encap attribute length is bad: %d leftover octets",
2064 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2065 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2073 /* Prefix SID attribute
2074 * draft-ietf-idr-bgp-prefix-sid-05
2076 static bgp_attr_parse_ret_t
2077 bgp_attr_prefix_sid(struct bgp_attr_parser_args
*args
,
2078 struct bgp_nlri
*mp_update
)
2080 struct peer
*const peer
= args
->peer
;
2081 struct attr
*const attr
= args
->attr
;
2084 u_int32_t label_index
;
2085 struct in6_addr ipv6_sid
;
2086 u_int32_t srgb_base
;
2087 u_int32_t srgb_range
;
2090 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
2092 type
= stream_getc(peer
->ibuf
);
2093 length
= stream_getw(peer
->ibuf
);
2095 if (type
== BGP_PREFIX_SID_LABEL_INDEX
) {
2096 if (length
!= BGP_PREFIX_SID_LABEL_INDEX_LENGTH
) {
2098 "Prefix SID label index length is %d instead of %d",
2099 length
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
2100 return bgp_attr_malformed(
2101 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2105 /* Ignore flags and reserved */
2106 stream_getc(peer
->ibuf
);
2107 stream_getw(peer
->ibuf
);
2109 /* Fetch the label index and see if it is valid. */
2110 label_index
= stream_getl(peer
->ibuf
);
2111 if (label_index
== BGP_INVALID_LABEL_INDEX
)
2112 return bgp_attr_malformed(
2113 args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2116 /* Store label index; subsequently, we'll check on
2118 attr
->label_index
= label_index
;
2121 * Ignore the Label index attribute unless received for
2125 if (!mp_update
->length
2126 || mp_update
->safi
!= SAFI_LABELED_UNICAST
)
2127 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
2130 /* Placeholder code for the IPv6 SID type */
2131 else if (type
== BGP_PREFIX_SID_IPV6
) {
2132 if (length
!= BGP_PREFIX_SID_IPV6_LENGTH
) {
2133 zlog_err("Prefix SID IPv6 length is %d instead of %d",
2134 length
, BGP_PREFIX_SID_IPV6_LENGTH
);
2135 return bgp_attr_malformed(
2136 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2140 /* Ignore reserved */
2141 stream_getc(peer
->ibuf
);
2142 stream_getw(peer
->ibuf
);
2144 stream_get(&ipv6_sid
, peer
->ibuf
, 16);
2147 /* Placeholder code for the Originator SRGB type */
2148 else if (type
== BGP_PREFIX_SID_ORIGINATOR_SRGB
) {
2150 stream_getw(peer
->ibuf
);
2154 if (length
% BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
) {
2156 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2157 length
, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
);
2158 return bgp_attr_malformed(
2159 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2163 srgb_count
= length
/ BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
;
2165 for (int i
= 0; i
< srgb_count
; i
++) {
2166 stream_get(&srgb_base
, peer
->ibuf
, 3);
2167 stream_get(&srgb_range
, peer
->ibuf
, 3);
2171 return BGP_ATTR_PARSE_PROCEED
;
2174 /* BGP unknown attribute treatment. */
2175 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2177 bgp_size_t total
= args
->total
;
2178 struct transit
*transit
;
2179 struct peer
*const peer
= args
->peer
;
2180 struct attr
*const attr
= args
->attr
;
2181 u_char
*const startp
= args
->startp
;
2182 const u_char type
= args
->type
;
2183 const u_char flag
= args
->flags
;
2184 const bgp_size_t length
= args
->length
;
2186 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2188 "%s Unknown attribute is received (type %d, length %d)",
2189 peer
->host
, type
, length
);
2191 /* Forward read pointer of input stream. */
2192 stream_forward_getp(peer
->ibuf
, length
);
2194 /* If any of the mandatory well-known attributes are not recognized,
2195 then the Error Subcode is set to Unrecognized Well-known
2196 Attribute. The Data field contains the unrecognized attribute
2197 (type, length and value). */
2198 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2199 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2203 /* Unrecognized non-transitive optional attributes must be quietly
2204 ignored and not passed along to other BGP peers. */
2205 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2206 return BGP_ATTR_PARSE_PROCEED
;
2208 /* If a path with recognized transitive optional attribute is
2209 accepted and passed along to other BGP peers and the Partial bit
2210 in the Attribute Flags octet is set to 1 by some previous AS, it
2211 is not set back to 0 by the current AS. */
2212 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2214 /* Store transitive attribute to the end of attr->transit. */
2216 attr
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2218 transit
= attr
->transit
;
2221 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2222 transit
->length
+ total
);
2224 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2226 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2227 transit
->length
+= total
;
2229 return BGP_ATTR_PARSE_PROCEED
;
2232 /* Well-known attribute check. */
2233 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2237 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2239 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2240 return BGP_ATTR_PARSE_PROCEED
;
2242 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2243 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2244 are present, it should. Check for any other attribute being present
2247 if (attr
->flag
== ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))
2248 return BGP_ATTR_PARSE_PROCEED
;
2250 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2251 type
= BGP_ATTR_ORIGIN
;
2253 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2254 type
= BGP_ATTR_AS_PATH
;
2256 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2258 * NLRI is empty. We can't easily check NLRI empty here though.
2260 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2261 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2262 type
= BGP_ATTR_NEXT_HOP
;
2264 if (peer
->sort
== BGP_PEER_IBGP
2265 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2266 type
= BGP_ATTR_LOCAL_PREF
;
2269 zlog_warn("%s Missing well-known attribute %s.", peer
->host
,
2270 lookup_msg(attr_str
, type
, NULL
));
2271 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2272 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2274 return BGP_ATTR_PARSE_ERROR
;
2276 return BGP_ATTR_PARSE_PROCEED
;
2279 /* Read attribute of update packet. This function is called from
2280 bgp_update_receive() in bgp_packet.c. */
2281 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2282 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2283 struct bgp_nlri
*mp_withdraw
)
2289 u_char
*startp
, *endp
;
2291 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2292 /* we need the as4_path only until we have synthesized the as_path with
2294 /* same goes for as4_aggregator */
2295 struct aspath
*as4_path
= NULL
;
2296 as_t as4_aggregator
= 0;
2297 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2299 /* Initialize bitmap. */
2300 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2302 /* End pointer of BGP attribute. */
2303 endp
= BGP_INPUT_PNT(peer
) + size
;
2305 /* Get attributes to the end of attribute length. */
2306 while (BGP_INPUT_PNT(peer
) < endp
) {
2307 /* Check remaining length check.*/
2308 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2309 /* XXX warning: long int format, int arg (arg 5) */
2311 "%s: error BGP attribute length %lu is smaller than min len",
2313 (unsigned long)(endp
2314 - STREAM_PNT(BGP_INPUT(peer
))));
2316 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2317 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2318 return BGP_ATTR_PARSE_ERROR
;
2321 /* Fetch attribute flag and type. */
2322 startp
= BGP_INPUT_PNT(peer
);
2323 /* "The lower-order four bits of the Attribute Flags octet are
2324 unused. They MUST be zero when sent and MUST be ignored when
2326 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2327 type
= stream_getc(BGP_INPUT(peer
));
2329 /* Check whether Extended-Length applies and is in bounds */
2330 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2331 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2333 "%s: Extended length set, but just %lu bytes of attr header",
2335 (unsigned long)(endp
2336 - STREAM_PNT(BGP_INPUT(peer
))));
2338 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2339 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2340 return BGP_ATTR_PARSE_ERROR
;
2343 /* Check extended attribue length bit. */
2344 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2345 length
= stream_getw(BGP_INPUT(peer
));
2347 length
= stream_getc(BGP_INPUT(peer
));
2349 /* If any attribute appears more than once in the UPDATE
2350 message, then the Error Subcode is set to Malformed Attribute
2353 if (CHECK_BITMAP(seen
, type
)) {
2355 "%s: error BGP attribute type %d appears twice in a message",
2358 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2359 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2360 return BGP_ATTR_PARSE_ERROR
;
2363 /* Set type to bitmap to check duplicate attribute. `type' is
2364 unsigned char so it never overflow bitmap range. */
2366 SET_BITMAP(seen
, type
);
2368 /* Overflow check. */
2369 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2371 if (attr_endp
> endp
) {
2373 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2374 peer
->host
, type
, length
, size
, attr_endp
,
2376 bgp_notify_send_with_data(
2377 peer
, BGP_NOTIFY_UPDATE_ERR
,
2378 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, startp
,
2379 attr_endp
- startp
);
2380 return BGP_ATTR_PARSE_ERROR
;
2383 struct bgp_attr_parser_args attr_args
= {
2390 .total
= attr_endp
- startp
,
2394 /* If any recognized attribute has Attribute Flags that conflict
2395 with the Attribute Type Code, then the Error Subcode is set
2397 Attribute Flags Error. The Data field contains the erroneous
2398 attribute (type, length and value). */
2399 if (bgp_attr_flag_invalid(&attr_args
)) {
2400 bgp_attr_parse_ret_t ret
;
2401 ret
= bgp_attr_malformed(
2402 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2404 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2409 /* OK check attribute and store it's value. */
2411 case BGP_ATTR_ORIGIN
:
2412 ret
= bgp_attr_origin(&attr_args
);
2414 case BGP_ATTR_AS_PATH
:
2415 ret
= bgp_attr_aspath(&attr_args
);
2417 case BGP_ATTR_AS4_PATH
:
2418 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2420 case BGP_ATTR_NEXT_HOP
:
2421 ret
= bgp_attr_nexthop(&attr_args
);
2423 case BGP_ATTR_MULTI_EXIT_DISC
:
2424 ret
= bgp_attr_med(&attr_args
);
2426 case BGP_ATTR_LOCAL_PREF
:
2427 ret
= bgp_attr_local_pref(&attr_args
);
2429 case BGP_ATTR_ATOMIC_AGGREGATE
:
2430 ret
= bgp_attr_atomic(&attr_args
);
2432 case BGP_ATTR_AGGREGATOR
:
2433 ret
= bgp_attr_aggregator(&attr_args
);
2435 case BGP_ATTR_AS4_AGGREGATOR
:
2436 ret
= bgp_attr_as4_aggregator(&attr_args
,
2438 &as4_aggregator_addr
);
2440 case BGP_ATTR_COMMUNITIES
:
2441 ret
= bgp_attr_community(&attr_args
);
2443 case BGP_ATTR_LARGE_COMMUNITIES
:
2444 ret
= bgp_attr_large_community(&attr_args
);
2446 case BGP_ATTR_ORIGINATOR_ID
:
2447 ret
= bgp_attr_originator_id(&attr_args
);
2449 case BGP_ATTR_CLUSTER_LIST
:
2450 ret
= bgp_attr_cluster_list(&attr_args
);
2452 case BGP_ATTR_MP_REACH_NLRI
:
2453 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2455 case BGP_ATTR_MP_UNREACH_NLRI
:
2456 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2458 case BGP_ATTR_EXT_COMMUNITIES
:
2459 ret
= bgp_attr_ext_communities(&attr_args
);
2464 case BGP_ATTR_ENCAP
:
2465 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2468 case BGP_ATTR_PREFIX_SID
:
2469 ret
= bgp_attr_prefix_sid(&attr_args
, mp_update
);
2472 ret
= bgp_attr_unknown(&attr_args
);
2476 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2477 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2478 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2479 ret
= BGP_ATTR_PARSE_ERROR
;
2482 /* If hard error occured immediately return to the caller. */
2483 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2484 zlog_warn("%s: Attribute %s, parse error", peer
->host
,
2485 lookup_msg(attr_str
, type
, NULL
));
2487 aspath_unintern(&as4_path
);
2490 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2493 "%s: Attribute %s, parse error - treating as withdrawal",
2494 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2496 aspath_unintern(&as4_path
);
2500 /* Check the fetched length. */
2501 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2502 zlog_warn("%s: BGP attribute %s, fetch error",
2503 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2504 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2505 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2507 aspath_unintern(&as4_path
);
2508 return BGP_ATTR_PARSE_ERROR
;
2512 /* Check final read pointer is same as end pointer. */
2513 if (BGP_INPUT_PNT(peer
) != endp
) {
2514 zlog_warn("%s: BGP attribute %s, length mismatch", peer
->host
,
2515 lookup_msg(attr_str
, type
, NULL
));
2516 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2517 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2519 aspath_unintern(&as4_path
);
2520 return BGP_ATTR_PARSE_ERROR
;
2523 /* Check all mandatory well-known attributes are present */
2525 bgp_attr_parse_ret_t ret
;
2526 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2528 aspath_unintern(&as4_path
);
2534 * At this place we can see whether we got AS4_PATH and/or
2535 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2536 * We can not do this before we've read all attributes because
2537 * the as4 handling does not say whether AS4_PATH has to be sent
2538 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2539 * in relationship to AGGREGATOR.
2540 * So, to be defensive, we are not relying on any order and read
2541 * all attributes first, including these 32bit ones, and now,
2542 * afterwards, we look what and if something is to be done for as4.
2544 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2547 /* actually... this doesn't ever return failure currently, but
2548 * better safe than sorry */
2549 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2550 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2551 &as4_aggregator_addr
)) {
2552 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2553 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2555 aspath_unintern(&as4_path
);
2556 return BGP_ATTR_PARSE_ERROR
;
2559 /* At this stage, we have done all fiddling with as4, and the
2560 * resulting info is in attr->aggregator resp. attr->aspath
2561 * so we can chuck as4_aggregator and as4_path alltogether in
2562 * order to save memory
2565 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2566 /* The flag that we got this is still there, but that does not
2571 * The "rest" of the code does nothing with as4_aggregator.
2572 * there is no memory attached specifically which is not part
2574 * so ignoring just means do nothing.
2577 * Finally do the checks on the aspath we did not do yet
2578 * because we waited for a potentially synthesized aspath.
2580 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2581 ret
= bgp_attr_aspath_check(peer
, attr
);
2582 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2585 /* Finally intern unknown attribute. */
2587 attr
->transit
= transit_intern(attr
->transit
);
2588 if (attr
->encap_subtlvs
)
2589 attr
->encap_subtlvs
=
2590 encap_intern(attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2592 if (attr
->vnc_subtlvs
)
2594 encap_intern(attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2597 return BGP_ATTR_PARSE_PROCEED
;
2600 size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
, afi_t afi
,
2601 safi_t safi
, struct bpacket_attr_vec_arr
*vecarr
,
2606 iana_safi_t pkt_safi
;
2609 /* Set extended bit always to encode the attribute length as 2 bytes */
2610 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2611 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2612 sizep
= stream_get_endp(s
);
2613 stream_putw(s
, 0); /* Marker: Attribute length. */
2616 /* Convert AFI, SAFI to values for packet. */
2617 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2619 stream_putw(s
, pkt_afi
); /* AFI */
2620 stream_putc(s
, pkt_safi
); /* SAFI */
2624 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
))
2625 nh_afi
= peer_cap_enhe(peer
, afi
, safi
) ? AFI_IP6
: AFI_IP
;
2627 nh_afi
= BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->mp_nexthop_len
);
2630 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2635 case SAFI_MULTICAST
:
2636 case SAFI_LABELED_UNICAST
:
2638 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2642 stream_putl(s
, 0); /* RD = 0, per RFC */
2644 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2649 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2658 case SAFI_MULTICAST
:
2659 case SAFI_LABELED_UNICAST
:
2661 if (attr
->mp_nexthop_len
2662 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2664 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
);
2665 stream_put(s
, &attr
->mp_nexthop_global
,
2667 stream_put(s
, &attr
->mp_nexthop_local
,
2670 stream_putc(s
, IPV6_MAX_BYTELEN
);
2671 stream_put(s
, &attr
->mp_nexthop_global
,
2675 case SAFI_MPLS_VPN
: {
2676 if (attr
->mp_nexthop_len
2677 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2679 stream_putl(s
, 0); /* RD = 0, per RFC */
2681 stream_put(s
, &attr
->mp_nexthop_global
,
2683 } else if (attr
->mp_nexthop_len
2684 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2686 stream_putl(s
, 0); /* RD = 0, per RFC */
2688 stream_put(s
, &attr
->mp_nexthop_global
,
2690 stream_putl(s
, 0); /* RD = 0, per RFC */
2692 stream_put(s
, &attr
->mp_nexthop_local
,
2697 stream_putc(s
, IPV6_MAX_BYTELEN
);
2698 stream_put(s
, &attr
->mp_nexthop_global
,
2707 "Bad nexthop when sening to %s, AFI %u SAFI %u nhlen %d",
2708 peer
->host
, afi
, safi
, attr
->mp_nexthop_len
);
2717 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2718 struct prefix
*p
, struct prefix_rd
*prd
,
2719 mpls_label_t
*label
, int addpath_encode
,
2720 u_int32_t addpath_tx_id
, struct attr
*attr
)
2722 if (safi
== SAFI_MPLS_VPN
) {
2724 stream_putl(s
, addpath_tx_id
);
2725 /* Label, RD, Prefix write. */
2726 stream_putc(s
, p
->prefixlen
+ 88);
2727 stream_put(s
, label
, BGP_LABEL_BYTES
);
2728 stream_put(s
, prd
->val
, 8);
2729 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2730 } else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
2731 /* EVPN prefix - contents depend on type */
2732 bgp_evpn_encode_prefix(s
, p
, prd
, label
, attr
, addpath_encode
,
2734 } else if (safi
== SAFI_LABELED_UNICAST
) {
2735 /* Prefix write with label. */
2736 stream_put_labeled_prefix(s
, p
, label
);
2738 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2741 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2743 int size
= PSIZE(p
->prefixlen
);
2744 if (safi
== SAFI_MPLS_VPN
)
2746 else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2747 size
+= 232; // TODO: Maximum possible for type-2, type-3 and
2753 * Encodes the tunnel encapsulation attribute,
2754 * and with ENABLE_BGP_VNC the VNC attribute which uses
2755 * almost the same TLV format
2757 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2758 struct stream
*s
, struct attr
*attr
,
2761 unsigned int attrlenfield
= 0;
2762 unsigned int attrhdrlen
= 0;
2763 struct bgp_attr_encap_subtlv
*subtlvs
;
2764 struct bgp_attr_encap_subtlv
*st
;
2765 const char *attrname
;
2767 if (!attr
|| (attrtype
== BGP_ATTR_ENCAP
2768 && (!attr
->encap_tunneltype
2769 || attr
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2773 case BGP_ATTR_ENCAP
:
2774 attrname
= "Tunnel Encap";
2775 subtlvs
= attr
->encap_subtlvs
;
2776 if (subtlvs
== NULL
) /* nothing to do */
2779 * The tunnel encap attr has an "outer" tlv.
2781 * L = total length of subtlvs,
2782 * V = concatenated subtlvs.
2784 attrlenfield
= 2 + 2; /* T + L */
2785 attrhdrlen
= 1 + 1; /* subTLV T + L */
2791 subtlvs
= attr
->vnc_subtlvs
;
2792 if (subtlvs
== NULL
) /* nothing to do */
2794 attrlenfield
= 0; /* no outer T + L */
2795 attrhdrlen
= 2 + 2; /* subTLV T + L */
2803 /* compute attr length */
2804 for (st
= subtlvs
; st
; st
= st
->next
) {
2805 attrlenfield
+= (attrhdrlen
+ st
->length
);
2808 if (attrlenfield
> 0xffff) {
2809 zlog_info("%s attribute is too long (length=%d), can't send it",
2810 attrname
, attrlenfield
);
2814 if (attrlenfield
> 0xff) {
2815 /* 2-octet length field */
2816 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2817 | BGP_ATTR_FLAG_EXTLEN
);
2818 stream_putc(s
, attrtype
);
2819 stream_putw(s
, attrlenfield
& 0xffff);
2821 /* 1-octet length field */
2822 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2823 stream_putc(s
, attrtype
);
2824 stream_putc(s
, attrlenfield
& 0xff);
2827 if (attrtype
== BGP_ATTR_ENCAP
) {
2828 /* write outer T+L */
2829 stream_putw(s
, attr
->encap_tunneltype
);
2830 stream_putw(s
, attrlenfield
- 4);
2833 /* write each sub-tlv */
2834 for (st
= subtlvs
; st
; st
= st
->next
) {
2835 if (attrtype
== BGP_ATTR_ENCAP
) {
2836 stream_putc(s
, st
->type
);
2837 stream_putc(s
, st
->length
);
2840 stream_putw(s
, st
->type
);
2841 stream_putw(s
, st
->length
);
2844 stream_put(s
, st
->value
, st
->length
);
2848 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
2850 /* Set MP attribute length. Don't count the (2) bytes used to encode
2852 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
2855 /* Make attribute packet. */
2856 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
2857 struct stream
*s
, struct attr
*attr
,
2858 struct bpacket_attr_vec_arr
*vecarr
,
2859 struct prefix
*p
, afi_t afi
, safi_t safi
,
2860 struct peer
*from
, struct prefix_rd
*prd
,
2861 mpls_label_t
*label
, int addpath_encode
,
2862 u_int32_t addpath_tx_id
)
2865 size_t aspath_sizep
;
2866 struct aspath
*aspath
;
2867 int send_as4_path
= 0;
2868 int send_as4_aggregator
= 0;
2869 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2874 /* Remember current pointer. */
2875 cp
= stream_get_endp(s
);
2878 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
2879 && !peer_cap_enhe(peer
, afi
, safi
))) {
2880 size_t mpattrlen_pos
= 0;
2882 mpattrlen_pos
= bgp_packet_mpattr_start(s
, peer
, afi
, safi
,
2884 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
2885 addpath_encode
, addpath_tx_id
, attr
);
2886 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2889 /* Origin attribute. */
2890 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2891 stream_putc(s
, BGP_ATTR_ORIGIN
);
2893 stream_putc(s
, attr
->origin
);
2895 /* AS path attribute. */
2897 /* If remote-peer is EBGP */
2898 if (peer
->sort
== BGP_PEER_EBGP
2899 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2900 PEER_FLAG_AS_PATH_UNCHANGED
)
2901 || attr
->aspath
->segments
== NULL
)
2902 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2903 PEER_FLAG_RSERVER_CLIENT
))) {
2904 aspath
= aspath_dup(attr
->aspath
);
2906 /* Even though we may not be configured for confederations we
2908 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2909 aspath
= aspath_delete_confed_seq(aspath
);
2911 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
2912 /* Stuff our path CONFED_ID on the front */
2913 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
2915 if (peer
->change_local_as
) {
2916 /* If replace-as is specified, we only use the
2917 change_local_as when
2918 advertising routes. */
2921 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
2922 aspath
= aspath_add_seq(aspath
,
2925 aspath
= aspath_add_seq(aspath
,
2926 peer
->change_local_as
);
2928 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
2931 } else if (peer
->sort
== BGP_PEER_CONFED
) {
2932 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2934 aspath
= aspath_dup(attr
->aspath
);
2935 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
2937 aspath
= attr
->aspath
;
2939 /* If peer is not AS4 capable, then:
2940 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2941 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2943 * types are in it (i.e. exclude them if they are there)
2944 * AND do this only if there is at least one asnum > 65535 in the
2946 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2948 * all ASnums > 65535 to BGP_AS_TRANS
2951 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
2952 stream_putc(s
, BGP_ATTR_AS_PATH
);
2953 aspath_sizep
= stream_get_endp(s
);
2955 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
2957 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2960 if (!use32bit
&& aspath_has_as4(aspath
))
2962 1; /* we'll do this later, at the correct place */
2964 /* Nexthop attribute. */
2965 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
2966 && !peer_cap_enhe(peer
, afi
, safi
)) {
2967 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2968 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2969 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2970 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2973 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2974 } else if (peer_cap_enhe(from
, afi
, safi
)) {
2976 * Likely this is the case when an IPv4 prefix was
2978 * Extended Next-hop capability and now being advertised
2981 * Setting the mandatory (ipv4) next-hop attribute here
2983 * implicit next-hop self with correct (ipv4 address
2986 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2987 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2988 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2991 stream_put_ipv4(s
, 0);
2995 /* MED attribute. */
2996 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
2997 || bgp
->maxmed_active
) {
2998 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
2999 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3001 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
3005 /* Local preference. */
3006 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
3007 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3008 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3010 stream_putl(s
, attr
->local_pref
);
3013 /* Atomic aggregate. */
3014 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3015 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3016 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3021 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3022 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3023 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3024 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3027 /* AS4 capable peer */
3029 stream_putl(s
, attr
->aggregator_as
);
3031 /* 2-byte AS peer */
3034 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3036 if (attr
->aggregator_as
> 65535) {
3037 stream_putw(s
, BGP_AS_TRANS
);
3039 /* we have to send AS4_AGGREGATOR, too.
3040 * we'll do that later in order to send
3041 * attributes in ascending
3044 send_as4_aggregator
= 1;
3046 stream_putw(s
, (u_int16_t
)attr
->aggregator_as
);
3048 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3051 /* Community attribute. */
3052 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3053 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3054 if (attr
->community
->size
* 4 > 255) {
3055 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3056 | BGP_ATTR_FLAG_TRANS
3057 | BGP_ATTR_FLAG_EXTLEN
);
3058 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3059 stream_putw(s
, attr
->community
->size
* 4);
3061 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3062 | BGP_ATTR_FLAG_TRANS
);
3063 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3064 stream_putc(s
, attr
->community
->size
* 4);
3066 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3070 * Large Community attribute.
3072 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3073 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3074 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3075 if (lcom_length(attr
->lcommunity
) > 255) {
3076 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3077 | BGP_ATTR_FLAG_TRANS
3078 | BGP_ATTR_FLAG_EXTLEN
);
3079 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3080 stream_putw(s
, lcom_length(attr
->lcommunity
));
3082 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3083 | BGP_ATTR_FLAG_TRANS
);
3084 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3085 stream_putc(s
, lcom_length(attr
->lcommunity
));
3087 stream_put(s
, attr
->lcommunity
->val
,
3088 lcom_length(attr
->lcommunity
));
3091 /* Route Reflector. */
3092 if (peer
->sort
== BGP_PEER_IBGP
&& from
3093 && from
->sort
== BGP_PEER_IBGP
) {
3094 /* Originator ID. */
3095 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3096 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3099 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3100 stream_put_in_addr(s
, &attr
->originator_id
);
3102 stream_put_in_addr(s
, &from
->remote_id
);
3105 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3106 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3108 if (attr
->cluster
) {
3109 stream_putc(s
, attr
->cluster
->length
+ 4);
3110 /* If this peer configuration's parent BGP has
3112 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3113 stream_put_in_addr(s
, &bgp
->cluster_id
);
3115 stream_put_in_addr(s
, &bgp
->router_id
);
3116 stream_put(s
, attr
->cluster
->list
,
3117 attr
->cluster
->length
);
3120 /* If this peer configuration's parent BGP has
3122 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3123 stream_put_in_addr(s
, &bgp
->cluster_id
);
3125 stream_put_in_addr(s
, &bgp
->router_id
);
3129 /* Extended Communities attribute. */
3130 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3131 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3132 if (peer
->sort
== BGP_PEER_IBGP
3133 || peer
->sort
== BGP_PEER_CONFED
) {
3134 if (attr
->ecommunity
->size
* 8 > 255) {
3135 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3136 | BGP_ATTR_FLAG_TRANS
3137 | BGP_ATTR_FLAG_EXTLEN
);
3138 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3139 stream_putw(s
, attr
->ecommunity
->size
* 8);
3141 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3142 | BGP_ATTR_FLAG_TRANS
);
3143 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3144 stream_putc(s
, attr
->ecommunity
->size
* 8);
3146 stream_put(s
, attr
->ecommunity
->val
,
3147 attr
->ecommunity
->size
* 8);
3151 int ecom_tr_size
= 0;
3154 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3155 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3158 if (CHECK_FLAG(tbit
,
3159 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3166 if (ecom_tr_size
* 8 > 255) {
3169 BGP_ATTR_FLAG_OPTIONAL
3170 | BGP_ATTR_FLAG_TRANS
3171 | BGP_ATTR_FLAG_EXTLEN
);
3173 BGP_ATTR_EXT_COMMUNITIES
);
3174 stream_putw(s
, ecom_tr_size
* 8);
3178 BGP_ATTR_FLAG_OPTIONAL
3179 | BGP_ATTR_FLAG_TRANS
);
3181 BGP_ATTR_EXT_COMMUNITIES
);
3182 stream_putc(s
, ecom_tr_size
* 8);
3185 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3186 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3191 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3194 stream_put(s
, pnt
, 8);
3200 /* Label index attribute. */
3201 if (safi
== SAFI_LABELED_UNICAST
) {
3202 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3203 u_int32_t label_index
;
3205 label_index
= attr
->label_index
;
3207 if (label_index
!= BGP_INVALID_LABEL_INDEX
) {
3208 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3209 | BGP_ATTR_FLAG_TRANS
);
3210 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3212 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3214 BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3215 stream_putc(s
, 0); // reserved
3216 stream_putw(s
, 0); // flags
3217 stream_putl(s
, label_index
);
3222 if (send_as4_path
) {
3223 /* If the peer is NOT As4 capable, AND */
3224 /* there are ASnums > 65535 in path THEN
3225 * give out AS4_PATH */
3227 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3229 * Hm, I wonder... confederation things *should* only be at
3230 * the beginning of an aspath, right? Then we should use
3231 * aspath_delete_confed_seq for this, because it is already
3233 * Folks, talk to me: what is reasonable here!?
3235 aspath
= aspath_delete_confed_seq(aspath
);
3237 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3238 | BGP_ATTR_FLAG_EXTLEN
);
3239 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3240 aspath_sizep
= stream_get_endp(s
);
3242 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3245 if (aspath
!= attr
->aspath
)
3246 aspath_free(aspath
);
3248 if (send_as4_aggregator
) {
3249 /* send AS4_AGGREGATOR, at this place */
3250 /* this section of code moved here in order to ensure the
3252 * *ascending* order of attributes
3254 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3255 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3257 stream_putl(s
, attr
->aggregator_as
);
3258 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3261 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3262 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3263 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3264 /* Tunnel Encap attribute */
3265 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3269 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3273 /* Unknown transit attribute. */
3275 stream_put(s
, attr
->transit
->val
, attr
->transit
->length
);
3277 /* Return total size of attribute. */
3278 return stream_get_endp(s
) - cp
;
3281 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3283 unsigned long attrlen_pnt
;
3285 iana_safi_t pkt_safi
;
3287 /* Set extended bit always to encode the attribute length as 2 bytes */
3288 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3289 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3291 attrlen_pnt
= stream_get_endp(s
);
3292 stream_putw(s
, 0); /* Length of this attribute. */
3294 /* Convert AFI, SAFI to values for packet. */
3295 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3297 stream_putw(s
, pkt_afi
);
3298 stream_putc(s
, pkt_safi
);
3303 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3304 safi_t safi
, struct prefix_rd
*prd
,
3305 mpls_label_t
*label
, int addpath_encode
,
3306 u_int32_t addpath_tx_id
, struct attr
*attr
)
3308 u_char wlabel
[3] = {0x80, 0x00, 0x00};
3310 if (safi
== SAFI_LABELED_UNICAST
)
3311 label
= (mpls_label_t
*)wlabel
;
3313 return bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
3314 addpath_encode
, addpath_tx_id
, attr
);
3317 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3319 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3322 /* Initialization of attribute. */
3323 void bgp_attr_init(void)
3335 void bgp_attr_finish(void)
3340 ecommunity_finish();
3341 lcommunity_finish();
3347 /* Make attribute packet. */
3348 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3349 struct prefix
*prefix
)
3354 struct aspath
*aspath
;
3355 int addpath_encode
= 0;
3356 u_int32_t addpath_tx_id
= 0;
3358 /* Remember current pointer. */
3359 cp
= stream_get_endp(s
);
3361 /* Place holder of length. */
3364 /* Origin attribute. */
3365 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3366 stream_putc(s
, BGP_ATTR_ORIGIN
);
3368 stream_putc(s
, attr
->origin
);
3370 aspath
= attr
->aspath
;
3372 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3373 stream_putc(s
, BGP_ATTR_AS_PATH
);
3374 aspath_lenp
= stream_get_endp(s
);
3377 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3379 /* Nexthop attribute. */
3380 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3381 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3382 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3383 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3385 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3388 /* MED attribute. */
3389 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3390 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3391 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3393 stream_putl(s
, attr
->med
);
3396 /* Local preference. */
3397 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3398 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3399 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3401 stream_putl(s
, attr
->local_pref
);
3404 /* Atomic aggregate. */
3405 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3406 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3407 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3412 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3413 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3414 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3416 stream_putl(s
, attr
->aggregator_as
);
3417 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3420 /* Community attribute. */
3421 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3422 if (attr
->community
->size
* 4 > 255) {
3423 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3424 | BGP_ATTR_FLAG_TRANS
3425 | BGP_ATTR_FLAG_EXTLEN
);
3426 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3427 stream_putw(s
, attr
->community
->size
* 4);
3429 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3430 | BGP_ATTR_FLAG_TRANS
);
3431 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3432 stream_putc(s
, attr
->community
->size
* 4);
3434 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3437 /* Large Community attribute. */
3438 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3439 if (lcom_length(attr
->lcommunity
) > 255) {
3440 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3441 | BGP_ATTR_FLAG_TRANS
3442 | BGP_ATTR_FLAG_EXTLEN
);
3443 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3444 stream_putw(s
, lcom_length(attr
->lcommunity
));
3446 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3447 | BGP_ATTR_FLAG_TRANS
);
3448 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3449 stream_putc(s
, lcom_length(attr
->lcommunity
));
3452 stream_put(s
, attr
->lcommunity
->val
, lcom_length(attr
->lcommunity
));
3455 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3456 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
3457 && (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3458 || attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3461 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3462 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3463 sizep
= stream_get_endp(s
);
3466 stream_putc(s
, 0); /* Marker: Attribute length. */
3467 stream_putw(s
, AFI_IP6
); /* AFI */
3468 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3471 stream_putc(s
, attr
->mp_nexthop_len
);
3472 stream_put(s
, &attr
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3473 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3474 stream_put(s
, &attr
->mp_nexthop_local
,
3481 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3484 /* Set MP attribute length. */
3485 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3489 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3490 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
3491 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3492 | BGP_ATTR_FLAG_TRANS
);
3493 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3495 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3496 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3497 stream_putc(s
, 0); // reserved
3498 stream_putw(s
, 0); // flags
3499 stream_putl(s
, attr
->label_index
);
3503 /* Return total size of attribute. */
3504 len
= stream_get_endp(s
) - cp
- 2;
3505 stream_putw_at(s
, cp
, len
);