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"
33 #define CONF_FILE "/etc/ldpd.conf"
34 #define LDPD_USER "_ldpd"
36 #define LDPD_OPT_VERBOSE 0x00000001
37 #define LDPD_OPT_VERBOSE2 0x00000002
38 #define LDPD_OPT_NOACTION 0x00000004
40 #define TCP_MD5_KEY_LEN 80
41 #define L2VPN_NAME_LEN 32
43 #define RT_BUF_SIZE 16384
44 #define MAX_RTSOCK_BUF 128 * 1024
45 #define LDP_BACKLOG 128
47 #define F_LDPD_INSERTED 0x0001
48 #define F_CONNECTED 0x0002
49 #define F_STATIC 0x0004
50 #define F_DYNAMIC 0x0008
51 #define F_REJECT 0x0010
52 #define F_BLACKHOLE 0x0020
53 #define F_REDISTRIBUTED 0x0040
58 int (*handler
)(struct thread
*);
64 int (*handler_write
)(struct thread
*);
65 struct thread
*ev_write
;
66 int (*handler_read
)(struct thread
*);
67 struct thread
*ev_read
;
73 IMSG_CTL_SHOW_INTERFACE
,
74 IMSG_CTL_SHOW_DISCOVERY
,
75 IMSG_CTL_SHOW_DISC_IFACE
,
76 IMSG_CTL_SHOW_DISC_TNBR
,
77 IMSG_CTL_SHOW_DISC_ADJ
,
79 IMSG_CTL_SHOW_NBR_DISC
,
80 IMSG_CTL_SHOW_NBR_END
,
82 IMSG_CTL_SHOW_L2VPN_PW
,
83 IMSG_CTL_SHOW_L2VPN_BINDING
,
86 IMSG_CTL_FIB_DECOUPLE
,
101 IMSG_LABEL_MAPPING_FULL
,
107 IMSG_REQUEST_ADD_END
,
109 IMSG_MAPPING_ADD_END
,
111 IMSG_RELEASE_ADD_END
,
113 IMSG_WITHDRAW_ADD_END
,
117 IMSG_NOTIFICATION_SEND
,
121 IMSG_NETWORK_ADD_END
,
126 IMSG_REQUEST_SOCKETS
,
133 IMSG_RECONF_L2VPN_IF
,
134 IMSG_RECONF_L2VPN_PW
,
135 IMSG_RECONF_L2VPN_IPW
,
146 #define IN6_IS_SCOPE_EMBED(a) \
147 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
148 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
149 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
151 /* interface states */
152 #define IF_STA_DOWN 0x01
153 #define IF_STA_ACTIVE 0x02
155 /* targeted neighbor states */
156 #define TNBR_STA_DOWN 0x01
157 #define TNBR_STA_ACTIVE 0x02
159 /* interface types */
165 /* neighbor states */
166 #define NBR_STA_PRESENT 0x0001
167 #define NBR_STA_INITIAL 0x0002
168 #define NBR_STA_OPENREC 0x0004
169 #define NBR_STA_OPENSENT 0x0008
170 #define NBR_STA_OPER 0x0010
171 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
172 NBR_STA_OPENSENT | NBR_STA_OPER)
174 /* neighbor events */
179 NBR_EVT_CLOSE_SESSION
,
181 NBR_EVT_KEEPALIVE_RCVD
,
187 /* neighbor actions */
190 NBR_ACT_RST_KTIMEOUT
,
193 NBR_ACT_CONNECT_SETUP
,
194 NBR_ACT_PASSIVE_INIT
,
195 NBR_ACT_KEEPALIVE_SEND
,
196 NBR_ACT_CLOSE_SESSION
199 TAILQ_HEAD(mapping_head
, mapping_entry
);
207 union ldpd_addr prefix
;
218 uint32_t status_code
;
227 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
228 #define F_MAP_STATUS 0x02 /* status */
229 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
230 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
231 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
232 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
235 uint32_t status_code
;
236 uint32_t msg_id
; /* network byte order */
237 uint16_t msg_type
; /* network byte order */
242 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
243 #define F_NOTIF_FEC 0x02 /* fec tlv present */
246 LIST_ENTRY(if_addr
) entry
;
248 union ldpd_addr addr
;
250 union ldpd_addr dstbrd
;
252 LIST_HEAD(if_addr_head
, if_addr
);
259 LIST_HEAD(, adj
) adj_list
;
261 struct thread
*hello_timer
;
262 uint16_t hello_holdtime
;
263 uint16_t hello_interval
;
267 RB_ENTRY(iface
) entry
;
268 char name
[IF_NAMESIZE
];
269 unsigned int ifindex
;
270 struct if_addr_head addr_list
;
271 struct in6_addr linklocal
;
272 enum iface_type type
;
274 struct iface_af ipv4
;
275 struct iface_af ipv6
;
278 RB_HEAD(iface_head
, iface
);
279 RB_PROTOTYPE(iface_head
, iface
, entry
, iface_compare
);
280 DECLARE_QOBJ_TYPE(iface
)
282 /* source of targeted hellos */
284 RB_ENTRY(tnbr
) entry
;
285 struct thread
*hello_timer
;
288 union ldpd_addr addr
;
294 RB_HEAD(tnbr_head
, tnbr
);
295 RB_PROTOTYPE(tnbr_head
, tnbr
, entry
, tnbr_compare
);
296 DECLARE_QOBJ_TYPE(tnbr
)
297 #define F_TNBR_CONFIGURED 0x01
298 #define F_TNBR_DYNAMIC 0x02
305 /* neighbor specific parameters */
307 RB_ENTRY(nbr_params
) entry
;
308 struct in_addr lsr_id
;
313 enum auth_method method
;
314 char md5key
[TCP_MD5_KEY_LEN
];
320 RB_HEAD(nbrp_head
, nbr_params
);
321 RB_PROTOTYPE(nbrp_head
, nbr_params
, entry
, nbr_params_compare
);
322 DECLARE_QOBJ_TYPE(nbr_params
)
323 #define F_NBRP_KEEPALIVE 0x01
324 #define F_NBRP_GTSM 0x02
325 #define F_NBRP_GTSM_HOPS 0x04
328 RB_ENTRY(l2vpn_if
) entry
;
330 char ifname
[IF_NAMESIZE
];
331 unsigned int ifindex
;
335 RB_HEAD(l2vpn_if_head
, l2vpn_if
);
336 RB_PROTOTYPE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
);
337 DECLARE_QOBJ_TYPE(l2vpn_if
)
340 LIST_ENTRY(l2vpn_pw
) entry
;
342 struct in_addr lsr_id
;
344 union ldpd_addr addr
;
346 char ifname
[IF_NAMESIZE
];
347 unsigned int ifindex
;
348 uint32_t remote_group
;
350 uint32_t remote_status
;
354 DECLARE_QOBJ_TYPE(l2vpn_pw
)
355 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
356 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
357 #define F_PW_CWORD_CONF 0x04 /* control word configured */
358 #define F_PW_CWORD 0x08 /* control word negotiated */
359 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
360 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
363 RB_ENTRY(l2vpn
) entry
;
364 char name
[L2VPN_NAME_LEN
];
368 char br_ifname
[IF_NAMESIZE
];
369 unsigned int br_ifindex
;
370 struct l2vpn_if_head if_tree
;
371 LIST_HEAD(, l2vpn_pw
) pw_list
;
372 LIST_HEAD(, l2vpn_pw
) pw_inactive_list
;
375 RB_HEAD(l2vpn_head
, l2vpn
);
376 RB_PROTOTYPE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
);
377 DECLARE_QOBJ_TYPE(l2vpn
)
378 #define L2VPN_TYPE_VPWS 1
379 #define L2VPN_TYPE_VPLS 2
399 struct ldpd_af_conf
{
401 uint16_t lhello_holdtime
;
402 uint16_t lhello_interval
;
403 uint16_t thello_holdtime
;
404 uint16_t thello_interval
;
405 union ldpd_addr trans_addr
;
408 #define F_LDPD_AF_ENABLED 0x0001
409 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
410 #define F_LDPD_AF_EXPNULL 0x0004
411 #define F_LDPD_AF_NO_GTSM 0x0008
414 struct in_addr rtr_id
;
415 struct ldpd_af_conf ipv4
;
416 struct ldpd_af_conf ipv6
;
417 struct iface_head iface_tree
;
418 struct tnbr_head tnbr_tree
;
419 struct nbrp_head nbrp_tree
;
420 struct l2vpn_head l2vpn_tree
;
421 uint16_t lhello_holdtime
;
422 uint16_t lhello_interval
;
423 uint16_t thello_holdtime
;
424 uint16_t thello_interval
;
429 DECLARE_QOBJ_TYPE(ldpd_conf
)
430 #define F_LDPD_NO_FIB_UPDATE 0x0001
431 #define F_LDPD_DS_CISCO_INTEROP 0x0002
432 #define F_LDPD_ENABLED 0x0004
434 struct ldpd_af_global
{
435 struct thread
*disc_ev
;
436 struct thread
*edisc_ev
;
438 int ldp_edisc_socket
;
439 int ldp_session_socket
;
445 struct in_addr rtr_id
;
446 struct ldpd_af_global ipv4
;
447 struct ldpd_af_global ipv6
;
448 uint32_t conf_seqnum
;
450 struct if_addr_head addr_list
;
451 LIST_HEAD(, adj
) adj_list
;
452 struct in_addr mcast_addr_v4
;
453 struct in6_addr mcast_addr_v6
;
454 TAILQ_HEAD(, pending_conn
) pending_conns
;
460 union ldpd_addr prefix
;
462 union ldpd_addr nexthop
;
463 uint32_t local_label
;
464 uint32_t remote_label
;
465 unsigned short ifindex
;
471 unsigned short ifindex
;
474 union ldpd_addr nexthop
;
475 uint32_t local_label
;
476 uint32_t remote_label
;
481 unsigned short ifindex
;
483 union ldpd_addr addr
;
485 union ldpd_addr dstbrd
;
489 char ifname
[IF_NAMESIZE
];
490 unsigned short ifindex
;
495 /* control data structures */
498 char name
[IF_NAMESIZE
];
499 unsigned int ifindex
;
502 enum iface_type type
;
503 uint16_t hello_holdtime
;
504 uint16_t hello_interval
;
510 char name
[IF_NAMESIZE
];
516 struct ctl_disc_tnbr
{
518 union ldpd_addr addr
;
525 enum hello_type type
;
526 char ifname
[IF_NAMESIZE
];
527 union ldpd_addr src_addr
;
529 union ldpd_addr trans_addr
;
535 union ldpd_addr laddr
;
537 union ldpd_addr raddr
;
546 union ldpd_addr prefix
;
548 struct in_addr nexthop
; /* lsr-id */
549 uint32_t local_label
;
550 uint32_t remote_label
;
558 char l2vpn_name
[L2VPN_NAME_LEN
];
559 char ifname
[IF_NAMESIZE
];
561 struct in_addr lsr_id
;
562 uint32_t local_label
;
564 uint16_t local_ifmtu
;
566 uint32_t remote_label
;
568 uint16_t remote_ifmtu
;
569 uint8_t remote_cword
;
573 extern struct ldpd_conf
*ldpd_conf
;
574 extern struct ldpd_global global
;
577 struct ldpd_conf
*parse_config(char *);
578 int cmdline_symset(char *);
581 void kif_redistribute(const char *);
582 int kr_change(struct kroute
*);
583 int kr_delete(struct kroute
*);
584 int kmpw_set(struct kpw
*);
585 int kmpw_unset(struct kpw
*);
588 uint8_t mask2prefixlen(in_addr_t
);
589 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
590 in_addr_t
prefixlen2mask(uint8_t);
591 struct in6_addr
*prefixlen2mask6(uint8_t);
592 void ldp_applymask(int, union ldpd_addr
*,
593 const union ldpd_addr
*, int);
594 int ldp_addrcmp(int, const union ldpd_addr
*,
595 const union ldpd_addr
*);
596 int ldp_addrisset(int, const union ldpd_addr
*);
597 int ldp_prefixcmp(int, const union ldpd_addr
*,
598 const union ldpd_addr
*, uint8_t);
599 int bad_addr_v4(struct in_addr
);
600 int bad_addr_v6(struct in6_addr
*);
601 int bad_addr(int, union ldpd_addr
*);
602 void embedscope(struct sockaddr_in6
*);
603 void recoverscope(struct sockaddr_in6
*);
604 void addscope(struct sockaddr_in6
*, uint32_t);
605 void clearscope(struct in6_addr
*);
606 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
607 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
609 socklen_t
sockaddr_len(struct sockaddr
*);
612 int ldp_write_handler(struct thread
*);
613 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
614 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
615 int main_imsg_compose_both(enum imsg_type
, void *,
617 void imsg_event_add(struct imsgev
*);
618 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
619 pid_t
, int, void *, uint16_t);
620 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
621 void evbuf_event_add(struct evbuf
*);
622 void evbuf_init(struct evbuf
*, int,
623 int (*)(struct thread
*), void *);
624 void evbuf_clear(struct evbuf
*);
625 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
626 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
627 int ldp_is_dual_stack(struct ldpd_conf
*);
628 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
629 int ldp_reload(struct ldpd_conf
*);
630 int ldp_reload_ref(struct ldpd_conf
*, void **);
631 struct ldpd_conf
*ldp_dup_config_ref(struct ldpd_conf
*, void **ref
);
632 struct ldpd_conf
*ldp_dup_config(struct ldpd_conf
*);
633 void ldp_clear_config(struct ldpd_conf
*);
634 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
635 struct ldpd_conf
*config_new_empty(void);
636 void config_clear(struct ldpd_conf
*);
639 /* NOTE: the parameters' names should be preserved because of codegen */
640 struct iface
*iface_new_api(struct ldpd_conf
*conf
,
642 void iface_del_api(struct ldpd_conf
*conf
,
643 struct iface
*iface
);
644 struct tnbr
*tnbr_new_api(struct ldpd_conf
*conf
, int af
,
645 union ldpd_addr
*addr
);
646 void tnbr_del_api(struct ldpd_conf
*conf
, struct tnbr
*tnbr
);
647 struct nbr_params
*nbrp_new_api(struct ldpd_conf
*conf
,
648 struct in_addr lsr_id
);
649 void nbrp_del_api(struct ldpd_conf
*conf
,
650 struct nbr_params
*nbrp
);
651 struct l2vpn
*l2vpn_new_api(struct ldpd_conf
*conf
, const char *name
);
652 void l2vpn_del_api(struct ldpd_conf
*conf
,
653 struct l2vpn
*l2vpn
);
654 struct l2vpn_if
*l2vpn_if_new_api(struct ldpd_conf
*conf
,
655 struct l2vpn
*l2vpn
, const char *ifname
);
656 void l2vpn_if_del_api(struct l2vpn
*l2vpn
,
657 struct l2vpn_if
*lif
);
658 struct l2vpn_pw
*l2vpn_pw_new_api(struct ldpd_conf
*conf
,
659 struct l2vpn
*l2vpn
, const char *ifname
);
660 void l2vpn_pw_del_api(struct l2vpn_pw
*pw
);
663 int ldp_create_socket(int, enum socket_type
);
664 void sock_set_nonblock(int);
665 void sock_set_cloexec(int);
666 void sock_set_recvbuf(int);
667 int sock_set_reuse(int, int);
668 int sock_set_bindany(int, int);
669 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
670 int sock_set_ipv4_tos(int, int);
671 int sock_set_ipv4_pktinfo(int, int);
672 int sock_set_ipv4_recvdstaddr(int, int);
673 int sock_set_ipv4_recvif(int, int);
674 int sock_set_ipv4_minttl(int, int);
675 int sock_set_ipv4_ucast_ttl(int fd
, int);
676 int sock_set_ipv4_mcast_ttl(int, uint8_t);
677 int sock_set_ipv4_mcast(struct iface
*);
678 int sock_set_ipv4_mcast_loop(int);
679 int sock_set_ipv6_dscp(int, int);
680 int sock_set_ipv6_pktinfo(int, int);
681 int sock_set_ipv6_minhopcount(int, int);
682 int sock_set_ipv6_ucast_hops(int, int);
683 int sock_set_ipv6_mcast_hops(int, int);
684 int sock_set_ipv6_mcast(struct iface
*);
685 int sock_set_ipv6_mcast_loop(int);
688 extern struct thread_master
*master
;
689 extern char ctl_sock_path
[MAXPATHLEN
];
692 void ldp_zebra_init(struct thread_master
*);
696 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
697 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
698 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
699 (IN6_IS_ADDR_MULTICAST(a) && \
700 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
703 #endif /* _LDPD_H_ */