1 /*	$OpenBSD: bgpd.h,v 1.413 2021/03/02 09:45:07 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 #ifndef __BGPD_H__
19 #define	__BGPD_H__
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/queue.h>
24 #include <sys/tree.h>
25 #include <net/route.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <net/if.h>
29 #include <net/pfkeyv2.h>
30 
31 #include <poll.h>
32 #include <stdarg.h>
33 
34 #include <imsg.h>
35 
36 #define	BGP_VERSION			4
37 #define	BGP_PORT			179
38 #define	PEER_DESCR_LEN			32
39 #define	REASON_LEN			256	/* includes NUL terminator */
40 #define	PFTABLE_LEN			32
41 #define	TCP_MD5_KEY_LEN			80
42 #define	IPSEC_ENC_KEY_LEN		32
43 #define	IPSEC_AUTH_KEY_LEN		20
44 #define	SET_NAME_LEN			128
45 
46 #define	MAX_PKTSIZE			4096
47 #define	MIN_HOLDTIME			3
48 #define	READ_BUF_SIZE			65535
49 #define	MAX_SOCK_BUF			(4 * READ_BUF_SIZE)
50 #define	RT_BUF_SIZE			16384
51 #define	MAX_RTSOCK_BUF			(2 * 1024 * 1024)
52 #define	MAX_COMM_MATCH			3
53 
54 #define	BGPD_OPT_VERBOSE		0x0001
55 #define	BGPD_OPT_VERBOSE2		0x0002
56 #define	BGPD_OPT_NOACTION		0x0004
57 #define	BGPD_OPT_FORCE_DEMOTE		0x0008
58 
59 #define	BGPD_FLAG_REFLECTOR		0x0004
60 #define	BGPD_FLAG_NEXTHOP_BGP		0x0010
61 #define	BGPD_FLAG_NEXTHOP_DEFAULT	0x0020
62 #define	BGPD_FLAG_DECISION_MASK		0x0f00
63 #define	BGPD_FLAG_DECISION_ROUTEAGE	0x0100
64 #define	BGPD_FLAG_DECISION_TRANS_AS	0x0200
65 #define	BGPD_FLAG_DECISION_MED_ALWAYS	0x0400
66 #define	BGPD_FLAG_DECISION_ALL_PATHS	0x0800
67 #define	BGPD_FLAG_NO_AS_SET		0x1000
68 
69 #define	BGPD_LOG_UPDATES		0x0001
70 
71 #ifndef BGPD_USER
72 #define	BGPD_USER			"_bgpd"
73 #endif
74 
75 #ifndef SYSCONFDIR
76 #define SYSCONFDIR "/etc"
77 #endif
78 #define	CONFFILE			SYSCONFDIR "/bgpd.conf"
79 
80 #ifndef RUNSTATEDIR
81 #define RUNSTATEDIR "/var/run"
82 #endif
83 #define	SOCKET_NAME			RUNSTATEDIR "/bgpd.sock"
84 
85 #define	F_BGPD_INSERTED		0x0001
86 #define	F_KERNEL		0x0002
87 #define	F_CONNECTED		0x0004
88 #define	F_NEXTHOP		0x0008
89 #define	F_DOWN			0x0010
90 #define	F_STATIC		0x0020
91 #define	F_DYNAMIC		0x0040
92 #define	F_REJECT		0x0080
93 #define	F_BLACKHOLE		0x0100
94 #define	F_LONGER		0x0200
95 #define	F_SHORTER		0x0400
96 #define	F_MPLS			0x0800
97 #define	F_CTL_DETAIL		0x1000	/* only set on requests */
98 #define	F_CTL_ADJ_IN		0x2000	/* only set on requests */
99 #define	F_CTL_ADJ_OUT		0x4000	/* only set on requests */
100 #define	F_CTL_ACTIVE		0x8000
101 #define	F_RTLABEL		0x10000
102 #define	F_CTL_SSV		0x20000	/* only used by bgpctl */
103 #define	F_CTL_INVALID		0x40000 /* only set on requests */
104 #define	F_CTL_OVS_VALID		0x80000
105 #define	F_CTL_OVS_INVALID	0x100000
106 #define	F_CTL_OVS_NOTFOUND	0x200000
107 #define	F_CTL_NEIGHBORS		0x400000 /* only used by bgpctl */
108 
109 /*
110  * Note that these numeric assignments differ from the numbers commonly
111  * used in route origin validation context.
112  */
113 #define	ROA_NOTFOUND		0x0	/* default */
114 #define	ROA_INVALID		0x1
115 #define	ROA_VALID		0x2
116 #define	ROA_MASK		0x3
117 
118 /*
119  * Limit the number of messages queued in the session engine.
120  * The SE will send an IMSG_XOFF messages to the RDE if the high water mark
121  * is reached. The RDE should then throttle this peer or control connection.
122  * Once the message queue in the SE drops below the low water mark an
123  * IMSG_XON message will be sent and the RDE will produce more messages again.
124  */
125 #define RDE_RUNNER_ROUNDS	100
126 #define SESS_MSG_HIGH_MARK	2000
127 #define SESS_MSG_LOW_MARK	500
128 #define CTL_MSG_HIGH_MARK	500
129 #define CTL_MSG_LOW_MARK	100
130 
131 enum bgpd_process {
132 	PROC_MAIN,
133 	PROC_SE,
134 	PROC_RDE,
135 	PROC_RTR,
136 };
137 
138 enum reconf_action {
139 	RECONF_NONE,
140 	RECONF_KEEP,
141 	RECONF_REINIT,
142 	RECONF_RELOAD,
143 	RECONF_DELETE
144 };
145 
146 /* Address Family Numbers as per RFC 1700 */
147 #define	AFI_UNSPEC	0
148 #define	AFI_IPv4	1
149 #define	AFI_IPv6	2
150 
151 /* Subsequent Address Family Identifier as per RFC 4760 */
152 #define	SAFI_NONE	0
153 #define	SAFI_UNICAST	1
154 #define	SAFI_MULTICAST	2
155 #define	SAFI_MPLS	4
156 #define	SAFI_MPLSVPN	128
157 
158 struct aid {
159 	u_int16_t	 afi;
160 	sa_family_t	 af;
161 	u_int8_t	 safi;
162 	char		*name;
163 };
164 
165 extern const struct aid aid_vals[];
166 
167 #define	AID_UNSPEC	0
168 #define	AID_INET	1
169 #define	AID_INET6	2
170 #define	AID_VPN_IPv4	3
171 #define	AID_VPN_IPv6	4
172 #define	AID_MAX		5
173 #define	AID_MIN		1	/* skip AID_UNSPEC since that is a dummy */
174 
175 #define AID_VALS	{					\
176 	/* afi, af, safii, name */				\
177 	{ AFI_UNSPEC, AF_UNSPEC, SAFI_NONE, "unspec"},		\
178 	{ AFI_IPv4, AF_INET, SAFI_UNICAST, "IPv4 unicast" },	\
179 	{ AFI_IPv6, AF_INET6, SAFI_UNICAST, "IPv6 unicast" },	\
180 	{ AFI_IPv4, AF_INET, SAFI_MPLSVPN, "IPv4 vpn" },	\
181 	{ AFI_IPv6, AF_INET6, SAFI_MPLSVPN, "IPv6 vpn" }	\
182 }
183 
184 #define AID_PTSIZE	{				\
185 	0,						\
186 	sizeof(struct pt_entry4),			\
187 	sizeof(struct pt_entry6),			\
188 	sizeof(struct pt_entry_vpn4),			\
189 	sizeof(struct pt_entry_vpn6)			\
190 }
191 
192 
193 #define BGP_MPLS_BOS	0x01
194 
195 struct bgpd_addr {
196 	union {
197 		struct in_addr		v4;
198 		struct in6_addr		v6;
199 		/* maximum size for a prefix is 256 bits */
200 	} ba;		    /* 128-bit address */
201 	u_int64_t	rd;		/* route distinguisher for VPN addrs */
202 	u_int32_t	scope_id;	/* iface scope id for v6 */
203 	u_int8_t	aid;
204 	u_int8_t	labellen;	/* size of the labelstack */
205 	u_int8_t	labelstack[18];	/* max that makes sense */
206 #define	v4	ba.v4
207 #define	v6	ba.v6
208 };
209 
210 #define	DEFAULT_LISTENER	0x01
211 #define	LISTENER_LISTENING	0x02
212 
213 struct listen_addr {
214 	TAILQ_ENTRY(listen_addr)	entry;
215 	struct sockaddr_storage		sa;
216 	int				fd;
217 	enum reconf_action		reconf;
218 	socklen_t			sa_len;
219 	u_int8_t			flags;
220 };
221 
222 TAILQ_HEAD(listen_addrs, listen_addr);
223 TAILQ_HEAD(filter_set_head, filter_set);
224 
225 struct peer;
226 RB_HEAD(peer_head, peer);
227 
228 struct l3vpn;
229 SIMPLEQ_HEAD(l3vpn_head, l3vpn);
230 
231 struct network;
232 TAILQ_HEAD(network_head, network);
233 
234 struct prefixset;
235 SIMPLEQ_HEAD(prefixset_head, prefixset);
236 struct prefixset_item;
237 RB_HEAD(prefixset_tree, prefixset_item);
238 
239 struct tentry_v4;
240 struct tentry_v6;
241 struct trie_head {
242 	struct tentry_v4	*root_v4;
243 	struct tentry_v6	*root_v6;
244 	int			 match_default_v4;
245 	int			 match_default_v6;
246 	size_t			 v4_cnt;
247 	size_t			 v6_cnt;
248 };
249 
250 struct rde_prefixset {
251 	char				name[SET_NAME_LEN];
252 	struct trie_head		th;
253 	SIMPLEQ_ENTRY(rde_prefixset)	entry;
254 	time_t				lastchange;
255 	int				dirty;
256 };
257 SIMPLEQ_HEAD(rde_prefixset_head, rde_prefixset);
258 
259 struct roa {
260 	RB_ENTRY(roa)	entry;
261 	uint8_t		aid;
262 	uint8_t		prefixlen;
263 	uint8_t		maxlen;
264 	uint8_t		pad;
265 	uint32_t	asnum;
266 	union {
267 		struct in_addr	inet;
268 		struct in6_addr	inet6;
269 	}		prefix;
270 };
271 
272 RB_HEAD(roa_tree, roa);
273 
274 struct set_table;
275 struct as_set;
276 SIMPLEQ_HEAD(as_set_head, as_set);
277 
278 struct filter_rule;
279 TAILQ_HEAD(filter_head, filter_rule);
280 
281 struct rtr_config;
282 SIMPLEQ_HEAD(rtr_config_head, rtr_config);
283 
284 struct bgpd_config {
285 	struct peer_head			 peers;
286 	struct l3vpn_head			 l3vpns;
287 	struct network_head			 networks;
288 	struct filter_head			*filters;
289 	struct listen_addrs			*listen_addrs;
290 	struct mrt_head				*mrt;
291 	struct prefixset_head			 prefixsets;
292 	struct prefixset_head			 originsets;
293 	struct roa_tree				 roa;
294 	struct rde_prefixset_head		 rde_prefixsets;
295 	struct rde_prefixset_head		 rde_originsets;
296 	struct as_set_head			 as_sets;
297 	struct rtr_config_head			 rtrs;
298 	char					*csock;
299 	char					*rcsock;
300 	int					 flags;
301 	int					 log;
302 	u_int					 default_tableid;
303 	u_int32_t				 bgpid;
304 	u_int32_t				 clusterid;
305 	u_int32_t				 as;
306 	u_int16_t				 short_as;
307 	u_int16_t				 holdtime;
308 	u_int16_t				 min_holdtime;
309 	u_int16_t				 connectretry;
310 	u_int8_t				 fib_priority;
311 };
312 
313 extern int cmd_opts;
314 
315 enum export_type {
316 	EXPORT_UNSET,
317 	EXPORT_NONE,
318 	EXPORT_DEFAULT_ROUTE
319 };
320 
321 enum enforce_as {
322 	ENFORCE_AS_UNDEF,
323 	ENFORCE_AS_OFF,
324 	ENFORCE_AS_ON
325 };
326 
327 enum auth_method {
328 	AUTH_NONE,
329 	AUTH_MD5SIG,
330 	AUTH_IPSEC_MANUAL_ESP,
331 	AUTH_IPSEC_MANUAL_AH,
332 	AUTH_IPSEC_IKE_ESP,
333 	AUTH_IPSEC_IKE_AH
334 };
335 
336 struct peer_auth {
337 	char			md5key[TCP_MD5_KEY_LEN];
338 	char			auth_key_in[IPSEC_AUTH_KEY_LEN];
339 	char			auth_key_out[IPSEC_AUTH_KEY_LEN];
340 	char			enc_key_in[IPSEC_ENC_KEY_LEN];
341 	char			enc_key_out[IPSEC_ENC_KEY_LEN];
342 	u_int32_t		spi_in;
343 	u_int32_t		spi_out;
344 	enum auth_method	method;
345 	u_int8_t		md5key_len;
346 	u_int8_t		auth_alg_in;
347 	u_int8_t		auth_alg_out;
348 	u_int8_t		auth_keylen_in;
349 	u_int8_t		auth_keylen_out;
350 	u_int8_t		enc_alg_in;
351 	u_int8_t		enc_alg_out;
352 	u_int8_t		enc_keylen_in;
353 	u_int8_t		enc_keylen_out;
354 };
355 
356 struct capabilities {
357 	struct {
358 		int16_t	timeout;	/* graceful restart timeout */
359 		int8_t	flags[AID_MAX];	/* graceful restart per AID flags */
360 		int8_t	restart;	/* graceful restart, RFC 4724 */
361 	}	grestart;
362 	int8_t	mp[AID_MAX];		/* multiprotocol extensions, RFC 4760 */
363 	int8_t	refresh;		/* route refresh, RFC 2918 */
364 	int8_t	as4byte;		/* 4-byte ASnum, RFC 4893 */
365 };
366 
367 #define	CAPA_GR_PRESENT		0x01
368 #define	CAPA_GR_RESTART		0x02
369 #define	CAPA_GR_FORWARD		0x04
370 #define	CAPA_GR_RESTARTING	0x08
371 
372 #define	CAPA_GR_TIMEMASK	0x0fff
373 #define	CAPA_GR_R_FLAG		0x8000
374 #define	CAPA_GR_F_FLAG		0x80
375 
376 struct peer_config {
377 	struct bgpd_addr	 remote_addr;
378 	struct bgpd_addr	 local_addr_v4;
379 	struct bgpd_addr	 local_addr_v6;
380 	struct peer_auth	 auth;
381 	struct capabilities	 capabilities;
382 	char			 group[PEER_DESCR_LEN];
383 	char			 descr[PEER_DESCR_LEN];
384 	char			 reason[REASON_LEN];
385 	char			 rib[PEER_DESCR_LEN];
386 	char			 if_depend[IFNAMSIZ];
387 	char			 demote_group[IFNAMSIZ];
388 	u_int32_t		 id;
389 	u_int32_t		 groupid;
390 	u_int32_t		 remote_as;
391 	u_int32_t		 local_as;
392 	u_int32_t		 max_prefix;
393 	u_int32_t		 max_out_prefix;
394 	enum export_type	 export_type;
395 	enum enforce_as		 enforce_as;
396 	enum enforce_as		 enforce_local_as;
397 	u_int16_t		 max_prefix_restart;
398 	u_int16_t		 max_out_prefix_restart;
399 	u_int16_t		 holdtime;
400 	u_int16_t		 min_holdtime;
401 	u_int16_t		 local_short_as;
402 	u_int8_t		 template;
403 	u_int8_t		 remote_masklen;
404 	u_int8_t		 ebgp;		/* 0 = ibgp else ebgp */
405 	u_int8_t		 distance;	/* 1 = direct, >1 = multihop */
406 	u_int8_t		 passive;
407 	u_int8_t		 down;
408 	u_int8_t		 announce_capa;
409 	u_int8_t		 reflector_client;
410 	u_int8_t		 ttlsec;	/* TTL security hack */
411 	u_int8_t		 flags;
412 };
413 
414 #define	PEER_ID_NONE		0
415 #define	PEER_ID_SELF		1
416 #define	PEER_ID_STATIC_MIN	2	/* exclude self */
417 #define	PEER_ID_STATIC_MAX	(UINT_MAX / 2)
418 #define	PEER_ID_DYN_MAX		UINT_MAX
419 
420 #define PEERFLAG_TRANS_AS	0x01
421 #define PEERFLAG_LOG_UPDATES	0x02
422 #define PEERFLAG_EVALUATE_ALL	0x04
423 #define PEERFLAG_NO_AS_SET	0x08
424 
425 enum network_type {
426 	NETWORK_DEFAULT,	/* from network statements */
427 	NETWORK_STATIC,
428 	NETWORK_CONNECTED,
429 	NETWORK_RTLABEL,
430 	NETWORK_MRTCLONE,
431 	NETWORK_PRIORITY,
432 	NETWORK_PREFIXSET,
433 };
434 
435 struct network_config {
436 	struct bgpd_addr	 prefix;
437 	struct filter_set_head	 attrset;
438 	char			 psname[SET_NAME_LEN];
439 	u_int64_t		 rd;
440 	u_int16_t		 rtlabel;
441 	enum network_type	 type;
442 	u_int8_t		 prefixlen;
443 	u_int8_t		 priority;
444 	u_int8_t		 old;	/* used for reloading */
445 };
446 
447 struct network {
448 	struct network_config		net;
449 	TAILQ_ENTRY(network)		entry;
450 };
451 
452 enum rtr_error {
453 	NO_ERROR = -1,
454 	CORRUPT_DATA = 0,
455 	INTERNAL_ERROR,
456 	NO_DATA_AVAILABLE,
457 	INVALID_REQUEST,
458 	UNSUPP_PROTOCOL_VERS,
459 	UNSUPP_PDU_TYPE,
460 	UNK_REC_WDRAWL,
461 	DUP_REC_RECV,
462 	UNEXP_PROTOCOL_VERS,
463 };
464 
465 struct rtr_config {
466 	SIMPLEQ_ENTRY(rtr_config)	entry;
467 	char				descr[PEER_DESCR_LEN];
468 	struct bgpd_addr		remote_addr;
469 	struct bgpd_addr		local_addr;
470 	u_int32_t			id;
471 	in_addr_t			remote_port;
472 };
473 
474 struct ctl_show_rtr {
475 	char			descr[PEER_DESCR_LEN];
476 	struct bgpd_addr	remote_addr;
477 	struct bgpd_addr	local_addr;
478 	uint32_t		serial;
479 	uint32_t		refresh;
480 	uint32_t		retry;
481 	uint32_t		expire;
482 	int			session_id;
483 	in_addr_t		remote_port;
484 	enum rtr_error 		last_sent_error;
485 	enum rtr_error		last_recv_error;
486 	char			last_sent_msg[REASON_LEN];
487 	char			last_recv_msg[REASON_LEN];
488 };
489 
490 enum imsg_type {
491 	IMSG_NONE,
492 	IMSG_CTL_END,
493 	IMSG_CTL_RELOAD,
494 	IMSG_CTL_FIB_COUPLE,
495 	IMSG_CTL_FIB_DECOUPLE,
496 	IMSG_CTL_NEIGHBOR_UP,
497 	IMSG_CTL_NEIGHBOR_DOWN,
498 	IMSG_CTL_NEIGHBOR_CLEAR,
499 	IMSG_CTL_NEIGHBOR_RREFRESH,
500 	IMSG_CTL_NEIGHBOR_DESTROY,
501 	IMSG_CTL_KROUTE,
502 	IMSG_CTL_KROUTE_ADDR,
503 	IMSG_CTL_RESULT,
504 	IMSG_CTL_SHOW_NEIGHBOR,
505 	IMSG_CTL_SHOW_NEXTHOP,
506 	IMSG_CTL_SHOW_INTERFACE,
507 	IMSG_CTL_SHOW_RIB,
508 	IMSG_CTL_SHOW_RIB_PREFIX,
509 	IMSG_CTL_SHOW_RIB_COMMUNITIES,
510 	IMSG_CTL_SHOW_RIB_ATTR,
511 	IMSG_CTL_SHOW_NETWORK,
512 	IMSG_CTL_SHOW_RIB_MEM,
513 	IMSG_CTL_SHOW_RIB_HASH,
514 	IMSG_CTL_SHOW_TERSE,
515 	IMSG_CTL_SHOW_TIMER,
516 	IMSG_CTL_LOG_VERBOSE,
517 	IMSG_CTL_SHOW_FIB_TABLES,
518 	IMSG_CTL_SHOW_SET,
519 	IMSG_CTL_SHOW_RTR,
520 	IMSG_CTL_TERMINATE,
521 	IMSG_NETWORK_ADD,
522 	IMSG_NETWORK_ASPATH,
523 	IMSG_NETWORK_ATTR,
524 	IMSG_NETWORK_REMOVE,
525 	IMSG_NETWORK_FLUSH,
526 	IMSG_NETWORK_DONE,
527 	IMSG_FILTER_SET,
528 	IMSG_SOCKET_CONN,
529 	IMSG_SOCKET_CONN_CTL,
530 	IMSG_SOCKET_CONN_RTR,
531 	IMSG_RECONF_CONF,
532 	IMSG_RECONF_RIB,
533 	IMSG_RECONF_PEER,
534 	IMSG_RECONF_FILTER,
535 	IMSG_RECONF_LISTENER,
536 	IMSG_RECONF_CTRL,
537 	IMSG_RECONF_VPN,
538 	IMSG_RECONF_VPN_EXPORT,
539 	IMSG_RECONF_VPN_IMPORT,
540 	IMSG_RECONF_VPN_DONE,
541 	IMSG_RECONF_PREFIX_SET,
542 	IMSG_RECONF_PREFIX_SET_ITEM,
543 	IMSG_RECONF_AS_SET,
544 	IMSG_RECONF_AS_SET_ITEMS,
545 	IMSG_RECONF_AS_SET_DONE,
546 	IMSG_RECONF_ORIGIN_SET,
547 	IMSG_RECONF_ROA_SET,
548 	IMSG_RECONF_ROA_ITEM,
549 	IMSG_RECONF_RTR_CONFIG,
550 	IMSG_RECONF_DRAIN,
551 	IMSG_RECONF_DONE,
552 	IMSG_UPDATE,
553 	IMSG_UPDATE_ERR,
554 	IMSG_SESSION_ADD,
555 	IMSG_SESSION_UP,
556 	IMSG_SESSION_DOWN,
557 	IMSG_SESSION_STALE,
558 	IMSG_SESSION_FLUSH,
559 	IMSG_SESSION_RESTARTED,
560 	IMSG_PFKEY_RELOAD,
561 	IMSG_MRT_OPEN,
562 	IMSG_MRT_REOPEN,
563 	IMSG_MRT_CLOSE,
564 	IMSG_KROUTE_CHANGE,
565 	IMSG_KROUTE_DELETE,
566 	IMSG_KROUTE_FLUSH,
567 	IMSG_NEXTHOP_ADD,
568 	IMSG_NEXTHOP_REMOVE,
569 	IMSG_NEXTHOP_UPDATE,
570 	IMSG_PFTABLE_ADD,
571 	IMSG_PFTABLE_REMOVE,
572 	IMSG_PFTABLE_COMMIT,
573 	IMSG_REFRESH,
574 	IMSG_IFINFO,
575 	IMSG_DEMOTE,
576 	IMSG_XON,
577 	IMSG_XOFF
578 };
579 
580 struct demote_msg {
581 	char		 demote_group[IFNAMSIZ];
582 	int		 level;
583 };
584 
585 enum ctl_results {
586 	CTL_RES_OK,
587 	CTL_RES_NOSUCHPEER,
588 	CTL_RES_DENIED,
589 	CTL_RES_NOCAP,
590 	CTL_RES_PARSE_ERROR,
591 	CTL_RES_PENDING,
592 	CTL_RES_NOMEM,
593 	CTL_RES_BADPEER,
594 	CTL_RES_BADSTATE,
595 	CTL_RES_NOSUCHRIB
596 };
597 
598 /* needed for session.h parse prototype */
599 LIST_HEAD(mrt_head, mrt);
600 
601 /* error codes and subcodes needed in SE and RDE */
602 enum err_codes {
603 	ERR_HEADER = 1,
604 	ERR_OPEN,
605 	ERR_UPDATE,
606 	ERR_HOLDTIMEREXPIRED,
607 	ERR_FSM,
608 	ERR_CEASE
609 };
610 
611 enum suberr_update {
612 	ERR_UPD_UNSPECIFIC,
613 	ERR_UPD_ATTRLIST,
614 	ERR_UPD_UNKNWN_WK_ATTR,
615 	ERR_UPD_MISSNG_WK_ATTR,
616 	ERR_UPD_ATTRFLAGS,
617 	ERR_UPD_ATTRLEN,
618 	ERR_UPD_ORIGIN,
619 	ERR_UPD_LOOP,
620 	ERR_UPD_NEXTHOP,
621 	ERR_UPD_OPTATTR,
622 	ERR_UPD_NETWORK,
623 	ERR_UPD_ASPATH
624 };
625 
626 enum suberr_cease {
627 	ERR_CEASE_MAX_PREFIX = 1,
628 	ERR_CEASE_ADMIN_DOWN,
629 	ERR_CEASE_PEER_UNCONF,
630 	ERR_CEASE_ADMIN_RESET,
631 	ERR_CEASE_CONN_REJECT,
632 	ERR_CEASE_OTHER_CHANGE,
633 	ERR_CEASE_COLLISION,
634 	ERR_CEASE_RSRC_EXHAUST,
635 	ERR_CEASE_HARD_RESET,
636 	ERR_CEASE_MAX_SENT_PREFIX
637 };
638 
639 struct kroute_node;
640 struct kroute6_node;
641 struct knexthop_node;
642 struct kredist_node;
643 RB_HEAD(kroute_tree, kroute_node);
644 RB_HEAD(kroute6_tree, kroute6_node);
645 RB_HEAD(knexthop_tree, knexthop_node);
646 RB_HEAD(kredist_tree, kredist_node);
647 
648 struct ktable {
649 	char			 descr[PEER_DESCR_LEN];
650 	struct kroute_tree	 krt;
651 	struct kroute6_tree	 krt6;
652 	struct knexthop_tree	 knt;
653 	struct kredist_tree	 kredist;
654 	struct network_head	 krn;
655 	u_int			 rtableid;
656 	u_int			 nhtableid; /* rdomain id for nexthop lookup */
657 	int			 nhrefcnt;  /* refcnt for nexthop table */
658 	enum reconf_action	 state;
659 	u_int8_t		 fib_conf;  /* configured FIB sync flag */
660 	u_int8_t		 fib_sync;  /* is FIB synced with kernel? */
661 };
662 
663 struct kroute_full {
664 	struct bgpd_addr	prefix;
665 	struct bgpd_addr	nexthop;
666 	char			label[RTLABEL_LEN];
667 	u_int16_t		labelid;
668 	u_int16_t		flags;
669 	u_short			ifindex;
670 	u_int8_t		prefixlen;
671 	u_int8_t		priority;
672 };
673 
674 struct kroute {
675 	struct in_addr	prefix;
676 	struct in_addr	nexthop;
677 	u_int32_t	mplslabel;
678 	u_int16_t	flags;
679 	u_int16_t	labelid;
680 	u_short		ifindex;
681 	u_int8_t	prefixlen;
682 	u_int8_t	priority;
683 };
684 
685 struct kroute6 {
686 	struct in6_addr	prefix;
687 	struct in6_addr	nexthop;
688 	u_int32_t	mplslabel;
689 	u_int16_t	flags;
690 	u_int16_t	labelid;
691 	u_short		ifindex;
692 	u_int8_t	prefixlen;
693 	u_int8_t	priority;
694 };
695 
696 struct kroute_nexthop {
697 	struct bgpd_addr	nexthop;
698 	struct bgpd_addr	gateway;
699 	struct bgpd_addr	net;
700 	u_int8_t		valid;
701 	u_int8_t		connected;
702 	u_int8_t		netlen;
703 };
704 
705 struct kif {
706 	char			 ifname[IFNAMSIZ];
707 	u_int64_t		 baudrate;
708 	u_int			 rdomain;
709 	int			 flags;
710 	u_short			 ifindex;
711 	u_int8_t		 if_type;
712 	u_int8_t		 link_state;
713 	u_int8_t		 nh_reachable;	/* for nexthop verification */
714 	u_int8_t		 depend_state;	/* for session depend on */
715 };
716 
717 struct session_up {
718 	struct bgpd_addr	local_v4_addr;
719 	struct bgpd_addr	local_v6_addr;
720 	struct bgpd_addr	remote_addr;
721 	struct capabilities	capa;
722 	u_int32_t		remote_bgpid;
723 	u_int16_t		short_as;
724 };
725 
726 struct pftable_msg {
727 	struct bgpd_addr	addr;
728 	char			pftable[PFTABLE_LEN];
729 	u_int8_t		len;
730 };
731 
732 struct ctl_show_interface {
733 	char			 ifname[IFNAMSIZ];
734 	char			 linkstate[32];
735 	char			 media[32];
736 	u_int64_t		 baudrate;
737 	u_int			 rdomain;
738 	u_int8_t		 nh_reachable;
739 	u_int8_t		 is_up;
740 };
741 
742 struct ctl_show_nexthop {
743 	struct bgpd_addr		addr;
744 	struct ctl_show_interface	iface;
745 	union {
746 		struct kroute		kr4;
747 		struct kroute6		kr6;
748 	} kr;
749 	u_int8_t			valid;
750 	u_int8_t			krvalid;
751 };
752 
753 struct ctl_show_set {
754 	char			name[SET_NAME_LEN];
755 	time_t			lastchange;
756 	size_t			v4_cnt;
757 	size_t			v6_cnt;
758 	size_t			as_cnt;
759 	enum {
760 		ASNUM_SET,
761 		PREFIX_SET,
762 		ORIGIN_SET,
763 		ROA_SET,
764 	}			type;
765 };
766 
767 struct ctl_neighbor {
768 	struct bgpd_addr	addr;
769 	char			descr[PEER_DESCR_LEN];
770 	char			reason[REASON_LEN];
771 	int			show_timers;
772 	int			is_group;
773 };
774 
775 #define	F_PREF_ELIGIBLE	0x01
776 #define	F_PREF_ACTIVE	0x02
777 #define	F_PREF_INTERNAL	0x04
778 #define	F_PREF_ANNOUNCE	0x08
779 #define	F_PREF_STALE	0x10
780 #define	F_PREF_INVALID	0x20
781 
782 struct ctl_show_rib {
783 	struct bgpd_addr	true_nexthop;
784 	struct bgpd_addr	exit_nexthop;
785 	struct bgpd_addr	prefix;
786 	struct bgpd_addr	remote_addr;
787 	char			descr[PEER_DESCR_LEN];
788 	time_t			age;
789 	u_int32_t		remote_id;
790 	u_int32_t		local_pref;
791 	u_int32_t		med;
792 	u_int32_t		weight;
793 	u_int32_t		flags;
794 	u_int8_t		prefixlen;
795 	u_int8_t		origin;
796 	u_int8_t		validation_state;
797 	/* plus an aspath */
798 };
799 
800 enum as_spec {
801 	AS_UNDEF,
802 	AS_ALL,
803 	AS_SOURCE,
804 	AS_TRANSIT,
805 	AS_PEER,
806 	AS_EMPTY
807 };
808 
809 enum aslen_spec {
810 	ASLEN_NONE,
811 	ASLEN_MAX,
812 	ASLEN_SEQ
813 };
814 
815 #define AS_FLAG_NEIGHBORAS	0x01
816 #define AS_FLAG_AS_SET_NAME	0x02
817 #define AS_FLAG_AS_SET		0x04
818 
819 struct filter_as {
820 	char		 name[SET_NAME_LEN];
821 	struct as_set	*aset;
822 	u_int32_t	 as_min;
823 	u_int32_t	 as_max;
824 	enum as_spec	 type;
825 	u_int8_t	 flags;
826 	u_int8_t	 op;
827 };
828 
829 struct filter_aslen {
830 	u_int		aslen;
831 	enum aslen_spec	type;
832 };
833 
834 #define PREFIXSET_FLAG_FILTER	0x01
835 #define PREFIXSET_FLAG_DIRTY	0x02	/* prefix-set changed at reload */
836 #define PREFIXSET_FLAG_OPS	0x04	/* indiv. prefixes have prefixlenops */
837 #define PREFIXSET_FLAG_LONGER	0x08	/* filter all prefixes with or-longer */
838 
839 struct filter_prefixset {
840 	int			 flags;
841 	char			 name[SET_NAME_LEN];
842 	struct rde_prefixset	*ps;
843 };
844 
845 struct filter_originset {
846 	char			 name[SET_NAME_LEN];
847 	struct rde_prefixset	*ps;
848 };
849 
850 struct filter_ovs {
851 	u_int8_t		 validity;
852 	u_int8_t		 is_set;
853 };
854 
855 /*
856  * Communities are encoded depending on their type. The low byte of flags
857  * is the COMMUNITY_TYPE (BASIC, LARGE, EXT). BASIC encoding is just using
858  * data1 and data2, LARGE uses all data fields and EXT is also using all
859  * data fields. The 4-byte flags fields consists of up to 3 data flags
860  * for e.g. COMMUNITY_ANY and the low byte is the community type.
861  * If flags is 0 the community struct is unused. If the upper 24bit of
862  * flags is 0 a fast compare can be used.
863  * The code uses a type cast to u_int8_t to access the type.
864  */
865 struct community {
866 	u_int32_t	flags;
867 	u_int32_t	data1;
868 	u_int32_t	data2;
869 	u_int32_t	data3;
870 };
871 
872 struct ctl_show_rib_request {
873 	char			rib[PEER_DESCR_LEN];
874 	struct ctl_neighbor	neighbor;
875 	struct bgpd_addr	prefix;
876 	struct filter_as	as;
877 	struct community	community;
878 	u_int32_t		flags;
879 	u_int8_t		validation_state;
880 	pid_t			pid;
881 	enum imsg_type		type;
882 	u_int8_t		prefixlen;
883 	u_int8_t		aid;
884 };
885 
886 enum filter_actions {
887 	ACTION_NONE,
888 	ACTION_ALLOW,
889 	ACTION_DENY
890 };
891 
892 enum directions {
893 	DIR_IN = 1,
894 	DIR_OUT
895 };
896 
897 enum from_spec {
898 	FROM_ALL,
899 	FROM_ADDRESS,
900 	FROM_DESCR,
901 	FROM_GROUP
902 };
903 
904 enum comp_ops {
905 	OP_NONE,
906 	OP_RANGE,
907 	OP_XRANGE,
908 	OP_EQ,
909 	OP_NE,
910 	OP_LE,
911 	OP_LT,
912 	OP_GE,
913 	OP_GT
914 };
915 
916 struct filter_peers {
917 	u_int32_t	peerid;
918 	u_int32_t	groupid;
919 	u_int32_t	remote_as;
920 	u_int16_t	ribid;
921 	u_int8_t	ebgp;
922 	u_int8_t	ibgp;
923 };
924 
925 /* special community type, keep in sync with the attribute type */
926 #define	COMMUNITY_TYPE_NONE		0
927 #define	COMMUNITY_TYPE_BASIC		8
928 #define	COMMUNITY_TYPE_EXT		16
929 #define	COMMUNITY_TYPE_LARGE		32
930 
931 #define	COMMUNITY_ANY			1
932 #define	COMMUNITY_NEIGHBOR_AS		2
933 #define	COMMUNITY_LOCAL_AS		3
934 
935 /* wellknown community definitions */
936 #define	COMMUNITY_WELLKNOWN		0xffff
937 #define	COMMUNITY_GRACEFUL_SHUTDOWN	0x0000  /* RFC 8326 */
938 #define	COMMUNITY_BLACKHOLE		0x029A	/* RFC 7999 */
939 #define	COMMUNITY_NO_EXPORT		0xff01
940 #define	COMMUNITY_NO_ADVERTISE		0xff02
941 #define	COMMUNITY_NO_EXPSUBCONFED	0xff03
942 #define	COMMUNITY_NO_PEER		0xff04	/* RFC 3765 */
943 
944 /* extended community definitions */
945 #define EXT_COMMUNITY_IANA		0x80
946 #define EXT_COMMUNITY_NON_TRANSITIVE	0x40
947 #define EXT_COMMUNITY_VALUE		0x3f
948 /* extended types transitive */
949 #define EXT_COMMUNITY_TRANS_TWO_AS	0x00	/* 2 octet AS specific */
950 #define EXT_COMMUNITY_TRANS_IPV4	0x01	/* IPv4 specific */
951 #define EXT_COMMUNITY_TRANS_FOUR_AS	0x02	/* 4 octet AS specific */
952 #define EXT_COMMUNITY_TRANS_OPAQUE	0x03	/* opaque ext community */
953 #define EXT_COMMUNITY_TRANS_EVPN	0x06	/* EVPN RFC7432 */
954 /* extended types non-transitive */
955 #define EXT_COMMUNITY_NON_TRANS_TWO_AS	0x40	/* 2 octet AS specific */
956 #define EXT_COMMUNITY_NON_TRANS_IPV4	0x41	/* IPv4 specific */
957 #define EXT_COMMUNITY_NON_TRANS_FOUR_AS	0x42	/* 4 octet AS specific */
958 #define EXT_COMMUNITY_NON_TRANS_OPAQUE	0x43	/* opaque ext community */
959 #define EXT_COMMUNITY_UNKNOWN		-1
960 
961 /* BGP Origin Validation State Extended Community RFC8097 */
962 #define EXT_COMMUNITY_SUBTYPE_OVS	0
963 #define EXT_COMMUNITY_OVS_VALID		0
964 #define EXT_COMMUNITY_OVS_NOTFOUND	1
965 #define EXT_COMMUNITY_OVS_INVALID	2
966 
967 /* other handy defines */
968 #define EXT_COMMUNITY_OPAQUE_MAX	0xffffffffffffULL
969 #define EXT_COMMUNITY_FLAG_VALID	0x01
970 
971 struct ext_comm_pairs {
972 	short		type;
973 	u_int8_t	subtype;
974 	const char	*subname;
975 };
976 
977 #define IANA_EXT_COMMUNITIES	{				\
978 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x02, "rt" },		\
979 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x03, "soo" },		\
980 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x05, "odi" },		\
981 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x08, "bdc" },		\
982 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x09, "srcas" },		\
983 	{ EXT_COMMUNITY_TRANS_TWO_AS, 0x0a, "l2vid" },		\
984 								\
985 	{ EXT_COMMUNITY_TRANS_FOUR_AS, 0x02, "rt" },		\
986 	{ EXT_COMMUNITY_TRANS_FOUR_AS, 0x03, "soo" },		\
987 	{ EXT_COMMUNITY_TRANS_FOUR_AS, 0x05, "odi" },		\
988 	{ EXT_COMMUNITY_TRANS_FOUR_AS, 0x08, "bdc" },		\
989 	{ EXT_COMMUNITY_TRANS_FOUR_AS, 0x09, "srcas" },		\
990 								\
991 	{ EXT_COMMUNITY_TRANS_IPV4, 0x02, "rt" },		\
992 	{ EXT_COMMUNITY_TRANS_IPV4, 0x03, "soo" },		\
993 	{ EXT_COMMUNITY_TRANS_IPV4, 0x05, "odi" },		\
994 	{ EXT_COMMUNITY_TRANS_IPV4, 0x07, "ori" },		\
995 	{ EXT_COMMUNITY_TRANS_IPV4, 0x0a, "l2vid" },		\
996 	{ EXT_COMMUNITY_TRANS_IPV4, 0x0b, "vrfri" },		\
997 								\
998 	{ EXT_COMMUNITY_TRANS_OPAQUE, 0x06, "ort" },		\
999 	{ EXT_COMMUNITY_TRANS_OPAQUE, 0x0d, "defgw" },		\
1000 								\
1001 	{ EXT_COMMUNITY_NON_TRANS_OPAQUE, EXT_COMMUNITY_SUBTYPE_OVS, "ovs" }, \
1002 								\
1003 	{ EXT_COMMUNITY_TRANS_EVPN, 0x00, "mac-mob" },		\
1004 	{ EXT_COMMUNITY_TRANS_EVPN, 0x01, "esi-lab" },		\
1005 	{ EXT_COMMUNITY_TRANS_EVPN, 0x02, "esi-rt" },		\
1006 								\
1007 	{ 0 }							\
1008 }
1009 
1010 extern const struct ext_comm_pairs iana_ext_comms[];
1011 
1012 struct filter_prefix {
1013 	struct bgpd_addr	addr;
1014 	u_int8_t		op;
1015 	u_int8_t		len;
1016 	u_int8_t		len_min;
1017 	u_int8_t		len_max;
1018 };
1019 
1020 struct filter_nexthop {
1021 	struct bgpd_addr	addr;
1022 	u_int8_t		flags;
1023 #define FILTER_NEXTHOP_ADDR	1
1024 #define FILTER_NEXTHOP_NEIGHBOR	2
1025 };
1026 
1027 struct filter_match {
1028 	struct filter_prefix		prefix;
1029 	struct filter_nexthop		nexthop;
1030 	struct filter_as		as;
1031 	struct filter_aslen		aslen;
1032 	struct community		community[MAX_COMM_MATCH];
1033 	struct filter_prefixset		prefixset;
1034 	struct filter_originset		originset;
1035 	struct filter_ovs		ovs;
1036 };
1037 
1038 struct filter_rule {
1039 	TAILQ_ENTRY(filter_rule)	entry;
1040 	char				rib[PEER_DESCR_LEN];
1041 	struct filter_peers		peer;
1042 	struct filter_match		match;
1043 	struct filter_set_head		set;
1044 #define RDE_FILTER_SKIP_DIR		0
1045 #define RDE_FILTER_SKIP_GROUPID		1
1046 #define RDE_FILTER_SKIP_REMOTE_AS	2
1047 #define RDE_FILTER_SKIP_PEERID		3
1048 #define RDE_FILTER_SKIP_COUNT		4
1049 	struct filter_rule		*skip[RDE_FILTER_SKIP_COUNT];
1050 	enum filter_actions		action;
1051 	enum directions			dir;
1052 	u_int8_t			quick;
1053 };
1054 
1055 enum action_types {
1056 	ACTION_SET_LOCALPREF,
1057 	ACTION_SET_RELATIVE_LOCALPREF,
1058 	ACTION_SET_MED,
1059 	ACTION_SET_RELATIVE_MED,
1060 	ACTION_SET_WEIGHT,
1061 	ACTION_SET_RELATIVE_WEIGHT,
1062 	ACTION_SET_PREPEND_SELF,
1063 	ACTION_SET_PREPEND_PEER,
1064 	ACTION_SET_AS_OVERRIDE,
1065 	ACTION_SET_NEXTHOP,
1066 	ACTION_SET_NEXTHOP_REF,
1067 	ACTION_SET_NEXTHOP_REJECT,
1068 	ACTION_SET_NEXTHOP_BLACKHOLE,
1069 	ACTION_SET_NEXTHOP_NOMODIFY,
1070 	ACTION_SET_NEXTHOP_SELF,
1071 	ACTION_DEL_COMMUNITY,
1072 	ACTION_SET_COMMUNITY,
1073 	ACTION_PFTABLE,
1074 	ACTION_PFTABLE_ID,
1075 	ACTION_RTLABEL,
1076 	ACTION_RTLABEL_ID,
1077 	ACTION_SET_ORIGIN
1078 };
1079 
1080 struct nexthop;
1081 struct filter_set {
1082 	TAILQ_ENTRY(filter_set)		entry;
1083 	union {
1084 		u_int8_t			 prepend;
1085 		u_int16_t			 id;
1086 		u_int32_t			 metric;
1087 		int32_t				 relative;
1088 		struct bgpd_addr		 nexthop;
1089 		struct nexthop			*nh_ref;
1090 		struct community		 community;
1091 		char				 pftable[PFTABLE_LEN];
1092 		char				 rtlabel[RTLABEL_LEN];
1093 		u_int8_t			 origin;
1094 	} action;
1095 	enum action_types		type;
1096 };
1097 
1098 struct roa_set {
1099 	u_int32_t	as;	/* must be first */
1100 	u_int32_t	maxlen;	/* change type for better struct layout */
1101 };
1102 
1103 struct prefixset_item {
1104 	struct filter_prefix		p;
1105 	RB_ENTRY(prefixset_item)	entry;
1106 };
1107 
1108 struct prefixset {
1109 	int				 sflags;
1110 	char				 name[SET_NAME_LEN];
1111 	struct prefixset_tree		 psitems;
1112 	struct roa_tree			 roaitems;
1113 	SIMPLEQ_ENTRY(prefixset)	 entry;
1114 };
1115 
1116 struct as_set {
1117 	char				 name[SET_NAME_LEN];
1118 	SIMPLEQ_ENTRY(as_set)		 entry;
1119 	struct set_table		*set;
1120 	time_t				 lastchange;
1121 	int				 dirty;
1122 };
1123 
1124 struct l3vpn {
1125 	SIMPLEQ_ENTRY(l3vpn)		entry;
1126 	char				descr[PEER_DESCR_LEN];
1127 	char				ifmpe[IFNAMSIZ];
1128 	struct filter_set_head		import;
1129 	struct filter_set_head		export;
1130 	struct network_head		net_l;
1131 	u_int64_t			rd;
1132 	u_int				rtableid;
1133 	u_int				label;
1134 	int				flags;
1135 };
1136 
1137 struct rde_rib {
1138 	SIMPLEQ_ENTRY(rde_rib)	entry;
1139 	char			name[PEER_DESCR_LEN];
1140 	u_int			rtableid;
1141 	u_int16_t		id;
1142 	u_int16_t		flags;
1143 };
1144 SIMPLEQ_HEAD(rib_names, rde_rib);
1145 extern struct rib_names ribnames;
1146 
1147 /* rde_rib flags */
1148 #define F_RIB_LOCAL		0x0001
1149 #define F_RIB_NOEVALUATE	0x0002
1150 #define F_RIB_NOFIB		0x0004
1151 #define F_RIB_NOFIBSYNC		0x0008
1152 
1153 /* 4-byte magic AS number */
1154 #define AS_TRANS	23456
1155 /* AS_NONE for origin validation */
1156 #define AS_NONE		0
1157 
1158 struct rde_memstats {
1159 	long long	path_cnt;
1160 	long long	path_refs;
1161 	long long	prefix_cnt;
1162 	long long	rib_cnt;
1163 	long long	pt_cnt[AID_MAX];
1164 	long long	nexthop_cnt;
1165 	long long	aspath_cnt;
1166 	long long	aspath_size;
1167 	long long	aspath_refs;
1168 	long long	comm_cnt;
1169 	long long	comm_nmemb;
1170 	long long	comm_size;
1171 	long long	comm_refs;
1172 	long long	attr_cnt;
1173 	long long	attr_refs;
1174 	long long	attr_data;
1175 	long long	attr_dcnt;
1176 	long long	aset_cnt;
1177 	long long	aset_size;
1178 	long long	aset_nmemb;
1179 	long long	pset_cnt;
1180 	long long	pset_size;
1181 };
1182 
1183 struct rde_hashstats {
1184 	char		name[16];
1185 	long long	num;
1186 	long long	min;
1187 	long long	max;
1188 	long long	sum;
1189 	long long	sumq;
1190 };
1191 
1192 #define	MRT_FILE_LEN	512
1193 #define	MRT2MC(x)	((struct mrt_config *)(x))
1194 
1195 enum mrt_type {
1196 	MRT_NONE,
1197 	MRT_TABLE_DUMP,
1198 	MRT_TABLE_DUMP_MP,
1199 	MRT_TABLE_DUMP_V2,
1200 	MRT_ALL_IN,
1201 	MRT_ALL_OUT,
1202 	MRT_UPDATE_IN,
1203 	MRT_UPDATE_OUT
1204 };
1205 
1206 enum mrt_state {
1207 	MRT_STATE_RUNNING,
1208 	MRT_STATE_OPEN,
1209 	MRT_STATE_REOPEN,
1210 	MRT_STATE_REMOVE
1211 };
1212 
1213 struct mrt {
1214 	char			rib[PEER_DESCR_LEN];
1215 	struct msgbuf		wbuf;
1216 	LIST_ENTRY(mrt)		entry;
1217 	u_int32_t		peer_id;
1218 	u_int32_t		group_id;
1219 	enum mrt_type		type;
1220 	enum mrt_state		state;
1221 	u_int16_t		seqnum;
1222 };
1223 
1224 struct mrt_config {
1225 	struct mrt		conf;
1226 	char			name[MRT_FILE_LEN];	/* base file name */
1227 	char			file[MRT_FILE_LEN];	/* actual file name */
1228 	time_t			ReopenTimer;
1229 	int			ReopenTimerInterval;
1230 };
1231 
1232 /* prototypes */
1233 /* bgpd.c */
1234 void		 send_nexthop_update(struct kroute_nexthop *);
1235 void		 send_imsg_session(int, pid_t, void *, u_int16_t);
1236 int		 send_network(int, struct network_config *,
1237 		     struct filter_set_head *);
1238 int		 bgpd_filternexthop(struct kroute *, struct kroute6 *);
1239 void		 set_pollfd(struct pollfd *, struct imsgbuf *);
1240 int		 handle_pollfd(struct pollfd *, struct imsgbuf *);
1241 
1242 /* control.c */
1243 int	control_imsg_relay(struct imsg *);
1244 
1245 /* config.c */
1246 struct bgpd_config	*new_config(void);
1247 void		copy_config(struct bgpd_config *, struct bgpd_config *);
1248 void		free_l3vpns(struct l3vpn_head *);
1249 void		free_config(struct bgpd_config *);
1250 void		free_prefixsets(struct prefixset_head *);
1251 void		free_rde_prefixsets(struct rde_prefixset_head *);
1252 void		free_prefixtree(struct prefixset_tree *);
1253 void		free_roatree(struct roa_tree *);
1254 void		free_rtrs(struct rtr_config_head *);
1255 void		filterlist_free(struct filter_head *);
1256 int		host(const char *, struct bgpd_addr *, u_int8_t *);
1257 u_int32_t	get_bgpid(void);
1258 void		expand_networks(struct bgpd_config *);
1259 RB_PROTOTYPE(prefixset_tree, prefixset_item, entry, prefixset_cmp);
1260 int		roa_cmp(struct roa *, struct roa *);
1261 RB_PROTOTYPE(roa_tree, roa, entry, roa_cmp);
1262 
1263 /* kroute.c */
1264 int		 kr_init(int *);
1265 int		 ktable_update(u_int, char *, int, u_int8_t);
1266 void		 ktable_preload(void);
1267 void		 ktable_postload(u_int8_t);
1268 int		 ktable_exists(u_int, u_int *);
1269 int		 kr_change(u_int, struct kroute_full *,  u_int8_t);
1270 int		 kr_delete(u_int, struct kroute_full *, u_int8_t);
1271 int		 kr_flush(u_int);
1272 void		 kr_shutdown(u_int8_t, u_int);
1273 void		 kr_fib_couple(u_int, u_int8_t);
1274 void		 kr_fib_couple_all(u_int8_t);
1275 void		 kr_fib_decouple(u_int, u_int8_t);
1276 void		 kr_fib_decouple_all(u_int8_t);
1277 void		 kr_fib_update_prio_all(u_int8_t);
1278 int		 kr_dispatch_msg(u_int rdomain);
1279 int		 kr_nexthop_add(u_int32_t, struct bgpd_addr *,
1280 		    struct bgpd_config *);
1281 void		 kr_nexthop_delete(u_int32_t, struct bgpd_addr *,
1282 		    struct bgpd_config *);
1283 void		 kr_show_route(struct imsg *);
1284 void		 kr_ifinfo(char *);
1285 void		 kr_net_reload(u_int, u_int64_t, struct network_head *);
1286 int		 kr_reload(void);
1287 struct in6_addr	*prefixlen2mask6(u_int8_t prefixlen);
1288 int		 get_mpe_config(const char *, u_int *, u_int *);
1289 
1290 /* log.c */
1291 void		 log_peer_info(const struct peer_config *, const char *, ...)
1292 			__attribute__((__format__ (printf, 2, 3)));
1293 void		 log_peer_warn(const struct peer_config *, const char *, ...)
1294 			__attribute__((__format__ (printf, 2, 3)));
1295 void		 log_peer_warnx(const struct peer_config *, const char *, ...)
1296 			__attribute__((__format__ (printf, 2, 3)));
1297 
1298 /* mrt.c */
1299 void		 mrt_clear_seq(void);
1300 void		 mrt_write(struct mrt *);
1301 void		 mrt_clean(struct mrt *);
1302 void		 mrt_init(struct imsgbuf *, struct imsgbuf *);
1303 time_t		 mrt_timeout(struct mrt_head *);
1304 void		 mrt_reconfigure(struct mrt_head *);
1305 void		 mrt_handler(struct mrt_head *);
1306 struct mrt	*mrt_get(struct mrt_head *, struct mrt *);
1307 void		 mrt_mergeconfig(struct mrt_head *, struct mrt_head *);
1308 
1309 /* name2id.c */
1310 u_int16_t	 rib_name2id(const char *);
1311 const char	*rib_id2name(u_int16_t);
1312 void		 rib_unref(u_int16_t);
1313 void		 rib_ref(u_int16_t);
1314 u_int16_t	 rtlabel_name2id(const char *);
1315 const char	*rtlabel_id2name(u_int16_t);
1316 void		 rtlabel_unref(u_int16_t);
1317 u_int16_t	 rtlabel_ref(u_int16_t);
1318 u_int16_t	 pftable_name2id(const char *);
1319 const char	*pftable_id2name(u_int16_t);
1320 void		 pftable_unref(u_int16_t);
1321 u_int16_t	 pftable_ref(u_int16_t);
1322 
1323 /* parse.y */
1324 int		 	cmdline_symset(char *);
1325 struct prefixset	*find_prefixset(char *, struct prefixset_head *);
1326 struct bgpd_config	*parse_config(char *, struct peer_head *,
1327 			    struct rtr_config_head *);
1328 
1329 /* pftable.c */
1330 int	pftable_exists(const char *);
1331 int	pftable_add(const char *);
1332 int	pftable_clear_all(void);
1333 int	pftable_addr_add(struct pftable_msg *);
1334 int	pftable_addr_remove(struct pftable_msg *);
1335 int	pftable_commit(void);
1336 
1337 /* rde_filter.c */
1338 void	filterset_free(struct filter_set_head *);
1339 int	filterset_cmp(struct filter_set *, struct filter_set *);
1340 void	filterset_move(struct filter_set_head *, struct filter_set_head *);
1341 void	filterset_copy(struct filter_set_head *, struct filter_set_head *);
1342 const char	*filterset_name(enum action_types);
1343 
1344 /* rde_sets.c */
1345 struct as_set	*as_sets_lookup(struct as_set_head *, const char *);
1346 struct as_set	*as_sets_new(struct as_set_head *, const char *, size_t,
1347 		    size_t);
1348 void		 as_sets_free(struct as_set_head *);
1349 void		 as_sets_mark_dirty(struct as_set_head *, struct as_set_head *);
1350 int		 as_set_match(const struct as_set *, u_int32_t);
1351 
1352 struct set_table	*set_new(size_t, size_t);
1353 void			 set_free(struct set_table *);
1354 int			 set_add(struct set_table *, void *, size_t);
1355 void			*set_get(struct set_table *, size_t *);
1356 void			 set_prep(struct set_table *);
1357 void			*set_match(const struct set_table *, u_int32_t);
1358 int			 set_equal(const struct set_table *,
1359 			    const struct set_table *);
1360 size_t			 set_nmemb(const struct set_table *);
1361 
1362 /* rde_trie.c */
1363 int	trie_add(struct trie_head *, struct bgpd_addr *, u_int8_t, u_int8_t,
1364 	    u_int8_t);
1365 int	trie_roa_add(struct trie_head *, struct roa *);
1366 void	trie_free(struct trie_head *);
1367 int	trie_match(struct trie_head *, struct bgpd_addr *, u_int8_t, int);
1368 int	trie_roa_check(struct trie_head *, struct bgpd_addr *, u_int8_t,
1369 	    u_int32_t);
1370 void	trie_dump(struct trie_head *);
1371 int	trie_equal(struct trie_head *, struct trie_head *);
1372 
1373 /* timer.c */
1374 time_t			 getmonotime(void);
1375 
1376 /* util.c */
1377 const char	*log_addr(const struct bgpd_addr *);
1378 const char	*log_in6addr(const struct in6_addr *);
1379 const char	*log_sockaddr(struct sockaddr *, socklen_t);
1380 const char	*log_as(u_int32_t);
1381 const char	*log_rd(u_int64_t);
1382 const char	*log_ext_subtype(short, u_int8_t);
1383 const char	*log_reason(const char *);
1384 const char	*log_rtr_error(enum rtr_error);
1385 int		 aspath_snprint(char *, size_t, void *, u_int16_t);
1386 int		 aspath_asprint(char **, void *, u_int16_t);
1387 size_t		 aspath_strlen(void *, u_int16_t);
1388 u_int32_t	 aspath_extract(const void *, int);
1389 int		 aspath_verify(void *, u_int16_t, int, int);
1390 #define		 AS_ERR_LEN	-1
1391 #define		 AS_ERR_TYPE	-2
1392 #define		 AS_ERR_BAD	-3
1393 #define		 AS_ERR_SOFT	-4
1394 u_char		*aspath_inflate(void *, u_int16_t, u_int16_t *);
1395 int		 nlri_get_prefix(u_char *, u_int16_t, struct bgpd_addr *,
1396 		     u_int8_t *);
1397 int		 nlri_get_prefix6(u_char *, u_int16_t, struct bgpd_addr *,
1398 		     u_int8_t *);
1399 int		 nlri_get_vpn4(u_char *, u_int16_t, struct bgpd_addr *,
1400 		     u_int8_t *, int);
1401 int		 nlri_get_vpn6(u_char *, u_int16_t, struct bgpd_addr *,
1402 		     u_int8_t *, int);
1403 int		 prefix_compare(const struct bgpd_addr *,
1404 		    const struct bgpd_addr *, int);
1405 in_addr_t	 prefixlen2mask(u_int8_t);
1406 void		 inet4applymask(struct in_addr *, const struct in_addr *, int);
1407 void		 inet6applymask(struct in6_addr *, const struct in6_addr *,
1408 		    int);
1409 const char	*aid2str(u_int8_t);
1410 int		 aid2afi(u_int8_t, u_int16_t *, u_int8_t *);
1411 int		 afi2aid(u_int16_t, u_int8_t, u_int8_t *);
1412 sa_family_t	 aid2af(u_int8_t);
1413 int		 af2aid(sa_family_t, u_int8_t, u_int8_t *);
1414 struct sockaddr	*addr2sa(const struct bgpd_addr *, u_int16_t, socklen_t *);
1415 void		 sa2addr(struct sockaddr *, struct bgpd_addr *, u_int16_t *);
1416 const char *	 get_baudrate(unsigned long long, char *);
1417 
1418 static const char * const log_procnames[] = {
1419 	"parent",
1420 	"SE",
1421 	"RDE",
1422 	"RTR"
1423 };
1424 
1425 /* logmsg.c and needed by bgpctl */
1426 static const char * const statenames[] = {
1427 	"None",
1428 	"Idle",
1429 	"Connect",
1430 	"Active",
1431 	"OpenSent",
1432 	"OpenConfirm",
1433 	"Established"
1434 };
1435 
1436 static const char * const msgtypenames[] = {
1437 	"NONE",
1438 	"OPEN",
1439 	"UPDATE",
1440 	"NOTIFICATION",
1441 	"KEEPALIVE",
1442 	"RREFRESH"
1443 };
1444 
1445 static const char * const eventnames[] = {
1446 	"None",
1447 	"Start",
1448 	"Stop",
1449 	"Connection opened",
1450 	"Connection closed",
1451 	"Connection open failed",
1452 	"Fatal error",
1453 	"ConnectRetryTimer expired",
1454 	"HoldTimer expired",
1455 	"KeepaliveTimer expired",
1456 	"SendHoldTimer expired",
1457 	"OPEN message received",
1458 	"KEEPALIVE message received",
1459 	"UPDATE message received",
1460 	"NOTIFICATION received"
1461 };
1462 
1463 static const char * const errnames[] = {
1464 	"none",
1465 	"Header error",
1466 	"error in OPEN message",
1467 	"error in UPDATE message",
1468 	"HoldTimer expired",
1469 	"Finite State Machine error",
1470 	"Cease"
1471 };
1472 
1473 static const char * const suberr_header_names[] = {
1474 	"none",
1475 	"synchronization error",
1476 	"wrong length",
1477 	"unknown message type"
1478 };
1479 
1480 static const char * const suberr_open_names[] = {
1481 	"none",
1482 	"version mismatch",
1483 	"AS unacceptable",
1484 	"BGPID invalid",
1485 	"optional parameter error",
1486 	"authentication error",
1487 	"unacceptable holdtime",
1488 	"unsupported capability",
1489 	"group membership conflict",	/* draft-ietf-idr-bgp-multisession-07 */
1490 	"group membership required"	/* draft-ietf-idr-bgp-multisession-07 */
1491 };
1492 
1493 static const char * const suberr_fsm_names[] = {
1494 	"unspecified error",
1495 	"received unexpected message in OpenSent",
1496 	"received unexpected message in OpenConfirm",
1497 	"received unexpected message in Established"
1498 };
1499 
1500 static const char * const suberr_update_names[] = {
1501 	"none",
1502 	"attribute list error",
1503 	"unknown well-known attribute",
1504 	"well-known attribute missing",
1505 	"attribute flags error",
1506 	"attribute length wrong",
1507 	"origin unacceptable",
1508 	"loop detected",
1509 	"nexthop unacceptable",
1510 	"optional attribute error",
1511 	"network unacceptable",
1512 	"AS-Path unacceptable"
1513 };
1514 
1515 static const char * const suberr_cease_names[] = {
1516 	"none",
1517 	"received max-prefix exceeded",
1518 	"administratively down",
1519 	"peer unconfigured",
1520 	"administrative reset",
1521 	"connection rejected",
1522 	"other config change",
1523 	"collision",
1524 	"resource exhaustion",
1525 	"hard reset",
1526 	"sent max-prefix exceeded"
1527 };
1528 
1529 static const char * const ctl_res_strerror[] = {
1530 	"no error",
1531 	"no such neighbor",
1532 	"permission denied",
1533 	"neighbor does not have this capability",
1534 	"config file has errors, reload failed",
1535 	"previous reload still running",
1536 	"out of memory",
1537 	"not a cloned peer",
1538 	"peer still active, down peer first",
1539 	"no such RIB"
1540 };
1541 
1542 static const char * const timernames[] = {
1543 	"None",
1544 	"ConnectRetryTimer",
1545 	"KeepaliveTimer",
1546 	"HoldTimer",
1547 	"SendHoldTimer",
1548 	"IdleHoldTimer",
1549 	"IdleHoldResetTimer",
1550 	"CarpUndemoteTimer",
1551 	"RestartTimer",
1552 	"RTR RefreshTimer",
1553 	"RTR RetryTimer",
1554 	"RTR ExpireTimer",
1555 	""
1556 };
1557 
1558 #endif /* __BGPD_H__ */
1559