1 /* Redistribution Handler
 
   2  * Copyright (C) 1998 Kunihiro Ishiguro
 
   4  * This file is part of GNU Zebra.
 
   6  * GNU Zebra is free software; you can redistribute it and/or modify it
 
   7  * under the terms of the GNU General Public License as published by the
 
   8  * Free Software Foundation; either version 2, or (at your option) any
 
  11  * GNU Zebra is distributed in the hope that it will be useful, but
 
  12  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
  14  * General Public License for more details.
 
  16  * You should have received a copy of the GNU General Public License
 
  17  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 
  18  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
  35 #include "zebra/rib.h"
 
  36 #include "zebra/zserv.h"
 
  37 #include "zebra/redistribute.h"
 
  38 #include "zebra/debug.h"
 
  39 #include "zebra/router-id.h"
 
  41 /* master zebra server structure */
 
  42 extern struct zebra_t zebrad;
 
  45 zebra_check_addr (struct prefix *p)
 
  47   if (p->family == AF_INET)
 
  51       addr = p->u.prefix4.s_addr;
 
  54       if (IPV4_NET127 (addr)
 
  56           || IPV4_LINKLOCAL(addr))
 
  60   if (p->family == AF_INET6)
 
  62       if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
 
  64       if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
 
  67 #endif /* HAVE_IPV6 */
 
  72 is_default (struct prefix *p)
 
  74   if (p->family == AF_INET)
 
  75     if (p->u.prefix4.s_addr == 0 && p->prefixlen == 0)
 
  78 #if 0  /* IPv6 default separation is now pending until protocol daemon
 
  80   if (p->family == AF_INET6)
 
  81     if (IN6_IS_ADDR_UNSPECIFIED (&p->u.prefix6) && p->prefixlen == 0)
 
  84 #endif /* HAVE_IPV6 */
 
  89 zebra_redistribute_default (struct zserv *client, vrf_id_t vrf_id)
 
  92   struct route_table *table;
 
  93   struct route_node *rn;
 
  96   struct prefix_ipv6 p6;
 
  97 #endif /* HAVE_IPV6 */
 
 100   /* Lookup default route. */
 
 101   memset (&p, 0, sizeof (struct prefix_ipv4));
 
 105   table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
 
 108       rn = route_node_lookup (table, (struct prefix *)&p);
 
 111           RNODE_FOREACH_RIB (rn, newrib)
 
 112             if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
 
 113                 && newrib->distance != DISTANCE_INFINITY)
 
 114               zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
 
 115           route_unlock_node (rn);
 
 120   /* Lookup default route. */
 
 121   memset (&p6, 0, sizeof (struct prefix_ipv6));
 
 122   p6.family = AF_INET6;
 
 125   table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
 
 128       rn = route_node_lookup (table, (struct prefix *)&p6);
 
 131           RNODE_FOREACH_RIB (rn, newrib)
 
 132             if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
 
 133                 && newrib->distance != DISTANCE_INFINITY)
 
 134               zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
 
 135           route_unlock_node (rn);
 
 138 #endif /* HAVE_IPV6 */
 
 141 /* Redistribute routes. */
 
 143 zebra_redistribute (struct zserv *client, int type, vrf_id_t vrf_id)
 
 146   struct route_table *table;
 
 147   struct route_node *rn;
 
 149   table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
 
 151     for (rn = route_top (table); rn; rn = route_next (rn))
 
 152       RNODE_FOREACH_RIB (rn, newrib)
 
 154           if (IS_ZEBRA_DEBUG_EVENT)
 
 155             zlog_debug("%s: checking: selected=%d, type=%d, distance=%d, zebra_check_addr=%d",
 
 156                        __func__, CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED),
 
 157                        newrib->type, newrib->distance, zebra_check_addr (&rn->p));
 
 158           if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
 
 159               && newrib->type == type
 
 160               && newrib->distance != DISTANCE_INFINITY
 
 161               && zebra_check_addr (&rn->p))
 
 163               client->redist_v4_add_cnt++;
 
 164               zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
 
 169   table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
 
 171     for (rn = route_top (table); rn; rn = route_next (rn))
 
 172       RNODE_FOREACH_RIB (rn, newrib)
 
 173         if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
 
 174             && newrib->type == type 
 
 175             && newrib->distance != DISTANCE_INFINITY
 
 176             && zebra_check_addr (&rn->p))
 
 178             client->redist_v6_add_cnt++;
 
 179             zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
 
 181 #endif /* HAVE_IPV6 */
 
 185 redistribute_add (struct prefix *p, struct rib *rib, struct rib *rib_old)
 
 187   struct listnode *node, *nnode;
 
 188   struct zserv *client;
 
 190   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 192       if ((is_default (p) &&
 
 193            vrf_bitmap_check (client->redist_default, rib->vrf_id))
 
 194           || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
 
 196           if (p->family == AF_INET)
 
 198               client->redist_v4_add_cnt++;
 
 199               zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
 
 201           if (p->family == AF_INET6)
 
 203               client->redist_v6_add_cnt++;
 
 204               zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
 
 207       else if (rib_old && vrf_bitmap_check (client->redist[rib_old->type], 
 
 210           /* redistribute_add has implicit withdraw semantics, so there
 
 211            * may be an old route already redistributed that is being updated.
 
 213            * However, if the new route is of a type that is /not/ redistributed
 
 214            * to the client, then we must ensure the old route is explicitly
 
 217           if (p->family == AF_INET)
 
 218             zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib_old);
 
 219           if (p->family == AF_INET6)
 
 220             zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib_old);
 
 226 redistribute_delete (struct prefix *p, struct rib *rib)
 
 228   struct listnode *node, *nnode;
 
 229   struct zserv *client;
 
 231   /* Add DISTANCE_INFINITY check. */
 
 232   if (rib->distance == DISTANCE_INFINITY)
 
 235   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 237       if ((is_default (p) &&
 
 238            vrf_bitmap_check (client->redist_default, rib->vrf_id))
 
 239           || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
 
 241           if (p->family == AF_INET)
 
 242             zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
 
 244           if (p->family == AF_INET6)
 
 245             zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
 
 246 #endif /* HAVE_IPV6 */
 
 252 zebra_redistribute_add (int command, struct zserv *client, int length,
 
 257   type = stream_getc (client->ibuf);
 
 259   if (type == 0 || type >= ZEBRA_ROUTE_MAX)
 
 262   if (! vrf_bitmap_check (client->redist[type], vrf_id))
 
 264       vrf_bitmap_set (client->redist[type], vrf_id);
 
 265       zebra_redistribute (client, type, vrf_id);
 
 270 zebra_redistribute_delete (int command, struct zserv *client, int length,
 
 275   type = stream_getc (client->ibuf);
 
 277   if (type == 0 || type >= ZEBRA_ROUTE_MAX)
 
 280   vrf_bitmap_unset (client->redist[type], vrf_id);
 
 284 zebra_redistribute_default_add (int command, struct zserv *client, int length,
 
 287   vrf_bitmap_set (client->redist_default, vrf_id);
 
 288   zebra_redistribute_default (client, vrf_id);
 
 292 zebra_redistribute_default_delete (int command, struct zserv *client,
 
 293     int length, vrf_id_t vrf_id)
 
 295   vrf_bitmap_unset (client->redist_default, vrf_id);
 
 298 /* Interface up information. */
 
 300 zebra_interface_up_update (struct interface *ifp)
 
 302   struct listnode *node, *nnode;
 
 303   struct zserv *client;
 
 305   if (IS_ZEBRA_DEBUG_EVENT)
 
 306     zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
 
 308   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 311         zsend_interface_update (ZEBRA_INTERFACE_UP, client, ifp);
 
 312         zsend_interface_link_params (client, ifp);
 
 316 /* Interface down information. */
 
 318 zebra_interface_down_update (struct interface *ifp)
 
 320   struct listnode *node, *nnode;
 
 321   struct zserv *client;
 
 323   if (IS_ZEBRA_DEBUG_EVENT)
 
 324     zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
 
 326   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 328       zsend_interface_update (ZEBRA_INTERFACE_DOWN, client, ifp);
 
 332 /* Interface information update. */
 
 334 zebra_interface_add_update (struct interface *ifp)
 
 336   struct listnode *node, *nnode;
 
 337   struct zserv *client;
 
 339   if (IS_ZEBRA_DEBUG_EVENT)
 
 340     zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
 
 342   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 346         zsend_interface_add (client, ifp);
 
 347         zsend_interface_link_params (client, ifp);
 
 352 zebra_interface_delete_update (struct interface *ifp)
 
 354   struct listnode *node, *nnode;
 
 355   struct zserv *client;
 
 357   if (IS_ZEBRA_DEBUG_EVENT)
 
 358     zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
 
 360   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 364         zsend_interface_delete (client, ifp);
 
 368 /* Interface address addition. */
 
 370 zebra_interface_address_add_update (struct interface *ifp,
 
 371                                     struct connected *ifc)
 
 373   struct listnode *node, *nnode;
 
 374   struct zserv *client;
 
 377   if (IS_ZEBRA_DEBUG_EVENT)
 
 379       char buf[PREFIX_STRLEN];
 
 382       zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
 
 383                   prefix2str (p, buf, sizeof(buf)),
 
 387   if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
 
 388     zlog_warn("WARNING: advertising address to clients that is not yet usable.");
 
 390   router_id_add_address(ifc);
 
 392   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 393     if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
 
 395         client->connected_rt_add_cnt++;
 
 396         zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, ifp, ifc);
 
 400 /* Interface address deletion. */
 
 402 zebra_interface_address_delete_update (struct interface *ifp,
 
 403                                        struct connected *ifc)
 
 405   struct listnode *node, *nnode;
 
 406   struct zserv *client;
 
 409   if (IS_ZEBRA_DEBUG_EVENT)
 
 411       char buf[PREFIX_STRLEN];
 
 414       zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
 
 415                   prefix2str (p, buf, sizeof(buf)),
 
 419   router_id_del_address(ifc);
 
 421   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 422     if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
 
 424         client->connected_rt_del_cnt++;
 
 425         zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE, client, ifp, ifc);
 
 429 /* Interface parameters update */
 
 431 zebra_interface_parameters_update (struct interface *ifp)
 
 433   struct listnode *node, *nnode;
 
 434   struct zserv *client;
 
 436   if (IS_ZEBRA_DEBUG_EVENT)
 
 437     zlog_debug ("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s", ifp->name);
 
 439   for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
 
 441       zsend_interface_link_params (client, ifp);