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; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1913 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1914 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1915 | table = zebra_vrf_table (AFI_IP, safi, rib->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1916 | if (! table) |
| 1917 | return 0; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1918 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1919 | /* Make it sure prefixlen is applied to the prefix. */ |
| 1920 | apply_mask_ipv4 (p); |
| 1921 | |
| 1922 | /* Set default distance by route type. */ |
| 1923 | if (rib->distance == 0) |
| 1924 | { |
| 1925 | rib->distance = route_info[rib->type].distance; |
| 1926 | |
| 1927 | /* iBGP distance is 200. */ |
| 1928 | if (rib->type == ZEBRA_ROUTE_BGP |
| 1929 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 1930 | rib->distance = 200; |
| 1931 | } |
| 1932 | |
| 1933 | /* Lookup route node.*/ |
| 1934 | rn = route_node_get (table, (struct prefix *) p); |
| 1935 | |
| 1936 | /* If same type of route are installed, treat it as a implicit |
| 1937 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1938 | RNODE_FOREACH_RIB (rn, same) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1939 | { |
Paul Jakma | 0b8c4f1 | 2007-06-27 11:12:38 +0000 | [diff] [blame] | 1940 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1941 | continue; |
| 1942 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1943 | if (same->type == rib->type && same->table == rib->table |
| 1944 | && same->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1945 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1946 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1947 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1948 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1949 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) |
| 1950 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1951 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1952 | |
| 1953 | /* Link new rib to node.*/ |
| 1954 | rib_addnode (rn, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1955 | if (IS_ZEBRA_DEBUG_RIB) |
| 1956 | { |
| 1957 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1958 | __func__, (void *)rn, (void *)rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1959 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1960 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1961 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1962 | /* Free implicit route.*/ |
| 1963 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1964 | { |
| 1965 | if (IS_ZEBRA_DEBUG_RIB) |
| 1966 | { |
| 1967 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1968 | __func__, (void *)rn, (void *)same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 1969 | rib_dump (p, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1970 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1971 | rib_delnode (rn, 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 | |
| 1974 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1975 | return 0; |
| 1976 | } |
| 1977 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1978 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1979 | int |
| 1980 | rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 1981 | struct in_addr *gate, ifindex_t ifindex, |
| 1982 | vrf_id_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1983 | { |
| 1984 | struct route_table *table; |
| 1985 | struct route_node *rn; |
| 1986 | struct rib *rib; |
| 1987 | struct rib *fib = NULL; |
| 1988 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1989 | struct nexthop *nexthop, *tnexthop; |
| 1990 | int recursing; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1991 | char buf1[PREFIX_STRLEN]; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 1992 | char buf2[INET_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1993 | |
| 1994 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1995 | table = zebra_vrf_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1996 | if (! table) |
| 1997 | return 0; |
| 1998 | |
| 1999 | /* Apply mask. */ |
| 2000 | apply_mask_ipv4 (p); |
| 2001 | |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2002 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2003 | { |
| 2004 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2005 | zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u via %s ifindex %d", |
| 2006 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2007 | inet_ntoa (*gate), |
| 2008 | ifindex); |
| 2009 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2010 | zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u ifindex %d", |
| 2011 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Christian Franke | b52aef1 | 2013-11-27 17:06:15 +0000 | [diff] [blame] | 2012 | ifindex); |
| 2013 | } |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2014 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2015 | /* Lookup route node. */ |
| 2016 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2017 | if (! rn) |
| 2018 | { |
| 2019 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2020 | { |
| 2021 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2022 | zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib", |
| 2023 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2024 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2025 | ifindex); |
| 2026 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2027 | zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib", |
| 2028 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2029 | ifindex); |
| 2030 | } |
| 2031 | return ZEBRA_ERR_RTNOEXIST; |
| 2032 | } |
| 2033 | |
| 2034 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2035 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2036 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2037 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2038 | continue; |
| 2039 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 2040 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2041 | fib = rib; |
| 2042 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2043 | if (rib->type != type) |
| 2044 | continue; |
| 2045 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2046 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2047 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2048 | if (nexthop->ifindex != ifindex) |
| 2049 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2050 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2051 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2052 | rib->refcnt--; |
| 2053 | route_unlock_node (rn); |
| 2054 | route_unlock_node (rn); |
| 2055 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2056 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2057 | same = rib; |
| 2058 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2059 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2060 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2061 | else |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2062 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2063 | if (gate == NULL) |
| 2064 | { |
| 2065 | same = rib; |
| 2066 | break; |
| 2067 | } |
| 2068 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2069 | if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate)) |
| 2070 | { |
| 2071 | same = rib; |
| 2072 | break; |
| 2073 | } |
| 2074 | if (same) |
| 2075 | break; |
| 2076 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2077 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | /* If same type of route can't be found and this message is from |
| 2079 | kernel. */ |
| 2080 | if (! same) |
| 2081 | { |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 2082 | if (fib && type == ZEBRA_ROUTE_KERNEL && |
| 2083 | CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) |
| 2084 | { |
| 2085 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2086 | { |
| 2087 | zlog_debug ("Zebra route %s/%d was deleted by others from kernel", |
| 2088 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
| 2089 | p->prefixlen); |
| 2090 | } |
| 2091 | /* This means someone else, other than Zebra, has deleted |
| 2092 | * a Zebra router from the kernel. We will add it back */ |
| 2093 | rib_update_kernel(rn, NULL, fib); |
| 2094 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2095 | else |
| 2096 | { |
| 2097 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2098 | { |
| 2099 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2100 | zlog_debug ("route %s vrf %u via %s ifindex %d type %d " |
| 2101 | "doesn't exist in rib", |
| 2102 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2103 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2104 | ifindex, |
| 2105 | type); |
| 2106 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2107 | zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib", |
| 2108 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2109 | ifindex, |
| 2110 | type); |
| 2111 | } |
| 2112 | route_unlock_node (rn); |
| 2113 | return ZEBRA_ERR_RTNOEXIST; |
| 2114 | } |
| 2115 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2116 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2117 | if (same) |
| 2118 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2120 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2121 | return 0; |
| 2122 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2123 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2124 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2125 | static void |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2126 | 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] | 2127 | { |
| 2128 | struct rib *rib; |
| 2129 | struct route_node *rn; |
| 2130 | struct route_table *table; |
| 2131 | |
| 2132 | /* Lookup table. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2133 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2134 | if (! table) |
| 2135 | return; |
| 2136 | |
| 2137 | /* Lookup existing route */ |
| 2138 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2139 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2140 | { |
| 2141 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2142 | continue; |
| 2143 | |
| 2144 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2145 | break; |
| 2146 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2147 | |
| 2148 | if (rib) |
| 2149 | { |
| 2150 | /* Same distance static route is there. Update it with new |
| 2151 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2152 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2153 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2154 | { |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2155 | case STATIC_IPV4_GATEWAY: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2156 | rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2157 | break; |
| 2158 | case STATIC_IPV4_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2159 | rib_nexthop_ifname_add (rib, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2160 | break; |
| 2161 | case STATIC_IPV4_BLACKHOLE: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2162 | rib_nexthop_blackhole_add (rib); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2163 | break; |
| 2164 | case STATIC_IPV6_GATEWAY: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2165 | rib_nexthop_ipv6_add (rib, &si->addr.ipv6); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2166 | break; |
| 2167 | case STATIC_IPV6_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2168 | rib_nexthop_ifname_add (rib, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2169 | break; |
| 2170 | case STATIC_IPV6_GATEWAY_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2171 | rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2172 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2173 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2174 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2175 | } |
| 2176 | else |
| 2177 | { |
| 2178 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2179 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2180 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2181 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2182 | rib->distance = si->distance; |
| 2183 | rib->metric = 0; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2184 | rib->vrf_id = si->vrf_id; |
Nolan Leake | b0145dd | 2012-09-13 17:17:31 +0000 | [diff] [blame] | 2185 | rib->table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2186 | rib->nexthop_num = 0; |
| 2187 | |
| 2188 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2189 | { |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2190 | case STATIC_IPV4_GATEWAY: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2191 | rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2192 | break; |
| 2193 | case STATIC_IPV4_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2194 | rib_nexthop_ifname_add (rib, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2195 | break; |
| 2196 | case STATIC_IPV4_BLACKHOLE: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2197 | rib_nexthop_blackhole_add (rib); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2198 | break; |
| 2199 | case STATIC_IPV6_GATEWAY: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2200 | rib_nexthop_ipv6_add (rib, &si->addr.ipv6); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2201 | break; |
| 2202 | case STATIC_IPV6_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2203 | rib_nexthop_ifname_add (rib, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2204 | break; |
| 2205 | case STATIC_IPV6_GATEWAY_IFNAME: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2206 | rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname); |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2207 | break; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2208 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2209 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2210 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2211 | rib->flags = si->flags; |
| 2212 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2213 | /* Link this rib to the tree. */ |
| 2214 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2218 | static int |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2219 | static_nexthop_same (struct nexthop *nexthop, struct static_route *si) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2220 | { |
| 2221 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 2222 | && si->type == STATIC_IPV4_GATEWAY |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2223 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2224 | return 1; |
| 2225 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2226 | && si->type == STATIC_IPV4_IFNAME |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2227 | && strcmp (nexthop->ifname, si->ifname) == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2228 | return 1; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2229 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
| 2230 | && si->type == STATIC_IPV4_BLACKHOLE) |
| 2231 | return 1; |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2232 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 2233 | && si->type == STATIC_IPV6_GATEWAY |
| 2234 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)) |
| 2235 | return 1; |
| 2236 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2237 | && si->type == STATIC_IPV6_IFNAME |
| 2238 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2239 | return 1; |
| 2240 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 2241 | && si->type == STATIC_IPV6_GATEWAY_IFNAME |
| 2242 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6) |
| 2243 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2244 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 2245 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* Uninstall static route from RIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2249 | static void |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2250 | 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] | 2251 | { |
| 2252 | struct route_node *rn; |
| 2253 | struct rib *rib; |
| 2254 | struct nexthop *nexthop; |
| 2255 | struct route_table *table; |
| 2256 | |
| 2257 | /* Lookup table. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2258 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2259 | if (! table) |
| 2260 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2261 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2262 | /* Lookup existing route with type and distance. */ |
| 2263 | rn = route_node_lookup (table, p); |
| 2264 | if (! rn) |
| 2265 | return; |
| 2266 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2267 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2268 | { |
| 2269 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2270 | continue; |
| 2271 | |
| 2272 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2273 | break; |
| 2274 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2275 | |
| 2276 | if (! rib) |
| 2277 | { |
| 2278 | route_unlock_node (rn); |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | /* Lookup nexthop. */ |
| 2283 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Donald Sharp | a979ab7 | 2015-11-04 13:26:38 -0500 | [diff] [blame] | 2284 | if (static_nexthop_same (nexthop, si)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2285 | break; |
| 2286 | |
| 2287 | /* Can't find nexthop. */ |
| 2288 | if (! nexthop) |
| 2289 | { |
| 2290 | route_unlock_node (rn); |
| 2291 | return; |
| 2292 | } |
| 2293 | |
| 2294 | /* Check nexthop. */ |
| 2295 | if (rib->nexthop_num == 1) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2296 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2297 | else |
| 2298 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 2299 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 2300 | rib_uninstall (rn, rib); |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2301 | rib_nexthop_delete (rib, nexthop); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 2302 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2303 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2304 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2305 | /* Unlock node. */ |
| 2306 | route_unlock_node (rn); |
| 2307 | } |
| 2308 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2309 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2310 | static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate, |
| 2311 | const char *ifname, u_char flags, u_char distance, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2312 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2313 | { |
| 2314 | u_char type = 0; |
| 2315 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2316 | struct static_route *si; |
| 2317 | struct static_route *pp; |
| 2318 | struct static_route *cp; |
| 2319 | struct static_route *update = NULL; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2320 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
| 2321 | struct route_table *stable = zvrf->stable[AFI_IP][safi]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2322 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2323 | if (! stable) |
| 2324 | return -1; |
| 2325 | |
| 2326 | /* Lookup static route prefix. */ |
| 2327 | rn = route_node_get (stable, p); |
| 2328 | |
| 2329 | /* Make flags. */ |
| 2330 | if (gate) |
| 2331 | type = STATIC_IPV4_GATEWAY; |
paul | 368aa3f | 2003-05-25 23:24:50 +0000 | [diff] [blame] | 2332 | else if (ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2333 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2334 | else |
| 2335 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2336 | |
| 2337 | /* Do nothing if there is a same static route. */ |
| 2338 | for (si = rn->info; si; si = si->next) |
| 2339 | { |
| 2340 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2341 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
| 2342 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2343 | { |
| 2344 | if (distance == si->distance) |
| 2345 | { |
| 2346 | route_unlock_node (rn); |
| 2347 | return 0; |
| 2348 | } |
| 2349 | else |
| 2350 | update = si; |
| 2351 | } |
| 2352 | } |
| 2353 | |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2354 | /* Distance changed. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2355 | if (update) |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2356 | static_delete_ipv4_safi (safi, p, gate, ifname, update->distance, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* Make new static route structure. */ |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2359 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2360 | |
| 2361 | si->type = type; |
| 2362 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2363 | si->flags = flags; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2364 | si->vrf_id = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2365 | |
| 2366 | if (gate) |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2367 | si->addr.ipv4 = *gate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2368 | if (ifname) |
David Lamparter | 23757db | 2016-02-24 06:26:02 +0100 | [diff] [blame] | 2369 | si->ifname = XSTRDUP (MTYPE_TMP, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* Add new static route information to the tree with sort by |
| 2372 | distance value and gateway address. */ |
| 2373 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2374 | { |
| 2375 | if (si->distance < cp->distance) |
| 2376 | break; |
| 2377 | if (si->distance > cp->distance) |
| 2378 | continue; |
| 2379 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) |
| 2380 | { |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2381 | if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2382 | break; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2383 | if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2384 | continue; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | /* Make linked list. */ |
| 2389 | if (pp) |
| 2390 | pp->next = si; |
| 2391 | else |
| 2392 | rn->info = si; |
| 2393 | if (cp) |
| 2394 | cp->prev = si; |
| 2395 | si->prev = pp; |
| 2396 | si->next = cp; |
| 2397 | |
| 2398 | /* Install into rib. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2399 | static_install_route (AFI_IP, safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2400 | |
| 2401 | return 1; |
| 2402 | } |
| 2403 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2404 | int |
Everton Marques | 33d86db | 2014-07-14 11:19:00 -0300 | [diff] [blame] | 2405 | static_delete_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2406 | const char *ifname, u_char distance, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2407 | { |
| 2408 | u_char type = 0; |
| 2409 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2410 | struct static_route *si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2411 | struct route_table *stable; |
| 2412 | |
| 2413 | /* Lookup table. */ |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 2414 | stable = zebra_vrf_static_table (AFI_IP, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2415 | if (! stable) |
| 2416 | return -1; |
| 2417 | |
| 2418 | /* Lookup static route prefix. */ |
| 2419 | rn = route_node_lookup (stable, p); |
| 2420 | if (! rn) |
| 2421 | return 0; |
| 2422 | |
| 2423 | /* Make flags. */ |
| 2424 | if (gate) |
| 2425 | type = STATIC_IPV4_GATEWAY; |
| 2426 | else if (ifname) |
| 2427 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2428 | else |
| 2429 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2430 | |
| 2431 | /* Find same static route is the tree */ |
| 2432 | for (si = rn->info; si; si = si->next) |
| 2433 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2434 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
| 2435 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | |
| 2438 | /* Can't find static route. */ |
| 2439 | if (! si) |
| 2440 | { |
| 2441 | route_unlock_node (rn); |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | /* Install into rib. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2446 | static_uninstall_route (AFI_IP, safi, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* Unlink static route from linked list. */ |
| 2449 | if (si->prev) |
| 2450 | si->prev->next = si->next; |
| 2451 | else |
| 2452 | rn->info = si->next; |
| 2453 | if (si->next) |
| 2454 | si->next->prev = si->prev; |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2455 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2456 | |
| 2457 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2458 | if (ifname) |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2459 | XFREE (0, si->ifname); |
| 2460 | XFREE (MTYPE_STATIC_ROUTE, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2461 | |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2462 | route_unlock_node (rn); |
| 2463 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2464 | return 1; |
| 2465 | } |
| 2466 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2467 | int |
| 2468 | rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 2469 | struct in6_addr *gate, ifindex_t ifindex, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2470 | vrf_id_t vrf_id, int table_id, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 2471 | 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] | 2472 | { |
| 2473 | struct rib *rib; |
| 2474 | struct rib *same = NULL; |
| 2475 | struct route_table *table; |
| 2476 | struct route_node *rn; |
| 2477 | struct nexthop *nexthop; |
| 2478 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2479 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2480 | table = zebra_vrf_table (AFI_IP6, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2481 | if (! table) |
| 2482 | return 0; |
| 2483 | |
| 2484 | /* Make sure mask is applied. */ |
| 2485 | apply_mask_ipv6 (p); |
| 2486 | |
| 2487 | /* Set default distance by route type. */ |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2488 | if (!distance) |
| 2489 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2490 | |
| 2491 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 2492 | distance = 200; |
| 2493 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2494 | /* Lookup route node.*/ |
| 2495 | rn = route_node_get (table, (struct prefix *) p); |
| 2496 | |
| 2497 | /* If same type of route are installed, treat it as a implicit |
| 2498 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2499 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2500 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2501 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2502 | continue; |
| 2503 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2504 | if (rib->type != type) |
| 2505 | continue; |
| 2506 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2507 | { |
| 2508 | same = rib; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2509 | break; |
| 2510 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2511 | else if ((nexthop = rib->nexthop) && |
| 2512 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
| 2513 | nexthop->ifindex == ifindex) |
| 2514 | { |
| 2515 | rib->refcnt++; |
| 2516 | return 0; |
| 2517 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2521 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2522 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2523 | rib->type = type; |
| 2524 | rib->distance = distance; |
| 2525 | rib->flags = flags; |
| 2526 | rib->metric = metric; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 2527 | rib->mtu = mtu; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2528 | rib->vrf_id = vrf_id; |
| 2529 | rib->table = table_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2530 | rib->nexthop_num = 0; |
| 2531 | rib->uptime = time (NULL); |
| 2532 | |
| 2533 | /* Nexthop settings. */ |
| 2534 | if (gate) |
| 2535 | { |
| 2536 | if (ifindex) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2537 | rib_nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2538 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2539 | rib_nexthop_ipv6_add (rib, gate); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2540 | } |
| 2541 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 2542 | rib_nexthop_ifindex_add (rib, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2543 | |
| 2544 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2545 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 2546 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2547 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2548 | |
| 2549 | /* Link new rib to node.*/ |
| 2550 | rib_addnode (rn, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2551 | if (IS_ZEBRA_DEBUG_RIB) |
| 2552 | { |
| 2553 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2554 | __func__, (void *)rn, (void *)rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2555 | rib_dump (p, rib); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2556 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2557 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2558 | /* Free implicit route.*/ |
| 2559 | if (same) |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2560 | { |
| 2561 | if (IS_ZEBRA_DEBUG_RIB) |
| 2562 | { |
| 2563 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 2564 | __func__, (void *)rn, (void *)same); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 2565 | rib_dump (p, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2566 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2567 | rib_delnode (rn, same); |
Vincent Bernat | fed643f | 2012-10-23 16:00:42 +0000 | [diff] [blame] | 2568 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2569 | |
| 2570 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 2574 | int |
| 2575 | rib_add_ipv6_multipath (struct prefix_ipv6 *p, struct rib *rib, safi_t safi) |
| 2576 | { |
| 2577 | struct route_table *table; |
| 2578 | struct route_node *rn; |
| 2579 | struct rib *same = NULL; |
| 2580 | struct nexthop *nexthop; |
| 2581 | int ret = 0; |
| 2582 | |
| 2583 | if (!rib) |
| 2584 | return 0; /* why are we getting called with NULL rib */ |
| 2585 | |
| 2586 | /* Lookup table. */ |
| 2587 | table = zebra_vrf_table (AFI_IP6, safi, rib->vrf_id); |
| 2588 | |
| 2589 | if (! table) |
| 2590 | return 0; |
| 2591 | |
| 2592 | /* Make sure mask is applied. */ |
| 2593 | apply_mask_ipv6 (p); |
| 2594 | |
| 2595 | /* Set default distance by route type. */ |
| 2596 | if (rib->distance == 0) |
| 2597 | { |
| 2598 | rib->distance = route_info[rib->type].distance; |
| 2599 | |
| 2600 | /* iBGP distance is 200. */ |
| 2601 | if (rib->type == ZEBRA_ROUTE_BGP |
| 2602 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 2603 | rib->distance = 200; |
| 2604 | } |
| 2605 | |
| 2606 | /* Lookup route node.*/ |
| 2607 | rn = route_node_get (table, (struct prefix *) p); |
| 2608 | |
| 2609 | /* If same type of route are installed, treat it as a implicit |
| 2610 | withdraw. */ |
| 2611 | RNODE_FOREACH_RIB (rn, same) { |
| 2612 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) { |
| 2613 | continue; |
| 2614 | } |
| 2615 | if (same->type != rib->type) { |
| 2616 | continue; |
| 2617 | } |
| 2618 | |
| 2619 | if (same->table != rib->table) { |
| 2620 | continue; |
| 2621 | } |
| 2622 | if (same->type != ZEBRA_ROUTE_CONNECT) { |
| 2623 | break; |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2628 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) { |
| 2629 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) { |
| 2630 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | /* Link new rib to node.*/ |
| 2635 | rib_addnode (rn, rib); |
| 2636 | ret = 1; |
| 2637 | /* Free implicit route.*/ |
| 2638 | if (same) |
| 2639 | { |
| 2640 | if (IS_ZEBRA_DEBUG_RIB) |
| 2641 | { |
| 2642 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
| 2643 | __func__, rn, same); |
| 2644 | rib_dump ((struct prefix *)p, same); |
| 2645 | } |
| 2646 | rib_delnode (rn, same); |
| 2647 | ret = -1; |
| 2648 | } |
| 2649 | |
| 2650 | route_unlock_node (rn); |
| 2651 | return ret; |
| 2652 | } |
| 2653 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2654 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2655 | int |
| 2656 | rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 2657 | struct in6_addr *gate, ifindex_t ifindex, |
| 2658 | vrf_id_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2659 | { |
| 2660 | struct route_table *table; |
| 2661 | struct route_node *rn; |
| 2662 | struct rib *rib; |
| 2663 | struct rib *fib = NULL; |
| 2664 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2665 | struct nexthop *nexthop, *tnexthop; |
| 2666 | int recursing; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 2667 | char buf1[PREFIX_STRLEN]; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2668 | char buf2[INET6_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2669 | |
| 2670 | /* Apply mask. */ |
| 2671 | apply_mask_ipv6 (p); |
| 2672 | |
| 2673 | /* Lookup table. */ |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2674 | table = zebra_vrf_table (AFI_IP6, safi, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2675 | if (! table) |
| 2676 | return 0; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2677 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2678 | /* Lookup route node. */ |
| 2679 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2680 | if (! rn) |
| 2681 | { |
| 2682 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2683 | { |
| 2684 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2685 | zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib", |
| 2686 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2687 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2688 | ifindex); |
| 2689 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2690 | zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib", |
| 2691 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2692 | ifindex); |
| 2693 | } |
| 2694 | return ZEBRA_ERR_RTNOEXIST; |
| 2695 | } |
| 2696 | |
| 2697 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2698 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2699 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2700 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2701 | continue; |
| 2702 | |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 2703 | if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2704 | fib = rib; |
| 2705 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2706 | if (rib->type != type) |
| 2707 | continue; |
| 2708 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2709 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2710 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2711 | if (nexthop->ifindex != ifindex) |
| 2712 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2713 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2714 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2715 | rib->refcnt--; |
| 2716 | route_unlock_node (rn); |
| 2717 | route_unlock_node (rn); |
| 2718 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2719 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2720 | same = rib; |
| 2721 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2722 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2723 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2724 | else |
| 2725 | { |
| 2726 | if (gate == NULL) |
| 2727 | { |
| 2728 | same = rib; |
| 2729 | break; |
| 2730 | } |
| 2731 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2732 | if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate)) |
| 2733 | { |
| 2734 | same = rib; |
| 2735 | break; |
| 2736 | } |
| 2737 | if (same) |
| 2738 | break; |
| 2739 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /* If same type of route can't be found and this message is from |
| 2743 | kernel. */ |
| 2744 | if (! same) |
| 2745 | { |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 2746 | if (fib && type == ZEBRA_ROUTE_KERNEL && |
| 2747 | CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) |
| 2748 | { |
| 2749 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2750 | { |
| 2751 | zlog_debug ("Zebra route %s/%d was deleted by others from kernel", |
| 2752 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
| 2753 | p->prefixlen); |
| 2754 | } |
| 2755 | /* This means someone else, other than Zebra, has deleted a Zebra |
| 2756 | * route from the kernel. We will add it back */ |
| 2757 | rib_update_kernel(rn, NULL, fib); |
| 2758 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2759 | else |
| 2760 | { |
| 2761 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2762 | { |
| 2763 | if (gate) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2764 | zlog_debug ("route %s vrf %u via %s ifindex %d type %d " |
| 2765 | "doesn't exist in rib", |
| 2766 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2767 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2768 | ifindex, |
| 2769 | type); |
| 2770 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2771 | zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib", |
| 2772 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2773 | ifindex, |
| 2774 | type); |
| 2775 | } |
| 2776 | route_unlock_node (rn); |
| 2777 | return ZEBRA_ERR_RTNOEXIST; |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | if (same) |
| 2782 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2783 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2784 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2785 | return 0; |
| 2786 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2787 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2788 | /* Add static route into static route configuration. */ |
| 2789 | int |
| 2790 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 2791 | const char *ifname, u_char flags, u_char distance, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2792 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2793 | { |
| 2794 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2795 | struct static_route *si; |
| 2796 | struct static_route *pp; |
| 2797 | struct static_route *cp; |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2798 | struct static_route *update = NULL; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2799 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
| 2800 | struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2801 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2802 | if (! stable) |
| 2803 | return -1; |
Paul Jakma | 27b4725 | 2006-07-02 16:38:54 +0000 | [diff] [blame] | 2804 | |
| 2805 | if (!gate && |
| 2806 | (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME)) |
| 2807 | return -1; |
| 2808 | |
| 2809 | if (!ifname && |
| 2810 | (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME)) |
| 2811 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2812 | |
| 2813 | /* Lookup static route prefix. */ |
| 2814 | rn = route_node_get (stable, p); |
| 2815 | |
| 2816 | /* Do nothing if there is a same static route. */ |
| 2817 | for (si = rn->info; si; si = si->next) |
| 2818 | { |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2819 | if (type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2820 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2821 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2822 | { |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2823 | if (distance == si->distance) |
| 2824 | { |
| 2825 | route_unlock_node (rn); |
| 2826 | return 0; |
| 2827 | } |
| 2828 | else |
| 2829 | update = si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Donald Sharp | c1900e0 | 2015-11-04 13:26:40 -0500 | [diff] [blame] | 2833 | if (update) |
| 2834 | static_delete_ipv6(p, type, gate, ifname, si->distance, vrf_id); |
| 2835 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2836 | /* Make new static route structure. */ |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2837 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2838 | |
| 2839 | si->type = type; |
| 2840 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2841 | si->flags = flags; |
Feng Lu | 7aaf4ea | 2015-05-22 11:40:06 +0200 | [diff] [blame] | 2842 | si->vrf_id = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2843 | |
| 2844 | switch (type) |
| 2845 | { |
| 2846 | case STATIC_IPV6_GATEWAY: |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2847 | si->addr.ipv6 = *gate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2848 | break; |
| 2849 | case STATIC_IPV6_IFNAME: |
David Lamparter | 23757db | 2016-02-24 06:26:02 +0100 | [diff] [blame] | 2850 | si->ifname = XSTRDUP (MTYPE_TMP, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2851 | break; |
| 2852 | case STATIC_IPV6_GATEWAY_IFNAME: |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2853 | si->addr.ipv6 = *gate; |
David Lamparter | 23757db | 2016-02-24 06:26:02 +0100 | [diff] [blame] | 2854 | si->ifname = XSTRDUP (MTYPE_TMP, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | } |
| 2857 | |
| 2858 | /* Add new static route information to the tree with sort by |
| 2859 | distance value and gateway address. */ |
| 2860 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2861 | { |
| 2862 | if (si->distance < cp->distance) |
| 2863 | break; |
| 2864 | if (si->distance > cp->distance) |
| 2865 | continue; |
| 2866 | } |
| 2867 | |
| 2868 | /* Make linked list. */ |
| 2869 | if (pp) |
| 2870 | pp->next = si; |
| 2871 | else |
| 2872 | rn->info = si; |
| 2873 | if (cp) |
| 2874 | cp->prev = si; |
| 2875 | si->prev = pp; |
| 2876 | si->next = cp; |
| 2877 | |
| 2878 | /* Install into rib. */ |
Donald Sharp | 9bcdd1a | 2015-11-04 13:26:37 -0500 | [diff] [blame] | 2879 | static_install_route (AFI_IP6, SAFI_UNICAST, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2880 | |
| 2881 | return 1; |
| 2882 | } |
| 2883 | |
| 2884 | /* Delete static route from static route configuration. */ |
| 2885 | int |
| 2886 | static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2887 | const char *ifname, u_char distance, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2888 | { |
| 2889 | struct route_node *rn; |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2890 | struct static_route *si; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2891 | struct route_table *stable; |
| 2892 | |
| 2893 | /* Lookup table. */ |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 2894 | stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2895 | if (! stable) |
| 2896 | return -1; |
| 2897 | |
| 2898 | /* Lookup static route prefix. */ |
| 2899 | rn = route_node_lookup (stable, p); |
| 2900 | if (! rn) |
| 2901 | return 0; |
| 2902 | |
| 2903 | /* Find same static route is the tree */ |
| 2904 | for (si = rn->info; si; si = si->next) |
| 2905 | if (distance == si->distance |
| 2906 | && type == si->type |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2907 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2908 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2909 | break; |
| 2910 | |
| 2911 | /* Can't find static route. */ |
| 2912 | if (! si) |
| 2913 | { |
| 2914 | route_unlock_node (rn); |
| 2915 | return 0; |
| 2916 | } |
| 2917 | |
| 2918 | /* Install into rib. */ |
Donald Sharp | 949b719 | 2015-11-04 13:26:39 -0500 | [diff] [blame] | 2919 | static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* Unlink static route from linked list. */ |
| 2922 | if (si->prev) |
| 2923 | si->prev->next = si->next; |
| 2924 | else |
| 2925 | rn->info = si->next; |
| 2926 | if (si->next) |
| 2927 | si->next->prev = si->prev; |
| 2928 | |
| 2929 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2930 | if (ifname) |
| 2931 | XFREE (0, si->ifname); |
Donald Sharp | d4c27d6 | 2015-11-04 13:26:35 -0500 | [diff] [blame] | 2932 | XFREE (MTYPE_STATIC_ROUTE, si); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2933 | |
| 2934 | return 1; |
| 2935 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2936 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2937 | /* RIB update function. */ |
| 2938 | void |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2939 | rib_update (vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2940 | { |
| 2941 | struct route_node *rn; |
| 2942 | struct route_table *table; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2943 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2944 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2945 | if (table) |
| 2946 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2947 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2948 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2949 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2950 | table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2951 | if (table) |
| 2952 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2953 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2954 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2957 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2958 | /* Remove all routes which comes from non main table. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2959 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2960 | rib_weed_table (struct route_table *table) |
| 2961 | { |
| 2962 | struct route_node *rn; |
| 2963 | struct rib *rib; |
| 2964 | struct rib *next; |
| 2965 | |
| 2966 | if (table) |
| 2967 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2968 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2969 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2970 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2971 | continue; |
| 2972 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 2973 | if (rib->table != zebrad.rtm_table_default && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | rib->table != RT_TABLE_MAIN) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2975 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | /* Delete all routes from non main table. */ |
| 2980 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2981 | rib_weed_tables (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2982 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2983 | vrf_iter_t iter; |
| 2984 | struct zebra_vrf *zvrf; |
| 2985 | |
| 2986 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 2987 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 2988 | { |
| 2989 | rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 2990 | rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 2991 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2992 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2993 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 2994 | #if 0 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2995 | /* Delete self installed routes after zebra is relaunched. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2996 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2997 | rib_sweep_table (struct route_table *table) |
| 2998 | { |
| 2999 | struct route_node *rn; |
| 3000 | struct rib *rib; |
| 3001 | struct rib *next; |
| 3002 | int ret = 0; |
| 3003 | |
| 3004 | if (table) |
| 3005 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3006 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3007 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3008 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3009 | continue; |
| 3010 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3011 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
| 3012 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) |
| 3013 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 3014 | ret = rib_update_kernel (rn, rib, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3015 | if (! ret) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3016 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
| 3019 | } |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3020 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3021 | |
| 3022 | /* Sweep all RIB tables. */ |
| 3023 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3024 | rib_sweep_route (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3025 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3026 | vrf_iter_t iter; |
| 3027 | struct zebra_vrf *zvrf; |
| 3028 | |
| 3029 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3030 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3031 | { |
| 3032 | rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 3033 | rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3034 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3035 | } |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3036 | |
| 3037 | /* Remove specific by protocol routes from 'table'. */ |
| 3038 | static unsigned long |
| 3039 | rib_score_proto_table (u_char proto, struct route_table *table) |
| 3040 | { |
| 3041 | struct route_node *rn; |
| 3042 | struct rib *rib; |
| 3043 | struct rib *next; |
| 3044 | unsigned long n = 0; |
| 3045 | |
| 3046 | if (table) |
| 3047 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3048 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3049 | { |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3050 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3051 | continue; |
| 3052 | if (rib->type == proto) |
| 3053 | { |
| 3054 | rib_delnode (rn, rib); |
| 3055 | n++; |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | return n; |
| 3060 | } |
| 3061 | |
| 3062 | /* Remove specific by protocol routes. */ |
| 3063 | unsigned long |
| 3064 | rib_score_proto (u_char proto) |
| 3065 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3066 | vrf_iter_t iter; |
| 3067 | struct zebra_vrf *zvrf; |
| 3068 | unsigned long cnt = 0; |
| 3069 | |
| 3070 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3071 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3072 | cnt += rib_score_proto_table (proto, zvrf->table[AFI_IP][SAFI_UNICAST]) |
| 3073 | +rib_score_proto_table (proto, zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3074 | |
| 3075 | return cnt; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3076 | } |
| 3077 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3078 | /* Close RIB and clean up kernel routes. */ |
Feng Lu | 267ceb2 | 2015-05-22 11:40:09 +0200 | [diff] [blame] | 3079 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3080 | rib_close_table (struct route_table *table) |
| 3081 | { |
| 3082 | struct route_node *rn; |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3083 | rib_table_info_t *info = table->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3084 | struct rib *rib; |
| 3085 | |
| 3086 | if (table) |
| 3087 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3088 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3089 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 3090 | if (!CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3091 | continue; |
| 3092 | |
David Lamparter | 7ce9e6a | 2015-01-12 07:05:06 +0100 | [diff] [blame] | 3093 | if (info->safi == SAFI_UNICAST) |
| 3094 | zfpm_trigger_update (rn, NULL); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 3095 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3096 | if (! RIB_SYSTEM_ROUTE (rib)) |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 3097 | rib_update_kernel (rn, rib, NULL); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3098 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* Close all RIB tables. */ |
| 3102 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3103 | rib_close (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3104 | { |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 3105 | vrf_iter_t iter; |
| 3106 | struct zebra_vrf *zvrf; |
| 3107 | |
| 3108 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
| 3109 | if ((zvrf = vrf_iter2info (iter)) != NULL) |
| 3110 | { |
| 3111 | rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]); |
| 3112 | rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); |
| 3113 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3114 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 3115 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3116 | /* Routing information base initialize. */ |
| 3117 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3118 | rib_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3119 | { |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3120 | rib_queue_init (&zebrad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3121 | } |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3122 | |
| 3123 | /* |
| 3124 | * vrf_id_get_next |
| 3125 | * |
| 3126 | * Get the first vrf id that is greater than the given vrf id if any. |
| 3127 | * |
| 3128 | * Returns TRUE if a vrf id was found, FALSE otherwise. |
| 3129 | */ |
| 3130 | static inline int |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3131 | 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] | 3132 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3133 | vrf_iter_t iter = vrf_iterator (vrf_id); |
| 3134 | struct zebra_vrf *zvrf = vrf_iter2info (iter); |
| 3135 | |
| 3136 | /* The same one ? Then find out the next. */ |
| 3137 | if (zvrf && (zvrf->vrf_id == vrf_id)) |
| 3138 | zvrf = vrf_iter2info (vrf_next (iter)); |
| 3139 | |
| 3140 | if (zvrf) |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3141 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3142 | *next_id_p = zvrf->vrf_id; |
| 3143 | return 1; |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * rib_tables_iter_next |
| 3151 | * |
| 3152 | * Returns the next table in the iteration. |
| 3153 | */ |
| 3154 | struct route_table * |
| 3155 | rib_tables_iter_next (rib_tables_iter_t *iter) |
| 3156 | { |
| 3157 | struct route_table *table; |
| 3158 | |
| 3159 | /* |
| 3160 | * Array that helps us go over all AFI/SAFI combinations via one |
| 3161 | * index. |
| 3162 | */ |
| 3163 | static struct { |
| 3164 | afi_t afi; |
| 3165 | safi_t safi; |
| 3166 | } afi_safis[] = { |
| 3167 | { AFI_IP, SAFI_UNICAST }, |
| 3168 | { AFI_IP, SAFI_MULTICAST }, |
| 3169 | { AFI_IP6, SAFI_UNICAST }, |
| 3170 | { AFI_IP6, SAFI_MULTICAST }, |
| 3171 | }; |
| 3172 | |
| 3173 | table = NULL; |
| 3174 | |
| 3175 | switch (iter->state) |
| 3176 | { |
| 3177 | |
| 3178 | case RIB_TABLES_ITER_S_INIT: |
Donald Sharp | 0d27129 | 2015-11-19 07:21:30 -0500 | [diff] [blame] | 3179 | iter->vrf_id = VRF_DEFAULT; |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3180 | iter->afi_safi_ix = -1; |
| 3181 | |
| 3182 | /* Fall through */ |
| 3183 | |
| 3184 | case RIB_TABLES_ITER_S_ITERATING: |
| 3185 | iter->afi_safi_ix++; |
| 3186 | while (1) |
| 3187 | { |
| 3188 | |
| 3189 | while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis)) |
| 3190 | { |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3191 | table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi, |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3192 | afi_safis[iter->afi_safi_ix].safi, |
| 3193 | iter->vrf_id); |
| 3194 | if (table) |
| 3195 | break; |
| 3196 | |
| 3197 | iter->afi_safi_ix++; |
| 3198 | } |
| 3199 | |
| 3200 | /* |
| 3201 | * Found another table in this vrf. |
| 3202 | */ |
| 3203 | if (table) |
| 3204 | break; |
| 3205 | |
| 3206 | /* |
| 3207 | * Done with all tables in the current vrf, go to the next |
| 3208 | * one. |
| 3209 | */ |
| 3210 | if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id)) |
| 3211 | break; |
| 3212 | |
| 3213 | iter->afi_safi_ix = 0; |
| 3214 | } |
| 3215 | |
| 3216 | break; |
| 3217 | |
| 3218 | case RIB_TABLES_ITER_S_DONE: |
| 3219 | return NULL; |
| 3220 | } |
| 3221 | |
| 3222 | if (table) |
| 3223 | iter->state = RIB_TABLES_ITER_S_ITERATING; |
| 3224 | else |
| 3225 | iter->state = RIB_TABLES_ITER_S_DONE; |
| 3226 | |
| 3227 | return table; |
| 3228 | } |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3229 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 3230 | /* Lookup VRF by identifier. */ |
| 3231 | struct zebra_vrf * |
| 3232 | zebra_vrf_lookup (vrf_id_t vrf_id) |
| 3233 | { |
| 3234 | return vrf_info_lookup (vrf_id); |
| 3235 | } |
| 3236 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3237 | /* |
| 3238 | * Create a routing table for the specific AFI/SAFI in the given VRF. |
| 3239 | */ |
| 3240 | static void |
| 3241 | zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi) |
| 3242 | { |
| 3243 | rib_table_info_t *info; |
| 3244 | struct route_table *table; |
| 3245 | |
| 3246 | assert (!zvrf->table[afi][safi]); |
| 3247 | |
| 3248 | table = route_table_init (); |
| 3249 | zvrf->table[afi][safi] = table; |
| 3250 | |
| 3251 | info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info)); |
| 3252 | info->zvrf = zvrf; |
| 3253 | info->afi = afi; |
| 3254 | info->safi = safi; |
| 3255 | table->info = info; |
| 3256 | } |
| 3257 | |
| 3258 | /* Allocate new zebra VRF. */ |
| 3259 | struct zebra_vrf * |
| 3260 | zebra_vrf_alloc (vrf_id_t vrf_id) |
| 3261 | { |
| 3262 | struct zebra_vrf *zvrf; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 3263 | #ifdef HAVE_NETLINK |
| 3264 | char nl_name[64]; |
| 3265 | #endif |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3266 | |
| 3267 | zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf)); |
| 3268 | |
| 3269 | /* Allocate routing table and static table. */ |
| 3270 | zebra_vrf_table_create (zvrf, AFI_IP, SAFI_UNICAST); |
| 3271 | zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_UNICAST); |
| 3272 | zvrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 3273 | zvrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
| 3274 | zebra_vrf_table_create (zvrf, AFI_IP, SAFI_MULTICAST); |
| 3275 | zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_MULTICAST); |
| 3276 | zvrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init (); |
| 3277 | zvrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init (); |
| 3278 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 3279 | zvrf->rnh_table[AFI_IP] = route_table_init(); |
| 3280 | zvrf->rnh_table[AFI_IP6] = route_table_init(); |
| 3281 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3282 | /* Set VRF ID */ |
| 3283 | zvrf->vrf_id = vrf_id; |
| 3284 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 3285 | #ifdef HAVE_NETLINK |
| 3286 | /* Initialize netlink sockets */ |
| 3287 | snprintf (nl_name, 64, "netlink-listen (vrf %u)", vrf_id); |
| 3288 | zvrf->netlink.sock = -1; |
| 3289 | zvrf->netlink.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name); |
| 3290 | |
| 3291 | snprintf (nl_name, 64, "netlink-cmd (vrf %u)", vrf_id); |
| 3292 | zvrf->netlink_cmd.sock = -1; |
| 3293 | zvrf->netlink_cmd.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name); |
| 3294 | #endif |
| 3295 | |
Feng Lu | 41f44a2 | 2015-05-22 11:39:56 +0200 | [diff] [blame] | 3296 | return zvrf; |
| 3297 | } |
| 3298 | |
| 3299 | /* Lookup the routing table in an enabled VRF. */ |
| 3300 | struct route_table * |
| 3301 | zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id) |
| 3302 | { |
| 3303 | struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id); |
| 3304 | |
| 3305 | if (!zvrf) |
| 3306 | return NULL; |
| 3307 | |
| 3308 | if (afi >= AFI_MAX || safi >= SAFI_MAX) |
| 3309 | return NULL; |
| 3310 | |
| 3311 | return zvrf->table[afi][safi]; |
| 3312 | } |
| 3313 | |
| 3314 | /* Lookup the static routing table in a VRF. */ |
| 3315 | struct route_table * |
| 3316 | zebra_vrf_static_table (afi_t afi, safi_t safi, vrf_id_t vrf_id) |
| 3317 | { |
| 3318 | struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id); |
| 3319 | |
| 3320 | if (!zvrf) |
| 3321 | return NULL; |
| 3322 | |
| 3323 | if (afi >= AFI_MAX || safi >= SAFI_MAX) |
| 3324 | return NULL; |
| 3325 | |
| 3326 | return zvrf->stable[afi][safi]; |
| 3327 | } |
| 3328 | |