1 /* $OpenBSD: ospfd.h,v 1.108 2021/01/19 09:37:53 claudio 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 #include "log.h" 35 36 #define CONF_FILE "/etc/ospfd.conf" 37 #define OSPFD_SOCKET "/var/run/ospfd.sock" 38 #define OSPFD_USER "_ospfd" 39 40 #define NBR_HASHSIZE 128 41 #define LSA_HASHSIZE 512 42 43 #define NBR_IDSELF 1 44 #define NBR_CNTSTART (NBR_IDSELF + 1) 45 46 #define READ_BUF_SIZE 65535 47 #define PKG_DEF_SIZE 512 /* compromise */ 48 #define RT_BUF_SIZE 16384 49 #define MAX_RTSOCK_BUF (2 * 1024 * 1024) 50 51 #define OSPFD_FLAG_NO_FIB_UPDATE 0x0001 52 #define OSPFD_FLAG_STUB_ROUTER 0x0002 53 54 #define F_OSPFD_INSERTED 0x0001 55 #define F_KERNEL 0x0002 56 #define F_CONNECTED 0x0004 57 #define F_STATIC 0x0008 58 #define F_DYNAMIC 0x0010 59 #define F_DOWN 0x0020 60 #define F_REJECT 0x0040 61 #define F_BLACKHOLE 0x0080 62 #define F_REDISTRIBUTED 0x0100 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_CONTROLFD, 105 IMSG_KROUTE_CHANGE, 106 IMSG_KROUTE_DELETE, 107 IMSG_IFINFO, 108 IMSG_NEIGHBOR_UP, 109 IMSG_NEIGHBOR_DOWN, 110 IMSG_NEIGHBOR_ADDR, 111 IMSG_NEIGHBOR_CHANGE, 112 IMSG_NEIGHBOR_CAPA, 113 IMSG_NETWORK_ADD, 114 IMSG_NETWORK_DEL, 115 IMSG_AREA_CHANGE, 116 IMSG_DD, 117 IMSG_DD_END, 118 IMSG_DD_BADLSA, 119 IMSG_DB_SNAPSHOT, 120 IMSG_DB_END, 121 IMSG_LS_REQ, 122 IMSG_LS_UPD, 123 IMSG_LS_SNAP, 124 IMSG_LS_ACK, 125 IMSG_LS_FLOOD, 126 IMSG_LS_BADREQ, 127 IMSG_LS_MAXAGE, 128 IMSG_ABR_UP, 129 IMSG_ABR_DOWN, 130 IMSG_RECONF_CONF, 131 IMSG_RECONF_AREA, 132 IMSG_RECONF_IFACE, 133 IMSG_RECONF_AUTHMD, 134 IMSG_RECONF_REDIST, 135 IMSG_RECONF_END, 136 IMSG_DEMOTE, 137 IMSG_IFADDRADD, 138 IMSG_IFADDRDEL 139 }; 140 141 #define REDIST_CONNECTED 0x01 142 #define REDIST_STATIC 0x02 143 #define REDIST_LABEL 0x04 144 #define REDIST_ADDR 0x08 145 #define REDIST_NO 0x10 146 #define REDIST_DEFAULT 0x20 147 148 struct redistribute { 149 SIMPLEQ_ENTRY(redistribute) entry; 150 struct in_addr addr; 151 struct in_addr mask; 152 u_int32_t metric; 153 u_int16_t label; 154 u_int16_t type; 155 char dependon[IFNAMSIZ]; 156 }; 157 SIMPLEQ_HEAD(redist_list, redistribute); 158 159 struct vertex; 160 struct rde_nbr; 161 RB_HEAD(lsa_tree, vertex); 162 163 struct area { 164 LIST_ENTRY(area) entry; 165 struct in_addr id; 166 struct lsa_tree lsa_tree; 167 168 LIST_HEAD(, iface) iface_list; 169 LIST_HEAD(, rde_nbr) nbr_list; 170 struct redist_list redist_list; 171 /* list addr_range_list; */ 172 char demote_group[IFNAMSIZ]; 173 u_int32_t stub_default_cost; 174 u_int32_t num_spf_calc; 175 int active; 176 u_int8_t transit; 177 u_int8_t stub; 178 u_int8_t dirty; 179 u_int8_t demote_level; 180 }; 181 182 /* interface states */ 183 #define IF_STA_NEW 0x00 /* dummy state for reload */ 184 #define IF_STA_DOWN 0x01 185 #define IF_STA_LOOPBACK 0x02 186 #define IF_STA_WAITING 0x04 187 #define IF_STA_POINTTOPOINT 0x08 188 #define IF_STA_DROTHER 0x10 189 #define IF_STA_BACKUP 0x20 190 #define IF_STA_DR 0x40 191 #define IF_STA_DRORBDR (IF_STA_DR | IF_STA_BACKUP) 192 #define IF_STA_MULTI (IF_STA_DROTHER | IF_STA_BACKUP | IF_STA_DR) 193 #define IF_STA_ANY 0x7f 194 195 /* interface events */ 196 enum iface_event { 197 IF_EVT_NOTHING, 198 IF_EVT_UP, 199 IF_EVT_WTIMER, 200 IF_EVT_BACKUP_SEEN, 201 IF_EVT_NBR_CHNG, 202 IF_EVT_LOOP, 203 IF_EVT_UNLOOP, 204 IF_EVT_DOWN 205 }; 206 207 /* interface actions */ 208 enum iface_action { 209 IF_ACT_NOTHING, 210 IF_ACT_STRT, 211 IF_ACT_ELECT, 212 IF_ACT_RST 213 }; 214 215 /* interface types */ 216 enum iface_type { 217 IF_TYPE_POINTOPOINT, 218 IF_TYPE_BROADCAST, 219 IF_TYPE_NBMA, 220 IF_TYPE_POINTOMULTIPOINT, 221 IF_TYPE_VIRTUALLINK 222 }; 223 224 /* neighbor states */ 225 #define NBR_STA_DOWN 0x0001 226 #define NBR_STA_ATTEMPT 0x0002 227 #define NBR_STA_INIT 0x0004 228 #define NBR_STA_2_WAY 0x0008 229 #define NBR_STA_XSTRT 0x0010 230 #define NBR_STA_SNAP 0x0020 231 #define NBR_STA_XCHNG 0x0040 232 #define NBR_STA_LOAD 0x0080 233 #define NBR_STA_FULL 0x0100 234 #define NBR_STA_ACTIVE (~NBR_STA_DOWN) 235 #define NBR_STA_FLOOD (NBR_STA_XCHNG | NBR_STA_LOAD | NBR_STA_FULL) 236 #define NBR_STA_ADJFORM (NBR_STA_XSTRT | NBR_STA_SNAP | NBR_STA_FLOOD) 237 #define NBR_STA_BIDIR (NBR_STA_2_WAY | NBR_STA_ADJFORM) 238 #define NBR_STA_PRELIM (NBR_STA_DOWN | NBR_STA_ATTEMPT | NBR_STA_INIT) 239 #define NBR_STA_ANY 0xffff 240 241 /* neighbor events */ 242 enum nbr_event { 243 NBR_EVT_NOTHING, 244 NBR_EVT_HELLO_RCVD, 245 NBR_EVT_2_WAY_RCVD, 246 NBR_EVT_NEG_DONE, 247 NBR_EVT_SNAP_DONE, 248 NBR_EVT_XCHNG_DONE, 249 NBR_EVT_BAD_LS_REQ, 250 NBR_EVT_LOAD_DONE, 251 NBR_EVT_ADJ_OK, 252 NBR_EVT_SEQ_NUM_MIS, 253 NBR_EVT_1_WAY_RCVD, 254 NBR_EVT_KILL_NBR, 255 NBR_EVT_ITIMER, 256 NBR_EVT_LL_DOWN, 257 NBR_EVT_ADJTMOUT 258 }; 259 260 /* neighbor actions */ 261 enum nbr_action { 262 NBR_ACT_NOTHING, 263 NBR_ACT_RST_ITIMER, 264 NBR_ACT_STRT_ITIMER, 265 NBR_ACT_EVAL, 266 NBR_ACT_SNAP, 267 NBR_ACT_SNAP_DONE, 268 NBR_ACT_XCHNG_DONE, 269 NBR_ACT_ADJ_OK, 270 NBR_ACT_RESTRT_DD, 271 NBR_ACT_DEL, 272 NBR_ACT_CLR_LST, 273 NBR_ACT_HELLO_CHK 274 }; 275 276 /* auth types */ 277 enum auth_type { 278 AUTH_NONE, 279 AUTH_SIMPLE, 280 AUTH_CRYPT 281 }; 282 283 /* spf states */ 284 enum spf_state { 285 SPF_IDLE, 286 SPF_DELAY, 287 SPF_HOLD, 288 SPF_HOLDQUEUE 289 }; 290 291 enum dst_type { 292 DT_NET, 293 DT_RTR 294 }; 295 296 enum path_type { 297 PT_INTRA_AREA, 298 PT_INTER_AREA, 299 PT_TYPE1_EXT, 300 PT_TYPE2_EXT 301 }; 302 303 enum rib_type { 304 RIB_NET = 1, 305 RIB_RTR, 306 RIB_EXT 307 }; 308 309 struct auth_md { 310 TAILQ_ENTRY(auth_md) entry; 311 char key[MD5_DIGEST_LENGTH]; 312 u_int8_t keyid; 313 }; 314 315 /* lsa list used in RDE and OE */ 316 TAILQ_HEAD(lsa_head, lsa_entry); 317 TAILQ_HEAD(auth_md_head, auth_md); 318 319 struct iface { 320 LIST_ENTRY(iface) entry; 321 struct event hello_timer; 322 struct event wait_timer; 323 struct event lsack_tx_timer; 324 325 LIST_HEAD(, nbr) nbr_list; 326 struct auth_md_head auth_md_list; 327 struct lsa_head ls_ack_list; 328 struct lsa_tree lsa_tree; 329 330 char name[IF_NAMESIZE]; 331 char demote_group[IFNAMSIZ]; 332 char dependon[IFNAMSIZ]; 333 char auth_key[MAX_SIMPLE_AUTH_LEN]; 334 struct in_addr addr; 335 struct in_addr dst; 336 struct in_addr mask; 337 struct in_addr abr_id; 338 struct nbr *dr; /* designated router */ 339 struct nbr *bdr; /* backup designated router */ 340 struct nbr *self; 341 struct area *area; 342 343 u_int64_t baudrate; 344 u_int32_t dead_interval; 345 u_int32_t fast_hello_interval; 346 u_int32_t ls_ack_cnt; 347 u_int32_t crypt_seq_num; 348 time_t uptime; 349 unsigned int ifindex; 350 u_int rdomain; 351 int fd; 352 int state; 353 int mtu; 354 int depend_ok; 355 u_int16_t flags; 356 u_int16_t transmit_delay; 357 u_int16_t hello_interval; 358 u_int16_t rxmt_interval; 359 u_int16_t metric; 360 enum iface_type type; 361 enum auth_type auth_type; 362 u_int8_t if_type; 363 u_int8_t auth_keyid; 364 u_int8_t linkstate; 365 u_int8_t priority; 366 u_int8_t passive; 367 }; 368 369 struct ifaddrchange { 370 struct in_addr addr; 371 struct in_addr mask; 372 struct in_addr dst; 373 unsigned int ifindex; 374 }; 375 376 /* ospf_conf */ 377 enum ospfd_process { 378 PROC_MAIN, 379 PROC_OSPF_ENGINE, 380 PROC_RDE_ENGINE 381 }; 382 extern enum ospfd_process ospfd_process; 383 384 struct ospfd_conf { 385 struct event ev; 386 struct in_addr rtr_id; 387 LIST_HEAD(, area) area_list; 388 LIST_HEAD(, vertex) cand_list; 389 struct redist_list redist_list; 390 391 u_int32_t opts; 392 #define OSPFD_OPT_VERBOSE 0x00000001 393 #define OSPFD_OPT_VERBOSE2 0x00000002 394 #define OSPFD_OPT_NOACTION 0x00000004 395 #define OSPFD_OPT_STUB_ROUTER 0x00000008 396 #define OSPFD_OPT_FORCE_DEMOTE 0x00000010 397 u_int32_t spf_delay; 398 u_int32_t spf_hold_time; 399 time_t uptime; 400 int spf_state; 401 int ospf_socket; 402 int flags; 403 int redist_label_or_prefix; 404 u_int8_t rfc1583compat; 405 u_int8_t border; 406 u_int8_t redistribute; 407 u_int8_t fib_priority; 408 u_int rdomain; 409 char *csock; 410 }; 411 412 /* kroute */ 413 struct kroute { 414 struct in_addr prefix; 415 struct in_addr nexthop; 416 u_int32_t ext_tag; 417 u_int32_t metric; 418 u_int16_t flags; 419 u_int16_t rtlabel; 420 u_short ifindex; 421 u_int8_t prefixlen; 422 u_int8_t priority; 423 }; 424 425 struct kif_addr { 426 TAILQ_ENTRY(kif_addr) entry; 427 struct in_addr addr; 428 struct in_addr mask; 429 struct in_addr dstbrd; 430 }; 431 432 struct kif { 433 char ifname[IF_NAMESIZE]; 434 u_int64_t baudrate; 435 int flags; 436 int mtu; 437 unsigned int ifindex; 438 u_int rdomain; 439 u_int8_t if_type; 440 u_int8_t link_state; 441 u_int8_t nh_reachable; /* for nexthop verification */ 442 }; 443 444 /* name2id */ 445 struct n2id_label { 446 TAILQ_ENTRY(n2id_label) entry; 447 char *name; 448 u_int16_t id; 449 u_int32_t ext_tag; 450 int ref; 451 }; 452 453 TAILQ_HEAD(n2id_labels, n2id_label); 454 extern struct n2id_labels rt_labels; 455 456 /* control data structures */ 457 struct ctl_iface { 458 char name[IF_NAMESIZE]; 459 struct in_addr addr; 460 struct in_addr mask; 461 struct in_addr area; 462 struct in_addr rtr_id; 463 struct in_addr dr_id; 464 struct in_addr dr_addr; 465 struct in_addr bdr_id; 466 struct in_addr bdr_addr; 467 struct timeval hello_timer; 468 time_t uptime; 469 u_int64_t baudrate; 470 u_int32_t dead_interval; 471 u_int32_t fast_hello_interval; 472 unsigned int ifindex; 473 int state; 474 int mtu; 475 int nbr_cnt; 476 int adj_cnt; 477 u_int16_t transmit_delay; 478 u_int16_t hello_interval; 479 u_int16_t flags; 480 u_int16_t metric; 481 u_int16_t rxmt_interval; 482 enum iface_type type; 483 u_int8_t linkstate; 484 u_int8_t if_type; 485 u_int8_t priority; 486 u_int8_t passive; 487 enum auth_type auth_type; 488 u_int8_t auth_keyid; 489 char dependon[IF_NAMESIZE]; 490 int depend_ok; 491 }; 492 493 struct ctl_nbr { 494 char name[IF_NAMESIZE]; 495 struct in_addr id; 496 struct in_addr addr; 497 struct in_addr dr; 498 struct in_addr bdr; 499 struct in_addr area; 500 time_t dead_timer; 501 time_t uptime; 502 u_int32_t db_sum_lst_cnt; 503 u_int32_t ls_req_lst_cnt; 504 u_int32_t ls_retrans_lst_cnt; 505 u_int32_t state_chng_cnt; 506 int nbr_state; 507 int iface_state; 508 u_int8_t priority; 509 u_int8_t options; 510 }; 511 512 struct ctl_rt { 513 struct in_addr prefix; 514 struct in_addr nexthop; 515 struct in_addr area; 516 struct in_addr adv_rtr; 517 time_t uptime; 518 u_int32_t cost; 519 u_int32_t cost2; 520 enum path_type p_type; 521 enum dst_type d_type; 522 u_int8_t flags; 523 u_int8_t prefixlen; 524 u_int8_t connected; 525 }; 526 527 struct ctl_sum { 528 struct in_addr rtr_id; 529 u_int32_t spf_delay; 530 u_int32_t spf_hold_time; 531 u_int32_t num_ext_lsa; 532 u_int32_t num_area; 533 u_int32_t ext_lsa_cksum; 534 time_t uptime; 535 u_int8_t rfc1583compat; 536 }; 537 538 struct ctl_sum_area { 539 struct in_addr area; 540 u_int32_t num_iface; 541 u_int32_t num_adj_nbr; 542 u_int32_t num_spf_calc; 543 u_int32_t num_lsa; 544 u_int32_t lsa_cksum; 545 }; 546 547 struct demote_msg { 548 char demote_group[IF_NAMESIZE]; 549 int level; 550 }; 551 552 /* area.c */ 553 struct area *area_new(void); 554 int area_del(struct area *); 555 struct area *area_find(struct ospfd_conf *, struct in_addr); 556 void area_track(struct area *); 557 int area_border_router(struct ospfd_conf *); 558 u_int8_t area_ospf_options(struct area *); 559 560 /* carp.c */ 561 int carp_demote_init(char *, int); 562 void carp_demote_shutdown(void); 563 int carp_demote_get(char *); 564 int carp_demote_set(char *, int); 565 566 /* parse.y */ 567 struct ospfd_conf *parse_config(char *, int); 568 u_int32_t get_rtr_id(void); 569 int cmdline_symset(char *); 570 void conf_clear_redist_list(struct redist_list *); 571 572 /* in_cksum.c */ 573 u_int16_t in_cksum(void *, size_t); 574 575 /* iso_cksum.c */ 576 u_int16_t iso_cksum(void *, u_int16_t, u_int16_t); 577 578 /* kroute.c */ 579 int kif_init(void); 580 void kif_clear(void); 581 int kr_init(int, u_int, int, u_int8_t); 582 int kr_change(struct kroute *, int); 583 int kr_delete(struct kroute *); 584 void kr_shutdown(void); 585 void kr_fib_couple(void); 586 void kr_fib_decouple(void); 587 void kr_fib_reload(void); 588 void kr_fib_update_prio(u_int8_t); 589 void kr_dispatch_msg(int, short, void *); 590 void kr_show_route(struct imsg *); 591 void kr_ifinfo(char *, pid_t); 592 struct kif *kif_findname(char *, struct in_addr, struct kif_addr **); 593 void kr_reload(int); 594 595 u_int8_t mask2prefixlen(in_addr_t); 596 in_addr_t prefixlen2mask(u_int8_t); 597 598 /* logmsg.h */ 599 const char *nbr_state_name(int); 600 const char *if_state_name(int); 601 const char *if_type_name(enum iface_type); 602 const char *if_auth_name(enum auth_type); 603 const char *dst_type_name(enum dst_type); 604 const char *path_type_name(enum path_type); 605 606 /* name2id.c */ 607 u_int16_t rtlabel_name2id(const char *); 608 const char *rtlabel_id2name(u_int16_t); 609 void rtlabel_unref(u_int16_t); 610 u_int32_t rtlabel_id2tag(u_int16_t); 611 u_int16_t rtlabel_tag2id(u_int32_t); 612 void rtlabel_tag(u_int16_t, u_int32_t); 613 614 /* ospfd.c */ 615 void main_imsg_compose_ospfe(int, pid_t, void *, u_int16_t); 616 void main_imsg_compose_ospfe_fd(int, pid_t, int); 617 void main_imsg_compose_rde(int, pid_t, void *, u_int16_t); 618 int ospf_redistribute(struct kroute *, u_int32_t *); 619 void merge_config(struct ospfd_conf *, struct ospfd_conf *); 620 void imsg_event_add(struct imsgev *); 621 int imsg_compose_event(struct imsgev *, u_int16_t, u_int32_t, 622 pid_t, int, void *, u_int16_t); 623 int ifstate_is_up(struct kif *kif); 624 625 /* printconf.c */ 626 void print_config(struct ospfd_conf *); 627 628 #endif /* _OSPFD_H_ */ 629