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
28 * along with GNU Zebra; see the file COPYING. If not, write to the Free
29 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
47 #include "eigrpd/eigrp_structs.h"
48 #include "eigrpd/eigrpd.h"
49 #include "eigrpd/eigrp_interface.h"
50 #include "eigrpd/eigrp_neighbor.h"
51 #include "eigrpd/eigrp_packet.h"
52 #include "eigrpd/eigrp_zebra.h"
53 #include "eigrpd/eigrp_vty.h"
54 #include "eigrpd/eigrp_network.h"
55 #include "eigrpd/eigrp_topology.h"
56 #include "eigrpd/eigrp_memory.h"
59 eigrp_delete_from_if (struct interface
*, struct eigrp_interface
*);
62 eigrp_add_to_if (struct interface
*ifp
, struct eigrp_interface
*ei
)
64 struct route_node
*rn
;
68 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
70 rn
= route_node_get (IF_OIFS (ifp
), &p
);
71 /* rn->info should either be NULL or equal to this ei
72 * as route_node_get may return an existing node
74 assert (!rn
->info
|| rn
->info
== ei
);
78 struct eigrp_interface
*
79 eigrp_if_new (struct eigrp
*eigrp
, struct interface
*ifp
, struct prefix
*p
)
81 struct eigrp_interface
*ei
;
84 if ((ei
= eigrp_if_table_lookup (ifp
, p
)) == NULL
)
86 ei
= XCALLOC (MTYPE_EIGRP_IF
, sizeof (struct eigrp_interface
));
87 memset (ei
, 0, sizeof (struct eigrp_interface
));
92 /* Set zebra interface pointer. */
96 eigrp_add_to_if (ifp
, ei
);
97 listnode_add (eigrp
->eiflist
, ei
);
99 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
101 /* Initialize neighbor list. */
102 ei
->nbrs
= list_new ();
104 ei
->crypt_seqnum
= time (NULL
);
106 /* Initialize lists */
107 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++)
110 ei
->prefix
[i
] = NULL
;
111 ei
->routemap
[i
] = NULL
;
117 /* lookup ei for specified prefix/ifp */
118 struct eigrp_interface
*
119 eigrp_if_table_lookup (struct interface
*ifp
, struct prefix
*prefix
)
122 struct route_node
*rn
;
123 struct eigrp_interface
*rninfo
= NULL
;
126 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
128 /* route_node_get implicitly locks */
129 if ((rn
= route_node_lookup (IF_OIFS (ifp
), &p
)))
131 rninfo
= (struct eigrp_interface
*) rn
->info
;
132 route_unlock_node (rn
);
139 eigrp_if_delete_hook (struct interface
*ifp
)
141 struct route_node
*rn
;
143 route_table_finish (IF_OIFS (ifp
));
145 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
147 eigrp_del_if_params (rn
->info
);
148 route_table_finish (IF_OIFS_PARAMS (ifp
));
150 XFREE (MTYPE_EIGRP_IF_INFO
, ifp
->info
);
156 struct list
*eigrp_iflist
;
161 /* Initialize Zebra interface data structure. */
162 if_add_hook (IF_NEW_HOOK
, eigrp_if_new_hook
);
163 if_add_hook (IF_DELETE_HOOK
, eigrp_if_delete_hook
);
167 eigrp_if_new_hook (struct interface
*ifp
)
171 ifp
->info
= XCALLOC (MTYPE_EIGRP_IF_INFO
, sizeof (struct eigrp_if_info
));
173 IF_OIFS (ifp
) = route_table_init ();
174 IF_OIFS_PARAMS (ifp
) = route_table_init ();
176 IF_DEF_PARAMS (ifp
) = eigrp_new_if_params ();
178 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
179 IF_DEF_PARAMS (ifp
)->v_hello
= (u_int32_t
) EIGRP_HELLO_INTERVAL_DEFAULT
;
181 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
182 IF_DEF_PARAMS (ifp
)->v_wait
= (u_int16_t
) EIGRP_HOLD_INTERVAL_DEFAULT
;
184 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), bandwidth
);
185 IF_DEF_PARAMS (ifp
)->bandwidth
= (u_int32_t
) EIGRP_BANDWIDTH_DEFAULT
;
187 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), delay
);
188 IF_DEF_PARAMS (ifp
)->delay
= (u_int32_t
) EIGRP_DELAY_DEFAULT
;
190 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), reliability
);
191 IF_DEF_PARAMS (ifp
)->reliability
= (u_char
) EIGRP_RELIABILITY_DEFAULT
;
193 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), load
);
194 IF_DEF_PARAMS (ifp
)->load
= (u_char
) EIGRP_LOAD_DEFAULT
;
196 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
197 IF_DEF_PARAMS (ifp
)->auth_type
= EIGRP_AUTH_TYPE_NONE
;
199 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_keychain
);
200 IF_DEF_PARAMS (ifp
)->auth_keychain
= NULL
;
205 struct eigrp_if_params
*
206 eigrp_new_if_params (void)
208 struct eigrp_if_params
*eip
;
210 eip
= XCALLOC (MTYPE_EIGRP_IF_PARAMS
, sizeof (struct eigrp_if_params
));
214 UNSET_IF_PARAM (eip
, passive_interface
);
215 UNSET_IF_PARAM (eip
, v_hello
);
216 UNSET_IF_PARAM (eip
, v_wait
);
217 UNSET_IF_PARAM (eip
, bandwidth
);
218 UNSET_IF_PARAM (eip
, delay
);
219 UNSET_IF_PARAM (eip
, reliability
);
220 UNSET_IF_PARAM (eip
, load
);
221 UNSET_IF_PARAM (eip
, auth_keychain
);
222 UNSET_IF_PARAM (eip
, auth_type
);
228 eigrp_del_if_params (struct eigrp_if_params
*eip
)
230 if(eip
->auth_keychain
)
231 free(eip
->auth_keychain
);
233 XFREE (MTYPE_EIGRP_IF_PARAMS
, eip
);
236 struct eigrp_if_params
*
237 eigrp_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
239 struct prefix_ipv4 p
;
240 struct route_node
*rn
;
243 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
246 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*) &p
);
250 route_unlock_node (rn
);
258 eigrp_if_up (struct eigrp_interface
*ei
)
260 struct eigrp_prefix_entry
*pe
;
261 struct eigrp_neighbor_entry
*ne
;
262 struct eigrp_metrics metric
;
263 struct eigrp_interface
*ei2
;
264 struct listnode
*node
, *nnode
;
265 struct eigrp
*eigrp
= eigrp_lookup ();
271 eigrp_adjust_sndbuflen (eigrp
, ei
->ifp
->mtu
);
273 zlog_warn ("%s: eigrp_lookup () returned NULL", __func__
);
274 eigrp_if_stream_set (ei
);
276 /* Set multicast memberships appropriately for new state. */
277 eigrp_if_set_multicast (ei
);
279 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), NULL
);
282 metric
.bandwith
= eigrp_bandwidth_to_scaled (EIGRP_IF_PARAM (ei
,bandwidth
));
283 metric
.delay
= eigrp_delay_to_scaled (EIGRP_IF_PARAM (ei
,delay
));
284 metric
.load
= EIGRP_IF_PARAM (ei
,load
);
285 metric
.reliability
= EIGRP_IF_PARAM (ei
,reliability
);
286 metric
.mtu
[0] = 0xDC;
287 metric
.mtu
[1] = 0x05;
288 metric
.mtu
[2] = 0x00;
289 metric
.hop_count
= 0;
293 /*Add connected entry to topology table*/
295 struct prefix_ipv4
*dest_addr
= prefix_ipv4_new ();
297 dest_addr
->family
= AF_INET
;
298 dest_addr
->prefix
= ei
->connected
->address
->u
.prefix4
;
299 dest_addr
->prefixlen
= ei
->connected
->address
->prefixlen
;
300 apply_mask_ipv4 (dest_addr
);
301 pe
= eigrp_topology_table_lookup_ipv4 (eigrp
->topology_table
, dest_addr
);
305 pe
= eigrp_prefix_entry_new ();
306 pe
->serno
= eigrp
->serno
;
307 pe
->destination_ipv4
= dest_addr
;
309 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
311 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
312 pe
->fdistance
= eigrp_calculate_metrics (eigrp
, &metric
);
313 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
314 eigrp_prefix_entry_add (eigrp
->topology_table
, pe
);
315 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
317 ne
= eigrp_neighbor_entry_new ();
319 ne
->reported_metric
= metric
;
320 ne
->total_metric
= metric
;
321 ne
->distance
= eigrp_calculate_metrics (eigrp
, &metric
);
322 ne
->reported_distance
= 0;
324 ne
->adv_router
= eigrp
->neighbor_self
;
325 ne
->flags
= EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG
;
326 eigrp_neighbor_entry_add (pe
, ne
);
328 for (ALL_LIST_ELEMENTS (eigrp
->eiflist
, node
, nnode
, ei2
))
330 if (ei2
->nbrs
->count
!= 0)
332 eigrp_update_send (ei2
);
336 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
337 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
343 eigrp_if_down (struct eigrp_interface
*ei
)
345 struct listnode
*node
, *nnode
;
346 struct eigrp_neighbor
*nbr
;
351 /* Shutdown packet reception and sending */
353 THREAD_OFF (ei
->t_hello
);
355 eigrp_if_stream_unset (ei
);
357 /*Set infinite metrics to routes learned by this interface and start query process*/
358 for (ALL_LIST_ELEMENTS (ei
->nbrs
, node
, nnode
, nbr
))
360 eigrp_nbr_delete(nbr
);
367 eigrp_if_stream_set (struct eigrp_interface
*ei
)
369 /* set output fifo queue. */
370 if (ei
->obuf
== NULL
)
371 ei
->obuf
= eigrp_fifo_new ();
375 eigrp_if_stream_unset (struct eigrp_interface
*ei
)
377 struct eigrp
*eigrp
= ei
->eigrp
;
381 eigrp_fifo_free (ei
->obuf
);
386 listnode_delete (eigrp
->oi_write_q
, ei
);
387 if (list_isempty (eigrp
->oi_write_q
))
388 thread_cancel (eigrp
->t_write
);
395 eigrp_if_set_multicast (struct eigrp_interface
*ei
)
397 if ((EIGRP_IF_PASSIVE_STATUS (ei
) == EIGRP_IF_ACTIVE
))
399 /* The interface should belong to the EIGRP-all-routers group. */
400 if (!EI_MEMBER_CHECK (ei
, MEMBER_ALLROUTERS
)
401 && (eigrp_if_add_allspfrouters (ei
->eigrp
, ei
->address
,
402 ei
->ifp
->ifindex
) >= 0))
403 /* Set the flag only if the system call to join succeeded. */
404 EI_MEMBER_JOINED (ei
, MEMBER_ALLROUTERS
);
408 /* The interface should NOT belong to the EIGRP-all-routers group. */
409 if (EI_MEMBER_CHECK (ei
, MEMBER_ALLROUTERS
))
411 /* Only actually drop if this is the last reference */
412 if (EI_MEMBER_COUNT (ei
, MEMBER_ALLROUTERS
) == 1)
413 eigrp_if_drop_allspfrouters (ei
->eigrp
, ei
->address
,
415 /* Unset the flag regardless of whether the system call to leave
416 the group succeeded, since it's much safer to assume that
417 we are not a member. */
418 EI_MEMBER_LEFT (ei
, MEMBER_ALLROUTERS
);
424 eigrp_default_iftype (struct interface
*ifp
)
426 if (if_is_pointopoint (ifp
))
427 return EIGRP_IFTYPE_POINTOPOINT
;
428 else if (if_is_loopback (ifp
))
429 return EIGRP_IFTYPE_LOOPBACK
;
431 return EIGRP_IFTYPE_BROADCAST
;
435 eigrp_if_free (struct eigrp_interface
*ei
, int source
)
437 struct prefix_ipv4 dest_addr
;
438 struct eigrp_prefix_entry
*pe
;
439 struct eigrp
*eigrp
= eigrp_lookup ();
441 if (source
== INTERFACE_DOWN_BY_VTY
)
443 THREAD_OFF (ei
->t_hello
);
444 eigrp_hello_send(ei
,EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
447 dest_addr
.family
= AF_INET
;
448 dest_addr
.prefix
= ei
->connected
->address
->u
.prefix4
;
449 dest_addr
.prefixlen
= ei
->connected
->address
->prefixlen
;
450 apply_mask_ipv4(&dest_addr
);
451 pe
= eigrp_topology_table_lookup_ipv4 (eigrp
->topology_table
, &dest_addr
);
453 eigrp_prefix_entry_delete (eigrp
->topology_table
, pe
);
457 list_delete (ei
->nbrs
);
458 eigrp_delete_from_if (ei
->ifp
, ei
);
459 listnode_delete (ei
->eigrp
->eiflist
, ei
);
461 thread_cancel_event (master
, ei
);
463 memset (ei
, 0, sizeof (*ei
));
464 XFREE (MTYPE_EIGRP_IF
, ei
);
468 eigrp_delete_from_if (struct interface
*ifp
, struct eigrp_interface
*ei
)
470 struct route_node
*rn
;
474 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
476 rn
= route_node_lookup (IF_OIFS (ei
->ifp
), &p
);
480 route_unlock_node (rn
);
481 route_unlock_node (rn
);
484 /* Simulate down/up on the interface. This is needed, for example, when
487 eigrp_if_reset (struct interface
*ifp
)
489 struct route_node
*rn
;
491 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
493 struct eigrp_interface
*ei
;
495 if ((ei
= rn
->info
) == NULL
)
503 struct eigrp_interface
*
504 eigrp_if_lookup_by_local_addr (struct eigrp
*eigrp
, struct interface
*ifp
,
505 struct in_addr address
)
507 struct listnode
*node
;
508 struct eigrp_interface
*ei
;
510 for (ALL_LIST_ELEMENTS_RO (eigrp
->eiflist
, node
, ei
))
512 if (ifp
&& ei
->ifp
!= ifp
)
515 if (IPV4_ADDR_SAME (&address
, &ei
->address
->u
.prefix4
))
523 * @fn eigrp_if_lookup_by_name
525 * @param[in] eigrp EIGRP process
526 * @param[in] if_name Name of the interface
528 * @return struct eigrp_interface *
531 * Function is used for lookup interface by name.
533 struct eigrp_interface
*
534 eigrp_if_lookup_by_name (struct eigrp
*eigrp
, const char *if_name
)
536 struct eigrp_interface
*ei
;
537 struct listnode
*node
;
539 /* iterate over all eigrp interfaces */
540 for (ALL_LIST_ELEMENTS_RO (eigrp
->eiflist
, node
, ei
))
542 /* compare int name with eigrp interface's name */
543 if(strcmp(ei
->ifp
->name
, if_name
) == 0)
552 /* determine receiving interface by ifp and source address */
553 struct eigrp_interface
*
554 eigrp_if_lookup_recv_if (struct eigrp
*eigrp
, struct in_addr src
,
555 struct interface
*ifp
)
557 struct route_node
*rn
;
558 struct prefix_ipv4 addr
;
559 struct eigrp_interface
*ei
, *match
;
561 addr
.family
= AF_INET
;
563 addr
.prefixlen
= IPV4_MAX_BITLEN
;
567 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
571 if (!ei
) /* oi can be NULL for PtP aliases */
574 if (if_is_loopback (ei
->ifp
))
577 if (prefix_match (CONNECTED_PREFIX (ei
->connected
),
578 (struct prefix
*) &addr
))
581 || (match
->address
->prefixlen
< ei
->address
->prefixlen
))
590 eigrp_bandwidth_to_scaled (u_int32_t bandwidth
)
592 uint64_t temp_bandwidth
= (256ull * 10000000) / bandwidth
;
595 temp_bandwidth
< EIGRP_MAX_METRIC
? temp_bandwidth
: EIGRP_MAX_METRIC
;
597 return (u_int32_t
) temp_bandwidth
;
601 eigrp_scaled_to_bandwidth (u_int32_t scaled
)
603 uint64_t temp_scaled
= scaled
* (256ull * 10000000);
606 temp_scaled
< EIGRP_MAX_METRIC
? temp_scaled
: EIGRP_MAX_METRIC
;
608 return (u_int32_t
) temp_scaled
;
612 eigrp_delay_to_scaled (u_int32_t delay
)
618 eigrp_scaled_to_delay (u_int32_t scaled
)