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.
26 #include "openbsd-tree.h"
38 #define CONF_FILE "/etc/ldpd.conf"
39 #define LDPD_USER "_ldpd"
41 #define LDPD_FD_ASYNC 3
42 #define LDPD_FD_SYNC 4
44 #define LDPD_OPT_VERBOSE 0x00000001
45 #define LDPD_OPT_VERBOSE2 0x00000002
46 #define LDPD_OPT_NOACTION 0x00000004
48 #define TCP_MD5_KEY_LEN 80
50 #define RT_BUF_SIZE 16384
51 #define MAX_RTSOCK_BUF 128 * 1024
52 #define LDP_BACKLOG 128
54 #define F_LDPD_INSERTED 0x0001
55 #define F_CONNECTED 0x0002
56 #define F_STATIC 0x0004
57 #define F_DYNAMIC 0x0008
58 #define F_REJECT 0x0010
59 #define F_BLACKHOLE 0x0020
60 #define F_REDISTRIBUTED 0x0040
65 int (*handler
)(struct thread
*);
71 int (*handler_write
)(struct thread
*);
72 struct thread
*ev_write
;
73 int (*handler_read
)(struct thread
*);
74 struct thread
*ev_read
;
80 IMSG_CTL_SHOW_INTERFACE
,
81 IMSG_CTL_SHOW_DISCOVERY
,
82 IMSG_CTL_SHOW_DISCOVERY_DTL
,
83 IMSG_CTL_SHOW_DISC_IFACE
,
84 IMSG_CTL_SHOW_DISC_TNBR
,
85 IMSG_CTL_SHOW_DISC_ADJ
,
87 IMSG_CTL_SHOW_NBR_DISC
,
88 IMSG_CTL_SHOW_NBR_END
,
90 IMSG_CTL_SHOW_LIB_BEGIN
,
91 IMSG_CTL_SHOW_LIB_SENT
,
92 IMSG_CTL_SHOW_LIB_RCVD
,
93 IMSG_CTL_SHOW_LIB_END
,
94 IMSG_CTL_SHOW_L2VPN_PW
,
95 IMSG_CTL_SHOW_L2VPN_BINDING
,
98 IMSG_CTL_FIB_DECOUPLE
,
100 IMSG_CTL_KROUTE_ADDR
,
103 IMSG_CTL_LOG_VERBOSE
,
115 IMSG_LABEL_MAPPING_FULL
,
121 IMSG_REQUEST_ADD_END
,
123 IMSG_MAPPING_ADD_END
,
125 IMSG_RELEASE_ADD_END
,
127 IMSG_WITHDRAW_ADD_END
,
131 IMSG_NOTIFICATION_SEND
,
139 IMSG_REQUEST_SOCKETS
,
146 IMSG_RECONF_L2VPN_IF
,
147 IMSG_RECONF_L2VPN_PW
,
148 IMSG_RECONF_L2VPN_IPW
,
162 char ctl_sock_path
[MAXPATHLEN
];
163 char zclient_serv_path
[MAXPATHLEN
];
164 unsigned short instance
;
168 char name
[ACL_NAMSIZ
];
169 enum access_type type
;
177 #define IN6_IS_SCOPE_EMBED(a) \
178 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
179 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
180 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
182 /* interface states */
183 #define IF_STA_DOWN 0x01
184 #define IF_STA_ACTIVE 0x02
186 /* targeted neighbor states */
187 #define TNBR_STA_DOWN 0x01
188 #define TNBR_STA_ACTIVE 0x02
190 /* interface types */
196 /* neighbor states */
197 #define NBR_STA_PRESENT 0x0001
198 #define NBR_STA_INITIAL 0x0002
199 #define NBR_STA_OPENREC 0x0004
200 #define NBR_STA_OPENSENT 0x0008
201 #define NBR_STA_OPER 0x0010
202 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
203 NBR_STA_OPENSENT | NBR_STA_OPER)
205 /* neighbor events */
210 NBR_EVT_CLOSE_SESSION
,
212 NBR_EVT_KEEPALIVE_RCVD
,
218 /* neighbor actions */
221 NBR_ACT_RST_KTIMEOUT
,
224 NBR_ACT_CONNECT_SETUP
,
225 NBR_ACT_PASSIVE_INIT
,
226 NBR_ACT_KEEPALIVE_SEND
,
227 NBR_ACT_CLOSE_SESSION
230 /* forward declarations */
231 RB_HEAD(global_adj_head
, adj
);
232 RB_HEAD(nbr_adj_head
, adj
);
233 RB_HEAD(ia_adj_head
, adj
);
241 union ldpd_addr prefix
;
259 uint32_t status_code
;
268 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
269 #define F_MAP_STATUS 0x02 /* status */
270 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
271 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
272 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
273 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
276 uint32_t status_code
;
277 uint32_t msg_id
; /* network byte order */
278 uint16_t msg_type
; /* network byte order */
288 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
289 #define F_NOTIF_FEC 0x02 /* fec tlv present */
290 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
293 LIST_ENTRY(if_addr
) entry
;
295 union ldpd_addr addr
;
297 union ldpd_addr dstbrd
;
299 LIST_HEAD(if_addr_head
, if_addr
);
306 struct ia_adj_head adj_tree
;
308 struct thread
*hello_timer
;
309 uint16_t hello_holdtime
;
310 uint16_t hello_interval
;
314 RB_ENTRY(iface
) entry
;
315 char name
[IF_NAMESIZE
];
317 struct if_addr_head addr_list
;
318 struct in6_addr linklocal
;
319 enum iface_type type
;
321 struct iface_af ipv4
;
322 struct iface_af ipv6
;
325 RB_HEAD(iface_head
, iface
);
326 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
327 DECLARE_QOBJ_TYPE(iface
)
329 /* source of targeted hellos */
331 RB_ENTRY(tnbr
) entry
;
332 struct thread
*hello_timer
;
335 union ldpd_addr addr
;
341 RB_HEAD(tnbr_head
, tnbr
);
342 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
343 DECLARE_QOBJ_TYPE(tnbr
)
344 #define F_TNBR_CONFIGURED 0x01
345 #define F_TNBR_DYNAMIC 0x02
352 /* neighbor specific parameters */
354 RB_ENTRY(nbr_params
) entry
;
355 struct in_addr lsr_id
;
360 enum auth_method method
;
361 char md5key
[TCP_MD5_KEY_LEN
];
367 RB_HEAD(nbrp_head
, nbr_params
);
368 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
369 DECLARE_QOBJ_TYPE(nbr_params
)
370 #define F_NBRP_KEEPALIVE 0x01
371 #define F_NBRP_GTSM 0x02
372 #define F_NBRP_GTSM_HOPS 0x04
375 uint32_t kalive_sent
;
376 uint32_t kalive_rcvd
;
379 uint32_t addrwdraw_sent
;
380 uint32_t addrwdraw_rcvd
;
383 uint32_t capability_sent
;
384 uint32_t capability_rcvd
;
385 uint32_t labelmap_sent
;
386 uint32_t labelmap_rcvd
;
387 uint32_t labelreq_sent
;
388 uint32_t labelreq_rcvd
;
389 uint32_t labelwdraw_sent
;
390 uint32_t labelwdraw_rcvd
;
391 uint32_t labelrel_sent
;
392 uint32_t labelrel_rcvd
;
393 uint32_t labelabreq_sent
;
394 uint32_t labelabreq_rcvd
;
398 RB_ENTRY(l2vpn_if
) entry
;
400 char ifname
[IF_NAMESIZE
];
403 uint8_t mac
[ETH_ALEN
];
406 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
407 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
408 DECLARE_QOBJ_TYPE(l2vpn_if
)
411 RB_ENTRY(l2vpn_pw
) entry
;
413 struct in_addr lsr_id
;
415 union ldpd_addr addr
;
417 char ifname
[IF_NAMESIZE
];
420 uint32_t remote_group
;
422 uint32_t local_status
;
423 uint32_t remote_status
;
427 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
428 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
429 DECLARE_QOBJ_TYPE(l2vpn_pw
)
430 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
431 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
432 #define F_PW_CWORD_CONF 0x04 /* control word configured */
433 #define F_PW_CWORD 0x08 /* control word negotiated */
434 #define F_PW_STATIC_NBR_ADDR 0x10 /* static neighbor address configured */
437 RB_ENTRY(l2vpn
) entry
;
438 char name
[L2VPN_NAME_LEN
];
442 char br_ifname
[IF_NAMESIZE
];
443 ifindex_t br_ifindex
;
444 struct l2vpn_if_head if_tree
;
445 struct l2vpn_pw_head pw_tree
;
446 struct l2vpn_pw_head pw_inactive_tree
;
449 RB_HEAD(l2vpn_head
, l2vpn
);
450 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
451 DECLARE_QOBJ_TYPE(l2vpn
)
452 #define L2VPN_TYPE_VPWS 1
453 #define L2VPN_TYPE_VPLS 2
456 extern enum ldpd_process
{
462 static const char * const log_procnames
[] = {
479 struct ldpd_af_conf
{
481 uint16_t lhello_holdtime
;
482 uint16_t lhello_interval
;
483 uint16_t thello_holdtime
;
484 uint16_t thello_interval
;
485 union ldpd_addr trans_addr
;
486 char acl_thello_accept_from
[ACL_NAMSIZ
];
487 char acl_label_allocate_for
[ACL_NAMSIZ
];
488 char acl_label_advertise_to
[ACL_NAMSIZ
];
489 char acl_label_advertise_for
[ACL_NAMSIZ
];
490 char acl_label_expnull_for
[ACL_NAMSIZ
];
491 char acl_label_accept_from
[ACL_NAMSIZ
];
492 char acl_label_accept_for
[ACL_NAMSIZ
];
495 #define F_LDPD_AF_ENABLED 0x0001
496 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
497 #define F_LDPD_AF_EXPNULL 0x0004
498 #define F_LDPD_AF_NO_GTSM 0x0008
499 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
502 struct in_addr rtr_id
;
503 struct ldpd_af_conf ipv4
;
504 struct ldpd_af_conf ipv6
;
505 struct iface_head iface_tree
;
506 struct tnbr_head tnbr_tree
;
507 struct nbrp_head nbrp_tree
;
508 struct l2vpn_head l2vpn_tree
;
509 uint16_t lhello_holdtime
;
510 uint16_t lhello_interval
;
511 uint16_t thello_holdtime
;
512 uint16_t thello_interval
;
517 DECLARE_QOBJ_TYPE(ldpd_conf
)
518 #define F_LDPD_NO_FIB_UPDATE 0x0001
519 #define F_LDPD_DS_CISCO_INTEROP 0x0002
520 #define F_LDPD_ENABLED 0x0004
521 #define F_LDPD_ORDERED_CONTROL 0x0008
524 struct ldpd_af_global
{
525 struct thread
*disc_ev
;
526 struct thread
*edisc_ev
;
528 int ldp_edisc_socket
;
529 int ldp_session_socket
;
534 struct in_addr rtr_id
;
535 struct ldpd_af_global ipv4
;
536 struct ldpd_af_global ipv6
;
537 uint32_t conf_seqnum
;
539 struct if_addr_head addr_list
;
540 struct global_adj_head adj_tree
;
541 struct in_addr mcast_addr_v4
;
542 struct in6_addr mcast_addr_v6
;
543 TAILQ_HEAD(, pending_conn
) pending_conns
;
549 union ldpd_addr prefix
;
551 union ldpd_addr nexthop
;
552 uint32_t local_label
;
553 uint32_t remote_label
;
556 uint8_t route_instance
;
561 char ifname
[IF_NAMESIZE
];
564 union ldpd_addr addr
;
566 union ldpd_addr dstbrd
;
570 char ifname
[IF_NAMESIZE
];
574 uint8_t mac
[ETH_ALEN
];
579 char acl
[ACL_NAMSIZ
];
581 union ldpd_addr addr
;
585 /* control data structures */
588 char name
[IF_NAMESIZE
];
591 enum iface_type type
;
592 uint16_t hello_holdtime
;
593 uint16_t hello_interval
;
599 char name
[IF_NAMESIZE
];
605 struct ctl_disc_tnbr
{
607 union ldpd_addr addr
;
614 enum hello_type type
;
615 char ifname
[IF_NAMESIZE
];
616 union ldpd_addr src_addr
;
618 uint16_t holdtime_remaining
;
619 union ldpd_addr trans_addr
;
626 union ldpd_addr laddr
;
628 union ldpd_addr raddr
;
630 enum auth_method auth_method
;
634 struct ldp_stats stats
;
640 union ldpd_addr prefix
;
642 struct in_addr nexthop
; /* lsr-id */
643 uint32_t local_label
;
644 uint32_t remote_label
;
652 char l2vpn_name
[L2VPN_NAME_LEN
];
653 char ifname
[IF_NAMESIZE
];
655 struct in_addr lsr_id
;
656 uint32_t local_label
;
658 uint16_t local_ifmtu
;
660 uint32_t remote_label
;
662 uint16_t remote_ifmtu
;
663 uint8_t remote_cword
;
667 extern struct ldpd_conf
*ldpd_conf
, *vty_conf
;
668 extern struct ldpd_global global
;
669 extern struct ldpd_init init
;
672 struct ldpd_conf
*parse_config(char *);
673 int cmdline_symset(char *);
676 void pw2zpw(struct l2vpn_pw
*, struct zapi_pw
*);
677 void kif_redistribute(const char *);
678 int kr_change(struct kroute
*);
679 int kr_delete(struct kroute
*);
680 int kmpw_add(struct zapi_pw
*);
681 int kmpw_del(struct zapi_pw
*);
682 int kmpw_set(struct zapi_pw
*);
683 int kmpw_unset(struct zapi_pw
*);
686 uint8_t mask2prefixlen(in_addr_t
);
687 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
688 in_addr_t
prefixlen2mask(uint8_t);
689 struct in6_addr
*prefixlen2mask6(uint8_t);
690 void ldp_applymask(int, union ldpd_addr
*,
691 const union ldpd_addr
*, int);
692 int ldp_addrcmp(int, const union ldpd_addr
*,
693 const union ldpd_addr
*);
694 int ldp_addrisset(int, const union ldpd_addr
*);
695 int ldp_prefixcmp(int, const union ldpd_addr
*,
696 const union ldpd_addr
*, uint8_t);
697 int bad_addr_v4(struct in_addr
);
698 int bad_addr_v6(struct in6_addr
*);
699 int bad_addr(int, union ldpd_addr
*);
700 void embedscope(struct sockaddr_in6
*);
701 void recoverscope(struct sockaddr_in6
*);
702 void addscope(struct sockaddr_in6
*, uint32_t);
703 void clearscope(struct in6_addr
*);
704 void addr2sa(int af
, const union ldpd_addr
*, uint16_t,
705 union sockunion
*su
);
706 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
708 socklen_t
sockaddr_len(struct sockaddr
*);
711 int ldp_write_handler(struct thread
*);
712 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
713 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
714 int main_imsg_compose_both(enum imsg_type
, void *,
716 void imsg_event_add(struct imsgev
*);
717 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
718 pid_t
, int, void *, uint16_t);
719 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
720 void evbuf_event_add(struct evbuf
*);
721 void evbuf_init(struct evbuf
*, int,
722 int (*)(struct thread
*), void *);
723 void evbuf_clear(struct evbuf
*);
724 int ldp_acl_request(struct imsgev
*, char *, int,
725 union ldpd_addr
*, uint8_t);
726 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
727 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
728 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
729 int ldp_is_dual_stack(struct ldpd_conf
*);
730 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
731 int ldp_config_apply(struct vty
*, struct ldpd_conf
*);
732 void ldp_clear_config(struct ldpd_conf
*);
733 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
734 struct ldpd_conf
*config_new_empty(void);
735 void config_clear(struct ldpd_conf
*);
738 /* NOTE: the parameters' names should be preserved because of codegen */
739 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
741 void iface_del_api(struct ldpd_conf
*conf
,
742 struct iface
*iface
);
743 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
744 union ldpd_addr
*addr
);
745 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
746 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
747 struct in_addr lsr_id
);
748 void nbrp_del_api(struct ldpd_conf
*conf
,
749 struct nbr_params
*nbrp
);
750 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
751 void l2vpn_del_api(struct ldpd_conf
*conf
,
752 struct l2vpn
*l2vpn
);
753 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
754 struct l2vpn
*l2vpn
, const char *ifname
);
755 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
756 struct l2vpn_if
*lif
);
757 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
758 struct l2vpn
*l2vpn
, const char *ifname
);
759 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
760 struct l2vpn_pw
*pw
);
763 int ldp_create_socket(int, enum socket_type
);
764 void sock_set_nonblock(int);
765 void sock_set_cloexec(int);
766 void sock_set_recvbuf(int);
767 int sock_set_reuse(int, int);
768 int sock_set_bindany(int, int);
769 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
770 int sock_set_ipv4_tos(int, int);
771 int sock_set_ipv4_pktinfo(int, int);
772 int sock_set_ipv4_recvdstaddr(int fd
, ifindex_t ifindex
);
773 int sock_set_ipv4_recvif(int, int);
774 int sock_set_ipv4_minttl(int, int);
775 int sock_set_ipv4_ucast_ttl(int fd
, int);
776 int sock_set_ipv4_mcast_ttl(int, uint8_t);
777 int sock_set_ipv4_mcast(struct iface
*);
778 int sock_set_ipv4_mcast_loop(int);
779 int sock_set_ipv6_dscp(int, int);
780 int sock_set_ipv6_pktinfo(int, int);
781 int sock_set_ipv6_minhopcount(int, int);
782 int sock_set_ipv6_ucast_hops(int, int);
783 int sock_set_ipv6_mcast_hops(int, int);
784 int sock_set_ipv6_mcast(struct iface
*);
785 int sock_set_ipv6_mcast_loop(int);
793 const char *log_sockaddr(void *);
794 const char *log_in6addr(const struct in6_addr
*);
795 const char *log_in6addr_scope(const struct in6_addr
*addr
,
797 const char *log_addr(int, const union ldpd_addr
*);
798 char *log_label(uint32_t);
799 const char *log_time(time_t);
800 char *log_hello_src(const struct hello_source
*);
801 const char *log_map(const struct map
*);
802 const char *log_fec(const struct fec
*);
803 const char *af_name(int);
804 const char *socket_name(int);
805 const char *nbr_state_name(int);
806 const char *if_state_name(int);
807 const char *if_type_name(enum iface_type
);
808 const char *msg_name(uint16_t);
809 const char *status_code_name(uint32_t);
810 const char *pw_type_name(uint16_t);
813 extern struct thread_master
*master
;
814 extern char ctl_sock_path
[MAXPATHLEN
];
817 void ldp_zebra_init(struct thread_master
*);
818 void ldp_zebra_destroy(void);
822 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
823 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
824 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
825 (IN6_IS_ADDR_MULTICAST(a) && \
826 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
829 #endif /* _LDPD_H_ */