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"
35 #define CONF_FILE "/etc/ldpd.conf"
36 #define LDPD_USER "_ldpd"
38 #define LDPD_FD_ASYNC 3
39 #define LDPD_FD_SYNC 4
41 #define LDPD_OPT_VERBOSE 0x00000001
42 #define LDPD_OPT_VERBOSE2 0x00000002
43 #define LDPD_OPT_NOACTION 0x00000004
45 #define TCP_MD5_KEY_LEN 80
46 #define L2VPN_NAME_LEN 32
48 #define RT_BUF_SIZE 16384
49 #define MAX_RTSOCK_BUF 128 * 1024
50 #define LDP_BACKLOG 128
52 #define F_LDPD_INSERTED 0x0001
53 #define F_CONNECTED 0x0002
54 #define F_STATIC 0x0004
55 #define F_DYNAMIC 0x0008
56 #define F_REJECT 0x0010
57 #define F_BLACKHOLE 0x0020
58 #define F_REDISTRIBUTED 0x0040
63 int (*handler
)(struct thread
*);
69 int (*handler_write
)(struct thread
*);
70 struct thread
*ev_write
;
71 int (*handler_read
)(struct thread
*);
72 struct thread
*ev_read
;
78 IMSG_CTL_SHOW_INTERFACE
,
79 IMSG_CTL_SHOW_DISCOVERY
,
80 IMSG_CTL_SHOW_DISCOVERY_DTL
,
81 IMSG_CTL_SHOW_DISC_IFACE
,
82 IMSG_CTL_SHOW_DISC_TNBR
,
83 IMSG_CTL_SHOW_DISC_ADJ
,
85 IMSG_CTL_SHOW_NBR_DISC
,
86 IMSG_CTL_SHOW_NBR_END
,
88 IMSG_CTL_SHOW_LIB_BEGIN
,
89 IMSG_CTL_SHOW_LIB_SENT
,
90 IMSG_CTL_SHOW_LIB_RCVD
,
91 IMSG_CTL_SHOW_LIB_END
,
92 IMSG_CTL_SHOW_L2VPN_PW
,
93 IMSG_CTL_SHOW_L2VPN_BINDING
,
96 IMSG_CTL_FIB_DECOUPLE
,
101 IMSG_CTL_LOG_VERBOSE
,
104 IMSG_KPWLABEL_CHANGE
,
105 IMSG_KPWLABEL_DELETE
,
111 IMSG_LABEL_MAPPING_FULL
,
117 IMSG_REQUEST_ADD_END
,
119 IMSG_MAPPING_ADD_END
,
121 IMSG_RELEASE_ADD_END
,
123 IMSG_WITHDRAW_ADD_END
,
127 IMSG_NOTIFICATION_SEND
,
135 IMSG_REQUEST_SOCKETS
,
142 IMSG_RECONF_L2VPN_IF
,
143 IMSG_RECONF_L2VPN_PW
,
144 IMSG_RECONF_L2VPN_IPW
,
149 IMSG_GET_LABEL_CHUNK
,
150 IMSG_RELEASE_LABEL_CHUNK
158 #define IN6_IS_SCOPE_EMBED(a) \
159 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
160 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
161 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
163 /* interface states */
164 #define IF_STA_DOWN 0x01
165 #define IF_STA_ACTIVE 0x02
167 /* targeted neighbor states */
168 #define TNBR_STA_DOWN 0x01
169 #define TNBR_STA_ACTIVE 0x02
171 /* interface types */
177 /* neighbor states */
178 #define NBR_STA_PRESENT 0x0001
179 #define NBR_STA_INITIAL 0x0002
180 #define NBR_STA_OPENREC 0x0004
181 #define NBR_STA_OPENSENT 0x0008
182 #define NBR_STA_OPER 0x0010
183 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
184 NBR_STA_OPENSENT | NBR_STA_OPER)
186 /* neighbor events */
191 NBR_EVT_CLOSE_SESSION
,
193 NBR_EVT_KEEPALIVE_RCVD
,
199 /* neighbor actions */
202 NBR_ACT_RST_KTIMEOUT
,
205 NBR_ACT_CONNECT_SETUP
,
206 NBR_ACT_PASSIVE_INIT
,
207 NBR_ACT_KEEPALIVE_SEND
,
208 NBR_ACT_CLOSE_SESSION
211 /* forward declarations */
212 RB_HEAD(global_adj_head
, adj
);
213 RB_HEAD(nbr_adj_head
, adj
);
214 RB_HEAD(ia_adj_head
, adj
);
222 union ldpd_addr prefix
;
240 uint32_t status_code
;
249 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
250 #define F_MAP_STATUS 0x02 /* status */
251 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
252 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
253 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
254 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
257 uint32_t status_code
;
258 uint32_t msg_id
; /* network byte order */
259 uint16_t msg_type
; /* network byte order */
269 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
270 #define F_NOTIF_FEC 0x02 /* fec tlv present */
271 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
274 LIST_ENTRY(if_addr
) entry
;
276 union ldpd_addr addr
;
278 union ldpd_addr dstbrd
;
280 LIST_HEAD(if_addr_head
, if_addr
);
287 struct ia_adj_head adj_tree
;
289 struct thread
*hello_timer
;
290 uint16_t hello_holdtime
;
291 uint16_t hello_interval
;
295 RB_ENTRY(iface
) entry
;
296 char name
[IF_NAMESIZE
];
297 unsigned int ifindex
;
298 struct if_addr_head addr_list
;
299 struct in6_addr linklocal
;
300 enum iface_type type
;
302 struct iface_af ipv4
;
303 struct iface_af ipv6
;
306 RB_HEAD(iface_head
, iface
);
307 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
308 DECLARE_QOBJ_TYPE(iface
)
310 /* source of targeted hellos */
312 RB_ENTRY(tnbr
) entry
;
313 struct thread
*hello_timer
;
316 union ldpd_addr addr
;
322 RB_HEAD(tnbr_head
, tnbr
);
323 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
324 DECLARE_QOBJ_TYPE(tnbr
)
325 #define F_TNBR_CONFIGURED 0x01
326 #define F_TNBR_DYNAMIC 0x02
333 /* neighbor specific parameters */
335 RB_ENTRY(nbr_params
) entry
;
336 struct in_addr lsr_id
;
341 enum auth_method method
;
342 char md5key
[TCP_MD5_KEY_LEN
];
348 RB_HEAD(nbrp_head
, nbr_params
);
349 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
350 DECLARE_QOBJ_TYPE(nbr_params
)
351 #define F_NBRP_KEEPALIVE 0x01
352 #define F_NBRP_GTSM 0x02
353 #define F_NBRP_GTSM_HOPS 0x04
356 uint32_t kalive_sent
;
357 uint32_t kalive_rcvd
;
360 uint32_t addrwdraw_sent
;
361 uint32_t addrwdraw_rcvd
;
364 uint32_t capability_sent
;
365 uint32_t capability_rcvd
;
366 uint32_t labelmap_sent
;
367 uint32_t labelmap_rcvd
;
368 uint32_t labelreq_sent
;
369 uint32_t labelreq_rcvd
;
370 uint32_t labelwdraw_sent
;
371 uint32_t labelwdraw_rcvd
;
372 uint32_t labelrel_sent
;
373 uint32_t labelrel_rcvd
;
374 uint32_t labelabreq_sent
;
375 uint32_t labelabreq_rcvd
;
379 RB_ENTRY(l2vpn_if
) entry
;
381 char ifname
[IF_NAMESIZE
];
382 unsigned int ifindex
;
384 uint8_t mac
[ETHER_ADDR_LEN
];
387 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
388 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
389 DECLARE_QOBJ_TYPE(l2vpn_if
)
392 RB_ENTRY(l2vpn_pw
) entry
;
394 struct in_addr lsr_id
;
396 union ldpd_addr addr
;
398 char ifname
[IF_NAMESIZE
];
399 unsigned int ifindex
;
400 uint32_t remote_group
;
402 uint32_t remote_status
;
406 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
407 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
408 DECLARE_QOBJ_TYPE(l2vpn_pw
)
409 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
410 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
411 #define F_PW_CWORD_CONF 0x04 /* control word configured */
412 #define F_PW_CWORD 0x08 /* control word negotiated */
413 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
414 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
417 RB_ENTRY(l2vpn
) entry
;
418 char name
[L2VPN_NAME_LEN
];
422 char br_ifname
[IF_NAMESIZE
];
423 unsigned int br_ifindex
;
424 struct l2vpn_if_head if_tree
;
425 struct l2vpn_pw_head pw_tree
;
426 struct l2vpn_pw_head pw_inactive_tree
;
429 RB_HEAD(l2vpn_head
, l2vpn
);
430 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
431 DECLARE_QOBJ_TYPE(l2vpn
)
432 #define L2VPN_TYPE_VPWS 1
433 #define L2VPN_TYPE_VPLS 2
453 struct ldpd_af_conf
{
455 uint16_t lhello_holdtime
;
456 uint16_t lhello_interval
;
457 uint16_t thello_holdtime
;
458 uint16_t thello_interval
;
459 union ldpd_addr trans_addr
;
460 char acl_thello_accept_from
[ACL_NAMSIZ
];
461 char acl_label_allocate_for
[ACL_NAMSIZ
];
462 char acl_label_advertise_to
[ACL_NAMSIZ
];
463 char acl_label_advertise_for
[ACL_NAMSIZ
];
464 char acl_label_expnull_for
[ACL_NAMSIZ
];
465 char acl_label_accept_from
[ACL_NAMSIZ
];
466 char acl_label_accept_for
[ACL_NAMSIZ
];
469 #define F_LDPD_AF_ENABLED 0x0001
470 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
471 #define F_LDPD_AF_EXPNULL 0x0004
472 #define F_LDPD_AF_NO_GTSM 0x0008
473 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
476 struct in_addr rtr_id
;
477 struct ldpd_af_conf ipv4
;
478 struct ldpd_af_conf ipv6
;
479 struct iface_head iface_tree
;
480 struct tnbr_head tnbr_tree
;
481 struct nbrp_head nbrp_tree
;
482 struct l2vpn_head l2vpn_tree
;
483 uint16_t lhello_holdtime
;
484 uint16_t lhello_interval
;
485 uint16_t thello_holdtime
;
486 uint16_t thello_interval
;
491 DECLARE_QOBJ_TYPE(ldpd_conf
)
492 #define F_LDPD_NO_FIB_UPDATE 0x0001
493 #define F_LDPD_DS_CISCO_INTEROP 0x0002
494 #define F_LDPD_ENABLED 0x0004
496 struct ldpd_af_global
{
497 struct thread
*disc_ev
;
498 struct thread
*edisc_ev
;
500 int ldp_edisc_socket
;
501 int ldp_session_socket
;
508 struct in_addr rtr_id
;
509 struct ldpd_af_global ipv4
;
510 struct ldpd_af_global ipv6
;
511 uint32_t conf_seqnum
;
513 struct if_addr_head addr_list
;
514 struct global_adj_head adj_tree
;
515 struct in_addr mcast_addr_v4
;
516 struct in6_addr mcast_addr_v6
;
517 TAILQ_HEAD(, pending_conn
) pending_conns
;
523 union ldpd_addr prefix
;
525 union ldpd_addr nexthop
;
526 uint32_t local_label
;
527 uint32_t remote_label
;
528 unsigned short ifindex
;
534 unsigned short ifindex
;
537 union ldpd_addr nexthop
;
538 uint32_t local_label
;
539 uint32_t remote_label
;
544 unsigned short ifindex
;
546 union ldpd_addr addr
;
548 union ldpd_addr dstbrd
;
552 char ifname
[IF_NAMESIZE
];
553 unsigned short ifindex
;
555 uint8_t mac
[ETHER_ADDR_LEN
];
560 char acl
[ACL_NAMSIZ
];
562 union ldpd_addr addr
;
566 /* control data structures */
569 char name
[IF_NAMESIZE
];
570 unsigned int ifindex
;
573 enum iface_type type
;
574 uint16_t hello_holdtime
;
575 uint16_t hello_interval
;
581 char name
[IF_NAMESIZE
];
587 struct ctl_disc_tnbr
{
589 union ldpd_addr addr
;
596 enum hello_type type
;
597 char ifname
[IF_NAMESIZE
];
598 union ldpd_addr src_addr
;
600 uint16_t holdtime_remaining
;
601 union ldpd_addr trans_addr
;
608 union ldpd_addr laddr
;
610 union ldpd_addr raddr
;
612 enum auth_method auth_method
;
616 struct ldp_stats stats
;
622 union ldpd_addr prefix
;
624 struct in_addr nexthop
; /* lsr-id */
625 uint32_t local_label
;
626 uint32_t remote_label
;
634 char l2vpn_name
[L2VPN_NAME_LEN
];
635 char ifname
[IF_NAMESIZE
];
637 struct in_addr lsr_id
;
638 uint32_t local_label
;
640 uint16_t local_ifmtu
;
642 uint32_t remote_label
;
644 uint16_t remote_ifmtu
;
645 uint8_t remote_cword
;
649 extern struct ldpd_conf
*ldpd_conf
, *vty_conf
;
650 extern struct ldpd_global global
;
653 struct ldpd_conf
*parse_config(char *);
654 int cmdline_symset(char *);
657 void kif_redistribute(const char *);
658 int kr_change(struct kroute
*);
659 int kr_delete(struct kroute
*);
660 int kmpw_set(struct kpw
*);
661 int kmpw_unset(struct kpw
*);
664 uint8_t mask2prefixlen(in_addr_t
);
665 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
666 in_addr_t
prefixlen2mask(uint8_t);
667 struct in6_addr
*prefixlen2mask6(uint8_t);
668 void ldp_applymask(int, union ldpd_addr
*,
669 const union ldpd_addr
*, int);
670 int ldp_addrcmp(int, const union ldpd_addr
*,
671 const union ldpd_addr
*);
672 int ldp_addrisset(int, const union ldpd_addr
*);
673 int ldp_prefixcmp(int, const union ldpd_addr
*,
674 const union ldpd_addr
*, uint8_t);
675 int bad_addr_v4(struct in_addr
);
676 int bad_addr_v6(struct in6_addr
*);
677 int bad_addr(int, union ldpd_addr
*);
678 void embedscope(struct sockaddr_in6
*);
679 void recoverscope(struct sockaddr_in6
*);
680 void addscope(struct sockaddr_in6
*, uint32_t);
681 void clearscope(struct in6_addr
*);
682 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
683 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
685 socklen_t
sockaddr_len(struct sockaddr
*);
688 int ldp_write_handler(struct thread
*);
689 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
690 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
691 int main_imsg_compose_both(enum imsg_type
, void *,
693 void imsg_event_add(struct imsgev
*);
694 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
695 pid_t
, int, void *, uint16_t);
696 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
697 void evbuf_event_add(struct evbuf
*);
698 void evbuf_init(struct evbuf
*, int,
699 int (*)(struct thread
*), void *);
700 void evbuf_clear(struct evbuf
*);
701 int ldp_acl_request(struct imsgev
*, char *, int,
702 union ldpd_addr
*, uint8_t);
703 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
704 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
705 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
706 int ldp_is_dual_stack(struct ldpd_conf
*);
707 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
708 int ldp_reload(struct ldpd_conf
*);
709 void ldp_clear_config(struct ldpd_conf
*);
710 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
711 struct ldpd_conf
*config_new_empty(void);
712 void config_clear(struct ldpd_conf
*);
715 /* NOTE: the parameters' names should be preserved because of codegen */
716 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
718 void iface_del_api(struct ldpd_conf
*conf
,
719 struct iface
*iface
);
720 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
721 union ldpd_addr
*addr
);
722 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
723 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
724 struct in_addr lsr_id
);
725 void nbrp_del_api(struct ldpd_conf
*conf
,
726 struct nbr_params
*nbrp
);
727 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
728 void l2vpn_del_api(struct ldpd_conf
*conf
,
729 struct l2vpn
*l2vpn
);
730 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
731 struct l2vpn
*l2vpn
, const char *ifname
);
732 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
733 struct l2vpn_if
*lif
);
734 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
735 struct l2vpn
*l2vpn
, const char *ifname
);
736 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
737 struct l2vpn_pw
*pw
);
740 int ldp_create_socket(int, enum socket_type
);
741 void sock_set_nonblock(int);
742 void sock_set_cloexec(int);
743 void sock_set_recvbuf(int);
744 int sock_set_reuse(int, int);
745 int sock_set_bindany(int, int);
746 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
747 int sock_set_ipv4_tos(int, int);
748 int sock_set_ipv4_pktinfo(int, int);
749 int sock_set_ipv4_recvdstaddr(int, int);
750 int sock_set_ipv4_recvif(int, int);
751 int sock_set_ipv4_minttl(int, int);
752 int sock_set_ipv4_ucast_ttl(int fd
, int);
753 int sock_set_ipv4_mcast_ttl(int, uint8_t);
754 int sock_set_ipv4_mcast(struct iface
*);
755 int sock_set_ipv4_mcast_loop(int);
756 int sock_set_ipv6_dscp(int, int);
757 int sock_set_ipv6_pktinfo(int, int);
758 int sock_set_ipv6_minhopcount(int, int);
759 int sock_set_ipv6_ucast_hops(int, int);
760 int sock_set_ipv6_mcast_hops(int, int);
761 int sock_set_ipv6_mcast(struct iface
*);
762 int sock_set_ipv6_mcast_loop(int);
765 extern struct thread_master
*master
;
766 extern char ctl_sock_path
[MAXPATHLEN
];
769 void ldp_zebra_init(struct thread_master
*);
770 void ldp_zebra_destroy(void);
774 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
775 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
776 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
777 (IN6_IS_ADDR_MULTICAST(a) && \
778 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
781 #endif /* _LDPD_H_ */