]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
Merge pull request #6483 from sylane/router-id-v6
[mirror_frr.git] / bgpd / bgp_vpn.c
1 /* VPN Related functions
2 * Copyright (C) 2017 6WIND
3 *
4 * This file is part of FRRouting
5 *
6 * FRRouting is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRRouting is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22 #include "command.h"
23 #include "prefix.h"
24 #include "lib/json.h"
25
26 #include "bgpd/bgpd.h"
27 #include "bgpd/bgp_route.h"
28 #include "bgpd/bgp_table.h"
29 #include "bgpd/bgp_attr.h"
30 #include "bgpd/bgp_mplsvpn.h"
31 #include "bgpd/bgp_vpn.h"
32 #include "bgpd/bgp_updgrp.h"
33
34 int show_adj_route_vpn(struct vty *vty, struct peer *peer,
35 struct prefix_rd *prd, afi_t afi, safi_t safi,
36 bool use_json)
37 {
38 struct bgp *bgp;
39 struct bgp_table *table;
40 struct bgp_dest *dest;
41 struct bgp_dest *rm;
42 int rd_header;
43 int header = 1;
44 json_object *json = NULL;
45 json_object *json_scode = NULL;
46 json_object *json_ocode = NULL;
47 json_object *json_adv = NULL;
48 json_object *json_routes = NULL;
49 char rd_str[BUFSIZ];
50 unsigned long output_count = 0;
51
52 bgp = bgp_get_default();
53 if (bgp == NULL) {
54 if (!use_json)
55 vty_out(vty, "No BGP process is configured\n");
56 else
57 vty_out(vty, "{}\n");
58 return CMD_WARNING;
59 }
60
61 if (use_json) {
62 json_scode = json_object_new_object();
63 json_ocode = json_object_new_object();
64 json = json_object_new_object();
65 json_adv = json_object_new_object();
66
67 json_object_string_add(json_scode, "suppressed", "s");
68 json_object_string_add(json_scode, "damped", "d");
69 json_object_string_add(json_scode, "history", "h");
70 json_object_string_add(json_scode, "valid", "*");
71 json_object_string_add(json_scode, "best", ">");
72 json_object_string_add(json_scode, "internal", "i");
73
74 json_object_string_add(json_ocode, "igp", "i");
75 json_object_string_add(json_ocode, "egp", "e");
76 json_object_string_add(json_ocode, "incomplete", "?");
77 }
78
79 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
80 dest = bgp_route_next(dest)) {
81 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
82
83 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
84 continue;
85
86 table = bgp_dest_get_bgp_table_info(dest);
87 if (table == NULL)
88 continue;
89
90 /*
91 * Initialize variables for each RD
92 * All prefixes under an RD is aggregated within "json_routes"
93 */
94 rd_header = 1;
95 memset(rd_str, 0, sizeof(rd_str));
96 json_routes = NULL;
97
98 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
99 struct bgp_adj_out *adj = NULL;
100 struct attr *attr = NULL;
101 struct peer_af *paf = NULL;
102
103 RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out)
104 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
105 if (paf->peer != peer || !adj->attr)
106 continue;
107
108 attr = adj->attr;
109 break;
110 }
111
112 if (bgp_dest_get_bgp_path_info(rm) == NULL)
113 continue;
114
115 if (!attr)
116 continue;
117
118 if (header) {
119 if (use_json) {
120 json_object_int_add(
121 json, "bgpTableVersion", 0);
122 json_object_string_add(
123 json, "bgpLocalRouterId",
124 inet_ntoa(bgp->router_id));
125 json_object_int_add(
126 json,
127 "defaultLocPrf",
128 bgp->default_local_pref);
129 json_object_int_add(
130 json, "localAS",
131 bgp->as);
132 json_object_object_add(json,
133 "bgpStatusCodes",
134 json_scode);
135 json_object_object_add(json,
136 "bgpOriginCodes",
137 json_ocode);
138 } else {
139 vty_out(vty,
140 "BGP table version is 0, local router ID is %s\n",
141 inet_ntoa(bgp->router_id));
142 vty_out(vty, "Default local pref %u, ",
143 bgp->default_local_pref);
144 vty_out(vty, "local AS %u\n", bgp->as);
145 vty_out(vty,
146 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
147 vty_out(vty,
148 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
149 vty_out(vty, V4_HEADER);
150 }
151 header = 0;
152 }
153
154 if (rd_header) {
155 uint16_t type;
156 struct rd_as rd_as = {0};
157 struct rd_ip rd_ip = {0};
158 #ifdef ENABLE_BGP_VNC
159 struct rd_vnc_eth rd_vnc_eth = {0};
160 #endif
161 const uint8_t *pnt;
162
163 pnt = dest_p->u.val;
164
165 /* Decode RD type. */
166 type = decode_rd_type(pnt);
167 /* Decode RD value. */
168 if (type == RD_TYPE_AS)
169 decode_rd_as(pnt + 2, &rd_as);
170 else if (type == RD_TYPE_AS4)
171 decode_rd_as4(pnt + 2, &rd_as);
172 else if (type == RD_TYPE_IP)
173 decode_rd_ip(pnt + 2, &rd_ip);
174 #ifdef ENABLE_BGP_VNC
175 else if (type == RD_TYPE_VNC_ETH)
176 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
177 #endif
178 if (use_json) {
179 json_routes = json_object_new_object();
180
181 if (type == RD_TYPE_AS
182 || type == RD_TYPE_AS4)
183 snprintf(rd_str, sizeof(rd_str),
184 "%u:%d", rd_as.as,
185 rd_as.val);
186 else if (type == RD_TYPE_IP)
187 snprintf(rd_str, sizeof(rd_str),
188 "%s:%d",
189 inet_ntoa(rd_ip.ip),
190 rd_ip.val);
191 json_object_string_add(
192 json_routes,
193 "rd", rd_str);
194 } else {
195 vty_out(vty, "Route Distinguisher: ");
196
197 if (type == RD_TYPE_AS
198 || type == RD_TYPE_AS4)
199 vty_out(vty, "%u:%d", rd_as.as,
200 rd_as.val);
201 else if (type == RD_TYPE_IP)
202 vty_out(vty, "%s:%d",
203 inet_ntoa(rd_ip.ip),
204 rd_ip.val);
205 #ifdef ENABLE_BGP_VNC
206 else if (type == RD_TYPE_VNC_ETH)
207 vty_out(vty,
208 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
209 rd_vnc_eth.local_nve_id,
210 rd_vnc_eth.macaddr
211 .octet[0],
212 rd_vnc_eth.macaddr
213 .octet[1],
214 rd_vnc_eth.macaddr
215 .octet[2],
216 rd_vnc_eth.macaddr
217 .octet[3],
218 rd_vnc_eth.macaddr
219 .octet[4],
220 rd_vnc_eth.macaddr
221 .octet[5]);
222 #endif
223
224 vty_out(vty, "\n");
225 }
226 rd_header = 0;
227 }
228 route_vty_out_tmp(vty, bgp_dest_get_prefix(rm), attr,
229 safi, use_json, json_routes, false);
230 output_count++;
231 }
232
233 if (use_json && json_routes)
234 json_object_object_add(json_adv, rd_str, json_routes);
235 }
236
237 if (use_json) {
238 json_object_object_add(json, "advertisedRoutes", json_adv);
239 json_object_int_add(json,
240 "totalPrefixCounter", output_count);
241 vty_out(vty, "%s\n", json_object_to_json_string_ext(
242 json, JSON_C_TO_STRING_PRETTY));
243 json_object_free(json);
244 } else
245 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
246
247 return CMD_SUCCESS;
248 }