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