blob: b5bf1ccb0af4c7bab02f594d577ace77b10ef0d2 [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"
Donald Sharp4f847372015-12-22 15:24:25 -050031#include "vty.h"
paul88424682004-05-09 18:21:35 +000032
33#include "zebra/rib.h"
34#include "zebra/rt.h"
paul5c78b3d2006-01-25 04:31:40 +000035#include "zebra/interface.h"
David Lamparter285ed0c2015-09-15 02:46:53 -070036#include "zebra/ioctl_solaris.h"
paul88424682004-05-09 18:21:35 +000037
paul48a46fa2004-05-11 10:55:22 +000038extern struct zebra_privs_t zserv_privs;
paul88424682004-05-09 18:21:35 +000039
40/* clear and set interface name string */
41void
paul5c78b3d2006-01-25 04:31:40 +000042lifreq_set_name (struct lifreq *lifreq, const char *ifname)
paul88424682004-05-09 18:21:35 +000043{
paul5c78b3d2006-01-25 04:31:40 +000044 strncpy (lifreq->lifr_name, ifname, IFNAMSIZ);
paul88424682004-05-09 18:21:35 +000045}
46
47/* call ioctl system call */
48int
49if_ioctl (u_long request, caddr_t buffer)
50{
51 int sock;
ajs4460e7a2005-01-29 17:07:40 +000052 int ret;
53 int err;
paul88424682004-05-09 18:21:35 +000054
55 if (zserv_privs.change(ZPRIVS_RAISE))
56 zlog (NULL, LOG_ERR, "Can't raise privileges");
57
58 sock = socket (AF_INET, SOCK_DGRAM, 0);
59 if (sock < 0)
60 {
ajs6a52d0d2005-01-30 18:49:28 +000061 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000062 if (zserv_privs.change(ZPRIVS_LOWER))
63 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000064 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +000065 exit (1);
66 }
67
ajs4460e7a2005-01-29 17:07:40 +000068 if ((ret = ioctl (sock, request, buffer)) < 0)
69 err = errno;
paul88424682004-05-09 18:21:35 +000070
71 if (zserv_privs.change(ZPRIVS_LOWER))
72 zlog (NULL, LOG_ERR, "Can't lower privileges");
73
paul88424682004-05-09 18:21:35 +000074 close (sock);
75
76 if (ret < 0)
77 {
78 errno = err;
79 return ret;
80 }
81 return 0;
82}
83
paul5b73a672004-07-23 15:26:14 +000084
paul88424682004-05-09 18:21:35 +000085int
86if_ioctl_ipv6 (u_long request, caddr_t buffer)
87{
paul5b73a672004-07-23 15:26:14 +000088#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +000089 int sock;
ajs4460e7a2005-01-29 17:07:40 +000090 int ret;
91 int err;
paul88424682004-05-09 18:21:35 +000092
93 if (zserv_privs.change(ZPRIVS_RAISE))
94 zlog (NULL, LOG_ERR, "Can't raise privileges");
95
96 sock = socket (AF_INET6, SOCK_DGRAM, 0);
97 if (sock < 0)
98 {
ajs6a52d0d2005-01-30 18:49:28 +000099 int save_errno = errno;
paul88424682004-05-09 18:21:35 +0000100 if (zserv_privs.change(ZPRIVS_LOWER))
101 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +0000102 zlog_err("Cannot create IPv6 datagram socket: %s",
103 safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +0000104 exit (1);
105 }
106
ajs4460e7a2005-01-29 17:07:40 +0000107 if ((ret = ioctl (sock, request, buffer)) < 0)
108 err = errno;
paul88424682004-05-09 18:21:35 +0000109
110 if (zserv_privs.change(ZPRIVS_LOWER))
111 zlog (NULL, LOG_ERR, "Can't lower privileges");
112
paul88424682004-05-09 18:21:35 +0000113 close (sock);
114
115 if (ret < 0)
116 {
117 errno = err;
118 return ret;
119 }
paul5b73a672004-07-23 15:26:14 +0000120#endif /* HAVE_IPV6 */
121
paul88424682004-05-09 18:21:35 +0000122 return 0;
123}
paul88424682004-05-09 18:21:35 +0000124
125/*
126 * get interface metric
127 * -- if value is not avaliable set -1
128 */
129void
130if_get_metric (struct interface *ifp)
131{
132 struct lifreq lifreq;
133 int ret;
134
paul5c78b3d2006-01-25 04:31:40 +0000135 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000136
paul88424682004-05-09 18:21:35 +0000137 if (ifp->flags & IFF_IPV4)
138 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000139#ifdef SOLARIS_IPV6
paul88424682004-05-09 18:21:35 +0000140 else if (ifp->flags & IFF_IPV6)
141 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000142#endif /* SOLARIS_IPV6 */
paul88424682004-05-09 18:21:35 +0000143 else
144 ret = -1;
paul88424682004-05-09 18:21:35 +0000145
146 if (ret < 0)
147 return;
148
149 ifp->metric = lifreq.lifr_metric;
150
151 if (ifp->metric == 0)
152 ifp->metric = 1;
153}
154
155/* get interface MTU */
156void
157if_get_mtu (struct interface *ifp)
158{
159 struct lifreq lifreq;
160 int ret;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000161 u_char changed = 0;
paul88424682004-05-09 18:21:35 +0000162
paul88424682004-05-09 18:21:35 +0000163 if (ifp->flags & IFF_IPV4)
164 {
paul5c78b3d2006-01-25 04:31:40 +0000165 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000166 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
167 if (ret < 0)
168 {
169 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
170 ifp->name);
171 ifp->mtu = -1;
172 }
173 else
174 {
175 ifp->mtu = lifreq.lifr_metric;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000176 changed = 1;
paul88424682004-05-09 18:21:35 +0000177 }
178 }
179
paul5b73a672004-07-23 15:26:14 +0000180#ifdef HAVE_IPV6
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000181 if (ifp->flags & IFF_IPV6)
182 {
183 memset(&lifreq, 0, sizeof(lifreq));
184 lifreq_set_name (&lifreq, ifp->name);
paul5b73a672004-07-23 15:26:14 +0000185
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000186 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
187 if (ret < 0)
paul88424682004-05-09 18:21:35 +0000188 {
189 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
190 ifp->mtu6 = -1;
191 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000192 else
paul88424682004-05-09 18:21:35 +0000193 {
194 ifp->mtu6 = lifreq.lifr_metric;
Paul Jakmabd9da7f2007-04-29 16:50:46 +0000195 changed = 1;
paul88424682004-05-09 18:21:35 +0000196 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000197 }
198#endif /* HAVE_IPV6 */
199
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000200 if (changed)
201 zebra_interface_up_update(ifp);
paul88424682004-05-09 18:21:35 +0000202}
203
204/* Set up interface's address, netmask (and broadcast? ).
205 Solaris uses ifname:number semantics to set IP address aliases. */
206int
207if_set_prefix (struct interface *ifp, struct connected *ifc)
208{
209 int ret;
210 struct ifreq ifreq;
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;
216
217 p = (struct prefix_ipv4 *) ifc->address;
218
219 ifaddr = *p;
220
221 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
222
223 addr.sin_addr = p->prefix;
224 addr.sin_family = p->family;
225 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
226
227 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
228
229 if (ret < 0)
230 return ret;
231
232 /* We need mask for make broadcast addr. */
233 masklen2ip (p->prefixlen, &mask.sin_addr);
234
235 if (if_is_broadcast (ifp))
236 {
237 apply_mask_ipv4 (&ifaddr);
238 addr.sin_addr = ifaddr.prefix;
239
240 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
241 broad.sin_family = p->family;
242
243 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
244 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
245 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000246 return ret;
paul88424682004-05-09 18:21:35 +0000247 }
248
249 mask.sin_family = p->family;
250#ifdef SUNOS_5
251 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
252#else
253 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000254#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000255 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
256
257 return ((ret < 0) ? ret : 0);
258}
259
260/* Set up interface's address, netmask (and broadcast).
261 Solaris uses ifname:number semantics to set IP address aliases. */
262int
263if_unset_prefix (struct interface *ifp, struct connected *ifc)
264{
265 int ret;
266 struct ifreq ifreq;
267 struct sockaddr_in addr;
268 struct prefix_ipv4 *p;
269
270 p = (struct prefix_ipv4 *) ifc->address;
271
272 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
273
274 memset (&addr, 0, sizeof (struct sockaddr_in));
275 addr.sin_family = p->family;
276 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
277
278 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
279
280 if (ret < 0)
281 return ret;
282
283 return 0;
284}
285
paul5c78b3d2006-01-25 04:31:40 +0000286/* 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..
paul0752ef02005-11-03 12:35:21 +0000290 */
paul5c78b3d2006-01-25 04:31:40 +0000291int
292if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af)
paul0752ef02005-11-03 12:35:21 +0000293{
paul5c78b3d2006-01-25 04:31:40 +0000294 struct lifreq lifreq;
295 int ret;
296
297 lifreq_set_name (&lifreq, ifname);
298
299 ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq);
300
301 if (ret)
302 zlog_debug ("%s: ifname %s, error %s (%d)",
303 __func__, ifname, safe_strerror (errno), errno);
304
305 *flags = lifreq.lifr_flags;
306
307 return ret;
paul0752ef02005-11-03 12:35:21 +0000308}
309
paul88424682004-05-09 18:21:35 +0000310/* get interface flags */
311void
312if_get_flags (struct interface *ifp)
313{
David Lamparter6d936222015-09-15 02:59:04 -0700314 int ret4 = 0, ret6 = 0;
paul5c78b3d2006-01-25 04:31:40 +0000315 uint64_t newflags = 0;
316 uint64_t tmpflags;
paul88424682004-05-09 18:21:35 +0000317
318 if (ifp->flags & IFF_IPV4)
319 {
paul5c78b3d2006-01-25 04:31:40 +0000320 ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET);
paul0752ef02005-11-03 12:35:21 +0000321
322 if (!ret4)
paul5c78b3d2006-01-25 04:31:40 +0000323 newflags |= tmpflags;
324 else if (errno == ENXIO)
325 {
326 /* it's gone */
327 UNSET_FLAG (ifp->flags, IFF_UP);
328 if_flags_update (ifp, ifp->flags);
329 }
paul88424682004-05-09 18:21:35 +0000330 }
331
332 if (ifp->flags & IFF_IPV6)
333 {
paul5c78b3d2006-01-25 04:31:40 +0000334 ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6);
paul0752ef02005-11-03 12:35:21 +0000335
336 if (!ret6)
paul5c78b3d2006-01-25 04:31:40 +0000337 newflags |= tmpflags;
338 else if (errno == ENXIO)
339 {
340 /* it's gone */
341 UNSET_FLAG (ifp->flags, IFF_UP);
342 if_flags_update (ifp, ifp->flags);
343 }
paul88424682004-05-09 18:21:35 +0000344 }
paul0752ef02005-11-03 12:35:21 +0000345
346 /* only update flags if one of above succeeded */
347 if ( !(ret4 && ret6) )
paul5c78b3d2006-01-25 04:31:40 +0000348 if_flags_update (ifp, newflags);
paul88424682004-05-09 18:21:35 +0000349}
350
351/* Set interface flags */
352int
paul5c78b3d2006-01-25 04:31:40 +0000353if_set_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000354{
355 int ret;
356 struct lifreq lifreq;
357
paul5c78b3d2006-01-25 04:31:40 +0000358 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000359
360 lifreq.lifr_flags = ifp->flags;
361 lifreq.lifr_flags |= flags;
362
paul88424682004-05-09 18:21:35 +0000363 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);
367 else
368 ret = -1;
369
370 if (ret < 0)
371 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000372 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000373 else
374 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000375
376 return ret;
paul88424682004-05-09 18:21:35 +0000377}
378
379/* Unset interface's flag. */
380int
paul5c78b3d2006-01-25 04:31:40 +0000381if_unset_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000382{
383 int ret;
384 struct lifreq lifreq;
385
paul5c78b3d2006-01-25 04:31:40 +0000386 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000387
388 lifreq.lifr_flags = ifp->flags;
389 lifreq.lifr_flags &= ~flags;
390
paul88424682004-05-09 18:21:35 +0000391 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);
395 else
396 ret = -1;
397
398 if (ret < 0)
399 zlog_info ("can't unset interface flags");
400 else
401 ret = 0;
402
paul48a46fa2004-05-11 10:55:22 +0000403 return ret;
paul88424682004-05-09 18:21:35 +0000404}
405
406#ifdef HAVE_IPV6
407
408/* Interface's address add/delete functions. */
409int
410if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
411{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300412 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000413
Timo Teräsbe6335d2015-05-23 11:08:41 +0300414 zlog_warn ("Can't set %s on interface %s",
Martin Winterfe561252015-09-15 19:12:31 -0700415 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300416 ifp->name);
paul88424682004-05-09 18:21:35 +0000417
418 return 0;
419
420}
421
422int
423if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
424{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300425 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000426
Timo Teräsbe6335d2015-05-23 11:08:41 +0300427 zlog_warn ("Can't delete %s on interface %s",
Martin Winterfe561252015-09-15 19:12:31 -0700428 prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
Timo Teräsbe6335d2015-05-23 11:08:41 +0300429 ifp->name);
paul88424682004-05-09 18:21:35 +0000430
431 return 0;
432
433}
434
435#endif /* HAVE_IPV6 */