blob: 5ec327acdcfafe28f1f865b13cace0a81b676900 [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 {
58 if (zserv_privs.change(ZPRIVS_LOWER))
59 zlog (NULL, LOG_ERR, "Can't lower privileges");
60 perror ("socket");
61 exit (1);
62 }
63
ajs4460e7a2005-01-29 17:07:40 +000064 if ((ret = ioctl (sock, request, buffer)) < 0)
65 err = errno;
paul88424682004-05-09 18:21:35 +000066
67 if (zserv_privs.change(ZPRIVS_LOWER))
68 zlog (NULL, LOG_ERR, "Can't lower privileges");
69
paul88424682004-05-09 18:21:35 +000070 close (sock);
71
72 if (ret < 0)
73 {
74 errno = err;
75 return ret;
76 }
77 return 0;
78}
79
paul5b73a672004-07-23 15:26:14 +000080
paul88424682004-05-09 18:21:35 +000081int
82if_ioctl_ipv6 (u_long request, caddr_t buffer)
83{
paul5b73a672004-07-23 15:26:14 +000084#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +000085 int sock;
ajs4460e7a2005-01-29 17:07:40 +000086 int ret;
87 int err;
paul88424682004-05-09 18:21:35 +000088
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
ajs4460e7a2005-01-29 17:07:40 +0000101 if ((ret = ioctl (sock, request, buffer)) < 0)
102 err = errno;
paul88424682004-05-09 18:21:35 +0000103
104 if (zserv_privs.change(ZPRIVS_LOWER))
105 zlog (NULL, LOG_ERR, "Can't lower privileges");
106
paul88424682004-05-09 18:21:35 +0000107 close (sock);
108
109 if (ret < 0)
110 {
111 errno = err;
112 return ret;
113 }
paul5b73a672004-07-23 15:26:14 +0000114#endif /* HAVE_IPV6 */
115
paul88424682004-05-09 18:21:35 +0000116 return 0;
117}
paul88424682004-05-09 18:21:35 +0000118
119/*
120 * get interface metric
121 * -- if value is not avaliable set -1
122 */
123void
124if_get_metric (struct interface *ifp)
125{
126 struct lifreq lifreq;
127 int ret;
128
129 lifreq_set_name (&lifreq, ifp);
130
paul88424682004-05-09 18:21:35 +0000131 if (ifp->flags & IFF_IPV4)
132 ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000133#ifdef SOLARIS_IPV6
paul88424682004-05-09 18:21:35 +0000134 else if (ifp->flags & IFF_IPV6)
135 ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
paul5b73a672004-07-23 15:26:14 +0000136#endif /* SOLARIS_IPV6 */
paul88424682004-05-09 18:21:35 +0000137 else
138 ret = -1;
paul88424682004-05-09 18:21:35 +0000139
140 if (ret < 0)
141 return;
142
143 ifp->metric = lifreq.lifr_metric;
144
145 if (ifp->metric == 0)
146 ifp->metric = 1;
147}
148
149/* get interface MTU */
150void
151if_get_mtu (struct interface *ifp)
152{
153 struct lifreq lifreq;
154 int ret;
155
paul88424682004-05-09 18:21:35 +0000156 if (ifp->flags & IFF_IPV4)
157 {
158 lifreq_set_name (&lifreq, ifp);
159 ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
160 if (ret < 0)
161 {
162 zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
163 ifp->name);
164 ifp->mtu = -1;
165 }
166 else
167 {
168 ifp->mtu = lifreq.lifr_metric;
169 }
170 }
171
paul5b73a672004-07-23 15:26:14 +0000172#ifdef HAVE_IPV6
paul88424682004-05-09 18:21:35 +0000173 if ((ifp->flags & IFF_IPV6) == 0)
paul5b73a672004-07-23 15:26:14 +0000174 return;
175
176 memset(&lifreq, 0, sizeof(lifreq));
paul88424682004-05-09 18:21:35 +0000177 lifreq_set_name (&lifreq, ifp);
paul5b73a672004-07-23 15:26:14 +0000178
paul88424682004-05-09 18:21:35 +0000179 ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
180 if (ret < 0)
181 {
182 zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
183 ifp->mtu6 = -1;
184 }
185 else
186 {
187 ifp->mtu6 = lifreq.lifr_metric;
188 }
paul5b73a672004-07-23 15:26:14 +0000189#endif /* HAVE_IPV6 */
paul88424682004-05-09 18:21:35 +0000190}
191
192/* Set up interface's address, netmask (and broadcast? ).
193 Solaris uses ifname:number semantics to set IP address aliases. */
194int
195if_set_prefix (struct interface *ifp, struct connected *ifc)
196{
197 int ret;
198 struct ifreq ifreq;
199 struct sockaddr_in addr;
200 struct sockaddr_in broad;
201 struct sockaddr_in mask;
202 struct prefix_ipv4 ifaddr;
203 struct prefix_ipv4 *p;
204
205 p = (struct prefix_ipv4 *) ifc->address;
206
207 ifaddr = *p;
208
209 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
210
211 addr.sin_addr = p->prefix;
212 addr.sin_family = p->family;
213 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
214
215 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
216
217 if (ret < 0)
218 return ret;
219
220 /* We need mask for make broadcast addr. */
221 masklen2ip (p->prefixlen, &mask.sin_addr);
222
223 if (if_is_broadcast (ifp))
224 {
225 apply_mask_ipv4 (&ifaddr);
226 addr.sin_addr = ifaddr.prefix;
227
228 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
229 broad.sin_family = p->family;
230
231 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
232 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
233 if (ret < 0)
paul48a46fa2004-05-11 10:55:22 +0000234 return ret;
paul88424682004-05-09 18:21:35 +0000235 }
236
237 mask.sin_family = p->family;
238#ifdef SUNOS_5
239 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
240#else
241 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
paul48a46fa2004-05-11 10:55:22 +0000242#endif /* SUNOS_5 */
paul88424682004-05-09 18:21:35 +0000243 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
244
245 return ((ret < 0) ? ret : 0);
246}
247
248/* Set up interface's address, netmask (and broadcast).
249 Solaris uses ifname:number semantics to set IP address aliases. */
250int
251if_unset_prefix (struct interface *ifp, struct connected *ifc)
252{
253 int ret;
254 struct ifreq ifreq;
255 struct sockaddr_in addr;
256 struct prefix_ipv4 *p;
257
258 p = (struct prefix_ipv4 *) ifc->address;
259
260 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
261
262 memset (&addr, 0, sizeof (struct sockaddr_in));
263 addr.sin_family = p->family;
264 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
265
266 ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
267
268 if (ret < 0)
269 return ret;
270
271 return 0;
272}
273
274/* get interface flags */
275void
276if_get_flags (struct interface *ifp)
277{
278 int ret;
279 struct lifreq lifreq;
280 unsigned long flags4 = 0, flags6 = 0;
281
282 if (ifp->flags & IFF_IPV4)
283 {
284 lifreq_set_name (&lifreq, ifp);
285
paul88424682004-05-09 18:21:35 +0000286 ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
paul88424682004-05-09 18:21:35 +0000287
288 flags4 = (lifreq.lifr_flags & 0xffffffff);
289 if (!(flags4 & IFF_UP))
290 flags4 &= ~IFF_IPV4;
291 }
292
293 if (ifp->flags & IFF_IPV6)
294 {
295 lifreq_set_name (&lifreq, ifp);
296
paul88424682004-05-09 18:21:35 +0000297 ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
paul88424682004-05-09 18:21:35 +0000298
299 flags6 = (lifreq.lifr_flags & 0xffffffff);
300 if (!(flags6 & IFF_UP))
301 flags6 &= ~IFF_IPV6;
302 }
303
304 ifp->flags = (flags4 | flags6);
305}
306
307/* Set interface flags */
308int
309if_set_flags (struct interface *ifp, unsigned long flags)
310{
311 int ret;
312 struct lifreq lifreq;
313
314 lifreq_set_name (&lifreq, ifp);
315
316 lifreq.lifr_flags = ifp->flags;
317 lifreq.lifr_flags |= flags;
318
paul88424682004-05-09 18:21:35 +0000319 if (ifp->flags & IFF_IPV4)
320 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
321 else if (ifp->flags & IFF_IPV6)
322 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
323 else
324 ret = -1;
325
326 if (ret < 0)
327 zlog_info ("can't set interface flags on %s: %s", ifp->name,
ajs6099b3b2004-11-20 02:06:59 +0000328 safe_strerror (errno));
paul88424682004-05-09 18:21:35 +0000329 else
330 ret = 0;
paul48a46fa2004-05-11 10:55:22 +0000331
332 return ret;
paul88424682004-05-09 18:21:35 +0000333}
334
335/* Unset interface's flag. */
336int
337if_unset_flags (struct interface *ifp, unsigned long flags)
338{
339 int ret;
340 struct lifreq lifreq;
341
342 lifreq_set_name (&lifreq, ifp);
343
344 lifreq.lifr_flags = ifp->flags;
345 lifreq.lifr_flags &= ~flags;
346
paul88424682004-05-09 18:21:35 +0000347 if (ifp->flags & IFF_IPV4)
348 ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
349 else if (ifp->flags & IFF_IPV6)
350 ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
351 else
352 ret = -1;
353
354 if (ret < 0)
355 zlog_info ("can't unset interface flags");
356 else
357 ret = 0;
358
paul48a46fa2004-05-11 10:55:22 +0000359 return ret;
paul88424682004-05-09 18:21:35 +0000360}
361
362#ifdef HAVE_IPV6
363
364/* Interface's address add/delete functions. */
365int
366if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
367{
368 char addrbuf[INET_ADDRSTRLEN];
369
370 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
371 addrbuf, sizeof (addrbuf));
372 zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
373
374 return 0;
375
376}
377
378int
379if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
380{
381 char addrbuf[INET_ADDRSTRLEN];
382
383 inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
384 addrbuf, sizeof (addrbuf));
385 zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
386
387 return 0;
388
389}
390
391#endif /* HAVE_IPV6 */