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