-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheigrp_tlv1.c
433 lines (370 loc) · 12 KB
/
eigrp_tlv1.c
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
/*
* EIGRP Packet Processor for EIGRP TLV version 1 processing
* Copyright (C) 2018
* Authors:
* Donnie Savage
*
* The intent of this file is to define a self contained TLV processor which
* can be agnostic to the main EIGRP routing process. This will eliminate
* special processing to handle the narrow versus wide metrics and the TLV
* packet format differences.
* All data sent to, or returned from, this TLV processor will be normalized
* to the system (meaning a delay is a delay and you dont have to worry about
* conversion)
*/
#include "eigrpd/eigrpd.h"
#include "eigrpd/eigrp_types.h"
#include "eigrpd/eigrp_structs.h"
#include "eigrpd/eigrp_interface.h"
#include "eigrpd/eigrp_neighbor.h"
#include "eigrpd/eigrp_packet.h"
#include "eigrpd/eigrp_network.h"
#include "eigrpd/eigrp_topology.h"
#include "eigrpd/eigrp_fsm.h"
#include "eigrpd/eigrp_metric.h"
#include "eigrpd/eigrp_dump.h"
/**
* Number of useful macros which map to the packet
*/
// Vector Metrics
#define EIGRP_TLV1_METRIC \
uint32_t delay; \
uint32_t bandwidth; \
uint8_t mtu[3]; \
uint8_t hop_count; \
uint8_t reliability; \
uint8_t load; \
uint8_t tag; \
uint8_t flags;
#define EIGRP_TLV1_METRIC_SIZE 16
// Nexthop (0 if this router)
#define EIGRP_TLV1_IPV4_NEXTHOP uint32_t nexthop;
#define EIGRP_TLV1_IPV4_NEXTHOP_SIZE 4
// External Data for redistributed route
#define EIGRP_TLV1_EXTDATA \
uint32_t orig; \
uint32_t as; \
uint32_t tag; \
uint32_t metric; \
uint16_t reserved; \
uint8_t protocol; \
uint8_t flags;
#define EIGRP_TLV1_EXTDATA_SIZE 20
/**
* structure to lay over a packet for encoding/decoding
*/
typedef struct eigrp_tlv1_header {
EIGRP_TLV_HDR
EIGRP_TLV1_IPV4_NEXTHOP
} __attribute__((packed)) eigrp_tlv1_header_t;
typedef struct eigrp_tlv1_internal_ipv4 {
EIGRP_TLV_HDR
EIGRP_TLV1_IPV4_NEXTHOP
EIGRP_TLV1_METRIC
// destination address
uint8_t prefix_length;
unsigned char destination_part[4];
} __attribute__((packed)) eigrp_tlv1_internal_ipv4_t;
typedef struct eigrp_tlv1_external_ipv4 {
EIGRP_TLV_HDR
EIGRP_TLV1_IPV4_NEXTHOP
EIGRP_TLV1_METRIC
// destination address
uint8_t prefix_length;
unsigned char destination_part[4];
} __attribute__((packed)) eigrp_tlv1_external_ipv4_t;
typedef struct eigrp_tlv1_extdata {
EIGRP_TLV1_EXTDATA
} __attribute__((packed)) eigrp_tlv1_extdata_t;
#define EIGRP_IPV4_MIN_TLV \
(EIGRP_TLV_HDR_SIZE + EIGRP_TLV1_IPV4_NEXTHOP_SIZE \
+ EIGRP_TLV1_METRIC_SIZE)
/**
* extract the external route information provide by the
* router redistributing the original route
*/
static uint16_t eigrp_tlv1_external_decode(eigrp_stream_t *pkt,
eigrp_extdata_t *extdata)
{
extdata->orig = stream_getl(pkt);
extdata->as = stream_getl(pkt);
extdata->tag = stream_getl(pkt);
extdata->metric = stream_getl(pkt);
extdata->reserved = stream_getw(pkt);
extdata->protocol = stream_getc(pkt);
extdata->flags = stream_getc(pkt);
return (EIGRP_TLV1_EXTDATA_SIZE);
}
static uint16_t eigrp_tlv1_external_encode(eigrp_stream_t *pkt,
eigrp_extdata_t *extdata)
{
stream_putl(pkt, extdata->orig);
stream_putl(pkt, extdata->as);
stream_putl(pkt, extdata->tag);
stream_putl(pkt, extdata->metric);
stream_putw(pkt, extdata->reserved);
stream_putc(pkt, extdata->protocol);
stream_putc(pkt, extdata->flags);
return (EIGRP_TLV1_EXTDATA_SIZE);
}
/**
* extract the vector metric from the TLV and put it into a usable form
*/
static uint16_t eigrp_tlv1_metric_decode(eigrp_stream_t *pkt,
eigrp_metrics_t *metric)
{
/* TLV1.2 provides metric in 32bit form, need to scale */
metric->delay = eigrp_scaled_to_delay(stream_getl(pkt));
metric->bandwidth = stream_getl(pkt);
metric->mtu[0] = stream_getc(pkt);
metric->mtu[1] = stream_getc(pkt);
metric->mtu[2] = stream_getc(pkt);
metric->hop_count = stream_getc(pkt);
metric->reliability = stream_getc(pkt);
metric->load = stream_getc(pkt);
metric->tag = stream_getc(pkt);
metric->flags = stream_getc(pkt);
return (EIGRP_TLV1_METRIC_SIZE);
}
static uint16_t eigrp_tlv1_metric_encode(eigrp_stream_t *pkt,
eigrp_metrics_t *metric)
{
/*
* TLV1.2 supports classic metrics, need to scale it down to 32 bits
*/
stream_putl(pkt, eigrp_delay_to_scaled(metric->delay));
stream_putl(pkt, metric->bandwidth);
stream_putc(pkt, metric->mtu[2]);
stream_putc(pkt, metric->mtu[1]);
stream_putc(pkt, metric->mtu[0]);
stream_putc(pkt, metric->hop_count);
stream_putc(pkt, metric->reliability);
stream_putc(pkt, metric->load);
stream_putc(pkt, metric->tag);
stream_putc(pkt, metric->flags);
return (EIGRP_TLV1_METRIC_SIZE);
}
static uint16_t eigrp_tlv1_addr_decode(eigrp_stream_t *pkt,
struct prefix *dest)
{
unsigned char prefixpart[4];
// DVS: wont work for v6
dest->family = AF_INET;
dest->prefixlen = stream_getc(pkt);
dest->prefixlen = (dest->prefixlen + 7) / 8;
switch (dest->prefixlen) {
case 1:
prefixpart[0] = stream_getc(pkt);
prefixpart[1] = 0;
prefixpart[2] = 0;
prefixpart[3] = 0;
break;
case 2:
prefixpart[0] = stream_getc(pkt);
prefixpart[1] = stream_getc(pkt);
prefixpart[2] = 0;
prefixpart[3] = 0;
break;
case 3:
prefixpart[0] = stream_getc(pkt);
prefixpart[1] = stream_getc(pkt);
prefixpart[2] = stream_getc(pkt);
prefixpart[3] = 0;
break;
case 4:
prefixpart[0] = stream_getc(pkt);
prefixpart[1] = stream_getc(pkt);
prefixpart[2] = stream_getc(pkt);
prefixpart[3] = stream_getc(pkt);
break;
default:
zlog_err("%s: Unexpected prefix length: %d",
__PRETTY_FUNCTION__, dest->prefixlen);
return 0;
}
dest->u.prefix4.s_addr = ((prefixpart[3] << 24) | (prefixpart[2] << 16)
| (prefixpart[1] << 8) | (prefixpart[0]));
return (dest->prefixlen + 1);
}
static uint16_t eigrp_tlv1_addr_encode(eigrp_stream_t *pkt,
eigrp_route_descriptor_t *route)
{
struct prefix *dest = route->prefix->destination; // DVS: move to route->dest
uint16_t prefixlen;
stream_putc(pkt, dest->prefixlen);
prefixlen = (dest->prefixlen + 7) / 8;
stream_putc(pkt, dest->u.prefix4.s_addr & 0xFF);
/* OK, i could add "((fallthrough)) to each of these, but thats less
* readable than replicating the lines. Plus the optimize will blow all
* this out anywat
*/
switch (prefixlen) {
case 4:
stream_putc(pkt, (dest->u.prefix4.s_addr >> 24) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr >> 16) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr >> 8) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr) & 0xFF);
break;
case 3:
stream_putc(pkt, (dest->u.prefix4.s_addr >> 16) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr >> 8) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr) & 0xFF);
break;
case 2:
stream_putc(pkt, (dest->u.prefix4.s_addr >> 8) & 0xFF);
stream_putc(pkt, (dest->u.prefix4.s_addr) & 0xFF);
break;
case 1:
stream_putc(pkt, (dest->u.prefix4.s_addr) & 0xFF);
break;
default:
zlog_err("%s: Unexpected prefix length: %d",
__PRETTY_FUNCTION__, prefixlen);
return 0;
}
return (prefixlen + 1);
}
static uint16_t eigrp_tlv1_nexthop_decode(eigrp_instance_t *eigrp,
eigrp_stream_t *pkt,
eigrp_route_descriptor_t *route)
{
// if doing no-nexthop-self, then use the of the source peer
if (/*eigrp->no_nextop_self == */ FALSE) {
route->nexthop.ip.v4.s_addr = stream_getl(pkt);
} else {
route->nexthop.ip.v4.s_addr = 0L;
}
return (EIGRP_TLV1_IPV4_NEXTHOP_SIZE);
}
static uint16_t eigrp_tlv1_nexthop_encode(eigrp_stream_t *pkt,
eigrp_route_descriptor_t *route)
{
stream_putl(pkt, route->nexthop.ip.v4.s_addr);
return (EIGRP_TLV1_IPV4_NEXTHOP_SIZE);
}
/**
* decode an incoming TLV into a topology route and return it for processing
*/
static eigrp_route_descriptor_t *eigrp_tlv1_decoder(eigrp_instance_t *eigrp,
eigrp_neighbor_t *nbr,
eigrp_stream_t *pkt,
uint16_t pktlen)
{
eigrp_route_descriptor_t *route = NULL;
uint16_t type, length;
uint16_t bytes = 0;
type = stream_getw(pkt);
length = stream_getw(pkt);
/* lets be sure we have at least enough info in the packet to
* process one TLV. if not, then no point in even trying
*/
if ((length > pktlen) || (length < EIGRP_IPV4_MIN_TLV)) {
if (IS_DEBUG_EIGRP_PACKET(0, RECV)) {
zlog_debug("EIGRP TLV: Neighbor(%s) corrupt packet",
eigrp_print_addr(&nbr->src));
}
return NULL;
}
/* allocate buffer */
route = eigrp_route_descriptor_new(nbr->ei);
if (!route) {
return NULL;
}
route->type = (type == EIGRP_TLV_IPv4_EXT) ? EIGRP_EXT : EIGRP_INT;
/* decode nexthop */
bytes += eigrp_tlv1_nexthop_decode(eigrp, pkt, route);
/* figure out what type of TLV we are processing */
switch (type) {
case EIGRP_TLV_IPv4_EXT:
bytes += eigrp_tlv1_external_decode(pkt, &route->extdata);
bytes += eigrp_tlv1_addr_decode(pkt, &route->dest);
break;
case EIGRP_TLV_IPv4_INT:
bytes += eigrp_tlv1_metric_decode(pkt, &route->metric);
/* metric and (optional) external info has been
* processed, now lets collect all the destination(s).
*
* NOTE:
* While the RFC calls out for the ability to send
* multiple destinations in one TLV, its never been
* implemented.
*
* BOGO: we should consider adding this at some pooint,
* but this is classic metrics, so its likely to never
* get implemented. For now, I am going to ignore any
* additional destintations afer the first one.
*/
bytes += eigrp_tlv1_addr_decode(pkt, &route->dest);
break;
default:
if (IS_DEBUG_EIGRP_PACKET(0, RECV)) {
zlog_debug(
"EIGRP TLV: Neighbor(%s): invalid TLV_type(%u)",
eigrp_print_addr(&nbr->src), type);
}
break;
}
return (route);
}
static uint16_t eigrp_tlv1_encoder(eigrp_instance_t *eigrp, eigrp_neighbor_t *nbr,
eigrp_stream_t *pkt,
eigrp_route_descriptor_t *route)
{
eigrp_interface_t *ei = nbr->ei;
size_t tlv_start = stream_get_getp(pkt);
size_t tlv_end;
uint16_t type, length = 0;
/* Filtering
* TODO: Work in progress
*/
if (ei) {
if (eigrp_update_prefix_apply(eigrp, ei, EIGRP_FILTER_OUT,
route->prefix->destination)) {
zlog_info(
"Prefix Filtered: Setting Metric to EIGRP_MAX_METRIC");
route->metric.delay = EIGRP_MAX_METRIC;
}
}
// need to find a better way to handle - use of stream_put is awarkward
//
// most likely i will convert stream data pointer to packed overlay
// structure.
//
type = route->type;
stream_putw(pkt, type);
length += 2;
stream_putw(pkt, length);
length += 2;
// encode nexthop
length += eigrp_tlv1_nexthop_encode(pkt, route);
switch (type) {
case EIGRP_TLV_IPv4_EXT:
length += eigrp_tlv1_external_encode(pkt, &route->extdata);
length += eigrp_tlv1_metric_encode(pkt, &route->metric);
length += eigrp_tlv1_addr_encode(pkt, route);
break;
case EIGRP_TLV_IPv4_INT:
length += eigrp_tlv1_metric_encode(pkt, &route->metric);
length += eigrp_tlv1_addr_encode(pkt, route);
break;
default:
if (IS_DEBUG_EIGRP_PACKET(0, RECV)) {
zlog_debug("Neighbor(%s): invalid TLV_type(%u)",
eigrp_print_addr(&nbr->src), type);
}
break;
}
// now the fix up...
tlv_end = stream_get_endp(pkt);
stream_set_getp(pkt, tlv_start);
stream_putw(pkt, type);
stream_putw(pkt, length);
stream_set_endp(pkt, tlv_end);
return (length);
}
void eigrp_tlv1_init(eigrp_neighbor_t *nbr)
{
// setup vectors for processing Version 1 TLVs
nbr->tlv_decoder = &eigrp_tlv1_decoder;
nbr->tlv_encoder = &eigrp_tlv1_encoder;
}