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" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 36 | extern struct zebra_privs_t zserv_privs; |
| 37 | |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 38 | /* kernel socket export */ |
| 39 | extern int rtm_write (int message, union sockunion *dest, |
| 40 | union sockunion *mask, union sockunion *gate, |
| 41 | unsigned int index, int zebra_flags, int metric); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
| 43 | /* Adjust netmask socket length. Return value is a adjusted sin_len |
| 44 | value. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 45 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | sin_masklen (struct in_addr mask) |
| 47 | { |
| 48 | char *p, *lim; |
| 49 | int len; |
| 50 | struct sockaddr_in sin; |
| 51 | |
| 52 | if (mask.s_addr == 0) |
| 53 | return sizeof (long); |
| 54 | |
| 55 | sin.sin_addr = mask; |
| 56 | len = sizeof (struct sockaddr_in); |
| 57 | |
| 58 | lim = (char *) &sin.sin_addr; |
| 59 | p = lim + sizeof (sin.sin_addr); |
| 60 | |
| 61 | while (*--p == 0 && p >= lim) |
| 62 | len--; |
| 63 | return len; |
| 64 | } |
| 65 | |
| 66 | /* Interface between zebra message and rtm message. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 67 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family) |
| 69 | |
| 70 | { |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 71 | struct sockaddr_in *mask = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | struct sockaddr_in sin_dest, sin_mask, sin_gate; |
| 73 | struct nexthop *nexthop; |
| 74 | int nexthop_num = 0; |
| 75 | unsigned int ifindex = 0; |
| 76 | int gate = 0; |
| 77 | int error; |
| 78 | |
| 79 | memset (&sin_dest, 0, sizeof (struct sockaddr_in)); |
| 80 | sin_dest.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 81 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | sin_dest.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 83 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | sin_dest.sin_addr = p->u.prefix4; |
| 85 | |
| 86 | memset (&sin_mask, 0, sizeof (struct sockaddr_in)); |
| 87 | |
| 88 | memset (&sin_gate, 0, sizeof (struct sockaddr_in)); |
| 89 | sin_gate.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 90 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | sin_gate.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 92 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | |
| 94 | /* Make gateway. */ |
| 95 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 96 | { |
| 97 | gate = 0; |
| 98 | |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 99 | /* |
| 100 | * XXX We need to refrain from kernel operations in some cases, |
| 101 | * but this if statement seems overly cautious - what about |
| 102 | * other than ADD and DELETE? |
| 103 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | if ((cmd == RTM_ADD |
| 105 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 106 | || (cmd == RTM_DELETE |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | )) |
| 109 | { |
| 110 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 111 | { |
| 112 | if (nexthop->rtype == NEXTHOP_TYPE_IPV4 || |
| 113 | nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 114 | { |
| 115 | sin_gate.sin_addr = nexthop->rgate.ipv4; |
| 116 | gate = 1; |
| 117 | } |
| 118 | if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX |
| 119 | || nexthop->rtype == NEXTHOP_TYPE_IFNAME |
| 120 | || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 121 | ifindex = nexthop->rifindex; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | if (nexthop->type == NEXTHOP_TYPE_IPV4 || |
| 126 | nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 127 | { |
| 128 | sin_gate.sin_addr = nexthop->gate.ipv4; |
| 129 | gate = 1; |
| 130 | } |
| 131 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 132 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 133 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 134 | ifindex = nexthop->ifindex; |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 135 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) |
| 136 | { |
| 137 | struct in_addr loopback; |
| 138 | loopback.s_addr = htonl (INADDR_LOOPBACK); |
| 139 | sin_gate.sin_addr = loopback; |
| 140 | gate = 1; |
| 141 | } |
| 142 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | if (gate && p->prefixlen == 32) |
| 145 | mask = NULL; |
| 146 | else |
| 147 | { |
| 148 | masklen2ip (p->prefixlen, &sin_mask.sin_addr); |
gdt | 6083e1f | 2005-12-29 15:59:57 +0000 | [diff] [blame] | 149 | sin_mask.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 150 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | sin_mask.sin_len = sin_masklen (sin_mask.sin_addr); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 152 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | mask = &sin_mask; |
| 154 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 156 | error = rtm_write (cmd, |
| 157 | (union sockunion *)&sin_dest, |
| 158 | (union sockunion *)mask, |
| 159 | gate ? (union sockunion *)&sin_gate : NULL, |
| 160 | ifindex, |
| 161 | rib->flags, |
| 162 | rib->metric); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | |
| 164 | #if 0 |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 165 | if (error) |
| 166 | { |
| 167 | zlog_info ("kernel_rtm_ipv4(): nexthop %d add error=%d.", |
| 168 | nexthop_num, error); |
| 169 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | #endif |
Greg Troxel | dfdb8f1 | 2007-08-02 14:13:56 +0000 | [diff] [blame] | 171 | if (error == 0) |
| 172 | { |
| 173 | nexthop_num++; |
| 174 | if (cmd == RTM_ADD) |
| 175 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 176 | } |
Greg Troxel | f76594a | 2007-08-02 14:07:07 +0000 | [diff] [blame] | 177 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* If there is no useful nexthop then return. */ |
| 181 | if (nexthop_num == 0) |
| 182 | { |
| 183 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 184 | zlog_debug ("kernel_rtm_ipv4(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | return 0; /*XXX*/ |
| 189 | } |
| 190 | |
| 191 | int |
| 192 | kernel_add_ipv4 (struct prefix *p, struct rib *rib) |
| 193 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 194 | int route; |
| 195 | |
| 196 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 197 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 198 | route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET); |
| 199 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 200 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 201 | |
| 202 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int |
| 206 | kernel_delete_ipv4 (struct prefix *p, struct rib *rib) |
| 207 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 208 | int route; |
| 209 | |
| 210 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 211 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 212 | route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET); |
| 213 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 214 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 215 | |
| 216 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | #ifdef HAVE_IPV6 |
| 220 | |
| 221 | /* Calculate sin6_len value for netmask socket value. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 222 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | sin6_masklen (struct in6_addr mask) |
| 224 | { |
| 225 | struct sockaddr_in6 sin6; |
| 226 | char *p, *lim; |
| 227 | int len; |
| 228 | |
| 229 | #if defined (INRIA) |
| 230 | if (IN_ANYADDR6 (mask)) |
| 231 | return sizeof (long); |
| 232 | #else /* ! INRIA */ |
| 233 | if (IN6_IS_ADDR_UNSPECIFIED (&mask)) |
| 234 | return sizeof (long); |
| 235 | #endif /* ! INRIA */ |
| 236 | |
| 237 | sin6.sin6_addr = mask; |
| 238 | len = sizeof (struct sockaddr_in6); |
| 239 | |
| 240 | lim = (char *) & sin6.sin6_addr; |
| 241 | p = lim + sizeof (sin6.sin6_addr); |
| 242 | |
| 243 | while (*--p == 0 && p >= lim) |
| 244 | len--; |
| 245 | |
| 246 | return len; |
| 247 | } |
| 248 | |
| 249 | /* Interface between zebra message and rtm message. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 250 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | kernel_rtm_ipv6 (int message, struct prefix_ipv6 *dest, |
| 252 | struct in6_addr *gate, int index, int flags) |
| 253 | { |
| 254 | struct sockaddr_in6 *mask; |
| 255 | struct sockaddr_in6 sin_dest, sin_mask, sin_gate; |
| 256 | |
| 257 | memset (&sin_dest, 0, sizeof (struct sockaddr_in6)); |
| 258 | sin_dest.sin6_family = AF_INET6; |
| 259 | #ifdef SIN6_LEN |
| 260 | sin_dest.sin6_len = sizeof (struct sockaddr_in6); |
| 261 | #endif /* SIN6_LEN */ |
| 262 | |
| 263 | memset (&sin_mask, 0, sizeof (struct sockaddr_in6)); |
| 264 | |
| 265 | memset (&sin_gate, 0, sizeof (struct sockaddr_in6)); |
| 266 | sin_gate.sin6_family = AF_INET6; |
| 267 | #ifdef SIN6_LEN |
| 268 | sin_gate.sin6_len = sizeof (struct sockaddr_in6); |
| 269 | #endif /* SIN6_LEN */ |
| 270 | |
| 271 | sin_dest.sin6_addr = dest->prefix; |
| 272 | |
| 273 | if (gate) |
| 274 | memcpy (&sin_gate.sin6_addr, gate, sizeof (struct in6_addr)); |
| 275 | |
| 276 | /* Under kame set interface index to link local address. */ |
| 277 | #ifdef KAME |
| 278 | |
| 279 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ |
| 280 | do { \ |
| 281 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ |
| 282 | (a).s6_addr[3] = (i) & 0xff; \ |
| 283 | } while (0) |
| 284 | |
| 285 | if (gate && IN6_IS_ADDR_LINKLOCAL(gate)) |
| 286 | SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, index); |
| 287 | #endif /* KAME */ |
| 288 | |
| 289 | if (gate && dest->prefixlen == 128) |
| 290 | mask = NULL; |
| 291 | else |
| 292 | { |
| 293 | masklen2ip6 (dest->prefixlen, &sin_mask.sin6_addr); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 294 | sin_mask.sin6_family = AF_INET6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | #ifdef SIN6_LEN |
| 296 | sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr); |
| 297 | #endif /* SIN6_LEN */ |
| 298 | mask = &sin_mask; |
| 299 | } |
| 300 | |
| 301 | return rtm_write (message, |
| 302 | (union sockunion *) &sin_dest, |
| 303 | (union sockunion *) mask, |
| 304 | gate ? (union sockunion *)&sin_gate : NULL, |
| 305 | index, |
| 306 | flags, |
| 307 | 0); |
| 308 | } |
| 309 | |
| 310 | /* Interface between zebra message and rtm message. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 311 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib, |
| 313 | int family) |
| 314 | { |
| 315 | struct sockaddr_in6 *mask; |
| 316 | struct sockaddr_in6 sin_dest, sin_mask, sin_gate; |
| 317 | struct nexthop *nexthop; |
| 318 | int nexthop_num = 0; |
| 319 | unsigned int ifindex = 0; |
| 320 | int gate = 0; |
| 321 | int error; |
| 322 | |
| 323 | memset (&sin_dest, 0, sizeof (struct sockaddr_in6)); |
| 324 | sin_dest.sin6_family = AF_INET6; |
| 325 | #ifdef SIN6_LEN |
| 326 | sin_dest.sin6_len = sizeof (struct sockaddr_in6); |
| 327 | #endif /* SIN6_LEN */ |
| 328 | sin_dest.sin6_addr = p->u.prefix6; |
| 329 | |
| 330 | memset (&sin_mask, 0, sizeof (struct sockaddr_in6)); |
| 331 | |
| 332 | memset (&sin_gate, 0, sizeof (struct sockaddr_in6)); |
| 333 | sin_gate.sin6_family = AF_INET6; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 334 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | sin_gate.sin6_len = sizeof (struct sockaddr_in6); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 336 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | |
| 338 | /* Make gateway. */ |
| 339 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 340 | { |
| 341 | gate = 0; |
| 342 | |
| 343 | if ((cmd == RTM_ADD |
| 344 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 345 | || (cmd == RTM_DELETE |
| 346 | #if 0 |
| 347 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) |
| 348 | #endif |
| 349 | )) |
| 350 | { |
| 351 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 352 | { |
| 353 | if (nexthop->rtype == NEXTHOP_TYPE_IPV6 |
| 354 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME |
| 355 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 356 | { |
| 357 | sin_gate.sin6_addr = nexthop->rgate.ipv6; |
| 358 | gate = 1; |
| 359 | } |
| 360 | if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX |
| 361 | || nexthop->rtype == NEXTHOP_TYPE_IFNAME |
| 362 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME |
| 363 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 364 | ifindex = nexthop->rifindex; |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 369 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 370 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 371 | { |
| 372 | sin_gate.sin6_addr = nexthop->gate.ipv6; |
| 373 | gate = 1; |
| 374 | } |
| 375 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 376 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 377 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 378 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 379 | ifindex = nexthop->ifindex; |
| 380 | } |
| 381 | |
| 382 | if (cmd == RTM_ADD) |
| 383 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 384 | } |
| 385 | |
| 386 | /* Under kame set interface index to link local address. */ |
| 387 | #ifdef KAME |
| 388 | |
| 389 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ |
| 390 | do { \ |
| 391 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ |
| 392 | (a).s6_addr[3] = (i) & 0xff; \ |
| 393 | } while (0) |
| 394 | |
| 395 | if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr)) |
| 396 | SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex); |
| 397 | #endif /* KAME */ |
| 398 | |
| 399 | if (gate && p->prefixlen == 128) |
| 400 | mask = NULL; |
| 401 | else |
| 402 | { |
| 403 | masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 404 | sin_mask.sin6_family = AF_INET6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 405 | #ifdef SIN6_LEN |
| 406 | sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr); |
| 407 | #endif /* SIN6_LEN */ |
| 408 | mask = &sin_mask; |
| 409 | } |
| 410 | |
| 411 | error = rtm_write (cmd, |
| 412 | (union sockunion *) &sin_dest, |
| 413 | (union sockunion *) mask, |
| 414 | gate ? (union sockunion *)&sin_gate : NULL, |
| 415 | ifindex, |
| 416 | rib->flags, |
| 417 | rib->metric); |
| 418 | |
| 419 | #if 0 |
| 420 | if (error) |
| 421 | { |
| 422 | zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.", |
| 423 | nexthop_num, error); |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | nexthop_num++; |
| 428 | } |
| 429 | |
| 430 | /* If there is no useful nexthop then return. */ |
| 431 | if (nexthop_num == 0) |
| 432 | { |
| 433 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 434 | zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | return 0; /*XXX*/ |
| 439 | } |
| 440 | |
| 441 | int |
| 442 | kernel_add_ipv6 (struct prefix *p, struct rib *rib) |
| 443 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 444 | int route; |
| 445 | |
| 446 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 447 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 448 | route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6); |
| 449 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 450 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 451 | |
| 452 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | int |
| 456 | kernel_delete_ipv6 (struct prefix *p, struct rib *rib) |
| 457 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 458 | int route; |
| 459 | |
| 460 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 461 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 462 | route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6); |
| 463 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 464 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 465 | |
| 466 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Delete IPv6 route from the kernel. */ |
| 470 | int |
| 471 | kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate, |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 472 | unsigned int index, int flags, int table) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 473 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 474 | int route; |
| 475 | |
| 476 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 477 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 478 | route = kernel_rtm_ipv6 (RTM_DELETE, dest, gate, index, flags); |
| 479 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 480 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 481 | |
| 482 | return route; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | } |
| 484 | #endif /* HAVE_IPV6 */ |