]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_vpn.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bgpd / bgp_vpn.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
784d3a42 2/* VPN Related functions
896014f4
DL
3 * Copyright (C) 2017 6WIND
4 *
5 * This file is part of FRRouting
896014f4 6 */
784d3a42
PG
7
8#include <zebra.h>
9#include "command.h"
10#include "prefix.h"
11#include "lib/json.h"
23d0a753 12#include "lib/printfrr.h"
784d3a42
PG
13
14#include "bgpd/bgpd.h"
15#include "bgpd/bgp_route.h"
16#include "bgpd/bgp_table.h"
17#include "bgpd/bgp_attr.h"
18#include "bgpd/bgp_mplsvpn.h"
19#include "bgpd/bgp_vpn.h"
dc387b0f 20#include "bgpd/bgp_updgrp.h"
784d3a42 21
d62a17ae 22int show_adj_route_vpn(struct vty *vty, struct peer *peer,
23 struct prefix_rd *prd, afi_t afi, safi_t safi,
9f049418 24 bool use_json)
784d3a42 25{
d62a17ae 26 struct bgp *bgp;
27 struct bgp_table *table;
9bcb3eef
DS
28 struct bgp_dest *dest;
29 struct bgp_dest *rm;
d62a17ae 30 int rd_header;
31 int header = 1;
32 json_object *json = NULL;
33 json_object *json_scode = NULL;
34 json_object *json_ocode = NULL;
dc387b0f 35 json_object *json_adv = NULL;
d62a17ae 36 json_object *json_routes = NULL;
dc387b0f
LK
37 char rd_str[BUFSIZ];
38 unsigned long output_count = 0;
d62a17ae 39
40 bgp = bgp_get_default();
41 if (bgp == NULL) {
42 if (!use_json)
43 vty_out(vty, "No BGP process is configured\n");
16307668
RW
44 else
45 vty_out(vty, "{}\n");
d62a17ae 46 return CMD_WARNING;
47 }
48
49 if (use_json) {
50 json_scode = json_object_new_object();
51 json_ocode = json_object_new_object();
d62a17ae 52 json = json_object_new_object();
dc387b0f 53 json_adv = json_object_new_object();
d62a17ae 54
55 json_object_string_add(json_scode, "suppressed", "s");
56 json_object_string_add(json_scode, "damped", "d");
57 json_object_string_add(json_scode, "history", "h");
58 json_object_string_add(json_scode, "valid", "*");
59 json_object_string_add(json_scode, "best", ">");
60 json_object_string_add(json_scode, "internal", "i");
61
62 json_object_string_add(json_ocode, "igp", "i");
63 json_object_string_add(json_ocode, "egp", "e");
64 json_object_string_add(json_ocode, "incomplete", "?");
65 }
66
9bcb3eef
DS
67 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
68 dest = bgp_route_next(dest)) {
69 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
b54892e0 70
9bcb3eef 71 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
d62a17ae 72 continue;
73
9bcb3eef 74 table = bgp_dest_get_bgp_table_info(dest);
c4936a1a
DS
75 if (table == NULL)
76 continue;
d62a17ae 77
f5cfc290
LK
78 /*
79 * Initialize variables for each RD
80 * All prefixes under an RD is aggregated within "json_routes"
81 */
c4936a1a 82 rd_header = 1;
dc387b0f 83 memset(rd_str, 0, sizeof(rd_str));
f5cfc290 84 json_routes = NULL;
c4936a1a
DS
85
86 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
dc387b0f
LK
87 struct bgp_adj_out *adj = NULL;
88 struct attr *attr = NULL;
89 struct peer_af *paf = NULL;
90
91 RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out)
92 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
93 if (paf->peer != peer || !adj->attr)
94 continue;
95
96 attr = adj->attr;
97 break;
98 }
99
9bcb3eef 100 if (bgp_dest_get_bgp_path_info(rm) == NULL)
c4936a1a
DS
101 continue;
102
10d5be75
LK
103 if (!attr)
104 continue;
105
c4936a1a
DS
106 if (header) {
107 if (use_json) {
108 json_object_int_add(
109 json, "bgpTableVersion", 0);
c949c771 110 json_object_string_addf(
c4936a1a 111 json, "bgpLocalRouterId",
c949c771 112 "%pI4", &bgp->router_id);
dc387b0f
LK
113 json_object_int_add(
114 json,
115 "defaultLocPrf",
116 bgp->default_local_pref);
117 json_object_int_add(
118 json, "localAS",
119 bgp->as);
c4936a1a
DS
120 json_object_object_add(json,
121 "bgpStatusCodes",
122 json_scode);
123 json_object_object_add(json,
124 "bgpOriginCodes",
125 json_ocode);
126 } else {
127 vty_out(vty,
23d0a753
DA
128 "BGP table version is 0, local router ID is %pI4\n",
129 &bgp->router_id);
dc387b0f
LK
130 vty_out(vty, "Default local pref %u, ",
131 bgp->default_local_pref);
132 vty_out(vty, "local AS %u\n", bgp->as);
c4936a1a
DS
133 vty_out(vty,
134 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
135 vty_out(vty,
136 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
137 vty_out(vty, V4_HEADER);
138 }
139 header = 0;
140 }
d62a17ae 141
c4936a1a
DS
142 if (rd_header) {
143 uint16_t type;
144 struct rd_as rd_as = {0};
145 struct rd_ip rd_ip = {0};
49e5a4a0 146#ifdef ENABLE_BGP_VNC
c4936a1a 147 struct rd_vnc_eth rd_vnc_eth = {0};
784d3a42 148#endif
b54892e0 149 const uint8_t *pnt;
c4936a1a 150
9bcb3eef 151 pnt = dest_p->u.val;
c4936a1a
DS
152
153 /* Decode RD type. */
154 type = decode_rd_type(pnt);
155 /* Decode RD value. */
156 if (type == RD_TYPE_AS)
157 decode_rd_as(pnt + 2, &rd_as);
158 else if (type == RD_TYPE_AS4)
159 decode_rd_as4(pnt + 2, &rd_as);
160 else if (type == RD_TYPE_IP)
161 decode_rd_ip(pnt + 2, &rd_ip);
49e5a4a0 162#ifdef ENABLE_BGP_VNC
c4936a1a
DS
163 else if (type == RD_TYPE_VNC_ETH)
164 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
784d3a42 165#endif
c4936a1a 166 if (use_json) {
dc387b0f
LK
167 json_routes = json_object_new_object();
168
c4936a1a
DS
169 if (type == RD_TYPE_AS
170 || type == RD_TYPE_AS4)
772270f3
QY
171 snprintf(rd_str, sizeof(rd_str),
172 "%u:%d", rd_as.as,
173 rd_as.val);
c4936a1a 174 else if (type == RD_TYPE_IP)
23d0a753
DA
175 snprintfrr(rd_str,
176 sizeof(rd_str),
177 "%pI4:%d", &rd_ip.ip,
178 rd_ip.val);
c4936a1a
DS
179 json_object_string_add(
180 json_routes,
dc387b0f 181 "rd", rd_str);
c4936a1a
DS
182 } else {
183 vty_out(vty, "Route Distinguisher: ");
184
185 if (type == RD_TYPE_AS
186 || type == RD_TYPE_AS4)
187 vty_out(vty, "%u:%d", rd_as.as,
188 rd_as.val);
189 else if (type == RD_TYPE_IP)
23d0a753
DA
190 vty_out(vty, "%pI4:%d",
191 &rd_ip.ip, rd_ip.val);
49e5a4a0 192#ifdef ENABLE_BGP_VNC
c4936a1a
DS
193 else if (type == RD_TYPE_VNC_ETH)
194 vty_out(vty,
195 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
196 rd_vnc_eth.local_nve_id,
197 rd_vnc_eth.macaddr
198 .octet[0],
199 rd_vnc_eth.macaddr
200 .octet[1],
201 rd_vnc_eth.macaddr
202 .octet[2],
203 rd_vnc_eth.macaddr
204 .octet[3],
205 rd_vnc_eth.macaddr
206 .octet[4],
207 rd_vnc_eth.macaddr
208 .octet[5]);
784d3a42
PG
209#endif
210
c4936a1a 211 vty_out(vty, "\n");
d62a17ae 212 }
c4936a1a
DS
213 rd_header = 0;
214 }
7d3cae70
DA
215 route_vty_out_tmp(vty, rm, bgp_dest_get_prefix(rm),
216 attr, safi, use_json, json_routes,
217 false);
dc387b0f 218 output_count++;
d62a17ae 219 }
dc387b0f 220
f5cfc290 221 if (use_json && json_routes)
dc387b0f 222 json_object_object_add(json_adv, rd_str, json_routes);
d62a17ae 223 }
dc387b0f 224
d62a17ae 225 if (use_json) {
dc387b0f
LK
226 json_object_object_add(json, "advertisedRoutes", json_adv);
227 json_object_int_add(json,
228 "totalPrefixCounter", output_count);
75eeda93 229 vty_json(vty, json);
dc387b0f
LK
230 } else
231 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
232
d62a17ae 233 return CMD_SUCCESS;
784d3a42 234}