xref: /openbsd-src/usr.sbin/ospfd/ospfd.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ospfd.h,v 1.95 2016/09/02 14:02:48 benno Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _OSPFD_H_
21 #define _OSPFD_H_
22 
23 #include <sys/queue.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <sys/tree.h>
27 #include <md5.h>
28 #include <net/if.h>
29 #include <netinet/in.h>
30 #include <event.h>
31 
32 #include <imsg.h>
33 #include "ospf.h"
34 
35 #define CONF_FILE		"/etc/ospfd.conf"
36 #define	OSPFD_SOCKET		"/var/run/ospfd.sock"
37 #define OSPFD_USER		"_ospfd"
38 
39 #define NBR_HASHSIZE		128
40 #define LSA_HASHSIZE		512
41 
42 #define NBR_IDSELF		1
43 #define NBR_CNTSTART		(NBR_IDSELF + 1)
44 
45 #define	READ_BUF_SIZE		65535
46 #define	PKG_DEF_SIZE		512	/* compromise */
47 #define	RT_BUF_SIZE		16384
48 #define	MAX_RTSOCK_BUF		128 * 1024
49 
50 #define	OSPFD_FLAG_NO_FIB_UPDATE	0x0001
51 #define	OSPFD_FLAG_STUB_ROUTER		0x0002
52 
53 #define	F_OSPFD_INSERTED	0x0001
54 #define	F_KERNEL		0x0002
55 #define	F_CONNECTED		0x0004
56 #define	F_STATIC		0x0008
57 #define	F_DYNAMIC		0x0010
58 #define	F_DOWN			0x0020
59 #define	F_REJECT		0x0040
60 #define	F_BLACKHOLE		0x0080
61 #define	F_REDISTRIBUTED		0x0100
62 #define	F_FORCED_NEXTHOP	0x0200
63 
64 static const char * const log_procnames[] = {
65 	"parent",
66 	"ospfe",
67 	"rde"
68 };
69 
70 struct imsgev {
71 	struct imsgbuf		 ibuf;
72 	void			(*handler)(int, short, void *);
73 	struct event		 ev;
74 	void			*data;
75 	short			 events;
76 };
77 
78 enum imsg_type {
79 	IMSG_NONE,
80 	IMSG_CTL_RELOAD,
81 	IMSG_CTL_SHOW_INTERFACE,
82 	IMSG_CTL_SHOW_DATABASE,
83 	IMSG_CTL_SHOW_DB_EXT,
84 	IMSG_CTL_SHOW_DB_NET,
85 	IMSG_CTL_SHOW_DB_RTR,
86 	IMSG_CTL_SHOW_DB_SELF,
87 	IMSG_CTL_SHOW_DB_SUM,
88 	IMSG_CTL_SHOW_DB_ASBR,
89 	IMSG_CTL_SHOW_DB_OPAQ,
90 	IMSG_CTL_SHOW_NBR,
91 	IMSG_CTL_SHOW_RIB,
92 	IMSG_CTL_SHOW_SUM,
93 	IMSG_CTL_SHOW_SUM_AREA,
94 	IMSG_CTL_FIB_COUPLE,
95 	IMSG_CTL_FIB_DECOUPLE,
96 	IMSG_CTL_FIB_RELOAD,
97 	IMSG_CTL_AREA,
98 	IMSG_CTL_IFACE,
99 	IMSG_CTL_KROUTE,
100 	IMSG_CTL_KROUTE_ADDR,
101 	IMSG_CTL_IFINFO,
102 	IMSG_CTL_END,
103 	IMSG_CTL_LOG_VERBOSE,
104 	IMSG_KROUTE_CHANGE,
105 	IMSG_KROUTE_DELETE,
106 	IMSG_IFINFO,
107 	IMSG_NEIGHBOR_UP,
108 	IMSG_NEIGHBOR_DOWN,
109 	IMSG_NEIGHBOR_CHANGE,
110 	IMSG_NEIGHBOR_CAPA,
111 	IMSG_NETWORK_ADD,
112 	IMSG_NETWORK_DEL,
113 	IMSG_AREA_CHANGE,
114 	IMSG_DD,
115 	IMSG_DD_END,
116 	IMSG_DD_BADLSA,
117 	IMSG_DB_SNAPSHOT,
118 	IMSG_DB_END,
119 	IMSG_LS_REQ,
120 	IMSG_LS_UPD,
121 	IMSG_LS_SNAP,
122 	IMSG_LS_ACK,
123 	IMSG_LS_FLOOD,
124 	IMSG_LS_BADREQ,
125 	IMSG_LS_MAXAGE,
126 	IMSG_ABR_UP,
127 	IMSG_ABR_DOWN,
128 	IMSG_RECONF_CONF,
129 	IMSG_RECONF_AREA,
130 	IMSG_RECONF_IFACE,
131 	IMSG_RECONF_AUTHMD,
132 	IMSG_RECONF_REDIST,
133 	IMSG_RECONF_END,
134 	IMSG_DEMOTE,
135 	IMSG_IFADDRDEL
136 };
137 
138 #define	REDIST_CONNECTED	0x01
139 #define	REDIST_STATIC		0x02
140 #define	REDIST_LABEL		0x04
141 #define	REDIST_ADDR		0x08
142 #define	REDIST_NO		0x10
143 #define	REDIST_DEFAULT		0x20
144 
145 struct redistribute {
146 	SIMPLEQ_ENTRY(redistribute)	entry;
147 	struct in_addr			addr;
148 	struct in_addr			mask;
149 	u_int32_t			metric;
150 	u_int16_t			label;
151 	u_int16_t			type;
152 };
153 SIMPLEQ_HEAD(redist_list, redistribute);
154 
155 struct vertex;
156 struct rde_nbr;
157 RB_HEAD(lsa_tree, vertex);
158 
159 struct area {
160 	LIST_ENTRY(area)	 entry;
161 	struct in_addr		 id;
162 	struct lsa_tree		 lsa_tree;
163 
164 	LIST_HEAD(, iface)	 iface_list;
165 	LIST_HEAD(, rde_nbr)	 nbr_list;
166 	struct redist_list	 redist_list;
167 /*	list			 addr_range_list; */
168 	char			 demote_group[IFNAMSIZ];
169 	u_int32_t		 stub_default_cost;
170 	u_int32_t		 num_spf_calc;
171 	int			 active;
172 	u_int8_t		 transit;
173 	u_int8_t		 stub;
174 	u_int8_t		 dirty;
175 	u_int8_t		 demote_level;
176 };
177 
178 /* interface states */
179 #define	IF_STA_NEW		0x00	/* dummy state for reload */
180 #define	IF_STA_DOWN		0x01
181 #define	IF_STA_LOOPBACK		0x02
182 #define	IF_STA_WAITING		0x04
183 #define	IF_STA_POINTTOPOINT	0x08
184 #define	IF_STA_DROTHER		0x10
185 #define	IF_STA_BACKUP		0x20
186 #define	IF_STA_DR		0x40
187 #define IF_STA_DRORBDR		(IF_STA_DR | IF_STA_BACKUP)
188 #define	IF_STA_MULTI		(IF_STA_DROTHER | IF_STA_BACKUP | IF_STA_DR)
189 #define	IF_STA_ANY		0x7f
190 
191 /* interface events */
192 enum iface_event {
193 	IF_EVT_NOTHING,
194 	IF_EVT_UP,
195 	IF_EVT_WTIMER,
196 	IF_EVT_BACKUP_SEEN,
197 	IF_EVT_NBR_CHNG,
198 	IF_EVT_LOOP,
199 	IF_EVT_UNLOOP,
200 	IF_EVT_DOWN
201 };
202 
203 /* interface actions */
204 enum iface_action {
205 	IF_ACT_NOTHING,
206 	IF_ACT_STRT,
207 	IF_ACT_ELECT,
208 	IF_ACT_RST
209 };
210 
211 /* interface types */
212 enum iface_type {
213 	IF_TYPE_POINTOPOINT,
214 	IF_TYPE_BROADCAST,
215 	IF_TYPE_NBMA,
216 	IF_TYPE_POINTOMULTIPOINT,
217 	IF_TYPE_VIRTUALLINK
218 };
219 
220 /* neighbor states */
221 #define	NBR_STA_DOWN		0x0001
222 #define	NBR_STA_ATTEMPT		0x0002
223 #define	NBR_STA_INIT		0x0004
224 #define	NBR_STA_2_WAY		0x0008
225 #define	NBR_STA_XSTRT		0x0010
226 #define NBR_STA_SNAP		0x0020
227 #define	NBR_STA_XCHNG		0x0040
228 #define	NBR_STA_LOAD		0x0080
229 #define	NBR_STA_FULL		0x0100
230 #define	NBR_STA_ACTIVE		(~NBR_STA_DOWN)
231 #define	NBR_STA_FLOOD		(NBR_STA_XCHNG | NBR_STA_LOAD | NBR_STA_FULL)
232 #define	NBR_STA_ADJFORM		(NBR_STA_XSTRT | NBR_STA_SNAP | NBR_STA_FLOOD)
233 #define	NBR_STA_BIDIR		(NBR_STA_2_WAY | NBR_STA_ADJFORM)
234 #define	NBR_STA_PRELIM		(NBR_STA_DOWN | NBR_STA_ATTEMPT | NBR_STA_INIT)
235 #define	NBR_STA_ANY		0xffff
236 
237 /* neighbor events */
238 enum nbr_event {
239 	NBR_EVT_NOTHING,
240 	NBR_EVT_HELLO_RCVD,
241 	NBR_EVT_2_WAY_RCVD,
242 	NBR_EVT_NEG_DONE,
243 	NBR_EVT_SNAP_DONE,
244 	NBR_EVT_XCHNG_DONE,
245 	NBR_EVT_BAD_LS_REQ,
246 	NBR_EVT_LOAD_DONE,
247 	NBR_EVT_ADJ_OK,
248 	NBR_EVT_SEQ_NUM_MIS,
249 	NBR_EVT_1_WAY_RCVD,
250 	NBR_EVT_KILL_NBR,
251 	NBR_EVT_ITIMER,
252 	NBR_EVT_LL_DOWN,
253 	NBR_EVT_ADJTMOUT
254 };
255 
256 /* neighbor actions */
257 enum nbr_action {
258 	NBR_ACT_NOTHING,
259 	NBR_ACT_RST_ITIMER,
260 	NBR_ACT_STRT_ITIMER,
261 	NBR_ACT_EVAL,
262 	NBR_ACT_SNAP,
263 	NBR_ACT_SNAP_DONE,
264 	NBR_ACT_XCHNG_DONE,
265 	NBR_ACT_ADJ_OK,
266 	NBR_ACT_RESTRT_DD,
267 	NBR_ACT_DEL,
268 	NBR_ACT_CLR_LST,
269 	NBR_ACT_HELLO_CHK
270 };
271 
272 /* auth types */
273 enum auth_type {
274 	AUTH_NONE,
275 	AUTH_SIMPLE,
276 	AUTH_CRYPT
277 };
278 
279 /* spf states */
280 enum spf_state {
281 	SPF_IDLE,
282 	SPF_DELAY,
283 	SPF_HOLD,
284 	SPF_HOLDQUEUE
285 };
286 
287 enum dst_type {
288 	DT_NET,
289 	DT_RTR
290 };
291 
292 enum path_type {
293 	PT_INTRA_AREA,
294 	PT_INTER_AREA,
295 	PT_TYPE1_EXT,
296 	PT_TYPE2_EXT
297 };
298 
299 enum rib_type {
300 	RIB_NET = 1,
301 	RIB_RTR,
302 	RIB_EXT
303 };
304 
305 struct auth_md {
306 	TAILQ_ENTRY(auth_md)	 entry;
307 	char			 key[MD5_DIGEST_LENGTH];
308 	u_int8_t		 keyid;
309 };
310 
311 /* lsa list used in RDE and OE */
312 TAILQ_HEAD(lsa_head, lsa_entry);
313 TAILQ_HEAD(auth_md_head, auth_md);
314 
315 struct iface {
316 	LIST_ENTRY(iface)	 entry;
317 	struct event		 hello_timer;
318 	struct event		 wait_timer;
319 	struct event		 lsack_tx_timer;
320 
321 	LIST_HEAD(, nbr)	 nbr_list;
322 	struct auth_md_head	 auth_md_list;
323 	struct lsa_head		 ls_ack_list;
324 	struct lsa_tree		 lsa_tree;
325 
326 	char			 name[IF_NAMESIZE];
327 	char			 demote_group[IFNAMSIZ];
328 	char			 auth_key[MAX_SIMPLE_AUTH_LEN];
329 	struct in_addr		 addr;
330 	struct in_addr		 dst;
331 	struct in_addr		 mask;
332 	struct in_addr		 abr_id;
333 	struct nbr		*dr;	/* designated router */
334 	struct nbr		*bdr;	/* backup designated router */
335 	struct nbr		*self;
336 	struct area		*area;
337 
338 	u_int64_t		 baudrate;
339 	u_int32_t		 dead_interval;
340 	u_int32_t		 fast_hello_interval;
341 	u_int32_t		 ls_ack_cnt;
342 	u_int32_t		 crypt_seq_num;
343 	time_t			 uptime;
344 	unsigned int		 ifindex;
345 	u_int			 rdomain;
346 	int			 fd;
347 	int			 state;
348 	int			 mtu;
349 	u_int16_t		 flags;
350 	u_int16_t		 transmit_delay;
351 	u_int16_t		 hello_interval;
352 	u_int16_t		 rxmt_interval;
353 	u_int16_t		 metric;
354 	enum iface_type		 type;
355 	enum auth_type		 auth_type;
356 	u_int8_t		 if_type;
357 	u_int8_t		 auth_keyid;
358 	u_int8_t		 linkstate;
359 	u_int8_t		 priority;
360 	u_int8_t		 passive;
361 };
362 
363 struct ifaddrdel {
364 	struct in_addr		addr;
365 	unsigned int		ifindex;
366 };
367 
368 /* ospf_conf */
369 enum {
370 	PROC_MAIN,
371 	PROC_OSPF_ENGINE,
372 	PROC_RDE_ENGINE
373 } ospfd_process;
374 
375 struct ospfd_conf {
376 	struct event		ev;
377 	struct in_addr		rtr_id;
378 	LIST_HEAD(, area)	area_list;
379 	LIST_HEAD(, vertex)	cand_list;
380 	struct redist_list	redist_list;
381 
382 	u_int32_t		opts;
383 #define OSPFD_OPT_VERBOSE	0x00000001
384 #define OSPFD_OPT_VERBOSE2	0x00000002
385 #define OSPFD_OPT_NOACTION	0x00000004
386 #define OSPFD_OPT_STUB_ROUTER	0x00000008
387 #define OSPFD_OPT_FORCE_DEMOTE	0x00000010
388 	u_int32_t		spf_delay;
389 	u_int32_t		spf_hold_time;
390 	time_t			uptime;
391 	int			spf_state;
392 	int			ospf_socket;
393 	int			flags;
394 	u_int8_t		rfc1583compat;
395 	u_int8_t		border;
396 	u_int8_t		redistribute;
397 	u_int			rdomain;
398 	char			*csock;
399 };
400 
401 /* kroute */
402 struct kroute {
403 	struct in_addr	prefix;
404 	struct in_addr	nexthop;
405 	u_int32_t	ext_tag;
406 	u_int32_t	metric;
407 	u_int16_t	flags;
408 	u_int16_t	rtlabel;
409 	u_short		ifindex;
410 	u_int8_t	prefixlen;
411 	u_int8_t	priority;
412 };
413 
414 struct kif_addr {
415 	TAILQ_ENTRY(kif_addr)	 entry;
416 	struct in_addr		 addr;
417 	struct in_addr		 mask;
418 	struct in_addr		 dstbrd;
419 };
420 
421 struct kif {
422 	char			 ifname[IF_NAMESIZE];
423 	u_int64_t		 baudrate;
424 	int			 flags;
425 	int			 mtu;
426 	unsigned int		 ifindex;
427 	u_int			 rdomain;
428 	u_int8_t		 if_type;
429 	u_int8_t		 link_state;
430 	u_int8_t		 nh_reachable;	/* for nexthop verification */
431 };
432 
433 /* name2id */
434 struct n2id_label {
435 	TAILQ_ENTRY(n2id_label)	 entry;
436 	char			*name;
437 	u_int16_t		 id;
438 	u_int32_t		 ext_tag;
439 	int			 ref;
440 };
441 
442 TAILQ_HEAD(n2id_labels, n2id_label);
443 extern struct n2id_labels rt_labels;
444 
445 /* control data structures */
446 struct ctl_iface {
447 	char			 name[IF_NAMESIZE];
448 	struct in_addr		 addr;
449 	struct in_addr		 mask;
450 	struct in_addr		 area;
451 	struct in_addr		 rtr_id;
452 	struct in_addr		 dr_id;
453 	struct in_addr		 dr_addr;
454 	struct in_addr		 bdr_id;
455 	struct in_addr		 bdr_addr;
456 	struct timeval		 hello_timer;
457 	time_t			 uptime;
458 	u_int64_t		 baudrate;
459 	u_int32_t		 dead_interval;
460 	u_int32_t		 fast_hello_interval;
461 	unsigned int		 ifindex;
462 	int			 state;
463 	int			 mtu;
464 	int			 nbr_cnt;
465 	int			 adj_cnt;
466 	u_int16_t		 transmit_delay;
467 	u_int16_t		 hello_interval;
468 	u_int16_t		 flags;
469 	u_int16_t		 metric;
470 	u_int16_t		 rxmt_interval;
471 	enum iface_type		 type;
472 	u_int8_t		 linkstate;
473 	u_int8_t		 if_type;
474 	u_int8_t		 priority;
475 	u_int8_t		 passive;
476 	enum auth_type		 auth_type;
477 	u_int8_t		 auth_keyid;
478 };
479 
480 struct ctl_nbr {
481 	char			 name[IF_NAMESIZE];
482 	struct in_addr		 id;
483 	struct in_addr		 addr;
484 	struct in_addr		 dr;
485 	struct in_addr		 bdr;
486 	struct in_addr		 area;
487 	time_t			 dead_timer;
488 	time_t			 uptime;
489 	u_int32_t		 db_sum_lst_cnt;
490 	u_int32_t		 ls_req_lst_cnt;
491 	u_int32_t		 ls_retrans_lst_cnt;
492 	u_int32_t		 state_chng_cnt;
493 	int			 nbr_state;
494 	int			 iface_state;
495 	u_int8_t		 priority;
496 	u_int8_t		 options;
497 };
498 
499 struct ctl_rt {
500 	struct in_addr		 prefix;
501 	struct in_addr		 nexthop;
502 	struct in_addr		 area;
503 	struct in_addr		 adv_rtr;
504 	time_t			 uptime;
505 	u_int32_t		 cost;
506 	u_int32_t		 cost2;
507 	enum path_type		 p_type;
508 	enum dst_type		 d_type;
509 	u_int8_t		 flags;
510 	u_int8_t		 prefixlen;
511 	u_int8_t		 connected;
512 };
513 
514 struct ctl_sum {
515 	struct in_addr		 rtr_id;
516 	u_int32_t		 spf_delay;
517 	u_int32_t		 spf_hold_time;
518 	u_int32_t		 num_ext_lsa;
519 	u_int32_t		 num_area;
520 	u_int32_t		 ext_lsa_cksum;
521 	time_t			 uptime;
522 	u_int8_t		 rfc1583compat;
523 };
524 
525 struct ctl_sum_area {
526 	struct in_addr		 area;
527 	u_int32_t		 num_iface;
528 	u_int32_t		 num_adj_nbr;
529 	u_int32_t		 num_spf_calc;
530 	u_int32_t		 num_lsa;
531 	u_int32_t		 lsa_cksum;
532 };
533 
534 struct demote_msg {
535 	char			 demote_group[IF_NAMESIZE];
536 	int			 level;
537 };
538 
539 /* area.c */
540 struct area	*area_new(void);
541 int		 area_del(struct area *);
542 struct area	*area_find(struct ospfd_conf *, struct in_addr);
543 void		 area_track(struct area *);
544 int		 area_border_router(struct ospfd_conf *);
545 u_int8_t	 area_ospf_options(struct area *);
546 
547 /* carp.c */
548 int		 carp_demote_init(char *, int);
549 void		 carp_demote_shutdown(void);
550 int		 carp_demote_get(char *);
551 int		 carp_demote_set(char *, int);
552 
553 /* parse.y */
554 struct ospfd_conf	*parse_config(char *, int);
555 int			 cmdline_symset(char *);
556 
557 /* in_cksum.c */
558 u_int16_t	 in_cksum(void *, size_t);
559 
560 /* iso_cksum.c */
561 u_int16_t	 iso_cksum(void *, u_int16_t, u_int16_t);
562 
563 /* kroute.c */
564 int		 kif_init(void);
565 void		 kif_clear(void);
566 int		 kr_init(int, u_int);
567 int		 kr_change(struct kroute *, int);
568 int		 kr_delete(struct kroute *);
569 void		 kr_shutdown(void);
570 void		 kr_fib_couple(void);
571 void		 kr_fib_decouple(void);
572 void		 kr_fib_reload(void);
573 void		 kr_dispatch_msg(int, short, void *);
574 void		 kr_show_route(struct imsg *);
575 void		 kr_ifinfo(char *, pid_t);
576 struct kif	*kif_findname(char *, struct in_addr, struct kif_addr **);
577 void		 kr_reload(void);
578 
579 u_int8_t	mask2prefixlen(in_addr_t);
580 in_addr_t	prefixlen2mask(u_int8_t);
581 
582 /* log.h */
583 const char	*nbr_state_name(int);
584 const char	*if_state_name(int);
585 const char	*if_type_name(enum iface_type);
586 const char	*if_auth_name(enum auth_type);
587 const char	*dst_type_name(enum dst_type);
588 const char	*path_type_name(enum path_type);
589 
590 /* name2id.c */
591 u_int16_t	 rtlabel_name2id(const char *);
592 const char	*rtlabel_id2name(u_int16_t);
593 void		 rtlabel_unref(u_int16_t);
594 u_int32_t	 rtlabel_id2tag(u_int16_t);
595 u_int16_t	 rtlabel_tag2id(u_int32_t);
596 void		 rtlabel_tag(u_int16_t, u_int32_t);
597 
598 /* ospfd.c */
599 void	main_imsg_compose_ospfe(int, pid_t, void *, u_int16_t);
600 void	main_imsg_compose_rde(int, pid_t, void *, u_int16_t);
601 int	ospf_redistribute(struct kroute *, u_int32_t *);
602 void	merge_config(struct ospfd_conf *, struct ospfd_conf *);
603 void	imsg_event_add(struct imsgev *);
604 int	imsg_compose_event(struct imsgev *, u_int16_t, u_int32_t,
605 	    pid_t, int, void *, u_int16_t);
606 
607 /* printconf.c */
608 void	print_config(struct ospfd_conf *);
609 
610 #endif	/* _OSPFD_H_ */
611