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