blob: 2fdffc61d74c6a0aafdb43c19be71f4ceecce655 [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"
30
31#include "zebra/rib.h"
32#include "zebra/rt.h"
33
34
35/* clear and set interface name string */
36void
37lifreq_set_name (struct lifreq *lifreq, struct interface *ifp)
38{
39 strncpy (lifreq->lifr_name, ifp->name, IFNAMSIZ);
40}
41
42/* call ioctl system call */
43int
44if_ioctl (u_long request, caddr_t buffer)
45{
46 int sock;
47 int ret = 0;
48 int err = 0;
49
50 if (zserv_privs.change(ZPRIVS_RAISE))
51 zlog (NULL, LOG_ERR, "Can't raise privileges");
52
53 sock = socket (AF_INET, SOCK_DGRAM, 0);
54 if (sock < 0)
55 {
56 if (zserv_privs.change(ZPRIVS_LOWER))
57 zlog (NULL, LOG_ERR, "Can't lower privileges");
58 perror ("socket");
59 exit (1);
60 }
61
62 ret = ioctl (sock, request, buffer);
63
64 if (zserv_privs.change(ZPRIVS_LOWER))
65 zlog (NULL, LOG_ERR, "Can't lower privileges");
66
67 if (ret < 0)
68 {
69 err = errno;
70 }
71 close (sock);
72
73 if (ret < 0)
74 {
75 errno = err;
76 return ret;
77 }
78 return 0;
79}
80
81#ifdef HAVE_IPV6
82int
83if_ioctl_ipv6 (u_long request, caddr_t buffer)
84{
85 int sock;
86 int ret = 0;
87 int err = 0;
88
89 if (zserv_privs.change(ZPRIVS_RAISE))
90 zlog (NULL, LOG_ERR, "Can't raise privileges");
91
92 sock = socket (AF_INET6, SOCK_DGRAM, 0);
93 if (sock < 0)
94 {
95 if (zserv_privs.change(ZPRIVS_LOWER))
96 zlog (NULL, LOG_ERR, "Can't lower privileges");
97 perror ("socket");
98 exit (1);
99 }
100
101 ret = ioctl (sock, request, buffer);
102
103 if (zserv_privs.change(ZPRIVS_LOWER))
104 zlog (NULL, LOG_ERR, "Can't lower privileges");
105
106 if (ret < 0)
107 {
108 err = errno;
109 }
110 close (sock);
111
112 if (ret < 0)
113 {
114 errno = err;
115 return ret;
116 }
117 return 0;
118}
119#endif /* HAVE_IPV6 */
120
121/*
122 * get interface metric
123 * -- if value is not avaliable set -1
124 */
125void
126if_get_metric (struct interface *ifp)
127{
128 struct lifreq lifreq;
129 int ret;
130
131 lifreq_set_name (&lifreq, ifp);
132
133 if (zserv_privs.change(ZPRIVS_RAISE))
134 zlog (NULL, LOG_ERR, "Can't raise privileges");
135
136 if (ifp->flags & IFF_IPV4)
137 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 else
141 ret = -1;
142
143 if (zserv_privs.change(ZPRIVS_LOWER))
144 zlog (NULL, LOG_ERR, "Can't lower privileges");
145
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;
161
162 if (zserv_privs.change(ZPRIVS_RAISE))
163 zlog (NULL, LOG_ERR, "Can't raise privileges");
164
165 if (ifp->flags & IFF_IPV4)
166 {
167 lifreq_set_name (&lifreq, ifp);
168 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
169 if (ret < 0)
170 {
171 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
172 ifp->name);
173 ifp->mtu = -1;
174 }
175 else
176 {
177 ifp->mtu = lifreq.lifr_metric;
178 }
179 }
180
181
182 if ((ifp->flags & IFF_IPV6) == 0)
183 goto out;
184
185 lifreq_set_name (&lifreq, ifp);
186 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
187 if (ret < 0)
188 {
189 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
190 ifp->mtu6 = -1;
191 }
192 else
193 {
194 ifp->mtu6 = lifreq.lifr_metric;
195 }
196
197out:
198 if (zserv_privs.change(ZPRIVS_LOWER))
199 zlog (NULL, LOG_ERR, "Can't lower privileges");
200}
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)
244 goto out;
245 }
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));
252#endif /* SUNOS5 */
253 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
284/* get interface flags */
285void
286if_get_flags (struct interface *ifp)
287{
288 int ret;
289 struct lifreq lifreq;
290 unsigned long flags4 = 0, flags6 = 0;
291
292 if (ifp->flags & IFF_IPV4)
293 {
294 lifreq_set_name (&lifreq, ifp);
295
296 if (zserv_privs.change(ZPRIVS_RAISE))
297 zlog (NULL, LOG_ERR, "Can't raise privileges");
298
299 ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
300
301 if (zserv_privs.change(ZPRIVS_LOWER))
302 zlog (NULL, LOG_ERR, "Can't lower privileges");
303
304 flags4 = (lifreq.lifr_flags & 0xffffffff);
305 if (!(flags4 & IFF_UP))
306 flags4 &= ~IFF_IPV4;
307 }
308
309 if (ifp->flags & IFF_IPV6)
310 {
311 lifreq_set_name (&lifreq, ifp);
312
313 if (zserv_privs.change(ZPRIVS_RAISE))
314 zlog (NULL, LOG_ERR, "Can't raise privileges");
315
316 ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
317
318 if (zserv_privs.change(ZPRIVS_LOWER))
319 zlog (NULL, LOG_ERR, "Can't lower privileges");
320
321 flags6 = (lifreq.lifr_flags & 0xffffffff);
322 if (!(flags6 & IFF_UP))
323 flags6 &= ~IFF_IPV6;
324 }
325
326 ifp->flags = (flags4 | flags6);
327}
328
329/* Set interface flags */
330int
331if_set_flags (struct interface *ifp, unsigned long flags)
332{
333 int ret;
334 struct lifreq lifreq;
335
336 lifreq_set_name (&lifreq, ifp);
337
338 lifreq.lifr_flags = ifp->flags;
339 lifreq.lifr_flags |= flags;
340
341 if (zserv_privs.change(ZPRIVS_RAISE))
342 zlog (NULL, LOG_ERR, "Can't raise privileges");
343
344 if (ifp->flags & IFF_IPV4)
345 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
346 else if (ifp->flags & IFF_IPV6)
347 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
348 else
349 ret = -1;
350
351 if (ret < 0)
352 zlog_info ("can't set interface flags on %s: %s", ifp->name,
353 strerror (errno));
354 else
355 ret = 0;
356
357 if (zserv_privs.change(ZPRIVS_LOWER))
358 zlog (NULL, LOG_ERR, "Can't lower privileges");
359}
360
361/* Unset interface's flag. */
362int
363if_unset_flags (struct interface *ifp, unsigned long flags)
364{
365 int ret;
366 struct lifreq lifreq;
367
368 lifreq_set_name (&lifreq, ifp);
369
370 lifreq.lifr_flags = ifp->flags;
371 lifreq.lifr_flags &= ~flags;
372
373 if (zserv_privs.change(ZPRIVS_RAISE))
374 zlog (NULL, LOG_ERR, "Can't raise privileges");
375
376 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
388 if (zserv_privs.change(ZPRIVS_LOWER))
389 zlog (NULL, LOG_ERR, "Can't lower privileges");
390}
391
392#ifdef HAVE_IPV6
393
394/* Interface's address add/delete functions. */
395int
396if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
397{
398 char addrbuf[INET_ADDRSTRLEN];
399
400 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
401 addrbuf, sizeof (addrbuf));
402 zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
403
404 return 0;
405
406}
407
408int
409if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
410{
411 char addrbuf[INET_ADDRSTRLEN];
412
413 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
414 addrbuf, sizeof (addrbuf));
415 zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
416
417 return 0;
418
419}
420
421#endif /* HAVE_IPV6 */