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