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