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