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 | |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 277 | /* Solaris IFF_UP flag reflects only the primary interface as the |
| 278 | * routing socket only sends IFINFO for the primary interface. Hence |
| 279 | * ~IFF_UP does not per se imply all the logical interfaces are also |
| 280 | * down - which we only know of as addresses. Instead we must determine |
| 281 | * whether the interface really is up or not according to how many |
| 282 | * addresses are still attached. (Solaris always sends RTM_DELADDR if |
| 283 | * an interface, logical or not, goes ~IFF_UP). |
| 284 | * |
| 285 | * Ie, we mangle IFF_UP to reflect whether or not there are addresses |
| 286 | * left in struct connected, not the actual underlying IFF_UP flag |
| 287 | * (which corresponds to just one address of all the logical interfaces) |
| 288 | * |
| 289 | * Setting IFF_UP within zebra to administratively shutdown the |
| 290 | * interface will affect only the primary interface/address on Solaris. |
| 291 | */ |
| 292 | static inline void |
| 293 | if_mangle_up (struct interface *ifp) |
| 294 | { |
| 295 | if (listcount(ifp->connected) > 0) |
| 296 | SET_FLAG (ifp->flags, IFF_UP); |
| 297 | else |
| 298 | UNSET_FLAG (ifp->flags, IFF_UP); |
| 299 | } |
| 300 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 301 | /* get interface flags */ |
| 302 | void |
| 303 | if_get_flags (struct interface *ifp) |
| 304 | { |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 305 | int ret4, ret6; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 306 | struct lifreq lifreq; |
| 307 | unsigned long flags4 = 0, flags6 = 0; |
| 308 | |
| 309 | if (ifp->flags & IFF_IPV4) |
| 310 | { |
| 311 | lifreq_set_name (&lifreq, ifp); |
| 312 | |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 313 | ret4 = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq); |
| 314 | |
| 315 | if (!ret4) |
| 316 | flags4 = (lifreq.lifr_flags & 0xffffffff); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (ifp->flags & IFF_IPV6) |
| 320 | { |
| 321 | lifreq_set_name (&lifreq, ifp); |
| 322 | |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 323 | ret6 = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq); |
| 324 | |
| 325 | if (!ret6) |
| 326 | flags6 = (lifreq.lifr_flags & 0xffffffff); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 327 | } |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 328 | |
| 329 | /* only update flags if one of above succeeded */ |
| 330 | if ( !(ret4 && ret6) ) |
| 331 | ifp->flags = (flags4 | flags6); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 332 | |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 333 | if_mangle_up (ifp); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Set interface flags */ |
| 337 | int |
| 338 | if_set_flags (struct interface *ifp, unsigned long flags) |
| 339 | { |
| 340 | int ret; |
| 341 | struct lifreq lifreq; |
| 342 | |
| 343 | lifreq_set_name (&lifreq, ifp); |
| 344 | |
| 345 | lifreq.lifr_flags = ifp->flags; |
| 346 | lifreq.lifr_flags |= flags; |
| 347 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 348 | if (ifp->flags & IFF_IPV4) |
| 349 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 350 | else if (ifp->flags & IFF_IPV6) |
| 351 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 352 | else |
| 353 | ret = -1; |
| 354 | |
| 355 | if (ret < 0) |
| 356 | zlog_info ("can't set interface flags on %s: %s", ifp->name, |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 357 | safe_strerror (errno)); |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 358 | else |
| 359 | ret = 0; |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 360 | |
| 361 | return ret; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* Unset interface's flag. */ |
| 365 | int |
| 366 | if_unset_flags (struct interface *ifp, unsigned long flags) |
| 367 | { |
| 368 | int ret; |
| 369 | struct lifreq lifreq; |
| 370 | |
| 371 | lifreq_set_name (&lifreq, ifp); |
| 372 | |
| 373 | lifreq.lifr_flags = ifp->flags; |
| 374 | lifreq.lifr_flags &= ~flags; |
| 375 | |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 376 | if (ifp->flags & IFF_IPV4) |
| 377 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 378 | else if (ifp->flags & IFF_IPV6) |
| 379 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); |
| 380 | else |
| 381 | ret = -1; |
| 382 | |
| 383 | if (ret < 0) |
| 384 | zlog_info ("can't unset interface flags"); |
| 385 | else |
| 386 | ret = 0; |
| 387 | |
paul | 48a46fa | 2004-05-11 10:55:22 +0000 | [diff] [blame] | 388 | return ret; |
paul | 8842468 | 2004-05-09 18:21:35 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | #ifdef HAVE_IPV6 |
| 392 | |
| 393 | /* Interface's address add/delete functions. */ |
| 394 | int |
| 395 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) |
| 396 | { |
| 397 | char addrbuf[INET_ADDRSTRLEN]; |
| 398 | |
| 399 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), |
| 400 | addrbuf, sizeof (addrbuf)); |
| 401 | zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name); |
| 402 | |
| 403 | return 0; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | int |
| 408 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) |
| 409 | { |
| 410 | char addrbuf[INET_ADDRSTRLEN]; |
| 411 | |
| 412 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), |
| 413 | addrbuf, sizeof (addrbuf)); |
| 414 | zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name); |
| 415 | |
| 416 | return 0; |
| 417 | |
| 418 | } |
| 419 | |
| 420 | #endif /* HAVE_IPV6 */ |