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
|
/* $OpenBSD: tlv.c,v 1.14 2016/09/02 16:46:29 renato Exp $ */
/*
* Copyright (c) 2015 Renato Westphal <renato@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 <sys/types.h>
#include <sys/utsname.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eigrpd.h"
#include "eigrpe.h"
#include "log.h"
int
gen_parameter_tlv(struct ibuf *buf, struct eigrp_iface *ei, int peerterm)
{
struct tlv_parameter tp;
tp.type = htons(TLV_TYPE_PARAMETER);
tp.length = htons(TLV_TYPE_PARAMETER_LEN);
if (peerterm) {
tp.kvalues[0] = 255;
tp.kvalues[1] = 255;
tp.kvalues[2] = 255;
tp.kvalues[3] = 255;
tp.kvalues[4] = 255;
tp.kvalues[5] = 0;
} else
memcpy(tp.kvalues, ei->eigrp->kvalues, 6);
tp.holdtime = htons(ei->hello_holdtime);
return (ibuf_add(buf, &tp, sizeof(tp)));
}
int
gen_sequence_tlv(struct ibuf *buf, struct seq_addr_head *seq_addr_list)
{
struct tlv tlv, *tlvp;
struct seq_addr_entry *sa;
uint8_t alen;
uint16_t len = TLV_HDR_LEN;
size_t original_size = ibuf_size(buf);
tlv.type = htons(TLV_TYPE_SEQ);
if (ibuf_add(buf, &tlv, sizeof(tlv))) {
log_warn("%s: ibuf_add failed", __func__);
return (-1);
}
TAILQ_FOREACH(sa, seq_addr_list, entry) {
switch (sa->af) {
case AF_INET:
alen = INADDRSZ;
break;
case AF_INET6:
alen = IN6ADDRSZ;
break;
default:
fatalx("gen_sequence_tlv: unknown address family");
}
if (ibuf_add(buf, &alen, sizeof(alen)))
return (-1);
if (ibuf_add(buf, &sa->addr, alen)) {
log_warn("%s: ibuf_add failed", __func__);
return (-1);
}
len += (sizeof(alen) + alen);
}
/* adjust tlv length */
if ((tlvp = ibuf_seek(buf, original_size, sizeof(*tlvp))) == NULL)
fatalx("gen_sequence_tlv: buf_seek failed");
tlvp->length = htons(len);
return (0);
}
int
gen_sw_version_tlv(struct ibuf *buf)
{
struct tlv_sw_version ts;
struct utsname u;
unsigned int vendor_os_major;
unsigned int vendor_os_minor;
memset(&ts, 0, sizeof(ts));
ts.type = htons(TLV_TYPE_SW_VERSION);
ts.length = htons(TLV_TYPE_SW_VERSION_LEN);
if (uname(&u) == 0) {
if (sscanf(u.release, "%u.%u", &vendor_os_major,
&vendor_os_minor) == 2) {
ts.vendor_os_major = (uint8_t) vendor_os_major;
ts.vendor_os_minor = (uint8_t) vendor_os_minor;
}
}
ts.eigrp_major = EIGRP_VERSION_MAJOR;
ts.eigrp_minor = EIGRP_VERSION_MINOR;
return (ibuf_add(buf, &ts, sizeof(ts)));
}
int
gen_mcast_seq_tlv(struct ibuf *buf, uint32_t seq)
{
struct tlv_mcast_seq tm;
tm.type = htons(TLV_TYPE_MCAST_SEQ);
tm.length = htons(TLV_TYPE_MCAST_SEQ_LEN);
tm.seq = htonl(seq);
return (ibuf_add(buf, &tm, sizeof(tm)));
}
uint16_t
len_route_tlv(struct rinfo *ri)
{
uint16_t len = TLV_HDR_LEN;
switch (ri->af) {
case AF_INET:
len += sizeof(ri->nexthop.v4);
len += PREFIX_SIZE4(ri->prefixlen);
break;
case AF_INET6:
len += sizeof(ri->nexthop.v6);
len += PREFIX_SIZE6(ri->prefixlen);
break;
default:
break;
}
len += sizeof(ri->metric);
if (ri->type == EIGRP_ROUTE_EXTERNAL)
len += sizeof(ri->emetric);
len += sizeof(ri->prefixlen);
return (len);
}
int
gen_route_tlv(struct ibuf *buf, struct rinfo *ri)
{
struct tlv tlv, *tlvp;
struct in_addr addr;
struct classic_metric metric;
struct classic_emetric emetric;
uint16_t tlvlen;
uint8_t pflen;
size_t original_size = ibuf_size(buf);
switch (ri->af) {
case AF_INET:
tlv.type = TLV_PROTO_IPV4;
break;
case AF_INET6:
tlv.type = TLV_PROTO_IPV6;
break;
default:
fatalx("gen_route_tlv: unknown af");
}
switch (ri->type) {
case EIGRP_ROUTE_INTERNAL:
tlv.type |= TLV_ROUTE_INTERNAL;
break;
case EIGRP_ROUTE_EXTERNAL:
tlv.type |= TLV_ROUTE_EXTERNAL;
break;
default:
fatalx("gen_route_tlv: unknown type");
}
tlv.type = htons(tlv.type);
if (ibuf_add(buf, &tlv, sizeof(tlv)))
return (-1);
tlvlen = TLV_HDR_LEN;
/* nexthop */
switch (ri->af) {
case AF_INET:
addr.s_addr = htonl(ri->nexthop.v4.s_addr);
if (ibuf_add(buf, &addr, sizeof(addr)))
return (-1);
tlvlen += sizeof(ri->nexthop.v4);
break;
case AF_INET6:
if (ibuf_add(buf, &ri->nexthop.v6, sizeof(ri->nexthop.v6)))
return (-1);
tlvlen += sizeof(ri->nexthop.v6);
break;
default:
fatalx("gen_route_tlv: unknown af");
}
/* exterior metric */
if (ri->type == EIGRP_ROUTE_EXTERNAL) {
emetric = ri->emetric;
emetric.routerid = htonl(emetric.routerid);
emetric.as = htonl(emetric.as);
emetric.tag = htonl(emetric.tag);
emetric.metric = htonl(emetric.metric);
emetric.reserved = htons(emetric.reserved);
if (ibuf_add(buf, &emetric, sizeof(emetric)))
return (-1);
tlvlen += sizeof(emetric);
}
/* metric */
metric = ri->metric;
metric.delay = htonl(metric.delay);
metric.bandwidth = htonl(metric.bandwidth);
if (ibuf_add(buf, &metric, sizeof(metric)))
return (-1);
tlvlen += sizeof(metric);
/* destination */
if (ibuf_add(buf, &ri->prefixlen, sizeof(ri->prefixlen)))
return (-1);
switch (ri->af) {
case AF_INET:
pflen = PREFIX_SIZE4(ri->prefixlen);
if (ibuf_add(buf, &ri->prefix.v4, pflen))
return (-1);
break;
case AF_INET6:
pflen = PREFIX_SIZE6(ri->prefixlen);
if (ibuf_add(buf, &ri->prefix.v6, pflen))
return (-1);
break;
default:
fatalx("gen_route_tlv: unknown af");
}
tlvlen += sizeof(pflen) + pflen;
/* adjust tlv length */
if ((tlvp = ibuf_seek(buf, original_size, sizeof(*tlvp))) == NULL)
fatalx("gen_route_tlv: buf_seek failed");
tlvp->length = htons(tlvlen);
return (0);
}
struct tlv_parameter *
tlv_decode_parameter(struct tlv *tlv, char *buf)
{
struct tlv_parameter *tp;
if (ntohs(tlv->length) != TLV_TYPE_PARAMETER_LEN) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (NULL);
}
tp = (struct tlv_parameter *)buf;
return (tp);
}
int
tlv_decode_seq(int af, struct tlv *tlv, char *buf,
struct seq_addr_head *seq_addr_list)
{
uint16_t len;
uint8_t alen;
struct seq_addr_entry *sa;
len = ntohs(tlv->length);
if (len < TLV_HDR_LEN) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (-1);
}
buf += TLV_HDR_LEN;
len -= TLV_HDR_LEN;
while (len > 0) {
memcpy(&alen, buf, sizeof(alen));
buf += sizeof(alen);
len -= sizeof(alen);
if (alen > len) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (-1);
}
switch (af) {
case AF_INET:
if (alen != INADDRSZ) {
log_debug("%s: invalid address length",
__func__);
return (-1);
}
break;
case AF_INET6:
if (alen != IN6ADDRSZ) {
log_debug("%s: invalid address length",
__func__);
return (-1);
}
break;
default:
fatalx("tlv_decode_seq: unknown af");
}
if ((sa = calloc(1, sizeof(*sa))) == NULL)
fatal("tlv_decode_seq");
sa->af = af;
memcpy(&sa->addr, buf, alen);
TAILQ_INSERT_TAIL(seq_addr_list, sa, entry);
buf += alen;
len -= alen;
}
return (0);
}
struct tlv_sw_version *
tlv_decode_sw_version(struct tlv *tlv, char *buf)
{
struct tlv_sw_version *tv;
if (ntohs(tlv->length) != TLV_TYPE_SW_VERSION_LEN) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (NULL);
}
tv = (struct tlv_sw_version *)buf;
return (tv);
}
struct tlv_mcast_seq *
tlv_decode_mcast_seq(struct tlv *tlv, char *buf)
{
struct tlv_mcast_seq *tm;
if (ntohs(tlv->length) != TLV_TYPE_MCAST_SEQ_LEN) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (NULL);
}
tm = (struct tlv_mcast_seq *)buf;
return (tm);
}
int
tlv_decode_route(int af, struct tlv *tlv, char *buf, struct rinfo *ri)
{
unsigned int tlv_len, min_len, max_plen, plen, offset;
ri->af = af;
switch (ri->af) {
case AF_INET:
min_len = TLV_TYPE_IPV4_INT_MIN_LEN;
max_plen = sizeof(ri->prefix.v4);
break;
case AF_INET6:
min_len = TLV_TYPE_IPV6_INT_MIN_LEN;
max_plen = sizeof(ri->prefix.v6);
break;
default:
fatalx("tlv_decode_route: unknown af");
}
switch (ntohs(tlv->type) & TLV_TYPE_MASK) {
case TLV_ROUTE_INTERNAL:
ri->type = EIGRP_ROUTE_INTERNAL;
break;
case TLV_ROUTE_EXTERNAL:
ri->type = EIGRP_ROUTE_EXTERNAL;
min_len += sizeof(struct classic_emetric);
break;
default:
fatalx("tlv_decode_route: unknown type");
}
tlv_len = ntohs(tlv->length);
if (tlv_len < min_len) {
log_debug("%s: malformed tlv (bad length)", __func__);
return (-1);
}
/* nexthop */
offset = TLV_HDR_LEN;
switch (af) {
case AF_INET:
memcpy(&ri->nexthop.v4, buf + offset, sizeof(ri->nexthop.v4));
offset += sizeof(ri->nexthop.v4);
break;
case AF_INET6:
memcpy(&ri->nexthop.v6, buf + offset, sizeof(ri->nexthop.v6));
offset += sizeof(ri->nexthop.v6);
break;
default:
fatalx("tlv_decode_route: unknown af");
}
/* exterior metric */
if (ri->type == EIGRP_ROUTE_EXTERNAL) {
memcpy(&ri->emetric, buf + offset, sizeof(ri->emetric));
ri->emetric.routerid = ntohl(ri->emetric.routerid);
ri->emetric.as = ntohl(ri->emetric.as);
ri->emetric.tag = ntohl(ri->emetric.tag);
ri->emetric.metric = ntohl(ri->emetric.metric);
ri->emetric.reserved = ntohs(ri->emetric.reserved);
offset += sizeof(ri->emetric);
}
/* metric */
memcpy(&ri->metric, buf + offset, sizeof(ri->metric));
ri->metric.delay = ntohl(ri->metric.delay);
ri->metric.bandwidth = ntohl(ri->metric.bandwidth);
offset += sizeof(ri->metric);
/* prefixlen */
memcpy(&ri->prefixlen, buf + offset, sizeof(ri->prefixlen));
offset += sizeof(ri->prefixlen);
/*
* Different versions of IOS can use a different number of bytes to
* encode the same IPv6 prefix. This sucks but we have to deal with it.
* Instead of calculating the number of bytes based on the value of the
* prefixlen field, let's get this number by subtracting the size of all
* other fields from the total size of the TLV. It works because all
* the other fields have a fixed length.
*/
plen = tlv_len - min_len;
/* safety check */
if (plen > max_plen) {
log_debug("%s: malformed tlv", __func__);
return (-1);
}
/* destination */
switch (af) {
case AF_INET:
memset(&ri->prefix.v4, 0, sizeof(ri->prefix.v4));
memcpy(&ri->prefix.v4, buf + offset, plen);
break;
case AF_INET6:
memset(&ri->prefix.v6, 0, sizeof(ri->prefix.v6));
memcpy(&ri->prefix.v6, buf + offset, plen);
break;
default:
fatalx("tlv_decode_route: unknown af");
}
/* check if the network is valid */
if (bad_addr(af, &ri->prefix) ||
(af == AF_INET6 && IN6_IS_SCOPE_EMBED(&ri->prefix.v6))) {
log_debug("%s: malformed tlv (invalid prefix): %s", __func__,
log_addr(af, &ri->prefix));
return (-1);
}
/* just in case... */
eigrp_applymask(af, &ri->prefix, &ri->prefix, ri->prefixlen);
return (0);
}
void
metric_encode_mtu(uint8_t *dst, int mtu)
{
dst[0] = (mtu & 0x00FF0000) >> 16;
dst[1] = (mtu & 0x0000FF00) >> 8;
dst[2] = (mtu & 0x000000FF);
}
int
metric_decode_mtu(uint8_t *mtu)
{
return ((mtu[0] << 16) + (mtu[1] << 8) + mtu[2]);
}
|