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 /* Attribute strings for logging. */
48 static const struct message attr_str
[] =
50 { BGP_ATTR_ORIGIN
, "ORIGIN" },
51 { BGP_ATTR_AS_PATH
, "AS_PATH" },
52 { BGP_ATTR_NEXT_HOP
, "NEXT_HOP" },
53 { BGP_ATTR_MULTI_EXIT_DISC
, "MULTI_EXIT_DISC" },
54 { BGP_ATTR_LOCAL_PREF
, "LOCAL_PREF" },
55 { BGP_ATTR_ATOMIC_AGGREGATE
, "ATOMIC_AGGREGATE" },
56 { BGP_ATTR_AGGREGATOR
, "AGGREGATOR" },
57 { BGP_ATTR_COMMUNITIES
, "COMMUNITY" },
58 { BGP_ATTR_ORIGINATOR_ID
, "ORIGINATOR_ID" },
59 { BGP_ATTR_CLUSTER_LIST
, "CLUSTER_LIST" },
60 { BGP_ATTR_DPA
, "DPA" },
61 { BGP_ATTR_ADVERTISER
, "ADVERTISER"} ,
62 { BGP_ATTR_RCID_PATH
, "RCID_PATH" },
63 { BGP_ATTR_MP_REACH_NLRI
, "MP_REACH_NLRI" },
64 { BGP_ATTR_MP_UNREACH_NLRI
, "MP_UNREACH_NLRI" },
65 { BGP_ATTR_EXT_COMMUNITIES
, "EXT_COMMUNITIES" },
66 { BGP_ATTR_AS4_PATH
, "AS4_PATH" },
67 { BGP_ATTR_AS4_AGGREGATOR
, "AS4_AGGREGATOR" },
68 { BGP_ATTR_AS_PATHLIMIT
, "AS_PATHLIMIT" },
69 { BGP_ATTR_ENCAP
, "ENCAP" },
71 static const int attr_str_max
= array_size(attr_str
);
73 static const struct message attr_flag_str
[] =
75 { BGP_ATTR_FLAG_OPTIONAL
, "Optional" },
76 { BGP_ATTR_FLAG_TRANS
, "Transitive" },
77 { BGP_ATTR_FLAG_PARTIAL
, "Partial" },
78 /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
79 { BGP_ATTR_FLAG_EXTLEN
, "Extended Length" },
82 static struct hash
*cluster_hash
;
85 cluster_hash_alloc (void *p
)
87 const struct cluster_list
*val
= (const struct cluster_list
*) p
;
88 struct cluster_list
*cluster
;
90 cluster
= XMALLOC (MTYPE_CLUSTER
, sizeof (struct cluster_list
));
91 cluster
->length
= val
->length
;
95 cluster
->list
= XMALLOC (MTYPE_CLUSTER_VAL
, val
->length
);
96 memcpy (cluster
->list
, val
->list
, val
->length
);
106 /* Cluster list related functions. */
107 static struct cluster_list
*
108 cluster_parse (struct in_addr
* pnt
, int length
)
110 struct cluster_list tmp
;
111 struct cluster_list
*cluster
;
116 cluster
= hash_get (cluster_hash
, &tmp
, cluster_hash_alloc
);
122 cluster_loop_check (struct cluster_list
*cluster
, struct in_addr originator
)
126 for (i
= 0; i
< cluster
->length
/ 4; i
++)
127 if (cluster
->list
[i
].s_addr
== originator
.s_addr
)
133 cluster_hash_key_make (void *p
)
135 const struct cluster_list
*cluster
= p
;
137 return jhash(cluster
->list
, cluster
->length
, 0);
141 cluster_hash_cmp (const void *p1
, const void *p2
)
143 const struct cluster_list
* cluster1
= p1
;
144 const struct cluster_list
* cluster2
= p2
;
146 return (cluster1
->length
== cluster2
->length
&&
147 memcmp (cluster1
->list
, cluster2
->list
, cluster1
->length
) == 0);
151 cluster_free (struct cluster_list
*cluster
)
154 XFREE (MTYPE_CLUSTER_VAL
, cluster
->list
);
155 XFREE (MTYPE_CLUSTER
, cluster
);
158 static struct cluster_list
*
159 cluster_dup (struct cluster_list
*cluster
)
161 struct cluster_list
*new;
163 new = XCALLOC (MTYPE_CLUSTER
, sizeof (struct cluster_list
));
164 new->length
= cluster
->length
;
168 new->list
= XMALLOC (MTYPE_CLUSTER_VAL
, cluster
->length
);
169 memcpy (new->list
, cluster
->list
, cluster
->length
);
177 static struct cluster_list
*
178 cluster_intern (struct cluster_list
*cluster
)
180 struct cluster_list
*find
;
182 find
= hash_get (cluster_hash
, cluster
, cluster_hash_alloc
);
189 cluster_unintern (struct cluster_list
*cluster
)
194 if (cluster
->refcnt
== 0)
196 hash_release (cluster_hash
, cluster
);
197 cluster_free (cluster
);
204 cluster_hash
= hash_create (cluster_hash_key_make
, cluster_hash_cmp
);
208 cluster_finish (void)
210 hash_free (cluster_hash
);
214 struct bgp_attr_encap_subtlv
*
215 encap_tlv_dup(struct bgp_attr_encap_subtlv
*orig
)
217 struct bgp_attr_encap_subtlv
*new;
218 struct bgp_attr_encap_subtlv
*tail
;
219 struct bgp_attr_encap_subtlv
*p
;
221 for (p
= orig
, tail
= new = NULL
; p
; p
= p
->next
) {
222 int size
= sizeof(struct bgp_attr_encap_subtlv
) - 1 + p
->length
;
224 tail
->next
= XCALLOC(MTYPE_ENCAP_TLV
, size
);
227 tail
= new = XCALLOC(MTYPE_ENCAP_TLV
, size
);
230 memcpy(tail
, p
, size
);
238 encap_free(struct bgp_attr_encap_subtlv
*p
)
240 struct bgp_attr_encap_subtlv
*next
;
244 XFREE(MTYPE_ENCAP_TLV
, p
);
250 bgp_attr_flush_encap(struct attr
*attr
)
252 if (!attr
|| !attr
->extra
)
255 if (attr
->extra
->encap_subtlvs
) {
256 encap_free(attr
->extra
->encap_subtlvs
);
257 attr
->extra
->encap_subtlvs
= NULL
;
262 * Compare encap sub-tlv chains
267 * This algorithm could be made faster if needed
270 encap_same(struct bgp_attr_encap_subtlv
*h1
, struct bgp_attr_encap_subtlv
*h2
)
272 struct bgp_attr_encap_subtlv
*p
;
273 struct bgp_attr_encap_subtlv
*q
;
284 for (p
= h1
; p
; p
= p
->next
) {
285 for (q
= h2
; q
; q
= q
->next
) {
286 if ((p
->type
== q
->type
) &&
287 (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
) &&
300 (p
->length
== q
->length
) &&
301 !memcmp(p
->value
, q
->value
, p
->length
)) {
313 /* Unknown transit attribute. */
314 static struct hash
*transit_hash
;
317 transit_free (struct transit
*transit
)
320 XFREE (MTYPE_TRANSIT_VAL
, transit
->val
);
321 XFREE (MTYPE_TRANSIT
, transit
);
324 static struct transit
*
325 transit_dup (struct transit
*transit
)
329 new = XCALLOC (MTYPE_TRANSIT
, sizeof (struct transit
));
330 new->length
= transit
->length
;
333 new->val
= XMALLOC (MTYPE_TRANSIT_VAL
, transit
->length
);
334 memcpy (new->val
, transit
->val
, transit
->length
);
343 transit_hash_alloc (void *p
)
345 /* Transit structure is already allocated. */
349 static struct transit
*
350 transit_intern (struct transit
*transit
)
352 struct transit
*find
;
354 find
= hash_get (transit_hash
, transit
, transit_hash_alloc
);
356 transit_free (transit
);
363 transit_unintern (struct transit
*transit
)
368 if (transit
->refcnt
== 0)
370 hash_release (transit_hash
, transit
);
371 transit_free (transit
);
376 transit_hash_key_make (void *p
)
378 const struct transit
* transit
= p
;
380 return jhash(transit
->val
, transit
->length
, 0);
384 transit_hash_cmp (const void *p1
, const void *p2
)
386 const struct transit
* transit1
= p1
;
387 const struct transit
* transit2
= p2
;
389 return (transit1
->length
== transit2
->length
&&
390 memcmp (transit1
->val
, transit2
->val
, transit1
->length
) == 0);
396 transit_hash
= hash_create (transit_hash_key_make
, transit_hash_cmp
);
400 transit_finish (void)
402 hash_free (transit_hash
);
406 /* Attribute hash routines. */
407 static struct hash
*attrhash
;
409 static struct attr_extra
*
410 bgp_attr_extra_new (void)
412 return XCALLOC (MTYPE_ATTR_EXTRA
, sizeof (struct attr_extra
));
416 bgp_attr_extra_free (struct attr
*attr
)
420 if (attr
->extra
->encap_subtlvs
) {
421 encap_free(attr
->extra
->encap_subtlvs
);
422 attr
->extra
->encap_subtlvs
= NULL
;
424 XFREE (MTYPE_ATTR_EXTRA
, attr
->extra
);
430 bgp_attr_extra_get (struct attr
*attr
)
433 attr
->extra
= bgp_attr_extra_new();
437 /* Shallow copy of an attribute
438 * Though, not so shallow that it doesn't copy the contents
439 * of the attr_extra pointed to by 'extra'
442 bgp_attr_dup (struct attr
*new, struct attr
*orig
)
444 struct attr_extra
*extra
= new->extra
;
447 /* if caller provided attr_extra space, use it in any case.
449 * This is neccesary even if orig->extra equals NULL, because otherwise
450 * memory may be later allocated on the heap by bgp_attr_extra_get.
452 * That memory would eventually be leaked, because the caller must not
453 * call bgp_attr_extra_free if he provided attr_extra on the stack.
458 memset(new->extra
, 0, sizeof(struct attr_extra
));
460 *new->extra
= *orig
->extra
;
461 if (orig
->extra
->encap_subtlvs
) {
462 new->extra
->encap_subtlvs
= encap_tlv_dup(orig
->extra
->encap_subtlvs
);
466 else if (orig
->extra
)
468 new->extra
= bgp_attr_extra_new();
469 *new->extra
= *orig
->extra
;
470 if (orig
->extra
->encap_subtlvs
) {
471 new->extra
->encap_subtlvs
= encap_tlv_dup(orig
->extra
->encap_subtlvs
);
477 bgp_attr_deep_dup (struct attr
*new, struct attr
*orig
)
480 new->aspath
= aspath_dup(orig
->aspath
);
483 new->community
= community_dup(orig
->community
);
487 if (orig
->extra
->ecommunity
)
488 new->extra
->ecommunity
= ecommunity_dup(orig
->extra
->ecommunity
);
489 if (orig
->extra
->cluster
)
490 new->extra
->cluster
= cluster_dup(orig
->extra
->cluster
);
491 if (orig
->extra
->transit
)
492 new->extra
->transit
= transit_dup(orig
->extra
->transit
);
497 bgp_attr_deep_free (struct attr
*attr
)
500 aspath_free(attr
->aspath
);
503 community_free(attr
->community
);
507 if (attr
->extra
->ecommunity
)
508 ecommunity_free(&attr
->extra
->ecommunity
);
509 if (attr
->extra
->cluster
)
510 cluster_free(attr
->extra
->cluster
);
511 if (attr
->extra
->transit
)
512 transit_free(attr
->extra
->transit
);
519 return attrhash
->count
;
523 attr_unknown_count (void)
525 return transit_hash
->count
;
529 attrhash_key_make (void *p
)
531 const struct attr
*attr
= (struct attr
*) p
;
532 const struct attr_extra
*extra
= attr
->extra
;
534 #define MIX(val) key = jhash_1word(val, key)
537 MIX(attr
->nexthop
.s_addr
);
539 MIX(attr
->local_pref
);
542 key
+= attr
->nexthop
.s_addr
;
544 key
+= attr
->local_pref
;
548 MIX(extra
->aggregator_as
);
549 MIX(extra
->aggregator_addr
.s_addr
);
551 MIX(extra
->mp_nexthop_global_in
.s_addr
);
552 MIX(extra
->originator_id
.s_addr
);
557 MIX(aspath_key_make (attr
->aspath
));
559 MIX(community_hash_make (attr
->community
));
563 if (extra
->ecommunity
)
564 MIX(ecommunity_hash_make (extra
->ecommunity
));
566 MIX(cluster_hash_key_make (extra
->cluster
));
568 MIX(transit_hash_key_make (extra
->transit
));
571 MIX(extra
->mp_nexthop_len
);
572 key
= jhash(extra
->mp_nexthop_global
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
573 key
= jhash(extra
->mp_nexthop_local
.s6_addr
, IPV6_MAX_BYTELEN
, key
);
574 #endif /* HAVE_IPV6 */
581 attrhash_cmp (const void *p1
, const void *p2
)
583 const struct attr
* attr1
= p1
;
584 const struct attr
* attr2
= p2
;
586 if (attr1
->flag
== attr2
->flag
587 && attr1
->origin
== attr2
->origin
588 && attr1
->nexthop
.s_addr
== attr2
->nexthop
.s_addr
589 && attr1
->aspath
== attr2
->aspath
590 && attr1
->community
== attr2
->community
591 && attr1
->med
== attr2
->med
592 && attr1
->local_pref
== attr2
->local_pref
593 && attr1
->rmap_change_flags
== attr2
->rmap_change_flags
)
595 const struct attr_extra
*ae1
= attr1
->extra
;
596 const struct attr_extra
*ae2
= attr2
->extra
;
599 && ae1
->aggregator_as
== ae2
->aggregator_as
600 && ae1
->aggregator_addr
.s_addr
== ae2
->aggregator_addr
.s_addr
601 && ae1
->weight
== ae2
->weight
602 && ae1
->tag
== ae2
->tag
604 && ae1
->mp_nexthop_len
== ae2
->mp_nexthop_len
605 && IPV6_ADDR_SAME (&ae1
->mp_nexthop_global
, &ae2
->mp_nexthop_global
)
606 && IPV6_ADDR_SAME (&ae1
->mp_nexthop_local
, &ae2
->mp_nexthop_local
)
607 #endif /* HAVE_IPV6 */
608 && IPV4_ADDR_SAME (&ae1
->mp_nexthop_global_in
, &ae2
->mp_nexthop_global_in
)
609 && ae1
->ecommunity
== ae2
->ecommunity
610 && ae1
->cluster
== ae2
->cluster
611 && ae1
->transit
== ae2
->transit
612 && (ae1
->encap_tunneltype
== ae2
->encap_tunneltype
)
613 && encap_same(ae1
->encap_subtlvs
, ae2
->encap_subtlvs
)
614 && IPV4_ADDR_SAME (&ae1
->originator_id
, &ae2
->originator_id
))
618 /* neither attribute has extra attributes, so they're same */
628 attrhash
= hash_create (attrhash_key_make
, attrhash_cmp
);
632 attrhash_finish (void)
634 hash_free (attrhash
);
639 attr_show_all_iterator (struct hash_backet
*backet
, struct vty
*vty
)
641 struct attr
*attr
= backet
->data
;
643 vty_out (vty
, "attr[%ld] nexthop %s%s", attr
->refcnt
,
644 inet_ntoa (attr
->nexthop
), VTY_NEWLINE
);
648 attr_show_all (struct vty
*vty
)
650 hash_iterate (attrhash
,
651 (void (*)(struct hash_backet
*, void *))
652 attr_show_all_iterator
,
657 bgp_attr_hash_alloc (void *p
)
659 const struct attr
* val
= (const struct attr
*) p
;
662 attr
= XMALLOC (MTYPE_ATTR
, sizeof (struct attr
));
666 attr
->extra
= bgp_attr_extra_new ();
667 *attr
->extra
= *val
->extra
;
669 if (attr
->extra
->encap_subtlvs
) {
670 attr
->extra
->encap_subtlvs
= encap_tlv_dup(attr
->extra
->encap_subtlvs
);
677 /* Internet argument attribute. */
679 bgp_attr_intern (struct attr
*attr
)
683 /* Intern referenced strucutre. */
686 if (! attr
->aspath
->refcnt
)
687 attr
->aspath
= aspath_intern (attr
->aspath
);
689 attr
->aspath
->refcnt
++;
693 if (! attr
->community
->refcnt
)
694 attr
->community
= community_intern (attr
->community
);
696 attr
->community
->refcnt
++;
700 struct attr_extra
*attre
= attr
->extra
;
702 if (attre
->ecommunity
)
704 if (! attre
->ecommunity
->refcnt
)
705 attre
->ecommunity
= ecommunity_intern (attre
->ecommunity
);
707 attre
->ecommunity
->refcnt
++;
712 if (! attre
->cluster
->refcnt
)
713 attre
->cluster
= cluster_intern (attre
->cluster
);
715 attre
->cluster
->refcnt
++;
719 if (! attre
->transit
->refcnt
)
720 attre
->transit
= transit_intern (attre
->transit
);
722 attre
->transit
->refcnt
++;
726 find
= (struct attr
*) hash_get (attrhash
, attr
, bgp_attr_hash_alloc
);
733 * Increment the refcount on various structures that attr holds.
734 * Note on usage: call _only_ when the 'attr' object has already
735 * been 'intern'ed and exists in 'attrhash' table. The function
736 * serves to hold a reference to that (real) object.
737 * Note also that the caller can safely call bgp_attr_unintern()
738 * after calling bgp_attr_refcount(). That would release the
739 * reference and could result in a free() of the attr object.
742 bgp_attr_refcount (struct attr
*attr
)
744 /* Intern referenced strucutre. */
746 attr
->aspath
->refcnt
++;
749 attr
->community
->refcnt
++;
753 struct attr_extra
*attre
= attr
->extra
;
754 if (attre
->ecommunity
)
755 attre
->ecommunity
->refcnt
++;
758 attre
->cluster
->refcnt
++;
761 attre
->transit
->refcnt
++;
767 /* Make network statement's attribute. */
769 bgp_attr_default_set (struct attr
*attr
, u_char origin
)
771 memset (attr
, 0, sizeof (struct attr
));
772 bgp_attr_extra_get (attr
);
774 attr
->origin
= origin
;
775 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
776 attr
->aspath
= aspath_empty ();
777 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
778 attr
->extra
->weight
= BGP_ATTR_DEFAULT_WEIGHT
;
779 attr
->extra
->tag
= 0;
780 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
782 attr
->extra
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
789 /* Make network statement's attribute. */
791 bgp_attr_default_intern (u_char origin
)
796 memset (&attr
, 0, sizeof (struct attr
));
797 bgp_attr_extra_get (&attr
);
799 bgp_attr_default_set(&attr
, origin
);
801 new = bgp_attr_intern (&attr
);
802 bgp_attr_extra_free (&attr
);
804 aspath_unintern (&new->aspath
);
808 /* Create the attributes for an aggregate */
810 bgp_attr_aggregate_intern (struct bgp
*bgp
, u_char origin
,
811 struct aspath
*aspath
,
812 struct community
*community
, int as_set
,
813 u_char atomic_aggregate
)
817 struct attr_extra attre
;
819 memset (&attr
, 0, sizeof (struct attr
));
820 memset (&attre
, 0, sizeof (struct attr_extra
));
823 /* Origin attribute. */
824 attr
.origin
= origin
;
825 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
827 /* AS path attribute. */
829 attr
.aspath
= aspath_intern (aspath
);
831 attr
.aspath
= aspath_empty ();
832 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
834 /* Next hop attribute. */
835 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
839 attr
.community
= community
;
840 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
);
843 attre
.weight
= BGP_ATTR_DEFAULT_WEIGHT
;
845 attre
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
847 if (! as_set
|| atomic_aggregate
)
848 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
);
849 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
);
850 if (CHECK_FLAG (bgp
->config
, BGP_CONFIG_CONFEDERATION
))
851 attre
.aggregator_as
= bgp
->confed_id
;
853 attre
.aggregator_as
= bgp
->as
;
854 attre
.aggregator_addr
= bgp
->router_id
;
856 new = bgp_attr_intern (&attr
);
858 aspath_unintern (&new->aspath
);
862 /* Unintern just the sub-components of the attr, but not the attr */
864 bgp_attr_unintern_sub (struct attr
*attr
)
866 /* aspath refcount shoud be decrement. */
868 aspath_unintern (&attr
->aspath
);
869 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
));
872 community_unintern (&attr
->community
);
873 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
));
877 if (attr
->extra
->ecommunity
)
878 ecommunity_unintern (&attr
->extra
->ecommunity
);
879 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
));
881 if (attr
->extra
->cluster
)
882 cluster_unintern (attr
->extra
->cluster
);
883 UNSET_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST
));
885 if (attr
->extra
->transit
)
886 transit_unintern (attr
->extra
->transit
);
890 /* Free bgp attribute and aspath. */
892 bgp_attr_unintern (struct attr
**pattr
)
894 struct attr
*attr
= *pattr
;
897 struct attr_extra tmp_extra
;
899 /* Decrement attribute reference. */
906 tmp
.extra
= &tmp_extra
;
907 memcpy (tmp
.extra
, attr
->extra
, sizeof (struct attr_extra
));
910 /* If reference becomes zero then free attribute object. */
911 if (attr
->refcnt
== 0)
913 ret
= hash_release (attrhash
, attr
);
914 assert (ret
!= NULL
);
915 bgp_attr_extra_free (attr
);
916 XFREE (MTYPE_ATTR
, attr
);
920 bgp_attr_unintern_sub (&tmp
);
924 bgp_attr_flush (struct attr
*attr
)
926 if (attr
->aspath
&& ! attr
->aspath
->refcnt
)
927 aspath_free (attr
->aspath
);
928 if (attr
->community
&& ! attr
->community
->refcnt
)
929 community_free (attr
->community
);
932 struct attr_extra
*attre
= attr
->extra
;
934 if (attre
->ecommunity
&& ! attre
->ecommunity
->refcnt
)
935 ecommunity_free (&attre
->ecommunity
);
936 if (attre
->cluster
&& ! attre
->cluster
->refcnt
)
937 cluster_free (attre
->cluster
);
938 if (attre
->transit
&& ! attre
->transit
->refcnt
)
939 transit_free (attre
->transit
);
940 encap_free(attre
->encap_subtlvs
);
941 attre
->encap_subtlvs
= NULL
;
945 /* Implement draft-scudder-idr-optional-transitive behaviour and
946 * avoid resetting sessions for malformed attributes which are
947 * are partial/optional and hence where the error likely was not
948 * introduced by the sending neighbour.
950 static bgp_attr_parse_ret_t
951 bgp_attr_malformed (struct bgp_attr_parser_args
*args
, u_char subcode
,
954 struct peer
*const peer
= args
->peer
;
955 const u_int8_t flags
= args
->flags
;
956 /* startp and length must be special-cased, as whether or not to
957 * send the attribute data with the NOTIFY depends on the error,
958 * the caller therefore signals this with the seperate length argument
960 u_char
*notify_datap
= (length
> 0 ? args
->startp
: NULL
);
962 /* Only relax error handling for eBGP peers */
963 if (peer
->sort
!= BGP_PEER_EBGP
)
965 bgp_notify_send_with_data (peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
966 notify_datap
, length
);
967 return BGP_ATTR_PARSE_ERROR
;
971 /* Adjust the stream getp to the end of the attribute, in case we can
972 * still proceed but the caller hasn't read all the attribute.
974 stream_set_getp (BGP_INPUT (peer
),
975 (args
->startp
- STREAM_DATA (BGP_INPUT (peer
)))
978 switch (args
->type
) {
979 /* where an attribute is relatively inconsequential, e.g. it does not
980 * affect route selection, and can be safely ignored, then any such
981 * attributes which are malformed should just be ignored and the route
982 * processed as normal.
984 case BGP_ATTR_AS4_AGGREGATOR
:
985 case BGP_ATTR_AGGREGATOR
:
986 case BGP_ATTR_ATOMIC_AGGREGATE
:
987 return BGP_ATTR_PARSE_PROCEED
;
989 /* Core attributes, particularly ones which may influence route
990 * selection, should always cause session resets
992 case BGP_ATTR_ORIGIN
:
993 case BGP_ATTR_AS_PATH
:
994 case BGP_ATTR_NEXT_HOP
:
995 case BGP_ATTR_MULTI_EXIT_DISC
:
996 case BGP_ATTR_LOCAL_PREF
:
997 case BGP_ATTR_COMMUNITIES
:
998 case BGP_ATTR_ORIGINATOR_ID
:
999 case BGP_ATTR_CLUSTER_LIST
:
1000 case BGP_ATTR_MP_REACH_NLRI
:
1001 case BGP_ATTR_MP_UNREACH_NLRI
:
1002 case BGP_ATTR_EXT_COMMUNITIES
:
1003 bgp_notify_send_with_data (peer
, BGP_NOTIFY_UPDATE_ERR
, subcode
,
1004 notify_datap
, length
);
1005 return BGP_ATTR_PARSE_ERROR
;
1008 /* Partial optional attributes that are malformed should not cause
1009 * the whole session to be reset. Instead treat it as a withdrawal
1010 * of the routes, if possible.
1012 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
)
1013 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1014 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_PARTIAL
))
1015 return BGP_ATTR_PARSE_WITHDRAW
;
1017 /* default to reset */
1018 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1021 /* Find out what is wrong with the path attribute flag bits and log the error.
1022 "Flag bits" here stand for Optional, Transitive and Partial, but not for
1023 Extended Length. Checking O/T/P bits at once implies, that the attribute
1024 being diagnosed is defined by RFC as either a "well-known" or an "optional,
1025 non-transitive" attribute. */
1027 bgp_attr_flags_diagnose (struct bgp_attr_parser_args
*args
,
1028 u_int8_t desired_flags
/* how RFC says it must be */
1032 u_char real_flags
= args
->flags
;
1033 const u_int8_t attr_code
= args
->type
;
1035 desired_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1036 real_flags
&= ~BGP_ATTR_FLAG_EXTLEN
;
1037 for (i
= 0; i
<= 2; i
++) /* O,T,P, but not E */
1040 CHECK_FLAG (desired_flags
, attr_flag_str
[i
].key
) !=
1041 CHECK_FLAG (real_flags
, attr_flag_str
[i
].key
)
1044 zlog_err ("%s attribute must%s be flagged as \"%s\"",
1045 LOOKUP (attr_str
, attr_code
),
1046 CHECK_FLAG (desired_flags
, attr_flag_str
[i
].key
) ? "" : " not",
1047 attr_flag_str
[i
].str
);
1052 zlog_debug ("Strange, %s called for attr %s, but no problem found with flags"
1053 " (real flags 0x%x, desired 0x%x)",
1054 __func__
, LOOKUP (attr_str
, attr_code
),
1055 real_flags
, desired_flags
);
1059 /* Required flags for attributes. EXTLEN will be masked off when testing,
1060 * as will PARTIAL for optional+transitive attributes.
1062 const u_int8_t attr_flags_values
[] = {
1063 [BGP_ATTR_ORIGIN
] = BGP_ATTR_FLAG_TRANS
,
1064 [BGP_ATTR_AS_PATH
] = BGP_ATTR_FLAG_TRANS
,
1065 [BGP_ATTR_NEXT_HOP
] = BGP_ATTR_FLAG_TRANS
,
1066 [BGP_ATTR_MULTI_EXIT_DISC
] = BGP_ATTR_FLAG_OPTIONAL
,
1067 [BGP_ATTR_LOCAL_PREF
] = BGP_ATTR_FLAG_TRANS
,
1068 [BGP_ATTR_ATOMIC_AGGREGATE
] = BGP_ATTR_FLAG_TRANS
,
1069 [BGP_ATTR_AGGREGATOR
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1070 [BGP_ATTR_COMMUNITIES
] = BGP_ATTR_FLAG_TRANS
| BGP_ATTR_FLAG_OPTIONAL
,
1071 [BGP_ATTR_ORIGINATOR_ID
] = BGP_ATTR_FLAG_OPTIONAL
,
1072 [BGP_ATTR_CLUSTER_LIST
] = BGP_ATTR_FLAG_OPTIONAL
,
1073 [BGP_ATTR_MP_REACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1074 [BGP_ATTR_MP_UNREACH_NLRI
] = BGP_ATTR_FLAG_OPTIONAL
,
1075 [BGP_ATTR_EXT_COMMUNITIES
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1076 [BGP_ATTR_AS4_PATH
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1077 [BGP_ATTR_AS4_AGGREGATOR
] = BGP_ATTR_FLAG_OPTIONAL
| BGP_ATTR_FLAG_TRANS
,
1079 static const size_t attr_flags_values_max
=
1080 sizeof (attr_flags_values
) / sizeof (attr_flags_values
[0]);
1083 bgp_attr_flag_invalid (struct bgp_attr_parser_args
*args
)
1085 u_int8_t mask
= BGP_ATTR_FLAG_EXTLEN
;
1086 const u_int8_t flags
= args
->flags
;
1087 const u_int8_t attr_code
= args
->type
;
1089 /* there may be attributes we don't know about */
1090 if (attr_code
> attr_flags_values_max
)
1092 if (attr_flags_values
[attr_code
] == 0)
1095 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to
1098 if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL
, flags
)
1099 && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS
, flags
))
1101 zlog_err ("%s well-known attributes must have transitive flag set (%x)",
1102 LOOKUP (attr_str
, attr_code
), flags
);
1106 /* "For well-known attributes and for optional non-transitive attributes,
1107 * the Partial bit MUST be set to 0."
1109 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_PARTIAL
))
1111 if (!CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
))
1113 zlog_err ("%s well-known attribute "
1114 "must NOT have the partial flag set (%x)",
1115 LOOKUP (attr_str
, attr_code
), flags
);
1118 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1119 && !CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
))
1121 zlog_err ("%s optional + transitive attribute "
1122 "must NOT have the partial flag set (%x)",
1123 LOOKUP (attr_str
, attr_code
), flags
);
1128 /* Optional transitive attributes may go through speakers that don't
1129 * reocgnise them and set the Partial bit.
1131 if (CHECK_FLAG (flags
, BGP_ATTR_FLAG_OPTIONAL
)
1132 && CHECK_FLAG (flags
, BGP_ATTR_FLAG_TRANS
))
1133 SET_FLAG (mask
, BGP_ATTR_FLAG_PARTIAL
);
1136 == attr_flags_values
[attr_code
])
1139 bgp_attr_flags_diagnose (args
, attr_flags_values
[attr_code
]);
1143 /* Get origin attribute of the update message. */
1144 static bgp_attr_parse_ret_t
1145 bgp_attr_origin (struct bgp_attr_parser_args
*args
)
1147 struct peer
*const peer
= args
->peer
;
1148 struct attr
*const attr
= args
->attr
;
1149 const bgp_size_t length
= args
->length
;
1151 /* If any recognized attribute has Attribute Length that conflicts
1152 with the expected length (based on the attribute type code), then
1153 the Error Subcode is set to Attribute Length Error. The Data
1154 field contains the erroneous attribute (type, length and
1158 zlog_err ("Origin attribute length is not one %d", length
);
1159 return bgp_attr_malformed (args
,
1160 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1164 /* Fetch origin attribute. */
1165 attr
->origin
= stream_getc (BGP_INPUT (peer
));
1167 /* If the ORIGIN attribute has an undefined value, then the Error
1168 Subcode is set to Invalid Origin Attribute. The Data field
1169 contains the unrecognized attribute (type, length and value). */
1170 if ((attr
->origin
!= BGP_ORIGIN_IGP
)
1171 && (attr
->origin
!= BGP_ORIGIN_EGP
)
1172 && (attr
->origin
!= BGP_ORIGIN_INCOMPLETE
))
1174 zlog_err ("Origin attribute value is invalid %d", attr
->origin
);
1175 return bgp_attr_malformed (args
,
1176 BGP_NOTIFY_UPDATE_INVAL_ORIGIN
,
1180 /* Set oring attribute flag. */
1181 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
);
1186 /* Parse AS path information. This function is wrapper of
1189 bgp_attr_aspath (struct bgp_attr_parser_args
*args
)
1191 struct attr
*const attr
= args
->attr
;
1192 struct peer
*const peer
= args
->peer
;
1193 const bgp_size_t length
= args
->length
;
1196 * peer with AS4 => will get 4Byte ASnums
1197 * otherwise, will get 16 Bit
1199 attr
->aspath
= aspath_parse (peer
->ibuf
, length
,
1200 CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
));
1202 /* In case of IBGP, length will be zero. */
1205 zlog_err ("Malformed AS path from %s, length is %d", peer
->host
, length
);
1206 return bgp_attr_malformed (args
, BGP_NOTIFY_UPDATE_MAL_AS_PATH
, 0);
1209 /* Set aspath attribute flag. */
1210 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
);
1212 return BGP_ATTR_PARSE_PROCEED
;
1215 static bgp_attr_parse_ret_t
1216 bgp_attr_aspath_check (struct peer
*const peer
, struct attr
*const attr
)
1218 /* These checks were part of bgp_attr_aspath, but with
1219 * as4 we should to check aspath things when
1220 * aspath synthesizing with as4_path has already taken place.
1221 * Otherwise we check ASPATH and use the synthesized thing, and that is
1223 * So do the checks later, i.e. here
1225 struct bgp
*bgp
= peer
->bgp
;
1226 struct aspath
*aspath
;
1228 /* Confederation sanity check. */
1229 if ((peer
->sort
== BGP_PEER_CONFED
&& ! aspath_left_confed_check (attr
->aspath
)) ||
1230 (peer
->sort
== BGP_PEER_EBGP
&& aspath_confed_check (attr
->aspath
)))
1232 zlog_err ("Malformed AS path from %s", peer
->host
);
1233 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1234 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1235 return BGP_ATTR_PARSE_ERROR
;
1238 /* First AS check for EBGP. */
1239 if (bgp
!= NULL
&& bgp_flag_check (bgp
, BGP_FLAG_ENFORCE_FIRST_AS
))
1241 if (peer
->sort
== BGP_PEER_EBGP
1242 && ! aspath_firstas_check (attr
->aspath
, peer
->as
))
1244 zlog_err ("%s incorrect first AS (must be %u)", peer
->host
, peer
->as
);
1245 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1246 BGP_NOTIFY_UPDATE_MAL_AS_PATH
);
1247 return BGP_ATTR_PARSE_ERROR
;
1251 /* local-as prepend */
1252 if (peer
->change_local_as
&&
1253 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
))
1255 aspath
= aspath_dup (attr
->aspath
);
1256 aspath
= aspath_add_seq (aspath
, peer
->change_local_as
);
1257 aspath_unintern (&attr
->aspath
);
1258 attr
->aspath
= aspath_intern (aspath
);
1261 return BGP_ATTR_PARSE_PROCEED
;
1264 /* Parse AS4 path information. This function is another wrapper of
1267 bgp_attr_as4_path (struct bgp_attr_parser_args
*args
, struct aspath
**as4_path
)
1269 struct peer
*const peer
= args
->peer
;
1270 struct attr
*const attr
= args
->attr
;
1271 const bgp_size_t length
= args
->length
;
1273 *as4_path
= aspath_parse (peer
->ibuf
, length
, 1);
1275 /* In case of IBGP, length will be zero. */
1278 zlog_err ("Malformed AS4 path from %s, length is %d", peer
->host
, length
);
1279 return bgp_attr_malformed (args
,
1280 BGP_NOTIFY_UPDATE_MAL_AS_PATH
,
1284 /* Set aspath attribute flag. */
1286 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH
);
1288 return BGP_ATTR_PARSE_PROCEED
;
1291 /* Nexthop attribute. */
1292 static bgp_attr_parse_ret_t
1293 bgp_attr_nexthop (struct bgp_attr_parser_args
*args
)
1295 struct peer
*const peer
= args
->peer
;
1296 struct attr
*const attr
= args
->attr
;
1297 const bgp_size_t length
= args
->length
;
1299 in_addr_t nexthop_h
, nexthop_n
;
1301 /* Check nexthop attribute length. */
1304 zlog_err ("Nexthop attribute length isn't four [%d]", length
);
1306 return bgp_attr_malformed (args
,
1307 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1311 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1312 attribute must result in a NOTIFICATION message (this is implemented below).
1313 At the same time, semantically incorrect NEXT_HOP is more likely to be just
1314 logged locally (this is implemented somewhere else). The UPDATE message
1315 gets ignored in any of these cases. */
1316 nexthop_n
= stream_get_ipv4 (peer
->ibuf
);
1317 nexthop_h
= ntohl (nexthop_n
);
1318 if ((IPV4_NET0 (nexthop_h
) || IPV4_NET127 (nexthop_h
) || IPV4_CLASS_DE (nexthop_h
))
1319 && !BGP_DEBUG (allow_martians
, ALLOW_MARTIANS
)) /* loopbacks may be used in testing */
1321 char buf
[INET_ADDRSTRLEN
];
1322 inet_ntop (AF_INET
, &nexthop_n
, buf
, INET_ADDRSTRLEN
);
1323 zlog_err ("Martian nexthop %s", buf
);
1324 return bgp_attr_malformed (args
,
1325 BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP
,
1329 attr
->nexthop
.s_addr
= nexthop_n
;
1330 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
);
1332 return BGP_ATTR_PARSE_PROCEED
;
1335 /* MED atrribute. */
1336 static bgp_attr_parse_ret_t
1337 bgp_attr_med (struct bgp_attr_parser_args
*args
)
1339 struct peer
*const peer
= args
->peer
;
1340 struct attr
*const attr
= args
->attr
;
1341 const bgp_size_t length
= args
->length
;
1346 zlog_err ("MED attribute length isn't four [%d]", length
);
1348 return bgp_attr_malformed (args
,
1349 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1353 attr
->med
= stream_getl (peer
->ibuf
);
1355 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
);
1357 return BGP_ATTR_PARSE_PROCEED
;
1360 /* Local preference attribute. */
1361 static bgp_attr_parse_ret_t
1362 bgp_attr_local_pref (struct bgp_attr_parser_args
*args
)
1364 struct peer
*const peer
= args
->peer
;
1365 struct attr
*const attr
= args
->attr
;
1366 const bgp_size_t length
= args
->length
;
1371 zlog_err ("LOCAL_PREF attribute length isn't 4 [%u]", length
);
1372 return bgp_attr_malformed (args
,
1373 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1377 /* If it is contained in an UPDATE message that is received from an
1378 external peer, then this attribute MUST be ignored by the
1379 receiving speaker. */
1380 if (peer
->sort
== BGP_PEER_EBGP
)
1382 stream_forward_getp (peer
->ibuf
, length
);
1383 return BGP_ATTR_PARSE_PROCEED
;
1386 attr
->local_pref
= stream_getl (peer
->ibuf
);
1388 /* Set atomic aggregate flag. */
1389 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
);
1391 return BGP_ATTR_PARSE_PROCEED
;
1394 /* Atomic aggregate. */
1396 bgp_attr_atomic (struct bgp_attr_parser_args
*args
)
1398 struct attr
*const attr
= args
->attr
;
1399 const bgp_size_t length
= args
->length
;
1404 zlog_err ("ATOMIC_AGGREGATE attribute length isn't 0 [%u]", length
);
1405 return bgp_attr_malformed (args
,
1406 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1410 /* Set atomic aggregate flag. */
1411 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
);
1413 return BGP_ATTR_PARSE_PROCEED
;
1416 /* Aggregator attribute */
1418 bgp_attr_aggregator (struct bgp_attr_parser_args
*args
)
1420 struct peer
*const peer
= args
->peer
;
1421 struct attr
*const attr
= args
->attr
;
1422 const bgp_size_t length
= args
->length
;
1425 struct attr_extra
*attre
= bgp_attr_extra_get (attr
);
1427 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1428 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
))
1431 if (length
!= wantedlen
)
1433 zlog_err ("AGGREGATOR attribute length isn't %u [%u]", wantedlen
, length
);
1434 return bgp_attr_malformed (args
,
1435 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1439 if ( CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) )
1440 attre
->aggregator_as
= stream_getl (peer
->ibuf
);
1442 attre
->aggregator_as
= stream_getw (peer
->ibuf
);
1443 attre
->aggregator_addr
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1445 /* Set atomic aggregate flag. */
1446 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
);
1448 return BGP_ATTR_PARSE_PROCEED
;
1451 /* New Aggregator attribute */
1452 static bgp_attr_parse_ret_t
1453 bgp_attr_as4_aggregator (struct bgp_attr_parser_args
*args
,
1454 as_t
*as4_aggregator_as
,
1455 struct in_addr
*as4_aggregator_addr
)
1457 struct peer
*const peer
= args
->peer
;
1458 struct attr
*const attr
= args
->attr
;
1459 const bgp_size_t length
= args
->length
;
1463 zlog_err ("New Aggregator length is not 8 [%d]", length
);
1464 return bgp_attr_malformed (args
,
1465 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1469 *as4_aggregator_as
= stream_getl (peer
->ibuf
);
1470 as4_aggregator_addr
->s_addr
= stream_get_ipv4 (peer
->ibuf
);
1472 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR
);
1474 return BGP_ATTR_PARSE_PROCEED
;
1477 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1479 static bgp_attr_parse_ret_t
1480 bgp_attr_munge_as4_attrs (struct peer
*const peer
,
1481 struct attr
*const attr
,
1482 struct aspath
*as4_path
, as_t as4_aggregator
,
1483 struct in_addr
*as4_aggregator_addr
)
1485 int ignore_as4_path
= 0;
1486 struct aspath
*newpath
;
1487 struct attr_extra
*attre
= attr
->extra
;
1491 /* NULL aspath shouldn't be possible as bgp_attr_parse should have
1492 * checked that all well-known, mandatory attributes were present.
1494 * Can only be a problem with peer itself - hard error
1496 return BGP_ATTR_PARSE_ERROR
;
1499 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
))
1501 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1503 * It is worth a warning though, because the peer really
1504 * should not send them
1506 if (BGP_DEBUG(as4
, AS4
))
1508 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH
)))
1509 zlog_debug ("[AS4] %s %s AS4_PATH",
1510 peer
->host
, "AS4 capable peer, yet it sent");
1512 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR
)))
1513 zlog_debug ("[AS4] %s %s AS4_AGGREGATOR",
1514 peer
->host
, "AS4 capable peer, yet it sent");
1517 return BGP_ATTR_PARSE_PROCEED
;
1520 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1521 * because that may override AS4_PATH
1523 if (attr
->flag
& (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR
) ) )
1525 if (attr
->flag
& (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
) ) )
1530 * if the as_number in aggregator is not AS_TRANS,
1531 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1532 * and the Aggregator shall be taken as
1533 * info on the aggregating node, and the AS_PATH
1534 * shall be taken as the AS_PATH
1536 * the Aggregator shall be ignored and the
1537 * AS4_AGGREGATOR shall be taken as the
1538 * Aggregating node and the AS_PATH is to be
1539 * constructed "as in all other cases"
1541 if (attre
->aggregator_as
!= BGP_AS_TRANS
)
1544 if ( BGP_DEBUG(as4
, AS4
))
1545 zlog_debug ("[AS4] %s BGP not AS4 capable peer"
1546 " send AGGREGATOR != AS_TRANS and"
1547 " AS4_AGGREGATOR, so ignore"
1548 " AS4_AGGREGATOR and AS4_PATH", peer
->host
);
1549 ignore_as4_path
= 1;
1553 /* "New_aggregator shall be taken as aggregator" */
1554 attre
->aggregator_as
= as4_aggregator
;
1555 attre
->aggregator_addr
.s_addr
= as4_aggregator_addr
->s_addr
;
1560 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1561 * That is bogus - but reading the conditions
1562 * we have to handle AS4_AGGREGATOR as if it were
1563 * AGGREGATOR in that case
1565 if ( BGP_DEBUG(as4
, AS4
))
1566 zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
1567 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1568 " it as if AGGREGATOR with AS_TRANS had been there", peer
->host
);
1569 (attre
= bgp_attr_extra_get (attr
))->aggregator_as
= as4_aggregator
;
1570 /* sweep it under the carpet and simulate a "good" AGGREGATOR */
1571 attr
->flag
|= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
));
1575 /* need to reconcile NEW_AS_PATH and AS_PATH */
1576 if (!ignore_as4_path
&& (attr
->flag
& (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH
))))
1578 newpath
= aspath_reconcile_as4 (attr
->aspath
, as4_path
);
1579 aspath_unintern (&attr
->aspath
);
1580 attr
->aspath
= aspath_intern (newpath
);
1582 return BGP_ATTR_PARSE_PROCEED
;
1585 /* Community attribute. */
1586 static bgp_attr_parse_ret_t
1587 bgp_attr_community (struct bgp_attr_parser_args
*args
)
1589 struct peer
*const peer
= args
->peer
;
1590 struct attr
*const attr
= args
->attr
;
1591 const bgp_size_t length
= args
->length
;
1595 attr
->community
= NULL
;
1596 return BGP_ATTR_PARSE_PROCEED
;
1600 community_parse ((u_int32_t
*)stream_pnt (peer
->ibuf
), length
);
1602 /* XXX: fix community_parse to use stream API and remove this */
1603 stream_forward_getp (peer
->ibuf
, length
);
1605 if (!attr
->community
)
1606 return bgp_attr_malformed (args
,
1607 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1610 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
);
1612 return BGP_ATTR_PARSE_PROCEED
;
1615 /* Originator ID attribute. */
1616 static bgp_attr_parse_ret_t
1617 bgp_attr_originator_id (struct bgp_attr_parser_args
*args
)
1619 struct peer
*const peer
= args
->peer
;
1620 struct attr
*const attr
= args
->attr
;
1621 const bgp_size_t length
= args
->length
;
1626 zlog_err ("Bad originator ID length %d", length
);
1628 return bgp_attr_malformed (args
,
1629 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1633 (bgp_attr_extra_get (attr
))->originator_id
.s_addr
1634 = stream_get_ipv4 (peer
->ibuf
);
1636 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID
);
1638 return BGP_ATTR_PARSE_PROCEED
;
1641 /* Cluster list attribute. */
1642 static bgp_attr_parse_ret_t
1643 bgp_attr_cluster_list (struct bgp_attr_parser_args
*args
)
1645 struct peer
*const peer
= args
->peer
;
1646 struct attr
*const attr
= args
->attr
;
1647 const bgp_size_t length
= args
->length
;
1652 zlog_err ("Bad cluster list length %d", length
);
1654 return bgp_attr_malformed (args
, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
1658 (bgp_attr_extra_get (attr
))->cluster
1659 = cluster_parse ((struct in_addr
*)stream_pnt (peer
->ibuf
), length
);
1661 /* XXX: Fix cluster_parse to use stream API and then remove this */
1662 stream_forward_getp (peer
->ibuf
, length
);
1664 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST
);
1666 return BGP_ATTR_PARSE_PROCEED
;
1669 /* Multiprotocol reachability information parse. */
1671 bgp_mp_reach_parse (struct bgp_attr_parser_args
*args
,
1672 struct bgp_nlri
*mp_update
)
1676 bgp_size_t nlri_len
;
1681 struct peer
*const peer
= args
->peer
;
1682 struct attr
*const attr
= args
->attr
;
1683 const bgp_size_t length
= args
->length
;
1684 struct attr_extra
*attre
= bgp_attr_extra_get(attr
);
1686 /* Set end of packet. */
1687 s
= BGP_INPUT(peer
);
1688 start
= stream_get_getp(s
);
1690 /* safe to read statically sized header? */
1691 #define BGP_MP_REACH_MIN_SIZE 5
1692 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1693 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_REACH_MIN_SIZE
))
1695 zlog_info ("%s: %s sent invalid length, %lu",
1696 __func__
, peer
->host
, (unsigned long)length
);
1697 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1700 /* Load AFI, SAFI. */
1701 afi
= stream_getw (s
);
1702 safi
= stream_getc (s
);
1704 /* Get nexthop length. */
1705 attre
->mp_nexthop_len
= stream_getc (s
);
1707 if (LEN_LEFT
< attre
->mp_nexthop_len
)
1709 zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute",
1710 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1711 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1714 /* Nexthop length check. */
1715 switch (attre
->mp_nexthop_len
)
1717 case BGP_ATTR_NHLEN_IPV4
:
1718 stream_get (&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1719 /* Probably needed for RFC 2283 */
1720 if (attr
->nexthop
.s_addr
== 0)
1721 memcpy(&attr
->nexthop
.s_addr
, &attre
->mp_nexthop_global_in
, IPV4_MAX_BYTELEN
);
1723 case BGP_ATTR_NHLEN_VPNV4
:
1724 stream_getl (s
); /* RD high */
1725 stream_getl (s
); /* RD low */
1726 stream_get (&attre
->mp_nexthop_global_in
, s
, IPV4_MAX_BYTELEN
);
1729 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
1730 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
1731 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
)
1733 stream_getl (s
); /* RD high */
1734 stream_getl (s
); /* RD low */
1736 stream_get (&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1738 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
1739 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
1740 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
1742 stream_getl (s
); /* RD high */
1743 stream_getl (s
); /* RD low */
1745 stream_get (&attre
->mp_nexthop_global
, s
, IPV6_MAX_BYTELEN
);
1746 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
1748 stream_getl (s
); /* RD high */
1749 stream_getl (s
); /* RD low */
1751 stream_get (&attre
->mp_nexthop_local
, s
, IPV6_MAX_BYTELEN
);
1752 if (! IN6_IS_ADDR_LINKLOCAL (&attre
->mp_nexthop_local
))
1754 char buf1
[INET6_ADDRSTRLEN
];
1755 char buf2
[INET6_ADDRSTRLEN
];
1757 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
1758 zlog_debug ("%s rcvd nexthops %s, %s -- ignoring non-LL value",
1760 inet_ntop (AF_INET6
, &attre
->mp_nexthop_global
,
1761 buf1
, INET6_ADDRSTRLEN
),
1762 inet_ntop (AF_INET6
, &attre
->mp_nexthop_local
,
1763 buf2
, INET6_ADDRSTRLEN
));
1765 attre
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
1768 #endif /* HAVE_IPV6 */
1770 zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d",
1771 __func__
, peer
->host
, attre
->mp_nexthop_len
);
1772 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1777 zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
1778 __func__
, peer
->host
);
1779 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1784 if ((val
= stream_getc (s
)))
1785 zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field",
1789 /* must have nrli_len, what is left of the attribute */
1790 nlri_len
= LEN_LEFT
;
1791 if ((!nlri_len
) || (nlri_len
> STREAM_READABLE(s
)))
1793 zlog_info ("%s: (%s) Failed to read NLRI",
1794 __func__
, peer
->host
);
1795 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1798 if (safi
!= SAFI_MPLS_LABELED_VPN
)
1800 ret
= bgp_nlri_sanity_check (peer
, afi
, safi
, stream_pnt (s
),
1801 nlri_len
, &num_mp_pfx
);
1804 zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
1805 __func__
, peer
->host
);
1806 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1810 mp_update
->afi
= afi
;
1811 mp_update
->safi
= safi
;
1812 mp_update
->nlri
= stream_pnt (s
);
1813 mp_update
->length
= nlri_len
;
1815 stream_forward_getp (s
, nlri_len
);
1817 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI
);
1819 return BGP_ATTR_PARSE_PROCEED
;
1823 /* Multiprotocol unreachable parse */
1825 bgp_mp_unreach_parse (struct bgp_attr_parser_args
*args
,
1826 struct bgp_nlri
*mp_withdraw
)
1831 u_int16_t withdraw_len
;
1834 struct peer
*const peer
= args
->peer
;
1835 struct attr
*const attr
= args
->attr
;
1836 const bgp_size_t length
= args
->length
;
1840 #define BGP_MP_UNREACH_MIN_SIZE 3
1841 if ((length
> STREAM_READABLE(s
)) || (length
< BGP_MP_UNREACH_MIN_SIZE
))
1842 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1844 afi
= stream_getw (s
);
1845 safi
= stream_getc (s
);
1847 withdraw_len
= length
- BGP_MP_UNREACH_MIN_SIZE
;
1849 if (safi
!= SAFI_MPLS_LABELED_VPN
)
1851 ret
= bgp_nlri_sanity_check (peer
, afi
, safi
, stream_pnt (s
),
1852 withdraw_len
, &num_mp_pfx
);
1854 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS
;
1857 mp_withdraw
->afi
= afi
;
1858 mp_withdraw
->safi
= safi
;
1859 mp_withdraw
->nlri
= stream_pnt (s
);
1860 mp_withdraw
->length
= withdraw_len
;
1862 stream_forward_getp (s
, withdraw_len
);
1864 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
);
1866 return BGP_ATTR_PARSE_PROCEED
;
1869 /* Extended Community attribute. */
1870 static bgp_attr_parse_ret_t
1871 bgp_attr_ext_communities (struct bgp_attr_parser_args
*args
)
1873 struct peer
*const peer
= args
->peer
;
1874 struct attr
*const attr
= args
->attr
;
1875 const bgp_size_t length
= args
->length
;
1880 attr
->extra
->ecommunity
= NULL
;
1881 /* Empty extcomm doesn't seem to be invalid per se */
1882 return BGP_ATTR_PARSE_PROCEED
;
1885 (bgp_attr_extra_get (attr
))->ecommunity
=
1886 ecommunity_parse ((u_int8_t
*)stream_pnt (peer
->ibuf
), length
);
1887 /* XXX: fix ecommunity_parse to use stream API */
1888 stream_forward_getp (peer
->ibuf
, length
);
1890 if (!attr
->extra
->ecommunity
)
1891 return bgp_attr_malformed (args
,
1892 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1895 attr
->flag
|= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
);
1897 return BGP_ATTR_PARSE_PROCEED
;
1900 /* Parse Tunnel Encap attribute in an UPDATE */
1904 struct peer
*peer
, /* IN */
1905 bgp_size_t length
, /* IN: attr's length field */
1906 struct attr
*attr
, /* IN: caller already allocated */
1907 u_char flag
, /* IN: attr's flags field */
1911 struct attr_extra
*attre
= NULL
;
1912 struct bgp_attr_encap_subtlv
*stlv_last
= NULL
;
1913 uint16_t tunneltype
;
1915 total
= length
+ (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
) ? 4 : 3);
1917 if (!CHECK_FLAG(flag
, BGP_ATTR_FLAG_TRANS
)
1918 || !CHECK_FLAG(flag
, BGP_ATTR_FLAG_OPTIONAL
))
1920 zlog_info ("Tunnel Encap attribute flag isn't optional and transitive %d", flag
);
1921 bgp_notify_send_with_data (peer
,
1922 BGP_NOTIFY_UPDATE_ERR
,
1923 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
1928 if (BGP_ATTR_ENCAP
== type
) {
1929 /* read outer TLV type and length */
1930 uint16_t tlv_length
;
1933 zlog_info ("Tunnel Encap attribute not long enough to contain outer T,L");
1934 bgp_notify_send_with_data(peer
,
1935 BGP_NOTIFY_UPDATE_ERR
,
1936 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1940 tunneltype
= stream_getw (BGP_INPUT (peer
));
1941 tlv_length
= stream_getw (BGP_INPUT (peer
));
1944 if (tlv_length
!= length
) {
1945 zlog_info ("%s: tlv_length(%d) != length(%d)",
1946 __func__
, tlv_length
, length
);
1950 while (length
>= 4) {
1951 uint16_t subtype
= 0;
1952 uint16_t sublength
= 0;
1953 struct bgp_attr_encap_subtlv
*tlv
;
1955 if (BGP_ATTR_ENCAP
== type
) {
1956 subtype
= stream_getc (BGP_INPUT (peer
));
1957 sublength
= stream_getc (BGP_INPUT (peer
));
1961 if (sublength
> length
) {
1962 zlog_info ("Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1964 bgp_notify_send_with_data (peer
,
1965 BGP_NOTIFY_UPDATE_ERR
,
1966 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
1971 /* alloc and copy sub-tlv */
1972 /* TBD make sure these are freed when attributes are released */
1973 tlv
= XCALLOC (MTYPE_ENCAP_TLV
, sizeof(struct bgp_attr_encap_subtlv
)-1+sublength
);
1974 tlv
->type
= subtype
;
1975 tlv
->length
= sublength
;
1976 stream_get(tlv
->value
, peer
->ibuf
, sublength
);
1977 length
-= sublength
;
1979 /* attach tlv to encap chain */
1981 attre
= bgp_attr_extra_get(attr
);
1982 if (BGP_ATTR_ENCAP
== type
) {
1983 for (stlv_last
= attre
->encap_subtlvs
; stlv_last
&& stlv_last
->next
;
1984 stlv_last
= stlv_last
->next
);
1986 stlv_last
->next
= tlv
;
1988 attre
->encap_subtlvs
= tlv
;
1992 stlv_last
->next
= tlv
;
1997 if (attre
&& (BGP_ATTR_ENCAP
== type
)) {
1998 attre
->encap_tunneltype
= tunneltype
;
2002 /* spurious leftover data */
2003 zlog_info ("Tunnel Encap attribute length is bad: %d leftover octets", length
);
2004 bgp_notify_send_with_data (peer
,
2005 BGP_NOTIFY_UPDATE_ERR
,
2006 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
,
2014 /* BGP unknown attribute treatment. */
2015 static bgp_attr_parse_ret_t
2016 bgp_attr_unknown (struct bgp_attr_parser_args
*args
)
2018 bgp_size_t total
= args
->total
;
2019 struct transit
*transit
;
2020 struct attr_extra
*attre
;
2021 struct peer
*const peer
= args
->peer
;
2022 struct attr
*const attr
= args
->attr
;
2023 u_char
*const startp
= args
->startp
;
2024 const u_char type
= args
->type
;
2025 const u_char flag
= args
->flags
;
2026 const bgp_size_t length
= args
->length
;
2028 if (bgp_debug_update(peer
, NULL
, NULL
, 1))
2029 zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
2030 peer
->host
, type
, length
);
2032 /* Forward read pointer of input stream. */
2033 stream_forward_getp (peer
->ibuf
, length
);
2035 /* If any of the mandatory well-known attributes are not recognized,
2036 then the Error Subcode is set to Unrecognized Well-known
2037 Attribute. The Data field contains the unrecognized attribute
2038 (type, length and value). */
2039 if (!CHECK_FLAG (flag
, BGP_ATTR_FLAG_OPTIONAL
))
2041 return bgp_attr_malformed (args
,
2042 BGP_NOTIFY_UPDATE_UNREC_ATTR
,
2046 /* Unrecognized non-transitive optional attributes must be quietly
2047 ignored and not passed along to other BGP peers. */
2048 if (! CHECK_FLAG (flag
, BGP_ATTR_FLAG_TRANS
))
2049 return BGP_ATTR_PARSE_PROCEED
;
2051 /* If a path with recognized transitive optional attribute is
2052 accepted and passed along to other BGP peers and the Partial bit
2053 in the Attribute Flags octet is set to 1 by some previous AS, it
2054 is not set back to 0 by the current AS. */
2055 SET_FLAG (*startp
, BGP_ATTR_FLAG_PARTIAL
);
2057 /* Store transitive attribute to the end of attr->transit. */
2058 if (! ((attre
= bgp_attr_extra_get(attr
))->transit
) )
2059 attre
->transit
= XCALLOC (MTYPE_TRANSIT
, sizeof (struct transit
));
2061 transit
= attre
->transit
;
2064 transit
->val
= XREALLOC (MTYPE_TRANSIT_VAL
, transit
->val
,
2065 transit
->length
+ total
);
2067 transit
->val
= XMALLOC (MTYPE_TRANSIT_VAL
, total
);
2069 memcpy (transit
->val
+ transit
->length
, startp
, total
);
2070 transit
->length
+= total
;
2072 return BGP_ATTR_PARSE_PROCEED
;
2075 /* Well-known attribute check. */
2077 bgp_attr_check (struct peer
*peer
, struct attr
*attr
)
2081 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2083 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
) && !attr
->flag
)
2084 return BGP_ATTR_PARSE_PROCEED
;
2086 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2087 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2088 are present, it should. Check for any other attribute being present
2091 if (attr
->flag
== ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
))
2092 return BGP_ATTR_PARSE_PROCEED
;
2094 if (! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN
)))
2095 type
= BGP_ATTR_ORIGIN
;
2097 if (! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
)))
2098 type
= BGP_ATTR_AS_PATH
;
2100 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present and
2101 * NLRI is empty. We can't easily check NLRI empty here though.
2103 if (!CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
))
2104 && !CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_MP_REACH_NLRI
)))
2105 type
= BGP_ATTR_NEXT_HOP
;
2107 if (peer
->sort
== BGP_PEER_IBGP
2108 && ! CHECK_FLAG (attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
)))
2109 type
= BGP_ATTR_LOCAL_PREF
;
2113 zlog_warn ("%s Missing well-known attribute %s.", peer
->host
,
2114 LOOKUP (attr_str
, type
));
2115 bgp_notify_send_with_data (peer
,
2116 BGP_NOTIFY_UPDATE_ERR
,
2117 BGP_NOTIFY_UPDATE_MISS_ATTR
,
2119 return BGP_ATTR_PARSE_ERROR
;
2121 return BGP_ATTR_PARSE_PROCEED
;
2124 /* Read attribute of update packet. This function is called from
2125 bgp_update_receive() in bgp_packet.c. */
2126 bgp_attr_parse_ret_t
2127 bgp_attr_parse (struct peer
*peer
, struct attr
*attr
, bgp_size_t size
,
2128 struct bgp_nlri
*mp_update
, struct bgp_nlri
*mp_withdraw
)
2134 u_char
*startp
, *endp
;
2136 u_char seen
[BGP_ATTR_BITMAP_SIZE
];
2137 /* we need the as4_path only until we have synthesized the as_path with it */
2138 /* same goes for as4_aggregator */
2139 struct aspath
*as4_path
= NULL
;
2140 as_t as4_aggregator
= 0;
2141 struct in_addr as4_aggregator_addr
= { .s_addr
= 0 };
2143 /* Initialize bitmap. */
2144 memset (seen
, 0, BGP_ATTR_BITMAP_SIZE
);
2146 /* End pointer of BGP attribute. */
2147 endp
= BGP_INPUT_PNT (peer
) + size
;
2149 /* Get attributes to the end of attribute length. */
2150 while (BGP_INPUT_PNT (peer
) < endp
)
2152 /* Check remaining length check.*/
2153 if (endp
- BGP_INPUT_PNT (peer
) < BGP_ATTR_MIN_LEN
)
2155 /* XXX warning: long int format, int arg (arg 5) */
2156 zlog_warn ("%s: error BGP attribute length %lu is smaller than min len",
2158 (unsigned long) (endp
- STREAM_PNT (BGP_INPUT (peer
))));
2160 bgp_notify_send (peer
,
2161 BGP_NOTIFY_UPDATE_ERR
,
2162 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2163 return BGP_ATTR_PARSE_ERROR
;
2166 /* Fetch attribute flag and type. */
2167 startp
= BGP_INPUT_PNT (peer
);
2168 /* "The lower-order four bits of the Attribute Flags octet are
2169 unused. They MUST be zero when sent and MUST be ignored when
2171 flag
= 0xF0 & stream_getc (BGP_INPUT (peer
));
2172 type
= stream_getc (BGP_INPUT (peer
));
2174 /* Check whether Extended-Length applies and is in bounds */
2175 if (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
)
2176 && ((endp
- startp
) < (BGP_ATTR_MIN_LEN
+ 1)))
2178 zlog_warn ("%s: Extended length set, but just %lu bytes of attr header",
2180 (unsigned long) (endp
- STREAM_PNT (BGP_INPUT (peer
))));
2182 bgp_notify_send (peer
,
2183 BGP_NOTIFY_UPDATE_ERR
,
2184 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2185 return BGP_ATTR_PARSE_ERROR
;
2188 /* Check extended attribue length bit. */
2189 if (CHECK_FLAG (flag
, BGP_ATTR_FLAG_EXTLEN
))
2190 length
= stream_getw (BGP_INPUT (peer
));
2192 length
= stream_getc (BGP_INPUT (peer
));
2194 /* If any attribute appears more than once in the UPDATE
2195 message, then the Error Subcode is set to Malformed Attribute
2198 if (CHECK_BITMAP (seen
, type
))
2200 zlog_warn ("%s: error BGP attribute type %d appears twice in a message",
2203 bgp_notify_send (peer
,
2204 BGP_NOTIFY_UPDATE_ERR
,
2205 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2206 return BGP_ATTR_PARSE_ERROR
;
2209 /* Set type to bitmap to check duplicate attribute. `type' is
2210 unsigned char so it never overflow bitmap range. */
2212 SET_BITMAP (seen
, type
);
2214 /* Overflow check. */
2215 attr_endp
= BGP_INPUT_PNT (peer
) + length
;
2217 if (attr_endp
> endp
)
2219 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
);
2220 bgp_notify_send_with_data (peer
,
2221 BGP_NOTIFY_UPDATE_ERR
,
2222 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
,
2223 startp
, attr_endp
- startp
);
2224 return BGP_ATTR_PARSE_ERROR
;
2227 struct bgp_attr_parser_args attr_args
= {
2234 .total
= attr_endp
- startp
,
2238 /* If any recognized attribute has Attribute Flags that conflict
2239 with the Attribute Type Code, then the Error Subcode is set to
2240 Attribute Flags Error. The Data field contains the erroneous
2241 attribute (type, length and value). */
2242 if (bgp_attr_flag_invalid (&attr_args
))
2244 bgp_attr_parse_ret_t ret
;
2245 ret
= bgp_attr_malformed (&attr_args
,
2246 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR
,
2248 if (ret
== BGP_ATTR_PARSE_PROCEED
)
2253 /* OK check attribute and store it's value. */
2256 case BGP_ATTR_ORIGIN
:
2257 ret
= bgp_attr_origin (&attr_args
);
2259 case BGP_ATTR_AS_PATH
:
2260 ret
= bgp_attr_aspath (&attr_args
);
2262 case BGP_ATTR_AS4_PATH
:
2263 ret
= bgp_attr_as4_path (&attr_args
, &as4_path
);
2265 case BGP_ATTR_NEXT_HOP
:
2266 ret
= bgp_attr_nexthop (&attr_args
);
2268 case BGP_ATTR_MULTI_EXIT_DISC
:
2269 ret
= bgp_attr_med (&attr_args
);
2271 case BGP_ATTR_LOCAL_PREF
:
2272 ret
= bgp_attr_local_pref (&attr_args
);
2274 case BGP_ATTR_ATOMIC_AGGREGATE
:
2275 ret
= bgp_attr_atomic (&attr_args
);
2277 case BGP_ATTR_AGGREGATOR
:
2278 ret
= bgp_attr_aggregator (&attr_args
);
2280 case BGP_ATTR_AS4_AGGREGATOR
:
2281 ret
= bgp_attr_as4_aggregator (&attr_args
,
2283 &as4_aggregator_addr
);
2285 case BGP_ATTR_COMMUNITIES
:
2286 ret
= bgp_attr_community (&attr_args
);
2288 case BGP_ATTR_ORIGINATOR_ID
:
2289 ret
= bgp_attr_originator_id (&attr_args
);
2291 case BGP_ATTR_CLUSTER_LIST
:
2292 ret
= bgp_attr_cluster_list (&attr_args
);
2294 case BGP_ATTR_MP_REACH_NLRI
:
2295 ret
= bgp_mp_reach_parse (&attr_args
, mp_update
);
2297 case BGP_ATTR_MP_UNREACH_NLRI
:
2298 ret
= bgp_mp_unreach_parse (&attr_args
, mp_withdraw
);
2300 case BGP_ATTR_EXT_COMMUNITIES
:
2301 ret
= bgp_attr_ext_communities (&attr_args
);
2303 case BGP_ATTR_ENCAP
:
2304 ret
= bgp_attr_encap (type
, peer
, length
, attr
, flag
, startp
);
2307 ret
= bgp_attr_unknown (&attr_args
);
2311 if (ret
== BGP_ATTR_PARSE_ERROR_NOTIFYPLS
)
2313 bgp_notify_send (peer
,
2314 BGP_NOTIFY_UPDATE_ERR
,
2315 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2316 ret
= BGP_ATTR_PARSE_ERROR
;
2319 /* If hard error occured immediately return to the caller. */
2320 if (ret
== BGP_ATTR_PARSE_ERROR
)
2322 zlog_warn ("%s: Attribute %s, parse error",
2324 LOOKUP (attr_str
, type
));
2326 aspath_unintern (&as4_path
);
2329 if (ret
== BGP_ATTR_PARSE_WITHDRAW
)
2332 zlog_warn ("%s: Attribute %s, parse error - treating as withdrawal",
2334 LOOKUP (attr_str
, type
));
2336 aspath_unintern (&as4_path
);
2340 /* Check the fetched length. */
2341 if (BGP_INPUT_PNT (peer
) != attr_endp
)
2343 zlog_warn ("%s: BGP attribute %s, fetch error",
2344 peer
->host
, LOOKUP (attr_str
, type
));
2345 bgp_notify_send (peer
,
2346 BGP_NOTIFY_UPDATE_ERR
,
2347 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2349 aspath_unintern (&as4_path
);
2350 return BGP_ATTR_PARSE_ERROR
;
2354 /* Check final read pointer is same as end pointer. */
2355 if (BGP_INPUT_PNT (peer
) != endp
)
2357 zlog_warn ("%s: BGP attribute %s, length mismatch",
2358 peer
->host
, LOOKUP (attr_str
, type
));
2359 bgp_notify_send (peer
,
2360 BGP_NOTIFY_UPDATE_ERR
,
2361 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR
);
2363 aspath_unintern (&as4_path
);
2364 return BGP_ATTR_PARSE_ERROR
;
2367 /* Check all mandatory well-known attributes are present */
2369 bgp_attr_parse_ret_t ret
;
2370 if ((ret
= bgp_attr_check (peer
, attr
)) < 0)
2373 aspath_unintern (&as4_path
);
2379 * At this place we can see whether we got AS4_PATH and/or
2380 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2381 * We can not do this before we've read all attributes because
2382 * the as4 handling does not say whether AS4_PATH has to be sent
2383 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2384 * in relationship to AGGREGATOR.
2385 * So, to be defensive, we are not relying on any order and read
2386 * all attributes first, including these 32bit ones, and now,
2387 * afterwards, we look what and if something is to be done for as4.
2389 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2392 /* actually... this doesn't ever return failure currently, but
2393 * better safe than sorry */
2394 if (CHECK_FLAG(attr
->flag
, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH
))
2395 && bgp_attr_munge_as4_attrs (peer
, attr
, as4_path
,
2396 as4_aggregator
, &as4_aggregator_addr
))
2398 bgp_notify_send (peer
,
2399 BGP_NOTIFY_UPDATE_ERR
,
2400 BGP_NOTIFY_UPDATE_MAL_ATTR
);
2402 aspath_unintern (&as4_path
);
2403 return BGP_ATTR_PARSE_ERROR
;
2406 /* At this stage, we have done all fiddling with as4, and the
2407 * resulting info is in attr->aggregator resp. attr->aspath
2408 * so we can chuck as4_aggregator and as4_path alltogether in
2409 * order to save memory
2413 aspath_unintern (&as4_path
); /* unintern - it is in the hash */
2414 /* The flag that we got this is still there, but that does not
2419 * The "rest" of the code does nothing with as4_aggregator.
2420 * there is no memory attached specifically which is not part
2422 * so ignoring just means do nothing.
2425 * Finally do the checks on the aspath we did not do yet
2426 * because we waited for a potentially synthesized aspath.
2428 if (attr
->flag
& (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH
)))
2430 ret
= bgp_attr_aspath_check (peer
, attr
);
2431 if (ret
!= BGP_ATTR_PARSE_PROCEED
)
2435 /* Finally intern unknown attribute. */
2436 if (attr
->extra
&& attr
->extra
->transit
)
2437 attr
->extra
->transit
= transit_intern (attr
->extra
->transit
);
2439 return BGP_ATTR_PARSE_PROCEED
;
2443 bgp_packet_mpattr_start (struct stream
*s
, afi_t afi
, safi_t safi
, afi_t nh_afi
,
2444 struct bpacket_attr_vec_arr
*vecarr
,
2449 /* Set extended bit always to encode the attribute length as 2 bytes */
2450 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
2451 stream_putc (s
, BGP_ATTR_MP_REACH_NLRI
);
2452 sizep
= stream_get_endp (s
);
2453 stream_putw (s
, 0); /* Marker: Attribute length. */
2455 stream_putw (s
, afi
);
2456 stream_putc (s
, (safi
== SAFI_MPLS_VPN
) ? SAFI_MPLS_LABELED_VPN
: safi
);
2465 case SAFI_MULTICAST
:
2466 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2468 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
2471 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2472 stream_putc (s
, 12);
2473 stream_putl (s
, 0); /* RD = 0, per RFC */
2475 stream_put (s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2479 stream_put (s
, &attr
->extra
->mp_nexthop_global_in
, 4);
2490 case SAFI_MULTICAST
:
2492 struct attr_extra
*attre
= attr
->extra
;
2494 assert (attr
->extra
);
2495 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2496 stream_putc (s
, attre
->mp_nexthop_len
);
2497 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2498 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
2499 stream_put (s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
2504 struct attr_extra
*attre
= attr
->extra
;
2506 assert (attr
->extra
);
2507 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
2508 stream_putc (s
, 24);
2509 stream_putl (s
, 0); /* RD = 0, per RFC */
2511 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2512 } else if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
2513 stream_putc (s
, 48);
2514 stream_putl (s
, 0); /* RD = 0, per RFC */
2516 stream_put (s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2517 stream_putl (s
, 0); /* RD = 0, per RFC */
2519 stream_put (s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
2524 assert (attr
->extra
);
2525 stream_putc (s
, IPV6_MAX_BYTELEN
);
2526 stream_put (s
, &attr
->extra
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
2532 #endif /*HAVE_IPV6*/
2543 bgp_packet_mpattr_prefix (struct stream
*s
, afi_t afi
, safi_t safi
,
2544 struct prefix
*p
, struct prefix_rd
*prd
,
2545 u_char
*tag
, int addpath_encode
,
2546 u_int32_t addpath_tx_id
)
2548 if (safi
== SAFI_MPLS_VPN
)
2551 stream_putl(s
, addpath_tx_id
);
2552 /* Tag, RD, Prefix write. */
2553 stream_putc (s
, p
->prefixlen
+ 88);
2554 stream_put (s
, tag
, 3);
2555 stream_put (s
, prd
->val
, 8);
2556 stream_put (s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
2559 stream_put_prefix_addpath (s
, p
, addpath_encode
, addpath_tx_id
);
2563 bgp_packet_mpattr_prefix_size (afi_t afi
, safi_t safi
, struct prefix
*p
)
2565 int size
= PSIZE (p
->prefixlen
);
2566 if (safi
== SAFI_MPLS_VPN
)
2572 * Encodes the tunnel encapsulation attribute
2575 bgp_packet_mpattr_tea(
2582 unsigned int attrlenfield
= 0;
2583 unsigned int attrhdrlen
= 0;
2584 struct bgp_attr_encap_subtlv
*subtlvs
;
2585 struct bgp_attr_encap_subtlv
*st
;
2586 const char *attrname
;
2588 if (!attr
|| !attr
->extra
)
2592 case BGP_ATTR_ENCAP
:
2593 attrname
= "Tunnel Encap";
2594 subtlvs
= attr
->extra
->encap_subtlvs
;
2597 * The tunnel encap attr has an "outer" tlv.
2599 * L = total length of subtlvs,
2600 * V = concatenated subtlvs.
2602 attrlenfield
= 2 + 2; /* T + L */
2603 attrhdrlen
= 1 + 1; /* subTLV T + L */
2611 /* if no tlvs, don't make attr */
2612 if (subtlvs
== NULL
)
2615 /* compute attr length */
2616 for (st
= subtlvs
; st
; st
= st
->next
) {
2617 attrlenfield
+= (attrhdrlen
+ st
->length
);
2620 if (attrlenfield
> 0xffff) {
2621 zlog_info ("%s attribute is too long (length=%d), can't send it",
2627 if (attrlenfield
> 0xff) {
2628 /* 2-octet length field */
2630 BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
2631 stream_putc (s
, attrtype
);
2632 stream_putw (s
, attrlenfield
& 0xffff);
2634 /* 1-octet length field */
2635 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
);
2636 stream_putc (s
, attrtype
);
2637 stream_putc (s
, attrlenfield
& 0xff);
2640 if (attrtype
== BGP_ATTR_ENCAP
) {
2641 /* write outer T+L */
2642 stream_putw(s
, attr
->extra
->encap_tunneltype
);
2643 stream_putw(s
, attrlenfield
- 4);
2646 /* write each sub-tlv */
2647 for (st
= subtlvs
; st
; st
= st
->next
) {
2648 if (attrtype
== BGP_ATTR_ENCAP
) {
2649 stream_putc (s
, st
->type
);
2650 stream_putc (s
, st
->length
);
2652 stream_put (s
, st
->value
, st
->length
);
2657 bgp_packet_mpattr_end (struct stream
*s
, size_t sizep
)
2659 /* Set MP attribute length. Don't count the (2) bytes used to encode
2661 stream_putw_at (s
, sizep
, (stream_get_endp (s
) - sizep
) - 2);
2664 /* Make attribute packet. */
2666 bgp_packet_attribute (struct bgp
*bgp
, struct peer
*peer
,
2667 struct stream
*s
, struct attr
*attr
,
2668 struct bpacket_attr_vec_arr
*vecarr
,
2669 struct prefix
*p
, afi_t afi
, safi_t safi
,
2670 struct peer
*from
, struct prefix_rd
*prd
, u_char
*tag
,
2672 u_int32_t addpath_tx_id
)
2675 size_t aspath_sizep
;
2676 struct aspath
*aspath
;
2677 int send_as4_path
= 0;
2678 int send_as4_aggregator
= 0;
2679 int use32bit
= (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)) ? 1 : 0;
2684 /* Remember current pointer. */
2685 cp
= stream_get_endp (s
);
2687 if (p
&& !((afi
== AFI_IP
&& safi
== SAFI_UNICAST
) &&
2688 !peer_cap_enhe(peer
)))
2690 size_t mpattrlen_pos
= 0;
2692 mpattrlen_pos
= bgp_packet_mpattr_start(s
, afi
, safi
,
2693 (peer_cap_enhe(peer
) ? AFI_IP6
: afi
),
2695 bgp_packet_mpattr_prefix(s
, afi
, safi
, p
, prd
, tag
,
2696 addpath_encode
, addpath_tx_id
);
2697 bgp_packet_mpattr_end(s
, mpattrlen_pos
);
2700 /* Origin attribute. */
2701 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2702 stream_putc (s
, BGP_ATTR_ORIGIN
);
2704 stream_putc (s
, attr
->origin
);
2706 /* AS path attribute. */
2708 /* If remote-peer is EBGP */
2709 if (peer
->sort
== BGP_PEER_EBGP
2710 && (! CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_AS_PATH_UNCHANGED
)
2711 || attr
->aspath
->segments
== NULL
)
2712 && (! CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
)))
2714 aspath
= aspath_dup (attr
->aspath
);
2716 /* Even though we may not be configured for confederations we may have
2717 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2718 aspath
= aspath_delete_confed_seq (aspath
);
2720 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
2722 /* Stuff our path CONFED_ID on the front */
2723 aspath
= aspath_add_seq (aspath
, bgp
->confed_id
);
2727 if (peer
->change_local_as
) {
2728 /* If replace-as is specified, we only use the change_local_as when
2729 advertising routes. */
2730 if( ! CHECK_FLAG (peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
) ) {
2731 aspath
= aspath_add_seq (aspath
, peer
->local_as
);
2733 aspath
= aspath_add_seq (aspath
, peer
->change_local_as
);
2735 aspath
= aspath_add_seq (aspath
, peer
->local_as
);
2739 else if (peer
->sort
== BGP_PEER_CONFED
)
2741 /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
2742 aspath
= aspath_dup (attr
->aspath
);
2743 aspath
= aspath_add_confed_seq (aspath
, peer
->local_as
);
2746 aspath
= attr
->aspath
;
2748 /* If peer is not AS4 capable, then:
2749 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2750 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
2751 * types are in it (i.e. exclude them if they are there)
2752 * AND do this only if there is at least one asnum > 65535 in the path!
2753 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
2754 * all ASnums > 65535 to BGP_AS_TRANS
2757 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2758 stream_putc (s
, BGP_ATTR_AS_PATH
);
2759 aspath_sizep
= stream_get_endp (s
);
2761 stream_putw_at (s
, aspath_sizep
, aspath_put (s
, aspath
, use32bit
));
2763 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2766 if (!use32bit
&& aspath_has_as4 (aspath
))
2767 send_as4_path
= 1; /* we'll do this later, at the correct place */
2769 /* Nexthop attribute. */
2770 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&& !peer_cap_enhe(peer
))
2772 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP
))
2774 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2775 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
2776 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, attr
);
2778 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
2780 else if (safi
== SAFI_UNICAST
&& peer_cap_enhe(from
))
2783 * Likely this is the case when an IPv4 prefix was received with
2784 * Extended Next-hop capability and now being advertised to
2786 * Setting the mandatory (ipv4) next-hop attribute here to enable
2787 * implicit next-hop self with correct (ipv4 address family).
2789 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2790 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
2791 bpacket_attr_vec_arr_set_vec (vecarr
, BGP_ATTR_VEC_NH
, s
, NULL
);
2793 stream_put_ipv4 (s
, 0);
2797 /* MED attribute. */
2798 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
) ||
2801 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2802 stream_putc (s
, BGP_ATTR_MULTI_EXIT_DISC
);
2804 stream_putl (s
, (bgp
->maxmed_active
? bgp
->maxmed_value
: attr
->med
));
2807 /* Local preference. */
2808 if (peer
->sort
== BGP_PEER_IBGP
||
2809 peer
->sort
== BGP_PEER_CONFED
)
2811 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2812 stream_putc (s
, BGP_ATTR_LOCAL_PREF
);
2814 stream_putl (s
, attr
->local_pref
);
2817 /* Atomic aggregate. */
2818 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
))
2820 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
2821 stream_putc (s
, BGP_ATTR_ATOMIC_AGGREGATE
);
2826 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
))
2828 assert (attr
->extra
);
2830 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
2831 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2832 stream_putc (s
, BGP_ATTR_AGGREGATOR
);
2836 /* AS4 capable peer */
2838 stream_putl (s
, attr
->extra
->aggregator_as
);
2842 /* 2-byte AS peer */
2845 /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
2846 if ( attr
->extra
->aggregator_as
> 65535 )
2848 stream_putw (s
, BGP_AS_TRANS
);
2850 /* we have to send AS4_AGGREGATOR, too.
2851 * we'll do that later in order to send attributes in ascending
2854 send_as4_aggregator
= 1;
2857 stream_putw (s
, (u_int16_t
) attr
->extra
->aggregator_as
);
2859 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
2862 /* Community attribute. */
2863 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
)
2864 && (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
)))
2866 if (attr
->community
->size
* 4 > 255)
2868 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2869 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
2870 stream_putw (s
, attr
->community
->size
* 4);
2874 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2875 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
2876 stream_putc (s
, attr
->community
->size
* 4);
2878 stream_put (s
, attr
->community
->val
, attr
->community
->size
* 4);
2881 /* Route Reflector. */
2882 if (peer
->sort
== BGP_PEER_IBGP
2884 && from
->sort
== BGP_PEER_IBGP
)
2886 /* Originator ID. */
2887 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2888 stream_putc (s
, BGP_ATTR_ORIGINATOR_ID
);
2891 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
2892 stream_put_in_addr (s
, &attr
->extra
->originator_id
);
2894 stream_put_in_addr (s
, &from
->remote_id
);
2897 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
2898 stream_putc (s
, BGP_ATTR_CLUSTER_LIST
);
2900 if (attr
->extra
&& attr
->extra
->cluster
)
2902 stream_putc (s
, attr
->extra
->cluster
->length
+ 4);
2903 /* If this peer configuration's parent BGP has cluster_id. */
2904 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
2905 stream_put_in_addr (s
, &bgp
->cluster_id
);
2907 stream_put_in_addr (s
, &bgp
->router_id
);
2908 stream_put (s
, attr
->extra
->cluster
->list
,
2909 attr
->extra
->cluster
->length
);
2914 /* If this peer configuration's parent BGP has cluster_id. */
2915 if (bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
2916 stream_put_in_addr (s
, &bgp
->cluster_id
);
2918 stream_put_in_addr (s
, &bgp
->router_id
);
2922 /* Extended Communities attribute. */
2923 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_EXT_COMMUNITY
)
2924 && (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
)))
2926 struct attr_extra
*attre
= attr
->extra
;
2930 if (peer
->sort
== BGP_PEER_IBGP
2931 || peer
->sort
== BGP_PEER_CONFED
)
2933 if (attre
->ecommunity
->size
* 8 > 255)
2935 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2936 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
2937 stream_putw (s
, attre
->ecommunity
->size
* 8);
2941 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2942 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
2943 stream_putc (s
, attre
->ecommunity
->size
* 8);
2945 stream_put (s
, attre
->ecommunity
->val
, attre
->ecommunity
->size
* 8);
2951 int ecom_tr_size
= 0;
2954 for (i
= 0; i
< attre
->ecommunity
->size
; i
++)
2956 pnt
= attre
->ecommunity
->val
+ (i
* 8);
2959 if (CHECK_FLAG (tbit
, ECOMMUNITY_FLAG_NON_TRANSITIVE
))
2967 if (ecom_tr_size
* 8 > 255)
2969 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
2970 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
2971 stream_putw (s
, ecom_tr_size
* 8);
2975 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
2976 stream_putc (s
, BGP_ATTR_EXT_COMMUNITIES
);
2977 stream_putc (s
, ecom_tr_size
* 8);
2980 for (i
= 0; i
< attre
->ecommunity
->size
; i
++)
2982 pnt
= attre
->ecommunity
->val
+ (i
* 8);
2985 if (CHECK_FLAG (tbit
, ECOMMUNITY_FLAG_NON_TRANSITIVE
))
2988 stream_put (s
, pnt
, 8);
2994 if ( send_as4_path
)
2996 /* If the peer is NOT As4 capable, AND */
2997 /* there are ASnums > 65535 in path THEN
2998 * give out AS4_PATH */
3000 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3002 * Hm, I wonder... confederation things *should* only be at
3003 * the beginning of an aspath, right? Then we should use
3004 * aspath_delete_confed_seq for this, because it is already
3006 * Folks, talk to me: what is reasonable here!?
3008 aspath
= aspath_delete_confed_seq (aspath
);
3010 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
3011 stream_putc (s
, BGP_ATTR_AS4_PATH
);
3012 aspath_sizep
= stream_get_endp (s
);
3014 stream_putw_at (s
, aspath_sizep
, aspath_put (s
, aspath
, 1));
3017 if (aspath
!= attr
->aspath
)
3018 aspath_free (aspath
);
3020 if ( send_as4_aggregator
)
3022 assert (attr
->extra
);
3024 /* send AS4_AGGREGATOR, at this place */
3025 /* this section of code moved here in order to ensure the correct
3026 * *ascending* order of attributes
3028 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3029 stream_putc (s
, BGP_ATTR_AS4_AGGREGATOR
);
3031 stream_putl (s
, attr
->extra
->aggregator_as
);
3032 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
3035 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) &&
3036 (safi
== SAFI_ENCAP
|| safi
== SAFI_MPLS_VPN
))
3038 /* Tunnel Encap attribute */
3039 bgp_packet_mpattr_tea(bgp
, peer
, s
, attr
, BGP_ATTR_ENCAP
);
3042 /* Unknown transit attribute. */
3043 if (attr
->extra
&& attr
->extra
->transit
)
3044 stream_put (s
, attr
->extra
->transit
->val
, attr
->extra
->transit
->length
);
3046 /* Return total size of attribute. */
3047 return stream_get_endp (s
) - cp
;
3051 bgp_packet_mpunreach_start (struct stream
*s
, afi_t afi
, safi_t safi
)
3053 unsigned long attrlen_pnt
;
3055 /* Set extended bit always to encode the attribute length as 2 bytes */
3056 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_EXTLEN
);
3057 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
3059 attrlen_pnt
= stream_get_endp (s
);
3060 stream_putw (s
, 0); /* Length of this attribute. */
3062 stream_putw (s
, afi
);
3063 stream_putc (s
, (safi
== SAFI_MPLS_VPN
) ? SAFI_MPLS_LABELED_VPN
: safi
);
3068 bgp_packet_mpunreach_prefix (struct stream
*s
, struct prefix
*p
,
3069 afi_t afi
, safi_t safi
, struct prefix_rd
*prd
,
3070 u_char
*tag
, int addpath_encode
,
3071 u_int32_t addpath_tx_id
)
3073 if (safi
== SAFI_MPLS_VPN
)
3077 stream_putl(s
, addpath_tx_id
);
3079 stream_putc (s
, p
->prefixlen
+ 88);
3080 stream_put (s
, tag
, 3);
3081 stream_put (s
, prd
->val
, 8);
3082 stream_put (s
, &p
->u
.prefix
, PSIZE (p
->prefixlen
));
3085 stream_put_prefix_addpath (s
, p
, addpath_encode
, addpath_tx_id
);
3089 bgp_packet_mpunreach_end (struct stream
*s
, size_t attrlen_pnt
)
3091 bgp_packet_mpattr_end (s
, attrlen_pnt
);
3094 /* Initialization of attribute. */
3096 bgp_attr_init (void)
3107 bgp_attr_finish (void)
3111 community_finish ();
3112 ecommunity_finish ();
3117 /* Make attribute packet. */
3119 bgp_dump_routes_attr (struct stream
*s
, struct attr
*attr
,
3120 struct prefix
*prefix
)
3125 struct aspath
*aspath
;
3126 int addpath_encode
= 0;
3127 u_int32_t addpath_tx_id
= 0;
3129 /* Remember current pointer. */
3130 cp
= stream_get_endp (s
);
3132 /* Place holder of length. */
3135 /* Origin attribute. */
3136 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3137 stream_putc (s
, BGP_ATTR_ORIGIN
);
3139 stream_putc (s
, attr
->origin
);
3141 aspath
= attr
->aspath
;
3143 stream_putc (s
, BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3144 stream_putc (s
, BGP_ATTR_AS_PATH
);
3145 aspath_lenp
= stream_get_endp (s
);
3148 stream_putw_at (s
, aspath_lenp
, aspath_put (s
, aspath
, 1));
3150 /* Nexthop attribute. */
3151 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3154 && prefix
->family
!= AF_INET6
3155 #endif /* HAVE_IPV6 */
3158 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3159 stream_putc (s
, BGP_ATTR_NEXT_HOP
);
3161 stream_put_ipv4 (s
, attr
->nexthop
.s_addr
);
3164 /* MED attribute. */
3165 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC
))
3167 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
3168 stream_putc (s
, BGP_ATTR_MULTI_EXIT_DISC
);
3170 stream_putl (s
, attr
->med
);
3173 /* Local preference. */
3174 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF
))
3176 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3177 stream_putc (s
, BGP_ATTR_LOCAL_PREF
);
3179 stream_putl (s
, attr
->local_pref
);
3182 /* Atomic aggregate. */
3183 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE
))
3185 stream_putc (s
, BGP_ATTR_FLAG_TRANS
);
3186 stream_putc (s
, BGP_ATTR_ATOMIC_AGGREGATE
);
3191 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR
))
3193 assert (attr
->extra
);
3194 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3195 stream_putc (s
, BGP_ATTR_AGGREGATOR
);
3197 stream_putl (s
, attr
->extra
->aggregator_as
);
3198 stream_put_ipv4 (s
, attr
->extra
->aggregator_addr
.s_addr
);
3201 /* Community attribute. */
3202 if (attr
->flag
& ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
))
3204 if (attr
->community
->size
* 4 > 255)
3206 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
|BGP_ATTR_FLAG_EXTLEN
);
3207 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
3208 stream_putw (s
, attr
->community
->size
* 4);
3212 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
|BGP_ATTR_FLAG_TRANS
);
3213 stream_putc (s
, BGP_ATTR_COMMUNITIES
);
3214 stream_putc (s
, attr
->community
->size
* 4);
3216 stream_put (s
, attr
->community
->val
, attr
->community
->size
* 4);
3220 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3221 if (prefix
!= NULL
&& prefix
->family
== AF_INET6
&& attr
->extra
&&
3222 (attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
||
3223 attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) )
3226 struct attr_extra
*attre
= attr
->extra
;
3228 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
3229 stream_putc(s
, BGP_ATTR_MP_REACH_NLRI
);
3230 sizep
= stream_get_endp (s
);
3233 stream_putc (s
, 0); /* Marker: Attribute length. */
3234 stream_putw(s
, AFI_IP6
); /* AFI */
3235 stream_putc(s
, SAFI_UNICAST
); /* SAFI */
3238 stream_putc(s
, attre
->mp_nexthop_len
);
3239 stream_put(s
, &attre
->mp_nexthop_global
, IPV6_MAX_BYTELEN
);
3240 if (attre
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
3241 stream_put(s
, &attre
->mp_nexthop_local
, IPV6_MAX_BYTELEN
);
3247 stream_put_prefix_addpath (s
, prefix
, addpath_encode
, addpath_tx_id
);
3249 /* Set MP attribute length. */
3250 stream_putc_at (s
, sizep
, (stream_get_endp (s
) - sizep
) - 1);
3252 #endif /* HAVE_IPV6 */
3254 /* Return total size of attribute. */
3255 len
= stream_get_endp (s
) - cp
- 2;
3256 stream_putw_at (s
, cp
, len
);