blob: 12737cbf061f8e498b0c7aa2426d443d5d4a72fe [file] [log] [blame]
paul88424682004-05-09 18:21:35 +00001/*
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"
paul48a46fa2004-05-11 10:55:22 +000030#include "privs.h"
paul88424682004-05-09 18:21:35 +000031
32#include "zebra/rib.h"
33#include "zebra/rt.h"
paul5c78b3d2006-01-25 04:31:40 +000034#include "zebra/interface.h"
David Lamparter285ed0c2015-09-15 02:46:53 -070035#include "zebra/ioctl_solaris.h"
paul88424682004-05-09 18:21:35 +000036
paul48a46fa2004-05-11 10:55:22 +000037extern struct zebra_privs_t zserv_privs;
paul88424682004-05-09 18:21:35 +000038
39/* clear and set interface name string */
40void
paul5c78b3d2006-01-25 04:31:40 +000041lifreq_set_name (struct lifreq *lifreq, const char *ifname)
paul88424682004-05-09 18:21:35 +000042{
paul5c78b3d2006-01-25 04:31:40 +000043 strncpy (lifreq->lifr_name, ifname, IFNAMSIZ);
paul88424682004-05-09 18:21:35 +000044}
45
46/* call ioctl system call */
47int
48if_ioctl (u_long request, caddr_t buffer)
49{
50 int sock;
ajs4460e7a2005-01-29 17:07:40 +000051 int ret;
52 int err;
paul88424682004-05-09 18:21:35 +000053
54 if (zserv_privs.change(ZPRIVS_RAISE))
55 zlog (NULL, LOG_ERR, "Can't raise privileges");
56
57 sock = socket (AF_INET, SOCK_DGRAM, 0);
58 if (sock < 0)
59 {
ajs6a52d0d2005-01-30 18:49:28 +000060 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000061 if (zserv_privs.change(ZPRIVS_LOWER))
62 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000063 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +000064 exit (1);
65 }
66
ajs4460e7a2005-01-29 17:07:40 +000067 if ((ret = ioctl (sock, request, buffer)) < 0)
68 err = errno;
paul88424682004-05-09 18:21:35 +000069
70 if (zserv_privs.change(ZPRIVS_LOWER))
71 zlog (NULL, LOG_ERR, "Can't lower privileges");
72
paul88424682004-05-09 18:21:35 +000073 close (sock);
74
75 if (ret < 0)
76 {
77 errno = err;
78 return ret;
79 }
80 return 0;
81}
82
paul5b73a672004-07-23 15:26:14 +000083
paul88424682004-05-09 18:21:35 +000084int
85if_ioctl_ipv6 (u_long request, caddr_t buffer)
86{
paul5b73a672004-07-23 15:26:14 +000087#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +000088 int sock;
ajs4460e7a2005-01-29 17:07:40 +000089 int ret;
90 int err;
paul88424682004-05-09 18:21:35 +000091
92 if (zserv_privs.change(ZPRIVS_RAISE))
93 zlog (NULL, LOG_ERR, "Can't raise privileges");
94
95 sock = socket (AF_INET6, SOCK_DGRAM, 0);
96 if (sock < 0)
97 {
ajs6a52d0d2005-01-30 18:49:28 +000098 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000099 if (zserv_privs.change(ZPRIVS_LOWER))
100 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +0000101 zlog_err("Cannot create IPv6 datagram socket: %s",
102 safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +0000103 exit (1);
104 }
105
ajs4460e7a2005-01-29 17:07:40 +0000106 if ((ret = ioctl (sock, request, buffer)) < 0)
107 err = errno;
paul88424682004-05-09 18:21:35 +0000108
109 if (zserv_privs.change(ZPRIVS_LOWER))
110 zlog (NULL, LOG_ERR, "Can't lower privileges");
111
paul88424682004-05-09 18:21:35 +0000112 close (sock);
113
114 if (ret < 0)
115 {
116 errno = err;
117 return ret;
118 }
paul5b73a672004-07-23 15:26:14 +0000119#endif /* HAVE_IPV6 */
120
paul88424682004-05-09 18:21:35 +0000121 return 0;
122}
paul88424682004-05-09 18:21:35 +0000123
124/*
125 * get interface metric
126 * -- if value is not avaliable set -1
127 */
128void
129if_get_metric (struct interface *ifp)
130{
131 struct lifreq lifreq;
132 int ret;
133
paul5c78b3d2006-01-25 04:31:40 +0000134 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000135
paul88424682004-05-09 18:21:35 +0000136 if (ifp->flags & IFF_IPV4)
137 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000138#ifdef SOLARIS_IPV6
paul88424682004-05-09 18:21:35 +0000139 else if (ifp->flags & IFF_IPV6)
140 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000141#endif /* SOLARIS_IPV6 */
paul88424682004-05-09 18:21:35 +0000142 else
143 ret = -1;
paul88424682004-05-09 18:21:35 +0000144
145 if (ret < 0)
146 return;
147
148 ifp->metric = lifreq.lifr_metric;
149
150 if (ifp->metric == 0)
151 ifp->metric = 1;
152}
153
154/* get interface MTU */
155void
156if_get_mtu (struct interface *ifp)
157{
158 struct lifreq lifreq;
159 int ret;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000160 u_char changed = 0;
paul88424682004-05-09 18:21:35 +0000161
paul88424682004-05-09 18:21:35 +0000162 if (ifp->flags & IFF_IPV4)
163 {
paul5c78b3d2006-01-25 04:31:40 +0000164 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000165 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
166 if (ret < 0)
167 {
168 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
169 ifp->name);
170 ifp->mtu = -1;
171 }
172 else
173 {
174 ifp->mtu = lifreq.lifr_metric;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000175 changed = 1;
paul88424682004-05-09 18:21:35 +0000176 }
177 }
178
paul5b73a672004-07-23 15:26:14 +0000179#ifdef HAVE_IPV6
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000180 if (ifp->flags & IFF_IPV6)
181 {
182 memset(&lifreq, 0, sizeof(lifreq));
183 lifreq_set_name (&lifreq, ifp->name);
paul5b73a672004-07-23 15:26:14 +0000184
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000185 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
186 if (ret < 0)
paul88424682004-05-09 18:21:35 +0000187 {
188 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
189 ifp->mtu6 = -1;
190 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000191 else
paul88424682004-05-09 18:21:35 +0000192 {
193 ifp->mtu6 = lifreq.lifr_metric;
Paul Jakmabd9da7f2007-04-29 16:50:46 +0000194 changed = 1;
paul88424682004-05-09 18:21:35 +0000195 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000196 }
197#endif /* HAVE_IPV6 */
198
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000199 if (changed)
200 zebra_interface_up_update(ifp);
paul88424682004-05-09 18:21:35 +0000201}
202
203/* Set up interface's address, netmask (and broadcast? ).
204 Solaris uses ifname:number semantics to set IP address aliases. */
205int
206if_set_prefix (struct interface *ifp, struct connected *ifc)
207{
208 int ret;
209 struct ifreq ifreq;
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;
215
216 p = (struct prefix_ipv4 *) ifc->address;
217
218 ifaddr = *p;
219
220 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
221
222 addr.sin_addr = p->prefix;
223 addr.sin_family = p->family;
224 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
225
226 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
227
228 if (ret < 0)
229 return ret;
230
231 /* We need mask for make broadcast addr. */
232 masklen2ip (p->prefixlen, &mask.sin_addr);
233
234 if (if_is_broadcast (ifp))
235 {
236 apply_mask_ipv4 (&ifaddr);
237 addr.sin_addr = ifaddr.prefix;
238
239 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
240 broad.sin_family = p->family;
241
242 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
243 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
244 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000245 return ret;
paul88424682004-05-09 18:21:35 +0000246 }
247
248 mask.sin_family = p->family;
249#ifdef SUNOS_5
250 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
251#else
252 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000253#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000254 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
255
256 return ((ret < 0) ? ret : 0);
257}
258
259/* Set up interface's address, netmask (and broadcast).
260 Solaris uses ifname:number semantics to set IP address aliases. */
261int
262if_unset_prefix (struct interface *ifp, struct connected *ifc)
263{
264 int ret;
265 struct ifreq ifreq;
266 struct sockaddr_in addr;
267 struct prefix_ipv4 *p;
268
269 p = (struct prefix_ipv4 *) ifc->address;
270
271 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
272
273 memset (&addr, 0, sizeof (struct sockaddr_in));
274 addr.sin_family = p->family;
275 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
276
277 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
278
279 if (ret < 0)
280 return ret;
281
282 return 0;
283}
284
paul5c78b3d2006-01-25 04:31:40 +0000285/* 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..
paul0752ef02005-11-03 12:35:21 +0000289 */
paul5c78b3d2006-01-25 04:31:40 +0000290int
291if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af)
paul0752ef02005-11-03 12:35:21 +0000292{
paul5c78b3d2006-01-25 04:31:40 +0000293 struct lifreq lifreq;
294 int ret;
295
296 lifreq_set_name (&lifreq, ifname);
297
298 ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq);
299
300 if (ret)
301 zlog_debug ("%s: ifname %s, error %s (%d)",
302 __func__, ifname, safe_strerror (errno), errno);
303
304 *flags = lifreq.lifr_flags;
305
306 return ret;
paul0752ef02005-11-03 12:35:21 +0000307}
308
paul88424682004-05-09 18:21:35 +0000309/* get interface flags */
310void
311if_get_flags (struct interface *ifp)
312{
David Lamparter6d936222015-09-15 02:59:04 -0700313 int ret4 = 0, ret6 = 0;
paul5c78b3d2006-01-25 04:31:40 +0000314 uint64_t newflags = 0;
315 uint64_t tmpflags;
paul88424682004-05-09 18:21:35 +0000316
317 if (ifp->flags & IFF_IPV4)
318 {
paul5c78b3d2006-01-25 04:31:40 +0000319 ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET);
paul0752ef02005-11-03 12:35:21 +0000320
321 if (!ret4)
paul5c78b3d2006-01-25 04:31:40 +0000322 newflags |= tmpflags;
323 else if (errno == ENXIO)
324 {
325 /* it's gone */
326 UNSET_FLAG (ifp->flags, IFF_UP);
327 if_flags_update (ifp, ifp->flags);
328 }
paul88424682004-05-09 18:21:35 +0000329 }
330
331 if (ifp->flags & IFF_IPV6)
332 {
paul5c78b3d2006-01-25 04:31:40 +0000333 ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6);
paul0752ef02005-11-03 12:35:21 +0000334
335 if (!ret6)
paul5c78b3d2006-01-25 04:31:40 +0000336 newflags |= tmpflags;
337 else if (errno == ENXIO)
338 {
339 /* it's gone */
340 UNSET_FLAG (ifp->flags, IFF_UP);
341 if_flags_update (ifp, ifp->flags);
342 }
paul88424682004-05-09 18:21:35 +0000343 }
paul0752ef02005-11-03 12:35:21 +0000344
345 /* only update flags if one of above succeeded */
346 if ( !(ret4 && ret6) )
paul5c78b3d2006-01-25 04:31:40 +0000347 if_flags_update (ifp, newflags);
paul88424682004-05-09 18:21:35 +0000348}
349
350/* Set interface flags */
351int
paul5c78b3d2006-01-25 04:31:40 +0000352if_set_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000353{
354 int ret;
355 struct lifreq lifreq;
356
paul5c78b3d2006-01-25 04:31:40 +0000357 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000358
359 lifreq.lifr_flags = ifp->flags;
360 lifreq.lifr_flags |= flags;
361
paul88424682004-05-09 18:21:35 +0000362 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);
366 else
367 ret = -1;
368
369 if (ret < 0)
370 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000371 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000372 else
373 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000374
375 return ret;
paul88424682004-05-09 18:21:35 +0000376}
377
378/* Unset interface's flag. */
379int
paul5c78b3d2006-01-25 04:31:40 +0000380if_unset_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000381{
382 int ret;
383 struct lifreq lifreq;
384
paul5c78b3d2006-01-25 04:31:40 +0000385 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000386
387 lifreq.lifr_flags = ifp->flags;
388 lifreq.lifr_flags &= ~flags;
389
paul88424682004-05-09 18:21:35 +0000390 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);
394 else
395 ret = -1;
396
397 if (ret < 0)
398 zlog_info ("can't unset interface flags");
399 else
400 ret = 0;
401
paul48a46fa2004-05-11 10:55:22 +0000402 return ret;
paul88424682004-05-09 18:21:35 +0000403}
404
405#ifdef HAVE_IPV6
406
407/* Interface's address add/delete functions. */
408int
409if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
410{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300411 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000412
Timo Teräsbe6335d2015-05-23 11:08:41 +0300413 zlog_warn ("Can't set %s on interface %s",
Martin Winterfe561252015-09-15 19:12:31 -0700414 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300415 ifp->name);
paul88424682004-05-09 18:21:35 +0000416
417 return 0;
418
419}
420
421int
422if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
423{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300424 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000425
Timo Teräsbe6335d2015-05-23 11:08:41 +0300426 zlog_warn ("Can't delete %s on interface %s",
Martin Winterfe561252015-09-15 19:12:31 -0700427 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300428 ifp->name);
paul88424682004-05-09 18:21:35 +0000429
430 return 0;
431
432}
433
434#endif /* HAVE_IPV6 */