]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_vpn.c
Merge pull request #5172 from donaldsharp/sa_clean_and_clean
[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"
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"
dc387b0f 32#include "bgpd/bgp_updgrp.h"
784d3a42 33
d62a17ae 34int show_adj_route_vpn(struct vty *vty, struct peer *peer,
35 struct prefix_rd *prd, afi_t afi, safi_t safi,
9f049418 36 bool use_json)
784d3a42 37{
d62a17ae 38 struct bgp *bgp;
39 struct bgp_table *table;
40 struct bgp_node *rn;
41 struct bgp_node *rm;
d62a17ae 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;
dc387b0f 47 json_object *json_adv = NULL;
d62a17ae 48 json_object *json_routes = NULL;
dc387b0f
LK
49 char rd_str[BUFSIZ];
50 unsigned long output_count = 0;
d62a17ae 51
52 bgp = bgp_get_default();
53 if (bgp == NULL) {
54 if (!use_json)
55 vty_out(vty, "No BGP process is configured\n");
16307668
RW
56 else
57 vty_out(vty, "{}\n");
d62a17ae 58 return CMD_WARNING;
59 }
60
61 if (use_json) {
62 json_scode = json_object_new_object();
63 json_ocode = json_object_new_object();
d62a17ae 64 json = json_object_new_object();
dc387b0f 65 json_adv = json_object_new_object();
d62a17ae 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
dcc1615e 79 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
d62a17ae 80 rn = bgp_route_next(rn)) {
81 if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
82 continue;
83
67009e22 84 table = bgp_node_get_bgp_table_info(rn);
c4936a1a
DS
85 if (table == NULL)
86 continue;
d62a17ae 87
c4936a1a
DS
88
89 rd_header = 1;
dc387b0f 90 memset(rd_str, 0, sizeof(rd_str));
c4936a1a
DS
91
92 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
dc387b0f
LK
93 struct bgp_adj_out *adj = NULL;
94 struct attr *attr = NULL;
95 struct peer_af *paf = NULL;
96
97 RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out)
98 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
99 if (paf->peer != peer || !adj->attr)
100 continue;
101
102 attr = adj->attr;
103 break;
104 }
105
106 if (bgp_node_get_bgp_path_info(rm) == NULL)
c4936a1a
DS
107 continue;
108
109 if (header) {
110 if (use_json) {
111 json_object_int_add(
112 json, "bgpTableVersion", 0);
113 json_object_string_add(
114 json, "bgpLocalRouterId",
115 inet_ntoa(bgp->router_id));
dc387b0f
LK
116 json_object_int_add(
117 json,
118 "defaultLocPrf",
119 bgp->default_local_pref);
120 json_object_int_add(
121 json, "localAS",
122 bgp->as);
c4936a1a
DS
123 json_object_object_add(json,
124 "bgpStatusCodes",
125 json_scode);
126 json_object_object_add(json,
127 "bgpOriginCodes",
128 json_ocode);
129 } else {
130 vty_out(vty,
131 "BGP table version is 0, local router ID is %s\n",
132 inet_ntoa(bgp->router_id));
dc387b0f
LK
133 vty_out(vty, "Default local pref %u, ",
134 bgp->default_local_pref);
135 vty_out(vty, "local AS %u\n", bgp->as);
c4936a1a
DS
136 vty_out(vty,
137 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
138 vty_out(vty,
139 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
140 vty_out(vty, V4_HEADER);
141 }
142 header = 0;
143 }
d62a17ae 144
c4936a1a
DS
145 if (rd_header) {
146 uint16_t type;
147 struct rd_as rd_as = {0};
148 struct rd_ip rd_ip = {0};
784d3a42 149#if ENABLE_BGP_VNC
c4936a1a 150 struct rd_vnc_eth rd_vnc_eth = {0};
784d3a42 151#endif
c4936a1a
DS
152 uint8_t *pnt;
153
154 pnt = rn->p.u.val;
155
156 /* Decode RD type. */
157 type = decode_rd_type(pnt);
158 /* Decode RD value. */
159 if (type == RD_TYPE_AS)
160 decode_rd_as(pnt + 2, &rd_as);
161 else if (type == RD_TYPE_AS4)
162 decode_rd_as4(pnt + 2, &rd_as);
163 else if (type == RD_TYPE_IP)
164 decode_rd_ip(pnt + 2, &rd_ip);
784d3a42 165#if ENABLE_BGP_VNC
c4936a1a
DS
166 else if (type == RD_TYPE_VNC_ETH)
167 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
784d3a42 168#endif
c4936a1a 169 if (use_json) {
dc387b0f
LK
170 json_routes = json_object_new_object();
171
c4936a1a
DS
172 if (type == RD_TYPE_AS
173 || type == RD_TYPE_AS4)
dc387b0f 174 sprintf(rd_str, "%u:%d",
c4936a1a
DS
175 rd_as.as, rd_as.val);
176 else if (type == RD_TYPE_IP)
dc387b0f 177 sprintf(rd_str, "%s:%d",
c4936a1a
DS
178 inet_ntoa(rd_ip.ip),
179 rd_ip.val);
180 json_object_string_add(
181 json_routes,
dc387b0f 182 "rd", rd_str);
c4936a1a
DS
183 } else {
184 vty_out(vty, "Route Distinguisher: ");
185
186 if (type == RD_TYPE_AS
187 || type == RD_TYPE_AS4)
188 vty_out(vty, "%u:%d", rd_as.as,
189 rd_as.val);
190 else if (type == RD_TYPE_IP)
191 vty_out(vty, "%s:%d",
192 inet_ntoa(rd_ip.ip),
193 rd_ip.val);
784d3a42 194#if ENABLE_BGP_VNC
c4936a1a
DS
195 else if (type == RD_TYPE_VNC_ETH)
196 vty_out(vty,
197 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
198 rd_vnc_eth.local_nve_id,
199 rd_vnc_eth.macaddr
200 .octet[0],
201 rd_vnc_eth.macaddr
202 .octet[1],
203 rd_vnc_eth.macaddr
204 .octet[2],
205 rd_vnc_eth.macaddr
206 .octet[3],
207 rd_vnc_eth.macaddr
208 .octet[4],
209 rd_vnc_eth.macaddr
210 .octet[5]);
784d3a42
PG
211#endif
212
c4936a1a 213 vty_out(vty, "\n");
d62a17ae 214 }
c4936a1a
DS
215 rd_header = 0;
216 }
dc387b0f
LK
217 route_vty_out_tmp(vty, &rm->p, attr,
218 safi, use_json,
219 json_routes);
220 output_count++;
d62a17ae 221 }
dc387b0f
LK
222
223 if (use_json)
224 json_object_object_add(json_adv, rd_str, json_routes);
d62a17ae 225 }
dc387b0f 226
d62a17ae 227 if (use_json) {
dc387b0f
LK
228 json_object_object_add(json, "advertisedRoutes", json_adv);
229 json_object_int_add(json,
230 "totalPrefixCounter", output_count);
9d303b37
DL
231 vty_out(vty, "%s\n", json_object_to_json_string_ext(
232 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 233 json_object_free(json);
dc387b0f
LK
234 } else
235 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
236
d62a17ae 237 return CMD_SUCCESS;
784d3a42 238}