blob: 16934f06337921f34fcf2fd5b5ed017450c147aa [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"
paul88424682004-05-09 18:21:35 +000035
paul48a46fa2004-05-11 10:55:22 +000036extern struct zebra_privs_t zserv_privs;
paul88424682004-05-09 18:21:35 +000037
38/* clear and set interface name string */
39void
paul5c78b3d2006-01-25 04:31:40 +000040lifreq_set_name (struct lifreq *lifreq, const char *ifname)
paul88424682004-05-09 18:21:35 +000041{
paul5c78b3d2006-01-25 04:31:40 +000042 strncpy (lifreq->lifr_name, ifname, IFNAMSIZ);
paul88424682004-05-09 18:21:35 +000043}
44
45/* call ioctl system call */
46int
47if_ioctl (u_long request, caddr_t buffer)
48{
49 int sock;
ajs4460e7a2005-01-29 17:07:40 +000050 int ret;
51 int err;
paul88424682004-05-09 18:21:35 +000052
53 if (zserv_privs.change(ZPRIVS_RAISE))
54 zlog (NULL, LOG_ERR, "Can't raise privileges");
55
56 sock = socket (AF_INET, SOCK_DGRAM, 0);
57 if (sock < 0)
58 {
ajs6a52d0d2005-01-30 18:49:28 +000059 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000060 if (zserv_privs.change(ZPRIVS_LOWER))
61 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000062 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +000063 exit (1);
64 }
65
ajs4460e7a2005-01-29 17:07:40 +000066 if ((ret = ioctl (sock, request, buffer)) < 0)
67 err = errno;
paul88424682004-05-09 18:21:35 +000068
69 if (zserv_privs.change(ZPRIVS_LOWER))
70 zlog (NULL, LOG_ERR, "Can't lower privileges");
71
paul88424682004-05-09 18:21:35 +000072 close (sock);
73
74 if (ret < 0)
75 {
76 errno = err;
77 return ret;
78 }
79 return 0;
80}
81
paul5b73a672004-07-23 15:26:14 +000082
paul88424682004-05-09 18:21:35 +000083int
84if_ioctl_ipv6 (u_long request, caddr_t buffer)
85{
paul5b73a672004-07-23 15:26:14 +000086#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +000087 int sock;
ajs4460e7a2005-01-29 17:07:40 +000088 int ret;
89 int err;
paul88424682004-05-09 18:21:35 +000090
91 if (zserv_privs.change(ZPRIVS_RAISE))
92 zlog (NULL, LOG_ERR, "Can't raise privileges");
93
94 sock = socket (AF_INET6, SOCK_DGRAM, 0);
95 if (sock < 0)
96 {
ajs6a52d0d2005-01-30 18:49:28 +000097 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000098 if (zserv_privs.change(ZPRIVS_LOWER))
99 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +0000100 zlog_err("Cannot create IPv6 datagram socket: %s",
101 safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +0000102 exit (1);
103 }
104
ajs4460e7a2005-01-29 17:07:40 +0000105 if ((ret = ioctl (sock, request, buffer)) < 0)
106 err = errno;
paul88424682004-05-09 18:21:35 +0000107
108 if (zserv_privs.change(ZPRIVS_LOWER))
109 zlog (NULL, LOG_ERR, "Can't lower privileges");
110
paul88424682004-05-09 18:21:35 +0000111 close (sock);
112
113 if (ret < 0)
114 {
115 errno = err;
116 return ret;
117 }
paul5b73a672004-07-23 15:26:14 +0000118#endif /* HAVE_IPV6 */
119
paul88424682004-05-09 18:21:35 +0000120 return 0;
121}
paul88424682004-05-09 18:21:35 +0000122
123/*
124 * get interface metric
125 * -- if value is not avaliable set -1
126 */
127void
128if_get_metric (struct interface *ifp)
129{
130 struct lifreq lifreq;
131 int ret;
132
paul5c78b3d2006-01-25 04:31:40 +0000133 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000134
paul88424682004-05-09 18:21:35 +0000135 if (ifp->flags & IFF_IPV4)
136 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000137#ifdef SOLARIS_IPV6
paul88424682004-05-09 18:21:35 +0000138 else if (ifp->flags & IFF_IPV6)
139 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000140#endif /* SOLARIS_IPV6 */
paul88424682004-05-09 18:21:35 +0000141 else
142 ret = -1;
paul88424682004-05-09 18:21:35 +0000143
144 if (ret < 0)
145 return;
146
147 ifp->metric = lifreq.lifr_metric;
148
149 if (ifp->metric == 0)
150 ifp->metric = 1;
151}
152
153/* get interface MTU */
154void
155if_get_mtu (struct interface *ifp)
156{
157 struct lifreq lifreq;
158 int ret;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000159 u_char changed = 0;
paul88424682004-05-09 18:21:35 +0000160
paul88424682004-05-09 18:21:35 +0000161 if (ifp->flags & IFF_IPV4)
162 {
paul5c78b3d2006-01-25 04:31:40 +0000163 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000164 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
165 if (ret < 0)
166 {
167 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
168 ifp->name);
169 ifp->mtu = -1;
170 }
171 else
172 {
173 ifp->mtu = lifreq.lifr_metric;
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000174 changed = 1;
paul88424682004-05-09 18:21:35 +0000175 }
176 }
177
paul5b73a672004-07-23 15:26:14 +0000178#ifdef HAVE_IPV6
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000179 if (ifp->flags & IFF_IPV6)
180 {
181 memset(&lifreq, 0, sizeof(lifreq));
182 lifreq_set_name (&lifreq, ifp->name);
paul5b73a672004-07-23 15:26:14 +0000183
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000184 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
185 if (ret < 0)
paul88424682004-05-09 18:21:35 +0000186 {
187 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
188 ifp->mtu6 = -1;
189 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000190 else
paul88424682004-05-09 18:21:35 +0000191 {
192 ifp->mtu6 = lifreq.lifr_metric;
Paul Jakmabd9da7f2007-04-29 16:50:46 +0000193 changed = 1;
paul88424682004-05-09 18:21:35 +0000194 }
Denis Ovsienkoa13663a2007-08-21 16:12:40 +0000195 }
196#endif /* HAVE_IPV6 */
197
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000198 if (changed)
199 zebra_interface_up_update(ifp);
paul88424682004-05-09 18:21:35 +0000200}
201
202/* Set up interface's address, netmask (and broadcast? ).
203 Solaris uses ifname:number semantics to set IP address aliases. */
204int
205if_set_prefix (struct interface *ifp, struct connected *ifc)
206{
207 int ret;
208 struct ifreq ifreq;
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;
214
215 p = (struct prefix_ipv4 *) ifc->address;
216
217 ifaddr = *p;
218
219 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
220
221 addr.sin_addr = p->prefix;
222 addr.sin_family = p->family;
223 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
224
225 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
226
227 if (ret < 0)
228 return ret;
229
230 /* We need mask for make broadcast addr. */
231 masklen2ip (p->prefixlen, &mask.sin_addr);
232
233 if (if_is_broadcast (ifp))
234 {
235 apply_mask_ipv4 (&ifaddr);
236 addr.sin_addr = ifaddr.prefix;
237
238 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
239 broad.sin_family = p->family;
240
241 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
242 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
243 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000244 return ret;
paul88424682004-05-09 18:21:35 +0000245 }
246
247 mask.sin_family = p->family;
248#ifdef SUNOS_5
249 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
250#else
251 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000252#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000253 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
254
255 return ((ret < 0) ? ret : 0);
256}
257
258/* Set up interface's address, netmask (and broadcast).
259 Solaris uses ifname:number semantics to set IP address aliases. */
260int
261if_unset_prefix (struct interface *ifp, struct connected *ifc)
262{
263 int ret;
264 struct ifreq ifreq;
265 struct sockaddr_in addr;
266 struct prefix_ipv4 *p;
267
268 p = (struct prefix_ipv4 *) ifc->address;
269
270 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
271
272 memset (&addr, 0, sizeof (struct sockaddr_in));
273 addr.sin_family = p->family;
274 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
275
276 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
277
278 if (ret < 0)
279 return ret;
280
281 return 0;
282}
283
paul5c78b3d2006-01-25 04:31:40 +0000284/* 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..
paul0752ef02005-11-03 12:35:21 +0000288 */
paul5c78b3d2006-01-25 04:31:40 +0000289int
290if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af)
paul0752ef02005-11-03 12:35:21 +0000291{
paul5c78b3d2006-01-25 04:31:40 +0000292 struct lifreq lifreq;
293 int ret;
294
295 lifreq_set_name (&lifreq, ifname);
296
297 ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq);
298
299 if (ret)
300 zlog_debug ("%s: ifname %s, error %s (%d)",
301 __func__, ifname, safe_strerror (errno), errno);
302
303 *flags = lifreq.lifr_flags;
304
305 return ret;
paul0752ef02005-11-03 12:35:21 +0000306}
307
paul88424682004-05-09 18:21:35 +0000308/* get interface flags */
309void
310if_get_flags (struct interface *ifp)
311{
paul0752ef02005-11-03 12:35:21 +0000312 int ret4, ret6;
paul5c78b3d2006-01-25 04:31:40 +0000313 uint64_t newflags = 0;
314 uint64_t tmpflags;
paul88424682004-05-09 18:21:35 +0000315
316 if (ifp->flags & IFF_IPV4)
317 {
paul5c78b3d2006-01-25 04:31:40 +0000318 ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET);
paul0752ef02005-11-03 12:35:21 +0000319
320 if (!ret4)
paul5c78b3d2006-01-25 04:31:40 +0000321 newflags |= tmpflags;
322 else if (errno == ENXIO)
323 {
324 /* it's gone */
325 UNSET_FLAG (ifp->flags, IFF_UP);
326 if_flags_update (ifp, ifp->flags);
327 }
paul88424682004-05-09 18:21:35 +0000328 }
329
330 if (ifp->flags & IFF_IPV6)
331 {
paul5c78b3d2006-01-25 04:31:40 +0000332 ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6);
paul0752ef02005-11-03 12:35:21 +0000333
334 if (!ret6)
paul5c78b3d2006-01-25 04:31:40 +0000335 newflags |= tmpflags;
336 else if (errno == ENXIO)
337 {
338 /* it's gone */
339 UNSET_FLAG (ifp->flags, IFF_UP);
340 if_flags_update (ifp, ifp->flags);
341 }
paul88424682004-05-09 18:21:35 +0000342 }
paul0752ef02005-11-03 12:35:21 +0000343
344 /* only update flags if one of above succeeded */
345 if ( !(ret4 && ret6) )
paul5c78b3d2006-01-25 04:31:40 +0000346 if_flags_update (ifp, newflags);
paul88424682004-05-09 18:21:35 +0000347}
348
349/* Set interface flags */
350int
paul5c78b3d2006-01-25 04:31:40 +0000351if_set_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000352{
353 int ret;
354 struct lifreq lifreq;
355
paul5c78b3d2006-01-25 04:31:40 +0000356 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000357
358 lifreq.lifr_flags = ifp->flags;
359 lifreq.lifr_flags |= flags;
360
paul88424682004-05-09 18:21:35 +0000361 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);
365 else
366 ret = -1;
367
368 if (ret < 0)
369 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000370 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000371 else
372 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000373
374 return ret;
paul88424682004-05-09 18:21:35 +0000375}
376
377/* Unset interface's flag. */
378int
paul5c78b3d2006-01-25 04:31:40 +0000379if_unset_flags (struct interface *ifp, uint64_t flags)
paul88424682004-05-09 18:21:35 +0000380{
381 int ret;
382 struct lifreq lifreq;
383
paul5c78b3d2006-01-25 04:31:40 +0000384 lifreq_set_name (&lifreq, ifp->name);
paul88424682004-05-09 18:21:35 +0000385
386 lifreq.lifr_flags = ifp->flags;
387 lifreq.lifr_flags &= ~flags;
388
paul88424682004-05-09 18:21:35 +0000389 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);
393 else
394 ret = -1;
395
396 if (ret < 0)
397 zlog_info ("can't unset interface flags");
398 else
399 ret = 0;
400
paul48a46fa2004-05-11 10:55:22 +0000401 return ret;
paul88424682004-05-09 18:21:35 +0000402}
403
404#ifdef HAVE_IPV6
405
406/* Interface's address add/delete functions. */
407int
408if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
409{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300410 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000411
Timo Teräsbe6335d2015-05-23 11:08:41 +0300412 zlog_warn ("Can't set %s on interface %s",
413 prefix2str(ifc->address->prefix, addrbuf, sizeof(addrbuf)),
414 ifp->name);
paul88424682004-05-09 18:21:35 +0000415
416 return 0;
417
418}
419
420int
421if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
422{
Timo Teräsbe6335d2015-05-23 11:08:41 +0300423 char addrbuf[PREFIX_STRLEN];
paul88424682004-05-09 18:21:35 +0000424
Timo Teräsbe6335d2015-05-23 11:08:41 +0300425 zlog_warn ("Can't delete %s on interface %s",
426 prefix2str(ifc->address->prefix, addrbuf, sizeof(addrbuf)),
427 ifp->name);
paul88424682004-05-09 18:21:35 +0000428
429 return 0;
430
431}
432
433#endif /* HAVE_IPV6 */