blob: ec1d2c440384855ba94f5e84c99d4d3dbeaadf4e [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"
34
paul48a46fa2004-05-11 10:55:22 +000035extern struct zebra_privs_t zserv_privs;
paul88424682004-05-09 18:21:35 +000036
37/* clear and set interface name string */
38void
39lifreq_set_name (struct lifreq *lifreq, struct interface *ifp)
40{
41 strncpy (lifreq->lifr_name, ifp->name, IFNAMSIZ);
42}
43
44/* call ioctl system call */
45int
46if_ioctl (u_long request, caddr_t buffer)
47{
48 int sock;
ajs4460e7a2005-01-29 17:07:40 +000049 int ret;
50 int err;
paul88424682004-05-09 18:21:35 +000051
52 if (zserv_privs.change(ZPRIVS_RAISE))
53 zlog (NULL, LOG_ERR, "Can't raise privileges");
54
55 sock = socket (AF_INET, SOCK_DGRAM, 0);
56 if (sock < 0)
57 {
ajs6a52d0d2005-01-30 18:49:28 +000058 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000059 if (zserv_privs.change(ZPRIVS_LOWER))
60 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000061 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +000062 exit (1);
63 }
64
ajs4460e7a2005-01-29 17:07:40 +000065 if ((ret = ioctl (sock, request, buffer)) < 0)
66 err = errno;
paul88424682004-05-09 18:21:35 +000067
68 if (zserv_privs.change(ZPRIVS_LOWER))
69 zlog (NULL, LOG_ERR, "Can't lower privileges");
70
paul88424682004-05-09 18:21:35 +000071 close (sock);
72
73 if (ret < 0)
74 {
75 errno = err;
76 return ret;
77 }
78 return 0;
79}
80
paul5b73a672004-07-23 15:26:14 +000081
paul88424682004-05-09 18:21:35 +000082int
83if_ioctl_ipv6 (u_long request, caddr_t buffer)
84{
paul5b73a672004-07-23 15:26:14 +000085#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +000086 int sock;
ajs4460e7a2005-01-29 17:07:40 +000087 int ret;
88 int err;
paul88424682004-05-09 18:21:35 +000089
90 if (zserv_privs.change(ZPRIVS_RAISE))
91 zlog (NULL, LOG_ERR, "Can't raise privileges");
92
93 sock = socket (AF_INET6, SOCK_DGRAM, 0);
94 if (sock < 0)
95 {
ajs6a52d0d2005-01-30 18:49:28 +000096 int save_errno = errno;
paul88424682004-05-09 18:21:35 +000097 if (zserv_privs.change(ZPRIVS_LOWER))
98 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000099 zlog_err("Cannot create IPv6 datagram socket: %s",
100 safe_strerror(save_errno));
paul88424682004-05-09 18:21:35 +0000101 exit (1);
102 }
103
ajs4460e7a2005-01-29 17:07:40 +0000104 if ((ret = ioctl (sock, request, buffer)) < 0)
105 err = errno;
paul88424682004-05-09 18:21:35 +0000106
107 if (zserv_privs.change(ZPRIVS_LOWER))
108 zlog (NULL, LOG_ERR, "Can't lower privileges");
109
paul88424682004-05-09 18:21:35 +0000110 close (sock);
111
112 if (ret < 0)
113 {
114 errno = err;
115 return ret;
116 }
paul5b73a672004-07-23 15:26:14 +0000117#endif /* HAVE_IPV6 */
118
paul88424682004-05-09 18:21:35 +0000119 return 0;
120}
paul88424682004-05-09 18:21:35 +0000121
122/*
123 * get interface metric
124 * -- if value is not avaliable set -1
125 */
126void
127if_get_metric (struct interface *ifp)
128{
129 struct lifreq lifreq;
130 int ret;
131
132 lifreq_set_name (&lifreq, ifp);
133
paul88424682004-05-09 18:21:35 +0000134 if (ifp->flags & IFF_IPV4)
135 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000136#ifdef SOLARIS_IPV6
paul88424682004-05-09 18:21:35 +0000137 else if (ifp->flags & IFF_IPV6)
138 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000139#endif /* SOLARIS_IPV6 */
paul88424682004-05-09 18:21:35 +0000140 else
141 ret = -1;
paul88424682004-05-09 18:21:35 +0000142
143 if (ret < 0)
144 return;
145
146 ifp->metric = lifreq.lifr_metric;
147
148 if (ifp->metric == 0)
149 ifp->metric = 1;
150}
151
152/* get interface MTU */
153void
154if_get_mtu (struct interface *ifp)
155{
156 struct lifreq lifreq;
157 int ret;
158
paul88424682004-05-09 18:21:35 +0000159 if (ifp->flags & IFF_IPV4)
160 {
161 lifreq_set_name (&lifreq, ifp);
162 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
163 if (ret < 0)
164 {
165 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
166 ifp->name);
167 ifp->mtu = -1;
168 }
169 else
170 {
171 ifp->mtu = lifreq.lifr_metric;
172 }
173 }
174
paul5b73a672004-07-23 15:26:14 +0000175#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +0000176 if ((ifp->flags & IFF_IPV6) == 0)
paul5b73a672004-07-23 15:26:14 +0000177 return;
178
179 memset(&lifreq, 0, sizeof(lifreq));
paul88424682004-05-09 18:21:35 +0000180 lifreq_set_name (&lifreq, ifp);
paul5b73a672004-07-23 15:26:14 +0000181
paul88424682004-05-09 18:21:35 +0000182 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
183 if (ret < 0)
184 {
185 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
186 ifp->mtu6 = -1;
187 }
188 else
189 {
190 ifp->mtu6 = lifreq.lifr_metric;
191 }
paul5b73a672004-07-23 15:26:14 +0000192#endif /* HAVE_IPV6 */
paul88424682004-05-09 18:21:35 +0000193}
194
195/* Set up interface's address, netmask (and broadcast? ).
196 Solaris uses ifname:number semantics to set IP address aliases. */
197int
198if_set_prefix (struct interface *ifp, struct connected *ifc)
199{
200 int ret;
201 struct ifreq ifreq;
202 struct sockaddr_in addr;
203 struct sockaddr_in broad;
204 struct sockaddr_in mask;
205 struct prefix_ipv4 ifaddr;
206 struct prefix_ipv4 *p;
207
208 p = (struct prefix_ipv4 *) ifc->address;
209
210 ifaddr = *p;
211
212 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
213
214 addr.sin_addr = p->prefix;
215 addr.sin_family = p->family;
216 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
217
218 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
219
220 if (ret < 0)
221 return ret;
222
223 /* We need mask for make broadcast addr. */
224 masklen2ip (p->prefixlen, &mask.sin_addr);
225
226 if (if_is_broadcast (ifp))
227 {
228 apply_mask_ipv4 (&ifaddr);
229 addr.sin_addr = ifaddr.prefix;
230
231 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
232 broad.sin_family = p->family;
233
234 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
235 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
236 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000237 return ret;
paul88424682004-05-09 18:21:35 +0000238 }
239
240 mask.sin_family = p->family;
241#ifdef SUNOS_5
242 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
243#else
244 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000245#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000246 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
247
248 return ((ret < 0) ? ret : 0);
249}
250
251/* Set up interface's address, netmask (and broadcast).
252 Solaris uses ifname:number semantics to set IP address aliases. */
253int
254if_unset_prefix (struct interface *ifp, struct connected *ifc)
255{
256 int ret;
257 struct ifreq ifreq;
258 struct sockaddr_in addr;
259 struct prefix_ipv4 *p;
260
261 p = (struct prefix_ipv4 *) ifc->address;
262
263 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
264
265 memset (&addr, 0, sizeof (struct sockaddr_in));
266 addr.sin_family = p->family;
267 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
268
269 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
270
271 if (ret < 0)
272 return ret;
273
274 return 0;
275}
276
paul0752ef02005-11-03 12:35:21 +0000277/* Solaris IFF_UP flag reflects only the primary interface as the
278 * routing socket only sends IFINFO for the primary interface. Hence
279 * ~IFF_UP does not per se imply all the logical interfaces are also
280 * down - which we only know of as addresses. Instead we must determine
281 * whether the interface really is up or not according to how many
282 * addresses are still attached. (Solaris always sends RTM_DELADDR if
283 * an interface, logical or not, goes ~IFF_UP).
284 *
285 * Ie, we mangle IFF_UP to reflect whether or not there are addresses
286 * left in struct connected, not the actual underlying IFF_UP flag
287 * (which corresponds to just one address of all the logical interfaces)
288 *
289 * Setting IFF_UP within zebra to administratively shutdown the
290 * interface will affect only the primary interface/address on Solaris.
291 */
292static inline void
293if_mangle_up (struct interface *ifp)
294{
295 if (listcount(ifp->connected) > 0)
296 SET_FLAG (ifp->flags, IFF_UP);
297 else
298 UNSET_FLAG (ifp->flags, IFF_UP);
299}
300
paul88424682004-05-09 18:21:35 +0000301/* get interface flags */
302void
303if_get_flags (struct interface *ifp)
304{
paul0752ef02005-11-03 12:35:21 +0000305 int ret4, ret6;
paul88424682004-05-09 18:21:35 +0000306 struct lifreq lifreq;
307 unsigned long flags4 = 0, flags6 = 0;
308
309 if (ifp->flags & IFF_IPV4)
310 {
311 lifreq_set_name (&lifreq, ifp);
312
paul0752ef02005-11-03 12:35:21 +0000313 ret4 = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
314
315 if (!ret4)
316 flags4 = (lifreq.lifr_flags & 0xffffffff);
paul88424682004-05-09 18:21:35 +0000317 }
318
319 if (ifp->flags & IFF_IPV6)
320 {
321 lifreq_set_name (&lifreq, ifp);
322
paul0752ef02005-11-03 12:35:21 +0000323 ret6 = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
324
325 if (!ret6)
326 flags6 = (lifreq.lifr_flags & 0xffffffff);
paul88424682004-05-09 18:21:35 +0000327 }
paul0752ef02005-11-03 12:35:21 +0000328
329 /* only update flags if one of above succeeded */
330 if ( !(ret4 && ret6) )
331 ifp->flags = (flags4 | flags6);
paul88424682004-05-09 18:21:35 +0000332
paul0752ef02005-11-03 12:35:21 +0000333 if_mangle_up (ifp);
paul88424682004-05-09 18:21:35 +0000334}
335
336/* Set interface flags */
337int
338if_set_flags (struct interface *ifp, unsigned long flags)
339{
340 int ret;
341 struct lifreq lifreq;
342
343 lifreq_set_name (&lifreq, ifp);
344
345 lifreq.lifr_flags = ifp->flags;
346 lifreq.lifr_flags |= flags;
347
paul88424682004-05-09 18:21:35 +0000348 if (ifp->flags & IFF_IPV4)
349 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
350 else if (ifp->flags & IFF_IPV6)
351 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
352 else
353 ret = -1;
354
355 if (ret < 0)
356 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000357 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000358 else
359 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000360
361 return ret;
paul88424682004-05-09 18:21:35 +0000362}
363
364/* Unset interface's flag. */
365int
366if_unset_flags (struct interface *ifp, unsigned long flags)
367{
368 int ret;
369 struct lifreq lifreq;
370
371 lifreq_set_name (&lifreq, ifp);
372
373 lifreq.lifr_flags = ifp->flags;
374 lifreq.lifr_flags &= ~flags;
375
paul88424682004-05-09 18:21:35 +0000376 if (ifp->flags & IFF_IPV4)
377 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
378 else if (ifp->flags & IFF_IPV6)
379 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
380 else
381 ret = -1;
382
383 if (ret < 0)
384 zlog_info ("can't unset interface flags");
385 else
386 ret = 0;
387
paul48a46fa2004-05-11 10:55:22 +0000388 return ret;
paul88424682004-05-09 18:21:35 +0000389}
390
391#ifdef HAVE_IPV6
392
393/* Interface's address add/delete functions. */
394int
395if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
396{
397 char addrbuf[INET_ADDRSTRLEN];
398
399 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
400 addrbuf, sizeof (addrbuf));
401 zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
402
403 return 0;
404
405}
406
407int
408if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
409{
410 char addrbuf[INET_ADDRSTRLEN];
411
412 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
413 addrbuf, sizeof (addrbuf));
414 zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
415
416 return 0;
417
418}
419
420#endif /* HAVE_IPV6 */