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