]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_zebra.c
Merge pull request #4861 from NaveenThanikachalam/logs
[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
54 DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table")
55 DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute")
56 DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments")
57
58 DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp))
59 DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp))
60
61 /* Zebra structure to hold current status. */
62 struct zclient *zclient = NULL;
63
64 /* For registering threads. */
65 extern struct thread_master *master;
66
67 /* Router-id update message from zebra. */
68 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
69 {
70 struct ospf *ospf = NULL;
71 struct prefix router_id;
72 zebra_router_id_update_read(zclient->ibuf, &router_id);
73
74 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
75 char buf[PREFIX2STR_BUFFER];
76 prefix2str(&router_id, buf, sizeof(buf));
77 zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf,
78 ospf_vrf_id_to_name(vrf_id), vrf_id);
79 }
80
81 ospf = ospf_lookup_by_vrf_id(vrf_id);
82
83 if (ospf != NULL) {
84 ospf->router_id_zebra = router_id.u.prefix4;
85 ospf_router_id_update(ospf);
86 } else {
87 if (IS_DEBUG_OSPF_EVENT) {
88 char buf[PREFIX2STR_BUFFER];
89
90 prefix2str(&router_id, buf, sizeof(buf));
91 zlog_debug(
92 "%s: ospf instance not found for vrf %s id %u router_id %s",
93 __PRETTY_FUNCTION__,
94 ospf_vrf_id_to_name(vrf_id), vrf_id, buf);
95 }
96 }
97 return 0;
98 }
99
100 /* Inteface addition message from zebra. */
101 static int ospf_interface_add(ZAPI_CALLBACK_ARGS)
102 {
103 struct interface *ifp = NULL;
104 struct ospf *ospf = NULL;
105
106 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
107 if (ifp == NULL)
108 return 0;
109
110 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
111 zlog_debug(
112 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
113 ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
114 ifp->vrf_id, ifp->ifindex,
115 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu,
116 ifp->speed);
117
118 assert(ifp->info);
119
120 if (IF_DEF_PARAMS(ifp)
121 && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
122 SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
123 IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
124 }
125
126 ospf = ospf_lookup_by_vrf_id(vrf_id);
127 if (!ospf)
128 return 0;
129
130 ospf_if_recalculate_output_cost(ifp);
131
132 ospf_if_update(ospf, ifp);
133
134 hook_call(ospf_if_update, ifp);
135
136 return 0;
137 }
138
139 static int ospf_interface_delete(ZAPI_CALLBACK_ARGS)
140 {
141 struct interface *ifp;
142 struct stream *s;
143 struct route_node *rn;
144
145 s = zclient->ibuf;
146 /* zebra_interface_state_read() updates interface structure in iflist */
147 ifp = zebra_interface_state_read(s, vrf_id);
148
149 if (ifp == NULL)
150 return 0;
151
152 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
153 zlog_debug(
154 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
155 ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
156 ifp->vrf_id, ifp->ifindex,
157 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
158
159 hook_call(ospf_if_delete, ifp);
160
161 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
162 if (rn->info)
163 ospf_if_free((struct ospf_interface *)rn->info);
164
165 if_set_index(ifp, IFINDEX_INTERNAL);
166 return 0;
167 }
168
169 static struct interface *zebra_interface_if_lookup(struct stream *s,
170 vrf_id_t vrf_id)
171 {
172 char ifname_tmp[INTERFACE_NAMSIZ];
173
174 /* Read interface name. */
175 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
176
177 /* And look it up. */
178 return if_lookup_by_name(ifname_tmp, vrf_id);
179 }
180
181 static int ospf_interface_state_up(ZAPI_CALLBACK_ARGS)
182 {
183 struct interface *ifp;
184 struct ospf_interface *oi;
185 struct route_node *rn;
186
187 ifp = zebra_interface_if_lookup(zclient->ibuf, vrf_id);
188
189 if (ifp == NULL)
190 return 0;
191
192 /* Interface is already up. */
193 if (if_is_operative(ifp)) {
194 /* Temporarily keep ifp values. */
195 struct interface if_tmp;
196 memcpy(&if_tmp, ifp, sizeof(struct interface));
197
198 zebra_interface_if_set_value(zclient->ibuf, ifp);
199
200 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
201 zlog_debug(
202 "Zebra: Interface[%s] state update speed %u -> %u, bw %d -> %d",
203 ifp->name, if_tmp.speed, ifp->speed,
204 if_tmp.bandwidth, ifp->bandwidth);
205
206 ospf_if_recalculate_output_cost(ifp);
207
208 if (if_tmp.mtu != ifp->mtu) {
209 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
210 zlog_debug(
211 "Zebra: Interface[%s] MTU change %u -> %u.",
212 ifp->name, if_tmp.mtu, ifp->mtu);
213
214 /* Must reset the interface (simulate down/up) when MTU
215 * changes. */
216 ospf_if_reset(ifp);
217 }
218 return 0;
219 }
220
221 zebra_interface_if_set_value(zclient->ibuf, ifp);
222
223 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
224 zlog_debug("Zebra: Interface[%s] state change to up.",
225 ifp->name);
226
227 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
228 if ((oi = rn->info) == NULL)
229 continue;
230
231 ospf_if_up(oi);
232 }
233
234 return 0;
235 }
236
237 static int ospf_interface_state_down(ZAPI_CALLBACK_ARGS)
238 {
239 struct interface *ifp;
240 struct ospf_interface *oi;
241 struct route_node *node;
242
243 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
244
245 if (ifp == NULL)
246 return 0;
247
248 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
249 zlog_debug("Zebra: Interface[%s] state change to down.",
250 ifp->name);
251
252 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
253 if ((oi = node->info) == NULL)
254 continue;
255 ospf_if_down(oi);
256 }
257
258 return 0;
259 }
260
261 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS)
262 {
263 struct connected *c;
264 struct ospf *ospf = NULL;
265
266
267 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
268
269 if (c == NULL)
270 return 0;
271
272 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
273 char buf[PREFIX2STR_BUFFER];
274 prefix2str(c->address, buf, sizeof(buf));
275 zlog_debug("Zebra: interface %s address add %s vrf %s id %u",
276 c->ifp->name, buf, ospf_vrf_id_to_name(vrf_id),
277 vrf_id);
278 }
279
280 ospf = ospf_lookup_by_vrf_id(vrf_id);
281 if (!ospf)
282 return 0;
283
284 ospf_if_update(ospf, c->ifp);
285
286 hook_call(ospf_if_update, c->ifp);
287
288 return 0;
289 }
290
291 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)
292 {
293 struct connected *c;
294 struct interface *ifp;
295 struct ospf_interface *oi;
296 struct route_node *rn;
297 struct prefix p;
298
299 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
300
301 if (c == NULL)
302 return 0;
303
304 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
305 char buf[PREFIX2STR_BUFFER];
306 prefix2str(c->address, buf, sizeof(buf));
307 zlog_debug("Zebra: interface %s address delete %s",
308 c->ifp->name, buf);
309 }
310
311 ifp = c->ifp;
312 p = *c->address;
313 p.prefixlen = IPV4_MAX_PREFIXLEN;
314
315 rn = route_node_lookup(IF_OIFS(ifp), &p);
316 if (!rn) {
317 connected_free(c);
318 return 0;
319 }
320
321 assert(rn->info);
322 oi = rn->info;
323 route_unlock_node(rn);
324
325 /* Call interface hook functions to clean up */
326 ospf_if_free(oi);
327
328 hook_call(ospf_if_update, c->ifp);
329
330 connected_free(c);
331
332 return 0;
333 }
334
335 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS)
336 {
337 struct interface *ifp;
338
339 ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id);
340
341 if (ifp == NULL)
342 return 0;
343
344 /* Update TE TLV */
345 ospf_mpls_te_update_if(ifp);
346
347 return 0;
348 }
349
350 /* VRF update for an interface. */
351 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)
352 {
353 struct interface *ifp = NULL;
354 vrf_id_t new_vrf_id;
355
356 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
357 &new_vrf_id);
358 if (!ifp)
359 return 0;
360
361 if (IS_DEBUG_OSPF_EVENT)
362 zlog_debug(
363 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
364 __PRETTY_FUNCTION__, ifp->name, vrf_id,
365 ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
366
367 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
368 if_update_to_new_vrf(ifp, new_vrf_id);
369
370 return 0;
371 }
372
373 void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
374 struct ospf_route * or)
375 {
376 struct zapi_route api;
377 struct zapi_nexthop *api_nh;
378 uint8_t distance;
379 struct ospf_path *path;
380 struct listnode *node;
381 int count = 0;
382
383 memset(&api, 0, sizeof(api));
384 api.vrf_id = ospf->vrf_id;
385 api.type = ZEBRA_ROUTE_OSPF;
386 api.instance = ospf->instance;
387 api.safi = SAFI_UNICAST;
388
389 memcpy(&api.prefix, p, sizeof(*p));
390 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
391
392 /* Metric value. */
393 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
394 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
395 api.metric = or->cost + or->u.ext.type2_cost;
396 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
397 api.metric = or->u.ext.type2_cost;
398 else
399 api.metric = or->cost;
400
401 /* Check if path type is ASE */
402 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
403 || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
404 && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
405 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
406 api.tag = or->u.ext.tag;
407 }
408
409 /* Distance value. */
410 distance = ospf_distance_apply(ospf, p, or);
411 if (distance) {
412 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
413 api.distance = distance;
414 }
415
416 /* Nexthop, ifindex, distance and metric information. */
417 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
418 if (count >= MULTIPATH_NUM)
419 break;
420 api_nh = &api.nexthops[count];
421 #ifdef HAVE_NETLINK
422 if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY
423 && path->ifindex != 0)) {
424 #else /* HAVE_NETLINK */
425 if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
426 #endif /* HAVE_NETLINK */
427 api_nh->gate.ipv4 = path->nexthop;
428 api_nh->ifindex = path->ifindex;
429 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
430 } else if (path->nexthop.s_addr != INADDR_ANY) {
431 api_nh->gate.ipv4 = path->nexthop;
432 api_nh->type = NEXTHOP_TYPE_IPV4;
433 } else {
434 api_nh->ifindex = path->ifindex;
435 api_nh->type = NEXTHOP_TYPE_IFINDEX;
436 }
437 api_nh->vrf_id = ospf->vrf_id;
438 count++;
439
440 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
441 char buf[2][INET_ADDRSTRLEN];
442 struct interface *ifp;
443
444 ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
445
446 zlog_debug(
447 "Zebra: Route add %s nexthop %s, ifindex=%d %s",
448 prefix2str(p, buf[0], sizeof(buf[0])),
449 inet_ntop(AF_INET, &path->nexthop,
450 buf[1], sizeof(buf[1])),
451 path->ifindex, ifp ? ifp->name : " ");
452 }
453 }
454 api.nexthop_num = count;
455
456 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
457 }
458
459 void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
460 struct ospf_route * or)
461 {
462 struct zapi_route api;
463
464 memset(&api, 0, sizeof(api));
465 api.vrf_id = ospf->vrf_id;
466 api.type = ZEBRA_ROUTE_OSPF;
467 api.instance = ospf->instance;
468 api.safi = SAFI_UNICAST;
469 memcpy(&api.prefix, p, sizeof(*p));
470
471 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
472 char buf[PREFIX2STR_BUFFER];
473 zlog_debug("Zebra: Route delete %s",
474 prefix2str(p, buf, sizeof(buf)));
475 }
476
477 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
478 }
479
480 void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
481 {
482 struct zapi_route api;
483
484 memset(&api, 0, sizeof(api));
485 api.vrf_id = ospf->vrf_id;
486 api.type = ZEBRA_ROUTE_OSPF;
487 api.instance = ospf->instance;
488 api.safi = SAFI_UNICAST;
489 memcpy(&api.prefix, p, sizeof(*p));
490 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
491
492 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
493
494 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
495 char buf[PREFIX2STR_BUFFER];
496 zlog_debug("Zebra: Route add discard %s",
497 prefix2str(p, buf, sizeof(buf)));
498 }
499 }
500
501 void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
502 {
503 struct zapi_route api;
504
505 memset(&api, 0, sizeof(api));
506 api.vrf_id = ospf->vrf_id;
507 api.type = ZEBRA_ROUTE_OSPF;
508 api.instance = ospf->instance;
509 api.safi = SAFI_UNICAST;
510 memcpy(&api.prefix, p, sizeof(*p));
511 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
512
513 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
514
515 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
516 char buf[PREFIX2STR_BUFFER];
517 zlog_debug("Zebra: Route delete discard %s",
518 prefix2str(p, buf, sizeof(buf)));
519 }
520 }
521
522 struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
523 unsigned short instance)
524 {
525 struct list *ext_list;
526 struct listnode *node;
527 struct ospf_external *ext;
528
529 ext_list = ospf->external[type];
530 if (!ext_list)
531 return (NULL);
532
533 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
534 if (ext->instance == instance)
535 return ext;
536
537 return NULL;
538 }
539
540 struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
541 unsigned short instance)
542 {
543 struct list *ext_list;
544 struct ospf_external *ext;
545
546 ext = ospf_external_lookup(ospf, type, instance);
547 if (ext)
548 return ext;
549
550 if (!ospf->external[type])
551 ospf->external[type] = list_new();
552
553 ext_list = ospf->external[type];
554 ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
555 ext->instance = instance;
556 EXTERNAL_INFO(ext) = route_table_init();
557
558 listnode_add(ext_list, ext);
559
560 return ext;
561 }
562
563 void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
564 {
565 struct ospf_external *ext;
566
567 ext = ospf_external_lookup(ospf, type, instance);
568
569 if (ext) {
570 if (EXTERNAL_INFO(ext))
571 route_table_finish(EXTERNAL_INFO(ext));
572
573 listnode_delete(ospf->external[type], ext);
574
575 if (!ospf->external[type]->count)
576 list_delete(&ospf->external[type]);
577
578 XFREE(MTYPE_OSPF_EXTERNAL, ext);
579 }
580 }
581
582 struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
583 unsigned short instance)
584 {
585 struct list *red_list;
586 struct listnode *node;
587 struct ospf_redist *red;
588
589 red_list = ospf->redist[type];
590 if (!red_list)
591 return (NULL);
592
593 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
594 if (red->instance == instance)
595 return red;
596
597 return NULL;
598 }
599
600 struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
601 unsigned short instance)
602 {
603 struct list *red_list;
604 struct ospf_redist *red;
605
606 red = ospf_redist_lookup(ospf, type, instance);
607 if (red)
608 return red;
609
610 if (!ospf->redist[type])
611 ospf->redist[type] = list_new();
612
613 red_list = ospf->redist[type];
614 red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
615 red->instance = instance;
616 red->dmetric.type = -1;
617 red->dmetric.value = -1;
618 ROUTEMAP_NAME(red) = NULL;
619 ROUTEMAP(red) = NULL;
620
621 listnode_add(red_list, red);
622
623 return red;
624 }
625
626 void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
627 {
628 struct ospf_redist *red;
629
630 red = ospf_redist_lookup(ospf, type, instance);
631
632 if (red) {
633 listnode_delete(ospf->redist[type], red);
634 if (!ospf->redist[type]->count) {
635 list_delete(&ospf->redist[type]);
636 }
637 ospf_routemap_unset(red);
638 XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
639 }
640 }
641
642
643 int ospf_is_type_redistributed(struct ospf *ospf, int type,
644 unsigned short instance)
645 {
646 return (DEFAULT_ROUTE_TYPE(type)
647 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
648 ospf->vrf_id)
649 : ((instance
650 && redist_check_instance(
651 &zclient->mi_redist[AFI_IP][type],
652 instance))
653 || (!instance
654 && vrf_bitmap_check(
655 zclient->redist[AFI_IP][type],
656 ospf->vrf_id))));
657 }
658
659 int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance,
660 int mtype, int mvalue)
661 {
662 int force = 0;
663 struct ospf_redist *red;
664
665 red = ospf_redist_lookup(ospf, type, instance);
666
667 if (red == NULL) {
668 zlog_err(
669 "Redistribute[%s][%d]: Lookup failed Type[%d] , Metric[%d]",
670 ospf_redist_string(type), instance,
671 metric_type(ospf, type, instance),
672 metric_value(ospf, type, instance));
673 return CMD_WARNING_CONFIG_FAILED;
674 }
675
676 if (ospf_is_type_redistributed(ospf, type, instance)) {
677 if (mtype != red->dmetric.type) {
678 red->dmetric.type = mtype;
679 force = LSA_REFRESH_FORCE;
680 }
681 if (mvalue != red->dmetric.value) {
682 red->dmetric.value = mvalue;
683 force = LSA_REFRESH_FORCE;
684 }
685
686 ospf_external_lsa_refresh_type(ospf, type, instance, force);
687
688 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
689 zlog_debug(
690 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
691 ospf_redist_string(type), instance,
692 metric_type(ospf, type, instance),
693 metric_value(ospf, type, instance));
694
695 return CMD_SUCCESS;
696 }
697
698 red->dmetric.type = mtype;
699 red->dmetric.value = mvalue;
700
701 ospf_external_add(ospf, type, instance);
702
703 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
704 instance, ospf->vrf_id);
705
706 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
707 zlog_debug(
708 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
709 ospf_redist_string(type), instance, ospf->vrf_id,
710 metric_type(ospf, type, instance),
711 metric_value(ospf, type, instance));
712
713 ospf_asbr_status_update(ospf, ++ospf->redistribute);
714
715 return CMD_SUCCESS;
716 }
717
718 int ospf_redistribute_unset(struct ospf *ospf, int type,
719 unsigned short instance)
720 {
721 if (type == zclient->redist_default && instance == zclient->instance)
722 return CMD_SUCCESS;
723
724 if (!ospf_is_type_redistributed(ospf, type, instance))
725 return CMD_SUCCESS;
726
727 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
728 instance, ospf->vrf_id);
729
730 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
731 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
732 ospf_redist_string(type), instance, ospf->vrf_id);
733
734 /* Remove the routes from OSPF table. */
735 ospf_redistribute_withdraw(ospf, type, instance);
736
737 ospf_external_del(ospf, type, instance);
738
739 ospf_asbr_status_update(ospf, --ospf->redistribute);
740
741 return CMD_SUCCESS;
742 }
743
744 int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
745 int mvalue)
746 {
747 struct prefix_ipv4 p;
748 struct in_addr nexthop;
749 int cur_originate = ospf->default_originate;
750 const char *type_str = NULL;
751
752 nexthop.s_addr = 0;
753 p.family = AF_INET;
754 p.prefix.s_addr = 0;
755 p.prefixlen = 0;
756
757 ospf->default_originate = originate;
758
759 if (cur_originate == originate) {
760 /* Refresh the lsa since metric might different */
761 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
762 zlog_debug(
763 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
764 ospf_redist_string(DEFAULT_ROUTE),
765 metric_type(ospf, DEFAULT_ROUTE, 0),
766 metric_value(ospf, DEFAULT_ROUTE, 0));
767
768 ospf_external_lsa_refresh_default(ospf);
769 return CMD_SUCCESS;
770 }
771
772 switch (cur_originate) {
773 case DEFAULT_ORIGINATE_NONE:
774 break;
775 case DEFAULT_ORIGINATE_ZEBRA:
776 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
777 zclient, AFI_IP, ospf->vrf_id);
778 ospf->redistribute--;
779 break;
780 case DEFAULT_ORIGINATE_ALWAYS:
781 ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
782 ospf_external_del(ospf, DEFAULT_ROUTE, 0);
783 ospf->redistribute--;
784 break;
785 }
786
787 switch (originate) {
788 case DEFAULT_ORIGINATE_NONE:
789 type_str = "none";
790 break;
791 case DEFAULT_ORIGINATE_ZEBRA:
792 type_str = "normal";
793 ospf->redistribute++;
794 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
795 zclient, AFI_IP, ospf->vrf_id);
796 break;
797 case DEFAULT_ORIGINATE_ALWAYS:
798 type_str = "always";
799 ospf->redistribute++;
800 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
801 ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop,
802 0);
803 break;
804 }
805
806 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
807 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
808 type_str,
809 metric_type(ospf, DEFAULT_ROUTE, 0),
810 metric_value(ospf, DEFAULT_ROUTE, 0));
811
812 ospf_external_lsa_refresh_default(ospf);
813 ospf_asbr_status_update(ospf, ospf->redistribute);
814 return CMD_SUCCESS;
815 }
816
817 static int ospf_external_lsa_originate_check(struct ospf *ospf,
818 struct external_info *ei)
819 {
820 /* If prefix is multicast, then do not originate LSA. */
821 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
822 zlog_info(
823 "LSA[Type5:%s]: Not originate AS-external-LSA, "
824 "Prefix belongs multicast",
825 inet_ntoa(ei->p.prefix));
826 return 0;
827 }
828
829 /* Take care of default-originate. */
830 if (is_prefix_default(&ei->p))
831 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
832 zlog_info(
833 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
834 "for default");
835 return 0;
836 }
837
838 return 1;
839 }
840
841 /* If connected prefix is OSPF enable interface, then do not announce. */
842 int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
843 {
844 struct listnode *node;
845 struct ospf_interface *oi;
846
847
848 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
849 if (prefix_match(oi->address, (struct prefix *)&ei->p))
850 return 0;
851 return 1;
852 }
853
854 /* return 1 if external LSA must be originated, 0 otherwise */
855 int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
856 int *changed)
857 {
858 struct route_map_set_values save_values;
859 struct prefix_ipv4 *p = &ei->p;
860 struct ospf_redist *red;
861 uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
862 unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
863
864 if (changed)
865 *changed = 0;
866
867 if (!ospf_external_lsa_originate_check(ospf, ei))
868 return 0;
869
870 /* Take care connected route. */
871 if (type == ZEBRA_ROUTE_CONNECT
872 && !ospf_distribute_check_connected(ospf, ei))
873 return 0;
874
875 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
876 /* distirbute-list exists, but access-list may not? */
877 if (DISTRIBUTE_LIST(ospf, type))
878 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
879 == FILTER_DENY) {
880 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
881 char buf[PREFIX2STR_BUFFER];
882 zlog_debug(
883 "Redistribute[%s]: %s filtered by distribute-list.",
884 ospf_redist_string(type),
885 prefix2str(p, buf, sizeof(buf)));
886 }
887 return 0;
888 }
889
890 save_values = ei->route_map_set;
891 ospf_reset_route_map_set_values(&ei->route_map_set);
892
893 /* apply route-map if needed */
894 red = ospf_redist_lookup(ospf, type, instance);
895 if (red && ROUTEMAP_NAME(red)) {
896 route_map_result_t ret;
897
898 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
899 RMAP_OSPF, ei);
900
901 if (ret == RMAP_DENYMATCH) {
902 ei->route_map_set = save_values;
903 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
904 char buf[PREFIX2STR_BUFFER];
905 zlog_debug(
906 "Redistribute[%s]: %s filtered by route-map.",
907 ospf_redist_string(type),
908 prefix2str(p, buf, sizeof(buf)));
909 }
910 return 0;
911 }
912
913 /* check if 'route-map set' changed something */
914 if (changed)
915 *changed = !ospf_route_map_set_compare(
916 &ei->route_map_set, &save_values);
917 }
918
919 return 1;
920 }
921
922 /* OSPF route-map set for redistribution */
923 void ospf_routemap_set(struct ospf_redist *red, const char *name)
924 {
925 if (ROUTEMAP_NAME(red)) {
926 route_map_counter_decrement(ROUTEMAP(red));
927 free(ROUTEMAP_NAME(red));
928 }
929
930 ROUTEMAP_NAME(red) = strdup(name);
931 ROUTEMAP(red) = route_map_lookup_by_name(name);
932 route_map_counter_increment(ROUTEMAP(red));
933 }
934
935 void ospf_routemap_unset(struct ospf_redist *red)
936 {
937 if (ROUTEMAP_NAME(red)) {
938 route_map_counter_decrement(ROUTEMAP(red));
939 free(ROUTEMAP_NAME(red));
940 }
941
942 ROUTEMAP_NAME(red) = NULL;
943 ROUTEMAP(red) = NULL;
944 }
945
946 /* Zebra route add and delete treatment. */
947 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
948 {
949 struct zapi_route api;
950 struct prefix_ipv4 p;
951 unsigned long ifindex;
952 struct in_addr nexthop;
953 struct external_info *ei;
954 struct ospf *ospf;
955 int i;
956 uint8_t rt_type;
957
958 ospf = ospf_lookup_by_vrf_id(vrf_id);
959 if (ospf == NULL)
960 return 0;
961
962 if (zapi_route_decode(zclient->ibuf, &api) < 0)
963 return -1;
964
965 ifindex = api.nexthops[0].ifindex;
966 nexthop = api.nexthops[0].gate.ipv4;
967 rt_type = api.type;
968
969 memcpy(&p, &api.prefix, sizeof(p));
970 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
971 return 0;
972
973 /* Re-destributed route is default route.
974 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
975 * updating ex-info. But in resetting (no default-info
976 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
977 * Resolved this inconsistency by maintaining same route type.
978 */
979 if (is_prefix_default(&p))
980 rt_type = DEFAULT_ROUTE;
981
982 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
983 char buf_prefix[PREFIX_STRLEN];
984 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
985
986 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %s",
987 __func__, zserv_command_string(cmd),
988 zebra_route_string(api.type), vrf_id, buf_prefix);
989 }
990
991 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
992 /* XXX|HACK|TODO|FIXME:
993 * Maybe we should ignore reject/blackhole routes? Testing
994 * shows that there is no problems though and this is only way
995 * to "summarize" routes in ASBR at the moment. Maybe we need
996 * just a better generalised solution for these types?
997 */
998
999 /* Protocol tag overwrites all other tag value sent by zebra */
1000 if (ospf->dtag[rt_type] > 0)
1001 api.tag = ospf->dtag[rt_type];
1002
1003 /*
1004 * Given zebra sends update for a prefix via ADD message, it
1005 * should
1006 * be considered as an implicit DEL for that prefix with other
1007 * source
1008 * types.
1009 */
1010 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
1011 if (i != rt_type)
1012 ospf_external_info_delete(ospf, i, api.instance,
1013 p);
1014
1015 ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
1016 ifindex, nexthop, api.tag);
1017 if (ei == NULL) {
1018 /* Nothing has changed, so nothing to do; return */
1019 return 0;
1020 }
1021 if (ospf->router_id.s_addr != 0) {
1022 if (ei) {
1023 if (is_prefix_default(&p))
1024 ospf_external_lsa_refresh_default(ospf);
1025 else {
1026 struct ospf_lsa *current;
1027
1028 current = ospf_external_info_find_lsa(
1029 ospf, &ei->p);
1030 if (!current)
1031 ospf_external_lsa_originate(
1032 ospf, ei);
1033 else {
1034 if (IS_DEBUG_OSPF(
1035 zebra,
1036 ZEBRA_REDISTRIBUTE))
1037 zlog_debug(
1038 "ospf_zebra_read_route() : %s refreshing LSA",
1039 inet_ntoa(
1040 p.prefix));
1041 ospf_external_lsa_refresh(
1042 ospf, current, ei,
1043 LSA_REFRESH_FORCE);
1044 }
1045 }
1046 }
1047 }
1048 } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1049 {
1050 ospf_external_info_delete(ospf, rt_type, api.instance, p);
1051 if (is_prefix_default(&p))
1052 ospf_external_lsa_refresh_default(ospf);
1053 else
1054 ospf_external_lsa_flush(ospf, rt_type, &p,
1055 ifindex /*, nexthop */);
1056 }
1057
1058 return 0;
1059 }
1060
1061
1062 int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
1063 {
1064 /* Lookup access-list for distribute-list. */
1065 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
1066
1067 /* Clear previous distribute-name. */
1068 if (DISTRIBUTE_NAME(ospf, type))
1069 free(DISTRIBUTE_NAME(ospf, type));
1070
1071 /* Set distribute-name. */
1072 DISTRIBUTE_NAME(ospf, type) = strdup(name);
1073
1074 /* If access-list have been set, schedule update timer. */
1075 if (DISTRIBUTE_LIST(ospf, type))
1076 ospf_distribute_list_update(ospf, type, 0);
1077
1078 return CMD_SUCCESS;
1079 }
1080
1081 int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1082 const char *name)
1083 {
1084 /* Schedule update timer. */
1085 if (DISTRIBUTE_LIST(ospf, type))
1086 ospf_distribute_list_update(ospf, type, 0);
1087
1088 /* Unset distribute-list. */
1089 DISTRIBUTE_LIST(ospf, type) = NULL;
1090
1091 /* Clear distribute-name. */
1092 if (DISTRIBUTE_NAME(ospf, type))
1093 free(DISTRIBUTE_NAME(ospf, type));
1094
1095 DISTRIBUTE_NAME(ospf, type) = NULL;
1096
1097 return CMD_SUCCESS;
1098 }
1099
1100 /* distribute-list update timer. */
1101 static int ospf_distribute_list_update_timer(struct thread *thread)
1102 {
1103 struct route_node *rn;
1104 struct external_info *ei;
1105 struct route_table *rt;
1106 struct ospf_lsa *lsa;
1107 int type, default_refresh = 0, arg_type;
1108 struct ospf *ospf = NULL;
1109 void **arg = THREAD_ARG(thread);
1110
1111 ospf = (struct ospf *)arg[0];
1112 arg_type = (int)(intptr_t)arg[1];
1113
1114 if (ospf == NULL)
1115 return 0;
1116
1117 ospf->t_distribute_update = NULL;
1118
1119 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1120
1121 if (IS_DEBUG_OSPF_EVENT) {
1122 zlog_debug(
1123 "%s: ospf distribute-list update arg_type %d vrf %s id %d",
1124 __PRETTY_FUNCTION__, arg_type,
1125 ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id);
1126 }
1127
1128 /* foreach all external info. */
1129 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1130 struct list *ext_list;
1131 struct listnode *node;
1132 struct ospf_external *ext;
1133
1134 ext_list = ospf->external[type];
1135 if (!ext_list)
1136 continue;
1137
1138 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1139 rt = ext->external_info;
1140 if (!rt)
1141 continue;
1142 for (rn = route_top(rt); rn; rn = route_next(rn))
1143 if ((ei = rn->info) != NULL) {
1144 if (is_prefix_default(&ei->p))
1145 default_refresh = 1;
1146 else if (
1147 (lsa = ospf_external_info_find_lsa(
1148 ospf, &ei->p)))
1149 ospf_external_lsa_refresh(
1150 ospf, lsa, ei,
1151 LSA_REFRESH_IF_CHANGED);
1152 else
1153 ospf_external_lsa_originate(
1154 ospf, ei);
1155 }
1156 }
1157 }
1158 if (default_refresh)
1159 ospf_external_lsa_refresh_default(ospf);
1160
1161 XFREE(MTYPE_OSPF_DIST_ARGS, arg);
1162 return 0;
1163 }
1164
1165 /* Update distribute-list and set timer to apply access-list. */
1166 void ospf_distribute_list_update(struct ospf *ospf, int type,
1167 unsigned short instance)
1168 {
1169 struct route_table *rt;
1170 struct ospf_external *ext;
1171 void **args = XCALLOC(MTYPE_OSPF_DIST_ARGS, sizeof(void *) * 2);
1172
1173 args[0] = ospf;
1174 args[1] = (void *)((ptrdiff_t)type);
1175
1176 /* External info does not exist. */
1177 ext = ospf_external_lookup(ospf, type, instance);
1178 if (!ext || !(rt = EXTERNAL_INFO(ext))) {
1179 XFREE(MTYPE_OSPF_DIST_ARGS, args);
1180 return;
1181 }
1182
1183 /* If exists previously invoked thread, then let it continue. */
1184 if (ospf->t_distribute_update) {
1185 XFREE(MTYPE_OSPF_DIST_ARGS, args);
1186 return;
1187 }
1188
1189 /* Set timer. */
1190 ospf->t_distribute_update = NULL;
1191 thread_add_timer_msec(master, ospf_distribute_list_update_timer,
1192 (void **)args, ospf->min_ls_interval,
1193 &ospf->t_distribute_update);
1194 }
1195
1196 /* If access-list is updated, apply some check. */
1197 static void ospf_filter_update(struct access_list *access)
1198 {
1199 struct ospf *ospf;
1200 int type;
1201 int abr_inv = 0;
1202 struct ospf_area *area;
1203 struct listnode *node, *n1;
1204
1205 /* If OSPF instance does not exist, return right now. */
1206 if (listcount(om->ospf) == 0)
1207 return;
1208
1209 /* Iterate all ospf [VRF] instances */
1210 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1211 /* Update distribute-list, and apply filter. */
1212 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1213 struct list *red_list;
1214 struct ospf_redist *red;
1215
1216 red_list = ospf->redist[type];
1217 if (red_list)
1218 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1219 red)) {
1220 if (ROUTEMAP(red)) {
1221 /* if route-map is not NULL it
1222 * may be
1223 * using this access list */
1224 ospf_distribute_list_update(
1225 ospf, type,
1226 red->instance);
1227 }
1228 }
1229
1230 /* There is place for route-map for default-information
1231 * (ZEBRA_ROUTE_MAX),
1232 * but no distribute list. */
1233 if (type == ZEBRA_ROUTE_MAX)
1234 break;
1235
1236 if (DISTRIBUTE_NAME(ospf, type)) {
1237 /* Keep old access-list for distribute-list. */
1238 struct access_list *old =
1239 DISTRIBUTE_LIST(ospf, type);
1240
1241 /* Update access-list for distribute-list. */
1242 DISTRIBUTE_LIST(ospf, type) =
1243 access_list_lookup(
1244 AFI_IP,
1245 DISTRIBUTE_NAME(ospf, type));
1246
1247 /* No update for this distribute type. */
1248 if (old == NULL
1249 && DISTRIBUTE_LIST(ospf, type) == NULL)
1250 continue;
1251
1252 /* Schedule distribute-list update timer. */
1253 if (DISTRIBUTE_LIST(ospf, type) == NULL
1254 || strcmp(DISTRIBUTE_NAME(ospf, type),
1255 access->name)
1256 == 0)
1257 ospf_distribute_list_update(ospf, type,
1258 0);
1259 }
1260 }
1261
1262 /* Update Area access-list. */
1263 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1264 if (EXPORT_NAME(area)) {
1265 EXPORT_LIST(area) = NULL;
1266 abr_inv++;
1267 }
1268
1269 if (IMPORT_NAME(area)) {
1270 IMPORT_LIST(area) = NULL;
1271 abr_inv++;
1272 }
1273 }
1274
1275 /* Schedule ABR tasks -- this will be changed -- takada. */
1276 if (IS_OSPF_ABR(ospf) && abr_inv)
1277 ospf_schedule_abr_task(ospf);
1278 }
1279 }
1280
1281 /* If prefix-list is updated, do some updates. */
1282 void ospf_prefix_list_update(struct prefix_list *plist)
1283 {
1284 struct ospf *ospf = NULL;
1285 int type;
1286 int abr_inv = 0;
1287 struct ospf_area *area;
1288 struct listnode *node, *n1;
1289
1290 /* If OSPF instatnce does not exist, return right now. */
1291 if (listcount(om->ospf) == 0)
1292 return;
1293
1294 /* Iterate all ospf [VRF] instances */
1295 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1296
1297 /* Update all route-maps which are used
1298 * as redistribution filters.
1299 * They might use prefix-list.
1300 */
1301 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1302 struct list *red_list;
1303 struct ospf_redist *red;
1304
1305 red_list = ospf->redist[type];
1306 if (red_list) {
1307 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1308 red)) {
1309 if (ROUTEMAP(red)) {
1310 /* if route-map is not NULL
1311 * it may be using
1312 * this prefix list */
1313 ospf_distribute_list_update(
1314 ospf, type,
1315 red->instance);
1316 }
1317 }
1318 }
1319 }
1320
1321 /* Update area filter-lists. */
1322 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1323 /* Update filter-list in. */
1324 if (PREFIX_NAME_IN(area))
1325 if (strcmp(PREFIX_NAME_IN(area),
1326 prefix_list_name(plist))
1327 == 0) {
1328 PREFIX_LIST_IN(area) =
1329 prefix_list_lookup(
1330 AFI_IP,
1331 PREFIX_NAME_IN(area));
1332 abr_inv++;
1333 }
1334
1335 /* Update filter-list out. */
1336 if (PREFIX_NAME_OUT(area))
1337 if (strcmp(PREFIX_NAME_OUT(area),
1338 prefix_list_name(plist))
1339 == 0) {
1340 PREFIX_LIST_IN(area) =
1341 prefix_list_lookup(
1342 AFI_IP,
1343 PREFIX_NAME_OUT(area));
1344 abr_inv++;
1345 }
1346 }
1347
1348 /* Schedule ABR task. */
1349 if (IS_OSPF_ABR(ospf) && abr_inv)
1350 ospf_schedule_abr_task(ospf);
1351 }
1352 }
1353
1354 static struct ospf_distance *ospf_distance_new(void)
1355 {
1356 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
1357 }
1358
1359 static void ospf_distance_free(struct ospf_distance *odistance)
1360 {
1361 XFREE(MTYPE_OSPF_DISTANCE, odistance);
1362 }
1363
1364 int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1365 const char *distance_str, const char *ip_str,
1366 const char *access_list_str)
1367 {
1368 int ret;
1369 struct prefix_ipv4 p;
1370 uint8_t distance;
1371 struct route_node *rn;
1372 struct ospf_distance *odistance;
1373
1374 ret = str2prefix_ipv4(ip_str, &p);
1375 if (ret == 0) {
1376 vty_out(vty, "Malformed prefix\n");
1377 return CMD_WARNING_CONFIG_FAILED;
1378 }
1379
1380 distance = atoi(distance_str);
1381
1382 /* Get OSPF distance node. */
1383 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1384 if (rn->info) {
1385 odistance = rn->info;
1386 route_unlock_node(rn);
1387 } else {
1388 odistance = ospf_distance_new();
1389 rn->info = odistance;
1390 }
1391
1392 /* Set distance value. */
1393 odistance->distance = distance;
1394
1395 /* Reset access-list configuration. */
1396 if (odistance->access_list) {
1397 free(odistance->access_list);
1398 odistance->access_list = NULL;
1399 }
1400 if (access_list_str)
1401 odistance->access_list = strdup(access_list_str);
1402
1403 return CMD_SUCCESS;
1404 }
1405
1406 int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1407 const char *distance_str, const char *ip_str,
1408 char const *access_list_str)
1409 {
1410 int ret;
1411 struct prefix_ipv4 p;
1412 struct route_node *rn;
1413 struct ospf_distance *odistance;
1414
1415 ret = str2prefix_ipv4(ip_str, &p);
1416 if (ret == 0) {
1417 vty_out(vty, "Malformed prefix\n");
1418 return CMD_WARNING_CONFIG_FAILED;
1419 }
1420
1421 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1422 if (!rn) {
1423 vty_out(vty, "Can't find specified prefix\n");
1424 return CMD_WARNING_CONFIG_FAILED;
1425 }
1426
1427 odistance = rn->info;
1428
1429 if (odistance->access_list)
1430 free(odistance->access_list);
1431 ospf_distance_free(odistance);
1432
1433 rn->info = NULL;
1434 route_unlock_node(rn);
1435 route_unlock_node(rn);
1436
1437 return CMD_SUCCESS;
1438 }
1439
1440 void ospf_distance_reset(struct ospf *ospf)
1441 {
1442 struct route_node *rn;
1443 struct ospf_distance *odistance;
1444
1445 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn))
1446 if ((odistance = rn->info) != NULL) {
1447 if (odistance->access_list)
1448 free(odistance->access_list);
1449 ospf_distance_free(odistance);
1450 rn->info = NULL;
1451 route_unlock_node(rn);
1452 }
1453 }
1454
1455 uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1456 struct ospf_route * or)
1457 {
1458
1459 if (ospf == NULL)
1460 return 0;
1461
1462 if (ospf->distance_intra)
1463 if (or->path_type == OSPF_PATH_INTRA_AREA)
1464 return ospf->distance_intra;
1465
1466 if (ospf->distance_inter)
1467 if (or->path_type == OSPF_PATH_INTER_AREA)
1468 return ospf->distance_inter;
1469
1470 if (ospf->distance_external)
1471 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1472 or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1473 return ospf->distance_external;
1474
1475 if (ospf->distance_all)
1476 return ospf->distance_all;
1477
1478 return 0;
1479 }
1480
1481 void ospf_zebra_vrf_register(struct ospf *ospf)
1482 {
1483 if (!zclient || zclient->sock < 0 || !ospf)
1484 return;
1485
1486 if (ospf->vrf_id != VRF_UNKNOWN) {
1487 if (IS_DEBUG_OSPF_EVENT)
1488 zlog_debug("%s: Register VRF %s id %u",
1489 __PRETTY_FUNCTION__,
1490 ospf_vrf_id_to_name(ospf->vrf_id),
1491 ospf->vrf_id);
1492 zclient_send_reg_requests(zclient, ospf->vrf_id);
1493 }
1494 }
1495
1496 void ospf_zebra_vrf_deregister(struct ospf *ospf)
1497 {
1498 if (!zclient || zclient->sock < 0 || !ospf)
1499 return;
1500
1501 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1502 if (IS_DEBUG_OSPF_EVENT)
1503 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1504 __PRETTY_FUNCTION__,
1505 ospf_vrf_id_to_name(ospf->vrf_id),
1506 ospf->vrf_id);
1507 /* Deregister for router-id, interfaces,
1508 * redistributed routes. */
1509 zclient_send_dereg_requests(zclient, ospf->vrf_id);
1510 }
1511 }
1512 static void ospf_zebra_connected(struct zclient *zclient)
1513 {
1514 /* Send the client registration */
1515 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
1516
1517 zclient_send_reg_requests(zclient, VRF_DEFAULT);
1518 }
1519
1520 void ospf_zebra_init(struct thread_master *master, unsigned short instance)
1521 {
1522 /* Allocate zebra structure. */
1523 zclient = zclient_new(master, &zclient_options_default);
1524 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
1525 zclient->zebra_connected = ospf_zebra_connected;
1526 zclient->router_id_update = ospf_router_id_update_zebra;
1527 zclient->interface_add = ospf_interface_add;
1528 zclient->interface_delete = ospf_interface_delete;
1529 zclient->interface_up = ospf_interface_state_up;
1530 zclient->interface_down = ospf_interface_state_down;
1531 zclient->interface_address_add = ospf_interface_address_add;
1532 zclient->interface_address_delete = ospf_interface_address_delete;
1533 zclient->interface_link_params = ospf_interface_link_params;
1534 zclient->interface_vrf_update = ospf_interface_vrf_update;
1535
1536 zclient->redistribute_route_add = ospf_zebra_read_route;
1537 zclient->redistribute_route_del = ospf_zebra_read_route;
1538
1539 access_list_add_hook(ospf_filter_update);
1540 access_list_delete_hook(ospf_filter_update);
1541 prefix_list_add_hook(ospf_prefix_list_update);
1542 prefix_list_delete_hook(ospf_prefix_list_update);
1543 }