]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_zebra.c
*: Add camelCase JSON keys in addition to PascalCase
[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 int 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 0;
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 return 1;
575 }
576
577
578 void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
579 {
580 struct ospf_external *ext;
581
582 ext = ospf_external_lookup(ospf, type, instance);
583
584 if (ext) {
585 if (EXTERNAL_INFO(ext))
586 route_table_finish(EXTERNAL_INFO(ext));
587
588 listnode_delete(ospf->external[type], ext);
589
590 if (!ospf->external[type]->count)
591 list_delete(&ospf->external[type]);
592
593 XFREE(MTYPE_OSPF_EXTERNAL, ext);
594 }
595
596 /*
597 * Check if default needs to be flushed too.
598 */
599 thread_add_event(master, ospf_external_lsa_default_routemap_timer, ospf,
600 0, &ospf->t_default_routemap_timer);
601 }
602
603 /* Update NHLFE for Prefix SID */
604 void ospf_zebra_update_prefix_sid(const struct sr_prefix *srp)
605 {
606 struct zapi_labels zl;
607 struct zapi_nexthop *znh;
608 struct zapi_nexthop *znh_backup;
609 struct listnode *node;
610 struct ospf_path *path;
611
612 /* Prepare message. */
613 memset(&zl, 0, sizeof(zl));
614 zl.type = ZEBRA_LSP_OSPF_SR;
615 zl.local_label = srp->label_in;
616
617 switch (srp->type) {
618 case LOCAL_SID:
619 /* Set Label for local Prefix */
620 znh = &zl.nexthops[zl.nexthop_num++];
621 znh->type = NEXTHOP_TYPE_IFINDEX;
622 znh->ifindex = srp->nhlfe.ifindex;
623 znh->label_num = 1;
624 znh->labels[0] = srp->nhlfe.label_out;
625
626 osr_debug("SR (%s): Configure Prefix %pFX with labels %u/%u",
627 __func__, (struct prefix *)&srp->prefv4,
628 srp->label_in, srp->nhlfe.label_out);
629
630 break;
631
632 case PREF_SID:
633 /* Update route in the RIB too. */
634 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
635 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
636 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
637 zl.route.prefix.family = srp->prefv4.family;
638 zl.route.type = ZEBRA_ROUTE_OSPF;
639 zl.route.instance = 0;
640
641 /* Check that SRP contains at least one valid path */
642 if (srp->route == NULL) {
643 return;
644 }
645
646 osr_debug("SR (%s): Configure Prefix %pFX with",
647 __func__, (struct prefix *)&srp->prefv4);
648
649 for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
650 if (path->srni.label_out == MPLS_INVALID_LABEL)
651 continue;
652
653 if (zl.nexthop_num >= MULTIPATH_NUM)
654 break;
655
656 /*
657 * TI-LFA backup path label stack comes first, if
658 * present.
659 */
660 if (path->srni.backup_label_stack) {
661 znh_backup = &zl.backup_nexthops
662 [zl.backup_nexthop_num++];
663 znh_backup->type = NEXTHOP_TYPE_IPV4;
664 znh_backup->gate.ipv4 =
665 path->srni.backup_nexthop;
666
667 memcpy(znh_backup->labels,
668 path->srni.backup_label_stack->label,
669 sizeof(mpls_label_t)
670 * path->srni.backup_label_stack
671 ->num_labels);
672
673 znh_backup->label_num =
674 path->srni.backup_label_stack
675 ->num_labels;
676 if (path->srni.label_out
677 != MPLS_LABEL_IPV4_EXPLICIT_NULL
678 && path->srni.label_out
679 != MPLS_LABEL_IMPLICIT_NULL)
680 znh_backup->labels
681 [znh_backup->label_num++] =
682 path->srni.label_out;
683 }
684
685 znh = &zl.nexthops[zl.nexthop_num++];
686 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
687 znh->gate.ipv4 = path->nexthop;
688 znh->ifindex = path->ifindex;
689 znh->label_num = 1;
690 znh->labels[0] = path->srni.label_out;
691
692 osr_debug(" |- labels %u/%u", srp->label_in,
693 path->srni.label_out);
694
695 /* Set TI-LFA backup nexthop info if present */
696 if (path->srni.backup_label_stack) {
697 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
698 SET_FLAG(znh->flags,
699 ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
700
701 /* Just care about a single TI-LFA backup path
702 * for now */
703 znh->backup_num = 1;
704 znh->backup_idx[0] = zl.backup_nexthop_num - 1;
705 }
706 }
707 break;
708 case ADJ_SID:
709 case LAN_ADJ_SID:
710 return;
711 }
712
713 /* Finally, send message to zebra. */
714 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_REPLACE, &zl);
715 }
716
717 /* Remove NHLFE for Prefix-SID */
718 void ospf_zebra_delete_prefix_sid(const struct sr_prefix *srp)
719 {
720 struct zapi_labels zl;
721
722 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__,
723 srp->label_in, (struct prefix *)&srp->prefv4);
724
725 /* Prepare message. */
726 memset(&zl, 0, sizeof(zl));
727 zl.type = ZEBRA_LSP_OSPF_SR;
728 zl.local_label = srp->label_in;
729
730 if (srp->type == PREF_SID) {
731 /* Update route in the RIB too */
732 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
733 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
734 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
735 zl.route.prefix.family = srp->prefv4.family;
736 zl.route.type = ZEBRA_ROUTE_OSPF;
737 zl.route.instance = 0;
738 }
739
740 /* Send message to zebra. */
741 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE, &zl);
742 }
743
744 /* Send MPLS Label entry to Zebra for installation or deletion */
745 void ospf_zebra_send_adjacency_sid(int cmd, struct sr_nhlfe nhlfe)
746 {
747 struct zapi_labels zl;
748 struct zapi_nexthop *znh;
749
750 osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__,
751 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
752 nhlfe.label_in, nhlfe.label_out, nhlfe.ifindex);
753
754 memset(&zl, 0, sizeof(zl));
755 zl.type = ZEBRA_LSP_OSPF_SR;
756 zl.local_label = nhlfe.label_in;
757 zl.nexthop_num = 1;
758 znh = &zl.nexthops[0];
759 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
760 znh->gate.ipv4 = nhlfe.nexthop;
761 znh->ifindex = nhlfe.ifindex;
762 znh->label_num = 1;
763 znh->labels[0] = nhlfe.label_out;
764
765 (void)zebra_send_mpls_labels(zclient, cmd, &zl);
766 }
767
768 struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
769 unsigned short instance)
770 {
771 struct list *red_list;
772 struct listnode *node;
773 struct ospf_redist *red;
774
775 red_list = ospf->redist[type];
776 if (!red_list)
777 return (NULL);
778
779 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
780 if (red->instance == instance)
781 return red;
782
783 return NULL;
784 }
785
786 struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
787 unsigned short instance)
788 {
789 struct list *red_list;
790 struct ospf_redist *red;
791
792 red = ospf_redist_lookup(ospf, type, instance);
793 if (red)
794 return red;
795
796 if (!ospf->redist[type])
797 ospf->redist[type] = list_new();
798
799 red_list = ospf->redist[type];
800 red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
801 red->instance = instance;
802 red->dmetric.type = -1;
803 red->dmetric.value = -1;
804 ROUTEMAP_NAME(red) = NULL;
805 ROUTEMAP(red) = NULL;
806
807 listnode_add(red_list, red);
808
809 return red;
810 }
811
812 void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
813 {
814 struct ospf_redist *red;
815
816 red = ospf_redist_lookup(ospf, type, instance);
817
818 if (red) {
819 listnode_delete(ospf->redist[type], red);
820 if (!ospf->redist[type]->count) {
821 list_delete(&ospf->redist[type]);
822 }
823 ospf_routemap_unset(red);
824 XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
825 }
826 }
827
828
829 int ospf_is_type_redistributed(struct ospf *ospf, int type,
830 unsigned short instance)
831 {
832 return (DEFAULT_ROUTE_TYPE(type)
833 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
834 ospf->vrf_id)
835 : ((instance
836 && redist_check_instance(
837 &zclient->mi_redist[AFI_IP][type],
838 instance))
839 || (!instance
840 && vrf_bitmap_check(
841 zclient->redist[AFI_IP][type],
842 ospf->vrf_id))));
843 }
844
845 int ospf_redistribute_update(struct ospf *ospf, struct ospf_redist *red,
846 int type, unsigned short instance, int mtype,
847 int mvalue)
848 {
849 int force = 0;
850
851 if (mtype != red->dmetric.type) {
852 red->dmetric.type = mtype;
853 force = LSA_REFRESH_FORCE;
854 }
855 if (mvalue != red->dmetric.value) {
856 red->dmetric.value = mvalue;
857 force = LSA_REFRESH_FORCE;
858 }
859
860 ospf_external_lsa_refresh_type(ospf, type, instance, force);
861
862 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
863 zlog_debug(
864 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
865 ospf_redist_string(type), instance,
866 metric_type(ospf, type, instance),
867 metric_value(ospf, type, instance));
868
869 return CMD_SUCCESS;
870 }
871
872 int ospf_redistribute_set(struct ospf *ospf, struct ospf_redist *red, int type,
873 unsigned short instance, int mtype, int mvalue)
874 {
875 red->dmetric.type = mtype;
876 red->dmetric.value = mvalue;
877
878 ospf_external_add(ospf, type, instance);
879
880 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
881 instance, ospf->vrf_id);
882
883 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
884 zlog_debug(
885 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
886 ospf_redist_string(type), instance, ospf->vrf_id,
887 metric_type(ospf, type, instance),
888 metric_value(ospf, type, instance));
889
890 ospf_asbr_status_update(ospf, ++ospf->redistribute);
891
892 return CMD_SUCCESS;
893 }
894
895 int ospf_redistribute_unset(struct ospf *ospf, int type,
896 unsigned short instance)
897 {
898 if (type == zclient->redist_default && instance == zclient->instance)
899 return CMD_SUCCESS;
900
901 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
902 instance, ospf->vrf_id);
903
904 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
905 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
906 ospf_redist_string(type), instance, ospf->vrf_id);
907
908 /* Remove the routes from OSPF table. */
909 ospf_redistribute_withdraw(ospf, type, instance);
910
911 ospf_external_del(ospf, type, instance);
912
913 ospf_asbr_status_update(ospf, --ospf->redistribute);
914
915 return CMD_SUCCESS;
916 }
917
918 int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
919 int mvalue)
920 {
921 struct prefix_ipv4 p;
922 struct in_addr nexthop;
923 int cur_originate = ospf->default_originate;
924 const char *type_str = NULL;
925
926 nexthop.s_addr = INADDR_ANY;
927 p.family = AF_INET;
928 p.prefix.s_addr = INADDR_ANY;
929 p.prefixlen = 0;
930
931 ospf->default_originate = originate;
932
933 if (cur_originate == originate) {
934 /* Refresh the lsa since metric might different */
935 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
936 zlog_debug(
937 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
938 ospf_redist_string(DEFAULT_ROUTE),
939 metric_type(ospf, DEFAULT_ROUTE, 0),
940 metric_value(ospf, DEFAULT_ROUTE, 0));
941
942 ospf_external_lsa_refresh_default(ospf);
943 return CMD_SUCCESS;
944 }
945
946 switch (cur_originate) {
947 case DEFAULT_ORIGINATE_NONE:
948 break;
949 case DEFAULT_ORIGINATE_ZEBRA:
950 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
951 zclient, AFI_IP, ospf->vrf_id);
952 ospf->redistribute--;
953 break;
954 case DEFAULT_ORIGINATE_ALWAYS:
955 ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
956 ospf_external_del(ospf, DEFAULT_ROUTE, 0);
957 ospf->redistribute--;
958 break;
959 }
960
961 switch (originate) {
962 case DEFAULT_ORIGINATE_NONE:
963 type_str = "none";
964 break;
965 case DEFAULT_ORIGINATE_ZEBRA:
966 type_str = "normal";
967 ospf->redistribute++;
968 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
969 zclient, AFI_IP, ospf->vrf_id);
970 break;
971 case DEFAULT_ORIGINATE_ALWAYS:
972 type_str = "always";
973 ospf->redistribute++;
974 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
975 ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop,
976 0);
977 break;
978 }
979
980 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
981 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
982 type_str,
983 metric_type(ospf, DEFAULT_ROUTE, 0),
984 metric_value(ospf, DEFAULT_ROUTE, 0));
985
986 ospf_external_lsa_refresh_default(ospf);
987 ospf_asbr_status_update(ospf, ospf->redistribute);
988 return CMD_SUCCESS;
989 }
990
991 static int ospf_external_lsa_originate_check(struct ospf *ospf,
992 struct external_info *ei)
993 {
994 /* If prefix is multicast, then do not originate LSA. */
995 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
996 zlog_info(
997 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
998 &ei->p.prefix);
999 return 0;
1000 }
1001
1002 /* Take care of default-originate. */
1003 if (is_default_prefix4(&ei->p))
1004 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
1005 zlog_info(
1006 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
1007 return 0;
1008 }
1009
1010 return 1;
1011 }
1012
1013 /* If connected prefix is OSPF enable interface, then do not announce. */
1014 int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
1015 {
1016 struct listnode *node;
1017 struct ospf_interface *oi;
1018
1019
1020 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1021 if (prefix_match(oi->address, (struct prefix *)&ei->p))
1022 return 0;
1023 return 1;
1024 }
1025
1026
1027 /* Apply default route-map on ei received. */
1028 int ospf_external_info_apply_default_routemap(struct ospf *ospf,
1029 struct external_info *ei,
1030 struct external_info *default_ei)
1031 {
1032 struct ospf_redist *red;
1033 int type = default_ei->type;
1034 struct prefix_ipv4 *p = &ei->p;
1035 struct route_map_set_values save_values;
1036
1037
1038 if (!ospf_external_lsa_originate_check(ospf, default_ei))
1039 return 0;
1040
1041 save_values = default_ei->route_map_set;
1042 ospf_reset_route_map_set_values(&default_ei->route_map_set);
1043
1044 /* apply route-map if needed */
1045 red = ospf_redist_lookup(ospf, type, ospf->instance);
1046 if (red && ROUTEMAP_NAME(red)) {
1047 route_map_result_t ret;
1048
1049 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
1050
1051 if (ret == RMAP_DENYMATCH) {
1052 ei->route_map_set = save_values;
1053 return 0;
1054 }
1055 }
1056
1057 return 1;
1058 }
1059
1060
1061 /*
1062 * Default originated is based on route-map condition then
1063 * apply route-map on received external info. Originate or
1064 * flush based on route-map condition.
1065 */
1066 static bool ospf_external_lsa_default_routemap_apply(struct ospf *ospf,
1067 struct external_info *ei,
1068 int cmd)
1069 {
1070 struct external_info *default_ei;
1071 struct prefix_ipv4 p;
1072 struct ospf_lsa *lsa;
1073 int ret;
1074
1075 p.family = AF_INET;
1076 p.prefixlen = 0;
1077 p.prefix.s_addr = INADDR_ANY;
1078
1079
1080 /* Get the default extenal info. */
1081 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
1082 ospf->instance, &p);
1083 if (!default_ei) {
1084 /* Nothing to be done here. */
1085 return false;
1086 }
1087
1088 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1089 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1090 &ei->p.prefix, cmd);
1091
1092 ret = ospf_external_info_apply_default_routemap(ospf, ei, default_ei);
1093
1094 /* If deny then nothing to be done both in add and del case. */
1095 if (!ret) {
1096 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1097 zlog_debug("Default originte routemap deny for ei: %pI4",
1098 &ei->p.prefix);
1099 return false;
1100 }
1101
1102 /* Get the default LSA. */
1103 lsa = ospf_external_info_find_lsa(ospf, &p);
1104
1105 /* If this is add route and permit then ooriginate default. */
1106 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1107 /* If permit and default already advertise then return. */
1108 if (lsa && !IS_LSA_MAXAGE(lsa)) {
1109 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1110 zlog_debug("Default lsa already originated");
1111 return true;
1112 }
1113
1114 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1115 zlog_debug("Originating/Refreshing default lsa");
1116
1117 if (lsa && IS_LSA_MAXAGE(lsa))
1118 /* Refresh lsa.*/
1119 ospf_external_lsa_refresh(ospf, lsa, default_ei, true,
1120 false);
1121 else
1122 /* If permit and default not advertised then advertise.
1123 */
1124 ospf_external_lsa_originate(ospf, default_ei);
1125
1126 } else if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) {
1127 /* If deny and lsa is not originated then nothing to be done.*/
1128 if (!lsa) {
1129 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1130 zlog_debug(
1131 "Default lsa not originated, not flushing");
1132 return true;
1133 }
1134
1135 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1136 zlog_debug(
1137 "Running default route-map again as ei: %pI4 deleted",
1138 &ei->p.prefix);
1139 /*
1140 * if this route delete was permitted then we need to check
1141 * there are any other external info which can still trigger
1142 * default route origination else flush it.
1143 */
1144 thread_add_event(master,
1145 ospf_external_lsa_default_routemap_timer, ospf,
1146 0, &ospf->t_default_routemap_timer);
1147 }
1148
1149 return true;
1150 }
1151
1152 /* return 1 if external LSA must be originated, 0 otherwise */
1153 int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
1154 int *changed)
1155 {
1156 struct route_map_set_values save_values;
1157 struct prefix_ipv4 *p = &ei->p;
1158 struct ospf_redist *red;
1159 uint8_t type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
1160 unsigned short instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
1161 route_tag_t saved_tag = 0;
1162
1163 /* Default is handled differently. */
1164 if (type == DEFAULT_ROUTE)
1165 return 1;
1166
1167 if (changed)
1168 *changed = 0;
1169
1170 if (!ospf_external_lsa_originate_check(ospf, ei))
1171 return 0;
1172
1173 /* Take care connected route. */
1174 if (type == ZEBRA_ROUTE_CONNECT
1175 && !ospf_distribute_check_connected(ospf, ei))
1176 return 0;
1177
1178 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
1179 /* distirbute-list exists, but access-list may not? */
1180 if (DISTRIBUTE_LIST(ospf, type))
1181 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
1182 == FILTER_DENY) {
1183 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1184 zlog_debug(
1185 "Redistribute[%s]: %pFX filtered by distribute-list.",
1186 ospf_redist_string(type), p);
1187 return 0;
1188 }
1189
1190 save_values = ei->route_map_set;
1191 ospf_reset_route_map_set_values(&ei->route_map_set);
1192
1193 saved_tag = ei->tag;
1194 /* Resetting with original route tag */
1195 ei->tag = ei->orig_tag;
1196
1197 /* apply route-map if needed */
1198 red = ospf_redist_lookup(ospf, type, instance);
1199 if (red && ROUTEMAP_NAME(red)) {
1200 route_map_result_t ret;
1201
1202 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
1203
1204 if (ret == RMAP_DENYMATCH) {
1205 ei->route_map_set = save_values;
1206 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1207 zlog_debug(
1208 "Redistribute[%s]: %pFX filtered by route-map.",
1209 ospf_redist_string(type), p);
1210 return 0;
1211 }
1212
1213 /* check if 'route-map set' changed something */
1214 if (changed) {
1215 *changed = !ospf_route_map_set_compare(
1216 &ei->route_map_set, &save_values);
1217
1218 /* check if tag is modified */
1219 *changed |= (saved_tag != ei->tag);
1220 }
1221 }
1222
1223 return 1;
1224 }
1225
1226 /* OSPF route-map set for redistribution */
1227 void ospf_routemap_set(struct ospf_redist *red, const char *name)
1228 {
1229 if (ROUTEMAP_NAME(red)) {
1230 route_map_counter_decrement(ROUTEMAP(red));
1231 free(ROUTEMAP_NAME(red));
1232 }
1233
1234 ROUTEMAP_NAME(red) = strdup(name);
1235 ROUTEMAP(red) = route_map_lookup_by_name(name);
1236 route_map_counter_increment(ROUTEMAP(red));
1237 }
1238
1239 void ospf_routemap_unset(struct ospf_redist *red)
1240 {
1241 if (ROUTEMAP_NAME(red)) {
1242 route_map_counter_decrement(ROUTEMAP(red));
1243 free(ROUTEMAP_NAME(red));
1244 }
1245
1246 ROUTEMAP_NAME(red) = NULL;
1247 ROUTEMAP(red) = NULL;
1248 }
1249
1250 static int ospf_zebra_gr_update(struct ospf *ospf, int command,
1251 uint32_t stale_time)
1252 {
1253 struct zapi_cap api;
1254
1255 if (!zclient || zclient->sock < 0 || !ospf)
1256 return 1;
1257
1258 memset(&api, 0, sizeof(struct zapi_cap));
1259 api.cap = command;
1260 api.stale_removal_time = stale_time;
1261 api.vrf_id = ospf->vrf_id;
1262
1263 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient,
1264 &api);
1265
1266 return 0;
1267 }
1268
1269 int ospf_zebra_gr_enable(struct ospf *ospf, uint32_t stale_time)
1270 {
1271 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_CAPABILITIES,
1272 stale_time);
1273 }
1274
1275 int ospf_zebra_gr_disable(struct ospf *ospf)
1276 {
1277 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_DISABLE, 0);
1278 }
1279
1280 /* Zebra route add and delete treatment. */
1281 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
1282 {
1283 struct zapi_route api;
1284 struct prefix_ipv4 p;
1285 unsigned long ifindex;
1286 struct in_addr nexthop;
1287 struct external_info *ei;
1288 struct ospf *ospf;
1289 int i;
1290 uint8_t rt_type;
1291
1292 ospf = ospf_lookup_by_vrf_id(vrf_id);
1293 if (ospf == NULL)
1294 return 0;
1295
1296 if (zapi_route_decode(zclient->ibuf, &api) < 0)
1297 return -1;
1298
1299 ifindex = api.nexthops[0].ifindex;
1300 nexthop = api.nexthops[0].gate.ipv4;
1301 rt_type = api.type;
1302
1303 memcpy(&p, &api.prefix, sizeof(p));
1304 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
1305 return 0;
1306
1307 /* Re-destributed route is default route.
1308 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1309 * updating ex-info. But in resetting (no default-info
1310 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1311 * Resolved this inconsistency by maintaining same route type.
1312 */
1313 if (is_default_prefix4(&p))
1314 rt_type = DEFAULT_ROUTE;
1315
1316 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1317 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %pFX",
1318 __func__, zserv_command_string(cmd),
1319 zebra_route_string(api.type), vrf_id, &api.prefix);
1320
1321 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1322 /* XXX|HACK|TODO|FIXME:
1323 * Maybe we should ignore reject/blackhole routes? Testing
1324 * shows that there is no problems though and this is only way
1325 * to "summarize" routes in ASBR at the moment. Maybe we need
1326 * just a better generalised solution for these types?
1327 */
1328
1329 /* Protocol tag overwrites all other tag value sent by zebra */
1330 if (ospf->dtag[rt_type] > 0)
1331 api.tag = ospf->dtag[rt_type];
1332
1333 /*
1334 * Given zebra sends update for a prefix via ADD message, it
1335 * should
1336 * be considered as an implicit DEL for that prefix with other
1337 * source
1338 * types.
1339 */
1340 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
1341 if (i != rt_type)
1342 ospf_external_info_delete(ospf, i, api.instance,
1343 p);
1344
1345 ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
1346 ifindex, nexthop, api.tag);
1347 if (ei == NULL) {
1348 /* Nothing has changed, so nothing to do; return */
1349 return 0;
1350 }
1351 if (ospf->router_id.s_addr != INADDR_ANY) {
1352 if (is_default_prefix4(&p))
1353 ospf_external_lsa_refresh_default(ospf);
1354 else {
1355 struct ospf_external_aggr_rt *aggr;
1356 struct as_external_lsa *al;
1357 struct ospf_lsa *lsa = NULL;
1358 struct in_addr mask;
1359
1360 aggr = ospf_external_aggr_match(ospf, &ei->p);
1361
1362 if (aggr) {
1363 /* Check the AS-external-LSA
1364 * should be originated.
1365 */
1366 if (!ospf_redistribute_check(ospf, ei,
1367 NULL))
1368 return 0;
1369
1370 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
1371 zlog_debug(
1372 "%s: Send Aggreate LSA (%pI4/%d)",
1373 __func__,
1374 &aggr->p.prefix,
1375 aggr->p.prefixlen);
1376
1377 ospf_originate_summary_lsa(ospf, aggr,
1378 ei);
1379
1380 /* Handling the case where the
1381 * external route prefix
1382 * and aggegate prefix is same
1383 * If same dont flush the
1384 * originated
1385 * external LSA.
1386 */
1387 if (prefix_same(
1388 (struct prefix *)&aggr->p,
1389 (struct prefix *)&ei->p))
1390 return 0;
1391
1392 lsa = ospf_external_info_find_lsa(
1393 ospf, &ei->p);
1394
1395 if (lsa) {
1396 al = (struct as_external_lsa *)
1397 lsa->data;
1398 masklen2ip(ei->p.prefixlen,
1399 &mask);
1400
1401 if (mask.s_addr
1402 != al->mask.s_addr)
1403 return 0;
1404
1405 ospf_external_lsa_flush(
1406 ospf, ei->type, &ei->p,
1407 0);
1408 }
1409 } else {
1410 struct ospf_lsa *current;
1411
1412 current = ospf_external_info_find_lsa(
1413 ospf, &ei->p);
1414 if (!current) {
1415 /* Check the
1416 * AS-external-LSA
1417 * should be
1418 * originated.
1419 */
1420 if (!ospf_redistribute_check(
1421 ospf, ei, NULL))
1422 return 0;
1423
1424 ospf_external_lsa_originate(
1425 ospf, ei);
1426 } else {
1427 if (IS_DEBUG_OSPF(
1428 zebra,
1429 ZEBRA_REDISTRIBUTE))
1430 zlog_debug(
1431 "%s: %pI4 refreshing LSA",
1432 __func__,
1433 &p.prefix);
1434 ospf_external_lsa_refresh(
1435 ospf, current, ei,
1436 LSA_REFRESH_FORCE,
1437 false);
1438 }
1439 }
1440 }
1441 }
1442
1443 /*
1444 * Check if default-information originate is
1445 * with some routemap prefix/access list match.
1446 */
1447 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1448
1449 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1450 struct ospf_external_aggr_rt *aggr;
1451
1452 ei = ospf_external_info_lookup(ospf, rt_type, api.instance, &p);
1453 if (ei == NULL)
1454 return 0;
1455
1456 /*
1457 * Check if default-information originate i
1458 * with some routemap prefix/access list match.
1459 * Apply before ei is deleted.
1460 */
1461 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1462
1463 aggr = ospf_external_aggr_match(ospf, &ei->p);
1464
1465 if (aggr && (ei->aggr_route == aggr)) {
1466 ospf_unlink_ei_from_aggr(ospf, aggr, ei);
1467
1468 ospf_external_info_delete(ospf, rt_type, api.instance,
1469 p);
1470 } else {
1471 ospf_external_info_delete(ospf, rt_type, api.instance,
1472 p);
1473
1474 if (is_default_prefix4(&p))
1475 ospf_external_lsa_refresh_default(ospf);
1476 else
1477 ospf_external_lsa_flush(ospf, rt_type, &p,
1478 ifindex /*, nexthop */);
1479 }
1480 }
1481
1482 return 0;
1483 }
1484
1485
1486 int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
1487 {
1488 /* Lookup access-list for distribute-list. */
1489 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
1490
1491 /* Clear previous distribute-name. */
1492 if (DISTRIBUTE_NAME(ospf, type))
1493 free(DISTRIBUTE_NAME(ospf, type));
1494
1495 /* Set distribute-name. */
1496 DISTRIBUTE_NAME(ospf, type) = strdup(name);
1497
1498 /* If access-list have been set, schedule update timer. */
1499 if (DISTRIBUTE_LIST(ospf, type))
1500 ospf_distribute_list_update(ospf, type, 0);
1501
1502 return CMD_SUCCESS;
1503 }
1504
1505 int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1506 const char *name)
1507 {
1508 /* Schedule update timer. */
1509 if (DISTRIBUTE_LIST(ospf, type))
1510 ospf_distribute_list_update(ospf, type, 0);
1511
1512 /* Unset distribute-list. */
1513 DISTRIBUTE_LIST(ospf, type) = NULL;
1514
1515 /* Clear distribute-name. */
1516 if (DISTRIBUTE_NAME(ospf, type))
1517 free(DISTRIBUTE_NAME(ospf, type));
1518
1519 DISTRIBUTE_NAME(ospf, type) = NULL;
1520
1521 return CMD_SUCCESS;
1522 }
1523
1524 /* distribute-list update timer. */
1525 static int ospf_distribute_list_update_timer(struct thread *thread)
1526 {
1527 struct route_node *rn;
1528 struct external_info *ei;
1529 struct route_table *rt;
1530 struct ospf_lsa *lsa;
1531 int type, default_refresh = 0;
1532 struct ospf *ospf = THREAD_ARG(thread);
1533
1534 if (ospf == NULL)
1535 return 0;
1536
1537 ospf->t_distribute_update = NULL;
1538
1539 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1540
1541 if (IS_DEBUG_OSPF_EVENT) {
1542 zlog_debug("%s: ospf distribute-list update vrf %s id %d",
1543 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1544 ospf->vrf_id);
1545 }
1546
1547 /* foreach all external info. */
1548 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1549 struct list *ext_list;
1550 struct listnode *node;
1551 struct ospf_external *ext;
1552
1553 ext_list = ospf->external[type];
1554 if (!ext_list)
1555 continue;
1556
1557 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1558 rt = ext->external_info;
1559 if (!rt)
1560 continue;
1561 for (rn = route_top(rt); rn; rn = route_next(rn)) {
1562 ei = rn->info;
1563 if (!ei)
1564 continue;
1565
1566 if (is_default_prefix4(&ei->p))
1567 default_refresh = 1;
1568 else {
1569 struct ospf_external_aggr_rt *aggr;
1570
1571 aggr = ospf_external_aggr_match(ospf,
1572 &ei->p);
1573 if (aggr) {
1574 /* Check the
1575 * AS-external-LSA
1576 * should be originated.
1577 */
1578 if (!ospf_redistribute_check(
1579 ospf, ei, NULL)) {
1580
1581 ospf_unlink_ei_from_aggr(
1582 ospf, aggr, ei);
1583 continue;
1584 }
1585
1586 if (IS_DEBUG_OSPF(
1587 lsa,
1588 EXTNL_LSA_AGGR))
1589 zlog_debug(
1590 "%s: Send Aggregate LSA (%pI4/%d)",
1591 __func__,
1592 &aggr->p.prefix,
1593 aggr->p.prefixlen);
1594
1595 /* Originate Aggregate
1596 * LSA
1597 */
1598 ospf_originate_summary_lsa(
1599 ospf, aggr, ei);
1600 } else if (
1601 (lsa = ospf_external_info_find_lsa(
1602 ospf, &ei->p))) {
1603 int force =
1604 LSA_REFRESH_IF_CHANGED;
1605 /* If this is a MaxAge
1606 * LSA, we need to
1607 * force refresh it
1608 * because distribute
1609 * settings might have
1610 * changed and now,
1611 * this LSA needs to be
1612 * originated, not be
1613 * removed.
1614 * If we don't force
1615 * refresh it, it will
1616 * remain a MaxAge LSA
1617 * because it will look
1618 * like it hasn't
1619 * changed. Neighbors
1620 * will not receive
1621 * updates for this LSA.
1622 */
1623 if (IS_LSA_MAXAGE(lsa))
1624 force = LSA_REFRESH_FORCE;
1625
1626 ospf_external_lsa_refresh(
1627 ospf, lsa, ei, force,
1628 false);
1629 } else {
1630 if (!ospf_redistribute_check(
1631 ospf, ei, NULL))
1632 continue;
1633 ospf_external_lsa_originate(
1634 ospf, ei);
1635 }
1636 }
1637 }
1638 }
1639 }
1640 if (default_refresh)
1641 ospf_external_lsa_refresh_default(ospf);
1642
1643 return 0;
1644 }
1645
1646 /* Update distribute-list and set timer to apply access-list. */
1647 void ospf_distribute_list_update(struct ospf *ospf, int type,
1648 unsigned short instance)
1649 {
1650 struct ospf_external *ext;
1651
1652 /* External info does not exist. */
1653 ext = ospf_external_lookup(ospf, type, instance);
1654 if (!ext || !EXTERNAL_INFO(ext))
1655 return;
1656
1657 /* Set timer. If timer is already started, this call does nothing. */
1658 thread_add_timer_msec(master, ospf_distribute_list_update_timer, ospf,
1659 ospf->min_ls_interval,
1660 &ospf->t_distribute_update);
1661 }
1662
1663 /* If access-list is updated, apply some check. */
1664 static void ospf_filter_update(struct access_list *access)
1665 {
1666 struct ospf *ospf;
1667 int type;
1668 int abr_inv = 0;
1669 struct ospf_area *area;
1670 struct listnode *node, *n1;
1671
1672 /* If OSPF instance does not exist, return right now. */
1673 if (listcount(om->ospf) == 0)
1674 return;
1675
1676 /* Iterate all ospf [VRF] instances */
1677 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1678 /* Update distribute-list, and apply filter. */
1679 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1680 struct list *red_list;
1681 struct ospf_redist *red;
1682
1683 red_list = ospf->redist[type];
1684 if (red_list)
1685 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1686 red)) {
1687 if (ROUTEMAP(red)) {
1688 /* if route-map is not NULL it
1689 * may be
1690 * using this access list */
1691 ospf_distribute_list_update(
1692 ospf, type,
1693 red->instance);
1694 }
1695 }
1696
1697 /* There is place for route-map for default-information
1698 * (ZEBRA_ROUTE_MAX),
1699 * but no distribute list. */
1700 if (type == ZEBRA_ROUTE_MAX)
1701 break;
1702
1703 if (DISTRIBUTE_NAME(ospf, type)) {
1704 /* Keep old access-list for distribute-list. */
1705 struct access_list *old =
1706 DISTRIBUTE_LIST(ospf, type);
1707
1708 /* Update access-list for distribute-list. */
1709 DISTRIBUTE_LIST(ospf, type) =
1710 access_list_lookup(
1711 AFI_IP,
1712 DISTRIBUTE_NAME(ospf, type));
1713
1714 /* No update for this distribute type. */
1715 if (old == NULL
1716 && DISTRIBUTE_LIST(ospf, type) == NULL)
1717 continue;
1718
1719 /* Schedule distribute-list update timer. */
1720 if (DISTRIBUTE_LIST(ospf, type) == NULL
1721 || strcmp(DISTRIBUTE_NAME(ospf, type),
1722 access->name)
1723 == 0)
1724 ospf_distribute_list_update(ospf, type,
1725 0);
1726 }
1727 }
1728
1729 /* Update Area access-list. */
1730 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1731 if (EXPORT_NAME(area)) {
1732 EXPORT_LIST(area) = NULL;
1733 abr_inv++;
1734 }
1735
1736 if (IMPORT_NAME(area)) {
1737 IMPORT_LIST(area) = NULL;
1738 abr_inv++;
1739 }
1740 }
1741
1742 /* Schedule ABR tasks -- this will be changed -- takada. */
1743 if (IS_OSPF_ABR(ospf) && abr_inv)
1744 ospf_schedule_abr_task(ospf);
1745 }
1746 }
1747
1748 /* If prefix-list is updated, do some updates. */
1749 void ospf_prefix_list_update(struct prefix_list *plist)
1750 {
1751 struct ospf *ospf = NULL;
1752 int type;
1753 int abr_inv = 0;
1754 struct ospf_area *area;
1755 struct listnode *node, *n1;
1756
1757 /* If OSPF instatnce does not exist, return right now. */
1758 if (listcount(om->ospf) == 0)
1759 return;
1760
1761 /* Iterate all ospf [VRF] instances */
1762 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1763
1764 /* Update all route-maps which are used
1765 * as redistribution filters.
1766 * They might use prefix-list.
1767 */
1768 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1769 struct list *red_list;
1770 struct ospf_redist *red;
1771
1772 red_list = ospf->redist[type];
1773 if (!red_list)
1774 continue;
1775
1776 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
1777 if (ROUTEMAP(red)) {
1778 /* if route-map is not NULL
1779 * it may be using
1780 * this prefix list */
1781 ospf_distribute_list_update(
1782 ospf, type, red->instance);
1783 }
1784 }
1785 }
1786
1787 /* Update area filter-lists. */
1788 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1789 /* Update filter-list in. */
1790 if (PREFIX_NAME_IN(area)
1791 && strcmp(PREFIX_NAME_IN(area),
1792 prefix_list_name(plist))
1793 == 0) {
1794 PREFIX_LIST_IN(area) = prefix_list_lookup(
1795 AFI_IP, PREFIX_NAME_IN(area));
1796 abr_inv++;
1797 }
1798
1799 /* Update filter-list out. */
1800 if (PREFIX_NAME_OUT(area)
1801 && strcmp(PREFIX_NAME_OUT(area),
1802 prefix_list_name(plist))
1803 == 0) {
1804 PREFIX_LIST_IN(area) = prefix_list_lookup(
1805 AFI_IP, PREFIX_NAME_OUT(area));
1806 abr_inv++;
1807 }
1808 }
1809
1810 /* Schedule ABR task. */
1811 if (IS_OSPF_ABR(ospf) && abr_inv)
1812 ospf_schedule_abr_task(ospf);
1813 }
1814 }
1815
1816 static struct ospf_distance *ospf_distance_new(void)
1817 {
1818 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
1819 }
1820
1821 static void ospf_distance_free(struct ospf_distance *odistance)
1822 {
1823 XFREE(MTYPE_OSPF_DISTANCE, odistance);
1824 }
1825
1826 int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1827 const char *distance_str, const char *ip_str,
1828 const char *access_list_str)
1829 {
1830 int ret;
1831 struct prefix_ipv4 p;
1832 uint8_t distance;
1833 struct route_node *rn;
1834 struct ospf_distance *odistance;
1835
1836 ret = str2prefix_ipv4(ip_str, &p);
1837 if (ret == 0) {
1838 vty_out(vty, "Malformed prefix\n");
1839 return CMD_WARNING_CONFIG_FAILED;
1840 }
1841
1842 distance = atoi(distance_str);
1843
1844 /* Get OSPF distance node. */
1845 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1846 if (rn->info) {
1847 odistance = rn->info;
1848 route_unlock_node(rn);
1849 } else {
1850 odistance = ospf_distance_new();
1851 rn->info = odistance;
1852 }
1853
1854 /* Set distance value. */
1855 odistance->distance = distance;
1856
1857 /* Reset access-list configuration. */
1858 if (odistance->access_list) {
1859 free(odistance->access_list);
1860 odistance->access_list = NULL;
1861 }
1862 if (access_list_str)
1863 odistance->access_list = strdup(access_list_str);
1864
1865 return CMD_SUCCESS;
1866 }
1867
1868 int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1869 const char *distance_str, const char *ip_str,
1870 char const *access_list_str)
1871 {
1872 int ret;
1873 struct prefix_ipv4 p;
1874 struct route_node *rn;
1875 struct ospf_distance *odistance;
1876
1877 ret = str2prefix_ipv4(ip_str, &p);
1878 if (ret == 0) {
1879 vty_out(vty, "Malformed prefix\n");
1880 return CMD_WARNING_CONFIG_FAILED;
1881 }
1882
1883 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1884 if (!rn) {
1885 vty_out(vty, "Can't find specified prefix\n");
1886 return CMD_WARNING_CONFIG_FAILED;
1887 }
1888
1889 odistance = rn->info;
1890
1891 if (odistance->access_list)
1892 free(odistance->access_list);
1893 ospf_distance_free(odistance);
1894
1895 rn->info = NULL;
1896 route_unlock_node(rn);
1897 route_unlock_node(rn);
1898
1899 return CMD_SUCCESS;
1900 }
1901
1902 void ospf_distance_reset(struct ospf *ospf)
1903 {
1904 struct route_node *rn;
1905 struct ospf_distance *odistance;
1906
1907 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn)) {
1908 odistance = rn->info;
1909 if (!odistance)
1910 continue;
1911
1912 if (odistance->access_list)
1913 free(odistance->access_list);
1914 ospf_distance_free(odistance);
1915 rn->info = NULL;
1916 route_unlock_node(rn);
1917 }
1918 }
1919
1920 uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1921 struct ospf_route * or)
1922 {
1923
1924 if (ospf == NULL)
1925 return 0;
1926
1927 if (ospf->distance_intra && or->path_type == OSPF_PATH_INTRA_AREA)
1928 return ospf->distance_intra;
1929
1930 if (ospf->distance_inter && or->path_type == OSPF_PATH_INTER_AREA)
1931 return ospf->distance_inter;
1932
1933 if (ospf->distance_external
1934 && (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1935 or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
1936 return ospf->distance_external;
1937
1938 if (ospf->distance_all)
1939 return ospf->distance_all;
1940
1941 return 0;
1942 }
1943
1944 void ospf_zebra_vrf_register(struct ospf *ospf)
1945 {
1946 if (!zclient || zclient->sock < 0 || !ospf)
1947 return;
1948
1949 if (ospf->vrf_id != VRF_UNKNOWN) {
1950 if (IS_DEBUG_OSPF_EVENT)
1951 zlog_debug("%s: Register VRF %s id %u", __func__,
1952 ospf_vrf_id_to_name(ospf->vrf_id),
1953 ospf->vrf_id);
1954 zclient_send_reg_requests(zclient, ospf->vrf_id);
1955 }
1956 }
1957
1958 void ospf_zebra_vrf_deregister(struct ospf *ospf)
1959 {
1960 if (!zclient || zclient->sock < 0 || !ospf)
1961 return;
1962
1963 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1964 if (IS_DEBUG_OSPF_EVENT)
1965 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1966 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1967 ospf->vrf_id);
1968 /* Deregister for router-id, interfaces,
1969 * redistributed routes. */
1970 zclient_send_dereg_requests(zclient, ospf->vrf_id);
1971 }
1972 }
1973
1974 /* Label Manager Functions */
1975
1976 /**
1977 * Check if Label Manager is Ready or not.
1978 *
1979 * @return True if Label Manager is ready, False otherwise
1980 */
1981 bool ospf_zebra_label_manager_ready(void)
1982 {
1983 return (zclient_sync->sock > 0);
1984 }
1985
1986 /**
1987 * Request Label Range to the Label Manager.
1988 *
1989 * @param base base label of the label range to request
1990 * @param chunk_size size of the label range to request
1991 *
1992 * @return 0 on success, -1 on failure
1993 */
1994 int ospf_zebra_request_label_range(uint32_t base, uint32_t chunk_size)
1995 {
1996 int ret;
1997 uint32_t start, end;
1998
1999 if (zclient_sync->sock < 0)
2000 return -1;
2001
2002 ret = lm_get_label_chunk(zclient_sync, 0, base, chunk_size, &start,
2003 &end);
2004 if (ret < 0) {
2005 zlog_warn("%s: error getting label range!", __func__);
2006 return -1;
2007 }
2008
2009 return 0;
2010 }
2011
2012 /**
2013 * Release Label Range to the Label Manager.
2014 *
2015 * @param start start of label range to release
2016 * @param end end of label range to release
2017 *
2018 * @return 0 on success, -1 otherwise
2019 */
2020 int ospf_zebra_release_label_range(uint32_t start, uint32_t end)
2021 {
2022 int ret;
2023
2024 if (zclient_sync->sock < 0)
2025 return -1;
2026
2027 ret = lm_release_label_chunk(zclient_sync, start, end);
2028 if (ret < 0) {
2029 zlog_warn("%s: error releasing label range!", __func__);
2030 return -1;
2031 }
2032
2033 return 0;
2034 }
2035
2036 /**
2037 * Connect to the Label Manager.
2038 *
2039 * @return 0 on success, -1 otherwise
2040 */
2041 int ospf_zebra_label_manager_connect(void)
2042 {
2043 /* Connect to label manager. */
2044 if (zclient_socket_connect(zclient_sync) < 0) {
2045 zlog_warn("%s: failed connecting synchronous zclient!",
2046 __func__);
2047 return -1;
2048 }
2049 /* make socket non-blocking */
2050 set_nonblocking(zclient_sync->sock);
2051
2052 /* Send hello to notify zebra this is a synchronous client */
2053 if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) {
2054 zlog_warn("%s: failed sending hello for synchronous zclient!",
2055 __func__);
2056 close(zclient_sync->sock);
2057 zclient_sync->sock = -1;
2058 return -1;
2059 }
2060
2061 /* Connect to label manager */
2062 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
2063 zlog_warn("%s: failed connecting to label manager!", __func__);
2064 if (zclient_sync->sock > 0) {
2065 close(zclient_sync->sock);
2066 zclient_sync->sock = -1;
2067 }
2068 return -1;
2069 }
2070
2071 osr_debug("SR (%s): Successfully connected to the Label Manager",
2072 __func__);
2073
2074 return 0;
2075 }
2076
2077 static void ospf_zebra_connected(struct zclient *zclient)
2078 {
2079 /* Send the client registration */
2080 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2081
2082 zclient_send_reg_requests(zclient, VRF_DEFAULT);
2083 }
2084
2085 /*
2086 * opaque messages between processes
2087 */
2088 static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS)
2089 {
2090 struct stream *s;
2091 struct zapi_opaque_msg info;
2092 struct ldp_igp_sync_if_state state;
2093 struct ldp_igp_sync_announce announce;
2094 struct zapi_opaque_reg_info dst;
2095 int ret = 0;
2096
2097 s = zclient->ibuf;
2098
2099 if (zclient_opaque_decode(s, &info) != 0)
2100 return -1;
2101
2102 switch (info.type) {
2103 case LINK_STATE_SYNC:
2104 STREAM_GETC(s, dst.proto);
2105 STREAM_GETW(s, dst.instance);
2106 STREAM_GETL(s, dst.session_id);
2107 dst.type = LINK_STATE_SYNC;
2108 ret = ospf_te_sync_ted(dst);
2109 break;
2110 case LDP_IGP_SYNC_IF_STATE_UPDATE:
2111 STREAM_GET(&state, s, sizeof(state));
2112 ret = ospf_ldp_sync_state_update(state);
2113 break;
2114 case LDP_IGP_SYNC_ANNOUNCE_UPDATE:
2115 STREAM_GET(&announce, s, sizeof(announce));
2116 ret = ospf_ldp_sync_announce_update(announce);
2117 break;
2118 default:
2119 break;
2120 }
2121
2122 stream_failure:
2123
2124 return ret;
2125 }
2126
2127 static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS)
2128 {
2129 int ret = 0;
2130
2131 struct zapi_client_close_info info;
2132
2133 if (zapi_client_close_notify_decode(zclient->ibuf, &info) < 0)
2134 return -1;
2135
2136 ospf_ldp_sync_handle_client_close(&info);
2137
2138 return ret;
2139 }
2140
2141 static zclient_handler *const ospf_handlers[] = {
2142 [ZEBRA_ROUTER_ID_UPDATE] = ospf_router_id_update_zebra,
2143 [ZEBRA_INTERFACE_ADDRESS_ADD] = ospf_interface_address_add,
2144 [ZEBRA_INTERFACE_ADDRESS_DELETE] = ospf_interface_address_delete,
2145 [ZEBRA_INTERFACE_LINK_PARAMS] = ospf_interface_link_params,
2146 [ZEBRA_INTERFACE_VRF_UPDATE] = ospf_interface_vrf_update,
2147
2148 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ospf_zebra_read_route,
2149 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ospf_zebra_read_route,
2150
2151 [ZEBRA_OPAQUE_MESSAGE] = ospf_opaque_msg_handler,
2152
2153 [ZEBRA_CLIENT_CLOSE_NOTIFY] = ospf_zebra_client_close_notify,
2154 };
2155
2156 void ospf_zebra_init(struct thread_master *master, unsigned short instance)
2157 {
2158 /* Allocate zebra structure. */
2159 zclient = zclient_new(master, &zclient_options_default, ospf_handlers,
2160 array_size(ospf_handlers));
2161 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
2162 zclient->zebra_connected = ospf_zebra_connected;
2163
2164 /* Initialize special zclient for synchronous message exchanges. */
2165 struct zclient_options options = zclient_options_default;
2166 options.synchronous = true;
2167 zclient_sync = zclient_new(master, &options, NULL, 0);
2168 zclient_sync->sock = -1;
2169 zclient_sync->redist_default = ZEBRA_ROUTE_OSPF;
2170 zclient_sync->instance = instance;
2171 /*
2172 * session_id must be different from default value (0) to distinguish
2173 * the asynchronous socket from the synchronous one
2174 */
2175 zclient_sync->session_id = 1;
2176 zclient_sync->privs = &ospfd_privs;
2177
2178 access_list_add_hook(ospf_filter_update);
2179 access_list_delete_hook(ospf_filter_update);
2180 prefix_list_add_hook(ospf_prefix_list_update);
2181 prefix_list_delete_hook(ospf_prefix_list_update);
2182 }
2183
2184 void ospf_zebra_send_arp(const struct interface *ifp, const struct prefix *p)
2185 {
2186 zclient_send_neigh_discovery_req(zclient, ifp, p);
2187 }