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