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
|
/*
* EIGRP Topology Table.
* Copyright (C) 2013-2016
* Authors:
* Donnie Savage
* Jan Janovic
* Matej Perina
* Peter Orsag
* Peter Paluch
* Frantisek Gazo
* Tomas Hvorkovy
* Martin Kontsek
* Lukas Koribsky
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "log.h"
#include "linklist.h"
#include "vty.h"
#include "eigrpd/eigrp_structs.h"
#include "eigrpd/eigrpd.h"
#include "eigrpd/eigrp_interface.h"
#include "eigrpd/eigrp_neighbor.h"
#include "eigrpd/eigrp_packet.h"
#include "eigrpd/eigrp_zebra.h"
#include "eigrpd/eigrp_vty.h"
#include "eigrpd/eigrp_network.h"
#include "eigrpd/eigrp_dump.h"
#include "eigrpd/eigrp_topology.h"
#include "eigrpd/eigrp_fsm.h"
#include "eigrpd/eigrp_memory.h"
static int
eigrp_prefix_entry_cmp(struct eigrp_prefix_entry *, struct eigrp_prefix_entry *);
static void
eigrp_prefix_entry_del(struct eigrp_prefix_entry *);
static int
eigrp_neighbor_entry_cmp(struct eigrp_neighbor_entry *,
struct eigrp_neighbor_entry *);
/*
* asdf;laksdjf;lajsdf;kasdjf;asdjf;
* asdfaskdjfa;sdkjf;adlskj
* Returns linkedlist used as topology table
* cmp - assigned function for comparing topology nodes
* del - assigned function executed before deleting topology node by list function
*/
struct list *
eigrp_topology_new()
{
struct list* new = list_new();
new->cmp = (int
(*)(void *, void *)) eigrp_prefix_entry_cmp;
new->del = (void
(*)(void *)) eigrp_prefix_entry_del;
return new;
}
/*
* Topology node comparison
*/
static int
eigrp_prefix_entry_cmp(struct eigrp_prefix_entry *node1,
struct eigrp_prefix_entry *node2)
{
if (node1->af == AF_INET)
{
if (node2->af == AF_INET)
{
if (node1->destination_ipv4->prefix.s_addr
< node2->destination_ipv4->prefix.s_addr)
{
return -1; // if it belong above node2
}
else
{
if (node1->destination_ipv4->prefix.s_addr
> node2->destination_ipv4->prefix.s_addr)
{
return 1; //if it belongs under node2
}
else
{
return 0; // same value... ERROR...in case of adding same prefix again
}
}
}
else
{
return 1;
}
}
else
{ // TODO check if the prefix dont exists
return 1; // add to end
}
}
/*
* Topology node delete
*/
static void
eigrp_prefix_entry_del(struct eigrp_prefix_entry *node)
{
list_delete_all_node(node->entries);
list_free(node->entries);
}
/*
* Returns new created toplogy node
* cmp - assigned function for comparing topology entry
*/
struct eigrp_prefix_entry *
eigrp_prefix_entry_new()
{
struct eigrp_prefix_entry *new;
new = XCALLOC(MTYPE_EIGRP_PREFIX_ENTRY, sizeof(struct eigrp_prefix_entry));
new->entries = list_new();
new->rij = list_new();
new->entries->cmp = (int
(*)(void *, void *)) eigrp_neighbor_entry_cmp;
new->distance = new->fdistance = new->rdistance = EIGRP_MAX_METRIC;
new->destination_ipv4 = NULL;
new->destination_ipv6 = NULL;
return new;
}
/*
* Topology entry comparison
*/
static int
eigrp_neighbor_entry_cmp(struct eigrp_neighbor_entry *entry1,
struct eigrp_neighbor_entry *entry2)
{
if (entry1->distance < entry2->distance) // parameter used in list_add_sort ()
return -1; // actually set to sort by distance
if (entry1->distance > entry2->distance)
return 1;
return 0;
}
/*
* Returns new topology entry
*/
struct eigrp_neighbor_entry *
eigrp_neighbor_entry_new()
{
struct eigrp_neighbor_entry *new;
new = XCALLOC(MTYPE_EIGRP_NEIGHBOR_ENTRY,
sizeof(struct eigrp_neighbor_entry));
new->reported_distance = EIGRP_MAX_METRIC;
new->distance = EIGRP_MAX_METRIC;
return new;
}
/*
* Freeing topology table list
*/
void
eigrp_topology_free(struct list *list)
{
list_free(list);
}
/*
* Deleting all topology nodes in table
*/
void
eigrp_topology_cleanup(struct list *topology)
{
assert(topology);
eigrp_topology_delete_all(topology);
}
/*
* Adding topology node to topology table
*/
void
eigrp_prefix_entry_add(struct list *topology, struct eigrp_prefix_entry *node)
{
if (listnode_lookup(topology, node) == NULL)
{
listnode_add_sort(topology, node);
}
}
/*
* Adding topology entry to topology node
*/
void
eigrp_neighbor_entry_add(struct eigrp_prefix_entry *node,
struct eigrp_neighbor_entry *entry)
{
if (listnode_lookup(node->entries, entry) == NULL)
{
listnode_add_sort(node->entries, entry);
entry->prefix = node;
}
}
/*
* Deleting topology node from topology table
*/
void
eigrp_prefix_entry_delete(struct list *topology,
struct eigrp_prefix_entry *node)
{
if (listnode_lookup(topology, node) != NULL)
{
list_delete_all_node(node->entries);
list_free(node->entries);
list_free(node->rij);
listnode_delete(topology, node);
XFREE(MTYPE_EIGRP_PREFIX_ENTRY,node);
}
}
/*
* Deleting topology entry from topology node
*/
void
eigrp_neighbor_entry_delete(struct eigrp_prefix_entry *node,
struct eigrp_neighbor_entry *entry)
{
if (listnode_lookup(node->entries, entry) != NULL)
{
listnode_delete(node->entries, entry);
XFREE(MTYPE_EIGRP_NEIGHBOR_ENTRY,entry);
}
}
/*
* Deleting all nodes from topology table
*/
void
eigrp_topology_delete_all(struct list *topology)
{
list_delete_all_node(topology);
}
/*
* Return 0 if topology is not empty
* otherwise return 1
*/
unsigned int
eigrp_topology_table_isempty(struct list *topology)
{
if (topology->count)
return 1;
else
return 0;
}
struct eigrp_prefix_entry *
eigrp_topology_table_lookup_ipv4(struct list *topology_table,
struct prefix_ipv4 * address)
{
struct eigrp_prefix_entry *data;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(topology_table, node, data))
{
if ((data->af == AF_INET)
&& (data->destination_ipv4->prefix.s_addr == address->prefix.s_addr)
&& (data->destination_ipv4->prefixlen == address->prefixlen))
return data;
}
return NULL;
}
/* TODO
struct eigrp_prefix_entry *
eigrp_topology_table_lookup_ipv6 (struct list *topology_table,
struct prefix_ipv6 * address)
{
struct eigrp_prefix_entry *data;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS (topology_table, node, nnode, data))
{
if (comparison)
return data;
}
return NULL;
}
*/
struct list *
eigrp_topology_get_successor(struct eigrp_prefix_entry *table_node)
{
struct list *successors = list_new();
;
struct eigrp_neighbor_entry *data;
struct listnode *node1, *node2;
for (ALL_LIST_ELEMENTS(table_node->entries, node1, node2, data))
{
if (data->flags & EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG)
{
listnode_add(successors, data);
}
}
return successors;
}
/*extern struct eigrp_neighbor_entry *
eigrp_topology_get_fsuccessor (struct eigrp_prefix_entry *table_node)
{
struct eigrp_neighbor_entry *data;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS (table_node->entries, node, nnode, data))
{
if ((data->flags & EIGRP_NEIGHBOR_ENTRY_FSUCCESSOR_FLAG) == 1)
{
return data;
}
}
return NULL;
}*/
struct eigrp_neighbor_entry *
eigrp_prefix_entry_lookup(struct list *entries, struct eigrp_neighbor *nbr)
{
struct eigrp_neighbor_entry *data;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(entries, node, nnode, data))
{
if (data->adv_router == nbr)
{
return data;
}
}
return NULL;
}
/* Lookup all prefixes from specified neighbor */
struct list *
eigrp_neighbor_prefixes_lookup(struct eigrp *eigrp, struct eigrp_neighbor *nbr)
{
struct listnode *node1, *node11, *node2, *node22;
struct eigrp_prefix_entry *prefix;
struct eigrp_neighbor_entry *entry;
/* create new empty list for prefixes storage */
struct list *prefixes = list_new();
/* iterate over all prefixes in topology table */
for (ALL_LIST_ELEMENTS(eigrp->topology_table, node1, node11, prefix))
{
/* iterate over all neighbor entry in prefix */
for (ALL_LIST_ELEMENTS(prefix->entries, node2, node22, entry))
{
/* if entry is from specified neighbor, add to list */
if (entry->adv_router == nbr)
{
listnode_add(prefixes, prefix);
}
}
}
/* return list of prefixes from specified neighbor */
return prefixes;
}
int
eigrp_topology_update_distance(struct eigrp_fsm_action_message *msg)
{
struct eigrp *eigrp = msg->eigrp;
struct eigrp_prefix_entry *prefix = msg->prefix;
struct eigrp_neighbor_entry *entry = msg->entry;
int change = 0;
assert(entry);
struct TLV_IPv4_External_type *ext_data = NULL;
struct TLV_IPv4_Internal_type *int_data = NULL;
if (msg->data_type == EIGRP_TLV_IPv4_INT)
{
int_data = msg->data.ipv4_int_type;
if (eigrp_metrics_is_same(&int_data->metric,&entry->reported_metric))
{
return 0; // No change
}
change =
entry->reported_distance
< eigrp_calculate_metrics(eigrp, &int_data->metric) ? 1 :
entry->reported_distance
> eigrp_calculate_metrics(eigrp, &int_data->metric) ? 2 : 3; // Increase : Decrease : No change
entry->reported_metric = int_data->metric;
entry->reported_distance = eigrp_calculate_metrics(eigrp,
&int_data->metric);
entry->distance = eigrp_calculate_total_metrics(eigrp, entry);
}
else
{
ext_data = msg->data.ipv4_ext_data;
if (eigrp_metrics_is_same (&ext_data->metric, &entry->reported_metric))
return 0;
}
/*
* Move to correct position in list according to new distance
*/
listnode_delete(prefix->entries, entry);
listnode_add_sort(prefix->entries, entry);
return change;
}
void
eigrp_topology_update_all_node_flags(struct eigrp *eigrp)
{
struct list *table = eigrp->topology_table;
struct eigrp_prefix_entry *data;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(table, node, nnode, data))
{
eigrp_topology_update_node_flags(data);
}
}
void
eigrp_topology_update_node_flags(struct eigrp_prefix_entry *dest)
{
struct listnode *node;
struct eigrp_neighbor_entry *entry;
struct eigrp * eigrp = eigrp_lookup();
for (ALL_LIST_ELEMENTS_RO(dest->entries, node, entry))
{
if ((entry->distance <= (u_int64_t)(dest->distance*eigrp->variance)) && entry->distance != EIGRP_MAX_METRIC) // is successor
{
entry->flags |= EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG;
entry->flags &= 0xfd; // 1111 1101 set fs flag to zero
}
else if (entry->reported_distance < dest->fdistance) // is feasible successor
{
entry->flags |= EIGRP_NEIGHBOR_ENTRY_FSUCCESSOR_FLAG;
entry->flags &= 0xfe; // 1111 1110 set successor flag to zero
}
else
{
entry->flags &= 0xfc; // 1111 1100 set successor and fs flag to zero
}
}
}
void
eigrp_update_routing_table(struct eigrp_prefix_entry * prefix)
{
struct listnode *node;
struct eigrp_neighbor_entry *entry;
for (ALL_LIST_ELEMENTS_RO(prefix->entries, node, entry))
{
if (entry->flags & EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG)
{
if (!(entry->flags & EIGRP_NEIGHBOR_ENTRY_INTABLE_FLAG))
{
eigrp_zebra_route_add(prefix->destination_ipv4, entry);
entry->flags += EIGRP_NEIGHBOR_ENTRY_INTABLE_FLAG;
}
}
else if (entry->flags & EIGRP_NEIGHBOR_ENTRY_INTABLE_FLAG)
{
eigrp_zebra_route_delete(prefix->destination_ipv4, entry);
entry->flags -= EIGRP_NEIGHBOR_ENTRY_INTABLE_FLAG;
}
}
}
void
eigrp_topology_neighbor_down(struct eigrp *eigrp, struct eigrp_neighbor * nbr)
{
struct listnode *node1, *node11, *node2, *node22;
struct eigrp_prefix_entry *prefix;
struct eigrp_neighbor_entry *entry;
for (ALL_LIST_ELEMENTS(eigrp->topology_table, node1, node11, prefix))
{
for (ALL_LIST_ELEMENTS(prefix->entries, node2, node22, entry))
{
if (entry->adv_router == nbr)
{
struct eigrp_fsm_action_message *msg;
msg = XCALLOC(MTYPE_EIGRP_FSM_MSG,
sizeof(struct eigrp_fsm_action_message));
struct TLV_IPv4_Internal_type * tlv = eigrp_IPv4_InternalTLV_new();
tlv->metric.delay = EIGRP_MAX_METRIC;
msg->packet_type = EIGRP_OPC_UPDATE;
msg->eigrp = eigrp;
msg->data_type = EIGRP_TLV_IPv4_INT;
msg->adv_router = nbr;
msg->data.ipv4_int_type = tlv;
msg->entry = entry;
msg->prefix = prefix;
int event = eigrp_get_fsm_event(msg);
eigrp_fsm_event(msg, event);
}
}
}
eigrp_query_send_all(eigrp);
eigrp_update_send_all(eigrp,nbr->ei);
}
void
eigrp_update_topology_table_prefix(struct list * table, struct eigrp_prefix_entry * prefix)
{
struct listnode *node1, *node2;
struct eigrp_neighbor_entry *entry;
for (ALL_LIST_ELEMENTS(prefix->entries, node1, node2, entry))
{
if(entry->distance == EIGRP_MAX_METRIC)
{
eigrp_neighbor_entry_delete(prefix,entry);
}
}
if(prefix->distance == EIGRP_MAX_METRIC && prefix->nt != EIGRP_TOPOLOGY_TYPE_CONNECTED)
{
eigrp_prefix_entry_delete(table,prefix);
}
}
/*int
eigrp_topology_get_successor_count (struct eigrp_prefix_entry *prefix)
{
struct listnode *node;
struct eigrp_neighbor_entry *entry;
int count = 0;
for (ALL_LIST_ELEMENTS_RO (prefix->entries,node,entry))
{
if ((entry->flags & EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG) == EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG)
{
count ++;
}
}
return count;
}
*/
|