]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / bgpd / bgp_vpn.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* VPN Related functions
3 * Copyright (C) 2017 6WIND
4 *
5 * This file is part of FRRouting
6 */
7
8 #include <zebra.h>
9 #include "command.h"
10 #include "prefix.h"
11 #include "lib/json.h"
12 #include "lib/printfrr.h"
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"
20 #include "bgpd/bgp_updgrp.h"
21
22 int show_adj_route_vpn(struct vty *vty, struct peer *peer,
23 struct prefix_rd *prd, afi_t afi, safi_t safi,
24 bool use_json)
25 {
26 struct bgp *bgp;
27 struct bgp_table *table;
28 struct bgp_dest *dest;
29 struct bgp_dest *rm;
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;
35 json_object *json_adv = NULL;
36 json_object *json_routes = NULL;
37 char rd_str[BUFSIZ];
38 unsigned long output_count = 0;
39
40 bgp = bgp_get_default();
41 if (bgp == NULL) {
42 if (!use_json)
43 vty_out(vty, "No BGP process is configured\n");
44 else
45 vty_out(vty, "{}\n");
46 return CMD_WARNING;
47 }
48
49 if (use_json) {
50 json_scode = json_object_new_object();
51 json_ocode = json_object_new_object();
52 json = json_object_new_object();
53 json_adv = json_object_new_object();
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
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);
70
71 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
72 continue;
73
74 table = bgp_dest_get_bgp_table_info(dest);
75 if (table == NULL)
76 continue;
77
78 /*
79 * Initialize variables for each RD
80 * All prefixes under an RD is aggregated within "json_routes"
81 */
82 rd_header = 1;
83 memset(rd_str, 0, sizeof(rd_str));
84 json_routes = NULL;
85
86 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
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
100 if (bgp_dest_get_bgp_path_info(rm) == NULL)
101 continue;
102
103 if (!attr)
104 continue;
105
106 if (header) {
107 if (use_json) {
108 json_object_int_add(
109 json, "bgpTableVersion", 0);
110 json_object_string_addf(
111 json, "bgpLocalRouterId",
112 "%pI4", &bgp->router_id);
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);
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,
128 "BGP table version is 0, local router ID is %pI4\n",
129 &bgp->router_id);
130 vty_out(vty, "Default local pref %u, ",
131 bgp->default_local_pref);
132 vty_out(vty, "local AS %u\n", bgp->as);
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 }
141
142 if (rd_header) {
143 uint16_t type;
144 struct rd_as rd_as = {0};
145 struct rd_ip rd_ip = {0};
146 #ifdef ENABLE_BGP_VNC
147 struct rd_vnc_eth rd_vnc_eth = {0};
148 #endif
149 const uint8_t *pnt;
150
151 pnt = dest_p->u.val;
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);
162 #ifdef ENABLE_BGP_VNC
163 else if (type == RD_TYPE_VNC_ETH)
164 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
165 #endif
166 if (use_json) {
167 json_routes = json_object_new_object();
168
169 if (type == RD_TYPE_AS
170 || type == RD_TYPE_AS4)
171 snprintf(rd_str, sizeof(rd_str),
172 "%u:%d", rd_as.as,
173 rd_as.val);
174 else if (type == RD_TYPE_IP)
175 snprintfrr(rd_str,
176 sizeof(rd_str),
177 "%pI4:%d", &rd_ip.ip,
178 rd_ip.val);
179 json_object_string_add(
180 json_routes,
181 "rd", rd_str);
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)
190 vty_out(vty, "%pI4:%d",
191 &rd_ip.ip, rd_ip.val);
192 #ifdef ENABLE_BGP_VNC
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]);
209 #endif
210
211 vty_out(vty, "\n");
212 }
213 rd_header = 0;
214 }
215 route_vty_out_tmp(vty, rm, bgp_dest_get_prefix(rm),
216 attr, safi, use_json, json_routes,
217 false);
218 output_count++;
219 }
220
221 if (use_json && json_routes)
222 json_object_object_add(json_adv, rd_str, json_routes);
223 }
224
225 if (use_json) {
226 json_object_object_add(json, "advertisedRoutes", json_adv);
227 json_object_int_add(json,
228 "totalPrefixCounter", output_count);
229 vty_json(vty, json);
230 } else
231 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
232
233 return CMD_SUCCESS;
234 }