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