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