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