]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_neighbor.c
Merge pull request #805 from Orange-OpenSource/master
[mirror_frr.git] / eigrpd / eigrp_neighbor.c
1 /*
2 * EIGRP Neighbor Handling.
3 * Copyright (C) 2013-2016
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 * Frantisek Gazo
11 * Tomas Hvorkovy
12 * Martin Kontsek
13 * Lukas Koribsky
14 *
15 * This file is part of GNU Zebra.
16 *
17 * GNU Zebra is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2, or (at your option) any
20 * later version.
21 *
22 * GNU Zebra is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; see the file COPYING; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32 #include <zebra.h>
33
34 #include "linklist.h"
35 #include "prefix.h"
36 #include "memory.h"
37 #include "command.h"
38 #include "thread.h"
39 #include "stream.h"
40 #include "table.h"
41 #include "log.h"
42 #include "keychain.h"
43 #include "vty.h"
44
45 #include "eigrpd/eigrp_structs.h"
46 #include "eigrpd/eigrpd.h"
47 #include "eigrpd/eigrp_interface.h"
48 #include "eigrpd/eigrp_neighbor.h"
49 #include "eigrpd/eigrp_dump.h"
50 #include "eigrpd/eigrp_packet.h"
51 #include "eigrpd/eigrp_zebra.h"
52 #include "eigrpd/eigrp_vty.h"
53 #include "eigrpd/eigrp_network.h"
54 #include "eigrpd/eigrp_topology.h"
55 #include "eigrpd/eigrp_memory.h"
56
57 struct eigrp_neighbor *eigrp_nbr_new(struct eigrp_interface *ei)
58 {
59 struct eigrp_neighbor *nbr;
60
61 /* Allcate new neighbor. */
62 nbr = XCALLOC(MTYPE_EIGRP_NEIGHBOR, sizeof(struct eigrp_neighbor));
63
64 /* Relate neighbor to the interface. */
65 nbr->ei = ei;
66
67 /* Set default values. */
68 eigrp_nbr_state_set(nbr, EIGRP_NEIGHBOR_DOWN);
69
70 return nbr;
71 }
72
73 /**
74 *@fn void dissect_eigrp_sw_version (tvbuff_t *tvb, proto_tree *tree,
75 * proto_item *ti)
76 *
77 * @par
78 * Create a new neighbor structure and initalize it.
79 */
80 static struct eigrp_neighbor *eigrp_nbr_add(struct eigrp_interface *ei,
81 struct eigrp_header *eigrph,
82 struct ip *iph)
83 {
84 struct eigrp_neighbor *nbr;
85
86 nbr = eigrp_nbr_new(ei);
87 nbr->src = iph->ip_src;
88
89 // if (IS_DEBUG_EIGRP_EVENT)
90 // zlog_debug("NSM[%s:%s]: start", IF_NAME (nbr->oi),
91 // inet_ntoa (nbr->router_id));
92
93 return nbr;
94 }
95
96 struct eigrp_neighbor *eigrp_nbr_get(struct eigrp_interface *ei,
97 struct eigrp_header *eigrph,
98 struct ip *iph)
99 {
100 struct eigrp_neighbor *nbr;
101 struct listnode *node, *nnode;
102
103 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
104 if (iph->ip_src.s_addr == nbr->src.s_addr) {
105 return nbr;
106 }
107 }
108
109 nbr = eigrp_nbr_add(ei, eigrph, iph);
110 listnode_add(ei->nbrs, nbr);
111
112 return nbr;
113 }
114
115 /**
116 * @fn eigrp_nbr_lookup_by_addr
117 *
118 * @param[in] ei EIGRP interface
119 * @param[in] nbr_addr Address of neighbor
120 *
121 * @return void
122 *
123 * @par
124 * Function is used for neighbor lookup by address
125 * in specified interface.
126 */
127 struct eigrp_neighbor *eigrp_nbr_lookup_by_addr(struct eigrp_interface *ei,
128 struct in_addr *addr)
129 {
130 struct eigrp_neighbor *nbr;
131 struct listnode *node, *nnode;
132
133 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
134 if (addr->s_addr == nbr->src.s_addr) {
135 return nbr;
136 }
137 }
138
139 return NULL;
140 }
141
142 /**
143 * @fn eigrp_nbr_lookup_by_addr_process
144 *
145 * @param[in] eigrp EIGRP process
146 * @param[in] nbr_addr Address of neighbor
147 *
148 * @return void
149 *
150 * @par
151 * Function is used for neighbor lookup by address
152 * in whole EIGRP process.
153 */
154 struct eigrp_neighbor *eigrp_nbr_lookup_by_addr_process(struct eigrp *eigrp,
155 struct in_addr nbr_addr)
156 {
157 struct eigrp_interface *ei;
158 struct listnode *node, *node2, *nnode2;
159 struct eigrp_neighbor *nbr;
160
161 /* iterate over all eigrp interfaces */
162 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
163 /* iterate over all neighbors on eigrp interface */
164 for (ALL_LIST_ELEMENTS(ei->nbrs, node2, nnode2, nbr)) {
165 /* compare if neighbor address is same as arg address */
166 if (nbr->src.s_addr == nbr_addr.s_addr) {
167 return nbr;
168 }
169 }
170 }
171
172 return NULL;
173 }
174
175
176 /* Delete specified EIGRP neighbor from interface. */
177 void eigrp_nbr_delete(struct eigrp_neighbor *nbr)
178 {
179 eigrp_nbr_state_set(nbr, EIGRP_NEIGHBOR_DOWN);
180 if (nbr->ei)
181 eigrp_topology_neighbor_down(nbr->ei->eigrp, nbr);
182
183 /* Cancel all events. */ /* Thread lookup cost would be negligible. */
184 thread_cancel_event(master, nbr);
185 eigrp_fifo_free(nbr->multicast_queue);
186 eigrp_fifo_free(nbr->retrans_queue);
187 THREAD_OFF(nbr->t_holddown);
188
189 if (nbr->ei)
190 listnode_delete(nbr->ei->nbrs, nbr);
191 XFREE(MTYPE_EIGRP_NEIGHBOR, nbr);
192 }
193
194 int holddown_timer_expired(struct thread *thread)
195 {
196 struct eigrp_neighbor *nbr;
197
198 nbr = THREAD_ARG(thread);
199
200 zlog_info("Neighbor %s (%s) is down: holding time expired",
201 inet_ntoa(nbr->src),
202 ifindex2ifname(nbr->ei->ifp->ifindex, VRF_DEFAULT));
203 nbr->state = EIGRP_NEIGHBOR_DOWN;
204 eigrp_nbr_delete(nbr);
205
206 return 0;
207 }
208
209 u_char eigrp_nbr_state_get(struct eigrp_neighbor *nbr)
210 {
211 return (nbr->state);
212 }
213
214 void eigrp_nbr_state_set(struct eigrp_neighbor *nbr, u_char state)
215 {
216 nbr->state = state;
217
218 if (eigrp_nbr_state_get(nbr) == EIGRP_NEIGHBOR_DOWN) {
219 // reset all the seq/ack counters
220 nbr->recv_sequence_number = 0;
221 nbr->init_sequence_number = 0;
222 nbr->retrans_counter = 0;
223
224 // Kvalues
225 nbr->K1 = EIGRP_K1_DEFAULT;
226 nbr->K2 = EIGRP_K2_DEFAULT;
227 nbr->K3 = EIGRP_K3_DEFAULT;
228 nbr->K4 = EIGRP_K4_DEFAULT;
229 nbr->K5 = EIGRP_K5_DEFAULT;
230 nbr->K6 = EIGRP_K6_DEFAULT;
231
232 // hold time..
233 nbr->v_holddown = EIGRP_HOLD_INTERVAL_DEFAULT;
234 THREAD_OFF(nbr->t_holddown);
235
236 /* out with the old */
237 if (nbr->multicast_queue)
238 eigrp_fifo_free(nbr->multicast_queue);
239 if (nbr->retrans_queue)
240 eigrp_fifo_free(nbr->retrans_queue);
241
242 /* in with the new */
243 nbr->retrans_queue = eigrp_fifo_new();
244 nbr->multicast_queue = eigrp_fifo_new();
245
246 nbr->crypt_seqnum = 0;
247 }
248 }
249
250 const char *eigrp_nbr_state_str(struct eigrp_neighbor *nbr)
251 {
252 const char *state;
253 switch (nbr->state) {
254 case EIGRP_NEIGHBOR_DOWN:
255 state = "Down";
256 break;
257 case EIGRP_NEIGHBOR_PENDING:
258 state = "Waiting for Init";
259 break;
260 case EIGRP_NEIGHBOR_UP:
261 state = "Up";
262 break;
263 default:
264 state = "Unknown";
265 break;
266 }
267
268 return (state);
269 }
270
271 void eigrp_nbr_state_update(struct eigrp_neighbor *nbr)
272 {
273 switch (nbr->state) {
274 case EIGRP_NEIGHBOR_DOWN: {
275 /*Start Hold Down Timer for neighbor*/
276 // THREAD_OFF(nbr->t_holddown);
277 // THREAD_TIMER_ON(master, nbr->t_holddown,
278 // holddown_timer_expired,
279 // nbr, nbr->v_holddown);
280 break;
281 }
282 case EIGRP_NEIGHBOR_PENDING: {
283 /*Reset Hold Down Timer for neighbor*/
284 THREAD_OFF(nbr->t_holddown);
285 thread_add_timer(master, holddown_timer_expired, nbr,
286 nbr->v_holddown, &nbr->t_holddown);
287 break;
288 }
289 case EIGRP_NEIGHBOR_UP: {
290 /*Reset Hold Down Timer for neighbor*/
291 THREAD_OFF(nbr->t_holddown);
292 thread_add_timer(master, holddown_timer_expired, nbr,
293 nbr->v_holddown, &nbr->t_holddown);
294 break;
295 }
296 }
297 }
298
299 int eigrp_nbr_count_get(void)
300 {
301 struct eigrp_interface *iface;
302 struct listnode *node, *node2, *nnode2;
303 struct eigrp_neighbor *nbr;
304 struct eigrp *eigrp = eigrp_lookup();
305 u_int32_t counter;
306
307 if (eigrp == NULL) {
308 zlog_debug("EIGRP Routing Process not enabled");
309 return 0;
310 }
311
312 counter = 0;
313 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, iface)) {
314 for (ALL_LIST_ELEMENTS(iface->nbrs, node2, nnode2, nbr)) {
315 if (nbr->state == EIGRP_NEIGHBOR_UP) {
316 counter++;
317 }
318 }
319 }
320 return counter;
321 }
322
323 /**
324 * @fn eigrp_nbr_hard_restart
325 *
326 * @param[in] nbr Neighbor who would receive hard restart
327 * @param[in] vty Virtual terminal for log output
328 * @return void
329 *
330 * @par
331 * Function used for executing hard restart for neighbor:
332 * Send Hello packet with Peer Termination TLV with
333 * neighbor's address, set it's state to DOWN and delete the neighbor
334 */
335 void eigrp_nbr_hard_restart(struct eigrp_neighbor *nbr, struct vty *vty)
336 {
337 if (nbr == NULL) {
338 zlog_err("Nbr Hard restart: Neighbor not specified.");
339 return;
340 }
341
342 zlog_debug("Neighbor %s (%s) is down: manually cleared",
343 inet_ntoa(nbr->src),
344 ifindex2ifname(nbr->ei->ifp->ifindex, VRF_DEFAULT));
345 if (vty != NULL) {
346 vty_time_print(vty, 0);
347 vty_out(vty, "Neighbor %s (%s) is down: manually cleared\n",
348 inet_ntoa(nbr->src),
349 ifindex2ifname(nbr->ei->ifp->ifindex, VRF_DEFAULT));
350 }
351
352 /* send Hello with Peer Termination TLV */
353 eigrp_hello_send(nbr->ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN_NBR,
354 &(nbr->src));
355 /* set neighbor to DOWN */
356 nbr->state = EIGRP_NEIGHBOR_DOWN;
357 /* delete neighbor */
358 eigrp_nbr_delete(nbr);
359 }