| 1 | /*	$OpenBSD: bfd.h,v 1.14 2025/07/24 00:49:22 jsg Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2016 Peter Hessler <phessler@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Support for Bi-directional Forwarding Detection (RFC 5880 / 5881) |
| 21 | */ |
| 22 | |
| 23 | #ifndef _NET_BFD_H_ |
| 24 | #define _NET_BFD_H_ |
| 25 | |
| 26 | /* Public Interface */ |
| 27 | |
| 28 | #define BFD_MODE_ASYNC			1 |
| 29 | #define BFD_MODE_DEMAND			2 |
| 30 | |
| 31 | /* Diagnostic Code (RFC 5880 Page 8) */ |
| 32 | #define BFD_DIAG_NONE			0 |
| 33 | #define BFD_DIAG_EXPIRED		1 |
| 34 | #define BFD_DIAG_ECHO_FAILED		2 |
| 35 | #define BFD_DIAG_NEIGHBOR_SIGDOWN	3 |
| 36 | #define BFD_DIAG_FIB_RESET		4 |
| 37 | #define BFD_DIAG_PATH_DOWN		5 |
| 38 | #define BFD_DIAG_CONCAT_PATH_DOWN	6 |
| 39 | #define BFD_DIAG_ADMIN_DOWN		7 |
| 40 | #define BFD_DIAG_CONCAT_REVERSE_DOWN	8 |
| 41 | |
| 42 | /* State (RFC 5880 Page 8) */ |
| 43 | #define BFD_STATE_ADMINDOWN		0 |
| 44 | #define BFD_STATE_DOWN			1 |
| 45 | #define BFD_STATE_INIT			2 |
| 46 | #define BFD_STATE_UP			3 |
| 47 | |
| 48 | /* Flags (RFC 5880 Page 8) */ |
| 49 | #define BFD_FLAG_P			0x20 |
| 50 | #define BFD_FLAG_F			0x10 |
| 51 | #define BFD_FLAG_C			0x08 |
| 52 | #define BFD_FLAG_A			0x04 |
| 53 | #define BFD_FLAG_D			0x02 |
| 54 | #define BFD_FLAG_M			0x01 |
| 55 | |
| 56 | struct sockaddr_bfd { |
| 57 | 	uint8_t		bs_len;		/* total length */ |
| 58 | 	uint8_t		bs_family;	/* address family */ |
| 59 | 	/* above matches sockaddr_storage */ |
| 60 | |
| 61 | 	/* Sorted for bit boundaries */ |
| 62 | 	uint16_t	bs_mode; |
| 63 | 	uint32_t	bs_localdiscr; |
| 64 | |
| 65 | 	int64_t		bs_uptime; |
| 66 | |
| 67 | 	int64_t		bs_lastuptime; |
| 68 | |
| 69 | 	uint32_t	bs_mintx; |
| 70 | 	uint32_t	bs_minrx; |
| 71 | |
| 72 | 	uint32_t	bs_minecho; |
| 73 | 	uint32_t	bs_localdiag; |
| 74 | |
| 75 | 	uint32_t	bs_remotediscr; |
| 76 | 	uint32_t	bs_remotediag; |
| 77 | |
| 78 | 	uint16_t	bs_multiplier; |
| 79 | 	uint16_t	bs_pad0; |
| 80 | 	unsigned int	bs_state; |
| 81 | 	unsigned int	bs_remotestate; |
| 82 | 	unsigned int	bs_laststate; |
| 83 | 	unsigned int	bs_error; |
| 84 | |
| 85 | 	/* add padding to reach a power of two */ |
| 86 | 	uint64_t	bs_pad1; |
| 87 | }; |
| 88 | |
| 89 | struct bfd_msghdr { |
| 90 | 	uint16_t	bm_msglen; |
| 91 | 	uint8_t		bm_version; |
| 92 | 	uint8_t		bm_type; |
| 93 | 	uint16_t	bm_hdrlen; |
| 94 | 	uint16_t	bm_index; |
| 95 | |
| 96 | 	uint16_t	bm_tableid; |
| 97 | 	uint8_t		bm_priority; |
| 98 | 	uint8_t		bm_mpls; |
| 99 | 	int		bm_addrs; |
| 100 | 	int		bm_flags; |
| 101 | 	/* above matches rt_msghdr */ |
| 102 | 	uint16_t	bm_pad0;	/* for 4 byte boundary */ |
| 103 | |
| 104 | 	struct sockaddr_bfd	bm_sa;	/* bfd msg for userland */ |
| 105 | }; |
| 106 | |
| 107 | #ifdef _KERNEL |
| 108 | /* state machine from RFC 5880 6.8.1*/ |
| 109 | struct bfd_neighbor { |
| 110 | 	uint32_t	bn_lstate;		/* SessionState */ |
| 111 | 	uint32_t	bn_rstate;		/* RemoteSessionState */ |
| 112 | 	uint32_t	bn_ldiscr;		/* LocalDiscr */ |
| 113 | 	uint32_t	bn_rdiscr;		/* RemoteDiscr */ |
| 114 | 	uint32_t	bn_ldiag;		/* LocalDiag */ |
| 115 | 	uint32_t	bn_rdiag;		/* RemoteDiag */ |
| 116 | 	uint32_t	bn_mintx;		/* DesiredMinTxInterval */ |
| 117 | 	uint32_t	bn_req_minrx;		/* RequiredMinRxInterval */ |
| 118 | 	uint32_t	bn_rminrx;		/* RemoteMinRxInterval */ |
| 119 | 	uint32_t	bn_demand;		/* DemandMode */ |
| 120 | 	uint32_t	bn_rdemand;		/* RemoteDemandMode */ |
| 121 | 	uint32_t	bn_authtype;		/* AuthType */ |
| 122 | 	uint32_t	bn_rauthseq;		/* RcvAuthSeq */ |
| 123 | 	uint32_t	bn_lauthseq;		/* XmitAuthSeq */ |
| 124 | 	uint32_t	bn_authseqknown;	/* AuthSeqKnown */ |
| 125 | 	uint16_t	bn_mult;		/* DetectMult */ |
| 126 | }; |
| 127 | |
| 128 | struct bfd_config { |
| 129 | 	TAILQ_ENTRY(bfd_config)	 bc_entry; |
| 130 | 	struct socket		*bc_so; |
| 131 | 	struct socket		*bc_upcallso; |
| 132 | 	struct socket		*bc_soecho; |
| 133 | 	struct socket		*bc_sosend; |
| 134 | 	struct rtentry		*bc_rt; |
| 135 | 	struct bfd_neighbor	*bc_neighbor; |
| 136 | 	struct timeval		*bc_time; |
| 137 | 	struct task		 bc_bfd_task; |
| 138 | 	struct task		 bc_bfd_send_task; |
| 139 | 	struct task		 bc_upcall_task; |
| 140 | 	struct task		 bc_clear_task; |
| 141 | 	struct timeout		 bc_timo_rx; |
| 142 | 	struct timeout		 bc_timo_tx; |
| 143 | 	time_t			 bc_lastuptime; |
| 144 | 	unsigned int		 bc_laststate; |
| 145 | 	unsigned int		 bc_state; |
| 146 | 	unsigned int		 bc_poll; |
| 147 | 	unsigned int		 bc_error; |
| 148 | 	uint32_t		 bc_minrx; |
| 149 | 	uint32_t		 bc_mintx; |
| 150 | 	uint32_t		 bc_minecho; |
| 151 | 	uint16_t		 bc_multiplier; |
| 152 | 	uint16_t		 bc_mode; |
| 153 | }; |
| 154 | |
| 155 | struct sockaddr *bfd2sa(const struct rtentry *, struct sockaddr_bfd *); |
| 156 | |
| 157 | int		 bfdset(struct rtentry *); |
| 158 | void		 bfdclear(struct rtentry *); |
| 159 | void		 bfdinit(void); |
| 160 | |
| 161 | #endif /* _KERNEL */ |
| 162 | |
| 163 | #endif /* _NET_BFD_H_ */ |