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