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