2 * Common ioctl functions for Solaris.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
33 #include "zebra/rib.h"
35 #include "zebra/interface.h"
36 #include "zebra/ioctl_solaris.h"
38 extern struct zebra_privs_t zserv_privs;
40 /* clear and set interface name string */
42 lifreq_set_name (struct lifreq *lifreq, const char *ifname)
44 strncpy (lifreq->lifr_name, ifname, IFNAMSIZ);
47 /* call ioctl system call */
49 if_ioctl (u_long request, caddr_t buffer)
55 if (zserv_privs.change(ZPRIVS_RAISE))
56 zlog (NULL, LOG_ERR, "Can't raise privileges");
58 sock = socket (AF_INET, SOCK_DGRAM, 0);
61 int save_errno = errno;
62 if (zserv_privs.change(ZPRIVS_LOWER))
63 zlog (NULL, LOG_ERR, "Can't lower privileges");
64 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
68 if ((ret = ioctl (sock, request, buffer)) < 0)
71 if (zserv_privs.change(ZPRIVS_LOWER))
72 zlog (NULL, LOG_ERR, "Can't lower privileges");
86 if_ioctl_ipv6 (u_long request, caddr_t buffer)
93 if (zserv_privs.change(ZPRIVS_RAISE))
94 zlog (NULL, LOG_ERR, "Can't raise privileges");
96 sock = socket (AF_INET6, SOCK_DGRAM, 0);
99 int save_errno = errno;
100 if (zserv_privs.change(ZPRIVS_LOWER))
101 zlog (NULL, LOG_ERR, "Can't lower privileges");
102 zlog_err("Cannot create IPv6 datagram socket: %s",
103 safe_strerror(save_errno));
107 if ((ret = ioctl (sock, request, buffer)) < 0)
110 if (zserv_privs.change(ZPRIVS_LOWER))
111 zlog (NULL, LOG_ERR, "Can't lower privileges");
120 #endif /* HAVE_IPV6 */
126 * get interface metric
127 * -- if value is not avaliable set -1
130 if_get_metric (struct interface *ifp)
132 struct lifreq lifreq;
135 lifreq_set_name (&lifreq, ifp->name);
137 if (ifp->flags & IFF_IPV4)
138 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
140 else if (ifp->flags & IFF_IPV6)
141 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
142 #endif /* SOLARIS_IPV6 */
149 ifp->metric = lifreq.lifr_metric;
151 if (ifp->metric == 0)
155 /* get interface MTU */
157 if_get_mtu (struct interface *ifp)
159 struct lifreq lifreq;
163 if (ifp->flags & IFF_IPV4)
165 lifreq_set_name (&lifreq, ifp->name);
166 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
169 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
175 ifp->mtu = lifreq.lifr_metric;
181 if (ifp->flags & IFF_IPV6)
183 memset(&lifreq, 0, sizeof(lifreq));
184 lifreq_set_name (&lifreq, ifp->name);
186 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
189 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
194 ifp->mtu6 = lifreq.lifr_metric;
198 #endif /* HAVE_IPV6 */
201 zebra_interface_up_update(ifp);
204 /* Set up interface's address, netmask (and broadcast? ).
205 Solaris uses ifname:number semantics to set IP address aliases. */
207 if_set_prefix (struct interface *ifp, struct connected *ifc)
211 struct sockaddr_in addr;
212 struct sockaddr_in broad;
213 struct sockaddr_in mask;
214 struct prefix_ipv4 ifaddr;
215 struct prefix_ipv4 *p;
217 p = (struct prefix_ipv4 *) ifc->address;
221 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
223 addr.sin_addr = p->prefix;
224 addr.sin_family = p->family;
225 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
227 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
232 /* We need mask for make broadcast addr. */
233 masklen2ip (p->prefixlen, &mask.sin_addr);
235 if (if_is_broadcast (ifp))
237 apply_mask_ipv4 (&ifaddr);
238 addr.sin_addr = ifaddr.prefix;
240 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
241 broad.sin_family = p->family;
243 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
244 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
249 mask.sin_family = p->family;
251 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
253 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
255 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
257 return ((ret < 0) ? ret : 0);
260 /* Set up interface's address, netmask (and broadcast).
261 Solaris uses ifname:number semantics to set IP address aliases. */
263 if_unset_prefix (struct interface *ifp, struct connected *ifc)
267 struct sockaddr_in addr;
268 struct prefix_ipv4 *p;
270 p = (struct prefix_ipv4 *) ifc->address;
272 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
274 memset (&addr, 0, sizeof (struct sockaddr_in));
275 addr.sin_family = p->family;
276 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
278 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
286 /* Get just the flags for the given name.
287 * Used by the normal 'if_get_flags' function, as well
288 * as the bootup interface-list code, which has to peek at per-address
289 * flags in order to figure out which ones should be ignored..
292 if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af)
294 struct lifreq lifreq;
297 lifreq_set_name (&lifreq, ifname);
299 ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq);
302 zlog_debug ("%s: ifname %s, error %s (%d)",
303 __func__, ifname, safe_strerror (errno), errno);
305 *flags = lifreq.lifr_flags;
310 /* get interface flags */
312 if_get_flags (struct interface *ifp)
314 int ret4 = 0, ret6 = 0;
315 uint64_t newflags = 0;
318 if (ifp->flags & IFF_IPV4)
320 ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET);
323 newflags |= tmpflags;
324 else if (errno == ENXIO)
327 UNSET_FLAG (ifp->flags, IFF_UP);
328 if_flags_update (ifp, ifp->flags);
332 if (ifp->flags & IFF_IPV6)
334 ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6);
337 newflags |= tmpflags;
338 else if (errno == ENXIO)
341 UNSET_FLAG (ifp->flags, IFF_UP);
342 if_flags_update (ifp, ifp->flags);
346 /* only update flags if one of above succeeded */
347 if ( !(ret4 && ret6) )
348 if_flags_update (ifp, newflags);
351 /* Set interface flags */
353 if_set_flags (struct interface *ifp, uint64_t flags)
356 struct lifreq lifreq;
358 lifreq_set_name (&lifreq, ifp->name);
360 lifreq.lifr_flags = ifp->flags;
361 lifreq.lifr_flags |= flags;
363 if (ifp->flags & IFF_IPV4)
364 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
365 else if (ifp->flags & IFF_IPV6)
366 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
371 zlog_info ("can't set interface flags on %s: %s", ifp->name,
372 safe_strerror (errno));
379 /* Unset interface's flag. */
381 if_unset_flags (struct interface *ifp, uint64_t flags)
384 struct lifreq lifreq;
386 lifreq_set_name (&lifreq, ifp->name);
388 lifreq.lifr_flags = ifp->flags;
389 lifreq.lifr_flags &= ~flags;
391 if (ifp->flags & IFF_IPV4)
392 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
393 else if (ifp->flags & IFF_IPV6)
394 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
399 zlog_info ("can't unset interface flags");
408 /* Interface's address add/delete functions. */
410 if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
412 char addrbuf[PREFIX_STRLEN];
414 zlog_warn ("Can't set %s on interface %s",
415 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
423 if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
425 char addrbuf[PREFIX_STRLEN];
427 zlog_warn ("Can't delete %s on interface %s",
428 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
435 #endif /* HAVE_IPV6 */