1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * EIGRP Daemon Program.
4 * Copyright (C) 2013-2014
25 #include "sockunion.h" /* for inet_aton () */
31 #include "lib_errors.h"
32 #include "distribute.h"
34 #include "eigrpd/eigrp_structs.h"
35 #include "eigrpd/eigrpd.h"
36 #include "eigrpd/eigrp_interface.h"
37 #include "eigrpd/eigrp_zebra.h"
38 #include "eigrpd/eigrp_vty.h"
39 #include "eigrpd/eigrp_neighbor.h"
40 #include "eigrpd/eigrp_packet.h"
41 #include "eigrpd/eigrp_network.h"
42 #include "eigrpd/eigrp_topology.h"
43 #include "eigrpd/eigrp_filter.h"
45 DEFINE_MGROUP(EIGRPD
, "eigrpd");
47 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_TOP
, "EIGRP structure");
49 DEFINE_QOBJ_TYPE(eigrp
);
51 static struct eigrp_master eigrp_master
;
53 struct eigrp_master
*eigrp_om
;
55 extern struct zclient
*zclient
;
56 extern struct in_addr router_id_zebra
;
60 * void eigrp_router_id_update(struct eigrp *eigrp)
63 * update routerid associated with this instance of EIGRP.
64 * If the id changes, then call if_update for each interface
65 * to resync the topology database with all neighbors
67 * Select the router ID based on these priorities:
68 * 1. Statically assigned router ID is always the first choice.
69 * 2. If there is no statically assigned router ID, then try to stick
70 * with the most recent value, since changing router ID's is very
72 * 3. Last choice: just go with whatever the zebra daemon recommends.
75 * router id for EIGRP is really just a 32 bit number. Cisco historically
76 * displays it in dotted decimal notation, and will pickup an IP address
77 * from an interface so it can be 'auto-configed" to a uniqe value
79 * This does not work for IPv6, and to make the code simpler, its
80 * stored and processed internerall as a 32bit number
82 void eigrp_router_id_update(struct eigrp
*eigrp
)
84 struct vrf
*vrf
= vrf_lookup_by_id(eigrp
->vrf_id
);
85 struct interface
*ifp
;
86 struct in_addr router_id
, router_id_old
;
88 router_id_old
= eigrp
->router_id
;
90 if (eigrp
->router_id_static
.s_addr
!= INADDR_ANY
)
91 router_id
= eigrp
->router_id_static
;
93 else if (eigrp
->router_id
.s_addr
!= INADDR_ANY
)
94 router_id
= eigrp
->router_id
;
97 router_id
= router_id_zebra
;
99 eigrp
->router_id
= router_id
;
100 if (router_id_old
.s_addr
!= router_id
.s_addr
) {
101 /* update eigrp_interface's */
102 FOR_ALL_INTERFACES (vrf
, ifp
)
103 eigrp_if_update(ifp
);
107 void eigrp_master_init(void)
111 memset(&eigrp_master
, 0, sizeof(eigrp_master
));
113 eigrp_om
= &eigrp_master
;
114 eigrp_om
->eigrp
= list_new();
117 eigrp_om
->start_time
= tv
.tv_sec
;
120 /* Allocate new eigrp structure. */
121 static struct eigrp
*eigrp_new(uint16_t as
, vrf_id_t vrf_id
)
123 struct eigrp
*eigrp
= XCALLOC(MTYPE_EIGRP_TOP
, sizeof(struct eigrp
));
125 /* init information relevant to peers */
126 eigrp
->vrf_id
= vrf_id
;
129 eigrp
->router_id
.s_addr
= INADDR_ANY
;
130 eigrp
->router_id_static
.s_addr
= INADDR_ANY
;
131 eigrp
->sequence_number
= 1;
133 /*Configure default K Values for EIGRP Process*/
134 eigrp
->k_values
[0] = EIGRP_K1_DEFAULT
;
135 eigrp
->k_values
[1] = EIGRP_K2_DEFAULT
;
136 eigrp
->k_values
[2] = EIGRP_K3_DEFAULT
;
137 eigrp
->k_values
[3] = EIGRP_K4_DEFAULT
;
138 eigrp
->k_values
[4] = EIGRP_K5_DEFAULT
;
139 eigrp
->k_values
[5] = EIGRP_K6_DEFAULT
;
141 /* init internal data structures */
142 eigrp
->eiflist
= list_new();
143 eigrp
->passive_interface_default
= EIGRP_IF_ACTIVE
;
144 eigrp
->networks
= eigrp_topology_new();
146 eigrp
->fd
= eigrp_sock_init(vrf_lookup_by_id(vrf_id
));
151 "%s: fatal error: eigrp_sock_init was unable to open a socket",
156 eigrp
->maxsndbuflen
= getsockopt_so_sendbuf(eigrp
->fd
);
158 eigrp
->ibuf
= stream_new(EIGRP_PACKET_MAX_LEN
+ 1);
160 event_add_read(master
, eigrp_read
, eigrp
, eigrp
->fd
, &eigrp
->t_read
);
161 eigrp
->oi_write_q
= list_new();
163 eigrp
->topology_table
= route_table_init();
165 eigrp
->neighbor_self
= eigrp_nbr_new(NULL
);
166 eigrp
->neighbor_self
->src
.s_addr
= INADDR_ANY
;
168 eigrp
->variance
= EIGRP_VARIANCE_DEFAULT
;
169 eigrp
->max_paths
= EIGRP_MAX_PATHS_DEFAULT
;
172 eigrp
->serno_last_update
= 0;
173 eigrp
->topology_changes_externalIPV4
= list_new();
174 eigrp
->topology_changes_internalIPV4
= list_new();
176 eigrp
->list
[EIGRP_FILTER_IN
] = NULL
;
177 eigrp
->list
[EIGRP_FILTER_OUT
] = NULL
;
179 eigrp
->prefix
[EIGRP_FILTER_IN
] = NULL
;
180 eigrp
->prefix
[EIGRP_FILTER_OUT
] = NULL
;
182 eigrp
->routemap
[EIGRP_FILTER_IN
] = NULL
;
183 eigrp
->routemap
[EIGRP_FILTER_OUT
] = NULL
;
185 /* Distribute list install. */
186 eigrp
->distribute_ctx
=
187 distribute_list_ctx_create(vrf_lookup_by_id(eigrp
->vrf_id
));
188 distribute_list_add_hook(eigrp
->distribute_ctx
,
189 eigrp_distribute_update
);
190 distribute_list_delete_hook(eigrp
->distribute_ctx
,
191 eigrp_distribute_update
);
194 eigrp->if_rmap_ctx = if_rmap_ctx_create(eigrp->vrf_id);
195 if_rmap_hook_add (eigrp_if_rmap_update);
196 if_rmap_hook_delete (eigrp_if_rmap_update);
198 QOBJ_REG(eigrp
, eigrp
);
202 struct eigrp
*eigrp_get(uint16_t as
, vrf_id_t vrf_id
)
206 eigrp
= eigrp_lookup(vrf_id
);
208 eigrp
= eigrp_new(as
, vrf_id
);
209 listnode_add(eigrp_om
->eigrp
, eigrp
);
215 /* Shut down the entire process */
216 void eigrp_terminate(void)
219 struct listnode
*node
, *nnode
;
221 /* shutdown already in progress */
222 if (CHECK_FLAG(eigrp_om
->options
, EIGRP_MASTER_SHUTDOWN
))
225 SET_FLAG(eigrp_om
->options
, EIGRP_MASTER_SHUTDOWN
);
227 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
))
233 void eigrp_finish(struct eigrp
*eigrp
)
235 eigrp_finish_final(eigrp
);
237 /* eigrp being shut-down? If so, was this the last eigrp instance? */
238 if (CHECK_FLAG(eigrp_om
->options
, EIGRP_MASTER_SHUTDOWN
)
239 && (listcount(eigrp_om
->eigrp
) == 0)) {
241 zclient_stop(zclient
);
242 zclient_free(zclient
);
250 /* Final cleanup of eigrp instance */
251 void eigrp_finish_final(struct eigrp
*eigrp
)
253 struct eigrp_interface
*ei
;
254 struct eigrp_neighbor
*nbr
;
255 struct listnode
*node
, *nnode
, *node2
, *nnode2
;
257 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei
)) {
258 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node2
, nnode2
, nbr
))
259 eigrp_nbr_delete(nbr
);
260 eigrp_if_free(ei
, INTERFACE_DOWN_BY_FINAL
);
263 EVENT_OFF(eigrp
->t_write
);
264 EVENT_OFF(eigrp
->t_read
);
267 list_delete(&eigrp
->eiflist
);
268 list_delete(&eigrp
->oi_write_q
);
270 eigrp_topology_free(eigrp
, eigrp
->topology_table
);
272 eigrp_nbr_delete(eigrp
->neighbor_self
);
274 list_delete(&eigrp
->topology_changes_externalIPV4
);
275 list_delete(&eigrp
->topology_changes_internalIPV4
);
277 listnode_delete(eigrp_om
->eigrp
, eigrp
);
279 stream_free(eigrp
->ibuf
);
280 distribute_list_delete(&eigrp
->distribute_ctx
);
281 XFREE(MTYPE_EIGRP_TOP
, eigrp
);
284 /*Look for existing eigrp process*/
285 struct eigrp
*eigrp_lookup(vrf_id_t vrf_id
)
288 struct listnode
*node
, *nnode
;
290 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
))
291 if (eigrp
->vrf_id
== vrf_id
)