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