blob: f8e7f22b650352ec38b0142acf98d6a8314b5ab9 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Common ioctl functions.
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"
pauledd7c242003-06-04 13:59:38 +000030#include "privs.h"
paul718e3742002-12-13 20:15:29 +000031
32#include "zebra/rib.h"
33#include "zebra/rt.h"
34
pauledd7c242003-06-04 13:59:38 +000035extern struct zebra_privs_t zserv_privs;
36
paul718e3742002-12-13 20:15:29 +000037/* clear and set interface name string */
38void
39ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
40{
41 strncpy (ifreq->ifr_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
pauledd7c242003-06-04 13:59:38 +000052 if (zserv_privs.change(ZPRIVS_RAISE))
53 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000054 sock = socket (AF_INET, SOCK_DGRAM, 0);
55 if (sock < 0)
56 {
pauledd7c242003-06-04 13:59:38 +000057 if (zserv_privs.change(ZPRIVS_LOWER))
58 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +000059 perror ("socket");
60 exit (1);
61 }
paul718e3742002-12-13 20:15:29 +000062 ret = ioctl (sock, request, buffer);
pauledd7c242003-06-04 13:59:38 +000063 if (zserv_privs.change(ZPRIVS_LOWER))
64 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +000065 if (ret < 0)
66 {
67 err = errno;
68 }
69 close (sock);
70
71 if (ret < 0)
72 {
73 errno = err;
74 return ret;
75 }
76 return 0;
77}
78
79#ifdef HAVE_IPV6
80int
81if_ioctl_ipv6 (u_long request, caddr_t buffer)
82{
83 int sock;
84 int ret = 0;
85 int err = 0;
86
pauledd7c242003-06-04 13:59:38 +000087 if (zserv_privs.change(ZPRIVS_RAISE))
88 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000089 sock = socket (AF_INET6, SOCK_DGRAM, 0);
90 if (sock < 0)
91 {
pauledd7c242003-06-04 13:59:38 +000092 if (zserv_privs.change(ZPRIVS_LOWER))
93 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +000094 perror ("socket");
95 exit (1);
96 }
97
98 ret = ioctl (sock, request, buffer);
pauledd7c242003-06-04 13:59:38 +000099 if (zserv_privs.change(ZPRIVS_LOWER))
100 zlog (NULL, LOG_ERR, "Can't lower privileges");
101
paul718e3742002-12-13 20:15:29 +0000102 if (ret < 0)
103 {
104 err = errno;
105 }
106 close (sock);
107
108 if (ret < 0)
109 {
110 errno = err;
111 return ret;
112 }
113 return 0;
114}
115#endif /* HAVE_IPV6 */
116
117/*
118 * get interface metric
119 * -- if value is not avaliable set -1
120 */
121void
122if_get_metric (struct interface *ifp)
123{
124#ifdef SIOCGIFMETRIC
125 struct ifreq ifreq;
126
127 ifreq_set_name (&ifreq, ifp);
128
129 if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
130 return;
131 ifp->metric = ifreq.ifr_metric;
132 if (ifp->metric == 0)
133 ifp->metric = 1;
134#else /* SIOCGIFMETRIC */
135 ifp->metric = -1;
136#endif /* SIOCGIFMETRIC */
137}
138
139/* get interface MTU */
140void
141if_get_mtu (struct interface *ifp)
142{
143 struct ifreq ifreq;
144
145 ifreq_set_name (&ifreq, ifp);
146
147#if defined(SIOCGIFMTU)
148 if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
149 {
150 zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
151 ifp->mtu = -1;
152 return;
153 }
154
155#ifdef SUNOS_5
156 ifp->mtu = ifreq.ifr_metric;
157#else
158 ifp->mtu = ifreq.ifr_mtu;
159#endif /* SUNOS_5 */
160
161#else
162 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
163 ifp->mtu = -1;
164#endif
165}
166
167#ifdef HAVE_NETLINK
168/* Interface address setting via netlink interface. */
169int
170if_set_prefix (struct interface *ifp, struct connected *ifc)
171{
172 return kernel_address_add_ipv4 (ifp, ifc);
173}
174
175/* Interface address is removed using netlink interface. */
176int
177if_unset_prefix (struct interface *ifp, struct connected *ifc)
178{
179 return kernel_address_delete_ipv4 (ifp, ifc);
180}
181#else /* ! HAVE_NETLINK */
182#ifdef HAVE_IFALIASREQ
183/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
184 has ifaliasreq structure. */
185int
186if_set_prefix (struct interface *ifp, struct connected *ifc)
187{
188 int ret;
189 struct ifaliasreq addreq;
190 struct sockaddr_in addr;
191 struct sockaddr_in mask;
192 struct prefix_ipv4 *p;
193
194 p = (struct prefix_ipv4 *) ifc->address;
195
196 memset (&addreq, 0, sizeof addreq);
197 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
198
199 memset (&addr, 0, sizeof (struct sockaddr_in));
200 addr.sin_addr = p->prefix;
201 addr.sin_family = p->family;
202#ifdef HAVE_SIN_LEN
203 addr.sin_len = sizeof (struct sockaddr_in);
204#endif
205 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
206
207 memset (&mask, 0, sizeof (struct sockaddr_in));
208 masklen2ip (p->prefixlen, &mask.sin_addr);
209 mask.sin_family = p->family;
210#ifdef HAVE_SIN_LEN
211 mask.sin_len = sizeof (struct sockaddr_in);
212#endif
213 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
214
215 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
216 if (ret < 0)
217 return ret;
218 return 0;
219}
220
221/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
222 has ifaliasreq structure. */
223int
224if_unset_prefix (struct interface *ifp, struct connected *ifc)
225{
226 int ret;
227 struct ifaliasreq addreq;
228 struct sockaddr_in addr;
229 struct sockaddr_in mask;
230 struct prefix_ipv4 *p;
231
232 p = (struct prefix_ipv4 *)ifc->address;
233
234 memset (&addreq, 0, sizeof addreq);
235 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
236
237 memset (&addr, 0, sizeof (struct sockaddr_in));
238 addr.sin_addr = p->prefix;
239 addr.sin_family = p->family;
240#ifdef HAVE_SIN_LEN
241 addr.sin_len = sizeof (struct sockaddr_in);
242#endif
243 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
244
245 memset (&mask, 0, sizeof (struct sockaddr_in));
246 masklen2ip (p->prefixlen, &mask.sin_addr);
247 mask.sin_family = p->family;
248#ifdef HAVE_SIN_LEN
249 mask.sin_len = sizeof (struct sockaddr_in);
250#endif
251 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
252
253 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
254 if (ret < 0)
255 return ret;
256 return 0;
257}
258#else
259/* Set up interface's address, netmask (and broadcas? ). Linux or
260 Solaris uses ifname:number semantics to set IP address aliases. */
261int
262if_set_prefix (struct interface *ifp, struct connected *ifc)
263{
264 int ret;
265 struct ifreq ifreq;
266 struct sockaddr_in addr;
267 struct sockaddr_in broad;
268 struct sockaddr_in mask;
269 struct prefix_ipv4 ifaddr;
270 struct prefix_ipv4 *p;
271
272 p = (struct prefix_ipv4 *) ifc->address;
273
274 ifaddr = *p;
275
276 ifreq_set_name (&ifreq, ifp);
277
278 addr.sin_addr = p->prefix;
279 addr.sin_family = p->family;
280 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
281 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
282 if (ret < 0)
283 return ret;
284
285 /* We need mask for make broadcast addr. */
286 masklen2ip (p->prefixlen, &mask.sin_addr);
287
288 if (if_is_broadcast (ifp))
289 {
290 apply_mask_ipv4 (&ifaddr);
291 addr.sin_addr = ifaddr.prefix;
292
293 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
294 broad.sin_family = p->family;
295
296 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
297 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
298 if (ret < 0)
299 return ret;
300 }
301
302 mask.sin_family = p->family;
303#ifdef SUNOS_5
304 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
305#else
306 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
307#endif /* SUNOS5 */
308 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
309 if (ret < 0)
310 return ret;
311
312 return 0;
313}
314
315/* Set up interface's address, netmask (and broadcas? ). Linux or
316 Solaris uses ifname:number semantics to set IP address aliases. */
317int
318if_unset_prefix (struct interface *ifp, struct connected *ifc)
319{
320 int ret;
321 struct ifreq ifreq;
322 struct sockaddr_in addr;
323 struct prefix_ipv4 *p;
324
325 p = (struct prefix_ipv4 *) ifc->address;
326
327 ifreq_set_name (&ifreq, ifp);
328
329 memset (&addr, 0, sizeof (struct sockaddr_in));
330 addr.sin_family = p->family;
331 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
332 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
333 if (ret < 0)
334 return ret;
335
336 return 0;
337}
338#endif /* HAVE_IFALIASREQ */
339#endif /* HAVE_NETLINK */
340
341/* get interface flags */
342void
343if_get_flags (struct interface *ifp)
344{
345 int ret;
346 struct ifreq ifreq;
347
348 ifreq_set_name (&ifreq, ifp);
349
350 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
351 if (ret < 0)
352 {
353 perror ("ioctl");
354 return;
355 }
356
357 ifp->flags = ifreq.ifr_flags & 0x0000ffff;
358}
359
360/* Set interface flags */
361int
362if_set_flags (struct interface *ifp, unsigned long flags)
363{
364 int ret;
365 struct ifreq ifreq;
366
367 ifreq_set_name (&ifreq, ifp);
368
369 ifreq.ifr_flags = ifp->flags;
370 ifreq.ifr_flags |= flags;
371
372 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
373
374 if (ret < 0)
375 {
376 zlog_info ("can't set interface flags");
377 return ret;
378 }
379 return 0;
380}
381
382/* Unset interface's flag. */
383int
384if_unset_flags (struct interface *ifp, unsigned long flags)
385{
386 int ret;
387 struct ifreq ifreq;
388
389 ifreq_set_name (&ifreq, ifp);
390
391 ifreq.ifr_flags = ifp->flags;
392 ifreq.ifr_flags &= ~flags;
393
394 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
395
396 if (ret < 0)
397 {
398 zlog_info ("can't unset interface flags");
399 return ret;
400 }
401 return 0;
402}
403
404#ifdef HAVE_IPV6
405
406#ifdef LINUX_IPV6
407#ifndef _LINUX_IN6_H
408/* linux/include/net/ipv6.h */
409struct in6_ifreq
410{
411 struct in6_addr ifr6_addr;
412 u_int32_t ifr6_prefixlen;
413 int ifr6_ifindex;
414};
415#endif /* _LINUX_IN6_H */
416
417/* Interface's address add/delete functions. */
418int
419if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
420{
421 int ret;
422 struct prefix_ipv6 *p;
423 struct in6_ifreq ifreq;
424
425 p = (struct prefix_ipv6 *) ifc->address;
426
427 memset (&ifreq, 0, sizeof (struct in6_ifreq));
428
429 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
430 ifreq.ifr6_ifindex = ifp->ifindex;
431 ifreq.ifr6_prefixlen = p->prefixlen;
432
433 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
434
435 return ret;
436}
437
438int
439if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
440{
441 int ret;
442 struct prefix_ipv6 *p;
443 struct in6_ifreq ifreq;
444
445 p = (struct prefix_ipv6 *) ifc->address;
446
447 memset (&ifreq, 0, sizeof (struct in6_ifreq));
448
449 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
450 ifreq.ifr6_ifindex = ifp->ifindex;
451 ifreq.ifr6_prefixlen = p->prefixlen;
452
453 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
454
455 return ret;
456}
457#else /* LINUX_IPV6 */
458#ifdef HAVE_IN6_ALIASREQ
459#ifndef ND6_INFINITE_LIFETIME
460#define ND6_INFINITE_LIFETIME 0xffffffffL
461#endif /* ND6_INFINITE_LIFETIME */
462int
463if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
464{
465 int ret;
466 struct in6_aliasreq addreq;
467 struct sockaddr_in6 addr;
468 struct sockaddr_in6 mask;
469 struct prefix_ipv6 *p;
470
471 p = (struct prefix_ipv6 * ) ifc->address;
472
473 memset (&addreq, 0, sizeof addreq);
474 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
475
476 memset (&addr, 0, sizeof (struct sockaddr_in6));
477 addr.sin6_addr = p->prefix;
478 addr.sin6_family = p->family;
479#ifdef HAVE_SIN_LEN
480 addr.sin6_len = sizeof (struct sockaddr_in6);
481#endif
482 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
483
484 memset (&mask, 0, sizeof (struct sockaddr_in6));
485 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
486 mask.sin6_family = p->family;
487#ifdef HAVE_SIN_LEN
488 mask.sin6_len = sizeof (struct sockaddr_in6);
489#endif
490 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
491
hasso726f9b22003-05-25 21:04:54 +0000492#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000493 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
494 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000495#endif
paul718e3742002-12-13 20:15:29 +0000496
497 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
498 if (ret < 0)
499 return ret;
500 return 0;
501}
502
503int
504if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
505{
506 int ret;
507 struct in6_aliasreq addreq;
508 struct sockaddr_in6 addr;
509 struct sockaddr_in6 mask;
510 struct prefix_ipv6 *p;
511
512 p = (struct prefix_ipv6 *) ifc->address;
513
514 memset (&addreq, 0, sizeof addreq);
515 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
516
517 memset (&addr, 0, sizeof (struct sockaddr_in6));
518 addr.sin6_addr = p->prefix;
519 addr.sin6_family = p->family;
520#ifdef HAVE_SIN_LEN
521 addr.sin6_len = sizeof (struct sockaddr_in6);
522#endif
523 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
524
525 memset (&mask, 0, sizeof (struct sockaddr_in6));
526 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
527 mask.sin6_family = p->family;
528#ifdef HAVE_SIN_LEN
529 mask.sin6_len = sizeof (struct sockaddr_in6);
530#endif
531 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
532
hasso726f9b22003-05-25 21:04:54 +0000533#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000534 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
535 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000536#endif
paul718e3742002-12-13 20:15:29 +0000537
538 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
539 if (ret < 0)
540 return ret;
541 return 0;
542}
543#else
544int
545if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
546{
547 return 0;
548}
549
550int
551if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
552{
553 return 0;
554}
555#endif /* HAVE_IN6_ALIASREQ */
556
557#endif /* LINUX_IPV6 */
558
559#endif /* HAVE_IPV6 */