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"
57 static void eigrp_delete_from_if(struct interface
*, struct eigrp_interface
*);
59 static void eigrp_add_to_if(struct interface
*ifp
, struct eigrp_interface
*ei
)
61 struct route_node
*rn
;
65 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
67 rn
= route_node_get(IF_OIFS(ifp
), &p
);
68 /* rn->info should either be NULL or equal to this ei
69 * as route_node_get may return an existing node
71 assert(!rn
->info
|| rn
->info
== ei
);
75 struct eigrp_interface
*eigrp_if_new(struct eigrp
*eigrp
, struct interface
*ifp
,
78 struct eigrp_interface
*ei
;
81 if ((ei
= eigrp_if_table_lookup(ifp
, p
)) == NULL
) {
82 ei
= XCALLOC(MTYPE_EIGRP_IF
, sizeof(struct eigrp_interface
));
83 memset(ei
, 0, sizeof(struct eigrp_interface
));
87 /* Set zebra interface pointer. */
91 eigrp_add_to_if(ifp
, ei
);
92 listnode_add(eigrp
->eiflist
, ei
);
94 ei
->type
= EIGRP_IFTYPE_BROADCAST
;
96 /* Initialize neighbor list. */
97 ei
->nbrs
= list_new();
99 ei
->crypt_seqnum
= time(NULL
);
101 /* Initialize lists */
102 for (i
= 0; i
< EIGRP_FILTER_MAX
; i
++) {
104 ei
->prefix
[i
] = NULL
;
105 ei
->routemap
[i
] = NULL
;
111 /* lookup ei for specified prefix/ifp */
112 struct eigrp_interface
*eigrp_if_table_lookup(struct interface
*ifp
,
113 struct prefix
*prefix
)
116 struct route_node
*rn
;
117 struct eigrp_interface
*rninfo
= NULL
;
120 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
122 /* route_node_get implicitly locks */
123 if ((rn
= route_node_lookup(IF_OIFS(ifp
), &p
))) {
124 rninfo
= (struct eigrp_interface
*)rn
->info
;
125 route_unlock_node(rn
);
131 int eigrp_if_delete_hook(struct interface
*ifp
)
133 struct route_node
*rn
;
135 route_table_finish(IF_OIFS(ifp
));
137 for (rn
= route_top(IF_OIFS_PARAMS(ifp
)); rn
; rn
= route_next(rn
))
139 eigrp_del_if_params(rn
->info
);
140 route_table_finish(IF_OIFS_PARAMS(ifp
));
142 XFREE(MTYPE_EIGRP_IF_INFO
, ifp
->info
);
148 struct list
*eigrp_iflist
;
152 /* Initialize Zebra interface data structure. */
153 if_add_hook(IF_NEW_HOOK
, eigrp_if_new_hook
);
154 if_add_hook(IF_DELETE_HOOK
, eigrp_if_delete_hook
);
157 int eigrp_if_new_hook(struct interface
*ifp
)
161 ifp
->info
= XCALLOC(MTYPE_EIGRP_IF_INFO
, sizeof(struct eigrp_if_info
));
163 IF_OIFS(ifp
) = route_table_init();
164 IF_OIFS_PARAMS(ifp
) = route_table_init();
166 IF_DEF_PARAMS(ifp
) = eigrp_new_if_params();
168 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_hello
);
169 IF_DEF_PARAMS(ifp
)->v_hello
= (u_int32_t
)EIGRP_HELLO_INTERVAL_DEFAULT
;
171 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_wait
);
172 IF_DEF_PARAMS(ifp
)->v_wait
= (u_int16_t
)EIGRP_HOLD_INTERVAL_DEFAULT
;
174 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), bandwidth
);
175 IF_DEF_PARAMS(ifp
)->bandwidth
= (u_int32_t
)EIGRP_BANDWIDTH_DEFAULT
;
177 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), delay
);
178 IF_DEF_PARAMS(ifp
)->delay
= (u_int32_t
)EIGRP_DELAY_DEFAULT
;
180 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), reliability
);
181 IF_DEF_PARAMS(ifp
)->reliability
= (u_char
)EIGRP_RELIABILITY_DEFAULT
;
183 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), load
);
184 IF_DEF_PARAMS(ifp
)->load
= (u_char
)EIGRP_LOAD_DEFAULT
;
186 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_type
);
187 IF_DEF_PARAMS(ifp
)->auth_type
= EIGRP_AUTH_TYPE_NONE
;
189 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_keychain
);
190 IF_DEF_PARAMS(ifp
)->auth_keychain
= NULL
;
195 struct eigrp_if_params
*eigrp_new_if_params(void)
197 struct eigrp_if_params
*eip
;
199 eip
= XCALLOC(MTYPE_EIGRP_IF_PARAMS
, sizeof(struct eigrp_if_params
));
203 UNSET_IF_PARAM(eip
, passive_interface
);
204 UNSET_IF_PARAM(eip
, v_hello
);
205 UNSET_IF_PARAM(eip
, v_wait
);
206 UNSET_IF_PARAM(eip
, bandwidth
);
207 UNSET_IF_PARAM(eip
, delay
);
208 UNSET_IF_PARAM(eip
, reliability
);
209 UNSET_IF_PARAM(eip
, load
);
210 UNSET_IF_PARAM(eip
, auth_keychain
);
211 UNSET_IF_PARAM(eip
, auth_type
);
216 void eigrp_del_if_params(struct eigrp_if_params
*eip
)
218 if (eip
->auth_keychain
)
219 free(eip
->auth_keychain
);
221 XFREE(MTYPE_EIGRP_IF_PARAMS
, eip
);
224 struct eigrp_if_params
*eigrp_lookup_if_params(struct interface
*ifp
,
227 struct prefix_ipv4 p
;
228 struct route_node
*rn
;
231 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
234 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
237 route_unlock_node(rn
);
244 int eigrp_if_up(struct eigrp_interface
*ei
)
246 struct eigrp_prefix_entry
*pe
;
247 struct eigrp_neighbor_entry
*ne
;
248 struct eigrp_metrics metric
;
249 struct eigrp_interface
*ei2
;
250 struct listnode
*node
, *nnode
;
251 struct eigrp
*eigrp
= eigrp_lookup();
257 eigrp_adjust_sndbuflen(eigrp
, ei
->ifp
->mtu
);
259 zlog_warn("%s: eigrp_lookup () returned NULL", __func__
);
260 eigrp_if_stream_set(ei
);
262 /* Set multicast memberships appropriately for new state. */
263 eigrp_if_set_multicast(ei
);
265 thread_add_event(master
, eigrp_hello_timer
, ei
, (1), NULL
);
269 eigrp_bandwidth_to_scaled(EIGRP_IF_PARAM(ei
, bandwidth
));
270 metric
.delay
= eigrp_delay_to_scaled(EIGRP_IF_PARAM(ei
, delay
));
271 metric
.load
= EIGRP_IF_PARAM(ei
, load
);
272 metric
.reliability
= EIGRP_IF_PARAM(ei
, reliability
);
273 metric
.mtu
[0] = 0xDC;
274 metric
.mtu
[1] = 0x05;
275 metric
.mtu
[2] = 0x00;
276 metric
.hop_count
= 0;
280 /*Add connected entry to topology table*/
282 struct prefix_ipv4 dest_addr
;
284 dest_addr
.family
= AF_INET
;
285 dest_addr
.prefix
= ei
->connected
->address
->u
.prefix4
;
286 dest_addr
.prefixlen
= ei
->connected
->address
->prefixlen
;
287 apply_mask_ipv4(&dest_addr
);
288 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
292 pe
= eigrp_prefix_entry_new();
293 pe
->serno
= eigrp
->serno
;
294 pe
->destination_ipv4
= prefix_ipv4_new();
295 prefix_copy((struct prefix
*)pe
->destination_ipv4
,
296 (struct prefix
*)&dest_addr
);
298 pe
->nt
= EIGRP_TOPOLOGY_TYPE_CONNECTED
;
300 pe
->state
= EIGRP_FSM_STATE_PASSIVE
;
301 pe
->fdistance
= eigrp_calculate_metrics(eigrp
, metric
);
302 pe
->req_action
|= EIGRP_FSM_NEED_UPDATE
;
303 eigrp_prefix_entry_add(eigrp
->topology_table
, pe
);
304 listnode_add(eigrp
->topology_changes_internalIPV4
, pe
);
306 ne
= eigrp_neighbor_entry_new();
308 ne
->reported_metric
= metric
;
309 ne
->total_metric
= metric
;
310 ne
->distance
= eigrp_calculate_metrics(eigrp
, metric
);
311 ne
->reported_distance
= 0;
313 ne
->adv_router
= eigrp
->neighbor_self
;
314 ne
->flags
= EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG
;
315 eigrp_neighbor_entry_add(pe
, ne
);
317 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei2
)) {
318 if (ei2
->nbrs
->count
!= 0) {
319 eigrp_update_send(ei2
);
323 pe
->req_action
&= ~EIGRP_FSM_NEED_UPDATE
;
324 listnode_delete(eigrp
->topology_changes_internalIPV4
, pe
);
329 int eigrp_if_down(struct eigrp_interface
*ei
)
331 struct listnode
*node
, *nnode
;
332 struct eigrp_neighbor
*nbr
;
337 /* Shutdown packet reception and sending */
339 THREAD_OFF(ei
->t_hello
);
341 eigrp_if_stream_unset(ei
);
343 /*Set infinite metrics to routes learned by this interface and start
345 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node
, nnode
, nbr
)) {
346 eigrp_nbr_delete(nbr
);
352 void eigrp_if_stream_set(struct eigrp_interface
*ei
)
354 /* set output fifo queue. */
355 if (ei
->obuf
== NULL
)
356 ei
->obuf
= eigrp_fifo_new();
359 void eigrp_if_stream_unset(struct eigrp_interface
*ei
)
361 struct eigrp
*eigrp
= ei
->eigrp
;
364 eigrp_fifo_free(ei
->obuf
);
367 if (ei
->on_write_q
) {
368 listnode_delete(eigrp
->oi_write_q
, ei
);
369 if (list_isempty(eigrp
->oi_write_q
))
370 thread_cancel(eigrp
->t_write
);
376 void eigrp_if_set_multicast(struct eigrp_interface
*ei
)
378 if ((EIGRP_IF_PASSIVE_STATUS(ei
) == EIGRP_IF_ACTIVE
)) {
379 /* The interface should belong to the EIGRP-all-routers group.
381 if (!EI_MEMBER_CHECK(ei
, MEMBER_ALLROUTERS
)
382 && (eigrp_if_add_allspfrouters(ei
->eigrp
, ei
->address
,
385 /* Set the flag only if the system call to join
387 EI_MEMBER_JOINED(ei
, MEMBER_ALLROUTERS
);
389 /* The interface should NOT belong to the EIGRP-all-routers
391 if (EI_MEMBER_CHECK(ei
, MEMBER_ALLROUTERS
)) {
392 /* Only actually drop if this is the last reference */
393 if (EI_MEMBER_COUNT(ei
, MEMBER_ALLROUTERS
) == 1)
394 eigrp_if_drop_allspfrouters(ei
->eigrp
,
397 /* Unset the flag regardless of whether the system call
399 the group succeeded, since it's much safer to assume
401 we are not a member. */
402 EI_MEMBER_LEFT(ei
, MEMBER_ALLROUTERS
);
407 u_char
eigrp_default_iftype(struct interface
*ifp
)
409 if (if_is_pointopoint(ifp
))
410 return EIGRP_IFTYPE_POINTOPOINT
;
411 else if (if_is_loopback(ifp
))
412 return EIGRP_IFTYPE_LOOPBACK
;
414 return EIGRP_IFTYPE_BROADCAST
;
417 void eigrp_if_free(struct eigrp_interface
*ei
, int source
)
419 struct prefix_ipv4 dest_addr
;
420 struct eigrp_prefix_entry
*pe
;
421 struct eigrp
*eigrp
= eigrp_lookup();
423 if (source
== INTERFACE_DOWN_BY_VTY
) {
424 THREAD_OFF(ei
->t_hello
);
425 eigrp_hello_send(ei
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
, NULL
);
428 dest_addr
.family
= AF_INET
;
429 dest_addr
.prefix
= ei
->connected
->address
->u
.prefix4
;
430 dest_addr
.prefixlen
= ei
->connected
->address
->prefixlen
;
431 apply_mask_ipv4(&dest_addr
);
432 pe
= eigrp_topology_table_lookup_ipv4(eigrp
->topology_table
,
435 eigrp_prefix_entry_delete(eigrp
->topology_table
, pe
);
439 list_delete(ei
->nbrs
);
440 eigrp_delete_from_if(ei
->ifp
, ei
);
441 listnode_delete(ei
->eigrp
->eiflist
, ei
);
443 thread_cancel_event(master
, ei
);
445 memset(ei
, 0, sizeof(*ei
));
446 XFREE(MTYPE_EIGRP_IF
, ei
);
449 static void eigrp_delete_from_if(struct interface
*ifp
,
450 struct eigrp_interface
*ei
)
452 struct route_node
*rn
;
456 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
458 rn
= route_node_lookup(IF_OIFS(ei
->ifp
), &p
);
462 route_unlock_node(rn
);
463 route_unlock_node(rn
);
466 /* Simulate down/up on the interface. This is needed, for example, when
468 void eigrp_if_reset(struct interface
*ifp
)
470 struct route_node
*rn
;
472 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
473 struct eigrp_interface
*ei
;
475 if ((ei
= rn
->info
) == NULL
)
483 struct eigrp_interface
*eigrp_if_lookup_by_local_addr(struct eigrp
*eigrp
,
484 struct interface
*ifp
,
485 struct in_addr address
)
487 struct listnode
*node
;
488 struct eigrp_interface
*ei
;
490 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
491 if (ifp
&& ei
->ifp
!= ifp
)
494 if (IPV4_ADDR_SAME(&address
, &ei
->address
->u
.prefix4
))
502 * @fn eigrp_if_lookup_by_name
504 * @param[in] eigrp EIGRP process
505 * @param[in] if_name Name of the interface
507 * @return struct eigrp_interface *
510 * Function is used for lookup interface by name.
512 struct eigrp_interface
*eigrp_if_lookup_by_name(struct eigrp
*eigrp
,
515 struct eigrp_interface
*ei
;
516 struct listnode
*node
;
518 /* iterate over all eigrp interfaces */
519 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
520 /* compare int name with eigrp interface's name */
521 if (strcmp(ei
->ifp
->name
, if_name
) == 0) {
529 /* determine receiving interface by ifp and source address */
530 struct eigrp_interface
*eigrp_if_lookup_recv_if(struct eigrp
*eigrp
,
532 struct interface
*ifp
)
534 struct route_node
*rn
;
535 struct prefix_ipv4 addr
;
536 struct eigrp_interface
*ei
, *match
;
538 addr
.family
= AF_INET
;
540 addr
.prefixlen
= IPV4_MAX_BITLEN
;
544 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
547 if (!ei
) /* oi can be NULL for PtP aliases */
550 if (if_is_loopback(ei
->ifp
))
553 if (prefix_match(CONNECTED_PREFIX(ei
->connected
),
554 (struct prefix
*)&addr
)) {
555 if ((match
== NULL
) || (match
->address
->prefixlen
556 < ei
->address
->prefixlen
))
564 u_int32_t
eigrp_bandwidth_to_scaled(u_int32_t bandwidth
)
566 uint64_t temp_bandwidth
= (256ull * 10000000) / bandwidth
;
568 temp_bandwidth
= temp_bandwidth
< EIGRP_MAX_METRIC
? temp_bandwidth
571 return (u_int32_t
)temp_bandwidth
;
574 u_int32_t
eigrp_scaled_to_bandwidth(u_int32_t scaled
)
576 uint64_t temp_scaled
= scaled
* (256ull * 10000000);
579 temp_scaled
< EIGRP_MAX_METRIC
? temp_scaled
: EIGRP_MAX_METRIC
;
581 return (u_int32_t
)temp_scaled
;
584 u_int32_t
eigrp_delay_to_scaled(u_int32_t delay
)
589 u_int32_t
eigrp_scaled_to_delay(u_int32_t scaled
)