blob: ff253be1914875d3fc77ff90c653030480e7859c [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
pauld1724b62003-10-22 02:41:52 +0000367 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000368 ifreq_set_name (&ifreq, ifp);
369
370 ifreq.ifr_flags = ifp->flags;
371 ifreq.ifr_flags |= flags;
372
373 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
374
375 if (ret < 0)
376 {
377 zlog_info ("can't set interface flags");
378 return ret;
379 }
380 return 0;
381}
382
383/* Unset interface's flag. */
384int
385if_unset_flags (struct interface *ifp, unsigned long flags)
386{
387 int ret;
388 struct ifreq ifreq;
389
pauld1724b62003-10-22 02:41:52 +0000390 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000391 ifreq_set_name (&ifreq, ifp);
392
393 ifreq.ifr_flags = ifp->flags;
394 ifreq.ifr_flags &= ~flags;
395
396 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
397
398 if (ret < 0)
399 {
400 zlog_info ("can't unset interface flags");
401 return ret;
402 }
403 return 0;
404}
405
406#ifdef HAVE_IPV6
407
408#ifdef LINUX_IPV6
409#ifndef _LINUX_IN6_H
410/* linux/include/net/ipv6.h */
411struct in6_ifreq
412{
413 struct in6_addr ifr6_addr;
414 u_int32_t ifr6_prefixlen;
415 int ifr6_ifindex;
416};
417#endif /* _LINUX_IN6_H */
418
419/* Interface's address add/delete functions. */
420int
421if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
422{
423 int ret;
424 struct prefix_ipv6 *p;
425 struct in6_ifreq ifreq;
426
427 p = (struct prefix_ipv6 *) ifc->address;
428
429 memset (&ifreq, 0, sizeof (struct in6_ifreq));
430
431 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
432 ifreq.ifr6_ifindex = ifp->ifindex;
433 ifreq.ifr6_prefixlen = p->prefixlen;
434
435 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
436
437 return ret;
438}
439
440int
441if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
442{
443 int ret;
444 struct prefix_ipv6 *p;
445 struct in6_ifreq ifreq;
446
447 p = (struct prefix_ipv6 *) ifc->address;
448
449 memset (&ifreq, 0, sizeof (struct in6_ifreq));
450
451 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
452 ifreq.ifr6_ifindex = ifp->ifindex;
453 ifreq.ifr6_prefixlen = p->prefixlen;
454
455 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
456
457 return ret;
458}
459#else /* LINUX_IPV6 */
460#ifdef HAVE_IN6_ALIASREQ
461#ifndef ND6_INFINITE_LIFETIME
462#define ND6_INFINITE_LIFETIME 0xffffffffL
463#endif /* ND6_INFINITE_LIFETIME */
464int
465if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
466{
467 int ret;
468 struct in6_aliasreq addreq;
469 struct sockaddr_in6 addr;
470 struct sockaddr_in6 mask;
471 struct prefix_ipv6 *p;
472
473 p = (struct prefix_ipv6 * ) ifc->address;
474
475 memset (&addreq, 0, sizeof addreq);
476 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
477
478 memset (&addr, 0, sizeof (struct sockaddr_in6));
479 addr.sin6_addr = p->prefix;
480 addr.sin6_family = p->family;
481#ifdef HAVE_SIN_LEN
482 addr.sin6_len = sizeof (struct sockaddr_in6);
483#endif
484 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
485
486 memset (&mask, 0, sizeof (struct sockaddr_in6));
487 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
488 mask.sin6_family = p->family;
489#ifdef HAVE_SIN_LEN
490 mask.sin6_len = sizeof (struct sockaddr_in6);
491#endif
492 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
paul0e4f1902003-06-12 02:13:45 +0000493
494 addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
495 addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
paul718e3742002-12-13 20:15:29 +0000496
hasso726f9b22003-05-25 21:04:54 +0000497#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000498 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
499 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000500#endif
paul718e3742002-12-13 20:15:29 +0000501
502 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
503 if (ret < 0)
504 return ret;
505 return 0;
506}
507
508int
509if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
510{
511 int ret;
512 struct in6_aliasreq addreq;
513 struct sockaddr_in6 addr;
514 struct sockaddr_in6 mask;
515 struct prefix_ipv6 *p;
516
517 p = (struct prefix_ipv6 *) ifc->address;
518
519 memset (&addreq, 0, sizeof addreq);
520 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
521
522 memset (&addr, 0, sizeof (struct sockaddr_in6));
523 addr.sin6_addr = p->prefix;
524 addr.sin6_family = p->family;
525#ifdef HAVE_SIN_LEN
526 addr.sin6_len = sizeof (struct sockaddr_in6);
527#endif
528 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
529
530 memset (&mask, 0, sizeof (struct sockaddr_in6));
531 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
532 mask.sin6_family = p->family;
533#ifdef HAVE_SIN_LEN
534 mask.sin6_len = sizeof (struct sockaddr_in6);
535#endif
536 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
537
hasso726f9b22003-05-25 21:04:54 +0000538#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000539 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
540 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000541#endif
paul718e3742002-12-13 20:15:29 +0000542
543 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
544 if (ret < 0)
545 return ret;
546 return 0;
547}
548#else
549int
550if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
551{
552 return 0;
553}
554
555int
556if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
557{
558 return 0;
559}
560#endif /* HAVE_IN6_ALIASREQ */
561
562#endif /* LINUX_IPV6 */
563
564#endif /* HAVE_IPV6 */