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