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