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 /* Check if this is a Gateway MAC-IP advertisement */
1880 attr
->default_gw
= bgp_attr_default_gw(attr
);
1882 /* Extract the Rmac, if any */
1883 bgp_attr_rmac(attr
, &attr
->rmac
);
1885 return BGP_ATTR_PARSE_PROCEED
;
1888 /* Parse Tunnel Encap attribute in an UPDATE */
1889 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
1890 bgp_size_t length
, /* IN: attr's length field */
1891 struct attr
*attr
, /* IN: caller already allocated */
1892 u_char flag
, /* IN: attr's flags field */
1896 uint16_t tunneltype
= 0;
1898 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1900 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1901 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
1903 "Tunnel Encap attribute flag isn't optional and transitive %d",
1905 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
1906 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1911 if (BGP_ATTR_ENCAP
== type
) {
1912 /* read outer TLV type and length */
1913 uint16_t tlv_length
;
1917 "Tunnel Encap attribute not long enough to contain outer T,L");
1918 bgp_notify_send_with_data(
1919 peer
, BGP_NOTIFY_UPDATE_ERR
,
1920 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1923 tunneltype
= stream_getw(BGP_INPUT(peer
));
1924 tlv_length
= stream_getw(BGP_INPUT(peer
));
1927 if (tlv_length
!= length
) {
1928 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
1929 tlv_length
, length
);
1933 while (length
>= 4) {
1934 uint16_t subtype
= 0;
1935 uint16_t sublength
= 0;
1936 struct bgp_attr_encap_subtlv
*tlv
;
1938 if (BGP_ATTR_ENCAP
== type
) {
1939 subtype
= stream_getc(BGP_INPUT(peer
));
1940 sublength
= stream_getc(BGP_INPUT(peer
));
1944 subtype
= stream_getw(BGP_INPUT(peer
));
1945 sublength
= stream_getw(BGP_INPUT(peer
));
1950 if (sublength
> length
) {
1952 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1954 bgp_notify_send_with_data(
1955 peer
, BGP_NOTIFY_UPDATE_ERR
,
1956 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
1960 /* alloc and copy sub-tlv */
1961 /* TBD make sure these are freed when attributes are released */
1962 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
1963 sizeof(struct bgp_attr_encap_subtlv
)
1965 tlv
->type
= subtype
;
1966 tlv
->length
= sublength
;
1967 stream_get(tlv
->value
, peer
->curr
, sublength
);
1968 length
-= sublength
;
1970 /* attach tlv to encap chain */
1971 if (BGP_ATTR_ENCAP
== type
) {
1972 struct bgp_attr_encap_subtlv
*stlv_last
;
1973 for (stlv_last
= attr
->encap_subtlvs
;
1974 stlv_last
&& stlv_last
->next
;
1975 stlv_last
= stlv_last
->next
)
1978 stlv_last
->next
= tlv
;
1980 attr
->encap_subtlvs
= tlv
;
1984 struct bgp_attr_encap_subtlv
*stlv_last
;
1985 for (stlv_last
= attr
->vnc_subtlvs
;
1986 stlv_last
&& stlv_last
->next
;
1987 stlv_last
= stlv_last
->next
)
1990 stlv_last
->next
= tlv
;
1992 attr
->vnc_subtlvs
= tlv
;
1998 if (BGP_ATTR_ENCAP
== type
) {
1999 attr
->encap_tunneltype
= tunneltype
;
2003 /* spurious leftover data */
2005 "Tunnel Encap attribute length is bad: %d leftover octets",
2007 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2008 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2016 /* Prefix SID attribute
2017 * draft-ietf-idr-bgp-prefix-sid-05
2019 static bgp_attr_parse_ret_t
2020 bgp_attr_prefix_sid(struct bgp_attr_parser_args
*args
,
2021 struct bgp_nlri
*mp_update
)
2023 struct peer
*const peer
= args
->peer
;
2024 struct attr
*const attr
= args
->attr
;
2027 u_int32_t label_index
;
2028 struct in6_addr ipv6_sid
;
2029 u_int32_t srgb_base
;
2030 u_int32_t srgb_range
;
2033 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
2035 type
= stream_getc(peer
->curr
);
2036 length
= stream_getw(peer
->curr
);
2038 if (type
== BGP_PREFIX_SID_LABEL_INDEX
) {
2039 if (length
!= BGP_PREFIX_SID_LABEL_INDEX_LENGTH
) {
2041 "Prefix SID label index length is %d instead of %d",
2042 length
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
2043 return bgp_attr_malformed(
2044 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2048 /* Ignore flags and reserved */
2049 stream_getc(peer
->curr
);
2050 stream_getw(peer
->curr
);
2052 /* Fetch the label index and see if it is valid. */
2053 label_index
= stream_getl(peer
->curr
);
2054 if (label_index
== BGP_INVALID_LABEL_INDEX
)
2055 return bgp_attr_malformed(
2056 args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2059 /* Store label index; subsequently, we'll check on
2061 attr
->label_index
= label_index
;
2064 * Ignore the Label index attribute unless received for
2068 if (!mp_update
->length
2069 || mp_update
->safi
!= SAFI_LABELED_UNICAST
)
2070 attr
->label_index
= BGP_INVALID_LABEL_INDEX
;
2073 /* Placeholder code for the IPv6 SID type */
2074 else if (type
== BGP_PREFIX_SID_IPV6
) {
2075 if (length
!= BGP_PREFIX_SID_IPV6_LENGTH
) {
2076 zlog_err("Prefix SID IPv6 length is %d instead of %d",
2077 length
, BGP_PREFIX_SID_IPV6_LENGTH
);
2078 return bgp_attr_malformed(
2079 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2083 /* Ignore reserved */
2084 stream_getc(peer
->curr
);
2085 stream_getw(peer
->curr
);
2087 stream_get(&ipv6_sid
, peer
->curr
, 16);
2090 /* Placeholder code for the Originator SRGB type */
2091 else if (type
== BGP_PREFIX_SID_ORIGINATOR_SRGB
) {
2093 stream_getw(peer
->curr
);
2097 if (length
% BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
) {
2099 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2100 length
, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
);
2101 return bgp_attr_malformed(
2102 args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2106 srgb_count
= length
/ BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH
;
2108 for (int i
= 0; i
< srgb_count
; i
++) {
2109 stream_get(&srgb_base
, peer
->curr
, 3);
2110 stream_get(&srgb_range
, peer
->curr
, 3);
2114 return BGP_ATTR_PARSE_PROCEED
;
2117 /* BGP unknown attribute treatment. */
2118 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2120 bgp_size_t total
= args
->total
;
2121 struct transit
*transit
;
2122 struct peer
*const peer
= args
->peer
;
2123 struct attr
*const attr
= args
->attr
;
2124 u_char
*const startp
= args
->startp
;
2125 const u_char type
= args
->type
;
2126 const u_char flag
= args
->flags
;
2127 const bgp_size_t length
= args
->length
;
2129 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2131 "%s Unknown attribute is received (type %d, length %d)",
2132 peer
->host
, type
, length
);
2134 /* Forward read pointer of input stream. */
2135 stream_forward_getp(peer
->curr
, length
);
2137 /* If any of the mandatory well-known attributes are not recognized,
2138 then the Error Subcode is set to Unrecognized Well-known
2139 Attribute. The Data field contains the unrecognized attribute
2140 (type, length and value). */
2141 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2142 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2146 /* Unrecognized non-transitive optional attributes must be quietly
2147 ignored and not passed along to other BGP peers. */
2148 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2149 return BGP_ATTR_PARSE_PROCEED
;
2151 /* If a path with recognized transitive optional attribute is
2152 accepted and passed along to other BGP peers and the Partial bit
2153 in the Attribute Flags octet is set to 1 by some previous AS, it
2154 is not set back to 0 by the current AS. */
2155 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2157 /* Store transitive attribute to the end of attr->transit. */
2159 attr
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2161 transit
= attr
->transit
;
2164 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2165 transit
->length
+ total
);
2167 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2169 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2170 transit
->length
+= total
;
2172 return BGP_ATTR_PARSE_PROCEED
;
2175 /* Well-known attribute check. */
2176 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2180 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2182 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2183 return BGP_ATTR_PARSE_PROCEED
;
2185 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2186 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2187 are present, it should. Check for any other attribute being present
2190 if (attr
->flag
== ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))
2191 return BGP_ATTR_PARSE_PROCEED
;
2193 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2194 type
= BGP_ATTR_ORIGIN
;
2196 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2197 type
= BGP_ATTR_AS_PATH
;
2199 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2201 * NLRI is empty. We can't easily check NLRI empty here though.
2203 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2204 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2205 type
= BGP_ATTR_NEXT_HOP
;
2207 if (peer
->sort
== BGP_PEER_IBGP
2208 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2209 type
= BGP_ATTR_LOCAL_PREF
;
2212 zlog_warn("%s Missing well-known attribute %s.", peer
->host
,
2213 lookup_msg(attr_str
, type
, NULL
));
2214 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2215 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2217 return BGP_ATTR_PARSE_ERROR
;
2219 return BGP_ATTR_PARSE_PROCEED
;
2222 /* Read attribute of update packet. This function is called from
2223 bgp_update_receive() in bgp_packet.c. */
2224 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2225 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2226 struct bgp_nlri
*mp_withdraw
)
2232 u_char
*startp
, *endp
;
2234 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2235 /* we need the as4_path only until we have synthesized the as_path with
2237 /* same goes for as4_aggregator */
2238 struct aspath
*as4_path
= NULL
;
2239 as_t as4_aggregator
= 0;
2240 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2242 /* Initialize bitmap. */
2243 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2245 /* End pointer of BGP attribute. */
2246 endp
= BGP_INPUT_PNT(peer
) + size
;
2248 /* Get attributes to the end of attribute length. */
2249 while (BGP_INPUT_PNT(peer
) < endp
) {
2250 /* Check remaining length check.*/
2251 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2252 /* XXX warning: long int format, int arg (arg 5) */
2254 "%s: error BGP attribute length %lu is smaller than min len",
2256 (unsigned long)(endp
2257 - stream_pnt(BGP_INPUT(peer
))));
2259 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2260 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2261 return BGP_ATTR_PARSE_ERROR
;
2264 /* Fetch attribute flag and type. */
2265 startp
= BGP_INPUT_PNT(peer
);
2266 /* "The lower-order four bits of the Attribute Flags octet are
2267 unused. They MUST be zero when sent and MUST be ignored when
2269 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2270 type
= stream_getc(BGP_INPUT(peer
));
2272 /* Check whether Extended-Length applies and is in bounds */
2273 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2274 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2276 "%s: Extended length set, but just %lu bytes of attr header",
2278 (unsigned long)(endp
2279 - stream_pnt(BGP_INPUT(peer
))));
2281 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2282 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2283 return BGP_ATTR_PARSE_ERROR
;
2286 /* Check extended attribue length bit. */
2287 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2288 length
= stream_getw(BGP_INPUT(peer
));
2290 length
= stream_getc(BGP_INPUT(peer
));
2292 /* If any attribute appears more than once in the UPDATE
2293 message, then the Error Subcode is set to Malformed Attribute
2296 if (CHECK_BITMAP(seen
, type
)) {
2298 "%s: error BGP attribute type %d appears twice in a message",
2301 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2302 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2303 return BGP_ATTR_PARSE_ERROR
;
2306 /* Set type to bitmap to check duplicate attribute. `type' is
2307 unsigned char so it never overflow bitmap range. */
2309 SET_BITMAP(seen
, type
);
2311 /* Overflow check. */
2312 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2314 if (attr_endp
> endp
) {
2316 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2317 peer
->host
, type
, length
, size
, attr_endp
,
2321 * If any recognized attribute has an Attribute
2322 * Length that conflicts with the expected length
2323 * (based on the attribute type code), then the
2324 * Error Subcode MUST be set to Attribute Length
2325 * Error. The Data field MUST contain the erroneous
2326 * attribute (type, length, and value).
2328 * We do not currently have a good way to determine the
2329 * length of the attribute independent of the length
2330 * received in the message. Instead we send the
2331 * minimum between the amount of data we have and the
2332 * amount specified by the attribute length field.
2334 * Instead of directly passing in the packet buffer and
2335 * offset we use the stream_get* functions to read into
2336 * a stack buffer, since they perform bounds checking
2337 * and we are working with untrusted data.
2339 unsigned char ndata
[BGP_MAX_PACKET_SIZE
];
2340 memset(ndata
, 0x00, sizeof(ndata
));
2342 CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 2 : 1;
2343 /* Rewind to end of flag field */
2344 stream_forward_getp(BGP_INPUT(peer
), -(1 + lfl
));
2346 stream_get(&ndata
[0], BGP_INPUT(peer
), 1);
2348 stream_get(&ndata
[1], BGP_INPUT(peer
), lfl
);
2350 size_t atl
= attr_endp
- startp
;
2351 size_t ndl
= MIN(atl
, STREAM_READABLE(BGP_INPUT(peer
)));
2352 stream_get(&ndata
[lfl
+ 1], BGP_INPUT(peer
), ndl
);
2354 bgp_notify_send_with_data(
2355 peer
, BGP_NOTIFY_UPDATE_ERR
,
2356 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, ndata
,
2359 return BGP_ATTR_PARSE_ERROR
;
2362 struct bgp_attr_parser_args attr_args
= {
2369 .total
= attr_endp
- startp
,
2373 /* If any recognized attribute has Attribute Flags that conflict
2374 with the Attribute Type Code, then the Error Subcode is set
2376 Attribute Flags Error. The Data field contains the erroneous
2377 attribute (type, length and value). */
2378 if (bgp_attr_flag_invalid(&attr_args
)) {
2379 bgp_attr_parse_ret_t ret
;
2380 ret
= bgp_attr_malformed(
2381 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2383 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2388 /* OK check attribute and store it's value. */
2390 case BGP_ATTR_ORIGIN
:
2391 ret
= bgp_attr_origin(&attr_args
);
2393 case BGP_ATTR_AS_PATH
:
2394 ret
= bgp_attr_aspath(&attr_args
);
2396 case BGP_ATTR_AS4_PATH
:
2397 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2399 case BGP_ATTR_NEXT_HOP
:
2400 ret
= bgp_attr_nexthop(&attr_args
);
2402 case BGP_ATTR_MULTI_EXIT_DISC
:
2403 ret
= bgp_attr_med(&attr_args
);
2405 case BGP_ATTR_LOCAL_PREF
:
2406 ret
= bgp_attr_local_pref(&attr_args
);
2408 case BGP_ATTR_ATOMIC_AGGREGATE
:
2409 ret
= bgp_attr_atomic(&attr_args
);
2411 case BGP_ATTR_AGGREGATOR
:
2412 ret
= bgp_attr_aggregator(&attr_args
);
2414 case BGP_ATTR_AS4_AGGREGATOR
:
2415 ret
= bgp_attr_as4_aggregator(&attr_args
,
2417 &as4_aggregator_addr
);
2419 case BGP_ATTR_COMMUNITIES
:
2420 ret
= bgp_attr_community(&attr_args
);
2422 case BGP_ATTR_LARGE_COMMUNITIES
:
2423 ret
= bgp_attr_large_community(&attr_args
);
2425 case BGP_ATTR_ORIGINATOR_ID
:
2426 ret
= bgp_attr_originator_id(&attr_args
);
2428 case BGP_ATTR_CLUSTER_LIST
:
2429 ret
= bgp_attr_cluster_list(&attr_args
);
2431 case BGP_ATTR_MP_REACH_NLRI
:
2432 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2434 case BGP_ATTR_MP_UNREACH_NLRI
:
2435 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2437 case BGP_ATTR_EXT_COMMUNITIES
:
2438 ret
= bgp_attr_ext_communities(&attr_args
);
2443 case BGP_ATTR_ENCAP
:
2444 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2447 case BGP_ATTR_PREFIX_SID
:
2448 ret
= bgp_attr_prefix_sid(&attr_args
, mp_update
);
2451 ret
= bgp_attr_unknown(&attr_args
);
2455 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2456 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2457 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2458 ret
= BGP_ATTR_PARSE_ERROR
;
2461 if (ret
== BGP_ATTR_PARSE_EOR
) {
2463 aspath_unintern(&as4_path
);
2467 /* If hard error occured immediately return to the caller. */
2468 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2469 zlog_warn("%s: Attribute %s, parse error", peer
->host
,
2470 lookup_msg(attr_str
, type
, NULL
));
2472 aspath_unintern(&as4_path
);
2475 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2478 "%s: Attribute %s, parse error - treating as withdrawal",
2479 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2481 aspath_unintern(&as4_path
);
2485 /* Check the fetched length. */
2486 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2487 zlog_warn("%s: BGP attribute %s, fetch error",
2488 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2489 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2490 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2492 aspath_unintern(&as4_path
);
2493 return BGP_ATTR_PARSE_ERROR
;
2497 /* Check final read pointer is same as end pointer. */
2498 if (BGP_INPUT_PNT(peer
) != endp
) {
2499 zlog_warn("%s: BGP attribute %s, length mismatch", peer
->host
,
2500 lookup_msg(attr_str
, type
, NULL
));
2501 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2502 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2504 aspath_unintern(&as4_path
);
2505 return BGP_ATTR_PARSE_ERROR
;
2508 /* Check all mandatory well-known attributes are present */
2510 bgp_attr_parse_ret_t ret
;
2511 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2513 aspath_unintern(&as4_path
);
2519 * At this place we can see whether we got AS4_PATH and/or
2520 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2521 * We can not do this before we've read all attributes because
2522 * the as4 handling does not say whether AS4_PATH has to be sent
2523 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2524 * in relationship to AGGREGATOR.
2525 * So, to be defensive, we are not relying on any order and read
2526 * all attributes first, including these 32bit ones, and now,
2527 * afterwards, we look what and if something is to be done for as4.
2529 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2532 /* actually... this doesn't ever return failure currently, but
2533 * better safe than sorry */
2534 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2535 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2536 &as4_aggregator_addr
)) {
2537 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2538 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2540 aspath_unintern(&as4_path
);
2541 return BGP_ATTR_PARSE_ERROR
;
2544 /* At this stage, we have done all fiddling with as4, and the
2545 * resulting info is in attr->aggregator resp. attr->aspath
2546 * so we can chuck as4_aggregator and as4_path alltogether in
2547 * order to save memory
2550 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2551 /* The flag that we got this is still there, but that does not
2556 * The "rest" of the code does nothing with as4_aggregator.
2557 * there is no memory attached specifically which is not part
2559 * so ignoring just means do nothing.
2562 * Finally do the checks on the aspath we did not do yet
2563 * because we waited for a potentially synthesized aspath.
2565 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2566 ret
= bgp_attr_aspath_check(peer
, attr
);
2567 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2570 /* Finally intern unknown attribute. */
2572 attr
->transit
= transit_intern(attr
->transit
);
2573 if (attr
->encap_subtlvs
)
2574 attr
->encap_subtlvs
=
2575 encap_intern(attr
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2577 if (attr
->vnc_subtlvs
)
2579 encap_intern(attr
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2582 return BGP_ATTR_PARSE_PROCEED
;
2585 size_t bgp_packet_mpattr_start(struct stream
*s
, struct peer
*peer
, afi_t afi
,
2586 safi_t safi
, struct bpacket_attr_vec_arr
*vecarr
,
2591 iana_safi_t pkt_safi
;
2594 /* Set extended bit always to encode the attribute length as 2 bytes */
2595 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2596 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2597 sizep
= stream_get_endp(s
);
2598 stream_putw(s
, 0); /* Marker: Attribute length. */
2601 /* Convert AFI, SAFI to values for packet. */
2602 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2604 stream_putw(s
, pkt_afi
); /* AFI */
2605 stream_putc(s
, pkt_safi
); /* SAFI */
2609 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
))
2610 nh_afi
= peer_cap_enhe(peer
, afi
, safi
) ? AFI_IP6
: AFI_IP
;
2612 nh_afi
= BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->mp_nexthop_len
);
2615 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2620 case SAFI_MULTICAST
:
2621 case SAFI_LABELED_UNICAST
:
2623 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2627 stream_putl(s
, 0); /* RD = 0, per RFC */
2629 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2634 stream_put(s
, &attr
->mp_nexthop_global_in
, 4);
2643 case SAFI_MULTICAST
:
2644 case SAFI_LABELED_UNICAST
:
2646 if (attr
->mp_nexthop_len
2647 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2649 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
);
2650 stream_put(s
, &attr
->mp_nexthop_global
,
2652 stream_put(s
, &attr
->mp_nexthop_local
,
2655 stream_putc(s
, IPV6_MAX_BYTELEN
);
2656 stream_put(s
, &attr
->mp_nexthop_global
,
2660 case SAFI_MPLS_VPN
: {
2661 if (attr
->mp_nexthop_len
2662 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2664 stream_putl(s
, 0); /* RD = 0, per RFC */
2666 stream_put(s
, &attr
->mp_nexthop_global
,
2668 } else if (attr
->mp_nexthop_len
2669 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2671 stream_putl(s
, 0); /* RD = 0, per RFC */
2673 stream_put(s
, &attr
->mp_nexthop_global
,
2675 stream_putl(s
, 0); /* RD = 0, per RFC */
2677 stream_put(s
, &attr
->mp_nexthop_local
,
2682 stream_putc(s
, IPV6_MAX_BYTELEN
);
2683 stream_put(s
, &attr
->mp_nexthop_global
,
2692 "Bad nexthop when sening to %s, AFI %u SAFI %u nhlen %d",
2693 peer
->host
, afi
, safi
, attr
->mp_nexthop_len
);
2702 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2703 struct prefix
*p
, struct prefix_rd
*prd
,
2704 mpls_label_t
*label
, u_int32_t num_labels
,
2705 int addpath_encode
, u_int32_t addpath_tx_id
,
2708 if (safi
== SAFI_MPLS_VPN
) {
2710 stream_putl(s
, addpath_tx_id
);
2711 /* Label, RD, Prefix write. */
2712 stream_putc(s
, p
->prefixlen
+ 88);
2713 stream_put(s
, label
, BGP_LABEL_BYTES
);
2714 stream_put(s
, prd
->val
, 8);
2715 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2716 } else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
2717 /* EVPN prefix - contents depend on type */
2718 bgp_evpn_encode_prefix(s
, p
, prd
, label
, num_labels
,
2719 attr
, addpath_encode
, addpath_tx_id
);
2720 } else if (safi
== SAFI_LABELED_UNICAST
) {
2721 /* Prefix write with label. */
2722 stream_put_labeled_prefix(s
, p
, label
);
2724 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2727 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2729 int size
= PSIZE(p
->prefixlen
);
2730 if (safi
== SAFI_MPLS_VPN
)
2732 else if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2733 size
+= 232; // TODO: Maximum possible for type-2, type-3 and
2739 * Encodes the tunnel encapsulation attribute,
2740 * and with ENABLE_BGP_VNC the VNC attribute which uses
2741 * almost the same TLV format
2743 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2744 struct stream
*s
, struct attr
*attr
,
2747 unsigned int attrlenfield
= 0;
2748 unsigned int attrhdrlen
= 0;
2749 struct bgp_attr_encap_subtlv
*subtlvs
;
2750 struct bgp_attr_encap_subtlv
*st
;
2751 const char *attrname
;
2753 if (!attr
|| (attrtype
== BGP_ATTR_ENCAP
2754 && (!attr
->encap_tunneltype
2755 || attr
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2759 case BGP_ATTR_ENCAP
:
2760 attrname
= "Tunnel Encap";
2761 subtlvs
= attr
->encap_subtlvs
;
2762 if (subtlvs
== NULL
) /* nothing to do */
2765 * The tunnel encap attr has an "outer" tlv.
2767 * L = total length of subtlvs,
2768 * V = concatenated subtlvs.
2770 attrlenfield
= 2 + 2; /* T + L */
2771 attrhdrlen
= 1 + 1; /* subTLV T + L */
2777 subtlvs
= attr
->vnc_subtlvs
;
2778 if (subtlvs
== NULL
) /* nothing to do */
2780 attrlenfield
= 0; /* no outer T + L */
2781 attrhdrlen
= 2 + 2; /* subTLV T + L */
2789 /* compute attr length */
2790 for (st
= subtlvs
; st
; st
= st
->next
) {
2791 attrlenfield
+= (attrhdrlen
+ st
->length
);
2794 if (attrlenfield
> 0xffff) {
2795 zlog_info("%s attribute is too long (length=%d), can't send it",
2796 attrname
, attrlenfield
);
2800 if (attrlenfield
> 0xff) {
2801 /* 2-octet length field */
2802 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2803 | BGP_ATTR_FLAG_EXTLEN
);
2804 stream_putc(s
, attrtype
);
2805 stream_putw(s
, attrlenfield
& 0xffff);
2807 /* 1-octet length field */
2808 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2809 stream_putc(s
, attrtype
);
2810 stream_putc(s
, attrlenfield
& 0xff);
2813 if (attrtype
== BGP_ATTR_ENCAP
) {
2814 /* write outer T+L */
2815 stream_putw(s
, attr
->encap_tunneltype
);
2816 stream_putw(s
, attrlenfield
- 4);
2819 /* write each sub-tlv */
2820 for (st
= subtlvs
; st
; st
= st
->next
) {
2821 if (attrtype
== BGP_ATTR_ENCAP
) {
2822 stream_putc(s
, st
->type
);
2823 stream_putc(s
, st
->length
);
2826 stream_putw(s
, st
->type
);
2827 stream_putw(s
, st
->length
);
2830 stream_put(s
, st
->value
, st
->length
);
2834 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
2836 /* Set MP attribute length. Don't count the (2) bytes used to encode
2838 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
2841 /* Make attribute packet. */
2842 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
2843 struct stream
*s
, struct attr
*attr
,
2844 struct bpacket_attr_vec_arr
*vecarr
,
2845 struct prefix
*p
, afi_t afi
, safi_t safi
,
2846 struct peer
*from
, struct prefix_rd
*prd
,
2847 mpls_label_t
*label
, u_int32_t num_labels
,
2848 int addpath_encode
, u_int32_t addpath_tx_id
)
2851 size_t aspath_sizep
;
2852 struct aspath
*aspath
;
2853 int send_as4_path
= 0;
2854 int send_as4_aggregator
= 0;
2855 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2860 /* Remember current pointer. */
2861 cp
= stream_get_endp(s
);
2864 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
2865 && !peer_cap_enhe(peer
, afi
, safi
))) {
2866 size_t mpattrlen_pos
= 0;
2868 mpattrlen_pos
= bgp_packet_mpattr_start(s
, peer
, afi
, safi
,
2870 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
,
2872 addpath_encode
, addpath_tx_id
, attr
);
2873 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2876 /* Origin attribute. */
2877 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2878 stream_putc(s
, BGP_ATTR_ORIGIN
);
2880 stream_putc(s
, attr
->origin
);
2882 /* AS path attribute. */
2884 /* If remote-peer is EBGP */
2885 if (peer
->sort
== BGP_PEER_EBGP
2886 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2887 PEER_FLAG_AS_PATH_UNCHANGED
)
2888 || attr
->aspath
->segments
== NULL
)
2889 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2890 PEER_FLAG_RSERVER_CLIENT
))) {
2891 aspath
= aspath_dup(attr
->aspath
);
2893 /* Even though we may not be configured for confederations we
2895 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2896 aspath
= aspath_delete_confed_seq(aspath
);
2898 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
2899 /* Stuff our path CONFED_ID on the front */
2900 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
2902 if (peer
->change_local_as
) {
2903 /* If replace-as is specified, we only use the
2904 change_local_as when
2905 advertising routes. */
2908 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
2909 aspath
= aspath_add_seq(aspath
,
2912 aspath
= aspath_add_seq(aspath
,
2913 peer
->change_local_as
);
2915 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
2918 } else if (peer
->sort
== BGP_PEER_CONFED
) {
2919 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2921 aspath
= aspath_dup(attr
->aspath
);
2922 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
2924 aspath
= attr
->aspath
;
2926 /* If peer is not AS4 capable, then:
2927 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2928 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2930 * types are in it (i.e. exclude them if they are there)
2931 * AND do this only if there is at least one asnum > 65535 in the
2933 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2935 * all ASnums > 65535 to BGP_AS_TRANS
2938 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
2939 stream_putc(s
, BGP_ATTR_AS_PATH
);
2940 aspath_sizep
= stream_get_endp(s
);
2942 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
2944 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2947 if (!use32bit
&& aspath_has_as4(aspath
))
2949 1; /* we'll do this later, at the correct place */
2951 /* Nexthop attribute. */
2952 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
2953 && !peer_cap_enhe(peer
, afi
, safi
)) {
2954 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2955 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2956 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2957 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2960 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2961 } else if (peer_cap_enhe(from
, afi
, safi
)) {
2963 * Likely this is the case when an IPv4 prefix was
2965 * Extended Next-hop capability and now being advertised
2968 * Setting the mandatory (ipv4) next-hop attribute here
2970 * implicit next-hop self with correct (ipv4 address
2973 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2974 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2975 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2978 stream_put_ipv4(s
, 0);
2982 /* MED attribute. */
2983 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
2984 || bgp
->maxmed_active
) {
2985 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
2986 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
2988 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
2992 /* Local preference. */
2993 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
2994 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2995 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
2997 stream_putl(s
, attr
->local_pref
);
3000 /* Atomic aggregate. */
3001 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3002 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3003 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3008 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3009 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3010 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3011 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3014 /* AS4 capable peer */
3016 stream_putl(s
, attr
->aggregator_as
);
3018 /* 2-byte AS peer */
3021 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3023 if (attr
->aggregator_as
> 65535) {
3024 stream_putw(s
, BGP_AS_TRANS
);
3026 /* we have to send AS4_AGGREGATOR, too.
3027 * we'll do that later in order to send
3028 * attributes in ascending
3031 send_as4_aggregator
= 1;
3033 stream_putw(s
, (u_int16_t
)attr
->aggregator_as
);
3035 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3038 /* Community attribute. */
3039 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3040 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3041 if (attr
->community
->size
* 4 > 255) {
3042 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3043 | BGP_ATTR_FLAG_TRANS
3044 | BGP_ATTR_FLAG_EXTLEN
);
3045 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3046 stream_putw(s
, attr
->community
->size
* 4);
3048 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3049 | BGP_ATTR_FLAG_TRANS
);
3050 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3051 stream_putc(s
, attr
->community
->size
* 4);
3053 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3057 * Large Community attribute.
3059 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3060 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3061 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3062 if (lcom_length(attr
->lcommunity
) > 255) {
3063 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3064 | BGP_ATTR_FLAG_TRANS
3065 | BGP_ATTR_FLAG_EXTLEN
);
3066 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3067 stream_putw(s
, lcom_length(attr
->lcommunity
));
3069 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3070 | BGP_ATTR_FLAG_TRANS
);
3071 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3072 stream_putc(s
, lcom_length(attr
->lcommunity
));
3074 stream_put(s
, attr
->lcommunity
->val
,
3075 lcom_length(attr
->lcommunity
));
3078 /* Route Reflector. */
3079 if (peer
->sort
== BGP_PEER_IBGP
&& from
3080 && from
->sort
== BGP_PEER_IBGP
) {
3081 /* Originator ID. */
3082 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3083 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3086 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3087 stream_put_in_addr(s
, &attr
->originator_id
);
3089 stream_put_in_addr(s
, &from
->remote_id
);
3092 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3093 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3095 if (attr
->cluster
) {
3096 stream_putc(s
, attr
->cluster
->length
+ 4);
3097 /* If this peer configuration's parent BGP has
3099 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3100 stream_put_in_addr(s
, &bgp
->cluster_id
);
3102 stream_put_in_addr(s
, &bgp
->router_id
);
3103 stream_put(s
, attr
->cluster
->list
,
3104 attr
->cluster
->length
);
3107 /* If this peer configuration's parent BGP has
3109 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3110 stream_put_in_addr(s
, &bgp
->cluster_id
);
3112 stream_put_in_addr(s
, &bgp
->router_id
);
3116 /* Extended Communities attribute. */
3117 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3118 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3119 if (peer
->sort
== BGP_PEER_IBGP
3120 || peer
->sort
== BGP_PEER_CONFED
) {
3121 if (attr
->ecommunity
->size
* 8 > 255) {
3122 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3123 | BGP_ATTR_FLAG_TRANS
3124 | BGP_ATTR_FLAG_EXTLEN
);
3125 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3126 stream_putw(s
, attr
->ecommunity
->size
* 8);
3128 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3129 | BGP_ATTR_FLAG_TRANS
);
3130 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3131 stream_putc(s
, attr
->ecommunity
->size
* 8);
3133 stream_put(s
, attr
->ecommunity
->val
,
3134 attr
->ecommunity
->size
* 8);
3138 int ecom_tr_size
= 0;
3141 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3142 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3145 if (CHECK_FLAG(tbit
,
3146 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3153 if (ecom_tr_size
* 8 > 255) {
3156 BGP_ATTR_FLAG_OPTIONAL
3157 | BGP_ATTR_FLAG_TRANS
3158 | BGP_ATTR_FLAG_EXTLEN
);
3160 BGP_ATTR_EXT_COMMUNITIES
);
3161 stream_putw(s
, ecom_tr_size
* 8);
3165 BGP_ATTR_FLAG_OPTIONAL
3166 | BGP_ATTR_FLAG_TRANS
);
3168 BGP_ATTR_EXT_COMMUNITIES
);
3169 stream_putc(s
, ecom_tr_size
* 8);
3172 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
3173 pnt
= attr
->ecommunity
->val
+ (i
* 8);
3178 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3181 stream_put(s
, pnt
, 8);
3187 /* Label index attribute. */
3188 if (safi
== SAFI_LABELED_UNICAST
) {
3189 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3190 u_int32_t label_index
;
3192 label_index
= attr
->label_index
;
3194 if (label_index
!= BGP_INVALID_LABEL_INDEX
) {
3195 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3196 | BGP_ATTR_FLAG_TRANS
);
3197 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3199 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3201 BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3202 stream_putc(s
, 0); // reserved
3203 stream_putw(s
, 0); // flags
3204 stream_putl(s
, label_index
);
3209 if (send_as4_path
) {
3210 /* If the peer is NOT As4 capable, AND */
3211 /* there are ASnums > 65535 in path THEN
3212 * give out AS4_PATH */
3214 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3216 * Hm, I wonder... confederation things *should* only be at
3217 * the beginning of an aspath, right? Then we should use
3218 * aspath_delete_confed_seq for this, because it is already
3220 * Folks, talk to me: what is reasonable here!?
3222 aspath
= aspath_delete_confed_seq(aspath
);
3224 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3225 | BGP_ATTR_FLAG_EXTLEN
);
3226 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3227 aspath_sizep
= stream_get_endp(s
);
3229 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3232 if (aspath
!= attr
->aspath
)
3233 aspath_free(aspath
);
3235 if (send_as4_aggregator
) {
3236 /* send AS4_AGGREGATOR, at this place */
3237 /* this section of code moved here in order to ensure the
3239 * *ascending* order of attributes
3241 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3242 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3244 stream_putl(s
, attr
->aggregator_as
);
3245 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3248 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3249 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3250 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3251 /* Tunnel Encap attribute */
3252 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3256 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3261 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
3262 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3263 stream_putc(s
, BGP_ATTR_PMSI_TUNNEL
);
3264 stream_putc(s
, 9); // Length
3265 stream_putc(s
, 0); // Flags
3266 stream_putc(s
, 6); // Tunnel type: Ingress Replication (6)
3267 stream_put(s
, &(attr
->label
), BGP_LABEL_BYTES
); // MPLS Label / VXLAN VNI
3268 stream_put_ipv4(s
, attr
->nexthop
.s_addr
); // Unicast tunnel endpoint IP address
3271 /* Unknown transit attribute. */
3273 stream_put(s
, attr
->transit
->val
, attr
->transit
->length
);
3275 /* Return total size of attribute. */
3276 return stream_get_endp(s
) - cp
;
3279 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3281 unsigned long attrlen_pnt
;
3283 iana_safi_t pkt_safi
;
3285 /* Set extended bit always to encode the attribute length as 2 bytes */
3286 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3287 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3289 attrlen_pnt
= stream_get_endp(s
);
3290 stream_putw(s
, 0); /* Length of this attribute. */
3292 /* Convert AFI, SAFI to values for packet. */
3293 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3295 stream_putw(s
, pkt_afi
);
3296 stream_putc(s
, pkt_safi
);
3301 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3302 safi_t safi
, struct prefix_rd
*prd
,
3303 mpls_label_t
*label
, u_int32_t num_labels
,
3304 int addpath_encode
, u_int32_t addpath_tx_id
,
3307 u_char wlabel
[3] = {0x80, 0x00, 0x00};
3309 if (safi
== SAFI_LABELED_UNICAST
) {
3310 label
= (mpls_label_t
*)wlabel
;
3314 return bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
,
3316 addpath_encode
, addpath_tx_id
, attr
);
3319 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3321 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3324 /* Initialization of attribute. */
3325 void bgp_attr_init(void)
3337 void bgp_attr_finish(void)
3342 ecommunity_finish();
3343 lcommunity_finish();
3349 /* Make attribute packet. */
3350 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3351 struct prefix
*prefix
)
3356 struct aspath
*aspath
;
3357 int addpath_encode
= 0;
3358 u_int32_t addpath_tx_id
= 0;
3360 /* Remember current pointer. */
3361 cp
= stream_get_endp(s
);
3363 /* Place holder of length. */
3366 /* Origin attribute. */
3367 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3368 stream_putc(s
, BGP_ATTR_ORIGIN
);
3370 stream_putc(s
, attr
->origin
);
3372 aspath
= attr
->aspath
;
3374 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3375 stream_putc(s
, BGP_ATTR_AS_PATH
);
3376 aspath_lenp
= stream_get_endp(s
);
3379 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3381 /* Nexthop attribute. */
3382 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3383 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3384 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3385 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3387 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3390 /* MED attribute. */
3391 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3392 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3393 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3395 stream_putl(s
, attr
->med
);
3398 /* Local preference. */
3399 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3400 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3401 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3403 stream_putl(s
, attr
->local_pref
);
3406 /* Atomic aggregate. */
3407 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3408 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3409 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3414 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3415 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3416 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3418 stream_putl(s
, attr
->aggregator_as
);
3419 stream_put_ipv4(s
, attr
->aggregator_addr
.s_addr
);
3422 /* Community attribute. */
3423 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3424 if (attr
->community
->size
* 4 > 255) {
3425 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3426 | BGP_ATTR_FLAG_TRANS
3427 | BGP_ATTR_FLAG_EXTLEN
);
3428 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3429 stream_putw(s
, attr
->community
->size
* 4);
3431 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3432 | BGP_ATTR_FLAG_TRANS
);
3433 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3434 stream_putc(s
, attr
->community
->size
* 4);
3436 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3439 /* Large Community attribute. */
3440 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3441 if (lcom_length(attr
->lcommunity
) > 255) {
3442 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3443 | BGP_ATTR_FLAG_TRANS
3444 | BGP_ATTR_FLAG_EXTLEN
);
3445 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3446 stream_putw(s
, lcom_length(attr
->lcommunity
));
3448 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3449 | BGP_ATTR_FLAG_TRANS
);
3450 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3451 stream_putc(s
, lcom_length(attr
->lcommunity
));
3454 stream_put(s
, attr
->lcommunity
->val
, lcom_length(attr
->lcommunity
));
3457 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3458 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
3459 && (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3460 || attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3463 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3464 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3465 sizep
= stream_get_endp(s
);
3468 stream_putc(s
, 0); /* Marker: Attribute length. */
3469 stream_putw(s
, AFI_IP6
); /* AFI */
3470 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3473 stream_putc(s
, attr
->mp_nexthop_len
);
3474 stream_put(s
, &attr
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3475 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3476 stream_put(s
, &attr
->mp_nexthop_local
,
3483 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3486 /* Set MP attribute length. */
3487 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3491 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
)) {
3492 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
3493 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
3494 | BGP_ATTR_FLAG_TRANS
);
3495 stream_putc(s
, BGP_ATTR_PREFIX_SID
);
3497 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX
);
3498 stream_putc(s
, BGP_PREFIX_SID_LABEL_INDEX_LENGTH
);
3499 stream_putc(s
, 0); // reserved
3500 stream_putw(s
, 0); // flags
3501 stream_putl(s
, attr
->label_index
);
3505 /* Return total size of attribute. */
3506 len
= stream_get_endp(s
) - cp
- 2;
3507 stream_putw_at(s
, cp
, len
);