VirtualBox

source: vbox/trunk/src/VBox/Main/solaris/NetIf-solaris.cpp@ 31611

Last change on this file since 31611 was 31337, checked in by vboxsync, 14 years ago

Main/Solaris: New DVD drive enumeration, works on Solaris 10 & Nevada. Removed now obsolete NSL_RESOLVED stuff on Solaris.

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

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