1 /* $OpenBSD: ldpd.h,v 1.86 2017/03/04 00:12:26 renato Exp $ */ 2 3 /* 4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org> 5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> 6 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 #ifndef _LDPD_H_ 23 #define _LDPD_H_ 24 25 #include <sys/types.h> 26 #include <sys/socket.h> 27 #include <sys/queue.h> 28 #include <sys/tree.h> 29 #include <net/if.h> 30 #include <netinet/in.h> 31 #include <event.h> 32 #include <imsg.h> 33 34 #include "ldp.h" 35 36 #define CONF_FILE "/etc/ldpd.conf" 37 #define LDPD_SOCKET "/var/run/ldpd.sock" 38 #define LDPD_USER "_ldpd" 39 40 #define LDPD_OPT_VERBOSE 0x00000001 41 #define LDPD_OPT_VERBOSE2 0x00000002 42 #define LDPD_OPT_NOACTION 0x00000004 43 44 #define TCP_MD5_KEY_LEN 80 45 #define L2VPN_NAME_LEN 32 46 47 #define RT_BUF_SIZE 16384 48 #define MAX_RTSOCK_BUF 128 * 1024 49 #define LDP_BACKLOG 128 50 51 #define F_LDPD_INSERTED 0x0001 52 #define F_CONNECTED 0x0002 53 #define F_STATIC 0x0004 54 #define F_DYNAMIC 0x0008 55 #define F_REJECT 0x0010 56 #define F_BLACKHOLE 0x0020 57 #define F_REDISTRIBUTED 0x0040 58 59 struct evbuf { 60 struct msgbuf wbuf; 61 struct event ev; 62 }; 63 64 struct imsgev { 65 struct imsgbuf ibuf; 66 void (*handler)(int, short, void *); 67 struct event ev; 68 short events; 69 }; 70 71 enum imsg_type { 72 IMSG_NONE, 73 IMSG_CTL_RELOAD, 74 IMSG_CTL_SHOW_INTERFACE, 75 IMSG_CTL_SHOW_DISCOVERY, 76 IMSG_CTL_SHOW_NBR, 77 IMSG_CTL_SHOW_LIB, 78 IMSG_CTL_SHOW_L2VPN_PW, 79 IMSG_CTL_SHOW_L2VPN_BINDING, 80 IMSG_CTL_CLEAR_NBR, 81 IMSG_CTL_FIB_COUPLE, 82 IMSG_CTL_FIB_DECOUPLE, 83 IMSG_CTL_KROUTE, 84 IMSG_CTL_KROUTE_ADDR, 85 IMSG_CTL_IFINFO, 86 IMSG_CTL_END, 87 IMSG_CTL_LOG_VERBOSE, 88 IMSG_KLABEL_CHANGE, 89 IMSG_KLABEL_DELETE, 90 IMSG_KPWLABEL_CHANGE, 91 IMSG_KPWLABEL_DELETE, 92 IMSG_IFSTATUS, 93 IMSG_NEWADDR, 94 IMSG_DELADDR, 95 IMSG_LABEL_MAPPING, 96 IMSG_LABEL_MAPPING_FULL, 97 IMSG_LABEL_REQUEST, 98 IMSG_LABEL_RELEASE, 99 IMSG_LABEL_WITHDRAW, 100 IMSG_LABEL_ABORT, 101 IMSG_REQUEST_ADD, 102 IMSG_REQUEST_ADD_END, 103 IMSG_MAPPING_ADD, 104 IMSG_MAPPING_ADD_END, 105 IMSG_RELEASE_ADD, 106 IMSG_RELEASE_ADD_END, 107 IMSG_WITHDRAW_ADD, 108 IMSG_WITHDRAW_ADD_END, 109 IMSG_ADDRESS_ADD, 110 IMSG_ADDRESS_DEL, 111 IMSG_NOTIFICATION, 112 IMSG_NOTIFICATION_SEND, 113 IMSG_NEIGHBOR_UP, 114 IMSG_NEIGHBOR_DOWN, 115 IMSG_NETWORK_ADD, 116 IMSG_NETWORK_DEL, 117 IMSG_SOCKET_IPC, 118 IMSG_SOCKET_NET, 119 IMSG_CLOSE_SOCKETS, 120 IMSG_REQUEST_SOCKETS, 121 IMSG_SETUP_SOCKETS, 122 IMSG_RECONF_CONF, 123 IMSG_RECONF_IFACE, 124 IMSG_RECONF_TNBR, 125 IMSG_RECONF_NBRP, 126 IMSG_RECONF_L2VPN, 127 IMSG_RECONF_L2VPN_IF, 128 IMSG_RECONF_L2VPN_PW, 129 IMSG_RECONF_END 130 }; 131 132 union ldpd_addr { 133 struct in_addr v4; 134 struct in6_addr v6; 135 }; 136 137 #define IN6_IS_SCOPE_EMBED(a) \ 138 ((IN6_IS_ADDR_LINKLOCAL(a)) || \ 139 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \ 140 (IN6_IS_ADDR_MC_INTFACELOCAL(a))) 141 142 /* interface states */ 143 #define IF_STA_DOWN 0x01 144 #define IF_STA_ACTIVE 0x02 145 146 /* targeted neighbor states */ 147 #define TNBR_STA_DOWN 0x01 148 #define TNBR_STA_ACTIVE 0x02 149 150 /* interface types */ 151 enum iface_type { 152 IF_TYPE_POINTOPOINT, 153 IF_TYPE_BROADCAST 154 }; 155 156 /* neighbor states */ 157 #define NBR_STA_PRESENT 0x0001 158 #define NBR_STA_INITIAL 0x0002 159 #define NBR_STA_OPENREC 0x0004 160 #define NBR_STA_OPENSENT 0x0008 161 #define NBR_STA_OPER 0x0010 162 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \ 163 NBR_STA_OPENSENT | NBR_STA_OPER) 164 165 /* neighbor events */ 166 enum nbr_event { 167 NBR_EVT_NOTHING, 168 NBR_EVT_MATCH_ADJ, 169 NBR_EVT_CONNECT_UP, 170 NBR_EVT_CLOSE_SESSION, 171 NBR_EVT_INIT_RCVD, 172 NBR_EVT_KEEPALIVE_RCVD, 173 NBR_EVT_PDU_RCVD, 174 NBR_EVT_PDU_SENT, 175 NBR_EVT_INIT_SENT 176 }; 177 178 /* neighbor actions */ 179 enum nbr_action { 180 NBR_ACT_NOTHING, 181 NBR_ACT_RST_KTIMEOUT, 182 NBR_ACT_SESSION_EST, 183 NBR_ACT_RST_KTIMER, 184 NBR_ACT_CONNECT_SETUP, 185 NBR_ACT_PASSIVE_INIT, 186 NBR_ACT_KEEPALIVE_SEND, 187 NBR_ACT_CLOSE_SESSION 188 }; 189 190 TAILQ_HEAD(mapping_head, mapping_entry); 191 192 struct map { 193 uint8_t type; 194 uint32_t msg_id; 195 union { 196 struct { 197 uint16_t af; 198 union ldpd_addr prefix; 199 uint8_t prefixlen; 200 } prefix; 201 struct { 202 uint16_t type; 203 uint32_t pwid; 204 uint32_t group_id; 205 uint16_t ifmtu; 206 } pwid; 207 struct { 208 uint8_t type; 209 union { 210 uint16_t prefix_af; 211 uint16_t pw_type; 212 } u; 213 } twcard; 214 } fec; 215 struct { 216 uint32_t status_code; 217 uint32_t msg_id; 218 uint16_t msg_type; 219 } st; 220 uint32_t label; 221 uint32_t requestid; 222 uint32_t pw_status; 223 uint8_t flags; 224 }; 225 #define F_MAP_REQ_ID 0x01 /* optional request message id present */ 226 #define F_MAP_STATUS 0x02 /* status */ 227 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */ 228 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */ 229 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */ 230 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */ 231 232 struct notify_msg { 233 uint32_t status_code; 234 uint32_t msg_id; /* network byte order */ 235 uint16_t msg_type; /* network byte order */ 236 uint32_t pw_status; 237 struct map fec; 238 struct { 239 uint16_t type; 240 uint16_t length; 241 char *data; 242 } rtlvs; 243 uint8_t flags; 244 }; 245 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */ 246 #define F_NOTIF_FEC 0x02 /* fec tlv present */ 247 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */ 248 249 struct if_addr { 250 LIST_ENTRY(if_addr) entry; 251 int af; 252 union ldpd_addr addr; 253 uint8_t prefixlen; 254 union ldpd_addr dstbrd; 255 }; 256 LIST_HEAD(if_addr_head, if_addr); 257 258 struct iface_af { 259 struct iface *iface; 260 int af; 261 int enabled; 262 int state; 263 LIST_HEAD(, adj) adj_list; 264 time_t uptime; 265 struct event hello_timer; 266 uint16_t hello_holdtime; 267 uint16_t hello_interval; 268 }; 269 270 struct iface { 271 LIST_ENTRY(iface) entry; 272 char name[IF_NAMESIZE]; 273 unsigned int ifindex; 274 unsigned int rdomain; 275 struct if_addr_head addr_list; 276 struct in6_addr linklocal; 277 enum iface_type type; 278 uint8_t if_type; 279 uint16_t flags; 280 uint8_t linkstate; 281 struct iface_af ipv4; 282 struct iface_af ipv6; 283 }; 284 285 /* source of targeted hellos */ 286 struct tnbr { 287 LIST_ENTRY(tnbr) entry; 288 struct event hello_timer; 289 struct adj *adj; 290 int af; 291 union ldpd_addr addr; 292 int state; 293 uint16_t hello_holdtime; 294 uint16_t hello_interval; 295 uint16_t pw_count; 296 uint8_t flags; 297 }; 298 #define F_TNBR_CONFIGURED 0x01 299 #define F_TNBR_DYNAMIC 0x02 300 301 enum auth_method { 302 AUTH_NONE, 303 AUTH_MD5SIG 304 }; 305 306 /* neighbor specific parameters */ 307 struct nbr_params { 308 LIST_ENTRY(nbr_params) entry; 309 struct in_addr lsr_id; 310 uint16_t keepalive; 311 int gtsm_enabled; 312 uint8_t gtsm_hops; 313 struct { 314 enum auth_method method; 315 char md5key[TCP_MD5_KEY_LEN]; 316 uint8_t md5key_len; 317 } auth; 318 uint8_t flags; 319 }; 320 #define F_NBRP_KEEPALIVE 0x01 321 #define F_NBRP_GTSM 0x02 322 #define F_NBRP_GTSM_HOPS 0x04 323 324 struct l2vpn_if { 325 LIST_ENTRY(l2vpn_if) entry; 326 struct l2vpn *l2vpn; 327 char ifname[IF_NAMESIZE]; 328 unsigned int ifindex; 329 uint16_t flags; 330 uint8_t link_state; 331 }; 332 333 struct l2vpn_pw { 334 LIST_ENTRY(l2vpn_pw) entry; 335 struct l2vpn *l2vpn; 336 struct in_addr lsr_id; 337 int af; 338 union ldpd_addr addr; 339 uint32_t pwid; 340 char ifname[IF_NAMESIZE]; 341 unsigned int ifindex; 342 uint32_t remote_group; 343 uint16_t remote_mtu; 344 uint32_t remote_status; 345 uint8_t flags; 346 }; 347 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */ 348 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */ 349 #define F_PW_CWORD_CONF 0x04 /* control word configured */ 350 #define F_PW_CWORD 0x08 /* control word negotiated */ 351 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */ 352 353 struct l2vpn { 354 LIST_ENTRY(l2vpn) entry; 355 char name[L2VPN_NAME_LEN]; 356 int type; 357 int pw_type; 358 int mtu; 359 char br_ifname[IF_NAMESIZE]; 360 unsigned int br_ifindex; 361 LIST_HEAD(, l2vpn_if) if_list; 362 LIST_HEAD(, l2vpn_pw) pw_list; 363 }; 364 #define L2VPN_TYPE_VPWS 1 365 #define L2VPN_TYPE_VPLS 2 366 367 /* ldp_conf */ 368 enum ldpd_process { 369 PROC_MAIN, 370 PROC_LDP_ENGINE, 371 PROC_LDE_ENGINE 372 } ldpd_process; 373 374 static const char * const log_procnames[] = { 375 "parent", 376 "ldpe", 377 "lde" 378 }; 379 380 enum socket_type { 381 LDP_SOCKET_DISC, 382 LDP_SOCKET_EDISC, 383 LDP_SOCKET_SESSION 384 }; 385 386 enum hello_type { 387 HELLO_LINK, 388 HELLO_TARGETED 389 }; 390 391 struct ldpd_af_conf { 392 uint16_t keepalive; 393 uint16_t thello_holdtime; 394 uint16_t thello_interval; 395 union ldpd_addr trans_addr; 396 int flags; 397 }; 398 #define F_LDPD_AF_ENABLED 0x0001 399 #define F_LDPD_AF_THELLO_ACCEPT 0x0002 400 #define F_LDPD_AF_EXPNULL 0x0004 401 #define F_LDPD_AF_NO_GTSM 0x0008 402 403 struct ldpd_conf { 404 struct in_addr rtr_id; 405 unsigned int rdomain; 406 struct ldpd_af_conf ipv4; 407 struct ldpd_af_conf ipv6; 408 LIST_HEAD(, iface) iface_list; 409 LIST_HEAD(, tnbr) tnbr_list; 410 LIST_HEAD(, nbr_params) nbrp_list; 411 LIST_HEAD(, l2vpn) l2vpn_list; 412 uint16_t trans_pref; 413 int flags; 414 }; 415 #define F_LDPD_NO_FIB_UPDATE 0x0001 416 #define F_LDPD_DS_CISCO_INTEROP 0x0002 417 418 struct ldpd_af_global { 419 struct event disc_ev; 420 struct event edisc_ev; 421 int ldp_disc_socket; 422 int ldp_edisc_socket; 423 int ldp_session_socket; 424 }; 425 426 struct ldpd_global { 427 int cmd_opts; 428 char *csock; 429 time_t uptime; 430 struct ldpd_af_global ipv4; 431 struct ldpd_af_global ipv6; 432 uint32_t conf_seqnum; 433 int pfkeysock; 434 struct if_addr_head addr_list; 435 LIST_HEAD(, adj) adj_list; 436 struct in_addr mcast_addr_v4; 437 struct in6_addr mcast_addr_v6; 438 TAILQ_HEAD(, pending_conn) pending_conns; 439 }; 440 441 /* kroute */ 442 struct kroute { 443 int af; 444 union ldpd_addr prefix; 445 uint8_t prefixlen; 446 union ldpd_addr nexthop; 447 uint32_t local_label; 448 uint32_t remote_label; 449 unsigned short ifindex; 450 uint8_t priority; 451 uint16_t flags; 452 }; 453 454 struct kpw { 455 unsigned short ifindex; 456 int pw_type; 457 int af; 458 union ldpd_addr nexthop; 459 uint32_t local_label; 460 uint32_t remote_label; 461 uint8_t flags; 462 }; 463 464 struct kaddr { 465 unsigned short ifindex; 466 int af; 467 union ldpd_addr addr; 468 uint8_t prefixlen; 469 union ldpd_addr dstbrd; 470 }; 471 472 struct kif { 473 char ifname[IF_NAMESIZE]; 474 unsigned short ifindex; 475 int flags; 476 uint8_t link_state; 477 int mtu; 478 unsigned int rdomain; 479 uint8_t if_type; 480 uint64_t baudrate; 481 }; 482 483 /* control data structures */ 484 struct ctl_iface { 485 int af; 486 char name[IF_NAMESIZE]; 487 unsigned int ifindex; 488 int state; 489 uint16_t flags; 490 uint8_t linkstate; 491 enum iface_type type; 492 uint8_t if_type; 493 uint16_t hello_holdtime; 494 uint16_t hello_interval; 495 time_t uptime; 496 uint16_t adj_cnt; 497 }; 498 499 struct ctl_adj { 500 int af; 501 struct in_addr id; 502 enum hello_type type; 503 char ifname[IF_NAMESIZE]; 504 union ldpd_addr src_addr; 505 uint16_t holdtime; 506 union ldpd_addr trans_addr; 507 }; 508 509 struct ctl_nbr { 510 int af; 511 struct in_addr id; 512 union ldpd_addr laddr; 513 union ldpd_addr raddr; 514 time_t uptime; 515 int nbr_state; 516 }; 517 518 struct ctl_rt { 519 int af; 520 union ldpd_addr prefix; 521 uint8_t prefixlen; 522 struct in_addr nexthop; /* lsr-id */ 523 uint32_t local_label; 524 uint32_t remote_label; 525 uint8_t flags; 526 uint8_t in_use; 527 }; 528 529 struct ctl_pw { 530 uint16_t type; 531 char ifname[IF_NAMESIZE]; 532 uint32_t pwid; 533 struct in_addr lsr_id; 534 uint32_t local_label; 535 uint32_t local_gid; 536 uint16_t local_ifmtu; 537 uint32_t remote_label; 538 uint32_t remote_gid; 539 uint16_t remote_ifmtu; 540 uint32_t status; 541 }; 542 543 extern struct ldpd_conf *ldpd_conf; 544 extern struct ldpd_global global; 545 546 /* parse.y */ 547 struct ldpd_conf *parse_config(char *); 548 int cmdline_symset(char *); 549 550 /* kroute.c */ 551 int kif_init(void); 552 int kr_init(int, unsigned int); 553 void kif_redistribute(const char *); 554 int kr_change(struct kroute *); 555 int kr_delete(struct kroute *); 556 void kr_shutdown(void); 557 void kr_fib_couple(void); 558 void kr_fib_decouple(void); 559 void kr_change_egress_label(int, int); 560 void kr_show_route(struct imsg *); 561 void kr_ifinfo(char *, pid_t); 562 struct kif *kif_findname(char *); 563 void kif_clear(void); 564 int kmpw_set(struct kpw *); 565 int kmpw_unset(struct kpw *); 566 567 /* util.c */ 568 uint8_t mask2prefixlen(in_addr_t); 569 uint8_t mask2prefixlen6(struct sockaddr_in6 *); 570 in_addr_t prefixlen2mask(uint8_t); 571 struct in6_addr *prefixlen2mask6(uint8_t); 572 void ldp_applymask(int, union ldpd_addr *, 573 const union ldpd_addr *, int); 574 int ldp_addrcmp(int, const union ldpd_addr *, 575 const union ldpd_addr *); 576 int ldp_addrisset(int, const union ldpd_addr *); 577 int ldp_prefixcmp(int, const union ldpd_addr *, 578 const union ldpd_addr *, uint8_t); 579 int bad_addr_v4(struct in_addr); 580 int bad_addr_v6(struct in6_addr *); 581 int bad_addr(int, union ldpd_addr *); 582 void embedscope(struct sockaddr_in6 *); 583 void recoverscope(struct sockaddr_in6 *); 584 void addscope(struct sockaddr_in6 *, uint32_t); 585 void clearscope(struct in6_addr *); 586 struct sockaddr *addr2sa(int af, union ldpd_addr *, uint16_t); 587 void sa2addr(struct sockaddr *, int *, union ldpd_addr *); 588 589 /* ldpd.c */ 590 void main_imsg_compose_ldpe(int, pid_t, void *, uint16_t); 591 void main_imsg_compose_lde(int, pid_t, void *, uint16_t); 592 void imsg_event_add(struct imsgev *); 593 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, 594 int, void *, uint16_t); 595 void evbuf_enqueue(struct evbuf *, struct ibuf *); 596 void evbuf_event_add(struct evbuf *); 597 void evbuf_init(struct evbuf *, int, void (*)(int, short, void *), void *); 598 void evbuf_clear(struct evbuf *); 599 struct ldpd_af_conf *ldp_af_conf_get(struct ldpd_conf *, int); 600 struct ldpd_af_global *ldp_af_global_get(struct ldpd_global *, int); 601 int ldp_is_dual_stack(struct ldpd_conf *); 602 void merge_config(struct ldpd_conf *, struct ldpd_conf *); 603 struct ldpd_conf *config_new_empty(void); 604 void config_clear(struct ldpd_conf *); 605 606 /* socket.c */ 607 int ldp_create_socket(int, enum socket_type); 608 void sock_set_recvbuf(int); 609 int sock_set_reuse(int, int); 610 int sock_set_bindany(int, int); 611 int sock_set_ipv4_tos(int, int); 612 int sock_set_ipv4_recvif(int, int); 613 int sock_set_ipv4_minttl(int, int); 614 int sock_set_ipv4_ucast_ttl(int fd, int); 615 int sock_set_ipv4_mcast_ttl(int, uint8_t); 616 int sock_set_ipv4_mcast(struct iface *); 617 int sock_set_ipv4_mcast_loop(int); 618 int sock_set_ipv6_dscp(int, int); 619 int sock_set_ipv6_pktinfo(int, int); 620 int sock_set_ipv6_minhopcount(int, int); 621 int sock_set_ipv6_ucast_hops(int, int); 622 int sock_set_ipv6_mcast_hops(int, int); 623 int sock_set_ipv6_mcast(struct iface *); 624 int sock_set_ipv6_mcast_loop(int); 625 626 /* printconf.c */ 627 void print_config(struct ldpd_conf *); 628 629 /* logmsg.h */ 630 struct in6_addr; 631 union ldpd_addr; 632 struct hello_source; 633 struct fec; 634 635 const char *log_sockaddr(void *); 636 const char *log_in6addr(const struct in6_addr *); 637 const char *log_in6addr_scope(const struct in6_addr *, unsigned int); 638 const char *log_addr(int, const union ldpd_addr *); 639 char *log_label(uint32_t); 640 char *log_hello_src(const struct hello_source *); 641 const char *log_map(const struct map *); 642 const char *log_fec(const struct fec *); 643 const char *af_name(int); 644 const char *socket_name(int); 645 const char *nbr_state_name(int); 646 const char *if_state_name(int); 647 const char *if_type_name(enum iface_type); 648 const char *msg_name(uint16_t); 649 const char *status_code_name(uint32_t); 650 const char *pw_type_name(uint16_t); 651 652 #endif /* _LDPD_H_ */ 653