]> git.proxmox.com Git - mirror_frr.git/blame - zebra/redistribute.c
ospfd: Get `default-information originate` working for ospf instances
[mirror_frr.git] / zebra / redistribute.c
CommitLineData
718e3744 1/* Redistribution Handler
2 * Copyright (C) 1998 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
22
23#include "vector.h"
24#include "vty.h"
25#include "command.h"
26#include "prefix.h"
27#include "table.h"
28#include "stream.h"
29#include "zclient.h"
30#include "linklist.h"
31#include "log.h"
b72ede27 32#include "vrf.h"
05737783 33#include "srcdest_table.h"
718e3744 34
35#include "zebra/rib.h"
161e9ab7 36#include "zebra/zebra_router.h"
7c551956
DS
37#include "zebra/zebra_ns.h"
38#include "zebra/zebra_vrf.h"
8902474b 39#include "zebra/zebra_routemap.h"
718e3744 40#include "zebra/redistribute.h"
41#include "zebra/debug.h"
18a6dce6 42#include "zebra/router-id.h"
bf094f69 43#include "zebra/zapi_msg.h"
1a98c087 44#include "zebra/zebra_vxlan.h"
364fed6b 45#include "zebra/zebra_errors.h"
718e3744 46
244c1cdc
DS
47#define ZEBRA_PTM_SUPPORT
48
7a4bb9c5
DS
49/* array holding redistribute info about table redistribution */
50/* bit AFI is set if that AFI is redistributing routes from this table */
032bfaaf 51static int zebra_import_table_used[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
d7c0a89a 52static uint32_t zebra_import_table_distance[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
7a4bb9c5 53
fe257ae7 54int is_zebra_import_table_enabled(afi_t afi, vrf_id_t vrf_id, uint32_t table_id)
7a4bb9c5 55{
1e9f448f
DS
56 /*
57 * Make sure that what we are called with actualy makes sense
58 */
59 if (afi == AFI_MAX)
60 return 0;
61
95a29032
DS
62 if (is_zebra_valid_kernel_table(table_id) &&
63 table_id < ZEBRA_KERNEL_TABLE_MAX)
d62a17ae 64 return zebra_import_table_used[afi][table_id];
65 return 0;
7a4bb9c5
DS
66}
67
d62a17ae 68static void zebra_redistribute_default(struct zserv *client, vrf_id_t vrf_id)
718e3744 69{
d62a17ae 70 int afi;
71 struct prefix p;
72 struct route_table *table;
73 struct route_node *rn;
74 struct route_entry *newre;
75
76 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
29a35332
DS
77
78 if (!vrf_bitmap_check(client->redist_default[afi], vrf_id))
79 continue;
80
d62a17ae 81 /* Lookup table. */
82 table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id);
83 if (!table)
84 continue;
85
86 /* Lookup default route. */
87 memset(&p, 0, sizeof(p));
88 p.family = afi2family(afi);
89 rn = route_node_lookup(table, &p);
90 if (!rn)
91 continue;
92
a2addae8 93 RNODE_FOREACH_RE (rn, newre) {
c3d0d6e8 94 if (CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED))
407c87a6 95 zsend_redistribute_route(
a2addae8
RW
96 ZEBRA_REDISTRIBUTE_ROUTE_ADD, client,
97 &rn->p, NULL, newre);
407c87a6 98 }
d62a17ae 99
100 route_unlock_node(rn);
101 }
718e3744 102}
103
104/* Redistribute routes. */
d7c0a89a
QY
105static void zebra_redistribute(struct zserv *client, int type,
106 unsigned short instance, vrf_id_t vrf_id,
107 int afi)
718e3744 108{
d62a17ae 109 struct route_entry *newre;
110 struct route_table *table;
111 struct route_node *rn;
112
113 table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id);
114 if (!table)
115 return;
116
f0c4b8e1 117 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 118 RNODE_FOREACH_RE (rn, newre) {
86391e56 119 const struct prefix *dst_p, *src_p;
3e178809 120
d62a17ae 121 srcdest_rnode_prefixes(rn, &dst_p, &src_p);
122
14a4d9d0 123 if (IS_ZEBRA_DEBUG_RIB)
d62a17ae 124 zlog_debug(
1945fb7a 125 "%s: client %s %pFX(%u:%u) checking: selected=%d, type=%d, distance=%d, metric=%d zebra_check_addr=%d",
d62a17ae 126 __func__,
1b6e575b 127 zebra_route_string(client->proto),
1945fb7a
DS
128 dst_p, vrf_id, newre->instance,
129 !!CHECK_FLAG(newre->flags,
130 ZEBRA_FLAG_SELECTED),
d62a17ae 131 newre->type, newre->distance,
3e178809 132 newre->metric, zebra_check_addr(dst_p));
d62a17ae 133
134 if (!CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED))
135 continue;
136 if ((type != ZEBRA_ROUTE_ALL
137 && (newre->type != type
138 || newre->instance != instance)))
139 continue;
d62a17ae 140 if (!zebra_check_addr(dst_p))
141 continue;
142
74489921
RW
143 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD,
144 client, dst_p, src_p, newre);
d62a17ae 145 }
718e3744 146}
147
14fe366e
S
148/*
149 * Function to check if prefix is candidate for
150 * redistribute.
151 */
152static bool zebra_redistribute_check(const struct route_entry *re,
153 struct zserv *client,
154 const struct prefix *p, int afi)
155{
79a9ad14
DS
156 struct zebra_vrf *zvrf;
157
14fe366e
S
158 /* Process only if there is valid re */
159 if (!re)
160 return false;
161
79a9ad14
DS
162 zvrf = vrf_info_lookup(re->vrf_id);
163 if (re->vrf_id == VRF_DEFAULT && zvrf->table_id != re->table)
164 return false;
165
14fe366e
S
166 /* If default route and redistributed */
167 if (is_default_prefix(p)
168 && vrf_bitmap_check(client->redist_default[afi], re->vrf_id))
169 return true;
170
171 /* If redistribute in enabled for zebra route all */
172 if (vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL], re->vrf_id))
173 return true;
174
175 /*
176 * If multi-instance then check for route
177 * redistribution for given instance.
178 */
179 if (re->instance
180 && redist_check_instance(&client->mi_redist[afi][re->type],
181 re->instance))
182 return true;
183
184 /* If redistribution is enabled for give route type. */
185 if (vrf_bitmap_check(client->redist[afi][re->type], re->vrf_id))
186 return true;
187
188 return false;
189}
190
c41fc67b 191/* Either advertise a route for redistribution to registered clients or */
192/* withdraw redistribution if add cannot be done for client */
86391e56 193void redistribute_update(const struct prefix *p, const struct prefix *src_p,
40f321c0
MS
194 const struct route_entry *re,
195 const struct route_entry *prev_re)
718e3744 196{
d62a17ae 197 struct listnode *node, *nnode;
198 struct zserv *client;
d62a17ae 199 int afi;
d62a17ae 200
2dbe669b 201 if (IS_ZEBRA_DEBUG_RIB)
d62a17ae 202 zlog_debug(
1945fb7a
DS
203 "(%u:%u):%pFX(%u): Redist update re %p (%s), old %p (%s)",
204 re->vrf_id, re->table, p, re->instance, re,
2dbe669b 205 zebra_route_string(re->type), prev_re,
2da33d6b 206 prev_re ? zebra_route_string(prev_re->type) : "None");
d62a17ae 207
208 afi = family2afi(p->family);
209 if (!afi) {
e914ccbe 210 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
1d5453d6 211 "%s: Unknown AFI/SAFI prefix received", __func__);
d62a17ae 212 return;
c41fc67b 213 }
79becec8 214 if (!zebra_check_addr(p)) {
215 if (IS_ZEBRA_DEBUG_RIB)
2dbe669b 216 zlog_debug("Redist update filter prefix %pFX", p);
79becec8 217 return;
218 }
219
d62a17ae 220
161e9ab7 221 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
14fe366e 222 if (zebra_redistribute_check(re, client, p, afi)) {
14a4d9d0 223 if (IS_ZEBRA_DEBUG_RIB) {
3e178809 224 zlog_debug(
2dbe669b 225 "%s: client %s %pFX(%u:%u), type=%d, distance=%d, metric=%d",
c2c02b76 226 __func__,
2dbe669b 227 zebra_route_string(client->proto), p,
c2c02b76
DS
228 re->vrf_id, re->table, re->type,
229 re->distance, re->metric);
3e178809 230 }
74489921
RW
231 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD,
232 client, p, src_p, re);
14fe366e 233 } else if (zebra_redistribute_check(prev_re, client, p, afi))
74489921
RW
234 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
235 client, p, src_p, prev_re);
c41fc67b 236 }
718e3744 237}
238
40f321c0
MS
239/*
240 * During a route delete, where 'new_re' is NULL, redist a delete to all
241 * clients registered for the type of 'old_re'.
242 * During a route update, redist a delete to any clients who will not see
243 * an update when the new route is installed. There are cases when a client
244 * may have seen a redist for 'old_re', but will not see
245 * the redist for 'new_re'.
246 */
86391e56 247void redistribute_delete(const struct prefix *p, const struct prefix *src_p,
40f321c0
MS
248 const struct route_entry *old_re,
249 const struct route_entry *new_re)
718e3744 250{
d62a17ae 251 struct listnode *node, *nnode;
252 struct zserv *client;
d62a17ae 253 int afi;
40f321c0
MS
254 vrf_id_t vrfid;
255
256 if (old_re)
257 vrfid = old_re->vrf_id;
258 else if (new_re)
259 vrfid = new_re->vrf_id;
260 else
261 return;
d62a17ae 262
263 if (IS_ZEBRA_DEBUG_RIB) {
2dbe669b
DA
264 zlog_debug("%u:%pFX: Redist del: re %p (%s), new re %p (%s)",
265 vrfid, p, old_re,
266 old_re ? zebra_route_string(old_re->type) : "None",
267 new_re,
268 new_re ? zebra_route_string(new_re->type) : "None");
d62a17ae 269 }
270
d62a17ae 271 afi = family2afi(p->family);
272 if (!afi) {
e914ccbe 273 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
1d5453d6 274 "%s: Unknown AFI/SAFI prefix received",
40f321c0 275 __func__);
d62a17ae 276 return;
277 }
278
40f321c0 279 /* Skip invalid (e.g. linklocal) prefix */
79becec8 280 if (!zebra_check_addr(p)) {
40f321c0
MS
281 if (IS_ZEBRA_DEBUG_RIB) {
282 zlog_debug(
2dbe669b
DA
283 "%u:%pFX: Redist del old: skipping invalid prefix",
284 vrfid, p);
40f321c0 285 }
79becec8 286 return;
287 }
288
161e9ab7 289 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
290 /* Do not send unsolicited messages to synchronous clients. */
291 if (client->synchronous)
292 continue;
14fe366e
S
293 /*
294 * Skip this client if it will receive an update for the
295 * 'new' re
296 */
297 if (zebra_redistribute_check(new_re, client, p, afi))
298 continue;
40f321c0
MS
299
300 /* Send a delete for the 'old' re to any subscribed client. */
14fe366e 301 if (zebra_redistribute_check(old_re, client, p, afi))
74489921 302 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
40f321c0 303 client, p, src_p, old_re);
1eb8ef25 304 }
718e3744 305}
306
14fe366e 307
89f4e507 308void zebra_redistribute_add(ZAPI_HANDLER_ARGS)
718e3744 309{
ec93aa12
DS
310 afi_t afi = 0;
311 int type = 0;
d7c0a89a 312 unsigned short instance;
d62a17ae 313
1002497a
QY
314 STREAM_GETC(msg, afi);
315 STREAM_GETC(msg, type);
316 STREAM_GETW(msg, instance);
d62a17ae 317
1b6e575b
PZ
318 if (IS_ZEBRA_DEBUG_EVENT)
319 zlog_debug(
c479e756 320 "%s: client proto %s afi=%d, wants %s, vrf %s(%u), instance=%d",
1b6e575b 321 __func__, zebra_route_string(client->proto), afi,
c479e756
DS
322 zebra_route_string(type), VRF_LOGNAME(zvrf->vrf),
323 zvrf_id(zvrf), instance);
1b6e575b 324
e1fa928d 325 if (afi == 0 || afi >= AFI_MAX) {
e914ccbe 326 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
15569c58 327 "%s: Specified afi %d does not exist", __func__, afi);
d62a17ae 328 return;
ec93aa12
DS
329 }
330
331 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
9df414fe 332 zlog_debug("%s: Specified Route Type %d does not exist",
15569c58 333 __func__, type);
ec93aa12
DS
334 return;
335 }
d62a17ae 336
337 if (instance) {
338 if (!redist_check_instance(&client->mi_redist[afi][type],
339 instance)) {
340 redist_add_instance(&client->mi_redist[afi][type],
341 instance);
342 zebra_redistribute(client, type, instance,
343 zvrf_id(zvrf), afi);
344 }
345 } else {
d9083050
IR
346 if (!vrf_bitmap_check(client->redist[afi][type],
347 zvrf_id(zvrf))) {
348 if (IS_ZEBRA_DEBUG_EVENT)
349 zlog_debug(
350 "%s: setting vrf %s(%u) redist bitmap",
351 __func__, VRF_LOGNAME(zvrf->vrf),
352 zvrf_id(zvrf));
353 vrf_bitmap_set(client->redist[afi][type],
354 zvrf_id(zvrf));
355 zebra_redistribute(client, type, 0, zvrf_id(zvrf), afi);
356 }
4e1cadf0 357 }
ec93aa12
DS
358
359stream_failure:
360 return;
ebf08631 361}
718e3744 362
89f4e507 363void zebra_redistribute_delete(ZAPI_HANDLER_ARGS)
718e3744 364{
ec93aa12
DS
365 afi_t afi = 0;
366 int type = 0;
d7c0a89a 367 unsigned short instance;
d62a17ae 368
1002497a
QY
369 STREAM_GETC(msg, afi);
370 STREAM_GETC(msg, type);
371 STREAM_GETW(msg, instance);
d62a17ae 372
67da9573
EDP
373 if (IS_ZEBRA_DEBUG_EVENT)
374 zlog_debug(
375 "%s: client proto %s afi=%d, no longer wants %s, vrf %s(%u), instance=%d",
376 __func__, zebra_route_string(client->proto), afi,
377 zebra_route_string(type), VRF_LOGNAME(zvrf->vrf),
378 zvrf_id(zvrf), instance);
379
380
e1fa928d 381 if (afi == 0 || afi >= AFI_MAX) {
e914ccbe 382 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
15569c58 383 "%s: Specified afi %d does not exist", __func__, afi);
d62a17ae 384 return;
ec93aa12
DS
385 }
386
387 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
9df414fe 388 zlog_debug("%s: Specified Route Type %d does not exist",
15569c58 389 __func__, type);
ec93aa12
DS
390 return;
391 }
d62a17ae 392
393 /*
394 * NOTE: no need to withdraw the previously advertised routes. The
395 * clients
396 * themselves should keep track of the received routes from zebra and
397 * withdraw them when necessary.
398 */
399 if (instance)
400 redist_del_instance(&client->mi_redist[afi][type], instance);
401 else
402 vrf_bitmap_unset(client->redist[afi][type], zvrf_id(zvrf));
ec93aa12
DS
403
404stream_failure:
405 return;
ebf08631 406}
718e3744 407
89f4e507 408void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS)
718e3744 409{
49db7a7b
RW
410 afi_t afi = 0;
411
412 STREAM_GETC(msg, afi);
413
414 if (afi == 0 || afi >= AFI_MAX) {
415 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
15569c58 416 "%s: Specified afi %u does not exist", __func__, afi);
49db7a7b
RW
417 return;
418 }
419
420 vrf_bitmap_set(client->redist_default[afi], zvrf_id(zvrf));
d62a17ae 421 zebra_redistribute_default(client, zvrf_id(zvrf));
49db7a7b
RW
422
423stream_failure:
424 return;
d62a17ae 425}
718e3744 426
89f4e507 427void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS)
718e3744 428{
49db7a7b
RW
429 afi_t afi = 0;
430
431 STREAM_GETC(msg, afi);
432
433 if (afi == 0 || afi >= AFI_MAX) {
434 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
15569c58 435 "%s: Specified afi %u does not exist", __func__, afi);
49db7a7b
RW
436 return;
437 }
438
439 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
440
441stream_failure:
442 return;
d62a17ae 443}
718e3744 444
445/* Interface up information. */
d62a17ae 446void zebra_interface_up_update(struct interface *ifp)
718e3744 447{
d62a17ae 448 struct listnode *node, *nnode;
449 struct zserv *client;
450
451 if (IS_ZEBRA_DEBUG_EVENT)
096f7609
IR
452 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s vrf %s(%u)",
453 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
d62a17ae 454
455 if (ifp->ptm_status || !ifp->ptm_enable) {
161e9ab7 456 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode,
a8a20c4e 457 client)) {
17da84a4
KS
458 /* Do not send unsolicited messages to synchronous
459 * clients.
460 */
461 if (client->synchronous)
462 continue;
463
a8a20c4e
DS
464 zsend_interface_update(ZEBRA_INTERFACE_UP,
465 client, ifp);
466 zsend_interface_link_params(client, ifp);
467 }
16f1b9ee 468 }
718e3744 469}
470
471/* Interface down information. */
d62a17ae 472void zebra_interface_down_update(struct interface *ifp)
718e3744 473{
d62a17ae 474 struct listnode *node, *nnode;
475 struct zserv *client;
718e3744 476
d62a17ae 477 if (IS_ZEBRA_DEBUG_EVENT)
096f7609
IR
478 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s vrf %s(%u)",
479 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
718e3744 480
161e9ab7 481 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
482 /* Do not send unsolicited messages to synchronous clients. */
483 if (client->synchronous)
484 continue;
485
d62a17ae 486 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
487 }
718e3744 488}
489
490/* Interface information update. */
d62a17ae 491void zebra_interface_add_update(struct interface *ifp)
718e3744 492{
d62a17ae 493 struct listnode *node, *nnode;
494 struct zserv *client;
495
496 if (IS_ZEBRA_DEBUG_EVENT)
096f7609
IR
497 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s vrf %s(%u)",
498 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
d62a17ae 499
a8a20c4e 500 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
501 /* Do not send unsolicited messages to synchronous clients. */
502 if (client->synchronous)
503 continue;
504
a8a20c4e
DS
505 client->ifadd_cnt++;
506 zsend_interface_add(client, ifp);
507 zsend_interface_link_params(client, ifp);
508 }
718e3744 509}
510
d62a17ae 511void zebra_interface_delete_update(struct interface *ifp)
718e3744 512{
d62a17ae 513 struct listnode *node, *nnode;
514 struct zserv *client;
718e3744 515
d62a17ae 516 if (IS_ZEBRA_DEBUG_EVENT)
096f7609
IR
517 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s vrf %s(%u)",
518 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
718e3744 519
161e9ab7 520 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
521 /* Do not send unsolicited messages to synchronous clients. */
522 if (client->synchronous)
523 continue;
524
d62a17ae 525 client->ifdel_cnt++;
526 zsend_interface_delete(client, ifp);
527 }
718e3744 528}
529
530/* Interface address addition. */
d62a17ae 531void zebra_interface_address_add_update(struct interface *ifp,
532 struct connected *ifc)
718e3744 533{
d62a17ae 534 struct listnode *node, *nnode;
535 struct zserv *client;
536 struct prefix *p;
537
538 if (IS_ZEBRA_DEBUG_EVENT) {
d62a17ae 539 p = ifc->address;
2dbe669b 540 zlog_debug(
096f7609
IR
541 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %pFX on %s vrf %s(%u)",
542 p, ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
d62a17ae 543 }
544
545 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
9df414fe 546 flog_warn(
e914ccbe 547 EC_ZEBRA_ADVERTISING_UNUSABLE_ADDR,
ad6f7449 548 "advertising address to clients that is not yet usable.");
d62a17ae 549
1a98c087
MK
550 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 1);
551
d62a17ae 552 router_id_add_address(ifc);
553
17da84a4
KS
554 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
555 /* Do not send unsolicited messages to synchronous clients. */
556 if (client->synchronous)
557 continue;
558
d62a17ae 559 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
560 client->connected_rt_add_cnt++;
561 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD,
562 client, ifp, ifc);
563 }
17da84a4 564 }
718e3744 565}
566
567/* Interface address deletion. */
d62a17ae 568void zebra_interface_address_delete_update(struct interface *ifp,
569 struct connected *ifc)
718e3744 570{
d62a17ae 571 struct listnode *node, *nnode;
572 struct zserv *client;
573 struct prefix *p;
574
575 if (IS_ZEBRA_DEBUG_EVENT) {
d62a17ae 576 p = ifc->address;
2dbe669b 577 zlog_debug(
096f7609
IR
578 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %pFX on %s vrf %s(%u)",
579 p, ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
d62a17ae 580 }
581
1a98c087
MK
582 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0);
583
d62a17ae 584 router_id_del_address(ifc);
585
17da84a4
KS
586 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
587 /* Do not send unsolicited messages to synchronous clients. */
588 if (client->synchronous)
589 continue;
590
d62a17ae 591 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
592 client->connected_rt_del_cnt++;
593 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE,
594 client, ifp, ifc);
595 }
17da84a4 596 }
718e3744 597}
d5a5c8f0 598
c8e264b6 599/* Interface VRF change. May need to delete from clients not interested in
600 * the new VRF. Note that this function is invoked *prior* to the VRF change.
601 */
d62a17ae 602void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id)
c8e264b6 603{
d62a17ae 604 struct listnode *node, *nnode;
605 struct zserv *client;
606
607 if (IS_ZEBRA_DEBUG_EVENT)
608 zlog_debug(
609 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
096f7609 610 ifp->name, ifp->vrf->vrf_id, new_vrf_id);
d62a17ae 611
161e9ab7 612 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
613 /* Do not send unsolicited messages to synchronous clients. */
614 if (client->synchronous)
615 continue;
616
d62a17ae 617 /* Need to delete if the client is not interested in the new
618 * VRF. */
619 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
620 client->ifdel_cnt++;
621 zsend_interface_delete(client, ifp);
622 zsend_interface_vrf_update(client, ifp, new_vrf_id);
623 }
c8e264b6 624}
625
626/* Interface VRF change. This function is invoked *post* VRF change and sends an
627 * add to clients who are interested in the new VRF but not in the old VRF.
628 */
d62a17ae 629void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id)
c8e264b6 630{
d62a17ae 631 struct listnode *node, *nnode;
632 struct zserv *client;
633
634 if (IS_ZEBRA_DEBUG_EVENT)
635 zlog_debug(
636 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
096f7609 637 ifp->name, old_vrf_id, ifp->vrf->vrf_id);
d62a17ae 638
161e9ab7 639 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
640 /* Do not send unsolicited messages to synchronous clients. */
641 if (client->synchronous)
642 continue;
643
d62a17ae 644 /* Need to add if the client is interested in the new VRF. */
645 client->ifadd_cnt++;
646 zsend_interface_add(client, ifp);
647 zsend_interface_addresses(client, ifp);
648 }
c8e264b6 649}
650
fe257ae7
DS
651int zebra_add_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
652 struct route_entry *re, const char *rmap_name)
7a4bb9c5 653{
d62a17ae 654 struct route_entry *newre;
655 struct route_entry *same;
656 struct prefix p;
0eb97b86 657 struct nexthop_group *ng;
b68885f9 658 route_map_result_t ret = RMAP_PERMITMATCH;
f8c175f3 659 afi_t afi;
d62a17ae 660
f8c175f3 661 afi = family2afi(rn->p.family);
d62a17ae 662 if (rmap_name)
663 ret = zebra_import_table_route_map_check(
0eb97b86 664 afi, re->type, re->instance, &rn->p,
c415d895 665 re->nhe->nhg.nexthop,
fe257ae7 666 zvrf->vrf->vrf_id, re->tag, rmap_name);
d62a17ae 667
b68885f9 668 if (ret != RMAP_PERMITMATCH) {
85c615ac 669 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
fe257ae7 670 zebra_del_import_table_entry(zvrf, rn, re);
20796bc3
DS
671 return 0;
672 }
d62a17ae 673
f8c175f3 674 prefix_copy(&p, &rn->p);
d62a17ae 675
a2addae8
RW
676 RNODE_FOREACH_RE (rn, same) {
677 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
f8c175f3 678 continue;
20796bc3 679
996c9314 680 if (same->type == re->type && same->instance == re->instance
f8c175f3
DS
681 && same->table == re->table
682 && same->type != ZEBRA_ROUTE_CONNECT)
683 break;
684 }
20796bc3 685
e71c84ca
DS
686 if (same) {
687 UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED);
fe257ae7 688 zebra_del_import_table_entry(zvrf, rn, same);
e71c84ca 689 }
f8c175f3 690
996c9314 691 newre = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
5c4b6e57
DS
692 newre->type = ZEBRA_ROUTE_TABLE;
693 newre->distance = zebra_import_table_distance[afi][re->table];
694 newre->flags = re->flags;
695 newre->metric = re->metric;
696 newre->mtu = re->mtu;
317c6a10 697 newre->table = zvrf->table_id;
98572489 698 newre->uptime = monotime(NULL);
5c4b6e57 699 newre->instance = re->table;
5c4b6e57 700
0eb97b86 701 ng = nexthop_group_new();
c415d895 702 copy_nexthops(&ng->nexthop, re->nhe->nhg.nexthop, NULL);
0eb97b86
MS
703
704 rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre, ng);
5c4b6e57 705
d62a17ae 706 return 0;
7a4bb9c5
DS
707}
708
fe257ae7
DS
709int zebra_del_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
710 struct route_entry *re)
7a4bb9c5 711{
d62a17ae 712 struct prefix p;
f8c175f3 713 afi_t afi;
7a4bb9c5 714
f8c175f3
DS
715 afi = family2afi(rn->p.family);
716 prefix_copy(&p, &rn->p);
7a4bb9c5 717
fe257ae7 718 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_TABLE,
c415d895 719 re->table, re->flags, &p, NULL, re->nhe->nhg.nexthop,
0eb97b86 720 re->nhe_id, zvrf->table_id, re->metric, re->distance,
3ceae22b 721 false);
7a4bb9c5 722
d62a17ae 723 return 0;
7a4bb9c5
DS
724}
725
726/* Assuming no one calls this with the main routing table */
fe257ae7
DS
727int zebra_import_table(afi_t afi, vrf_id_t vrf_id, uint32_t table_id,
728 uint32_t distance, const char *rmap_name, int add)
7a4bb9c5 729{
d62a17ae 730 struct route_table *table;
731 struct route_entry *re;
732 struct route_node *rn;
fe257ae7 733 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf_id);
d62a17ae 734
735 if (!is_zebra_valid_kernel_table(table_id)
c447ad08 736 || (table_id == RT_TABLE_MAIN))
95f7965d 737 return -1;
d62a17ae 738
739 if (afi >= AFI_MAX)
95f7965d 740 return -1;
d62a17ae 741
c7c0b007
SW
742 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST, vrf_id,
743 table_id);
d62a17ae 744 if (table == NULL) {
745 return 0;
746 } else if (IS_ZEBRA_DEBUG_RIB) {
747 zlog_debug("%s routes from table %d",
748 add ? "Importing" : "Unimporting", table_id);
7a4bb9c5
DS
749 }
750
d62a17ae 751 if (add) {
752 if (rmap_name)
753 zebra_add_import_table_route_map(afi, rmap_name,
754 table_id);
755 else {
756 rmap_name =
757 zebra_get_import_table_route_map(afi, table_id);
3660beec 758 if (rmap_name) {
d62a17ae 759 zebra_del_import_table_route_map(afi, table_id);
3660beec
DS
760 rmap_name = NULL;
761 }
d62a17ae 762 }
7a4bb9c5 763
d62a17ae 764 zebra_import_table_used[afi][table_id] = 1;
765 zebra_import_table_distance[afi][table_id] = distance;
766 } else {
767 zebra_import_table_used[afi][table_id] = 0;
768 zebra_import_table_distance[afi][table_id] =
769 ZEBRA_TABLE_DISTANCE_DEFAULT;
770
771 rmap_name = zebra_get_import_table_route_map(afi, table_id);
3660beec 772 if (rmap_name) {
d62a17ae 773 zebra_del_import_table_route_map(afi, table_id);
3660beec
DS
774 rmap_name = NULL;
775 }
7a4bb9c5 776 }
7a4bb9c5 777
d62a17ae 778 for (rn = route_top(table); rn; rn = route_next(rn)) {
779 /* For each entry in the non-default routing table,
780 * add the entry in the main table
781 */
782 if (!rn->info)
783 continue;
784
a2addae8 785 RNODE_FOREACH_RE (rn, re) {
d62a17ae 786 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
787 continue;
788 break;
7a4bb9c5 789 }
8902474b 790
d62a17ae 791 if (!re)
792 continue;
8902474b 793
d62a17ae 794 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
795 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) {
796 if (add)
fe257ae7
DS
797 zebra_add_import_table_entry(zvrf, rn, re,
798 rmap_name);
d62a17ae 799 else
fe257ae7 800 zebra_del_import_table_entry(zvrf, rn, re);
d62a17ae 801 }
802 }
803 return 0;
804}
805
fe257ae7 806int zebra_import_table_config(struct vty *vty, vrf_id_t vrf_id)
d62a17ae 807{
808 int i;
809 afi_t afi;
810 int write = 0;
811 char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"};
812 const char *rmap_name;
813
814 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
815 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
fe257ae7 816 if (!is_zebra_import_table_enabled(afi, vrf_id, i))
20796bc3 817 continue;
d62a17ae 818
20796bc3
DS
819 if (zebra_import_table_distance[afi][i]
820 != ZEBRA_TABLE_DISTANCE_DEFAULT) {
996c9314 821 vty_out(vty, "%s import-table %d distance %d",
20796bc3
DS
822 afi_str[afi], i,
823 zebra_import_table_distance[afi][i]);
824 } else {
996c9314
LB
825 vty_out(vty, "%s import-table %d", afi_str[afi],
826 i);
d62a17ae 827 }
20796bc3
DS
828
829 rmap_name = zebra_get_import_table_route_map(afi, i);
830 if (rmap_name)
996c9314 831 vty_out(vty, " route-map %s", rmap_name);
20796bc3
DS
832
833 vty_out(vty, "\n");
834 write = 1;
d62a17ae 835 }
7a4bb9c5 836 }
7a4bb9c5 837
d62a17ae 838 return write;
7a4bb9c5 839}
8902474b 840
fe257ae7
DS
841static void zebra_import_table_rm_update_vrf_afi(struct zebra_vrf *zvrf,
842 afi_t afi, int table_id,
843 const char *rmap)
8902474b 844{
d62a17ae 845 struct route_table *table;
846 struct route_entry *re;
847 struct route_node *rn;
848 const char *rmap_name;
849
fe257ae7
DS
850 rmap_name = zebra_get_import_table_route_map(afi, table_id);
851 if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0))
852 return;
d62a17ae 853
c7c0b007
SW
854 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST,
855 zvrf->vrf->vrf_id, table_id);
fe257ae7
DS
856 if (!table) {
857 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
858 zlog_debug("%s: Table id=%d not found", __func__,
859 table_id);
860 return;
861 }
862
863 for (rn = route_top(table); rn; rn = route_next(rn)) {
864 /*
865 * For each entry in the non-default routing table,
866 * add the entry in the main table
867 */
868 if (!rn->info)
869 continue;
870
871 RNODE_FOREACH_RE (rn, re) {
872 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
d5b8c216 873 continue;
fe257ae7
DS
874 break;
875 }
876
877 if (!re)
878 continue;
879
880 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
881 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6)))
882 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
883 }
884
885 return;
886}
887
888static void zebra_import_table_rm_update_vrf(struct zebra_vrf *zvrf,
889 const char *rmap)
890{
891 afi_t afi;
892 int i;
893
894 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
895 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
896 if (!is_zebra_import_table_enabled(
897 afi, zvrf->vrf->vrf_id, i))
2c7ef20d 898 continue;
2c7ef20d 899
fe257ae7
DS
900 zebra_import_table_rm_update_vrf_afi(zvrf, afi, i,
901 rmap);
d62a17ae 902 }
8902474b 903 }
fe257ae7 904}
8902474b 905
fe257ae7
DS
906void zebra_import_table_rm_update(const char *rmap)
907{
908 struct vrf *vrf;
909 struct zebra_vrf *zvrf;
910
911 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
912 zvrf = vrf->info;
913
914 if (!zvrf)
915 continue;
916
917 zebra_import_table_rm_update_vrf(zvrf, rmap);
918 }
8902474b 919}
16f1b9ee
OD
920
921/* Interface parameters update */
d62a17ae 922void zebra_interface_parameters_update(struct interface *ifp)
16f1b9ee 923{
d62a17ae 924 struct listnode *node, *nnode;
925 struct zserv *client;
16f1b9ee 926
d62a17ae 927 if (IS_ZEBRA_DEBUG_EVENT)
096f7609
IR
928 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s vrf %s(%u)",
929 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
16f1b9ee 930
17da84a4
KS
931 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
932 /* Do not send unsolicited messages to synchronous clients. */
933 if (client->synchronous)
934 continue;
935
a8a20c4e 936 zsend_interface_link_params(client, ifp);
17da84a4 937 }
16f1b9ee 938}