]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_interface.c
Merge pull request #9738 from rgirada/ospfv3_crash
[mirror_frr.git] / eigrpd / eigrp_interface.c
1 /*
2 * EIGRP Interface Functions.
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 "thread.h"
35 #include "linklist.h"
36 #include "prefix.h"
37 #include "if.h"
38 #include "table.h"
39 #include "memory.h"
40 #include "command.h"
41 #include "stream.h"
42 #include "log.h"
43 #include "keychain.h"
44 #include "vrf.h"
45
46 #include "eigrpd/eigrp_structs.h"
47 #include "eigrpd/eigrpd.h"
48 #include "eigrpd/eigrp_interface.h"
49 #include "eigrpd/eigrp_neighbor.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_fsm.h"
56 #include "eigrpd/eigrp_dump.h"
57 #include "eigrpd/eigrp_types.h"
58 #include "eigrpd/eigrp_metric.h"
59
60 DEFINE_MTYPE_STATIC(EIGRPD, EIGRP_IF, "EIGRP interface");
61 DEFINE_MTYPE_STATIC(EIGRPD, EIGRP_IF_INFO, "EIGRP Interface Information");
62
63 struct eigrp_interface *eigrp_if_new(struct eigrp *eigrp, struct interface *ifp,
64 struct prefix *p)
65 {
66 struct eigrp_interface *ei = ifp->info;
67 int i;
68
69 if (ei)
70 return ei;
71
72 ei = XCALLOC(MTYPE_EIGRP_IF, sizeof(struct eigrp_interface));
73
74 /* Set zebra interface pointer. */
75 ei->ifp = ifp;
76 prefix_copy(&ei->address, p);
77
78 ifp->info = ei;
79 listnode_add(eigrp->eiflist, ei);
80
81 ei->type = EIGRP_IFTYPE_BROADCAST;
82
83 /* Initialize neighbor list. */
84 ei->nbrs = list_new();
85
86 ei->crypt_seqnum = time(NULL);
87
88 /* Initialize lists */
89 for (i = 0; i < EIGRP_FILTER_MAX; i++) {
90 ei->list[i] = NULL;
91 ei->prefix[i] = NULL;
92 ei->routemap[i] = NULL;
93 }
94
95 ei->eigrp = eigrp;
96
97 ei->params.v_hello = EIGRP_HELLO_INTERVAL_DEFAULT;
98 ei->params.v_wait = EIGRP_HOLD_INTERVAL_DEFAULT;
99 ei->params.bandwidth = EIGRP_BANDWIDTH_DEFAULT;
100 ei->params.delay = EIGRP_DELAY_DEFAULT;
101 ei->params.reliability = EIGRP_RELIABILITY_DEFAULT;
102 ei->params.load = EIGRP_LOAD_DEFAULT;
103 ei->params.auth_type = EIGRP_AUTH_TYPE_NONE;
104 ei->params.auth_keychain = NULL;
105
106 ei->curr_bandwidth = ifp->bandwidth;
107 ei->curr_mtu = ifp->mtu;
108
109 return ei;
110 }
111
112 int eigrp_if_delete_hook(struct interface *ifp)
113 {
114 struct eigrp_interface *ei = ifp->info;
115 struct eigrp *eigrp;
116
117 if (!ei)
118 return 0;
119
120 list_delete(&ei->nbrs);
121
122 eigrp = ei->eigrp;
123 listnode_delete(eigrp->eiflist, ei);
124
125 eigrp_fifo_free(ei->obuf);
126
127 XFREE(MTYPE_EIGRP_IF_INFO, ifp->info);
128
129 return 0;
130 }
131
132 static int eigrp_ifp_create(struct interface *ifp)
133 {
134 struct eigrp_interface *ei = ifp->info;
135
136 if (!ei)
137 return 0;
138
139 ei->params.type = eigrp_default_iftype(ifp);
140
141 eigrp_if_update(ifp);
142
143 return 0;
144 }
145
146 static int eigrp_ifp_up(struct interface *ifp)
147 {
148 struct eigrp_interface *ei = ifp->info;
149
150 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
151 zlog_debug("Zebra: Interface[%s] state change to up.",
152 ifp->name);
153
154 if (!ei)
155 return 0;
156
157 if (ei->curr_bandwidth != ifp->bandwidth) {
158 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
159 zlog_debug(
160 "Zebra: Interface[%s] bandwidth change %d -> %d.",
161 ifp->name, ei->curr_bandwidth,
162 ifp->bandwidth);
163
164 ei->curr_bandwidth = ifp->bandwidth;
165 // eigrp_if_recalculate_output_cost (ifp);
166 }
167
168 if (ei->curr_mtu != ifp->mtu) {
169 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
170 zlog_debug(
171 "Zebra: Interface[%s] MTU change %u -> %u.",
172 ifp->name, ei->curr_mtu, ifp->mtu);
173
174 ei->curr_mtu = ifp->mtu;
175 /* Must reset the interface (simulate down/up) when MTU
176 * changes. */
177 eigrp_if_reset(ifp);
178 return 0;
179 }
180
181 eigrp_if_up(ifp->info);
182
183 return 0;
184 }
185
186 static int eigrp_ifp_down(struct interface *ifp)
187 {
188 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
189 zlog_debug("Zebra: Interface[%s] state change to down.",
190 ifp->name);
191
192 if (ifp->info)
193 eigrp_if_down(ifp->info);
194
195 return 0;
196 }
197
198 static int eigrp_ifp_destroy(struct interface *ifp)
199 {
200 if (if_is_up(ifp))
201 zlog_warn("Zebra: got delete of %s, but interface is still up",
202 ifp->name);
203
204 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
205 zlog_debug(
206 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
207 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
208 ifp->metric, ifp->mtu);
209
210 if (ifp->info)
211 eigrp_if_free(ifp->info, INTERFACE_DOWN_BY_ZEBRA);
212
213 return 0;
214 }
215
216 struct list *eigrp_iflist;
217
218 void eigrp_if_init(void)
219 {
220 if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up,
221 eigrp_ifp_down, eigrp_ifp_destroy);
222 /* Initialize Zebra interface data structure. */
223 // hook_register_prio(if_add, 0, eigrp_if_new);
224 hook_register_prio(if_del, 0, eigrp_if_delete_hook);
225 }
226
227
228 void eigrp_del_if_params(struct eigrp_if_params *eip)
229 {
230 if (eip->auth_keychain)
231 free(eip->auth_keychain);
232 }
233
234 /*
235 * Set the network byte order of the 3 bytes we send
236 * of the mtu of the link.
237 */
238 static void eigrp_mtu_convert(struct eigrp_metrics *metric, uint32_t host_mtu)
239 {
240 uint32_t network_mtu = htonl(host_mtu);
241 uint8_t *nm = (uint8_t *)&network_mtu;
242
243 metric->mtu[0] = nm[1];
244 metric->mtu[1] = nm[2];
245 metric->mtu[2] = nm[3];
246 }
247
248 int eigrp_if_up(struct eigrp_interface *ei)
249 {
250 struct eigrp_prefix_descriptor *pe;
251 struct eigrp_route_descriptor *ne;
252 struct eigrp_metrics metric;
253 struct eigrp_interface *ei2;
254 struct listnode *node, *nnode;
255 struct eigrp *eigrp;
256
257 if (ei == NULL)
258 return 0;
259
260 eigrp = ei->eigrp;
261 eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu);
262
263 eigrp_if_stream_set(ei);
264
265 /* Set multicast memberships appropriately for new state. */
266 eigrp_if_set_multicast(ei);
267
268 thread_add_event(master, eigrp_hello_timer, ei, (1), &ei->t_hello);
269
270 /*Prepare metrics*/
271 metric.bandwidth = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
272 metric.delay = eigrp_delay_to_scaled(ei->params.delay);
273 metric.load = ei->params.load;
274 metric.reliability = ei->params.reliability;
275 eigrp_mtu_convert(&metric, ei->ifp->mtu);
276 metric.hop_count = 0;
277 metric.flags = 0;
278 metric.tag = 0;
279
280 /*Add connected entry to topology table*/
281
282 ne = eigrp_route_descriptor_new();
283 ne->ei = ei;
284 ne->reported_metric = metric;
285 ne->total_metric = metric;
286 ne->distance = eigrp_calculate_metrics(eigrp, metric);
287 ne->reported_distance = 0;
288 ne->adv_router = eigrp->neighbor_self;
289 ne->flags = EIGRP_ROUTE_DESCRIPTOR_SUCCESSOR_FLAG;
290
291 struct prefix dest_addr;
292
293 dest_addr = ei->address;
294 apply_mask(&dest_addr);
295 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
296 &dest_addr);
297
298 if (pe == NULL) {
299 pe = eigrp_prefix_descriptor_new();
300 pe->serno = eigrp->serno;
301 pe->destination = (struct prefix *)prefix_ipv4_new();
302 prefix_copy(pe->destination, &dest_addr);
303 pe->af = AF_INET;
304 pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED;
305
306 ne->prefix = pe;
307 pe->reported_metric = metric;
308 pe->state = EIGRP_FSM_STATE_PASSIVE;
309 pe->fdistance = eigrp_calculate_metrics(eigrp, metric);
310 pe->req_action |= EIGRP_FSM_NEED_UPDATE;
311 eigrp_prefix_descriptor_add(eigrp->topology_table, pe);
312 listnode_add(eigrp->topology_changes_internalIPV4, pe);
313
314 eigrp_route_descriptor_add(eigrp, pe, ne);
315
316 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei2)) {
317 eigrp_update_send(ei2);
318 }
319
320 pe->req_action &= ~EIGRP_FSM_NEED_UPDATE;
321 listnode_delete(eigrp->topology_changes_internalIPV4, pe);
322 } else {
323 struct eigrp_fsm_action_message msg;
324
325 ne->prefix = pe;
326 eigrp_route_descriptor_add(eigrp, pe, ne);
327
328 msg.packet_type = EIGRP_OPC_UPDATE;
329 msg.eigrp = eigrp;
330 msg.data_type = EIGRP_CONNECTED;
331 msg.adv_router = NULL;
332 msg.entry = ne;
333 msg.prefix = pe;
334
335 eigrp_fsm_event(&msg);
336 }
337
338 return 1;
339 }
340
341 int eigrp_if_down(struct eigrp_interface *ei)
342 {
343 struct listnode *node, *nnode;
344 struct eigrp_neighbor *nbr;
345
346 if (ei == NULL)
347 return 0;
348
349 /* Shutdown packet reception and sending */
350 THREAD_OFF(ei->t_hello);
351
352 eigrp_if_stream_unset(ei);
353
354 /*Set infinite metrics to routes learned by this interface and start
355 * query process*/
356 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
357 eigrp_nbr_delete(nbr);
358 }
359
360 return 1;
361 }
362
363 void eigrp_if_stream_set(struct eigrp_interface *ei)
364 {
365 /* set output fifo queue. */
366 if (ei->obuf == NULL)
367 ei->obuf = eigrp_fifo_new();
368 }
369
370 void eigrp_if_stream_unset(struct eigrp_interface *ei)
371 {
372 struct eigrp *eigrp = ei->eigrp;
373
374 if (ei->on_write_q) {
375 listnode_delete(eigrp->oi_write_q, ei);
376 if (list_isempty(eigrp->oi_write_q))
377 thread_cancel(&(eigrp->t_write));
378 ei->on_write_q = 0;
379 }
380 }
381
382 bool eigrp_if_is_passive(struct eigrp_interface *ei)
383 {
384 if (ei->params.passive_interface == EIGRP_IF_ACTIVE)
385 return false;
386
387 if (ei->eigrp->passive_interface_default == EIGRP_IF_ACTIVE)
388 return false;
389
390 return true;
391 }
392
393 void eigrp_if_set_multicast(struct eigrp_interface *ei)
394 {
395 if (!eigrp_if_is_passive(ei)) {
396 /* The interface should belong to the EIGRP-all-routers group.
397 */
398 if (!ei->member_allrouters
399 && (eigrp_if_add_allspfrouters(ei->eigrp, &ei->address,
400 ei->ifp->ifindex)
401 >= 0))
402 /* Set the flag only if the system call to join
403 * succeeded. */
404 ei->member_allrouters = true;
405 } else {
406 /* The interface should NOT belong to the EIGRP-all-routers
407 * group. */
408 if (ei->member_allrouters) {
409 /* Only actually drop if this is the last reference */
410 eigrp_if_drop_allspfrouters(ei->eigrp, &ei->address,
411 ei->ifp->ifindex);
412 /* Unset the flag regardless of whether the system call
413 to leave
414 the group succeeded, since it's much safer to assume
415 that
416 we are not a member. */
417 ei->member_allrouters = false;
418 }
419 }
420 }
421
422 uint8_t eigrp_default_iftype(struct interface *ifp)
423 {
424 if (if_is_pointopoint(ifp))
425 return EIGRP_IFTYPE_POINTOPOINT;
426 else if (if_is_loopback(ifp))
427 return EIGRP_IFTYPE_LOOPBACK;
428 else
429 return EIGRP_IFTYPE_BROADCAST;
430 }
431
432 void eigrp_if_free(struct eigrp_interface *ei, int source)
433 {
434 struct prefix dest_addr;
435 struct eigrp_prefix_descriptor *pe;
436 struct eigrp *eigrp = ei->eigrp;
437
438 if (source == INTERFACE_DOWN_BY_VTY) {
439 thread_cancel(&ei->t_hello);
440 eigrp_hello_send(ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN, NULL);
441 }
442
443 dest_addr = ei->address;
444 apply_mask(&dest_addr);
445 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
446 &dest_addr);
447 if (pe)
448 eigrp_prefix_descriptor_delete(eigrp, eigrp->topology_table,
449 pe);
450
451 eigrp_if_down(ei);
452
453 listnode_delete(ei->eigrp->eiflist, ei);
454 }
455
456 /* Simulate down/up on the interface. This is needed, for example, when
457 the MTU changes. */
458 void eigrp_if_reset(struct interface *ifp)
459 {
460 struct eigrp_interface *ei = ifp->info;
461
462 if (!ei)
463 return;
464
465 eigrp_if_down(ei);
466 eigrp_if_up(ei);
467 }
468
469 struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp,
470 struct interface *ifp,
471 struct in_addr address)
472 {
473 struct listnode *node;
474 struct eigrp_interface *ei;
475
476 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
477 if (ifp && ei->ifp != ifp)
478 continue;
479
480 if (IPV4_ADDR_SAME(&address, &ei->address.u.prefix4))
481 return ei;
482 }
483
484 return NULL;
485 }
486
487 /**
488 * @fn eigrp_if_lookup_by_name
489 *
490 * @param[in] eigrp EIGRP process
491 * @param[in] if_name Name of the interface
492 *
493 * @return struct eigrp_interface *
494 *
495 * @par
496 * Function is used for lookup interface by name.
497 */
498 struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp,
499 const char *if_name)
500 {
501 struct eigrp_interface *ei;
502 struct listnode *node;
503
504 /* iterate over all eigrp interfaces */
505 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
506 /* compare int name with eigrp interface's name */
507 if (strcmp(ei->ifp->name, if_name) == 0) {
508 return ei;
509 }
510 }
511
512 return NULL;
513 }