2 * Interface looking up by ioctl () on Solaris.
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
26 #include "sockunion.h"
29 #include "connected.h"
36 #include "zebra/interface.h"
37 #include "zebra/ioctl_solaris.h"
38 #include "zebra/rib.h"
40 static int if_get_addr (struct interface *, struct sockaddr *, const char *);
41 static void interface_info_ioctl (struct interface *);
42 extern struct zebra_privs_t zserv_privs;
45 interface_list_ioctl (int af)
52 struct lifreq *lifreq;
53 struct lifconf lifconf;
54 struct interface *ifp;
57 size_t needed, lastneeded = 0;
60 if (zserv_privs.change(ZPRIVS_RAISE))
61 zlog (NULL, LOG_ERR, "Can't raise privileges");
63 sock = socket (af, SOCK_DGRAM, 0);
66 zlog_warn ("Can't make %s socket stream: %s",
67 (af == AF_INET ? "AF_INET" : "AF_INET6"), safe_strerror (errno));
69 if (zserv_privs.change(ZPRIVS_LOWER))
70 zlog (NULL, LOG_ERR, "Can't lower privileges");
75 calculate_lifc_len: /* must hold privileges to enter here */
76 lifn.lifn_family = af;
77 lifn.lifn_flags = LIFC_NOXMIT; /* we want NOXMIT interfaces too */
78 ret = ioctl (sock, SIOCGLIFNUM, &lifn);
81 if (zserv_privs.change(ZPRIVS_LOWER))
82 zlog (NULL, LOG_ERR, "Can't lower privileges");
86 zlog_warn ("interface_list_ioctl: SIOCGLIFNUM failed %s",
87 safe_strerror (save_errno));
91 ifnum = lifn.lifn_count;
94 * When calculating the buffer size needed, add a small number
95 * of interfaces to those we counted. We do this to capture
96 * the interface status of potential interfaces which may have
97 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
99 needed = (ifnum + 4) * sizeof (struct lifreq);
100 if (needed > lastneeded || needed < lastneeded / 2)
103 XFREE (MTYPE_TMP, buf);
104 if ((buf = XMALLOC (MTYPE_TMP, needed)) == NULL)
106 zlog_warn ("interface_list_ioctl: malloc failed");
113 lifconf.lifc_family = af;
114 lifconf.lifc_flags = LIFC_NOXMIT;
115 lifconf.lifc_len = needed;
116 lifconf.lifc_buf = buf;
118 if (zserv_privs.change(ZPRIVS_RAISE))
119 zlog (NULL, LOG_ERR, "Can't raise privileges");
121 ret = ioctl (sock, SIOCGLIFCONF, &lifconf);
126 goto calculate_lifc_len; /* deliberately hold privileges */
128 zlog_warn ("SIOCGLIFCONF: %s", safe_strerror (errno));
130 if (zserv_privs.change(ZPRIVS_LOWER))
131 zlog (NULL, LOG_ERR, "Can't lower privileges");
136 if (zserv_privs.change(ZPRIVS_LOWER))
137 zlog (NULL, LOG_ERR, "Can't lower privileges");
139 /* Allocate interface. */
140 lifreq = lifconf.lifc_req;
142 for (n = 0; n < lifconf.lifc_len; n += sizeof (struct lifreq))
144 /* we treat Solaris logical interfaces as addresses, because that is
145 * how PF_ROUTE on Solaris treats them. Hence we can not directly use
146 * the lifreq_name to get the ifp. We need to normalise the name
147 * before attempting get.
149 * Solaris logical interface names are in the form of:
150 * <interface name>:<logical interface id>
152 unsigned int normallen = 0;
155 /* We should exclude ~IFF_UP interfaces, as we'll find out about them
156 * coming up later through RTM_NEWADDR message on the route socket.
158 if (if_get_flags_direct (lifreq->lifr_name, &lifflags,
159 lifreq->lifr_addr.ss_family)
160 || !CHECK_FLAG (lifflags, IFF_UP))
166 /* Find the normalised name */
167 while ( (normallen < sizeof(lifreq->lifr_name))
168 && ( *(lifreq->lifr_name + normallen) != '\0')
169 && ( *(lifreq->lifr_name + normallen) != ':') )
172 ifp = if_get_by_name_len(lifreq->lifr_name, normallen);
174 if (lifreq->lifr_addr.ss_family == AF_INET)
175 ifp->flags |= IFF_IPV4;
177 if (lifreq->lifr_addr.ss_family == AF_INET6)
180 ifp->flags |= IFF_IPV6;
184 #endif /* HAVE_IPV6 */
189 interface_info_ioctl (ifp);
191 /* If a logical interface pass the full name so it can be
192 * as a label on the address
194 if ( *(lifreq->lifr_name + normallen) != '\0')
195 if_get_addr (ifp, (struct sockaddr *) &lifreq->lifr_addr,
198 if_get_addr (ifp, (struct sockaddr *) &lifreq->lifr_addr, NULL);
200 /* Poke the interface flags. Lets IFF_UP mangling kick in */
201 if_flags_update (ifp, ifp->flags);
208 XFREE (MTYPE_TMP, lifconf.lifc_buf);
212 /* Get interface's index by ioctl. */
214 if_get_index (struct interface *ifp)
217 struct lifreq lifreq;
219 lifreq_set_name (&lifreq, ifp->name);
221 if (ifp->flags & IFF_IPV4)
222 ret = AF_IOCTL (AF_INET, SIOCGLIFINDEX, (caddr_t) & lifreq);
223 else if (ifp->flags & IFF_IPV6)
224 ret = AF_IOCTL (AF_INET6, SIOCGLIFINDEX, (caddr_t) & lifreq);
230 zlog_warn ("SIOCGLIFINDEX(%s) failed", ifp->name);
234 /* OK we got interface index. */
236 ifp->ifindex = lifreq.lifr_ifindex;
238 ifp->ifindex = lifreq.lifr_index;
245 /* Interface address lookup by ioctl. This function only looks up
247 #define ADDRLEN(sa) (((sa)->sa_family == AF_INET ? \
248 sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)))
250 #define SIN(s) ((struct sockaddr_in *)(s))
251 #define SIN6(s) ((struct sockaddr_in6 *)(s))
253 /* Retrieve address information for the given ifp */
255 if_get_addr (struct interface *ifp, struct sockaddr *addr, const char *label)
258 struct lifreq lifreq;
259 struct sockaddr_storage mask, dest;
260 char *dest_pnt = NULL;
261 u_char prefixlen = 0;
265 /* Interface's name and address family.
266 * We need to use the logical interface name / label, if we've been
267 * given one, in order to get the right address
269 strncpy (lifreq.lifr_name, (label ? label : ifp->name), IFNAMSIZ);
271 /* Interface's address. */
272 memcpy (&lifreq.lifr_addr, addr, ADDRLEN (addr));
273 af = addr->sa_family;
275 /* Point to point or broad cast address pointer init. */
278 if (AF_IOCTL (af, SIOCGLIFDSTADDR, (caddr_t) & lifreq) >= 0)
280 memcpy (&dest, &lifreq.lifr_dstaddr, ADDRLEN (addr));
282 dest_pnt = (char *) &(SIN (&dest)->sin_addr);
284 dest_pnt = (char *) &(SIN6 (&dest)->sin6_addr);
285 flags = ZEBRA_IFA_PEER;
290 ret = if_ioctl (SIOCGLIFNETMASK, (caddr_t) & lifreq);
294 if (errno != EADDRNOTAVAIL)
296 zlog_warn ("SIOCGLIFNETMASK (%s) fail: %s", ifp->name,
297 safe_strerror (errno));
302 memcpy (&mask, &lifreq.lifr_addr, ADDRLEN (addr));
304 prefixlen = ip_masklen (SIN (&mask)->sin_addr);
305 if (!dest_pnt && (if_ioctl (SIOCGLIFBRDADDR, (caddr_t) & lifreq) >= 0))
307 memcpy (&dest, &lifreq.lifr_broadaddr, sizeof (struct sockaddr_in));
308 dest_pnt = (char *) &SIN (&dest)->sin_addr;
312 else if (af == AF_INET6)
314 if (if_ioctl_ipv6 (SIOCGLIFSUBNET, (caddr_t) & lifreq) < 0)
316 if (ifp->flags & IFF_POINTOPOINT)
317 prefixlen = IPV6_MAX_BITLEN;
319 zlog_warn ("SIOCGLIFSUBNET (%s) fail: %s",
320 ifp->name, safe_strerror (errno));
324 prefixlen = lifreq.lifr_addrlen;
327 #endif /* HAVE_IPV6 */
329 /* Set address to the interface. */
331 connected_add_ipv4 (ifp, flags, &SIN (addr)->sin_addr, prefixlen,
332 (struct in_addr *) dest_pnt, label);
334 else if (af == AF_INET6)
335 connected_add_ipv6 (ifp, flags, &SIN6 (addr)->sin6_addr, prefixlen,
336 (struct in6_addr *) dest_pnt, label);
337 #endif /* HAVE_IPV6 */
342 /* Fetch interface information via ioctl(). */
344 interface_info_ioctl (struct interface *ifp)
352 /* Lookup all interface information. */
354 interface_list (struct zebra_vrf *zvrf)
356 if (zvrf->vrf_id != VRF_DEFAULT)
358 zlog_warn ("interface_list: ignore VRF %u", zvrf->vrf_id);
361 interface_list_ioctl (AF_INET);
362 interface_list_ioctl (AF_INET6);
363 interface_list_ioctl (AF_UNSPEC);
367 if_lookup_linklocal (struct interface *ifp)
370 struct listnode *node;
371 struct connected *ifc;
376 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc))
378 if ((ifc->address->family == AF_INET6) &&
379 (IN6_IS_ADDR_LINKLOCAL (&ifc->address->u.prefix6)))
382 #endif /* HAVE_IPV6 */