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
72 flags
|= F_HELLO_REQ_TARG
;
73 fd
= (ldp_af_global_get(&global
, af
))->ldp_edisc_socket
;
75 /* unicast destination address */
79 fatalx("send_hello: unknown hello type");
82 /* calculate message size */
83 size
= LDP_HDR_SIZE
+ LDP_MSG_SIZE
+ sizeof(struct hello_prms_tlv
);
86 size
+= sizeof(struct hello_prms_opt4_tlv
);
89 size
+= sizeof(struct hello_prms_opt16_tlv
);
92 fatalx("send_hello: unknown af");
94 size
+= sizeof(struct hello_prms_opt4_tlv
);
95 if (ldp_is_dual_stack(leconf
))
96 size
+= sizeof(struct hello_prms_opt4_tlv
);
98 /* generate message */
99 if ((buf
= ibuf_open(size
)) == NULL
)
102 err
|= gen_ldp_hdr(buf
, size
);
103 size
-= LDP_HDR_SIZE
;
104 err
|= gen_msg_hdr(buf
, MSG_TYPE_HELLO
, size
);
105 err
|= gen_hello_prms_tlv(buf
, holdtime
, flags
);
108 * RFC 7552 - Section 6.1:
109 * "An LSR MUST include only the transport address whose address
110 * family is the same as that of the IP packet carrying the Hello
115 err
|= gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_IPV4TRANSADDR
,
116 leconf
->ipv4
.trans_addr
.v4
.s_addr
);
119 err
|= gen_opt16_hello_prms_tlv(buf
, TLV_TYPE_IPV6TRANSADDR
,
120 leconf
->ipv6
.trans_addr
.v6
.s6_addr
);
123 fatalx("send_hello: unknown af");
126 err
|= gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_CONFIG
,
127 htonl(global
.conf_seqnum
));
130 * RFC 7552 - Section 6.1.1:
131 * "A Dual-stack LSR (i.e., an LSR supporting Dual-stack LDP for a peer)
132 * MUST include the Dual-Stack capability TLV in all of its LDP Hellos".
134 if (ldp_is_dual_stack(leconf
))
135 err
|= gen_ds_hello_prms_tlv(buf
, leconf
->trans_pref
);
144 debug_hello_send("iface %s (%s) holdtime %u", ia
->iface
->name
,
145 af_name(ia
->af
), holdtime
);
148 debug_hello_send("targeted-neighbor %s (%s) holdtime %u",
149 log_addr(tnbr
->af
, &tnbr
->addr
), af_name(tnbr
->af
),
153 fatalx("send_hello: unknown hello type");
156 send_packet(fd
, af
, &dst
, ia
, buf
->buf
, buf
->wpos
);
163 recv_hello(struct in_addr lsr_id
, struct ldp_msg
*msg
, int af
,
164 union ldpd_addr
*src
, struct iface
*iface
, int multicast
, char *buf
,
167 struct adj
*adj
= NULL
;
168 struct nbr
*nbr
, *nbrt
;
169 uint16_t holdtime
= 0, flags
= 0;
172 union ldpd_addr trans_addr
;
173 ifindex_t scope_id
= 0;
174 uint32_t conf_seqnum
;
177 struct hello_source source
;
178 struct iface_af
*ia
= NULL
;
179 struct tnbr
*tnbr
= NULL
;
181 r
= tlv_decode_hello_prms(buf
, len
, &holdtime
, &flags
);
183 log_debug("%s: lsr-id %pI4: failed to decode params", __func__
,
188 if (holdtime
!= 0 && holdtime
< MIN_HOLDTIME
) {
189 log_debug("%s: lsr-id %pI4: invalid hello holdtime (%u)",
190 __func__
, &lsr_id
, holdtime
);
193 if (multicast
&& (flags
& F_HELLO_TARGETED
)) {
194 log_debug("%s: lsr-id %pI4: multicast targeted hello", __func__
,
198 if (!multicast
&& !((flags
& F_HELLO_TARGETED
))) {
199 log_debug("%s: lsr-id %pI4: unicast link hello", __func__
,
206 r
= tlv_decode_opt_hello_prms(buf
, len
, &tlvs_rcvd
, af
, &trans_addr
,
207 &conf_seqnum
, &trans_pref
);
209 log_debug("%s: lsr-id %pI4: failed to decode optional params",
214 log_debug("%s: lsr-id %pI4: unexpected data in message",
218 ds_tlv
= (tlvs_rcvd
& F_HELLO_TLV_RCVD_DS
) ? 1 : 0;
220 /* implicit transport address */
221 if (!(tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
))
223 if (bad_addr(af
, &trans_addr
)) {
224 log_debug("%s: lsr-id %pI4: invalid transport address %s",
225 __func__
, &lsr_id
, log_addr(af
, &trans_addr
));
228 if (af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&trans_addr
.v6
)) {
230 * RFC 7552 - Section 6.1:
231 * "An LSR MUST use a global unicast IPv6 address in an IPv6
232 * Transport Address optional object of outgoing targeted
233 * Hellos and check for the same in incoming targeted Hellos
234 * (i.e., MUST discard the targeted Hello if it failed the
237 if (flags
& F_HELLO_TARGETED
) {
238 log_debug("%s: lsr-id %pI4: invalid targeted hello transport address %s", __func__
, &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 %pI4: targeted hello with link-local source address", __func__
,
258 tnbr
= tnbr_find(leconf
, af
, src
);
260 /* remove the dynamic tnbr if the 'R' bit was cleared */
261 if (tnbr
&& (tnbr
->flags
& F_TNBR_DYNAMIC
) &&
262 !((flags
& F_HELLO_REQ_TARG
))) {
263 tnbr
->flags
&= ~F_TNBR_DYNAMIC
;
264 tnbr
= tnbr_check(leconf
, tnbr
);
268 struct ldpd_af_conf
*af_conf
;
270 if (!(flags
& F_HELLO_REQ_TARG
))
272 af_conf
= ldp_af_conf_get(leconf
, af
);
273 if (!(af_conf
->flags
& F_LDPD_AF_THELLO_ACCEPT
))
275 if (ldpe_acl_check(af_conf
->acl_thello_accept_from
, af
,
276 src
, (af
== AF_INET
) ? 32 : 128) != FILTER_PERMIT
)
279 tnbr
= tnbr_new(af
, src
);
280 tnbr
->flags
|= F_TNBR_DYNAMIC
;
282 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
285 source
.type
= HELLO_TARGETED
;
286 source
.target
= tnbr
;
288 ia
= iface_af_get(iface
, af
);
289 source
.type
= HELLO_LINK
;
291 source
.link
.src_addr
= *src
;
294 debug_hello_recv("%s lsr-id %pI4 transport-address %s holdtime %u%s",
295 log_hello_src(&source
), &lsr_id
, log_addr(af
, &trans_addr
),
296 holdtime
, (ds_tlv
) ? " (dual stack TLV present)" : "");
298 adj
= adj_find(lsr_id
, &source
);
299 if (adj
&& adj
->ds_tlv
!= ds_tlv
) {
301 * Transient condition, ignore packet and wait until adjacency
306 nbr
= nbr_find_ldpid(lsr_id
.s_addr
);
308 /* check dual-stack tlv */
309 if (ds_tlv
&& trans_pref
!= leconf
->trans_pref
) {
311 * RFC 7552 - Section 6.1.1:
312 * "If the Dual-Stack capability TLV is present and the remote
313 * preference does not match the local preference (or does not
314 * get recognized), then the LSR MUST discard the Hello message
316 * If an LDP session was already in place, then the LSR MUST
317 * send a fatal Notification message with status code of
318 * 'Transport Connection Mismatch' and reset the session".
320 log_debug("%s: lsr-id %pI4: remote transport preference does not match the local preference", __func__
, &lsr_id
);
322 session_shutdown(nbr
, S_TRANS_MISMTCH
, msg
->id
,
325 adj_del(adj
, S_SHUTDOWN
);
330 * Check for noncompliant dual-stack neighbor according to
331 * RFC 7552 section 6.1.1.
333 if (nbr
&& !ds_tlv
) {
336 if (nbr_adj_count(nbr
, AF_INET6
) > 0) {
337 session_shutdown(nbr
, S_DS_NONCMPLNCE
,
343 if (nbr_adj_count(nbr
, AF_INET
) > 0) {
344 session_shutdown(nbr
, S_DS_NONCMPLNCE
,
350 fatalx("recv_hello: unknown af");
355 * Protections against misconfigured networks and buggy implementations.
357 if (nbr
&& nbr
->af
== af
&&
358 (ldp_addrcmp(af
, &nbr
->raddr
, &trans_addr
) ||
359 nbr
->raddr_scope
!= scope_id
)) {
360 log_warnx("%s: lsr-id %pI4: hello packet advertising a different transport address", __func__
, &lsr_id
);
362 adj_del(adj
, S_SHUTDOWN
);
366 nbrt
= nbr_find_addr(af
, &trans_addr
);
368 log_debug("%s: transport address %s is already being used by lsr-id %pI4", __func__
, log_addr(af
,
369 &trans_addr
), &nbrt
->id
);
371 adj_del(adj
, S_SHUTDOWN
);
377 adj
= adj_new(lsr_id
, &source
, &trans_addr
);
380 RB_INSERT(nbr_adj_head
, &nbr
->adj_tree
, adj
);
382 ldp_sync_fsm_adj_event(adj
, LDP_SYNC_EVT_ADJ_NEW
);
384 adj
->ds_tlv
= ds_tlv
;
387 * If the hello adjacency's address-family doesn't match the local
388 * preference, then an adjacency is still created but we don't attempt
389 * to start an LDP session.
391 if (nbr
== NULL
&& (!ds_tlv
||
392 ((trans_pref
== DUAL_STACK_LDPOV4
&& af
== AF_INET
) ||
393 (trans_pref
== DUAL_STACK_LDPOV6
&& af
== AF_INET6
))))
394 nbr
= nbr_new(lsr_id
, af
, ds_tlv
, &trans_addr
, scope_id
);
396 /* dynamic LDPv4 GTSM negotiation as per RFC 6720 */
398 if (flags
& F_HELLO_GTSM
)
399 nbr
->flags
|= F_NBR_GTSM_NEGOTIATED
;
401 nbr
->flags
&= ~F_NBR_GTSM_NEGOTIATED
;
404 /* update neighbor's configuration sequence number */
405 if (nbr
&& (tlvs_rcvd
& F_HELLO_TLV_RCVD_CONF
)) {
406 if (conf_seqnum
> nbr
->conf_seqnum
&&
407 nbr_pending_idtimer(nbr
))
408 nbr_stop_idtimer(nbr
);
409 nbr
->conf_seqnum
= conf_seqnum
;
412 /* always update the holdtime to properly handle runtime changes */
413 switch (source
.type
) {
416 holdtime
= LINK_DFLT_HOLDTIME
;
418 adj
->holdtime
= min(if_get_hello_holdtime(ia
), holdtime
);
422 holdtime
= TARGETED_DFLT_HOLDTIME
;
424 adj
->holdtime
= min(tnbr_get_hello_holdtime(tnbr
), holdtime
);
426 if (adj
->holdtime
!= INFINITE_HOLDTIME
)
427 adj_start_itimer(adj
);
429 adj_stop_itimer(adj
);
431 if (nbr
&& nbr
->state
== NBR_STA_PRESENT
&& !nbr_pending_idtimer(nbr
) &&
432 nbr_session_active_role(nbr
) && !nbr_pending_connect(nbr
))
433 nbr_establish_connection(nbr
);
437 gen_hello_prms_tlv(struct ibuf
*buf
, uint16_t holdtime
, uint16_t flags
)
439 struct hello_prms_tlv parms
;
441 memset(&parms
, 0, sizeof(parms
));
442 parms
.type
= htons(TLV_TYPE_COMMONHELLO
);
443 parms
.length
= htons(sizeof(parms
.holdtime
) + sizeof(parms
.flags
));
444 parms
.holdtime
= htons(holdtime
);
445 parms
.flags
= htons(flags
);
447 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
451 gen_opt4_hello_prms_tlv(struct ibuf
*buf
, uint16_t type
, uint32_t value
)
453 struct hello_prms_opt4_tlv parms
;
455 memset(&parms
, 0, sizeof(parms
));
456 parms
.type
= htons(type
);
457 parms
.length
= htons(sizeof(parms
.value
));
460 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
464 gen_opt16_hello_prms_tlv(struct ibuf
*buf
, uint16_t type
, uint8_t *value
)
466 struct hello_prms_opt16_tlv parms
;
468 memset(&parms
, 0, sizeof(parms
));
469 parms
.type
= htons(type
);
470 parms
.length
= htons(sizeof(parms
.value
));
471 memcpy(&parms
.value
, value
, sizeof(parms
.value
));
473 return (ibuf_add(buf
, &parms
, sizeof(parms
)));
477 gen_ds_hello_prms_tlv(struct ibuf
*buf
, uint32_t value
)
479 if (leconf
->flags
& F_LDPD_DS_CISCO_INTEROP
)
480 value
= htonl(value
);
482 value
= htonl(value
<< 28);
484 return (gen_opt4_hello_prms_tlv(buf
, TLV_TYPE_DUALSTACK
, value
));
488 tlv_decode_hello_prms(char *buf
, uint16_t len
, uint16_t *holdtime
,
491 struct hello_prms_tlv tlv
;
493 if (len
< sizeof(tlv
))
495 memcpy(&tlv
, buf
, sizeof(tlv
));
497 if (tlv
.type
!= htons(TLV_TYPE_COMMONHELLO
))
499 if (ntohs(tlv
.length
) != sizeof(tlv
) - TLV_HDR_SIZE
)
502 *holdtime
= ntohs(tlv
.holdtime
);
503 *flags
= ntohs(tlv
.flags
);
505 return (sizeof(tlv
));
509 tlv_decode_opt_hello_prms(char *buf
, uint16_t len
, int *tlvs_rcvd
, int af
,
510 union ldpd_addr
*addr
, uint32_t *conf_number
, uint16_t *trans_pref
)
517 memset(addr
, 0, sizeof(*addr
));
522 * RFC 7552 - Section 6.1:
523 * "An LSR SHOULD accept the Hello message that contains both IPv4 and
524 * IPv6 Transport Address optional objects but MUST use only the
525 * transport address whose address family is the same as that of the
526 * IP packet carrying the Hello message. An LSR SHOULD accept only
527 * the first Transport Address optional object for a given address
528 * family in the received Hello message and ignore the rest if the
529 * LSR receives more than one Transport Address optional object for a
530 * given address family".
532 while (len
>= sizeof(tlv
)) {
533 memcpy(&tlv
, buf
, TLV_HDR_SIZE
);
534 tlv_len
= ntohs(tlv
.length
);
535 if (tlv_len
+ TLV_HDR_SIZE
> len
)
539 total
+= TLV_HDR_SIZE
;
541 switch (ntohs(tlv
.type
)) {
542 case TLV_TYPE_IPV4TRANSADDR
:
543 if (tlv_len
!= sizeof(addr
->v4
))
547 if (*tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
)
549 memcpy(&addr
->v4
, buf
, sizeof(addr
->v4
));
550 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_ADDR
;
552 case TLV_TYPE_IPV6TRANSADDR
:
553 if (tlv_len
!= sizeof(addr
->v6
))
557 if (*tlvs_rcvd
& F_HELLO_TLV_RCVD_ADDR
)
559 memcpy(&addr
->v6
, buf
, sizeof(addr
->v6
));
560 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_ADDR
;
562 case TLV_TYPE_CONFIG
:
563 if (tlv_len
!= sizeof(uint32_t))
565 memcpy(conf_number
, buf
, sizeof(uint32_t));
566 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_CONF
;
568 case TLV_TYPE_DUALSTACK
:
569 if (tlv_len
!= sizeof(uint32_t))
572 * RFC 7552 - Section 6.1:
573 * "A Single-stack LSR does not need to use the
574 * Dual-Stack capability in Hello messages and SHOULD
575 * ignore this capability if received".
577 if (!ldp_is_dual_stack(leconf
))
579 /* Shame on you, Cisco! */
580 if (leconf
->flags
& F_LDPD_DS_CISCO_INTEROP
) {
581 memcpy(trans_pref
, buf
+ sizeof(uint16_t),
583 *trans_pref
= ntohs(*trans_pref
);
585 memcpy(trans_pref
, buf
, sizeof(uint16_t));
586 *trans_pref
= ntohs(*trans_pref
) >> 12;
588 *tlvs_rcvd
|= F_HELLO_TLV_RCVD_DS
;
591 /* if unknown flag set, ignore TLV */
592 if (!(ntohs(tlv
.type
) & UNKNOWN_FLAG
))