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 */
51 #include "ospfd/ospf_te.h"
53 /* Zebra structure to hold current status. */
54 struct zclient *zclient = NULL;
56 /* For registering threads. */
57 extern struct thread_master *master;
58 struct in_addr router_id_zebra;
60 /* Router-id update message from zebra. */
62 ospf_router_id_update_zebra (int command, struct zclient *zclient,
63 zebra_size_t length, vrf_id_t vrf_id)
66 struct prefix router_id;
67 zebra_router_id_update_read(zclient->ibuf,&router_id);
69 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
72 prefix2str(&router_id, buf, sizeof(buf));
73 zlog_debug("Zebra rcvd: router id update %s", buf);
76 router_id_zebra = router_id.u.prefix4;
78 ospf = ospf_lookup ();
81 ospf_router_id_update (ospf);
86 /* Inteface addition message from zebra. */
88 ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length,
91 struct interface *ifp;
93 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
95 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
96 zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
97 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
98 ifp->metric, ifp->mtu);
102 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
104 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
105 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
108 ospf_if_update (NULL, ifp);
111 ospf_snmp_if_update (ifp);
112 #endif /* HAVE_SNMP */
118 ospf_interface_delete (int command, struct zclient *zclient,
119 zebra_size_t length, vrf_id_t vrf_id)
121 struct interface *ifp;
123 struct route_node *rn;
126 /* zebra_interface_state_read() updates interface structure in iflist */
127 ifp = zebra_interface_state_read (s, vrf_id);
133 zlog_warn ("Zebra: got delete of %s, but interface is still up",
136 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
138 ("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
139 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
142 ospf_snmp_if_delete (ifp);
143 #endif /* HAVE_SNMP */
145 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
147 ospf_if_free ((struct ospf_interface *) rn->info);
149 ifp->ifindex = IFINDEX_INTERNAL;
153 static struct interface *
154 zebra_interface_if_lookup (struct stream *s, vrf_id_t vrf_id)
156 char ifname_tmp[INTERFACE_NAMSIZ];
158 /* Read interface name. */
159 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
161 /* And look it up. */
162 return if_lookup_by_name_len(ifname_tmp,
163 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
167 ospf_interface_state_up (int command, struct zclient *zclient,
168 zebra_size_t length, vrf_id_t vrf_id)
170 struct interface *ifp;
171 struct ospf_interface *oi;
172 struct route_node *rn;
174 ifp = zebra_interface_if_lookup (zclient->ibuf, vrf_id);
179 /* Interface is already up. */
180 if (if_is_operative (ifp))
182 /* Temporarily keep ifp values. */
183 struct interface if_tmp;
184 memcpy (&if_tmp, ifp, sizeof (struct interface));
186 zebra_interface_if_set_value (zclient->ibuf, ifp);
188 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
189 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
191 if (if_tmp.bandwidth != ifp->bandwidth)
193 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
194 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
195 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
197 ospf_if_recalculate_output_cost (ifp);
200 if (if_tmp.mtu != ifp->mtu)
202 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
203 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
204 ifp->name, if_tmp.mtu, ifp->mtu);
206 /* Must reset the interface (simulate down/up) when MTU changes. */
212 zebra_interface_if_set_value (zclient->ibuf, ifp);
214 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
215 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
217 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
219 if ((oi = rn->info) == NULL)
229 ospf_interface_state_down (int command, struct zclient *zclient,
230 zebra_size_t length, vrf_id_t vrf_id)
232 struct interface *ifp;
233 struct ospf_interface *oi;
234 struct route_node *node;
236 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
241 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
242 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
244 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
246 if ((oi = node->info) == NULL)
255 ospf_interface_address_add (int command, struct zclient *zclient,
256 zebra_size_t length, vrf_id_t vrf_id)
260 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
265 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
268 prefix2str(c->address, buf, sizeof(buf));
269 zlog_debug("Zebra: interface %s address add %s", c->ifp->name, buf);
272 ospf_if_update (NULL, c->ifp);
275 ospf_snmp_if_update (c->ifp);
276 #endif /* HAVE_SNMP */
282 ospf_interface_address_delete (int command, struct zclient *zclient,
283 zebra_size_t length, vrf_id_t vrf_id)
286 struct interface *ifp;
287 struct ospf_interface *oi;
288 struct route_node *rn;
291 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
296 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
299 prefix2str(c->address, buf, sizeof(buf));
300 zlog_debug("Zebra: interface %s address delete %s", c->ifp->name, buf);
305 p.prefixlen = IPV4_MAX_PREFIXLEN;
307 rn = route_node_lookup (IF_OIFS (ifp), &p);
316 route_unlock_node (rn);
318 /* Call interface hook functions to clean up */
322 ospf_snmp_if_update (c->ifp);
323 #endif /* HAVE_SNMP */
331 ospf_interface_link_params (int command, struct zclient *zclient,
334 struct interface *ifp;
336 ifp = zebra_interface_link_params_read (zclient->ibuf);
342 ospf_mpls_te_update_if (ifp);
349 ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
356 struct ospf_path *path;
357 struct listnode *node;
359 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
364 /* OSPF pass nexthop and metric */
365 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
366 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
368 /* Distance value. */
369 distance = ospf_distance_apply (p, or);
371 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
373 /* Check if path type is ASE */
374 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL) ||
375 (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)) &&
376 (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX))
377 SET_FLAG (message, ZAPI_MESSAGE_TAG);
383 /* Put command, type, flags, message. */
384 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD, VRF_DEFAULT);
385 stream_putc (s, ZEBRA_ROUTE_OSPF);
386 stream_putc (s, flags);
387 stream_putc (s, message);
388 stream_putw (s, SAFI_UNICAST);
390 /* Put prefix information. */
391 psize = PSIZE (p->prefixlen);
392 stream_putc (s, p->prefixlen);
393 stream_write (s, (u_char *) & p->prefix, psize);
396 stream_putc (s, or->paths->count);
398 /* Nexthop, ifindex, distance and metric information. */
399 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
401 if (path->nexthop.s_addr != INADDR_ANY &&
404 stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
405 stream_put_in_addr (s, &path->nexthop);
406 stream_putl (s, path->ifindex);
408 else if (path->nexthop.s_addr != INADDR_ANY)
410 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
411 stream_put_in_addr (s, &path->nexthop);
415 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
417 stream_putl (s, path->ifindex);
422 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
424 char buf[2][INET_ADDRSTRLEN];
425 zlog_debug("Zebra: Route add %s/%d nexthop %s",
426 inet_ntop(AF_INET, &p->prefix,
427 buf[0], sizeof(buf[0])),
429 inet_ntop(AF_INET, &path->nexthop,
430 buf[1], sizeof(buf[1])));
434 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
435 stream_putc (s, distance);
436 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
438 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
439 stream_putl (s, or->cost + or->u.ext.type2_cost);
440 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
441 stream_putl (s, or->u.ext.type2_cost);
443 stream_putl (s, or->cost);
446 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
447 stream_putl (s, or->u.ext.tag);
449 stream_putw_at (s, 0, stream_get_endp (s));
451 zclient_send_message(zclient);
456 ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
463 struct ospf_path *path;
464 struct listnode *node;
466 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
470 /* Distance value. */
471 distance = ospf_distance_apply (p, or);
476 /* Put command, type, flags, message. */
477 zclient_create_header (s, ZEBRA_IPV4_ROUTE_DELETE, VRF_DEFAULT);
478 stream_putc (s, ZEBRA_ROUTE_OSPF);
479 stream_putc (s, flags);
480 stream_putc (s, message);
481 stream_putw (s, SAFI_UNICAST);
483 /* Put prefix information. */
484 psize = PSIZE (p->prefixlen);
485 stream_putc (s, p->prefixlen);
486 stream_write (s, (u_char *) & p->prefix, psize);
489 stream_putc (s, or->paths->count);
491 /* Nexthop, ifindex, distance and metric information. */
492 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
494 if (path->nexthop.s_addr != INADDR_ANY &&
497 stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
498 stream_put_in_addr (s, &path->nexthop);
499 stream_putl (s, path->ifindex);
501 else if (path->nexthop.s_addr != INADDR_ANY)
503 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
504 stream_put_in_addr (s, &path->nexthop);
508 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
509 stream_putl (s, path->ifindex);
512 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
514 char buf[2][INET_ADDRSTRLEN];
515 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
516 inet_ntop(AF_INET, &p->prefix,
517 buf[0], sizeof(buf[0])),
519 inet_ntop(AF_INET, &path->nexthop,
520 buf[1], sizeof(buf[1])));
524 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
525 stream_putc (s, distance);
526 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
528 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
529 stream_putl (s, or->cost + or->u.ext.type2_cost);
530 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
531 stream_putl (s, or->u.ext.type2_cost);
533 stream_putl (s, or->cost);
536 stream_putw_at (s, 0, stream_get_endp (s));
538 zclient_send_message(zclient);
543 ospf_zebra_add_discard (struct prefix_ipv4 *p)
545 struct zapi_ipv4 api;
547 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
549 api.vrf_id = VRF_DEFAULT;
550 api.type = ZEBRA_ROUTE_OSPF;
551 api.flags = ZEBRA_FLAG_BLACKHOLE;
553 api.safi = SAFI_UNICAST;
554 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
559 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
561 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
562 zlog_debug ("Zebra: Route add discard %s/%d",
563 inet_ntoa (p->prefix), p->prefixlen);
568 ospf_zebra_delete_discard (struct prefix_ipv4 *p)
570 struct zapi_ipv4 api;
572 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
574 api.vrf_id = VRF_DEFAULT;
575 api.type = ZEBRA_ROUTE_OSPF;
576 api.flags = ZEBRA_FLAG_BLACKHOLE;
578 api.safi = SAFI_UNICAST;
579 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
584 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
586 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
587 zlog_debug ("Zebra: Route delete discard %s/%d",
588 inet_ntoa (p->prefix), p->prefixlen);
594 ospf_is_type_redistributed (int type)
596 return (DEFAULT_ROUTE_TYPE (type)) ?
597 vrf_bitmap_check (zclient->default_information, VRF_DEFAULT) : \
598 vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT);
602 ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
606 if (ospf_is_type_redistributed (type))
608 if (mtype != ospf->dmetric[type].type)
610 ospf->dmetric[type].type = mtype;
611 force = LSA_REFRESH_FORCE;
613 if (mvalue != ospf->dmetric[type].value)
615 ospf->dmetric[type].value = mvalue;
616 force = LSA_REFRESH_FORCE;
619 ospf_external_lsa_refresh_type (ospf, type, force);
621 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
622 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
623 ospf_redist_string(type),
624 metric_type (ospf, type), metric_value (ospf, type));
629 ospf->dmetric[type].type = mtype;
630 ospf->dmetric[type].value = mvalue;
632 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
634 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
635 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
636 ospf_redist_string(type),
637 metric_type (ospf, type), metric_value (ospf, type));
639 ospf_asbr_status_update (ospf, ++ospf->redistribute);
645 ospf_redistribute_unset (struct ospf *ospf, int type)
647 if (type == zclient->redist_default)
650 if (!ospf_is_type_redistributed (type))
653 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type, VRF_DEFAULT);
655 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
656 zlog_debug ("Redistribute[%s]: Stop",
657 ospf_redist_string(type));
659 ospf->dmetric[type].type = -1;
660 ospf->dmetric[type].value = -1;
662 /* Remove the routes from OSPF table. */
663 ospf_redistribute_withdraw (ospf, type);
665 ospf_asbr_status_update (ospf, --ospf->redistribute);
671 ospf_redistribute_default_set (struct ospf *ospf, int originate,
672 int mtype, int mvalue)
674 ospf->default_originate = originate;
675 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
676 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
678 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
680 /* if ospf->default_originate changes value, is calling
681 ospf_external_lsa_refresh_default sufficient to implement
683 ospf_external_lsa_refresh_default (ospf);
685 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
686 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
687 ospf_redist_string(DEFAULT_ROUTE),
688 metric_type (ospf, DEFAULT_ROUTE),
689 metric_value (ospf, DEFAULT_ROUTE));
693 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient,
696 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
697 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
698 metric_type (ospf, DEFAULT_ROUTE),
699 metric_value (ospf, DEFAULT_ROUTE));
701 if (ospf->router_id.s_addr == 0)
702 ospf->external_origin |= (1 << DEFAULT_ROUTE);
704 thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
706 ospf_asbr_status_update (ospf, ++ospf->redistribute);
712 ospf_redistribute_default_unset (struct ospf *ospf)
714 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
717 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
718 ospf->dmetric[DEFAULT_ROUTE].type = -1;
719 ospf->dmetric[DEFAULT_ROUTE].value = -1;
721 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient,
724 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
725 zlog_debug ("Redistribute[DEFAULT]: Stop");
727 ospf_asbr_status_update (ospf, --ospf->redistribute);
733 ospf_external_lsa_originate_check (struct ospf *ospf,
734 struct external_info *ei)
736 /* If prefix is multicast, then do not originate LSA. */
737 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
739 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
740 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
744 /* Take care of default-originate. */
745 if (is_prefix_default (&ei->p))
746 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
748 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
756 /* If connected prefix is OSPF enable interface, then do not announce. */
758 ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
760 struct listnode *node;
761 struct ospf_interface *oi;
764 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
765 if (prefix_match (oi->address, (struct prefix *) &ei->p))
770 /* return 1 if external LSA must be originated, 0 otherwise */
772 ospf_redistribute_check (struct ospf *ospf,
773 struct external_info *ei, int *changed)
775 struct route_map_set_values save_values;
776 struct prefix_ipv4 *p = &ei->p;
777 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
782 if (!ospf_external_lsa_originate_check (ospf, ei))
785 /* Take care connected route. */
786 if (type == ZEBRA_ROUTE_CONNECT &&
787 !ospf_distribute_check_connected (ospf, ei))
790 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
791 /* distirbute-list exists, but access-list may not? */
792 if (DISTRIBUTE_LIST (ospf, type))
793 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
795 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
796 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
797 ospf_redist_string(type),
798 inet_ntoa (p->prefix), p->prefixlen);
802 save_values = ei->route_map_set;
803 ospf_reset_route_map_set_values (&ei->route_map_set);
805 /* apply route-map if needed */
806 if (ROUTEMAP_NAME (ospf, type))
810 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
813 if (ret == RMAP_DENYMATCH)
815 ei->route_map_set = save_values;
816 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
817 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
818 ospf_redist_string(type),
819 inet_ntoa (p->prefix), p->prefixlen);
823 /* check if 'route-map set' changed something */
825 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
832 /* OSPF route-map set for redistribution */
834 ospf_routemap_set (struct ospf *ospf, int type, const char *name)
836 if (ROUTEMAP_NAME (ospf, type))
837 free (ROUTEMAP_NAME (ospf, type));
839 ROUTEMAP_NAME (ospf, type) = strdup (name);
840 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
844 ospf_routemap_unset (struct ospf *ospf, int type)
846 if (ROUTEMAP_NAME (ospf, type))
847 free (ROUTEMAP_NAME (ospf, type));
849 ROUTEMAP_NAME (ospf, type) = NULL;
850 ROUTEMAP (ospf, type) = NULL;
853 /* Zebra route add and delete treatment. */
855 ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
856 zebra_size_t length, vrf_id_t vrf_id)
859 struct zapi_ipv4 api;
860 unsigned long ifindex;
861 struct in_addr nexthop;
862 struct prefix_ipv4 p;
863 struct external_info *ei;
865 unsigned char plength = 0;
871 /* Type, flags, message. */
872 api.type = stream_getc (s);
873 api.flags = stream_getc (s);
874 api.message = stream_getc (s);
877 memset (&p, 0, sizeof (struct prefix_ipv4));
879 plength = stream_getc (s);
880 p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
881 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
883 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
886 /* Nexthop, ifindex, distance, metric. */
887 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
889 api.nexthop_num = stream_getc (s);
890 nexthop.s_addr = stream_get_ipv4 (s);
892 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
894 api.ifindex_num = stream_getc (s);
895 /* XXX assert(api.ifindex_num == 1); */
896 ifindex = stream_getl (s);
898 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
899 api.distance = stream_getc (s);
900 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
901 api.metric = stream_getl (s);
902 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
903 api.tag = stream_getl (s);
907 ospf = ospf_lookup ();
911 if (command == ZEBRA_IPV4_ROUTE_ADD)
913 /* XXX|HACK|TODO|FIXME:
914 * Maybe we should ignore reject/blackhole routes? Testing shows that
915 * there is no problems though and this is only way to "summarize"
916 * routes in ASBR at the moment. Maybe we need just a better generalised
917 * solution for these types?
919 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
920 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
924 /* Protocol tag overwrites all other tag value send by zebra */
925 if (ospf->dtag[api.type] > 0)
926 api.tag = ospf->dtag[api.type];
928 ei = ospf_external_info_add (api.type, p, ifindex, nexthop, api.tag);
930 if (ospf->router_id.s_addr == 0)
931 /* Set flags to generate AS-external-LSA originate event
932 for each redistributed protocols later. */
933 ospf->external_origin |= (1 << api.type);
938 if (is_prefix_default (&p))
939 ospf_external_lsa_refresh_default (ospf);
942 struct ospf_lsa *current;
944 current = ospf_external_info_find_lsa (ospf, &ei->p);
946 ospf_external_lsa_originate (ospf, ei);
947 else if (IS_LSA_MAXAGE (current))
948 ospf_external_lsa_refresh (ospf, current,
949 ei, LSA_REFRESH_FORCE);
951 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
952 inet_ntoa (p.prefix));
957 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
959 ospf_external_info_delete (api.type, p);
960 if (is_prefix_default (&p))
961 ospf_external_lsa_refresh_default (ospf);
963 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
971 ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
973 /* Lookup access-list for distribute-list. */
974 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
976 /* Clear previous distribute-name. */
977 if (DISTRIBUTE_NAME (ospf, type))
978 free (DISTRIBUTE_NAME (ospf, type));
980 /* Set distribute-name. */
981 DISTRIBUTE_NAME (ospf, type) = strdup (name);
983 /* If access-list have been set, schedule update timer. */
984 if (DISTRIBUTE_LIST (ospf, type))
985 ospf_distribute_list_update (ospf, type);
991 ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
993 /* Schedule update timer. */
994 if (DISTRIBUTE_LIST (ospf, type))
995 ospf_distribute_list_update (ospf, type);
997 /* Unset distribute-list. */
998 DISTRIBUTE_LIST (ospf, type) = NULL;
1000 /* Clear distribute-name. */
1001 if (DISTRIBUTE_NAME (ospf, type))
1002 free (DISTRIBUTE_NAME (ospf, type));
1004 DISTRIBUTE_NAME (ospf, type) = NULL;
1009 /* distribute-list update timer. */
1011 ospf_distribute_list_update_timer (struct thread *thread)
1013 struct route_node *rn;
1014 struct external_info *ei;
1015 struct route_table *rt;
1016 struct ospf_lsa *lsa;
1017 int type, default_refresh = 0;
1020 ospf = ospf_lookup ();
1024 ospf->t_distribute_update = NULL;
1026 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
1028 /* foreach all external info. */
1029 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
1031 rt = EXTERNAL_INFO (type);
1034 for (rn = route_top (rt); rn; rn = route_next (rn))
1035 if ((ei = rn->info) != NULL)
1037 if (is_prefix_default (&ei->p))
1038 default_refresh = 1;
1039 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
1040 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
1042 ospf_external_lsa_originate (ospf, ei);
1045 if (default_refresh)
1046 ospf_external_lsa_refresh_default (ospf);
1050 /* Update distribute-list and set timer to apply access-list. */
1052 ospf_distribute_list_update (struct ospf *ospf, uintptr_t type)
1054 struct route_table *rt;
1056 /* External info does not exist. */
1057 if (!(rt = EXTERNAL_INFO (type)))
1060 /* If exists previously invoked thread, then let it continue. */
1061 if (ospf->t_distribute_update)
1065 ospf->t_distribute_update =
1066 thread_add_timer_msec (master, ospf_distribute_list_update_timer,
1067 (void *) type, ospf->min_ls_interval);
1070 /* If access-list is updated, apply some check. */
1072 ospf_filter_update (const char *name)
1077 struct ospf_area *area;
1078 struct listnode *node;
1080 /* If OSPF instatnce does not exist, return right now. */
1081 ospf = ospf_lookup ();
1085 /* Update distribute-list, and apply filter. */
1086 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
1088 if (ROUTEMAP (ospf, type) != NULL)
1090 /* if route-map is not NULL it may be using this access list */
1091 ospf_distribute_list_update (ospf, type);
1095 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1096 * but no distribute list. */
1097 if (type == ZEBRA_ROUTE_MAX)
1100 if (DISTRIBUTE_NAME (ospf, type))
1102 /* Keep old access-list for distribute-list. */
1103 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1105 /* Update access-list for distribute-list. */
1106 DISTRIBUTE_LIST (ospf, type) =
1107 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1109 /* No update for this distribute type. */
1110 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1113 /* Schedule distribute-list update timer. */
1114 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1115 strcmp (DISTRIBUTE_NAME (ospf, type), name) == 0)
1116 ospf_distribute_list_update (ospf, type);
1120 /* Update Area access-list. */
1121 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1123 if (EXPORT_NAME (area))
1125 EXPORT_LIST (area) = NULL;
1129 if (IMPORT_NAME (area))
1131 IMPORT_LIST (area) = NULL;
1136 /* Schedule ABR tasks -- this will be changed -- takada. */
1137 if (IS_OSPF_ABR (ospf) && abr_inv)
1138 ospf_schedule_abr_task (ospf);
1141 /* If prefix-list is updated, do some updates. */
1143 ospf_prefix_list_update (struct prefix_list *plist)
1148 struct ospf_area *area;
1149 struct listnode *node;
1151 /* If OSPF instatnce does not exist, return right now. */
1152 ospf = ospf_lookup ();
1156 /* Update all route-maps which are used as redistribution filters.
1157 * They might use prefix-list.
1159 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
1161 if (ROUTEMAP (ospf, type) != NULL)
1163 /* If route-map is not NULL it may be using this prefix list */
1164 ospf_distribute_list_update (ospf, type);
1169 /* Update area filter-lists. */
1170 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1172 /* Update filter-list in. */
1173 if (PREFIX_NAME_IN (area))
1174 if (strcmp (PREFIX_NAME_IN (area), prefix_list_name (plist)) == 0)
1176 PREFIX_LIST_IN (area) =
1177 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1181 /* Update filter-list out. */
1182 if (PREFIX_NAME_OUT (area))
1183 if (strcmp (PREFIX_NAME_OUT (area), prefix_list_name (plist)) == 0)
1185 PREFIX_LIST_IN (area) =
1186 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1191 /* Schedule ABR task. */
1192 if (IS_OSPF_ABR (ospf) && abr_inv)
1193 ospf_schedule_abr_task (ospf);
1196 static struct ospf_distance *
1197 ospf_distance_new (void)
1199 return XCALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1203 ospf_distance_free (struct ospf_distance *odistance)
1205 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1209 ospf_distance_set (struct vty *vty, struct ospf *ospf,
1210 const char *distance_str,
1212 const char *access_list_str)
1215 struct prefix_ipv4 p;
1217 struct route_node *rn;
1218 struct ospf_distance *odistance;
1220 ret = str2prefix_ipv4 (ip_str, &p);
1223 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1227 distance = atoi (distance_str);
1229 /* Get OSPF distance node. */
1230 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
1233 odistance = rn->info;
1234 route_unlock_node (rn);
1238 odistance = ospf_distance_new ();
1239 rn->info = odistance;
1242 /* Set distance value. */
1243 odistance->distance = distance;
1245 /* Reset access-list configuration. */
1246 if (odistance->access_list)
1248 free (odistance->access_list);
1249 odistance->access_list = NULL;
1251 if (access_list_str)
1252 odistance->access_list = strdup (access_list_str);
1258 ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1259 const char *distance_str,
1260 const char *ip_str, char
1261 const *access_list_str)
1264 struct prefix_ipv4 p;
1265 struct route_node *rn;
1266 struct ospf_distance *odistance;
1268 ret = str2prefix_ipv4 (ip_str, &p);
1271 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1275 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1278 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1282 odistance = rn->info;
1284 if (odistance->access_list)
1285 free (odistance->access_list);
1286 ospf_distance_free (odistance);
1289 route_unlock_node (rn);
1290 route_unlock_node (rn);
1296 ospf_distance_reset (struct ospf *ospf)
1298 struct route_node *rn;
1299 struct ospf_distance *odistance;
1301 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
1302 if ((odistance = rn->info) != NULL)
1304 if (odistance->access_list)
1305 free (odistance->access_list);
1306 ospf_distance_free (odistance);
1308 route_unlock_node (rn);
1313 ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1317 ospf = ospf_lookup ();
1321 if (ospf->distance_intra)
1322 if (or->path_type == OSPF_PATH_INTRA_AREA)
1323 return ospf->distance_intra;
1325 if (ospf->distance_inter)
1326 if (or->path_type == OSPF_PATH_INTER_AREA)
1327 return ospf->distance_inter;
1329 if (ospf->distance_external)
1330 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
1331 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1332 return ospf->distance_external;
1334 if (ospf->distance_all)
1335 return ospf->distance_all;
1341 ospf_zebra_connected (struct zclient *zclient)
1343 zclient_send_requests (zclient, VRF_DEFAULT);
1347 ospf_zebra_init (struct thread_master *master)
1349 /* Allocate zebra structure. */
1350 zclient = zclient_new (master);
1351 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
1352 zclient->zebra_connected = ospf_zebra_connected;
1353 zclient->router_id_update = ospf_router_id_update_zebra;
1354 zclient->interface_add = ospf_interface_add;
1355 zclient->interface_delete = ospf_interface_delete;
1356 zclient->interface_up = ospf_interface_state_up;
1357 zclient->interface_down = ospf_interface_state_down;
1358 zclient->interface_address_add = ospf_interface_address_add;
1359 zclient->interface_address_delete = ospf_interface_address_delete;
1360 zclient->interface_link_params = ospf_interface_link_params;
1362 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1363 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1365 access_list_add_hook (ospf_filter_update);
1366 access_list_delete_hook (ospf_filter_update);
1367 prefix_list_add_hook (ospf_prefix_list_update);
1368 prefix_list_delete_hook (ospf_prefix_list_update);