1 /* $OpenBSD: ldpd.h,v 1.84 2017/03/04 00:06:10 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 } fec; 208 struct { 209 uint32_t status_code; 210 uint32_t msg_id; 211 uint16_t msg_type; 212 } st; 213 uint32_t label; 214 uint32_t requestid; 215 uint32_t pw_status; 216 uint8_t flags; 217 }; 218 #define F_MAP_REQ_ID 0x01 /* optional request message id present */ 219 #define F_MAP_STATUS 0x02 /* status */ 220 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */ 221 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */ 222 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */ 223 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */ 224 225 struct notify_msg { 226 uint32_t status_code; 227 uint32_t msg_id; /* network byte order */ 228 uint16_t msg_type; /* network byte order */ 229 uint32_t pw_status; 230 struct map fec; 231 struct { 232 uint16_t type; 233 uint16_t length; 234 char *data; 235 } rtlvs; 236 uint8_t flags; 237 }; 238 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */ 239 #define F_NOTIF_FEC 0x02 /* fec tlv present */ 240 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */ 241 242 struct if_addr { 243 LIST_ENTRY(if_addr) entry; 244 int af; 245 union ldpd_addr addr; 246 uint8_t prefixlen; 247 union ldpd_addr dstbrd; 248 }; 249 LIST_HEAD(if_addr_head, if_addr); 250 251 struct iface_af { 252 struct iface *iface; 253 int af; 254 int enabled; 255 int state; 256 LIST_HEAD(, adj) adj_list; 257 time_t uptime; 258 struct event hello_timer; 259 uint16_t hello_holdtime; 260 uint16_t hello_interval; 261 }; 262 263 struct iface { 264 LIST_ENTRY(iface) entry; 265 char name[IF_NAMESIZE]; 266 unsigned int ifindex; 267 unsigned int rdomain; 268 struct if_addr_head addr_list; 269 struct in6_addr linklocal; 270 enum iface_type type; 271 uint8_t if_type; 272 uint16_t flags; 273 uint8_t linkstate; 274 struct iface_af ipv4; 275 struct iface_af ipv6; 276 }; 277 278 /* source of targeted hellos */ 279 struct tnbr { 280 LIST_ENTRY(tnbr) entry; 281 struct event hello_timer; 282 struct adj *adj; 283 int af; 284 union ldpd_addr addr; 285 int state; 286 uint16_t hello_holdtime; 287 uint16_t hello_interval; 288 uint16_t pw_count; 289 uint8_t flags; 290 }; 291 #define F_TNBR_CONFIGURED 0x01 292 #define F_TNBR_DYNAMIC 0x02 293 294 enum auth_method { 295 AUTH_NONE, 296 AUTH_MD5SIG 297 }; 298 299 /* neighbor specific parameters */ 300 struct nbr_params { 301 LIST_ENTRY(nbr_params) entry; 302 struct in_addr lsr_id; 303 uint16_t keepalive; 304 int gtsm_enabled; 305 uint8_t gtsm_hops; 306 struct { 307 enum auth_method method; 308 char md5key[TCP_MD5_KEY_LEN]; 309 uint8_t md5key_len; 310 } auth; 311 uint8_t flags; 312 }; 313 #define F_NBRP_KEEPALIVE 0x01 314 #define F_NBRP_GTSM 0x02 315 #define F_NBRP_GTSM_HOPS 0x04 316 317 struct l2vpn_if { 318 LIST_ENTRY(l2vpn_if) entry; 319 struct l2vpn *l2vpn; 320 char ifname[IF_NAMESIZE]; 321 unsigned int ifindex; 322 uint16_t flags; 323 uint8_t link_state; 324 }; 325 326 struct l2vpn_pw { 327 LIST_ENTRY(l2vpn_pw) entry; 328 struct l2vpn *l2vpn; 329 struct in_addr lsr_id; 330 int af; 331 union ldpd_addr addr; 332 uint32_t pwid; 333 char ifname[IF_NAMESIZE]; 334 unsigned int ifindex; 335 uint32_t remote_group; 336 uint16_t remote_mtu; 337 uint32_t remote_status; 338 uint8_t flags; 339 }; 340 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */ 341 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */ 342 #define F_PW_CWORD_CONF 0x04 /* control word configured */ 343 #define F_PW_CWORD 0x08 /* control word negotiated */ 344 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */ 345 346 struct l2vpn { 347 LIST_ENTRY(l2vpn) entry; 348 char name[L2VPN_NAME_LEN]; 349 int type; 350 int pw_type; 351 int mtu; 352 char br_ifname[IF_NAMESIZE]; 353 unsigned int br_ifindex; 354 LIST_HEAD(, l2vpn_if) if_list; 355 LIST_HEAD(, l2vpn_pw) pw_list; 356 }; 357 #define L2VPN_TYPE_VPWS 1 358 #define L2VPN_TYPE_VPLS 2 359 360 /* ldp_conf */ 361 enum ldpd_process { 362 PROC_MAIN, 363 PROC_LDP_ENGINE, 364 PROC_LDE_ENGINE 365 } ldpd_process; 366 367 static const char * const log_procnames[] = { 368 "parent", 369 "ldpe", 370 "lde" 371 }; 372 373 enum socket_type { 374 LDP_SOCKET_DISC, 375 LDP_SOCKET_EDISC, 376 LDP_SOCKET_SESSION 377 }; 378 379 enum hello_type { 380 HELLO_LINK, 381 HELLO_TARGETED 382 }; 383 384 struct ldpd_af_conf { 385 uint16_t keepalive; 386 uint16_t thello_holdtime; 387 uint16_t thello_interval; 388 union ldpd_addr trans_addr; 389 int flags; 390 }; 391 #define F_LDPD_AF_ENABLED 0x0001 392 #define F_LDPD_AF_THELLO_ACCEPT 0x0002 393 #define F_LDPD_AF_EXPNULL 0x0004 394 #define F_LDPD_AF_NO_GTSM 0x0008 395 396 struct ldpd_conf { 397 struct in_addr rtr_id; 398 unsigned int rdomain; 399 struct ldpd_af_conf ipv4; 400 struct ldpd_af_conf ipv6; 401 LIST_HEAD(, iface) iface_list; 402 LIST_HEAD(, tnbr) tnbr_list; 403 LIST_HEAD(, nbr_params) nbrp_list; 404 LIST_HEAD(, l2vpn) l2vpn_list; 405 uint16_t trans_pref; 406 int flags; 407 }; 408 #define F_LDPD_NO_FIB_UPDATE 0x0001 409 #define F_LDPD_DS_CISCO_INTEROP 0x0002 410 411 struct ldpd_af_global { 412 struct event disc_ev; 413 struct event edisc_ev; 414 int ldp_disc_socket; 415 int ldp_edisc_socket; 416 int ldp_session_socket; 417 }; 418 419 struct ldpd_global { 420 int cmd_opts; 421 char *csock; 422 time_t uptime; 423 struct ldpd_af_global ipv4; 424 struct ldpd_af_global ipv6; 425 uint32_t conf_seqnum; 426 int pfkeysock; 427 struct if_addr_head addr_list; 428 LIST_HEAD(, adj) adj_list; 429 struct in_addr mcast_addr_v4; 430 struct in6_addr mcast_addr_v6; 431 TAILQ_HEAD(, pending_conn) pending_conns; 432 }; 433 434 /* kroute */ 435 struct kroute { 436 int af; 437 union ldpd_addr prefix; 438 uint8_t prefixlen; 439 union ldpd_addr nexthop; 440 uint32_t local_label; 441 uint32_t remote_label; 442 unsigned short ifindex; 443 uint8_t priority; 444 uint16_t flags; 445 }; 446 447 struct kpw { 448 unsigned short ifindex; 449 int pw_type; 450 int af; 451 union ldpd_addr nexthop; 452 uint32_t local_label; 453 uint32_t remote_label; 454 uint8_t flags; 455 }; 456 457 struct kaddr { 458 unsigned short ifindex; 459 int af; 460 union ldpd_addr addr; 461 uint8_t prefixlen; 462 union ldpd_addr dstbrd; 463 }; 464 465 struct kif { 466 char ifname[IF_NAMESIZE]; 467 unsigned short ifindex; 468 int flags; 469 uint8_t link_state; 470 int mtu; 471 unsigned int rdomain; 472 uint8_t if_type; 473 uint64_t baudrate; 474 }; 475 476 /* control data structures */ 477 struct ctl_iface { 478 int af; 479 char name[IF_NAMESIZE]; 480 unsigned int ifindex; 481 int state; 482 uint16_t flags; 483 uint8_t linkstate; 484 enum iface_type type; 485 uint8_t if_type; 486 uint16_t hello_holdtime; 487 uint16_t hello_interval; 488 time_t uptime; 489 uint16_t adj_cnt; 490 }; 491 492 struct ctl_adj { 493 int af; 494 struct in_addr id; 495 enum hello_type type; 496 char ifname[IF_NAMESIZE]; 497 union ldpd_addr src_addr; 498 uint16_t holdtime; 499 union ldpd_addr trans_addr; 500 }; 501 502 struct ctl_nbr { 503 int af; 504 struct in_addr id; 505 union ldpd_addr laddr; 506 union ldpd_addr raddr; 507 time_t uptime; 508 int nbr_state; 509 }; 510 511 struct ctl_rt { 512 int af; 513 union ldpd_addr prefix; 514 uint8_t prefixlen; 515 struct in_addr nexthop; /* lsr-id */ 516 uint32_t local_label; 517 uint32_t remote_label; 518 uint8_t flags; 519 uint8_t in_use; 520 }; 521 522 struct ctl_pw { 523 uint16_t type; 524 char ifname[IF_NAMESIZE]; 525 uint32_t pwid; 526 struct in_addr lsr_id; 527 uint32_t local_label; 528 uint32_t local_gid; 529 uint16_t local_ifmtu; 530 uint32_t remote_label; 531 uint32_t remote_gid; 532 uint16_t remote_ifmtu; 533 uint32_t status; 534 }; 535 536 extern struct ldpd_conf *ldpd_conf; 537 extern struct ldpd_global global; 538 539 /* parse.y */ 540 struct ldpd_conf *parse_config(char *); 541 int cmdline_symset(char *); 542 543 /* kroute.c */ 544 int kif_init(void); 545 int kr_init(int, unsigned int); 546 void kif_redistribute(const char *); 547 int kr_change(struct kroute *); 548 int kr_delete(struct kroute *); 549 void kr_shutdown(void); 550 void kr_fib_couple(void); 551 void kr_fib_decouple(void); 552 void kr_change_egress_label(int, int); 553 void kr_show_route(struct imsg *); 554 void kr_ifinfo(char *, pid_t); 555 struct kif *kif_findname(char *); 556 void kif_clear(void); 557 int kmpw_set(struct kpw *); 558 int kmpw_unset(struct kpw *); 559 560 /* util.c */ 561 uint8_t mask2prefixlen(in_addr_t); 562 uint8_t mask2prefixlen6(struct sockaddr_in6 *); 563 in_addr_t prefixlen2mask(uint8_t); 564 struct in6_addr *prefixlen2mask6(uint8_t); 565 void ldp_applymask(int, union ldpd_addr *, 566 const union ldpd_addr *, int); 567 int ldp_addrcmp(int, const union ldpd_addr *, 568 const union ldpd_addr *); 569 int ldp_addrisset(int, const union ldpd_addr *); 570 int ldp_prefixcmp(int, const union ldpd_addr *, 571 const union ldpd_addr *, uint8_t); 572 int bad_addr_v4(struct in_addr); 573 int bad_addr_v6(struct in6_addr *); 574 int bad_addr(int, union ldpd_addr *); 575 void embedscope(struct sockaddr_in6 *); 576 void recoverscope(struct sockaddr_in6 *); 577 void addscope(struct sockaddr_in6 *, uint32_t); 578 void clearscope(struct in6_addr *); 579 struct sockaddr *addr2sa(int af, union ldpd_addr *, uint16_t); 580 void sa2addr(struct sockaddr *, int *, union ldpd_addr *); 581 582 /* ldpd.c */ 583 void main_imsg_compose_ldpe(int, pid_t, void *, uint16_t); 584 void main_imsg_compose_lde(int, pid_t, void *, uint16_t); 585 void imsg_event_add(struct imsgev *); 586 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t, 587 int, void *, uint16_t); 588 void evbuf_enqueue(struct evbuf *, struct ibuf *); 589 void evbuf_event_add(struct evbuf *); 590 void evbuf_init(struct evbuf *, int, void (*)(int, short, void *), void *); 591 void evbuf_clear(struct evbuf *); 592 struct ldpd_af_conf *ldp_af_conf_get(struct ldpd_conf *, int); 593 struct ldpd_af_global *ldp_af_global_get(struct ldpd_global *, int); 594 int ldp_is_dual_stack(struct ldpd_conf *); 595 void merge_config(struct ldpd_conf *, struct ldpd_conf *); 596 struct ldpd_conf *config_new_empty(void); 597 void config_clear(struct ldpd_conf *); 598 599 /* socket.c */ 600 int ldp_create_socket(int, enum socket_type); 601 void sock_set_recvbuf(int); 602 int sock_set_reuse(int, int); 603 int sock_set_bindany(int, int); 604 int sock_set_ipv4_tos(int, int); 605 int sock_set_ipv4_recvif(int, int); 606 int sock_set_ipv4_minttl(int, int); 607 int sock_set_ipv4_ucast_ttl(int fd, int); 608 int sock_set_ipv4_mcast_ttl(int, uint8_t); 609 int sock_set_ipv4_mcast(struct iface *); 610 int sock_set_ipv4_mcast_loop(int); 611 int sock_set_ipv6_dscp(int, int); 612 int sock_set_ipv6_pktinfo(int, int); 613 int sock_set_ipv6_minhopcount(int, int); 614 int sock_set_ipv6_ucast_hops(int, int); 615 int sock_set_ipv6_mcast_hops(int, int); 616 int sock_set_ipv6_mcast(struct iface *); 617 int sock_set_ipv6_mcast_loop(int); 618 619 /* printconf.c */ 620 void print_config(struct ldpd_conf *); 621 622 /* logmsg.h */ 623 struct in6_addr; 624 union ldpd_addr; 625 struct hello_source; 626 struct fec; 627 628 const char *log_sockaddr(void *); 629 const char *log_in6addr(const struct in6_addr *); 630 const char *log_in6addr_scope(const struct in6_addr *, unsigned int); 631 const char *log_addr(int, const union ldpd_addr *); 632 char *log_label(uint32_t); 633 char *log_hello_src(const struct hello_source *); 634 const char *log_map(const struct map *); 635 const char *log_fec(const struct fec *); 636 const char *af_name(int); 637 const char *socket_name(int); 638 const char *nbr_state_name(int); 639 const char *if_state_name(int); 640 const char *if_type_name(enum iface_type); 641 const char *msg_name(uint16_t); 642 const char *status_code_name(uint32_t); 643 const char *pw_type_name(uint16_t); 644 645 #endif /* _LDPD_H_ */ 646