1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
/* $OpenBSD$ */
/*
* Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
* Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <zebra.h>
#include "ldpd.h"
#include "ldpe.h"
#include "log.h"
#include "ldp_debug.h"
static int gen_hello_prms_tlv(struct ibuf *buf, uint16_t, uint16_t);
static int gen_opt4_hello_prms_tlv(struct ibuf *, uint16_t, uint32_t);
static int gen_opt16_hello_prms_tlv(struct ibuf *, uint16_t, uint8_t *);
static int gen_ds_hello_prms_tlv(struct ibuf *, uint32_t);
static int tlv_decode_hello_prms(char *, uint16_t, uint16_t *, uint16_t *);
static int tlv_decode_opt_hello_prms(char *, uint16_t, int *, int,
union ldpd_addr *, uint32_t *, uint16_t *);
int
send_hello(enum hello_type type, struct iface_af *ia, struct tnbr *tnbr)
{
int af;
union ldpd_addr dst;
uint16_t size, holdtime = 0, flags = 0;
int fd = 0;
struct ibuf *buf;
int err = 0;
switch (type) {
case HELLO_LINK:
af = ia->af;
holdtime = if_get_hello_holdtime(ia);
flags = 0;
fd = (ldp_af_global_get(&global, af))->ldp_disc_socket;
/* multicast destination address */
switch (af) {
case AF_INET:
if (!(leconf->ipv4.flags & F_LDPD_AF_NO_GTSM))
flags |= F_HELLO_GTSM;
dst.v4 = global.mcast_addr_v4;
break;
case AF_INET6:
dst.v6 = global.mcast_addr_v6;
break;
default:
fatalx("send_hello: unknown af");
}
break;
case HELLO_TARGETED:
af = tnbr->af;
holdtime = tnbr_get_hello_holdtime(tnbr);
flags = F_HELLO_TARGETED;
if ((tnbr->flags & F_TNBR_CONFIGURED) || tnbr->pw_count)
flags |= F_HELLO_REQ_TARG;
fd = (ldp_af_global_get(&global, af))->ldp_edisc_socket;
/* unicast destination address */
dst = tnbr->addr;
break;
default:
fatalx("send_hello: unknown hello type");
}
/* calculate message size */
size = LDP_HDR_SIZE + LDP_MSG_SIZE + sizeof(struct hello_prms_tlv);
switch (af) {
case AF_INET:
size += sizeof(struct hello_prms_opt4_tlv);
break;
case AF_INET6:
size += sizeof(struct hello_prms_opt16_tlv);
break;
default:
fatalx("send_hello: unknown af");
}
size += sizeof(struct hello_prms_opt4_tlv);
if (ldp_is_dual_stack(leconf))
size += sizeof(struct hello_prms_opt4_tlv);
/* generate message */
if ((buf = ibuf_open(size)) == NULL)
fatal(__func__);
err |= gen_ldp_hdr(buf, size);
size -= LDP_HDR_SIZE;
err |= gen_msg_hdr(buf, MSG_TYPE_HELLO, size);
err |= gen_hello_prms_tlv(buf, holdtime, flags);
/*
* RFC 7552 - Section 6.1:
* "An LSR MUST include only the transport address whose address
* family is the same as that of the IP packet carrying the Hello
* message".
*/
switch (af) {
case AF_INET:
err |= gen_opt4_hello_prms_tlv(buf, TLV_TYPE_IPV4TRANSADDR,
leconf->ipv4.trans_addr.v4.s_addr);
break;
case AF_INET6:
err |= gen_opt16_hello_prms_tlv(buf, TLV_TYPE_IPV6TRANSADDR,
leconf->ipv6.trans_addr.v6.s6_addr);
break;
default:
fatalx("send_hello: unknown af");
}
err |= gen_opt4_hello_prms_tlv(buf, TLV_TYPE_CONFIG,
htonl(global.conf_seqnum));
/*
* RFC 7552 - Section 6.1.1:
* "A Dual-stack LSR (i.e., an LSR supporting Dual-stack LDP for a peer)
* MUST include the Dual-Stack capability TLV in all of its LDP Hellos".
*/
if (ldp_is_dual_stack(leconf))
err |= gen_ds_hello_prms_tlv(buf, leconf->trans_pref);
if (err) {
ibuf_free(buf);
return (-1);
}
switch (type) {
case HELLO_LINK:
debug_hello_send("iface %s (%s) holdtime %u", ia->iface->name,
af_name(ia->af), holdtime);
break;
case HELLO_TARGETED:
debug_hello_send("targeted-neighbor %s (%s) holdtime %u",
log_addr(tnbr->af, &tnbr->addr), af_name(tnbr->af),
holdtime);
break;
default:
fatalx("send_hello: unknown hello type");
}
send_packet(fd, af, &dst, ia, buf->buf, buf->wpos);
ibuf_free(buf);
return (0);
}
void
recv_hello(struct in_addr lsr_id, struct ldp_msg *msg, int af,
union ldpd_addr *src, struct iface *iface, int multicast, char *buf,
uint16_t len)
{
struct adj *adj = NULL;
struct nbr *nbr, *nbrt;
uint16_t holdtime = 0, flags = 0;
int tlvs_rcvd;
int ds_tlv;
union ldpd_addr trans_addr;
uint32_t scope_id = 0;
uint32_t conf_seqnum;
uint16_t trans_pref;
int r;
struct hello_source source;
struct iface_af *ia = NULL;
struct tnbr *tnbr = NULL;
r = tlv_decode_hello_prms(buf, len, &holdtime, &flags);
if (r == -1) {
log_debug("%s: lsr-id %s: failed to decode params", __func__,
inet_ntoa(lsr_id));
return;
}
/* safety checks */
if (holdtime != 0 && holdtime < MIN_HOLDTIME) {
log_debug("%s: lsr-id %s: invalid hello holdtime (%u)",
__func__, inet_ntoa(lsr_id), holdtime);
return;
}
if (multicast && (flags & F_HELLO_TARGETED)) {
log_debug("%s: lsr-id %s: multicast targeted hello", __func__,
inet_ntoa(lsr_id));
return;
}
if (!multicast && !((flags & F_HELLO_TARGETED))) {
log_debug("%s: lsr-id %s: unicast link hello", __func__,
inet_ntoa(lsr_id));
return;
}
buf += r;
len -= r;
r = tlv_decode_opt_hello_prms(buf, len, &tlvs_rcvd, af, &trans_addr,
&conf_seqnum, &trans_pref);
if (r == -1) {
log_debug("%s: lsr-id %s: failed to decode optional params",
__func__, inet_ntoa(lsr_id));
return;
}
if (r != len) {
log_debug("%s: lsr-id %s: unexpected data in message",
__func__, inet_ntoa(lsr_id));
return;
}
/* implicit transport address */
if (!(tlvs_rcvd & F_HELLO_TLV_RCVD_ADDR))
trans_addr = *src;
if (bad_addr(af, &trans_addr)) {
log_debug("%s: lsr-id %s: invalid transport address %s",
__func__, inet_ntoa(lsr_id), log_addr(af, &trans_addr));
return;
}
if (af == AF_INET6 && IN6_IS_SCOPE_EMBED(&trans_addr.v6)) {
/*
* RFC 7552 - Section 6.1:
* "An LSR MUST use a global unicast IPv6 address in an IPv6
* Transport Address optional object of outgoing targeted
* Hellos and check for the same in incoming targeted Hellos
* (i.e., MUST discard the targeted Hello if it failed the
* check)".
*/
if (flags & F_HELLO_TARGETED) {
log_debug("%s: lsr-id %s: invalid targeted hello "
"transport address %s", __func__, inet_ntoa(lsr_id),
log_addr(af, &trans_addr));
return;
}
scope_id = iface->ifindex;
}
memset(&source, 0, sizeof(source));
if (flags & F_HELLO_TARGETED) {
/*
* RFC 7552 - Section 5.2:
* "The link-local IPv6 addresses MUST NOT be used as the
* targeted LDP Hello packet's source or destination addresses".
*/
if (af == AF_INET6 && IN6_IS_SCOPE_EMBED(&src->v6)) {
log_debug("%s: lsr-id %s: targeted hello with "
"link-local source address", __func__,
inet_ntoa(lsr_id));
return;
}
tnbr = tnbr_find(leconf, af, src);
/* remove the dynamic tnbr if the 'R' bit was cleared */
if (tnbr && (tnbr->flags & F_TNBR_DYNAMIC) &&
!((flags & F_HELLO_REQ_TARG))) {
tnbr->flags &= ~F_TNBR_DYNAMIC;
tnbr = tnbr_check(leconf, tnbr);
}
if (!tnbr) {
struct ldpd_af_conf *af_conf;
if (!(flags & F_HELLO_REQ_TARG))
return;
af_conf = ldp_af_conf_get(leconf, af);
if (!(af_conf->flags & F_LDPD_AF_THELLO_ACCEPT))
return;
if (ldpe_acl_check(af_conf->acl_thello_accept_from, af,
src, (af == AF_INET) ? 32 : 128) != FILTER_PERMIT)
return;
tnbr = tnbr_new(af, src);
tnbr->flags |= F_TNBR_DYNAMIC;
tnbr_update(tnbr);
RB_INSERT(tnbr_head, &leconf->tnbr_tree, tnbr);
}
source.type = HELLO_TARGETED;
source.target = tnbr;
} else {
ia = iface_af_get(iface, af);
source.type = HELLO_LINK;
source.link.ia = ia;
source.link.src_addr = *src;
}
adj = adj_find(&source);
nbr = nbr_find_ldpid(lsr_id.s_addr);
/* check dual-stack tlv */
ds_tlv = (tlvs_rcvd & F_HELLO_TLV_RCVD_DS) ? 1 : 0;
if (ds_tlv && trans_pref != leconf->trans_pref) {
/*
* RFC 7552 - Section 6.1.1:
* "If the Dual-Stack capability TLV is present and the remote
* preference does not match the local preference (or does not
* get recognized), then the LSR MUST discard the Hello message
* and log an error.
* If an LDP session was already in place, then the LSR MUST
* send a fatal Notification message with status code of
* 'Transport Connection Mismatch' and reset the session".
*/
log_debug("%s: lsr-id %s: remote transport preference does not "
"match the local preference", __func__, inet_ntoa(lsr_id));
if (nbr)
session_shutdown(nbr, S_TRANS_MISMTCH, msg->id,
msg->type);
if (adj)
adj_del(adj, S_SHUTDOWN);
return;
}
/*
* Check for noncompliant dual-stack neighbor according to
* RFC 7552 section 6.1.1.
*/
if (nbr && !ds_tlv) {
switch (af) {
case AF_INET:
if (nbr_adj_count(nbr, AF_INET6) > 0) {
session_shutdown(nbr, S_DS_NONCMPLNCE,
msg->id, msg->type);
return;
}
break;
case AF_INET6:
if (nbr_adj_count(nbr, AF_INET) > 0) {
session_shutdown(nbr, S_DS_NONCMPLNCE,
msg->id, msg->type);
return;
}
break;
default:
fatalx("recv_hello: unknown af");
}
}
/*
* Protections against misconfigured networks and buggy implementations.
*/
if (nbr && nbr->af == af &&
(ldp_addrcmp(af, &nbr->raddr, &trans_addr) ||
nbr->raddr_scope != scope_id)) {
log_warnx("%s: lsr-id %s: hello packet advertising a different "
"transport address", __func__, inet_ntoa(lsr_id));
if (adj)
adj_del(adj, S_SHUTDOWN);
return;
}
if (nbr == NULL) {
nbrt = nbr_find_addr(af, &trans_addr);
if (nbrt) {
log_debug("%s: transport address %s is already being "
"used by lsr-id %s", __func__, log_addr(af,
&trans_addr), inet_ntoa(nbrt->id));
if (adj)
adj_del(adj, S_SHUTDOWN);
return;
}
}
if (adj == NULL) {
adj = adj_new(lsr_id, &source, &trans_addr);
if (nbr) {
adj->nbr = nbr;
RB_INSERT(nbr_adj_head, &nbr->adj_tree, adj);
}
}
/*
* If the hello adjacency's address-family doesn't match the local
* preference, then an adjacency is still created but we don't attempt
* to start an LDP session.
*/
if (nbr == NULL && (!ds_tlv ||
((trans_pref == DUAL_STACK_LDPOV4 && af == AF_INET) ||
(trans_pref == DUAL_STACK_LDPOV6 && af == AF_INET6))))
nbr = nbr_new(lsr_id, af, ds_tlv, &trans_addr, scope_id);
/* dynamic LDPv4 GTSM negotiation as per RFC 6720 */
if (nbr) {
if (flags & F_HELLO_GTSM)
nbr->flags |= F_NBR_GTSM_NEGOTIATED;
else
nbr->flags &= ~F_NBR_GTSM_NEGOTIATED;
}
/* update neighbor's configuration sequence number */
if (nbr && (tlvs_rcvd & F_HELLO_TLV_RCVD_CONF)) {
if (conf_seqnum > nbr->conf_seqnum &&
nbr_pending_idtimer(nbr))
nbr_stop_idtimer(nbr);
nbr->conf_seqnum = conf_seqnum;
}
/* always update the holdtime to properly handle runtime changes */
switch (source.type) {
case HELLO_LINK:
if (holdtime == 0)
holdtime = LINK_DFLT_HOLDTIME;
adj->holdtime = min(if_get_hello_holdtime(ia), holdtime);
break;
case HELLO_TARGETED:
if (holdtime == 0)
holdtime = TARGETED_DFLT_HOLDTIME;
adj->holdtime = min(tnbr_get_hello_holdtime(tnbr), holdtime);
}
if (adj->holdtime != INFINITE_HOLDTIME)
adj_start_itimer(adj);
else
adj_stop_itimer(adj);
debug_hello_recv("%s lsr-id %s transport-address %s holdtime %u%s",
log_hello_src(&source), inet_ntoa(lsr_id), log_addr(af, &trans_addr),
holdtime, (ds_tlv) ? " (dual stack TLV present)" : "");
if (nbr && nbr->state == NBR_STA_PRESENT && !nbr_pending_idtimer(nbr) &&
nbr_session_active_role(nbr) && !nbr_pending_connect(nbr))
nbr_establish_connection(nbr);
}
static int
gen_hello_prms_tlv(struct ibuf *buf, uint16_t holdtime, uint16_t flags)
{
struct hello_prms_tlv parms;
memset(&parms, 0, sizeof(parms));
parms.type = htons(TLV_TYPE_COMMONHELLO);
parms.length = htons(sizeof(parms.holdtime) + sizeof(parms.flags));
parms.holdtime = htons(holdtime);
parms.flags = htons(flags);
return (ibuf_add(buf, &parms, sizeof(parms)));
}
static int
gen_opt4_hello_prms_tlv(struct ibuf *buf, uint16_t type, uint32_t value)
{
struct hello_prms_opt4_tlv parms;
memset(&parms, 0, sizeof(parms));
parms.type = htons(type);
parms.length = htons(sizeof(parms.value));
parms.value = value;
return (ibuf_add(buf, &parms, sizeof(parms)));
}
static int
gen_opt16_hello_prms_tlv(struct ibuf *buf, uint16_t type, uint8_t *value)
{
struct hello_prms_opt16_tlv parms;
memset(&parms, 0, sizeof(parms));
parms.type = htons(type);
parms.length = htons(sizeof(parms.value));
memcpy(&parms.value, value, sizeof(parms.value));
return (ibuf_add(buf, &parms, sizeof(parms)));
}
static int
gen_ds_hello_prms_tlv(struct ibuf *buf, uint32_t value)
{
if (leconf->flags & F_LDPD_DS_CISCO_INTEROP)
value = htonl(value);
else
value = htonl(value << 28);
return (gen_opt4_hello_prms_tlv(buf, TLV_TYPE_DUALSTACK, value));
}
static int
tlv_decode_hello_prms(char *buf, uint16_t len, uint16_t *holdtime,
uint16_t *flags)
{
struct hello_prms_tlv tlv;
if (len < sizeof(tlv))
return (-1);
memcpy(&tlv, buf, sizeof(tlv));
if (tlv.type != htons(TLV_TYPE_COMMONHELLO))
return (-1);
if (ntohs(tlv.length) != sizeof(tlv) - TLV_HDR_SIZE)
return (-1);
*holdtime = ntohs(tlv.holdtime);
*flags = ntohs(tlv.flags);
return (sizeof(tlv));
}
static int
tlv_decode_opt_hello_prms(char *buf, uint16_t len, int *tlvs_rcvd, int af,
union ldpd_addr *addr, uint32_t *conf_number, uint16_t *trans_pref)
{
struct tlv tlv;
uint16_t tlv_len;
int total = 0;
*tlvs_rcvd = 0;
memset(addr, 0, sizeof(*addr));
*conf_number = 0;
*trans_pref = 0;
/*
* RFC 7552 - Section 6.1:
* "An LSR SHOULD accept the Hello message that contains both IPv4 and
* IPv6 Transport Address optional objects but MUST use only the
* transport address whose address family is the same as that of the
* IP packet carrying the Hello message. An LSR SHOULD accept only
* the first Transport Address optional object for a given address
* family in the received Hello message and ignore the rest if the
* LSR receives more than one Transport Address optional object for a
* given address family".
*/
while (len >= sizeof(tlv)) {
memcpy(&tlv, buf, TLV_HDR_SIZE);
tlv_len = ntohs(tlv.length);
if (tlv_len + TLV_HDR_SIZE > len)
return (-1);
buf += TLV_HDR_SIZE;
len -= TLV_HDR_SIZE;
total += TLV_HDR_SIZE;
switch (ntohs(tlv.type)) {
case TLV_TYPE_IPV4TRANSADDR:
if (tlv_len != sizeof(addr->v4))
return (-1);
if (af != AF_INET)
return (-1);
if (*tlvs_rcvd & F_HELLO_TLV_RCVD_ADDR)
break;
memcpy(&addr->v4, buf, sizeof(addr->v4));
*tlvs_rcvd |= F_HELLO_TLV_RCVD_ADDR;
break;
case TLV_TYPE_IPV6TRANSADDR:
if (tlv_len != sizeof(addr->v6))
return (-1);
if (af != AF_INET6)
return (-1);
if (*tlvs_rcvd & F_HELLO_TLV_RCVD_ADDR)
break;
memcpy(&addr->v6, buf, sizeof(addr->v6));
*tlvs_rcvd |= F_HELLO_TLV_RCVD_ADDR;
break;
case TLV_TYPE_CONFIG:
if (tlv_len != sizeof(uint32_t))
return (-1);
memcpy(conf_number, buf, sizeof(uint32_t));
*tlvs_rcvd |= F_HELLO_TLV_RCVD_CONF;
break;
case TLV_TYPE_DUALSTACK:
if (tlv_len != sizeof(uint32_t))
return (-1);
/*
* RFC 7552 - Section 6.1:
* "A Single-stack LSR does not need to use the
* Dual-Stack capability in Hello messages and SHOULD
* ignore this capability if received".
*/
if (!ldp_is_dual_stack(leconf))
break;
/* Shame on you, Cisco! */
if (leconf->flags & F_LDPD_DS_CISCO_INTEROP) {
memcpy(trans_pref, buf + sizeof(uint16_t),
sizeof(uint16_t));
*trans_pref = ntohs(*trans_pref);
} else {
memcpy(trans_pref, buf , sizeof(uint16_t));
*trans_pref = ntohs(*trans_pref) >> 12;
}
*tlvs_rcvd |= F_HELLO_TLV_RCVD_DS;
break;
default:
/* if unknown flag set, ignore TLV */
if (!(ntohs(tlv.type) & UNKNOWN_FLAG))
return (-1);
break;
}
buf += tlv_len;
len -= tlv_len;
total += tlv_len;
}
return (total);
}
|