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"
58 struct eigrp_interface
*eigrp_if_new(struct eigrp
*eigrp
, struct interface
*ifp
,
61 struct eigrp_interface
*ei
= ifp
->info
;
67 ei
= XCALLOC(MTYPE_EIGRP_IF
, sizeof(struct eigrp_interface
));
69 /* Set zebra interface pointer. */
74 listnode_add(eigrp
->eiflist
, ei
);
76 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
78 /* Initialize neighbor list. */
79 ei
->nbrs
= list_new();
81 ei
->crypt_seqnum
= time(NULL
);
83 /* Initialize lists */
84 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++) {
87 ei
->routemap
[i
] = NULL
;
92 ei
->params
.v_hello
= EIGRP_HELLO_INTERVAL_DEFAULT
;
93 ei
->params
.v_wait
= EIGRP_HOLD_INTERVAL_DEFAULT
;
94 ei
->params
.bandwidth
= EIGRP_BANDWIDTH_DEFAULT
;
95 ei
->params
.delay
= EIGRP_DELAY_DEFAULT
;
96 ei
->params
.reliability
= EIGRP_RELIABILITY_DEFAULT
;
97 ei
->params
.load
= EIGRP_LOAD_DEFAULT
;
98 ei
->params
.auth_type
= EIGRP_AUTH_TYPE_NONE
;
99 ei
->params
.auth_keychain
= NULL
;
104 int eigrp_if_delete_hook(struct interface
*ifp
)
106 struct eigrp_interface
*ei
= ifp
->info
;
112 list_delete_and_null(&ei
->nbrs
);
115 listnode_delete(eigrp
->eiflist
, ei
);
117 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
123 struct list
*eigrp_iflist
;
127 /* Initialize Zebra interface data structure. */
128 //hook_register_prio(if_add, 0, eigrp_if_new);
129 hook_register_prio(if_del
, 0, eigrp_if_delete_hook
);
134 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
136 if (eip
->auth_keychain
)
137 free(eip
->auth_keychain
);
140 int eigrp_if_up(struct eigrp_interface
*ei
)
142 struct eigrp_prefix_entry
*pe
;
143 struct eigrp_nexthop_entry
*ne
;
144 struct eigrp_metrics metric
;
145 struct eigrp_interface
*ei2
;
146 struct listnode
*node
, *nnode
;
153 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
155 eigrp_if_stream_set(ei
);
157 /* Set multicast memberships appropriately for new state. */
158 eigrp_if_set_multicast(ei
);
160 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), NULL
);
164 eigrp_bandwidth_to_scaled(ei
->params
.bandwidth
);
165 metric
.delay
= eigrp_delay_to_scaled(ei
->params
.delay
);
166 metric
.load
= ei
->params
.load
;
167 metric
.reliability
= ei
->params
.reliability
;
168 metric
.mtu
[0] = 0xDC;
169 metric
.mtu
[1] = 0x05;
170 metric
.mtu
[2] = 0x00;
171 metric
.hop_count
= 0;
175 /*Add connected entry to topology table*/
177 ne
= eigrp_nexthop_entry_new();
179 ne
->reported_metric
= metric
;
180 ne
->total_metric
= metric
;
181 ne
->distance
= eigrp_calculate_metrics(eigrp
, metric
);
182 ne
->reported_distance
= 0;
183 ne
->adv_router
= eigrp
->neighbor_self
;
184 ne
->flags
= EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG
;
186 struct prefix dest_addr
;
188 dest_addr
.family
= AF_INET
;
189 dest_addr
.u
.prefix4
= ei
->connected
->address
->u
.prefix4
;
190 dest_addr
.prefixlen
= ei
->connected
->address
->prefixlen
;
191 apply_mask(&dest_addr
);
192 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
196 pe
= eigrp_prefix_entry_new();
197 pe
->serno
= eigrp
->serno
;
198 pe
->destination
= (struct prefix
*)prefix_ipv4_new();
199 prefix_copy(pe
->destination
, &dest_addr
);
201 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
204 pe
->reported_metric
= metric
;
205 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
206 pe
->fdistance
= eigrp_calculate_metrics(eigrp
, metric
);
207 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
208 eigrp_prefix_entry_add(eigrp
->topology_table
, pe
);
209 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
211 eigrp_nexthop_entry_add(pe
, ne
);
213 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
214 eigrp_update_send(ei2
);
217 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
218 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
220 struct eigrp_fsm_action_message msg
;
223 eigrp_nexthop_entry_add(pe
, ne
);
225 msg
.packet_type
= EIGRP_OPC_UPDATE
;
227 msg
.data_type
= EIGRP_CONNECTED
;
228 msg
.adv_router
= NULL
;
232 eigrp_fsm_event(&msg
);
238 int eigrp_if_down(struct eigrp_interface
*ei
)
240 struct listnode
*node
, *nnode
;
241 struct eigrp_neighbor
*nbr
;
246 /* Shutdown packet reception and sending */
248 THREAD_OFF(ei
->t_hello
);
250 eigrp_if_stream_unset(ei
);
252 /*Set infinite metrics to routes learned by this interface and start
254 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
255 eigrp_nbr_delete(nbr
);
261 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
263 /* set output fifo queue. */
264 if (ei
->obuf
== NULL
)
265 ei
->obuf
= eigrp_fifo_new();
268 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
270 struct eigrp
*eigrp
= ei
->eigrp
;
273 eigrp_fifo_free(ei
->obuf
);
276 if (ei
->on_write_q
) {
277 listnode_delete(eigrp
->oi_write_q
, ei
);
278 if (list_isempty(eigrp
->oi_write_q
))
279 thread_cancel(eigrp
->t_write
);
285 bool eigrp_if_is_passive(struct eigrp_interface
*ei
)
287 if (ei
->params
.passive_interface
== EIGRP_IF_ACTIVE
)
290 if (ei
->eigrp
->passive_interface_default
== EIGRP_IF_ACTIVE
)
296 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
298 if (!eigrp_if_is_passive(ei
)) {
299 /* The interface should belong to the EIGRP-all-routers group.
301 if (!ei
->member_allrouters
302 && (eigrp_if_add_allspfrouters(ei
->eigrp
, ei
->address
,
305 /* Set the flag only if the system call to join
307 ei
->member_allrouters
= true;
309 /* The interface should NOT belong to the EIGRP-all-routers
311 if (ei
->member_allrouters
) {
312 /* Only actually drop if this is the last reference */
313 eigrp_if_drop_allspfrouters(ei
->eigrp
,
316 /* Unset the flag regardless of whether the system call
318 the group succeeded, since it's much safer to assume
320 we are not a member. */
321 ei
->member_allrouters
= false;
326 u_char
eigrp_default_iftype(struct interface
*ifp
)
328 if (if_is_pointopoint(ifp
))
329 return EIGRP_IFTYPE_POINTOPOINT
;
330 else if (if_is_loopback(ifp
))
331 return EIGRP_IFTYPE_LOOPBACK
;
333 return EIGRP_IFTYPE_BROADCAST
;
336 void eigrp_if_free(struct eigrp_interface
*ei
,
339 struct prefix dest_addr
;
340 struct eigrp_prefix_entry
*pe
;
341 struct eigrp
*eigrp
= eigrp_lookup();
343 if (source
== INTERFACE_DOWN_BY_VTY
) {
344 THREAD_OFF(ei
->t_hello
);
345 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
348 dest_addr
= *ei
->connected
->address
;
349 apply_mask(&dest_addr
);
350 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
353 eigrp_prefix_entry_delete(eigrp
->topology_table
, pe
);
357 list_delete_and_null(&ei
->nbrs
);
358 listnode_delete(ei
->eigrp
->eiflist
, ei
);
361 /* Simulate down/up on the interface. This is needed, for example, when
363 void eigrp_if_reset(struct interface
*ifp
)
365 struct eigrp_interface
*ei
= ifp
->info
;
374 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
375 struct interface
*ifp
,
376 struct in_addr address
)
378 struct listnode
*node
;
379 struct eigrp_interface
*ei
;
381 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
382 if (ifp
&& ei
->ifp
!= ifp
)
385 if (IPV4_ADDR_SAME(&address
, &ei
->address
->u
.prefix4
))
393 * @fn eigrp_if_lookup_by_name
395 * @param[in] eigrp EIGRP process
396 * @param[in] if_name Name of the interface
398 * @return struct eigrp_interface *
401 * Function is used for lookup interface by name.
403 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
406 struct eigrp_interface
*ei
;
407 struct listnode
*node
;
409 /* iterate over all eigrp interfaces */
410 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
411 /* compare int name with eigrp interface's name */
412 if (strcmp(ei
->ifp
->name
, if_name
) == 0) {
420 u_int32_t
eigrp_bandwidth_to_scaled(u_int32_t bandwidth
)
422 uint64_t temp_bandwidth
= (256ull * 10000000) / bandwidth
;
424 temp_bandwidth
= temp_bandwidth
< EIGRP_MAX_METRIC
? temp_bandwidth
427 return (u_int32_t
)temp_bandwidth
;
430 u_int32_t
eigrp_scaled_to_bandwidth(u_int32_t scaled
)
432 uint64_t temp_scaled
= scaled
* (256ull * 10000000);
435 temp_scaled
< EIGRP_MAX_METRIC
? temp_scaled
: EIGRP_MAX_METRIC
;
437 return (u_int32_t
)temp_scaled
;
440 u_int32_t
eigrp_delay_to_scaled(u_int32_t delay
)
445 u_int32_t
eigrp_scaled_to_delay(u_int32_t scaled
)