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