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")
56 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_DIST_ARGS
, "OSPF Distribute arguments")
58 DEFINE_HOOK(ospf_if_update
, (struct interface
* ifp
), (ifp
))
59 DEFINE_HOOK(ospf_if_delete
, (struct interface
* ifp
), (ifp
))
61 /* Zebra structure to hold current status. */
62 struct zclient
*zclient
= NULL
;
64 /* For registering threads. */
65 extern struct thread_master
*master
;
66 struct in_addr router_id_zebra
;
68 /* Router-id update message from zebra. */
69 static int ospf_router_id_update_zebra(int command
, struct zclient
*zclient
,
70 zebra_size_t length
, vrf_id_t vrf_id
)
72 struct ospf
*ospf
= NULL
;
73 struct prefix router_id
;
74 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
76 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
)) {
77 char buf
[PREFIX2STR_BUFFER
];
78 prefix2str(&router_id
, buf
, sizeof(buf
));
79 zlog_debug("Zebra rcvd: router id update %s vrf %s id %u",
80 buf
, ospf_vrf_id_to_name(vrf_id
), vrf_id
);
83 router_id_zebra
= router_id
.u
.prefix4
;
85 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
88 ospf_router_id_update(ospf
);
90 if (IS_DEBUG_OSPF_EVENT
) {
91 char buf
[PREFIX2STR_BUFFER
];
93 prefix2str(&router_id
, buf
, sizeof(buf
));
94 zlog_debug("%s: ospf instance not found for vrf %s id %u router_id %s",
96 ospf_vrf_id_to_name(vrf_id
), vrf_id
, buf
);
102 /* Inteface addition message from zebra. */
103 static int ospf_interface_add(int command
, struct zclient
*zclient
,
104 zebra_size_t length
, vrf_id_t vrf_id
)
106 struct interface
*ifp
= NULL
;
107 struct ospf
*ospf
= NULL
;
109 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
113 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
115 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
116 ifp
->name
, ospf_vrf_id_to_name(ifp
->vrf_id
),
117 ifp
->vrf_id
, ifp
->ifindex
,
118 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
122 if (!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
);
127 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
131 ospf_if_update(ospf
, ifp
);
133 hook_call(ospf_if_update
, ifp
);
138 static int ospf_interface_delete(int command
, struct zclient
*zclient
,
139 zebra_size_t length
, vrf_id_t vrf_id
)
141 struct interface
*ifp
;
143 struct route_node
*rn
;
146 /* zebra_interface_state_read() updates interface structure in iflist */
147 ifp
= zebra_interface_state_read(s
, vrf_id
);
153 zlog_warn("Zebra: got delete of %s, but interface is still up",
156 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
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
);
163 hook_call(ospf_if_delete
, ifp
);
165 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
))
167 ospf_if_free((struct ospf_interface
*)rn
->info
);
169 ifp
->ifindex
= IFINDEX_DELETED
;
173 static struct interface
*zebra_interface_if_lookup(struct stream
*s
,
176 char ifname_tmp
[INTERFACE_NAMSIZ
];
178 /* Read interface name. */
179 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
181 /* And look it up. */
182 return if_lookup_by_name(ifname_tmp
, vrf_id
);
185 static int ospf_interface_state_up(int command
, struct zclient
*zclient
,
186 zebra_size_t length
, vrf_id_t vrf_id
)
188 struct interface
*ifp
;
189 struct ospf_interface
*oi
;
190 struct route_node
*rn
;
192 ifp
= zebra_interface_if_lookup(zclient
->ibuf
, vrf_id
);
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
));
203 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
205 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
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
);
211 ospf_if_recalculate_output_cost(ifp
);
213 if (if_tmp
.mtu
!= ifp
->mtu
) {
214 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
216 "Zebra: Interface[%s] MTU change %u -> %u.",
217 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
219 /* Must reset the interface (simulate down/up) when MTU
226 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
228 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
229 zlog_debug("Zebra: Interface[%s] state change to up.",
232 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
233 if ((oi
= rn
->info
) == NULL
)
242 static int ospf_interface_state_down(int command
, struct zclient
*zclient
,
243 zebra_size_t length
, vrf_id_t vrf_id
)
245 struct interface
*ifp
;
246 struct ospf_interface
*oi
;
247 struct route_node
*node
;
249 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
254 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
255 zlog_debug("Zebra: Interface[%s] state change to down.",
258 for (node
= route_top(IF_OIFS(ifp
)); node
; node
= route_next(node
)) {
259 if ((oi
= node
->info
) == NULL
)
267 static int ospf_interface_address_add(int command
, struct zclient
*zclient
,
268 zebra_size_t length
, vrf_id_t vrf_id
)
271 struct ospf
*ospf
= NULL
;
274 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
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
),
287 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
291 ospf_if_update(ospf
, c
->ifp
);
293 hook_call(ospf_if_update
, c
->ifp
);
298 static int ospf_interface_address_delete(int command
, struct zclient
*zclient
,
299 zebra_size_t length
, vrf_id_t vrf_id
)
302 struct interface
*ifp
;
303 struct ospf_interface
*oi
;
304 struct route_node
*rn
;
307 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
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",
321 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
323 rn
= route_node_lookup(IF_OIFS(ifp
), &p
);
331 route_unlock_node(rn
);
333 /* Call interface hook functions to clean up */
336 hook_call(ospf_if_update
, c
->ifp
);
343 static int ospf_interface_link_params(int command
, struct zclient
*zclient
,
346 struct interface
*ifp
;
348 ifp
= zebra_interface_link_params_read(zclient
->ibuf
);
354 ospf_mpls_te_update_if(ifp
);
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
)
363 struct interface
*ifp
= NULL
;
366 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
371 if (IS_DEBUG_OSPF_EVENT
)
372 zlog_debug("%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
);
376 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
377 if_update_to_new_vrf(ifp
, new_vrf_id
);
382 void ospf_zebra_add(struct ospf
*ospf
, struct prefix_ipv4
*p
,
383 struct ospf_route
*or)
385 struct zapi_route api
;
386 struct zapi_nexthop
*api_nh
;
388 struct ospf_path
*path
;
389 struct listnode
*node
;
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
;
398 memcpy(&api
.prefix
, p
, sizeof(*p
));
399 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
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
;
408 api
.metric
= or->cost
;
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
;
418 /* Distance value. */
419 distance
= ospf_distance_apply(ospf
, p
, or);
421 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
422 api
.distance
= distance
;
425 /* Nexthop, ifindex, distance and metric information. */
426 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
)) {
427 if (count
>= MULTIPATH_NUM
)
429 api_nh
= &api
.nexthops
[count
];
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
;
443 api_nh
->ifindex
= path
->ifindex
;
444 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
448 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
449 char buf
[2][INET_ADDRSTRLEN
];
451 "Zebra: Route add %s/%d nexthop %s, ifindex=%d",
452 inet_ntop(AF_INET
, &p
->prefix
, buf
[0],
454 p
->prefixlen
, inet_ntop(AF_INET
, &path
->nexthop
,
455 buf
[1], sizeof(buf
[1])),
459 api
.nexthop_num
= count
;
461 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
464 void ospf_zebra_delete(struct ospf
*ospf
, struct prefix_ipv4
*p
,
465 struct ospf_route
*or)
467 struct zapi_route api
;
469 memset(&api
, 0, sizeof(api
));
470 api
.vrf_id
= ospf
->vrf_id
;
471 api
.type
= ZEBRA_ROUTE_OSPF
;
472 api
.instance
= ospf
->instance
;
473 api
.safi
= SAFI_UNICAST
;
474 memcpy(&api
.prefix
, p
, sizeof(*p
));
476 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
477 char buf
[INET_ADDRSTRLEN
];
478 zlog_debug("Zebra: Route delete %s/%d",
479 inet_ntop(AF_INET
, &p
->prefix
, buf
, sizeof(buf
[0])),
483 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
486 void ospf_zebra_add_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
488 struct zapi_route api
;
490 memset(&api
, 0, sizeof(api
));
491 api
.vrf_id
= ospf
->vrf_id
;
492 api
.type
= ZEBRA_ROUTE_OSPF
;
493 api
.instance
= ospf
->instance
;
494 api
.safi
= SAFI_UNICAST
;
495 memcpy(&api
.prefix
, p
, sizeof(*p
));
496 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
498 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
500 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
501 zlog_debug("Zebra: Route add discard %s/%d",
502 inet_ntoa(p
->prefix
), p
->prefixlen
);
505 void ospf_zebra_delete_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
507 struct zapi_route api
;
509 memset(&api
, 0, sizeof(api
));
510 api
.vrf_id
= ospf
->vrf_id
;
511 api
.type
= ZEBRA_ROUTE_OSPF
;
512 api
.instance
= ospf
->instance
;
513 api
.safi
= SAFI_UNICAST
;
514 memcpy(&api
.prefix
, p
, sizeof(*p
));
515 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
517 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
519 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
520 zlog_debug("Zebra: Route delete discard %s/%d",
521 inet_ntoa(p
->prefix
), p
->prefixlen
);
524 struct ospf_external
*ospf_external_lookup(u_char type
, u_short instance
)
526 struct list
*ext_list
;
527 struct listnode
*node
;
528 struct ospf_external
*ext
;
530 ext_list
= om
->external
[type
];
534 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
535 if (ext
->instance
== instance
)
541 struct ospf_external
*ospf_external_add(u_char type
, u_short instance
)
543 struct list
*ext_list
;
544 struct ospf_external
*ext
;
546 ext
= ospf_external_lookup(type
, instance
);
550 if (!om
->external
[type
])
551 om
->external
[type
] = list_new();
553 ext_list
= om
->external
[type
];
554 ext
= (struct ospf_external
*)XCALLOC(MTYPE_OSPF_EXTERNAL
,
555 sizeof(struct ospf_external
));
556 ext
->instance
= instance
;
557 EXTERNAL_INFO(ext
) = route_table_init();
559 listnode_add(ext_list
, ext
);
564 void ospf_external_del(u_char type
, u_short instance
)
566 struct ospf_external
*ext
;
568 ext
= ospf_external_lookup(type
, instance
);
571 if (EXTERNAL_INFO(ext
))
572 route_table_finish(EXTERNAL_INFO(ext
));
574 listnode_delete(om
->external
[type
], ext
);
575 if (!om
->external
[type
]->count
) {
576 list_delete_and_null(&om
->external
[type
]);
578 XFREE(MTYPE_OSPF_EXTERNAL
, ext
);
582 struct ospf_redist
*ospf_redist_lookup(struct ospf
*ospf
, u_char type
,
585 struct list
*red_list
;
586 struct listnode
*node
;
587 struct ospf_redist
*red
;
589 red_list
= ospf
->redist
[type
];
593 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
594 if (red
->instance
== instance
)
600 struct ospf_redist
*ospf_redist_add(struct ospf
*ospf
, u_char type
,
603 struct list
*red_list
;
604 struct ospf_redist
*red
;
606 red
= ospf_redist_lookup(ospf
, type
, instance
);
610 if (!ospf
->redist
[type
])
611 ospf
->redist
[type
] = list_new();
613 red_list
= ospf
->redist
[type
];
614 red
= (struct ospf_redist
*)XCALLOC(MTYPE_OSPF_REDISTRIBUTE
,
615 sizeof(struct ospf_redist
));
616 red
->instance
= instance
;
617 red
->dmetric
.type
= -1;
618 red
->dmetric
.value
= -1;
620 listnode_add(red_list
, red
);
625 void ospf_redist_del(struct ospf
*ospf
, u_char type
, u_short instance
)
627 struct ospf_redist
*red
;
629 red
= ospf_redist_lookup(ospf
, type
, instance
);
632 listnode_delete(ospf
->redist
[type
], red
);
633 if (!ospf
->redist
[type
]->count
) {
634 list_delete_and_null(&ospf
->redist
[type
]);
636 ospf_routemap_unset(red
);
637 XFREE(MTYPE_OSPF_REDISTRIBUTE
, red
);
642 int ospf_is_type_redistributed(struct ospf
*ospf
, int type
, u_short instance
)
644 return (DEFAULT_ROUTE_TYPE(type
)
645 ? vrf_bitmap_check(zclient
->default_information
,
648 && redist_check_instance(
649 &zclient
->mi_redist
[AFI_IP
][type
],
653 zclient
->redist
[AFI_IP
][type
],
657 int ospf_redistribute_set(struct ospf
*ospf
, int type
, u_short instance
,
658 int mtype
, int mvalue
)
661 struct ospf_redist
*red
;
663 red
= ospf_redist_lookup(ospf
, type
, instance
);
664 if (ospf_is_type_redistributed(ospf
, type
, instance
)) {
665 if (mtype
!= red
->dmetric
.type
) {
666 red
->dmetric
.type
= mtype
;
667 force
= LSA_REFRESH_FORCE
;
669 if (mvalue
!= red
->dmetric
.value
) {
670 red
->dmetric
.value
= mvalue
;
671 force
= LSA_REFRESH_FORCE
;
674 ospf_external_lsa_refresh_type(ospf
, type
, instance
, force
);
676 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
678 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
679 ospf_redist_string(type
), instance
,
680 metric_type(ospf
, type
, instance
),
681 metric_value(ospf
, type
, instance
));
686 red
->dmetric
.type
= mtype
;
687 red
->dmetric
.value
= mvalue
;
689 ospf_external_add(type
, instance
);
691 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
692 instance
, ospf
->vrf_id
);
694 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
695 zlog_debug("Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
696 ospf_redist_string(type
), instance
, ospf
->vrf_id
,
697 metric_type(ospf
, type
, instance
),
698 metric_value(ospf
, type
, instance
));
700 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
705 int ospf_redistribute_unset(struct ospf
*ospf
, int type
, u_short instance
)
707 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
710 if (!ospf_is_type_redistributed(ospf
, type
, instance
))
713 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
,
714 instance
, ospf
->vrf_id
);
716 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
717 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
718 ospf_redist_string(type
), instance
, ospf
->vrf_id
);
720 ospf_redist_del(ospf
, type
, instance
);
722 /* Remove the routes from OSPF table. */
723 ospf_redistribute_withdraw(ospf
, type
, instance
);
725 ospf_external_del(type
, instance
);
727 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
732 int ospf_redistribute_default_set(struct ospf
*ospf
, int originate
, int mtype
,
735 struct ospf_redist
*red
;
737 ospf
->default_originate
= originate
;
739 red
= ospf_redist_add(ospf
, DEFAULT_ROUTE
, 0);
740 red
->dmetric
.type
= mtype
;
741 red
->dmetric
.value
= mvalue
;
743 ospf_external_add(DEFAULT_ROUTE
, 0);
745 if (ospf_is_type_redistributed(ospf
, DEFAULT_ROUTE
, 0)) {
746 /* if ospf->default_originate changes value, is calling
747 ospf_external_lsa_refresh_default sufficient to implement
749 ospf_external_lsa_refresh_default(ospf
);
751 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
753 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
754 ospf_redist_string(DEFAULT_ROUTE
),
755 metric_type(ospf
, DEFAULT_ROUTE
, 0),
756 metric_value(ospf
, DEFAULT_ROUTE
, 0));
760 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
,
763 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
764 zlog_debug("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
765 metric_type(ospf
, DEFAULT_ROUTE
, 0),
766 metric_value(ospf
, DEFAULT_ROUTE
, 0));
768 if (ospf
->router_id
.s_addr
== 0)
769 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
771 thread_add_timer(master
, ospf_default_originate_timer
, ospf
, 1,
774 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
779 int ospf_redistribute_default_unset(struct ospf
*ospf
)
781 if (!ospf_is_type_redistributed(ospf
, DEFAULT_ROUTE
, 0))
784 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
785 ospf_redist_del(ospf
, DEFAULT_ROUTE
, 0);
787 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
,
790 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
791 zlog_debug("Redistribute[DEFAULT]: Stop");
793 // Pending: how does the external_info cleanup work in this case?
795 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
800 static int ospf_external_lsa_originate_check(struct ospf
*ospf
,
801 struct external_info
*ei
)
803 /* If prefix is multicast, then do not originate LSA. */
804 if (IN_MULTICAST(htonl(ei
->p
.prefix
.s_addr
))) {
806 "LSA[Type5:%s]: Not originate AS-external-LSA, "
807 "Prefix belongs multicast",
808 inet_ntoa(ei
->p
.prefix
));
812 /* Take care of default-originate. */
813 if (is_prefix_default(&ei
->p
))
814 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
) {
816 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
824 /* If connected prefix is OSPF enable interface, then do not announce. */
825 int ospf_distribute_check_connected(struct ospf
*ospf
, struct external_info
*ei
)
827 struct listnode
*node
;
828 struct ospf_interface
*oi
;
831 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
832 if (prefix_match(oi
->address
, (struct prefix
*)&ei
->p
))
837 /* return 1 if external LSA must be originated, 0 otherwise */
838 int ospf_redistribute_check(struct ospf
*ospf
, struct external_info
*ei
,
841 struct route_map_set_values save_values
;
842 struct prefix_ipv4
*p
= &ei
->p
;
843 struct ospf_redist
*red
;
844 u_char type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
845 u_short instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
850 if (!ospf_external_lsa_originate_check(ospf
, ei
))
853 /* Take care connected route. */
854 if (type
== ZEBRA_ROUTE_CONNECT
855 && !ospf_distribute_check_connected(ospf
, ei
))
858 if (!DEFAULT_ROUTE_TYPE(type
) && DISTRIBUTE_NAME(ospf
, type
))
859 /* distirbute-list exists, but access-list may not? */
860 if (DISTRIBUTE_LIST(ospf
, type
))
861 if (access_list_apply(DISTRIBUTE_LIST(ospf
, type
), p
)
863 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
865 "Redistribute[%s]: %s/%d filtered by ditribute-list.",
866 ospf_redist_string(type
),
867 inet_ntoa(p
->prefix
),
872 save_values
= ei
->route_map_set
;
873 ospf_reset_route_map_set_values(&ei
->route_map_set
);
875 /* apply route-map if needed */
876 red
= ospf_redist_lookup(ospf
, type
, instance
);
877 if (red
&& ROUTEMAP_NAME(red
)) {
880 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
,
883 if (ret
== RMAP_DENYMATCH
) {
884 ei
->route_map_set
= save_values
;
885 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
887 "Redistribute[%s]: %s/%d filtered by route-map.",
888 ospf_redist_string(type
),
889 inet_ntoa(p
->prefix
), p
->prefixlen
);
893 /* check if 'route-map set' changed something */
895 *changed
= !ospf_route_map_set_compare(
896 &ei
->route_map_set
, &save_values
);
902 /* OSPF route-map set for redistribution */
903 void ospf_routemap_set(struct ospf_redist
*red
, const char *name
)
905 if (ROUTEMAP_NAME(red
))
906 free(ROUTEMAP_NAME(red
));
908 ROUTEMAP_NAME(red
) = strdup(name
);
909 ROUTEMAP(red
) = route_map_lookup_by_name(name
);
912 void ospf_routemap_unset(struct ospf_redist
*red
)
914 if (ROUTEMAP_NAME(red
))
915 free(ROUTEMAP_NAME(red
));
917 ROUTEMAP_NAME(red
) = NULL
;
918 ROUTEMAP(red
) = NULL
;
921 /* Zebra route add and delete treatment. */
922 static int ospf_zebra_read_route(int command
, struct zclient
*zclient
,
923 zebra_size_t length
, vrf_id_t vrf_id
)
925 struct zapi_route api
;
926 struct prefix_ipv4 p
;
927 unsigned long ifindex
;
928 struct in_addr nexthop
;
929 struct external_info
*ei
;
933 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
937 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
940 ifindex
= api
.nexthops
[0].ifindex
;
941 nexthop
= api
.nexthops
[0].gate
.ipv4
;
943 memcpy(&p
, &api
.prefix
, sizeof(p
));
944 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
947 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
948 /* XXX|HACK|TODO|FIXME:
949 * Maybe we should ignore reject/blackhole routes? Testing
950 * shows that there is no problems though and this is only way
951 * to "summarize" routes in ASBR at the moment. Maybe we need
952 * just a better generalised solution for these types?
955 /* Protocol tag overwrites all other tag value sent by zebra */
956 if (ospf
->dtag
[api
.type
] > 0)
957 api
.tag
= ospf
->dtag
[api
.type
];
960 * Given zebra sends update for a prefix via ADD message, it
962 * be considered as an implicit DEL for that prefix with other
966 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
968 ospf_external_info_delete(i
, api
.instance
, p
);
970 ei
= ospf_external_info_add(api
.type
, api
.instance
, p
, ifindex
,
973 /* Nothing has changed, so nothing to do; return */
976 if (ospf
->router_id
.s_addr
== 0)
977 /* Set flags to generate AS-external-LSA originate event
978 for each redistributed protocols later. */
979 ospf
->external_origin
|= (1 << api
.type
);
982 if (is_prefix_default(&p
))
983 ospf_external_lsa_refresh_default(ospf
);
985 struct ospf_lsa
*current
;
987 current
= ospf_external_info_find_lsa(
990 ospf_external_lsa_originate(
997 "ospf_zebra_read_route() : %s refreshing LSA",
1000 ospf_external_lsa_refresh(
1007 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1009 ospf_external_info_delete(api
.type
, api
.instance
, p
);
1010 if (is_prefix_default(&p
))
1011 ospf_external_lsa_refresh_default(ospf
);
1013 ospf_external_lsa_flush(ospf
, api
.type
, &p
,
1014 ifindex
/*, nexthop */);
1021 int ospf_distribute_list_out_set(struct ospf
*ospf
, int type
, const char *name
)
1023 /* Lookup access-list for distribute-list. */
1024 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(AFI_IP
, name
);
1026 /* Clear previous distribute-name. */
1027 if (DISTRIBUTE_NAME(ospf
, type
))
1028 free(DISTRIBUTE_NAME(ospf
, type
));
1030 /* Set distribute-name. */
1031 DISTRIBUTE_NAME(ospf
, type
) = strdup(name
);
1033 /* If access-list have been set, schedule update timer. */
1034 if (DISTRIBUTE_LIST(ospf
, type
))
1035 ospf_distribute_list_update(ospf
, type
, 0);
1040 int ospf_distribute_list_out_unset(struct ospf
*ospf
, int type
,
1043 /* Schedule update timer. */
1044 if (DISTRIBUTE_LIST(ospf
, type
))
1045 ospf_distribute_list_update(ospf
, type
, 0);
1047 /* Unset distribute-list. */
1048 DISTRIBUTE_LIST(ospf
, type
) = NULL
;
1050 /* Clear distribute-name. */
1051 if (DISTRIBUTE_NAME(ospf
, type
))
1052 free(DISTRIBUTE_NAME(ospf
, type
));
1054 DISTRIBUTE_NAME(ospf
, type
) = NULL
;
1059 /* distribute-list update timer. */
1060 static int ospf_distribute_list_update_timer(struct thread
*thread
)
1062 struct route_node
*rn
;
1063 struct external_info
*ei
;
1064 struct route_table
*rt
;
1065 struct ospf_lsa
*lsa
;
1066 int type
, default_refresh
= 0, arg_type
;
1067 struct ospf
*ospf
= NULL
;
1068 void **arg
= THREAD_ARG (thread
);
1070 ospf
= (struct ospf
*)arg
[0];
1071 arg_type
= (int)(intptr_t)arg
[1];
1076 ospf
->t_distribute_update
= NULL
;
1078 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1080 if (IS_DEBUG_OSPF_EVENT
) {
1081 zlog_debug("%s: ospf distribute-list update arg_type %d vrf %s id %d",
1082 __PRETTY_FUNCTION__
, arg_type
,
1083 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
);
1086 /* foreach all external info. */
1087 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1088 struct list
*ext_list
;
1089 struct listnode
*node
;
1090 struct ospf_external
*ext
;
1092 ext_list
= om
->external
[type
];
1096 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
1097 rt
= ext
->external_info
;
1100 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
1101 if ((ei
= rn
->info
) != NULL
) {
1102 if (is_prefix_default(&ei
->p
))
1103 default_refresh
= 1;
1105 (lsa
= ospf_external_info_find_lsa(
1107 ospf_external_lsa_refresh(
1109 LSA_REFRESH_IF_CHANGED
);
1111 ospf_external_lsa_originate(
1116 if (default_refresh
)
1117 ospf_external_lsa_refresh_default(ospf
);
1119 XFREE(MTYPE_OSPF_DIST_ARGS
, arg
);
1123 /* Update distribute-list and set timer to apply access-list. */
1124 void ospf_distribute_list_update(struct ospf
*ospf
, int type
,
1127 struct route_table
*rt
;
1128 struct ospf_external
*ext
;
1129 void **args
= XCALLOC(MTYPE_OSPF_DIST_ARGS
, sizeof(void *)*2);
1132 args
[1] = (void *)((ptrdiff_t) type
);
1134 /* External info does not exist. */
1135 ext
= ospf_external_lookup(type
, instance
);
1136 if (!ext
|| !(rt
= EXTERNAL_INFO(ext
)))
1139 /* If exists previously invoked thread, then let it continue. */
1140 if (ospf
->t_distribute_update
)
1144 ospf
->t_distribute_update
= NULL
;
1145 thread_add_timer_msec(master
, ospf_distribute_list_update_timer
,
1146 (void **)args
, ospf
->min_ls_interval
,
1147 &ospf
->t_distribute_update
);
1150 /* If access-list is updated, apply some check. */
1151 static void ospf_filter_update(struct access_list
*access
)
1156 struct ospf_area
*area
;
1157 struct listnode
*node
, *n1
;
1159 /* If OSPF instance does not exist, return right now. */
1160 if (listcount(om
->ospf
) == 0)
1163 /* Iterate all ospf [VRF] instances */
1164 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1165 /* Update distribute-list, and apply filter. */
1166 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1167 struct list
*red_list
;
1168 struct listnode
*node
;
1169 struct ospf_redist
*red
;
1171 red_list
= ospf
->redist
[type
];
1173 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1174 if (ROUTEMAP(red
)) {
1175 /* if route-map is not NULL it may be
1176 * using this access list */
1177 ospf_distribute_list_update(
1179 type
, red
->instance
);
1183 /* There is place for route-map for default-information
1184 * (ZEBRA_ROUTE_MAX),
1185 * but no distribute list. */
1186 if (type
== ZEBRA_ROUTE_MAX
)
1189 if (DISTRIBUTE_NAME(ospf
, type
)) {
1190 /* Keep old access-list for distribute-list. */
1191 struct access_list
*old
= DISTRIBUTE_LIST(ospf
,
1194 /* Update access-list for distribute-list. */
1195 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(
1196 AFI_IP
, DISTRIBUTE_NAME(ospf
, type
));
1198 /* No update for this distribute type. */
1199 if (old
== NULL
&& DISTRIBUTE_LIST(ospf
, type
) == NULL
)
1202 /* Schedule distribute-list update timer. */
1203 if (DISTRIBUTE_LIST(ospf
, type
) == NULL
1204 || strcmp(DISTRIBUTE_NAME(ospf
, type
), access
->name
)
1206 ospf_distribute_list_update(ospf
, type
, 0);
1210 /* Update Area access-list. */
1211 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1212 if (EXPORT_NAME(area
)) {
1213 EXPORT_LIST(area
) = NULL
;
1217 if (IMPORT_NAME(area
)) {
1218 IMPORT_LIST(area
) = NULL
;
1223 /* Schedule ABR tasks -- this will be changed -- takada. */
1224 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1225 ospf_schedule_abr_task(ospf
);
1229 /* If prefix-list is updated, do some updates. */
1230 void ospf_prefix_list_update(struct prefix_list
*plist
)
1232 struct ospf
*ospf
= NULL
;
1235 struct ospf_area
*area
;
1236 struct listnode
*node
, *n1
;
1238 /* If OSPF instatnce does not exist, return right now. */
1239 if (listcount(om
->ospf
) == 0)
1242 /* Iterate all ospf [VRF] instances */
1243 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1245 /* Update all route-maps which are used
1246 * as redistribution filters.
1247 * They might use prefix-list.
1249 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1250 struct list
*red_list
;
1251 struct listnode
*node
;
1252 struct ospf_redist
*red
;
1254 red_list
= ospf
->redist
[type
];
1256 for (ALL_LIST_ELEMENTS_RO(red_list
,
1258 if (ROUTEMAP(red
)) {
1259 /* if route-map is not NULL
1261 * this prefix list */
1262 ospf_distribute_list_update(
1270 /* Update area filter-lists. */
1271 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1272 /* Update filter-list in. */
1273 if (PREFIX_NAME_IN(area
))
1274 if (strcmp(PREFIX_NAME_IN(area
),
1275 prefix_list_name(plist
)) == 0) {
1276 PREFIX_LIST_IN(area
) =
1279 PREFIX_NAME_IN(area
));
1283 /* Update filter-list out. */
1284 if (PREFIX_NAME_OUT(area
))
1285 if (strcmp(PREFIX_NAME_OUT(area
),
1286 prefix_list_name(plist
)) == 0) {
1287 PREFIX_LIST_IN(area
) =
1290 PREFIX_NAME_OUT(area
));
1295 /* Schedule ABR task. */
1296 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1297 ospf_schedule_abr_task(ospf
);
1301 static struct ospf_distance
*ospf_distance_new(void)
1303 return XCALLOC(MTYPE_OSPF_DISTANCE
, sizeof(struct ospf_distance
));
1306 static void ospf_distance_free(struct ospf_distance
*odistance
)
1308 XFREE(MTYPE_OSPF_DISTANCE
, odistance
);
1311 int ospf_distance_set(struct vty
*vty
, struct ospf
*ospf
,
1312 const char *distance_str
, const char *ip_str
,
1313 const char *access_list_str
)
1316 struct prefix_ipv4 p
;
1318 struct route_node
*rn
;
1319 struct ospf_distance
*odistance
;
1321 ret
= str2prefix_ipv4(ip_str
, &p
);
1323 vty_out(vty
, "Malformed prefix\n");
1324 return CMD_WARNING_CONFIG_FAILED
;
1327 distance
= atoi(distance_str
);
1329 /* Get OSPF distance node. */
1330 rn
= route_node_get(ospf
->distance_table
, (struct prefix
*)&p
);
1332 odistance
= rn
->info
;
1333 route_unlock_node(rn
);
1335 odistance
= ospf_distance_new();
1336 rn
->info
= odistance
;
1339 /* Set distance value. */
1340 odistance
->distance
= distance
;
1342 /* Reset access-list configuration. */
1343 if (odistance
->access_list
) {
1344 free(odistance
->access_list
);
1345 odistance
->access_list
= NULL
;
1347 if (access_list_str
)
1348 odistance
->access_list
= strdup(access_list_str
);
1353 int ospf_distance_unset(struct vty
*vty
, struct ospf
*ospf
,
1354 const char *distance_str
, const char *ip_str
,
1355 char const *access_list_str
)
1358 struct prefix_ipv4 p
;
1359 struct route_node
*rn
;
1360 struct ospf_distance
*odistance
;
1362 ret
= str2prefix_ipv4(ip_str
, &p
);
1364 vty_out(vty
, "Malformed prefix\n");
1365 return CMD_WARNING_CONFIG_FAILED
;
1368 rn
= route_node_lookup(ospf
->distance_table
, (struct prefix
*)&p
);
1370 vty_out(vty
, "Can't find specified prefix\n");
1371 return CMD_WARNING_CONFIG_FAILED
;
1374 odistance
= rn
->info
;
1376 if (odistance
->access_list
)
1377 free(odistance
->access_list
);
1378 ospf_distance_free(odistance
);
1381 route_unlock_node(rn
);
1382 route_unlock_node(rn
);
1387 void ospf_distance_reset(struct ospf
*ospf
)
1389 struct route_node
*rn
;
1390 struct ospf_distance
*odistance
;
1392 for (rn
= route_top(ospf
->distance_table
); rn
; rn
= route_next(rn
))
1393 if ((odistance
= rn
->info
) != NULL
) {
1394 if (odistance
->access_list
)
1395 free(odistance
->access_list
);
1396 ospf_distance_free(odistance
);
1398 route_unlock_node(rn
);
1402 u_char
ospf_distance_apply(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1403 struct ospf_route
*or)
1409 if (ospf
->distance_intra
)
1410 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1411 return ospf
->distance_intra
;
1413 if (ospf
->distance_inter
)
1414 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1415 return ospf
->distance_inter
;
1417 if (ospf
->distance_external
)
1418 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
||
1419 or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1420 return ospf
->distance_external
;
1422 if (ospf
->distance_all
)
1423 return ospf
->distance_all
;
1428 void ospf_zebra_vrf_register(struct ospf
*ospf
)
1430 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1433 if (ospf
->vrf_id
!= VRF_DEFAULT
&& ospf
->vrf_id
!= VRF_UNKNOWN
) {
1434 if (IS_DEBUG_OSPF_EVENT
)
1435 zlog_debug("%s: Register VRF %s id %u",
1436 __PRETTY_FUNCTION__
,
1437 ospf_vrf_id_to_name(ospf
->vrf_id
),
1439 zclient_send_reg_requests(zclient
, ospf
->vrf_id
);
1443 void ospf_zebra_vrf_deregister(struct ospf
*ospf
)
1445 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1448 if (ospf
->vrf_id
!= VRF_DEFAULT
&& ospf
->vrf_id
!= VRF_UNKNOWN
) {
1449 if (IS_DEBUG_OSPF_EVENT
)
1450 zlog_debug("%s: De-Register VRF %s id %u",
1451 __PRETTY_FUNCTION__
,
1452 ospf_vrf_id_to_name(ospf
->vrf_id
),
1454 /* Deregister for router-id, interfaces,
1455 * redistributed routes. */
1456 zclient_send_dereg_requests(zclient
, ospf
->vrf_id
);
1459 static void ospf_zebra_connected(struct zclient
*zclient
)
1461 /* Send the client registration */
1462 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1464 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
1467 void ospf_zebra_init(struct thread_master
*master
, u_short instance
)
1469 /* Allocate zebra structure. */
1470 zclient
= zclient_new(master
);
1471 zclient_init(zclient
, ZEBRA_ROUTE_OSPF
, instance
);
1472 zclient
->zebra_connected
= ospf_zebra_connected
;
1473 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1474 zclient
->interface_add
= ospf_interface_add
;
1475 zclient
->interface_delete
= ospf_interface_delete
;
1476 zclient
->interface_up
= ospf_interface_state_up
;
1477 zclient
->interface_down
= ospf_interface_state_down
;
1478 zclient
->interface_address_add
= ospf_interface_address_add
;
1479 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1480 zclient
->interface_link_params
= ospf_interface_link_params
;
1481 zclient
->interface_vrf_update
= ospf_interface_vrf_update
;
1483 zclient
->redistribute_route_add
= ospf_zebra_read_route
;
1484 zclient
->redistribute_route_del
= ospf_zebra_read_route
;
1486 access_list_add_hook(ospf_filter_update
);
1487 access_list_delete_hook(ospf_filter_update
);
1488 prefix_list_add_hook(ospf_prefix_list_update
);
1489 prefix_list_delete_hook(ospf_prefix_list_update
);