paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common ioctl functions for Solaris. |
| 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 | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 30 | #include "privs.h" |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 31 | |
| 32 | #include "zebra/rib.h" |
| 33 | #include "zebra/rt.h" |
| 34 | |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 35 | extern struct zebra_privs_t zserv_privs; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 36 | |
| 37 | /* clear and set interface name string */ |
| 38 | void |
| 39 | lifreq_set_name (struct lifreq *lifreq, struct interface *ifp) |
| 40 | { |
| 41 | strncpy (lifreq->lifr_name, ifp->name, IFNAMSIZ); |
| 42 | } |
| 43 | |
| 44 | /* call ioctl system call */ |
| 45 | int |
| 46 | if_ioctl (u_long request, caddr_t buffer) |
| 47 | { |
| 48 | int sock; |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 49 | int ret; |
| 50 | int err; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 51 | |
| 52 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 53 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 54 | |
| 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 | 8842468 | 2004-05-09 18:21:35 +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 | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 62 | exit (1); |
| 63 | } |
| 64 | |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 65 | if ((ret = ioctl (sock, request, buffer)) < 0) |
| 66 | err = errno; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 67 | |
| 68 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 69 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 70 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 71 | close (sock); |
| 72 | |
| 73 | if (ret < 0) |
| 74 | { |
| 75 | errno = err; |
| 76 | return ret; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 81 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 82 | int |
| 83 | if_ioctl_ipv6 (u_long request, caddr_t buffer) |
| 84 | { |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 85 | #ifdef HAVE_IPV6 |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 86 | int sock; |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 87 | int ret; |
| 88 | int err; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 89 | |
| 90 | if (zserv_privs.change(ZPRIVS_RAISE)) |
| 91 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 92 | |
| 93 | sock = socket (AF_INET6, SOCK_DGRAM, 0); |
| 94 | if (sock < 0) |
| 95 | { |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 96 | int save_errno = errno; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 97 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 98 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
ajs | 6a52d0d | 2005-01-30 18:49:28 +0000 | [diff] [blame] | 99 | zlog_err("Cannot create IPv6 datagram socket: %s", |
| 100 | safe_strerror(save_errno)); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 101 | exit (1); |
| 102 | } |
| 103 | |
ajs | 4460e7a | 2005-01-29 17:07:40 +0000 | [diff] [blame] | 104 | if ((ret = ioctl (sock, request, buffer)) < 0) |
| 105 | err = errno; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 106 | |
| 107 | if (zserv_privs.change(ZPRIVS_LOWER)) |
| 108 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 109 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 110 | close (sock); |
| 111 | |
| 112 | if (ret < 0) |
| 113 | { |
| 114 | errno = err; |
| 115 | return ret; |
| 116 | } |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 117 | #endif /* HAVE_IPV6 */ |
| 118 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * get interface metric |
| 124 | * -- if value is not avaliable set -1 |
| 125 | */ |
| 126 | void |
| 127 | if_get_metric (struct interface *ifp) |
| 128 | { |
| 129 | struct lifreq lifreq; |
| 130 | int ret; |
| 131 | |
| 132 | lifreq_set_name (&lifreq, ifp); |
| 133 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 134 | if (ifp->flags & IFF_IPV4) |
| 135 | ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq); |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 136 | #ifdef SOLARIS_IPV6 |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 137 | else if (ifp->flags & IFF_IPV6) |
| 138 | ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq); |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 139 | #endif /* SOLARIS_IPV6 */ |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 140 | else |
| 141 | ret = -1; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 142 | |
| 143 | if (ret < 0) |
| 144 | return; |
| 145 | |
| 146 | ifp->metric = lifreq.lifr_metric; |
| 147 | |
| 148 | if (ifp->metric == 0) |
| 149 | ifp->metric = 1; |
| 150 | } |
| 151 | |
| 152 | /* get interface MTU */ |
| 153 | void |
| 154 | if_get_mtu (struct interface *ifp) |
| 155 | { |
| 156 | struct lifreq lifreq; |
| 157 | int ret; |
| 158 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 159 | if (ifp->flags & IFF_IPV4) |
| 160 | { |
| 161 | lifreq_set_name (&lifreq, ifp); |
| 162 | ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq); |
| 163 | if (ret < 0) |
| 164 | { |
| 165 | zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)", |
| 166 | ifp->name); |
| 167 | ifp->mtu = -1; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | ifp->mtu = lifreq.lifr_metric; |
| 172 | } |
| 173 | } |
| 174 | |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 175 | #ifdef HAVE_IPV6 |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 176 | if ((ifp->flags & IFF_IPV6) == 0) |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 177 | return; |
| 178 | |
| 179 | memset(&lifreq, 0, sizeof(lifreq)); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 180 | lifreq_set_name (&lifreq, ifp); |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 181 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 182 | ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq); |
| 183 | if (ret < 0) |
| 184 | { |
| 185 | zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name); |
| 186 | ifp->mtu6 = -1; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | ifp->mtu6 = lifreq.lifr_metric; |
| 191 | } |
paul | 5b73a67 | 2004-07-23 15:26:14 +0000 | [diff] [blame] | 192 | #endif /* HAVE_IPV6 */ |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Set up interface's address, netmask (and broadcast? ). |
| 196 | Solaris uses ifname:number semantics to set IP address aliases. */ |
| 197 | int |
| 198 | if_set_prefix (struct interface *ifp, struct connected *ifc) |
| 199 | { |
| 200 | int ret; |
| 201 | struct ifreq ifreq; |
| 202 | struct sockaddr_in addr; |
| 203 | struct sockaddr_in broad; |
| 204 | struct sockaddr_in mask; |
| 205 | struct prefix_ipv4 ifaddr; |
| 206 | struct prefix_ipv4 *p; |
| 207 | |
| 208 | p = (struct prefix_ipv4 *) ifc->address; |
| 209 | |
| 210 | ifaddr = *p; |
| 211 | |
| 212 | strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ); |
| 213 | |
| 214 | addr.sin_addr = p->prefix; |
| 215 | addr.sin_family = p->family; |
| 216 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); |
| 217 | |
| 218 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq); |
| 219 | |
| 220 | if (ret < 0) |
| 221 | return ret; |
| 222 | |
| 223 | /* We need mask for make broadcast addr. */ |
| 224 | masklen2ip (p->prefixlen, &mask.sin_addr); |
| 225 | |
| 226 | if (if_is_broadcast (ifp)) |
| 227 | { |
| 228 | apply_mask_ipv4 (&ifaddr); |
| 229 | addr.sin_addr = ifaddr.prefix; |
| 230 | |
| 231 | broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr); |
| 232 | broad.sin_family = p->family; |
| 233 | |
| 234 | memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in)); |
| 235 | ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq); |
| 236 | if (ret < 0) |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 237 | return ret; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | mask.sin_family = p->family; |
| 241 | #ifdef SUNOS_5 |
| 242 | memcpy (&mask, &ifreq.ifr_addr, sizeof (mask)); |
| 243 | #else |
| 244 | memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in)); |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 245 | #endif /* SUNOS_5 */ |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 246 | ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq); |
| 247 | |
| 248 | return ((ret < 0) ? ret : 0); |
| 249 | } |
| 250 | |
| 251 | /* Set up interface's address, netmask (and broadcast). |
| 252 | Solaris uses ifname:number semantics to set IP address aliases. */ |
| 253 | int |
| 254 | if_unset_prefix (struct interface *ifp, struct connected *ifc) |
| 255 | { |
| 256 | int ret; |
| 257 | struct ifreq ifreq; |
| 258 | struct sockaddr_in addr; |
| 259 | struct prefix_ipv4 *p; |
| 260 | |
| 261 | p = (struct prefix_ipv4 *) ifc->address; |
| 262 | |
| 263 | strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ); |
| 264 | |
| 265 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 266 | addr.sin_family = p->family; |
| 267 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); |
| 268 | |
| 269 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq); |
| 270 | |
| 271 | if (ret < 0) |
| 272 | return ret; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* get interface flags */ |
| 278 | void |
| 279 | if_get_flags (struct interface *ifp) |
| 280 | { |
| 281 | int ret; |
| 282 | struct lifreq lifreq; |
| 283 | unsigned long flags4 = 0, flags6 = 0; |
| 284 | |
| 285 | if (ifp->flags & IFF_IPV4) |
| 286 | { |
| 287 | lifreq_set_name (&lifreq, ifp); |
| 288 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 289 | ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 290 | |
| 291 | flags4 = (lifreq.lifr_flags & 0xffffffff); |
| 292 | if (!(flags4 & IFF_UP)) |
| 293 | flags4 &= ~IFF_IPV4; |
| 294 | } |
| 295 | |
| 296 | if (ifp->flags & IFF_IPV6) |
| 297 | { |
| 298 | lifreq_set_name (&lifreq, ifp); |
| 299 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 300 | ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 301 | |
| 302 | flags6 = (lifreq.lifr_flags & 0xffffffff); |
| 303 | if (!(flags6 & IFF_UP)) |
| 304 | flags6 &= ~IFF_IPV6; |
| 305 | } |
| 306 | |
| 307 | ifp->flags = (flags4 | flags6); |
| 308 | } |
| 309 | |
| 310 | /* Set interface flags */ |
| 311 | int |
| 312 | if_set_flags (struct interface *ifp, unsigned long flags) |
| 313 | { |
| 314 | int ret; |
| 315 | struct lifreq lifreq; |
| 316 | |
| 317 | lifreq_set_name (&lifreq, ifp); |
| 318 | |
| 319 | lifreq.lifr_flags = ifp->flags; |
| 320 | lifreq.lifr_flags |= flags; |
| 321 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 322 | if (ifp->flags & IFF_IPV4) |
| 323 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 324 | else if (ifp->flags & IFF_IPV6) |
| 325 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 326 | else |
| 327 | ret = -1; |
| 328 | |
| 329 | if (ret < 0) |
| 330 | zlog_info ("can't set interface flags on %s: %s", ifp->name, |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 331 | safe_strerror (errno)); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 332 | else |
| 333 | ret = 0; |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 334 | |
| 335 | return ret; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Unset interface's flag. */ |
| 339 | int |
| 340 | if_unset_flags (struct interface *ifp, unsigned long flags) |
| 341 | { |
| 342 | int ret; |
| 343 | struct lifreq lifreq; |
| 344 | |
| 345 | lifreq_set_name (&lifreq, ifp); |
| 346 | |
| 347 | lifreq.lifr_flags = ifp->flags; |
| 348 | lifreq.lifr_flags &= ~flags; |
| 349 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 350 | if (ifp->flags & IFF_IPV4) |
| 351 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 352 | else if (ifp->flags & IFF_IPV6) |
| 353 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 354 | else |
| 355 | ret = -1; |
| 356 | |
| 357 | if (ret < 0) |
| 358 | zlog_info ("can't unset interface flags"); |
| 359 | else |
| 360 | ret = 0; |
| 361 | |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 362 | return ret; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | #ifdef HAVE_IPV6 |
| 366 | |
| 367 | /* Interface's address add/delete functions. */ |
| 368 | int |
| 369 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) |
| 370 | { |
| 371 | char addrbuf[INET_ADDRSTRLEN]; |
| 372 | |
| 373 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), |
| 374 | addrbuf, sizeof (addrbuf)); |
| 375 | zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name); |
| 376 | |
| 377 | return 0; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | int |
| 382 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) |
| 383 | { |
| 384 | char addrbuf[INET_ADDRSTRLEN]; |
| 385 | |
| 386 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), |
| 387 | addrbuf, sizeof (addrbuf)); |
| 388 | zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name); |
| 389 | |
| 390 | return 0; |
| 391 | |
| 392 | } |
| 393 | |
| 394 | #endif /* HAVE_IPV6 */ |