xref: /openbsd/usr.sbin/bgpd/session.h (revision 09467b48)
1 /*	$OpenBSD: session.h,v 1.146 2020/05/10 13:38:46 deraadt 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	MSG_PROCESS_LIMIT		25
39 #define	SESSION_CLEAR_DELAY		5
40 
41 enum session_state {
42 	STATE_NONE,
43 	STATE_IDLE,
44 	STATE_CONNECT,
45 	STATE_ACTIVE,
46 	STATE_OPENSENT,
47 	STATE_OPENCONFIRM,
48 	STATE_ESTABLISHED
49 };
50 
51 enum session_events {
52 	EVNT_NONE,
53 	EVNT_START,
54 	EVNT_STOP,
55 	EVNT_CON_OPEN,
56 	EVNT_CON_CLOSED,
57 	EVNT_CON_OPENFAIL,
58 	EVNT_CON_FATAL,
59 	EVNT_TIMER_CONNRETRY,
60 	EVNT_TIMER_HOLDTIME,
61 	EVNT_TIMER_KEEPALIVE,
62 	EVNT_RCVD_OPEN,
63 	EVNT_RCVD_KEEPALIVE,
64 	EVNT_RCVD_UPDATE,
65 	EVNT_RCVD_NOTIFICATION
66 };
67 
68 enum msg_type {
69 	OPEN = 1,
70 	UPDATE,
71 	NOTIFICATION,
72 	KEEPALIVE,
73 	RREFRESH
74 };
75 
76 enum suberr_header {
77 	ERR_HDR_SYNC = 1,
78 	ERR_HDR_LEN,
79 	ERR_HDR_TYPE
80 };
81 
82 enum suberr_open {
83 	ERR_OPEN_VERSION = 1,
84 	ERR_OPEN_AS,
85 	ERR_OPEN_BGPID,
86 	ERR_OPEN_OPT,
87 	ERR_OPEN_AUTH,
88 	ERR_OPEN_HOLDTIME,
89 	ERR_OPEN_CAPA,
90 	ERR_OPEN_GROUP_CONFLICT,	/* draft-ietf-idr-bgp-multisession-07 */
91 	ERR_OPEN_GROUP_REQUIRED		/* draft-ietf-idr-bgp-multisession-07 */
92 };
93 
94 enum suberr_fsm {
95 	ERR_FSM_UNSPECIFIC = 0,
96 	ERR_FSM_UNEX_OPENSENT,
97 	ERR_FSM_UNEX_OPENCONFIRM,
98 	ERR_FSM_UNEX_ESTABLISHED
99 };
100 
101 enum opt_params {
102 	OPT_PARAM_NONE,
103 	OPT_PARAM_AUTH,
104 	OPT_PARAM_CAPABILITIES
105 };
106 
107 enum capa_codes {
108 	CAPA_NONE,
109 	CAPA_MP,
110 	CAPA_REFRESH,
111 	CAPA_RESTART = 64,
112 	CAPA_AS4BYTE = 65
113 };
114 
115 struct bgp_msg {
116 	struct ibuf	*buf;
117 	enum msg_type	 type;
118 	u_int16_t	 len;
119 };
120 
121 struct msg_header {
122 	u_char			 marker[MSGSIZE_HEADER_MARKER];
123 	u_int16_t		 len;
124 	u_int8_t		 type;
125 };
126 
127 struct msg_open {
128 	struct msg_header	 header;
129 	u_int32_t		 bgpid;
130 	u_int16_t		 myas;
131 	u_int16_t		 holdtime;
132 	u_int8_t		 version;
133 	u_int8_t		 optparamlen;
134 };
135 
136 struct bgpd_sysdep {
137 	u_int8_t		no_pfkey;
138 	u_int8_t		no_md5sig;
139 };
140 
141 struct ctl_conn {
142 	TAILQ_ENTRY(ctl_conn)	entry;
143 	struct imsgbuf		ibuf;
144 	int			restricted;
145 	int			throttled;
146 	int			terminate;
147 };
148 
149 TAILQ_HEAD(ctl_conns, ctl_conn)	ctl_conns;
150 
151 struct peer_stats {
152 	unsigned long long	 msg_rcvd_open;
153 	unsigned long long	 msg_rcvd_update;
154 	unsigned long long	 msg_rcvd_notification;
155 	unsigned long long	 msg_rcvd_keepalive;
156 	unsigned long long	 msg_rcvd_rrefresh;
157 	unsigned long long	 msg_sent_open;
158 	unsigned long long	 msg_sent_update;
159 	unsigned long long	 msg_sent_notification;
160 	unsigned long long	 msg_sent_keepalive;
161 	unsigned long long	 msg_sent_rrefresh;
162 	unsigned long long	 prefix_rcvd_update;
163 	unsigned long long	 prefix_rcvd_withdraw;
164 	unsigned long long	 prefix_rcvd_eor;
165 	unsigned long long	 prefix_sent_update;
166 	unsigned long long	 prefix_sent_withdraw;
167 	unsigned long long	 prefix_sent_eor;
168 	time_t			 last_updown;
169 	time_t			 last_read;
170 	time_t			 last_write;
171 	u_int32_t		 prefix_cnt;
172 	u_int32_t		 prefix_out_cnt;
173 	u_int8_t		 last_sent_errcode;
174 	u_int8_t		 last_sent_suberr;
175 	u_int8_t		 last_rcvd_errcode;
176 	u_int8_t		 last_rcvd_suberr;
177 	char			 last_reason[REASON_LEN];
178 };
179 
180 enum Timer {
181 	Timer_None,
182 	Timer_ConnectRetry,
183 	Timer_Keepalive,
184 	Timer_Hold,
185 	Timer_IdleHold,
186 	Timer_IdleHoldReset,
187 	Timer_CarpUndemote,
188 	Timer_RestartTimeout,
189 	Timer_Max
190 };
191 
192 struct peer_timer {
193 	TAILQ_ENTRY(peer_timer)	entry;
194 	enum Timer		type;
195 	time_t			val;
196 };
197 
198 TAILQ_HEAD(peer_timer_head, peer_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 {
210 		struct bgpd_addr	local_addr;
211 		u_int32_t		spi_in;
212 		u_int32_t		spi_out;
213 		enum auth_method	method;
214 		u_int8_t		established;
215 	}			 auth;
216 	struct bgpd_addr	 local;
217 	struct bgpd_addr	 local_alt;
218 	struct bgpd_addr	 remote;
219 	struct peer_timer_head	 timers;
220 	struct msgbuf		 wbuf;
221 	struct ibuf_read	*rbuf;
222 	struct peer		*template;
223 	int			 fd;
224 	int			 lasterr;
225 	u_int			 errcnt;
226 	u_int			 IdleHoldTime;
227 	u_int32_t		 remote_bgpid;
228 	enum session_state	 state;
229 	enum session_state	 prev_state;
230 	enum reconf_action	 reconf_action;
231 	u_int16_t		 short_as;
232 	u_int16_t		 holdtime;
233 	u_int16_t		 local_port;
234 	u_int16_t		 remote_port;
235 	u_int8_t		 depend_ok;
236 	u_int8_t		 demoted;
237 	u_int8_t		 passive;
238 	u_int8_t		 throttled;
239 	u_int8_t		 rpending;
240 };
241 
242 extern time_t		 pauseaccept;
243 
244 struct ctl_timer {
245 	enum Timer	type;
246 	time_t		val;
247 };
248 
249 /* carp.c */
250 int	 carp_demote_init(char *, int);
251 void	 carp_demote_shutdown(void);
252 int	 carp_demote_get(char *);
253 int	 carp_demote_set(char *, int);
254 
255 /* config.c */
256 void	 merge_config(struct bgpd_config *, struct bgpd_config *);
257 int	 prepare_listeners(struct bgpd_config *);
258 
259 /* control.c */
260 int	control_check(char *);
261 int	control_init(int, char *);
262 int	control_listen(int);
263 void	control_shutdown(int);
264 int	control_dispatch_msg(struct pollfd *, u_int *, struct peer_head *);
265 unsigned int	control_accept(int, int);
266 
267 /* log.c */
268 char		*log_fmt_peer(const struct peer_config *);
269 void		 log_statechange(struct peer *, enum session_state,
270 		    enum session_events);
271 void		 log_notification(const struct peer *, u_int8_t, u_int8_t,
272 		    u_char *, u_int16_t, const char *);
273 void		 log_conn_attempt(const struct peer *, struct sockaddr *,
274 		    socklen_t);
275 
276 /* mrt.c */
277 void		 mrt_dump_bgp_msg(struct mrt *, void *, u_int16_t,
278 		     struct peer *);
279 void		 mrt_dump_state(struct mrt *, u_int16_t, u_int16_t,
280 		     struct peer *);
281 void		 mrt_done(struct mrt *);
282 
283 /* parse.y */
284 struct bgpd_config *parse_config(char *, struct peer_head *);
285 
286 /* pfkey.c */
287 int	pfkey_read(int, struct sadb_msg *);
288 int	pfkey_establish(struct peer *);
289 int	pfkey_remove(struct peer *);
290 int	pfkey_init(void);
291 int	tcp_md5_check(int, struct peer *);
292 int	tcp_md5_set(int, struct peer *);
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 /* session.c */
304 RB_PROTOTYPE(peer_head, peer, entry, peer_compare);
305 
306 void		 session_main(int, int);
307 void		 bgp_fsm(struct peer *, enum session_events);
308 int		 session_neighbor_rrefresh(struct peer *p);
309 struct peer	*getpeerbydesc(struct bgpd_config *, const char *);
310 struct peer	*getpeerbyip(struct bgpd_config *, struct sockaddr *);
311 struct peer	*getpeerbyid(struct bgpd_config *, u_int32_t);
312 int		 peer_matched(struct peer *, struct ctl_neighbor *);
313 int		 imsg_ctl_parent(int, u_int32_t, pid_t, void *, u_int16_t);
314 int		 imsg_ctl_rde(int, pid_t, void *, u_int16_t);
315 void		 session_stop(struct peer *, u_int8_t);
316 
317 /* timer.c */
318 struct peer_timer	*timer_get(struct peer *, enum Timer);
319 struct peer_timer	*timer_nextisdue(struct peer *, time_t);
320 time_t			 timer_nextduein(struct peer *, time_t);
321 int			 timer_running(struct peer *, enum Timer, time_t *);
322 void			 timer_set(struct peer *, enum Timer, u_int);
323 void			 timer_stop(struct peer *, enum Timer);
324 void			 timer_remove(struct peer *, enum Timer);
325 void			 timer_remove_all(struct peer *);
326