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