paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Kernel routing table updates by routing socket. |
| 3 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "if.h" |
| 26 | #include "prefix.h" |
| 27 | #include "sockunion.h" |
| 28 | #include "log.h" |
| 29 | #include "str.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 30 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
| 32 | #include "zebra/debug.h" |
| 33 | #include "zebra/rib.h" |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 34 | #include "zebra/rt.h" |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 35 | #include "zebra/kernel_socket.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 37 | extern struct zebra_privs_t zserv_privs; |
| 38 | |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 39 | /* kernel socket export */ |
| 40 | extern int rtm_write (int message, union sockunion *dest, |
| 41 | union sockunion *mask, union sockunion *gate, |
| 42 | unsigned int index, int zebra_flags, int metric); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | |
| 44 | /* Adjust netmask socket length. Return value is a adjusted sin_len |
| 45 | value. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 46 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | sin_masklen (struct in_addr mask) |
| 48 | { |
| 49 | char *p, *lim; |
| 50 | int len; |
| 51 | struct sockaddr_in sin; |
| 52 | |
| 53 | if (mask.s_addr == 0) |
| 54 | return sizeof (long); |
| 55 | |
| 56 | sin.sin_addr = mask; |
| 57 | len = sizeof (struct sockaddr_in); |
| 58 | |
| 59 | lim = (char *) &sin.sin_addr; |
| 60 | p = lim + sizeof (sin.sin_addr); |
| 61 | |
| 62 | while (*--p == 0 && p >= lim) |
| 63 | len--; |
| 64 | return len; |
| 65 | } |
| 66 | |
| 67 | /* Interface between zebra message and rtm message. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 68 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family) |
| 70 | |
| 71 | { |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 72 | struct sockaddr_in *mask = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | struct sockaddr_in sin_dest, sin_mask, sin_gate; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 74 | struct nexthop *nexthop, *tnexthop; |
| 75 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | int nexthop_num = 0; |
| 77 | unsigned int ifindex = 0; |
| 78 | int gate = 0; |
| 79 | int error; |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 80 | char prefix_buf[PREFIX_STRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 82 | if (IS_ZEBRA_DEBUG_RIB) |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 83 | prefix2str (p, prefix_buf, sizeof(prefix_buf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | memset (&sin_dest, 0, sizeof (struct sockaddr_in)); |
| 85 | sin_dest.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 86 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | sin_dest.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 88 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | sin_dest.sin_addr = p->u.prefix4; |
| 90 | |
| 91 | memset (&sin_mask, 0, sizeof (struct sockaddr_in)); |
| 92 | |
| 93 | memset (&sin_gate, 0, sizeof (struct sockaddr_in)); |
| 94 | sin_gate.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 95 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | sin_gate.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 97 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | |
| 99 | /* Make gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 100 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 102 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 103 | continue; |
| 104 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | gate = 0; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 106 | char gate_buf[INET_ADDRSTRLEN] = "NULL"; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 108 | /* |
| 109 | * XXX We need to refrain from kernel operations in some cases, |
| 110 | * but this if statement seems overly cautious - what about |
| 111 | * other than ADD and DELETE? |
| 112 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | if ((cmd == RTM_ADD |
| 114 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 115 | || (cmd == RTM_DELETE |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 117 | )) |
| 118 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 119 | if (nexthop->type == NEXTHOP_TYPE_IPV4 || |
| 120 | nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 122 | sin_gate.sin_addr = nexthop->gate.ipv4; |
| 123 | gate = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 125 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 126 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 127 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 128 | ifindex = nexthop->ifindex; |
| 129 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 131 | struct in_addr loopback; |
| 132 | loopback.s_addr = htonl (INADDR_LOOPBACK); |
| 133 | sin_gate.sin_addr = loopback; |
| 134 | gate = 1; |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 135 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | if (gate && p->prefixlen == 32) |
| 138 | mask = NULL; |
| 139 | else |
| 140 | { |
| 141 | masklen2ip (p->prefixlen, &sin_mask.sin_addr); |
gdt | 6083e1f | 2005-12-29 15:59:57 +0000 | [diff] [blame] | 142 | sin_mask.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 143 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | sin_mask.sin_len = sin_masklen (sin_mask.sin_addr); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 145 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | mask = &sin_mask; |
| 147 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 149 | error = rtm_write (cmd, |
| 150 | (union sockunion *)&sin_dest, |
| 151 | (union sockunion *)mask, |
| 152 | gate ? (union sockunion *)&sin_gate : NULL, |
| 153 | ifindex, |
| 154 | rib->flags, |
| 155 | rib->metric); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 157 | if (IS_ZEBRA_DEBUG_RIB) |
| 158 | { |
| 159 | if (!gate) |
| 160 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 161 | zlog_debug ("%s: %s: attention! gate not found for rib %p", |
| 162 | __func__, prefix_buf, rib); |
David Lamparter | f7bf415 | 2013-10-22 17:10:21 +0000 | [diff] [blame] | 163 | rib_dump (p, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 164 | } |
| 165 | else |
| 166 | inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN); |
| 167 | } |
| 168 | |
| 169 | switch (error) |
| 170 | { |
| 171 | /* We only flag nexthops as being in FIB if rtm_write() did its work. */ |
| 172 | case ZEBRA_ERR_NOERROR: |
| 173 | nexthop_num++; |
| 174 | if (IS_ZEBRA_DEBUG_RIB) |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 175 | zlog_debug ("%s: %s: successfully did NH %s", |
| 176 | __func__, prefix_buf, gate_buf); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 177 | if (cmd == RTM_ADD) |
| 178 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 179 | break; |
| 180 | |
| 181 | /* The only valid case for this error is kernel's failure to install |
| 182 | * a multipath route, which is common for FreeBSD. This should be |
| 183 | * ignored silently, but logged as an error otherwise. |
| 184 | */ |
| 185 | case ZEBRA_ERR_RTEXIST: |
| 186 | if (cmd != RTM_ADD) |
| 187 | zlog_err ("%s: rtm_write() returned %d for command %d", |
| 188 | __func__, error, cmd); |
| 189 | continue; |
| 190 | break; |
| 191 | |
| 192 | /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't |
| 193 | * normal to get any other messages in ANY case. |
| 194 | */ |
| 195 | case ZEBRA_ERR_RTNOEXIST: |
| 196 | case ZEBRA_ERR_RTUNREACH: |
| 197 | default: |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 198 | zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s", |
| 199 | __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)), |
| 200 | error, lookup (rtm_type_str, cmd)); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | } /* if (cmd and flags make sense) */ |
| 204 | else |
| 205 | if (IS_ZEBRA_DEBUG_RIB) |
| 206 | zlog_debug ("%s: odd command %s for flags %d", |
Denis Ovsienko | 2d84452 | 2007-09-14 11:31:55 +0000 | [diff] [blame] | 207 | __func__, lookup (rtm_type_str, cmd), nexthop->flags); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 208 | } /* for (ALL_NEXTHOPS_RO(...))*/ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 209 | |
| 210 | /* If there was no useful nexthop, then complain. */ |
| 211 | if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL) |
| 212 | zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
| 214 | return 0; /*XXX*/ |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | kernel_add_ipv4 (struct prefix *p, struct rib *rib) |
| 219 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 220 | int route; |
| 221 | |
| 222 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 223 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 224 | route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET); |
| 225 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 226 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 227 | |
| 228 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int |
| 232 | kernel_delete_ipv4 (struct prefix *p, struct rib *rib) |
| 233 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 234 | int route; |
| 235 | |
| 236 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 237 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 238 | route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET); |
| 239 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 240 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 241 | |
| 242 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | #ifdef HAVE_IPV6 |
| 246 | |
| 247 | /* Calculate sin6_len value for netmask socket value. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 248 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | sin6_masklen (struct in6_addr mask) |
| 250 | { |
| 251 | struct sockaddr_in6 sin6; |
| 252 | char *p, *lim; |
| 253 | int len; |
| 254 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 255 | if (IN6_IS_ADDR_UNSPECIFIED (&mask)) |
| 256 | return sizeof (long); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 257 | |
| 258 | sin6.sin6_addr = mask; |
| 259 | len = sizeof (struct sockaddr_in6); |
| 260 | |
| 261 | lim = (char *) & sin6.sin6_addr; |
| 262 | p = lim + sizeof (sin6.sin6_addr); |
| 263 | |
| 264 | while (*--p == 0 && p >= lim) |
| 265 | len--; |
| 266 | |
| 267 | return len; |
| 268 | } |
| 269 | |
| 270 | /* Interface between zebra message and rtm message. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 271 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib, |
| 273 | int family) |
| 274 | { |
| 275 | struct sockaddr_in6 *mask; |
| 276 | struct sockaddr_in6 sin_dest, sin_mask, sin_gate; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 277 | struct nexthop *nexthop, *tnexthop; |
| 278 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | int nexthop_num = 0; |
| 280 | unsigned int ifindex = 0; |
| 281 | int gate = 0; |
| 282 | int error; |
| 283 | |
| 284 | memset (&sin_dest, 0, sizeof (struct sockaddr_in6)); |
| 285 | sin_dest.sin6_family = AF_INET6; |
| 286 | #ifdef SIN6_LEN |
| 287 | sin_dest.sin6_len = sizeof (struct sockaddr_in6); |
| 288 | #endif /* SIN6_LEN */ |
| 289 | sin_dest.sin6_addr = p->u.prefix6; |
| 290 | |
| 291 | memset (&sin_mask, 0, sizeof (struct sockaddr_in6)); |
| 292 | |
| 293 | memset (&sin_gate, 0, sizeof (struct sockaddr_in6)); |
| 294 | sin_gate.sin6_family = AF_INET6; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 295 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | sin_gate.sin6_len = sizeof (struct sockaddr_in6); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 297 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 298 | |
| 299 | /* Make gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 300 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 302 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 303 | continue; |
| 304 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | gate = 0; |
| 306 | |
| 307 | if ((cmd == RTM_ADD |
| 308 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 309 | || (cmd == RTM_DELETE |
| 310 | #if 0 |
| 311 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) |
| 312 | #endif |
| 313 | )) |
| 314 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 315 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 316 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 317 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 319 | sin_gate.sin6_addr = nexthop->gate.ipv6; |
| 320 | gate = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 322 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 323 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 324 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 325 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 326 | ifindex = nexthop->ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | |
| 328 | if (cmd == RTM_ADD) |
| 329 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 330 | } |
| 331 | |
| 332 | /* Under kame set interface index to link local address. */ |
| 333 | #ifdef KAME |
| 334 | |
| 335 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ |
| 336 | do { \ |
| 337 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ |
| 338 | (a).s6_addr[3] = (i) & 0xff; \ |
| 339 | } while (0) |
| 340 | |
| 341 | if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr)) |
| 342 | SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex); |
| 343 | #endif /* KAME */ |
| 344 | |
| 345 | if (gate && p->prefixlen == 128) |
| 346 | mask = NULL; |
| 347 | else |
| 348 | { |
| 349 | masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 350 | sin_mask.sin6_family = AF_INET6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | #ifdef SIN6_LEN |
| 352 | sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr); |
| 353 | #endif /* SIN6_LEN */ |
| 354 | mask = &sin_mask; |
| 355 | } |
| 356 | |
| 357 | error = rtm_write (cmd, |
| 358 | (union sockunion *) &sin_dest, |
| 359 | (union sockunion *) mask, |
| 360 | gate ? (union sockunion *)&sin_gate : NULL, |
| 361 | ifindex, |
| 362 | rib->flags, |
| 363 | rib->metric); |
| 364 | |
| 365 | #if 0 |
| 366 | if (error) |
| 367 | { |
| 368 | zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.", |
| 369 | nexthop_num, error); |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | nexthop_num++; |
| 374 | } |
| 375 | |
| 376 | /* If there is no useful nexthop then return. */ |
| 377 | if (nexthop_num == 0) |
| 378 | { |
| 379 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 380 | zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | return 0; /*XXX*/ |
| 385 | } |
| 386 | |
| 387 | int |
| 388 | kernel_add_ipv6 (struct prefix *p, struct rib *rib) |
| 389 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 390 | int route; |
| 391 | |
| 392 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 393 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 394 | route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6); |
| 395 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 396 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 397 | |
| 398 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | int |
| 402 | kernel_delete_ipv6 (struct prefix *p, struct rib *rib) |
| 403 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 404 | int route; |
| 405 | |
| 406 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 407 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 408 | route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6); |
| 409 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 410 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 411 | |
| 412 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 413 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | #endif /* HAVE_IPV6 */ |