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