-
Notifications
You must be signed in to change notification settings - Fork 1
/
eigrp_neighbor.c
348 lines (300 loc) · 8.26 KB
/
eigrp_neighbor.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* EIGRP Neighbor Handling.
* Copyright (C) 2013-2016
* Authors:
* Donnie Savage
* Jan Janovic
* Matej Perina
* Peter Orsag
* Peter Paluch
* Frantisek Gazo
* Tomas Hvorkovy
* Martin Kontsek
* Lukas Koribsky
*/
#include "eigrpd/eigrpd.h"
#include "eigrpd/eigrp_structs.h"
#include "eigrpd/eigrp_neighbor.h"
#include "eigrpd/eigrp_packet.h"
#include "eigrpd/eigrp_network.h"
#include "eigrpd/eigrp_topology.h"
#include "eigrpd/eigrp_zebra.h"
DEFINE_MTYPE_STATIC(EIGRPD, EIGRP_NEIGHBOR, "EIGRP neighbor");
static inline eigrp_route_descriptor_t *
eigrp_tlv_decoder_safe(eigrp_instance_t *eigrp, eigrp_neighbor_t *nbr,
eigrp_stream_t *pkt, uint16_t pktlen)
{
return NULL;
}
static inline uint16_t eigrp_tlv_encoder_safe(eigrp_instance_t *eigrp,
eigrp_neighbor_t *nbr,
eigrp_stream_t *pkt,
eigrp_route_descriptor_t *route)
{
return 0;
}
/**
* initalize neighbor
*/
static void eigrp_nbr_init(eigrp_neighbor_t *nbr, eigrp_addr_t *src)
{
eigrp_addr_copy(&nbr->src, src);
/* copy over the values passed in by the neighbor */
nbr->K1 = EIGRP_K1_DEFAULT;
nbr->K2 = EIGRP_K2_DEFAULT;
nbr->K3 = EIGRP_K3_DEFAULT;
nbr->K4 = EIGRP_K4_DEFAULT;
nbr->K5 = EIGRP_K5_DEFAULT;
nbr->K6 = EIGRP_K6_DEFAULT;
nbr->v_holddown = EIGRP_HOLD_INTERVAL_DEFAULT;
nbr->tlv_decoder = (eigrp_tlv_decoder_t)&eigrp_tlv_decoder_safe;
nbr->tlv_encoder = (eigrp_tlv_encoder_t)&eigrp_tlv_encoder_safe;
// if (IS_DEBUG_EIGRP_EVENT)
// zlog_debug("NSM[%s:%s]: start", EIGRP_INTF_NAME (nbr->oi),
// eigrp_print_routerid(nbr->router_id));
}
/**
* Create a new neighbor structure and initalize it.
*/
eigrp_neighbor_t *eigrp_nbr_create(eigrp_interface_t *ei, eigrp_addr_t *src)
{
eigrp_neighbor_t *nbr;
/* Allcate new neighbor. */
nbr = XCALLOC(MTYPE_EIGRP_NEIGHBOR, sizeof(eigrp_neighbor_t));
/* Relate neighbor to the interface. */
nbr->ei = ei;
/* Set default values. */
eigrp_nbr_init(nbr, src);
eigrp_nbr_state_set(nbr, EIGRP_NEIGHBOR_DOWN);
// If this is the 'self' neighbor, then you dont have an interface
if (ei) {
listnode_add(ei->nbrs, nbr);
}
return nbr;
}
eigrp_neighbor_t *eigrp_nbr_lookup(eigrp_interface_t *ei, eigrp_header_t *eigrph,
eigrp_addr_t *src)
{
eigrp_neighbor_t *nbr;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
if (eigrp_addr_same(src, &nbr->src)) {
return nbr;
}
}
return NULL;
}
/**
* @fn eigrp_nbr_lookup_by_addr
*
* @param[in] ei EIGRP interface
* @param[in] nbr_addr Address of neighbor
*
* @return void
*
* @par
* Function is used for neighbor lookup by address
* in specified interface.
*/
eigrp_neighbor_t *eigrp_nbr_lookup_by_addr(eigrp_interface_t *ei,
struct in_addr *addr)
{
eigrp_neighbor_t *nbr;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
if (addr->s_addr == nbr->src.ip.v4.s_addr) {
return nbr;
}
}
return NULL;
}
/**
* @fn eigrp_nbr_lookup_by_addr_process
*
* @param[in] eigrp EIGRP process
* @param[in] nbr_addr Address of neighbor
*
* @return void
*
* @par
* Function is used for neighbor lookup by address
* in whole EIGRP process.
*/
eigrp_neighbor_t *eigrp_nbr_lookup_by_addr_process(eigrp_instance_t *eigrp,
struct in_addr nbr_addr)
{
eigrp_interface_t *ei;
struct listnode *node, *node2, *nnode2;
eigrp_neighbor_t *nbr;
/* iterate over all eigrp interfaces */
for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
/* iterate over all neighbors on eigrp interface */
for (ALL_LIST_ELEMENTS(ei->nbrs, node2, nnode2, nbr)) {
/* compare if neighbor address is same as arg address */
if (nbr->src.ip.v4.s_addr == nbr_addr.s_addr) {
return nbr;
}
}
}
return NULL;
}
/* Delete specified EIGRP neighbor from interface. */
void eigrp_nbr_delete(eigrp_neighbor_t *nbr)
{
eigrp_nbr_state_set(nbr, EIGRP_NEIGHBOR_DOWN);
if (nbr->ei)
eigrp_topology_neighbor_down(nbr->ei->eigrp, nbr);
/* Cancel all events. */ /* Event lookup cost would be negligible. */
event_cancel_event(eigrpd_event, nbr);
eigrp_packet_queue_free(nbr->multicast_queue);
eigrp_packet_queue_free(nbr->retrans_queue);
EVENT_OFF(nbr->t_holddown);
if (nbr->ei)
listnode_delete(nbr->ei->nbrs, nbr);
XFREE(MTYPE_EIGRP_NEIGHBOR, nbr);
}
void holddown_timer_expired(struct event *event)
{
eigrp_neighbor_t *nbr = EVENT_ARG(event);
eigrp_instance_t *eigrp = nbr->ei->eigrp;
zlog_info("Neighbor %s (%s) is down: holding time expired",
eigrp_print_addr(&nbr->src),
ifindex2ifname(nbr->ei->ifp->ifindex, eigrp->vrf_id));
nbr->state = EIGRP_NEIGHBOR_DOWN;
eigrp_nbr_delete(nbr);
return;
}
uint8_t eigrp_nbr_state_get(eigrp_neighbor_t *nbr)
{
return (nbr->state);
}
void eigrp_nbr_state_set(eigrp_neighbor_t *nbr, uint8_t state)
{
nbr->state = state;
if (eigrp_nbr_state_get(nbr) == EIGRP_NEIGHBOR_DOWN) {
// reset all the seq/ack counters
nbr->recv_sequence_number = 0;
nbr->init_sequence_number = 0;
nbr->retrans_counter = 0;
// Kvalues
nbr->K1 = EIGRP_K1_DEFAULT;
nbr->K2 = EIGRP_K2_DEFAULT;
nbr->K3 = EIGRP_K3_DEFAULT;
nbr->K4 = EIGRP_K4_DEFAULT;
nbr->K5 = EIGRP_K5_DEFAULT;
nbr->K6 = EIGRP_K6_DEFAULT;
// hold time..
nbr->v_holddown = EIGRP_HOLD_INTERVAL_DEFAULT;
EVENT_OFF(nbr->t_holddown);
/* out with the old */
if (nbr->multicast_queue)
eigrp_packet_queue_free(nbr->multicast_queue);
if (nbr->retrans_queue)
eigrp_packet_queue_free(nbr->retrans_queue);
/* in with the new */
nbr->retrans_queue = eigrp_packet_queue_new();
nbr->multicast_queue = eigrp_packet_queue_new();
nbr->crypt_seqnum = 0;
}
}
const char *eigrp_nbr_state_str(eigrp_neighbor_t *nbr)
{
const char *state;
switch (nbr->state) {
case EIGRP_NEIGHBOR_DOWN:
state = "Down";
break;
case EIGRP_NEIGHBOR_PENDING:
state = "Waiting for Init";
break;
case EIGRP_NEIGHBOR_UP:
state = "Up";
break;
default:
state = "Unknown";
break;
}
return (state);
}
void eigrp_nbr_state_update(eigrp_neighbor_t *nbr)
{
switch (nbr->state) {
case EIGRP_NEIGHBOR_DOWN: {
/*Start Hold Down Timer for neighbor*/
// EVENT_OFF(nbr->t_holddown);
// EVENT_TIMER_ON(eigrpd_event, nbr->t_holddown,
// holddown_timer_expired,
// nbr, nbr->v_holddown);
break;
}
case EIGRP_NEIGHBOR_PENDING: {
/*Reset Hold Down Timer for neighbor*/
EVENT_OFF(nbr->t_holddown);
event_add_timer(eigrpd_event, holddown_timer_expired, nbr,
nbr->v_holddown, &nbr->t_holddown);
break;
}
case EIGRP_NEIGHBOR_UP: {
/*Reset Hold Down Timer for neighbor*/
EVENT_OFF(nbr->t_holddown);
event_add_timer(eigrpd_event, holddown_timer_expired, nbr,
nbr->v_holddown, &nbr->t_holddown);
break;
}
}
}
int eigrp_nbr_count_get(eigrp_instance_t *eigrp)
{
eigrp_interface_t *iface;
struct listnode *node, *node2, *nnode2;
eigrp_neighbor_t *nbr;
uint32_t counter;
counter = 0;
for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, iface)) {
for (ALL_LIST_ELEMENTS(iface->nbrs, node2, nnode2, nbr)) {
if (nbr->state == EIGRP_NEIGHBOR_UP) {
counter++;
}
}
}
return counter;
}
/**
* @fn eigrp_nbr_hard_restart
*
* @param[in] nbr Neighbor who would receive hard restart
* @param[in] vty Virtual terminal for log output
* @return void
*
* @par
* Function used for executing hard restart for neighbor:
* Send Hello packet with Peer Termination TLV with
* neighbor's address, set it's state to DOWN and delete the neighbor
*/
void eigrp_nbr_hard_restart(eigrp_instance_t *eigrp, eigrp_neighbor_t *nbr,
struct vty *vty)
{
zlog_debug("Neighbor %s (%s) is down: manually cleared",
eigrp_print_addr(&nbr->src),
ifindex2ifname(nbr->ei->ifp->ifindex, eigrp->vrf_id));
if (vty != NULL) {
vty_time_print(vty, 0);
vty_out(vty, "Neighbor %s (%s) is down: manually cleared\n",
eigrp_print_addr(&nbr->src),
ifindex2ifname(nbr->ei->ifp->ifindex, eigrp->vrf_id));
}
/* send Hello with Peer Termination TLV */
eigrp_hello_send(nbr->ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN_NBR, &nbr->src);
/* set neighbor to DOWN */
nbr->state = EIGRP_NEIGHBOR_DOWN;
/* delete neighbor */
eigrp_nbr_delete(nbr);
}
int eigrp_nbr_split_horizon_check(eigrp_route_descriptor_t *erd,
eigrp_interface_t *ei)
{
if (erd->distance == EIGRP_MAX_METRIC)
return 0;
return (erd->ei == ei);
}