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