]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_attr.c
Merge pull request #1720 from qlyoung/gitignore-changelog
[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 /* Check if this is a Gateway MAC-IP advertisement */
1880 attr->default_gw = bgp_attr_default_gw(attr);
1881
1882 /* Extract the Rmac, if any */
1883 bgp_attr_rmac(attr, &attr->rmac);
1884
1885 return BGP_ATTR_PARSE_PROCEED;
1886 }
1887
1888 /* Parse Tunnel Encap attribute in an UPDATE */
1889 static int bgp_attr_encap(uint8_t type, struct peer *peer, /* IN */
1890 bgp_size_t length, /* IN: attr's length field */
1891 struct attr *attr, /* IN: caller already allocated */
1892 u_char flag, /* IN: attr's flags field */
1893 u_char *startp)
1894 {
1895 bgp_size_t total;
1896 uint16_t tunneltype = 0;
1897
1898 total = length + (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
1899
1900 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS)
1901 || !CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL)) {
1902 zlog_info(
1903 "Tunnel Encap attribute flag isn't optional and transitive %d",
1904 flag);
1905 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
1906 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
1907 startp, total);
1908 return -1;
1909 }
1910
1911 if (BGP_ATTR_ENCAP == type) {
1912 /* read outer TLV type and length */
1913 uint16_t tlv_length;
1914
1915 if (length < 4) {
1916 zlog_info(
1917 "Tunnel Encap attribute not long enough to contain outer T,L");
1918 bgp_notify_send_with_data(
1919 peer, BGP_NOTIFY_UPDATE_ERR,
1920 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, startp, total);
1921 return -1;
1922 }
1923 tunneltype = stream_getw(BGP_INPUT(peer));
1924 tlv_length = stream_getw(BGP_INPUT(peer));
1925 length -= 4;
1926
1927 if (tlv_length != length) {
1928 zlog_info("%s: tlv_length(%d) != length(%d)", __func__,
1929 tlv_length, length);
1930 }
1931 }
1932
1933 while (length >= 4) {
1934 uint16_t subtype = 0;
1935 uint16_t sublength = 0;
1936 struct bgp_attr_encap_subtlv *tlv;
1937
1938 if (BGP_ATTR_ENCAP == type) {
1939 subtype = stream_getc(BGP_INPUT(peer));
1940 sublength = stream_getc(BGP_INPUT(peer));
1941 length -= 2;
1942 #if ENABLE_BGP_VNC
1943 } else {
1944 subtype = stream_getw(BGP_INPUT(peer));
1945 sublength = stream_getw(BGP_INPUT(peer));
1946 length -= 4;
1947 #endif
1948 }
1949
1950 if (sublength > length) {
1951 zlog_info(
1952 "Tunnel Encap attribute sub-tlv length %d exceeds remaining length %d",
1953 sublength, length);
1954 bgp_notify_send_with_data(
1955 peer, BGP_NOTIFY_UPDATE_ERR,
1956 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, startp, total);
1957 return -1;
1958 }
1959
1960 /* alloc and copy sub-tlv */
1961 /* TBD make sure these are freed when attributes are released */
1962 tlv = XCALLOC(MTYPE_ENCAP_TLV,
1963 sizeof(struct bgp_attr_encap_subtlv)
1964 + sublength);
1965 tlv->type = subtype;
1966 tlv->length = sublength;
1967 stream_get(tlv->value, peer->curr, sublength);
1968 length -= sublength;
1969
1970 /* attach tlv to encap chain */
1971 if (BGP_ATTR_ENCAP == type) {
1972 struct bgp_attr_encap_subtlv *stlv_last;
1973 for (stlv_last = attr->encap_subtlvs;
1974 stlv_last && stlv_last->next;
1975 stlv_last = stlv_last->next)
1976 ;
1977 if (stlv_last) {
1978 stlv_last->next = tlv;
1979 } else {
1980 attr->encap_subtlvs = tlv;
1981 }
1982 #if ENABLE_BGP_VNC
1983 } else {
1984 struct bgp_attr_encap_subtlv *stlv_last;
1985 for (stlv_last = attr->vnc_subtlvs;
1986 stlv_last && stlv_last->next;
1987 stlv_last = stlv_last->next)
1988 ;
1989 if (stlv_last) {
1990 stlv_last->next = tlv;
1991 } else {
1992 attr->vnc_subtlvs = tlv;
1993 }
1994 #endif
1995 }
1996 }
1997
1998 if (BGP_ATTR_ENCAP == type) {
1999 attr->encap_tunneltype = tunneltype;
2000 }
2001
2002 if (length) {
2003 /* spurious leftover data */
2004 zlog_info(
2005 "Tunnel Encap attribute length is bad: %d leftover octets",
2006 length);
2007 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
2008 BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
2009 startp, total);
2010 return -1;
2011 }
2012
2013 return 0;
2014 }
2015
2016 /* Prefix SID attribute
2017 * draft-ietf-idr-bgp-prefix-sid-05
2018 */
2019 static bgp_attr_parse_ret_t
2020 bgp_attr_prefix_sid(struct bgp_attr_parser_args *args,
2021 struct bgp_nlri *mp_update)
2022 {
2023 struct peer *const peer = args->peer;
2024 struct attr *const attr = args->attr;
2025 int type;
2026 int length;
2027 u_int32_t label_index;
2028 struct in6_addr ipv6_sid;
2029 u_int32_t srgb_base;
2030 u_int32_t srgb_range;
2031 int srgb_count;
2032
2033 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
2034
2035 type = stream_getc(peer->curr);
2036 length = stream_getw(peer->curr);
2037
2038 if (type == BGP_PREFIX_SID_LABEL_INDEX) {
2039 if (length != BGP_PREFIX_SID_LABEL_INDEX_LENGTH) {
2040 zlog_err(
2041 "Prefix SID label index length is %d instead of %d",
2042 length, BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
2043 return bgp_attr_malformed(
2044 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2045 args->total);
2046 }
2047
2048 /* Ignore flags and reserved */
2049 stream_getc(peer->curr);
2050 stream_getw(peer->curr);
2051
2052 /* Fetch the label index and see if it is valid. */
2053 label_index = stream_getl(peer->curr);
2054 if (label_index == BGP_INVALID_LABEL_INDEX)
2055 return bgp_attr_malformed(
2056 args, BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
2057 args->total);
2058
2059 /* Store label index; subsequently, we'll check on
2060 * address-family */
2061 attr->label_index = label_index;
2062
2063 /*
2064 * Ignore the Label index attribute unless received for
2065 * labeled-unicast
2066 * SAFI.
2067 */
2068 if (!mp_update->length
2069 || mp_update->safi != SAFI_LABELED_UNICAST)
2070 attr->label_index = BGP_INVALID_LABEL_INDEX;
2071 }
2072
2073 /* Placeholder code for the IPv6 SID type */
2074 else if (type == BGP_PREFIX_SID_IPV6) {
2075 if (length != BGP_PREFIX_SID_IPV6_LENGTH) {
2076 zlog_err("Prefix SID IPv6 length is %d instead of %d",
2077 length, BGP_PREFIX_SID_IPV6_LENGTH);
2078 return bgp_attr_malformed(
2079 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2080 args->total);
2081 }
2082
2083 /* Ignore reserved */
2084 stream_getc(peer->curr);
2085 stream_getw(peer->curr);
2086
2087 stream_get(&ipv6_sid, peer->curr, 16);
2088 }
2089
2090 /* Placeholder code for the Originator SRGB type */
2091 else if (type == BGP_PREFIX_SID_ORIGINATOR_SRGB) {
2092 /* Ignore flags */
2093 stream_getw(peer->curr);
2094
2095 length -= 2;
2096
2097 if (length % BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH) {
2098 zlog_err(
2099 "Prefix SID Originator SRGB length is %d, it must be a multiple of %d ",
2100 length, BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH);
2101 return bgp_attr_malformed(
2102 args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
2103 args->total);
2104 }
2105
2106 srgb_count = length / BGP_PREFIX_SID_ORIGINATOR_SRGB_LENGTH;
2107
2108 for (int i = 0; i < srgb_count; i++) {
2109 stream_get(&srgb_base, peer->curr, 3);
2110 stream_get(&srgb_range, peer->curr, 3);
2111 }
2112 }
2113
2114 return BGP_ATTR_PARSE_PROCEED;
2115 }
2116
2117 /* BGP unknown attribute treatment. */
2118 static bgp_attr_parse_ret_t bgp_attr_unknown(struct bgp_attr_parser_args *args)
2119 {
2120 bgp_size_t total = args->total;
2121 struct transit *transit;
2122 struct peer *const peer = args->peer;
2123 struct attr *const attr = args->attr;
2124 u_char *const startp = args->startp;
2125 const u_char type = args->type;
2126 const u_char flag = args->flags;
2127 const bgp_size_t length = args->length;
2128
2129 if (bgp_debug_update(peer, NULL, NULL, 1))
2130 zlog_debug(
2131 "%s Unknown attribute is received (type %d, length %d)",
2132 peer->host, type, length);
2133
2134 /* Forward read pointer of input stream. */
2135 stream_forward_getp(peer->curr, length);
2136
2137 /* If any of the mandatory well-known attributes are not recognized,
2138 then the Error Subcode is set to Unrecognized Well-known
2139 Attribute. The Data field contains the unrecognized attribute
2140 (type, length and value). */
2141 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_OPTIONAL)) {
2142 return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_UNREC_ATTR,
2143 args->total);
2144 }
2145
2146 /* Unrecognized non-transitive optional attributes must be quietly
2147 ignored and not passed along to other BGP peers. */
2148 if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS))
2149 return BGP_ATTR_PARSE_PROCEED;
2150
2151 /* If a path with recognized transitive optional attribute is
2152 accepted and passed along to other BGP peers and the Partial bit
2153 in the Attribute Flags octet is set to 1 by some previous AS, it
2154 is not set back to 0 by the current AS. */
2155 SET_FLAG(*startp, BGP_ATTR_FLAG_PARTIAL);
2156
2157 /* Store transitive attribute to the end of attr->transit. */
2158 if (!attr->transit)
2159 attr->transit = XCALLOC(MTYPE_TRANSIT, sizeof(struct transit));
2160
2161 transit = attr->transit;
2162
2163 if (transit->val)
2164 transit->val = XREALLOC(MTYPE_TRANSIT_VAL, transit->val,
2165 transit->length + total);
2166 else
2167 transit->val = XMALLOC(MTYPE_TRANSIT_VAL, total);
2168
2169 memcpy(transit->val + transit->length, startp, total);
2170 transit->length += total;
2171
2172 return BGP_ATTR_PARSE_PROCEED;
2173 }
2174
2175 /* Well-known attribute check. */
2176 static int bgp_attr_check(struct peer *peer, struct attr *attr)
2177 {
2178 u_char type = 0;
2179
2180 /* BGP Graceful-Restart End-of-RIB for IPv4 unicast is signaled as an
2181 * empty UPDATE. */
2182 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV) && !attr->flag)
2183 return BGP_ATTR_PARSE_PROCEED;
2184
2185 /* "An UPDATE message that contains the MP_UNREACH_NLRI is not required
2186 to carry any other path attributes.", though if MP_REACH_NLRI or NLRI
2187 are present, it should. Check for any other attribute being present
2188 instead.
2189 */
2190 if (attr->flag == ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI))
2191 return BGP_ATTR_PARSE_PROCEED;
2192
2193 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN)))
2194 type = BGP_ATTR_ORIGIN;
2195
2196 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
2197 type = BGP_ATTR_AS_PATH;
2198
2199 /* RFC 2858 makes Next-Hop optional/ignored, if MP_REACH_NLRI is present
2200 * and
2201 * NLRI is empty. We can't easily check NLRI empty here though.
2202 */
2203 if (!CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP))
2204 && !CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI)))
2205 type = BGP_ATTR_NEXT_HOP;
2206
2207 if (peer->sort == BGP_PEER_IBGP
2208 && !CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))
2209 type = BGP_ATTR_LOCAL_PREF;
2210
2211 if (type) {
2212 zlog_warn("%s Missing well-known attribute %s.", peer->host,
2213 lookup_msg(attr_str, type, NULL));
2214 bgp_notify_send_with_data(peer, BGP_NOTIFY_UPDATE_ERR,
2215 BGP_NOTIFY_UPDATE_MISS_ATTR, &type,
2216 1);
2217 return BGP_ATTR_PARSE_ERROR;
2218 }
2219 return BGP_ATTR_PARSE_PROCEED;
2220 }
2221
2222 /* Read attribute of update packet. This function is called from
2223 bgp_update_receive() in bgp_packet.c. */
2224 bgp_attr_parse_ret_t bgp_attr_parse(struct peer *peer, struct attr *attr,
2225 bgp_size_t size, struct bgp_nlri *mp_update,
2226 struct bgp_nlri *mp_withdraw)
2227 {
2228 int ret;
2229 u_char flag = 0;
2230 u_char type = 0;
2231 bgp_size_t length;
2232 u_char *startp, *endp;
2233 u_char *attr_endp;
2234 u_char seen[BGP_ATTR_BITMAP_SIZE];
2235 /* we need the as4_path only until we have synthesized the as_path with
2236 * it */
2237 /* same goes for as4_aggregator */
2238 struct aspath *as4_path = NULL;
2239 as_t as4_aggregator = 0;
2240 struct in_addr as4_aggregator_addr = {.s_addr = 0};
2241
2242 /* Initialize bitmap. */
2243 memset(seen, 0, BGP_ATTR_BITMAP_SIZE);
2244
2245 /* End pointer of BGP attribute. */
2246 endp = BGP_INPUT_PNT(peer) + size;
2247
2248 /* Get attributes to the end of attribute length. */
2249 while (BGP_INPUT_PNT(peer) < endp) {
2250 /* Check remaining length check.*/
2251 if (endp - BGP_INPUT_PNT(peer) < BGP_ATTR_MIN_LEN) {
2252 /* XXX warning: long int format, int arg (arg 5) */
2253 zlog_warn(
2254 "%s: error BGP attribute length %lu is smaller than min len",
2255 peer->host,
2256 (unsigned long)(endp
2257 - stream_pnt(BGP_INPUT(peer))));
2258
2259 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2260 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2261 return BGP_ATTR_PARSE_ERROR;
2262 }
2263
2264 /* Fetch attribute flag and type. */
2265 startp = BGP_INPUT_PNT(peer);
2266 /* "The lower-order four bits of the Attribute Flags octet are
2267 unused. They MUST be zero when sent and MUST be ignored when
2268 received." */
2269 flag = 0xF0 & stream_getc(BGP_INPUT(peer));
2270 type = stream_getc(BGP_INPUT(peer));
2271
2272 /* Check whether Extended-Length applies and is in bounds */
2273 if (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN)
2274 && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1))) {
2275 zlog_warn(
2276 "%s: Extended length set, but just %lu bytes of attr header",
2277 peer->host,
2278 (unsigned long)(endp
2279 - stream_pnt(BGP_INPUT(peer))));
2280
2281 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2282 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2283 return BGP_ATTR_PARSE_ERROR;
2284 }
2285
2286 /* Check extended attribue length bit. */
2287 if (CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN))
2288 length = stream_getw(BGP_INPUT(peer));
2289 else
2290 length = stream_getc(BGP_INPUT(peer));
2291
2292 /* If any attribute appears more than once in the UPDATE
2293 message, then the Error Subcode is set to Malformed Attribute
2294 List. */
2295
2296 if (CHECK_BITMAP(seen, type)) {
2297 zlog_warn(
2298 "%s: error BGP attribute type %d appears twice in a message",
2299 peer->host, type);
2300
2301 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2302 BGP_NOTIFY_UPDATE_MAL_ATTR);
2303 return BGP_ATTR_PARSE_ERROR;
2304 }
2305
2306 /* Set type to bitmap to check duplicate attribute. `type' is
2307 unsigned char so it never overflow bitmap range. */
2308
2309 SET_BITMAP(seen, type);
2310
2311 /* Overflow check. */
2312 attr_endp = BGP_INPUT_PNT(peer) + length;
2313
2314 if (attr_endp > endp) {
2315 zlog_warn(
2316 "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p",
2317 peer->host, type, length, size, attr_endp,
2318 endp);
2319 /*
2320 * RFC 4271 6.3
2321 * If any recognized attribute has an Attribute
2322 * Length that conflicts with the expected length
2323 * (based on the attribute type code), then the
2324 * Error Subcode MUST be set to Attribute Length
2325 * Error. The Data field MUST contain the erroneous
2326 * attribute (type, length, and value).
2327 * ----------
2328 * We do not currently have a good way to determine the
2329 * length of the attribute independent of the length
2330 * received in the message. Instead we send the
2331 * minimum between the amount of data we have and the
2332 * amount specified by the attribute length field.
2333 *
2334 * Instead of directly passing in the packet buffer and
2335 * offset we use the stream_get* functions to read into
2336 * a stack buffer, since they perform bounds checking
2337 * and we are working with untrusted data.
2338 */
2339 unsigned char ndata[BGP_MAX_PACKET_SIZE];
2340 memset(ndata, 0x00, sizeof(ndata));
2341 size_t lfl =
2342 CHECK_FLAG(flag, BGP_ATTR_FLAG_EXTLEN) ? 2 : 1;
2343 /* Rewind to end of flag field */
2344 stream_forward_getp(BGP_INPUT(peer), -(1 + lfl));
2345 /* Type */
2346 stream_get(&ndata[0], BGP_INPUT(peer), 1);
2347 /* Length */
2348 stream_get(&ndata[1], BGP_INPUT(peer), lfl);
2349 /* Value */
2350 size_t atl = attr_endp - startp;
2351 size_t ndl = MIN(atl, STREAM_READABLE(BGP_INPUT(peer)));
2352 stream_get(&ndata[lfl + 1], BGP_INPUT(peer), ndl);
2353
2354 bgp_notify_send_with_data(
2355 peer, BGP_NOTIFY_UPDATE_ERR,
2356 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, ndata,
2357 ndl + lfl + 1);
2358
2359 return BGP_ATTR_PARSE_ERROR;
2360 }
2361
2362 struct bgp_attr_parser_args attr_args = {
2363 .peer = peer,
2364 .length = length,
2365 .attr = attr,
2366 .type = type,
2367 .flags = flag,
2368 .startp = startp,
2369 .total = attr_endp - startp,
2370 };
2371
2372
2373 /* If any recognized attribute has Attribute Flags that conflict
2374 with the Attribute Type Code, then the Error Subcode is set
2375 to
2376 Attribute Flags Error. The Data field contains the erroneous
2377 attribute (type, length and value). */
2378 if (bgp_attr_flag_invalid(&attr_args)) {
2379 bgp_attr_parse_ret_t ret;
2380 ret = bgp_attr_malformed(
2381 &attr_args, BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
2382 attr_args.total);
2383 if (ret == BGP_ATTR_PARSE_PROCEED)
2384 continue;
2385 return ret;
2386 }
2387
2388 /* OK check attribute and store it's value. */
2389 switch (type) {
2390 case BGP_ATTR_ORIGIN:
2391 ret = bgp_attr_origin(&attr_args);
2392 break;
2393 case BGP_ATTR_AS_PATH:
2394 ret = bgp_attr_aspath(&attr_args);
2395 break;
2396 case BGP_ATTR_AS4_PATH:
2397 ret = bgp_attr_as4_path(&attr_args, &as4_path);
2398 break;
2399 case BGP_ATTR_NEXT_HOP:
2400 ret = bgp_attr_nexthop(&attr_args);
2401 break;
2402 case BGP_ATTR_MULTI_EXIT_DISC:
2403 ret = bgp_attr_med(&attr_args);
2404 break;
2405 case BGP_ATTR_LOCAL_PREF:
2406 ret = bgp_attr_local_pref(&attr_args);
2407 break;
2408 case BGP_ATTR_ATOMIC_AGGREGATE:
2409 ret = bgp_attr_atomic(&attr_args);
2410 break;
2411 case BGP_ATTR_AGGREGATOR:
2412 ret = bgp_attr_aggregator(&attr_args);
2413 break;
2414 case BGP_ATTR_AS4_AGGREGATOR:
2415 ret = bgp_attr_as4_aggregator(&attr_args,
2416 &as4_aggregator,
2417 &as4_aggregator_addr);
2418 break;
2419 case BGP_ATTR_COMMUNITIES:
2420 ret = bgp_attr_community(&attr_args);
2421 break;
2422 case BGP_ATTR_LARGE_COMMUNITIES:
2423 ret = bgp_attr_large_community(&attr_args);
2424 break;
2425 case BGP_ATTR_ORIGINATOR_ID:
2426 ret = bgp_attr_originator_id(&attr_args);
2427 break;
2428 case BGP_ATTR_CLUSTER_LIST:
2429 ret = bgp_attr_cluster_list(&attr_args);
2430 break;
2431 case BGP_ATTR_MP_REACH_NLRI:
2432 ret = bgp_mp_reach_parse(&attr_args, mp_update);
2433 break;
2434 case BGP_ATTR_MP_UNREACH_NLRI:
2435 ret = bgp_mp_unreach_parse(&attr_args, mp_withdraw);
2436 break;
2437 case BGP_ATTR_EXT_COMMUNITIES:
2438 ret = bgp_attr_ext_communities(&attr_args);
2439 break;
2440 #if ENABLE_BGP_VNC
2441 case BGP_ATTR_VNC:
2442 #endif
2443 case BGP_ATTR_ENCAP:
2444 ret = bgp_attr_encap(type, peer, length, attr, flag,
2445 startp);
2446 break;
2447 case BGP_ATTR_PREFIX_SID:
2448 ret = bgp_attr_prefix_sid(&attr_args, mp_update);
2449 break;
2450 default:
2451 ret = bgp_attr_unknown(&attr_args);
2452 break;
2453 }
2454
2455 if (ret == BGP_ATTR_PARSE_ERROR_NOTIFYPLS) {
2456 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2457 BGP_NOTIFY_UPDATE_MAL_ATTR);
2458 ret = BGP_ATTR_PARSE_ERROR;
2459 }
2460
2461 if (ret == BGP_ATTR_PARSE_EOR) {
2462 if (as4_path)
2463 aspath_unintern(&as4_path);
2464 return ret;
2465 }
2466
2467 /* If hard error occured immediately return to the caller. */
2468 if (ret == BGP_ATTR_PARSE_ERROR) {
2469 zlog_warn("%s: Attribute %s, parse error", peer->host,
2470 lookup_msg(attr_str, type, NULL));
2471 if (as4_path)
2472 aspath_unintern(&as4_path);
2473 return ret;
2474 }
2475 if (ret == BGP_ATTR_PARSE_WITHDRAW) {
2476
2477 zlog_warn(
2478 "%s: Attribute %s, parse error - treating as withdrawal",
2479 peer->host, lookup_msg(attr_str, type, NULL));
2480 if (as4_path)
2481 aspath_unintern(&as4_path);
2482 return ret;
2483 }
2484
2485 /* Check the fetched length. */
2486 if (BGP_INPUT_PNT(peer) != attr_endp) {
2487 zlog_warn("%s: BGP attribute %s, fetch error",
2488 peer->host, lookup_msg(attr_str, type, NULL));
2489 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2490 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2491 if (as4_path)
2492 aspath_unintern(&as4_path);
2493 return BGP_ATTR_PARSE_ERROR;
2494 }
2495 }
2496
2497 /* Check final read pointer is same as end pointer. */
2498 if (BGP_INPUT_PNT(peer) != endp) {
2499 zlog_warn("%s: BGP attribute %s, length mismatch", peer->host,
2500 lookup_msg(attr_str, type, NULL));
2501 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2502 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
2503 if (as4_path)
2504 aspath_unintern(&as4_path);
2505 return BGP_ATTR_PARSE_ERROR;
2506 }
2507
2508 /* Check all mandatory well-known attributes are present */
2509 {
2510 bgp_attr_parse_ret_t ret;
2511 if ((ret = bgp_attr_check(peer, attr)) < 0) {
2512 if (as4_path)
2513 aspath_unintern(&as4_path);
2514 return ret;
2515 }
2516 }
2517
2518 /*
2519 * At this place we can see whether we got AS4_PATH and/or
2520 * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
2521 * We can not do this before we've read all attributes because
2522 * the as4 handling does not say whether AS4_PATH has to be sent
2523 * after AS_PATH or not - and when AS4_AGGREGATOR will be send
2524 * in relationship to AGGREGATOR.
2525 * So, to be defensive, we are not relying on any order and read
2526 * all attributes first, including these 32bit ones, and now,
2527 * afterwards, we look what and if something is to be done for as4.
2528 *
2529 * It is possible to not have AS_PATH, e.g. GR EoR and sole
2530 * MP_UNREACH_NLRI.
2531 */
2532 /* actually... this doesn't ever return failure currently, but
2533 * better safe than sorry */
2534 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH))
2535 && bgp_attr_munge_as4_attrs(peer, attr, as4_path, as4_aggregator,
2536 &as4_aggregator_addr)) {
2537 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
2538 BGP_NOTIFY_UPDATE_MAL_ATTR);
2539 if (as4_path)
2540 aspath_unintern(&as4_path);
2541 return BGP_ATTR_PARSE_ERROR;
2542 }
2543
2544 /* At this stage, we have done all fiddling with as4, and the
2545 * resulting info is in attr->aggregator resp. attr->aspath
2546 * so we can chuck as4_aggregator and as4_path alltogether in
2547 * order to save memory
2548 */
2549 if (as4_path) {
2550 aspath_unintern(&as4_path); /* unintern - it is in the hash */
2551 /* The flag that we got this is still there, but that does not
2552 * do any trouble
2553 */
2554 }
2555 /*
2556 * The "rest" of the code does nothing with as4_aggregator.
2557 * there is no memory attached specifically which is not part
2558 * of the attr.
2559 * so ignoring just means do nothing.
2560 */
2561 /*
2562 * Finally do the checks on the aspath we did not do yet
2563 * because we waited for a potentially synthesized aspath.
2564 */
2565 if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH))) {
2566 ret = bgp_attr_aspath_check(peer, attr);
2567 if (ret != BGP_ATTR_PARSE_PROCEED)
2568 return ret;
2569 }
2570 /* Finally intern unknown attribute. */
2571 if (attr->transit)
2572 attr->transit = transit_intern(attr->transit);
2573 if (attr->encap_subtlvs)
2574 attr->encap_subtlvs =
2575 encap_intern(attr->encap_subtlvs, ENCAP_SUBTLV_TYPE);
2576 #if ENABLE_BGP_VNC
2577 if (attr->vnc_subtlvs)
2578 attr->vnc_subtlvs =
2579 encap_intern(attr->vnc_subtlvs, VNC_SUBTLV_TYPE);
2580 #endif
2581
2582 return BGP_ATTR_PARSE_PROCEED;
2583 }
2584
2585 size_t bgp_packet_mpattr_start(struct stream *s, struct peer *peer, afi_t afi,
2586 safi_t safi, struct bpacket_attr_vec_arr *vecarr,
2587 struct attr *attr)
2588 {
2589 size_t sizep;
2590 iana_afi_t pkt_afi;
2591 iana_safi_t pkt_safi;
2592 afi_t nh_afi;
2593
2594 /* Set extended bit always to encode the attribute length as 2 bytes */
2595 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_EXTLEN);
2596 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
2597 sizep = stream_get_endp(s);
2598 stream_putw(s, 0); /* Marker: Attribute length. */
2599
2600
2601 /* Convert AFI, SAFI to values for packet. */
2602 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
2603
2604 stream_putw(s, pkt_afi); /* AFI */
2605 stream_putc(s, pkt_safi); /* SAFI */
2606
2607 /* Nexthop AFI */
2608 if (afi == AFI_IP
2609 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
2610 nh_afi = peer_cap_enhe(peer, afi, safi) ? AFI_IP6 : AFI_IP;
2611 else
2612 nh_afi = BGP_NEXTHOP_AFI_FROM_NHLEN(attr->mp_nexthop_len);
2613
2614 /* Nexthop */
2615 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s, attr);
2616 switch (nh_afi) {
2617 case AFI_IP:
2618 switch (safi) {
2619 case SAFI_UNICAST:
2620 case SAFI_MULTICAST:
2621 case SAFI_LABELED_UNICAST:
2622 stream_putc(s, 4);
2623 stream_put_ipv4(s, attr->nexthop.s_addr);
2624 break;
2625 case SAFI_MPLS_VPN:
2626 stream_putc(s, 12);
2627 stream_putl(s, 0); /* RD = 0, per RFC */
2628 stream_putl(s, 0);
2629 stream_put(s, &attr->mp_nexthop_global_in, 4);
2630 break;
2631 case SAFI_ENCAP:
2632 case SAFI_EVPN:
2633 stream_putc(s, 4);
2634 stream_put(s, &attr->mp_nexthop_global_in, 4);
2635 break;
2636 default:
2637 break;
2638 }
2639 break;
2640 case AFI_IP6:
2641 switch (safi) {
2642 case SAFI_UNICAST:
2643 case SAFI_MULTICAST:
2644 case SAFI_LABELED_UNICAST:
2645 case SAFI_EVPN: {
2646 if (attr->mp_nexthop_len
2647 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
2648 stream_putc(s,
2649 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL);
2650 stream_put(s, &attr->mp_nexthop_global,
2651 IPV6_MAX_BYTELEN);
2652 stream_put(s, &attr->mp_nexthop_local,
2653 IPV6_MAX_BYTELEN);
2654 } else {
2655 stream_putc(s, IPV6_MAX_BYTELEN);
2656 stream_put(s, &attr->mp_nexthop_global,
2657 IPV6_MAX_BYTELEN);
2658 }
2659 } break;
2660 case SAFI_MPLS_VPN: {
2661 if (attr->mp_nexthop_len
2662 == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
2663 stream_putc(s, 24);
2664 stream_putl(s, 0); /* RD = 0, per RFC */
2665 stream_putl(s, 0);
2666 stream_put(s, &attr->mp_nexthop_global,
2667 IPV6_MAX_BYTELEN);
2668 } else if (attr->mp_nexthop_len
2669 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
2670 stream_putc(s, 48);
2671 stream_putl(s, 0); /* RD = 0, per RFC */
2672 stream_putl(s, 0);
2673 stream_put(s, &attr->mp_nexthop_global,
2674 IPV6_MAX_BYTELEN);
2675 stream_putl(s, 0); /* RD = 0, per RFC */
2676 stream_putl(s, 0);
2677 stream_put(s, &attr->mp_nexthop_local,
2678 IPV6_MAX_BYTELEN);
2679 }
2680 } break;
2681 case SAFI_ENCAP:
2682 stream_putc(s, IPV6_MAX_BYTELEN);
2683 stream_put(s, &attr->mp_nexthop_global,
2684 IPV6_MAX_BYTELEN);
2685 break;
2686 default:
2687 break;
2688 }
2689 break;
2690 default:
2691 zlog_err(
2692 "Bad nexthop when sening to %s, AFI %u SAFI %u nhlen %d",
2693 peer->host, afi, safi, attr->mp_nexthop_len);
2694 break;
2695 }
2696
2697 /* SNPA */
2698 stream_putc(s, 0);
2699 return sizep;
2700 }
2701
2702 void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
2703 struct prefix *p, struct prefix_rd *prd,
2704 mpls_label_t *label, u_int32_t num_labels,
2705 int addpath_encode, u_int32_t addpath_tx_id,
2706 struct attr *attr)
2707 {
2708 if (safi == SAFI_MPLS_VPN) {
2709 if (addpath_encode)
2710 stream_putl(s, addpath_tx_id);
2711 /* Label, RD, Prefix write. */
2712 stream_putc(s, p->prefixlen + 88);
2713 stream_put(s, label, BGP_LABEL_BYTES);
2714 stream_put(s, prd->val, 8);
2715 stream_put(s, &p->u.prefix, PSIZE(p->prefixlen));
2716 } else if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
2717 /* EVPN prefix - contents depend on type */
2718 bgp_evpn_encode_prefix(s, p, prd, label, num_labels,
2719 attr, addpath_encode, addpath_tx_id);
2720 } else if (safi == SAFI_LABELED_UNICAST) {
2721 /* Prefix write with label. */
2722 stream_put_labeled_prefix(s, p, label);
2723 } else
2724 stream_put_prefix_addpath(s, p, addpath_encode, addpath_tx_id);
2725 }
2726
2727 size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi, struct prefix *p)
2728 {
2729 int size = PSIZE(p->prefixlen);
2730 if (safi == SAFI_MPLS_VPN)
2731 size += 88;
2732 else if (afi == AFI_L2VPN && safi == SAFI_EVPN)
2733 size += 232; // TODO: Maximum possible for type-2, type-3 and
2734 // type-5
2735 return size;
2736 }
2737
2738 /*
2739 * Encodes the tunnel encapsulation attribute,
2740 * and with ENABLE_BGP_VNC the VNC attribute which uses
2741 * almost the same TLV format
2742 */
2743 static void bgp_packet_mpattr_tea(struct bgp *bgp, struct peer *peer,
2744 struct stream *s, struct attr *attr,
2745 uint8_t attrtype)
2746 {
2747 unsigned int attrlenfield = 0;
2748 unsigned int attrhdrlen = 0;
2749 struct bgp_attr_encap_subtlv *subtlvs;
2750 struct bgp_attr_encap_subtlv *st;
2751 const char *attrname;
2752
2753 if (!attr || (attrtype == BGP_ATTR_ENCAP
2754 && (!attr->encap_tunneltype
2755 || attr->encap_tunneltype == BGP_ENCAP_TYPE_MPLS)))
2756 return;
2757
2758 switch (attrtype) {
2759 case BGP_ATTR_ENCAP:
2760 attrname = "Tunnel Encap";
2761 subtlvs = attr->encap_subtlvs;
2762 if (subtlvs == NULL) /* nothing to do */
2763 return;
2764 /*
2765 * The tunnel encap attr has an "outer" tlv.
2766 * T = tunneltype,
2767 * L = total length of subtlvs,
2768 * V = concatenated subtlvs.
2769 */
2770 attrlenfield = 2 + 2; /* T + L */
2771 attrhdrlen = 1 + 1; /* subTLV T + L */
2772 break;
2773
2774 #if ENABLE_BGP_VNC
2775 case BGP_ATTR_VNC:
2776 attrname = "VNC";
2777 subtlvs = attr->vnc_subtlvs;
2778 if (subtlvs == NULL) /* nothing to do */
2779 return;
2780 attrlenfield = 0; /* no outer T + L */
2781 attrhdrlen = 2 + 2; /* subTLV T + L */
2782 break;
2783 #endif
2784
2785 default:
2786 assert(0);
2787 }
2788
2789 /* compute attr length */
2790 for (st = subtlvs; st; st = st->next) {
2791 attrlenfield += (attrhdrlen + st->length);
2792 }
2793
2794 if (attrlenfield > 0xffff) {
2795 zlog_info("%s attribute is too long (length=%d), can't send it",
2796 attrname, attrlenfield);
2797 return;
2798 }
2799
2800 if (attrlenfield > 0xff) {
2801 /* 2-octet length field */
2802 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL
2803 | BGP_ATTR_FLAG_EXTLEN);
2804 stream_putc(s, attrtype);
2805 stream_putw(s, attrlenfield & 0xffff);
2806 } else {
2807 /* 1-octet length field */
2808 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL);
2809 stream_putc(s, attrtype);
2810 stream_putc(s, attrlenfield & 0xff);
2811 }
2812
2813 if (attrtype == BGP_ATTR_ENCAP) {
2814 /* write outer T+L */
2815 stream_putw(s, attr->encap_tunneltype);
2816 stream_putw(s, attrlenfield - 4);
2817 }
2818
2819 /* write each sub-tlv */
2820 for (st = subtlvs; st; st = st->next) {
2821 if (attrtype == BGP_ATTR_ENCAP) {
2822 stream_putc(s, st->type);
2823 stream_putc(s, st->length);
2824 #if ENABLE_BGP_VNC
2825 } else {
2826 stream_putw(s, st->type);
2827 stream_putw(s, st->length);
2828 #endif
2829 }
2830 stream_put(s, st->value, st->length);
2831 }
2832 }
2833
2834 void bgp_packet_mpattr_end(struct stream *s, size_t sizep)
2835 {
2836 /* Set MP attribute length. Don't count the (2) bytes used to encode
2837 the attr length */
2838 stream_putw_at(s, sizep, (stream_get_endp(s) - sizep) - 2);
2839 }
2840
2841 /* Make attribute packet. */
2842 bgp_size_t bgp_packet_attribute(struct bgp *bgp, struct peer *peer,
2843 struct stream *s, struct attr *attr,
2844 struct bpacket_attr_vec_arr *vecarr,
2845 struct prefix *p, afi_t afi, safi_t safi,
2846 struct peer *from, struct prefix_rd *prd,
2847 mpls_label_t *label, u_int32_t num_labels,
2848 int addpath_encode, u_int32_t addpath_tx_id)
2849 {
2850 size_t cp;
2851 size_t aspath_sizep;
2852 struct aspath *aspath;
2853 int send_as4_path = 0;
2854 int send_as4_aggregator = 0;
2855 int use32bit = (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0;
2856
2857 if (!bgp)
2858 bgp = peer->bgp;
2859
2860 /* Remember current pointer. */
2861 cp = stream_get_endp(s);
2862
2863 if (p
2864 && !((afi == AFI_IP && safi == SAFI_UNICAST)
2865 && !peer_cap_enhe(peer, afi, safi))) {
2866 size_t mpattrlen_pos = 0;
2867
2868 mpattrlen_pos = bgp_packet_mpattr_start(s, peer, afi, safi,
2869 vecarr, attr);
2870 bgp_packet_mpattr_prefix(s, afi, safi, p, prd,
2871 label, num_labels,
2872 addpath_encode, addpath_tx_id, attr);
2873 bgp_packet_mpattr_end(s, mpattrlen_pos);
2874 }
2875
2876 /* Origin attribute. */
2877 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2878 stream_putc(s, BGP_ATTR_ORIGIN);
2879 stream_putc(s, 1);
2880 stream_putc(s, attr->origin);
2881
2882 /* AS path attribute. */
2883
2884 /* If remote-peer is EBGP */
2885 if (peer->sort == BGP_PEER_EBGP
2886 && (!CHECK_FLAG(peer->af_flags[afi][safi],
2887 PEER_FLAG_AS_PATH_UNCHANGED)
2888 || attr->aspath->segments == NULL)
2889 && (!CHECK_FLAG(peer->af_flags[afi][safi],
2890 PEER_FLAG_RSERVER_CLIENT))) {
2891 aspath = aspath_dup(attr->aspath);
2892
2893 /* Even though we may not be configured for confederations we
2894 * may have
2895 * RXed an AS_PATH with AS_CONFED_SEQUENCE or AS_CONFED_SET */
2896 aspath = aspath_delete_confed_seq(aspath);
2897
2898 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
2899 /* Stuff our path CONFED_ID on the front */
2900 aspath = aspath_add_seq(aspath, bgp->confed_id);
2901 } else {
2902 if (peer->change_local_as) {
2903 /* If replace-as is specified, we only use the
2904 change_local_as when
2905 advertising routes. */
2906 if (!CHECK_FLAG(
2907 peer->flags,
2908 PEER_FLAG_LOCAL_AS_REPLACE_AS)) {
2909 aspath = aspath_add_seq(aspath,
2910 peer->local_as);
2911 }
2912 aspath = aspath_add_seq(aspath,
2913 peer->change_local_as);
2914 } else {
2915 aspath = aspath_add_seq(aspath, peer->local_as);
2916 }
2917 }
2918 } else if (peer->sort == BGP_PEER_CONFED) {
2919 /* A confed member, so we need to do the AS_CONFED_SEQUENCE
2920 * thing */
2921 aspath = aspath_dup(attr->aspath);
2922 aspath = aspath_add_confed_seq(aspath, peer->local_as);
2923 } else
2924 aspath = attr->aspath;
2925
2926 /* If peer is not AS4 capable, then:
2927 * - send the created AS_PATH out as AS4_PATH (optional, transitive),
2928 * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path
2929 * segment
2930 * types are in it (i.e. exclude them if they are there)
2931 * AND do this only if there is at least one asnum > 65535 in the
2932 * path!
2933 * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and
2934 * change
2935 * all ASnums > 65535 to BGP_AS_TRANS
2936 */
2937
2938 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_EXTLEN);
2939 stream_putc(s, BGP_ATTR_AS_PATH);
2940 aspath_sizep = stream_get_endp(s);
2941 stream_putw(s, 0);
2942 stream_putw_at(s, aspath_sizep, aspath_put(s, aspath, use32bit));
2943
2944 /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
2945 * in the path
2946 */
2947 if (!use32bit && aspath_has_as4(aspath))
2948 send_as4_path =
2949 1; /* we'll do this later, at the correct place */
2950
2951 /* Nexthop attribute. */
2952 if (afi == AFI_IP && safi == SAFI_UNICAST
2953 && !peer_cap_enhe(peer, afi, safi)) {
2954 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
2955 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2956 stream_putc(s, BGP_ATTR_NEXT_HOP);
2957 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s,
2958 attr);
2959 stream_putc(s, 4);
2960 stream_put_ipv4(s, attr->nexthop.s_addr);
2961 } else if (peer_cap_enhe(from, afi, safi)) {
2962 /*
2963 * Likely this is the case when an IPv4 prefix was
2964 * received with
2965 * Extended Next-hop capability and now being advertised
2966 * to
2967 * non-ENHE peers.
2968 * Setting the mandatory (ipv4) next-hop attribute here
2969 * to enable
2970 * implicit next-hop self with correct (ipv4 address
2971 * family).
2972 */
2973 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2974 stream_putc(s, BGP_ATTR_NEXT_HOP);
2975 bpacket_attr_vec_arr_set_vec(vecarr, BGP_ATTR_VEC_NH, s,
2976 NULL);
2977 stream_putc(s, 4);
2978 stream_put_ipv4(s, 0);
2979 }
2980 }
2981
2982 /* MED attribute. */
2983 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)
2984 || bgp->maxmed_active) {
2985 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
2986 stream_putc(s, BGP_ATTR_MULTI_EXIT_DISC);
2987 stream_putc(s, 4);
2988 stream_putl(s, (bgp->maxmed_active ? bgp->maxmed_value
2989 : attr->med));
2990 }
2991
2992 /* Local preference. */
2993 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED) {
2994 stream_putc(s, BGP_ATTR_FLAG_TRANS);
2995 stream_putc(s, BGP_ATTR_LOCAL_PREF);
2996 stream_putc(s, 4);
2997 stream_putl(s, attr->local_pref);
2998 }
2999
3000 /* Atomic aggregate. */
3001 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
3002 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3003 stream_putc(s, BGP_ATTR_ATOMIC_AGGREGATE);
3004 stream_putc(s, 0);
3005 }
3006
3007 /* Aggregator. */
3008 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR)) {
3009 /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
3010 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3011 stream_putc(s, BGP_ATTR_AGGREGATOR);
3012
3013 if (use32bit) {
3014 /* AS4 capable peer */
3015 stream_putc(s, 8);
3016 stream_putl(s, attr->aggregator_as);
3017 } else {
3018 /* 2-byte AS peer */
3019 stream_putc(s, 6);
3020
3021 /* Is ASN representable in 2-bytes? Or must AS_TRANS be
3022 * used? */
3023 if (attr->aggregator_as > 65535) {
3024 stream_putw(s, BGP_AS_TRANS);
3025
3026 /* we have to send AS4_AGGREGATOR, too.
3027 * we'll do that later in order to send
3028 * attributes in ascending
3029 * order.
3030 */
3031 send_as4_aggregator = 1;
3032 } else
3033 stream_putw(s, (u_int16_t)attr->aggregator_as);
3034 }
3035 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3036 }
3037
3038 /* Community attribute. */
3039 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
3040 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))) {
3041 if (attr->community->size * 4 > 255) {
3042 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3043 | BGP_ATTR_FLAG_TRANS
3044 | BGP_ATTR_FLAG_EXTLEN);
3045 stream_putc(s, BGP_ATTR_COMMUNITIES);
3046 stream_putw(s, attr->community->size * 4);
3047 } else {
3048 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3049 | BGP_ATTR_FLAG_TRANS);
3050 stream_putc(s, BGP_ATTR_COMMUNITIES);
3051 stream_putc(s, attr->community->size * 4);
3052 }
3053 stream_put(s, attr->community->val, attr->community->size * 4);
3054 }
3055
3056 /*
3057 * Large Community attribute.
3058 */
3059 if (CHECK_FLAG(peer->af_flags[afi][safi],
3060 PEER_FLAG_SEND_LARGE_COMMUNITY)
3061 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))) {
3062 if (lcom_length(attr->lcommunity) > 255) {
3063 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3064 | BGP_ATTR_FLAG_TRANS
3065 | BGP_ATTR_FLAG_EXTLEN);
3066 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3067 stream_putw(s, lcom_length(attr->lcommunity));
3068 } else {
3069 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3070 | BGP_ATTR_FLAG_TRANS);
3071 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3072 stream_putc(s, lcom_length(attr->lcommunity));
3073 }
3074 stream_put(s, attr->lcommunity->val,
3075 lcom_length(attr->lcommunity));
3076 }
3077
3078 /* Route Reflector. */
3079 if (peer->sort == BGP_PEER_IBGP && from
3080 && from->sort == BGP_PEER_IBGP) {
3081 /* Originator ID. */
3082 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3083 stream_putc(s, BGP_ATTR_ORIGINATOR_ID);
3084 stream_putc(s, 4);
3085
3086 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
3087 stream_put_in_addr(s, &attr->originator_id);
3088 else
3089 stream_put_in_addr(s, &from->remote_id);
3090
3091 /* Cluster list. */
3092 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3093 stream_putc(s, BGP_ATTR_CLUSTER_LIST);
3094
3095 if (attr->cluster) {
3096 stream_putc(s, attr->cluster->length + 4);
3097 /* If this peer configuration's parent BGP has
3098 * cluster_id. */
3099 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
3100 stream_put_in_addr(s, &bgp->cluster_id);
3101 else
3102 stream_put_in_addr(s, &bgp->router_id);
3103 stream_put(s, attr->cluster->list,
3104 attr->cluster->length);
3105 } else {
3106 stream_putc(s, 4);
3107 /* If this peer configuration's parent BGP has
3108 * cluster_id. */
3109 if (bgp->config & BGP_CONFIG_CLUSTER_ID)
3110 stream_put_in_addr(s, &bgp->cluster_id);
3111 else
3112 stream_put_in_addr(s, &bgp->router_id);
3113 }
3114 }
3115
3116 /* Extended Communities attribute. */
3117 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
3118 && (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) {
3119 if (peer->sort == BGP_PEER_IBGP
3120 || peer->sort == BGP_PEER_CONFED) {
3121 if (attr->ecommunity->size * 8 > 255) {
3122 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3123 | BGP_ATTR_FLAG_TRANS
3124 | BGP_ATTR_FLAG_EXTLEN);
3125 stream_putc(s, BGP_ATTR_EXT_COMMUNITIES);
3126 stream_putw(s, attr->ecommunity->size * 8);
3127 } else {
3128 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3129 | BGP_ATTR_FLAG_TRANS);
3130 stream_putc(s, BGP_ATTR_EXT_COMMUNITIES);
3131 stream_putc(s, attr->ecommunity->size * 8);
3132 }
3133 stream_put(s, attr->ecommunity->val,
3134 attr->ecommunity->size * 8);
3135 } else {
3136 u_int8_t *pnt;
3137 int tbit;
3138 int ecom_tr_size = 0;
3139 int i;
3140
3141 for (i = 0; i < attr->ecommunity->size; i++) {
3142 pnt = attr->ecommunity->val + (i * 8);
3143 tbit = *pnt;
3144
3145 if (CHECK_FLAG(tbit,
3146 ECOMMUNITY_FLAG_NON_TRANSITIVE))
3147 continue;
3148
3149 ecom_tr_size++;
3150 }
3151
3152 if (ecom_tr_size) {
3153 if (ecom_tr_size * 8 > 255) {
3154 stream_putc(
3155 s,
3156 BGP_ATTR_FLAG_OPTIONAL
3157 | BGP_ATTR_FLAG_TRANS
3158 | BGP_ATTR_FLAG_EXTLEN);
3159 stream_putc(s,
3160 BGP_ATTR_EXT_COMMUNITIES);
3161 stream_putw(s, ecom_tr_size * 8);
3162 } else {
3163 stream_putc(
3164 s,
3165 BGP_ATTR_FLAG_OPTIONAL
3166 | BGP_ATTR_FLAG_TRANS);
3167 stream_putc(s,
3168 BGP_ATTR_EXT_COMMUNITIES);
3169 stream_putc(s, ecom_tr_size * 8);
3170 }
3171
3172 for (i = 0; i < attr->ecommunity->size; i++) {
3173 pnt = attr->ecommunity->val + (i * 8);
3174 tbit = *pnt;
3175
3176 if (CHECK_FLAG(
3177 tbit,
3178 ECOMMUNITY_FLAG_NON_TRANSITIVE))
3179 continue;
3180
3181 stream_put(s, pnt, 8);
3182 }
3183 }
3184 }
3185 }
3186
3187 /* Label index attribute. */
3188 if (safi == SAFI_LABELED_UNICAST) {
3189 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID)) {
3190 u_int32_t label_index;
3191
3192 label_index = attr->label_index;
3193
3194 if (label_index != BGP_INVALID_LABEL_INDEX) {
3195 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3196 | BGP_ATTR_FLAG_TRANS);
3197 stream_putc(s, BGP_ATTR_PREFIX_SID);
3198 stream_putc(s, 10);
3199 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX);
3200 stream_putw(s,
3201 BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
3202 stream_putc(s, 0); // reserved
3203 stream_putw(s, 0); // flags
3204 stream_putl(s, label_index);
3205 }
3206 }
3207 }
3208
3209 if (send_as4_path) {
3210 /* If the peer is NOT As4 capable, AND */
3211 /* there are ASnums > 65535 in path THEN
3212 * give out AS4_PATH */
3213
3214 /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
3215 * path segments!
3216 * Hm, I wonder... confederation things *should* only be at
3217 * the beginning of an aspath, right? Then we should use
3218 * aspath_delete_confed_seq for this, because it is already
3219 * there! (JK)
3220 * Folks, talk to me: what is reasonable here!?
3221 */
3222 aspath = aspath_delete_confed_seq(aspath);
3223
3224 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL
3225 | BGP_ATTR_FLAG_EXTLEN);
3226 stream_putc(s, BGP_ATTR_AS4_PATH);
3227 aspath_sizep = stream_get_endp(s);
3228 stream_putw(s, 0);
3229 stream_putw_at(s, aspath_sizep, aspath_put(s, aspath, 1));
3230 }
3231
3232 if (aspath != attr->aspath)
3233 aspath_free(aspath);
3234
3235 if (send_as4_aggregator) {
3236 /* send AS4_AGGREGATOR, at this place */
3237 /* this section of code moved here in order to ensure the
3238 * correct
3239 * *ascending* order of attributes
3240 */
3241 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3242 stream_putc(s, BGP_ATTR_AS4_AGGREGATOR);
3243 stream_putc(s, 8);
3244 stream_putl(s, attr->aggregator_as);
3245 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3246 }
3247
3248 if (((afi == AFI_IP || afi == AFI_IP6)
3249 && (safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN))
3250 || (afi == AFI_L2VPN && safi == SAFI_EVPN)) {
3251 /* Tunnel Encap attribute */
3252 bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_ENCAP);
3253
3254 #if ENABLE_BGP_VNC
3255 /* VNC attribute */
3256 bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_VNC);
3257 #endif
3258 }
3259
3260 /* PMSI Tunnel */
3261 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)) {
3262 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3263 stream_putc(s, BGP_ATTR_PMSI_TUNNEL);
3264 stream_putc(s, 9); // Length
3265 stream_putc(s, 0); // Flags
3266 stream_putc(s, 6); // Tunnel type: Ingress Replication (6)
3267 stream_put(s, &(attr->label), BGP_LABEL_BYTES); // MPLS Label / VXLAN VNI
3268 stream_put_ipv4(s, attr->nexthop.s_addr); // Unicast tunnel endpoint IP address
3269 }
3270
3271 /* Unknown transit attribute. */
3272 if (attr->transit)
3273 stream_put(s, attr->transit->val, attr->transit->length);
3274
3275 /* Return total size of attribute. */
3276 return stream_get_endp(s) - cp;
3277 }
3278
3279 size_t bgp_packet_mpunreach_start(struct stream *s, afi_t afi, safi_t safi)
3280 {
3281 unsigned long attrlen_pnt;
3282 iana_afi_t pkt_afi;
3283 iana_safi_t pkt_safi;
3284
3285 /* Set extended bit always to encode the attribute length as 2 bytes */
3286 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_EXTLEN);
3287 stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
3288
3289 attrlen_pnt = stream_get_endp(s);
3290 stream_putw(s, 0); /* Length of this attribute. */
3291
3292 /* Convert AFI, SAFI to values for packet. */
3293 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
3294
3295 stream_putw(s, pkt_afi);
3296 stream_putc(s, pkt_safi);
3297
3298 return attrlen_pnt;
3299 }
3300
3301 void bgp_packet_mpunreach_prefix(struct stream *s, struct prefix *p, afi_t afi,
3302 safi_t safi, struct prefix_rd *prd,
3303 mpls_label_t *label, u_int32_t num_labels,
3304 int addpath_encode, u_int32_t addpath_tx_id,
3305 struct attr *attr)
3306 {
3307 u_char wlabel[3] = {0x80, 0x00, 0x00};
3308
3309 if (safi == SAFI_LABELED_UNICAST) {
3310 label = (mpls_label_t *)wlabel;
3311 num_labels = 1;
3312 }
3313
3314 return bgp_packet_mpattr_prefix(s, afi, safi, p, prd,
3315 label, num_labels,
3316 addpath_encode, addpath_tx_id, attr);
3317 }
3318
3319 void bgp_packet_mpunreach_end(struct stream *s, size_t attrlen_pnt)
3320 {
3321 bgp_packet_mpattr_end(s, attrlen_pnt);
3322 }
3323
3324 /* Initialization of attribute. */
3325 void bgp_attr_init(void)
3326 {
3327 aspath_init();
3328 attrhash_init();
3329 community_init();
3330 ecommunity_init();
3331 lcommunity_init();
3332 cluster_init();
3333 transit_init();
3334 encap_init();
3335 }
3336
3337 void bgp_attr_finish(void)
3338 {
3339 aspath_finish();
3340 attrhash_finish();
3341 community_finish();
3342 ecommunity_finish();
3343 lcommunity_finish();
3344 cluster_finish();
3345 transit_finish();
3346 encap_finish();
3347 }
3348
3349 /* Make attribute packet. */
3350 void bgp_dump_routes_attr(struct stream *s, struct attr *attr,
3351 struct prefix *prefix)
3352 {
3353 unsigned long cp;
3354 unsigned long len;
3355 size_t aspath_lenp;
3356 struct aspath *aspath;
3357 int addpath_encode = 0;
3358 u_int32_t addpath_tx_id = 0;
3359
3360 /* Remember current pointer. */
3361 cp = stream_get_endp(s);
3362
3363 /* Place holder of length. */
3364 stream_putw(s, 0);
3365
3366 /* Origin attribute. */
3367 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3368 stream_putc(s, BGP_ATTR_ORIGIN);
3369 stream_putc(s, 1);
3370 stream_putc(s, attr->origin);
3371
3372 aspath = attr->aspath;
3373
3374 stream_putc(s, BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_EXTLEN);
3375 stream_putc(s, BGP_ATTR_AS_PATH);
3376 aspath_lenp = stream_get_endp(s);
3377 stream_putw(s, 0);
3378
3379 stream_putw_at(s, aspath_lenp, aspath_put(s, aspath, 1));
3380
3381 /* Nexthop attribute. */
3382 /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
3383 if (prefix != NULL && prefix->family != AF_INET6) {
3384 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3385 stream_putc(s, BGP_ATTR_NEXT_HOP);
3386 stream_putc(s, 4);
3387 stream_put_ipv4(s, attr->nexthop.s_addr);
3388 }
3389
3390 /* MED attribute. */
3391 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
3392 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3393 stream_putc(s, BGP_ATTR_MULTI_EXIT_DISC);
3394 stream_putc(s, 4);
3395 stream_putl(s, attr->med);
3396 }
3397
3398 /* Local preference. */
3399 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
3400 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3401 stream_putc(s, BGP_ATTR_LOCAL_PREF);
3402 stream_putc(s, 4);
3403 stream_putl(s, attr->local_pref);
3404 }
3405
3406 /* Atomic aggregate. */
3407 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
3408 stream_putc(s, BGP_ATTR_FLAG_TRANS);
3409 stream_putc(s, BGP_ATTR_ATOMIC_AGGREGATE);
3410 stream_putc(s, 0);
3411 }
3412
3413 /* Aggregator. */
3414 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR)) {
3415 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS);
3416 stream_putc(s, BGP_ATTR_AGGREGATOR);
3417 stream_putc(s, 8);
3418 stream_putl(s, attr->aggregator_as);
3419 stream_put_ipv4(s, attr->aggregator_addr.s_addr);
3420 }
3421
3422 /* Community attribute. */
3423 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
3424 if (attr->community->size * 4 > 255) {
3425 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3426 | BGP_ATTR_FLAG_TRANS
3427 | BGP_ATTR_FLAG_EXTLEN);
3428 stream_putc(s, BGP_ATTR_COMMUNITIES);
3429 stream_putw(s, attr->community->size * 4);
3430 } else {
3431 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3432 | BGP_ATTR_FLAG_TRANS);
3433 stream_putc(s, BGP_ATTR_COMMUNITIES);
3434 stream_putc(s, attr->community->size * 4);
3435 }
3436 stream_put(s, attr->community->val, attr->community->size * 4);
3437 }
3438
3439 /* Large Community attribute. */
3440 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) {
3441 if (lcom_length(attr->lcommunity) > 255) {
3442 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3443 | BGP_ATTR_FLAG_TRANS
3444 | BGP_ATTR_FLAG_EXTLEN);
3445 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3446 stream_putw(s, lcom_length(attr->lcommunity));
3447 } else {
3448 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3449 | BGP_ATTR_FLAG_TRANS);
3450 stream_putc(s, BGP_ATTR_LARGE_COMMUNITIES);
3451 stream_putc(s, lcom_length(attr->lcommunity));
3452 }
3453
3454 stream_put(s, attr->lcommunity->val, lcom_length(attr->lcommunity));
3455 }
3456
3457 /* Add a MP_NLRI attribute to dump the IPv6 next hop */
3458 if (prefix != NULL && prefix->family == AF_INET6
3459 && (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
3460 || attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)) {
3461 int sizep;
3462
3463 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
3464 stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
3465 sizep = stream_get_endp(s);
3466
3467 /* MP header */
3468 stream_putc(s, 0); /* Marker: Attribute length. */
3469 stream_putw(s, AFI_IP6); /* AFI */
3470 stream_putc(s, SAFI_UNICAST); /* SAFI */
3471
3472 /* Next hop */
3473 stream_putc(s, attr->mp_nexthop_len);
3474 stream_put(s, &attr->mp_nexthop_global, IPV6_MAX_BYTELEN);
3475 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3476 stream_put(s, &attr->mp_nexthop_local,
3477 IPV6_MAX_BYTELEN);
3478
3479 /* SNPA */
3480 stream_putc(s, 0);
3481
3482 /* Prefix */
3483 stream_put_prefix_addpath(s, prefix, addpath_encode,
3484 addpath_tx_id);
3485
3486 /* Set MP attribute length. */
3487 stream_putc_at(s, sizep, (stream_get_endp(s) - sizep) - 1);
3488 }
3489
3490 /* Prefix SID */
3491 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID)) {
3492 if (attr->label_index != BGP_INVALID_LABEL_INDEX) {
3493 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL
3494 | BGP_ATTR_FLAG_TRANS);
3495 stream_putc(s, BGP_ATTR_PREFIX_SID);
3496 stream_putc(s, 10);
3497 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX);
3498 stream_putc(s, BGP_PREFIX_SID_LABEL_INDEX_LENGTH);
3499 stream_putc(s, 0); // reserved
3500 stream_putw(s, 0); // flags
3501 stream_putl(s, attr->label_index);
3502 }
3503 }
3504
3505 /* Return total size of attribute. */
3506 len = stream_get_endp(s) - cp - 2;
3507 stream_putw_at(s, cp, len);
3508 }