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(&ei
->nbrs
);
115 listnode_delete(eigrp
->eiflist
, ei
);
117 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
123 struct list
*eigrp_iflist
;
125 void eigrp_if_init(void)
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
);
133 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
135 if (eip
->auth_keychain
)
136 free(eip
->auth_keychain
);
139 int eigrp_if_up(struct eigrp_interface
*ei
)
141 struct eigrp_prefix_entry
*pe
;
142 struct eigrp_nexthop_entry
*ne
;
143 struct eigrp_metrics metric
;
144 struct eigrp_interface
*ei2
;
145 struct listnode
*node
, *nnode
;
152 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
154 eigrp_if_stream_set(ei
);
156 /* Set multicast memberships appropriately for new state. */
157 eigrp_if_set_multicast(ei
);
159 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), NULL
);
162 metric
.bandwidth
= eigrp_bandwidth_to_scaled(ei
->params
.bandwidth
);
163 metric
.delay
= eigrp_delay_to_scaled(ei
->params
.delay
);
164 metric
.load
= ei
->params
.load
;
165 metric
.reliability
= ei
->params
.reliability
;
166 metric
.mtu
[0] = 0xDC;
167 metric
.mtu
[1] = 0x05;
168 metric
.mtu
[2] = 0x00;
169 metric
.hop_count
= 0;
173 /*Add connected entry to topology table*/
175 ne
= eigrp_nexthop_entry_new();
177 ne
->reported_metric
= metric
;
178 ne
->total_metric
= metric
;
179 ne
->distance
= eigrp_calculate_metrics(eigrp
, metric
);
180 ne
->reported_distance
= 0;
181 ne
->adv_router
= eigrp
->neighbor_self
;
182 ne
->flags
= EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG
;
184 struct prefix dest_addr
;
186 dest_addr
= *ei
->address
;
187 apply_mask(&dest_addr
);
188 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
192 pe
= eigrp_prefix_entry_new();
193 pe
->serno
= eigrp
->serno
;
194 pe
->destination
= (struct prefix
*)prefix_ipv4_new();
195 prefix_copy(pe
->destination
, &dest_addr
);
197 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
200 pe
->reported_metric
= metric
;
201 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
202 pe
->fdistance
= eigrp_calculate_metrics(eigrp
, metric
);
203 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
204 eigrp_prefix_entry_add(eigrp
->topology_table
, pe
);
205 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
207 eigrp_nexthop_entry_add(pe
, ne
);
209 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
210 eigrp_update_send(ei2
);
213 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
214 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
216 struct eigrp_fsm_action_message msg
;
219 eigrp_nexthop_entry_add(pe
, ne
);
221 msg
.packet_type
= EIGRP_OPC_UPDATE
;
223 msg
.data_type
= EIGRP_CONNECTED
;
224 msg
.adv_router
= NULL
;
228 eigrp_fsm_event(&msg
);
234 int eigrp_if_down(struct eigrp_interface
*ei
)
236 struct listnode
*node
, *nnode
;
237 struct eigrp_neighbor
*nbr
;
242 /* Shutdown packet reception and sending */
244 THREAD_OFF(ei
->t_hello
);
246 eigrp_if_stream_unset(ei
);
248 /*Set infinite metrics to routes learned by this interface and start
250 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
251 eigrp_nbr_delete(nbr
);
257 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
259 /* set output fifo queue. */
260 if (ei
->obuf
== NULL
)
261 ei
->obuf
= eigrp_fifo_new();
264 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
266 struct eigrp
*eigrp
= ei
->eigrp
;
269 eigrp_fifo_free(ei
->obuf
);
272 if (ei
->on_write_q
) {
273 listnode_delete(eigrp
->oi_write_q
, ei
);
274 if (list_isempty(eigrp
->oi_write_q
))
275 thread_cancel(eigrp
->t_write
);
281 bool eigrp_if_is_passive(struct eigrp_interface
*ei
)
283 if (ei
->params
.passive_interface
== EIGRP_IF_ACTIVE
)
286 if (ei
->eigrp
->passive_interface_default
== EIGRP_IF_ACTIVE
)
292 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
294 if (!eigrp_if_is_passive(ei
)) {
295 /* The interface should belong to the EIGRP-all-routers group.
297 if (!ei
->member_allrouters
298 && (eigrp_if_add_allspfrouters(ei
->eigrp
, ei
->address
,
301 /* Set the flag only if the system call to join
303 ei
->member_allrouters
= true;
305 /* The interface should NOT belong to the EIGRP-all-routers
307 if (ei
->member_allrouters
) {
308 /* Only actually drop if this is the last reference */
309 eigrp_if_drop_allspfrouters(ei
->eigrp
, ei
->address
,
311 /* Unset the flag regardless of whether the system call
313 the group succeeded, since it's much safer to assume
315 we are not a member. */
316 ei
->member_allrouters
= false;
321 uint8_t eigrp_default_iftype(struct interface
*ifp
)
323 if (if_is_pointopoint(ifp
))
324 return EIGRP_IFTYPE_POINTOPOINT
;
325 else if (if_is_loopback(ifp
))
326 return EIGRP_IFTYPE_LOOPBACK
;
328 return EIGRP_IFTYPE_BROADCAST
;
331 void eigrp_if_free(struct eigrp_interface
*ei
, int source
)
333 struct prefix dest_addr
;
334 struct eigrp_prefix_entry
*pe
;
335 struct eigrp
*eigrp
= eigrp_lookup();
340 if (source
== INTERFACE_DOWN_BY_VTY
) {
341 THREAD_OFF(ei
->t_hello
);
342 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
345 dest_addr
= *ei
->address
;
346 apply_mask(&dest_addr
);
347 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
350 eigrp_prefix_entry_delete(eigrp
->topology_table
, pe
);
354 list_delete(&ei
->nbrs
);
355 listnode_delete(ei
->eigrp
->eiflist
, ei
);
358 /* Simulate down/up on the interface. This is needed, for example, when
360 void eigrp_if_reset(struct interface
*ifp
)
362 struct eigrp_interface
*ei
= ifp
->info
;
371 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
372 struct interface
*ifp
,
373 struct in_addr address
)
375 struct listnode
*node
;
376 struct eigrp_interface
*ei
;
378 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
379 if (ifp
&& ei
->ifp
!= ifp
)
382 if (IPV4_ADDR_SAME(&address
, &ei
->address
->u
.prefix4
))
390 * @fn eigrp_if_lookup_by_name
392 * @param[in] eigrp EIGRP process
393 * @param[in] if_name Name of the interface
395 * @return struct eigrp_interface *
398 * Function is used for lookup interface by name.
400 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
403 struct eigrp_interface
*ei
;
404 struct listnode
*node
;
406 /* iterate over all eigrp interfaces */
407 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
408 /* compare int name with eigrp interface's name */
409 if (strcmp(ei
->ifp
->name
, if_name
) == 0) {
417 uint32_t eigrp_bandwidth_to_scaled(uint32_t bandwidth
)
419 uint64_t temp_bandwidth
= (256ull * 10000000) / bandwidth
;
421 temp_bandwidth
= temp_bandwidth
< EIGRP_MAX_METRIC
? temp_bandwidth
424 return (uint32_t)temp_bandwidth
;
427 uint32_t eigrp_scaled_to_bandwidth(uint32_t scaled
)
429 uint64_t temp_scaled
= scaled
* (256ull * 10000000);
432 temp_scaled
< EIGRP_MAX_METRIC
? temp_scaled
: EIGRP_MAX_METRIC
;
434 return (uint32_t)temp_scaled
;
437 uint32_t eigrp_delay_to_scaled(uint32_t delay
)
442 uint32_t eigrp_scaled_to_delay(uint32_t scaled
)