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 * |
| 201 | nexthop_ifindex_add (struct rib *rib, unsigned int ifindex) |
| 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 | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 246 | struct in_addr *src, unsigned int 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, |
| 294 | unsigned int ifindex) |
| 295 | { |
| 296 | struct nexthop *nexthop; |
| 297 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 298 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 300 | nexthop->gate.ipv6 = *ipv6; |
| 301 | nexthop->ifindex = ifindex; |
| 302 | |
| 303 | nexthop_add (rib, nexthop); |
| 304 | |
| 305 | return nexthop; |
| 306 | } |
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; |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1077 | unsigned int prev_active, prev_index, new_active; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1078 | |
| 1079 | rib->nexthop_active_num = 0; |
Timo Teräs | 7eb6136 | 2015-11-02 16:50:05 +0200 | [diff] [blame] | 1080 | UNSET_FLAG (rib->status, RIB_ENTRY_CHANGED); |
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) |
| 1126 | for (ALL_NEXTHOPS_RO(new->nexthop, nexthop, tnexthop, recursing)) |
| 1127 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1128 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1129 | if (old) |
| 1130 | for (ALL_NEXTHOPS_RO(old->nexthop, nexthop, tnexthop, recursing)) |
| 1131 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1132 | |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | /* Uninstall the route from kernel. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1137 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1138 | rib_uninstall (struct route_node *rn, struct rib *rib) |
| 1139 | { |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1140 | rib_table_info_t *info = rn->table->info; |
| 1141 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1142 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1143 | { |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 1144 | if (info->safi == SAFI_UNICAST) |
| 1145 | zfpm_trigger_update (rn, "rib_uninstall"); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1146 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | redistribute_delete (&rn->p, rib); |
| 1148 | if (! RIB_SYSTEM_ROUTE (rib)) |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1149 | rib_update_kernel (rn, rib, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1154 | static void rib_unlink (struct route_node *, struct rib *); |
| 1155 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1156 | /* |
| 1157 | * rib_can_delete_dest |
| 1158 | * |
| 1159 | * Returns TRUE if the given dest can be deleted from the table. |
| 1160 | */ |
| 1161 | static int |
| 1162 | rib_can_delete_dest (rib_dest_t *dest) |
| 1163 | { |
| 1164 | if (dest->routes) |
| 1165 | { |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1169 | /* |
| 1170 | * Don't delete the dest if we have to update the FPM about this |
| 1171 | * prefix. |
| 1172 | */ |
| 1173 | if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) || |
| 1174 | CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM)) |
| 1175 | return 0; |
| 1176 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * rib_gc_dest |
| 1182 | * |
| 1183 | * Garbage collect the rib dest corresponding to the given route node |
| 1184 | * if appropriate. |
| 1185 | * |
| 1186 | * Returns TRUE if the dest was deleted, FALSE otherwise. |
| 1187 | */ |
| 1188 | int |
| 1189 | rib_gc_dest (struct route_node *rn) |
| 1190 | { |
| 1191 | rib_dest_t *dest; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1192 | |
| 1193 | dest = rib_dest_from_rnode (rn); |
| 1194 | if (!dest) |
| 1195 | return 0; |
| 1196 | |
| 1197 | if (!rib_can_delete_dest (dest)) |
| 1198 | return 0; |
| 1199 | |
| 1200 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1201 | rnode_debug (rn, "removing dest from table"); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1202 | |
| 1203 | dest->rnode = NULL; |
| 1204 | XFREE (MTYPE_RIB_DEST, dest); |
| 1205 | rn->info = NULL; |
| 1206 | |
| 1207 | /* |
| 1208 | * Release the one reference that we keep on the route node. |
| 1209 | */ |
| 1210 | route_unlock_node (rn); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1214 | /* Check if 'alternate' RIB entry is better than 'current'. */ |
| 1215 | static struct rib * |
| 1216 | rib_choose_best (struct rib *current, struct rib *alternate) |
| 1217 | { |
| 1218 | if (current == NULL) |
| 1219 | return alternate; |
| 1220 | |
| 1221 | /* filter route selection in following order: |
| 1222 | * - connected beats other types |
| 1223 | * - lower distance beats higher |
| 1224 | * - lower metric beats higher for equal distance |
| 1225 | * - last, hence oldest, route wins tie break. |
| 1226 | */ |
| 1227 | |
| 1228 | /* Connected routes. Pick the last connected |
| 1229 | * route of the set of lowest metric connected routes. |
| 1230 | */ |
| 1231 | if (alternate->type == ZEBRA_ROUTE_CONNECT) |
| 1232 | { |
| 1233 | if (current->type != ZEBRA_ROUTE_CONNECT |
| 1234 | || alternate->metric <= current->metric) |
| 1235 | return alternate; |
| 1236 | |
| 1237 | return current; |
| 1238 | } |
| 1239 | |
| 1240 | if (current->type == ZEBRA_ROUTE_CONNECT) |
| 1241 | return current; |
| 1242 | |
| 1243 | /* higher distance loses */ |
| 1244 | if (alternate->distance < current->distance) |
| 1245 | return alternate; |
| 1246 | if (current->distance < alternate->distance) |
| 1247 | return current; |
| 1248 | |
| 1249 | /* metric tie-breaks equal distance */ |
| 1250 | if (alternate->metric <= current->metric) |
| 1251 | return alternate; |
| 1252 | |
| 1253 | return current; |
| 1254 | } |
| 1255 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1256 | /* Core function for processing routing information base. */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1257 | static void |
| 1258 | rib_process (struct route_node *rn) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1259 | { |
| 1260 | struct rib *rib; |
| 1261 | struct rib *next; |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1262 | struct rib *old_selected = NULL; |
| 1263 | struct rib *new_selected = NULL; |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1264 | struct rib *old_fib = NULL; |
| 1265 | struct rib *new_fib = NULL; |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1266 | int installed = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1267 | struct nexthop *nexthop = NULL, *tnexthop; |
| 1268 | int recursing; |
Balaji | 9511633 | 2014-10-23 15:25:25 +0000 | [diff] [blame] | 1269 | rib_table_info_t *info; |
| 1270 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1271 | assert (rn); |
Balaji | 9511633 | 2014-10-23 15:25:25 +0000 | [diff] [blame] | 1272 | |
| 1273 | info = rn->table->info; |
| 1274 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1275 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1276 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | /* Currently installed rib. */ |
| 1278 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1279 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1280 | assert (old_selected == NULL); |
| 1281 | old_selected = rib; |
| 1282 | } |
| 1283 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
| 1284 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1285 | assert (old_fib == NULL); |
| 1286 | old_fib = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1287 | } |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1288 | |
| 1289 | /* Skip deleted entries from selection */ |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1290 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1291 | continue; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1292 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1293 | /* Skip unreachable nexthop. */ |
| 1294 | if (! nexthop_active_update (rn, rib, 0)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1295 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* Infinit distance. */ |
| 1298 | if (rib->distance == DISTANCE_INFINITY) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1299 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1300 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1301 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_FIB_OVERRIDE)) |
| 1302 | new_fib = rib_choose_best(new_fib, rib); |
| 1303 | else |
| 1304 | new_selected = rib_choose_best(new_selected, rib); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1305 | } /* RNODE_FOREACH_RIB_SAFE */ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1306 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1307 | /* If no FIB override route, use the selected route also for FIB */ |
| 1308 | if (new_fib == NULL) |
| 1309 | new_fib = new_selected; |
| 1310 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1311 | /* After the cycle is finished, the following pointers will be set: |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1312 | * old_selected --- RIB entry currently having SELECTED |
| 1313 | * new_selected --- RIB entry that is newly SELECTED |
| 1314 | * old_fib --- RIB entry currently in kernel FIB |
| 1315 | * new_fib --- RIB entry that is newly to be in kernel FIB |
| 1316 | * |
| 1317 | * new_selected will get SELECTED flag, and is going to be redistributed |
| 1318 | * 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] | 1319 | */ |
| 1320 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1321 | /* Set real nexthops. */ |
| 1322 | if (new_fib) |
| 1323 | nexthop_active_update (rn, new_fib, 1); |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1324 | if (new_selected && new_selected != new_fib) |
| 1325 | nexthop_active_update (rn, new_selected, 1); |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1326 | |
| 1327 | /* Update kernel if FIB entry has changed */ |
| 1328 | if (old_fib != new_fib |
| 1329 | || (new_fib && CHECK_FLAG (new_fib->status, RIB_ENTRY_CHANGED))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1330 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1331 | if (old_fib && old_fib != new_fib) |
| 1332 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1333 | if (! RIB_SYSTEM_ROUTE (old_fib) && (! new_fib || RIB_SYSTEM_ROUTE (new_fib))) |
| 1334 | rib_update_kernel (rn, old_fib, NULL); |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1335 | UNSET_FLAG (old_fib->status, RIB_ENTRY_SELECTED_FIB); |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (new_fib) |
| 1339 | { |
| 1340 | /* Install new or replace existing FIB entry */ |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1341 | SET_FLAG (new_fib->status, RIB_ENTRY_SELECTED_FIB); |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1342 | if (! RIB_SYSTEM_ROUTE (new_fib)) |
| 1343 | rib_update_kernel (rn, old_fib, new_fib); |
| 1344 | } |
| 1345 | |
| 1346 | if (info->safi == SAFI_UNICAST) |
| 1347 | zfpm_trigger_update (rn, "updating existing route"); |
| 1348 | } |
| 1349 | else if (old_fib == new_fib && new_fib && ! RIB_SYSTEM_ROUTE (new_fib)) |
| 1350 | { |
| 1351 | /* Housekeeping code to deal with race conditions in kernel with |
| 1352 | * linux netlink reporting interface up before IPv4 or IPv6 protocol |
| 1353 | * is ready to add routes. This makes sure routes are IN the kernel. |
| 1354 | */ |
| 1355 | for (ALL_NEXTHOPS_RO(new_fib->nexthop, nexthop, tnexthop, recursing)) |
| 1356 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 1357 | { |
| 1358 | installed = 1; |
| 1359 | break; |
| 1360 | } |
| 1361 | if (! installed) |
| 1362 | rib_update_kernel (rn, NULL, new_fib); |
| 1363 | } |
| 1364 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1365 | /* Redistribute SELECTED entry */ |
| 1366 | if (old_selected != new_selected |
| 1367 | || (new_selected && CHECK_FLAG (new_selected->status, RIB_ENTRY_CHANGED))) |
| 1368 | { |
| 1369 | if (old_selected) |
| 1370 | { |
| 1371 | if (! new_selected) |
| 1372 | redistribute_delete (&rn->p, old_selected); |
| 1373 | if (old_selected != new_selected) |
| 1374 | UNSET_FLAG (old_selected->flags, ZEBRA_FLAG_SELECTED); |
| 1375 | } |
| 1376 | |
| 1377 | if (new_selected) |
| 1378 | { |
| 1379 | /* Install new or replace existing redistributed entry */ |
| 1380 | SET_FLAG (new_selected->flags, ZEBRA_FLAG_SELECTED); |
| 1381 | redistribute_add (&rn->p, new_selected); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1385 | /* Remove all RIB entries queued for removal */ |
| 1386 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
| 1387 | { |
| 1388 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1389 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1390 | if (IS_ZEBRA_DEBUG_RIB) |
| 1391 | rnode_debug (rn, "rn %p, removing rib %p", |
| 1392 | (void *)rn, (void *)rib); |
| 1393 | rib_unlink (rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1394 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1397 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1398 | rnode_debug (rn, "rn %p dequeued", (void *)rn); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* |
| 1401 | * Check if the dest can be deleted now. |
| 1402 | */ |
| 1403 | rib_gc_dest (rn); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1406 | /* Take a list of route_node structs and return 1, if there was a record |
| 1407 | * picked from it and processed by rib_process(). Don't process more, |
| 1408 | * than one RN record; operate only in the specified sub-queue. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1409 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1410 | static unsigned int |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1411 | process_subq (struct list * subq, u_char qindex) |
| 1412 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1413 | struct listnode *lnode = listhead (subq); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1414 | struct route_node *rnode; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1415 | |
| 1416 | if (!lnode) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1417 | return 0; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1418 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1419 | rnode = listgetdata (lnode); |
| 1420 | rib_process (rnode); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1421 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1422 | if (rnode->info) |
| 1423 | UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex)); |
| 1424 | |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1425 | #if 0 |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1426 | else |
| 1427 | { |
| 1428 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
| 1429 | __func__, rnode, rnode->lock); |
| 1430 | zlog_backtrace(LOG_DEBUG); |
| 1431 | } |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1432 | #endif |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1433 | route_unlock_node (rnode); |
| 1434 | list_delete_node (subq, lnode); |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | /* Dispatch the meta queue by picking, processing and unlocking the next RN from |
| 1439 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data |
| 1440 | * is pointed to the meta queue structure. |
| 1441 | */ |
| 1442 | static wq_item_status |
| 1443 | meta_queue_process (struct work_queue *dummy, void *data) |
| 1444 | { |
| 1445 | struct meta_queue * mq = data; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1446 | unsigned i; |
| 1447 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1448 | for (i = 0; i < MQ_SIZE; i++) |
| 1449 | if (process_subq (mq->subq[i], i)) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1450 | { |
| 1451 | mq->size--; |
| 1452 | break; |
| 1453 | } |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1454 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; |
| 1455 | } |
| 1456 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1457 | /* |
| 1458 | * Map from rib types to queue type (priority) in meta queue |
| 1459 | */ |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1460 | static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = { |
| 1461 | [ZEBRA_ROUTE_SYSTEM] = 4, |
| 1462 | [ZEBRA_ROUTE_KERNEL] = 0, |
| 1463 | [ZEBRA_ROUTE_CONNECT] = 0, |
| 1464 | [ZEBRA_ROUTE_STATIC] = 1, |
| 1465 | [ZEBRA_ROUTE_RIP] = 2, |
| 1466 | [ZEBRA_ROUTE_RIPNG] = 2, |
| 1467 | [ZEBRA_ROUTE_OSPF] = 2, |
| 1468 | [ZEBRA_ROUTE_OSPF6] = 2, |
| 1469 | [ZEBRA_ROUTE_ISIS] = 2, |
| 1470 | [ZEBRA_ROUTE_BGP] = 3, |
| 1471 | [ZEBRA_ROUTE_HSLS] = 4, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1472 | [ZEBRA_ROUTE_BABEL] = 2, |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1473 | }; |
| 1474 | |
| 1475 | /* Look into the RN and queue it into one or more priority queues, |
| 1476 | * increasing the size for each data push done. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1477 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1478 | static void |
| 1479 | rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1480 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1481 | struct rib *rib; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1482 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1483 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1484 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1485 | u_char qindex = meta_queue_map[rib->type]; |
| 1486 | |
| 1487 | /* Invariant: at this point we always have rn->info set. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1488 | if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags, |
| 1489 | RIB_ROUTE_QUEUED (qindex))) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1490 | { |
| 1491 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1492 | rnode_debug (rn, "rn %p is already queued in sub-queue %u", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1493 | (void *)rn, qindex); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1494 | continue; |
| 1495 | } |
| 1496 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1497 | SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex)); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1498 | listnode_add (mq->subq[qindex], rn); |
| 1499 | route_lock_node (rn); |
| 1500 | mq->size++; |
| 1501 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1502 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1503 | rnode_debug (rn, "queued rn %p into sub-queue %u", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1504 | (void *)rn, qindex); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1505 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1508 | /* Add route_node to work queue and schedule processing */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1509 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1510 | rib_queue_add (struct zebra_t *zebra, struct route_node *rn) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1511 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1512 | assert (zebra && rn); |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1513 | |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1514 | /* 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] | 1515 | if (!rnode_to_ribs (rn)) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1516 | { |
| 1517 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1518 | __func__, (void *)rn, rn->lock); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1519 | zlog_backtrace(LOG_DEBUG); |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1524 | rnode_info (rn, "work queue added"); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1525 | |
| 1526 | assert (zebra); |
| 1527 | |
| 1528 | if (zebra->ribq == NULL) |
| 1529 | { |
| 1530 | zlog_err ("%s: work_queue does not exist!", __func__); |
| 1531 | return; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1532 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1533 | |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1534 | /* |
| 1535 | * The RIB queue should normally be either empty or holding the only |
| 1536 | * work_queue_item element. In the latter case this element would |
| 1537 | * hold a pointer to the meta queue structure, which must be used to |
| 1538 | * actually queue the route nodes to process. So create the MQ |
| 1539 | * holder, if necessary, then push the work into it in any case. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1540 | * This semantics was introduced after 0.99.9 release. |
| 1541 | */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1542 | if (!zebra->ribq->items->count) |
| 1543 | work_queue_add (zebra->ribq, zebra->mq); |
| 1544 | |
| 1545 | rib_meta_queue_add (zebra->mq, rn); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1546 | |
| 1547 | if (IS_ZEBRA_DEBUG_RIB_Q) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1548 | rnode_debug (rn, "rn %p queued", (void *)rn); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1549 | |
| 1550 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1553 | /* Create new meta queue. |
| 1554 | A destructor function doesn't seem to be necessary here. |
| 1555 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1556 | static struct meta_queue * |
| 1557 | meta_queue_new (void) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1558 | { |
| 1559 | struct meta_queue *new; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1560 | unsigned i; |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1561 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1562 | new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue)); |
| 1563 | assert(new); |
| 1564 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1565 | for (i = 0; i < MQ_SIZE; i++) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1566 | { |
| 1567 | new->subq[i] = list_new (); |
| 1568 | assert(new->subq[i]); |
| 1569 | } |
| 1570 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1571 | return new; |
| 1572 | } |
| 1573 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1574 | /* initialise zebra rib work queue */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1575 | static void |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1576 | rib_queue_init (struct zebra_t *zebra) |
| 1577 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1578 | assert (zebra); |
| 1579 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1580 | if (! (zebra->ribq = work_queue_new (zebra->master, |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1581 | "route_node processing"))) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1582 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1583 | zlog_err ("%s: could not initialise work queue!", __func__); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1584 | return; |
| 1585 | } |
| 1586 | |
| 1587 | /* fill in the work queue spec */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1588 | zebra->ribq->spec.workfunc = &meta_queue_process; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1589 | zebra->ribq->spec.errorfunc = NULL; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1590 | /* XXX: TODO: These should be runtime configurable via vty */ |
| 1591 | zebra->ribq->spec.max_retries = 3; |
Paul Jakma | 457eb9a | 2006-07-27 19:59:58 +0000 | [diff] [blame] | 1592 | zebra->ribq->spec.hold = rib_process_hold_time; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1593 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1594 | if (!(zebra->mq = meta_queue_new ())) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1595 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1596 | zlog_err ("%s: could not initialise meta queue!", __func__); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1597 | return; |
| 1598 | } |
| 1599 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1602 | /* RIB updates are processed via a queue of pointers to route_nodes. |
| 1603 | * |
| 1604 | * The queue length is bounded by the maximal size of the routing table, |
| 1605 | * as a route_node will not be requeued, if already queued. |
| 1606 | * |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1607 | * RIBs are submitted via rib_addnode or rib_delnode which set minimal |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 1608 | * state, or static_install_route (when an existing RIB is updated) |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1609 | * and then submit route_node to queue for best-path selection later. |
| 1610 | * Order of add/delete state changes are preserved for any given RIB. |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1611 | * |
| 1612 | * Deleted RIBs are reaped during best-path selection. |
| 1613 | * |
| 1614 | * rib_addnode |
| 1615 | * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1616 | * |-------->| | best RIB, if required |
| 1617 | * | | |
| 1618 | * static_install->|->rib_addqueue...... -> rib_process |
| 1619 | * | | |
| 1620 | * |-------->| |-> rib_unlink |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1621 | * |-> set RIB_ENTRY_REMOVE | |
| 1622 | * rib_delnode (RIB freed) |
| 1623 | * |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1624 | * The 'info' pointer of a route_node points to a rib_dest_t |
| 1625 | * ('dest'). Queueing state for a route_node is kept on the dest. The |
| 1626 | * dest is created on-demand by rib_link() and is kept around at least |
| 1627 | * 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] | 1628 | * |
| 1629 | * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code): |
| 1630 | * |
| 1631 | * - route_nodes: refcounted by: |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1632 | * - dest attached to route_node: |
| 1633 | * - managed by: rib_link/rib_gc_dest |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1634 | * - route_node processing queue |
| 1635 | * - managed by: rib_addqueue, rib_process. |
| 1636 | * |
| 1637 | */ |
| 1638 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1639 | /* Add RIB to head of the route node. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1640 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1641 | rib_link (struct route_node *rn, struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1642 | { |
| 1643 | struct rib *head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1644 | rib_dest_t *dest; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1645 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1646 | assert (rib && rn); |
| 1647 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1648 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1649 | rnode_debug (rn, "rn %p, rib %p", (void *)rn, (void *)rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1650 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1651 | dest = rib_dest_from_rnode (rn); |
| 1652 | if (!dest) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1653 | { |
| 1654 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | 9481374 | 2014-04-24 20:22:53 +0200 | [diff] [blame] | 1655 | rnode_debug (rn, "adding dest to table"); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1656 | |
| 1657 | dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t)); |
| 1658 | route_lock_node (rn); /* rn route table reference */ |
| 1659 | rn->info = dest; |
| 1660 | dest->rnode = rn; |
| 1661 | } |
| 1662 | |
| 1663 | head = dest->routes; |
| 1664 | if (head) |
| 1665 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1666 | head->prev = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1667 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1668 | rib->next = head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1669 | dest->routes = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1670 | rib_queue_add (&zebrad, rn); |
| 1671 | } |
| 1672 | |
| 1673 | static void |
| 1674 | rib_addnode (struct route_node *rn, struct rib *rib) |
| 1675 | { |
| 1676 | /* RIB node has been un-removed before route-node is processed. |
| 1677 | * route_node must hence already be on the queue for processing.. |
| 1678 | */ |
| 1679 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1680 | { |
| 1681 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1682 | 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] | 1683 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1684 | UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1685 | return; |
| 1686 | } |
| 1687 | rib_link (rn, rib); |
| 1688 | } |
| 1689 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1690 | /* |
| 1691 | * rib_unlink |
| 1692 | * |
| 1693 | * Detach a rib structure from a route_node. |
| 1694 | * |
| 1695 | * Note that a call to rib_unlink() should be followed by a call to |
| 1696 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no |
| 1697 | * longer required to be deleted. |
| 1698 | */ |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1699 | static void |
| 1700 | rib_unlink (struct route_node *rn, struct rib *rib) |
| 1701 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1702 | rib_dest_t *dest; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1703 | |
| 1704 | assert (rn && rib); |
| 1705 | |
| 1706 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1707 | rnode_debug (rn, "rn %p, rib %p", (void *)rn, (void *)rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1708 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1709 | dest = rib_dest_from_rnode (rn); |
| 1710 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1711 | if (rib->next) |
| 1712 | rib->next->prev = rib->prev; |
| 1713 | |
| 1714 | if (rib->prev) |
| 1715 | rib->prev->next = rib->next; |
| 1716 | else |
| 1717 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1718 | dest->routes = rib->next; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | /* free RIB and nexthops */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1722 | nexthops_free(rib->nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1723 | XFREE (MTYPE_RIB, rib); |
| 1724 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1727 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1728 | rib_delnode (struct route_node *rn, struct rib *rib) |
| 1729 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1730 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1731 | rnode_debug (rn, "rn %p, rib %p, removing", (void *)rn, (void *)rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1732 | SET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1733 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | int |
| 1737 | rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1738 | struct in_addr *gate, struct in_addr *src, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1739 | unsigned int ifindex, vrf_id_t vrf_id, int table_id, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1740 | 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] | 1741 | { |
| 1742 | struct rib *rib; |
| 1743 | struct rib *same = NULL; |
| 1744 | struct route_table *table; |
| 1745 | struct route_node *rn; |
| 1746 | struct nexthop *nexthop; |
| 1747 | |
| 1748 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1749 | table = zebra_vrf_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1750 | if (! table) |
| 1751 | return 0; |
| 1752 | |
| 1753 | /* Make it sure prefixlen is applied to the prefix. */ |
| 1754 | apply_mask_ipv4 (p); |
| 1755 | |
| 1756 | /* Set default distance by route type. */ |
| 1757 | if (distance == 0) |
| 1758 | { |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 1759 | if ((unsigned)type >= array_size(route_info)) |
David Lamparter | 7052f22 | 2009-08-27 00:28:28 +0200 | [diff] [blame] | 1760 | distance = 150; |
| 1761 | else |
| 1762 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1763 | |
| 1764 | /* iBGP distance is 200. */ |
| 1765 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 1766 | distance = 200; |
| 1767 | } |
| 1768 | |
| 1769 | /* Lookup route node.*/ |
| 1770 | rn = route_node_get (table, (struct prefix *) p); |
| 1771 | |
| 1772 | /* If same type of route are installed, treat it as a implicit |
| 1773 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1774 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1775 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1776 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1777 | continue; |
| 1778 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1779 | if (rib->type != type) |
| 1780 | continue; |
| 1781 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1782 | { |
| 1783 | same = rib; |
| 1784 | break; |
| 1785 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1786 | /* Duplicate connected route comes in. */ |
| 1787 | else if ((nexthop = rib->nexthop) && |
| 1788 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1789 | nexthop->ifindex == ifindex && |
| 1790 | !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1791 | { |
| 1792 | rib->refcnt++; |
| 1793 | return 0 ; |
| 1794 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1798 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1799 | rib->type = type; |
| 1800 | rib->distance = distance; |
| 1801 | rib->flags = flags; |
| 1802 | rib->metric = metric; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1803 | rib->mtu = mtu; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1804 | rib->vrf_id = vrf_id; |
| 1805 | rib->table = table_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1806 | rib->nexthop_num = 0; |
| 1807 | rib->uptime = time (NULL); |
| 1808 | |
| 1809 | /* Nexthop settings. */ |
| 1810 | if (gate) |
| 1811 | { |
| 1812 | if (ifindex) |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1813 | nexthop_ipv4_ifindex_add (rib, gate, src, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1814 | else |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1815 | nexthop_ipv4_add (rib, gate, src); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1816 | } |
| 1817 | else |
| 1818 | nexthop_ifindex_add (rib, ifindex); |
| 1819 | |
| 1820 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1821 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 1822 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1823 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1824 | |
| 1825 | /* Link new rib to node.*/ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1826 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1827 | zlog_debug ("%s: calling rib_addnode (%p, %p)", |
| 1828 | __func__, (void *)rn, (void *)rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | rib_addnode (rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1830 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1831 | /* Free implicit route.*/ |
| 1832 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1833 | { |
| 1834 | if (IS_ZEBRA_DEBUG_RIB) |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1835 | zlog_debug ("%s: calling rib_delnode (%p, %p)", |
| 1836 | __func__, (void *)rn, (void *)rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1837 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1838 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1839 | |
| 1840 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1841 | return 0; |
| 1842 | } |
| 1843 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1844 | /* This function dumps the contents of a given RIB entry into |
| 1845 | * standard debug log. Calling function name and IP prefix in |
| 1846 | * question are passed as 1st and 2nd arguments. |
| 1847 | */ |
| 1848 | |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1849 | void _rib_dump (const char * func, |
| 1850 | union prefix46constptr pp, const struct rib * rib) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1851 | { |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1852 | const struct prefix *p = pp.p; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1853 | char straddr[PREFIX_STRLEN]; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1854 | struct nexthop *nexthop, *tnexthop; |
| 1855 | int recursing; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1856 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1857 | zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func, (void *)rib, |
| 1858 | prefix2str(p, straddr, sizeof(straddr)), rib->vrf_id); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1859 | zlog_debug |
| 1860 | ( |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1861 | "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d", |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1862 | func, |
| 1863 | rib->refcnt, |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1864 | (unsigned long) rib->uptime, |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1865 | rib->type, |
| 1866 | rib->table |
| 1867 | ); |
| 1868 | zlog_debug |
| 1869 | ( |
| 1870 | "%s: metric == %u, distance == %u, flags == %u, status == %u", |
| 1871 | func, |
| 1872 | rib->metric, |
| 1873 | rib->distance, |
| 1874 | rib->flags, |
| 1875 | rib->status |
| 1876 | ); |
| 1877 | zlog_debug |
| 1878 | ( |
| 1879 | "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u", |
| 1880 | func, |
| 1881 | rib->nexthop_num, |
| 1882 | rib->nexthop_active_num, |
| 1883 | rib->nexthop_fib_num |
| 1884 | ); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 1885 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1886 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1887 | { |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 1888 | inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1889 | zlog_debug |
| 1890 | ( |
| 1891 | "%s: %s %s with flags %s%s%s", |
| 1892 | func, |
| 1893 | (recursing ? " NH" : "NH"), |
| 1894 | straddr, |
| 1895 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""), |
| 1896 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""), |
| 1897 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "") |
| 1898 | ); |
| 1899 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1900 | zlog_debug ("%s: dump complete", func); |
| 1901 | } |
| 1902 | |
| 1903 | /* This is an exported helper to rtm_read() to dump the strange |
| 1904 | * RIB entry found by rib_lookup_ipv4_route() |
| 1905 | */ |
| 1906 | |
| 1907 | void rib_lookup_and_dump (struct prefix_ipv4 * p) |
| 1908 | { |
| 1909 | struct route_table *table; |
| 1910 | struct route_node *rn; |
| 1911 | struct rib *rib; |
| 1912 | char prefix_buf[INET_ADDRSTRLEN]; |
| 1913 | |
| 1914 | /* Lookup table. */ |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 1915 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1916 | if (! table) |
| 1917 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 1918 | zlog_err ("%s: zebra_vrf_table() returned NULL", __func__); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1919 | return; |
| 1920 | } |
| 1921 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1922 | /* Scan the RIB table for exactly matching RIB entry. */ |
| 1923 | rn = route_node_lookup (table, (struct prefix *) p); |
| 1924 | |
| 1925 | /* No route for this prefix. */ |
| 1926 | if (! rn) |
| 1927 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1928 | zlog_debug ("%s: lookup failed for %s", __func__, |
| 1929 | prefix2str((struct prefix*) p, prefix_buf, sizeof(prefix_buf))); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | /* Unlock node. */ |
| 1934 | route_unlock_node (rn); |
| 1935 | |
| 1936 | /* let's go */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1937 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1938 | { |
| 1939 | zlog_debug |
| 1940 | ( |
| 1941 | "%s: rn %p, rib %p: %s, %s", |
| 1942 | __func__, |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1943 | (void *)rn, |
| 1944 | (void *)rib, |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1945 | (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"), |
| 1946 | (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected") |
| 1947 | ); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1948 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1952 | /* Check if requested address assignment will fail due to another |
| 1953 | * route being installed by zebra in FIB already. Take necessary |
| 1954 | * actions, if needed: remove such a route from FIB and deSELECT |
| 1955 | * corresponding RIB entry. Then put affected RN into RIBQ head. |
| 1956 | */ |
| 1957 | void rib_lookup_and_pushup (struct prefix_ipv4 * p) |
| 1958 | { |
| 1959 | struct route_table *table; |
| 1960 | struct route_node *rn; |
| 1961 | struct rib *rib; |
| 1962 | unsigned changed = 0; |
| 1963 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 1964 | if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT))) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1965 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 1966 | zlog_err ("%s: zebra_vrf_table() returned NULL", __func__); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | /* No matches would be the simplest case. */ |
| 1971 | if (NULL == (rn = route_node_lookup (table, (struct prefix *) p))) |
| 1972 | return; |
| 1973 | |
| 1974 | /* Unlock node. */ |
| 1975 | route_unlock_node (rn); |
| 1976 | |
| 1977 | /* Check all RIB entries. In case any changes have to be done, requeue |
| 1978 | * the RN into RIBQ head. If the routing message about the new connected |
| 1979 | * route (generated by the IP address we are going to assign very soon) |
| 1980 | * comes before the RIBQ is processed, the new RIB entry will join |
| 1981 | * RIBQ record already on head. This is necessary for proper revalidation |
| 1982 | * of the rest of the RIB. |
| 1983 | */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1984 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1985 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 1986 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB) && |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1987 | ! RIB_SYSTEM_ROUTE (rib)) |
| 1988 | { |
| 1989 | changed = 1; |
| 1990 | if (IS_ZEBRA_DEBUG_RIB) |
| 1991 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1992 | char buf[PREFIX_STRLEN]; |
| 1993 | zlog_debug ("%s: freeing way for connected prefix %s", __func__, |
| 1994 | prefix2str(&rn->p, buf, sizeof(buf))); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1995 | rib_dump (&rn->p, rib); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1996 | } |
| 1997 | rib_uninstall (rn, rib); |
| 1998 | } |
| 1999 | } |
| 2000 | if (changed) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2001 | rib_queue_add (&zebrad, rn); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2004 | int |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2005 | 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] | 2006 | { |
| 2007 | struct route_table *table; |
| 2008 | struct route_node *rn; |
| 2009 | struct rib *same; |
| 2010 | struct nexthop *nexthop; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2011 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2012 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2013 | table = zebra_vrf_table (AFI_IP, safi, rib->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2014 | if (! table) |
| 2015 | return 0; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2016 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2017 | /* Make it sure prefixlen is applied to the prefix. */ |
| 2018 | apply_mask_ipv4 (p); |
| 2019 | |
| 2020 | /* Set default distance by route type. */ |
| 2021 | if (rib->distance == 0) |
| 2022 | { |
| 2023 | rib->distance = route_info[rib->type].distance; |
| 2024 | |
| 2025 | /* iBGP distance is 200. */ |
| 2026 | if (rib->type == ZEBRA_ROUTE_BGP |
| 2027 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 2028 | rib->distance = 200; |
| 2029 | } |
| 2030 | |
| 2031 | /* Lookup route node.*/ |
| 2032 | rn = route_node_get (table, (struct prefix *) p); |
| 2033 | |
| 2034 | /* If same type of route are installed, treat it as a implicit |
| 2035 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2036 | RNODE_FOREACH_RIB (rn, same) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2037 | { |
Paul Jakma | 0b8c4f1 | 2007-06-27 11:12:38 +0000 | [diff] [blame] | 2038 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2039 | continue; |
| 2040 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2041 | if (same->type == rib->type && same->table == rib->table |
| 2042 | && same->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2043 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2044 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2045 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2046 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2047 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) |
| 2048 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2049 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2050 | |
| 2051 | /* Link new rib to node.*/ |
| 2052 | rib_addnode (rn, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2053 | if (IS_ZEBRA_DEBUG_RIB) |
| 2054 | { |
| 2055 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2056 | __func__, (void *)rn, (void *)rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2057 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2058 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2059 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2060 | /* Free implicit route.*/ |
| 2061 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2062 | { |
| 2063 | if (IS_ZEBRA_DEBUG_RIB) |
| 2064 | { |
| 2065 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2066 | __func__, (void *)rn, (void *)same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2067 | rib_dump (p, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2068 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2069 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2070 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2071 | |
| 2072 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2073 | return 0; |
| 2074 | } |
| 2075 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2076 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2077 | int |
| 2078 | rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2079 | struct in_addr *gate, unsigned int ifindex, vrf_id_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2080 | { |
| 2081 | struct route_table *table; |
| 2082 | struct route_node *rn; |
| 2083 | struct rib *rib; |
| 2084 | struct rib *fib = NULL; |
| 2085 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2086 | struct nexthop *nexthop, *tnexthop; |
| 2087 | int recursing; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 2088 | char buf1[PREFIX_STRLEN]; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2089 | char buf2[INET_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2090 | |
| 2091 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2092 | table = zebra_vrf_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2093 | if (! table) |
| 2094 | return 0; |
| 2095 | |
| 2096 | /* Apply mask. */ |
| 2097 | apply_mask_ipv4 (p); |
| 2098 | |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2099 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2100 | { |
| 2101 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2102 | zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u via %s ifindex %d", |
| 2103 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2104 | inet_ntoa (*gate), |
| 2105 | ifindex); |
| 2106 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2107 | zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u ifindex %d", |
| 2108 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2109 | ifindex); |
| 2110 | } |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2111 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2112 | /* Lookup route node. */ |
| 2113 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2114 | if (! rn) |
| 2115 | { |
| 2116 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2117 | { |
| 2118 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2119 | zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib", |
| 2120 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2121 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2122 | ifindex); |
| 2123 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2124 | zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib", |
| 2125 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2126 | ifindex); |
| 2127 | } |
| 2128 | return ZEBRA_ERR_RTNOEXIST; |
| 2129 | } |
| 2130 | |
| 2131 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2132 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2133 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2134 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2135 | continue; |
| 2136 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 2137 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2138 | fib = rib; |
| 2139 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2140 | if (rib->type != type) |
| 2141 | continue; |
| 2142 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2143 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2144 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2145 | if (nexthop->ifindex != ifindex) |
| 2146 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2147 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2148 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2149 | rib->refcnt--; |
| 2150 | route_unlock_node (rn); |
| 2151 | route_unlock_node (rn); |
| 2152 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2153 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2154 | same = rib; |
| 2155 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2156 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2157 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2158 | else |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2159 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2160 | if (gate == NULL) |
| 2161 | { |
| 2162 | same = rib; |
| 2163 | break; |
| 2164 | } |
| 2165 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2166 | if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate)) |
| 2167 | { |
| 2168 | same = rib; |
| 2169 | break; |
| 2170 | } |
| 2171 | if (same) |
| 2172 | break; |
| 2173 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2174 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2175 | /* If same type of route can't be found and this message is from |
| 2176 | kernel. */ |
| 2177 | if (! same) |
| 2178 | { |
| 2179 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2180 | { |
| 2181 | /* Unset flags. */ |
| 2182 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2183 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2184 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 2185 | UNSET_FLAG (fib->status, RIB_ENTRY_SELECTED_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2186 | } |
| 2187 | else |
| 2188 | { |
| 2189 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2190 | { |
| 2191 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2192 | zlog_debug ("route %s vrf %u via %s ifindex %d type %d " |
| 2193 | "doesn't exist in rib", |
| 2194 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2195 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2196 | ifindex, |
| 2197 | type); |
| 2198 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2199 | zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib", |
| 2200 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2201 | ifindex, |
| 2202 | type); |
| 2203 | } |
| 2204 | route_unlock_node (rn); |
| 2205 | return ZEBRA_ERR_RTNOEXIST; |
| 2206 | } |
| 2207 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2208 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2209 | if (same) |
| 2210 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2211 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2212 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2213 | return 0; |
| 2214 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2215 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2216 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2217 | static void |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2218 | 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] | 2219 | { |
| 2220 | struct rib *rib; |
| 2221 | struct route_node *rn; |
| 2222 | struct route_table *table; |
| 2223 | |
| 2224 | /* Lookup table. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2225 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2226 | if (! table) |
| 2227 | return; |
| 2228 | |
| 2229 | /* Lookup existing route */ |
| 2230 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2231 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2232 | { |
| 2233 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2234 | continue; |
| 2235 | |
| 2236 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2237 | break; |
| 2238 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2239 | |
| 2240 | if (rib) |
| 2241 | { |
| 2242 | /* Same distance static route is there. Update it with new |
| 2243 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2244 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2245 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2246 | { |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2247 | case STATIC_IPV4_GATEWAY: |
| 2248 | nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
| 2249 | break; |
| 2250 | case STATIC_IPV4_IFNAME: |
| 2251 | nexthop_ifname_add (rib, si->ifname); |
| 2252 | break; |
| 2253 | case STATIC_IPV4_BLACKHOLE: |
| 2254 | nexthop_blackhole_add (rib); |
| 2255 | break; |
| 2256 | case STATIC_IPV6_GATEWAY: |
| 2257 | nexthop_ipv6_add (rib, &si->addr.ipv6); |
| 2258 | break; |
| 2259 | case STATIC_IPV6_IFNAME: |
| 2260 | nexthop_ifname_add (rib, si->ifname); |
| 2261 | break; |
| 2262 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2263 | nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname); |
| 2264 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2265 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2266 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2267 | } |
| 2268 | else |
| 2269 | { |
| 2270 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2271 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2272 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2273 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2274 | rib->distance = si->distance; |
| 2275 | rib->metric = 0; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2276 | rib->vrf_id = si->vrf_id; |
Nolan Leake | b0145dd | 2012-09-13 17:17:31 +0000 | [diff] [blame] | 2277 | rib->table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2278 | rib->nexthop_num = 0; |
| 2279 | |
| 2280 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2281 | { |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2282 | case STATIC_IPV4_GATEWAY: |
| 2283 | nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
| 2284 | break; |
| 2285 | case STATIC_IPV4_IFNAME: |
| 2286 | nexthop_ifname_add (rib, si->ifname); |
| 2287 | break; |
| 2288 | case STATIC_IPV4_BLACKHOLE: |
| 2289 | nexthop_blackhole_add (rib); |
| 2290 | break; |
| 2291 | case STATIC_IPV6_GATEWAY: |
| 2292 | nexthop_ipv6_add (rib, &si->addr.ipv6); |
| 2293 | break; |
| 2294 | case STATIC_IPV6_IFNAME: |
| 2295 | nexthop_ifname_add (rib, si->ifname); |
| 2296 | break; |
| 2297 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2298 | nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname); |
| 2299 | break; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2300 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2301 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2302 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2303 | rib->flags = si->flags; |
| 2304 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2305 | /* Link this rib to the tree. */ |
| 2306 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2310 | static int |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2311 | static_nexthop_same (struct nexthop *nexthop, struct static_route *si) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2312 | { |
| 2313 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 2314 | && si->type == STATIC_IPV4_GATEWAY |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2315 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2316 | return 1; |
| 2317 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2318 | && si->type == STATIC_IPV4_IFNAME |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2319 | && strcmp (nexthop->ifname, si->ifname) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2320 | return 1; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2321 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
| 2322 | && si->type == STATIC_IPV4_BLACKHOLE) |
| 2323 | return 1; |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2324 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 2325 | && si->type == STATIC_IPV6_GATEWAY |
| 2326 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)) |
| 2327 | return 1; |
| 2328 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2329 | && si->type == STATIC_IPV6_IFNAME |
| 2330 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2331 | return 1; |
| 2332 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 2333 | && si->type == STATIC_IPV6_GATEWAY_IFNAME |
| 2334 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6) |
| 2335 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2336 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 2337 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | /* Uninstall static route from RIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2341 | static void |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2342 | 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] | 2343 | { |
| 2344 | struct route_node *rn; |
| 2345 | struct rib *rib; |
| 2346 | struct nexthop *nexthop; |
| 2347 | struct route_table *table; |
| 2348 | |
| 2349 | /* Lookup table. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2350 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2351 | if (! table) |
| 2352 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2353 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2354 | /* Lookup existing route with type and distance. */ |
| 2355 | rn = route_node_lookup (table, p); |
| 2356 | if (! rn) |
| 2357 | return; |
| 2358 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2359 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2360 | { |
| 2361 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2362 | continue; |
| 2363 | |
| 2364 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2365 | break; |
| 2366 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2367 | |
| 2368 | if (! rib) |
| 2369 | { |
| 2370 | route_unlock_node (rn); |
| 2371 | return; |
| 2372 | } |
| 2373 | |
| 2374 | /* Lookup nexthop. */ |
| 2375 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2376 | if (static_nexthop_same (nexthop, si)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | |
| 2379 | /* Can't find nexthop. */ |
| 2380 | if (! nexthop) |
| 2381 | { |
| 2382 | route_unlock_node (rn); |
| 2383 | return; |
| 2384 | } |
| 2385 | |
| 2386 | /* Check nexthop. */ |
| 2387 | if (rib->nexthop_num == 1) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2388 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2389 | else |
| 2390 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 2391 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 2392 | rib_uninstall (rn, rib); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 2393 | nexthop_delete (rib, nexthop); |
| 2394 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2395 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2396 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2397 | /* Unlock node. */ |
| 2398 | route_unlock_node (rn); |
| 2399 | } |
| 2400 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2401 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2402 | static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate, |
| 2403 | const char *ifname, u_char flags, u_char distance, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2404 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2405 | { |
| 2406 | u_char type = 0; |
| 2407 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2408 | struct static_route *si; |
| 2409 | struct static_route *pp; |
| 2410 | struct static_route *cp; |
| 2411 | struct static_route *update = NULL; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2412 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
| 2413 | struct route_table *stable = zvrf->stable[AFI_IP][safi]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2414 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2415 | if (! stable) |
| 2416 | return -1; |
| 2417 | |
| 2418 | /* Lookup static route prefix. */ |
| 2419 | rn = route_node_get (stable, p); |
| 2420 | |
| 2421 | /* Make flags. */ |
| 2422 | if (gate) |
| 2423 | type = STATIC_IPV4_GATEWAY; |
paul | 368aa3f | 2003-05-25 23:24:50 +0000 | [diff] [blame] | 2424 | else if (ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2425 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2426 | else |
| 2427 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* Do nothing if there is a same static route. */ |
| 2430 | for (si = rn->info; si; si = si->next) |
| 2431 | { |
| 2432 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2433 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
| 2434 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2435 | { |
| 2436 | if (distance == si->distance) |
| 2437 | { |
| 2438 | route_unlock_node (rn); |
| 2439 | return 0; |
| 2440 | } |
| 2441 | else |
| 2442 | update = si; |
| 2443 | } |
| 2444 | } |
| 2445 | |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2446 | /* Distance changed. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2447 | if (update) |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2448 | static_delete_ipv4_safi (safi, p, gate, ifname, update->distance, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2449 | |
| 2450 | /* Make new static route structure. */ |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2451 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2452 | |
| 2453 | si->type = type; |
| 2454 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2455 | si->flags = flags; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2456 | si->vrf_id = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2457 | |
| 2458 | if (gate) |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2459 | si->addr.ipv4 = *gate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2460 | if (ifname) |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2461 | si->ifname = XSTRDUP (0, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2462 | |
| 2463 | /* Add new static route information to the tree with sort by |
| 2464 | distance value and gateway address. */ |
| 2465 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2466 | { |
| 2467 | if (si->distance < cp->distance) |
| 2468 | break; |
| 2469 | if (si->distance > cp->distance) |
| 2470 | continue; |
| 2471 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) |
| 2472 | { |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2473 | if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2474 | break; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2475 | if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2476 | continue; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | /* Make linked list. */ |
| 2481 | if (pp) |
| 2482 | pp->next = si; |
| 2483 | else |
| 2484 | rn->info = si; |
| 2485 | if (cp) |
| 2486 | cp->prev = si; |
| 2487 | si->prev = pp; |
| 2488 | si->next = cp; |
| 2489 | |
| 2490 | /* Install into rib. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2491 | static_install_route (AFI_IP, safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2492 | |
| 2493 | return 1; |
| 2494 | } |
| 2495 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2496 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2497 | 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] | 2498 | const char *ifname, u_char distance, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2499 | { |
| 2500 | u_char type = 0; |
| 2501 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2502 | struct static_route *si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2503 | struct route_table *stable; |
| 2504 | |
| 2505 | /* Lookup table. */ |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 2506 | stable = zebra_vrf_static_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2507 | if (! stable) |
| 2508 | return -1; |
| 2509 | |
| 2510 | /* Lookup static route prefix. */ |
| 2511 | rn = route_node_lookup (stable, p); |
| 2512 | if (! rn) |
| 2513 | return 0; |
| 2514 | |
| 2515 | /* Make flags. */ |
| 2516 | if (gate) |
| 2517 | type = STATIC_IPV4_GATEWAY; |
| 2518 | else if (ifname) |
| 2519 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2520 | else |
| 2521 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2522 | |
| 2523 | /* Find same static route is the tree */ |
| 2524 | for (si = rn->info; si; si = si->next) |
| 2525 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2526 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
| 2527 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | |
| 2530 | /* Can't find static route. */ |
| 2531 | if (! si) |
| 2532 | { |
| 2533 | route_unlock_node (rn); |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | /* Install into rib. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2538 | static_uninstall_route (AFI_IP, safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2539 | |
| 2540 | /* Unlink static route from linked list. */ |
| 2541 | if (si->prev) |
| 2542 | si->prev->next = si->next; |
| 2543 | else |
| 2544 | rn->info = si->next; |
| 2545 | if (si->next) |
| 2546 | si->next->prev = si->prev; |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2547 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2548 | |
| 2549 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2550 | if (ifname) |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2551 | XFREE (0, si->ifname); |
| 2552 | XFREE (MTYPE_STATIC_ROUTE, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2553 | |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2554 | route_unlock_node (rn); |
| 2555 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2556 | return 1; |
| 2557 | } |
| 2558 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2559 | int |
| 2560 | rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2561 | struct in6_addr *gate, unsigned int ifindex, |
| 2562 | vrf_id_t vrf_id, int table_id, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 2563 | 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] | 2564 | { |
| 2565 | struct rib *rib; |
| 2566 | struct rib *same = NULL; |
| 2567 | struct route_table *table; |
| 2568 | struct route_node *rn; |
| 2569 | struct nexthop *nexthop; |
| 2570 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2571 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2572 | table = zebra_vrf_table (AFI_IP6, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2573 | if (! table) |
| 2574 | return 0; |
| 2575 | |
| 2576 | /* Make sure mask is applied. */ |
| 2577 | apply_mask_ipv6 (p); |
| 2578 | |
| 2579 | /* Set default distance by route type. */ |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2580 | if (!distance) |
| 2581 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2582 | |
| 2583 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 2584 | distance = 200; |
| 2585 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2586 | /* Lookup route node.*/ |
| 2587 | rn = route_node_get (table, (struct prefix *) p); |
| 2588 | |
| 2589 | /* If same type of route are installed, treat it as a implicit |
| 2590 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2591 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2592 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2593 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2594 | continue; |
| 2595 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2596 | if (rib->type != type) |
| 2597 | continue; |
| 2598 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2599 | { |
| 2600 | same = rib; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2601 | break; |
| 2602 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2603 | else if ((nexthop = rib->nexthop) && |
| 2604 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
| 2605 | nexthop->ifindex == ifindex) |
| 2606 | { |
| 2607 | rib->refcnt++; |
| 2608 | return 0; |
| 2609 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2613 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2614 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2615 | rib->type = type; |
| 2616 | rib->distance = distance; |
| 2617 | rib->flags = flags; |
| 2618 | rib->metric = metric; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 2619 | rib->mtu = mtu; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2620 | rib->vrf_id = vrf_id; |
| 2621 | rib->table = table_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2622 | rib->nexthop_num = 0; |
| 2623 | rib->uptime = time (NULL); |
| 2624 | |
| 2625 | /* Nexthop settings. */ |
| 2626 | if (gate) |
| 2627 | { |
| 2628 | if (ifindex) |
| 2629 | nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
| 2630 | else |
| 2631 | nexthop_ipv6_add (rib, gate); |
| 2632 | } |
| 2633 | else |
| 2634 | nexthop_ifindex_add (rib, ifindex); |
| 2635 | |
| 2636 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2637 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 2638 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2639 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2640 | |
| 2641 | /* Link new rib to node.*/ |
| 2642 | rib_addnode (rn, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2643 | if (IS_ZEBRA_DEBUG_RIB) |
| 2644 | { |
| 2645 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2646 | __func__, (void *)rn, (void *)rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2647 | rib_dump (p, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2648 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2649 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2650 | /* Free implicit route.*/ |
| 2651 | if (same) |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2652 | { |
| 2653 | if (IS_ZEBRA_DEBUG_RIB) |
| 2654 | { |
| 2655 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2656 | __func__, (void *)rn, (void *)same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2657 | rib_dump (p, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2658 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2659 | rib_delnode (rn, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2660 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2661 | |
| 2662 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2663 | return 0; |
| 2664 | } |
| 2665 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2666 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2667 | int |
| 2668 | rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2669 | struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2670 | { |
| 2671 | struct route_table *table; |
| 2672 | struct route_node *rn; |
| 2673 | struct rib *rib; |
| 2674 | struct rib *fib = NULL; |
| 2675 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2676 | struct nexthop *nexthop, *tnexthop; |
| 2677 | int recursing; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 2678 | char buf1[PREFIX_STRLEN]; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2679 | char buf2[INET6_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2680 | |
| 2681 | /* Apply mask. */ |
| 2682 | apply_mask_ipv6 (p); |
| 2683 | |
| 2684 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2685 | table = zebra_vrf_table (AFI_IP6, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2686 | if (! table) |
| 2687 | return 0; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2688 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2689 | /* Lookup route node. */ |
| 2690 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2691 | if (! rn) |
| 2692 | { |
| 2693 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2694 | { |
| 2695 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2696 | zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib", |
| 2697 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2698 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2699 | ifindex); |
| 2700 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2701 | zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib", |
| 2702 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2703 | ifindex); |
| 2704 | } |
| 2705 | return ZEBRA_ERR_RTNOEXIST; |
| 2706 | } |
| 2707 | |
| 2708 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2709 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2710 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2711 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2712 | continue; |
| 2713 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 2714 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2715 | fib = rib; |
| 2716 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2717 | if (rib->type != type) |
| 2718 | continue; |
| 2719 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2720 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2721 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2722 | if (nexthop->ifindex != ifindex) |
| 2723 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2724 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2725 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2726 | rib->refcnt--; |
| 2727 | route_unlock_node (rn); |
| 2728 | route_unlock_node (rn); |
| 2729 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2730 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2731 | same = rib; |
| 2732 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2733 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2734 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2735 | else |
| 2736 | { |
| 2737 | if (gate == NULL) |
| 2738 | { |
| 2739 | same = rib; |
| 2740 | break; |
| 2741 | } |
| 2742 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2743 | if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate)) |
| 2744 | { |
| 2745 | same = rib; |
| 2746 | break; |
| 2747 | } |
| 2748 | if (same) |
| 2749 | break; |
| 2750 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* If same type of route can't be found and this message is from |
| 2754 | kernel. */ |
| 2755 | if (! same) |
| 2756 | { |
| 2757 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2758 | { |
| 2759 | /* Unset flags. */ |
| 2760 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2761 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2762 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 2763 | UNSET_FLAG (fib->status, RIB_ENTRY_SELECTED_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2764 | } |
| 2765 | else |
| 2766 | { |
| 2767 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2768 | { |
| 2769 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2770 | zlog_debug ("route %s vrf %u via %s ifindex %d type %d " |
| 2771 | "doesn't exist in rib", |
| 2772 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2773 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2774 | ifindex, |
| 2775 | type); |
| 2776 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2777 | zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib", |
| 2778 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2779 | ifindex, |
| 2780 | type); |
| 2781 | } |
| 2782 | route_unlock_node (rn); |
| 2783 | return ZEBRA_ERR_RTNOEXIST; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | if (same) |
| 2788 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2789 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2790 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2791 | return 0; |
| 2792 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2793 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2794 | |
| 2795 | /* Add static route into static route configuration. */ |
| 2796 | int |
| 2797 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 2798 | const char *ifname, u_char flags, u_char distance, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2799 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2800 | { |
| 2801 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2802 | struct static_route *si; |
| 2803 | struct static_route *pp; |
| 2804 | struct static_route *cp; |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2805 | struct static_route *update = NULL; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2806 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
| 2807 | struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2808 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2809 | if (! stable) |
| 2810 | return -1; |
Paul Jakma | 27b4725 | 2006-07-02 16:38:54 +0000 | [diff] [blame] | 2811 | |
| 2812 | if (!gate && |
| 2813 | (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME)) |
| 2814 | return -1; |
| 2815 | |
| 2816 | if (!ifname && |
| 2817 | (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME)) |
| 2818 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2819 | |
| 2820 | /* Lookup static route prefix. */ |
| 2821 | rn = route_node_get (stable, p); |
| 2822 | |
| 2823 | /* Do nothing if there is a same static route. */ |
| 2824 | for (si = rn->info; si; si = si->next) |
| 2825 | { |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2826 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2827 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2828 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2829 | { |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2830 | if (distance == si->distance) |
| 2831 | { |
| 2832 | route_unlock_node (rn); |
| 2833 | return 0; |
| 2834 | } |
| 2835 | else |
| 2836 | update = si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2840 | if (update) |
| 2841 | static_delete_ipv6(p, type, gate, ifname, si->distance, vrf_id); |
| 2842 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2843 | /* Make new static route structure. */ |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2844 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2845 | |
| 2846 | si->type = type; |
| 2847 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2848 | si->flags = flags; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2849 | si->vrf_id = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2850 | |
| 2851 | switch (type) |
| 2852 | { |
| 2853 | case STATIC_IPV6_GATEWAY: |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2854 | si->addr.ipv6 = *gate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | case STATIC_IPV6_IFNAME: |
| 2857 | si->ifname = XSTRDUP (0, ifname); |
| 2858 | break; |
| 2859 | case STATIC_IPV6_GATEWAY_IFNAME: |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2860 | si->addr.ipv6 = *gate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2861 | si->ifname = XSTRDUP (0, ifname); |
| 2862 | break; |
| 2863 | } |
| 2864 | |
| 2865 | /* Add new static route information to the tree with sort by |
| 2866 | distance value and gateway address. */ |
| 2867 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2868 | { |
| 2869 | if (si->distance < cp->distance) |
| 2870 | break; |
| 2871 | if (si->distance > cp->distance) |
| 2872 | continue; |
| 2873 | } |
| 2874 | |
| 2875 | /* Make linked list. */ |
| 2876 | if (pp) |
| 2877 | pp->next = si; |
| 2878 | else |
| 2879 | rn->info = si; |
| 2880 | if (cp) |
| 2881 | cp->prev = si; |
| 2882 | si->prev = pp; |
| 2883 | si->next = cp; |
| 2884 | |
| 2885 | /* Install into rib. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2886 | static_install_route (AFI_IP6, SAFI_UNICAST, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2887 | |
| 2888 | return 1; |
| 2889 | } |
| 2890 | |
| 2891 | /* Delete static route from static route configuration. */ |
| 2892 | int |
| 2893 | 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] | 2894 | const char *ifname, u_char distance, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2895 | { |
| 2896 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2897 | struct static_route *si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2898 | struct route_table *stable; |
| 2899 | |
| 2900 | /* Lookup table. */ |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 2901 | stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2902 | if (! stable) |
| 2903 | return -1; |
| 2904 | |
| 2905 | /* Lookup static route prefix. */ |
| 2906 | rn = route_node_lookup (stable, p); |
| 2907 | if (! rn) |
| 2908 | return 0; |
| 2909 | |
| 2910 | /* Find same static route is the tree */ |
| 2911 | for (si = rn->info; si; si = si->next) |
| 2912 | if (distance == si->distance |
| 2913 | && type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2914 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2915 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2916 | break; |
| 2917 | |
| 2918 | /* Can't find static route. */ |
| 2919 | if (! si) |
| 2920 | { |
| 2921 | route_unlock_node (rn); |
| 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | /* Install into rib. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2926 | static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Unlink static route from linked list. */ |
| 2929 | if (si->prev) |
| 2930 | si->prev->next = si->next; |
| 2931 | else |
| 2932 | rn->info = si->next; |
| 2933 | if (si->next) |
| 2934 | si->next->prev = si->prev; |
| 2935 | |
| 2936 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2937 | if (ifname) |
| 2938 | XFREE (0, si->ifname); |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2939 | XFREE (MTYPE_STATIC_ROUTE, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2940 | |
| 2941 | return 1; |
| 2942 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2943 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2944 | /* RIB update function. */ |
| 2945 | void |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2946 | rib_update (vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2947 | { |
| 2948 | struct route_node *rn; |
| 2949 | struct route_table *table; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2950 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2951 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2952 | if (table) |
| 2953 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2954 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2955 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2956 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2957 | table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2958 | if (table) |
| 2959 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2960 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2961 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2964 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2965 | /* Remove all routes which comes from non main table. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2966 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2967 | rib_weed_table (struct route_table *table) |
| 2968 | { |
| 2969 | struct route_node *rn; |
| 2970 | struct rib *rib; |
| 2971 | struct rib *next; |
| 2972 | |
| 2973 | if (table) |
| 2974 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2975 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2976 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2977 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2978 | continue; |
| 2979 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 2980 | if (rib->table != zebrad.rtm_table_default && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2981 | rib->table != RT_TABLE_MAIN) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2982 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | /* Delete all routes from non main table. */ |
| 2987 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2988 | rib_weed_tables (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2989 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2990 | vrf_iter_t iter; |
| 2991 | struct zebra_vrf *zvrf; |
| 2992 | |
| 2993 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 2994 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 2995 | { |
| 2996 | rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 2997 | rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 2998 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2999 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3000 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3001 | #if 0 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3002 | /* Delete self installed routes after zebra is relaunched. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3003 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3004 | rib_sweep_table (struct route_table *table) |
| 3005 | { |
| 3006 | struct route_node *rn; |
| 3007 | struct rib *rib; |
| 3008 | struct rib *next; |
| 3009 | int ret = 0; |
| 3010 | |
| 3011 | if (table) |
| 3012 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3013 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3014 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3015 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3016 | continue; |
| 3017 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3018 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
| 3019 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) |
| 3020 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 3021 | ret = rib_update_kernel (rn, rib, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3022 | if (! ret) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3023 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | } |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3027 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3028 | |
| 3029 | /* Sweep all RIB tables. */ |
| 3030 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3031 | rib_sweep_route (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3032 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3033 | vrf_iter_t iter; |
| 3034 | struct zebra_vrf *zvrf; |
| 3035 | |
| 3036 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3037 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3038 | { |
| 3039 | rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 3040 | rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3041 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3042 | } |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3043 | |
| 3044 | /* Remove specific by protocol routes from 'table'. */ |
| 3045 | static unsigned long |
| 3046 | rib_score_proto_table (u_char proto, struct route_table *table) |
| 3047 | { |
| 3048 | struct route_node *rn; |
| 3049 | struct rib *rib; |
| 3050 | struct rib *next; |
| 3051 | unsigned long n = 0; |
| 3052 | |
| 3053 | if (table) |
| 3054 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3055 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3056 | { |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3057 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3058 | continue; |
| 3059 | if (rib->type == proto) |
| 3060 | { |
| 3061 | rib_delnode (rn, rib); |
| 3062 | n++; |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | return n; |
| 3067 | } |
| 3068 | |
| 3069 | /* Remove specific by protocol routes. */ |
| 3070 | unsigned long |
| 3071 | rib_score_proto (u_char proto) |
| 3072 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3073 | vrf_iter_t iter; |
| 3074 | struct zebra_vrf *zvrf; |
| 3075 | unsigned long cnt = 0; |
| 3076 | |
| 3077 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3078 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3079 | cnt += rib_score_proto_table (proto, zvrf->table[AFI_IP][SAFI_UNICAST]) |
| 3080 | +rib_score_proto_table (proto, zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3081 | |
| 3082 | return cnt; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3083 | } |
| 3084 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3085 | /* Close RIB and clean up kernel routes. */ |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 3086 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3087 | rib_close_table (struct route_table *table) |
| 3088 | { |
| 3089 | struct route_node *rn; |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3090 | rib_table_info_t *info = table->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3091 | struct rib *rib; |
| 3092 | |
| 3093 | if (table) |
| 3094 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3095 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3096 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 3097 | if (!CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3098 | continue; |
| 3099 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3100 | if (info->safi == SAFI_UNICAST) |
| 3101 | zfpm_trigger_update (rn, NULL); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 3102 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3103 | if (! RIB_SYSTEM_ROUTE (rib)) |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 3104 | rib_update_kernel (rn, rib, NULL); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3105 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* Close all RIB tables. */ |
| 3109 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3110 | rib_close (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3111 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3112 | vrf_iter_t iter; |
| 3113 | struct zebra_vrf *zvrf; |
| 3114 | |
| 3115 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3116 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3117 | { |
| 3118 | rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 3119 | rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3120 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3121 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3122 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3123 | /* Routing information base initialize. */ |
| 3124 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3125 | rib_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3126 | { |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3127 | rib_queue_init (&zebrad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3128 | } |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3129 | |
| 3130 | /* |
| 3131 | * vrf_id_get_next |
| 3132 | * |
| 3133 | * Get the first vrf id that is greater than the given vrf id if any. |
| 3134 | * |
| 3135 | * Returns TRUE if a vrf id was found, FALSE otherwise. |
| 3136 | */ |
| 3137 | static inline int |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3138 | 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] | 3139 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3140 | vrf_iter_t iter = vrf_iterator (vrf_id); |
| 3141 | struct zebra_vrf *zvrf = vrf_iter2info (iter); |
| 3142 | |
| 3143 | /* The same one ? Then find out the next. */ |
| 3144 | if (zvrf && (zvrf->vrf_id == vrf_id)) |
| 3145 | zvrf = vrf_iter2info (vrf_next (iter)); |
| 3146 | |
| 3147 | if (zvrf) |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3148 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3149 | *next_id_p = zvrf->vrf_id; |
| 3150 | return 1; |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * rib_tables_iter_next |
| 3158 | * |
| 3159 | * Returns the next table in the iteration. |
| 3160 | */ |
| 3161 | struct route_table * |
| 3162 | rib_tables_iter_next (rib_tables_iter_t *iter) |
| 3163 | { |
| 3164 | struct route_table *table; |
| 3165 | |
| 3166 | /* |
| 3167 | * Array that helps us go over all AFI/SAFI combinations via one |
| 3168 | * index. |
| 3169 | */ |
| 3170 | static struct { |
| 3171 | afi_t afi; |
| 3172 | safi_t safi; |
| 3173 | } afi_safis[] = { |
| 3174 | { AFI_IP, SAFI_UNICAST }, |
| 3175 | { AFI_IP, SAFI_MULTICAST }, |
| 3176 | { AFI_IP6, SAFI_UNICAST }, |
| 3177 | { AFI_IP6, SAFI_MULTICAST }, |
| 3178 | }; |
| 3179 | |
| 3180 | table = NULL; |
| 3181 | |
| 3182 | switch (iter->state) |
| 3183 | { |
| 3184 | |
| 3185 | case RIB_TABLES_ITER_S_INIT: |
Donald Sharp | 0d27129 | 2015-11-19 07:21:30 -0500 | [diff] [blame] | 3186 | iter->vrf_id = VRF_DEFAULT; |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3187 | iter->afi_safi_ix = -1; |
| 3188 | |
| 3189 | /* Fall through */ |
| 3190 | |
| 3191 | case RIB_TABLES_ITER_S_ITERATING: |
| 3192 | iter->afi_safi_ix++; |
| 3193 | while (1) |
| 3194 | { |
| 3195 | |
| 3196 | while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis)) |
| 3197 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3198 | table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi, |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3199 | afi_safis[iter->afi_safi_ix].safi, |
| 3200 | iter->vrf_id); |
| 3201 | if (table) |
| 3202 | break; |
| 3203 | |
| 3204 | iter->afi_safi_ix++; |
| 3205 | } |
| 3206 | |
| 3207 | /* |
| 3208 | * Found another table in this vrf. |
| 3209 | */ |
| 3210 | if (table) |
| 3211 | break; |
| 3212 | |
| 3213 | /* |
| 3214 | * Done with all tables in the current vrf, go to the next |
| 3215 | * one. |
| 3216 | */ |
| 3217 | if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id)) |
| 3218 | break; |
| 3219 | |
| 3220 | iter->afi_safi_ix = 0; |
| 3221 | } |
| 3222 | |
| 3223 | break; |
| 3224 | |
| 3225 | case RIB_TABLES_ITER_S_DONE: |
| 3226 | return NULL; |
| 3227 | } |
| 3228 | |
| 3229 | if (table) |
| 3230 | iter->state = RIB_TABLES_ITER_S_ITERATING; |
| 3231 | else |
| 3232 | iter->state = RIB_TABLES_ITER_S_DONE; |
| 3233 | |
| 3234 | return table; |
| 3235 | } |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3236 | |
| 3237 | /* |
| 3238 | * Create a routing table for the specific AFI/SAFI in the given VRF. |
| 3239 | */ |
| 3240 | static void |
| 3241 | zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi) |
| 3242 | { |
| 3243 | rib_table_info_t *info; |
| 3244 | struct route_table *table; |
| 3245 | |
| 3246 | assert (!zvrf->table[afi][safi]); |
| 3247 | |
| 3248 | table = route_table_init (); |
| 3249 | zvrf->table[afi][safi] = table; |
| 3250 | |
| 3251 | info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info)); |
| 3252 | info->zvrf = zvrf; |
| 3253 | info->afi = afi; |
| 3254 | info->safi = safi; |
| 3255 | table->info = info; |
| 3256 | } |
| 3257 | |
| 3258 | /* Allocate new zebra VRF. */ |
| 3259 | struct zebra_vrf * |
| 3260 | zebra_vrf_alloc (vrf_id_t vrf_id) |
| 3261 | { |
| 3262 | struct zebra_vrf *zvrf; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 3263 | #ifdef HAVE_NETLINK |
| 3264 | char nl_name[64]; |
| 3265 | #endif |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3266 | |
| 3267 | zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf)); |
| 3268 | |
| 3269 | /* Allocate routing table and static table. */ |
| 3270 | zebra_vrf_table_create (zvrf, AFI_IP, SAFI_UNICAST); |
| 3271 | zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_UNICAST); |
| 3272 | zvrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 3273 | zvrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
| 3274 | zebra_vrf_table_create (zvrf, AFI_IP, SAFI_MULTICAST); |
| 3275 | zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_MULTICAST); |
| 3276 | zvrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init (); |
| 3277 | zvrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init (); |
| 3278 | |
| 3279 | /* Set VRF ID */ |
| 3280 | zvrf->vrf_id = vrf_id; |
| 3281 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 3282 | #ifdef HAVE_NETLINK |
| 3283 | /* Initialize netlink sockets */ |
| 3284 | snprintf (nl_name, 64, "netlink-listen (vrf %u)", vrf_id); |
| 3285 | zvrf->netlink.sock = -1; |
| 3286 | zvrf->netlink.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name); |
| 3287 | |
| 3288 | snprintf (nl_name, 64, "netlink-cmd (vrf %u)", vrf_id); |
| 3289 | zvrf->netlink_cmd.sock = -1; |
| 3290 | zvrf->netlink_cmd.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name); |
| 3291 | #endif |
| 3292 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3293 | return zvrf; |
| 3294 | } |
| 3295 | |
| 3296 | /* Lookup the routing table in an enabled VRF. */ |
| 3297 | struct route_table * |
| 3298 | zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id) |
| 3299 | { |
| 3300 | struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id); |
| 3301 | |
| 3302 | if (!zvrf) |
| 3303 | return NULL; |
| 3304 | |
| 3305 | if (afi >= AFI_MAX || safi >= SAFI_MAX) |
| 3306 | return NULL; |
| 3307 | |
| 3308 | return zvrf->table[afi][safi]; |
| 3309 | } |
| 3310 | |
| 3311 | /* Lookup the static routing table in a VRF. */ |
| 3312 | struct route_table * |
| 3313 | zebra_vrf_static_table (afi_t afi, safi_t safi, vrf_id_t vrf_id) |
| 3314 | { |
| 3315 | struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id); |
| 3316 | |
| 3317 | if (!zvrf) |
| 3318 | return NULL; |
| 3319 | |
| 3320 | if (afi >= AFI_MAX || safi >= SAFI_MAX) |
| 3321 | return NULL; |
| 3322 | |
| 3323 | return zvrf->stable[afi][safi]; |
| 3324 | } |
| 3325 | |