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"
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
,
160 char ctl_sock_path
[MAXPATHLEN
];
161 char zclient_serv_path
[MAXPATHLEN
];
170 #define IN6_IS_SCOPE_EMBED(a) \
171 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
172 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
173 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
175 /* interface states */
176 #define IF_STA_DOWN 0x01
177 #define IF_STA_ACTIVE 0x02
179 /* targeted neighbor states */
180 #define TNBR_STA_DOWN 0x01
181 #define TNBR_STA_ACTIVE 0x02
183 /* interface types */
189 /* neighbor states */
190 #define NBR_STA_PRESENT 0x0001
191 #define NBR_STA_INITIAL 0x0002
192 #define NBR_STA_OPENREC 0x0004
193 #define NBR_STA_OPENSENT 0x0008
194 #define NBR_STA_OPER 0x0010
195 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
196 NBR_STA_OPENSENT | NBR_STA_OPER)
198 /* neighbor events */
203 NBR_EVT_CLOSE_SESSION
,
205 NBR_EVT_KEEPALIVE_RCVD
,
211 /* neighbor actions */
214 NBR_ACT_RST_KTIMEOUT
,
217 NBR_ACT_CONNECT_SETUP
,
218 NBR_ACT_PASSIVE_INIT
,
219 NBR_ACT_KEEPALIVE_SEND
,
220 NBR_ACT_CLOSE_SESSION
223 /* forward declarations */
224 RB_HEAD(global_adj_head
, adj
);
225 RB_HEAD(nbr_adj_head
, adj
);
226 RB_HEAD(ia_adj_head
, adj
);
234 union ldpd_addr prefix
;
252 uint32_t status_code
;
261 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
262 #define F_MAP_STATUS 0x02 /* status */
263 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
264 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
265 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
266 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
269 uint32_t status_code
;
270 uint32_t msg_id
; /* network byte order */
271 uint16_t msg_type
; /* network byte order */
281 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
282 #define F_NOTIF_FEC 0x02 /* fec tlv present */
283 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
286 LIST_ENTRY(if_addr
) entry
;
288 union ldpd_addr addr
;
290 union ldpd_addr dstbrd
;
292 LIST_HEAD(if_addr_head
, if_addr
);
299 struct ia_adj_head adj_tree
;
301 struct thread
*hello_timer
;
302 uint16_t hello_holdtime
;
303 uint16_t hello_interval
;
307 RB_ENTRY(iface
) entry
;
308 char name
[IF_NAMESIZE
];
309 unsigned int ifindex
;
310 struct if_addr_head addr_list
;
311 struct in6_addr linklocal
;
312 enum iface_type type
;
314 struct iface_af ipv4
;
315 struct iface_af ipv6
;
318 RB_HEAD(iface_head
, iface
);
319 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
320 DECLARE_QOBJ_TYPE(iface
)
322 /* source of targeted hellos */
324 RB_ENTRY(tnbr
) entry
;
325 struct thread
*hello_timer
;
328 union ldpd_addr addr
;
334 RB_HEAD(tnbr_head
, tnbr
);
335 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
336 DECLARE_QOBJ_TYPE(tnbr
)
337 #define F_TNBR_CONFIGURED 0x01
338 #define F_TNBR_DYNAMIC 0x02
345 /* neighbor specific parameters */
347 RB_ENTRY(nbr_params
) entry
;
348 struct in_addr lsr_id
;
353 enum auth_method method
;
354 char md5key
[TCP_MD5_KEY_LEN
];
360 RB_HEAD(nbrp_head
, nbr_params
);
361 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
362 DECLARE_QOBJ_TYPE(nbr_params
)
363 #define F_NBRP_KEEPALIVE 0x01
364 #define F_NBRP_GTSM 0x02
365 #define F_NBRP_GTSM_HOPS 0x04
368 uint32_t kalive_sent
;
369 uint32_t kalive_rcvd
;
372 uint32_t addrwdraw_sent
;
373 uint32_t addrwdraw_rcvd
;
376 uint32_t capability_sent
;
377 uint32_t capability_rcvd
;
378 uint32_t labelmap_sent
;
379 uint32_t labelmap_rcvd
;
380 uint32_t labelreq_sent
;
381 uint32_t labelreq_rcvd
;
382 uint32_t labelwdraw_sent
;
383 uint32_t labelwdraw_rcvd
;
384 uint32_t labelrel_sent
;
385 uint32_t labelrel_rcvd
;
386 uint32_t labelabreq_sent
;
387 uint32_t labelabreq_rcvd
;
391 RB_ENTRY(l2vpn_if
) entry
;
393 char ifname
[IF_NAMESIZE
];
394 unsigned int ifindex
;
396 uint8_t mac
[ETH_ALEN
];
399 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
400 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
401 DECLARE_QOBJ_TYPE(l2vpn_if
)
404 RB_ENTRY(l2vpn_pw
) entry
;
406 struct in_addr lsr_id
;
408 union ldpd_addr addr
;
410 char ifname
[IF_NAMESIZE
];
411 unsigned int ifindex
;
412 uint32_t remote_group
;
414 uint32_t local_status
;
415 uint32_t remote_status
;
419 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
420 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
421 DECLARE_QOBJ_TYPE(l2vpn_pw
)
422 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
423 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
424 #define F_PW_CWORD_CONF 0x04 /* control word configured */
425 #define F_PW_CWORD 0x08 /* control word negotiated */
426 #define F_PW_STATIC_NBR_ADDR 0x10 /* static neighbor address configured */
429 RB_ENTRY(l2vpn
) entry
;
430 char name
[L2VPN_NAME_LEN
];
434 char br_ifname
[IF_NAMESIZE
];
435 unsigned int br_ifindex
;
436 struct l2vpn_if_head if_tree
;
437 struct l2vpn_pw_head pw_tree
;
438 struct l2vpn_pw_head pw_inactive_tree
;
441 RB_HEAD(l2vpn_head
, l2vpn
);
442 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
443 DECLARE_QOBJ_TYPE(l2vpn
)
444 #define L2VPN_TYPE_VPWS 1
445 #define L2VPN_TYPE_VPLS 2
454 static const char * const log_procnames
[] = {
471 struct ldpd_af_conf
{
473 uint16_t lhello_holdtime
;
474 uint16_t lhello_interval
;
475 uint16_t thello_holdtime
;
476 uint16_t thello_interval
;
477 union ldpd_addr trans_addr
;
478 char acl_thello_accept_from
[ACL_NAMSIZ
];
479 char acl_label_allocate_for
[ACL_NAMSIZ
];
480 char acl_label_advertise_to
[ACL_NAMSIZ
];
481 char acl_label_advertise_for
[ACL_NAMSIZ
];
482 char acl_label_expnull_for
[ACL_NAMSIZ
];
483 char acl_label_accept_from
[ACL_NAMSIZ
];
484 char acl_label_accept_for
[ACL_NAMSIZ
];
487 #define F_LDPD_AF_ENABLED 0x0001
488 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
489 #define F_LDPD_AF_EXPNULL 0x0004
490 #define F_LDPD_AF_NO_GTSM 0x0008
491 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
494 struct in_addr rtr_id
;
495 struct ldpd_af_conf ipv4
;
496 struct ldpd_af_conf ipv6
;
497 struct iface_head iface_tree
;
498 struct tnbr_head tnbr_tree
;
499 struct nbrp_head nbrp_tree
;
500 struct l2vpn_head l2vpn_tree
;
501 uint16_t lhello_holdtime
;
502 uint16_t lhello_interval
;
503 uint16_t thello_holdtime
;
504 uint16_t thello_interval
;
509 DECLARE_QOBJ_TYPE(ldpd_conf
)
510 #define F_LDPD_NO_FIB_UPDATE 0x0001
511 #define F_LDPD_DS_CISCO_INTEROP 0x0002
512 #define F_LDPD_ENABLED 0x0004
514 struct ldpd_af_global
{
515 struct thread
*disc_ev
;
516 struct thread
*edisc_ev
;
518 int ldp_edisc_socket
;
519 int ldp_session_socket
;
524 struct in_addr rtr_id
;
525 struct ldpd_af_global ipv4
;
526 struct ldpd_af_global ipv6
;
527 uint32_t conf_seqnum
;
529 struct if_addr_head addr_list
;
530 struct global_adj_head adj_tree
;
531 struct in_addr mcast_addr_v4
;
532 struct in6_addr mcast_addr_v6
;
533 TAILQ_HEAD(, pending_conn
) pending_conns
;
539 union ldpd_addr prefix
;
541 union ldpd_addr nexthop
;
542 uint32_t local_label
;
543 uint32_t remote_label
;
544 unsigned short ifindex
;
550 char ifname
[IF_NAMESIZE
];
551 unsigned short ifindex
;
553 union ldpd_addr addr
;
555 union ldpd_addr dstbrd
;
559 char ifname
[IF_NAMESIZE
];
560 unsigned short ifindex
;
563 uint8_t mac
[ETH_ALEN
];
568 char acl
[ACL_NAMSIZ
];
570 union ldpd_addr addr
;
574 /* control data structures */
577 char name
[IF_NAMESIZE
];
578 unsigned int ifindex
;
580 enum iface_type type
;
581 uint16_t hello_holdtime
;
582 uint16_t hello_interval
;
588 char name
[IF_NAMESIZE
];
594 struct ctl_disc_tnbr
{
596 union ldpd_addr addr
;
603 enum hello_type type
;
604 char ifname
[IF_NAMESIZE
];
605 union ldpd_addr src_addr
;
607 uint16_t holdtime_remaining
;
608 union ldpd_addr trans_addr
;
615 union ldpd_addr laddr
;
617 union ldpd_addr raddr
;
619 enum auth_method auth_method
;
623 struct ldp_stats stats
;
629 union ldpd_addr prefix
;
631 struct in_addr nexthop
; /* lsr-id */
632 uint32_t local_label
;
633 uint32_t remote_label
;
641 char l2vpn_name
[L2VPN_NAME_LEN
];
642 char ifname
[IF_NAMESIZE
];
644 struct in_addr lsr_id
;
645 uint32_t local_label
;
647 uint16_t local_ifmtu
;
649 uint32_t remote_label
;
651 uint16_t remote_ifmtu
;
652 uint8_t remote_cword
;
656 extern struct ldpd_conf
*ldpd_conf
, *vty_conf
;
657 extern struct ldpd_global global
;
658 extern struct ldpd_init init
;
661 struct ldpd_conf
*parse_config(char *);
662 int cmdline_symset(char *);
665 void pw2zpw(struct l2vpn_pw
*, struct zapi_pw
*);
666 void kif_redistribute(const char *);
667 int kr_change(struct kroute
*);
668 int kr_delete(struct kroute
*);
669 int kmpw_add(struct zapi_pw
*);
670 int kmpw_del(struct zapi_pw
*);
671 int kmpw_set(struct zapi_pw
*);
672 int kmpw_unset(struct zapi_pw
*);
675 uint8_t mask2prefixlen(in_addr_t
);
676 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
677 in_addr_t
prefixlen2mask(uint8_t);
678 struct in6_addr
*prefixlen2mask6(uint8_t);
679 void ldp_applymask(int, union ldpd_addr
*,
680 const union ldpd_addr
*, int);
681 int ldp_addrcmp(int, const union ldpd_addr
*,
682 const union ldpd_addr
*);
683 int ldp_addrisset(int, const union ldpd_addr
*);
684 int ldp_prefixcmp(int, const union ldpd_addr
*,
685 const union ldpd_addr
*, uint8_t);
686 int bad_addr_v4(struct in_addr
);
687 int bad_addr_v6(struct in6_addr
*);
688 int bad_addr(int, union ldpd_addr
*);
689 void embedscope(struct sockaddr_in6
*);
690 void recoverscope(struct sockaddr_in6
*);
691 void addscope(struct sockaddr_in6
*, uint32_t);
692 void clearscope(struct in6_addr
*);
693 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
694 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
696 socklen_t
sockaddr_len(struct sockaddr
*);
699 int ldp_write_handler(struct thread
*);
700 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
701 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
702 int main_imsg_compose_both(enum imsg_type
, void *,
704 void imsg_event_add(struct imsgev
*);
705 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
706 pid_t
, int, void *, uint16_t);
707 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
708 void evbuf_event_add(struct evbuf
*);
709 void evbuf_init(struct evbuf
*, int,
710 int (*)(struct thread
*), void *);
711 void evbuf_clear(struct evbuf
*);
712 int ldp_acl_request(struct imsgev
*, char *, int,
713 union ldpd_addr
*, uint8_t);
714 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
715 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
716 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
717 int ldp_is_dual_stack(struct ldpd_conf
*);
718 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
719 int ldp_config_apply(struct vty
*, struct ldpd_conf
*);
720 void ldp_clear_config(struct ldpd_conf
*);
721 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
722 struct ldpd_conf
*config_new_empty(void);
723 void config_clear(struct ldpd_conf
*);
726 /* NOTE: the parameters' names should be preserved because of codegen */
727 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
729 void iface_del_api(struct ldpd_conf
*conf
,
730 struct iface
*iface
);
731 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
732 union ldpd_addr
*addr
);
733 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
734 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
735 struct in_addr lsr_id
);
736 void nbrp_del_api(struct ldpd_conf
*conf
,
737 struct nbr_params
*nbrp
);
738 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
739 void l2vpn_del_api(struct ldpd_conf
*conf
,
740 struct l2vpn
*l2vpn
);
741 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
742 struct l2vpn
*l2vpn
, const char *ifname
);
743 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
744 struct l2vpn_if
*lif
);
745 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
746 struct l2vpn
*l2vpn
, const char *ifname
);
747 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
748 struct l2vpn_pw
*pw
);
751 int ldp_create_socket(int, enum socket_type
);
752 void sock_set_nonblock(int);
753 void sock_set_cloexec(int);
754 void sock_set_recvbuf(int);
755 int sock_set_reuse(int, int);
756 int sock_set_bindany(int, int);
757 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
758 int sock_set_ipv4_tos(int, int);
759 int sock_set_ipv4_pktinfo(int, int);
760 int sock_set_ipv4_recvdstaddr(int, int);
761 int sock_set_ipv4_recvif(int, int);
762 int sock_set_ipv4_minttl(int, int);
763 int sock_set_ipv4_ucast_ttl(int fd
, int);
764 int sock_set_ipv4_mcast_ttl(int, uint8_t);
765 int sock_set_ipv4_mcast(struct iface
*);
766 int sock_set_ipv4_mcast_loop(int);
767 int sock_set_ipv6_dscp(int, int);
768 int sock_set_ipv6_pktinfo(int, int);
769 int sock_set_ipv6_minhopcount(int, int);
770 int sock_set_ipv6_ucast_hops(int, int);
771 int sock_set_ipv6_mcast_hops(int, int);
772 int sock_set_ipv6_mcast(struct iface
*);
773 int sock_set_ipv6_mcast_loop(int);
781 const char *log_sockaddr(void *);
782 const char *log_in6addr(const struct in6_addr
*);
783 const char *log_in6addr_scope(const struct in6_addr
*, unsigned int);
784 const char *log_addr(int, const union ldpd_addr
*);
785 char *log_label(uint32_t);
786 const char *log_time(time_t);
787 char *log_hello_src(const struct hello_source
*);
788 const char *log_map(const struct map
*);
789 const char *log_fec(const struct fec
*);
790 const char *af_name(int);
791 const char *socket_name(int);
792 const char *nbr_state_name(int);
793 const char *if_state_name(int);
794 const char *if_type_name(enum iface_type
);
795 const char *msg_name(uint16_t);
796 const char *status_code_name(uint32_t);
797 const char *pw_type_name(uint16_t);
800 extern struct thread_master
*master
;
801 extern char ctl_sock_path
[MAXPATHLEN
];
804 void ldp_zebra_init(struct thread_master
*);
805 void ldp_zebra_destroy(void);
809 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
810 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
811 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
812 (IN6_IS_ADDR_MULTICAST(a) && \
813 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
816 #endif /* _LDPD_H_ */