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.
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_abr.h"
43 #include "ospfd/ospf_lsa.h"
44 #include "ospfd/ospf_dump.h"
45 #include "ospfd/ospf_route.h"
46 #include "ospfd/ospf_zebra.h"
48 #include "ospfd/ospf_snmp.h"
49 #endif /* HAVE_SNMP */
51 /* Zebra structure to hold current status. */
52 struct zclient
*zclient
= NULL
;
54 /* For registering threads. */
55 extern struct thread_master
*master
;
57 /* Inteface addition message from zebra. */
59 ospf_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
61 struct interface
*ifp
;
63 ifp
= zebra_interface_add_read (zclient
->ibuf
);
65 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
66 zlog_info ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
67 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu
);
69 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
71 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
72 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_BROADCAST
;
74 if (if_is_broadcast (ifp
))
75 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_BROADCAST
;
76 else if (if_is_pointopoint (ifp
))
77 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_POINTOPOINT
;
78 else if (if_is_loopback (ifp
))
79 IF_DEF_PARAMS (ifp
)->type
= OSPF_IFTYPE_LOOPBACK
;
85 ospf_snmp_if_update (ifp
);
86 #endif /* HAVE_SNMP */
92 ospf_interface_delete (int command
, struct zclient
*zclient
,
95 struct interface
*ifp
;
97 struct route_node
*rn
;
100 /* zebra_interface_state_read() updates interface structure in iflist */
101 ifp
= zebra_interface_state_read (s
);
107 zlog_warn ("Zebra: got delete of %s, but interface is still up",
110 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
111 zlog_info ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
112 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu
);
115 ospf_snmp_if_delete (ifp
);
116 #endif /* HAVE_SNMP */
118 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
120 ospf_if_free ((struct ospf_interface
*) rn
->info
);
122 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
124 ospf_del_if_params (rn
->info
);
132 zebra_interface_if_lookup (struct stream
*s
)
134 struct interface
*ifp
;
135 u_char ifname_tmp
[INTERFACE_NAMSIZ
];
137 /* Read interface name. */
138 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
140 /* Lookup this by interface index. */
141 ifp
= if_lookup_by_name (ifname_tmp
);
143 /* If such interface does not exist, indicate an error */
151 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
153 /* Read interface's index. */
154 ifp
->ifindex
= stream_getl (s
);
156 /* Read interface's value. */
157 ifp
->flags
= stream_getl (s
);
158 ifp
->metric
= stream_getl (s
);
159 ifp
->mtu
= stream_getl (s
);
160 ifp
->bandwidth
= stream_getl (s
);
164 ospf_interface_state_up (int command
, struct zclient
*zclient
,
167 struct interface
*ifp
;
168 struct interface if_tmp
;
169 struct ospf_interface
*oi
;
170 struct route_node
*rn
;
172 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
177 /* Interface is already up. */
180 /* Temporarily keep ifp values. */
181 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
183 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
185 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
186 zlog_info ("Zebra: Interface[%s] state update.", ifp
->name
);
188 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
190 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
191 zlog_info ("Zebra: Interface[%s] bandwidth change %d -> %d.",
192 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
194 ospf_if_recalculate_output_cost (ifp
);
199 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
201 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
202 zlog_info ("Zebra: Interface[%s] state change to up.", ifp
->name
);
204 for (rn
= route_top (IF_OIFS (ifp
));rn
; rn
= route_next (rn
))
206 if ( (oi
= rn
->info
) == NULL
)
216 ospf_interface_state_down (int command
, struct zclient
*zclient
,
219 struct interface
*ifp
;
220 struct ospf_interface
*oi
;
221 struct route_node
*node
;
223 ifp
= zebra_interface_state_read (zclient
->ibuf
);
228 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
229 zlog_info ("Zebra: Interface[%s] state change to down.", ifp
->name
);
231 for (node
= route_top (IF_OIFS (ifp
));node
; node
= route_next (node
))
233 if ( (oi
= node
->info
) == NULL
)
242 ospf_interface_address_add (int command
, struct zclient
*zclient
,
247 c
= zebra_interface_address_add_read (zclient
->ibuf
);
253 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
258 if (p
->family
== AF_INET
)
259 zlog_info (" connected address %s/%d",
260 inet_atop (p
->u
.prefix4
), p
->prefixlen
);
267 ospf_snmp_if_update (c
->ifp
);
268 #endif /* HAVE_SNMP */
274 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_delete_read (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 */
314 ospf_zebra_add (struct prefix_ipv4
*p
, struct ospf_route
*or)
321 struct ospf_path
*path
;
324 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
329 /* OSPF pass nexthop and metric */
330 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
331 SET_FLAG (message
, ZAPI_MESSAGE_METRIC
);
333 /* Distance value. */
334 distance
= ospf_distance_apply (p
, or);
336 SET_FLAG (message
, ZAPI_MESSAGE_DISTANCE
);
342 /* Length place holder. */
345 /* Put command, type, flags, message. */
346 stream_putc (s
, ZEBRA_IPV4_ROUTE_ADD
);
347 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
348 stream_putc (s
, flags
);
349 stream_putc (s
, message
);
351 /* Put prefix information. */
352 psize
= PSIZE (p
->prefixlen
);
353 stream_putc (s
, p
->prefixlen
);
354 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
357 stream_putc (s
, or->path
->count
);
359 /* Nexthop, ifindex, distance and metric information. */
360 for (node
= listhead (or->path
); node
; nextnode (node
))
362 path
= getdata (node
);
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
);
379 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
380 stream_putc (s
, distance
);
381 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
383 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
384 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
385 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
386 stream_putl (s
, or->u
.ext
.type2_cost
);
388 stream_putl (s
, or->cost
);
391 stream_putw_at (s
, 0, stream_get_endp (s
));
393 writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
396 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
400 nexthop_str
= strdup (inet_ntoa (*nexthop
));
401 zlog_info ("Zebra: Route add %s/%d nexthop %s metric %d",
402 inet_ntoa (p
->prefix
), p
->prefixlen
, nexthop_str
,
411 ospf_zebra_delete (struct prefix_ipv4
*p
, struct ospf_route
*or)
413 struct zapi_ipv4 api
;
415 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
417 api
.type
= ZEBRA_ROUTE_OSPF
;
420 zapi_ipv4_delete (zclient
, p
, &api
);
423 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
427 nexthop_str
= strdup (inet_ntoa (*nexthop
));
428 zlog_info ("Zebra: Route delete %s/%d nexthop %s",
429 inet_ntoa (p
->prefix
), p
->prefixlen
, nexthop_str
);
437 ospf_zebra_add_discard (struct prefix_ipv4
*p
)
439 struct zapi_ipv4 api
;
441 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
443 api
.type
= ZEBRA_ROUTE_OSPF
;
444 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
446 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
450 zapi_ipv4_add (zclient
, p
, &api
);
455 ospf_zebra_delete_discard (struct prefix_ipv4
*p
)
457 struct zapi_ipv4 api
;
459 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
461 api
.type
= ZEBRA_ROUTE_OSPF
;
462 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
464 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
468 zapi_ipv4_delete (zclient
, p
, &api
);
473 ospf_is_type_redistributed (int type
)
475 return (DEFAULT_ROUTE_TYPE (type
)) ?
476 zclient
->default_information
: zclient
->redist
[type
];
480 ospf_redistribute_set (int type
, int mtype
, int mvalue
)
484 if (ospf_is_type_redistributed (type
))
486 if (mtype
!= ospf_top
->dmetric
[type
].type
)
488 ospf_top
->dmetric
[type
].type
= mtype
;
489 force
= LSA_REFRESH_FORCE
;
491 if (mvalue
!= ospf_top
->dmetric
[type
].value
)
493 ospf_top
->dmetric
[type
].value
= mvalue
;
494 force
= LSA_REFRESH_FORCE
;
497 ospf_external_lsa_refresh_type (type
, force
);
499 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
500 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
501 LOOKUP (ospf_redistributed_proto
, type
),
502 metric_type (type
), metric_value (type
));
507 ospf_top
->dmetric
[type
].type
= mtype
;
508 ospf_top
->dmetric
[type
].value
= mvalue
;
510 zclient_redistribute_set (zclient
, type
);
512 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
513 zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]",
514 LOOKUP (ospf_redistributed_proto
, type
),
515 metric_type (type
), metric_value (type
));
517 ospf_asbr_status_update (++ospf_top
->redistribute
);
523 ospf_redistribute_unset (int type
)
525 if (type
== zclient
->redist_default
)
528 if (! ospf_is_type_redistributed (type
))
531 zclient_redistribute_unset (zclient
, type
);
533 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
534 zlog_info ("Redistribute[%s]: Stop",
535 LOOKUP (ospf_redistributed_proto
, type
));
537 ospf_top
->dmetric
[type
].type
= -1;
538 ospf_top
->dmetric
[type
].value
= -1;
540 /* Remove the routes from OSPF table. */
541 ospf_redistribute_withdraw (type
);
543 ospf_asbr_status_update (--ospf_top
->redistribute
);
549 ospf_redistribute_default_set (int originate
, int mtype
, int mvalue
)
552 if (ospf_is_type_redistributed (DEFAULT_ROUTE
))
554 if (mtype
!= ospf_top
->dmetric
[DEFAULT_ROUTE
].type
)
556 ospf_top
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
559 if (mvalue
!= ospf_top
->dmetric
[DEFAULT_ROUTE
].value
)
562 ospf_top
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
565 ospf_external_lsa_refresh_default ();
567 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
568 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
569 LOOKUP (ospf_redistributed_proto
, DEFAULT_ROUTE
),
570 metric_type (DEFAULT_ROUTE
),
571 metric_value (DEFAULT_ROUTE
));
575 ospf_top
->default_originate
= originate
;
576 ospf_top
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
577 ospf_top
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
579 zclient_redistribute_default_set (zclient
);
581 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
582 zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
583 metric_type (DEFAULT_ROUTE
), metric_value (DEFAULT_ROUTE
));
586 if (ospf_top
->router_id
.s_addr
== 0)
587 ospf_top
->external_origin
|= (1 << DEFAULT_ROUTE
);
589 thread_add_timer (master
, ospf_default_originate_timer
,
590 &ospf_top
->default_originate
, 1);
592 ospf_asbr_status_update (++ospf_top
->redistribute
);
598 ospf_redistribute_default_unset ()
600 if (!ospf_is_type_redistributed (DEFAULT_ROUTE
))
603 ospf_top
->default_originate
= DEFAULT_ORIGINATE_NONE
;
604 ospf_top
->dmetric
[DEFAULT_ROUTE
].type
= -1;
605 ospf_top
->dmetric
[DEFAULT_ROUTE
].value
= -1;
607 zclient_redistribute_default_unset (zclient
);
609 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
610 zlog_info ("Redistribute[DEFAULT]: Stop");
612 ospf_asbr_status_update (--ospf_top
->redistribute
);
618 ospf_external_lsa_originate_check (struct external_info
*ei
)
620 /* If prefix is multicast, then do not originate LSA. */
621 if (IN_MULTICAST (htonl (ei
->p
.prefix
.s_addr
)))
623 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
624 "Prefix belongs multicast", inet_ntoa (ei
->p
.prefix
));
628 /* Take care of default-originate. */
629 if (is_prefix_default (&ei
->p
))
630 if (ospf_top
->default_originate
== DEFAULT_ORIGINATE_NONE
)
632 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
640 /* If connected prefix is OSPF enable interface, then do not announce. */
642 ospf_distribute_check_connected (struct external_info
*ei
)
644 struct route_node
*rn
;
646 for (rn
= route_top (ospf_top
->networks
); rn
; rn
= route_next (rn
))
647 if (rn
->info
!= NULL
)
648 if (prefix_match (&rn
->p
, (struct prefix
*)&ei
->p
))
654 /* return 1 if external LSA must be originated, 0 otherwise */
656 ospf_redistribute_check (struct external_info
*ei
, int *changed
)
658 struct route_map_set_values save_values
;
659 struct prefix_ipv4
*p
= &ei
->p
;
660 u_char type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
665 if (!ospf_external_lsa_originate_check (ei
))
668 /* Take care connected route. */
669 if (type
== ZEBRA_ROUTE_CONNECT
&& !ospf_distribute_check_connected (ei
))
672 if (!DEFAULT_ROUTE_TYPE (type
) && DISTRIBUTE_NAME (type
))
673 /* distirbute-list exists, but access-list may not? */
674 if (DISTRIBUTE_LIST (type
))
675 if (access_list_apply (DISTRIBUTE_LIST (type
), p
) == FILTER_DENY
)
677 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
678 zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
679 LOOKUP (ospf_redistributed_proto
, type
),
680 inet_ntoa (p
->prefix
), p
->prefixlen
);
684 save_values
= ei
->route_map_set
;
685 ospf_reset_route_map_set_values (&ei
->route_map_set
);
687 /* apply route-map if needed */
688 if (ROUTEMAP_NAME (type
))
692 ret
= route_map_apply (ROUTEMAP (type
), (struct prefix
*)p
,
695 if (ret
== RMAP_DENYMATCH
)
697 ei
->route_map_set
= save_values
;
698 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
699 zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.",
700 LOOKUP (ospf_redistributed_proto
, type
),
701 inet_ntoa (p
->prefix
), p
->prefixlen
);
705 /* check if 'route-map set' changed something */
707 *changed
= !ospf_route_map_set_compare (&ei
->route_map_set
,
714 /* OSPF route-map set for redistribution */
716 ospf_routemap_set (int type
, char *name
)
718 if (ROUTEMAP_NAME (type
))
719 free (ROUTEMAP_NAME (type
));
721 ROUTEMAP_NAME (type
) = strdup (name
);
722 ROUTEMAP (type
) = route_map_lookup_by_name (name
);
726 ospf_routemap_unset (int type
)
728 if (ROUTEMAP_NAME (type
))
729 free (ROUTEMAP_NAME (type
));
731 ROUTEMAP_NAME (type
) = NULL
;
732 ROUTEMAP (type
) = NULL
;
735 /* Zebra route add and delete treatment. */
737 ospf_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
741 struct zapi_ipv4 api
;
742 unsigned long ifindex
;
743 struct in_addr nexthop
;
744 struct prefix_ipv4 p
;
745 struct external_info
*ei
;
751 /* Type, flags, message. */
752 api
.type
= stream_getc (s
);
753 api
.flags
= stream_getc (s
);
754 api
.message
= stream_getc (s
);
757 memset (&p
, 0, sizeof (struct prefix_ipv4
));
759 p
.prefixlen
= stream_getc (s
);
760 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
762 /* Nexthop, ifindex, distance, metric. */
763 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
765 api
.nexthop_num
= stream_getc (s
);
766 nexthop
.s_addr
= stream_get_ipv4 (s
);
768 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
770 api
.ifindex_num
= stream_getc (s
);
771 ifindex
= stream_getl (s
);
773 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
774 api
.distance
= stream_getc (s
);
775 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
776 api
.metric
= stream_getl (s
);
778 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
780 ei
= ospf_external_info_add (api
.type
, p
, ifindex
, nexthop
);
782 if (ospf_top
->router_id
.s_addr
== 0)
783 /* Set flags to generate AS-external-LSA originate event
784 for each redistributed protocols later. */
785 ospf_top
->external_origin
|= (1 << api
.type
);
790 if (is_prefix_default (&p
))
791 ospf_external_lsa_refresh_default ();
794 struct ospf_lsa
*current
;
796 current
= ospf_external_info_find_lsa (&ei
->p
);
798 ospf_external_lsa_originate (ei
);
799 else if (IS_LSA_MAXAGE (current
))
800 ospf_external_lsa_refresh (current
, ei
, LSA_REFRESH_FORCE
);
802 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
803 inet_ntoa (p
.prefix
));
808 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
810 ospf_external_info_delete (api
.type
, p
);
811 if ( !is_prefix_default (&p
))
812 ospf_external_lsa_flush (api
.type
, &p
, ifindex
, nexthop
);
814 ospf_external_lsa_refresh_default ();
822 ospf_distribute_list_out_set (int type
, char *name
)
824 /* Lookup access-list for distribute-list. */
825 DISTRIBUTE_LIST (type
) = access_list_lookup (AFI_IP
, name
);
827 /* Clear previous distribute-name. */
828 if (DISTRIBUTE_NAME (type
))
829 free (DISTRIBUTE_NAME (type
));
831 /* Set distribute-name. */
832 DISTRIBUTE_NAME (type
) = strdup (name
);
834 /* If access-list have been set, schedule update timer. */
835 if (DISTRIBUTE_LIST (type
))
836 ospf_distribute_list_update (type
);
842 ospf_distribute_list_out_unset (int type
, char *name
)
844 /* Schedule update timer. */
845 if (DISTRIBUTE_LIST (type
))
846 ospf_distribute_list_update (type
);
848 /* Unset distribute-list. */
849 DISTRIBUTE_LIST (type
) = NULL
;
851 /* Clear distribute-name. */
852 if (DISTRIBUTE_NAME (type
))
853 free (DISTRIBUTE_NAME (type
));
855 DISTRIBUTE_NAME (type
) = NULL
;
860 /* distribute-list update timer. */
862 ospf_distribute_list_update_timer (struct thread
*thread
)
864 struct route_node
*rn
;
865 struct external_info
*ei
;
866 struct route_table
*rt
;
867 struct ospf_lsa
*lsa
;
870 type
= (int) THREAD_ARG (thread
);
871 rt
= EXTERNAL_INFO (type
);
873 ospf_top
->t_distribute_update
= NULL
;
875 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
877 /* foreach all external info. */
879 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
880 if ((ei
= rn
->info
) != NULL
)
882 if (is_prefix_default (&ei
->p
))
883 ospf_external_lsa_refresh_default ();
884 else if ((lsa
= ospf_external_info_find_lsa (&ei
->p
)))
885 ospf_external_lsa_refresh (lsa
, ei
, LSA_REFRESH_IF_CHANGED
);
887 ospf_external_lsa_originate (ei
);
892 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
894 /* Update distribute-list and set timer to apply access-list. */
896 ospf_distribute_list_update (int type
)
898 struct route_table
*rt
;
900 zlog_info ("ospf_distribute_list_update(): start");
902 /* External info does not exist. */
903 if (!(rt
= EXTERNAL_INFO (type
)))
906 /* If exists previously invoked thread, then cancel it. */
907 if (ospf_top
->t_distribute_update
)
908 OSPF_TIMER_OFF (ospf_top
->t_distribute_update
);
911 ospf_top
->t_distribute_update
=
912 thread_add_timer (master
, ospf_distribute_list_update_timer
,
913 (void *) type
, OSPF_DISTRIBUTE_UPDATE_DELAY
);
915 zlog_info ("ospf_distribute_list_update(): stop");
918 /* If access-list is updated, apply some check. */
920 ospf_filter_update (struct access_list
*access
)
924 struct ospf_area
*area
;
927 /* If OSPF instatnce does not exist, return right now. */
932 /* Update distribute-list, and apply filter. */
933 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
935 if (ROUTEMAP (type
) != NULL
)
937 /* if route-map is not NULL it may be using this access list */
938 ospf_distribute_list_update (type
);
943 if (DISTRIBUTE_NAME (type
))
945 /* Keep old access-list for distribute-list. */
946 struct access_list
*old
= DISTRIBUTE_LIST (type
);
948 /* Update access-list for distribute-list. */
949 DISTRIBUTE_LIST (type
) =
950 access_list_lookup (AFI_IP
, DISTRIBUTE_NAME (type
));
952 /* No update for this distribute type. */
953 if (old
== NULL
&& DISTRIBUTE_LIST (type
) == NULL
)
956 /* Schedule distribute-list update timer. */
957 if (DISTRIBUTE_LIST (type
) == NULL
||
958 strcmp (DISTRIBUTE_NAME (type
), access
->name
) == 0)
959 ospf_distribute_list_update (type
);
963 /* Update Area access-list. */
964 for (node
= listhead (ospf_top
->areas
); node
; nextnode (node
))
965 if ((area
= getdata (node
)) != NULL
)
967 if (EXPORT_NAME (area
))
969 EXPORT_LIST (area
) = NULL
;
973 if (IMPORT_NAME (area
))
975 IMPORT_LIST (area
) = NULL
;
980 /* Schedule ABR tasks -- this will be changed -- takada. */
981 if (OSPF_IS_ABR
&& abr_inv
)
982 ospf_schedule_abr_task ();
986 struct ospf_distance
*
989 struct ospf_distance
*new;
990 new = XMALLOC (MTYPE_OSPF_DISTANCE
, sizeof (struct ospf_distance
));
991 memset (new, 0, sizeof (struct ospf_distance
));
996 ospf_distance_free (struct ospf_distance
*odistance
)
998 XFREE (MTYPE_OSPF_DISTANCE
, odistance
);
1002 ospf_distance_set (struct vty
*vty
, char *distance_str
, char *ip_str
,
1003 char *access_list_str
)
1006 struct prefix_ipv4 p
;
1008 struct route_node
*rn
;
1009 struct ospf_distance
*odistance
;
1011 ret
= str2prefix_ipv4 (ip_str
, &p
);
1014 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1018 distance
= atoi (distance_str
);
1020 /* Get OSPF distance node. */
1021 rn
= route_node_get (ospf_top
->distance_table
, (struct prefix
*) &p
);
1024 odistance
= rn
->info
;
1025 route_unlock_node (rn
);
1029 odistance
= ospf_distance_new ();
1030 rn
->info
= odistance
;
1033 /* Set distance value. */
1034 odistance
->distance
= distance
;
1036 /* Reset access-list configuration. */
1037 if (odistance
->access_list
)
1039 free (odistance
->access_list
);
1040 odistance
->access_list
= NULL
;
1042 if (access_list_str
)
1043 odistance
->access_list
= strdup (access_list_str
);
1049 ospf_distance_unset (struct vty
*vty
, char *distance_str
, char *ip_str
,
1050 char *access_list_str
)
1053 struct prefix_ipv4 p
;
1055 struct route_node
*rn
;
1056 struct ospf_distance
*odistance
;
1058 ret
= str2prefix_ipv4 (ip_str
, &p
);
1061 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1065 distance
= atoi (distance_str
);
1067 rn
= route_node_lookup (ospf_top
->distance_table
, (struct prefix
*)&p
);
1070 vty_out (vty
, "Can't find specified prefix%s", VTY_NEWLINE
);
1074 odistance
= rn
->info
;
1076 if (odistance
->access_list
)
1077 free (odistance
->access_list
);
1078 ospf_distance_free (odistance
);
1081 route_unlock_node (rn
);
1082 route_unlock_node (rn
);
1088 ospf_distance_reset ()
1090 struct route_node
*rn
;
1091 struct ospf_distance
*odistance
;
1093 for (rn
= route_top (ospf_top
->distance_table
); rn
; rn
= route_next (rn
))
1094 if ((odistance
= rn
->info
) != NULL
)
1096 if (odistance
->access_list
)
1097 free (odistance
->access_list
);
1098 ospf_distance_free (odistance
);
1100 route_unlock_node (rn
);
1105 ospf_distance_apply (struct prefix_ipv4
*p
, struct ospf_route
*or)
1108 struct route_node
*rn
;
1109 struct ospf_distance
*odistance
;
1110 struct access_list
*alist
;
1111 struct prefix_ipv4 q
;
1113 memset (&q
, 0, sizeof (struct prefix_ipv4
));
1116 q
.prefixlen
= IPV4_MAX_BITLEN
;
1123 rn
= route_node_match (ospf_top
->distance_table
, (struct prefix
*) &q
);
1126 odistance
= rn
->info
;
1127 route_unlock_node (rn
);
1129 if (odistance
->access_list
)
1131 alist
= access_list_lookup (AFI_IP
, odistance
->access_list
);
1134 if (access_list_apply (alist
, (struct prefix
*) p
) == FILTER_DENY
)
1137 return odistance
->distance
;
1140 return odistance
->distance
;
1144 if (ospf_top
->distance_intra
)
1145 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1146 return ospf_top
->distance_intra
;
1148 if (ospf_top
->distance_inter
)
1149 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1150 return ospf_top
->distance_inter
;
1152 if (ospf_top
->distance_external
)
1153 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
1154 || or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1155 return ospf_top
->distance_external
;
1157 if (ospf_top
->distance_all
)
1158 return ospf_top
->distance_all
;
1166 /* Allocate zebra structure. */
1167 zclient
= zclient_new ();
1168 zclient_init (zclient
, ZEBRA_ROUTE_OSPF
);
1169 zclient
->interface_add
= ospf_interface_add
;
1170 zclient
->interface_delete
= ospf_interface_delete
;
1171 zclient
->interface_up
= ospf_interface_state_up
;
1172 zclient
->interface_down
= ospf_interface_state_down
;
1173 zclient
->interface_address_add
= ospf_interface_address_add
;
1174 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1175 zclient
->ipv4_route_add
= ospf_zebra_read_ipv4
;
1176 zclient
->ipv4_route_delete
= ospf_zebra_read_ipv4
;
1178 access_list_add_hook (ospf_filter_update
);
1179 access_list_delete_hook (ospf_filter_update
);