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