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