4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include "ldp_debug.h"
27 static int gen_hello_prms_tlv(struct ibuf
*buf
, uint16_t, uint16_t);
28 static int gen_opt4_hello_prms_tlv(struct ibuf
*, uint16_t, uint32_t);
29 static int gen_opt16_hello_prms_tlv(struct ibuf
*, uint16_t, uint8_t *);
30 static int gen_ds_hello_prms_tlv(struct ibuf
*, uint32_t);
31 static int tlv_decode_hello_prms(char *, uint16_t, uint16_t *, uint16_t *);
32 static int tlv_decode_opt_hello_prms(char *, uint16_t, int *, int,
33 union ldpd_addr
*, uint32_t *, uint16_t *);
36 send_hello(enum hello_type type
, struct iface_af
*ia
, struct tnbr
*tnbr
)
40 uint16_t size
, holdtime
= 0, flags
= 0;
48 holdtime
= if_get_hello_holdtime(ia
);
50 fd
= (ldp_af_global_get(&global
, af
))->ldp_disc_socket
;
52 /* multicast destination address */
55 if (!(leconf
->ipv4
.flags
& F_LDPD_AF_NO_GTSM
))
56 flags
|= F_HELLO_GTSM
;
57 dst
.v4
= global
.mcast_addr_v4
;
60 dst
.v6
= global
.mcast_addr_v6
;
63 fatalx("send_hello: unknown af");
68 holdtime
= tnbr_get_hello_holdtime(tnbr
);
69 flags
= F_HELLO_TARGETED
;
70 if ((tnbr
->flags
& F_TNBR_CONFIGURED
) || tnbr
->pw_count
)
71 flags
|= F_HELLO_REQ_TARG
;
72 fd
= (ldp_af_global_get(&global
, af
))->ldp_edisc_socket
;
74 /* unicast destination address */
78 fatalx("send_hello: unknown hello type");
81 /* calculate message size */
82 size
= LDP_HDR_SIZE
+ LDP_MSG_SIZE
+ sizeof(struct hello_prms_tlv
);
85 size
+= sizeof(struct hello_prms_opt4_tlv
);
88 size
+= sizeof(struct hello_prms_opt16_tlv
);
91 fatalx("send_hello: unknown af");
93 size
+= sizeof(struct hello_prms_opt4_tlv
);
94 if (ldp_is_dual_stack(leconf
))
95 size
+= sizeof(struct hello_prms_opt4_tlv
);
97 /* generate message */
98 if ((buf
= ibuf_open(size
)) == NULL
)
101 err
|= gen_ldp_hdr(buf
, size
);
102 size
-= LDP_HDR_SIZE
;
103 err
|= gen_msg_hdr(buf
, MSG_TYPE_HELLO
, size
);
104 err
|= gen_hello_prms_tlv(buf
, holdtime
, flags
);
107 * RFC 7552 - Section 6.1:
108 * "An LSR MUST include only the transport address whose address
109 * family is the same as that of the IP packet carrying the Hello
114 err
|= gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_IPV4TRANSADDR
,
115 leconf
->ipv4
.trans_addr
.v4
.s_addr
);
118 err
|= gen_opt16_hello_prms_tlv(buf
, TLV_TYPE_IPV6TRANSADDR
,
119 leconf
->ipv6
.trans_addr
.v6
.s6_addr
);
122 fatalx("send_hello: unknown af");
125 err
|= gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_CONFIG
,
126 htonl(global
.conf_seqnum
));
129 * RFC 7552 - Section 6.1.1:
130 * "A Dual-stack LSR (i.e., an LSR supporting Dual-stack LDP for a peer)
131 * MUST include the Dual-Stack capability TLV in all of its LDP Hellos".
133 if (ldp_is_dual_stack(leconf
))
134 err
|= gen_ds_hello_prms_tlv(buf
, leconf
->trans_pref
);
143 debug_hello_send("iface %s (%s) holdtime %u", ia
->iface
->name
,
144 af_name(ia
->af
), holdtime
);
147 debug_hello_send("targeted-neighbor %s (%s) holdtime %u",
148 log_addr(tnbr
->af
, &tnbr
->addr
), af_name(tnbr
->af
),
152 fatalx("send_hello: unknown hello type");
155 send_packet(fd
, af
, &dst
, ia
, buf
->buf
, buf
->wpos
);
162 recv_hello(struct in_addr lsr_id
, struct ldp_msg
*msg
, int af
,
163 union ldpd_addr
*src
, struct iface
*iface
, int multicast
, char *buf
,
166 struct adj
*adj
= NULL
;
167 struct nbr
*nbr
, *nbrt
;
168 uint16_t holdtime
= 0, flags
= 0;
171 union ldpd_addr trans_addr
;
172 ifindex_t scope_id
= 0;
173 uint32_t conf_seqnum
;
176 struct hello_source source
;
177 struct iface_af
*ia
= NULL
;
178 struct tnbr
*tnbr
= NULL
;
180 r
= tlv_decode_hello_prms(buf
, len
, &holdtime
, &flags
);
182 log_debug("%s: lsr-id %s: failed to decode params", __func__
,
187 if (holdtime
!= 0 && holdtime
< MIN_HOLDTIME
) {
188 log_debug("%s: lsr-id %s: invalid hello holdtime (%u)",
189 __func__
, inet_ntoa(lsr_id
), holdtime
);
192 if (multicast
&& (flags
& F_HELLO_TARGETED
)) {
193 log_debug("%s: lsr-id %s: multicast targeted hello", __func__
,
197 if (!multicast
&& !((flags
& F_HELLO_TARGETED
))) {
198 log_debug("%s: lsr-id %s: unicast link hello", __func__
,
205 r
= tlv_decode_opt_hello_prms(buf
, len
, &tlvs_rcvd
, af
, &trans_addr
,
206 &conf_seqnum
, &trans_pref
);
208 log_debug("%s: lsr-id %s: failed to decode optional params",
209 __func__
, inet_ntoa(lsr_id
));
213 log_debug("%s: lsr-id %s: unexpected data in message",
214 __func__
, inet_ntoa(lsr_id
));
217 ds_tlv
= (tlvs_rcvd
& F_HELLO_TLV_RCVD_DS
) ? 1 : 0;
219 /* implicit transport address */
220 if (!(tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
))
222 if (bad_addr(af
, &trans_addr
)) {
223 log_debug("%s: lsr-id %s: invalid transport address %s",
224 __func__
, inet_ntoa(lsr_id
), log_addr(af
, &trans_addr
));
227 if (af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&trans_addr
.v6
)) {
229 * RFC 7552 - Section 6.1:
230 * "An LSR MUST use a global unicast IPv6 address in an IPv6
231 * Transport Address optional object of outgoing targeted
232 * Hellos and check for the same in incoming targeted Hellos
233 * (i.e., MUST discard the targeted Hello if it failed the
236 if (flags
& F_HELLO_TARGETED
) {
237 log_debug("%s: lsr-id %s: invalid targeted hello "
238 "transport address %s", __func__
, inet_ntoa(lsr_id
),
239 log_addr(af
, &trans_addr
));
242 scope_id
= iface
->ifindex
;
245 memset(&source
, 0, sizeof(source
));
246 if (flags
& F_HELLO_TARGETED
) {
248 * RFC 7552 - Section 5.2:
249 * "The link-local IPv6 addresses MUST NOT be used as the
250 * targeted LDP Hello packet's source or destination addresses".
252 if (af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&src
->v6
)) {
253 log_debug("%s: lsr-id %s: targeted hello with "
254 "link-local source address", __func__
,
259 tnbr
= tnbr_find(leconf
, af
, src
);
261 /* remove the dynamic tnbr if the 'R' bit was cleared */
262 if (tnbr
&& (tnbr
->flags
& F_TNBR_DYNAMIC
) &&
263 !((flags
& F_HELLO_REQ_TARG
))) {
264 tnbr
->flags
&= ~F_TNBR_DYNAMIC
;
265 tnbr
= tnbr_check(leconf
, tnbr
);
269 struct ldpd_af_conf
*af_conf
;
271 if (!(flags
& F_HELLO_REQ_TARG
))
273 af_conf
= ldp_af_conf_get(leconf
, af
);
274 if (!(af_conf
->flags
& F_LDPD_AF_THELLO_ACCEPT
))
276 if (ldpe_acl_check(af_conf
->acl_thello_accept_from
, af
,
277 src
, (af
== AF_INET
) ? 32 : 128) != FILTER_PERMIT
)
280 tnbr
= tnbr_new(af
, src
);
281 tnbr
->flags
|= F_TNBR_DYNAMIC
;
283 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
286 source
.type
= HELLO_TARGETED
;
287 source
.target
= tnbr
;
289 ia
= iface_af_get(iface
, af
);
290 source
.type
= HELLO_LINK
;
292 source
.link
.src_addr
= *src
;
295 debug_hello_recv("%s lsr-id %s transport-address %s holdtime %u%s",
296 log_hello_src(&source
), inet_ntoa(lsr_id
), log_addr(af
, &trans_addr
),
297 holdtime
, (ds_tlv
) ? " (dual stack TLV present)" : "");
299 adj
= adj_find(lsr_id
, &source
);
300 if (adj
&& adj
->ds_tlv
!= ds_tlv
) {
302 * Transient condition, ignore packet and wait until adjacency
307 nbr
= nbr_find_ldpid(lsr_id
.s_addr
);
309 /* check dual-stack tlv */
310 if (ds_tlv
&& trans_pref
!= leconf
->trans_pref
) {
312 * RFC 7552 - Section 6.1.1:
313 * "If the Dual-Stack capability TLV is present and the remote
314 * preference does not match the local preference (or does not
315 * get recognized), then the LSR MUST discard the Hello message
317 * If an LDP session was already in place, then the LSR MUST
318 * send a fatal Notification message with status code of
319 * 'Transport Connection Mismatch' and reset the session".
321 log_debug("%s: lsr-id %s: remote transport preference does not "
322 "match the local preference", __func__
, inet_ntoa(lsr_id
));
324 session_shutdown(nbr
, S_TRANS_MISMTCH
, msg
->id
,
327 adj_del(adj
, S_SHUTDOWN
);
332 * Check for noncompliant dual-stack neighbor according to
333 * RFC 7552 section 6.1.1.
335 if (nbr
&& !ds_tlv
) {
338 if (nbr_adj_count(nbr
, AF_INET6
) > 0) {
339 session_shutdown(nbr
, S_DS_NONCMPLNCE
,
345 if (nbr_adj_count(nbr
, AF_INET
) > 0) {
346 session_shutdown(nbr
, S_DS_NONCMPLNCE
,
352 fatalx("recv_hello: unknown af");
357 * Protections against misconfigured networks and buggy implementations.
359 if (nbr
&& nbr
->af
== af
&&
360 (ldp_addrcmp(af
, &nbr
->raddr
, &trans_addr
) ||
361 nbr
->raddr_scope
!= scope_id
)) {
362 log_warnx("%s: lsr-id %s: hello packet advertising a different "
363 "transport address", __func__
, inet_ntoa(lsr_id
));
365 adj_del(adj
, S_SHUTDOWN
);
369 nbrt
= nbr_find_addr(af
, &trans_addr
);
371 log_debug("%s: transport address %s is already being "
372 "used by lsr-id %s", __func__
, log_addr(af
,
373 &trans_addr
), inet_ntoa(nbrt
->id
));
375 adj_del(adj
, S_SHUTDOWN
);
381 adj
= adj_new(lsr_id
, &source
, &trans_addr
);
384 RB_INSERT(nbr_adj_head
, &nbr
->adj_tree
, adj
);
387 adj
->ds_tlv
= ds_tlv
;
390 * If the hello adjacency's address-family doesn't match the local
391 * preference, then an adjacency is still created but we don't attempt
392 * to start an LDP session.
394 if (nbr
== NULL
&& (!ds_tlv
||
395 ((trans_pref
== DUAL_STACK_LDPOV4
&& af
== AF_INET
) ||
396 (trans_pref
== DUAL_STACK_LDPOV6
&& af
== AF_INET6
))))
397 nbr
= nbr_new(lsr_id
, af
, ds_tlv
, &trans_addr
, scope_id
);
399 /* dynamic LDPv4 GTSM negotiation as per RFC 6720 */
401 if (flags
& F_HELLO_GTSM
)
402 nbr
->flags
|= F_NBR_GTSM_NEGOTIATED
;
404 nbr
->flags
&= ~F_NBR_GTSM_NEGOTIATED
;
407 /* update neighbor's configuration sequence number */
408 if (nbr
&& (tlvs_rcvd
& F_HELLO_TLV_RCVD_CONF
)) {
409 if (conf_seqnum
> nbr
->conf_seqnum
&&
410 nbr_pending_idtimer(nbr
))
411 nbr_stop_idtimer(nbr
);
412 nbr
->conf_seqnum
= conf_seqnum
;
415 /* always update the holdtime to properly handle runtime changes */
416 switch (source
.type
) {
419 holdtime
= LINK_DFLT_HOLDTIME
;
421 adj
->holdtime
= min(if_get_hello_holdtime(ia
), holdtime
);
425 holdtime
= TARGETED_DFLT_HOLDTIME
;
427 adj
->holdtime
= min(tnbr_get_hello_holdtime(tnbr
), holdtime
);
429 if (adj
->holdtime
!= INFINITE_HOLDTIME
)
430 adj_start_itimer(adj
);
432 adj_stop_itimer(adj
);
434 if (nbr
&& nbr
->state
== NBR_STA_PRESENT
&& !nbr_pending_idtimer(nbr
) &&
435 nbr_session_active_role(nbr
) && !nbr_pending_connect(nbr
))
436 nbr_establish_connection(nbr
);
440 gen_hello_prms_tlv(struct ibuf
*buf
, uint16_t holdtime
, uint16_t flags
)
442 struct hello_prms_tlv parms
;
444 memset(&parms
, 0, sizeof(parms
));
445 parms
.type
= htons(TLV_TYPE_COMMONHELLO
);
446 parms
.length
= htons(sizeof(parms
.holdtime
) + sizeof(parms
.flags
));
447 parms
.holdtime
= htons(holdtime
);
448 parms
.flags
= htons(flags
);
450 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
454 gen_opt4_hello_prms_tlv(struct ibuf
*buf
, uint16_t type
, uint32_t value
)
456 struct hello_prms_opt4_tlv parms
;
458 memset(&parms
, 0, sizeof(parms
));
459 parms
.type
= htons(type
);
460 parms
.length
= htons(sizeof(parms
.value
));
463 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
467 gen_opt16_hello_prms_tlv(struct ibuf
*buf
, uint16_t type
, uint8_t *value
)
469 struct hello_prms_opt16_tlv parms
;
471 memset(&parms
, 0, sizeof(parms
));
472 parms
.type
= htons(type
);
473 parms
.length
= htons(sizeof(parms
.value
));
474 memcpy(&parms
.value
, value
, sizeof(parms
.value
));
476 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
480 gen_ds_hello_prms_tlv(struct ibuf
*buf
, uint32_t value
)
482 if (leconf
->flags
& F_LDPD_DS_CISCO_INTEROP
)
483 value
= htonl(value
);
485 value
= htonl(value
<< 28);
487 return (gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_DUALSTACK
, value
));
491 tlv_decode_hello_prms(char *buf
, uint16_t len
, uint16_t *holdtime
,
494 struct hello_prms_tlv tlv
;
496 if (len
< sizeof(tlv
))
498 memcpy(&tlv
, buf
, sizeof(tlv
));
500 if (tlv
.type
!= htons(TLV_TYPE_COMMONHELLO
))
502 if (ntohs(tlv
.length
) != sizeof(tlv
) - TLV_HDR_SIZE
)
505 *holdtime
= ntohs(tlv
.holdtime
);
506 *flags
= ntohs(tlv
.flags
);
508 return (sizeof(tlv
));
512 tlv_decode_opt_hello_prms(char *buf
, uint16_t len
, int *tlvs_rcvd
, int af
,
513 union ldpd_addr
*addr
, uint32_t *conf_number
, uint16_t *trans_pref
)
520 memset(addr
, 0, sizeof(*addr
));
525 * RFC 7552 - Section 6.1:
526 * "An LSR SHOULD accept the Hello message that contains both IPv4 and
527 * IPv6 Transport Address optional objects but MUST use only the
528 * transport address whose address family is the same as that of the
529 * IP packet carrying the Hello message. An LSR SHOULD accept only
530 * the first Transport Address optional object for a given address
531 * family in the received Hello message and ignore the rest if the
532 * LSR receives more than one Transport Address optional object for a
533 * given address family".
535 while (len
>= sizeof(tlv
)) {
536 memcpy(&tlv
, buf
, TLV_HDR_SIZE
);
537 tlv_len
= ntohs(tlv
.length
);
538 if (tlv_len
+ TLV_HDR_SIZE
> len
)
542 total
+= TLV_HDR_SIZE
;
544 switch (ntohs(tlv
.type
)) {
545 case TLV_TYPE_IPV4TRANSADDR
:
546 if (tlv_len
!= sizeof(addr
->v4
))
550 if (*tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
)
552 memcpy(&addr
->v4
, buf
, sizeof(addr
->v4
));
553 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_ADDR
;
555 case TLV_TYPE_IPV6TRANSADDR
:
556 if (tlv_len
!= sizeof(addr
->v6
))
560 if (*tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
)
562 memcpy(&addr
->v6
, buf
, sizeof(addr
->v6
));
563 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_ADDR
;
565 case TLV_TYPE_CONFIG
:
566 if (tlv_len
!= sizeof(uint32_t))
568 memcpy(conf_number
, buf
, sizeof(uint32_t));
569 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_CONF
;
571 case TLV_TYPE_DUALSTACK
:
572 if (tlv_len
!= sizeof(uint32_t))
575 * RFC 7552 - Section 6.1:
576 * "A Single-stack LSR does not need to use the
577 * Dual-Stack capability in Hello messages and SHOULD
578 * ignore this capability if received".
580 if (!ldp_is_dual_stack(leconf
))
582 /* Shame on you, Cisco! */
583 if (leconf
->flags
& F_LDPD_DS_CISCO_INTEROP
) {
584 memcpy(trans_pref
, buf
+ sizeof(uint16_t),
586 *trans_pref
= ntohs(*trans_pref
);
588 memcpy(trans_pref
, buf
, sizeof(uint16_t));
589 *trans_pref
= ntohs(*trans_pref
) >> 12;
591 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_DS
;
594 /* if unknown flag set, ignore TLV */
595 if (!(ntohs(tlv
.type
) & UNKNOWN_FLAG
))