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