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