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"
36 extern struct zebra_privs_t zserv_privs
;
38 /* clear and set interface name string */
40 lifreq_set_name (struct lifreq
*lifreq
, const char *ifname
)
42 strncpy (lifreq
->lifr_name
, ifname
, IFNAMSIZ
);
45 /* call ioctl system call */
47 if_ioctl (u_long request
, caddr_t buffer
)
53 if (zserv_privs
.change(ZPRIVS_RAISE
))
54 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
56 sock
= socket (AF_INET
, SOCK_DGRAM
, 0);
59 int save_errno
= errno
;
60 if (zserv_privs
.change(ZPRIVS_LOWER
))
61 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
62 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno
));
66 if ((ret
= ioctl (sock
, request
, buffer
)) < 0)
69 if (zserv_privs
.change(ZPRIVS_LOWER
))
70 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
84 if_ioctl_ipv6 (u_long request
, caddr_t buffer
)
91 if (zserv_privs
.change(ZPRIVS_RAISE
))
92 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
94 sock
= socket (AF_INET6
, SOCK_DGRAM
, 0);
97 int save_errno
= errno
;
98 if (zserv_privs
.change(ZPRIVS_LOWER
))
99 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
100 zlog_err("Cannot create IPv6 datagram socket: %s",
101 safe_strerror(save_errno
));
105 if ((ret
= ioctl (sock
, request
, buffer
)) < 0)
108 if (zserv_privs
.change(ZPRIVS_LOWER
))
109 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
118 #endif /* HAVE_IPV6 */
124 * get interface metric
125 * -- if value is not avaliable set -1
128 if_get_metric (struct interface
*ifp
)
130 struct lifreq lifreq
;
133 lifreq_set_name (&lifreq
, ifp
->name
);
135 if (ifp
->flags
& IFF_IPV4
)
136 ret
= AF_IOCTL (AF_INET
, SIOCGLIFMETRIC
, (caddr_t
) & lifreq
);
138 else if (ifp
->flags
& IFF_IPV6
)
139 ret
= AF_IOCTL (AF_INET6
, SIOCGLIFMETRIC
, (caddr_t
) & lifreq
);
140 #endif /* SOLARIS_IPV6 */
147 ifp
->metric
= lifreq
.lifr_metric
;
149 if (ifp
->metric
== 0)
153 /* get interface MTU */
155 if_get_mtu (struct interface
*ifp
)
157 struct lifreq lifreq
;
161 if (ifp
->flags
& IFF_IPV4
)
163 lifreq_set_name (&lifreq
, ifp
->name
);
164 ret
= AF_IOCTL (AF_INET
, SIOCGLIFMTU
, (caddr_t
) & lifreq
);
167 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
173 ifp
->mtu
= lifreq
.lifr_metric
;
179 if (ifp
->flags
& IFF_IPV6
)
181 memset(&lifreq
, 0, sizeof(lifreq
));
182 lifreq_set_name (&lifreq
, ifp
->name
);
184 ret
= AF_IOCTL (AF_INET6
, SIOCGLIFMTU
, (caddr_t
) & lifreq
);
187 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp
->name
);
192 ifp
->mtu6
= lifreq
.lifr_metric
;
196 #endif /* HAVE_IPV6 */
199 zebra_interface_up_update(ifp
);
202 /* Set up interface's address, netmask (and broadcast? ).
203 Solaris uses ifname:number semantics to set IP address aliases. */
205 if_set_prefix (struct interface
*ifp
, struct connected
*ifc
)
209 struct sockaddr_in addr
;
210 struct sockaddr_in broad
;
211 struct sockaddr_in mask
;
212 struct prefix_ipv4 ifaddr
;
213 struct prefix_ipv4
*p
;
215 p
= (struct prefix_ipv4
*) ifc
->address
;
219 strncpy (ifreq
.ifr_name
, ifp
->name
, IFNAMSIZ
);
221 addr
.sin_addr
= p
->prefix
;
222 addr
.sin_family
= p
->family
;
223 memcpy (&ifreq
.ifr_addr
, &addr
, sizeof (struct sockaddr_in
));
225 ret
= if_ioctl (SIOCSIFADDR
, (caddr_t
) & ifreq
);
230 /* We need mask for make broadcast addr. */
231 masklen2ip (p
->prefixlen
, &mask
.sin_addr
);
233 if (if_is_broadcast (ifp
))
235 apply_mask_ipv4 (&ifaddr
);
236 addr
.sin_addr
= ifaddr
.prefix
;
238 broad
.sin_addr
.s_addr
= (addr
.sin_addr
.s_addr
| ~mask
.sin_addr
.s_addr
);
239 broad
.sin_family
= p
->family
;
241 memcpy (&ifreq
.ifr_broadaddr
, &broad
, sizeof (struct sockaddr_in
));
242 ret
= if_ioctl (SIOCSIFBRDADDR
, (caddr_t
) & ifreq
);
247 mask
.sin_family
= p
->family
;
249 memcpy (&mask
, &ifreq
.ifr_addr
, sizeof (mask
));
251 memcpy (&ifreq
.ifr_netmask
, &mask
, sizeof (struct sockaddr_in
));
253 ret
= if_ioctl (SIOCSIFNETMASK
, (caddr_t
) & ifreq
);
255 return ((ret
< 0) ? ret
: 0);
258 /* Set up interface's address, netmask (and broadcast).
259 Solaris uses ifname:number semantics to set IP address aliases. */
261 if_unset_prefix (struct interface
*ifp
, struct connected
*ifc
)
265 struct sockaddr_in addr
;
266 struct prefix_ipv4
*p
;
268 p
= (struct prefix_ipv4
*) ifc
->address
;
270 strncpy (ifreq
.ifr_name
, ifp
->name
, IFNAMSIZ
);
272 memset (&addr
, 0, sizeof (struct sockaddr_in
));
273 addr
.sin_family
= p
->family
;
274 memcpy (&ifreq
.ifr_addr
, &addr
, sizeof (struct sockaddr_in
));
276 ret
= if_ioctl (SIOCSIFADDR
, (caddr_t
) & ifreq
);
284 /* Get just the flags for the given name.
285 * Used by the normal 'if_get_flags' function, as well
286 * as the bootup interface-list code, which has to peek at per-address
287 * flags in order to figure out which ones should be ignored..
290 if_get_flags_direct (const char *ifname
, uint64_t *flags
, unsigned int af
)
292 struct lifreq lifreq
;
295 lifreq_set_name (&lifreq
, ifname
);
297 ret
= AF_IOCTL (af
, SIOCGLIFFLAGS
, (caddr_t
) &lifreq
);
300 zlog_debug ("%s: ifname %s, error %s (%d)",
301 __func__
, ifname
, safe_strerror (errno
), errno
);
303 *flags
= lifreq
.lifr_flags
;
308 /* get interface flags */
310 if_get_flags (struct interface
*ifp
)
313 uint64_t newflags
= 0;
316 if (ifp
->flags
& IFF_IPV4
)
318 ret4
= if_get_flags_direct (ifp
->name
, &tmpflags
, AF_INET
);
321 newflags
|= tmpflags
;
322 else if (errno
== ENXIO
)
325 UNSET_FLAG (ifp
->flags
, IFF_UP
);
326 if_flags_update (ifp
, ifp
->flags
);
330 if (ifp
->flags
& IFF_IPV6
)
332 ret6
= if_get_flags_direct (ifp
->name
, &tmpflags
, AF_INET6
);
335 newflags
|= tmpflags
;
336 else if (errno
== ENXIO
)
339 UNSET_FLAG (ifp
->flags
, IFF_UP
);
340 if_flags_update (ifp
, ifp
->flags
);
344 /* only update flags if one of above succeeded */
345 if ( !(ret4
&& ret6
) )
346 if_flags_update (ifp
, newflags
);
349 /* Set interface flags */
351 if_set_flags (struct interface
*ifp
, uint64_t flags
)
354 struct lifreq lifreq
;
356 lifreq_set_name (&lifreq
, ifp
->name
);
358 lifreq
.lifr_flags
= ifp
->flags
;
359 lifreq
.lifr_flags
|= flags
;
361 if (ifp
->flags
& IFF_IPV4
)
362 ret
= AF_IOCTL (AF_INET
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
363 else if (ifp
->flags
& IFF_IPV6
)
364 ret
= AF_IOCTL (AF_INET6
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
369 zlog_info ("can't set interface flags on %s: %s", ifp
->name
,
370 safe_strerror (errno
));
377 /* Unset interface's flag. */
379 if_unset_flags (struct interface
*ifp
, uint64_t flags
)
382 struct lifreq lifreq
;
384 lifreq_set_name (&lifreq
, ifp
->name
);
386 lifreq
.lifr_flags
= ifp
->flags
;
387 lifreq
.lifr_flags
&= ~flags
;
389 if (ifp
->flags
& IFF_IPV4
)
390 ret
= AF_IOCTL (AF_INET
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
391 else if (ifp
->flags
& IFF_IPV6
)
392 ret
= AF_IOCTL (AF_INET6
, SIOCSLIFFLAGS
, (caddr_t
) & lifreq
);
397 zlog_info ("can't unset interface flags");
406 /* Interface's address add/delete functions. */
408 if_prefix_add_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
410 char addrbuf
[INET_ADDRSTRLEN
];
412 inet_ntop (AF_INET6
, &(((struct prefix_ipv6
*) (ifc
->address
))->prefix
),
413 addrbuf
, sizeof (addrbuf
));
414 zlog_warn ("Can't set %s on interface %s", addrbuf
, ifp
->name
);
421 if_prefix_delete_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
423 char addrbuf
[INET_ADDRSTRLEN
];
425 inet_ntop (AF_INET6
, &(((struct prefix_ipv6
*) (ifc
->address
))->prefix
),
426 addrbuf
, sizeof (addrbuf
));
427 zlog_warn ("Can't delete %s on interface %s", addrbuf
, ifp
->name
);
433 #endif /* HAVE_IPV6 */