]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_attr.c
Merge pull request #1590 from chiragshah6/pim_dev
[mirror_frr.git] / bgpd / bgp_attr.c
1 /* BGP attributes management routines.
2 * Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "linklist.h"
24 #include "prefix.h"
25 #include "memory.h"
26 #include "vector.h"
27 #include "stream.h"
28 #include "log.h"
29 #include "hash.h"
30 #include "jhash.h"
31 #include "queue.h"
32 #include "table.h"
33 #include "filter.h"
34 #include "command.h"
35
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_aspath.h"
40 #include "bgpd/bgp_community.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_label.h"
43 #include "bgpd/bgp_packet.h"
44 #include "bgpd/bgp_ecommunity.h"
45 #include "bgpd/bgp_lcommunity.h"
46 #include "bgpd/bgp_updgrp.h"
47 #include "bgpd/bgp_encap_types.h"
48 #if ENABLE_BGP_VNC
49 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
50 #include "bgp_encap_types.h"
51 #include "bgp_vnc_types.h"
52 #endif
53 #include "bgp_encap_types.h"
54 #include "bgp_evpn.h"
55
56 /* Attribute strings for logging. */
57 static const struct message attr_str[] = {
58 {BGP_ATTR_ORIGIN, "ORIGIN"},
59 {BGP_ATTR_AS_PATH, "AS_PATH"},
60 {BGP_ATTR_NEXT_HOP, "NEXT_HOP"},
61 {BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC"},
62 {BGP_ATTR_LOCAL_PREF, "LOCAL_PREF"},
63 {BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE"},
64 {BGP_ATTR_AGGREGATOR, "AGGREGATOR"},
65 {BGP_ATTR_COMMUNITIES, "COMMUNITY"},
66 {BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID"},
67 {BGP_ATTR_CLUSTER_LIST, "CLUSTER_LIST"},
68 {BGP_ATTR_DPA, "DPA"},
69 {BGP_ATTR_ADVERTISER, "ADVERTISER"},
70 {BGP_ATTR_RCID_PATH, "RCID_PATH"},
71 {BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI"},
72 {BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI"},
73 {BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES"},
74 {BGP_ATTR_AS4_PATH, "AS4_PATH"},
75 {BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR"},
76 {BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT"},
77 {BGP_ATTR_PMSI_TUNNEL, "PMSI_TUNNEL_ATTRIBUTE"},
78 {BGP_ATTR_ENCAP, "ENCAP"},
79 #if ENABLE_BGP_VNC
80 {BGP_ATTR_VNC, "VNC"},
81 #endif
82 {BGP_ATTR_LARGE_COMMUNITIES, "LARGE_COMMUNITY"},
83 {BGP_ATTR_PREFIX_SID, "PREFIX_SID"},
84 {0}};
85
86 static const struct message attr_flag_str[] =
87 {
88 {BGP_ATTR_FLAG_OPTIONAL, "Optional"},
89 {BGP_ATTR_FLAG_TRANS, "Transitive"},
90 {BGP_ATTR_FLAG_PARTIAL, "Partial"},
91 /* bgp_attr_flags_diagnose() relies on this bit being last in
92 this list */
93 {BGP_ATTR_FLAG_EXTLEN, "Extended Length"},
94 {0}};
95
96 static struct hash *cluster_hash;
97
98 static void *cluster_hash_alloc(void *p)
99 {
100 const struct cluster_list *val = (const struct cluster_list *)p;
101 struct cluster_list *cluster;
102
103 cluster = XMALLOC(MTYPE_CLUSTER, sizeof(struct cluster_list));
104 cluster->length = val->length;
105
106 if (cluster->length) {
107 cluster->list = XMALLOC(MTYPE_CLUSTER_VAL, val->length);
108 memcpy(cluster->list, val->list, val->length);
109 } else
110 cluster->list = NULL;
111
112 cluster->refcnt = 0;
113
114 return cluster;
115 }
116
117 /* Cluster list related functions. */
118 static struct cluster_list *cluster_parse(struct in_addr *pnt, int length)
119 {
120 struct cluster_list tmp;
121 struct cluster_list *cluster;
122
123 tmp.length = length;
124 tmp.list = pnt;
125
126 cluster = hash_get(cluster_hash, &tmp, cluster_hash_alloc);
127 cluster->refcnt++;
128 return cluster;
129 }
130
131 int cluster_loop_check(struct cluster_list *cluster, struct in_addr originator)
132 {
133 int i;
134
135 for (i = 0; i < cluster->length / 4; i++)
136 if (cluster->list[i].s_addr == originator.s_addr)
137 return 1;
138 return 0;
139 }
140
141 static unsigned int cluster_hash_key_make(void *p)
142 {
143 const struct cluster_list *cluster = p;
144
145 return jhash(cluster->list, cluster->length, 0);
146 }
147
148 static int cluster_hash_cmp(const void *p1, const void *p2)
149 {
150 const struct cluster_list *cluster1 = p1;
151 const struct cluster_list *cluster2 = p2;
152
153 return (cluster1->length == cluster2->length
154 && memcmp(cluster1->list, cluster2->list, cluster1->length)
155 == 0);
156 }
157
158 static void cluster_free(struct cluster_list *cluster)
159 {
160 if (cluster->list)
161 XFREE(MTYPE_CLUSTER_VAL, cluster->list);
162 XFREE(MTYPE_CLUSTER, cluster);
163 }
164
165 static struct cluster_list *cluster_intern(struct cluster_list *cluster)
166 {
167 struct cluster_list *find;
168
169 find = hash_get(cluster_hash, cluster, cluster_hash_alloc);
170 find->refcnt++;
171
172 return find;
173 }
174
175 void cluster_unintern(struct cluster_list *cluster)
176 {
177 if (cluster->refcnt)
178 cluster->refcnt--;
179
180 if (cluster->refcnt == 0) {
181 hash_release(cluster_hash, cluster);
182 cluster_free(cluster);
183 }
184 }
185
186 static void cluster_init(void)
187 {
188 cluster_hash = hash_create(cluster_hash_key_make,
189 cluster_hash_cmp,
190 "BGP Cluster");
191 }
192
193 static void cluster_finish(void)
194 {
195 hash_clean(cluster_hash, (void (*)(void *))cluster_free);
196 hash_free(cluster_hash);
197 cluster_hash = NULL;
198 }
199
200 static struct hash *encap_hash = NULL;
201 #if ENABLE_BGP_VNC
202 static struct hash *vnc_hash = NULL;
203 #endif
204
205 struct bgp_attr_encap_subtlv *encap_tlv_dup(struct bgp_attr_encap_subtlv *orig)
206 {
207 struct bgp_attr_encap_subtlv *new;
208 struct bgp_attr_encap_subtlv *tail;
209 struct bgp_attr_encap_subtlv *p;
210
211 for (p = orig, tail = new = NULL; p; p = p->next) {
212 int size = sizeof(struct bgp_attr_encap_subtlv) + p->length;
213 if (tail) {
214 tail->next = XCALLOC(MTYPE_ENCAP_TLV, size);
215 tail = tail->next;
216 } else {
217 tail = new = XCALLOC(MTYPE_ENCAP_TLV, size);
218 }
219 assert(tail);
220 memcpy(tail, p, size);
221 tail->next = NULL;
222 }
223
224 return new;
225 }
226
227 static void encap_free(struct bgp_attr_encap_subtlv *p)
228 {
229 struct bgp_attr_encap_subtlv *next;
230 while (p) {
231 next = p->next;
232 p->next = NULL;
233 XFREE(MTYPE_ENCAP_TLV, p);
234 p = next;
235 }
236 }
237
238 void bgp_attr_flush_encap(struct attr *attr)
239 {
240 if (!attr)
241 return;
242
243 if (attr->encap_subtlvs) {
244 encap_free(attr->encap_subtlvs);
245 attr->encap_subtlvs = NULL;
246 }
247 #if ENABLE_BGP_VNC
248 if (attr->vnc_subtlvs) {
249 encap_free(attr->vnc_subtlvs);
250 attr->vnc_subtlvs = NULL;
251 }
252 #endif
253 }
254
255 /*
256 * Compare encap sub-tlv chains
257 *
258 * 1 = equivalent
259 * 0 = not equivalent
260 *
261 * This algorithm could be made faster if needed
262 */
263 static int encap_same(struct bgp_attr_encap_subtlv *h1,
264 struct bgp_attr_encap_subtlv *h2)
265 {
266 struct bgp_attr_encap_subtlv *p;
267 struct bgp_attr_encap_subtlv *q;
268
269 if (h1 == h2)
270 return 1;
271 if (h1 == NULL || h2 == NULL)
272 return 0;
273
274 for (p = h1; p; p = p->next) {
275 for (q = h2; q; q = q->next) {
276 if ((p->type == q->type) && (p->length == q->length)
277 && !memcmp(p->value, q->value, p->length)) {
278
279 break;
280 }
281 }
282 if (!q)
283 return 0;
284 }
285
286 for (p = h2; p; p = p->next) {
287 for (q = h1; q; q = q->next) {
288 if ((p->type == q->type) && (p->length == q->length)
289 && !memcmp(p->value, q->value, p->length)) {
290
291 break;
292 }
293 }
294 if (!q)
295 return 0;
296 }
297
298 return 1;
299 }
300
301 static void *encap_hash_alloc(void *p)
302 {
303 /* Encap structure is already allocated. */
304 return p;
305 }
306
307 typedef enum {
308 ENCAP_SUBTLV_TYPE,
309 #if ENABLE_BGP_VNC
310 VNC_SUBTLV_TYPE
311 #endif
312 } encap_subtlv_type;
313
314 static struct bgp_attr_encap_subtlv *
315 encap_intern(struct bgp_attr_encap_subtlv *encap, encap_subtlv_type type)
316 {
317 struct bgp_attr_encap_subtlv *find;
318 struct hash *hash = encap_hash;
319 #if ENABLE_BGP_VNC
320 if (type == VNC_SUBTLV_TYPE)
321 hash = vnc_hash;
322 #endif
323
324 find = hash_get(hash, encap, encap_hash_alloc);
325 if (find != encap)
326 encap_free(encap);
327 find->refcnt++;
328
329 return find;
330 }
331
332 static void encap_unintern(struct bgp_attr_encap_subtlv **encapp,
333 encap_subtlv_type type)
334 {
335 struct bgp_attr_encap_subtlv *encap = *encapp;
336 if (encap->refcnt)
337 encap->refcnt--;
338
339 if (encap->refcnt == 0) {
340 struct hash *hash = encap_hash;
341 #if ENABLE_BGP_VNC
342 if (type == VNC_SUBTLV_TYPE)
343 hash = vnc_hash;
344 #endif
345 hash_release(hash, encap);
346 encap_free(encap);
347 *encapp = NULL;
348 }
349 }
350
351 static unsigned int encap_hash_key_make(void *p)
352 {
353 const struct bgp_attr_encap_subtlv *encap = p;
354
355 return jhash(encap->value, encap->length, 0);
356 }
357
358 static int encap_hash_cmp(const void *p1, const void *p2)
359 {
360 return encap_same((struct bgp_attr_encap_subtlv *)p1,
361 (struct bgp_attr_encap_subtlv *)p2);
362 }
363
364 static void encap_init(void)
365 {
366 encap_hash = hash_create(encap_hash_key_make,
367 encap_hash_cmp,
368 "BGP Encap Hash");
369 #if ENABLE_BGP_VNC
370 vnc_hash = hash_create(encap_hash_key_make,
371 encap_hash_cmp,
372 "BGP VNC Hash");
373 #endif
374 }
375
376 static void encap_finish(void)
377 {
378 hash_clean(encap_hash, (void (*)(void *))encap_free);
379 hash_free(encap_hash);
380 encap_hash = NULL;
381 #if ENABLE_BGP_VNC
382 hash_clean(vnc_hash, (void (*)(void *))encap_free);
383 hash_free(vnc_hash);
384 vnc_hash = NULL;
385 #endif
386 }
387
388 static bool overlay_index_same(const struct attr *a1, const struct attr *a2)
389 {
390 if (!a1 && a2)
391 return false;
392 if (!a2 && a1)
393 return false;
394 if (!a1 && !a2)
395 return true;
396 return !memcmp(&(a1->evpn_overlay), &(a2->evpn_overlay),
397 sizeof(struct overlay_index));
398 }
399
400 /* Unknown transit attribute. */
401 static struct hash *transit_hash;
402
403 static void transit_free(struct transit *transit)
404 {
405 if (transit->val)
406 XFREE(MTYPE_TRANSIT_VAL, transit->val);
407 XFREE(MTYPE_TRANSIT, transit);
408 }
409
410 static void *transit_hash_alloc(void *p)
411 {
412 /* Transit structure is already allocated. */
413 return p;
414 }
415
416 static struct transit *transit_intern(struct transit *transit)
417 {
418 struct transit *find;
419
420 find = hash_get(transit_hash, transit, transit_hash_alloc);
421 if (find != transit)
422 transit_free(transit);
423 find->refcnt++;
424
425 return find;
426 }
427
428 void transit_unintern(struct transit *transit)
429 {
430 if (transit->refcnt)
431 transit->refcnt--;
432
433 if (transit->refcnt == 0) {
434 hash_release(transit_hash, transit);
435 transit_free(transit);
436 }
437 }
438
439 static unsigned int transit_hash_key_make(void *p)
440 {
441 const struct transit *transit = p;
442
443 return jhash(transit->val, transit->length, 0);
444 }
445
446 static int transit_hash_cmp(const void *p1, const void *p2)
447 {
448 const struct transit *transit1 = p1;
449 const struct transit *transit2 = p2;
450
451 return (transit1->length == transit2->length
452 && memcmp(transit1->val, transit2->val, transit1->length) == 0);
453 }
454
455 static void transit_init(void)
456 {
457 transit_hash = hash_create(transit_hash_key_make,
458 transit_hash_cmp,
459 "BGP Transit Hash");
460 }
461
462 static void transit_finish(void)
463 {
464 hash_clean(transit_hash, (void (*)(void *))transit_free);
465 hash_free(transit_hash);
466 transit_hash = NULL;
467 }
468
469 /* Attribute hash routines. */
470 static struct hash *attrhash;
471
472 /* Shallow copy of an attribute
473 * Though, not so shallow that it doesn't copy the contents
474 * of the attr_extra pointed to by 'extra'
475 */
476 void bgp_attr_dup(struct attr *new, struct attr *orig)
477 {
478 *new = *orig;
479 }
480
481 unsigned long int attr_count(void)
482 {
483 return attrhash->count;
484 }
485
486 unsigned long int attr_unknown_count(void)
487 {
488 return transit_hash->count;
489 }
490
491 unsigned int attrhash_key_make(void *p)
492 {
493 const struct attr *attr = (struct attr *)p;
494 uint32_t key = 0;
495 #define MIX(val) key = jhash_1word(val, key)
496 #define MIX3(a, b, c) key = jhash_3words((a), (b), (c), key)
497
498 MIX3(attr->origin, attr->nexthop.s_addr, attr->med);
499 MIX3(attr->local_pref, attr->aggregator_as, attr->aggregator_addr.s_addr);
500 MIX3(attr->weight, attr->mp_nexthop_global_in.s_addr,
501 attr->originator_id.s_addr);
502 MIX3(attr->tag, attr->label, attr->label_index);
503
504 if (attr->aspath)
505 MIX(aspath_key_make(attr->aspath));
506 if (attr->community)
507 MIX(community_hash_make(attr->community));
508
509 if (attr->lcommunity)
510 MIX(lcommunity_hash_make(attr->lcommunity));
511 if (attr->ecommunity)
512 MIX(ecommunity_hash_make(attr->ecommunity));
513 if (attr->cluster)
514 MIX(cluster_hash_key_make(attr->cluster));
515 if (attr->transit)
516 MIX(transit_hash_key_make(attr->transit));
517 if (attr->encap_subtlvs)
518 MIX(encap_hash_key_make(attr->encap_subtlvs));
519 #if ENABLE_BGP_VNC
520 if (attr->vnc_subtlvs)
521 MIX(encap_hash_key_make(attr->vnc_subtlvs));
522 #endif
523 MIX(attr->mp_nexthop_len);
524 key = jhash(attr->mp_nexthop_global.s6_addr, IPV6_MAX_BYTELEN, key);
525 key = jhash(attr->mp_nexthop_local.s6_addr, IPV6_MAX_BYTELEN, key);
526
527 return key;
528 }
529
530 int attrhash_cmp(const void *p1, const void *p2)
531 {
532 const struct attr *attr1 = p1;
533 const struct attr *attr2 = p2;
534
535 if (attr1->flag == attr2->flag && attr1->origin == attr2->origin
536 && attr1->nexthop.s_addr == attr2->nexthop.s_addr
537 && attr1->aspath == attr2->aspath
538 && attr1->community == attr2->community && attr1->med == attr2->med
539 && attr1->local_pref == attr2->local_pref
540 && attr1->rmap_change_flags == attr2->rmap_change_flags) {
541 if (attr1->aggregator_as == attr2->aggregator_as
542 && attr1->aggregator_addr.s_addr
543 == attr2->aggregator_addr.s_addr
544 && attr1->weight == attr2->weight
545 && attr1->tag == attr2->tag
546 && attr1->label_index == attr2->label_index
547 && attr1->mp_nexthop_len == attr2->mp_nexthop_len
548 && attr1->ecommunity == attr2->ecommunity
549 && attr1->lcommunity == attr2->lcommunity
550 && attr1->cluster == attr2->cluster
551 && attr1->transit == attr2->transit
552 && (attr1->encap_tunneltype == attr2->encap_tunneltype)
553 && encap_same(attr1->encap_subtlvs, attr2->encap_subtlvs)
554 #if ENABLE_BGP_VNC
555 && encap_same(attr1->vnc_subtlvs, attr2->vnc_subtlvs)
556 #endif
557 && IPV6_ADDR_SAME(&attr1->mp_nexthop_global,
558 &attr2->mp_nexthop_global)
559 && IPV6_ADDR_SAME(&attr1->mp_nexthop_local,
560 &attr2->mp_nexthop_local)
561 && IPV4_ADDR_SAME(&attr1->mp_nexthop_global_in,
562 &attr2->mp_nexthop_global_in)
563 && IPV4_ADDR_SAME(&attr1->originator_id,
564 &attr2->originator_id)
565 && overlay_index_same(attr1, attr2))
566 return 1;
567 }
568
569 return 0;
570 }
571
572 static void attrhash_init(void)
573 {
574 attrhash = hash_create(attrhash_key_make,
575 attrhash_cmp,
576 "BGP Attributes");
577 }
578
579 /*
580 * special for hash_clean below
581 */
582 static void attr_vfree(void *attr)
583 {
584 XFREE(MTYPE_ATTR, attr);
585 }
586
587 static void attrhash_finish(void)
588 {
589 hash_clean(attrhash, attr_vfree);
590 hash_free(attrhash);
591 attrhash = NULL;
592 }
593
594 static void attr_show_all_iterator(struct hash_backet *backet, struct vty *vty)
595 {
596 struct attr *attr = backet->data;
597
598 vty_out(vty, "attr[%ld] nexthop %s\n", attr->refcnt,
599 inet_ntoa(attr->nexthop));
600 }
601
602 void attr_show_all(struct vty *vty)
603 {
604 hash_iterate(attrhash, (void (*)(struct hash_backet *,
605 void *))attr_show_all_iterator,
606 vty);
607 }
608
609 static void *bgp_attr_hash_alloc(void *p)
610 {
611 struct attr *val = (struct attr *)p;
612 struct attr *attr;
613
614 attr = XMALLOC(MTYPE_ATTR, sizeof(struct attr));
615 *attr = *val;
616 if (val->encap_subtlvs) {
617 val->encap_subtlvs = NULL;
618 }
619 #if ENABLE_BGP_VNC
620 if (val->vnc_subtlvs) {
621 val->vnc_subtlvs = NULL;
622 }
623 #endif
624 attr->refcnt = 0;
625 return attr;
626 }
627
628 /* Internet argument attribute. */
629 struct attr *bgp_attr_intern(struct attr *attr)
630 {
631 struct attr *find;
632
633 /* Intern referenced strucutre. */
634 if (attr->aspath) {
635 if (!attr->aspath->refcnt)
636 attr->aspath = aspath_intern(attr->aspath);
637 else
638 attr->aspath->refcnt++;
639 }
640 if (attr->community) {
641 if (!attr->community->refcnt)
642 attr->community = community_intern(attr->community);
643 else
644 attr->community->refcnt++;
645 }
646
647 if (attr->ecommunity) {
648 if (!attr->ecommunity->refcnt)
649 attr->ecommunity = ecommunity_intern(attr->ecommunity);
650 else
651 attr->ecommunity->refcnt++;
652 }
653 if (attr->lcommunity) {
654 if (!attr->lcommunity->refcnt)
655 attr->lcommunity = lcommunity_intern(attr->lcommunity);
656 else
657 attr->lcommunity->refcnt++;
658 }
659 if (attr->cluster) {
660 if (!attr->cluster->refcnt)
661 attr->cluster = cluster_intern(attr->cluster);
662 else
663 attr->cluster->refcnt++;
664 }
665 if (attr->transit) {
666 if (!attr->transit->refcnt)
667 attr->transit = transit_intern(attr->transit);
668 else
669 attr->transit->refcnt++;
670 }
671 if (attr->encap_subtlvs) {
672 if (!attr->encap_subtlvs->refcnt)
673 attr->encap_subtlvs = encap_intern(attr->encap_subtlvs,
674 ENCAP_SUBTLV_TYPE);
675 else
676 attr->encap_subtlvs->refcnt++;
677 }
678 #if ENABLE_BGP_VNC
679 if (attr->vnc_subtlvs) {
680 if (!attr->vnc_subtlvs->refcnt)
681 attr->vnc_subtlvs = encap_intern(attr->vnc_subtlvs,
682 VNC_SUBTLV_TYPE);
683 else
684 attr->vnc_subtlvs->refcnt++;
685 }
686 #endif
687
688 /* At this point, attr only contains intern'd pointers. that means
689 * if we find it in attrhash, it has all the same pointers and we
690 * correctly updated the refcounts on these.
691 * If we don't find it, we need to allocate a one because in all
692 * cases this returns a new reference to a hashed attr, but the input
693 * wasn't on hash. */
694 find = (struct attr *)hash_get(attrhash, attr, bgp_attr_hash_alloc);
695 find->refcnt++;
696
697 return find;
698 }
699
700 /* Make network statement's attribute. */
701 struct attr *bgp_attr_default_set(struct attr *attr, u_char origin)
702 {
703 memset(attr, 0, sizeof(struct attr));
704
705 attr->origin = origin;
706 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN);
707 attr->aspath = aspath_empty();
708 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH);
709 attr->weight = BGP_ATTR_DEFAULT_WEIGHT;
710 attr->tag = 0;
711 attr->label_index = BGP_INVALID_LABEL_INDEX;
712 attr->label = MPLS_INVALID_LABEL;
713 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
714 attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
715
716 return attr;
717 }
718
719 /* Create the attributes for an aggregate */
720 struct attr *bgp_attr_aggregate_intern(struct bgp *bgp, u_char origin,
721 struct aspath *aspath,
722 struct community *community, int as_set,
723 u_char atomic_aggregate)
724 {
725 struct attr attr;
726 struct attr *new;
727
728 memset(&attr, 0, sizeof(struct attr));
729
730 /* Origin attribute. */
731 attr.origin = origin;
732 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN);
733
734 /* AS path attribute. */
735 if (aspath)
736 attr.aspath = aspath_intern(aspath);
737 else
738 attr.aspath = aspath_empty();
739 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH);
740
741 /* Next hop attribute. */
742 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
743
744 if (community) {
745 u_int32_t gshut = COMMUNITY_GSHUT;
746
747 /* If we are not shutting down ourselves and we are
748 * aggregating a route that contains the GSHUT community we
749 * need to remove that community when creating the aggregate */
750 if (!bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN) &&
751 community_include(community, gshut)) {
752 community_del_val(community, &gshut);
753 }
754
755 attr.community = community;
756 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
757 }
758
759 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) {
760 bgp_attr_add_gshut_community(&attr);
761 }
762
763 attr.label_index = BGP_INVALID_LABEL_INDEX;
764 attr.label = MPLS_INVALID_LABEL;
765 attr.weight = BGP_ATTR_DEFAULT_WEIGHT;
766 attr.mp_nexthop_len = IPV6_MAX_BYTELEN;
767 if (!as_set || atomic_aggregate)
768 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
769 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR);
770 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
771 attr.aggregator_as = bgp->confed_id;
772 else
773 attr.aggregator_as = bgp->as;
774 attr.aggregator_addr = bgp->router_id;
775 attr.label_index = BGP_INVALID_LABEL_INDEX;
776 attr.label = MPLS_INVALID_LABEL;
777
778 new = bgp_attr_intern(&attr);
779
780 aspath_unintern(&new->aspath);
781 return new;
782 }
783
784 /* Unintern just the sub-components of the attr, but not the attr */
785 void bgp_attr_unintern_sub(struct attr *attr)
786 {
787 /* aspath refcount shoud be decrement. */
788 if (attr->aspath)
789 aspath_unintern(&attr->aspath);
790 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH));
791
792 if (attr->community)
793 community_unintern(&attr->community);
794 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES));
795
796 if (attr->ecommunity)
797 ecommunity_unintern(&attr->ecommunity);
798 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES));
799
800 if (attr->lcommunity)
801 lcommunity_unintern(&attr->lcommunity);
802 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES));
803
804 if (attr->cluster)
805 cluster_unintern(attr->cluster);
806 UNSET_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST));
807
808 if (attr->transit)
809 transit_unintern(attr->transit);
810
811 if (attr->encap_subtlvs)
812 encap_unintern(&attr->encap_subtlvs, ENCAP_SUBTLV_TYPE);
813
814 #if ENABLE_BGP_VNC
815 if (attr->vnc_subtlvs)
816 encap_unintern(&attr->vnc_subtlvs, VNC_SUBTLV_TYPE);
817 #endif
818 }
819
820 /*
821 * We have some show commands that let you experimentally
822 * apply a route-map. When we apply the route-map
823 * we are reseting values but not saving them for
824 * posterity via intern'ing( because route-maps don't
825 * do that) but at this point in time we need
826 * to compare the new attr to the old and if the
827 * routemap has changed it we need to, as Snoop Dog says,
828 * Drop it like it's hot
829 */
830 void bgp_attr_undup(struct attr *new, struct attr *old)
831 {
832 if (new->aspath != old->aspath)
833 aspath_free(new->aspath);
834
835 if (new->community != old->community)
836 community_free(new->community);
837
838 if (new->ecommunity != old->ecommunity)
839 ecommunity_free(&new->ecommunity);
840
841 if (new->lcommunity != old->lcommunity)
842 lcommunity_free(&new->lcommunity);
843
844 }
845
846 /* Free bgp attribute and aspath. */
847 void bgp_attr_unintern(struct attr **pattr)
848 {
849 struct attr *attr = *pattr;
850 struct attr *ret;
851 struct attr tmp;
852
853 /* Decrement attribute reference. */
854 attr->refcnt--;
855
856 tmp = *attr;
857
858 /* If reference becomes zero then free attribute object. */
859 if (attr->refcnt == 0) {
860 ret = hash_release(attrhash, attr);
861 assert(ret != NULL);
862 XFREE(MTYPE_ATTR, attr);
863 *pattr = NULL;
864 }
865
866 bgp_attr_unintern_sub(&tmp);
867 }
868
869 void bgp_attr_flush(struct attr *attr)
870 {
871 if (attr->aspath && !attr->aspath->refcnt) {
872 aspath_free(attr->aspath);
873 attr->aspath = NULL;
874 }
875 if (attr->community && !attr->community->refcnt) {
876 community_free(attr->community);
877 attr->community = NULL;
878 }
879
880 if (attr->ecommunity && !attr->ecommunity->refcnt)
881 ecommunity_free(&attr->ecommunity);
882 if (attr->lcommunity && !attr->lcommunity->refcnt)
883 lcommunity_free(&attr->lcommunity);
884 if (attr->cluster && !attr->cluster->refcnt) {
885 cluster_free(attr->cluster);
886 attr->cluster = NULL;
887 }
888 if (attr->transit && !attr->transit->refcnt) {
889 transit_free(attr->transit);
890 attr->transit = NULL;
891 }
892 if (attr->encap_subtlvs && !attr->encap_subtlvs->refcnt) {
893 encap_free(attr->encap_subtlvs);
894 attr->encap_subtlvs = NULL;
895 }
896 #if ENABLE_BGP_VNC
897 if (attr->vnc_subtlvs && !attr->vnc_subtlvs->refcnt) {
898 encap_free(attr->vnc_subtlvs);
899 attr->vnc_subtlvs = NULL;
900 }
901 #endif
902 }
903
904 /* Implement draft-scudder-idr-optional-transitive behaviour and
905 * avoid resetting sessions for malformed attributes which are
906 * are partial/optional and hence where the error likely was not
907 * introduced by the sending neighbour.
908 */
909 static bgp_attr_parse_ret_t
910 bgp_attr_malformed(struct bgp_attr_parser_args *args, u_char subcode,
911 bgp_size_t length)
912 {
913 struct peer *const peer = args->peer;
914 const u_int8_t flags = args->flags;
915 /* startp and length must be special-cased, as whether or not to
916 * send the attribute data with the NOTIFY depends on the error,
917 * the caller therefore signals this with the seperate length argument
918 */
919 u_char *notify_datap = (length > 0 ? args->startp : NULL);
920
921 /* Only relax error handling for eBGP peers */
922 if (peer->sort != BGP_PEER_EBGP) {
923 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR, subcode,
924 notify_datap, length);
925 return BGP_ATTR_PARSE_ERROR;
926 }
927
928 /* Adjust the stream getp to the end of the attribute, in case we can
929 * still proceed but the caller hasn't read all the attribute.
930 */
931 stream_set_getp(BGP_INPUT(peer),
932 (args->startp - STREAM_DATA(BGP_INPUT(peer)))
933 + args->total);
934
935 switch (args->type) {
936 /* where an attribute is relatively inconsequential, e.g. it does not
937 * affect route selection, and can be safely ignored, then any such
938 * attributes which are malformed should just be ignored and the route
939 * processed as normal.
940 */
941 case BGP_ATTR_AS4_AGGREGATOR:
942 case BGP_ATTR_AGGREGATOR:
943 case BGP_ATTR_ATOMIC_AGGREGATE:
944 return BGP_ATTR_PARSE_PROCEED;
945
946 /* Core attributes, particularly ones which may influence route
947 * selection, should always cause session resets
948 */
949 case BGP_ATTR_ORIGIN:
950 case BGP_ATTR_AS_PATH:
951 case BGP_ATTR_NEXT_HOP:
952 case BGP_ATTR_MULTI_EXIT_DISC:
953 case BGP_ATTR_LOCAL_PREF:
954 case BGP_ATTR_COMMUNITIES:
955 case BGP_ATTR_ORIGINATOR_ID:
956 case BGP_ATTR_CLUSTER_LIST:
957 case BGP_ATTR_MP_REACH_NLRI:
958 case BGP_ATTR_MP_UNREACH_NLRI:
959 case BGP_ATTR_EXT_COMMUNITIES:
960 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR, subcode,
961 notify_datap, length);
962 return BGP_ATTR_PARSE_ERROR;
963 }
964
965 /* Partial optional attributes that are malformed should not cause
966 * the whole session to be reset. Instead treat it as a withdrawal
967 * of the routes, if possible.
968 */
969 if (CHECK_FLAG(flags, BGP_ATTR_FLAG_TRANS)
970 && CHECK_FLAG(flags, BGP_ATTR_FLAG_OPTIONAL)
971 && CHECK_FLAG(flags, BGP_ATTR_FLAG_PARTIAL))
972 return BGP_ATTR_PARSE_WITHDRAW;
973
974 /* default to reset */
975 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
976 }
977
978 /* Find out what is wrong with the path attribute flag bits and log the error.
979 "Flag bits" here stand for Optional, Transitive and Partial, but not for
980 Extended Length. Checking O/T/P bits at once implies, that the attribute
981 being diagnosed is defined by RFC as either a "well-known" or an "optional,
982 non-transitive" attribute. */
983 static void
984 bgp_attr_flags_diagnose(struct bgp_attr_parser_args *args,
985 u_int8_t desired_flags /* how RFC says it must be */
986 )
987 {
988 u_char seen = 0, i;
989 u_char real_flags = args->flags;
990 const u_int8_t attr_code = args->type;
991
992 desired_flags &= ~BGP_ATTR_FLAG_EXTLEN;
993 real_flags &= ~BGP_ATTR_FLAG_EXTLEN;
994 for (i = 0; i <= 2; i++) /* O,T,P, but not E */
995 if (CHECK_FLAG(desired_flags, attr_flag_str[i].key)
996 != CHECK_FLAG(real_flags, attr_flag_str[i].key)) {
997 zlog_err("%s attribute must%s be flagged as \"%s\"",
998 lookup_msg(attr_str, attr_code, NULL),
999 CHECK_FLAG(desired_flags, attr_flag_str[i].key)
1000 ? ""
1001 : " not",
1002 attr_flag_str[i].str);
1003 seen = 1;
1004 }
1005 if (!seen) {
1006 zlog_debug(
1007 "Strange, %s called for attr %s, but no problem found with flags"
1008 " (real flags 0x%x, desired 0x%x)",
1009 __func__, lookup_msg(attr_str, attr_code, NULL),
1010 real_flags, desired_flags);
1011 }
1012 }
1013
1014 /* Required flags for attributes. EXTLEN will be masked off when testing,
1015 * as will PARTIAL for optional+transitive attributes.
1016 */
1017 const u_int8_t attr_flags_values[] = {
1018 [BGP_ATTR_ORIGIN] = BGP_ATTR_FLAG_TRANS,
1019 [BGP_ATTR_AS_PATH] = BGP_ATTR_FLAG_TRANS,
1020 [BGP_ATTR_NEXT_HOP] = BGP_ATTR_FLAG_TRANS,
1021 [BGP_ATTR_MULTI_EXIT_DISC] = BGP_ATTR_FLAG_OPTIONAL,
1022 [BGP_ATTR_LOCAL_PREF] = BGP_ATTR_FLAG_TRANS,
1023 [BGP_ATTR_ATOMIC_AGGREGATE] = BGP_ATTR_FLAG_TRANS,
1024 [BGP_ATTR_AGGREGATOR] =
1025 BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
1026 [BGP_ATTR_COMMUNITIES] =
1027 BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
1028 [BGP_ATTR_ORIGINATOR_ID] = BGP_ATTR_FLAG_OPTIONAL,
1029 [BGP_ATTR_CLUSTER_LIST] = BGP_ATTR_FLAG_OPTIONAL,
1030 [BGP_ATTR_MP_REACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
1031 [BGP_ATTR_MP_UNREACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
1032 [BGP_ATTR_EXT_COMMUNITIES] =
1033 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1034 [BGP_ATTR_AS4_PATH] =
1035 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1036 [BGP_ATTR_AS4_AGGREGATOR] =
1037 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1038 [BGP_ATTR_PMSI_TUNNEL] =
1039 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1040 [BGP_ATTR_LARGE_COMMUNITIES] =
1041 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1042 [BGP_ATTR_PREFIX_SID] =
1043 BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
1044 };
1045 static const size_t attr_flags_values_max = array_size(attr_flags_values) - 1;
1046
1047 static int bgp_attr_flag_invalid(struct bgp_attr_parser_args *args)
1048 {
1049 u_int8_t mask = BGP_ATTR_FLAG_EXTLEN;
1050 const u_int8_t flags = args->flags;
1051 const u_int8_t attr_code = args->type;
1052
1053 /* there may be attributes we don't know about */
1054 if (attr_code > attr_flags_values_max)
1055 return 0;
1056 if (attr_flags_values[attr_code] == 0)
1057 return 0;
1058
1059 /* RFC4271, "For well-known attributes, the Transitive bit MUST be set
1060 * to
1061 * 1."
1062 */
1063 if (!CHECK_FLAG(BGP_ATTR_FLAG_OPTIONAL, flags)
1064 && !CHECK_FLAG(BGP_ATTR_FLAG_TRANS, flags)) {
1065 zlog_err(
1066 "%s well-known attributes must have transitive flag set (%x)",
1067 lookup_msg(attr_str, attr_code, NULL), flags);
1068 return 1;
1069 }
1070
1071 /* "For well-known attributes and for optional non-transitive
1072 * attributes,
1073 * the Partial bit MUST be set to 0."
1074 */
1075 if (CHECK_FLAG(flags, BGP_ATTR_FLAG_PARTIAL)) {
1076 if (!CHECK_FLAG(flags, BGP_ATTR_FLAG_OPTIONAL)) {
1077 zlog_err(
1078 "%s well-known attribute "
1079 "must NOT have the partial flag set (%x)",
1080 lookup_msg(attr_str, attr_code, NULL), flags);
1081 return 1;
1082 }
1083 if (CHECK_FLAG(flags, BGP_ATTR_FLAG_OPTIONAL)
1084 && !CHECK_FLAG(flags, BGP_ATTR_FLAG_TRANS)) {
1085 zlog_err(
1086 "%s optional + transitive attribute "
1087 "must NOT have the partial flag set (%x)",
1088 lookup_msg(attr_str, attr_code, NULL), flags);
1089 return 1;
1090 }
1091 }
1092
1093 /* Optional transitive attributes may go through speakers that don't
1094 * reocgnise them and set the Partial bit.
1095 */
1096 if (CHECK_FLAG(flags, BGP_ATTR_FLAG_OPTIONAL)
1097 && CHECK_FLAG(flags, BGP_ATTR_FLAG_TRANS))
1098 SET_FLAG(mask, BGP_ATTR_FLAG_PARTIAL);
1099
1100 if ((flags & ~mask) == attr_flags_values[attr_code])
1101 return 0;
1102
1103 bgp_attr_flags_diagnose(args, attr_flags_values[attr_code]);
1104 return 1;
1105 }
1106
1107 /* Get origin attribute of the update message. */
1108 static bgp_attr_parse_ret_t bgp_attr_origin(struct bgp_attr_parser_args *args)
1109 {
1110 struct peer *const peer = args->peer;
1111 struct attr *const attr = args->attr;
1112 const bgp_size_t length = args->length;
1113
1114 /* If any recognized attribute has Attribute Length that conflicts
1115 with the expected length (based on the attribute type code), then
1116 the Error Subcode is set to Attribute Length Error. The Data
1117 field contains the erroneous attribute (type, length and
1118 value). */
1119 if (length != 1) {
1120 zlog_err("Origin attribute length is not one %d", length);
1121 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1122 args->total);
1123 }
1124
1125 /* Fetch origin attribute. */
1126 attr->origin = stream_getc(BGP_INPUT(peer));
1127
1128 /* If the ORIGIN attribute has an undefined value, then the Error
1129 Subcode is set to Invalid Origin Attribute. The Data field
1130 contains the unrecognized attribute (type, length and value). */
1131 if ((attr->origin != BGP_ORIGIN_IGP) && (attr->origin != BGP_ORIGIN_EGP)
1132 && (attr->origin != BGP_ORIGIN_INCOMPLETE)) {
1133 zlog_err("Origin attribute value is invalid %d", attr->origin);
1134 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
1135 args->total);
1136 }
1137
1138 /* Set oring attribute flag. */
1139 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGIN);
1140
1141 return 0;
1142 }
1143
1144 /* Parse AS path information. This function is wrapper of
1145 aspath_parse. */
1146 static int bgp_attr_aspath(struct bgp_attr_parser_args *args)
1147 {
1148 struct attr *const attr = args->attr;
1149 struct peer *const peer = args->peer;
1150 const bgp_size_t length = args->length;
1151
1152 /*
1153 * peer with AS4 => will get 4Byte ASnums
1154 * otherwise, will get 16 Bit
1155 */
1156 attr->aspath = aspath_parse(peer->curr, length,
1157 CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV));
1158
1159 /* In case of IBGP, length will be zero. */
1160 if (!attr->aspath) {
1161 zlog_err("Malformed AS path from %s, length is %d", peer->host,
1162 length);
1163 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
1164 0);
1165 }
1166
1167 /* Set aspath attribute flag. */
1168 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH);
1169
1170 return BGP_ATTR_PARSE_PROCEED;
1171 }
1172
1173 static bgp_attr_parse_ret_t bgp_attr_aspath_check(struct peer *const peer,
1174 struct attr *const attr)
1175 {
1176 /* These checks were part of bgp_attr_aspath, but with
1177 * as4 we should to check aspath things when
1178 * aspath synthesizing with as4_path has already taken place.
1179 * Otherwise we check ASPATH and use the synthesized thing, and that is
1180 * not right.
1181 * So do the checks later, i.e. here
1182 */
1183 struct bgp *bgp = peer->bgp;
1184 struct aspath *aspath;
1185
1186 /* Confederation sanity check. */
1187 if ((peer->sort == BGP_PEER_CONFED
1188 && !aspath_left_confed_check(attr->aspath))
1189 || (peer->sort == BGP_PEER_EBGP
1190 && aspath_confed_check(attr->aspath))) {
1191 zlog_err("Malformed AS path from %s", peer->host);
1192 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1193 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
1194 return BGP_ATTR_PARSE_ERROR;
1195 }
1196
1197 /* First AS check for EBGP. */
1198 if (bgp != NULL && bgp_flag_check(bgp, BGP_FLAG_ENFORCE_FIRST_AS)) {
1199 if (peer->sort == BGP_PEER_EBGP
1200 && !aspath_firstas_check(attr->aspath, peer->as)) {
1201 zlog_err("%s incorrect first AS (must be %u)",
1202 peer->host, peer->as);
1203 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1204 BGP_NOTIFY_UPDATE_MAL_AS_PATH);
1205 return BGP_ATTR_PARSE_ERROR;
1206 }
1207 }
1208
1209 /* local-as prepend */
1210 if (peer->change_local_as
1211 && !CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND)) {
1212 aspath = aspath_dup(attr->aspath);
1213 aspath = aspath_add_seq(aspath, peer->change_local_as);
1214 aspath_unintern(&attr->aspath);
1215 attr->aspath = aspath_intern(aspath);
1216 }
1217
1218 return BGP_ATTR_PARSE_PROCEED;
1219 }
1220
1221 /* Parse AS4 path information. This function is another wrapper of
1222 aspath_parse. */
1223 static int bgp_attr_as4_path(struct bgp_attr_parser_args *args,
1224 struct aspath **as4_path)
1225 {
1226 struct peer *const peer = args->peer;
1227 struct attr *const attr = args->attr;
1228 const bgp_size_t length = args->length;
1229
1230 *as4_path = aspath_parse(peer->curr, length, 1);
1231
1232 /* In case of IBGP, length will be zero. */
1233 if (!*as4_path) {
1234 zlog_err("Malformed AS4 path from %s, length is %d", peer->host,
1235 length);
1236 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
1237 0);
1238 }
1239
1240 /* Set aspath attribute flag. */
1241 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH);
1242
1243 return BGP_ATTR_PARSE_PROCEED;
1244 }
1245
1246 /* Nexthop attribute. */
1247 static bgp_attr_parse_ret_t bgp_attr_nexthop(struct bgp_attr_parser_args *args)
1248 {
1249 struct peer *const peer = args->peer;
1250 struct attr *const attr = args->attr;
1251 const bgp_size_t length = args->length;
1252
1253 in_addr_t nexthop_h, nexthop_n;
1254
1255 /* Check nexthop attribute length. */
1256 if (length != 4) {
1257 zlog_err("Nexthop attribute length isn't four [%d]", length);
1258
1259 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1260 args->total);
1261 }
1262
1263 /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
1264 attribute must result in a NOTIFICATION message (this is implemented
1265 below).
1266 At the same time, semantically incorrect NEXT_HOP is more likely to
1267 be just
1268 logged locally (this is implemented somewhere else). The UPDATE
1269 message
1270 gets ignored in any of these cases. */
1271 nexthop_n = stream_get_ipv4(peer->curr);
1272 nexthop_h = ntohl(nexthop_n);
1273 if ((IPV4_NET0(nexthop_h) || IPV4_NET127(nexthop_h)
1274 || IPV4_CLASS_DE(nexthop_h))
1275 && !BGP_DEBUG(
1276 allow_martians,
1277 ALLOW_MARTIANS)) /* loopbacks may be used in testing */
1278 {
1279 char buf[INET_ADDRSTRLEN];
1280 inet_ntop(AF_INET, &nexthop_n, buf, INET_ADDRSTRLEN);
1281 zlog_err("Martian nexthop %s", buf);
1282 return bgp_attr_malformed(
1283 args, BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP, args->total);
1284 }
1285
1286 attr->nexthop.s_addr = nexthop_n;
1287 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1288
1289 return BGP_ATTR_PARSE_PROCEED;
1290 }
1291
1292 /* MED atrribute. */
1293 static bgp_attr_parse_ret_t bgp_attr_med(struct bgp_attr_parser_args *args)
1294 {
1295 struct peer *const peer = args->peer;
1296 struct attr *const attr = args->attr;
1297 const bgp_size_t length = args->length;
1298
1299 /* Length check. */
1300 if (length != 4) {
1301 zlog_err("MED attribute length isn't four [%d]", length);
1302
1303 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1304 args->total);
1305 }
1306
1307 attr->med = stream_getl(peer->curr);
1308
1309 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
1310
1311 return BGP_ATTR_PARSE_PROCEED;
1312 }
1313
1314 /* Local preference attribute. */
1315 static bgp_attr_parse_ret_t
1316 bgp_attr_local_pref(struct bgp_attr_parser_args *args)
1317 {
1318 struct peer *const peer = args->peer;
1319 struct attr *const attr = args->attr;
1320 const bgp_size_t length = args->length;
1321
1322 /* Length check. */
1323 if (length != 4) {
1324 zlog_err("LOCAL_PREF attribute length isn't 4 [%u]", length);
1325 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1326 args->total);
1327 }
1328
1329 /* If it is contained in an UPDATE message that is received from an
1330 external peer, then this attribute MUST be ignored by the
1331 receiving speaker. */
1332 if (peer->sort == BGP_PEER_EBGP) {
1333 stream_forward_getp(peer->curr, length);
1334 return BGP_ATTR_PARSE_PROCEED;
1335 }
1336
1337 attr->local_pref = stream_getl(peer->curr);
1338
1339 /* Set the local-pref flag. */
1340 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1341
1342 return BGP_ATTR_PARSE_PROCEED;
1343 }
1344
1345 /* Atomic aggregate. */
1346 static int bgp_attr_atomic(struct bgp_attr_parser_args *args)
1347 {
1348 struct attr *const attr = args->attr;
1349 const bgp_size_t length = args->length;
1350
1351 /* Length check. */
1352 if (length != 0) {
1353 zlog_err("ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
1354 length);
1355 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1356 args->total);
1357 }
1358
1359 /* Set atomic aggregate flag. */
1360 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
1361
1362 return BGP_ATTR_PARSE_PROCEED;
1363 }
1364
1365 /* Aggregator attribute */
1366 static int bgp_attr_aggregator(struct bgp_attr_parser_args *args)
1367 {
1368 struct peer *const peer = args->peer;
1369 struct attr *const attr = args->attr;
1370 const bgp_size_t length = args->length;
1371
1372 int wantedlen = 6;
1373
1374 /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
1375 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV))
1376 wantedlen = 8;
1377
1378 if (length != wantedlen) {
1379 zlog_err("AGGREGATOR attribute length isn't %u [%u]", wantedlen,
1380 length);
1381 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1382 args->total);
1383 }
1384
1385 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV))
1386 attr->aggregator_as = stream_getl(peer->curr);
1387 else
1388 attr->aggregator_as = stream_getw(peer->curr);
1389 attr->aggregator_addr.s_addr = stream_get_ipv4(peer->curr);
1390
1391 /* Set atomic aggregate flag. */
1392 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR);
1393
1394 return BGP_ATTR_PARSE_PROCEED;
1395 }
1396
1397 /* New Aggregator attribute */
1398 static bgp_attr_parse_ret_t
1399 bgp_attr_as4_aggregator(struct bgp_attr_parser_args *args,
1400 as_t *as4_aggregator_as,
1401 struct in_addr *as4_aggregator_addr)
1402 {
1403 struct peer *const peer = args->peer;
1404 struct attr *const attr = args->attr;
1405 const bgp_size_t length = args->length;
1406
1407 if (length != 8) {
1408 zlog_err("New Aggregator length is not 8 [%d]", length);
1409 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1410 0);
1411 }
1412
1413 *as4_aggregator_as = stream_getl(peer->curr);
1414 as4_aggregator_addr->s_addr = stream_get_ipv4(peer->curr);
1415
1416 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR);
1417
1418 return BGP_ATTR_PARSE_PROCEED;
1419 }
1420
1421 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
1422 */
1423 static bgp_attr_parse_ret_t
1424 bgp_attr_munge_as4_attrs(struct peer *const peer, struct attr *const attr,
1425 struct aspath *as4_path, as_t as4_aggregator,
1426 struct in_addr *as4_aggregator_addr)
1427 {
1428 int ignore_as4_path = 0;
1429 struct aspath *newpath;
1430
1431 if (!attr->aspath) {
1432 /* NULL aspath shouldn't be possible as bgp_attr_parse should
1433 * have
1434 * checked that all well-known, mandatory attributes were
1435 * present.
1436 *
1437 * Can only be a problem with peer itself - hard error
1438 */
1439 return BGP_ATTR_PARSE_ERROR;
1440 }
1441
1442 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)) {
1443 /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
1444 * if given.
1445 * It is worth a warning though, because the peer really
1446 * should not send them
1447 */
1448 if (BGP_DEBUG(as4, AS4)) {
1449 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))
1450 zlog_debug("[AS4] %s %s AS4_PATH", peer->host,
1451 "AS4 capable peer, yet it sent");
1452
1453 if (attr->flag
1454 & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR)))
1455 zlog_debug("[AS4] %s %s AS4_AGGREGATOR",
1456 peer->host,
1457 "AS4 capable peer, yet it sent");
1458 }
1459
1460 return BGP_ATTR_PARSE_PROCEED;
1461 }
1462
1463 /* We have a asn16 peer. First, look for AS4_AGGREGATOR
1464 * because that may override AS4_PATH
1465 */
1466 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR))) {
1467 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR))) {
1468 /* received both.
1469 * if the as_number in aggregator is not AS_TRANS,
1470 * then AS4_AGGREGATOR and AS4_PATH shall be ignored
1471 * and the Aggregator shall be taken as
1472 * info on the aggregating node, and the AS_PATH
1473 * shall be taken as the AS_PATH
1474 * otherwise
1475 * the Aggregator shall be ignored and the
1476 * AS4_AGGREGATOR shall be taken as the
1477 * Aggregating node and the AS_PATH is to be
1478 * constructed "as in all other cases"
1479 */
1480 if (attr->aggregator_as != BGP_AS_TRANS) {
1481 /* ignore */
1482 if (BGP_DEBUG(as4, AS4))
1483 zlog_debug(
1484 "[AS4] %s BGP not AS4 capable peer"
1485 " send AGGREGATOR != AS_TRANS and"
1486 " AS4_AGGREGATOR, so ignore"
1487 " AS4_AGGREGATOR and AS4_PATH",
1488 peer->host);
1489 ignore_as4_path = 1;
1490 } else {
1491 /* "New_aggregator shall be taken as aggregator"
1492 */
1493 attr->aggregator_as = as4_aggregator;
1494 attr->aggregator_addr.s_addr =
1495 as4_aggregator_addr->s_addr;
1496 }
1497 } else {
1498 /* We received a AS4_AGGREGATOR but no AGGREGATOR.
1499 * That is bogus - but reading the conditions
1500 * we have to handle AS4_AGGREGATOR as if it were
1501 * AGGREGATOR in that case
1502 */
1503 if (BGP_DEBUG(as4, AS4))
1504 zlog_debug(
1505 "[AS4] %s BGP not AS4 capable peer send"
1506 " AS4_AGGREGATOR but no AGGREGATOR, will take"
1507 " it as if AGGREGATOR with AS_TRANS had been there",
1508 peer->host);
1509 attr->aggregator_as = as4_aggregator;
1510 /* sweep it under the carpet and simulate a "good"
1511 * AGGREGATOR */
1512 attr->flag |= (ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR));
1513 }
1514 }
1515
1516 /* need to reconcile NEW_AS_PATH and AS_PATH */
1517 if (!ignore_as4_path
1518 && (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))) {
1519 newpath = aspath_reconcile_as4(attr->aspath, as4_path);
1520 aspath_unintern(&attr->aspath);
1521 attr->aspath = aspath_intern(newpath);
1522 }
1523 return BGP_ATTR_PARSE_PROCEED;
1524 }
1525
1526 /* Community attribute. */
1527 static bgp_attr_parse_ret_t
1528 bgp_attr_community(struct bgp_attr_parser_args *args)
1529 {
1530 struct peer *const peer = args->peer;
1531 struct attr *const attr = args->attr;
1532 const bgp_size_t length = args->length;
1533
1534 if (length == 0) {
1535 attr->community = NULL;
1536 return BGP_ATTR_PARSE_PROCEED;
1537 }
1538
1539 attr->community =
1540 community_parse((u_int32_t *)stream_pnt(peer->curr), length);
1541
1542 /* XXX: fix community_parse to use stream API and remove this */
1543 stream_forward_getp(peer->curr, length);
1544
1545 if (!attr->community)
1546 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1547 args->total);
1548
1549 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
1550
1551 return BGP_ATTR_PARSE_PROCEED;
1552 }
1553
1554 /* Originator ID attribute. */
1555 static bgp_attr_parse_ret_t
1556 bgp_attr_originator_id(struct bgp_attr_parser_args *args)
1557 {
1558 struct peer *const peer = args->peer;
1559 struct attr *const attr = args->attr;
1560 const bgp_size_t length = args->length;
1561
1562 /* Length check. */
1563 if (length != 4) {
1564 zlog_err("Bad originator ID length %d", length);
1565
1566 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1567 args->total);
1568 }
1569
1570 attr->originator_id.s_addr = stream_get_ipv4(peer->curr);
1571
1572 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID);
1573
1574 return BGP_ATTR_PARSE_PROCEED;
1575 }
1576
1577 /* Cluster list attribute. */
1578 static bgp_attr_parse_ret_t
1579 bgp_attr_cluster_list(struct bgp_attr_parser_args *args)
1580 {
1581 struct peer *const peer = args->peer;
1582 struct attr *const attr = args->attr;
1583 const bgp_size_t length = args->length;
1584
1585 /* Check length. */
1586 if (length % 4) {
1587 zlog_err("Bad cluster list length %d", length);
1588
1589 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
1590 args->total);
1591 }
1592
1593 attr->cluster =
1594 cluster_parse((struct in_addr *)stream_pnt(peer->curr), length);
1595
1596 /* XXX: Fix cluster_parse to use stream API and then remove this */
1597 stream_forward_getp(peer->curr, length);
1598
1599 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST);
1600
1601 return BGP_ATTR_PARSE_PROCEED;
1602 }
1603
1604 /* Multiprotocol reachability information parse. */
1605 int bgp_mp_reach_parse(struct bgp_attr_parser_args *args,
1606 struct bgp_nlri *mp_update)
1607 {
1608 iana_afi_t pkt_afi;
1609 afi_t afi;
1610 iana_safi_t pkt_safi;
1611 safi_t safi;
1612 bgp_size_t nlri_len;
1613 size_t start;
1614 struct stream *s;
1615 struct peer *const peer = args->peer;
1616 struct attr *const attr = args->attr;
1617 const bgp_size_t length = args->length;
1618
1619 /* Set end of packet. */
1620 s = BGP_INPUT(peer);
1621 start = stream_get_getp(s);
1622
1623 /* safe to read statically sized header? */
1624 #define BGP_MP_REACH_MIN_SIZE 5
1625 #define LEN_LEFT (length - (stream_get_getp(s) - start))
1626 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE)) {
1627 zlog_info("%s: %s sent invalid length, %lu", __func__,
1628 peer->host, (unsigned long)length);
1629 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1630 }
1631
1632 /* Load AFI, SAFI. */
1633 pkt_afi = stream_getw(s);
1634 pkt_safi = stream_getc(s);
1635
1636 /* Convert AFI, SAFI to internal values, check. */
1637 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
1638 /* Log if AFI or SAFI is unrecognized. This is not an error
1639 * unless
1640 * the attribute is otherwise malformed.
1641 */
1642 if (bgp_debug_update(peer, NULL, NULL, 0))
1643 zlog_debug(
1644 "%s: MP_REACH received AFI %u or SAFI %u is unrecognized",
1645 peer->host, pkt_afi, pkt_safi);
1646 return BGP_ATTR_PARSE_ERROR;
1647 }
1648
1649 /* Get nexthop length. */
1650 attr->mp_nexthop_len = stream_getc(s);
1651
1652 if (LEN_LEFT < attr->mp_nexthop_len) {
1653 zlog_info(
1654 "%s: %s, MP nexthop length, %u, goes past end of attribute",
1655 __func__, peer->host, attr->mp_nexthop_len);
1656 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1657 }
1658
1659 /* Nexthop length check. */
1660 switch (attr->mp_nexthop_len) {
1661 case BGP_ATTR_NHLEN_VPNV4:
1662 stream_getl(s); /* RD high */
1663 stream_getl(s); /* RD low */
1664 /*
1665 * NOTE: intentional fall through
1666 * - for consistency in rx processing
1667 *
1668 * The following comment is to signal GCC this intention
1669 * and supress the warning
1670 */
1671 /* FALLTHRU */
1672 case BGP_ATTR_NHLEN_IPV4:
1673 stream_get(&attr->mp_nexthop_global_in, s, IPV4_MAX_BYTELEN);
1674 /* Probably needed for RFC 2283 */
1675 if (attr->nexthop.s_addr == 0)
1676 memcpy(&attr->nexthop.s_addr,
1677 &attr->mp_nexthop_global_in, IPV4_MAX_BYTELEN);
1678 break;
1679 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
1680 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
1681 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
1682 stream_getl(s); /* RD high */
1683 stream_getl(s); /* RD low */
1684 }
1685 stream_get(&attr->mp_nexthop_global, s, IPV6_MAX_BYTELEN);
1686 break;
1687 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
1688 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL:
1689 if (attr->mp_nexthop_len
1690 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
1691 stream_getl(s); /* RD high */
1692 stream_getl(s); /* RD low */
1693 }
1694 stream_get(&attr->mp_nexthop_global, s, IPV6_MAX_BYTELEN);
1695 if (attr->mp_nexthop_len
1696 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
1697 stream_getl(s); /* RD high */
1698 stream_getl(s); /* RD low */
1699 }
1700 stream_get(&attr->mp_nexthop_local, s, IPV6_MAX_BYTELEN);
1701 if (!IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_local)) {
1702 char buf1[INET6_ADDRSTRLEN];
1703 char buf2[INET6_ADDRSTRLEN];
1704
1705 if (bgp_debug_update(peer, NULL, NULL, 1))
1706 zlog_debug(
1707 "%s rcvd nexthops %s, %s -- ignoring non-LL value",
1708 peer->host,
1709 inet_ntop(AF_INET6,
1710 &attr->mp_nexthop_global,
1711 buf1, INET6_ADDRSTRLEN),
1712 inet_ntop(AF_INET6,
1713 &attr->mp_nexthop_local, buf2,
1714 INET6_ADDRSTRLEN));
1715
1716 attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
1717 }
1718 break;
1719 default:
1720 zlog_info("%s: (%s) Wrong multiprotocol next hop length: %d",
1721 __func__, peer->host, attr->mp_nexthop_len);
1722 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1723 }
1724
1725 if (!LEN_LEFT) {
1726 zlog_info("%s: (%s) Failed to read SNPA and NLRI(s)", __func__,
1727 peer->host);
1728 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1729 }
1730
1731 {
1732 u_char val;
1733 if ((val = stream_getc(s)))
1734 zlog_warn(
1735 "%s sent non-zero value, %u, for defunct SNPA-length field",
1736 peer->host, val);
1737 }
1738
1739 /* must have nrli_len, what is left of the attribute */
1740 nlri_len = LEN_LEFT;
1741 if (nlri_len > STREAM_READABLE(s)) {
1742 zlog_info("%s: (%s) Failed to read NLRI", __func__, peer->host);
1743 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1744 }
1745
1746 if (!nlri_len) {
1747 zlog_info("%s: (%s) No Reachability, Treating as a EOR marker",
1748 __func__, peer->host);
1749
1750 mp_update->afi = afi;
1751 mp_update->safi = safi;
1752 return BGP_ATTR_PARSE_EOR;
1753 }
1754
1755 mp_update->afi = afi;
1756 mp_update->safi = safi;
1757 mp_update->nlri = stream_pnt(s);
1758 mp_update->length = nlri_len;
1759
1760 stream_forward_getp(s, nlri_len);
1761
1762 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI);
1763
1764 return BGP_ATTR_PARSE_PROCEED;
1765 #undef LEN_LEFT
1766 }
1767
1768 /* Multiprotocol unreachable parse */
1769 int bgp_mp_unreach_parse(struct bgp_attr_parser_args *args,
1770 struct bgp_nlri *mp_withdraw)
1771 {
1772 struct stream *s;
1773 iana_afi_t pkt_afi;
1774 afi_t afi;
1775 iana_safi_t pkt_safi;
1776 safi_t safi;
1777 u_int16_t withdraw_len;
1778 struct peer *const peer = args->peer;
1779 struct attr *const attr = args->attr;
1780 const bgp_size_t length = args->length;
1781
1782 s = peer->curr;
1783
1784 #define BGP_MP_UNREACH_MIN_SIZE 3
1785 if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE))
1786 return BGP_ATTR_PARSE_ERROR_NOTIFYPLS;
1787
1788 pkt_afi = stream_getw(s);
1789 pkt_safi = stream_getc(s);
1790
1791 /* Convert AFI, SAFI to internal values, check. */
1792 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
1793 /* Log if AFI or SAFI is unrecognized. This is not an error
1794 * unless
1795 * the attribute is otherwise malformed.
1796 */
1797 if (bgp_debug_update(peer, NULL, NULL, 0))
1798 zlog_debug(
1799 "%s: MP_UNREACH received AFI %u or SAFI %u is unrecognized",
1800 peer->host, pkt_afi, pkt_safi);
1801 return BGP_ATTR_PARSE_ERROR;
1802 }
1803
1804 withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE;
1805
1806 mp_withdraw->afi = afi;
1807 mp_withdraw->safi = safi;
1808 mp_withdraw->nlri = stream_pnt(s);
1809 mp_withdraw->length = withdraw_len;
1810
1811 stream_forward_getp(s, withdraw_len);
1812
1813 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI);
1814
1815 return BGP_ATTR_PARSE_PROCEED;
1816 }
1817
1818 /* Large Community attribute. */
1819 static bgp_attr_parse_ret_t
1820 bgp_attr_large_community(struct bgp_attr_parser_args *args)
1821 {
1822 struct peer *const peer = args->peer;
1823 struct attr *const attr = args->attr;
1824 const bgp_size_t length = args->length;
1825
1826 /*
1827 * Large community follows new attribute format.
1828 */
1829 if (length == 0) {
1830 attr->lcommunity = NULL;
1831 /* Empty extcomm doesn't seem to be invalid per se */
1832 return BGP_ATTR_PARSE_PROCEED;
1833 }
1834
1835 attr->lcommunity =
1836 lcommunity_parse((u_int8_t *)stream_pnt(peer->curr), length);
1837 /* XXX: fix ecommunity_parse to use stream API */
1838 stream_forward_getp(peer->curr, length);
1839
1840 if (!attr->lcommunity)
1841 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1842 args->total);
1843
1844 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
1845
1846 return BGP_ATTR_PARSE_PROCEED;
1847 }
1848
1849 /* Extended Community attribute. */
1850 static bgp_attr_parse_ret_t
1851 bgp_attr_ext_communities(struct bgp_attr_parser_args *args)
1852 {
1853 struct peer *const peer = args->peer;
1854 struct attr *const attr = args->attr;
1855 const bgp_size_t length = args->length;
1856 u_char sticky = 0;
1857
1858 if (length == 0) {
1859 attr->ecommunity = NULL;
1860 /* Empty extcomm doesn't seem to be invalid per se */
1861 return BGP_ATTR_PARSE_PROCEED;
1862 }
1863
1864 attr->ecommunity =
1865 ecommunity_parse((u_int8_t *)stream_pnt(peer->curr), length);
1866 /* XXX: fix ecommunity_parse to use stream API */
1867 stream_forward_getp(peer->curr, length);
1868
1869 if (!attr->ecommunity)
1870 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
1871 args->total);
1872
1873 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
1874
1875 /* Extract MAC mobility sequence number, if any. */
1876 attr->mm_seqnum = bgp_attr_mac_mobility_seqnum(attr, &sticky);
1877 attr->sticky = sticky;
1878
1879 return BGP_ATTR_PARSE_PROCEED;
1880 }
1881
1882 /* Parse Tunnel Encap attribute in an UPDATE */
1883 static int bgp_attr_encap(uint8_t type, struct peer *peer, /* IN */
1884 bgp_size_t length, /* IN: attr's length field */
1885 struct attr *attr, /* IN: caller already allocated */
1886 u_char flag, /* IN: attr's flags field */
1887 u_char *startp)
1888 {
1889 bgp_size_t total;
1890 uint16_t tunneltype = 0;
1891
1892 total = length + (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
1893
1894 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS)
1895 || !CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL)) {
1896 zlog_info(
1897 "Tunnel Encap attribute flag isn't optional and transitive %d",
1898 flag);
1899 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
1900 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
1901 startp, total);
1902 return -1;
1903 }
1904
1905 if (BGP_ATTR_ENCAP == type) {
1906 /* read outer TLV type and length */
1907 uint16_t tlv_length;
1908
1909 if (length < 4) {
1910 zlog_info(
1911 "Tunnel Encap attribute not long enough to contain outer T,L");
1912 bgp_notify_send_with_data(
1913 peer, BGP_NOTIFY_UPDATE_ERR,
1914 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, startp, total);
1915 return -1;
1916 }
1917 tunneltype = stream_getw(BGP_INPUT(peer));
1918 tlv_length = stream_getw(BGP_INPUT(peer));
1919 length -= 4;
1920
1921 if (tlv_length != length) {
1922 zlog_info("%s: tlv_length(%d) != length(%d)", __func__,
1923 tlv_length, length);
1924 }
1925 }
1926
1927 while (length >= 4) {
1928 uint16_t subtype = 0;
1929 uint16_t sublength = 0;
1930 struct bgp_attr_encap_subtlv *tlv;
1931
1932 if (BGP_ATTR_ENCAP == type) {
1933 subtype = stream_getc(BGP_INPUT(peer));
1934 sublength = stream_getc(BGP_INPUT(peer));
1935 length -= 2;
1936 #if ENABLE_BGP_VNC
1937 } else {
1938 subtype = stream_getw(BGP_INPUT(peer));
1939 sublength = stream_getw(BGP_INPUT(peer));
1940 length -= 4;
1941 #endif
1942 }
1943
1944 if (sublength > length) {
1945 zlog_info(
1946 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1947 sublength, length);
1948 bgp_notify_send_with_data(
1949 peer, BGP_NOTIFY_UPDATE_ERR,
1950 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, startp, total);
1951 return -1;
1952 }
1953
1954 /* alloc and copy sub-tlv */
1955 /* TBD make sure these are freed when attributes are released */
1956 tlv = XCALLOC(MTYPE_ENCAP_TLV,
1957 sizeof(struct bgp_attr_encap_subtlv)
1958 + sublength);
1959 tlv->type = subtype;
1960 tlv->length = sublength;
1961 stream_get(tlv->value, peer->curr, sublength);
1962 length -= sublength;
1963
1964 /* attach tlv to encap chain */
1965 if (BGP_ATTR_ENCAP == type) {
1966 struct bgp_attr_encap_subtlv *stlv_last;
1967 for (stlv_last = attr->encap_subtlvs;
1968 stlv_last && stlv_last->next;
1969 stlv_last = stlv_last->next)
1970 ;
1971 if (stlv_last) {
1972 stlv_last->next = tlv;
1973 } else {
1974 attr->encap_subtlvs = tlv;
1975 }
1976 #if ENABLE_BGP_VNC
1977 } else {
1978 struct bgp_attr_encap_subtlv *stlv_last;
1979 for (stlv_last = attr->vnc_subtlvs;
1980 stlv_last && stlv_last->next;
1981 stlv_last = stlv_last->next)
1982 ;
1983 if (stlv_last) {
1984 stlv_last->next = tlv;
1985 } else {
1986 attr->vnc_subtlvs = tlv;
1987 }
1988 #endif
1989 }
1990 }
1991
1992 if (BGP_ATTR_ENCAP == type) {
1993 attr->encap_tunneltype = tunneltype;
1994 }
1995
1996 if (length) {
1997 /* spurious leftover data */
1998 zlog_info(
1999 "Tunnel Encap attribute length is bad: %d leftover octets",
2000 length);
2001 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
2002 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
2003 startp, total);
2004 return -1;
2005 }
2006
2007 return 0;
2008 }
2009
2010 /* Prefix SID attribute
2011 * draft-ietf-idr-bgp-prefix-sid-05
2012 */
2013 static bgp_attr_parse_ret_t
2014 bgp_attr_prefix_sid(struct bgp_attr_parser_args *args,
2015 struct bgp_nlri *mp_update)
2016 {
2017 struct peer *const peer = args->peer;
2018 struct attr *const attr = args->attr;
2019 int type;
2020 int length;
2021 u_int32_t label_index;
2022 struct in6_addr ipv6_sid;
2023 u_int32_t srgb_base;
2024 u_int32_t srgb_range;
2025 int srgb_count;
2026
2027 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
2028
2029 type = stream_getc(peer->curr);
2030 length = stream_getw(peer->curr);
2031
2032 if (type == BGP_PREFIX_SID_LABEL_INDEX) {
2033 if (length != BGP_PREFIX_SID_LABEL_INDEX_LENGTH) {
2034 zlog_err(
2035 "Prefix SID label index length is %d instead of %d",
2036 length, BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
2037 return bgp_attr_malformed(
2038 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2039 args->total);
2040 }
2041
2042 /* Ignore flags and reserved */
2043 stream_getc(peer->curr);
2044 stream_getw(peer->curr);
2045
2046 /* Fetch the label index and see if it is valid. */
2047 label_index = stream_getl(peer->curr);
2048 if (label_index == BGP_INVALID_LABEL_INDEX)
2049 return bgp_attr_malformed(
2050 args, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
2051 args->total);
2052
2053 /* Store label index; subsequently, we'll check on
2054 * address-family */
2055 attr->label_index = label_index;
2056
2057 /*
2058 * Ignore the Label index attribute unless received for
2059 * labeled-unicast
2060 * SAFI.
2061 */
2062 if (!mp_update->length
2063 || mp_update->safi != SAFI_LABELED_UNICAST)
2064 attr->label_index = BGP_INVALID_LABEL_INDEX;
2065 }
2066
2067 /* Placeholder code for the IPv6 SID type */
2068 else if (type == BGP_PREFIX_SID_IPV6) {
2069 if (length != BGP_PREFIX_SID_IPV6_LENGTH) {
2070 zlog_err("Prefix SID IPv6 length is %d instead of %d",
2071 length, BGP_PREFIX_SID_IPV6_LENGTH);
2072 return bgp_attr_malformed(
2073 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2074 args->total);
2075 }
2076
2077 /* Ignore reserved */
2078 stream_getc(peer->curr);
2079 stream_getw(peer->curr);
2080
2081 stream_get(&ipv6_sid, peer->curr, 16);
2082 }
2083
2084 /* Placeholder code for the Originator SRGB type */
2085 else if (type == BGP_PREFIX_SID_ORIGINATOR_SRGB) {
2086 /* Ignore flags */
2087 stream_getw(peer->curr);
2088
2089 length -= 2;
2090
2091 if (length % BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH) {
2092 zlog_err(
2093 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2094 length, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH);
2095 return bgp_attr_malformed(
2096 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2097 args->total);
2098 }
2099
2100 srgb_count = length / BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH;
2101
2102 for (int i = 0; i < srgb_count; i++) {
2103 stream_get(&srgb_base, peer->curr, 3);
2104 stream_get(&srgb_range, peer->curr, 3);
2105 }
2106 }
2107
2108 return BGP_ATTR_PARSE_PROCEED;
2109 }
2110
2111 /* BGP unknown attribute treatment. */
2112 static bgp_attr_parse_ret_t bgp_attr_unknown(struct bgp_attr_parser_args *args)
2113 {
2114 bgp_size_t total = args->total;
2115 struct transit *transit;
2116 struct peer *const peer = args->peer;
2117 struct attr *const attr = args->attr;
2118 u_char *const startp = args->startp;
2119 const u_char type = args->type;
2120 const u_char flag = args->flags;
2121 const bgp_size_t length = args->length;
2122
2123 if (bgp_debug_update(peer, NULL, NULL, 1))
2124 zlog_debug(
2125 "%s Unknown attribute is received (type %d, length %d)",
2126 peer->host, type, length);
2127
2128 /* Forward read pointer of input stream. */
2129 stream_forward_getp(peer->curr, length);
2130
2131 /* If any of the mandatory well-known attributes are not recognized,
2132 then the Error Subcode is set to Unrecognized Well-known
2133 Attribute. The Data field contains the unrecognized attribute
2134 (type, length and value). */
2135 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL)) {
2136 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_UNREC_ATTR,
2137 args->total);
2138 }
2139
2140 /* Unrecognized non-transitive optional attributes must be quietly
2141 ignored and not passed along to other BGP peers. */
2142 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS))
2143 return BGP_ATTR_PARSE_PROCEED;
2144
2145 /* If a path with recognized transitive optional attribute is
2146 accepted and passed along to other BGP peers and the Partial bit
2147 in the Attribute Flags octet is set to 1 by some previous AS, it
2148 is not set back to 0 by the current AS. */
2149 SET_FLAG(*startp, BGP_ATTR_FLAG_PARTIAL);
2150
2151 /* Store transitive attribute to the end of attr->transit. */
2152 if (!attr->transit)
2153 attr->transit = XCALLOC(MTYPE_TRANSIT, sizeof(struct transit));
2154
2155 transit = attr->transit;
2156
2157 if (transit->val)
2158 transit->val = XREALLOC(MTYPE_TRANSIT_VAL, transit->val,
2159 transit->length + total);
2160 else
2161 transit->val = XMALLOC(MTYPE_TRANSIT_VAL, total);
2162
2163 memcpy(transit->val + transit->length, startp, total);
2164 transit->length += total;
2165
2166 return BGP_ATTR_PARSE_PROCEED;
2167 }
2168
2169 /* Well-known attribute check. */
2170 static int bgp_attr_check(struct peer *peer, struct attr *attr)
2171 {
2172 u_char type = 0;
2173
2174 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2175 * empty UPDATE. */
2176 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV) && !attr->flag)
2177 return BGP_ATTR_PARSE_PROCEED;
2178
2179 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2180 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2181 are present, it should. Check for any other attribute being present
2182 instead.
2183 */
2184 if (attr->flag == ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI))
2185 return BGP_ATTR_PARSE_PROCEED;
2186
2187 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN)))
2188 type = BGP_ATTR_ORIGIN;
2189
2190 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
2191 type = BGP_ATTR_AS_PATH;
2192
2193 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2194 * and
2195 * NLRI is empty. We can't easily check NLRI empty here though.
2196 */
2197 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP))
2198 && !CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)))
2199 type = BGP_ATTR_NEXT_HOP;
2200
2201 if (peer->sort == BGP_PEER_IBGP
2202 && !CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))
2203 type = BGP_ATTR_LOCAL_PREF;
2204
2205 if (type) {
2206 zlog_warn("%s Missing well-known attribute %s.", peer->host,
2207 lookup_msg(attr_str, type, NULL));
2208 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
2209 BGP_NOTIFY_UPDATE_MISS_ATTR, &type,
2210 1);
2211 return BGP_ATTR_PARSE_ERROR;
2212 }
2213 return BGP_ATTR_PARSE_PROCEED;
2214 }
2215
2216 /* Read attribute of update packet. This function is called from
2217 bgp_update_receive() in bgp_packet.c. */
2218 bgp_attr_parse_ret_t bgp_attr_parse(struct peer *peer, struct attr *attr,
2219 bgp_size_t size, struct bgp_nlri *mp_update,
2220 struct bgp_nlri *mp_withdraw)
2221 {
2222 int ret;
2223 u_char flag = 0;
2224 u_char type = 0;
2225 bgp_size_t length;
2226 u_char *startp, *endp;
2227 u_char *attr_endp;
2228 u_char seen[BGP_ATTR_BITMAP_SIZE];
2229 /* we need the as4_path only until we have synthesized the as_path with
2230 * it */
2231 /* same goes for as4_aggregator */
2232 struct aspath *as4_path = NULL;
2233 as_t as4_aggregator = 0;
2234 struct in_addr as4_aggregator_addr = {.s_addr = 0};
2235
2236 /* Initialize bitmap. */
2237 memset(seen, 0, BGP_ATTR_BITMAP_SIZE);
2238
2239 /* End pointer of BGP attribute. */
2240 endp = BGP_INPUT_PNT(peer) + size;
2241
2242 /* Get attributes to the end of attribute length. */
2243 while (BGP_INPUT_PNT(peer) < endp) {
2244 /* Check remaining length check.*/
2245 if (endp - BGP_INPUT_PNT(peer) < BGP_ATTR_MIN_LEN) {
2246 /* XXX warning: long int format, int arg (arg 5) */
2247 zlog_warn(
2248 "%s: error BGP attribute length %lu is smaller than min len",
2249 peer->host,
2250 (unsigned long)(endp
2251 - stream_pnt(BGP_INPUT(peer))));
2252
2253 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2254 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2255 return BGP_ATTR_PARSE_ERROR;
2256 }
2257
2258 /* Fetch attribute flag and type. */
2259 startp = BGP_INPUT_PNT(peer);
2260 /* "The lower-order four bits of the Attribute Flags octet are
2261 unused. They MUST be zero when sent and MUST be ignored when
2262 received." */
2263 flag = 0xF0 & stream_getc(BGP_INPUT(peer));
2264 type = stream_getc(BGP_INPUT(peer));
2265
2266 /* Check whether Extended-Length applies and is in bounds */
2267 if (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN)
2268 && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1))) {
2269 zlog_warn(
2270 "%s: Extended length set, but just %lu bytes of attr header",
2271 peer->host,
2272 (unsigned long)(endp
2273 - stream_pnt(BGP_INPUT(peer))));
2274
2275 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2276 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2277 return BGP_ATTR_PARSE_ERROR;
2278 }
2279
2280 /* Check extended attribue length bit. */
2281 if (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN))
2282 length = stream_getw(BGP_INPUT(peer));
2283 else
2284 length = stream_getc(BGP_INPUT(peer));
2285
2286 /* If any attribute appears more than once in the UPDATE
2287 message, then the Error Subcode is set to Malformed Attribute
2288 List. */
2289
2290 if (CHECK_BITMAP(seen, type)) {
2291 zlog_warn(
2292 "%s: error BGP attribute type %d appears twice in a message",
2293 peer->host, type);
2294
2295 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2296 BGP_NOTIFY_UPDATE_MAL_ATTR);
2297 return BGP_ATTR_PARSE_ERROR;
2298 }
2299
2300 /* Set type to bitmap to check duplicate attribute. `type' is
2301 unsigned char so it never overflow bitmap range. */
2302
2303 SET_BITMAP(seen, type);
2304
2305 /* Overflow check. */
2306 attr_endp = BGP_INPUT_PNT(peer) + length;
2307
2308 if (attr_endp > endp) {
2309 zlog_warn(
2310 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2311 peer->host, type, length, size, attr_endp,
2312 endp);
2313 /*
2314 * RFC 4271 6.3
2315 * If any recognized attribute has an Attribute
2316 * Length that conflicts with the expected length
2317 * (based on the attribute type code), then the
2318 * Error Subcode MUST be set to Attribute Length
2319 * Error. The Data field MUST contain the erroneous
2320 * attribute (type, length, and value).
2321 * ----------
2322 * We do not currently have a good way to determine the
2323 * length of the attribute independent of the length
2324 * received in the message. Instead we send the
2325 * minimum between the amount of data we have and the
2326 * amount specified by the attribute length field.
2327 *
2328 * Instead of directly passing in the packet buffer and
2329 * offset we use the stream_get* functions to read into
2330 * a stack buffer, since they perform bounds checking
2331 * and we are working with untrusted data.
2332 */
2333 unsigned char ndata[BGP_MAX_PACKET_SIZE];
2334 memset(ndata, 0x00, sizeof(ndata));
2335 size_t lfl =
2336 CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN) ? 2 : 1;
2337 /* Rewind to end of flag field */
2338 stream_forward_getp(BGP_INPUT(peer), -(1 + lfl));
2339 /* Type */
2340 stream_get(&ndata[0], BGP_INPUT(peer), 1);
2341 /* Length */
2342 stream_get(&ndata[1], BGP_INPUT(peer), lfl);
2343 /* Value */
2344 size_t atl = attr_endp - startp;
2345 size_t ndl = MIN(atl, STREAM_READABLE(BGP_INPUT(peer)));
2346 stream_get(&ndata[lfl + 1], BGP_INPUT(peer), ndl);
2347
2348 bgp_notify_send_with_data(
2349 peer, BGP_NOTIFY_UPDATE_ERR,
2350 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, ndata,
2351 ndl + lfl + 1);
2352
2353 return BGP_ATTR_PARSE_ERROR;
2354 }
2355
2356 struct bgp_attr_parser_args attr_args = {
2357 .peer = peer,
2358 .length = length,
2359 .attr = attr,
2360 .type = type,
2361 .flags = flag,
2362 .startp = startp,
2363 .total = attr_endp - startp,
2364 };
2365
2366
2367 /* If any recognized attribute has Attribute Flags that conflict
2368 with the Attribute Type Code, then the Error Subcode is set
2369 to
2370 Attribute Flags Error. The Data field contains the erroneous
2371 attribute (type, length and value). */
2372 if (bgp_attr_flag_invalid(&attr_args)) {
2373 bgp_attr_parse_ret_t ret;
2374 ret = bgp_attr_malformed(
2375 &attr_args, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
2376 attr_args.total);
2377 if (ret == BGP_ATTR_PARSE_PROCEED)
2378 continue;
2379 return ret;
2380 }
2381
2382 /* OK check attribute and store it's value. */
2383 switch (type) {
2384 case BGP_ATTR_ORIGIN:
2385 ret = bgp_attr_origin(&attr_args);
2386 break;
2387 case BGP_ATTR_AS_PATH:
2388 ret = bgp_attr_aspath(&attr_args);
2389 break;
2390 case BGP_ATTR_AS4_PATH:
2391 ret = bgp_attr_as4_path(&attr_args, &as4_path);
2392 break;
2393 case BGP_ATTR_NEXT_HOP:
2394 ret = bgp_attr_nexthop(&attr_args);
2395 break;
2396 case BGP_ATTR_MULTI_EXIT_DISC:
2397 ret = bgp_attr_med(&attr_args);
2398 break;
2399 case BGP_ATTR_LOCAL_PREF:
2400 ret = bgp_attr_local_pref(&attr_args);
2401 break;
2402 case BGP_ATTR_ATOMIC_AGGREGATE:
2403 ret = bgp_attr_atomic(&attr_args);
2404 break;
2405 case BGP_ATTR_AGGREGATOR:
2406 ret = bgp_attr_aggregator(&attr_args);
2407 break;
2408 case BGP_ATTR_AS4_AGGREGATOR:
2409 ret = bgp_attr_as4_aggregator(&attr_args,
2410 &as4_aggregator,
2411 &as4_aggregator_addr);
2412 break;
2413 case BGP_ATTR_COMMUNITIES:
2414 ret = bgp_attr_community(&attr_args);
2415 break;
2416 case BGP_ATTR_LARGE_COMMUNITIES:
2417 ret = bgp_attr_large_community(&attr_args);
2418 break;
2419 case BGP_ATTR_ORIGINATOR_ID:
2420 ret = bgp_attr_originator_id(&attr_args);
2421 break;
2422 case BGP_ATTR_CLUSTER_LIST:
2423 ret = bgp_attr_cluster_list(&attr_args);
2424 break;
2425 case BGP_ATTR_MP_REACH_NLRI:
2426 ret = bgp_mp_reach_parse(&attr_args, mp_update);
2427 break;
2428 case BGP_ATTR_MP_UNREACH_NLRI:
2429 ret = bgp_mp_unreach_parse(&attr_args, mp_withdraw);
2430 break;
2431 case BGP_ATTR_EXT_COMMUNITIES:
2432 ret = bgp_attr_ext_communities(&attr_args);
2433 break;
2434 #if ENABLE_BGP_VNC
2435 case BGP_ATTR_VNC:
2436 #endif
2437 case BGP_ATTR_ENCAP:
2438 ret = bgp_attr_encap(type, peer, length, attr, flag,
2439 startp);
2440 break;
2441 case BGP_ATTR_PREFIX_SID:
2442 ret = bgp_attr_prefix_sid(&attr_args, mp_update);
2443 break;
2444 default:
2445 ret = bgp_attr_unknown(&attr_args);
2446 break;
2447 }
2448
2449 if (ret == BGP_ATTR_PARSE_ERROR_NOTIFYPLS) {
2450 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2451 BGP_NOTIFY_UPDATE_MAL_ATTR);
2452 ret = BGP_ATTR_PARSE_ERROR;
2453 }
2454
2455 if (ret == BGP_ATTR_PARSE_EOR) {
2456 if (as4_path)
2457 aspath_unintern(&as4_path);
2458 return ret;
2459 }
2460
2461 /* If hard error occured immediately return to the caller. */
2462 if (ret == BGP_ATTR_PARSE_ERROR) {
2463 zlog_warn("%s: Attribute %s, parse error", peer->host,
2464 lookup_msg(attr_str, type, NULL));
2465 if (as4_path)
2466 aspath_unintern(&as4_path);
2467 return ret;
2468 }
2469 if (ret == BGP_ATTR_PARSE_WITHDRAW) {
2470
2471 zlog_warn(
2472 "%s: Attribute %s, parse error - treating as withdrawal",
2473 peer->host, lookup_msg(attr_str, type, NULL));
2474 if (as4_path)
2475 aspath_unintern(&as4_path);
2476 return ret;
2477 }
2478
2479 /* Check the fetched length. */
2480 if (BGP_INPUT_PNT(peer) != attr_endp) {
2481 zlog_warn("%s: BGP attribute %s, fetch error",
2482 peer->host, lookup_msg(attr_str, type, NULL));
2483 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2484 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2485 if (as4_path)
2486 aspath_unintern(&as4_path);
2487 return BGP_ATTR_PARSE_ERROR;
2488 }
2489 }
2490
2491 /* Check final read pointer is same as end pointer. */
2492 if (BGP_INPUT_PNT(peer) != endp) {
2493 zlog_warn("%s: BGP attribute %s, length mismatch", peer->host,
2494 lookup_msg(attr_str, type, NULL));
2495 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2496 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2497 if (as4_path)
2498 aspath_unintern(&as4_path);
2499 return BGP_ATTR_PARSE_ERROR;
2500 }
2501
2502 /* Check all mandatory well-known attributes are present */
2503 {
2504 bgp_attr_parse_ret_t ret;
2505 if ((ret = bgp_attr_check(peer, attr)) < 0) {
2506 if (as4_path)
2507 aspath_unintern(&as4_path);
2508 return ret;
2509 }
2510 }
2511
2512 /*
2513 * At this place we can see whether we got AS4_PATH and/or
2514 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2515 * We can not do this before we've read all attributes because
2516 * the as4 handling does not say whether AS4_PATH has to be sent
2517 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2518 * in relationship to AGGREGATOR.
2519 * So, to be defensive, we are not relying on any order and read
2520 * all attributes first, including these 32bit ones, and now,
2521 * afterwards, we look what and if something is to be done for as4.
2522 *
2523 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2524 * MP_UNREACH_NLRI.
2525 */
2526 /* actually... this doesn't ever return failure currently, but
2527 * better safe than sorry */
2528 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH))
2529 && bgp_attr_munge_as4_attrs(peer, attr, as4_path, as4_aggregator,
2530 &as4_aggregator_addr)) {
2531 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2532 BGP_NOTIFY_UPDATE_MAL_ATTR);
2533 if (as4_path)
2534 aspath_unintern(&as4_path);
2535 return BGP_ATTR_PARSE_ERROR;
2536 }
2537
2538 /* At this stage, we have done all fiddling with as4, and the
2539 * resulting info is in attr->aggregator resp. attr->aspath
2540 * so we can chuck as4_aggregator and as4_path alltogether in
2541 * order to save memory
2542 */
2543 if (as4_path) {
2544 aspath_unintern(&as4_path); /* unintern - it is in the hash */
2545 /* The flag that we got this is still there, but that does not
2546 * do any trouble
2547 */
2548 }
2549 /*
2550 * The "rest" of the code does nothing with as4_aggregator.
2551 * there is no memory attached specifically which is not part
2552 * of the attr.
2553 * so ignoring just means do nothing.
2554 */
2555 /*
2556 * Finally do the checks on the aspath we did not do yet
2557 * because we waited for a potentially synthesized aspath.
2558 */
2559 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH))) {
2560 ret = bgp_attr_aspath_check(peer, attr);
2561 if (ret != BGP_ATTR_PARSE_PROCEED)
2562 return ret;
2563 }
2564 /* Finally intern unknown attribute. */
2565 if (attr->transit)
2566 attr->transit = transit_intern(attr->transit);
2567 if (attr->encap_subtlvs)
2568 attr->encap_subtlvs =
2569 encap_intern(attr->encap_subtlvs, ENCAP_SUBTLV_TYPE);
2570 #if ENABLE_BGP_VNC
2571 if (attr->vnc_subtlvs)
2572 attr->vnc_subtlvs =
2573 encap_intern(attr->vnc_subtlvs, VNC_SUBTLV_TYPE);
2574 #endif
2575
2576 return BGP_ATTR_PARSE_PROCEED;
2577 }
2578
2579 size_t bgp_packet_mpattr_start(struct stream *s, struct peer *peer, afi_t afi,
2580 safi_t safi, struct bpacket_attr_vec_arr *vecarr,
2581 struct attr *attr)
2582 {
2583 size_t sizep;
2584 iana_afi_t pkt_afi;
2585 iana_safi_t pkt_safi;
2586 afi_t nh_afi;
2587
2588 /* Set extended bit always to encode the attribute length as 2 bytes */
2589 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_EXTLEN);
2590 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
2591 sizep = stream_get_endp(s);
2592 stream_putw(s, 0); /* Marker: Attribute length. */
2593
2594
2595 /* Convert AFI, SAFI to values for packet. */
2596 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
2597
2598 stream_putw(s, pkt_afi); /* AFI */
2599 stream_putc(s, pkt_safi); /* SAFI */
2600
2601 /* Nexthop AFI */
2602 if (afi == AFI_IP
2603 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
2604 nh_afi = peer_cap_enhe(peer, afi, safi) ? AFI_IP6 : AFI_IP;
2605 else
2606 nh_afi = BGP_NEXTHOP_AFI_FROM_NHLEN(attr->mp_nexthop_len);
2607
2608 /* Nexthop */
2609 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s, attr);
2610 switch (nh_afi) {
2611 case AFI_IP:
2612 switch (safi) {
2613 case SAFI_UNICAST:
2614 case SAFI_MULTICAST:
2615 case SAFI_LABELED_UNICAST:
2616 stream_putc(s, 4);
2617 stream_put_ipv4(s, attr->nexthop.s_addr);
2618 break;
2619 case SAFI_MPLS_VPN:
2620 stream_putc(s, 12);
2621 stream_putl(s, 0); /* RD = 0, per RFC */
2622 stream_putl(s, 0);
2623 stream_put(s, &attr->mp_nexthop_global_in, 4);
2624 break;
2625 case SAFI_ENCAP:
2626 case SAFI_EVPN:
2627 stream_putc(s, 4);
2628 stream_put(s, &attr->mp_nexthop_global_in, 4);
2629 break;
2630 default:
2631 break;
2632 }
2633 break;
2634 case AFI_IP6:
2635 switch (safi) {
2636 case SAFI_UNICAST:
2637 case SAFI_MULTICAST:
2638 case SAFI_LABELED_UNICAST:
2639 case SAFI_EVPN: {
2640 if (attr->mp_nexthop_len
2641 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
2642 stream_putc(s,
2643 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL);
2644 stream_put(s, &attr->mp_nexthop_global,
2645 IPV6_MAX_BYTELEN);
2646 stream_put(s, &attr->mp_nexthop_local,
2647 IPV6_MAX_BYTELEN);
2648 } else {
2649 stream_putc(s, IPV6_MAX_BYTELEN);
2650 stream_put(s, &attr->mp_nexthop_global,
2651 IPV6_MAX_BYTELEN);
2652 }
2653 } break;
2654 case SAFI_MPLS_VPN: {
2655 if (attr->mp_nexthop_len
2656 == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
2657 stream_putc(s, 24);
2658 stream_putl(s, 0); /* RD = 0, per RFC */
2659 stream_putl(s, 0);
2660 stream_put(s, &attr->mp_nexthop_global,
2661 IPV6_MAX_BYTELEN);
2662 } else if (attr->mp_nexthop_len
2663 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
2664 stream_putc(s, 48);
2665 stream_putl(s, 0); /* RD = 0, per RFC */
2666 stream_putl(s, 0);
2667 stream_put(s, &attr->mp_nexthop_global,
2668 IPV6_MAX_BYTELEN);
2669 stream_putl(s, 0); /* RD = 0, per RFC */
2670 stream_putl(s, 0);
2671 stream_put(s, &attr->mp_nexthop_local,
2672 IPV6_MAX_BYTELEN);
2673 }
2674 } break;
2675 case SAFI_ENCAP:
2676 stream_putc(s, IPV6_MAX_BYTELEN);
2677 stream_put(s, &attr->mp_nexthop_global,
2678 IPV6_MAX_BYTELEN);
2679 break;
2680 default:
2681 break;
2682 }
2683 break;
2684 default:
2685 zlog_err(
2686 "Bad nexthop when sening to %s, AFI %u SAFI %u nhlen %d",
2687 peer->host, afi, safi, attr->mp_nexthop_len);
2688 break;
2689 }
2690
2691 /* SNPA */
2692 stream_putc(s, 0);
2693 return sizep;
2694 }
2695
2696 void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
2697 struct prefix *p, struct prefix_rd *prd,
2698 mpls_label_t *label, int addpath_encode,
2699 u_int32_t addpath_tx_id, struct attr *attr)
2700 {
2701 if (safi == SAFI_MPLS_VPN) {
2702 if (addpath_encode)
2703 stream_putl(s, addpath_tx_id);
2704 /* Label, RD, Prefix write. */
2705 stream_putc(s, p->prefixlen + 88);
2706 stream_put(s, label, BGP_LABEL_BYTES);
2707 stream_put(s, prd->val, 8);
2708 stream_put(s, &p->u.prefix, PSIZE(p->prefixlen));
2709 } else if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
2710 /* EVPN prefix - contents depend on type */
2711 bgp_evpn_encode_prefix(s, p, prd, label, attr, addpath_encode,
2712 addpath_tx_id);
2713 } else if (safi == SAFI_LABELED_UNICAST) {
2714 /* Prefix write with label. */
2715 stream_put_labeled_prefix(s, p, label);
2716 } else
2717 stream_put_prefix_addpath(s, p, addpath_encode, addpath_tx_id);
2718 }
2719
2720 size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi, struct prefix *p)
2721 {
2722 int size = PSIZE(p->prefixlen);
2723 if (safi == SAFI_MPLS_VPN)
2724 size += 88;
2725 else if (afi == AFI_L2VPN && safi == SAFI_EVPN)
2726 size += 232; // TODO: Maximum possible for type-2, type-3 and
2727 // type-5
2728 return size;
2729 }
2730
2731 /*
2732 * Encodes the tunnel encapsulation attribute,
2733 * and with ENABLE_BGP_VNC the VNC attribute which uses
2734 * almost the same TLV format
2735 */
2736 static void bgp_packet_mpattr_tea(struct bgp *bgp, struct peer *peer,
2737 struct stream *s, struct attr *attr,
2738 uint8_t attrtype)
2739 {
2740 unsigned int attrlenfield = 0;
2741 unsigned int attrhdrlen = 0;
2742 struct bgp_attr_encap_subtlv *subtlvs;
2743 struct bgp_attr_encap_subtlv *st;
2744 const char *attrname;
2745
2746 if (!attr || (attrtype == BGP_ATTR_ENCAP
2747 && (!attr->encap_tunneltype
2748 || attr->encap_tunneltype == BGP_ENCAP_TYPE_MPLS)))
2749 return;
2750
2751 switch (attrtype) {
2752 case BGP_ATTR_ENCAP:
2753 attrname = "Tunnel Encap";
2754 subtlvs = attr->encap_subtlvs;
2755 if (subtlvs == NULL) /* nothing to do */
2756 return;
2757 /*
2758 * The tunnel encap attr has an "outer" tlv.
2759 * T = tunneltype,
2760 * L = total length of subtlvs,
2761 * V = concatenated subtlvs.
2762 */
2763 attrlenfield = 2 + 2; /* T + L */
2764 attrhdrlen = 1 + 1; /* subTLV T + L */
2765 break;
2766
2767 #if ENABLE_BGP_VNC
2768 case BGP_ATTR_VNC:
2769 attrname = "VNC";
2770 subtlvs = attr->vnc_subtlvs;
2771 if (subtlvs == NULL) /* nothing to do */
2772 return;
2773 attrlenfield = 0; /* no outer T + L */
2774 attrhdrlen = 2 + 2; /* subTLV T + L */
2775 break;
2776 #endif
2777
2778 default:
2779 assert(0);
2780 }
2781
2782 /* compute attr length */
2783 for (st = subtlvs; st; st = st->next) {
2784 attrlenfield += (attrhdrlen + st->length);
2785 }
2786
2787 if (attrlenfield > 0xffff) {
2788 zlog_info("%s attribute is too long (length=%d), can't send it",
2789 attrname, attrlenfield);
2790 return;
2791 }
2792
2793 if (attrlenfield > 0xff) {
2794 /* 2-octet length field */
2795 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL
2796 | BGP_ATTR_FLAG_EXTLEN);
2797 stream_putc(s, attrtype);
2798 stream_putw(s, attrlenfield & 0xffff);
2799 } else {
2800 /* 1-octet length field */
2801 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL);
2802 stream_putc(s, attrtype);
2803 stream_putc(s, attrlenfield & 0xff);
2804 }
2805
2806 if (attrtype == BGP_ATTR_ENCAP) {
2807 /* write outer T+L */
2808 stream_putw(s, attr->encap_tunneltype);
2809 stream_putw(s, attrlenfield - 4);
2810 }
2811
2812 /* write each sub-tlv */
2813 for (st = subtlvs; st; st = st->next) {
2814 if (attrtype == BGP_ATTR_ENCAP) {
2815 stream_putc(s, st->type);
2816 stream_putc(s, st->length);
2817 #if ENABLE_BGP_VNC
2818 } else {
2819 stream_putw(s, st->type);
2820 stream_putw(s, st->length);
2821 #endif
2822 }
2823 stream_put(s, st->value, st->length);
2824 }
2825 }
2826
2827 void bgp_packet_mpattr_end(struct stream *s, size_t sizep)
2828 {
2829 /* Set MP attribute length. Don't count the (2) bytes used to encode
2830 the attr length */
2831 stream_putw_at(s, sizep, (stream_get_endp(s) - sizep) - 2);
2832 }
2833
2834 /* Make attribute packet. */
2835 bgp_size_t bgp_packet_attribute(struct bgp *bgp, struct peer *peer,
2836 struct stream *s, struct attr *attr,
2837 struct bpacket_attr_vec_arr *vecarr,
2838 struct prefix *p, afi_t afi, safi_t safi,
2839 struct peer *from, struct prefix_rd *prd,
2840 mpls_label_t *label, int addpath_encode,
2841 u_int32_t addpath_tx_id)
2842 {
2843 size_t cp;
2844 size_t aspath_sizep;
2845 struct aspath *aspath;
2846 int send_as4_path = 0;
2847 int send_as4_aggregator = 0;
2848 int use32bit = (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0;
2849
2850 if (!bgp)
2851 bgp = peer->bgp;
2852
2853 /* Remember current pointer. */
2854 cp = stream_get_endp(s);
2855
2856 if (p
2857 && !((afi == AFI_IP && safi == SAFI_UNICAST)
2858 && !peer_cap_enhe(peer, afi, safi))) {
2859 size_t mpattrlen_pos = 0;
2860
2861 mpattrlen_pos = bgp_packet_mpattr_start(s, peer, afi, safi,
2862 vecarr, attr);
2863 bgp_packet_mpattr_prefix(s, afi, safi, p, prd, label,
2864 addpath_encode, addpath_tx_id, attr);
2865 bgp_packet_mpattr_end(s, mpattrlen_pos);
2866 }
2867
2868 /* Origin attribute. */
2869 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2870 stream_putc(s, BGP_ATTR_ORIGIN);
2871 stream_putc(s, 1);
2872 stream_putc(s, attr->origin);
2873
2874 /* AS path attribute. */
2875
2876 /* If remote-peer is EBGP */
2877 if (peer->sort == BGP_PEER_EBGP
2878 && (!CHECK_FLAG(peer->af_flags[afi][safi],
2879 PEER_FLAG_AS_PATH_UNCHANGED)
2880 || attr->aspath->segments == NULL)
2881 && (!CHECK_FLAG(peer->af_flags[afi][safi],
2882 PEER_FLAG_RSERVER_CLIENT))) {
2883 aspath = aspath_dup(attr->aspath);
2884
2885 /* Even though we may not be configured for confederations we
2886 * may have
2887 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2888 aspath = aspath_delete_confed_seq(aspath);
2889
2890 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
2891 /* Stuff our path CONFED_ID on the front */
2892 aspath = aspath_add_seq(aspath, bgp->confed_id);
2893 } else {
2894 if (peer->change_local_as) {
2895 /* If replace-as is specified, we only use the
2896 change_local_as when
2897 advertising routes. */
2898 if (!CHECK_FLAG(
2899 peer->flags,
2900 PEER_FLAG_LOCAL_AS_REPLACE_AS)) {
2901 aspath = aspath_add_seq(aspath,
2902 peer->local_as);
2903 }
2904 aspath = aspath_add_seq(aspath,
2905 peer->change_local_as);
2906 } else {
2907 aspath = aspath_add_seq(aspath, peer->local_as);
2908 }
2909 }
2910 } else if (peer->sort == BGP_PEER_CONFED) {
2911 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2912 * thing */
2913 aspath = aspath_dup(attr->aspath);
2914 aspath = aspath_add_confed_seq(aspath, peer->local_as);
2915 } else
2916 aspath = attr->aspath;
2917
2918 /* If peer is not AS4 capable, then:
2919 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2920 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2921 * segment
2922 * types are in it (i.e. exclude them if they are there)
2923 * AND do this only if there is at least one asnum > 65535 in the
2924 * path!
2925 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2926 * change
2927 * all ASnums > 65535 to BGP_AS_TRANS
2928 */
2929
2930 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_EXTLEN);
2931 stream_putc(s, BGP_ATTR_AS_PATH);
2932 aspath_sizep = stream_get_endp(s);
2933 stream_putw(s, 0);
2934 stream_putw_at(s, aspath_sizep, aspath_put(s, aspath, use32bit));
2935
2936 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2937 * in the path
2938 */
2939 if (!use32bit && aspath_has_as4(aspath))
2940 send_as4_path =
2941 1; /* we'll do this later, at the correct place */
2942
2943 /* Nexthop attribute. */
2944 if (afi == AFI_IP && safi == SAFI_UNICAST
2945 && !peer_cap_enhe(peer, afi, safi)) {
2946 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
2947 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2948 stream_putc(s, BGP_ATTR_NEXT_HOP);
2949 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s,
2950 attr);
2951 stream_putc(s, 4);
2952 stream_put_ipv4(s, attr->nexthop.s_addr);
2953 } else if (peer_cap_enhe(from, afi, safi)) {
2954 /*
2955 * Likely this is the case when an IPv4 prefix was
2956 * received with
2957 * Extended Next-hop capability and now being advertised
2958 * to
2959 * non-ENHE peers.
2960 * Setting the mandatory (ipv4) next-hop attribute here
2961 * to enable
2962 * implicit next-hop self with correct (ipv4 address
2963 * family).
2964 */
2965 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2966 stream_putc(s, BGP_ATTR_NEXT_HOP);
2967 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s,
2968 NULL);
2969 stream_putc(s, 4);
2970 stream_put_ipv4(s, 0);
2971 }
2972 }
2973
2974 /* MED attribute. */
2975 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)
2976 || bgp->maxmed_active) {
2977 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
2978 stream_putc(s, BGP_ATTR_MULTI_EXIT_DISC);
2979 stream_putc(s, 4);
2980 stream_putl(s, (bgp->maxmed_active ? bgp->maxmed_value
2981 : attr->med));
2982 }
2983
2984 /* Local preference. */
2985 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED) {
2986 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2987 stream_putc(s, BGP_ATTR_LOCAL_PREF);
2988 stream_putc(s, 4);
2989 stream_putl(s, attr->local_pref);
2990 }
2991
2992 /* Atomic aggregate. */
2993 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
2994 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2995 stream_putc(s, BGP_ATTR_ATOMIC_AGGREGATE);
2996 stream_putc(s, 0);
2997 }
2998
2999 /* Aggregator. */
3000 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR)) {
3001 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3002 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3003 stream_putc(s, BGP_ATTR_AGGREGATOR);
3004
3005 if (use32bit) {
3006 /* AS4 capable peer */
3007 stream_putc(s, 8);
3008 stream_putl(s, attr->aggregator_as);
3009 } else {
3010 /* 2-byte AS peer */
3011 stream_putc(s, 6);
3012
3013 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3014 * used? */
3015 if (attr->aggregator_as > 65535) {
3016 stream_putw(s, BGP_AS_TRANS);
3017
3018 /* we have to send AS4_AGGREGATOR, too.
3019 * we'll do that later in order to send
3020 * attributes in ascending
3021 * order.
3022 */
3023 send_as4_aggregator = 1;
3024 } else
3025 stream_putw(s, (u_int16_t)attr->aggregator_as);
3026 }
3027 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3028 }
3029
3030 /* Community attribute. */
3031 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
3032 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))) {
3033 if (attr->community->size * 4 > 255) {
3034 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3035 | BGP_ATTR_FLAG_TRANS
3036 | BGP_ATTR_FLAG_EXTLEN);
3037 stream_putc(s, BGP_ATTR_COMMUNITIES);
3038 stream_putw(s, attr->community->size * 4);
3039 } else {
3040 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3041 | BGP_ATTR_FLAG_TRANS);
3042 stream_putc(s, BGP_ATTR_COMMUNITIES);
3043 stream_putc(s, attr->community->size * 4);
3044 }
3045 stream_put(s, attr->community->val, attr->community->size * 4);
3046 }
3047
3048 /*
3049 * Large Community attribute.
3050 */
3051 if (CHECK_FLAG(peer->af_flags[afi][safi],
3052 PEER_FLAG_SEND_LARGE_COMMUNITY)
3053 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))) {
3054 if (lcom_length(attr->lcommunity) > 255) {
3055 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3056 | BGP_ATTR_FLAG_TRANS
3057 | BGP_ATTR_FLAG_EXTLEN);
3058 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3059 stream_putw(s, lcom_length(attr->lcommunity));
3060 } else {
3061 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3062 | BGP_ATTR_FLAG_TRANS);
3063 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3064 stream_putc(s, lcom_length(attr->lcommunity));
3065 }
3066 stream_put(s, attr->lcommunity->val,
3067 lcom_length(attr->lcommunity));
3068 }
3069
3070 /* Route Reflector. */
3071 if (peer->sort == BGP_PEER_IBGP && from
3072 && from->sort == BGP_PEER_IBGP) {
3073 /* Originator ID. */
3074 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3075 stream_putc(s, BGP_ATTR_ORIGINATOR_ID);
3076 stream_putc(s, 4);
3077
3078 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
3079 stream_put_in_addr(s, &attr->originator_id);
3080 else
3081 stream_put_in_addr(s, &from->remote_id);
3082
3083 /* Cluster list. */
3084 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3085 stream_putc(s, BGP_ATTR_CLUSTER_LIST);
3086
3087 if (attr->cluster) {
3088 stream_putc(s, attr->cluster->length + 4);
3089 /* If this peer configuration's parent BGP has
3090 * cluster_id. */
3091 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
3092 stream_put_in_addr(s, &bgp->cluster_id);
3093 else
3094 stream_put_in_addr(s, &bgp->router_id);
3095 stream_put(s, attr->cluster->list,
3096 attr->cluster->length);
3097 } else {
3098 stream_putc(s, 4);
3099 /* If this peer configuration's parent BGP has
3100 * cluster_id. */
3101 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
3102 stream_put_in_addr(s, &bgp->cluster_id);
3103 else
3104 stream_put_in_addr(s, &bgp->router_id);
3105 }
3106 }
3107
3108 /* Extended Communities attribute. */
3109 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
3110 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) {
3111 if (peer->sort == BGP_PEER_IBGP
3112 || peer->sort == BGP_PEER_CONFED) {
3113 if (attr->ecommunity->size * 8 > 255) {
3114 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3115 | BGP_ATTR_FLAG_TRANS
3116 | BGP_ATTR_FLAG_EXTLEN);
3117 stream_putc(s, BGP_ATTR_EXT_COMMUNITIES);
3118 stream_putw(s, attr->ecommunity->size * 8);
3119 } else {
3120 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3121 | BGP_ATTR_FLAG_TRANS);
3122 stream_putc(s, BGP_ATTR_EXT_COMMUNITIES);
3123 stream_putc(s, attr->ecommunity->size * 8);
3124 }
3125 stream_put(s, attr->ecommunity->val,
3126 attr->ecommunity->size * 8);
3127 } else {
3128 u_int8_t *pnt;
3129 int tbit;
3130 int ecom_tr_size = 0;
3131 int i;
3132
3133 for (i = 0; i < attr->ecommunity->size; i++) {
3134 pnt = attr->ecommunity->val + (i * 8);
3135 tbit = *pnt;
3136
3137 if (CHECK_FLAG(tbit,
3138 ECOMMUNITY_FLAG_NON_TRANSITIVE))
3139 continue;
3140
3141 ecom_tr_size++;
3142 }
3143
3144 if (ecom_tr_size) {
3145 if (ecom_tr_size * 8 > 255) {
3146 stream_putc(
3147 s,
3148 BGP_ATTR_FLAG_OPTIONAL
3149 | BGP_ATTR_FLAG_TRANS
3150 | BGP_ATTR_FLAG_EXTLEN);
3151 stream_putc(s,
3152 BGP_ATTR_EXT_COMMUNITIES);
3153 stream_putw(s, ecom_tr_size * 8);
3154 } else {
3155 stream_putc(
3156 s,
3157 BGP_ATTR_FLAG_OPTIONAL
3158 | BGP_ATTR_FLAG_TRANS);
3159 stream_putc(s,
3160 BGP_ATTR_EXT_COMMUNITIES);
3161 stream_putc(s, ecom_tr_size * 8);
3162 }
3163
3164 for (i = 0; i < attr->ecommunity->size; i++) {
3165 pnt = attr->ecommunity->val + (i * 8);
3166 tbit = *pnt;
3167
3168 if (CHECK_FLAG(
3169 tbit,
3170 ECOMMUNITY_FLAG_NON_TRANSITIVE))
3171 continue;
3172
3173 stream_put(s, pnt, 8);
3174 }
3175 }
3176 }
3177 }
3178
3179 /* Label index attribute. */
3180 if (safi == SAFI_LABELED_UNICAST) {
3181 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID)) {
3182 u_int32_t label_index;
3183
3184 label_index = attr->label_index;
3185
3186 if (label_index != BGP_INVALID_LABEL_INDEX) {
3187 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3188 | BGP_ATTR_FLAG_TRANS);
3189 stream_putc(s, BGP_ATTR_PREFIX_SID);
3190 stream_putc(s, 10);
3191 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX);
3192 stream_putw(s,
3193 BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
3194 stream_putc(s, 0); // reserved
3195 stream_putw(s, 0); // flags
3196 stream_putl(s, label_index);
3197 }
3198 }
3199 }
3200
3201 if (send_as4_path) {
3202 /* If the peer is NOT As4 capable, AND */
3203 /* there are ASnums > 65535 in path THEN
3204 * give out AS4_PATH */
3205
3206 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3207 * path segments!
3208 * Hm, I wonder... confederation things *should* only be at
3209 * the beginning of an aspath, right? Then we should use
3210 * aspath_delete_confed_seq for this, because it is already
3211 * there! (JK)
3212 * Folks, talk to me: what is reasonable here!?
3213 */
3214 aspath = aspath_delete_confed_seq(aspath);
3215
3216 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL
3217 | BGP_ATTR_FLAG_EXTLEN);
3218 stream_putc(s, BGP_ATTR_AS4_PATH);
3219 aspath_sizep = stream_get_endp(s);
3220 stream_putw(s, 0);
3221 stream_putw_at(s, aspath_sizep, aspath_put(s, aspath, 1));
3222 }
3223
3224 if (aspath != attr->aspath)
3225 aspath_free(aspath);
3226
3227 if (send_as4_aggregator) {
3228 /* send AS4_AGGREGATOR, at this place */
3229 /* this section of code moved here in order to ensure the
3230 * correct
3231 * *ascending* order of attributes
3232 */
3233 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3234 stream_putc(s, BGP_ATTR_AS4_AGGREGATOR);
3235 stream_putc(s, 8);
3236 stream_putl(s, attr->aggregator_as);
3237 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3238 }
3239
3240 if (((afi == AFI_IP || afi == AFI_IP6)
3241 && (safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN))
3242 || (afi == AFI_L2VPN && safi == SAFI_EVPN)) {
3243 /* Tunnel Encap attribute */
3244 bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_ENCAP);
3245
3246 #if ENABLE_BGP_VNC
3247 /* VNC attribute */
3248 bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_VNC);
3249 #endif
3250 }
3251
3252 /* PMSI Tunnel */
3253 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)) {
3254 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3255 stream_putc(s, BGP_ATTR_PMSI_TUNNEL);
3256 stream_putc(s, 9); // Length
3257 stream_putc(s, 0); // Flags
3258 stream_putc(s, 6); // Tunnel type: Ingress Replication (6)
3259 stream_put(s, &(attr->label), BGP_LABEL_BYTES); // MPLS Label / VXLAN VNI
3260 stream_put_ipv4(s, attr->nexthop.s_addr); // Unicast tunnel endpoint IP address
3261 }
3262
3263 /* Unknown transit attribute. */
3264 if (attr->transit)
3265 stream_put(s, attr->transit->val, attr->transit->length);
3266
3267 /* Return total size of attribute. */
3268 return stream_get_endp(s) - cp;
3269 }
3270
3271 size_t bgp_packet_mpunreach_start(struct stream *s, afi_t afi, safi_t safi)
3272 {
3273 unsigned long attrlen_pnt;
3274 iana_afi_t pkt_afi;
3275 iana_safi_t pkt_safi;
3276
3277 /* Set extended bit always to encode the attribute length as 2 bytes */
3278 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_EXTLEN);
3279 stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
3280
3281 attrlen_pnt = stream_get_endp(s);
3282 stream_putw(s, 0); /* Length of this attribute. */
3283
3284 /* Convert AFI, SAFI to values for packet. */
3285 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
3286
3287 stream_putw(s, pkt_afi);
3288 stream_putc(s, pkt_safi);
3289
3290 return attrlen_pnt;
3291 }
3292
3293 void bgp_packet_mpunreach_prefix(struct stream *s, struct prefix *p, afi_t afi,
3294 safi_t safi, struct prefix_rd *prd,
3295 mpls_label_t *label, int addpath_encode,
3296 u_int32_t addpath_tx_id, struct attr *attr)
3297 {
3298 u_char wlabel[3] = {0x80, 0x00, 0x00};
3299
3300 if (safi == SAFI_LABELED_UNICAST)
3301 label = (mpls_label_t *)wlabel;
3302
3303 return bgp_packet_mpattr_prefix(s, afi, safi, p, prd, label,
3304 addpath_encode, addpath_tx_id, attr);
3305 }
3306
3307 void bgp_packet_mpunreach_end(struct stream *s, size_t attrlen_pnt)
3308 {
3309 bgp_packet_mpattr_end(s, attrlen_pnt);
3310 }
3311
3312 /* Initialization of attribute. */
3313 void bgp_attr_init(void)
3314 {
3315 aspath_init();
3316 attrhash_init();
3317 community_init();
3318 ecommunity_init();
3319 lcommunity_init();
3320 cluster_init();
3321 transit_init();
3322 encap_init();
3323 }
3324
3325 void bgp_attr_finish(void)
3326 {
3327 aspath_finish();
3328 attrhash_finish();
3329 community_finish();
3330 ecommunity_finish();
3331 lcommunity_finish();
3332 cluster_finish();
3333 transit_finish();
3334 encap_finish();
3335 }
3336
3337 /* Make attribute packet. */
3338 void bgp_dump_routes_attr(struct stream *s, struct attr *attr,
3339 struct prefix *prefix)
3340 {
3341 unsigned long cp;
3342 unsigned long len;
3343 size_t aspath_lenp;
3344 struct aspath *aspath;
3345 int addpath_encode = 0;
3346 u_int32_t addpath_tx_id = 0;
3347
3348 /* Remember current pointer. */
3349 cp = stream_get_endp(s);
3350
3351 /* Place holder of length. */
3352 stream_putw(s, 0);
3353
3354 /* Origin attribute. */
3355 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3356 stream_putc(s, BGP_ATTR_ORIGIN);
3357 stream_putc(s, 1);
3358 stream_putc(s, attr->origin);
3359
3360 aspath = attr->aspath;
3361
3362 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_EXTLEN);
3363 stream_putc(s, BGP_ATTR_AS_PATH);
3364 aspath_lenp = stream_get_endp(s);
3365 stream_putw(s, 0);
3366
3367 stream_putw_at(s, aspath_lenp, aspath_put(s, aspath, 1));
3368
3369 /* Nexthop attribute. */
3370 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3371 if (prefix != NULL && prefix->family != AF_INET6) {
3372 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3373 stream_putc(s, BGP_ATTR_NEXT_HOP);
3374 stream_putc(s, 4);
3375 stream_put_ipv4(s, attr->nexthop.s_addr);
3376 }
3377
3378 /* MED attribute. */
3379 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
3380 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3381 stream_putc(s, BGP_ATTR_MULTI_EXIT_DISC);
3382 stream_putc(s, 4);
3383 stream_putl(s, attr->med);
3384 }
3385
3386 /* Local preference. */
3387 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
3388 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3389 stream_putc(s, BGP_ATTR_LOCAL_PREF);
3390 stream_putc(s, 4);
3391 stream_putl(s, attr->local_pref);
3392 }
3393
3394 /* Atomic aggregate. */
3395 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
3396 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3397 stream_putc(s, BGP_ATTR_ATOMIC_AGGREGATE);
3398 stream_putc(s, 0);
3399 }
3400
3401 /* Aggregator. */
3402 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR)) {
3403 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3404 stream_putc(s, BGP_ATTR_AGGREGATOR);
3405 stream_putc(s, 8);
3406 stream_putl(s, attr->aggregator_as);
3407 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3408 }
3409
3410 /* Community attribute. */
3411 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
3412 if (attr->community->size * 4 > 255) {
3413 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3414 | BGP_ATTR_FLAG_TRANS
3415 | BGP_ATTR_FLAG_EXTLEN);
3416 stream_putc(s, BGP_ATTR_COMMUNITIES);
3417 stream_putw(s, attr->community->size * 4);
3418 } else {
3419 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3420 | BGP_ATTR_FLAG_TRANS);
3421 stream_putc(s, BGP_ATTR_COMMUNITIES);
3422 stream_putc(s, attr->community->size * 4);
3423 }
3424 stream_put(s, attr->community->val, attr->community->size * 4);
3425 }
3426
3427 /* Large Community attribute. */
3428 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) {
3429 if (lcom_length(attr->lcommunity) > 255) {
3430 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3431 | BGP_ATTR_FLAG_TRANS
3432 | BGP_ATTR_FLAG_EXTLEN);
3433 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3434 stream_putw(s, lcom_length(attr->lcommunity));
3435 } else {
3436 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3437 | BGP_ATTR_FLAG_TRANS);
3438 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3439 stream_putc(s, lcom_length(attr->lcommunity));
3440 }
3441
3442 stream_put(s, attr->lcommunity->val, lcom_length(attr->lcommunity));
3443 }
3444
3445 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3446 if (prefix != NULL && prefix->family == AF_INET6
3447 && (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
3448 || attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)) {
3449 int sizep;
3450
3451 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3452 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
3453 sizep = stream_get_endp(s);
3454
3455 /* MP header */
3456 stream_putc(s, 0); /* Marker: Attribute length. */
3457 stream_putw(s, AFI_IP6); /* AFI */
3458 stream_putc(s, SAFI_UNICAST); /* SAFI */
3459
3460 /* Next hop */
3461 stream_putc(s, attr->mp_nexthop_len);
3462 stream_put(s, &attr->mp_nexthop_global, IPV6_MAX_BYTELEN);
3463 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3464 stream_put(s, &attr->mp_nexthop_local,
3465 IPV6_MAX_BYTELEN);
3466
3467 /* SNPA */
3468 stream_putc(s, 0);
3469
3470 /* Prefix */
3471 stream_put_prefix_addpath(s, prefix, addpath_encode,
3472 addpath_tx_id);
3473
3474 /* Set MP attribute length. */
3475 stream_putc_at(s, sizep, (stream_get_endp(s) - sizep) - 1);
3476 }
3477
3478 /* Prefix SID */
3479 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID)) {
3480 if (attr->label_index != BGP_INVALID_LABEL_INDEX) {
3481 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3482 | BGP_ATTR_FLAG_TRANS);
3483 stream_putc(s, BGP_ATTR_PREFIX_SID);
3484 stream_putc(s, 10);
3485 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX);
3486 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
3487 stream_putc(s, 0); // reserved
3488 stream_putw(s, 0); // flags
3489 stream_putl(s, attr->label_index);
3490 }
3491 }
3492
3493 /* Return total size of attribute. */
3494 len = stream_get_endp(s) - cp - 2;
3495 stream_putw_at(s, cp, len);
3496 }