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 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
71 prefix2str(&router_id
, buf
, sizeof(buf
));
72 zlog_debug("Zebra rcvd: router id update %s", buf
);
75 router_id_zebra
= router_id
.u
.prefix4
;
77 ospf
= ospf_lookup ();
80 ospf_router_id_update (ospf
);
85 /* Inteface addition message from zebra. */
87 ospf_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
89 struct interface
*ifp
;
91 ifp
= zebra_interface_add_read (zclient
->ibuf
);
93 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
94 zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
95 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
96 ifp
->metric
, ifp
->mtu
);
100 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
102 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
103 IF_DEF_PARAMS (ifp
)->type
= ospf_default_iftype(ifp
);
106 ospf_if_update (NULL
, ifp
);
109 ospf_snmp_if_update (ifp
);
110 #endif /* HAVE_SNMP */
116 ospf_interface_delete (int command
, struct zclient
*zclient
,
119 struct interface
*ifp
;
121 struct route_node
*rn
;
124 /* zebra_interface_state_read() updates interface structure in iflist */
125 ifp
= zebra_interface_state_read (s
);
131 zlog_warn ("Zebra: got delete of %s, but interface is still up",
134 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
136 ("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
137 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
140 ospf_snmp_if_delete (ifp
);
141 #endif /* HAVE_SNMP */
143 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
145 ospf_if_free ((struct ospf_interface
*) rn
->info
);
147 ifp
->ifindex
= IFINDEX_INTERNAL
;
151 static struct interface
*
152 zebra_interface_if_lookup (struct stream
*s
)
154 char ifname_tmp
[INTERFACE_NAMSIZ
];
156 /* Read interface name. */
157 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
159 /* And look it up. */
160 return if_lookup_by_name_len(ifname_tmp
,
161 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
165 ospf_interface_state_up (int command
, struct zclient
*zclient
,
168 struct interface
*ifp
;
169 struct ospf_interface
*oi
;
170 struct route_node
*rn
;
172 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
177 /* Interface is already up. */
178 if (if_is_operative (ifp
))
180 /* Temporarily keep ifp values. */
181 struct interface if_tmp
;
182 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
184 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
186 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
187 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
189 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
191 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
192 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
193 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
195 ospf_if_recalculate_output_cost (ifp
);
198 if (if_tmp
.mtu
!= ifp
->mtu
)
200 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
201 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
202 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
204 /* Must reset the interface (simulate down/up) when MTU changes. */
210 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
212 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
213 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp
->name
);
215 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
217 if ((oi
= rn
->info
) == NULL
)
227 ospf_interface_state_down (int command
, struct zclient
*zclient
,
230 struct interface
*ifp
;
231 struct ospf_interface
*oi
;
232 struct route_node
*node
;
234 ifp
= zebra_interface_state_read (zclient
->ibuf
);
239 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
240 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
242 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
244 if ((oi
= node
->info
) == NULL
)
253 ospf_interface_address_add (int command
, struct zclient
*zclient
,
258 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
263 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
266 prefix2str(c
->address
, buf
, sizeof(buf
));
267 zlog_debug("Zebra: interface %s address add %s", c
->ifp
->name
, buf
);
270 ospf_if_update (NULL
, c
->ifp
);
273 ospf_snmp_if_update (c
->ifp
);
274 #endif /* HAVE_SNMP */
280 ospf_interface_address_delete (int command
, struct zclient
*zclient
,
284 struct interface
*ifp
;
285 struct ospf_interface
*oi
;
286 struct route_node
*rn
;
289 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
294 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
297 prefix2str(c
->address
, buf
, sizeof(buf
));
298 zlog_debug("Zebra: interface %s address delete %s", c
->ifp
->name
, buf
);
303 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
305 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
315 /* Call interface hook functions to clean up */
319 ospf_snmp_if_update (c
->ifp
);
320 #endif /* HAVE_SNMP */
328 ospf_zebra_add (struct prefix_ipv4
*p
, struct ospf_route
*or)
335 struct ospf_path
*path
;
336 struct listnode
*node
;
338 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
343 /* OSPF pass nexthop and metric */
344 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
345 SET_FLAG (message
, ZAPI_MESSAGE_METRIC
);
347 /* Distance value. */
348 distance
= ospf_distance_apply (p
, or);
350 SET_FLAG (message
, ZAPI_MESSAGE_DISTANCE
);
356 /* Put command, type, flags, message. */
357 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_ADD
);
358 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
359 stream_putc (s
, flags
);
360 stream_putc (s
, message
);
361 stream_putw (s
, SAFI_UNICAST
);
363 /* Put prefix information. */
364 psize
= PSIZE (p
->prefixlen
);
365 stream_putc (s
, p
->prefixlen
);
366 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
369 stream_putc (s
, or->paths
->count
);
371 /* Nexthop, ifindex, distance and metric information. */
372 for (ALL_LIST_ELEMENTS_RO (or->paths
, node
, path
))
374 if (path
->nexthop
.s_addr
!= INADDR_ANY
&&
377 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_IFINDEX
);
378 stream_put_in_addr (s
, &path
->nexthop
);
379 stream_putl (s
, path
->ifindex
);
381 else if (path
->nexthop
.s_addr
!= INADDR_ANY
)
383 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
384 stream_put_in_addr (s
, &path
->nexthop
);
388 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
390 stream_putl (s
, path
->ifindex
);
395 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
397 char buf
[2][INET_ADDRSTRLEN
];
398 zlog_debug("Zebra: Route add %s/%d nexthop %s",
399 inet_ntop(AF_INET
, &p
->prefix
,
400 buf
[0], sizeof(buf
[0])),
402 inet_ntop(AF_INET
, &path
->nexthop
,
403 buf
[1], sizeof(buf
[1])));
407 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
408 stream_putc (s
, distance
);
409 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
411 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
412 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
413 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
414 stream_putl (s
, or->u
.ext
.type2_cost
);
416 stream_putl (s
, or->cost
);
419 stream_putw_at (s
, 0, stream_get_endp (s
));
421 zclient_send_message(zclient
);
426 ospf_zebra_delete (struct prefix_ipv4
*p
, struct ospf_route
*or)
433 struct ospf_path
*path
;
434 struct listnode
*node
;
436 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
440 /* Distance value. */
441 distance
= ospf_distance_apply (p
, or);
446 /* Put command, type, flags, message. */
447 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_DELETE
);
448 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
449 stream_putc (s
, flags
);
450 stream_putc (s
, message
);
451 stream_putw (s
, SAFI_UNICAST
);
453 /* Put prefix information. */
454 psize
= PSIZE (p
->prefixlen
);
455 stream_putc (s
, p
->prefixlen
);
456 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
459 stream_putc (s
, or->paths
->count
);
461 /* Nexthop, ifindex, distance and metric information. */
462 for (ALL_LIST_ELEMENTS_RO (or->paths
, node
, path
))
464 if (path
->nexthop
.s_addr
!= INADDR_ANY
&&
467 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_IFINDEX
);
468 stream_put_in_addr (s
, &path
->nexthop
);
469 stream_putl (s
, path
->ifindex
);
471 else if (path
->nexthop
.s_addr
!= INADDR_ANY
)
473 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
474 stream_put_in_addr (s
, &path
->nexthop
);
478 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
479 stream_putl (s
, path
->ifindex
);
482 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
484 char buf
[2][INET_ADDRSTRLEN
];
485 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
486 inet_ntop(AF_INET
, &p
->prefix
,
487 buf
[0], sizeof(buf
[0])),
489 inet_ntop(AF_INET
, &path
->nexthop
,
490 buf
[1], sizeof(buf
[1])));
494 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
495 stream_putc (s
, distance
);
496 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
498 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
499 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
500 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
501 stream_putl (s
, or->u
.ext
.type2_cost
);
503 stream_putl (s
, or->cost
);
506 stream_putw_at (s
, 0, stream_get_endp (s
));
508 zclient_send_message(zclient
);
513 ospf_zebra_add_discard (struct prefix_ipv4
*p
)
515 struct zapi_ipv4 api
;
517 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
519 api
.type
= ZEBRA_ROUTE_OSPF
;
520 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
522 api
.safi
= SAFI_UNICAST
;
523 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
527 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD
, zclient
, p
, &api
);
529 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
530 zlog_debug ("Zebra: Route add discard %s/%d",
531 inet_ntoa (p
->prefix
), p
->prefixlen
);
536 ospf_zebra_delete_discard (struct prefix_ipv4
*p
)
538 struct zapi_ipv4 api
;
540 if (zclient
->redist
[ZEBRA_ROUTE_OSPF
])
542 api
.type
= ZEBRA_ROUTE_OSPF
;
543 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
545 api
.safi
= SAFI_UNICAST
;
546 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
550 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
552 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
553 zlog_debug ("Zebra: Route delete discard %s/%d",
554 inet_ntoa (p
->prefix
), p
->prefixlen
);
560 ospf_is_type_redistributed (int type
)
562 return (DEFAULT_ROUTE_TYPE (type
)) ?
563 zclient
->default_information
: zclient
->redist
[type
];
567 ospf_redistribute_set (struct ospf
*ospf
, int type
, int mtype
, int mvalue
)
571 if (ospf_is_type_redistributed (type
))
573 if (mtype
!= ospf
->dmetric
[type
].type
)
575 ospf
->dmetric
[type
].type
= mtype
;
576 force
= LSA_REFRESH_FORCE
;
578 if (mvalue
!= ospf
->dmetric
[type
].value
)
580 ospf
->dmetric
[type
].value
= mvalue
;
581 force
= LSA_REFRESH_FORCE
;
584 ospf_external_lsa_refresh_type (ospf
, type
, force
);
586 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
587 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
588 ospf_redist_string(type
),
589 metric_type (ospf
, type
), metric_value (ospf
, type
));
594 ospf
->dmetric
[type
].type
= mtype
;
595 ospf
->dmetric
[type
].value
= mvalue
;
597 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
599 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
600 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
601 ospf_redist_string(type
),
602 metric_type (ospf
, type
), metric_value (ospf
, type
));
604 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
610 ospf_redistribute_unset (struct ospf
*ospf
, int type
)
612 if (type
== zclient
->redist_default
)
615 if (!ospf_is_type_redistributed (type
))
618 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
620 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
621 zlog_debug ("Redistribute[%s]: Stop",
622 ospf_redist_string(type
));
624 ospf
->dmetric
[type
].type
= -1;
625 ospf
->dmetric
[type
].value
= -1;
627 /* Remove the routes from OSPF table. */
628 ospf_redistribute_withdraw (ospf
, type
);
630 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
636 ospf_redistribute_default_set (struct ospf
*ospf
, int originate
,
637 int mtype
, int mvalue
)
639 ospf
->default_originate
= originate
;
640 ospf
->dmetric
[DEFAULT_ROUTE
].type
= mtype
;
641 ospf
->dmetric
[DEFAULT_ROUTE
].value
= mvalue
;
643 if (ospf_is_type_redistributed (DEFAULT_ROUTE
))
645 /* if ospf->default_originate changes value, is calling
646 ospf_external_lsa_refresh_default sufficient to implement
648 ospf_external_lsa_refresh_default (ospf
);
650 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
651 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
652 ospf_redist_string(DEFAULT_ROUTE
),
653 metric_type (ospf
, DEFAULT_ROUTE
),
654 metric_value (ospf
, DEFAULT_ROUTE
));
658 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
);
660 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
661 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
662 metric_type (ospf
, DEFAULT_ROUTE
),
663 metric_value (ospf
, DEFAULT_ROUTE
));
665 if (ospf
->router_id
.s_addr
== 0)
666 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
668 thread_add_timer (master
, ospf_default_originate_timer
, ospf
, 1);
670 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
676 ospf_redistribute_default_unset (struct ospf
*ospf
)
678 if (!ospf_is_type_redistributed (DEFAULT_ROUTE
))
681 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
682 ospf
->dmetric
[DEFAULT_ROUTE
].type
= -1;
683 ospf
->dmetric
[DEFAULT_ROUTE
].value
= -1;
685 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
);
687 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
688 zlog_debug ("Redistribute[DEFAULT]: Stop");
690 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
696 ospf_external_lsa_originate_check (struct ospf
*ospf
,
697 struct external_info
*ei
)
699 /* If prefix is multicast, then do not originate LSA. */
700 if (IN_MULTICAST (htonl (ei
->p
.prefix
.s_addr
)))
702 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
703 "Prefix belongs multicast", inet_ntoa (ei
->p
.prefix
));
707 /* Take care of default-originate. */
708 if (is_prefix_default (&ei
->p
))
709 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
)
711 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
719 /* If connected prefix is OSPF enable interface, then do not announce. */
721 ospf_distribute_check_connected (struct ospf
*ospf
, struct external_info
*ei
)
723 struct listnode
*node
;
724 struct ospf_interface
*oi
;
727 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
728 if (prefix_match (oi
->address
, (struct prefix
*) &ei
->p
))
733 /* return 1 if external LSA must be originated, 0 otherwise */
735 ospf_redistribute_check (struct ospf
*ospf
,
736 struct external_info
*ei
, int *changed
)
738 struct route_map_set_values save_values
;
739 struct prefix_ipv4
*p
= &ei
->p
;
740 u_char type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
745 if (!ospf_external_lsa_originate_check (ospf
, ei
))
748 /* Take care connected route. */
749 if (type
== ZEBRA_ROUTE_CONNECT
&&
750 !ospf_distribute_check_connected (ospf
, ei
))
753 if (!DEFAULT_ROUTE_TYPE (type
) && DISTRIBUTE_NAME (ospf
, type
))
754 /* distirbute-list exists, but access-list may not? */
755 if (DISTRIBUTE_LIST (ospf
, type
))
756 if (access_list_apply (DISTRIBUTE_LIST (ospf
, type
), p
) == FILTER_DENY
)
758 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
759 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
760 ospf_redist_string(type
),
761 inet_ntoa (p
->prefix
), p
->prefixlen
);
765 save_values
= ei
->route_map_set
;
766 ospf_reset_route_map_set_values (&ei
->route_map_set
);
768 /* apply route-map if needed */
769 if (ROUTEMAP_NAME (ospf
, type
))
773 ret
= route_map_apply (ROUTEMAP (ospf
, type
), (struct prefix
*) p
,
776 if (ret
== RMAP_DENYMATCH
)
778 ei
->route_map_set
= save_values
;
779 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
780 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
781 ospf_redist_string(type
),
782 inet_ntoa (p
->prefix
), p
->prefixlen
);
786 /* check if 'route-map set' changed something */
788 *changed
= !ospf_route_map_set_compare (&ei
->route_map_set
,
795 /* OSPF route-map set for redistribution */
797 ospf_routemap_set (struct ospf
*ospf
, int type
, const char *name
)
799 if (ROUTEMAP_NAME (ospf
, type
))
800 free (ROUTEMAP_NAME (ospf
, type
));
802 ROUTEMAP_NAME (ospf
, type
) = strdup (name
);
803 ROUTEMAP (ospf
, type
) = route_map_lookup_by_name (name
);
807 ospf_routemap_unset (struct ospf
*ospf
, int type
)
809 if (ROUTEMAP_NAME (ospf
, type
))
810 free (ROUTEMAP_NAME (ospf
, type
));
812 ROUTEMAP_NAME (ospf
, type
) = NULL
;
813 ROUTEMAP (ospf
, type
) = NULL
;
816 /* Zebra route add and delete treatment. */
818 ospf_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
822 struct zapi_ipv4 api
;
823 unsigned long ifindex
;
824 struct in_addr nexthop
;
825 struct prefix_ipv4 p
;
826 struct external_info
*ei
;
833 /* Type, flags, message. */
834 api
.type
= stream_getc (s
);
835 api
.flags
= stream_getc (s
);
836 api
.message
= stream_getc (s
);
839 memset (&p
, 0, sizeof (struct prefix_ipv4
));
841 p
.prefixlen
= stream_getc (s
);
842 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
844 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
847 /* Nexthop, ifindex, distance, metric. */
848 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
850 api
.nexthop_num
= stream_getc (s
);
851 nexthop
.s_addr
= stream_get_ipv4 (s
);
853 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
855 api
.ifindex_num
= stream_getc (s
);
856 /* XXX assert(api.ifindex_num == 1); */
857 ifindex
= stream_getl (s
);
859 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
860 api
.distance
= stream_getc (s
);
861 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
862 api
.metric
= stream_getl (s
);
864 ospf
= ospf_lookup ();
868 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
870 /* XXX|HACK|TODO|FIXME:
871 * Maybe we should ignore reject/blackhole routes? Testing shows that
872 * there is no problems though and this is only way to "summarize"
873 * routes in ASBR at the moment. Maybe we need just a better generalised
874 * solution for these types?
876 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
877 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
881 ei
= ospf_external_info_add (api
.type
, p
, ifindex
, nexthop
);
883 if (ospf
->router_id
.s_addr
== 0)
884 /* Set flags to generate AS-external-LSA originate event
885 for each redistributed protocols later. */
886 ospf
->external_origin
|= (1 << api
.type
);
891 if (is_prefix_default (&p
))
892 ospf_external_lsa_refresh_default (ospf
);
895 struct ospf_lsa
*current
;
897 current
= ospf_external_info_find_lsa (ospf
, &ei
->p
);
899 ospf_external_lsa_originate (ospf
, ei
);
900 else if (IS_LSA_MAXAGE (current
))
901 ospf_external_lsa_refresh (ospf
, current
,
902 ei
, LSA_REFRESH_FORCE
);
904 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
905 inet_ntoa (p
.prefix
));
910 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
912 ospf_external_info_delete (api
.type
, p
);
913 if (is_prefix_default (&p
))
914 ospf_external_lsa_refresh_default (ospf
);
916 ospf_external_lsa_flush (ospf
, api
.type
, &p
, ifindex
/*, nexthop */);
924 ospf_distribute_list_out_set (struct ospf
*ospf
, int type
, const char *name
)
926 /* Lookup access-list for distribute-list. */
927 DISTRIBUTE_LIST (ospf
, type
) = access_list_lookup (AFI_IP
, name
);
929 /* Clear previous distribute-name. */
930 if (DISTRIBUTE_NAME (ospf
, type
))
931 free (DISTRIBUTE_NAME (ospf
, type
));
933 /* Set distribute-name. */
934 DISTRIBUTE_NAME (ospf
, type
) = strdup (name
);
936 /* If access-list have been set, schedule update timer. */
937 if (DISTRIBUTE_LIST (ospf
, type
))
938 ospf_distribute_list_update (ospf
, type
);
944 ospf_distribute_list_out_unset (struct ospf
*ospf
, int type
, const char *name
)
946 /* Schedule update timer. */
947 if (DISTRIBUTE_LIST (ospf
, type
))
948 ospf_distribute_list_update (ospf
, type
);
950 /* Unset distribute-list. */
951 DISTRIBUTE_LIST (ospf
, type
) = NULL
;
953 /* Clear distribute-name. */
954 if (DISTRIBUTE_NAME (ospf
, type
))
955 free (DISTRIBUTE_NAME (ospf
, type
));
957 DISTRIBUTE_NAME (ospf
, type
) = NULL
;
962 /* distribute-list update timer. */
964 ospf_distribute_list_update_timer (struct thread
*thread
)
966 struct route_node
*rn
;
967 struct external_info
*ei
;
968 struct route_table
*rt
;
969 struct ospf_lsa
*lsa
;
970 int type
, default_refresh
= 0;
973 ospf
= ospf_lookup ();
977 ospf
->t_distribute_update
= NULL
;
979 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
981 /* foreach all external info. */
982 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
984 rt
= EXTERNAL_INFO (type
);
987 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
988 if ((ei
= rn
->info
) != NULL
)
990 if (is_prefix_default (&ei
->p
))
992 else if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
993 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_IF_CHANGED
);
995 ospf_external_lsa_originate (ospf
, ei
);
999 ospf_external_lsa_refresh_default (ospf
);
1003 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
1005 /* Update distribute-list and set timer to apply access-list. */
1007 ospf_distribute_list_update (struct ospf
*ospf
, uintptr_t type
)
1009 struct route_table
*rt
;
1011 /* External info does not exist. */
1012 if (!(rt
= EXTERNAL_INFO (type
)))
1015 /* If exists previously invoked thread, then let it continue. */
1016 if (ospf
->t_distribute_update
)
1020 ospf
->t_distribute_update
=
1021 thread_add_timer (master
, ospf_distribute_list_update_timer
,
1022 (void *) type
, OSPF_DISTRIBUTE_UPDATE_DELAY
);
1025 /* If access-list is updated, apply some check. */
1027 ospf_filter_update (struct access_list
*access
)
1032 struct ospf_area
*area
;
1033 struct listnode
*node
;
1035 /* If OSPF instatnce does not exist, return right now. */
1036 ospf
= ospf_lookup ();
1040 /* Update distribute-list, and apply filter. */
1041 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
1043 if (ROUTEMAP (ospf
, type
) != NULL
)
1045 /* if route-map is not NULL it may be using this access list */
1046 ospf_distribute_list_update (ospf
, type
);
1050 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1051 * but no distribute list. */
1052 if (type
== ZEBRA_ROUTE_MAX
)
1055 if (DISTRIBUTE_NAME (ospf
, type
))
1057 /* Keep old access-list for distribute-list. */
1058 struct access_list
*old
= DISTRIBUTE_LIST (ospf
, type
);
1060 /* Update access-list for distribute-list. */
1061 DISTRIBUTE_LIST (ospf
, type
) =
1062 access_list_lookup (AFI_IP
, DISTRIBUTE_NAME (ospf
, type
));
1064 /* No update for this distribute type. */
1065 if (old
== NULL
&& DISTRIBUTE_LIST (ospf
, type
) == NULL
)
1068 /* Schedule distribute-list update timer. */
1069 if (DISTRIBUTE_LIST (ospf
, type
) == NULL
||
1070 strcmp (DISTRIBUTE_NAME (ospf
, type
), access
->name
) == 0)
1071 ospf_distribute_list_update (ospf
, type
);
1075 /* Update Area access-list. */
1076 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1078 if (EXPORT_NAME (area
))
1080 EXPORT_LIST (area
) = NULL
;
1084 if (IMPORT_NAME (area
))
1086 IMPORT_LIST (area
) = NULL
;
1091 /* Schedule ABR tasks -- this will be changed -- takada. */
1092 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1093 ospf_schedule_abr_task (ospf
);
1096 /* If prefix-list is updated, do some updates. */
1098 ospf_prefix_list_update (struct prefix_list
*plist
)
1103 struct ospf_area
*area
;
1104 struct listnode
*node
;
1106 /* If OSPF instatnce does not exist, return right now. */
1107 ospf
= ospf_lookup ();
1111 /* Update all route-maps which are used as redistribution filters.
1112 * They might use prefix-list.
1114 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
1116 if (ROUTEMAP (ospf
, type
) != NULL
)
1118 /* If route-map is not NULL it may be using this prefix list */
1119 ospf_distribute_list_update (ospf
, type
);
1124 /* Update area filter-lists. */
1125 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1127 /* Update filter-list in. */
1128 if (PREFIX_NAME_IN (area
))
1129 if (strcmp (PREFIX_NAME_IN (area
), plist
->name
) == 0)
1131 PREFIX_LIST_IN (area
) =
1132 prefix_list_lookup (AFI_IP
, PREFIX_NAME_IN (area
));
1136 /* Update filter-list out. */
1137 if (PREFIX_NAME_OUT (area
))
1138 if (strcmp (PREFIX_NAME_OUT (area
), plist
->name
) == 0)
1140 PREFIX_LIST_IN (area
) =
1141 prefix_list_lookup (AFI_IP
, PREFIX_NAME_OUT (area
));
1146 /* Schedule ABR task. */
1147 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1148 ospf_schedule_abr_task (ospf
);
1151 static struct ospf_distance
*
1152 ospf_distance_new (void)
1154 return XCALLOC (MTYPE_OSPF_DISTANCE
, sizeof (struct ospf_distance
));
1158 ospf_distance_free (struct ospf_distance
*odistance
)
1160 XFREE (MTYPE_OSPF_DISTANCE
, odistance
);
1164 ospf_distance_set (struct vty
*vty
, struct ospf
*ospf
,
1165 const char *distance_str
,
1167 const char *access_list_str
)
1170 struct prefix_ipv4 p
;
1172 struct route_node
*rn
;
1173 struct ospf_distance
*odistance
;
1175 ret
= str2prefix_ipv4 (ip_str
, &p
);
1178 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1182 distance
= atoi (distance_str
);
1184 /* Get OSPF distance node. */
1185 rn
= route_node_get (ospf
->distance_table
, (struct prefix
*) &p
);
1188 odistance
= rn
->info
;
1189 route_unlock_node (rn
);
1193 odistance
= ospf_distance_new ();
1194 rn
->info
= odistance
;
1197 /* Set distance value. */
1198 odistance
->distance
= distance
;
1200 /* Reset access-list configuration. */
1201 if (odistance
->access_list
)
1203 free (odistance
->access_list
);
1204 odistance
->access_list
= NULL
;
1206 if (access_list_str
)
1207 odistance
->access_list
= strdup (access_list_str
);
1213 ospf_distance_unset (struct vty
*vty
, struct ospf
*ospf
,
1214 const char *distance_str
,
1215 const char *ip_str
, char
1216 const *access_list_str
)
1219 struct prefix_ipv4 p
;
1220 struct route_node
*rn
;
1221 struct ospf_distance
*odistance
;
1223 ret
= str2prefix_ipv4 (ip_str
, &p
);
1226 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1230 rn
= route_node_lookup (ospf
->distance_table
, (struct prefix
*) &p
);
1233 vty_out (vty
, "Can't find specified prefix%s", VTY_NEWLINE
);
1237 odistance
= rn
->info
;
1239 if (odistance
->access_list
)
1240 free (odistance
->access_list
);
1241 ospf_distance_free (odistance
);
1244 route_unlock_node (rn
);
1245 route_unlock_node (rn
);
1251 ospf_distance_reset (struct ospf
*ospf
)
1253 struct route_node
*rn
;
1254 struct ospf_distance
*odistance
;
1256 for (rn
= route_top (ospf
->distance_table
); rn
; rn
= route_next (rn
))
1257 if ((odistance
= rn
->info
) != NULL
)
1259 if (odistance
->access_list
)
1260 free (odistance
->access_list
);
1261 ospf_distance_free (odistance
);
1263 route_unlock_node (rn
);
1268 ospf_distance_apply (struct prefix_ipv4
*p
, struct ospf_route
*or)
1272 ospf
= ospf_lookup ();
1276 if (ospf
->distance_intra
)
1277 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1278 return ospf
->distance_intra
;
1280 if (ospf
->distance_inter
)
1281 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1282 return ospf
->distance_inter
;
1284 if (ospf
->distance_external
)
1285 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
1286 || or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1287 return ospf
->distance_external
;
1289 if (ospf
->distance_all
)
1290 return ospf
->distance_all
;
1298 /* Allocate zebra structure. */
1299 zclient
= zclient_new ();
1300 zclient_init (zclient
, ZEBRA_ROUTE_OSPF
);
1301 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1302 zclient
->interface_add
= ospf_interface_add
;
1303 zclient
->interface_delete
= ospf_interface_delete
;
1304 zclient
->interface_up
= ospf_interface_state_up
;
1305 zclient
->interface_down
= ospf_interface_state_down
;
1306 zclient
->interface_address_add
= ospf_interface_address_add
;
1307 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1308 zclient
->ipv4_route_add
= ospf_zebra_read_ipv4
;
1309 zclient
->ipv4_route_delete
= ospf_zebra_read_ipv4
;
1311 access_list_add_hook (ospf_filter_update
);
1312 access_list_delete_hook (ospf_filter_update
);
1313 prefix_list_add_hook (ospf_prefix_list_update
);
1314 prefix_list_delete_hook (ospf_prefix_list_update
);