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