]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
Merge pull request #3502 from donaldsharp/socket_to_me_baby
[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 bgp_path_info *path;
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 table = bgp_node_get_bgp_table_info(rn);
83 if (table == NULL)
84 continue;
85
86 if (use_json)
87 json_array = json_object_new_array();
88 else
89 json_array = NULL;
90
91 rd_header = 1;
92
93 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
94 path = bgp_node_get_bgp_path_info(rm);
95 if (path == NULL)
96 continue;
97
98 if (header) {
99 if (use_json) {
100 json_object_int_add(
101 json, "bgpTableVersion", 0);
102 json_object_string_add(
103 json, "bgpLocalRouterId",
104 inet_ntoa(bgp->router_id));
105 json_object_object_add(json,
106 "bgpStatusCodes",
107 json_scode);
108 json_object_object_add(json,
109 "bgpOriginCodes",
110 json_ocode);
111 } else {
112 vty_out(vty,
113 "BGP table version is 0, local router ID is %s\n",
114 inet_ntoa(bgp->router_id));
115 vty_out(vty,
116 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
117 vty_out(vty,
118 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
119 vty_out(vty, V4_HEADER);
120 }
121 header = 0;
122 }
123
124 if (rd_header) {
125 uint16_t type;
126 struct rd_as rd_as = {0};
127 struct rd_ip rd_ip = {0};
128 #if ENABLE_BGP_VNC
129 struct rd_vnc_eth rd_vnc_eth = {0};
130 #endif
131 uint8_t *pnt;
132
133 pnt = rn->p.u.val;
134
135 /* Decode RD type. */
136 type = decode_rd_type(pnt);
137 /* Decode RD value. */
138 if (type == RD_TYPE_AS)
139 decode_rd_as(pnt + 2, &rd_as);
140 else if (type == RD_TYPE_AS4)
141 decode_rd_as4(pnt + 2, &rd_as);
142 else if (type == RD_TYPE_IP)
143 decode_rd_ip(pnt + 2, &rd_ip);
144 #if ENABLE_BGP_VNC
145 else if (type == RD_TYPE_VNC_ETH)
146 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
147 #endif
148 if (use_json) {
149 char buffer[BUFSIZ];
150 if (type == RD_TYPE_AS
151 || type == RD_TYPE_AS4)
152 sprintf(buffer, "%u:%d",
153 rd_as.as, rd_as.val);
154 else if (type == RD_TYPE_IP)
155 sprintf(buffer, "%s:%d",
156 inet_ntoa(rd_ip.ip),
157 rd_ip.val);
158 json_object_string_add(
159 json_routes,
160 "routeDistinguisher", buffer);
161 } else {
162 vty_out(vty, "Route Distinguisher: ");
163
164 if (type == RD_TYPE_AS
165 || type == RD_TYPE_AS4)
166 vty_out(vty, "%u:%d", rd_as.as,
167 rd_as.val);
168 else if (type == RD_TYPE_IP)
169 vty_out(vty, "%s:%d",
170 inet_ntoa(rd_ip.ip),
171 rd_ip.val);
172 #if ENABLE_BGP_VNC
173 else if (type == RD_TYPE_VNC_ETH)
174 vty_out(vty,
175 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
176 rd_vnc_eth.local_nve_id,
177 rd_vnc_eth.macaddr
178 .octet[0],
179 rd_vnc_eth.macaddr
180 .octet[1],
181 rd_vnc_eth.macaddr
182 .octet[2],
183 rd_vnc_eth.macaddr
184 .octet[3],
185 rd_vnc_eth.macaddr
186 .octet[4],
187 rd_vnc_eth.macaddr
188 .octet[5]);
189 #endif
190
191 vty_out(vty, "\n");
192 }
193 rd_header = 0;
194 }
195 if (use_json) {
196 char buf[BUFSIZ];
197
198 prefix2str(&rm->p, buf, sizeof(buf));
199 json_object_object_add(json_routes, buf,
200 json_array);
201 } else {
202 route_vty_out_tmp(vty, &rm->p, path->attr,
203 SAFI_MPLS_VPN, use_json,
204 json_array);
205 }
206 }
207 }
208 if (use_json) {
209 json_object_object_add(json, "routes", json_routes);
210 vty_out(vty, "%s\n", json_object_to_json_string_ext(
211 json, JSON_C_TO_STRING_PRETTY));
212 json_object_free(json);
213 }
214 return CMD_SUCCESS;
215 }