2 * Zebra connect library for OSPFd
3 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
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
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.
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
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"
54 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_EXTERNAL
, "OSPF External route table")
55 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_REDISTRIBUTE
, "OSPF Redistriute")
57 DEFINE_HOOK(ospf_if_update
, (struct interface
* ifp
), (ifp
))
58 DEFINE_HOOK(ospf_if_delete
, (struct interface
* ifp
), (ifp
))
60 /* Zebra structure to hold current status. */
61 struct zclient
*zclient
= NULL
;
63 /* For registering threads. */
64 extern struct thread_master
*master
;
65 struct in_addr router_id_zebra
;
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
)
72 struct prefix router_id
;
73 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
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", buf
);
81 router_id_zebra
= router_id
.u
.prefix4
;
86 ospf_router_id_update(ospf
);
91 /* Inteface addition message from zebra. */
92 static int ospf_interface_add(int command
, struct zclient
*zclient
,
93 zebra_size_t length
, vrf_id_t vrf_id
)
95 struct interface
*ifp
;
97 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
99 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
101 "Zebra: interface add %s[%u] index %d flags %llx metric %d mtu %d",
102 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
103 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
107 if (!OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp
), type
)) {
108 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), type
);
109 IF_DEF_PARAMS(ifp
)->type
= ospf_default_iftype(ifp
);
112 ospf_if_update(NULL
, ifp
);
114 hook_call(ospf_if_update
, ifp
);
119 static int ospf_interface_delete(int command
, struct zclient
*zclient
,
120 zebra_size_t length
, vrf_id_t vrf_id
)
122 struct interface
*ifp
;
124 struct route_node
*rn
;
127 /* zebra_interface_state_read() updates interface structure in iflist */
128 ifp
= zebra_interface_state_read(s
, vrf_id
);
134 zlog_warn("Zebra: got delete of %s, but interface is still up",
137 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
139 "Zebra: interface delete %s[%u] index %d flags %llx metric %d mtu %d",
140 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
141 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
143 hook_call(ospf_if_delete
, ifp
);
145 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
))
147 ospf_if_free((struct ospf_interface
*)rn
->info
);
149 ifp
->ifindex
= IFINDEX_DELETED
;
153 static struct interface
*zebra_interface_if_lookup(struct stream
*s
,
156 char ifname_tmp
[INTERFACE_NAMSIZ
];
158 /* Read interface name. */
159 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
161 /* And look it up. */
162 return if_lookup_by_name_len(
163 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), VRF_DEFAULT
);
166 static int ospf_interface_state_up(int command
, struct zclient
*zclient
,
167 zebra_size_t length
, vrf_id_t vrf_id
)
169 struct interface
*ifp
;
170 struct ospf_interface
*oi
;
171 struct route_node
*rn
;
173 ifp
= zebra_interface_if_lookup(zclient
->ibuf
, vrf_id
);
178 /* Interface is already up. */
179 if (if_is_operative(ifp
)) {
180 /* Temporarily keep ifp values. */
181 struct interface if_tmp
;
182 memcpy(&if_tmp
, ifp
, sizeof(struct interface
));
184 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
186 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
188 "Zebra: Interface[%s] state update speed %u -> %u, bw %d -> %d",
189 ifp
->name
, if_tmp
.speed
, ifp
->speed
,
190 if_tmp
.bandwidth
, ifp
->bandwidth
);
192 ospf_if_recalculate_output_cost(ifp
);
194 if (if_tmp
.mtu
!= ifp
->mtu
) {
195 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
197 "Zebra: Interface[%s] MTU change %u -> %u.",
198 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
200 /* Must reset the interface (simulate down/up) when MTU
207 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
209 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
210 zlog_debug("Zebra: Interface[%s] state change to up.",
213 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
214 if ((oi
= rn
->info
) == NULL
)
223 static int ospf_interface_state_down(int command
, struct zclient
*zclient
,
224 zebra_size_t length
, vrf_id_t vrf_id
)
226 struct interface
*ifp
;
227 struct ospf_interface
*oi
;
228 struct route_node
*node
;
230 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
235 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
236 zlog_debug("Zebra: Interface[%s] state change to down.",
239 for (node
= route_top(IF_OIFS(ifp
)); node
; node
= route_next(node
)) {
240 if ((oi
= node
->info
) == NULL
)
248 static int ospf_interface_address_add(int command
, struct zclient
*zclient
,
249 zebra_size_t length
, vrf_id_t vrf_id
)
253 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
258 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
)) {
259 char buf
[PREFIX2STR_BUFFER
];
260 prefix2str(c
->address
, buf
, sizeof(buf
));
261 zlog_debug("Zebra: interface %s address add %s", c
->ifp
->name
,
265 ospf_if_update(NULL
, c
->ifp
);
267 hook_call(ospf_if_update
, c
->ifp
);
272 static int ospf_interface_address_delete(int command
, struct zclient
*zclient
,
273 zebra_size_t length
, vrf_id_t vrf_id
)
276 struct interface
*ifp
;
277 struct ospf_interface
*oi
;
278 struct route_node
*rn
;
281 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
286 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
)) {
287 char buf
[PREFIX2STR_BUFFER
];
288 prefix2str(c
->address
, buf
, sizeof(buf
));
289 zlog_debug("Zebra: interface %s address delete %s",
295 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
297 rn
= route_node_lookup(IF_OIFS(ifp
), &p
);
305 route_unlock_node(rn
);
307 /* Call interface hook functions to clean up */
310 hook_call(ospf_if_update
, c
->ifp
);
317 static int ospf_interface_link_params(int command
, struct zclient
*zclient
,
320 struct interface
*ifp
;
322 ifp
= zebra_interface_link_params_read(zclient
->ibuf
);
328 ospf_mpls_te_update_if(ifp
);
334 void ospf_zebra_add(struct prefix_ipv4
*p
, struct ospf_route
* or)
336 struct zapi_route api
;
337 struct zapi_nexthop
*api_nh
;
339 struct ospf_path
*path
;
340 struct listnode
*node
;
341 struct ospf
*ospf
= ospf_lookup();
344 memset(&api
, 0, sizeof(api
));
345 api
.vrf_id
= VRF_DEFAULT
;
346 api
.type
= ZEBRA_ROUTE_OSPF
;
347 api
.instance
= ospf
->instance
;
348 api
.safi
= SAFI_UNICAST
;
350 memcpy(&api
.prefix
, p
, sizeof(*p
));
351 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
352 api
.nexthop_num
= or->paths
->count
;
355 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
356 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
357 api
.metric
= or->cost
+ or->u
.ext
.type2_cost
;
358 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
359 api
.metric
= or->u
.ext
.type2_cost
;
361 api
.metric
= or->cost
;
363 /* Check if path type is ASE */
364 if (((or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
365 || (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
))
366 && (or->u
.ext
.tag
> 0) && (or->u
.ext
.tag
<= ROUTE_TAG_MAX
)) {
367 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
368 api
.tag
= or->u
.ext
.tag
;
371 /* Distance value. */
372 distance
= ospf_distance_apply(p
, or);
374 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
375 api
.distance
= distance
;
378 /* Nexthop, ifindex, distance and metric information. */
379 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
)) {
380 api_nh
= &api
.nexthops
[count
];
382 if (path
->unnumbered
|| (path
->nexthop
.s_addr
!= INADDR_ANY
383 && path
->ifindex
!= 0)) {
384 #else /* HAVE_NETLINK */
385 if (path
->nexthop
.s_addr
!= INADDR_ANY
&& path
->ifindex
!= 0) {
386 #endif /* HAVE_NETLINK */
387 api_nh
->gate
.ipv4
= path
->nexthop
;
388 api_nh
->ifindex
= path
->ifindex
;
389 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
390 } else if (path
->nexthop
.s_addr
!= INADDR_ANY
) {
391 api_nh
->gate
.ipv4
= path
->nexthop
;
392 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
394 api_nh
->ifindex
= path
->ifindex
;
395 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
399 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
400 char buf
[2][INET_ADDRSTRLEN
];
402 "Zebra: Route add %s/%d nexthop %s, ifindex=%d",
403 inet_ntop(AF_INET
, &p
->prefix
, buf
[0],
405 p
->prefixlen
, inet_ntop(AF_INET
, &path
->nexthop
,
406 buf
[1], sizeof(buf
[1])),
411 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
414 void ospf_zebra_delete(struct prefix_ipv4
*p
, struct ospf_route
* or)
416 struct zapi_route api
;
417 struct ospf
*ospf
= ospf_lookup();
419 memset(&api
, 0, sizeof(api
));
420 api
.vrf_id
= VRF_DEFAULT
;
421 api
.type
= ZEBRA_ROUTE_OSPF
;
422 api
.instance
= ospf
->instance
;
423 api
.safi
= SAFI_UNICAST
;
424 memcpy(&api
.prefix
, p
, sizeof(*p
));
426 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
427 char buf
[INET_ADDRSTRLEN
];
428 zlog_debug("Zebra: Route delete %s/%d",
429 inet_ntop(AF_INET
, &p
->prefix
, buf
, sizeof(buf
[0])),
433 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
436 void ospf_zebra_add_discard(struct prefix_ipv4
*p
)
438 struct zapi_route api
;
439 struct ospf
*ospf
= ospf_lookup();
441 memset(&api
, 0, sizeof(api
));
442 api
.vrf_id
= VRF_DEFAULT
;
443 api
.type
= ZEBRA_ROUTE_OSPF
;
444 api
.instance
= ospf
->instance
;
445 api
.safi
= SAFI_UNICAST
;
446 memcpy(&api
.prefix
, p
, sizeof(*p
));
447 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
449 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
451 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
452 zlog_debug("Zebra: Route add discard %s/%d",
453 inet_ntoa(p
->prefix
), p
->prefixlen
);
456 void ospf_zebra_delete_discard(struct prefix_ipv4
*p
)
458 struct zapi_route api
;
459 struct ospf
*ospf
= ospf_lookup();
461 memset(&api
, 0, sizeof(api
));
462 api
.vrf_id
= VRF_DEFAULT
;
463 api
.type
= ZEBRA_ROUTE_OSPF
;
464 api
.instance
= ospf
->instance
;
465 api
.safi
= SAFI_UNICAST
;
466 memcpy(&api
.prefix
, p
, sizeof(*p
));
467 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
469 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
471 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
472 zlog_debug("Zebra: Route delete discard %s/%d",
473 inet_ntoa(p
->prefix
), p
->prefixlen
);
476 struct ospf_external
*ospf_external_lookup(u_char type
, u_short instance
)
478 struct list
*ext_list
;
479 struct listnode
*node
;
480 struct ospf_external
*ext
;
482 ext_list
= om
->external
[type
];
486 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
487 if (ext
->instance
== instance
)
493 struct ospf_external
*ospf_external_add(u_char type
, u_short instance
)
495 struct list
*ext_list
;
496 struct ospf_external
*ext
;
498 ext
= ospf_external_lookup(type
, instance
);
502 if (!om
->external
[type
])
503 om
->external
[type
] = list_new();
505 ext_list
= om
->external
[type
];
506 ext
= (struct ospf_external
*)XCALLOC(MTYPE_OSPF_EXTERNAL
,
507 sizeof(struct ospf_external
));
508 ext
->instance
= instance
;
509 EXTERNAL_INFO(ext
) = route_table_init();
511 listnode_add(ext_list
, ext
);
516 void ospf_external_del(u_char type
, u_short instance
)
518 struct ospf_external
*ext
;
520 ext
= ospf_external_lookup(type
, instance
);
523 if (EXTERNAL_INFO(ext
))
524 route_table_finish(EXTERNAL_INFO(ext
));
526 listnode_delete(om
->external
[type
], ext
);
527 if (!om
->external
[type
]->count
) {
528 list_free(om
->external
[type
]);
529 om
->external
[type
] = NULL
;
531 XFREE(MTYPE_OSPF_EXTERNAL
, ext
);
535 struct ospf_redist
*ospf_redist_lookup(struct ospf
*ospf
, u_char type
,
538 struct list
*red_list
;
539 struct listnode
*node
;
540 struct ospf_redist
*red
;
542 red_list
= ospf
->redist
[type
];
546 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
547 if (red
->instance
== instance
)
553 struct ospf_redist
*ospf_redist_add(struct ospf
*ospf
, u_char type
,
556 struct list
*red_list
;
557 struct ospf_redist
*red
;
559 red
= ospf_redist_lookup(ospf
, type
, instance
);
563 if (!ospf
->redist
[type
])
564 ospf
->redist
[type
] = list_new();
566 red_list
= ospf
->redist
[type
];
567 red
= (struct ospf_redist
*)XCALLOC(MTYPE_OSPF_REDISTRIBUTE
,
568 sizeof(struct ospf_redist
));
569 red
->instance
= instance
;
570 red
->dmetric
.type
= -1;
571 red
->dmetric
.value
= -1;
573 listnode_add(red_list
, red
);
578 void ospf_redist_del(struct ospf
*ospf
, u_char type
, u_short instance
)
580 struct ospf_redist
*red
;
582 red
= ospf_redist_lookup(ospf
, type
, instance
);
585 listnode_delete(ospf
->redist
[type
], red
);
586 if (!ospf
->redist
[type
]->count
) {
587 list_free(ospf
->redist
[type
]);
588 ospf
->redist
[type
] = NULL
;
590 ospf_routemap_unset(red
);
591 XFREE(MTYPE_OSPF_REDISTRIBUTE
, red
);
596 int ospf_is_type_redistributed(int type
, u_short instance
)
598 return (DEFAULT_ROUTE_TYPE(type
)
599 ? vrf_bitmap_check(zclient
->default_information
,
602 && redist_check_instance(
603 &zclient
->mi_redist
[AFI_IP
][type
],
607 zclient
->redist
[AFI_IP
][type
],
611 int ospf_redistribute_set(struct ospf
*ospf
, int type
, u_short instance
,
612 int mtype
, int mvalue
)
615 struct ospf_redist
*red
;
617 red
= ospf_redist_lookup(ospf
, type
, instance
);
618 if (ospf_is_type_redistributed(type
, instance
)) {
619 if (mtype
!= red
->dmetric
.type
) {
620 red
->dmetric
.type
= mtype
;
621 force
= LSA_REFRESH_FORCE
;
623 if (mvalue
!= red
->dmetric
.value
) {
624 red
->dmetric
.value
= mvalue
;
625 force
= LSA_REFRESH_FORCE
;
628 ospf_external_lsa_refresh_type(ospf
, type
, instance
, force
);
630 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
632 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
633 ospf_redist_string(type
), instance
,
634 metric_type(ospf
, type
, instance
),
635 metric_value(ospf
, type
, instance
));
640 red
->dmetric
.type
= mtype
;
641 red
->dmetric
.value
= mvalue
;
643 ospf_external_add(type
, instance
);
645 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
646 instance
, VRF_DEFAULT
);
648 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
649 zlog_debug("Redistribute[%s][%d]: Start Type[%d], Metric[%d]",
650 ospf_redist_string(type
), instance
,
651 metric_type(ospf
, type
, instance
),
652 metric_value(ospf
, type
, instance
));
654 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
659 int ospf_redistribute_unset(struct ospf
*ospf
, int type
, u_short instance
)
661 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
664 if (!ospf_is_type_redistributed(type
, instance
))
667 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
,
668 instance
, VRF_DEFAULT
);
670 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
671 zlog_debug("Redistribute[%s][%d]: Stop",
672 ospf_redist_string(type
), instance
);
674 ospf_redist_del(ospf
, type
, instance
);
676 /* Remove the routes from OSPF table. */
677 ospf_redistribute_withdraw(ospf
, type
, instance
);
679 ospf_external_del(type
, instance
);
681 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
686 int ospf_redistribute_default_set(struct ospf
*ospf
, int originate
, int mtype
,
689 struct ospf_redist
*red
;
691 ospf
->default_originate
= originate
;
693 red
= ospf_redist_add(ospf
, DEFAULT_ROUTE
, 0);
694 red
->dmetric
.type
= mtype
;
695 red
->dmetric
.value
= mvalue
;
697 ospf_external_add(DEFAULT_ROUTE
, 0);
699 if (ospf_is_type_redistributed(DEFAULT_ROUTE
, 0)) {
700 /* if ospf->default_originate changes value, is calling
701 ospf_external_lsa_refresh_default sufficient to implement
703 ospf_external_lsa_refresh_default(ospf
);
705 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
707 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
708 ospf_redist_string(DEFAULT_ROUTE
),
709 metric_type(ospf
, DEFAULT_ROUTE
, 0),
710 metric_value(ospf
, DEFAULT_ROUTE
, 0));
714 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
,
717 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
718 zlog_debug("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
719 metric_type(ospf
, DEFAULT_ROUTE
, 0),
720 metric_value(ospf
, DEFAULT_ROUTE
, 0));
722 if (ospf
->router_id
.s_addr
== 0)
723 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
725 thread_add_timer(master
, ospf_default_originate_timer
, ospf
, 1,
728 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
733 int ospf_redistribute_default_unset(struct ospf
*ospf
)
735 if (!ospf_is_type_redistributed(DEFAULT_ROUTE
, 0))
738 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
739 ospf_redist_del(ospf
, DEFAULT_ROUTE
, 0);
741 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
,
744 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
745 zlog_debug("Redistribute[DEFAULT]: Stop");
747 // Pending: how does the external_info cleanup work in this case?
749 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
754 static int ospf_external_lsa_originate_check(struct ospf
*ospf
,
755 struct external_info
*ei
)
757 /* If prefix is multicast, then do not originate LSA. */
758 if (IN_MULTICAST(htonl(ei
->p
.prefix
.s_addr
))) {
760 "LSA[Type5:%s]: Not originate AS-external-LSA, "
761 "Prefix belongs multicast",
762 inet_ntoa(ei
->p
.prefix
));
766 /* Take care of default-originate. */
767 if (is_prefix_default(&ei
->p
))
768 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
) {
770 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
778 /* If connected prefix is OSPF enable interface, then do not announce. */
779 int ospf_distribute_check_connected(struct ospf
*ospf
, struct external_info
*ei
)
781 struct listnode
*node
;
782 struct ospf_interface
*oi
;
785 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
786 if (prefix_match(oi
->address
, (struct prefix
*)&ei
->p
))
791 /* return 1 if external LSA must be originated, 0 otherwise */
792 int ospf_redistribute_check(struct ospf
*ospf
, struct external_info
*ei
,
795 struct route_map_set_values save_values
;
796 struct prefix_ipv4
*p
= &ei
->p
;
797 struct ospf_redist
*red
;
798 u_char type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
799 u_short instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
804 if (!ospf_external_lsa_originate_check(ospf
, ei
))
807 /* Take care connected route. */
808 if (type
== ZEBRA_ROUTE_CONNECT
809 && !ospf_distribute_check_connected(ospf
, ei
))
812 if (!DEFAULT_ROUTE_TYPE(type
) && DISTRIBUTE_NAME(ospf
, type
))
813 /* distirbute-list exists, but access-list may not? */
814 if (DISTRIBUTE_LIST(ospf
, type
))
815 if (access_list_apply(DISTRIBUTE_LIST(ospf
, type
), p
)
817 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
819 "Redistribute[%s]: %s/%d filtered by ditribute-list.",
820 ospf_redist_string(type
),
821 inet_ntoa(p
->prefix
),
826 save_values
= ei
->route_map_set
;
827 ospf_reset_route_map_set_values(&ei
->route_map_set
);
829 /* apply route-map if needed */
830 red
= ospf_redist_lookup(ospf
, type
, instance
);
831 if (red
&& ROUTEMAP_NAME(red
)) {
834 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
,
837 if (ret
== RMAP_DENYMATCH
) {
838 ei
->route_map_set
= save_values
;
839 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
841 "Redistribute[%s]: %s/%d filtered by route-map.",
842 ospf_redist_string(type
),
843 inet_ntoa(p
->prefix
), p
->prefixlen
);
847 /* check if 'route-map set' changed something */
849 *changed
= !ospf_route_map_set_compare(
850 &ei
->route_map_set
, &save_values
);
856 /* OSPF route-map set for redistribution */
857 void ospf_routemap_set(struct ospf_redist
*red
, const char *name
)
859 if (ROUTEMAP_NAME(red
))
860 free(ROUTEMAP_NAME(red
));
862 ROUTEMAP_NAME(red
) = strdup(name
);
863 ROUTEMAP(red
) = route_map_lookup_by_name(name
);
866 void ospf_routemap_unset(struct ospf_redist
*red
)
868 if (ROUTEMAP_NAME(red
))
869 free(ROUTEMAP_NAME(red
));
871 ROUTEMAP_NAME(red
) = NULL
;
872 ROUTEMAP(red
) = NULL
;
875 /* Zebra route add and delete treatment. */
876 static int ospf_zebra_read_route(int command
, struct zclient
*zclient
,
877 zebra_size_t length
, vrf_id_t vrf_id
)
879 struct zapi_route api
;
880 struct prefix_ipv4 p
;
881 unsigned long ifindex
;
882 struct in_addr nexthop
;
883 struct external_info
*ei
;
887 ospf
= ospf_lookup();
891 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
894 ifindex
= api
.nexthops
[0].ifindex
;
895 nexthop
= api
.nexthops
[0].gate
.ipv4
;
897 memcpy(&p
, &api
.prefix
, sizeof(p
));
898 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
901 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
902 /* XXX|HACK|TODO|FIXME:
903 * Maybe we should ignore reject/blackhole routes? Testing
904 * shows that there is no problems though and this is only way
905 * to "summarize" routes in ASBR at the moment. Maybe we need
906 * just a better generalised solution for these types?
909 /* Protocol tag overwrites all other tag value sent by zebra */
910 if (ospf
->dtag
[api
.type
] > 0)
911 api
.tag
= ospf
->dtag
[api
.type
];
914 * Given zebra sends update for a prefix via ADD message, it
916 * be considered as an implicit DEL for that prefix with other
920 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
922 ospf_external_info_delete(i
, api
.instance
, p
);
924 ei
= ospf_external_info_add(api
.type
, api
.instance
, p
, ifindex
,
927 /* Nothing has changed, so nothing to do; return */
930 if (ospf
->router_id
.s_addr
== 0)
931 /* Set flags to generate AS-external-LSA originate event
932 for each redistributed protocols later. */
933 ospf
->external_origin
|= (1 << api
.type
);
936 if (is_prefix_default(&p
))
937 ospf_external_lsa_refresh_default(ospf
);
939 struct ospf_lsa
*current
;
941 current
= ospf_external_info_find_lsa(
944 ospf_external_lsa_originate(
951 "ospf_zebra_read_route() : %s refreshing LSA",
954 ospf_external_lsa_refresh(
961 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
963 ospf_external_info_delete(api
.type
, api
.instance
, p
);
964 if (is_prefix_default(&p
))
965 ospf_external_lsa_refresh_default(ospf
);
967 ospf_external_lsa_flush(ospf
, api
.type
, &p
,
968 ifindex
/*, nexthop */);
975 int ospf_distribute_list_out_set(struct ospf
*ospf
, int type
, const char *name
)
977 /* Lookup access-list for distribute-list. */
978 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(AFI_IP
, name
);
980 /* Clear previous distribute-name. */
981 if (DISTRIBUTE_NAME(ospf
, type
))
982 free(DISTRIBUTE_NAME(ospf
, type
));
984 /* Set distribute-name. */
985 DISTRIBUTE_NAME(ospf
, type
) = strdup(name
);
987 /* If access-list have been set, schedule update timer. */
988 if (DISTRIBUTE_LIST(ospf
, type
))
989 ospf_distribute_list_update(ospf
, type
, 0);
994 int ospf_distribute_list_out_unset(struct ospf
*ospf
, int type
,
997 /* Schedule update timer. */
998 if (DISTRIBUTE_LIST(ospf
, type
))
999 ospf_distribute_list_update(ospf
, type
, 0);
1001 /* Unset distribute-list. */
1002 DISTRIBUTE_LIST(ospf
, type
) = NULL
;
1004 /* Clear distribute-name. */
1005 if (DISTRIBUTE_NAME(ospf
, type
))
1006 free(DISTRIBUTE_NAME(ospf
, type
));
1008 DISTRIBUTE_NAME(ospf
, type
) = NULL
;
1013 /* distribute-list update timer. */
1014 static int ospf_distribute_list_update_timer(struct thread
*thread
)
1016 struct route_node
*rn
;
1017 struct external_info
*ei
;
1018 struct route_table
*rt
;
1019 struct ospf_lsa
*lsa
;
1020 int type
, default_refresh
= 0;
1023 ospf
= ospf_lookup();
1027 ospf
->t_distribute_update
= NULL
;
1029 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1031 /* foreach all external info. */
1032 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1033 struct list
*ext_list
;
1034 struct listnode
*node
;
1035 struct ospf_external
*ext
;
1037 ext_list
= om
->external
[type
];
1041 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
1042 rt
= ext
->external_info
;
1045 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
1046 if ((ei
= rn
->info
) != NULL
) {
1047 if (is_prefix_default(&ei
->p
))
1048 default_refresh
= 1;
1050 (lsa
= ospf_external_info_find_lsa(
1052 ospf_external_lsa_refresh(
1054 LSA_REFRESH_IF_CHANGED
);
1056 ospf_external_lsa_originate(
1061 if (default_refresh
)
1062 ospf_external_lsa_refresh_default(ospf
);
1066 /* Update distribute-list and set timer to apply access-list. */
1067 void ospf_distribute_list_update(struct ospf
*ospf
, uintptr_t type
,
1070 struct route_table
*rt
;
1071 struct ospf_external
*ext
;
1073 /* External info does not exist. */
1074 ext
= ospf_external_lookup(type
, instance
);
1075 if (!ext
|| !(rt
= EXTERNAL_INFO(ext
)))
1078 /* If exists previously invoked thread, then let it continue. */
1079 if (ospf
->t_distribute_update
)
1083 ospf
->t_distribute_update
= NULL
;
1084 thread_add_timer_msec(master
, ospf_distribute_list_update_timer
,
1085 (void *)type
, ospf
->min_ls_interval
,
1086 &ospf
->t_distribute_update
);
1089 /* If access-list is updated, apply some check. */
1090 static void ospf_filter_update(struct access_list
*access
)
1095 struct ospf_area
*area
;
1096 struct listnode
*node
;
1098 /* If OSPF instance does not exist, return right now. */
1099 ospf
= ospf_lookup();
1103 /* Update distribute-list, and apply filter. */
1104 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1105 struct list
*red_list
;
1106 struct listnode
*node
;
1107 struct ospf_redist
*red
;
1109 red_list
= ospf
->redist
[type
];
1111 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1112 if (ROUTEMAP(red
)) {
1113 /* if route-map is not NULL it may be
1114 * using this access list */
1115 ospf_distribute_list_update(
1116 ospf
, type
, red
->instance
);
1120 /* There is place for route-map for default-information
1121 * (ZEBRA_ROUTE_MAX),
1122 * but no distribute list. */
1123 if (type
== ZEBRA_ROUTE_MAX
)
1126 if (DISTRIBUTE_NAME(ospf
, type
)) {
1127 /* Keep old access-list for distribute-list. */
1128 struct access_list
*old
= DISTRIBUTE_LIST(ospf
, type
);
1130 /* Update access-list for distribute-list. */
1131 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(
1132 AFI_IP
, DISTRIBUTE_NAME(ospf
, type
));
1134 /* No update for this distribute type. */
1135 if (old
== NULL
&& DISTRIBUTE_LIST(ospf
, type
) == NULL
)
1138 /* Schedule distribute-list update timer. */
1139 if (DISTRIBUTE_LIST(ospf
, type
) == NULL
1140 || strcmp(DISTRIBUTE_NAME(ospf
, type
), access
->name
)
1142 ospf_distribute_list_update(ospf
, type
, 0);
1146 /* Update Area access-list. */
1147 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1148 if (EXPORT_NAME(area
)) {
1149 EXPORT_LIST(area
) = NULL
;
1153 if (IMPORT_NAME(area
)) {
1154 IMPORT_LIST(area
) = NULL
;
1159 /* Schedule ABR tasks -- this will be changed -- takada. */
1160 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1161 ospf_schedule_abr_task(ospf
);
1164 /* If prefix-list is updated, do some updates. */
1165 void ospf_prefix_list_update(struct prefix_list
*plist
)
1170 struct ospf_area
*area
;
1171 struct listnode
*node
;
1173 /* If OSPF instatnce does not exist, return right now. */
1174 ospf
= ospf_lookup();
1178 /* Update all route-maps which are used as redistribution filters.
1179 * They might use prefix-list.
1181 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1182 struct list
*red_list
;
1183 struct listnode
*node
;
1184 struct ospf_redist
*red
;
1186 red_list
= ospf
->redist
[type
];
1188 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1189 if (ROUTEMAP(red
)) {
1190 /* if route-map is not NULL it may be
1191 * using this prefix list */
1192 ospf_distribute_list_update(
1193 ospf
, type
, red
->instance
);
1198 /* Update area filter-lists. */
1199 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1200 /* Update filter-list in. */
1201 if (PREFIX_NAME_IN(area
))
1202 if (strcmp(PREFIX_NAME_IN(area
),
1203 prefix_list_name(plist
))
1205 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1206 AFI_IP
, PREFIX_NAME_IN(area
));
1210 /* Update filter-list out. */
1211 if (PREFIX_NAME_OUT(area
))
1212 if (strcmp(PREFIX_NAME_OUT(area
),
1213 prefix_list_name(plist
))
1215 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1216 AFI_IP
, PREFIX_NAME_OUT(area
));
1221 /* Schedule ABR task. */
1222 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1223 ospf_schedule_abr_task(ospf
);
1226 static struct ospf_distance
*ospf_distance_new(void)
1228 return XCALLOC(MTYPE_OSPF_DISTANCE
, sizeof(struct ospf_distance
));
1231 static void ospf_distance_free(struct ospf_distance
*odistance
)
1233 XFREE(MTYPE_OSPF_DISTANCE
, odistance
);
1236 int ospf_distance_set(struct vty
*vty
, struct ospf
*ospf
,
1237 const char *distance_str
, const char *ip_str
,
1238 const char *access_list_str
)
1241 struct prefix_ipv4 p
;
1243 struct route_node
*rn
;
1244 struct ospf_distance
*odistance
;
1246 ret
= str2prefix_ipv4(ip_str
, &p
);
1248 vty_out(vty
, "Malformed prefix\n");
1249 return CMD_WARNING_CONFIG_FAILED
;
1252 distance
= atoi(distance_str
);
1254 /* Get OSPF distance node. */
1255 rn
= route_node_get(ospf
->distance_table
, (struct prefix
*)&p
);
1257 odistance
= rn
->info
;
1258 route_unlock_node(rn
);
1260 odistance
= ospf_distance_new();
1261 rn
->info
= odistance
;
1264 /* Set distance value. */
1265 odistance
->distance
= distance
;
1267 /* Reset access-list configuration. */
1268 if (odistance
->access_list
) {
1269 free(odistance
->access_list
);
1270 odistance
->access_list
= NULL
;
1272 if (access_list_str
)
1273 odistance
->access_list
= strdup(access_list_str
);
1278 int ospf_distance_unset(struct vty
*vty
, struct ospf
*ospf
,
1279 const char *distance_str
, const char *ip_str
,
1280 char const *access_list_str
)
1283 struct prefix_ipv4 p
;
1284 struct route_node
*rn
;
1285 struct ospf_distance
*odistance
;
1287 ret
= str2prefix_ipv4(ip_str
, &p
);
1289 vty_out(vty
, "Malformed prefix\n");
1290 return CMD_WARNING_CONFIG_FAILED
;
1293 rn
= route_node_lookup(ospf
->distance_table
, (struct prefix
*)&p
);
1295 vty_out(vty
, "Can't find specified prefix\n");
1296 return CMD_WARNING_CONFIG_FAILED
;
1299 odistance
= rn
->info
;
1301 if (odistance
->access_list
)
1302 free(odistance
->access_list
);
1303 ospf_distance_free(odistance
);
1306 route_unlock_node(rn
);
1307 route_unlock_node(rn
);
1312 void ospf_distance_reset(struct ospf
*ospf
)
1314 struct route_node
*rn
;
1315 struct ospf_distance
*odistance
;
1317 for (rn
= route_top(ospf
->distance_table
); rn
; rn
= route_next(rn
))
1318 if ((odistance
= rn
->info
) != NULL
) {
1319 if (odistance
->access_list
)
1320 free(odistance
->access_list
);
1321 ospf_distance_free(odistance
);
1323 route_unlock_node(rn
);
1327 u_char
ospf_distance_apply(struct prefix_ipv4
*p
, struct ospf_route
* or)
1331 ospf
= ospf_lookup();
1335 if (ospf
->distance_intra
)
1336 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1337 return ospf
->distance_intra
;
1339 if (ospf
->distance_inter
)
1340 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1341 return ospf
->distance_inter
;
1343 if (ospf
->distance_external
)
1344 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
||
1345 or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1346 return ospf
->distance_external
;
1348 if (ospf
->distance_all
)
1349 return ospf
->distance_all
;
1354 static void ospf_zebra_connected(struct zclient
*zclient
)
1356 /* Send the client registration */
1357 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1359 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
1362 void ospf_zebra_init(struct thread_master
*master
, u_short instance
)
1364 /* Allocate zebra structure. */
1365 zclient
= zclient_new(master
);
1366 zclient_init(zclient
, ZEBRA_ROUTE_OSPF
, instance
);
1367 zclient
->zebra_connected
= ospf_zebra_connected
;
1368 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1369 zclient
->interface_add
= ospf_interface_add
;
1370 zclient
->interface_delete
= ospf_interface_delete
;
1371 zclient
->interface_up
= ospf_interface_state_up
;
1372 zclient
->interface_down
= ospf_interface_state_down
;
1373 zclient
->interface_address_add
= ospf_interface_address_add
;
1374 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1375 zclient
->interface_link_params
= ospf_interface_link_params
;
1377 zclient
->redistribute_route_add
= ospf_zebra_read_route
;
1378 zclient
->redistribute_route_del
= ospf_zebra_read_route
;
1380 access_list_add_hook(ospf_filter_update
);
1381 access_list_delete_hook(ospf_filter_update
);
1382 prefix_list_add_hook(ospf_prefix_list_update
);
1383 prefix_list_delete_hook(ospf_prefix_list_update
);