]> git.proxmox.com Git - mirror_frr.git/blame - eigrpd/eigrpd.c
Merge pull request #2426 from pacovn/Coverity_23089_Buffer_not_null_terminated
[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"
8879bd22 45#include "libfrr.h"
7f57883e
DS
46
47#include "eigrpd/eigrp_structs.h"
48#include "eigrpd/eigrpd.h"
49#include "eigrpd/eigrp_interface.h"
50#include "eigrpd/eigrp_zebra.h"
51#include "eigrpd/eigrp_vty.h"
52#include "eigrpd/eigrp_neighbor.h"
53#include "eigrpd/eigrp_packet.h"
54#include "eigrpd/eigrp_network.h"
55#include "eigrpd/eigrp_topology.h"
56#include "eigrpd/eigrp_memory.h"
57
58DEFINE_QOBJ_TYPE(eigrp)
59
60static struct eigrp_master eigrp_master;
61
62struct eigrp_master *eigrp_om;
63
7f57883e
DS
64static void eigrp_delete(struct eigrp *);
65static struct eigrp *eigrp_new(const char *);
66static void eigrp_add(struct eigrp *);
67
68extern struct zclient *zclient;
69extern struct in_addr router_id_zebra;
70
71
72/*
73 * void eigrp_router_id_update(struct eigrp *eigrp)
74 *
75 * Description:
76 * update routerid associated with this instance of EIGRP.
77 * If the id changes, then call if_update for each interface
78 * to resync the topology database with all neighbors
79 *
80 * Select the router ID based on these priorities:
81 * 1. Statically assigned router ID is always the first choice.
82 * 2. If there is no statically assigned router ID, then try to stick
83 * with the most recent value, since changing router ID's is very
84 * disruptive.
85 * 3. Last choice: just go with whatever the zebra daemon recommends.
86 *
87 * Note:
88 * router id for EIGRP is really just a 32 bit number. Cisco historically
89 * displays it in dotted decimal notation, and will pickup an IP address
90 * from an interface so it can be 'auto-configed" to a uniqe value
91 *
92 * This does not work for IPv6, and to make the code simpler, its
93 * stored and processed internerall as a 32bit number
94 */
d62a17ae 95void eigrp_router_id_update(struct eigrp *eigrp)
7f57883e 96{
f4e14fdb 97 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
d62a17ae 98 struct interface *ifp;
d7c0a89a 99 uint32_t router_id, router_id_old;
7f57883e 100
d62a17ae 101 router_id_old = eigrp->router_id;
7f57883e 102
d62a17ae 103 if (eigrp->router_id_static != 0)
104 router_id = eigrp->router_id_static;
7f57883e 105
d62a17ae 106 else if (eigrp->router_id != 0)
107 router_id = eigrp->router_id;
7f57883e 108
d62a17ae 109 else
110 router_id = router_id_zebra.s_addr;
7f57883e 111
d62a17ae 112 eigrp->router_id = router_id;
113 if (router_id_old != router_id) {
114 // if (IS_DEBUG_EIGRP_EVENT)
115 // zlog_debug("Router-ID[NEW:%s]: Update",
116 // inet_ntoa(eigrp->router_id));
7f57883e 117
d62a17ae 118 /* update eigrp_interface's */
451fda4f 119 FOR_ALL_INTERFACES (vrf, ifp)
d62a17ae 120 eigrp_if_update(ifp);
121 }
7f57883e
DS
122}
123
d62a17ae 124void eigrp_master_init()
7f57883e 125{
d62a17ae 126 struct timeval tv;
7f57883e 127
d62a17ae 128 memset(&eigrp_master, 0, sizeof(struct eigrp_master));
7f57883e 129
d62a17ae 130 eigrp_om = &eigrp_master;
131 eigrp_om->eigrp = list_new();
7f57883e 132
d62a17ae 133 monotime(&tv);
134 eigrp_om->start_time = tv.tv_sec;
7f57883e
DS
135}
136
7f57883e 137/* Allocate new eigrp structure. */
d62a17ae 138static struct eigrp *eigrp_new(const char *AS)
7f57883e 139{
d62a17ae 140 struct eigrp *eigrp = XCALLOC(MTYPE_EIGRP_TOP, sizeof(struct eigrp));
141 int eigrp_socket;
142
143 /* init information relevant to peers */
144 eigrp->vrid = 0;
145 eigrp->AS = atoi(AS);
146 eigrp->router_id = 0L;
147 eigrp->router_id_static = 0L;
148 eigrp->sequence_number = 1;
149
150 /*Configure default K Values for EIGRP Process*/
151 eigrp->k_values[0] = EIGRP_K1_DEFAULT;
152 eigrp->k_values[1] = EIGRP_K2_DEFAULT;
153 eigrp->k_values[2] = EIGRP_K3_DEFAULT;
154 eigrp->k_values[3] = EIGRP_K4_DEFAULT;
155 eigrp->k_values[4] = EIGRP_K5_DEFAULT;
156 eigrp->k_values[5] = EIGRP_K6_DEFAULT;
157
158 /* init internal data structures */
159 eigrp->eiflist = list_new();
160 eigrp->passive_interface_default = EIGRP_IF_ACTIVE;
9ca66cc7 161 eigrp->networks = eigrp_topology_new();
d62a17ae 162
163 if ((eigrp_socket = eigrp_sock_init()) < 0) {
164 zlog_err(
165 "eigrp_new: fatal error: eigrp_sock_init was unable to open "
166 "a socket");
167 exit(1);
168 }
169
170 eigrp->fd = eigrp_socket;
171 eigrp->maxsndbuflen = getsockopt_so_sendbuf(eigrp->fd);
172
173 if ((eigrp->ibuf = stream_new(EIGRP_PACKET_MAX_LEN + 1)) == NULL) {
174 zlog_err(
175 "eigrp_new: fatal error: stream_new (%u) failed allocating ibuf",
176 EIGRP_PACKET_MAX_LEN + 1);
177 exit(1);
178 }
179
180 eigrp->t_read = NULL;
181 thread_add_read(master, eigrp_read, eigrp, eigrp->fd, &eigrp->t_read);
182 eigrp->oi_write_q = list_new();
183
9ca66cc7 184 eigrp->topology_table = route_table_init();
d62a17ae 185
186 eigrp->neighbor_self = eigrp_nbr_new(NULL);
187 eigrp->neighbor_self->src.s_addr = INADDR_ANY;
188
189 eigrp->variance = EIGRP_VARIANCE_DEFAULT;
190 eigrp->max_paths = EIGRP_MAX_PATHS_DEFAULT;
191
192 eigrp->serno = 0;
193 eigrp->serno_last_update = 0;
194 eigrp->topology_changes_externalIPV4 = list_new();
195 eigrp->topology_changes_internalIPV4 = list_new();
196
197 eigrp->list[EIGRP_FILTER_IN] = NULL;
198 eigrp->list[EIGRP_FILTER_OUT] = NULL;
199
200 eigrp->prefix[EIGRP_FILTER_IN] = NULL;
201 eigrp->prefix[EIGRP_FILTER_OUT] = NULL;
202
203 eigrp->routemap[EIGRP_FILTER_IN] = NULL;
204 eigrp->routemap[EIGRP_FILTER_OUT] = NULL;
205
206 QOBJ_REG(eigrp, eigrp);
207 return eigrp;
7f57883e
DS
208}
209
d62a17ae 210static void eigrp_add(struct eigrp *eigrp)
7f57883e 211{
d62a17ae 212 listnode_add(eigrp_om->eigrp, eigrp);
7f57883e
DS
213}
214
d62a17ae 215static void eigrp_delete(struct eigrp *eigrp)
7f57883e 216{
d62a17ae 217 listnode_delete(eigrp_om->eigrp, eigrp);
7f57883e
DS
218}
219
d62a17ae 220struct eigrp *eigrp_get(const char *AS)
7f57883e 221{
d62a17ae 222 struct eigrp *eigrp;
7f57883e 223
d62a17ae 224 eigrp = eigrp_lookup();
225 if (eigrp == NULL) {
226 eigrp = eigrp_new(AS);
227 eigrp_add(eigrp);
228 }
7f57883e 229
d62a17ae 230 return eigrp;
7f57883e
DS
231}
232
233/* Shut down the entire process */
d62a17ae 234void eigrp_terminate(void)
7f57883e 235{
d62a17ae 236 struct eigrp *eigrp;
237 struct listnode *node, *nnode;
7f57883e 238
d62a17ae 239 /* shutdown already in progress */
240 if (CHECK_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN))
241 return;
7f57883e 242
d62a17ae 243 SET_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN);
7f57883e 244
d62a17ae 245 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp))
246 eigrp_finish(eigrp);
8879bd22
RW
247
248 frr_fini();
7f57883e
DS
249}
250
d62a17ae 251void eigrp_finish(struct eigrp *eigrp)
7f57883e 252{
d62a17ae 253 eigrp_finish_final(eigrp);
254
255 /* eigrp being shut-down? If so, was this the last eigrp instance? */
256 if (CHECK_FLAG(eigrp_om->options, EIGRP_MASTER_SHUTDOWN)
257 && (listcount(eigrp_om->eigrp) == 0)) {
258 if (zclient) {
259 zclient_stop(zclient);
260 zclient_free(zclient);
261 }
262 exit(0);
edaf6c01 263 }
7f57883e 264
d62a17ae 265 return;
7f57883e
DS
266}
267
268/* Final cleanup of eigrp instance */
d62a17ae 269void eigrp_finish_final(struct eigrp *eigrp)
7f57883e 270{
d62a17ae 271 struct eigrp_interface *ei;
272 struct eigrp_neighbor *nbr;
273 struct listnode *node, *nnode, *node2, *nnode2;
274
275 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
276 for (ALL_LIST_ELEMENTS(ei->nbrs, node2, nnode2, nbr))
277 eigrp_nbr_delete(nbr);
278 eigrp_if_free(ei, INTERFACE_DOWN_BY_FINAL);
279 }
7f57883e 280
d62a17ae 281 THREAD_OFF(eigrp->t_write);
282 THREAD_OFF(eigrp->t_read);
283 close(eigrp->fd);
7f57883e 284
affe9e99
DS
285 list_delete_and_null(&eigrp->eiflist);
286 list_delete_and_null(&eigrp->oi_write_q);
7f57883e 287
d62a17ae 288 eigrp_topology_cleanup(eigrp->topology_table);
289 eigrp_topology_free(eigrp->topology_table);
7f57883e 290
d62a17ae 291 eigrp_nbr_delete(eigrp->neighbor_self);
7f57883e 292
052fe054
DS
293 list_delete_and_null(&eigrp->topology_changes_externalIPV4);
294 list_delete_and_null(&eigrp->topology_changes_internalIPV4);
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}