]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_vpn.c
Merge pull request #7809 from donaldsharp/m_coverity
[mirror_frr.git] / bgpd / bgp_vpn.c
CommitLineData
784d3a42 1/* VPN Related functions
896014f4
DL
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 */
784d3a42
PG
20
21#include <zebra.h>
22#include "command.h"
23#include "prefix.h"
24#include "lib/json.h"
23d0a753 25#include "lib/printfrr.h"
784d3a42
PG
26
27#include "bgpd/bgpd.h"
28#include "bgpd/bgp_route.h"
29#include "bgpd/bgp_table.h"
30#include "bgpd/bgp_attr.h"
31#include "bgpd/bgp_mplsvpn.h"
32#include "bgpd/bgp_vpn.h"
dc387b0f 33#include "bgpd/bgp_updgrp.h"
784d3a42 34
d62a17ae 35int show_adj_route_vpn(struct vty *vty, struct peer *peer,
36 struct prefix_rd *prd, afi_t afi, safi_t safi,
9f049418 37 bool use_json)
784d3a42 38{
d62a17ae 39 struct bgp *bgp;
40 struct bgp_table *table;
9bcb3eef
DS
41 struct bgp_dest *dest;
42 struct bgp_dest *rm;
d62a17ae 43 int rd_header;
44 int header = 1;
45 json_object *json = NULL;
46 json_object *json_scode = NULL;
47 json_object *json_ocode = NULL;
dc387b0f 48 json_object *json_adv = NULL;
d62a17ae 49 json_object *json_routes = NULL;
dc387b0f
LK
50 char rd_str[BUFSIZ];
51 unsigned long output_count = 0;
d62a17ae 52
53 bgp = bgp_get_default();
54 if (bgp == NULL) {
55 if (!use_json)
56 vty_out(vty, "No BGP process is configured\n");
16307668
RW
57 else
58 vty_out(vty, "{}\n");
d62a17ae 59 return CMD_WARNING;
60 }
61
62 if (use_json) {
63 json_scode = json_object_new_object();
64 json_ocode = json_object_new_object();
d62a17ae 65 json = json_object_new_object();
dc387b0f 66 json_adv = json_object_new_object();
d62a17ae 67
68 json_object_string_add(json_scode, "suppressed", "s");
69 json_object_string_add(json_scode, "damped", "d");
70 json_object_string_add(json_scode, "history", "h");
71 json_object_string_add(json_scode, "valid", "*");
72 json_object_string_add(json_scode, "best", ">");
73 json_object_string_add(json_scode, "internal", "i");
74
75 json_object_string_add(json_ocode, "igp", "i");
76 json_object_string_add(json_ocode, "egp", "e");
77 json_object_string_add(json_ocode, "incomplete", "?");
78 }
79
9bcb3eef
DS
80 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
81 dest = bgp_route_next(dest)) {
82 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
b54892e0 83
9bcb3eef 84 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
d62a17ae 85 continue;
86
9bcb3eef 87 table = bgp_dest_get_bgp_table_info(dest);
c4936a1a
DS
88 if (table == NULL)
89 continue;
d62a17ae 90
f5cfc290
LK
91 /*
92 * Initialize variables for each RD
93 * All prefixes under an RD is aggregated within "json_routes"
94 */
c4936a1a 95 rd_header = 1;
dc387b0f 96 memset(rd_str, 0, sizeof(rd_str));
f5cfc290 97 json_routes = NULL;
c4936a1a
DS
98
99 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
dc387b0f
LK
100 struct bgp_adj_out *adj = NULL;
101 struct attr *attr = NULL;
102 struct peer_af *paf = NULL;
103
104 RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out)
105 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
106 if (paf->peer != peer || !adj->attr)
107 continue;
108
109 attr = adj->attr;
110 break;
111 }
112
9bcb3eef 113 if (bgp_dest_get_bgp_path_info(rm) == NULL)
c4936a1a
DS
114 continue;
115
10d5be75
LK
116 if (!attr)
117 continue;
118
c4936a1a
DS
119 if (header) {
120 if (use_json) {
23d0a753
DA
121 char buf[BUFSIZ] = {0};
122
c4936a1a
DS
123 json_object_int_add(
124 json, "bgpTableVersion", 0);
125 json_object_string_add(
126 json, "bgpLocalRouterId",
23d0a753
DA
127 inet_ntop(AF_INET,
128 &bgp->router_id, buf,
129 sizeof(buf)));
dc387b0f
LK
130 json_object_int_add(
131 json,
132 "defaultLocPrf",
133 bgp->default_local_pref);
134 json_object_int_add(
135 json, "localAS",
136 bgp->as);
c4936a1a
DS
137 json_object_object_add(json,
138 "bgpStatusCodes",
139 json_scode);
140 json_object_object_add(json,
141 "bgpOriginCodes",
142 json_ocode);
143 } else {
144 vty_out(vty,
23d0a753
DA
145 "BGP table version is 0, local router ID is %pI4\n",
146 &bgp->router_id);
dc387b0f
LK
147 vty_out(vty, "Default local pref %u, ",
148 bgp->default_local_pref);
149 vty_out(vty, "local AS %u\n", bgp->as);
c4936a1a
DS
150 vty_out(vty,
151 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
152 vty_out(vty,
153 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
154 vty_out(vty, V4_HEADER);
155 }
156 header = 0;
157 }
d62a17ae 158
c4936a1a
DS
159 if (rd_header) {
160 uint16_t type;
161 struct rd_as rd_as = {0};
162 struct rd_ip rd_ip = {0};
49e5a4a0 163#ifdef ENABLE_BGP_VNC
c4936a1a 164 struct rd_vnc_eth rd_vnc_eth = {0};
784d3a42 165#endif
b54892e0 166 const uint8_t *pnt;
c4936a1a 167
9bcb3eef 168 pnt = dest_p->u.val;
c4936a1a
DS
169
170 /* Decode RD type. */
171 type = decode_rd_type(pnt);
172 /* Decode RD value. */
173 if (type == RD_TYPE_AS)
174 decode_rd_as(pnt + 2, &rd_as);
175 else if (type == RD_TYPE_AS4)
176 decode_rd_as4(pnt + 2, &rd_as);
177 else if (type == RD_TYPE_IP)
178 decode_rd_ip(pnt + 2, &rd_ip);
49e5a4a0 179#ifdef ENABLE_BGP_VNC
c4936a1a
DS
180 else if (type == RD_TYPE_VNC_ETH)
181 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
784d3a42 182#endif
c4936a1a 183 if (use_json) {
dc387b0f
LK
184 json_routes = json_object_new_object();
185
c4936a1a
DS
186 if (type == RD_TYPE_AS
187 || type == RD_TYPE_AS4)
772270f3
QY
188 snprintf(rd_str, sizeof(rd_str),
189 "%u:%d", rd_as.as,
190 rd_as.val);
c4936a1a 191 else if (type == RD_TYPE_IP)
23d0a753
DA
192 snprintfrr(rd_str,
193 sizeof(rd_str),
194 "%pI4:%d", &rd_ip.ip,
195 rd_ip.val);
c4936a1a
DS
196 json_object_string_add(
197 json_routes,
dc387b0f 198 "rd", rd_str);
c4936a1a
DS
199 } else {
200 vty_out(vty, "Route Distinguisher: ");
201
202 if (type == RD_TYPE_AS
203 || type == RD_TYPE_AS4)
204 vty_out(vty, "%u:%d", rd_as.as,
205 rd_as.val);
206 else if (type == RD_TYPE_IP)
23d0a753
DA
207 vty_out(vty, "%pI4:%d",
208 &rd_ip.ip, rd_ip.val);
49e5a4a0 209#ifdef ENABLE_BGP_VNC
c4936a1a
DS
210 else if (type == RD_TYPE_VNC_ETH)
211 vty_out(vty,
212 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
213 rd_vnc_eth.local_nve_id,
214 rd_vnc_eth.macaddr
215 .octet[0],
216 rd_vnc_eth.macaddr
217 .octet[1],
218 rd_vnc_eth.macaddr
219 .octet[2],
220 rd_vnc_eth.macaddr
221 .octet[3],
222 rd_vnc_eth.macaddr
223 .octet[4],
224 rd_vnc_eth.macaddr
225 .octet[5]);
784d3a42
PG
226#endif
227
c4936a1a 228 vty_out(vty, "\n");
d62a17ae 229 }
c4936a1a
DS
230 rd_header = 0;
231 }
9bcb3eef 232 route_vty_out_tmp(vty, bgp_dest_get_prefix(rm), attr,
ae248832 233 safi, use_json, json_routes, false);
dc387b0f 234 output_count++;
d62a17ae 235 }
dc387b0f 236
f5cfc290 237 if (use_json && json_routes)
dc387b0f 238 json_object_object_add(json_adv, rd_str, json_routes);
d62a17ae 239 }
dc387b0f 240
d62a17ae 241 if (use_json) {
dc387b0f
LK
242 json_object_object_add(json, "advertisedRoutes", json_adv);
243 json_object_int_add(json,
244 "totalPrefixCounter", output_count);
9d303b37
DL
245 vty_out(vty, "%s\n", json_object_to_json_string_ext(
246 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 247 json_object_free(json);
dc387b0f
LK
248 } else
249 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
250
d62a17ae 251 return CMD_SUCCESS;
784d3a42 252}