]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_flowspec_vty.c
bgpd: Adding BGP GR Global & Per Neighbour FSM changes
[mirror_frr.git] / bgpd / bgp_flowspec_vty.c
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"
32 #include "bgpd/bgp_debug.h"
33 #include "bgpd/bgp_pbr.h"
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"},
53 {0}
54 };
55
56 static const struct message bgp_flowspec_display_min[] = {
57 {FLOWSPEC_DEST_PREFIX, "to"},
58 {FLOWSPEC_SRC_PREFIX, "from"},
59 {FLOWSPEC_IP_PROTOCOL, "proto"},
60 {FLOWSPEC_PORT, "port"},
61 {FLOWSPEC_DEST_PORT, "dstp"},
62 {FLOWSPEC_SRC_PORT, "srcp"},
63 {FLOWSPEC_ICMP_TYPE, "type"},
64 {FLOWSPEC_ICMP_CODE, "code"},
65 {FLOWSPEC_TCP_FLAGS, "tcp"},
66 {FLOWSPEC_PKT_LEN, "pktlen"},
67 {FLOWSPEC_DSCP, "dscp"},
68 {FLOWSPEC_FRAGMENT, "pktfrag"},
69 {0}
70 };
71
72 #define FS_STRING_UPDATE(count, ptr, format, remaining_len) do { \
73 int _len_written; \
74 \
75 if (((format) == NLRI_STRING_FORMAT_DEBUG) && (count)) {\
76 _len_written = snprintf((ptr), (remaining_len), \
77 ", "); \
78 (remaining_len) -= _len_written; \
79 (ptr) += _len_written; \
80 } else if (((format) == NLRI_STRING_FORMAT_MIN) \
81 && (count)) { \
82 _len_written = snprintf((ptr), (remaining_len), \
83 " "); \
84 (remaining_len) -= _len_written; \
85 (ptr) += _len_written; \
86 } \
87 count++; \
88 } while (0)
89
90 /* Parse FLOWSPEC NLRI
91 * passed return_string string has assumed length
92 * BGP_FLOWSPEC_STRING_DISPLAY_MAX
93 */
94 void bgp_fs_nlri_get_string(unsigned char *nlri_content, size_t len,
95 char *return_string, int format,
96 json_object *json_path)
97 {
98 uint32_t offset = 0;
99 int type;
100 int ret = 0, error = 0;
101 char *ptr = return_string;
102 char local_string[BGP_FLOWSPEC_STRING_DISPLAY_MAX];
103 int count = 0;
104 char extra[2] = "";
105 char pre_extra[2] = "";
106 const struct message *bgp_flowspec_display;
107 enum bgp_flowspec_util_nlri_t type_util;
108 int len_string = BGP_FLOWSPEC_STRING_DISPLAY_MAX;
109 int len_written;
110
111 if (format == NLRI_STRING_FORMAT_LARGE) {
112 snprintf(pre_extra, sizeof(pre_extra), "\t");
113 snprintf(extra, sizeof(extra), "\n");
114 bgp_flowspec_display = bgp_flowspec_display_large;
115 } else
116 bgp_flowspec_display = bgp_flowspec_display_min;
117 /* if needed. type_util can be set to other values */
118 type_util = BGP_FLOWSPEC_RETURN_STRING;
119 error = 0;
120 while (offset < len-1 && error >= 0) {
121 type = nlri_content[offset];
122 offset++;
123 switch (type) {
124 case FLOWSPEC_DEST_PREFIX:
125 case FLOWSPEC_SRC_PREFIX:
126 ret = bgp_flowspec_ip_address(
127 type_util,
128 nlri_content+offset,
129 len - offset,
130 local_string, &error);
131 if (ret <= 0)
132 break;
133 if (json_path) {
134 json_object_string_add(json_path,
135 lookup_msg(bgp_flowspec_display, type, ""),
136 local_string);
137 break;
138 }
139 FS_STRING_UPDATE(count, ptr, format, len_string);
140 len_written = snprintf(ptr, len_string, "%s%s %s%s",
141 pre_extra,
142 lookup_msg(bgp_flowspec_display,
143 type, ""),
144 local_string, extra);
145 len_string -= len_written;
146 ptr += len_written;
147 break;
148 case FLOWSPEC_IP_PROTOCOL:
149 case FLOWSPEC_PORT:
150 case FLOWSPEC_DEST_PORT:
151 case FLOWSPEC_SRC_PORT:
152 case FLOWSPEC_ICMP_TYPE:
153 case FLOWSPEC_ICMP_CODE:
154 ret = bgp_flowspec_op_decode(type_util,
155 nlri_content+offset,
156 len - offset,
157 local_string, &error);
158 if (ret <= 0)
159 break;
160 if (json_path) {
161 json_object_string_add(json_path,
162 lookup_msg(bgp_flowspec_display, type, ""),
163 local_string);
164 break;
165 }
166 FS_STRING_UPDATE(count, ptr, format, len_string);
167 len_written = snprintf(ptr, len_string, "%s%s %s%s",
168 pre_extra,
169 lookup_msg(bgp_flowspec_display,
170 type, ""),
171 local_string, extra);
172 len_string -= len_written;
173 ptr += len_written;
174 break;
175 case FLOWSPEC_TCP_FLAGS:
176 ret = bgp_flowspec_bitmask_decode(
177 type_util,
178 nlri_content+offset,
179 len - offset,
180 local_string, &error);
181 if (ret <= 0)
182 break;
183 if (json_path) {
184 json_object_string_add(json_path,
185 lookup_msg(bgp_flowspec_display,
186 type, ""),
187 local_string);
188 break;
189 }
190 FS_STRING_UPDATE(count, ptr, format, len_string);
191 len_written = snprintf(ptr, len_string, "%s%s %s%s",
192 pre_extra,
193 lookup_msg(bgp_flowspec_display,
194 type, ""),
195 local_string, extra);
196 len_string -= len_written;
197 ptr += len_written;
198 break;
199 case FLOWSPEC_PKT_LEN:
200 case FLOWSPEC_DSCP:
201 ret = bgp_flowspec_op_decode(
202 type_util,
203 nlri_content + offset,
204 len - offset, local_string,
205 &error);
206 if (ret <= 0)
207 break;
208 if (json_path) {
209 json_object_string_add(json_path,
210 lookup_msg(bgp_flowspec_display, type, ""),
211 local_string);
212 break;
213 }
214 FS_STRING_UPDATE(count, ptr, format, len_string);
215 len_written = snprintf(ptr, len_string, "%s%s %s%s",
216 pre_extra,
217 lookup_msg(bgp_flowspec_display,
218 type, ""),
219 local_string, extra);
220 len_string -= len_written;
221 ptr += len_written;
222 break;
223 case FLOWSPEC_FRAGMENT:
224 ret = bgp_flowspec_bitmask_decode(
225 type_util,
226 nlri_content+offset,
227 len - offset,
228 local_string, &error);
229 if (ret <= 0)
230 break;
231 if (json_path) {
232 json_object_string_add(json_path,
233 lookup_msg(bgp_flowspec_display,
234 type, ""),
235 local_string);
236 break;
237 }
238 FS_STRING_UPDATE(count, ptr, format, len_string);
239 len_written = snprintf(ptr, len_string, "%s%s %s%s",
240 pre_extra,
241 lookup_msg(bgp_flowspec_display,
242 type, ""),
243 local_string, extra);
244 len_string -= len_written;
245 ptr += len_written;
246 break;
247 default:
248 error = -1;
249 break;
250 }
251 offset += ret;
252 }
253 }
254
255 void route_vty_out_flowspec(struct vty *vty, struct prefix *p,
256 struct bgp_path_info *path, int display,
257 json_object *json_paths)
258 {
259 struct attr *attr;
260 char return_string[BGP_FLOWSPEC_STRING_DISPLAY_MAX];
261 char *s;
262 json_object *json_nlri_path = NULL;
263 json_object *json_ecom_path = NULL;
264 json_object *json_time_path = NULL;
265 char timebuf[BGP_UPTIME_LEN];
266
267 /* Print prefix */
268 if (p != NULL) {
269 if (p->family != AF_FLOWSPEC)
270 return;
271 if (json_paths) {
272 if (display == NLRI_STRING_FORMAT_JSON)
273 json_nlri_path = json_object_new_object();
274 else
275 json_nlri_path = json_paths;
276 }
277 if (display == NLRI_STRING_FORMAT_LARGE && path)
278 vty_out(vty, "BGP flowspec entry: (flags 0x%x)\n",
279 path->flags);
280 bgp_fs_nlri_get_string((unsigned char *)
281 p->u.prefix_flowspec.ptr,
282 p->u.prefix_flowspec.prefixlen,
283 return_string,
284 display,
285 json_nlri_path);
286 if (display == NLRI_STRING_FORMAT_LARGE)
287 vty_out(vty, "%s", return_string);
288 else if (display == NLRI_STRING_FORMAT_DEBUG)
289 vty_out(vty, "%s", return_string);
290 else if (display == NLRI_STRING_FORMAT_MIN)
291 vty_out(vty, " %-30s", return_string);
292 else if (json_paths && display == NLRI_STRING_FORMAT_JSON)
293 json_object_array_add(json_paths, json_nlri_path);
294 }
295 if (!path)
296 return;
297 if (path->attr->ecommunity) {
298 /* Print attribute */
299 attr = path->attr;
300 s = ecommunity_ecom2str(attr->ecommunity,
301 ECOMMUNITY_FORMAT_ROUTE_MAP, 0);
302 if (!s)
303 return;
304 if (display == NLRI_STRING_FORMAT_LARGE)
305 vty_out(vty, "\t%s\n", s);
306 else if (display == NLRI_STRING_FORMAT_MIN)
307 vty_out(vty, "%s", s);
308 else if (json_paths) {
309 json_ecom_path = json_object_new_object();
310 json_object_string_add(json_ecom_path,
311 "ecomlist", s);
312 if (display == NLRI_STRING_FORMAT_JSON)
313 json_object_array_add(json_paths,
314 json_ecom_path);
315 }
316 if (attr->nexthop.s_addr != 0 &&
317 display == NLRI_STRING_FORMAT_LARGE)
318 vty_out(vty, "\tNLRI NH %-16s\n",
319 inet_ntoa(attr->nexthop));
320 XFREE(MTYPE_ECOMMUNITY_STR, s);
321 }
322 peer_uptime(path->uptime, timebuf, BGP_UPTIME_LEN, 0, NULL);
323 if (display == NLRI_STRING_FORMAT_LARGE) {
324 vty_out(vty, "\treceived for %8s\n", timebuf);
325 } else if (json_paths) {
326 json_time_path = json_object_new_object();
327 json_object_string_add(json_time_path,
328 "time", timebuf);
329 if (display == NLRI_STRING_FORMAT_JSON)
330 json_object_array_add(json_paths, json_time_path);
331 }
332 if (display == NLRI_STRING_FORMAT_LARGE) {
333 struct bgp_path_info_extra *extra =
334 bgp_path_info_extra_get(path);
335 bool list_began = false;
336
337 if (extra->bgp_fs_pbr && listcount(extra->bgp_fs_pbr)) {
338 struct listnode *node;
339 struct bgp_pbr_match_entry *bpme;
340 struct bgp_pbr_match *bpm;
341 struct list *list_bpm;
342
343 list_bpm = list_new();
344 vty_out(vty, "\tinstalled in PBR");
345 for (ALL_LIST_ELEMENTS_RO(extra->bgp_fs_pbr,
346 node, bpme)) {
347 bpm = bpme->backpointer;
348 if (listnode_lookup(list_bpm, bpm))
349 continue;
350 listnode_add(list_bpm, bpm);
351 if (!list_began) {
352 vty_out(vty, " (");
353 list_began = true;
354 } else
355 vty_out(vty, ", ");
356 vty_out(vty, "%s", bpm->ipset_name);
357 }
358 list_delete(&list_bpm);
359 }
360 if (extra->bgp_fs_iprule && listcount(extra->bgp_fs_iprule)) {
361 struct listnode *node;
362 struct bgp_pbr_rule *bpr;
363
364 if (!list_began)
365 vty_out(vty, "\tinstalled in PBR");
366 for (ALL_LIST_ELEMENTS_RO(extra->bgp_fs_iprule,
367 node, bpr)) {
368 if (!bpr->action)
369 continue;
370 if (!list_began) {
371 vty_out(vty, " (");
372 list_began = true;
373 } else
374 vty_out(vty, ", ");
375 vty_out(vty, "-ipv4-rule %d action lookup %u-",
376 bpr->priority,
377 bpr->action->table_id);
378 }
379 if (list_began)
380 vty_out(vty, ")");
381 vty_out(vty, "\n");
382 }
383 if (!list_began)
384 vty_out(vty, "\tnot installed in PBR\n");
385 }
386 }
387
388 int bgp_show_table_flowspec(struct vty *vty, struct bgp *bgp, afi_t afi,
389 struct bgp_table *table, enum bgp_show_type type,
390 void *output_arg, bool use_json, int is_last,
391 unsigned long *output_cum, unsigned long *total_cum)
392 {
393 struct bgp_path_info *pi;
394 struct bgp_node *rn;
395 unsigned long total_count = 0;
396 json_object *json_paths = NULL;
397 int display = NLRI_STRING_FORMAT_LARGE;
398
399 if (type != bgp_show_type_detail)
400 return CMD_SUCCESS;
401
402 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
403 pi = bgp_node_get_bgp_path_info(rn);
404 if (pi == NULL)
405 continue;
406 if (use_json) {
407 json_paths = json_object_new_array();
408 display = NLRI_STRING_FORMAT_JSON;
409 }
410 for (; pi; pi = pi->next) {
411 total_count++;
412 route_vty_out_flowspec(vty, &rn->p, pi, display,
413 json_paths);
414 }
415 if (use_json) {
416 vty_out(vty, "%s\n",
417 json_object_to_json_string_ext(
418 json_paths,
419 JSON_C_TO_STRING_PRETTY));
420 json_object_free(json_paths);
421 json_paths = NULL;
422 }
423 }
424 if (total_count && !use_json)
425 vty_out(vty,
426 "\nDisplayed %ld flowspec entries\n",
427 total_count);
428 return CMD_SUCCESS;
429 }
430
431 DEFUN (debug_bgp_flowspec,
432 debug_bgp_flowspec_cmd,
433 "debug bgp flowspec",
434 DEBUG_STR
435 BGP_STR
436 "BGP allow flowspec debugging entries\n")
437 {
438 if (vty->node == CONFIG_NODE)
439 DEBUG_ON(flowspec, FLOWSPEC);
440 else {
441 TERM_DEBUG_ON(flowspec, FLOWSPEC);
442 vty_out(vty, "BGP flowspec debugging is on\n");
443 }
444 return CMD_SUCCESS;
445 }
446
447 DEFUN (no_debug_bgp_flowspec,
448 no_debug_bgp_flowspec_cmd,
449 "no debug bgp flowspec",
450 NO_STR
451 DEBUG_STR
452 BGP_STR
453 "BGP allow flowspec debugging entries\n")
454 {
455 if (vty->node == CONFIG_NODE)
456 DEBUG_OFF(flowspec, FLOWSPEC);
457 else {
458 TERM_DEBUG_OFF(flowspec, FLOWSPEC);
459 vty_out(vty, "BGP flowspec debugging is off\n");
460 }
461 return CMD_SUCCESS;
462 }
463
464 int bgp_fs_config_write_pbr(struct vty *vty, struct bgp *bgp,
465 afi_t afi, safi_t safi)
466 {
467 struct bgp_pbr_interface *pbr_if;
468 bool declare_node = false;
469 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
470 struct bgp_pbr_interface_head *head;
471 bool bgp_pbr_interface_any;
472
473 if (!bgp_pbr_cfg || safi != SAFI_FLOWSPEC || afi != AFI_IP)
474 return 0;
475 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
476 bgp_pbr_interface_any = bgp_pbr_cfg->pbr_interface_any_ipv4;
477 if (!RB_EMPTY(bgp_pbr_interface_head, head) ||
478 !bgp_pbr_interface_any)
479 declare_node = true;
480 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
481 vty_out(vty, " local-install %s\n", pbr_if->name);
482 }
483 return declare_node ? 1 : 0;
484 }
485
486 static int bgp_fs_local_install_interface(struct bgp *bgp,
487 const char *no, const char *ifname)
488 {
489 struct bgp_pbr_interface *pbr_if;
490 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
491 struct bgp_pbr_interface_head *head;
492 bool *bgp_pbr_interface_any;
493
494 if (!bgp_pbr_cfg)
495 return CMD_SUCCESS;
496 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
497 bgp_pbr_interface_any = &(bgp_pbr_cfg->pbr_interface_any_ipv4);
498 if (no) {
499 if (!ifname) {
500 if (*bgp_pbr_interface_any) {
501 *bgp_pbr_interface_any = false;
502 /* remove all other interface list */
503 bgp_pbr_reset(bgp, AFI_IP);
504 }
505 return CMD_SUCCESS;
506 }
507 pbr_if = bgp_pbr_interface_lookup(ifname, head);
508 if (!pbr_if)
509 return CMD_SUCCESS;
510 RB_REMOVE(bgp_pbr_interface_head, head, pbr_if);
511 return CMD_SUCCESS;
512 }
513 if (ifname) {
514 pbr_if = bgp_pbr_interface_lookup(ifname, head);
515 if (pbr_if)
516 return CMD_SUCCESS;
517 pbr_if = XCALLOC(MTYPE_TMP,
518 sizeof(struct bgp_pbr_interface));
519 strlcpy(pbr_if->name, ifname, INTERFACE_NAMSIZ);
520 RB_INSERT(bgp_pbr_interface_head, head, pbr_if);
521 *bgp_pbr_interface_any = false;
522 } else {
523 /* set to default */
524 if (!*bgp_pbr_interface_any) {
525 /* remove all other interface list
526 */
527 bgp_pbr_reset(bgp, AFI_IP);
528 *bgp_pbr_interface_any = true;
529 }
530 }
531 return CMD_SUCCESS;
532 }
533
534 DEFUN (bgp_fs_local_install_ifname,
535 bgp_fs_local_install_ifname_cmd,
536 "[no] local-install INTERFACE",
537 NO_STR
538 "Apply local policy routing\n"
539 "Interface name\n")
540 {
541 struct bgp *bgp = VTY_GET_CONTEXT(bgp);
542 int idx = 0;
543 const char *no = strmatch(argv[0]->text, "no") ? "no" : NULL;
544 char *ifname = argv_find(argv, argc, "INTERFACE", &idx) ?
545 argv[idx]->arg : NULL;
546
547 return bgp_fs_local_install_interface(bgp, no, ifname);
548 }
549
550 extern int bgp_flowspec_display_match_per_ip(afi_t afi, struct bgp_table *rib,
551 struct prefix *match,
552 int prefix_check, struct vty *vty,
553 bool use_json,
554 json_object *json_paths)
555 {
556 struct bgp_node *rn;
557 struct prefix *prefix;
558 int display = 0;
559
560 for (rn = bgp_table_top(rib); rn; rn = bgp_route_next(rn)) {
561 prefix = &rn->p;
562
563 if (prefix->family != AF_FLOWSPEC)
564 continue;
565
566 if (bgp_flowspec_contains_prefix(prefix, match, prefix_check)) {
567 route_vty_out_flowspec(
568 vty, &rn->p, bgp_node_get_bgp_path_info(rn),
569 use_json ? NLRI_STRING_FORMAT_JSON
570 : NLRI_STRING_FORMAT_LARGE,
571 json_paths);
572 display++;
573 }
574 }
575 return display;
576 }
577
578 void bgp_flowspec_vty_init(void)
579 {
580 install_element(ENABLE_NODE, &debug_bgp_flowspec_cmd);
581 install_element(CONFIG_NODE, &debug_bgp_flowspec_cmd);
582 install_element(ENABLE_NODE, &no_debug_bgp_flowspec_cmd);
583 install_element(CONFIG_NODE, &no_debug_bgp_flowspec_cmd);
584 install_element(BGP_FLOWSPECV4_NODE, &bgp_fs_local_install_ifname_cmd);
585 }