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
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_abr.h"
44 #include "ospfd/ospf_lsa.h"
45 #include "ospfd/ospf_dump.h"
46 #include "ospfd/ospf_route.h"
47 #include "ospfd/ospf_zebra.h"
49 #include "ospfd/ospf_snmp.h"
50 #endif /* HAVE_SNMP */
52 /* Zebra structure to hold current status. */
53 struct zclient
*zclient
= NULL
;
55 /* For registering threads. */
56 extern struct thread_master
*master
;
57 struct in_addr router_id_zebra
;
59 /* Router-id update message from zebra. */
61 ospf_router_id_update_zebra (int command
, struct zclient
*zclient
,
65 struct prefix router_id
;
66 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
68 router_id_zebra
= router_id
.u
.prefix4
;
70 ospf
= ospf_lookup ();
73 if (ospf
->t_router_id_update
== NULL
)
74 OSPF_TIMER_ON (ospf
->t_router_id_update
, ospf_router_id_update_timer
,
75 OSPF_ROUTER_ID_UPDATE_DELAY
);
80 /* Inteface addition message from zebra. */
82 ospf_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
84 struct interface
*ifp
;
87 ifp
= zebra_interface_add_read (zclient
->ibuf
);
89 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
90 zlog_debug ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
91 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu
);
95 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
97 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
98 IF_DEF_PARAMS (ifp
)->type
= ospf_default_iftype(ifp
);
101 ospf
= ospf_lookup ();
103 ospf_if_update (ospf
);
106 ospf_snmp_if_update (ifp
);
107 #endif /* HAVE_SNMP */
113 ospf_interface_delete (int command
, struct zclient
*zclient
,
116 struct interface
*ifp
;
118 struct route_node
*rn
;
121 /* zebra_interface_state_read() updates interface structure in iflist */
122 ifp
= zebra_interface_state_read (s
);
128 zlog_warn ("Zebra: got delete of %s, but interface is still up",
131 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
133 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
134 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu
);
137 ospf_snmp_if_delete (ifp
);
138 #endif /* HAVE_SNMP */
140 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
142 ospf_if_free ((struct ospf_interface
*) rn
->info
);
144 ifp
->ifindex
= IFINDEX_INTERNAL
;
148 static struct interface
*
149 zebra_interface_if_lookup (struct stream
*s
)
151 char ifname_tmp
[INTERFACE_NAMSIZ
];
153 /* Read interface name. */
154 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
156 /* And look it up. */
157 return if_lookup_by_name_len(ifname_tmp
,
158 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
162 ospf_interface_state_up (int command
, struct zclient
*zclient
,
165 struct interface
*ifp
;
166 struct ospf_interface
*oi
;
167 struct route_node
*rn
;
169 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
174 /* Interface is already up. */
175 if (if_is_operative (ifp
))
177 /* Temporarily keep ifp values. */
178 struct interface if_tmp
;
179 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
181 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
183 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
184 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
186 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
188 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
189 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
190 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
192 ospf_if_recalculate_output_cost (ifp
);
195 if (if_tmp
.mtu
!= ifp
->mtu
)
197 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
198 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
199 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
201 /* Must reset the interface (simulate down/up) when MTU changes. */
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.", ifp
->name
);
212 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
214 if ((oi
= rn
->info
) == NULL
)
224 ospf_interface_state_down (int command
, struct zclient
*zclient
,
227 struct interface
*ifp
;
228 struct ospf_interface
*oi
;
229 struct route_node
*node
;
231 ifp
= zebra_interface_state_read (zclient
->ibuf
);
236 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
237 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
239 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
241 if ((oi
= node
->info
) == NULL
)
250 ospf_interface_address_add (int command
, struct zclient
*zclient
,
256 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
261 ospf
= ospf_lookup ();
263 ospf_if_update (ospf
);
266 ospf_snmp_if_update (c
->ifp
);
267 #endif /* HAVE_SNMP */
273 ospf_interface_address_delete (int command
, struct zclient
*zclient
,
278 struct interface
*ifp
;
279 struct ospf_interface
*oi
;
280 struct route_node
*rn
;
283 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
290 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
292 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
299 /* Call interface hook functions to clean up */
303 ospf_snmp_if_update (c
->ifp
);
304 #endif /* HAVE_SNMP */
308 ospf
= ospf_lookup ();
310 ospf_if_update (ospf
);
316 ospf_zebra_add (struct prefix_ipv4
*p
, struct ospf_route
*or)
323 struct ospf_path
*path
;
324 struct listnode
*node
;
326 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
331 /* OSPF pass nexthop and metric */
332 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
333 SET_FLAG (message
, ZAPI_MESSAGE_METRIC
);
335 /* Distance value. */
336 distance
= ospf_distance_apply (p
, or);
338 SET_FLAG (message
, ZAPI_MESSAGE_DISTANCE
);
344 /* Length place holder. */
347 /* Put command, type, flags, message. */
348 stream_putc (s
, ZEBRA_IPV4_ROUTE_ADD
);
349 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
350 stream_putc (s
, flags
);
351 stream_putc (s
, message
);
353 /* Put prefix information. */
354 psize
= PSIZE (p
->prefixlen
);
355 stream_putc (s
, p
->prefixlen
);
356 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
359 stream_putc (s
, or->paths
->count
);
361 /* Nexthop, ifindex, distance and metric information. */
362 for (ALL_LIST_ELEMENTS_RO (or->paths
, node
, path
))
364 if (path
->nexthop
.s_addr
!= INADDR_ANY
)
366 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
367 stream_put_in_addr (s
, &path
->nexthop
);
371 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
373 stream_putl (s
, path
->oi
->ifp
->ifindex
);
378 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
380 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
381 inet_ntoa (p
->prefix
),
382 p
->prefixlen
, inet_ntoa (path
->nexthop
));
386 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
387 stream_putc (s
, distance
);
388 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
390 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
391 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
392 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
393 stream_putl (s
, or->u
.ext
.type2_cost
);
395 stream_putl (s
, or->cost
);
398 stream_putw_at (s
, 0, stream_get_endp (s
));
400 writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
405 ospf_zebra_delete (struct prefix_ipv4
*p
, struct ospf_route
*or)
407 struct zapi_ipv4 api
;
408 struct ospf_path
*path
;
409 struct in_addr
*nexthop
;
410 struct listnode
*node
, *nnode
;
412 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
414 api
.type
= ZEBRA_ROUTE_OSPF
;
420 for (ALL_LIST_ELEMENTS (or->paths
, node
, nnode
, path
))
422 if (path
->nexthop
.s_addr
!= INADDR_ANY
)
424 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
426 nexthop
= &path
->nexthop
;
427 api
.nexthop
= &nexthop
;
429 else if (ospf_if_exists(path
->oi
) && (path
->oi
->ifp
))
431 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
433 api
.ifindex
= &path
->oi
->ifp
->ifindex
;
435 else if ( IS_DEBUG_OSPF(zebra
,ZEBRA_REDISTRIBUTE
) )
437 zlog_debug("Zebra: no ifp %s %d",
438 inet_ntoa(p
->prefix
),
442 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
444 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
) && api
.nexthop_num
)
446 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
447 inet_ntoa (p
->prefix
),
448 p
->prefixlen
, inet_ntoa (**api
.nexthop
));
450 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
) && api
.ifindex_num
)
452 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
453 inet_ntoa (p
->prefix
),
454 p
->prefixlen
, *api
.ifindex
);
461 ospf_zebra_add_discard (struct prefix_ipv4
*p
)
463 struct zapi_ipv4 api
;
465 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
467 api
.type
= ZEBRA_ROUTE_OSPF
;
468 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
470 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
474 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD
, zclient
, p
, &api
);
479 ospf_zebra_delete_discard (struct prefix_ipv4
*p
)
481 struct zapi_ipv4 api
;
483 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
485 api
.type
= ZEBRA_ROUTE_OSPF
;
486 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
488 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
492 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
494 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
495 zlog_debug ("Zebra: Route delete discard %s/%d",
496 inet_ntoa (p
->prefix
), p
->prefixlen
);
502 ospf_is_type_redistributed (int type
)
504 return (DEFAULT_ROUTE_TYPE (type
)) ?
505 zclient
->default_information
: zclient
->redist
[type
];
509 ospf_redistribute_set (struct ospf
*ospf
, int type
, int mtype
, int mvalue
)
513 if (ospf_is_type_redistributed (type
))
515 if (mtype
!= ospf
->dmetric
[type
].type
)
517 ospf
->dmetric
[type
].type
= mtype
;
518 force
= LSA_REFRESH_FORCE
;
520 if (mvalue
!= ospf
->dmetric
[type
].value
)
522 ospf
->dmetric
[type
].value
= mvalue
;
523 force
= LSA_REFRESH_FORCE
;
526 ospf_external_lsa_refresh_type (ospf
, type
, force
);
528 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
529 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
530 LOOKUP (ospf_redistributed_proto
, type
),
531 metric_type (ospf
, type
), metric_value (ospf
, type
));
536 ospf
->dmetric
[type
].type
= mtype
;
537 ospf
->dmetric
[type
].value
= mvalue
;
539 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
541 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
542 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
543 LOOKUP (ospf_redistributed_proto
, type
),
544 metric_type (ospf
, type
), metric_value (ospf
, type
));
546 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
552 ospf_redistribute_unset (struct ospf
*ospf
, int type
)
554 if (type
== zclient
->redist_default
)
557 if (!ospf_is_type_redistributed (type
))
560 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
562 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
563 zlog_debug ("Redistribute[%s]: Stop",
564 LOOKUP (ospf_redistributed_proto
, type
));
566 ospf
->dmetric
[type
].type
= -1;
567 ospf
->dmetric
[type
].value
= -1;
569 /* Remove the routes from OSPF table. */
570 ospf_redistribute_withdraw (type
);
572 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
578 ospf_redistribute_default_set (struct ospf
*ospf
, int originate
,
579 int mtype
, int mvalue
)
583 if (ospf_is_type_redistributed (DEFAULT_ROUTE
))
585 if (mtype
!= ospf
->dmetric
[DEFAULT_ROUTE
].type
)
587 ospf
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
590 if (mvalue
!= ospf
->dmetric
[DEFAULT_ROUTE
].value
)
593 ospf
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
596 ospf_external_lsa_refresh_default (ospf
);
598 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
599 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
600 LOOKUP (ospf_redistributed_proto
, DEFAULT_ROUTE
),
601 metric_type (ospf
, DEFAULT_ROUTE
),
602 metric_value (ospf
, DEFAULT_ROUTE
));
606 ospf
->default_originate
= originate
;
607 ospf
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
608 ospf
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
610 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
);
612 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
613 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
614 metric_type (ospf
, DEFAULT_ROUTE
),
615 metric_value (ospf
, DEFAULT_ROUTE
));
617 if (ospf
->router_id
.s_addr
== 0)
618 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
620 thread_add_timer (master
, ospf_default_originate_timer
,
621 &ospf
->default_originate
, 1);
623 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
629 ospf_redistribute_default_unset (struct ospf
*ospf
)
631 if (!ospf_is_type_redistributed (DEFAULT_ROUTE
))
634 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
635 ospf
->dmetric
[DEFAULT_ROUTE
].type
= -1;
636 ospf
->dmetric
[DEFAULT_ROUTE
].value
= -1;
638 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
);
640 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
641 zlog_debug ("Redistribute[DEFAULT]: Stop");
643 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
649 ospf_external_lsa_originate_check (struct ospf
*ospf
,
650 struct external_info
*ei
)
652 /* If prefix is multicast, then do not originate LSA. */
653 if (IN_MULTICAST (htonl (ei
->p
.prefix
.s_addr
)))
655 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
656 "Prefix belongs multicast", inet_ntoa (ei
->p
.prefix
));
660 /* Take care of default-originate. */
661 if (is_prefix_default (&ei
->p
))
662 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
)
664 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
672 /* If connected prefix is OSPF enable interface, then do not announce. */
674 ospf_distribute_check_connected (struct ospf
*ospf
, struct external_info
*ei
)
676 struct route_node
*rn
;
678 for (rn
= route_top (ospf
->networks
); rn
; rn
= route_next (rn
))
679 if (rn
->info
!= NULL
)
680 if (prefix_match (&rn
->p
, (struct prefix
*) &ei
->p
))
682 route_unlock_node (rn
);
689 /* return 1 if external LSA must be originated, 0 otherwise */
691 ospf_redistribute_check (struct ospf
*ospf
,
692 struct external_info
*ei
, int *changed
)
694 struct route_map_set_values save_values
;
695 struct prefix_ipv4
*p
= &ei
->p
;
696 u_char type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
701 if (!ospf_external_lsa_originate_check (ospf
, ei
))
704 /* Take care connected route. */
705 if (type
== ZEBRA_ROUTE_CONNECT
&&
706 !ospf_distribute_check_connected (ospf
, ei
))
709 if (!DEFAULT_ROUTE_TYPE (type
) && DISTRIBUTE_NAME (ospf
, type
))
710 /* distirbute-list exists, but access-list may not? */
711 if (DISTRIBUTE_LIST (ospf
, type
))
712 if (access_list_apply (DISTRIBUTE_LIST (ospf
, type
), p
) == FILTER_DENY
)
714 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
715 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
716 LOOKUP (ospf_redistributed_proto
, type
),
717 inet_ntoa (p
->prefix
), p
->prefixlen
);
721 save_values
= ei
->route_map_set
;
722 ospf_reset_route_map_set_values (&ei
->route_map_set
);
724 /* apply route-map if needed */
725 if (ROUTEMAP_NAME (ospf
, type
))
729 ret
= route_map_apply (ROUTEMAP (ospf
, type
), (struct prefix
*) p
,
732 if (ret
== RMAP_DENYMATCH
)
734 ei
->route_map_set
= save_values
;
735 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
736 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
737 LOOKUP (ospf_redistributed_proto
, type
),
738 inet_ntoa (p
->prefix
), p
->prefixlen
);
742 /* check if 'route-map set' changed something */
744 *changed
= !ospf_route_map_set_compare (&ei
->route_map_set
,
751 /* OSPF route-map set for redistribution */
753 ospf_routemap_set (struct ospf
*ospf
, int type
, const char *name
)
755 if (ROUTEMAP_NAME (ospf
, type
))
756 free (ROUTEMAP_NAME (ospf
, type
));
758 ROUTEMAP_NAME (ospf
, type
) = strdup (name
);
759 ROUTEMAP (ospf
, type
) = route_map_lookup_by_name (name
);
763 ospf_routemap_unset (struct ospf
*ospf
, int type
)
765 if (ROUTEMAP_NAME (ospf
, type
))
766 free (ROUTEMAP_NAME (ospf
, type
));
768 ROUTEMAP_NAME (ospf
, type
) = NULL
;
769 ROUTEMAP (ospf
, type
) = NULL
;
772 /* Zebra route add and delete treatment. */
774 ospf_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
778 struct zapi_ipv4 api
;
779 unsigned long ifindex
;
780 struct in_addr nexthop
;
781 struct prefix_ipv4 p
;
782 struct external_info
*ei
;
789 /* Type, flags, message. */
790 api
.type
= stream_getc (s
);
791 api
.flags
= stream_getc (s
);
792 api
.message
= stream_getc (s
);
795 memset (&p
, 0, sizeof (struct prefix_ipv4
));
797 p
.prefixlen
= stream_getc (s
);
798 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
800 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
803 /* Nexthop, ifindex, distance, metric. */
804 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
806 api
.nexthop_num
= stream_getc (s
);
807 nexthop
.s_addr
= stream_get_ipv4 (s
);
809 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
811 api
.ifindex_num
= stream_getc (s
);
812 /* XXX assert(api.ifindex_num == 1); */
813 ifindex
= stream_getl (s
);
815 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
816 api
.distance
= stream_getc (s
);
817 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
818 api
.metric
= stream_getl (s
);
820 ospf
= ospf_lookup ();
824 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
826 /* XXX|HACK|TODO|FIXME:
827 * Maybe we should ignore reject/blackhole routes? Testing shows that
828 * there is no problems though and this is only way to "summarize"
829 * routes in ASBR at the moment. Maybe we need just a better generalised
830 * solution for these types?
832 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
833 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
837 ei
= ospf_external_info_add (api
.type
, p
, ifindex
, nexthop
);
839 if (ospf
->router_id
.s_addr
== 0)
840 /* Set flags to generate AS-external-LSA originate event
841 for each redistributed protocols later. */
842 ospf
->external_origin
|= (1 << api
.type
);
847 if (is_prefix_default (&p
))
848 ospf_external_lsa_refresh_default (ospf
);
851 struct ospf_lsa
*current
;
853 current
= ospf_external_info_find_lsa (ospf
, &ei
->p
);
855 ospf_external_lsa_originate (ospf
, ei
);
856 else if (IS_LSA_MAXAGE (current
))
857 ospf_external_lsa_refresh (ospf
, current
,
858 ei
, LSA_REFRESH_FORCE
);
860 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
861 inet_ntoa (p
.prefix
));
866 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
868 ospf_external_info_delete (api
.type
, p
);
869 if (is_prefix_default (&p
))
870 ospf_external_lsa_refresh_default (ospf
);
872 ospf_external_lsa_flush (ospf
, api
.type
, &p
, ifindex
, nexthop
);
880 ospf_distribute_list_out_set (struct ospf
*ospf
, int type
, const char *name
)
882 /* Lookup access-list for distribute-list. */
883 DISTRIBUTE_LIST (ospf
, type
) = access_list_lookup (AFI_IP
, name
);
885 /* Clear previous distribute-name. */
886 if (DISTRIBUTE_NAME (ospf
, type
))
887 free (DISTRIBUTE_NAME (ospf
, type
));
889 /* Set distribute-name. */
890 DISTRIBUTE_NAME (ospf
, type
) = strdup (name
);
892 /* If access-list have been set, schedule update timer. */
893 if (DISTRIBUTE_LIST (ospf
, type
))
894 ospf_distribute_list_update (ospf
, type
);
900 ospf_distribute_list_out_unset (struct ospf
*ospf
, int type
, const char *name
)
902 /* Schedule update timer. */
903 if (DISTRIBUTE_LIST (ospf
, type
))
904 ospf_distribute_list_update (ospf
, type
);
906 /* Unset distribute-list. */
907 DISTRIBUTE_LIST (ospf
, type
) = NULL
;
909 /* Clear distribute-name. */
910 if (DISTRIBUTE_NAME (ospf
, type
))
911 free (DISTRIBUTE_NAME (ospf
, type
));
913 DISTRIBUTE_NAME (ospf
, type
) = NULL
;
918 /* distribute-list update timer. */
920 ospf_distribute_list_update_timer (struct thread
*thread
)
922 struct route_node
*rn
;
923 struct external_info
*ei
;
924 struct route_table
*rt
;
925 struct ospf_lsa
*lsa
;
929 type
= (int) THREAD_ARG (thread
);
930 assert (type
< ZEBRA_ROUTE_MAX
);
932 rt
= EXTERNAL_INFO (type
);
934 ospf
= ospf_lookup ();
938 ospf
->t_distribute_update
= NULL
;
940 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
942 /* foreach all external info. */
944 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
945 if ((ei
= rn
->info
) != NULL
)
947 if (is_prefix_default (&ei
->p
))
948 ospf_external_lsa_refresh_default (ospf
);
949 else if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
950 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_IF_CHANGED
);
952 ospf_external_lsa_originate (ospf
, ei
);
957 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
959 /* Update distribute-list and set timer to apply access-list. */
961 ospf_distribute_list_update (struct ospf
*ospf
, int type
)
963 struct route_table
*rt
;
965 /* External info does not exist. */
966 if (!(rt
= EXTERNAL_INFO (type
)))
969 /* If exists previously invoked thread, then cancel it. */
970 if (ospf
->t_distribute_update
)
971 OSPF_TIMER_OFF (ospf
->t_distribute_update
);
974 ospf
->t_distribute_update
=
975 thread_add_timer (master
, ospf_distribute_list_update_timer
,
976 (void *) type
, OSPF_DISTRIBUTE_UPDATE_DELAY
);
979 /* If access-list is updated, apply some check. */
981 ospf_filter_update (struct access_list
*access
)
986 struct ospf_area
*area
;
987 struct listnode
*node
;
989 /* If OSPF instatnce does not exist, return right now. */
990 ospf
= ospf_lookup ();
994 /* Update distribute-list, and apply filter. */
995 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
997 if (ROUTEMAP (ospf
, type
) != NULL
)
999 /* if route-map is not NULL it may be using this access list */
1000 ospf_distribute_list_update (ospf
, type
);
1005 if (DISTRIBUTE_NAME (ospf
, type
))
1007 /* Keep old access-list for distribute-list. */
1008 struct access_list
*old
= DISTRIBUTE_LIST (ospf
, type
);
1010 /* Update access-list for distribute-list. */
1011 DISTRIBUTE_LIST (ospf
, type
) =
1012 access_list_lookup (AFI_IP
, DISTRIBUTE_NAME (ospf
, type
));
1014 /* No update for this distribute type. */
1015 if (old
== NULL
&& DISTRIBUTE_LIST (ospf
, type
) == NULL
)
1018 /* Schedule distribute-list update timer. */
1019 if (DISTRIBUTE_LIST (ospf
, type
) == NULL
||
1020 strcmp (DISTRIBUTE_NAME (ospf
, type
), access
->name
) == 0)
1021 ospf_distribute_list_update (ospf
, type
);
1025 /* Update Area access-list. */
1026 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1028 if (EXPORT_NAME (area
))
1030 EXPORT_LIST (area
) = NULL
;
1034 if (IMPORT_NAME (area
))
1036 IMPORT_LIST (area
) = NULL
;
1041 /* Schedule ABR tasks -- this will be changed -- takada. */
1042 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1043 ospf_schedule_abr_task (ospf
);
1046 /* If prefix-list is updated, do some updates. */
1048 ospf_prefix_list_update (struct prefix_list
*plist
)
1053 struct ospf_area
*area
;
1054 struct listnode
*node
;
1056 /* If OSPF instatnce does not exist, return right now. */
1057 ospf
= ospf_lookup ();
1061 /* Update all route-maps which are used as redistribution filters.
1062 * They might use prefix-list.
1064 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
1066 if (ROUTEMAP (ospf
, type
) != NULL
)
1068 /* If route-map is not NULL it may be using this prefix list */
1069 ospf_distribute_list_update (ospf
, type
);
1074 /* Update area filter-lists. */
1075 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1077 /* Update filter-list in. */
1078 if (PREFIX_NAME_IN (area
))
1079 if (strcmp (PREFIX_NAME_IN (area
), plist
->name
) == 0)
1081 PREFIX_LIST_IN (area
) =
1082 prefix_list_lookup (AFI_IP
, PREFIX_NAME_IN (area
));
1086 /* Update filter-list out. */
1087 if (PREFIX_NAME_OUT (area
))
1088 if (strcmp (PREFIX_NAME_OUT (area
), plist
->name
) == 0)
1090 PREFIX_LIST_IN (area
) =
1091 prefix_list_lookup (AFI_IP
, PREFIX_NAME_OUT (area
));
1096 /* Schedule ABR task. */
1097 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1098 ospf_schedule_abr_task (ospf
);
1101 struct ospf_distance
*
1102 ospf_distance_new ()
1104 struct ospf_distance
*new;
1105 new = XMALLOC (MTYPE_OSPF_DISTANCE
, sizeof (struct ospf_distance
));
1106 memset (new, 0, sizeof (struct ospf_distance
));
1111 ospf_distance_free (struct ospf_distance
*odistance
)
1113 XFREE (MTYPE_OSPF_DISTANCE
, odistance
);
1117 ospf_distance_set (struct vty
*vty
, struct ospf
*ospf
,
1118 const char *distance_str
,
1120 const char *access_list_str
)
1123 struct prefix_ipv4 p
;
1125 struct route_node
*rn
;
1126 struct ospf_distance
*odistance
;
1128 ret
= str2prefix_ipv4 (ip_str
, &p
);
1131 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1135 distance
= atoi (distance_str
);
1137 /* Get OSPF distance node. */
1138 rn
= route_node_get (ospf
->distance_table
, (struct prefix
*) &p
);
1141 odistance
= rn
->info
;
1142 route_unlock_node (rn
);
1146 odistance
= ospf_distance_new ();
1147 rn
->info
= odistance
;
1150 /* Set distance value. */
1151 odistance
->distance
= distance
;
1153 /* Reset access-list configuration. */
1154 if (odistance
->access_list
)
1156 free (odistance
->access_list
);
1157 odistance
->access_list
= NULL
;
1159 if (access_list_str
)
1160 odistance
->access_list
= strdup (access_list_str
);
1166 ospf_distance_unset (struct vty
*vty
, struct ospf
*ospf
,
1167 const char *distance_str
,
1168 const char *ip_str
, char
1169 const *access_list_str
)
1172 struct prefix_ipv4 p
;
1174 struct route_node
*rn
;
1175 struct ospf_distance
*odistance
;
1177 ret
= str2prefix_ipv4 (ip_str
, &p
);
1180 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1184 distance
= atoi (distance_str
);
1186 rn
= route_node_lookup (ospf
->distance_table
, (struct prefix
*) &p
);
1189 vty_out (vty
, "Can't find specified prefix%s", VTY_NEWLINE
);
1193 odistance
= rn
->info
;
1195 if (odistance
->access_list
)
1196 free (odistance
->access_list
);
1197 ospf_distance_free (odistance
);
1200 route_unlock_node (rn
);
1201 route_unlock_node (rn
);
1207 ospf_distance_reset (struct ospf
*ospf
)
1209 struct route_node
*rn
;
1210 struct ospf_distance
*odistance
;
1212 for (rn
= route_top (ospf
->distance_table
); rn
; rn
= route_next (rn
))
1213 if ((odistance
= rn
->info
) != NULL
)
1215 if (odistance
->access_list
)
1216 free (odistance
->access_list
);
1217 ospf_distance_free (odistance
);
1219 route_unlock_node (rn
);
1224 ospf_distance_apply (struct prefix_ipv4
*p
, struct ospf_route
*or)
1228 ospf
= ospf_lookup ();
1232 if (ospf
->distance_intra
)
1233 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1234 return ospf
->distance_intra
;
1236 if (ospf
->distance_inter
)
1237 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1238 return ospf
->distance_inter
;
1240 if (ospf
->distance_external
)
1241 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
1242 || or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1243 return ospf
->distance_external
;
1245 if (ospf
->distance_all
)
1246 return ospf
->distance_all
;
1254 /* Allocate zebra structure. */
1255 zclient
= zclient_new ();
1256 zclient_init (zclient
, ZEBRA_ROUTE_OSPF
);
1257 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1258 zclient
->interface_add
= ospf_interface_add
;
1259 zclient
->interface_delete
= ospf_interface_delete
;
1260 zclient
->interface_up
= ospf_interface_state_up
;
1261 zclient
->interface_down
= ospf_interface_state_down
;
1262 zclient
->interface_address_add
= ospf_interface_address_add
;
1263 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1264 zclient
->ipv4_route_add
= ospf_zebra_read_ipv4
;
1265 zclient
->ipv4_route_delete
= ospf_zebra_read_ipv4
;
1267 access_list_add_hook (ospf_filter_update
);
1268 access_list_delete_hook (ospf_filter_update
);
1269 prefix_list_add_hook (ospf_prefix_list_update
);
1270 prefix_list_delete_hook (ospf_prefix_list_update
);