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