]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_zebra.c
*: Convert `struct event_master` to `struct event_loop`
[mirror_frr.git] / ospfd / ospf_zebra.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/*
3 * Zebra connect library for OSPFd
4 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
718e3744 5 */
6
7#include <zebra.h>
8
cb37cb33 9#include "event.h"
718e3744 10#include "command.h"
11#include "network.h"
12#include "prefix.h"
13#include "routemap.h"
14#include "table.h"
15#include "stream.h"
16#include "memory.h"
17#include "zclient.h"
18#include "filter.h"
dd669bb0 19#include "plist.h"
718e3744 20#include "log.h"
5b5d66c4 21#include "route_opaque.h"
2376c3f2 22#include "lib/bfd.h"
5b30316e 23#include "nexthop.h"
718e3744 24
25#include "ospfd/ospfd.h"
26#include "ospfd/ospf_interface.h"
27#include "ospfd/ospf_ism.h"
28#include "ospfd/ospf_asbr.h"
29#include "ospfd/ospf_asbr.h"
30#include "ospfd/ospf_abr.h"
31#include "ospfd/ospf_lsa.h"
32#include "ospfd/ospf_dump.h"
33#include "ospfd/ospf_route.h"
d5a5c8f0
DS
34#include "ospfd/ospf_lsdb.h"
35#include "ospfd/ospf_neighbor.h"
36#include "ospfd/ospf_nsm.h"
718e3744 37#include "ospfd/ospf_zebra.h"
16f1b9ee 38#include "ospfd/ospf_te.h"
43e7abb5 39#include "ospfd/ospf_sr.h"
132a782e 40#include "ospfd/ospf_ldp_sync.h"
718e3744 41
bf8d3d6a
DL
42DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table");
43DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute");
dfac5d39 44
1febb13d 45
718e3744 46/* Zebra structure to hold current status. */
47struct zclient *zclient = NULL;
d81b8e0e
OD
48/* and for the Synchronous connection to the Label Manager */
49static struct zclient *zclient_sync;
718e3744 50
51/* For registering threads. */
cd9d0537 52extern struct event_loop *master;
18a6dce6 53
54/* Router-id update message from zebra. */
121f9dee 55static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
18a6dce6 56{
b5a8894d 57 struct ospf *ospf = NULL;
d62a17ae 58 struct prefix router_id;
59 zebra_router_id_update_read(zclient->ibuf, &router_id);
60
2dbe669b
DA
61 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
62 zlog_debug("Zebra rcvd: router id update %pFX vrf %s id %u",
63 &router_id, ospf_vrf_id_to_name(vrf_id), vrf_id);
d62a17ae 64
b5a8894d 65 ospf = ospf_lookup_by_vrf_id(vrf_id);
d62a17ae 66
6021c6c0
CS
67 if (ospf != NULL) {
68 ospf->router_id_zebra = router_id.u.prefix4;
d62a17ae 69 ospf_router_id_update(ospf);
6021c6c0 70 } else {
2dbe669b 71 if (IS_DEBUG_OSPF_EVENT)
996c9314 72 zlog_debug(
2dbe669b 73 "%s: ospf instance not found for vrf %s id %u router_id %pFX",
15569c58 74 __func__, ospf_vrf_id_to_name(vrf_id), vrf_id,
2dbe669b 75 &router_id);
b5a8894d 76 }
d62a17ae 77 return 0;
18a6dce6 78}
718e3744 79
121f9dee 80static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS)
718e3744 81{
d62a17ae 82 struct connected *c;
b5a8894d
CS
83 struct ospf *ospf = NULL;
84
718e3744 85
121f9dee 86 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
718e3744 87
d62a17ae 88 if (c == NULL)
89 return 0;
718e3744 90
2dbe669b
DA
91 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
92 zlog_debug("Zebra: interface %s address add %pFX vrf %s id %u",
93 c->ifp->name, c->address,
94 ospf_vrf_id_to_name(vrf_id), vrf_id);
7f643ebf 95
b5a8894d 96 ospf = ospf_lookup_by_vrf_id(vrf_id);
43b8d1d8
CS
97 if (!ospf)
98 return 0;
b5a8894d
CS
99
100 ospf_if_update(ospf, c->ifp);
718e3744 101
ef7bd2a3 102 ospf_if_interface(c->ifp);
718e3744 103
d62a17ae 104 return 0;
718e3744 105}
106
121f9dee 107static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)
718e3744 108{
d62a17ae 109 struct connected *c;
110 struct interface *ifp;
111 struct ospf_interface *oi;
112 struct route_node *rn;
113 struct prefix p;
114
121f9dee 115 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
d62a17ae 116
117 if (c == NULL)
118 return 0;
119
2dbe669b
DA
120 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
121 zlog_debug("Zebra: interface %s address delete %pFX",
122 c->ifp->name, c->address);
7f643ebf 123
d62a17ae 124 ifp = c->ifp;
125 p = *c->address;
936fbaef 126 p.prefixlen = IPV4_MAX_BITLEN;
718e3744 127
d62a17ae 128 rn = route_node_lookup(IF_OIFS(ifp), &p);
129 if (!rn) {
721c0857 130 connected_free(&c);
d62a17ae 131 return 0;
132 }
718e3744 133
d62a17ae 134 assert(rn->info);
135 oi = rn->info;
136 route_unlock_node(rn);
cf795c5d 137
d62a17ae 138 /* Call interface hook functions to clean up */
139 ospf_if_free(oi);
cf795c5d 140
ef7bd2a3 141 ospf_if_interface(c->ifp);
718e3744 142
721c0857 143 connected_free(&c);
718e3744 144
d62a17ae 145 return 0;
718e3744 146}
72357f2b 147
121f9dee 148static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS)
16f1b9ee 149{
d62a17ae 150 struct interface *ifp;
0b4124c1 151 bool changed = false;
16f1b9ee 152
0b4124c1 153 ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id, &changed);
16f1b9ee 154
0b4124c1 155 if (ifp == NULL || !changed)
d62a17ae 156 return 0;
16f1b9ee 157
d62a17ae 158 /* Update TE TLV */
159 ospf_mpls_te_update_if(ifp);
16f1b9ee 160
d62a17ae 161 return 0;
16f1b9ee
OD
162}
163
b5a8894d 164/* VRF update for an interface. */
121f9dee 165static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)
b5a8894d
CS
166{
167 struct interface *ifp = NULL;
168 vrf_id_t new_vrf_id;
16f1b9ee 169
b5a8894d 170 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
996c9314 171 &new_vrf_id);
b5a8894d
CS
172 if (!ifp)
173 return 0;
174
175 if (IS_DEBUG_OSPF_EVENT)
996c9314
LB
176 zlog_debug(
177 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
15569c58 178 __func__, ifp->name, vrf_id,
996c9314 179 ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
b5a8894d
CS
180
181 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
a36898e7 182 if_update_to_new_vrf(ifp, new_vrf_id);
b5a8894d 183
996c9314 184 return 0;
b5a8894d
CS
185}
186
7fd0729f
G
187/* Nexthop, ifindex, distance and metric information. */
188static void ospf_zebra_add_nexthop(struct ospf *ospf, struct ospf_path *path,
189 struct zapi_route *api)
190{
191 struct zapi_nexthop *api_nh;
192 struct zapi_nexthop *api_nh_backup;
193
194 /* TI-LFA backup path label stack comes first, if present */
195 if (path->srni.backup_label_stack) {
196 api_nh_backup = &api->backup_nexthops[api->backup_nexthop_num];
197 api_nh_backup->vrf_id = ospf->vrf_id;
198
f026b24e 199 api_nh_backup->type = NEXTHOP_TYPE_IPV4;
7fd0729f
G
200 api_nh_backup->gate.ipv4 = path->srni.backup_nexthop;
201
202 api_nh_backup->label_num =
203 path->srni.backup_label_stack->num_labels;
204 memcpy(api_nh_backup->labels,
205 path->srni.backup_label_stack->label,
206 sizeof(mpls_label_t) * api_nh_backup->label_num);
207
208 api->backup_nexthop_num++;
209 }
210
211 /* And here comes the primary nexthop */
212 api_nh = &api->nexthops[api->nexthop_num];
213#ifdef HAVE_NETLINK
214 if (path->unnumbered
215 || (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0)) {
216#else /* HAVE_NETLINK */
217 if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
218#endif /* HAVE_NETLINK */
219 api_nh->gate.ipv4 = path->nexthop;
220 api_nh->ifindex = path->ifindex;
221 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
222 } else if (path->nexthop.s_addr != INADDR_ANY) {
223 api_nh->gate.ipv4 = path->nexthop;
224 api_nh->type = NEXTHOP_TYPE_IPV4;
225 } else {
226 api_nh->ifindex = path->ifindex;
227 api_nh->type = NEXTHOP_TYPE_IFINDEX;
228 }
229 api_nh->vrf_id = ospf->vrf_id;
230
231 /* Set TI-LFA backup nexthop info if present */
232 if (path->srni.backup_label_stack) {
233 SET_FLAG(api->message, ZAPI_MESSAGE_BACKUP_NEXTHOPS);
234 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
235
236 /* Just care about a single TI-LFA backup path for now */
237 api_nh->backup_num = 1;
238 api_nh->backup_idx[0] = api->backup_nexthop_num - 1;
239 }
240
241 api->nexthop_num++;
242}
243
5b5d66c4
RW
244static void ospf_zebra_append_opaque_attr(struct ospf_route *or,
245 struct zapi_route *api)
246{
247 struct ospf_zebra_opaque ospf_opaque = {};
248
249 /* OSPF path type */
250 snprintf(ospf_opaque.path_type, sizeof(ospf_opaque.path_type), "%s",
251 ospf_path_type_name(or->path_type));
252
253 switch (or->path_type) {
254 case OSPF_PATH_INTRA_AREA:
255 case OSPF_PATH_INTER_AREA:
256 /* OSPF area ID */
257 (void)inet_ntop(AF_INET, &or->u.std.area_id,
258 ospf_opaque.area_id,
259 sizeof(ospf_opaque.area_id));
260 break;
261 case OSPF_PATH_TYPE1_EXTERNAL:
262 case OSPF_PATH_TYPE2_EXTERNAL:
263 /* OSPF route tag */
264 snprintf(ospf_opaque.tag, sizeof(ospf_opaque.tag), "%u",
265 or->u.ext.tag);
266 break;
267 default:
268 break;
269 }
270
271 SET_FLAG(api->message, ZAPI_MESSAGE_OPAQUE);
272 api->opaque.length = sizeof(struct ospf_zebra_opaque);
273 memcpy(api->opaque.data, &ospf_opaque, api->opaque.length);
274}
275
b5a8894d 276void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
996c9314 277 struct ospf_route * or)
718e3744 278{
5fef910e 279 struct zapi_route api;
d7c0a89a 280 uint8_t distance;
d62a17ae 281 struct ospf_path *path;
282 struct listnode *node;
d62a17ae 283
10514170
RW
284 if (ospf->gr_info.restart_in_progress) {
285 if (IS_DEBUG_OSPF_GR)
286 zlog_debug(
287 "Zebra: Graceful Restart in progress -- not installing %pFX",
288 p);
289 return;
290 }
291
5fef910e 292 memset(&api, 0, sizeof(api));
b5a8894d 293 api.vrf_id = ospf->vrf_id;
5fef910e
RW
294 api.type = ZEBRA_ROUTE_OSPF;
295 api.instance = ospf->instance;
296 api.safi = SAFI_UNICAST;
d00061ea 297
5fef910e
RW
298 memcpy(&api.prefix, p, sizeof(*p));
299 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
d00061ea 300
5fef910e
RW
301 /* Metric value. */
302 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
303 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
304 api.metric = or->cost + or->u.ext.type2_cost;
305 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
306 api.metric = or->u.ext.type2_cost;
307 else
308 api.metric = or->cost;
d00061ea
RW
309
310 /* Check if path type is ASE */
311 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
312 || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
5fef910e
RW
313 && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
314 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
315 api.tag = or->u.ext.tag;
316 }
d00061ea 317
5fef910e 318 /* Distance value. */
b5a8894d 319 distance = ospf_distance_apply(ospf, p, or);
5fef910e
RW
320 if (distance) {
321 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
322 api.distance = distance;
323 }
d00061ea 324
d00061ea 325 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
3d5b9855 326 if (api.nexthop_num >= ospf->max_multipath)
a74e593b 327 break;
7fd0729f
G
328
329 ospf_zebra_add_nexthop(ospf, path, &api);
72357f2b 330
d00061ea 331 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
96065dc3
CS
332 struct interface *ifp;
333
334 ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
4259ea81 335
d00061ea 336 zlog_debug(
2dbe669b
DA
337 "Zebra: Route add %pFX nexthop %pI4, ifindex=%d %s",
338 p, &path->nexthop, path->ifindex,
339 ifp ? ifp->name : " ");
d62a17ae 340 }
d00061ea 341 }
d62a17ae 342
5b5d66c4
RW
343 if (CHECK_FLAG(ospf->config, OSPF_SEND_EXTRA_DATA_TO_ZEBRA))
344 ospf_zebra_append_opaque_attr(or, &api);
345
5fef910e 346 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
718e3744 347}
348
b5a8894d 349void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
996c9314 350 struct ospf_route * or)
718e3744 351{
5fef910e 352 struct zapi_route api;
d62a17ae 353
10514170
RW
354 if (ospf->gr_info.restart_in_progress) {
355 if (IS_DEBUG_OSPF_GR)
356 zlog_debug(
357 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
358 p);
359 return;
360 }
361
5fef910e 362 memset(&api, 0, sizeof(api));
b5a8894d 363 api.vrf_id = ospf->vrf_id;
5fef910e
RW
364 api.type = ZEBRA_ROUTE_OSPF;
365 api.instance = ospf->instance;
366 api.safi = SAFI_UNICAST;
367 memcpy(&api.prefix, p, sizeof(*p));
d62a17ae 368
2dbe669b
DA
369 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
370 zlog_debug("Zebra: Route delete %pFX", p);
d00061ea 371
5fef910e 372 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
718e3744 373}
374
b5a8894d 375void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
718e3744 376{
5fef910e 377 struct zapi_route api;
d62a17ae 378
10514170
RW
379 if (ospf->gr_info.restart_in_progress) {
380 if (IS_DEBUG_OSPF_GR)
381 zlog_debug(
382 "Zebra: Graceful Restart in progress -- not installing %pFX",
383 p);
384 return;
385 }
386
5fef910e 387 memset(&api, 0, sizeof(api));
b5a8894d 388 api.vrf_id = ospf->vrf_id;
d00061ea
RW
389 api.type = ZEBRA_ROUTE_OSPF;
390 api.instance = ospf->instance;
d00061ea 391 api.safi = SAFI_UNICAST;
5fef910e 392 memcpy(&api.prefix, p, sizeof(*p));
09a484dd 393 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 394
5fef910e 395 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
d00061ea 396
2dbe669b
DA
397 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
398 zlog_debug("Zebra: Route add discard %pFX", p);
718e3744 399}
400
b5a8894d 401void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
718e3744 402{
5fef910e 403 struct zapi_route api;
d62a17ae 404
10514170
RW
405 if (ospf->gr_info.restart_in_progress) {
406 if (IS_DEBUG_OSPF_GR)
407 zlog_debug(
408 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
409 p);
410 return;
411 }
412
5fef910e 413 memset(&api, 0, sizeof(api));
b5a8894d 414 api.vrf_id = ospf->vrf_id;
d00061ea
RW
415 api.type = ZEBRA_ROUTE_OSPF;
416 api.instance = ospf->instance;
d00061ea 417 api.safi = SAFI_UNICAST;
5fef910e 418 memcpy(&api.prefix, p, sizeof(*p));
09a484dd 419 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 420
5fef910e 421 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
d00061ea 422
2dbe669b
DA
423 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
424 zlog_debug("Zebra: Route delete discard %pFX", p);
718e3744 425}
426
d7c0a89a
QY
427struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
428 unsigned short instance)
7c8ff89e 429{
d62a17ae 430 struct list *ext_list;
431 struct listnode *node;
432 struct ospf_external *ext;
7c8ff89e 433
de1ac5fd 434 ext_list = ospf->external[type];
d62a17ae 435 if (!ext_list)
436 return (NULL);
7c8ff89e 437
d62a17ae 438 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
439 if (ext->instance == instance)
440 return ext;
7c8ff89e 441
d62a17ae 442 return NULL;
7c8ff89e
DS
443}
444
d7c0a89a
QY
445struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
446 unsigned short instance)
7c8ff89e 447{
d62a17ae 448 struct list *ext_list;
449 struct ospf_external *ext;
7c8ff89e 450
de1ac5fd 451 ext = ospf_external_lookup(ospf, type, instance);
d62a17ae 452 if (ext)
453 return ext;
7c8ff89e 454
de1ac5fd
CS
455 if (!ospf->external[type])
456 ospf->external[type] = list_new();
7c8ff89e 457
de1ac5fd 458 ext_list = ospf->external[type];
9f5dc319 459 ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
d62a17ae 460 ext->instance = instance;
461 EXTERNAL_INFO(ext) = route_table_init();
7c8ff89e 462
d62a17ae 463 listnode_add(ext_list, ext);
7c8ff89e 464
d62a17ae 465 return ext;
7c8ff89e
DS
466}
467
1febb13d
S
468/*
469 * Walk all the ei received from zebra for a route type and apply
470 * default route-map.
471 */
472bool ospf_external_default_routemap_apply_walk(struct ospf *ospf,
473 struct list *ext_list,
474 struct external_info *default_ei)
475{
476 struct listnode *node;
477 struct ospf_external *ext;
478 struct route_node *rn;
479 struct external_info *ei = NULL;
480 int ret = 0;
481
482 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
483 if (!ext->external_info)
484 continue;
485
486 for (rn = route_top(ext->external_info); rn;
487 rn = route_next(rn)) {
488 ei = rn->info;
489 if (!ei)
490 continue;
491 ret = ospf_external_info_apply_default_routemap(
492 ospf, ei, default_ei);
493 if (ret)
494 break;
495 }
496 }
497
498 if (ret && ei) {
499 if (IS_DEBUG_OSPF_DEFAULT_INFO)
96b663a3
MS
500 zlog_debug("Default originate routemap permit ei: %pI4",
501 &ei->p.prefix);
1febb13d
S
502 return true;
503 }
504
505 return false;
506}
507
508/*
509 * Function to originate or flush default after applying
510 * route-map on all ei.
511 */
e6685141 512static void ospf_external_lsa_default_routemap_timer(struct event *thread)
1febb13d
S
513{
514 struct list *ext_list;
e16d030c 515 struct ospf *ospf = EVENT_ARG(thread);
1febb13d
S
516 struct prefix_ipv4 p;
517 int type;
518 int ret = 0;
519 struct ospf_lsa *lsa;
520 struct external_info *default_ei;
521
522 p.family = AF_INET;
523 p.prefixlen = 0;
524 p.prefix.s_addr = INADDR_ANY;
525
526 /* Get the default extenal info. */
527 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
528 ospf->instance, &p);
529 if (!default_ei) {
530 /* Nothing to be done here. */
531 if (IS_DEBUG_OSPF_DEFAULT_INFO)
532 zlog_debug("Default originate info not present");
cc9f21da 533 return;
1febb13d
S
534 }
535
536 /* For all the ei apply route-map */
537 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
538 ext_list = ospf->external[type];
539 if (!ext_list || type == ZEBRA_ROUTE_OSPF)
540 continue;
541
542 ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
543 default_ei);
544 if (ret)
545 break;
546 }
547
548 /* Get the default LSA. */
549 lsa = ospf_external_info_find_lsa(ospf, &p);
550
551 /* If permit then originate default. */
552 if (ret && !lsa)
553 ospf_external_lsa_originate(ospf, default_ei);
554 else if (ret && lsa && IS_LSA_MAXAGE(lsa))
63f0e941 555 ospf_external_lsa_refresh(ospf, lsa, default_ei, true, false);
1febb13d
S
556 else if (!ret && lsa)
557 ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &default_ei->p, 0);
1febb13d
S
558}
559
560
d7c0a89a 561void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
7c8ff89e 562{
d62a17ae 563 struct ospf_external *ext;
564
de1ac5fd 565 ext = ospf_external_lookup(ospf, type, instance);
d62a17ae 566
567 if (ext) {
568 if (EXTERNAL_INFO(ext))
569 route_table_finish(EXTERNAL_INFO(ext));
570
de1ac5fd
CS
571 listnode_delete(ospf->external[type], ext);
572
573 if (!ospf->external[type]->count)
6a154c88 574 list_delete(&ospf->external[type]);
de1ac5fd 575
7f586094 576 XFREE(MTYPE_OSPF_EXTERNAL, ext);
d62a17ae 577 }
1febb13d
S
578
579 /*
580 * Check if default needs to be flushed too.
581 */
907a2395
DS
582 event_add_event(master, ospf_external_lsa_default_routemap_timer, ospf,
583 0, &ospf->t_default_routemap_timer);
7c8ff89e
DS
584}
585
43e7abb5
OD
586/* Update NHLFE for Prefix SID */
587void ospf_zebra_update_prefix_sid(const struct sr_prefix *srp)
588{
589 struct zapi_labels zl;
590 struct zapi_nexthop *znh;
4ea5353b 591 struct zapi_nexthop *znh_backup;
43e7abb5
OD
592 struct listnode *node;
593 struct ospf_path *path;
594
43e7abb5
OD
595 /* Prepare message. */
596 memset(&zl, 0, sizeof(zl));
597 zl.type = ZEBRA_LSP_OSPF_SR;
598 zl.local_label = srp->label_in;
599
600 switch (srp->type) {
601 case LOCAL_SID:
602 /* Set Label for local Prefix */
603 znh = &zl.nexthops[zl.nexthop_num++];
604 znh->type = NEXTHOP_TYPE_IFINDEX;
605 znh->ifindex = srp->nhlfe.ifindex;
606 znh->label_num = 1;
607 znh->labels[0] = srp->nhlfe.label_out;
56981b40
OD
608
609 osr_debug("SR (%s): Configure Prefix %pFX with labels %u/%u",
610 __func__, (struct prefix *)&srp->prefv4,
611 srp->label_in, srp->nhlfe.label_out);
612
43e7abb5
OD
613 break;
614
615 case PREF_SID:
616 /* Update route in the RIB too. */
617 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
618 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
619 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
620 zl.route.prefix.family = srp->prefv4.family;
621 zl.route.type = ZEBRA_ROUTE_OSPF;
622 zl.route.instance = 0;
623
624 /* Check that SRP contains at least one valid path */
625 if (srp->route == NULL) {
626 return;
627 }
56981b40
OD
628
629 osr_debug("SR (%s): Configure Prefix %pFX with",
630 __func__, (struct prefix *)&srp->prefv4);
631
43e7abb5
OD
632 for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
633 if (path->srni.label_out == MPLS_INVALID_LABEL)
634 continue;
635
636 if (zl.nexthop_num >= MULTIPATH_NUM)
637 break;
638
f026b24e
G
639 /*
640 * TI-LFA backup path label stack comes first, if
641 * present.
642 */
4ea5353b
G
643 if (path->srni.backup_label_stack) {
644 znh_backup = &zl.backup_nexthops
645 [zl.backup_nexthop_num++];
f026b24e 646 znh_backup->type = NEXTHOP_TYPE_IPV4;
4ea5353b
G
647 znh_backup->gate.ipv4 =
648 path->srni.backup_nexthop;
649
4ea5353b
G
650 memcpy(znh_backup->labels,
651 path->srni.backup_label_stack->label,
652 sizeof(mpls_label_t)
f026b24e
G
653 * path->srni.backup_label_stack
654 ->num_labels);
655
656 znh_backup->label_num =
657 path->srni.backup_label_stack
658 ->num_labels;
659 if (path->srni.label_out
660 != MPLS_LABEL_IPV4_EXPLICIT_NULL
661 && path->srni.label_out
662 != MPLS_LABEL_IMPLICIT_NULL)
663 znh_backup->labels
664 [znh_backup->label_num++] =
665 path->srni.label_out;
4ea5353b
G
666 }
667
43e7abb5
OD
668 znh = &zl.nexthops[zl.nexthop_num++];
669 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
670 znh->gate.ipv4 = path->nexthop;
671 znh->ifindex = path->ifindex;
672 znh->label_num = 1;
673 znh->labels[0] = path->srni.label_out;
4ea5353b 674
56981b40 675 osr_debug(" |- labels %u/%u", srp->label_in,
270e66a2 676 path->srni.label_out);
56981b40 677
4ea5353b
G
678 /* Set TI-LFA backup nexthop info if present */
679 if (path->srni.backup_label_stack) {
680 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
681 SET_FLAG(znh->flags,
682 ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
683
684 /* Just care about a single TI-LFA backup path
685 * for now */
686 znh->backup_num = 1;
687 znh->backup_idx[0] = zl.backup_nexthop_num - 1;
688 }
43e7abb5
OD
689 }
690 break;
5d8c8d0b
DS
691 case ADJ_SID:
692 case LAN_ADJ_SID:
43e7abb5
OD
693 return;
694 }
695
696 /* Finally, send message to zebra. */
697 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_REPLACE, &zl);
698}
699
700/* Remove NHLFE for Prefix-SID */
701void ospf_zebra_delete_prefix_sid(const struct sr_prefix *srp)
702{
703 struct zapi_labels zl;
704
705 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__,
706 srp->label_in, (struct prefix *)&srp->prefv4);
707
708 /* Prepare message. */
709 memset(&zl, 0, sizeof(zl));
710 zl.type = ZEBRA_LSP_OSPF_SR;
711 zl.local_label = srp->label_in;
712
713 if (srp->type == PREF_SID) {
714 /* Update route in the RIB too */
715 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
716 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
717 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
718 zl.route.prefix.family = srp->prefv4.family;
719 zl.route.type = ZEBRA_ROUTE_OSPF;
720 zl.route.instance = 0;
721 }
722
723 /* Send message to zebra. */
724 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE, &zl);
725}
726
727/* Send MPLS Label entry to Zebra for installation or deletion */
728void ospf_zebra_send_adjacency_sid(int cmd, struct sr_nhlfe nhlfe)
729{
730 struct zapi_labels zl;
731 struct zapi_nexthop *znh;
732
733 osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__,
734 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
735 nhlfe.label_in, nhlfe.label_out, nhlfe.ifindex);
736
737 memset(&zl, 0, sizeof(zl));
738 zl.type = ZEBRA_LSP_OSPF_SR;
739 zl.local_label = nhlfe.label_in;
740 zl.nexthop_num = 1;
741 znh = &zl.nexthops[0];
742 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
743 znh->gate.ipv4 = nhlfe.nexthop;
744 znh->ifindex = nhlfe.ifindex;
745 znh->label_num = 1;
746 znh->labels[0] = nhlfe.label_out;
747
748 (void)zebra_send_mpls_labels(zclient, cmd, &zl);
749}
750
d7c0a89a
QY
751struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
752 unsigned short instance)
7c8ff89e 753{
d62a17ae 754 struct list *red_list;
755 struct listnode *node;
756 struct ospf_redist *red;
7c8ff89e 757
d62a17ae 758 red_list = ospf->redist[type];
759 if (!red_list)
760 return (NULL);
7c8ff89e 761
d62a17ae 762 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
763 if (red->instance == instance)
764 return red;
7c8ff89e 765
d62a17ae 766 return NULL;
7c8ff89e
DS
767}
768
d7c0a89a
QY
769struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
770 unsigned short instance)
7c8ff89e 771{
d62a17ae 772 struct list *red_list;
773 struct ospf_redist *red;
7c8ff89e 774
d62a17ae 775 red = ospf_redist_lookup(ospf, type, instance);
776 if (red)
777 return red;
7c8ff89e 778
d62a17ae 779 if (!ospf->redist[type])
780 ospf->redist[type] = list_new();
7c8ff89e 781
d62a17ae 782 red_list = ospf->redist[type];
9f5dc319 783 red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
d62a17ae 784 red->instance = instance;
785 red->dmetric.type = -1;
786 red->dmetric.value = -1;
1fb93326 787 ROUTEMAP_NAME(red) = NULL;
788 ROUTEMAP(red) = NULL;
7c8ff89e 789
d62a17ae 790 listnode_add(red_list, red);
7c8ff89e 791
d62a17ae 792 return red;
7c8ff89e
DS
793}
794
d7c0a89a 795void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
7c8ff89e 796{
d62a17ae 797 struct ospf_redist *red;
798
799 red = ospf_redist_lookup(ospf, type, instance);
800
801 if (red) {
802 listnode_delete(ospf->redist[type], red);
803 if (!ospf->redist[type]->count) {
6a154c88 804 list_delete(&ospf->redist[type]);
d62a17ae 805 }
97a69b31 806 ospf_routemap_unset(red);
7f586094 807 XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
d62a17ae 808 }
7c8ff89e
DS
809}
810
811
d7c0a89a
QY
812int ospf_is_type_redistributed(struct ospf *ospf, int type,
813 unsigned short instance)
718e3744 814{
d62a17ae 815 return (DEFAULT_ROUTE_TYPE(type)
49db7a7b 816 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
b5a8894d 817 ospf->vrf_id)
d62a17ae 818 : ((instance
819 && redist_check_instance(
b259f5af
DS
820 &zclient->mi_redist[AFI_IP][type],
821 instance))
d62a17ae 822 || (!instance
823 && vrf_bitmap_check(
b259f5af
DS
824 zclient->redist[AFI_IP][type],
825 ospf->vrf_id))));
718e3744 826}
827
7bced643
IR
828int ospf_redistribute_update(struct ospf *ospf, struct ospf_redist *red,
829 int type, unsigned short instance, int mtype,
830 int mvalue)
718e3744 831{
d62a17ae 832 int force = 0;
162dbe41 833
7bced643
IR
834 if (mtype != red->dmetric.type) {
835 red->dmetric.type = mtype;
836 force = LSA_REFRESH_FORCE;
837 }
838 if (mvalue != red->dmetric.value) {
839 red->dmetric.value = mvalue;
840 force = LSA_REFRESH_FORCE;
162dbe41 841 }
842
7bced643 843 ospf_external_lsa_refresh_type(ospf, type, instance, force);
d62a17ae 844
7bced643
IR
845 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
846 zlog_debug(
847 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
848 ospf_redist_string(type), instance,
849 metric_type(ospf, type, instance),
850 metric_value(ospf, type, instance));
d62a17ae 851
7bced643
IR
852 return CMD_SUCCESS;
853}
d62a17ae 854
7bced643
IR
855int ospf_redistribute_set(struct ospf *ospf, struct ospf_redist *red, int type,
856 unsigned short instance, int mtype, int mvalue)
857{
d62a17ae 858 red->dmetric.type = mtype;
859 red->dmetric.value = mvalue;
860
de1ac5fd 861 ospf_external_add(ospf, type, instance);
d62a17ae 862
863 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
b5a8894d 864 instance, ospf->vrf_id);
d62a17ae 865
866 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
996c9314
LB
867 zlog_debug(
868 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
869 ospf_redist_string(type), instance, ospf->vrf_id,
870 metric_type(ospf, type, instance),
871 metric_value(ospf, type, instance));
d62a17ae 872
873 ospf_asbr_status_update(ospf, ++ospf->redistribute);
874
875 return CMD_SUCCESS;
718e3744 876}
877
d7c0a89a
QY
878int ospf_redistribute_unset(struct ospf *ospf, int type,
879 unsigned short instance)
718e3744 880{
d62a17ae 881 if (type == zclient->redist_default && instance == zclient->instance)
882 return CMD_SUCCESS;
718e3744 883
d62a17ae 884 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
b5a8894d 885 instance, ospf->vrf_id);
cf795c5d 886
d62a17ae 887 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
b5a8894d
CS
888 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
889 ospf_redist_string(type), instance, ospf->vrf_id);
718e3744 890
d62a17ae 891 /* Remove the routes from OSPF table. */
892 ospf_redistribute_withdraw(ospf, type, instance);
7c8ff89e 893
de1ac5fd 894 ospf_external_del(ospf, type, instance);
718e3744 895
d62a17ae 896 ospf_asbr_status_update(ospf, --ospf->redistribute);
718e3744 897
d62a17ae 898 return CMD_SUCCESS;
718e3744 899}
900
d62a17ae 901int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
902 int mvalue)
718e3744 903{
1fb93326 904 struct prefix_ipv4 p;
905 struct in_addr nexthop;
906 int cur_originate = ospf->default_originate;
d5eac1e0 907 const char *type_str = NULL;
1fb93326 908
975a328e 909 nexthop.s_addr = INADDR_ANY;
1fb93326 910 p.family = AF_INET;
975a328e 911 p.prefix.s_addr = INADDR_ANY;
1fb93326 912 p.prefixlen = 0;
913
d62a17ae 914 ospf->default_originate = originate;
020709f9 915
d5eac1e0 916 if (cur_originate == originate) {
1fb93326 917 /* Refresh the lsa since metric might different */
d62a17ae 918 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
919 zlog_debug(
920 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
921 ospf_redist_string(DEFAULT_ROUTE),
922 metric_type(ospf, DEFAULT_ROUTE, 0),
923 metric_value(ospf, DEFAULT_ROUTE, 0));
718e3744 924
1fb93326 925 ospf_external_lsa_refresh_default(ospf);
d5eac1e0 926 return CMD_SUCCESS;
1fb93326 927 }
718e3744 928
d5eac1e0
DL
929 switch (cur_originate) {
930 case DEFAULT_ORIGINATE_NONE:
931 break;
932 case DEFAULT_ORIGINATE_ZEBRA:
1fb93326 933 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
49db7a7b 934 zclient, AFI_IP, ospf->vrf_id);
d5eac1e0
DL
935 ospf->redistribute--;
936 break;
937 case DEFAULT_ORIGINATE_ALWAYS:
938 ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
939 ospf_external_del(ospf, DEFAULT_ROUTE, 0);
940 ospf->redistribute--;
941 break;
1fb93326 942 }
718e3744 943
d5eac1e0
DL
944 switch (originate) {
945 case DEFAULT_ORIGINATE_NONE:
946 type_str = "none";
947 break;
948 case DEFAULT_ORIGINATE_ZEBRA:
949 type_str = "normal";
950 ospf->redistribute++;
951 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
952 zclient, AFI_IP, ospf->vrf_id);
953 break;
954 case DEFAULT_ORIGINATE_ALWAYS:
955 type_str = "always";
956 ospf->redistribute++;
957 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
958 ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop,
959 0);
960 break;
961 }
718e3744 962
d62a17ae 963 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
d5eac1e0
DL
964 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
965 type_str,
966 metric_type(ospf, DEFAULT_ROUTE, 0),
967 metric_value(ospf, DEFAULT_ROUTE, 0));
718e3744 968
d5eac1e0
DL
969 ospf_external_lsa_refresh_default(ospf);
970 ospf_asbr_status_update(ospf, ospf->redistribute);
d62a17ae 971 return CMD_SUCCESS;
718e3744 972}
973
d62a17ae 974static int ospf_external_lsa_originate_check(struct ospf *ospf,
975 struct external_info *ei)
718e3744 976{
d62a17ae 977 /* If prefix is multicast, then do not originate LSA. */
978 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
979 zlog_info(
96b663a3
MS
980 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
981 &ei->p.prefix);
d62a17ae 982 return 0;
983 }
984
985 /* Take care of default-originate. */
1fe59b44 986 if (is_default_prefix4(&ei->p))
d62a17ae 987 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
988 zlog_info(
3efd0893 989 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
d62a17ae 990 return 0;
991 }
992
993 return 1;
718e3744 994}
995
996/* If connected prefix is OSPF enable interface, then do not announce. */
d62a17ae 997int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
718e3744 998{
d62a17ae 999 struct listnode *node;
1000 struct ospf_interface *oi;
718e3744 1001
718e3744 1002
d62a17ae 1003 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1004 if (prefix_match(oi->address, (struct prefix *)&ei->p))
1005 return 0;
1006 return 1;
718e3744 1007}
1008
1febb13d
S
1009
1010/* Apply default route-map on ei received. */
1011int ospf_external_info_apply_default_routemap(struct ospf *ospf,
1012 struct external_info *ei,
1013 struct external_info *default_ei)
1014{
1015 struct ospf_redist *red;
1016 int type = default_ei->type;
1017 struct prefix_ipv4 *p = &ei->p;
1018 struct route_map_set_values save_values;
1019
1020
1021 if (!ospf_external_lsa_originate_check(ospf, default_ei))
1022 return 0;
1023
1024 save_values = default_ei->route_map_set;
1025 ospf_reset_route_map_set_values(&default_ei->route_map_set);
1026
1027 /* apply route-map if needed */
1028 red = ospf_redist_lookup(ospf, type, ospf->instance);
1029 if (red && ROUTEMAP_NAME(red)) {
1030 route_map_result_t ret;
1031
1782514f 1032 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
1febb13d
S
1033
1034 if (ret == RMAP_DENYMATCH) {
1035 ei->route_map_set = save_values;
1036 return 0;
1037 }
1038 }
1039
1040 return 1;
1041}
1042
1043
1044/*
1045 * Default originated is based on route-map condition then
1046 * apply route-map on received external info. Originate or
1047 * flush based on route-map condition.
1048 */
1049static bool ospf_external_lsa_default_routemap_apply(struct ospf *ospf,
1050 struct external_info *ei,
1051 int cmd)
1052{
1053 struct external_info *default_ei;
1054 struct prefix_ipv4 p;
1055 struct ospf_lsa *lsa;
1056 int ret;
1057
1058 p.family = AF_INET;
1059 p.prefixlen = 0;
1060 p.prefix.s_addr = INADDR_ANY;
1061
1062
1063 /* Get the default extenal info. */
1064 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
1065 ospf->instance, &p);
1066 if (!default_ei) {
1067 /* Nothing to be done here. */
1068 return false;
1069 }
1070
1071 if (IS_DEBUG_OSPF_DEFAULT_INFO)
96b663a3
MS
1072 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1073 &ei->p.prefix, cmd);
1febb13d
S
1074
1075 ret = ospf_external_info_apply_default_routemap(ospf, ei, default_ei);
1076
1077 /* If deny then nothing to be done both in add and del case. */
1078 if (!ret) {
1079 if (IS_DEBUG_OSPF_DEFAULT_INFO)
96b663a3
MS
1080 zlog_debug("Default originte routemap deny for ei: %pI4",
1081 &ei->p.prefix);
1febb13d
S
1082 return false;
1083 }
1084
1085 /* Get the default LSA. */
1086 lsa = ospf_external_info_find_lsa(ospf, &p);
1087
1088 /* If this is add route and permit then ooriginate default. */
1089 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1090 /* If permit and default already advertise then return. */
1091 if (lsa && !IS_LSA_MAXAGE(lsa)) {
1092 if (IS_DEBUG_OSPF_DEFAULT_INFO)
02c671af 1093 zlog_debug("Default lsa already originated");
1febb13d
S
1094 return true;
1095 }
1096
1097 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1098 zlog_debug("Originating/Refreshing default lsa");
1099
1100 if (lsa && IS_LSA_MAXAGE(lsa))
1101 /* Refresh lsa.*/
63f0e941 1102 ospf_external_lsa_refresh(ospf, lsa, default_ei, true,
1103 false);
1febb13d
S
1104 else
1105 /* If permit and default not advertised then advertise.
1106 */
1107 ospf_external_lsa_originate(ospf, default_ei);
1108
1109 } else if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) {
1110 /* If deny and lsa is not originated then nothing to be done.*/
1111 if (!lsa) {
1112 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1113 zlog_debug(
1114 "Default lsa not originated, not flushing");
1115 return true;
1116 }
1117
1118 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1119 zlog_debug(
96b663a3
MS
1120 "Running default route-map again as ei: %pI4 deleted",
1121 &ei->p.prefix);
1febb13d
S
1122 /*
1123 * if this route delete was permitted then we need to check
1124 * there are any other external info which can still trigger
1125 * default route origination else flush it.
1126 */
907a2395
DS
1127 event_add_event(master,
1128 ospf_external_lsa_default_routemap_timer, ospf,
1129 0, &ospf->t_default_routemap_timer);
1febb13d
S
1130 }
1131
1132 return true;
1133}
1134
718e3744 1135/* return 1 if external LSA must be originated, 0 otherwise */
d62a17ae 1136int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
1137 int *changed)
718e3744 1138{
d62a17ae 1139 struct route_map_set_values save_values;
1140 struct prefix_ipv4 *p = &ei->p;
1141 struct ospf_redist *red;
1fe59b44
RZ
1142 uint8_t type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
1143 unsigned short instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
87e6e11e 1144 route_tag_t saved_tag = 0;
d62a17ae 1145
1febb13d
S
1146 /* Default is handled differently. */
1147 if (type == DEFAULT_ROUTE)
1148 return 1;
1149
d62a17ae 1150 if (changed)
1151 *changed = 0;
1152
1153 if (!ospf_external_lsa_originate_check(ospf, ei))
1154 return 0;
1155
1156 /* Take care connected route. */
1157 if (type == ZEBRA_ROUTE_CONNECT
1158 && !ospf_distribute_check_connected(ospf, ei))
1159 return 0;
1160
1161 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
1162 /* distirbute-list exists, but access-list may not? */
1163 if (DISTRIBUTE_LIST(ospf, type))
1164 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
1165 == FILTER_DENY) {
2dbe669b 1166 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
d62a17ae 1167 zlog_debug(
2dbe669b
DA
1168 "Redistribute[%s]: %pFX filtered by distribute-list.",
1169 ospf_redist_string(type), p);
d62a17ae 1170 return 0;
1171 }
1172
1173 save_values = ei->route_map_set;
1174 ospf_reset_route_map_set_values(&ei->route_map_set);
04e94d39 1175
87e6e11e 1176 saved_tag = ei->tag;
04e94d39 1177 /* Resetting with original route tag */
1178 ei->tag = ei->orig_tag;
d62a17ae 1179
1180 /* apply route-map if needed */
1181 red = ospf_redist_lookup(ospf, type, instance);
1182 if (red && ROUTEMAP_NAME(red)) {
b68885f9 1183 route_map_result_t ret;
d62a17ae 1184
1782514f 1185 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
d62a17ae 1186
1187 if (ret == RMAP_DENYMATCH) {
1188 ei->route_map_set = save_values;
2dbe669b 1189 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
d62a17ae 1190 zlog_debug(
2dbe669b
DA
1191 "Redistribute[%s]: %pFX filtered by route-map.",
1192 ospf_redist_string(type), p);
d62a17ae 1193 return 0;
1194 }
1195
1196 /* check if 'route-map set' changed something */
87e6e11e 1197 if (changed) {
d62a17ae 1198 *changed = !ospf_route_map_set_compare(
1199 &ei->route_map_set, &save_values);
87e6e11e 1200
1201 /* check if tag is modified */
1202 *changed |= (saved_tag != ei->tag);
1203 }
d62a17ae 1204 }
1205
1206 return 1;
718e3744 1207}
1208
1209/* OSPF route-map set for redistribution */
d62a17ae 1210void ospf_routemap_set(struct ospf_redist *red, const char *name)
718e3744 1211{
93d836e6 1212 if (ROUTEMAP_NAME(red)) {
1213 route_map_counter_decrement(ROUTEMAP(red));
d62a17ae 1214 free(ROUTEMAP_NAME(red));
93d836e6 1215 }
718e3744 1216
d62a17ae 1217 ROUTEMAP_NAME(red) = strdup(name);
1218 ROUTEMAP(red) = route_map_lookup_by_name(name);
93d836e6 1219 route_map_counter_increment(ROUTEMAP(red));
718e3744 1220}
1221
d62a17ae 1222void ospf_routemap_unset(struct ospf_redist *red)
718e3744 1223{
93d836e6 1224 if (ROUTEMAP_NAME(red)) {
1225 route_map_counter_decrement(ROUTEMAP(red));
d62a17ae 1226 free(ROUTEMAP_NAME(red));
93d836e6 1227 }
718e3744 1228
d62a17ae 1229 ROUTEMAP_NAME(red) = NULL;
1230 ROUTEMAP(red) = NULL;
718e3744 1231}
1232
10514170
RW
1233static int ospf_zebra_gr_update(struct ospf *ospf, int command,
1234 uint32_t stale_time)
1235{
1236 struct zapi_cap api;
1237
1238 if (!zclient || zclient->sock < 0 || !ospf)
1239 return 1;
1240
6006b807 1241 memset(&api, 0, sizeof(api));
10514170
RW
1242 api.cap = command;
1243 api.stale_removal_time = stale_time;
1244 api.vrf_id = ospf->vrf_id;
1245
1246 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient,
1247 &api);
1248
1249 return 0;
1250}
1251
1252int ospf_zebra_gr_enable(struct ospf *ospf, uint32_t stale_time)
1253{
1254 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_CAPABILITIES,
1255 stale_time);
1256}
1257
1258int ospf_zebra_gr_disable(struct ospf *ospf)
1259{
1260 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_DISABLE, 0);
1261}
1262
718e3744 1263/* Zebra route add and delete treatment. */
121f9dee 1264static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
718e3744 1265{
74489921
RW
1266 struct zapi_route api;
1267 struct prefix_ipv4 p;
5e43330e 1268 struct prefix pgen;
d62a17ae 1269 unsigned long ifindex;
1270 struct in_addr nexthop;
d62a17ae 1271 struct external_info *ei;
1272 struct ospf *ospf;
1273 int i;
1fb93326 1274 uint8_t rt_type;
d62a17ae 1275
b5a8894d 1276 ospf = ospf_lookup_by_vrf_id(vrf_id);
74489921
RW
1277 if (ospf == NULL)
1278 return 0;
d62a17ae 1279
74489921
RW
1280 if (zapi_route_decode(zclient->ibuf, &api) < 0)
1281 return -1;
d62a17ae 1282
74489921
RW
1283 ifindex = api.nexthops[0].ifindex;
1284 nexthop = api.nexthops[0].gate.ipv4;
1fb93326 1285 rt_type = api.type;
d62a17ae 1286
74489921 1287 memcpy(&p, &api.prefix, sizeof(p));
d62a17ae 1288 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
1289 return 0;
1290
5e43330e
FD
1291 pgen.family = p.family;
1292 pgen.prefixlen = p.prefixlen;
1293 pgen.u.prefix4 = p.prefix;
1294
1fb93326 1295 /* Re-destributed route is default route.
1296 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1297 * updating ex-info. But in resetting (no default-info
1298 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1299 * Resolved this inconsistency by maintaining same route type.
1300 */
5e43330e 1301 if ((is_default_prefix(&pgen)) && (api.type != ZEBRA_ROUTE_OSPF))
1fb93326 1302 rt_type = DEFAULT_ROUTE;
1303
2dbe669b
DA
1304 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1305 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %pFX",
ae4080c5 1306 __func__, zserv_command_string(cmd),
2dbe669b 1307 zebra_route_string(api.type), vrf_id, &api.prefix);
5c780bc9 1308
121f9dee 1309 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
d62a17ae 1310 /* XXX|HACK|TODO|FIXME:
09a484dd
DL
1311 * Maybe we should ignore reject/blackhole routes? Testing
1312 * shows that there is no problems though and this is only way
1313 * to "summarize" routes in ASBR at the moment. Maybe we need
1314 * just a better generalised solution for these types?
d62a17ae 1315 */
1316
74489921 1317 /* Protocol tag overwrites all other tag value sent by zebra */
1fb93326 1318 if (ospf->dtag[rt_type] > 0)
1319 api.tag = ospf->dtag[rt_type];
d62a17ae 1320
1321 /*
1322 * Given zebra sends update for a prefix via ADD message, it
1323 * should
1324 * be considered as an implicit DEL for that prefix with other
1325 * source
1326 * types.
1327 */
1fb93326 1328 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
1329 if (i != rt_type)
996c9314
LB
1330 ospf_external_info_delete(ospf, i, api.instance,
1331 p);
d62a17ae 1332
1fb93326 1333 ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
de1ac5fd 1334 ifindex, nexthop, api.tag);
d62a17ae 1335 if (ei == NULL) {
1336 /* Nothing has changed, so nothing to do; return */
1337 return 0;
1338 }
975a328e 1339 if (ospf->router_id.s_addr != INADDR_ANY) {
1fe59b44 1340 if (is_default_prefix4(&p))
6ec9c6a8
DS
1341 ospf_external_lsa_refresh_default(ospf);
1342 else {
1343 struct ospf_external_aggr_rt *aggr;
1344 struct as_external_lsa *al;
1345 struct ospf_lsa *lsa = NULL;
1346 struct in_addr mask;
1347
1348 aggr = ospf_external_aggr_match(ospf, &ei->p);
1349
1350 if (aggr) {
1351 /* Check the AS-external-LSA
1352 * should be originated.
1353 */
1354 if (!ospf_redistribute_check(ospf, ei,
1355 NULL))
1356 return 0;
1357
1358 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
1359 zlog_debug(
1360 "%s: Send Aggreate LSA (%pI4/%d)",
1361 __func__,
1362 &aggr->p.prefix,
1363 aggr->p.prefixlen);
1364
1365 ospf_originate_summary_lsa(ospf, aggr,
1366 ei);
1367
1368 /* Handling the case where the
1369 * external route prefix
1370 * and aggegate prefix is same
1461559c 1371 * If same don't flush the
6ec9c6a8
DS
1372 * originated
1373 * external LSA.
1374 */
1375 if (prefix_same(
1376 (struct prefix *)&aggr->p,
1377 (struct prefix *)&ei->p))
1378 return 0;
1379
1380 lsa = ospf_external_info_find_lsa(
1381 ospf, &ei->p);
1382
1383 if (lsa) {
1384 al = (struct as_external_lsa *)
1385 lsa->data;
1386 masklen2ip(ei->p.prefixlen,
1387 &mask);
1388
1389 if (mask.s_addr
1390 != al->mask.s_addr)
1391 return 0;
960417cf 1392
6ec9c6a8
DS
1393 ospf_external_lsa_flush(
1394 ospf, ei->type, &ei->p,
1395 0);
1396 }
1397 } else {
1398 struct ospf_lsa *current;
1399
1400 current = ospf_external_info_find_lsa(
1401 ospf, &ei->p);
1402 if (!current) {
1403 /* Check the
1404 * AS-external-LSA
1405 * should be
1406 * originated.
960417cf 1407 */
1408 if (!ospf_redistribute_check(
1409 ospf, ei, NULL))
1410 return 0;
d62a17ae 1411
6ec9c6a8
DS
1412 ospf_external_lsa_originate(
1413 ospf, ei);
1414 } else {
d62a17ae 1415 if (IS_DEBUG_OSPF(
6ec9c6a8
DS
1416 zebra,
1417 ZEBRA_REDISTRIBUTE))
d62a17ae 1418 zlog_debug(
6ec9c6a8 1419 "%s: %pI4 refreshing LSA",
960417cf 1420 __func__,
6ec9c6a8
DS
1421 &p.prefix);
1422 ospf_external_lsa_refresh(
1423 ospf, current, ei,
1424 LSA_REFRESH_FORCE,
1425 false);
d62a17ae 1426 }
1427 }
1428 }
1429 }
1febb13d
S
1430
1431 /*
1432 * Check if default-information originate is
1433 * with some routemap prefix/access list match.
1434 */
1435 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1436
6ec9c6a8 1437 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
960417cf 1438 struct ospf_external_aggr_rt *aggr;
1439
1febb13d 1440 ei = ospf_external_info_lookup(ospf, rt_type, api.instance, &p);
960417cf 1441 if (ei == NULL)
1442 return 0;
6ec9c6a8
DS
1443
1444 /*
1445 * Check if default-information originate i
1446 * with some routemap prefix/access list match.
1447 * Apply before ei is deleted.
1448 */
1449 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1febb13d 1450
960417cf 1451 aggr = ospf_external_aggr_match(ospf, &ei->p);
1452
1453 if (aggr && (ei->aggr_route == aggr)) {
1454 ospf_unlink_ei_from_aggr(ospf, aggr, ei);
1455
1456 ospf_external_info_delete(ospf, rt_type, api.instance,
1457 p);
1458 } else {
1459 ospf_external_info_delete(ospf, rt_type, api.instance,
1460 p);
1461
1fe59b44 1462 if (is_default_prefix4(&p))
960417cf 1463 ospf_external_lsa_refresh_default(ospf);
1464 else
1465 ospf_external_lsa_flush(ospf, rt_type, &p,
1466 ifindex /*, nexthop */);
1467 }
5048fe14 1468 }
d62a17ae 1469
1470 return 0;
718e3744 1471}
6b0655a2 1472
cf795c5d 1473
d62a17ae 1474int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
718e3744 1475{
d62a17ae 1476 /* Lookup access-list for distribute-list. */
1477 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
718e3744 1478
d62a17ae 1479 /* Clear previous distribute-name. */
1480 if (DISTRIBUTE_NAME(ospf, type))
1481 free(DISTRIBUTE_NAME(ospf, type));
718e3744 1482
d62a17ae 1483 /* Set distribute-name. */
1484 DISTRIBUTE_NAME(ospf, type) = strdup(name);
718e3744 1485
d62a17ae 1486 /* If access-list have been set, schedule update timer. */
1487 if (DISTRIBUTE_LIST(ospf, type))
1488 ospf_distribute_list_update(ospf, type, 0);
718e3744 1489
d62a17ae 1490 return CMD_SUCCESS;
718e3744 1491}
1492
d62a17ae 1493int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1494 const char *name)
718e3744 1495{
d62a17ae 1496 /* Schedule update timer. */
1497 if (DISTRIBUTE_LIST(ospf, type))
1498 ospf_distribute_list_update(ospf, type, 0);
718e3744 1499
d62a17ae 1500 /* Unset distribute-list. */
1501 DISTRIBUTE_LIST(ospf, type) = NULL;
718e3744 1502
d62a17ae 1503 /* Clear distribute-name. */
1504 if (DISTRIBUTE_NAME(ospf, type))
1505 free(DISTRIBUTE_NAME(ospf, type));
cf795c5d 1506
d62a17ae 1507 DISTRIBUTE_NAME(ospf, type) = NULL;
718e3744 1508
d62a17ae 1509 return CMD_SUCCESS;
718e3744 1510}
1511
1512/* distribute-list update timer. */
e6685141 1513static void ospf_distribute_list_update_timer(struct event *thread)
718e3744 1514{
d62a17ae 1515 struct route_node *rn;
1516 struct external_info *ei;
1517 struct route_table *rt;
1518 struct ospf_lsa *lsa;
dcaed63d 1519 int type, default_refresh = 0;
e16d030c 1520 struct ospf *ospf = EVENT_ARG(thread);
d62a17ae 1521
d62a17ae 1522 if (ospf == NULL)
cc9f21da 1523 return;
d62a17ae 1524
1525 ospf->t_distribute_update = NULL;
1526
1527 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1528
b5a8894d 1529 if (IS_DEBUG_OSPF_EVENT) {
dcaed63d
IR
1530 zlog_debug("%s: ospf distribute-list update vrf %s id %d",
1531 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1532 ospf->vrf_id);
b5a8894d
CS
1533 }
1534
d62a17ae 1535 /* foreach all external info. */
1536 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1537 struct list *ext_list;
1538 struct listnode *node;
1539 struct ospf_external *ext;
1540
de1ac5fd 1541 ext_list = ospf->external[type];
d62a17ae 1542 if (!ext_list)
1543 continue;
1544
1545 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1546 rt = ext->external_info;
1547 if (!rt)
1548 continue;
6ec9c6a8
DS
1549 for (rn = route_top(rt); rn; rn = route_next(rn)) {
1550 ei = rn->info;
1551 if (!ei)
1552 continue;
1553
1fe59b44 1554 if (is_default_prefix4(&ei->p))
6ec9c6a8
DS
1555 default_refresh = 1;
1556 else {
1557 struct ospf_external_aggr_rt *aggr;
1558
1559 aggr = ospf_external_aggr_match(ospf,
1560 &ei->p);
1561 if (aggr) {
1562 /* Check the
1563 * AS-external-LSA
1564 * should be originated.
1565 */
1566 if (!ospf_redistribute_check(
1567 ospf, ei, NULL)) {
1568
1569 ospf_unlink_ei_from_aggr(
960417cf 1570 ospf, aggr, ei);
6ec9c6a8 1571 continue;
960417cf 1572 }
6ec9c6a8
DS
1573
1574 if (IS_DEBUG_OSPF(
1575 lsa,
1576 EXTNL_LSA_AGGR))
1577 zlog_debug(
1578 "%s: Send Aggregate LSA (%pI4/%d)",
1579 __func__,
1580 &aggr->p.prefix,
1581 aggr->p.prefixlen);
1582
1583 /* Originate Aggregate
1584 * LSA
1585 */
1586 ospf_originate_summary_lsa(
1587 ospf, aggr, ei);
1588 } else if (
1589 (lsa = ospf_external_info_find_lsa(
1590 ospf, &ei->p))) {
1591 int force =
1592 LSA_REFRESH_IF_CHANGED;
1593 /* If this is a MaxAge
1594 * LSA, we need to
1595 * force refresh it
1596 * because distribute
1597 * settings might have
1598 * changed and now,
1599 * this LSA needs to be
1600 * originated, not be
1601 * removed.
1602 * If we don't force
1603 * refresh it, it will
1604 * remain a MaxAge LSA
1605 * because it will look
1606 * like it hasn't
1607 * changed. Neighbors
1608 * will not receive
1609 * updates for this LSA.
1610 */
1611 if (IS_LSA_MAXAGE(lsa))
1612 force = LSA_REFRESH_FORCE;
1613
1614 ospf_external_lsa_refresh(
1615 ospf, lsa, ei, force,
1616 false);
1617 } else {
1618 if (!ospf_redistribute_check(
1619 ospf, ei, NULL))
1620 continue;
1621 ospf_external_lsa_originate(
1622 ospf, ei);
960417cf 1623 }
d62a17ae 1624 }
6ec9c6a8 1625 }
d62a17ae 1626 }
1627 }
1628 if (default_refresh)
1629 ospf_external_lsa_refresh_default(ospf);
718e3744 1630}
1631
718e3744 1632/* Update distribute-list and set timer to apply access-list. */
d7c0a89a
QY
1633void ospf_distribute_list_update(struct ospf *ospf, int type,
1634 unsigned short instance)
718e3744 1635{
d62a17ae 1636 struct ospf_external *ext;
b5a8894d 1637
d62a17ae 1638 /* External info does not exist. */
de1ac5fd 1639 ext = ospf_external_lookup(ospf, type, instance);
dcaed63d 1640 if (!ext || !EXTERNAL_INFO(ext))
d62a17ae 1641 return;
1642
dcaed63d 1643 /* Set timer. If timer is already started, this call does nothing. */
907a2395
DS
1644 event_add_timer_msec(master, ospf_distribute_list_update_timer, ospf,
1645 ospf->min_ls_interval, &ospf->t_distribute_update);
718e3744 1646}
1647
1648/* If access-list is updated, apply some check. */
d62a17ae 1649static void ospf_filter_update(struct access_list *access)
718e3744 1650{
d62a17ae 1651 struct ospf *ospf;
1652 int type;
1653 int abr_inv = 0;
1654 struct ospf_area *area;
b5a8894d 1655 struct listnode *node, *n1;
d62a17ae 1656
1657 /* If OSPF instance does not exist, return right now. */
b5a8894d 1658 if (listcount(om->ospf) == 0)
d62a17ae 1659 return;
1660
b5a8894d 1661 /* Iterate all ospf [VRF] instances */
43b8d1d8 1662 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
b5a8894d
CS
1663 /* Update distribute-list, and apply filter. */
1664 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1665 struct list *red_list;
b5a8894d
CS
1666 struct ospf_redist *red;
1667
1668 red_list = ospf->redist[type];
1669 if (red_list)
996c9314
LB
1670 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1671 red)) {
b5a8894d 1672 if (ROUTEMAP(red)) {
996c9314
LB
1673 /* if route-map is not NULL it
1674 * may be
b5a8894d
CS
1675 * using this access list */
1676 ospf_distribute_list_update(
996c9314
LB
1677 ospf, type,
1678 red->instance);
b5a8894d 1679 }
d62a17ae 1680 }
d62a17ae 1681
b5a8894d
CS
1682 /* There is place for route-map for default-information
1683 * (ZEBRA_ROUTE_MAX),
1684 * but no distribute list. */
1685 if (type == ZEBRA_ROUTE_MAX)
1686 break;
1687
1688 if (DISTRIBUTE_NAME(ospf, type)) {
1689 /* Keep old access-list for distribute-list. */
996c9314
LB
1690 struct access_list *old =
1691 DISTRIBUTE_LIST(ospf, type);
b5a8894d
CS
1692
1693 /* Update access-list for distribute-list. */
996c9314
LB
1694 DISTRIBUTE_LIST(ospf, type) =
1695 access_list_lookup(
1696 AFI_IP,
1697 DISTRIBUTE_NAME(ospf, type));
b5a8894d
CS
1698
1699 /* No update for this distribute type. */
996c9314
LB
1700 if (old == NULL
1701 && DISTRIBUTE_LIST(ospf, type) == NULL)
b5a8894d
CS
1702 continue;
1703
1704 /* Schedule distribute-list update timer. */
1705 if (DISTRIBUTE_LIST(ospf, type) == NULL
996c9314
LB
1706 || strcmp(DISTRIBUTE_NAME(ospf, type),
1707 access->name)
1708 == 0)
1709 ospf_distribute_list_update(ospf, type,
1710 0);
b5a8894d 1711 }
d62a17ae 1712 }
d62a17ae 1713
b5a8894d
CS
1714 /* Update Area access-list. */
1715 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1716 if (EXPORT_NAME(area)) {
1717 EXPORT_LIST(area) = NULL;
1718 abr_inv++;
1719 }
d62a17ae 1720
b5a8894d
CS
1721 if (IMPORT_NAME(area)) {
1722 IMPORT_LIST(area) = NULL;
1723 abr_inv++;
1724 }
d62a17ae 1725 }
d62a17ae 1726
b5a8894d
CS
1727 /* Schedule ABR tasks -- this will be changed -- takada. */
1728 if (IS_OSPF_ABR(ospf) && abr_inv)
1729 ospf_schedule_abr_task(ospf);
1730 }
718e3744 1731}
dd669bb0 1732
1733/* If prefix-list is updated, do some updates. */
2d4093ee 1734static void ospf_prefix_list_update(struct prefix_list *plist)
dd669bb0 1735{
b5a8894d 1736 struct ospf *ospf = NULL;
d62a17ae 1737 int type;
1738 int abr_inv = 0;
1739 struct ospf_area *area;
b5a8894d 1740 struct listnode *node, *n1;
d62a17ae 1741
1742 /* If OSPF instatnce does not exist, return right now. */
b5a8894d 1743 if (listcount(om->ospf) == 0)
d62a17ae 1744 return;
1745
b5a8894d
CS
1746 /* Iterate all ospf [VRF] instances */
1747 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1748
1749 /* Update all route-maps which are used
1750 * as redistribution filters.
1751 * They might use prefix-list.
1752 */
1753 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1754 struct list *red_list;
b5a8894d
CS
1755 struct ospf_redist *red;
1756
1757 red_list = ospf->redist[type];
6ec9c6a8
DS
1758 if (!red_list)
1759 continue;
1760
1761 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
1762 if (ROUTEMAP(red)) {
1763 /* if route-map is not NULL
1764 * it may be using
1765 * this prefix list */
1766 ospf_distribute_list_update(
1767 ospf, type, red->instance);
d62a17ae 1768 }
1769 }
b5a8894d 1770 }
d62a17ae 1771
b5a8894d
CS
1772 /* Update area filter-lists. */
1773 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1774 /* Update filter-list in. */
6ec9c6a8
DS
1775 if (PREFIX_NAME_IN(area)
1776 && strcmp(PREFIX_NAME_IN(area),
1777 prefix_list_name(plist))
1778 == 0) {
1779 PREFIX_LIST_IN(area) = prefix_list_lookup(
1780 AFI_IP, PREFIX_NAME_IN(area));
1781 abr_inv++;
1782 }
d62a17ae 1783
b5a8894d 1784 /* Update filter-list out. */
6ec9c6a8
DS
1785 if (PREFIX_NAME_OUT(area)
1786 && strcmp(PREFIX_NAME_OUT(area),
1787 prefix_list_name(plist))
1788 == 0) {
9629b44f 1789 PREFIX_LIST_OUT(area) = prefix_list_lookup(
6ec9c6a8
DS
1790 AFI_IP, PREFIX_NAME_OUT(area));
1791 abr_inv++;
1792 }
b5a8894d 1793 }
d62a17ae 1794
b5a8894d
CS
1795 /* Schedule ABR task. */
1796 if (IS_OSPF_ABR(ospf) && abr_inv)
1797 ospf_schedule_abr_task(ospf);
1798 }
dd669bb0 1799}
cf795c5d 1800
d62a17ae 1801static struct ospf_distance *ospf_distance_new(void)
718e3744 1802{
d62a17ae 1803 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
718e3744 1804}
1805
d62a17ae 1806static void ospf_distance_free(struct ospf_distance *odistance)
718e3744 1807{
d62a17ae 1808 XFREE(MTYPE_OSPF_DISTANCE, odistance);
718e3744 1809}
1810
d62a17ae 1811int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1812 const char *distance_str, const char *ip_str,
1813 const char *access_list_str)
718e3744 1814{
d62a17ae 1815 int ret;
1816 struct prefix_ipv4 p;
d7c0a89a 1817 uint8_t distance;
d62a17ae 1818 struct route_node *rn;
1819 struct ospf_distance *odistance;
1820
1821 ret = str2prefix_ipv4(ip_str, &p);
1822 if (ret == 0) {
1823 vty_out(vty, "Malformed prefix\n");
1824 return CMD_WARNING_CONFIG_FAILED;
1825 }
1826
1827 distance = atoi(distance_str);
1828
1829 /* Get OSPF distance node. */
1830 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1831 if (rn->info) {
1832 odistance = rn->info;
1833 route_unlock_node(rn);
1834 } else {
1835 odistance = ospf_distance_new();
1836 rn->info = odistance;
1837 }
1838
1839 /* Set distance value. */
1840 odistance->distance = distance;
1841
1842 /* Reset access-list configuration. */
1843 if (odistance->access_list) {
1844 free(odistance->access_list);
1845 odistance->access_list = NULL;
1846 }
1847 if (access_list_str)
1848 odistance->access_list = strdup(access_list_str);
1849
1850 return CMD_SUCCESS;
718e3744 1851}
1852
d62a17ae 1853int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1854 const char *distance_str, const char *ip_str,
1855 char const *access_list_str)
718e3744 1856{
d62a17ae 1857 int ret;
1858 struct prefix_ipv4 p;
1859 struct route_node *rn;
1860 struct ospf_distance *odistance;
1861
1862 ret = str2prefix_ipv4(ip_str, &p);
1863 if (ret == 0) {
1864 vty_out(vty, "Malformed prefix\n");
1865 return CMD_WARNING_CONFIG_FAILED;
1866 }
1867
1868 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1869 if (!rn) {
1870 vty_out(vty, "Can't find specified prefix\n");
1871 return CMD_WARNING_CONFIG_FAILED;
1872 }
1873
1874 odistance = rn->info;
1875
1876 if (odistance->access_list)
1877 free(odistance->access_list);
1878 ospf_distance_free(odistance);
1879
1880 rn->info = NULL;
1881 route_unlock_node(rn);
1882 route_unlock_node(rn);
1883
1884 return CMD_SUCCESS;
718e3744 1885}
1886
d62a17ae 1887void ospf_distance_reset(struct ospf *ospf)
718e3744 1888{
d62a17ae 1889 struct route_node *rn;
1890 struct ospf_distance *odistance;
1891
6ec9c6a8
DS
1892 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn)) {
1893 odistance = rn->info;
1894 if (!odistance)
1895 continue;
1896
1897 if (odistance->access_list)
1898 free(odistance->access_list);
1899 ospf_distance_free(odistance);
1900 rn->info = NULL;
1901 route_unlock_node(rn);
1902 }
718e3744 1903}
1904
d7c0a89a
QY
1905uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1906 struct ospf_route * or)
718e3744 1907{
718e3744 1908
d62a17ae 1909 if (ospf == NULL)
1910 return 0;
718e3744 1911
6ec9c6a8
DS
1912 if (ospf->distance_intra && or->path_type == OSPF_PATH_INTRA_AREA)
1913 return ospf->distance_intra;
718e3744 1914
6ec9c6a8
DS
1915 if (ospf->distance_inter && or->path_type == OSPF_PATH_INTER_AREA)
1916 return ospf->distance_inter;
718e3744 1917
6ec9c6a8
DS
1918 if (ospf->distance_external
1919 && (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1920 or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
1921 return ospf->distance_external;
cf795c5d 1922
d62a17ae 1923 if (ospf->distance_all)
1924 return ospf->distance_all;
718e3744 1925
d62a17ae 1926 return 0;
718e3744 1927}
1928
b5a8894d
CS
1929void ospf_zebra_vrf_register(struct ospf *ospf)
1930{
1931 if (!zclient || zclient->sock < 0 || !ospf)
1932 return;
1933
6021c6c0 1934 if (ospf->vrf_id != VRF_UNKNOWN) {
b5a8894d 1935 if (IS_DEBUG_OSPF_EVENT)
15569c58 1936 zlog_debug("%s: Register VRF %s id %u", __func__,
b5a8894d
CS
1937 ospf_vrf_id_to_name(ospf->vrf_id),
1938 ospf->vrf_id);
1939 zclient_send_reg_requests(zclient, ospf->vrf_id);
1940 }
1941}
1942
1943void ospf_zebra_vrf_deregister(struct ospf *ospf)
1944{
1945 if (!zclient || zclient->sock < 0 || !ospf)
1946 return;
1947
1948 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1949 if (IS_DEBUG_OSPF_EVENT)
6021c6c0 1950 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
15569c58 1951 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
b5a8894d
CS
1952 ospf->vrf_id);
1953 /* Deregister for router-id, interfaces,
1954 * redistributed routes. */
1955 zclient_send_dereg_requests(zclient, ospf->vrf_id);
1956 }
1957}
b61264a8 1958
d81b8e0e
OD
1959/* Label Manager Functions */
1960
1961/**
1962 * Check if Label Manager is Ready or not.
1963 *
1964 * @return True if Label Manager is ready, False otherwise
1965 */
1966bool ospf_zebra_label_manager_ready(void)
1967{
1968 return (zclient_sync->sock > 0);
1969}
1970
1971/**
1972 * Request Label Range to the Label Manager.
1973 *
1974 * @param base base label of the label range to request
1975 * @param chunk_size size of the label range to request
1976 *
1977 * @return 0 on success, -1 on failure
1978 */
1979int ospf_zebra_request_label_range(uint32_t base, uint32_t chunk_size)
1980{
1981 int ret;
1982 uint32_t start, end;
1983
1984 if (zclient_sync->sock < 0)
1985 return -1;
1986
1987 ret = lm_get_label_chunk(zclient_sync, 0, base, chunk_size, &start,
1988 &end);
1989 if (ret < 0) {
1990 zlog_warn("%s: error getting label range!", __func__);
1991 return -1;
1992 }
1993
1994 return 0;
1995}
1996
1997/**
1998 * Release Label Range to the Label Manager.
1999 *
2000 * @param start start of label range to release
2001 * @param end end of label range to release
2002 *
2003 * @return 0 on success, -1 otherwise
2004 */
2005int ospf_zebra_release_label_range(uint32_t start, uint32_t end)
2006{
2007 int ret;
2008
2009 if (zclient_sync->sock < 0)
2010 return -1;
2011
2012 ret = lm_release_label_chunk(zclient_sync, start, end);
2013 if (ret < 0) {
2014 zlog_warn("%s: error releasing label range!", __func__);
2015 return -1;
2016 }
2017
2018 return 0;
2019}
2020
2021/**
2022 * Connect to the Label Manager.
2023 *
2024 * @return 0 on success, -1 otherwise
2025 */
2026int ospf_zebra_label_manager_connect(void)
2027{
2028 /* Connect to label manager. */
2029 if (zclient_socket_connect(zclient_sync) < 0) {
2030 zlog_warn("%s: failed connecting synchronous zclient!",
2031 __func__);
2032 return -1;
2033 }
2034 /* make socket non-blocking */
2035 set_nonblocking(zclient_sync->sock);
2036
2037 /* Send hello to notify zebra this is a synchronous client */
7cfdb485 2038 if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) {
d81b8e0e
OD
2039 zlog_warn("%s: failed sending hello for synchronous zclient!",
2040 __func__);
2041 close(zclient_sync->sock);
2042 zclient_sync->sock = -1;
2043 return -1;
2044 }
2045
2046 /* Connect to label manager */
2047 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
2048 zlog_warn("%s: failed connecting to label manager!", __func__);
2049 if (zclient_sync->sock > 0) {
2050 close(zclient_sync->sock);
2051 zclient_sync->sock = -1;
2052 }
2053 return -1;
2054 }
2055
2056 osr_debug("SR (%s): Successfully connected to the Label Manager",
2057 __func__);
2058
2059 return 0;
2060}
2061
d62a17ae 2062static void ospf_zebra_connected(struct zclient *zclient)
7076bb2f 2063{
d62a17ae 2064 /* Send the client registration */
0945d5ed 2065 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2376c3f2 2066
d62a17ae 2067 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
2068}
2069
132a782e 2070/*
2071 * opaque messages between processes
2072 */
2073static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS)
2074{
2075 struct stream *s;
2076 struct zapi_opaque_msg info;
2077 struct ldp_igp_sync_if_state state;
2078 struct ldp_igp_sync_announce announce;
f173deb3 2079 struct zapi_opaque_reg_info dst;
132a782e 2080 int ret = 0;
2081
2082 s = zclient->ibuf;
2083
2084 if (zclient_opaque_decode(s, &info) != 0)
2085 return -1;
2086
2087 switch (info.type) {
f173deb3
OD
2088 case LINK_STATE_SYNC:
2089 STREAM_GETC(s, dst.proto);
2090 STREAM_GETW(s, dst.instance);
2091 STREAM_GETL(s, dst.session_id);
2092 dst.type = LINK_STATE_SYNC;
2093 ret = ospf_te_sync_ted(dst);
2094 break;
132a782e 2095 case LDP_IGP_SYNC_IF_STATE_UPDATE:
2096 STREAM_GET(&state, s, sizeof(state));
2097 ret = ospf_ldp_sync_state_update(state);
2098 break;
2099 case LDP_IGP_SYNC_ANNOUNCE_UPDATE:
2100 STREAM_GET(&announce, s, sizeof(announce));
2101 ret = ospf_ldp_sync_announce_update(announce);
2102 break;
132a782e 2103 default:
2104 break;
2105 }
2106
2107stream_failure:
2108
2109 return ret;
2110}
2111
cb135cc9
KS
2112static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS)
2113{
2114 int ret = 0;
2115
2116 struct zapi_client_close_info info;
2117
2118 if (zapi_client_close_notify_decode(zclient->ibuf, &info) < 0)
2119 return -1;
2120
2121 ospf_ldp_sync_handle_client_close(&info);
2122
2123 return ret;
2124}
2125
a243d1db
DL
2126static zclient_handler *const ospf_handlers[] = {
2127 [ZEBRA_ROUTER_ID_UPDATE] = ospf_router_id_update_zebra,
2128 [ZEBRA_INTERFACE_ADDRESS_ADD] = ospf_interface_address_add,
2129 [ZEBRA_INTERFACE_ADDRESS_DELETE] = ospf_interface_address_delete,
2130 [ZEBRA_INTERFACE_LINK_PARAMS] = ospf_interface_link_params,
2131 [ZEBRA_INTERFACE_VRF_UPDATE] = ospf_interface_vrf_update,
2132
2133 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ospf_zebra_read_route,
2134 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ospf_zebra_read_route,
2135
2136 [ZEBRA_OPAQUE_MESSAGE] = ospf_opaque_msg_handler,
2137
2138 [ZEBRA_CLIENT_CLOSE_NOTIFY] = ospf_zebra_client_close_notify,
2139};
2140
cd9d0537 2141void ospf_zebra_init(struct event_loop *master, unsigned short instance)
718e3744 2142{
d62a17ae 2143 /* Allocate zebra structure. */
a243d1db
DL
2144 zclient = zclient_new(master, &zclient_options_default, ospf_handlers,
2145 array_size(ospf_handlers));
342213ea 2146 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
d62a17ae 2147 zclient->zebra_connected = ospf_zebra_connected;
d62a17ae 2148
d81b8e0e
OD
2149 /* Initialize special zclient for synchronous message exchanges. */
2150 struct zclient_options options = zclient_options_default;
2151 options.synchronous = true;
a243d1db 2152 zclient_sync = zclient_new(master, &options, NULL, 0);
d81b8e0e
OD
2153 zclient_sync->sock = -1;
2154 zclient_sync->redist_default = ZEBRA_ROUTE_OSPF;
2155 zclient_sync->instance = instance;
2156 /*
2157 * session_id must be different from default value (0) to distinguish
2158 * the asynchronous socket from the synchronous one
2159 */
2160 zclient_sync->session_id = 1;
2161 zclient_sync->privs = &ospfd_privs;
2162
d62a17ae 2163 access_list_add_hook(ospf_filter_update);
2164 access_list_delete_hook(ospf_filter_update);
2165 prefix_list_add_hook(ospf_prefix_list_update);
2166 prefix_list_delete_hook(ospf_prefix_list_update);
718e3744 2167}
aa530b62
JU
2168
2169void ospf_zebra_send_arp(const struct interface *ifp, const struct prefix *p)
2170{
2171 zclient_send_neigh_discovery_req(zclient, ifp, p);
2172}