]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrpd.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / eigrpd / eigrpd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * EIGRP Daemon Program.
4 * Copyright (C) 2013-2014
5 * Authors:
6 * Donnie Savage
7 * Jan Janovic
8 * Matej Perina
9 * Peter Orsag
10 * Peter Paluch
11 */
12
13 #include <zebra.h>
14
15 #include "frrevent.h"
16 #include "vty.h"
17 #include "command.h"
18 #include "linklist.h"
19 #include "prefix.h"
20 #include "table.h"
21 #include "if.h"
22 #include "memory.h"
23 #include "stream.h"
24 #include "log.h"
25 #include "sockunion.h" /* for inet_aton () */
26 #include "zclient.h"
27 #include "plist.h"
28 #include "sockopt.h"
29 #include "keychain.h"
30 #include "libfrr.h"
31 #include "lib_errors.h"
32 #include "distribute.h"
33
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"
44
45 DEFINE_MGROUP(EIGRPD, "eigrpd");
46
47 DEFINE_MTYPE_STATIC(EIGRPD, EIGRP_TOP, "EIGRP structure");
48
49 DEFINE_QOBJ_TYPE(eigrp);
50
51 static struct eigrp_master eigrp_master;
52
53 struct eigrp_master *eigrp_om;
54
55 extern struct zclient *zclient;
56 extern struct in_addr router_id_zebra;
57
58
59 /*
60 * void eigrp_router_id_update(struct eigrp *eigrp)
61 *
62 * Description:
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
66 *
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
71 * disruptive.
72 * 3. Last choice: just go with whatever the zebra daemon recommends.
73 *
74 * Note:
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
78 *
79 * This does not work for IPv6, and to make the code simpler, its
80 * stored and processed internerall as a 32bit number
81 */
82 void eigrp_router_id_update(struct eigrp *eigrp)
83 {
84 struct vrf *vrf = vrf_lookup_by_id(eigrp->vrf_id);
85 struct interface *ifp;
86 struct in_addr router_id, router_id_old;
87
88 router_id_old = eigrp->router_id;
89
90 if (eigrp->router_id_static.s_addr != INADDR_ANY)
91 router_id = eigrp->router_id_static;
92
93 else if (eigrp->router_id.s_addr != INADDR_ANY)
94 router_id = eigrp->router_id;
95
96 else
97 router_id = router_id_zebra;
98
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);
104 }
105 }
106
107 void eigrp_master_init(void)
108 {
109 struct timeval tv;
110
111 memset(&eigrp_master, 0, sizeof(eigrp_master));
112
113 eigrp_om = &eigrp_master;
114 eigrp_om->eigrp = list_new();
115
116 monotime(&tv);
117 eigrp_om->start_time = tv.tv_sec;
118 }
119
120 /* Allocate new eigrp structure. */
121 static struct eigrp *eigrp_new(uint16_t as, vrf_id_t vrf_id)
122 {
123 struct eigrp *eigrp = XCALLOC(MTYPE_EIGRP_TOP, sizeof(struct eigrp));
124
125 /* init information relevant to peers */
126 eigrp->vrf_id = vrf_id;
127 eigrp->vrid = 0;
128 eigrp->AS = as;
129 eigrp->router_id.s_addr = INADDR_ANY;
130 eigrp->router_id_static.s_addr = INADDR_ANY;
131 eigrp->sequence_number = 1;
132
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;
140
141 /* init internal data structures */
142 eigrp->eiflist = list_new();
143 eigrp->passive_interface_default = EIGRP_IF_ACTIVE;
144 eigrp->networks = eigrp_topology_new();
145
146 eigrp->fd = eigrp_sock_init(vrf_lookup_by_id(vrf_id));
147
148 if (eigrp->fd < 0) {
149 flog_err_sys(
150 EC_LIB_SOCKET,
151 "%s: fatal error: eigrp_sock_init was unable to open a socket",
152 __func__);
153 exit(1);
154 }
155
156 eigrp->maxsndbuflen = getsockopt_so_sendbuf(eigrp->fd);
157
158 eigrp->ibuf = stream_new(EIGRP_PACKET_MAX_LEN + 1);
159
160 event_add_read(master, eigrp_read, eigrp, eigrp->fd, &eigrp->t_read);
161 eigrp->oi_write_q = list_new();
162
163 eigrp->topology_table = route_table_init();
164
165 eigrp->neighbor_self = eigrp_nbr_new(NULL);
166 eigrp->neighbor_self->src.s_addr = INADDR_ANY;
167
168 eigrp->variance = EIGRP_VARIANCE_DEFAULT;
169 eigrp->max_paths = EIGRP_MAX_PATHS_DEFAULT;
170
171 eigrp->serno = 0;
172 eigrp->serno_last_update = 0;
173 eigrp->topology_changes_externalIPV4 = list_new();
174 eigrp->topology_changes_internalIPV4 = list_new();
175
176 eigrp->list[EIGRP_FILTER_IN] = NULL;
177 eigrp->list[EIGRP_FILTER_OUT] = NULL;
178
179 eigrp->prefix[EIGRP_FILTER_IN] = NULL;
180 eigrp->prefix[EIGRP_FILTER_OUT] = NULL;
181
182 eigrp->routemap[EIGRP_FILTER_IN] = NULL;
183 eigrp->routemap[EIGRP_FILTER_OUT] = NULL;
184
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);
192
193 /*
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);
197 */
198 QOBJ_REG(eigrp, eigrp);
199 return eigrp;
200 }
201
202 struct eigrp *eigrp_get(uint16_t as, vrf_id_t vrf_id)
203 {
204 struct eigrp *eigrp;
205
206 eigrp = eigrp_lookup(vrf_id);
207 if (eigrp == NULL) {
208 eigrp = eigrp_new(as, vrf_id);
209 listnode_add(eigrp_om->eigrp, eigrp);
210 }
211
212 return eigrp;
213 }
214
215 /* Shut down the entire process */
216 void eigrp_terminate(void)
217 {
218 struct eigrp *eigrp;
219 struct listnode *node, *nnode;
220
221 /* shutdown already in progress */
222 if (CHECK_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN))
223 return;
224
225 SET_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN);
226
227 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp))
228 eigrp_finish(eigrp);
229
230 frr_fini();
231 }
232
233 void eigrp_finish(struct eigrp *eigrp)
234 {
235 eigrp_finish_final(eigrp);
236
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)) {
240 if (zclient) {
241 zclient_stop(zclient);
242 zclient_free(zclient);
243 }
244 exit(0);
245 }
246
247 return;
248 }
249
250 /* Final cleanup of eigrp instance */
251 void eigrp_finish_final(struct eigrp *eigrp)
252 {
253 struct eigrp_interface *ei;
254 struct eigrp_neighbor *nbr;
255 struct listnode *node, *nnode, *node2, *nnode2;
256
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);
261 }
262
263 EVENT_OFF(eigrp->t_write);
264 EVENT_OFF(eigrp->t_read);
265 close(eigrp->fd);
266
267 list_delete(&eigrp->eiflist);
268 list_delete(&eigrp->oi_write_q);
269
270 eigrp_topology_free(eigrp, eigrp->topology_table);
271
272 eigrp_nbr_delete(eigrp->neighbor_self);
273
274 list_delete(&eigrp->topology_changes_externalIPV4);
275 list_delete(&eigrp->topology_changes_internalIPV4);
276
277 listnode_delete(eigrp_om->eigrp, eigrp);
278
279 stream_free(eigrp->ibuf);
280 distribute_list_delete(&eigrp->distribute_ctx);
281 XFREE(MTYPE_EIGRP_TOP, eigrp);
282 }
283
284 /*Look for existing eigrp process*/
285 struct eigrp *eigrp_lookup(vrf_id_t vrf_id)
286 {
287 struct eigrp *eigrp;
288 struct listnode *node, *nnode;
289
290 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp))
291 if (eigrp->vrf_id == vrf_id)
292 return eigrp;
293
294 return NULL;
295 }