1 // SPDX-License-Identifier: ISC
5 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
6 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
7 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
18 static struct iface
*disc_find_iface(unsigned int, int, union ldpd_addr
*);
19 static void session_read(struct event
*thread
);
20 static void session_write(struct event
*thread
);
21 static ssize_t
session_get_pdu(struct ibuf_read
*, char **);
22 static void tcp_close(struct tcp_conn
*);
23 static struct pending_conn
*pending_conn_new(int, int, union ldpd_addr
*);
24 static void pending_conn_timeout(struct event
*thread
);
27 gen_ldp_hdr(struct ibuf
*buf
, uint16_t size
)
29 struct ldp_hdr ldp_hdr
;
31 memset(&ldp_hdr
, 0, sizeof(ldp_hdr
));
32 ldp_hdr
.version
= htons(LDP_VERSION
);
33 /* exclude the 'Version' and 'PDU Length' fields from the total */
34 ldp_hdr
.length
= htons(size
- LDP_HDR_DEAD_LEN
);
35 ldp_hdr
.lsr_id
= ldp_rtr_id_get(leconf
);
36 ldp_hdr
.lspace_id
= 0;
38 return (ibuf_add(buf
, &ldp_hdr
, LDP_HDR_SIZE
));
42 gen_msg_hdr(struct ibuf
*buf
, uint16_t type
, uint16_t size
)
44 static int msgcnt
= 0;
47 memset(&msg
, 0, sizeof(msg
));
48 msg
.type
= htons(type
);
49 /* exclude the 'Type' and 'Length' fields from the total */
50 msg
.length
= htons(size
- LDP_MSG_DEAD_LEN
);
51 msg
.id
= htonl(++msgcnt
);
53 return (ibuf_add(buf
, &msg
, sizeof(msg
)));
58 send_packet(int fd
, int af
, union ldpd_addr
*dst
, struct iface_af
*ia
,
59 void *pkt
, size_t len
)
65 if (ia
&& IN_MULTICAST(ntohl(dst
->v4
.s_addr
))) {
66 /* set outgoing interface for multicast traffic */
67 if (sock_set_ipv4_mcast(ia
->iface
) == -1) {
68 log_debug("%s: error setting multicast interface, %s", __func__
, ia
->iface
->name
);
74 if (ia
&& IN6_IS_ADDR_MULTICAST(&dst
->v6
)) {
75 /* set outgoing interface for multicast traffic */
76 if (sock_set_ipv6_mcast(ia
->iface
) == -1) {
77 log_debug("%s: error setting multicast interface, %s", __func__
, ia
->iface
->name
);
83 fatalx("send_packet: unknown af");
86 addr2sa(af
, dst
, LDP_PORT
, &su
);
87 if (sendto(fd
, pkt
, len
, 0, &su
.sa
, sockaddr_len(&su
.sa
)) == -1) {
88 log_warn("%s: error sending packet to %s", __func__
,
89 log_sockaddr(&su
.sa
));
96 /* Discovery functions */
97 void disc_recv_packet(struct event
*thread
)
99 int fd
= EVENT_FD(thread
);
100 struct event
**threadp
= EVENT_ARG(thread
);
104 #ifdef HAVE_STRUCT_SOCKADDR_DL
105 char buf
[CMSG_SPACE(sizeof(struct sockaddr_dl
))];
107 char buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
111 struct sockaddr_storage from
;
115 struct cmsghdr
*cmsg
;
121 unsigned int ifindex
= 0;
122 struct iface
*iface
= NULL
;
124 struct ldp_hdr ldp_hdr
;
128 struct in_addr lsr_id
;
130 /* reschedule read */
131 event_add_read(master
, disc_recv_packet
, threadp
, fd
, threadp
);
134 memset(&m
, 0, sizeof(m
));
135 iov
.iov_base
= buf
= pkt_ptr
;
136 iov
.iov_len
= IBUF_READ_SIZE
;
138 m
.msg_namelen
= sizeof(from
);
141 m
.msg_control
= &cmsgbuf
.buf
;
142 m
.msg_controllen
= sizeof(cmsgbuf
.buf
);
144 if ((r
= recvmsg(fd
, &m
, 0)) == -1) {
145 if (errno
!= EAGAIN
&& errno
!= EINTR
)
146 log_debug("%s: read error: %s", __func__
, strerror(errno
));
150 sa2addr((struct sockaddr
*)&from
, &af
, &src
, NULL
);
152 multicast
= (m
.msg_flags
& MSG_MCAST
) ? 1 : 0;
155 for (cmsg
= CMSG_FIRSTHDR(&m
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&m
, cmsg
)) {
156 #if defined(HAVE_IP_PKTINFO)
158 cmsg
->cmsg_level
== IPPROTO_IP
&&
159 cmsg
->cmsg_type
== IP_PKTINFO
) {
160 struct in_pktinfo
*pktinfo
;
162 pktinfo
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
163 if (IN_MULTICAST(ntohl(pktinfo
->ipi_addr
.s_addr
)))
167 #elif defined(HAVE_IP_RECVDSTADDR)
169 cmsg
->cmsg_level
== IPPROTO_IP
&&
170 cmsg
->cmsg_type
== IP_RECVDSTADDR
) {
171 struct in_addr
*addr
;
173 addr
= (struct in_addr
*)CMSG_DATA(cmsg
);
174 if (IN_MULTICAST(ntohl(addr
->s_addr
)))
179 #error "Unsupported socket API"
181 if (af
== AF_INET6
&&
182 cmsg
->cmsg_level
== IPPROTO_IPV6
&&
183 cmsg
->cmsg_type
== IPV6_PKTINFO
) {
184 struct in6_pktinfo
*pktinfo
;
186 pktinfo
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
187 if (IN6_IS_ADDR_MULTICAST(&pktinfo
->ipi6_addr
))
192 #endif /* MSG_MCAST */
193 if (bad_addr(af
, &src
)) {
194 log_debug("%s: invalid source address: %s", __func__
, log_addr(af
, &src
));
197 ifindex
= getsockopt_ifindex(af
, &m
);
199 /* find a matching interface */
201 iface
= disc_find_iface(ifindex
, af
, &src
);
206 /* check packet size */
208 if (len
< (LDP_HDR_SIZE
+ LDP_MSG_SIZE
) || len
> LDP_MAX_LEN
) {
209 log_debug("%s: bad packet size, source %s", __func__
, log_addr(af
, &src
));
213 /* LDP header sanity checks */
214 memcpy(&ldp_hdr
, buf
, sizeof(ldp_hdr
));
215 if (ntohs(ldp_hdr
.version
) != LDP_VERSION
) {
216 log_debug("%s: invalid LDP version %d, source %s", __func__
,
217 ntohs(ldp_hdr
.version
), log_addr(af
, &src
));
220 if (ntohs(ldp_hdr
.lspace_id
) != 0) {
221 log_debug("%s: invalid label space %u, source %s", __func__
,
222 ntohs(ldp_hdr
.lspace_id
), log_addr(af
, &src
));
225 /* check "PDU Length" field */
226 pdu_len
= ntohs(ldp_hdr
.length
);
227 if ((pdu_len
< (LDP_HDR_PDU_LEN
+ LDP_MSG_SIZE
)) ||
228 (pdu_len
> (len
- LDP_HDR_DEAD_LEN
))) {
229 log_debug("%s: invalid LDP packet length %u, source %s",
230 __func__
, ntohs(ldp_hdr
.length
), log_addr(af
, &src
));
236 lsr_id
.s_addr
= ldp_hdr
.lsr_id
;
239 * For UDP, we process only the first message of each packet. This does
240 * not impose any restrictions since LDP uses UDP only for sending Hello
243 memcpy(&msg
, buf
, sizeof(msg
));
245 /* check "Message Length" field */
246 msg_len
= ntohs(msg
.length
);
247 if (msg_len
< LDP_MSG_LEN
|| ((msg_len
+ LDP_MSG_DEAD_LEN
) > pdu_len
)) {
248 log_debug("%s: invalid LDP message length %u, source %s",
249 __func__
, ntohs(msg
.length
), log_addr(af
, &src
));
255 /* switch LDP packet type */
256 switch (ntohs(msg
.type
)) {
258 recv_hello(lsr_id
, &msg
, af
, &src
, iface
, multicast
, buf
, len
);
261 log_debug("%s: unknown LDP packet type, source %s", __func__
,
266 static struct iface
*
267 disc_find_iface(unsigned int ifindex
, int af
, union ldpd_addr
*src
)
272 iface
= if_lookup(leconf
, ifindex
);
276 ia
= iface_af_get(iface
, af
);
281 * RFC 7552 - Section 5.1:
282 * "Link-local IPv6 address MUST be used as the source IP address in
283 * IPv6 LDP Link Hellos".
285 if (af
== AF_INET6
&& !IN6_IS_ADDR_LINKLOCAL(&src
->v6
))
291 void session_accept(struct event
*thread
)
293 int fd
= EVENT_FD(thread
);
294 struct sockaddr_storage src
;
295 socklen_t len
= sizeof(src
);
298 union ldpd_addr addr
;
300 struct pending_conn
*pconn
;
302 newfd
= accept(fd
, (struct sockaddr
*)&src
, &len
);
305 * Pause accept if we are out of file descriptors, or
306 * libevent will haunt us here too.
308 if (errno
== ENFILE
|| errno
== EMFILE
) {
310 } else if (errno
!= EWOULDBLOCK
&& errno
!= EINTR
&& errno
!= ECONNABORTED
)
311 log_debug("%s: accept error: %s", __func__
, strerror(errno
));
314 sock_set_nonblock(newfd
);
316 sa2addr((struct sockaddr
*)&src
, &af
, &addr
, NULL
);
319 * Since we don't support label spaces, we can identify this neighbor
320 * just by its source address. This way we don't need to wait for its
321 * Initialization message to know who we are talking to.
323 nbr
= nbr_find_addr(af
, &addr
);
326 * According to RFC 5036, we would need to send a No Hello
327 * Error Notification message and close this TCP connection
328 * right now. But doing so would trigger the backoff exponential
329 * timer in the remote peer, which would considerably slow down
330 * the session establishment process. The trick here is to wait
331 * five seconds before sending the Notification Message. There's
332 * a good chance that the remote peer will send us a Hello
333 * message within this interval, so it's worth waiting before
334 * taking a more drastic measure.
336 pconn
= pending_conn_find(af
, &addr
);
340 pending_conn_new(newfd
, af
, &addr
);
343 /* protection against buggy implementations */
344 if (nbr_session_active_role(nbr
)) {
348 if (nbr
->state
!= NBR_STA_PRESENT
) {
349 log_debug("%s: lsr-id %pI4: rejecting additional transport connection", __func__
, &nbr
->id
);
354 session_accept_nbr(nbr
, newfd
);
358 session_accept_nbr(struct nbr
*nbr
, int fd
)
361 struct nbr_params
*nbrp
;
365 nbrp
= nbr_params_find(leconf
, nbr
->id
);
366 if (nbr_gtsm_check(fd
, nbr
, nbrp
)) {
371 if (nbrp
&& nbrp
->auth
.method
== AUTH_MD5SIG
) {
372 if (sysdep
.no_pfkey
|| sysdep
.no_md5sig
) {
373 log_warnx("md5sig configured but not available");
379 if (getsockopt(fd
, IPPROTO_TCP
, TCP_MD5SIG
, &opt
, &len
) == -1)
380 fatal("getsockopt TCP_MD5SIG");
381 if (!opt
) { /* non-md5'd connection! */
382 log_warnx("connection attempt without md5 signature");
389 nbr
->tcp
= tcp_new(fd
, nbr
);
390 nbr_fsm(nbr
, NBR_EVT_MATCH_ADJ
);
393 static void session_read(struct event
*thread
)
395 int fd
= EVENT_FD(thread
);
396 struct nbr
*nbr
= EVENT_ARG(thread
);
397 struct tcp_conn
*tcp
= nbr
->tcp
;
398 struct ldp_hdr
*ldp_hdr
;
400 char *buf
= NULL
, *pdu
;
402 uint16_t pdu_len
, msg_len
, msg_size
, max_pdu_len
;
405 event_add_read(master
, session_read
, nbr
, fd
, &tcp
->rev
);
407 if ((n
= read(fd
, tcp
->rbuf
->buf
+ tcp
->rbuf
->wpos
,
408 sizeof(tcp
->rbuf
->buf
) - tcp
->rbuf
->wpos
)) == -1) {
409 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
410 log_warn("%s: read error", __func__
);
411 nbr_fsm(nbr
, NBR_EVT_CLOSE_SESSION
);
418 /* connection closed */
419 log_debug("%s: connection closed by remote end", __func__
);
420 nbr_fsm(nbr
, NBR_EVT_CLOSE_SESSION
);
423 tcp
->rbuf
->wpos
+= n
;
425 while ((len
= session_get_pdu(tcp
->rbuf
, &buf
)) > 0) {
427 ldp_hdr
= (struct ldp_hdr
*)pdu
;
428 if (ntohs(ldp_hdr
->version
) != LDP_VERSION
) {
429 session_shutdown(nbr
, S_BAD_PROTO_VER
, 0, 0);
434 pdu_len
= ntohs(ldp_hdr
->length
);
436 * RFC 5036 - Section 3.5.3:
437 * "Prior to completion of the negotiation, the maximum
438 * allowable length is 4096 bytes".
440 if (nbr
->state
== NBR_STA_OPER
)
441 max_pdu_len
= nbr
->max_pdu_len
;
443 max_pdu_len
= LDP_MAX_LEN
;
444 if (pdu_len
< (LDP_HDR_PDU_LEN
+ LDP_MSG_SIZE
) || pdu_len
> max_pdu_len
) {
445 session_shutdown(nbr
, S_BAD_PDU_LEN
, 0, 0);
449 pdu_len
-= LDP_HDR_PDU_LEN
;
450 if (ldp_hdr
->lsr_id
!= nbr
->id
.s_addr
|| ldp_hdr
->lspace_id
!= 0) {
451 session_shutdown(nbr
, S_BAD_LDP_ID
, 0, 0);
458 nbr_fsm(nbr
, NBR_EVT_PDU_RCVD
);
460 while (len
>= LDP_MSG_SIZE
) {
463 msg
= (struct ldp_msg
*)pdu
;
464 type
= ntohs(msg
->type
);
465 msg_len
= ntohs(msg
->length
);
466 if (msg_len
< LDP_MSG_LEN
|| (msg_len
+ LDP_MSG_DEAD_LEN
) > pdu_len
) {
467 session_shutdown(nbr
, S_BAD_MSG_LEN
, msg
->id
, msg
->type
);
471 msg_size
= msg_len
+ LDP_MSG_DEAD_LEN
;
474 /* check for error conditions earlier */
477 if ((nbr
->state
!= NBR_STA_INITIAL
) &&
478 (nbr
->state
!= NBR_STA_OPENSENT
)) {
479 session_shutdown(nbr
, S_SHUTDOWN
, msg
->id
, msg
->type
);
484 case MSG_TYPE_KEEPALIVE
:
485 if ((nbr
->state
== NBR_STA_INITIAL
) ||
486 (nbr
->state
== NBR_STA_OPENSENT
)) {
487 session_shutdown(nbr
, S_SHUTDOWN
, msg
->id
, msg
->type
);
492 case MSG_TYPE_NOTIFICATION
:
495 if (nbr
->state
!= NBR_STA_OPER
) {
496 session_shutdown(nbr
, S_SHUTDOWN
, msg
->id
, msg
->type
);
503 /* switch LDP packet type */
505 case MSG_TYPE_NOTIFICATION
:
506 ret
= recv_notification(nbr
, pdu
, msg_size
);
509 ret
= recv_init(nbr
, pdu
, msg_size
);
511 case MSG_TYPE_KEEPALIVE
:
512 ret
= recv_keepalive(nbr
, pdu
, msg_size
);
514 case MSG_TYPE_CAPABILITY
:
515 ret
= recv_capability(nbr
, pdu
, msg_size
);
518 case MSG_TYPE_ADDRWITHDRAW
:
519 ret
= recv_address(nbr
, pdu
, msg_size
);
521 case MSG_TYPE_LABELMAPPING
:
522 case MSG_TYPE_LABELREQUEST
:
523 case MSG_TYPE_LABELWITHDRAW
:
524 case MSG_TYPE_LABELRELEASE
:
525 case MSG_TYPE_LABELABORTREQ
:
526 ret
= recv_labelmessage(nbr
, pdu
, msg_size
, type
);
529 log_debug("%s: unknown LDP message from nbr %pI4",
531 if (!(ntohs(msg
->type
) & UNKNOWN_FLAG
)) {
532 nbr
->stats
.unknown_msg
++;
533 send_notification(nbr
->tcp
, S_UNKNOWN_MSG
, msg
->id
, msg
->type
);
535 /* ignore the message */
541 /* parser failed, giving up */
546 /* no errors - update per neighbor message counters */
548 case MSG_TYPE_NOTIFICATION
:
549 nbr
->stats
.notif_rcvd
++;
551 case MSG_TYPE_KEEPALIVE
:
552 nbr
->stats
.kalive_rcvd
++;
554 case MSG_TYPE_CAPABILITY
:
555 nbr
->stats
.capability_rcvd
++;
558 nbr
->stats
.addr_rcvd
++;
560 case MSG_TYPE_ADDRWITHDRAW
:
561 nbr
->stats
.addrwdraw_rcvd
++;
563 case MSG_TYPE_LABELMAPPING
:
564 nbr
->stats
.labelmap_rcvd
++;
566 case MSG_TYPE_LABELREQUEST
:
567 nbr
->stats
.labelreq_rcvd
++;
569 case MSG_TYPE_LABELWITHDRAW
:
570 nbr
->stats
.labelwdraw_rcvd
++;
572 case MSG_TYPE_LABELRELEASE
:
573 nbr
->stats
.labelrel_rcvd
++;
575 case MSG_TYPE_LABELABORTREQ
:
576 nbr
->stats
.labelabreq_rcvd
++;
582 /* Analyse the next message */
589 session_shutdown(nbr
, S_BAD_PDU_LEN
, 0, 0);
594 /* shouldn't happen, session_get_pdu should be > 0 if buf was
595 * allocated - but let's get rid of the SA warning.
600 static void session_write(struct event
*thread
)
602 struct tcp_conn
*tcp
= EVENT_ARG(thread
);
603 struct nbr
*nbr
= tcp
->nbr
;
607 if (msgbuf_write(&tcp
->wbuf
.wbuf
) <= 0)
608 if (errno
!= EAGAIN
&& nbr
)
609 nbr_fsm(nbr
, NBR_EVT_CLOSE_SESSION
);
611 if (nbr
== NULL
&& !tcp
->wbuf
.wbuf
.queued
) {
613 * We are done sending the notification message, now we can
620 evbuf_event_add(&tcp
->wbuf
);
624 session_shutdown(struct nbr
*nbr
, uint32_t status
, uint32_t msg_id
,
627 switch (nbr
->state
) {
628 case NBR_STA_PRESENT
:
629 if (nbr_pending_connect(nbr
))
630 EVENT_OFF(nbr
->ev_connect
);
632 case NBR_STA_INITIAL
:
633 case NBR_STA_OPENREC
:
634 case NBR_STA_OPENSENT
:
635 /* update SNMP session counters during initialization */
636 leconf
->stats
.session_attempts
++;
637 send_notification(nbr
->tcp
, status
, msg_id
, msg_type
);
639 nbr_fsm(nbr
, NBR_EVT_CLOSE_SESSION
);
642 send_notification(nbr
->tcp
, status
, msg_id
, msg_type
);
644 nbr_fsm(nbr
, NBR_EVT_CLOSE_SESSION
);
647 fatalx("session_shutdown: unknown neighbor state");
652 session_close(struct nbr
*nbr
)
654 log_debug("%s: closing session with lsr-id %pI4", __func__
, &nbr
->id
);
656 ldp_sync_fsm_nbr_event(nbr
, LDP_SYNC_EVT_SESSION_CLOSE
);
659 nbr_stop_ktimer(nbr
);
660 nbr_stop_ktimeout(nbr
);
661 nbr_stop_itimeout(nbr
);
665 session_get_pdu(struct ibuf_read
*r
, char **b
)
668 size_t av
, dlen
, left
;
674 memcpy(&l
, r
->buf
, sizeof(l
));
675 dlen
= ntohs(l
.length
) + LDP_HDR_DEAD_LEN
;
679 if ((*b
= malloc(dlen
)) == NULL
)
682 memcpy(*b
, r
->buf
, dlen
);
685 memmove(r
->buf
, r
->buf
+ dlen
, left
);
694 tcp_new(int fd
, struct nbr
*nbr
)
696 struct tcp_conn
*tcp
;
697 struct sockaddr_storage ss
;
698 socklen_t len
= sizeof(ss
);
700 if ((tcp
= calloc(1, sizeof(*tcp
))) == NULL
)
704 evbuf_init(&tcp
->wbuf
, tcp
->fd
, session_write
, tcp
);
707 if ((tcp
->rbuf
= calloc(1, sizeof(struct ibuf_read
))) == NULL
)
710 event_add_read(master
, session_read
, nbr
, tcp
->fd
, &tcp
->rev
);
714 if (getsockname(fd
, (struct sockaddr
*)&ss
, &len
) != 0)
715 log_warn("%s: getsockname", __func__
);
717 sa2addr((struct sockaddr
*)&ss
, NULL
, NULL
, &tcp
->lport
);
718 if (getpeername(fd
, (struct sockaddr
*)&ss
, &len
) != 0)
719 log_warn("%s: getpeername", __func__
);
721 sa2addr((struct sockaddr
*)&ss
, NULL
, NULL
, &tcp
->rport
);
727 tcp_close(struct tcp_conn
*tcp
)
729 /* try to flush write buffer */
730 msgbuf_write(&tcp
->wbuf
.wbuf
);
731 evbuf_clear(&tcp
->wbuf
);
736 tcp
->nbr
->tcp
= NULL
;
744 static struct pending_conn
*
745 pending_conn_new(int fd
, int af
, union ldpd_addr
*addr
)
747 struct pending_conn
*pconn
;
749 if ((pconn
= calloc(1, sizeof(*pconn
))) == NULL
)
755 TAILQ_INSERT_TAIL(&global
.pending_conns
, pconn
, entry
);
756 pconn
->ev_timeout
= NULL
;
757 event_add_timer(master
, pending_conn_timeout
, pconn
,
758 PENDING_CONN_TIMEOUT
, &pconn
->ev_timeout
);
764 pending_conn_del(struct pending_conn
*pconn
)
766 EVENT_OFF(pconn
->ev_timeout
);
767 TAILQ_REMOVE(&global
.pending_conns
, pconn
, entry
);
771 struct pending_conn
*
772 pending_conn_find(int af
, union ldpd_addr
*addr
)
774 struct pending_conn
*pconn
;
776 TAILQ_FOREACH(pconn
, &global
.pending_conns
, entry
)
777 if (af
== pconn
->af
&& ldp_addrcmp(af
, addr
, &pconn
->addr
) == 0)
783 static void pending_conn_timeout(struct event
*thread
)
785 struct pending_conn
*pconn
= EVENT_ARG(thread
);
786 struct tcp_conn
*tcp
;
788 pconn
->ev_timeout
= NULL
;
790 log_debug("%s: no adjacency with remote end: %s", __func__
,
791 log_addr(pconn
->af
, &pconn
->addr
));
794 * Create a write buffer detached from any neighbor to send a
795 * notification message reliably.
797 tcp
= tcp_new(pconn
->fd
, NULL
);
798 send_notification(tcp
, S_NO_HELLO
, 0, 0);
799 msgbuf_write(&tcp
->wbuf
.wbuf
);
801 pending_conn_del(pconn
);