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_updgrp.h"
45 #include "bgpd/bgp_encap_types.h"
47 # include "bgp_rfapi_cfg.h"
48 # include "bgp_encap_types.h"
49 # include "bgp_vnc_types.h"
52 /* Attribute strings for logging. */
53 static const struct message attr_str
[] =
55 { BGP_ATTR_ORIGIN
, "ORIGIN" },
56 { BGP_ATTR_AS_PATH
, "AS_PATH" },
57 { BGP_ATTR_NEXT_HOP
, "NEXT_HOP" },
58 { BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC" },
59 { BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF" },
60 { BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE" },
61 { BGP_ATTR_AGGREGATOR
, "AGGREGATOR" },
62 { BGP_ATTR_COMMUNITIES
, "COMMUNITY" },
63 { BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID" },
64 { BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST" },
65 { BGP_ATTR_DPA
, "DPA" },
66 { BGP_ATTR_ADVERTISER
, "ADVERTISER"} ,
67 { BGP_ATTR_RCID_PATH
, "RCID_PATH" },
68 { BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI" },
69 { BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI" },
70 { BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES" },
71 { BGP_ATTR_AS4_PATH
, "AS4_PATH" },
72 { BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR" },
73 { BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT" },
74 { BGP_ATTR_ENCAP
, "ENCAP" },
76 { BGP_ATTR_VNC
, "VNC" },
79 static const int attr_str_max
= array_size(attr_str
);
81 static const struct message attr_flag_str
[] =
83 { BGP_ATTR_FLAG_OPTIONAL
, "Optional" },
84 { BGP_ATTR_FLAG_TRANS
, "Transitive" },
85 { BGP_ATTR_FLAG_PARTIAL
, "Partial" },
86 /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
87 { BGP_ATTR_FLAG_EXTLEN
, "Extended Length" },
90 static struct hash
*cluster_hash
;
93 cluster_hash_alloc (void *p
)
95 const struct cluster_list
*val
= (const struct cluster_list
*) p
;
96 struct cluster_list
*cluster
;
98 cluster
= XMALLOC (MTYPE_CLUSTER
, sizeof (struct cluster_list
));
99 cluster
->length
= val
->length
;
103 cluster
->list
= XMALLOC (MTYPE_CLUSTER_VAL
, val
->length
);
104 memcpy (cluster
->list
, val
->list
, val
->length
);
107 cluster
->list
= NULL
;
114 /* Cluster list related functions. */
115 static struct cluster_list
*
116 cluster_parse (struct in_addr
* pnt
, int length
)
118 struct cluster_list tmp
;
119 struct cluster_list
*cluster
;
124 cluster
= hash_get (cluster_hash
, &tmp
, cluster_hash_alloc
);
130 cluster_loop_check (struct cluster_list
*cluster
, struct in_addr originator
)
134 for (i
= 0; i
< cluster
->length
/ 4; i
++)
135 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
141 cluster_hash_key_make (void *p
)
143 const struct cluster_list
*cluster
= p
;
145 return jhash(cluster
->list
, cluster
->length
, 0);
149 cluster_hash_cmp (const void *p1
, const void *p2
)
151 const struct cluster_list
* cluster1
= p1
;
152 const struct cluster_list
* cluster2
= p2
;
154 return (cluster1
->length
== cluster2
->length
&&
155 memcmp (cluster1
->list
, cluster2
->list
, cluster1
->length
) == 0);
159 cluster_free (struct cluster_list
*cluster
)
162 XFREE (MTYPE_CLUSTER_VAL
, cluster
->list
);
163 XFREE (MTYPE_CLUSTER
, cluster
);
166 static struct cluster_list
*
167 cluster_dup (struct cluster_list
*cluster
)
169 struct cluster_list
*new;
171 new = XCALLOC (MTYPE_CLUSTER
, sizeof (struct cluster_list
));
172 new->length
= cluster
->length
;
176 new->list
= XMALLOC (MTYPE_CLUSTER_VAL
, cluster
->length
);
177 memcpy (new->list
, cluster
->list
, cluster
->length
);
185 static struct cluster_list
*
186 cluster_intern (struct cluster_list
*cluster
)
188 struct cluster_list
*find
;
190 find
= hash_get (cluster_hash
, cluster
, cluster_hash_alloc
);
197 cluster_unintern (struct cluster_list
*cluster
)
202 if (cluster
->refcnt
== 0)
204 hash_release (cluster_hash
, cluster
);
205 cluster_free (cluster
);
212 cluster_hash
= hash_create (cluster_hash_key_make
, cluster_hash_cmp
);
216 cluster_finish (void)
218 hash_free (cluster_hash
);
222 struct bgp_attr_encap_subtlv
*
223 encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
225 struct bgp_attr_encap_subtlv
*new;
226 struct bgp_attr_encap_subtlv
*tail
;
227 struct bgp_attr_encap_subtlv
*p
;
229 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
230 int size
= sizeof(struct bgp_attr_encap_subtlv
) - 1 + p
->length
;
232 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
235 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
238 memcpy(tail
, p
, size
);
246 encap_free(struct bgp_attr_encap_subtlv
*p
)
248 struct bgp_attr_encap_subtlv
*next
;
252 XFREE(MTYPE_ENCAP_TLV
, p
);
258 bgp_attr_flush_encap(struct attr
*attr
)
260 if (!attr
|| !attr
->extra
)
263 if (attr
->extra
->encap_subtlvs
) {
264 encap_free(attr
->extra
->encap_subtlvs
);
265 attr
->extra
->encap_subtlvs
= NULL
;
268 if (attr
->extra
->vnc_subtlvs
) {
269 encap_free(attr
->extra
->vnc_subtlvs
);
270 attr
->extra
->vnc_subtlvs
= NULL
;
276 * Compare encap sub-tlv chains
281 * This algorithm could be made faster if needed
284 encap_same(struct bgp_attr_encap_subtlv
*h1
, struct bgp_attr_encap_subtlv
*h2
)
286 struct bgp_attr_encap_subtlv
*p
;
287 struct bgp_attr_encap_subtlv
*q
;
298 for (p
= h1
; p
; p
= p
->next
) {
299 for (q
= h2
; q
; q
= q
->next
) {
300 if ((p
->type
== q
->type
) &&
301 (p
->length
== q
->length
) &&
302 !memcmp(p
->value
, q
->value
, p
->length
)) {
311 for (p
= h2
; p
; p
= p
->next
) {
312 for (q
= h1
; q
; q
= q
->next
) {
313 if ((p
->type
== q
->type
) &&
314 (p
->length
== q
->length
) &&
315 !memcmp(p
->value
, q
->value
, p
->length
)) {
327 /* Unknown transit attribute. */
328 static struct hash
*transit_hash
;
331 transit_free (struct transit
*transit
)
334 XFREE (MTYPE_TRANSIT_VAL
, transit
->val
);
335 XFREE (MTYPE_TRANSIT
, transit
);
338 static struct transit
*
339 transit_dup (struct transit
*transit
)
343 new = XCALLOC (MTYPE_TRANSIT
, sizeof (struct transit
));
344 new->length
= transit
->length
;
347 new->val
= XMALLOC (MTYPE_TRANSIT_VAL
, transit
->length
);
348 memcpy (new->val
, transit
->val
, transit
->length
);
357 transit_hash_alloc (void *p
)
359 /* Transit structure is already allocated. */
363 static struct transit
*
364 transit_intern (struct transit
*transit
)
366 struct transit
*find
;
368 find
= hash_get (transit_hash
, transit
, transit_hash_alloc
);
370 transit_free (transit
);
377 transit_unintern (struct transit
*transit
)
382 if (transit
->refcnt
== 0)
384 hash_release (transit_hash
, transit
);
385 transit_free (transit
);
390 transit_hash_key_make (void *p
)
392 const struct transit
* transit
= p
;
394 return jhash(transit
->val
, transit
->length
, 0);
398 transit_hash_cmp (const void *p1
, const void *p2
)
400 const struct transit
* transit1
= p1
;
401 const struct transit
* transit2
= p2
;
403 return (transit1
->length
== transit2
->length
&&
404 memcmp (transit1
->val
, transit2
->val
, transit1
->length
) == 0);
410 transit_hash
= hash_create (transit_hash_key_make
, transit_hash_cmp
);
414 transit_finish (void)
416 hash_free (transit_hash
);
420 /* Attribute hash routines. */
421 static struct hash
*attrhash
;
423 static struct attr_extra
*
424 bgp_attr_extra_new (void)
426 return XCALLOC (MTYPE_ATTR_EXTRA
, sizeof (struct attr_extra
));
430 bgp_attr_extra_free (struct attr
*attr
)
434 if (attr
->extra
->encap_subtlvs
) {
435 encap_free(attr
->extra
->encap_subtlvs
);
436 attr
->extra
->encap_subtlvs
= NULL
;
439 if (attr
->extra
->vnc_subtlvs
) {
440 encap_free(attr
->extra
->vnc_subtlvs
);
441 attr
->extra
->vnc_subtlvs
= NULL
;
444 XFREE (MTYPE_ATTR_EXTRA
, attr
->extra
);
450 bgp_attr_extra_get (struct attr
*attr
)
453 attr
->extra
= bgp_attr_extra_new();
457 /* Shallow copy of an attribute
458 * Though, not so shallow that it doesn't copy the contents
459 * of the attr_extra pointed to by 'extra'
462 bgp_attr_dup (struct attr
*new, struct attr
*orig
)
464 struct attr_extra
*extra
= new->extra
;
467 /* if caller provided attr_extra space, use it in any case.
469 * This is neccesary even if orig->extra equals NULL, because otherwise
470 * memory may be later allocated on the heap by bgp_attr_extra_get.
472 * That memory would eventually be leaked, because the caller must not
473 * call bgp_attr_extra_free if he provided attr_extra on the stack.
478 memset(new->extra
, 0, sizeof(struct attr_extra
));
480 *new->extra
= *orig
->extra
;
481 if (orig
->extra
->encap_subtlvs
) {
482 new->extra
->encap_subtlvs
= encap_tlv_dup(orig
->extra
->encap_subtlvs
);
485 if (orig
->extra
->vnc_subtlvs
) {
486 new->extra
->vnc_subtlvs
= encap_tlv_dup(orig
->extra
->vnc_subtlvs
);
491 else if (orig
->extra
)
493 new->extra
= bgp_attr_extra_new();
494 *new->extra
= *orig
->extra
;
495 if (orig
->extra
->encap_subtlvs
) {
496 new->extra
->encap_subtlvs
= encap_tlv_dup(orig
->extra
->encap_subtlvs
);
499 if (orig
->extra
->vnc_subtlvs
) {
500 new->extra
->vnc_subtlvs
= encap_tlv_dup(orig
->extra
->vnc_subtlvs
);
507 bgp_attr_deep_dup (struct attr
*new, struct attr
*orig
)
510 new->aspath
= aspath_dup(orig
->aspath
);
513 new->community
= community_dup(orig
->community
);
517 if (orig
->extra
->ecommunity
)
518 new->extra
->ecommunity
= ecommunity_dup(orig
->extra
->ecommunity
);
519 if (orig
->extra
->cluster
)
520 new->extra
->cluster
= cluster_dup(orig
->extra
->cluster
);
521 if (orig
->extra
->transit
)
522 new->extra
->transit
= transit_dup(orig
->extra
->transit
);
527 bgp_attr_deep_free (struct attr
*attr
)
530 aspath_free(attr
->aspath
);
533 community_free(attr
->community
);
537 if (attr
->extra
->ecommunity
)
538 ecommunity_free(&attr
->extra
->ecommunity
);
539 if (attr
->extra
->cluster
)
540 cluster_free(attr
->extra
->cluster
);
541 if (attr
->extra
->transit
)
542 transit_free(attr
->extra
->transit
);
549 return attrhash
->count
;
553 attr_unknown_count (void)
555 return transit_hash
->count
;
559 attrhash_key_make (void *p
)
561 const struct attr
*attr
= (struct attr
*) p
;
562 const struct attr_extra
*extra
= attr
->extra
;
564 #define MIX(val) key = jhash_1word(val, key)
567 MIX(attr
->nexthop
.s_addr
);
569 MIX(attr
->local_pref
);
572 key
+= attr
->nexthop
.s_addr
;
574 key
+= attr
->local_pref
;
578 MIX(extra
->aggregator_as
);
579 MIX(extra
->aggregator_addr
.s_addr
);
581 MIX(extra
->mp_nexthop_global_in
.s_addr
);
582 MIX(extra
->originator_id
.s_addr
);
587 MIX(aspath_key_make (attr
->aspath
));
589 MIX(community_hash_make (attr
->community
));
593 if (extra
->ecommunity
)
594 MIX(ecommunity_hash_make (extra
->ecommunity
));
596 MIX(cluster_hash_key_make (extra
->cluster
));
598 MIX(transit_hash_key_make (extra
->transit
));
601 MIX(extra
->mp_nexthop_len
);
602 key
= jhash(extra
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
603 key
= jhash(extra
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
604 #endif /* HAVE_IPV6 */
611 attrhash_cmp (const void *p1
, const void *p2
)
613 const struct attr
* attr1
= p1
;
614 const struct attr
* attr2
= p2
;
616 if (attr1
->flag
== attr2
->flag
617 && attr1
->origin
== attr2
->origin
618 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
619 && attr1
->aspath
== attr2
->aspath
620 && attr1
->community
== attr2
->community
621 && attr1
->med
== attr2
->med
622 && attr1
->local_pref
== attr2
->local_pref
623 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
)
625 const struct attr_extra
*ae1
= attr1
->extra
;
626 const struct attr_extra
*ae2
= attr2
->extra
;
629 && ae1
->aggregator_as
== ae2
->aggregator_as
630 && ae1
->aggregator_addr
.s_addr
== ae2
->aggregator_addr
.s_addr
631 && ae1
->weight
== ae2
->weight
632 && ae1
->tag
== ae2
->tag
634 && ae1
->mp_nexthop_len
== ae2
->mp_nexthop_len
635 && IPV6_ADDR_SAME (&ae1
->mp_nexthop_global
, &ae2
->mp_nexthop_global
)
636 && IPV6_ADDR_SAME (&ae1
->mp_nexthop_local
, &ae2
->mp_nexthop_local
)
637 #endif /* HAVE_IPV6 */
638 && IPV4_ADDR_SAME (&ae1
->mp_nexthop_global_in
, &ae2
->mp_nexthop_global_in
)
639 && ae1
->ecommunity
== ae2
->ecommunity
640 && ae1
->cluster
== ae2
->cluster
641 && ae1
->transit
== ae2
->transit
642 && (ae1
->encap_tunneltype
== ae2
->encap_tunneltype
)
643 && encap_same(ae1
->encap_subtlvs
, ae2
->encap_subtlvs
)
645 && encap_same(ae1
->vnc_subtlvs
, ae2
->vnc_subtlvs
)
647 && IPV4_ADDR_SAME (&ae1
->originator_id
, &ae2
->originator_id
))
651 /* neither attribute has extra attributes, so they're same */
661 attrhash
= hash_create (attrhash_key_make
, attrhash_cmp
);
665 attrhash_finish (void)
667 hash_free (attrhash
);
672 attr_show_all_iterator (struct hash_backet
*backet
, struct vty
*vty
)
674 struct attr
*attr
= backet
->data
;
676 vty_out (vty
, "attr[%ld] nexthop %s%s", attr
->refcnt
,
677 inet_ntoa (attr
->nexthop
), VTY_NEWLINE
);
681 attr_show_all (struct vty
*vty
)
683 hash_iterate (attrhash
,
684 (void (*)(struct hash_backet
*, void *))
685 attr_show_all_iterator
,
690 bgp_attr_hash_alloc (void *p
)
692 const struct attr
* val
= (const struct attr
*) p
;
695 attr
= XMALLOC (MTYPE_ATTR
, sizeof (struct attr
));
699 attr
->extra
= bgp_attr_extra_new ();
700 *attr
->extra
= *val
->extra
;
702 if (attr
->extra
->encap_subtlvs
) {
703 attr
->extra
->encap_subtlvs
= encap_tlv_dup(attr
->extra
->encap_subtlvs
);
706 if (attr
->extra
->vnc_subtlvs
) {
707 attr
->extra
->vnc_subtlvs
= encap_tlv_dup(attr
->extra
->vnc_subtlvs
);
715 /* Internet argument attribute. */
717 bgp_attr_intern (struct attr
*attr
)
721 /* Intern referenced strucutre. */
724 if (! attr
->aspath
->refcnt
)
725 attr
->aspath
= aspath_intern (attr
->aspath
);
727 attr
->aspath
->refcnt
++;
731 if (! attr
->community
->refcnt
)
732 attr
->community
= community_intern (attr
->community
);
734 attr
->community
->refcnt
++;
738 struct attr_extra
*attre
= attr
->extra
;
740 if (attre
->ecommunity
)
742 if (! attre
->ecommunity
->refcnt
)
743 attre
->ecommunity
= ecommunity_intern (attre
->ecommunity
);
745 attre
->ecommunity
->refcnt
++;
750 if (! attre
->cluster
->refcnt
)
751 attre
->cluster
= cluster_intern (attre
->cluster
);
753 attre
->cluster
->refcnt
++;
757 if (! attre
->transit
->refcnt
)
758 attre
->transit
= transit_intern (attre
->transit
);
760 attre
->transit
->refcnt
++;
764 find
= (struct attr
*) hash_get (attrhash
, attr
, bgp_attr_hash_alloc
);
771 * Increment the refcount on various structures that attr holds.
772 * Note on usage: call _only_ when the 'attr' object has already
773 * been 'intern'ed and exists in 'attrhash' table. The function
774 * serves to hold a reference to that (real) object.
775 * Note also that the caller can safely call bgp_attr_unintern()
776 * after calling bgp_attr_refcount(). That would release the
777 * reference and could result in a free() of the attr object.
780 bgp_attr_refcount (struct attr
*attr
)
782 /* Intern referenced strucutre. */
784 attr
->aspath
->refcnt
++;
787 attr
->community
->refcnt
++;
791 struct attr_extra
*attre
= attr
->extra
;
792 if (attre
->ecommunity
)
793 attre
->ecommunity
->refcnt
++;
796 attre
->cluster
->refcnt
++;
799 attre
->transit
->refcnt
++;
805 /* Make network statement's attribute. */
807 bgp_attr_default_set (struct attr
*attr
, u_char origin
)
809 memset (attr
, 0, sizeof (struct attr
));
810 bgp_attr_extra_get (attr
);
812 attr
->origin
= origin
;
813 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
814 attr
->aspath
= aspath_empty ();
815 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
816 attr
->extra
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
817 attr
->extra
->tag
= 0;
818 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
820 attr
->extra
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
827 /* Make network statement's attribute. */
829 bgp_attr_default_intern (u_char origin
)
834 memset (&attr
, 0, sizeof (struct attr
));
835 bgp_attr_extra_get (&attr
);
837 bgp_attr_default_set(&attr
, origin
);
839 new = bgp_attr_intern (&attr
);
840 bgp_attr_extra_free (&attr
);
842 aspath_unintern (&new->aspath
);
846 /* Create the attributes for an aggregate */
848 bgp_attr_aggregate_intern (struct bgp
*bgp
, u_char origin
,
849 struct aspath
*aspath
,
850 struct community
*community
, int as_set
,
851 u_char atomic_aggregate
)
855 struct attr_extra attre
;
857 memset (&attr
, 0, sizeof (struct attr
));
858 memset (&attre
, 0, sizeof (struct attr_extra
));
861 /* Origin attribute. */
862 attr
.origin
= origin
;
863 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
865 /* AS path attribute. */
867 attr
.aspath
= aspath_intern (aspath
);
869 attr
.aspath
= aspath_empty ();
870 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
872 /* Next hop attribute. */
873 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
877 attr
.community
= community
;
878 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
);
881 attre
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
883 attre
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
885 if (! as_set
|| atomic_aggregate
)
886 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
);
887 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
);
888 if (CHECK_FLAG (bgp
->config
, BGP_CONFIG_CONFEDERATION
))
889 attre
.aggregator_as
= bgp
->confed_id
;
891 attre
.aggregator_as
= bgp
->as
;
892 attre
.aggregator_addr
= bgp
->router_id
;
894 new = bgp_attr_intern (&attr
);
896 aspath_unintern (&new->aspath
);
900 /* Unintern just the sub-components of the attr, but not the attr */
902 bgp_attr_unintern_sub (struct attr
*attr
)
904 /* aspath refcount shoud be decrement. */
906 aspath_unintern (&attr
->aspath
);
907 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
));
910 community_unintern (&attr
->community
);
911 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
));
915 if (attr
->extra
->ecommunity
)
916 ecommunity_unintern (&attr
->extra
->ecommunity
);
917 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
));
919 if (attr
->extra
->cluster
)
920 cluster_unintern (attr
->extra
->cluster
);
921 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST
));
923 if (attr
->extra
->transit
)
924 transit_unintern (attr
->extra
->transit
);
928 /* Free bgp attribute and aspath. */
930 bgp_attr_unintern (struct attr
**pattr
)
932 struct attr
*attr
= *pattr
;
935 struct attr_extra tmp_extra
;
937 /* Decrement attribute reference. */
944 tmp
.extra
= &tmp_extra
;
945 memcpy (tmp
.extra
, attr
->extra
, sizeof (struct attr_extra
));
948 /* If reference becomes zero then free attribute object. */
949 if (attr
->refcnt
== 0)
951 ret
= hash_release (attrhash
, attr
);
952 assert (ret
!= NULL
);
953 bgp_attr_extra_free (attr
);
954 XFREE (MTYPE_ATTR
, attr
);
958 bgp_attr_unintern_sub (&tmp
);
962 bgp_attr_flush (struct attr
*attr
)
964 if (attr
->aspath
&& ! attr
->aspath
->refcnt
)
965 aspath_free (attr
->aspath
);
966 if (attr
->community
&& ! attr
->community
->refcnt
)
967 community_free (attr
->community
);
970 struct attr_extra
*attre
= attr
->extra
;
972 if (attre
->ecommunity
&& ! attre
->ecommunity
->refcnt
)
973 ecommunity_free (&attre
->ecommunity
);
974 if (attre
->cluster
&& ! attre
->cluster
->refcnt
)
975 cluster_free (attre
->cluster
);
976 if (attre
->transit
&& ! attre
->transit
->refcnt
)
977 transit_free (attre
->transit
);
978 encap_free(attre
->encap_subtlvs
);
979 attre
->encap_subtlvs
= NULL
;
981 encap_free(attre
->vnc_subtlvs
);
982 attre
->vnc_subtlvs
= NULL
;
987 /* Implement draft-scudder-idr-optional-transitive behaviour and
988 * avoid resetting sessions for malformed attributes which are
989 * are partial/optional and hence where the error likely was not
990 * introduced by the sending neighbour.
992 static bgp_attr_parse_ret_t
993 bgp_attr_malformed (struct bgp_attr_parser_args
*args
, u_char subcode
,
996 struct peer
*const peer
= args
->peer
;
997 const u_int8_t flags
= args
->flags
;
998 /* startp and length must be special-cased, as whether or not to
999 * send the attribute data with the NOTIFY depends on the error,
1000 * the caller therefore signals this with the seperate length argument
1002 u_char
*notify_datap
= (length
> 0 ? args
->startp
: NULL
);
1004 /* Only relax error handling for eBGP peers */
1005 if (peer
->sort
!= BGP_PEER_EBGP
)
1007 bgp_notify_send_with_data (peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1008 notify_datap
, length
);
1009 return BGP_ATTR_PARSE_ERROR
;
1013 /* Adjust the stream getp to the end of the attribute, in case we can
1014 * still proceed but the caller hasn't read all the attribute.
1016 stream_set_getp (BGP_INPUT (peer
),
1017 (args
->startp
- STREAM_DATA (BGP_INPUT (peer
)))
1020 switch (args
->type
) {
1021 /* where an attribute is relatively inconsequential, e.g. it does not
1022 * affect route selection, and can be safely ignored, then any such
1023 * attributes which are malformed should just be ignored and the route
1024 * processed as normal.
1026 case BGP_ATTR_AS4_AGGREGATOR
:
1027 case BGP_ATTR_AGGREGATOR
:
1028 case BGP_ATTR_ATOMIC_AGGREGATE
:
1029 return BGP_ATTR_PARSE_PROCEED
;
1031 /* Core attributes, particularly ones which may influence route
1032 * selection, should always cause session resets
1034 case BGP_ATTR_ORIGIN
:
1035 case BGP_ATTR_AS_PATH
:
1036 case BGP_ATTR_NEXT_HOP
:
1037 case BGP_ATTR_MULTI_EXIT_DISC
:
1038 case BGP_ATTR_LOCAL_PREF
:
1039 case BGP_ATTR_COMMUNITIES
:
1040 case BGP_ATTR_ORIGINATOR_ID
:
1041 case BGP_ATTR_CLUSTER_LIST
:
1042 case BGP_ATTR_MP_REACH_NLRI
:
1043 case BGP_ATTR_MP_UNREACH_NLRI
:
1044 case BGP_ATTR_EXT_COMMUNITIES
:
1045 bgp_notify_send_with_data (peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1046 notify_datap
, length
);
1047 return BGP_ATTR_PARSE_ERROR
;
1050 /* Partial optional attributes that are malformed should not cause
1051 * the whole session to be reset. Instead treat it as a withdrawal
1052 * of the routes, if possible.
1054 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
)
1055 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1056 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_PARTIAL
))
1057 return BGP_ATTR_PARSE_WITHDRAW
;
1059 /* default to reset */
1060 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1063 /* Find out what is wrong with the path attribute flag bits and log the error.
1064 "Flag bits" here stand for Optional, Transitive and Partial, but not for
1065 Extended Length. Checking O/T/P bits at once implies, that the attribute
1066 being diagnosed is defined by RFC as either a "well-known" or an "optional,
1067 non-transitive" attribute. */
1069 bgp_attr_flags_diagnose (struct bgp_attr_parser_args
*args
,
1070 u_int8_t desired_flags
/* how RFC says it must be */
1074 u_char real_flags
= args
->flags
;
1075 const u_int8_t attr_code
= args
->type
;
1077 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1078 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1079 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
1082 CHECK_FLAG (desired_flags
, attr_flag_str
[i
].key
) !=
1083 CHECK_FLAG (real_flags
, attr_flag_str
[i
].key
)
1086 zlog_err ("%s attribute must%s be flagged as \"%s\"",
1087 LOOKUP (attr_str
, attr_code
),
1088 CHECK_FLAG (desired_flags
, attr_flag_str
[i
].key
) ? "" : " not",
1089 attr_flag_str
[i
].str
);
1094 zlog_debug ("Strange, %s called for attr %s, but no problem found with flags"
1095 " (real flags 0x%x, desired 0x%x)",
1096 __func__
, LOOKUP (attr_str
, attr_code
),
1097 real_flags
, desired_flags
);
1101 /* Required flags for attributes. EXTLEN will be masked off when testing,
1102 * as will PARTIAL for optional+transitive attributes.
1104 const u_int8_t attr_flags_values
[] = {
1105 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1106 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1107 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1108 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1109 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1110 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1111 [BGP_ATTR_AGGREGATOR
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1112 [BGP_ATTR_COMMUNITIES
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1113 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1114 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1115 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1116 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1117 [BGP_ATTR_EXT_COMMUNITIES
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1118 [BGP_ATTR_AS4_PATH
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1119 [BGP_ATTR_AS4_AGGREGATOR
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1121 static const size_t attr_flags_values_max
=
1122 sizeof (attr_flags_values
) / sizeof (attr_flags_values
[0]);
1125 bgp_attr_flag_invalid (struct bgp_attr_parser_args
*args
)
1127 u_int8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1128 const u_int8_t flags
= args
->flags
;
1129 const u_int8_t attr_code
= args
->type
;
1131 /* there may be attributes we don't know about */
1132 if (attr_code
> attr_flags_values_max
)
1134 if (attr_flags_values
[attr_code
] == 0)
1137 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to
1140 if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL
, flags
)
1141 && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS
, flags
))
1143 zlog_err ("%s well-known attributes must have transitive flag set (%x)",
1144 LOOKUP (attr_str
, attr_code
), flags
);
1148 /* "For well-known attributes and for optional non-transitive attributes,
1149 * the Partial bit MUST be set to 0."
1151 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_PARTIAL
))
1153 if (!CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
))
1155 zlog_err ("%s well-known attribute "
1156 "must NOT have the partial flag set (%x)",
1157 LOOKUP (attr_str
, attr_code
), flags
);
1160 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1161 && !CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
))
1163 zlog_err ("%s optional + transitive attribute "
1164 "must NOT have the partial flag set (%x)",
1165 LOOKUP (attr_str
, attr_code
), flags
);
1170 /* Optional transitive attributes may go through speakers that don't
1171 * reocgnise them and set the Partial bit.
1173 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1174 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
))
1175 SET_FLAG (mask
, BGP_ATTR_FLAG_PARTIAL
);
1178 == attr_flags_values
[attr_code
])
1181 bgp_attr_flags_diagnose (args
, attr_flags_values
[attr_code
]);
1185 /* Get origin attribute of the update message. */
1186 static bgp_attr_parse_ret_t
1187 bgp_attr_origin (struct bgp_attr_parser_args
*args
)
1189 struct peer
*const peer
= args
->peer
;
1190 struct attr
*const attr
= args
->attr
;
1191 const bgp_size_t length
= args
->length
;
1193 /* If any recognized attribute has Attribute Length that conflicts
1194 with the expected length (based on the attribute type code), then
1195 the Error Subcode is set to Attribute Length Error. The Data
1196 field contains the erroneous attribute (type, length and
1200 zlog_err ("Origin attribute length is not one %d", length
);
1201 return bgp_attr_malformed (args
,
1202 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1206 /* Fetch origin attribute. */
1207 attr
->origin
= stream_getc (BGP_INPUT (peer
));
1209 /* If the ORIGIN attribute has an undefined value, then the Error
1210 Subcode is set to Invalid Origin Attribute. The Data field
1211 contains the unrecognized attribute (type, length and value). */
1212 if ((attr
->origin
!= BGP_ORIGIN_IGP
)
1213 && (attr
->origin
!= BGP_ORIGIN_EGP
)
1214 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
))
1216 zlog_err ("Origin attribute value is invalid %d", attr
->origin
);
1217 return bgp_attr_malformed (args
,
1218 BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1222 /* Set oring attribute flag. */
1223 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
1228 /* Parse AS path information. This function is wrapper of
1231 bgp_attr_aspath (struct bgp_attr_parser_args
*args
)
1233 struct attr
*const attr
= args
->attr
;
1234 struct peer
*const peer
= args
->peer
;
1235 const bgp_size_t length
= args
->length
;
1238 * peer with AS4 => will get 4Byte ASnums
1239 * otherwise, will get 16 Bit
1241 attr
->aspath
= aspath_parse (peer
->ibuf
, length
,
1242 CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
));
1244 /* In case of IBGP, length will be zero. */
1247 zlog_err ("Malformed AS path from %s, length is %d", peer
->host
, length
);
1248 return bgp_attr_malformed (args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
, 0);
1251 /* Set aspath attribute flag. */
1252 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
1254 return BGP_ATTR_PARSE_PROCEED
;
1257 static bgp_attr_parse_ret_t
1258 bgp_attr_aspath_check (struct peer
*const peer
, struct attr
*const attr
)
1260 /* These checks were part of bgp_attr_aspath, but with
1261 * as4 we should to check aspath things when
1262 * aspath synthesizing with as4_path has already taken place.
1263 * Otherwise we check ASPATH and use the synthesized thing, and that is
1265 * So do the checks later, i.e. here
1267 struct bgp
*bgp
= peer
->bgp
;
1268 struct aspath
*aspath
;
1270 /* Confederation sanity check. */
1271 if ((peer
->sort
== BGP_PEER_CONFED
&& ! aspath_left_confed_check (attr
->aspath
)) ||
1272 (peer
->sort
== BGP_PEER_EBGP
&& aspath_confed_check (attr
->aspath
)))
1274 zlog_err ("Malformed AS path from %s", peer
->host
);
1275 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1276 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1277 return BGP_ATTR_PARSE_ERROR
;
1280 /* First AS check for EBGP. */
1281 if (bgp
!= NULL
&& bgp_flag_check (bgp
, BGP_FLAG_ENFORCE_FIRST_AS
))
1283 if (peer
->sort
== BGP_PEER_EBGP
1284 && ! aspath_firstas_check (attr
->aspath
, peer
->as
))
1286 zlog_err ("%s incorrect first AS (must be %u)", peer
->host
, peer
->as
);
1287 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1288 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1289 return BGP_ATTR_PARSE_ERROR
;
1293 /* local-as prepend */
1294 if (peer
->change_local_as
&&
1295 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
))
1297 aspath
= aspath_dup (attr
->aspath
);
1298 aspath
= aspath_add_seq (aspath
, peer
->change_local_as
);
1299 aspath_unintern (&attr
->aspath
);
1300 attr
->aspath
= aspath_intern (aspath
);
1303 return BGP_ATTR_PARSE_PROCEED
;
1306 /* Parse AS4 path information. This function is another wrapper of
1309 bgp_attr_as4_path (struct bgp_attr_parser_args
*args
, struct aspath
**as4_path
)
1311 struct peer
*const peer
= args
->peer
;
1312 struct attr
*const attr
= args
->attr
;
1313 const bgp_size_t length
= args
->length
;
1315 *as4_path
= aspath_parse (peer
->ibuf
, length
, 1);
1317 /* In case of IBGP, length will be zero. */
1320 zlog_err ("Malformed AS4 path from %s, length is %d", peer
->host
, length
);
1321 return bgp_attr_malformed (args
,
1322 BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1326 /* Set aspath attribute flag. */
1328 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH
);
1330 return BGP_ATTR_PARSE_PROCEED
;
1333 /* Nexthop attribute. */
1334 static bgp_attr_parse_ret_t
1335 bgp_attr_nexthop (struct bgp_attr_parser_args
*args
)
1337 struct peer
*const peer
= args
->peer
;
1338 struct attr
*const attr
= args
->attr
;
1339 const bgp_size_t length
= args
->length
;
1341 in_addr_t nexthop_h
, nexthop_n
;
1343 /* Check nexthop attribute length. */
1346 zlog_err ("Nexthop attribute length isn't four [%d]", length
);
1348 return bgp_attr_malformed (args
,
1349 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1353 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1354 attribute must result in a NOTIFICATION message (this is implemented below).
1355 At the same time, semantically incorrect NEXT_HOP is more likely to be just
1356 logged locally (this is implemented somewhere else). The UPDATE message
1357 gets ignored in any of these cases. */
1358 nexthop_n
= stream_get_ipv4 (peer
->ibuf
);
1359 nexthop_h
= ntohl (nexthop_n
);
1360 if ((IPV4_NET0 (nexthop_h
) || IPV4_NET127 (nexthop_h
) || IPV4_CLASS_DE (nexthop_h
))
1361 && !BGP_DEBUG (allow_martians
, ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1363 char buf
[INET_ADDRSTRLEN
];
1364 inet_ntop (AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1365 zlog_err ("Martian nexthop %s", buf
);
1366 return bgp_attr_malformed (args
,
1367 BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
,
1371 attr
->nexthop
.s_addr
= nexthop_n
;
1372 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
1374 return BGP_ATTR_PARSE_PROCEED
;
1377 /* MED atrribute. */
1378 static bgp_attr_parse_ret_t
1379 bgp_attr_med (struct bgp_attr_parser_args
*args
)
1381 struct peer
*const peer
= args
->peer
;
1382 struct attr
*const attr
= args
->attr
;
1383 const bgp_size_t length
= args
->length
;
1388 zlog_err ("MED attribute length isn't four [%d]", length
);
1390 return bgp_attr_malformed (args
,
1391 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1395 attr
->med
= stream_getl (peer
->ibuf
);
1397 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
);
1399 return BGP_ATTR_PARSE_PROCEED
;
1402 /* Local preference attribute. */
1403 static bgp_attr_parse_ret_t
1404 bgp_attr_local_pref (struct bgp_attr_parser_args
*args
)
1406 struct peer
*const peer
= args
->peer
;
1407 struct attr
*const attr
= args
->attr
;
1408 const bgp_size_t length
= args
->length
;
1413 zlog_err ("LOCAL_PREF attribute length isn't 4 [%u]", length
);
1414 return bgp_attr_malformed (args
,
1415 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1419 /* If it is contained in an UPDATE message that is received from an
1420 external peer, then this attribute MUST be ignored by the
1421 receiving speaker. */
1422 if (peer
->sort
== BGP_PEER_EBGP
)
1424 stream_forward_getp (peer
->ibuf
, length
);
1425 return BGP_ATTR_PARSE_PROCEED
;
1428 attr
->local_pref
= stream_getl (peer
->ibuf
);
1430 /* Set atomic aggregate flag. */
1431 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
);
1433 return BGP_ATTR_PARSE_PROCEED
;
1436 /* Atomic aggregate. */
1438 bgp_attr_atomic (struct bgp_attr_parser_args
*args
)
1440 struct attr
*const attr
= args
->attr
;
1441 const bgp_size_t length
= args
->length
;
1446 zlog_err ("ATOMIC_AGGREGATE attribute length isn't 0 [%u]", length
);
1447 return bgp_attr_malformed (args
,
1448 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1452 /* Set atomic aggregate flag. */
1453 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
);
1455 return BGP_ATTR_PARSE_PROCEED
;
1458 /* Aggregator attribute */
1460 bgp_attr_aggregator (struct bgp_attr_parser_args
*args
)
1462 struct peer
*const peer
= args
->peer
;
1463 struct attr
*const attr
= args
->attr
;
1464 const bgp_size_t length
= args
->length
;
1467 struct attr_extra
*attre
= bgp_attr_extra_get (attr
);
1469 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1470 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
))
1473 if (length
!= wantedlen
)
1475 zlog_err ("AGGREGATOR attribute length isn't %u [%u]", wantedlen
, length
);
1476 return bgp_attr_malformed (args
,
1477 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1481 if ( CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) )
1482 attre
->aggregator_as
= stream_getl (peer
->ibuf
);
1484 attre
->aggregator_as
= stream_getw (peer
->ibuf
);
1485 attre
->aggregator_addr
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1487 /* Set atomic aggregate flag. */
1488 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
);
1490 return BGP_ATTR_PARSE_PROCEED
;
1493 /* New Aggregator attribute */
1494 static bgp_attr_parse_ret_t
1495 bgp_attr_as4_aggregator (struct bgp_attr_parser_args
*args
,
1496 as_t
*as4_aggregator_as
,
1497 struct in_addr
*as4_aggregator_addr
)
1499 struct peer
*const peer
= args
->peer
;
1500 struct attr
*const attr
= args
->attr
;
1501 const bgp_size_t length
= args
->length
;
1505 zlog_err ("New Aggregator length is not 8 [%d]", length
);
1506 return bgp_attr_malformed (args
,
1507 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1511 *as4_aggregator_as
= stream_getl (peer
->ibuf
);
1512 as4_aggregator_addr
->s_addr
= stream_get_ipv4 (peer
->ibuf
);
1514 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR
);
1516 return BGP_ATTR_PARSE_PROCEED
;
1519 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1521 static bgp_attr_parse_ret_t
1522 bgp_attr_munge_as4_attrs (struct peer
*const peer
,
1523 struct attr
*const attr
,
1524 struct aspath
*as4_path
, as_t as4_aggregator
,
1525 struct in_addr
*as4_aggregator_addr
)
1527 int ignore_as4_path
= 0;
1528 struct aspath
*newpath
;
1529 struct attr_extra
*attre
= attr
->extra
;
1533 /* NULL aspath shouldn't be possible as bgp_attr_parse should have
1534 * checked that all well-known, mandatory attributes were present.
1536 * Can only be a problem with peer itself - hard error
1538 return BGP_ATTR_PARSE_ERROR
;
1541 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
))
1543 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1545 * It is worth a warning though, because the peer really
1546 * should not send them
1548 if (BGP_DEBUG(as4
, AS4
))
1550 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1551 zlog_debug ("[AS4] %s %s AS4_PATH",
1552 peer
->host
, "AS4 capable peer, yet it sent");
1554 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1555 zlog_debug ("[AS4] %s %s AS4_AGGREGATOR",
1556 peer
->host
, "AS4 capable peer, yet it sent");
1559 return BGP_ATTR_PARSE_PROCEED
;
1562 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1563 * because that may override AS4_PATH
1565 if (attr
->flag
& (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR
) ) )
1567 if (attr
->flag
& (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
) ) )
1572 * if the as_number in aggregator is not AS_TRANS,
1573 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1574 * and the Aggregator shall be taken as
1575 * info on the aggregating node, and the AS_PATH
1576 * shall be taken as the AS_PATH
1578 * the Aggregator shall be ignored and the
1579 * AS4_AGGREGATOR shall be taken as the
1580 * Aggregating node and the AS_PATH is to be
1581 * constructed "as in all other cases"
1583 if (attre
->aggregator_as
!= BGP_AS_TRANS
)
1586 if ( BGP_DEBUG(as4
, AS4
))
1587 zlog_debug ("[AS4] %s BGP not AS4 capable peer"
1588 " send AGGREGATOR != AS_TRANS and"
1589 " AS4_AGGREGATOR, so ignore"
1590 " AS4_AGGREGATOR and AS4_PATH", peer
->host
);
1591 ignore_as4_path
= 1;
1595 /* "New_aggregator shall be taken as aggregator" */
1596 attre
->aggregator_as
= as4_aggregator
;
1597 attre
->aggregator_addr
.s_addr
= as4_aggregator_addr
->s_addr
;
1602 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1603 * That is bogus - but reading the conditions
1604 * we have to handle AS4_AGGREGATOR as if it were
1605 * AGGREGATOR in that case
1607 if ( BGP_DEBUG(as4
, AS4
))
1608 zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
1609 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1610 " it as if AGGREGATOR with AS_TRANS had been there", peer
->host
);
1611 (attre
= bgp_attr_extra_get (attr
))->aggregator_as
= as4_aggregator
;
1612 /* sweep it under the carpet and simulate a "good" AGGREGATOR */
1613 attr
->flag
|= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
));
1617 /* need to reconcile NEW_AS_PATH and AS_PATH */
1618 if (!ignore_as4_path
&& (attr
->flag
& (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH
))))
1620 newpath
= aspath_reconcile_as4 (attr
->aspath
, as4_path
);
1621 aspath_unintern (&attr
->aspath
);
1622 attr
->aspath
= aspath_intern (newpath
);
1624 return BGP_ATTR_PARSE_PROCEED
;
1627 /* Community attribute. */
1628 static bgp_attr_parse_ret_t
1629 bgp_attr_community (struct bgp_attr_parser_args
*args
)
1631 struct peer
*const peer
= args
->peer
;
1632 struct attr
*const attr
= args
->attr
;
1633 const bgp_size_t length
= args
->length
;
1637 attr
->community
= NULL
;
1638 return BGP_ATTR_PARSE_PROCEED
;
1642 community_parse ((u_int32_t
*)stream_pnt (peer
->ibuf
), length
);
1644 /* XXX: fix community_parse to use stream API and remove this */
1645 stream_forward_getp (peer
->ibuf
, length
);
1647 if (!attr
->community
)
1648 return bgp_attr_malformed (args
,
1649 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1652 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
);
1654 return BGP_ATTR_PARSE_PROCEED
;
1657 /* Originator ID attribute. */
1658 static bgp_attr_parse_ret_t
1659 bgp_attr_originator_id (struct bgp_attr_parser_args
*args
)
1661 struct peer
*const peer
= args
->peer
;
1662 struct attr
*const attr
= args
->attr
;
1663 const bgp_size_t length
= args
->length
;
1668 zlog_err ("Bad originator ID length %d", length
);
1670 return bgp_attr_malformed (args
,
1671 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1675 (bgp_attr_extra_get (attr
))->originator_id
.s_addr
1676 = stream_get_ipv4 (peer
->ibuf
);
1678 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID
);
1680 return BGP_ATTR_PARSE_PROCEED
;
1683 /* Cluster list attribute. */
1684 static bgp_attr_parse_ret_t
1685 bgp_attr_cluster_list (struct bgp_attr_parser_args
*args
)
1687 struct peer
*const peer
= args
->peer
;
1688 struct attr
*const attr
= args
->attr
;
1689 const bgp_size_t length
= args
->length
;
1694 zlog_err ("Bad cluster list length %d", length
);
1696 return bgp_attr_malformed (args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1700 (bgp_attr_extra_get (attr
))->cluster
1701 = cluster_parse ((struct in_addr
*)stream_pnt (peer
->ibuf
), length
);
1703 /* XXX: Fix cluster_parse to use stream API and then remove this */
1704 stream_forward_getp (peer
->ibuf
, length
);
1706 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST
);
1708 return BGP_ATTR_PARSE_PROCEED
;
1711 /* Multiprotocol reachability information parse. */
1713 bgp_mp_reach_parse (struct bgp_attr_parser_args
*args
,
1714 struct bgp_nlri
*mp_update
)
1718 bgp_size_t nlri_len
;
1723 struct peer
*const peer
= args
->peer
;
1724 struct attr
*const attr
= args
->attr
;
1725 const bgp_size_t length
= args
->length
;
1726 struct attr_extra
*attre
= bgp_attr_extra_get(attr
);
1728 /* Set end of packet. */
1729 s
= BGP_INPUT(peer
);
1730 start
= stream_get_getp(s
);
1732 /* safe to read statically sized header? */
1733 #define BGP_MP_REACH_MIN_SIZE 5
1734 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1735 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
))
1737 zlog_info ("%s: %s sent invalid length, %lu",
1738 __func__
, peer
->host
, (unsigned long)length
);
1739 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1742 /* Load AFI, SAFI. */
1743 afi
= stream_getw (s
);
1744 safi
= stream_getc (s
);
1746 /* Get nexthop length. */
1747 attre
->mp_nexthop_len
= stream_getc (s
);
1749 if (LEN_LEFT
< attre
->mp_nexthop_len
)
1751 zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute",
1752 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1753 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1756 /* Nexthop length check. */
1757 switch (attre
->mp_nexthop_len
)
1759 case BGP_ATTR_NHLEN_IPV4
:
1760 stream_get (&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1761 /* Probably needed for RFC 2283 */
1762 if (attr
->nexthop
.s_addr
== 0)
1763 memcpy(&attr
->nexthop
.s_addr
, &attre
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1765 case BGP_ATTR_NHLEN_VPNV4
:
1766 stream_getl (s
); /* RD high */
1767 stream_getl (s
); /* RD low */
1768 stream_get (&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1771 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1772 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1773 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
)
1775 stream_getl (s
); /* RD high */
1776 stream_getl (s
); /* RD low */
1778 stream_get (&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1780 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1781 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1782 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
1784 stream_getl (s
); /* RD high */
1785 stream_getl (s
); /* RD low */
1787 stream_get (&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1788 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
1790 stream_getl (s
); /* RD high */
1791 stream_getl (s
); /* RD low */
1793 stream_get (&attre
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1794 if (! IN6_IS_ADDR_LINKLOCAL (&attre
->mp_nexthop_local
))
1796 char buf1
[INET6_ADDRSTRLEN
];
1797 char buf2
[INET6_ADDRSTRLEN
];
1799 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1800 zlog_debug ("%s rcvd nexthops %s, %s -- ignoring non-LL value",
1802 inet_ntop (AF_INET6
, &attre
->mp_nexthop_global
,
1803 buf1
, INET6_ADDRSTRLEN
),
1804 inet_ntop (AF_INET6
, &attre
->mp_nexthop_local
,
1805 buf2
, INET6_ADDRSTRLEN
));
1807 attre
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1810 #endif /* HAVE_IPV6 */
1812 zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d",
1813 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1814 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1819 zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
1820 __func__
, peer
->host
);
1821 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1826 if ((val
= stream_getc (s
)))
1827 zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field",
1831 /* must have nrli_len, what is left of the attribute */
1832 nlri_len
= LEN_LEFT
;
1833 if ((!nlri_len
) || (nlri_len
> STREAM_READABLE(s
)))
1835 zlog_info ("%s: (%s) Failed to read NLRI",
1836 __func__
, peer
->host
);
1837 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1840 if (safi
!= SAFI_MPLS_LABELED_VPN
)
1842 ret
= bgp_nlri_sanity_check (peer
, afi
, safi
, stream_pnt (s
),
1843 nlri_len
, &num_mp_pfx
);
1846 zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
1847 __func__
, peer
->host
);
1848 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1852 mp_update
->afi
= afi
;
1853 mp_update
->safi
= safi
;
1854 mp_update
->nlri
= stream_pnt (s
);
1855 mp_update
->length
= nlri_len
;
1857 stream_forward_getp (s
, nlri_len
);
1859 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI
);
1861 return BGP_ATTR_PARSE_PROCEED
;
1865 /* Multiprotocol unreachable parse */
1867 bgp_mp_unreach_parse (struct bgp_attr_parser_args
*args
,
1868 struct bgp_nlri
*mp_withdraw
)
1873 u_int16_t withdraw_len
;
1876 struct peer
*const peer
= args
->peer
;
1877 struct attr
*const attr
= args
->attr
;
1878 const bgp_size_t length
= args
->length
;
1882 #define BGP_MP_UNREACH_MIN_SIZE 3
1883 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1884 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1886 afi
= stream_getw (s
);
1887 safi
= stream_getc (s
);
1889 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1891 if (safi
!= SAFI_MPLS_LABELED_VPN
)
1893 ret
= bgp_nlri_sanity_check (peer
, afi
, safi
, stream_pnt (s
),
1894 withdraw_len
, &num_mp_pfx
);
1896 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1899 mp_withdraw
->afi
= afi
;
1900 mp_withdraw
->safi
= safi
;
1901 mp_withdraw
->nlri
= stream_pnt (s
);
1902 mp_withdraw
->length
= withdraw_len
;
1904 stream_forward_getp (s
, withdraw_len
);
1906 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
);
1908 return BGP_ATTR_PARSE_PROCEED
;
1911 /* Extended Community attribute. */
1912 static bgp_attr_parse_ret_t
1913 bgp_attr_ext_communities (struct bgp_attr_parser_args
*args
)
1915 struct peer
*const peer
= args
->peer
;
1916 struct attr
*const attr
= args
->attr
;
1917 const bgp_size_t length
= args
->length
;
1922 attr
->extra
->ecommunity
= NULL
;
1923 /* Empty extcomm doesn't seem to be invalid per se */
1924 return BGP_ATTR_PARSE_PROCEED
;
1927 (bgp_attr_extra_get (attr
))->ecommunity
=
1928 ecommunity_parse ((u_int8_t
*)stream_pnt (peer
->ibuf
), length
);
1929 /* XXX: fix ecommunity_parse to use stream API */
1930 stream_forward_getp (peer
->ibuf
, length
);
1932 if (!attr
->extra
->ecommunity
)
1933 return bgp_attr_malformed (args
,
1934 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1937 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
);
1939 return BGP_ATTR_PARSE_PROCEED
;
1942 /* Parse Tunnel Encap attribute in an UPDATE */
1946 struct peer
*peer
, /* IN */
1947 bgp_size_t length
, /* IN: attr's length field */
1948 struct attr
*attr
, /* IN: caller already allocated */
1949 u_char flag
, /* IN: attr's flags field */
1953 struct attr_extra
*attre
= NULL
;
1954 struct bgp_attr_encap_subtlv
*stlv_last
= NULL
;
1955 uint16_t tunneltype
;
1957 total
= length
+ (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1959 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1960 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
))
1962 zlog_info ("Tunnel Encap attribute flag isn't optional and transitive %d", flag
);
1963 bgp_notify_send_with_data (peer
,
1964 BGP_NOTIFY_UPDATE_ERR
,
1965 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1970 if (BGP_ATTR_ENCAP
== type
) {
1971 /* read outer TLV type and length */
1972 uint16_t tlv_length
;
1975 zlog_info ("Tunnel Encap attribute not long enough to contain outer T,L");
1976 bgp_notify_send_with_data(peer
,
1977 BGP_NOTIFY_UPDATE_ERR
,
1978 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1982 tunneltype
= stream_getw (BGP_INPUT (peer
));
1983 tlv_length
= stream_getw (BGP_INPUT (peer
));
1986 if (tlv_length
!= length
) {
1987 zlog_info ("%s: tlv_length(%d) != length(%d)",
1988 __func__
, tlv_length
, length
);
1992 while (length
>= 4) {
1993 uint16_t subtype
= 0;
1994 uint16_t sublength
= 0;
1995 struct bgp_attr_encap_subtlv
*tlv
;
1997 if (BGP_ATTR_ENCAP
== type
) {
1998 subtype
= stream_getc (BGP_INPUT (peer
));
1999 sublength
= stream_getc (BGP_INPUT (peer
));
2003 subtype
= stream_getw (BGP_INPUT (peer
));
2004 sublength
= stream_getw (BGP_INPUT (peer
));
2009 if (sublength
> length
) {
2010 zlog_info ("Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
2012 bgp_notify_send_with_data (peer
,
2013 BGP_NOTIFY_UPDATE_ERR
,
2014 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2019 /* alloc and copy sub-tlv */
2020 /* TBD make sure these are freed when attributes are released */
2021 tlv
= XCALLOC (MTYPE_ENCAP_TLV
, sizeof(struct bgp_attr_encap_subtlv
)-1+sublength
);
2022 tlv
->type
= subtype
;
2023 tlv
->length
= sublength
;
2024 stream_get(tlv
->value
, peer
->ibuf
, sublength
);
2025 length
-= sublength
;
2027 /* attach tlv to encap chain */
2029 attre
= bgp_attr_extra_get(attr
);
2030 if (BGP_ATTR_ENCAP
== type
) {
2031 for (stlv_last
= attre
->encap_subtlvs
; stlv_last
&& stlv_last
->next
;
2032 stlv_last
= stlv_last
->next
);
2034 stlv_last
->next
= tlv
;
2036 attre
->encap_subtlvs
= tlv
;
2040 for (stlv_last
= attre
->vnc_subtlvs
; stlv_last
&& stlv_last
->next
;
2041 stlv_last
= stlv_last
->next
);
2043 stlv_last
->next
= tlv
;
2045 attre
->vnc_subtlvs
= tlv
;
2050 stlv_last
->next
= tlv
;
2055 if (attre
&& (BGP_ATTR_ENCAP
== type
)) {
2056 attre
->encap_tunneltype
= tunneltype
;
2060 /* spurious leftover data */
2061 zlog_info ("Tunnel Encap attribute length is bad: %d leftover octets", length
);
2062 bgp_notify_send_with_data (peer
,
2063 BGP_NOTIFY_UPDATE_ERR
,
2064 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2072 /* BGP unknown attribute treatment. */
2073 static bgp_attr_parse_ret_t
2074 bgp_attr_unknown (struct bgp_attr_parser_args
*args
)
2076 bgp_size_t total
= args
->total
;
2077 struct transit
*transit
;
2078 struct attr_extra
*attre
;
2079 struct peer
*const peer
= args
->peer
;
2080 struct attr
*const attr
= args
->attr
;
2081 u_char
*const startp
= args
->startp
;
2082 const u_char type
= args
->type
;
2083 const u_char flag
= args
->flags
;
2084 const bgp_size_t length
= args
->length
;
2086 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2087 zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
2088 peer
->host
, type
, length
);
2090 /* Forward read pointer of input stream. */
2091 stream_forward_getp (peer
->ibuf
, length
);
2093 /* If any of the mandatory well-known attributes are not recognized,
2094 then the Error Subcode is set to Unrecognized Well-known
2095 Attribute. The Data field contains the unrecognized attribute
2096 (type, length and value). */
2097 if (!CHECK_FLAG (flag
, BGP_ATTR_FLAG_OPTIONAL
))
2099 return bgp_attr_malformed (args
,
2100 BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2104 /* Unrecognized non-transitive optional attributes must be quietly
2105 ignored and not passed along to other BGP peers. */
2106 if (! CHECK_FLAG (flag
, BGP_ATTR_FLAG_TRANS
))
2107 return BGP_ATTR_PARSE_PROCEED
;
2109 /* If a path with recognized transitive optional attribute is
2110 accepted and passed along to other BGP peers and the Partial bit
2111 in the Attribute Flags octet is set to 1 by some previous AS, it
2112 is not set back to 0 by the current AS. */
2113 SET_FLAG (*startp
, BGP_ATTR_FLAG_PARTIAL
);
2115 /* Store transitive attribute to the end of attr->transit. */
2116 if (! ((attre
= bgp_attr_extra_get(attr
))->transit
) )
2117 attre
->transit
= XCALLOC (MTYPE_TRANSIT
, sizeof (struct transit
));
2119 transit
= attre
->transit
;
2122 transit
->val
= XREALLOC (MTYPE_TRANSIT_VAL
, transit
->val
,
2123 transit
->length
+ total
);
2125 transit
->val
= XMALLOC (MTYPE_TRANSIT_VAL
, total
);
2127 memcpy (transit
->val
+ transit
->length
, startp
, total
);
2128 transit
->length
+= total
;
2130 return BGP_ATTR_PARSE_PROCEED
;
2133 /* Well-known attribute check. */
2135 bgp_attr_check (struct peer
*peer
, struct attr
*attr
)
2139 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2141 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2142 return BGP_ATTR_PARSE_PROCEED
;
2144 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2145 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2146 are present, it should. Check for any other attribute being present
2149 if (attr
->flag
== ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
))
2150 return BGP_ATTR_PARSE_PROCEED
;
2152 if (! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
)))
2153 type
= BGP_ATTR_ORIGIN
;
2155 if (! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
)))
2156 type
= BGP_ATTR_AS_PATH
;
2158 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present and
2159 * NLRI is empty. We can't easily check NLRI empty here though.
2161 if (!CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
))
2162 && !CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI
)))
2163 type
= BGP_ATTR_NEXT_HOP
;
2165 if (peer
->sort
== BGP_PEER_IBGP
2166 && ! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
)))
2167 type
= BGP_ATTR_LOCAL_PREF
;
2171 zlog_warn ("%s Missing well-known attribute %s.", peer
->host
,
2172 LOOKUP (attr_str
, type
));
2173 bgp_notify_send_with_data (peer
,
2174 BGP_NOTIFY_UPDATE_ERR
,
2175 BGP_NOTIFY_UPDATE_MISS_ATTR
,
2177 return BGP_ATTR_PARSE_ERROR
;
2179 return BGP_ATTR_PARSE_PROCEED
;
2182 /* Read attribute of update packet. This function is called from
2183 bgp_update_receive() in bgp_packet.c. */
2184 bgp_attr_parse_ret_t
2185 bgp_attr_parse (struct peer
*peer
, struct attr
*attr
, bgp_size_t size
,
2186 struct bgp_nlri
*mp_update
, struct bgp_nlri
*mp_withdraw
)
2192 u_char
*startp
, *endp
;
2194 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2195 /* we need the as4_path only until we have synthesized the as_path with it */
2196 /* same goes for as4_aggregator */
2197 struct aspath
*as4_path
= NULL
;
2198 as_t as4_aggregator
= 0;
2199 struct in_addr as4_aggregator_addr
= { .s_addr
= 0 };
2201 /* Initialize bitmap. */
2202 memset (seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2204 /* End pointer of BGP attribute. */
2205 endp
= BGP_INPUT_PNT (peer
) + size
;
2207 /* Get attributes to the end of attribute length. */
2208 while (BGP_INPUT_PNT (peer
) < endp
)
2210 /* Check remaining length check.*/
2211 if (endp
- BGP_INPUT_PNT (peer
) < BGP_ATTR_MIN_LEN
)
2213 /* XXX warning: long int format, int arg (arg 5) */
2214 zlog_warn ("%s: error BGP attribute length %lu is smaller than min len",
2216 (unsigned long) (endp
- STREAM_PNT (BGP_INPUT (peer
))));
2218 bgp_notify_send (peer
,
2219 BGP_NOTIFY_UPDATE_ERR
,
2220 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2221 return BGP_ATTR_PARSE_ERROR
;
2224 /* Fetch attribute flag and type. */
2225 startp
= BGP_INPUT_PNT (peer
);
2226 /* "The lower-order four bits of the Attribute Flags octet are
2227 unused. They MUST be zero when sent and MUST be ignored when
2229 flag
= 0xF0 & stream_getc (BGP_INPUT (peer
));
2230 type
= stream_getc (BGP_INPUT (peer
));
2232 /* Check whether Extended-Length applies and is in bounds */
2233 if (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
)
2234 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1)))
2236 zlog_warn ("%s: Extended length set, but just %lu bytes of attr header",
2238 (unsigned long) (endp
- STREAM_PNT (BGP_INPUT (peer
))));
2240 bgp_notify_send (peer
,
2241 BGP_NOTIFY_UPDATE_ERR
,
2242 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2243 return BGP_ATTR_PARSE_ERROR
;
2246 /* Check extended attribue length bit. */
2247 if (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
))
2248 length
= stream_getw (BGP_INPUT (peer
));
2250 length
= stream_getc (BGP_INPUT (peer
));
2252 /* If any attribute appears more than once in the UPDATE
2253 message, then the Error Subcode is set to Malformed Attribute
2256 if (CHECK_BITMAP (seen
, type
))
2258 zlog_warn ("%s: error BGP attribute type %d appears twice in a message",
2261 bgp_notify_send (peer
,
2262 BGP_NOTIFY_UPDATE_ERR
,
2263 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2264 return BGP_ATTR_PARSE_ERROR
;
2267 /* Set type to bitmap to check duplicate attribute. `type' is
2268 unsigned char so it never overflow bitmap range. */
2270 SET_BITMAP (seen
, type
);
2272 /* Overflow check. */
2273 attr_endp
= BGP_INPUT_PNT (peer
) + length
;
2275 if (attr_endp
> endp
)
2277 zlog_warn ("%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p", peer
->host
, type
, length
, size
, attr_endp
, endp
);
2278 bgp_notify_send_with_data (peer
,
2279 BGP_NOTIFY_UPDATE_ERR
,
2280 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2281 startp
, attr_endp
- startp
);
2282 return BGP_ATTR_PARSE_ERROR
;
2285 struct bgp_attr_parser_args attr_args
= {
2292 .total
= attr_endp
- startp
,
2296 /* If any recognized attribute has Attribute Flags that conflict
2297 with the Attribute Type Code, then the Error Subcode is set to
2298 Attribute Flags Error. The Data field contains the erroneous
2299 attribute (type, length and value). */
2300 if (bgp_attr_flag_invalid (&attr_args
))
2302 bgp_attr_parse_ret_t ret
;
2303 ret
= bgp_attr_malformed (&attr_args
,
2304 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2306 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2311 /* OK check attribute and store it's value. */
2314 case BGP_ATTR_ORIGIN
:
2315 ret
= bgp_attr_origin (&attr_args
);
2317 case BGP_ATTR_AS_PATH
:
2318 ret
= bgp_attr_aspath (&attr_args
);
2320 case BGP_ATTR_AS4_PATH
:
2321 ret
= bgp_attr_as4_path (&attr_args
, &as4_path
);
2323 case BGP_ATTR_NEXT_HOP
:
2324 ret
= bgp_attr_nexthop (&attr_args
);
2326 case BGP_ATTR_MULTI_EXIT_DISC
:
2327 ret
= bgp_attr_med (&attr_args
);
2329 case BGP_ATTR_LOCAL_PREF
:
2330 ret
= bgp_attr_local_pref (&attr_args
);
2332 case BGP_ATTR_ATOMIC_AGGREGATE
:
2333 ret
= bgp_attr_atomic (&attr_args
);
2335 case BGP_ATTR_AGGREGATOR
:
2336 ret
= bgp_attr_aggregator (&attr_args
);
2338 case BGP_ATTR_AS4_AGGREGATOR
:
2339 ret
= bgp_attr_as4_aggregator (&attr_args
,
2341 &as4_aggregator_addr
);
2343 case BGP_ATTR_COMMUNITIES
:
2344 ret
= bgp_attr_community (&attr_args
);
2346 case BGP_ATTR_ORIGINATOR_ID
:
2347 ret
= bgp_attr_originator_id (&attr_args
);
2349 case BGP_ATTR_CLUSTER_LIST
:
2350 ret
= bgp_attr_cluster_list (&attr_args
);
2352 case BGP_ATTR_MP_REACH_NLRI
:
2353 ret
= bgp_mp_reach_parse (&attr_args
, mp_update
);
2355 case BGP_ATTR_MP_UNREACH_NLRI
:
2356 ret
= bgp_mp_unreach_parse (&attr_args
, mp_withdraw
);
2358 case BGP_ATTR_EXT_COMMUNITIES
:
2359 ret
= bgp_attr_ext_communities (&attr_args
);
2364 case BGP_ATTR_ENCAP
:
2365 ret
= bgp_attr_encap (type
, peer
, length
, attr
, flag
, startp
);
2368 ret
= bgp_attr_unknown (&attr_args
);
2372 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
)
2374 bgp_notify_send (peer
,
2375 BGP_NOTIFY_UPDATE_ERR
,
2376 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2377 ret
= BGP_ATTR_PARSE_ERROR
;
2380 /* If hard error occured immediately return to the caller. */
2381 if (ret
== BGP_ATTR_PARSE_ERROR
)
2383 zlog_warn ("%s: Attribute %s, parse error",
2385 LOOKUP (attr_str
, type
));
2387 aspath_unintern (&as4_path
);
2390 if (ret
== BGP_ATTR_PARSE_WITHDRAW
)
2393 zlog_warn ("%s: Attribute %s, parse error - treating as withdrawal",
2395 LOOKUP (attr_str
, type
));
2397 aspath_unintern (&as4_path
);
2401 /* Check the fetched length. */
2402 if (BGP_INPUT_PNT (peer
) != attr_endp
)
2404 zlog_warn ("%s: BGP attribute %s, fetch error",
2405 peer
->host
, LOOKUP (attr_str
, type
));
2406 bgp_notify_send (peer
,
2407 BGP_NOTIFY_UPDATE_ERR
,
2408 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2410 aspath_unintern (&as4_path
);
2411 return BGP_ATTR_PARSE_ERROR
;
2415 /* Check final read pointer is same as end pointer. */
2416 if (BGP_INPUT_PNT (peer
) != endp
)
2418 zlog_warn ("%s: BGP attribute %s, length mismatch",
2419 peer
->host
, LOOKUP (attr_str
, type
));
2420 bgp_notify_send (peer
,
2421 BGP_NOTIFY_UPDATE_ERR
,
2422 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2424 aspath_unintern (&as4_path
);
2425 return BGP_ATTR_PARSE_ERROR
;
2428 /* Check all mandatory well-known attributes are present */
2430 bgp_attr_parse_ret_t ret
;
2431 if ((ret
= bgp_attr_check (peer
, attr
)) < 0)
2434 aspath_unintern (&as4_path
);
2440 * At this place we can see whether we got AS4_PATH and/or
2441 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2442 * We can not do this before we've read all attributes because
2443 * the as4 handling does not say whether AS4_PATH has to be sent
2444 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2445 * in relationship to AGGREGATOR.
2446 * So, to be defensive, we are not relying on any order and read
2447 * all attributes first, including these 32bit ones, and now,
2448 * afterwards, we look what and if something is to be done for as4.
2450 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2453 /* actually... this doesn't ever return failure currently, but
2454 * better safe than sorry */
2455 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
))
2456 && bgp_attr_munge_as4_attrs (peer
, attr
, as4_path
,
2457 as4_aggregator
, &as4_aggregator_addr
))
2459 bgp_notify_send (peer
,
2460 BGP_NOTIFY_UPDATE_ERR
,
2461 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2463 aspath_unintern (&as4_path
);
2464 return BGP_ATTR_PARSE_ERROR
;
2467 /* At this stage, we have done all fiddling with as4, and the
2468 * resulting info is in attr->aggregator resp. attr->aspath
2469 * so we can chuck as4_aggregator and as4_path alltogether in
2470 * order to save memory
2474 aspath_unintern (&as4_path
); /* unintern - it is in the hash */
2475 /* The flag that we got this is still there, but that does not
2480 * The "rest" of the code does nothing with as4_aggregator.
2481 * there is no memory attached specifically which is not part
2483 * so ignoring just means do nothing.
2486 * Finally do the checks on the aspath we did not do yet
2487 * because we waited for a potentially synthesized aspath.
2489 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2491 ret
= bgp_attr_aspath_check (peer
, attr
);
2492 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2496 /* Finally intern unknown attribute. */
2497 if (attr
->extra
&& attr
->extra
->transit
)
2498 attr
->extra
->transit
= transit_intern (attr
->extra
->transit
);
2500 return BGP_ATTR_PARSE_PROCEED
;
2504 bgp_packet_mpattr_start (struct stream
*s
, afi_t afi
, safi_t safi
, afi_t nh_afi
,
2505 struct bpacket_attr_vec_arr
*vecarr
,
2510 /* Set extended bit always to encode the attribute length as 2 bytes */
2511 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
2512 stream_putc (s
, BGP_ATTR_MP_REACH_NLRI
);
2513 sizep
= stream_get_endp (s
);
2514 stream_putw (s
, 0); /* Marker: Attribute length. */
2516 stream_putw (s
, afi
);
2517 stream_putc (s
, (safi
== SAFI_MPLS_VPN
) ? SAFI_MPLS_LABELED_VPN
: safi
);
2526 case SAFI_MULTICAST
:
2527 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2529 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
2532 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2533 stream_putc (s
, 12);
2534 stream_putl (s
, 0); /* RD = 0, per RFC */
2536 stream_put (s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2540 stream_put (s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2551 case SAFI_MULTICAST
:
2553 struct attr_extra
*attre
= attr
->extra
;
2555 assert (attr
->extra
);
2556 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2557 stream_putc (s
, attre
->mp_nexthop_len
);
2558 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2559 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
2560 stream_put (s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
2565 struct attr_extra
*attre
= attr
->extra
;
2567 assert (attr
->extra
);
2568 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2569 stream_putc (s
, 24);
2570 stream_putl (s
, 0); /* RD = 0, per RFC */
2572 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2573 } else if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2574 stream_putc (s
, 48);
2575 stream_putl (s
, 0); /* RD = 0, per RFC */
2577 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2578 stream_putl (s
, 0); /* RD = 0, per RFC */
2580 stream_put (s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
2585 assert (attr
->extra
);
2586 stream_putc (s
, IPV6_MAX_BYTELEN
);
2587 stream_put (s
, &attr
->extra
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2593 #endif /*HAVE_IPV6*/
2604 bgp_packet_mpattr_prefix (struct stream
*s
, afi_t afi
, safi_t safi
,
2605 struct prefix
*p
, struct prefix_rd
*prd
,
2606 u_char
*tag
, int addpath_encode
,
2607 u_int32_t addpath_tx_id
)
2609 if (safi
== SAFI_MPLS_VPN
)
2612 stream_putl(s
, addpath_tx_id
);
2613 /* Tag, RD, Prefix write. */
2614 stream_putc (s
, p
->prefixlen
+ 88);
2615 stream_put (s
, tag
, 3);
2616 stream_put (s
, prd
->val
, 8);
2617 stream_put (s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
2620 stream_put_prefix_addpath (s
, p
, addpath_encode
, addpath_tx_id
);
2624 bgp_packet_mpattr_prefix_size (afi_t afi
, safi_t safi
, struct prefix
*p
)
2626 int size
= PSIZE (p
->prefixlen
);
2627 if (safi
== SAFI_MPLS_VPN
)
2633 * Encodes the tunnel encapsulation attribute,
2634 * and with ENABLE_BGP_VNC the VNC attribute which uses
2635 * almost the same TLV format
2638 bgp_packet_mpattr_tea(
2645 unsigned int attrlenfield
= 0;
2646 unsigned int attrhdrlen
= 0;
2647 struct bgp_attr_encap_subtlv
*subtlvs
;
2648 struct bgp_attr_encap_subtlv
*st
;
2649 const char *attrname
;
2651 if (!attr
|| !attr
->extra
)
2655 case BGP_ATTR_ENCAP
:
2656 attrname
= "Tunnel Encap";
2657 subtlvs
= attr
->extra
->encap_subtlvs
;
2660 * The tunnel encap attr has an "outer" tlv.
2662 * L = total length of subtlvs,
2663 * V = concatenated subtlvs.
2665 attrlenfield
= 2 + 2; /* T + L */
2666 attrhdrlen
= 1 + 1; /* subTLV T + L */
2672 subtlvs
= attr
->extra
->vnc_subtlvs
;
2673 attrlenfield
= 0; /* no outer T + L */
2674 attrhdrlen
= 2 + 2; /* subTLV T + L */
2683 /* if no tlvs, don't make attr */
2684 if (subtlvs
== NULL
)
2687 /* compute attr length */
2688 for (st
= subtlvs
; st
; st
= st
->next
) {
2689 attrlenfield
+= (attrhdrlen
+ st
->length
);
2692 if (attrlenfield
> 0xffff) {
2693 zlog_info ("%s attribute is too long (length=%d), can't send it",
2699 if (attrlenfield
> 0xff) {
2700 /* 2-octet length field */
2702 BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
2703 stream_putc (s
, attrtype
);
2704 stream_putw (s
, attrlenfield
& 0xffff);
2706 /* 1-octet length field */
2707 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
);
2708 stream_putc (s
, attrtype
);
2709 stream_putc (s
, attrlenfield
& 0xff);
2712 if (attrtype
== BGP_ATTR_ENCAP
) {
2713 /* write outer T+L */
2714 stream_putw(s
, attr
->extra
->encap_tunneltype
);
2715 stream_putw(s
, attrlenfield
- 4);
2718 /* write each sub-tlv */
2719 for (st
= subtlvs
; st
; st
= st
->next
) {
2720 if (attrtype
== BGP_ATTR_ENCAP
) {
2721 stream_putc (s
, st
->type
);
2722 stream_putc (s
, st
->length
);
2725 stream_putw (s
, st
->type
);
2726 stream_putw (s
, st
->length
);
2729 stream_put (s
, st
->value
, st
->length
);
2734 bgp_packet_mpattr_end (struct stream
*s
, size_t sizep
)
2736 /* Set MP attribute length. Don't count the (2) bytes used to encode
2738 stream_putw_at (s
, sizep
, (stream_get_endp (s
) - sizep
) - 2);
2741 /* Make attribute packet. */
2743 bgp_packet_attribute (struct bgp
*bgp
, struct peer
*peer
,
2744 struct stream
*s
, struct attr
*attr
,
2745 struct bpacket_attr_vec_arr
*vecarr
,
2746 struct prefix
*p
, afi_t afi
, safi_t safi
,
2747 struct peer
*from
, struct prefix_rd
*prd
, u_char
*tag
,
2749 u_int32_t addpath_tx_id
)
2752 size_t aspath_sizep
;
2753 struct aspath
*aspath
;
2754 int send_as4_path
= 0;
2755 int send_as4_aggregator
= 0;
2756 int use32bit
= (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2761 /* Remember current pointer. */
2762 cp
= stream_get_endp (s
);
2764 if (p
&& !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
) &&
2765 !peer_cap_enhe(peer
)))
2767 size_t mpattrlen_pos
= 0;
2769 mpattrlen_pos
= bgp_packet_mpattr_start(s
, afi
, safi
,
2770 (peer_cap_enhe(peer
) ? AFI_IP6
: afi
),
2772 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, tag
,
2773 addpath_encode
, addpath_tx_id
);
2774 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2777 /* Origin attribute. */
2778 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2779 stream_putc (s
, BGP_ATTR_ORIGIN
);
2781 stream_putc (s
, attr
->origin
);
2783 /* AS path attribute. */
2785 /* If remote-peer is EBGP */
2786 if (peer
->sort
== BGP_PEER_EBGP
2787 && (! CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_AS_PATH_UNCHANGED
)
2788 || attr
->aspath
->segments
== NULL
)
2789 && (! CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
)))
2791 aspath
= aspath_dup (attr
->aspath
);
2793 /* Even though we may not be configured for confederations we may have
2794 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2795 aspath
= aspath_delete_confed_seq (aspath
);
2797 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
2799 /* Stuff our path CONFED_ID on the front */
2800 aspath
= aspath_add_seq (aspath
, bgp
->confed_id
);
2804 if (peer
->change_local_as
) {
2805 /* If replace-as is specified, we only use the change_local_as when
2806 advertising routes. */
2807 if( ! CHECK_FLAG (peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
) ) {
2808 aspath
= aspath_add_seq (aspath
, peer
->local_as
);
2810 aspath
= aspath_add_seq (aspath
, peer
->change_local_as
);
2812 aspath
= aspath_add_seq (aspath
, peer
->local_as
);
2816 else if (peer
->sort
== BGP_PEER_CONFED
)
2818 /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
2819 aspath
= aspath_dup (attr
->aspath
);
2820 aspath
= aspath_add_confed_seq (aspath
, peer
->local_as
);
2823 aspath
= attr
->aspath
;
2825 /* If peer is not AS4 capable, then:
2826 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2827 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
2828 * types are in it (i.e. exclude them if they are there)
2829 * AND do this only if there is at least one asnum > 65535 in the path!
2830 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
2831 * all ASnums > 65535 to BGP_AS_TRANS
2834 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2835 stream_putc (s
, BGP_ATTR_AS_PATH
);
2836 aspath_sizep
= stream_get_endp (s
);
2838 stream_putw_at (s
, aspath_sizep
, aspath_put (s
, aspath
, use32bit
));
2840 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2843 if (!use32bit
&& aspath_has_as4 (aspath
))
2844 send_as4_path
= 1; /* we'll do this later, at the correct place */
2846 /* Nexthop attribute. */
2847 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&& !peer_cap_enhe(peer
))
2849 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
))
2851 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2852 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
2853 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2855 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
2857 else if (safi
== SAFI_UNICAST
&& peer_cap_enhe(from
))
2860 * Likely this is the case when an IPv4 prefix was received with
2861 * Extended Next-hop capability and now being advertised to
2863 * Setting the mandatory (ipv4) next-hop attribute here to enable
2864 * implicit next-hop self with correct (ipv4 address family).
2866 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2867 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
2868 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, NULL
);
2870 stream_put_ipv4 (s
, 0);
2874 /* MED attribute. */
2875 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
) ||
2878 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2879 stream_putc (s
, BGP_ATTR_MULTI_EXIT_DISC
);
2881 stream_putl (s
, (bgp
->maxmed_active
? bgp
->maxmed_value
: attr
->med
));
2884 /* Local preference. */
2885 if (peer
->sort
== BGP_PEER_IBGP
||
2886 peer
->sort
== BGP_PEER_CONFED
)
2888 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2889 stream_putc (s
, BGP_ATTR_LOCAL_PREF
);
2891 stream_putl (s
, attr
->local_pref
);
2894 /* Atomic aggregate. */
2895 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
))
2897 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2898 stream_putc (s
, BGP_ATTR_ATOMIC_AGGREGATE
);
2903 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
))
2905 assert (attr
->extra
);
2907 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
2908 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2909 stream_putc (s
, BGP_ATTR_AGGREGATOR
);
2913 /* AS4 capable peer */
2915 stream_putl (s
, attr
->extra
->aggregator_as
);
2919 /* 2-byte AS peer */
2922 /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
2923 if ( attr
->extra
->aggregator_as
> 65535 )
2925 stream_putw (s
, BGP_AS_TRANS
);
2927 /* we have to send AS4_AGGREGATOR, too.
2928 * we'll do that later in order to send attributes in ascending
2931 send_as4_aggregator
= 1;
2934 stream_putw (s
, (u_int16_t
) attr
->extra
->aggregator_as
);
2936 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
2939 /* Community attribute. */
2940 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
2941 && (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
)))
2943 if (attr
->community
->size
* 4 > 255)
2945 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2946 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
2947 stream_putw (s
, attr
->community
->size
* 4);
2951 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2952 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
2953 stream_putc (s
, attr
->community
->size
* 4);
2955 stream_put (s
, attr
->community
->val
, attr
->community
->size
* 4);
2958 /* Route Reflector. */
2959 if (peer
->sort
== BGP_PEER_IBGP
2961 && from
->sort
== BGP_PEER_IBGP
)
2963 /* Originator ID. */
2964 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2965 stream_putc (s
, BGP_ATTR_ORIGINATOR_ID
);
2968 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
2969 stream_put_in_addr (s
, &attr
->extra
->originator_id
);
2971 stream_put_in_addr (s
, &from
->remote_id
);
2974 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2975 stream_putc (s
, BGP_ATTR_CLUSTER_LIST
);
2977 if (attr
->extra
&& attr
->extra
->cluster
)
2979 stream_putc (s
, attr
->extra
->cluster
->length
+ 4);
2980 /* If this peer configuration's parent BGP has cluster_id. */
2981 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
2982 stream_put_in_addr (s
, &bgp
->cluster_id
);
2984 stream_put_in_addr (s
, &bgp
->router_id
);
2985 stream_put (s
, attr
->extra
->cluster
->list
,
2986 attr
->extra
->cluster
->length
);
2991 /* If this peer configuration's parent BGP has cluster_id. */
2992 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
2993 stream_put_in_addr (s
, &bgp
->cluster_id
);
2995 stream_put_in_addr (s
, &bgp
->router_id
);
2999 /* Extended Communities attribute. */
3000 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
3001 && (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
)))
3003 struct attr_extra
*attre
= attr
->extra
;
3007 if (peer
->sort
== BGP_PEER_IBGP
3008 || peer
->sort
== BGP_PEER_CONFED
)
3010 if (attre
->ecommunity
->size
* 8 > 255)
3012 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3013 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
3014 stream_putw (s
, attre
->ecommunity
->size
* 8);
3018 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3019 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
3020 stream_putc (s
, attre
->ecommunity
->size
* 8);
3022 stream_put (s
, attre
->ecommunity
->val
, attre
->ecommunity
->size
* 8);
3028 int ecom_tr_size
= 0;
3031 for (i
= 0; i
< attre
->ecommunity
->size
; i
++)
3033 pnt
= attre
->ecommunity
->val
+ (i
* 8);
3036 if (CHECK_FLAG (tbit
, ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3044 if (ecom_tr_size
* 8 > 255)
3046 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3047 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
3048 stream_putw (s
, ecom_tr_size
* 8);
3052 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3053 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
3054 stream_putc (s
, ecom_tr_size
* 8);
3057 for (i
= 0; i
< attre
->ecommunity
->size
; i
++)
3059 pnt
= attre
->ecommunity
->val
+ (i
* 8);
3062 if (CHECK_FLAG (tbit
, ECOMMUNITY_FLAG_NON_TRANSITIVE
))
3065 stream_put (s
, pnt
, 8);
3071 if ( send_as4_path
)
3073 /* If the peer is NOT As4 capable, AND */
3074 /* there are ASnums > 65535 in path THEN
3075 * give out AS4_PATH */
3077 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3079 * Hm, I wonder... confederation things *should* only be at
3080 * the beginning of an aspath, right? Then we should use
3081 * aspath_delete_confed_seq for this, because it is already
3083 * Folks, talk to me: what is reasonable here!?
3085 aspath
= aspath_delete_confed_seq (aspath
);
3087 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
3088 stream_putc (s
, BGP_ATTR_AS4_PATH
);
3089 aspath_sizep
= stream_get_endp (s
);
3091 stream_putw_at (s
, aspath_sizep
, aspath_put (s
, aspath
, 1));
3094 if (aspath
!= attr
->aspath
)
3095 aspath_free (aspath
);
3097 if ( send_as4_aggregator
)
3099 assert (attr
->extra
);
3101 /* send AS4_AGGREGATOR, at this place */
3102 /* this section of code moved here in order to ensure the correct
3103 * *ascending* order of attributes
3105 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3106 stream_putc (s
, BGP_ATTR_AS4_AGGREGATOR
);
3108 stream_putl (s
, attr
->extra
->aggregator_as
);
3109 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
3112 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) &&
3113 (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3115 /* Tunnel Encap attribute */
3116 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3120 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_VNC
);
3124 /* Unknown transit attribute. */
3125 if (attr
->extra
&& attr
->extra
->transit
)
3126 stream_put (s
, attr
->extra
->transit
->val
, attr
->extra
->transit
->length
);
3128 /* Return total size of attribute. */
3129 return stream_get_endp (s
) - cp
;
3133 bgp_packet_mpunreach_start (struct stream
*s
, afi_t afi
, safi_t safi
)
3135 unsigned long attrlen_pnt
;
3137 /* Set extended bit always to encode the attribute length as 2 bytes */
3138 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
3139 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
3141 attrlen_pnt
= stream_get_endp (s
);
3142 stream_putw (s
, 0); /* Length of this attribute. */
3144 stream_putw (s
, afi
);
3145 stream_putc (s
, (safi
== SAFI_MPLS_VPN
) ? SAFI_MPLS_LABELED_VPN
: safi
);
3150 bgp_packet_mpunreach_prefix (struct stream
*s
, struct prefix
*p
,
3151 afi_t afi
, safi_t safi
, struct prefix_rd
*prd
,
3152 u_char
*tag
, int addpath_encode
,
3153 u_int32_t addpath_tx_id
)
3155 if (safi
== SAFI_MPLS_VPN
)
3159 stream_putl(s
, addpath_tx_id
);
3161 stream_putc (s
, p
->prefixlen
+ 88);
3162 stream_put (s
, tag
, 3);
3163 stream_put (s
, prd
->val
, 8);
3164 stream_put (s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
3167 stream_put_prefix_addpath (s
, p
, addpath_encode
, addpath_tx_id
);
3171 bgp_packet_mpunreach_end (struct stream
*s
, size_t attrlen_pnt
)
3173 bgp_packet_mpattr_end (s
, attrlen_pnt
);
3176 /* Initialization of attribute. */
3178 bgp_attr_init (void)
3189 bgp_attr_finish (void)
3193 community_finish ();
3194 ecommunity_finish ();
3199 /* Make attribute packet. */
3201 bgp_dump_routes_attr (struct stream
*s
, struct attr
*attr
,
3202 struct prefix
*prefix
)
3207 struct aspath
*aspath
;
3208 int addpath_encode
= 0;
3209 u_int32_t addpath_tx_id
= 0;
3211 /* Remember current pointer. */
3212 cp
= stream_get_endp (s
);
3214 /* Place holder of length. */
3217 /* Origin attribute. */
3218 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3219 stream_putc (s
, BGP_ATTR_ORIGIN
);
3221 stream_putc (s
, attr
->origin
);
3223 aspath
= attr
->aspath
;
3225 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3226 stream_putc (s
, BGP_ATTR_AS_PATH
);
3227 aspath_lenp
= stream_get_endp (s
);
3230 stream_putw_at (s
, aspath_lenp
, aspath_put (s
, aspath
, 1));
3232 /* Nexthop attribute. */
3233 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3236 && prefix
->family
!= AF_INET6
3237 #endif /* HAVE_IPV6 */
3240 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3241 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
3243 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
3246 /* MED attribute. */
3247 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
))
3249 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
3250 stream_putc (s
, BGP_ATTR_MULTI_EXIT_DISC
);
3252 stream_putl (s
, attr
->med
);
3255 /* Local preference. */
3256 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
))
3258 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3259 stream_putc (s
, BGP_ATTR_LOCAL_PREF
);
3261 stream_putl (s
, attr
->local_pref
);
3264 /* Atomic aggregate. */
3265 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
))
3267 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3268 stream_putc (s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3273 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
))
3275 assert (attr
->extra
);
3276 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3277 stream_putc (s
, BGP_ATTR_AGGREGATOR
);
3279 stream_putl (s
, attr
->extra
->aggregator_as
);
3280 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
3283 /* Community attribute. */
3284 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
))
3286 if (attr
->community
->size
* 4 > 255)
3288 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3289 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
3290 stream_putw (s
, attr
->community
->size
* 4);
3294 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3295 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
3296 stream_putc (s
, attr
->community
->size
* 4);
3298 stream_put (s
, attr
->community
->val
, attr
->community
->size
* 4);
3302 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3303 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
&& attr
->extra
&&
3304 (attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
||
3305 attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) )
3308 struct attr_extra
*attre
= attr
->extra
;
3310 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3311 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3312 sizep
= stream_get_endp (s
);
3315 stream_putc (s
, 0); /* Marker: Attribute length. */
3316 stream_putw(s
, AFI_IP6
); /* AFI */
3317 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3320 stream_putc(s
, attre
->mp_nexthop_len
);
3321 stream_put(s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3322 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3323 stream_put(s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
3329 stream_put_prefix_addpath (s
, prefix
, addpath_encode
, addpath_tx_id
);
3331 /* Set MP attribute length. */
3332 stream_putc_at (s
, sizep
, (stream_get_endp (s
) - sizep
) - 1);
3334 #endif /* HAVE_IPV6 */
3336 /* Return total size of attribute. */
3337 len
= stream_get_endp (s
) - cp
- 2;
3338 stream_putw_at (s
, cp
, len
);