1 /*********************************************************************
2 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 * This program 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 Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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
18 * bfd.h: implements the BFD protocol.
24 #include <netinet/in.h>
31 #include "lib/libfrr.h"
33 #include "lib/queue.h"
37 #define ETHERNET_ADDRESS_LENGTH 6
40 #define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
42 #define BFDD_JSON_CONV_OPTIONS (0)
46 DECLARE_MTYPE(BFDD_TMP
);
47 DECLARE_MTYPE(BFDD_CONFIG
);
48 DECLARE_MTYPE(BFDD_LABEL
);
49 DECLARE_MTYPE(BFDD_CONTROL
);
50 DECLARE_MTYPE(BFDD_NOTIFICATION
);
53 uint32_t desired_min_tx
;
54 uint32_t required_min_rx
;
55 uint32_t required_min_echo
;
60 uint32_t remote_discr
;
64 * Format of control packet. From section 4)
76 struct bfd_discrs discrs
;
77 struct bfd_timers timers
;
81 * Format of Echo packet.
97 /* Macros for manipulating control packets */
98 #define BFD_VERMASK 0x03
99 #define BFD_DIAGMASK 0x1F
100 #define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
101 #define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
102 #define BFD_VERSION 1
103 #define BFD_PBIT 0x20
104 #define BFD_FBIT 0x10
105 #define BFD_CBIT 0x08
106 #define BFD_ABIT 0x04
107 #define BFD_DEMANDBIT 0x02
108 #define BFD_SETDEMANDBIT(flags, val) \
111 flags |= BFD_DEMANDBIT; \
113 #define BFD_SETPBIT(flags, val) \
118 #define BFD_GETPBIT(flags) (flags & BFD_PBIT)
119 #define BFD_SETFBIT(flags, val) \
124 #define BFD_GETFBIT(flags) (flags & BFD_FBIT)
125 #define BFD_SETSTATE(flags, val) \
128 flags |= (val & 0x3) << 6; \
130 #define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
131 #define BFD_ECHO_VERSION 1
132 #define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
134 enum bfd_diagnosticis
{
136 /* Control Detection Time Expired. */
137 BD_CONTROL_EXPIRED
= 1,
138 /* Echo Function Failed. */
140 /* Neighbor Signaled Session Down. */
141 BD_NEIGHBOR_DOWN
= 3,
142 /* Forwarding Plane Reset. */
143 BD_FORWARDING_RESET
= 4,
146 /* Concatenated Path Down. */
147 BD_CONCATPATH_DOWN
= 6,
148 /* Administratively Down. */
150 /* Reverse Concatenated Path Down. */
151 BD_REVCONCATPATH_DOWN
= 8,
152 /* 9..31: reserved. */
155 /* BFD session flags */
156 enum bfd_session_flags
{
157 BFD_SESS_FLAG_NONE
= 0,
158 BFD_SESS_FLAG_ECHO
= 1 << 0, /* BFD Echo functionality */
159 BFD_SESS_FLAG_ECHO_ACTIVE
= 1 << 1, /* BFD Echo Packets are being sent
162 BFD_SESS_FLAG_MH
= 1 << 2, /* BFD Multi-hop session */
163 BFD_SESS_FLAG_IPV6
= 1 << 4, /* BFD IPv6 session */
164 BFD_SESS_FLAG_SEND_EVT_ACTIVE
= 1 << 5, /* send event timer active */
165 BFD_SESS_FLAG_SEND_EVT_IGNORE
= 1 << 6, /* ignore send event when timer
168 BFD_SESS_FLAG_SHUTDOWN
= 1 << 7, /* disable BGP peer function */
171 #define BFD_SET_FLAG(field, flag) (field |= flag)
172 #define BFD_UNSET_FLAG(field, flag) (field &= ~flag)
173 #define BFD_CHECK_FLAG(field, flag) (field & flag)
175 /* BFD session hash keys */
176 struct bfd_shop_key
{
177 struct sockaddr_any peer
;
178 char port_name
[MAXNAMELEN
+ 1];
181 struct bfd_mhop_key
{
182 struct sockaddr_any peer
;
183 struct sockaddr_any local
;
184 char vrf_name
[MAXNAMELEN
+ 1];
187 struct bfd_session_stats
{
188 uint64_t rx_ctrl_pkt
;
189 uint64_t tx_ctrl_pkt
;
190 uint64_t rx_echo_pkt
;
191 uint64_t tx_echo_pkt
;
193 uint64_t session_down
;
194 uint64_t znotification
;
197 /* bfd_session shortcut label forwarding. */
201 * Session state information
205 /* protocol state per RFC 5880*/
207 struct bfd_discrs discrs
;
211 uint8_t remote_detect_mult
;
215 struct bfd_timers timers
;
216 struct bfd_timers new_timers
;
219 struct thread
*echo_recvtimer_ev
;
220 struct thread
*recvtimer_ev
;
222 uint64_t echo_xmt_TO
;
223 struct thread
*xmttimer_ev
;
224 struct thread
*echo_xmttimer_ev
;
225 uint64_t echo_detect_TO
;
227 /* software object state */
230 /* This and the localDiscr are the keys to state info */
231 struct peer_label
*pl
;
233 struct bfd_shop_key shop
;
234 struct bfd_mhop_key mhop
;
238 struct sockaddr_any local_address
;
239 struct sockaddr_any local_ip
;
241 uint8_t local_mac
[ETHERNET_ADDRESS_LENGTH
];
242 uint8_t peer_mac
[ETHERNET_ADDRESS_LENGTH
];
244 /* BFD session flags */
245 enum bfd_session_flags flags
;
247 struct bfd_session_stats stats
;
249 struct timeval uptime
; /* last up time */
250 struct timeval downtime
; /* last down time */
252 /* Remote peer data (for debugging mostly) */
254 struct bfd_timers remote_timers
;
256 uint64_t refcount
; /* number of pointers referencing this. */
258 /* VTY context data. */
261 DECLARE_QOBJ_TYPE(bfd_session
);
264 TAILQ_ENTRY(peer_label
) pl_entry
;
266 struct bfd_session
*pl_bs
;
267 char pl_label
[MAXNAMELEN
];
269 TAILQ_HEAD(pllist
, peer_label
);
271 struct bfd_diag_str_list
{
276 struct bfd_state_str_list
{
283 char name
[MAXNAMELEN
+ 1];
288 char ifname
[MAXNAMELEN
+ 1];
292 /* States defined per 4.1 */
293 #define PTM_BFD_ADM_DOWN 0
294 #define PTM_BFD_DOWN 1
295 #define PTM_BFD_INIT 2
299 /* Various constants */
300 /* Retrieved from ptm_timer.h from Cumulus PTM sources. */
301 #define MSEC_PER_SEC 1000L
302 #define NSEC_PER_MSEC 1000000L
304 #define BFD_DEF_DEMAND 0
305 #define BFD_DEFDETECTMULT 3
306 #define BFD_DEFDESIREDMINTX (300 * MSEC_PER_SEC)
307 #define BFD_DEFREQUIREDMINRX (300 * MSEC_PER_SEC)
308 #define BFD_DEF_REQ_MIN_ECHO (50 * MSEC_PER_SEC)
309 #define BFD_DEF_SLOWTX (2000 * MSEC_PER_SEC)
310 #define BFD_DEF_MHOP_TTL 5
311 #define BFD_PKT_LEN 24 /* Length of control packet */
312 #define BFD_TTL_VAL 255
313 #define BFD_RCV_TTL_VAL 1
314 #define BFD_TOS_VAL 0xC0
315 #define BFD_PKT_INFO_VAL 1
316 #define BFD_IPV6_PKT_INFO_VAL 1
317 #define BFD_IPV6_ONLY_VAL 1
318 #define BFD_SRCPORTINIT 49142
319 #define BFD_SRCPORTMAX 65536
320 #define BFD_DEFDESTPORT 3784
321 #define BFD_DEF_ECHO_PORT 3785
322 #define BFD_DEF_MHOP_DEST_PORT 4784
323 #define BFD_CMD_STRING_LEN (MAXNAMELEN + 50)
324 #define BFD_BUFFER_LEN (BFD_CMD_STRING_LEN + MAXNAMELEN + 1)
329 * Daemon control code to speak with local consumers.
332 /* See 'bfdctrl.h' for client protocol definitions. */
334 struct bfd_control_buffer
{
338 struct bfd_control_msg
*bcb_bcm
;
343 struct bfd_control_queue
{
344 TAILQ_ENTRY(bfd_control_queue
) bcq_entry
;
346 struct bfd_control_buffer bcq_bcb
;
348 TAILQ_HEAD(bcqueue
, bfd_control_queue
);
350 struct bfd_notify_peer
{
351 TAILQ_ENTRY(bfd_notify_peer
) bnp_entry
;
353 struct bfd_session
*bnp_bs
;
355 TAILQ_HEAD(bnplist
, bfd_notify_peer
);
357 struct bfd_control_socket
{
358 TAILQ_ENTRY(bfd_control_socket
) bcs_entry
;
361 struct thread
*bcs_ev
;
362 struct thread
*bcs_outev
;
363 struct bcqueue bcs_bcqueue
;
365 /* Notification data */
367 struct bnplist bcs_bnplist
;
369 enum bc_msg_version bcs_version
;
370 enum bc_msg_type bcs_type
;
372 /* Message buffering */
373 struct bfd_control_buffer bcs_bin
;
374 struct bfd_control_buffer
*bcs_bout
;
376 TAILQ_HEAD(bcslist
, bfd_control_socket
);
378 int control_init(const char *path
);
379 void control_shutdown(void);
380 int control_notify(struct bfd_session
*bs
);
381 int control_notify_config(const char *op
, struct bfd_session
*bs
);
382 int control_accept(struct thread
*t
);
388 * Daemon specific code.
397 struct thread
*bg_ev
[6];
400 struct thread
*bg_csockev
;
401 struct bcslist bg_bcslist
;
403 struct pllist bg_pllist
;
405 extern struct bfd_global bglobal
;
406 extern struct bfd_diag_str_list diag_list
[];
407 extern struct bfd_state_str_list state_list
[];
409 void socket_close(int *s
);
415 * Contains the code related with loading/reloading configuration.
417 int parse_config(const char *fname
);
418 int config_request_add(const char *jsonstr
);
419 int config_request_del(const char *jsonstr
);
420 char *config_response(const char *status
, const char *error
);
421 char *config_notify(struct bfd_session
*bs
);
422 char *config_notify_config(const char *op
, struct bfd_session
*bs
);
424 typedef int (*bpc_handle
)(struct bfd_peer_cfg
*, void *arg
);
425 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
428 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
);
429 struct peer_label
*pl_find(const char *label
);
430 void pl_free(struct peer_label
*pl
);
436 * Contains code that does the logging procedures. Might implement multiple
437 * backends (e.g. zebra log, syslog or other logging lib).
440 /* level vs syslog equivalent */
441 BLOG_DEBUG
= 0, /* LOG_DEBUG */
442 BLOG_INFO
= 1, /* LOG_INFO */
443 BLOG_WARNING
= 2, /* LOG_WARNING */
444 BLOG_ERROR
= 3, /* LOG_ERR */
445 BLOG_FATAL
= 4, /* LOG_CRIT */
448 void log_init(int foreground
, enum blog_level level
,
449 struct frr_daemon_info
*fdi
);
450 void log_info(const char *fmt
, ...);
451 void log_debug(const char *fmt
, ...);
452 void log_warning(const char *fmt
, ...);
453 void log_error(const char *fmt
, ...);
454 void log_fatal(const char *fmt
, ...);
460 * Contains the code related with receiving/seding, packing/unpacking BFD data.
462 int bp_set_ttlv6(int sd
, uint8_t value
);
463 int bp_set_ttl(int sd
, uint8_t value
);
464 int bp_set_tosv6(int sd
, uint8_t value
);
465 int bp_set_tos(int sd
, uint8_t value
);
466 int bp_bind_dev(int sd
, const char *dev
);
468 int bp_udp_shop(void);
469 int bp_udp_mhop(void);
470 int bp_udp6_shop(void);
471 int bp_udp6_mhop(void);
472 int bp_peer_socket(struct bfd_peer_cfg
*bpc
);
473 int bp_peer_socketv6(struct bfd_peer_cfg
*bpc
);
474 int bp_echo_socket(void);
475 int bp_echov6_socket(void);
477 void ptm_bfd_snd(struct bfd_session
*bfd
, int fbit
);
478 void ptm_bfd_echo_snd(struct bfd_session
*bfd
);
480 int bfd_recv_cb(struct thread
*t
);
486 * Contains the code related with event loop.
488 typedef void (*bfd_ev_cb
)(struct thread
*t
);
490 void bfd_recvtimer_update(struct bfd_session
*bs
);
491 void bfd_echo_recvtimer_update(struct bfd_session
*bs
);
492 void bfd_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
493 void bfd_echo_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
495 void bfd_xmttimer_delete(struct bfd_session
*bs
);
496 void bfd_echo_xmttimer_delete(struct bfd_session
*bs
);
497 void bfd_recvtimer_delete(struct bfd_session
*bs
);
498 void bfd_echo_recvtimer_delete(struct bfd_session
*bs
);
500 void bfd_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
501 void bfd_echo_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
502 void bfd_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
503 void bfd_echo_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
509 * BFD protocol specific code.
511 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
);
512 int ptm_bfd_ses_del(struct bfd_peer_cfg
*bpc
);
513 void ptm_bfd_ses_dn(struct bfd_session
*bfd
, uint8_t diag
);
514 void ptm_bfd_ses_up(struct bfd_session
*bfd
);
515 void fetch_portname_from_ifindex(int ifindex
, char *ifname
, size_t ifnamelen
);
516 void ptm_bfd_echo_stop(struct bfd_session
*bfd
, int polling
);
517 void ptm_bfd_echo_start(struct bfd_session
*bfd
);
518 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
);
519 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
);
520 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
, char *port_name
,
521 struct sockaddr_any
*peer
,
522 struct sockaddr_any
*local
,
523 char *vrf_name
, bool is_mhop
);
525 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
);
526 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
);
527 void bfd_set_polling(struct bfd_session
*bs
);
528 const char *satostr(struct sockaddr_any
*sa
);
529 const char *diag2str(uint8_t diag
);
530 int strtosa(const char *addr
, struct sockaddr_any
*sa
);
531 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
);
532 const char *bs_to_string(struct bfd_session
*bs
);
534 /* BFD hash data structures interface */
535 void bfd_initialize(void);
536 void bfd_shutdown(void);
537 struct bfd_session
*bfd_id_lookup(uint32_t id
);
538 struct bfd_session
*bfd_shop_lookup(struct bfd_shop_key shop
);
539 struct bfd_session
*bfd_mhop_lookup(struct bfd_mhop_key mhop
);
540 struct bfd_vrf
*bfd_vrf_lookup(int vrf_id
);
541 struct bfd_iface
*bfd_iface_lookup(const char *ifname
);
543 struct bfd_session
*bfd_id_delete(uint32_t id
);
544 struct bfd_session
*bfd_shop_delete(struct bfd_shop_key shop
);
545 struct bfd_session
*bfd_mhop_delete(struct bfd_mhop_key mhop
);
546 struct bfd_vrf
*bfd_vrf_delete(int vrf_id
);
547 struct bfd_iface
*bfd_iface_delete(const char *ifname
);
549 bool bfd_id_insert(struct bfd_session
*bs
);
550 bool bfd_shop_insert(struct bfd_session
*bs
);
551 bool bfd_mhop_insert(struct bfd_session
*bs
);
552 bool bfd_vrf_insert(struct bfd_vrf
*vrf
);
553 bool bfd_iface_insert(struct bfd_iface
*iface
);
555 typedef void (*hash_iter_func
)(struct hash_backet
*hb
, void *arg
);
556 void bfd_id_iterate(hash_iter_func hif
, void *arg
);
557 void bfd_shop_iterate(hash_iter_func hif
, void *arg
);
558 void bfd_mhop_iterate(hash_iter_func hif
, void *arg
);
559 void bfd_vrf_iterate(hash_iter_func hif
, void *arg
);
560 void bfd_iface_iterate(hash_iter_func hif
, void *arg
);
562 /* Export callback functions for `event.c`. */
563 extern struct thread_master
*master
;
565 int bfd_recvtimer_cb(struct thread
*t
);
566 int bfd_echo_recvtimer_cb(struct thread
*t
);
567 int bfd_xmt_cb(struct thread
*t
);
568 int bfd_echo_xmt_cb(struct thread
*t
);
574 * BFD daemon vty shell commands.
576 void bfdd_vty_init(void);
582 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
);
583 void bfdd_zclient_stop(void);
585 int ptm_bfd_notify(struct bfd_session
*bs
);
589 * OS compatibility functions.
591 #if defined(BFD_LINUX) || defined(BFD_BSD)
592 int ptm_bfd_fetch_ifindex(const char *ifname
);
593 void ptm_bfd_fetch_local_mac(const char *ifname
, uint8_t *mac
);
594 void fetch_portname_from_ifindex(int ifindex
, char *ifname
, size_t ifnamelen
);
595 #endif /* BFD_LINUX || BFD_BSD */
598 ssize_t
bsd_echo_sock_read(int sd
, uint8_t *buf
, ssize_t
*buflen
,
599 struct sockaddr_storage
*ss
, socklen_t
*sslen
,
600 uint8_t *ttl
, uint32_t *id
);