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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "sockunion.h"
30 #include "connected.h"
32 #include "zebra_memory.h"
37 #include "lib_errors.h"
39 #include "zebra/interface.h"
40 #include "zebra/ioctl_solaris.h"
41 #include "zebra/rib.h"
44 static int if_get_addr(struct interface
*, struct sockaddr
*, const char *);
45 static void interface_info_ioctl(struct interface
*);
46 extern struct zebra_privs_t zserv_privs
;
48 static int interface_list_ioctl(int af
)
55 struct lifreq
*lifreq
;
56 struct lifconf lifconf
;
57 struct interface
*ifp
;
59 size_t needed
, lastneeded
= 0;
62 frr_elevate_privs(&zserv_privs
) {
63 sock
= socket(af
, SOCK_DGRAM
, 0);
67 zlog_warn("Can't make %s socket stream: %s",
68 (af
== AF_INET
? "AF_INET" : "AF_INET6"),
69 safe_strerror(errno
));
74 frr_elevate_privs(&zserv_privs
) {
75 lifn
.lifn_family
= af
;
76 lifn
.lifn_flags
= LIFC_NOXMIT
;
77 /* we want NOXMIT interfaces too */
78 ret
= ioctl(sock
, SIOCGLIFNUM
, &lifn
);
82 zlog_warn("interface_list_ioctl: SIOCGLIFNUM failed %s",
83 safe_strerror(errno
));
87 ifnum
= lifn
.lifn_count
;
90 * When calculating the buffer size needed, add a small number
91 * of interfaces to those we counted. We do this to capture
92 * the interface status of potential interfaces which may have
93 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
95 needed
= (ifnum
+ 4) * sizeof(struct lifreq
);
96 if (needed
> lastneeded
|| needed
< lastneeded
/ 2) {
98 XFREE(MTYPE_TMP
, buf
);
99 buf
= XMALLOC(MTYPE_TMP
, needed
);
103 lifconf
.lifc_family
= af
;
104 lifconf
.lifc_flags
= LIFC_NOXMIT
;
105 lifconf
.lifc_len
= needed
;
106 lifconf
.lifc_buf
= buf
;
108 frr_elevate_privs(&zserv_privs
) {
109 ret
= ioctl(sock
, SIOCGLIFCONF
, &lifconf
);
114 goto calculate_lifc_len
;
116 zlog_warn("SIOCGLIFCONF: %s", safe_strerror(errno
));
120 /* Allocate interface. */
121 lifreq
= lifconf
.lifc_req
;
123 for (n
= 0; n
< lifconf
.lifc_len
; n
+= sizeof(struct lifreq
)) {
124 /* we treat Solaris logical interfaces as addresses, because
126 * how PF_ROUTE on Solaris treats them. Hence we can not
128 * the lifreq_name to get the ifp. We need to normalise the
130 * before attempting get.
132 * Solaris logical interface names are in the form of:
133 * <interface name>:<logical interface id>
135 unsigned int normallen
= 0;
138 /* We should exclude ~IFF_UP interfaces, as we'll find out about
140 * coming up later through RTM_NEWADDR message on the route
143 if (if_get_flags_direct(lifreq
->lifr_name
, &lifflags
,
144 lifreq
->lifr_addr
.ss_family
)
145 || !CHECK_FLAG(lifflags
, IFF_UP
)) {
150 /* Find the normalised name */
151 while ((normallen
< sizeof(lifreq
->lifr_name
))
152 && (*(lifreq
->lifr_name
+ normallen
) != '\0')
153 && (*(lifreq
->lifr_name
+ normallen
) != ':'))
156 ifp
= if_get_by_name(lifreq
->lifr_name
, VRF_DEFAULT
, 0);
158 if (lifreq
->lifr_addr
.ss_family
== AF_INET
)
159 ifp
->flags
|= IFF_IPV4
;
161 if (lifreq
->lifr_addr
.ss_family
== AF_INET6
) {
162 ifp
->flags
|= IFF_IPV6
;
167 interface_info_ioctl(ifp
);
169 /* If a logical interface pass the full name so it can be
170 * as a label on the address
172 if (*(lifreq
->lifr_name
+ normallen
) != '\0')
173 if_get_addr(ifp
, (struct sockaddr
*)&lifreq
->lifr_addr
,
176 if_get_addr(ifp
, (struct sockaddr
*)&lifreq
->lifr_addr
,
179 /* Poke the interface flags. Lets IFF_UP mangling kick in */
180 if_flags_update(ifp
, ifp
->flags
);
187 XFREE(MTYPE_TMP
, lifconf
.lifc_buf
);
191 /* Get interface's index by ioctl. */
192 static int if_get_index(struct interface
*ifp
)
195 struct lifreq lifreq
;
197 lifreq_set_name(&lifreq
, ifp
->name
);
199 if (ifp
->flags
& IFF_IPV4
)
200 ret
= AF_IOCTL(AF_INET
, SIOCGLIFINDEX
, (caddr_t
)&lifreq
);
201 else if (ifp
->flags
& IFF_IPV6
)
202 ret
= AF_IOCTL(AF_INET6
, SIOCGLIFINDEX
, (caddr_t
)&lifreq
);
207 zlog_warn("SIOCGLIFINDEX(%s) failed", ifp
->name
);
211 /* OK we got interface index. */
213 if_set_index(ifp
, lifreq
.lifr_ifindex
);
215 if_set_index(ifp
, lifreq
.lifr_index
);
221 /* Interface address lookup by ioctl. This function only looks up
223 #define ADDRLEN(sa) \
224 (((sa)->sa_family == AF_INET ? sizeof(struct sockaddr_in) \
225 : sizeof(struct sockaddr_in6)))
227 #define SIN(s) ((struct sockaddr_in *)(s))
228 #define SIN6(s) ((struct sockaddr_in6 *)(s))
230 /* Retrieve address information for the given ifp */
231 static int if_get_addr(struct interface
*ifp
, struct sockaddr
*addr
,
235 struct lifreq lifreq
;
236 struct sockaddr_storage mask
, dest
;
237 char *dest_pnt
= NULL
;
238 uint8_t prefixlen
= 0;
242 /* Interface's name and address family.
243 * We need to use the logical interface name / label, if we've been
244 * given one, in order to get the right address
246 strncpy(lifreq
.lifr_name
, (label
? label
: ifp
->name
), IFNAMSIZ
);
248 /* Interface's address. */
249 memcpy(&lifreq
.lifr_addr
, addr
, ADDRLEN(addr
));
250 af
= addr
->sa_family
;
252 /* Point to point or broad cast address pointer init. */
255 if (AF_IOCTL(af
, SIOCGLIFDSTADDR
, (caddr_t
)&lifreq
) >= 0) {
256 memcpy(&dest
, &lifreq
.lifr_dstaddr
, ADDRLEN(addr
));
258 dest_pnt
= (char *)&(SIN(&dest
)->sin_addr
);
260 dest_pnt
= (char *)&(SIN6(&dest
)->sin6_addr
);
261 flags
= ZEBRA_IFA_PEER
;
265 ret
= if_ioctl(SIOCGLIFNETMASK
, (caddr_t
)&lifreq
);
268 if (errno
!= EADDRNOTAVAIL
) {
269 zlog_warn("SIOCGLIFNETMASK (%s) fail: %s",
270 ifp
->name
, safe_strerror(errno
));
275 memcpy(&mask
, &lifreq
.lifr_addr
, ADDRLEN(addr
));
277 prefixlen
= ip_masklen(SIN(&mask
)->sin_addr
);
279 && (if_ioctl(SIOCGLIFBRDADDR
, (caddr_t
)&lifreq
) >= 0)) {
280 memcpy(&dest
, &lifreq
.lifr_broadaddr
,
281 sizeof(struct sockaddr_in
));
282 dest_pnt
= (char *)&SIN(&dest
)->sin_addr
;
284 } else if (af
== AF_INET6
) {
285 if (if_ioctl_ipv6(SIOCGLIFSUBNET
, (caddr_t
)&lifreq
) < 0) {
286 if (ifp
->flags
& IFF_POINTOPOINT
)
287 prefixlen
= IPV6_MAX_BITLEN
;
289 zlog_warn("SIOCGLIFSUBNET (%s) fail: %s",
290 ifp
->name
, safe_strerror(errno
));
292 prefixlen
= lifreq
.lifr_addrlen
;
296 /* Set address to the interface. */
298 connected_add_ipv4(ifp
, flags
, &SIN(addr
)->sin_addr
, prefixlen
,
299 (struct in_addr
*)dest_pnt
, label
);
300 else if (af
== AF_INET6
)
301 connected_add_ipv6(ifp
, flags
, &SIN6(addr
)->sin6_addr
, NULL
,
307 /* Fetch interface information via ioctl(). */
308 static void interface_info_ioctl(struct interface
*ifp
)
316 /* Lookup all interface information. */
317 void interface_list(struct zebra_ns
*zns
)
319 if (zns
->ns_id
!= NS_DEFAULT
) {
320 zlog_warn("interface_list: ignore NS %u", zns
->ns_id
);
323 interface_list_ioctl(AF_INET
);
324 interface_list_ioctl(AF_INET6
);
325 interface_list_ioctl(AF_UNSPEC
);
328 struct connected
*if_lookup_linklocal(struct interface
*ifp
)
330 struct listnode
*node
;
331 struct connected
*ifc
;
336 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
337 if ((ifc
->address
->family
== AF_INET6
)
338 && (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)))