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"
32 #define CONF_FILE "/etc/ldpd.conf"
33 #define LDPD_USER "_ldpd"
35 #define LDPD_OPT_VERBOSE 0x00000001
36 #define LDPD_OPT_VERBOSE2 0x00000002
37 #define LDPD_OPT_NOACTION 0x00000004
39 #define TCP_MD5_KEY_LEN 80
40 #define L2VPN_NAME_LEN 32
42 #define RT_BUF_SIZE 16384
43 #define MAX_RTSOCK_BUF 128 * 1024
44 #define LDP_BACKLOG 128
46 #define F_LDPD_INSERTED 0x0001
47 #define F_CONNECTED 0x0002
48 #define F_STATIC 0x0004
49 #define F_DYNAMIC 0x0008
50 #define F_REJECT 0x0010
51 #define F_BLACKHOLE 0x0020
52 #define F_REDISTRIBUTED 0x0040
57 int (*handler
)(struct thread
*);
63 int (*handler_write
)(struct thread
*);
64 struct thread
*ev_write
;
65 int (*handler_read
)(struct thread
*);
66 struct thread
*ev_read
;
72 IMSG_CTL_SHOW_INTERFACE
,
73 IMSG_CTL_SHOW_DISCOVERY
,
74 IMSG_CTL_SHOW_DISC_IFACE
,
75 IMSG_CTL_SHOW_DISC_TNBR
,
76 IMSG_CTL_SHOW_DISC_ADJ
,
78 IMSG_CTL_SHOW_NBR_DISC
,
79 IMSG_CTL_SHOW_NBR_END
,
81 IMSG_CTL_SHOW_L2VPN_PW
,
82 IMSG_CTL_SHOW_L2VPN_BINDING
,
85 IMSG_CTL_FIB_DECOUPLE
,
100 IMSG_LABEL_MAPPING_FULL
,
106 IMSG_REQUEST_ADD_END
,
108 IMSG_MAPPING_ADD_END
,
110 IMSG_RELEASE_ADD_END
,
112 IMSG_WITHDRAW_ADD_END
,
116 IMSG_NOTIFICATION_SEND
,
124 IMSG_REQUEST_SOCKETS
,
131 IMSG_RECONF_L2VPN_IF
,
132 IMSG_RECONF_L2VPN_PW
,
133 IMSG_RECONF_L2VPN_IPW
,
144 #define IN6_IS_SCOPE_EMBED(a) \
145 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
146 (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \
147 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
149 /* interface states */
150 #define IF_STA_DOWN 0x01
151 #define IF_STA_ACTIVE 0x02
153 /* targeted neighbor states */
154 #define TNBR_STA_DOWN 0x01
155 #define TNBR_STA_ACTIVE 0x02
157 /* interface types */
163 /* neighbor states */
164 #define NBR_STA_PRESENT 0x0001
165 #define NBR_STA_INITIAL 0x0002
166 #define NBR_STA_OPENREC 0x0004
167 #define NBR_STA_OPENSENT 0x0008
168 #define NBR_STA_OPER 0x0010
169 #define NBR_STA_SESSION (NBR_STA_INITIAL | NBR_STA_OPENREC | \
170 NBR_STA_OPENSENT | NBR_STA_OPER)
172 /* neighbor events */
177 NBR_EVT_CLOSE_SESSION
,
179 NBR_EVT_KEEPALIVE_RCVD
,
185 /* neighbor actions */
188 NBR_ACT_RST_KTIMEOUT
,
191 NBR_ACT_CONNECT_SETUP
,
192 NBR_ACT_PASSIVE_INIT
,
193 NBR_ACT_KEEPALIVE_SEND
,
194 NBR_ACT_CLOSE_SESSION
197 TAILQ_HEAD(mapping_head
, mapping_entry
);
205 union ldpd_addr prefix
;
216 uint32_t status_code
;
225 #define F_MAP_REQ_ID 0x01 /* optional request message id present */
226 #define F_MAP_STATUS 0x02 /* status */
227 #define F_MAP_PW_CWORD 0x04 /* pseudowire control word */
228 #define F_MAP_PW_ID 0x08 /* pseudowire connection id */
229 #define F_MAP_PW_IFMTU 0x10 /* pseudowire interface parameter */
230 #define F_MAP_PW_STATUS 0x20 /* pseudowire status */
233 uint32_t status_code
;
234 uint32_t msg_id
; /* network byte order */
235 uint16_t msg_type
; /* network byte order */
240 #define F_NOTIF_PW_STATUS 0x01 /* pseudowire status tlv present */
241 #define F_NOTIF_FEC 0x02 /* fec tlv present */
244 LIST_ENTRY(if_addr
) entry
;
246 union ldpd_addr addr
;
248 union ldpd_addr dstbrd
;
250 LIST_HEAD(if_addr_head
, if_addr
);
257 LIST_HEAD(, adj
) adj_list
;
259 struct thread
*hello_timer
;
260 uint16_t hello_holdtime
;
261 uint16_t hello_interval
;
265 LIST_ENTRY(iface
) entry
;
266 char name
[IF_NAMESIZE
];
267 unsigned int ifindex
;
268 struct if_addr_head addr_list
;
269 struct in6_addr linklocal
;
270 enum iface_type type
;
272 struct iface_af ipv4
;
273 struct iface_af ipv6
;
276 /* source of targeted hellos */
278 LIST_ENTRY(tnbr
) entry
;
279 struct thread
*hello_timer
;
282 union ldpd_addr addr
;
287 #define F_TNBR_CONFIGURED 0x01
288 #define F_TNBR_DYNAMIC 0x02
295 /* neighbor specific parameters */
297 LIST_ENTRY(nbr_params
) entry
;
298 struct in_addr lsr_id
;
303 enum auth_method method
;
304 char md5key
[TCP_MD5_KEY_LEN
];
309 #define F_NBRP_KEEPALIVE 0x01
310 #define F_NBRP_GTSM 0x02
311 #define F_NBRP_GTSM_HOPS 0x04
314 LIST_ENTRY(l2vpn_if
) entry
;
316 char ifname
[IF_NAMESIZE
];
317 unsigned int ifindex
;
322 LIST_ENTRY(l2vpn_pw
) entry
;
324 struct in_addr lsr_id
;
326 union ldpd_addr addr
;
328 char ifname
[IF_NAMESIZE
];
329 unsigned int ifindex
;
330 uint32_t remote_group
;
332 uint32_t remote_status
;
335 #define F_PW_STATUSTLV_CONF 0x01 /* status tlv configured */
336 #define F_PW_STATUSTLV 0x02 /* status tlv negotiated */
337 #define F_PW_CWORD_CONF 0x04 /* control word configured */
338 #define F_PW_CWORD 0x08 /* control word negotiated */
339 #define F_PW_STATUS_UP 0x10 /* pseudowire is operational */
340 #define F_PW_STATIC_NBR_ADDR 0x20 /* static neighbor address configured */
343 LIST_ENTRY(l2vpn
) entry
;
344 char name
[L2VPN_NAME_LEN
];
348 char br_ifname
[IF_NAMESIZE
];
349 unsigned int br_ifindex
;
350 LIST_HEAD(, l2vpn_if
) if_list
;
351 LIST_HEAD(, l2vpn_pw
) pw_list
;
352 LIST_HEAD(, l2vpn_pw
) pw_inactive_list
;
354 #define L2VPN_TYPE_VPWS 1
355 #define L2VPN_TYPE_VPLS 2
375 struct ldpd_af_conf
{
377 uint16_t lhello_holdtime
;
378 uint16_t lhello_interval
;
379 uint16_t thello_holdtime
;
380 uint16_t thello_interval
;
381 union ldpd_addr trans_addr
;
384 #define F_LDPD_AF_ENABLED 0x0001
385 #define F_LDPD_AF_THELLO_ACCEPT 0x0002
386 #define F_LDPD_AF_EXPNULL 0x0004
387 #define F_LDPD_AF_NO_GTSM 0x0008
390 struct in_addr rtr_id
;
391 struct ldpd_af_conf ipv4
;
392 struct ldpd_af_conf ipv6
;
393 LIST_HEAD(, iface
) iface_list
;
394 LIST_HEAD(, tnbr
) tnbr_list
;
395 LIST_HEAD(, nbr_params
) nbrp_list
;
396 LIST_HEAD(, l2vpn
) l2vpn_list
;
397 uint16_t lhello_holdtime
;
398 uint16_t lhello_interval
;
399 uint16_t thello_holdtime
;
400 uint16_t thello_interval
;
404 #define F_LDPD_NO_FIB_UPDATE 0x0001
405 #define F_LDPD_DS_CISCO_INTEROP 0x0002
406 #define F_LDPD_ENABLED 0x0004
408 struct ldpd_af_global
{
409 struct thread
*disc_ev
;
410 struct thread
*edisc_ev
;
412 int ldp_edisc_socket
;
413 int ldp_session_socket
;
419 struct in_addr rtr_id
;
420 struct ldpd_af_global ipv4
;
421 struct ldpd_af_global ipv6
;
422 uint32_t conf_seqnum
;
424 struct if_addr_head addr_list
;
425 LIST_HEAD(, adj
) adj_list
;
426 struct in_addr mcast_addr_v4
;
427 struct in6_addr mcast_addr_v6
;
428 TAILQ_HEAD(, pending_conn
) pending_conns
;
434 union ldpd_addr prefix
;
436 union ldpd_addr nexthop
;
437 uint32_t local_label
;
438 uint32_t remote_label
;
439 unsigned short ifindex
;
445 unsigned short ifindex
;
448 union ldpd_addr nexthop
;
449 uint32_t local_label
;
450 uint32_t remote_label
;
455 unsigned short ifindex
;
457 union ldpd_addr addr
;
459 union ldpd_addr dstbrd
;
463 char ifname
[IF_NAMESIZE
];
464 unsigned short ifindex
;
469 /* control data structures */
472 char name
[IF_NAMESIZE
];
473 unsigned int ifindex
;
476 enum iface_type type
;
477 uint16_t hello_holdtime
;
478 uint16_t hello_interval
;
484 char name
[IF_NAMESIZE
];
490 struct ctl_disc_tnbr
{
492 union ldpd_addr addr
;
499 enum hello_type type
;
500 char ifname
[IF_NAMESIZE
];
501 union ldpd_addr src_addr
;
503 union ldpd_addr trans_addr
;
509 union ldpd_addr laddr
;
511 union ldpd_addr raddr
;
520 union ldpd_addr prefix
;
522 struct in_addr nexthop
; /* lsr-id */
523 uint32_t local_label
;
524 uint32_t remote_label
;
532 char l2vpn_name
[L2VPN_NAME_LEN
];
533 char ifname
[IF_NAMESIZE
];
535 struct in_addr lsr_id
;
536 uint32_t local_label
;
538 uint16_t local_ifmtu
;
540 uint32_t remote_label
;
542 uint16_t remote_ifmtu
;
543 uint8_t remote_cword
;
547 extern struct ldpd_conf
*ldpd_conf
;
548 extern struct ldpd_global global
;
551 struct ldpd_conf
*parse_config(char *);
552 int cmdline_symset(char *);
555 void kif_redistribute(const char *);
556 int kr_change(struct kroute
*);
557 int kr_delete(struct kroute
*);
558 int kmpw_set(struct kpw
*);
559 int kmpw_unset(struct kpw
*);
562 uint8_t mask2prefixlen(in_addr_t
);
563 uint8_t mask2prefixlen6(struct sockaddr_in6
*);
564 in_addr_t
prefixlen2mask(uint8_t);
565 struct in6_addr
*prefixlen2mask6(uint8_t);
566 void ldp_applymask(int, union ldpd_addr
*,
567 const union ldpd_addr
*, int);
568 int ldp_addrcmp(int, const union ldpd_addr
*,
569 const union ldpd_addr
*);
570 int ldp_addrisset(int, const union ldpd_addr
*);
571 int ldp_prefixcmp(int, const union ldpd_addr
*,
572 const union ldpd_addr
*, uint8_t);
573 int bad_addr_v4(struct in_addr
);
574 int bad_addr_v6(struct in6_addr
*);
575 int bad_addr(int, union ldpd_addr
*);
576 void embedscope(struct sockaddr_in6
*);
577 void recoverscope(struct sockaddr_in6
*);
578 void addscope(struct sockaddr_in6
*, uint32_t);
579 void clearscope(struct in6_addr
*);
580 struct sockaddr
*addr2sa(int af
, union ldpd_addr
*, uint16_t);
581 void sa2addr(struct sockaddr
*, int *, union ldpd_addr
*,
583 socklen_t
sockaddr_len(struct sockaddr
*);
586 int ldp_write_handler(struct thread
*);
587 void main_imsg_compose_ldpe(int, pid_t
, void *, uint16_t);
588 void main_imsg_compose_lde(int, pid_t
, void *, uint16_t);
589 int main_imsg_compose_both(enum imsg_type
, void *,
591 void imsg_event_add(struct imsgev
*);
592 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
593 pid_t
, int, void *, uint16_t);
594 void evbuf_enqueue(struct evbuf
*, struct ibuf
*);
595 void evbuf_event_add(struct evbuf
*);
596 void evbuf_init(struct evbuf
*, int,
597 int (*)(struct thread
*), void *);
598 void evbuf_clear(struct evbuf
*);
599 struct ldpd_af_conf
*ldp_af_conf_get(struct ldpd_conf
*, int);
600 struct ldpd_af_global
*ldp_af_global_get(struct ldpd_global
*, int);
601 int ldp_is_dual_stack(struct ldpd_conf
*);
602 in_addr_t
ldp_rtr_id_get(struct ldpd_conf
*);
603 int ldp_reload(struct ldpd_conf
*);
604 struct ldpd_conf
*ldp_dup_config(struct ldpd_conf
*);
605 void ldp_clear_config(struct ldpd_conf
*);
606 void merge_config(struct ldpd_conf
*, struct ldpd_conf
*);
607 struct ldpd_conf
*config_new_empty(void);
608 void config_clear(struct ldpd_conf
*);
611 int ldp_create_socket(int, enum socket_type
);
612 void sock_set_nonblock(int);
613 void sock_set_cloexec(int);
614 void sock_set_recvbuf(int);
615 int sock_set_reuse(int, int);
616 int sock_set_bindany(int, int);
617 int sock_set_md5sig(int, int, union ldpd_addr
*, const char *);
618 int sock_set_ipv4_tos(int, int);
619 int sock_set_ipv4_pktinfo(int, int);
620 int sock_set_ipv4_recvdstaddr(int, int);
621 int sock_set_ipv4_recvif(int, int);
622 int sock_set_ipv4_minttl(int, int);
623 int sock_set_ipv4_ucast_ttl(int fd
, int);
624 int sock_set_ipv4_mcast_ttl(int, uint8_t);
625 int sock_set_ipv4_mcast(struct iface
*);
626 int sock_set_ipv4_mcast_loop(int);
627 int sock_set_ipv6_dscp(int, int);
628 int sock_set_ipv6_pktinfo(int, int);
629 int sock_set_ipv6_minhopcount(int, int);
630 int sock_set_ipv6_ucast_hops(int, int);
631 int sock_set_ipv6_mcast_hops(int, int);
632 int sock_set_ipv6_mcast(struct iface
*);
633 int sock_set_ipv6_mcast_loop(int);
636 extern struct thread_master
*master
;
639 void ldp_zebra_init(struct thread_master
*);
643 #define __IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
644 #define __IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
645 #define IN6_IS_ADDR_MC_INTFACELOCAL(a) \
646 (IN6_IS_ADDR_MULTICAST(a) && \
647 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_INTFACELOCAL))
650 #endif /* _LDPD_H_ */