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