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_PMSI_TUNNEL
, "PMSI_TUNNEL_ATTRIBUTE"},
78 {BGP_ATTR_ENCAP
, "ENCAP"},
80 {BGP_ATTR_VNC
, "VNC"},
82 {BGP_ATTR_LARGE_COMMUNITIES
, "LARGE_COMMUNITY"},
83 {BGP_ATTR_PREFIX_SID
, "PREFIX_SID"},
86 static const struct message attr_flag_str
[] =
88 {BGP_ATTR_FLAG_OPTIONAL
, "Optional"},
89 {BGP_ATTR_FLAG_TRANS
, "Transitive"},
90 {BGP_ATTR_FLAG_PARTIAL
, "Partial"},
91 /* bgp_attr_flags_diagnose() relies on this bit being last in
93 {BGP_ATTR_FLAG_EXTLEN
, "Extended Length"},
96 static struct hash
*cluster_hash
;
98 static void *cluster_hash_alloc(void *p
)
100 const struct cluster_list
*val
= (const struct cluster_list
*)p
;
101 struct cluster_list
*cluster
;
103 cluster
= XMALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
104 cluster
->length
= val
->length
;
106 if (cluster
->length
) {
107 cluster
->list
= XMALLOC(MTYPE_CLUSTER_VAL
, val
->length
);
108 memcpy(cluster
->list
, val
->list
, val
->length
);
110 cluster
->list
= NULL
;
117 /* Cluster list related functions. */
118 static struct cluster_list
*cluster_parse(struct in_addr
*pnt
, int length
)
120 struct cluster_list tmp
;
121 struct cluster_list
*cluster
;
126 cluster
= hash_get(cluster_hash
, &tmp
, cluster_hash_alloc
);
131 int cluster_loop_check(struct cluster_list
*cluster
, struct in_addr originator
)
135 for (i
= 0; i
< cluster
->length
/ 4; i
++)
136 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
141 static unsigned int cluster_hash_key_make(void *p
)
143 const struct cluster_list
*cluster
= p
;
145 return jhash(cluster
->list
, cluster
->length
, 0);
148 static int cluster_hash_cmp(const void *p1
, const void *p2
)
150 const struct cluster_list
*cluster1
= p1
;
151 const struct cluster_list
*cluster2
= p2
;
153 return (cluster1
->length
== cluster2
->length
154 && memcmp(cluster1
->list
, cluster2
->list
, cluster1
->length
)
158 static void cluster_free(struct cluster_list
*cluster
)
161 XFREE(MTYPE_CLUSTER_VAL
, cluster
->list
);
162 XFREE(MTYPE_CLUSTER
, cluster
);
165 static struct cluster_list
*cluster_intern(struct cluster_list
*cluster
)
167 struct cluster_list
*find
;
169 find
= hash_get(cluster_hash
, cluster
, cluster_hash_alloc
);
175 void cluster_unintern(struct cluster_list
*cluster
)
180 if (cluster
->refcnt
== 0) {
181 hash_release(cluster_hash
, cluster
);
182 cluster_free(cluster
);
186 static void cluster_init(void)
188 cluster_hash
= hash_create(cluster_hash_key_make
,
193 static void cluster_finish(void)
195 hash_clean(cluster_hash
, (void (*)(void *))cluster_free
);
196 hash_free(cluster_hash
);
200 static struct hash
*encap_hash
= NULL
;
202 static struct hash
*vnc_hash
= NULL
;
205 struct bgp_attr_encap_subtlv
*encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
207 struct bgp_attr_encap_subtlv
*new;
208 struct bgp_attr_encap_subtlv
*tail
;
209 struct bgp_attr_encap_subtlv
*p
;
211 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
212 int size
= sizeof(struct bgp_attr_encap_subtlv
) + p
->length
;
214 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
217 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
220 memcpy(tail
, p
, size
);
227 static void encap_free(struct bgp_attr_encap_subtlv
*p
)
229 struct bgp_attr_encap_subtlv
*next
;
233 XFREE(MTYPE_ENCAP_TLV
, p
);
238 void bgp_attr_flush_encap(struct attr
*attr
)
243 if (attr
->encap_subtlvs
) {
244 encap_free(attr
->encap_subtlvs
);
245 attr
->encap_subtlvs
= NULL
;
248 if (attr
->vnc_subtlvs
) {
249 encap_free(attr
->vnc_subtlvs
);
250 attr
->vnc_subtlvs
= NULL
;
256 * Compare encap sub-tlv chains
261 * This algorithm could be made faster if needed
263 static int encap_same(struct bgp_attr_encap_subtlv
*h1
,
264 struct bgp_attr_encap_subtlv
*h2
)
266 struct bgp_attr_encap_subtlv
*p
;
267 struct bgp_attr_encap_subtlv
*q
;
271 if (h1
== NULL
|| h2
== NULL
)
274 for (p
= h1
; p
; p
= p
->next
) {
275 for (q
= h2
; q
; q
= q
->next
) {
276 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
277 && !memcmp(p
->value
, q
->value
, p
->length
)) {
286 for (p
= h2
; p
; p
= p
->next
) {
287 for (q
= h1
; q
; q
= q
->next
) {
288 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
289 && !memcmp(p
->value
, q
->value
, p
->length
)) {
301 static void *encap_hash_alloc(void *p
)
303 /* Encap structure is already allocated. */
314 static struct bgp_attr_encap_subtlv
*
315 encap_intern(struct bgp_attr_encap_subtlv
*encap
, encap_subtlv_type type
)
317 struct bgp_attr_encap_subtlv
*find
;
318 struct hash
*hash
= encap_hash
;
320 if (type
== VNC_SUBTLV_TYPE
)
324 find
= hash_get(hash
, encap
, encap_hash_alloc
);
332 static void encap_unintern(struct bgp_attr_encap_subtlv
**encapp
,
333 encap_subtlv_type type
)
335 struct bgp_attr_encap_subtlv
*encap
= *encapp
;
339 if (encap
->refcnt
== 0) {
340 struct hash
*hash
= encap_hash
;
342 if (type
== VNC_SUBTLV_TYPE
)
345 hash_release(hash
, encap
);
351 static unsigned int encap_hash_key_make(void *p
)
353 const struct bgp_attr_encap_subtlv
*encap
= p
;
355 return jhash(encap
->value
, encap
->length
, 0);
358 static int encap_hash_cmp(const void *p1
, const void *p2
)
360 return encap_same((struct bgp_attr_encap_subtlv
*)p1
,
361 (struct bgp_attr_encap_subtlv
*)p2
);
364 static void encap_init(void)
366 encap_hash
= hash_create(encap_hash_key_make
,
370 vnc_hash
= hash_create(encap_hash_key_make
,
376 static void encap_finish(void)
378 hash_clean(encap_hash
, (void (*)(void *))encap_free
);
379 hash_free(encap_hash
);
382 hash_clean(vnc_hash
, (void (*)(void *))encap_free
);
388 static bool overlay_index_same(const struct attr
*a1
, const struct attr
*a2
)
396 return !memcmp(&(a1
->evpn_overlay
), &(a2
->evpn_overlay
),
397 sizeof(struct overlay_index
));
400 /* Unknown transit attribute. */
401 static struct hash
*transit_hash
;
403 static void transit_free(struct transit
*transit
)
406 XFREE(MTYPE_TRANSIT_VAL
, transit
->val
);
407 XFREE(MTYPE_TRANSIT
, transit
);
410 static void *transit_hash_alloc(void *p
)
412 /* Transit structure is already allocated. */
416 static struct transit
*transit_intern(struct transit
*transit
)
418 struct transit
*find
;
420 find
= hash_get(transit_hash
, transit
, transit_hash_alloc
);
422 transit_free(transit
);
428 void transit_unintern(struct transit
*transit
)
433 if (transit
->refcnt
== 0) {
434 hash_release(transit_hash
, transit
);
435 transit_free(transit
);
439 static unsigned int transit_hash_key_make(void *p
)
441 const struct transit
*transit
= p
;
443 return jhash(transit
->val
, transit
->length
, 0);
446 static int transit_hash_cmp(const void *p1
, const void *p2
)
448 const struct transit
*transit1
= p1
;
449 const struct transit
*transit2
= p2
;
451 return (transit1
->length
== transit2
->length
452 && memcmp(transit1
->val
, transit2
->val
, transit1
->length
) == 0);
455 static void transit_init(void)
457 transit_hash
= hash_create(transit_hash_key_make
,
462 static void transit_finish(void)
464 hash_clean(transit_hash
, (void (*)(void *))transit_free
);
465 hash_free(transit_hash
);
469 /* Attribute hash routines. */
470 static struct hash
*attrhash
;
472 /* Shallow copy of an attribute
473 * Though, not so shallow that it doesn't copy the contents
474 * of the attr_extra pointed to by 'extra'
476 void bgp_attr_dup(struct attr
*new, struct attr
*orig
)
481 unsigned long int attr_count(void)
483 return attrhash
->count
;
486 unsigned long int attr_unknown_count(void)
488 return transit_hash
->count
;
491 unsigned int attrhash_key_make(void *p
)
493 const struct attr
*attr
= (struct attr
*)p
;
495 #define MIX(val) key = jhash_1word(val, key)
496 #define MIX3(a, b, c) key = jhash_3words((a), (b), (c), key)
498 MIX3(attr
->origin
, attr
->nexthop
.s_addr
, attr
->med
);
499 MIX3(attr
->local_pref
, attr
->aggregator_as
, attr
->aggregator_addr
.s_addr
);
500 MIX3(attr
->weight
, attr
->mp_nexthop_global_in
.s_addr
,
501 attr
->originator_id
.s_addr
);
502 MIX3(attr
->tag
, attr
->label
, attr
->label_index
);
505 MIX(aspath_key_make(attr
->aspath
));
507 MIX(community_hash_make(attr
->community
));
509 if (attr
->lcommunity
)
510 MIX(lcommunity_hash_make(attr
->lcommunity
));
511 if (attr
->ecommunity
)
512 MIX(ecommunity_hash_make(attr
->ecommunity
));
514 MIX(cluster_hash_key_make(attr
->cluster
));
516 MIX(transit_hash_key_make(attr
->transit
));
517 if (attr
->encap_subtlvs
)
518 MIX(encap_hash_key_make(attr
->encap_subtlvs
));
520 if (attr
->vnc_subtlvs
)
521 MIX(encap_hash_key_make(attr
->vnc_subtlvs
));
523 MIX(attr
->mp_nexthop_len
);
524 key
= jhash(attr
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
525 key
= jhash(attr
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
530 int attrhash_cmp(const void *p1
, const void *p2
)
532 const struct attr
*attr1
= p1
;
533 const struct attr
*attr2
= p2
;
535 if (attr1
->flag
== attr2
->flag
&& attr1
->origin
== attr2
->origin
536 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
537 && attr1
->aspath
== attr2
->aspath
538 && attr1
->community
== attr2
->community
&& attr1
->med
== attr2
->med
539 && attr1
->local_pref
== attr2
->local_pref
540 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
) {
541 if (attr1
->aggregator_as
== attr2
->aggregator_as
542 && attr1
->aggregator_addr
.s_addr
543 == attr2
->aggregator_addr
.s_addr
544 && attr1
->weight
== attr2
->weight
545 && attr1
->tag
== attr2
->tag
546 && attr1
->label_index
== attr2
->label_index
547 && attr1
->mp_nexthop_len
== attr2
->mp_nexthop_len
548 && attr1
->ecommunity
== attr2
->ecommunity
549 && attr1
->lcommunity
== attr2
->lcommunity
550 && attr1
->cluster
== attr2
->cluster
551 && attr1
->transit
== attr2
->transit
552 && (attr1
->encap_tunneltype
== attr2
->encap_tunneltype
)
553 && encap_same(attr1
->encap_subtlvs
, attr2
->encap_subtlvs
)
555 && encap_same(attr1
->vnc_subtlvs
, attr2
->vnc_subtlvs
)
557 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_global
,
558 &attr2
->mp_nexthop_global
)
559 && IPV6_ADDR_SAME(&attr1
->mp_nexthop_local
,
560 &attr2
->mp_nexthop_local
)
561 && IPV4_ADDR_SAME(&attr1
->mp_nexthop_global_in
,
562 &attr2
->mp_nexthop_global_in
)
563 && IPV4_ADDR_SAME(&attr1
->originator_id
,
564 &attr2
->originator_id
)
565 && overlay_index_same(attr1
, attr2
))
572 static void attrhash_init(void)
574 attrhash
= hash_create(attrhash_key_make
,
580 * special for hash_clean below
582 static void attr_vfree(void *attr
)
584 XFREE(MTYPE_ATTR
, attr
);
587 static void attrhash_finish(void)
589 hash_clean(attrhash
, attr_vfree
);
594 static void attr_show_all_iterator(struct hash_backet
*backet
, struct vty
*vty
)
596 struct attr
*attr
= backet
->data
;
598 vty_out(vty
, "attr[%ld] nexthop %s\n", attr
->refcnt
,
599 inet_ntoa(attr
->nexthop
));
602 void attr_show_all(struct vty
*vty
)
604 hash_iterate(attrhash
, (void (*)(struct hash_backet
*,
605 void *))attr_show_all_iterator
,
609 static void *bgp_attr_hash_alloc(void *p
)
611 struct attr
*val
= (struct attr
*)p
;
614 attr
= XMALLOC(MTYPE_ATTR
, sizeof(struct attr
));
616 if (val
->encap_subtlvs
) {
617 val
->encap_subtlvs
= NULL
;
620 if (val
->vnc_subtlvs
) {
621 val
->vnc_subtlvs
= NULL
;
628 /* Internet argument attribute. */
629 struct attr
*bgp_attr_intern(struct attr
*attr
)
633 /* Intern referenced strucutre. */
635 if (!attr
->aspath
->refcnt
)
636 attr
->aspath
= aspath_intern(attr
->aspath
);
638 attr
->aspath
->refcnt
++;
640 if (attr
->community
) {
641 if (!attr
->community
->refcnt
)
642 attr
->community
= community_intern(attr
->community
);
644 attr
->community
->refcnt
++;
647 if (attr
->ecommunity
) {
648 if (!attr
->ecommunity
->refcnt
)
649 attr
->ecommunity
= ecommunity_intern(attr
->ecommunity
);
651 attr
->ecommunity
->refcnt
++;
653 if (attr
->lcommunity
) {
654 if (!attr
->lcommunity
->refcnt
)
655 attr
->lcommunity
= lcommunity_intern(attr
->lcommunity
);
657 attr
->lcommunity
->refcnt
++;
660 if (!attr
->cluster
->refcnt
)
661 attr
->cluster
= cluster_intern(attr
->cluster
);
663 attr
->cluster
->refcnt
++;
666 if (!attr
->transit
->refcnt
)
667 attr
->transit
= transit_intern(attr
->transit
);
669 attr
->transit
->refcnt
++;
671 if (attr
->encap_subtlvs
) {
672 if (!attr
->encap_subtlvs
->refcnt
)
673 attr
->encap_subtlvs
= encap_intern(attr
->encap_subtlvs
,
676 attr
->encap_subtlvs
->refcnt
++;
679 if (attr
->vnc_subtlvs
) {
680 if (!attr
->vnc_subtlvs
->refcnt
)
681 attr
->vnc_subtlvs
= encap_intern(attr
->vnc_subtlvs
,
684 attr
->vnc_subtlvs
->refcnt
++;
688 /* At this point, attr only contains intern'd pointers. that means
689 * if we find it in attrhash, it has all the same pointers and we
690 * correctly updated the refcounts on these.
691 * If we don't find it, we need to allocate a one because in all
692 * cases this returns a new reference to a hashed attr, but the input
694 find
= (struct attr
*)hash_get(attrhash
, attr
, bgp_attr_hash_alloc
);
700 /* Make network statement's attribute. */
701 struct attr
*bgp_attr_default_set(struct attr
*attr
, u_char origin
)
703 memset(attr
, 0, sizeof(struct attr
));
705 attr
->origin
= origin
;
706 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
707 attr
->aspath
= aspath_empty();
708 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
709 attr
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
711 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
712 attr
->label
= MPLS_INVALID_LABEL
;
713 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
714 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
719 /* Create the attributes for an aggregate */
720 struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, u_char origin
,
721 struct aspath
*aspath
,
722 struct community
*community
, int as_set
,
723 u_char atomic_aggregate
)
728 memset(&attr
, 0, sizeof(struct attr
));
730 /* Origin attribute. */
731 attr
.origin
= origin
;
732 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
734 /* AS path attribute. */
736 attr
.aspath
= aspath_intern(aspath
);
738 attr
.aspath
= aspath_empty();
739 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
741 /* Next hop attribute. */
742 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
745 u_int32_t gshut
= COMMUNITY_GSHUT
;
747 /* If we are not shutting down ourselves and we are
748 * aggregating a route that contains the GSHUT community we
749 * need to remove that community when creating the aggregate */
750 if (!bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
) &&
751 community_include(community
, gshut
)) {
752 community_del_val(community
, &gshut
);
755 attr
.community
= community
;
756 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
759 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
760 bgp_attr_add_gshut_community(&attr
);
763 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
764 attr
.label
= MPLS_INVALID_LABEL
;
765 attr
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
766 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
767 if (!as_set
|| atomic_aggregate
)
768 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
769 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
770 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
771 attr
.aggregator_as
= bgp
->confed_id
;
773 attr
.aggregator_as
= bgp
->as
;
774 attr
.aggregator_addr
= bgp
->router_id
;
775 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
776 attr
.label
= MPLS_INVALID_LABEL
;
778 new = bgp_attr_intern(&attr
);
780 aspath_unintern(&new->aspath
);
784 /* Unintern just the sub-components of the attr, but not the attr */
785 void bgp_attr_unintern_sub(struct attr
*attr
)
787 /* aspath refcount shoud be decrement. */
789 aspath_unintern(&attr
->aspath
);
790 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
));
793 community_unintern(&attr
->community
);
794 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
));
796 if (attr
->ecommunity
)
797 ecommunity_unintern(&attr
->ecommunity
);
798 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
));
800 if (attr
->lcommunity
)
801 lcommunity_unintern(&attr
->lcommunity
);
802 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
));
805 cluster_unintern(attr
->cluster
);
806 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
));
809 transit_unintern(attr
->transit
);
811 if (attr
->encap_subtlvs
)
812 encap_unintern(&attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
815 if (attr
->vnc_subtlvs
)
816 encap_unintern(&attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
821 * We have some show commands that let you experimentally
822 * apply a route-map. When we apply the route-map
823 * we are reseting values but not saving them for
824 * posterity via intern'ing( because route-maps don't
825 * do that) but at this point in time we need
826 * to compare the new attr to the old and if the
827 * routemap has changed it we need to, as Snoop Dog says,
828 * Drop it like it's hot
830 void bgp_attr_undup(struct attr
*new, struct attr
*old
)
832 if (new->aspath
!= old
->aspath
)
833 aspath_free(new->aspath
);
835 if (new->community
!= old
->community
)
836 community_free(new->community
);
838 if (new->ecommunity
!= old
->ecommunity
)
839 ecommunity_free(&new->ecommunity
);
841 if (new->lcommunity
!= old
->lcommunity
)
842 lcommunity_free(&new->lcommunity
);
846 /* Free bgp attribute and aspath. */
847 void bgp_attr_unintern(struct attr
**pattr
)
849 struct attr
*attr
= *pattr
;
853 /* Decrement attribute reference. */
858 /* If reference becomes zero then free attribute object. */
859 if (attr
->refcnt
== 0) {
860 ret
= hash_release(attrhash
, attr
);
862 XFREE(MTYPE_ATTR
, attr
);
866 bgp_attr_unintern_sub(&tmp
);
869 void bgp_attr_flush(struct attr
*attr
)
871 if (attr
->aspath
&& !attr
->aspath
->refcnt
) {
872 aspath_free(attr
->aspath
);
875 if (attr
->community
&& !attr
->community
->refcnt
) {
876 community_free(attr
->community
);
877 attr
->community
= NULL
;
880 if (attr
->ecommunity
&& !attr
->ecommunity
->refcnt
)
881 ecommunity_free(&attr
->ecommunity
);
882 if (attr
->lcommunity
&& !attr
->lcommunity
->refcnt
)
883 lcommunity_free(&attr
->lcommunity
);
884 if (attr
->cluster
&& !attr
->cluster
->refcnt
) {
885 cluster_free(attr
->cluster
);
886 attr
->cluster
= NULL
;
888 if (attr
->transit
&& !attr
->transit
->refcnt
) {
889 transit_free(attr
->transit
);
890 attr
->transit
= NULL
;
892 if (attr
->encap_subtlvs
&& !attr
->encap_subtlvs
->refcnt
) {
893 encap_free(attr
->encap_subtlvs
);
894 attr
->encap_subtlvs
= NULL
;
897 if (attr
->vnc_subtlvs
&& !attr
->vnc_subtlvs
->refcnt
) {
898 encap_free(attr
->vnc_subtlvs
);
899 attr
->vnc_subtlvs
= NULL
;
904 /* Implement draft-scudder-idr-optional-transitive behaviour and
905 * avoid resetting sessions for malformed attributes which are
906 * are partial/optional and hence where the error likely was not
907 * introduced by the sending neighbour.
909 static bgp_attr_parse_ret_t
910 bgp_attr_malformed(struct bgp_attr_parser_args
*args
, u_char subcode
,
913 struct peer
*const peer
= args
->peer
;
914 const u_int8_t flags
= args
->flags
;
915 /* startp and length must be special-cased, as whether or not to
916 * send the attribute data with the NOTIFY depends on the error,
917 * the caller therefore signals this with the seperate length argument
919 u_char
*notify_datap
= (length
> 0 ? args
->startp
: NULL
);
921 /* Only relax error handling for eBGP peers */
922 if (peer
->sort
!= BGP_PEER_EBGP
) {
923 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
924 notify_datap
, length
);
925 return BGP_ATTR_PARSE_ERROR
;
928 /* Adjust the stream getp to the end of the attribute, in case we can
929 * still proceed but the caller hasn't read all the attribute.
931 stream_set_getp(BGP_INPUT(peer
),
932 (args
->startp
- STREAM_DATA(BGP_INPUT(peer
)))
935 switch (args
->type
) {
936 /* where an attribute is relatively inconsequential, e.g. it does not
937 * affect route selection, and can be safely ignored, then any such
938 * attributes which are malformed should just be ignored and the route
939 * processed as normal.
941 case BGP_ATTR_AS4_AGGREGATOR
:
942 case BGP_ATTR_AGGREGATOR
:
943 case BGP_ATTR_ATOMIC_AGGREGATE
:
944 return BGP_ATTR_PARSE_PROCEED
;
946 /* Core attributes, particularly ones which may influence route
947 * selection, should always cause session resets
949 case BGP_ATTR_ORIGIN
:
950 case BGP_ATTR_AS_PATH
:
951 case BGP_ATTR_NEXT_HOP
:
952 case BGP_ATTR_MULTI_EXIT_DISC
:
953 case BGP_ATTR_LOCAL_PREF
:
954 case BGP_ATTR_COMMUNITIES
:
955 case BGP_ATTR_ORIGINATOR_ID
:
956 case BGP_ATTR_CLUSTER_LIST
:
957 case BGP_ATTR_MP_REACH_NLRI
:
958 case BGP_ATTR_MP_UNREACH_NLRI
:
959 case BGP_ATTR_EXT_COMMUNITIES
:
960 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
961 notify_datap
, length
);
962 return BGP_ATTR_PARSE_ERROR
;
965 /* Partial optional attributes that are malformed should not cause
966 * the whole session to be reset. Instead treat it as a withdrawal
967 * of the routes, if possible.
969 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)
970 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
971 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
))
972 return BGP_ATTR_PARSE_WITHDRAW
;
974 /* default to reset */
975 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
978 /* Find out what is wrong with the path attribute flag bits and log the error.
979 "Flag bits" here stand for Optional, Transitive and Partial, but not for
980 Extended Length. Checking O/T/P bits at once implies, that the attribute
981 being diagnosed is defined by RFC as either a "well-known" or an "optional,
982 non-transitive" attribute. */
984 bgp_attr_flags_diagnose(struct bgp_attr_parser_args
*args
,
985 u_int8_t desired_flags
/* how RFC says it must be */
989 u_char real_flags
= args
->flags
;
990 const u_int8_t attr_code
= args
->type
;
992 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
993 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
994 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
995 if (CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
996 != CHECK_FLAG(real_flags
, attr_flag_str
[i
].key
)) {
997 zlog_err("%s attribute must%s be flagged as \"%s\"",
998 lookup_msg(attr_str
, attr_code
, NULL
),
999 CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1002 attr_flag_str
[i
].str
);
1007 "Strange, %s called for attr %s, but no problem found with flags"
1008 " (real flags 0x%x, desired 0x%x)",
1009 __func__
, lookup_msg(attr_str
, attr_code
, NULL
),
1010 real_flags
, desired_flags
);
1014 /* Required flags for attributes. EXTLEN will be masked off when testing,
1015 * as will PARTIAL for optional+transitive attributes.
1017 const u_int8_t attr_flags_values
[] = {
1018 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1019 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1020 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1021 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1022 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1023 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1024 [BGP_ATTR_AGGREGATOR
] =
1025 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1026 [BGP_ATTR_COMMUNITIES
] =
1027 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1028 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1029 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1030 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1031 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1032 [BGP_ATTR_EXT_COMMUNITIES
] =
1033 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1034 [BGP_ATTR_AS4_PATH
] =
1035 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1036 [BGP_ATTR_AS4_AGGREGATOR
] =
1037 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1038 [BGP_ATTR_PMSI_TUNNEL
] =
1039 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1040 [BGP_ATTR_LARGE_COMMUNITIES
] =
1041 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1042 [BGP_ATTR_PREFIX_SID
] =
1043 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1045 static const size_t attr_flags_values_max
= array_size(attr_flags_values
) - 1;
1047 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args
*args
)
1049 u_int8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1050 const u_int8_t flags
= args
->flags
;
1051 const u_int8_t attr_code
= args
->type
;
1053 /* there may be attributes we don't know about */
1054 if (attr_code
> attr_flags_values_max
)
1056 if (attr_flags_values
[attr_code
] == 0)
1059 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1063 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL
, flags
)
1064 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS
, flags
)) {
1066 "%s well-known attributes must have transitive flag set (%x)",
1067 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1071 /* "For well-known attributes and for optional non-transitive
1073 * the Partial bit MUST be set to 0."
1075 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
)) {
1076 if (!CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)) {
1078 "%s well-known attribute "
1079 "must NOT have the partial flag set (%x)",
1080 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1083 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1084 && !CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)) {
1086 "%s optional + transitive attribute "
1087 "must NOT have the partial flag set (%x)",
1088 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1093 /* Optional transitive attributes may go through speakers that don't
1094 * reocgnise them and set the Partial bit.
1096 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1097 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
))
1098 SET_FLAG(mask
, BGP_ATTR_FLAG_PARTIAL
);
1100 if ((flags
& ~mask
) == attr_flags_values
[attr_code
])
1103 bgp_attr_flags_diagnose(args
, attr_flags_values
[attr_code
]);
1107 /* Get origin attribute of the update message. */
1108 static bgp_attr_parse_ret_t
bgp_attr_origin(struct bgp_attr_parser_args
*args
)
1110 struct peer
*const peer
= args
->peer
;
1111 struct attr
*const attr
= args
->attr
;
1112 const bgp_size_t length
= args
->length
;
1114 /* If any recognized attribute has Attribute Length that conflicts
1115 with the expected length (based on the attribute type code), then
1116 the Error Subcode is set to Attribute Length Error. The Data
1117 field contains the erroneous attribute (type, length and
1120 zlog_err("Origin attribute length is not one %d", length
);
1121 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1125 /* Fetch origin attribute. */
1126 attr
->origin
= stream_getc(BGP_INPUT(peer
));
1128 /* If the ORIGIN attribute has an undefined value, then the Error
1129 Subcode is set to Invalid Origin Attribute. The Data field
1130 contains the unrecognized attribute (type, length and value). */
1131 if ((attr
->origin
!= BGP_ORIGIN_IGP
) && (attr
->origin
!= BGP_ORIGIN_EGP
)
1132 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
)) {
1133 zlog_err("Origin attribute value is invalid %d", attr
->origin
);
1134 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1138 /* Set oring attribute flag. */
1139 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
1144 /* Parse AS path information. This function is wrapper of
1146 static int bgp_attr_aspath(struct bgp_attr_parser_args
*args
)
1148 struct attr
*const attr
= args
->attr
;
1149 struct peer
*const peer
= args
->peer
;
1150 const bgp_size_t length
= args
->length
;
1153 * peer with AS4 => will get 4Byte ASnums
1154 * otherwise, will get 16 Bit
1156 attr
->aspath
= aspath_parse(peer
->curr
, length
,
1157 CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
));
1159 /* In case of IBGP, length will be zero. */
1160 if (!attr
->aspath
) {
1161 zlog_err("Malformed AS path from %s, length is %d", peer
->host
,
1163 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1167 /* Set aspath attribute flag. */
1168 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
1170 return BGP_ATTR_PARSE_PROCEED
;
1173 static bgp_attr_parse_ret_t
bgp_attr_aspath_check(struct peer
*const peer
,
1174 struct attr
*const attr
)
1176 /* These checks were part of bgp_attr_aspath, but with
1177 * as4 we should to check aspath things when
1178 * aspath synthesizing with as4_path has already taken place.
1179 * Otherwise we check ASPATH and use the synthesized thing, and that is
1181 * So do the checks later, i.e. here
1183 struct bgp
*bgp
= peer
->bgp
;
1184 struct aspath
*aspath
;
1186 /* Confederation sanity check. */
1187 if ((peer
->sort
== BGP_PEER_CONFED
1188 && !aspath_left_confed_check(attr
->aspath
))
1189 || (peer
->sort
== BGP_PEER_EBGP
1190 && aspath_confed_check(attr
->aspath
))) {
1191 zlog_err("Malformed AS path from %s", peer
->host
);
1192 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1193 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1194 return BGP_ATTR_PARSE_ERROR
;
1197 /* First AS check for EBGP. */
1198 if (bgp
!= NULL
&& bgp_flag_check(bgp
, BGP_FLAG_ENFORCE_FIRST_AS
)) {
1199 if (peer
->sort
== BGP_PEER_EBGP
1200 && !aspath_firstas_check(attr
->aspath
, peer
->as
)) {
1201 zlog_err("%s incorrect first AS (must be %u)",
1202 peer
->host
, peer
->as
);
1203 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1204 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1205 return BGP_ATTR_PARSE_ERROR
;
1209 /* local-as prepend */
1210 if (peer
->change_local_as
1211 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)) {
1212 aspath
= aspath_dup(attr
->aspath
);
1213 aspath
= aspath_add_seq(aspath
, peer
->change_local_as
);
1214 aspath_unintern(&attr
->aspath
);
1215 attr
->aspath
= aspath_intern(aspath
);
1218 return BGP_ATTR_PARSE_PROCEED
;
1221 /* Parse AS4 path information. This function is another wrapper of
1223 static int bgp_attr_as4_path(struct bgp_attr_parser_args
*args
,
1224 struct aspath
**as4_path
)
1226 struct peer
*const peer
= args
->peer
;
1227 struct attr
*const attr
= args
->attr
;
1228 const bgp_size_t length
= args
->length
;
1230 *as4_path
= aspath_parse(peer
->curr
, length
, 1);
1232 /* In case of IBGP, length will be zero. */
1234 zlog_err("Malformed AS4 path from %s, length is %d", peer
->host
,
1236 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1240 /* Set aspath attribute flag. */
1241 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
);
1243 return BGP_ATTR_PARSE_PROCEED
;
1246 /* Nexthop attribute. */
1247 static bgp_attr_parse_ret_t
bgp_attr_nexthop(struct bgp_attr_parser_args
*args
)
1249 struct peer
*const peer
= args
->peer
;
1250 struct attr
*const attr
= args
->attr
;
1251 const bgp_size_t length
= args
->length
;
1253 in_addr_t nexthop_h
, nexthop_n
;
1255 /* Check nexthop attribute length. */
1257 zlog_err("Nexthop attribute length isn't four [%d]", length
);
1259 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1263 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1264 attribute must result in a NOTIFICATION message (this is implemented
1266 At the same time, semantically incorrect NEXT_HOP is more likely to
1268 logged locally (this is implemented somewhere else). The UPDATE
1270 gets ignored in any of these cases. */
1271 nexthop_n
= stream_get_ipv4(peer
->curr
);
1272 nexthop_h
= ntohl(nexthop_n
);
1273 if ((IPV4_NET0(nexthop_h
) || IPV4_NET127(nexthop_h
)
1274 || IPV4_CLASS_DE(nexthop_h
))
1277 ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1279 char buf
[INET_ADDRSTRLEN
];
1280 inet_ntop(AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1281 zlog_err("Martian nexthop %s", buf
);
1282 return bgp_attr_malformed(
1283 args
, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
, args
->total
);
1286 attr
->nexthop
.s_addr
= nexthop_n
;
1287 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1289 return BGP_ATTR_PARSE_PROCEED
;
1292 /* MED atrribute. */
1293 static bgp_attr_parse_ret_t
bgp_attr_med(struct bgp_attr_parser_args
*args
)
1295 struct peer
*const peer
= args
->peer
;
1296 struct attr
*const attr
= args
->attr
;
1297 const bgp_size_t length
= args
->length
;
1301 zlog_err("MED attribute length isn't four [%d]", length
);
1303 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1307 attr
->med
= stream_getl(peer
->curr
);
1309 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
1311 return BGP_ATTR_PARSE_PROCEED
;
1314 /* Local preference attribute. */
1315 static bgp_attr_parse_ret_t
1316 bgp_attr_local_pref(struct bgp_attr_parser_args
*args
)
1318 struct peer
*const peer
= args
->peer
;
1319 struct attr
*const attr
= args
->attr
;
1320 const bgp_size_t length
= args
->length
;
1324 zlog_err("LOCAL_PREF attribute length isn't 4 [%u]", length
);
1325 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1329 /* If it is contained in an UPDATE message that is received from an
1330 external peer, then this attribute MUST be ignored by the
1331 receiving speaker. */
1332 if (peer
->sort
== BGP_PEER_EBGP
) {
1333 stream_forward_getp(peer
->curr
, length
);
1334 return BGP_ATTR_PARSE_PROCEED
;
1337 attr
->local_pref
= stream_getl(peer
->curr
);
1339 /* Set the local-pref flag. */
1340 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1342 return BGP_ATTR_PARSE_PROCEED
;
1345 /* Atomic aggregate. */
1346 static int bgp_attr_atomic(struct bgp_attr_parser_args
*args
)
1348 struct attr
*const attr
= args
->attr
;
1349 const bgp_size_t length
= args
->length
;
1353 zlog_err("ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1355 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1359 /* Set atomic aggregate flag. */
1360 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
1362 return BGP_ATTR_PARSE_PROCEED
;
1365 /* Aggregator attribute */
1366 static int bgp_attr_aggregator(struct bgp_attr_parser_args
*args
)
1368 struct peer
*const peer
= args
->peer
;
1369 struct attr
*const attr
= args
->attr
;
1370 const bgp_size_t length
= args
->length
;
1374 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1375 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1378 if (length
!= wantedlen
) {
1379 zlog_err("AGGREGATOR attribute length isn't %u [%u]", wantedlen
,
1381 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1385 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1386 attr
->aggregator_as
= stream_getl(peer
->curr
);
1388 attr
->aggregator_as
= stream_getw(peer
->curr
);
1389 attr
->aggregator_addr
.s_addr
= stream_get_ipv4(peer
->curr
);
1391 /* Set atomic aggregate flag. */
1392 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
1394 return BGP_ATTR_PARSE_PROCEED
;
1397 /* New Aggregator attribute */
1398 static bgp_attr_parse_ret_t
1399 bgp_attr_as4_aggregator(struct bgp_attr_parser_args
*args
,
1400 as_t
*as4_aggregator_as
,
1401 struct in_addr
*as4_aggregator_addr
)
1403 struct peer
*const peer
= args
->peer
;
1404 struct attr
*const attr
= args
->attr
;
1405 const bgp_size_t length
= args
->length
;
1408 zlog_err("New Aggregator length is not 8 [%d]", length
);
1409 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1413 *as4_aggregator_as
= stream_getl(peer
->curr
);
1414 as4_aggregator_addr
->s_addr
= stream_get_ipv4(peer
->curr
);
1416 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
);
1418 return BGP_ATTR_PARSE_PROCEED
;
1421 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1423 static bgp_attr_parse_ret_t
1424 bgp_attr_munge_as4_attrs(struct peer
*const peer
, struct attr
*const attr
,
1425 struct aspath
*as4_path
, as_t as4_aggregator
,
1426 struct in_addr
*as4_aggregator_addr
)
1428 int ignore_as4_path
= 0;
1429 struct aspath
*newpath
;
1431 if (!attr
->aspath
) {
1432 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1434 * checked that all well-known, mandatory attributes were
1437 * Can only be a problem with peer itself - hard error
1439 return BGP_ATTR_PARSE_ERROR
;
1442 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) {
1443 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1445 * It is worth a warning though, because the peer really
1446 * should not send them
1448 if (BGP_DEBUG(as4
, AS4
)) {
1449 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1450 zlog_debug("[AS4] %s %s AS4_PATH", peer
->host
,
1451 "AS4 capable peer, yet it sent");
1454 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1455 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1457 "AS4 capable peer, yet it sent");
1460 return BGP_ATTR_PARSE_PROCEED
;
1463 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1464 * because that may override AS4_PATH
1466 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
))) {
1467 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
1469 * if the as_number in aggregator is not AS_TRANS,
1470 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1471 * and the Aggregator shall be taken as
1472 * info on the aggregating node, and the AS_PATH
1473 * shall be taken as the AS_PATH
1475 * the Aggregator shall be ignored and the
1476 * AS4_AGGREGATOR shall be taken as the
1477 * Aggregating node and the AS_PATH is to be
1478 * constructed "as in all other cases"
1480 if (attr
->aggregator_as
!= BGP_AS_TRANS
) {
1482 if (BGP_DEBUG(as4
, AS4
))
1484 "[AS4] %s BGP not AS4 capable peer"
1485 " send AGGREGATOR != AS_TRANS and"
1486 " AS4_AGGREGATOR, so ignore"
1487 " AS4_AGGREGATOR and AS4_PATH",
1489 ignore_as4_path
= 1;
1491 /* "New_aggregator shall be taken as aggregator"
1493 attr
->aggregator_as
= as4_aggregator
;
1494 attr
->aggregator_addr
.s_addr
=
1495 as4_aggregator_addr
->s_addr
;
1498 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1499 * That is bogus - but reading the conditions
1500 * we have to handle AS4_AGGREGATOR as if it were
1501 * AGGREGATOR in that case
1503 if (BGP_DEBUG(as4
, AS4
))
1505 "[AS4] %s BGP not AS4 capable peer send"
1506 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1507 " it as if AGGREGATOR with AS_TRANS had been there",
1509 attr
->aggregator_as
= as4_aggregator
;
1510 /* sweep it under the carpet and simulate a "good"
1512 attr
->flag
|= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
));
1516 /* need to reconcile NEW_AS_PATH and AS_PATH */
1517 if (!ignore_as4_path
1518 && (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))) {
1519 newpath
= aspath_reconcile_as4(attr
->aspath
, as4_path
);
1520 aspath_unintern(&attr
->aspath
);
1521 attr
->aspath
= aspath_intern(newpath
);
1523 return BGP_ATTR_PARSE_PROCEED
;
1526 /* Community attribute. */
1527 static bgp_attr_parse_ret_t
1528 bgp_attr_community(struct bgp_attr_parser_args
*args
)
1530 struct peer
*const peer
= args
->peer
;
1531 struct attr
*const attr
= args
->attr
;
1532 const bgp_size_t length
= args
->length
;
1535 attr
->community
= NULL
;
1536 return BGP_ATTR_PARSE_PROCEED
;
1540 community_parse((u_int32_t
*)stream_pnt(peer
->curr
), length
);
1542 /* XXX: fix community_parse to use stream API and remove this */
1543 stream_forward_getp(peer
->curr
, length
);
1545 if (!attr
->community
)
1546 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1549 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1551 return BGP_ATTR_PARSE_PROCEED
;
1554 /* Originator ID attribute. */
1555 static bgp_attr_parse_ret_t
1556 bgp_attr_originator_id(struct bgp_attr_parser_args
*args
)
1558 struct peer
*const peer
= args
->peer
;
1559 struct attr
*const attr
= args
->attr
;
1560 const bgp_size_t length
= args
->length
;
1564 zlog_err("Bad originator ID length %d", length
);
1566 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1570 attr
->originator_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1572 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
);
1574 return BGP_ATTR_PARSE_PROCEED
;
1577 /* Cluster list attribute. */
1578 static bgp_attr_parse_ret_t
1579 bgp_attr_cluster_list(struct bgp_attr_parser_args
*args
)
1581 struct peer
*const peer
= args
->peer
;
1582 struct attr
*const attr
= args
->attr
;
1583 const bgp_size_t length
= args
->length
;
1587 zlog_err("Bad cluster list length %d", length
);
1589 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1594 cluster_parse((struct in_addr
*)stream_pnt(peer
->curr
), length
);
1596 /* XXX: Fix cluster_parse to use stream API and then remove this */
1597 stream_forward_getp(peer
->curr
, length
);
1599 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
);
1601 return BGP_ATTR_PARSE_PROCEED
;
1604 /* Multiprotocol reachability information parse. */
1605 int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
1606 struct bgp_nlri
*mp_update
)
1610 iana_safi_t pkt_safi
;
1612 bgp_size_t nlri_len
;
1615 struct peer
*const peer
= args
->peer
;
1616 struct attr
*const attr
= args
->attr
;
1617 const bgp_size_t length
= args
->length
;
1619 /* Set end of packet. */
1620 s
= BGP_INPUT(peer
);
1621 start
= stream_get_getp(s
);
1623 /* safe to read statically sized header? */
1624 #define BGP_MP_REACH_MIN_SIZE 5
1625 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1626 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
)) {
1627 zlog_info("%s: %s sent invalid length, %lu", __func__
,
1628 peer
->host
, (unsigned long)length
);
1629 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1632 /* Load AFI, SAFI. */
1633 pkt_afi
= stream_getw(s
);
1634 pkt_safi
= stream_getc(s
);
1636 /* Convert AFI, SAFI to internal values, check. */
1637 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1638 /* Log if AFI or SAFI is unrecognized. This is not an error
1640 * the attribute is otherwise malformed.
1642 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1644 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1645 peer
->host
, pkt_afi
, pkt_safi
);
1646 return BGP_ATTR_PARSE_ERROR
;
1649 /* Get nexthop length. */
1650 attr
->mp_nexthop_len
= stream_getc(s
);
1652 if (LEN_LEFT
< attr
->mp_nexthop_len
) {
1654 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1655 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1656 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1659 /* Nexthop length check. */
1660 switch (attr
->mp_nexthop_len
) {
1661 case BGP_ATTR_NHLEN_VPNV4
:
1662 stream_getl(s
); /* RD high */
1663 stream_getl(s
); /* RD low */
1665 * NOTE: intentional fall through
1666 * - for consistency in rx processing
1668 * The following comment is to signal GCC this intention
1669 * and supress the warning
1672 case BGP_ATTR_NHLEN_IPV4
:
1673 stream_get(&attr
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1674 /* Probably needed for RFC 2283 */
1675 if (attr
->nexthop
.s_addr
== 0)
1676 memcpy(&attr
->nexthop
.s_addr
,
1677 &attr
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1679 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1680 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1681 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1682 stream_getl(s
); /* RD high */
1683 stream_getl(s
); /* RD low */
1685 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1687 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1688 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1689 if (attr
->mp_nexthop_len
1690 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1691 stream_getl(s
); /* RD high */
1692 stream_getl(s
); /* RD low */
1694 stream_get(&attr
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1695 if (attr
->mp_nexthop_len
1696 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1697 stream_getl(s
); /* RD high */
1698 stream_getl(s
); /* RD low */
1700 stream_get(&attr
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1701 if (!IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
)) {
1702 char buf1
[INET6_ADDRSTRLEN
];
1703 char buf2
[INET6_ADDRSTRLEN
];
1705 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1707 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1710 &attr
->mp_nexthop_global
,
1711 buf1
, INET6_ADDRSTRLEN
),
1713 &attr
->mp_nexthop_local
, buf2
,
1716 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1720 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1721 __func__
, peer
->host
, attr
->mp_nexthop_len
);
1722 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1726 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__
,
1728 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1733 if ((val
= stream_getc(s
)))
1735 "%s sent non-zero value, %u, for defunct SNPA-length field",
1739 /* must have nrli_len, what is left of the attribute */
1740 nlri_len
= LEN_LEFT
;
1741 if (nlri_len
> STREAM_READABLE(s
)) {
1742 zlog_info("%s: (%s) Failed to read NLRI", __func__
, peer
->host
);
1743 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1747 zlog_info("%s: (%s) No Reachability, Treating as a EOR marker",
1748 __func__
, peer
->host
);
1750 mp_update
->afi
= afi
;
1751 mp_update
->safi
= safi
;
1752 return BGP_ATTR_PARSE_EOR
;
1755 mp_update
->afi
= afi
;
1756 mp_update
->safi
= safi
;
1757 mp_update
->nlri
= stream_pnt(s
);
1758 mp_update
->length
= nlri_len
;
1760 stream_forward_getp(s
, nlri_len
);
1762 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
);
1764 return BGP_ATTR_PARSE_PROCEED
;
1768 /* Multiprotocol unreachable parse */
1769 int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
1770 struct bgp_nlri
*mp_withdraw
)
1775 iana_safi_t pkt_safi
;
1777 u_int16_t withdraw_len
;
1778 struct peer
*const peer
= args
->peer
;
1779 struct attr
*const attr
= args
->attr
;
1780 const bgp_size_t length
= args
->length
;
1784 #define BGP_MP_UNREACH_MIN_SIZE 3
1785 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1786 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1788 pkt_afi
= stream_getw(s
);
1789 pkt_safi
= stream_getc(s
);
1791 /* Convert AFI, SAFI to internal values, check. */
1792 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1793 /* Log if AFI or SAFI is unrecognized. This is not an error
1795 * the attribute is otherwise malformed.
1797 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1799 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1800 peer
->host
, pkt_afi
, pkt_safi
);
1801 return BGP_ATTR_PARSE_ERROR
;
1804 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1806 mp_withdraw
->afi
= afi
;
1807 mp_withdraw
->safi
= safi
;
1808 mp_withdraw
->nlri
= stream_pnt(s
);
1809 mp_withdraw
->length
= withdraw_len
;
1811 stream_forward_getp(s
, withdraw_len
);
1813 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
);
1815 return BGP_ATTR_PARSE_PROCEED
;
1818 /* Large Community attribute. */
1819 static bgp_attr_parse_ret_t
1820 bgp_attr_large_community(struct bgp_attr_parser_args
*args
)
1822 struct peer
*const peer
= args
->peer
;
1823 struct attr
*const attr
= args
->attr
;
1824 const bgp_size_t length
= args
->length
;
1827 * Large community follows new attribute format.
1830 attr
->lcommunity
= NULL
;
1831 /* Empty extcomm doesn't seem to be invalid per se */
1832 return BGP_ATTR_PARSE_PROCEED
;
1836 lcommunity_parse((u_int8_t
*)stream_pnt(peer
->curr
), length
);
1837 /* XXX: fix ecommunity_parse to use stream API */
1838 stream_forward_getp(peer
->curr
, length
);
1840 if (!attr
->lcommunity
)
1841 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1844 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
1846 return BGP_ATTR_PARSE_PROCEED
;
1849 /* Extended Community attribute. */
1850 static bgp_attr_parse_ret_t
1851 bgp_attr_ext_communities(struct bgp_attr_parser_args
*args
)
1853 struct peer
*const peer
= args
->peer
;
1854 struct attr
*const attr
= args
->attr
;
1855 const bgp_size_t length
= args
->length
;
1859 attr
->ecommunity
= NULL
;
1860 /* Empty extcomm doesn't seem to be invalid per se */
1861 return BGP_ATTR_PARSE_PROCEED
;
1865 ecommunity_parse((u_int8_t
*)stream_pnt(peer
->curr
), length
);
1866 /* XXX: fix ecommunity_parse to use stream API */
1867 stream_forward_getp(peer
->curr
, length
);
1869 if (!attr
->ecommunity
)
1870 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1873 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
1875 /* Extract MAC mobility sequence number, if any. */
1876 attr
->mm_seqnum
= bgp_attr_mac_mobility_seqnum(attr
, &sticky
);
1877 attr
->sticky
= sticky
;
1879 return BGP_ATTR_PARSE_PROCEED
;
1882 /* Parse Tunnel Encap attribute in an UPDATE */
1883 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
1884 bgp_size_t length
, /* IN: attr's length field */
1885 struct attr
*attr
, /* IN: caller already allocated */
1886 u_char flag
, /* IN: attr's flags field */
1890 uint16_t tunneltype
= 0;
1892 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1894 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1895 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
1897 "Tunnel Encap attribute flag isn't optional and transitive %d",
1899 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
1900 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1905 if (BGP_ATTR_ENCAP
== type
) {
1906 /* read outer TLV type and length */
1907 uint16_t tlv_length
;
1911 "Tunnel Encap attribute not long enough to contain outer T,L");
1912 bgp_notify_send_with_data(
1913 peer
, BGP_NOTIFY_UPDATE_ERR
,
1914 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1917 tunneltype
= stream_getw(BGP_INPUT(peer
));
1918 tlv_length
= stream_getw(BGP_INPUT(peer
));
1921 if (tlv_length
!= length
) {
1922 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
1923 tlv_length
, length
);
1927 while (length
>= 4) {
1928 uint16_t subtype
= 0;
1929 uint16_t sublength
= 0;
1930 struct bgp_attr_encap_subtlv
*tlv
;
1932 if (BGP_ATTR_ENCAP
== type
) {
1933 subtype
= stream_getc(BGP_INPUT(peer
));
1934 sublength
= stream_getc(BGP_INPUT(peer
));
1938 subtype
= stream_getw(BGP_INPUT(peer
));
1939 sublength
= stream_getw(BGP_INPUT(peer
));
1944 if (sublength
> length
) {
1946 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1948 bgp_notify_send_with_data(
1949 peer
, BGP_NOTIFY_UPDATE_ERR
,
1950 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1954 /* alloc and copy sub-tlv */
1955 /* TBD make sure these are freed when attributes are released */
1956 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
1957 sizeof(struct bgp_attr_encap_subtlv
)
1959 tlv
->type
= subtype
;
1960 tlv
->length
= sublength
;
1961 stream_get(tlv
->value
, peer
->curr
, sublength
);
1962 length
-= sublength
;
1964 /* attach tlv to encap chain */
1965 if (BGP_ATTR_ENCAP
== type
) {
1966 struct bgp_attr_encap_subtlv
*stlv_last
;
1967 for (stlv_last
= attr
->encap_subtlvs
;
1968 stlv_last
&& stlv_last
->next
;
1969 stlv_last
= stlv_last
->next
)
1972 stlv_last
->next
= tlv
;
1974 attr
->encap_subtlvs
= tlv
;
1978 struct bgp_attr_encap_subtlv
*stlv_last
;
1979 for (stlv_last
= attr
->vnc_subtlvs
;
1980 stlv_last
&& stlv_last
->next
;
1981 stlv_last
= stlv_last
->next
)
1984 stlv_last
->next
= tlv
;
1986 attr
->vnc_subtlvs
= tlv
;
1992 if (BGP_ATTR_ENCAP
== type
) {
1993 attr
->encap_tunneltype
= tunneltype
;
1997 /* spurious leftover data */
1999 "Tunnel Encap attribute length is bad: %d leftover octets",
2001 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2002 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2010 /* Prefix SID attribute
2011 * draft-ietf-idr-bgp-prefix-sid-05
2013 static bgp_attr_parse_ret_t
2014 bgp_attr_prefix_sid(struct bgp_attr_parser_args
*args
,
2015 struct bgp_nlri
*mp_update
)
2017 struct peer
*const peer
= args
->peer
;
2018 struct attr
*const attr
= args
->attr
;
2021 u_int32_t label_index
;
2022 struct in6_addr ipv6_sid
;
2023 u_int32_t srgb_base
;
2024 u_int32_t srgb_range
;
2027 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
2029 type
= stream_getc(peer
->curr
);
2030 length
= stream_getw(peer
->curr
);
2032 if (type
== BGP_PREFIX_SID_LABEL_INDEX
) {
2033 if (length
!= BGP_PREFIX_SID_LABEL_INDEX_LENGTH
) {
2035 "Prefix SID label index length is %d instead of %d",
2036 length
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
2037 return bgp_attr_malformed(
2038 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2042 /* Ignore flags and reserved */
2043 stream_getc(peer
->curr
);
2044 stream_getw(peer
->curr
);
2046 /* Fetch the label index and see if it is valid. */
2047 label_index
= stream_getl(peer
->curr
);
2048 if (label_index
== BGP_INVALID_LABEL_INDEX
)
2049 return bgp_attr_malformed(
2050 args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2053 /* Store label index; subsequently, we'll check on
2055 attr
->label_index
= label_index
;
2058 * Ignore the Label index attribute unless received for
2062 if (!mp_update
->length
2063 || mp_update
->safi
!= SAFI_LABELED_UNICAST
)
2064 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
2067 /* Placeholder code for the IPv6 SID type */
2068 else if (type
== BGP_PREFIX_SID_IPV6
) {
2069 if (length
!= BGP_PREFIX_SID_IPV6_LENGTH
) {
2070 zlog_err("Prefix SID IPv6 length is %d instead of %d",
2071 length
, BGP_PREFIX_SID_IPV6_LENGTH
);
2072 return bgp_attr_malformed(
2073 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2077 /* Ignore reserved */
2078 stream_getc(peer
->curr
);
2079 stream_getw(peer
->curr
);
2081 stream_get(&ipv6_sid
, peer
->curr
, 16);
2084 /* Placeholder code for the Originator SRGB type */
2085 else if (type
== BGP_PREFIX_SID_ORIGINATOR_SRGB
) {
2087 stream_getw(peer
->curr
);
2091 if (length
% BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
) {
2093 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2094 length
, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
);
2095 return bgp_attr_malformed(
2096 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2100 srgb_count
= length
/ BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
;
2102 for (int i
= 0; i
< srgb_count
; i
++) {
2103 stream_get(&srgb_base
, peer
->curr
, 3);
2104 stream_get(&srgb_range
, peer
->curr
, 3);
2108 return BGP_ATTR_PARSE_PROCEED
;
2111 /* BGP unknown attribute treatment. */
2112 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2114 bgp_size_t total
= args
->total
;
2115 struct transit
*transit
;
2116 struct peer
*const peer
= args
->peer
;
2117 struct attr
*const attr
= args
->attr
;
2118 u_char
*const startp
= args
->startp
;
2119 const u_char type
= args
->type
;
2120 const u_char flag
= args
->flags
;
2121 const bgp_size_t length
= args
->length
;
2123 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2125 "%s Unknown attribute is received (type %d, length %d)",
2126 peer
->host
, type
, length
);
2128 /* Forward read pointer of input stream. */
2129 stream_forward_getp(peer
->curr
, length
);
2131 /* If any of the mandatory well-known attributes are not recognized,
2132 then the Error Subcode is set to Unrecognized Well-known
2133 Attribute. The Data field contains the unrecognized attribute
2134 (type, length and value). */
2135 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2136 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2140 /* Unrecognized non-transitive optional attributes must be quietly
2141 ignored and not passed along to other BGP peers. */
2142 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2143 return BGP_ATTR_PARSE_PROCEED
;
2145 /* If a path with recognized transitive optional attribute is
2146 accepted and passed along to other BGP peers and the Partial bit
2147 in the Attribute Flags octet is set to 1 by some previous AS, it
2148 is not set back to 0 by the current AS. */
2149 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2151 /* Store transitive attribute to the end of attr->transit. */
2153 attr
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2155 transit
= attr
->transit
;
2158 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2159 transit
->length
+ total
);
2161 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2163 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2164 transit
->length
+= total
;
2166 return BGP_ATTR_PARSE_PROCEED
;
2169 /* Well-known attribute check. */
2170 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2174 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2176 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2177 return BGP_ATTR_PARSE_PROCEED
;
2179 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2180 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2181 are present, it should. Check for any other attribute being present
2184 if (attr
->flag
== ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))
2185 return BGP_ATTR_PARSE_PROCEED
;
2187 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2188 type
= BGP_ATTR_ORIGIN
;
2190 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2191 type
= BGP_ATTR_AS_PATH
;
2193 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2195 * NLRI is empty. We can't easily check NLRI empty here though.
2197 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2198 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2199 type
= BGP_ATTR_NEXT_HOP
;
2201 if (peer
->sort
== BGP_PEER_IBGP
2202 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2203 type
= BGP_ATTR_LOCAL_PREF
;
2206 zlog_warn("%s Missing well-known attribute %s.", peer
->host
,
2207 lookup_msg(attr_str
, type
, NULL
));
2208 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2209 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2211 return BGP_ATTR_PARSE_ERROR
;
2213 return BGP_ATTR_PARSE_PROCEED
;
2216 /* Read attribute of update packet. This function is called from
2217 bgp_update_receive() in bgp_packet.c. */
2218 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2219 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2220 struct bgp_nlri
*mp_withdraw
)
2226 u_char
*startp
, *endp
;
2228 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2229 /* we need the as4_path only until we have synthesized the as_path with
2231 /* same goes for as4_aggregator */
2232 struct aspath
*as4_path
= NULL
;
2233 as_t as4_aggregator
= 0;
2234 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2236 /* Initialize bitmap. */
2237 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2239 /* End pointer of BGP attribute. */
2240 endp
= BGP_INPUT_PNT(peer
) + size
;
2242 /* Get attributes to the end of attribute length. */
2243 while (BGP_INPUT_PNT(peer
) < endp
) {
2244 /* Check remaining length check.*/
2245 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2246 /* XXX warning: long int format, int arg (arg 5) */
2248 "%s: error BGP attribute length %lu is smaller than min len",
2250 (unsigned long)(endp
2251 - stream_pnt(BGP_INPUT(peer
))));
2253 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2254 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2255 return BGP_ATTR_PARSE_ERROR
;
2258 /* Fetch attribute flag and type. */
2259 startp
= BGP_INPUT_PNT(peer
);
2260 /* "The lower-order four bits of the Attribute Flags octet are
2261 unused. They MUST be zero when sent and MUST be ignored when
2263 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2264 type
= stream_getc(BGP_INPUT(peer
));
2266 /* Check whether Extended-Length applies and is in bounds */
2267 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2268 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2270 "%s: Extended length set, but just %lu bytes of attr header",
2272 (unsigned long)(endp
2273 - stream_pnt(BGP_INPUT(peer
))));
2275 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2276 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2277 return BGP_ATTR_PARSE_ERROR
;
2280 /* Check extended attribue length bit. */
2281 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2282 length
= stream_getw(BGP_INPUT(peer
));
2284 length
= stream_getc(BGP_INPUT(peer
));
2286 /* If any attribute appears more than once in the UPDATE
2287 message, then the Error Subcode is set to Malformed Attribute
2290 if (CHECK_BITMAP(seen
, type
)) {
2292 "%s: error BGP attribute type %d appears twice in a message",
2295 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2296 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2297 return BGP_ATTR_PARSE_ERROR
;
2300 /* Set type to bitmap to check duplicate attribute. `type' is
2301 unsigned char so it never overflow bitmap range. */
2303 SET_BITMAP(seen
, type
);
2305 /* Overflow check. */
2306 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2308 if (attr_endp
> endp
) {
2310 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2311 peer
->host
, type
, length
, size
, attr_endp
,
2315 * If any recognized attribute has an Attribute
2316 * Length that conflicts with the expected length
2317 * (based on the attribute type code), then the
2318 * Error Subcode MUST be set to Attribute Length
2319 * Error. The Data field MUST contain the erroneous
2320 * attribute (type, length, and value).
2322 * We do not currently have a good way to determine the
2323 * length of the attribute independent of the length
2324 * received in the message. Instead we send the
2325 * minimum between the amount of data we have and the
2326 * amount specified by the attribute length field.
2328 * Instead of directly passing in the packet buffer and
2329 * offset we use the stream_get* functions to read into
2330 * a stack buffer, since they perform bounds checking
2331 * and we are working with untrusted data.
2333 unsigned char ndata
[BGP_MAX_PACKET_SIZE
];
2334 memset(ndata
, 0x00, sizeof(ndata
));
2336 CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 2 : 1;
2337 /* Rewind to end of flag field */
2338 stream_forward_getp(BGP_INPUT(peer
), -(1 + lfl
));
2340 stream_get(&ndata
[0], BGP_INPUT(peer
), 1);
2342 stream_get(&ndata
[1], BGP_INPUT(peer
), lfl
);
2344 size_t atl
= attr_endp
- startp
;
2345 size_t ndl
= MIN(atl
, STREAM_READABLE(BGP_INPUT(peer
)));
2346 stream_get(&ndata
[lfl
+ 1], BGP_INPUT(peer
), ndl
);
2348 bgp_notify_send_with_data(
2349 peer
, BGP_NOTIFY_UPDATE_ERR
,
2350 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, ndata
,
2353 return BGP_ATTR_PARSE_ERROR
;
2356 struct bgp_attr_parser_args attr_args
= {
2363 .total
= attr_endp
- startp
,
2367 /* If any recognized attribute has Attribute Flags that conflict
2368 with the Attribute Type Code, then the Error Subcode is set
2370 Attribute Flags Error. The Data field contains the erroneous
2371 attribute (type, length and value). */
2372 if (bgp_attr_flag_invalid(&attr_args
)) {
2373 bgp_attr_parse_ret_t ret
;
2374 ret
= bgp_attr_malformed(
2375 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2377 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2382 /* OK check attribute and store it's value. */
2384 case BGP_ATTR_ORIGIN
:
2385 ret
= bgp_attr_origin(&attr_args
);
2387 case BGP_ATTR_AS_PATH
:
2388 ret
= bgp_attr_aspath(&attr_args
);
2390 case BGP_ATTR_AS4_PATH
:
2391 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2393 case BGP_ATTR_NEXT_HOP
:
2394 ret
= bgp_attr_nexthop(&attr_args
);
2396 case BGP_ATTR_MULTI_EXIT_DISC
:
2397 ret
= bgp_attr_med(&attr_args
);
2399 case BGP_ATTR_LOCAL_PREF
:
2400 ret
= bgp_attr_local_pref(&attr_args
);
2402 case BGP_ATTR_ATOMIC_AGGREGATE
:
2403 ret
= bgp_attr_atomic(&attr_args
);
2405 case BGP_ATTR_AGGREGATOR
:
2406 ret
= bgp_attr_aggregator(&attr_args
);
2408 case BGP_ATTR_AS4_AGGREGATOR
:
2409 ret
= bgp_attr_as4_aggregator(&attr_args
,
2411 &as4_aggregator_addr
);
2413 case BGP_ATTR_COMMUNITIES
:
2414 ret
= bgp_attr_community(&attr_args
);
2416 case BGP_ATTR_LARGE_COMMUNITIES
:
2417 ret
= bgp_attr_large_community(&attr_args
);
2419 case BGP_ATTR_ORIGINATOR_ID
:
2420 ret
= bgp_attr_originator_id(&attr_args
);
2422 case BGP_ATTR_CLUSTER_LIST
:
2423 ret
= bgp_attr_cluster_list(&attr_args
);
2425 case BGP_ATTR_MP_REACH_NLRI
:
2426 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2428 case BGP_ATTR_MP_UNREACH_NLRI
:
2429 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2431 case BGP_ATTR_EXT_COMMUNITIES
:
2432 ret
= bgp_attr_ext_communities(&attr_args
);
2437 case BGP_ATTR_ENCAP
:
2438 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2441 case BGP_ATTR_PREFIX_SID
:
2442 ret
= bgp_attr_prefix_sid(&attr_args
, mp_update
);
2445 ret
= bgp_attr_unknown(&attr_args
);
2449 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2450 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2451 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2452 ret
= BGP_ATTR_PARSE_ERROR
;
2455 if (ret
== BGP_ATTR_PARSE_EOR
) {
2457 aspath_unintern(&as4_path
);
2461 /* If hard error occured immediately return to the caller. */
2462 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2463 zlog_warn("%s: Attribute %s, parse error", peer
->host
,
2464 lookup_msg(attr_str
, type
, NULL
));
2466 aspath_unintern(&as4_path
);
2469 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2472 "%s: Attribute %s, parse error - treating as withdrawal",
2473 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2475 aspath_unintern(&as4_path
);
2479 /* Check the fetched length. */
2480 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2481 zlog_warn("%s: BGP attribute %s, fetch error",
2482 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2483 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2484 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2486 aspath_unintern(&as4_path
);
2487 return BGP_ATTR_PARSE_ERROR
;
2491 /* Check final read pointer is same as end pointer. */
2492 if (BGP_INPUT_PNT(peer
) != endp
) {
2493 zlog_warn("%s: BGP attribute %s, length mismatch", peer
->host
,
2494 lookup_msg(attr_str
, type
, NULL
));
2495 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2496 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2498 aspath_unintern(&as4_path
);
2499 return BGP_ATTR_PARSE_ERROR
;
2502 /* Check all mandatory well-known attributes are present */
2504 bgp_attr_parse_ret_t ret
;
2505 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2507 aspath_unintern(&as4_path
);
2513 * At this place we can see whether we got AS4_PATH and/or
2514 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2515 * We can not do this before we've read all attributes because
2516 * the as4 handling does not say whether AS4_PATH has to be sent
2517 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2518 * in relationship to AGGREGATOR.
2519 * So, to be defensive, we are not relying on any order and read
2520 * all attributes first, including these 32bit ones, and now,
2521 * afterwards, we look what and if something is to be done for as4.
2523 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2526 /* actually... this doesn't ever return failure currently, but
2527 * better safe than sorry */
2528 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2529 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2530 &as4_aggregator_addr
)) {
2531 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2532 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2534 aspath_unintern(&as4_path
);
2535 return BGP_ATTR_PARSE_ERROR
;
2538 /* At this stage, we have done all fiddling with as4, and the
2539 * resulting info is in attr->aggregator resp. attr->aspath
2540 * so we can chuck as4_aggregator and as4_path alltogether in
2541 * order to save memory
2544 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2545 /* The flag that we got this is still there, but that does not
2550 * The "rest" of the code does nothing with as4_aggregator.
2551 * there is no memory attached specifically which is not part
2553 * so ignoring just means do nothing.
2556 * Finally do the checks on the aspath we did not do yet
2557 * because we waited for a potentially synthesized aspath.
2559 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2560 ret
= bgp_attr_aspath_check(peer
, attr
);
2561 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2564 /* Finally intern unknown attribute. */
2566 attr
->transit
= transit_intern(attr
->transit
);
2567 if (attr
->encap_subtlvs
)
2568 attr
->encap_subtlvs
=
2569 encap_intern(attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2571 if (attr
->vnc_subtlvs
)
2573 encap_intern(attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2576 return BGP_ATTR_PARSE_PROCEED
;
2579 size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
, afi_t afi
,
2580 safi_t safi
, struct bpacket_attr_vec_arr
*vecarr
,
2585 iana_safi_t pkt_safi
;
2588 /* Set extended bit always to encode the attribute length as 2 bytes */
2589 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2590 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2591 sizep
= stream_get_endp(s
);
2592 stream_putw(s
, 0); /* Marker: Attribute length. */
2595 /* Convert AFI, SAFI to values for packet. */
2596 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2598 stream_putw(s
, pkt_afi
); /* AFI */
2599 stream_putc(s
, pkt_safi
); /* SAFI */
2603 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
))
2604 nh_afi
= peer_cap_enhe(peer
, afi
, safi
) ? AFI_IP6
: AFI_IP
;
2606 nh_afi
= BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->mp_nexthop_len
);
2609 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2614 case SAFI_MULTICAST
:
2615 case SAFI_LABELED_UNICAST
:
2617 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2621 stream_putl(s
, 0); /* RD = 0, per RFC */
2623 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2628 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2637 case SAFI_MULTICAST
:
2638 case SAFI_LABELED_UNICAST
:
2640 if (attr
->mp_nexthop_len
2641 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2643 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
);
2644 stream_put(s
, &attr
->mp_nexthop_global
,
2646 stream_put(s
, &attr
->mp_nexthop_local
,
2649 stream_putc(s
, IPV6_MAX_BYTELEN
);
2650 stream_put(s
, &attr
->mp_nexthop_global
,
2654 case SAFI_MPLS_VPN
: {
2655 if (attr
->mp_nexthop_len
2656 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2658 stream_putl(s
, 0); /* RD = 0, per RFC */
2660 stream_put(s
, &attr
->mp_nexthop_global
,
2662 } else if (attr
->mp_nexthop_len
2663 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2665 stream_putl(s
, 0); /* RD = 0, per RFC */
2667 stream_put(s
, &attr
->mp_nexthop_global
,
2669 stream_putl(s
, 0); /* RD = 0, per RFC */
2671 stream_put(s
, &attr
->mp_nexthop_local
,
2676 stream_putc(s
, IPV6_MAX_BYTELEN
);
2677 stream_put(s
, &attr
->mp_nexthop_global
,
2686 "Bad nexthop when sening to %s, AFI %u SAFI %u nhlen %d",
2687 peer
->host
, afi
, safi
, attr
->mp_nexthop_len
);
2696 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2697 struct prefix
*p
, struct prefix_rd
*prd
,
2698 mpls_label_t
*label
, int addpath_encode
,
2699 u_int32_t addpath_tx_id
, struct attr
*attr
)
2701 if (safi
== SAFI_MPLS_VPN
) {
2703 stream_putl(s
, addpath_tx_id
);
2704 /* Label, RD, Prefix write. */
2705 stream_putc(s
, p
->prefixlen
+ 88);
2706 stream_put(s
, label
, BGP_LABEL_BYTES
);
2707 stream_put(s
, prd
->val
, 8);
2708 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2709 } else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
2710 /* EVPN prefix - contents depend on type */
2711 bgp_evpn_encode_prefix(s
, p
, prd
, label
, attr
, addpath_encode
,
2713 } else if (safi
== SAFI_LABELED_UNICAST
) {
2714 /* Prefix write with label. */
2715 stream_put_labeled_prefix(s
, p
, label
);
2717 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2720 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2722 int size
= PSIZE(p
->prefixlen
);
2723 if (safi
== SAFI_MPLS_VPN
)
2725 else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2726 size
+= 232; // TODO: Maximum possible for type-2, type-3 and
2732 * Encodes the tunnel encapsulation attribute,
2733 * and with ENABLE_BGP_VNC the VNC attribute which uses
2734 * almost the same TLV format
2736 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2737 struct stream
*s
, struct attr
*attr
,
2740 unsigned int attrlenfield
= 0;
2741 unsigned int attrhdrlen
= 0;
2742 struct bgp_attr_encap_subtlv
*subtlvs
;
2743 struct bgp_attr_encap_subtlv
*st
;
2744 const char *attrname
;
2746 if (!attr
|| (attrtype
== BGP_ATTR_ENCAP
2747 && (!attr
->encap_tunneltype
2748 || attr
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2752 case BGP_ATTR_ENCAP
:
2753 attrname
= "Tunnel Encap";
2754 subtlvs
= attr
->encap_subtlvs
;
2755 if (subtlvs
== NULL
) /* nothing to do */
2758 * The tunnel encap attr has an "outer" tlv.
2760 * L = total length of subtlvs,
2761 * V = concatenated subtlvs.
2763 attrlenfield
= 2 + 2; /* T + L */
2764 attrhdrlen
= 1 + 1; /* subTLV T + L */
2770 subtlvs
= attr
->vnc_subtlvs
;
2771 if (subtlvs
== NULL
) /* nothing to do */
2773 attrlenfield
= 0; /* no outer T + L */
2774 attrhdrlen
= 2 + 2; /* subTLV T + L */
2782 /* compute attr length */
2783 for (st
= subtlvs
; st
; st
= st
->next
) {
2784 attrlenfield
+= (attrhdrlen
+ st
->length
);
2787 if (attrlenfield
> 0xffff) {
2788 zlog_info("%s attribute is too long (length=%d), can't send it",
2789 attrname
, attrlenfield
);
2793 if (attrlenfield
> 0xff) {
2794 /* 2-octet length field */
2795 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2796 | BGP_ATTR_FLAG_EXTLEN
);
2797 stream_putc(s
, attrtype
);
2798 stream_putw(s
, attrlenfield
& 0xffff);
2800 /* 1-octet length field */
2801 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2802 stream_putc(s
, attrtype
);
2803 stream_putc(s
, attrlenfield
& 0xff);
2806 if (attrtype
== BGP_ATTR_ENCAP
) {
2807 /* write outer T+L */
2808 stream_putw(s
, attr
->encap_tunneltype
);
2809 stream_putw(s
, attrlenfield
- 4);
2812 /* write each sub-tlv */
2813 for (st
= subtlvs
; st
; st
= st
->next
) {
2814 if (attrtype
== BGP_ATTR_ENCAP
) {
2815 stream_putc(s
, st
->type
);
2816 stream_putc(s
, st
->length
);
2819 stream_putw(s
, st
->type
);
2820 stream_putw(s
, st
->length
);
2823 stream_put(s
, st
->value
, st
->length
);
2827 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
2829 /* Set MP attribute length. Don't count the (2) bytes used to encode
2831 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
2834 /* Make attribute packet. */
2835 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
2836 struct stream
*s
, struct attr
*attr
,
2837 struct bpacket_attr_vec_arr
*vecarr
,
2838 struct prefix
*p
, afi_t afi
, safi_t safi
,
2839 struct peer
*from
, struct prefix_rd
*prd
,
2840 mpls_label_t
*label
, int addpath_encode
,
2841 u_int32_t addpath_tx_id
)
2844 size_t aspath_sizep
;
2845 struct aspath
*aspath
;
2846 int send_as4_path
= 0;
2847 int send_as4_aggregator
= 0;
2848 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2853 /* Remember current pointer. */
2854 cp
= stream_get_endp(s
);
2857 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
2858 && !peer_cap_enhe(peer
, afi
, safi
))) {
2859 size_t mpattrlen_pos
= 0;
2861 mpattrlen_pos
= bgp_packet_mpattr_start(s
, peer
, afi
, safi
,
2863 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
2864 addpath_encode
, addpath_tx_id
, attr
);
2865 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2868 /* Origin attribute. */
2869 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2870 stream_putc(s
, BGP_ATTR_ORIGIN
);
2872 stream_putc(s
, attr
->origin
);
2874 /* AS path attribute. */
2876 /* If remote-peer is EBGP */
2877 if (peer
->sort
== BGP_PEER_EBGP
2878 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2879 PEER_FLAG_AS_PATH_UNCHANGED
)
2880 || attr
->aspath
->segments
== NULL
)
2881 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2882 PEER_FLAG_RSERVER_CLIENT
))) {
2883 aspath
= aspath_dup(attr
->aspath
);
2885 /* Even though we may not be configured for confederations we
2887 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2888 aspath
= aspath_delete_confed_seq(aspath
);
2890 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
2891 /* Stuff our path CONFED_ID on the front */
2892 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
2894 if (peer
->change_local_as
) {
2895 /* If replace-as is specified, we only use the
2896 change_local_as when
2897 advertising routes. */
2900 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
2901 aspath
= aspath_add_seq(aspath
,
2904 aspath
= aspath_add_seq(aspath
,
2905 peer
->change_local_as
);
2907 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
2910 } else if (peer
->sort
== BGP_PEER_CONFED
) {
2911 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2913 aspath
= aspath_dup(attr
->aspath
);
2914 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
2916 aspath
= attr
->aspath
;
2918 /* If peer is not AS4 capable, then:
2919 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2920 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2922 * types are in it (i.e. exclude them if they are there)
2923 * AND do this only if there is at least one asnum > 65535 in the
2925 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2927 * all ASnums > 65535 to BGP_AS_TRANS
2930 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
2931 stream_putc(s
, BGP_ATTR_AS_PATH
);
2932 aspath_sizep
= stream_get_endp(s
);
2934 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
2936 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2939 if (!use32bit
&& aspath_has_as4(aspath
))
2941 1; /* we'll do this later, at the correct place */
2943 /* Nexthop attribute. */
2944 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
2945 && !peer_cap_enhe(peer
, afi
, safi
)) {
2946 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2947 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2948 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2949 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2952 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2953 } else if (peer_cap_enhe(from
, afi
, safi
)) {
2955 * Likely this is the case when an IPv4 prefix was
2957 * Extended Next-hop capability and now being advertised
2960 * Setting the mandatory (ipv4) next-hop attribute here
2962 * implicit next-hop self with correct (ipv4 address
2965 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2966 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2967 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2970 stream_put_ipv4(s
, 0);
2974 /* MED attribute. */
2975 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
2976 || bgp
->maxmed_active
) {
2977 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
2978 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
2980 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
2984 /* Local preference. */
2985 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
2986 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2987 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
2989 stream_putl(s
, attr
->local_pref
);
2992 /* Atomic aggregate. */
2993 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
2994 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2995 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3000 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3001 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3002 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3003 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3006 /* AS4 capable peer */
3008 stream_putl(s
, attr
->aggregator_as
);
3010 /* 2-byte AS peer */
3013 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3015 if (attr
->aggregator_as
> 65535) {
3016 stream_putw(s
, BGP_AS_TRANS
);
3018 /* we have to send AS4_AGGREGATOR, too.
3019 * we'll do that later in order to send
3020 * attributes in ascending
3023 send_as4_aggregator
= 1;
3025 stream_putw(s
, (u_int16_t
)attr
->aggregator_as
);
3027 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3030 /* Community attribute. */
3031 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3032 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3033 if (attr
->community
->size
* 4 > 255) {
3034 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3035 | BGP_ATTR_FLAG_TRANS
3036 | BGP_ATTR_FLAG_EXTLEN
);
3037 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3038 stream_putw(s
, attr
->community
->size
* 4);
3040 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3041 | BGP_ATTR_FLAG_TRANS
);
3042 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3043 stream_putc(s
, attr
->community
->size
* 4);
3045 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3049 * Large Community attribute.
3051 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3052 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3053 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3054 if (lcom_length(attr
->lcommunity
) > 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_LARGE_COMMUNITIES
);
3059 stream_putw(s
, lcom_length(attr
->lcommunity
));
3061 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3062 | BGP_ATTR_FLAG_TRANS
);
3063 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3064 stream_putc(s
, lcom_length(attr
->lcommunity
));
3066 stream_put(s
, attr
->lcommunity
->val
,
3067 lcom_length(attr
->lcommunity
));
3070 /* Route Reflector. */
3071 if (peer
->sort
== BGP_PEER_IBGP
&& from
3072 && from
->sort
== BGP_PEER_IBGP
) {
3073 /* Originator ID. */
3074 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3075 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3078 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3079 stream_put_in_addr(s
, &attr
->originator_id
);
3081 stream_put_in_addr(s
, &from
->remote_id
);
3084 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3085 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3087 if (attr
->cluster
) {
3088 stream_putc(s
, attr
->cluster
->length
+ 4);
3089 /* If this peer configuration's parent BGP has
3091 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3092 stream_put_in_addr(s
, &bgp
->cluster_id
);
3094 stream_put_in_addr(s
, &bgp
->router_id
);
3095 stream_put(s
, attr
->cluster
->list
,
3096 attr
->cluster
->length
);
3099 /* If this peer configuration's parent BGP has
3101 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3102 stream_put_in_addr(s
, &bgp
->cluster_id
);
3104 stream_put_in_addr(s
, &bgp
->router_id
);
3108 /* Extended Communities attribute. */
3109 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3110 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3111 if (peer
->sort
== BGP_PEER_IBGP
3112 || peer
->sort
== BGP_PEER_CONFED
) {
3113 if (attr
->ecommunity
->size
* 8 > 255) {
3114 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3115 | BGP_ATTR_FLAG_TRANS
3116 | BGP_ATTR_FLAG_EXTLEN
);
3117 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3118 stream_putw(s
, attr
->ecommunity
->size
* 8);
3120 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3121 | BGP_ATTR_FLAG_TRANS
);
3122 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3123 stream_putc(s
, attr
->ecommunity
->size
* 8);
3125 stream_put(s
, attr
->ecommunity
->val
,
3126 attr
->ecommunity
->size
* 8);
3130 int ecom_tr_size
= 0;
3133 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3134 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3137 if (CHECK_FLAG(tbit
,
3138 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3145 if (ecom_tr_size
* 8 > 255) {
3148 BGP_ATTR_FLAG_OPTIONAL
3149 | BGP_ATTR_FLAG_TRANS
3150 | BGP_ATTR_FLAG_EXTLEN
);
3152 BGP_ATTR_EXT_COMMUNITIES
);
3153 stream_putw(s
, ecom_tr_size
* 8);
3157 BGP_ATTR_FLAG_OPTIONAL
3158 | BGP_ATTR_FLAG_TRANS
);
3160 BGP_ATTR_EXT_COMMUNITIES
);
3161 stream_putc(s
, ecom_tr_size
* 8);
3164 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3165 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3170 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3173 stream_put(s
, pnt
, 8);
3179 /* Label index attribute. */
3180 if (safi
== SAFI_LABELED_UNICAST
) {
3181 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3182 u_int32_t label_index
;
3184 label_index
= attr
->label_index
;
3186 if (label_index
!= BGP_INVALID_LABEL_INDEX
) {
3187 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3188 | BGP_ATTR_FLAG_TRANS
);
3189 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3191 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3193 BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3194 stream_putc(s
, 0); // reserved
3195 stream_putw(s
, 0); // flags
3196 stream_putl(s
, label_index
);
3201 if (send_as4_path
) {
3202 /* If the peer is NOT As4 capable, AND */
3203 /* there are ASnums > 65535 in path THEN
3204 * give out AS4_PATH */
3206 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3208 * Hm, I wonder... confederation things *should* only be at
3209 * the beginning of an aspath, right? Then we should use
3210 * aspath_delete_confed_seq for this, because it is already
3212 * Folks, talk to me: what is reasonable here!?
3214 aspath
= aspath_delete_confed_seq(aspath
);
3216 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3217 | BGP_ATTR_FLAG_EXTLEN
);
3218 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3219 aspath_sizep
= stream_get_endp(s
);
3221 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3224 if (aspath
!= attr
->aspath
)
3225 aspath_free(aspath
);
3227 if (send_as4_aggregator
) {
3228 /* send AS4_AGGREGATOR, at this place */
3229 /* this section of code moved here in order to ensure the
3231 * *ascending* order of attributes
3233 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3234 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3236 stream_putl(s
, attr
->aggregator_as
);
3237 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3240 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3241 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3242 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3243 /* Tunnel Encap attribute */
3244 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3248 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3253 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
3254 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3255 stream_putc(s
, BGP_ATTR_PMSI_TUNNEL
);
3256 stream_putc(s
, 9); // Length
3257 stream_putc(s
, 0); // Flags
3258 stream_putc(s
, 6); // Tunnel type: Ingress Replication (6)
3259 stream_put(s
, &(attr
->label
), BGP_LABEL_BYTES
); // MPLS Label / VXLAN VNI
3260 stream_put_ipv4(s
, attr
->nexthop
.s_addr
); // Unicast tunnel endpoint IP address
3263 /* Unknown transit attribute. */
3265 stream_put(s
, attr
->transit
->val
, attr
->transit
->length
);
3267 /* Return total size of attribute. */
3268 return stream_get_endp(s
) - cp
;
3271 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3273 unsigned long attrlen_pnt
;
3275 iana_safi_t pkt_safi
;
3277 /* Set extended bit always to encode the attribute length as 2 bytes */
3278 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3279 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3281 attrlen_pnt
= stream_get_endp(s
);
3282 stream_putw(s
, 0); /* Length of this attribute. */
3284 /* Convert AFI, SAFI to values for packet. */
3285 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3287 stream_putw(s
, pkt_afi
);
3288 stream_putc(s
, pkt_safi
);
3293 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3294 safi_t safi
, struct prefix_rd
*prd
,
3295 mpls_label_t
*label
, int addpath_encode
,
3296 u_int32_t addpath_tx_id
, struct attr
*attr
)
3298 u_char wlabel
[3] = {0x80, 0x00, 0x00};
3300 if (safi
== SAFI_LABELED_UNICAST
)
3301 label
= (mpls_label_t
*)wlabel
;
3303 return bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, label
,
3304 addpath_encode
, addpath_tx_id
, attr
);
3307 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3309 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3312 /* Initialization of attribute. */
3313 void bgp_attr_init(void)
3325 void bgp_attr_finish(void)
3330 ecommunity_finish();
3331 lcommunity_finish();
3337 /* Make attribute packet. */
3338 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3339 struct prefix
*prefix
)
3344 struct aspath
*aspath
;
3345 int addpath_encode
= 0;
3346 u_int32_t addpath_tx_id
= 0;
3348 /* Remember current pointer. */
3349 cp
= stream_get_endp(s
);
3351 /* Place holder of length. */
3354 /* Origin attribute. */
3355 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3356 stream_putc(s
, BGP_ATTR_ORIGIN
);
3358 stream_putc(s
, attr
->origin
);
3360 aspath
= attr
->aspath
;
3362 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3363 stream_putc(s
, BGP_ATTR_AS_PATH
);
3364 aspath_lenp
= stream_get_endp(s
);
3367 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3369 /* Nexthop attribute. */
3370 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3371 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3372 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3373 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3375 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3378 /* MED attribute. */
3379 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3380 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3381 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3383 stream_putl(s
, attr
->med
);
3386 /* Local preference. */
3387 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3388 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3389 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3391 stream_putl(s
, attr
->local_pref
);
3394 /* Atomic aggregate. */
3395 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3396 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3397 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3402 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3403 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3404 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3406 stream_putl(s
, attr
->aggregator_as
);
3407 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3410 /* Community attribute. */
3411 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3412 if (attr
->community
->size
* 4 > 255) {
3413 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3414 | BGP_ATTR_FLAG_TRANS
3415 | BGP_ATTR_FLAG_EXTLEN
);
3416 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3417 stream_putw(s
, attr
->community
->size
* 4);
3419 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3420 | BGP_ATTR_FLAG_TRANS
);
3421 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3422 stream_putc(s
, attr
->community
->size
* 4);
3424 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3427 /* Large Community attribute. */
3428 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3429 if (lcom_length(attr
->lcommunity
) > 255) {
3430 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3431 | BGP_ATTR_FLAG_TRANS
3432 | BGP_ATTR_FLAG_EXTLEN
);
3433 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3434 stream_putw(s
, lcom_length(attr
->lcommunity
));
3436 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3437 | BGP_ATTR_FLAG_TRANS
);
3438 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3439 stream_putc(s
, lcom_length(attr
->lcommunity
));
3442 stream_put(s
, attr
->lcommunity
->val
, lcom_length(attr
->lcommunity
));
3445 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3446 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
3447 && (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3448 || attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3451 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3452 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3453 sizep
= stream_get_endp(s
);
3456 stream_putc(s
, 0); /* Marker: Attribute length. */
3457 stream_putw(s
, AFI_IP6
); /* AFI */
3458 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3461 stream_putc(s
, attr
->mp_nexthop_len
);
3462 stream_put(s
, &attr
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3463 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3464 stream_put(s
, &attr
->mp_nexthop_local
,
3471 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3474 /* Set MP attribute length. */
3475 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3479 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3480 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
3481 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3482 | BGP_ATTR_FLAG_TRANS
);
3483 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3485 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3486 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3487 stream_putc(s
, 0); // reserved
3488 stream_putw(s
, 0); // flags
3489 stream_putl(s
, attr
->label_index
);
3493 /* Return total size of attribute. */
3494 len
= stream_get_endp(s
) - cp
- 2;
3495 stream_putw_at(s
, cp
, len
);