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
)
142 struct route_node
*rn
;
144 route_table_finish (IF_OIFS (ifp
));
146 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
148 eigrp_del_if_params (rn
->info
);
149 route_table_finish (IF_OIFS_PARAMS (ifp
));
151 XFREE (MTYPE_EIGRP_IF_INFO
, ifp
->info
);
157 struct list
*eigrp_iflist
;
162 /* Initialize Zebra interface data structure. */
163 if_add_hook (IF_NEW_HOOK
, eigrp_if_new_hook
);
164 if_add_hook (IF_DELETE_HOOK
, eigrp_if_delete_hook
);
168 eigrp_if_new_hook (struct interface
*ifp
)
172 ifp
->info
= XCALLOC (MTYPE_EIGRP_IF_INFO
, sizeof (struct eigrp_if_info
));
174 IF_OIFS (ifp
) = route_table_init ();
175 IF_OIFS_PARAMS (ifp
) = route_table_init ();
177 IF_DEF_PARAMS (ifp
) = eigrp_new_if_params ();
179 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
180 IF_DEF_PARAMS (ifp
)->v_hello
= (u_int32_t
) EIGRP_HELLO_INTERVAL_DEFAULT
;
182 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
183 IF_DEF_PARAMS (ifp
)->v_wait
= (u_int16_t
) EIGRP_HOLD_INTERVAL_DEFAULT
;
185 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), bandwidth
);
186 IF_DEF_PARAMS (ifp
)->bandwidth
= (u_int32_t
) EIGRP_BANDWIDTH_DEFAULT
;
188 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), delay
);
189 IF_DEF_PARAMS (ifp
)->delay
= (u_int32_t
) EIGRP_DELAY_DEFAULT
;
191 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), reliability
);
192 IF_DEF_PARAMS (ifp
)->reliability
= (u_char
) EIGRP_RELIABILITY_DEFAULT
;
194 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), load
);
195 IF_DEF_PARAMS (ifp
)->load
= (u_char
) EIGRP_LOAD_DEFAULT
;
197 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
198 IF_DEF_PARAMS (ifp
)->auth_type
= EIGRP_AUTH_TYPE_NONE
;
200 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_keychain
);
201 IF_DEF_PARAMS (ifp
)->auth_keychain
= NULL
;
206 struct eigrp_if_params
*
207 eigrp_new_if_params (void)
209 struct eigrp_if_params
*eip
;
211 eip
= XCALLOC (MTYPE_EIGRP_IF_PARAMS
, sizeof (struct eigrp_if_params
));
215 UNSET_IF_PARAM (eip
, passive_interface
);
216 UNSET_IF_PARAM (eip
, v_hello
);
217 UNSET_IF_PARAM (eip
, v_wait
);
218 UNSET_IF_PARAM (eip
, bandwidth
);
219 UNSET_IF_PARAM (eip
, delay
);
220 UNSET_IF_PARAM (eip
, reliability
);
221 UNSET_IF_PARAM (eip
, load
);
222 UNSET_IF_PARAM (eip
, auth_keychain
);
223 UNSET_IF_PARAM (eip
, auth_type
);
230 eigrp_del_if_params (struct eigrp_if_params
*eip
)
232 if(eip
->auth_keychain
)
233 free(eip
->auth_keychain
);
235 XFREE (MTYPE_EIGRP_IF_PARAMS
, eip
);
238 struct eigrp_if_params
*
239 eigrp_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
241 struct prefix_ipv4 p
;
242 struct route_node
*rn
;
245 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
248 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*) &p
);
252 route_unlock_node (rn
);
260 eigrp_if_up (struct eigrp_interface
*ei
)
262 struct eigrp_prefix_entry
*pe
;
263 struct eigrp_neighbor_entry
*ne
;
264 struct eigrp_metrics metric
;
265 struct eigrp_interface
*ei2
;
266 struct listnode
*node
, *nnode
;
267 struct eigrp
*eigrp
= eigrp_lookup ();
273 eigrp_adjust_sndbuflen (eigrp
, ei
->ifp
->mtu
);
275 zlog_warn ("%s: eigrp_lookup () returned NULL", __func__
);
276 eigrp_if_stream_set (ei
);
278 /* Set multicast memberships appropriately for new state. */
279 eigrp_if_set_multicast (ei
);
281 thread_add_event (master
, eigrp_hello_timer
, ei
, (1));
284 metric
.bandwith
= eigrp_bandwidth_to_scaled (EIGRP_IF_PARAM (ei
,bandwidth
));
285 metric
.delay
= eigrp_delay_to_scaled (EIGRP_IF_PARAM (ei
,delay
));
286 metric
.load
= EIGRP_IF_PARAM (ei
,load
);
287 metric
.reliability
= EIGRP_IF_PARAM (ei
,reliability
);
288 metric
.mtu
[0] = 0xDC;
289 metric
.mtu
[1] = 0x05;
290 metric
.mtu
[2] = 0x00;
291 metric
.hop_count
= 0;
295 /*Add connected entry to topology table*/
297 struct prefix_ipv4
*dest_addr
= prefix_ipv4_new ();
299 dest_addr
->family
= AF_INET
;
300 dest_addr
->prefix
= ei
->connected
->address
->u
.prefix4
;
301 dest_addr
->prefixlen
= ei
->connected
->address
->prefixlen
;
302 apply_mask_ipv4 (dest_addr
);
303 pe
= eigrp_topology_table_lookup_ipv4 (eigrp
->topology_table
, dest_addr
);
307 pe
= eigrp_prefix_entry_new ();
308 pe
->serno
= eigrp
->serno
;
309 pe
->destination_ipv4
= dest_addr
;
311 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
313 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
314 pe
->fdistance
= eigrp_calculate_metrics (eigrp
, &metric
);
315 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
316 eigrp_prefix_entry_add (eigrp
->topology_table
, pe
);
317 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
319 ne
= eigrp_neighbor_entry_new ();
321 ne
->reported_metric
= metric
;
322 ne
->total_metric
= metric
;
323 ne
->distance
= eigrp_calculate_metrics (eigrp
, &metric
);
324 ne
->reported_distance
= 0;
326 ne
->adv_router
= eigrp
->neighbor_self
;
327 ne
->flags
= EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG
;
328 eigrp_neighbor_entry_add (pe
, ne
);
330 for (ALL_LIST_ELEMENTS (eigrp
->eiflist
, node
, nnode
, ei2
))
332 if (ei2
->nbrs
->count
!= 0)
334 eigrp_update_send (ei2
);
338 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
339 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
345 eigrp_if_down (struct eigrp_interface
*ei
)
347 struct listnode
*node
, *nnode
;
348 struct eigrp_neighbor
*nbr
;
353 /* Shutdown packet reception and sending */
355 THREAD_OFF (ei
->t_hello
);
357 eigrp_if_stream_unset (ei
);
359 /*Set infinite metrics to routes learned by this interface and start query process*/
360 for (ALL_LIST_ELEMENTS (ei
->nbrs
, node
, nnode
, nbr
))
362 eigrp_nbr_delete(nbr
);
369 eigrp_if_stream_set (struct eigrp_interface
*ei
)
371 /* set output fifo queue. */
372 if (ei
->obuf
== NULL
)
373 ei
->obuf
= eigrp_fifo_new ();
377 eigrp_if_stream_unset (struct eigrp_interface
*ei
)
379 struct eigrp
*eigrp
= ei
->eigrp
;
383 eigrp_fifo_free (ei
->obuf
);
388 listnode_delete (eigrp
->oi_write_q
, ei
);
389 if (list_isempty (eigrp
->oi_write_q
))
390 thread_cancel (eigrp
->t_write
);
397 eigrp_if_set_multicast (struct eigrp_interface
*ei
)
399 if ((EIGRP_IF_PASSIVE_STATUS (ei
) == EIGRP_IF_ACTIVE
))
401 /* The interface should belong to the EIGRP-all-routers group. */
402 if (!EI_MEMBER_CHECK (ei
, MEMBER_ALLROUTERS
)
403 && (eigrp_if_add_allspfrouters (ei
->eigrp
, ei
->address
,
404 ei
->ifp
->ifindex
) >= 0))
405 /* Set the flag only if the system call to join succeeded. */
406 EI_MEMBER_JOINED (ei
, MEMBER_ALLROUTERS
);
410 /* The interface should NOT belong to the EIGRP-all-routers group. */
411 if (EI_MEMBER_CHECK (ei
, MEMBER_ALLROUTERS
))
413 /* Only actually drop if this is the last reference */
414 if (EI_MEMBER_COUNT (ei
, MEMBER_ALLROUTERS
) == 1)
415 eigrp_if_drop_allspfrouters (ei
->eigrp
, ei
->address
,
417 /* Unset the flag regardless of whether the system call to leave
418 the group succeeded, since it's much safer to assume that
419 we are not a member. */
420 EI_MEMBER_LEFT (ei
, MEMBER_ALLROUTERS
);
426 eigrp_default_iftype (struct interface
*ifp
)
428 if (if_is_pointopoint (ifp
))
429 return EIGRP_IFTYPE_POINTOPOINT
;
430 else if (if_is_loopback (ifp
))
431 return EIGRP_IFTYPE_LOOPBACK
;
433 return EIGRP_IFTYPE_BROADCAST
;
437 eigrp_if_free (struct eigrp_interface
*ei
, int source
)
439 struct prefix_ipv4 dest_addr
;
440 struct eigrp_prefix_entry
*pe
;
441 struct eigrp
*eigrp
= eigrp_lookup ();
443 if (source
== INTERFACE_DOWN_BY_VTY
)
445 THREAD_OFF (ei
->t_hello
);
446 eigrp_hello_send(ei
,EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
449 dest_addr
.family
= AF_INET
;
450 dest_addr
.prefix
= ei
->connected
->address
->u
.prefix4
;
451 dest_addr
.prefixlen
= ei
->connected
->address
->prefixlen
;
452 apply_mask_ipv4(&dest_addr
);
453 pe
= eigrp_topology_table_lookup_ipv4 (eigrp
->topology_table
, &dest_addr
);
455 eigrp_prefix_entry_delete (eigrp
->topology_table
, pe
);
459 list_delete (ei
->nbrs
);
460 eigrp_delete_from_if (ei
->ifp
, ei
);
461 listnode_delete (ei
->eigrp
->eiflist
, ei
);
463 thread_cancel_event (master
, ei
);
465 memset (ei
, 0, sizeof (*ei
));
466 XFREE (MTYPE_EIGRP_IF
, ei
);
470 eigrp_delete_from_if (struct interface
*ifp
, struct eigrp_interface
*ei
)
472 struct route_node
*rn
;
476 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
478 rn
= route_node_lookup (IF_OIFS (ei
->ifp
), &p
);
482 route_unlock_node (rn
);
483 route_unlock_node (rn
);
486 /* Simulate down/up on the interface. This is needed, for example, when
489 eigrp_if_reset (struct interface
*ifp
)
491 struct route_node
*rn
;
493 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
495 struct eigrp_interface
*ei
;
497 if ((ei
= rn
->info
) == NULL
)
505 struct eigrp_interface
*
506 eigrp_if_lookup_by_local_addr (struct eigrp
*eigrp
, struct interface
*ifp
,
507 struct in_addr address
)
509 struct listnode
*node
;
510 struct eigrp_interface
*ei
;
512 for (ALL_LIST_ELEMENTS_RO (eigrp
->eiflist
, node
, ei
))
514 if (ifp
&& ei
->ifp
!= ifp
)
517 if (IPV4_ADDR_SAME (&address
, &ei
->address
->u
.prefix4
))
525 * @fn eigrp_if_lookup_by_name
527 * @param[in] eigrp EIGRP process
528 * @param[in] if_name Name of the interface
530 * @return struct eigrp_interface *
533 * Function is used for lookup interface by name.
535 struct eigrp_interface
*
536 eigrp_if_lookup_by_name (struct eigrp
*eigrp
, const char *if_name
)
538 struct eigrp_interface
*ei
;
539 struct listnode
*node
;
541 /* iterate over all eigrp interfaces */
542 for (ALL_LIST_ELEMENTS_RO (eigrp
->eiflist
, node
, ei
))
544 /* compare int name with eigrp interface's name */
545 if(strcmp(ei
->ifp
->name
, if_name
) == 0)
554 /* determine receiving interface by ifp and source address */
555 struct eigrp_interface
*
556 eigrp_if_lookup_recv_if (struct eigrp
*eigrp
, struct in_addr src
,
557 struct interface
*ifp
)
559 struct route_node
*rn
;
560 struct prefix_ipv4 addr
;
561 struct eigrp_interface
*ei
, *match
;
563 addr
.family
= AF_INET
;
565 addr
.prefixlen
= IPV4_MAX_BITLEN
;
569 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
573 if (!ei
) /* oi can be NULL for PtP aliases */
576 if (if_is_loopback (ei
->ifp
))
579 if (prefix_match (CONNECTED_PREFIX (ei
->connected
),
580 (struct prefix
*) &addr
))
583 || (match
->address
->prefixlen
< ei
->address
->prefixlen
))
592 eigrp_bandwidth_to_scaled (u_int32_t bandwidth
)
594 u_int64_t temp_bandwidth
= (256ull * 10000000) / bandwidth
;
597 temp_bandwidth
< EIGRP_MAX_METRIC
? temp_bandwidth
: EIGRP_MAX_METRIC
;
599 return (u_int32_t
) temp_bandwidth
;
604 eigrp_scaled_to_bandwidth (u_int32_t scaled
)
606 u_int64_t temp_scaled
= scaled
* (256ull * 10000000);
609 temp_scaled
< EIGRP_MAX_METRIC
? temp_scaled
: EIGRP_MAX_METRIC
;
611 return (u_int32_t
) temp_scaled
;
615 eigrp_delay_to_scaled (u_int32_t delay
)
621 eigrp_scaled_to_delay (u_int32_t scaled
)