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