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