blob: 8ebd0f41d00faff29d3d2af94d1b10f3a1e8fced [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;
49 int ret = 0;
50 int err = 0;
51
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 {
58 if (zserv_privs.change(ZPRIVS_LOWER))
59 zlog (NULL, LOG_ERR, "Can't lower privileges");
60 perror ("socket");
61 exit (1);
62 }
63
64 ret = ioctl (sock, request, buffer);
65
66 if (zserv_privs.change(ZPRIVS_LOWER))
67 zlog (NULL, LOG_ERR, "Can't lower privileges");
68
69 if (ret < 0)
70 {
71 err = errno;
72 }
73 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;
89 int ret = 0;
90 int err = 0;
91
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 {
98 if (zserv_privs.change(ZPRIVS_LOWER))
99 zlog (NULL, LOG_ERR, "Can't lower privileges");
100 perror ("socket");
101 exit (1);
102 }
103
104 ret = ioctl (sock, request, buffer);
105
106 if (zserv_privs.change(ZPRIVS_LOWER))
107 zlog (NULL, LOG_ERR, "Can't lower privileges");
108
109 if (ret < 0)
110 {
111 err = errno;
112 }
113 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
135 lifreq_set_name (&lifreq, ifp);
136
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;
161
paul88424682004-05-09 18:21:35 +0000162 if (ifp->flags & IFF_IPV4)
163 {
164 lifreq_set_name (&lifreq, ifp);
165 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;
175 }
176 }
177
paul5b73a672004-07-23 15:26:14 +0000178#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +0000179 if ((ifp->flags & IFF_IPV6) == 0)
paul5b73a672004-07-23 15:26:14 +0000180 return;
181
182 memset(&lifreq, 0, sizeof(lifreq));
paul88424682004-05-09 18:21:35 +0000183 lifreq_set_name (&lifreq, ifp);
paul5b73a672004-07-23 15:26:14 +0000184
paul88424682004-05-09 18:21:35 +0000185 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
186 if (ret < 0)
187 {
188 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
189 ifp->mtu6 = -1;
190 }
191 else
192 {
193 ifp->mtu6 = lifreq.lifr_metric;
194 }
paul5b73a672004-07-23 15:26:14 +0000195#endif /* HAVE_IPV6 */
paul88424682004-05-09 18:21:35 +0000196}
197
198/* Set up interface's address, netmask (and broadcast? ).
199 Solaris uses ifname:number semantics to set IP address aliases. */
200int
201if_set_prefix (struct interface *ifp, struct connected *ifc)
202{
203 int ret;
204 struct ifreq ifreq;
205 struct sockaddr_in addr;
206 struct sockaddr_in broad;
207 struct sockaddr_in mask;
208 struct prefix_ipv4 ifaddr;
209 struct prefix_ipv4 *p;
210
211 p = (struct prefix_ipv4 *) ifc->address;
212
213 ifaddr = *p;
214
215 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
216
217 addr.sin_addr = p->prefix;
218 addr.sin_family = p->family;
219 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
220
221 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
222
223 if (ret < 0)
224 return ret;
225
226 /* We need mask for make broadcast addr. */
227 masklen2ip (p->prefixlen, &mask.sin_addr);
228
229 if (if_is_broadcast (ifp))
230 {
231 apply_mask_ipv4 (&ifaddr);
232 addr.sin_addr = ifaddr.prefix;
233
234 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
235 broad.sin_family = p->family;
236
237 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
238 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
239 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000240 return ret;
paul88424682004-05-09 18:21:35 +0000241 }
242
243 mask.sin_family = p->family;
244#ifdef SUNOS_5
245 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
246#else
247 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000248#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000249 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
250
251 return ((ret < 0) ? ret : 0);
252}
253
254/* Set up interface's address, netmask (and broadcast).
255 Solaris uses ifname:number semantics to set IP address aliases. */
256int
257if_unset_prefix (struct interface *ifp, struct connected *ifc)
258{
259 int ret;
260 struct ifreq ifreq;
261 struct sockaddr_in addr;
262 struct prefix_ipv4 *p;
263
264 p = (struct prefix_ipv4 *) ifc->address;
265
266 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
267
268 memset (&addr, 0, sizeof (struct sockaddr_in));
269 addr.sin_family = p->family;
270 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
271
272 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
273
274 if (ret < 0)
275 return ret;
276
277 return 0;
278}
279
280/* get interface flags */
281void
282if_get_flags (struct interface *ifp)
283{
284 int ret;
285 struct lifreq lifreq;
286 unsigned long flags4 = 0, flags6 = 0;
287
288 if (ifp->flags & IFF_IPV4)
289 {
290 lifreq_set_name (&lifreq, ifp);
291
paul88424682004-05-09 18:21:35 +0000292 ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
paul88424682004-05-09 18:21:35 +0000293
294 flags4 = (lifreq.lifr_flags & 0xffffffff);
295 if (!(flags4 & IFF_UP))
296 flags4 &= ~IFF_IPV4;
297 }
298
299 if (ifp->flags & IFF_IPV6)
300 {
301 lifreq_set_name (&lifreq, ifp);
302
paul88424682004-05-09 18:21:35 +0000303 ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
paul88424682004-05-09 18:21:35 +0000304
305 flags6 = (lifreq.lifr_flags & 0xffffffff);
306 if (!(flags6 & IFF_UP))
307 flags6 &= ~IFF_IPV6;
308 }
309
310 ifp->flags = (flags4 | flags6);
311}
312
313/* Set interface flags */
314int
315if_set_flags (struct interface *ifp, unsigned long flags)
316{
317 int ret;
318 struct lifreq lifreq;
319
320 lifreq_set_name (&lifreq, ifp);
321
322 lifreq.lifr_flags = ifp->flags;
323 lifreq.lifr_flags |= flags;
324
paul88424682004-05-09 18:21:35 +0000325 if (ifp->flags & IFF_IPV4)
326 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
327 else if (ifp->flags & IFF_IPV6)
328 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
329 else
330 ret = -1;
331
332 if (ret < 0)
333 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000334 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000335 else
336 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000337
338 return ret;
paul88424682004-05-09 18:21:35 +0000339}
340
341/* Unset interface's flag. */
342int
343if_unset_flags (struct interface *ifp, unsigned long flags)
344{
345 int ret;
346 struct lifreq lifreq;
347
348 lifreq_set_name (&lifreq, ifp);
349
350 lifreq.lifr_flags = ifp->flags;
351 lifreq.lifr_flags &= ~flags;
352
paul88424682004-05-09 18:21:35 +0000353 if (ifp->flags & IFF_IPV4)
354 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
355 else if (ifp->flags & IFF_IPV6)
356 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
357 else
358 ret = -1;
359
360 if (ret < 0)
361 zlog_info ("can't unset interface flags");
362 else
363 ret = 0;
364
paul48a46fa2004-05-11 10:55:22 +0000365 return ret;
paul88424682004-05-09 18:21:35 +0000366}
367
368#ifdef HAVE_IPV6
369
370/* Interface's address add/delete functions. */
371int
372if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
373{
374 char addrbuf[INET_ADDRSTRLEN];
375
376 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
377 addrbuf, sizeof (addrbuf));
378 zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
379
380 return 0;
381
382}
383
384int
385if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
386{
387 char addrbuf[INET_ADDRSTRLEN];
388
389 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
390 addrbuf, sizeof (addrbuf));
391 zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
392
393 return 0;
394
395}
396
397#endif /* HAVE_IPV6 */