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