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