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_DISC_IFACE
,
81 IMSG_CTL_SHOW_DISC_TNBR
,
82 IMSG_CTL_SHOW_DISC_ADJ
,
84 IMSG_CTL_SHOW_NBR_DISC
,
85 IMSG_CTL_SHOW_NBR_END
,
87 IMSG_CTL_SHOW_L2VPN_PW
,
88 IMSG_CTL_SHOW_L2VPN_BINDING
,
91 IMSG_CTL_FIB_DECOUPLE
,
100 IMSG_KPWLABEL_DELETE
,
106 IMSG_LABEL_MAPPING_FULL
,
112 IMSG_REQUEST_ADD_END
,
114 IMSG_MAPPING_ADD_END
,
116 IMSG_RELEASE_ADD_END
,
118 IMSG_WITHDRAW_ADD_END
,
122 IMSG_NOTIFICATION_SEND
,
130 IMSG_REQUEST_SOCKETS
,
137 IMSG_RECONF_L2VPN_IF
,
138 IMSG_RECONF_L2VPN_PW
,
139 IMSG_RECONF_L2VPN_IPW
,
151 #define IN6_IS_SCOPE_EMBED(a) \
152 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
153 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
154 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
156 /* interface states */
157 #define IF_STA_DOWN 0x01
158 #define IF_STA_ACTIVE 0x02
160 /* targeted neighbor states */
161 #define TNBR_STA_DOWN 0x01
162 #define TNBR_STA_ACTIVE 0x02
164 /* interface types */
170 /* neighbor states */
171 #define NBR_STA_PRESENT 0x0001
172 #define NBR_STA_INITIAL 0x0002
173 #define NBR_STA_OPENREC 0x0004
174 #define NBR_STA_OPENSENT 0x0008
175 #define NBR_STA_OPER 0x0010
176 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
177 NBR_STA_OPENSENT | NBR_STA_OPER)
179 /* neighbor events */
184 NBR_EVT_CLOSE_SESSION
,
186 NBR_EVT_KEEPALIVE_RCVD
,
192 /* neighbor actions */
195 NBR_ACT_RST_KTIMEOUT
,
198 NBR_ACT_CONNECT_SETUP
,
199 NBR_ACT_PASSIVE_INIT
,
200 NBR_ACT_KEEPALIVE_SEND
,
201 NBR_ACT_CLOSE_SESSION
204 /* forward declarations */
205 RB_HEAD(global_adj_head
, adj
);
206 RB_HEAD(nbr_adj_head
, adj
);
207 RB_HEAD(ia_adj_head
, adj
);
215 union ldpd_addr prefix
;
233 uint32_t status_code
;
242 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
243 #define F_MAP_STATUS 0x02 /* status */
244 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
245 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
246 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
247 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
250 uint32_t status_code
;
251 uint32_t msg_id
; /* network byte order */
252 uint16_t msg_type
; /* network byte order */
262 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
263 #define F_NOTIF_FEC 0x02 /* fec tlv present */
264 #define F_NOTIF_RETURNED_TLVS 0x04 /* returned tlvs present */
267 LIST_ENTRY(if_addr
) entry
;
269 union ldpd_addr addr
;
271 union ldpd_addr dstbrd
;
273 LIST_HEAD(if_addr_head
, if_addr
);
280 struct ia_adj_head adj_tree
;
282 struct thread
*hello_timer
;
283 uint16_t hello_holdtime
;
284 uint16_t hello_interval
;
288 RB_ENTRY(iface
) entry
;
289 char name
[IF_NAMESIZE
];
290 unsigned int ifindex
;
291 struct if_addr_head addr_list
;
292 struct in6_addr linklocal
;
293 enum iface_type type
;
295 struct iface_af ipv4
;
296 struct iface_af ipv6
;
299 RB_HEAD(iface_head
, iface
);
300 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
301 DECLARE_QOBJ_TYPE(iface
)
303 /* source of targeted hellos */
305 RB_ENTRY(tnbr
) entry
;
306 struct thread
*hello_timer
;
309 union ldpd_addr addr
;
315 RB_HEAD(tnbr_head
, tnbr
);
316 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
317 DECLARE_QOBJ_TYPE(tnbr
)
318 #define F_TNBR_CONFIGURED 0x01
319 #define F_TNBR_DYNAMIC 0x02
326 /* neighbor specific parameters */
328 RB_ENTRY(nbr_params
) entry
;
329 struct in_addr lsr_id
;
334 enum auth_method method
;
335 char md5key
[TCP_MD5_KEY_LEN
];
341 RB_HEAD(nbrp_head
, nbr_params
);
342 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
343 DECLARE_QOBJ_TYPE(nbr_params
)
344 #define F_NBRP_KEEPALIVE 0x01
345 #define F_NBRP_GTSM 0x02
346 #define F_NBRP_GTSM_HOPS 0x04
349 RB_ENTRY(l2vpn_if
) entry
;
351 char ifname
[IF_NAMESIZE
];
352 unsigned int ifindex
;
354 uint8_t mac
[ETHER_ADDR_LEN
];
357 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
358 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
359 DECLARE_QOBJ_TYPE(l2vpn_if
)
362 RB_ENTRY(l2vpn_pw
) entry
;
364 struct in_addr lsr_id
;
366 union ldpd_addr addr
;
368 char ifname
[IF_NAMESIZE
];
369 unsigned int ifindex
;
370 uint32_t remote_group
;
372 uint32_t remote_status
;
376 RB_HEAD(l2vpn_pw_head
, l2vpn_pw
);
377 RB_PROTOTYPE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
);
378 DECLARE_QOBJ_TYPE(l2vpn_pw
)
379 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
380 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
381 #define F_PW_CWORD_CONF 0x04 /* control word configured */
382 #define F_PW_CWORD 0x08 /* control word negotiated */
383 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
384 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
387 RB_ENTRY(l2vpn
) entry
;
388 char name
[L2VPN_NAME_LEN
];
392 char br_ifname
[IF_NAMESIZE
];
393 unsigned int br_ifindex
;
394 struct l2vpn_if_head if_tree
;
395 struct l2vpn_pw_head pw_tree
;
396 struct l2vpn_pw_head pw_inactive_tree
;
399 RB_HEAD(l2vpn_head
, l2vpn
);
400 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
401 DECLARE_QOBJ_TYPE(l2vpn
)
402 #define L2VPN_TYPE_VPWS 1
403 #define L2VPN_TYPE_VPLS 2
423 struct ldpd_af_conf
{
425 uint16_t lhello_holdtime
;
426 uint16_t lhello_interval
;
427 uint16_t thello_holdtime
;
428 uint16_t thello_interval
;
429 union ldpd_addr trans_addr
;
430 char acl_thello_accept_from
[ACL_NAMSIZ
];
431 char acl_label_allocate_for
[ACL_NAMSIZ
];
432 char acl_label_advertise_to
[ACL_NAMSIZ
];
433 char acl_label_advertise_for
[ACL_NAMSIZ
];
434 char acl_label_expnull_for
[ACL_NAMSIZ
];
435 char acl_label_accept_from
[ACL_NAMSIZ
];
436 char acl_label_accept_for
[ACL_NAMSIZ
];
439 #define F_LDPD_AF_ENABLED 0x0001
440 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
441 #define F_LDPD_AF_EXPNULL 0x0004
442 #define F_LDPD_AF_NO_GTSM 0x0008
443 #define F_LDPD_AF_ALLOCHOSTONLY 0x0010
446 struct in_addr rtr_id
;
447 struct ldpd_af_conf ipv4
;
448 struct ldpd_af_conf ipv6
;
449 struct iface_head iface_tree
;
450 struct tnbr_head tnbr_tree
;
451 struct nbrp_head nbrp_tree
;
452 struct l2vpn_head l2vpn_tree
;
453 uint16_t lhello_holdtime
;
454 uint16_t lhello_interval
;
455 uint16_t thello_holdtime
;
456 uint16_t thello_interval
;
461 DECLARE_QOBJ_TYPE(ldpd_conf
)
462 #define F_LDPD_NO_FIB_UPDATE 0x0001
463 #define F_LDPD_DS_CISCO_INTEROP 0x0002
464 #define F_LDPD_ENABLED 0x0004
466 struct ldpd_af_global
{
467 struct thread
*disc_ev
;
468 struct thread
*edisc_ev
;
470 int ldp_edisc_socket
;
471 int ldp_session_socket
;
477 struct in_addr rtr_id
;
478 struct ldpd_af_global ipv4
;
479 struct ldpd_af_global ipv6
;
480 uint32_t conf_seqnum
;
482 struct if_addr_head addr_list
;
483 struct global_adj_head adj_tree
;
484 struct in_addr mcast_addr_v4
;
485 struct in6_addr mcast_addr_v6
;
486 TAILQ_HEAD(, pending_conn
) pending_conns
;
492 union ldpd_addr prefix
;
494 union ldpd_addr nexthop
;
495 uint32_t local_label
;
496 uint32_t remote_label
;
497 unsigned short ifindex
;
503 unsigned short ifindex
;
506 union ldpd_addr nexthop
;
507 uint32_t local_label
;
508 uint32_t remote_label
;
513 unsigned short ifindex
;
515 union ldpd_addr addr
;
517 union ldpd_addr dstbrd
;
521 char ifname
[IF_NAMESIZE
];
522 unsigned short ifindex
;
524 uint8_t mac
[ETHER_ADDR_LEN
];
529 char acl
[ACL_NAMSIZ
];
531 union ldpd_addr addr
;
535 /* control data structures */
538 char name
[IF_NAMESIZE
];
539 unsigned int ifindex
;
542 enum iface_type type
;
543 uint16_t hello_holdtime
;
544 uint16_t hello_interval
;
550 char name
[IF_NAMESIZE
];
556 struct ctl_disc_tnbr
{
558 union ldpd_addr addr
;
565 enum hello_type type
;
566 char ifname
[IF_NAMESIZE
];
567 union ldpd_addr src_addr
;
569 union ldpd_addr trans_addr
;
575 union ldpd_addr laddr
;
577 union ldpd_addr raddr
;
586 union ldpd_addr prefix
;
588 struct in_addr nexthop
; /* lsr-id */
589 uint32_t local_label
;
590 uint32_t remote_label
;
598 char l2vpn_name
[L2VPN_NAME_LEN
];
599 char ifname
[IF_NAMESIZE
];
601 struct in_addr lsr_id
;
602 uint32_t local_label
;
604 uint16_t local_ifmtu
;
606 uint32_t remote_label
;
608 uint16_t remote_ifmtu
;
609 uint8_t remote_cword
;
613 extern struct ldpd_conf
*ldpd_conf
;
614 extern struct ldpd_global global
;
617 struct ldpd_conf
*parse_config(char *);
618 int cmdline_symset(char *);
621 void kif_redistribute(const char *);
622 int kr_change(struct kroute
*);
623 int kr_delete(struct kroute
*);
624 int kmpw_set(struct kpw
*);
625 int kmpw_unset(struct kpw
*);
628 uint8_t mask2prefixlen(in_addr_t
);
629 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
630 in_addr_t
prefixlen2mask(uint8_t);
631 struct in6_addr
*prefixlen2mask6(uint8_t);
632 void ldp_applymask(int, union ldpd_addr
*,
633 const union ldpd_addr
*, int);
634 int ldp_addrcmp(int, const union ldpd_addr
*,
635 const union ldpd_addr
*);
636 int ldp_addrisset(int, const union ldpd_addr
*);
637 int ldp_prefixcmp(int, const union ldpd_addr
*,
638 const union ldpd_addr
*, uint8_t);
639 int bad_addr_v4(struct in_addr
);
640 int bad_addr_v6(struct in6_addr
*);
641 int bad_addr(int, union ldpd_addr
*);
642 void embedscope(struct sockaddr_in6
*);
643 void recoverscope(struct sockaddr_in6
*);
644 void addscope(struct sockaddr_in6
*, uint32_t);
645 void clearscope(struct in6_addr
*);
646 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
647 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
649 socklen_t
sockaddr_len(struct sockaddr
*);
652 int ldp_write_handler(struct thread
*);
653 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
654 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
655 int main_imsg_compose_both(enum imsg_type
, void *,
657 void imsg_event_add(struct imsgev
*);
658 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
659 pid_t
, int, void *, uint16_t);
660 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
661 void evbuf_event_add(struct evbuf
*);
662 void evbuf_init(struct evbuf
*, int,
663 int (*)(struct thread
*), void *);
664 void evbuf_clear(struct evbuf
*);
665 int ldp_acl_request(struct imsgev
*, char *, int,
666 union ldpd_addr
*, uint8_t);
667 void ldp_acl_reply(struct imsgev
*, struct acl_check
*);
668 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
669 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
670 int ldp_is_dual_stack(struct ldpd_conf
*);
671 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
672 int ldp_reload(struct ldpd_conf
*);
673 int ldp_reload_ref(struct ldpd_conf
*, void **);
674 struct ldpd_conf
*ldp_dup_config_ref(struct ldpd_conf
*, void **ref
);
675 struct ldpd_conf
*ldp_dup_config(struct ldpd_conf
*);
676 void ldp_clear_config(struct ldpd_conf
*);
677 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
678 struct ldpd_conf
*config_new_empty(void);
679 void config_clear(struct ldpd_conf
*);
682 /* NOTE: the parameters' names should be preserved because of codegen */
683 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
685 void iface_del_api(struct ldpd_conf
*conf
,
686 struct iface
*iface
);
687 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
688 union ldpd_addr
*addr
);
689 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
690 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
691 struct in_addr lsr_id
);
692 void nbrp_del_api(struct ldpd_conf
*conf
,
693 struct nbr_params
*nbrp
);
694 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
695 void l2vpn_del_api(struct ldpd_conf
*conf
,
696 struct l2vpn
*l2vpn
);
697 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
698 struct l2vpn
*l2vpn
, const char *ifname
);
699 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
700 struct l2vpn_if
*lif
);
701 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
702 struct l2vpn
*l2vpn
, const char *ifname
);
703 void l2vpn_pw_del_api(struct l2vpn
*l2vpn
,
704 struct l2vpn_pw
*pw
);
707 int ldp_create_socket(int, enum socket_type
);
708 void sock_set_nonblock(int);
709 void sock_set_cloexec(int);
710 void sock_set_recvbuf(int);
711 int sock_set_reuse(int, int);
712 int sock_set_bindany(int, int);
713 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
714 int sock_set_ipv4_tos(int, int);
715 int sock_set_ipv4_pktinfo(int, int);
716 int sock_set_ipv4_recvdstaddr(int, int);
717 int sock_set_ipv4_recvif(int, int);
718 int sock_set_ipv4_minttl(int, int);
719 int sock_set_ipv4_ucast_ttl(int fd
, int);
720 int sock_set_ipv4_mcast_ttl(int, uint8_t);
721 int sock_set_ipv4_mcast(struct iface
*);
722 int sock_set_ipv4_mcast_loop(int);
723 int sock_set_ipv6_dscp(int, int);
724 int sock_set_ipv6_pktinfo(int, int);
725 int sock_set_ipv6_minhopcount(int, int);
726 int sock_set_ipv6_ucast_hops(int, int);
727 int sock_set_ipv6_mcast_hops(int, int);
728 int sock_set_ipv6_mcast(struct iface
*);
729 int sock_set_ipv6_mcast_loop(int);
732 extern struct thread_master
*master
;
733 extern char ctl_sock_path
[MAXPATHLEN
];
736 void ldp_zebra_init(struct thread_master
*);
737 void ldp_zebra_destroy(void);
741 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
742 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
743 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
744 (IN6_IS_ADDR_MULTICAST(a) && \
745 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
748 #endif /* _LDPD_H_ */