paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common ioctl functions. |
| 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 "linklist.h" |
| 26 | #include "if.h" |
| 27 | #include "prefix.h" |
| 28 | #include "ioctl.h" |
| 29 | #include "log.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/rib.h" |
| 33 | #include "zebra/rt.h" |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 34 | #include "zebra/interface.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 | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | /* clear and set interface name string */ |
| 39 | void |
| 40 | ifreq_set_name (struct ifreq *ifreq, struct interface *ifp) |
| 41 | { |
| 42 | strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ); |
| 43 | } |
| 44 | |
| 45 | /* call ioctl system call */ |
| 46 | int |
| 47 | if_ioctl (u_long request, caddr_t buffer) |
| 48 | { |
| 49 | int sock; |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 50 | int ret; |
| 51 | int err; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 53 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 54 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | sock = socket (AF_INET, SOCK_DGRAM, 0); |
| 56 | if (sock < 0) |
| 57 | { |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 58 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 59 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 60 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 61 | zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | exit (1); |
| 63 | } |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 64 | if ((ret = ioctl (sock, request, buffer)) < 0) |
| 65 | err = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 66 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 67 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | close (sock); |
| 69 | |
| 70 | if (ret < 0) |
| 71 | { |
| 72 | errno = err; |
| 73 | return ret; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | #ifdef HAVE_IPV6 |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 79 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | if_ioctl_ipv6 (u_long request, caddr_t buffer) |
| 81 | { |
| 82 | int sock; |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 83 | int ret; |
| 84 | int err; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 86 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 87 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | sock = socket (AF_INET6, SOCK_DGRAM, 0); |
| 89 | if (sock < 0) |
| 90 | { |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 91 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 92 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 93 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 94 | zlog_err("Cannot create IPv6 datagram socket: %s", |
| 95 | safe_strerror(save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | exit (1); |
| 97 | } |
| 98 | |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 99 | if ((ret = ioctl (sock, request, buffer)) < 0) |
| 100 | err = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 101 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 102 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | close (sock); |
| 104 | |
| 105 | if (ret < 0) |
| 106 | { |
| 107 | errno = err; |
| 108 | return ret; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | #endif /* HAVE_IPV6 */ |
| 113 | |
| 114 | /* |
| 115 | * get interface metric |
| 116 | * -- if value is not avaliable set -1 |
| 117 | */ |
| 118 | void |
| 119 | if_get_metric (struct interface *ifp) |
| 120 | { |
| 121 | #ifdef SIOCGIFMETRIC |
| 122 | struct ifreq ifreq; |
| 123 | |
| 124 | ifreq_set_name (&ifreq, ifp); |
| 125 | |
| 126 | if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0) |
| 127 | return; |
| 128 | ifp->metric = ifreq.ifr_metric; |
| 129 | if (ifp->metric == 0) |
| 130 | ifp->metric = 1; |
| 131 | #else /* SIOCGIFMETRIC */ |
| 132 | ifp->metric = -1; |
| 133 | #endif /* SIOCGIFMETRIC */ |
| 134 | } |
| 135 | |
| 136 | /* get interface MTU */ |
| 137 | void |
| 138 | if_get_mtu (struct interface *ifp) |
| 139 | { |
| 140 | struct ifreq ifreq; |
| 141 | |
| 142 | ifreq_set_name (&ifreq, ifp); |
| 143 | |
| 144 | #if defined(SIOCGIFMTU) |
| 145 | if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0) |
| 146 | { |
| 147 | zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)"); |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 148 | ifp->mtu6 = ifp->mtu = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | #ifdef SUNOS_5 |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 153 | ifp->mtu6 = ifp->mtu = ifreq.ifr_metric; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | #else |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 155 | ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | #endif /* SUNOS_5 */ |
| 157 | |
| 158 | #else |
| 159 | zlog (NULL, LOG_INFO, "Can't lookup mtu on this system"); |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 160 | ifp->mtu6 = ifp->mtu = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
| 164 | #ifdef HAVE_NETLINK |
| 165 | /* Interface address setting via netlink interface. */ |
| 166 | int |
| 167 | if_set_prefix (struct interface *ifp, struct connected *ifc) |
| 168 | { |
| 169 | return kernel_address_add_ipv4 (ifp, ifc); |
| 170 | } |
| 171 | |
| 172 | /* Interface address is removed using netlink interface. */ |
| 173 | int |
| 174 | if_unset_prefix (struct interface *ifp, struct connected *ifc) |
| 175 | { |
| 176 | return kernel_address_delete_ipv4 (ifp, ifc); |
| 177 | } |
| 178 | #else /* ! HAVE_NETLINK */ |
| 179 | #ifdef HAVE_IFALIASREQ |
| 180 | /* Set up interface's IP address, netmask (and broadcas? ). *BSD may |
| 181 | has ifaliasreq structure. */ |
| 182 | int |
| 183 | if_set_prefix (struct interface *ifp, struct connected *ifc) |
| 184 | { |
| 185 | int ret; |
| 186 | struct ifaliasreq addreq; |
| 187 | struct sockaddr_in addr; |
| 188 | struct sockaddr_in mask; |
| 189 | struct prefix_ipv4 *p; |
| 190 | |
| 191 | p = (struct prefix_ipv4 *) ifc->address; |
| 192 | |
| 193 | memset (&addreq, 0, sizeof addreq); |
| 194 | strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name); |
| 195 | |
| 196 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 197 | addr.sin_addr = p->prefix; |
| 198 | addr.sin_family = p->family; |
| 199 | #ifdef HAVE_SIN_LEN |
| 200 | addr.sin_len = sizeof (struct sockaddr_in); |
| 201 | #endif |
| 202 | memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in)); |
| 203 | |
| 204 | memset (&mask, 0, sizeof (struct sockaddr_in)); |
| 205 | masklen2ip (p->prefixlen, &mask.sin_addr); |
| 206 | mask.sin_family = p->family; |
| 207 | #ifdef HAVE_SIN_LEN |
| 208 | mask.sin_len = sizeof (struct sockaddr_in); |
| 209 | #endif |
| 210 | memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in)); |
| 211 | |
| 212 | ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* Set up interface's IP address, netmask (and broadcas? ). *BSD may |
| 219 | has ifaliasreq structure. */ |
| 220 | int |
| 221 | if_unset_prefix (struct interface *ifp, struct connected *ifc) |
| 222 | { |
| 223 | int ret; |
| 224 | struct ifaliasreq addreq; |
| 225 | struct sockaddr_in addr; |
| 226 | struct sockaddr_in mask; |
| 227 | struct prefix_ipv4 *p; |
| 228 | |
| 229 | p = (struct prefix_ipv4 *)ifc->address; |
| 230 | |
| 231 | memset (&addreq, 0, sizeof addreq); |
| 232 | strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name); |
| 233 | |
| 234 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 235 | addr.sin_addr = p->prefix; |
| 236 | addr.sin_family = p->family; |
| 237 | #ifdef HAVE_SIN_LEN |
| 238 | addr.sin_len = sizeof (struct sockaddr_in); |
| 239 | #endif |
| 240 | memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in)); |
| 241 | |
| 242 | memset (&mask, 0, sizeof (struct sockaddr_in)); |
| 243 | masklen2ip (p->prefixlen, &mask.sin_addr); |
| 244 | mask.sin_family = p->family; |
| 245 | #ifdef HAVE_SIN_LEN |
| 246 | mask.sin_len = sizeof (struct sockaddr_in); |
| 247 | #endif |
| 248 | memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in)); |
| 249 | |
| 250 | ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq); |
| 251 | if (ret < 0) |
| 252 | return ret; |
| 253 | return 0; |
| 254 | } |
| 255 | #else |
| 256 | /* Set up interface's address, netmask (and broadcas? ). Linux or |
| 257 | Solaris uses ifname:number semantics to set IP address aliases. */ |
| 258 | int |
| 259 | if_set_prefix (struct interface *ifp, struct connected *ifc) |
| 260 | { |
| 261 | int ret; |
| 262 | struct ifreq ifreq; |
| 263 | struct sockaddr_in addr; |
| 264 | struct sockaddr_in broad; |
| 265 | struct sockaddr_in mask; |
| 266 | struct prefix_ipv4 ifaddr; |
| 267 | struct prefix_ipv4 *p; |
| 268 | |
| 269 | p = (struct prefix_ipv4 *) ifc->address; |
| 270 | |
| 271 | ifaddr = *p; |
| 272 | |
| 273 | ifreq_set_name (&ifreq, ifp); |
| 274 | |
| 275 | addr.sin_addr = p->prefix; |
| 276 | addr.sin_family = p->family; |
| 277 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); |
| 278 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq); |
| 279 | if (ret < 0) |
| 280 | return ret; |
| 281 | |
| 282 | /* We need mask for make broadcast addr. */ |
| 283 | masklen2ip (p->prefixlen, &mask.sin_addr); |
| 284 | |
| 285 | if (if_is_broadcast (ifp)) |
| 286 | { |
| 287 | apply_mask_ipv4 (&ifaddr); |
| 288 | addr.sin_addr = ifaddr.prefix; |
| 289 | |
| 290 | broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr); |
| 291 | broad.sin_family = p->family; |
| 292 | |
| 293 | memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in)); |
| 294 | ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | mask.sin_family = p->family; |
| 300 | #ifdef SUNOS_5 |
| 301 | memcpy (&mask, &ifreq.ifr_addr, sizeof (mask)); |
| 302 | #else |
| 303 | memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in)); |
| 304 | #endif /* SUNOS5 */ |
| 305 | ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq); |
| 306 | if (ret < 0) |
| 307 | return ret; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* Set up interface's address, netmask (and broadcas? ). Linux or |
| 313 | Solaris uses ifname:number semantics to set IP address aliases. */ |
| 314 | int |
| 315 | if_unset_prefix (struct interface *ifp, struct connected *ifc) |
| 316 | { |
| 317 | int ret; |
| 318 | struct ifreq ifreq; |
| 319 | struct sockaddr_in addr; |
| 320 | struct prefix_ipv4 *p; |
| 321 | |
| 322 | p = (struct prefix_ipv4 *) ifc->address; |
| 323 | |
| 324 | ifreq_set_name (&ifreq, ifp); |
| 325 | |
| 326 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 327 | addr.sin_family = p->family; |
| 328 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); |
| 329 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq); |
| 330 | if (ret < 0) |
| 331 | return ret; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | #endif /* HAVE_IFALIASREQ */ |
| 336 | #endif /* HAVE_NETLINK */ |
| 337 | |
| 338 | /* get interface flags */ |
| 339 | void |
| 340 | if_get_flags (struct interface *ifp) |
| 341 | { |
| 342 | int ret; |
| 343 | struct ifreq ifreq; |
| 344 | |
| 345 | ifreq_set_name (&ifreq, ifp); |
| 346 | |
| 347 | ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq); |
| 348 | if (ret < 0) |
| 349 | { |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 350 | zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 354 | if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* Set interface flags */ |
| 358 | int |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 359 | if_set_flags (struct interface *ifp, uint64_t flags) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | { |
| 361 | int ret; |
| 362 | struct ifreq ifreq; |
| 363 | |
paul | d1724b6 | 2003-10-22 02:41:52 +0000 | [diff] [blame] | 364 | memset (&ifreq, 0, sizeof(struct ifreq)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | ifreq_set_name (&ifreq, ifp); |
| 366 | |
| 367 | ifreq.ifr_flags = ifp->flags; |
| 368 | ifreq.ifr_flags |= flags; |
| 369 | |
| 370 | ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq); |
| 371 | |
| 372 | if (ret < 0) |
| 373 | { |
| 374 | zlog_info ("can't set interface flags"); |
| 375 | return ret; |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* Unset interface's flag. */ |
| 381 | int |
paul | 5c78b3d | 2006-01-25 04:31:40 +0000 | [diff] [blame] | 382 | if_unset_flags (struct interface *ifp, uint64_t flags) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | { |
| 384 | int ret; |
| 385 | struct ifreq ifreq; |
| 386 | |
paul | d1724b6 | 2003-10-22 02:41:52 +0000 | [diff] [blame] | 387 | memset (&ifreq, 0, sizeof(struct ifreq)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | ifreq_set_name (&ifreq, ifp); |
| 389 | |
| 390 | ifreq.ifr_flags = ifp->flags; |
| 391 | ifreq.ifr_flags &= ~flags; |
| 392 | |
| 393 | ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq); |
| 394 | |
| 395 | if (ret < 0) |
| 396 | { |
| 397 | zlog_info ("can't unset interface flags"); |
| 398 | return ret; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | #ifdef HAVE_IPV6 |
| 404 | |
| 405 | #ifdef LINUX_IPV6 |
| 406 | #ifndef _LINUX_IN6_H |
| 407 | /* linux/include/net/ipv6.h */ |
| 408 | struct in6_ifreq |
| 409 | { |
| 410 | struct in6_addr ifr6_addr; |
| 411 | u_int32_t ifr6_prefixlen; |
| 412 | int ifr6_ifindex; |
| 413 | }; |
| 414 | #endif /* _LINUX_IN6_H */ |
| 415 | |
| 416 | /* Interface's address add/delete functions. */ |
| 417 | int |
| 418 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) |
| 419 | { |
| 420 | int ret; |
| 421 | struct prefix_ipv6 *p; |
| 422 | struct in6_ifreq ifreq; |
| 423 | |
| 424 | p = (struct prefix_ipv6 *) ifc->address; |
| 425 | |
| 426 | memset (&ifreq, 0, sizeof (struct in6_ifreq)); |
| 427 | |
| 428 | memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr)); |
| 429 | ifreq.ifr6_ifindex = ifp->ifindex; |
| 430 | ifreq.ifr6_prefixlen = p->prefixlen; |
| 431 | |
| 432 | ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | int |
| 438 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) |
| 439 | { |
| 440 | int ret; |
| 441 | struct prefix_ipv6 *p; |
| 442 | struct in6_ifreq ifreq; |
| 443 | |
| 444 | p = (struct prefix_ipv6 *) ifc->address; |
| 445 | |
| 446 | memset (&ifreq, 0, sizeof (struct in6_ifreq)); |
| 447 | |
| 448 | memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr)); |
| 449 | ifreq.ifr6_ifindex = ifp->ifindex; |
| 450 | ifreq.ifr6_prefixlen = p->prefixlen; |
| 451 | |
| 452 | ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq); |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | #else /* LINUX_IPV6 */ |
| 457 | #ifdef HAVE_IN6_ALIASREQ |
| 458 | #ifndef ND6_INFINITE_LIFETIME |
| 459 | #define ND6_INFINITE_LIFETIME 0xffffffffL |
| 460 | #endif /* ND6_INFINITE_LIFETIME */ |
| 461 | int |
| 462 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) |
| 463 | { |
| 464 | int ret; |
| 465 | struct in6_aliasreq addreq; |
| 466 | struct sockaddr_in6 addr; |
| 467 | struct sockaddr_in6 mask; |
| 468 | struct prefix_ipv6 *p; |
| 469 | |
| 470 | p = (struct prefix_ipv6 * ) ifc->address; |
| 471 | |
| 472 | memset (&addreq, 0, sizeof addreq); |
| 473 | strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name); |
| 474 | |
| 475 | memset (&addr, 0, sizeof (struct sockaddr_in6)); |
| 476 | addr.sin6_addr = p->prefix; |
| 477 | addr.sin6_family = p->family; |
| 478 | #ifdef HAVE_SIN_LEN |
| 479 | addr.sin6_len = sizeof (struct sockaddr_in6); |
| 480 | #endif |
| 481 | memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6)); |
| 482 | |
| 483 | memset (&mask, 0, sizeof (struct sockaddr_in6)); |
| 484 | masklen2ip6 (p->prefixlen, &mask.sin6_addr); |
| 485 | mask.sin6_family = p->family; |
| 486 | #ifdef HAVE_SIN_LEN |
| 487 | mask.sin6_len = sizeof (struct sockaddr_in6); |
| 488 | #endif |
| 489 | memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6)); |
paul | 0e4f190 | 2003-06-12 02:13:45 +0000 | [diff] [blame] | 490 | |
| 491 | addreq.ifra_lifetime.ia6t_vltime = 0xffffffff; |
| 492 | addreq.ifra_lifetime.ia6t_pltime = 0xffffffff; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 493 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 494 | #ifdef HAVE_IFRA_LIFETIME |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; |
| 496 | addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 497 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | |
| 499 | ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int |
| 506 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) |
| 507 | { |
| 508 | int ret; |
| 509 | struct in6_aliasreq addreq; |
| 510 | struct sockaddr_in6 addr; |
| 511 | struct sockaddr_in6 mask; |
| 512 | struct prefix_ipv6 *p; |
| 513 | |
| 514 | p = (struct prefix_ipv6 *) ifc->address; |
| 515 | |
| 516 | memset (&addreq, 0, sizeof addreq); |
| 517 | strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name); |
| 518 | |
| 519 | memset (&addr, 0, sizeof (struct sockaddr_in6)); |
| 520 | addr.sin6_addr = p->prefix; |
| 521 | addr.sin6_family = p->family; |
| 522 | #ifdef HAVE_SIN_LEN |
| 523 | addr.sin6_len = sizeof (struct sockaddr_in6); |
| 524 | #endif |
| 525 | memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6)); |
| 526 | |
| 527 | memset (&mask, 0, sizeof (struct sockaddr_in6)); |
| 528 | masklen2ip6 (p->prefixlen, &mask.sin6_addr); |
| 529 | mask.sin6_family = p->family; |
| 530 | #ifdef HAVE_SIN_LEN |
| 531 | mask.sin6_len = sizeof (struct sockaddr_in6); |
| 532 | #endif |
| 533 | memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6)); |
| 534 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 535 | #ifdef HAVE_IFRA_LIFETIME |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; |
| 537 | addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 538 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | |
| 540 | ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq); |
| 541 | if (ret < 0) |
| 542 | return ret; |
| 543 | return 0; |
| 544 | } |
| 545 | #else |
| 546 | int |
| 547 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) |
| 548 | { |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int |
| 553 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) |
| 554 | { |
| 555 | return 0; |
| 556 | } |
| 557 | #endif /* HAVE_IN6_ALIASREQ */ |
| 558 | |
| 559 | #endif /* LINUX_IPV6 */ |
| 560 | |
| 561 | #endif /* HAVE_IPV6 */ |