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