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