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