]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
Merge pull request #3370 from pguibert6WIND/default_vrf_initialization
[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
33 int show_adj_route_vpn(struct vty *vty, struct peer *peer,
34 struct prefix_rd *prd, afi_t afi, safi_t safi,
35 bool use_json)
36 {
37 struct bgp *bgp;
38 struct bgp_table *table;
39 struct bgp_node *rn;
40 struct bgp_node *rm;
41 struct attr *attr;
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_routes = NULL;
48 json_object *json_array = NULL;
49
50 bgp = bgp_get_default();
51 if (bgp == NULL) {
52 if (!use_json)
53 vty_out(vty, "No BGP process is configured\n");
54 else
55 vty_out(vty, "{}\n");
56 return CMD_WARNING;
57 }
58
59 if (use_json) {
60 json_scode = json_object_new_object();
61 json_ocode = json_object_new_object();
62 json_routes = json_object_new_object();
63 json = json_object_new_object();
64
65 json_object_string_add(json_scode, "suppressed", "s");
66 json_object_string_add(json_scode, "damped", "d");
67 json_object_string_add(json_scode, "history", "h");
68 json_object_string_add(json_scode, "valid", "*");
69 json_object_string_add(json_scode, "best", ">");
70 json_object_string_add(json_scode, "internal", "i");
71
72 json_object_string_add(json_ocode, "igp", "i");
73 json_object_string_add(json_ocode, "egp", "e");
74 json_object_string_add(json_ocode, "incomplete", "?");
75 }
76
77 for (rn = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); rn;
78 rn = bgp_route_next(rn)) {
79 if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
80 continue;
81
82 if ((table = rn->info) != NULL) {
83 if (use_json)
84 json_array = json_object_new_array();
85 else
86 json_array = NULL;
87
88 rd_header = 1;
89
90 for (rm = bgp_table_top(table); rm;
91 rm = bgp_route_next(rm)) {
92 if ((attr = rm->info) != NULL) {
93 if (header) {
94 if (use_json) {
95 json_object_int_add(
96 json,
97 "bgpTableVersion",
98 0);
99 json_object_string_add(
100 json,
101 "bgpLocalRouterId",
102 inet_ntoa(
103 bgp->router_id));
104 json_object_object_add(
105 json,
106 "bgpStatusCodes",
107 json_scode);
108 json_object_object_add(
109 json,
110 "bgpOriginCodes",
111 json_ocode);
112 } else {
113 vty_out(vty,
114 "BGP table version is 0, local router ID is %s\n",
115 inet_ntoa(
116 bgp->router_id));
117 vty_out(vty,
118 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
119 vty_out(vty,
120 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
121 vty_out(vty, V4_HEADER);
122 }
123 header = 0;
124 }
125
126 if (rd_header) {
127 uint16_t type;
128 struct rd_as rd_as = {0};
129 struct rd_ip rd_ip = {0};
130 #if ENABLE_BGP_VNC
131 struct rd_vnc_eth rd_vnc_eth = {
132 0};
133 #endif
134 uint8_t *pnt;
135
136 pnt = rn->p.u.val;
137
138 /* Decode RD type. */
139 type = decode_rd_type(pnt);
140 /* Decode RD value. */
141 if (type == RD_TYPE_AS)
142 decode_rd_as(pnt + 2,
143 &rd_as);
144 else if (type == RD_TYPE_AS4)
145 decode_rd_as4(pnt + 2,
146 &rd_as);
147 else if (type == RD_TYPE_IP)
148 decode_rd_ip(pnt + 2,
149 &rd_ip);
150 #if ENABLE_BGP_VNC
151 else if (type
152 == RD_TYPE_VNC_ETH)
153 decode_rd_vnc_eth(
154 pnt,
155 &rd_vnc_eth);
156 #endif
157
158 if (use_json) {
159 char buffer[BUFSIZ];
160 if (type == RD_TYPE_AS
161 || type == RD_TYPE_AS4)
162 sprintf(buffer,
163 "%u:%d",
164 rd_as.as,
165 rd_as.val);
166 else if (type
167 == RD_TYPE_IP)
168 sprintf(buffer,
169 "%s:%d",
170 inet_ntoa(
171 rd_ip.ip),
172 rd_ip.val);
173 json_object_string_add(
174 json_routes,
175 "routeDistinguisher",
176 buffer);
177 } else {
178 vty_out(vty,
179 "Route Distinguisher: ");
180
181 if (type == RD_TYPE_AS
182 || type == RD_TYPE_AS4)
183 vty_out(vty,
184 "%u:%d",
185 rd_as.as,
186 rd_as.val);
187 else if (type
188 == RD_TYPE_IP)
189 vty_out(vty,
190 "%s:%d",
191 inet_ntoa(
192 rd_ip.ip),
193 rd_ip.val);
194 #if ENABLE_BGP_VNC
195 else if (
196 type
197 == RD_TYPE_VNC_ETH)
198 vty_out(vty,
199 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
200 rd_vnc_eth
201 .local_nve_id,
202 rd_vnc_eth
203 .macaddr
204 .octet[0],
205 rd_vnc_eth
206 .macaddr
207 .octet[1],
208 rd_vnc_eth
209 .macaddr
210 .octet[2],
211 rd_vnc_eth
212 .macaddr
213 .octet[3],
214 rd_vnc_eth
215 .macaddr
216 .octet[4],
217 rd_vnc_eth
218 .macaddr
219 .octet[5]);
220 #endif
221
222 vty_out(vty, "\n");
223 }
224 rd_header = 0;
225 }
226 if (use_json) {
227 char buf_a[BUFSIZ];
228 char buf_b[BUFSIZ];
229
230 sprintf(buf_a, "%s/%d",
231 inet_ntop(rm->p.family,
232 rm->p.u.val,
233 buf_b,
234 BUFSIZ),
235 rm->p.prefixlen);
236 json_object_object_add(
237 json_routes, buf_a,
238 json_array);
239 } else {
240 route_vty_out_tmp(
241 vty, &rm->p, attr,
242 SAFI_MPLS_VPN, use_json,
243 json_array);
244 }
245 }
246 }
247 }
248 }
249 if (use_json) {
250 json_object_object_add(json, "routes", json_routes);
251 vty_out(vty, "%s\n", json_object_to_json_string_ext(
252 json, JSON_C_TO_STRING_PRETTY));
253 json_object_free(json);
254 }
255 return CMD_SUCCESS;
256 }