]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_route.c
Merge pull request #3172 from opensourcerouting/bfd-mhop-lookup
[mirror_frr.git] / bgpd / bgp_route.c
CommitLineData
718e3744 1/* BGP routing information
896014f4
DL
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2016 Job Snijders <job@instituut.net>
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
718e3744 21
22#include <zebra.h>
8d0ab76d 23#include <math.h>
718e3744 24
25#include "prefix.h"
26#include "linklist.h"
27#include "memory.h"
28#include "command.h"
29#include "stream.h"
30#include "filter.h"
718e3744 31#include "log.h"
32#include "routemap.h"
33#include "buffer.h"
34#include "sockunion.h"
35#include "plist.h"
36#include "thread.h"
200df115 37#include "workqueue.h"
3f9c7369 38#include "queue.h"
6e919709 39#include "memory.h"
4dcadbef 40#include "lib/json.h"
d8d084d9 41#include "lib_errors.h"
718e3744 42
43#include "bgpd/bgpd.h"
44#include "bgpd/bgp_table.h"
45#include "bgpd/bgp_route.h"
46#include "bgpd/bgp_attr.h"
47#include "bgpd/bgp_debug.h"
14454c9f 48#include "bgpd/bgp_errors.h"
718e3744 49#include "bgpd/bgp_aspath.h"
50#include "bgpd/bgp_regex.h"
51#include "bgpd/bgp_community.h"
52#include "bgpd/bgp_ecommunity.h"
57d187bc 53#include "bgpd/bgp_lcommunity.h"
718e3744 54#include "bgpd/bgp_clist.h"
55#include "bgpd/bgp_packet.h"
56#include "bgpd/bgp_filter.h"
57#include "bgpd/bgp_fsm.h"
58#include "bgpd/bgp_mplsvpn.h"
59#include "bgpd/bgp_nexthop.h"
60#include "bgpd/bgp_damp.h"
61#include "bgpd/bgp_advertise.h"
62#include "bgpd/bgp_zebra.h"
0a486e5f 63#include "bgpd/bgp_vty.h"
96450faf 64#include "bgpd/bgp_mpath.h"
fc9a856f 65#include "bgpd/bgp_nht.h"
3f9c7369 66#include "bgpd/bgp_updgrp.h"
cd1964ff 67#include "bgpd/bgp_label.h"
718e3744 68
65efcfce 69#if ENABLE_BGP_VNC
f8b6f499
LB
70#include "bgpd/rfapi/rfapi_backend.h"
71#include "bgpd/rfapi/vnc_import_bgp.h"
72#include "bgpd/rfapi/vnc_export_bgp.h"
65efcfce 73#endif
aee875b5
PG
74#include "bgpd/bgp_encap_types.h"
75#include "bgpd/bgp_encap_tlv.h"
684a7227 76#include "bgpd/bgp_evpn.h"
3da6fcd5 77#include "bgpd/bgp_evpn_vty.h"
dba3c1d3 78#include "bgpd/bgp_flowspec.h"
98a9dbc7 79#include "bgpd/bgp_flowspec_util.h"
45918cfb 80#include "bgpd/bgp_pbr.h"
65efcfce 81
e2a86ad9
DS
82#ifndef VTYSH_EXTRACT_PL
83#include "bgpd/bgp_route_clippy.c"
84#endif
aee875b5 85
718e3744 86/* Extern from bgp_dump.c */
dde72586
SH
87extern const char *bgp_origin_str[];
88extern const char *bgp_origin_long_str[];
6b0655a2 89
b7d08f5a 90/* PMSI strings. */
91#define PMSI_TNLTYPE_STR_NO_INFO "No info"
92#define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
93static const struct message bgp_pmsi_tnltype_str[] = {
94 {PMSI_TNLTYPE_NO_INFO, PMSI_TNLTYPE_STR_NO_INFO},
95 {PMSI_TNLTYPE_RSVP_TE_P2MP, "RSVP-TE P2MP"},
96 {PMSI_TNLTYPE_MLDP_P2MP, "mLDP P2MP"},
97 {PMSI_TNLTYPE_PIM_SSM, "PIM-SSM"},
98 {PMSI_TNLTYPE_PIM_SM, "PIM-SM"},
99 {PMSI_TNLTYPE_PIM_BIDIR, "PIM-BIDIR"},
100 {PMSI_TNLTYPE_INGR_REPL, "Ingress Replication"},
101 {PMSI_TNLTYPE_MLDP_MP2MP, "mLDP MP2MP"},
88fbc36f 102 {0}
103};
b7d08f5a 104
9df8b37c
PZ
105#define VRFID_NONE_STR "-"
106
d62a17ae 107struct bgp_node *bgp_afi_node_get(struct bgp_table *table, afi_t afi,
108 safi_t safi, struct prefix *p,
109 struct prefix_rd *prd)
110{
111 struct bgp_node *rn;
112 struct bgp_node *prn = NULL;
113
114 assert(table);
115 if (!table)
116 return NULL;
117
118 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
119 || (safi == SAFI_EVPN)) {
120 prn = bgp_node_get(table, (struct prefix *)prd);
121
122 if (prn->info == NULL)
960035b2 123 prn->info = bgp_table_init(table->bgp, afi, safi);
d62a17ae 124 else
125 bgp_unlock_node(prn);
126 table = prn->info;
127 }
718e3744 128
d62a17ae 129 rn = bgp_node_get(table, p);
718e3744 130
d62a17ae 131 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
132 || (safi == SAFI_EVPN))
133 rn->prn = prn;
718e3744 134
d62a17ae 135 return rn;
718e3744 136}
6b0655a2 137
d62a17ae 138struct bgp_node *bgp_afi_node_lookup(struct bgp_table *table, afi_t afi,
139 safi_t safi, struct prefix *p,
140 struct prefix_rd *prd)
128ea8ab 141{
d62a17ae 142 struct bgp_node *rn;
143 struct bgp_node *prn = NULL;
128ea8ab 144
d62a17ae 145 if (!table)
146 return NULL;
128ea8ab 147
d62a17ae 148 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
149 || (safi == SAFI_EVPN)) {
150 prn = bgp_node_lookup(table, (struct prefix *)prd);
151 if (!prn)
152 return NULL;
128ea8ab 153
d62a17ae 154 if (prn->info == NULL) {
155 bgp_unlock_node(prn);
156 return NULL;
157 }
128ea8ab 158
d62a17ae 159 table = prn->info;
160 }
128ea8ab 161
d62a17ae 162 rn = bgp_node_lookup(table, p);
128ea8ab 163
d62a17ae 164 return rn;
128ea8ab 165}
166
18ee8310
DS
167/* Allocate bgp_path_info_extra */
168static struct bgp_path_info_extra *bgp_path_info_extra_new(void)
fb982c25 169{
4b7e6066
DS
170 struct bgp_path_info_extra *new;
171 new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA,
172 sizeof(struct bgp_path_info_extra));
b57ba6d2
MK
173 new->label[0] = MPLS_INVALID_LABEL;
174 new->num_labels = 0;
d62a17ae 175 return new;
fb982c25
PJ
176}
177
18ee8310 178static void bgp_path_info_extra_free(struct bgp_path_info_extra **extra)
fb982c25 179{
4b7e6066 180 struct bgp_path_info_extra *e;
d62a17ae 181
c93a3b77
DS
182 if (!extra || !*extra)
183 return;
d62a17ae 184
c93a3b77
DS
185 e = *extra;
186 if (e->damp_info)
187 bgp_damp_info_free(e->damp_info, 0);
d62a17ae 188
c93a3b77
DS
189 e->damp_info = NULL;
190 if (e->parent) {
40381db7 191 struct bgp_path_info *bpi = (struct bgp_path_info *)e->parent;
c93a3b77 192
40381db7 193 if (bpi->net) {
0e70e6c8
DL
194 /* FIXME: since multiple e may have the same e->parent
195 * and e->parent->net is holding a refcount for each
196 * of them, we need to do some fudging here.
197 *
40381db7
DS
198 * WARNING: if bpi->net->lock drops to 0, bpi may be
199 * freed as well (because bpi->net was holding the
200 * last reference to bpi) => write after free!
0e70e6c8
DL
201 */
202 unsigned refcount;
203
40381db7
DS
204 bpi = bgp_path_info_lock(bpi);
205 refcount = bpi->net->lock - 1;
206 bgp_unlock_node((struct bgp_node *)bpi->net);
0e70e6c8 207 if (!refcount)
40381db7
DS
208 bpi->net = NULL;
209 bgp_path_info_unlock(bpi);
0e70e6c8 210 }
18ee8310 211 bgp_path_info_unlock(e->parent);
c93a3b77 212 e->parent = NULL;
d62a17ae 213 }
c93a3b77
DS
214
215 if (e->bgp_orig)
216 bgp_unlock(e->bgp_orig);
c26edcda
PG
217
218 if ((*extra)->bgp_fs_pbr)
6a154c88 219 list_delete(&((*extra)->bgp_fs_pbr));
c93a3b77
DS
220 XFREE(MTYPE_BGP_ROUTE_EXTRA, *extra);
221
222 *extra = NULL;
fb982c25
PJ
223}
224
18ee8310
DS
225/* Get bgp_path_info extra information for the given bgp_path_info, lazy
226 * allocated if required.
fb982c25 227 */
40381db7 228struct bgp_path_info_extra *bgp_path_info_extra_get(struct bgp_path_info *pi)
fb982c25 229{
40381db7
DS
230 if (!pi->extra)
231 pi->extra = bgp_path_info_extra_new();
232 return pi->extra;
fb982c25
PJ
233}
234
65efcfce 235/* Allocate new bgp info structure. */
18ee8310 236struct bgp_path_info *bgp_path_info_new(void)
65efcfce 237{
4b7e6066 238 return XCALLOC(MTYPE_BGP_ROUTE, sizeof(struct bgp_path_info));
65efcfce
LB
239}
240
718e3744 241/* Free bgp route information. */
9b6d8fcf 242static void bgp_path_info_free(struct bgp_path_info *path)
718e3744 243{
9b6d8fcf
DS
244 if (path->attr)
245 bgp_attr_unintern(&path->attr);
fb018d25 246
9b6d8fcf
DS
247 bgp_unlink_nexthop(path);
248 bgp_path_info_extra_free(&path->extra);
249 bgp_path_info_mpath_free(&path->mpath);
718e3744 250
9b6d8fcf 251 peer_unlock(path->peer); /* bgp_path_info peer reference */
200df115 252
9b6d8fcf 253 XFREE(MTYPE_BGP_ROUTE, path);
718e3744 254}
255
9b6d8fcf 256struct bgp_path_info *bgp_path_info_lock(struct bgp_path_info *path)
200df115 257{
9b6d8fcf
DS
258 path->lock++;
259 return path;
200df115 260}
261
9b6d8fcf 262struct bgp_path_info *bgp_path_info_unlock(struct bgp_path_info *path)
200df115 263{
9b6d8fcf
DS
264 assert(path && path->lock > 0);
265 path->lock--;
d62a17ae 266
9b6d8fcf 267 if (path->lock == 0) {
200df115 268#if 0
269 zlog_debug ("%s: unlocked and freeing", __func__);
270 zlog_backtrace (LOG_DEBUG);
271#endif
9b6d8fcf 272 bgp_path_info_free(path);
d62a17ae 273 return NULL;
274 }
200df115 275
276#if 0
9b6d8fcf 277 if (path->lock == 1)
200df115 278 {
279 zlog_debug ("%s: unlocked to 1", __func__);
280 zlog_backtrace (LOG_DEBUG);
281 }
282#endif
d62a17ae 283
9b6d8fcf 284 return path;
200df115 285}
286
40381db7 287void bgp_path_info_add(struct bgp_node *rn, struct bgp_path_info *pi)
718e3744 288{
4b7e6066 289 struct bgp_path_info *top;
718e3744 290
d62a17ae 291 top = rn->info;
292
40381db7
DS
293 pi->next = rn->info;
294 pi->prev = NULL;
d62a17ae 295 if (top)
40381db7
DS
296 top->prev = pi;
297 rn->info = pi;
d62a17ae 298
40381db7 299 bgp_path_info_lock(pi);
d62a17ae 300 bgp_lock_node(rn);
40381db7 301 peer_lock(pi->peer); /* bgp_path_info peer reference */
718e3744 302}
303
d62a17ae 304/* Do the actual removal of info from RIB, for use by bgp_process
b40d939b 305 completion callback *only* */
40381db7 306void bgp_path_info_reap(struct bgp_node *rn, struct bgp_path_info *pi)
718e3744 307{
40381db7
DS
308 if (pi->next)
309 pi->next->prev = pi->prev;
310 if (pi->prev)
311 pi->prev->next = pi->next;
d62a17ae 312 else
40381db7 313 rn->info = pi->next;
d62a17ae 314
40381db7
DS
315 bgp_path_info_mpath_dequeue(pi);
316 bgp_path_info_unlock(pi);
d62a17ae 317 bgp_unlock_node(rn);
718e3744 318}
319
40381db7 320void bgp_path_info_delete(struct bgp_node *rn, struct bgp_path_info *pi)
b40d939b 321{
40381db7 322 bgp_path_info_set_flag(rn, pi, BGP_PATH_REMOVED);
d62a17ae 323 /* set of previous already took care of pcount */
40381db7 324 UNSET_FLAG(pi->flags, BGP_PATH_VALID);
b40d939b 325}
326
18ee8310 327/* undo the effects of a previous call to bgp_path_info_delete; typically
8d45210e
AS
328 called when a route is deleted and then quickly re-added before the
329 deletion has been processed */
40381db7 330void bgp_path_info_restore(struct bgp_node *rn, struct bgp_path_info *pi)
8d45210e 331{
40381db7 332 bgp_path_info_unset_flag(rn, pi, BGP_PATH_REMOVED);
d62a17ae 333 /* unset of previous already took care of pcount */
40381db7 334 SET_FLAG(pi->flags, BGP_PATH_VALID);
8d45210e
AS
335}
336
d62a17ae 337/* Adjust pcount as required */
40381db7 338static void bgp_pcount_adjust(struct bgp_node *rn, struct bgp_path_info *pi)
1a392d46 339{
d62a17ae 340 struct bgp_table *table;
67174041 341
d62a17ae 342 assert(rn && bgp_node_table(rn));
40381db7 343 assert(pi && pi->peer && pi->peer->bgp);
6f58544d 344
d62a17ae 345 table = bgp_node_table(rn);
67174041 346
40381db7 347 if (pi->peer == pi->peer->bgp->peer_self)
d62a17ae 348 return;
349
40381db7
DS
350 if (!BGP_PATH_COUNTABLE(pi)
351 && CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
d62a17ae 352
40381db7 353 UNSET_FLAG(pi->flags, BGP_PATH_COUNTED);
d62a17ae 354
355 /* slight hack, but more robust against errors. */
40381db7
DS
356 if (pi->peer->pcount[table->afi][table->safi])
357 pi->peer->pcount[table->afi][table->safi]--;
d8d084d9 358 else
450971aa 359 flog_err(EC_LIB_DEVELOPMENT,
d8d084d9 360 "Asked to decrement 0 prefix count for peer");
40381db7
DS
361 } else if (BGP_PATH_COUNTABLE(pi)
362 && !CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
363 SET_FLAG(pi->flags, BGP_PATH_COUNTED);
364 pi->peer->pcount[table->afi][table->safi]++;
d62a17ae 365 }
1a392d46
PJ
366}
367
40381db7
DS
368static int bgp_label_index_differs(struct bgp_path_info *pi1,
369 struct bgp_path_info *pi2)
28d58fd7 370{
40381db7 371 return (!(pi1->attr->label_index == pi2->attr->label_index));
28d58fd7 372}
1a392d46 373
18ee8310 374/* Set/unset bgp_path_info flags, adjusting any other state as needed.
1a392d46
PJ
375 * This is here primarily to keep prefix-count in check.
376 */
40381db7 377void bgp_path_info_set_flag(struct bgp_node *rn, struct bgp_path_info *pi,
18ee8310 378 uint32_t flag)
1a392d46 379{
40381db7 380 SET_FLAG(pi->flags, flag);
d62a17ae 381
382 /* early bath if we know it's not a flag that changes countability state
383 */
384 if (!CHECK_FLAG(flag,
1defdda8 385 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
d62a17ae 386 return;
387
40381db7 388 bgp_pcount_adjust(rn, pi);
1a392d46
PJ
389}
390
40381db7 391void bgp_path_info_unset_flag(struct bgp_node *rn, struct bgp_path_info *pi,
18ee8310 392 uint32_t flag)
1a392d46 393{
40381db7 394 UNSET_FLAG(pi->flags, flag);
d62a17ae 395
396 /* early bath if we know it's not a flag that changes countability state
397 */
398 if (!CHECK_FLAG(flag,
1defdda8 399 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
d62a17ae 400 return;
401
40381db7 402 bgp_pcount_adjust(rn, pi);
1a392d46
PJ
403}
404
718e3744 405/* Get MED value. If MED value is missing and "bgp bestpath
406 missing-as-worst" is specified, treat it as the worst value. */
d7c0a89a 407static uint32_t bgp_med_value(struct attr *attr, struct bgp *bgp)
d62a17ae 408{
409 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
410 return attr->med;
411 else {
412 if (bgp_flag_check(bgp, BGP_FLAG_MED_MISSING_AS_WORST))
413 return BGP_MED_MAX;
414 else
415 return 0;
416 }
718e3744 417}
418
40381db7 419void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info *pi, char *buf)
2ec1e66f 420{
40381db7
DS
421 if (pi->addpath_rx_id)
422 sprintf(buf, "path %s (addpath rxid %d)", pi->peer->host,
423 pi->addpath_rx_id);
d62a17ae 424 else
40381db7 425 sprintf(buf, "path %s", pi->peer->host);
d62a17ae 426}
9fbdd100 427
d62a17ae 428/* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
429 */
18ee8310
DS
430static int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
431 struct bgp_path_info *exist, int *paths_eq,
432 struct bgp_maxpaths_cfg *mpath_cfg, int debug,
433 char *pfx_buf, afi_t afi, safi_t safi)
d62a17ae 434{
435 struct attr *newattr, *existattr;
436 bgp_peer_sort_t new_sort;
437 bgp_peer_sort_t exist_sort;
d7c0a89a
QY
438 uint32_t new_pref;
439 uint32_t exist_pref;
440 uint32_t new_med;
441 uint32_t exist_med;
442 uint32_t new_weight;
443 uint32_t exist_weight;
d62a17ae 444 uint32_t newm, existm;
445 struct in_addr new_id;
446 struct in_addr exist_id;
447 int new_cluster;
448 int exist_cluster;
449 int internal_as_route;
450 int confed_as_route;
04d14c8b 451 int ret = 0;
d62a17ae 452 char new_buf[PATH_ADDPATH_STR_BUFFER];
453 char exist_buf[PATH_ADDPATH_STR_BUFFER];
d7c0a89a
QY
454 uint32_t new_mm_seq;
455 uint32_t exist_mm_seq;
d62a17ae 456
457 *paths_eq = 0;
458
459 /* 0. Null check. */
460 if (new == NULL) {
461 if (debug)
462 zlog_debug("%s: new is NULL", pfx_buf);
463 return 0;
464 }
2ec1e66f 465
d62a17ae 466 if (debug)
18ee8310 467 bgp_path_info_path_with_addpath_rx_str(new, new_buf);
718e3744 468
d62a17ae 469 if (exist == NULL) {
470 if (debug)
471 zlog_debug("%s: %s is the initial bestpath", pfx_buf,
472 new_buf);
473 return 1;
474 }
2ec1e66f 475
d62a17ae 476 if (debug) {
18ee8310 477 bgp_path_info_path_with_addpath_rx_str(exist, exist_buf);
d62a17ae 478 zlog_debug("%s: Comparing %s flags 0x%x with %s flags 0x%x",
479 pfx_buf, new_buf, new->flags, exist_buf,
480 exist->flags);
481 }
8ff56318 482
d62a17ae 483 newattr = new->attr;
484 existattr = exist->attr;
485
486 /* For EVPN routes, we cannot just go by local vs remote, we have to
487 * look at the MAC mobility sequence number, if present.
488 */
489 if (safi == SAFI_EVPN) {
490 /* This is an error condition described in RFC 7432 Section
491 * 15.2. The RFC
492 * states that in this scenario "the PE MUST alert the operator"
493 * but it
494 * does not state what other action to take. In order to provide
495 * some
496 * consistency in this scenario we are going to prefer the path
497 * with the
498 * sticky flag.
499 */
500 if (newattr->sticky != existattr->sticky) {
501 if (!debug) {
502 prefix2str(&new->net->p, pfx_buf,
503 sizeof(*pfx_buf)
504 * PREFIX2STR_BUFFER);
18ee8310
DS
505 bgp_path_info_path_with_addpath_rx_str(new,
506 new_buf);
507 bgp_path_info_path_with_addpath_rx_str(
508 exist, exist_buf);
d62a17ae 509 }
510
511 if (newattr->sticky && !existattr->sticky) {
deff24ca
DS
512 if (debug)
513 zlog_debug(
514 "%s: %s wins over %s due to sticky MAC flag",
515 pfx_buf, new_buf, exist_buf);
d62a17ae 516 return 1;
517 }
518
519 if (!newattr->sticky && existattr->sticky) {
deff24ca
DS
520 if (debug)
521 zlog_debug(
522 "%s: %s loses to %s due to sticky MAC flag",
523 pfx_buf, new_buf, exist_buf);
d62a17ae 524 return 0;
525 }
526 }
128ea8ab 527
d62a17ae 528 new_mm_seq = mac_mobility_seqnum(newattr);
529 exist_mm_seq = mac_mobility_seqnum(existattr);
8ff56318 530
d62a17ae 531 if (new_mm_seq > exist_mm_seq) {
532 if (debug)
533 zlog_debug(
534 "%s: %s wins over %s due to MM seq %u > %u",
535 pfx_buf, new_buf, exist_buf, new_mm_seq,
536 exist_mm_seq);
537 return 1;
538 }
8ff56318 539
d62a17ae 540 if (new_mm_seq < exist_mm_seq) {
541 if (debug)
542 zlog_debug(
543 "%s: %s loses to %s due to MM seq %u < %u",
544 pfx_buf, new_buf, exist_buf, new_mm_seq,
545 exist_mm_seq);
546 return 0;
547 }
548 }
9fbdd100 549
d62a17ae 550 /* 1. Weight check. */
d62a17ae 551 new_weight = newattr->weight;
552 exist_weight = existattr->weight;
8ff56318 553
d62a17ae 554 if (new_weight > exist_weight) {
555 if (debug)
556 zlog_debug("%s: %s wins over %s due to weight %d > %d",
557 pfx_buf, new_buf, exist_buf, new_weight,
558 exist_weight);
559 return 1;
560 }
718e3744 561
d62a17ae 562 if (new_weight < exist_weight) {
563 if (debug)
564 zlog_debug("%s: %s loses to %s due to weight %d < %d",
565 pfx_buf, new_buf, exist_buf, new_weight,
566 exist_weight);
567 return 0;
568 }
9fbdd100 569
d62a17ae 570 /* 2. Local preference check. */
571 new_pref = exist_pref = bgp->default_local_pref;
572
573 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
574 new_pref = newattr->local_pref;
575 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
576 exist_pref = existattr->local_pref;
577
578 if (new_pref > exist_pref) {
579 if (debug)
580 zlog_debug(
581 "%s: %s wins over %s due to localpref %d > %d",
582 pfx_buf, new_buf, exist_buf, new_pref,
583 exist_pref);
584 return 1;
585 }
718e3744 586
d62a17ae 587 if (new_pref < exist_pref) {
588 if (debug)
589 zlog_debug(
590 "%s: %s loses to %s due to localpref %d < %d",
591 pfx_buf, new_buf, exist_buf, new_pref,
592 exist_pref);
593 return 0;
594 }
9fbdd100 595
d62a17ae 596 /* 3. Local route check. We prefer:
597 * - BGP_ROUTE_STATIC
598 * - BGP_ROUTE_AGGREGATE
599 * - BGP_ROUTE_REDISTRIBUTE
600 */
90f4f482 601 if (!(new->sub_type == BGP_ROUTE_NORMAL ||
602 new->sub_type == BGP_ROUTE_IMPORTED)) {
d62a17ae 603 if (debug)
604 zlog_debug(
605 "%s: %s wins over %s due to preferred BGP_ROUTE type",
606 pfx_buf, new_buf, exist_buf);
607 return 1;
608 }
718e3744 609
90f4f482 610 if (!(exist->sub_type == BGP_ROUTE_NORMAL ||
88493076 611 exist->sub_type == BGP_ROUTE_IMPORTED)) {
d62a17ae 612 if (debug)
613 zlog_debug(
614 "%s: %s loses to %s due to preferred BGP_ROUTE type",
615 pfx_buf, new_buf, exist_buf);
616 return 0;
6811845b 617 }
718e3744 618
d62a17ae 619 /* 4. AS path length check. */
620 if (!bgp_flag_check(bgp, BGP_FLAG_ASPATH_IGNORE)) {
621 int exist_hops = aspath_count_hops(existattr->aspath);
622 int exist_confeds = aspath_count_confeds(existattr->aspath);
623
624 if (bgp_flag_check(bgp, BGP_FLAG_ASPATH_CONFED)) {
625 int aspath_hops;
626
627 aspath_hops = aspath_count_hops(newattr->aspath);
628 aspath_hops += aspath_count_confeds(newattr->aspath);
629
630 if (aspath_hops < (exist_hops + exist_confeds)) {
631 if (debug)
632 zlog_debug(
633 "%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
634 pfx_buf, new_buf, exist_buf,
635 aspath_hops,
636 (exist_hops + exist_confeds));
637 return 1;
638 }
639
640 if (aspath_hops > (exist_hops + exist_confeds)) {
641 if (debug)
642 zlog_debug(
643 "%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
644 pfx_buf, new_buf, exist_buf,
645 aspath_hops,
646 (exist_hops + exist_confeds));
647 return 0;
648 }
649 } else {
650 int newhops = aspath_count_hops(newattr->aspath);
651
652 if (newhops < exist_hops) {
653 if (debug)
654 zlog_debug(
655 "%s: %s wins over %s due to aspath hopcount %d < %d",
656 pfx_buf, new_buf, exist_buf,
657 newhops, exist_hops);
658 return 1;
659 }
660
661 if (newhops > exist_hops) {
662 if (debug)
663 zlog_debug(
664 "%s: %s loses to %s due to aspath hopcount %d > %d",
665 pfx_buf, new_buf, exist_buf,
666 newhops, exist_hops);
667 return 0;
668 }
669 }
670 }
9fbdd100 671
d62a17ae 672 /* 5. Origin check. */
673 if (newattr->origin < existattr->origin) {
674 if (debug)
675 zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
676 pfx_buf, new_buf, exist_buf,
677 bgp_origin_long_str[newattr->origin],
678 bgp_origin_long_str[existattr->origin]);
679 return 1;
680 }
718e3744 681
d62a17ae 682 if (newattr->origin > existattr->origin) {
683 if (debug)
684 zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
685 pfx_buf, new_buf, exist_buf,
686 bgp_origin_long_str[newattr->origin],
687 bgp_origin_long_str[existattr->origin]);
688 return 0;
689 }
718e3744 690
d62a17ae 691 /* 6. MED check. */
692 internal_as_route = (aspath_count_hops(newattr->aspath) == 0
693 && aspath_count_hops(existattr->aspath) == 0);
694 confed_as_route = (aspath_count_confeds(newattr->aspath) > 0
695 && aspath_count_confeds(existattr->aspath) > 0
696 && aspath_count_hops(newattr->aspath) == 0
697 && aspath_count_hops(existattr->aspath) == 0);
698
699 if (bgp_flag_check(bgp, BGP_FLAG_ALWAYS_COMPARE_MED)
700 || (bgp_flag_check(bgp, BGP_FLAG_MED_CONFED) && confed_as_route)
701 || aspath_cmp_left(newattr->aspath, existattr->aspath)
702 || aspath_cmp_left_confed(newattr->aspath, existattr->aspath)
703 || internal_as_route) {
704 new_med = bgp_med_value(new->attr, bgp);
705 exist_med = bgp_med_value(exist->attr, bgp);
706
707 if (new_med < exist_med) {
708 if (debug)
709 zlog_debug(
710 "%s: %s wins over %s due to MED %d < %d",
711 pfx_buf, new_buf, exist_buf, new_med,
712 exist_med);
713 return 1;
714 }
8ff56318 715
d62a17ae 716 if (new_med > exist_med) {
717 if (debug)
718 zlog_debug(
719 "%s: %s loses to %s due to MED %d > %d",
720 pfx_buf, new_buf, exist_buf, new_med,
721 exist_med);
722 return 0;
723 }
724 }
9fbdd100 725
d62a17ae 726 /* 7. Peer type check. */
727 new_sort = new->peer->sort;
728 exist_sort = exist->peer->sort;
729
730 if (new_sort == BGP_PEER_EBGP
731 && (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED)) {
732 if (debug)
733 zlog_debug(
734 "%s: %s wins over %s due to eBGP peer > iBGP peer",
735 pfx_buf, new_buf, exist_buf);
736 return 1;
737 }
718e3744 738
d62a17ae 739 if (exist_sort == BGP_PEER_EBGP
740 && (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED)) {
741 if (debug)
742 zlog_debug(
743 "%s: %s loses to %s due to iBGP peer < eBGP peer",
744 pfx_buf, new_buf, exist_buf);
745 return 0;
746 }
8ff56318 747
d62a17ae 748 /* 8. IGP metric check. */
749 newm = existm = 0;
8ff56318 750
d62a17ae 751 if (new->extra)
752 newm = new->extra->igpmetric;
753 if (exist->extra)
754 existm = exist->extra->igpmetric;
9fbdd100 755
d62a17ae 756 if (newm < existm) {
757 if (debug)
758 zlog_debug(
759 "%s: %s wins over %s due to IGP metric %d < %d",
760 pfx_buf, new_buf, exist_buf, newm, existm);
761 ret = 1;
762 }
718e3744 763
d62a17ae 764 if (newm > existm) {
765 if (debug)
766 zlog_debug(
767 "%s: %s loses to %s due to IGP metric %d > %d",
768 pfx_buf, new_buf, exist_buf, newm, existm);
769 ret = 0;
5e242b0d 770 }
5e242b0d 771
d62a17ae 772 /* 9. Same IGP metric. Compare the cluster list length as
773 representative of IGP hops metric. Rewrite the metric value
774 pair (newm, existm) with the cluster list length. Prefer the
775 path with smaller cluster list length. */
776 if (newm == existm) {
777 if (peer_sort(new->peer) == BGP_PEER_IBGP
778 && peer_sort(exist->peer) == BGP_PEER_IBGP
779 && (mpath_cfg == NULL
780 || CHECK_FLAG(
781 mpath_cfg->ibgp_flags,
782 BGP_FLAG_IBGP_MULTIPATH_SAME_CLUSTERLEN))) {
783 newm = BGP_CLUSTER_LIST_LENGTH(new->attr);
784 existm = BGP_CLUSTER_LIST_LENGTH(exist->attr);
785
786 if (newm < existm) {
787 if (debug)
788 zlog_debug(
789 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
790 pfx_buf, new_buf, exist_buf,
791 newm, existm);
792 ret = 1;
793 }
794
795 if (newm > existm) {
796 if (debug)
797 zlog_debug(
798 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
799 pfx_buf, new_buf, exist_buf,
800 newm, existm);
801 ret = 0;
802 }
803 }
804 }
31a4638f 805
d62a17ae 806 /* 10. confed-external vs. confed-internal */
807 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
808 if (new_sort == BGP_PEER_CONFED
809 && exist_sort == BGP_PEER_IBGP) {
810 if (debug)
811 zlog_debug(
812 "%s: %s wins over %s due to confed-external peer > confed-internal peer",
813 pfx_buf, new_buf, exist_buf);
814 return 1;
815 }
718e3744 816
d62a17ae 817 if (exist_sort == BGP_PEER_CONFED
818 && new_sort == BGP_PEER_IBGP) {
819 if (debug)
820 zlog_debug(
821 "%s: %s loses to %s due to confed-internal peer < confed-external peer",
822 pfx_buf, new_buf, exist_buf);
823 return 0;
824 }
825 }
718e3744 826
d62a17ae 827 /* 11. Maximum path check. */
828 if (newm == existm) {
829 /* If one path has a label but the other does not, do not treat
830 * them as equals for multipath
831 */
a4d82a8a 832 if ((new->extra &&bgp_is_valid_label(&new->extra->label[0]))
d62a17ae 833 != (exist->extra
b57ba6d2 834 && bgp_is_valid_label(&exist->extra->label[0]))) {
d62a17ae 835 if (debug)
836 zlog_debug(
837 "%s: %s and %s cannot be multipath, one has a label while the other does not",
838 pfx_buf, new_buf, exist_buf);
839 } else if (bgp_flag_check(bgp,
840 BGP_FLAG_ASPATH_MULTIPATH_RELAX)) {
841
842 /*
843 * For the two paths, all comparison steps till IGP
844 * metric
845 * have succeeded - including AS_PATH hop count. Since
846 * 'bgp
847 * bestpath as-path multipath-relax' knob is on, we
848 * don't need
849 * an exact match of AS_PATH. Thus, mark the paths are
850 * equal.
851 * That will trigger both these paths to get into the
852 * multipath
853 * array.
854 */
855 *paths_eq = 1;
856
857 if (debug)
858 zlog_debug(
859 "%s: %s and %s are equal via multipath-relax",
860 pfx_buf, new_buf, exist_buf);
861 } else if (new->peer->sort == BGP_PEER_IBGP) {
862 if (aspath_cmp(new->attr->aspath,
863 exist->attr->aspath)) {
864 *paths_eq = 1;
865
866 if (debug)
867 zlog_debug(
868 "%s: %s and %s are equal via matching aspaths",
869 pfx_buf, new_buf, exist_buf);
870 }
871 } else if (new->peer->as == exist->peer->as) {
872 *paths_eq = 1;
873
874 if (debug)
875 zlog_debug(
876 "%s: %s and %s are equal via same remote-as",
877 pfx_buf, new_buf, exist_buf);
878 }
879 } else {
880 /*
881 * TODO: If unequal cost ibgp multipath is enabled we can
882 * mark the paths as equal here instead of returning
883 */
884 if (debug) {
885 if (ret == 1)
886 zlog_debug(
887 "%s: %s wins over %s after IGP metric comparison",
888 pfx_buf, new_buf, exist_buf);
889 else
890 zlog_debug(
891 "%s: %s loses to %s after IGP metric comparison",
892 pfx_buf, new_buf, exist_buf);
893 }
894 return ret;
895 }
718e3744 896
d62a17ae 897 /* 12. If both paths are external, prefer the path that was received
898 first (the oldest one). This step minimizes route-flap, since a
899 newer path won't displace an older one, even if it was the
900 preferred route based on the additional decision criteria below. */
901 if (!bgp_flag_check(bgp, BGP_FLAG_COMPARE_ROUTER_ID)
902 && new_sort == BGP_PEER_EBGP && exist_sort == BGP_PEER_EBGP) {
1defdda8 903 if (CHECK_FLAG(new->flags, BGP_PATH_SELECTED)) {
d62a17ae 904 if (debug)
905 zlog_debug(
906 "%s: %s wins over %s due to oldest external",
907 pfx_buf, new_buf, exist_buf);
908 return 1;
909 }
9fbdd100 910
1defdda8 911 if (CHECK_FLAG(exist->flags, BGP_PATH_SELECTED)) {
d62a17ae 912 if (debug)
913 zlog_debug(
914 "%s: %s loses to %s due to oldest external",
915 pfx_buf, new_buf, exist_buf);
916 return 0;
917 }
918 }
718e3744 919
d62a17ae 920 /* 13. Router-ID comparision. */
921 /* If one of the paths is "stale", the corresponding peer router-id will
922 * be 0 and would always win over the other path. If originator id is
923 * used for the comparision, it will decide which path is better.
924 */
925 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
926 new_id.s_addr = newattr->originator_id.s_addr;
927 else
928 new_id.s_addr = new->peer->remote_id.s_addr;
929 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
930 exist_id.s_addr = existattr->originator_id.s_addr;
931 else
932 exist_id.s_addr = exist->peer->remote_id.s_addr;
933
934 if (ntohl(new_id.s_addr) < ntohl(exist_id.s_addr)) {
935 if (debug)
936 zlog_debug(
937 "%s: %s wins over %s due to Router-ID comparison",
938 pfx_buf, new_buf, exist_buf);
939 return 1;
940 }
718e3744 941
d62a17ae 942 if (ntohl(new_id.s_addr) > ntohl(exist_id.s_addr)) {
943 if (debug)
944 zlog_debug(
945 "%s: %s loses to %s due to Router-ID comparison",
946 pfx_buf, new_buf, exist_buf);
947 return 0;
948 }
9fbdd100 949
d62a17ae 950 /* 14. Cluster length comparision. */
951 new_cluster = BGP_CLUSTER_LIST_LENGTH(new->attr);
952 exist_cluster = BGP_CLUSTER_LIST_LENGTH(exist->attr);
953
954 if (new_cluster < exist_cluster) {
955 if (debug)
956 zlog_debug(
957 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
958 pfx_buf, new_buf, exist_buf, new_cluster,
959 exist_cluster);
960 return 1;
961 }
718e3744 962
d62a17ae 963 if (new_cluster > exist_cluster) {
964 if (debug)
965 zlog_debug(
966 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
967 pfx_buf, new_buf, exist_buf, new_cluster,
968 exist_cluster);
969 return 0;
970 }
9fbdd100 971
d62a17ae 972 /* 15. Neighbor address comparision. */
973 /* Do this only if neither path is "stale" as stale paths do not have
974 * valid peer information (as the connection may or may not be up).
975 */
1defdda8 976 if (CHECK_FLAG(exist->flags, BGP_PATH_STALE)) {
d62a17ae 977 if (debug)
978 zlog_debug(
979 "%s: %s wins over %s due to latter path being STALE",
980 pfx_buf, new_buf, exist_buf);
981 return 1;
982 }
0de5153c 983
1defdda8 984 if (CHECK_FLAG(new->flags, BGP_PATH_STALE)) {
d62a17ae 985 if (debug)
986 zlog_debug(
987 "%s: %s loses to %s due to former path being STALE",
988 pfx_buf, new_buf, exist_buf);
989 return 0;
990 }
718e3744 991
d62a17ae 992 /* locally configured routes to advertise do not have su_remote */
993 if (new->peer->su_remote == NULL)
994 return 0;
995 if (exist->peer->su_remote == NULL)
996 return 1;
9fbdd100 997
d62a17ae 998 ret = sockunion_cmp(new->peer->su_remote, exist->peer->su_remote);
999
1000 if (ret == 1) {
1001 if (debug)
1002 zlog_debug(
1003 "%s: %s loses to %s due to Neighor IP comparison",
1004 pfx_buf, new_buf, exist_buf);
1005 return 0;
1006 }
1007
1008 if (ret == -1) {
1009 if (debug)
1010 zlog_debug(
1011 "%s: %s wins over %s due to Neighor IP comparison",
1012 pfx_buf, new_buf, exist_buf);
1013 return 1;
1014 }
9fbdd100 1015
d62a17ae 1016 if (debug)
1017 zlog_debug("%s: %s wins over %s due to nothing left to compare",
1018 pfx_buf, new_buf, exist_buf);
718e3744 1019
d62a17ae 1020 return 1;
718e3744 1021}
1022
65efcfce
LB
1023/* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
1024 * is preferred, or 0 if they are the same (usually will only occur if
d62a17ae 1025 * multipath is enabled
65efcfce 1026 * This version is compatible with */
18ee8310
DS
1027int bgp_path_info_cmp_compatible(struct bgp *bgp, struct bgp_path_info *new,
1028 struct bgp_path_info *exist, char *pfx_buf,
1029 afi_t afi, safi_t safi)
d62a17ae 1030{
1031 int paths_eq;
1032 int ret;
18ee8310
DS
1033 ret = bgp_path_info_cmp(bgp, new, exist, &paths_eq, NULL, 0, pfx_buf,
1034 afi, safi);
d62a17ae 1035
1036 if (paths_eq)
1037 ret = 0;
1038 else {
1039 if (ret == 1)
1040 ret = -1;
1041 else
1042 ret = 1;
1043 }
1044 return ret;
65efcfce
LB
1045}
1046
d62a17ae 1047static enum filter_type bgp_input_filter(struct peer *peer, struct prefix *p,
1048 struct attr *attr, afi_t afi,
1049 safi_t safi)
718e3744 1050{
d62a17ae 1051 struct bgp_filter *filter;
718e3744 1052
d62a17ae 1053 filter = &peer->filter[afi][safi];
718e3744 1054
d62a17ae 1055#define FILTER_EXIST_WARN(F, f, filter) \
1056 if (BGP_DEBUG(update, UPDATE_IN) && !(F##_IN(filter))) \
d8d084d9
DS
1057 zlog_debug("%s: Could not find configured input %s-list %s!", \
1058 peer->host, #f, F##_IN_NAME(filter));
d62a17ae 1059
1060 if (DISTRIBUTE_IN_NAME(filter)) {
1061 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
1062
1063 if (access_list_apply(DISTRIBUTE_IN(filter), p) == FILTER_DENY)
1064 return FILTER_DENY;
1065 }
1066
1067 if (PREFIX_LIST_IN_NAME(filter)) {
1068 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
1069
1070 if (prefix_list_apply(PREFIX_LIST_IN(filter), p) == PREFIX_DENY)
1071 return FILTER_DENY;
1072 }
1073
1074 if (FILTER_LIST_IN_NAME(filter)) {
1075 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
1076
1077 if (as_list_apply(FILTER_LIST_IN(filter), attr->aspath)
1078 == AS_FILTER_DENY)
1079 return FILTER_DENY;
1080 }
1081
1082 return FILTER_PERMIT;
650f76c2 1083#undef FILTER_EXIST_WARN
718e3744 1084}
1085
d62a17ae 1086static enum filter_type bgp_output_filter(struct peer *peer, struct prefix *p,
1087 struct attr *attr, afi_t afi,
1088 safi_t safi)
718e3744 1089{
d62a17ae 1090 struct bgp_filter *filter;
1091
1092 filter = &peer->filter[afi][safi];
1093
1094#define FILTER_EXIST_WARN(F, f, filter) \
1095 if (BGP_DEBUG(update, UPDATE_OUT) && !(F##_OUT(filter))) \
d8d084d9
DS
1096 zlog_debug("%s: Could not find configured output %s-list %s!", \
1097 peer->host, #f, F##_OUT_NAME(filter));
718e3744 1098
d62a17ae 1099 if (DISTRIBUTE_OUT_NAME(filter)) {
1100 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
718e3744 1101
d62a17ae 1102 if (access_list_apply(DISTRIBUTE_OUT(filter), p) == FILTER_DENY)
1103 return FILTER_DENY;
1104 }
1105
1106 if (PREFIX_LIST_OUT_NAME(filter)) {
1107 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
650f76c2 1108
d62a17ae 1109 if (prefix_list_apply(PREFIX_LIST_OUT(filter), p)
1110 == PREFIX_DENY)
1111 return FILTER_DENY;
1112 }
718e3744 1113
d62a17ae 1114 if (FILTER_LIST_OUT_NAME(filter)) {
1115 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
718e3744 1116
d62a17ae 1117 if (as_list_apply(FILTER_LIST_OUT(filter), attr->aspath)
1118 == AS_FILTER_DENY)
1119 return FILTER_DENY;
1120 }
718e3744 1121
d62a17ae 1122 return FILTER_PERMIT;
650f76c2 1123#undef FILTER_EXIST_WARN
718e3744 1124}
1125
1126/* If community attribute includes no_export then return 1. */
d62a17ae 1127static int bgp_community_filter(struct peer *peer, struct attr *attr)
1128{
1129 if (attr->community) {
1130 /* NO_ADVERTISE check. */
1131 if (community_include(attr->community, COMMUNITY_NO_ADVERTISE))
1132 return 1;
1133
1134 /* NO_EXPORT check. */
1135 if (peer->sort == BGP_PEER_EBGP
1136 && community_include(attr->community, COMMUNITY_NO_EXPORT))
1137 return 1;
1138
1139 /* NO_EXPORT_SUBCONFED check. */
1140 if (peer->sort == BGP_PEER_EBGP
1141 || peer->sort == BGP_PEER_CONFED)
1142 if (community_include(attr->community,
1143 COMMUNITY_NO_EXPORT_SUBCONFED))
1144 return 1;
1145 }
1146 return 0;
718e3744 1147}
1148
1149/* Route reflection loop check. */
d62a17ae 1150static int bgp_cluster_filter(struct peer *peer, struct attr *attr)
718e3744 1151{
d62a17ae 1152 struct in_addr cluster_id;
718e3744 1153
d62a17ae 1154 if (attr->cluster) {
1155 if (peer->bgp->config & BGP_CONFIG_CLUSTER_ID)
1156 cluster_id = peer->bgp->cluster_id;
1157 else
1158 cluster_id = peer->bgp->router_id;
1159
1160 if (cluster_loop_check(attr->cluster, cluster_id))
1161 return 1;
1162 }
1163 return 0;
718e3744 1164}
6b0655a2 1165
d62a17ae 1166static int bgp_input_modifier(struct peer *peer, struct prefix *p,
1167 struct attr *attr, afi_t afi, safi_t safi,
1168 const char *rmap_name)
718e3744 1169{
d62a17ae 1170 struct bgp_filter *filter;
40381db7 1171 struct bgp_path_info rmap_path;
d62a17ae 1172 route_map_result_t ret;
1173 struct route_map *rmap = NULL;
718e3744 1174
d62a17ae 1175 filter = &peer->filter[afi][safi];
718e3744 1176
d62a17ae 1177 /* Apply default weight value. */
1178 if (peer->weight[afi][safi])
1179 attr->weight = peer->weight[afi][safi];
718e3744 1180
d62a17ae 1181 if (rmap_name) {
1182 rmap = route_map_lookup_by_name(rmap_name);
98a4a44e 1183
d62a17ae 1184 if (rmap == NULL)
1185 return RMAP_DENY;
1186 } else {
1187 if (ROUTE_MAP_IN_NAME(filter)) {
1188 rmap = ROUTE_MAP_IN(filter);
98a4a44e 1189
d62a17ae 1190 if (rmap == NULL)
1191 return RMAP_DENY;
1192 }
1193 }
0b16f239 1194
d62a17ae 1195 /* Route map apply. */
1196 if (rmap) {
40381db7 1197 memset(&rmap_path, 0, sizeof(struct bgp_path_info));
d62a17ae 1198 /* Duplicate current value to new strucutre for modification. */
40381db7
DS
1199 rmap_path.peer = peer;
1200 rmap_path.attr = attr;
718e3744 1201
d62a17ae 1202 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN);
ac41b2a2 1203
d62a17ae 1204 /* Apply BGP route map to the attribute. */
40381db7 1205 ret = route_map_apply(rmap, p, RMAP_BGP, &rmap_path);
0b16f239 1206
d62a17ae 1207 peer->rmap_type = 0;
0b16f239 1208
1f2263be 1209 if (ret == RMAP_DENYMATCH)
d62a17ae 1210 return RMAP_DENY;
0b16f239 1211 }
d62a17ae 1212 return RMAP_PERMIT;
0b16f239
DS
1213}
1214
d62a17ae 1215static int bgp_output_modifier(struct peer *peer, struct prefix *p,
1216 struct attr *attr, afi_t afi, safi_t safi,
1217 const char *rmap_name)
0b16f239 1218{
40381db7 1219 struct bgp_path_info rmap_path;
d62a17ae 1220 route_map_result_t ret;
1221 struct route_map *rmap = NULL;
d7c0a89a 1222 uint8_t rmap_type;
0b16f239 1223
b787157a
DS
1224 /*
1225 * So if we get to this point and have no rmap_name
1226 * we want to just show the output as it currently
1227 * exists.
1228 */
1229 if (!rmap_name)
1230 return RMAP_PERMIT;
0b16f239 1231
d62a17ae 1232 /* Apply default weight value. */
1233 if (peer->weight[afi][safi])
1234 attr->weight = peer->weight[afi][safi];
0b16f239 1235
b787157a 1236 rmap = route_map_lookup_by_name(rmap_name);
98a4a44e 1237
b787157a
DS
1238 /*
1239 * If we have a route map name and we do not find
1240 * the routemap that means we have an implicit
1241 * deny.
1242 */
1243 if (rmap == NULL)
1244 return RMAP_DENY;
0b16f239 1245
40381db7 1246 memset(&rmap_path, 0, sizeof(struct bgp_path_info));
d62a17ae 1247 /* Route map apply. */
b787157a 1248 /* Duplicate current value to new strucutre for modification. */
40381db7
DS
1249 rmap_path.peer = peer;
1250 rmap_path.attr = attr;
0b16f239 1251
0f672529 1252 rmap_type = peer->rmap_type;
b787157a 1253 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
0b16f239 1254
b787157a 1255 /* Apply BGP route map to the attribute. */
40381db7 1256 ret = route_map_apply(rmap, p, RMAP_BGP, &rmap_path);
ac41b2a2 1257
0f672529 1258 peer->rmap_type = rmap_type;
b787157a
DS
1259
1260 if (ret == RMAP_DENYMATCH)
1261 /*
1262 * caller has multiple error paths with bgp_attr_flush()
1263 */
1264 return RMAP_DENY;
ac41b2a2 1265
d62a17ae 1266 return RMAP_PERMIT;
718e3744 1267}
6b0655a2 1268
5000f21c 1269/* If this is an EBGP peer with remove-private-AS */
d62a17ae 1270static void bgp_peer_remove_private_as(struct bgp *bgp, afi_t afi, safi_t safi,
1271 struct peer *peer, struct attr *attr)
1272{
1273 if (peer->sort == BGP_PEER_EBGP
1274 && (peer_af_flag_check(peer, afi, safi,
1275 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1276 || peer_af_flag_check(peer, afi, safi,
1277 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE)
1278 || peer_af_flag_check(peer, afi, safi,
1279 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)
1280 || peer_af_flag_check(peer, afi, safi,
1281 PEER_FLAG_REMOVE_PRIVATE_AS))) {
1282 // Take action on the entire aspath
1283 if (peer_af_flag_check(peer, afi, safi,
1284 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1285 || peer_af_flag_check(peer, afi, safi,
1286 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)) {
1287 if (peer_af_flag_check(
1288 peer, afi, safi,
1289 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE))
1290 attr->aspath = aspath_replace_private_asns(
1291 attr->aspath, bgp->as);
1292
1293 // The entire aspath consists of private ASNs so create
1294 // an empty aspath
1295 else if (aspath_private_as_check(attr->aspath))
1296 attr->aspath = aspath_empty_get();
1297
1298 // There are some public and some private ASNs, remove
1299 // the private ASNs
1300 else
1301 attr->aspath = aspath_remove_private_asns(
1302 attr->aspath);
1303 }
1304
1305 // 'all' was not specified so the entire aspath must be private
1306 // ASNs
1307 // for us to do anything
1308 else if (aspath_private_as_check(attr->aspath)) {
1309 if (peer_af_flag_check(
1310 peer, afi, safi,
1311 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE))
1312 attr->aspath = aspath_replace_private_asns(
1313 attr->aspath, bgp->as);
1314 else
1315 attr->aspath = aspath_empty_get();
1316 }
1317 }
5000f21c
DS
1318}
1319
c7122e14 1320/* If this is an EBGP peer with as-override */
d62a17ae 1321static void bgp_peer_as_override(struct bgp *bgp, afi_t afi, safi_t safi,
1322 struct peer *peer, struct attr *attr)
1323{
1324 if (peer->sort == BGP_PEER_EBGP
1325 && peer_af_flag_check(peer, afi, safi, PEER_FLAG_AS_OVERRIDE)) {
1326 if (aspath_single_asn_check(attr->aspath, peer->as))
1327 attr->aspath = aspath_replace_specific_asn(
1328 attr->aspath, peer->as, bgp->as);
1329 }
1330}
1331
7f323236
DW
1332void bgp_attr_add_gshut_community(struct attr *attr)
1333{
1334 struct community *old;
1335 struct community *new;
1336 struct community *merge;
1337 struct community *gshut;
1338
1339 old = attr->community;
1340 gshut = community_str2com("graceful-shutdown");
1341
990f4f91 1342 assert(gshut);
1343
7f323236
DW
1344 if (old) {
1345 merge = community_merge(community_dup(old), gshut);
1346
a4d82a8a 1347 if (old->refcnt == 0)
7f323236
DW
1348 community_free(old);
1349
1350 new = community_uniq_sort(merge);
1351 community_free(merge);
1352 } else {
1353 new = community_dup(gshut);
1354 }
1355
1356 community_free(gshut);
1357 attr->community = new;
1358 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
1359
1360 /* When we add the graceful-shutdown community we must also
1361 * lower the local-preference */
1362 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1363 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
1364}
1365
1366
d7c0a89a 1367static void subgroup_announce_reset_nhop(uint8_t family, struct attr *attr)
c7122e14 1368{
960035b2 1369 if (family == AF_INET) {
d62a17ae 1370 attr->nexthop.s_addr = 0;
960035b2
PZ
1371 attr->mp_nexthop_global_in.s_addr = 0;
1372 }
d62a17ae 1373 if (family == AF_INET6)
1374 memset(&attr->mp_nexthop_global, 0, IPV6_MAX_BYTELEN);
ace295a9
MK
1375 if (family == AF_EVPN)
1376 memset(&attr->mp_nexthop_global_in, 0, BGP_ATTR_NHLEN_IPV4);
c7122e14
DS
1377}
1378
40381db7 1379int subgroup_announce_check(struct bgp_node *rn, struct bgp_path_info *pi,
d62a17ae 1380 struct update_subgroup *subgrp, struct prefix *p,
1381 struct attr *attr)
1382{
1383 struct bgp_filter *filter;
1384 struct peer *from;
1385 struct peer *peer;
1386 struct peer *onlypeer;
1387 struct bgp *bgp;
40381db7 1388 struct attr *piattr;
d62a17ae 1389 char buf[PREFIX_STRLEN];
1390 int ret;
1391 int transparent;
1392 int reflect;
1393 afi_t afi;
1394 safi_t safi;
1395 int samepeer_safe = 0; /* for synthetic mplsvpns routes */
1396
1397 if (DISABLE_BGP_ANNOUNCE)
1398 return 0;
1399
1400 afi = SUBGRP_AFI(subgrp);
1401 safi = SUBGRP_SAFI(subgrp);
1402 peer = SUBGRP_PEER(subgrp);
1403 onlypeer = NULL;
1404 if (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL))
1405 onlypeer = SUBGRP_PFIRST(subgrp)->peer;
1406
40381db7 1407 from = pi->peer;
d62a17ae 1408 filter = &peer->filter[afi][safi];
1409 bgp = SUBGRP_INST(subgrp);
40381db7
DS
1410 piattr = bgp_path_info_mpath_count(pi) ? bgp_path_info_mpath_attr(pi)
1411 : pi->attr;
3f9c7369 1412
65efcfce 1413#if ENABLE_BGP_VNC
d62a17ae 1414 if (((afi == AFI_IP) || (afi == AFI_IP6)) && (safi == SAFI_MPLS_VPN)
40381db7
DS
1415 && ((pi->type == ZEBRA_ROUTE_BGP_DIRECT)
1416 || (pi->type == ZEBRA_ROUTE_BGP_DIRECT_EXT))) {
d62a17ae 1417
1418 /*
1419 * direct and direct_ext type routes originate internally even
1420 * though they can have peer pointers that reference other
1421 * systems
1422 */
1423 prefix2str(p, buf, PREFIX_STRLEN);
1424 zlog_debug("%s: pfx %s bgp_direct->vpn route peer safe",
1425 __func__, buf);
1426 samepeer_safe = 1;
1427 }
65efcfce
LB
1428#endif
1429
ddb5b488
PZ
1430 if (((afi == AFI_IP) || (afi == AFI_IP6))
1431 && ((safi == SAFI_MPLS_VPN) || (safi == SAFI_UNICAST))
40381db7
DS
1432 && (pi->type == ZEBRA_ROUTE_BGP)
1433 && (pi->sub_type == BGP_ROUTE_IMPORTED)) {
ddb5b488
PZ
1434
1435 /* Applies to routes leaked vpn->vrf and vrf->vpn */
1436
1437 samepeer_safe = 1;
1438 }
1439
d62a17ae 1440 /* With addpath we may be asked to TX all kinds of paths so make sure
40381db7
DS
1441 * pi is valid */
1442 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID)
1443 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)
1444 || CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
d62a17ae 1445 return 0;
1446 }
adbac85e 1447
d62a17ae 1448 /* If this is not the bestpath then check to see if there is an enabled
1449 * addpath
1450 * feature that requires us to advertise it */
40381db7
DS
1451 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
1452 if (!bgp_addpath_tx_path(peer, afi, safi, pi)) {
d62a17ae 1453 return 0;
1454 }
1455 }
06370dac 1456
d62a17ae 1457 /* Aggregate-address suppress check. */
40381db7 1458 if (pi->extra && pi->extra->suppress)
d62a17ae 1459 if (!UNSUPPRESS_MAP_NAME(filter)) {
1460 return 0;
1461 }
3f9c7369 1462
13b7e7f0
DS
1463 /*
1464 * If we are doing VRF 2 VRF leaking via the import
1465 * statement, we want to prevent the route going
1466 * off box as that the RT and RD created are localy
1467 * significant and globaly useless.
1468 */
40381db7
DS
1469 if (safi == SAFI_MPLS_VPN && pi->extra && pi->extra->num_labels
1470 && pi->extra->label[0] == BGP_PREVENT_VRF_2_VRF_LEAK)
13b7e7f0
DS
1471 return 0;
1472
d62a17ae 1473 /* If it's labeled safi, make sure the route has a valid label. */
1474 if (safi == SAFI_LABELED_UNICAST) {
40381db7 1475 mpls_label_t label = bgp_adv_label(rn, pi, peer, afi, safi);
d62a17ae 1476 if (!bgp_is_valid_label(&label)) {
1477 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1478 zlog_debug("u%" PRIu64 ":s%" PRIu64
1479 " %s/%d is filtered - no label (%p)",
1480 subgrp->update_group->id, subgrp->id,
1481 inet_ntop(p->family, &p->u.prefix,
1482 buf, SU_ADDRSTRLEN),
1483 p->prefixlen, &label);
1484 return 0;
1485 }
1486 }
cd1964ff 1487
d62a17ae 1488 /* Do not send back route to sender. */
1489 if (onlypeer && from == onlypeer) {
1490 return 0;
1491 }
3f9c7369 1492
d62a17ae 1493 /* Do not send the default route in the BGP table if the neighbor is
1494 * configured for default-originate */
1495 if (CHECK_FLAG(peer->af_flags[afi][safi],
1496 PEER_FLAG_DEFAULT_ORIGINATE)) {
1497 if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
1498 return 0;
1499 else if (p->family == AF_INET6 && p->prefixlen == 0)
1500 return 0;
1501 }
4125bb67 1502
d62a17ae 1503 /* Transparency check. */
1504 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
1505 && CHECK_FLAG(from->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
1506 transparent = 1;
1507 else
1508 transparent = 0;
1509
1510 /* If community is not disabled check the no-export and local. */
40381db7 1511 if (!transparent && bgp_community_filter(peer, piattr)) {
d62a17ae 1512 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1513 zlog_debug(
1514 "subgrpannouncecheck: community filter check fail");
1515 return 0;
1516 }
3f9c7369 1517
d62a17ae 1518 /* If the attribute has originator-id and it is same as remote
1519 peer's id. */
40381db7
DS
1520 if (onlypeer && piattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
1521 && (IPV4_ADDR_SAME(&onlypeer->remote_id, &piattr->originator_id))) {
d62a17ae 1522 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1523 zlog_debug(
1524 "%s [Update:SEND] %s originator-id is same as "
1525 "remote router-id",
1526 onlypeer->host,
1527 prefix2str(p, buf, sizeof(buf)));
1528 return 0;
1529 }
3f9c7369 1530
d62a17ae 1531 /* ORF prefix-list filter check */
1532 if (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
1533 && (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
1534 || CHECK_FLAG(peer->af_cap[afi][safi],
1535 PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
1536 if (peer->orf_plist[afi][safi]) {
1537 if (prefix_list_apply(peer->orf_plist[afi][safi], p)
1538 == PREFIX_DENY) {
1539 if (bgp_debug_update(NULL, p,
1540 subgrp->update_group, 0))
1541 zlog_debug(
1542 "%s [Update:SEND] %s is filtered via ORF",
1543 peer->host,
1544 prefix2str(p, buf,
1545 sizeof(buf)));
1546 return 0;
1547 }
1548 }
1549
1550 /* Output filter check. */
40381db7 1551 if (bgp_output_filter(peer, p, piattr, afi, safi) == FILTER_DENY) {
d62a17ae 1552 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1553 zlog_debug("%s [Update:SEND] %s is filtered",
1554 peer->host, prefix2str(p, buf, sizeof(buf)));
1555 return 0;
1556 }
3f9c7369
DS
1557
1558#ifdef BGP_SEND_ASPATH_CHECK
d62a17ae 1559 /* AS path loop check. */
40381db7 1560 if (onlypeer && aspath_loop_check(piattr->aspath, onlypeer->as)) {
d62a17ae 1561 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1562 zlog_debug(
1563 "%s [Update:SEND] suppress announcement to peer AS %u "
1564 "that is part of AS path.",
1565 onlypeer->host, onlypeer->as);
1566 return 0;
1567 }
3f9c7369
DS
1568#endif /* BGP_SEND_ASPATH_CHECK */
1569
d62a17ae 1570 /* If we're a CONFED we need to loop check the CONFED ID too */
1571 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
40381db7 1572 if (aspath_loop_check(piattr->aspath, bgp->confed_id)) {
d62a17ae 1573 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1574 zlog_debug(
1575 "%s [Update:SEND] suppress announcement to peer AS %u"
1576 " is AS path.",
1577 peer->host, bgp->confed_id);
1578 return 0;
1579 }
3f9c7369 1580 }
3f9c7369 1581
d62a17ae 1582 /* Route-Reflect check. */
1583 if (from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
1584 reflect = 1;
1585 else
1586 reflect = 0;
1587
1588 /* IBGP reflection check. */
1589 if (reflect && !samepeer_safe) {
1590 /* A route from a Client peer. */
1591 if (CHECK_FLAG(from->af_flags[afi][safi],
1592 PEER_FLAG_REFLECTOR_CLIENT)) {
1593 /* Reflect to all the Non-Client peers and also to the
1594 Client peers other than the originator. Originator
1595 check
1596 is already done. So there is noting to do. */
1597 /* no bgp client-to-client reflection check. */
1598 if (bgp_flag_check(bgp, BGP_FLAG_NO_CLIENT_TO_CLIENT))
1599 if (CHECK_FLAG(peer->af_flags[afi][safi],
1600 PEER_FLAG_REFLECTOR_CLIENT))
1601 return 0;
1602 } else {
1603 /* A route from a Non-client peer. Reflect to all other
1604 clients. */
1605 if (!CHECK_FLAG(peer->af_flags[afi][safi],
1606 PEER_FLAG_REFLECTOR_CLIENT))
1607 return 0;
1608 }
1609 }
3f9c7369 1610
d62a17ae 1611 /* For modify attribute, copy it to temporary structure. */
40381db7 1612 bgp_attr_dup(attr, piattr);
d62a17ae 1613
1614 /* If local-preference is not set. */
1615 if ((peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
1616 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))) {
1617 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1618 attr->local_pref = bgp->default_local_pref;
3f9c7369 1619 }
3f9c7369 1620
d62a17ae 1621 /* If originator-id is not set and the route is to be reflected,
1622 set the originator id */
1623 if (reflect
1624 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)))) {
1625 IPV4_ADDR_COPY(&(attr->originator_id), &(from->remote_id));
1626 SET_FLAG(attr->flag, BGP_ATTR_ORIGINATOR_ID);
1627 }
3f9c7369 1628
d62a17ae 1629 /* Remove MED if its an EBGP peer - will get overwritten by route-maps
1630 */
1631 if (peer->sort == BGP_PEER_EBGP
1632 && attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
1633 if (from != bgp->peer_self && !transparent
1634 && !CHECK_FLAG(peer->af_flags[afi][safi],
1635 PEER_FLAG_MED_UNCHANGED))
1636 attr->flag &=
1637 ~(ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC));
1638 }
3f9c7369 1639
d62a17ae 1640 /* Since the nexthop attribute can vary per peer, it is not explicitly
1641 * set
1642 * in announce check, only certain flags and length (or number of
1643 * nexthops
1644 * -- for IPv6/MP_REACH) are set here in order to guide the update
1645 * formation
1646 * code in setting the nexthop(s) on a per peer basis in
1647 * reformat_peer().
1648 * Typically, the source nexthop in the attribute is preserved but in
1649 * the
1650 * scenarios where we know it will always be overwritten, we reset the
1651 * nexthop to "0" in an attempt to achieve better Update packing. An
1652 * example of this is when a prefix from each of 2 IBGP peers needs to
1653 * be
1654 * announced to an EBGP peer (and they have the same attributes barring
1655 * their nexthop).
1656 */
1657 if (reflect)
1658 SET_FLAG(attr->rmap_change_flags, BATTR_REFLECTED);
1659
1660#define NEXTHOP_IS_V6 \
1661 ((safi != SAFI_ENCAP && safi != SAFI_MPLS_VPN \
1662 && (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi))) \
1663 || ((safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN) \
1664 && attr->mp_nexthop_len >= IPV6_MAX_BYTELEN))
1665
1666 /* IPv6/MP starts with 1 nexthop. The link-local address is passed only
1667 * if
1668 * the peer (group) is configured to receive link-local nexthop
1669 * unchanged
1670 * and it is available in the prefix OR we're not reflecting the route
1671 * and
1672 * the peer (group) to whom we're going to announce is on a shared
1673 * network
1674 * and this is either a self-originated route or the peer is EBGP.
1675 */
1676 if (NEXTHOP_IS_V6) {
1677 attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
1678 if ((CHECK_FLAG(peer->af_flags[afi][safi],
1679 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)
1680 && IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_local))
1681 || (!reflect && peer->shared_network
1682 && (from == bgp->peer_self
1683 || peer->sort == BGP_PEER_EBGP))) {
1684 attr->mp_nexthop_len =
1685 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
1686 }
3f9c7369 1687
d62a17ae 1688 /* Clear off link-local nexthop in source, whenever it is not
1689 * needed to
1690 * ensure more prefixes share the same attribute for
1691 * announcement.
1692 */
1693 if (!(CHECK_FLAG(peer->af_flags[afi][safi],
1694 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)))
1695 memset(&attr->mp_nexthop_local, 0, IPV6_MAX_BYTELEN);
1696 }
3f9c7369 1697
d62a17ae 1698 bgp_peer_remove_private_as(bgp, afi, safi, peer, attr);
1699 bgp_peer_as_override(bgp, afi, safi, peer, attr);
1700
1701 /* Route map & unsuppress-map apply. */
40381db7
DS
1702 if (ROUTE_MAP_OUT_NAME(filter) || (pi->extra && pi->extra->suppress)) {
1703 struct bgp_path_info rmap_path;
1704 struct bgp_path_info_extra dummy_rmap_path_extra;
d62a17ae 1705 struct attr dummy_attr;
1706
40381db7
DS
1707 memset(&rmap_path, 0, sizeof(struct bgp_path_info));
1708 rmap_path.peer = peer;
1709 rmap_path.attr = attr;
d37ba549 1710
40381db7
DS
1711 if (pi->extra) {
1712 memcpy(&dummy_rmap_path_extra, pi->extra,
4b7e6066 1713 sizeof(struct bgp_path_info_extra));
40381db7 1714 rmap_path.extra = &dummy_rmap_path_extra;
16f7ce2b
MK
1715 }
1716
d62a17ae 1717 /* don't confuse inbound and outbound setting */
1718 RESET_FLAG(attr->rmap_change_flags);
1719
1720 /*
1721 * The route reflector is not allowed to modify the attributes
1722 * of the reflected IBGP routes unless explicitly allowed.
1723 */
1724 if ((from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
1725 && !bgp_flag_check(bgp,
1726 BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY)) {
1727 bgp_attr_dup(&dummy_attr, attr);
40381db7 1728 rmap_path.attr = &dummy_attr;
d62a17ae 1729 }
3f9c7369 1730
d62a17ae 1731 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
3f9c7369 1732
40381db7 1733 if (pi->extra && pi->extra->suppress)
d62a17ae 1734 ret = route_map_apply(UNSUPPRESS_MAP(filter), p,
40381db7 1735 RMAP_BGP, &rmap_path);
d62a17ae 1736 else
1737 ret = route_map_apply(ROUTE_MAP_OUT(filter), p,
40381db7 1738 RMAP_BGP, &rmap_path);
d62a17ae 1739
1740 peer->rmap_type = 0;
1741
1742 if (ret == RMAP_DENYMATCH) {
1743 bgp_attr_flush(attr);
1744 return 0;
1745 }
3f9c7369 1746 }
3f9c7369 1747
7f323236 1748 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) {
a4d82a8a
PZ
1749 if (peer->sort == BGP_PEER_IBGP
1750 || peer->sort == BGP_PEER_CONFED) {
7f323236
DW
1751 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1752 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
1753 } else {
1754 bgp_attr_add_gshut_community(attr);
1755 }
1756 }
1757
d62a17ae 1758 /* After route-map has been applied, we check to see if the nexthop to
1759 * be carried in the attribute (that is used for the announcement) can
1760 * be cleared off or not. We do this in all cases where we would be
1761 * setting the nexthop to "ourselves". For IPv6, we only need to
1762 * consider
1763 * the global nexthop here; the link-local nexthop would have been
1764 * cleared
1765 * already, and if not, it is required by the update formation code.
1766 * Also see earlier comments in this function.
1767 */
1768 /*
1769 * If route-map has performed some operation on the nexthop or the peer
1770 * configuration says to pass it unchanged, we cannot reset the nexthop
1771 * here, so only attempt to do it if these aren't true. Note that the
1772 * route-map handler itself might have cleared the nexthop, if for
1773 * example,
1774 * it is configured as 'peer-address'.
1775 */
1776 if (!bgp_rmap_nhop_changed(attr->rmap_change_flags,
40381db7 1777 piattr->rmap_change_flags)
d62a17ae 1778 && !transparent
1779 && !CHECK_FLAG(peer->af_flags[afi][safi],
1780 PEER_FLAG_NEXTHOP_UNCHANGED)) {
1781 /* We can reset the nexthop, if setting (or forcing) it to
1782 * 'self' */
1783 if (CHECK_FLAG(peer->af_flags[afi][safi],
1784 PEER_FLAG_NEXTHOP_SELF)
1785 || CHECK_FLAG(peer->af_flags[afi][safi],
1786 PEER_FLAG_FORCE_NEXTHOP_SELF)) {
1787 if (!reflect
1788 || CHECK_FLAG(peer->af_flags[afi][safi],
1789 PEER_FLAG_FORCE_NEXTHOP_SELF))
1790 subgroup_announce_reset_nhop(
1791 (peer_cap_enhe(peer, afi, safi)
1792 ? AF_INET6
1793 : p->family),
1794 attr);
1795 } else if (peer->sort == BGP_PEER_EBGP) {
1796 /* Can also reset the nexthop if announcing to EBGP, but
1797 * only if
1798 * no peer in the subgroup is on a shared subnet.
1799 * Note: 3rd party nexthop currently implemented for
1800 * IPv4 only.
1801 */
40381db7 1802 if (!bgp_subgrp_multiaccess_check_v4(piattr->nexthop,
65d4e0c6 1803 subgrp))
d62a17ae 1804 subgroup_announce_reset_nhop(
1805 (peer_cap_enhe(peer, afi, safi)
a4d82a8a
PZ
1806 ? AF_INET6
1807 : p->family),
d62a17ae 1808 attr);
40381db7 1809 } else if (CHECK_FLAG(pi->flags, BGP_PATH_ANNC_NH_SELF)) {
960035b2
PZ
1810 /*
1811 * This flag is used for leaked vpn-vrf routes
1812 */
1813 int family = p->family;
1814
1815 if (peer_cap_enhe(peer, afi, safi))
1816 family = AF_INET6;
1817
1818 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
1819 zlog_debug(
1defdda8 1820 "%s: BGP_PATH_ANNC_NH_SELF, family=%s",
960035b2
PZ
1821 __func__, family2str(family));
1822 subgroup_announce_reset_nhop(family, attr);
d62a17ae 1823 }
960035b2 1824
d62a17ae 1825 /* If IPv6/MP and nexthop does not have any override and happens
1826 * to
1827 * be a link-local address, reset it so that we don't pass along
1828 * the
1829 * source's link-local IPv6 address to recipients who may not be
1830 * on
1831 * the same interface.
1832 */
1833 if (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi)) {
1834 if (IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_global))
1835 subgroup_announce_reset_nhop(AF_INET6, attr);
1836 }
1837 }
3f9c7369 1838
d62a17ae 1839 return 1;
3f9c7369
DS
1840}
1841
d62a17ae 1842void bgp_best_selection(struct bgp *bgp, struct bgp_node *rn,
1843 struct bgp_maxpaths_cfg *mpath_cfg,
4b7e6066
DS
1844 struct bgp_path_info_pair *result, afi_t afi,
1845 safi_t safi)
1846{
1847 struct bgp_path_info *new_select;
1848 struct bgp_path_info *old_select;
40381db7
DS
1849 struct bgp_path_info *pi;
1850 struct bgp_path_info *pi1;
1851 struct bgp_path_info *pi2;
1852 struct bgp_path_info *nextpi = NULL;
d62a17ae 1853 int paths_eq, do_mpath, debug;
1854 struct list mp_list;
1855 char pfx_buf[PREFIX2STR_BUFFER];
1856 char path_buf[PATH_ADDPATH_STR_BUFFER];
1857
1858 bgp_mp_list_init(&mp_list);
1859 do_mpath =
1860 (mpath_cfg->maxpaths_ebgp > 1 || mpath_cfg->maxpaths_ibgp > 1);
1861
1862 debug = bgp_debug_bestpath(&rn->p);
1863
1864 if (debug)
1865 prefix2str(&rn->p, pfx_buf, sizeof(pfx_buf));
1866
1867 /* bgp deterministic-med */
1868 new_select = NULL;
1869 if (bgp_flag_check(bgp, BGP_FLAG_DETERMINISTIC_MED)) {
1870
1defdda8 1871 /* Clear BGP_PATH_DMED_SELECTED for all paths */
40381db7
DS
1872 for (pi1 = rn->info; pi1; pi1 = pi1->next)
1873 bgp_path_info_unset_flag(rn, pi1,
18ee8310 1874 BGP_PATH_DMED_SELECTED);
d62a17ae 1875
40381db7
DS
1876 for (pi1 = rn->info; pi1; pi1 = pi1->next) {
1877 if (CHECK_FLAG(pi1->flags, BGP_PATH_DMED_CHECK))
d62a17ae 1878 continue;
40381db7 1879 if (BGP_PATH_HOLDDOWN(pi1))
d62a17ae 1880 continue;
40381db7
DS
1881 if (pi1->peer && pi1->peer != bgp->peer_self)
1882 if (pi1->peer->status != Established)
d62a17ae 1883 continue;
1884
40381db7
DS
1885 new_select = pi1;
1886 if (pi1->next) {
1887 for (pi2 = pi1->next; pi2; pi2 = pi2->next) {
1888 if (CHECK_FLAG(pi2->flags,
1defdda8 1889 BGP_PATH_DMED_CHECK))
d62a17ae 1890 continue;
40381db7 1891 if (BGP_PATH_HOLDDOWN(pi2))
d62a17ae 1892 continue;
40381db7
DS
1893 if (pi2->peer
1894 && pi2->peer != bgp->peer_self
d62a17ae 1895 && !CHECK_FLAG(
40381db7 1896 pi2->peer->sflags,
d62a17ae 1897 PEER_STATUS_NSF_WAIT))
40381db7 1898 if (pi2->peer->status
d62a17ae 1899 != Established)
1900 continue;
1901
121e245d
DS
1902 if (!aspath_cmp_left(pi1->attr->aspath,
1903 pi2->attr->aspath)
1904 && !aspath_cmp_left_confed(
40381db7 1905 pi1->attr->aspath,
121e245d
DS
1906 pi2->attr->aspath))
1907 continue;
1908
1909 if (bgp_path_info_cmp(
1910 bgp, pi2, new_select,
1911 &paths_eq, mpath_cfg, debug,
1912 pfx_buf, afi, safi)) {
1913 bgp_path_info_unset_flag(
1914 rn, new_select,
1915 BGP_PATH_DMED_SELECTED);
1916 new_select = pi2;
d62a17ae 1917 }
121e245d
DS
1918
1919 bgp_path_info_set_flag(
1920 rn, pi2, BGP_PATH_DMED_CHECK);
d62a17ae 1921 }
1922 }
18ee8310
DS
1923 bgp_path_info_set_flag(rn, new_select,
1924 BGP_PATH_DMED_CHECK);
1925 bgp_path_info_set_flag(rn, new_select,
1926 BGP_PATH_DMED_SELECTED);
d62a17ae 1927
1928 if (debug) {
18ee8310
DS
1929 bgp_path_info_path_with_addpath_rx_str(
1930 new_select, path_buf);
c66faab1 1931 zlog_debug("%s: %s is the bestpath from AS %u",
d62a17ae 1932 pfx_buf, path_buf,
1933 aspath_get_first_as(
1934 new_select->attr->aspath));
1935 }
1936 }
1937 }
96450faf 1938
d62a17ae 1939 /* Check old selected route and new selected route. */
1940 old_select = NULL;
1941 new_select = NULL;
40381db7
DS
1942 for (pi = rn->info; (pi != NULL) && (nextpi = pi->next, 1);
1943 pi = nextpi) {
1944 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
1945 old_select = pi;
d62a17ae 1946
40381db7 1947 if (BGP_PATH_HOLDDOWN(pi)) {
d62a17ae 1948 /* reap REMOVED routes, if needs be
1949 * selected route must stay for a while longer though
1950 */
40381db7
DS
1951 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
1952 && (pi != old_select))
1953 bgp_path_info_reap(rn, pi);
d62a17ae 1954
ddb5b488 1955 if (debug)
40381db7
DS
1956 zlog_debug("%s: pi %p in holddown", __func__,
1957 pi);
ddb5b488 1958
d62a17ae 1959 continue;
1960 }
96450faf 1961
40381db7
DS
1962 if (pi->peer && pi->peer != bgp->peer_self
1963 && !CHECK_FLAG(pi->peer->sflags, PEER_STATUS_NSF_WAIT))
1964 if (pi->peer->status != Established) {
ddb5b488
PZ
1965
1966 if (debug)
1967 zlog_debug(
40381db7
DS
1968 "%s: pi %p non self peer %s not estab state",
1969 __func__, pi, pi->peer->host);
ddb5b488 1970
d62a17ae 1971 continue;
ddb5b488 1972 }
9fbdd100 1973
d62a17ae 1974 if (bgp_flag_check(bgp, BGP_FLAG_DETERMINISTIC_MED)
40381db7
DS
1975 && (!CHECK_FLAG(pi->flags, BGP_PATH_DMED_SELECTED))) {
1976 bgp_path_info_unset_flag(rn, pi, BGP_PATH_DMED_CHECK);
ddb5b488 1977 if (debug)
40381db7 1978 zlog_debug("%s: pi %p dmed", __func__, pi);
d62a17ae 1979 continue;
1980 }
9fbdd100 1981
40381db7 1982 bgp_path_info_unset_flag(rn, pi, BGP_PATH_DMED_CHECK);
06370dac 1983
40381db7 1984 if (bgp_path_info_cmp(bgp, pi, new_select, &paths_eq, mpath_cfg,
18ee8310 1985 debug, pfx_buf, afi, safi)) {
40381db7 1986 new_select = pi;
d62a17ae 1987 }
1988 }
718e3744 1989
d62a17ae 1990 /* Now that we know which path is the bestpath see if any of the other
1991 * paths
1992 * qualify as multipaths
1993 */
1994 if (debug) {
1995 if (new_select)
18ee8310
DS
1996 bgp_path_info_path_with_addpath_rx_str(new_select,
1997 path_buf);
d62a17ae 1998 else
1999 sprintf(path_buf, "NONE");
2000 zlog_debug(
2001 "%s: After path selection, newbest is %s oldbest was %s",
2002 pfx_buf, path_buf,
2003 old_select ? old_select->peer->host : "NONE");
96450faf 2004 }
9fbdd100 2005
d62a17ae 2006 if (do_mpath && new_select) {
40381db7
DS
2007 for (pi = rn->info; (pi != NULL) && (nextpi = pi->next, 1);
2008 pi = nextpi) {
d62a17ae 2009
2010 if (debug)
18ee8310 2011 bgp_path_info_path_with_addpath_rx_str(
40381db7 2012 pi, path_buf);
d62a17ae 2013
40381db7 2014 if (pi == new_select) {
d62a17ae 2015 if (debug)
2016 zlog_debug(
2017 "%s: %s is the bestpath, add to the multipath list",
2018 pfx_buf, path_buf);
40381db7 2019 bgp_mp_list_add(&mp_list, pi);
d62a17ae 2020 continue;
2021 }
2022
40381db7 2023 if (BGP_PATH_HOLDDOWN(pi))
d62a17ae 2024 continue;
2025
40381db7
DS
2026 if (pi->peer && pi->peer != bgp->peer_self
2027 && !CHECK_FLAG(pi->peer->sflags,
d62a17ae 2028 PEER_STATUS_NSF_WAIT))
40381db7 2029 if (pi->peer->status != Established)
d62a17ae 2030 continue;
2031
40381db7 2032 if (!bgp_path_info_nexthop_cmp(pi, new_select)) {
d62a17ae 2033 if (debug)
2034 zlog_debug(
2035 "%s: %s has the same nexthop as the bestpath, skip it",
2036 pfx_buf, path_buf);
2037 continue;
2038 }
2039
40381db7 2040 bgp_path_info_cmp(bgp, pi, new_select, &paths_eq,
18ee8310 2041 mpath_cfg, debug, pfx_buf, afi, safi);
d62a17ae 2042
2043 if (paths_eq) {
2044 if (debug)
2045 zlog_debug(
2046 "%s: %s is equivalent to the bestpath, add to the multipath list",
2047 pfx_buf, path_buf);
40381db7 2048 bgp_mp_list_add(&mp_list, pi);
d62a17ae 2049 }
2050 }
2051 }
fee0f4c6 2052
18ee8310
DS
2053 bgp_path_info_mpath_update(rn, new_select, old_select, &mp_list,
2054 mpath_cfg);
2055 bgp_path_info_mpath_aggregate_update(new_select, old_select);
d62a17ae 2056 bgp_mp_list_clear(&mp_list);
96450faf 2057
d62a17ae 2058 result->old = old_select;
2059 result->new = new_select;
96450faf 2060
d62a17ae 2061 return;
fee0f4c6 2062}
2063
3f9c7369
DS
2064/*
2065 * A new route/change in bestpath of an existing route. Evaluate the path
2066 * for advertisement to the subgroup.
2067 */
d62a17ae 2068int subgroup_process_announce_selected(struct update_subgroup *subgrp,
4b7e6066 2069 struct bgp_path_info *selected,
d62a17ae 2070 struct bgp_node *rn,
d7c0a89a 2071 uint32_t addpath_tx_id)
d62a17ae 2072{
2073 struct prefix *p;
2074 struct peer *onlypeer;
2075 struct attr attr;
2076 afi_t afi;
2077 safi_t safi;
adbac85e 2078
d62a17ae 2079 p = &rn->p;
2080 afi = SUBGRP_AFI(subgrp);
2081 safi = SUBGRP_SAFI(subgrp);
2082 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
2083 : NULL);
2084
e0207895
PZ
2085 if (BGP_DEBUG(update, UPDATE_OUT)) {
2086 char buf_prefix[PREFIX_STRLEN];
2087 prefix2str(p, buf_prefix, sizeof(buf_prefix));
ddb5b488
PZ
2088 zlog_debug("%s: p=%s, selected=%p", __func__, buf_prefix,
2089 selected);
e0207895
PZ
2090 }
2091
d62a17ae 2092 /* First update is deferred until ORF or ROUTE-REFRESH is received */
996c9314
LB
2093 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[afi][safi],
2094 PEER_STATUS_ORF_WAIT_REFRESH))
d62a17ae 2095 return 0;
2096
2097 memset(&attr, 0, sizeof(struct attr));
2098 /* It's initialized in bgp_announce_check() */
2099
2100 /* Announcement to the subgroup. If the route is filtered withdraw it.
2101 */
2102 if (selected) {
2103 if (subgroup_announce_check(rn, selected, subgrp, p, &attr))
2104 bgp_adj_out_set_subgroup(rn, subgrp, &attr, selected);
2105 else
2106 bgp_adj_out_unset_subgroup(rn, subgrp, 1,
2107 selected->addpath_tx_id);
2108 }
2109
2110 /* If selected is NULL we must withdraw the path using addpath_tx_id */
2111 else {
2112 bgp_adj_out_unset_subgroup(rn, subgrp, 1, addpath_tx_id);
2113 }
558d1fec 2114
d62a17ae 2115 return 0;
200df115 2116}
fee0f4c6 2117
3064bf43 2118/*
e1072051 2119 * Clear IGP changed flag and attribute changed flag for a route (all paths).
2120 * This is called at the end of route processing.
3064bf43 2121 */
d62a17ae 2122void bgp_zebra_clear_route_change_flags(struct bgp_node *rn)
3064bf43 2123{
40381db7 2124 struct bgp_path_info *pi;
3064bf43 2125
40381db7
DS
2126 for (pi = rn->info; pi; pi = pi->next) {
2127 if (BGP_PATH_HOLDDOWN(pi))
d62a17ae 2128 continue;
40381db7
DS
2129 UNSET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2130 UNSET_FLAG(pi->flags, BGP_PATH_ATTR_CHANGED);
d62a17ae 2131 }
3064bf43 2132}
2133
2134/*
2135 * Has the route changed from the RIB's perspective? This is invoked only
2136 * if the route selection returns the same best route as earlier - to
2137 * determine if we need to update zebra or not.
2138 */
4b7e6066
DS
2139int bgp_zebra_has_route_changed(struct bgp_node *rn,
2140 struct bgp_path_info *selected)
d62a17ae 2141{
4b7e6066 2142 struct bgp_path_info *mpinfo;
d62a17ae 2143
2bb9eff4
DS
2144 /* If this is multipath, check all selected paths for any nexthop
2145 * change or attribute change. Some attribute changes (e.g., community)
2146 * aren't of relevance to the RIB, but we'll update zebra to ensure
2147 * we handle the case of BGP nexthop change. This is the behavior
2148 * when the best path has an attribute change anyway.
d62a17ae 2149 */
1defdda8
DS
2150 if (CHECK_FLAG(selected->flags, BGP_PATH_IGP_CHANGED)
2151 || CHECK_FLAG(selected->flags, BGP_PATH_MULTIPATH_CHG))
d62a17ae 2152 return 1;
2153
2bb9eff4
DS
2154 /*
2155 * If this is multipath, check all selected paths for any nexthop change
d62a17ae 2156 */
18ee8310
DS
2157 for (mpinfo = bgp_path_info_mpath_first(selected); mpinfo;
2158 mpinfo = bgp_path_info_mpath_next(mpinfo)) {
1defdda8
DS
2159 if (CHECK_FLAG(mpinfo->flags, BGP_PATH_IGP_CHANGED)
2160 || CHECK_FLAG(mpinfo->flags, BGP_PATH_ATTR_CHANGED))
d62a17ae 2161 return 1;
2162 }
3064bf43 2163
d62a17ae 2164 /* Nothing has changed from the RIB's perspective. */
2165 return 0;
3064bf43 2166}
2167
d62a17ae 2168struct bgp_process_queue {
2169 struct bgp *bgp;
a4d82a8a 2170 STAILQ_HEAD(, bgp_node) pqueue;
aac24838
JB
2171#define BGP_PROCESS_QUEUE_EOIU_MARKER (1 << 0)
2172 unsigned int flags;
2173 unsigned int queued;
200df115 2174};
2175
3103e8d2
DS
2176/*
2177 * old_select = The old best path
2178 * new_select = the new best path
2179 *
2180 * if (!old_select && new_select)
2181 * We are sending new information on.
2182 *
2183 * if (old_select && new_select) {
2184 * if (new_select != old_select)
2185 * We have a new best path send a change
2186 * else
2187 * We've received a update with new attributes that needs
2188 * to be passed on.
2189 * }
2190 *
2191 * if (old_select && !new_select)
2192 * We have no eligible route that we can announce or the rn
2193 * is being removed.
2194 */
aac24838
JB
2195static void bgp_process_main_one(struct bgp *bgp, struct bgp_node *rn,
2196 afi_t afi, safi_t safi)
d62a17ae 2197{
4b7e6066
DS
2198 struct bgp_path_info *new_select;
2199 struct bgp_path_info *old_select;
2200 struct bgp_path_info_pair old_and_new;
ddb5b488
PZ
2201 char pfx_buf[PREFIX2STR_BUFFER];
2202 int debug = 0;
d62a17ae 2203
2204 /* Is it end of initial update? (after startup) */
2205 if (!rn) {
2206 quagga_timestamp(3, bgp->update_delay_zebra_resume_time,
2207 sizeof(bgp->update_delay_zebra_resume_time));
2208
2209 bgp->main_zebra_update_hold = 0;
05c7a1cc
QY
2210 FOREACH_AFI_SAFI (afi, safi) {
2211 if (bgp_fibupd_safi(safi))
2212 bgp_zebra_announce_table(bgp, afi, safi);
2213 }
d62a17ae 2214 bgp->main_peers_update_hold = 0;
2215
2216 bgp_start_routeadv(bgp);
aac24838 2217 return;
d62a17ae 2218 }
cb1faec9 2219
b575a12c
A
2220 struct prefix *p = &rn->p;
2221
ddb5b488
PZ
2222 debug = bgp_debug_bestpath(&rn->p);
2223 if (debug) {
2224 prefix2str(&rn->p, pfx_buf, sizeof(pfx_buf));
2225 zlog_debug("%s: p=%s afi=%s, safi=%s start", __func__, pfx_buf,
2226 afi2str(afi), safi2str(safi));
2227 }
2228
d62a17ae 2229 /* Best path selection. */
2230 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
2231 afi, safi);
2232 old_select = old_and_new.old;
2233 new_select = old_and_new.new;
2234
2235 /* Do we need to allocate or free labels?
2236 * Right now, since we only deal with per-prefix labels, it is not
cb499ebb 2237 * necessary to do this upon changes to best path except if the label
09fdc88c 2238 * index changes
d62a17ae 2239 */
318cac96 2240 if (bgp->allocate_mpls_labels[afi][safi]) {
d62a17ae 2241 if (new_select) {
2242 if (!old_select
2243 || bgp_label_index_differs(new_select, old_select)
2244 || new_select->sub_type != old_select->sub_type) {
2245 if (new_select->sub_type == BGP_ROUTE_STATIC
2246 && new_select->attr->flag
2247 & ATTR_FLAG_BIT(
2248 BGP_ATTR_PREFIX_SID)
2249 && new_select->attr->label_index
2250 != BGP_INVALID_LABEL_INDEX) {
2251 if (CHECK_FLAG(
2252 rn->flags,
2253 BGP_NODE_REGISTERED_FOR_LABEL))
2254 bgp_unregister_for_label(rn);
70e98a7f 2255 label_ntop(MPLS_LABEL_IMPLICIT_NULL, 1,
d62a17ae 2256 &rn->local_label);
2257 bgp_set_valid_label(&rn->local_label);
2258 } else
2259 bgp_register_for_label(rn, new_select);
2260 }
a4d82a8a
PZ
2261 } else if (CHECK_FLAG(rn->flags,
2262 BGP_NODE_REGISTERED_FOR_LABEL)) {
d62a17ae 2263 bgp_unregister_for_label(rn);
318cac96
DW
2264 }
2265 } else if (CHECK_FLAG(rn->flags, BGP_NODE_REGISTERED_FOR_LABEL)) {
2266 bgp_unregister_for_label(rn);
d62a17ae 2267 }
cd1964ff 2268
ddb5b488
PZ
2269 if (debug) {
2270 prefix2str(&rn->p, pfx_buf, sizeof(pfx_buf));
2271 zlog_debug(
2272 "%s: p=%s afi=%s, safi=%s, old_select=%p, new_select=%p",
2273 __func__, pfx_buf, afi2str(afi), safi2str(safi),
2274 old_select, new_select);
2275 }
2276
d62a17ae 2277 /* If best route remains the same and this is not due to user-initiated
2278 * clear, see exactly what needs to be done.
2279 */
d62a17ae 2280 if (old_select && old_select == new_select
2281 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
1defdda8 2282 && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
d62a17ae 2283 && !bgp->addpath_tx_used[afi][safi]) {
2284 if (bgp_zebra_has_route_changed(rn, old_select)) {
65efcfce 2285#if ENABLE_BGP_VNC
d62a17ae 2286 vnc_import_bgp_add_route(bgp, p, old_select);
2287 vnc_import_bgp_exterior_add_route(bgp, p, old_select);
65efcfce 2288#endif
bb744275 2289 if (bgp_fibupd_safi(safi)
ddb5b488
PZ
2290 && !bgp_option_check(BGP_OPT_NO_FIB)) {
2291
2292 if (new_select->type == ZEBRA_ROUTE_BGP
2293 && (new_select->sub_type == BGP_ROUTE_NORMAL
2294 || new_select->sub_type
2295 == BGP_ROUTE_IMPORTED))
2296
2297 bgp_zebra_announce(rn, p, old_select,
2298 bgp, afi, safi);
2299 }
d62a17ae 2300 }
1defdda8 2301 UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
d62a17ae 2302 bgp_zebra_clear_route_change_flags(rn);
2303
2304 /* If there is a change of interest to peers, reannounce the
2305 * route. */
1defdda8 2306 if (CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
d62a17ae 2307 || CHECK_FLAG(rn->flags, BGP_NODE_LABEL_CHANGED)) {
2308 group_announce_route(bgp, afi, safi, rn, new_select);
2309
2310 /* unicast routes must also be annouced to
2311 * labeled-unicast update-groups */
2312 if (safi == SAFI_UNICAST)
2313 group_announce_route(bgp, afi,
2314 SAFI_LABELED_UNICAST, rn,
2315 new_select);
2316
1defdda8 2317 UNSET_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED);
d62a17ae 2318 UNSET_FLAG(rn->flags, BGP_NODE_LABEL_CHANGED);
2319 }
fee0f4c6 2320
d62a17ae 2321 UNSET_FLAG(rn->flags, BGP_NODE_PROCESS_SCHEDULED);
aac24838 2322 return;
d62a17ae 2323 }
8ad7271d 2324
d62a17ae 2325 /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set
2326 */
2327 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
2328
2329 /* bestpath has changed; bump version */
2330 if (old_select || new_select) {
2331 bgp_bump_version(rn);
2332
2333 if (!bgp->t_rmap_def_originate_eval) {
2334 bgp_lock(bgp);
2335 thread_add_timer(
2336 bm->master,
2337 update_group_refresh_default_originate_route_map,
2338 bgp, RMAP_DEFAULT_ORIGINATE_EVAL_TIMER,
2339 &bgp->t_rmap_def_originate_eval);
2340 }
2341 }
3f9c7369 2342
d62a17ae 2343 if (old_select)
18ee8310 2344 bgp_path_info_unset_flag(rn, old_select, BGP_PATH_SELECTED);
d62a17ae 2345 if (new_select) {
ddb5b488
PZ
2346 if (debug)
2347 zlog_debug("%s: setting SELECTED flag", __func__);
18ee8310
DS
2348 bgp_path_info_set_flag(rn, new_select, BGP_PATH_SELECTED);
2349 bgp_path_info_unset_flag(rn, new_select, BGP_PATH_ATTR_CHANGED);
1defdda8 2350 UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
d62a17ae 2351 }
338b3424 2352
65efcfce 2353#if ENABLE_BGP_VNC
d62a17ae 2354 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
2355 if (old_select != new_select) {
2356 if (old_select) {
2357 vnc_import_bgp_exterior_del_route(bgp, p,
2358 old_select);
2359 vnc_import_bgp_del_route(bgp, p, old_select);
2360 }
2361 if (new_select) {
2362 vnc_import_bgp_exterior_add_route(bgp, p,
2363 new_select);
2364 vnc_import_bgp_add_route(bgp, p, new_select);
2365 }
2366 }
2367 }
65efcfce
LB
2368#endif
2369
d62a17ae 2370 group_announce_route(bgp, afi, safi, rn, new_select);
2371
2372 /* unicast routes must also be annouced to labeled-unicast update-groups
2373 */
2374 if (safi == SAFI_UNICAST)
2375 group_announce_route(bgp, afi, SAFI_LABELED_UNICAST, rn,
2376 new_select);
2377
2378 /* FIB update. */
2379 if (bgp_fibupd_safi(safi) && (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
2380 && !bgp_option_check(BGP_OPT_NO_FIB)) {
2381 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
2382 && (new_select->sub_type == BGP_ROUTE_NORMAL
ddb5b488 2383 || new_select->sub_type == BGP_ROUTE_AGGREGATE
2b659f33
MK
2384 || new_select->sub_type == BGP_ROUTE_IMPORTED)) {
2385
2386 /* if this is an evpn imported type-5 prefix,
2387 * we need to withdraw the route first to clear
2388 * the nh neigh and the RMAC entry.
2389 */
2390 if (old_select &&
2391 is_route_parent_evpn(old_select))
2392 bgp_zebra_withdraw(p, old_select, bgp, safi);
ddb5b488 2393
d62a17ae 2394 bgp_zebra_announce(rn, p, new_select, bgp, afi, safi);
2b659f33 2395 } else {
d62a17ae 2396 /* Withdraw the route from the kernel. */
2397 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
2398 && (old_select->sub_type == BGP_ROUTE_NORMAL
ddb5b488
PZ
2399 || old_select->sub_type == BGP_ROUTE_AGGREGATE
2400 || old_select->sub_type == BGP_ROUTE_IMPORTED))
2401
568e10ca 2402 bgp_zebra_withdraw(p, old_select, bgp, safi);
d62a17ae 2403 }
718e3744 2404 }
3064bf43 2405
5424b7ba
MK
2406 /* advertise/withdraw type-5 routes */
2407 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
7b1bf203 2408 if (advertise_type5_routes(bgp, afi) && new_select &&
7e4ed18e
MK
2409 (!new_select->extra || !new_select->extra->parent)) {
2410
2411 /* apply the route-map */
2412 if (bgp->adv_cmd_rmap[afi][safi].map) {
2413 int ret = 0;
2414
3518f352
DS
2415 ret = route_map_apply(
2416 bgp->adv_cmd_rmap[afi][safi].map,
2417 &rn->p, RMAP_BGP, new_select);
7e4ed18e 2418 if (ret == RMAP_MATCH)
3518f352
DS
2419 bgp_evpn_advertise_type5_route(
2420 bgp, &rn->p, new_select->attr,
2421 afi, safi);
7e4ed18e
MK
2422 } else {
2423 bgp_evpn_advertise_type5_route(bgp,
2424 &rn->p,
2425 new_select->attr,
2426 afi, safi);
2427
2428 }
2429 } else if (advertise_type5_routes(bgp, afi) && old_select &&
2430 (!old_select->extra || !old_select->extra->parent))
31310b25 2431 bgp_evpn_withdraw_type5_route(bgp, &rn->p, afi, safi);
5424b7ba
MK
2432 }
2433
d62a17ae 2434 /* Clear any route change flags. */
2435 bgp_zebra_clear_route_change_flags(rn);
3064bf43 2436
18ee8310 2437 /* Reap old select bgp_path_info, if it has been removed */
1defdda8 2438 if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
18ee8310 2439 bgp_path_info_reap(rn, old_select);
d62a17ae 2440
2441 UNSET_FLAG(rn->flags, BGP_NODE_PROCESS_SCHEDULED);
aac24838 2442 return;
718e3744 2443}
2444
aac24838 2445static wq_item_status bgp_process_wq(struct work_queue *wq, void *data)
200df115 2446{
aac24838
JB
2447 struct bgp_process_queue *pqnode = data;
2448 struct bgp *bgp = pqnode->bgp;
d62a17ae 2449 struct bgp_table *table;
ac021f40 2450 struct bgp_node *rn;
aac24838
JB
2451
2452 /* eoiu marker */
2453 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)) {
2454 bgp_process_main_one(bgp, NULL, 0, 0);
60466a63
QY
2455 /* should always have dedicated wq call */
2456 assert(STAILQ_FIRST(&pqnode->pqueue) == NULL);
aac24838
JB
2457 return WQ_SUCCESS;
2458 }
2459
ac021f40
LB
2460 while (!STAILQ_EMPTY(&pqnode->pqueue)) {
2461 rn = STAILQ_FIRST(&pqnode->pqueue);
2462 STAILQ_REMOVE_HEAD(&pqnode->pqueue, pq);
b3e1dd3c 2463 STAILQ_NEXT(rn, pq) = NULL; /* complete unlink */
aac24838 2464 table = bgp_node_table(rn);
ac021f40 2465 /* note, new RNs may be added as part of processing */
aac24838 2466 bgp_process_main_one(bgp, rn, table->afi, table->safi);
cb1faec9 2467
aac24838 2468 bgp_unlock_node(rn);
d62a17ae 2469 bgp_table_unlock(table);
2470 }
aac24838
JB
2471
2472 return WQ_SUCCESS;
2473}
2474
2475static void bgp_processq_del(struct work_queue *wq, void *data)
2476{
2477 struct bgp_process_queue *pqnode = data;
2478
2479 bgp_unlock(pqnode->bgp);
2480
2481 XFREE(MTYPE_BGP_PROCESS_QUEUE, pqnode);
200df115 2482}
2483
d62a17ae 2484void bgp_process_queue_init(void)
200df115 2485{
0ce1ca80 2486 if (!bm->process_main_queue)
d62a17ae 2487 bm->process_main_queue =
2488 work_queue_new(bm->master, "process_main_queue");
2489
aac24838 2490 bm->process_main_queue->spec.workfunc = &bgp_process_wq;
d62a17ae 2491 bm->process_main_queue->spec.del_item_data = &bgp_processq_del;
2492 bm->process_main_queue->spec.max_retries = 0;
2493 bm->process_main_queue->spec.hold = 50;
2494 /* Use a higher yield value of 50ms for main queue processing */
2495 bm->process_main_queue->spec.yield = 50 * 1000L;
200df115 2496}
2497
cfe8d15a 2498static struct bgp_process_queue *bgp_processq_alloc(struct bgp *bgp)
aac24838
JB
2499{
2500 struct bgp_process_queue *pqnode;
2501
a4d82a8a
PZ
2502 pqnode = XCALLOC(MTYPE_BGP_PROCESS_QUEUE,
2503 sizeof(struct bgp_process_queue));
aac24838
JB
2504
2505 /* unlocked in bgp_processq_del */
2506 pqnode->bgp = bgp_lock(bgp);
2507 STAILQ_INIT(&pqnode->pqueue);
2508
aac24838
JB
2509 return pqnode;
2510}
2511
d62a17ae 2512void bgp_process(struct bgp *bgp, struct bgp_node *rn, afi_t afi, safi_t safi)
fee0f4c6 2513{
aac24838
JB
2514#define ARBITRARY_PROCESS_QLEN 10000
2515 struct work_queue *wq = bm->process_main_queue;
d62a17ae 2516 struct bgp_process_queue *pqnode;
cfe8d15a 2517 int pqnode_reuse = 0;
495f0b13 2518
d62a17ae 2519 /* already scheduled for processing? */
2520 if (CHECK_FLAG(rn->flags, BGP_NODE_PROCESS_SCHEDULED))
2521 return;
2e02b9b2 2522
aac24838 2523 if (wq == NULL)
d62a17ae 2524 return;
2525
aac24838 2526 /* Add route nodes to an existing work queue item until reaching the
a4d82a8a
PZ
2527 limit only if is from the same BGP view and it's not an EOIU marker
2528 */
aac24838
JB
2529 if (work_queue_item_count(wq)) {
2530 struct work_queue_item *item = work_queue_last_item(wq);
2531 pqnode = item->data;
228da428 2532
a4d82a8a
PZ
2533 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)
2534 || pqnode->bgp != bgp
2535 || pqnode->queued >= ARBITRARY_PROCESS_QLEN)
cfe8d15a
LB
2536 pqnode = bgp_processq_alloc(bgp);
2537 else
2538 pqnode_reuse = 1;
aac24838 2539 } else
cfe8d15a 2540 pqnode = bgp_processq_alloc(bgp);
aac24838 2541 /* all unlocked in bgp_process_wq */
d62a17ae 2542 bgp_table_lock(bgp_node_table(rn));
aac24838 2543
d62a17ae 2544 SET_FLAG(rn->flags, BGP_NODE_PROCESS_SCHEDULED);
aac24838
JB
2545 bgp_lock_node(rn);
2546
60466a63
QY
2547 /* can't be enqueued twice */
2548 assert(STAILQ_NEXT(rn, pq) == NULL);
aac24838
JB
2549 STAILQ_INSERT_TAIL(&pqnode->pqueue, rn, pq);
2550 pqnode->queued++;
2551
cfe8d15a
LB
2552 if (!pqnode_reuse)
2553 work_queue_add(wq, pqnode);
2554
d62a17ae 2555 return;
fee0f4c6 2556}
0a486e5f 2557
d62a17ae 2558void bgp_add_eoiu_mark(struct bgp *bgp)
cb1faec9 2559{
d62a17ae 2560 struct bgp_process_queue *pqnode;
cb1faec9 2561
d62a17ae 2562 if (bm->process_main_queue == NULL)
2563 return;
2e02b9b2 2564
cfe8d15a 2565 pqnode = bgp_processq_alloc(bgp);
cb1faec9 2566
aac24838 2567 SET_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER);
cfe8d15a 2568 work_queue_add(bm->process_main_queue, pqnode);
cb1faec9
DS
2569}
2570
d62a17ae 2571static int bgp_maximum_prefix_restart_timer(struct thread *thread)
0a486e5f 2572{
d62a17ae 2573 struct peer *peer;
0a486e5f 2574
d62a17ae 2575 peer = THREAD_ARG(thread);
2576 peer->t_pmax_restart = NULL;
0a486e5f 2577
d62a17ae 2578 if (bgp_debug_neighbor_events(peer))
2579 zlog_debug(
2580 "%s Maximum-prefix restart timer expired, restore peering",
2581 peer->host);
0a486e5f 2582
a9bafa95
DS
2583 if ((peer_clear(peer, NULL) < 0) && bgp_debug_neighbor_events(peer))
2584 zlog_debug("%s: %s peer_clear failed",
2585 __PRETTY_FUNCTION__, peer->host);
0a486e5f 2586
d62a17ae 2587 return 0;
0a486e5f 2588}
2589
d62a17ae 2590int bgp_maximum_prefix_overflow(struct peer *peer, afi_t afi, safi_t safi,
2591 int always)
718e3744 2592{
d62a17ae 2593 iana_afi_t pkt_afi;
5c525538 2594 iana_safi_t pkt_safi;
9cabb64b 2595
d62a17ae 2596 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
2597 return 0;
e0701b79 2598
d62a17ae 2599 if (peer->pcount[afi][safi] > peer->pmax[afi][safi]) {
2600 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2601 PEER_STATUS_PREFIX_LIMIT)
2602 && !always)
2603 return 0;
e0701b79 2604
d62a17ae 2605 zlog_info(
2606 "%%MAXPFXEXCEED: No. of %s prefix received from %s %ld exceed, "
2607 "limit %ld",
2608 afi_safi_print(afi, safi), peer->host,
2609 peer->pcount[afi][safi], peer->pmax[afi][safi]);
2610 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
2611
2612 if (CHECK_FLAG(peer->af_flags[afi][safi],
2613 PEER_FLAG_MAX_PREFIX_WARNING))
2614 return 0;
2615
2616 /* Convert AFI, SAFI to values for packet. */
2617 pkt_afi = afi_int2iana(afi);
2618 pkt_safi = safi_int2iana(safi);
2619 {
d7c0a89a 2620 uint8_t ndata[7];
d62a17ae 2621
2622 ndata[0] = (pkt_afi >> 8);
2623 ndata[1] = pkt_afi;
2624 ndata[2] = pkt_safi;
2625 ndata[3] = (peer->pmax[afi][safi] >> 24);
2626 ndata[4] = (peer->pmax[afi][safi] >> 16);
2627 ndata[5] = (peer->pmax[afi][safi] >> 8);
2628 ndata[6] = (peer->pmax[afi][safi]);
2629
2630 SET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
2631 bgp_notify_send_with_data(peer, BGP_NOTIFY_CEASE,
2632 BGP_NOTIFY_CEASE_MAX_PREFIX,
2633 ndata, 7);
2634 }
2635
2636 /* Dynamic peers will just close their connection. */
2637 if (peer_dynamic_neighbor(peer))
2638 return 1;
2639
2640 /* restart timer start */
2641 if (peer->pmax_restart[afi][safi]) {
2642 peer->v_pmax_restart =
2643 peer->pmax_restart[afi][safi] * 60;
2644
2645 if (bgp_debug_neighbor_events(peer))
2646 zlog_debug(
2647 "%s Maximum-prefix restart timer started for %d secs",
2648 peer->host, peer->v_pmax_restart);
2649
2650 BGP_TIMER_ON(peer->t_pmax_restart,
2651 bgp_maximum_prefix_restart_timer,
2652 peer->v_pmax_restart);
2653 }
2654
2655 return 1;
2656 } else
2657 UNSET_FLAG(peer->af_sflags[afi][safi],
2658 PEER_STATUS_PREFIX_LIMIT);
2659
2660 if (peer->pcount[afi][safi]
2661 > (peer->pmax[afi][safi] * peer->pmax_threshold[afi][safi] / 100)) {
2662 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2663 PEER_STATUS_PREFIX_THRESHOLD)
2664 && !always)
2665 return 0;
2666
2667 zlog_info(
2668 "%%MAXPFX: No. of %s prefix received from %s reaches %ld, max %ld",
2669 afi_safi_print(afi, safi), peer->host,
2670 peer->pcount[afi][safi], peer->pmax[afi][safi]);
2671 SET_FLAG(peer->af_sflags[afi][safi],
2672 PEER_STATUS_PREFIX_THRESHOLD);
2673 } else
2674 UNSET_FLAG(peer->af_sflags[afi][safi],
2675 PEER_STATUS_PREFIX_THRESHOLD);
2676 return 0;
718e3744 2677}
2678
b40d939b 2679/* Unconditionally remove the route from the RIB, without taking
2680 * damping into consideration (eg, because the session went down)
2681 */
40381db7 2682void bgp_rib_remove(struct bgp_node *rn, struct bgp_path_info *pi,
4b7e6066 2683 struct peer *peer, afi_t afi, safi_t safi)
718e3744 2684{
40381db7 2685 bgp_aggregate_decrement(peer->bgp, &rn->p, pi, afi, safi);
d62a17ae 2686
40381db7
DS
2687 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
2688 bgp_path_info_delete(rn, pi); /* keep historical info */
d62a17ae 2689
2690 bgp_process(peer->bgp, rn, afi, safi);
2691}
2692
40381db7 2693static void bgp_rib_withdraw(struct bgp_node *rn, struct bgp_path_info *pi,
d62a17ae 2694 struct peer *peer, afi_t afi, safi_t safi,
2695 struct prefix_rd *prd)
2696{
d62a17ae 2697 /* apply dampening, if result is suppressed, we'll be retaining
18ee8310 2698 * the bgp_path_info in the RIB for historical reference.
d62a17ae 2699 */
2700 if (CHECK_FLAG(peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
2701 && peer->sort == BGP_PEER_EBGP)
40381db7 2702 if ((bgp_damp_withdraw(pi, rn, afi, safi, 0))
d62a17ae 2703 == BGP_DAMP_SUPPRESSED) {
40381db7 2704 bgp_aggregate_decrement(peer->bgp, &rn->p, pi, afi,
d62a17ae 2705 safi);
2706 return;
2707 }
2708
65efcfce 2709#if ENABLE_BGP_VNC
d62a17ae 2710 if (safi == SAFI_MPLS_VPN) {
2711 struct bgp_node *prn = NULL;
2712 struct bgp_table *table = NULL;
2713
2714 prn = bgp_node_get(peer->bgp->rib[afi][safi],
2715 (struct prefix *)prd);
2716 if (prn->info) {
2717 table = (struct bgp_table *)(prn->info);
2718
2719 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
40381db7 2720 peer->bgp, prd, table, &rn->p, pi);
d62a17ae 2721 }
2722 bgp_unlock_node(prn);
2723 }
2724 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
40381db7 2725 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
65efcfce 2726
40381db7 2727 vnc_import_bgp_del_route(peer->bgp, &rn->p, pi);
d62a17ae 2728 vnc_import_bgp_exterior_del_route(peer->bgp, &rn->p,
40381db7 2729 pi);
d62a17ae 2730 }
65efcfce 2731 }
d62a17ae 2732#endif
128ea8ab 2733
d62a17ae 2734 /* If this is an EVPN route, process for un-import. */
2735 if (safi == SAFI_EVPN)
40381db7 2736 bgp_evpn_unimport_route(peer->bgp, afi, safi, &rn->p, pi);
128ea8ab 2737
40381db7 2738 bgp_rib_remove(rn, pi, peer, afi, safi);
718e3744 2739}
2740
4b7e6066
DS
2741struct bgp_path_info *info_make(int type, int sub_type, unsigned short instance,
2742 struct peer *peer, struct attr *attr,
2743 struct bgp_node *rn)
fb018d25 2744{
4b7e6066 2745 struct bgp_path_info *new;
fb018d25 2746
d62a17ae 2747 /* Make new BGP info. */
4b7e6066 2748 new = XCALLOC(MTYPE_BGP_ROUTE, sizeof(struct bgp_path_info));
d62a17ae 2749 new->type = type;
2750 new->instance = instance;
2751 new->sub_type = sub_type;
2752 new->peer = peer;
2753 new->attr = attr;
2754 new->uptime = bgp_clock();
2755 new->net = rn;
2756 new->addpath_tx_id = ++peer->bgp->addpath_tx_id;
2757 return new;
fb018d25
DS
2758}
2759
d62a17ae 2760static void overlay_index_update(struct attr *attr,
2761 struct eth_segment_id *eth_s_id,
2762 union gw_addr *gw_ip)
684a7227 2763{
d62a17ae 2764 if (!attr)
2765 return;
684a7227 2766
d62a17ae 2767 if (eth_s_id == NULL) {
2768 memset(&(attr->evpn_overlay.eth_s_id), 0,
2769 sizeof(struct eth_segment_id));
2770 } else {
2771 memcpy(&(attr->evpn_overlay.eth_s_id), eth_s_id,
2772 sizeof(struct eth_segment_id));
2773 }
2774 if (gw_ip == NULL) {
2775 memset(&(attr->evpn_overlay.gw_ip), 0, sizeof(union gw_addr));
2776 } else {
2777 memcpy(&(attr->evpn_overlay.gw_ip), gw_ip,
2778 sizeof(union gw_addr));
2779 }
684a7227
PG
2780}
2781
40381db7 2782static bool overlay_index_equal(afi_t afi, struct bgp_path_info *path,
d62a17ae 2783 struct eth_segment_id *eth_s_id,
2784 union gw_addr *gw_ip)
2785{
40381db7
DS
2786 struct eth_segment_id *path_eth_s_id, *path_eth_s_id_remote;
2787 union gw_addr *path_gw_ip, *path_gw_ip_remote;
11ebf4ed
DS
2788 union {
2789 struct eth_segment_id esi;
2790 union gw_addr ip;
2791 } temp;
d62a17ae 2792
2793 if (afi != AFI_L2VPN)
2794 return true;
40381db7 2795 if (!path->attr) {
11ebf4ed 2796 memset(&temp, 0, sizeof(temp));
40381db7
DS
2797 path_eth_s_id = &temp.esi;
2798 path_gw_ip = &temp.ip;
11ebf4ed 2799
d62a17ae 2800 if (eth_s_id == NULL && gw_ip == NULL)
2801 return true;
2802 } else {
40381db7
DS
2803 path_eth_s_id = &(path->attr->evpn_overlay.eth_s_id);
2804 path_gw_ip = &(path->attr->evpn_overlay.gw_ip);
d62a17ae 2805 }
11ebf4ed
DS
2806
2807 if (gw_ip == NULL) {
2808 memset(&temp, 0, sizeof(temp));
40381db7 2809 path_gw_ip_remote = &temp.ip;
11ebf4ed 2810 } else
40381db7 2811 path_gw_ip_remote = gw_ip;
11ebf4ed
DS
2812
2813 if (eth_s_id == NULL) {
2814 memset(&temp, 0, sizeof(temp));
40381db7 2815 path_eth_s_id_remote = &temp.esi;
11ebf4ed 2816 } else
40381db7 2817 path_eth_s_id_remote = eth_s_id;
11ebf4ed 2818
40381db7 2819 if (!memcmp(path_gw_ip, path_gw_ip_remote, sizeof(union gw_addr)))
d62a17ae 2820 return false;
11ebf4ed 2821
40381db7 2822 return !memcmp(path_eth_s_id, path_eth_s_id_remote,
d62a17ae 2823 sizeof(struct eth_segment_id));
684a7227
PG
2824}
2825
c265ee22 2826/* Check if received nexthop is valid or not. */
d62a17ae 2827static int bgp_update_martian_nexthop(struct bgp *bgp, afi_t afi, safi_t safi,
2828 struct attr *attr)
2829{
2830 int ret = 0;
2831
2832 /* Only validated for unicast and multicast currently. */
2833 /* Also valid for EVPN where the nexthop is an IP address. */
2834 if (safi != SAFI_UNICAST && safi != SAFI_MULTICAST && safi != SAFI_EVPN)
2835 return 0;
2836
2837 /* If NEXT_HOP is present, validate it. */
2838 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
2839 if (attr->nexthop.s_addr == 0
2840 || IPV4_CLASS_DE(ntohl(attr->nexthop.s_addr))
2841 || bgp_nexthop_self(bgp, attr->nexthop))
2842 return 1;
2843 }
c265ee22 2844
d62a17ae 2845 /* If MP_NEXTHOP is present, validate it. */
2846 /* Note: For IPv6 nexthops, we only validate the global (1st) nexthop;
2847 * there is code in bgp_attr.c to ignore the link-local (2nd) nexthop if
2848 * it is not an IPv6 link-local address.
2849 */
2850 if (attr->mp_nexthop_len) {
2851 switch (attr->mp_nexthop_len) {
2852 case BGP_ATTR_NHLEN_IPV4:
2853 case BGP_ATTR_NHLEN_VPNV4:
2854 ret = (attr->mp_nexthop_global_in.s_addr == 0
2855 || IPV4_CLASS_DE(ntohl(
2856 attr->mp_nexthop_global_in.s_addr))
2857 || bgp_nexthop_self(bgp,
2858 attr->mp_nexthop_global_in));
2859 break;
2860
2861 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
2862 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
2863 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
2864 ret = (IN6_IS_ADDR_UNSPECIFIED(&attr->mp_nexthop_global)
2865 || IN6_IS_ADDR_LOOPBACK(&attr->mp_nexthop_global)
2866 || IN6_IS_ADDR_MULTICAST(
2867 &attr->mp_nexthop_global));
2868 break;
2869
2870 default:
2871 ret = 1;
2872 break;
2873 }
2874 }
c265ee22 2875
d62a17ae 2876 return ret;
2877}
2878
d7c0a89a 2879int bgp_update(struct peer *peer, struct prefix *p, uint32_t addpath_id,
d62a17ae 2880 struct attr *attr, afi_t afi, safi_t safi, int type,
a4d82a8a 2881 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a 2882 uint32_t num_labels, int soft_reconfig,
a4d82a8a 2883 struct bgp_route_evpn *evpn)
d62a17ae 2884{
2885 int ret;
2886 int aspath_loop_count = 0;
2887 struct bgp_node *rn;
2888 struct bgp *bgp;
2889 struct attr new_attr;
2890 struct attr *attr_new;
40381db7 2891 struct bgp_path_info *pi;
4b7e6066
DS
2892 struct bgp_path_info *new;
2893 struct bgp_path_info_extra *extra;
d62a17ae 2894 const char *reason;
2895 char pfx_buf[BGP_PRD_PATH_STRLEN];
d62a17ae 2896 int connected = 0;
2897 int do_loop_check = 1;
2898 int has_valid_label = 0;
65efcfce 2899#if ENABLE_BGP_VNC
d62a17ae 2900 int vnc_implicit_withdraw = 0;
65efcfce 2901#endif
d62a17ae 2902 int same_attr = 0;
718e3744 2903
d62a17ae 2904 memset(&new_attr, 0, sizeof(struct attr));
2905 new_attr.label_index = BGP_INVALID_LABEL_INDEX;
2906 new_attr.label = MPLS_INVALID_LABEL;
f9a3a260 2907
d62a17ae 2908 bgp = peer->bgp;
2909 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
b57ba6d2
MK
2910 /* TODO: Check to see if we can get rid of "is_valid_label" */
2911 if (afi == AFI_L2VPN && safi == SAFI_EVPN)
2912 has_valid_label = (num_labels > 0) ? 1 : 0;
2913 else
2914 has_valid_label = bgp_is_valid_label(label);
718e3744 2915
d62a17ae 2916 /* When peer's soft reconfiguration enabled. Record input packet in
2917 Adj-RIBs-In. */
2918 if (!soft_reconfig
2919 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
2920 && peer != bgp->peer_self)
2921 bgp_adj_in_set(rn, peer, attr, addpath_id);
2922
2923 /* Check previously received route. */
40381db7
DS
2924 for (pi = rn->info; pi; pi = pi->next)
2925 if (pi->peer == peer && pi->type == type
2926 && pi->sub_type == sub_type
2927 && pi->addpath_rx_id == addpath_id)
d62a17ae 2928 break;
2929
2930 /* AS path local-as loop check. */
2931 if (peer->change_local_as) {
c4368918
DW
2932 if (peer->allowas_in[afi][safi])
2933 aspath_loop_count = peer->allowas_in[afi][safi];
a4d82a8a
PZ
2934 else if (!CHECK_FLAG(peer->flags,
2935 PEER_FLAG_LOCAL_AS_NO_PREPEND))
d62a17ae 2936 aspath_loop_count = 1;
2937
2938 if (aspath_loop_check(attr->aspath, peer->change_local_as)
2939 > aspath_loop_count) {
2940 reason = "as-path contains our own AS;";
2941 goto filtered;
2942 }
718e3744 2943 }
718e3744 2944
d62a17ae 2945 /* If the peer is configured for "allowas-in origin" and the last ASN in
2946 * the
2947 * as-path is our ASN then we do not need to call aspath_loop_check
2948 */
2949 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN_ORIGIN))
2950 if (aspath_get_last_as(attr->aspath) == bgp->as)
2951 do_loop_check = 0;
2952
2953 /* AS path loop check. */
2954 if (do_loop_check) {
2955 if (aspath_loop_check(attr->aspath, bgp->as)
2956 > peer->allowas_in[afi][safi]
2957 || (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)
2958 && aspath_loop_check(attr->aspath, bgp->confed_id)
2959 > peer->allowas_in[afi][safi])) {
2960 reason = "as-path contains our own AS;";
2961 goto filtered;
2962 }
2963 }
aac9ef6c 2964
d62a17ae 2965 /* Route reflector originator ID check. */
2966 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
2967 && IPV4_ADDR_SAME(&bgp->router_id, &attr->originator_id)) {
2968 reason = "originator is us;";
2969 goto filtered;
2970 }
718e3744 2971
d62a17ae 2972 /* Route reflector cluster ID check. */
2973 if (bgp_cluster_filter(peer, attr)) {
2974 reason = "reflected from the same cluster;";
2975 goto filtered;
2976 }
718e3744 2977
d62a17ae 2978 /* Apply incoming filter. */
2979 if (bgp_input_filter(peer, p, attr, afi, safi) == FILTER_DENY) {
2980 reason = "filter;";
2981 goto filtered;
2982 }
718e3744 2983
d62a17ae 2984 bgp_attr_dup(&new_attr, attr);
2985
2986 /* Apply incoming route-map.
2987 * NB: new_attr may now contain newly allocated values from route-map
2988 * "set"
2989 * commands, so we need bgp_attr_flush in the error paths, until we
2990 * intern
2991 * the attr (which takes over the memory references) */
2992 if (bgp_input_modifier(peer, p, &new_attr, afi, safi, NULL)
2993 == RMAP_DENY) {
2994 reason = "route-map;";
2995 bgp_attr_flush(&new_attr);
2996 goto filtered;
2997 }
718e3744 2998
7f323236
DW
2999 if (peer->sort == BGP_PEER_EBGP) {
3000
a4d82a8a
PZ
3001 /* If we receive the graceful-shutdown community from an eBGP
3002 * peer we must lower local-preference */
3003 if (new_attr.community
3004 && community_include(new_attr.community, COMMUNITY_GSHUT)) {
7f323236
DW
3005 new_attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
3006 new_attr.local_pref = BGP_GSHUT_LOCAL_PREF;
3007
a4d82a8a
PZ
3008 /* If graceful-shutdown is configured then add the GSHUT
3009 * community to all paths received from eBGP peers */
3010 } else if (bgp_flag_check(peer->bgp,
3011 BGP_FLAG_GRACEFUL_SHUTDOWN)) {
7f323236
DW
3012 bgp_attr_add_gshut_community(&new_attr);
3013 }
3014 }
3015
d62a17ae 3016 /* next hop check. */
a4d82a8a
PZ
3017 if (!CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD)
3018 && bgp_update_martian_nexthop(bgp, afi, safi, &new_attr)) {
d62a17ae 3019 reason = "martian or self next-hop;";
3020 bgp_attr_flush(&new_attr);
3021 goto filtered;
3022 }
718e3744 3023
d62a17ae 3024 attr_new = bgp_attr_intern(&new_attr);
3025
3026 /* If the update is implicit withdraw. */
40381db7
DS
3027 if (pi) {
3028 pi->uptime = bgp_clock();
3029 same_attr = attrhash_cmp(pi->attr, attr_new);
d62a17ae 3030
3031 /* Same attribute comes in. */
40381db7
DS
3032 if (!CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
3033 && attrhash_cmp(pi->attr, attr_new)
d62a17ae 3034 && (!has_valid_label
40381db7 3035 || memcmp(&(bgp_path_info_extra_get(pi))->label, label,
b57ba6d2 3036 num_labels * sizeof(mpls_label_t))
d62a17ae 3037 == 0)
3038 && (overlay_index_equal(
40381db7 3039 afi, pi, evpn == NULL ? NULL : &evpn->eth_s_id,
d62a17ae 3040 evpn == NULL ? NULL : &evpn->gw_ip))) {
3041 if (CHECK_FLAG(bgp->af_flags[afi][safi],
3042 BGP_CONFIG_DAMPENING)
3043 && peer->sort == BGP_PEER_EBGP
40381db7 3044 && CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
d62a17ae 3045 if (bgp_debug_update(peer, p, NULL, 1)) {
3046 bgp_debug_rdpfxpath2str(
a4d82a8a
PZ
3047 afi, safi, prd, p, label,
3048 num_labels, addpath_id ? 1 : 0,
3049 addpath_id, pfx_buf,
3050 sizeof(pfx_buf));
d62a17ae 3051 zlog_debug("%s rcvd %s", peer->host,
3052 pfx_buf);
3053 }
3054
40381db7 3055 if (bgp_damp_update(pi, rn, afi, safi)
d62a17ae 3056 != BGP_DAMP_SUPPRESSED) {
40381db7 3057 bgp_aggregate_increment(bgp, p, pi, afi,
d62a17ae 3058 safi);
3059 bgp_process(bgp, rn, afi, safi);
3060 }
3061 } else /* Duplicate - odd */
3062 {
3063 if (bgp_debug_update(peer, p, NULL, 1)) {
3064 if (!peer->rcvd_attr_printed) {
3065 zlog_debug(
3066 "%s rcvd UPDATE w/ attr: %s",
3067 peer->host,
3068 peer->rcvd_attr_str);
3069 peer->rcvd_attr_printed = 1;
3070 }
3071
3072 bgp_debug_rdpfxpath2str(
a4d82a8a
PZ
3073 afi, safi, prd, p, label,
3074 num_labels, addpath_id ? 1 : 0,
3075 addpath_id, pfx_buf,
3076 sizeof(pfx_buf));
d62a17ae 3077 zlog_debug(
3078 "%s rcvd %s...duplicate ignored",
3079 peer->host, pfx_buf);
3080 }
3081
3082 /* graceful restart STALE flag unset. */
40381db7 3083 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE)) {
18ee8310 3084 bgp_path_info_unset_flag(
40381db7 3085 rn, pi, BGP_PATH_STALE);
d62a17ae 3086 bgp_process(bgp, rn, afi, safi);
3087 }
3088 }
3089
3090 bgp_unlock_node(rn);
3091 bgp_attr_unintern(&attr_new);
3092
3093 return 0;
3094 }
718e3744 3095
d62a17ae 3096 /* Withdraw/Announce before we fully processed the withdraw */
40381db7 3097 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
d62a17ae 3098 if (bgp_debug_update(peer, p, NULL, 1)) {
3099 bgp_debug_rdpfxpath2str(
a4d82a8a 3100 afi, safi, prd, p, label, num_labels,
d62a17ae 3101 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3102 sizeof(pfx_buf));
3103 zlog_debug(
3104 "%s rcvd %s, flapped quicker than processing",
3105 peer->host, pfx_buf);
3106 }
3107
40381db7 3108 bgp_path_info_restore(rn, pi);
d62a17ae 3109 }
718e3744 3110
d62a17ae 3111 /* Received Logging. */
3112 if (bgp_debug_update(peer, p, NULL, 1)) {
a4d82a8a
PZ
3113 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label,
3114 num_labels, addpath_id ? 1 : 0,
3115 addpath_id, pfx_buf,
3116 sizeof(pfx_buf));
d62a17ae 3117 zlog_debug("%s rcvd %s", peer->host, pfx_buf);
3118 }
718e3744 3119
d62a17ae 3120 /* graceful restart STALE flag unset. */
40381db7
DS
3121 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE))
3122 bgp_path_info_unset_flag(rn, pi, BGP_PATH_STALE);
d62a17ae 3123
3124 /* The attribute is changed. */
40381db7 3125 bgp_path_info_set_flag(rn, pi, BGP_PATH_ATTR_CHANGED);
d62a17ae 3126
3127 /* implicit withdraw, decrement aggregate and pcount here.
3128 * only if update is accepted, they'll increment below.
3129 */
40381db7 3130 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
d62a17ae 3131
3132 /* Update bgp route dampening information. */
3133 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
3134 && peer->sort == BGP_PEER_EBGP) {
3135 /* This is implicit withdraw so we should update
3136 dampening
3137 information. */
40381db7
DS
3138 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
3139 bgp_damp_withdraw(pi, rn, afi, safi, 1);
d62a17ae 3140 }
65efcfce 3141#if ENABLE_BGP_VNC
d62a17ae 3142 if (safi == SAFI_MPLS_VPN) {
3143 struct bgp_node *prn = NULL;
3144 struct bgp_table *table = NULL;
3145
3146 prn = bgp_node_get(bgp->rib[afi][safi],
3147 (struct prefix *)prd);
3148 if (prn->info) {
3149 table = (struct bgp_table *)(prn->info);
3150
3151 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
40381db7 3152 bgp, prd, table, p, pi);
d62a17ae 3153 }
3154 bgp_unlock_node(prn);
3155 }
3156 if ((afi == AFI_IP || afi == AFI_IP6)
3157 && (safi == SAFI_UNICAST)) {
40381db7 3158 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
d62a17ae 3159 /*
3160 * Implicit withdraw case.
3161 */
3162 ++vnc_implicit_withdraw;
40381db7
DS
3163 vnc_import_bgp_del_route(bgp, p, pi);
3164 vnc_import_bgp_exterior_del_route(bgp, p, pi);
d62a17ae 3165 }
3166 }
65efcfce 3167#endif
128ea8ab 3168
d62a17ae 3169 /* Special handling for EVPN update of an existing route. If the
3170 * extended community attribute has changed, we need to
3171 * un-import
3172 * the route using its existing extended community. It will be
3173 * subsequently processed for import with the new extended
3174 * community.
3175 */
3176 if (safi == SAFI_EVPN && !same_attr) {
40381db7 3177 if ((pi->attr->flag
d62a17ae 3178 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))
3179 && (attr_new->flag
3180 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) {
3181 int cmp;
3182
40381db7 3183 cmp = ecommunity_cmp(pi->attr->ecommunity,
d62a17ae 3184 attr_new->ecommunity);
3185 if (!cmp) {
3186 if (bgp_debug_update(peer, p, NULL, 1))
3187 zlog_debug(
3188 "Change in EXT-COMM, existing %s new %s",
3189 ecommunity_str(
40381db7 3190 pi->attr->ecommunity),
d62a17ae 3191 ecommunity_str(
3192 attr_new->ecommunity));
3193 bgp_evpn_unimport_route(bgp, afi, safi,
40381db7 3194 p, pi);
d62a17ae 3195 }
3196 }
3197 }
718e3744 3198
d62a17ae 3199 /* Update to new attribute. */
40381db7
DS
3200 bgp_attr_unintern(&pi->attr);
3201 pi->attr = attr_new;
d62a17ae 3202
3203 /* Update MPLS label */
3204 if (has_valid_label) {
40381db7 3205 extra = bgp_path_info_extra_get(pi);
b57ba6d2
MK
3206 memcpy(&extra->label, label,
3207 num_labels * sizeof(mpls_label_t));
3208 extra->num_labels = num_labels;
3209 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
3210 bgp_set_valid_label(&extra->label[0]);
fc9a856f 3211 }
718e3744 3212
65efcfce 3213#if ENABLE_BGP_VNC
d62a17ae 3214 if ((afi == AFI_IP || afi == AFI_IP6)
3215 && (safi == SAFI_UNICAST)) {
3216 if (vnc_implicit_withdraw) {
3217 /*
3218 * Add back the route with its new attributes
3219 * (e.g., nexthop).
3220 * The route is still selected, until the route
3221 * selection
3222 * queued by bgp_process actually runs. We have
3223 * to make this
3224 * update to the VNC side immediately to avoid
3225 * racing against
3226 * configuration changes (e.g., route-map
3227 * changes) which
3228 * trigger re-importation of the entire RIB.
3229 */
40381db7
DS
3230 vnc_import_bgp_add_route(bgp, p, pi);
3231 vnc_import_bgp_exterior_add_route(bgp, p, pi);
d62a17ae 3232 }
3233 }
65efcfce 3234#endif
d62a17ae 3235 /* Update Overlay Index */
3236 if (afi == AFI_L2VPN) {
3237 overlay_index_update(
40381db7 3238 pi->attr, evpn == NULL ? NULL : &evpn->eth_s_id,
d62a17ae 3239 evpn == NULL ? NULL : &evpn->gw_ip);
3240 }
65efcfce 3241
d62a17ae 3242 /* Update bgp route dampening information. */
3243 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
3244 && peer->sort == BGP_PEER_EBGP) {
3245 /* Now we do normal update dampening. */
40381db7 3246 ret = bgp_damp_update(pi, rn, afi, safi);
d62a17ae 3247 if (ret == BGP_DAMP_SUPPRESSED) {
3248 bgp_unlock_node(rn);
3249 return 0;
3250 }
3251 }
128ea8ab 3252
d62a17ae 3253 /* Nexthop reachability check - for unicast and
3254 * labeled-unicast.. */
3255 if ((afi == AFI_IP || afi == AFI_IP6)
3256 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST)) {
3257 if (peer->sort == BGP_PEER_EBGP && peer->ttl == 1
3258 && !CHECK_FLAG(peer->flags,
3259 PEER_FLAG_DISABLE_CONNECTED_CHECK)
3260 && !bgp_flag_check(
3261 bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
3262 connected = 1;
3263 else
3264 connected = 0;
3265
960035b2
PZ
3266 struct bgp *bgp_nexthop = bgp;
3267
40381db7
DS
3268 if (pi->extra && pi->extra->bgp_orig)
3269 bgp_nexthop = pi->extra->bgp_orig;
960035b2 3270
40381db7
DS
3271 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop, afi, pi,
3272 NULL, connected)
a4d82a8a 3273 || CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
40381db7 3274 bgp_path_info_set_flag(rn, pi, BGP_PATH_VALID);
d62a17ae 3275 else {
3276 if (BGP_DEBUG(nht, NHT)) {
3277 char buf1[INET6_ADDRSTRLEN];
3278 inet_ntop(AF_INET,
3279 (const void *)&attr_new
3280 ->nexthop,
3281 buf1, INET6_ADDRSTRLEN);
3282 zlog_debug("%s(%s): NH unresolved",
3283 __FUNCTION__, buf1);
3284 }
40381db7 3285 bgp_path_info_unset_flag(rn, pi,
18ee8310 3286 BGP_PATH_VALID);
d62a17ae 3287 }
3288 } else
40381db7 3289 bgp_path_info_set_flag(rn, pi, BGP_PATH_VALID);
d62a17ae 3290
3291#if ENABLE_BGP_VNC
3292 if (safi == SAFI_MPLS_VPN) {
3293 struct bgp_node *prn = NULL;
3294 struct bgp_table *table = NULL;
3295
3296 prn = bgp_node_get(bgp->rib[afi][safi],
3297 (struct prefix *)prd);
3298 if (prn->info) {
3299 table = (struct bgp_table *)(prn->info);
3300
3301 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
40381db7 3302 bgp, prd, table, p, pi);
d62a17ae 3303 }
3304 bgp_unlock_node(prn);
3305 }
3306#endif
718e3744 3307
d62a17ae 3308 /* If this is an EVPN route and some attribute has changed,
3309 * process
3310 * route for import. If the extended community has changed, we
3311 * would
3312 * have done the un-import earlier and the import would result
3313 * in the
3314 * route getting injected into appropriate L2 VNIs. If it is
3315 * just
3316 * some other attribute change, the import will result in
3317 * updating
3318 * the attributes for the route in the VNI(s).
3319 */
3320 if (safi == SAFI_EVPN && !same_attr)
40381db7 3321 bgp_evpn_import_route(bgp, afi, safi, p, pi);
d62a17ae 3322
3323 /* Process change. */
40381db7 3324 bgp_aggregate_increment(bgp, p, pi, afi, safi);
d62a17ae 3325
3326 bgp_process(bgp, rn, afi, safi);
3327 bgp_unlock_node(rn);
558d1fec 3328
ddb5b488
PZ
3329 if (SAFI_UNICAST == safi
3330 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
3331 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3332
40381db7 3333 vpn_leak_from_vrf_update(bgp_get_default(), bgp, pi);
ddb5b488
PZ
3334 }
3335 if ((SAFI_MPLS_VPN == safi)
3336 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3337
40381db7 3338 vpn_leak_to_vrf_update(bgp, pi);
ddb5b488
PZ
3339 }
3340
28070ee3 3341#if ENABLE_BGP_VNC
d62a17ae 3342 if (SAFI_MPLS_VPN == safi) {
3343 mpls_label_t label_decoded = decode_label(label);
28070ee3 3344
d62a17ae 3345 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
3346 type, sub_type, &label_decoded);
3347 }
3348 if (SAFI_ENCAP == safi) {
3349 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
3350 type, sub_type, NULL);
3351 }
28070ee3
PZ
3352#endif
3353
d62a17ae 3354 return 0;
3355 } // End of implicit withdraw
718e3744 3356
d62a17ae 3357 /* Received Logging. */
3358 if (bgp_debug_update(peer, p, NULL, 1)) {
3359 if (!peer->rcvd_attr_printed) {
3360 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer->host,
3361 peer->rcvd_attr_str);
3362 peer->rcvd_attr_printed = 1;
3363 }
718e3744 3364
a4d82a8a 3365 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
d62a17ae 3366 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3367 sizeof(pfx_buf));
3368 zlog_debug("%s rcvd %s", peer->host, pfx_buf);
3369 }
718e3744 3370
d62a17ae 3371 /* Make new BGP info. */
3372 new = info_make(type, sub_type, 0, peer, attr_new, rn);
718e3744 3373
d62a17ae 3374 /* Update MPLS label */
3375 if (has_valid_label) {
18ee8310 3376 extra = bgp_path_info_extra_get(new);
a4d82a8a 3377 memcpy(&extra->label, label, num_labels * sizeof(mpls_label_t));
b57ba6d2
MK
3378 extra->num_labels = num_labels;
3379 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
3380 bgp_set_valid_label(&extra->label[0]);
fc9a856f 3381 }
718e3744 3382
d62a17ae 3383 /* Update Overlay Index */
3384 if (afi == AFI_L2VPN) {
3385 overlay_index_update(new->attr,
3386 evpn == NULL ? NULL : &evpn->eth_s_id,
3387 evpn == NULL ? NULL : &evpn->gw_ip);
3388 }
3389 /* Nexthop reachability check. */
3390 if ((afi == AFI_IP || afi == AFI_IP6)
3391 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST)) {
3392 if (peer->sort == BGP_PEER_EBGP && peer->ttl == 1
3393 && !CHECK_FLAG(peer->flags,
3394 PEER_FLAG_DISABLE_CONNECTED_CHECK)
3395 && !bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
3396 connected = 1;
3397 else
3398 connected = 0;
3399
960035b2 3400 if (bgp_find_or_add_nexthop(bgp, bgp, afi, new, NULL, connected)
a4d82a8a 3401 || CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
18ee8310 3402 bgp_path_info_set_flag(rn, new, BGP_PATH_VALID);
d62a17ae 3403 else {
3404 if (BGP_DEBUG(nht, NHT)) {
3405 char buf1[INET6_ADDRSTRLEN];
3406 inet_ntop(AF_INET,
3407 (const void *)&attr_new->nexthop,
3408 buf1, INET6_ADDRSTRLEN);
3409 zlog_debug("%s(%s): NH unresolved",
3410 __FUNCTION__, buf1);
3411 }
18ee8310 3412 bgp_path_info_unset_flag(rn, new, BGP_PATH_VALID);
d62a17ae 3413 }
3414 } else
18ee8310 3415 bgp_path_info_set_flag(rn, new, BGP_PATH_VALID);
a82478b9 3416
d62a17ae 3417 /* Addpath ID */
3418 new->addpath_rx_id = addpath_id;
3419
3420 /* Increment prefix */
3421 bgp_aggregate_increment(bgp, p, new, afi, safi);
3422
3423 /* Register new BGP information. */
18ee8310 3424 bgp_path_info_add(rn, new);
d62a17ae 3425
3426 /* route_node_get lock */
3427 bgp_unlock_node(rn);
558d1fec 3428
65efcfce 3429#if ENABLE_BGP_VNC
d62a17ae 3430 if (safi == SAFI_MPLS_VPN) {
3431 struct bgp_node *prn = NULL;
3432 struct bgp_table *table = NULL;
3433
3434 prn = bgp_node_get(bgp->rib[afi][safi], (struct prefix *)prd);
3435 if (prn->info) {
3436 table = (struct bgp_table *)(prn->info);
3437
3438 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
3439 bgp, prd, table, p, new);
3440 }
3441 bgp_unlock_node(prn);
3442 }
65efcfce
LB
3443#endif
3444
d62a17ae 3445 /* If maximum prefix count is configured and current prefix
3446 count exeed it. */
3447 if (bgp_maximum_prefix_overflow(peer, afi, safi, 0))
3448 return -1;
718e3744 3449
d62a17ae 3450 /* If this is an EVPN route, process for import. */
3451 if (safi == SAFI_EVPN)
3452 bgp_evpn_import_route(bgp, afi, safi, p, new);
128ea8ab 3453
d62a17ae 3454 /* Process change. */
3455 bgp_process(bgp, rn, afi, safi);
718e3744 3456
ddb5b488
PZ
3457 if (SAFI_UNICAST == safi
3458 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
3459 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3460 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
3461 }
3462 if ((SAFI_MPLS_VPN == safi)
3463 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3464
3465 vpn_leak_to_vrf_update(bgp, new);
3466 }
28070ee3 3467#if ENABLE_BGP_VNC
d62a17ae 3468 if (SAFI_MPLS_VPN == safi) {
3469 mpls_label_t label_decoded = decode_label(label);
28070ee3 3470
d62a17ae 3471 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
3472 sub_type, &label_decoded);
3473 }
3474 if (SAFI_ENCAP == safi) {
3475 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
3476 sub_type, NULL);
3477 }
28070ee3
PZ
3478#endif
3479
d62a17ae 3480 return 0;
718e3744 3481
d62a17ae 3482/* This BGP update is filtered. Log the reason then update BGP
3483 entry. */
3484filtered:
3485 if (bgp_debug_update(peer, p, NULL, 1)) {
3486 if (!peer->rcvd_attr_printed) {
3487 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer->host,
3488 peer->rcvd_attr_str);
3489 peer->rcvd_attr_printed = 1;
3490 }
718e3744 3491
a4d82a8a 3492 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
d62a17ae 3493 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3494 sizeof(pfx_buf));
3495 zlog_debug("%s rcvd UPDATE about %s -- DENIED due to: %s",
3496 peer->host, pfx_buf, reason);
3497 }
128ea8ab 3498
40381db7 3499 if (pi) {
d62a17ae 3500 /* If this is an EVPN route, un-import it as it is now filtered.
3501 */
3502 if (safi == SAFI_EVPN)
40381db7 3503 bgp_evpn_unimport_route(bgp, afi, safi, p, pi);
718e3744 3504
ddb5b488
PZ
3505 if (SAFI_UNICAST == safi
3506 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
3507 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3508
40381db7 3509 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
ddb5b488
PZ
3510 }
3511 if ((SAFI_MPLS_VPN == safi)
3512 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3513
40381db7 3514 vpn_leak_to_vrf_withdraw(bgp, pi);
ddb5b488
PZ
3515 }
3516
40381db7 3517 bgp_rib_remove(rn, pi, peer, afi, safi);
d62a17ae 3518 }
3519
3520 bgp_unlock_node(rn);
558d1fec 3521
97736e32 3522#if ENABLE_BGP_VNC
d62a17ae 3523 /*
3524 * Filtered update is treated as an implicit withdrawal (see
3525 * bgp_rib_remove()
3526 * a few lines above)
3527 */
3528 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
3529 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
3530 0);
3531 }
97736e32
PZ
3532#endif
3533
d62a17ae 3534 return 0;
718e3744 3535}
3536
d7c0a89a 3537int bgp_withdraw(struct peer *peer, struct prefix *p, uint32_t addpath_id,
d62a17ae 3538 struct attr *attr, afi_t afi, safi_t safi, int type,
a4d82a8a 3539 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a 3540 uint32_t num_labels, struct bgp_route_evpn *evpn)
718e3744 3541{
d62a17ae 3542 struct bgp *bgp;
3543 char pfx_buf[BGP_PRD_PATH_STRLEN];
3544 struct bgp_node *rn;
40381db7 3545 struct bgp_path_info *pi;
718e3744 3546
28070ee3 3547#if ENABLE_BGP_VNC
d62a17ae 3548 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
3549 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
3550 0);
3551 }
28070ee3
PZ
3552#endif
3553
d62a17ae 3554 bgp = peer->bgp;
3555
3556 /* Lookup node. */
3557 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
3558
3559 /* If peer is soft reconfiguration enabled. Record input packet for
3560 * further calculation.
3561 *
3562 * Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
3563 * routes that are filtered. This tanks out Quagga RS pretty badly due
3564 * to
3565 * the iteration over all RS clients.
3566 * Since we need to remove the entry from adj_in anyway, do that first
3567 * and
3568 * if there was no entry, we don't need to do anything more.
3569 */
3570 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
3571 && peer != bgp->peer_self)
3572 if (!bgp_adj_in_unset(rn, peer, addpath_id)) {
3573 if (bgp_debug_update(peer, p, NULL, 1)) {
3574 bgp_debug_rdpfxpath2str(
a4d82a8a 3575 afi, safi, prd, p, label, num_labels,
d62a17ae 3576 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3577 sizeof(pfx_buf));
3578 zlog_debug(
3579 "%s withdrawing route %s not in adj-in",
3580 peer->host, pfx_buf);
3581 }
3582 bgp_unlock_node(rn);
3583 return 0;
3584 }
cd808e74 3585
d62a17ae 3586 /* Lookup withdrawn route. */
40381db7
DS
3587 for (pi = rn->info; pi; pi = pi->next)
3588 if (pi->peer == peer && pi->type == type
3589 && pi->sub_type == sub_type
3590 && pi->addpath_rx_id == addpath_id)
d62a17ae 3591 break;
3592
3593 /* Logging. */
3594 if (bgp_debug_update(peer, p, NULL, 1)) {
a4d82a8a 3595 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
d62a17ae 3596 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3597 sizeof(pfx_buf));
3598 zlog_debug("%s rcvd UPDATE about %s -- withdrawn", peer->host,
3599 pfx_buf);
3600 }
718e3744 3601
d62a17ae 3602 /* Withdraw specified route from routing table. */
40381db7
DS
3603 if (pi && !CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
3604 bgp_rib_withdraw(rn, pi, peer, afi, safi, prd);
ddb5b488
PZ
3605 if (SAFI_UNICAST == safi
3606 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
3607 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
40381db7 3608 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
ddb5b488
PZ
3609 }
3610 if ((SAFI_MPLS_VPN == safi)
3611 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3612
40381db7 3613 vpn_leak_to_vrf_withdraw(bgp, pi);
ddb5b488
PZ
3614 }
3615 } else if (bgp_debug_update(peer, p, NULL, 1)) {
a4d82a8a 3616 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
d62a17ae 3617 addpath_id ? 1 : 0, addpath_id, pfx_buf,
3618 sizeof(pfx_buf));
3619 zlog_debug("%s Can't find the route %s", peer->host, pfx_buf);
3620 }
718e3744 3621
d62a17ae 3622 /* Unlock bgp_node_get() lock. */
3623 bgp_unlock_node(rn);
3624
3625 return 0;
718e3744 3626}
6b0655a2 3627
d62a17ae 3628void bgp_default_originate(struct peer *peer, afi_t afi, safi_t safi,
3629 int withdraw)
718e3744 3630{
d62a17ae 3631 struct update_subgroup *subgrp;
3632 subgrp = peer_subgroup(peer, afi, safi);
3633 subgroup_default_originate(subgrp, withdraw);
3f9c7369 3634}
6182d65b 3635
718e3744 3636
3f9c7369
DS
3637/*
3638 * bgp_stop_announce_route_timer
3639 */
d62a17ae 3640void bgp_stop_announce_route_timer(struct peer_af *paf)
3f9c7369 3641{
d62a17ae 3642 if (!paf->t_announce_route)
3643 return;
3644
3645 THREAD_TIMER_OFF(paf->t_announce_route);
718e3744 3646}
6b0655a2 3647
3f9c7369
DS
3648/*
3649 * bgp_announce_route_timer_expired
3650 *
3651 * Callback that is invoked when the route announcement timer for a
3652 * peer_af expires.
3653 */
d62a17ae 3654static int bgp_announce_route_timer_expired(struct thread *t)
718e3744 3655{
d62a17ae 3656 struct peer_af *paf;
3657 struct peer *peer;
558d1fec 3658
d62a17ae 3659 paf = THREAD_ARG(t);
3660 peer = paf->peer;
718e3744 3661
d62a17ae 3662 if (peer->status != Established)
3663 return 0;
3f9c7369 3664
d62a17ae 3665 if (!peer->afc_nego[paf->afi][paf->safi])
3666 return 0;
3f9c7369 3667
d62a17ae 3668 peer_af_announce_route(paf, 1);
3669 return 0;
718e3744 3670}
3671
3f9c7369
DS
3672/*
3673 * bgp_announce_route
3674 *
3675 * *Triggers* announcement of routes of a given AFI/SAFI to a peer.
3676 */
d62a17ae 3677void bgp_announce_route(struct peer *peer, afi_t afi, safi_t safi)
3678{
3679 struct peer_af *paf;
3680 struct update_subgroup *subgrp;
3681
3682 paf = peer_af_find(peer, afi, safi);
3683 if (!paf)
3684 return;
3685 subgrp = PAF_SUBGRP(paf);
3686
3687 /*
3688 * Ignore if subgroup doesn't exist (implies AF is not negotiated)
3689 * or a refresh has already been triggered.
3690 */
3691 if (!subgrp || paf->t_announce_route)
3692 return;
3693
3694 /*
3695 * Start a timer to stagger/delay the announce. This serves
3696 * two purposes - announcement can potentially be combined for
3697 * multiple peers and the announcement doesn't happen in the
3698 * vty context.
3699 */
3700 thread_add_timer_msec(bm->master, bgp_announce_route_timer_expired, paf,
3701 (subgrp->peer_count == 1)
3702 ? BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS
3703 : BGP_ANNOUNCE_ROUTE_DELAY_MS,
3704 &paf->t_announce_route);
3f9c7369
DS
3705}
3706
3707/*
3708 * Announce routes from all AF tables to a peer.
3709 *
3710 * This should ONLY be called when there is a need to refresh the
3711 * routes to the peer based on a policy change for this peer alone
3712 * or a route refresh request received from the peer.
3713 * The operation will result in splitting the peer from its existing
3714 * subgroups and putting it in new subgroups.
3715 */
d62a17ae 3716void bgp_announce_route_all(struct peer *peer)
718e3744 3717{
d62a17ae 3718 afi_t afi;
3719 safi_t safi;
3720
05c7a1cc
QY
3721 FOREACH_AFI_SAFI (afi, safi)
3722 bgp_announce_route(peer, afi, safi);
718e3744 3723}
6b0655a2 3724
d62a17ae 3725static void bgp_soft_reconfig_table(struct peer *peer, afi_t afi, safi_t safi,
3726 struct bgp_table *table,
3727 struct prefix_rd *prd)
718e3744 3728{
d62a17ae 3729 int ret;
3730 struct bgp_node *rn;
3731 struct bgp_adj_in *ain;
718e3744 3732
d62a17ae 3733 if (!table)
3734 table = peer->bgp->rib[afi][safi];
718e3744 3735
d62a17ae 3736 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
3737 for (ain = rn->adj_in; ain; ain = ain->next) {
ea47320b
DL
3738 if (ain->peer != peer)
3739 continue;
8692c506 3740
40381db7 3741 struct bgp_path_info *pi = rn->info;
d7c0a89a 3742 uint32_t num_labels = 0;
b57ba6d2
MK
3743 mpls_label_t *label_pnt = NULL;
3744
40381db7
DS
3745 if (pi && pi->extra)
3746 num_labels = pi->extra->num_labels;
b57ba6d2 3747 if (num_labels)
40381db7 3748 label_pnt = &pi->extra->label[0];
8692c506 3749
ea47320b
DL
3750 ret = bgp_update(peer, &rn->p, ain->addpath_rx_id,
3751 ain->attr, afi, safi, ZEBRA_ROUTE_BGP,
a4d82a8a
PZ
3752 BGP_ROUTE_NORMAL, prd, label_pnt,
3753 num_labels, 1, NULL);
ea47320b
DL
3754
3755 if (ret < 0) {
3756 bgp_unlock_node(rn);
3757 return;
d62a17ae 3758 }
3759 }
718e3744 3760}
3761
d62a17ae 3762void bgp_soft_reconfig_in(struct peer *peer, afi_t afi, safi_t safi)
718e3744 3763{
d62a17ae 3764 struct bgp_node *rn;
3765 struct bgp_table *table;
718e3744 3766
d62a17ae 3767 if (peer->status != Established)
3768 return;
718e3744 3769
d62a17ae 3770 if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP)
3771 && (safi != SAFI_EVPN))
3772 bgp_soft_reconfig_table(peer, afi, safi, NULL, NULL);
3773 else
3774 for (rn = bgp_table_top(peer->bgp->rib[afi][safi]); rn;
3775 rn = bgp_route_next(rn))
3776 if ((table = rn->info) != NULL) {
3777 struct prefix_rd prd;
3778 prd.family = AF_UNSPEC;
3779 prd.prefixlen = 64;
3780 memcpy(&prd.val, rn->p.u.val, 8);
8692c506 3781
d62a17ae 3782 bgp_soft_reconfig_table(peer, afi, safi, table,
3783 &prd);
3784 }
718e3744 3785}
6b0655a2 3786
228da428 3787
d62a17ae 3788struct bgp_clear_node_queue {
3789 struct bgp_node *rn;
228da428
CC
3790};
3791
d62a17ae 3792static wq_item_status bgp_clear_route_node(struct work_queue *wq, void *data)
200df115 3793{
d62a17ae 3794 struct bgp_clear_node_queue *cnq = data;
3795 struct bgp_node *rn = cnq->rn;
3796 struct peer *peer = wq->spec.data;
40381db7 3797 struct bgp_path_info *pi;
3103e8d2 3798 struct bgp *bgp;
d62a17ae 3799 afi_t afi = bgp_node_table(rn)->afi;
3800 safi_t safi = bgp_node_table(rn)->safi;
3801
3802 assert(rn && peer);
3103e8d2 3803 bgp = peer->bgp;
d62a17ae 3804
3805 /* It is possible that we have multiple paths for a prefix from a peer
3806 * if that peer is using AddPath.
3807 */
40381db7
DS
3808 for (pi = rn->info; pi; pi = pi->next) {
3809 if (pi->peer != peer)
ea47320b
DL
3810 continue;
3811
3812 /* graceful restart STALE flag set. */
3813 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)
3814 && peer->nsf[afi][safi]
40381db7
DS
3815 && !CHECK_FLAG(pi->flags, BGP_PATH_STALE)
3816 && !CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
3817 bgp_path_info_set_flag(rn, pi, BGP_PATH_STALE);
ea47320b
DL
3818 else {
3819 /* If this is an EVPN route, process for
3820 * un-import. */
3821 if (safi == SAFI_EVPN)
40381db7
DS
3822 bgp_evpn_unimport_route(bgp, afi, safi, &rn->p,
3823 pi);
3103e8d2
DS
3824 /* Handle withdraw for VRF route-leaking and L3VPN */
3825 if (SAFI_UNICAST == safi
3826 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF ||
960035b2 3827 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
3103e8d2 3828 vpn_leak_from_vrf_withdraw(bgp_get_default(),
40381db7 3829 bgp, pi);
960035b2 3830 }
3103e8d2 3831 if (SAFI_MPLS_VPN == safi &&
960035b2 3832 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
40381db7 3833 vpn_leak_to_vrf_withdraw(bgp, pi);
960035b2 3834 }
3103e8d2 3835
40381db7 3836 bgp_rib_remove(rn, pi, peer, afi, safi);
d62a17ae 3837 }
ea47320b 3838 }
d62a17ae 3839 return WQ_SUCCESS;
200df115 3840}
3841
d62a17ae 3842static void bgp_clear_node_queue_del(struct work_queue *wq, void *data)
200df115 3843{
d62a17ae 3844 struct bgp_clear_node_queue *cnq = data;
3845 struct bgp_node *rn = cnq->rn;
3846 struct bgp_table *table = bgp_node_table(rn);
228da428 3847
d62a17ae 3848 bgp_unlock_node(rn);
3849 bgp_table_unlock(table);
3850 XFREE(MTYPE_BGP_CLEAR_NODE_QUEUE, cnq);
200df115 3851}
3852
d62a17ae 3853static void bgp_clear_node_complete(struct work_queue *wq)
200df115 3854{
d62a17ae 3855 struct peer *peer = wq->spec.data;
64e580a7 3856
d62a17ae 3857 /* Tickle FSM to start moving again */
3858 BGP_EVENT_ADD(peer, Clearing_Completed);
3859
3860 peer_unlock(peer); /* bgp_clear_route */
200df115 3861}
718e3744 3862
d62a17ae 3863static void bgp_clear_node_queue_init(struct peer *peer)
200df115 3864{
d62a17ae 3865 char wname[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
3866
3867 snprintf(wname, sizeof(wname), "clear %s", peer->host);
3868#undef CLEAR_QUEUE_NAME_LEN
3869
0ce1ca80 3870 peer->clear_node_queue = work_queue_new(bm->master, wname);
d62a17ae 3871 peer->clear_node_queue->spec.hold = 10;
3872 peer->clear_node_queue->spec.workfunc = &bgp_clear_route_node;
3873 peer->clear_node_queue->spec.del_item_data = &bgp_clear_node_queue_del;
3874 peer->clear_node_queue->spec.completion_func = &bgp_clear_node_complete;
3875 peer->clear_node_queue->spec.max_retries = 0;
3876
3877 /* we only 'lock' this peer reference when the queue is actually active
3878 */
3879 peer->clear_node_queue->spec.data = peer;
65ca75e0
PJ
3880}
3881
d62a17ae 3882static void bgp_clear_route_table(struct peer *peer, afi_t afi, safi_t safi,
3883 struct bgp_table *table)
65ca75e0 3884{
d62a17ae 3885 struct bgp_node *rn;
3886 int force = bm->process_main_queue ? 0 : 1;
6cf159b9 3887
d62a17ae 3888 if (!table)
3889 table = peer->bgp->rib[afi][safi];
dc83d712 3890
d62a17ae 3891 /* If still no table => afi/safi isn't configured at all or smth. */
3892 if (!table)
3893 return;
dc83d712 3894
d62a17ae 3895 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
40381db7 3896 struct bgp_path_info *pi, *next;
d62a17ae 3897 struct bgp_adj_in *ain;
3898 struct bgp_adj_in *ain_next;
3899
3900 /* XXX:TODO: This is suboptimal, every non-empty route_node is
3901 * queued for every clearing peer, regardless of whether it is
3902 * relevant to the peer at hand.
3903 *
3904 * Overview: There are 3 different indices which need to be
3905 * scrubbed, potentially, when a peer is removed:
3906 *
3907 * 1 peer's routes visible via the RIB (ie accepted routes)
3908 * 2 peer's routes visible by the (optional) peer's adj-in index
3909 * 3 other routes visible by the peer's adj-out index
3910 *
3911 * 3 there is no hurry in scrubbing, once the struct peer is
3912 * removed from bgp->peer, we could just GC such deleted peer's
3913 * adj-outs at our leisure.
3914 *
3915 * 1 and 2 must be 'scrubbed' in some way, at least made
3916 * invisible via RIB index before peer session is allowed to be
3917 * brought back up. So one needs to know when such a 'search' is
3918 * complete.
3919 *
3920 * Ideally:
3921 *
3922 * - there'd be a single global queue or a single RIB walker
3923 * - rather than tracking which route_nodes still need to be
3924 * examined on a peer basis, we'd track which peers still
3925 * aren't cleared
3926 *
3927 * Given that our per-peer prefix-counts now should be reliable,
3928 * this may actually be achievable. It doesn't seem to be a huge
3929 * problem at this time,
3930 *
3931 * It is possible that we have multiple paths for a prefix from
3932 * a peer
3933 * if that peer is using AddPath.
3934 */
3935 ain = rn->adj_in;
3936 while (ain) {
3937 ain_next = ain->next;
3938
3939 if (ain->peer == peer) {
3940 bgp_adj_in_remove(rn, ain);
3941 bgp_unlock_node(rn);
3942 }
3943
3944 ain = ain_next;
3945 }
3946
40381db7
DS
3947 for (pi = rn->info; pi; pi = next) {
3948 next = pi->next;
3949 if (pi->peer != peer)
d62a17ae 3950 continue;
3951
3952 if (force)
40381db7 3953 bgp_path_info_reap(rn, pi);
d62a17ae 3954 else {
3955 struct bgp_clear_node_queue *cnq;
3956
3957 /* both unlocked in bgp_clear_node_queue_del */
3958 bgp_table_lock(bgp_node_table(rn));
3959 bgp_lock_node(rn);
3960 cnq = XCALLOC(
3961 MTYPE_BGP_CLEAR_NODE_QUEUE,
3962 sizeof(struct bgp_clear_node_queue));
3963 cnq->rn = rn;
3964 work_queue_add(peer->clear_node_queue, cnq);
3965 break;
3966 }
3967 }
3968 }
3969 return;
3970}
3971
3972void bgp_clear_route(struct peer *peer, afi_t afi, safi_t safi)
3973{
3974 struct bgp_node *rn;
3975 struct bgp_table *table;
3976
3977 if (peer->clear_node_queue == NULL)
3978 bgp_clear_node_queue_init(peer);
3979
3980 /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
3981 * Idle until it receives a Clearing_Completed event. This protects
3982 * against peers which flap faster than we can we clear, which could
3983 * lead to:
3984 *
3985 * a) race with routes from the new session being installed before
3986 * clear_route_node visits the node (to delete the route of that
3987 * peer)
3988 * b) resource exhaustion, clear_route_node likely leads to an entry
3989 * on the process_main queue. Fast-flapping could cause that queue
3990 * to grow and grow.
3991 */
3992
3993 /* lock peer in assumption that clear-node-queue will get nodes; if so,
3994 * the unlock will happen upon work-queue completion; other wise, the
3995 * unlock happens at the end of this function.
3996 */
3997 if (!peer->clear_node_queue->thread)
3998 peer_lock(peer);
3999
4000 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN)
4001 bgp_clear_route_table(peer, afi, safi, NULL);
4002 else
4003 for (rn = bgp_table_top(peer->bgp->rib[afi][safi]); rn;
4004 rn = bgp_route_next(rn))
4005 if ((table = rn->info) != NULL)
4006 bgp_clear_route_table(peer, afi, safi, table);
4007
4008 /* unlock if no nodes got added to the clear-node-queue. */
4009 if (!peer->clear_node_queue->thread)
4010 peer_unlock(peer);
718e3744 4011}
d62a17ae 4012
4013void bgp_clear_route_all(struct peer *peer)
718e3744 4014{
d62a17ae 4015 afi_t afi;
4016 safi_t safi;
718e3744 4017
05c7a1cc
QY
4018 FOREACH_AFI_SAFI (afi, safi)
4019 bgp_clear_route(peer, afi, safi);
65efcfce
LB
4020
4021#if ENABLE_BGP_VNC
d62a17ae 4022 rfapiProcessPeerDown(peer);
65efcfce 4023#endif
718e3744 4024}
4025
d62a17ae 4026void bgp_clear_adj_in(struct peer *peer, afi_t afi, safi_t safi)
718e3744 4027{
d62a17ae 4028 struct bgp_table *table;
4029 struct bgp_node *rn;
4030 struct bgp_adj_in *ain;
4031 struct bgp_adj_in *ain_next;
718e3744 4032
d62a17ae 4033 table = peer->bgp->rib[afi][safi];
718e3744 4034
d62a17ae 4035 /* It is possible that we have multiple paths for a prefix from a peer
4036 * if that peer is using AddPath.
4037 */
4038 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
4039 ain = rn->adj_in;
43143c8f 4040
d62a17ae 4041 while (ain) {
4042 ain_next = ain->next;
43143c8f 4043
d62a17ae 4044 if (ain->peer == peer) {
4045 bgp_adj_in_remove(rn, ain);
4046 bgp_unlock_node(rn);
4047 }
43143c8f 4048
d62a17ae 4049 ain = ain_next;
4050 }
4051 }
718e3744 4052}
93406d87 4053
d62a17ae 4054void bgp_clear_stale_route(struct peer *peer, afi_t afi, safi_t safi)
4055{
4056 struct bgp_node *rn;
40381db7 4057 struct bgp_path_info *pi;
d62a17ae 4058 struct bgp_table *table;
4059
4060 if (safi == SAFI_MPLS_VPN) {
4061 for (rn = bgp_table_top(peer->bgp->rib[afi][safi]); rn;
4062 rn = bgp_route_next(rn)) {
4063 struct bgp_node *rm;
d62a17ae 4064
4065 /* look for neighbor in tables */
ea47320b
DL
4066 if ((table = rn->info) == NULL)
4067 continue;
4068
4069 for (rm = bgp_table_top(table); rm;
4070 rm = bgp_route_next(rm))
40381db7
DS
4071 for (pi = rm->info; pi; pi = pi->next) {
4072 if (pi->peer != peer)
ea47320b 4073 continue;
40381db7 4074 if (!CHECK_FLAG(pi->flags,
1defdda8 4075 BGP_PATH_STALE))
ea47320b
DL
4076 break;
4077
40381db7 4078 bgp_rib_remove(rm, pi, peer, afi, safi);
ea47320b
DL
4079 break;
4080 }
d62a17ae 4081 }
4082 } else {
4083 for (rn = bgp_table_top(peer->bgp->rib[afi][safi]); rn;
4084 rn = bgp_route_next(rn))
40381db7
DS
4085 for (pi = rn->info; pi; pi = pi->next) {
4086 if (pi->peer != peer)
ea47320b 4087 continue;
40381db7 4088 if (!CHECK_FLAG(pi->flags, BGP_PATH_STALE))
d62a17ae 4089 break;
40381db7 4090 bgp_rib_remove(rn, pi, peer, afi, safi);
ea47320b
DL
4091 break;
4092 }
d62a17ae 4093 }
93406d87 4094}
6b0655a2 4095
568e10ca 4096static void bgp_cleanup_table(struct bgp *bgp, struct bgp_table *table,
4097 safi_t safi)
bb86c601 4098{
d62a17ae 4099 struct bgp_node *rn;
40381db7 4100 struct bgp_path_info *pi;
4b7e6066 4101 struct bgp_path_info *next;
bb86c601 4102
d62a17ae 4103 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
40381db7
DS
4104 for (pi = rn->info; pi; pi = next) {
4105 next = pi->next;
4106 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4107 && pi->type == ZEBRA_ROUTE_BGP
4108 && (pi->sub_type == BGP_ROUTE_NORMAL
4109 || pi->sub_type == BGP_ROUTE_AGGREGATE
4110 || pi->sub_type == BGP_ROUTE_IMPORTED)) {
ddb5b488 4111
d62a17ae 4112 if (bgp_fibupd_safi(safi))
40381db7
DS
4113 bgp_zebra_withdraw(&rn->p, pi, bgp,
4114 safi);
4115 bgp_path_info_reap(rn, pi);
d62a17ae 4116 }
4117 }
bb86c601
LB
4118}
4119
718e3744 4120/* Delete all kernel routes. */
d62a17ae 4121void bgp_cleanup_routes(struct bgp *bgp)
4122{
4123 afi_t afi;
4124 struct bgp_node *rn;
4125
4126 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
4127 if (afi == AFI_L2VPN)
4128 continue;
568e10ca 4129 bgp_cleanup_table(bgp, bgp->rib[afi][SAFI_UNICAST],
4130 SAFI_UNICAST);
d62a17ae 4131 /*
4132 * VPN and ENCAP and EVPN tables are two-level (RD is top level)
4133 */
4134 if (afi != AFI_L2VPN) {
4135 safi_t safi;
4136 safi = SAFI_MPLS_VPN;
4137 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
4138 rn = bgp_route_next(rn)) {
4139 if (rn->info) {
568e10ca 4140 bgp_cleanup_table(bgp,
d62a17ae 4141 (struct bgp_table *)(rn->info),
4142 safi);
4143 bgp_table_finish((struct bgp_table **)&(
4144 rn->info));
4145 rn->info = NULL;
4146 bgp_unlock_node(rn);
4147 }
4148 }
4149 safi = SAFI_ENCAP;
4150 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
4151 rn = bgp_route_next(rn)) {
4152 if (rn->info) {
568e10ca 4153 bgp_cleanup_table(bgp,
d62a17ae 4154 (struct bgp_table *)(rn->info),
4155 safi);
4156 bgp_table_finish((struct bgp_table **)&(
4157 rn->info));
4158 rn->info = NULL;
4159 bgp_unlock_node(rn);
4160 }
4161 }
4162 }
4163 }
4164 for (rn = bgp_table_top(bgp->rib[AFI_L2VPN][SAFI_EVPN]); rn;
4165 rn = bgp_route_next(rn)) {
4166 if (rn->info) {
568e10ca 4167 bgp_cleanup_table(bgp,
4168 (struct bgp_table *)(rn->info),
d62a17ae 4169 SAFI_EVPN);
4170 bgp_table_finish((struct bgp_table **)&(rn->info));
4171 rn->info = NULL;
4172 bgp_unlock_node(rn);
4173 }
bb86c601 4174 }
718e3744 4175}
4176
d62a17ae 4177void bgp_reset(void)
718e3744 4178{
d62a17ae 4179 vty_reset();
4180 bgp_zclient_reset();
4181 access_list_reset();
4182 prefix_list_reset();
718e3744 4183}
6b0655a2 4184
d62a17ae 4185static int bgp_addpath_encode_rx(struct peer *peer, afi_t afi, safi_t safi)
adbac85e 4186{
d62a17ae 4187 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
4188 && CHECK_FLAG(peer->af_cap[afi][safi],
4189 PEER_CAP_ADDPATH_AF_TX_RCV));
adbac85e
DW
4190}
4191
718e3744 4192/* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
4193 value. */
d62a17ae 4194int bgp_nlri_parse_ip(struct peer *peer, struct attr *attr,
4195 struct bgp_nlri *packet)
4196{
d7c0a89a
QY
4197 uint8_t *pnt;
4198 uint8_t *lim;
d62a17ae 4199 struct prefix p;
4200 int psize;
4201 int ret;
4202 afi_t afi;
4203 safi_t safi;
4204 int addpath_encoded;
d7c0a89a 4205 uint32_t addpath_id;
d62a17ae 4206
d62a17ae 4207 pnt = packet->nlri;
4208 lim = pnt + packet->length;
4209 afi = packet->afi;
4210 safi = packet->safi;
4211 addpath_id = 0;
4212 addpath_encoded = bgp_addpath_encode_rx(peer, afi, safi);
4213
4214 /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
4215 syntactic validity. If the field is syntactically incorrect,
4216 then the Error Subcode is set to Invalid Network Field. */
4217 for (; pnt < lim; pnt += psize) {
4218 /* Clear prefix structure. */
4219 memset(&p, 0, sizeof(struct prefix));
4220
4221 if (addpath_encoded) {
4222
4223 /* When packet overflow occurs return immediately. */
4224 if (pnt + BGP_ADDPATH_ID_LEN > lim)
4225 return -1;
4226
4227 addpath_id = ntohl(*((uint32_t *)pnt));
4228 pnt += BGP_ADDPATH_ID_LEN;
4229 }
718e3744 4230
d62a17ae 4231 /* Fetch prefix length. */
4232 p.prefixlen = *pnt++;
4233 /* afi/safi validity already verified by caller,
4234 * bgp_update_receive */
4235 p.family = afi2family(afi);
4236
4237 /* Prefix length check. */
4238 if (p.prefixlen > prefix_blen(&p) * 8) {
af4c2728 4239 flog_err(
e50f7cfd 4240 EC_BGP_UPDATE_RCV,
14454c9f 4241 "%s [Error] Update packet error (wrong prefix length %d for afi %u)",
d62a17ae 4242 peer->host, p.prefixlen, packet->afi);
4243 return -1;
4244 }
6b0655a2 4245
d62a17ae 4246 /* Packet size overflow check. */
4247 psize = PSIZE(p.prefixlen);
4248
4249 /* When packet overflow occur return immediately. */
4250 if (pnt + psize > lim) {
af4c2728 4251 flog_err(
e50f7cfd 4252 EC_BGP_UPDATE_RCV,
d62a17ae 4253 "%s [Error] Update packet error (prefix length %d overflows packet)",
4254 peer->host, p.prefixlen);
4255 return -1;
4256 }
4257
4258 /* Defensive coding, double-check the psize fits in a struct
4259 * prefix */
4260 if (psize > (ssize_t)sizeof(p.u)) {
af4c2728 4261 flog_err(
e50f7cfd 4262 EC_BGP_UPDATE_RCV,
d62a17ae 4263 "%s [Error] Update packet error (prefix length %d too large for prefix storage %zu)",
4264 peer->host, p.prefixlen, sizeof(p.u));
4265 return -1;
4266 }
4267
4268 /* Fetch prefix from NLRI packet. */
a85297a7 4269 memcpy(p.u.val, pnt, psize);
d62a17ae 4270
4271 /* Check address. */
4272 if (afi == AFI_IP && safi == SAFI_UNICAST) {
4273 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
4274 /* From RFC4271 Section 6.3:
4275 *
4276 * If a prefix in the NLRI field is semantically
4277 * incorrect
4278 * (e.g., an unexpected multicast IP address),
4279 * an error SHOULD
4280 * be logged locally, and the prefix SHOULD be
4281 * ignored.
a4d82a8a 4282 */
af4c2728 4283 flog_err(
e50f7cfd 4284 EC_BGP_UPDATE_RCV,
d62a17ae 4285 "%s: IPv4 unicast NLRI is multicast address %s, ignoring",
4286 peer->host, inet_ntoa(p.u.prefix4));
4287 continue;
4288 }
4289 }
4290
4291 /* Check address. */
4292 if (afi == AFI_IP6 && safi == SAFI_UNICAST) {
4293 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
4294 char buf[BUFSIZ];
4295
af4c2728 4296 flog_err(
e50f7cfd 4297 EC_BGP_UPDATE_RCV,
d62a17ae 4298 "%s: IPv6 unicast NLRI is link-local address %s, ignoring",
4299 peer->host,
4300 inet_ntop(AF_INET6, &p.u.prefix6, buf,
4301 BUFSIZ));
4302
4303 continue;
4304 }
4305 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
4306 char buf[BUFSIZ];
4307
af4c2728 4308 flog_err(
e50f7cfd 4309 EC_BGP_UPDATE_RCV,
d62a17ae 4310 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
4311 peer->host,
4312 inet_ntop(AF_INET6, &p.u.prefix6, buf,
4313 BUFSIZ));
4314
4315 continue;
4316 }
4317 }
4318
4319 /* Normal process. */
4320 if (attr)
4321 ret = bgp_update(peer, &p, addpath_id, attr, afi, safi,
4322 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4323 NULL, NULL, 0, 0, NULL);
d62a17ae 4324 else
4325 ret = bgp_withdraw(peer, &p, addpath_id, attr, afi,
4326 safi, ZEBRA_ROUTE_BGP,
a4d82a8a
PZ
4327 BGP_ROUTE_NORMAL, NULL, NULL, 0,
4328 NULL);
d62a17ae 4329
4330 /* Address family configuration mismatch or maximum-prefix count
4331 overflow. */
4332 if (ret < 0)
4333 return -1;
4334 }
4335
4336 /* Packet length consistency check. */
4337 if (pnt != lim) {
af4c2728 4338 flog_err(
e50f7cfd 4339 EC_BGP_UPDATE_RCV,
d62a17ae 4340 "%s [Error] Update packet error (prefix length mismatch with total length)",
4341 peer->host);
4342 return -1;
4343 }
6b0655a2 4344
d62a17ae 4345 return 0;
718e3744 4346}
4347
d62a17ae 4348static struct bgp_static *bgp_static_new(void)
718e3744 4349{
d62a17ae 4350 return XCALLOC(MTYPE_BGP_STATIC, sizeof(struct bgp_static));
718e3744 4351}
4352
d62a17ae 4353static void bgp_static_free(struct bgp_static *bgp_static)
fee0f4c6 4354{
d62a17ae 4355 if (bgp_static->rmap.name)
4356 XFREE(MTYPE_ROUTE_MAP_NAME, bgp_static->rmap.name);
4357 if (bgp_static->eth_s_id)
4358 XFREE(MTYPE_ATTR, bgp_static->eth_s_id);
4359 XFREE(MTYPE_BGP_STATIC, bgp_static);
4360}
4361
4362void bgp_static_update(struct bgp *bgp, struct prefix *p,
4363 struct bgp_static *bgp_static, afi_t afi, safi_t safi)
4364{
4365 struct bgp_node *rn;
40381db7 4366 struct bgp_path_info *pi;
4b7e6066 4367 struct bgp_path_info *new;
40381db7 4368 struct bgp_path_info rmap_path;
d62a17ae 4369 struct attr attr;
4370 struct attr *attr_new;
4371 int ret;
65efcfce 4372#if ENABLE_BGP_VNC
d62a17ae 4373 int vnc_implicit_withdraw = 0;
65efcfce 4374#endif
fee0f4c6 4375
d62a17ae 4376 assert(bgp_static);
4377 if (!bgp_static)
4378 return;
dd8103a9 4379
d62a17ae 4380 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
718e3744 4381
d62a17ae 4382 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
718e3744 4383
d62a17ae 4384 attr.nexthop = bgp_static->igpnexthop;
4385 attr.med = bgp_static->igpmetric;
4386 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
41367172 4387
d62a17ae 4388 if (bgp_static->atomic)
4389 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
6cf48acc 4390
d62a17ae 4391 /* Store label index, if required. */
4392 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX) {
4393 attr.label_index = bgp_static->label_index;
4394 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
4395 }
718e3744 4396
d62a17ae 4397 /* Apply route-map. */
4398 if (bgp_static->rmap.name) {
4399 struct attr attr_tmp = attr;
80ced710 4400
40381db7
DS
4401 memset(&rmap_path, 0, sizeof(struct bgp_path_info));
4402 rmap_path.peer = bgp->peer_self;
4403 rmap_path.attr = &attr_tmp;
fee0f4c6 4404
d62a17ae 4405 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
286e1e71 4406
40381db7
DS
4407 ret = route_map_apply(bgp_static->rmap.map, p, RMAP_BGP,
4408 &rmap_path);
fee0f4c6 4409
d62a17ae 4410 bgp->peer_self->rmap_type = 0;
718e3744 4411
d62a17ae 4412 if (ret == RMAP_DENYMATCH) {
4413 /* Free uninterned attribute. */
4414 bgp_attr_flush(&attr_tmp);
718e3744 4415
d62a17ae 4416 /* Unintern original. */
4417 aspath_unintern(&attr.aspath);
4418 bgp_static_withdraw(bgp, p, afi, safi);
4419 return;
4420 }
7f323236
DW
4421
4422 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN))
4423 bgp_attr_add_gshut_community(&attr_tmp);
4424
d62a17ae 4425 attr_new = bgp_attr_intern(&attr_tmp);
7f323236
DW
4426 } else {
4427
4428 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN))
4429 bgp_attr_add_gshut_community(&attr);
4430
d62a17ae 4431 attr_new = bgp_attr_intern(&attr);
7f323236 4432 }
718e3744 4433
40381db7
DS
4434 for (pi = rn->info; pi; pi = pi->next)
4435 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
4436 && pi->sub_type == BGP_ROUTE_STATIC)
d62a17ae 4437 break;
4438
40381db7
DS
4439 if (pi) {
4440 if (attrhash_cmp(pi->attr, attr_new)
4441 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
d62a17ae 4442 && !bgp_flag_check(bgp, BGP_FLAG_FORCE_STATIC_PROCESS)) {
4443 bgp_unlock_node(rn);
4444 bgp_attr_unintern(&attr_new);
4445 aspath_unintern(&attr.aspath);
4446 return;
4447 } else {
4448 /* The attribute is changed. */
40381db7 4449 bgp_path_info_set_flag(rn, pi, BGP_PATH_ATTR_CHANGED);
d62a17ae 4450
4451 /* Rewrite BGP route information. */
40381db7
DS
4452 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
4453 bgp_path_info_restore(rn, pi);
d62a17ae 4454 else
40381db7 4455 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
65efcfce 4456#if ENABLE_BGP_VNC
d62a17ae 4457 if ((afi == AFI_IP || afi == AFI_IP6)
4458 && (safi == SAFI_UNICAST)) {
40381db7 4459 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
d62a17ae 4460 /*
4461 * Implicit withdraw case.
40381db7 4462 * We have to do this before pi is
d62a17ae 4463 * changed
4464 */
4465 ++vnc_implicit_withdraw;
40381db7 4466 vnc_import_bgp_del_route(bgp, p, pi);
d62a17ae 4467 vnc_import_bgp_exterior_del_route(
40381db7 4468 bgp, p, pi);
d62a17ae 4469 }
4470 }
65efcfce 4471#endif
40381db7
DS
4472 bgp_attr_unintern(&pi->attr);
4473 pi->attr = attr_new;
4474 pi->uptime = bgp_clock();
65efcfce 4475#if ENABLE_BGP_VNC
d62a17ae 4476 if ((afi == AFI_IP || afi == AFI_IP6)
4477 && (safi == SAFI_UNICAST)) {
4478 if (vnc_implicit_withdraw) {
40381db7 4479 vnc_import_bgp_add_route(bgp, p, pi);
d62a17ae 4480 vnc_import_bgp_exterior_add_route(
40381db7 4481 bgp, p, pi);
d62a17ae 4482 }
4483 }
65efcfce 4484#endif
718e3744 4485
d62a17ae 4486 /* Nexthop reachability check. */
4487 if (bgp_flag_check(bgp, BGP_FLAG_IMPORT_CHECK)
4488 && (safi == SAFI_UNICAST
4489 || safi == SAFI_LABELED_UNICAST)) {
960035b2
PZ
4490
4491 struct bgp *bgp_nexthop = bgp;
4492
40381db7
DS
4493 if (pi->extra && pi->extra->bgp_orig)
4494 bgp_nexthop = pi->extra->bgp_orig;
960035b2
PZ
4495
4496 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop,
40381db7
DS
4497 afi, pi, NULL, 0))
4498 bgp_path_info_set_flag(rn, pi,
18ee8310 4499 BGP_PATH_VALID);
d62a17ae 4500 else {
4501 if (BGP_DEBUG(nht, NHT)) {
4502 char buf1[INET6_ADDRSTRLEN];
4503 inet_ntop(p->family,
4504 &p->u.prefix, buf1,
4505 INET6_ADDRSTRLEN);
4506 zlog_debug(
4507 "%s(%s): Route not in table, not advertising",
4508 __FUNCTION__, buf1);
4509 }
18ee8310 4510 bgp_path_info_unset_flag(
40381db7 4511 rn, pi, BGP_PATH_VALID);
d62a17ae 4512 }
4513 } else {
4514 /* Delete the NHT structure if any, if we're
4515 * toggling between
4516 * enabling/disabling import check. We
4517 * deregister the route
4518 * from NHT to avoid overloading NHT and the
4519 * process interaction
4520 */
40381db7
DS
4521 bgp_unlink_nexthop(pi);
4522 bgp_path_info_set_flag(rn, pi, BGP_PATH_VALID);
d62a17ae 4523 }
4524 /* Process change. */
40381db7 4525 bgp_aggregate_increment(bgp, p, pi, afi, safi);
d62a17ae 4526 bgp_process(bgp, rn, afi, safi);
ddb5b488
PZ
4527
4528 if (SAFI_UNICAST == safi
4529 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4530 || bgp->inst_type
4531 == BGP_INSTANCE_TYPE_DEFAULT)) {
4532 vpn_leak_from_vrf_update(bgp_get_default(), bgp,
40381db7 4533 pi);
ddb5b488
PZ
4534 }
4535
d62a17ae 4536 bgp_unlock_node(rn);
4537 aspath_unintern(&attr.aspath);
4538 return;
4539 }
718e3744 4540 }
718e3744 4541
d62a17ae 4542 /* Make new BGP info. */
4543 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
4544 attr_new, rn);
4545 /* Nexthop reachability check. */
4546 if (bgp_flag_check(bgp, BGP_FLAG_IMPORT_CHECK)
4547 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST)) {
960035b2 4548 if (bgp_find_or_add_nexthop(bgp, bgp, afi, new, NULL, 0))
18ee8310 4549 bgp_path_info_set_flag(rn, new, BGP_PATH_VALID);
d62a17ae 4550 else {
4551 if (BGP_DEBUG(nht, NHT)) {
4552 char buf1[INET6_ADDRSTRLEN];
4553 inet_ntop(p->family, &p->u.prefix, buf1,
4554 INET6_ADDRSTRLEN);
4555 zlog_debug(
4556 "%s(%s): Route not in table, not advertising",
4557 __FUNCTION__, buf1);
4558 }
18ee8310 4559 bgp_path_info_unset_flag(rn, new, BGP_PATH_VALID);
d62a17ae 4560 }
4561 } else {
4562 /* Delete the NHT structure if any, if we're toggling between
4563 * enabling/disabling import check. We deregister the route
4564 * from NHT to avoid overloading NHT and the process interaction
4565 */
4566 bgp_unlink_nexthop(new);
4567
18ee8310 4568 bgp_path_info_set_flag(rn, new, BGP_PATH_VALID);
fc9a856f 4569 }
078430f6 4570
d62a17ae 4571 /* Aggregate address increment. */
4572 bgp_aggregate_increment(bgp, p, new, afi, safi);
718e3744 4573
d62a17ae 4574 /* Register new BGP information. */
18ee8310 4575 bgp_path_info_add(rn, new);
718e3744 4576
d62a17ae 4577 /* route_node_get lock */
4578 bgp_unlock_node(rn);
4579
4580 /* Process change. */
4581 bgp_process(bgp, rn, afi, safi);
4582
ddb5b488
PZ
4583 if (SAFI_UNICAST == safi
4584 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4585 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4586 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
4587 }
4588
d62a17ae 4589 /* Unintern original. */
4590 aspath_unintern(&attr.aspath);
718e3744 4591}
4592
d62a17ae 4593void bgp_static_withdraw(struct bgp *bgp, struct prefix *p, afi_t afi,
4594 safi_t safi)
718e3744 4595{
d62a17ae 4596 struct bgp_node *rn;
40381db7 4597 struct bgp_path_info *pi;
718e3744 4598
d62a17ae 4599 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
718e3744 4600
d62a17ae 4601 /* Check selected route and self inserted route. */
40381db7
DS
4602 for (pi = rn->info; pi; pi = pi->next)
4603 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
4604 && pi->sub_type == BGP_ROUTE_STATIC)
d62a17ae 4605 break;
4606
4607 /* Withdraw static BGP route from routing table. */
40381db7 4608 if (pi) {
ddb5b488
PZ
4609 if (SAFI_UNICAST == safi
4610 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4611 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
40381db7 4612 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
ddb5b488 4613 }
40381db7
DS
4614 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
4615 bgp_unlink_nexthop(pi);
4616 bgp_path_info_delete(rn, pi);
d62a17ae 4617 bgp_process(bgp, rn, afi, safi);
4618 }
718e3744 4619
d62a17ae 4620 /* Unlock bgp_node_lookup. */
4621 bgp_unlock_node(rn);
718e3744 4622}
4623
137446f9
LB
4624/*
4625 * Used for SAFI_MPLS_VPN and SAFI_ENCAP
4626 */
d62a17ae 4627static void bgp_static_withdraw_safi(struct bgp *bgp, struct prefix *p,
4628 afi_t afi, safi_t safi,
4629 struct prefix_rd *prd)
718e3744 4630{
d62a17ae 4631 struct bgp_node *rn;
40381db7 4632 struct bgp_path_info *pi;
718e3744 4633
d62a17ae 4634 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
718e3744 4635
d62a17ae 4636 /* Check selected route and self inserted route. */
40381db7
DS
4637 for (pi = rn->info; pi; pi = pi->next)
4638 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
4639 && pi->sub_type == BGP_ROUTE_STATIC)
d62a17ae 4640 break;
718e3744 4641
d62a17ae 4642 /* Withdraw static BGP route from routing table. */
40381db7 4643 if (pi) {
65efcfce 4644#if ENABLE_BGP_VNC
d62a17ae 4645 rfapiProcessWithdraw(
40381db7 4646 pi->peer, NULL, p, prd, pi->attr, afi, safi, pi->type,
d62a17ae 4647 1); /* Kill, since it is an administrative change */
65efcfce 4648#endif
ddb5b488
PZ
4649 if (SAFI_MPLS_VPN == safi
4650 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
40381db7 4651 vpn_leak_to_vrf_withdraw(bgp, pi);
ddb5b488 4652 }
40381db7
DS
4653 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
4654 bgp_path_info_delete(rn, pi);
d62a17ae 4655 bgp_process(bgp, rn, afi, safi);
4656 }
718e3744 4657
d62a17ae 4658 /* Unlock bgp_node_lookup. */
4659 bgp_unlock_node(rn);
718e3744 4660}
4661
d62a17ae 4662static void bgp_static_update_safi(struct bgp *bgp, struct prefix *p,
4663 struct bgp_static *bgp_static, afi_t afi,
4664 safi_t safi)
137446f9 4665{
d62a17ae 4666 struct bgp_node *rn;
4b7e6066 4667 struct bgp_path_info *new;
d62a17ae 4668 struct attr *attr_new;
4669 struct attr attr = {0};
40381db7 4670 struct bgp_path_info *pi;
65efcfce 4671#if ENABLE_BGP_VNC
d62a17ae 4672 mpls_label_t label = 0;
65efcfce 4673#endif
d7c0a89a 4674 uint32_t num_labels = 0;
d62a17ae 4675 union gw_addr add;
137446f9 4676
d62a17ae 4677 assert(bgp_static);
137446f9 4678
b57ba6d2
MK
4679 if (bgp_static->label != MPLS_INVALID_LABEL)
4680 num_labels = 1;
d62a17ae 4681 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p,
4682 &bgp_static->prd);
137446f9 4683
d62a17ae 4684 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
137446f9 4685
d62a17ae 4686 attr.nexthop = bgp_static->igpnexthop;
4687 attr.med = bgp_static->igpmetric;
4688 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
137446f9 4689
d62a17ae 4690 if ((safi == SAFI_EVPN) || (safi == SAFI_MPLS_VPN)
4691 || (safi == SAFI_ENCAP)) {
4692 if (afi == AFI_IP) {
4693 attr.mp_nexthop_global_in = bgp_static->igpnexthop;
4694 attr.mp_nexthop_len = IPV4_MAX_BYTELEN;
4695 }
4696 }
4697 if (afi == AFI_L2VPN) {
4698 if (bgp_static->gatewayIp.family == AF_INET)
4699 add.ipv4.s_addr =
4700 bgp_static->gatewayIp.u.prefix4.s_addr;
4701 else if (bgp_static->gatewayIp.family == AF_INET6)
4702 memcpy(&(add.ipv6), &(bgp_static->gatewayIp.u.prefix6),
4703 sizeof(struct in6_addr));
4704 overlay_index_update(&attr, bgp_static->eth_s_id, &add);
4705 if (bgp_static->encap_tunneltype == BGP_ENCAP_TYPE_VXLAN) {
4706 struct bgp_encap_type_vxlan bet;
4707 memset(&bet, 0, sizeof(struct bgp_encap_type_vxlan));
3714a385 4708 bet.vnid = p->u.prefix_evpn.prefix_addr.eth_tag;
d62a17ae 4709 bgp_encap_type_vxlan_to_tlv(&bet, &attr);
4710 }
4711 if (bgp_static->router_mac) {
4712 bgp_add_routermac_ecom(&attr, bgp_static->router_mac);
4713 }
4714 }
4715 /* Apply route-map. */
4716 if (bgp_static->rmap.name) {
4717 struct attr attr_tmp = attr;
40381db7 4718 struct bgp_path_info rmap_path;
d62a17ae 4719 int ret;
137446f9 4720
40381db7
DS
4721 rmap_path.peer = bgp->peer_self;
4722 rmap_path.attr = &attr_tmp;
137446f9 4723
d62a17ae 4724 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
137446f9 4725
40381db7
DS
4726 ret = route_map_apply(bgp_static->rmap.map, p, RMAP_BGP,
4727 &rmap_path);
137446f9 4728
d62a17ae 4729 bgp->peer_self->rmap_type = 0;
137446f9 4730
d62a17ae 4731 if (ret == RMAP_DENYMATCH) {
4732 /* Free uninterned attribute. */
4733 bgp_attr_flush(&attr_tmp);
137446f9 4734
d62a17ae 4735 /* Unintern original. */
4736 aspath_unintern(&attr.aspath);
4737 bgp_static_withdraw_safi(bgp, p, afi, safi,
4738 &bgp_static->prd);
4739 return;
4740 }
137446f9 4741
d62a17ae 4742 attr_new = bgp_attr_intern(&attr_tmp);
4743 } else {
4744 attr_new = bgp_attr_intern(&attr);
4745 }
137446f9 4746
40381db7
DS
4747 for (pi = rn->info; pi; pi = pi->next)
4748 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
4749 && pi->sub_type == BGP_ROUTE_STATIC)
d62a17ae 4750 break;
4751
40381db7 4752 if (pi) {
d62a17ae 4753 memset(&add, 0, sizeof(union gw_addr));
40381db7
DS
4754 if (attrhash_cmp(pi->attr, attr_new)
4755 && overlay_index_equal(afi, pi, bgp_static->eth_s_id, &add)
4756 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
d62a17ae 4757 bgp_unlock_node(rn);
4758 bgp_attr_unintern(&attr_new);
4759 aspath_unintern(&attr.aspath);
4760 return;
4761 } else {
4762 /* The attribute is changed. */
40381db7 4763 bgp_path_info_set_flag(rn, pi, BGP_PATH_ATTR_CHANGED);
d62a17ae 4764
4765 /* Rewrite BGP route information. */
40381db7
DS
4766 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
4767 bgp_path_info_restore(rn, pi);
d62a17ae 4768 else
40381db7
DS
4769 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
4770 bgp_attr_unintern(&pi->attr);
4771 pi->attr = attr_new;
4772 pi->uptime = bgp_clock();
65efcfce 4773#if ENABLE_BGP_VNC
40381db7
DS
4774 if (pi->extra)
4775 label = decode_label(&pi->extra->label[0]);
65efcfce 4776#endif
137446f9 4777
d62a17ae 4778 /* Process change. */
40381db7 4779 bgp_aggregate_increment(bgp, p, pi, afi, safi);
d62a17ae 4780 bgp_process(bgp, rn, afi, safi);
ddb5b488
PZ
4781
4782 if (SAFI_MPLS_VPN == safi
4783 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
40381db7 4784 vpn_leak_to_vrf_update(bgp, pi);
ddb5b488 4785 }
65efcfce 4786#if ENABLE_BGP_VNC
40381db7
DS
4787 rfapiProcessUpdate(pi->peer, NULL, p, &bgp_static->prd,
4788 pi->attr, afi, safi, pi->type,
4789 pi->sub_type, &label);
65efcfce 4790#endif
d62a17ae 4791 bgp_unlock_node(rn);
4792 aspath_unintern(&attr.aspath);
4793 return;
4794 }
4795 }
137446f9
LB
4796
4797
d62a17ae 4798 /* Make new BGP info. */
4799 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
4800 attr_new, rn);
1defdda8 4801 SET_FLAG(new->flags, BGP_PATH_VALID);
18ee8310 4802 new->extra = bgp_path_info_extra_new();
b57ba6d2
MK
4803 if (num_labels) {
4804 new->extra->label[0] = bgp_static->label;
4805 new->extra->num_labels = num_labels;
4806 }
65efcfce 4807#if ENABLE_BGP_VNC
d62a17ae 4808 label = decode_label(&bgp_static->label);
65efcfce 4809#endif
137446f9 4810
d62a17ae 4811 /* Aggregate address increment. */
4812 bgp_aggregate_increment(bgp, p, new, afi, safi);
137446f9 4813
d62a17ae 4814 /* Register new BGP information. */
18ee8310 4815 bgp_path_info_add(rn, new);
d62a17ae 4816 /* route_node_get lock */
4817 bgp_unlock_node(rn);
137446f9 4818
d62a17ae 4819 /* Process change. */
4820 bgp_process(bgp, rn, afi, safi);
137446f9 4821
ddb5b488
PZ
4822 if (SAFI_MPLS_VPN == safi
4823 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4824 vpn_leak_to_vrf_update(bgp, new);
4825 }
65efcfce 4826#if ENABLE_BGP_VNC
d62a17ae 4827 rfapiProcessUpdate(new->peer, NULL, p, &bgp_static->prd, new->attr, afi,
4828 safi, new->type, new->sub_type, &label);
65efcfce
LB
4829#endif
4830
d62a17ae 4831 /* Unintern original. */
4832 aspath_unintern(&attr.aspath);
137446f9
LB
4833}
4834
718e3744 4835/* Configure static BGP network. When user don't run zebra, static
4836 route should be installed as valid. */
e2a86ad9
DS
4837static int bgp_static_set(struct vty *vty, const char *negate,
4838 const char *ip_str, afi_t afi, safi_t safi,
d7c0a89a 4839 const char *rmap, int backdoor, uint32_t label_index)
d62a17ae 4840{
4841 VTY_DECLVAR_CONTEXT(bgp, bgp);
4842 int ret;
4843 struct prefix p;
4844 struct bgp_static *bgp_static;
4845 struct bgp_node *rn;
d7c0a89a 4846 uint8_t need_update = 0;
d62a17ae 4847
4848 /* Convert IP prefix string to struct prefix. */
4849 ret = str2prefix(ip_str, &p);
4850 if (!ret) {
4851 vty_out(vty, "%% Malformed prefix\n");
4852 return CMD_WARNING_CONFIG_FAILED;
4853 }
4854 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
4855 vty_out(vty, "%% Malformed prefix (link-local address)\n");
4856 return CMD_WARNING_CONFIG_FAILED;
4857 }
718e3744 4858
d62a17ae 4859 apply_mask(&p);
718e3744 4860
e2a86ad9 4861 if (negate) {
718e3744 4862
e2a86ad9
DS
4863 /* Set BGP static route configuration. */
4864 rn = bgp_node_lookup(bgp->route[afi][safi], &p);
d62a17ae 4865
e2a86ad9 4866 if (!rn) {
a4d82a8a 4867 vty_out(vty, "%% Can't find static route specified\n");
d62a17ae 4868 return CMD_WARNING_CONFIG_FAILED;
4869 }
4870
a78beeb5 4871 bgp_static = bgp_static_get_node_info(rn);
d62a17ae 4872
e2a86ad9
DS
4873 if ((label_index != BGP_INVALID_LABEL_INDEX)
4874 && (label_index != bgp_static->label_index)) {
4875 vty_out(vty,
4876 "%% label-index doesn't match static route\n");
4877 return CMD_WARNING_CONFIG_FAILED;
d62a17ae 4878 }
d62a17ae 4879
e2a86ad9
DS
4880 if ((rmap && bgp_static->rmap.name)
4881 && strcmp(rmap, bgp_static->rmap.name)) {
4882 vty_out(vty,
4883 "%% route-map name doesn't match static route\n");
4884 return CMD_WARNING_CONFIG_FAILED;
d62a17ae 4885 }
718e3744 4886
e2a86ad9
DS
4887 /* Update BGP RIB. */
4888 if (!bgp_static->backdoor)
4889 bgp_static_withdraw(bgp, &p, afi, safi);
718e3744 4890
e2a86ad9
DS
4891 /* Clear configuration. */
4892 bgp_static_free(bgp_static);
a78beeb5 4893 bgp_static_set_node_info(rn, NULL);
e2a86ad9
DS
4894 bgp_unlock_node(rn);
4895 bgp_unlock_node(rn);
4896 } else {
718e3744 4897
e2a86ad9
DS
4898 /* Set BGP static route configuration. */
4899 rn = bgp_node_get(bgp->route[afi][safi], &p);
718e3744 4900
a78beeb5
DS
4901 bgp_static = bgp_static_get_node_info(rn);
4902 if (bgp_static) {
e2a86ad9 4903 /* Configuration change. */
e2a86ad9
DS
4904 /* Label index cannot be changed. */
4905 if (bgp_static->label_index != label_index) {
4906 vty_out(vty, "%% cannot change label-index\n");
4907 return CMD_WARNING_CONFIG_FAILED;
4908 }
d62a17ae 4909
e2a86ad9 4910 /* Check previous routes are installed into BGP. */
a4d82a8a
PZ
4911 if (bgp_static->valid
4912 && bgp_static->backdoor != backdoor)
e2a86ad9 4913 need_update = 1;
718e3744 4914
e2a86ad9 4915 bgp_static->backdoor = backdoor;
718e3744 4916
e2a86ad9
DS
4917 if (rmap) {
4918 if (bgp_static->rmap.name)
4919 XFREE(MTYPE_ROUTE_MAP_NAME,
a4d82a8a 4920 bgp_static->rmap.name);
e2a86ad9
DS
4921 bgp_static->rmap.name =
4922 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
4923 bgp_static->rmap.map =
4924 route_map_lookup_by_name(rmap);
4925 } else {
4926 if (bgp_static->rmap.name)
4927 XFREE(MTYPE_ROUTE_MAP_NAME,
a4d82a8a 4928 bgp_static->rmap.name);
e2a86ad9
DS
4929 bgp_static->rmap.name = NULL;
4930 bgp_static->rmap.map = NULL;
4931 bgp_static->valid = 0;
4932 }
4933 bgp_unlock_node(rn);
4934 } else {
4935 /* New configuration. */
4936 bgp_static = bgp_static_new();
4937 bgp_static->backdoor = backdoor;
4938 bgp_static->valid = 0;
4939 bgp_static->igpmetric = 0;
4940 bgp_static->igpnexthop.s_addr = 0;
4941 bgp_static->label_index = label_index;
718e3744 4942
e2a86ad9
DS
4943 if (rmap) {
4944 if (bgp_static->rmap.name)
4945 XFREE(MTYPE_ROUTE_MAP_NAME,
a4d82a8a 4946 bgp_static->rmap.name);
e2a86ad9
DS
4947 bgp_static->rmap.name =
4948 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
4949 bgp_static->rmap.map =
4950 route_map_lookup_by_name(rmap);
4951 }
a78beeb5 4952 bgp_static_set_node_info(rn, bgp_static);
e2a86ad9 4953 }
d62a17ae 4954
e2a86ad9
DS
4955 bgp_static->valid = 1;
4956 if (need_update)
4957 bgp_static_withdraw(bgp, &p, afi, safi);
d62a17ae 4958
e2a86ad9
DS
4959 if (!bgp_static->backdoor)
4960 bgp_static_update(bgp, &p, bgp_static, afi, safi);
4961 }
d62a17ae 4962
4963 return CMD_SUCCESS;
4964}
4965
4966void bgp_static_add(struct bgp *bgp)
4967{
4968 afi_t afi;
4969 safi_t safi;
4970 struct bgp_node *rn;
4971 struct bgp_node *rm;
4972 struct bgp_table *table;
4973 struct bgp_static *bgp_static;
4974
05c7a1cc
QY
4975 FOREACH_AFI_SAFI (afi, safi)
4976 for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
4977 rn = bgp_route_next(rn)) {
4978 if (rn->info == NULL)
4979 continue;
ea47320b 4980
05c7a1cc
QY
4981 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
4982 || (safi == SAFI_EVPN)) {
4983 table = rn->info;
4984
4985 for (rm = bgp_table_top(table); rm;
4986 rm = bgp_route_next(rm)) {
a78beeb5
DS
4987 bgp_static =
4988 bgp_static_get_node_info(rm);
05c7a1cc
QY
4989 bgp_static_update_safi(bgp, &rm->p,
4990 bgp_static, afi,
4991 safi);
d62a17ae 4992 }
05c7a1cc 4993 } else {
a78beeb5
DS
4994 bgp_static_update(bgp, &rn->p,
4995 bgp_static_get_node_info(rn),
4996 afi, safi);
ea47320b 4997 }
05c7a1cc 4998 }
6aeb9e78
DS
4999}
5000
718e3744 5001/* Called from bgp_delete(). Delete all static routes from the BGP
5002 instance. */
d62a17ae 5003void bgp_static_delete(struct bgp *bgp)
5004{
5005 afi_t afi;
5006 safi_t safi;
5007 struct bgp_node *rn;
5008 struct bgp_node *rm;
5009 struct bgp_table *table;
5010 struct bgp_static *bgp_static;
5011
05c7a1cc
QY
5012 FOREACH_AFI_SAFI (afi, safi)
5013 for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
5014 rn = bgp_route_next(rn)) {
5015 if (rn->info == NULL)
5016 continue;
ea47320b 5017
05c7a1cc
QY
5018 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
5019 || (safi == SAFI_EVPN)) {
5020 table = rn->info;
5021
5022 for (rm = bgp_table_top(table); rm;
5023 rm = bgp_route_next(rm)) {
a78beeb5
DS
5024 bgp_static =
5025 bgp_static_get_node_info(rm);
05c7a1cc
QY
5026 bgp_static_withdraw_safi(
5027 bgp, &rm->p, AFI_IP, safi,
5028 (struct prefix_rd *)&rn->p);
ea47320b 5029 bgp_static_free(bgp_static);
a78beeb5 5030 bgp_static_set_node_info(rn, NULL);
ea47320b 5031 bgp_unlock_node(rn);
d62a17ae 5032 }
05c7a1cc 5033 } else {
a78beeb5 5034 bgp_static = bgp_static_get_node_info(rn);
05c7a1cc
QY
5035 bgp_static_withdraw(bgp, &rn->p, afi, safi);
5036 bgp_static_free(bgp_static);
a78beeb5 5037 bgp_static_set_node_info(rn, NULL);
05c7a1cc 5038 bgp_unlock_node(rn);
ea47320b 5039 }
05c7a1cc 5040 }
d62a17ae 5041}
5042
5043void bgp_static_redo_import_check(struct bgp *bgp)
5044{
5045 afi_t afi;
5046 safi_t safi;
5047 struct bgp_node *rn;
5048 struct bgp_node *rm;
5049 struct bgp_table *table;
5050 struct bgp_static *bgp_static;
5051
5052 /* Use this flag to force reprocessing of the route */
5053 bgp_flag_set(bgp, BGP_FLAG_FORCE_STATIC_PROCESS);
05c7a1cc
QY
5054 FOREACH_AFI_SAFI (afi, safi) {
5055 for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
5056 rn = bgp_route_next(rn)) {
5057 if (rn->info == NULL)
5058 continue;
ea47320b 5059
05c7a1cc
QY
5060 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
5061 || (safi == SAFI_EVPN)) {
5062 table = rn->info;
5063
5064 for (rm = bgp_table_top(table); rm;
5065 rm = bgp_route_next(rm)) {
a78beeb5
DS
5066 bgp_static =
5067 bgp_static_get_node_info(rm);
05c7a1cc
QY
5068 bgp_static_update_safi(bgp, &rm->p,
5069 bgp_static, afi,
5070 safi);
d62a17ae 5071 }
05c7a1cc 5072 } else {
a78beeb5 5073 bgp_static = bgp_static_get_node_info(rn);
05c7a1cc
QY
5074 bgp_static_update(bgp, &rn->p, bgp_static, afi,
5075 safi);
ea47320b 5076 }
05c7a1cc
QY
5077 }
5078 }
d62a17ae 5079 bgp_flag_unset(bgp, BGP_FLAG_FORCE_STATIC_PROCESS);
5080}
5081
5082static void bgp_purge_af_static_redist_routes(struct bgp *bgp, afi_t afi,
5083 safi_t safi)
5084{
5085 struct bgp_table *table;
5086 struct bgp_node *rn;
40381db7 5087 struct bgp_path_info *pi;
d62a17ae 5088
5089 table = bgp->rib[afi][safi];
5090 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
40381db7
DS
5091 for (pi = rn->info; pi; pi = pi->next) {
5092 if (pi->peer == bgp->peer_self
5093 && ((pi->type == ZEBRA_ROUTE_BGP
5094 && pi->sub_type == BGP_ROUTE_STATIC)
5095 || (pi->type != ZEBRA_ROUTE_BGP
5096 && pi->sub_type
d62a17ae 5097 == BGP_ROUTE_REDISTRIBUTE))) {
40381db7 5098 bgp_aggregate_decrement(bgp, &rn->p, pi, afi,
d62a17ae 5099 safi);
40381db7
DS
5100 bgp_unlink_nexthop(pi);
5101 bgp_path_info_delete(rn, pi);
d62a17ae 5102 bgp_process(bgp, rn, afi, safi);
5103 }
5104 }
5105 }
ad4cbda1 5106}
5107
5108/*
5109 * Purge all networks and redistributed routes from routing table.
5110 * Invoked upon the instance going down.
5111 */
d62a17ae 5112void bgp_purge_static_redist_routes(struct bgp *bgp)
ad4cbda1 5113{
d62a17ae 5114 afi_t afi;
5115 safi_t safi;
ad4cbda1 5116
05c7a1cc
QY
5117 FOREACH_AFI_SAFI (afi, safi)
5118 bgp_purge_af_static_redist_routes(bgp, afi, safi);
ad4cbda1 5119}
5120
137446f9
LB
5121/*
5122 * gpz 110624
5123 * Currently this is used to set static routes for VPN and ENCAP.
5124 * I think it can probably be factored with bgp_static_set.
5125 */
d62a17ae 5126int bgp_static_set_safi(afi_t afi, safi_t safi, struct vty *vty,
5127 const char *ip_str, const char *rd_str,
5128 const char *label_str, const char *rmap_str,
5129 int evpn_type, const char *esi, const char *gwip,
5130 const char *ethtag, const char *routermac)
5131{
5132 VTY_DECLVAR_CONTEXT(bgp, bgp);
5133 int ret;
5134 struct prefix p;
5135 struct prefix_rd prd;
5136 struct bgp_node *prn;
5137 struct bgp_node *rn;
5138 struct bgp_table *table;
5139 struct bgp_static *bgp_static;
5140 mpls_label_t label = MPLS_INVALID_LABEL;
5141 struct prefix gw_ip;
5142
5143 /* validate ip prefix */
5144 ret = str2prefix(ip_str, &p);
5145 if (!ret) {
5146 vty_out(vty, "%% Malformed prefix\n");
5147 return CMD_WARNING_CONFIG_FAILED;
5148 }
5149 apply_mask(&p);
5150 if ((afi == AFI_L2VPN)
5151 && (bgp_build_evpn_prefix(evpn_type,
5152 ethtag != NULL ? atol(ethtag) : 0, &p))) {
5153 vty_out(vty, "%% L2VPN prefix could not be forged\n");
5154 return CMD_WARNING_CONFIG_FAILED;
5155 }
718e3744 5156
d62a17ae 5157 ret = str2prefix_rd(rd_str, &prd);
5158 if (!ret) {
5159 vty_out(vty, "%% Malformed rd\n");
5160 return CMD_WARNING_CONFIG_FAILED;
5161 }
718e3744 5162
d62a17ae 5163 if (label_str) {
5164 unsigned long label_val;
5165 label_val = strtoul(label_str, NULL, 10);
5166 encode_label(label_val, &label);
5167 }
9bedbb1e 5168
d62a17ae 5169 if (safi == SAFI_EVPN) {
5170 if (esi && str2esi(esi, NULL) == 0) {
5171 vty_out(vty, "%% Malformed ESI\n");
5172 return CMD_WARNING_CONFIG_FAILED;
5173 }
5174 if (routermac && prefix_str2mac(routermac, NULL) == 0) {
5175 vty_out(vty, "%% Malformed Router MAC\n");
5176 return CMD_WARNING_CONFIG_FAILED;
5177 }
5178 if (gwip) {
5179 memset(&gw_ip, 0, sizeof(struct prefix));
5180 ret = str2prefix(gwip, &gw_ip);
5181 if (!ret) {
5182 vty_out(vty, "%% Malformed GatewayIp\n");
5183 return CMD_WARNING_CONFIG_FAILED;
5184 }
5185 if ((gw_ip.family == AF_INET
3714a385 5186 && is_evpn_prefix_ipaddr_v6(
d62a17ae 5187 (struct prefix_evpn *)&p))
5188 || (gw_ip.family == AF_INET6
3714a385 5189 && is_evpn_prefix_ipaddr_v4(
d62a17ae 5190 (struct prefix_evpn *)&p))) {
5191 vty_out(vty,
5192 "%% GatewayIp family differs with IP prefix\n");
5193 return CMD_WARNING_CONFIG_FAILED;
5194 }
5195 }
5196 }
5197 prn = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
5198 if (prn->info == NULL)
960035b2 5199 prn->info = bgp_table_init(bgp, afi, safi);
d62a17ae 5200 else
5201 bgp_unlock_node(prn);
5202 table = prn->info;
5203
5204 rn = bgp_node_get(table, &p);
5205
5206 if (rn->info) {
5207 vty_out(vty, "%% Same network configuration exists\n");
5208 bgp_unlock_node(rn);
5209 } else {
5210 /* New configuration. */
5211 bgp_static = bgp_static_new();
5212 bgp_static->backdoor = 0;
5213 bgp_static->valid = 0;
5214 bgp_static->igpmetric = 0;
5215 bgp_static->igpnexthop.s_addr = 0;
5216 bgp_static->label = label;
5217 bgp_static->prd = prd;
5218
5219 if (rmap_str) {
5220 if (bgp_static->rmap.name)
5221 XFREE(MTYPE_ROUTE_MAP_NAME,
5222 bgp_static->rmap.name);
5223 bgp_static->rmap.name =
5224 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_str);
5225 bgp_static->rmap.map =
5226 route_map_lookup_by_name(rmap_str);
5227 }
718e3744 5228
d62a17ae 5229 if (safi == SAFI_EVPN) {
5230 if (esi) {
5231 bgp_static->eth_s_id =
5232 XCALLOC(MTYPE_ATTR,
5233 sizeof(struct eth_segment_id));
5234 str2esi(esi, bgp_static->eth_s_id);
5235 }
5236 if (routermac) {
5237 bgp_static->router_mac =
28328ea9 5238 XCALLOC(MTYPE_ATTR, ETH_ALEN + 1);
56cb79b6
A
5239 (void)prefix_str2mac(routermac,
5240 bgp_static->router_mac);
d62a17ae 5241 }
5242 if (gwip)
5243 prefix_copy(&bgp_static->gatewayIp, &gw_ip);
5244 }
a78beeb5 5245 bgp_static_set_node_info(rn, bgp_static);
718e3744 5246
d62a17ae 5247 bgp_static->valid = 1;
5248 bgp_static_update_safi(bgp, &p, bgp_static, afi, safi);
5249 }
718e3744 5250
d62a17ae 5251 return CMD_SUCCESS;
718e3744 5252}
5253
5254/* Configure static BGP network. */
d62a17ae 5255int bgp_static_unset_safi(afi_t afi, safi_t safi, struct vty *vty,
5256 const char *ip_str, const char *rd_str,
5257 const char *label_str, int evpn_type, const char *esi,
5258 const char *gwip, const char *ethtag)
5259{
5260 VTY_DECLVAR_CONTEXT(bgp, bgp);
5261 int ret;
5262 struct prefix p;
5263 struct prefix_rd prd;
5264 struct bgp_node *prn;
5265 struct bgp_node *rn;
5266 struct bgp_table *table;
5267 struct bgp_static *bgp_static;
5268 mpls_label_t label = MPLS_INVALID_LABEL;
5269
5270 /* Convert IP prefix string to struct prefix. */
5271 ret = str2prefix(ip_str, &p);
5272 if (!ret) {
5273 vty_out(vty, "%% Malformed prefix\n");
5274 return CMD_WARNING_CONFIG_FAILED;
5275 }
5276 apply_mask(&p);
5277 if ((afi == AFI_L2VPN)
5278 && (bgp_build_evpn_prefix(evpn_type,
5279 ethtag != NULL ? atol(ethtag) : 0, &p))) {
5280 vty_out(vty, "%% L2VPN prefix could not be forged\n");
5281 return CMD_WARNING_CONFIG_FAILED;
5282 }
5283 ret = str2prefix_rd(rd_str, &prd);
5284 if (!ret) {
5285 vty_out(vty, "%% Malformed rd\n");
5286 return CMD_WARNING_CONFIG_FAILED;
5287 }
718e3744 5288
d62a17ae 5289 if (label_str) {
5290 unsigned long label_val;
5291 label_val = strtoul(label_str, NULL, 10);
5292 encode_label(label_val, &label);
5293 }
718e3744 5294
d62a17ae 5295 prn = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
5296 if (prn->info == NULL)
960035b2 5297 prn->info = bgp_table_init(bgp, afi, safi);
d62a17ae 5298 else
5299 bgp_unlock_node(prn);
5300 table = prn->info;
718e3744 5301
d62a17ae 5302 rn = bgp_node_lookup(table, &p);
6b0655a2 5303
d62a17ae 5304 if (rn) {
5305 bgp_static_withdraw_safi(bgp, &p, afi, safi, &prd);
73ac8160 5306
a78beeb5 5307 bgp_static = bgp_static_get_node_info(rn);
d62a17ae 5308 bgp_static_free(bgp_static);
a78beeb5 5309 bgp_static_set_node_info(rn, NULL);
d62a17ae 5310 bgp_unlock_node(rn);
5311 bgp_unlock_node(rn);
5312 } else
5313 vty_out(vty, "%% Can't find the route\n");
5314
5315 return CMD_SUCCESS;
5316}
5317
5318static int bgp_table_map_set(struct vty *vty, afi_t afi, safi_t safi,
5319 const char *rmap_name)
5320{
5321 VTY_DECLVAR_CONTEXT(bgp, bgp);
5322 struct bgp_rmap *rmap;
5323
5324 rmap = &bgp->table_map[afi][safi];
5325 if (rmap_name) {
5326 if (rmap->name)
5327 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
5328 rmap->name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_name);
5329 rmap->map = route_map_lookup_by_name(rmap_name);
5330 } else {
5331 if (rmap->name)
5332 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
5333 rmap->name = NULL;
5334 rmap->map = NULL;
5335 }
73ac8160 5336
d62a17ae 5337 if (bgp_fibupd_safi(safi))
5338 bgp_zebra_announce_table(bgp, afi, safi);
73ac8160 5339
d62a17ae 5340 return CMD_SUCCESS;
73ac8160
DS
5341}
5342
d62a17ae 5343static int bgp_table_map_unset(struct vty *vty, afi_t afi, safi_t safi,
5344 const char *rmap_name)
73ac8160 5345{
d62a17ae 5346 VTY_DECLVAR_CONTEXT(bgp, bgp);
5347 struct bgp_rmap *rmap;
73ac8160 5348
d62a17ae 5349 rmap = &bgp->table_map[afi][safi];
5350 if (rmap->name)
5351 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
5352 rmap->name = NULL;
5353 rmap->map = NULL;
73ac8160 5354
d62a17ae 5355 if (bgp_fibupd_safi(safi))
5356 bgp_zebra_announce_table(bgp, afi, safi);
73ac8160 5357
d62a17ae 5358 return CMD_SUCCESS;
73ac8160
DS
5359}
5360
2b791107 5361void bgp_config_write_table_map(struct vty *vty, struct bgp *bgp, afi_t afi,
60466a63 5362 safi_t safi)
73ac8160 5363{
d62a17ae 5364 if (bgp->table_map[afi][safi].name) {
d62a17ae 5365 vty_out(vty, " table-map %s\n",
5366 bgp->table_map[afi][safi].name);
5367 }
73ac8160
DS
5368}
5369
73ac8160
DS
5370DEFUN (bgp_table_map,
5371 bgp_table_map_cmd,
5372 "table-map WORD",
5373 "BGP table to RIB route download filter\n"
5374 "Name of the route map\n")
5375{
d62a17ae 5376 int idx_word = 1;
5377 return bgp_table_map_set(vty, bgp_node_afi(vty), bgp_node_safi(vty),
5378 argv[idx_word]->arg);
73ac8160
DS
5379}
5380DEFUN (no_bgp_table_map,
5381 no_bgp_table_map_cmd,
5382 "no table-map WORD",
3a2d747c 5383 NO_STR
73ac8160
DS
5384 "BGP table to RIB route download filter\n"
5385 "Name of the route map\n")
5386{
d62a17ae 5387 int idx_word = 2;
5388 return bgp_table_map_unset(vty, bgp_node_afi(vty), bgp_node_safi(vty),
5389 argv[idx_word]->arg);
73ac8160
DS
5390}
5391
e2a86ad9
DS
5392DEFPY(bgp_network,
5393 bgp_network_cmd,
5394 "[no] network \
5395 <A.B.C.D/M$prefix|A.B.C.D$address [mask A.B.C.D$netmask]> \
5396 [{route-map WORD$map_name|label-index (0-1048560)$label_index| \
5397 backdoor$backdoor}]",
5398 NO_STR
5399 "Specify a network to announce via BGP\n"
5400 "IPv4 prefix\n"
5401 "Network number\n"
5402 "Network mask\n"
5403 "Network mask\n"
5404 "Route-map to modify the attributes\n"
5405 "Name of the route map\n"
5406 "Label index to associate with the prefix\n"
5407 "Label index value\n"
5408 "Specify a BGP backdoor route\n")
5409{
5410 char addr_prefix_str[BUFSIZ];
5411
5412 if (address_str) {
5413 int ret;
718e3744 5414
e2a86ad9
DS
5415 ret = netmask_str2prefix_str(address_str, netmask_str,
5416 addr_prefix_str);
5417 if (!ret) {
5418 vty_out(vty, "%% Inconsistent address and mask\n");
5419 return CMD_WARNING_CONFIG_FAILED;
5420 }
d62a17ae 5421 }
718e3744 5422
a4d82a8a
PZ
5423 return bgp_static_set(
5424 vty, no, address_str ? addr_prefix_str : prefix_str, AFI_IP,
5425 bgp_node_safi(vty), map_name, backdoor ? 1 : 0,
5426 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
718e3744 5427}
5428
e2a86ad9
DS
5429DEFPY(ipv6_bgp_network,
5430 ipv6_bgp_network_cmd,
5431 "[no] network X:X::X:X/M$prefix \
5432 [{route-map WORD$map_name|label-index (0-1048560)$label_index}]",
5433 NO_STR
5434 "Specify a network to announce via BGP\n"
5435 "IPv6 prefix\n"
5436 "Route-map to modify the attributes\n"
5437 "Name of the route map\n"
5438 "Label index to associate with the prefix\n"
5439 "Label index value\n")
718e3744 5440{
a4d82a8a
PZ
5441 return bgp_static_set(
5442 vty, no, prefix_str, AFI_IP6, bgp_node_safi(vty), map_name, 0,
5443 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
1b6d5c7e
VV
5444}
5445
718e3744 5446/* Aggreagete address:
5447
5448 advertise-map Set condition to advertise attribute
5449 as-set Generate AS set path information
5450 attribute-map Set attributes of aggregate
5451 route-map Set parameters of aggregate
5452 summary-only Filter more specific routes from updates
5453 suppress-map Conditionally filter more specific routes from updates
5454 <cr>
5455 */
d62a17ae 5456struct bgp_aggregate {
5457 /* Summary-only flag. */
d7c0a89a 5458 uint8_t summary_only;
718e3744 5459
d62a17ae 5460 /* AS set generation. */
d7c0a89a 5461 uint8_t as_set;
718e3744 5462
d62a17ae 5463 /* Route-map for aggregated route. */
5464 struct route_map *map;
718e3744 5465
d62a17ae 5466 /* Suppress-count. */
5467 unsigned long count;
718e3744 5468
d62a17ae 5469 /* SAFI configuration. */
5470 safi_t safi;
718e3744 5471};
5472
d62a17ae 5473static struct bgp_aggregate *bgp_aggregate_new(void)
718e3744 5474{
d62a17ae 5475 return XCALLOC(MTYPE_BGP_AGGREGATE, sizeof(struct bgp_aggregate));
718e3744 5476}
5477
d62a17ae 5478static void bgp_aggregate_free(struct bgp_aggregate *aggregate)
718e3744 5479{
d62a17ae 5480 XFREE(MTYPE_BGP_AGGREGATE, aggregate);
5481}
718e3744 5482
40381db7 5483static int bgp_aggregate_info_same(struct bgp_path_info *pi, uint8_t origin,
29f7d023 5484 struct aspath *aspath,
eaaf8adb
DS
5485 struct community *comm)
5486{
5487 static struct aspath *ae = NULL;
5488
5489 if (!ae)
5490 ae = aspath_empty();
5491
40381db7 5492 if (!pi)
eaaf8adb
DS
5493 return 0;
5494
40381db7 5495 if (origin != pi->attr->origin)
29f7d023
DS
5496 return 0;
5497
40381db7 5498 if (!aspath_cmp(pi->attr->aspath, (aspath) ? aspath : ae))
eaaf8adb
DS
5499 return 0;
5500
40381db7 5501 if (!community_cmp(pi->attr->community, comm))
eaaf8adb
DS
5502 return 0;
5503
40381db7 5504 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID))
7ce8a8e0
DS
5505 return 0;
5506
eaaf8adb
DS
5507 return 1;
5508}
5509
c701010e
DS
5510static void bgp_aggregate_install(struct bgp *bgp, afi_t afi, safi_t safi,
5511 struct prefix *p, uint8_t origin,
5512 struct aspath *aspath,
5513 struct community *community,
5514 uint8_t atomic_aggregate,
5515 struct bgp_aggregate *aggregate)
5516{
5517 struct bgp_node *rn;
5518 struct bgp_table *table;
40381db7 5519 struct bgp_path_info *pi, *new;
c701010e
DS
5520
5521 table = bgp->rib[afi][safi];
5522
5523 rn = bgp_node_get(table, p);
eaaf8adb 5524
40381db7
DS
5525 for (pi = rn->info; pi; pi = pi->next)
5526 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
5527 && pi->sub_type == BGP_ROUTE_AGGREGATE)
eaaf8adb
DS
5528 break;
5529
c701010e 5530 if (aggregate->count > 0) {
eaaf8adb
DS
5531 /*
5532 * If the aggregate information has not changed
5533 * no need to re-install it again.
5534 */
29f7d023
DS
5535 if (bgp_aggregate_info_same(rn->info, origin, aspath,
5536 community)) {
eaaf8adb
DS
5537 bgp_unlock_node(rn);
5538
5539 if (aspath)
5540 aspath_free(aspath);
5541 if (community)
5542 community_free(community);
5543
5544 return;
5545 }
5546
5547 /*
5548 * Mark the old as unusable
5549 */
40381db7
DS
5550 if (pi)
5551 bgp_path_info_delete(rn, pi);
eaaf8adb 5552
c701010e
DS
5553 new = info_make(
5554 ZEBRA_ROUTE_BGP, BGP_ROUTE_AGGREGATE, 0, bgp->peer_self,
5555 bgp_attr_aggregate_intern(bgp, origin, aspath,
5556 community, aggregate->as_set,
5557 atomic_aggregate),
5558 rn);
1defdda8 5559 SET_FLAG(new->flags, BGP_PATH_VALID);
c701010e 5560
18ee8310 5561 bgp_path_info_add(rn, new);
c701010e
DS
5562 bgp_process(bgp, rn, afi, safi);
5563 } else {
40381db7
DS
5564 for (pi = rn->info; pi; pi = pi->next)
5565 if (pi->peer == bgp->peer_self
5566 && pi->type == ZEBRA_ROUTE_BGP
5567 && pi->sub_type == BGP_ROUTE_AGGREGATE)
c701010e
DS
5568 break;
5569
5570 /* Withdraw static BGP route from routing table. */
40381db7
DS
5571 if (pi) {
5572 bgp_path_info_delete(rn, pi);
c701010e
DS
5573 bgp_process(bgp, rn, afi, safi);
5574 }
5575 }
5576
5577 bgp_unlock_node(rn);
5578}
5579
b5d58c32 5580/* Update an aggregate as routes are added/removed from the BGP table */
d62a17ae 5581static void bgp_aggregate_route(struct bgp *bgp, struct prefix *p,
40381db7 5582 struct bgp_path_info *pinew, afi_t afi,
4b7e6066 5583 safi_t safi, struct bgp_path_info *del,
d62a17ae 5584 struct bgp_aggregate *aggregate)
5585{
5586 struct bgp_table *table;
5587 struct bgp_node *top;
5588 struct bgp_node *rn;
d7c0a89a 5589 uint8_t origin;
d62a17ae 5590 struct aspath *aspath = NULL;
5591 struct aspath *asmerge = NULL;
5592 struct community *community = NULL;
5593 struct community *commerge = NULL;
40381db7 5594 struct bgp_path_info *pi;
d62a17ae 5595 unsigned long match = 0;
d7c0a89a 5596 uint8_t atomic_aggregate = 0;
d62a17ae 5597
d62a17ae 5598 /* ORIGIN attribute: If at least one route among routes that are
5599 aggregated has ORIGIN with the value INCOMPLETE, then the
5600 aggregated route must have the ORIGIN attribute with the value
5601 INCOMPLETE. Otherwise, if at least one route among routes that
5602 are aggregated has ORIGIN with the value EGP, then the aggregated
5603 route must have the origin attribute with the value EGP. In all
5604 other case the value of the ORIGIN attribute of the aggregated
5605 route is INTERNAL. */
5606 origin = BGP_ORIGIN_IGP;
718e3744 5607
d62a17ae 5608 table = bgp->rib[afi][safi];
718e3744 5609
d62a17ae 5610 top = bgp_node_get(table, p);
5611 for (rn = bgp_node_get(table, p); rn;
c2ff8b3e
DS
5612 rn = bgp_route_next_until(rn, top)) {
5613 if (rn->p.prefixlen <= p->prefixlen)
5614 continue;
d62a17ae 5615
c2ff8b3e 5616 match = 0;
d62a17ae 5617
40381db7
DS
5618 for (pi = rn->info; pi; pi = pi->next) {
5619 if (BGP_PATH_HOLDDOWN(pi))
c2ff8b3e 5620 continue;
718e3744 5621
40381db7 5622 if (del && pi == del)
c2ff8b3e 5623 continue;
718e3744 5624
40381db7 5625 if (pi->attr->flag
c2ff8b3e
DS
5626 & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
5627 atomic_aggregate = 1;
d62a17ae 5628
40381db7 5629 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
c2ff8b3e 5630 continue;
d62a17ae 5631
f273fef1
DS
5632 /*
5633 * summary-only aggregate route suppress
5634 * aggregated route announcements.
5635 */
c2ff8b3e 5636 if (aggregate->summary_only) {
40381db7
DS
5637 (bgp_path_info_extra_get(pi))->suppress++;
5638 bgp_path_info_set_flag(rn, pi,
18ee8310 5639 BGP_PATH_ATTR_CHANGED);
c2ff8b3e 5640 match++;
d62a17ae 5641 }
c2ff8b3e
DS
5642
5643 aggregate->count++;
5644
f273fef1
DS
5645 /*
5646 * If at least one route among routes that are
5647 * aggregated has ORIGIN with the value INCOMPLETE,
5648 * then the aggregated route MUST have the ORIGIN
5649 * attribute with the value INCOMPLETE. Otherwise, if
5650 * at least one route among routes that are aggregated
5651 * has ORIGIN with the value EGP, then the aggregated
5652 * route MUST have the ORIGIN attribute with the value
5653 * EGP.
5654 */
40381db7
DS
5655 if (origin < pi->attr->origin)
5656 origin = pi->attr->origin;
c2ff8b3e
DS
5657
5658 if (!aggregate->as_set)
5659 continue;
5660
f273fef1
DS
5661 /*
5662 * as-set aggregate route generate origin, as path,
5663 * and community aggregation.
5664 */
c2ff8b3e
DS
5665 if (aspath) {
5666 asmerge = aspath_aggregate(aspath,
40381db7 5667 pi->attr->aspath);
c2ff8b3e
DS
5668 aspath_free(aspath);
5669 aspath = asmerge;
5670 } else
40381db7 5671 aspath = aspath_dup(pi->attr->aspath);
c2ff8b3e 5672
40381db7 5673 if (!pi->attr->community)
c2ff8b3e
DS
5674 continue;
5675
5676 if (community) {
5677 commerge = community_merge(community,
40381db7 5678 pi->attr->community);
c2ff8b3e
DS
5679 community = community_uniq_sort(commerge);
5680 community_free(commerge);
5681 } else
40381db7 5682 community = community_dup(pi->attr->community);
d62a17ae 5683 }
c2ff8b3e
DS
5684 if (match)
5685 bgp_process(bgp, rn, afi, safi);
5686 }
d62a17ae 5687 bgp_unlock_node(top);
718e3744 5688
40381db7 5689 if (pinew) {
718e3744 5690 aggregate->count++;
5691
d62a17ae 5692 if (aggregate->summary_only)
40381db7 5693 (bgp_path_info_extra_get(pinew))->suppress++;
d62a17ae 5694
40381db7
DS
5695 if (origin < pinew->attr->origin)
5696 origin = pinew->attr->origin;
d62a17ae 5697
5698 if (aggregate->as_set) {
5699 if (aspath) {
5700 asmerge = aspath_aggregate(aspath,
40381db7 5701 pinew->attr->aspath);
d62a17ae 5702 aspath_free(aspath);
5703 aspath = asmerge;
5704 } else
40381db7 5705 aspath = aspath_dup(pinew->attr->aspath);
d62a17ae 5706
40381db7 5707 if (pinew->attr->community) {
d62a17ae 5708 if (community) {
5709 commerge = community_merge(
5710 community,
40381db7 5711 pinew->attr->community);
d62a17ae 5712 community =
5713 community_uniq_sort(commerge);
5714 community_free(commerge);
5715 } else
5716 community = community_dup(
40381db7 5717 pinew->attr->community);
d62a17ae 5718 }
718e3744 5719 }
718e3744 5720 }
718e3744 5721
c701010e
DS
5722 bgp_aggregate_install(bgp, afi, safi, p, origin, aspath, community,
5723 atomic_aggregate, aggregate);
d62a17ae 5724
c701010e 5725 if (aggregate->count == 0) {
d62a17ae 5726 if (aspath)
5727 aspath_free(aspath);
5728 if (community)
5729 community_free(community);
5730 }
718e3744 5731}
5732
3b7db173
DS
5733static void bgp_aggregate_delete(struct bgp *bgp, struct prefix *p, afi_t afi,
5734 safi_t safi, struct bgp_aggregate *aggregate)
5735{
5736 struct bgp_table *table;
5737 struct bgp_node *top;
5738 struct bgp_node *rn;
40381db7 5739 struct bgp_path_info *pi;
3b7db173
DS
5740 unsigned long match;
5741
5742 table = bgp->rib[afi][safi];
5743
5744 /* If routes exists below this node, generate aggregate routes. */
5745 top = bgp_node_get(table, p);
5746 for (rn = bgp_node_get(table, p); rn;
5747 rn = bgp_route_next_until(rn, top)) {
5748 if (rn->p.prefixlen <= p->prefixlen)
5749 continue;
5750 match = 0;
5751
40381db7
DS
5752 for (pi = rn->info; pi; pi = pi->next) {
5753 if (BGP_PATH_HOLDDOWN(pi))
3b7db173
DS
5754 continue;
5755
40381db7 5756 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
3b7db173
DS
5757 continue;
5758
40381db7
DS
5759 if (aggregate->summary_only && pi->extra) {
5760 pi->extra->suppress--;
3b7db173 5761
40381db7 5762 if (pi->extra->suppress == 0) {
18ee8310 5763 bgp_path_info_set_flag(
40381db7 5764 rn, pi, BGP_PATH_ATTR_CHANGED);
3b7db173
DS
5765 match++;
5766 }
5767 }
5768 aggregate->count--;
5769 }
5770
5771 /* If this node was suppressed, process the change. */
5772 if (match)
5773 bgp_process(bgp, rn, afi, safi);
5774 }
5775 bgp_unlock_node(top);
5776}
718e3744 5777
d62a17ae 5778void bgp_aggregate_increment(struct bgp *bgp, struct prefix *p,
40381db7 5779 struct bgp_path_info *pi, afi_t afi, safi_t safi)
718e3744 5780{
d62a17ae 5781 struct bgp_node *child;
5782 struct bgp_node *rn;
5783 struct bgp_aggregate *aggregate;
5784 struct bgp_table *table;
718e3744 5785
d62a17ae 5786 table = bgp->aggregate[afi][safi];
f018db83 5787
d62a17ae 5788 /* No aggregates configured. */
5789 if (bgp_table_top_nolock(table) == NULL)
5790 return;
f018db83 5791
d62a17ae 5792 if (p->prefixlen == 0)
5793 return;
718e3744 5794
40381db7 5795 if (BGP_PATH_HOLDDOWN(pi))
d62a17ae 5796 return;
718e3744 5797
d62a17ae 5798 child = bgp_node_get(table, p);
718e3744 5799
d62a17ae 5800 /* Aggregate address configuration check. */
b1e62edd
DS
5801 for (rn = child; rn; rn = bgp_node_parent_nolock(rn)) {
5802 aggregate = bgp_aggregate_get_node_info(rn);
5803 if (aggregate != NULL && rn->p.prefixlen < p->prefixlen) {
d62a17ae 5804 bgp_aggregate_delete(bgp, &rn->p, afi, safi, aggregate);
40381db7 5805 bgp_aggregate_route(bgp, &rn->p, pi, afi, safi, NULL,
d62a17ae 5806 aggregate);
5807 }
b1e62edd 5808 }
d62a17ae 5809 bgp_unlock_node(child);
718e3744 5810}
5811
d62a17ae 5812void bgp_aggregate_decrement(struct bgp *bgp, struct prefix *p,
4b7e6066 5813 struct bgp_path_info *del, afi_t afi, safi_t safi)
718e3744 5814{
d62a17ae 5815 struct bgp_node *child;
5816 struct bgp_node *rn;
5817 struct bgp_aggregate *aggregate;
5818 struct bgp_table *table;
718e3744 5819
d62a17ae 5820 table = bgp->aggregate[afi][safi];
718e3744 5821
d62a17ae 5822 /* No aggregates configured. */
5823 if (bgp_table_top_nolock(table) == NULL)
5824 return;
718e3744 5825
d62a17ae 5826 if (p->prefixlen == 0)
5827 return;
718e3744 5828
d62a17ae 5829 child = bgp_node_get(table, p);
718e3744 5830
d62a17ae 5831 /* Aggregate address configuration check. */
b1e62edd
DS
5832 for (rn = child; rn; rn = bgp_node_parent_nolock(rn)) {
5833 aggregate = bgp_aggregate_get_node_info(rn);
5834 if (aggregate != NULL && rn->p.prefixlen < p->prefixlen) {
d62a17ae 5835 bgp_aggregate_delete(bgp, &rn->p, afi, safi, aggregate);
5836 bgp_aggregate_route(bgp, &rn->p, NULL, afi, safi, del,
5837 aggregate);
5838 }
b1e62edd 5839 }
d62a17ae 5840 bgp_unlock_node(child);
5841}
718e3744 5842
718e3744 5843/* Aggregate route attribute. */
5844#define AGGREGATE_SUMMARY_ONLY 1
5845#define AGGREGATE_AS_SET 1
5846
d62a17ae 5847static int bgp_aggregate_unset(struct vty *vty, const char *prefix_str,
5848 afi_t afi, safi_t safi)
718e3744 5849{
d62a17ae 5850 VTY_DECLVAR_CONTEXT(bgp, bgp);
5851 int ret;
5852 struct prefix p;
5853 struct bgp_node *rn;
5854 struct bgp_aggregate *aggregate;
718e3744 5855
d62a17ae 5856 /* Convert string to prefix structure. */
5857 ret = str2prefix(prefix_str, &p);
5858 if (!ret) {
5859 vty_out(vty, "Malformed prefix\n");
5860 return CMD_WARNING_CONFIG_FAILED;
5861 }
5862 apply_mask(&p);
5863
5864 /* Old configuration check. */
5865 rn = bgp_node_lookup(bgp->aggregate[afi][safi], &p);
5866 if (!rn) {
5867 vty_out(vty,
5868 "%% There is no aggregate-address configuration.\n");
5869 return CMD_WARNING_CONFIG_FAILED;
5870 }
f6269b4f 5871
b1e62edd 5872 aggregate = bgp_aggregate_get_node_info(rn);
09990cdd 5873 bgp_aggregate_delete(bgp, &p, afi, safi, aggregate);
c701010e 5874 bgp_aggregate_install(bgp, afi, safi, &p, 0, NULL, NULL, 0, aggregate);
d62a17ae 5875
5876 /* Unlock aggregate address configuration. */
b1e62edd 5877 bgp_aggregate_set_node_info(rn, NULL);
d62a17ae 5878 bgp_aggregate_free(aggregate);
5879 bgp_unlock_node(rn);
5880 bgp_unlock_node(rn);
5881
5882 return CMD_SUCCESS;
5883}
5884
5885static int bgp_aggregate_set(struct vty *vty, const char *prefix_str, afi_t afi,
d7c0a89a 5886 safi_t safi, uint8_t summary_only, uint8_t as_set)
d62a17ae 5887{
5888 VTY_DECLVAR_CONTEXT(bgp, bgp);
5889 int ret;
5890 struct prefix p;
5891 struct bgp_node *rn;
5892 struct bgp_aggregate *aggregate;
5893
5894 /* Convert string to prefix structure. */
5895 ret = str2prefix(prefix_str, &p);
5896 if (!ret) {
5897 vty_out(vty, "Malformed prefix\n");
5898 return CMD_WARNING_CONFIG_FAILED;
5899 }
5900 apply_mask(&p);
5901
3624ac81
DS
5902 if ((afi == AFI_IP && p.prefixlen == IPV4_MAX_BITLEN) ||
5903 (afi == AFI_IP6 && p.prefixlen == IPV6_MAX_BITLEN)) {
5904 vty_out(vty, "Specified prefix: %s will not result in any useful aggregation, disallowing\n",
5905 prefix_str);
5906 return CMD_WARNING_CONFIG_FAILED;
5907 }
5908
d62a17ae 5909 /* Old configuration check. */
5910 rn = bgp_node_get(bgp->aggregate[afi][safi], &p);
5911
5912 if (rn->info) {
5913 vty_out(vty, "There is already same aggregate network.\n");
5914 /* try to remove the old entry */
5915 ret = bgp_aggregate_unset(vty, prefix_str, afi, safi);
5916 if (ret) {
5917 vty_out(vty, "Error deleting aggregate.\n");
5918 bgp_unlock_node(rn);
5919 return CMD_WARNING_CONFIG_FAILED;
5920 }
5921 }
718e3744 5922
d62a17ae 5923 /* Make aggregate address structure. */
5924 aggregate = bgp_aggregate_new();
5925 aggregate->summary_only = summary_only;
5926 aggregate->as_set = as_set;
5927 aggregate->safi = safi;
b1e62edd 5928 bgp_aggregate_set_node_info(rn, aggregate);
718e3744 5929
d62a17ae 5930 /* Aggregate address insert into BGP routing table. */
f273fef1 5931 bgp_aggregate_route(bgp, &p, NULL, afi, safi, NULL, aggregate);
718e3744 5932
d62a17ae 5933 return CMD_SUCCESS;
718e3744 5934}
5935
5936DEFUN (aggregate_address,
5937 aggregate_address_cmd,
e3e6107d 5938 "aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
718e3744 5939 "Configure BGP aggregate entries\n"
5940 "Aggregate prefix\n"
5941 "Generate AS set path information\n"
a636c635
DW
5942 "Filter more specific routes from updates\n"
5943 "Filter more specific routes from updates\n"
5944 "Generate AS set path information\n")
718e3744 5945{
d62a17ae 5946 int idx = 0;
5947 argv_find(argv, argc, "A.B.C.D/M", &idx);
5948 char *prefix = argv[idx]->arg;
5949 int as_set =
5950 argv_find(argv, argc, "as-set", &idx) ? AGGREGATE_AS_SET : 0;
5951 idx = 0;
5952 int summary_only = argv_find(argv, argc, "summary-only", &idx)
5953 ? AGGREGATE_SUMMARY_ONLY
5954 : 0;
e3e6107d 5955
d62a17ae 5956 return bgp_aggregate_set(vty, prefix, AFI_IP, bgp_node_safi(vty),
5957 summary_only, as_set);
718e3744 5958}
5959
e3e6107d
QY
5960DEFUN (aggregate_address_mask,
5961 aggregate_address_mask_cmd,
5962 "aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
718e3744 5963 "Configure BGP aggregate entries\n"
5964 "Aggregate address\n"
5965 "Aggregate mask\n"
5966 "Generate AS set path information\n"
a636c635
DW
5967 "Filter more specific routes from updates\n"
5968 "Filter more specific routes from updates\n"
5969 "Generate AS set path information\n")
718e3744 5970{
d62a17ae 5971 int idx = 0;
5972 argv_find(argv, argc, "A.B.C.D", &idx);
5973 char *prefix = argv[idx]->arg;
5974 char *mask = argv[idx + 1]->arg;
5975 int as_set =
5976 argv_find(argv, argc, "as-set", &idx) ? AGGREGATE_AS_SET : 0;
5977 idx = 0;
5978 int summary_only = argv_find(argv, argc, "summary-only", &idx)
5979 ? AGGREGATE_SUMMARY_ONLY
5980 : 0;
5981
5982 char prefix_str[BUFSIZ];
5983 int ret = netmask_str2prefix_str(prefix, mask, prefix_str);
5984
5985 if (!ret) {
5986 vty_out(vty, "%% Inconsistent address and mask\n");
5987 return CMD_WARNING_CONFIG_FAILED;
5988 }
718e3744 5989
d62a17ae 5990 return bgp_aggregate_set(vty, prefix_str, AFI_IP, bgp_node_safi(vty),
5991 summary_only, as_set);
718e3744 5992}
5993
718e3744 5994DEFUN (no_aggregate_address,
5995 no_aggregate_address_cmd,
e3e6107d 5996 "no aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
718e3744 5997 NO_STR
5998 "Configure BGP aggregate entries\n"
a636c635
DW
5999 "Aggregate prefix\n"
6000 "Generate AS set path information\n"
e3e6107d
QY
6001 "Filter more specific routes from updates\n"
6002 "Filter more specific routes from updates\n"
6003 "Generate AS set path information\n")
718e3744 6004{
d62a17ae 6005 int idx = 0;
6006 argv_find(argv, argc, "A.B.C.D/M", &idx);
6007 char *prefix = argv[idx]->arg;
6008 return bgp_aggregate_unset(vty, prefix, AFI_IP, bgp_node_safi(vty));
718e3744 6009}
6010
718e3744 6011DEFUN (no_aggregate_address_mask,
6012 no_aggregate_address_mask_cmd,
e3e6107d 6013 "no aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
718e3744 6014 NO_STR
6015 "Configure BGP aggregate entries\n"
6016 "Aggregate address\n"
a636c635
DW
6017 "Aggregate mask\n"
6018 "Generate AS set path information\n"
e3e6107d
QY
6019 "Filter more specific routes from updates\n"
6020 "Filter more specific routes from updates\n"
6021 "Generate AS set path information\n")
718e3744 6022{
d62a17ae 6023 int idx = 0;
6024 argv_find(argv, argc, "A.B.C.D", &idx);
6025 char *prefix = argv[idx]->arg;
6026 char *mask = argv[idx + 1]->arg;
718e3744 6027
d62a17ae 6028 char prefix_str[BUFSIZ];
6029 int ret = netmask_str2prefix_str(prefix, mask, prefix_str);
718e3744 6030
d62a17ae 6031 if (!ret) {
6032 vty_out(vty, "%% Inconsistent address and mask\n");
6033 return CMD_WARNING_CONFIG_FAILED;
6034 }
718e3744 6035
d62a17ae 6036 return bgp_aggregate_unset(vty, prefix_str, AFI_IP, bgp_node_safi(vty));
718e3744 6037}
6038
718e3744 6039DEFUN (ipv6_aggregate_address,
6040 ipv6_aggregate_address_cmd,
e3e6107d 6041 "aggregate-address X:X::X:X/M [summary-only]",
718e3744 6042 "Configure BGP aggregate entries\n"
6043 "Aggregate prefix\n"
6044 "Filter more specific routes from updates\n")
6045{
d62a17ae 6046 int idx = 0;
6047 argv_find(argv, argc, "X:X::X:X/M", &idx);
6048 char *prefix = argv[idx]->arg;
6049 int sum_only = argv_find(argv, argc, "summary-only", &idx)
6050 ? AGGREGATE_SUMMARY_ONLY
6051 : 0;
6052 return bgp_aggregate_set(vty, prefix, AFI_IP6, SAFI_UNICAST, sum_only,
6053 0);
718e3744 6054}
6055
6056DEFUN (no_ipv6_aggregate_address,
6057 no_ipv6_aggregate_address_cmd,
e3e6107d 6058 "no aggregate-address X:X::X:X/M [summary-only]",
718e3744 6059 NO_STR
6060 "Configure BGP aggregate entries\n"
16cedbb0
QY
6061 "Aggregate prefix\n"
6062 "Filter more specific routes from updates\n")
718e3744 6063{
d62a17ae 6064 int idx = 0;
6065 argv_find(argv, argc, "X:X::X:X/M", &idx);
6066 char *prefix = argv[idx]->arg;
6067 return bgp_aggregate_unset(vty, prefix, AFI_IP6, SAFI_UNICAST);
718e3744 6068}
6069
718e3744 6070/* Redistribute route treatment. */
d62a17ae 6071void bgp_redistribute_add(struct bgp *bgp, struct prefix *p,
9de1f7ff
DS
6072 const union g_addr *nexthop, ifindex_t ifindex,
6073 enum nexthop_types_t nhtype, uint32_t metric,
d7c0a89a
QY
6074 uint8_t type, unsigned short instance,
6075 route_tag_t tag)
d62a17ae 6076{
4b7e6066 6077 struct bgp_path_info *new;
40381db7
DS
6078 struct bgp_path_info *bpi;
6079 struct bgp_path_info rmap_path;
d62a17ae 6080 struct bgp_node *bn;
6081 struct attr attr;
6082 struct attr *new_attr;
6083 afi_t afi;
6084 int ret;
6085 struct bgp_redist *red;
6086
6087 /* Make default attribute. */
6088 bgp_attr_default_set(&attr, BGP_ORIGIN_INCOMPLETE);
9de1f7ff 6089
a4d82a8a 6090 switch (nhtype) {
9de1f7ff
DS
6091 case NEXTHOP_TYPE_IFINDEX:
6092 break;
6093 case NEXTHOP_TYPE_IPV4:
6094 case NEXTHOP_TYPE_IPV4_IFINDEX:
6095 attr.nexthop = nexthop->ipv4;
6096 break;
6097 case NEXTHOP_TYPE_IPV6:
6098 case NEXTHOP_TYPE_IPV6_IFINDEX:
6099 attr.mp_nexthop_global = nexthop->ipv6;
6100 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
6101 break;
6102 case NEXTHOP_TYPE_BLACKHOLE:
74489921
RW
6103 switch (p->family) {
6104 case AF_INET:
9de1f7ff 6105 attr.nexthop.s_addr = INADDR_ANY;
74489921
RW
6106 break;
6107 case AF_INET6:
9de1f7ff
DS
6108 memset(&attr.mp_nexthop_global, 0,
6109 sizeof(attr.mp_nexthop_global));
74489921 6110 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
9de1f7ff 6111 break;
74489921 6112 }
9de1f7ff 6113 break;
d62a17ae 6114 }
74489921 6115 attr.nh_ifindex = ifindex;
f04a80a5 6116
d62a17ae 6117 attr.med = metric;
6118 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
6119 attr.tag = tag;
718e3744 6120
d62a17ae 6121 afi = family2afi(p->family);
6aeb9e78 6122
d62a17ae 6123 red = bgp_redist_lookup(bgp, afi, type, instance);
6124 if (red) {
6125 struct attr attr_new;
718e3744 6126
d62a17ae 6127 /* Copy attribute for modification. */
6128 bgp_attr_dup(&attr_new, &attr);
718e3744 6129
d62a17ae 6130 if (red->redist_metric_flag)
6131 attr_new.med = red->redist_metric;
718e3744 6132
d62a17ae 6133 /* Apply route-map. */
6134 if (red->rmap.name) {
40381db7
DS
6135 memset(&rmap_path, 0, sizeof(struct bgp_path_info));
6136 rmap_path.peer = bgp->peer_self;
6137 rmap_path.attr = &attr_new;
718e3744 6138
d62a17ae 6139 SET_FLAG(bgp->peer_self->rmap_type,
6140 PEER_RMAP_TYPE_REDISTRIBUTE);
6141
6142 ret = route_map_apply(red->rmap.map, p, RMAP_BGP,
40381db7 6143 &rmap_path);
d62a17ae 6144
6145 bgp->peer_self->rmap_type = 0;
6146
6147 if (ret == RMAP_DENYMATCH) {
6148 /* Free uninterned attribute. */
6149 bgp_attr_flush(&attr_new);
6150
6151 /* Unintern original. */
6152 aspath_unintern(&attr.aspath);
6153 bgp_redistribute_delete(bgp, p, type, instance);
6154 return;
6155 }
6156 }
6157
7f323236
DW
6158 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN))
6159 bgp_attr_add_gshut_community(&attr_new);
6160
d62a17ae 6161 bn = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
6162 SAFI_UNICAST, p, NULL);
6163
6164 new_attr = bgp_attr_intern(&attr_new);
6165
40381db7
DS
6166 for (bpi = bn->info; bpi; bpi = bpi->next)
6167 if (bpi->peer == bgp->peer_self
6168 && bpi->sub_type == BGP_ROUTE_REDISTRIBUTE)
d62a17ae 6169 break;
6170
40381db7 6171 if (bpi) {
d62a17ae 6172 /* Ensure the (source route) type is updated. */
40381db7
DS
6173 bpi->type = type;
6174 if (attrhash_cmp(bpi->attr, new_attr)
6175 && !CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
d62a17ae 6176 bgp_attr_unintern(&new_attr);
6177 aspath_unintern(&attr.aspath);
6178 bgp_unlock_node(bn);
6179 return;
6180 } else {
6181 /* The attribute is changed. */
40381db7 6182 bgp_path_info_set_flag(bn, bpi,
18ee8310 6183 BGP_PATH_ATTR_CHANGED);
d62a17ae 6184
6185 /* Rewrite BGP route information. */
40381db7
DS
6186 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
6187 bgp_path_info_restore(bn, bpi);
d62a17ae 6188 else
40381db7
DS
6189 bgp_aggregate_decrement(
6190 bgp, p, bpi, afi, SAFI_UNICAST);
6191 bgp_attr_unintern(&bpi->attr);
6192 bpi->attr = new_attr;
6193 bpi->uptime = bgp_clock();
d62a17ae 6194
6195 /* Process change. */
40381db7 6196 bgp_aggregate_increment(bgp, p, bpi, afi,
d62a17ae 6197 SAFI_UNICAST);
6198 bgp_process(bgp, bn, afi, SAFI_UNICAST);
6199 bgp_unlock_node(bn);
6200 aspath_unintern(&attr.aspath);
ddb5b488
PZ
6201
6202 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
6203 || (bgp->inst_type
6204 == BGP_INSTANCE_TYPE_DEFAULT)) {
6205
6206 vpn_leak_from_vrf_update(
40381db7 6207 bgp_get_default(), bgp, bpi);
ddb5b488 6208 }
d62a17ae 6209 return;
6210 }
6211 }
6212
6213 new = info_make(type, BGP_ROUTE_REDISTRIBUTE, instance,
6214 bgp->peer_self, new_attr, bn);
1defdda8 6215 SET_FLAG(new->flags, BGP_PATH_VALID);
d62a17ae 6216
6217 bgp_aggregate_increment(bgp, p, new, afi, SAFI_UNICAST);
18ee8310 6218 bgp_path_info_add(bn, new);
d62a17ae 6219 bgp_unlock_node(bn);
6220 bgp_process(bgp, bn, afi, SAFI_UNICAST);
ddb5b488
PZ
6221
6222 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
6223 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6224
6225 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
6226 }
d62a17ae 6227 }
6228
6229 /* Unintern original. */
6230 aspath_unintern(&attr.aspath);
718e3744 6231}
6232
d7c0a89a
QY
6233void bgp_redistribute_delete(struct bgp *bgp, struct prefix *p, uint8_t type,
6234 unsigned short instance)
718e3744 6235{
d62a17ae 6236 afi_t afi;
6237 struct bgp_node *rn;
40381db7 6238 struct bgp_path_info *pi;
d62a17ae 6239 struct bgp_redist *red;
718e3744 6240
d62a17ae 6241 afi = family2afi(p->family);
718e3744 6242
d62a17ae 6243 red = bgp_redist_lookup(bgp, afi, type, instance);
6244 if (red) {
6245 rn = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
6246 SAFI_UNICAST, p, NULL);
6247
40381db7
DS
6248 for (pi = rn->info; pi; pi = pi->next)
6249 if (pi->peer == bgp->peer_self && pi->type == type)
d62a17ae 6250 break;
6251
40381db7 6252 if (pi) {
ddb5b488
PZ
6253 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
6254 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6255
6256 vpn_leak_from_vrf_withdraw(bgp_get_default(),
40381db7 6257 bgp, pi);
ddb5b488 6258 }
40381db7
DS
6259 bgp_aggregate_decrement(bgp, p, pi, afi, SAFI_UNICAST);
6260 bgp_path_info_delete(rn, pi);
d62a17ae 6261 bgp_process(bgp, rn, afi, SAFI_UNICAST);
6262 }
6263 bgp_unlock_node(rn);
6264 }
6265}
6266
6267/* Withdraw specified route type's route. */
6268void bgp_redistribute_withdraw(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 6269 unsigned short instance)
d62a17ae 6270{
6271 struct bgp_node *rn;
40381db7 6272 struct bgp_path_info *pi;
d62a17ae 6273 struct bgp_table *table;
6274
6275 table = bgp->rib[afi][SAFI_UNICAST];
6276
6277 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
40381db7
DS
6278 for (pi = rn->info; pi; pi = pi->next)
6279 if (pi->peer == bgp->peer_self && pi->type == type
6280 && pi->instance == instance)
d62a17ae 6281 break;
6282
40381db7 6283 if (pi) {
ddb5b488
PZ
6284 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
6285 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6286
6287 vpn_leak_from_vrf_withdraw(bgp_get_default(),
40381db7 6288 bgp, pi);
ddb5b488 6289 }
40381db7 6290 bgp_aggregate_decrement(bgp, &rn->p, pi, afi,
d62a17ae 6291 SAFI_UNICAST);
40381db7 6292 bgp_path_info_delete(rn, pi);
d62a17ae 6293 bgp_process(bgp, rn, afi, SAFI_UNICAST);
6294 }
718e3744 6295 }
718e3744 6296}
6b0655a2 6297
718e3744 6298/* Static function to display route. */
9c92b5f7
MK
6299static void route_vty_out_route(struct prefix *p, struct vty *vty,
6300 json_object *json)
718e3744 6301{
be054588 6302 int len = 0;
d62a17ae 6303 char buf[BUFSIZ];
37d4e0df 6304 char buf2[BUFSIZ];
718e3744 6305
d62a17ae 6306 if (p->family == AF_INET) {
c6462ff4 6307 if (!json) {
89e5e9f0
PM
6308 len = vty_out(
6309 vty, "%s/%d",
6310 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
6311 p->prefixlen);
c6462ff4
MK
6312 } else {
6313 json_object_string_add(json, "prefix",
6314 inet_ntop(p->family,
6315 &p->u.prefix, buf,
6316 BUFSIZ));
6317 json_object_int_add(json, "prefixLen", p->prefixlen);
37d4e0df
AD
6318 prefix2str(p, buf2, PREFIX_STRLEN);
6319 json_object_string_add(json, "network", buf2);
c6462ff4 6320 }
d62a17ae 6321 } else if (p->family == AF_ETHERNET) {
b03b8898
DS
6322 prefix2str(p, buf, PREFIX_STRLEN);
6323 len = vty_out(vty, "%s", buf);
6324 } else if (p->family == AF_EVPN) {
57f7feb6 6325 if (!json)
60466a63
QY
6326 len = vty_out(
6327 vty, "%s",
6328 bgp_evpn_route2str((struct prefix_evpn *)p, buf,
6329 BUFSIZ));
57f7feb6 6330 else
60466a63 6331 bgp_evpn_route2json((struct prefix_evpn *)p, json);
dba3c1d3
PG
6332 } else if (p->family == AF_FLOWSPEC) {
6333 route_vty_out_flowspec(vty, p, NULL,
d33fc23b
PG
6334 json ?
6335 NLRI_STRING_FORMAT_JSON_SIMPLE :
6336 NLRI_STRING_FORMAT_MIN, json);
9c92b5f7 6337 } else {
c6462ff4 6338 if (!json)
60466a63
QY
6339 len = vty_out(
6340 vty, "%s/%d",
6341 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
6342 p->prefixlen);
37d4e0df
AD
6343 else {
6344 json_object_string_add(json, "prefix",
6345 inet_ntop(p->family,
6346 &p->u.prefix, buf,
6347 BUFSIZ));
6348 json_object_int_add(json, "prefixLen", p->prefixlen);
6349 prefix2str(p, buf2, PREFIX_STRLEN);
6350 json_object_string_add(json, "network", buf2);
6351 }
9c92b5f7 6352 }
d62a17ae 6353
9c92b5f7
MK
6354 if (!json) {
6355 len = 17 - len;
6356 if (len < 1)
6357 vty_out(vty, "\n%*s", 20, " ");
6358 else
6359 vty_out(vty, "%*s", len, " ");
6360 }
718e3744 6361}
6362
d62a17ae 6363enum bgp_display_type {
6364 normal_list,
718e3744 6365};
6366
18ee8310 6367/* Print the short form route status for a bgp_path_info */
4b7e6066 6368static void route_vty_short_status_out(struct vty *vty,
9b6d8fcf 6369 struct bgp_path_info *path,
d62a17ae 6370 json_object *json_path)
718e3744 6371{
d62a17ae 6372 if (json_path) {
b05a1c8b 6373
d62a17ae 6374 /* Route status display. */
9b6d8fcf 6375 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
d62a17ae 6376 json_object_boolean_true_add(json_path, "removed");
b05a1c8b 6377
9b6d8fcf 6378 if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
d62a17ae 6379 json_object_boolean_true_add(json_path, "stale");
b05a1c8b 6380
9b6d8fcf 6381 if (path->extra && path->extra->suppress)
d62a17ae 6382 json_object_boolean_true_add(json_path, "suppressed");
b05a1c8b 6383
9b6d8fcf
DS
6384 if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
6385 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
d62a17ae 6386 json_object_boolean_true_add(json_path, "valid");
b05a1c8b 6387
d62a17ae 6388 /* Selected */
9b6d8fcf 6389 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
d62a17ae 6390 json_object_boolean_true_add(json_path, "history");
b05a1c8b 6391
9b6d8fcf 6392 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
d62a17ae 6393 json_object_boolean_true_add(json_path, "damped");
b05a1c8b 6394
9b6d8fcf 6395 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
d62a17ae 6396 json_object_boolean_true_add(json_path, "bestpath");
b05a1c8b 6397
9b6d8fcf 6398 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
d62a17ae 6399 json_object_boolean_true_add(json_path, "multipath");
b05a1c8b 6400
d62a17ae 6401 /* Internal route. */
9b6d8fcf
DS
6402 if ((path->peer->as)
6403 && (path->peer->as == path->peer->local_as))
d62a17ae 6404 json_object_string_add(json_path, "pathFrom",
6405 "internal");
6406 else
6407 json_object_string_add(json_path, "pathFrom",
6408 "external");
b05a1c8b 6409
d62a17ae 6410 return;
6411 }
b05a1c8b 6412
d62a17ae 6413 /* Route status display. */
9b6d8fcf 6414 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
d62a17ae 6415 vty_out(vty, "R");
9b6d8fcf 6416 else if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
d62a17ae 6417 vty_out(vty, "S");
9b6d8fcf 6418 else if (path->extra && path->extra->suppress)
d62a17ae 6419 vty_out(vty, "s");
9b6d8fcf
DS
6420 else if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
6421 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
d62a17ae 6422 vty_out(vty, "*");
6423 else
6424 vty_out(vty, " ");
6425
6426 /* Selected */
9b6d8fcf 6427 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
d62a17ae 6428 vty_out(vty, "h");
9b6d8fcf 6429 else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
d62a17ae 6430 vty_out(vty, "d");
9b6d8fcf 6431 else if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
d62a17ae 6432 vty_out(vty, ">");
9b6d8fcf 6433 else if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
d62a17ae 6434 vty_out(vty, "=");
6435 else
6436 vty_out(vty, " ");
718e3744 6437
d62a17ae 6438 /* Internal route. */
9b6d8fcf
DS
6439 if (path->peer && (path->peer->as)
6440 && (path->peer->as == path->peer->local_as))
d62a17ae 6441 vty_out(vty, "i");
6442 else
6443 vty_out(vty, " ");
b40d939b 6444}
6445
6446/* called from terminal list command */
4b7e6066 6447void route_vty_out(struct vty *vty, struct prefix *p,
9b6d8fcf 6448 struct bgp_path_info *path, int display, safi_t safi,
4b7e6066 6449 json_object *json_paths)
d62a17ae 6450{
6451 struct attr *attr;
6452 json_object *json_path = NULL;
6453 json_object *json_nexthops = NULL;
6454 json_object *json_nexthop_global = NULL;
6455 json_object *json_nexthop_ll = NULL;
9df8b37c 6456 char vrf_id_str[VRF_NAMSIZ] = {0};
1defdda8 6457 bool nexthop_self =
9b6d8fcf 6458 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
9df8b37c 6459 bool nexthop_othervrf = false;
43089216 6460 vrf_id_t nexthop_vrfid = VRF_DEFAULT;
9df8b37c 6461 const char *nexthop_vrfname = "Default";
d62a17ae 6462
6463 if (json_paths)
6464 json_path = json_object_new_object();
6465
6466 /* short status lead text */
9b6d8fcf 6467 route_vty_short_status_out(vty, path, json_path);
d62a17ae 6468
6469 if (!json_paths) {
6470 /* print prefix and mask */
6471 if (!display)
9c92b5f7 6472 route_vty_out_route(p, vty, json_path);
d62a17ae 6473 else
6474 vty_out(vty, "%*s", 17, " ");
9c92b5f7 6475 } else {
b682f6de 6476 route_vty_out_route(p, vty, json_path);
d62a17ae 6477 }
47fc97cc 6478
d62a17ae 6479 /* Print attribute */
9b6d8fcf 6480 attr = path->attr;
445c2480
DS
6481 if (!attr) {
6482 if (json_paths)
6483 json_object_array_add(json_paths, json_path);
6484 else
6485 vty_out(vty, "\n");
d62a17ae 6486
445c2480
DS
6487 return;
6488 }
6489
9df8b37c
PZ
6490 /*
6491 * If vrf id of nexthop is different from that of prefix,
6492 * set up printable string to append
6493 */
9b6d8fcf 6494 if (path->extra && path->extra->bgp_orig) {
9df8b37c
PZ
6495 const char *self = "";
6496
6497 if (nexthop_self)
6498 self = "<";
6499
6500 nexthop_othervrf = true;
9b6d8fcf 6501 nexthop_vrfid = path->extra->bgp_orig->vrf_id;
9df8b37c 6502
9b6d8fcf 6503 if (path->extra->bgp_orig->vrf_id == VRF_UNKNOWN)
9df8b37c
PZ
6504 snprintf(vrf_id_str, sizeof(vrf_id_str),
6505 "@%s%s", VRFID_NONE_STR, self);
6506 else
6507 snprintf(vrf_id_str, sizeof(vrf_id_str), "@%u%s",
9b6d8fcf 6508 path->extra->bgp_orig->vrf_id, self);
9df8b37c 6509
9b6d8fcf
DS
6510 if (path->extra->bgp_orig->inst_type
6511 != BGP_INSTANCE_TYPE_DEFAULT)
9df8b37c 6512
9b6d8fcf 6513 nexthop_vrfname = path->extra->bgp_orig->name;
9df8b37c
PZ
6514 } else {
6515 const char *self = "";
6516
6517 if (nexthop_self)
6518 self = "<";
6519
6520 snprintf(vrf_id_str, sizeof(vrf_id_str), "%s", self);
6521 }
6522
445c2480
DS
6523 /*
6524 * For ENCAP and EVPN routes, nexthop address family is not
6525 * neccessarily the same as the prefix address family.
6526 * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
6527 * EVPN routes are also exchanged with a MP nexthop. Currently,
6528 * this
6529 * is only IPv4, the value will be present in either
6530 * attr->nexthop or
6531 * attr->mp_nexthop_global_in
6532 */
6533 if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
6534 char buf[BUFSIZ];
6535 char nexthop[128];
6536 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
6537
6538 switch (af) {
6539 case AF_INET:
6540 sprintf(nexthop, "%s",
a4d82a8a
PZ
6541 inet_ntop(af, &attr->mp_nexthop_global_in, buf,
6542 BUFSIZ));
445c2480
DS
6543 break;
6544 case AF_INET6:
6545 sprintf(nexthop, "%s",
a4d82a8a
PZ
6546 inet_ntop(af, &attr->mp_nexthop_global, buf,
6547 BUFSIZ));
445c2480
DS
6548 break;
6549 default:
6550 sprintf(nexthop, "?");
6551 break;
d62a17ae 6552 }
d62a17ae 6553
445c2480
DS
6554 if (json_paths) {
6555 json_nexthop_global = json_object_new_object();
6556
a4d82a8a
PZ
6557 json_object_string_add(json_nexthop_global, "afi",
6558 (af == AF_INET) ? "ip" : "ipv6");
445c2480 6559 json_object_string_add(json_nexthop_global,
a4d82a8a 6560 (af == AF_INET) ? "ip" : "ipv6",
445c2480
DS
6561 nexthop);
6562 json_object_boolean_true_add(json_nexthop_global,
6563 "used");
6564 } else
9df8b37c 6565 vty_out(vty, "%s%s", nexthop, vrf_id_str);
445c2480
DS
6566 } else if (safi == SAFI_EVPN) {
6567 if (json_paths) {
6568 json_nexthop_global = json_object_new_object();
6569
6570 json_object_string_add(json_nexthop_global, "ip",
6571 inet_ntoa(attr->nexthop));
a4d82a8a
PZ
6572 json_object_string_add(json_nexthop_global, "afi",
6573 "ipv4");
445c2480
DS
6574 json_object_boolean_true_add(json_nexthop_global,
6575 "used");
6576 } else
9df8b37c
PZ
6577 vty_out(vty, "%-16s%s", inet_ntoa(attr->nexthop),
6578 vrf_id_str);
d33fc23b 6579 } else if (safi == SAFI_FLOWSPEC) {
026b914a
PG
6580 if (attr->nexthop.s_addr != 0) {
6581 if (json_paths) {
6582 json_nexthop_global = json_object_new_object();
6583 json_object_string_add(
6584 json_nexthop_global, "ip",
6585 inet_ntoa(attr->nexthop));
6586 json_object_string_add(json_nexthop_global,
6587 "afi", "ipv4");
6588 json_object_boolean_true_add(json_nexthop_global,
6589 "used");
6590 } else {
6591 vty_out(vty, "%-16s", inet_ntoa(attr->nexthop));
6592 }
6593 }
d33fc23b 6594 } else if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
445c2480
DS
6595 if (json_paths) {
6596 json_nexthop_global = json_object_new_object();
d62a17ae 6597
a4d82a8a
PZ
6598 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_EVPN))
6599 json_object_string_add(
6600 json_nexthop_global, "ip",
6601 inet_ntoa(attr->mp_nexthop_global_in));
445c2480 6602 else
a4d82a8a
PZ
6603 json_object_string_add(
6604 json_nexthop_global, "ip",
6605 inet_ntoa(attr->nexthop));
445c2480 6606
a4d82a8a
PZ
6607 json_object_string_add(json_nexthop_global, "afi",
6608 "ipv4");
445c2480
DS
6609 json_object_boolean_true_add(json_nexthop_global,
6610 "used");
6611 } else {
9df8b37c
PZ
6612 char buf[BUFSIZ];
6613
d87ff2dd 6614 snprintf(buf, sizeof(buf), "%s%s",
6615 inet_ntoa(attr->nexthop), vrf_id_str);
9df8b37c 6616 vty_out(vty, "%-16s", buf);
d62a17ae 6617 }
445c2480 6618 }
b05a1c8b 6619
445c2480 6620 /* IPv6 Next Hop */
a4d82a8a 6621 else if (p->family == AF_INET6 || BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
445c2480
DS
6622 int len;
6623 char buf[BUFSIZ];
d62a17ae 6624
445c2480
DS
6625 if (json_paths) {
6626 json_nexthop_global = json_object_new_object();
a4d82a8a
PZ
6627 json_object_string_add(
6628 json_nexthop_global, "ip",
6629 inet_ntop(AF_INET6, &attr->mp_nexthop_global,
6630 buf, BUFSIZ));
6631 json_object_string_add(json_nexthop_global, "afi",
6632 "ipv6");
6633 json_object_string_add(json_nexthop_global, "scope",
6634 "global");
445c2480
DS
6635
6636 /* We display both LL & GL if both have been
6637 * received */
6638 if ((attr->mp_nexthop_len == 32)
9b6d8fcf 6639 || (path->peer->conf_if)) {
a4d82a8a 6640 json_nexthop_ll = json_object_new_object();
d62a17ae 6641 json_object_string_add(
445c2480 6642 json_nexthop_ll, "ip",
a4d82a8a
PZ
6643 inet_ntop(AF_INET6,
6644 &attr->mp_nexthop_local, buf,
6645 BUFSIZ));
6646 json_object_string_add(json_nexthop_ll, "afi",
6647 "ipv6");
6648 json_object_string_add(json_nexthop_ll, "scope",
445c2480 6649 "link-local");
d62a17ae 6650
a4d82a8a
PZ
6651 if ((IPV6_ADDR_CMP(&attr->mp_nexthop_global,
6652 &attr->mp_nexthop_local)
445c2480
DS
6653 != 0)
6654 && !attr->mp_nexthop_prefer_global)
d62a17ae 6655 json_object_boolean_true_add(
a4d82a8a 6656 json_nexthop_ll, "used");
445c2480
DS
6657 else
6658 json_object_boolean_true_add(
a4d82a8a 6659 json_nexthop_global, "used");
445c2480
DS
6660 } else
6661 json_object_boolean_true_add(
6662 json_nexthop_global, "used");
6663 } else {
6664 /* Display LL if LL/Global both in table unless
6665 * prefer-global is set */
6666 if (((attr->mp_nexthop_len == 32)
6667 && !attr->mp_nexthop_prefer_global)
9b6d8fcf
DS
6668 || (path->peer->conf_if)) {
6669 if (path->peer->conf_if) {
a4d82a8a 6670 len = vty_out(vty, "%s",
9b6d8fcf 6671 path->peer->conf_if);
445c2480
DS
6672 len = 16 - len; /* len of IPv6
6673 addr + max
6674 len of def
6675 ifname */
6676
6677 if (len < 1)
a4d82a8a 6678 vty_out(vty, "\n%*s", 36, " ");
445c2480 6679 else
a4d82a8a 6680 vty_out(vty, "%*s", len, " ");
d62a17ae 6681 } else {
6682 len = vty_out(
9df8b37c 6683 vty, "%s%s",
d62a17ae 6684 inet_ntop(
6685 AF_INET6,
445c2480 6686 &attr->mp_nexthop_local,
9df8b37c
PZ
6687 buf, BUFSIZ),
6688 vrf_id_str);
d62a17ae 6689 len = 16 - len;
6690
6691 if (len < 1)
a4d82a8a 6692 vty_out(vty, "\n%*s", 36, " ");
d62a17ae 6693 else
a4d82a8a 6694 vty_out(vty, "%*s", len, " ");
d62a17ae 6695 }
445c2480 6696 } else {
a4d82a8a 6697 len = vty_out(
9df8b37c 6698 vty, "%s%s",
a4d82a8a
PZ
6699 inet_ntop(AF_INET6,
6700 &attr->mp_nexthop_global, buf,
9df8b37c
PZ
6701 BUFSIZ),
6702 vrf_id_str);
445c2480
DS
6703 len = 16 - len;
6704
6705 if (len < 1)
6706 vty_out(vty, "\n%*s", 36, " ");
6707 else
6708 vty_out(vty, "%*s", len, " ");
d62a17ae 6709 }
6710 }
445c2480 6711 }
718e3744 6712
445c2480
DS
6713 /* MED/Metric */
6714 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
6715 if (json_paths)
a4d82a8a 6716 json_object_int_add(json_path, "med", attr->med);
445c2480
DS
6717 else
6718 vty_out(vty, "%10u", attr->med);
6719 else if (!json_paths)
6720 vty_out(vty, " ");
d62a17ae 6721
445c2480
DS
6722 /* Local Pref */
6723 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
d62a17ae 6724 if (json_paths)
445c2480
DS
6725 json_object_int_add(json_path, "localpref",
6726 attr->local_pref);
d62a17ae 6727 else
445c2480
DS
6728 vty_out(vty, "%7u", attr->local_pref);
6729 else if (!json_paths)
6730 vty_out(vty, " ");
d62a17ae 6731
445c2480
DS
6732 if (json_paths)
6733 json_object_int_add(json_path, "weight", attr->weight);
6734 else
6735 vty_out(vty, "%7u ", attr->weight);
47fc97cc 6736
445c2480
DS
6737 if (json_paths) {
6738 char buf[BUFSIZ];
a4d82a8a
PZ
6739 json_object_string_add(
6740 json_path, "peerId",
9b6d8fcf 6741 sockunion2str(&path->peer->su, buf, SU_ADDRSTRLEN));
445c2480 6742 }
b05a1c8b 6743
445c2480
DS
6744 /* Print aspath */
6745 if (attr->aspath) {
d62a17ae 6746 if (json_paths)
445c2480
DS
6747 json_object_string_add(json_path, "aspath",
6748 attr->aspath->str);
d62a17ae 6749 else
445c2480 6750 aspath_print_vty(vty, "%s", attr->aspath, " ");
d62a17ae 6751 }
f1aa5d8a 6752
445c2480
DS
6753 /* Print origin */
6754 if (json_paths)
a4d82a8a
PZ
6755 json_object_string_add(json_path, "origin",
6756 bgp_origin_long_str[attr->origin]);
445c2480
DS
6757 else
6758 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
6759
9df8b37c
PZ
6760 if (json_paths) {
6761 if (nexthop_self)
6762 json_object_boolean_true_add(json_path,
6763 "announceNexthopSelf");
6764 if (nexthop_othervrf) {
6765 json_object_string_add(json_path, "nhVrfName",
6766 nexthop_vrfname);
6767
6768 json_object_int_add(json_path, "nhVrfId",
6769 ((nexthop_vrfid == VRF_UNKNOWN)
6770 ? -1
6771 : (int)nexthop_vrfid));
6772 }
6773 }
6774
d62a17ae 6775 if (json_paths) {
6776 if (json_nexthop_global || json_nexthop_ll) {
6777 json_nexthops = json_object_new_array();
f1aa5d8a 6778
d62a17ae 6779 if (json_nexthop_global)
6780 json_object_array_add(json_nexthops,
6781 json_nexthop_global);
f1aa5d8a 6782
d62a17ae 6783 if (json_nexthop_ll)
6784 json_object_array_add(json_nexthops,
6785 json_nexthop_ll);
f1aa5d8a 6786
d62a17ae 6787 json_object_object_add(json_path, "nexthops",
6788 json_nexthops);
6789 }
6790
6791 json_object_array_add(json_paths, json_path);
6792 } else {
6793 vty_out(vty, "\n");
65efcfce 6794#if ENABLE_BGP_VNC
d62a17ae 6795 /* prints an additional line, indented, with VNC info, if
6796 * present */
6797 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
9b6d8fcf 6798 rfapi_vty_out_vncinfo(vty, p, path, safi);
65efcfce 6799#endif
d62a17ae 6800 }
6801}
718e3744 6802
6803/* called from terminal list command */
d62a17ae 6804void route_vty_out_tmp(struct vty *vty, struct prefix *p, struct attr *attr,
9f049418 6805 safi_t safi, bool use_json, json_object *json_ar)
d62a17ae 6806{
6807 json_object *json_status = NULL;
6808 json_object *json_net = NULL;
6809 char buff[BUFSIZ];
37d4e0df 6810 char buf2[BUFSIZ];
d62a17ae 6811 /* Route status display. */
6812 if (use_json) {
6813 json_status = json_object_new_object();
6814 json_net = json_object_new_object();
6815 } else {
6816 vty_out(vty, "*");
6817 vty_out(vty, ">");
6818 vty_out(vty, " ");
6819 }
718e3744 6820
d62a17ae 6821 /* print prefix and mask */
37d4e0df 6822 if (use_json) {
d62a17ae 6823 json_object_string_add(
6824 json_net, "addrPrefix",
6825 inet_ntop(p->family, &p->u.prefix, buff, BUFSIZ));
37d4e0df
AD
6826 json_object_int_add(json_net, "prefixLen", p->prefixlen);
6827 prefix2str(p, buf2, PREFIX_STRLEN);
6828 json_object_string_add(json_net, "network", buf2);
6829 } else
9c92b5f7 6830 route_vty_out_route(p, vty, NULL);
d62a17ae 6831
6832 /* Print attribute */
6833 if (attr) {
6834 if (use_json) {
6835 if (p->family == AF_INET
6836 && (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
6837 || safi == SAFI_EVPN
6838 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
6839 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
6840 || safi == SAFI_EVPN)
6841 json_object_string_add(
6842 json_net, "nextHop",
6843 inet_ntoa(
6844 attr->mp_nexthop_global_in));
6845 else
6846 json_object_string_add(
6847 json_net, "nextHop",
6848 inet_ntoa(attr->nexthop));
6849 } else if (p->family == AF_INET6
6850 || BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
6851 char buf[BUFSIZ];
6852
6853 json_object_string_add(
6854 json_net, "netHopGloabal",
6855 inet_ntop(AF_INET6,
6856 &attr->mp_nexthop_global, buf,
6857 BUFSIZ));
6858 }
6859
6860 if (attr->flag
6861 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
6862 json_object_int_add(json_net, "metric",
6863 attr->med);
6864
6865 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
6866 json_object_int_add(json_net, "localPref",
6867 attr->local_pref);
6868
6869 json_object_int_add(json_net, "weight", attr->weight);
6870
6871 /* Print aspath */
6872 if (attr->aspath)
6873 json_object_string_add(json_net, "asPath",
6874 attr->aspath->str);
6875
6876 /* Print origin */
6877 json_object_string_add(json_net, "bgpOriginCode",
6878 bgp_origin_str[attr->origin]);
6879 } else {
6880 if (p->family == AF_INET
6881 && (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
6882 || safi == SAFI_EVPN
6883 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
6884 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
6885 || safi == SAFI_EVPN)
6886 vty_out(vty, "%-16s",
6887 inet_ntoa(
6888 attr->mp_nexthop_global_in));
6889 else
6890 vty_out(vty, "%-16s",
6891 inet_ntoa(attr->nexthop));
6892 } else if (p->family == AF_INET6
6893 || BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
6894 int len;
6895 char buf[BUFSIZ];
6896
6897 len = vty_out(
6898 vty, "%s",
6899 inet_ntop(AF_INET6,
6900 &attr->mp_nexthop_global, buf,
6901 BUFSIZ));
6902 len = 16 - len;
6903 if (len < 1)
6904 vty_out(vty, "\n%*s", 36, " ");
6905 else
6906 vty_out(vty, "%*s", len, " ");
6907 }
6908 if (attr->flag
6909 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
6910 vty_out(vty, "%10u", attr->med);
6911 else
6912 vty_out(vty, " ");
718e3744 6913
d62a17ae 6914 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
6915 vty_out(vty, "%7u", attr->local_pref);
6916 else
6917 vty_out(vty, " ");
6918
6919 vty_out(vty, "%7u ", attr->weight);
6920
6921 /* Print aspath */
6922 if (attr->aspath)
6923 aspath_print_vty(vty, "%s", attr->aspath, " ");
6924
6925 /* Print origin */
6926 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
6927 }
6928 }
6929 if (use_json) {
6930 json_object_boolean_true_add(json_status, "*");
6931 json_object_boolean_true_add(json_status, ">");
6932 json_object_object_add(json_net, "appliedStatusSymbols",
6933 json_status);
6934 char buf_cut[BUFSIZ];
6935 json_object_object_add(
6936 json_ar,
6937 inet_ntop(p->family, &p->u.prefix, buf_cut, BUFSIZ),
6938 json_net);
6939 } else
6940 vty_out(vty, "\n");
6941}
6942
6943void route_vty_out_tag(struct vty *vty, struct prefix *p,
9b6d8fcf 6944 struct bgp_path_info *path, int display, safi_t safi,
d62a17ae 6945 json_object *json)
6946{
6947 json_object *json_out = NULL;
6948 struct attr *attr;
6949 mpls_label_t label = MPLS_INVALID_LABEL;
6950
9b6d8fcf 6951 if (!path->extra)
d62a17ae 6952 return;
6953
6954 if (json)
6955 json_out = json_object_new_object();
6956
6957 /* short status lead text */
9b6d8fcf 6958 route_vty_short_status_out(vty, path, json_out);
d62a17ae 6959
6960 /* print prefix and mask */
6961 if (json == NULL) {
6962 if (!display)
9c92b5f7 6963 route_vty_out_route(p, vty, NULL);
d62a17ae 6964 else
6965 vty_out(vty, "%*s", 17, " ");
6966 }
6967
6968 /* Print attribute */
9b6d8fcf 6969 attr = path->attr;
d62a17ae 6970 if (attr) {
6971 if (((p->family == AF_INET)
6972 && ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)))
a4d82a8a 6973 || (safi == SAFI_EVPN && !BGP_ATTR_NEXTHOP_AFI_IP6(attr))
d62a17ae 6974 || (!BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
6975 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
6976 || safi == SAFI_EVPN) {
6977 if (json)
6978 json_object_string_add(
6979 json_out, "mpNexthopGlobalIn",
6980 inet_ntoa(
6981 attr->mp_nexthop_global_in));
6982 else
6983 vty_out(vty, "%-16s",
6984 inet_ntoa(
6985 attr->mp_nexthop_global_in));
6986 } else {
6987 if (json)
6988 json_object_string_add(
6989 json_out, "nexthop",
6990 inet_ntoa(attr->nexthop));
6991 else
6992 vty_out(vty, "%-16s",
6993 inet_ntoa(attr->nexthop));
6994 }
6995 } else if (((p->family == AF_INET6)
6996 && ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)))
b03b8898 6997 || (safi == SAFI_EVPN
d62a17ae 6998 && BGP_ATTR_NEXTHOP_AFI_IP6(attr))
6999 || (BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
ff44f570
DS
7000 char buf_a[512];
7001 char buf_b[512];
d62a17ae 7002 char buf_c[BUFSIZ];
7003 if (attr->mp_nexthop_len
7004 == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
7005 if (json)
7006 json_object_string_add(
7007 json_out, "mpNexthopGlobalIn",
7008 inet_ntop(
7009 AF_INET6,
7010 &attr->mp_nexthop_global,
11f9b450 7011 buf_a, sizeof(buf_a)));
d62a17ae 7012 else
7013 vty_out(vty, "%s",
7014 inet_ntop(
7015 AF_INET6,
7016 &attr->mp_nexthop_global,
11f9b450 7017 buf_a, sizeof(buf_a)));
d62a17ae 7018 } else if (attr->mp_nexthop_len
7019 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
7020 if (json) {
7021 inet_ntop(AF_INET6,
7022 &attr->mp_nexthop_global,
11f9b450 7023 buf_a, sizeof(buf_a));
d62a17ae 7024 inet_ntop(AF_INET6,
7025 &attr->mp_nexthop_local,
11f9b450 7026 buf_b, sizeof(buf_b));
d62a17ae 7027 sprintf(buf_c, "%s(%s)", buf_a, buf_b);
7028 json_object_string_add(
7029 json_out,
7030 "mpNexthopGlobalLocal", buf_c);
7031 } else
7032 vty_out(vty, "%s(%s)",
7033 inet_ntop(
7034 AF_INET6,
7035 &attr->mp_nexthop_global,
11f9b450 7036 buf_a, sizeof(buf_a)),
d62a17ae 7037 inet_ntop(
7038 AF_INET6,
7039 &attr->mp_nexthop_local,
11f9b450 7040 buf_b, sizeof(buf_b)));
d62a17ae 7041 }
7042 }
7043 }
7044
9b6d8fcf 7045 label = decode_label(&path->extra->label[0]);
d62a17ae 7046
7047 if (bgp_is_valid_label(&label)) {
7048 if (json) {
7049 json_object_int_add(json_out, "notag", label);
7050 json_object_array_add(json, json_out);
7051 } else {
7052 vty_out(vty, "notag/%d", label);
7053 vty_out(vty, "\n");
7054 }
7055 }
7056}
718e3744 7057
d62a17ae 7058void route_vty_out_overlay(struct vty *vty, struct prefix *p,
9b6d8fcf 7059 struct bgp_path_info *path, int display,
d62a17ae 7060 json_object *json_paths)
718e3744 7061{
d62a17ae 7062 struct attr *attr;
7063 char buf[BUFSIZ];
7064 json_object *json_path = NULL;
784d3a42 7065
d62a17ae 7066 if (json_paths)
7067 json_path = json_object_new_object();
856ca177 7068
9b6d8fcf 7069 if (!path->extra)
d62a17ae 7070 return;
718e3744 7071
d62a17ae 7072 /* short status lead text */
9b6d8fcf 7073 route_vty_short_status_out(vty, path, json_path);
856ca177 7074
d62a17ae 7075 /* print prefix and mask */
7076 if (!display)
9c92b5f7 7077 route_vty_out_route(p, vty, NULL);
d62a17ae 7078 else
7079 vty_out(vty, "%*s", 17, " ");
7080
7081 /* Print attribute */
9b6d8fcf 7082 attr = path->attr;
d62a17ae 7083 if (attr) {
7084 char buf1[BUFSIZ];
7085 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
7086
7087 switch (af) {
7088 case AF_INET:
7089 vty_out(vty, "%-16s",
7090 inet_ntop(af, &attr->mp_nexthop_global_in, buf,
7091 BUFSIZ));
7092 break;
7093 case AF_INET6:
7094 vty_out(vty, "%s(%s)",
7095 inet_ntop(af, &attr->mp_nexthop_global, buf,
7096 BUFSIZ),
7097 inet_ntop(af, &attr->mp_nexthop_local, buf1,
7098 BUFSIZ));
7099 break;
7100 default:
7101 vty_out(vty, "?");
7102 }
988258b4
QY
7103
7104 char *str = esi2str(&(attr->evpn_overlay.eth_s_id));
7105
7106 vty_out(vty, "%s", str);
7107 XFREE(MTYPE_TMP, str);
7108
3714a385 7109 if (is_evpn_prefix_ipaddr_v4((struct prefix_evpn *)p)) {
988258b4
QY
7110 vty_out(vty, "/%s",
7111 inet_ntoa(attr->evpn_overlay.gw_ip.ipv4));
3714a385 7112 } else if (is_evpn_prefix_ipaddr_v6((struct prefix_evpn *)p)) {
988258b4
QY
7113 vty_out(vty, "/%s",
7114 inet_ntop(AF_INET6,
7115 &(attr->evpn_overlay.gw_ip.ipv6), buf,
7116 BUFSIZ));
7117 }
7118 if (attr->ecommunity) {
7119 char *mac = NULL;
7120 struct ecommunity_val *routermac = ecommunity_lookup(
7121 attr->ecommunity, ECOMMUNITY_ENCODE_EVPN,
7122 ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC);
7123 if (routermac)
7124 mac = ecom_mac2str((char *)routermac->val);
7125 if (mac) {
7126 vty_out(vty, "/%s", (char *)mac);
7127 XFREE(MTYPE_TMP, mac);
7128 }
7129 }
7130 vty_out(vty, "\n");
718e3744 7131 }
718e3744 7132
d62a17ae 7133}
718e3744 7134
d62a17ae 7135/* dampening route */
7136static void damp_route_vty_out(struct vty *vty, struct prefix *p,
9b6d8fcf 7137 struct bgp_path_info *path, int display,
4b7e6066 7138 safi_t safi, bool use_json, json_object *json)
d62a17ae 7139{
7140 struct attr *attr;
7141 int len;
7142 char timebuf[BGP_UPTIME_LEN];
7143
7144 /* short status lead text */
9b6d8fcf 7145 route_vty_short_status_out(vty, path, json);
d62a17ae 7146
7147 /* print prefix and mask */
7148 if (!use_json) {
7149 if (!display)
9c92b5f7 7150 route_vty_out_route(p, vty, NULL);
d62a17ae 7151 else
7152 vty_out(vty, "%*s", 17, " ");
7153 }
7154
9b6d8fcf 7155 len = vty_out(vty, "%s", path->peer->host);
d62a17ae 7156 len = 17 - len;
7157 if (len < 1) {
7158 if (!use_json)
7159 vty_out(vty, "\n%*s", 34, " ");
7160 } else {
7161 if (use_json)
7162 json_object_int_add(json, "peerHost", len);
7163 else
7164 vty_out(vty, "%*s", len, " ");
7165 }
7166
7167 if (use_json)
9b6d8fcf 7168 bgp_damp_reuse_time_vty(vty, path, timebuf, BGP_UPTIME_LEN,
d62a17ae 7169 use_json, json);
7170 else
9b6d8fcf
DS
7171 vty_out(vty, "%s ",
7172 bgp_damp_reuse_time_vty(vty, path, timebuf,
7173 BGP_UPTIME_LEN, use_json,
7174 json));
d62a17ae 7175
7176 /* Print attribute */
9b6d8fcf 7177 attr = path->attr;
d62a17ae 7178 if (attr) {
7179 /* Print aspath */
7180 if (attr->aspath) {
7181 if (use_json)
7182 json_object_string_add(json, "asPath",
7183 attr->aspath->str);
7184 else
7185 aspath_print_vty(vty, "%s", attr->aspath, " ");
7186 }
7187
7188 /* Print origin */
7189 if (use_json)
7190 json_object_string_add(json, "origin",
7191 bgp_origin_str[attr->origin]);
7192 else
7193 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
7194 }
7195 if (!use_json)
7196 vty_out(vty, "\n");
7197}
718e3744 7198
d62a17ae 7199/* flap route */
7200static void flap_route_vty_out(struct vty *vty, struct prefix *p,
9b6d8fcf 7201 struct bgp_path_info *path, int display,
4b7e6066 7202 safi_t safi, bool use_json, json_object *json)
784d3a42 7203{
d62a17ae 7204 struct attr *attr;
7205 struct bgp_damp_info *bdi;
7206 char timebuf[BGP_UPTIME_LEN];
7207 int len;
784d3a42 7208
9b6d8fcf 7209 if (!path->extra)
d62a17ae 7210 return;
784d3a42 7211
9b6d8fcf 7212 bdi = path->extra->damp_info;
784d3a42 7213
d62a17ae 7214 /* short status lead text */
9b6d8fcf 7215 route_vty_short_status_out(vty, path, json);
784d3a42 7216
d62a17ae 7217 /* print prefix and mask */
7218 if (!use_json) {
7219 if (!display)
9c92b5f7 7220 route_vty_out_route(p, vty, NULL);
d62a17ae 7221 else
7222 vty_out(vty, "%*s", 17, " ");
7223 }
784d3a42 7224
9b6d8fcf 7225 len = vty_out(vty, "%s", path->peer->host);
d62a17ae 7226 len = 16 - len;
7227 if (len < 1) {
7228 if (!use_json)
7229 vty_out(vty, "\n%*s", 33, " ");
7230 } else {
7231 if (use_json)
7232 json_object_int_add(json, "peerHost", len);
7233 else
7234 vty_out(vty, "%*s", len, " ");
7235 }
784d3a42 7236
d62a17ae 7237 len = vty_out(vty, "%d", bdi->flap);
7238 len = 5 - len;
7239 if (len < 1) {
7240 if (!use_json)
7241 vty_out(vty, " ");
7242 } else {
7243 if (use_json)
7244 json_object_int_add(json, "bdiFlap", len);
7245 else
7246 vty_out(vty, "%*s", len, " ");
7247 }
7248
7249 if (use_json)
7250 peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, use_json,
7251 json);
7252 else
996c9314
LB
7253 vty_out(vty, "%s ", peer_uptime(bdi->start_time, timebuf,
7254 BGP_UPTIME_LEN, 0, NULL));
d62a17ae 7255
9b6d8fcf
DS
7256 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
7257 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
d62a17ae 7258 if (use_json)
9b6d8fcf 7259 bgp_damp_reuse_time_vty(vty, path, timebuf,
d62a17ae 7260 BGP_UPTIME_LEN, use_json, json);
7261 else
7262 vty_out(vty, "%s ",
9b6d8fcf 7263 bgp_damp_reuse_time_vty(vty, path, timebuf,
d62a17ae 7264 BGP_UPTIME_LEN,
7265 use_json, json));
7266 } else {
7267 if (!use_json)
7268 vty_out(vty, "%*s ", 8, " ");
7269 }
7270
7271 /* Print attribute */
9b6d8fcf 7272 attr = path->attr;
d62a17ae 7273 if (attr) {
7274 /* Print aspath */
7275 if (attr->aspath) {
7276 if (use_json)
7277 json_object_string_add(json, "asPath",
7278 attr->aspath->str);
7279 else
7280 aspath_print_vty(vty, "%s", attr->aspath, " ");
7281 }
7282
7283 /* Print origin */
7284 if (use_json)
7285 json_object_string_add(json, "origin",
7286 bgp_origin_str[attr->origin]);
7287 else
7288 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
7289 }
7290 if (!use_json)
7291 vty_out(vty, "\n");
7292}
7293
7294static void route_vty_out_advertised_to(struct vty *vty, struct peer *peer,
7295 int *first, const char *header,
7296 json_object *json_adv_to)
7297{
7298 char buf1[INET6_ADDRSTRLEN];
7299 json_object *json_peer = NULL;
7300
7301 if (json_adv_to) {
7302 /* 'advertised-to' is a dictionary of peers we have advertised
7303 * this
7304 * prefix too. The key is the peer's IP or swpX, the value is
7305 * the
7306 * hostname if we know it and "" if not.
7307 */
7308 json_peer = json_object_new_object();
7309
7310 if (peer->hostname)
7311 json_object_string_add(json_peer, "hostname",
7312 peer->hostname);
7313
7314 if (peer->conf_if)
7315 json_object_object_add(json_adv_to, peer->conf_if,
7316 json_peer);
7317 else
7318 json_object_object_add(
7319 json_adv_to,
7320 sockunion2str(&peer->su, buf1, SU_ADDRSTRLEN),
7321 json_peer);
7322 } else {
7323 if (*first) {
7324 vty_out(vty, "%s", header);
7325 *first = 0;
7326 }
7327
7328 if (peer->hostname
7329 && bgp_flag_check(peer->bgp, BGP_FLAG_SHOW_HOSTNAME)) {
7330 if (peer->conf_if)
7331 vty_out(vty, " %s(%s)", peer->hostname,
7332 peer->conf_if);
7333 else
7334 vty_out(vty, " %s(%s)", peer->hostname,
7335 sockunion2str(&peer->su, buf1,
7336 SU_ADDRSTRLEN));
7337 } else {
7338 if (peer->conf_if)
7339 vty_out(vty, " %s", peer->conf_if);
7340 else
7341 vty_out(vty, " %s",
7342 sockunion2str(&peer->su, buf1,
7343 SU_ADDRSTRLEN));
7344 }
7345 }
784d3a42
PG
7346}
7347
d62a17ae 7348void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct prefix *p,
9b6d8fcf 7349 struct bgp_path_info *path, afi_t afi, safi_t safi,
d62a17ae 7350 json_object *json_paths)
7351{
7352 char buf[INET6_ADDRSTRLEN];
7353 char buf1[BUFSIZ];
d62a17ae 7354 char buf2[EVPN_ROUTE_STRLEN];
d62a17ae 7355 struct attr *attr;
7356 int sockunion_vty_out(struct vty *, union sockunion *);
7357 time_t tbuf;
7358 json_object *json_bestpath = NULL;
7359 json_object *json_cluster_list = NULL;
7360 json_object *json_cluster_list_list = NULL;
7361 json_object *json_ext_community = NULL;
7362 json_object *json_last_update = NULL;
7fd077aa 7363 json_object *json_pmsi = NULL;
d62a17ae 7364 json_object *json_nexthop_global = NULL;
7365 json_object *json_nexthop_ll = NULL;
7366 json_object *json_nexthops = NULL;
7367 json_object *json_path = NULL;
7368 json_object *json_peer = NULL;
7369 json_object *json_string = NULL;
7370 json_object *json_adv_to = NULL;
7371 int first = 0;
7372 struct listnode *node, *nnode;
7373 struct peer *peer;
7374 int addpath_capable;
7375 int has_adj;
7376 unsigned int first_as;
1defdda8 7377 bool nexthop_self =
9b6d8fcf 7378 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
d62a17ae 7379
7380 if (json_paths) {
7381 json_path = json_object_new_object();
7382 json_peer = json_object_new_object();
7383 json_nexthop_global = json_object_new_object();
7384 }
7385
d62a17ae 7386 if (!json_paths && safi == SAFI_EVPN) {
b57ba6d2 7387 char tag_buf[30];
d62a17ae 7388
7389 bgp_evpn_route2str((struct prefix_evpn *)p, buf2, sizeof(buf2));
7390 vty_out(vty, " Route %s", buf2);
7391 tag_buf[0] = '\0';
9b6d8fcf
DS
7392 if (path->extra && path->extra->num_labels) {
7393 bgp_evpn_label2str(path->extra->label,
7394 path->extra->num_labels, tag_buf,
a4d82a8a 7395 sizeof(tag_buf));
d62a17ae 7396 vty_out(vty, " VNI %s", tag_buf);
7397 }
7398 vty_out(vty, "\n");
9b6d8fcf 7399 if (path->extra && path->extra->parent) {
4b7e6066 7400 struct bgp_path_info *parent_ri;
d62a17ae 7401 struct bgp_node *rn, *prn;
7402
9b6d8fcf 7403 parent_ri = (struct bgp_path_info *)path->extra->parent;
d62a17ae 7404 rn = parent_ri->net;
7405 if (rn && rn->prn) {
7406 prn = rn->prn;
7407 vty_out(vty, " Imported from %s:%s\n",
7408 prefix_rd2str(
7409 (struct prefix_rd *)&prn->p,
06b9f471 7410 buf1, sizeof(buf1)),
d62a17ae 7411 buf2);
7412 }
7413 }
7414 }
d62a17ae 7415
9b6d8fcf 7416 attr = path->attr;
d62a17ae 7417
7418 if (attr) {
7419 /* Line1 display AS-path, Aggregator */
7420 if (attr->aspath) {
7421 if (json_paths) {
68e1a55b
DS
7422 if (!attr->aspath->json)
7423 aspath_str_update(attr->aspath, true);
d62a17ae 7424 json_object_lock(attr->aspath->json);
7425 json_object_object_add(json_path, "aspath",
7426 attr->aspath->json);
7427 } else {
7428 if (attr->aspath->segments)
7429 aspath_print_vty(vty, " %s",
7430 attr->aspath, "");
7431 else
7432 vty_out(vty, " Local");
7433 }
7434 }
7435
9b6d8fcf 7436 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED)) {
d62a17ae 7437 if (json_paths)
7438 json_object_boolean_true_add(json_path,
7439 "removed");
7440 else
7441 vty_out(vty, ", (removed)");
7442 }
7443
9b6d8fcf 7444 if (CHECK_FLAG(path->flags, BGP_PATH_STALE)) {
d62a17ae 7445 if (json_paths)
7446 json_object_boolean_true_add(json_path,
7447 "stale");
7448 else
7449 vty_out(vty, ", (stale)");
7450 }
7451
7452 if (CHECK_FLAG(attr->flag,
7453 ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR))) {
7454 if (json_paths) {
7455 json_object_int_add(json_path, "aggregatorAs",
7456 attr->aggregator_as);
7457 json_object_string_add(
7458 json_path, "aggregatorId",
7459 inet_ntoa(attr->aggregator_addr));
7460 } else {
7461 vty_out(vty, ", (aggregated by %u %s)",
7462 attr->aggregator_as,
7463 inet_ntoa(attr->aggregator_addr));
7464 }
7465 }
7466
9b6d8fcf 7467 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
d62a17ae 7468 PEER_FLAG_REFLECTOR_CLIENT)) {
7469 if (json_paths)
7470 json_object_boolean_true_add(
7471 json_path, "rxedFromRrClient");
7472 else
7473 vty_out(vty, ", (Received from a RR-client)");
7474 }
7475
9b6d8fcf 7476 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
d62a17ae 7477 PEER_FLAG_RSERVER_CLIENT)) {
7478 if (json_paths)
7479 json_object_boolean_true_add(
7480 json_path, "rxedFromRsClient");
7481 else
7482 vty_out(vty, ", (Received from a RS-client)");
7483 }
7484
9b6d8fcf 7485 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
d62a17ae 7486 if (json_paths)
7487 json_object_boolean_true_add(
7488 json_path, "dampeningHistoryEntry");
7489 else
7490 vty_out(vty, ", (history entry)");
9b6d8fcf 7491 } else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)) {
d62a17ae 7492 if (json_paths)
7493 json_object_boolean_true_add(
7494 json_path, "dampeningSuppressed");
7495 else
7496 vty_out(vty, ", (suppressed due to dampening)");
7497 }
7498
7499 if (!json_paths)
7500 vty_out(vty, "\n");
7501
7502 /* Line2 display Next-hop, Neighbor, Router-id */
7503 /* Display the nexthop */
a4d82a8a
PZ
7504 if ((p->family == AF_INET || p->family == AF_ETHERNET
7505 || p->family == AF_EVPN)
d62a17ae 7506 && (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
7507 || safi == SAFI_EVPN
7508 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
7509 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
7510 || safi == SAFI_EVPN) {
7511 if (json_paths)
7512 json_object_string_add(
7513 json_nexthop_global, "ip",
7514 inet_ntoa(
7515 attr->mp_nexthop_global_in));
7516 else
7517 vty_out(vty, " %s",
7518 inet_ntoa(
7519 attr->mp_nexthop_global_in));
7520 } else {
7521 if (json_paths)
7522 json_object_string_add(
7523 json_nexthop_global, "ip",
7524 inet_ntoa(attr->nexthop));
7525 else
7526 vty_out(vty, " %s",
7527 inet_ntoa(attr->nexthop));
7528 }
7529
7530 if (json_paths)
7531 json_object_string_add(json_nexthop_global,
7532 "afi", "ipv4");
7533 } else {
7534 if (json_paths) {
7535 json_object_string_add(
7536 json_nexthop_global, "ip",
7537 inet_ntop(AF_INET6,
7538 &attr->mp_nexthop_global, buf,
7539 INET6_ADDRSTRLEN));
7540 json_object_string_add(json_nexthop_global,
7541 "afi", "ipv6");
7542 json_object_string_add(json_nexthop_global,
7543 "scope", "global");
7544 } else {
7545 vty_out(vty, " %s",
7546 inet_ntop(AF_INET6,
7547 &attr->mp_nexthop_global, buf,
7548 INET6_ADDRSTRLEN));
7549 }
7550 }
7551
7552 /* Display the IGP cost or 'inaccessible' */
9b6d8fcf 7553 if (!CHECK_FLAG(path->flags, BGP_PATH_VALID)) {
d62a17ae 7554 if (json_paths)
7555 json_object_boolean_false_add(
7556 json_nexthop_global, "accessible");
7557 else
7558 vty_out(vty, " (inaccessible)");
7559 } else {
9b6d8fcf 7560 if (path->extra && path->extra->igpmetric) {
d62a17ae 7561 if (json_paths)
7562 json_object_int_add(
7563 json_nexthop_global, "metric",
9b6d8fcf 7564 path->extra->igpmetric);
d62a17ae 7565 else
7566 vty_out(vty, " (metric %u)",
9b6d8fcf 7567 path->extra->igpmetric);
d62a17ae 7568 }
7569
7570 /* IGP cost is 0, display this only for json */
7571 else {
7572 if (json_paths)
7573 json_object_int_add(json_nexthop_global,
7574 "metric", 0);
7575 }
7576
7577 if (json_paths)
7578 json_object_boolean_true_add(
7579 json_nexthop_global, "accessible");
7580 }
7581
7582 /* Display peer "from" output */
7583 /* This path was originated locally */
9b6d8fcf 7584 if (path->peer == bgp->peer_self) {
d62a17ae 7585
7586 if (safi == SAFI_EVPN
7587 || (p->family == AF_INET
7588 && !BGP_ATTR_NEXTHOP_AFI_IP6(attr))) {
7589 if (json_paths)
7590 json_object_string_add(
7591 json_peer, "peerId", "0.0.0.0");
7592 else
7593 vty_out(vty, " from 0.0.0.0 ");
7594 } else {
7595 if (json_paths)
7596 json_object_string_add(json_peer,
7597 "peerId", "::");
7598 else
7599 vty_out(vty, " from :: ");
7600 }
7601
7602 if (json_paths)
7603 json_object_string_add(
7604 json_peer, "routerId",
7605 inet_ntoa(bgp->router_id));
7606 else
7607 vty_out(vty, "(%s)", inet_ntoa(bgp->router_id));
7608 }
7609
7610 /* We RXed this path from one of our peers */
7611 else {
7612
7613 if (json_paths) {
7614 json_object_string_add(
7615 json_peer, "peerId",
9b6d8fcf 7616 sockunion2str(&path->peer->su, buf,
d62a17ae 7617 SU_ADDRSTRLEN));
7618 json_object_string_add(
7619 json_peer, "routerId",
7620 inet_ntop(AF_INET,
9b6d8fcf 7621 &path->peer->remote_id, buf1,
06b9f471 7622 sizeof(buf1)));
d62a17ae 7623
9b6d8fcf 7624 if (path->peer->hostname)
d62a17ae 7625 json_object_string_add(
7626 json_peer, "hostname",
9b6d8fcf 7627 path->peer->hostname);
d62a17ae 7628
9b6d8fcf 7629 if (path->peer->domainname)
d62a17ae 7630 json_object_string_add(
7631 json_peer, "domainname",
9b6d8fcf 7632 path->peer->domainname);
d62a17ae 7633
9b6d8fcf 7634 if (path->peer->conf_if)
d62a17ae 7635 json_object_string_add(
7636 json_peer, "interface",
9b6d8fcf 7637 path->peer->conf_if);
d62a17ae 7638 } else {
9b6d8fcf
DS
7639 if (path->peer->conf_if) {
7640 if (path->peer->hostname
d62a17ae 7641 && bgp_flag_check(
9b6d8fcf 7642 path->peer->bgp,
d62a17ae 7643 BGP_FLAG_SHOW_HOSTNAME))
7644 vty_out(vty, " from %s(%s)",
9b6d8fcf
DS
7645 path->peer->hostname,
7646 path->peer->conf_if);
d62a17ae 7647 else
7648 vty_out(vty, " from %s",
9b6d8fcf 7649 path->peer->conf_if);
d62a17ae 7650 } else {
9b6d8fcf 7651 if (path->peer->hostname
d62a17ae 7652 && bgp_flag_check(
9b6d8fcf 7653 path->peer->bgp,
d62a17ae 7654 BGP_FLAG_SHOW_HOSTNAME))
7655 vty_out(vty, " from %s(%s)",
9b6d8fcf
DS
7656 path->peer->hostname,
7657 path->peer->host);
d62a17ae 7658 else
7659 vty_out(vty, " from %s",
7660 sockunion2str(
9b6d8fcf 7661 &path->peer->su,
d62a17ae 7662 buf,
7663 SU_ADDRSTRLEN));
7664 }
7665
7666 if (attr->flag
7667 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
7668 vty_out(vty, " (%s)",
7669 inet_ntoa(attr->originator_id));
7670 else
7671 vty_out(vty, " (%s)",
7672 inet_ntop(
7673 AF_INET,
9b6d8fcf 7674 &path->peer->remote_id,
06b9f471 7675 buf1, sizeof(buf1)));
d62a17ae 7676 }
7677 }
7678
9df8b37c
PZ
7679 /*
7680 * Note when vrfid of nexthop is different from that of prefix
7681 */
9b6d8fcf
DS
7682 if (path->extra && path->extra->bgp_orig) {
7683 vrf_id_t nexthop_vrfid = path->extra->bgp_orig->vrf_id;
9df8b37c
PZ
7684
7685 if (json_paths) {
7686 const char *vn;
7687
9b6d8fcf
DS
7688 if (path->extra->bgp_orig->inst_type
7689 == BGP_INSTANCE_TYPE_DEFAULT)
9df8b37c
PZ
7690
7691 vn = "Default";
7692 else
9b6d8fcf 7693 vn = path->extra->bgp_orig->name;
9df8b37c
PZ
7694
7695 json_object_string_add(json_path, "nhVrfName",
7696 vn);
7697
7698 if (nexthop_vrfid == VRF_UNKNOWN) {
7699 json_object_int_add(json_path,
7700 "nhVrfId", -1);
7701 } else {
7702 json_object_int_add(json_path,
7703 "nhVrfId", (int)nexthop_vrfid);
7704 }
7705 } else {
7706 if (nexthop_vrfid == VRF_UNKNOWN)
7707 vty_out(vty, " vrf ?");
7708 else
7709 vty_out(vty, " vrf %u", nexthop_vrfid);
7710 }
7711 }
7712
7713 if (nexthop_self) {
7714 if (json_paths) {
7715 json_object_boolean_true_add(json_path,
7716 "announceNexthopSelf");
7717 } else {
7718 vty_out(vty, " announce-nh-self");
7719 }
7720 }
7721
d62a17ae 7722 if (!json_paths)
7723 vty_out(vty, "\n");
7724
7725 /* display the link-local nexthop */
7726 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
7727 if (json_paths) {
7728 json_nexthop_ll = json_object_new_object();
7729 json_object_string_add(
7730 json_nexthop_ll, "ip",
7731 inet_ntop(AF_INET6,
7732 &attr->mp_nexthop_local, buf,
7733 INET6_ADDRSTRLEN));
7734 json_object_string_add(json_nexthop_ll, "afi",
7735 "ipv6");
7736 json_object_string_add(json_nexthop_ll, "scope",
7737 "link-local");
7738
7739 json_object_boolean_true_add(json_nexthop_ll,
7740 "accessible");
7741
7742 if (!attr->mp_nexthop_prefer_global)
7743 json_object_boolean_true_add(
7744 json_nexthop_ll, "used");
7745 else
7746 json_object_boolean_true_add(
7747 json_nexthop_global, "used");
7748 } else {
7749 vty_out(vty, " (%s) %s\n",
7750 inet_ntop(AF_INET6,
7751 &attr->mp_nexthop_local, buf,
7752 INET6_ADDRSTRLEN),
7753 attr->mp_nexthop_prefer_global
7754 ? "(prefer-global)"
7755 : "(used)");
7756 }
7757 }
7758 /* If we do not have a link-local nexthop then we must flag the
7759 global as "used" */
7760 else {
7761 if (json_paths)
7762 json_object_boolean_true_add(
7763 json_nexthop_global, "used");
7764 }
718e3744 7765
d62a17ae 7766 /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid,
7767 * Int/Ext/Local, Atomic, best */
7768 if (json_paths)
7769 json_object_string_add(
7770 json_path, "origin",
7771 bgp_origin_long_str[attr->origin]);
7772 else
7773 vty_out(vty, " Origin %s",
7774 bgp_origin_long_str[attr->origin]);
7775
7776 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
7777 if (json_paths)
7778 json_object_int_add(json_path, "med",
7779 attr->med);
7780 else
7781 vty_out(vty, ", metric %u", attr->med);
7782 }
718e3744 7783
d62a17ae 7784 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
7785 if (json_paths)
7786 json_object_int_add(json_path, "localpref",
7787 attr->local_pref);
7788 else
7789 vty_out(vty, ", localpref %u",
7790 attr->local_pref);
d62a17ae 7791 }
718e3744 7792
d62a17ae 7793 if (attr->weight != 0) {
7794 if (json_paths)
7795 json_object_int_add(json_path, "weight",
7796 attr->weight);
7797 else
7798 vty_out(vty, ", weight %u", attr->weight);
7799 }
718e3744 7800
d62a17ae 7801 if (attr->tag != 0) {
7802 if (json_paths)
7803 json_object_int_add(json_path, "tag",
7804 attr->tag);
7805 else
7806 vty_out(vty, ", tag %" ROUTE_TAG_PRI,
7807 attr->tag);
7808 }
718e3744 7809
9b6d8fcf 7810 if (!CHECK_FLAG(path->flags, BGP_PATH_VALID)) {
d62a17ae 7811 if (json_paths)
7812 json_object_boolean_false_add(json_path,
7813 "valid");
7814 else
7815 vty_out(vty, ", invalid");
9b6d8fcf 7816 } else if (!CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
d62a17ae 7817 if (json_paths)
7818 json_object_boolean_true_add(json_path,
7819 "valid");
7820 else
7821 vty_out(vty, ", valid");
7822 }
718e3744 7823
9b6d8fcf
DS
7824 if (path->peer != bgp->peer_self) {
7825 if (path->peer->as == path->peer->local_as) {
d62a17ae 7826 if (CHECK_FLAG(bgp->config,
7827 BGP_CONFIG_CONFEDERATION)) {
7828 if (json_paths)
7829 json_object_string_add(
7830 json_peer, "type",
7831 "confed-internal");
7832 else
7833 vty_out(vty,
7834 ", confed-internal");
7835 } else {
7836 if (json_paths)
7837 json_object_string_add(
7838 json_peer, "type",
7839 "internal");
7840 else
7841 vty_out(vty, ", internal");
7842 }
7843 } else {
7844 if (bgp_confederation_peers_check(
9b6d8fcf 7845 bgp, path->peer->as)) {
d62a17ae 7846 if (json_paths)
7847 json_object_string_add(
7848 json_peer, "type",
7849 "confed-external");
7850 else
7851 vty_out(vty,
7852 ", confed-external");
7853 } else {
7854 if (json_paths)
7855 json_object_string_add(
7856 json_peer, "type",
7857 "external");
7858 else
7859 vty_out(vty, ", external");
7860 }
7861 }
9b6d8fcf 7862 } else if (path->sub_type == BGP_ROUTE_AGGREGATE) {
d62a17ae 7863 if (json_paths) {
7864 json_object_boolean_true_add(json_path,
7865 "aggregated");
7866 json_object_boolean_true_add(json_path,
7867 "local");
7868 } else {
7869 vty_out(vty, ", aggregated, local");
7870 }
9b6d8fcf 7871 } else if (path->type != ZEBRA_ROUTE_BGP) {
d62a17ae 7872 if (json_paths)
7873 json_object_boolean_true_add(json_path,
7874 "sourced");
7875 else
7876 vty_out(vty, ", sourced");
7877 } else {
7878 if (json_paths) {
7879 json_object_boolean_true_add(json_path,
7880 "sourced");
7881 json_object_boolean_true_add(json_path,
7882 "local");
7883 } else {
7884 vty_out(vty, ", sourced, local");
7885 }
7886 }
718e3744 7887
d62a17ae 7888 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
7889 if (json_paths)
7890 json_object_boolean_true_add(json_path,
7891 "atomicAggregate");
7892 else
7893 vty_out(vty, ", atomic-aggregate");
7894 }
718e3744 7895
9b6d8fcf
DS
7896 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH)
7897 || (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)
7898 && bgp_path_info_mpath_count(path))) {
d62a17ae 7899 if (json_paths)
7900 json_object_boolean_true_add(json_path,
7901 "multipath");
7902 else
7903 vty_out(vty, ", multipath");
7904 }
856ca177 7905
d62a17ae 7906 // Mark the bestpath(s)
9b6d8fcf 7907 if (CHECK_FLAG(path->flags, BGP_PATH_DMED_SELECTED)) {
d62a17ae 7908 first_as = aspath_get_first_as(attr->aspath);
7909
7910 if (json_paths) {
7911 if (!json_bestpath)
7912 json_bestpath =
7913 json_object_new_object();
7914 json_object_int_add(json_bestpath,
7915 "bestpathFromAs", first_as);
7916 } else {
7917 if (first_as)
fb2b0934 7918 vty_out(vty, ", bestpath-from-AS %u",
d62a17ae 7919 first_as);
7920 else
7921 vty_out(vty,
7922 ", bestpath-from-AS Local");
7923 }
7924 }
718e3744 7925
9b6d8fcf 7926 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)) {
d62a17ae 7927 if (json_paths) {
7928 if (!json_bestpath)
7929 json_bestpath =
7930 json_object_new_object();
7931 json_object_boolean_true_add(json_bestpath,
7932 "overall");
7933 } else
7934 vty_out(vty, ", best");
7935 }
718e3744 7936
d62a17ae 7937 if (json_bestpath)
7938 json_object_object_add(json_path, "bestpath",
7939 json_bestpath);
7940
7941 if (!json_paths)
7942 vty_out(vty, "\n");
7943
7944 /* Line 4 display Community */
7945 if (attr->community) {
7946 if (json_paths) {
a69ea8ae 7947 if (!attr->community->json)
a4d82a8a 7948 community_str(attr->community, true);
d62a17ae 7949 json_object_lock(attr->community->json);
7950 json_object_object_add(json_path, "community",
7951 attr->community->json);
7952 } else {
7953 vty_out(vty, " Community: %s\n",
7954 attr->community->str);
7955 }
7956 }
718e3744 7957
d62a17ae 7958 /* Line 5 display Extended-community */
7959 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
7960 if (json_paths) {
7961 json_ext_community = json_object_new_object();
7962 json_object_string_add(json_ext_community,
7963 "string",
7964 attr->ecommunity->str);
7965 json_object_object_add(json_path,
7966 "extendedCommunity",
7967 json_ext_community);
7968 } else {
7969 vty_out(vty, " Extended Community: %s\n",
7970 attr->ecommunity->str);
7971 }
7972 }
adbac85e 7973
d62a17ae 7974 /* Line 6 display Large community */
b96879c0
DS
7975 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) {
7976 if (json_paths) {
8d9b8ed9
PM
7977 if (!attr->lcommunity->json)
7978 lcommunity_str(attr->lcommunity, true);
7979 json_object_lock(attr->lcommunity->json);
b96879c0
DS
7980 json_object_object_add(json_path,
7981 "largeCommunity",
8d9b8ed9 7982 attr->lcommunity->json);
b96879c0
DS
7983 } else {
7984 vty_out(vty, " Large Community: %s\n",
7985 attr->lcommunity->str);
7986 }
7987 }
d62a17ae 7988
7989 /* Line 7 display Originator, Cluster-id */
7990 if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
7991 || (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))) {
7992 if (attr->flag
7993 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) {
7994 if (json_paths)
7995 json_object_string_add(
7996 json_path, "originatorId",
7997 inet_ntoa(attr->originator_id));
7998 else
7999 vty_out(vty, " Originator: %s",
8000 inet_ntoa(attr->originator_id));
8001 }
8002
8003 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) {
8004 int i;
8005
8006 if (json_paths) {
8007 json_cluster_list =
8008 json_object_new_object();
8009 json_cluster_list_list =
8010 json_object_new_array();
8011
8012 for (i = 0;
8013 i < attr->cluster->length / 4;
8014 i++) {
8015 json_string = json_object_new_string(
8016 inet_ntoa(
8017 attr->cluster->list
8018 [i]));
8019 json_object_array_add(
8020 json_cluster_list_list,
8021 json_string);
8022 }
8023
8024 /* struct cluster_list does not have
8025 "str" variable like
8026 * aspath and community do. Add this
8027 someday if someone
8028 * asks for it.
8029 json_object_string_add(json_cluster_list,
8030 "string", attr->cluster->str);
8031 */
8032 json_object_object_add(
8033 json_cluster_list, "list",
8034 json_cluster_list_list);
8035 json_object_object_add(
8036 json_path, "clusterList",
8037 json_cluster_list);
8038 } else {
8039 vty_out(vty, ", Cluster list: ");
8040
8041 for (i = 0;
8042 i < attr->cluster->length / 4;
8043 i++) {
8044 vty_out(vty, "%s ",
8045 inet_ntoa(
8046 attr->cluster->list
8047 [i]));
8048 }
8049 }
8050 }
8051
8052 if (!json_paths)
8053 vty_out(vty, "\n");
8054 }
adbac85e 8055
9b6d8fcf
DS
8056 if (path->extra && path->extra->damp_info)
8057 bgp_damp_info_vty(vty, path, json_path);
d62a17ae 8058
6a527b2f 8059 /* Remote Label */
9b6d8fcf 8060 if (path->extra && bgp_is_valid_label(&path->extra->label[0])
6a527b2f 8061 && safi != SAFI_EVPN) {
9b6d8fcf 8062 mpls_label_t label = label_pton(&path->extra->label[0]);
121e245d 8063
d62a17ae 8064 if (json_paths)
8065 json_object_int_add(json_path, "remoteLabel",
8066 label);
8067 else
8068 vty_out(vty, " Remote label: %d\n", label);
8069 }
b05a1c8b 8070
d62a17ae 8071 /* Label Index */
8072 if (attr->label_index != BGP_INVALID_LABEL_INDEX) {
8073 if (json_paths)
8074 json_object_int_add(json_path, "labelIndex",
8075 attr->label_index);
8076 else
8077 vty_out(vty, " Label Index: %d\n",
8078 attr->label_index);
8079 }
520d5d76 8080
d62a17ae 8081 /* Line 8 display Addpath IDs */
9b6d8fcf 8082 if (path->addpath_rx_id || path->addpath_tx_id) {
d62a17ae 8083 if (json_paths) {
8084 json_object_int_add(json_path, "addpathRxId",
9b6d8fcf 8085 path->addpath_rx_id);
d62a17ae 8086 json_object_int_add(json_path, "addpathTxId",
9b6d8fcf 8087 path->addpath_tx_id);
d62a17ae 8088 } else {
8089 vty_out(vty, " AddPath ID: RX %u, TX %u\n",
9b6d8fcf
DS
8090 path->addpath_rx_id,
8091 path->addpath_tx_id);
d62a17ae 8092 }
8093 }
718e3744 8094
d62a17ae 8095 /* If we used addpath to TX a non-bestpath we need to display
8096 * "Advertised to" on a path-by-path basis */
8097 if (bgp->addpath_tx_used[afi][safi]) {
8098 first = 1;
8099
8100 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8101 addpath_capable =
8102 bgp_addpath_encode_tx(peer, afi, safi);
8103 has_adj = bgp_adj_out_lookup(
9b6d8fcf 8104 peer, path->net, path->addpath_tx_id);
d62a17ae 8105
8106 if ((addpath_capable && has_adj)
8107 || (!addpath_capable && has_adj
9b6d8fcf 8108 && CHECK_FLAG(path->flags,
1defdda8 8109 BGP_PATH_SELECTED))) {
d62a17ae 8110 if (json_path && !json_adv_to)
8111 json_adv_to =
8112 json_object_new_object();
8113
8114 route_vty_out_advertised_to(
8115 vty, peer, &first,
8116 " Advertised to:",
8117 json_adv_to);
8118 }
8119 }
8120
8121 if (json_path) {
8122 if (json_adv_to) {
8123 json_object_object_add(json_path,
8124 "advertisedTo",
8125 json_adv_to);
8126 }
8127 } else {
8128 if (!first) {
8129 vty_out(vty, "\n");
8130 }
8131 }
8132 }
b05a1c8b 8133
d62a17ae 8134 /* Line 9 display Uptime */
9b6d8fcf 8135 tbuf = time(NULL) - (bgp_clock() - path->uptime);
d62a17ae 8136 if (json_paths) {
8137 json_last_update = json_object_new_object();
8138 json_object_int_add(json_last_update, "epoch", tbuf);
8139 json_object_string_add(json_last_update, "string",
8140 ctime(&tbuf));
8141 json_object_object_add(json_path, "lastUpdate",
8142 json_last_update);
8143 } else
8144 vty_out(vty, " Last update: %s", ctime(&tbuf));
7fd077aa 8145
8146 /* Line 10 display PMSI tunnel attribute, if present */
8147 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)) {
b7d08f5a 8148 const char *str = lookup_msg(bgp_pmsi_tnltype_str,
8149 attr->pmsi_tnl_type,
8150 PMSI_TNLTYPE_STR_DEFAULT);
8151
7fd077aa 8152 if (json_paths) {
8153 json_pmsi = json_object_new_object();
b7d08f5a 8154 json_object_string_add(json_pmsi,
8155 "tunnelType", str);
7fd077aa 8156 json_object_object_add(json_path, "pmsi",
8157 json_pmsi);
8158 } else
8159 vty_out(vty, " PMSI Tunnel Type: %s\n",
b7d08f5a 8160 str);
7fd077aa 8161 }
8162
d62a17ae 8163 }
f1aa5d8a 8164
d62a17ae 8165 /* We've constructed the json object for this path, add it to the json
8166 * array of paths
8167 */
8168 if (json_paths) {
8169 if (json_nexthop_global || json_nexthop_ll) {
8170 json_nexthops = json_object_new_array();
f1aa5d8a 8171
d62a17ae 8172 if (json_nexthop_global)
8173 json_object_array_add(json_nexthops,
8174 json_nexthop_global);
f1aa5d8a 8175
d62a17ae 8176 if (json_nexthop_ll)
8177 json_object_array_add(json_nexthops,
8178 json_nexthop_ll);
f1aa5d8a 8179
d62a17ae 8180 json_object_object_add(json_path, "nexthops",
8181 json_nexthops);
8182 }
8183
8184 json_object_object_add(json_path, "peer", json_peer);
8185 json_object_array_add(json_paths, json_path);
8186 } else
8187 vty_out(vty, "\n");
b366b518
BB
8188}
8189
96ade3ed 8190#define BGP_SHOW_HEADER_CSV "Flags, Network, Next Hop, Metric, LocPrf, Weight, Path"
181039f3
DL
8191#define BGP_SHOW_DAMP_HEADER " Network From Reuse Path\n"
8192#define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path\n"
718e3744 8193
d62a17ae 8194static int bgp_show_prefix_list(struct vty *vty, struct bgp *bgp,
8195 const char *prefix_list_str, afi_t afi,
8196 safi_t safi, enum bgp_show_type type);
8197static int bgp_show_filter_list(struct vty *vty, struct bgp *bgp,
8198 const char *filter, afi_t afi, safi_t safi,
8199 enum bgp_show_type type);
8200static int bgp_show_route_map(struct vty *vty, struct bgp *bgp,
8201 const char *rmap_str, afi_t afi, safi_t safi,
8202 enum bgp_show_type type);
8203static int bgp_show_community_list(struct vty *vty, struct bgp *bgp,
8204 const char *com, int exact, afi_t afi,
8205 safi_t safi);
8206static int bgp_show_prefix_longer(struct vty *vty, struct bgp *bgp,
8207 const char *prefix, afi_t afi, safi_t safi,
8208 enum bgp_show_type type);
a4d82a8a
PZ
8209static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
8210 afi_t afi, safi_t safi, enum bgp_show_type type);
7f323236
DW
8211static int bgp_show_community(struct vty *vty, struct bgp *bgp,
8212 const char *comstr, int exact, afi_t afi,
9f049418 8213 safi_t safi, bool use_json);
d62a17ae 8214
1ae44dfc
LB
8215
8216static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
d62a17ae 8217 struct bgp_table *table, enum bgp_show_type type,
9f049418 8218 void *output_arg, bool use_json, char *rd,
a4d82a8a
PZ
8219 int is_last, unsigned long *output_cum,
8220 unsigned long *total_cum,
9386b588 8221 unsigned long *json_header_depth)
d62a17ae 8222{
40381db7 8223 struct bgp_path_info *pi;
d62a17ae 8224 struct bgp_node *rn;
8225 int header = 1;
8226 int display;
1ae44dfc
LB
8227 unsigned long output_count = 0;
8228 unsigned long total_count = 0;
d62a17ae 8229 struct prefix *p;
8230 char buf[BUFSIZ];
8231 char buf2[BUFSIZ];
8232 json_object *json_paths = NULL;
8233 int first = 1;
8234
1ae44dfc
LB
8235 if (output_cum && *output_cum != 0)
8236 header = 0;
8237
9386b588 8238 if (use_json && !*json_header_depth) {
d62a17ae 8239 vty_out(vty,
66f80d74
DS
8240 "{\n \"vrfId\": %d,\n \"vrfName\": \"%s\",\n \"tableVersion\": %" PRId64
8241 ",\n \"routerId\": \"%s\",\n \"routes\": { ",
a8bf7d9c 8242 bgp->vrf_id == VRF_UNKNOWN ? -1 : (int)bgp->vrf_id,
d62a17ae 8243 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT ? "Default"
8244 : bgp->name,
8245 table->version, inet_ntoa(bgp->router_id));
9386b588
PZ
8246 *json_header_depth = 2;
8247 if (rd) {
445c2480 8248 vty_out(vty, " \"routeDistinguishers\" : {");
9386b588
PZ
8249 ++*json_header_depth;
8250 }
d62a17ae 8251 }
718e3744 8252
445c2480
DS
8253 if (use_json && rd) {
8254 vty_out(vty, " \"%s\" : { ", rd);
8255 }
8256
d62a17ae 8257 /* Start processing of routes. */
98ce9a06
DS
8258 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
8259 if (rn->info == NULL)
8260 continue;
d62a17ae 8261
98ce9a06 8262 display = 0;
98ce9a06
DS
8263 if (use_json)
8264 json_paths = json_object_new_array();
8265 else
8266 json_paths = NULL;
d62a17ae 8267
40381db7 8268 for (pi = rn->info; pi; pi = pi->next) {
98ce9a06
DS
8269 total_count++;
8270 if (type == bgp_show_type_flap_statistics
8271 || type == bgp_show_type_flap_neighbor
8272 || type == bgp_show_type_dampend_paths
8273 || type == bgp_show_type_damp_neighbor) {
40381db7 8274 if (!(pi->extra && pi->extra->damp_info))
98ce9a06
DS
8275 continue;
8276 }
8277 if (type == bgp_show_type_regexp) {
8278 regex_t *regex = output_arg;
d62a17ae 8279
40381db7 8280 if (bgp_regexec(regex, pi->attr->aspath)
98ce9a06
DS
8281 == REG_NOMATCH)
8282 continue;
8283 }
8284 if (type == bgp_show_type_prefix_list) {
8285 struct prefix_list *plist = output_arg;
d62a17ae 8286
98ce9a06
DS
8287 if (prefix_list_apply(plist, &rn->p)
8288 != PREFIX_PERMIT)
8289 continue;
8290 }
8291 if (type == bgp_show_type_filter_list) {
8292 struct as_list *as_list = output_arg;
d62a17ae 8293
40381db7 8294 if (as_list_apply(as_list, pi->attr->aspath)
98ce9a06
DS
8295 != AS_FILTER_PERMIT)
8296 continue;
8297 }
8298 if (type == bgp_show_type_route_map) {
8299 struct route_map *rmap = output_arg;
9b6d8fcf 8300 struct bgp_path_info path;
98ce9a06
DS
8301 struct attr dummy_attr;
8302 int ret;
d62a17ae 8303
40381db7 8304 bgp_attr_dup(&dummy_attr, pi->attr);
d62a17ae 8305
40381db7 8306 path.peer = pi->peer;
9b6d8fcf 8307 path.attr = &dummy_attr;
d62a17ae 8308
a4d82a8a 8309 ret = route_map_apply(rmap, &rn->p, RMAP_BGP,
9b6d8fcf 8310 &path);
98ce9a06
DS
8311 if (ret == RMAP_DENYMATCH)
8312 continue;
8313 }
8314 if (type == bgp_show_type_neighbor
8315 || type == bgp_show_type_flap_neighbor
8316 || type == bgp_show_type_damp_neighbor) {
8317 union sockunion *su = output_arg;
8318
40381db7
DS
8319 if (pi->peer == NULL
8320 || pi->peer->su_remote == NULL
8321 || !sockunion_same(pi->peer->su_remote, su))
98ce9a06
DS
8322 continue;
8323 }
8324 if (type == bgp_show_type_cidr_only) {
d7c0a89a 8325 uint32_t destination;
d62a17ae 8326
98ce9a06
DS
8327 destination = ntohl(rn->p.u.prefix4.s_addr);
8328 if (IN_CLASSC(destination)
8329 && rn->p.prefixlen == 24)
8330 continue;
8331 if (IN_CLASSB(destination)
8332 && rn->p.prefixlen == 16)
8333 continue;
8334 if (IN_CLASSA(destination)
8335 && rn->p.prefixlen == 8)
8336 continue;
8337 }
8338 if (type == bgp_show_type_prefix_longer) {
f7813c7c 8339 p = output_arg;
98ce9a06
DS
8340 if (!prefix_match(p, &rn->p))
8341 continue;
8342 }
8343 if (type == bgp_show_type_community_all) {
40381db7 8344 if (!pi->attr->community)
98ce9a06
DS
8345 continue;
8346 }
8347 if (type == bgp_show_type_community) {
8348 struct community *com = output_arg;
d62a17ae 8349
40381db7
DS
8350 if (!pi->attr->community
8351 || !community_match(pi->attr->community,
98ce9a06
DS
8352 com))
8353 continue;
8354 }
8355 if (type == bgp_show_type_community_exact) {
8356 struct community *com = output_arg;
d62a17ae 8357
40381db7
DS
8358 if (!pi->attr->community
8359 || !community_cmp(pi->attr->community, com))
98ce9a06
DS
8360 continue;
8361 }
8362 if (type == bgp_show_type_community_list) {
8363 struct community_list *list = output_arg;
d62a17ae 8364
40381db7 8365 if (!community_list_match(pi->attr->community,
a4d82a8a 8366 list))
98ce9a06
DS
8367 continue;
8368 }
a4d82a8a 8369 if (type == bgp_show_type_community_list_exact) {
98ce9a06 8370 struct community_list *list = output_arg;
d62a17ae 8371
98ce9a06 8372 if (!community_list_exact_match(
40381db7 8373 pi->attr->community, list))
98ce9a06
DS
8374 continue;
8375 }
8376 if (type == bgp_show_type_lcommunity) {
8377 struct lcommunity *lcom = output_arg;
d62a17ae 8378
40381db7
DS
8379 if (!pi->attr->lcommunity
8380 || !lcommunity_match(pi->attr->lcommunity,
98ce9a06
DS
8381 lcom))
8382 continue;
8383 }
8384 if (type == bgp_show_type_lcommunity_list) {
8385 struct community_list *list = output_arg;
d62a17ae 8386
40381db7 8387 if (!lcommunity_list_match(pi->attr->lcommunity,
a4d82a8a 8388 list))
98ce9a06
DS
8389 continue;
8390 }
8391 if (type == bgp_show_type_lcommunity_all) {
40381db7 8392 if (!pi->attr->lcommunity)
98ce9a06
DS
8393 continue;
8394 }
8395 if (type == bgp_show_type_dampend_paths
8396 || type == bgp_show_type_damp_neighbor) {
40381db7
DS
8397 if (!CHECK_FLAG(pi->flags, BGP_PATH_DAMPED)
8398 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
98ce9a06
DS
8399 continue;
8400 }
8401
8402 if (!use_json && header) {
996c9314 8403 vty_out(vty, "BGP table version is %" PRIu64
9df8b37c 8404 ", local router ID is %s, vrf id ",
98ce9a06
DS
8405 table->version,
8406 inet_ntoa(bgp->router_id));
9df8b37c
PZ
8407 if (bgp->vrf_id == VRF_UNKNOWN)
8408 vty_out(vty, "%s", VRFID_NONE_STR);
8409 else
8410 vty_out(vty, "%u", bgp->vrf_id);
8411 vty_out(vty, "\n");
98ce9a06 8412 vty_out(vty, BGP_SHOW_SCODE_HEADER);
9df8b37c 8413 vty_out(vty, BGP_SHOW_NCODE_HEADER);
98ce9a06 8414 vty_out(vty, BGP_SHOW_OCODE_HEADER);
d62a17ae 8415 if (type == bgp_show_type_dampend_paths
8416 || type == bgp_show_type_damp_neighbor)
98ce9a06 8417 vty_out(vty, BGP_SHOW_DAMP_HEADER);
a4d82a8a
PZ
8418 else if (type == bgp_show_type_flap_statistics
8419 || type == bgp_show_type_flap_neighbor)
98ce9a06 8420 vty_out(vty, BGP_SHOW_FLAP_HEADER);
d62a17ae 8421 else
98ce9a06
DS
8422 vty_out(vty, BGP_SHOW_HEADER);
8423 header = 0;
d62a17ae 8424 }
98ce9a06
DS
8425 if (rd != NULL && !display && !output_count) {
8426 if (!use_json)
8427 vty_out(vty,
8428 "Route Distinguisher: %s\n",
8429 rd);
d62a17ae 8430 }
98ce9a06
DS
8431 if (type == bgp_show_type_dampend_paths
8432 || type == bgp_show_type_damp_neighbor)
40381db7 8433 damp_route_vty_out(vty, &rn->p, pi, display,
a4d82a8a 8434 safi, use_json, json_paths);
98ce9a06
DS
8435 else if (type == bgp_show_type_flap_statistics
8436 || type == bgp_show_type_flap_neighbor)
40381db7 8437 flap_route_vty_out(vty, &rn->p, pi, display,
a4d82a8a 8438 safi, use_json, json_paths);
98ce9a06 8439 else
40381db7 8440 route_vty_out(vty, &rn->p, pi, display, safi,
a4d82a8a 8441 json_paths);
98ce9a06 8442 display++;
d62a17ae 8443 }
8444
98ce9a06
DS
8445 if (display) {
8446 output_count++;
8447 if (!use_json)
8448 continue;
8449
8450 p = &rn->p;
8451 sprintf(buf2, "%s/%d",
a4d82a8a 8452 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
98ce9a06 8453 p->prefixlen);
8cc84563
DS
8454 if (first)
8455 vty_out(vty, "\"%s\": ", buf2);
8456 else
8457 vty_out(vty, ",\"%s\": ", buf2);
8458
98ce9a06 8459 vty_out(vty, "%s",
23b2a7ef 8460 json_object_to_json_string(json_paths));
98ce9a06 8461 json_object_free(json_paths);
449feb8e 8462 json_paths = NULL;
98ce9a06
DS
8463 first = 0;
8464 }
8465 }
8466
1ae44dfc
LB
8467 if (output_cum) {
8468 output_count += *output_cum;
8469 *output_cum = output_count;
8470 }
8471 if (total_cum) {
8472 total_count += *total_cum;
8473 *total_cum = total_count;
8474 }
d62a17ae 8475 if (use_json) {
9386b588 8476 if (rd) {
a4d82a8a 8477 vty_out(vty, " }%s ", (is_last ? "" : ","));
9386b588
PZ
8478 }
8479 if (is_last) {
a4d82a8a
PZ
8480 unsigned long i;
8481 for (i = 0; i < *json_header_depth; ++i)
8482 vty_out(vty, " } ");
9386b588 8483 }
d62a17ae 8484 } else {
1ae44dfc
LB
8485 if (is_last) {
8486 /* No route is displayed */
8487 if (output_count == 0) {
8488 if (type == bgp_show_type_normal)
8489 vty_out(vty,
8490 "No BGP prefixes displayed, %ld exist\n",
8491 total_count);
8492 } else
d62a17ae 8493 vty_out(vty,
1ae44dfc
LB
8494 "\nDisplayed %ld routes and %ld total paths\n",
8495 output_count, total_count);
8496 }
d62a17ae 8497 }
718e3744 8498
d62a17ae 8499 return CMD_SUCCESS;
718e3744 8500}
8501
1ae44dfc
LB
8502int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi,
8503 struct bgp_table *table, struct prefix_rd *prd_match,
9f049418 8504 enum bgp_show_type type, void *output_arg, bool use_json)
1ae44dfc
LB
8505{
8506 struct bgp_node *rn, *next;
8507 unsigned long output_cum = 0;
8508 unsigned long total_cum = 0;
9386b588 8509 unsigned long json_header_depth = 0;
0136788c
LB
8510 bool show_msg;
8511
8512 show_msg = (!use_json && type == bgp_show_type_normal);
1ae44dfc
LB
8513
8514 for (rn = bgp_table_top(table); rn; rn = next) {
8515 next = bgp_route_next(rn);
8516 if (prd_match && memcmp(rn->p.u.val, prd_match->val, 8) != 0)
8517 continue;
8518 if (rn->info != NULL) {
8519 struct prefix_rd prd;
06b9f471 8520 char rd[RD_ADDRSTRLEN];
1ae44dfc
LB
8521
8522 memcpy(&prd, &(rn->p), sizeof(struct prefix_rd));
06b9f471 8523 prefix_rd2str(&prd, rd, sizeof(rd));
1ae44dfc 8524 bgp_show_table(vty, bgp, safi, rn->info, type,
a4d82a8a 8525 output_arg, use_json, rd, next == NULL,
9386b588
PZ
8526 &output_cum, &total_cum,
8527 &json_header_depth);
0136788c
LB
8528 if (next == NULL)
8529 show_msg = false;
1ae44dfc
LB
8530 }
8531 }
0136788c
LB
8532 if (show_msg) {
8533 if (output_cum == 0)
8534 vty_out(vty, "No BGP prefixes displayed, %ld exist\n",
8535 total_cum);
8536 else
8537 vty_out(vty,
8538 "\nDisplayed %ld routes and %ld total paths\n",
8539 output_cum, total_cum);
8540 }
1ae44dfc
LB
8541 return CMD_SUCCESS;
8542}
d62a17ae 8543static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
9f049418 8544 enum bgp_show_type type, void *output_arg, bool use_json)
fee0f4c6 8545{
d62a17ae 8546 struct bgp_table *table;
9386b588 8547 unsigned long json_header_depth = 0;
fee0f4c6 8548
d62a17ae 8549 if (bgp == NULL) {
8550 bgp = bgp_get_default();
8551 }
fee0f4c6 8552
d62a17ae 8553 if (bgp == NULL) {
8554 if (!use_json)
8555 vty_out(vty, "No BGP process is configured\n");
16307668
RW
8556 else
8557 vty_out(vty, "{}\n");
d62a17ae 8558 return CMD_WARNING;
8559 }
4dd6177e 8560
1ae44dfc 8561 table = bgp->rib[afi][safi];
d62a17ae 8562 /* use MPLS and ENCAP specific shows until they are merged */
8563 if (safi == SAFI_MPLS_VPN) {
1ae44dfc
LB
8564 return bgp_show_table_rd(vty, bgp, safi, table, NULL, type,
8565 output_arg, use_json);
d62a17ae 8566 }
dba3c1d3
PG
8567
8568 if (safi == SAFI_FLOWSPEC && type == bgp_show_type_detail) {
8569 return bgp_show_table_flowspec(vty, bgp, afi, table, type,
8570 output_arg, use_json,
8571 1, NULL, NULL);
8572 }
d62a17ae 8573 /* labeled-unicast routes live in the unicast table */
8574 else if (safi == SAFI_LABELED_UNICAST)
8575 safi = SAFI_UNICAST;
fee0f4c6 8576
1ae44dfc 8577 return bgp_show_table(vty, bgp, safi, table, type, output_arg, use_json,
9386b588 8578 NULL, 1, NULL, NULL, &json_header_depth);
fee0f4c6 8579}
8580
d62a17ae 8581static void bgp_show_all_instances_routes_vty(struct vty *vty, afi_t afi,
9f049418 8582 safi_t safi, bool use_json)
f186de26 8583{
d62a17ae 8584 struct listnode *node, *nnode;
8585 struct bgp *bgp;
8586 int is_first = 1;
9f049418 8587 bool route_output = false;
f186de26 8588
d62a17ae 8589 if (use_json)
8590 vty_out(vty, "{\n");
9f689658 8591
d62a17ae 8592 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
9f049418 8593 route_output = true;
d62a17ae 8594 if (use_json) {
8595 if (!is_first)
8596 vty_out(vty, ",\n");
8597 else
8598 is_first = 0;
8599
8600 vty_out(vty, "\"%s\":",
8601 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
8602 ? "Default"
8603 : bgp->name);
8604 } else {
8605 vty_out(vty, "\nInstance %s:\n",
8606 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
8607 ? "Default"
8608 : bgp->name);
8609 }
8610 bgp_show(vty, bgp, afi, safi, bgp_show_type_normal, NULL,
8611 use_json);
8612 }
9f689658 8613
d62a17ae 8614 if (use_json)
8615 vty_out(vty, "}\n");
9f049418
DS
8616 else if (!route_output)
8617 vty_out(vty, "%% BGP instance not found\n");
f186de26 8618}
8619
718e3744 8620/* Header of detailed BGP route information */
d62a17ae 8621void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
8622 struct bgp_node *rn, struct prefix_rd *prd,
8623 afi_t afi, safi_t safi, json_object *json)
8624{
40381db7 8625 struct bgp_path_info *pi;
d62a17ae 8626 struct prefix *p;
8627 struct peer *peer;
8628 struct listnode *node, *nnode;
06b9f471 8629 char buf1[RD_ADDRSTRLEN];
d62a17ae 8630 char buf2[INET6_ADDRSTRLEN];
d62a17ae 8631 char buf3[EVPN_ROUTE_STRLEN];
0291c246 8632 char prefix_str[BUFSIZ];
d62a17ae 8633 int count = 0;
8634 int best = 0;
8635 int suppress = 0;
c5f1e1b2
C
8636 int accept_own = 0;
8637 int route_filter_translated_v4 = 0;
8638 int route_filter_v4 = 0;
8639 int route_filter_translated_v6 = 0;
8640 int route_filter_v6 = 0;
8641 int llgr_stale = 0;
8642 int no_llgr = 0;
8643 int accept_own_nexthop = 0;
8644 int blackhole = 0;
d62a17ae 8645 int no_export = 0;
8646 int no_advertise = 0;
8647 int local_as = 0;
c5f1e1b2 8648 int no_peer = 0;
d62a17ae 8649 int first = 1;
8650 int has_valid_label = 0;
8651 mpls_label_t label = 0;
8652 json_object *json_adv_to = NULL;
9bedbb1e 8653
d62a17ae 8654 p = &rn->p;
8655 has_valid_label = bgp_is_valid_label(&rn->local_label);
8656
8657 if (has_valid_label)
8658 label = label_pton(&rn->local_label);
8659
8660 if (json) {
8661 if (has_valid_label)
8662 json_object_int_add(json, "localLabel", label);
8663
60466a63
QY
8664 json_object_string_add(
8665 json, "prefix",
8666 prefix2str(p, prefix_str, sizeof(prefix_str)));
d62a17ae 8667 } else {
d62a17ae 8668 if (safi == SAFI_EVPN)
8669 vty_out(vty, "BGP routing table entry for %s%s%s\n",
06b9f471 8670 prd ? prefix_rd2str(prd, buf1, sizeof(buf1))
d62a17ae 8671 : "",
8672 prd ? ":" : "",
8673 bgp_evpn_route2str((struct prefix_evpn *)p,
8674 buf3, sizeof(buf3)));
8675 else
8676 vty_out(vty, "BGP routing table entry for %s%s%s/%d\n",
8677 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)
8678 ? prefix_rd2str(prd, buf1,
06b9f471 8679 sizeof(buf1))
d62a17ae 8680 : ""),
8681 safi == SAFI_MPLS_VPN ? ":" : "",
8682 inet_ntop(p->family, &p->u.prefix, buf2,
8683 INET6_ADDRSTRLEN),
8684 p->prefixlen);
cd1964ff 8685
d62a17ae 8686 if (has_valid_label)
8687 vty_out(vty, "Local label: %d\n", label);
d62a17ae 8688 if (bgp_labeled_safi(safi) && safi != SAFI_EVPN)
d62a17ae 8689 vty_out(vty, "not allocated\n");
8690 }
718e3744 8691
40381db7 8692 for (pi = rn->info; pi; pi = pi->next) {
d62a17ae 8693 count++;
40381db7 8694 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
d62a17ae 8695 best = count;
40381db7 8696 if (pi->extra && pi->extra->suppress)
d62a17ae 8697 suppress = 1;
cee9c031 8698
40381db7 8699 if (pi->attr->community == NULL)
cee9c031
QY
8700 continue;
8701
8702 no_advertise += community_include(
40381db7
DS
8703 pi->attr->community, COMMUNITY_NO_ADVERTISE);
8704 no_export += community_include(pi->attr->community,
cee9c031 8705 COMMUNITY_NO_EXPORT);
40381db7 8706 local_as += community_include(pi->attr->community,
cee9c031 8707 COMMUNITY_LOCAL_AS);
40381db7 8708 accept_own += community_include(pi->attr->community,
cee9c031
QY
8709 COMMUNITY_ACCEPT_OWN);
8710 route_filter_translated_v4 += community_include(
40381db7 8711 pi->attr->community,
cee9c031
QY
8712 COMMUNITY_ROUTE_FILTER_TRANSLATED_v4);
8713 route_filter_translated_v6 += community_include(
40381db7 8714 pi->attr->community,
cee9c031
QY
8715 COMMUNITY_ROUTE_FILTER_TRANSLATED_v6);
8716 route_filter_v4 += community_include(
40381db7 8717 pi->attr->community, COMMUNITY_ROUTE_FILTER_v4);
cee9c031 8718 route_filter_v6 += community_include(
40381db7
DS
8719 pi->attr->community, COMMUNITY_ROUTE_FILTER_v6);
8720 llgr_stale += community_include(pi->attr->community,
cee9c031 8721 COMMUNITY_LLGR_STALE);
40381db7 8722 no_llgr += community_include(pi->attr->community,
cee9c031
QY
8723 COMMUNITY_NO_LLGR);
8724 accept_own_nexthop +=
40381db7 8725 community_include(pi->attr->community,
cee9c031 8726 COMMUNITY_ACCEPT_OWN_NEXTHOP);
40381db7 8727 blackhole += community_include(pi->attr->community,
cee9c031 8728 COMMUNITY_BLACKHOLE);
40381db7 8729 no_peer += community_include(pi->attr->community,
cee9c031 8730 COMMUNITY_NO_PEER);
d62a17ae 8731 }
718e3744 8732 }
718e3744 8733
d62a17ae 8734 if (!json) {
8735 vty_out(vty, "Paths: (%d available", count);
8736 if (best) {
8737 vty_out(vty, ", best #%d", best);
8738 if (safi == SAFI_UNICAST)
8739 vty_out(vty, ", table %s",
8740 (bgp->inst_type
8741 == BGP_INSTANCE_TYPE_DEFAULT)
8742 ? "Default-IP-Routing-Table"
8743 : bgp->name);
8744 } else
8745 vty_out(vty, ", no best path");
8746
c5f1e1b2
C
8747 if (accept_own)
8748 vty_out(vty,
8749 ", accept own local route exported and imported in different VRF");
8750 else if (route_filter_translated_v4)
8751 vty_out(vty,
8752 ", mark translated RTs for VPNv4 route filtering");
8753 else if (route_filter_v4)
8754 vty_out(vty,
8755 ", attach RT as-is for VPNv4 route filtering");
8756 else if (route_filter_translated_v6)
8757 vty_out(vty,
8758 ", mark translated RTs for VPNv6 route filtering");
8759 else if (route_filter_v6)
8760 vty_out(vty,
8761 ", attach RT as-is for VPNv6 route filtering");
8762 else if (llgr_stale)
8763 vty_out(vty,
8764 ", mark routes to be retained for a longer time. Requeres support for Long-lived BGP Graceful Restart");
8765 else if (no_llgr)
8766 vty_out(vty,
8767 ", mark routes to not be treated according to Long-lived BGP Graceful Restart operations");
8768 else if (accept_own_nexthop)
8769 vty_out(vty,
8770 ", accept local nexthop");
8771 else if (blackhole)
8772 vty_out(vty, ", inform peer to blackhole prefix");
d62a17ae 8773 else if (no_export)
8774 vty_out(vty, ", not advertised to EBGP peer");
c5f1e1b2
C
8775 else if (no_advertise)
8776 vty_out(vty, ", not advertised to any peer");
d62a17ae 8777 else if (local_as)
8778 vty_out(vty, ", not advertised outside local AS");
c5f1e1b2
C
8779 else if (no_peer)
8780 vty_out(vty,
8781 ", inform EBGP peer not to advertise to their EBGP peers");
d62a17ae 8782
8783 if (suppress)
8784 vty_out(vty,
8785 ", Advertisements suppressed by an aggregate.");
8786 vty_out(vty, ")\n");
8787 }
718e3744 8788
d62a17ae 8789 /* If we are not using addpath then we can display Advertised to and
8790 * that will
8791 * show what peers we advertised the bestpath to. If we are using
8792 * addpath
8793 * though then we must display Advertised to on a path-by-path basis. */
8794 if (!bgp->addpath_tx_used[afi][safi]) {
8795 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8796 if (bgp_adj_out_lookup(peer, rn, 0)) {
8797 if (json && !json_adv_to)
8798 json_adv_to = json_object_new_object();
8799
8800 route_vty_out_advertised_to(
8801 vty, peer, &first,
8802 " Advertised to non peer-group peers:\n ",
8803 json_adv_to);
8804 }
8805 }
8806
8807 if (json) {
8808 if (json_adv_to) {
8809 json_object_object_add(json, "advertisedTo",
8810 json_adv_to);
8811 }
8812 } else {
8813 if (first)
8814 vty_out(vty, " Not advertised to any peer");
8815 vty_out(vty, "\n");
8816 }
8817 }
718e3744 8818}
8819
8820/* Display specified route of BGP table. */
d62a17ae 8821static int bgp_show_route_in_table(struct vty *vty, struct bgp *bgp,
8822 struct bgp_table *rib, const char *ip_str,
8823 afi_t afi, safi_t safi,
8824 struct prefix_rd *prd, int prefix_check,
9f049418 8825 enum bgp_path_type pathtype, bool use_json)
d62a17ae 8826{
8827 int ret;
8828 int header;
8829 int display = 0;
8830 struct prefix match;
8831 struct bgp_node *rn;
8832 struct bgp_node *rm;
40381db7 8833 struct bgp_path_info *pi;
d62a17ae 8834 struct bgp_table *table;
8835 json_object *json = NULL;
8836 json_object *json_paths = NULL;
8837
8838 /* Check IP address argument. */
8839 ret = str2prefix(ip_str, &match);
8840 if (!ret) {
8841 vty_out(vty, "address is malformed\n");
8842 return CMD_WARNING;
8843 }
718e3744 8844
d62a17ae 8845 match.family = afi2family(afi);
b05a1c8b 8846
d62a17ae 8847 if (use_json) {
8848 json = json_object_new_object();
8849 json_paths = json_object_new_array();
8850 }
718e3744 8851
d62a17ae 8852 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
8853 for (rn = bgp_table_top(rib); rn; rn = bgp_route_next(rn)) {
8854 if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
8855 continue;
8856
ea47320b
DL
8857 if ((table = rn->info) == NULL)
8858 continue;
d62a17ae 8859
ea47320b
DL
8860 header = 1;
8861
8862 if ((rm = bgp_node_match(table, &match)) == NULL)
8863 continue;
d62a17ae 8864
ea47320b
DL
8865 if (prefix_check
8866 && rm->p.prefixlen != match.prefixlen) {
8867 bgp_unlock_node(rm);
8868 continue;
8869 }
d62a17ae 8870
40381db7 8871 for (pi = rm->info; pi; pi = pi->next) {
ea47320b 8872 if (header) {
60466a63
QY
8873 route_vty_out_detail_header(
8874 vty, bgp, rm,
8875 (struct prefix_rd *)&rn->p,
ea47320b
DL
8876 AFI_IP, safi, json);
8877 header = 0;
d62a17ae 8878 }
ea47320b
DL
8879 display++;
8880
360660c6
DS
8881 if (pathtype == BGP_PATH_SHOW_ALL
8882 || (pathtype == BGP_PATH_SHOW_BESTPATH
40381db7 8883 && CHECK_FLAG(pi->flags,
1defdda8 8884 BGP_PATH_SELECTED))
360660c6 8885 || (pathtype == BGP_PATH_SHOW_MULTIPATH
40381db7 8886 && (CHECK_FLAG(pi->flags,
1defdda8 8887 BGP_PATH_MULTIPATH)
40381db7 8888 || CHECK_FLAG(pi->flags,
1defdda8 8889 BGP_PATH_SELECTED))))
ea47320b 8890 route_vty_out_detail(vty, bgp, &rm->p,
40381db7 8891 pi, AFI_IP, safi,
ea47320b 8892 json_paths);
d62a17ae 8893 }
ea47320b
DL
8894
8895 bgp_unlock_node(rm);
d62a17ae 8896 }
98a9dbc7 8897 } else if (safi == SAFI_FLOWSPEC) {
63a0b7a9
PG
8898 display = bgp_flowspec_display_match_per_ip(afi, rib,
8899 &match, prefix_check,
8900 vty,
8901 use_json,
8902 json_paths);
d62a17ae 8903 } else {
8904 header = 1;
8905
8906 if ((rn = bgp_node_match(rib, &match)) != NULL) {
8907 if (!prefix_check
8908 || rn->p.prefixlen == match.prefixlen) {
40381db7 8909 for (pi = rn->info; pi; pi = pi->next) {
d62a17ae 8910 if (header) {
8911 route_vty_out_detail_header(
8912 vty, bgp, rn, NULL, afi,
8913 safi, json);
8914 header = 0;
8915 }
8916 display++;
8917
360660c6
DS
8918 if (pathtype == BGP_PATH_SHOW_ALL
8919 || (pathtype
8920 == BGP_PATH_SHOW_BESTPATH
d62a17ae 8921 && CHECK_FLAG(
40381db7 8922 pi->flags,
1defdda8 8923 BGP_PATH_SELECTED))
360660c6
DS
8924 || (pathtype
8925 == BGP_PATH_SHOW_MULTIPATH
d62a17ae 8926 && (CHECK_FLAG(
40381db7 8927 pi->flags,
1defdda8 8928 BGP_PATH_MULTIPATH)
d62a17ae 8929 || CHECK_FLAG(
40381db7 8930 pi->flags,
1defdda8 8931 BGP_PATH_SELECTED))))
d62a17ae 8932 route_vty_out_detail(
40381db7 8933 vty, bgp, &rn->p, pi,
d62a17ae 8934 afi, safi, json_paths);
8935 }
8936 }
8937
8938 bgp_unlock_node(rn);
8939 }
8940 }
e5eee9af 8941
d62a17ae 8942 if (use_json) {
8943 if (display)
8944 json_object_object_add(json, "paths", json_paths);
8945
996c9314
LB
8946 vty_out(vty, "%s\n", json_object_to_json_string_ext(
8947 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 8948 json_object_free(json);
8949 } else {
8950 if (!display) {
8951 vty_out(vty, "%% Network not in table\n");
8952 return CMD_WARNING;
8953 }
8954 }
b05a1c8b 8955
d62a17ae 8956 return CMD_SUCCESS;
718e3744 8957}
8958
fee0f4c6 8959/* Display specified route of Main RIB */
d62a17ae 8960static int bgp_show_route(struct vty *vty, struct bgp *bgp, const char *ip_str,
8961 afi_t afi, safi_t safi, struct prefix_rd *prd,
8962 int prefix_check, enum bgp_path_type pathtype,
9f049418 8963 bool use_json)
d62a17ae 8964{
9b86009a 8965 if (!bgp) {
d62a17ae 8966 bgp = bgp_get_default();
9b86009a
RW
8967 if (!bgp) {
8968 if (!use_json)
8969 vty_out(vty, "No BGP process is configured\n");
16307668
RW
8970 else
8971 vty_out(vty, "{}\n");
9b86009a
RW
8972 return CMD_WARNING;
8973 }
8974 }
d62a17ae 8975
8976 /* labeled-unicast routes live in the unicast table */
8977 if (safi == SAFI_LABELED_UNICAST)
8978 safi = SAFI_UNICAST;
8979
8980 return bgp_show_route_in_table(vty, bgp, bgp->rib[afi][safi], ip_str,
8981 afi, safi, prd, prefix_check, pathtype,
8982 use_json);
8983}
8984
8985static int bgp_show_lcommunity(struct vty *vty, struct bgp *bgp, int argc,
8986 struct cmd_token **argv, afi_t afi, safi_t safi,
088f1098 8987 bool uj)
d62a17ae 8988{
8989 struct lcommunity *lcom;
8990 struct buffer *b;
8991 int i;
8992 char *str;
8993 int first = 0;
8994
8995 b = buffer_new(1024);
8996 for (i = 0; i < argc; i++) {
8997 if (first)
8998 buffer_putc(b, ' ');
8999 else {
9000 if (strmatch(argv[i]->text, "AA:BB:CC")) {
9001 first = 1;
9002 buffer_putstr(b, argv[i]->arg);
9003 }
9004 }
9005 }
9006 buffer_putc(b, '\0');
57d187bc 9007
d62a17ae 9008 str = buffer_getstr(b);
9009 buffer_free(b);
57d187bc 9010
d62a17ae 9011 lcom = lcommunity_str2com(str);
9012 XFREE(MTYPE_TMP, str);
9013 if (!lcom) {
9014 vty_out(vty, "%% Large-community malformed\n");
9015 return CMD_WARNING;
9016 }
57d187bc 9017
d62a17ae 9018 return bgp_show(vty, bgp, afi, safi, bgp_show_type_lcommunity, lcom,
9019 uj);
57d187bc
JS
9020}
9021
d62a17ae 9022static int bgp_show_lcommunity_list(struct vty *vty, struct bgp *bgp,
9023 const char *lcom, afi_t afi, safi_t safi,
088f1098 9024 bool uj)
57d187bc 9025{
d62a17ae 9026 struct community_list *list;
57d187bc 9027
d62a17ae 9028 list = community_list_lookup(bgp_clist, lcom,
9029 LARGE_COMMUNITY_LIST_MASTER);
9030 if (list == NULL) {
9031 vty_out(vty, "%% %s is not a valid large-community-list name\n",
9032 lcom);
9033 return CMD_WARNING;
9034 }
57d187bc 9035
d62a17ae 9036 return bgp_show(vty, bgp, afi, safi, bgp_show_type_lcommunity_list,
9037 list, uj);
fee0f4c6 9038}
9039
52951b63
DS
9040DEFUN (show_ip_bgp_large_community_list,
9041 show_ip_bgp_large_community_list_cmd,
4dd6177e 9042 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] large-community-list <(1-500)|WORD> [json]",
52951b63
DS
9043 SHOW_STR
9044 IP_STR
9045 BGP_STR
9046 BGP_INSTANCE_HELP_STR
9bedbb1e 9047 BGP_AFI_HELP_STR
4dd6177e 9048 BGP_SAFI_WITH_LABEL_HELP_STR
52951b63
DS
9049 "Display routes matching the large-community-list\n"
9050 "large-community-list number\n"
9051 "large-community-list name\n"
9052 JSON_STR)
9053{
d62a17ae 9054 char *vrf = NULL;
9055 afi_t afi = AFI_IP6;
9056 safi_t safi = SAFI_UNICAST;
9057 int idx = 0;
9058
9059 if (argv_find(argv, argc, "ip", &idx))
9060 afi = AFI_IP;
9061 if (argv_find(argv, argc, "view", &idx)
9062 || argv_find(argv, argc, "vrf", &idx))
9063 vrf = argv[++idx]->arg;
9064 if (argv_find(argv, argc, "ipv4", &idx)
9065 || argv_find(argv, argc, "ipv6", &idx)) {
9066 afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP;
9067 if (argv_find(argv, argc, "unicast", &idx)
9068 || argv_find(argv, argc, "multicast", &idx))
9069 safi = bgp_vty_safi_from_str(argv[idx]->text);
9070 }
9071
9f049418 9072 bool uj = use_json(argc, argv);
d62a17ae 9073
9074 struct bgp *bgp = bgp_lookup_by_name(vrf);
9075 if (bgp == NULL) {
9076 vty_out(vty, "Can't find BGP instance %s\n", vrf);
9077 return CMD_WARNING;
9078 }
9079
9080 argv_find(argv, argc, "large-community-list", &idx);
9081 return bgp_show_lcommunity_list(vty, bgp, argv[idx + 1]->arg, afi, safi,
9082 uj);
52951b63
DS
9083}
9084DEFUN (show_ip_bgp_large_community,
9085 show_ip_bgp_large_community_cmd,
4dd6177e 9086 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] large-community [AA:BB:CC] [json]",
52951b63
DS
9087 SHOW_STR
9088 IP_STR
9089 BGP_STR
9090 BGP_INSTANCE_HELP_STR
9bedbb1e 9091 BGP_AFI_HELP_STR
4dd6177e 9092 BGP_SAFI_WITH_LABEL_HELP_STR
52951b63
DS
9093 "Display routes matching the large-communities\n"
9094 "List of large-community numbers\n"
9095 JSON_STR)
9096{
d62a17ae 9097 char *vrf = NULL;
9098 afi_t afi = AFI_IP6;
9099 safi_t safi = SAFI_UNICAST;
9100 int idx = 0;
9101
9102 if (argv_find(argv, argc, "ip", &idx))
9103 afi = AFI_IP;
9104 if (argv_find(argv, argc, "view", &idx)
9105 || argv_find(argv, argc, "vrf", &idx))
9106 vrf = argv[++idx]->arg;
9107 if (argv_find(argv, argc, "ipv4", &idx)
9108 || argv_find(argv, argc, "ipv6", &idx)) {
9109 afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP;
9110 if (argv_find(argv, argc, "unicast", &idx)
9111 || argv_find(argv, argc, "multicast", &idx))
9112 safi = bgp_vty_safi_from_str(argv[idx]->text);
9113 }
9114
9f049418 9115 bool uj = use_json(argc, argv);
d62a17ae 9116
9117 struct bgp *bgp = bgp_lookup_by_name(vrf);
9118 if (bgp == NULL) {
9119 vty_out(vty, "Can't find BGP instance %s\n", vrf);
9120 return CMD_WARNING;
9121 }
9122
9123 if (argv_find(argv, argc, "AA:BB:CC", &idx))
9124 return bgp_show_lcommunity(vty, bgp, argc, argv, afi, safi, uj);
9125 else
9126 return bgp_show(vty, bgp, afi, safi,
9127 bgp_show_type_lcommunity_all, NULL, uj);
52951b63
DS
9128}
9129
d62a17ae 9130static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
9131 safi_t safi);
e01ca200 9132
7b2ff250
DW
9133
9134/* BGP route print out function without JSON */
af462945
DS
9135DEFUN (show_ip_bgp,
9136 show_ip_bgp_cmd,
4dd6177e 9137 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]]\
7b2ff250
DW
9138 <dampening <parameters>\
9139 |route-map WORD\
9140 |prefix-list WORD\
9141 |filter-list WORD\
9142 |statistics\
7b2ff250
DW
9143 |community-list <(1-500)|WORD> [exact-match]\
9144 |A.B.C.D/M longer-prefixes\
9145 |X:X::X:X/M longer-prefixes\
9146 >",
718e3744 9147 SHOW_STR
9148 IP_STR
9149 BGP_STR
a636c635 9150 BGP_INSTANCE_HELP_STR
4f280b15 9151 BGP_AFI_HELP_STR
4dd6177e 9152 BGP_SAFI_WITH_LABEL_HELP_STR
a636c635 9153 "Display detailed information about dampening\n"
af462945 9154 "Display detail of configured dampening parameters\n"
a636c635
DW
9155 "Display routes matching the route-map\n"
9156 "A route-map to match on\n"
9157 "Display routes conforming to the prefix-list\n"
8c3deaae 9158 "Prefix-list name\n"
a636c635
DW
9159 "Display routes conforming to the filter-list\n"
9160 "Regular expression access list name\n"
e01ca200 9161 "BGP RIB advertisement statistics\n"
a636c635
DW
9162 "Display routes matching the community-list\n"
9163 "community-list number\n"
9164 "community-list name\n"
9165 "Exact match of the communities\n"
0c7b1b01 9166 "IPv4 prefix\n"
8c3deaae 9167 "Display route and more specific routes\n"
0c7b1b01 9168 "IPv6 prefix\n"
7b2ff250 9169 "Display route and more specific routes\n")
718e3744 9170{
d62a17ae 9171 afi_t afi = AFI_IP6;
9172 safi_t safi = SAFI_UNICAST;
9173 int exact_match = 0;
d62a17ae 9174 struct bgp *bgp = NULL;
9175 int idx = 0;
9176
9177 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 9178 &bgp, false);
d62a17ae 9179 if (!idx)
9180 return CMD_WARNING;
9181
d62a17ae 9182 if (argv_find(argv, argc, "dampening", &idx)) {
7b2ff250 9183 if (argv_find(argv, argc, "parameters", &idx))
d62a17ae 9184 return bgp_show_dampening_parameters(vty, afi, safi);
9185 }
c016b6c7 9186
d62a17ae 9187 if (argv_find(argv, argc, "prefix-list", &idx))
9188 return bgp_show_prefix_list(vty, bgp, argv[idx + 1]->arg, afi,
9189 safi, bgp_show_type_prefix_list);
9190
9191 if (argv_find(argv, argc, "filter-list", &idx))
9192 return bgp_show_filter_list(vty, bgp, argv[idx + 1]->arg, afi,
9193 safi, bgp_show_type_filter_list);
9194
9195 if (argv_find(argv, argc, "statistics", &idx))
9196 return bgp_table_stats(vty, bgp, afi, safi);
9197
9198 if (argv_find(argv, argc, "route-map", &idx))
9199 return bgp_show_route_map(vty, bgp, argv[idx + 1]->arg, afi,
9200 safi, bgp_show_type_route_map);
9201
d62a17ae 9202 if (argv_find(argv, argc, "community-list", &idx)) {
9203 const char *clist_number_or_name = argv[++idx]->arg;
9204 if (++idx < argc && strmatch(argv[idx]->text, "exact-match"))
9205 exact_match = 1;
9206 return bgp_show_community_list(vty, bgp, clist_number_or_name,
9207 exact_match, afi, safi);
9208 }
9209 /* prefix-longer */
9210 if (argv_find(argv, argc, "A.B.C.D/M", &idx)
9211 || argv_find(argv, argc, "X:X::X:X/M", &idx))
9212 return bgp_show_prefix_longer(vty, bgp, argv[idx]->arg, afi,
9213 safi,
9214 bgp_show_type_prefix_longer);
9215
7b2ff250
DW
9216 return CMD_WARNING;
9217}
9218
9219/* BGP route print out function with JSON */
9220DEFUN (show_ip_bgp_json,
9221 show_ip_bgp_json_cmd,
9222 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]]\
cf4898bc
QY
9223 [cidr-only\
9224 |dampening <flap-statistics|dampened-paths>\
9225 |community [AA:NN|local-AS|no-advertise|no-export\
9226 |graceful-shutdown|no-peer|blackhole|llgr-stale|no-llgr\
9227 |accept-own|accept-own-nexthop|route-filter-v6\
9228 |route-filter-v4|route-filter-translated-v6\
9229 |route-filter-translated-v4] [exact-match]\
9230 ] [json]",
7b2ff250
DW
9231 SHOW_STR
9232 IP_STR
9233 BGP_STR
9234 BGP_INSTANCE_HELP_STR
9235 BGP_AFI_HELP_STR
9236 BGP_SAFI_WITH_LABEL_HELP_STR
9237 "Display only routes with non-natural netmasks\n"
9238 "Display detailed information about dampening\n"
9239 "Display flap statistics of routes\n"
9240 "Display paths suppressed due to dampening\n"
9241 "Display routes matching the communities\n"
d0086e8e
AD
9242 COMMUNITY_AANN_STR
9243 "Do not send outside local AS (well-known community)\n"
9244 "Do not advertise to any peer (well-known community)\n"
9245 "Do not export to next AS (well-known community)\n"
9246 "Graceful shutdown (well-known community)\n"
cf4898bc
QY
9247 "Do not export to any peer (well-known community)\n"
9248 "Inform EBGP peers to blackhole traffic to prefix (well-known community)\n"
9249 "Staled Long-lived Graceful Restart VPN route (well-known community)\n"
9250 "Removed because Long-lived Graceful Restart was not enabled for VPN route (well-known community)\n"
9251 "Should accept local VPN route if exported and imported into different VRF (well-known community)\n"
9252 "Should accept VPN route with local nexthop (well-known community)\n"
9253 "RT VPNv6 route filtering (well-known community)\n"
9254 "RT VPNv4 route filtering (well-known community)\n"
9255 "RT translated VPNv6 route filtering (well-known community)\n"
9256 "RT translated VPNv4 route filtering (well-known community)\n"
d0086e8e 9257 "Exact match of the communities\n"
7b2ff250
DW
9258 JSON_STR)
9259{
9260 afi_t afi = AFI_IP6;
9261 safi_t safi = SAFI_UNICAST;
9262 enum bgp_show_type sh_type = bgp_show_type_normal;
9263 struct bgp *bgp = NULL;
9264 int idx = 0;
d0086e8e 9265 int exact_match = 0;
9f049418
DS
9266 bool uj = use_json(argc, argv);
9267
9268 if (uj)
9269 argc--;
7b2ff250
DW
9270
9271 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 9272 &bgp, uj);
7b2ff250
DW
9273 if (!idx)
9274 return CMD_WARNING;
9275
7b2ff250
DW
9276 if (argv_find(argv, argc, "cidr-only", &idx))
9277 return bgp_show(vty, bgp, afi, safi, bgp_show_type_cidr_only,
9278 NULL, uj);
9279
9280 if (argv_find(argv, argc, "dampening", &idx)) {
9281 if (argv_find(argv, argc, "dampened-paths", &idx))
9282 return bgp_show(vty, bgp, afi, safi,
9283 bgp_show_type_dampend_paths, NULL, uj);
9284 else if (argv_find(argv, argc, "flap-statistics", &idx))
9285 return bgp_show(vty, bgp, afi, safi,
9286 bgp_show_type_flap_statistics, NULL,
9287 uj);
9288 }
9289
9290 if (argv_find(argv, argc, "community", &idx)) {
cf4898bc
QY
9291 char *maybecomm = idx + 1 < argc ? argv[idx + 1]->text : NULL;
9292 char *community = NULL;
d0086e8e 9293
cf4898bc
QY
9294 if (maybecomm && !strmatch(maybecomm, "json")
9295 && !strmatch(maybecomm, "exact-match"))
9296 community = maybecomm;
9297
9298 if (argv_find(argv, argc, "exact-match", &idx))
9299 exact_match = 1;
d0086e8e 9300
cf4898bc
QY
9301 if (community)
9302 return bgp_show_community(vty, bgp, community,
9303 exact_match, afi, safi, uj);
9304 else
d0086e8e 9305 return (bgp_show(vty, bgp, afi, safi,
cf4898bc
QY
9306 bgp_show_type_community_all, NULL,
9307 uj));
7b2ff250 9308 }
d0086e8e 9309
1ae44dfc 9310 return bgp_show(vty, bgp, afi, safi, sh_type, NULL, uj);
a636c635 9311}
47fc97cc 9312
718e3744 9313DEFUN (show_ip_bgp_route,
9314 show_ip_bgp_route_cmd,
4dd6177e 9315 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]]"
ae19d7dd 9316 "<A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [<bestpath|multipath>] [json]",
718e3744 9317 SHOW_STR
9318 IP_STR
9319 BGP_STR
a636c635 9320 BGP_INSTANCE_HELP_STR
4f280b15 9321 BGP_AFI_HELP_STR
4dd6177e 9322 BGP_SAFI_WITH_LABEL_HELP_STR
b05a1c8b 9323 "Network in the BGP routing table to display\n"
0c7b1b01 9324 "IPv4 prefix\n"
8c3deaae 9325 "Network in the BGP routing table to display\n"
0c7b1b01 9326 "IPv6 prefix\n"
4092b06c 9327 "Display only the bestpath\n"
b05a1c8b 9328 "Display only multipaths\n"
9973d184 9329 JSON_STR)
4092b06c 9330{
d62a17ae 9331 int prefix_check = 0;
ae19d7dd 9332
d62a17ae 9333 afi_t afi = AFI_IP6;
9334 safi_t safi = SAFI_UNICAST;
9335 char *prefix = NULL;
9336 struct bgp *bgp = NULL;
9337 enum bgp_path_type path_type;
9f049418 9338 bool uj = use_json(argc, argv);
b05a1c8b 9339
d62a17ae 9340 int idx = 0;
ae19d7dd 9341
d62a17ae 9342 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 9343 &bgp, uj);
d62a17ae 9344 if (!idx)
9345 return CMD_WARNING;
c41247f5 9346
d62a17ae 9347 if (!bgp) {
9348 vty_out(vty,
9349 "Specified 'all' vrf's but this command currently only works per view/vrf\n");
9350 return CMD_WARNING;
9351 }
a636c635 9352
d62a17ae 9353 /* <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> */
9354 if (argv_find(argv, argc, "A.B.C.D", &idx)
9355 || argv_find(argv, argc, "X:X::X:X", &idx))
9356 prefix_check = 0;
9357 else if (argv_find(argv, argc, "A.B.C.D/M", &idx)
9358 || argv_find(argv, argc, "X:X::X:X/M", &idx))
9359 prefix_check = 1;
9360
9361 if ((argv[idx]->type == IPV6_TKN || argv[idx]->type == IPV6_PREFIX_TKN)
9362 && afi != AFI_IP6) {
9363 vty_out(vty,
9364 "%% Cannot specify IPv6 address or prefix with IPv4 AFI\n");
9365 return CMD_WARNING;
9366 }
9367 if ((argv[idx]->type == IPV4_TKN || argv[idx]->type == IPV4_PREFIX_TKN)
9368 && afi != AFI_IP) {
9369 vty_out(vty,
9370 "%% Cannot specify IPv4 address or prefix with IPv6 AFI\n");
9371 return CMD_WARNING;
9372 }
9373
9374 prefix = argv[idx]->arg;
9375
9376 /* [<bestpath|multipath>] */
9377 if (argv_find(argv, argc, "bestpath", &idx))
360660c6 9378 path_type = BGP_PATH_SHOW_BESTPATH;
d62a17ae 9379 else if (argv_find(argv, argc, "multipath", &idx))
360660c6 9380 path_type = BGP_PATH_SHOW_MULTIPATH;
d62a17ae 9381 else
360660c6 9382 path_type = BGP_PATH_SHOW_ALL;
a636c635 9383
d62a17ae 9384 return bgp_show_route(vty, bgp, prefix, afi, safi, NULL, prefix_check,
9385 path_type, uj);
4092b06c
DS
9386}
9387
8c3deaae
QY
9388DEFUN (show_ip_bgp_regexp,
9389 show_ip_bgp_regexp_cmd,
4dd6177e 9390 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] regexp REGEX...",
8c3deaae
QY
9391 SHOW_STR
9392 IP_STR
9393 BGP_STR
b00b230a 9394 BGP_INSTANCE_HELP_STR
4f280b15 9395 BGP_AFI_HELP_STR
4dd6177e 9396 BGP_SAFI_WITH_LABEL_HELP_STR
8c3deaae
QY
9397 "Display routes matching the AS path regular expression\n"
9398 "A regular-expression to match the BGP AS paths\n")
9399{
d62a17ae 9400 afi_t afi = AFI_IP6;
9401 safi_t safi = SAFI_UNICAST;
9402 struct bgp *bgp = NULL;
8c3deaae 9403
d62a17ae 9404 int idx = 0;
9405 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 9406 &bgp, false);
d62a17ae 9407 if (!idx)
9408 return CMD_WARNING;
8c3deaae 9409
d62a17ae 9410 // get index of regex
9411 argv_find(argv, argc, "regexp", &idx);
9412 idx++;
8c3deaae 9413
d62a17ae 9414 char *regstr = argv_concat(argv, argc, idx);
e889891d 9415 int rc = bgp_show_regexp(vty, bgp, (const char *)regstr, afi, safi,
d62a17ae 9416 bgp_show_type_regexp);
9417 XFREE(MTYPE_TMP, regstr);
9418 return rc;
8c3deaae
QY
9419}
9420
a636c635
DW
9421DEFUN (show_ip_bgp_instance_all,
9422 show_ip_bgp_instance_all_cmd,
4dd6177e 9423 "show [ip] bgp <view|vrf> all ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] [json]",
4092b06c 9424 SHOW_STR
a636c635 9425 IP_STR
4092b06c 9426 BGP_STR
a636c635 9427 BGP_INSTANCE_ALL_HELP_STR
4f280b15 9428 BGP_AFI_HELP_STR
4dd6177e 9429 BGP_SAFI_WITH_LABEL_HELP_STR
9973d184 9430 JSON_STR)
4092b06c 9431{
d62a17ae 9432 afi_t afi = AFI_IP;
9433 safi_t safi = SAFI_UNICAST;
9434 struct bgp *bgp = NULL;
d62a17ae 9435 int idx = 0;
9f049418 9436 bool uj = use_json(argc, argv);
ae19d7dd 9437
d62a17ae 9438 if (uj)
9439 argc--;
e3e29b32 9440
9f049418
DS
9441 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9442 &bgp, uj);
9443 if (!idx)
9444 return CMD_WARNING;
9445
d62a17ae 9446 bgp_show_all_instances_routes_vty(vty, afi, safi, uj);
9447 return CMD_SUCCESS;
e3e29b32
LB
9448}
9449
a4d82a8a
PZ
9450static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
9451 afi_t afi, safi_t safi, enum bgp_show_type type)
718e3744 9452{
d62a17ae 9453 regex_t *regex;
9454 int rc;
e3e29b32 9455
d62a17ae 9456 regex = bgp_regcomp(regstr);
9457 if (!regex) {
9458 vty_out(vty, "Can't compile regexp %s\n", regstr);
9459 return CMD_WARNING;
9460 }
a636c635 9461
e889891d 9462 rc = bgp_show(vty, bgp, afi, safi, type, regex, 0);
d62a17ae 9463 bgp_regex_free(regex);
9464 return rc;
e3e29b32
LB
9465}
9466
d62a17ae 9467static int bgp_show_prefix_list(struct vty *vty, struct bgp *bgp,
9468 const char *prefix_list_str, afi_t afi,
9469 safi_t safi, enum bgp_show_type type)
e3e29b32 9470{
d62a17ae 9471 struct prefix_list *plist;
718e3744 9472
d62a17ae 9473 plist = prefix_list_lookup(afi, prefix_list_str);
9474 if (plist == NULL) {
9475 vty_out(vty, "%% %s is not a valid prefix-list name\n",
9476 prefix_list_str);
9477 return CMD_WARNING;
9478 }
718e3744 9479
d62a17ae 9480 return bgp_show(vty, bgp, afi, safi, type, plist, 0);
4092b06c
DS
9481}
9482
d62a17ae 9483static int bgp_show_filter_list(struct vty *vty, struct bgp *bgp,
9484 const char *filter, afi_t afi, safi_t safi,
9485 enum bgp_show_type type)
4092b06c 9486{
d62a17ae 9487 struct as_list *as_list;
718e3744 9488
d62a17ae 9489 as_list = as_list_lookup(filter);
9490 if (as_list == NULL) {
9491 vty_out(vty, "%% %s is not a valid AS-path access-list name\n",
9492 filter);
9493 return CMD_WARNING;
9494 }
a636c635 9495
d62a17ae 9496 return bgp_show(vty, bgp, afi, safi, type, as_list, 0);
718e3744 9497}
9498
d62a17ae 9499static int bgp_show_route_map(struct vty *vty, struct bgp *bgp,
9500 const char *rmap_str, afi_t afi, safi_t safi,
9501 enum bgp_show_type type)
718e3744 9502{
d62a17ae 9503 struct route_map *rmap;
bb46e94f 9504
d62a17ae 9505 rmap = route_map_lookup_by_name(rmap_str);
9506 if (!rmap) {
9507 vty_out(vty, "%% %s is not a valid route-map name\n", rmap_str);
9508 return CMD_WARNING;
9509 }
9510
9511 return bgp_show(vty, bgp, afi, safi, type, rmap, 0);
9512}
9513
7f323236
DW
9514static int bgp_show_community(struct vty *vty, struct bgp *bgp,
9515 const char *comstr, int exact, afi_t afi,
9f049418 9516 safi_t safi, bool use_json)
d62a17ae 9517{
9518 struct community *com;
d62a17ae 9519 int ret = 0;
9520
7f323236 9521 com = community_str2com(comstr);
d62a17ae 9522 if (!com) {
7f323236 9523 vty_out(vty, "%% Community malformed: %s\n", comstr);
d62a17ae 9524 return CMD_WARNING;
9525 }
9526
9527 ret = bgp_show(vty, bgp, afi, safi,
9528 (exact ? bgp_show_type_community_exact
9529 : bgp_show_type_community),
d0086e8e 9530 com, use_json);
d62a17ae 9531 community_free(com);
46c3ce83 9532
d62a17ae 9533 return ret;
718e3744 9534}
9535
d62a17ae 9536static int bgp_show_community_list(struct vty *vty, struct bgp *bgp,
9537 const char *com, int exact, afi_t afi,
9538 safi_t safi)
50ef26d4 9539{
d62a17ae 9540 struct community_list *list;
50ef26d4 9541
d62a17ae 9542 list = community_list_lookup(bgp_clist, com, COMMUNITY_LIST_MASTER);
9543 if (list == NULL) {
9544 vty_out(vty, "%% %s is not a valid community-list name\n", com);
9545 return CMD_WARNING;
9546 }
718e3744 9547
d62a17ae 9548 return bgp_show(vty, bgp, afi, safi,
9549 (exact ? bgp_show_type_community_list_exact
9550 : bgp_show_type_community_list),
9551 list, 0);
50ef26d4 9552}
9553
d62a17ae 9554static int bgp_show_prefix_longer(struct vty *vty, struct bgp *bgp,
9555 const char *prefix, afi_t afi, safi_t safi,
9556 enum bgp_show_type type)
718e3744 9557{
d62a17ae 9558 int ret;
9559 struct prefix *p;
47fc97cc 9560
d62a17ae 9561 p = prefix_new();
95cbbd2a 9562
d62a17ae 9563 ret = str2prefix(prefix, p);
9564 if (!ret) {
9565 vty_out(vty, "%% Malformed Prefix\n");
9566 return CMD_WARNING;
9567 }
47e9b292 9568
d62a17ae 9569 ret = bgp_show(vty, bgp, afi, safi, type, p, 0);
9570 prefix_free(p);
9571 return ret;
9572}
9573
9574static struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
9f049418 9575 const char *ip_str, bool use_json)
d62a17ae 9576{
9577 int ret;
9578 struct peer *peer;
9579 union sockunion su;
9580
9581 /* Get peer sockunion. */
9582 ret = str2sockunion(ip_str, &su);
9583 if (ret < 0) {
9584 peer = peer_lookup_by_conf_if(bgp, ip_str);
9585 if (!peer) {
9586 peer = peer_lookup_by_hostname(bgp, ip_str);
9587
9588 if (!peer) {
9589 if (use_json) {
9590 json_object *json_no = NULL;
9591 json_no = json_object_new_object();
9592 json_object_string_add(
9593 json_no,
9594 "malformedAddressOrName",
9595 ip_str);
9596 vty_out(vty, "%s\n",
66f80d74 9597 json_object_to_json_string_ext(
a4d82a8a
PZ
9598 json_no,
9599 JSON_C_TO_STRING_PRETTY));
d62a17ae 9600 json_object_free(json_no);
9601 } else
9602 vty_out(vty,
9603 "%% Malformed address or name: %s\n",
9604 ip_str);
9605 return NULL;
9606 }
9607 }
9608 return peer;
9609 }
718e3744 9610
d62a17ae 9611 /* Peer structure lookup. */
9612 peer = peer_lookup(bgp, &su);
9613 if (!peer) {
9614 if (use_json) {
9615 json_object *json_no = NULL;
9616 json_no = json_object_new_object();
9617 json_object_string_add(json_no, "warning",
9e6e6f46 9618 "No such neighbor in this view/vrf");
d62a17ae 9619 vty_out(vty, "%s\n",
a4d82a8a
PZ
9620 json_object_to_json_string_ext(
9621 json_no, JSON_C_TO_STRING_PRETTY));
d62a17ae 9622 json_object_free(json_no);
9623 } else
9e6e6f46 9624 vty_out(vty, "No such neighbor in this view/vrf\n");
d62a17ae 9625 return NULL;
9626 }
2815e61f 9627
d62a17ae 9628 return peer;
9629}
9630
9631enum bgp_stats {
9632 BGP_STATS_MAXBITLEN = 0,
9633 BGP_STATS_RIB,
9634 BGP_STATS_PREFIXES,
9635 BGP_STATS_TOTPLEN,
9636 BGP_STATS_UNAGGREGATEABLE,
9637 BGP_STATS_MAX_AGGREGATEABLE,
9638 BGP_STATS_AGGREGATES,
9639 BGP_STATS_SPACE,
9640 BGP_STATS_ASPATH_COUNT,
9641 BGP_STATS_ASPATH_MAXHOPS,
9642 BGP_STATS_ASPATH_TOTHOPS,
9643 BGP_STATS_ASPATH_MAXSIZE,
9644 BGP_STATS_ASPATH_TOTSIZE,
9645 BGP_STATS_ASN_HIGHEST,
9646 BGP_STATS_MAX,
a636c635 9647};
2815e61f 9648
d62a17ae 9649static const char *table_stats_strs[] = {
9d303b37
DL
9650 [BGP_STATS_PREFIXES] = "Total Prefixes",
9651 [BGP_STATS_TOTPLEN] = "Average prefix length",
9652 [BGP_STATS_RIB] = "Total Advertisements",
9653 [BGP_STATS_UNAGGREGATEABLE] = "Unaggregateable prefixes",
9654 [BGP_STATS_MAX_AGGREGATEABLE] =
9655 "Maximum aggregateable prefixes",
9656 [BGP_STATS_AGGREGATES] = "BGP Aggregate advertisements",
9657 [BGP_STATS_SPACE] = "Address space advertised",
9658 [BGP_STATS_ASPATH_COUNT] = "Advertisements with paths",
9659 [BGP_STATS_ASPATH_MAXHOPS] = "Longest AS-Path (hops)",
9660 [BGP_STATS_ASPATH_MAXSIZE] = "Largest AS-Path (bytes)",
9661 [BGP_STATS_ASPATH_TOTHOPS] = "Average AS-Path length (hops)",
9662 [BGP_STATS_ASPATH_TOTSIZE] = "Average AS-Path size (bytes)",
9663 [BGP_STATS_ASN_HIGHEST] = "Highest public ASN",
9664 [BGP_STATS_MAX] = NULL,
a636c635 9665};
2815e61f 9666
d62a17ae 9667struct bgp_table_stats {
9668 struct bgp_table *table;
9669 unsigned long long counts[BGP_STATS_MAX];
8d0ab76d 9670 double total_space;
ff7924f6
PJ
9671};
9672
a636c635
DW
9673#if 0
9674#define TALLY_SIGFIG 100000
9675static unsigned long
9676ravg_tally (unsigned long count, unsigned long oldavg, unsigned long newval)
ff7924f6 9677{
a636c635
DW
9678 unsigned long newtot = (count-1) * oldavg + (newval * TALLY_SIGFIG);
9679 unsigned long res = (newtot * TALLY_SIGFIG) / count;
9680 unsigned long ret = newtot / count;
07d0c4ed 9681
a636c635
DW
9682 if ((res % TALLY_SIGFIG) > (TALLY_SIGFIG/2))
9683 return ret + 1;
9684 else
9685 return ret;
9686}
9687#endif
ff7924f6 9688
d62a17ae 9689static int bgp_table_stats_walker(struct thread *t)
2815e61f 9690{
d62a17ae 9691 struct bgp_node *rn;
9692 struct bgp_node *top;
9693 struct bgp_table_stats *ts = THREAD_ARG(t);
9694 unsigned int space = 0;
a636c635 9695
d62a17ae 9696 if (!(top = bgp_table_top(ts->table)))
9697 return 0;
2815e61f 9698
d62a17ae 9699 switch (top->p.family) {
9700 case AF_INET:
9701 space = IPV4_MAX_BITLEN;
9702 break;
9703 case AF_INET6:
9704 space = IPV6_MAX_BITLEN;
9705 break;
9706 }
9707
9708 ts->counts[BGP_STATS_MAXBITLEN] = space;
9709
9710 for (rn = top; rn; rn = bgp_route_next(rn)) {
40381db7 9711 struct bgp_path_info *pi;
d62a17ae 9712 struct bgp_node *prn = bgp_node_parent_nolock(rn);
40381db7 9713 unsigned int pinum = 0;
d62a17ae 9714
9715 if (rn == top)
9716 continue;
9717
9718 if (!rn->info)
9719 continue;
9720
9721 ts->counts[BGP_STATS_PREFIXES]++;
9722 ts->counts[BGP_STATS_TOTPLEN] += rn->p.prefixlen;
ff7924f6 9723
a636c635
DW
9724#if 0
9725 ts->counts[BGP_STATS_AVGPLEN]
9726 = ravg_tally (ts->counts[BGP_STATS_PREFIXES],
9727 ts->counts[BGP_STATS_AVGPLEN],
9728 rn->p.prefixlen);
9729#endif
d62a17ae 9730
9731 /* check if the prefix is included by any other announcements */
9732 while (prn && !prn->info)
9733 prn = bgp_node_parent_nolock(prn);
9734
9735 if (prn == NULL || prn == top) {
9736 ts->counts[BGP_STATS_UNAGGREGATEABLE]++;
9737 /* announced address space */
9738 if (space)
a4d82a8a
PZ
9739 ts->total_space +=
9740 pow(2.0, space - rn->p.prefixlen);
d62a17ae 9741 } else if (prn->info)
9742 ts->counts[BGP_STATS_MAX_AGGREGATEABLE]++;
9743
40381db7
DS
9744 for (pi = rn->info; pi; pi = pi->next) {
9745 pinum++;
d62a17ae 9746 ts->counts[BGP_STATS_RIB]++;
9747
40381db7
DS
9748 if (pi->attr
9749 && (CHECK_FLAG(pi->attr->flag,
d62a17ae 9750 ATTR_FLAG_BIT(
9751 BGP_ATTR_ATOMIC_AGGREGATE))))
9752 ts->counts[BGP_STATS_AGGREGATES]++;
9753
9754 /* as-path stats */
40381db7 9755 if (pi->attr && pi->attr->aspath) {
d62a17ae 9756 unsigned int hops =
40381db7 9757 aspath_count_hops(pi->attr->aspath);
d62a17ae 9758 unsigned int size =
40381db7
DS
9759 aspath_size(pi->attr->aspath);
9760 as_t highest = aspath_highest(pi->attr->aspath);
d62a17ae 9761
9762 ts->counts[BGP_STATS_ASPATH_COUNT]++;
9763
9764 if (hops > ts->counts[BGP_STATS_ASPATH_MAXHOPS])
9765 ts->counts[BGP_STATS_ASPATH_MAXHOPS] =
9766 hops;
9767
9768 if (size > ts->counts[BGP_STATS_ASPATH_MAXSIZE])
9769 ts->counts[BGP_STATS_ASPATH_MAXSIZE] =
9770 size;
9771
9772 ts->counts[BGP_STATS_ASPATH_TOTHOPS] += hops;
9773 ts->counts[BGP_STATS_ASPATH_TOTSIZE] += size;
a636c635 9774#if 0
07d0c4ed 9775 ts->counts[BGP_STATS_ASPATH_AVGHOPS]
a636c635
DW
9776 = ravg_tally (ts->counts[BGP_STATS_ASPATH_COUNT],
9777 ts->counts[BGP_STATS_ASPATH_AVGHOPS],
9778 hops);
9779 ts->counts[BGP_STATS_ASPATH_AVGSIZE]
9780 = ravg_tally (ts->counts[BGP_STATS_ASPATH_COUNT],
9781 ts->counts[BGP_STATS_ASPATH_AVGSIZE],
9782 size);
9783#endif
d62a17ae 9784 if (highest > ts->counts[BGP_STATS_ASN_HIGHEST])
9785 ts->counts[BGP_STATS_ASN_HIGHEST] =
9786 highest;
9787 }
9788 }
9789 }
9790 return 0;
2815e61f 9791}
ff7924f6 9792
d62a17ae 9793static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
9794 safi_t safi)
2815e61f 9795{
d62a17ae 9796 struct bgp_table_stats ts;
9797 unsigned int i;
019386c2 9798
d62a17ae 9799 if (!bgp->rib[afi][safi]) {
9800 vty_out(vty, "%% No RIB exist's for the AFI(%d)/SAFI(%d)\n",
9801 afi, safi);
9802 return CMD_WARNING;
9803 }
019386c2 9804
d62a17ae 9805 vty_out(vty, "BGP %s RIB statistics\n", afi_safi_print(afi, safi));
019386c2 9806
d62a17ae 9807 /* labeled-unicast routes live in the unicast table */
9808 if (safi == SAFI_LABELED_UNICAST)
9809 safi = SAFI_UNICAST;
019386c2 9810
d62a17ae 9811 memset(&ts, 0, sizeof(ts));
9812 ts.table = bgp->rib[afi][safi];
9813 thread_execute(bm->master, bgp_table_stats_walker, &ts, 0);
ff7924f6 9814
d62a17ae 9815 for (i = 0; i < BGP_STATS_MAX; i++) {
9816 if (!table_stats_strs[i])
9817 continue;
9818
9819 switch (i) {
a636c635
DW
9820#if 0
9821 case BGP_STATS_ASPATH_AVGHOPS:
9822 case BGP_STATS_ASPATH_AVGSIZE:
9823 case BGP_STATS_AVGPLEN:
9824 vty_out (vty, "%-30s: ", table_stats_strs[i]);
9825 vty_out (vty, "%12.2f",
9826 (float)ts.counts[i] / (float)TALLY_SIGFIG);
9827 break;
9828#endif
d62a17ae 9829 case BGP_STATS_ASPATH_TOTHOPS:
9830 case BGP_STATS_ASPATH_TOTSIZE:
9831 vty_out(vty, "%-30s: ", table_stats_strs[i]);
9832 vty_out(vty, "%12.2f",
9833 ts.counts[i]
9834 ? (float)ts.counts[i]
9835 / (float)ts.counts
9836 [BGP_STATS_ASPATH_COUNT]
9837 : 0);
9838 break;
9839 case BGP_STATS_TOTPLEN:
9840 vty_out(vty, "%-30s: ", table_stats_strs[i]);
9841 vty_out(vty, "%12.2f",
9842 ts.counts[i]
9843 ? (float)ts.counts[i]
9844 / (float)ts.counts
9845 [BGP_STATS_PREFIXES]
9846 : 0);
9847 break;
9848 case BGP_STATS_SPACE:
9849 vty_out(vty, "%-30s: ", table_stats_strs[i]);
8d0ab76d
DL
9850 vty_out(vty, "%12g\n", ts.total_space);
9851
9852 if (afi == AFI_IP6) {
9853 vty_out(vty, "%30s: ", "/32 equivalent ");
9854 vty_out(vty, "%12g\n",
a4d82a8a 9855 ts.total_space * pow(2.0, -128 + 32));
8d0ab76d
DL
9856 vty_out(vty, "%30s: ", "/48 equivalent ");
9857 vty_out(vty, "%12g\n",
a4d82a8a 9858 ts.total_space * pow(2.0, -128 + 48));
8d0ab76d
DL
9859 } else {
9860 vty_out(vty, "%30s: ", "% announced ");
9861 vty_out(vty, "%12.2f\n",
9862 ts.total_space * 100. * pow(2.0, -32));
9863 vty_out(vty, "%30s: ", "/8 equivalent ");
9864 vty_out(vty, "%12.2f\n",
a4d82a8a 9865 ts.total_space * pow(2.0, -32 + 8));
8d0ab76d
DL
9866 vty_out(vty, "%30s: ", "/24 equivalent ");
9867 vty_out(vty, "%12.2f\n",
a4d82a8a 9868 ts.total_space * pow(2.0, -32 + 24));
8d0ab76d 9869 }
d62a17ae 9870 break;
9871 default:
9872 vty_out(vty, "%-30s: ", table_stats_strs[i]);
9873 vty_out(vty, "%12llu", ts.counts[i]);
9874 }
ff7924f6 9875
d62a17ae 9876 vty_out(vty, "\n");
9877 }
9878 return CMD_SUCCESS;
9879}
9880
9881enum bgp_pcounts {
9882 PCOUNT_ADJ_IN = 0,
9883 PCOUNT_DAMPED,
9884 PCOUNT_REMOVED,
9885 PCOUNT_HISTORY,
9886 PCOUNT_STALE,
9887 PCOUNT_VALID,
9888 PCOUNT_ALL,
9889 PCOUNT_COUNTED,
9890 PCOUNT_PFCNT, /* the figure we display to users */
9891 PCOUNT_MAX,
a636c635 9892};
718e3744 9893
d62a17ae 9894static const char *pcount_strs[] = {
9d303b37
DL
9895 [PCOUNT_ADJ_IN] = "Adj-in",
9896 [PCOUNT_DAMPED] = "Damped",
9897 [PCOUNT_REMOVED] = "Removed",
9898 [PCOUNT_HISTORY] = "History",
9899 [PCOUNT_STALE] = "Stale",
9900 [PCOUNT_VALID] = "Valid",
9901 [PCOUNT_ALL] = "All RIB",
9902 [PCOUNT_COUNTED] = "PfxCt counted",
9903 [PCOUNT_PFCNT] = "Useable",
9904 [PCOUNT_MAX] = NULL,
a636c635 9905};
718e3744 9906
d62a17ae 9907struct peer_pcounts {
9908 unsigned int count[PCOUNT_MAX];
9909 const struct peer *peer;
9910 const struct bgp_table *table;
a636c635 9911};
47fc97cc 9912
d62a17ae 9913static int bgp_peer_count_walker(struct thread *t)
9914{
9915 struct bgp_node *rn;
9916 struct peer_pcounts *pc = THREAD_ARG(t);
9917 const struct peer *peer = pc->peer;
9918
9919 for (rn = bgp_table_top(pc->table); rn; rn = bgp_route_next(rn)) {
9920 struct bgp_adj_in *ain;
40381db7 9921 struct bgp_path_info *pi;
d62a17ae 9922
9923 for (ain = rn->adj_in; ain; ain = ain->next)
9924 if (ain->peer == peer)
9925 pc->count[PCOUNT_ADJ_IN]++;
9926
40381db7
DS
9927 for (pi = rn->info; pi; pi = pi->next) {
9928 if (pi->peer != peer)
d62a17ae 9929 continue;
9930
9931 pc->count[PCOUNT_ALL]++;
9932
40381db7 9933 if (CHECK_FLAG(pi->flags, BGP_PATH_DAMPED))
d62a17ae 9934 pc->count[PCOUNT_DAMPED]++;
40381db7 9935 if (CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
d62a17ae 9936 pc->count[PCOUNT_HISTORY]++;
40381db7 9937 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
d62a17ae 9938 pc->count[PCOUNT_REMOVED]++;
40381db7 9939 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE))
d62a17ae 9940 pc->count[PCOUNT_STALE]++;
40381db7 9941 if (CHECK_FLAG(pi->flags, BGP_PATH_VALID))
d62a17ae 9942 pc->count[PCOUNT_VALID]++;
40381db7 9943 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
d62a17ae 9944 pc->count[PCOUNT_PFCNT]++;
9945
40381db7 9946 if (CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
d62a17ae 9947 pc->count[PCOUNT_COUNTED]++;
40381db7 9948 if (CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
1c50c1c0
QY
9949 flog_err(
9950 EC_LIB_DEVELOPMENT,
9951 "Attempting to count but flags say it is unusable");
d62a17ae 9952 } else {
40381db7 9953 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
1c50c1c0
QY
9954 flog_err(
9955 EC_LIB_DEVELOPMENT,
9956 "Not counted but flags say we should");
d62a17ae 9957 }
9958 }
9959 }
9960 return 0;
718e3744 9961}
9962
d62a17ae 9963static int bgp_peer_counts(struct vty *vty, struct peer *peer, afi_t afi,
9f049418 9964 safi_t safi, bool use_json)
856ca177 9965{
d62a17ae 9966 struct peer_pcounts pcounts = {.peer = peer};
9967 unsigned int i;
9968 json_object *json = NULL;
9969 json_object *json_loop = NULL;
856ca177 9970
d62a17ae 9971 if (use_json) {
9972 json = json_object_new_object();
9973 json_loop = json_object_new_object();
9974 }
718e3744 9975
d62a17ae 9976 if (!peer || !peer->bgp || !peer->afc[afi][safi]
9977 || !peer->bgp->rib[afi][safi]) {
9978 if (use_json) {
9979 json_object_string_add(
9980 json, "warning",
9981 "No such neighbor or address family");
9982 vty_out(vty, "%s\n", json_object_to_json_string(json));
9983 json_object_free(json);
9984 } else
9985 vty_out(vty, "%% No such neighbor or address family\n");
9986
9987 return CMD_WARNING;
9988 }
2a71e9ce 9989
d62a17ae 9990 memset(&pcounts, 0, sizeof(pcounts));
9991 pcounts.peer = peer;
9992 pcounts.table = peer->bgp->rib[afi][safi];
9993
9994 /* in-place call via thread subsystem so as to record execution time
d316210b
PZ
9995 * stats for the thread-walk (i.e. ensure this can't be blamed on
9996 * on just vty_read()).
9997 */
d62a17ae 9998 thread_execute(bm->master, bgp_peer_count_walker, &pcounts, 0);
9999
10000 if (use_json) {
10001 json_object_string_add(json, "prefixCountsFor", peer->host);
10002 json_object_string_add(json, "multiProtocol",
10003 afi_safi_print(afi, safi));
10004 json_object_int_add(json, "pfxCounter",
10005 peer->pcount[afi][safi]);
10006
10007 for (i = 0; i < PCOUNT_MAX; i++)
10008 json_object_int_add(json_loop, pcount_strs[i],
10009 pcounts.count[i]);
10010
10011 json_object_object_add(json, "ribTableWalkCounters", json_loop);
10012
10013 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
10014 json_object_string_add(json, "pfxctDriftFor",
10015 peer->host);
10016 json_object_string_add(
10017 json, "recommended",
10018 "Please report this bug, with the above command output");
10019 }
996c9314
LB
10020 vty_out(vty, "%s\n", json_object_to_json_string_ext(
10021 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 10022 json_object_free(json);
10023 } else {
10024
10025 if (peer->hostname
10026 && bgp_flag_check(peer->bgp, BGP_FLAG_SHOW_HOSTNAME)) {
10027 vty_out(vty, "Prefix counts for %s/%s, %s\n",
10028 peer->hostname, peer->host,
10029 afi_safi_print(afi, safi));
10030 } else {
10031 vty_out(vty, "Prefix counts for %s, %s\n", peer->host,
10032 afi_safi_print(afi, safi));
10033 }
10034
10035 vty_out(vty, "PfxCt: %ld\n", peer->pcount[afi][safi]);
10036 vty_out(vty, "\nCounts from RIB table walk:\n\n");
10037
10038 for (i = 0; i < PCOUNT_MAX; i++)
10039 vty_out(vty, "%20s: %-10d\n", pcount_strs[i],
10040 pcounts.count[i]);
10041
10042 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
10043 vty_out(vty, "%s [pcount] PfxCt drift!\n", peer->host);
10044 vty_out(vty,
10045 "Please report this bug, with the above command output\n");
10046 }
10047 }
10048
10049 return CMD_SUCCESS;
718e3744 10050}
10051
a636c635
DW
10052DEFUN (show_ip_bgp_instance_neighbor_prefix_counts,
10053 show_ip_bgp_instance_neighbor_prefix_counts_cmd,
c1a44e43 10054 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_CMD_STR"]] "
30a6a167 10055 "neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
718e3744 10056 SHOW_STR
10057 IP_STR
10058 BGP_STR
8386ac43 10059 BGP_INSTANCE_HELP_STR
9bedbb1e
DW
10060 BGP_AFI_HELP_STR
10061 BGP_SAFI_HELP_STR
0b16f239
DS
10062 "Detailed information on TCP and BGP neighbor connections\n"
10063 "Neighbor to display information about\n"
10064 "Neighbor to display information about\n"
91d37724 10065 "Neighbor on BGP configured interface\n"
a636c635 10066 "Display detailed prefix count information\n"
9973d184 10067 JSON_STR)
0b16f239 10068{
d62a17ae 10069 afi_t afi = AFI_IP6;
10070 safi_t safi = SAFI_UNICAST;
10071 struct peer *peer;
10072 int idx = 0;
10073 struct bgp *bgp = NULL;
9f049418
DS
10074 bool uj = use_json(argc, argv);
10075
10076 if (uj)
10077 argc--;
856ca177 10078
d62a17ae 10079 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 10080 &bgp, uj);
d62a17ae 10081 if (!idx)
10082 return CMD_WARNING;
0b16f239 10083
d62a17ae 10084 argv_find(argv, argc, "neighbors", &idx);
10085 peer = peer_lookup_in_view(vty, bgp, argv[idx + 1]->arg, uj);
10086 if (!peer)
10087 return CMD_WARNING;
bb46e94f 10088
d62a17ae 10089 return bgp_peer_counts(vty, peer, AFI_IP, SAFI_UNICAST, uj);
a636c635 10090}
0b16f239 10091
d6902373
PG
10092#ifdef KEEP_OLD_VPN_COMMANDS
10093DEFUN (show_ip_bgp_vpn_neighbor_prefix_counts,
10094 show_ip_bgp_vpn_neighbor_prefix_counts_cmd,
10095 "show [ip] bgp <vpnv4|vpnv6> all neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
2a71e9ce
TP
10096 SHOW_STR
10097 IP_STR
10098 BGP_STR
d6902373 10099 BGP_VPNVX_HELP_STR
91d37724 10100 "Display information about all VPNv4 NLRIs\n"
2a71e9ce
TP
10101 "Detailed information on TCP and BGP neighbor connections\n"
10102 "Neighbor to display information about\n"
10103 "Neighbor to display information about\n"
91d37724 10104 "Neighbor on BGP configured interface\n"
a636c635 10105 "Display detailed prefix count information\n"
9973d184 10106 JSON_STR)
a636c635 10107{
d62a17ae 10108 int idx_peer = 6;
10109 struct peer *peer;
9f049418 10110 bool uj = use_json(argc, argv);
a636c635 10111
d62a17ae 10112 peer = peer_lookup_in_view(vty, NULL, argv[idx_peer]->arg, uj);
10113 if (!peer)
10114 return CMD_WARNING;
10115
10116 return bgp_peer_counts(vty, peer, AFI_IP, SAFI_MPLS_VPN, uj);
a636c635
DW
10117}
10118
d6902373
PG
10119DEFUN (show_ip_bgp_vpn_all_route_prefix,
10120 show_ip_bgp_vpn_all_route_prefix_cmd,
10121 "show [ip] bgp <vpnv4|vpnv6> all <A.B.C.D|A.B.C.D/M> [json]",
91d37724
QY
10122 SHOW_STR
10123 IP_STR
10124 BGP_STR
d6902373 10125 BGP_VPNVX_HELP_STR
91d37724
QY
10126 "Display information about all VPNv4 NLRIs\n"
10127 "Network in the BGP routing table to display\n"
3a2d747c 10128 "Network in the BGP routing table to display\n"
9973d184 10129 JSON_STR)
91d37724 10130{
d62a17ae 10131 int idx = 0;
10132 char *network = NULL;
10133 struct bgp *bgp = bgp_get_default();
10134 if (!bgp) {
10135 vty_out(vty, "Can't find default instance\n");
10136 return CMD_WARNING;
10137 }
87e34b58 10138
d62a17ae 10139 if (argv_find(argv, argc, "A.B.C.D", &idx))
10140 network = argv[idx]->arg;
10141 else if (argv_find(argv, argc, "A.B.C.D/M", &idx))
10142 network = argv[idx]->arg;
10143 else {
10144 vty_out(vty, "Unable to figure out Network\n");
10145 return CMD_WARNING;
10146 }
87e34b58 10147
d62a17ae 10148 return bgp_show_route(vty, bgp, network, AFI_IP, SAFI_MPLS_VPN, NULL, 0,
360660c6 10149 BGP_PATH_SHOW_ALL, use_json(argc, argv));
91d37724 10150}
d6902373 10151#endif /* KEEP_OLD_VPN_COMMANDS */
91d37724 10152
4c63a661
PG
10153DEFUN (show_ip_bgp_l2vpn_evpn_all_route_prefix,
10154 show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd,
10155 "show [ip] bgp l2vpn evpn all <A.B.C.D|A.B.C.D/M> [json]",
10156 SHOW_STR
10157 IP_STR
10158 BGP_STR
10159 L2VPN_HELP_STR
10160 EVPN_HELP_STR
10161 "Display information about all EVPN NLRIs\n"
10162 "Network in the BGP routing table to display\n"
10163 "Network in the BGP routing table to display\n"
10164 JSON_STR)
10165{
d62a17ae 10166 int idx = 0;
10167 char *network = NULL;
a636c635 10168
d62a17ae 10169 if (argv_find(argv, argc, "A.B.C.D", &idx))
10170 network = argv[idx]->arg;
10171 else if (argv_find(argv, argc, "A.B.C.D/M", &idx))
10172 network = argv[idx]->arg;
10173 else {
10174 vty_out(vty, "Unable to figure out Network\n");
10175 return CMD_WARNING;
10176 }
10177 return bgp_show_route(vty, NULL, network, AFI_L2VPN, SAFI_EVPN, NULL, 0,
360660c6 10178 BGP_PATH_SHOW_ALL, use_json(argc, argv));
d62a17ae 10179}
10180
10181static void show_adj_route(struct vty *vty, struct peer *peer, afi_t afi,
6392aaa6 10182 safi_t safi, enum bgp_show_adj_route_type type,
9f049418 10183 const char *rmap_name, bool use_json,
6392aaa6 10184 json_object *json)
d62a17ae 10185{
10186 struct bgp_table *table;
10187 struct bgp_adj_in *ain;
10188 struct bgp_adj_out *adj;
10189 unsigned long output_count;
10190 unsigned long filtered_count;
10191 struct bgp_node *rn;
10192 int header1 = 1;
10193 struct bgp *bgp;
10194 int header2 = 1;
10195 struct attr attr;
10196 int ret;
10197 struct update_subgroup *subgrp;
10198 json_object *json_scode = NULL;
10199 json_object *json_ocode = NULL;
10200 json_object *json_ar = NULL;
10201 struct peer_af *paf;
f99def61 10202 bool route_filtered;
d62a17ae 10203
10204 if (use_json) {
10205 json_scode = json_object_new_object();
10206 json_ocode = json_object_new_object();
10207 json_ar = json_object_new_object();
10208
10209 json_object_string_add(json_scode, "suppressed", "s");
10210 json_object_string_add(json_scode, "damped", "d");
10211 json_object_string_add(json_scode, "history", "h");
10212 json_object_string_add(json_scode, "valid", "*");
10213 json_object_string_add(json_scode, "best", ">");
10214 json_object_string_add(json_scode, "multipath", "=");
10215 json_object_string_add(json_scode, "internal", "i");
10216 json_object_string_add(json_scode, "ribFailure", "r");
10217 json_object_string_add(json_scode, "stale", "S");
10218 json_object_string_add(json_scode, "removed", "R");
10219
10220 json_object_string_add(json_ocode, "igp", "i");
10221 json_object_string_add(json_ocode, "egp", "e");
10222 json_object_string_add(json_ocode, "incomplete", "?");
10223 }
a636c635 10224
d62a17ae 10225 bgp = peer->bgp;
a636c635 10226
d62a17ae 10227 if (!bgp) {
10228 if (use_json) {
10229 json_object_string_add(json, "alert", "no BGP");
10230 vty_out(vty, "%s\n", json_object_to_json_string(json));
10231 json_object_free(json);
10232 } else
10233 vty_out(vty, "%% No bgp\n");
10234 return;
10235 }
a636c635 10236
d62a17ae 10237 table = bgp->rib[afi][safi];
10238
10239 output_count = filtered_count = 0;
10240 subgrp = peer_subgroup(peer, afi, safi);
10241
6392aaa6 10242 if (type == bgp_show_adj_route_advertised && subgrp
d62a17ae 10243 && CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
10244 if (use_json) {
10245 json_object_int_add(json, "bgpTableVersion",
10246 table->version);
10247 json_object_string_add(json, "bgpLocalRouterId",
10248 inet_ntoa(bgp->router_id));
10249 json_object_object_add(json, "bgpStatusCodes",
10250 json_scode);
10251 json_object_object_add(json, "bgpOriginCodes",
10252 json_ocode);
07d0c4ed
DA
10253 json_object_string_add(
10254 json, "bgpOriginatingDefaultNetwork",
10255 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
d62a17ae 10256 } else {
996c9314 10257 vty_out(vty, "BGP table version is %" PRIu64
9df8b37c 10258 ", local router ID is %s, vrf id ",
d62a17ae 10259 table->version, inet_ntoa(bgp->router_id));
9df8b37c
PZ
10260 if (bgp->vrf_id == VRF_UNKNOWN)
10261 vty_out(vty, "%s", VRFID_NONE_STR);
10262 else
10263 vty_out(vty, "%u", bgp->vrf_id);
10264 vty_out(vty, "\n");
d62a17ae 10265 vty_out(vty, BGP_SHOW_SCODE_HEADER);
9df8b37c 10266 vty_out(vty, BGP_SHOW_NCODE_HEADER);
d62a17ae 10267 vty_out(vty, BGP_SHOW_OCODE_HEADER);
10268
07d0c4ed
DA
10269 vty_out(vty, "Originating default network %s\n\n",
10270 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
d62a17ae 10271 }
10272 header1 = 0;
10273 }
a636c635 10274
d62a17ae 10275 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
6392aaa6
PM
10276 if (type == bgp_show_adj_route_received
10277 || type == bgp_show_adj_route_filtered) {
d62a17ae 10278 for (ain = rn->adj_in; ain; ain = ain->next) {
6392aaa6 10279 if (ain->peer != peer || !ain->attr)
ea47320b 10280 continue;
6392aaa6 10281
ea47320b
DL
10282 if (header1) {
10283 if (use_json) {
10284 json_object_int_add(
60466a63 10285 json, "bgpTableVersion",
ea47320b
DL
10286 0);
10287 json_object_string_add(
10288 json,
10289 "bgpLocalRouterId",
10290 inet_ntoa(
10291 bgp->router_id));
10292 json_object_object_add(
60466a63 10293 json, "bgpStatusCodes",
ea47320b
DL
10294 json_scode);
10295 json_object_object_add(
60466a63 10296 json, "bgpOriginCodes",
ea47320b
DL
10297 json_ocode);
10298 } else {
10299 vty_out(vty,
9df8b37c 10300 "BGP table version is 0, local router ID is %s, vrf id ",
ea47320b 10301 inet_ntoa(
9df8b37c
PZ
10302 bgp->router_id));
10303 if (bgp->vrf_id == VRF_UNKNOWN)
10304 vty_out(vty, "%s",
10305 VRFID_NONE_STR);
10306 else
10307 vty_out(vty, "%u",
10308 bgp->vrf_id);
10309 vty_out(vty, "\n");
ea47320b
DL
10310 vty_out(vty,
10311 BGP_SHOW_SCODE_HEADER);
9df8b37c
PZ
10312 vty_out(vty,
10313 BGP_SHOW_NCODE_HEADER);
ea47320b
DL
10314 vty_out(vty,
10315 BGP_SHOW_OCODE_HEADER);
d62a17ae 10316 }
ea47320b
DL
10317 header1 = 0;
10318 }
10319 if (header2) {
10320 if (!use_json)
10321 vty_out(vty, BGP_SHOW_HEADER);
10322 header2 = 0;
10323 }
6392aaa6
PM
10324
10325 bgp_attr_dup(&attr, ain->attr);
f99def61
AD
10326 route_filtered = false;
10327
10328 /* Filter prefix using distribute list,
10329 * filter list or prefix list
10330 */
10331 if ((bgp_input_filter(peer, &rn->p, &attr, afi,
10332 safi)) == FILTER_DENY)
10333 route_filtered = true;
10334
10335 /* Filter prefix using route-map */
6392aaa6 10336 ret = bgp_input_modifier(peer, &rn->p, &attr,
13c8e163 10337 afi, safi, rmap_name);
6392aaa6 10338
13c8e163
AD
10339 if (type == bgp_show_adj_route_filtered &&
10340 !route_filtered && ret != RMAP_DENY) {
b755861b 10341 bgp_attr_undup(&attr, ain->attr);
6392aaa6 10342 continue;
d62a17ae 10343 }
6392aaa6 10344
13c8e163
AD
10345 if (type == bgp_show_adj_route_received &&
10346 (route_filtered || ret == RMAP_DENY))
6392aaa6
PM
10347 filtered_count++;
10348
10349 route_vty_out_tmp(vty, &rn->p, &attr, safi,
10350 use_json, json_ar);
b755861b 10351 bgp_attr_undup(&attr, ain->attr);
6392aaa6 10352 output_count++;
d62a17ae 10353 }
6392aaa6 10354 } else if (type == bgp_show_adj_route_advertised) {
d62a17ae 10355 for (adj = rn->adj_out; adj; adj = adj->next)
924c3f6a 10356 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
b755861b 10357 if (paf->peer != peer || !adj->attr)
924c3f6a 10358 continue;
d62a17ae 10359
924c3f6a
DS
10360 if (header1) {
10361 if (use_json) {
10362 json_object_int_add(
10363 json,
10364 "bgpTableVersion",
10365 table->version);
10366 json_object_string_add(
10367 json,
10368 "bgpLocalRouterId",
10369 inet_ntoa(
10370 bgp->router_id));
10371 json_object_object_add(
10372 json,
10373 "bgpStatusCodes",
10374 json_scode);
10375 json_object_object_add(
10376 json,
10377 "bgpOriginCodes",
10378 json_ocode);
10379 } else {
10380 vty_out(vty,
10381 "BGP table version is %" PRIu64
9df8b37c 10382 ", local router ID is %s, vrf id ",
924c3f6a
DS
10383 table->version,
10384 inet_ntoa(
10385 bgp->router_id));
9df8b37c
PZ
10386 if (bgp->vrf_id ==
10387 VRF_UNKNOWN)
10388 vty_out(vty,
10389 "%s",
10390 VRFID_NONE_STR);
10391 else
10392 vty_out(vty,
10393 "%u",
10394 bgp->vrf_id);
10395 vty_out(vty, "\n");
924c3f6a
DS
10396 vty_out(vty,
10397 BGP_SHOW_SCODE_HEADER);
9df8b37c
PZ
10398 vty_out(vty,
10399 BGP_SHOW_NCODE_HEADER);
924c3f6a
DS
10400 vty_out(vty,
10401 BGP_SHOW_OCODE_HEADER);
a2addae8 10402 }
924c3f6a
DS
10403 header1 = 0;
10404 }
924c3f6a
DS
10405 if (header2) {
10406 if (!use_json)
10407 vty_out(vty,
10408 BGP_SHOW_HEADER);
10409 header2 = 0;
10410 }
d62a17ae 10411
b755861b
PM
10412 bgp_attr_dup(&attr, adj->attr);
10413 ret = bgp_output_modifier(
10414 peer, &rn->p, &attr, afi, safi,
10415 rmap_name);
f46d8e1e 10416
b755861b
PM
10417 if (ret != RMAP_DENY) {
10418 route_vty_out_tmp(vty, &rn->p,
10419 &attr, safi,
10420 use_json,
10421 json_ar);
10422 output_count++;
10423 } else {
10424 filtered_count++;
a2addae8 10425 }
b755861b
PM
10426
10427 bgp_attr_undup(&attr, adj->attr);
924c3f6a 10428 }
d62a17ae 10429 }
10430 }
d62a17ae 10431
d62a17ae 10432 if (use_json) {
6392aaa6
PM
10433 json_object_object_add(json, "advertisedRoutes", json_ar);
10434 json_object_int_add(json, "totalPrefixCounter", output_count);
10435 json_object_int_add(json, "filteredPrefixCounter",
10436 filtered_count);
10437
996c9314
LB
10438 vty_out(vty, "%s\n", json_object_to_json_string_ext(
10439 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 10440 json_object_free(json);
6392aaa6
PM
10441 } else if (output_count > 0) {
10442 if (filtered_count > 0)
10443 vty_out(vty,
10444 "\nTotal number of prefixes %ld (%ld filtered)\n",
10445 output_count, filtered_count);
10446 else
10447 vty_out(vty, "\nTotal number of prefixes %ld\n",
10448 output_count);
d62a17ae 10449 }
a636c635 10450}
2a71e9ce 10451
d62a17ae 10452static int peer_adj_routes(struct vty *vty, struct peer *peer, afi_t afi,
6392aaa6 10453 safi_t safi, enum bgp_show_adj_route_type type,
9f049418 10454 const char *rmap_name, bool use_json)
0b16f239 10455{
d62a17ae 10456 json_object *json = NULL;
0b16f239 10457
d62a17ae 10458 if (use_json)
10459 json = json_object_new_object();
0b16f239 10460
d62a17ae 10461 /* labeled-unicast routes live in the unicast table */
10462 if (safi == SAFI_LABELED_UNICAST)
10463 safi = SAFI_UNICAST;
4dd6177e 10464
d62a17ae 10465 if (!peer || !peer->afc[afi][safi]) {
10466 if (use_json) {
10467 json_object_string_add(
10468 json, "warning",
10469 "No such neighbor or address family");
10470 vty_out(vty, "%s\n", json_object_to_json_string(json));
10471 json_object_free(json);
10472 } else
10473 vty_out(vty, "%% No such neighbor or address family\n");
0b16f239 10474
d62a17ae 10475 return CMD_WARNING;
10476 }
10477
6392aaa6
PM
10478 if ((type == bgp_show_adj_route_received
10479 || type == bgp_show_adj_route_filtered)
d62a17ae 10480 && !CHECK_FLAG(peer->af_flags[afi][safi],
10481 PEER_FLAG_SOFT_RECONFIG)) {
10482 if (use_json) {
10483 json_object_string_add(
10484 json, "warning",
10485 "Inbound soft reconfiguration not enabled");
10486 vty_out(vty, "%s\n", json_object_to_json_string(json));
10487 json_object_free(json);
10488 } else
10489 vty_out(vty,
10490 "%% Inbound soft reconfiguration not enabled\n");
10491
10492 return CMD_WARNING;
10493 }
0b16f239 10494
6392aaa6 10495 show_adj_route(vty, peer, afi, safi, type, rmap_name, use_json, json);
0b16f239 10496
d62a17ae 10497 return CMD_SUCCESS;
a636c635 10498}
50ef26d4 10499
a636c635
DW
10500DEFUN (show_ip_bgp_instance_neighbor_advertised_route,
10501 show_ip_bgp_instance_neighbor_advertised_route_cmd,
4dd6177e 10502 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] "
6392aaa6 10503 "neighbors <A.B.C.D|X:X::X:X|WORD> <advertised-routes|received-routes|filtered-routes> [route-map WORD] [json]",
718e3744 10504 SHOW_STR
10505 IP_STR
10506 BGP_STR
a636c635 10507 BGP_INSTANCE_HELP_STR
7395a2c9 10508 BGP_AFI_HELP_STR
4dd6177e 10509 BGP_SAFI_WITH_LABEL_HELP_STR
718e3744 10510 "Detailed information on TCP and BGP neighbor connections\n"
10511 "Neighbor to display information about\n"
10512 "Neighbor to display information about\n"
91d37724 10513 "Neighbor on BGP configured interface\n"
a636c635 10514 "Display the routes advertised to a BGP neighbor\n"
6392aaa6
PM
10515 "Display the received routes from neighbor\n"
10516 "Display the filtered routes received from neighbor\n"
a636c635
DW
10517 "Route-map to modify the attributes\n"
10518 "Name of the route map\n"
9973d184 10519 JSON_STR)
718e3744 10520{
d62a17ae 10521 afi_t afi = AFI_IP6;
10522 safi_t safi = SAFI_UNICAST;
10523 char *rmap_name = NULL;
10524 char *peerstr = NULL;
d62a17ae 10525 struct bgp *bgp = NULL;
10526 struct peer *peer;
6392aaa6 10527 enum bgp_show_adj_route_type type = bgp_show_adj_route_advertised;
d62a17ae 10528 int idx = 0;
9f049418 10529 bool uj = use_json(argc, argv);
6392aaa6 10530
d62a17ae 10531 if (uj)
10532 argc--;
30a6a167 10533
9f049418
DS
10534 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
10535 &bgp, uj);
10536 if (!idx)
10537 return CMD_WARNING;
10538
d62a17ae 10539 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
10540 argv_find(argv, argc, "neighbors", &idx);
10541 peerstr = argv[++idx]->arg;
8c3deaae 10542
d62a17ae 10543 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
10544 if (!peer)
10545 return CMD_WARNING;
856ca177 10546
d62a17ae 10547 if (argv_find(argv, argc, "advertised-routes", &idx))
6392aaa6
PM
10548 type = bgp_show_adj_route_advertised;
10549 else if (argv_find(argv, argc, "received-routes", &idx))
10550 type = bgp_show_adj_route_received;
10551 else if (argv_find(argv, argc, "filtered-routes", &idx))
10552 type = bgp_show_adj_route_filtered;
10553
d62a17ae 10554 if (argv_find(argv, argc, "route-map", &idx))
10555 rmap_name = argv[++idx]->arg;
95cbbd2a 10556
6392aaa6 10557 return peer_adj_routes(vty, peer, afi, safi, type, rmap_name, uj);
95cbbd2a
ML
10558}
10559
718e3744 10560DEFUN (show_ip_bgp_neighbor_received_prefix_filter,
10561 show_ip_bgp_neighbor_received_prefix_filter_cmd,
8c3deaae 10562 "show [ip] bgp [<ipv4|ipv6> [unicast]] neighbors <A.B.C.D|X:X::X:X|WORD> received prefix-filter [json]",
718e3744 10563 SHOW_STR
10564 IP_STR
10565 BGP_STR
8c3deaae
QY
10566 "Address Family\n"
10567 "Address Family\n"
718e3744 10568 "Address Family modifier\n"
10569 "Detailed information on TCP and BGP neighbor connections\n"
10570 "Neighbor to display information about\n"
10571 "Neighbor to display information about\n"
91d37724 10572 "Neighbor on BGP configured interface\n"
718e3744 10573 "Display information received from a BGP neighbor\n"
856ca177 10574 "Display the prefixlist filter\n"
9973d184 10575 JSON_STR)
718e3744 10576{
d62a17ae 10577 afi_t afi = AFI_IP6;
10578 safi_t safi = SAFI_UNICAST;
10579 char *peerstr = NULL;
10580
10581 char name[BUFSIZ];
10582 union sockunion su;
10583 struct peer *peer;
10584 int count, ret;
10585
10586 int idx = 0;
10587
10588 /* show [ip] bgp */
10589 if (argv_find(argv, argc, "ip", &idx))
10590 afi = AFI_IP;
10591 /* [<ipv4|ipv6> [unicast]] */
10592 if (argv_find(argv, argc, "ipv4", &idx))
10593 afi = AFI_IP;
10594 if (argv_find(argv, argc, "ipv6", &idx))
10595 afi = AFI_IP6;
10596 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
10597 argv_find(argv, argc, "neighbors", &idx);
10598 peerstr = argv[++idx]->arg;
10599
9f049418 10600 bool uj = use_json(argc, argv);
d62a17ae 10601
10602 ret = str2sockunion(peerstr, &su);
10603 if (ret < 0) {
10604 peer = peer_lookup_by_conf_if(NULL, peerstr);
10605 if (!peer) {
10606 if (uj)
10607 vty_out(vty, "{}\n");
10608 else
10609 vty_out(vty,
10610 "%% Malformed address or name: %s\n",
10611 peerstr);
10612 return CMD_WARNING;
10613 }
10614 } else {
10615 peer = peer_lookup(NULL, &su);
10616 if (!peer) {
10617 if (uj)
10618 vty_out(vty, "{}\n");
10619 else
10620 vty_out(vty, "No peer\n");
10621 return CMD_WARNING;
10622 }
10623 }
718e3744 10624
d62a17ae 10625 sprintf(name, "%s.%d.%d", peer->host, afi, safi);
10626 count = prefix_bgp_show_prefix_list(NULL, afi, name, uj);
10627 if (count) {
10628 if (!uj)
10629 vty_out(vty, "Address Family: %s\n",
10630 afi_safi_print(afi, safi));
10631 prefix_bgp_show_prefix_list(vty, afi, name, uj);
10632 } else {
10633 if (uj)
10634 vty_out(vty, "{}\n");
10635 else
10636 vty_out(vty, "No functional output\n");
10637 }
718e3744 10638
d62a17ae 10639 return CMD_SUCCESS;
10640}
10641
10642static int bgp_show_neighbor_route(struct vty *vty, struct peer *peer,
10643 afi_t afi, safi_t safi,
9f049418 10644 enum bgp_show_type type, bool use_json)
d62a17ae 10645{
8a893163
DW
10646 /* labeled-unicast routes live in the unicast table */
10647 if (safi == SAFI_LABELED_UNICAST)
10648 safi = SAFI_UNICAST;
10649
d62a17ae 10650 if (!peer || !peer->afc[afi][safi]) {
10651 if (use_json) {
10652 json_object *json_no = NULL;
10653 json_no = json_object_new_object();
10654 json_object_string_add(
10655 json_no, "warning",
10656 "No such neighbor or address family");
10657 vty_out(vty, "%s\n",
10658 json_object_to_json_string(json_no));
10659 json_object_free(json_no);
10660 } else
10661 vty_out(vty, "%% No such neighbor or address family\n");
10662 return CMD_WARNING;
10663 }
47fc97cc 10664
d62a17ae 10665 return bgp_show(vty, peer->bgp, afi, safi, type, &peer->su, use_json);
718e3744 10666}
10667
dba3c1d3
PG
10668DEFUN (show_ip_bgp_flowspec_routes_detailed,
10669 show_ip_bgp_flowspec_routes_detailed_cmd,
10670 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" flowspec] detail [json]",
10671 SHOW_STR
10672 IP_STR
10673 BGP_STR
10674 BGP_INSTANCE_HELP_STR
10675 BGP_AFI_HELP_STR
10676 "SAFI Flowspec\n"
10677 "Detailed information on flowspec entries\n"
10678 JSON_STR)
10679{
10680 afi_t afi = AFI_IP;
10681 safi_t safi = SAFI_UNICAST;
10682 struct bgp *bgp = NULL;
10683 int idx = 0;
9f049418
DS
10684 bool uj = use_json(argc, argv);
10685
10686 if (uj)
10687 argc--;
dba3c1d3
PG
10688
10689 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 10690 &bgp, uj);
dba3c1d3
PG
10691 if (!idx)
10692 return CMD_WARNING;
10693
9f049418 10694 return bgp_show(vty, bgp, afi, safi, bgp_show_type_detail, NULL, uj);
dba3c1d3
PG
10695}
10696
718e3744 10697DEFUN (show_ip_bgp_neighbor_routes,
10698 show_ip_bgp_neighbor_routes_cmd,
4dd6177e 10699 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] "
30a6a167 10700 "neighbors <A.B.C.D|X:X::X:X|WORD> <flap-statistics|dampened-routes|routes> [json]",
50ef26d4 10701 SHOW_STR
10702 IP_STR
10703 BGP_STR
8386ac43 10704 BGP_INSTANCE_HELP_STR
4f280b15 10705 BGP_AFI_HELP_STR
4dd6177e 10706 BGP_SAFI_WITH_LABEL_HELP_STR
718e3744 10707 "Detailed information on TCP and BGP neighbor connections\n"
10708 "Neighbor to display information about\n"
10709 "Neighbor to display information about\n"
91d37724 10710 "Neighbor on BGP configured interface\n"
2525cf39 10711 "Display flap statistics of the routes learned from neighbor\n"
8c3deaae
QY
10712 "Display the dampened routes received from neighbor\n"
10713 "Display routes learned from neighbor\n"
9973d184 10714 JSON_STR)
718e3744 10715{
d62a17ae 10716 char *peerstr = NULL;
10717 struct bgp *bgp = NULL;
10718 afi_t afi = AFI_IP6;
10719 safi_t safi = SAFI_UNICAST;
10720 struct peer *peer;
10721 enum bgp_show_type sh_type = bgp_show_type_neighbor;
d62a17ae 10722 int idx = 0;
9f049418
DS
10723 bool uj = use_json(argc, argv);
10724
10725 if (uj)
10726 argc--;
bb46e94f 10727
d62a17ae 10728 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
9f049418 10729 &bgp, uj);
d62a17ae 10730 if (!idx)
10731 return CMD_WARNING;
c493f2d8 10732
d62a17ae 10733 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
10734 argv_find(argv, argc, "neighbors", &idx);
10735 peerstr = argv[++idx]->arg;
8c3deaae 10736
d62a17ae 10737 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
9e6e6f46 10738 if (!peer)
d62a17ae 10739 return CMD_WARNING;
bb46e94f 10740
d62a17ae 10741 if (argv_find(argv, argc, "flap-statistics", &idx))
10742 sh_type = bgp_show_type_flap_neighbor;
10743 else if (argv_find(argv, argc, "dampened-routes", &idx))
10744 sh_type = bgp_show_type_damp_neighbor;
10745 else if (argv_find(argv, argc, "routes", &idx))
10746 sh_type = bgp_show_type_neighbor;
2525cf39 10747
d62a17ae 10748 return bgp_show_neighbor_route(vty, peer, afi, safi, sh_type, uj);
50ef26d4 10749}
6b0655a2 10750
734b349e 10751struct bgp_table *bgp_distance_table[AFI_MAX][SAFI_MAX];
718e3744 10752
d62a17ae 10753struct bgp_distance {
10754 /* Distance value for the IP source prefix. */
d7c0a89a 10755 uint8_t distance;
718e3744 10756
d62a17ae 10757 /* Name of the access-list to be matched. */
10758 char *access_list;
718e3744 10759};
10760
4f280b15
LB
10761DEFUN (show_bgp_afi_vpn_rd_route,
10762 show_bgp_afi_vpn_rd_route_cmd,
d114b977 10763 "show bgp "BGP_AFI_CMD_STR" vpn rd ASN:NN_OR_IP-ADDRESS:NN <A.B.C.D/M|X:X::X:X/M> [json]",
4f280b15
LB
10764 SHOW_STR
10765 BGP_STR
10766 BGP_AFI_HELP_STR
10767 "Address Family modifier\n"
10768 "Display information for a route distinguisher\n"
10769 "Route Distinguisher\n"
7395a2c9
DS
10770 "Network in the BGP routing table to display\n"
10771 "Network in the BGP routing table to display\n"
10772 JSON_STR)
4f280b15 10773{
d62a17ae 10774 int ret;
10775 struct prefix_rd prd;
10776 afi_t afi = AFI_MAX;
10777 int idx = 0;
4f280b15 10778
ff6566f3
DS
10779 if (!argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
10780 vty_out(vty, "%% Malformed Address Family\n");
10781 return CMD_WARNING;
10782 }
10783
d62a17ae 10784 ret = str2prefix_rd(argv[5]->arg, &prd);
10785 if (!ret) {
10786 vty_out(vty, "%% Malformed Route Distinguisher\n");
10787 return CMD_WARNING;
10788 }
ff6566f3 10789
d62a17ae 10790 return bgp_show_route(vty, NULL, argv[6]->arg, afi, SAFI_MPLS_VPN, &prd,
360660c6 10791 0, BGP_PATH_SHOW_ALL, use_json(argc, argv));
4f280b15
LB
10792}
10793
d62a17ae 10794static struct bgp_distance *bgp_distance_new(void)
718e3744 10795{
d62a17ae 10796 return XCALLOC(MTYPE_BGP_DISTANCE, sizeof(struct bgp_distance));
718e3744 10797}
10798
d62a17ae 10799static void bgp_distance_free(struct bgp_distance *bdistance)
718e3744 10800{
d62a17ae 10801 XFREE(MTYPE_BGP_DISTANCE, bdistance);
718e3744 10802}
10803
d62a17ae 10804static int bgp_distance_set(struct vty *vty, const char *distance_str,
10805 const char *ip_str, const char *access_list_str)
718e3744 10806{
d62a17ae 10807 int ret;
10808 afi_t afi;
10809 safi_t safi;
10810 struct prefix p;
d7c0a89a 10811 uint8_t distance;
d62a17ae 10812 struct bgp_node *rn;
10813 struct bgp_distance *bdistance;
718e3744 10814
d62a17ae 10815 afi = bgp_node_afi(vty);
10816 safi = bgp_node_safi(vty);
734b349e 10817
d62a17ae 10818 ret = str2prefix(ip_str, &p);
10819 if (ret == 0) {
10820 vty_out(vty, "Malformed prefix\n");
10821 return CMD_WARNING_CONFIG_FAILED;
10822 }
718e3744 10823
d62a17ae 10824 distance = atoi(distance_str);
718e3744 10825
d62a17ae 10826 /* Get BGP distance node. */
10827 rn = bgp_node_get(bgp_distance_table[afi][safi], (struct prefix *)&p);
ca2e160d
DS
10828 bdistance = bgp_distance_get_node(rn);
10829 if (bdistance)
d62a17ae 10830 bgp_unlock_node(rn);
ca2e160d 10831 else {
d62a17ae 10832 bdistance = bgp_distance_new();
a78beeb5 10833 bgp_distance_set_node_info(rn, bdistance);
d62a17ae 10834 }
718e3744 10835
d62a17ae 10836 /* Set distance value. */
10837 bdistance->distance = distance;
718e3744 10838
d62a17ae 10839 /* Reset access-list configuration. */
10840 if (bdistance->access_list) {
10841 XFREE(MTYPE_AS_LIST, bdistance->access_list);
10842 bdistance->access_list = NULL;
10843 }
10844 if (access_list_str)
10845 bdistance->access_list =
10846 XSTRDUP(MTYPE_AS_LIST, access_list_str);
718e3744 10847
d62a17ae 10848 return CMD_SUCCESS;
718e3744 10849}
10850
d62a17ae 10851static int bgp_distance_unset(struct vty *vty, const char *distance_str,
10852 const char *ip_str, const char *access_list_str)
718e3744 10853{
d62a17ae 10854 int ret;
10855 afi_t afi;
10856 safi_t safi;
10857 struct prefix p;
10858 int distance;
10859 struct bgp_node *rn;
10860 struct bgp_distance *bdistance;
718e3744 10861
d62a17ae 10862 afi = bgp_node_afi(vty);
10863 safi = bgp_node_safi(vty);
734b349e 10864
d62a17ae 10865 ret = str2prefix(ip_str, &p);
10866 if (ret == 0) {
10867 vty_out(vty, "Malformed prefix\n");
10868 return CMD_WARNING_CONFIG_FAILED;
10869 }
718e3744 10870
d62a17ae 10871 rn = bgp_node_lookup(bgp_distance_table[afi][safi],
10872 (struct prefix *)&p);
10873 if (!rn) {
10874 vty_out(vty, "Can't find specified prefix\n");
10875 return CMD_WARNING_CONFIG_FAILED;
10876 }
718e3744 10877
ca2e160d 10878 bdistance = bgp_distance_get_node(rn);
d62a17ae 10879 distance = atoi(distance_str);
1f9a9fff 10880
d62a17ae 10881 if (bdistance->distance != distance) {
10882 vty_out(vty, "Distance does not match configured\n");
10883 return CMD_WARNING_CONFIG_FAILED;
10884 }
718e3744 10885
d62a17ae 10886 if (bdistance->access_list)
10887 XFREE(MTYPE_AS_LIST, bdistance->access_list);
10888 bgp_distance_free(bdistance);
718e3744 10889
d62a17ae 10890 rn->info = NULL;
10891 bgp_unlock_node(rn);
10892 bgp_unlock_node(rn);
718e3744 10893
d62a17ae 10894 return CMD_SUCCESS;
718e3744 10895}
10896
718e3744 10897/* Apply BGP information to distance method. */
40381db7 10898uint8_t bgp_distance_apply(struct prefix *p, struct bgp_path_info *pinfo,
4b7e6066 10899 afi_t afi, safi_t safi, struct bgp *bgp)
d62a17ae 10900{
10901 struct bgp_node *rn;
10902 struct prefix q;
10903 struct peer *peer;
10904 struct bgp_distance *bdistance;
10905 struct access_list *alist;
10906 struct bgp_static *bgp_static;
10907
10908 if (!bgp)
10909 return 0;
10910
40381db7 10911 peer = pinfo->peer;
d62a17ae 10912
10913 /* Check source address. */
10914 sockunion2hostprefix(&peer->su, &q);
10915 rn = bgp_node_match(bgp_distance_table[afi][safi], &q);
10916 if (rn) {
ca2e160d 10917 bdistance = bgp_distance_get_node(rn);
d62a17ae 10918 bgp_unlock_node(rn);
10919
10920 if (bdistance->access_list) {
10921 alist = access_list_lookup(afi, bdistance->access_list);
10922 if (alist
10923 && access_list_apply(alist, p) == FILTER_PERMIT)
10924 return bdistance->distance;
10925 } else
10926 return bdistance->distance;
718e3744 10927 }
718e3744 10928
d62a17ae 10929 /* Backdoor check. */
10930 rn = bgp_node_lookup(bgp->route[afi][safi], p);
10931 if (rn) {
a78beeb5 10932 bgp_static = bgp_static_get_node_info(rn);
d62a17ae 10933 bgp_unlock_node(rn);
718e3744 10934
d62a17ae 10935 if (bgp_static->backdoor) {
10936 if (bgp->distance_local[afi][safi])
10937 return bgp->distance_local[afi][safi];
10938 else
10939 return ZEBRA_IBGP_DISTANCE_DEFAULT;
10940 }
718e3744 10941 }
718e3744 10942
d62a17ae 10943 if (peer->sort == BGP_PEER_EBGP) {
10944 if (bgp->distance_ebgp[afi][safi])
10945 return bgp->distance_ebgp[afi][safi];
10946 return ZEBRA_EBGP_DISTANCE_DEFAULT;
10947 } else {
10948 if (bgp->distance_ibgp[afi][safi])
10949 return bgp->distance_ibgp[afi][safi];
10950 return ZEBRA_IBGP_DISTANCE_DEFAULT;
10951 }
718e3744 10952}
10953
10954DEFUN (bgp_distance,
10955 bgp_distance_cmd,
6147e2c6 10956 "distance bgp (1-255) (1-255) (1-255)",
718e3744 10957 "Define an administrative distance\n"
10958 "BGP distance\n"
10959 "Distance for routes external to the AS\n"
10960 "Distance for routes internal to the AS\n"
10961 "Distance for local routes\n")
10962{
d62a17ae 10963 VTY_DECLVAR_CONTEXT(bgp, bgp);
10964 int idx_number = 2;
10965 int idx_number_2 = 3;
10966 int idx_number_3 = 4;
10967 afi_t afi;
10968 safi_t safi;
718e3744 10969
d62a17ae 10970 afi = bgp_node_afi(vty);
10971 safi = bgp_node_safi(vty);
718e3744 10972
d62a17ae 10973 bgp->distance_ebgp[afi][safi] = atoi(argv[idx_number]->arg);
10974 bgp->distance_ibgp[afi][safi] = atoi(argv[idx_number_2]->arg);
10975 bgp->distance_local[afi][safi] = atoi(argv[idx_number_3]->arg);
10976 return CMD_SUCCESS;
718e3744 10977}
10978
10979DEFUN (no_bgp_distance,
10980 no_bgp_distance_cmd,
a636c635 10981 "no distance bgp [(1-255) (1-255) (1-255)]",
718e3744 10982 NO_STR
10983 "Define an administrative distance\n"
10984 "BGP distance\n"
10985 "Distance for routes external to the AS\n"
10986 "Distance for routes internal to the AS\n"
10987 "Distance for local routes\n")
10988{
d62a17ae 10989 VTY_DECLVAR_CONTEXT(bgp, bgp);
10990 afi_t afi;
10991 safi_t safi;
718e3744 10992
d62a17ae 10993 afi = bgp_node_afi(vty);
10994 safi = bgp_node_safi(vty);
718e3744 10995
d62a17ae 10996 bgp->distance_ebgp[afi][safi] = 0;
10997 bgp->distance_ibgp[afi][safi] = 0;
10998 bgp->distance_local[afi][safi] = 0;
10999 return CMD_SUCCESS;
718e3744 11000}
11001
718e3744 11002
11003DEFUN (bgp_distance_source,
11004 bgp_distance_source_cmd,
6147e2c6 11005 "distance (1-255) A.B.C.D/M",
718e3744 11006 "Define an administrative distance\n"
11007 "Administrative distance\n"
11008 "IP source prefix\n")
11009{
d62a17ae 11010 int idx_number = 1;
11011 int idx_ipv4_prefixlen = 2;
11012 bgp_distance_set(vty, argv[idx_number]->arg,
11013 argv[idx_ipv4_prefixlen]->arg, NULL);
11014 return CMD_SUCCESS;
718e3744 11015}
11016
11017DEFUN (no_bgp_distance_source,
11018 no_bgp_distance_source_cmd,
6147e2c6 11019 "no distance (1-255) A.B.C.D/M",
718e3744 11020 NO_STR
11021 "Define an administrative distance\n"
11022 "Administrative distance\n"
11023 "IP source prefix\n")
11024{
d62a17ae 11025 int idx_number = 2;
11026 int idx_ipv4_prefixlen = 3;
11027 bgp_distance_unset(vty, argv[idx_number]->arg,
11028 argv[idx_ipv4_prefixlen]->arg, NULL);
11029 return CMD_SUCCESS;
718e3744 11030}
11031
11032DEFUN (bgp_distance_source_access_list,
11033 bgp_distance_source_access_list_cmd,
6147e2c6 11034 "distance (1-255) A.B.C.D/M WORD",
718e3744 11035 "Define an administrative distance\n"
11036 "Administrative distance\n"
11037 "IP source prefix\n"
11038 "Access list name\n")
11039{
d62a17ae 11040 int idx_number = 1;
11041 int idx_ipv4_prefixlen = 2;
11042 int idx_word = 3;
11043 bgp_distance_set(vty, argv[idx_number]->arg,
11044 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
11045 return CMD_SUCCESS;
718e3744 11046}
11047
11048DEFUN (no_bgp_distance_source_access_list,
11049 no_bgp_distance_source_access_list_cmd,
6147e2c6 11050 "no distance (1-255) A.B.C.D/M WORD",
718e3744 11051 NO_STR
11052 "Define an administrative distance\n"
11053 "Administrative distance\n"
11054 "IP source prefix\n"
11055 "Access list name\n")
11056{
d62a17ae 11057 int idx_number = 2;
11058 int idx_ipv4_prefixlen = 3;
11059 int idx_word = 4;
11060 bgp_distance_unset(vty, argv[idx_number]->arg,
11061 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
11062 return CMD_SUCCESS;
718e3744 11063}
6b0655a2 11064
734b349e
MZ
11065DEFUN (ipv6_bgp_distance_source,
11066 ipv6_bgp_distance_source_cmd,
39e92c06 11067 "distance (1-255) X:X::X:X/M",
734b349e
MZ
11068 "Define an administrative distance\n"
11069 "Administrative distance\n"
11070 "IP source prefix\n")
11071{
d62a17ae 11072 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, NULL);
11073 return CMD_SUCCESS;
734b349e
MZ
11074}
11075
11076DEFUN (no_ipv6_bgp_distance_source,
11077 no_ipv6_bgp_distance_source_cmd,
39e92c06 11078 "no distance (1-255) X:X::X:X/M",
734b349e
MZ
11079 NO_STR
11080 "Define an administrative distance\n"
11081 "Administrative distance\n"
11082 "IP source prefix\n")
11083{
d62a17ae 11084 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, NULL);
11085 return CMD_SUCCESS;
734b349e
MZ
11086}
11087
11088DEFUN (ipv6_bgp_distance_source_access_list,
11089 ipv6_bgp_distance_source_access_list_cmd,
39e92c06 11090 "distance (1-255) X:X::X:X/M WORD",
734b349e
MZ
11091 "Define an administrative distance\n"
11092 "Administrative distance\n"
11093 "IP source prefix\n"
11094 "Access list name\n")
11095{
d62a17ae 11096 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, argv[3]->arg);
11097 return CMD_SUCCESS;
734b349e
MZ
11098}
11099
11100DEFUN (no_ipv6_bgp_distance_source_access_list,
11101 no_ipv6_bgp_distance_source_access_list_cmd,
39e92c06 11102 "no distance (1-255) X:X::X:X/M WORD",
734b349e
MZ
11103 NO_STR
11104 "Define an administrative distance\n"
11105 "Administrative distance\n"
11106 "IP source prefix\n"
11107 "Access list name\n")
11108{
d62a17ae 11109 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, argv[4]->arg);
11110 return CMD_SUCCESS;
734b349e
MZ
11111}
11112
718e3744 11113DEFUN (bgp_damp_set,
11114 bgp_damp_set_cmd,
31500417 11115 "bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
718e3744 11116 "BGP Specific commands\n"
11117 "Enable route-flap dampening\n"
11118 "Half-life time for the penalty\n"
11119 "Value to start reusing a route\n"
11120 "Value to start suppressing a route\n"
11121 "Maximum duration to suppress a stable route\n")
11122{
d62a17ae 11123 VTY_DECLVAR_CONTEXT(bgp, bgp);
11124 int idx_half_life = 2;
11125 int idx_reuse = 3;
11126 int idx_suppress = 4;
11127 int idx_max_suppress = 5;
11128 int half = DEFAULT_HALF_LIFE * 60;
11129 int reuse = DEFAULT_REUSE;
11130 int suppress = DEFAULT_SUPPRESS;
11131 int max = 4 * half;
11132
11133 if (argc == 6) {
11134 half = atoi(argv[idx_half_life]->arg) * 60;
11135 reuse = atoi(argv[idx_reuse]->arg);
11136 suppress = atoi(argv[idx_suppress]->arg);
11137 max = atoi(argv[idx_max_suppress]->arg) * 60;
11138 } else if (argc == 3) {
11139 half = atoi(argv[idx_half_life]->arg) * 60;
11140 max = 4 * half;
11141 }
718e3744 11142
d62a17ae 11143 if (suppress < reuse) {
11144 vty_out(vty,
11145 "Suppress value cannot be less than reuse value \n");
11146 return 0;
11147 }
7ebe9748 11148
d62a17ae 11149 return bgp_damp_enable(bgp, bgp_node_afi(vty), bgp_node_safi(vty), half,
11150 reuse, suppress, max);
718e3744 11151}
11152
718e3744 11153DEFUN (bgp_damp_unset,
11154 bgp_damp_unset_cmd,
d04c479d 11155 "no bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
718e3744 11156 NO_STR
11157 "BGP Specific commands\n"
16cedbb0
QY
11158 "Enable route-flap dampening\n"
11159 "Half-life time for the penalty\n"
11160 "Value to start reusing a route\n"
11161 "Value to start suppressing a route\n"
11162 "Maximum duration to suppress a stable route\n")
718e3744 11163{
d62a17ae 11164 VTY_DECLVAR_CONTEXT(bgp, bgp);
11165 return bgp_damp_disable(bgp, bgp_node_afi(vty), bgp_node_safi(vty));
718e3744 11166}
11167
718e3744 11168/* Display specified route of BGP table. */
d62a17ae 11169static int bgp_clear_damp_route(struct vty *vty, const char *view_name,
11170 const char *ip_str, afi_t afi, safi_t safi,
11171 struct prefix_rd *prd, int prefix_check)
11172{
11173 int ret;
11174 struct prefix match;
11175 struct bgp_node *rn;
11176 struct bgp_node *rm;
40381db7
DS
11177 struct bgp_path_info *pi;
11178 struct bgp_path_info *pi_temp;
d62a17ae 11179 struct bgp *bgp;
11180 struct bgp_table *table;
11181
11182 /* BGP structure lookup. */
11183 if (view_name) {
11184 bgp = bgp_lookup_by_name(view_name);
11185 if (bgp == NULL) {
11186 vty_out(vty, "%% Can't find BGP instance %s\n",
11187 view_name);
11188 return CMD_WARNING;
11189 }
11190 } else {
11191 bgp = bgp_get_default();
11192 if (bgp == NULL) {
11193 vty_out(vty, "%% No BGP process is configured\n");
11194 return CMD_WARNING;
11195 }
718e3744 11196 }
718e3744 11197
d62a17ae 11198 /* Check IP address argument. */
11199 ret = str2prefix(ip_str, &match);
11200 if (!ret) {
11201 vty_out(vty, "%% address is malformed\n");
11202 return CMD_WARNING;
11203 }
718e3744 11204
d62a17ae 11205 match.family = afi2family(afi);
11206
11207 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
11208 || (safi == SAFI_EVPN)) {
11209 for (rn = bgp_table_top(bgp->rib[AFI_IP][safi]); rn;
11210 rn = bgp_route_next(rn)) {
11211 if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
11212 continue;
ea47320b
DL
11213 if ((table = rn->info) == NULL)
11214 continue;
11215 if ((rm = bgp_node_match(table, &match)) == NULL)
11216 continue;
d62a17ae 11217
ea47320b
DL
11218 if (!prefix_check
11219 || rm->p.prefixlen == match.prefixlen) {
40381db7
DS
11220 pi = rm->info;
11221 while (pi) {
11222 if (pi->extra && pi->extra->damp_info) {
11223 pi_temp = pi->next;
ea47320b 11224 bgp_damp_info_free(
40381db7 11225 pi->extra->damp_info,
60466a63 11226 1);
40381db7 11227 pi = pi_temp;
ea47320b 11228 } else
40381db7 11229 pi = pi->next;
d62a17ae 11230 }
ea47320b
DL
11231 }
11232
11233 bgp_unlock_node(rm);
d62a17ae 11234 }
11235 } else {
11236 if ((rn = bgp_node_match(bgp->rib[afi][safi], &match))
11237 != NULL) {
11238 if (!prefix_check
11239 || rn->p.prefixlen == match.prefixlen) {
40381db7
DS
11240 pi = rn->info;
11241 while (pi) {
11242 if (pi->extra && pi->extra->damp_info) {
11243 pi_temp = pi->next;
d62a17ae 11244 bgp_damp_info_free(
40381db7 11245 pi->extra->damp_info,
d62a17ae 11246 1);
40381db7 11247 pi = pi_temp;
d62a17ae 11248 } else
40381db7 11249 pi = pi->next;
d62a17ae 11250 }
11251 }
11252
11253 bgp_unlock_node(rn);
11254 }
11255 }
718e3744 11256
d62a17ae 11257 return CMD_SUCCESS;
718e3744 11258}
11259
11260DEFUN (clear_ip_bgp_dampening,
11261 clear_ip_bgp_dampening_cmd,
11262 "clear ip bgp dampening",
11263 CLEAR_STR
11264 IP_STR
11265 BGP_STR
11266 "Clear route flap dampening information\n")
11267{
d62a17ae 11268 bgp_damp_info_clean();
11269 return CMD_SUCCESS;
718e3744 11270}
11271
11272DEFUN (clear_ip_bgp_dampening_prefix,
11273 clear_ip_bgp_dampening_prefix_cmd,
11274 "clear ip bgp dampening A.B.C.D/M",
11275 CLEAR_STR
11276 IP_STR
11277 BGP_STR
11278 "Clear route flap dampening information\n"
0c7b1b01 11279 "IPv4 prefix\n")
718e3744 11280{
d62a17ae 11281 int idx_ipv4_prefixlen = 4;
11282 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4_prefixlen]->arg,
11283 AFI_IP, SAFI_UNICAST, NULL, 1);
718e3744 11284}
11285
11286DEFUN (clear_ip_bgp_dampening_address,
11287 clear_ip_bgp_dampening_address_cmd,
11288 "clear ip bgp dampening A.B.C.D",
11289 CLEAR_STR
11290 IP_STR
11291 BGP_STR
11292 "Clear route flap dampening information\n"
11293 "Network to clear damping information\n")
11294{
d62a17ae 11295 int idx_ipv4 = 4;
11296 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4]->arg, AFI_IP,
11297 SAFI_UNICAST, NULL, 0);
718e3744 11298}
11299
11300DEFUN (clear_ip_bgp_dampening_address_mask,
11301 clear_ip_bgp_dampening_address_mask_cmd,
11302 "clear ip bgp dampening A.B.C.D A.B.C.D",
11303 CLEAR_STR
11304 IP_STR
11305 BGP_STR
11306 "Clear route flap dampening information\n"
11307 "Network to clear damping information\n"
11308 "Network mask\n")
11309{
d62a17ae 11310 int idx_ipv4 = 4;
11311 int idx_ipv4_2 = 5;
11312 int ret;
11313 char prefix_str[BUFSIZ];
718e3744 11314
d62a17ae 11315 ret = netmask_str2prefix_str(argv[idx_ipv4]->arg, argv[idx_ipv4_2]->arg,
11316 prefix_str);
11317 if (!ret) {
11318 vty_out(vty, "%% Inconsistent address and mask\n");
11319 return CMD_WARNING;
11320 }
718e3744 11321
d62a17ae 11322 return bgp_clear_damp_route(vty, NULL, prefix_str, AFI_IP, SAFI_UNICAST,
11323 NULL, 0);
718e3744 11324}
6b0655a2 11325
825d9834
DS
11326static void show_bgp_peerhash_entry(struct hash_backet *backet, void *arg)
11327{
11328 struct vty *vty = arg;
11329 struct peer *peer = backet->data;
11330 char buf[SU_ADDRSTRLEN];
11331
11332 vty_out(vty, "\tPeer: %s %s\n", peer->host,
11333 sockunion2str(&peer->su, buf, sizeof(buf)));
11334}
11335
11336DEFUN (show_bgp_peerhash,
11337 show_bgp_peerhash_cmd,
11338 "show bgp peerhash",
11339 SHOW_STR
11340 BGP_STR
11341 "Display information about the BGP peerhash\n")
11342{
11343 struct list *instances = bm->bgp;
11344 struct listnode *node;
11345 struct bgp *bgp;
11346
11347 for (ALL_LIST_ELEMENTS_RO(instances, node, bgp)) {
11348 vty_out(vty, "BGP: %s\n", bgp->name);
11349 hash_iterate(bgp->peerhash, show_bgp_peerhash_entry,
11350 vty);
11351 }
11352
11353 return CMD_SUCCESS;
11354}
11355
587ff0fd 11356/* also used for encap safi */
2b791107
DL
11357static void bgp_config_write_network_vpn(struct vty *vty, struct bgp *bgp,
11358 afi_t afi, safi_t safi)
d62a17ae 11359{
11360 struct bgp_node *prn;
11361 struct bgp_node *rn;
11362 struct bgp_table *table;
11363 struct prefix *p;
11364 struct prefix_rd *prd;
11365 struct bgp_static *bgp_static;
11366 mpls_label_t label;
11367 char buf[SU_ADDRSTRLEN];
11368 char rdbuf[RD_ADDRSTRLEN];
11369
11370 /* Network configuration. */
11371 for (prn = bgp_table_top(bgp->route[afi][safi]); prn;
ea47320b
DL
11372 prn = bgp_route_next(prn)) {
11373 if ((table = prn->info) == NULL)
11374 continue;
d62a17ae 11375
60466a63 11376 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
a78beeb5
DS
11377 bgp_static = bgp_static_get_node_info(rn);
11378 if (bgp_static == NULL)
ea47320b 11379 continue;
d62a17ae 11380
ea47320b
DL
11381 p = &rn->p;
11382 prd = (struct prefix_rd *)&prn->p;
d62a17ae 11383
ea47320b 11384 /* "network" configuration display. */
06b9f471 11385 prefix_rd2str(prd, rdbuf, sizeof(rdbuf));
ea47320b
DL
11386 label = decode_label(&bgp_static->label);
11387
11388 vty_out(vty, " network %s/%d rd %s",
11389 inet_ntop(p->family, &p->u.prefix, buf,
11390 SU_ADDRSTRLEN),
11391 p->prefixlen, rdbuf);
11392 if (safi == SAFI_MPLS_VPN)
11393 vty_out(vty, " label %u", label);
11394
11395 if (bgp_static->rmap.name)
11396 vty_out(vty, " route-map %s",
11397 bgp_static->rmap.name);
e2a86ad9
DS
11398
11399 if (bgp_static->backdoor)
11400 vty_out(vty, " backdoor");
11401
ea47320b
DL
11402 vty_out(vty, "\n");
11403 }
11404 }
d62a17ae 11405}
11406
2b791107
DL
11407static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp,
11408 afi_t afi, safi_t safi)
d62a17ae 11409{
11410 struct bgp_node *prn;
11411 struct bgp_node *rn;
11412 struct bgp_table *table;
11413 struct prefix *p;
11414 struct prefix_rd *prd;
11415 struct bgp_static *bgp_static;
ff44f570 11416 char buf[PREFIX_STRLEN * 2];
d62a17ae 11417 char buf2[SU_ADDRSTRLEN];
11418 char rdbuf[RD_ADDRSTRLEN];
11419
11420 /* Network configuration. */
11421 for (prn = bgp_table_top(bgp->route[afi][safi]); prn;
ea47320b
DL
11422 prn = bgp_route_next(prn)) {
11423 if ((table = prn->info) == NULL)
11424 continue;
d62a17ae 11425
60466a63 11426 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
a78beeb5
DS
11427 bgp_static = bgp_static_get_node_info(rn);
11428 if (bgp_static == NULL)
ea47320b 11429 continue;
d62a17ae 11430
ea47320b
DL
11431 char *macrouter = NULL;
11432 char *esi = NULL;
d62a17ae 11433
ea47320b
DL
11434 if (bgp_static->router_mac)
11435 macrouter = prefix_mac2str(
11436 bgp_static->router_mac, NULL, 0);
11437 if (bgp_static->eth_s_id)
11438 esi = esi2str(bgp_static->eth_s_id);
11439 p = &rn->p;
11440 prd = (struct prefix_rd *)&prn->p;
d62a17ae 11441
ea47320b 11442 /* "network" configuration display. */
06b9f471 11443 prefix_rd2str(prd, rdbuf, sizeof(rdbuf));
197cb530
PG
11444 if (p->u.prefix_evpn.route_type == 5) {
11445 char local_buf[PREFIX_STRLEN];
3714a385 11446 uint8_t family = is_evpn_prefix_ipaddr_v4((
a4d82a8a
PZ
11447 struct prefix_evpn *)p)
11448 ? AF_INET
11449 : AF_INET6;
3714a385 11450 inet_ntop(family,
11451 &p->u.prefix_evpn.prefix_addr.ip.ip.addr,
a4d82a8a
PZ
11452 local_buf, PREFIX_STRLEN);
11453 sprintf(buf, "%s/%u", local_buf,
3714a385 11454 p->u.prefix_evpn.prefix_addr.ip_prefix_length);
197cb530
PG
11455 } else {
11456 prefix2str(p, buf, sizeof(buf));
11457 }
ea47320b 11458
a4d82a8a
PZ
11459 if (bgp_static->gatewayIp.family == AF_INET
11460 || bgp_static->gatewayIp.family == AF_INET6)
197cb530
PG
11461 inet_ntop(bgp_static->gatewayIp.family,
11462 &bgp_static->gatewayIp.u.prefix, buf2,
11463 sizeof(buf2));
ea47320b 11464 vty_out(vty,
7bcc8dac 11465 " network %s rd %s ethtag %u label %u esi %s gwip %s routermac %s\n",
3714a385 11466 buf, rdbuf,
11467 p->u.prefix_evpn.prefix_addr.eth_tag,
60466a63 11468 decode_label(&bgp_static->label), esi, buf2,
ea47320b
DL
11469 macrouter);
11470
11471 if (macrouter)
11472 XFREE(MTYPE_TMP, macrouter);
11473 if (esi)
11474 XFREE(MTYPE_TMP, esi);
11475 }
11476 }
3da6fcd5
PG
11477}
11478
718e3744 11479/* Configuration of static route announcement and aggregate
11480 information. */
2b791107
DL
11481void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
11482 safi_t safi)
d62a17ae 11483{
11484 struct bgp_node *rn;
11485 struct prefix *p;
11486 struct bgp_static *bgp_static;
11487 struct bgp_aggregate *bgp_aggregate;
11488 char buf[SU_ADDRSTRLEN];
11489
2b791107
DL
11490 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)) {
11491 bgp_config_write_network_vpn(vty, bgp, afi, safi);
11492 return;
11493 }
d62a17ae 11494
2b791107
DL
11495 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
11496 bgp_config_write_network_evpn(vty, bgp, afi, safi);
11497 return;
11498 }
d62a17ae 11499
11500 /* Network configuration. */
11501 for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
ea47320b 11502 rn = bgp_route_next(rn)) {
a78beeb5
DS
11503 bgp_static = bgp_static_get_node_info(rn);
11504 if (bgp_static == NULL)
ea47320b 11505 continue;
d62a17ae 11506
ea47320b 11507 p = &rn->p;
d62a17ae 11508
ea47320b 11509 /* "network" configuration display. */
60466a63 11510 if (bgp_option_check(BGP_OPT_CONFIG_CISCO) && afi == AFI_IP) {
d7c0a89a 11511 uint32_t destination;
ea47320b 11512 struct in_addr netmask;
d62a17ae 11513
ea47320b
DL
11514 destination = ntohl(p->u.prefix4.s_addr);
11515 masklen2ip(p->prefixlen, &netmask);
11516 vty_out(vty, " network %s",
11517 inet_ntop(p->family, &p->u.prefix, buf,
11518 SU_ADDRSTRLEN));
d62a17ae 11519
ea47320b
DL
11520 if ((IN_CLASSC(destination) && p->prefixlen == 24)
11521 || (IN_CLASSB(destination) && p->prefixlen == 16)
11522 || (IN_CLASSA(destination) && p->prefixlen == 8)
11523 || p->u.prefix4.s_addr == 0) {
11524 /* Natural mask is not display. */
11525 } else
11526 vty_out(vty, " mask %s", inet_ntoa(netmask));
11527 } else {
11528 vty_out(vty, " network %s/%d",
11529 inet_ntop(p->family, &p->u.prefix, buf,
11530 SU_ADDRSTRLEN),
11531 p->prefixlen);
11532 }
d62a17ae 11533
ea47320b
DL
11534 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX)
11535 vty_out(vty, " label-index %u",
11536 bgp_static->label_index);
d62a17ae 11537
ea47320b
DL
11538 if (bgp_static->rmap.name)
11539 vty_out(vty, " route-map %s", bgp_static->rmap.name);
e2a86ad9
DS
11540
11541 if (bgp_static->backdoor)
11542 vty_out(vty, " backdoor");
718e3744 11543
ea47320b
DL
11544 vty_out(vty, "\n");
11545 }
11546
d62a17ae 11547 /* Aggregate-address configuration. */
11548 for (rn = bgp_table_top(bgp->aggregate[afi][safi]); rn;
ea47320b 11549 rn = bgp_route_next(rn)) {
b1e62edd
DS
11550 bgp_aggregate = bgp_aggregate_get_node_info(rn);
11551 if (bgp_aggregate == NULL)
ea47320b 11552 continue;
d62a17ae 11553
ea47320b 11554 p = &rn->p;
d62a17ae 11555
ea47320b
DL
11556 if (bgp_option_check(BGP_OPT_CONFIG_CISCO) && afi == AFI_IP) {
11557 struct in_addr netmask;
d62a17ae 11558
ea47320b
DL
11559 masklen2ip(p->prefixlen, &netmask);
11560 vty_out(vty, " aggregate-address %s %s",
11561 inet_ntop(p->family, &p->u.prefix, buf,
11562 SU_ADDRSTRLEN),
11563 inet_ntoa(netmask));
11564 } else {
11565 vty_out(vty, " aggregate-address %s/%d",
11566 inet_ntop(p->family, &p->u.prefix, buf,
11567 SU_ADDRSTRLEN),
11568 p->prefixlen);
11569 }
d62a17ae 11570
ea47320b
DL
11571 if (bgp_aggregate->as_set)
11572 vty_out(vty, " as-set");
d62a17ae 11573
ea47320b
DL
11574 if (bgp_aggregate->summary_only)
11575 vty_out(vty, " summary-only");
718e3744 11576
ea47320b
DL
11577 vty_out(vty, "\n");
11578 }
d62a17ae 11579}
734b349e 11580
2b791107 11581void bgp_config_write_distance(struct vty *vty, struct bgp *bgp, afi_t afi,
60466a63 11582 safi_t safi)
d62a17ae 11583{
11584 struct bgp_node *rn;
11585 struct bgp_distance *bdistance;
11586
11587 /* Distance configuration. */
11588 if (bgp->distance_ebgp[afi][safi] && bgp->distance_ibgp[afi][safi]
11589 && bgp->distance_local[afi][safi]
11590 && (bgp->distance_ebgp[afi][safi] != ZEBRA_EBGP_DISTANCE_DEFAULT
11591 || bgp->distance_ibgp[afi][safi] != ZEBRA_IBGP_DISTANCE_DEFAULT
11592 || bgp->distance_local[afi][safi]
11593 != ZEBRA_IBGP_DISTANCE_DEFAULT)) {
d62a17ae 11594 vty_out(vty, " distance bgp %d %d %d\n",
11595 bgp->distance_ebgp[afi][safi],
11596 bgp->distance_ibgp[afi][safi],
11597 bgp->distance_local[afi][safi]);
11598 }
734b349e 11599
d62a17ae 11600 for (rn = bgp_table_top(bgp_distance_table[afi][safi]); rn;
ca2e160d
DS
11601 rn = bgp_route_next(rn)) {
11602 bdistance = bgp_distance_get_node(rn);
11603 if (bdistance != NULL) {
d62a17ae 11604 char buf[PREFIX_STRLEN];
11605
d62a17ae 11606 vty_out(vty, " distance %d %s %s\n",
11607 bdistance->distance,
11608 prefix2str(&rn->p, buf, sizeof(buf)),
11609 bdistance->access_list ? bdistance->access_list
11610 : "");
11611 }
ca2e160d 11612 }
718e3744 11613}
11614
11615/* Allocate routing table structure and install commands. */
d62a17ae 11616void bgp_route_init(void)
11617{
11618 afi_t afi;
11619 safi_t safi;
11620
11621 /* Init BGP distance table. */
05c7a1cc 11622 FOREACH_AFI_SAFI (afi, safi)
960035b2 11623 bgp_distance_table[afi][safi] = bgp_table_init(NULL, afi, safi);
d62a17ae 11624
11625 /* IPv4 BGP commands. */
11626 install_element(BGP_NODE, &bgp_table_map_cmd);
11627 install_element(BGP_NODE, &bgp_network_cmd);
d62a17ae 11628 install_element(BGP_NODE, &no_bgp_table_map_cmd);
d62a17ae 11629
11630 install_element(BGP_NODE, &aggregate_address_cmd);
11631 install_element(BGP_NODE, &aggregate_address_mask_cmd);
11632 install_element(BGP_NODE, &no_aggregate_address_cmd);
11633 install_element(BGP_NODE, &no_aggregate_address_mask_cmd);
11634
11635 /* IPv4 unicast configuration. */
11636 install_element(BGP_IPV4_NODE, &bgp_table_map_cmd);
11637 install_element(BGP_IPV4_NODE, &bgp_network_cmd);
d62a17ae 11638 install_element(BGP_IPV4_NODE, &no_bgp_table_map_cmd);
d62a17ae 11639
11640 install_element(BGP_IPV4_NODE, &aggregate_address_cmd);
11641 install_element(BGP_IPV4_NODE, &aggregate_address_mask_cmd);
11642 install_element(BGP_IPV4_NODE, &no_aggregate_address_cmd);
11643 install_element(BGP_IPV4_NODE, &no_aggregate_address_mask_cmd);
11644
11645 /* IPv4 multicast configuration. */
11646 install_element(BGP_IPV4M_NODE, &bgp_table_map_cmd);
11647 install_element(BGP_IPV4M_NODE, &bgp_network_cmd);
d62a17ae 11648 install_element(BGP_IPV4M_NODE, &no_bgp_table_map_cmd);
d62a17ae 11649 install_element(BGP_IPV4M_NODE, &aggregate_address_cmd);
11650 install_element(BGP_IPV4M_NODE, &aggregate_address_mask_cmd);
11651 install_element(BGP_IPV4M_NODE, &no_aggregate_address_cmd);
11652 install_element(BGP_IPV4M_NODE, &no_aggregate_address_mask_cmd);
11653
11654 /* IPv4 labeled-unicast configuration. */
11655 install_element(VIEW_NODE, &show_ip_bgp_instance_all_cmd);
11656 install_element(VIEW_NODE, &show_ip_bgp_cmd);
7b2ff250 11657 install_element(VIEW_NODE, &show_ip_bgp_json_cmd);
d62a17ae 11658 install_element(VIEW_NODE, &show_ip_bgp_route_cmd);
11659 install_element(VIEW_NODE, &show_ip_bgp_regexp_cmd);
11660
11661 install_element(VIEW_NODE,
11662 &show_ip_bgp_instance_neighbor_advertised_route_cmd);
11663 install_element(VIEW_NODE, &show_ip_bgp_neighbor_routes_cmd);
11664 install_element(VIEW_NODE,
11665 &show_ip_bgp_neighbor_received_prefix_filter_cmd);
d6902373 11666#ifdef KEEP_OLD_VPN_COMMANDS
d62a17ae 11667 install_element(VIEW_NODE, &show_ip_bgp_vpn_all_route_prefix_cmd);
d6902373 11668#endif /* KEEP_OLD_VPN_COMMANDS */
d62a17ae 11669 install_element(VIEW_NODE, &show_bgp_afi_vpn_rd_route_cmd);
11670 install_element(VIEW_NODE,
11671 &show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd);
57d187bc 11672
d62a17ae 11673 /* BGP dampening clear commands */
11674 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_cmd);
11675 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_prefix_cmd);
718e3744 11676
d62a17ae 11677 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_cmd);
11678 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_mask_cmd);
11679
11680 /* prefix count */
11681 install_element(ENABLE_NODE,
11682 &show_ip_bgp_instance_neighbor_prefix_counts_cmd);
d6902373 11683#ifdef KEEP_OLD_VPN_COMMANDS
d62a17ae 11684 install_element(ENABLE_NODE,
11685 &show_ip_bgp_vpn_neighbor_prefix_counts_cmd);
d6902373 11686#endif /* KEEP_OLD_VPN_COMMANDS */
ff7924f6 11687
d62a17ae 11688 /* New config IPv6 BGP commands. */
11689 install_element(BGP_IPV6_NODE, &bgp_table_map_cmd);
11690 install_element(BGP_IPV6_NODE, &ipv6_bgp_network_cmd);
d62a17ae 11691 install_element(BGP_IPV6_NODE, &no_bgp_table_map_cmd);
d62a17ae 11692
11693 install_element(BGP_IPV6_NODE, &ipv6_aggregate_address_cmd);
11694 install_element(BGP_IPV6_NODE, &no_ipv6_aggregate_address_cmd);
11695
11696 install_element(BGP_IPV6M_NODE, &ipv6_bgp_network_cmd);
d62a17ae 11697
d62a17ae 11698 install_element(BGP_NODE, &bgp_distance_cmd);
11699 install_element(BGP_NODE, &no_bgp_distance_cmd);
11700 install_element(BGP_NODE, &bgp_distance_source_cmd);
11701 install_element(BGP_NODE, &no_bgp_distance_source_cmd);
11702 install_element(BGP_NODE, &bgp_distance_source_access_list_cmd);
11703 install_element(BGP_NODE, &no_bgp_distance_source_access_list_cmd);
11704 install_element(BGP_IPV4_NODE, &bgp_distance_cmd);
11705 install_element(BGP_IPV4_NODE, &no_bgp_distance_cmd);
11706 install_element(BGP_IPV4_NODE, &bgp_distance_source_cmd);
11707 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_cmd);
11708 install_element(BGP_IPV4_NODE, &bgp_distance_source_access_list_cmd);
11709 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_access_list_cmd);
11710 install_element(BGP_IPV4M_NODE, &bgp_distance_cmd);
11711 install_element(BGP_IPV4M_NODE, &no_bgp_distance_cmd);
11712 install_element(BGP_IPV4M_NODE, &bgp_distance_source_cmd);
11713 install_element(BGP_IPV4M_NODE, &no_bgp_distance_source_cmd);
11714 install_element(BGP_IPV4M_NODE, &bgp_distance_source_access_list_cmd);
11715 install_element(BGP_IPV4M_NODE,
11716 &no_bgp_distance_source_access_list_cmd);
11717 install_element(BGP_IPV6_NODE, &bgp_distance_cmd);
11718 install_element(BGP_IPV6_NODE, &no_bgp_distance_cmd);
11719 install_element(BGP_IPV6_NODE, &ipv6_bgp_distance_source_cmd);
11720 install_element(BGP_IPV6_NODE, &no_ipv6_bgp_distance_source_cmd);
11721 install_element(BGP_IPV6_NODE,
11722 &ipv6_bgp_distance_source_access_list_cmd);
11723 install_element(BGP_IPV6_NODE,
11724 &no_ipv6_bgp_distance_source_access_list_cmd);
11725 install_element(BGP_IPV6M_NODE, &bgp_distance_cmd);
11726 install_element(BGP_IPV6M_NODE, &no_bgp_distance_cmd);
11727 install_element(BGP_IPV6M_NODE, &ipv6_bgp_distance_source_cmd);
11728 install_element(BGP_IPV6M_NODE, &no_ipv6_bgp_distance_source_cmd);
11729 install_element(BGP_IPV6M_NODE,
11730 &ipv6_bgp_distance_source_access_list_cmd);
11731 install_element(BGP_IPV6M_NODE,
11732 &no_ipv6_bgp_distance_source_access_list_cmd);
11733
11734 install_element(BGP_NODE, &bgp_damp_set_cmd);
11735 install_element(BGP_NODE, &bgp_damp_unset_cmd);
11736 install_element(BGP_IPV4_NODE, &bgp_damp_set_cmd);
11737 install_element(BGP_IPV4_NODE, &bgp_damp_unset_cmd);
11738
11739 /* IPv4 Multicast Mode */
11740 install_element(BGP_IPV4M_NODE, &bgp_damp_set_cmd);
11741 install_element(BGP_IPV4M_NODE, &bgp_damp_unset_cmd);
11742
11743 /* Large Communities */
11744 install_element(VIEW_NODE, &show_ip_bgp_large_community_list_cmd);
11745 install_element(VIEW_NODE, &show_ip_bgp_large_community_cmd);
dba3c1d3
PG
11746
11747 /* show bgp ipv4 flowspec detailed */
11748 install_element(VIEW_NODE, &show_ip_bgp_flowspec_routes_detailed_cmd);
11749
825d9834 11750 install_element(VIEW_NODE, &show_bgp_peerhash_cmd);
d62a17ae 11751}
11752
11753void bgp_route_finish(void)
11754{
11755 afi_t afi;
11756 safi_t safi;
11757
05c7a1cc
QY
11758 FOREACH_AFI_SAFI (afi, safi) {
11759 bgp_table_unlock(bgp_distance_table[afi][safi]);
11760 bgp_distance_table[afi][safi] = NULL;
11761 }
228da428 11762}