ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / bgpd / bgp_attr.c
blob5e7536aed4dd18226e3dbc9e335d3526d9a9d337
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "linklist.h"
24 #include "prefix.h"
25 #include "memory.h"
26 #include "vector.h"
27 #include "vty.h"
28 #include "stream.h"
29 #include "log.h"
30 #include "hash.h"
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_attr.h"
34 #include "bgpd/bgp_route.h"
35 #include "bgpd/bgp_aspath.h"
36 #include "bgpd/bgp_community.h"
37 #include "bgpd/bgp_debug.h"
38 #include "bgpd/bgp_packet.h"
39 #include "bgpd/bgp_ecommunity.h"
41 /* Attribute strings for logging. */
42 static const struct message attr_str [] =
44 { BGP_ATTR_ORIGIN, "ORIGIN" },
45 { BGP_ATTR_AS_PATH, "AS_PATH" },
46 { BGP_ATTR_NEXT_HOP, "NEXT_HOP" },
47 { BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" },
48 { BGP_ATTR_LOCAL_PREF, "LOCAL_PREF" },
49 { BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" },
50 { BGP_ATTR_AGGREGATOR, "AGGREGATOR" },
51 { BGP_ATTR_COMMUNITIES, "COMMUNITY" },
52 { BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID" },
53 { BGP_ATTR_CLUSTER_LIST, "CLUSTERLIST" },
54 { BGP_ATTR_DPA, "DPA" },
55 { BGP_ATTR_ADVERTISER, "ADVERTISER"} ,
56 { BGP_ATTR_RCID_PATH, "RCID_PATH" },
57 { BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI" },
58 { BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" },
59 { BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES" },
60 { BGP_ATTR_AS4_PATH, "AS4_PATH" },
61 { BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR" },
62 { BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT" },
64 static const int attr_str_max = sizeof(attr_str)/sizeof(attr_str[0]);
66 static struct hash *cluster_hash;
68 static void *
69 cluster_hash_alloc (void *p)
71 struct cluster_list * val = (struct cluster_list *) p;
72 struct cluster_list *cluster;
74 cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
75 cluster->length = val->length;
77 if (cluster->length)
79 cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length);
80 memcpy (cluster->list, val->list, val->length);
82 else
83 cluster->list = NULL;
85 cluster->refcnt = 0;
87 return cluster;
90 /* Cluster list related functions. */
91 static struct cluster_list *
92 cluster_parse (struct in_addr * pnt, int length)
94 struct cluster_list tmp;
95 struct cluster_list *cluster;
97 tmp.length = length;
98 tmp.list = pnt;
100 cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc);
101 cluster->refcnt++;
102 return cluster;
106 cluster_loop_check (struct cluster_list *cluster, struct in_addr originator)
108 int i;
110 for (i = 0; i < cluster->length / 4; i++)
111 if (cluster->list[i].s_addr == originator.s_addr)
112 return 1;
113 return 0;
116 static unsigned int
117 cluster_hash_key_make (void *p)
119 struct cluster_list * cluster = (struct cluster_list *) p;
120 unsigned int key = 0;
121 int length;
122 caddr_t pnt;
124 length = cluster->length;
125 pnt = (caddr_t) cluster->list;
127 while (length)
128 key += pnt[--length];
130 return key;
133 static int
134 cluster_hash_cmp (const void *p1, const void *p2)
136 const struct cluster_list * cluster1 = p1;
137 const struct cluster_list * cluster2 = p2;
139 return (cluster1->length == cluster2->length &&
140 memcmp (cluster1->list, cluster2->list, cluster1->length) == 0);
143 static void
144 cluster_free (struct cluster_list *cluster)
146 if (cluster->list)
147 XFREE (MTYPE_CLUSTER_VAL, cluster->list);
148 XFREE (MTYPE_CLUSTER, cluster);
151 #if 0
152 static struct cluster_list *
153 cluster_dup (struct cluster_list *cluster)
155 struct cluster_list *new;
157 new = XCALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
158 new->length = cluster->length;
160 if (cluster->length)
162 new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length);
163 memcpy (new->list, cluster->list, cluster->length);
165 else
166 new->list = NULL;
168 return new;
170 #endif
172 static struct cluster_list *
173 cluster_intern (struct cluster_list *cluster)
175 struct cluster_list *find;
177 find = hash_get (cluster_hash, cluster, cluster_hash_alloc);
178 find->refcnt++;
180 return find;
183 void
184 cluster_unintern (struct cluster_list *cluster)
186 struct cluster_list *ret;
188 if (cluster->refcnt)
189 cluster->refcnt--;
191 if (cluster->refcnt == 0)
193 ret = hash_release (cluster_hash, cluster);
194 cluster_free (cluster);
198 static void
199 cluster_init (void)
201 cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp);
204 static void
205 cluster_finish (void)
207 hash_free (cluster_hash);
208 cluster_hash = NULL;
211 /* Unknown transit attribute. */
212 static struct hash *transit_hash;
214 static void
215 transit_free (struct transit *transit)
217 if (transit->val)
218 XFREE (MTYPE_TRANSIT_VAL, transit->val);
219 XFREE (MTYPE_TRANSIT, transit);
223 static void *
224 transit_hash_alloc (void *p)
226 /* Transit structure is already allocated. */
227 return p;
230 static struct transit *
231 transit_intern (struct transit *transit)
233 struct transit *find;
235 find = hash_get (transit_hash, transit, transit_hash_alloc);
236 if (find != transit)
237 transit_free (transit);
238 find->refcnt++;
240 return find;
243 void
244 transit_unintern (struct transit *transit)
246 struct transit *ret;
248 if (transit->refcnt)
249 transit->refcnt--;
251 if (transit->refcnt == 0)
253 ret = hash_release (transit_hash, transit);
254 transit_free (transit);
258 static unsigned int
259 transit_hash_key_make (void *p)
261 struct transit * transit = (struct transit *) p;
262 unsigned int key = 0;
263 int length;
264 caddr_t pnt;
266 length = transit->length;
267 pnt = (caddr_t) transit->val;
269 while (length)
270 key += pnt[--length];
272 return key;
275 static int
276 transit_hash_cmp (const void *p1, const void *p2)
278 const struct transit * transit1 = p1;
279 const struct transit * transit2 = p2;
281 return (transit1->length == transit2->length &&
282 memcmp (transit1->val, transit2->val, transit1->length) == 0);
285 static void
286 transit_init (void)
288 transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp);
291 static void
292 transit_finish (void)
294 hash_free (transit_hash);
295 transit_hash = NULL;
298 /* Attribute hash routines. */
299 static struct hash *attrhash;
301 static struct attr_extra *
302 bgp_attr_extra_new (void)
304 return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra));
307 void
308 bgp_attr_extra_free (struct attr *attr)
310 if (attr->extra)
312 XFREE (MTYPE_ATTR_EXTRA, attr->extra);
313 attr->extra = NULL;
317 struct attr_extra *
318 bgp_attr_extra_get (struct attr *attr)
320 if (!attr->extra)
321 attr->extra = bgp_attr_extra_new();
322 return attr->extra;
325 /* Shallow copy of an attribute
326 * Though, not so shallow that it doesn't copy the contents
327 * of the attr_extra pointed to by 'extra'
329 void
330 bgp_attr_dup (struct attr *new, struct attr *orig)
332 *new = *orig;
333 if (orig->extra)
335 new->extra = bgp_attr_extra_new();
336 *new->extra = *orig->extra;
340 unsigned long int
341 attr_count (void)
343 return attrhash->count;
346 unsigned long int
347 attr_unknown_count (void)
349 return transit_hash->count;
352 unsigned int
353 attrhash_key_make (void *p)
355 struct attr * attr = (struct attr *) p;
356 unsigned int key = 0;
358 key += attr->origin;
359 key += attr->nexthop.s_addr;
360 key += attr->med;
361 key += attr->local_pref;
362 if (attr->pathlimit.as)
364 key += attr->pathlimit.ttl;
365 key += attr->pathlimit.as;
368 if (attr->extra)
370 key += attr->extra->aggregator_as;
371 key += attr->extra->aggregator_addr.s_addr;
372 key += attr->extra->weight;
373 key += attr->extra->mp_nexthop_global_in.s_addr;
376 if (attr->aspath)
377 key += aspath_key_make (attr->aspath);
378 if (attr->community)
379 key += community_hash_make (attr->community);
381 if (attr->extra)
383 if (attr->extra->ecommunity)
384 key += ecommunity_hash_make (attr->extra->ecommunity);
385 if (attr->extra->cluster)
386 key += cluster_hash_key_make (attr->extra->cluster);
387 if (attr->extra->transit)
388 key += transit_hash_key_make (attr->extra->transit);
390 #ifdef HAVE_IPV6
392 int i;
394 key += attr->extra->mp_nexthop_len;
395 for (i = 0; i < 16; i++)
396 key += attr->extra->mp_nexthop_global.s6_addr[i];
397 for (i = 0; i < 16; i++)
398 key += attr->extra->mp_nexthop_local.s6_addr[i];
400 #endif /* HAVE_IPV6 */
403 return key;
407 attrhash_cmp (const void *p1, const void *p2)
409 const struct attr * attr1 = p1;
410 const struct attr * attr2 = p2;
412 if (attr1->flag == attr2->flag
413 && attr1->origin == attr2->origin
414 && attr1->nexthop.s_addr == attr2->nexthop.s_addr
415 && attr1->aspath == attr2->aspath
416 && attr1->community == attr2->community
417 && attr1->med == attr2->med
418 && attr1->local_pref == attr2->local_pref
419 && attr1->pathlimit.ttl == attr2->pathlimit.ttl
420 && attr1->pathlimit.as == attr2->pathlimit.as)
422 const struct attr_extra *ae1 = attr1->extra;
423 const struct attr_extra *ae2 = attr2->extra;
425 if (ae1 && ae2
426 && ae1->aggregator_as == ae2->aggregator_as
427 && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr
428 && ae1->weight == ae2->weight
429 #ifdef HAVE_IPV6
430 && ae1->mp_nexthop_len == ae2->mp_nexthop_len
431 && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global)
432 && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local)
433 #endif /* HAVE_IPV6 */
434 && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in)
435 && ae1->ecommunity == ae2->ecommunity
436 && ae1->cluster == ae2->cluster
437 && ae1->transit == ae2->transit)
438 return 1;
439 else if (ae1 || ae2)
440 return 0;
441 /* neither attribute has extra attributes, so they're same */
442 return 1;
444 else
445 return 0;
448 static void
449 attrhash_init (void)
451 attrhash = hash_create (attrhash_key_make, attrhash_cmp);
454 static void
455 attrhash_finish (void)
457 hash_free (attrhash);
458 attrhash = NULL;
461 static void
462 attr_show_all_iterator (struct hash_backet *backet, struct vty *vty)
464 struct attr *attr = backet->data;
466 vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt,
467 inet_ntoa (attr->nexthop), VTY_NEWLINE);
470 void
471 attr_show_all (struct vty *vty)
473 hash_iterate (attrhash,
474 (void (*)(struct hash_backet *, void *))
475 attr_show_all_iterator,
476 vty);
479 static void *
480 bgp_attr_hash_alloc (void *p)
482 struct attr * val = (struct attr *) p;
483 struct attr *attr;
485 attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr));
486 *attr = *val;
487 if (val->extra)
489 attr->extra = bgp_attr_extra_new ();
490 *attr->extra = *val->extra;
492 attr->refcnt = 0;
493 return attr;
496 /* Internet argument attribute. */
497 struct attr *
498 bgp_attr_intern (struct attr *attr)
500 struct attr *find;
502 /* Intern referenced strucutre. */
503 if (attr->aspath)
505 if (! attr->aspath->refcnt)
506 attr->aspath = aspath_intern (attr->aspath);
507 else
508 attr->aspath->refcnt++;
510 if (attr->community)
512 if (! attr->community->refcnt)
513 attr->community = community_intern (attr->community);
514 else
515 attr->community->refcnt++;
517 if (attr->extra)
519 struct attr_extra *attre = attr->extra;
521 if (attre->ecommunity)
523 if (! attre->ecommunity->refcnt)
524 attre->ecommunity = ecommunity_intern (attre->ecommunity);
525 else
526 attre->ecommunity->refcnt++;
528 if (attre->cluster)
530 if (! attre->cluster->refcnt)
531 attre->cluster = cluster_intern (attre->cluster);
532 else
533 attre->cluster->refcnt++;
535 if (attre->transit)
537 if (! attre->transit->refcnt)
538 attre->transit = transit_intern (attre->transit);
539 else
540 attre->transit->refcnt++;
544 find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
545 find->refcnt++;
547 return find;
551 /* Make network statement's attribute. */
552 struct attr *
553 bgp_attr_default_set (struct attr *attr, u_char origin)
555 memset (attr, 0, sizeof (struct attr));
556 bgp_attr_extra_get (attr);
558 attr->origin = origin;
559 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
560 attr->aspath = aspath_empty ();
561 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
562 attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT;
563 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
564 #ifdef HAVE_IPV6
565 attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN;
566 #endif
568 return attr;
572 /* Make network statement's attribute. */
573 struct attr *
574 bgp_attr_default_intern (u_char origin)
576 struct attr attr;
577 struct attr *new;
578 struct attr_extra *attre;
580 memset (&attr, 0, sizeof (struct attr));
581 attre = bgp_attr_extra_get (&attr);
583 bgp_attr_default_set(&attr, origin);
585 new = bgp_attr_intern (&attr);
586 bgp_attr_extra_free (&attr);
588 aspath_unintern (new->aspath);
589 return new;
592 struct attr *
593 bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
594 struct aspath *aspath,
595 struct community *community, int as_set)
597 struct attr attr;
598 struct attr *new;
599 struct attr_extra *attre;
601 memset (&attr, 0, sizeof (struct attr));
602 attre = bgp_attr_extra_get (&attr);
604 /* Origin attribute. */
605 attr.origin = origin;
606 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
608 /* AS path attribute. */
609 if (aspath)
610 attr.aspath = aspath_intern (aspath);
611 else
612 attr.aspath = aspath_empty ();
613 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
615 /* Next hop attribute. */
616 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
618 if (community)
620 attr.community = community;
621 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
624 attre->weight = BGP_ATTR_DEFAULT_WEIGHT;
625 #ifdef HAVE_IPV6
626 attre->mp_nexthop_len = IPV6_MAX_BYTELEN;
627 #endif
628 if (! as_set)
629 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
630 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
631 if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
632 attre->aggregator_as = bgp->confed_id;
633 else
634 attre->aggregator_as = bgp->as;
635 attre->aggregator_addr = bgp->router_id;
637 new = bgp_attr_intern (&attr);
638 bgp_attr_extra_free (&attr);
640 aspath_unintern (new->aspath);
641 return new;
644 /* Free bgp attribute and aspath. */
645 void
646 bgp_attr_unintern (struct attr *attr)
648 struct attr *ret;
649 struct aspath *aspath;
650 struct community *community;
651 struct ecommunity *ecommunity = NULL;
652 struct cluster_list *cluster = NULL;
653 struct transit *transit = NULL;
655 /* Decrement attribute reference. */
656 attr->refcnt--;
657 aspath = attr->aspath;
658 community = attr->community;
659 if (attr->extra)
661 ecommunity = attr->extra->ecommunity;
662 cluster = attr->extra->cluster;
663 transit = attr->extra->transit;
666 /* If reference becomes zero then free attribute object. */
667 if (attr->refcnt == 0)
669 ret = hash_release (attrhash, attr);
670 assert (ret != NULL);
671 bgp_attr_extra_free (attr);
672 XFREE (MTYPE_ATTR, attr);
675 /* aspath refcount shoud be decrement. */
676 if (aspath)
677 aspath_unintern (aspath);
678 if (community)
679 community_unintern (community);
680 if (ecommunity)
681 ecommunity_unintern (ecommunity);
682 if (cluster)
683 cluster_unintern (cluster);
684 if (transit)
685 transit_unintern (transit);
688 void
689 bgp_attr_flush (struct attr *attr)
691 if (attr->aspath && ! attr->aspath->refcnt)
692 aspath_free (attr->aspath);
693 if (attr->community && ! attr->community->refcnt)
694 community_free (attr->community);
695 if (attr->extra)
697 struct attr_extra *attre = attr->extra;
698 if (attre->ecommunity && ! attre->ecommunity->refcnt)
699 ecommunity_free (attre->ecommunity);
700 if (attre->cluster && ! attre->cluster->refcnt)
701 cluster_free (attre->cluster);
702 if (attre->transit && ! attre->transit->refcnt)
703 transit_free (attre->transit);
707 /* Parse AS_PATHLIMIT attribute in an UPDATE */
708 static int
709 bgp_attr_aspathlimit (struct peer *peer, bgp_size_t length,
710 struct attr *attr, u_char flag, u_char *startp)
712 bgp_size_t total;
714 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
716 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS)
717 || !CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL))
719 zlog (peer->log, LOG_ERR,
720 "AS-Pathlimit attribute flag isn't transitive %d", flag);
721 bgp_notify_send_with_data (peer,
722 BGP_NOTIFY_UPDATE_ERR,
723 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
724 startp, total);
725 return -1;
728 if (length != 5)
730 zlog (peer->log, LOG_ERR,
731 "AS-Pathlimit length, %u, is not 5", length);
732 bgp_notify_send_with_data (peer,
733 BGP_NOTIFY_UPDATE_ERR,
734 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
735 startp, total);
736 return -1;
739 attr->pathlimit.ttl = stream_getc (BGP_INPUT(peer));
740 attr->pathlimit.as = stream_getl (BGP_INPUT(peer));
741 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATHLIMIT);
742 return 0;
744 /* Get origin attribute of the update message. */
745 static int
746 bgp_attr_origin (struct peer *peer, bgp_size_t length,
747 struct attr *attr, u_char flag, u_char *startp)
749 bgp_size_t total;
751 /* total is entire attribute length include Attribute Flags (1),
752 Attribute Type code (1) and Attribute length (1 or 2). */
753 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
755 /* If any recognized attribute has Attribute Flags that conflict
756 with the Attribute Type Code, then the Error Subcode is set to
757 Attribute Flags Error. The Data field contains the erroneous
758 attribute (type, length and value). */
759 if (flag != BGP_ATTR_FLAG_TRANS)
761 zlog (peer->log, LOG_ERR,
762 "Origin attribute flag isn't transitive %d", flag);
763 bgp_notify_send_with_data (peer,
764 BGP_NOTIFY_UPDATE_ERR,
765 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
766 startp, total);
767 return -1;
770 /* If any recognized attribute has Attribute Length that conflicts
771 with the expected length (based on the attribute type code), then
772 the Error Subcode is set to Attribute Length Error. The Data
773 field contains the erroneous attribute (type, length and
774 value). */
775 if (length != 1)
777 zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d",
778 length);
779 bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR,
780 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
781 startp, total);
782 return -1;
785 /* Fetch origin attribute. */
786 attr->origin = stream_getc (BGP_INPUT (peer));
788 /* If the ORIGIN attribute has an undefined value, then the Error
789 Subcode is set to Invalid Origin Attribute. The Data field
790 contains the unrecognized attribute (type, length and value). */
791 if ((attr->origin != BGP_ORIGIN_IGP)
792 && (attr->origin != BGP_ORIGIN_EGP)
793 && (attr->origin != BGP_ORIGIN_INCOMPLETE))
795 zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d",
796 attr->origin);
798 bgp_notify_send_with_data (peer,
799 BGP_NOTIFY_UPDATE_ERR,
800 BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
801 startp, total);
802 return -1;
805 /* Set oring attribute flag. */
806 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
808 return 0;
811 /* Parse AS path information. This function is wrapper of
812 aspath_parse. */
813 static int
814 bgp_attr_aspath (struct peer *peer, bgp_size_t length,
815 struct attr *attr, u_char flag, u_char *startp)
817 bgp_size_t total;
819 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
821 /* Flag check. */
822 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)
823 || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
825 zlog (peer->log, LOG_ERR,
826 "As-Path attribute flag isn't transitive %d", flag);
827 bgp_notify_send_with_data (peer,
828 BGP_NOTIFY_UPDATE_ERR,
829 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
830 startp, total);
831 return -1;
835 * peer with AS4 => will get 4Byte ASnums
836 * otherwise, will get 16 Bit
838 attr->aspath = aspath_parse (peer->ibuf, length,
839 CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV));
841 /* In case of IBGP, length will be zero. */
842 if (! attr->aspath)
844 zlog (peer->log, LOG_ERR, "Malformed AS path length is %d", length);
845 bgp_notify_send (peer,
846 BGP_NOTIFY_UPDATE_ERR,
847 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
848 return -1;
851 /* Forward pointer. */
852 /* stream_forward_getp (peer->ibuf, length);*/
854 /* Set aspath attribute flag. */
855 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
857 return 0;
860 static int bgp_attr_aspath_check( struct peer *peer,
861 struct attr *attr)
863 /* These checks were part of bgp_attr_aspath, but with
864 * as4 we should to check aspath things when
865 * aspath synthesizing with as4_path has already taken place.
866 * Otherwise we check ASPATH and use the synthesized thing, and that is
867 * not right.
868 * So do the checks later, i.e. here
870 struct bgp *bgp = peer->bgp;
871 struct aspath *aspath;
873 bgp = peer->bgp;
875 /* Confederation sanity check. */
876 if ((peer_sort (peer) == BGP_PEER_CONFED && ! aspath_left_confed_check (attr->aspath)) ||
877 (peer_sort (peer) == BGP_PEER_EBGP && aspath_confed_check (attr->aspath)))
879 zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host);
880 bgp_notify_send (peer,
881 BGP_NOTIFY_UPDATE_ERR,
882 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
883 return -1;
886 /* First AS check for EBGP. */
887 if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS))
889 if (peer_sort (peer) == BGP_PEER_EBGP
890 && ! aspath_firstas_check (attr->aspath, peer->as))
892 zlog (peer->log, LOG_ERR,
893 "%s incorrect first AS (must be %u)", peer->host, peer->as);
894 bgp_notify_send (peer,
895 BGP_NOTIFY_UPDATE_ERR,
896 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
897 return -1;
901 /* local-as prepend */
902 if (peer->change_local_as &&
903 ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
905 aspath = aspath_dup (attr->aspath);
906 aspath = aspath_add_seq (aspath, peer->change_local_as);
907 aspath_unintern (attr->aspath);
908 attr->aspath = aspath_intern (aspath);
911 return 0;
915 /* Parse AS4 path information. This function is another wrapper of
916 aspath_parse. */
917 static int
918 bgp_attr_as4_path (struct peer *peer, bgp_size_t length,
919 struct attr *attr, struct aspath **as4_path)
921 *as4_path = aspath_parse (peer->ibuf, length, 1);
923 /* Set aspath attribute flag. */
924 if (as4_path)
925 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH);
927 return 0;
930 /* Nexthop attribute. */
931 static int
932 bgp_attr_nexthop (struct peer *peer, bgp_size_t length,
933 struct attr *attr, u_char flag, u_char *startp)
935 bgp_size_t total;
937 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
939 /* Flag check. */
940 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)
941 || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
943 zlog (peer->log, LOG_ERR,
944 "Origin attribute flag isn't transitive %d", flag);
945 bgp_notify_send_with_data (peer,
946 BGP_NOTIFY_UPDATE_ERR,
947 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
948 startp, total);
949 return -1;
952 /* Check nexthop attribute length. */
953 if (length != 4)
955 zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]",
956 length);
958 bgp_notify_send_with_data (peer,
959 BGP_NOTIFY_UPDATE_ERR,
960 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
961 startp, total);
962 return -1;
965 attr->nexthop.s_addr = stream_get_ipv4 (peer->ibuf);
966 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
968 return 0;
971 /* MED atrribute. */
972 static int
973 bgp_attr_med (struct peer *peer, bgp_size_t length,
974 struct attr *attr, u_char flag, u_char *startp)
976 bgp_size_t total;
978 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
980 /* Length check. */
981 if (length != 4)
983 zlog (peer->log, LOG_ERR,
984 "MED attribute length isn't four [%d]", length);
986 bgp_notify_send_with_data (peer,
987 BGP_NOTIFY_UPDATE_ERR,
988 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
989 startp, total);
990 return -1;
993 attr->med = stream_getl (peer->ibuf);
995 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
997 return 0;
1000 /* Local preference attribute. */
1001 static int
1002 bgp_attr_local_pref (struct peer *peer, bgp_size_t length,
1003 struct attr *attr, u_char flag)
1005 /* If it is contained in an UPDATE message that is received from an
1006 external peer, then this attribute MUST be ignored by the
1007 receiving speaker. */
1008 if (peer_sort (peer) == BGP_PEER_EBGP)
1010 stream_forward_getp (peer->ibuf, length);
1011 return 0;
1014 if (length == 4)
1015 attr->local_pref = stream_getl (peer->ibuf);
1016 else
1017 attr->local_pref = 0;
1019 /* Set atomic aggregate flag. */
1020 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
1022 return 0;
1025 /* Atomic aggregate. */
1026 static int
1027 bgp_attr_atomic (struct peer *peer, bgp_size_t length,
1028 struct attr *attr, u_char flag)
1030 if (length != 0)
1032 zlog (peer->log, LOG_ERR, "Bad atomic aggregate length %d", length);
1034 bgp_notify_send (peer,
1035 BGP_NOTIFY_UPDATE_ERR,
1036 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1037 return -1;
1040 /* Set atomic aggregate flag. */
1041 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
1043 return 0;
1046 /* Aggregator attribute */
1047 static int
1048 bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
1049 struct attr *attr, u_char flag)
1051 int wantedlen = 6;
1052 struct attr_extra *attre = bgp_attr_extra_get (attr);
1054 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1055 if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
1056 wantedlen = 8;
1058 if (length != wantedlen)
1060 zlog (peer->log, LOG_ERR, "Aggregator length is not %d [%d]", wantedlen, length);
1062 bgp_notify_send (peer,
1063 BGP_NOTIFY_UPDATE_ERR,
1064 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1065 return -1;
1068 if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
1069 attre->aggregator_as = stream_getl (peer->ibuf);
1070 else
1071 attre->aggregator_as = stream_getw (peer->ibuf);
1072 attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
1074 /* Set atomic aggregate flag. */
1075 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
1077 return 0;
1080 /* New Aggregator attribute */
1081 static int
1082 bgp_attr_as4_aggregator (struct peer *peer, bgp_size_t length,
1083 struct attr *attr, as_t *as4_aggregator_as,
1084 struct in_addr *as4_aggregator_addr)
1086 if (length != 8)
1088 zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]", length);
1090 bgp_notify_send (peer,
1091 BGP_NOTIFY_UPDATE_ERR,
1092 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1093 return -1;
1095 *as4_aggregator_as = stream_getl (peer->ibuf);
1096 as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf);
1098 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR);
1100 return 0;
1103 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1105 static int
1106 bgp_attr_munge_as4_attrs (struct peer *peer, struct attr *attr,
1107 struct aspath *as4_path, as_t as4_aggregator,
1108 struct in_addr *as4_aggregator_addr)
1110 int ignore_as4_path = 0;
1111 struct aspath *newpath;
1112 struct attr_extra *attre = attr->extra;
1114 if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
1116 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1117 * if given.
1118 * It is worth a warning though, because the peer really
1119 * should not send them
1121 if (BGP_DEBUG(as4, AS4))
1123 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))
1124 zlog_debug ("[AS4] %s %s AS4_PATH",
1125 peer->host, "AS4 capable peer, yet it sent");
1127 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR)))
1128 zlog_debug ("[AS4] %s %s AS4_AGGREGATOR",
1129 peer->host, "AS4 capable peer, yet it sent");
1132 return 0;
1135 if (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))
1136 && !(attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH))))
1138 /* Hu? This is not supposed to happen at all!
1139 * got as4_path and no aspath,
1140 * This should already
1141 * have been handled by 'well known attributes missing'
1142 * But... yeah, paranoia
1143 * Take this as a "malformed attribute"
1145 zlog (peer->log, LOG_ERR,
1146 "%s BGP not AS4 capable peer sent AS4_PATH but"
1147 " no AS_PATH, cant do anything here", peer->host);
1148 bgp_notify_send (peer,
1149 BGP_NOTIFY_UPDATE_ERR,
1150 BGP_NOTIFY_UPDATE_MAL_ATTR);
1151 return -1;
1154 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1155 * because that may override AS4_PATH
1157 if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) )
1159 if ( attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
1161 assert (attre);
1163 /* received both.
1164 * if the as_number in aggregator is not AS_TRANS,
1165 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1166 * and the Aggregator shall be taken as
1167 * info on the aggregating node, and the AS_PATH
1168 * shall be taken as the AS_PATH
1169 * otherwise
1170 * the Aggregator shall be ignored and the
1171 * AS4_AGGREGATOR shall be taken as the
1172 * Aggregating node and the AS_PATH is to be
1173 * constructed "as in all other cases"
1175 if ( attre->aggregator_as != BGP_AS_TRANS )
1177 /* ignore */
1178 if ( BGP_DEBUG(as4, AS4))
1179 zlog_debug ("[AS4] %s BGP not AS4 capable peer"
1180 " send AGGREGATOR != AS_TRANS and"
1181 " AS4_AGGREGATOR, so ignore"
1182 " AS4_AGGREGATOR and AS4_PATH", peer->host);
1183 ignore_as4_path = 1;
1185 else
1187 /* "New_aggregator shall be taken as aggregator" */
1188 attre->aggregator_as = as4_aggregator;
1189 attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr;
1192 else
1194 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1195 * That is bogus - but reading the conditions
1196 * we have to handle AS4_AGGREGATOR as if it were
1197 * AGGREGATOR in that case
1199 if ( BGP_DEBUG(as4, AS4))
1200 zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
1201 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1202 " it as if AGGREGATOR with AS_TRANS had been there", peer->host);
1203 (attre = bgp_attr_extra_get (attr))->aggregator_as = as4_aggregator;
1204 /* sweep it under the carpet and simulate a "good" AGGREGATOR */
1205 attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR));
1209 /* need to reconcile NEW_AS_PATH and AS_PATH */
1210 if ( !ignore_as4_path && (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))) )
1212 newpath = aspath_reconcile_as4 (attr->aspath, as4_path);
1213 aspath_unintern (attr->aspath);
1214 attr->aspath = aspath_intern (newpath);
1216 return 0;
1219 /* Community attribute. */
1220 static int
1221 bgp_attr_community (struct peer *peer, bgp_size_t length,
1222 struct attr *attr, u_char flag)
1224 if (length == 0)
1226 attr->community = NULL;
1227 return 0;
1229 else
1231 attr->community =
1232 community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length);
1233 stream_forward_getp (peer->ibuf, length);
1236 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
1238 return 0;
1241 /* Originator ID attribute. */
1242 static int
1243 bgp_attr_originator_id (struct peer *peer, bgp_size_t length,
1244 struct attr *attr, u_char flag)
1246 if (length != 4)
1248 zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length);
1250 bgp_notify_send (peer,
1251 BGP_NOTIFY_UPDATE_ERR,
1252 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1253 return -1;
1256 (bgp_attr_extra_get (attr))->originator_id.s_addr
1257 = stream_get_ipv4 (peer->ibuf);
1259 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
1261 return 0;
1264 /* Cluster list attribute. */
1265 static int
1266 bgp_attr_cluster_list (struct peer *peer, bgp_size_t length,
1267 struct attr *attr, u_char flag)
1269 /* Check length. */
1270 if (length % 4)
1272 zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length);
1274 bgp_notify_send (peer,
1275 BGP_NOTIFY_UPDATE_ERR,
1276 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1277 return -1;
1280 (bgp_attr_extra_get (attr))->cluster
1281 = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length);
1283 stream_forward_getp (peer->ibuf, length);;
1285 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST);
1287 return 0;
1290 /* Multiprotocol reachability information parse. */
1292 bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
1293 struct bgp_nlri *mp_update)
1295 u_int16_t afi;
1296 u_char safi;
1297 bgp_size_t nlri_len;
1298 size_t start;
1299 int ret;
1300 struct stream *s;
1301 struct attr_extra *attre = bgp_attr_extra_get(attr);
1303 /* Set end of packet. */
1304 s = BGP_INPUT(peer);
1305 start = stream_get_getp(s);
1307 /* safe to read statically sized header? */
1308 #define BGP_MP_REACH_MIN_SIZE 5
1309 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1310 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE))
1312 zlog_info ("%s: %s sent invalid length, %lu",
1313 __func__, peer->host, (unsigned long)length);
1314 return -1;
1317 /* Load AFI, SAFI. */
1318 afi = stream_getw (s);
1319 safi = stream_getc (s);
1321 /* Get nexthop length. */
1322 attre->mp_nexthop_len = stream_getc (s);
1324 if (LEN_LEFT < attre->mp_nexthop_len)
1326 zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute",
1327 __func__, peer->host, attre->mp_nexthop_len);
1328 return -1;
1331 /* Nexthop length check. */
1332 switch (attre->mp_nexthop_len)
1334 case 4:
1335 stream_get (&attre->mp_nexthop_global_in, s, 4);
1336 /* Probably needed for RFC 2283 */
1337 if (attr->nexthop.s_addr == 0)
1338 memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4);
1339 break;
1340 case 12:
1342 u_int32_t rd_high;
1343 u_int32_t rd_low;
1345 rd_high = stream_getl (s);
1346 rd_low = stream_getl (s);
1347 stream_get (&attre->mp_nexthop_global_in, s, 4);
1349 break;
1350 #ifdef HAVE_IPV6
1351 case 16:
1352 stream_get (&attre->mp_nexthop_global, s, 16);
1353 break;
1354 case 32:
1355 stream_get (&attre->mp_nexthop_global, s, 16);
1356 stream_get (&attre->mp_nexthop_local, s, 16);
1357 if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local))
1359 char buf1[INET6_ADDRSTRLEN];
1360 char buf2[INET6_ADDRSTRLEN];
1362 if (BGP_DEBUG (update, UPDATE_IN))
1363 zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host,
1364 inet_ntop (AF_INET6, &attre->mp_nexthop_global,
1365 buf1, INET6_ADDRSTRLEN),
1366 inet_ntop (AF_INET6, &attre->mp_nexthop_local,
1367 buf2, INET6_ADDRSTRLEN));
1369 attre->mp_nexthop_len = 16;
1371 break;
1372 #endif /* HAVE_IPV6 */
1373 default:
1374 zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d",
1375 __func__, peer->host, attre->mp_nexthop_len);
1376 return -1;
1379 if (!LEN_LEFT)
1381 zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
1382 __func__, peer->host);
1383 return -1;
1387 u_char val;
1388 if ((val = stream_getc (s)))
1389 zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field",
1390 peer->host, val);
1393 /* must have nrli_len, what is left of the attribute */
1394 nlri_len = LEN_LEFT;
1395 if ((!nlri_len) || (nlri_len > STREAM_READABLE(s)))
1397 zlog_info ("%s: (%s) Failed to read NLRI",
1398 __func__, peer->host);
1399 return -1;
1402 if (safi != BGP_SAFI_VPNV4)
1404 ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), nlri_len);
1405 if (ret < 0)
1407 zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
1408 __func__, peer->host);
1409 return -1;
1413 mp_update->afi = afi;
1414 mp_update->safi = safi;
1415 mp_update->nlri = stream_pnt (s);
1416 mp_update->length = nlri_len;
1418 stream_forward_getp (s, nlri_len);
1420 return 0;
1421 #undef LEN_LEFT
1424 /* Multiprotocol unreachable parse */
1426 bgp_mp_unreach_parse (struct peer *peer, bgp_size_t length,
1427 struct bgp_nlri *mp_withdraw)
1429 struct stream *s;
1430 u_int16_t afi;
1431 u_char safi;
1432 u_int16_t withdraw_len;
1433 int ret;
1435 s = peer->ibuf;
1437 #define BGP_MP_UNREACH_MIN_SIZE 3
1438 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE))
1439 return -1;
1441 afi = stream_getw (s);
1442 safi = stream_getc (s);
1444 withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE;
1446 if (safi != BGP_SAFI_VPNV4)
1448 ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), withdraw_len);
1449 if (ret < 0)
1450 return -1;
1453 mp_withdraw->afi = afi;
1454 mp_withdraw->safi = safi;
1455 mp_withdraw->nlri = stream_pnt (s);
1456 mp_withdraw->length = withdraw_len;
1458 stream_forward_getp (s, withdraw_len);
1460 return 0;
1463 /* Extended Community attribute. */
1464 static int
1465 bgp_attr_ext_communities (struct peer *peer, bgp_size_t length,
1466 struct attr *attr, u_char flag)
1468 if (length == 0)
1470 if (attr->extra)
1471 attr->extra->ecommunity = NULL;
1473 else
1475 (bgp_attr_extra_get (attr))->ecommunity =
1476 ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length);
1477 stream_forward_getp (peer->ibuf, length);
1479 attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
1481 return 0;
1484 /* BGP unknown attribute treatment. */
1485 static int
1486 bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag,
1487 u_char type, bgp_size_t length, u_char *startp)
1489 bgp_size_t total;
1490 struct transit *transit;
1491 struct attr_extra *attre;
1493 if (BGP_DEBUG (normal, NORMAL))
1494 zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
1495 peer->host, type, length);
1497 if (BGP_DEBUG (events, EVENTS))
1498 zlog (peer->log, LOG_DEBUG,
1499 "Unknown attribute type %d length %d is received", type, length);
1501 /* Forward read pointer of input stream. */
1502 stream_forward_getp (peer->ibuf, length);
1504 /* Adjest total length to include type and length. */
1505 total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
1507 /* If any of the mandatory well-known attributes are not recognized,
1508 then the Error Subcode is set to Unrecognized Well-known
1509 Attribute. The Data field contains the unrecognized attribute
1510 (type, length and value). */
1511 if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
1513 /* Adjust startp to do not include flag value. */
1514 bgp_notify_send_with_data (peer,
1515 BGP_NOTIFY_UPDATE_ERR,
1516 BGP_NOTIFY_UPDATE_UNREC_ATTR,
1517 startp, total);
1518 return -1;
1521 /* Unrecognized non-transitive optional attributes must be quietly
1522 ignored and not passed along to other BGP peers. */
1523 if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
1524 return 0;
1526 /* If a path with recognized transitive optional attribute is
1527 accepted and passed along to other BGP peers and the Partial bit
1528 in the Attribute Flags octet is set to 1 by some previous AS, it
1529 is not set back to 0 by the current AS. */
1530 SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL);
1532 /* Store transitive attribute to the end of attr->transit. */
1533 if (! ((attre = bgp_attr_extra_get(attr))->transit) )
1534 attre->transit = XCALLOC (MTYPE_TRANSIT, sizeof (struct transit));
1536 transit = attre->transit;
1538 if (transit->val)
1539 transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val,
1540 transit->length + total);
1541 else
1542 transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total);
1544 memcpy (transit->val + transit->length, startp, total);
1545 transit->length += total;
1547 return 0;
1550 /* Read attribute of update packet. This function is called from
1551 bgp_update() in bgpd.c. */
1553 bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
1554 struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw)
1556 int ret;
1557 u_char flag;
1558 u_char type = 0;
1559 bgp_size_t length;
1560 u_char *startp, *endp;
1561 u_char *attr_endp;
1562 u_char seen[BGP_ATTR_BITMAP_SIZE];
1563 /* we need the as4_path only until we have synthesized the as_path with it */
1564 /* same goes for as4_aggregator */
1565 struct aspath *as4_path = NULL;
1566 as_t as4_aggregator = 0;
1567 struct in_addr as4_aggregator_addr = { 0 };
1569 /* Initialize bitmap. */
1570 memset (seen, 0, BGP_ATTR_BITMAP_SIZE);
1572 /* End pointer of BGP attribute. */
1573 endp = BGP_INPUT_PNT (peer) + size;
1575 /* Get attributes to the end of attribute length. */
1576 while (BGP_INPUT_PNT (peer) < endp)
1578 /* Check remaining length check.*/
1579 if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN)
1581 /* XXX warning: long int format, int arg (arg 5) */
1582 zlog (peer->log, LOG_WARNING,
1583 "%s error BGP attribute length %lu is smaller than min len",
1584 peer->host,
1585 (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
1587 bgp_notify_send (peer,
1588 BGP_NOTIFY_UPDATE_ERR,
1589 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1590 return -1;
1593 /* Fetch attribute flag and type. */
1594 startp = BGP_INPUT_PNT (peer);
1595 flag = stream_getc (BGP_INPUT (peer));
1596 type = stream_getc (BGP_INPUT (peer));
1598 /* Check whether Extended-Length applies and is in bounds */
1599 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN)
1600 && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1)))
1602 zlog (peer->log, LOG_WARNING,
1603 "%s Extended length set, but just %lu bytes of attr header",
1604 peer->host,
1605 (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
1607 bgp_notify_send (peer,
1608 BGP_NOTIFY_UPDATE_ERR,
1609 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1610 return -1;
1613 /* Check extended attribue length bit. */
1614 if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN))
1615 length = stream_getw (BGP_INPUT (peer));
1616 else
1617 length = stream_getc (BGP_INPUT (peer));
1619 /* If any attribute appears more than once in the UPDATE
1620 message, then the Error Subcode is set to Malformed Attribute
1621 List. */
1623 if (CHECK_BITMAP (seen, type))
1625 zlog (peer->log, LOG_WARNING,
1626 "%s error BGP attribute type %d appears twice in a message",
1627 peer->host, type);
1629 bgp_notify_send (peer,
1630 BGP_NOTIFY_UPDATE_ERR,
1631 BGP_NOTIFY_UPDATE_MAL_ATTR);
1632 return -1;
1635 /* Set type to bitmap to check duplicate attribute. `type' is
1636 unsigned char so it never overflow bitmap range. */
1638 SET_BITMAP (seen, type);
1640 /* Overflow check. */
1641 attr_endp = BGP_INPUT_PNT (peer) + length;
1643 if (attr_endp > endp)
1645 zlog (peer->log, LOG_WARNING,
1646 "%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);
1647 bgp_notify_send (peer,
1648 BGP_NOTIFY_UPDATE_ERR,
1649 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1650 return -1;
1653 /* OK check attribute and store it's value. */
1654 switch (type)
1656 case BGP_ATTR_ORIGIN:
1657 ret = bgp_attr_origin (peer, length, attr, flag, startp);
1658 break;
1659 case BGP_ATTR_AS_PATH:
1660 ret = bgp_attr_aspath (peer, length, attr, flag, startp);
1661 break;
1662 case BGP_ATTR_AS4_PATH:
1663 ret = bgp_attr_as4_path (peer, length, attr, &as4_path );
1664 break;
1665 case BGP_ATTR_NEXT_HOP:
1666 ret = bgp_attr_nexthop (peer, length, attr, flag, startp);
1667 break;
1668 case BGP_ATTR_MULTI_EXIT_DISC:
1669 ret = bgp_attr_med (peer, length, attr, flag, startp);
1670 break;
1671 case BGP_ATTR_LOCAL_PREF:
1672 ret = bgp_attr_local_pref (peer, length, attr, flag);
1673 break;
1674 case BGP_ATTR_ATOMIC_AGGREGATE:
1675 ret = bgp_attr_atomic (peer, length, attr, flag);
1676 break;
1677 case BGP_ATTR_AGGREGATOR:
1678 ret = bgp_attr_aggregator (peer, length, attr, flag);
1679 break;
1680 case BGP_ATTR_AS4_AGGREGATOR:
1681 ret = bgp_attr_as4_aggregator (peer, length, attr, &as4_aggregator, &as4_aggregator_addr);
1682 break;
1683 case BGP_ATTR_COMMUNITIES:
1684 ret = bgp_attr_community (peer, length, attr, flag);
1685 break;
1686 case BGP_ATTR_ORIGINATOR_ID:
1687 ret = bgp_attr_originator_id (peer, length, attr, flag);
1688 break;
1689 case BGP_ATTR_CLUSTER_LIST:
1690 ret = bgp_attr_cluster_list (peer, length, attr, flag);
1691 break;
1692 case BGP_ATTR_MP_REACH_NLRI:
1693 ret = bgp_mp_reach_parse (peer, length, attr, mp_update);
1694 break;
1695 case BGP_ATTR_MP_UNREACH_NLRI:
1696 ret = bgp_mp_unreach_parse (peer, length, mp_withdraw);
1697 break;
1698 case BGP_ATTR_EXT_COMMUNITIES:
1699 ret = bgp_attr_ext_communities (peer, length, attr, flag);
1700 break;
1701 case BGP_ATTR_AS_PATHLIMIT:
1702 ret = bgp_attr_aspathlimit (peer, length, attr, flag, startp);
1703 break;
1704 default:
1705 ret = bgp_attr_unknown (peer, attr, flag, type, length, startp);
1706 break;
1709 /* If error occured immediately return to the caller. */
1710 if (ret < 0)
1712 zlog (peer->log, LOG_WARNING,
1713 "%s: Attribute %s, parse error",
1714 peer->host,
1715 LOOKUP (attr_str, type));
1716 bgp_notify_send (peer,
1717 BGP_NOTIFY_UPDATE_ERR,
1718 BGP_NOTIFY_UPDATE_MAL_ATTR);
1719 return ret;
1722 /* Check the fetched length. */
1723 if (BGP_INPUT_PNT (peer) != attr_endp)
1725 zlog (peer->log, LOG_WARNING,
1726 "%s: BGP attribute %s, fetch error",
1727 peer->host, LOOKUP (attr_str, type));
1728 bgp_notify_send (peer,
1729 BGP_NOTIFY_UPDATE_ERR,
1730 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1731 return -1;
1735 /* Check final read pointer is same as end pointer. */
1736 if (BGP_INPUT_PNT (peer) != endp)
1738 zlog (peer->log, LOG_WARNING,
1739 "%s BGP attribute %s, length mismatch",
1740 peer->host, LOOKUP (attr_str, type));
1741 bgp_notify_send (peer,
1742 BGP_NOTIFY_UPDATE_ERR,
1743 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
1744 return -1;
1748 * At this place we can see whether we got AS4_PATH and/or
1749 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
1750 * We can not do this before we've read all attributes because
1751 * the as4 handling does not say whether AS4_PATH has to be sent
1752 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
1753 * in relationship to AGGREGATOR.
1754 * So, to be defensive, we are not relying on any order and read
1755 * all attributes first, including these 32bit ones, and now,
1756 * afterwards, we look what and if something is to be done for as4.
1758 if (bgp_attr_munge_as4_attrs (peer, attr, as4_path,
1759 as4_aggregator, &as4_aggregator_addr))
1760 return -1;
1762 /* At this stage, we have done all fiddling with as4, and the
1763 * resulting info is in attr->aggregator resp. attr->aspath
1764 * so we can chuck as4_aggregator and as4_path alltogether in
1765 * order to save memory
1767 if ( as4_path )
1769 aspath_unintern( as4_path ); /* unintern - it is in the hash */
1770 as4_path = NULL;
1771 /* The flag that we got this is still there, but that does not
1772 * do any trouble
1776 * The "rest" of the code does nothing with as4_aggregator.
1777 * there is no memory attached specifically which is not part
1778 * of the attr.
1779 * so ignoring just means do nothing.
1782 * Finally do the checks on the aspath we did not do yet
1783 * because we waited for a potentially synthesized aspath.
1785 if ( attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH)))
1787 ret = bgp_attr_aspath_check( peer, attr );
1788 if ( ret < 0 )
1789 return ret;
1792 /* Finally intern unknown attribute. */
1793 if (attr->extra && attr->extra->transit)
1794 attr->extra->transit = transit_intern (attr->extra->transit);
1796 return 0;
1799 /* Well-known attribute check. */
1801 bgp_attr_check (struct peer *peer, struct attr *attr)
1803 u_char type = 0;
1805 if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN)))
1806 type = BGP_ATTR_ORIGIN;
1808 if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)))
1809 type = BGP_ATTR_AS_PATH;
1811 if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP)))
1812 type = BGP_ATTR_NEXT_HOP;
1814 if (peer_sort (peer) == BGP_PEER_IBGP
1815 && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
1816 type = BGP_ATTR_LOCAL_PREF;
1818 if (type)
1820 zlog (peer->log, LOG_WARNING,
1821 "%s Missing well-known attribute %d.",
1822 peer->host, type);
1823 bgp_notify_send_with_data (peer,
1824 BGP_NOTIFY_UPDATE_ERR,
1825 BGP_NOTIFY_UPDATE_MISS_ATTR,
1826 &type, 1);
1827 return -1;
1829 return 0;
1832 int stream_put_prefix (struct stream *, struct prefix *);
1834 /* Make attribute packet. */
1835 bgp_size_t
1836 bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
1837 struct stream *s, struct attr *attr, struct prefix *p,
1838 afi_t afi, safi_t safi, struct peer *from,
1839 struct prefix_rd *prd, u_char *tag)
1841 size_t cp;
1842 size_t aspath_sizep;
1843 struct aspath *aspath;
1844 int send_as4_path = 0;
1845 int send_as4_aggregator = 0;
1846 int use32bit = (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0;
1848 if (! bgp)
1849 bgp = bgp_get_default ();
1851 /* Remember current pointer. */
1852 cp = stream_get_endp (s);
1854 /* Origin attribute. */
1855 stream_putc (s, BGP_ATTR_FLAG_TRANS);
1856 stream_putc (s, BGP_ATTR_ORIGIN);
1857 stream_putc (s, 1);
1858 stream_putc (s, attr->origin);
1860 /* AS path attribute. */
1862 /* If remote-peer is EBGP */
1863 if (peer_sort (peer) == BGP_PEER_EBGP
1864 && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
1865 || attr->aspath->segments == NULL)
1866 && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)))
1868 aspath = aspath_dup (attr->aspath);
1870 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
1872 /* Strip the confed info, and then stuff our path CONFED_ID
1873 on the front */
1874 aspath = aspath_delete_confed_seq (aspath);
1875 aspath = aspath_add_seq (aspath, bgp->confed_id);
1877 else
1879 aspath = aspath_add_seq (aspath, peer->local_as);
1880 if (peer->change_local_as)
1881 aspath = aspath_add_seq (aspath, peer->change_local_as);
1884 else if (peer_sort (peer) == BGP_PEER_CONFED)
1886 /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
1887 aspath = aspath_dup (attr->aspath);
1888 aspath = aspath_add_confed_seq (aspath, peer->local_as);
1890 else
1891 aspath = attr->aspath;
1893 /* If peer is not AS4 capable, then:
1894 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
1895 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
1896 * types are in it (i.e. exclude them if they are there)
1897 * AND do this only if there is at least one asnum > 65535 in the path!
1898 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
1899 * all ASnums > 65535 to BGP_AS_TRANS
1902 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
1903 stream_putc (s, BGP_ATTR_AS_PATH);
1904 aspath_sizep = stream_get_endp (s);
1905 stream_putw (s, 0);
1906 stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, use32bit));
1908 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
1909 * in the path
1911 if (!use32bit && aspath_has_as4 (aspath))
1912 send_as4_path = 1; /* we'll do this later, at the correct place */
1914 /* Nexthop attribute. */
1915 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP)
1917 stream_putc (s, BGP_ATTR_FLAG_TRANS);
1918 stream_putc (s, BGP_ATTR_NEXT_HOP);
1919 stream_putc (s, 4);
1920 if (safi == SAFI_MPLS_VPN)
1922 if (attr->nexthop.s_addr == 0)
1923 stream_put_ipv4 (s, peer->nexthop.v4.s_addr);
1924 else
1925 stream_put_ipv4 (s, attr->nexthop.s_addr);
1927 else
1928 stream_put_ipv4 (s, attr->nexthop.s_addr);
1931 /* MED attribute. */
1932 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
1934 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
1935 stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
1936 stream_putc (s, 4);
1937 stream_putl (s, attr->med);
1940 /* Local preference. */
1941 if (peer_sort (peer) == BGP_PEER_IBGP ||
1942 peer_sort (peer) == BGP_PEER_CONFED)
1944 stream_putc (s, BGP_ATTR_FLAG_TRANS);
1945 stream_putc (s, BGP_ATTR_LOCAL_PREF);
1946 stream_putc (s, 4);
1947 stream_putl (s, attr->local_pref);
1950 /* Atomic aggregate. */
1951 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
1953 stream_putc (s, BGP_ATTR_FLAG_TRANS);
1954 stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
1955 stream_putc (s, 0);
1958 /* Aggregator. */
1959 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
1961 assert (attr->extra);
1963 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
1964 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
1965 stream_putc (s, BGP_ATTR_AGGREGATOR);
1967 if (use32bit)
1969 /* AS4 capable peer */
1970 stream_putc (s, 8);
1971 stream_putl (s, attr->extra->aggregator_as);
1973 else
1975 /* 2-byte AS peer */
1976 stream_putc (s, 6);
1978 /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
1979 if ( attr->extra->aggregator_as > 65535 )
1981 stream_putw (s, BGP_AS_TRANS);
1983 /* we have to send AS4_AGGREGATOR, too.
1984 * we'll do that later in order to send attributes in ascending
1985 * order.
1987 send_as4_aggregator = 1;
1989 else
1990 stream_putw (s, (u_int16_t) attr->extra->aggregator_as);
1992 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
1995 /* Community attribute. */
1996 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
1997 && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)))
1999 if (attr->community->size * 4 > 255)
2001 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2002 stream_putc (s, BGP_ATTR_COMMUNITIES);
2003 stream_putw (s, attr->community->size * 4);
2005 else
2007 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2008 stream_putc (s, BGP_ATTR_COMMUNITIES);
2009 stream_putc (s, attr->community->size * 4);
2011 stream_put (s, attr->community->val, attr->community->size * 4);
2014 /* Route Reflector. */
2015 if (peer_sort (peer) == BGP_PEER_IBGP
2016 && from
2017 && peer_sort (from) == BGP_PEER_IBGP)
2019 /* Originator ID. */
2020 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2021 stream_putc (s, BGP_ATTR_ORIGINATOR_ID);
2022 stream_putc (s, 4);
2024 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
2025 stream_put_in_addr (s, &attr->extra->originator_id);
2026 else
2027 stream_put_in_addr (s, &from->remote_id);
2029 /* Cluster list. */
2030 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2031 stream_putc (s, BGP_ATTR_CLUSTER_LIST);
2033 if (attr->extra && attr->extra->cluster)
2035 stream_putc (s, attr->extra->cluster->length + 4);
2036 /* If this peer configuration's parent BGP has cluster_id. */
2037 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
2038 stream_put_in_addr (s, &bgp->cluster_id);
2039 else
2040 stream_put_in_addr (s, &bgp->router_id);
2041 stream_put (s, attr->extra->cluster->list,
2042 attr->extra->cluster->length);
2044 else
2046 stream_putc (s, 4);
2047 /* If this peer configuration's parent BGP has cluster_id. */
2048 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
2049 stream_put_in_addr (s, &bgp->cluster_id);
2050 else
2051 stream_put_in_addr (s, &bgp->router_id);
2055 #ifdef HAVE_IPV6
2056 /* If p is IPv6 address put it into attribute. */
2057 if (p->family == AF_INET6)
2059 unsigned long sizep;
2060 struct attr_extra *attre = attr->extra;
2062 assert (attr->extra);
2064 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2065 stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
2066 sizep = stream_get_endp (s);
2067 stream_putc (s, 0); /* Marker: Attribute length. */
2068 stream_putw (s, AFI_IP6); /* AFI */
2069 stream_putc (s, safi); /* SAFI */
2071 stream_putc (s, attre->mp_nexthop_len);
2073 if (attre->mp_nexthop_len == 16)
2074 stream_put (s, &attre->mp_nexthop_global, 16);
2075 else if (attre->mp_nexthop_len == 32)
2077 stream_put (s, &attre->mp_nexthop_global, 16);
2078 stream_put (s, &attre->mp_nexthop_local, 16);
2081 /* SNPA */
2082 stream_putc (s, 0);
2084 /* Prefix write. */
2085 stream_put_prefix (s, p);
2087 /* Set MP attribute length. */
2088 stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
2090 #endif /* HAVE_IPV6 */
2092 if (p->family == AF_INET && safi == SAFI_MULTICAST)
2094 unsigned long sizep;
2096 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2097 stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
2098 sizep = stream_get_endp (s);
2099 stream_putc (s, 0); /* Marker: Attribute Length. */
2100 stream_putw (s, AFI_IP); /* AFI */
2101 stream_putc (s, SAFI_MULTICAST); /* SAFI */
2103 stream_putc (s, 4);
2104 stream_put_ipv4 (s, attr->nexthop.s_addr);
2106 /* SNPA */
2107 stream_putc (s, 0);
2109 /* Prefix write. */
2110 stream_put_prefix (s, p);
2112 /* Set MP attribute length. */
2113 stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
2116 if (p->family == AF_INET && safi == SAFI_MPLS_VPN)
2118 unsigned long sizep;
2120 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2121 stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
2122 sizep = stream_get_endp (s);
2123 stream_putc (s, 0); /* Length of this attribute. */
2124 stream_putw (s, AFI_IP); /* AFI */
2125 stream_putc (s, BGP_SAFI_VPNV4); /* SAFI */
2127 stream_putc (s, 12);
2128 stream_putl (s, 0);
2129 stream_putl (s, 0);
2130 stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
2132 /* SNPA */
2133 stream_putc (s, 0);
2135 /* Tag, RD, Prefix write. */
2136 stream_putc (s, p->prefixlen + 88);
2137 stream_put (s, tag, 3);
2138 stream_put (s, prd->val, 8);
2139 stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
2141 /* Set MP attribute length. */
2142 stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
2145 /* Extended Communities attribute. */
2146 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
2147 && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)))
2149 struct attr_extra *attre = attr->extra;
2151 assert (attre);
2153 if (peer_sort (peer) == BGP_PEER_IBGP
2154 || peer_sort (peer) == BGP_PEER_CONFED)
2156 if (attre->ecommunity->size * 8 > 255)
2158 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2159 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2160 stream_putw (s, attre->ecommunity->size * 8);
2162 else
2164 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2165 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2166 stream_putc (s, attre->ecommunity->size * 8);
2168 stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8);
2170 else
2172 u_int8_t *pnt;
2173 int tbit;
2174 int ecom_tr_size = 0;
2175 int i;
2177 for (i = 0; i < attre->ecommunity->size; i++)
2179 pnt = attre->ecommunity->val + (i * 8);
2180 tbit = *pnt;
2182 if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
2183 continue;
2185 ecom_tr_size++;
2188 if (ecom_tr_size)
2190 if (ecom_tr_size * 8 > 255)
2192 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2193 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2194 stream_putw (s, ecom_tr_size * 8);
2196 else
2198 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2199 stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
2200 stream_putc (s, ecom_tr_size * 8);
2203 for (i = 0; i < attre->ecommunity->size; i++)
2205 pnt = attre->ecommunity->val + (i * 8);
2206 tbit = *pnt;
2208 if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
2209 continue;
2211 stream_put (s, pnt, 8);
2217 if ( send_as4_path )
2219 /* If the peer is NOT As4 capable, AND */
2220 /* there are ASnums > 65535 in path THEN
2221 * give out AS4_PATH */
2223 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
2224 * path segments!
2225 * Hm, I wonder... confederation things *should* only be at
2226 * the beginning of an aspath, right? Then we should use
2227 * aspath_delete_confed_seq for this, because it is already
2228 * there! (JK)
2229 * Folks, talk to me: what is reasonable here!?
2231 aspath = aspath_delete_confed_seq (aspath);
2233 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
2234 stream_putc (s, BGP_ATTR_AS4_PATH);
2235 aspath_sizep = stream_get_endp (s);
2236 stream_putw (s, 0);
2237 stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1));
2240 if (aspath != attr->aspath)
2241 aspath_free (aspath);
2243 if ( send_as4_aggregator )
2245 assert (attr->extra);
2247 /* send AS4_AGGREGATOR, at this place */
2248 /* this section of code moved here in order to ensure the correct
2249 * *ascending* order of attributes
2251 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2252 stream_putc (s, BGP_ATTR_AS4_AGGREGATOR);
2253 stream_putc (s, 8);
2254 stream_putl (s, attr->extra->aggregator_as);
2255 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
2258 /* AS-Pathlimit */
2259 if (attr->pathlimit.ttl)
2261 u_int32_t as = attr->pathlimit.as;
2263 /* should already have been done in announce_check(),
2264 * but just in case..
2266 if (!as)
2267 as = peer->local_as;
2269 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2270 stream_putc (s, BGP_ATTR_AS_PATHLIMIT);
2271 stream_putc (s, 5);
2272 stream_putc (s, attr->pathlimit.ttl);
2273 stream_putl (s, as);
2276 /* Unknown transit attribute. */
2277 if (attr->extra && attr->extra->transit)
2278 stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
2280 /* Return total size of attribute. */
2281 return stream_get_endp (s) - cp;
2284 bgp_size_t
2285 bgp_packet_withdraw (struct peer *peer, struct stream *s, struct prefix *p,
2286 afi_t afi, safi_t safi, struct prefix_rd *prd,
2287 u_char *tag)
2289 unsigned long cp;
2290 unsigned long attrlen_pnt;
2291 bgp_size_t size;
2293 cp = stream_get_endp (s);
2295 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2296 stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
2298 attrlen_pnt = stream_get_endp (s);
2299 stream_putc (s, 0); /* Length of this attribute. */
2301 stream_putw (s, family2afi (p->family));
2303 if (safi == SAFI_MPLS_VPN)
2305 /* SAFI */
2306 stream_putc (s, BGP_SAFI_VPNV4);
2308 /* prefix. */
2309 stream_putc (s, p->prefixlen + 88);
2310 stream_put (s, tag, 3);
2311 stream_put (s, prd->val, 8);
2312 stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
2314 else
2316 /* SAFI */
2317 stream_putc (s, safi);
2319 /* prefix */
2320 stream_put_prefix (s, p);
2323 /* Set MP attribute length. */
2324 size = stream_get_endp (s) - attrlen_pnt - 1;
2325 stream_putc_at (s, attrlen_pnt, size);
2327 return stream_get_endp (s) - cp;
2330 /* Initialization of attribute. */
2331 void
2332 bgp_attr_init (void)
2334 aspath_init ();
2335 attrhash_init ();
2336 community_init ();
2337 ecommunity_init ();
2338 cluster_init ();
2339 transit_init ();
2342 void
2343 bgp_attr_finish (void)
2345 aspath_finish ();
2346 attrhash_finish ();
2347 community_finish ();
2348 ecommunity_finish ();
2349 cluster_finish ();
2350 transit_finish ();
2353 /* Make attribute packet. */
2354 void
2355 bgp_dump_routes_attr (struct stream *s, struct attr *attr,
2356 struct prefix *prefix)
2358 unsigned long cp;
2359 unsigned long len;
2360 size_t aspath_lenp;
2361 struct aspath *aspath;
2363 /* Remember current pointer. */
2364 cp = stream_get_endp (s);
2366 /* Place holder of length. */
2367 stream_putw (s, 0);
2369 /* Origin attribute. */
2370 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2371 stream_putc (s, BGP_ATTR_ORIGIN);
2372 stream_putc (s, 1);
2373 stream_putc (s, attr->origin);
2375 aspath = attr->aspath;
2377 stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2378 stream_putc (s, BGP_ATTR_AS_PATH);
2379 aspath_lenp = stream_get_endp (s);
2380 stream_putw (s, 0);
2382 stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1));
2384 /* Nexthop attribute. */
2385 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
2386 if(prefix != NULL
2387 #ifdef HAVE_IPV6
2388 && prefix->family != AF_INET6
2389 #endif /* HAVE_IPV6 */
2392 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2393 stream_putc (s, BGP_ATTR_NEXT_HOP);
2394 stream_putc (s, 4);
2395 stream_put_ipv4 (s, attr->nexthop.s_addr);
2398 /* MED attribute. */
2399 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
2401 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
2402 stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
2403 stream_putc (s, 4);
2404 stream_putl (s, attr->med);
2407 /* Local preference. */
2408 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
2410 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2411 stream_putc (s, BGP_ATTR_LOCAL_PREF);
2412 stream_putc (s, 4);
2413 stream_putl (s, attr->local_pref);
2416 /* Atomic aggregate. */
2417 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
2419 stream_putc (s, BGP_ATTR_FLAG_TRANS);
2420 stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
2421 stream_putc (s, 0);
2424 /* Aggregator. */
2425 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
2427 assert (attr->extra);
2428 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2429 stream_putc (s, BGP_ATTR_AGGREGATOR);
2430 stream_putc (s, 8);
2431 stream_putl (s, attr->extra->aggregator_as);
2432 stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
2435 /* Community attribute. */
2436 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))
2438 if (attr->community->size * 4 > 255)
2440 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
2441 stream_putc (s, BGP_ATTR_COMMUNITIES);
2442 stream_putw (s, attr->community->size * 4);
2444 else
2446 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2447 stream_putc (s, BGP_ATTR_COMMUNITIES);
2448 stream_putc (s, attr->community->size * 4);
2450 stream_put (s, attr->community->val, attr->community->size * 4);
2453 #ifdef HAVE_IPV6
2454 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
2455 if (prefix != NULL && prefix->family == AF_INET6 && attr->extra &&
2456 (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) )
2458 int sizep;
2459 struct attr_extra *attre = attr->extra;
2461 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
2462 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
2463 sizep = stream_get_endp (s);
2465 /* MP header */
2466 stream_putc (s, 0); /* Marker: Attribute length. */
2467 stream_putw(s, AFI_IP6); /* AFI */
2468 stream_putc(s, SAFI_UNICAST); /* SAFI */
2470 /* Next hop */
2471 stream_putc(s, attre->mp_nexthop_len);
2472 stream_put(s, &attre->mp_nexthop_global, 16);
2473 if (attre->mp_nexthop_len == 32)
2474 stream_put(s, &attre->mp_nexthop_local, 16);
2476 /* SNPA */
2477 stream_putc(s, 0);
2479 /* Prefix */
2480 stream_put_prefix(s, prefix);
2482 /* Set MP attribute length. */
2483 stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
2485 #endif /* HAVE_IPV6 */
2487 /* AS-Pathlimit */
2488 if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AS_PATHLIMIT))
2490 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
2491 stream_putc (s, BGP_ATTR_AS_PATHLIMIT);
2492 stream_putc (s, 5);
2493 stream_putc (s, attr->pathlimit.ttl);
2494 stream_putl (s, attr->pathlimit.as);
2497 /* Return total size of attribute. */
2498 len = stream_get_endp (s) - cp - 2;
2499 stream_putw_at (s, cp, len);