2 * EIGRP Daemon Program.
3 * Copyright (C) 2013-2014
11 * This file is part of GNU Zebra.
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
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.
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
40 #include "sockunion.h" /* for inet_aton () */
46 #include "lib_errors.h"
47 #include "distribute.h"
49 #include "eigrpd/eigrp_structs.h"
50 #include "eigrpd/eigrpd.h"
51 #include "eigrpd/eigrp_interface.h"
52 #include "eigrpd/eigrp_zebra.h"
53 #include "eigrpd/eigrp_vty.h"
54 #include "eigrpd/eigrp_neighbor.h"
55 #include "eigrpd/eigrp_packet.h"
56 #include "eigrpd/eigrp_network.h"
57 #include "eigrpd/eigrp_topology.h"
58 #include "eigrpd/eigrp_memory.h"
59 #include "eigrpd/eigrp_filter.h"
61 DEFINE_QOBJ_TYPE(eigrp
)
63 static struct eigrp_master eigrp_master
;
65 struct eigrp_master
*eigrp_om
;
67 static struct eigrp
*eigrp_new(const char *);
69 extern struct zclient
*zclient
;
70 extern struct in_addr router_id_zebra
;
74 * void eigrp_router_id_update(struct eigrp *eigrp)
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
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
86 * 3. Last choice: just go with whatever the zebra daemon recommends.
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
93 * This does not work for IPv6, and to make the code simpler, its
94 * stored and processed internerall as a 32bit number
96 void eigrp_router_id_update(struct eigrp
*eigrp
)
98 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
99 struct interface
*ifp
;
100 struct in_addr router_id
, router_id_old
;
102 router_id_old
= eigrp
->router_id
;
104 if (eigrp
->router_id_static
.s_addr
!= 0)
105 router_id
= eigrp
->router_id_static
;
107 else if (eigrp
->router_id
.s_addr
!= 0)
108 router_id
= eigrp
->router_id
;
111 router_id
= router_id_zebra
;
113 eigrp
->router_id
= router_id
;
114 if (router_id_old
.s_addr
!= router_id
.s_addr
) {
115 // if (IS_DEBUG_EIGRP_EVENT)
116 // zlog_debug("Router-ID[NEW:%s]: Update",
117 // inet_ntoa(eigrp->router_id));
119 /* update eigrp_interface's */
120 FOR_ALL_INTERFACES (vrf
, ifp
)
121 eigrp_if_update(ifp
);
125 void eigrp_master_init(void)
129 memset(&eigrp_master
, 0, sizeof(struct eigrp_master
));
131 eigrp_om
= &eigrp_master
;
132 eigrp_om
->eigrp
= list_new();
135 eigrp_om
->start_time
= tv
.tv_sec
;
138 /* Allocate new eigrp structure. */
139 static struct eigrp
*eigrp_new(const char *AS
)
141 struct eigrp
*eigrp
= XCALLOC(MTYPE_EIGRP_TOP
, sizeof(struct eigrp
));
144 /* init information relevant to peers */
146 eigrp
->AS
= atoi(AS
);
147 eigrp
->router_id
.s_addr
= 0;
148 eigrp
->router_id_static
.s_addr
= 0;
149 eigrp
->sequence_number
= 1;
151 /*Configure default K Values for EIGRP Process*/
152 eigrp
->k_values
[0] = EIGRP_K1_DEFAULT
;
153 eigrp
->k_values
[1] = EIGRP_K2_DEFAULT
;
154 eigrp
->k_values
[2] = EIGRP_K3_DEFAULT
;
155 eigrp
->k_values
[3] = EIGRP_K4_DEFAULT
;
156 eigrp
->k_values
[4] = EIGRP_K5_DEFAULT
;
157 eigrp
->k_values
[5] = EIGRP_K6_DEFAULT
;
159 /* init internal data structures */
160 eigrp
->eiflist
= list_new();
161 eigrp
->passive_interface_default
= EIGRP_IF_ACTIVE
;
162 eigrp
->networks
= eigrp_topology_new();
164 if ((eigrp_socket
= eigrp_sock_init()) < 0) {
167 "eigrp_new: fatal error: eigrp_sock_init was unable to open a socket");
171 eigrp
->fd
= eigrp_socket
;
172 eigrp
->maxsndbuflen
= getsockopt_so_sendbuf(eigrp
->fd
);
174 eigrp
->ibuf
= stream_new(EIGRP_PACKET_MAX_LEN
+ 1);
176 eigrp
->t_read
= NULL
;
177 thread_add_read(master
, eigrp_read
, eigrp
, eigrp
->fd
, &eigrp
->t_read
);
178 eigrp
->oi_write_q
= list_new();
180 eigrp
->topology_table
= route_table_init();
182 eigrp
->neighbor_self
= eigrp_nbr_new(NULL
);
183 eigrp
->neighbor_self
->src
.s_addr
= INADDR_ANY
;
185 eigrp
->variance
= EIGRP_VARIANCE_DEFAULT
;
186 eigrp
->max_paths
= EIGRP_MAX_PATHS_DEFAULT
;
189 eigrp
->serno_last_update
= 0;
190 eigrp
->topology_changes_externalIPV4
= list_new();
191 eigrp
->topology_changes_internalIPV4
= list_new();
193 eigrp
->list
[EIGRP_FILTER_IN
] = NULL
;
194 eigrp
->list
[EIGRP_FILTER_OUT
] = NULL
;
196 eigrp
->prefix
[EIGRP_FILTER_IN
] = NULL
;
197 eigrp
->prefix
[EIGRP_FILTER_OUT
] = NULL
;
199 eigrp
->routemap
[EIGRP_FILTER_IN
] = NULL
;
200 eigrp
->routemap
[EIGRP_FILTER_OUT
] = NULL
;
202 /* Distribute list install. */
203 eigrp
->distribute_ctx
= distribute_list_ctx_create(
204 vrf_lookup_by_id(VRF_DEFAULT
));
205 distribute_list_add_hook(eigrp
->distribute_ctx
,
206 eigrp_distribute_update
);
207 distribute_list_delete_hook(eigrp
->distribute_ctx
,
208 eigrp_distribute_update
);
211 eigrp->if_rmap_ctx = if_rmap_ctx_create(
213 if_rmap_hook_add (eigrp_if_rmap_update);
214 if_rmap_hook_delete (eigrp_if_rmap_update);
216 QOBJ_REG(eigrp
, eigrp
);
220 struct eigrp
*eigrp_get(const char *AS
)
224 eigrp
= eigrp_lookup();
226 eigrp
= eigrp_new(AS
);
227 listnode_add(eigrp_om
->eigrp
, eigrp
);
233 /* Shut down the entire process */
234 void eigrp_terminate(void)
237 struct listnode
*node
, *nnode
;
239 /* shutdown already in progress */
240 if (CHECK_FLAG(eigrp_om
->options
, EIGRP_MASTER_SHUTDOWN
))
243 SET_FLAG(eigrp_om
->options
, EIGRP_MASTER_SHUTDOWN
);
245 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
))
251 void eigrp_finish(struct eigrp
*eigrp
)
253 eigrp_finish_final(eigrp
);
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)) {
259 zclient_stop(zclient
);
260 zclient_free(zclient
);
268 /* Final cleanup of eigrp instance */
269 void eigrp_finish_final(struct eigrp
*eigrp
)
271 struct eigrp_interface
*ei
;
272 struct eigrp_neighbor
*nbr
;
273 struct listnode
*node
, *nnode
, *node2
, *nnode2
;
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
);
281 THREAD_OFF(eigrp
->t_write
);
282 THREAD_OFF(eigrp
->t_read
);
285 list_delete(&eigrp
->eiflist
);
286 list_delete(&eigrp
->oi_write_q
);
288 eigrp_topology_free(eigrp
->topology_table
);
290 eigrp_nbr_delete(eigrp
->neighbor_self
);
292 list_delete(&eigrp
->topology_changes_externalIPV4
);
293 list_delete(&eigrp
->topology_changes_internalIPV4
);
295 listnode_delete(eigrp_om
->eigrp
, eigrp
);
297 stream_free(eigrp
->ibuf
);
298 distribute_list_delete(&eigrp
->distribute_ctx
);
299 XFREE(MTYPE_EIGRP_TOP
, eigrp
);
302 /*Look for existing eigrp process*/
303 struct eigrp
*eigrp_lookup(void)
305 if (listcount(eigrp_om
->eigrp
) == 0)
308 return listgetdata(listhead(eigrp_om
->eigrp
));