]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_zebra.c
Merge pull request #1222 from opensourcerouting/isis-spf-improvements
[mirror_frr.git] / eigrpd / eigrp_zebra.c
1 /*
2 * Zebra connect library for EIGRP.
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 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
26 */
27
28 #include <zebra.h>
29
30 #include "thread.h"
31 #include "command.h"
32 #include "network.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "table.h"
36 #include "stream.h"
37 #include "memory.h"
38 #include "zclient.h"
39 #include "filter.h"
40 #include "plist.h"
41 #include "log.h"
42 #include "nexthop.h"
43
44 #include "eigrpd/eigrp_structs.h"
45 #include "eigrpd/eigrpd.h"
46 #include "eigrpd/eigrp_interface.h"
47 #include "eigrpd/eigrp_neighbor.h"
48 #include "eigrpd/eigrp_packet.h"
49 #include "eigrpd/eigrp_zebra.h"
50 #include "eigrpd/eigrp_vty.h"
51 #include "eigrpd/eigrp_dump.h"
52 #include "eigrpd/eigrp_network.h"
53 #include "eigrpd/eigrp_topology.h"
54 #include "eigrpd/eigrp_fsm.h"
55
56 static int eigrp_interface_add(int, struct zclient *, zebra_size_t, vrf_id_t);
57 static int eigrp_interface_delete(int, struct zclient *, zebra_size_t,
58 vrf_id_t);
59 static int eigrp_interface_address_add(int, struct zclient *, zebra_size_t,
60 vrf_id_t vrf_id);
61 static int eigrp_interface_address_delete(int, struct zclient *, zebra_size_t,
62 vrf_id_t vrf_id);
63 static int eigrp_interface_state_up(int, struct zclient *, zebra_size_t,
64 vrf_id_t vrf_id);
65 static int eigrp_interface_state_down(int, struct zclient *, zebra_size_t,
66 vrf_id_t vrf_id);
67 static struct interface *zebra_interface_if_lookup(struct stream *);
68
69 static int eigrp_zebra_read_route(int, struct zclient *, zebra_size_t,
70 vrf_id_t vrf_id);
71
72 /* Zebra structure to hold current status. */
73 struct zclient *zclient = NULL;
74
75 /* For registering threads. */
76 extern struct thread_master *master;
77 struct in_addr router_id_zebra;
78
79 /* Router-id update message from zebra. */
80 static int eigrp_router_id_update_zebra(int command, struct zclient *zclient,
81 zebra_size_t length, vrf_id_t vrf_id)
82 {
83 struct eigrp *eigrp;
84 struct prefix router_id;
85 zebra_router_id_update_read(zclient->ibuf, &router_id);
86
87 router_id_zebra = router_id.u.prefix4;
88
89 eigrp = eigrp_lookup();
90
91 if (eigrp != NULL)
92 eigrp_router_id_update(eigrp);
93
94 return 0;
95 }
96
97 static void eigrp_zebra_connected(struct zclient *zclient)
98 {
99 zclient_send_reg_requests(zclient, VRF_DEFAULT);
100 }
101
102 void eigrp_zebra_init(void)
103 {
104 zclient = zclient_new(master);
105
106 zclient_init(zclient, ZEBRA_ROUTE_EIGRP, 0);
107 zclient->zebra_connected = eigrp_zebra_connected;
108 zclient->router_id_update = eigrp_router_id_update_zebra;
109 zclient->interface_add = eigrp_interface_add;
110 zclient->interface_delete = eigrp_interface_delete;
111 zclient->interface_up = eigrp_interface_state_up;
112 zclient->interface_down = eigrp_interface_state_down;
113 zclient->interface_address_add = eigrp_interface_address_add;
114 zclient->interface_address_delete = eigrp_interface_address_delete;
115 zclient->redistribute_route_add = eigrp_zebra_read_route;
116 zclient->redistribute_route_del = eigrp_zebra_read_route;
117 }
118
119
120 /* Zebra route add and delete treatment. */
121 static int eigrp_zebra_read_route(int command, struct zclient *zclient,
122 zebra_size_t length, vrf_id_t vrf_id)
123 {
124 struct zapi_route api;
125 struct eigrp *eigrp;
126
127 if (zapi_route_decode(zclient->ibuf, &api) < 0)
128 return -1;
129
130 if (IPV4_NET127(ntohl(api.prefix.u.prefix4.s_addr)))
131 return 0;
132
133 eigrp = eigrp_lookup();
134 if (eigrp == NULL)
135 return 0;
136
137 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
138
139 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
140 {
141 }
142
143 return 0;
144 }
145
146 /* Inteface addition message from zebra. */
147 static int eigrp_interface_add(int command, struct zclient *zclient,
148 zebra_size_t length, vrf_id_t vrf_id)
149 {
150 struct interface *ifp;
151
152 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
153
154 assert(ifp->info);
155
156 if (!EIGRP_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
157 SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
158 IF_DEF_PARAMS(ifp)->type = eigrp_default_iftype(ifp);
159 }
160
161 eigrp_if_update(ifp);
162
163 return 0;
164 }
165
166 static int eigrp_interface_delete(int command, struct zclient *zclient,
167 zebra_size_t length, vrf_id_t vrf_id)
168 {
169 struct interface *ifp;
170 struct stream *s;
171 struct route_node *rn;
172
173 s = zclient->ibuf;
174 /* zebra_interface_state_read () updates interface structure in iflist
175 */
176 ifp = zebra_interface_state_read(s, vrf_id);
177
178 if (ifp == NULL)
179 return 0;
180
181 if (if_is_up(ifp))
182 zlog_warn("Zebra: got delete of %s, but interface is still up",
183 ifp->name);
184
185 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
186 zlog_debug(
187 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
188 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
189 ifp->metric, ifp->mtu);
190
191 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
192 if (rn->info)
193 eigrp_if_free((struct eigrp_interface *)rn->info,
194 INTERFACE_DOWN_BY_ZEBRA);
195
196 ifp->ifindex = IFINDEX_INTERNAL;
197 return 0;
198 }
199
200 static int eigrp_interface_address_add(int command, struct zclient *zclient,
201 zebra_size_t length, vrf_id_t vrf_id)
202 {
203 struct connected *c;
204
205 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
206
207 if (c == NULL)
208 return 0;
209
210 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
211 char buf[128];
212 prefix2str(c->address, buf, sizeof(buf));
213 zlog_debug("Zebra: interface %s address add %s", c->ifp->name,
214 buf);
215 }
216
217 eigrp_if_update(c->ifp);
218
219 return 0;
220 }
221
222 static int eigrp_interface_address_delete(int command, struct zclient *zclient,
223 zebra_size_t length, vrf_id_t vrf_id)
224 {
225 struct connected *c;
226 struct interface *ifp;
227 struct eigrp_interface *ei;
228 struct route_node *rn;
229 struct prefix p;
230
231 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
232
233 if (c == NULL)
234 return 0;
235
236 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
237 char buf[128];
238 prefix2str(c->address, buf, sizeof(buf));
239 zlog_debug("Zebra: interface %s address delete %s",
240 c->ifp->name, buf);
241 }
242
243 ifp = c->ifp;
244 p = *c->address;
245 p.prefixlen = IPV4_MAX_PREFIXLEN;
246
247 rn = route_node_lookup(IF_OIFS(ifp), &p);
248 if (!rn) {
249 connected_free(c);
250 return 0;
251 }
252
253 assert(rn->info);
254 ei = rn->info;
255
256 /* Call interface hook functions to clean up */
257 eigrp_if_free(ei, INTERFACE_DOWN_BY_ZEBRA);
258
259 connected_free(c);
260
261 return 0;
262 }
263
264 static int eigrp_interface_state_up(int command, struct zclient *zclient,
265 zebra_size_t length, vrf_id_t vrf_id)
266 {
267 struct interface *ifp;
268 struct eigrp_interface *ei;
269 struct route_node *rn;
270
271 ifp = zebra_interface_if_lookup(zclient->ibuf);
272
273 if (ifp == NULL)
274 return 0;
275
276 /* Interface is already up. */
277 if (if_is_operative(ifp)) {
278 /* Temporarily keep ifp values. */
279 struct interface if_tmp;
280 memcpy(&if_tmp, ifp, sizeof(struct interface));
281
282 zebra_interface_if_set_value(zclient->ibuf, ifp);
283
284 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
285 zlog_debug("Zebra: Interface[%s] state update.",
286 ifp->name);
287
288 if (if_tmp.bandwidth != ifp->bandwidth) {
289 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
290 zlog_debug(
291 "Zebra: Interface[%s] bandwidth change %d -> %d.",
292 ifp->name, if_tmp.bandwidth,
293 ifp->bandwidth);
294
295 // eigrp_if_recalculate_output_cost (ifp);
296 }
297
298 if (if_tmp.mtu != ifp->mtu) {
299 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
300 zlog_debug(
301 "Zebra: Interface[%s] MTU change %u -> %u.",
302 ifp->name, if_tmp.mtu, ifp->mtu);
303
304 /* Must reset the interface (simulate down/up) when MTU
305 * changes. */
306 eigrp_if_reset(ifp);
307 }
308 return 0;
309 }
310
311 zebra_interface_if_set_value(zclient->ibuf, ifp);
312
313 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
314 zlog_debug("Zebra: Interface[%s] state change to up.",
315 ifp->name);
316
317 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
318 if ((ei = rn->info) == NULL)
319 continue;
320
321 eigrp_if_up(ei);
322 }
323
324 return 0;
325 }
326
327 static int eigrp_interface_state_down(int command, struct zclient *zclient,
328 zebra_size_t length, vrf_id_t vrf_id)
329 {
330 struct interface *ifp;
331 struct eigrp_interface *ei;
332 struct route_node *node;
333
334 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
335
336 if (ifp == NULL)
337 return 0;
338
339 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
340 zlog_debug("Zebra: Interface[%s] state change to down.",
341 ifp->name);
342
343 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
344 if ((ei = node->info) == NULL)
345 continue;
346 eigrp_if_down(ei);
347 }
348
349 return 0;
350 }
351
352 static struct interface *zebra_interface_if_lookup(struct stream *s)
353 {
354 char ifname_tmp[INTERFACE_NAMSIZ];
355
356 /* Read interface name. */
357 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
358
359 /* And look it up. */
360 return if_lookup_by_name_len(
361 ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), VRF_DEFAULT);
362 }
363
364 void eigrp_zebra_route_add(struct prefix *p, struct list *successors)
365 {
366 struct zapi_route api;
367 struct zapi_nexthop *api_nh;
368 struct eigrp_nexthop_entry *te;
369 struct listnode *node;
370 int count = 0;
371
372 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
373 return;
374
375 memset(&api, 0, sizeof(api));
376 api.vrf_id = VRF_DEFAULT;
377 api.type = ZEBRA_ROUTE_EIGRP;
378 api.safi = SAFI_UNICAST;
379 memcpy(&api.prefix, p, sizeof(*p));
380
381 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
382
383 /* Nexthop, ifindex, distance and metric information. */
384 for (ALL_LIST_ELEMENTS_RO(successors, node, te)) {
385 if (count >= MULTIPATH_NUM)
386 break;
387 api_nh = &api.nexthops[count];
388 if (te->adv_router->src.s_addr) {
389 api_nh->gate.ipv4 = te->adv_router->src;
390 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
391 } else
392 api_nh->type = NEXTHOP_TYPE_IFINDEX;
393 api_nh->ifindex = te->ei->ifp->ifindex;
394
395 count++;
396 }
397 api.nexthop_num = count;
398
399 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
400 char buf[2][PREFIX_STRLEN];
401 zlog_debug("Zebra: Route add %s nexthop %s",
402 prefix2str(p, buf[0], PREFIX_STRLEN),
403 inet_ntop(AF_INET, 0, buf[1], PREFIX_STRLEN));
404 }
405
406 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
407 }
408
409 void eigrp_zebra_route_delete(struct prefix *p)
410 {
411 struct zapi_route api;
412
413 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
414 return;
415
416 memset(&api, 0, sizeof(api));
417 api.vrf_id = VRF_DEFAULT;
418 api.type = ZEBRA_ROUTE_EIGRP;
419 api.safi = SAFI_UNICAST;
420 memcpy(&api.prefix, p, sizeof(*p));
421 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
422
423 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
424 char buf[PREFIX_STRLEN];
425 zlog_debug("Zebra: Route del %s",
426 prefix2str(p, buf, PREFIX_STRLEN));
427 }
428
429 return;
430 }
431
432 int eigrp_is_type_redistributed(int type)
433 {
434 return ((DEFAULT_ROUTE_TYPE(type))
435 ? vrf_bitmap_check(zclient->default_information,
436 VRF_DEFAULT)
437 : vrf_bitmap_check(zclient->redist[AFI_IP][type],
438 VRF_DEFAULT));
439 }
440
441 int eigrp_redistribute_set(struct eigrp *eigrp, int type,
442 struct eigrp_metrics metric)
443 {
444
445 if (eigrp_is_type_redistributed(type)) {
446 if (eigrp_metrics_is_same(metric, eigrp->dmetric[type])) {
447 eigrp->dmetric[type] = metric;
448 }
449
450 eigrp_external_routes_refresh(eigrp, type);
451
452 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
453 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
454 // Metric[%d]",
455 // eigrp_redist_string(type),
456 // metric_type (eigrp, type), metric_value
457 // (eigrp, type));
458 return CMD_SUCCESS;
459 }
460
461 eigrp->dmetric[type] = metric;
462
463 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, 0,
464 VRF_DEFAULT);
465
466 ++eigrp->redistribute;
467
468 return CMD_SUCCESS;
469 }
470
471 int eigrp_redistribute_unset(struct eigrp *eigrp, int type)
472 {
473
474 if (eigrp_is_type_redistributed(type)) {
475 memset(&eigrp->dmetric[type], 0, sizeof(struct eigrp_metrics));
476 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP,
477 type, 0, VRF_DEFAULT);
478 --eigrp->redistribute;
479 }
480
481 return CMD_SUCCESS;
482 }