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
,
156 char ctl_sock_path
[MAXPATHLEN
];
157 char zclient_serv_path
[MAXPATHLEN
];
166 #define IN6_IS_SCOPE_EMBED(a) \
167 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
168 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
169 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
171 /* interface states */
172 #define IF_STA_DOWN 0x01
173 #define IF_STA_ACTIVE 0x02
175 /* targeted neighbor states */
176 #define TNBR_STA_DOWN 0x01
177 #define TNBR_STA_ACTIVE 0x02
179 /* interface types */
185 /* neighbor states */
186 #define NBR_STA_PRESENT 0x0001
187 #define NBR_STA_INITIAL 0x0002
188 #define NBR_STA_OPENREC 0x0004
189 #define NBR_STA_OPENSENT 0x0008
190 #define NBR_STA_OPER 0x0010
191 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
192 NBR_STA_OPENSENT | NBR_STA_OPER)
194 /* neighbor events */
199 NBR_EVT_CLOSE_SESSION
,
201 NBR_EVT_KEEPALIVE_RCVD
,
207 /* neighbor actions */
210 NBR_ACT_RST_KTIMEOUT
,
213 NBR_ACT_CONNECT_SETUP
,
214 NBR_ACT_PASSIVE_INIT
,
215 NBR_ACT_KEEPALIVE_SEND
,
216 NBR_ACT_CLOSE_SESSION
219 /* forward declarations */
220 RB_HEAD(global_adj_head
, adj
);
221 RB_HEAD(nbr_adj_head
, adj
);
222 RB_HEAD(ia_adj_head
, adj
);
230 union ldpd_addr prefix
;
248 uint32_t status_code
;
257 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
258 #define F_MAP_STATUS 0x02 /* status */
259 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
260 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
261 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
262 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
265 uint32_t status_code
;
266 uint32_t msg_id
; /* network byte order */
267 uint16_t msg_type
; /* network byte order */
277 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
278 #define F_NOTIF_FEC 0x02 /* fec tlv present */
279 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
282 LIST_ENTRY(if_addr
) entry
;
284 union ldpd_addr addr
;
286 union ldpd_addr dstbrd
;
288 LIST_HEAD(if_addr_head
, if_addr
);
295 struct ia_adj_head adj_tree
;
297 struct thread
*hello_timer
;
298 uint16_t hello_holdtime
;
299 uint16_t hello_interval
;
303 RB_ENTRY(iface
) entry
;
304 char name
[IF_NAMESIZE
];
305 unsigned int ifindex
;
306 struct if_addr_head addr_list
;
307 struct in6_addr linklocal
;
308 enum iface_type type
;
310 struct iface_af ipv4
;
311 struct iface_af ipv6
;
314 RB_HEAD(iface_head
, iface
);
315 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
316 DECLARE_QOBJ_TYPE(iface
)
318 /* source of targeted hellos */
320 RB_ENTRY(tnbr
) entry
;
321 struct thread
*hello_timer
;
324 union ldpd_addr addr
;
330 RB_HEAD(tnbr_head
, tnbr
);
331 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
332 DECLARE_QOBJ_TYPE(tnbr
)
333 #define F_TNBR_CONFIGURED 0x01
334 #define F_TNBR_DYNAMIC 0x02
341 /* neighbor specific parameters */
343 RB_ENTRY(nbr_params
) entry
;
344 struct in_addr lsr_id
;
349 enum auth_method method
;
350 char md5key
[TCP_MD5_KEY_LEN
];
356 RB_HEAD(nbrp_head
, nbr_params
);
357 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
358 DECLARE_QOBJ_TYPE(nbr_params
)
359 #define F_NBRP_KEEPALIVE 0x01
360 #define F_NBRP_GTSM 0x02
361 #define F_NBRP_GTSM_HOPS 0x04
364 uint32_t kalive_sent
;
365 uint32_t kalive_rcvd
;
368 uint32_t addrwdraw_sent
;
369 uint32_t addrwdraw_rcvd
;
372 uint32_t capability_sent
;
373 uint32_t capability_rcvd
;
374 uint32_t labelmap_sent
;
375 uint32_t labelmap_rcvd
;
376 uint32_t labelreq_sent
;
377 uint32_t labelreq_rcvd
;
378 uint32_t labelwdraw_sent
;
379 uint32_t labelwdraw_rcvd
;
380 uint32_t labelrel_sent
;
381 uint32_t labelrel_rcvd
;
382 uint32_t labelabreq_sent
;
383 uint32_t labelabreq_rcvd
;
387 RB_ENTRY(l2vpn_if
) entry
;
389 char ifname
[IF_NAMESIZE
];
390 unsigned int ifindex
;
392 uint8_t mac
[ETH_ALEN
];
395 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
396 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
397 DECLARE_QOBJ_TYPE(l2vpn_if
)
400 RB_ENTRY(l2vpn_pw
) entry
;
402 struct in_addr lsr_id
;
404 union ldpd_addr addr
;
406 char ifname
[IF_NAMESIZE
];
407 unsigned int ifindex
;
408 uint32_t remote_group
;
410 uint32_t remote_status
;
414 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
415 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
416 DECLARE_QOBJ_TYPE(l2vpn_pw
)
417 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
418 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
419 #define F_PW_CWORD_CONF 0x04 /* control word configured */
420 #define F_PW_CWORD 0x08 /* control word negotiated */
421 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
422 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
425 RB_ENTRY(l2vpn
) entry
;
426 char name
[L2VPN_NAME_LEN
];
430 char br_ifname
[IF_NAMESIZE
];
431 unsigned int br_ifindex
;
432 struct l2vpn_if_head if_tree
;
433 struct l2vpn_pw_head pw_tree
;
434 struct l2vpn_pw_head pw_inactive_tree
;
437 RB_HEAD(l2vpn_head
, l2vpn
);
438 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
439 DECLARE_QOBJ_TYPE(l2vpn
)
440 #define L2VPN_TYPE_VPWS 1
441 #define L2VPN_TYPE_VPLS 2
450 static const char * const log_procnames
[] = {
467 struct ldpd_af_conf
{
469 uint16_t lhello_holdtime
;
470 uint16_t lhello_interval
;
471 uint16_t thello_holdtime
;
472 uint16_t thello_interval
;
473 union ldpd_addr trans_addr
;
474 char acl_thello_accept_from
[ACL_NAMSIZ
];
475 char acl_label_allocate_for
[ACL_NAMSIZ
];
476 char acl_label_advertise_to
[ACL_NAMSIZ
];
477 char acl_label_advertise_for
[ACL_NAMSIZ
];
478 char acl_label_expnull_for
[ACL_NAMSIZ
];
479 char acl_label_accept_from
[ACL_NAMSIZ
];
480 char acl_label_accept_for
[ACL_NAMSIZ
];
483 #define F_LDPD_AF_ENABLED 0x0001
484 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
485 #define F_LDPD_AF_EXPNULL 0x0004
486 #define F_LDPD_AF_NO_GTSM 0x0008
487 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
490 struct in_addr rtr_id
;
491 struct ldpd_af_conf ipv4
;
492 struct ldpd_af_conf ipv6
;
493 struct iface_head iface_tree
;
494 struct tnbr_head tnbr_tree
;
495 struct nbrp_head nbrp_tree
;
496 struct l2vpn_head l2vpn_tree
;
497 uint16_t lhello_holdtime
;
498 uint16_t lhello_interval
;
499 uint16_t thello_holdtime
;
500 uint16_t thello_interval
;
505 DECLARE_QOBJ_TYPE(ldpd_conf
)
506 #define F_LDPD_NO_FIB_UPDATE 0x0001
507 #define F_LDPD_DS_CISCO_INTEROP 0x0002
508 #define F_LDPD_ENABLED 0x0004
510 struct ldpd_af_global
{
511 struct thread
*disc_ev
;
512 struct thread
*edisc_ev
;
514 int ldp_edisc_socket
;
515 int ldp_session_socket
;
520 struct in_addr rtr_id
;
521 struct ldpd_af_global ipv4
;
522 struct ldpd_af_global ipv6
;
523 uint32_t conf_seqnum
;
525 struct if_addr_head addr_list
;
526 struct global_adj_head adj_tree
;
527 struct in_addr mcast_addr_v4
;
528 struct in6_addr mcast_addr_v6
;
529 TAILQ_HEAD(, pending_conn
) pending_conns
;
535 union ldpd_addr prefix
;
537 union ldpd_addr nexthop
;
538 uint32_t local_label
;
539 uint32_t remote_label
;
540 unsigned short ifindex
;
546 unsigned short ifindex
;
549 union ldpd_addr nexthop
;
550 uint32_t local_label
;
551 uint32_t remote_label
;
556 char ifname
[IF_NAMESIZE
];
557 unsigned short ifindex
;
559 union ldpd_addr addr
;
561 union ldpd_addr dstbrd
;
565 char ifname
[IF_NAMESIZE
];
566 unsigned short ifindex
;
569 uint8_t mac
[ETH_ALEN
];
574 char acl
[ACL_NAMSIZ
];
576 union ldpd_addr addr
;
580 /* control data structures */
583 char name
[IF_NAMESIZE
];
584 unsigned int ifindex
;
586 enum iface_type type
;
587 uint16_t hello_holdtime
;
588 uint16_t hello_interval
;
594 char name
[IF_NAMESIZE
];
600 struct ctl_disc_tnbr
{
602 union ldpd_addr addr
;
609 enum hello_type type
;
610 char ifname
[IF_NAMESIZE
];
611 union ldpd_addr src_addr
;
613 uint16_t holdtime_remaining
;
614 union ldpd_addr trans_addr
;
621 union ldpd_addr laddr
;
623 union ldpd_addr raddr
;
625 enum auth_method auth_method
;
629 struct ldp_stats stats
;
635 union ldpd_addr prefix
;
637 struct in_addr nexthop
; /* lsr-id */
638 uint32_t local_label
;
639 uint32_t remote_label
;
647 char l2vpn_name
[L2VPN_NAME_LEN
];
648 char ifname
[IF_NAMESIZE
];
650 struct in_addr lsr_id
;
651 uint32_t local_label
;
653 uint16_t local_ifmtu
;
655 uint32_t remote_label
;
657 uint16_t remote_ifmtu
;
658 uint8_t remote_cword
;
662 extern struct ldpd_conf
*ldpd_conf
, *vty_conf
;
663 extern struct ldpd_global global
;
664 extern struct ldpd_init init
;
667 struct ldpd_conf
*parse_config(char *);
668 int cmdline_symset(char *);
671 void kif_redistribute(const char *);
672 int kr_change(struct kroute
*);
673 int kr_delete(struct kroute
*);
674 int kmpw_set(struct kpw
*);
675 int kmpw_unset(struct kpw
*);
678 uint8_t mask2prefixlen(in_addr_t
);
679 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
680 in_addr_t
prefixlen2mask(uint8_t);
681 struct in6_addr
*prefixlen2mask6(uint8_t);
682 void ldp_applymask(int, union ldpd_addr
*,
683 const union ldpd_addr
*, int);
684 int ldp_addrcmp(int, const union ldpd_addr
*,
685 const union ldpd_addr
*);
686 int ldp_addrisset(int, const union ldpd_addr
*);
687 int ldp_prefixcmp(int, const union ldpd_addr
*,
688 const union ldpd_addr
*, uint8_t);
689 int bad_addr_v4(struct in_addr
);
690 int bad_addr_v6(struct in6_addr
*);
691 int bad_addr(int, union ldpd_addr
*);
692 void embedscope(struct sockaddr_in6
*);
693 void recoverscope(struct sockaddr_in6
*);
694 void addscope(struct sockaddr_in6
*, uint32_t);
695 void clearscope(struct in6_addr
*);
696 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
697 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
699 socklen_t
sockaddr_len(struct sockaddr
*);
702 int ldp_write_handler(struct thread
*);
703 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
704 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
705 int main_imsg_compose_both(enum imsg_type
, void *,
707 void imsg_event_add(struct imsgev
*);
708 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
709 pid_t
, int, void *, uint16_t);
710 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
711 void evbuf_event_add(struct evbuf
*);
712 void evbuf_init(struct evbuf
*, int,
713 int (*)(struct thread
*), void *);
714 void evbuf_clear(struct evbuf
*);
715 int ldp_acl_request(struct imsgev
*, char *, int,
716 union ldpd_addr
*, uint8_t);
717 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
718 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
719 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
720 int ldp_is_dual_stack(struct ldpd_conf
*);
721 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
722 int ldp_config_apply(struct vty
*, struct ldpd_conf
*);
723 void ldp_clear_config(struct ldpd_conf
*);
724 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
725 struct ldpd_conf
*config_new_empty(void);
726 void config_clear(struct ldpd_conf
*);
729 /* NOTE: the parameters' names should be preserved because of codegen */
730 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
732 void iface_del_api(struct ldpd_conf
*conf
,
733 struct iface
*iface
);
734 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
735 union ldpd_addr
*addr
);
736 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
737 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
738 struct in_addr lsr_id
);
739 void nbrp_del_api(struct ldpd_conf
*conf
,
740 struct nbr_params
*nbrp
);
741 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
742 void l2vpn_del_api(struct ldpd_conf
*conf
,
743 struct l2vpn
*l2vpn
);
744 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
745 struct l2vpn
*l2vpn
, const char *ifname
);
746 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
747 struct l2vpn_if
*lif
);
748 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
749 struct l2vpn
*l2vpn
, const char *ifname
);
750 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
751 struct l2vpn_pw
*pw
);
754 int ldp_create_socket(int, enum socket_type
);
755 void sock_set_nonblock(int);
756 void sock_set_cloexec(int);
757 void sock_set_recvbuf(int);
758 int sock_set_reuse(int, int);
759 int sock_set_bindany(int, int);
760 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
761 int sock_set_ipv4_tos(int, int);
762 int sock_set_ipv4_pktinfo(int, int);
763 int sock_set_ipv4_recvdstaddr(int, int);
764 int sock_set_ipv4_recvif(int, int);
765 int sock_set_ipv4_minttl(int, int);
766 int sock_set_ipv4_ucast_ttl(int fd
, int);
767 int sock_set_ipv4_mcast_ttl(int, uint8_t);
768 int sock_set_ipv4_mcast(struct iface
*);
769 int sock_set_ipv4_mcast_loop(int);
770 int sock_set_ipv6_dscp(int, int);
771 int sock_set_ipv6_pktinfo(int, int);
772 int sock_set_ipv6_minhopcount(int, int);
773 int sock_set_ipv6_ucast_hops(int, int);
774 int sock_set_ipv6_mcast_hops(int, int);
775 int sock_set_ipv6_mcast(struct iface
*);
776 int sock_set_ipv6_mcast_loop(int);
784 const char *log_sockaddr(void *);
785 const char *log_in6addr(const struct in6_addr
*);
786 const char *log_in6addr_scope(const struct in6_addr
*, unsigned int);
787 const char *log_addr(int, const union ldpd_addr
*);
788 char *log_label(uint32_t);
789 const char *log_time(time_t);
790 char *log_hello_src(const struct hello_source
*);
791 const char *log_map(const struct map
*);
792 const char *log_fec(const struct fec
*);
793 const char *af_name(int);
794 const char *socket_name(int);
795 const char *nbr_state_name(int);
796 const char *if_state_name(int);
797 const char *if_type_name(enum iface_type
);
798 const char *msg_name(uint16_t);
799 const char *status_code_name(uint32_t);
800 const char *pw_type_name(uint16_t);
803 extern struct thread_master
*master
;
804 extern char ctl_sock_path
[MAXPATHLEN
];
807 void ldp_zebra_init(struct thread_master
*);
808 void ldp_zebra_destroy(void);
812 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
813 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
814 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
815 (IN6_IS_ADDR_MULTICAST(a) && \
816 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
819 #endif /* _LDPD_H_ */