1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * EIGRP Interface Functions.
4 * Copyright (C) 2013-2016
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"
46 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_IF
, "EIGRP interface");
47 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_IF_INFO
, "EIGRP Interface Information");
49 struct eigrp_interface
*eigrp_if_new(struct eigrp
*eigrp
, struct interface
*ifp
,
52 struct eigrp_interface
*ei
= ifp
->info
;
58 ei
= XCALLOC(MTYPE_EIGRP_IF
, sizeof(struct eigrp_interface
));
60 /* Set zebra interface pointer. */
62 prefix_copy(&ei
->address
, p
);
65 listnode_add(eigrp
->eiflist
, ei
);
67 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
69 /* Initialize neighbor list. */
70 ei
->nbrs
= list_new();
72 ei
->crypt_seqnum
= frr_sequence32_next();
74 /* Initialize lists */
75 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++) {
78 ei
->routemap
[i
] = NULL
;
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
;
92 ei
->curr_bandwidth
= ifp
->bandwidth
;
93 ei
->curr_mtu
= ifp
->mtu
;
98 int eigrp_if_delete_hook(struct interface
*ifp
)
100 struct eigrp_interface
*ei
= ifp
->info
;
106 list_delete(&ei
->nbrs
);
109 listnode_delete(eigrp
->eiflist
, ei
);
111 eigrp_fifo_free(ei
->obuf
);
113 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
118 static int eigrp_ifp_create(struct interface
*ifp
)
120 struct eigrp_interface
*ei
= ifp
->info
;
125 ei
->params
.type
= eigrp_default_iftype(ifp
);
127 eigrp_if_update(ifp
);
132 static int eigrp_ifp_up(struct interface
*ifp
)
134 struct eigrp_interface
*ei
= ifp
->info
;
136 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
137 zlog_debug("Zebra: Interface[%s] state change to up.",
143 if (ei
->curr_bandwidth
!= ifp
->bandwidth
) {
144 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
146 "Zebra: Interface[%s] bandwidth change %d -> %d.",
147 ifp
->name
, ei
->curr_bandwidth
,
150 ei
->curr_bandwidth
= ifp
->bandwidth
;
151 // eigrp_if_recalculate_output_cost (ifp);
154 if (ei
->curr_mtu
!= ifp
->mtu
) {
155 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
157 "Zebra: Interface[%s] MTU change %u -> %u.",
158 ifp
->name
, ei
->curr_mtu
, ifp
->mtu
);
160 ei
->curr_mtu
= ifp
->mtu
;
161 /* Must reset the interface (simulate down/up) when MTU
167 eigrp_if_up(ifp
->info
);
172 static int eigrp_ifp_down(struct interface
*ifp
)
174 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
175 zlog_debug("Zebra: Interface[%s] state change to down.",
179 eigrp_if_down(ifp
->info
);
184 static int eigrp_ifp_destroy(struct interface
*ifp
)
187 zlog_warn("Zebra: got delete of %s, but interface is still up",
190 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
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
);
197 eigrp_if_free(ifp
->info
, INTERFACE_DOWN_BY_ZEBRA
);
202 struct list
*eigrp_iflist
;
204 void eigrp_if_init(void)
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
);
214 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
216 if (eip
->auth_keychain
)
217 free(eip
->auth_keychain
);
221 * Set the network byte order of the 3 bytes we send
222 * of the mtu of the link.
224 static void eigrp_mtu_convert(struct eigrp_metrics
*metric
, uint32_t host_mtu
)
226 uint32_t network_mtu
= htonl(host_mtu
);
227 uint8_t *nm
= (uint8_t *)&network_mtu
;
229 metric
->mtu
[0] = nm
[1];
230 metric
->mtu
[1] = nm
[2];
231 metric
->mtu
[2] = nm
[3];
234 int eigrp_if_up(struct eigrp_interface
*ei
)
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
;
247 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
249 eigrp_if_stream_set(ei
);
251 /* Set multicast memberships appropriately for new state. */
252 eigrp_if_set_multicast(ei
);
254 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), &ei
->t_hello
);
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;
266 /*Add connected entry to topology table*/
268 ne
= eigrp_route_descriptor_new();
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
;
277 struct prefix dest_addr
;
279 dest_addr
= ei
->address
;
280 apply_mask(&dest_addr
);
281 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
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
);
290 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
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
);
300 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
302 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
303 eigrp_update_send(ei2
);
306 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
307 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
309 struct eigrp_fsm_action_message msg
;
312 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
314 msg
.packet_type
= EIGRP_OPC_UPDATE
;
316 msg
.data_type
= EIGRP_CONNECTED
;
317 msg
.adv_router
= NULL
;
321 eigrp_fsm_event(&msg
);
327 int eigrp_if_down(struct eigrp_interface
*ei
)
329 struct listnode
*node
, *nnode
;
330 struct eigrp_neighbor
*nbr
;
335 /* Shutdown packet reception and sending */
336 THREAD_OFF(ei
->t_hello
);
338 eigrp_if_stream_unset(ei
);
340 /*Set infinite metrics to routes learned by this interface and start
342 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
343 eigrp_nbr_delete(nbr
);
349 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
351 /* set output fifo queue. */
352 if (ei
->obuf
== NULL
)
353 ei
->obuf
= eigrp_fifo_new();
356 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
358 struct eigrp
*eigrp
= ei
->eigrp
;
360 if (ei
->on_write_q
) {
361 listnode_delete(eigrp
->oi_write_q
, ei
);
362 if (list_isempty(eigrp
->oi_write_q
))
363 thread_cancel(&(eigrp
->t_write
));
368 bool eigrp_if_is_passive(struct eigrp_interface
*ei
)
370 if (ei
->params
.passive_interface
== EIGRP_IF_ACTIVE
)
373 if (ei
->eigrp
->passive_interface_default
== EIGRP_IF_ACTIVE
)
379 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
381 if (!eigrp_if_is_passive(ei
)) {
382 /* The interface should belong to the EIGRP-all-routers group.
384 if (!ei
->member_allrouters
385 && (eigrp_if_add_allspfrouters(ei
->eigrp
, &ei
->address
,
388 /* Set the flag only if the system call to join
390 ei
->member_allrouters
= true;
392 /* The interface should NOT belong to the EIGRP-all-routers
394 if (ei
->member_allrouters
) {
395 /* Only actually drop if this is the last reference */
396 eigrp_if_drop_allspfrouters(ei
->eigrp
, &ei
->address
,
398 /* Unset the flag regardless of whether the system call
400 the group succeeded, since it's much safer to assume
402 we are not a member. */
403 ei
->member_allrouters
= false;
408 uint8_t eigrp_default_iftype(struct interface
*ifp
)
410 if (if_is_pointopoint(ifp
))
411 return EIGRP_IFTYPE_POINTOPOINT
;
412 else if (if_is_loopback(ifp
))
413 return EIGRP_IFTYPE_LOOPBACK
;
415 return EIGRP_IFTYPE_BROADCAST
;
418 void eigrp_if_free(struct eigrp_interface
*ei
, int source
)
420 struct prefix dest_addr
;
421 struct eigrp_prefix_descriptor
*pe
;
422 struct eigrp
*eigrp
= ei
->eigrp
;
424 if (source
== INTERFACE_DOWN_BY_VTY
) {
425 thread_cancel(&ei
->t_hello
);
426 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
429 dest_addr
= ei
->address
;
430 apply_mask(&dest_addr
);
431 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
434 eigrp_prefix_descriptor_delete(eigrp
, eigrp
->topology_table
,
439 listnode_delete(ei
->eigrp
->eiflist
, ei
);
442 /* Simulate down/up on the interface. This is needed, for example, when
444 void eigrp_if_reset(struct interface
*ifp
)
446 struct eigrp_interface
*ei
= ifp
->info
;
455 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
456 struct interface
*ifp
,
457 struct in_addr address
)
459 struct listnode
*node
;
460 struct eigrp_interface
*ei
;
462 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
463 if (ifp
&& ei
->ifp
!= ifp
)
466 if (IPV4_ADDR_SAME(&address
, &ei
->address
.u
.prefix4
))
474 * @fn eigrp_if_lookup_by_name
476 * @param[in] eigrp EIGRP process
477 * @param[in] if_name Name of the interface
479 * @return struct eigrp_interface *
482 * Function is used for lookup interface by name.
484 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
487 struct eigrp_interface
*ei
;
488 struct listnode
*node
;
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) {