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"
36 #define CONF_FILE "/etc/ldpd.conf"
37 #define LDPD_USER "_ldpd"
39 #define LDPD_FD_ASYNC 3
40 #define LDPD_FD_SYNC 4
42 #define LDPD_OPT_VERBOSE 0x00000001
43 #define LDPD_OPT_VERBOSE2 0x00000002
44 #define LDPD_OPT_NOACTION 0x00000004
46 #define TCP_MD5_KEY_LEN 80
47 #define L2VPN_NAME_LEN 32
49 #define RT_BUF_SIZE 16384
50 #define MAX_RTSOCK_BUF 128 * 1024
51 #define LDP_BACKLOG 128
53 #define F_LDPD_INSERTED 0x0001
54 #define F_CONNECTED 0x0002
55 #define F_STATIC 0x0004
56 #define F_DYNAMIC 0x0008
57 #define F_REJECT 0x0010
58 #define F_BLACKHOLE 0x0020
59 #define F_REDISTRIBUTED 0x0040
64 int (*handler
)(struct thread
*);
70 int (*handler_write
)(struct thread
*);
71 struct thread
*ev_write
;
72 int (*handler_read
)(struct thread
*);
73 struct thread
*ev_read
;
79 IMSG_CTL_SHOW_INTERFACE
,
80 IMSG_CTL_SHOW_DISCOVERY
,
81 IMSG_CTL_SHOW_DISCOVERY_DTL
,
82 IMSG_CTL_SHOW_DISC_IFACE
,
83 IMSG_CTL_SHOW_DISC_TNBR
,
84 IMSG_CTL_SHOW_DISC_ADJ
,
86 IMSG_CTL_SHOW_NBR_DISC
,
87 IMSG_CTL_SHOW_NBR_END
,
89 IMSG_CTL_SHOW_LIB_BEGIN
,
90 IMSG_CTL_SHOW_LIB_SENT
,
91 IMSG_CTL_SHOW_LIB_RCVD
,
92 IMSG_CTL_SHOW_LIB_END
,
93 IMSG_CTL_SHOW_L2VPN_PW
,
94 IMSG_CTL_SHOW_L2VPN_BINDING
,
97 IMSG_CTL_FIB_DECOUPLE
,
102 IMSG_CTL_LOG_VERBOSE
,
105 IMSG_KPWLABEL_CHANGE
,
106 IMSG_KPWLABEL_DELETE
,
112 IMSG_LABEL_MAPPING_FULL
,
118 IMSG_REQUEST_ADD_END
,
120 IMSG_MAPPING_ADD_END
,
122 IMSG_RELEASE_ADD_END
,
124 IMSG_WITHDRAW_ADD_END
,
128 IMSG_NOTIFICATION_SEND
,
136 IMSG_REQUEST_SOCKETS
,
143 IMSG_RECONF_L2VPN_IF
,
144 IMSG_RECONF_L2VPN_PW
,
145 IMSG_RECONF_L2VPN_IPW
,
150 IMSG_GET_LABEL_CHUNK
,
151 IMSG_RELEASE_LABEL_CHUNK
,
158 char ctl_sock_path
[MAXPATHLEN
];
159 char zclient_serv_path
[MAXPATHLEN
];
168 #define IN6_IS_SCOPE_EMBED(a) \
169 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
170 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
171 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
173 /* interface states */
174 #define IF_STA_DOWN 0x01
175 #define IF_STA_ACTIVE 0x02
177 /* targeted neighbor states */
178 #define TNBR_STA_DOWN 0x01
179 #define TNBR_STA_ACTIVE 0x02
181 /* interface types */
187 /* neighbor states */
188 #define NBR_STA_PRESENT 0x0001
189 #define NBR_STA_INITIAL 0x0002
190 #define NBR_STA_OPENREC 0x0004
191 #define NBR_STA_OPENSENT 0x0008
192 #define NBR_STA_OPER 0x0010
193 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
194 NBR_STA_OPENSENT | NBR_STA_OPER)
196 /* neighbor events */
201 NBR_EVT_CLOSE_SESSION
,
203 NBR_EVT_KEEPALIVE_RCVD
,
209 /* neighbor actions */
212 NBR_ACT_RST_KTIMEOUT
,
215 NBR_ACT_CONNECT_SETUP
,
216 NBR_ACT_PASSIVE_INIT
,
217 NBR_ACT_KEEPALIVE_SEND
,
218 NBR_ACT_CLOSE_SESSION
221 /* forward declarations */
222 RB_HEAD(global_adj_head
, adj
);
223 RB_HEAD(nbr_adj_head
, adj
);
224 RB_HEAD(ia_adj_head
, adj
);
232 union ldpd_addr prefix
;
250 uint32_t status_code
;
259 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
260 #define F_MAP_STATUS 0x02 /* status */
261 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
262 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
263 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
264 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
267 uint32_t status_code
;
268 uint32_t msg_id
; /* network byte order */
269 uint16_t msg_type
; /* network byte order */
279 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
280 #define F_NOTIF_FEC 0x02 /* fec tlv present */
281 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
284 LIST_ENTRY(if_addr
) entry
;
286 union ldpd_addr addr
;
288 union ldpd_addr dstbrd
;
290 LIST_HEAD(if_addr_head
, if_addr
);
297 struct ia_adj_head adj_tree
;
299 struct thread
*hello_timer
;
300 uint16_t hello_holdtime
;
301 uint16_t hello_interval
;
305 RB_ENTRY(iface
) entry
;
306 char name
[IF_NAMESIZE
];
307 unsigned int ifindex
;
308 struct if_addr_head addr_list
;
309 struct in6_addr linklocal
;
310 enum iface_type type
;
312 struct iface_af ipv4
;
313 struct iface_af ipv6
;
316 RB_HEAD(iface_head
, iface
);
317 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
318 DECLARE_QOBJ_TYPE(iface
)
320 /* source of targeted hellos */
322 RB_ENTRY(tnbr
) entry
;
323 struct thread
*hello_timer
;
326 union ldpd_addr addr
;
332 RB_HEAD(tnbr_head
, tnbr
);
333 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
334 DECLARE_QOBJ_TYPE(tnbr
)
335 #define F_TNBR_CONFIGURED 0x01
336 #define F_TNBR_DYNAMIC 0x02
343 /* neighbor specific parameters */
345 RB_ENTRY(nbr_params
) entry
;
346 struct in_addr lsr_id
;
351 enum auth_method method
;
352 char md5key
[TCP_MD5_KEY_LEN
];
358 RB_HEAD(nbrp_head
, nbr_params
);
359 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
360 DECLARE_QOBJ_TYPE(nbr_params
)
361 #define F_NBRP_KEEPALIVE 0x01
362 #define F_NBRP_GTSM 0x02
363 #define F_NBRP_GTSM_HOPS 0x04
366 uint32_t kalive_sent
;
367 uint32_t kalive_rcvd
;
370 uint32_t addrwdraw_sent
;
371 uint32_t addrwdraw_rcvd
;
374 uint32_t capability_sent
;
375 uint32_t capability_rcvd
;
376 uint32_t labelmap_sent
;
377 uint32_t labelmap_rcvd
;
378 uint32_t labelreq_sent
;
379 uint32_t labelreq_rcvd
;
380 uint32_t labelwdraw_sent
;
381 uint32_t labelwdraw_rcvd
;
382 uint32_t labelrel_sent
;
383 uint32_t labelrel_rcvd
;
384 uint32_t labelabreq_sent
;
385 uint32_t labelabreq_rcvd
;
389 RB_ENTRY(l2vpn_if
) entry
;
391 char ifname
[IF_NAMESIZE
];
392 unsigned int ifindex
;
394 uint8_t mac
[ETHER_ADDR_LEN
];
397 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
398 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
399 DECLARE_QOBJ_TYPE(l2vpn_if
)
402 RB_ENTRY(l2vpn_pw
) entry
;
404 struct in_addr lsr_id
;
406 union ldpd_addr addr
;
408 char ifname
[IF_NAMESIZE
];
409 unsigned int ifindex
;
410 uint32_t remote_group
;
412 uint32_t remote_status
;
416 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
417 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
418 DECLARE_QOBJ_TYPE(l2vpn_pw
)
419 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
420 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
421 #define F_PW_CWORD_CONF 0x04 /* control word configured */
422 #define F_PW_CWORD 0x08 /* control word negotiated */
423 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
424 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
427 RB_ENTRY(l2vpn
) entry
;
428 char name
[L2VPN_NAME_LEN
];
432 char br_ifname
[IF_NAMESIZE
];
433 unsigned int br_ifindex
;
434 struct l2vpn_if_head if_tree
;
435 struct l2vpn_pw_head pw_tree
;
436 struct l2vpn_pw_head pw_inactive_tree
;
439 RB_HEAD(l2vpn_head
, l2vpn
);
440 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
441 DECLARE_QOBJ_TYPE(l2vpn
)
442 #define L2VPN_TYPE_VPWS 1
443 #define L2VPN_TYPE_VPLS 2
452 static const char * const log_procnames
[] = {
469 struct ldpd_af_conf
{
471 uint16_t lhello_holdtime
;
472 uint16_t lhello_interval
;
473 uint16_t thello_holdtime
;
474 uint16_t thello_interval
;
475 union ldpd_addr trans_addr
;
476 char acl_thello_accept_from
[ACL_NAMSIZ
];
477 char acl_label_allocate_for
[ACL_NAMSIZ
];
478 char acl_label_advertise_to
[ACL_NAMSIZ
];
479 char acl_label_advertise_for
[ACL_NAMSIZ
];
480 char acl_label_expnull_for
[ACL_NAMSIZ
];
481 char acl_label_accept_from
[ACL_NAMSIZ
];
482 char acl_label_accept_for
[ACL_NAMSIZ
];
485 #define F_LDPD_AF_ENABLED 0x0001
486 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
487 #define F_LDPD_AF_EXPNULL 0x0004
488 #define F_LDPD_AF_NO_GTSM 0x0008
489 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
492 struct in_addr rtr_id
;
493 struct ldpd_af_conf ipv4
;
494 struct ldpd_af_conf ipv6
;
495 struct iface_head iface_tree
;
496 struct tnbr_head tnbr_tree
;
497 struct nbrp_head nbrp_tree
;
498 struct l2vpn_head l2vpn_tree
;
499 uint16_t lhello_holdtime
;
500 uint16_t lhello_interval
;
501 uint16_t thello_holdtime
;
502 uint16_t thello_interval
;
507 DECLARE_QOBJ_TYPE(ldpd_conf
)
508 #define F_LDPD_NO_FIB_UPDATE 0x0001
509 #define F_LDPD_DS_CISCO_INTEROP 0x0002
510 #define F_LDPD_ENABLED 0x0004
512 struct ldpd_af_global
{
513 struct thread
*disc_ev
;
514 struct thread
*edisc_ev
;
516 int ldp_edisc_socket
;
517 int ldp_session_socket
;
523 struct in_addr rtr_id
;
524 struct ldpd_af_global ipv4
;
525 struct ldpd_af_global ipv6
;
526 uint32_t conf_seqnum
;
528 struct if_addr_head addr_list
;
529 struct global_adj_head adj_tree
;
530 struct in_addr mcast_addr_v4
;
531 struct in6_addr mcast_addr_v6
;
532 TAILQ_HEAD(, pending_conn
) pending_conns
;
538 union ldpd_addr prefix
;
540 union ldpd_addr nexthop
;
541 uint32_t local_label
;
542 uint32_t remote_label
;
543 unsigned short ifindex
;
549 unsigned short ifindex
;
552 union ldpd_addr nexthop
;
553 uint32_t local_label
;
554 uint32_t remote_label
;
559 char ifname
[IF_NAMESIZE
];
560 unsigned short ifindex
;
562 union ldpd_addr addr
;
564 union ldpd_addr dstbrd
;
568 char ifname
[IF_NAMESIZE
];
569 unsigned short ifindex
;
572 uint8_t mac
[ETHER_ADDR_LEN
];
577 char acl
[ACL_NAMSIZ
];
579 union ldpd_addr addr
;
583 /* control data structures */
586 char name
[IF_NAMESIZE
];
587 unsigned int ifindex
;
589 enum iface_type type
;
590 uint16_t hello_holdtime
;
591 uint16_t hello_interval
;
597 char name
[IF_NAMESIZE
];
603 struct ctl_disc_tnbr
{
605 union ldpd_addr addr
;
612 enum hello_type type
;
613 char ifname
[IF_NAMESIZE
];
614 union ldpd_addr src_addr
;
616 uint16_t holdtime_remaining
;
617 union ldpd_addr trans_addr
;
624 union ldpd_addr laddr
;
626 union ldpd_addr raddr
;
628 enum auth_method auth_method
;
632 struct ldp_stats stats
;
638 union ldpd_addr prefix
;
640 struct in_addr nexthop
; /* lsr-id */
641 uint32_t local_label
;
642 uint32_t remote_label
;
650 char l2vpn_name
[L2VPN_NAME_LEN
];
651 char ifname
[IF_NAMESIZE
];
653 struct in_addr lsr_id
;
654 uint32_t local_label
;
656 uint16_t local_ifmtu
;
658 uint32_t remote_label
;
660 uint16_t remote_ifmtu
;
661 uint8_t remote_cword
;
665 extern struct ldpd_conf
*ldpd_conf
, *vty_conf
;
666 extern struct ldpd_global global
;
667 extern struct ldpd_init init
;
670 struct ldpd_conf
*parse_config(char *);
671 int cmdline_symset(char *);
674 void kif_redistribute(const char *);
675 int kr_change(struct kroute
*);
676 int kr_delete(struct kroute
*);
677 int kmpw_set(struct kpw
*);
678 int kmpw_unset(struct kpw
*);
681 uint8_t mask2prefixlen(in_addr_t
);
682 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
683 in_addr_t
prefixlen2mask(uint8_t);
684 struct in6_addr
*prefixlen2mask6(uint8_t);
685 void ldp_applymask(int, union ldpd_addr
*,
686 const union ldpd_addr
*, int);
687 int ldp_addrcmp(int, const union ldpd_addr
*,
688 const union ldpd_addr
*);
689 int ldp_addrisset(int, const union ldpd_addr
*);
690 int ldp_prefixcmp(int, const union ldpd_addr
*,
691 const union ldpd_addr
*, uint8_t);
692 int bad_addr_v4(struct in_addr
);
693 int bad_addr_v6(struct in6_addr
*);
694 int bad_addr(int, union ldpd_addr
*);
695 void embedscope(struct sockaddr_in6
*);
696 void recoverscope(struct sockaddr_in6
*);
697 void addscope(struct sockaddr_in6
*, uint32_t);
698 void clearscope(struct in6_addr
*);
699 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
700 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
702 socklen_t
sockaddr_len(struct sockaddr
*);
705 int ldp_write_handler(struct thread
*);
706 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
707 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
708 int main_imsg_compose_both(enum imsg_type
, void *,
710 void imsg_event_add(struct imsgev
*);
711 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
712 pid_t
, int, void *, uint16_t);
713 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
714 void evbuf_event_add(struct evbuf
*);
715 void evbuf_init(struct evbuf
*, int,
716 int (*)(struct thread
*), void *);
717 void evbuf_clear(struct evbuf
*);
718 int ldp_acl_request(struct imsgev
*, char *, int,
719 union ldpd_addr
*, uint8_t);
720 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
721 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
722 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
723 int ldp_is_dual_stack(struct ldpd_conf
*);
724 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
725 int ldp_reload(struct ldpd_conf
*);
726 void ldp_clear_config(struct ldpd_conf
*);
727 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
728 struct ldpd_conf
*config_new_empty(void);
729 void config_clear(struct ldpd_conf
*);
732 /* NOTE: the parameters' names should be preserved because of codegen */
733 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
735 void iface_del_api(struct ldpd_conf
*conf
,
736 struct iface
*iface
);
737 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
738 union ldpd_addr
*addr
);
739 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
740 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
741 struct in_addr lsr_id
);
742 void nbrp_del_api(struct ldpd_conf
*conf
,
743 struct nbr_params
*nbrp
);
744 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
745 void l2vpn_del_api(struct ldpd_conf
*conf
,
746 struct l2vpn
*l2vpn
);
747 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
748 struct l2vpn
*l2vpn
, const char *ifname
);
749 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
750 struct l2vpn_if
*lif
);
751 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
752 struct l2vpn
*l2vpn
, const char *ifname
);
753 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
754 struct l2vpn_pw
*pw
);
757 int ldp_create_socket(int, enum socket_type
);
758 void sock_set_nonblock(int);
759 void sock_set_cloexec(int);
760 void sock_set_recvbuf(int);
761 int sock_set_reuse(int, int);
762 int sock_set_bindany(int, int);
763 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
764 int sock_set_ipv4_tos(int, int);
765 int sock_set_ipv4_pktinfo(int, int);
766 int sock_set_ipv4_recvdstaddr(int, int);
767 int sock_set_ipv4_recvif(int, int);
768 int sock_set_ipv4_minttl(int, int);
769 int sock_set_ipv4_ucast_ttl(int fd
, int);
770 int sock_set_ipv4_mcast_ttl(int, uint8_t);
771 int sock_set_ipv4_mcast(struct iface
*);
772 int sock_set_ipv4_mcast_loop(int);
773 int sock_set_ipv6_dscp(int, int);
774 int sock_set_ipv6_pktinfo(int, int);
775 int sock_set_ipv6_minhopcount(int, int);
776 int sock_set_ipv6_ucast_hops(int, int);
777 int sock_set_ipv6_mcast_hops(int, int);
778 int sock_set_ipv6_mcast(struct iface
*);
779 int sock_set_ipv6_mcast_loop(int);
787 const char *log_sockaddr(void *);
788 const char *log_in6addr(const struct in6_addr
*);
789 const char *log_in6addr_scope(const struct in6_addr
*, unsigned int);
790 const char *log_addr(int, const union ldpd_addr
*);
791 char *log_label(uint32_t);
792 const char *log_time(time_t);
793 char *log_hello_src(const struct hello_source
*);
794 const char *log_map(const struct map
*);
795 const char *log_fec(const struct fec
*);
796 const char *af_name(int);
797 const char *socket_name(int);
798 const char *nbr_state_name(int);
799 const char *if_state_name(int);
800 const char *if_type_name(enum iface_type
);
801 const char *msg_name(uint16_t);
802 const char *status_code_name(uint32_t);
803 const char *pw_type_name(uint16_t);
806 extern struct thread_master
*master
;
807 extern char ctl_sock_path
[MAXPATHLEN
];
810 void ldp_zebra_init(struct thread_master
*);
811 void ldp_zebra_destroy(void);
815 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
816 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
817 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
818 (IN6_IS_ADDR_MULTICAST(a) && \
819 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
822 #endif /* _LDPD_H_ */