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_IFTYPE_BROADCAST
;
100 if (if_is_broadcast (ifp
))
101 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_BROADCAST
;
102 else if (if_is_pointopoint (ifp
))
103 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_POINTOPOINT
;
104 else if (if_is_loopback (ifp
))
105 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_LOOPBACK
;
108 ospf
= ospf_lookup ();
110 ospf_if_update (ospf
);
113 ospf_snmp_if_update (ifp
);
114 #endif /* HAVE_SNMP */
120 ospf_interface_delete (int command
, struct zclient
*zclient
,
123 struct interface
*ifp
;
125 struct route_node
*rn
;
128 /* zebra_interface_state_read() updates interface structure in iflist */
129 ifp
= zebra_interface_state_read (s
);
135 zlog_warn ("Zebra: got delete of %s, but interface is still up",
138 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
140 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
141 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu
);
144 ospf_snmp_if_delete (ifp
);
145 #endif /* HAVE_SNMP */
147 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
149 ospf_if_free ((struct ospf_interface
*) rn
->info
);
155 zebra_interface_if_lookup (struct stream
*s
)
157 struct interface
*ifp
;
158 u_char ifname_tmp
[INTERFACE_NAMSIZ
];
160 /* Read interface name. */
161 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
163 /* Lookup this by interface index. */
164 ifp
= if_lookup_by_name ((char *) ifname_tmp
);
166 /* If such interface does not exist, indicate an error */
174 ospf_interface_state_up (int command
, struct zclient
*zclient
,
177 struct interface
*ifp
;
178 struct interface if_tmp
;
179 struct ospf_interface
*oi
;
180 struct route_node
*rn
;
182 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
187 /* Interface is already up. */
188 if (if_is_operative (ifp
))
190 /* Temporarily keep ifp values. */
191 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
193 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
195 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
196 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
198 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
200 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
201 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
202 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
204 ospf_if_recalculate_output_cost (ifp
);
209 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
211 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
212 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp
->name
);
214 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
216 if ((oi
= rn
->info
) == NULL
)
226 ospf_interface_state_down (int command
, struct zclient
*zclient
,
229 struct interface
*ifp
;
230 struct ospf_interface
*oi
;
231 struct route_node
*node
;
233 ifp
= zebra_interface_state_read (zclient
->ibuf
);
238 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
239 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
241 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
243 if ((oi
= node
->info
) == NULL
)
252 ospf_interface_address_add (int command
, struct zclient
*zclient
,
258 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
263 ospf
= ospf_lookup ();
265 ospf_if_update (ospf
);
268 ospf_snmp_if_update (c
->ifp
);
269 #endif /* HAVE_SNMP */
275 ospf_interface_address_delete (int command
, struct zclient
*zclient
,
280 struct interface
*ifp
;
281 struct ospf_interface
*oi
;
282 struct route_node
*rn
;
285 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
292 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
294 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
301 /* Call interface hook functions to clean up */
305 ospf_snmp_if_update (c
->ifp
);
306 #endif /* HAVE_SNMP */
310 ospf
= ospf_lookup ();
312 ospf_if_update (ospf
);
318 ospf_zebra_add (struct prefix_ipv4
*p
, struct ospf_route
*or)
325 struct ospf_path
*path
;
326 struct listnode
*node
;
328 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
333 /* OSPF pass nexthop and metric */
334 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
335 SET_FLAG (message
, ZAPI_MESSAGE_METRIC
);
337 /* Distance value. */
338 distance
= ospf_distance_apply (p
, or);
340 SET_FLAG (message
, ZAPI_MESSAGE_DISTANCE
);
346 /* Length place holder. */
349 /* Put command, type, flags, message. */
350 stream_putc (s
, ZEBRA_IPV4_ROUTE_ADD
);
351 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
352 stream_putc (s
, flags
);
353 stream_putc (s
, message
);
355 /* Put prefix information. */
356 psize
= PSIZE (p
->prefixlen
);
357 stream_putc (s
, p
->prefixlen
);
358 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
361 stream_putc (s
, or->paths
->count
);
363 /* Nexthop, ifindex, distance and metric information. */
364 for (node
= listhead (or->paths
); node
; nextnode (node
))
366 path
= getdata (node
);
368 if (path
->nexthop
.s_addr
!= INADDR_ANY
)
370 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
371 stream_put_in_addr (s
, &path
->nexthop
);
375 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
377 stream_putl (s
, path
->oi
->ifp
->ifindex
);
382 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
384 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
385 inet_ntoa (p
->prefix
),
386 p
->prefixlen
, inet_ntoa (path
->nexthop
));
390 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
391 stream_putc (s
, distance
);
392 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
394 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
395 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
396 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
397 stream_putl (s
, or->u
.ext
.type2_cost
);
399 stream_putl (s
, or->cost
);
402 stream_putw_at (s
, 0, stream_get_endp (s
));
404 writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
409 ospf_zebra_delete (struct prefix_ipv4
*p
, struct ospf_route
*or)
411 struct zapi_ipv4 api
;
412 struct ospf_path
*path
;
413 struct in_addr
*nexthop
;
414 struct listnode
*node
;
416 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
418 api
.type
= ZEBRA_ROUTE_OSPF
;
424 for (node
= listhead (or->paths
); node
; nextnode (node
))
426 path
= getdata (node
);
428 if (path
->nexthop
.s_addr
!= INADDR_ANY
)
430 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
432 nexthop
= &path
->nexthop
;
433 api
.nexthop
= &nexthop
;
435 else if (ospf_if_exists(path
->oi
) && (path
->oi
->ifp
))
437 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
439 api
.ifindex
= &path
->oi
->ifp
->ifindex
;
441 else if ( IS_DEBUG_OSPF(zebra
,ZEBRA_REDISTRIBUTE
) )
443 zlog_debug("Zebra: no ifp %s %d",
444 inet_ntoa(p
->prefix
),
448 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
450 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
) && api
.nexthop_num
)
452 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
453 inet_ntoa (p
->prefix
),
454 p
->prefixlen
, inet_ntoa (**api
.nexthop
));
456 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
) && api
.ifindex_num
)
458 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
459 inet_ntoa (p
->prefix
),
460 p
->prefixlen
, *api
.ifindex
);
467 ospf_zebra_add_discard (struct prefix_ipv4
*p
)
469 struct zapi_ipv4 api
;
471 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
473 api
.type
= ZEBRA_ROUTE_OSPF
;
474 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
476 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
480 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD
, zclient
, p
, &api
);
485 ospf_zebra_delete_discard (struct prefix_ipv4
*p
)
487 struct zapi_ipv4 api
;
489 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
491 api
.type
= ZEBRA_ROUTE_OSPF
;
492 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
494 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
498 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
500 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
501 zlog_debug ("Zebra: Route delete discard %s/%d",
502 inet_ntoa (p
->prefix
), p
->prefixlen
);
508 ospf_is_type_redistributed (int type
)
510 return (DEFAULT_ROUTE_TYPE (type
)) ?
511 zclient
->default_information
: zclient
->redist
[type
];
515 ospf_redistribute_set (struct ospf
*ospf
, int type
, int mtype
, int mvalue
)
519 if (ospf_is_type_redistributed (type
))
521 if (mtype
!= ospf
->dmetric
[type
].type
)
523 ospf
->dmetric
[type
].type
= mtype
;
524 force
= LSA_REFRESH_FORCE
;
526 if (mvalue
!= ospf
->dmetric
[type
].value
)
528 ospf
->dmetric
[type
].value
= mvalue
;
529 force
= LSA_REFRESH_FORCE
;
532 ospf_external_lsa_refresh_type (ospf
, type
, force
);
534 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
535 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
536 LOOKUP (ospf_redistributed_proto
, type
),
537 metric_type (ospf
, type
), metric_value (ospf
, type
));
542 ospf
->dmetric
[type
].type
= mtype
;
543 ospf
->dmetric
[type
].value
= mvalue
;
545 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
547 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
548 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
549 LOOKUP (ospf_redistributed_proto
, type
),
550 metric_type (ospf
, type
), metric_value (ospf
, type
));
552 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
558 ospf_redistribute_unset (struct ospf
*ospf
, int type
)
560 if (type
== zclient
->redist_default
)
563 if (!ospf_is_type_redistributed (type
))
566 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
568 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
569 zlog_debug ("Redistribute[%s]: Stop",
570 LOOKUP (ospf_redistributed_proto
, type
));
572 ospf
->dmetric
[type
].type
= -1;
573 ospf
->dmetric
[type
].value
= -1;
575 /* Remove the routes from OSPF table. */
576 ospf_redistribute_withdraw (type
);
578 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
584 ospf_redistribute_default_set (struct ospf
*ospf
, int originate
,
585 int mtype
, int mvalue
)
589 if (ospf_is_type_redistributed (DEFAULT_ROUTE
))
591 if (mtype
!= ospf
->dmetric
[DEFAULT_ROUTE
].type
)
593 ospf
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
596 if (mvalue
!= ospf
->dmetric
[DEFAULT_ROUTE
].value
)
599 ospf
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
602 ospf_external_lsa_refresh_default (ospf
);
604 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
605 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
606 LOOKUP (ospf_redistributed_proto
, DEFAULT_ROUTE
),
607 metric_type (ospf
, DEFAULT_ROUTE
),
608 metric_value (ospf
, DEFAULT_ROUTE
));
612 ospf
->default_originate
= originate
;
613 ospf
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
614 ospf
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
616 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
);
618 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
619 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
620 metric_type (ospf
, DEFAULT_ROUTE
),
621 metric_value (ospf
, DEFAULT_ROUTE
));
623 if (ospf
->router_id
.s_addr
== 0)
624 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
626 thread_add_timer (master
, ospf_default_originate_timer
,
627 &ospf
->default_originate
, 1);
629 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
635 ospf_redistribute_default_unset (struct ospf
*ospf
)
637 if (!ospf_is_type_redistributed (DEFAULT_ROUTE
))
640 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
641 ospf
->dmetric
[DEFAULT_ROUTE
].type
= -1;
642 ospf
->dmetric
[DEFAULT_ROUTE
].value
= -1;
644 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
);
646 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
647 zlog_debug ("Redistribute[DEFAULT]: Stop");
649 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
655 ospf_external_lsa_originate_check (struct ospf
*ospf
,
656 struct external_info
*ei
)
658 /* If prefix is multicast, then do not originate LSA. */
659 if (IN_MULTICAST (htonl (ei
->p
.prefix
.s_addr
)))
661 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
662 "Prefix belongs multicast", inet_ntoa (ei
->p
.prefix
));
666 /* Take care of default-originate. */
667 if (is_prefix_default (&ei
->p
))
668 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
)
670 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
678 /* If connected prefix is OSPF enable interface, then do not announce. */
680 ospf_distribute_check_connected (struct ospf
*ospf
, struct external_info
*ei
)
682 struct route_node
*rn
;
684 for (rn
= route_top (ospf
->networks
); rn
; rn
= route_next (rn
))
685 if (rn
->info
!= NULL
)
686 if (prefix_match (&rn
->p
, (struct prefix
*) &ei
->p
))
688 route_unlock_node (rn
);
695 /* return 1 if external LSA must be originated, 0 otherwise */
697 ospf_redistribute_check (struct ospf
*ospf
,
698 struct external_info
*ei
, int *changed
)
700 struct route_map_set_values save_values
;
701 struct prefix_ipv4
*p
= &ei
->p
;
702 u_char type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
707 if (!ospf_external_lsa_originate_check (ospf
, ei
))
710 /* Take care connected route. */
711 if (type
== ZEBRA_ROUTE_CONNECT
&&
712 !ospf_distribute_check_connected (ospf
, ei
))
715 if (!DEFAULT_ROUTE_TYPE (type
) && DISTRIBUTE_NAME (ospf
, type
))
716 /* distirbute-list exists, but access-list may not? */
717 if (DISTRIBUTE_LIST (ospf
, type
))
718 if (access_list_apply (DISTRIBUTE_LIST (ospf
, type
), p
) == FILTER_DENY
)
720 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
721 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
722 LOOKUP (ospf_redistributed_proto
, type
),
723 inet_ntoa (p
->prefix
), p
->prefixlen
);
727 save_values
= ei
->route_map_set
;
728 ospf_reset_route_map_set_values (&ei
->route_map_set
);
730 /* apply route-map if needed */
731 if (ROUTEMAP_NAME (ospf
, type
))
735 ret
= route_map_apply (ROUTEMAP (ospf
, type
), (struct prefix
*) p
,
738 if (ret
== RMAP_DENYMATCH
)
740 ei
->route_map_set
= save_values
;
741 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
742 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
743 LOOKUP (ospf_redistributed_proto
, type
),
744 inet_ntoa (p
->prefix
), p
->prefixlen
);
748 /* check if 'route-map set' changed something */
750 *changed
= !ospf_route_map_set_compare (&ei
->route_map_set
,
757 /* OSPF route-map set for redistribution */
759 ospf_routemap_set (struct ospf
*ospf
, int type
, const char *name
)
761 if (ROUTEMAP_NAME (ospf
, type
))
762 free (ROUTEMAP_NAME (ospf
, type
));
764 ROUTEMAP_NAME (ospf
, type
) = strdup (name
);
765 ROUTEMAP (ospf
, type
) = route_map_lookup_by_name (name
);
769 ospf_routemap_unset (struct ospf
*ospf
, int type
)
771 if (ROUTEMAP_NAME (ospf
, type
))
772 free (ROUTEMAP_NAME (ospf
, type
));
774 ROUTEMAP_NAME (ospf
, type
) = NULL
;
775 ROUTEMAP (ospf
, type
) = NULL
;
778 /* Zebra route add and delete treatment. */
780 ospf_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
784 struct zapi_ipv4 api
;
785 unsigned long ifindex
;
786 struct in_addr nexthop
;
787 struct prefix_ipv4 p
;
788 struct external_info
*ei
;
795 /* Type, flags, message. */
796 api
.type
= stream_getc (s
);
797 api
.flags
= stream_getc (s
);
798 api
.message
= stream_getc (s
);
801 memset (&p
, 0, sizeof (struct prefix_ipv4
));
803 p
.prefixlen
= stream_getc (s
);
804 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
806 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
809 /* Nexthop, ifindex, distance, metric. */
810 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
812 api
.nexthop_num
= stream_getc (s
);
813 nexthop
.s_addr
= stream_get_ipv4 (s
);
815 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
817 api
.ifindex_num
= stream_getc (s
);
818 /* XXX assert(api.ifindex_num == 1); */
819 ifindex
= stream_getl (s
);
821 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
822 api
.distance
= stream_getc (s
);
823 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
824 api
.metric
= stream_getl (s
);
826 ospf
= ospf_lookup ();
830 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
832 /* XXX|HACK|TODO|FIXME:
833 * Maybe we should ignore reject/blackhole routes? Testing shows that
834 * there is no problems though and this is only way to "summarize"
835 * routes in ASBR at the moment. Maybe we need just a better generalised
836 * solution for these types?
838 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
839 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
843 ei
= ospf_external_info_add (api
.type
, p
, ifindex
, nexthop
);
845 if (ospf
->router_id
.s_addr
== 0)
846 /* Set flags to generate AS-external-LSA originate event
847 for each redistributed protocols later. */
848 ospf
->external_origin
|= (1 << api
.type
);
853 if (is_prefix_default (&p
))
854 ospf_external_lsa_refresh_default (ospf
);
857 struct ospf_lsa
*current
;
859 current
= ospf_external_info_find_lsa (ospf
, &ei
->p
);
861 ospf_external_lsa_originate (ospf
, ei
);
862 else if (IS_LSA_MAXAGE (current
))
863 ospf_external_lsa_refresh (ospf
, current
,
864 ei
, LSA_REFRESH_FORCE
);
866 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
867 inet_ntoa (p
.prefix
));
872 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
874 ospf_external_info_delete (api
.type
, p
);
875 if (is_prefix_default (&p
))
876 ospf_external_lsa_refresh_default (ospf
);
878 ospf_external_lsa_flush (ospf
, api
.type
, &p
, ifindex
, nexthop
);
886 ospf_distribute_list_out_set (struct ospf
*ospf
, int type
, const char *name
)
888 /* Lookup access-list for distribute-list. */
889 DISTRIBUTE_LIST (ospf
, type
) = access_list_lookup (AFI_IP
, name
);
891 /* Clear previous distribute-name. */
892 if (DISTRIBUTE_NAME (ospf
, type
))
893 free (DISTRIBUTE_NAME (ospf
, type
));
895 /* Set distribute-name. */
896 DISTRIBUTE_NAME (ospf
, type
) = strdup (name
);
898 /* If access-list have been set, schedule update timer. */
899 if (DISTRIBUTE_LIST (ospf
, type
))
900 ospf_distribute_list_update (ospf
, type
);
906 ospf_distribute_list_out_unset (struct ospf
*ospf
, int type
, const char *name
)
908 /* Schedule update timer. */
909 if (DISTRIBUTE_LIST (ospf
, type
))
910 ospf_distribute_list_update (ospf
, type
);
912 /* Unset distribute-list. */
913 DISTRIBUTE_LIST (ospf
, type
) = NULL
;
915 /* Clear distribute-name. */
916 if (DISTRIBUTE_NAME (ospf
, type
))
917 free (DISTRIBUTE_NAME (ospf
, type
));
919 DISTRIBUTE_NAME (ospf
, type
) = NULL
;
924 /* distribute-list update timer. */
926 ospf_distribute_list_update_timer (struct thread
*thread
)
928 struct route_node
*rn
;
929 struct external_info
*ei
;
930 struct route_table
*rt
;
931 struct ospf_lsa
*lsa
;
935 type
= (int) THREAD_ARG (thread
);
936 assert (type
< ZEBRA_ROUTE_MAX
);
938 rt
= EXTERNAL_INFO (type
);
940 ospf
= ospf_lookup ();
944 ospf
->t_distribute_update
= NULL
;
946 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
948 /* foreach all external info. */
950 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
951 if ((ei
= rn
->info
) != NULL
)
953 if (is_prefix_default (&ei
->p
))
954 ospf_external_lsa_refresh_default (ospf
);
955 else if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
956 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_IF_CHANGED
);
958 ospf_external_lsa_originate (ospf
, ei
);
963 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
965 /* Update distribute-list and set timer to apply access-list. */
967 ospf_distribute_list_update (struct ospf
*ospf
, int type
)
969 struct route_table
*rt
;
971 /* External info does not exist. */
972 if (!(rt
= EXTERNAL_INFO (type
)))
975 /* If exists previously invoked thread, then cancel it. */
976 if (ospf
->t_distribute_update
)
977 OSPF_TIMER_OFF (ospf
->t_distribute_update
);
980 ospf
->t_distribute_update
=
981 thread_add_timer (master
, ospf_distribute_list_update_timer
,
982 (void *) type
, OSPF_DISTRIBUTE_UPDATE_DELAY
);
985 /* If access-list is updated, apply some check. */
987 ospf_filter_update (struct access_list
*access
)
992 struct ospf_area
*area
;
993 struct listnode
*node
;
995 /* If OSPF instatnce does not exist, return right now. */
996 ospf
= ospf_lookup ();
1000 /* Update distribute-list, and apply filter. */
1001 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
1003 if (ROUTEMAP (ospf
, type
) != NULL
)
1005 /* if route-map is not NULL it may be using this access list */
1006 ospf_distribute_list_update (ospf
, type
);
1011 if (DISTRIBUTE_NAME (ospf
, type
))
1013 /* Keep old access-list for distribute-list. */
1014 struct access_list
*old
= DISTRIBUTE_LIST (ospf
, type
);
1016 /* Update access-list for distribute-list. */
1017 DISTRIBUTE_LIST (ospf
, type
) =
1018 access_list_lookup (AFI_IP
, DISTRIBUTE_NAME (ospf
, type
));
1020 /* No update for this distribute type. */
1021 if (old
== NULL
&& DISTRIBUTE_LIST (ospf
, type
) == NULL
)
1024 /* Schedule distribute-list update timer. */
1025 if (DISTRIBUTE_LIST (ospf
, type
) == NULL
||
1026 strcmp (DISTRIBUTE_NAME (ospf
, type
), access
->name
) == 0)
1027 ospf_distribute_list_update (ospf
, type
);
1031 /* Update Area access-list. */
1032 for (node
= listhead (ospf
->areas
); node
; nextnode (node
))
1033 if ((area
= getdata (node
)) != NULL
)
1035 if (EXPORT_NAME (area
))
1037 EXPORT_LIST (area
) = NULL
;
1041 if (IMPORT_NAME (area
))
1043 IMPORT_LIST (area
) = NULL
;
1048 /* Schedule ABR tasks -- this will be changed -- takada. */
1049 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1050 ospf_schedule_abr_task (ospf
);
1053 /* If prefix-list is updated, do some updates. */
1055 ospf_prefix_list_update (struct prefix_list
*plist
)
1060 struct ospf_area
*area
;
1061 struct listnode
*node
;
1063 /* If OSPF instatnce does not exist, return right now. */
1064 ospf
= ospf_lookup ();
1068 /* Update all route-maps which are used as redistribution filters.
1069 * They might use prefix-list.
1071 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
1073 if (ROUTEMAP (ospf
, type
) != NULL
)
1075 /* If route-map is not NULL it may be using this prefix list */
1076 ospf_distribute_list_update (ospf
, type
);
1081 /* Update area filter-lists. */
1082 for (node
= listhead (ospf
->areas
); node
; nextnode (node
))
1083 if ((area
= getdata (node
)) != NULL
)
1085 /* Update filter-list in. */
1086 if (PREFIX_NAME_IN (area
))
1087 if (strcmp (PREFIX_NAME_IN (area
), plist
->name
) == 0)
1089 PREFIX_LIST_IN (area
) =
1090 prefix_list_lookup (AFI_IP
, PREFIX_NAME_IN (area
));
1094 /* Update filter-list out. */
1095 if (PREFIX_NAME_OUT (area
))
1096 if (strcmp (PREFIX_NAME_OUT (area
), plist
->name
) == 0)
1098 PREFIX_LIST_IN (area
) =
1099 prefix_list_lookup (AFI_IP
, PREFIX_NAME_OUT (area
));
1104 /* Schedule ABR task. */
1105 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1106 ospf_schedule_abr_task (ospf
);
1109 struct ospf_distance
*
1110 ospf_distance_new ()
1112 struct ospf_distance
*new;
1113 new = XMALLOC (MTYPE_OSPF_DISTANCE
, sizeof (struct ospf_distance
));
1114 memset (new, 0, sizeof (struct ospf_distance
));
1119 ospf_distance_free (struct ospf_distance
*odistance
)
1121 XFREE (MTYPE_OSPF_DISTANCE
, odistance
);
1125 ospf_distance_set (struct vty
*vty
, struct ospf
*ospf
,
1126 const char *distance_str
,
1128 const char *access_list_str
)
1131 struct prefix_ipv4 p
;
1133 struct route_node
*rn
;
1134 struct ospf_distance
*odistance
;
1136 ret
= str2prefix_ipv4 (ip_str
, &p
);
1139 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1143 distance
= atoi (distance_str
);
1145 /* Get OSPF distance node. */
1146 rn
= route_node_get (ospf
->distance_table
, (struct prefix
*) &p
);
1149 odistance
= rn
->info
;
1150 route_unlock_node (rn
);
1154 odistance
= ospf_distance_new ();
1155 rn
->info
= odistance
;
1158 /* Set distance value. */
1159 odistance
->distance
= distance
;
1161 /* Reset access-list configuration. */
1162 if (odistance
->access_list
)
1164 free (odistance
->access_list
);
1165 odistance
->access_list
= NULL
;
1167 if (access_list_str
)
1168 odistance
->access_list
= strdup (access_list_str
);
1174 ospf_distance_unset (struct vty
*vty
, struct ospf
*ospf
,
1175 const char *distance_str
,
1176 const char *ip_str
, char
1177 const *access_list_str
)
1180 struct prefix_ipv4 p
;
1182 struct route_node
*rn
;
1183 struct ospf_distance
*odistance
;
1185 ret
= str2prefix_ipv4 (ip_str
, &p
);
1188 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1192 distance
= atoi (distance_str
);
1194 rn
= route_node_lookup (ospf
->distance_table
, (struct prefix
*) &p
);
1197 vty_out (vty
, "Can't find specified prefix%s", VTY_NEWLINE
);
1201 odistance
= rn
->info
;
1203 if (odistance
->access_list
)
1204 free (odistance
->access_list
);
1205 ospf_distance_free (odistance
);
1208 route_unlock_node (rn
);
1209 route_unlock_node (rn
);
1215 ospf_distance_reset (struct ospf
*ospf
)
1217 struct route_node
*rn
;
1218 struct ospf_distance
*odistance
;
1220 for (rn
= route_top (ospf
->distance_table
); rn
; rn
= route_next (rn
))
1221 if ((odistance
= rn
->info
) != NULL
)
1223 if (odistance
->access_list
)
1224 free (odistance
->access_list
);
1225 ospf_distance_free (odistance
);
1227 route_unlock_node (rn
);
1232 ospf_distance_apply (struct prefix_ipv4
*p
, struct ospf_route
*or)
1236 ospf
= ospf_lookup ();
1240 if (ospf
->distance_intra
)
1241 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1242 return ospf
->distance_intra
;
1244 if (ospf
->distance_inter
)
1245 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1246 return ospf
->distance_inter
;
1248 if (ospf
->distance_external
)
1249 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
1250 || or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1251 return ospf
->distance_external
;
1253 if (ospf
->distance_all
)
1254 return ospf
->distance_all
;
1262 /* Allocate zebra structure. */
1263 zclient
= zclient_new ();
1264 zclient_init (zclient
, ZEBRA_ROUTE_OSPF
);
1265 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1266 zclient
->interface_add
= ospf_interface_add
;
1267 zclient
->interface_delete
= ospf_interface_delete
;
1268 zclient
->interface_up
= ospf_interface_state_up
;
1269 zclient
->interface_down
= ospf_interface_state_down
;
1270 zclient
->interface_address_add
= ospf_interface_address_add
;
1271 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1272 zclient
->ipv4_route_add
= ospf_zebra_read_ipv4
;
1273 zclient
->ipv4_route_delete
= ospf_zebra_read_ipv4
;
1275 access_list_add_hook (ospf_filter_update
);
1276 access_list_delete_hook (ospf_filter_update
);
1277 prefix_list_add_hook (ospf_prefix_list_update
);
1278 prefix_list_delete_hook (ospf_prefix_list_update
);