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_lsdb.h"
48 #include "ospfd/ospf_neighbor.h"
49 #include "ospfd/ospf_nsm.h"
50 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_snmp.h"
53 #endif /* HAVE_SNMP */
55 /* Zebra structure to hold current status. */
56 struct zclient
*zclient
= NULL
;
58 /* For registering threads. */
59 extern struct thread_master
*master
;
60 struct in_addr router_id_zebra
;
62 /* Router-id update message from zebra. */
64 ospf_router_id_update_zebra (int command
, struct zclient
*zclient
,
68 struct prefix router_id
;
69 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
71 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
74 prefix2str(&router_id
, buf
, sizeof(buf
));
75 zlog_debug("Zebra rcvd: router id update %s", buf
);
78 router_id_zebra
= router_id
.u
.prefix4
;
80 ospf
= ospf_lookup ();
83 ospf_router_id_update (ospf
);
88 /* Inteface addition message from zebra. */
90 ospf_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
92 struct interface
*ifp
;
94 ifp
= zebra_interface_add_read (zclient
->ibuf
);
96 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
97 zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
98 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
99 ifp
->metric
, ifp
->mtu
);
103 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
105 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
106 IF_DEF_PARAMS (ifp
)->type
= ospf_default_iftype(ifp
);
109 ospf_if_update (NULL
, ifp
);
112 ospf_snmp_if_update (ifp
);
113 #endif /* HAVE_SNMP */
119 ospf_interface_delete (int command
, struct zclient
*zclient
,
122 struct interface
*ifp
;
124 struct route_node
*rn
;
127 /* zebra_interface_state_read() updates interface structure in iflist */
128 ifp
= zebra_interface_state_read (s
);
134 zlog_warn ("Zebra: got delete of %s, but interface is still up",
137 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
139 ("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
140 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
143 ospf_snmp_if_delete (ifp
);
144 #endif /* HAVE_SNMP */
146 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
148 ospf_if_free ((struct ospf_interface
*) rn
->info
);
150 ifp
->ifindex
= IFINDEX_INTERNAL
;
154 static struct interface
*
155 zebra_interface_if_lookup (struct stream
*s
)
157 char ifname_tmp
[INTERFACE_NAMSIZ
];
159 /* Read interface name. */
160 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
162 /* And look it up. */
163 return if_lookup_by_name_len(ifname_tmp
,
164 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
168 ospf_interface_state_up (int command
, struct zclient
*zclient
,
171 struct interface
*ifp
;
172 struct ospf_interface
*oi
;
173 struct route_node
*rn
;
175 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
180 /* Interface is already up. */
181 if (if_is_operative (ifp
))
183 /* Temporarily keep ifp values. */
184 struct interface if_tmp
;
185 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
187 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
189 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
190 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
192 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
194 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
195 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
196 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
198 ospf_if_recalculate_output_cost (ifp
);
201 if (if_tmp
.mtu
!= ifp
->mtu
)
203 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
204 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
205 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
207 /* Must reset the interface (simulate down/up) when MTU changes. */
213 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
215 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
216 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp
->name
);
218 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
220 if ((oi
= rn
->info
) == NULL
)
230 ospf_interface_state_down (int command
, struct zclient
*zclient
,
233 struct interface
*ifp
;
234 struct ospf_interface
*oi
;
235 struct route_node
*node
;
237 ifp
= zebra_interface_state_read (zclient
->ibuf
);
242 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
243 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
245 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
247 if ((oi
= node
->info
) == NULL
)
256 ospf_interface_address_add (int command
, struct zclient
*zclient
,
261 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
266 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
269 prefix2str(c
->address
, buf
, sizeof(buf
));
270 zlog_debug("Zebra: interface %s address add %s", c
->ifp
->name
, buf
);
273 ospf_if_update (NULL
, c
->ifp
);
276 ospf_snmp_if_update (c
->ifp
);
277 #endif /* HAVE_SNMP */
283 ospf_interface_address_delete (int command
, struct zclient
*zclient
,
287 struct interface
*ifp
;
288 struct ospf_interface
*oi
;
289 struct route_node
*rn
;
292 c
= zebra_interface_address_read (command
, zclient
->ibuf
);
297 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
300 prefix2str(c
->address
, buf
, sizeof(buf
));
301 zlog_debug("Zebra: interface %s address delete %s", c
->ifp
->name
, buf
);
306 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
308 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
318 /* Call interface hook functions to clean up */
322 ospf_snmp_if_update (c
->ifp
);
323 #endif /* HAVE_SNMP */
331 ospf_interface_bfd_dest_down (int command
, struct zclient
*zclient
,
334 struct interface
*ifp
;
335 struct ospf_interface
*oi
;
336 struct ospf_if_params
*params
;
337 struct ospf_neighbor
*nbr
;
338 struct route_node
*node
;
341 ifp
= zebra_interface_bfd_read (zclient
->ibuf
, &p
, NULL
);
346 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
349 prefix2str(&p
, buf
, sizeof(buf
));
350 zlog_debug("Zebra: interface %s bfd destination %s down", ifp
->name
, buf
);
353 params
= IF_DEF_PARAMS (ifp
);
354 if (!OSPF_IF_PARAM_CONFIGURED (params
, bfd
))
357 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
359 if ((oi
= node
->info
) == NULL
)
362 nbr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &p
.u
.prefix4
);
366 if (IS_DEBUG_OSPF (nsm
, NSM_EVENTS
))
367 zlog_debug ("NSM[%s:%s]: BFD Down",
368 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->address
.u
.prefix4
));
370 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_InactivityTimer
);
377 ospf_zebra_add (struct prefix_ipv4
*p
, struct ospf_route
*or)
384 struct ospf_path
*path
;
385 struct listnode
*node
;
386 struct ospf
*ospf
= ospf_lookup ();
388 if (redist_check_instance(&zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_OSPF
], ospf
->instance
))
393 /* OSPF pass nexthop and metric */
394 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
395 SET_FLAG (message
, ZAPI_MESSAGE_METRIC
);
397 /* Distance value. */
398 distance
= ospf_distance_apply (p
, or);
400 SET_FLAG (message
, ZAPI_MESSAGE_DISTANCE
);
402 /* Check if path type is ASE and use only 16bit tags */
403 if (((or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
) ||
404 (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)) &&
405 (or->u
.ext
.tag
> 0) && (or->u
.ext
.tag
< UINT16_MAX
))
406 SET_FLAG (message
, ZAPI_MESSAGE_TAG
);
412 /* Put command, type, flags, message. */
413 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_ADD
);
414 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
415 stream_putw (s
, ospf
->instance
);
416 stream_putc (s
, flags
);
417 stream_putc (s
, message
);
418 stream_putw (s
, SAFI_UNICAST
);
420 /* Put prefix information. */
421 psize
= PSIZE (p
->prefixlen
);
422 stream_putc (s
, p
->prefixlen
);
423 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
426 stream_putc (s
, or->paths
->count
);
428 /* Nexthop, ifindex, distance and metric information. */
429 for (ALL_LIST_ELEMENTS_RO (or->paths
, node
, path
))
432 if (path
->unnumbered
)
434 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_ONLINK
);
435 stream_put_in_addr (s
, &path
->nexthop
);
437 stream_putl (s
, path
->ifindex
);
443 if (path
->nexthop
.s_addr
!= INADDR_ANY
&&
446 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_IFINDEX
);
447 stream_put_in_addr (s
, &path
->nexthop
);
448 stream_putl (s
, path
->ifindex
);
450 else if (path
->nexthop
.s_addr
!= INADDR_ANY
)
452 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
453 stream_put_in_addr (s
, &path
->nexthop
);
457 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
459 stream_putl (s
, path
->ifindex
);
464 #else /* HAVE_NETLINK */
465 if (path
->nexthop
.s_addr
!= INADDR_ANY
&&
468 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_IFINDEX
);
469 stream_put_in_addr (s
, &path
->nexthop
);
470 stream_putl (s
, path
->ifindex
);
472 else if (path
->nexthop
.s_addr
!= INADDR_ANY
)
474 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
475 stream_put_in_addr (s
, &path
->nexthop
);
479 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
481 stream_putl (s
, path
->ifindex
);
485 #endif /* HAVE_NETLINK */
487 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
489 char buf
[2][INET_ADDRSTRLEN
];
490 zlog_debug("Zebra: Route add %s/%d nexthop %s, ifindex=%d",
491 inet_ntop(AF_INET
, &p
->prefix
,
492 buf
[0], sizeof(buf
[0])),
494 inet_ntop(AF_INET
, &path
->nexthop
,
495 buf
[1], sizeof(buf
[1])),
500 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
501 stream_putc (s
, distance
);
502 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
504 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
505 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
506 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
507 stream_putl (s
, or->u
.ext
.type2_cost
);
509 stream_putl (s
, or->cost
);
512 if (CHECK_FLAG (message
, ZAPI_MESSAGE_TAG
))
513 stream_putw (s
, (u_short
)or->u
.ext
.tag
);
515 stream_putw_at (s
, 0, stream_get_endp (s
));
517 zclient_send_message(zclient
);
522 ospf_zebra_delete (struct prefix_ipv4
*p
, struct ospf_route
*or)
529 struct ospf_path
*path
;
530 struct listnode
*node
;
531 struct ospf
*ospf
= ospf_lookup ();
533 if (redist_check_instance(&zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_OSPF
], ospf
->instance
))
537 /* Distance value. */
538 distance
= ospf_distance_apply (p
, or);
543 /* Put command, type, flags, message. */
544 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_DELETE
);
545 stream_putc (s
, ZEBRA_ROUTE_OSPF
);
546 stream_putw (s
, ospf
->instance
);
547 stream_putc (s
, flags
);
548 stream_putc (s
, message
);
549 stream_putw (s
, SAFI_UNICAST
);
551 /* Put prefix information. */
552 psize
= PSIZE (p
->prefixlen
);
553 stream_putc (s
, p
->prefixlen
);
554 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
557 stream_putc (s
, or->paths
->count
);
559 /* Nexthop, ifindex, distance and metric information. */
560 for (ALL_LIST_ELEMENTS_RO (or->paths
, node
, path
))
562 if (path
->nexthop
.s_addr
!= INADDR_ANY
&&
565 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_IFINDEX
);
566 stream_put_in_addr (s
, &path
->nexthop
);
567 stream_putl (s
, path
->ifindex
);
569 else if (path
->nexthop
.s_addr
!= INADDR_ANY
)
571 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
572 stream_put_in_addr (s
, &path
->nexthop
);
576 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
577 stream_putl (s
, path
->ifindex
);
580 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
582 char buf
[2][INET_ADDRSTRLEN
];
583 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
584 inet_ntop(AF_INET
, &p
->prefix
,
585 buf
[0], sizeof(buf
[0])),
587 inet_ntop(AF_INET
, &path
->nexthop
,
588 buf
[1], sizeof(buf
[1])));
592 if (CHECK_FLAG (message
, ZAPI_MESSAGE_DISTANCE
))
593 stream_putc (s
, distance
);
594 if (CHECK_FLAG (message
, ZAPI_MESSAGE_METRIC
))
596 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
597 stream_putl (s
, or->cost
+ or->u
.ext
.type2_cost
);
598 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
599 stream_putl (s
, or->u
.ext
.type2_cost
);
601 stream_putl (s
, or->cost
);
604 stream_putw_at (s
, 0, stream_get_endp (s
));
606 zclient_send_message(zclient
);
611 ospf_zebra_add_discard (struct prefix_ipv4
*p
)
613 struct zapi_ipv4 api
;
614 struct ospf
*ospf
= ospf_lookup ();
616 if (redist_check_instance(&zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_OSPF
], ospf
->instance
))
618 api
.type
= ZEBRA_ROUTE_OSPF
;
619 api
.instance
= ospf
->instance
;
620 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
622 api
.safi
= SAFI_UNICAST
;
623 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
628 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD
, zclient
, p
, &api
);
630 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
631 zlog_debug ("Zebra: Route add discard %s/%d",
632 inet_ntoa (p
->prefix
), p
->prefixlen
);
637 ospf_zebra_delete_discard (struct prefix_ipv4
*p
)
639 struct zapi_ipv4 api
;
640 struct ospf
*ospf
= ospf_lookup ();
642 if (redist_check_instance(&zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_OSPF
], ospf
->instance
))
644 api
.type
= ZEBRA_ROUTE_OSPF
;
645 api
.instance
= ospf
->instance
;
646 api
.flags
= ZEBRA_FLAG_BLACKHOLE
;
648 api
.safi
= SAFI_UNICAST
;
649 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
654 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
656 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
657 zlog_debug ("Zebra: Route delete discard %s/%d",
658 inet_ntoa (p
->prefix
), p
->prefixlen
);
663 struct ospf_external
*
664 ospf_external_lookup (u_char type
, u_short instance
)
666 struct list
*ext_list
;
667 struct listnode
*node
;
668 struct ospf_external
*ext
;
670 ext_list
= om
->external
[type
];
674 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
675 if (ext
->instance
== instance
)
681 struct ospf_external
*
682 ospf_external_add (u_char type
, u_short instance
)
684 struct list
*ext_list
;
685 struct ospf_external
*ext
;
687 ext
= ospf_external_lookup(type
, instance
);
691 if (!om
->external
[type
])
692 om
->external
[type
] = list_new();
694 ext_list
= om
->external
[type
];
695 ext
= (struct ospf_external
*)calloc (1, sizeof(struct ospf_external
));
696 ext
->instance
= instance
;
697 EXTERNAL_INFO (ext
) = route_table_init ();
699 listnode_add(ext_list
, ext
);
705 ospf_external_del (u_char type
, u_short instance
)
707 struct ospf_external
*ext
;
709 ext
= ospf_external_lookup(type
, instance
);
713 if (EXTERNAL_INFO (ext
))
714 route_table_finish(EXTERNAL_INFO (ext
));
716 listnode_delete(om
->external
[type
], ext
);
717 if (!om
->external
[type
]->count
)
719 list_free(om
->external
[type
]);
720 om
->external
[type
] = NULL
;
726 ospf_redist_lookup (struct ospf
*ospf
, u_char type
, u_short instance
)
728 struct list
*red_list
;
729 struct listnode
*node
;
730 struct ospf_redist
*red
;
732 red_list
= ospf
->redist
[type
];
736 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
737 if (red
->instance
== instance
)
744 ospf_redist_add (struct ospf
*ospf
, u_char type
, u_short instance
)
746 struct list
*red_list
;
747 struct ospf_redist
*red
;
749 red
= ospf_redist_lookup(ospf
, type
, instance
);
753 if (!ospf
->redist
[type
])
754 ospf
->redist
[type
] = list_new();
756 red_list
= ospf
->redist
[type
];
757 red
= (struct ospf_redist
*)calloc (1, sizeof(struct ospf_redist
));
758 red
->instance
= instance
;
759 red
->dmetric
.type
= -1;
760 red
->dmetric
.value
= -1;
762 listnode_add(red_list
, red
);
768 ospf_redist_del (struct ospf
*ospf
, u_char type
, u_short instance
)
770 struct ospf_redist
*red
;
772 red
= ospf_redist_lookup(ospf
, type
, instance
);
776 listnode_delete(ospf
->redist
[type
], red
);
777 if (!ospf
->redist
[type
]->count
)
779 list_free(ospf
->redist
[type
]);
780 ospf
->redist
[type
] = NULL
;
787 ospf_is_type_redistributed (int type
, u_short instance
)
789 return (DEFAULT_ROUTE_TYPE (type
) ?
790 zclient
->default_information
:
791 redist_check_instance(&zclient
->redist
[AFI_IP
][type
], instance
));
795 ospf_redistribute_set (struct ospf
*ospf
, int type
, u_short instance
, int mtype
,
799 struct ospf_redist
*red
;
801 red
= ospf_redist_lookup(ospf
, type
, instance
);
802 if (ospf_is_type_redistributed (type
, instance
))
804 if (mtype
!= red
->dmetric
.type
)
806 red
->dmetric
.type
= mtype
;
807 force
= LSA_REFRESH_FORCE
;
809 if (mvalue
!= red
->dmetric
.value
)
811 red
->dmetric
.value
= mvalue
;
812 force
= LSA_REFRESH_FORCE
;
815 ospf_external_lsa_refresh_type (ospf
, type
, instance
, force
);
817 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
818 zlog_debug ("Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
819 ospf_redist_string(type
), instance
,
820 metric_type (ospf
, type
, instance
),
821 metric_value (ospf
, type
, instance
));
826 red
->dmetric
.type
= mtype
;
827 red
->dmetric
.value
= mvalue
;
829 ospf_external_add(type
, instance
);
831 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
, instance
);
833 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
834 zlog_debug ("Redistribute[%s][%d]: Start Type[%d], Metric[%d]",
835 ospf_redist_string(type
), instance
,
836 metric_type (ospf
, type
, instance
), metric_value (ospf
, type
, instance
));
838 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
844 ospf_redistribute_unset (struct ospf
*ospf
, int type
, u_short instance
)
846 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
849 if (!ospf_is_type_redistributed (type
, instance
))
852 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
, instance
);
854 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
855 zlog_debug ("Redistribute[%s][%d]: Stop",
856 ospf_redist_string(type
), instance
);
858 ospf_redist_del (ospf
, type
, instance
);
860 /* Remove the routes from OSPF table. */
861 ospf_redistribute_withdraw (ospf
, type
, instance
);
863 ospf_external_del(type
, instance
);
865 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
871 ospf_redistribute_default_set (struct ospf
*ospf
, int originate
,
872 int mtype
, int mvalue
)
874 struct ospf_redist
*red
;
876 ospf
->default_originate
= originate
;
878 red
= ospf_redist_add(ospf
, DEFAULT_ROUTE
, 0);
879 red
->dmetric
.type
= mtype
;
880 red
->dmetric
.value
= mvalue
;
882 ospf_external_add(DEFAULT_ROUTE
, 0);
884 if (ospf_is_type_redistributed (DEFAULT_ROUTE
, 0))
886 /* if ospf->default_originate changes value, is calling
887 ospf_external_lsa_refresh_default sufficient to implement
889 ospf_external_lsa_refresh_default (ospf
);
891 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
892 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
893 ospf_redist_string(DEFAULT_ROUTE
),
894 metric_type (ospf
, DEFAULT_ROUTE
, 0),
895 metric_value (ospf
, DEFAULT_ROUTE
, 0));
899 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
);
901 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
902 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
903 metric_type (ospf
, DEFAULT_ROUTE
, 0),
904 metric_value (ospf
, DEFAULT_ROUTE
, 0));
906 if (ospf
->router_id
.s_addr
== 0)
907 ospf
->external_origin
|= (1 << DEFAULT_ROUTE
);
909 thread_add_timer (master
, ospf_default_originate_timer
, ospf
, 1);
911 ospf_asbr_status_update (ospf
, ++ospf
->redistribute
);
917 ospf_redistribute_default_unset (struct ospf
*ospf
)
919 if (!ospf_is_type_redistributed (DEFAULT_ROUTE
, 0))
922 ospf
->default_originate
= DEFAULT_ORIGINATE_NONE
;
923 ospf_redist_del(ospf
, DEFAULT_ROUTE
, 0);
925 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
);
927 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
928 zlog_debug ("Redistribute[DEFAULT]: Stop");
930 //Pending: how does the external_info cleanup work in this case?
932 ospf_asbr_status_update (ospf
, --ospf
->redistribute
);
938 ospf_external_lsa_originate_check (struct ospf
*ospf
,
939 struct external_info
*ei
)
941 /* If prefix is multicast, then do not originate LSA. */
942 if (IN_MULTICAST (htonl (ei
->p
.prefix
.s_addr
)))
944 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
945 "Prefix belongs multicast", inet_ntoa (ei
->p
.prefix
));
949 /* Take care of default-originate. */
950 if (is_prefix_default (&ei
->p
))
951 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
)
953 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
961 /* If connected prefix is OSPF enable interface, then do not announce. */
963 ospf_distribute_check_connected (struct ospf
*ospf
, struct external_info
*ei
)
965 struct listnode
*node
;
966 struct ospf_interface
*oi
;
969 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
970 if (prefix_match (oi
->address
, (struct prefix
*) &ei
->p
))
975 /* return 1 if external LSA must be originated, 0 otherwise */
977 ospf_redistribute_check (struct ospf
*ospf
,
978 struct external_info
*ei
, int *changed
)
980 struct route_map_set_values save_values
;
981 struct prefix_ipv4
*p
= &ei
->p
;
982 struct ospf_redist
*red
;
983 u_char type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
984 u_short instance
= is_prefix_default (&ei
->p
) ? 0 : ei
->instance
;
989 if (!ospf_external_lsa_originate_check (ospf
, ei
))
992 /* Take care connected route. */
993 if (type
== ZEBRA_ROUTE_CONNECT
&&
994 !ospf_distribute_check_connected (ospf
, ei
))
997 if (!DEFAULT_ROUTE_TYPE (type
) && DISTRIBUTE_NAME (ospf
, type
))
998 /* distirbute-list exists, but access-list may not? */
999 if (DISTRIBUTE_LIST (ospf
, type
))
1000 if (access_list_apply (DISTRIBUTE_LIST (ospf
, type
), p
) == FILTER_DENY
)
1002 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
1003 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
1004 ospf_redist_string(type
),
1005 inet_ntoa (p
->prefix
), p
->prefixlen
);
1009 save_values
= ei
->route_map_set
;
1010 ospf_reset_route_map_set_values (&ei
->route_map_set
);
1012 /* apply route-map if needed */
1013 red
= ospf_redist_lookup (ospf
, type
, instance
);
1014 if (red
&& ROUTEMAP_NAME(red
))
1018 ret
= route_map_apply (ROUTEMAP (red
), (struct prefix
*) p
,
1021 if (ret
== RMAP_DENYMATCH
)
1023 ei
->route_map_set
= save_values
;
1024 if (IS_DEBUG_OSPF (zebra
, ZEBRA_REDISTRIBUTE
))
1025 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
1026 ospf_redist_string(type
),
1027 inet_ntoa (p
->prefix
), p
->prefixlen
);
1031 /* check if 'route-map set' changed something */
1033 *changed
= !ospf_route_map_set_compare (&ei
->route_map_set
,
1040 /* OSPF route-map set for redistribution */
1042 ospf_routemap_set (struct ospf_redist
*red
, const char *name
)
1044 if (ROUTEMAP_NAME (red
))
1045 free (ROUTEMAP_NAME (red
));
1047 ROUTEMAP_NAME (red
) = strdup (name
);
1048 ROUTEMAP (red
) = route_map_lookup_by_name (name
);
1052 ospf_routemap_unset (struct ospf_redist
*red
)
1054 if (ROUTEMAP_NAME (red
))
1055 free (ROUTEMAP_NAME (red
));
1057 ROUTEMAP_NAME (red
) = NULL
;
1058 ROUTEMAP (red
) = NULL
;
1061 /* Zebra route add and delete treatment. */
1063 ospf_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
1064 zebra_size_t length
)
1067 struct zapi_ipv4 api
;
1068 unsigned long ifindex
;
1069 struct in_addr nexthop
;
1070 struct prefix_ipv4 p
;
1071 struct external_info
*ei
;
1078 /* Type, flags, message. */
1079 api
.type
= stream_getc (s
);
1080 api
.instance
= stream_getw (s
);
1081 api
.flags
= stream_getc (s
);
1082 api
.message
= stream_getc (s
);
1085 memset (&p
, 0, sizeof (struct prefix_ipv4
));
1087 p
.prefixlen
= stream_getc (s
);
1088 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
1090 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
1093 /* Nexthop, ifindex, distance, metric. */
1094 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1096 api
.nexthop_num
= stream_getc (s
);
1097 nexthop
.s_addr
= stream_get_ipv4 (s
);
1099 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
1101 api
.ifindex_num
= stream_getc (s
);
1102 /* XXX assert(api.ifindex_num == 1); */
1103 ifindex
= stream_getl (s
);
1105 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
1106 api
.distance
= stream_getc (s
);
1107 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
1108 api
.metric
= stream_getl (s
);
1109 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
1110 api
.tag
= stream_getw (s
);
1114 ospf
= ospf_lookup ();
1118 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
1120 /* XXX|HACK|TODO|FIXME:
1121 * Maybe we should ignore reject/blackhole routes? Testing shows that
1122 * there is no problems though and this is only way to "summarize"
1123 * routes in ASBR at the moment. Maybe we need just a better generalised
1124 * solution for these types?
1126 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
1127 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
1131 /* Protocol tag overwrites all other tag value send by zebra */
1132 if (ospf
->dtag
[api
.type
] > 0)
1133 api
.tag
= ospf
->dtag
[api
.type
];
1135 ei
= ospf_external_info_add (api
.type
, api
.instance
, p
, ifindex
,
1138 if (ospf
->router_id
.s_addr
== 0)
1139 /* Set flags to generate AS-external-LSA originate event
1140 for each redistributed protocols later. */
1141 ospf
->external_origin
|= (1 << api
.type
);
1146 if (is_prefix_default (&p
))
1147 ospf_external_lsa_refresh_default (ospf
);
1150 struct ospf_lsa
*current
;
1152 current
= ospf_external_info_find_lsa (ospf
, &ei
->p
);
1154 ospf_external_lsa_originate (ospf
, ei
);
1155 else if (IS_LSA_MAXAGE (current
))
1156 ospf_external_lsa_refresh (ospf
, current
,
1157 ei
, LSA_REFRESH_FORCE
);
1159 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
1160 inet_ntoa (p
.prefix
));
1165 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
1167 ospf_external_info_delete (api
.type
, api
.instance
, p
);
1168 if (is_prefix_default (&p
))
1169 ospf_external_lsa_refresh_default (ospf
);
1171 ospf_external_lsa_flush (ospf
, api
.type
, &p
, ifindex
/*, nexthop */);
1179 ospf_distribute_list_out_set (struct ospf
*ospf
, int type
, const char *name
)
1181 /* Lookup access-list for distribute-list. */
1182 DISTRIBUTE_LIST (ospf
, type
) = access_list_lookup (AFI_IP
, name
);
1184 /* Clear previous distribute-name. */
1185 if (DISTRIBUTE_NAME (ospf
, type
))
1186 free (DISTRIBUTE_NAME (ospf
, type
));
1188 /* Set distribute-name. */
1189 DISTRIBUTE_NAME (ospf
, type
) = strdup (name
);
1191 /* If access-list have been set, schedule update timer. */
1192 if (DISTRIBUTE_LIST (ospf
, type
))
1193 ospf_distribute_list_update (ospf
, type
, 0);
1199 ospf_distribute_list_out_unset (struct ospf
*ospf
, int type
, const char *name
)
1201 /* Schedule update timer. */
1202 if (DISTRIBUTE_LIST (ospf
, type
))
1203 ospf_distribute_list_update (ospf
, type
, 0);
1205 /* Unset distribute-list. */
1206 DISTRIBUTE_LIST (ospf
, type
) = NULL
;
1208 /* Clear distribute-name. */
1209 if (DISTRIBUTE_NAME (ospf
, type
))
1210 free (DISTRIBUTE_NAME (ospf
, type
));
1212 DISTRIBUTE_NAME (ospf
, type
) = NULL
;
1217 /* distribute-list update timer. */
1219 ospf_distribute_list_update_timer (struct thread
*thread
)
1221 struct route_node
*rn
;
1222 struct external_info
*ei
;
1223 struct route_table
*rt
;
1224 struct ospf_lsa
*lsa
;
1225 int type
, default_refresh
= 0;
1228 ospf
= ospf_lookup ();
1232 ospf
->t_distribute_update
= NULL
;
1234 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
1236 /* foreach all external info. */
1237 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
1239 struct list
*ext_list
;
1240 struct listnode
*node
;
1241 struct ospf_external
*ext
;
1243 ext_list
= om
->external
[type
];
1247 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
1249 rt
= ext
->external_info
;
1252 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
1253 if ((ei
= rn
->info
) != NULL
)
1255 if (is_prefix_default (&ei
->p
))
1256 default_refresh
= 1;
1257 else if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
1258 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_IF_CHANGED
);
1260 ospf_external_lsa_originate (ospf
, ei
);
1264 if (default_refresh
)
1265 ospf_external_lsa_refresh_default (ospf
);
1269 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
1271 /* Update distribute-list and set timer to apply access-list. */
1273 ospf_distribute_list_update (struct ospf
*ospf
, uintptr_t type
,
1276 struct route_table
*rt
;
1277 struct ospf_external
*ext
;
1279 /* External info does not exist. */
1280 ext
= ospf_external_lookup(type
, instance
);
1281 if (!ext
|| !(rt
= EXTERNAL_INFO (ext
)))
1284 /* If exists previously invoked thread, then let it continue. */
1285 if (ospf
->t_distribute_update
)
1289 ospf
->t_distribute_update
=
1290 thread_add_timer (master
, ospf_distribute_list_update_timer
,
1291 (void *) type
, OSPF_DISTRIBUTE_UPDATE_DELAY
);
1294 /* If access-list is updated, apply some check. */
1296 ospf_filter_update (struct access_list
*access
)
1301 struct ospf_area
*area
;
1302 struct listnode
*node
;
1304 /* If OSPF instance does not exist, return right now. */
1305 ospf
= ospf_lookup ();
1309 /* Update distribute-list, and apply filter. */
1310 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
1312 struct list
*red_list
;
1313 struct listnode
*node
;
1314 struct ospf_redist
*red
;
1316 red_list
= ospf
->redist
[type
];
1318 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1322 /* if route-map is not NULL it may be using this access list */
1323 ospf_distribute_list_update (ospf
, type
, red
->instance
);
1327 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1328 * but no distribute list. */
1329 if (type
== ZEBRA_ROUTE_MAX
)
1332 if (DISTRIBUTE_NAME (ospf
, type
))
1334 /* Keep old access-list for distribute-list. */
1335 struct access_list
*old
= DISTRIBUTE_LIST (ospf
, type
);
1337 /* Update access-list for distribute-list. */
1338 DISTRIBUTE_LIST (ospf
, type
) =
1339 access_list_lookup (AFI_IP
, DISTRIBUTE_NAME (ospf
, type
));
1341 /* No update for this distribute type. */
1342 if (old
== NULL
&& DISTRIBUTE_LIST (ospf
, type
) == NULL
)
1345 /* Schedule distribute-list update timer. */
1346 if (DISTRIBUTE_LIST (ospf
, type
) == NULL
||
1347 strcmp (DISTRIBUTE_NAME (ospf
, type
), access
->name
) == 0)
1348 ospf_distribute_list_update (ospf
, type
, 0);
1352 /* Update Area access-list. */
1353 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1355 if (EXPORT_NAME (area
))
1357 EXPORT_LIST (area
) = NULL
;
1361 if (IMPORT_NAME (area
))
1363 IMPORT_LIST (area
) = NULL
;
1368 /* Schedule ABR tasks -- this will be changed -- takada. */
1369 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1370 ospf_schedule_abr_task (ospf
);
1373 /* If prefix-list is updated, do some updates. */
1375 ospf_prefix_list_update (struct prefix_list
*plist
)
1380 struct ospf_area
*area
;
1381 struct listnode
*node
;
1383 /* If OSPF instatnce does not exist, return right now. */
1384 ospf
= ospf_lookup ();
1388 /* Update all route-maps which are used as redistribution filters.
1389 * They might use prefix-list.
1391 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
1393 struct list
*red_list
;
1394 struct listnode
*node
;
1395 struct ospf_redist
*red
;
1397 red_list
= ospf
->redist
[type
];
1399 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1403 /* if route-map is not NULL it may be using this prefix list */
1404 ospf_distribute_list_update (ospf
, type
, red
->instance
);
1409 /* Update area filter-lists. */
1410 for (ALL_LIST_ELEMENTS_RO (ospf
->areas
, node
, area
))
1412 /* Update filter-list in. */
1413 if (PREFIX_NAME_IN (area
))
1414 if (strcmp (PREFIX_NAME_IN (area
), plist
->name
) == 0)
1416 PREFIX_LIST_IN (area
) =
1417 prefix_list_lookup (AFI_IP
, PREFIX_NAME_IN (area
));
1421 /* Update filter-list out. */
1422 if (PREFIX_NAME_OUT (area
))
1423 if (strcmp (PREFIX_NAME_OUT (area
), plist
->name
) == 0)
1425 PREFIX_LIST_IN (area
) =
1426 prefix_list_lookup (AFI_IP
, PREFIX_NAME_OUT (area
));
1431 /* Schedule ABR task. */
1432 if (IS_OSPF_ABR (ospf
) && abr_inv
)
1433 ospf_schedule_abr_task (ospf
);
1436 static struct ospf_distance
*
1437 ospf_distance_new (void)
1439 return XCALLOC (MTYPE_OSPF_DISTANCE
, sizeof (struct ospf_distance
));
1443 ospf_distance_free (struct ospf_distance
*odistance
)
1445 XFREE (MTYPE_OSPF_DISTANCE
, odistance
);
1449 ospf_distance_set (struct vty
*vty
, struct ospf
*ospf
,
1450 const char *distance_str
,
1452 const char *access_list_str
)
1455 struct prefix_ipv4 p
;
1457 struct route_node
*rn
;
1458 struct ospf_distance
*odistance
;
1460 ret
= str2prefix_ipv4 (ip_str
, &p
);
1463 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1467 distance
= atoi (distance_str
);
1469 /* Get OSPF distance node. */
1470 rn
= route_node_get (ospf
->distance_table
, (struct prefix
*) &p
);
1473 odistance
= rn
->info
;
1474 route_unlock_node (rn
);
1478 odistance
= ospf_distance_new ();
1479 rn
->info
= odistance
;
1482 /* Set distance value. */
1483 odistance
->distance
= distance
;
1485 /* Reset access-list configuration. */
1486 if (odistance
->access_list
)
1488 free (odistance
->access_list
);
1489 odistance
->access_list
= NULL
;
1491 if (access_list_str
)
1492 odistance
->access_list
= strdup (access_list_str
);
1498 ospf_distance_unset (struct vty
*vty
, struct ospf
*ospf
,
1499 const char *distance_str
,
1500 const char *ip_str
, char
1501 const *access_list_str
)
1504 struct prefix_ipv4 p
;
1505 struct route_node
*rn
;
1506 struct ospf_distance
*odistance
;
1508 ret
= str2prefix_ipv4 (ip_str
, &p
);
1511 vty_out (vty
, "Malformed prefix%s", VTY_NEWLINE
);
1515 rn
= route_node_lookup (ospf
->distance_table
, (struct prefix
*) &p
);
1518 vty_out (vty
, "Can't find specified prefix%s", VTY_NEWLINE
);
1522 odistance
= rn
->info
;
1524 if (odistance
->access_list
)
1525 free (odistance
->access_list
);
1526 ospf_distance_free (odistance
);
1529 route_unlock_node (rn
);
1530 route_unlock_node (rn
);
1536 ospf_distance_reset (struct ospf
*ospf
)
1538 struct route_node
*rn
;
1539 struct ospf_distance
*odistance
;
1541 for (rn
= route_top (ospf
->distance_table
); rn
; rn
= route_next (rn
))
1542 if ((odistance
= rn
->info
) != NULL
)
1544 if (odistance
->access_list
)
1545 free (odistance
->access_list
);
1546 ospf_distance_free (odistance
);
1548 route_unlock_node (rn
);
1553 ospf_distance_apply (struct prefix_ipv4
*p
, struct ospf_route
*or)
1557 ospf
= ospf_lookup ();
1561 if (ospf
->distance_intra
)
1562 if (or->path_type
== OSPF_PATH_INTRA_AREA
)
1563 return ospf
->distance_intra
;
1565 if (ospf
->distance_inter
)
1566 if (or->path_type
== OSPF_PATH_INTER_AREA
)
1567 return ospf
->distance_inter
;
1569 if (ospf
->distance_external
)
1570 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
1571 || or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
1572 return ospf
->distance_external
;
1574 if (ospf
->distance_all
)
1575 return ospf
->distance_all
;
1581 ospf_zebra_init (u_short instance
)
1583 /* Allocate zebra structure. */
1584 zclient
= zclient_new ();
1585 zclient_init (zclient
, ZEBRA_ROUTE_OSPF
, instance
);
1586 zclient
->router_id_update
= ospf_router_id_update_zebra
;
1587 zclient
->interface_add
= ospf_interface_add
;
1588 zclient
->interface_delete
= ospf_interface_delete
;
1589 zclient
->interface_up
= ospf_interface_state_up
;
1590 zclient
->interface_down
= ospf_interface_state_down
;
1591 zclient
->interface_address_add
= ospf_interface_address_add
;
1592 zclient
->interface_address_delete
= ospf_interface_address_delete
;
1593 zclient
->interface_bfd_dest_down
= ospf_interface_bfd_dest_down
;
1594 zclient
->ipv4_route_add
= ospf_zebra_read_ipv4
;
1595 zclient
->ipv4_route_delete
= ospf_zebra_read_ipv4
;
1597 access_list_add_hook (ospf_filter_update
);
1598 access_list_delete_hook (ospf_filter_update
);
1599 prefix_list_add_hook (ospf_prefix_list_update
);
1600 prefix_list_delete_hook (ospf_prefix_list_update
);