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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
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_packet.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_lcommunity.h"
45 #include "bgpd/bgp_updgrp.h"
46 #include "bgpd/bgp_encap_types.h"
48 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
49 #include "bgp_encap_types.h"
50 #include "bgp_vnc_types.h"
52 #include "bgp_encap_types.h"
55 /* Attribute strings for logging. */
56 static const struct message attr_str
[] = {
57 {BGP_ATTR_ORIGIN
, "ORIGIN"},
58 {BGP_ATTR_AS_PATH
, "AS_PATH"},
59 {BGP_ATTR_NEXT_HOP
, "NEXT_HOP"},
60 {BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC"},
61 {BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF"},
62 {BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE"},
63 {BGP_ATTR_AGGREGATOR
, "AGGREGATOR"},
64 {BGP_ATTR_COMMUNITIES
, "COMMUNITY"},
65 {BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID"},
66 {BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST"},
67 {BGP_ATTR_DPA
, "DPA"},
68 {BGP_ATTR_ADVERTISER
, "ADVERTISER"},
69 {BGP_ATTR_RCID_PATH
, "RCID_PATH"},
70 {BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI"},
71 {BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI"},
72 {BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES"},
73 {BGP_ATTR_AS4_PATH
, "AS4_PATH"},
74 {BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR"},
75 {BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT"},
76 {BGP_ATTR_ENCAP
, "ENCAP"},
78 {BGP_ATTR_VNC
, "VNC"},
80 {BGP_ATTR_LARGE_COMMUNITIES
, "LARGE_COMMUNITY"},
83 static const struct message attr_flag_str
[] =
85 {BGP_ATTR_FLAG_OPTIONAL
, "Optional"},
86 {BGP_ATTR_FLAG_TRANS
, "Transitive"},
87 {BGP_ATTR_FLAG_PARTIAL
, "Partial"},
88 /* bgp_attr_flags_diagnose() relies on this bit being last in
90 {BGP_ATTR_FLAG_EXTLEN
, "Extended Length"},
93 static struct hash
*cluster_hash
;
95 static void *cluster_hash_alloc(void *p
)
97 const struct cluster_list
*val
= (const struct cluster_list
*)p
;
98 struct cluster_list
*cluster
;
100 cluster
= XMALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
101 cluster
->length
= val
->length
;
103 if (cluster
->length
) {
104 cluster
->list
= XMALLOC(MTYPE_CLUSTER_VAL
, val
->length
);
105 memcpy(cluster
->list
, val
->list
, val
->length
);
107 cluster
->list
= NULL
;
114 /* Cluster list related functions. */
115 static struct cluster_list
*cluster_parse(struct in_addr
*pnt
, int length
)
117 struct cluster_list tmp
;
118 struct cluster_list
*cluster
;
123 cluster
= hash_get(cluster_hash
, &tmp
, cluster_hash_alloc
);
128 int cluster_loop_check(struct cluster_list
*cluster
, struct in_addr originator
)
132 for (i
= 0; i
< cluster
->length
/ 4; i
++)
133 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
138 static unsigned int cluster_hash_key_make(void *p
)
140 const struct cluster_list
*cluster
= p
;
142 return jhash(cluster
->list
, cluster
->length
, 0);
145 static int cluster_hash_cmp(const void *p1
, const void *p2
)
147 const struct cluster_list
*cluster1
= p1
;
148 const struct cluster_list
*cluster2
= p2
;
150 return (cluster1
->length
== cluster2
->length
151 && memcmp(cluster1
->list
, cluster2
->list
, cluster1
->length
)
155 static void cluster_free(struct cluster_list
*cluster
)
158 XFREE(MTYPE_CLUSTER_VAL
, cluster
->list
);
159 XFREE(MTYPE_CLUSTER
, cluster
);
162 static struct cluster_list
*cluster_dup(struct cluster_list
*cluster
)
164 struct cluster_list
*new;
166 new = XCALLOC(MTYPE_CLUSTER
, sizeof(struct cluster_list
));
167 new->length
= cluster
->length
;
169 if (cluster
->length
) {
170 new->list
= XMALLOC(MTYPE_CLUSTER_VAL
, cluster
->length
);
171 memcpy(new->list
, cluster
->list
, cluster
->length
);
178 static struct cluster_list
*cluster_intern(struct cluster_list
*cluster
)
180 struct cluster_list
*find
;
182 find
= hash_get(cluster_hash
, cluster
, cluster_hash_alloc
);
188 void cluster_unintern(struct cluster_list
*cluster
)
193 if (cluster
->refcnt
== 0) {
194 hash_release(cluster_hash
, cluster
);
195 cluster_free(cluster
);
199 static void cluster_init(void)
201 cluster_hash
= hash_create(cluster_hash_key_make
, cluster_hash_cmp
);
204 static void cluster_finish(void)
206 hash_clean(cluster_hash
, (void (*)(void *))cluster_free
);
207 hash_free(cluster_hash
);
211 static struct hash
*encap_hash
= NULL
;
213 static struct hash
*vnc_hash
= NULL
;
216 struct bgp_attr_encap_subtlv
*encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
218 struct bgp_attr_encap_subtlv
*new;
219 struct bgp_attr_encap_subtlv
*tail
;
220 struct bgp_attr_encap_subtlv
*p
;
222 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
223 int size
= sizeof(struct bgp_attr_encap_subtlv
) - 1 + p
->length
;
225 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
228 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
231 memcpy(tail
, p
, size
);
238 static void encap_free(struct bgp_attr_encap_subtlv
*p
)
240 struct bgp_attr_encap_subtlv
*next
;
244 XFREE(MTYPE_ENCAP_TLV
, p
);
249 void bgp_attr_flush_encap(struct attr
*attr
)
251 if (!attr
|| !attr
->extra
)
254 if (attr
->extra
->encap_subtlvs
) {
255 encap_free(attr
->extra
->encap_subtlvs
);
256 attr
->extra
->encap_subtlvs
= NULL
;
259 if (attr
->extra
->vnc_subtlvs
) {
260 encap_free(attr
->extra
->vnc_subtlvs
);
261 attr
->extra
->vnc_subtlvs
= NULL
;
267 * Compare encap sub-tlv chains
272 * This algorithm could be made faster if needed
274 static int encap_same(struct bgp_attr_encap_subtlv
*h1
,
275 struct bgp_attr_encap_subtlv
*h2
)
277 struct bgp_attr_encap_subtlv
*p
;
278 struct bgp_attr_encap_subtlv
*q
;
282 if (h1
== NULL
|| h2
== NULL
)
285 for (p
= h1
; p
; p
= p
->next
) {
286 for (q
= h2
; q
; q
= q
->next
) {
287 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
288 && !memcmp(p
->value
, q
->value
, p
->length
)) {
297 for (p
= h2
; p
; p
= p
->next
) {
298 for (q
= h1
; q
; q
= q
->next
) {
299 if ((p
->type
== q
->type
) && (p
->length
== q
->length
)
300 && !memcmp(p
->value
, q
->value
, p
->length
)) {
312 static void *encap_hash_alloc(void *p
)
314 /* Encap structure is already allocated. */
325 static struct bgp_attr_encap_subtlv
*
326 encap_intern(struct bgp_attr_encap_subtlv
*encap
, encap_subtlv_type type
)
328 struct bgp_attr_encap_subtlv
*find
;
329 struct hash
*hash
= encap_hash
;
331 if (type
== VNC_SUBTLV_TYPE
)
335 find
= hash_get(hash
, encap
, encap_hash_alloc
);
343 static void encap_unintern(struct bgp_attr_encap_subtlv
**encapp
,
344 encap_subtlv_type type
)
346 struct bgp_attr_encap_subtlv
*encap
= *encapp
;
350 if (encap
->refcnt
== 0) {
351 struct hash
*hash
= encap_hash
;
353 if (type
== VNC_SUBTLV_TYPE
)
356 hash_release(hash
, encap
);
362 static unsigned int encap_hash_key_make(void *p
)
364 const struct bgp_attr_encap_subtlv
*encap
= p
;
366 return jhash(encap
->value
, encap
->length
, 0);
369 static int encap_hash_cmp(const void *p1
, const void *p2
)
371 return encap_same((struct bgp_attr_encap_subtlv
*)p1
,
372 (struct bgp_attr_encap_subtlv
*)p2
);
375 static void encap_init(void)
377 encap_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
);
379 vnc_hash
= hash_create(encap_hash_key_make
, encap_hash_cmp
);
383 static void encap_finish(void)
385 hash_clean(encap_hash
, (void (*)(void *))encap_free
);
386 hash_free(encap_hash
);
389 hash_clean(vnc_hash
, (void (*)(void *))encap_free
);
395 static bool overlay_index_same(const struct attr_extra
*ae1
,
396 const struct attr_extra
*ae2
)
404 return !memcmp(&(ae1
->evpn_overlay
), &(ae2
->evpn_overlay
),
405 sizeof(struct overlay_index
));
408 /* Unknown transit attribute. */
409 static struct hash
*transit_hash
;
411 static void transit_free(struct transit
*transit
)
414 XFREE(MTYPE_TRANSIT_VAL
, transit
->val
);
415 XFREE(MTYPE_TRANSIT
, transit
);
418 static struct transit
*transit_dup(struct transit
*transit
)
422 new = XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
423 new->length
= transit
->length
;
425 new->val
= XMALLOC(MTYPE_TRANSIT_VAL
, transit
->length
);
426 memcpy(new->val
, transit
->val
, transit
->length
);
433 static void *transit_hash_alloc(void *p
)
435 /* Transit structure is already allocated. */
439 static struct transit
*transit_intern(struct transit
*transit
)
441 struct transit
*find
;
443 find
= hash_get(transit_hash
, transit
, transit_hash_alloc
);
445 transit_free(transit
);
451 void transit_unintern(struct transit
*transit
)
456 if (transit
->refcnt
== 0) {
457 hash_release(transit_hash
, transit
);
458 transit_free(transit
);
462 static unsigned int transit_hash_key_make(void *p
)
464 const struct transit
*transit
= p
;
466 return jhash(transit
->val
, transit
->length
, 0);
469 static int transit_hash_cmp(const void *p1
, const void *p2
)
471 const struct transit
*transit1
= p1
;
472 const struct transit
*transit2
= p2
;
474 return (transit1
->length
== transit2
->length
475 && memcmp(transit1
->val
, transit2
->val
, transit1
->length
) == 0);
478 static void transit_init(void)
480 transit_hash
= hash_create(transit_hash_key_make
, transit_hash_cmp
);
483 static void transit_finish(void)
485 hash_clean(transit_hash
, (void (*)(void *))transit_free
);
486 hash_free(transit_hash
);
490 /* Attribute hash routines. */
491 static struct hash
*attrhash
;
493 static struct attr_extra
*bgp_attr_extra_new(void)
495 return XCALLOC(MTYPE_ATTR_EXTRA
, sizeof(struct attr_extra
));
498 void bgp_attr_extra_free(struct attr
*attr
)
501 XFREE(MTYPE_ATTR_EXTRA
, attr
->extra
);
506 struct attr_extra
*bgp_attr_extra_get(struct attr
*attr
)
509 attr
->extra
= bgp_attr_extra_new();
513 /* Shallow copy of an attribute
514 * Though, not so shallow that it doesn't copy the contents
515 * of the attr_extra pointed to by 'extra'
517 void bgp_attr_dup(struct attr
*new, struct attr
*orig
)
519 struct attr_extra
*extra
= new->extra
;
522 /* if caller provided attr_extra space, use it in any case.
524 * This is neccesary even if orig->extra equals NULL, because otherwise
525 * memory may be later allocated on the heap by bgp_attr_extra_get.
527 * That memory would eventually be leaked, because the caller must not
528 * call bgp_attr_extra_free if he provided attr_extra on the stack.
532 memset(new->extra
, 0, sizeof(struct attr_extra
));
534 *new->extra
= *orig
->extra
;
536 } else if (orig
->extra
) {
537 new->extra
= bgp_attr_extra_new();
538 *new->extra
= *orig
->extra
;
542 void bgp_attr_deep_dup(struct attr
*new, struct attr
*orig
)
545 new->aspath
= aspath_dup(orig
->aspath
);
548 new->community
= community_dup(orig
->community
);
551 if (orig
->extra
->ecommunity
)
552 new->extra
->ecommunity
=
553 ecommunity_dup(orig
->extra
->ecommunity
);
554 if (orig
->extra
->lcommunity
)
555 new->extra
->lcommunity
=
556 lcommunity_dup(orig
->extra
->lcommunity
);
557 if (orig
->extra
->cluster
)
558 new->extra
->cluster
= cluster_dup(orig
->extra
->cluster
);
559 if (orig
->extra
->transit
)
560 new->extra
->transit
= transit_dup(orig
->extra
->transit
);
561 if (orig
->extra
->encap_subtlvs
)
562 new->extra
->encap_subtlvs
=
563 encap_tlv_dup(orig
->extra
->encap_subtlvs
);
565 if (orig
->extra
->vnc_subtlvs
)
566 new->extra
->vnc_subtlvs
=
567 encap_tlv_dup(orig
->extra
->vnc_subtlvs
);
572 void bgp_attr_deep_free(struct attr
*attr
)
575 aspath_free(attr
->aspath
);
578 community_free(attr
->community
);
581 if (attr
->extra
->ecommunity
)
582 ecommunity_free(&attr
->extra
->ecommunity
);
583 if (attr
->extra
->lcommunity
)
584 lcommunity_free(&attr
->extra
->lcommunity
);
585 if (attr
->extra
->cluster
)
586 cluster_free(attr
->extra
->cluster
);
587 if (attr
->extra
->transit
)
588 transit_free(attr
->extra
->transit
);
589 if (attr
->extra
->encap_subtlvs
)
590 encap_free(attr
->extra
->encap_subtlvs
);
592 if (attr
->extra
->vnc_subtlvs
)
593 encap_free(attr
->extra
->vnc_subtlvs
);
598 unsigned long int attr_count(void)
600 return attrhash
->count
;
603 unsigned long int attr_unknown_count(void)
605 return transit_hash
->count
;
608 unsigned int attrhash_key_make(void *p
)
610 const struct attr
*attr
= (struct attr
*)p
;
611 const struct attr_extra
*extra
= attr
->extra
;
613 #define MIX(val) key = jhash_1word(val, key)
616 MIX(attr
->nexthop
.s_addr
);
618 MIX(attr
->local_pref
);
621 key
+= attr
->nexthop
.s_addr
;
623 key
+= attr
->local_pref
;
626 MIX(extra
->aggregator_as
);
627 MIX(extra
->aggregator_addr
.s_addr
);
629 MIX(extra
->mp_nexthop_global_in
.s_addr
);
630 MIX(extra
->originator_id
.s_addr
);
635 MIX(aspath_key_make(attr
->aspath
));
637 MIX(community_hash_make(attr
->community
));
640 if (extra
->lcommunity
)
641 MIX(lcommunity_hash_make(extra
->lcommunity
));
642 if (extra
->ecommunity
)
643 MIX(ecommunity_hash_make(extra
->ecommunity
));
645 MIX(cluster_hash_key_make(extra
->cluster
));
647 MIX(transit_hash_key_make(extra
->transit
));
648 if (extra
->encap_subtlvs
)
649 MIX(encap_hash_key_make(extra
->encap_subtlvs
));
651 if (extra
->vnc_subtlvs
)
652 MIX(encap_hash_key_make(extra
->vnc_subtlvs
));
654 MIX(extra
->mp_nexthop_len
);
655 key
= jhash(extra
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
,
657 key
= jhash(extra
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
,
664 int attrhash_cmp(const void *p1
, const void *p2
)
666 const struct attr
*attr1
= p1
;
667 const struct attr
*attr2
= p2
;
669 if (attr1
->flag
== attr2
->flag
&& attr1
->origin
== attr2
->origin
670 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
671 && attr1
->aspath
== attr2
->aspath
672 && attr1
->community
== attr2
->community
&& attr1
->med
== attr2
->med
673 && attr1
->local_pref
== attr2
->local_pref
674 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
) {
675 const struct attr_extra
*ae1
= attr1
->extra
;
676 const struct attr_extra
*ae2
= attr2
->extra
;
678 if (ae1
&& ae2
&& ae1
->aggregator_as
== ae2
->aggregator_as
679 && ae1
->aggregator_addr
.s_addr
680 == ae2
->aggregator_addr
.s_addr
681 && ae1
->weight
== ae2
->weight
&& ae1
->tag
== ae2
->tag
682 && ae1
->mp_nexthop_len
== ae2
->mp_nexthop_len
683 && IPV6_ADDR_SAME(&ae1
->mp_nexthop_global
,
684 &ae2
->mp_nexthop_global
)
685 && IPV6_ADDR_SAME(&ae1
->mp_nexthop_local
,
686 &ae2
->mp_nexthop_local
)
687 && IPV4_ADDR_SAME(&ae1
->mp_nexthop_global_in
,
688 &ae2
->mp_nexthop_global_in
)
689 && ae1
->ecommunity
== ae2
->ecommunity
690 && ae1
->lcommunity
== ae2
->lcommunity
691 && ae1
->cluster
== ae2
->cluster
692 && ae1
->transit
== ae2
->transit
693 && (ae1
->encap_tunneltype
== ae2
->encap_tunneltype
)
694 && encap_same(ae1
->encap_subtlvs
, ae2
->encap_subtlvs
)
696 && encap_same(ae1
->vnc_subtlvs
, ae2
->vnc_subtlvs
)
698 && IPV4_ADDR_SAME(&ae1
->originator_id
, &ae2
->originator_id
)
699 && overlay_index_same(ae1
, ae2
))
703 /* neither attribute has extra attributes, so they're same */
709 static void attrhash_init(void)
711 attrhash
= hash_create(attrhash_key_make
, attrhash_cmp
);
715 * special for hash_clean below
717 static void attr_vfree(void *attr
)
719 bgp_attr_extra_free((struct attr
*)attr
);
720 XFREE(MTYPE_ATTR
, attr
);
723 static void attrhash_finish(void)
725 hash_clean(attrhash
, attr_vfree
);
730 static void attr_show_all_iterator(struct hash_backet
*backet
, struct vty
*vty
)
732 struct attr
*attr
= backet
->data
;
734 vty_out(vty
, "attr[%ld] nexthop %s%s", attr
->refcnt
,
735 inet_ntoa(attr
->nexthop
), VTY_NEWLINE
);
738 void attr_show_all(struct vty
*vty
)
740 hash_iterate(attrhash
, (void (*)(struct hash_backet
*,
741 void *))attr_show_all_iterator
,
745 static void *bgp_attr_hash_alloc(void *p
)
747 const struct attr
*val
= (const struct attr
*)p
;
750 attr
= XMALLOC(MTYPE_ATTR
, sizeof(struct attr
));
753 attr
->extra
= bgp_attr_extra_new();
754 *attr
->extra
= *val
->extra
;
755 if (val
->extra
->encap_subtlvs
) {
756 val
->extra
->encap_subtlvs
= NULL
;
759 if (val
->extra
->vnc_subtlvs
) {
760 val
->extra
->vnc_subtlvs
= NULL
;
768 /* Internet argument attribute. */
769 struct attr
*bgp_attr_intern(struct attr
*attr
)
773 /* Intern referenced strucutre. */
775 if (!attr
->aspath
->refcnt
)
776 attr
->aspath
= aspath_intern(attr
->aspath
);
778 attr
->aspath
->refcnt
++;
780 if (attr
->community
) {
781 if (!attr
->community
->refcnt
)
782 attr
->community
= community_intern(attr
->community
);
784 attr
->community
->refcnt
++;
787 struct attr_extra
*attre
= attr
->extra
;
789 if (attre
->ecommunity
) {
790 if (!attre
->ecommunity
->refcnt
)
792 ecommunity_intern(attre
->ecommunity
);
794 attre
->ecommunity
->refcnt
++;
796 if (attre
->lcommunity
) {
797 if (!attre
->lcommunity
->refcnt
)
799 lcommunity_intern(attre
->lcommunity
);
801 attre
->lcommunity
->refcnt
++;
803 if (attre
->cluster
) {
804 if (!attre
->cluster
->refcnt
)
805 attre
->cluster
= cluster_intern(attre
->cluster
);
807 attre
->cluster
->refcnt
++;
809 if (attre
->transit
) {
810 if (!attre
->transit
->refcnt
)
811 attre
->transit
= transit_intern(attre
->transit
);
813 attre
->transit
->refcnt
++;
815 if (attre
->encap_subtlvs
) {
816 if (!attre
->encap_subtlvs
->refcnt
)
817 attre
->encap_subtlvs
=
818 encap_intern(attre
->encap_subtlvs
,
821 attre
->encap_subtlvs
->refcnt
++;
824 if (attre
->vnc_subtlvs
) {
825 if (!attre
->vnc_subtlvs
->refcnt
)
826 attre
->vnc_subtlvs
= encap_intern(
827 attre
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
829 attre
->vnc_subtlvs
->refcnt
++;
834 find
= (struct attr
*)hash_get(attrhash
, attr
, bgp_attr_hash_alloc
);
840 /* Make network statement's attribute. */
841 struct attr
*bgp_attr_default_set(struct attr
*attr
, u_char origin
)
843 memset(attr
, 0, sizeof(struct attr
));
844 bgp_attr_extra_get(attr
);
846 attr
->origin
= origin
;
847 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
848 attr
->aspath
= aspath_empty();
849 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
850 attr
->extra
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
851 attr
->extra
->tag
= 0;
852 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
853 attr
->extra
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
859 /* Make network statement's attribute. */
860 struct attr
*bgp_attr_default_intern(u_char origin
)
865 memset(&attr
, 0, sizeof(struct attr
));
866 bgp_attr_extra_get(&attr
);
868 bgp_attr_default_set(&attr
, origin
);
870 new = bgp_attr_intern(&attr
);
871 bgp_attr_extra_free(&attr
);
873 aspath_unintern(&new->aspath
);
877 /* Create the attributes for an aggregate */
878 struct attr
*bgp_attr_aggregate_intern(struct bgp
*bgp
, u_char origin
,
879 struct aspath
*aspath
,
880 struct community
*community
, int as_set
,
881 u_char atomic_aggregate
)
885 struct attr_extra attre
;
887 memset(&attr
, 0, sizeof(struct attr
));
888 memset(&attre
, 0, sizeof(struct attr_extra
));
891 /* Origin attribute. */
892 attr
.origin
= origin
;
893 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
895 /* AS path attribute. */
897 attr
.aspath
= aspath_intern(aspath
);
899 attr
.aspath
= aspath_empty();
900 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
902 /* Next hop attribute. */
903 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
906 attr
.community
= community
;
907 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
910 attre
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
911 attre
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
912 if (!as_set
|| atomic_aggregate
)
913 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
914 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
915 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
916 attre
.aggregator_as
= bgp
->confed_id
;
918 attre
.aggregator_as
= bgp
->as
;
919 attre
.aggregator_addr
= bgp
->router_id
;
921 new = bgp_attr_intern(&attr
);
923 aspath_unintern(&new->aspath
);
927 /* Unintern just the sub-components of the attr, but not the attr */
928 void bgp_attr_unintern_sub(struct attr
*attr
)
930 /* aspath refcount shoud be decrement. */
932 aspath_unintern(&attr
->aspath
);
933 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
));
936 community_unintern(&attr
->community
);
937 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
));
940 if (attr
->extra
->ecommunity
)
941 ecommunity_unintern(&attr
->extra
->ecommunity
);
942 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
));
944 if (attr
->extra
->lcommunity
)
945 lcommunity_unintern(&attr
->extra
->lcommunity
);
946 UNSET_FLAG(attr
->flag
,
947 ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
));
949 if (attr
->extra
->cluster
)
950 cluster_unintern(attr
->extra
->cluster
);
951 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
));
953 if (attr
->extra
->transit
)
954 transit_unintern(attr
->extra
->transit
);
956 if (attr
->extra
->encap_subtlvs
)
957 encap_unintern(&attr
->extra
->encap_subtlvs
,
961 if (attr
->extra
->vnc_subtlvs
)
962 encap_unintern(&attr
->extra
->vnc_subtlvs
,
968 /* Free bgp attribute and aspath. */
969 void bgp_attr_unintern(struct attr
**pattr
)
971 struct attr
*attr
= *pattr
;
974 struct attr_extra tmp_extra
;
976 /* Decrement attribute reference. */
982 tmp
.extra
= &tmp_extra
;
983 memcpy(tmp
.extra
, attr
->extra
, sizeof(struct attr_extra
));
986 /* If reference becomes zero then free attribute object. */
987 if (attr
->refcnt
== 0) {
988 ret
= hash_release(attrhash
, attr
);
990 bgp_attr_extra_free(attr
);
991 XFREE(MTYPE_ATTR
, attr
);
995 bgp_attr_unintern_sub(&tmp
);
998 void bgp_attr_flush(struct attr
*attr
)
1000 if (attr
->aspath
&& !attr
->aspath
->refcnt
) {
1001 aspath_free(attr
->aspath
);
1002 attr
->aspath
= NULL
;
1004 if (attr
->community
&& !attr
->community
->refcnt
) {
1005 community_free(attr
->community
);
1006 attr
->community
= NULL
;
1009 struct attr_extra
*attre
= attr
->extra
;
1011 if (attre
->ecommunity
&& !attre
->ecommunity
->refcnt
)
1012 ecommunity_free(&attre
->ecommunity
);
1013 if (attre
->lcommunity
&& !attre
->lcommunity
->refcnt
)
1014 lcommunity_free(&attre
->lcommunity
);
1015 if (attre
->cluster
&& !attre
->cluster
->refcnt
) {
1016 cluster_free(attre
->cluster
);
1017 attre
->cluster
= NULL
;
1019 if (attre
->transit
&& !attre
->transit
->refcnt
) {
1020 transit_free(attre
->transit
);
1021 attre
->transit
= NULL
;
1023 if (attre
->encap_subtlvs
&& !attre
->encap_subtlvs
->refcnt
) {
1024 encap_free(attre
->encap_subtlvs
);
1025 attre
->encap_subtlvs
= NULL
;
1028 if (attre
->vnc_subtlvs
&& !attre
->vnc_subtlvs
->refcnt
) {
1029 encap_free(attre
->vnc_subtlvs
);
1030 attre
->vnc_subtlvs
= NULL
;
1036 /* Implement draft-scudder-idr-optional-transitive behaviour and
1037 * avoid resetting sessions for malformed attributes which are
1038 * are partial/optional and hence where the error likely was not
1039 * introduced by the sending neighbour.
1041 static bgp_attr_parse_ret_t
1042 bgp_attr_malformed(struct bgp_attr_parser_args
*args
, u_char subcode
,
1045 struct peer
*const peer
= args
->peer
;
1046 const u_int8_t flags
= args
->flags
;
1047 /* startp and length must be special-cased, as whether or not to
1048 * send the attribute data with the NOTIFY depends on the error,
1049 * the caller therefore signals this with the seperate length argument
1051 u_char
*notify_datap
= (length
> 0 ? args
->startp
: NULL
);
1053 /* Only relax error handling for eBGP peers */
1054 if (peer
->sort
!= BGP_PEER_EBGP
) {
1055 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1056 notify_datap
, length
);
1057 return BGP_ATTR_PARSE_ERROR
;
1060 /* Adjust the stream getp to the end of the attribute, in case we can
1061 * still proceed but the caller hasn't read all the attribute.
1063 stream_set_getp(BGP_INPUT(peer
),
1064 (args
->startp
- STREAM_DATA(BGP_INPUT(peer
)))
1067 switch (args
->type
) {
1068 /* where an attribute is relatively inconsequential, e.g. it does not
1069 * affect route selection, and can be safely ignored, then any such
1070 * attributes which are malformed should just be ignored and the route
1071 * processed as normal.
1073 case BGP_ATTR_AS4_AGGREGATOR
:
1074 case BGP_ATTR_AGGREGATOR
:
1075 case BGP_ATTR_ATOMIC_AGGREGATE
:
1076 return BGP_ATTR_PARSE_PROCEED
;
1078 /* Core attributes, particularly ones which may influence route
1079 * selection, should always cause session resets
1081 case BGP_ATTR_ORIGIN
:
1082 case BGP_ATTR_AS_PATH
:
1083 case BGP_ATTR_NEXT_HOP
:
1084 case BGP_ATTR_MULTI_EXIT_DISC
:
1085 case BGP_ATTR_LOCAL_PREF
:
1086 case BGP_ATTR_COMMUNITIES
:
1087 case BGP_ATTR_ORIGINATOR_ID
:
1088 case BGP_ATTR_CLUSTER_LIST
:
1089 case BGP_ATTR_MP_REACH_NLRI
:
1090 case BGP_ATTR_MP_UNREACH_NLRI
:
1091 case BGP_ATTR_EXT_COMMUNITIES
:
1092 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1093 notify_datap
, length
);
1094 return BGP_ATTR_PARSE_ERROR
;
1097 /* Partial optional attributes that are malformed should not cause
1098 * the whole session to be reset. Instead treat it as a withdrawal
1099 * of the routes, if possible.
1101 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)
1102 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1103 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
))
1104 return BGP_ATTR_PARSE_WITHDRAW
;
1106 /* default to reset */
1107 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1110 /* Find out what is wrong with the path attribute flag bits and log the error.
1111 "Flag bits" here stand for Optional, Transitive and Partial, but not for
1112 Extended Length. Checking O/T/P bits at once implies, that the attribute
1113 being diagnosed is defined by RFC as either a "well-known" or an "optional,
1114 non-transitive" attribute. */
1116 bgp_attr_flags_diagnose(struct bgp_attr_parser_args
*args
,
1117 u_int8_t desired_flags
/* how RFC says it must be */
1121 u_char real_flags
= args
->flags
;
1122 const u_int8_t attr_code
= args
->type
;
1124 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1125 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1126 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
1127 if (CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1128 != CHECK_FLAG(real_flags
, attr_flag_str
[i
].key
)) {
1129 zlog_err("%s attribute must%s be flagged as \"%s\"",
1130 lookup_msg(attr_str
, attr_code
, NULL
),
1131 CHECK_FLAG(desired_flags
, attr_flag_str
[i
].key
)
1134 attr_flag_str
[i
].str
);
1139 "Strange, %s called for attr %s, but no problem found with flags"
1140 " (real flags 0x%x, desired 0x%x)",
1141 __func__
, lookup_msg(attr_str
, attr_code
, NULL
),
1142 real_flags
, desired_flags
);
1146 /* Required flags for attributes. EXTLEN will be masked off when testing,
1147 * as will PARTIAL for optional+transitive attributes.
1149 const u_int8_t attr_flags_values
[] = {
1150 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1151 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1152 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1153 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1154 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1155 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1156 [BGP_ATTR_AGGREGATOR
] =
1157 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1158 [BGP_ATTR_COMMUNITIES
] =
1159 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1160 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1161 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1162 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1163 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1164 [BGP_ATTR_EXT_COMMUNITIES
] =
1165 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1166 [BGP_ATTR_AS4_PATH
] =
1167 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1168 [BGP_ATTR_AS4_AGGREGATOR
] =
1169 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1170 [BGP_ATTR_LARGE_COMMUNITIES
] =
1171 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1173 static const size_t attr_flags_values_max
= array_size(attr_flags_values
) - 1;
1175 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args
*args
)
1177 u_int8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1178 const u_int8_t flags
= args
->flags
;
1179 const u_int8_t attr_code
= args
->type
;
1181 /* there may be attributes we don't know about */
1182 if (attr_code
> attr_flags_values_max
)
1184 if (attr_flags_values
[attr_code
] == 0)
1187 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1191 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL
, flags
)
1192 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS
, flags
)) {
1194 "%s well-known attributes must have transitive flag set (%x)",
1195 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1199 /* "For well-known attributes and for optional non-transitive
1201 * the Partial bit MUST be set to 0."
1203 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_PARTIAL
)) {
1204 if (!CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)) {
1206 "%s well-known attribute "
1207 "must NOT have the partial flag set (%x)",
1208 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1211 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1212 && !CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
)) {
1214 "%s optional + transitive attribute "
1215 "must NOT have the partial flag set (%x)",
1216 lookup_msg(attr_str
, attr_code
, NULL
), flags
);
1221 /* Optional transitive attributes may go through speakers that don't
1222 * reocgnise them and set the Partial bit.
1224 if (CHECK_FLAG(flags
, BGP_ATTR_FLAG_OPTIONAL
)
1225 && CHECK_FLAG(flags
, BGP_ATTR_FLAG_TRANS
))
1226 SET_FLAG(mask
, BGP_ATTR_FLAG_PARTIAL
);
1228 if ((flags
& ~mask
) == attr_flags_values
[attr_code
])
1231 bgp_attr_flags_diagnose(args
, attr_flags_values
[attr_code
]);
1235 /* Get origin attribute of the update message. */
1236 static bgp_attr_parse_ret_t
bgp_attr_origin(struct bgp_attr_parser_args
*args
)
1238 struct peer
*const peer
= args
->peer
;
1239 struct attr
*const attr
= args
->attr
;
1240 const bgp_size_t length
= args
->length
;
1242 /* If any recognized attribute has Attribute Length that conflicts
1243 with the expected length (based on the attribute type code), then
1244 the Error Subcode is set to Attribute Length Error. The Data
1245 field contains the erroneous attribute (type, length and
1248 zlog_err("Origin attribute length is not one %d", length
);
1249 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1253 /* Fetch origin attribute. */
1254 attr
->origin
= stream_getc(BGP_INPUT(peer
));
1256 /* If the ORIGIN attribute has an undefined value, then the Error
1257 Subcode is set to Invalid Origin Attribute. The Data field
1258 contains the unrecognized attribute (type, length and value). */
1259 if ((attr
->origin
!= BGP_ORIGIN_IGP
) && (attr
->origin
!= BGP_ORIGIN_EGP
)
1260 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
)) {
1261 zlog_err("Origin attribute value is invalid %d", attr
->origin
);
1262 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1266 /* Set oring attribute flag. */
1267 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
);
1272 /* Parse AS path information. This function is wrapper of
1274 static int bgp_attr_aspath(struct bgp_attr_parser_args
*args
)
1276 struct attr
*const attr
= args
->attr
;
1277 struct peer
*const peer
= args
->peer
;
1278 const bgp_size_t length
= args
->length
;
1281 * peer with AS4 => will get 4Byte ASnums
1282 * otherwise, will get 16 Bit
1284 attr
->aspath
= aspath_parse(peer
->ibuf
, length
,
1285 CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
));
1287 /* In case of IBGP, length will be zero. */
1288 if (!attr
->aspath
) {
1289 zlog_err("Malformed AS path from %s, length is %d", peer
->host
,
1291 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1295 /* Set aspath attribute flag. */
1296 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
);
1298 return BGP_ATTR_PARSE_PROCEED
;
1301 static bgp_attr_parse_ret_t
bgp_attr_aspath_check(struct peer
*const peer
,
1302 struct attr
*const attr
)
1304 /* These checks were part of bgp_attr_aspath, but with
1305 * as4 we should to check aspath things when
1306 * aspath synthesizing with as4_path has already taken place.
1307 * Otherwise we check ASPATH and use the synthesized thing, and that is
1309 * So do the checks later, i.e. here
1311 struct bgp
*bgp
= peer
->bgp
;
1312 struct aspath
*aspath
;
1314 /* Confederation sanity check. */
1315 if ((peer
->sort
== BGP_PEER_CONFED
1316 && !aspath_left_confed_check(attr
->aspath
))
1317 || (peer
->sort
== BGP_PEER_EBGP
1318 && aspath_confed_check(attr
->aspath
))) {
1319 zlog_err("Malformed AS path from %s", peer
->host
);
1320 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1321 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1322 return BGP_ATTR_PARSE_ERROR
;
1325 /* First AS check for EBGP. */
1326 if (bgp
!= NULL
&& bgp_flag_check(bgp
, BGP_FLAG_ENFORCE_FIRST_AS
)) {
1327 if (peer
->sort
== BGP_PEER_EBGP
1328 && !aspath_firstas_check(attr
->aspath
, peer
->as
)) {
1329 zlog_err("%s incorrect first AS (must be %u)",
1330 peer
->host
, peer
->as
);
1331 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1332 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1333 return BGP_ATTR_PARSE_ERROR
;
1337 /* local-as prepend */
1338 if (peer
->change_local_as
1339 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)) {
1340 aspath
= aspath_dup(attr
->aspath
);
1341 aspath
= aspath_add_seq(aspath
, peer
->change_local_as
);
1342 aspath_unintern(&attr
->aspath
);
1343 attr
->aspath
= aspath_intern(aspath
);
1346 return BGP_ATTR_PARSE_PROCEED
;
1349 /* Parse AS4 path information. This function is another wrapper of
1351 static int bgp_attr_as4_path(struct bgp_attr_parser_args
*args
,
1352 struct aspath
**as4_path
)
1354 struct peer
*const peer
= args
->peer
;
1355 struct attr
*const attr
= args
->attr
;
1356 const bgp_size_t length
= args
->length
;
1358 *as4_path
= aspath_parse(peer
->ibuf
, length
, 1);
1360 /* In case of IBGP, length will be zero. */
1362 zlog_err("Malformed AS4 path from %s, length is %d", peer
->host
,
1364 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1368 /* Set aspath attribute flag. */
1370 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
);
1372 return BGP_ATTR_PARSE_PROCEED
;
1375 /* Nexthop attribute. */
1376 static bgp_attr_parse_ret_t
bgp_attr_nexthop(struct bgp_attr_parser_args
*args
)
1378 struct peer
*const peer
= args
->peer
;
1379 struct attr
*const attr
= args
->attr
;
1380 const bgp_size_t length
= args
->length
;
1382 in_addr_t nexthop_h
, nexthop_n
;
1384 /* Check nexthop attribute length. */
1386 zlog_err("Nexthop attribute length isn't four [%d]", length
);
1388 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1392 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1393 attribute must result in a NOTIFICATION message (this is implemented
1395 At the same time, semantically incorrect NEXT_HOP is more likely to
1397 logged locally (this is implemented somewhere else). The UPDATE
1399 gets ignored in any of these cases. */
1400 nexthop_n
= stream_get_ipv4(peer
->ibuf
);
1401 nexthop_h
= ntohl(nexthop_n
);
1402 if ((IPV4_NET0(nexthop_h
) || IPV4_NET127(nexthop_h
)
1403 || IPV4_CLASS_DE(nexthop_h
))
1406 ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1408 char buf
[INET_ADDRSTRLEN
];
1409 inet_ntop(AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1410 zlog_err("Martian nexthop %s", buf
);
1411 return bgp_attr_malformed(
1412 args
, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
, args
->total
);
1415 attr
->nexthop
.s_addr
= nexthop_n
;
1416 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1418 return BGP_ATTR_PARSE_PROCEED
;
1421 /* MED atrribute. */
1422 static bgp_attr_parse_ret_t
bgp_attr_med(struct bgp_attr_parser_args
*args
)
1424 struct peer
*const peer
= args
->peer
;
1425 struct attr
*const attr
= args
->attr
;
1426 const bgp_size_t length
= args
->length
;
1430 zlog_err("MED attribute length isn't four [%d]", length
);
1432 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1436 attr
->med
= stream_getl(peer
->ibuf
);
1438 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
1440 return BGP_ATTR_PARSE_PROCEED
;
1443 /* Local preference attribute. */
1444 static bgp_attr_parse_ret_t
1445 bgp_attr_local_pref(struct bgp_attr_parser_args
*args
)
1447 struct peer
*const peer
= args
->peer
;
1448 struct attr
*const attr
= args
->attr
;
1449 const bgp_size_t length
= args
->length
;
1453 zlog_err("LOCAL_PREF attribute length isn't 4 [%u]", length
);
1454 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1458 /* If it is contained in an UPDATE message that is received from an
1459 external peer, then this attribute MUST be ignored by the
1460 receiving speaker. */
1461 if (peer
->sort
== BGP_PEER_EBGP
) {
1462 stream_forward_getp(peer
->ibuf
, length
);
1463 return BGP_ATTR_PARSE_PROCEED
;
1466 attr
->local_pref
= stream_getl(peer
->ibuf
);
1468 /* Set atomic aggregate flag. */
1469 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1471 return BGP_ATTR_PARSE_PROCEED
;
1474 /* Atomic aggregate. */
1475 static int bgp_attr_atomic(struct bgp_attr_parser_args
*args
)
1477 struct attr
*const attr
= args
->attr
;
1478 const bgp_size_t length
= args
->length
;
1482 zlog_err("ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1484 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1488 /* Set atomic aggregate flag. */
1489 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
1491 return BGP_ATTR_PARSE_PROCEED
;
1494 /* Aggregator attribute */
1495 static int bgp_attr_aggregator(struct bgp_attr_parser_args
*args
)
1497 struct peer
*const peer
= args
->peer
;
1498 struct attr
*const attr
= args
->attr
;
1499 const bgp_size_t length
= args
->length
;
1502 struct attr_extra
*attre
= bgp_attr_extra_get(attr
);
1504 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1505 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1508 if (length
!= wantedlen
) {
1509 zlog_err("AGGREGATOR attribute length isn't %u [%u]", wantedlen
,
1511 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1515 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
))
1516 attre
->aggregator_as
= stream_getl(peer
->ibuf
);
1518 attre
->aggregator_as
= stream_getw(peer
->ibuf
);
1519 attre
->aggregator_addr
.s_addr
= stream_get_ipv4(peer
->ibuf
);
1521 /* Set atomic aggregate flag. */
1522 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
);
1524 return BGP_ATTR_PARSE_PROCEED
;
1527 /* New Aggregator attribute */
1528 static bgp_attr_parse_ret_t
1529 bgp_attr_as4_aggregator(struct bgp_attr_parser_args
*args
,
1530 as_t
*as4_aggregator_as
,
1531 struct in_addr
*as4_aggregator_addr
)
1533 struct peer
*const peer
= args
->peer
;
1534 struct attr
*const attr
= args
->attr
;
1535 const bgp_size_t length
= args
->length
;
1538 zlog_err("New Aggregator length is not 8 [%d]", length
);
1539 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1543 *as4_aggregator_as
= stream_getl(peer
->ibuf
);
1544 as4_aggregator_addr
->s_addr
= stream_get_ipv4(peer
->ibuf
);
1546 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
);
1548 return BGP_ATTR_PARSE_PROCEED
;
1551 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1553 static bgp_attr_parse_ret_t
1554 bgp_attr_munge_as4_attrs(struct peer
*const peer
, struct attr
*const attr
,
1555 struct aspath
*as4_path
, as_t as4_aggregator
,
1556 struct in_addr
*as4_aggregator_addr
)
1558 int ignore_as4_path
= 0;
1559 struct aspath
*newpath
;
1560 struct attr_extra
*attre
= attr
->extra
;
1562 if (!attr
->aspath
) {
1563 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1565 * checked that all well-known, mandatory attributes were
1568 * Can only be a problem with peer itself - hard error
1570 return BGP_ATTR_PARSE_ERROR
;
1573 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) {
1574 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1576 * It is worth a warning though, because the peer really
1577 * should not send them
1579 if (BGP_DEBUG(as4
, AS4
)) {
1580 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1581 zlog_debug("[AS4] %s %s AS4_PATH", peer
->host
,
1582 "AS4 capable peer, yet it sent");
1585 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1586 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1588 "AS4 capable peer, yet it sent");
1591 return BGP_ATTR_PARSE_PROCEED
;
1594 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1595 * because that may override AS4_PATH
1597 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
))) {
1598 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
1602 * if the as_number in aggregator is not AS_TRANS,
1603 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1604 * and the Aggregator shall be taken as
1605 * info on the aggregating node, and the AS_PATH
1606 * shall be taken as the AS_PATH
1608 * the Aggregator shall be ignored and the
1609 * AS4_AGGREGATOR shall be taken as the
1610 * Aggregating node and the AS_PATH is to be
1611 * constructed "as in all other cases"
1613 if (attre
->aggregator_as
!= BGP_AS_TRANS
) {
1615 if (BGP_DEBUG(as4
, AS4
))
1617 "[AS4] %s BGP not AS4 capable peer"
1618 " send AGGREGATOR != AS_TRANS and"
1619 " AS4_AGGREGATOR, so ignore"
1620 " AS4_AGGREGATOR and AS4_PATH",
1622 ignore_as4_path
= 1;
1624 /* "New_aggregator shall be taken as aggregator"
1626 attre
->aggregator_as
= as4_aggregator
;
1627 attre
->aggregator_addr
.s_addr
=
1628 as4_aggregator_addr
->s_addr
;
1631 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1632 * That is bogus - but reading the conditions
1633 * we have to handle AS4_AGGREGATOR as if it were
1634 * AGGREGATOR in that case
1636 if (BGP_DEBUG(as4
, AS4
))
1638 "[AS4] %s BGP not AS4 capable peer send"
1639 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1640 " it as if AGGREGATOR with AS_TRANS had been there",
1642 (attre
= bgp_attr_extra_get(attr
))->aggregator_as
=
1644 /* sweep it under the carpet and simulate a "good"
1646 attr
->flag
|= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
));
1650 /* need to reconcile NEW_AS_PATH and AS_PATH */
1651 if (!ignore_as4_path
1652 && (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))) {
1653 newpath
= aspath_reconcile_as4(attr
->aspath
, as4_path
);
1654 aspath_unintern(&attr
->aspath
);
1655 attr
->aspath
= aspath_intern(newpath
);
1657 return BGP_ATTR_PARSE_PROCEED
;
1660 /* Community attribute. */
1661 static bgp_attr_parse_ret_t
1662 bgp_attr_community(struct bgp_attr_parser_args
*args
)
1664 struct peer
*const peer
= args
->peer
;
1665 struct attr
*const attr
= args
->attr
;
1666 const bgp_size_t length
= args
->length
;
1669 attr
->community
= NULL
;
1670 return BGP_ATTR_PARSE_PROCEED
;
1674 community_parse((u_int32_t
*)stream_pnt(peer
->ibuf
), length
);
1676 /* XXX: fix community_parse to use stream API and remove this */
1677 stream_forward_getp(peer
->ibuf
, length
);
1679 if (!attr
->community
)
1680 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1683 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1685 return BGP_ATTR_PARSE_PROCEED
;
1688 /* Originator ID attribute. */
1689 static bgp_attr_parse_ret_t
1690 bgp_attr_originator_id(struct bgp_attr_parser_args
*args
)
1692 struct peer
*const peer
= args
->peer
;
1693 struct attr
*const attr
= args
->attr
;
1694 const bgp_size_t length
= args
->length
;
1698 zlog_err("Bad originator ID length %d", length
);
1700 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1704 (bgp_attr_extra_get(attr
))->originator_id
.s_addr
=
1705 stream_get_ipv4(peer
->ibuf
);
1707 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
);
1709 return BGP_ATTR_PARSE_PROCEED
;
1712 /* Cluster list attribute. */
1713 static bgp_attr_parse_ret_t
1714 bgp_attr_cluster_list(struct bgp_attr_parser_args
*args
)
1716 struct peer
*const peer
= args
->peer
;
1717 struct attr
*const attr
= args
->attr
;
1718 const bgp_size_t length
= args
->length
;
1722 zlog_err("Bad cluster list length %d", length
);
1724 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1728 (bgp_attr_extra_get(attr
))->cluster
=
1729 cluster_parse((struct in_addr
*)stream_pnt(peer
->ibuf
), length
);
1731 /* XXX: Fix cluster_parse to use stream API and then remove this */
1732 stream_forward_getp(peer
->ibuf
, length
);
1734 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
);
1736 return BGP_ATTR_PARSE_PROCEED
;
1739 /* Multiprotocol reachability information parse. */
1740 int bgp_mp_reach_parse(struct bgp_attr_parser_args
*args
,
1741 struct bgp_nlri
*mp_update
)
1745 safi_t pkt_safi
, safi
;
1746 bgp_size_t nlri_len
;
1749 struct peer
*const peer
= args
->peer
;
1750 struct attr
*const attr
= args
->attr
;
1751 const bgp_size_t length
= args
->length
;
1752 struct attr_extra
*attre
= bgp_attr_extra_get(attr
);
1754 /* Set end of packet. */
1755 s
= BGP_INPUT(peer
);
1756 start
= stream_get_getp(s
);
1758 /* safe to read statically sized header? */
1759 #define BGP_MP_REACH_MIN_SIZE 5
1760 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1761 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
)) {
1762 zlog_info("%s: %s sent invalid length, %lu", __func__
,
1763 peer
->host
, (unsigned long)length
);
1764 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1767 /* Load AFI, SAFI. */
1768 pkt_afi
= stream_getw(s
);
1769 pkt_safi
= stream_getc(s
);
1771 /* Convert AFI, SAFI to internal values, check. */
1772 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1773 /* Log if AFI or SAFI is unrecognized. This is not an error
1775 * the attribute is otherwise malformed.
1777 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1779 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1780 peer
->host
, pkt_afi
, pkt_safi
);
1781 return BGP_ATTR_PARSE_ERROR
;
1784 /* Get nexthop length. */
1785 attre
->mp_nexthop_len
= stream_getc(s
);
1787 if (LEN_LEFT
< attre
->mp_nexthop_len
) {
1789 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1790 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1791 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1794 /* Nexthop length check. */
1795 switch (attre
->mp_nexthop_len
) {
1796 case BGP_ATTR_NHLEN_IPV4
:
1797 stream_get(&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1798 /* Probably needed for RFC 2283 */
1799 if (attr
->nexthop
.s_addr
== 0)
1800 memcpy(&attr
->nexthop
.s_addr
,
1801 &attre
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1803 case BGP_ATTR_NHLEN_VPNV4
:
1804 stream_getl(s
); /* RD high */
1805 stream_getl(s
); /* RD low */
1806 stream_get(&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1808 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1809 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1810 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1811 stream_getl(s
); /* RD high */
1812 stream_getl(s
); /* RD low */
1814 stream_get(&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1816 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1817 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1818 if (attre
->mp_nexthop_len
1819 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1820 stream_getl(s
); /* RD high */
1821 stream_getl(s
); /* RD low */
1823 stream_get(&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1824 if (attre
->mp_nexthop_len
1825 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1826 stream_getl(s
); /* RD high */
1827 stream_getl(s
); /* RD low */
1829 stream_get(&attre
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1830 if (!IN6_IS_ADDR_LINKLOCAL(&attre
->mp_nexthop_local
)) {
1831 char buf1
[INET6_ADDRSTRLEN
];
1832 char buf2
[INET6_ADDRSTRLEN
];
1834 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1836 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1839 &attre
->mp_nexthop_global
,
1840 buf1
, INET6_ADDRSTRLEN
),
1842 &attre
->mp_nexthop_local
,
1843 buf2
, INET6_ADDRSTRLEN
));
1845 attre
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1849 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1850 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1851 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1855 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__
,
1857 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1862 if ((val
= stream_getc(s
)))
1864 "%s sent non-zero value, %u, for defunct SNPA-length field",
1868 /* must have nrli_len, what is left of the attribute */
1869 nlri_len
= LEN_LEFT
;
1870 if ((!nlri_len
) || (nlri_len
> STREAM_READABLE(s
))) {
1871 zlog_info("%s: (%s) Failed to read NLRI", __func__
, peer
->host
);
1872 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1875 mp_update
->afi
= afi
;
1876 mp_update
->safi
= safi
;
1877 mp_update
->nlri
= stream_pnt(s
);
1878 mp_update
->length
= nlri_len
;
1880 stream_forward_getp(s
, nlri_len
);
1882 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
);
1884 return BGP_ATTR_PARSE_PROCEED
;
1888 /* Multiprotocol unreachable parse */
1889 int bgp_mp_unreach_parse(struct bgp_attr_parser_args
*args
,
1890 struct bgp_nlri
*mp_withdraw
)
1895 safi_t pkt_safi
, safi
;
1896 u_int16_t withdraw_len
;
1897 struct peer
*const peer
= args
->peer
;
1898 struct attr
*const attr
= args
->attr
;
1899 const bgp_size_t length
= args
->length
;
1903 #define BGP_MP_UNREACH_MIN_SIZE 3
1904 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1905 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1907 pkt_afi
= stream_getw(s
);
1908 pkt_safi
= stream_getc(s
);
1910 /* Convert AFI, SAFI to internal values, check. */
1911 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1912 /* Log if AFI or SAFI is unrecognized. This is not an error
1914 * the attribute is otherwise malformed.
1916 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1918 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1919 peer
->host
, pkt_afi
, pkt_safi
);
1920 return BGP_ATTR_PARSE_ERROR
;
1923 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1925 mp_withdraw
->afi
= afi
;
1926 mp_withdraw
->safi
= safi
;
1927 mp_withdraw
->nlri
= stream_pnt(s
);
1928 mp_withdraw
->length
= withdraw_len
;
1930 stream_forward_getp(s
, withdraw_len
);
1932 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
);
1934 return BGP_ATTR_PARSE_PROCEED
;
1937 /* Large Community attribute. */
1938 static bgp_attr_parse_ret_t
1939 bgp_attr_large_community(struct bgp_attr_parser_args
*args
)
1941 struct peer
*const peer
= args
->peer
;
1942 struct attr
*const attr
= args
->attr
;
1943 const bgp_size_t length
= args
->length
;
1946 * Large community follows new attribute format.
1950 attr
->extra
->lcommunity
= NULL
;
1951 /* Empty extcomm doesn't seem to be invalid per se */
1952 return BGP_ATTR_PARSE_PROCEED
;
1955 (bgp_attr_extra_get(attr
))->lcommunity
=
1956 lcommunity_parse((u_int8_t
*)stream_pnt(peer
->ibuf
), length
);
1957 /* XXX: fix ecommunity_parse to use stream API */
1958 stream_forward_getp(peer
->ibuf
, length
);
1960 if (attr
->extra
&& !attr
->extra
->lcommunity
)
1961 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1964 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
);
1966 return BGP_ATTR_PARSE_PROCEED
;
1969 /* Extended Community attribute. */
1970 static bgp_attr_parse_ret_t
1971 bgp_attr_ext_communities(struct bgp_attr_parser_args
*args
)
1973 struct peer
*const peer
= args
->peer
;
1974 struct attr
*const attr
= args
->attr
;
1975 const bgp_size_t length
= args
->length
;
1979 attr
->extra
->ecommunity
= NULL
;
1980 /* Empty extcomm doesn't seem to be invalid per se */
1981 return BGP_ATTR_PARSE_PROCEED
;
1984 (bgp_attr_extra_get(attr
))->ecommunity
=
1985 ecommunity_parse((u_int8_t
*)stream_pnt(peer
->ibuf
), length
);
1986 /* XXX: fix ecommunity_parse to use stream API */
1987 stream_forward_getp(peer
->ibuf
, length
);
1989 if (attr
->extra
&& !attr
->extra
->ecommunity
)
1990 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1993 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
1995 return BGP_ATTR_PARSE_PROCEED
;
1998 /* Parse Tunnel Encap attribute in an UPDATE */
1999 static int bgp_attr_encap(uint8_t type
, struct peer
*peer
, /* IN */
2000 bgp_size_t length
, /* IN: attr's length field */
2001 struct attr
*attr
, /* IN: caller already allocated */
2002 u_char flag
, /* IN: attr's flags field */
2006 struct attr_extra
*attre
= NULL
;
2007 struct bgp_attr_encap_subtlv
*stlv_last
= NULL
;
2008 uint16_t tunneltype
= 0;
2010 total
= length
+ (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
2012 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
2013 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2015 "Tunnel Encap attribute flag isn't optional and transitive %d",
2017 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2018 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2023 if (BGP_ATTR_ENCAP
== type
) {
2024 /* read outer TLV type and length */
2025 uint16_t tlv_length
;
2029 "Tunnel Encap attribute not long enough to contain outer T,L");
2030 bgp_notify_send_with_data(
2031 peer
, BGP_NOTIFY_UPDATE_ERR
,
2032 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
2035 tunneltype
= stream_getw(BGP_INPUT(peer
));
2036 tlv_length
= stream_getw(BGP_INPUT(peer
));
2039 if (tlv_length
!= length
) {
2040 zlog_info("%s: tlv_length(%d) != length(%d)", __func__
,
2041 tlv_length
, length
);
2045 while (length
>= 4) {
2046 uint16_t subtype
= 0;
2047 uint16_t sublength
= 0;
2048 struct bgp_attr_encap_subtlv
*tlv
;
2050 if (BGP_ATTR_ENCAP
== type
) {
2051 subtype
= stream_getc(BGP_INPUT(peer
));
2052 sublength
= stream_getc(BGP_INPUT(peer
));
2056 subtype
= stream_getw(BGP_INPUT(peer
));
2057 sublength
= stream_getw(BGP_INPUT(peer
));
2062 if (sublength
> length
) {
2064 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
2066 bgp_notify_send_with_data(
2067 peer
, BGP_NOTIFY_UPDATE_ERR
,
2068 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
, startp
, total
);
2072 /* alloc and copy sub-tlv */
2073 /* TBD make sure these are freed when attributes are released */
2074 tlv
= XCALLOC(MTYPE_ENCAP_TLV
,
2075 sizeof(struct bgp_attr_encap_subtlv
) - 1
2077 tlv
->type
= subtype
;
2078 tlv
->length
= sublength
;
2079 stream_get(tlv
->value
, peer
->ibuf
, sublength
);
2080 length
-= sublength
;
2082 /* attach tlv to encap chain */
2084 attre
= bgp_attr_extra_get(attr
);
2085 if (BGP_ATTR_ENCAP
== type
) {
2086 for (stlv_last
= attre
->encap_subtlvs
;
2087 stlv_last
&& stlv_last
->next
;
2088 stlv_last
= stlv_last
->next
)
2091 stlv_last
->next
= tlv
;
2093 attre
->encap_subtlvs
= tlv
;
2097 for (stlv_last
= attre
->vnc_subtlvs
;
2098 stlv_last
&& stlv_last
->next
;
2099 stlv_last
= stlv_last
->next
)
2102 stlv_last
->next
= tlv
;
2104 attre
->vnc_subtlvs
= tlv
;
2109 stlv_last
->next
= tlv
;
2114 if (BGP_ATTR_ENCAP
== type
) {
2116 attre
= bgp_attr_extra_get(attr
);
2117 attre
->encap_tunneltype
= tunneltype
;
2121 /* spurious leftover data */
2123 "Tunnel Encap attribute length is bad: %d leftover octets",
2125 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2126 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2134 /* BGP unknown attribute treatment. */
2135 static bgp_attr_parse_ret_t
bgp_attr_unknown(struct bgp_attr_parser_args
*args
)
2137 bgp_size_t total
= args
->total
;
2138 struct transit
*transit
;
2139 struct attr_extra
*attre
;
2140 struct peer
*const peer
= args
->peer
;
2141 struct attr
*const attr
= args
->attr
;
2142 u_char
*const startp
= args
->startp
;
2143 const u_char type
= args
->type
;
2144 const u_char flag
= args
->flags
;
2145 const bgp_size_t length
= args
->length
;
2147 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2149 "%s Unknown attribute is received (type %d, length %d)",
2150 peer
->host
, type
, length
);
2152 /* Forward read pointer of input stream. */
2153 stream_forward_getp(peer
->ibuf
, length
);
2155 /* If any of the mandatory well-known attributes are not recognized,
2156 then the Error Subcode is set to Unrecognized Well-known
2157 Attribute. The Data field contains the unrecognized attribute
2158 (type, length and value). */
2159 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
)) {
2160 return bgp_attr_malformed(args
, BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2164 /* Unrecognized non-transitive optional attributes must be quietly
2165 ignored and not passed along to other BGP peers. */
2166 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
))
2167 return BGP_ATTR_PARSE_PROCEED
;
2169 /* If a path with recognized transitive optional attribute is
2170 accepted and passed along to other BGP peers and the Partial bit
2171 in the Attribute Flags octet is set to 1 by some previous AS, it
2172 is not set back to 0 by the current AS. */
2173 SET_FLAG(*startp
, BGP_ATTR_FLAG_PARTIAL
);
2175 /* Store transitive attribute to the end of attr->transit. */
2176 if (!((attre
= bgp_attr_extra_get(attr
))->transit
))
2177 attre
->transit
= XCALLOC(MTYPE_TRANSIT
, sizeof(struct transit
));
2179 transit
= attre
->transit
;
2182 transit
->val
= XREALLOC(MTYPE_TRANSIT_VAL
, transit
->val
,
2183 transit
->length
+ total
);
2185 transit
->val
= XMALLOC(MTYPE_TRANSIT_VAL
, total
);
2187 memcpy(transit
->val
+ transit
->length
, startp
, total
);
2188 transit
->length
+= total
;
2190 return BGP_ATTR_PARSE_PROCEED
;
2193 /* Well-known attribute check. */
2194 static int bgp_attr_check(struct peer
*peer
, struct attr
*attr
)
2198 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2200 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2201 return BGP_ATTR_PARSE_PROCEED
;
2203 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2204 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2205 are present, it should. Check for any other attribute being present
2208 if (attr
->flag
== ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
))
2209 return BGP_ATTR_PARSE_PROCEED
;
2211 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN
)))
2212 type
= BGP_ATTR_ORIGIN
;
2214 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2215 type
= BGP_ATTR_AS_PATH
;
2217 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2219 * NLRI is empty. We can't easily check NLRI empty here though.
2221 if (!CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
))
2222 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
)))
2223 type
= BGP_ATTR_NEXT_HOP
;
2225 if (peer
->sort
== BGP_PEER_IBGP
2226 && !CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))
2227 type
= BGP_ATTR_LOCAL_PREF
;
2230 zlog_warn("%s Missing well-known attribute %s.", peer
->host
,
2231 lookup_msg(attr_str
, type
, NULL
));
2232 bgp_notify_send_with_data(peer
, BGP_NOTIFY_UPDATE_ERR
,
2233 BGP_NOTIFY_UPDATE_MISS_ATTR
, &type
,
2235 return BGP_ATTR_PARSE_ERROR
;
2237 return BGP_ATTR_PARSE_PROCEED
;
2240 /* Read attribute of update packet. This function is called from
2241 bgp_update_receive() in bgp_packet.c. */
2242 bgp_attr_parse_ret_t
bgp_attr_parse(struct peer
*peer
, struct attr
*attr
,
2243 bgp_size_t size
, struct bgp_nlri
*mp_update
,
2244 struct bgp_nlri
*mp_withdraw
)
2250 u_char
*startp
, *endp
;
2252 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2253 /* we need the as4_path only until we have synthesized the as_path with
2255 /* same goes for as4_aggregator */
2256 struct aspath
*as4_path
= NULL
;
2257 as_t as4_aggregator
= 0;
2258 struct in_addr as4_aggregator_addr
= {.s_addr
= 0};
2260 /* Initialize bitmap. */
2261 memset(seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2263 /* End pointer of BGP attribute. */
2264 endp
= BGP_INPUT_PNT(peer
) + size
;
2266 /* Get attributes to the end of attribute length. */
2267 while (BGP_INPUT_PNT(peer
) < endp
) {
2268 /* Check remaining length check.*/
2269 if (endp
- BGP_INPUT_PNT(peer
) < BGP_ATTR_MIN_LEN
) {
2270 /* XXX warning: long int format, int arg (arg 5) */
2272 "%s: error BGP attribute length %lu is smaller than min len",
2274 (unsigned long)(endp
2275 - STREAM_PNT(BGP_INPUT(peer
))));
2277 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2278 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2279 return BGP_ATTR_PARSE_ERROR
;
2282 /* Fetch attribute flag and type. */
2283 startp
= BGP_INPUT_PNT(peer
);
2284 /* "The lower-order four bits of the Attribute Flags octet are
2285 unused. They MUST be zero when sent and MUST be ignored when
2287 flag
= 0xF0 & stream_getc(BGP_INPUT(peer
));
2288 type
= stream_getc(BGP_INPUT(peer
));
2290 /* Check whether Extended-Length applies and is in bounds */
2291 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
)
2292 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1))) {
2294 "%s: Extended length set, but just %lu bytes of attr header",
2296 (unsigned long)(endp
2297 - STREAM_PNT(BGP_INPUT(peer
))));
2299 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2300 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2301 return BGP_ATTR_PARSE_ERROR
;
2304 /* Check extended attribue length bit. */
2305 if (CHECK_FLAG(flag
, BGP_ATTR_FLAG_EXTLEN
))
2306 length
= stream_getw(BGP_INPUT(peer
));
2308 length
= stream_getc(BGP_INPUT(peer
));
2310 /* If any attribute appears more than once in the UPDATE
2311 message, then the Error Subcode is set to Malformed Attribute
2314 if (CHECK_BITMAP(seen
, type
)) {
2316 "%s: error BGP attribute type %d appears twice in a message",
2319 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2320 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2321 return BGP_ATTR_PARSE_ERROR
;
2324 /* Set type to bitmap to check duplicate attribute. `type' is
2325 unsigned char so it never overflow bitmap range. */
2327 SET_BITMAP(seen
, type
);
2329 /* Overflow check. */
2330 attr_endp
= BGP_INPUT_PNT(peer
) + length
;
2332 if (attr_endp
> endp
) {
2334 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2335 peer
->host
, type
, length
, size
, attr_endp
,
2337 bgp_notify_send_with_data(
2338 peer
, BGP_NOTIFY_UPDATE_ERR
,
2339 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
, startp
,
2340 attr_endp
- startp
);
2341 return BGP_ATTR_PARSE_ERROR
;
2344 struct bgp_attr_parser_args attr_args
= {
2351 .total
= attr_endp
- startp
,
2355 /* If any recognized attribute has Attribute Flags that conflict
2356 with the Attribute Type Code, then the Error Subcode is set
2358 Attribute Flags Error. The Data field contains the erroneous
2359 attribute (type, length and value). */
2360 if (bgp_attr_flag_invalid(&attr_args
)) {
2361 bgp_attr_parse_ret_t ret
;
2362 ret
= bgp_attr_malformed(
2363 &attr_args
, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2365 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2370 /* OK check attribute and store it's value. */
2372 case BGP_ATTR_ORIGIN
:
2373 ret
= bgp_attr_origin(&attr_args
);
2375 case BGP_ATTR_AS_PATH
:
2376 ret
= bgp_attr_aspath(&attr_args
);
2378 case BGP_ATTR_AS4_PATH
:
2379 ret
= bgp_attr_as4_path(&attr_args
, &as4_path
);
2381 case BGP_ATTR_NEXT_HOP
:
2382 ret
= bgp_attr_nexthop(&attr_args
);
2384 case BGP_ATTR_MULTI_EXIT_DISC
:
2385 ret
= bgp_attr_med(&attr_args
);
2387 case BGP_ATTR_LOCAL_PREF
:
2388 ret
= bgp_attr_local_pref(&attr_args
);
2390 case BGP_ATTR_ATOMIC_AGGREGATE
:
2391 ret
= bgp_attr_atomic(&attr_args
);
2393 case BGP_ATTR_AGGREGATOR
:
2394 ret
= bgp_attr_aggregator(&attr_args
);
2396 case BGP_ATTR_AS4_AGGREGATOR
:
2397 ret
= bgp_attr_as4_aggregator(&attr_args
,
2399 &as4_aggregator_addr
);
2401 case BGP_ATTR_COMMUNITIES
:
2402 ret
= bgp_attr_community(&attr_args
);
2404 case BGP_ATTR_LARGE_COMMUNITIES
:
2405 ret
= bgp_attr_large_community(&attr_args
);
2407 case BGP_ATTR_ORIGINATOR_ID
:
2408 ret
= bgp_attr_originator_id(&attr_args
);
2410 case BGP_ATTR_CLUSTER_LIST
:
2411 ret
= bgp_attr_cluster_list(&attr_args
);
2413 case BGP_ATTR_MP_REACH_NLRI
:
2414 ret
= bgp_mp_reach_parse(&attr_args
, mp_update
);
2416 case BGP_ATTR_MP_UNREACH_NLRI
:
2417 ret
= bgp_mp_unreach_parse(&attr_args
, mp_withdraw
);
2419 case BGP_ATTR_EXT_COMMUNITIES
:
2420 ret
= bgp_attr_ext_communities(&attr_args
);
2425 case BGP_ATTR_ENCAP
:
2426 ret
= bgp_attr_encap(type
, peer
, length
, attr
, flag
,
2430 ret
= bgp_attr_unknown(&attr_args
);
2434 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
) {
2435 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2436 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2437 ret
= BGP_ATTR_PARSE_ERROR
;
2440 /* If hard error occured immediately return to the caller. */
2441 if (ret
== BGP_ATTR_PARSE_ERROR
) {
2442 zlog_warn("%s: Attribute %s, parse error", peer
->host
,
2443 lookup_msg(attr_str
, type
, NULL
));
2445 aspath_unintern(&as4_path
);
2448 if (ret
== BGP_ATTR_PARSE_WITHDRAW
) {
2451 "%s: Attribute %s, parse error - treating as withdrawal",
2452 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2454 aspath_unintern(&as4_path
);
2458 /* Check the fetched length. */
2459 if (BGP_INPUT_PNT(peer
) != attr_endp
) {
2460 zlog_warn("%s: BGP attribute %s, fetch error",
2461 peer
->host
, lookup_msg(attr_str
, type
, NULL
));
2462 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2463 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2465 aspath_unintern(&as4_path
);
2466 return BGP_ATTR_PARSE_ERROR
;
2470 /* Check final read pointer is same as end pointer. */
2471 if (BGP_INPUT_PNT(peer
) != endp
) {
2472 zlog_warn("%s: BGP attribute %s, length mismatch", peer
->host
,
2473 lookup_msg(attr_str
, type
, NULL
));
2474 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2475 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2477 aspath_unintern(&as4_path
);
2478 return BGP_ATTR_PARSE_ERROR
;
2481 /* Check all mandatory well-known attributes are present */
2483 bgp_attr_parse_ret_t ret
;
2484 if ((ret
= bgp_attr_check(peer
, attr
)) < 0) {
2486 aspath_unintern(&as4_path
);
2492 * At this place we can see whether we got AS4_PATH and/or
2493 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2494 * We can not do this before we've read all attributes because
2495 * the as4 handling does not say whether AS4_PATH has to be sent
2496 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2497 * in relationship to AGGREGATOR.
2498 * So, to be defensive, we are not relying on any order and read
2499 * all attributes first, including these 32bit ones, and now,
2500 * afterwards, we look what and if something is to be done for as4.
2502 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2505 /* actually... this doesn't ever return failure currently, but
2506 * better safe than sorry */
2507 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))
2508 && bgp_attr_munge_as4_attrs(peer
, attr
, as4_path
, as4_aggregator
,
2509 &as4_aggregator_addr
)) {
2510 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
2511 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2513 aspath_unintern(&as4_path
);
2514 return BGP_ATTR_PARSE_ERROR
;
2517 /* At this stage, we have done all fiddling with as4, and the
2518 * resulting info is in attr->aggregator resp. attr->aspath
2519 * so we can chuck as4_aggregator and as4_path alltogether in
2520 * order to save memory
2523 aspath_unintern(&as4_path
); /* unintern - it is in the hash */
2524 /* The flag that we got this is still there, but that does not
2529 * The "rest" of the code does nothing with as4_aggregator.
2530 * there is no memory attached specifically which is not part
2532 * so ignoring just means do nothing.
2535 * Finally do the checks on the aspath we did not do yet
2536 * because we waited for a potentially synthesized aspath.
2538 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
))) {
2539 ret
= bgp_attr_aspath_check(peer
, attr
);
2540 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2544 /* Finally intern unknown attribute. */
2545 if (attr
->extra
->transit
)
2546 attr
->extra
->transit
=
2547 transit_intern(attr
->extra
->transit
);
2548 if (attr
->extra
->encap_subtlvs
)
2549 attr
->extra
->encap_subtlvs
= encap_intern(
2550 attr
->extra
->encap_subtlvs
, ENCAP_SUBTLV_TYPE
);
2552 if (attr
->extra
->vnc_subtlvs
)
2553 attr
->extra
->vnc_subtlvs
= encap_intern(
2554 attr
->extra
->vnc_subtlvs
, VNC_SUBTLV_TYPE
);
2558 return BGP_ATTR_PARSE_PROCEED
;
2561 size_t bgp_packet_mpattr_start(struct stream
*s
, afi_t afi
, safi_t safi
,
2563 struct bpacket_attr_vec_arr
*vecarr
,
2570 /* Set extended bit always to encode the attribute length as 2 bytes */
2571 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
2572 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
2573 sizep
= stream_get_endp(s
);
2574 stream_putw(s
, 0); /* Marker: Attribute length. */
2577 /* Convert AFI, SAFI to values for packet. */
2578 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
2580 stream_putw(s
, pkt_afi
); /* AFI */
2581 stream_putc(s
, pkt_safi
); /* SAFI */
2582 if (afi
== AFI_L2VPN
)
2584 else if (nh_afi
== AFI_MAX
)
2586 BGP_NEXTHOP_AFI_FROM_NHLEN(attr
->extra
->mp_nexthop_len
);
2588 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2593 case SAFI_MULTICAST
:
2594 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2597 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2601 stream_putl(s
, 0); /* RD = 0, per RFC */
2603 stream_put(s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2607 stream_put(s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2616 case SAFI_MULTICAST
: {
2617 struct attr_extra
*attre
= attr
->extra
;
2619 assert(attr
->extra
);
2620 stream_putc(s
, attre
->mp_nexthop_len
);
2621 stream_put(s
, &attre
->mp_nexthop_global
,
2623 if (attre
->mp_nexthop_len
2624 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
2625 stream_put(s
, &attre
->mp_nexthop_local
,
2628 case SAFI_MPLS_VPN
: {
2629 struct attr_extra
*attre
= attr
->extra
;
2631 assert(attr
->extra
);
2632 if (attre
->mp_nexthop_len
2633 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2635 stream_putl(s
, 0); /* RD = 0, per RFC */
2637 stream_put(s
, &attre
->mp_nexthop_global
,
2639 } else if (attre
->mp_nexthop_len
2640 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2642 stream_putl(s
, 0); /* RD = 0, per RFC */
2644 stream_put(s
, &attre
->mp_nexthop_global
,
2646 stream_putl(s
, 0); /* RD = 0, per RFC */
2648 stream_put(s
, &attre
->mp_nexthop_local
,
2653 assert(attr
->extra
);
2654 stream_putc(s
, IPV6_MAX_BYTELEN
);
2655 stream_put(s
, &attr
->extra
->mp_nexthop_global
,
2665 if (attr
->extra
->mp_nexthop_len
2666 == BGP_ATTR_NHLEN_VPNV4
) {
2668 stream_putl(s
, 0); /* RD = 0, per RFC */
2671 &attr
->extra
->mp_nexthop_global_in
,
2673 } else if (attr
->extra
->mp_nexthop_len
2674 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2676 stream_putl(s
, 0); /* RD = 0, per RFC */
2678 stream_put(s
, &attr
->extra
->mp_nexthop_global
,
2680 } else if (attr
->extra
->mp_nexthop_len
2681 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2683 stream_putl(s
, 0); /* RD = 0, per RFC */
2685 stream_put(s
, &attr
->extra
->mp_nexthop_global
,
2687 stream_putl(s
, 0); /* RD = 0, per RFC */
2689 stream_put(s
, &attr
->extra
->mp_nexthop_local
,
2706 void bgp_packet_mpattr_prefix(struct stream
*s
, afi_t afi
, safi_t safi
,
2707 struct prefix
*p
, struct prefix_rd
*prd
,
2708 u_char
*tag
, int addpath_encode
,
2709 u_int32_t addpath_tx_id
, struct attr
*attr
)
2711 if (safi
== SAFI_MPLS_VPN
) {
2713 stream_putl(s
, addpath_tx_id
);
2714 /* Tag, RD, Prefix write. */
2715 stream_putc(s
, p
->prefixlen
+ 88);
2716 stream_put(s
, tag
, 3);
2717 stream_put(s
, prd
->val
, 8);
2718 stream_put(s
, &p
->u
.prefix
, PSIZE(p
->prefixlen
));
2719 } else if (safi
== SAFI_EVPN
) {
2720 bgp_packet_mpattr_route_type_5(s
, p
, prd
, tag
, attr
);
2722 stream_put_prefix_addpath(s
, p
, addpath_encode
, addpath_tx_id
);
2725 size_t bgp_packet_mpattr_prefix_size(afi_t afi
, safi_t safi
, struct prefix
*p
)
2727 int size
= PSIZE(p
->prefixlen
);
2728 if (safi
== SAFI_MPLS_VPN
)
2734 * Encodes the tunnel encapsulation attribute,
2735 * and with ENABLE_BGP_VNC the VNC attribute which uses
2736 * almost the same TLV format
2738 static void bgp_packet_mpattr_tea(struct bgp
*bgp
, struct peer
*peer
,
2739 struct stream
*s
, struct attr
*attr
,
2742 unsigned int attrlenfield
= 0;
2743 unsigned int attrhdrlen
= 0;
2744 struct bgp_attr_encap_subtlv
*subtlvs
;
2745 struct bgp_attr_encap_subtlv
*st
;
2746 const char *attrname
;
2748 if (!attr
|| !attr
->extra
2749 || (attrtype
== BGP_ATTR_ENCAP
2750 && (!attr
->extra
->encap_tunneltype
2751 || attr
->extra
->encap_tunneltype
== BGP_ENCAP_TYPE_MPLS
)))
2755 case BGP_ATTR_ENCAP
:
2756 attrname
= "Tunnel Encap";
2757 subtlvs
= attr
->extra
->encap_subtlvs
;
2758 if (subtlvs
== NULL
) /* nothing to do */
2761 * The tunnel encap attr has an "outer" tlv.
2763 * L = total length of subtlvs,
2764 * V = concatenated subtlvs.
2766 attrlenfield
= 2 + 2; /* T + L */
2767 attrhdrlen
= 1 + 1; /* subTLV T + L */
2773 subtlvs
= attr
->extra
->vnc_subtlvs
;
2774 if (subtlvs
== NULL
) /* nothing to do */
2776 attrlenfield
= 0; /* no outer T + L */
2777 attrhdrlen
= 2 + 2; /* subTLV T + L */
2785 /* compute attr length */
2786 for (st
= subtlvs
; st
; st
= st
->next
) {
2787 attrlenfield
+= (attrhdrlen
+ st
->length
);
2790 if (attrlenfield
> 0xffff) {
2791 zlog_info("%s attribute is too long (length=%d), can't send it",
2792 attrname
, attrlenfield
);
2796 if (attrlenfield
> 0xff) {
2797 /* 2-octet length field */
2799 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
2800 | BGP_ATTR_FLAG_EXTLEN
);
2801 stream_putc(s
, attrtype
);
2802 stream_putw(s
, attrlenfield
& 0xffff);
2804 /* 1-octet length field */
2805 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
);
2806 stream_putc(s
, attrtype
);
2807 stream_putc(s
, attrlenfield
& 0xff);
2810 if (attrtype
== BGP_ATTR_ENCAP
) {
2811 /* write outer T+L */
2812 stream_putw(s
, attr
->extra
->encap_tunneltype
);
2813 stream_putw(s
, attrlenfield
- 4);
2816 /* write each sub-tlv */
2817 for (st
= subtlvs
; st
; st
= st
->next
) {
2818 if (attrtype
== BGP_ATTR_ENCAP
) {
2819 stream_putc(s
, st
->type
);
2820 stream_putc(s
, st
->length
);
2823 stream_putw(s
, st
->type
);
2824 stream_putw(s
, st
->length
);
2827 stream_put(s
, st
->value
, st
->length
);
2831 void bgp_packet_mpattr_end(struct stream
*s
, size_t sizep
)
2833 /* Set MP attribute length. Don't count the (2) bytes used to encode
2835 stream_putw_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 2);
2838 /* Make attribute packet. */
2839 bgp_size_t
bgp_packet_attribute(struct bgp
*bgp
, struct peer
*peer
,
2840 struct stream
*s
, struct attr
*attr
,
2841 struct bpacket_attr_vec_arr
*vecarr
,
2842 struct prefix
*p
, afi_t afi
, safi_t safi
,
2843 struct peer
*from
, struct prefix_rd
*prd
,
2844 u_char
*tag
, int addpath_encode
,
2845 u_int32_t addpath_tx_id
)
2848 size_t aspath_sizep
;
2849 struct aspath
*aspath
;
2850 int send_as4_path
= 0;
2851 int send_as4_aggregator
= 0;
2852 int use32bit
= (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2857 /* Remember current pointer. */
2858 cp
= stream_get_endp(s
);
2861 && !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
2862 && !peer_cap_enhe(peer
))) {
2863 size_t mpattrlen_pos
= 0;
2865 mpattrlen_pos
= bgp_packet_mpattr_start(
2867 (peer_cap_enhe(peer
) ? AFI_IP6
2868 : AFI_MAX
), /* get from NH */
2870 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, tag
,
2871 addpath_encode
, addpath_tx_id
, attr
);
2872 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2875 /* Origin attribute. */
2876 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2877 stream_putc(s
, BGP_ATTR_ORIGIN
);
2879 stream_putc(s
, attr
->origin
);
2881 /* AS path attribute. */
2883 /* If remote-peer is EBGP */
2884 if (peer
->sort
== BGP_PEER_EBGP
2885 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2886 PEER_FLAG_AS_PATH_UNCHANGED
)
2887 || attr
->aspath
->segments
== NULL
)
2888 && (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2889 PEER_FLAG_RSERVER_CLIENT
))) {
2890 aspath
= aspath_dup(attr
->aspath
);
2892 /* Even though we may not be configured for confederations we
2894 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2895 aspath
= aspath_delete_confed_seq(aspath
);
2897 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
2898 /* Stuff our path CONFED_ID on the front */
2899 aspath
= aspath_add_seq(aspath
, bgp
->confed_id
);
2901 if (peer
->change_local_as
) {
2902 /* If replace-as is specified, we only use the
2903 change_local_as when
2904 advertising routes. */
2907 PEER_FLAG_LOCAL_AS_REPLACE_AS
)) {
2908 aspath
= aspath_add_seq(aspath
,
2911 aspath
= aspath_add_seq(aspath
,
2912 peer
->change_local_as
);
2914 aspath
= aspath_add_seq(aspath
, peer
->local_as
);
2917 } else if (peer
->sort
== BGP_PEER_CONFED
) {
2918 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2920 aspath
= aspath_dup(attr
->aspath
);
2921 aspath
= aspath_add_confed_seq(aspath
, peer
->local_as
);
2923 aspath
= attr
->aspath
;
2925 /* If peer is not AS4 capable, then:
2926 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2927 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2929 * types are in it (i.e. exclude them if they are there)
2930 * AND do this only if there is at least one asnum > 65535 in the
2932 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2934 * all ASnums > 65535 to BGP_AS_TRANS
2937 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
2938 stream_putc(s
, BGP_ATTR_AS_PATH
);
2939 aspath_sizep
= stream_get_endp(s
);
2941 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, use32bit
));
2943 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2946 if (!use32bit
&& aspath_has_as4(aspath
))
2948 1; /* we'll do this later, at the correct place */
2950 /* Nexthop attribute. */
2951 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&& !peer_cap_enhe(peer
)) {
2952 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2953 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2954 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2955 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2958 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
2959 } else if (safi
== SAFI_UNICAST
&& peer_cap_enhe(from
)) {
2961 * Likely this is the case when an IPv4 prefix was
2963 * Extended Next-hop capability and now being advertised
2966 * Setting the mandatory (ipv4) next-hop attribute here
2968 * implicit next-hop self with correct (ipv4 address
2971 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2972 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
2973 bpacket_attr_vec_arr_set_vec(vecarr
, BGP_ATTR_VEC_NH
, s
,
2976 stream_put_ipv4(s
, 0);
2980 /* MED attribute. */
2981 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)
2982 || bgp
->maxmed_active
) {
2983 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
2984 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
2986 stream_putl(s
, (bgp
->maxmed_active
? bgp
->maxmed_value
2990 /* Local preference. */
2991 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
) {
2992 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
2993 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
2995 stream_putl(s
, attr
->local_pref
);
2998 /* Atomic aggregate. */
2999 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3000 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3001 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3006 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3007 assert(attr
->extra
);
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
->extra
->aggregator_as
);
3018 /* 2-byte AS peer */
3021 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3023 if (attr
->extra
->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;
3035 (u_int16_t
)attr
->extra
->aggregator_as
);
3037 stream_put_ipv4(s
, attr
->extra
->aggregator_addr
.s_addr
);
3040 /* Community attribute. */
3041 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
3042 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))) {
3043 if (attr
->community
->size
* 4 > 255) {
3045 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3046 | BGP_ATTR_FLAG_EXTLEN
);
3047 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3048 stream_putw(s
, attr
->community
->size
* 4);
3051 BGP_ATTR_FLAG_OPTIONAL
3052 | BGP_ATTR_FLAG_TRANS
);
3053 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3054 stream_putc(s
, attr
->community
->size
* 4);
3056 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3060 * Large Community attribute.
3062 if (attr
->extra
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
],
3063 PEER_FLAG_SEND_LARGE_COMMUNITY
)
3064 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))) {
3065 if (attr
->extra
->lcommunity
->size
* 12 > 255) {
3067 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3068 | BGP_ATTR_FLAG_EXTLEN
);
3069 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3070 stream_putw(s
, attr
->extra
->lcommunity
->size
* 12);
3073 BGP_ATTR_FLAG_OPTIONAL
3074 | BGP_ATTR_FLAG_TRANS
);
3075 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3076 stream_putc(s
, attr
->extra
->lcommunity
->size
* 12);
3078 stream_put(s
, attr
->extra
->lcommunity
->val
,
3079 attr
->extra
->lcommunity
->size
* 12);
3082 /* Route Reflector. */
3083 if (peer
->sort
== BGP_PEER_IBGP
&& from
3084 && from
->sort
== BGP_PEER_IBGP
) {
3085 /* Originator ID. */
3086 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3087 stream_putc(s
, BGP_ATTR_ORIGINATOR_ID
);
3090 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
3091 stream_put_in_addr(s
, &attr
->extra
->originator_id
);
3093 stream_put_in_addr(s
, &from
->remote_id
);
3096 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3097 stream_putc(s
, BGP_ATTR_CLUSTER_LIST
);
3099 if (attr
->extra
&& attr
->extra
->cluster
) {
3100 stream_putc(s
, attr
->extra
->cluster
->length
+ 4);
3101 /* If this peer configuration's parent BGP has
3103 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3104 stream_put_in_addr(s
, &bgp
->cluster_id
);
3106 stream_put_in_addr(s
, &bgp
->router_id
);
3107 stream_put(s
, attr
->extra
->cluster
->list
,
3108 attr
->extra
->cluster
->length
);
3111 /* If this peer configuration's parent BGP has
3113 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
3114 stream_put_in_addr(s
, &bgp
->cluster_id
);
3116 stream_put_in_addr(s
, &bgp
->router_id
);
3120 /* Extended Communities attribute. */
3121 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3122 && (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3123 struct attr_extra
*attre
= attr
->extra
;
3127 if (peer
->sort
== BGP_PEER_IBGP
3128 || peer
->sort
== BGP_PEER_CONFED
) {
3129 if (attre
->ecommunity
->size
* 8 > 255) {
3131 BGP_ATTR_FLAG_OPTIONAL
3132 | BGP_ATTR_FLAG_TRANS
3133 | BGP_ATTR_FLAG_EXTLEN
);
3134 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3135 stream_putw(s
, attre
->ecommunity
->size
* 8);
3138 BGP_ATTR_FLAG_OPTIONAL
3139 | BGP_ATTR_FLAG_TRANS
);
3140 stream_putc(s
, BGP_ATTR_EXT_COMMUNITIES
);
3141 stream_putc(s
, attre
->ecommunity
->size
* 8);
3143 stream_put(s
, attre
->ecommunity
->val
,
3144 attre
->ecommunity
->size
* 8);
3148 int ecom_tr_size
= 0;
3151 for (i
= 0; i
< attre
->ecommunity
->size
; i
++) {
3152 pnt
= attre
->ecommunity
->val
+ (i
* 8);
3155 if (CHECK_FLAG(tbit
,
3156 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3163 if (ecom_tr_size
* 8 > 255) {
3166 BGP_ATTR_FLAG_OPTIONAL
3167 | BGP_ATTR_FLAG_TRANS
3168 | BGP_ATTR_FLAG_EXTLEN
);
3170 BGP_ATTR_EXT_COMMUNITIES
);
3171 stream_putw(s
, ecom_tr_size
* 8);
3175 BGP_ATTR_FLAG_OPTIONAL
3176 | BGP_ATTR_FLAG_TRANS
);
3178 BGP_ATTR_EXT_COMMUNITIES
);
3179 stream_putc(s
, ecom_tr_size
* 8);
3182 for (i
= 0; i
< attre
->ecommunity
->size
; i
++) {
3183 pnt
= attre
->ecommunity
->val
+ (i
* 8);
3188 ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3191 stream_put(s
, pnt
, 8);
3197 if (send_as4_path
) {
3198 /* If the peer is NOT As4 capable, AND */
3199 /* there are ASnums > 65535 in path THEN
3200 * give out AS4_PATH */
3202 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3204 * Hm, I wonder... confederation things *should* only be at
3205 * the beginning of an aspath, right? Then we should use
3206 * aspath_delete_confed_seq for this, because it is already
3208 * Folks, talk to me: what is reasonable here!?
3210 aspath
= aspath_delete_confed_seq(aspath
);
3213 BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
3214 | BGP_ATTR_FLAG_EXTLEN
);
3215 stream_putc(s
, BGP_ATTR_AS4_PATH
);
3216 aspath_sizep
= stream_get_endp(s
);
3218 stream_putw_at(s
, aspath_sizep
, aspath_put(s
, aspath
, 1));
3221 if (aspath
!= attr
->aspath
)
3222 aspath_free(aspath
);
3224 if (send_as4_aggregator
) {
3225 assert(attr
->extra
);
3227 /* send AS4_AGGREGATOR, at this place */
3228 /* this section of code moved here in order to ensure the
3230 * *ascending* order of attributes
3232 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3233 stream_putc(s
, BGP_ATTR_AS4_AGGREGATOR
);
3235 stream_putl(s
, attr
->extra
->aggregator_as
);
3236 stream_put_ipv4(s
, attr
->extra
->aggregator_addr
.s_addr
);
3239 if (((afi
== AFI_IP
|| afi
== AFI_IP6
)
3240 && (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3241 || (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)) {
3242 /* Tunnel Encap attribute */
3243 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3247 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3251 /* Unknown transit attribute. */
3252 if (attr
->extra
&& attr
->extra
->transit
)
3253 stream_put(s
, attr
->extra
->transit
->val
,
3254 attr
->extra
->transit
->length
);
3256 /* Return total size of attribute. */
3257 return stream_get_endp(s
) - cp
;
3260 size_t bgp_packet_mpunreach_start(struct stream
*s
, afi_t afi
, safi_t safi
)
3262 unsigned long attrlen_pnt
;
3266 /* Set extended bit always to encode the attribute length as 2 bytes */
3267 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_EXTLEN
);
3268 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
3270 attrlen_pnt
= stream_get_endp(s
);
3271 stream_putw(s
, 0); /* Length of this attribute. */
3273 /* Convert AFI, SAFI to values for packet. */
3274 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
3276 stream_putw(s
, pkt_afi
);
3277 stream_putc(s
, pkt_safi
);
3282 void bgp_packet_mpunreach_prefix(struct stream
*s
, struct prefix
*p
, afi_t afi
,
3283 safi_t safi
, struct prefix_rd
*prd
,
3284 u_char
*tag
, int addpath_encode
,
3285 u_int32_t addpath_tx_id
, struct attr
*attr
)
3287 return bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, tag
,
3288 addpath_encode
, addpath_tx_id
, attr
);
3291 void bgp_packet_mpunreach_end(struct stream
*s
, size_t attrlen_pnt
)
3293 bgp_packet_mpattr_end(s
, attrlen_pnt
);
3296 /* Initialization of attribute. */
3297 void bgp_attr_init(void)
3309 void bgp_attr_finish(void)
3314 ecommunity_finish();
3315 lcommunity_finish();
3321 /* Make attribute packet. */
3322 void bgp_dump_routes_attr(struct stream
*s
, struct attr
*attr
,
3323 struct prefix
*prefix
)
3328 struct aspath
*aspath
;
3329 int addpath_encode
= 0;
3330 u_int32_t addpath_tx_id
= 0;
3332 /* Remember current pointer. */
3333 cp
= stream_get_endp(s
);
3335 /* Place holder of length. */
3338 /* Origin attribute. */
3339 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3340 stream_putc(s
, BGP_ATTR_ORIGIN
);
3342 stream_putc(s
, attr
->origin
);
3344 aspath
= attr
->aspath
;
3346 stream_putc(s
, BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_EXTLEN
);
3347 stream_putc(s
, BGP_ATTR_AS_PATH
);
3348 aspath_lenp
= stream_get_endp(s
);
3351 stream_putw_at(s
, aspath_lenp
, aspath_put(s
, aspath
, 1));
3353 /* Nexthop attribute. */
3354 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3355 if (prefix
!= NULL
&& prefix
->family
!= AF_INET6
) {
3356 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3357 stream_putc(s
, BGP_ATTR_NEXT_HOP
);
3359 stream_put_ipv4(s
, attr
->nexthop
.s_addr
);
3362 /* MED attribute. */
3363 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
3364 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3365 stream_putc(s
, BGP_ATTR_MULTI_EXIT_DISC
);
3367 stream_putl(s
, attr
->med
);
3370 /* Local preference. */
3371 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
3372 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3373 stream_putc(s
, BGP_ATTR_LOCAL_PREF
);
3375 stream_putl(s
, attr
->local_pref
);
3378 /* Atomic aggregate. */
3379 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
3380 stream_putc(s
, BGP_ATTR_FLAG_TRANS
);
3381 stream_putc(s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3386 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
)) {
3387 assert(attr
->extra
);
3388 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
);
3389 stream_putc(s
, BGP_ATTR_AGGREGATOR
);
3391 stream_putl(s
, attr
->extra
->aggregator_as
);
3392 stream_put_ipv4(s
, attr
->extra
->aggregator_addr
.s_addr
);
3395 /* Community attribute. */
3396 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
)) {
3397 if (attr
->community
->size
* 4 > 255) {
3399 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3400 | BGP_ATTR_FLAG_EXTLEN
);
3401 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3402 stream_putw(s
, attr
->community
->size
* 4);
3405 BGP_ATTR_FLAG_OPTIONAL
3406 | BGP_ATTR_FLAG_TRANS
);
3407 stream_putc(s
, BGP_ATTR_COMMUNITIES
);
3408 stream_putc(s
, attr
->community
->size
* 4);
3410 stream_put(s
, attr
->community
->val
, attr
->community
->size
* 4);
3413 /* Large Community attribute. */
3415 && attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
3416 if (attr
->extra
->lcommunity
->size
* 12 > 255) {
3418 BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
3419 | BGP_ATTR_FLAG_EXTLEN
);
3420 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3421 stream_putw(s
, attr
->extra
->lcommunity
->size
* 12);
3424 BGP_ATTR_FLAG_OPTIONAL
3425 | BGP_ATTR_FLAG_TRANS
);
3426 stream_putc(s
, BGP_ATTR_LARGE_COMMUNITIES
);
3427 stream_putc(s
, attr
->extra
->lcommunity
->size
* 12);
3430 stream_put(s
, attr
->extra
->lcommunity
->val
,
3431 attr
->extra
->lcommunity
->size
* 12);
3434 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3435 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
&& attr
->extra
3436 && (attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
3437 || attr
->extra
->mp_nexthop_len
3438 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)) {
3440 struct attr_extra
*attre
= attr
->extra
;
3442 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3443 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3444 sizep
= stream_get_endp(s
);
3447 stream_putc(s
, 0); /* Marker: Attribute length. */
3448 stream_putw(s
, AFI_IP6
); /* AFI */
3449 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3452 stream_putc(s
, attre
->mp_nexthop_len
);
3453 stream_put(s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3454 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3455 stream_put(s
, &attre
->mp_nexthop_local
,
3462 stream_put_prefix_addpath(s
, prefix
, addpath_encode
,
3465 /* Set MP attribute length. */
3466 stream_putc_at(s
, sizep
, (stream_get_endp(s
) - sizep
) - 1);
3469 /* Return total size of attribute. */
3470 len
= stream_get_endp(s
) - cp
- 2;
3471 stream_putw_at(s
, cp
, len
);