2 * Common ioctl functions.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
32 #include "zebra/rib.h"
34 #include "zebra/interface.h"
36 #ifdef HAVE_BSD_LINK_DETECT
37 #include <net/if_media.h>
38 #endif /* HAVE_BSD_LINK_DETECT*/
40 extern struct zebra_privs_t zserv_privs;
42 /* clear and set interface name string */
44 ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
46 strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ);
49 /* call ioctl system call */
51 if_ioctl (u_long request, caddr_t buffer)
57 if (zserv_privs.change(ZPRIVS_RAISE))
58 zlog (NULL, LOG_ERR, "Can't raise privileges");
59 sock = socket (AF_INET, SOCK_DGRAM, 0);
62 int save_errno = errno;
63 if (zserv_privs.change(ZPRIVS_LOWER))
64 zlog (NULL, LOG_ERR, "Can't lower privileges");
65 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
68 if ((ret = ioctl (sock, request, buffer)) < 0)
70 if (zserv_privs.change(ZPRIVS_LOWER))
71 zlog (NULL, LOG_ERR, "Can't lower privileges");
84 if_ioctl_ipv6 (u_long request, caddr_t buffer)
90 if (zserv_privs.change(ZPRIVS_RAISE))
91 zlog (NULL, LOG_ERR, "Can't raise privileges");
92 sock = socket (AF_INET6, SOCK_DGRAM, 0);
95 int save_errno = errno;
96 if (zserv_privs.change(ZPRIVS_LOWER))
97 zlog (NULL, LOG_ERR, "Can't lower privileges");
98 zlog_err("Cannot create IPv6 datagram socket: %s",
99 safe_strerror(save_errno));
103 if ((ret = ioctl (sock, request, buffer)) < 0)
105 if (zserv_privs.change(ZPRIVS_LOWER))
106 zlog (NULL, LOG_ERR, "Can't lower privileges");
116 #endif /* HAVE_IPV6 */
119 * get interface metric
120 * -- if value is not avaliable set -1
123 if_get_metric (struct interface *ifp)
128 ifreq_set_name (&ifreq, ifp);
130 if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
132 ifp->metric = ifreq.ifr_metric;
133 if (ifp->metric == 0)
135 #else /* SIOCGIFMETRIC */
137 #endif /* SIOCGIFMETRIC */
140 /* get interface MTU */
142 if_get_mtu (struct interface *ifp)
146 ifreq_set_name (&ifreq, ifp);
148 #if defined(SIOCGIFMTU)
149 if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
151 zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
152 ifp->mtu6 = ifp->mtu = -1;
157 ifp->mtu6 = ifp->mtu = ifreq.ifr_metric;
159 ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu;
163 zebra_interface_up_update(ifp);
166 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
167 ifp->mtu6 = ifp->mtu = -1;
172 /* Interface address setting via netlink interface. */
174 if_set_prefix (struct interface *ifp, struct connected *ifc)
176 return kernel_address_add_ipv4 (ifp, ifc);
179 /* Interface address is removed using netlink interface. */
181 if_unset_prefix (struct interface *ifp, struct connected *ifc)
183 return kernel_address_delete_ipv4 (ifp, ifc);
185 #else /* ! HAVE_NETLINK */
186 #ifdef HAVE_STRUCT_IFALIASREQ
187 /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
188 has ifaliasreq structure. */
190 if_set_prefix (struct interface *ifp, struct connected *ifc)
193 struct ifaliasreq addreq;
194 struct sockaddr_in addr;
195 struct sockaddr_in mask;
196 struct prefix_ipv4 *p;
198 p = (struct prefix_ipv4 *) ifc->address;
200 memset (&addreq, 0, sizeof addreq);
201 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
203 memset (&addr, 0, sizeof (struct sockaddr_in));
204 addr.sin_addr = p->prefix;
205 addr.sin_family = p->family;
206 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
207 addr.sin_len = sizeof (struct sockaddr_in);
209 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
211 memset (&mask, 0, sizeof (struct sockaddr_in));
212 masklen2ip (p->prefixlen, &mask.sin_addr);
213 mask.sin_family = p->family;
214 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
215 mask.sin_len = sizeof (struct sockaddr_in);
217 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
219 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
225 /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
226 has ifaliasreq structure. */
228 if_unset_prefix (struct interface *ifp, struct connected *ifc)
231 struct ifaliasreq addreq;
232 struct sockaddr_in addr;
233 struct sockaddr_in mask;
234 struct prefix_ipv4 *p;
236 p = (struct prefix_ipv4 *)ifc->address;
238 memset (&addreq, 0, sizeof addreq);
239 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
241 memset (&addr, 0, sizeof (struct sockaddr_in));
242 addr.sin_addr = p->prefix;
243 addr.sin_family = p->family;
244 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
245 addr.sin_len = sizeof (struct sockaddr_in);
247 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
249 memset (&mask, 0, sizeof (struct sockaddr_in));
250 masklen2ip (p->prefixlen, &mask.sin_addr);
251 mask.sin_family = p->family;
252 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
253 mask.sin_len = sizeof (struct sockaddr_in);
255 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
257 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
263 /* Set up interface's address, netmask (and broadcas? ). Linux or
264 Solaris uses ifname:number semantics to set IP address aliases. */
266 if_set_prefix (struct interface *ifp, struct connected *ifc)
270 struct sockaddr_in addr;
271 struct sockaddr_in broad;
272 struct sockaddr_in mask;
273 struct prefix_ipv4 ifaddr;
274 struct prefix_ipv4 *p;
276 p = (struct prefix_ipv4 *) ifc->address;
280 ifreq_set_name (&ifreq, ifp);
282 addr.sin_addr = p->prefix;
283 addr.sin_family = p->family;
284 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
285 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
289 /* We need mask for make broadcast addr. */
290 masklen2ip (p->prefixlen, &mask.sin_addr);
292 if (if_is_broadcast (ifp))
294 apply_mask_ipv4 (&ifaddr);
295 addr.sin_addr = ifaddr.prefix;
297 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
298 broad.sin_family = p->family;
300 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
301 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
306 mask.sin_family = p->family;
308 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
310 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
312 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
319 /* Set up interface's address, netmask (and broadcas? ). Linux or
320 Solaris uses ifname:number semantics to set IP address aliases. */
322 if_unset_prefix (struct interface *ifp, struct connected *ifc)
326 struct sockaddr_in addr;
327 struct prefix_ipv4 *p;
329 p = (struct prefix_ipv4 *) ifc->address;
331 ifreq_set_name (&ifreq, ifp);
333 memset (&addr, 0, sizeof (struct sockaddr_in));
334 addr.sin_family = p->family;
335 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
336 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
342 #endif /* HAVE_STRUCT_IFALIASREQ */
343 #endif /* HAVE_NETLINK */
345 /* get interface flags */
347 if_get_flags (struct interface *ifp)
351 #ifdef HAVE_BSD_LINK_DETECT
352 struct ifmediareq ifmr;
353 #endif /* HAVE_BSD_LINK_DETECT */
355 ifreq_set_name (&ifreq, ifp);
357 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
360 zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno));
363 #ifdef HAVE_BSD_LINK_DETECT /* Detect BSD link-state at start-up */
365 /* Per-default, IFF_RUNNING is held high, unless link-detect says
366 * otherwise - we abuse IFF_RUNNING inside zebra as a link-state flag,
367 * following practice on Linux and Solaris kernels
369 SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
371 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
373 (void) memset(&ifmr, 0, sizeof(ifmr));
374 strncpy (ifmr.ifm_name, ifp->name, IFNAMSIZ);
376 /* Seems not all interfaces implement this ioctl */
377 if (if_ioctl(SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
378 zlog_err("if_ioctl(SIOCGIFMEDIA) failed: %s", safe_strerror(errno));
379 else if (ifmr.ifm_status & IFM_AVALID) /* Link state is valid */
381 if (ifmr.ifm_status & IFM_ACTIVE)
382 SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
384 UNSET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
387 #endif /* HAVE_BSD_LINK_DETECT */
389 if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff));
392 /* Set interface flags */
394 if_set_flags (struct interface *ifp, uint64_t flags)
399 memset (&ifreq, 0, sizeof(struct ifreq));
400 ifreq_set_name (&ifreq, ifp);
402 ifreq.ifr_flags = ifp->flags;
403 ifreq.ifr_flags |= flags;
405 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
409 zlog_info ("can't set interface flags");
415 /* Unset interface's flag. */
417 if_unset_flags (struct interface *ifp, uint64_t flags)
422 memset (&ifreq, 0, sizeof(struct ifreq));
423 ifreq_set_name (&ifreq, ifp);
425 ifreq.ifr_flags = ifp->flags;
426 ifreq.ifr_flags &= ~flags;
428 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
432 zlog_info ("can't unset interface flags");
442 /* linux/include/net/ipv6.h */
445 struct in6_addr ifr6_addr;
446 u_int32_t ifr6_prefixlen;
449 #endif /* _LINUX_IN6_H */
451 /* Interface's address add/delete functions. */
453 if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
456 struct prefix_ipv6 *p;
457 struct in6_ifreq ifreq;
459 p = (struct prefix_ipv6 *) ifc->address;
461 memset (&ifreq, 0, sizeof (struct in6_ifreq));
463 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
464 ifreq.ifr6_ifindex = ifp->ifindex;
465 ifreq.ifr6_prefixlen = p->prefixlen;
467 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
473 if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
476 struct prefix_ipv6 *p;
477 struct in6_ifreq ifreq;
479 p = (struct prefix_ipv6 *) ifc->address;
481 memset (&ifreq, 0, sizeof (struct in6_ifreq));
483 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
484 ifreq.ifr6_ifindex = ifp->ifindex;
485 ifreq.ifr6_prefixlen = p->prefixlen;
487 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
491 #else /* LINUX_IPV6 */
492 #ifdef HAVE_STRUCT_IN6_ALIASREQ
493 #ifndef ND6_INFINITE_LIFETIME
494 #define ND6_INFINITE_LIFETIME 0xffffffffL
495 #endif /* ND6_INFINITE_LIFETIME */
497 if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
500 struct in6_aliasreq addreq;
501 struct sockaddr_in6 addr;
502 struct sockaddr_in6 mask;
503 struct prefix_ipv6 *p;
505 p = (struct prefix_ipv6 * ) ifc->address;
507 memset (&addreq, 0, sizeof addreq);
508 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
510 memset (&addr, 0, sizeof (struct sockaddr_in6));
511 addr.sin6_addr = p->prefix;
512 addr.sin6_family = p->family;
513 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
514 addr.sin6_len = sizeof (struct sockaddr_in6);
516 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
518 memset (&mask, 0, sizeof (struct sockaddr_in6));
519 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
520 mask.sin6_family = p->family;
521 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
522 mask.sin6_len = sizeof (struct sockaddr_in6);
524 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
526 addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
527 addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
529 #ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
530 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
531 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
534 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
541 if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
544 struct in6_aliasreq addreq;
545 struct sockaddr_in6 addr;
546 struct sockaddr_in6 mask;
547 struct prefix_ipv6 *p;
549 p = (struct prefix_ipv6 *) ifc->address;
551 memset (&addreq, 0, sizeof addreq);
552 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
554 memset (&addr, 0, sizeof (struct sockaddr_in6));
555 addr.sin6_addr = p->prefix;
556 addr.sin6_family = p->family;
557 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
558 addr.sin6_len = sizeof (struct sockaddr_in6);
560 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
562 memset (&mask, 0, sizeof (struct sockaddr_in6));
563 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
564 mask.sin6_family = p->family;
565 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
566 mask.sin6_len = sizeof (struct sockaddr_in6);
568 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
570 #ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
571 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
572 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
575 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
582 if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
588 if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
592 #endif /* HAVE_STRUCT_IN6_ALIASREQ */
594 #endif /* LINUX_IPV6 */
596 #endif /* HAVE_IPV6 */