2 * EIGRP Interface Functions.
3 * Copyright (C) 2013-2016
15 * This file is part of GNU Zebra.
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
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.
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
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"
60 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_IF
, "EIGRP interface");
61 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_IF_INFO
, "EIGRP Interface Information");
63 struct eigrp_interface
*eigrp_if_new(struct eigrp
*eigrp
, struct interface
*ifp
,
66 struct eigrp_interface
*ei
= ifp
->info
;
72 ei
= XCALLOC(MTYPE_EIGRP_IF
, sizeof(struct eigrp_interface
));
74 /* Set zebra interface pointer. */
76 prefix_copy(&ei
->address
, p
);
79 listnode_add(eigrp
->eiflist
, ei
);
81 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
83 /* Initialize neighbor list. */
84 ei
->nbrs
= list_new();
86 ei
->crypt_seqnum
= time(NULL
);
88 /* Initialize lists */
89 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++) {
92 ei
->routemap
[i
] = NULL
;
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
;
106 ei
->curr_bandwidth
= ifp
->bandwidth
;
107 ei
->curr_mtu
= ifp
->mtu
;
112 int eigrp_if_delete_hook(struct interface
*ifp
)
114 struct eigrp_interface
*ei
= ifp
->info
;
120 list_delete(&ei
->nbrs
);
123 listnode_delete(eigrp
->eiflist
, ei
);
125 eigrp_fifo_free(ei
->obuf
);
127 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
132 static int eigrp_ifp_create(struct interface
*ifp
)
134 struct eigrp_interface
*ei
= ifp
->info
;
139 ei
->params
.type
= eigrp_default_iftype(ifp
);
141 eigrp_if_update(ifp
);
146 static int eigrp_ifp_up(struct interface
*ifp
)
148 struct eigrp_interface
*ei
= ifp
->info
;
150 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
151 zlog_debug("Zebra: Interface[%s] state change to up.",
157 if (ei
->curr_bandwidth
!= ifp
->bandwidth
) {
158 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
160 "Zebra: Interface[%s] bandwidth change %d -> %d.",
161 ifp
->name
, ei
->curr_bandwidth
,
164 ei
->curr_bandwidth
= ifp
->bandwidth
;
165 // eigrp_if_recalculate_output_cost (ifp);
168 if (ei
->curr_mtu
!= ifp
->mtu
) {
169 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
171 "Zebra: Interface[%s] MTU change %u -> %u.",
172 ifp
->name
, ei
->curr_mtu
, ifp
->mtu
);
174 ei
->curr_mtu
= ifp
->mtu
;
175 /* Must reset the interface (simulate down/up) when MTU
181 eigrp_if_up(ifp
->info
);
186 static int eigrp_ifp_down(struct interface
*ifp
)
188 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
189 zlog_debug("Zebra: Interface[%s] state change to down.",
193 eigrp_if_down(ifp
->info
);
198 static int eigrp_ifp_destroy(struct interface
*ifp
)
201 zlog_warn("Zebra: got delete of %s, but interface is still up",
204 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
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
);
211 eigrp_if_free(ifp
->info
, INTERFACE_DOWN_BY_ZEBRA
);
216 struct list
*eigrp_iflist
;
218 void eigrp_if_init(void)
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
);
228 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
230 if (eip
->auth_keychain
)
231 free(eip
->auth_keychain
);
235 * Set the network byte order of the 3 bytes we send
236 * of the mtu of the link.
238 static void eigrp_mtu_convert(struct eigrp_metrics
*metric
, uint32_t host_mtu
)
240 uint32_t network_mtu
= htonl(host_mtu
);
241 uint8_t *nm
= (uint8_t *)&network_mtu
;
243 metric
->mtu
[0] = nm
[1];
244 metric
->mtu
[1] = nm
[2];
245 metric
->mtu
[2] = nm
[3];
248 int eigrp_if_up(struct eigrp_interface
*ei
)
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
;
261 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
263 eigrp_if_stream_set(ei
);
265 /* Set multicast memberships appropriately for new state. */
266 eigrp_if_set_multicast(ei
);
268 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), &ei
->t_hello
);
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;
280 /*Add connected entry to topology table*/
282 ne
= eigrp_route_descriptor_new();
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
;
291 struct prefix dest_addr
;
293 dest_addr
= ei
->address
;
294 apply_mask(&dest_addr
);
295 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
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
);
304 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
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
);
314 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
316 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
317 eigrp_update_send(ei2
);
320 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
321 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
323 struct eigrp_fsm_action_message msg
;
326 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
328 msg
.packet_type
= EIGRP_OPC_UPDATE
;
330 msg
.data_type
= EIGRP_CONNECTED
;
331 msg
.adv_router
= NULL
;
335 eigrp_fsm_event(&msg
);
341 int eigrp_if_down(struct eigrp_interface
*ei
)
343 struct listnode
*node
, *nnode
;
344 struct eigrp_neighbor
*nbr
;
349 /* Shutdown packet reception and sending */
350 THREAD_OFF(ei
->t_hello
);
352 eigrp_if_stream_unset(ei
);
354 /*Set infinite metrics to routes learned by this interface and start
356 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
357 eigrp_nbr_delete(nbr
);
363 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
365 /* set output fifo queue. */
366 if (ei
->obuf
== NULL
)
367 ei
->obuf
= eigrp_fifo_new();
370 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
372 struct eigrp
*eigrp
= ei
->eigrp
;
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
));
382 bool eigrp_if_is_passive(struct eigrp_interface
*ei
)
384 if (ei
->params
.passive_interface
== EIGRP_IF_ACTIVE
)
387 if (ei
->eigrp
->passive_interface_default
== EIGRP_IF_ACTIVE
)
393 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
395 if (!eigrp_if_is_passive(ei
)) {
396 /* The interface should belong to the EIGRP-all-routers group.
398 if (!ei
->member_allrouters
399 && (eigrp_if_add_allspfrouters(ei
->eigrp
, &ei
->address
,
402 /* Set the flag only if the system call to join
404 ei
->member_allrouters
= true;
406 /* The interface should NOT belong to the EIGRP-all-routers
408 if (ei
->member_allrouters
) {
409 /* Only actually drop if this is the last reference */
410 eigrp_if_drop_allspfrouters(ei
->eigrp
, &ei
->address
,
412 /* Unset the flag regardless of whether the system call
414 the group succeeded, since it's much safer to assume
416 we are not a member. */
417 ei
->member_allrouters
= false;
422 uint8_t eigrp_default_iftype(struct interface
*ifp
)
424 if (if_is_pointopoint(ifp
))
425 return EIGRP_IFTYPE_POINTOPOINT
;
426 else if (if_is_loopback(ifp
))
427 return EIGRP_IFTYPE_LOOPBACK
;
429 return EIGRP_IFTYPE_BROADCAST
;
432 void eigrp_if_free(struct eigrp_interface
*ei
, int source
)
434 struct prefix dest_addr
;
435 struct eigrp_prefix_descriptor
*pe
;
436 struct eigrp
*eigrp
= ei
->eigrp
;
438 if (source
== INTERFACE_DOWN_BY_VTY
) {
439 thread_cancel(&ei
->t_hello
);
440 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
443 dest_addr
= ei
->address
;
444 apply_mask(&dest_addr
);
445 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
448 eigrp_prefix_descriptor_delete(eigrp
, eigrp
->topology_table
,
453 listnode_delete(ei
->eigrp
->eiflist
, ei
);
456 /* Simulate down/up on the interface. This is needed, for example, when
458 void eigrp_if_reset(struct interface
*ifp
)
460 struct eigrp_interface
*ei
= ifp
->info
;
469 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
470 struct interface
*ifp
,
471 struct in_addr address
)
473 struct listnode
*node
;
474 struct eigrp_interface
*ei
;
476 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
477 if (ifp
&& ei
->ifp
!= ifp
)
480 if (IPV4_ADDR_SAME(&address
, &ei
->address
.u
.prefix4
))
488 * @fn eigrp_if_lookup_by_name
490 * @param[in] eigrp EIGRP process
491 * @param[in] if_name Name of the interface
493 * @return struct eigrp_interface *
496 * Function is used for lookup interface by name.
498 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
501 struct eigrp_interface
*ei
;
502 struct listnode
*node
;
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) {