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