VirtualBox

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

Last change on this file since 28742 was 28742, checked in by vboxsync, 15 years ago

Main/Solaris: added description for hxge.

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