]> git.proxmox.com Git - mirror_frr.git/blame - eigrpd/eigrpd.c
*: reindent
[mirror_frr.git] / eigrpd / eigrpd.c
CommitLineData
7f57883e
DS
1/*
2 * EIGRP Daemon Program.
3 * Copyright (C) 2013-2014
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 *
11 * This file is part of GNU Zebra.
12 *
13 * GNU Zebra is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * GNU Zebra is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
896014f4
DL
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
7f57883e
DS
26 */
27
28#include <zebra.h>
29
30#include "thread.h"
31#include "vty.h"
32#include "command.h"
33#include "linklist.h"
34#include "prefix.h"
35#include "table.h"
36#include "if.h"
37#include "memory.h"
38#include "stream.h"
39#include "log.h"
d62a17ae 40#include "sockunion.h" /* for inet_aton () */
7f57883e
DS
41#include "zclient.h"
42#include "plist.h"
43#include "sockopt.h"
44#include "keychain.h"
45
46#include "eigrpd/eigrp_structs.h"
47#include "eigrpd/eigrpd.h"
48#include "eigrpd/eigrp_interface.h"
49#include "eigrpd/eigrp_zebra.h"
50#include "eigrpd/eigrp_vty.h"
51#include "eigrpd/eigrp_neighbor.h"
52#include "eigrpd/eigrp_packet.h"
53#include "eigrpd/eigrp_network.h"
54#include "eigrpd/eigrp_topology.h"
55#include "eigrpd/eigrp_memory.h"
56
57DEFINE_QOBJ_TYPE(eigrp)
58
59static struct eigrp_master eigrp_master;
60
61struct eigrp_master *eigrp_om;
62
7f57883e
DS
63static void eigrp_delete(struct eigrp *);
64static struct eigrp *eigrp_new(const char *);
65static void eigrp_add(struct eigrp *);
66
67extern struct zclient *zclient;
68extern struct in_addr router_id_zebra;
69
70
71/*
72 * void eigrp_router_id_update(struct eigrp *eigrp)
73 *
74 * Description:
75 * update routerid associated with this instance of EIGRP.
76 * If the id changes, then call if_update for each interface
77 * to resync the topology database with all neighbors
78 *
79 * Select the router ID based on these priorities:
80 * 1. Statically assigned router ID is always the first choice.
81 * 2. If there is no statically assigned router ID, then try to stick
82 * with the most recent value, since changing router ID's is very
83 * disruptive.
84 * 3. Last choice: just go with whatever the zebra daemon recommends.
85 *
86 * Note:
87 * router id for EIGRP is really just a 32 bit number. Cisco historically
88 * displays it in dotted decimal notation, and will pickup an IP address
89 * from an interface so it can be 'auto-configed" to a uniqe value
90 *
91 * This does not work for IPv6, and to make the code simpler, its
92 * stored and processed internerall as a 32bit number
93 */
d62a17ae 94void eigrp_router_id_update(struct eigrp *eigrp)
7f57883e 95{
d62a17ae 96 struct interface *ifp;
97 struct listnode *node;
98 u_int32_t router_id, router_id_old;
7f57883e 99
d62a17ae 100 router_id_old = eigrp->router_id;
7f57883e 101
d62a17ae 102 if (eigrp->router_id_static != 0)
103 router_id = eigrp->router_id_static;
7f57883e 104
d62a17ae 105 else if (eigrp->router_id != 0)
106 router_id = eigrp->router_id;
7f57883e 107
d62a17ae 108 else
109 router_id = router_id_zebra.s_addr;
7f57883e 110
d62a17ae 111 eigrp->router_id = router_id;
112 if (router_id_old != router_id) {
113 // if (IS_DEBUG_EIGRP_EVENT)
114 // zlog_debug("Router-ID[NEW:%s]: Update",
115 // inet_ntoa(eigrp->router_id));
7f57883e 116
d62a17ae 117 /* update eigrp_interface's */
118 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp))
119 eigrp_if_update(ifp);
120 }
7f57883e
DS
121}
122
d62a17ae 123void eigrp_master_init()
7f57883e 124{
d62a17ae 125 struct timeval tv;
7f57883e 126
d62a17ae 127 memset(&eigrp_master, 0, sizeof(struct eigrp_master));
7f57883e 128
d62a17ae 129 eigrp_om = &eigrp_master;
130 eigrp_om->eigrp = list_new();
7f57883e 131
d62a17ae 132 monotime(&tv);
133 eigrp_om->start_time = tv.tv_sec;
7f57883e
DS
134}
135
7f57883e 136/* Allocate new eigrp structure. */
d62a17ae 137static struct eigrp *eigrp_new(const char *AS)
7f57883e 138{
d62a17ae 139 struct eigrp *eigrp = XCALLOC(MTYPE_EIGRP_TOP, sizeof(struct eigrp));
140 int eigrp_socket;
141
142 /* init information relevant to peers */
143 eigrp->vrid = 0;
144 eigrp->AS = atoi(AS);
145 eigrp->router_id = 0L;
146 eigrp->router_id_static = 0L;
147 eigrp->sequence_number = 1;
148
149 /*Configure default K Values for EIGRP Process*/
150 eigrp->k_values[0] = EIGRP_K1_DEFAULT;
151 eigrp->k_values[1] = EIGRP_K2_DEFAULT;
152 eigrp->k_values[2] = EIGRP_K3_DEFAULT;
153 eigrp->k_values[3] = EIGRP_K4_DEFAULT;
154 eigrp->k_values[4] = EIGRP_K5_DEFAULT;
155 eigrp->k_values[5] = EIGRP_K6_DEFAULT;
156
157 /* init internal data structures */
158 eigrp->eiflist = list_new();
159 eigrp->passive_interface_default = EIGRP_IF_ACTIVE;
160 eigrp->networks = route_table_init();
161
162 if ((eigrp_socket = eigrp_sock_init()) < 0) {
163 zlog_err(
164 "eigrp_new: fatal error: eigrp_sock_init was unable to open "
165 "a socket");
166 exit(1);
167 }
168
169 eigrp->fd = eigrp_socket;
170 eigrp->maxsndbuflen = getsockopt_so_sendbuf(eigrp->fd);
171
172 if ((eigrp->ibuf = stream_new(EIGRP_PACKET_MAX_LEN + 1)) == NULL) {
173 zlog_err(
174 "eigrp_new: fatal error: stream_new (%u) failed allocating ibuf",
175 EIGRP_PACKET_MAX_LEN + 1);
176 exit(1);
177 }
178
179 eigrp->t_read = NULL;
180 thread_add_read(master, eigrp_read, eigrp, eigrp->fd, &eigrp->t_read);
181 eigrp->oi_write_q = list_new();
182
183 eigrp->topology_table = eigrp_topology_new();
184
185 eigrp->neighbor_self = eigrp_nbr_new(NULL);
186 eigrp->neighbor_self->src.s_addr = INADDR_ANY;
187
188 eigrp->variance = EIGRP_VARIANCE_DEFAULT;
189 eigrp->max_paths = EIGRP_MAX_PATHS_DEFAULT;
190
191 eigrp->serno = 0;
192 eigrp->serno_last_update = 0;
193 eigrp->topology_changes_externalIPV4 = list_new();
194 eigrp->topology_changes_internalIPV4 = list_new();
195
196 eigrp->list[EIGRP_FILTER_IN] = NULL;
197 eigrp->list[EIGRP_FILTER_OUT] = NULL;
198
199 eigrp->prefix[EIGRP_FILTER_IN] = NULL;
200 eigrp->prefix[EIGRP_FILTER_OUT] = NULL;
201
202 eigrp->routemap[EIGRP_FILTER_IN] = NULL;
203 eigrp->routemap[EIGRP_FILTER_OUT] = NULL;
204
205 QOBJ_REG(eigrp, eigrp);
206 return eigrp;
7f57883e
DS
207}
208
d62a17ae 209static void eigrp_add(struct eigrp *eigrp)
7f57883e 210{
d62a17ae 211 listnode_add(eigrp_om->eigrp, eigrp);
7f57883e
DS
212}
213
d62a17ae 214static void eigrp_delete(struct eigrp *eigrp)
7f57883e 215{
d62a17ae 216 listnode_delete(eigrp_om->eigrp, eigrp);
7f57883e
DS
217}
218
d62a17ae 219struct eigrp *eigrp_get(const char *AS)
7f57883e 220{
d62a17ae 221 struct eigrp *eigrp;
7f57883e 222
d62a17ae 223 eigrp = eigrp_lookup();
224 if (eigrp == NULL) {
225 eigrp = eigrp_new(AS);
226 eigrp_add(eigrp);
227 }
7f57883e 228
d62a17ae 229 return eigrp;
7f57883e
DS
230}
231
232/* Shut down the entire process */
d62a17ae 233void eigrp_terminate(void)
7f57883e 234{
d62a17ae 235 struct eigrp *eigrp;
236 struct listnode *node, *nnode;
7f57883e 237
d62a17ae 238 /* shutdown already in progress */
239 if (CHECK_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN))
240 return;
7f57883e 241
d62a17ae 242 SET_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN);
7f57883e 243
d62a17ae 244 /* exit immediately if EIGRP not actually running */
245 if (listcount(eigrp_om->eigrp) == 0)
246 exit(0);
7f57883e 247
d62a17ae 248 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp))
249 eigrp_finish(eigrp);
7f57883e
DS
250}
251
d62a17ae 252void eigrp_finish(struct eigrp *eigrp)
7f57883e 253{
d62a17ae 254 eigrp_finish_final(eigrp);
255
256 /* eigrp being shut-down? If so, was this the last eigrp instance? */
257 if (CHECK_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN)
258 && (listcount(eigrp_om->eigrp) == 0)) {
259 if (zclient) {
260 zclient_stop(zclient);
261 zclient_free(zclient);
262 }
263 exit(0);
edaf6c01 264 }
7f57883e 265
d62a17ae 266 return;
7f57883e
DS
267}
268
269/* Final cleanup of eigrp instance */
d62a17ae 270void eigrp_finish_final(struct eigrp *eigrp)
7f57883e 271{
d62a17ae 272 struct eigrp_interface *ei;
273 struct eigrp_neighbor *nbr;
274 struct listnode *node, *nnode, *node2, *nnode2;
275
276 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
277 for (ALL_LIST_ELEMENTS(ei->nbrs, node2, nnode2, nbr))
278 eigrp_nbr_delete(nbr);
279 eigrp_if_free(ei, INTERFACE_DOWN_BY_FINAL);
280 }
7f57883e 281
d62a17ae 282 THREAD_OFF(eigrp->t_write);
283 THREAD_OFF(eigrp->t_read);
284 close(eigrp->fd);
7f57883e 285
d62a17ae 286 list_delete(eigrp->eiflist);
287 list_delete(eigrp->oi_write_q);
288 list_delete(eigrp->topology_changes_externalIPV4);
289 list_delete(eigrp->topology_changes_internalIPV4);
7f57883e 290
d62a17ae 291 eigrp_topology_cleanup(eigrp->topology_table);
292 eigrp_topology_free(eigrp->topology_table);
7f57883e 293
d62a17ae 294 eigrp_nbr_delete(eigrp->neighbor_self);
7f57883e 295
d62a17ae 296 eigrp_delete(eigrp);
7f57883e 297
d62a17ae 298 XFREE(MTYPE_EIGRP_TOP, eigrp);
7f57883e
DS
299}
300
301/*Look for existing eigrp process*/
d62a17ae 302struct eigrp *eigrp_lookup(void)
7f57883e 303{
d62a17ae 304 if (listcount(eigrp_om->eigrp) == 0)
305 return NULL;
7f57883e 306
d62a17ae 307 return listgetdata(listhead(eigrp_om->eigrp));
7f57883e 308}