]>
Commit | Line | Data |
---|---|---|
dba3c1d3 PG |
1 | /* BGP FlowSpec VTY |
2 | * Copyright (C) 2018 6WIND | |
3 | * | |
4 | * FRRouting is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by the | |
6 | * Free Software Foundation; either version 2, or (at your option) any | |
7 | * later version. | |
8 | * | |
9 | * FRRouting is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; see the file COPYING; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #include <zebra.h> | |
20 | #include "command.h" | |
21 | ||
22 | #include "bgpd/bgpd.h" | |
23 | #include "bgpd/bgp_table.h" | |
24 | #include "bgpd/bgp_attr.h" | |
25 | #include "bgpd/bgp_ecommunity.h" | |
26 | #include "bgpd/bgp_vty.h" | |
27 | #include "bgpd/bgp_route.h" | |
28 | #include "bgpd/bgp_aspath.h" | |
29 | #include "bgpd/bgp_flowspec.h" | |
30 | #include "bgpd/bgp_flowspec_util.h" | |
31 | #include "bgpd/bgp_flowspec_private.h" | |
268e1b9b | 32 | #include "bgpd/bgp_debug.h" |
b588b642 | 33 | #include "bgpd/bgp_pbr.h" |
dba3c1d3 PG |
34 | |
35 | /* Local Structures and variables declarations | |
36 | * This code block hosts the struct declared that host the flowspec rules | |
37 | * as well as some structure used to convert to stringx | |
38 | */ | |
39 | ||
40 | static const struct message bgp_flowspec_display_large[] = { | |
41 | {FLOWSPEC_DEST_PREFIX, "Destination Address"}, | |
42 | {FLOWSPEC_SRC_PREFIX, "Source Address"}, | |
43 | {FLOWSPEC_IP_PROTOCOL, "IP Protocol"}, | |
44 | {FLOWSPEC_PORT, "Port"}, | |
45 | {FLOWSPEC_DEST_PORT, "Destination Port"}, | |
46 | {FLOWSPEC_SRC_PORT, "Source Port"}, | |
47 | {FLOWSPEC_ICMP_TYPE, "ICMP Type"}, | |
48 | {FLOWSPEC_ICMP_CODE, "ICMP Code"}, | |
49 | {FLOWSPEC_TCP_FLAGS, "TCP Flags"}, | |
50 | {FLOWSPEC_PKT_LEN, "Packet Length"}, | |
51 | {FLOWSPEC_DSCP, "DSCP field"}, | |
52 | {FLOWSPEC_FRAGMENT, "Packet Fragment"}, | |
40881800 | 53 | {FLOWSPEC_FLOW_LABEL, "Packet Flow Label"}, |
dba3c1d3 PG |
54 | {0} |
55 | }; | |
56 | ||
57 | static const struct message bgp_flowspec_display_min[] = { | |
58 | {FLOWSPEC_DEST_PREFIX, "to"}, | |
59 | {FLOWSPEC_SRC_PREFIX, "from"}, | |
60 | {FLOWSPEC_IP_PROTOCOL, "proto"}, | |
61 | {FLOWSPEC_PORT, "port"}, | |
62 | {FLOWSPEC_DEST_PORT, "dstp"}, | |
63 | {FLOWSPEC_SRC_PORT, "srcp"}, | |
64 | {FLOWSPEC_ICMP_TYPE, "type"}, | |
65 | {FLOWSPEC_ICMP_CODE, "code"}, | |
01ffd28b | 66 | {FLOWSPEC_TCP_FLAGS, "tcp"}, |
dba3c1d3 PG |
67 | {FLOWSPEC_PKT_LEN, "pktlen"}, |
68 | {FLOWSPEC_DSCP, "dscp"}, | |
69 | {FLOWSPEC_FRAGMENT, "pktfrag"}, | |
40881800 | 70 | {FLOWSPEC_FLOW_LABEL, "flwlbl"}, |
dba3c1d3 PG |
71 | {0} |
72 | }; | |
73 | ||
362a06e3 PG |
74 | #define FS_STRING_UPDATE(count, ptr, format, remaining_len) do { \ |
75 | int _len_written; \ | |
76 | \ | |
77 | if (((format) == NLRI_STRING_FORMAT_DEBUG) && (count)) {\ | |
78 | _len_written = snprintf((ptr), (remaining_len), \ | |
79 | ", "); \ | |
80 | (remaining_len) -= _len_written; \ | |
81 | (ptr) += _len_written; \ | |
82 | } else if (((format) == NLRI_STRING_FORMAT_MIN) \ | |
83 | && (count)) { \ | |
84 | _len_written = snprintf((ptr), (remaining_len), \ | |
85 | " "); \ | |
86 | (remaining_len) -= _len_written; \ | |
87 | (ptr) += _len_written; \ | |
88 | } \ | |
89 | count++; \ | |
dba3c1d3 PG |
90 | } while (0) |
91 | ||
362a06e3 PG |
92 | /* Parse FLOWSPEC NLRI |
93 | * passed return_string string has assumed length | |
94 | * BGP_FLOWSPEC_STRING_DISPLAY_MAX | |
95 | */ | |
dba3c1d3 | 96 | void bgp_fs_nlri_get_string(unsigned char *nlri_content, size_t len, |
d33fc23b | 97 | char *return_string, int format, |
1840384b PG |
98 | json_object *json_path, |
99 | afi_t afi) | |
dba3c1d3 PG |
100 | { |
101 | uint32_t offset = 0; | |
102 | int type; | |
103 | int ret = 0, error = 0; | |
104 | char *ptr = return_string; | |
105 | char local_string[BGP_FLOWSPEC_STRING_DISPLAY_MAX]; | |
106 | int count = 0; | |
107 | char extra[2] = ""; | |
108 | char pre_extra[2] = ""; | |
109 | const struct message *bgp_flowspec_display; | |
d33fc23b | 110 | enum bgp_flowspec_util_nlri_t type_util; |
362a06e3 PG |
111 | int len_string = BGP_FLOWSPEC_STRING_DISPLAY_MAX; |
112 | int len_written; | |
dba3c1d3 PG |
113 | |
114 | if (format == NLRI_STRING_FORMAT_LARGE) { | |
115 | snprintf(pre_extra, sizeof(pre_extra), "\t"); | |
116 | snprintf(extra, sizeof(extra), "\n"); | |
117 | bgp_flowspec_display = bgp_flowspec_display_large; | |
118 | } else | |
119 | bgp_flowspec_display = bgp_flowspec_display_min; | |
d33fc23b PG |
120 | /* if needed. type_util can be set to other values */ |
121 | type_util = BGP_FLOWSPEC_RETURN_STRING; | |
dba3c1d3 PG |
122 | error = 0; |
123 | while (offset < len-1 && error >= 0) { | |
124 | type = nlri_content[offset]; | |
125 | offset++; | |
126 | switch (type) { | |
127 | case FLOWSPEC_DEST_PREFIX: | |
128 | case FLOWSPEC_SRC_PREFIX: | |
129 | ret = bgp_flowspec_ip_address( | |
d33fc23b | 130 | type_util, |
dba3c1d3 PG |
131 | nlri_content+offset, |
132 | len - offset, | |
1840384b | 133 | local_string, &error, |
9cec4121 | 134 | afi, NULL); |
dba3c1d3 PG |
135 | if (ret <= 0) |
136 | break; | |
d33fc23b PG |
137 | if (json_path) { |
138 | json_object_string_add(json_path, | |
139 | lookup_msg(bgp_flowspec_display, type, ""), | |
140 | local_string); | |
141 | break; | |
142 | } | |
362a06e3 PG |
143 | FS_STRING_UPDATE(count, ptr, format, len_string); |
144 | len_written = snprintf(ptr, len_string, "%s%s %s%s", | |
145 | pre_extra, | |
146 | lookup_msg(bgp_flowspec_display, | |
147 | type, ""), | |
148 | local_string, extra); | |
149 | len_string -= len_written; | |
150 | ptr += len_written; | |
dba3c1d3 | 151 | break; |
40881800 | 152 | case FLOWSPEC_FLOW_LABEL: |
dba3c1d3 PG |
153 | case FLOWSPEC_IP_PROTOCOL: |
154 | case FLOWSPEC_PORT: | |
155 | case FLOWSPEC_DEST_PORT: | |
156 | case FLOWSPEC_SRC_PORT: | |
157 | case FLOWSPEC_ICMP_TYPE: | |
158 | case FLOWSPEC_ICMP_CODE: | |
d33fc23b | 159 | ret = bgp_flowspec_op_decode(type_util, |
dba3c1d3 PG |
160 | nlri_content+offset, |
161 | len - offset, | |
162 | local_string, &error); | |
163 | if (ret <= 0) | |
164 | break; | |
d33fc23b PG |
165 | if (json_path) { |
166 | json_object_string_add(json_path, | |
167 | lookup_msg(bgp_flowspec_display, type, ""), | |
168 | local_string); | |
169 | break; | |
170 | } | |
362a06e3 PG |
171 | FS_STRING_UPDATE(count, ptr, format, len_string); |
172 | len_written = snprintf(ptr, len_string, "%s%s %s%s", | |
173 | pre_extra, | |
174 | lookup_msg(bgp_flowspec_display, | |
175 | type, ""), | |
dba3c1d3 | 176 | local_string, extra); |
362a06e3 PG |
177 | len_string -= len_written; |
178 | ptr += len_written; | |
dba3c1d3 PG |
179 | break; |
180 | case FLOWSPEC_TCP_FLAGS: | |
588ec356 | 181 | ret = bgp_flowspec_bitmask_decode( |
d33fc23b | 182 | type_util, |
dba3c1d3 PG |
183 | nlri_content+offset, |
184 | len - offset, | |
185 | local_string, &error); | |
186 | if (ret <= 0) | |
187 | break; | |
d33fc23b PG |
188 | if (json_path) { |
189 | json_object_string_add(json_path, | |
362a06e3 PG |
190 | lookup_msg(bgp_flowspec_display, |
191 | type, ""), | |
d33fc23b PG |
192 | local_string); |
193 | break; | |
194 | } | |
362a06e3 PG |
195 | FS_STRING_UPDATE(count, ptr, format, len_string); |
196 | len_written = snprintf(ptr, len_string, "%s%s %s%s", | |
197 | pre_extra, | |
198 | lookup_msg(bgp_flowspec_display, | |
199 | type, ""), | |
200 | local_string, extra); | |
201 | len_string -= len_written; | |
202 | ptr += len_written; | |
dba3c1d3 PG |
203 | break; |
204 | case FLOWSPEC_PKT_LEN: | |
205 | case FLOWSPEC_DSCP: | |
206 | ret = bgp_flowspec_op_decode( | |
d33fc23b | 207 | type_util, |
dba3c1d3 PG |
208 | nlri_content + offset, |
209 | len - offset, local_string, | |
210 | &error); | |
211 | if (ret <= 0) | |
212 | break; | |
d33fc23b PG |
213 | if (json_path) { |
214 | json_object_string_add(json_path, | |
215 | lookup_msg(bgp_flowspec_display, type, ""), | |
216 | local_string); | |
217 | break; | |
218 | } | |
362a06e3 PG |
219 | FS_STRING_UPDATE(count, ptr, format, len_string); |
220 | len_written = snprintf(ptr, len_string, "%s%s %s%s", | |
221 | pre_extra, | |
222 | lookup_msg(bgp_flowspec_display, | |
223 | type, ""), | |
dba3c1d3 | 224 | local_string, extra); |
362a06e3 PG |
225 | len_string -= len_written; |
226 | ptr += len_written; | |
dba3c1d3 PG |
227 | break; |
228 | case FLOWSPEC_FRAGMENT: | |
588ec356 PG |
229 | ret = bgp_flowspec_bitmask_decode( |
230 | type_util, | |
231 | nlri_content+offset, | |
232 | len - offset, | |
233 | local_string, &error); | |
dba3c1d3 PG |
234 | if (ret <= 0) |
235 | break; | |
d33fc23b PG |
236 | if (json_path) { |
237 | json_object_string_add(json_path, | |
238 | lookup_msg(bgp_flowspec_display, | |
239 | type, ""), | |
240 | local_string); | |
241 | break; | |
242 | } | |
362a06e3 PG |
243 | FS_STRING_UPDATE(count, ptr, format, len_string); |
244 | len_written = snprintf(ptr, len_string, "%s%s %s%s", | |
245 | pre_extra, | |
246 | lookup_msg(bgp_flowspec_display, | |
247 | type, ""), | |
248 | local_string, extra); | |
249 | len_string -= len_written; | |
250 | ptr += len_written; | |
dba3c1d3 PG |
251 | break; |
252 | default: | |
253 | error = -1; | |
254 | break; | |
255 | } | |
256 | offset += ret; | |
257 | } | |
258 | } | |
259 | ||
bd494ec5 | 260 | void route_vty_out_flowspec(struct vty *vty, const struct prefix *p, |
9b6d8fcf | 261 | struct bgp_path_info *path, int display, |
4b7e6066 | 262 | json_object *json_paths) |
dba3c1d3 PG |
263 | { |
264 | struct attr *attr; | |
265 | char return_string[BGP_FLOWSPEC_STRING_DISPLAY_MAX]; | |
c6423c31 | 266 | char *s1 = NULL, *s2 = NULL; |
d33fc23b PG |
267 | json_object *json_nlri_path = NULL; |
268 | json_object *json_ecom_path = NULL; | |
269 | json_object *json_time_path = NULL; | |
270 | char timebuf[BGP_UPTIME_LEN]; | |
1840384b | 271 | struct bgp_dest *dest = NULL; |
dba3c1d3 | 272 | |
1840384b PG |
273 | if (path) |
274 | dest = path->net; | |
275 | if (dest) | |
276 | bgp_dest_get_bgp_table_info(dest); | |
dba3c1d3 PG |
277 | /* Print prefix */ |
278 | if (p != NULL) { | |
279 | if (p->family != AF_FLOWSPEC) | |
280 | return; | |
d33fc23b PG |
281 | if (json_paths) { |
282 | if (display == NLRI_STRING_FORMAT_JSON) | |
283 | json_nlri_path = json_object_new_object(); | |
284 | else | |
285 | json_nlri_path = json_paths; | |
286 | } | |
9b6d8fcf | 287 | if (display == NLRI_STRING_FORMAT_LARGE && path) |
dba3c1d3 | 288 | vty_out(vty, "BGP flowspec entry: (flags 0x%x)\n", |
9b6d8fcf | 289 | path->flags); |
dba3c1d3 PG |
290 | bgp_fs_nlri_get_string((unsigned char *) |
291 | p->u.prefix_flowspec.ptr, | |
292 | p->u.prefix_flowspec.prefixlen, | |
293 | return_string, | |
d33fc23b | 294 | display, |
1840384b PG |
295 | json_nlri_path, |
296 | family2afi(p->u.prefix_flowspec | |
297 | .family)); | |
dba3c1d3 PG |
298 | if (display == NLRI_STRING_FORMAT_LARGE) |
299 | vty_out(vty, "%s", return_string); | |
300 | else if (display == NLRI_STRING_FORMAT_DEBUG) | |
301 | vty_out(vty, "%s", return_string); | |
d33fc23b | 302 | else if (display == NLRI_STRING_FORMAT_MIN) |
dba3c1d3 | 303 | vty_out(vty, " %-30s", return_string); |
d33fc23b PG |
304 | else if (json_paths && display == NLRI_STRING_FORMAT_JSON) |
305 | json_object_array_add(json_paths, json_nlri_path); | |
dba3c1d3 | 306 | } |
9b6d8fcf | 307 | if (!path) |
dba3c1d3 | 308 | return; |
9a659715 PG |
309 | if (path->attr && |
310 | (path->attr->ecommunity || path->attr->ipv6_ecommunity)) { | |
dba3c1d3 | 311 | /* Print attribute */ |
9b6d8fcf | 312 | attr = path->attr; |
9a659715 | 313 | if (attr->ecommunity) |
c6423c31 | 314 | s1 = ecommunity_ecom2str(attr->ecommunity, |
9a659715 PG |
315 | ECOMMUNITY_FORMAT_ROUTE_MAP, 0); |
316 | if (attr->ipv6_ecommunity) | |
317 | s2 = ecommunity_ecom2str(attr->ipv6_ecommunity, | |
318 | ECOMMUNITY_FORMAT_ROUTE_MAP, 0); | |
c6423c31 | 319 | if (!s1 && !s2) |
dba3c1d3 PG |
320 | return; |
321 | if (display == NLRI_STRING_FORMAT_LARGE) | |
c6423c31 PG |
322 | vty_out(vty, "\t%s%s%s\n", s1 ? s1 : "", |
323 | s2 && s1 ? " " : "", s2 ? s2 : ""); | |
d33fc23b | 324 | else if (display == NLRI_STRING_FORMAT_MIN) |
c6423c31 | 325 | vty_out(vty, "%s%s", s1 ? s1 : "", s2 ? s2 : ""); |
d33fc23b PG |
326 | else if (json_paths) { |
327 | json_ecom_path = json_object_new_object(); | |
c6423c31 | 328 | if (s1) |
9a659715 | 329 | json_object_string_add(json_ecom_path, |
c6423c31 | 330 | "ecomlist", s1); |
9a659715 PG |
331 | if (s2) |
332 | json_object_string_add(json_ecom_path, | |
333 | "ecom6list", s2); | |
d33fc23b PG |
334 | if (display == NLRI_STRING_FORMAT_JSON) |
335 | json_object_array_add(json_paths, | |
336 | json_ecom_path); | |
337 | } | |
f01e580f PG |
338 | if (display == NLRI_STRING_FORMAT_LARGE) { |
339 | char local_buff[INET6_ADDRSTRLEN]; | |
340 | ||
341 | local_buff[0] = '\0'; | |
342 | if (p->u.prefix_flowspec.family == AF_INET && | |
343 | attr->nexthop.s_addr != 0) | |
344 | inet_ntop(AF_INET, | |
345 | &attr->nexthop.s_addr, | |
346 | local_buff, | |
347 | INET6_ADDRSTRLEN); | |
348 | else if (p->u.prefix_flowspec.family == AF_INET6 && | |
349 | attr->mp_nexthop_len != 0 && | |
350 | attr->mp_nexthop_len != BGP_ATTR_NHLEN_IPV4 && | |
351 | attr->mp_nexthop_len != BGP_ATTR_NHLEN_VPNV4) | |
352 | inet_ntop(AF_INET6, | |
353 | &attr->mp_nexthop_global, | |
354 | local_buff, | |
355 | INET6_ADDRSTRLEN); | |
356 | if (local_buff[0] != '\0') | |
357 | vty_out(vty, "\tNLRI NH %s\n", | |
358 | local_buff); | |
359 | } | |
c6423c31 PG |
360 | XFREE(MTYPE_ECOMMUNITY_STR, s1); |
361 | XFREE(MTYPE_ECOMMUNITY_STR, s2); | |
dba3c1d3 | 362 | } |
9b6d8fcf | 363 | peer_uptime(path->uptime, timebuf, BGP_UPTIME_LEN, 0, NULL); |
b588b642 PG |
364 | if (display == NLRI_STRING_FORMAT_LARGE) { |
365 | vty_out(vty, "\treceived for %8s\n", timebuf); | |
366 | } else if (json_paths) { | |
d33fc23b PG |
367 | json_time_path = json_object_new_object(); |
368 | json_object_string_add(json_time_path, | |
369 | "time", timebuf); | |
370 | if (display == NLRI_STRING_FORMAT_JSON) | |
371 | json_object_array_add(json_paths, json_time_path); | |
dba3c1d3 | 372 | } |
b588b642 | 373 | if (display == NLRI_STRING_FORMAT_LARGE) { |
18ee8310 | 374 | struct bgp_path_info_extra *extra = |
9b6d8fcf | 375 | bgp_path_info_extra_get(path); |
3e3708cb | 376 | bool list_began = false; |
dba3c1d3 | 377 | |
3e3708cb | 378 | if (extra->bgp_fs_pbr && listcount(extra->bgp_fs_pbr)) { |
c26edcda | 379 | struct listnode *node; |
b588b642 PG |
380 | struct bgp_pbr_match_entry *bpme; |
381 | struct bgp_pbr_match *bpm; | |
c26edcda | 382 | struct list *list_bpm; |
b588b642 | 383 | |
c26edcda | 384 | list_bpm = list_new(); |
ce3c0614 | 385 | vty_out(vty, "\tinstalled in PBR"); |
c26edcda PG |
386 | for (ALL_LIST_ELEMENTS_RO(extra->bgp_fs_pbr, |
387 | node, bpme)) { | |
388 | bpm = bpme->backpointer; | |
389 | if (listnode_lookup(list_bpm, bpm)) | |
390 | continue; | |
391 | listnode_add(list_bpm, bpm); | |
503d1ec6 | 392 | if (!list_began) { |
c26edcda | 393 | vty_out(vty, " ("); |
503d1ec6 PG |
394 | list_began = true; |
395 | } else | |
c26edcda PG |
396 | vty_out(vty, ", "); |
397 | vty_out(vty, "%s", bpm->ipset_name); | |
c26edcda | 398 | } |
3e3708cb PG |
399 | list_delete(&list_bpm); |
400 | } | |
401 | if (extra->bgp_fs_iprule && listcount(extra->bgp_fs_iprule)) { | |
402 | struct listnode *node; | |
403 | struct bgp_pbr_rule *bpr; | |
404 | ||
405 | if (!list_began) | |
406 | vty_out(vty, "\tinstalled in PBR"); | |
ce3c0614 PG |
407 | for (ALL_LIST_ELEMENTS_RO(extra->bgp_fs_iprule, |
408 | node, bpr)) { | |
409 | if (!bpr->action) | |
410 | continue; | |
411 | if (!list_began) { | |
412 | vty_out(vty, " ("); | |
413 | list_began = true; | |
414 | } else | |
415 | vty_out(vty, ", "); | |
416 | vty_out(vty, "-ipv4-rule %d action lookup %u-", | |
417 | bpr->priority, | |
418 | bpr->action->table_id); | |
419 | } | |
3e3708cb | 420 | } |
026b0e3b PG |
421 | if (list_began) |
422 | vty_out(vty, ")\n"); | |
423 | else | |
b588b642 PG |
424 | vty_out(vty, "\tnot installed in PBR\n"); |
425 | } | |
dba3c1d3 PG |
426 | } |
427 | ||
428 | int bgp_show_table_flowspec(struct vty *vty, struct bgp *bgp, afi_t afi, | |
429 | struct bgp_table *table, enum bgp_show_type type, | |
088f1098 DS |
430 | void *output_arg, bool use_json, int is_last, |
431 | unsigned long *output_cum, unsigned long *total_cum) | |
dba3c1d3 | 432 | { |
40381db7 | 433 | struct bgp_path_info *pi; |
9bcb3eef | 434 | struct bgp_dest *dest; |
dba3c1d3 PG |
435 | unsigned long total_count = 0; |
436 | json_object *json_paths = NULL; | |
d33fc23b | 437 | int display = NLRI_STRING_FORMAT_LARGE; |
dba3c1d3 PG |
438 | |
439 | if (type != bgp_show_type_detail) | |
440 | return CMD_SUCCESS; | |
441 | ||
9bcb3eef DS |
442 | for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) { |
443 | pi = bgp_dest_get_bgp_path_info(dest); | |
6f94b685 | 444 | if (pi == NULL) |
dba3c1d3 | 445 | continue; |
d33fc23b PG |
446 | if (use_json) { |
447 | json_paths = json_object_new_array(); | |
448 | display = NLRI_STRING_FORMAT_JSON; | |
449 | } | |
6f94b685 | 450 | for (; pi; pi = pi->next) { |
dba3c1d3 | 451 | total_count++; |
9bcb3eef DS |
452 | route_vty_out_flowspec(vty, bgp_dest_get_prefix(dest), |
453 | pi, display, json_paths); | |
dba3c1d3 | 454 | } |
d33fc23b PG |
455 | if (use_json) { |
456 | vty_out(vty, "%s\n", | |
457 | json_object_to_json_string_ext( | |
458 | json_paths, | |
459 | JSON_C_TO_STRING_PRETTY)); | |
460 | json_object_free(json_paths); | |
461 | json_paths = NULL; | |
462 | } | |
dba3c1d3 | 463 | } |
d33fc23b | 464 | if (total_count && !use_json) |
dba3c1d3 PG |
465 | vty_out(vty, |
466 | "\nDisplayed %ld flowspec entries\n", | |
467 | total_count); | |
468 | return CMD_SUCCESS; | |
469 | } | |
470 | ||
268e1b9b PG |
471 | DEFUN (debug_bgp_flowspec, |
472 | debug_bgp_flowspec_cmd, | |
473 | "debug bgp flowspec", | |
474 | DEBUG_STR | |
475 | BGP_STR | |
476 | "BGP allow flowspec debugging entries\n") | |
477 | { | |
478 | if (vty->node == CONFIG_NODE) | |
479 | DEBUG_ON(flowspec, FLOWSPEC); | |
480 | else { | |
481 | TERM_DEBUG_ON(flowspec, FLOWSPEC); | |
482 | vty_out(vty, "BGP flowspec debugging is on\n"); | |
483 | } | |
484 | return CMD_SUCCESS; | |
485 | } | |
486 | ||
487 | DEFUN (no_debug_bgp_flowspec, | |
488 | no_debug_bgp_flowspec_cmd, | |
489 | "no debug bgp flowspec", | |
490 | NO_STR | |
491 | DEBUG_STR | |
492 | BGP_STR | |
493 | "BGP allow flowspec debugging entries\n") | |
494 | { | |
495 | if (vty->node == CONFIG_NODE) | |
496 | DEBUG_OFF(flowspec, FLOWSPEC); | |
497 | else { | |
498 | TERM_DEBUG_OFF(flowspec, FLOWSPEC); | |
499 | vty_out(vty, "BGP flowspec debugging is off\n"); | |
500 | } | |
501 | return CMD_SUCCESS; | |
502 | } | |
503 | ||
4762c213 PG |
504 | int bgp_fs_config_write_pbr(struct vty *vty, struct bgp *bgp, |
505 | afi_t afi, safi_t safi) | |
506 | { | |
507 | struct bgp_pbr_interface *pbr_if; | |
508 | bool declare_node = false; | |
509 | struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg; | |
510 | struct bgp_pbr_interface_head *head; | |
511 | bool bgp_pbr_interface_any; | |
512 | ||
8f242187 | 513 | if (!bgp_pbr_cfg || safi != SAFI_FLOWSPEC) |
4762c213 | 514 | return 0; |
8f242187 PG |
515 | if (afi == AFI_IP) { |
516 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv4); | |
517 | bgp_pbr_interface_any = bgp_pbr_cfg->pbr_interface_any_ipv4; | |
518 | } else if (afi == AFI_IP6) { | |
519 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv6); | |
520 | bgp_pbr_interface_any = bgp_pbr_cfg->pbr_interface_any_ipv6; | |
521 | } else { | |
522 | return 0; | |
523 | } | |
4762c213 PG |
524 | if (!RB_EMPTY(bgp_pbr_interface_head, head) || |
525 | !bgp_pbr_interface_any) | |
526 | declare_node = true; | |
527 | RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) { | |
528 | vty_out(vty, " local-install %s\n", pbr_if->name); | |
529 | } | |
4762c213 PG |
530 | return declare_node ? 1 : 0; |
531 | } | |
532 | ||
533 | static int bgp_fs_local_install_interface(struct bgp *bgp, | |
8f242187 PG |
534 | const char *no, const char *ifname, |
535 | afi_t afi) | |
4762c213 PG |
536 | { |
537 | struct bgp_pbr_interface *pbr_if; | |
538 | struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg; | |
539 | struct bgp_pbr_interface_head *head; | |
540 | bool *bgp_pbr_interface_any; | |
541 | ||
542 | if (!bgp_pbr_cfg) | |
543 | return CMD_SUCCESS; | |
8f242187 PG |
544 | if (afi == AFI_IP) { |
545 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv4); | |
546 | bgp_pbr_interface_any = &(bgp_pbr_cfg->pbr_interface_any_ipv4); | |
547 | } else { | |
548 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv6); | |
549 | bgp_pbr_interface_any = &(bgp_pbr_cfg->pbr_interface_any_ipv6); | |
550 | } | |
4762c213 PG |
551 | if (no) { |
552 | if (!ifname) { | |
553 | if (*bgp_pbr_interface_any) { | |
554 | *bgp_pbr_interface_any = false; | |
555 | /* remove all other interface list */ | |
8f242187 | 556 | bgp_pbr_reset(bgp, afi); |
4762c213 PG |
557 | } |
558 | return CMD_SUCCESS; | |
559 | } | |
560 | pbr_if = bgp_pbr_interface_lookup(ifname, head); | |
561 | if (!pbr_if) | |
562 | return CMD_SUCCESS; | |
563 | RB_REMOVE(bgp_pbr_interface_head, head, pbr_if); | |
564 | return CMD_SUCCESS; | |
565 | } | |
566 | if (ifname) { | |
567 | pbr_if = bgp_pbr_interface_lookup(ifname, head); | |
568 | if (pbr_if) | |
569 | return CMD_SUCCESS; | |
570 | pbr_if = XCALLOC(MTYPE_TMP, | |
571 | sizeof(struct bgp_pbr_interface)); | |
572 | strlcpy(pbr_if->name, ifname, INTERFACE_NAMSIZ); | |
573 | RB_INSERT(bgp_pbr_interface_head, head, pbr_if); | |
574 | *bgp_pbr_interface_any = false; | |
575 | } else { | |
576 | /* set to default */ | |
577 | if (!*bgp_pbr_interface_any) { | |
578 | /* remove all other interface list | |
579 | */ | |
8f242187 | 580 | bgp_pbr_reset(bgp, afi); |
4762c213 PG |
581 | *bgp_pbr_interface_any = true; |
582 | } | |
583 | } | |
584 | return CMD_SUCCESS; | |
585 | } | |
586 | ||
587 | DEFUN (bgp_fs_local_install_ifname, | |
588 | bgp_fs_local_install_ifname_cmd, | |
589 | "[no] local-install INTERFACE", | |
590 | NO_STR | |
591 | "Apply local policy routing\n" | |
592 | "Interface name\n") | |
593 | { | |
594 | struct bgp *bgp = VTY_GET_CONTEXT(bgp); | |
595 | int idx = 0; | |
36de6e0e | 596 | const char *no = strmatch(argv[0]->text, "no") ? "no" : NULL; |
4762c213 PG |
597 | char *ifname = argv_find(argv, argc, "INTERFACE", &idx) ? |
598 | argv[idx]->arg : NULL; | |
599 | ||
8f242187 PG |
600 | return bgp_fs_local_install_interface(bgp, no, ifname, |
601 | bgp_node_afi(vty)); | |
4762c213 PG |
602 | } |
603 | ||
088f1098 DS |
604 | extern int bgp_flowspec_display_match_per_ip(afi_t afi, struct bgp_table *rib, |
605 | struct prefix *match, | |
606 | int prefix_check, struct vty *vty, | |
607 | bool use_json, | |
608 | json_object *json_paths) | |
63a0b7a9 | 609 | { |
9bcb3eef | 610 | struct bgp_dest *dest; |
b54892e0 | 611 | const struct prefix *prefix; |
63a0b7a9 PG |
612 | int display = 0; |
613 | ||
9bcb3eef DS |
614 | for (dest = bgp_table_top(rib); dest; dest = bgp_route_next(dest)) { |
615 | prefix = bgp_dest_get_prefix(dest); | |
63a0b7a9 PG |
616 | |
617 | if (prefix->family != AF_FLOWSPEC) | |
618 | continue; | |
619 | ||
620 | if (bgp_flowspec_contains_prefix(prefix, match, prefix_check)) { | |
6f94b685 | 621 | route_vty_out_flowspec( |
9bcb3eef | 622 | vty, prefix, bgp_dest_get_bgp_path_info(dest), |
6f94b685 DS |
623 | use_json ? NLRI_STRING_FORMAT_JSON |
624 | : NLRI_STRING_FORMAT_LARGE, | |
625 | json_paths); | |
63a0b7a9 PG |
626 | display++; |
627 | } | |
628 | } | |
629 | return display; | |
630 | } | |
631 | ||
dba3c1d3 PG |
632 | void bgp_flowspec_vty_init(void) |
633 | { | |
268e1b9b PG |
634 | install_element(ENABLE_NODE, &debug_bgp_flowspec_cmd); |
635 | install_element(CONFIG_NODE, &debug_bgp_flowspec_cmd); | |
636 | install_element(ENABLE_NODE, &no_debug_bgp_flowspec_cmd); | |
637 | install_element(CONFIG_NODE, &no_debug_bgp_flowspec_cmd); | |
4762c213 | 638 | install_element(BGP_FLOWSPECV4_NODE, &bgp_fs_local_install_ifname_cmd); |
8f242187 | 639 | install_element(BGP_FLOWSPECV6_NODE, &bgp_fs_local_install_ifname_cmd); |
dba3c1d3 | 640 | } |