xref: /openbsd/usr.sbin/bgpd/session.h (revision d7629114)
1 /*	$OpenBSD: session.h,v 1.174 2024/10/01 11:49:24 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@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 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <time.h>
22 
23 #define	MAX_BACKLOG			5
24 #define	INTERVAL_CONNECTRETRY		120
25 #define	INTERVAL_HOLD_INITIAL		240
26 #define	INTERVAL_HOLD			90
27 #define	INTERVAL_IDLE_HOLD_INITIAL	30
28 #define	INTERVAL_HOLD_CLONED		3600
29 #define	INTERVAL_HOLD_DEMOTED		60
30 #define	MAX_IDLE_HOLD			3600
31 #define	MSGSIZE_HEADER			19
32 #define	MSGSIZE_HEADER_MARKER		16
33 #define	MSGSIZE_NOTIFICATION_MIN	21	/* 19 hdr + 1 code + 1 sub */
34 #define	MSGSIZE_OPEN_MIN		29
35 #define	MSGSIZE_UPDATE_MIN		23
36 #define	MSGSIZE_KEEPALIVE		MSGSIZE_HEADER
37 #define	MSGSIZE_RREFRESH		(MSGSIZE_HEADER + 4)
38 #define	MSGSIZE_RREFRESH_MIN		MSGSIZE_RREFRESH
39 #define	MSG_PROCESS_LIMIT		25
40 #define	SESSION_CLEAR_DELAY		5
41 
42 enum session_state {
43 	STATE_NONE,
44 	STATE_IDLE,
45 	STATE_CONNECT,
46 	STATE_ACTIVE,
47 	STATE_OPENSENT,
48 	STATE_OPENCONFIRM,
49 	STATE_ESTABLISHED
50 };
51 
52 enum session_events {
53 	EVNT_NONE,
54 	EVNT_START,
55 	EVNT_STOP,
56 	EVNT_CON_OPEN,
57 	EVNT_CON_CLOSED,
58 	EVNT_CON_OPENFAIL,
59 	EVNT_CON_FATAL,
60 	EVNT_TIMER_CONNRETRY,
61 	EVNT_TIMER_HOLDTIME,
62 	EVNT_TIMER_KEEPALIVE,
63 	EVNT_TIMER_SENDHOLD,
64 	EVNT_RCVD_OPEN,
65 	EVNT_RCVD_KEEPALIVE,
66 	EVNT_RCVD_UPDATE,
67 	EVNT_RCVD_NOTIFICATION
68 };
69 
70 enum msg_type {
71 	OPEN = 1,
72 	UPDATE,
73 	NOTIFICATION,
74 	KEEPALIVE,
75 	RREFRESH
76 };
77 
78 enum suberr_header {
79 	ERR_HDR_SYNC = 1,
80 	ERR_HDR_LEN,
81 	ERR_HDR_TYPE
82 };
83 
84 enum suberr_open {
85 	ERR_OPEN_VERSION = 1,
86 	ERR_OPEN_AS,
87 	ERR_OPEN_BGPID,
88 	ERR_OPEN_OPT,
89 	ERR_OPEN_AUTH,			/* deprecated */
90 	ERR_OPEN_HOLDTIME,
91 	ERR_OPEN_CAPA,
92 	ERR_OPEN_ROLE = 11,
93 };
94 
95 enum suberr_fsm {
96 	ERR_FSM_UNSPECIFIC = 0,
97 	ERR_FSM_UNEX_OPENSENT,
98 	ERR_FSM_UNEX_OPENCONFIRM,
99 	ERR_FSM_UNEX_ESTABLISHED
100 };
101 
102 enum opt_params {
103 	OPT_PARAM_NONE,
104 	OPT_PARAM_AUTH,
105 	OPT_PARAM_CAPABILITIES,
106 	OPT_PARAM_EXT_LEN=255,
107 };
108 
109 struct bgp_msg {
110 	struct ibuf	*buf;
111 	enum msg_type	 type;
112 	uint16_t	 len;
113 };
114 
115 struct bgpd_sysdep {
116 	uint8_t			no_pfkey;
117 	uint8_t			no_md5sig;
118 };
119 
120 struct ctl_conn {
121 	TAILQ_ENTRY(ctl_conn)	entry;
122 	struct imsgbuf		imsgbuf;
123 	int			restricted;
124 	int			throttled;
125 	int			terminate;
126 };
127 
128 struct peer_stats {
129 	unsigned long long	 msg_rcvd_open;
130 	unsigned long long	 msg_rcvd_update;
131 	unsigned long long	 msg_rcvd_notification;
132 	unsigned long long	 msg_rcvd_keepalive;
133 	unsigned long long	 msg_rcvd_rrefresh;
134 	unsigned long long	 msg_sent_open;
135 	unsigned long long	 msg_sent_update;
136 	unsigned long long	 msg_sent_notification;
137 	unsigned long long	 msg_sent_keepalive;
138 	unsigned long long	 msg_sent_rrefresh;
139 	unsigned long long	 refresh_rcvd_req;
140 	unsigned long long	 refresh_rcvd_borr;
141 	unsigned long long	 refresh_rcvd_eorr;
142 	unsigned long long	 refresh_sent_req;
143 	unsigned long long	 refresh_sent_borr;
144 	unsigned long long	 refresh_sent_eorr;
145 	unsigned long long	 prefix_rcvd_update;
146 	unsigned long long	 prefix_rcvd_withdraw;
147 	unsigned long long	 prefix_rcvd_eor;
148 	unsigned long long	 prefix_sent_update;
149 	unsigned long long	 prefix_sent_withdraw;
150 	unsigned long long	 prefix_sent_eor;
151 	time_t			 last_updown;
152 	time_t			 last_read;
153 	time_t			 last_write;
154 	uint32_t		 msg_queue_len;
155 	uint32_t		 prefix_cnt;
156 	uint32_t		 prefix_out_cnt;
157 	uint32_t		 pending_update;
158 	uint32_t		 pending_withdraw;
159 	uint8_t			 last_sent_errcode;
160 	uint8_t			 last_sent_suberr;
161 	uint8_t			 last_rcvd_errcode;
162 	uint8_t			 last_rcvd_suberr;
163 	char			 last_reason[REASON_LEN];
164 };
165 
166 struct auth_state {
167 	struct bgpd_addr	local_addr;
168 	struct bgpd_addr	remote_addr;
169 	uint32_t		spi_in;
170 	uint32_t		spi_out;
171 	enum auth_method	method;
172 	uint8_t			established;
173 };
174 
175 enum Timer {
176 	Timer_None,
177 	Timer_ConnectRetry,
178 	Timer_Keepalive,
179 	Timer_Hold,
180 	Timer_SendHold,
181 	Timer_IdleHold,
182 	Timer_IdleHoldReset,
183 	Timer_CarpUndemote,
184 	Timer_RestartTimeout,
185 	Timer_Rtr_Refresh,
186 	Timer_Rtr_Retry,
187 	Timer_Rtr_Expire,
188 	Timer_Rtr_Active,
189 	Timer_Max
190 };
191 
192 struct timer {
193 	TAILQ_ENTRY(timer)	entry;
194 	enum Timer		type;
195 	time_t			val;
196 };
197 
198 TAILQ_HEAD(timer_head, timer);
199 
200 struct peer {
201 	struct peer_config	 conf;
202 	struct peer_stats	 stats;
203 	RB_ENTRY(peer)		 entry;
204 	struct {
205 		struct capabilities	ann;
206 		struct capabilities	peer;
207 		struct capabilities	neg;
208 	}			 capa;
209 	struct auth_state	 auth_state;
210 	struct auth_config	 auth_conf;
211 	struct bgpd_addr	 local;
212 	struct bgpd_addr	 local_alt;
213 	struct bgpd_addr	 remote;
214 	struct timer_head	 timers;
215 	struct msgbuf		 wbuf;
216 	struct ibuf_read	*rbuf;
217 	struct peer		*template;
218 	int			 fd;
219 	int			 lasterr;
220 	u_int			 errcnt;
221 	u_int			 IdleHoldTime;
222 	unsigned int		 if_scope;	/* interface scope for IPv6 */
223 	uint32_t		 remote_bgpid;
224 	enum session_state	 state;
225 	enum session_state	 prev_state;
226 	enum reconf_action	 reconf_action;
227 	enum role		 remote_role;
228 	uint16_t		 short_as;
229 	uint16_t		 holdtime;
230 	uint16_t		 local_port;
231 	uint16_t		 remote_port;
232 	uint8_t			 depend_ok;
233 	uint8_t			 demoted;
234 	uint8_t			 passive;
235 	uint8_t			 throttled;
236 	uint8_t			 rpending;
237 };
238 
239 extern time_t		 pauseaccept;
240 
241 struct ctl_timer {
242 	enum Timer	type;
243 	time_t		val;
244 };
245 
246 /* carp.c */
247 int	 carp_demote_init(char *, int);
248 void	 carp_demote_shutdown(void);
249 int	 carp_demote_get(char *);
250 int	 carp_demote_set(char *, int);
251 
252 /* config.c */
253 void	 merge_config(struct bgpd_config *, struct bgpd_config *);
254 void	 free_deleted_peers(struct bgpd_config *);
255 int	 prepare_listeners(struct bgpd_config *);
256 
257 /* control.c */
258 int	control_check(char *);
259 int	control_init(int, char *);
260 int	control_listen(int);
261 size_t	control_fill_pfds(struct pollfd *, size_t);
262 void	control_shutdown(int);
263 int	control_dispatch_msg(struct pollfd *, struct peer_head *);
264 unsigned int	control_accept(int, int);
265 
266 /* log.c */
267 char	*log_fmt_peer(const struct peer_config *);
268 void	 log_statechange(struct peer *, enum session_state,
269 	    enum session_events);
270 void	 log_notification(const struct peer *, uint8_t, uint8_t,
271 	    const struct ibuf *, const char *);
272 void	 log_conn_attempt(const struct peer *, struct sockaddr *,
273 	    socklen_t);
274 
275 /* mrt.c */
276 void	 mrt_dump_bgp_msg(struct mrt *, void *, uint16_t,
277 	    struct peer *, enum msg_type);
278 void	 mrt_dump_state(struct mrt *, uint16_t, uint16_t,
279 	    struct peer *);
280 void	 mrt_done(struct mrt *);
281 
282 /* pfkey.c */
283 struct sadb_msg;
284 int	pfkey_read(int, struct sadb_msg *);
285 int	pfkey_establish(struct auth_state *, struct auth_config *,
286 	    const struct bgpd_addr *, const struct bgpd_addr *);
287 int	pfkey_remove(struct auth_state *);
288 int	pfkey_init(void);
289 int	pfkey_send_conf(struct imsgbuf *, uint32_t, struct auth_config *);
290 int	pfkey_recv_conf(struct peer *, struct imsg *);
291 int	tcp_md5_check(int, struct auth_config *);
292 int	tcp_md5_set(int, struct auth_config *, struct bgpd_addr *);
293 int	tcp_md5_prep_listener(struct listen_addr *, struct peer_head *);
294 void	tcp_md5_add_listener(struct bgpd_config *, struct peer *);
295 void	tcp_md5_del_listener(struct bgpd_config *, struct peer *);
296 
297 /* printconf.c */
298 void	print_config(struct bgpd_config *, struct rib_names *);
299 
300 /* rde.c */
301 void	rde_main(int, int);
302 
303 /* rtr_proto.c */
304 struct rtr_session;
305 size_t			 rtr_count(void);
306 void			 rtr_check_events(struct pollfd *, size_t);
307 size_t			 rtr_poll_events(struct pollfd *, size_t, time_t *);
308 struct rtr_session	*rtr_new(uint32_t, struct rtr_config_msg *);
309 struct rtr_session	*rtr_get(uint32_t);
310 void			 rtr_free(struct rtr_session *);
311 void			 rtr_open(struct rtr_session *, int);
312 void			 rtr_config_prep(void);
313 void			 rtr_config_merge(void);
314 void			 rtr_config_keep(struct rtr_session *,
315 			     struct rtr_config_msg *);
316 void			 rtr_roa_merge(struct roa_tree *);
317 void			 rtr_aspa_merge(struct aspa_tree *);
318 void			 rtr_shutdown(void);
319 void			 rtr_show(struct rtr_session *, pid_t);
320 
321 /* rtr.c */
322 void	rtr_sem_acquire(int);
323 void	rtr_sem_release(int);
324 void	rtr_roa_insert(struct roa_tree *, struct roa *);
325 void	rtr_aspa_insert(struct aspa_tree *, struct aspa_set *);
326 void	rtr_main(int, int);
327 void	rtr_imsg_compose(int, uint32_t, pid_t, void *, size_t);
328 void	rtr_recalc(void);
329 
330 /* session.c */
331 RB_PROTOTYPE(peer_head, peer, entry, peer_compare);
332 
333 void		 session_main(int, int);
334 void		 bgp_fsm(struct peer *, enum session_events);
335 int		 session_neighbor_rrefresh(struct peer *p);
336 struct peer	*getpeerbydesc(struct bgpd_config *, const char *);
337 struct peer	*getpeerbyip(struct bgpd_config *, struct sockaddr *);
338 struct peer	*getpeerbyid(struct bgpd_config *, uint32_t);
339 int		 peer_matched(struct peer *, struct ctl_neighbor *);
340 int		 imsg_ctl_parent(struct imsg *);
341 int		 imsg_ctl_rde(struct imsg *);
342 int		 imsg_ctl_rde_msg(int, uint32_t, pid_t);
343 void		 session_stop(struct peer *, uint8_t, const char *);
344 struct bgpd_addr *session_localaddr(struct peer *);
345 
346 /* timer.c */
347 struct timer	*timer_get(struct timer_head *, enum Timer);
348 struct timer	*timer_nextisdue(struct timer_head *, time_t);
349 time_t		 timer_nextduein(struct timer_head *, time_t);
350 int		 timer_running(struct timer_head *, enum Timer, time_t *);
351 void		 timer_set(struct timer_head *, enum Timer, u_int);
352 void		 timer_stop(struct timer_head *, enum Timer);
353 void		 timer_remove(struct timer_head *, enum Timer);
354 void		 timer_remove_all(struct timer_head *);
355