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