]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
bgpd: Additional check on presence of tag/label
[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
34 show_adj_route_vpn (struct vty *vty, struct peer *peer, struct prefix_rd *prd,
35 afi_t afi, safi_t safi, 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 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 {
53 if (!use_json)
54 vty_outln (vty, "No BGP process is configured");
55 return CMD_WARNING;
56 }
57
58 if (use_json)
59 {
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 {
80 if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
81 continue;
82
83 if ((table = rn->info) != NULL)
84 {
85 if (use_json)
86 json_array = json_object_new_array();
87 else
88 json_array = NULL;
89
90 rd_header = 1;
91
92 for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
93 {
94 if ((attr = rm->info) != NULL)
95 {
96 if (header)
97 {
98 if (use_json)
99 {
100 json_object_int_add(json, "bgpTableVersion", 0);
101 json_object_string_add(json, "bgpLocalRouterId", inet_ntoa (bgp->router_id));
102 json_object_object_add(json, "bgpStatusCodes", json_scode);
103 json_object_object_add(json, "bgpOriginCodes", json_ocode);
104 }
105 else
106 {
107 vty_outln (vty, "BGP table version is 0, local router ID is %s",
108 inet_ntoa(bgp->router_id));
109 vty_outln (vty,
110 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal");
111 vty_outln (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete%s",
112 VTYNL);
113 vty_outln (vty, V4_HEADER);
114 }
115 header = 0;
116 }
117
118 if (rd_header)
119 {
120 u_int16_t type;
121 struct rd_as rd_as;
122 struct rd_ip rd_ip = {0};
123 #if ENABLE_BGP_VNC
124 struct rd_vnc_eth rd_vnc_eth = {0};
125 #endif
126 u_char *pnt;
127
128 pnt = rn->p.u.val;
129
130 /* Decode RD type. */
131 type = decode_rd_type (pnt);
132 /* Decode RD value. */
133 if (type == RD_TYPE_AS)
134 decode_rd_as (pnt + 2, &rd_as);
135 else if (type == RD_TYPE_AS4)
136 decode_rd_as4 (pnt + 2, &rd_as);
137 else if (type == RD_TYPE_IP)
138 decode_rd_ip (pnt + 2, &rd_ip);
139 #if ENABLE_BGP_VNC
140 else if (type == RD_TYPE_VNC_ETH)
141 decode_rd_vnc_eth (pnt, &rd_vnc_eth);
142 #endif
143
144 if (use_json)
145 {
146 char buffer[BUFSIZ];
147 if (type == RD_TYPE_AS || type == RD_TYPE_AS4)
148 sprintf (buffer, "%u:%d", rd_as.as, rd_as.val);
149 else if (type == RD_TYPE_IP)
150 sprintf (buffer, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
151 json_object_string_add(json_routes, "routeDistinguisher", buffer);
152 }
153 else
154 {
155 vty_out (vty, "Route Distinguisher: ");
156
157 if (type == RD_TYPE_AS || type == RD_TYPE_AS4)
158 vty_out (vty, "%u:%d", rd_as.as, rd_as.val);
159 else if (type == RD_TYPE_IP)
160 vty_out (vty, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
161 #if ENABLE_BGP_VNC
162 else if (type == RD_TYPE_VNC_ETH)
163 vty_out (vty, "%u:%02x:%02x:%02x:%02x:%02x:%02x",
164 rd_vnc_eth.local_nve_id,
165 rd_vnc_eth.macaddr.octet[0],
166 rd_vnc_eth.macaddr.octet[1],
167 rd_vnc_eth.macaddr.octet[2],
168 rd_vnc_eth.macaddr.octet[3],
169 rd_vnc_eth.macaddr.octet[4],
170 rd_vnc_eth.macaddr.octet[5]);
171 #endif
172
173 vty_out (vty, VTYNL);
174 }
175 rd_header = 0;
176 }
177 route_vty_out_tmp (vty, &rm->p, attr, SAFI_MPLS_VPN, use_json, json_array);
178 }
179 }
180 if (use_json)
181 {
182 struct prefix *p;
183 char buf_a[BUFSIZ];
184 char buf_b[BUFSIZ];
185 p = &rm->p;
186 sprintf(buf_a, "%s/%d", inet_ntop (p->family, &p->u.prefix, buf_b, BUFSIZ), p->prefixlen);
187 json_object_object_add(json_routes, buf_a, json_array);
188 }
189 }
190 }
191 if (use_json)
192 {
193 json_object_object_add(json, "routes", json_routes);
194 vty_outln (vty, "%s",
195 json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
196 json_object_free(json);
197 }
198 return CMD_SUCCESS;
199 }
200