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