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"
34 #define CONF_FILE "/etc/ldpd.conf"
35 #define LDPD_USER "_ldpd"
37 #define LDPD_FD_ASYNC 3
38 #define LDPD_FD_SYNC 4
40 #define LDPD_OPT_VERBOSE 0x00000001
41 #define LDPD_OPT_VERBOSE2 0x00000002
42 #define LDPD_OPT_NOACTION 0x00000004
44 #define TCP_MD5_KEY_LEN 80
45 #define L2VPN_NAME_LEN 32
47 #define RT_BUF_SIZE 16384
48 #define MAX_RTSOCK_BUF 128 * 1024
49 #define LDP_BACKLOG 128
51 #define F_LDPD_INSERTED 0x0001
52 #define F_CONNECTED 0x0002
53 #define F_STATIC 0x0004
54 #define F_DYNAMIC 0x0008
55 #define F_REJECT 0x0010
56 #define F_BLACKHOLE 0x0020
57 #define F_REDISTRIBUTED 0x0040
62 int (*handler
)(struct thread
*);
68 int (*handler_write
)(struct thread
*);
69 struct thread
*ev_write
;
70 int (*handler_read
)(struct thread
*);
71 struct thread
*ev_read
;
77 IMSG_CTL_SHOW_INTERFACE
,
78 IMSG_CTL_SHOW_DISCOVERY
,
79 IMSG_CTL_SHOW_DISC_IFACE
,
80 IMSG_CTL_SHOW_DISC_TNBR
,
81 IMSG_CTL_SHOW_DISC_ADJ
,
83 IMSG_CTL_SHOW_NBR_DISC
,
84 IMSG_CTL_SHOW_NBR_END
,
86 IMSG_CTL_SHOW_L2VPN_PW
,
87 IMSG_CTL_SHOW_L2VPN_BINDING
,
90 IMSG_CTL_FIB_DECOUPLE
,
105 IMSG_LABEL_MAPPING_FULL
,
111 IMSG_REQUEST_ADD_END
,
113 IMSG_MAPPING_ADD_END
,
115 IMSG_RELEASE_ADD_END
,
117 IMSG_WITHDRAW_ADD_END
,
121 IMSG_NOTIFICATION_SEND
,
129 IMSG_REQUEST_SOCKETS
,
136 IMSG_RECONF_L2VPN_IF
,
137 IMSG_RECONF_L2VPN_PW
,
138 IMSG_RECONF_L2VPN_IPW
,
150 #define IN6_IS_SCOPE_EMBED(a) \
151 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
152 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
153 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
155 /* interface states */
156 #define IF_STA_DOWN 0x01
157 #define IF_STA_ACTIVE 0x02
159 /* targeted neighbor states */
160 #define TNBR_STA_DOWN 0x01
161 #define TNBR_STA_ACTIVE 0x02
163 /* interface types */
169 /* neighbor states */
170 #define NBR_STA_PRESENT 0x0001
171 #define NBR_STA_INITIAL 0x0002
172 #define NBR_STA_OPENREC 0x0004
173 #define NBR_STA_OPENSENT 0x0008
174 #define NBR_STA_OPER 0x0010
175 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
176 NBR_STA_OPENSENT | NBR_STA_OPER)
178 /* neighbor events */
183 NBR_EVT_CLOSE_SESSION
,
185 NBR_EVT_KEEPALIVE_RCVD
,
191 /* neighbor actions */
194 NBR_ACT_RST_KTIMEOUT
,
197 NBR_ACT_CONNECT_SETUP
,
198 NBR_ACT_PASSIVE_INIT
,
199 NBR_ACT_KEEPALIVE_SEND
,
200 NBR_ACT_CLOSE_SESSION
203 /* forward declarations */
204 RB_HEAD(global_adj_head
, adj
);
205 RB_HEAD(nbr_adj_head
, adj
);
206 RB_HEAD(ia_adj_head
, adj
);
214 union ldpd_addr prefix
;
225 uint32_t status_code
;
234 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
235 #define F_MAP_STATUS 0x02 /* status */
236 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
237 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
238 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
239 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
242 uint32_t status_code
;
243 uint32_t msg_id
; /* network byte order */
244 uint16_t msg_type
; /* network byte order */
249 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
250 #define F_NOTIF_FEC 0x02 /* fec tlv present */
253 LIST_ENTRY(if_addr
) entry
;
255 union ldpd_addr addr
;
257 union ldpd_addr dstbrd
;
259 LIST_HEAD(if_addr_head
, if_addr
);
266 struct ia_adj_head adj_tree
;
268 struct thread
*hello_timer
;
269 uint16_t hello_holdtime
;
270 uint16_t hello_interval
;
274 RB_ENTRY(iface
) entry
;
275 char name
[IF_NAMESIZE
];
276 unsigned int ifindex
;
277 struct if_addr_head addr_list
;
278 struct in6_addr linklocal
;
279 enum iface_type type
;
281 struct iface_af ipv4
;
282 struct iface_af ipv6
;
285 RB_HEAD(iface_head
, iface
);
286 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
287 DECLARE_QOBJ_TYPE(iface
)
289 /* source of targeted hellos */
291 RB_ENTRY(tnbr
) entry
;
292 struct thread
*hello_timer
;
295 union ldpd_addr addr
;
301 RB_HEAD(tnbr_head
, tnbr
);
302 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
303 DECLARE_QOBJ_TYPE(tnbr
)
304 #define F_TNBR_CONFIGURED 0x01
305 #define F_TNBR_DYNAMIC 0x02
312 /* neighbor specific parameters */
314 RB_ENTRY(nbr_params
) entry
;
315 struct in_addr lsr_id
;
320 enum auth_method method
;
321 char md5key
[TCP_MD5_KEY_LEN
];
327 RB_HEAD(nbrp_head
, nbr_params
);
328 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
329 DECLARE_QOBJ_TYPE(nbr_params
)
330 #define F_NBRP_KEEPALIVE 0x01
331 #define F_NBRP_GTSM 0x02
332 #define F_NBRP_GTSM_HOPS 0x04
335 RB_ENTRY(l2vpn_if
) entry
;
337 char ifname
[IF_NAMESIZE
];
338 unsigned int ifindex
;
342 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
343 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
344 DECLARE_QOBJ_TYPE(l2vpn_if
)
347 RB_ENTRY(l2vpn_pw
) entry
;
349 struct in_addr lsr_id
;
351 union ldpd_addr addr
;
353 char ifname
[IF_NAMESIZE
];
354 unsigned int ifindex
;
355 uint32_t remote_group
;
357 uint32_t remote_status
;
361 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
362 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
363 DECLARE_QOBJ_TYPE(l2vpn_pw
)
364 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
365 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
366 #define F_PW_CWORD_CONF 0x04 /* control word configured */
367 #define F_PW_CWORD 0x08 /* control word negotiated */
368 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
369 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
372 RB_ENTRY(l2vpn
) entry
;
373 char name
[L2VPN_NAME_LEN
];
377 char br_ifname
[IF_NAMESIZE
];
378 unsigned int br_ifindex
;
379 struct l2vpn_if_head if_tree
;
380 struct l2vpn_pw_head pw_tree
;
381 struct l2vpn_pw_head pw_inactive_tree
;
384 RB_HEAD(l2vpn_head
, l2vpn
);
385 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
386 DECLARE_QOBJ_TYPE(l2vpn
)
387 #define L2VPN_TYPE_VPWS 1
388 #define L2VPN_TYPE_VPLS 2
408 struct ldpd_af_conf
{
410 uint16_t lhello_holdtime
;
411 uint16_t lhello_interval
;
412 uint16_t thello_holdtime
;
413 uint16_t thello_interval
;
414 union ldpd_addr trans_addr
;
415 char acl_thello_accept_from
[ACL_NAMSIZ
];
416 char acl_label_allocate_for
[ACL_NAMSIZ
];
417 char acl_label_advertise_to
[ACL_NAMSIZ
];
418 char acl_label_advertise_for
[ACL_NAMSIZ
];
419 char acl_label_expnull_for
[ACL_NAMSIZ
];
420 char acl_label_accept_from
[ACL_NAMSIZ
];
421 char acl_label_accept_for
[ACL_NAMSIZ
];
424 #define F_LDPD_AF_ENABLED 0x0001
425 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
426 #define F_LDPD_AF_EXPNULL 0x0004
427 #define F_LDPD_AF_NO_GTSM 0x0008
428 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
431 struct in_addr rtr_id
;
432 struct ldpd_af_conf ipv4
;
433 struct ldpd_af_conf ipv6
;
434 struct iface_head iface_tree
;
435 struct tnbr_head tnbr_tree
;
436 struct nbrp_head nbrp_tree
;
437 struct l2vpn_head l2vpn_tree
;
438 uint16_t lhello_holdtime
;
439 uint16_t lhello_interval
;
440 uint16_t thello_holdtime
;
441 uint16_t thello_interval
;
446 DECLARE_QOBJ_TYPE(ldpd_conf
)
447 #define F_LDPD_NO_FIB_UPDATE 0x0001
448 #define F_LDPD_DS_CISCO_INTEROP 0x0002
449 #define F_LDPD_ENABLED 0x0004
451 struct ldpd_af_global
{
452 struct thread
*disc_ev
;
453 struct thread
*edisc_ev
;
455 int ldp_edisc_socket
;
456 int ldp_session_socket
;
462 struct in_addr rtr_id
;
463 struct ldpd_af_global ipv4
;
464 struct ldpd_af_global ipv6
;
465 uint32_t conf_seqnum
;
467 struct if_addr_head addr_list
;
468 struct global_adj_head adj_tree
;
469 struct in_addr mcast_addr_v4
;
470 struct in6_addr mcast_addr_v6
;
471 TAILQ_HEAD(, pending_conn
) pending_conns
;
477 union ldpd_addr prefix
;
479 union ldpd_addr nexthop
;
480 uint32_t local_label
;
481 uint32_t remote_label
;
482 unsigned short ifindex
;
488 unsigned short ifindex
;
491 union ldpd_addr nexthop
;
492 uint32_t local_label
;
493 uint32_t remote_label
;
498 unsigned short ifindex
;
500 union ldpd_addr addr
;
502 union ldpd_addr dstbrd
;
506 char ifname
[IF_NAMESIZE
];
507 unsigned short ifindex
;
513 char acl
[ACL_NAMSIZ
];
515 union ldpd_addr addr
;
519 /* control data structures */
522 char name
[IF_NAMESIZE
];
523 unsigned int ifindex
;
526 enum iface_type type
;
527 uint16_t hello_holdtime
;
528 uint16_t hello_interval
;
534 char name
[IF_NAMESIZE
];
540 struct ctl_disc_tnbr
{
542 union ldpd_addr addr
;
549 enum hello_type type
;
550 char ifname
[IF_NAMESIZE
];
551 union ldpd_addr src_addr
;
553 union ldpd_addr trans_addr
;
559 union ldpd_addr laddr
;
561 union ldpd_addr raddr
;
570 union ldpd_addr prefix
;
572 struct in_addr nexthop
; /* lsr-id */
573 uint32_t local_label
;
574 uint32_t remote_label
;
582 char l2vpn_name
[L2VPN_NAME_LEN
];
583 char ifname
[IF_NAMESIZE
];
585 struct in_addr lsr_id
;
586 uint32_t local_label
;
588 uint16_t local_ifmtu
;
590 uint32_t remote_label
;
592 uint16_t remote_ifmtu
;
593 uint8_t remote_cword
;
597 extern struct ldpd_conf
*ldpd_conf
;
598 extern struct ldpd_global global
;
601 struct ldpd_conf
*parse_config(char *);
602 int cmdline_symset(char *);
605 void kif_redistribute(const char *);
606 int kr_change(struct kroute
*);
607 int kr_delete(struct kroute
*);
608 int kmpw_set(struct kpw
*);
609 int kmpw_unset(struct kpw
*);
612 uint8_t mask2prefixlen(in_addr_t
);
613 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
614 in_addr_t
prefixlen2mask(uint8_t);
615 struct in6_addr
*prefixlen2mask6(uint8_t);
616 void ldp_applymask(int, union ldpd_addr
*,
617 const union ldpd_addr
*, int);
618 int ldp_addrcmp(int, const union ldpd_addr
*,
619 const union ldpd_addr
*);
620 int ldp_addrisset(int, const union ldpd_addr
*);
621 int ldp_prefixcmp(int, const union ldpd_addr
*,
622 const union ldpd_addr
*, uint8_t);
623 int bad_addr_v4(struct in_addr
);
624 int bad_addr_v6(struct in6_addr
*);
625 int bad_addr(int, union ldpd_addr
*);
626 void embedscope(struct sockaddr_in6
*);
627 void recoverscope(struct sockaddr_in6
*);
628 void addscope(struct sockaddr_in6
*, uint32_t);
629 void clearscope(struct in6_addr
*);
630 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
631 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
633 socklen_t
sockaddr_len(struct sockaddr
*);
636 int ldp_write_handler(struct thread
*);
637 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
638 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
639 int main_imsg_compose_both(enum imsg_type
, void *,
641 void imsg_event_add(struct imsgev
*);
642 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
643 pid_t
, int, void *, uint16_t);
644 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
645 void evbuf_event_add(struct evbuf
*);
646 void evbuf_init(struct evbuf
*, int,
647 int (*)(struct thread
*), void *);
648 void evbuf_clear(struct evbuf
*);
649 int ldp_acl_request(struct imsgev
*, char *, int,
650 union ldpd_addr
*, uint8_t);
651 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
652 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
653 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
654 int ldp_is_dual_stack(struct ldpd_conf
*);
655 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
656 int ldp_reload(struct ldpd_conf
*);
657 int ldp_reload_ref(struct ldpd_conf
*, void **);
658 struct ldpd_conf
*ldp_dup_config_ref(struct ldpd_conf
*, void **ref
);
659 struct ldpd_conf
*ldp_dup_config(struct ldpd_conf
*);
660 void ldp_clear_config(struct ldpd_conf
*);
661 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
662 struct ldpd_conf
*config_new_empty(void);
663 void config_clear(struct ldpd_conf
*);
666 /* NOTE: the parameters' names should be preserved because of codegen */
667 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
669 void iface_del_api(struct ldpd_conf
*conf
,
670 struct iface
*iface
);
671 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
672 union ldpd_addr
*addr
);
673 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
674 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
675 struct in_addr lsr_id
);
676 void nbrp_del_api(struct ldpd_conf
*conf
,
677 struct nbr_params
*nbrp
);
678 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
679 void l2vpn_del_api(struct ldpd_conf
*conf
,
680 struct l2vpn
*l2vpn
);
681 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
682 struct l2vpn
*l2vpn
, const char *ifname
);
683 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
684 struct l2vpn_if
*lif
);
685 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
686 struct l2vpn
*l2vpn
, const char *ifname
);
687 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
688 struct l2vpn_pw
*pw
);
691 int ldp_create_socket(int, enum socket_type
);
692 void sock_set_nonblock(int);
693 void sock_set_cloexec(int);
694 void sock_set_recvbuf(int);
695 int sock_set_reuse(int, int);
696 int sock_set_bindany(int, int);
697 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
698 int sock_set_ipv4_tos(int, int);
699 int sock_set_ipv4_pktinfo(int, int);
700 int sock_set_ipv4_recvdstaddr(int, int);
701 int sock_set_ipv4_recvif(int, int);
702 int sock_set_ipv4_minttl(int, int);
703 int sock_set_ipv4_ucast_ttl(int fd
, int);
704 int sock_set_ipv4_mcast_ttl(int, uint8_t);
705 int sock_set_ipv4_mcast(struct iface
*);
706 int sock_set_ipv4_mcast_loop(int);
707 int sock_set_ipv6_dscp(int, int);
708 int sock_set_ipv6_pktinfo(int, int);
709 int sock_set_ipv6_minhopcount(int, int);
710 int sock_set_ipv6_ucast_hops(int, int);
711 int sock_set_ipv6_mcast_hops(int, int);
712 int sock_set_ipv6_mcast(struct iface
*);
713 int sock_set_ipv6_mcast_loop(int);
716 extern struct thread_master
*master
;
717 extern char ctl_sock_path
[MAXPATHLEN
];
720 void ldp_zebra_init(struct thread_master
*);
721 void ldp_zebra_destroy(void);
725 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
726 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
727 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
728 (IN6_IS_ADDR_MULTICAST(a) && \
729 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
732 #endif /* _LDPD_H_ */