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