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>
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.
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.
25 #include "openbsd-queue.h"
26 #include "openbsd-tree.h"
33 #define CONF_FILE "/etc/ldpd.conf"
34 #define LDPD_USER "_ldpd"
36 #define LDPD_FD_ASYNC 3
37 #define LDPD_FD_SYNC 4
39 #define LDPD_OPT_VERBOSE 0x00000001
40 #define LDPD_OPT_VERBOSE2 0x00000002
41 #define LDPD_OPT_NOACTION 0x00000004
43 #define TCP_MD5_KEY_LEN 80
44 #define L2VPN_NAME_LEN 32
46 #define RT_BUF_SIZE 16384
47 #define MAX_RTSOCK_BUF 128 * 1024
48 #define LDP_BACKLOG 128
50 #define F_LDPD_INSERTED 0x0001
51 #define F_CONNECTED 0x0002
52 #define F_STATIC 0x0004
53 #define F_DYNAMIC 0x0008
54 #define F_REJECT 0x0010
55 #define F_BLACKHOLE 0x0020
56 #define F_REDISTRIBUTED 0x0040
61 int (*handler
)(struct thread
*);
67 int (*handler_write
)(struct thread
*);
68 struct thread
*ev_write
;
69 int (*handler_read
)(struct thread
*);
70 struct thread
*ev_read
;
76 IMSG_CTL_SHOW_INTERFACE
,
77 IMSG_CTL_SHOW_DISCOVERY
,
78 IMSG_CTL_SHOW_DISC_IFACE
,
79 IMSG_CTL_SHOW_DISC_TNBR
,
80 IMSG_CTL_SHOW_DISC_ADJ
,
82 IMSG_CTL_SHOW_NBR_DISC
,
83 IMSG_CTL_SHOW_NBR_END
,
85 IMSG_CTL_SHOW_L2VPN_PW
,
86 IMSG_CTL_SHOW_L2VPN_BINDING
,
89 IMSG_CTL_FIB_DECOUPLE
,
104 IMSG_LABEL_MAPPING_FULL
,
110 IMSG_REQUEST_ADD_END
,
112 IMSG_MAPPING_ADD_END
,
114 IMSG_RELEASE_ADD_END
,
116 IMSG_WITHDRAW_ADD_END
,
120 IMSG_NOTIFICATION_SEND
,
124 IMSG_NETWORK_ADD_END
,
129 IMSG_REQUEST_SOCKETS
,
136 IMSG_RECONF_L2VPN_IF
,
137 IMSG_RECONF_L2VPN_PW
,
138 IMSG_RECONF_L2VPN_IPW
,
149 #define IN6_IS_SCOPE_EMBED(a) \
150 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
151 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
152 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
154 /* interface states */
155 #define IF_STA_DOWN 0x01
156 #define IF_STA_ACTIVE 0x02
158 /* targeted neighbor states */
159 #define TNBR_STA_DOWN 0x01
160 #define TNBR_STA_ACTIVE 0x02
162 /* interface types */
168 /* neighbor states */
169 #define NBR_STA_PRESENT 0x0001
170 #define NBR_STA_INITIAL 0x0002
171 #define NBR_STA_OPENREC 0x0004
172 #define NBR_STA_OPENSENT 0x0008
173 #define NBR_STA_OPER 0x0010
174 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
175 NBR_STA_OPENSENT | NBR_STA_OPER)
177 /* neighbor events */
182 NBR_EVT_CLOSE_SESSION
,
184 NBR_EVT_KEEPALIVE_RCVD
,
190 /* neighbor actions */
193 NBR_ACT_RST_KTIMEOUT
,
196 NBR_ACT_CONNECT_SETUP
,
197 NBR_ACT_PASSIVE_INIT
,
198 NBR_ACT_KEEPALIVE_SEND
,
199 NBR_ACT_CLOSE_SESSION
202 /* forward declarations */
203 RB_HEAD(global_adj_head
, adj
);
204 RB_HEAD(nbr_adj_head
, adj
);
205 RB_HEAD(ia_adj_head
, adj
);
213 union ldpd_addr prefix
;
224 uint32_t status_code
;
233 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
234 #define F_MAP_STATUS 0x02 /* status */
235 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
236 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
237 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
238 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
241 uint32_t status_code
;
242 uint32_t msg_id
; /* network byte order */
243 uint16_t msg_type
; /* network byte order */
248 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
249 #define F_NOTIF_FEC 0x02 /* fec tlv present */
252 LIST_ENTRY(if_addr
) entry
;
254 union ldpd_addr addr
;
256 union ldpd_addr dstbrd
;
258 LIST_HEAD(if_addr_head
, if_addr
);
265 struct ia_adj_head adj_tree
;
267 struct thread
*hello_timer
;
268 uint16_t hello_holdtime
;
269 uint16_t hello_interval
;
273 RB_ENTRY(iface
) entry
;
274 char name
[IF_NAMESIZE
];
275 unsigned int ifindex
;
276 struct if_addr_head addr_list
;
277 struct in6_addr linklocal
;
278 enum iface_type type
;
280 struct iface_af ipv4
;
281 struct iface_af ipv6
;
284 RB_HEAD(iface_head
, iface
);
285 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
286 DECLARE_QOBJ_TYPE(iface
)
288 /* source of targeted hellos */
290 RB_ENTRY(tnbr
) entry
;
291 struct thread
*hello_timer
;
294 union ldpd_addr addr
;
300 RB_HEAD(tnbr_head
, tnbr
);
301 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
302 DECLARE_QOBJ_TYPE(tnbr
)
303 #define F_TNBR_CONFIGURED 0x01
304 #define F_TNBR_DYNAMIC 0x02
311 /* neighbor specific parameters */
313 RB_ENTRY(nbr_params
) entry
;
314 struct in_addr lsr_id
;
319 enum auth_method method
;
320 char md5key
[TCP_MD5_KEY_LEN
];
326 RB_HEAD(nbrp_head
, nbr_params
);
327 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
328 DECLARE_QOBJ_TYPE(nbr_params
)
329 #define F_NBRP_KEEPALIVE 0x01
330 #define F_NBRP_GTSM 0x02
331 #define F_NBRP_GTSM_HOPS 0x04
334 RB_ENTRY(l2vpn_if
) entry
;
336 char ifname
[IF_NAMESIZE
];
337 unsigned int ifindex
;
341 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
342 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
343 DECLARE_QOBJ_TYPE(l2vpn_if
)
346 RB_ENTRY(l2vpn_pw
) entry
;
348 struct in_addr lsr_id
;
350 union ldpd_addr addr
;
352 char ifname
[IF_NAMESIZE
];
353 unsigned int ifindex
;
354 uint32_t remote_group
;
356 uint32_t remote_status
;
360 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
361 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
362 DECLARE_QOBJ_TYPE(l2vpn_pw
)
363 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
364 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
365 #define F_PW_CWORD_CONF 0x04 /* control word configured */
366 #define F_PW_CWORD 0x08 /* control word negotiated */
367 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
368 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
371 RB_ENTRY(l2vpn
) entry
;
372 char name
[L2VPN_NAME_LEN
];
376 char br_ifname
[IF_NAMESIZE
];
377 unsigned int br_ifindex
;
378 struct l2vpn_if_head if_tree
;
379 struct l2vpn_pw_head pw_tree
;
380 struct l2vpn_pw_head pw_inactive_tree
;
383 RB_HEAD(l2vpn_head
, l2vpn
);
384 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
385 DECLARE_QOBJ_TYPE(l2vpn
)
386 #define L2VPN_TYPE_VPWS 1
387 #define L2VPN_TYPE_VPLS 2
407 struct ldpd_af_conf
{
409 uint16_t lhello_holdtime
;
410 uint16_t lhello_interval
;
411 uint16_t thello_holdtime
;
412 uint16_t thello_interval
;
413 union ldpd_addr trans_addr
;
416 #define F_LDPD_AF_ENABLED 0x0001
417 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
418 #define F_LDPD_AF_EXPNULL 0x0004
419 #define F_LDPD_AF_NO_GTSM 0x0008
422 struct in_addr rtr_id
;
423 struct ldpd_af_conf ipv4
;
424 struct ldpd_af_conf ipv6
;
425 struct iface_head iface_tree
;
426 struct tnbr_head tnbr_tree
;
427 struct nbrp_head nbrp_tree
;
428 struct l2vpn_head l2vpn_tree
;
429 uint16_t lhello_holdtime
;
430 uint16_t lhello_interval
;
431 uint16_t thello_holdtime
;
432 uint16_t thello_interval
;
437 DECLARE_QOBJ_TYPE(ldpd_conf
)
438 #define F_LDPD_NO_FIB_UPDATE 0x0001
439 #define F_LDPD_DS_CISCO_INTEROP 0x0002
440 #define F_LDPD_ENABLED 0x0004
442 struct ldpd_af_global
{
443 struct thread
*disc_ev
;
444 struct thread
*edisc_ev
;
446 int ldp_edisc_socket
;
447 int ldp_session_socket
;
453 struct in_addr rtr_id
;
454 struct ldpd_af_global ipv4
;
455 struct ldpd_af_global ipv6
;
456 uint32_t conf_seqnum
;
458 struct if_addr_head addr_list
;
459 struct global_adj_head adj_tree
;
460 struct in_addr mcast_addr_v4
;
461 struct in6_addr mcast_addr_v6
;
462 TAILQ_HEAD(, pending_conn
) pending_conns
;
468 union ldpd_addr prefix
;
470 union ldpd_addr nexthop
;
471 uint32_t local_label
;
472 uint32_t remote_label
;
473 unsigned short ifindex
;
479 unsigned short ifindex
;
482 union ldpd_addr nexthop
;
483 uint32_t local_label
;
484 uint32_t remote_label
;
489 unsigned short ifindex
;
491 union ldpd_addr addr
;
493 union ldpd_addr dstbrd
;
497 char ifname
[IF_NAMESIZE
];
498 unsigned short ifindex
;
503 /* control data structures */
506 char name
[IF_NAMESIZE
];
507 unsigned int ifindex
;
510 enum iface_type type
;
511 uint16_t hello_holdtime
;
512 uint16_t hello_interval
;
518 char name
[IF_NAMESIZE
];
524 struct ctl_disc_tnbr
{
526 union ldpd_addr addr
;
533 enum hello_type type
;
534 char ifname
[IF_NAMESIZE
];
535 union ldpd_addr src_addr
;
537 union ldpd_addr trans_addr
;
543 union ldpd_addr laddr
;
545 union ldpd_addr raddr
;
554 union ldpd_addr prefix
;
556 struct in_addr nexthop
; /* lsr-id */
557 uint32_t local_label
;
558 uint32_t remote_label
;
566 char l2vpn_name
[L2VPN_NAME_LEN
];
567 char ifname
[IF_NAMESIZE
];
569 struct in_addr lsr_id
;
570 uint32_t local_label
;
572 uint16_t local_ifmtu
;
574 uint32_t remote_label
;
576 uint16_t remote_ifmtu
;
577 uint8_t remote_cword
;
581 extern struct ldpd_conf
*ldpd_conf
;
582 extern struct ldpd_global global
;
585 struct ldpd_conf
*parse_config(char *);
586 int cmdline_symset(char *);
589 void kif_redistribute(const char *);
590 int kr_change(struct kroute
*);
591 int kr_delete(struct kroute
*);
592 int kmpw_set(struct kpw
*);
593 int kmpw_unset(struct kpw
*);
596 uint8_t mask2prefixlen(in_addr_t
);
597 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
598 in_addr_t
prefixlen2mask(uint8_t);
599 struct in6_addr
*prefixlen2mask6(uint8_t);
600 void ldp_applymask(int, union ldpd_addr
*,
601 const union ldpd_addr
*, int);
602 int ldp_addrcmp(int, const union ldpd_addr
*,
603 const union ldpd_addr
*);
604 int ldp_addrisset(int, const union ldpd_addr
*);
605 int ldp_prefixcmp(int, const union ldpd_addr
*,
606 const union ldpd_addr
*, uint8_t);
607 int bad_addr_v4(struct in_addr
);
608 int bad_addr_v6(struct in6_addr
*);
609 int bad_addr(int, union ldpd_addr
*);
610 void embedscope(struct sockaddr_in6
*);
611 void recoverscope(struct sockaddr_in6
*);
612 void addscope(struct sockaddr_in6
*, uint32_t);
613 void clearscope(struct in6_addr
*);
614 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
615 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
617 socklen_t
sockaddr_len(struct sockaddr
*);
620 int ldp_write_handler(struct thread
*);
621 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
622 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
623 int main_imsg_compose_both(enum imsg_type
, void *,
625 void imsg_event_add(struct imsgev
*);
626 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
627 pid_t
, int, void *, uint16_t);
628 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
629 void evbuf_event_add(struct evbuf
*);
630 void evbuf_init(struct evbuf
*, int,
631 int (*)(struct thread
*), void *);
632 void evbuf_clear(struct evbuf
*);
633 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
634 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
635 int ldp_is_dual_stack(struct ldpd_conf
*);
636 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
637 int ldp_reload(struct ldpd_conf
*);
638 int ldp_reload_ref(struct ldpd_conf
*, void **);
639 struct ldpd_conf
*ldp_dup_config_ref(struct ldpd_conf
*, void **ref
);
640 struct ldpd_conf
*ldp_dup_config(struct ldpd_conf
*);
641 void ldp_clear_config(struct ldpd_conf
*);
642 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
643 struct ldpd_conf
*config_new_empty(void);
644 void config_clear(struct ldpd_conf
*);
647 /* NOTE: the parameters' names should be preserved because of codegen */
648 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
650 void iface_del_api(struct ldpd_conf
*conf
,
651 struct iface
*iface
);
652 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
653 union ldpd_addr
*addr
);
654 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
655 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
656 struct in_addr lsr_id
);
657 void nbrp_del_api(struct ldpd_conf
*conf
,
658 struct nbr_params
*nbrp
);
659 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
660 void l2vpn_del_api(struct ldpd_conf
*conf
,
661 struct l2vpn
*l2vpn
);
662 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
663 struct l2vpn
*l2vpn
, const char *ifname
);
664 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
665 struct l2vpn_if
*lif
);
666 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
667 struct l2vpn
*l2vpn
, const char *ifname
);
668 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
669 struct l2vpn_pw
*pw
);
672 int ldp_create_socket(int, enum socket_type
);
673 void sock_set_nonblock(int);
674 void sock_set_cloexec(int);
675 void sock_set_recvbuf(int);
676 int sock_set_reuse(int, int);
677 int sock_set_bindany(int, int);
678 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
679 int sock_set_ipv4_tos(int, int);
680 int sock_set_ipv4_pktinfo(int, int);
681 int sock_set_ipv4_recvdstaddr(int, int);
682 int sock_set_ipv4_recvif(int, int);
683 int sock_set_ipv4_minttl(int, int);
684 int sock_set_ipv4_ucast_ttl(int fd
, int);
685 int sock_set_ipv4_mcast_ttl(int, uint8_t);
686 int sock_set_ipv4_mcast(struct iface
*);
687 int sock_set_ipv4_mcast_loop(int);
688 int sock_set_ipv6_dscp(int, int);
689 int sock_set_ipv6_pktinfo(int, int);
690 int sock_set_ipv6_minhopcount(int, int);
691 int sock_set_ipv6_ucast_hops(int, int);
692 int sock_set_ipv6_mcast_hops(int, int);
693 int sock_set_ipv6_mcast(struct iface
*);
694 int sock_set_ipv6_mcast_loop(int);
697 extern struct thread_master
*master
;
700 void ldp_zebra_init(struct thread_master
*);
701 void ldp_zebra_destroy(void);
705 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
706 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
707 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
708 (IN6_IS_ADDR_MULTICAST(a) && \
709 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
712 #endif /* _LDPD_H_ */