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_memory.h"
56 #include "eigrpd/eigrp_fsm.h"
57 #include "eigrpd/eigrp_dump.h"
58 #include "eigrpd/eigrp_types.h"
59 #include "eigrpd/eigrp_metric.h"
61 struct eigrp_interface
*eigrp_if_new(struct eigrp
*eigrp
, struct interface
*ifp
,
64 struct eigrp_interface
*ei
= ifp
->info
;
70 ei
= XCALLOC(MTYPE_EIGRP_IF
, sizeof(struct eigrp_interface
));
72 /* Set zebra interface pointer. */
74 prefix_copy(&ei
->address
, p
);
77 listnode_add(eigrp
->eiflist
, ei
);
79 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
81 /* Initialize neighbor list. */
82 ei
->nbrs
= list_new();
84 ei
->crypt_seqnum
= time(NULL
);
86 /* Initialize lists */
87 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++) {
90 ei
->routemap
[i
] = NULL
;
95 ei
->params
.v_hello
= EIGRP_HELLO_INTERVAL_DEFAULT
;
96 ei
->params
.v_wait
= EIGRP_HOLD_INTERVAL_DEFAULT
;
97 ei
->params
.bandwidth
= EIGRP_BANDWIDTH_DEFAULT
;
98 ei
->params
.delay
= EIGRP_DELAY_DEFAULT
;
99 ei
->params
.reliability
= EIGRP_RELIABILITY_DEFAULT
;
100 ei
->params
.load
= EIGRP_LOAD_DEFAULT
;
101 ei
->params
.auth_type
= EIGRP_AUTH_TYPE_NONE
;
102 ei
->params
.auth_keychain
= NULL
;
104 ei
->curr_bandwidth
= ifp
->bandwidth
;
105 ei
->curr_mtu
= ifp
->mtu
;
110 int eigrp_if_delete_hook(struct interface
*ifp
)
112 struct eigrp_interface
*ei
= ifp
->info
;
118 list_delete(&ei
->nbrs
);
121 listnode_delete(eigrp
->eiflist
, ei
);
123 eigrp_fifo_free(ei
->obuf
);
125 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
130 static int eigrp_ifp_create(struct interface
*ifp
)
132 struct eigrp_interface
*ei
= ifp
->info
;
137 ei
->params
.type
= eigrp_default_iftype(ifp
);
139 eigrp_if_update(ifp
);
144 static int eigrp_ifp_up(struct interface
*ifp
)
146 struct eigrp_interface
*ei
= ifp
->info
;
148 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
149 zlog_debug("Zebra: Interface[%s] state change to up.",
155 if (ei
->curr_bandwidth
!= ifp
->bandwidth
) {
156 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
158 "Zebra: Interface[%s] bandwidth change %d -> %d.",
159 ifp
->name
, ei
->curr_bandwidth
,
162 ei
->curr_bandwidth
= ifp
->bandwidth
;
163 // eigrp_if_recalculate_output_cost (ifp);
166 if (ei
->curr_mtu
!= ifp
->mtu
) {
167 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
169 "Zebra: Interface[%s] MTU change %u -> %u.",
170 ifp
->name
, ei
->curr_mtu
, ifp
->mtu
);
172 ei
->curr_mtu
= ifp
->mtu
;
173 /* Must reset the interface (simulate down/up) when MTU
179 eigrp_if_up(ifp
->info
);
184 static int eigrp_ifp_down(struct interface
*ifp
)
186 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
187 zlog_debug("Zebra: Interface[%s] state change to down.",
191 eigrp_if_down(ifp
->info
);
196 static int eigrp_ifp_destroy(struct interface
*ifp
)
199 zlog_warn("Zebra: got delete of %s, but interface is still up",
202 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
204 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
205 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
206 ifp
->metric
, ifp
->mtu
);
209 eigrp_if_free(ifp
->info
, INTERFACE_DOWN_BY_ZEBRA
);
214 struct list
*eigrp_iflist
;
216 void eigrp_if_init(void)
218 if_zapi_callbacks(eigrp_ifp_create
, eigrp_ifp_up
,
219 eigrp_ifp_down
, eigrp_ifp_destroy
);
220 /* Initialize Zebra interface data structure. */
221 // hook_register_prio(if_add, 0, eigrp_if_new);
222 hook_register_prio(if_del
, 0, eigrp_if_delete_hook
);
226 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
228 if (eip
->auth_keychain
)
229 free(eip
->auth_keychain
);
233 * Set the network byte order of the 3 bytes we send
234 * of the mtu of the link.
236 static void eigrp_mtu_convert(struct eigrp_metrics
*metric
, uint32_t host_mtu
)
238 uint32_t network_mtu
= htonl(host_mtu
);
239 uint8_t *nm
= (uint8_t *)&network_mtu
;
241 metric
->mtu
[0] = nm
[1];
242 metric
->mtu
[1] = nm
[2];
243 metric
->mtu
[2] = nm
[3];
246 int eigrp_if_up(struct eigrp_interface
*ei
)
248 struct eigrp_prefix_descriptor
*pe
;
249 struct eigrp_route_descriptor
*ne
;
250 struct eigrp_metrics metric
;
251 struct eigrp_interface
*ei2
;
252 struct listnode
*node
, *nnode
;
259 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
261 eigrp_if_stream_set(ei
);
263 /* Set multicast memberships appropriately for new state. */
264 eigrp_if_set_multicast(ei
);
266 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), NULL
);
269 metric
.bandwidth
= eigrp_bandwidth_to_scaled(ei
->params
.bandwidth
);
270 metric
.delay
= eigrp_delay_to_scaled(ei
->params
.delay
);
271 metric
.load
= ei
->params
.load
;
272 metric
.reliability
= ei
->params
.reliability
;
273 eigrp_mtu_convert(&metric
, ei
->ifp
->mtu
);
274 metric
.hop_count
= 0;
278 /*Add connected entry to topology table*/
280 ne
= eigrp_route_descriptor_new();
282 ne
->reported_metric
= metric
;
283 ne
->total_metric
= metric
;
284 ne
->distance
= eigrp_calculate_metrics(eigrp
, metric
);
285 ne
->reported_distance
= 0;
286 ne
->adv_router
= eigrp
->neighbor_self
;
287 ne
->flags
= EIGRP_ROUTE_DESCRIPTOR_SUCCESSOR_FLAG
;
289 struct prefix dest_addr
;
291 dest_addr
= ei
->address
;
292 apply_mask(&dest_addr
);
293 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
297 pe
= eigrp_prefix_descriptor_new();
298 pe
->serno
= eigrp
->serno
;
299 pe
->destination
= (struct prefix
*)prefix_ipv4_new();
300 prefix_copy(pe
->destination
, &dest_addr
);
302 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
305 pe
->reported_metric
= metric
;
306 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
307 pe
->fdistance
= eigrp_calculate_metrics(eigrp
, metric
);
308 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
309 eigrp_prefix_descriptor_add(eigrp
->topology_table
, pe
);
310 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
312 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
314 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
315 eigrp_update_send(ei2
);
318 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
319 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
321 struct eigrp_fsm_action_message msg
;
324 eigrp_route_descriptor_add(eigrp
, pe
, ne
);
326 msg
.packet_type
= EIGRP_OPC_UPDATE
;
328 msg
.data_type
= EIGRP_CONNECTED
;
329 msg
.adv_router
= NULL
;
333 eigrp_fsm_event(&msg
);
339 int eigrp_if_down(struct eigrp_interface
*ei
)
341 struct listnode
*node
, *nnode
;
342 struct eigrp_neighbor
*nbr
;
347 /* Shutdown packet reception and sending */
348 THREAD_OFF(ei
->t_hello
);
350 eigrp_if_stream_unset(ei
);
352 /*Set infinite metrics to routes learned by this interface and start
354 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
355 eigrp_nbr_delete(nbr
);
361 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
363 /* set output fifo queue. */
364 if (ei
->obuf
== NULL
)
365 ei
->obuf
= eigrp_fifo_new();
368 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
370 struct eigrp
*eigrp
= ei
->eigrp
;
372 if (ei
->on_write_q
) {
373 listnode_delete(eigrp
->oi_write_q
, ei
);
374 if (list_isempty(eigrp
->oi_write_q
))
375 thread_cancel(&(eigrp
->t_write
));
380 bool eigrp_if_is_passive(struct eigrp_interface
*ei
)
382 if (ei
->params
.passive_interface
== EIGRP_IF_ACTIVE
)
385 if (ei
->eigrp
->passive_interface_default
== EIGRP_IF_ACTIVE
)
391 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
393 if (!eigrp_if_is_passive(ei
)) {
394 /* The interface should belong to the EIGRP-all-routers group.
396 if (!ei
->member_allrouters
397 && (eigrp_if_add_allspfrouters(ei
->eigrp
, &ei
->address
,
400 /* Set the flag only if the system call to join
402 ei
->member_allrouters
= true;
404 /* The interface should NOT belong to the EIGRP-all-routers
406 if (ei
->member_allrouters
) {
407 /* Only actually drop if this is the last reference */
408 eigrp_if_drop_allspfrouters(ei
->eigrp
, &ei
->address
,
410 /* Unset the flag regardless of whether the system call
412 the group succeeded, since it's much safer to assume
414 we are not a member. */
415 ei
->member_allrouters
= false;
420 uint8_t eigrp_default_iftype(struct interface
*ifp
)
422 if (if_is_pointopoint(ifp
))
423 return EIGRP_IFTYPE_POINTOPOINT
;
424 else if (if_is_loopback(ifp
))
425 return EIGRP_IFTYPE_LOOPBACK
;
427 return EIGRP_IFTYPE_BROADCAST
;
430 void eigrp_if_free(struct eigrp_interface
*ei
, int source
)
432 struct prefix dest_addr
;
433 struct eigrp_prefix_descriptor
*pe
;
434 struct eigrp
*eigrp
= ei
->eigrp
;
436 if (source
== INTERFACE_DOWN_BY_VTY
) {
437 thread_cancel(&ei
->t_hello
);
438 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
441 dest_addr
= ei
->address
;
442 apply_mask(&dest_addr
);
443 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
446 eigrp_prefix_descriptor_delete(eigrp
, eigrp
->topology_table
,
451 listnode_delete(ei
->eigrp
->eiflist
, ei
);
454 /* Simulate down/up on the interface. This is needed, for example, when
456 void eigrp_if_reset(struct interface
*ifp
)
458 struct eigrp_interface
*ei
= ifp
->info
;
467 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
468 struct interface
*ifp
,
469 struct in_addr address
)
471 struct listnode
*node
;
472 struct eigrp_interface
*ei
;
474 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
475 if (ifp
&& ei
->ifp
!= ifp
)
478 if (IPV4_ADDR_SAME(&address
, &ei
->address
.u
.prefix4
))
486 * @fn eigrp_if_lookup_by_name
488 * @param[in] eigrp EIGRP process
489 * @param[in] if_name Name of the interface
491 * @return struct eigrp_interface *
494 * Function is used for lookup interface by name.
496 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
499 struct eigrp_interface
*ei
;
500 struct listnode
*node
;
502 /* iterate over all eigrp interfaces */
503 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
504 /* compare int name with eigrp interface's name */
505 if (strcmp(ei
->ifp
->name
, if_name
) == 0) {