]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_zebra.c
Merge pull request #1950 from pguibert6WIND/issue_1949
[mirror_frr.git] / ospfd / ospf_zebra.c
1 /*
2 * Zebra connect library for OSPFd
3 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "thread.h"
25 #include "command.h"
26 #include "network.h"
27 #include "prefix.h"
28 #include "routemap.h"
29 #include "table.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "zclient.h"
33 #include "filter.h"
34 #include "plist.h"
35 #include "log.h"
36 #include "lib/bfd.h"
37 #include "nexthop.h"
38
39 #include "ospfd/ospfd.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_ism.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_asbr.h"
44 #include "ospfd/ospf_abr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_dump.h"
47 #include "ospfd/ospf_route.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_nsm.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_te.h"
53
54 DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table")
55 DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute")
56 DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments")
57
58 DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp))
59 DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp))
60
61 /* Zebra structure to hold current status. */
62 struct zclient *zclient = NULL;
63
64 /* For registering threads. */
65 extern struct thread_master *master;
66
67 /* Router-id update message from zebra. */
68 static int ospf_router_id_update_zebra(int command, struct zclient *zclient,
69 zebra_size_t length, vrf_id_t vrf_id)
70 {
71 struct ospf *ospf = NULL;
72 struct prefix router_id;
73 zebra_router_id_update_read(zclient->ibuf, &router_id);
74
75 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
76 char buf[PREFIX2STR_BUFFER];
77 prefix2str(&router_id, buf, sizeof(buf));
78 zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf,
79 ospf_vrf_id_to_name(vrf_id), vrf_id);
80 }
81
82 ospf = ospf_lookup_by_vrf_id(vrf_id);
83
84 if (ospf != NULL) {
85 ospf->router_id_zebra = router_id.u.prefix4;
86 ospf_router_id_update(ospf);
87 } else {
88 if (IS_DEBUG_OSPF_EVENT) {
89 char buf[PREFIX2STR_BUFFER];
90
91 prefix2str(&router_id, buf, sizeof(buf));
92 zlog_debug(
93 "%s: ospf instance not found for vrf %s id %u router_id %s",
94 __PRETTY_FUNCTION__,
95 ospf_vrf_id_to_name(vrf_id), vrf_id, buf);
96 }
97 }
98 return 0;
99 }
100
101 /* Inteface addition message from zebra. */
102 static int ospf_interface_add(int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
104 {
105 struct interface *ifp = NULL;
106 struct ospf *ospf = NULL;
107
108 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
109 if (ifp == NULL)
110 return 0;
111
112 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
113 zlog_debug(
114 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
115 ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
116 ifp->vrf_id, ifp->ifindex,
117 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
118
119 assert(ifp->info);
120
121 if (IF_DEF_PARAMS(ifp)
122 && !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 if_set_index(ifp, IFINDEX_INTERNAL);
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(
373 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
374 __PRETTY_FUNCTION__, ifp->name, vrf_id,
375 ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
376
377 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
378 if_update_to_new_vrf(ifp, new_vrf_id);
379
380 return 0;
381 }
382
383 void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
384 struct ospf_route * or)
385 {
386 struct zapi_route api;
387 struct zapi_nexthop *api_nh;
388 uint8_t distance;
389 struct ospf_path *path;
390 struct listnode *node;
391 int count = 0;
392
393 memset(&api, 0, sizeof(api));
394 api.vrf_id = ospf->vrf_id;
395 api.type = ZEBRA_ROUTE_OSPF;
396 api.instance = ospf->instance;
397 api.safi = SAFI_UNICAST;
398
399 memcpy(&api.prefix, p, sizeof(*p));
400 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
401
402 /* Metric value. */
403 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
404 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
405 api.metric = or->cost + or->u.ext.type2_cost;
406 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
407 api.metric = or->u.ext.type2_cost;
408 else
409 api.metric = or->cost;
410
411 /* Check if path type is ASE */
412 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
413 || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
414 && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
415 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
416 api.tag = or->u.ext.tag;
417 }
418
419 /* Distance value. */
420 distance = ospf_distance_apply(ospf, p, or);
421 if (distance) {
422 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
423 api.distance = distance;
424 }
425
426 /* Nexthop, ifindex, distance and metric information. */
427 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
428 if (count >= MULTIPATH_NUM)
429 break;
430 api_nh = &api.nexthops[count];
431 #ifdef HAVE_NETLINK
432 if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY
433 && path->ifindex != 0)) {
434 #else /* HAVE_NETLINK */
435 if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
436 #endif /* HAVE_NETLINK */
437 api_nh->gate.ipv4 = path->nexthop;
438 api_nh->ifindex = path->ifindex;
439 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
440 } else if (path->nexthop.s_addr != INADDR_ANY) {
441 api_nh->gate.ipv4 = path->nexthop;
442 api_nh->type = NEXTHOP_TYPE_IPV4;
443 } else {
444 api_nh->ifindex = path->ifindex;
445 api_nh->type = NEXTHOP_TYPE_IFINDEX;
446 }
447 api_nh->vrf_id = ospf->vrf_id;
448 count++;
449
450 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
451 char buf[2][INET_ADDRSTRLEN];
452 zlog_debug(
453 "Zebra: Route add %s/%d nexthop %s, ifindex=%d",
454 inet_ntop(AF_INET, &p->prefix, buf[0],
455 sizeof(buf[0])),
456 p->prefixlen, inet_ntop(AF_INET, &path->nexthop,
457 buf[1], sizeof(buf[1])),
458 path->ifindex);
459 }
460 }
461 api.nexthop_num = count;
462
463 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
464 }
465
466 void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
467 struct ospf_route * or)
468 {
469 struct zapi_route api;
470
471 memset(&api, 0, sizeof(api));
472 api.vrf_id = ospf->vrf_id;
473 api.type = ZEBRA_ROUTE_OSPF;
474 api.instance = ospf->instance;
475 api.safi = SAFI_UNICAST;
476 memcpy(&api.prefix, p, sizeof(*p));
477
478 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
479 char buf[INET_ADDRSTRLEN];
480 zlog_debug("Zebra: Route delete %s/%d",
481 inet_ntop(AF_INET, &p->prefix, buf, sizeof(buf[0])),
482 p->prefixlen);
483 }
484
485 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
486 }
487
488 void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
489 {
490 struct zapi_route api;
491
492 memset(&api, 0, sizeof(api));
493 api.vrf_id = ospf->vrf_id;
494 api.type = ZEBRA_ROUTE_OSPF;
495 api.instance = ospf->instance;
496 api.safi = SAFI_UNICAST;
497 memcpy(&api.prefix, p, sizeof(*p));
498 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
499
500 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
501
502 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
503 zlog_debug("Zebra: Route add discard %s/%d",
504 inet_ntoa(p->prefix), p->prefixlen);
505 }
506
507 void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
508 {
509 struct zapi_route api;
510
511 memset(&api, 0, sizeof(api));
512 api.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, uint8_t type,
527 unsigned 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, uint8_t type,
545 unsigned 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, uint8_t type, unsigned 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, uint8_t type,
588 unsigned 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, uint8_t type,
606 unsigned 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, uint8_t type, unsigned 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,
648 unsigned short instance)
649 {
650 return (DEFAULT_ROUTE_TYPE(type)
651 ? vrf_bitmap_check(zclient->default_information,
652 ospf->vrf_id)
653 : ((instance
654 && redist_check_instance(
655 &zclient->mi_redist[AFI_IP][type],
656 instance))
657 || (!instance
658 && vrf_bitmap_check(
659 zclient->redist[AFI_IP][type],
660 ospf->vrf_id))));
661 }
662
663 int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance,
664 int mtype, int mvalue)
665 {
666 int force = 0;
667 struct ospf_redist *red;
668
669 red = ospf_redist_lookup(ospf, type, instance);
670 if (ospf_is_type_redistributed(ospf, type, instance)) {
671 if (mtype != red->dmetric.type) {
672 red->dmetric.type = mtype;
673 force = LSA_REFRESH_FORCE;
674 }
675 if (mvalue != red->dmetric.value) {
676 red->dmetric.value = mvalue;
677 force = LSA_REFRESH_FORCE;
678 }
679
680 ospf_external_lsa_refresh_type(ospf, type, instance, force);
681
682 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
683 zlog_debug(
684 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
685 ospf_redist_string(type), instance,
686 metric_type(ospf, type, instance),
687 metric_value(ospf, type, instance));
688
689 return CMD_SUCCESS;
690 }
691
692 red->dmetric.type = mtype;
693 red->dmetric.value = mvalue;
694
695 ospf_external_add(ospf, type, instance);
696
697 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
698 instance, ospf->vrf_id);
699
700 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
701 zlog_debug(
702 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
703 ospf_redist_string(type), instance, ospf->vrf_id,
704 metric_type(ospf, type, instance),
705 metric_value(ospf, type, instance));
706
707 ospf_asbr_status_update(ospf, ++ospf->redistribute);
708
709 return CMD_SUCCESS;
710 }
711
712 int ospf_redistribute_unset(struct ospf *ospf, int type,
713 unsigned short instance)
714 {
715 if (type == zclient->redist_default && instance == zclient->instance)
716 return CMD_SUCCESS;
717
718 if (!ospf_is_type_redistributed(ospf, type, instance))
719 return CMD_SUCCESS;
720
721 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
722 instance, ospf->vrf_id);
723
724 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
725 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
726 ospf_redist_string(type), instance, ospf->vrf_id);
727
728 ospf_redist_del(ospf, type, instance);
729
730 /* Remove the routes from OSPF table. */
731 ospf_redistribute_withdraw(ospf, type, instance);
732
733 ospf_external_del(ospf, type, instance);
734
735 ospf_asbr_status_update(ospf, --ospf->redistribute);
736
737 return CMD_SUCCESS;
738 }
739
740 int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
741 int mvalue)
742 {
743 struct ospf_redist *red;
744
745 ospf->default_originate = originate;
746
747 red = ospf_redist_add(ospf, DEFAULT_ROUTE, 0);
748 red->dmetric.type = mtype;
749 red->dmetric.value = mvalue;
750
751 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
752
753 if (ospf_is_type_redistributed(ospf, DEFAULT_ROUTE, 0)) {
754 /* if ospf->default_originate changes value, is calling
755 ospf_external_lsa_refresh_default sufficient to implement
756 the change? */
757 ospf_external_lsa_refresh_default(ospf);
758
759 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
760 zlog_debug(
761 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
762 ospf_redist_string(DEFAULT_ROUTE),
763 metric_type(ospf, DEFAULT_ROUTE, 0),
764 metric_value(ospf, DEFAULT_ROUTE, 0));
765 return CMD_SUCCESS;
766 }
767
768 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient,
769 ospf->vrf_id);
770
771 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
772 zlog_debug("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
773 metric_type(ospf, DEFAULT_ROUTE, 0),
774 metric_value(ospf, DEFAULT_ROUTE, 0));
775
776 if (ospf->router_id.s_addr == 0)
777 ospf->external_origin |= (1 << DEFAULT_ROUTE);
778 else
779 thread_add_timer(master, ospf_default_originate_timer, ospf, 1,
780 NULL);
781
782 ospf_asbr_status_update(ospf, ++ospf->redistribute);
783
784 return CMD_SUCCESS;
785 }
786
787 int ospf_redistribute_default_unset(struct ospf *ospf)
788 {
789 if (!ospf_is_type_redistributed(ospf, DEFAULT_ROUTE, 0))
790 return CMD_SUCCESS;
791
792 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
793 ospf_redist_del(ospf, DEFAULT_ROUTE, 0);
794
795 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient,
796 ospf->vrf_id);
797
798 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
799 zlog_debug("Redistribute[DEFAULT]: Stop");
800
801 // Pending: how does the external_info cleanup work in this case?
802
803 ospf_asbr_status_update(ospf, --ospf->redistribute);
804
805 return CMD_SUCCESS;
806 }
807
808 static int ospf_external_lsa_originate_check(struct ospf *ospf,
809 struct external_info *ei)
810 {
811 /* If prefix is multicast, then do not originate LSA. */
812 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
813 zlog_info(
814 "LSA[Type5:%s]: Not originate AS-external-LSA, "
815 "Prefix belongs multicast",
816 inet_ntoa(ei->p.prefix));
817 return 0;
818 }
819
820 /* Take care of default-originate. */
821 if (is_prefix_default(&ei->p))
822 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
823 zlog_info(
824 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
825 "for default");
826 return 0;
827 }
828
829 return 1;
830 }
831
832 /* If connected prefix is OSPF enable interface, then do not announce. */
833 int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
834 {
835 struct listnode *node;
836 struct ospf_interface *oi;
837
838
839 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
840 if (prefix_match(oi->address, (struct prefix *)&ei->p))
841 return 0;
842 return 1;
843 }
844
845 /* return 1 if external LSA must be originated, 0 otherwise */
846 int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
847 int *changed)
848 {
849 struct route_map_set_values save_values;
850 struct prefix_ipv4 *p = &ei->p;
851 struct ospf_redist *red;
852 uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
853 unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
854
855 if (changed)
856 *changed = 0;
857
858 if (!ospf_external_lsa_originate_check(ospf, ei))
859 return 0;
860
861 /* Take care connected route. */
862 if (type == ZEBRA_ROUTE_CONNECT
863 && !ospf_distribute_check_connected(ospf, ei))
864 return 0;
865
866 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
867 /* distirbute-list exists, but access-list may not? */
868 if (DISTRIBUTE_LIST(ospf, type))
869 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
870 == FILTER_DENY) {
871 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
872 zlog_debug(
873 "Redistribute[%s]: %s/%d filtered by ditribute-list.",
874 ospf_redist_string(type),
875 inet_ntoa(p->prefix),
876 p->prefixlen);
877 return 0;
878 }
879
880 save_values = ei->route_map_set;
881 ospf_reset_route_map_set_values(&ei->route_map_set);
882
883 /* apply route-map if needed */
884 red = ospf_redist_lookup(ospf, type, instance);
885 if (red && ROUTEMAP_NAME(red)) {
886 int ret;
887
888 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
889 RMAP_OSPF, ei);
890
891 if (ret == RMAP_DENYMATCH) {
892 ei->route_map_set = save_values;
893 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
894 zlog_debug(
895 "Redistribute[%s]: %s/%d filtered by route-map.",
896 ospf_redist_string(type),
897 inet_ntoa(p->prefix), p->prefixlen);
898 return 0;
899 }
900
901 /* check if 'route-map set' changed something */
902 if (changed)
903 *changed = !ospf_route_map_set_compare(
904 &ei->route_map_set, &save_values);
905 }
906
907 return 1;
908 }
909
910 /* OSPF route-map set for redistribution */
911 void ospf_routemap_set(struct ospf_redist *red, const char *name)
912 {
913 if (ROUTEMAP_NAME(red))
914 free(ROUTEMAP_NAME(red));
915
916 ROUTEMAP_NAME(red) = strdup(name);
917 ROUTEMAP(red) = route_map_lookup_by_name(name);
918 }
919
920 void ospf_routemap_unset(struct ospf_redist *red)
921 {
922 if (ROUTEMAP_NAME(red))
923 free(ROUTEMAP_NAME(red));
924
925 ROUTEMAP_NAME(red) = NULL;
926 ROUTEMAP(red) = NULL;
927 }
928
929 /* Zebra route add and delete treatment. */
930 static int ospf_zebra_read_route(int command, struct zclient *zclient,
931 zebra_size_t length, vrf_id_t vrf_id)
932 {
933 struct zapi_route api;
934 struct prefix_ipv4 p;
935 unsigned long ifindex;
936 struct in_addr nexthop;
937 struct external_info *ei;
938 struct ospf *ospf;
939 int i;
940
941 ospf = ospf_lookup_by_vrf_id(vrf_id);
942 if (ospf == NULL)
943 return 0;
944
945 if (zapi_route_decode(zclient->ibuf, &api) < 0)
946 return -1;
947
948 ifindex = api.nexthops[0].ifindex;
949 nexthop = api.nexthops[0].gate.ipv4;
950
951 memcpy(&p, &api.prefix, sizeof(p));
952 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
953 return 0;
954
955 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
956 char buf_prefix[PREFIX_STRLEN];
957 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
958
959 zlog_debug("%s: from client %s: vrf_id %d, p %s", __func__,
960 zebra_route_string(api.type), vrf_id, buf_prefix);
961 }
962
963 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
964 /* XXX|HACK|TODO|FIXME:
965 * Maybe we should ignore reject/blackhole routes? Testing
966 * shows that there is no problems though and this is only way
967 * to "summarize" routes in ASBR at the moment. Maybe we need
968 * just a better generalised solution for these types?
969 */
970
971 /* Protocol tag overwrites all other tag value sent by zebra */
972 if (ospf->dtag[api.type] > 0)
973 api.tag = ospf->dtag[api.type];
974
975 /*
976 * Given zebra sends update for a prefix via ADD message, it
977 * should
978 * be considered as an implicit DEL for that prefix with other
979 * source
980 * types.
981 */
982 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
983 if (i != api.type)
984 ospf_external_info_delete(ospf, i, api.instance,
985 p);
986
987 ei = ospf_external_info_add(ospf, api.type, api.instance, p,
988 ifindex, nexthop, api.tag);
989 if (ei == NULL) {
990 /* Nothing has changed, so nothing to do; return */
991 return 0;
992 }
993 if (ospf->router_id.s_addr == 0)
994 /* Set flags to generate AS-external-LSA originate event
995 for each redistributed protocols later. */
996 ospf->external_origin |= (1 << api.type);
997 else {
998 if (ei) {
999 if (is_prefix_default(&p))
1000 ospf_external_lsa_refresh_default(ospf);
1001 else {
1002 struct ospf_lsa *current;
1003
1004 current = ospf_external_info_find_lsa(
1005 ospf, &ei->p);
1006 if (!current)
1007 ospf_external_lsa_originate(
1008 ospf, ei);
1009 else {
1010 if (IS_DEBUG_OSPF(
1011 zebra,
1012 ZEBRA_REDISTRIBUTE))
1013 zlog_debug(
1014 "ospf_zebra_read_route() : %s refreshing LSA",
1015 inet_ntoa(
1016 p.prefix));
1017 ospf_external_lsa_refresh(
1018 ospf, current, ei,
1019 LSA_REFRESH_FORCE);
1020 }
1021 }
1022 }
1023 }
1024 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1025 {
1026 ospf_external_info_delete(ospf, api.type, api.instance, p);
1027 if (is_prefix_default(&p))
1028 ospf_external_lsa_refresh_default(ospf);
1029 else
1030 ospf_external_lsa_flush(ospf, api.type, &p,
1031 ifindex /*, nexthop */);
1032 }
1033
1034 return 0;
1035 }
1036
1037
1038 int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
1039 {
1040 /* Lookup access-list for distribute-list. */
1041 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
1042
1043 /* Clear previous distribute-name. */
1044 if (DISTRIBUTE_NAME(ospf, type))
1045 free(DISTRIBUTE_NAME(ospf, type));
1046
1047 /* Set distribute-name. */
1048 DISTRIBUTE_NAME(ospf, type) = strdup(name);
1049
1050 /* If access-list have been set, schedule update timer. */
1051 if (DISTRIBUTE_LIST(ospf, type))
1052 ospf_distribute_list_update(ospf, type, 0);
1053
1054 return CMD_SUCCESS;
1055 }
1056
1057 int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1058 const char *name)
1059 {
1060 /* Schedule update timer. */
1061 if (DISTRIBUTE_LIST(ospf, type))
1062 ospf_distribute_list_update(ospf, type, 0);
1063
1064 /* Unset distribute-list. */
1065 DISTRIBUTE_LIST(ospf, type) = NULL;
1066
1067 /* Clear distribute-name. */
1068 if (DISTRIBUTE_NAME(ospf, type))
1069 free(DISTRIBUTE_NAME(ospf, type));
1070
1071 DISTRIBUTE_NAME(ospf, type) = NULL;
1072
1073 return CMD_SUCCESS;
1074 }
1075
1076 /* distribute-list update timer. */
1077 static int ospf_distribute_list_update_timer(struct thread *thread)
1078 {
1079 struct route_node *rn;
1080 struct external_info *ei;
1081 struct route_table *rt;
1082 struct ospf_lsa *lsa;
1083 int type, default_refresh = 0, arg_type;
1084 struct ospf *ospf = NULL;
1085 void **arg = THREAD_ARG(thread);
1086
1087 ospf = (struct ospf *)arg[0];
1088 arg_type = (int)(intptr_t)arg[1];
1089
1090 if (ospf == NULL)
1091 return 0;
1092
1093 ospf->t_distribute_update = NULL;
1094
1095 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1096
1097 if (IS_DEBUG_OSPF_EVENT) {
1098 zlog_debug(
1099 "%s: ospf distribute-list update arg_type %d vrf %s id %d",
1100 __PRETTY_FUNCTION__, arg_type,
1101 ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id);
1102 }
1103
1104 /* foreach all external info. */
1105 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1106 struct list *ext_list;
1107 struct listnode *node;
1108 struct ospf_external *ext;
1109
1110 ext_list = ospf->external[type];
1111 if (!ext_list)
1112 continue;
1113
1114 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1115 rt = ext->external_info;
1116 if (!rt)
1117 continue;
1118 for (rn = route_top(rt); rn; rn = route_next(rn))
1119 if ((ei = rn->info) != NULL) {
1120 if (is_prefix_default(&ei->p))
1121 default_refresh = 1;
1122 else if (
1123 (lsa = ospf_external_info_find_lsa(
1124 ospf, &ei->p)))
1125 ospf_external_lsa_refresh(
1126 ospf, lsa, ei,
1127 LSA_REFRESH_IF_CHANGED);
1128 else
1129 ospf_external_lsa_originate(
1130 ospf, ei);
1131 }
1132 }
1133 }
1134 if (default_refresh)
1135 ospf_external_lsa_refresh_default(ospf);
1136
1137 XFREE(MTYPE_OSPF_DIST_ARGS, arg);
1138 return 0;
1139 }
1140
1141 /* Update distribute-list and set timer to apply access-list. */
1142 void ospf_distribute_list_update(struct ospf *ospf, int type,
1143 unsigned short instance)
1144 {
1145 struct route_table *rt;
1146 struct ospf_external *ext;
1147 void **args = XCALLOC(MTYPE_OSPF_DIST_ARGS, sizeof(void *) * 2);
1148
1149 args[0] = ospf;
1150 args[1] = (void *)((ptrdiff_t)type);
1151
1152 /* External info does not exist. */
1153 ext = ospf_external_lookup(ospf, type, instance);
1154 if (!ext || !(rt = EXTERNAL_INFO(ext))) {
1155 XFREE(MTYPE_OSPF_DIST_ARGS, args);
1156 return;
1157 }
1158
1159 /* If exists previously invoked thread, then let it continue. */
1160 if (ospf->t_distribute_update) {
1161 XFREE(MTYPE_OSPF_DIST_ARGS, args);
1162 return;
1163 }
1164
1165 /* Set timer. */
1166 ospf->t_distribute_update = NULL;
1167 thread_add_timer_msec(master, ospf_distribute_list_update_timer,
1168 (void **)args, ospf->min_ls_interval,
1169 &ospf->t_distribute_update);
1170 }
1171
1172 /* If access-list is updated, apply some check. */
1173 static void ospf_filter_update(struct access_list *access)
1174 {
1175 struct ospf *ospf;
1176 int type;
1177 int abr_inv = 0;
1178 struct ospf_area *area;
1179 struct listnode *node, *n1;
1180
1181 /* If OSPF instance does not exist, return right now. */
1182 if (listcount(om->ospf) == 0)
1183 return;
1184
1185 /* Iterate all ospf [VRF] instances */
1186 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1187 /* Update distribute-list, and apply filter. */
1188 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1189 struct list *red_list;
1190 struct listnode *node;
1191 struct ospf_redist *red;
1192
1193 red_list = ospf->redist[type];
1194 if (red_list)
1195 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1196 red)) {
1197 if (ROUTEMAP(red)) {
1198 /* if route-map is not NULL it
1199 * may be
1200 * using this access list */
1201 ospf_distribute_list_update(
1202 ospf, type,
1203 red->instance);
1204 }
1205 }
1206
1207 /* There is place for route-map for default-information
1208 * (ZEBRA_ROUTE_MAX),
1209 * but no distribute list. */
1210 if (type == ZEBRA_ROUTE_MAX)
1211 break;
1212
1213 if (DISTRIBUTE_NAME(ospf, type)) {
1214 /* Keep old access-list for distribute-list. */
1215 struct access_list *old =
1216 DISTRIBUTE_LIST(ospf, type);
1217
1218 /* Update access-list for distribute-list. */
1219 DISTRIBUTE_LIST(ospf, type) =
1220 access_list_lookup(
1221 AFI_IP,
1222 DISTRIBUTE_NAME(ospf, type));
1223
1224 /* No update for this distribute type. */
1225 if (old == NULL
1226 && DISTRIBUTE_LIST(ospf, type) == NULL)
1227 continue;
1228
1229 /* Schedule distribute-list update timer. */
1230 if (DISTRIBUTE_LIST(ospf, type) == NULL
1231 || strcmp(DISTRIBUTE_NAME(ospf, type),
1232 access->name)
1233 == 0)
1234 ospf_distribute_list_update(ospf, type,
1235 0);
1236 }
1237 }
1238
1239 /* Update Area access-list. */
1240 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1241 if (EXPORT_NAME(area)) {
1242 EXPORT_LIST(area) = NULL;
1243 abr_inv++;
1244 }
1245
1246 if (IMPORT_NAME(area)) {
1247 IMPORT_LIST(area) = NULL;
1248 abr_inv++;
1249 }
1250 }
1251
1252 /* Schedule ABR tasks -- this will be changed -- takada. */
1253 if (IS_OSPF_ABR(ospf) && abr_inv)
1254 ospf_schedule_abr_task(ospf);
1255 }
1256 }
1257
1258 /* If prefix-list is updated, do some updates. */
1259 void ospf_prefix_list_update(struct prefix_list *plist)
1260 {
1261 struct ospf *ospf = NULL;
1262 int type;
1263 int abr_inv = 0;
1264 struct ospf_area *area;
1265 struct listnode *node, *n1;
1266
1267 /* If OSPF instatnce does not exist, return right now. */
1268 if (listcount(om->ospf) == 0)
1269 return;
1270
1271 /* Iterate all ospf [VRF] instances */
1272 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1273
1274 /* Update all route-maps which are used
1275 * as redistribution filters.
1276 * They might use prefix-list.
1277 */
1278 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1279 struct list *red_list;
1280 struct listnode *node;
1281 struct ospf_redist *red;
1282
1283 red_list = ospf->redist[type];
1284 if (red_list) {
1285 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1286 red)) {
1287 if (ROUTEMAP(red)) {
1288 /* if route-map is not NULL
1289 * it may be using
1290 * this prefix list */
1291 ospf_distribute_list_update(
1292 ospf, type,
1293 red->instance);
1294 }
1295 }
1296 }
1297 }
1298
1299 /* Update area filter-lists. */
1300 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1301 /* Update filter-list in. */
1302 if (PREFIX_NAME_IN(area))
1303 if (strcmp(PREFIX_NAME_IN(area),
1304 prefix_list_name(plist))
1305 == 0) {
1306 PREFIX_LIST_IN(area) =
1307 prefix_list_lookup(
1308 AFI_IP,
1309 PREFIX_NAME_IN(area));
1310 abr_inv++;
1311 }
1312
1313 /* Update filter-list out. */
1314 if (PREFIX_NAME_OUT(area))
1315 if (strcmp(PREFIX_NAME_OUT(area),
1316 prefix_list_name(plist))
1317 == 0) {
1318 PREFIX_LIST_IN(area) =
1319 prefix_list_lookup(
1320 AFI_IP,
1321 PREFIX_NAME_OUT(area));
1322 abr_inv++;
1323 }
1324 }
1325
1326 /* Schedule ABR task. */
1327 if (IS_OSPF_ABR(ospf) && abr_inv)
1328 ospf_schedule_abr_task(ospf);
1329 }
1330 }
1331
1332 static struct ospf_distance *ospf_distance_new(void)
1333 {
1334 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
1335 }
1336
1337 static void ospf_distance_free(struct ospf_distance *odistance)
1338 {
1339 XFREE(MTYPE_OSPF_DISTANCE, odistance);
1340 }
1341
1342 int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1343 const char *distance_str, const char *ip_str,
1344 const char *access_list_str)
1345 {
1346 int ret;
1347 struct prefix_ipv4 p;
1348 uint8_t distance;
1349 struct route_node *rn;
1350 struct ospf_distance *odistance;
1351
1352 ret = str2prefix_ipv4(ip_str, &p);
1353 if (ret == 0) {
1354 vty_out(vty, "Malformed prefix\n");
1355 return CMD_WARNING_CONFIG_FAILED;
1356 }
1357
1358 distance = atoi(distance_str);
1359
1360 /* Get OSPF distance node. */
1361 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1362 if (rn->info) {
1363 odistance = rn->info;
1364 route_unlock_node(rn);
1365 } else {
1366 odistance = ospf_distance_new();
1367 rn->info = odistance;
1368 }
1369
1370 /* Set distance value. */
1371 odistance->distance = distance;
1372
1373 /* Reset access-list configuration. */
1374 if (odistance->access_list) {
1375 free(odistance->access_list);
1376 odistance->access_list = NULL;
1377 }
1378 if (access_list_str)
1379 odistance->access_list = strdup(access_list_str);
1380
1381 return CMD_SUCCESS;
1382 }
1383
1384 int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1385 const char *distance_str, const char *ip_str,
1386 char const *access_list_str)
1387 {
1388 int ret;
1389 struct prefix_ipv4 p;
1390 struct route_node *rn;
1391 struct ospf_distance *odistance;
1392
1393 ret = str2prefix_ipv4(ip_str, &p);
1394 if (ret == 0) {
1395 vty_out(vty, "Malformed prefix\n");
1396 return CMD_WARNING_CONFIG_FAILED;
1397 }
1398
1399 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1400 if (!rn) {
1401 vty_out(vty, "Can't find specified prefix\n");
1402 return CMD_WARNING_CONFIG_FAILED;
1403 }
1404
1405 odistance = rn->info;
1406
1407 if (odistance->access_list)
1408 free(odistance->access_list);
1409 ospf_distance_free(odistance);
1410
1411 rn->info = NULL;
1412 route_unlock_node(rn);
1413 route_unlock_node(rn);
1414
1415 return CMD_SUCCESS;
1416 }
1417
1418 void ospf_distance_reset(struct ospf *ospf)
1419 {
1420 struct route_node *rn;
1421 struct ospf_distance *odistance;
1422
1423 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn))
1424 if ((odistance = rn->info) != NULL) {
1425 if (odistance->access_list)
1426 free(odistance->access_list);
1427 ospf_distance_free(odistance);
1428 rn->info = NULL;
1429 route_unlock_node(rn);
1430 }
1431 }
1432
1433 uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1434 struct ospf_route * or)
1435 {
1436
1437 if (ospf == NULL)
1438 return 0;
1439
1440 if (ospf->distance_intra)
1441 if (or->path_type == OSPF_PATH_INTRA_AREA)
1442 return ospf->distance_intra;
1443
1444 if (ospf->distance_inter)
1445 if (or->path_type == OSPF_PATH_INTER_AREA)
1446 return ospf->distance_inter;
1447
1448 if (ospf->distance_external)
1449 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1450 or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1451 return ospf->distance_external;
1452
1453 if (ospf->distance_all)
1454 return ospf->distance_all;
1455
1456 return 0;
1457 }
1458
1459 void ospf_zebra_vrf_register(struct ospf *ospf)
1460 {
1461 if (!zclient || zclient->sock < 0 || !ospf)
1462 return;
1463
1464 if (ospf->vrf_id != VRF_UNKNOWN) {
1465 if (IS_DEBUG_OSPF_EVENT)
1466 zlog_debug("%s: Register VRF %s id %u",
1467 __PRETTY_FUNCTION__,
1468 ospf_vrf_id_to_name(ospf->vrf_id),
1469 ospf->vrf_id);
1470 zclient_send_reg_requests(zclient, ospf->vrf_id);
1471 }
1472 }
1473
1474 void ospf_zebra_vrf_deregister(struct ospf *ospf)
1475 {
1476 if (!zclient || zclient->sock < 0 || !ospf)
1477 return;
1478
1479 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1480 if (IS_DEBUG_OSPF_EVENT)
1481 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1482 __PRETTY_FUNCTION__,
1483 ospf_vrf_id_to_name(ospf->vrf_id),
1484 ospf->vrf_id);
1485 /* Deregister for router-id, interfaces,
1486 * redistributed routes. */
1487 zclient_send_dereg_requests(zclient, ospf->vrf_id);
1488 }
1489 }
1490 static void ospf_zebra_connected(struct zclient *zclient)
1491 {
1492 /* Send the client registration */
1493 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
1494
1495 zclient_send_reg_requests(zclient, VRF_DEFAULT);
1496 }
1497
1498 void ospf_zebra_init(struct thread_master *master, unsigned short instance)
1499 {
1500 /* Allocate zebra structure. */
1501 zclient = zclient_new_notify(master, &zclient_options_default);
1502 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
1503 zclient->zebra_connected = ospf_zebra_connected;
1504 zclient->router_id_update = ospf_router_id_update_zebra;
1505 zclient->interface_add = ospf_interface_add;
1506 zclient->interface_delete = ospf_interface_delete;
1507 zclient->interface_up = ospf_interface_state_up;
1508 zclient->interface_down = ospf_interface_state_down;
1509 zclient->interface_address_add = ospf_interface_address_add;
1510 zclient->interface_address_delete = ospf_interface_address_delete;
1511 zclient->interface_link_params = ospf_interface_link_params;
1512 zclient->interface_vrf_update = ospf_interface_vrf_update;
1513
1514 zclient->redistribute_route_add = ospf_zebra_read_route;
1515 zclient->redistribute_route_del = ospf_zebra_read_route;
1516
1517 access_list_add_hook(ospf_filter_update);
1518 access_list_delete_hook(ospf_filter_update);
1519 prefix_list_add_hook(ospf_prefix_list_update);
1520 prefix_list_delete_hook(ospf_prefix_list_update);
1521 }