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
32 #include "zebra/rib.h"
34 #include "zebra/interface.h"
35 #include "zebra/ioctl_solaris.h"
37 extern struct zebra_privs_t zserv_privs
;
39 /* clear and set interface name string */
41 lifreq_set_name (struct lifreq
*lifreq
, const char *ifname
)
43 strncpy (lifreq
->lifr_name
, ifname
, IFNAMSIZ
);
46 /* call ioctl system call */
48 if_ioctl (u_long request
, caddr_t buffer
)
54 if (zserv_privs
.change(ZPRIVS_RAISE
))
55 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
57 sock
= socket (AF_INET
, SOCK_DGRAM
, 0);
60 int save_errno
= errno
;
61 if (zserv_privs
.change(ZPRIVS_LOWER
))
62 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
63 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno
));
67 if ((ret
= ioctl (sock
, request
, buffer
)) < 0)
70 if (zserv_privs
.change(ZPRIVS_LOWER
))
71 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
85 if_ioctl_ipv6 (u_long request
, caddr_t buffer
)
92 if (zserv_privs
.change(ZPRIVS_RAISE
))
93 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
95 sock
= socket (AF_INET6
, SOCK_DGRAM
, 0);
98 int save_errno
= errno
;
99 if (zserv_privs
.change(ZPRIVS_LOWER
))
100 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
101 zlog_err("Cannot create IPv6 datagram socket: %s",
102 safe_strerror(save_errno
));
106 if ((ret
= ioctl (sock
, request
, buffer
)) < 0)
109 if (zserv_privs
.change(ZPRIVS_LOWER
))
110 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
119 #endif /* HAVE_IPV6 */
125 * get interface metric
126 * -- if value is not avaliable set -1
129 if_get_metric (struct interface
*ifp
)
131 struct lifreq lifreq
;
134 lifreq_set_name (&lifreq
, ifp
->name
);
136 if (ifp
->flags
& IFF_IPV4
)
137 ret
= AF_IOCTL (AF_INET
, SIOCGLIFMETRIC
, (caddr_t
) & lifreq
);
139 else if (ifp
->flags
& IFF_IPV6
)
140 ret
= AF_IOCTL (AF_INET6
, SIOCGLIFMETRIC
, (caddr_t
) & lifreq
);
141 #endif /* SOLARIS_IPV6 */
148 ifp
->metric
= lifreq
.lifr_metric
;
150 if (ifp
->metric
== 0)
154 /* get interface MTU */
156 if_get_mtu (struct interface
*ifp
)
158 struct lifreq lifreq
;
162 if (ifp
->flags
& IFF_IPV4
)
164 lifreq_set_name (&lifreq
, ifp
->name
);
165 ret
= AF_IOCTL (AF_INET
, SIOCGLIFMTU
, (caddr_t
) & lifreq
);
168 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
174 ifp
->mtu
= lifreq
.lifr_metric
;
180 if (ifp
->flags
& IFF_IPV6
)
182 memset(&lifreq
, 0, sizeof(lifreq
));
183 lifreq_set_name (&lifreq
, ifp
->name
);
185 ret
= AF_IOCTL (AF_INET6
, SIOCGLIFMTU
, (caddr_t
) & lifreq
);
188 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp
->name
);
193 ifp
->mtu6
= lifreq
.lifr_metric
;
197 #endif /* HAVE_IPV6 */
200 zebra_interface_up_update(ifp
);
203 /* Set up interface's address, netmask (and broadcast? ).
204 Solaris uses ifname:number semantics to set IP address aliases. */
206 if_set_prefix (struct interface
*ifp
, struct connected
*ifc
)
210 struct sockaddr_in addr
;
211 struct sockaddr_in broad
;
212 struct sockaddr_in mask
;
213 struct prefix_ipv4 ifaddr
;
214 struct prefix_ipv4
*p
;
216 p
= (struct prefix_ipv4
*) ifc
->address
;
220 strncpy (ifreq
.ifr_name
, ifp
->name
, IFNAMSIZ
);
222 addr
.sin_addr
= p
->prefix
;
223 addr
.sin_family
= p
->family
;
224 memcpy (&ifreq
.ifr_addr
, &addr
, sizeof (struct sockaddr_in
));
226 ret
= if_ioctl (SIOCSIFADDR
, (caddr_t
) & ifreq
);
231 /* We need mask for make broadcast addr. */
232 masklen2ip (p
->prefixlen
, &mask
.sin_addr
);
234 if (if_is_broadcast (ifp
))
236 apply_mask_ipv4 (&ifaddr
);
237 addr
.sin_addr
= ifaddr
.prefix
;
239 broad
.sin_addr
.s_addr
= (addr
.sin_addr
.s_addr
| ~mask
.sin_addr
.s_addr
);
240 broad
.sin_family
= p
->family
;
242 memcpy (&ifreq
.ifr_broadaddr
, &broad
, sizeof (struct sockaddr_in
));
243 ret
= if_ioctl (SIOCSIFBRDADDR
, (caddr_t
) & ifreq
);
248 mask
.sin_family
= p
->family
;
250 memcpy (&mask
, &ifreq
.ifr_addr
, sizeof (mask
));
252 memcpy (&ifreq
.ifr_netmask
, &mask
, sizeof (struct sockaddr_in
));
254 ret
= if_ioctl (SIOCSIFNETMASK
, (caddr_t
) & ifreq
);
256 return ((ret
< 0) ? ret
: 0);
259 /* Set up interface's address, netmask (and broadcast).
260 Solaris uses ifname:number semantics to set IP address aliases. */
262 if_unset_prefix (struct interface
*ifp
, struct connected
*ifc
)
266 struct sockaddr_in addr
;
267 struct prefix_ipv4
*p
;
269 p
= (struct prefix_ipv4
*) ifc
->address
;
271 strncpy (ifreq
.ifr_name
, ifp
->name
, IFNAMSIZ
);
273 memset (&addr
, 0, sizeof (struct sockaddr_in
));
274 addr
.sin_family
= p
->family
;
275 memcpy (&ifreq
.ifr_addr
, &addr
, sizeof (struct sockaddr_in
));
277 ret
= if_ioctl (SIOCSIFADDR
, (caddr_t
) & ifreq
);
285 /* Get just the flags for the given name.
286 * Used by the normal 'if_get_flags' function, as well
287 * as the bootup interface-list code, which has to peek at per-address
288 * flags in order to figure out which ones should be ignored..
291 if_get_flags_direct (const char *ifname
, uint64_t *flags
, unsigned int af
)
293 struct lifreq lifreq
;
296 lifreq_set_name (&lifreq
, ifname
);
298 ret
= AF_IOCTL (af
, SIOCGLIFFLAGS
, (caddr_t
) &lifreq
);
301 zlog_debug ("%s: ifname %s, error %s (%d)",
302 __func__
, ifname
, safe_strerror (errno
), errno
);
304 *flags
= lifreq
.lifr_flags
;
309 /* get interface flags */
311 if_get_flags (struct interface
*ifp
)
313 int ret4
= 0, ret6
= 0;
314 uint64_t newflags
= 0;
317 if (ifp
->flags
& IFF_IPV4
)
319 ret4
= if_get_flags_direct (ifp
->name
, &tmpflags
, AF_INET
);
322 newflags
|= tmpflags
;
323 else if (errno
== ENXIO
)
326 UNSET_FLAG (ifp
->flags
, IFF_UP
);
327 if_flags_update (ifp
, ifp
->flags
);
331 if (ifp
->flags
& IFF_IPV6
)
333 ret6
= if_get_flags_direct (ifp
->name
, &tmpflags
, AF_INET6
);
336 newflags
|= tmpflags
;
337 else if (errno
== ENXIO
)
340 UNSET_FLAG (ifp
->flags
, IFF_UP
);
341 if_flags_update (ifp
, ifp
->flags
);
345 /* only update flags if one of above succeeded */
346 if ( !(ret4
&& ret6
) )
347 if_flags_update (ifp
, newflags
);
350 /* Set interface flags */
352 if_set_flags (struct interface
*ifp
, uint64_t flags
)
355 struct lifreq lifreq
;
357 lifreq_set_name (&lifreq
, ifp
->name
);
359 lifreq
.lifr_flags
= ifp
->flags
;
360 lifreq
.lifr_flags
|= flags
;
362 if (ifp
->flags
& IFF_IPV4
)
363 ret
= AF_IOCTL (AF_INET
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
364 else if (ifp
->flags
& IFF_IPV6
)
365 ret
= AF_IOCTL (AF_INET6
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
370 zlog_info ("can't set interface flags on %s: %s", ifp
->name
,
371 safe_strerror (errno
));
378 /* Unset interface's flag. */
380 if_unset_flags (struct interface
*ifp
, uint64_t flags
)
383 struct lifreq lifreq
;
385 lifreq_set_name (&lifreq
, ifp
->name
);
387 lifreq
.lifr_flags
= ifp
->flags
;
388 lifreq
.lifr_flags
&= ~flags
;
390 if (ifp
->flags
& IFF_IPV4
)
391 ret
= AF_IOCTL (AF_INET
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
392 else if (ifp
->flags
& IFF_IPV6
)
393 ret
= AF_IOCTL (AF_INET6
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
398 zlog_info ("can't unset interface flags");
407 /* Interface's address add/delete functions. */
409 if_prefix_add_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
411 char addrbuf
[PREFIX_STRLEN
];
413 zlog_warn ("Can't set %s on interface %s",
414 prefix2str(ifc
->address
, addrbuf
, sizeof(addrbuf
)),
422 if_prefix_delete_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
424 char addrbuf
[PREFIX_STRLEN
];
426 zlog_warn ("Can't delete %s on interface %s",
427 prefix2str(ifc
->address
, addrbuf
, sizeof(addrbuf
)),
434 #endif /* HAVE_IPV6 */