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