2 * Copyright (C) 2018 6WIND
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
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.
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
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"
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
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"},
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"},
72 #define FS_STRING_UPDATE(count, ptr, format, remaining_len) do { \
75 if (((format) == NLRI_STRING_FORMAT_DEBUG) && (count)) {\
76 _len_written = snprintf((ptr), (remaining_len), \
78 (remaining_len) -= _len_written; \
79 (ptr) += _len_written; \
80 } else if (((format) == NLRI_STRING_FORMAT_MIN) \
82 _len_written = snprintf((ptr), (remaining_len), \
84 (remaining_len) -= _len_written; \
85 (ptr) += _len_written; \
90 /* Parse FLOWSPEC NLRI
91 * passed return_string string has assumed length
92 * BGP_FLOWSPEC_STRING_DISPLAY_MAX
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
,
101 int ret
= 0, error
= 0;
102 char *ptr
= return_string
;
103 char local_string
[BGP_FLOWSPEC_STRING_DISPLAY_MAX
];
106 char pre_extra
[2] = "";
107 const struct message
*bgp_flowspec_display
;
108 enum bgp_flowspec_util_nlri_t type_util
;
109 int len_string
= BGP_FLOWSPEC_STRING_DISPLAY_MAX
;
112 if (format
== NLRI_STRING_FORMAT_LARGE
) {
113 snprintf(pre_extra
, sizeof(pre_extra
), "\t");
114 snprintf(extra
, sizeof(extra
), "\n");
115 bgp_flowspec_display
= bgp_flowspec_display_large
;
117 bgp_flowspec_display
= bgp_flowspec_display_min
;
118 /* if needed. type_util can be set to other values */
119 type_util
= BGP_FLOWSPEC_RETURN_STRING
;
121 while (offset
< len
-1 && error
>= 0) {
122 type
= nlri_content
[offset
];
125 case FLOWSPEC_DEST_PREFIX
:
126 case FLOWSPEC_SRC_PREFIX
:
127 ret
= bgp_flowspec_ip_address(
131 local_string
, &error
,
136 json_object_string_add(json_path
,
137 lookup_msg(bgp_flowspec_display
, type
, ""),
141 FS_STRING_UPDATE(count
, ptr
, format
, len_string
);
142 len_written
= snprintf(ptr
, len_string
, "%s%s %s%s",
144 lookup_msg(bgp_flowspec_display
,
146 local_string
, extra
);
147 len_string
-= len_written
;
150 case FLOWSPEC_IP_PROTOCOL
:
152 case FLOWSPEC_DEST_PORT
:
153 case FLOWSPEC_SRC_PORT
:
154 case FLOWSPEC_ICMP_TYPE
:
155 case FLOWSPEC_ICMP_CODE
:
156 ret
= bgp_flowspec_op_decode(type_util
,
159 local_string
, &error
);
163 json_object_string_add(json_path
,
164 lookup_msg(bgp_flowspec_display
, type
, ""),
168 FS_STRING_UPDATE(count
, ptr
, format
, len_string
);
169 len_written
= snprintf(ptr
, len_string
, "%s%s %s%s",
171 lookup_msg(bgp_flowspec_display
,
173 local_string
, extra
);
174 len_string
-= len_written
;
177 case FLOWSPEC_TCP_FLAGS
:
178 ret
= bgp_flowspec_bitmask_decode(
182 local_string
, &error
);
186 json_object_string_add(json_path
,
187 lookup_msg(bgp_flowspec_display
,
192 FS_STRING_UPDATE(count
, ptr
, format
, len_string
);
193 len_written
= snprintf(ptr
, len_string
, "%s%s %s%s",
195 lookup_msg(bgp_flowspec_display
,
197 local_string
, extra
);
198 len_string
-= len_written
;
201 case FLOWSPEC_PKT_LEN
:
203 ret
= bgp_flowspec_op_decode(
205 nlri_content
+ offset
,
206 len
- offset
, local_string
,
211 json_object_string_add(json_path
,
212 lookup_msg(bgp_flowspec_display
, type
, ""),
216 FS_STRING_UPDATE(count
, ptr
, format
, len_string
);
217 len_written
= snprintf(ptr
, len_string
, "%s%s %s%s",
219 lookup_msg(bgp_flowspec_display
,
221 local_string
, extra
);
222 len_string
-= len_written
;
225 case FLOWSPEC_FRAGMENT
:
226 ret
= bgp_flowspec_bitmask_decode(
230 local_string
, &error
);
234 json_object_string_add(json_path
,
235 lookup_msg(bgp_flowspec_display
,
240 FS_STRING_UPDATE(count
, ptr
, format
, len_string
);
241 len_written
= snprintf(ptr
, len_string
, "%s%s %s%s",
243 lookup_msg(bgp_flowspec_display
,
245 local_string
, extra
);
246 len_string
-= len_written
;
257 void route_vty_out_flowspec(struct vty
*vty
, const struct prefix
*p
,
258 struct bgp_path_info
*path
, int display
,
259 json_object
*json_paths
)
262 char return_string
[BGP_FLOWSPEC_STRING_DISPLAY_MAX
];
264 json_object
*json_nlri_path
= NULL
;
265 json_object
*json_ecom_path
= NULL
;
266 json_object
*json_time_path
= NULL
;
267 char timebuf
[BGP_UPTIME_LEN
];
268 struct bgp_dest
*dest
= NULL
;
273 bgp_dest_get_bgp_table_info(dest
);
276 if (p
->family
!= AF_FLOWSPEC
)
279 if (display
== NLRI_STRING_FORMAT_JSON
)
280 json_nlri_path
= json_object_new_object();
282 json_nlri_path
= json_paths
;
284 if (display
== NLRI_STRING_FORMAT_LARGE
&& path
)
285 vty_out(vty
, "BGP flowspec entry: (flags 0x%x)\n",
287 bgp_fs_nlri_get_string((unsigned char *)
288 p
->u
.prefix_flowspec
.ptr
,
289 p
->u
.prefix_flowspec
.prefixlen
,
293 family2afi(p
->u
.prefix_flowspec
295 if (display
== NLRI_STRING_FORMAT_LARGE
)
296 vty_out(vty
, "%s", return_string
);
297 else if (display
== NLRI_STRING_FORMAT_DEBUG
)
298 vty_out(vty
, "%s", return_string
);
299 else if (display
== NLRI_STRING_FORMAT_MIN
)
300 vty_out(vty
, " %-30s", return_string
);
301 else if (json_paths
&& display
== NLRI_STRING_FORMAT_JSON
)
302 json_object_array_add(json_paths
, json_nlri_path
);
306 if (path
->attr
->ecommunity
) {
307 /* Print attribute */
309 s
= ecommunity_ecom2str(attr
->ecommunity
,
310 ECOMMUNITY_FORMAT_ROUTE_MAP
, 0);
313 if (display
== NLRI_STRING_FORMAT_LARGE
)
314 vty_out(vty
, "\t%s\n", s
);
315 else if (display
== NLRI_STRING_FORMAT_MIN
)
316 vty_out(vty
, "%s", s
);
317 else if (json_paths
) {
318 json_ecom_path
= json_object_new_object();
319 json_object_string_add(json_ecom_path
,
321 if (display
== NLRI_STRING_FORMAT_JSON
)
322 json_object_array_add(json_paths
,
325 if (attr
->nexthop
.s_addr
!= 0 &&
326 display
== NLRI_STRING_FORMAT_LARGE
)
327 vty_out(vty
, "\tNLRI NH %-16s\n",
328 inet_ntoa(attr
->nexthop
));
329 XFREE(MTYPE_ECOMMUNITY_STR
, s
);
331 peer_uptime(path
->uptime
, timebuf
, BGP_UPTIME_LEN
, 0, NULL
);
332 if (display
== NLRI_STRING_FORMAT_LARGE
) {
333 vty_out(vty
, "\treceived for %8s\n", timebuf
);
334 } else if (json_paths
) {
335 json_time_path
= json_object_new_object();
336 json_object_string_add(json_time_path
,
338 if (display
== NLRI_STRING_FORMAT_JSON
)
339 json_object_array_add(json_paths
, json_time_path
);
341 if (display
== NLRI_STRING_FORMAT_LARGE
) {
342 struct bgp_path_info_extra
*extra
=
343 bgp_path_info_extra_get(path
);
344 bool list_began
= false;
346 if (extra
->bgp_fs_pbr
&& listcount(extra
->bgp_fs_pbr
)) {
347 struct listnode
*node
;
348 struct bgp_pbr_match_entry
*bpme
;
349 struct bgp_pbr_match
*bpm
;
350 struct list
*list_bpm
;
352 list_bpm
= list_new();
353 vty_out(vty
, "\tinstalled in PBR");
354 for (ALL_LIST_ELEMENTS_RO(extra
->bgp_fs_pbr
,
356 bpm
= bpme
->backpointer
;
357 if (listnode_lookup(list_bpm
, bpm
))
359 listnode_add(list_bpm
, bpm
);
365 vty_out(vty
, "%s", bpm
->ipset_name
);
367 list_delete(&list_bpm
);
369 if (extra
->bgp_fs_iprule
&& listcount(extra
->bgp_fs_iprule
)) {
370 struct listnode
*node
;
371 struct bgp_pbr_rule
*bpr
;
374 vty_out(vty
, "\tinstalled in PBR");
375 for (ALL_LIST_ELEMENTS_RO(extra
->bgp_fs_iprule
,
384 vty_out(vty
, "-ipv4-rule %d action lookup %u-",
386 bpr
->action
->table_id
);
392 vty_out(vty
, "\tnot installed in PBR\n");
396 int bgp_show_table_flowspec(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
397 struct bgp_table
*table
, enum bgp_show_type type
,
398 void *output_arg
, bool use_json
, int is_last
,
399 unsigned long *output_cum
, unsigned long *total_cum
)
401 struct bgp_path_info
*pi
;
402 struct bgp_dest
*dest
;
403 unsigned long total_count
= 0;
404 json_object
*json_paths
= NULL
;
405 int display
= NLRI_STRING_FORMAT_LARGE
;
407 if (type
!= bgp_show_type_detail
)
410 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
411 pi
= bgp_dest_get_bgp_path_info(dest
);
415 json_paths
= json_object_new_array();
416 display
= NLRI_STRING_FORMAT_JSON
;
418 for (; pi
; pi
= pi
->next
) {
420 route_vty_out_flowspec(vty
, bgp_dest_get_prefix(dest
),
421 pi
, display
, json_paths
);
425 json_object_to_json_string_ext(
427 JSON_C_TO_STRING_PRETTY
));
428 json_object_free(json_paths
);
432 if (total_count
&& !use_json
)
434 "\nDisplayed %ld flowspec entries\n",
439 DEFUN (debug_bgp_flowspec
,
440 debug_bgp_flowspec_cmd
,
441 "debug bgp flowspec",
444 "BGP allow flowspec debugging entries\n")
446 if (vty
->node
== CONFIG_NODE
)
447 DEBUG_ON(flowspec
, FLOWSPEC
);
449 TERM_DEBUG_ON(flowspec
, FLOWSPEC
);
450 vty_out(vty
, "BGP flowspec debugging is on\n");
455 DEFUN (no_debug_bgp_flowspec
,
456 no_debug_bgp_flowspec_cmd
,
457 "no debug bgp flowspec",
461 "BGP allow flowspec debugging entries\n")
463 if (vty
->node
== CONFIG_NODE
)
464 DEBUG_OFF(flowspec
, FLOWSPEC
);
466 TERM_DEBUG_OFF(flowspec
, FLOWSPEC
);
467 vty_out(vty
, "BGP flowspec debugging is off\n");
472 int bgp_fs_config_write_pbr(struct vty
*vty
, struct bgp
*bgp
,
473 afi_t afi
, safi_t safi
)
475 struct bgp_pbr_interface
*pbr_if
;
476 bool declare_node
= false;
477 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
478 struct bgp_pbr_interface_head
*head
;
479 bool bgp_pbr_interface_any
;
481 if (!bgp_pbr_cfg
|| safi
!= SAFI_FLOWSPEC
|| afi
!= AFI_IP
)
483 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
484 bgp_pbr_interface_any
= bgp_pbr_cfg
->pbr_interface_any_ipv4
;
485 if (!RB_EMPTY(bgp_pbr_interface_head
, head
) ||
486 !bgp_pbr_interface_any
)
488 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
489 vty_out(vty
, " local-install %s\n", pbr_if
->name
);
491 return declare_node
? 1 : 0;
494 static int bgp_fs_local_install_interface(struct bgp
*bgp
,
495 const char *no
, const char *ifname
)
497 struct bgp_pbr_interface
*pbr_if
;
498 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
499 struct bgp_pbr_interface_head
*head
;
500 bool *bgp_pbr_interface_any
;
504 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
505 bgp_pbr_interface_any
= &(bgp_pbr_cfg
->pbr_interface_any_ipv4
);
508 if (*bgp_pbr_interface_any
) {
509 *bgp_pbr_interface_any
= false;
510 /* remove all other interface list */
511 bgp_pbr_reset(bgp
, AFI_IP
);
515 pbr_if
= bgp_pbr_interface_lookup(ifname
, head
);
518 RB_REMOVE(bgp_pbr_interface_head
, head
, pbr_if
);
522 pbr_if
= bgp_pbr_interface_lookup(ifname
, head
);
525 pbr_if
= XCALLOC(MTYPE_TMP
,
526 sizeof(struct bgp_pbr_interface
));
527 strlcpy(pbr_if
->name
, ifname
, INTERFACE_NAMSIZ
);
528 RB_INSERT(bgp_pbr_interface_head
, head
, pbr_if
);
529 *bgp_pbr_interface_any
= false;
532 if (!*bgp_pbr_interface_any
) {
533 /* remove all other interface list
535 bgp_pbr_reset(bgp
, AFI_IP
);
536 *bgp_pbr_interface_any
= true;
542 DEFUN (bgp_fs_local_install_ifname
,
543 bgp_fs_local_install_ifname_cmd
,
544 "[no] local-install INTERFACE",
546 "Apply local policy routing\n"
549 struct bgp
*bgp
= VTY_GET_CONTEXT(bgp
);
551 const char *no
= strmatch(argv
[0]->text
, "no") ? "no" : NULL
;
552 char *ifname
= argv_find(argv
, argc
, "INTERFACE", &idx
) ?
553 argv
[idx
]->arg
: NULL
;
555 return bgp_fs_local_install_interface(bgp
, no
, ifname
);
558 extern int bgp_flowspec_display_match_per_ip(afi_t afi
, struct bgp_table
*rib
,
559 struct prefix
*match
,
560 int prefix_check
, struct vty
*vty
,
562 json_object
*json_paths
)
564 struct bgp_dest
*dest
;
565 const struct prefix
*prefix
;
568 for (dest
= bgp_table_top(rib
); dest
; dest
= bgp_route_next(dest
)) {
569 prefix
= bgp_dest_get_prefix(dest
);
571 if (prefix
->family
!= AF_FLOWSPEC
)
574 if (bgp_flowspec_contains_prefix(prefix
, match
, prefix_check
)) {
575 route_vty_out_flowspec(
576 vty
, prefix
, bgp_dest_get_bgp_path_info(dest
),
577 use_json
? NLRI_STRING_FORMAT_JSON
578 : NLRI_STRING_FORMAT_LARGE
,
586 void bgp_flowspec_vty_init(void)
588 install_element(ENABLE_NODE
, &debug_bgp_flowspec_cmd
);
589 install_element(CONFIG_NODE
, &debug_bgp_flowspec_cmd
);
590 install_element(ENABLE_NODE
, &no_debug_bgp_flowspec_cmd
);
591 install_element(CONFIG_NODE
, &no_debug_bgp_flowspec_cmd
);
592 install_element(BGP_FLOWSPECV4_NODE
, &bgp_fs_local_install_ifname_cmd
);