paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Routing Information Base. |
| 2 | * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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 |
| 9 | * later version. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "prefix.h" |
| 25 | #include "table.h" |
| 26 | #include "memory.h" |
| 27 | #include "str.h" |
| 28 | #include "command.h" |
| 29 | #include "if.h" |
| 30 | #include "log.h" |
| 31 | #include "sockunion.h" |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 32 | #include "linklist.h" |
| 33 | #include "thread.h" |
| 34 | #include "workqueue.h" |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 35 | #include "prefix.h" |
| 36 | #include "routemap.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | #include "zebra/rib.h" |
| 39 | #include "zebra/rt.h" |
| 40 | #include "zebra/zserv.h" |
| 41 | #include "zebra/redistribute.h" |
| 42 | #include "zebra/debug.h" |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 43 | #include "zebra/zebra_fpm.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
| 45 | /* Default rtm_table for all clients */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 46 | extern struct zebra_t zebrad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | |
Paul Jakma | 457eb9a | 2006-07-27 19:59:58 +0000 | [diff] [blame] | 48 | /* Hold time for RIB process, should be very minimal. |
| 49 | * it is useful to able to set it otherwise for testing, hence exported |
| 50 | * as global here for test-rig code. |
| 51 | */ |
| 52 | int rib_process_hold_time = 10; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | /* Each route type's string and default distance value. */ |
Stephen Hemminger | d145bc0 | 2008-08-17 17:41:37 +0100 | [diff] [blame] | 55 | static const struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | { |
| 57 | int key; |
| 58 | int distance; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 59 | } route_info[ZEBRA_ROUTE_MAX] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | { |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 61 | [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0}, |
| 62 | [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0}, |
| 63 | [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0}, |
| 64 | [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1}, |
| 65 | [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120}, |
| 66 | [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120}, |
| 67 | [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110}, |
| 68 | [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110}, |
| 69 | [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115}, |
| 70 | [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */}, |
| 71 | [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 95}, |
David Lamparter | 7052f22 | 2009-08-27 00:28:28 +0200 | [diff] [blame] | 72 | /* no entry/default: 150 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 74 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | /* Vector for routing table. */ |
Stephen Hemminger | d145bc0 | 2008-08-17 17:41:37 +0100 | [diff] [blame] | 76 | static vector vrf_vector; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 78 | /* RPF lookup behaviour */ |
| 79 | static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG; |
| 80 | |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 81 | static void |
| 82 | _rnode_zlog(const char *_func, struct route_node *rn, int priority, |
| 83 | const char *msgfmt, ...) |
| 84 | { |
| 85 | char buf[INET6_ADDRSTRLEN + 4], *bptr; |
| 86 | char msgbuf[512]; |
| 87 | va_list ap; |
| 88 | |
| 89 | va_start(ap, msgfmt); |
| 90 | vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap); |
| 91 | va_end(ap); |
| 92 | |
| 93 | if (rn) |
| 94 | { |
David Lamparter | ab2ba61 | 2015-01-22 19:02:13 +0100 | [diff] [blame] | 95 | rib_table_info_t *info = rn->table->info; |
| 96 | |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 97 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
| 98 | bptr = buf + strlen(buf); |
David Lamparter | ab2ba61 | 2015-01-22 19:02:13 +0100 | [diff] [blame] | 99 | snprintf(bptr, buf + sizeof(buf) - bptr, "/%d%s", rn->p.prefixlen, |
| 100 | info->safi == SAFI_MULTICAST ? " (MRIB)" : ""); |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
| 104 | snprintf(buf, sizeof(buf), "{(route_node *) NULL}"); |
| 105 | } |
| 106 | |
| 107 | zlog (NULL, priority, "%s: %s: %s", _func, buf, msgbuf); |
| 108 | } |
| 109 | |
| 110 | #define rnode_debug(node, ...) \ |
| 111 | _rnode_zlog(__func__, node, LOG_DEBUG, __VA_ARGS__) |
| 112 | #define rnode_info(node, ...) \ |
| 113 | _rnode_zlog(__func__, node, LOG_INFO, __VA_ARGS__) |
| 114 | |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 115 | /* |
| 116 | * vrf_table_create |
| 117 | */ |
| 118 | static void |
| 119 | vrf_table_create (struct vrf *vrf, afi_t afi, safi_t safi) |
| 120 | { |
| 121 | rib_table_info_t *info; |
| 122 | struct route_table *table; |
| 123 | |
| 124 | assert (!vrf->table[afi][safi]); |
| 125 | |
| 126 | table = route_table_init (); |
| 127 | vrf->table[afi][safi] = table; |
| 128 | |
| 129 | info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info)); |
| 130 | info->vrf = vrf; |
| 131 | info->afi = afi; |
| 132 | info->safi = safi; |
| 133 | table->info = info; |
| 134 | } |
| 135 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | /* Allocate new VRF. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 137 | static struct vrf * |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 138 | vrf_alloc (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 139 | { |
| 140 | struct vrf *vrf; |
| 141 | |
| 142 | vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf)); |
| 143 | |
| 144 | /* Put name. */ |
| 145 | if (name) |
| 146 | vrf->name = XSTRDUP (MTYPE_VRF_NAME, name); |
| 147 | |
| 148 | /* Allocate routing table and static table. */ |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 149 | vrf_table_create (vrf, AFI_IP, SAFI_UNICAST); |
| 150 | vrf_table_create (vrf, AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 152 | vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 153 | vrf_table_create (vrf, AFI_IP, SAFI_MULTICAST); |
| 154 | vrf_table_create (vrf, AFI_IP6, SAFI_MULTICAST); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 155 | vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init (); |
| 156 | vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init (); |
| 157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | |
| 159 | return vrf; |
| 160 | } |
| 161 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | /* Lookup VRF by identifier. */ |
| 163 | struct vrf * |
| 164 | vrf_lookup (u_int32_t id) |
| 165 | { |
| 166 | return vector_lookup (vrf_vector, id); |
| 167 | } |
| 168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | /* Initialize VRF. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 170 | static void |
| 171 | vrf_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
| 173 | struct vrf *default_table; |
| 174 | |
| 175 | /* Allocate VRF vector. */ |
| 176 | vrf_vector = vector_init (1); |
| 177 | |
| 178 | /* Allocate default main table. */ |
| 179 | default_table = vrf_alloc ("Default-IP-Routing-Table"); |
| 180 | |
| 181 | /* Default table index must be 0. */ |
| 182 | vector_set_index (vrf_vector, 0, default_table); |
| 183 | } |
| 184 | |
| 185 | /* Lookup route table. */ |
| 186 | struct route_table * |
| 187 | vrf_table (afi_t afi, safi_t safi, u_int32_t id) |
| 188 | { |
| 189 | struct vrf *vrf; |
| 190 | |
| 191 | vrf = vrf_lookup (id); |
| 192 | if (! vrf) |
| 193 | return NULL; |
| 194 | |
Leonid Rosenboim | 9499bf2 | 2012-12-06 20:17:41 +0000 | [diff] [blame] | 195 | if( afi >= AFI_MAX || safi >= SAFI_MAX ) |
| 196 | return NULL; |
| 197 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | return vrf->table[afi][safi]; |
| 199 | } |
| 200 | |
| 201 | /* Lookup static route table. */ |
| 202 | struct route_table * |
| 203 | vrf_static_table (afi_t afi, safi_t safi, u_int32_t id) |
| 204 | { |
| 205 | struct vrf *vrf; |
| 206 | |
| 207 | vrf = vrf_lookup (id); |
| 208 | if (! vrf) |
| 209 | return NULL; |
| 210 | |
Leonid Rosenboim | 9499bf2 | 2012-12-06 20:17:41 +0000 | [diff] [blame] | 211 | if( afi >= AFI_MAX || safi >= SAFI_MAX ) |
| 212 | return NULL; |
| 213 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | return vrf->stable[afi][safi]; |
| 215 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 216 | |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 217 | /* |
| 218 | * nexthop_type_to_str |
| 219 | */ |
| 220 | const char * |
| 221 | nexthop_type_to_str (enum nexthop_types_t nh_type) |
| 222 | { |
| 223 | static const char *desc[] = { |
| 224 | "none", |
| 225 | "Directly connected", |
| 226 | "Interface route", |
| 227 | "IPv4 nexthop", |
| 228 | "IPv4 nexthop with ifindex", |
| 229 | "IPv4 nexthop with ifname", |
| 230 | "IPv6 nexthop", |
| 231 | "IPv6 nexthop with ifindex", |
| 232 | "IPv6 nexthop with ifname", |
| 233 | "Null0 nexthop", |
| 234 | }; |
| 235 | |
| 236 | if (nh_type >= ZEBRA_NUM_OF (desc)) |
| 237 | return "<Invalid nh type>"; |
| 238 | |
| 239 | return desc[nh_type]; |
| 240 | } |
| 241 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 242 | /* Add nexthop to the end of a nexthop list. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 243 | static void |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 244 | _nexthop_add (struct nexthop **target, struct nexthop *nexthop) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | { |
| 246 | struct nexthop *last; |
| 247 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 248 | for (last = *target; last && last->next; last = last->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | ; |
| 250 | if (last) |
| 251 | last->next = nexthop; |
| 252 | else |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 253 | *target = nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | nexthop->prev = last; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 255 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 257 | /* Add nexthop to the end of a rib node's nexthop list */ |
| 258 | static void |
| 259 | nexthop_add (struct rib *rib, struct nexthop *nexthop) |
| 260 | { |
| 261 | _nexthop_add(&rib->nexthop, nexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | rib->nexthop_num++; |
| 263 | } |
| 264 | |
| 265 | /* Delete specified nexthop from the list. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 266 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | nexthop_delete (struct rib *rib, struct nexthop *nexthop) |
| 268 | { |
| 269 | if (nexthop->next) |
| 270 | nexthop->next->prev = nexthop->prev; |
| 271 | if (nexthop->prev) |
| 272 | nexthop->prev->next = nexthop->next; |
| 273 | else |
| 274 | rib->nexthop = nexthop->next; |
| 275 | rib->nexthop_num--; |
| 276 | } |
| 277 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 278 | static void nexthops_free(struct nexthop *nexthop); |
| 279 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | /* Free nexthop. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 281 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | nexthop_free (struct nexthop *nexthop) |
| 283 | { |
paul | a4b7076 | 2003-05-16 17:19:48 +0000 | [diff] [blame] | 284 | if (nexthop->ifname) |
| 285 | XFREE (0, nexthop->ifname); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 286 | if (nexthop->resolved) |
| 287 | nexthops_free(nexthop->resolved); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | XFREE (MTYPE_NEXTHOP, nexthop); |
| 289 | } |
| 290 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 291 | /* Frees a list of nexthops */ |
| 292 | static void |
| 293 | nexthops_free (struct nexthop *nexthop) |
| 294 | { |
| 295 | struct nexthop *nh, *next; |
| 296 | |
| 297 | for (nh = nexthop; nh; nh = next) |
| 298 | { |
| 299 | next = nh->next; |
| 300 | nexthop_free (nh); |
| 301 | } |
| 302 | } |
| 303 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | struct nexthop * |
| 305 | nexthop_ifindex_add (struct rib *rib, unsigned int ifindex) |
| 306 | { |
| 307 | struct nexthop *nexthop; |
| 308 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 309 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | nexthop->type = NEXTHOP_TYPE_IFINDEX; |
| 311 | nexthop->ifindex = ifindex; |
| 312 | |
| 313 | nexthop_add (rib, nexthop); |
| 314 | |
| 315 | return nexthop; |
| 316 | } |
| 317 | |
| 318 | struct nexthop * |
| 319 | nexthop_ifname_add (struct rib *rib, char *ifname) |
| 320 | { |
| 321 | struct nexthop *nexthop; |
| 322 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 323 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | nexthop->type = NEXTHOP_TYPE_IFNAME; |
paul | a4b7076 | 2003-05-16 17:19:48 +0000 | [diff] [blame] | 325 | nexthop->ifname = XSTRDUP (0, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | |
| 327 | nexthop_add (rib, nexthop); |
| 328 | |
| 329 | return nexthop; |
| 330 | } |
| 331 | |
| 332 | struct nexthop * |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 333 | nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | { |
| 335 | struct nexthop *nexthop; |
| 336 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 337 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | nexthop->type = NEXTHOP_TYPE_IPV4; |
| 339 | nexthop->gate.ipv4 = *ipv4; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 340 | if (src) |
| 341 | nexthop->src.ipv4 = *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | |
| 343 | nexthop_add (rib, nexthop); |
| 344 | |
| 345 | return nexthop; |
| 346 | } |
| 347 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 348 | struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 350 | struct in_addr *src, unsigned int ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | { |
| 352 | struct nexthop *nexthop; |
| 353 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 354 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
| 356 | nexthop->gate.ipv4 = *ipv4; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 357 | if (src) |
| 358 | nexthop->src.ipv4 = *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | nexthop->ifindex = ifindex; |
| 360 | |
| 361 | nexthop_add (rib, nexthop); |
| 362 | |
| 363 | return nexthop; |
| 364 | } |
| 365 | |
| 366 | #ifdef HAVE_IPV6 |
| 367 | struct nexthop * |
| 368 | nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6) |
| 369 | { |
| 370 | struct nexthop *nexthop; |
| 371 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 372 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | nexthop->type = NEXTHOP_TYPE_IPV6; |
| 374 | nexthop->gate.ipv6 = *ipv6; |
| 375 | |
| 376 | nexthop_add (rib, nexthop); |
| 377 | |
| 378 | return nexthop; |
| 379 | } |
| 380 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 381 | static struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6, |
| 383 | char *ifname) |
| 384 | { |
| 385 | struct nexthop *nexthop; |
| 386 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 387 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME; |
| 389 | nexthop->gate.ipv6 = *ipv6; |
| 390 | nexthop->ifname = XSTRDUP (0, ifname); |
| 391 | |
| 392 | nexthop_add (rib, nexthop); |
| 393 | |
| 394 | return nexthop; |
| 395 | } |
| 396 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 397 | static struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6, |
| 399 | unsigned int ifindex) |
| 400 | { |
| 401 | struct nexthop *nexthop; |
| 402 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 403 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 404 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 405 | nexthop->gate.ipv6 = *ipv6; |
| 406 | nexthop->ifindex = ifindex; |
| 407 | |
| 408 | nexthop_add (rib, nexthop); |
| 409 | |
| 410 | return nexthop; |
| 411 | } |
| 412 | #endif /* HAVE_IPV6 */ |
| 413 | |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 414 | struct nexthop * |
| 415 | nexthop_blackhole_add (struct rib *rib) |
| 416 | { |
| 417 | struct nexthop *nexthop; |
| 418 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 419 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 420 | nexthop->type = NEXTHOP_TYPE_BLACKHOLE; |
| 421 | SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE); |
| 422 | |
| 423 | nexthop_add (rib, nexthop); |
| 424 | |
| 425 | return nexthop; |
| 426 | } |
| 427 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 428 | /* This method checks whether a recursive nexthop has at |
| 429 | * least one resolved nexthop in the fib. |
| 430 | */ |
| 431 | int |
| 432 | nexthop_has_fib_child(struct nexthop *nexthop) |
| 433 | { |
| 434 | struct nexthop *nh; |
| 435 | |
| 436 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 437 | return 0; |
| 438 | |
| 439 | for (nh = nexthop->resolved; nh; nh = nh->next) |
| 440 | if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB)) |
| 441 | return 1; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 447 | the route from FIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 448 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 449 | nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set, |
| 450 | struct route_node *top) |
| 451 | { |
| 452 | struct prefix_ipv4 p; |
| 453 | struct route_table *table; |
| 454 | struct route_node *rn; |
| 455 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 456 | int resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | struct nexthop *newhop; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 458 | struct nexthop *resolved_hop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | |
| 460 | if (nexthop->type == NEXTHOP_TYPE_IPV4) |
| 461 | nexthop->ifindex = 0; |
| 462 | |
| 463 | if (set) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 464 | { |
| 465 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 466 | nexthops_free(nexthop->resolved); |
| 467 | nexthop->resolved = NULL; |
| 468 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | |
| 470 | /* Make lookup prefix. */ |
| 471 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 472 | p.family = AF_INET; |
| 473 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 474 | p.prefix = nexthop->gate.ipv4; |
| 475 | |
| 476 | /* Lookup table. */ |
| 477 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 478 | if (! table) |
| 479 | return 0; |
| 480 | |
| 481 | rn = route_node_match (table, (struct prefix *) &p); |
| 482 | while (rn) |
| 483 | { |
| 484 | route_unlock_node (rn); |
| 485 | |
David Ward | a50c107 | 2009-12-03 15:34:39 +0300 | [diff] [blame] | 486 | /* If lookup self prefix return immediately. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | if (rn == top) |
| 488 | return 0; |
| 489 | |
| 490 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 491 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 492 | { |
| 493 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 494 | continue; |
| 495 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 496 | break; |
| 497 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | |
| 499 | /* If there is no selected route or matched route is EGP, go up |
| 500 | tree. */ |
| 501 | if (! match |
| 502 | || match->type == ZEBRA_ROUTE_BGP) |
| 503 | { |
| 504 | do { |
| 505 | rn = rn->parent; |
| 506 | } while (rn && rn->info == NULL); |
| 507 | if (rn) |
| 508 | route_lock_node (rn); |
| 509 | } |
| 510 | else |
| 511 | { |
Christian Franke | 48a53dc | 2013-07-05 15:35:38 +0000 | [diff] [blame] | 512 | /* If the longest prefix match for the nexthop yields |
| 513 | * a blackhole, mark it as inactive. */ |
| 514 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) |
| 515 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) |
| 516 | return 0; |
| 517 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 519 | { |
| 520 | /* Directly point connected route. */ |
| 521 | newhop = match->nexthop; |
| 522 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4) |
| 523 | nexthop->ifindex = newhop->ifindex; |
| 524 | |
| 525 | return 1; |
| 526 | } |
| 527 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 528 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 529 | resolved = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 530 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 531 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 532 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 533 | { |
| 534 | if (set) |
| 535 | { |
| 536 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 537 | |
| 538 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 539 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 540 | /* If the resolving route specifies a gateway, use it */ |
| 541 | if (newhop->type == NEXTHOP_TYPE_IPV4 |
| 542 | || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX |
| 543 | || newhop->type == NEXTHOP_TYPE_IPV4_IFNAME) |
| 544 | { |
| 545 | resolved_hop->type = newhop->type; |
| 546 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 547 | |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 548 | if (newhop->ifindex) |
| 549 | { |
| 550 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
| 551 | resolved_hop->ifindex = newhop->ifindex; |
| 552 | } |
| 553 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 554 | |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 555 | /* If the resolving route is an interface route, |
| 556 | * it means the gateway we are looking up is connected |
| 557 | * to that interface. (The actual network is _not_ onlink). |
| 558 | * Therefore, the resolved route should have the original |
| 559 | * gateway as nexthop as it is directly connected. |
| 560 | * |
| 561 | * On Linux, we have to set the onlink netlink flag because |
| 562 | * otherwise, the kernel won't accept the route. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 563 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 564 | || newhop->type == NEXTHOP_TYPE_IFNAME) |
| 565 | { |
| 566 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; |
| 567 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
| 568 | resolved_hop->gate.ipv4 = nexthop->gate.ipv4; |
| 569 | resolved_hop->ifindex = newhop->ifindex; |
| 570 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 571 | |
| 572 | _nexthop_add(&nexthop->resolved, resolved_hop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 573 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 574 | resolved = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 576 | return resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | } |
| 578 | else |
| 579 | { |
| 580 | return 0; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | #ifdef HAVE_IPV6 |
| 588 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 589 | the route from FIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 590 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 591 | nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set, |
| 592 | struct route_node *top) |
| 593 | { |
| 594 | struct prefix_ipv6 p; |
| 595 | struct route_table *table; |
| 596 | struct route_node *rn; |
| 597 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 598 | int resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 599 | struct nexthop *newhop; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 600 | struct nexthop *resolved_hop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 601 | |
| 602 | if (nexthop->type == NEXTHOP_TYPE_IPV6) |
| 603 | nexthop->ifindex = 0; |
| 604 | |
| 605 | if (set) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 606 | { |
| 607 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 608 | nexthops_free(nexthop->resolved); |
| 609 | nexthop->resolved = NULL; |
| 610 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | |
| 612 | /* Make lookup prefix. */ |
| 613 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 614 | p.family = AF_INET6; |
| 615 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 616 | p.prefix = nexthop->gate.ipv6; |
| 617 | |
| 618 | /* Lookup table. */ |
| 619 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 620 | if (! table) |
| 621 | return 0; |
| 622 | |
| 623 | rn = route_node_match (table, (struct prefix *) &p); |
| 624 | while (rn) |
| 625 | { |
| 626 | route_unlock_node (rn); |
| 627 | |
David Ward | a50c107 | 2009-12-03 15:34:39 +0300 | [diff] [blame] | 628 | /* If lookup self prefix return immediately. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 629 | if (rn == top) |
| 630 | return 0; |
| 631 | |
| 632 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 633 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 634 | { |
| 635 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 636 | continue; |
| 637 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 638 | break; |
| 639 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 640 | |
| 641 | /* If there is no selected route or matched route is EGP, go up |
| 642 | tree. */ |
| 643 | if (! match |
| 644 | || match->type == ZEBRA_ROUTE_BGP) |
| 645 | { |
| 646 | do { |
| 647 | rn = rn->parent; |
| 648 | } while (rn && rn->info == NULL); |
| 649 | if (rn) |
| 650 | route_lock_node (rn); |
| 651 | } |
| 652 | else |
| 653 | { |
Christian Franke | 48a53dc | 2013-07-05 15:35:38 +0000 | [diff] [blame] | 654 | /* If the longest prefix match for the nexthop yields |
| 655 | * a blackhole, mark it as inactive. */ |
| 656 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) |
| 657 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) |
| 658 | return 0; |
| 659 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 660 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 661 | { |
| 662 | /* Directly point connected route. */ |
| 663 | newhop = match->nexthop; |
| 664 | |
| 665 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6) |
| 666 | nexthop->ifindex = newhop->ifindex; |
| 667 | |
| 668 | return 1; |
| 669 | } |
| 670 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 671 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 672 | resolved = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 673 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 674 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 675 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 676 | { |
| 677 | if (set) |
| 678 | { |
| 679 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 680 | |
| 681 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 682 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 683 | /* See nexthop_active_ipv4 for a description how the |
| 684 | * resolved nexthop is constructed. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | if (newhop->type == NEXTHOP_TYPE_IPV6 |
| 686 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 687 | || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 688 | { |
| 689 | resolved_hop->type = newhop->type; |
| 690 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; |
| 691 | |
| 692 | if (newhop->ifindex) |
| 693 | { |
| 694 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 695 | resolved_hop->ifindex = newhop->ifindex; |
| 696 | } |
| 697 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 698 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
Christian Franke | c3e6b59 | 2013-07-05 15:35:40 +0000 | [diff] [blame] | 700 | || newhop->type == NEXTHOP_TYPE_IFNAME) |
| 701 | { |
| 702 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; |
| 703 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 704 | resolved_hop->gate.ipv6 = nexthop->gate.ipv6; |
| 705 | resolved_hop->ifindex = newhop->ifindex; |
| 706 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 707 | |
| 708 | _nexthop_add(&nexthop->resolved, resolved_hop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 710 | resolved = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 712 | return resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 713 | } |
| 714 | else |
| 715 | { |
| 716 | return 0; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | #endif /* HAVE_IPV6 */ |
| 723 | |
| 724 | struct rib * |
David Lamparter | 24480d4 | 2015-01-22 19:09:36 +0100 | [diff] [blame] | 725 | rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp, |
| 726 | struct route_node **rn_out) |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 727 | { |
| 728 | struct route_table *table; |
| 729 | struct route_node *rn; |
| 730 | struct rib *match; |
| 731 | struct nexthop *newhop, *tnewhop; |
| 732 | int recursing; |
| 733 | |
| 734 | /* Lookup table. */ |
| 735 | table = vrf_table (AFI_IP, safi, 0); |
| 736 | if (! table) |
| 737 | return 0; |
| 738 | |
| 739 | rn = route_node_match_ipv4 (table, &addr); |
| 740 | |
| 741 | while (rn) |
| 742 | { |
| 743 | route_unlock_node (rn); |
| 744 | |
| 745 | /* Pick up selected route. */ |
| 746 | RNODE_FOREACH_RIB (rn, match) |
| 747 | { |
| 748 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 749 | continue; |
| 750 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | /* If there is no selected route or matched route is EGP, go up |
| 755 | tree. */ |
Everton Marques | 83d7112 | 2014-09-19 16:39:34 -0300 | [diff] [blame] | 756 | if (!match || (skip_bgp && (match->type == ZEBRA_ROUTE_BGP))) |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 757 | { |
| 758 | do { |
| 759 | rn = rn->parent; |
| 760 | } while (rn && rn->info == NULL); |
| 761 | if (rn) |
| 762 | route_lock_node (rn); |
| 763 | } |
| 764 | else |
| 765 | { |
David Lamparter | 24480d4 | 2015-01-22 19:09:36 +0100 | [diff] [blame] | 766 | if (match->type != ZEBRA_ROUTE_CONNECT) |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 767 | { |
David Lamparter | 24480d4 | 2015-01-22 19:09:36 +0100 | [diff] [blame] | 768 | int found = 0; |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 769 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
| 770 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
David Lamparter | 24480d4 | 2015-01-22 19:09:36 +0100 | [diff] [blame] | 771 | { |
| 772 | found = 1; |
| 773 | break; |
| 774 | } |
| 775 | if (!found) |
| 776 | return NULL; |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 777 | } |
David Lamparter | 24480d4 | 2015-01-22 19:09:36 +0100 | [diff] [blame] | 778 | |
| 779 | if (rn_out) |
| 780 | *rn_out = rn; |
| 781 | return match; |
Everton Marques | 3dea178 | 2014-09-22 19:35:51 -0300 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | return NULL; |
| 785 | } |
| 786 | |
| 787 | struct rib * |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 788 | rib_match_ipv4_multicast (struct in_addr addr, struct route_node **rn_out) |
| 789 | { |
| 790 | struct rib *rib = NULL, *mrib = NULL, *urib = NULL; |
| 791 | struct route_node *m_rn = NULL, *u_rn = NULL; |
| 792 | int skip_bgp = 0; /* bool */ |
| 793 | |
| 794 | switch (ipv4_multicast_mode) |
| 795 | { |
| 796 | case MCAST_MRIB_ONLY: |
| 797 | return rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, rn_out); |
| 798 | case MCAST_URIB_ONLY: |
| 799 | return rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, rn_out); |
| 800 | case MCAST_NO_CONFIG: |
| 801 | case MCAST_MIX_MRIB_FIRST: |
| 802 | rib = mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn); |
| 803 | if (!mrib) |
| 804 | rib = urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn); |
| 805 | break; |
| 806 | case MCAST_MIX_DISTANCE: |
| 807 | mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn); |
| 808 | urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn); |
| 809 | if (mrib && urib) |
| 810 | rib = urib->distance < mrib->distance ? urib : mrib; |
| 811 | else if (mrib) |
| 812 | rib = mrib; |
| 813 | else if (urib) |
| 814 | rib = urib; |
| 815 | break; |
| 816 | case MCAST_MIX_PFXLEN: |
| 817 | mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn); |
| 818 | urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn); |
| 819 | if (mrib && urib) |
| 820 | rib = u_rn->p.prefixlen > m_rn->p.prefixlen ? urib : mrib; |
| 821 | else if (mrib) |
| 822 | rib = mrib; |
| 823 | else if (urib) |
| 824 | rib = urib; |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | if (rn_out) |
| 829 | *rn_out = (rib == mrib) ? m_rn : u_rn; |
| 830 | |
| 831 | if (IS_ZEBRA_DEBUG_RIB) |
| 832 | { |
| 833 | char buf[BUFSIZ]; |
| 834 | inet_ntop (AF_INET, &addr, buf, BUFSIZ); |
| 835 | |
| 836 | zlog_debug("%s: %s: found %s, using %s", |
| 837 | __func__, buf, |
| 838 | mrib ? (urib ? "MRIB+URIB" : "MRIB") : |
| 839 | urib ? "URIB" : "nothing", |
| 840 | rib == urib ? "URIB" : rib == mrib ? "MRIB" : "none"); |
| 841 | } |
| 842 | return rib; |
| 843 | } |
| 844 | |
| 845 | void |
| 846 | multicast_mode_ipv4_set (enum multicast_mode mode) |
| 847 | { |
| 848 | if (IS_ZEBRA_DEBUG_RIB) |
| 849 | zlog_debug("%s: multicast lookup mode set (%d)", __func__, mode); |
| 850 | ipv4_multicast_mode = mode; |
| 851 | } |
| 852 | |
| 853 | enum multicast_mode |
| 854 | multicast_mode_ipv4_get (void) |
| 855 | { |
| 856 | return ipv4_multicast_mode; |
| 857 | } |
| 858 | |
| 859 | struct rib * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 860 | rib_lookup_ipv4 (struct prefix_ipv4 *p) |
| 861 | { |
| 862 | struct route_table *table; |
| 863 | struct route_node *rn; |
| 864 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 865 | struct nexthop *nexthop, *tnexthop; |
| 866 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 867 | |
| 868 | /* Lookup table. */ |
| 869 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 870 | if (! table) |
| 871 | return 0; |
| 872 | |
| 873 | rn = route_node_lookup (table, (struct prefix *) p); |
| 874 | |
| 875 | /* No route for this prefix. */ |
| 876 | if (! rn) |
| 877 | return NULL; |
| 878 | |
| 879 | /* Unlock node. */ |
| 880 | route_unlock_node (rn); |
| 881 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 882 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 883 | { |
| 884 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 885 | continue; |
| 886 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 887 | break; |
| 888 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 889 | |
| 890 | if (! match || match->type == ZEBRA_ROUTE_BGP) |
| 891 | return NULL; |
| 892 | |
| 893 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 894 | return match; |
| 895 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 896 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 897 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 898 | return match; |
| 899 | |
| 900 | return NULL; |
| 901 | } |
| 902 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 903 | /* |
| 904 | * This clone function, unlike its original rib_lookup_ipv4(), checks |
| 905 | * if specified IPv4 route record (prefix/mask -> gate) exists in |
| 906 | * the whole RIB and has ZEBRA_FLAG_SELECTED set. |
| 907 | * |
| 908 | * Return values: |
| 909 | * -1: error |
| 910 | * 0: exact match found |
| 911 | * 1: a match was found with a different gate |
| 912 | * 2: connected route found |
| 913 | * 3: no matches found |
| 914 | */ |
| 915 | int |
| 916 | rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate) |
| 917 | { |
| 918 | struct route_table *table; |
| 919 | struct route_node *rn; |
| 920 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 921 | struct nexthop *nexthop, *tnexthop; |
| 922 | int recursing; |
| 923 | int nexthops_active; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 924 | |
| 925 | /* Lookup table. */ |
| 926 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 927 | if (! table) |
| 928 | return ZEBRA_RIB_LOOKUP_ERROR; |
| 929 | |
| 930 | /* Scan the RIB table for exactly matching RIB entry. */ |
| 931 | rn = route_node_lookup (table, (struct prefix *) p); |
| 932 | |
| 933 | /* No route for this prefix. */ |
| 934 | if (! rn) |
| 935 | return ZEBRA_RIB_NOTFOUND; |
| 936 | |
| 937 | /* Unlock node. */ |
| 938 | route_unlock_node (rn); |
| 939 | |
| 940 | /* Find out if a "selected" RR for the discovered RIB entry exists ever. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 941 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 942 | { |
| 943 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 944 | continue; |
| 945 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 946 | break; |
| 947 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 948 | |
| 949 | /* None such found :( */ |
| 950 | if (!match) |
| 951 | return ZEBRA_RIB_NOTFOUND; |
| 952 | |
| 953 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 954 | return ZEBRA_RIB_FOUND_CONNECTED; |
| 955 | |
| 956 | /* Ok, we have a cood candidate, let's check it's nexthop list... */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 957 | nexthops_active = 0; |
| 958 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 959 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 960 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 961 | nexthops_active = 1; |
| 962 | if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate)) |
| 963 | return ZEBRA_RIB_FOUND_EXACT; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 964 | if (IS_ZEBRA_DEBUG_RIB) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 965 | { |
| 966 | char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN]; |
| 967 | inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN); |
| 968 | inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN); |
| 969 | zlog_debug ("%s: qgate == %s, %s == %s", __func__, |
| 970 | qgate_buf, recursing ? "rgate" : "gate", gate_buf); |
| 971 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 972 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 973 | |
| 974 | if (nexthops_active) |
| 975 | return ZEBRA_RIB_FOUND_NOGATE; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 976 | |
| 977 | return ZEBRA_RIB_NOTFOUND; |
| 978 | } |
| 979 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | #ifdef HAVE_IPV6 |
| 981 | struct rib * |
| 982 | rib_match_ipv6 (struct in6_addr *addr) |
| 983 | { |
| 984 | struct prefix_ipv6 p; |
| 985 | struct route_table *table; |
| 986 | struct route_node *rn; |
| 987 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 988 | struct nexthop *newhop, *tnewhop; |
| 989 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 990 | |
| 991 | /* Lookup table. */ |
| 992 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 993 | if (! table) |
| 994 | return 0; |
| 995 | |
| 996 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 997 | p.family = AF_INET6; |
| 998 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 999 | IPV6_ADDR_COPY (&p.prefix, addr); |
| 1000 | |
| 1001 | rn = route_node_match (table, (struct prefix *) &p); |
| 1002 | |
| 1003 | while (rn) |
| 1004 | { |
| 1005 | route_unlock_node (rn); |
| 1006 | |
| 1007 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1008 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 1009 | { |
| 1010 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 1011 | continue; |
| 1012 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 1013 | break; |
| 1014 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1015 | |
| 1016 | /* If there is no selected route or matched route is EGP, go up |
| 1017 | tree. */ |
| 1018 | if (! match |
| 1019 | || match->type == ZEBRA_ROUTE_BGP) |
| 1020 | { |
| 1021 | do { |
| 1022 | rn = rn->parent; |
| 1023 | } while (rn && rn->info == NULL); |
| 1024 | if (rn) |
| 1025 | route_lock_node (rn); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 1030 | /* Directly point connected route. */ |
| 1031 | return match; |
| 1032 | else |
| 1033 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1034 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1035 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
| 1036 | return match; |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | #endif /* HAVE_IPV6 */ |
| 1044 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1045 | #define RIB_SYSTEM_ROUTE(R) \ |
| 1046 | ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT) |
| 1047 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1048 | /* This function verifies reachability of one given nexthop, which can be |
| 1049 | * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored |
| 1050 | * in nexthop->flags field. If the 4th parameter, 'set', is non-zero, |
| 1051 | * nexthop->ifindex will be updated appropriately as well. |
| 1052 | * An existing route map can turn (otherwise active) nexthop into inactive, but |
| 1053 | * not vice versa. |
| 1054 | * |
| 1055 | * The return value is the final value of 'ACTIVE' flag. |
| 1056 | */ |
| 1057 | |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1058 | static unsigned |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1059 | nexthop_active_check (struct route_node *rn, struct rib *rib, |
| 1060 | struct nexthop *nexthop, int set) |
| 1061 | { |
Christian Franke | f3a1732 | 2013-07-05 15:35:41 +0000 | [diff] [blame] | 1062 | rib_table_info_t *info = rn->table->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1063 | struct interface *ifp; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1064 | route_map_result_t ret = RMAP_MATCH; |
| 1065 | extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1]; |
| 1066 | struct route_map *rmap; |
| 1067 | int family; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1068 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1069 | family = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1070 | switch (nexthop->type) |
| 1071 | { |
| 1072 | case NEXTHOP_TYPE_IFINDEX: |
| 1073 | ifp = if_lookup_by_index (nexthop->ifindex); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 1074 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1075 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1076 | else |
| 1077 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1078 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1079 | case NEXTHOP_TYPE_IPV6_IFNAME: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1080 | family = AFI_IP6; |
| 1081 | case NEXTHOP_TYPE_IFNAME: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | ifp = if_lookup_by_name (nexthop->ifname); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 1083 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | { |
| 1085 | if (set) |
| 1086 | nexthop->ifindex = ifp->ifindex; |
| 1087 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | if (set) |
| 1092 | nexthop->ifindex = 0; |
| 1093 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1094 | } |
| 1095 | break; |
| 1096 | case NEXTHOP_TYPE_IPV4: |
| 1097 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1098 | family = AFI_IP; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1099 | if (nexthop_active_ipv4 (rib, nexthop, set, rn)) |
| 1100 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1101 | else |
| 1102 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1103 | break; |
| 1104 | #ifdef HAVE_IPV6 |
| 1105 | case NEXTHOP_TYPE_IPV6: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1106 | family = AFI_IP6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1107 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 1108 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1109 | else |
| 1110 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1111 | break; |
| 1112 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1113 | family = AFI_IP6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6)) |
| 1115 | { |
| 1116 | ifp = if_lookup_by_index (nexthop->ifindex); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 1117 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1119 | else |
| 1120 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 1125 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1126 | else |
| 1127 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1128 | } |
| 1129 | break; |
| 1130 | #endif /* HAVE_IPV6 */ |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1131 | case NEXTHOP_TYPE_BLACKHOLE: |
| 1132 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1133 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | default: |
| 1135 | break; |
| 1136 | } |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1137 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1138 | return 0; |
| 1139 | |
Christian Franke | f3a1732 | 2013-07-05 15:35:41 +0000 | [diff] [blame] | 1140 | /* XXX: What exactly do those checks do? Do we support |
| 1141 | * e.g. IPv4 routes with IPv6 nexthops or vice versa? */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1142 | if (RIB_SYSTEM_ROUTE(rib) || |
| 1143 | (family == AFI_IP && rn->p.family != AF_INET) || |
| 1144 | (family == AFI_IP6 && rn->p.family != AF_INET6)) |
| 1145 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1146 | |
Christian Franke | f3a1732 | 2013-07-05 15:35:41 +0000 | [diff] [blame] | 1147 | /* The original code didn't determine the family correctly |
| 1148 | * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi |
| 1149 | * from the rib_table_info in those cases. |
| 1150 | * Possibly it may be better to use only the rib_table_info |
| 1151 | * in every case. |
| 1152 | */ |
| 1153 | if (!family) |
| 1154 | family = info->afi; |
| 1155 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1156 | rmap = 0; |
| 1157 | if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX && |
| 1158 | proto_rm[family][rib->type]) |
| 1159 | rmap = route_map_lookup_by_name (proto_rm[family][rib->type]); |
| 1160 | if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX]) |
| 1161 | rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]); |
| 1162 | if (rmap) { |
| 1163 | ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop); |
| 1164 | } |
| 1165 | |
| 1166 | if (ret == RMAP_DENYMATCH) |
| 1167 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1168 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1169 | } |
| 1170 | |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1171 | /* Iterate over all nexthops of the given RIB entry and refresh their |
| 1172 | * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any |
| 1173 | * nexthop is found to toggle the ACTIVE flag, the whole rib structure |
| 1174 | * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is |
| 1175 | * transparently passed to nexthop_active_check(). |
| 1176 | * |
| 1177 | * Return value is the new number of active nexthops. |
| 1178 | */ |
| 1179 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1180 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1181 | nexthop_active_update (struct route_node *rn, struct rib *rib, int set) |
| 1182 | { |
| 1183 | struct nexthop *nexthop; |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1184 | unsigned int prev_active, prev_index, new_active; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1185 | |
| 1186 | rib->nexthop_active_num = 0; |
| 1187 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 1188 | |
| 1189 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1190 | { |
| 1191 | prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
Joakim Tjernlund | c3a5606 | 2009-06-24 19:15:36 +0200 | [diff] [blame] | 1192 | prev_index = nexthop->ifindex; |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1193 | if ((new_active = nexthop_active_check (rn, rib, nexthop, set))) |
| 1194 | rib->nexthop_active_num++; |
Joakim Tjernlund | c3a5606 | 2009-06-24 19:15:36 +0200 | [diff] [blame] | 1195 | if (prev_active != new_active || |
| 1196 | prev_index != nexthop->ifindex) |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1197 | SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 1198 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1199 | return rib->nexthop_active_num; |
| 1200 | } |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 1201 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1202 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1203 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1204 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1205 | rib_install_kernel (struct route_node *rn, struct rib *rib) |
| 1206 | { |
| 1207 | int ret = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1208 | struct nexthop *nexthop, *tnexthop; |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1209 | rib_table_info_t *info = rn->table->info; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1210 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1211 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1212 | if (info->safi != SAFI_UNICAST) |
| 1213 | { |
| 1214 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1215 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1216 | return; |
| 1217 | } |
| 1218 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1219 | /* |
| 1220 | * Make sure we update the FPM any time we send new information to |
| 1221 | * the kernel. |
| 1222 | */ |
| 1223 | zfpm_trigger_update (rn, "installing in kernel"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | switch (PREFIX_FAMILY (&rn->p)) |
| 1225 | { |
| 1226 | case AF_INET: |
| 1227 | ret = kernel_add_ipv4 (&rn->p, rib); |
| 1228 | break; |
| 1229 | #ifdef HAVE_IPV6 |
| 1230 | case AF_INET6: |
| 1231 | ret = kernel_add_ipv6 (&rn->p, rib); |
| 1232 | break; |
| 1233 | #endif /* HAVE_IPV6 */ |
| 1234 | } |
| 1235 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1236 | /* This condition is never met, if we are using rt_socket.c */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1237 | if (ret < 0) |
| 1238 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1239 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1240 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /* Uninstall the route from kernel. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1245 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1246 | rib_uninstall_kernel (struct route_node *rn, struct rib *rib) |
| 1247 | { |
| 1248 | int ret = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1249 | struct nexthop *nexthop, *tnexthop; |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1250 | rib_table_info_t *info = rn->table->info; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1251 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1253 | if (info->safi != SAFI_UNICAST) |
| 1254 | { |
| 1255 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1256 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1257 | return ret; |
| 1258 | } |
| 1259 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1260 | /* |
| 1261 | * Make sure we update the FPM any time we send new information to |
| 1262 | * the kernel. |
| 1263 | */ |
| 1264 | zfpm_trigger_update (rn, "uninstalling from kernel"); |
| 1265 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1266 | switch (PREFIX_FAMILY (&rn->p)) |
| 1267 | { |
| 1268 | case AF_INET: |
| 1269 | ret = kernel_delete_ipv4 (&rn->p, rib); |
| 1270 | break; |
| 1271 | #ifdef HAVE_IPV6 |
| 1272 | case AF_INET6: |
| 1273 | ret = kernel_delete_ipv6 (&rn->p, rib); |
| 1274 | break; |
| 1275 | #endif /* HAVE_IPV6 */ |
| 1276 | } |
| 1277 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1278 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1279 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | /* Uninstall the route from kernel. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1285 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1286 | rib_uninstall (struct route_node *rn, struct rib *rib) |
| 1287 | { |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1288 | rib_table_info_t *info = rn->table->info; |
| 1289 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1290 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 1291 | { |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1292 | if (info->safi == SAFI_UNICAST) |
| 1293 | zfpm_trigger_update (rn, "rib_uninstall"); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1294 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1295 | redistribute_delete (&rn->p, rib); |
| 1296 | if (! RIB_SYSTEM_ROUTE (rib)) |
| 1297 | rib_uninstall_kernel (rn, rib); |
| 1298 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1302 | static void rib_unlink (struct route_node *, struct rib *); |
| 1303 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1304 | /* |
| 1305 | * rib_can_delete_dest |
| 1306 | * |
| 1307 | * Returns TRUE if the given dest can be deleted from the table. |
| 1308 | */ |
| 1309 | static int |
| 1310 | rib_can_delete_dest (rib_dest_t *dest) |
| 1311 | { |
| 1312 | if (dest->routes) |
| 1313 | { |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | * Don't delete the dest if we have to update the FPM about this |
| 1319 | * prefix. |
| 1320 | */ |
| 1321 | if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) || |
| 1322 | CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM)) |
| 1323 | return 0; |
| 1324 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1325 | return 1; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * rib_gc_dest |
| 1330 | * |
| 1331 | * Garbage collect the rib dest corresponding to the given route node |
| 1332 | * if appropriate. |
| 1333 | * |
| 1334 | * Returns TRUE if the dest was deleted, FALSE otherwise. |
| 1335 | */ |
| 1336 | int |
| 1337 | rib_gc_dest (struct route_node *rn) |
| 1338 | { |
| 1339 | rib_dest_t *dest; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1340 | |
| 1341 | dest = rib_dest_from_rnode (rn); |
| 1342 | if (!dest) |
| 1343 | return 0; |
| 1344 | |
| 1345 | if (!rib_can_delete_dest (dest)) |
| 1346 | return 0; |
| 1347 | |
| 1348 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1349 | rnode_debug (rn, "removing dest from table"); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1350 | |
| 1351 | dest->rnode = NULL; |
| 1352 | XFREE (MTYPE_RIB_DEST, dest); |
| 1353 | rn->info = NULL; |
| 1354 | |
| 1355 | /* |
| 1356 | * Release the one reference that we keep on the route node. |
| 1357 | */ |
| 1358 | route_unlock_node (rn); |
| 1359 | return 1; |
| 1360 | } |
| 1361 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | /* Core function for processing routing information base. */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1363 | static void |
| 1364 | rib_process (struct route_node *rn) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | { |
| 1366 | struct rib *rib; |
| 1367 | struct rib *next; |
| 1368 | struct rib *fib = NULL; |
| 1369 | struct rib *select = NULL; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1370 | struct rib *del = NULL; |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1371 | int installed = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1372 | struct nexthop *nexthop = NULL, *tnexthop; |
| 1373 | int recursing; |
Balaji | 9511633 | 2014-10-23 15:25:25 +0000 | [diff] [blame] | 1374 | rib_table_info_t *info; |
| 1375 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1376 | assert (rn); |
Balaji | 9511633 | 2014-10-23 15:25:25 +0000 | [diff] [blame] | 1377 | |
| 1378 | info = rn->table->info; |
| 1379 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1380 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1381 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1382 | /* Currently installed rib. */ |
| 1383 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1384 | { |
| 1385 | assert (fib == NULL); |
| 1386 | fib = rib; |
| 1387 | } |
| 1388 | |
| 1389 | /* Unlock removed routes, so they'll be freed, bar the FIB entry, |
| 1390 | * which we need to do do further work with below. |
| 1391 | */ |
| 1392 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1393 | { |
| 1394 | if (rib != fib) |
| 1395 | { |
| 1396 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1397 | rnode_debug (rn, "rn %p, removing rib %p", rn, rib); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1398 | rib_unlink (rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1399 | } |
| 1400 | else |
| 1401 | del = rib; |
| 1402 | |
| 1403 | continue; |
| 1404 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1405 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1406 | /* Skip unreachable nexthop. */ |
| 1407 | if (! nexthop_active_update (rn, rib, 0)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1408 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1409 | |
| 1410 | /* Infinit distance. */ |
| 1411 | if (rib->distance == DISTANCE_INFINITY) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1412 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1413 | |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1414 | /* Newly selected rib, the common case. */ |
| 1415 | if (!select) |
| 1416 | { |
| 1417 | select = rib; |
| 1418 | continue; |
| 1419 | } |
| 1420 | |
| 1421 | /* filter route selection in following order: |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1422 | * - connected beats other types |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1423 | * - lower distance beats higher |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1424 | * - lower metric beats higher for equal distance |
| 1425 | * - last, hence oldest, route wins tie break. |
| 1426 | */ |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1427 | |
| 1428 | /* Connected routes. Pick the last connected |
| 1429 | * route of the set of lowest metric connected routes. |
| 1430 | */ |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1431 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1432 | { |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1433 | if (select->type != ZEBRA_ROUTE_CONNECT |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1434 | || rib->metric <= select->metric) |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1435 | select = rib; |
| 1436 | continue; |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1437 | } |
| 1438 | else if (select->type == ZEBRA_ROUTE_CONNECT) |
| 1439 | continue; |
| 1440 | |
| 1441 | /* higher distance loses */ |
| 1442 | if (rib->distance > select->distance) |
| 1443 | continue; |
| 1444 | |
| 1445 | /* lower wins */ |
| 1446 | if (rib->distance < select->distance) |
| 1447 | { |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1448 | select = rib; |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1449 | continue; |
| 1450 | } |
| 1451 | |
| 1452 | /* metric tie-breaks equal distance */ |
| 1453 | if (rib->metric <= select->metric) |
| 1454 | select = rib; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1455 | } /* RNODE_FOREACH_RIB_SAFE */ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1456 | |
| 1457 | /* After the cycle is finished, the following pointers will be set: |
| 1458 | * select --- the winner RIB entry, if any was found, otherwise NULL |
| 1459 | * fib --- the SELECTED RIB entry, if any, otherwise NULL |
| 1460 | * del --- equal to fib, if fib is queued for deletion, NULL otherwise |
| 1461 | * rib --- NULL |
| 1462 | */ |
| 1463 | |
| 1464 | /* Same RIB entry is selected. Update FIB and finish. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1465 | if (select && select == fib) |
| 1466 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1467 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1468 | rnode_debug (rn, "Updating existing route, select %p, fib %p", |
| 1469 | select, fib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1470 | if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1471 | { |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1472 | if (info->safi == SAFI_UNICAST) |
| 1473 | zfpm_trigger_update (rn, "updating existing route"); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1474 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1475 | redistribute_delete (&rn->p, select); |
| 1476 | if (! RIB_SYSTEM_ROUTE (select)) |
| 1477 | rib_uninstall_kernel (rn, select); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1478 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1479 | /* Set real nexthop. */ |
| 1480 | nexthop_active_update (rn, select, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1481 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1482 | if (! RIB_SYSTEM_ROUTE (select)) |
| 1483 | rib_install_kernel (rn, select); |
| 1484 | redistribute_add (&rn->p, select); |
| 1485 | } |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1486 | else if (! RIB_SYSTEM_ROUTE (select)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1487 | { |
| 1488 | /* Housekeeping code to deal with |
| 1489 | race conditions in kernel with linux |
| 1490 | netlink reporting interface up before IPv4 or IPv6 protocol |
| 1491 | is ready to add routes. |
| 1492 | This makes sure the routes are IN the kernel. |
| 1493 | */ |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1494 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1495 | for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing)) |
Denis Ovsienko | a3aaf5b | 2007-10-04 10:49:21 +0000 | [diff] [blame] | 1496 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1497 | { |
Denis Ovsienko | a3aaf5b | 2007-10-04 10:49:21 +0000 | [diff] [blame] | 1498 | installed = 1; |
| 1499 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1500 | } |
| 1501 | if (! installed) |
| 1502 | rib_install_kernel (rn, select); |
| 1503 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1504 | goto end; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1507 | /* At this point we either haven't found the best RIB entry or it is |
| 1508 | * different from what we currently intend to flag with SELECTED. In both |
| 1509 | * cases, if a RIB block is present in FIB, it should be withdrawn. |
| 1510 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1511 | if (fib) |
| 1512 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1513 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1514 | rnode_debug (rn, "Removing existing route, fib %p", fib); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1515 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1516 | if (info->safi == SAFI_UNICAST) |
| 1517 | zfpm_trigger_update (rn, "removing existing route"); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1518 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1519 | redistribute_delete (&rn->p, fib); |
| 1520 | if (! RIB_SYSTEM_ROUTE (fib)) |
| 1521 | rib_uninstall_kernel (rn, fib); |
| 1522 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 1523 | |
| 1524 | /* Set real nexthop. */ |
| 1525 | nexthop_active_update (rn, fib, 1); |
| 1526 | } |
| 1527 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1528 | /* Regardless of some RIB entry being SELECTED or not before, now we can |
| 1529 | * tell, that if a new winner exists, FIB is still not updated with this |
| 1530 | * data, but ready to be. |
| 1531 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1532 | if (select) |
| 1533 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1534 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1535 | rnode_debug (rn, "Adding route, select %p", select); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1536 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1537 | if (info->safi == SAFI_UNICAST) |
| 1538 | zfpm_trigger_update (rn, "new route selected"); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1539 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1540 | /* Set real nexthop. */ |
| 1541 | nexthop_active_update (rn, select, 1); |
| 1542 | |
| 1543 | if (! RIB_SYSTEM_ROUTE (select)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1544 | rib_install_kernel (rn, select); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1545 | SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED); |
| 1546 | redistribute_add (&rn->p, select); |
| 1547 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1548 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1549 | /* FIB route was removed, should be deleted */ |
| 1550 | if (del) |
| 1551 | { |
| 1552 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1553 | rnode_debug (rn, "Deleting fib %p, rn %p", del, rn); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1554 | rib_unlink (rn, del); |
| 1555 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1556 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1557 | end: |
| 1558 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1559 | rnode_debug (rn, "rn %p dequeued", rn); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* |
| 1562 | * Check if the dest can be deleted now. |
| 1563 | */ |
| 1564 | rib_gc_dest (rn); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1567 | /* Take a list of route_node structs and return 1, if there was a record |
| 1568 | * picked from it and processed by rib_process(). Don't process more, |
| 1569 | * than one RN record; operate only in the specified sub-queue. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1570 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1571 | static unsigned int |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1572 | process_subq (struct list * subq, u_char qindex) |
| 1573 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1574 | struct listnode *lnode = listhead (subq); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1575 | struct route_node *rnode; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1576 | |
| 1577 | if (!lnode) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1578 | return 0; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1579 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1580 | rnode = listgetdata (lnode); |
| 1581 | rib_process (rnode); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1582 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1583 | if (rnode->info) |
| 1584 | UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex)); |
| 1585 | |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1586 | #if 0 |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1587 | else |
| 1588 | { |
| 1589 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
| 1590 | __func__, rnode, rnode->lock); |
| 1591 | zlog_backtrace(LOG_DEBUG); |
| 1592 | } |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1593 | #endif |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1594 | route_unlock_node (rnode); |
| 1595 | list_delete_node (subq, lnode); |
| 1596 | return 1; |
| 1597 | } |
| 1598 | |
| 1599 | /* Dispatch the meta queue by picking, processing and unlocking the next RN from |
| 1600 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data |
| 1601 | * is pointed to the meta queue structure. |
| 1602 | */ |
| 1603 | static wq_item_status |
| 1604 | meta_queue_process (struct work_queue *dummy, void *data) |
| 1605 | { |
| 1606 | struct meta_queue * mq = data; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1607 | unsigned i; |
| 1608 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1609 | for (i = 0; i < MQ_SIZE; i++) |
| 1610 | if (process_subq (mq->subq[i], i)) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1611 | { |
| 1612 | mq->size--; |
| 1613 | break; |
| 1614 | } |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1615 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; |
| 1616 | } |
| 1617 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1618 | /* |
| 1619 | * Map from rib types to queue type (priority) in meta queue |
| 1620 | */ |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1621 | static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = { |
| 1622 | [ZEBRA_ROUTE_SYSTEM] = 4, |
| 1623 | [ZEBRA_ROUTE_KERNEL] = 0, |
| 1624 | [ZEBRA_ROUTE_CONNECT] = 0, |
| 1625 | [ZEBRA_ROUTE_STATIC] = 1, |
| 1626 | [ZEBRA_ROUTE_RIP] = 2, |
| 1627 | [ZEBRA_ROUTE_RIPNG] = 2, |
| 1628 | [ZEBRA_ROUTE_OSPF] = 2, |
| 1629 | [ZEBRA_ROUTE_OSPF6] = 2, |
| 1630 | [ZEBRA_ROUTE_ISIS] = 2, |
| 1631 | [ZEBRA_ROUTE_BGP] = 3, |
| 1632 | [ZEBRA_ROUTE_HSLS] = 4, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1633 | [ZEBRA_ROUTE_BABEL] = 2, |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1634 | }; |
| 1635 | |
| 1636 | /* Look into the RN and queue it into one or more priority queues, |
| 1637 | * increasing the size for each data push done. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1638 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1639 | static void |
| 1640 | rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1641 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1642 | struct rib *rib; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1643 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1644 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1645 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1646 | u_char qindex = meta_queue_map[rib->type]; |
| 1647 | |
| 1648 | /* Invariant: at this point we always have rn->info set. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1649 | if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags, |
| 1650 | RIB_ROUTE_QUEUED (qindex))) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1651 | { |
| 1652 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1653 | rnode_debug (rn, "rn %p is already queued in sub-queue %u", |
| 1654 | rn, qindex); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1655 | continue; |
| 1656 | } |
| 1657 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1658 | SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex)); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1659 | listnode_add (mq->subq[qindex], rn); |
| 1660 | route_lock_node (rn); |
| 1661 | mq->size++; |
| 1662 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1663 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1664 | rnode_debug (rn, "queued rn %p into sub-queue %u", |
| 1665 | rn, qindex); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1666 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1669 | /* Add route_node to work queue and schedule processing */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1670 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1671 | rib_queue_add (struct zebra_t *zebra, struct route_node *rn) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1672 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1673 | assert (zebra && rn); |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1674 | |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1675 | /* Pointless to queue a route_node with no RIB entries to add or remove */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1676 | if (!rnode_to_ribs (rn)) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1677 | { |
| 1678 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
| 1679 | __func__, rn, rn->lock); |
| 1680 | zlog_backtrace(LOG_DEBUG); |
| 1681 | return; |
| 1682 | } |
| 1683 | |
| 1684 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1685 | rnode_info (rn, "work queue added"); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1686 | |
| 1687 | assert (zebra); |
| 1688 | |
| 1689 | if (zebra->ribq == NULL) |
| 1690 | { |
| 1691 | zlog_err ("%s: work_queue does not exist!", __func__); |
| 1692 | return; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1693 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1694 | |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1695 | /* |
| 1696 | * The RIB queue should normally be either empty or holding the only |
| 1697 | * work_queue_item element. In the latter case this element would |
| 1698 | * hold a pointer to the meta queue structure, which must be used to |
| 1699 | * actually queue the route nodes to process. So create the MQ |
| 1700 | * holder, if necessary, then push the work into it in any case. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1701 | * This semantics was introduced after 0.99.9 release. |
| 1702 | */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1703 | if (!zebra->ribq->items->count) |
| 1704 | work_queue_add (zebra->ribq, zebra->mq); |
| 1705 | |
| 1706 | rib_meta_queue_add (zebra->mq, rn); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1707 | |
| 1708 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1709 | rnode_debug (rn, "rn %p queued", rn); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1710 | |
| 1711 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1714 | /* Create new meta queue. |
| 1715 | A destructor function doesn't seem to be necessary here. |
| 1716 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1717 | static struct meta_queue * |
| 1718 | meta_queue_new (void) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1719 | { |
| 1720 | struct meta_queue *new; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1721 | unsigned i; |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1722 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1723 | new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue)); |
| 1724 | assert(new); |
| 1725 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1726 | for (i = 0; i < MQ_SIZE; i++) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1727 | { |
| 1728 | new->subq[i] = list_new (); |
| 1729 | assert(new->subq[i]); |
| 1730 | } |
| 1731 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1732 | return new; |
| 1733 | } |
| 1734 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1735 | /* initialise zebra rib work queue */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1736 | static void |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1737 | rib_queue_init (struct zebra_t *zebra) |
| 1738 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1739 | assert (zebra); |
| 1740 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1741 | if (! (zebra->ribq = work_queue_new (zebra->master, |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1742 | "route_node processing"))) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1743 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1744 | zlog_err ("%s: could not initialise work queue!", __func__); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1745 | return; |
| 1746 | } |
| 1747 | |
| 1748 | /* fill in the work queue spec */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1749 | zebra->ribq->spec.workfunc = &meta_queue_process; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1750 | zebra->ribq->spec.errorfunc = NULL; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1751 | /* XXX: TODO: These should be runtime configurable via vty */ |
| 1752 | zebra->ribq->spec.max_retries = 3; |
Paul Jakma | 457eb9a | 2006-07-27 19:59:58 +0000 | [diff] [blame] | 1753 | zebra->ribq->spec.hold = rib_process_hold_time; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1754 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1755 | if (!(zebra->mq = meta_queue_new ())) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1756 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1757 | zlog_err ("%s: could not initialise meta queue!", __func__); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1758 | return; |
| 1759 | } |
| 1760 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1763 | /* RIB updates are processed via a queue of pointers to route_nodes. |
| 1764 | * |
| 1765 | * The queue length is bounded by the maximal size of the routing table, |
| 1766 | * as a route_node will not be requeued, if already queued. |
| 1767 | * |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1768 | * RIBs are submitted via rib_addnode or rib_delnode which set minimal |
| 1769 | * state, or static_install_ipv{4,6} (when an existing RIB is updated) |
| 1770 | * and then submit route_node to queue for best-path selection later. |
| 1771 | * Order of add/delete state changes are preserved for any given RIB. |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1772 | * |
| 1773 | * Deleted RIBs are reaped during best-path selection. |
| 1774 | * |
| 1775 | * rib_addnode |
| 1776 | * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1777 | * |-------->| | best RIB, if required |
| 1778 | * | | |
| 1779 | * static_install->|->rib_addqueue...... -> rib_process |
| 1780 | * | | |
| 1781 | * |-------->| |-> rib_unlink |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1782 | * |-> set RIB_ENTRY_REMOVE | |
| 1783 | * rib_delnode (RIB freed) |
| 1784 | * |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1785 | * The 'info' pointer of a route_node points to a rib_dest_t |
| 1786 | * ('dest'). Queueing state for a route_node is kept on the dest. The |
| 1787 | * dest is created on-demand by rib_link() and is kept around at least |
| 1788 | * as long as there are ribs hanging off it (@see rib_gc_dest()). |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1789 | * |
| 1790 | * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code): |
| 1791 | * |
| 1792 | * - route_nodes: refcounted by: |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1793 | * - dest attached to route_node: |
| 1794 | * - managed by: rib_link/rib_gc_dest |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1795 | * - route_node processing queue |
| 1796 | * - managed by: rib_addqueue, rib_process. |
| 1797 | * |
| 1798 | */ |
| 1799 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1800 | /* Add RIB to head of the route node. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1801 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1802 | rib_link (struct route_node *rn, struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1803 | { |
| 1804 | struct rib *head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1805 | rib_dest_t *dest; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1806 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1807 | assert (rib && rn); |
| 1808 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1809 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1810 | rnode_debug (rn, "rn %p, rib %p", rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1811 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1812 | dest = rib_dest_from_rnode (rn); |
| 1813 | if (!dest) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1814 | { |
| 1815 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1816 | rnode_debug (rn, "adding dest to table"); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1817 | |
| 1818 | dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t)); |
| 1819 | route_lock_node (rn); /* rn route table reference */ |
| 1820 | rn->info = dest; |
| 1821 | dest->rnode = rn; |
| 1822 | } |
| 1823 | |
| 1824 | head = dest->routes; |
| 1825 | if (head) |
| 1826 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1827 | head->prev = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1828 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | rib->next = head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1830 | dest->routes = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1831 | rib_queue_add (&zebrad, rn); |
| 1832 | } |
| 1833 | |
| 1834 | static void |
| 1835 | rib_addnode (struct route_node *rn, struct rib *rib) |
| 1836 | { |
| 1837 | /* RIB node has been un-removed before route-node is processed. |
| 1838 | * route_node must hence already be on the queue for processing.. |
| 1839 | */ |
| 1840 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1841 | { |
| 1842 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1843 | rnode_debug (rn, "rn %p, un-removed rib %p", rn, rib); |
| 1844 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1845 | UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1846 | return; |
| 1847 | } |
| 1848 | rib_link (rn, rib); |
| 1849 | } |
| 1850 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1851 | /* |
| 1852 | * rib_unlink |
| 1853 | * |
| 1854 | * Detach a rib structure from a route_node. |
| 1855 | * |
| 1856 | * Note that a call to rib_unlink() should be followed by a call to |
| 1857 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no |
| 1858 | * longer required to be deleted. |
| 1859 | */ |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1860 | static void |
| 1861 | rib_unlink (struct route_node *rn, struct rib *rib) |
| 1862 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1863 | rib_dest_t *dest; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1864 | |
| 1865 | assert (rn && rib); |
| 1866 | |
| 1867 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1868 | rnode_debug (rn, "rn %p, rib %p", rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1869 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1870 | dest = rib_dest_from_rnode (rn); |
| 1871 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1872 | if (rib->next) |
| 1873 | rib->next->prev = rib->prev; |
| 1874 | |
| 1875 | if (rib->prev) |
| 1876 | rib->prev->next = rib->next; |
| 1877 | else |
| 1878 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1879 | dest->routes = rib->next; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /* free RIB and nexthops */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1883 | nexthops_free(rib->nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1884 | XFREE (MTYPE_RIB, rib); |
| 1885 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1888 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1889 | rib_delnode (struct route_node *rn, struct rib *rib) |
| 1890 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1891 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1892 | rnode_debug (rn, "rn %p, rib %p, removing", rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1893 | SET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1894 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | int |
| 1898 | rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1899 | struct in_addr *gate, struct in_addr *src, |
| 1900 | unsigned int ifindex, u_int32_t vrf_id, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1901 | u_int32_t metric, u_char distance, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1902 | { |
| 1903 | struct rib *rib; |
| 1904 | struct rib *same = NULL; |
| 1905 | struct route_table *table; |
| 1906 | struct route_node *rn; |
| 1907 | struct nexthop *nexthop; |
| 1908 | |
| 1909 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1910 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1911 | if (! table) |
| 1912 | return 0; |
| 1913 | |
| 1914 | /* Make it sure prefixlen is applied to the prefix. */ |
| 1915 | apply_mask_ipv4 (p); |
| 1916 | |
| 1917 | /* Set default distance by route type. */ |
| 1918 | if (distance == 0) |
| 1919 | { |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 1920 | if ((unsigned)type >= array_size(route_info)) |
David Lamparter | 7052f22 | 2009-08-27 00:28:28 +0200 | [diff] [blame] | 1921 | distance = 150; |
| 1922 | else |
| 1923 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1924 | |
| 1925 | /* iBGP distance is 200. */ |
| 1926 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 1927 | distance = 200; |
| 1928 | } |
| 1929 | |
| 1930 | /* Lookup route node.*/ |
| 1931 | rn = route_node_get (table, (struct prefix *) p); |
| 1932 | |
| 1933 | /* If same type of route are installed, treat it as a implicit |
| 1934 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1935 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1936 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1937 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1938 | continue; |
| 1939 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1940 | if (rib->type != type) |
| 1941 | continue; |
| 1942 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1943 | { |
| 1944 | same = rib; |
| 1945 | break; |
| 1946 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1947 | /* Duplicate connected route comes in. */ |
| 1948 | else if ((nexthop = rib->nexthop) && |
| 1949 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1950 | nexthop->ifindex == ifindex && |
| 1951 | !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1952 | { |
| 1953 | rib->refcnt++; |
| 1954 | return 0 ; |
| 1955 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1959 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1960 | rib->type = type; |
| 1961 | rib->distance = distance; |
| 1962 | rib->flags = flags; |
| 1963 | rib->metric = metric; |
paul | b5f4502 | 2003-11-02 07:28:05 +0000 | [diff] [blame] | 1964 | rib->table = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1965 | rib->nexthop_num = 0; |
| 1966 | rib->uptime = time (NULL); |
| 1967 | |
| 1968 | /* Nexthop settings. */ |
| 1969 | if (gate) |
| 1970 | { |
| 1971 | if (ifindex) |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1972 | nexthop_ipv4_ifindex_add (rib, gate, src, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1973 | else |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1974 | nexthop_ipv4_add (rib, gate, src); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1975 | } |
| 1976 | else |
| 1977 | nexthop_ifindex_add (rib, ifindex); |
| 1978 | |
| 1979 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1980 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 1981 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1982 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1983 | |
| 1984 | /* Link new rib to node.*/ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1985 | if (IS_ZEBRA_DEBUG_RIB) |
| 1986 | zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1987 | rib_addnode (rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1988 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1989 | /* Free implicit route.*/ |
| 1990 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1991 | { |
| 1992 | if (IS_ZEBRA_DEBUG_RIB) |
| 1993 | zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1994 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1995 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1996 | |
| 1997 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1998 | return 0; |
| 1999 | } |
| 2000 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2001 | /* This function dumps the contents of a given RIB entry into |
| 2002 | * standard debug log. Calling function name and IP prefix in |
| 2003 | * question are passed as 1st and 2nd arguments. |
| 2004 | */ |
| 2005 | |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2006 | void _rib_dump (const char * func, |
| 2007 | union prefix46constptr pp, const struct rib * rib) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2008 | { |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2009 | const struct prefix *p = pp.p; |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2010 | char straddr[INET6_ADDRSTRLEN]; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2011 | struct nexthop *nexthop, *tnexthop; |
| 2012 | int recursing; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2013 | |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2014 | inet_ntop (p->family, &p->u.prefix, straddr, INET6_ADDRSTRLEN); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2015 | zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr, p->prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2016 | zlog_debug |
| 2017 | ( |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 2018 | "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d", |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2019 | func, |
| 2020 | rib->refcnt, |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 2021 | (unsigned long) rib->uptime, |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2022 | rib->type, |
| 2023 | rib->table |
| 2024 | ); |
| 2025 | zlog_debug |
| 2026 | ( |
| 2027 | "%s: metric == %u, distance == %u, flags == %u, status == %u", |
| 2028 | func, |
| 2029 | rib->metric, |
| 2030 | rib->distance, |
| 2031 | rib->flags, |
| 2032 | rib->status |
| 2033 | ); |
| 2034 | zlog_debug |
| 2035 | ( |
| 2036 | "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u", |
| 2037 | func, |
| 2038 | rib->nexthop_num, |
| 2039 | rib->nexthop_active_num, |
| 2040 | rib->nexthop_fib_num |
| 2041 | ); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2042 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2043 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2044 | { |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2045 | inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2046 | zlog_debug |
| 2047 | ( |
| 2048 | "%s: %s %s with flags %s%s%s", |
| 2049 | func, |
| 2050 | (recursing ? " NH" : "NH"), |
| 2051 | straddr, |
| 2052 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""), |
| 2053 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""), |
| 2054 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "") |
| 2055 | ); |
| 2056 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2057 | zlog_debug ("%s: dump complete", func); |
| 2058 | } |
| 2059 | |
| 2060 | /* This is an exported helper to rtm_read() to dump the strange |
| 2061 | * RIB entry found by rib_lookup_ipv4_route() |
| 2062 | */ |
| 2063 | |
| 2064 | void rib_lookup_and_dump (struct prefix_ipv4 * p) |
| 2065 | { |
| 2066 | struct route_table *table; |
| 2067 | struct route_node *rn; |
| 2068 | struct rib *rib; |
| 2069 | char prefix_buf[INET_ADDRSTRLEN]; |
| 2070 | |
| 2071 | /* Lookup table. */ |
| 2072 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 2073 | if (! table) |
| 2074 | { |
| 2075 | zlog_err ("%s: vrf_table() returned NULL", __func__); |
| 2076 | return; |
| 2077 | } |
| 2078 | |
| 2079 | inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN); |
| 2080 | /* Scan the RIB table for exactly matching RIB entry. */ |
| 2081 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2082 | |
| 2083 | /* No route for this prefix. */ |
| 2084 | if (! rn) |
| 2085 | { |
| 2086 | zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | /* Unlock node. */ |
| 2091 | route_unlock_node (rn); |
| 2092 | |
| 2093 | /* let's go */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2094 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2095 | { |
| 2096 | zlog_debug |
| 2097 | ( |
| 2098 | "%s: rn %p, rib %p: %s, %s", |
| 2099 | __func__, |
| 2100 | rn, |
| 2101 | rib, |
| 2102 | (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"), |
| 2103 | (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected") |
| 2104 | ); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2105 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2109 | /* Check if requested address assignment will fail due to another |
| 2110 | * route being installed by zebra in FIB already. Take necessary |
| 2111 | * actions, if needed: remove such a route from FIB and deSELECT |
| 2112 | * corresponding RIB entry. Then put affected RN into RIBQ head. |
| 2113 | */ |
| 2114 | void rib_lookup_and_pushup (struct prefix_ipv4 * p) |
| 2115 | { |
| 2116 | struct route_table *table; |
| 2117 | struct route_node *rn; |
| 2118 | struct rib *rib; |
| 2119 | unsigned changed = 0; |
| 2120 | |
| 2121 | if (NULL == (table = vrf_table (AFI_IP, SAFI_UNICAST, 0))) |
| 2122 | { |
| 2123 | zlog_err ("%s: vrf_table() returned NULL", __func__); |
| 2124 | return; |
| 2125 | } |
| 2126 | |
| 2127 | /* No matches would be the simplest case. */ |
| 2128 | if (NULL == (rn = route_node_lookup (table, (struct prefix *) p))) |
| 2129 | return; |
| 2130 | |
| 2131 | /* Unlock node. */ |
| 2132 | route_unlock_node (rn); |
| 2133 | |
| 2134 | /* Check all RIB entries. In case any changes have to be done, requeue |
| 2135 | * the RN into RIBQ head. If the routing message about the new connected |
| 2136 | * route (generated by the IP address we are going to assign very soon) |
| 2137 | * comes before the RIBQ is processed, the new RIB entry will join |
| 2138 | * RIBQ record already on head. This is necessary for proper revalidation |
| 2139 | * of the rest of the RIB. |
| 2140 | */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2141 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2142 | { |
| 2143 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) && |
| 2144 | ! RIB_SYSTEM_ROUTE (rib)) |
| 2145 | { |
| 2146 | changed = 1; |
| 2147 | if (IS_ZEBRA_DEBUG_RIB) |
| 2148 | { |
| 2149 | char buf[INET_ADDRSTRLEN]; |
| 2150 | inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN); |
| 2151 | zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__, buf, p->prefixlen); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2152 | rib_dump (&rn->p, rib); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2153 | } |
| 2154 | rib_uninstall (rn, rib); |
| 2155 | } |
| 2156 | } |
| 2157 | if (changed) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2158 | rib_queue_add (&zebrad, rn); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2161 | int |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2162 | rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2163 | { |
| 2164 | struct route_table *table; |
| 2165 | struct route_node *rn; |
| 2166 | struct rib *same; |
| 2167 | struct nexthop *nexthop; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2169 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2170 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2171 | if (! table) |
| 2172 | return 0; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2173 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2174 | /* Make it sure prefixlen is applied to the prefix. */ |
| 2175 | apply_mask_ipv4 (p); |
| 2176 | |
| 2177 | /* Set default distance by route type. */ |
| 2178 | if (rib->distance == 0) |
| 2179 | { |
| 2180 | rib->distance = route_info[rib->type].distance; |
| 2181 | |
| 2182 | /* iBGP distance is 200. */ |
| 2183 | if (rib->type == ZEBRA_ROUTE_BGP |
| 2184 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 2185 | rib->distance = 200; |
| 2186 | } |
| 2187 | |
| 2188 | /* Lookup route node.*/ |
| 2189 | rn = route_node_get (table, (struct prefix *) p); |
| 2190 | |
| 2191 | /* If same type of route are installed, treat it as a implicit |
| 2192 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2193 | RNODE_FOREACH_RIB (rn, same) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2194 | { |
Paul Jakma | 0b8c4f1 | 2007-06-27 11:12:38 +0000 | [diff] [blame] | 2195 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2196 | continue; |
| 2197 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2198 | if (same->type == rib->type && same->table == rib->table |
| 2199 | && same->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2200 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2201 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2202 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2203 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2204 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) |
| 2205 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2206 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2207 | |
| 2208 | /* Link new rib to node.*/ |
| 2209 | rib_addnode (rn, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2210 | if (IS_ZEBRA_DEBUG_RIB) |
| 2211 | { |
| 2212 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
| 2213 | __func__, rn, rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2214 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2215 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2216 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2217 | /* Free implicit route.*/ |
| 2218 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2219 | { |
| 2220 | if (IS_ZEBRA_DEBUG_RIB) |
| 2221 | { |
| 2222 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
| 2223 | __func__, rn, same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2224 | rib_dump (p, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2225 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2226 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2227 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2228 | |
| 2229 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2230 | return 0; |
| 2231 | } |
| 2232 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2233 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2234 | int |
| 2235 | rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2236 | struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2237 | { |
| 2238 | struct route_table *table; |
| 2239 | struct route_node *rn; |
| 2240 | struct rib *rib; |
| 2241 | struct rib *fib = NULL; |
| 2242 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2243 | struct nexthop *nexthop, *tnexthop; |
| 2244 | int recursing; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2245 | char buf1[INET_ADDRSTRLEN]; |
| 2246 | char buf2[INET_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2247 | |
| 2248 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2249 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2250 | if (! table) |
| 2251 | return 0; |
| 2252 | |
| 2253 | /* Apply mask. */ |
| 2254 | apply_mask_ipv4 (p); |
| 2255 | |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2256 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2257 | { |
| 2258 | if (gate) |
| 2259 | zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d", |
| 2260 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
| 2261 | p->prefixlen, |
| 2262 | inet_ntoa (*gate), |
| 2263 | ifindex); |
| 2264 | else |
| 2265 | zlog_debug ("rib_delete_ipv4(): route delete %s/%d ifindex %d", |
| 2266 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
| 2267 | p->prefixlen, |
| 2268 | ifindex); |
| 2269 | } |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2270 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2271 | /* Lookup route node. */ |
| 2272 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2273 | if (! rn) |
| 2274 | { |
| 2275 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2276 | { |
| 2277 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2278 | zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2279 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2280 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2281 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2282 | ifindex); |
| 2283 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2284 | zlog_debug ("route %s/%d ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2285 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2286 | p->prefixlen, |
| 2287 | ifindex); |
| 2288 | } |
| 2289 | return ZEBRA_ERR_RTNOEXIST; |
| 2290 | } |
| 2291 | |
| 2292 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2293 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2294 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2295 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2296 | continue; |
| 2297 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2298 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 2299 | fib = rib; |
| 2300 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2301 | if (rib->type != type) |
| 2302 | continue; |
| 2303 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2304 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2305 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2306 | if (nexthop->ifindex != ifindex) |
| 2307 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2308 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2309 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2310 | rib->refcnt--; |
| 2311 | route_unlock_node (rn); |
| 2312 | route_unlock_node (rn); |
| 2313 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2314 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2315 | same = rib; |
| 2316 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2317 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2318 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2319 | else |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2320 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2321 | if (gate == NULL) |
| 2322 | { |
| 2323 | same = rib; |
| 2324 | break; |
| 2325 | } |
| 2326 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2327 | if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate)) |
| 2328 | { |
| 2329 | same = rib; |
| 2330 | break; |
| 2331 | } |
| 2332 | if (same) |
| 2333 | break; |
| 2334 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2335 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2336 | /* If same type of route can't be found and this message is from |
| 2337 | kernel. */ |
| 2338 | if (! same) |
| 2339 | { |
| 2340 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2341 | { |
| 2342 | /* Unset flags. */ |
| 2343 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2344 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2345 | |
| 2346 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 2347 | } |
| 2348 | else |
| 2349 | { |
| 2350 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2351 | { |
| 2352 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2353 | zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2354 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2355 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2356 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2357 | ifindex, |
| 2358 | type); |
| 2359 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2360 | zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2361 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2362 | p->prefixlen, |
| 2363 | ifindex, |
| 2364 | type); |
| 2365 | } |
| 2366 | route_unlock_node (rn); |
| 2367 | return ZEBRA_ERR_RTNOEXIST; |
| 2368 | } |
| 2369 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2370 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2371 | if (same) |
| 2372 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2373 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2374 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2375 | return 0; |
| 2376 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2377 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2378 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2379 | static void |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2380 | static_install_ipv4 (safi_t safi, struct prefix *p, struct static_ipv4 *si) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2381 | { |
| 2382 | struct rib *rib; |
| 2383 | struct route_node *rn; |
| 2384 | struct route_table *table; |
| 2385 | |
| 2386 | /* Lookup table. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2387 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2388 | if (! table) |
| 2389 | return; |
| 2390 | |
| 2391 | /* Lookup existing route */ |
| 2392 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2393 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2394 | { |
| 2395 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2396 | continue; |
| 2397 | |
| 2398 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2399 | break; |
| 2400 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (rib) |
| 2403 | { |
| 2404 | /* Same distance static route is there. Update it with new |
| 2405 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2406 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2407 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2408 | { |
| 2409 | case STATIC_IPV4_GATEWAY: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2410 | nexthop_ipv4_add (rib, &si->gate.ipv4, NULL); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | case STATIC_IPV4_IFNAME: |
| 2413 | nexthop_ifname_add (rib, si->gate.ifname); |
| 2414 | break; |
| 2415 | case STATIC_IPV4_BLACKHOLE: |
| 2416 | nexthop_blackhole_add (rib); |
| 2417 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2418 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2419 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2420 | } |
| 2421 | else |
| 2422 | { |
| 2423 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2424 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2425 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2426 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2427 | rib->distance = si->distance; |
| 2428 | rib->metric = 0; |
Nolan Leake | b0145dd | 2012-09-13 17:17:31 +0000 | [diff] [blame] | 2429 | rib->table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2430 | rib->nexthop_num = 0; |
| 2431 | |
| 2432 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2433 | { |
| 2434 | case STATIC_IPV4_GATEWAY: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2435 | nexthop_ipv4_add (rib, &si->gate.ipv4, NULL); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | case STATIC_IPV4_IFNAME: |
| 2438 | nexthop_ifname_add (rib, si->gate.ifname); |
| 2439 | break; |
| 2440 | case STATIC_IPV4_BLACKHOLE: |
| 2441 | nexthop_blackhole_add (rib); |
| 2442 | break; |
| 2443 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2444 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2445 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2446 | rib->flags = si->flags; |
| 2447 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2448 | /* Link this rib to the tree. */ |
| 2449 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2450 | } |
| 2451 | } |
| 2452 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2453 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2454 | static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si) |
| 2455 | { |
| 2456 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 2457 | && si->type == STATIC_IPV4_GATEWAY |
| 2458 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4)) |
| 2459 | return 1; |
| 2460 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2461 | && si->type == STATIC_IPV4_IFNAME |
| 2462 | && strcmp (nexthop->ifname, si->gate.ifname) == 0) |
| 2463 | return 1; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2464 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
| 2465 | && si->type == STATIC_IPV4_BLACKHOLE) |
| 2466 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 2467 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* Uninstall static route from RIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2471 | static void |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2472 | static_uninstall_ipv4 (safi_t safi, struct prefix *p, struct static_ipv4 *si) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2473 | { |
| 2474 | struct route_node *rn; |
| 2475 | struct rib *rib; |
| 2476 | struct nexthop *nexthop; |
| 2477 | struct route_table *table; |
| 2478 | |
| 2479 | /* Lookup table. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2480 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2481 | if (! table) |
| 2482 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2483 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2484 | /* Lookup existing route with type and distance. */ |
| 2485 | rn = route_node_lookup (table, p); |
| 2486 | if (! rn) |
| 2487 | return; |
| 2488 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2489 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2490 | { |
| 2491 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2492 | continue; |
| 2493 | |
| 2494 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2495 | break; |
| 2496 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2497 | |
| 2498 | if (! rib) |
| 2499 | { |
| 2500 | route_unlock_node (rn); |
| 2501 | return; |
| 2502 | } |
| 2503 | |
| 2504 | /* Lookup nexthop. */ |
| 2505 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2506 | if (static_ipv4_nexthop_same (nexthop, si)) |
| 2507 | break; |
| 2508 | |
| 2509 | /* Can't find nexthop. */ |
| 2510 | if (! nexthop) |
| 2511 | { |
| 2512 | route_unlock_node (rn); |
| 2513 | return; |
| 2514 | } |
| 2515 | |
| 2516 | /* Check nexthop. */ |
| 2517 | if (rib->nexthop_num == 1) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2518 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2519 | else |
| 2520 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 2521 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 2522 | rib_uninstall (rn, rib); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 2523 | nexthop_delete (rib, nexthop); |
| 2524 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2525 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2526 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2527 | /* Unlock node. */ |
| 2528 | route_unlock_node (rn); |
| 2529 | } |
| 2530 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2531 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2532 | static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate, |
| 2533 | const char *ifname, u_char flags, u_char distance, |
| 2534 | u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2535 | { |
| 2536 | u_char type = 0; |
| 2537 | struct route_node *rn; |
| 2538 | struct static_ipv4 *si; |
| 2539 | struct static_ipv4 *pp; |
| 2540 | struct static_ipv4 *cp; |
| 2541 | struct static_ipv4 *update = NULL; |
| 2542 | struct route_table *stable; |
| 2543 | |
| 2544 | /* Lookup table. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2545 | stable = vrf_static_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2546 | if (! stable) |
| 2547 | return -1; |
| 2548 | |
| 2549 | /* Lookup static route prefix. */ |
| 2550 | rn = route_node_get (stable, p); |
| 2551 | |
| 2552 | /* Make flags. */ |
| 2553 | if (gate) |
| 2554 | type = STATIC_IPV4_GATEWAY; |
paul | 368aa3f | 2003-05-25 23:24:50 +0000 | [diff] [blame] | 2555 | else if (ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2556 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2557 | else |
| 2558 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2559 | |
| 2560 | /* Do nothing if there is a same static route. */ |
| 2561 | for (si = rn->info; si; si = si->next) |
| 2562 | { |
| 2563 | if (type == si->type |
| 2564 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 2565 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 2566 | { |
| 2567 | if (distance == si->distance) |
| 2568 | { |
| 2569 | route_unlock_node (rn); |
| 2570 | return 0; |
| 2571 | } |
| 2572 | else |
| 2573 | update = si; |
| 2574 | } |
| 2575 | } |
| 2576 | |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2577 | /* Distance changed. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2578 | if (update) |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2579 | static_delete_ipv4_safi (safi, p, gate, ifname, update->distance, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2580 | |
| 2581 | /* Make new static route structure. */ |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 2582 | si = XCALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2583 | |
| 2584 | si->type = type; |
| 2585 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2586 | si->flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2587 | |
| 2588 | if (gate) |
| 2589 | si->gate.ipv4 = *gate; |
| 2590 | if (ifname) |
| 2591 | si->gate.ifname = XSTRDUP (0, ifname); |
| 2592 | |
| 2593 | /* Add new static route information to the tree with sort by |
| 2594 | distance value and gateway address. */ |
| 2595 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2596 | { |
| 2597 | if (si->distance < cp->distance) |
| 2598 | break; |
| 2599 | if (si->distance > cp->distance) |
| 2600 | continue; |
| 2601 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) |
| 2602 | { |
| 2603 | if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr)) |
| 2604 | break; |
| 2605 | if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr)) |
| 2606 | continue; |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | /* Make linked list. */ |
| 2611 | if (pp) |
| 2612 | pp->next = si; |
| 2613 | else |
| 2614 | rn->info = si; |
| 2615 | if (cp) |
| 2616 | cp->prev = si; |
| 2617 | si->prev = pp; |
| 2618 | si->next = cp; |
| 2619 | |
| 2620 | /* Install into rib. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2621 | static_install_ipv4 (safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2622 | |
| 2623 | return 1; |
| 2624 | } |
| 2625 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2626 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2627 | static_delete_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate, |
| 2628 | const char *ifname, u_char distance, u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2629 | { |
| 2630 | u_char type = 0; |
| 2631 | struct route_node *rn; |
| 2632 | struct static_ipv4 *si; |
| 2633 | struct route_table *stable; |
| 2634 | |
| 2635 | /* Lookup table. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2636 | stable = vrf_static_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2637 | if (! stable) |
| 2638 | return -1; |
| 2639 | |
| 2640 | /* Lookup static route prefix. */ |
| 2641 | rn = route_node_lookup (stable, p); |
| 2642 | if (! rn) |
| 2643 | return 0; |
| 2644 | |
| 2645 | /* Make flags. */ |
| 2646 | if (gate) |
| 2647 | type = STATIC_IPV4_GATEWAY; |
| 2648 | else if (ifname) |
| 2649 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2650 | else |
| 2651 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2652 | |
| 2653 | /* Find same static route is the tree */ |
| 2654 | for (si = rn->info; si; si = si->next) |
| 2655 | if (type == si->type |
| 2656 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 2657 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 2658 | break; |
| 2659 | |
| 2660 | /* Can't find static route. */ |
| 2661 | if (! si) |
| 2662 | { |
| 2663 | route_unlock_node (rn); |
| 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | /* Install into rib. */ |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2668 | static_uninstall_ipv4 (safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2669 | |
| 2670 | /* Unlink static route from linked list. */ |
| 2671 | if (si->prev) |
| 2672 | si->prev->next = si->next; |
| 2673 | else |
| 2674 | rn->info = si->next; |
| 2675 | if (si->next) |
| 2676 | si->next->prev = si->prev; |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2677 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2678 | |
| 2679 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2680 | if (ifname) |
| 2681 | XFREE (0, si->gate.ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2682 | XFREE (MTYPE_STATIC_IPV4, si); |
| 2683 | |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2684 | route_unlock_node (rn); |
| 2685 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2686 | return 1; |
| 2687 | } |
| 2688 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2689 | #ifdef HAVE_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2690 | int |
| 2691 | rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2692 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2693 | u_int32_t metric, u_char distance, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2694 | { |
| 2695 | struct rib *rib; |
| 2696 | struct rib *same = NULL; |
| 2697 | struct route_table *table; |
| 2698 | struct route_node *rn; |
| 2699 | struct nexthop *nexthop; |
| 2700 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2701 | /* Lookup table. */ |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2702 | table = vrf_table (AFI_IP6, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2703 | if (! table) |
| 2704 | return 0; |
| 2705 | |
| 2706 | /* Make sure mask is applied. */ |
| 2707 | apply_mask_ipv6 (p); |
| 2708 | |
| 2709 | /* Set default distance by route type. */ |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2710 | if (!distance) |
| 2711 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2712 | |
| 2713 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 2714 | distance = 200; |
| 2715 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2716 | /* Lookup route node.*/ |
| 2717 | rn = route_node_get (table, (struct prefix *) p); |
| 2718 | |
| 2719 | /* If same type of route are installed, treat it as a implicit |
| 2720 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2721 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2722 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2723 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2724 | continue; |
| 2725 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2726 | if (rib->type != type) |
| 2727 | continue; |
| 2728 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2729 | { |
| 2730 | same = rib; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2733 | else if ((nexthop = rib->nexthop) && |
| 2734 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
| 2735 | nexthop->ifindex == ifindex) |
| 2736 | { |
| 2737 | rib->refcnt++; |
| 2738 | return 0; |
| 2739 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2743 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2744 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2745 | rib->type = type; |
| 2746 | rib->distance = distance; |
| 2747 | rib->flags = flags; |
| 2748 | rib->metric = metric; |
paul | b5f4502 | 2003-11-02 07:28:05 +0000 | [diff] [blame] | 2749 | rib->table = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2750 | rib->nexthop_num = 0; |
| 2751 | rib->uptime = time (NULL); |
| 2752 | |
| 2753 | /* Nexthop settings. */ |
| 2754 | if (gate) |
| 2755 | { |
| 2756 | if (ifindex) |
| 2757 | nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
| 2758 | else |
| 2759 | nexthop_ipv6_add (rib, gate); |
| 2760 | } |
| 2761 | else |
| 2762 | nexthop_ifindex_add (rib, ifindex); |
| 2763 | |
| 2764 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2765 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 2766 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2767 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2768 | |
| 2769 | /* Link new rib to node.*/ |
| 2770 | rib_addnode (rn, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2771 | if (IS_ZEBRA_DEBUG_RIB) |
| 2772 | { |
| 2773 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
| 2774 | __func__, rn, rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2775 | rib_dump (p, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2776 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2777 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2778 | /* Free implicit route.*/ |
| 2779 | if (same) |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2780 | { |
| 2781 | if (IS_ZEBRA_DEBUG_RIB) |
| 2782 | { |
| 2783 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
| 2784 | __func__, rn, same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2785 | rib_dump (p, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2786 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2787 | rib_delnode (rn, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2788 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2789 | |
| 2790 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2791 | return 0; |
| 2792 | } |
| 2793 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2794 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2795 | int |
| 2796 | rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2797 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2798 | { |
| 2799 | struct route_table *table; |
| 2800 | struct route_node *rn; |
| 2801 | struct rib *rib; |
| 2802 | struct rib *fib = NULL; |
| 2803 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2804 | struct nexthop *nexthop, *tnexthop; |
| 2805 | int recursing; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2806 | char buf1[INET6_ADDRSTRLEN]; |
| 2807 | char buf2[INET6_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2808 | |
| 2809 | /* Apply mask. */ |
| 2810 | apply_mask_ipv6 (p); |
| 2811 | |
| 2812 | /* Lookup table. */ |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2813 | table = vrf_table (AFI_IP6, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2814 | if (! table) |
| 2815 | return 0; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2816 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2817 | /* Lookup route node. */ |
| 2818 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2819 | if (! rn) |
| 2820 | { |
| 2821 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2822 | { |
| 2823 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2824 | zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2825 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2826 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2827 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2828 | ifindex); |
| 2829 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2830 | zlog_debug ("route %s/%d ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2831 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2832 | p->prefixlen, |
| 2833 | ifindex); |
| 2834 | } |
| 2835 | return ZEBRA_ERR_RTNOEXIST; |
| 2836 | } |
| 2837 | |
| 2838 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2839 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2840 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2841 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2842 | continue; |
| 2843 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2844 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 2845 | fib = rib; |
| 2846 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2847 | if (rib->type != type) |
| 2848 | continue; |
| 2849 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2850 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2851 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2852 | if (nexthop->ifindex != ifindex) |
| 2853 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2854 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2855 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2856 | rib->refcnt--; |
| 2857 | route_unlock_node (rn); |
| 2858 | route_unlock_node (rn); |
| 2859 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2860 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2861 | same = rib; |
| 2862 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2863 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2864 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2865 | else |
| 2866 | { |
| 2867 | if (gate == NULL) |
| 2868 | { |
| 2869 | same = rib; |
| 2870 | break; |
| 2871 | } |
| 2872 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2873 | if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate)) |
| 2874 | { |
| 2875 | same = rib; |
| 2876 | break; |
| 2877 | } |
| 2878 | if (same) |
| 2879 | break; |
| 2880 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | /* If same type of route can't be found and this message is from |
| 2884 | kernel. */ |
| 2885 | if (! same) |
| 2886 | { |
| 2887 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2888 | { |
| 2889 | /* Unset flags. */ |
| 2890 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2891 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2892 | |
| 2893 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 2894 | } |
| 2895 | else |
| 2896 | { |
| 2897 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2898 | { |
| 2899 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2900 | zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2901 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2902 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2903 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2904 | ifindex, |
| 2905 | type); |
| 2906 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2907 | zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2908 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2909 | p->prefixlen, |
| 2910 | ifindex, |
| 2911 | type); |
| 2912 | } |
| 2913 | route_unlock_node (rn); |
| 2914 | return ZEBRA_ERR_RTNOEXIST; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | if (same) |
| 2919 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2920 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2921 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2924 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2925 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2926 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2927 | static_install_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 2928 | { |
| 2929 | struct rib *rib; |
| 2930 | struct route_table *table; |
| 2931 | struct route_node *rn; |
| 2932 | |
| 2933 | /* Lookup table. */ |
| 2934 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 2935 | if (! table) |
| 2936 | return; |
| 2937 | |
| 2938 | /* Lookup existing route */ |
| 2939 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2940 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2941 | { |
| 2942 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2943 | continue; |
| 2944 | |
| 2945 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2946 | break; |
| 2947 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2948 | |
| 2949 | if (rib) |
| 2950 | { |
| 2951 | /* Same distance static route is there. Update it with new |
| 2952 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2953 | route_unlock_node (rn); |
| 2954 | |
| 2955 | switch (si->type) |
| 2956 | { |
| 2957 | case STATIC_IPV6_GATEWAY: |
| 2958 | nexthop_ipv6_add (rib, &si->ipv6); |
| 2959 | break; |
| 2960 | case STATIC_IPV6_IFNAME: |
| 2961 | nexthop_ifname_add (rib, si->ifname); |
| 2962 | break; |
| 2963 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2964 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 2965 | break; |
| 2966 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2967 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2968 | } |
| 2969 | else |
| 2970 | { |
| 2971 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2972 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2973 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2975 | rib->distance = si->distance; |
| 2976 | rib->metric = 0; |
Dinesh G Dutt | d1b0991 | 2014-09-30 12:54:13 -0700 | [diff] [blame] | 2977 | rib->table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2978 | rib->nexthop_num = 0; |
| 2979 | |
| 2980 | switch (si->type) |
| 2981 | { |
| 2982 | case STATIC_IPV6_GATEWAY: |
| 2983 | nexthop_ipv6_add (rib, &si->ipv6); |
| 2984 | break; |
| 2985 | case STATIC_IPV6_IFNAME: |
| 2986 | nexthop_ifname_add (rib, si->ifname); |
| 2987 | break; |
| 2988 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2989 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 2990 | break; |
| 2991 | } |
| 2992 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2993 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2994 | rib->flags = si->flags; |
| 2995 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2996 | /* Link this rib to the tree. */ |
| 2997 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3001 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3002 | static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si) |
| 3003 | { |
| 3004 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 3005 | && si->type == STATIC_IPV6_GATEWAY |
| 3006 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)) |
| 3007 | return 1; |
| 3008 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 3009 | && si->type == STATIC_IPV6_IFNAME |
| 3010 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 3011 | return 1; |
| 3012 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 3013 | && si->type == STATIC_IPV6_GATEWAY_IFNAME |
| 3014 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6) |
| 3015 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 3016 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 3017 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3020 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3021 | static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 3022 | { |
| 3023 | struct route_table *table; |
| 3024 | struct route_node *rn; |
| 3025 | struct rib *rib; |
| 3026 | struct nexthop *nexthop; |
| 3027 | |
| 3028 | /* Lookup table. */ |
| 3029 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 3030 | if (! table) |
| 3031 | return; |
| 3032 | |
| 3033 | /* Lookup existing route with type and distance. */ |
| 3034 | rn = route_node_lookup (table, (struct prefix *) p); |
| 3035 | if (! rn) |
| 3036 | return; |
| 3037 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3038 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3039 | { |
| 3040 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3041 | continue; |
| 3042 | |
| 3043 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 3044 | break; |
| 3045 | } |
| 3046 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3047 | if (! rib) |
| 3048 | { |
| 3049 | route_unlock_node (rn); |
| 3050 | return; |
| 3051 | } |
| 3052 | |
| 3053 | /* Lookup nexthop. */ |
| 3054 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 3055 | if (static_ipv6_nexthop_same (nexthop, si)) |
| 3056 | break; |
| 3057 | |
| 3058 | /* Can't find nexthop. */ |
| 3059 | if (! nexthop) |
| 3060 | { |
| 3061 | route_unlock_node (rn); |
| 3062 | return; |
| 3063 | } |
| 3064 | |
| 3065 | /* Check nexthop. */ |
| 3066 | if (rib->nexthop_num == 1) |
| 3067 | { |
| 3068 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3069 | } |
| 3070 | else |
| 3071 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 3072 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 3073 | rib_uninstall (rn, rib); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 3074 | nexthop_delete (rib, nexthop); |
| 3075 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3076 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3077 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3078 | /* Unlock node. */ |
| 3079 | route_unlock_node (rn); |
| 3080 | } |
| 3081 | |
| 3082 | /* Add static route into static route configuration. */ |
| 3083 | int |
| 3084 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 3085 | const char *ifname, u_char flags, u_char distance, |
| 3086 | u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3087 | { |
| 3088 | struct route_node *rn; |
| 3089 | struct static_ipv6 *si; |
| 3090 | struct static_ipv6 *pp; |
| 3091 | struct static_ipv6 *cp; |
| 3092 | struct route_table *stable; |
| 3093 | |
| 3094 | /* Lookup table. */ |
| 3095 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 3096 | if (! stable) |
| 3097 | return -1; |
Paul Jakma | 27b4725 | 2006-07-02 16:38:54 +0000 | [diff] [blame] | 3098 | |
| 3099 | if (!gate && |
| 3100 | (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME)) |
| 3101 | return -1; |
| 3102 | |
| 3103 | if (!ifname && |
| 3104 | (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME)) |
| 3105 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3106 | |
| 3107 | /* Lookup static route prefix. */ |
| 3108 | rn = route_node_get (stable, p); |
| 3109 | |
| 3110 | /* Do nothing if there is a same static route. */ |
| 3111 | for (si = rn->info; si; si = si->next) |
| 3112 | { |
| 3113 | if (distance == si->distance |
| 3114 | && type == si->type |
| 3115 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 3116 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 3117 | { |
| 3118 | route_unlock_node (rn); |
| 3119 | return 0; |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | /* Make new static route structure. */ |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 3124 | si = XCALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3125 | |
| 3126 | si->type = type; |
| 3127 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 3128 | si->flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3129 | |
| 3130 | switch (type) |
| 3131 | { |
| 3132 | case STATIC_IPV6_GATEWAY: |
| 3133 | si->ipv6 = *gate; |
| 3134 | break; |
| 3135 | case STATIC_IPV6_IFNAME: |
| 3136 | si->ifname = XSTRDUP (0, ifname); |
| 3137 | break; |
| 3138 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 3139 | si->ipv6 = *gate; |
| 3140 | si->ifname = XSTRDUP (0, ifname); |
| 3141 | break; |
| 3142 | } |
| 3143 | |
| 3144 | /* Add new static route information to the tree with sort by |
| 3145 | distance value and gateway address. */ |
| 3146 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 3147 | { |
| 3148 | if (si->distance < cp->distance) |
| 3149 | break; |
| 3150 | if (si->distance > cp->distance) |
| 3151 | continue; |
| 3152 | } |
| 3153 | |
| 3154 | /* Make linked list. */ |
| 3155 | if (pp) |
| 3156 | pp->next = si; |
| 3157 | else |
| 3158 | rn->info = si; |
| 3159 | if (cp) |
| 3160 | cp->prev = si; |
| 3161 | si->prev = pp; |
| 3162 | si->next = cp; |
| 3163 | |
| 3164 | /* Install into rib. */ |
| 3165 | static_install_ipv6 (p, si); |
| 3166 | |
| 3167 | return 1; |
| 3168 | } |
| 3169 | |
| 3170 | /* Delete static route from static route configuration. */ |
| 3171 | int |
| 3172 | static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 3173 | const char *ifname, u_char distance, u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3174 | { |
| 3175 | struct route_node *rn; |
| 3176 | struct static_ipv6 *si; |
| 3177 | struct route_table *stable; |
| 3178 | |
| 3179 | /* Lookup table. */ |
| 3180 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 3181 | if (! stable) |
| 3182 | return -1; |
| 3183 | |
| 3184 | /* Lookup static route prefix. */ |
| 3185 | rn = route_node_lookup (stable, p); |
| 3186 | if (! rn) |
| 3187 | return 0; |
| 3188 | |
| 3189 | /* Find same static route is the tree */ |
| 3190 | for (si = rn->info; si; si = si->next) |
| 3191 | if (distance == si->distance |
| 3192 | && type == si->type |
| 3193 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 3194 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 3195 | break; |
| 3196 | |
| 3197 | /* Can't find static route. */ |
| 3198 | if (! si) |
| 3199 | { |
| 3200 | route_unlock_node (rn); |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
| 3204 | /* Install into rib. */ |
| 3205 | static_uninstall_ipv6 (p, si); |
| 3206 | |
| 3207 | /* Unlink static route from linked list. */ |
| 3208 | if (si->prev) |
| 3209 | si->prev->next = si->next; |
| 3210 | else |
| 3211 | rn->info = si->next; |
| 3212 | if (si->next) |
| 3213 | si->next->prev = si->prev; |
| 3214 | |
| 3215 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 3216 | if (ifname) |
| 3217 | XFREE (0, si->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3218 | XFREE (MTYPE_STATIC_IPV6, si); |
| 3219 | |
| 3220 | return 1; |
| 3221 | } |
| 3222 | #endif /* HAVE_IPV6 */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3223 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3224 | /* RIB update function. */ |
| 3225 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3226 | rib_update (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3227 | { |
| 3228 | struct route_node *rn; |
| 3229 | struct route_table *table; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3231 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 3232 | if (table) |
| 3233 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3234 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3235 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3236 | |
| 3237 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 3238 | if (table) |
| 3239 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3240 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3241 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3244 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3245 | /* Remove all routes which comes from non main table. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3246 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3247 | rib_weed_table (struct route_table *table) |
| 3248 | { |
| 3249 | struct route_node *rn; |
| 3250 | struct rib *rib; |
| 3251 | struct rib *next; |
| 3252 | |
| 3253 | if (table) |
| 3254 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3255 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3256 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3257 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3258 | continue; |
| 3259 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 3260 | if (rib->table != zebrad.rtm_table_default && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3261 | rib->table != RT_TABLE_MAIN) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3262 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | /* Delete all routes from non main table. */ |
| 3267 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3268 | rib_weed_tables (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3269 | { |
| 3270 | rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3271 | rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3272 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3273 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3274 | /* Delete self installed routes after zebra is relaunched. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3275 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3276 | rib_sweep_table (struct route_table *table) |
| 3277 | { |
| 3278 | struct route_node *rn; |
| 3279 | struct rib *rib; |
| 3280 | struct rib *next; |
| 3281 | int ret = 0; |
| 3282 | |
| 3283 | if (table) |
| 3284 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3285 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3286 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3287 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3288 | continue; |
| 3289 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3290 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
| 3291 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) |
| 3292 | { |
| 3293 | ret = rib_uninstall_kernel (rn, rib); |
| 3294 | if (! ret) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3295 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | /* Sweep all RIB tables. */ |
| 3301 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3302 | rib_sweep_route (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3303 | { |
| 3304 | rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3305 | rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3306 | } |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3307 | |
| 3308 | /* Remove specific by protocol routes from 'table'. */ |
| 3309 | static unsigned long |
| 3310 | rib_score_proto_table (u_char proto, struct route_table *table) |
| 3311 | { |
| 3312 | struct route_node *rn; |
| 3313 | struct rib *rib; |
| 3314 | struct rib *next; |
| 3315 | unsigned long n = 0; |
| 3316 | |
| 3317 | if (table) |
| 3318 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3319 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3320 | { |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3321 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3322 | continue; |
| 3323 | if (rib->type == proto) |
| 3324 | { |
| 3325 | rib_delnode (rn, rib); |
| 3326 | n++; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | return n; |
| 3331 | } |
| 3332 | |
| 3333 | /* Remove specific by protocol routes. */ |
| 3334 | unsigned long |
| 3335 | rib_score_proto (u_char proto) |
| 3336 | { |
| 3337 | return rib_score_proto_table (proto, vrf_table (AFI_IP, SAFI_UNICAST, 0)) |
| 3338 | +rib_score_proto_table (proto, vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3339 | } |
| 3340 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3341 | /* Close RIB and clean up kernel routes. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3342 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3343 | rib_close_table (struct route_table *table) |
| 3344 | { |
| 3345 | struct route_node *rn; |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3346 | rib_table_info_t *info = table->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3347 | struct rib *rib; |
| 3348 | |
| 3349 | if (table) |
| 3350 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3351 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3352 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3353 | if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 3354 | continue; |
| 3355 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3356 | if (info->safi == SAFI_UNICAST) |
| 3357 | zfpm_trigger_update (rn, NULL); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 3358 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3359 | if (! RIB_SYSTEM_ROUTE (rib)) |
| 3360 | rib_uninstall_kernel (rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3361 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* Close all RIB tables. */ |
| 3365 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3366 | rib_close (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3367 | { |
| 3368 | rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3369 | rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3370 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3371 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3372 | /* Routing information base initialize. */ |
| 3373 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3374 | rib_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3375 | { |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3376 | rib_queue_init (&zebrad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3377 | /* VRF initialization. */ |
| 3378 | vrf_init (); |
| 3379 | } |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3380 | |
| 3381 | /* |
| 3382 | * vrf_id_get_next |
| 3383 | * |
| 3384 | * Get the first vrf id that is greater than the given vrf id if any. |
| 3385 | * |
| 3386 | * Returns TRUE if a vrf id was found, FALSE otherwise. |
| 3387 | */ |
| 3388 | static inline int |
| 3389 | vrf_id_get_next (uint32_t id, uint32_t *next_id_p) |
| 3390 | { |
| 3391 | while (++id < vector_active (vrf_vector)) |
| 3392 | { |
| 3393 | if (vrf_lookup (id)) |
| 3394 | { |
| 3395 | *next_id_p = id; |
| 3396 | return 1; |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | /* |
| 3404 | * rib_tables_iter_next |
| 3405 | * |
| 3406 | * Returns the next table in the iteration. |
| 3407 | */ |
| 3408 | struct route_table * |
| 3409 | rib_tables_iter_next (rib_tables_iter_t *iter) |
| 3410 | { |
| 3411 | struct route_table *table; |
| 3412 | |
| 3413 | /* |
| 3414 | * Array that helps us go over all AFI/SAFI combinations via one |
| 3415 | * index. |
| 3416 | */ |
| 3417 | static struct { |
| 3418 | afi_t afi; |
| 3419 | safi_t safi; |
| 3420 | } afi_safis[] = { |
| 3421 | { AFI_IP, SAFI_UNICAST }, |
| 3422 | { AFI_IP, SAFI_MULTICAST }, |
| 3423 | { AFI_IP6, SAFI_UNICAST }, |
| 3424 | { AFI_IP6, SAFI_MULTICAST }, |
| 3425 | }; |
| 3426 | |
| 3427 | table = NULL; |
| 3428 | |
| 3429 | switch (iter->state) |
| 3430 | { |
| 3431 | |
| 3432 | case RIB_TABLES_ITER_S_INIT: |
| 3433 | iter->vrf_id = 0; |
| 3434 | iter->afi_safi_ix = -1; |
| 3435 | |
| 3436 | /* Fall through */ |
| 3437 | |
| 3438 | case RIB_TABLES_ITER_S_ITERATING: |
| 3439 | iter->afi_safi_ix++; |
| 3440 | while (1) |
| 3441 | { |
| 3442 | |
| 3443 | while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis)) |
| 3444 | { |
| 3445 | table = vrf_table (afi_safis[iter->afi_safi_ix].afi, |
| 3446 | afi_safis[iter->afi_safi_ix].safi, |
| 3447 | iter->vrf_id); |
| 3448 | if (table) |
| 3449 | break; |
| 3450 | |
| 3451 | iter->afi_safi_ix++; |
| 3452 | } |
| 3453 | |
| 3454 | /* |
| 3455 | * Found another table in this vrf. |
| 3456 | */ |
| 3457 | if (table) |
| 3458 | break; |
| 3459 | |
| 3460 | /* |
| 3461 | * Done with all tables in the current vrf, go to the next |
| 3462 | * one. |
| 3463 | */ |
| 3464 | if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id)) |
| 3465 | break; |
| 3466 | |
| 3467 | iter->afi_safi_ix = 0; |
| 3468 | } |
| 3469 | |
| 3470 | break; |
| 3471 | |
| 3472 | case RIB_TABLES_ITER_S_DONE: |
| 3473 | return NULL; |
| 3474 | } |
| 3475 | |
| 3476 | if (table) |
| 3477 | iter->state = RIB_TABLES_ITER_S_ITERATING; |
| 3478 | else |
| 3479 | iter->state = RIB_TABLES_ITER_S_DONE; |
| 3480 | |
| 3481 | return table; |
| 3482 | } |