VirtualBox

source: vbox/trunk/src/VBox/Main/solaris/NetIfList-solaris.cpp@ 15456

Last change on this file since 15456 was 15456, checked in by vboxsync, 16 years ago

#3282 HostNetIf API: Solaris implementaion is functional including IP V6 addresses.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 KB
Line 
1/* $Id: NetIfList-solaris.cpp 15456 2008-12-14 10:28:37Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Solaris implementation.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#define LOG_GROUP LOG_GROUP_MAIN
37
38#include <iprt/err.h>
39#include <iprt/ctype.h>
40#include <list>
41
42#include "Logging.h"
43#include "HostNetworkInterfaceImpl.h"
44#include "netif.h"
45
46#ifdef VBOX_WITH_HOSTNETIF_API
47
48#include <map>
49#include <string>
50#include <fcntl.h>
51#include <unistd.h>
52#include <stropts.h>
53#include <errno.h>
54#include <limits.h>
55#include <stdio.h>
56#ifdef VBOX_SOLARIS_NSL_RESOLVED
57# include <libdevinfo.h>
58#endif
59#include <net/if.h>
60#include <sys/socket.h>
61#include <sys/sockio.h>
62#include <net/if_arp.h>
63#include <net/if.h>
64#include <sys/types.h>
65
66#include "DynLoadLibSolaris.h"
67
68static void vboxSolarisAddHostIface(char *pszIface, int Instance, void *pvHostNetworkInterfaceList)
69{
70 std::list<ComObjPtr <HostNetworkInterface> > *pList = (std::list<ComObjPtr <HostNetworkInterface> > *)pvHostNetworkInterfaceList;
71 Assert(pList);
72
73 typedef std::map <std::string, std::string> NICMap;
74 typedef std::pair <std::string, std::string> NICPair;
75 static NICMap SolarisNICMap;
76 if (SolarisNICMap.empty())
77 {
78 SolarisNICMap.insert(NICPair("afe", "ADMtek Centaur/Comet Fast Ethernet"));
79 SolarisNICMap.insert(NICPair("aggr", "Link Aggregation Interface"));
80 SolarisNICMap.insert(NICPair("bge", "Broadcom BCM57xx Gigabit Ethernet"));
81 SolarisNICMap.insert(NICPair("ce", "Cassini Gigabit Ethernet"));
82 SolarisNICMap.insert(NICPair("chxge", "Chelsio Ethernet"));
83 SolarisNICMap.insert(NICPair("dmfe", "Davicom Fast Ethernet"));
84 SolarisNICMap.insert(NICPair("dnet", "DEC 21040/41 21140 Ethernet"));
85 SolarisNICMap.insert(NICPair("e1000", "Intel PRO/1000 Gigabit Ethernet"));
86 SolarisNICMap.insert(NICPair("e1000g", "Intel PRO/1000 Gigabit Ethernet"));
87 SolarisNICMap.insert(NICPair("elx", "3COM EtherLink III Ethernet"));
88 SolarisNICMap.insert(NICPair("elxl", "3COM Ethernet"));
89 SolarisNICMap.insert(NICPair("eri", "eri Fast Ethernet"));
90 SolarisNICMap.insert(NICPair("ge", "GEM Gigabit Ethernet"));
91 SolarisNICMap.insert(NICPair("hme", "SUNW,hme Fast-Ethernet"));
92 SolarisNICMap.insert(NICPair("ipge", "PCI-E Gigabit Ethernet"));
93 SolarisNICMap.insert(NICPair("iprb", "Intel 82557/58/59 Ethernet"));
94 SolarisNICMap.insert(NICPair("mxfe", "Macronix 98715 Fast Ethernet"));
95 SolarisNICMap.insert(NICPair("nge", "Nvidia Gigabit Ethernet"));
96 SolarisNICMap.insert(NICPair("pcelx", "3COM EtherLink III PCMCIA Ethernet"));
97 SolarisNICMap.insert(NICPair("pcn", "AMD PCnet Ethernet"));
98 SolarisNICMap.insert(NICPair("qfe", "SUNW,qfe Quad Fast-Ethernet"));
99 SolarisNICMap.insert(NICPair("rge", "Realtek Gigabit Ethernet"));
100 SolarisNICMap.insert(NICPair("rtls", "Realtek 8139 Fast Ethernet"));
101 SolarisNICMap.insert(NICPair("skge", "SksKonnect Gigabit Ethernet"));
102 SolarisNICMap.insert(NICPair("spwr", "SMC EtherPower II 10/100 (9432) Ethernet"));
103 SolarisNICMap.insert(NICPair("vnic", "Virtual Network Interface Ethernet"));
104 SolarisNICMap.insert(NICPair("xge", "Neterior Xframe Gigabit Ethernet"));
105 SolarisNICMap.insert(NICPair("xge", "Neterior Xframe 10Gigabit Ethernet"));
106 }
107
108 /*
109 * Try picking up description from our NIC map.
110 */
111 char szNICInstance[128];
112 RTStrPrintf(szNICInstance, sizeof(szNICInstance), "%s%d", pszIface, Instance);
113 char szNICDesc[256];
114 std::string Description = SolarisNICMap[pszIface];
115 if (Description != "")
116 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - %s", szNICInstance, Description.c_str());
117 else
118 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - Ethernet", szNICInstance);
119
120 /*
121 * Try to get IP V4 address and netmask as well as Ethernet address.
122 */
123 NETIFINFO Info;
124 memset(&Info, 0, sizeof(Info));
125 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
126 if (Sock > 0)
127 {
128 struct lifreq IfReq;
129 strcpy(IfReq.lifr_name, szNICInstance);
130 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
131 {
132 memcpy(Info.IPAddress.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
133 sizeof(Info.IPAddress.au8));
134 struct arpreq ArpReq;
135 memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
136
137 /*
138 * We might fail if the interface has not been assigned an IP address.
139 * That doesn't matter; as long as it's plumbed we can pick it up.
140 * But, if it has not acquired an IP address we cannot obtain it's MAC
141 * address this way, so we just use all zeros there.
142 */
143 if (ioctl(Sock, SIOCGARP, &ArpReq) >= 0)
144 {
145 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
146 }
147
148 }
149
150 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
151 {
152 memcpy(Info.IPNetMask.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
153 sizeof(Info.IPNetMask.au8));
154 }
155 if (ioctl(Sock, SIOCGLIFFLAGS, &IfReq) >= 0)
156 {
157 Info.enmStatus = IfReq.lifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
158 }
159 close(Sock);
160 }
161 /*
162 * Try to get IP V6 address and netmask.
163 */
164 Sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
165 if (Sock > 0)
166 {
167 struct lifreq IfReq;
168 strcpy(IfReq.lifr_name, szNICInstance); if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
169 {
170 memcpy(Info.IPv6Address.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
171 sizeof(Info.IPv6Address.au8));
172 }
173 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
174 {
175 memcpy(Info.IPv6NetMask.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
176 sizeof(Info.IPv6NetMask.au8));
177 }
178 close(Sock);
179 }
180
181 /*
182 * Construct UUID with interface name and the MAC address if available.
183 */
184 RTUUID Uuid;
185 RTUuidClear(&Uuid);
186 memcpy(&Uuid, szNICInstance, RT_MIN(strlen(szNICInstance), sizeof(Uuid)));
187 Uuid.Gen.u8ClockSeqHiAndReserved = (Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
188 Uuid.Gen.u16TimeHiAndVersion = (Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
189 Uuid.Gen.au8Node[0] = Info.MACAddress.au8[0];
190 Uuid.Gen.au8Node[1] = Info.MACAddress.au8[1];
191 Uuid.Gen.au8Node[2] = Info.MACAddress.au8[2];
192 Uuid.Gen.au8Node[3] = Info.MACAddress.au8[3];
193 Uuid.Gen.au8Node[4] = Info.MACAddress.au8[4];
194 Uuid.Gen.au8Node[5] = Info.MACAddress.au8[5];
195 Info.Uuid = Uuid;
196 Info.enmType = NETIF_T_ETHERNET;
197
198 ComObjPtr<HostNetworkInterface> IfObj;
199 IfObj.createObject();
200 if (SUCCEEDED(IfObj->init(Bstr(szNICDesc), &Info)))
201 pList->push_back(IfObj);
202}
203
204static boolean_t vboxSolarisAddLinkHostIface(const char *pszIface, void *pvHostNetworkInterfaceList)
205{
206 /*
207 * Clip off the zone instance number from the interface name (if any).
208 */
209 char szIfaceName[128];
210 strcpy(szIfaceName, pszIface);
211 char *pszColon = (char *)memchr(szIfaceName, ':', sizeof(szIfaceName));
212 if (pszColon)
213 *pszColon = '\0';
214
215 /*
216 * Get the instance number from the interface name, then clip it off.
217 */
218 int cbInstance = 0;
219 int cbIface = strlen(szIfaceName);
220 const char *pszEnd = pszIface + cbIface - 1;
221 for (int i = 0; i < cbIface - 1; i++)
222 {
223 if (!RT_C_IS_DIGIT(*pszEnd))
224 break;
225 cbInstance++;
226 pszEnd--;
227 }
228
229 int Instance = atoi(pszEnd + 1);
230 strncpy(szIfaceName, pszIface, cbIface - cbInstance);
231 szIfaceName[cbIface - cbInstance] = '\0';
232
233 /*
234 * Add the interface.
235 */
236 vboxSolarisAddHostIface(szIfaceName, Instance, pvHostNetworkInterfaceList);
237
238 /*
239 * Continue walking...
240 */
241 return _B_FALSE;
242}
243
244static bool vboxSolarisSortNICList(const ComObjPtr <HostNetworkInterface> Iface1, const ComObjPtr <HostNetworkInterface> Iface2)
245{
246 Bstr Iface1Str;
247 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
248
249 Bstr Iface2Str;
250 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
251
252 return Iface1Str < Iface2Str;
253}
254
255static bool vboxSolarisSameNIC(const ComObjPtr <HostNetworkInterface> Iface1, const ComObjPtr <HostNetworkInterface> Iface2)
256{
257 Bstr Iface1Str;
258 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
259
260 Bstr Iface2Str;
261 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
262
263 return (Iface1Str == Iface2Str);
264}
265
266# ifdef VBOX_SOLARIS_NSL_RESOLVED
267static int vboxSolarisAddPhysHostIface(di_node_t Node, di_minor_t Minor, void *pvHostNetworkInterfaceList)
268{
269 /*
270 * Skip aggregations.
271 */
272 if (!strcmp(di_driver_name(Node), "aggr"))
273 return DI_WALK_CONTINUE;
274
275 /*
276 * Skip softmacs.
277 */
278 if (!strcmp(di_driver_name(Node), "softmac"))
279 return DI_WALK_CONTINUE;
280
281 vboxSolarisAddHostIface(di_driver_name(Node), di_instance(Node), pvHostNetworkInterfaceList);
282 return DI_WALK_CONTINUE;
283}
284# endif /* VBOX_SOLARIS_NSL_RESOLVED */
285
286int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
287{
288
289# ifdef VBOX_SOLARIS_NSL_RESOLVED
290
291 /*
292 * Use libdevinfo for determining all physical interfaces.
293 */
294 di_node_t Root;
295 Root = di_init("/", DINFOCACHE);
296 if (Root != DI_NODE_NIL)
297 {
298 di_walk_minor(Root, DDI_NT_NET, 0, &list, vboxSolarisAddPhysHostIface);
299 di_fini(Root);
300 }
301
302 /*
303 * Use libdlpi for determining all DLPI interfaces.
304 */
305 if (VBoxSolarisLibDlpiFound())
306 g_pfnLibDlpiWalk(vboxSolarisAddLinkHostIface, &list, 0);
307
308# endif /* VBOX_SOLARIS_NSL_RESOLVED */
309
310 /*
311 * This gets only the list of all plumbed logical interfaces.
312 * This is needed for zones which cannot access the device tree
313 * and in this case we just let them use the list of plumbed interfaces
314 * on the zone.
315 */
316 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
317 if (Sock > 0)
318 {
319 struct lifnum IfNum;
320 memset(&IfNum, 0, sizeof(IfNum));
321 IfNum.lifn_family = AF_INET;
322 int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
323 if (!rc)
324 {
325 struct lifreq Ifaces[24];
326 struct lifconf IfConfig;
327 memset(&IfConfig, 0, sizeof(IfConfig));
328 IfConfig.lifc_family = AF_INET;
329 IfConfig.lifc_len = sizeof(Ifaces);
330 IfConfig.lifc_buf = (caddr_t)&(Ifaces[0]);
331 rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
332 if (!rc)
333 {
334 for (int i = 0; i < IfNum.lifn_count; i++)
335 {
336 /*
337 * Skip loopback interfaces.
338 */
339 if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
340 continue;
341
342#if 0
343 rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
344 if (rc >= 0)
345 {
346 memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
347 sizeof(Info.IPAddress.au8));
348 // SIOCGLIFNETMASK
349 struct arpreq ArpReq;
350 memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
351
352 /*
353 * We might fail if the interface has not been assigned an IP address.
354 * That doesn't matter; as long as it's plumbed we can pick it up.
355 * But, if it has not acquired an IP address we cannot obtain it's MAC
356 * address this way, so we just use all zeros there.
357 */
358 rc = ioctl(Sock, SIOCGARP, &ArpReq);
359 if (rc >= 0)
360 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
361
362 char szNICDesc[LIFNAMSIZ + 256];
363 char *pszIface = Ifaces[i].lifr_name;
364 strcpy(szNICDesc, pszIface);
365
366 vboxSolarisAddLinkHostIface(pszIface, &list);
367 }
368#endif
369
370 char *pszIface = Ifaces[i].lifr_name;
371 vboxSolarisAddLinkHostIface(pszIface, &list);
372 }
373 }
374 }
375 close(Sock);
376 }
377
378 /*
379 * Weed out duplicates caused by dlpi_walk inconsistencies across Nevadas.
380 */
381 list.sort(vboxSolarisSortNICList);
382 list.unique(vboxSolarisSameNIC);
383
384 return VINF_SUCCESS;
385}
386#else
387int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
388{
389 return VERR_NOT_IMPLEMENTED;
390}
391#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette