VirtualBox

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

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

Main/solaris: Added some NIC descriptions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/* $Id: NetIf-solaris.cpp 37131 2011-05-18 11:43:37Z 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.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
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("atge", "Atheros/Attansic Gigabit Ethernet"));
65 SolarisNICMap.insert(NICPair("aggr", "Link Aggregation Interface"));
66 SolarisNICMap.insert(NICPair("bfe", "Broadcom BCM4401 Fast Ethernet"));
67 SolarisNICMap.insert(NICPair("bge", "Broadcom BCM57xx Gigabit Ethernet"));
68 SolarisNICMap.insert(NICPair("bnx", "Broadcom NetXtreme Gigabit Ethernet"));
69 SolarisNICMap.insert(NICPair("bnxe", "Broadcom NetXtreme II 10 Gigabit Ethernet"));
70 SolarisNICMap.insert(NICPair("ce", "Cassini Gigabit Ethernet"));
71 SolarisNICMap.insert(NICPair("chxge", "Chelsio Ethernet"));
72 SolarisNICMap.insert(NICPair("dmfe", "Davicom 9102 Fast Ethernet"));
73 SolarisNICMap.insert(NICPair("dnet", "DEC 21040/41 21140 Ethernet"));
74 SolarisNICMap.insert(NICPair("e1000", "Intel PRO/1000 Gigabit Ethernet"));
75 SolarisNICMap.insert(NICPair("e1000g", "Intel PRO/1000 Gigabit Ethernet"));
76 SolarisNICMap.insert(NICPair("elx", "3COM Etherlink III Ethernet"));
77 SolarisNICMap.insert(NICPair("elxl", "3COM Etherlink XL Ethernet"));
78 SolarisNICMap.insert(NICPair("eri", "eri Fast Ethernet"));
79 SolarisNICMap.insert(NICPair("ge", "GEM Gigabit Ethernet"));
80 SolarisNICMap.insert(NICPair("hme", "SUNW,hme Fast-Ethernet"));
81 SolarisNICMap.insert(NICPair("hxge", "Sun Blade 10 Gigabit Ethernet"));
82 SolarisNICMap.insert(NICPair("igb", "Intel 82575 PCI-E Gigabit Ethernet"));
83 SolarisNICMap.insert(NICPair("ipge", "PCI-E Gigabit Ethernet"));
84 SolarisNICMap.insert(NICPair("iprb", "Intel 82557/58/59 Ethernet"));
85 SolarisNICMap.insert(NICPair("ixgbe", "Intel 10 Gigabit PCI-E 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("sfe", "SiS900 Fast Ethernet"));
95 SolarisNICMap.insert(NICPair("skge", "SksKonnect Gigabit Ethernet"));
96 SolarisNICMap.insert(NICPair("spwr", "SMC EtherPower II 10/100 (9432) Ethernet"));
97 SolarisNICMap.insert(NICPair("vboxnet", "VirtualBox Host Ethernet"));
98 SolarisNICMap.insert(NICPair("vboxvnic_template", "VirtualBox Virtual Network Interface Template"));
99 SolarisNICMap.insert(NICPair("vlan", "Virtual LAN Ethernet"));
100 SolarisNICMap.insert(NICPair("vr", "VIA Rhine Fast Ethernet"));
101 SolarisNICMap.insert(NICPair("vnic", "Virtual Network Interface Ethernet"));
102 SolarisNICMap.insert(NICPair("xge", "Neterior Xframe 10Gigabit Ethernet"));
103 SolarisNICMap.insert(NICPair("yge", "Marvell Yukon 2 Fast Ethernet"));
104 }
105
106 /*
107 * Try picking up description from our NIC map.
108 */
109 char szNICInstance[128];
110 RTStrPrintf(szNICInstance, sizeof(szNICInstance), "%s%d", pszIface, Instance);
111 char szNICDesc[256];
112 std::string Description = SolarisNICMap[pszIface];
113 if (Description != "VirtualBox Host Ethernet")
114 {
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 else
121 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s", szNICInstance);
122
123 /*
124 * Try to get IP V4 address and netmask as well as Ethernet address.
125 */
126 NETIFINFO Info;
127 memset(&Info, 0, sizeof(Info));
128 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
129 if (Sock > 0)
130 {
131 struct lifreq IfReq;
132 strcpy(IfReq.lifr_name, szNICInstance);
133 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
134 {
135 memcpy(Info.IPAddress.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
136 sizeof(Info.IPAddress.au8));
137 struct arpreq ArpReq;
138 memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
139
140 /*
141 * We might fail if the interface has not been assigned an IP address.
142 * That doesn't matter; as long as it's plumbed we can pick it up.
143 * But, if it has not acquired an IP address we cannot obtain it's MAC
144 * address this way, so we just use all zeros there.
145 */
146 if (ioctl(Sock, SIOCGARP, &ArpReq) >= 0)
147 {
148 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
149 }
150
151 }
152
153 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
154 {
155 memcpy(Info.IPNetMask.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
156 sizeof(Info.IPNetMask.au8));
157 }
158 if (ioctl(Sock, SIOCGLIFFLAGS, &IfReq) >= 0)
159 {
160 Info.enmStatus = IfReq.lifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
161 }
162 close(Sock);
163 }
164 /*
165 * Try to get IP V6 address and netmask.
166 */
167 Sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
168 if (Sock > 0)
169 {
170 struct lifreq IfReq;
171 strcpy(IfReq.lifr_name, szNICInstance);
172 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
173 {
174 memcpy(Info.IPv6Address.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
175 sizeof(Info.IPv6Address.au8));
176 }
177 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
178 {
179 memcpy(Info.IPv6NetMask.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
180 sizeof(Info.IPv6NetMask.au8));
181 }
182 close(Sock);
183 }
184
185 /*
186 * Construct UUID with interface name and the MAC address if available.
187 */
188 RTUUID Uuid;
189 RTUuidClear(&Uuid);
190 memcpy(&Uuid, szNICInstance, RT_MIN(strlen(szNICInstance), sizeof(Uuid)));
191 Uuid.Gen.u8ClockSeqHiAndReserved = (Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
192 Uuid.Gen.u16TimeHiAndVersion = (Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
193 Uuid.Gen.au8Node[0] = Info.MACAddress.au8[0];
194 Uuid.Gen.au8Node[1] = Info.MACAddress.au8[1];
195 Uuid.Gen.au8Node[2] = Info.MACAddress.au8[2];
196 Uuid.Gen.au8Node[3] = Info.MACAddress.au8[3];
197 Uuid.Gen.au8Node[4] = Info.MACAddress.au8[4];
198 Uuid.Gen.au8Node[5] = Info.MACAddress.au8[5];
199 Info.Uuid = Uuid;
200 Info.enmMediumType = NETIF_T_ETHERNET;
201
202 HostNetworkInterfaceType_T enmType;
203 if (strncmp("vboxnet", szNICInstance, 7))
204 enmType = HostNetworkInterfaceType_Bridged;
205 else
206 enmType = HostNetworkInterfaceType_HostOnly;
207 ComObjPtr<HostNetworkInterface> IfObj;
208 IfObj.createObject();
209 if (SUCCEEDED(IfObj->init(Bstr(szNICDesc), enmType, &Info)))
210 pList->push_back(IfObj);
211}
212
213static boolean_t vboxSolarisAddLinkHostIface(const char *pszIface, void *pvHostNetworkInterfaceList)
214{
215 /*
216 * Skip IPSEC interfaces. It's at IP level.
217 */
218 if (!strncmp(pszIface, "ip.tun", 6))
219 return _B_FALSE;
220
221 /*
222 * Skip our own dynamic VNICs but don't skip VNIC templates.
223 * These names originate from VBoxNetFltBow-solaris.c, hardcoded here for now.
224 */
225 if ( strncmp(pszIface, "vboxvnic_template", 17)
226 && !strncmp(pszIface, "vboxvnic", 8))
227 return _B_FALSE;
228
229 /*
230 * Clip off the zone instance number from the interface name (if any).
231 */
232 char szIfaceName[128];
233 strcpy(szIfaceName, pszIface);
234 char *pszColon = (char *)memchr(szIfaceName, ':', sizeof(szIfaceName));
235 if (pszColon)
236 *pszColon = '\0';
237
238 /*
239 * Get the instance number from the interface name, then clip it off.
240 */
241 int cbInstance = 0;
242 int cbIface = strlen(szIfaceName);
243 const char *pszEnd = pszIface + cbIface - 1;
244 for (int i = 0; i < cbIface - 1; i++)
245 {
246 if (!RT_C_IS_DIGIT(*pszEnd))
247 break;
248 cbInstance++;
249 pszEnd--;
250 }
251
252 int Instance = atoi(pszEnd + 1);
253 strncpy(szIfaceName, pszIface, cbIface - cbInstance);
254 szIfaceName[cbIface - cbInstance] = '\0';
255
256 /*
257 * Add the interface.
258 */
259 vboxSolarisAddHostIface(szIfaceName, Instance, pvHostNetworkInterfaceList);
260
261 /*
262 * Continue walking...
263 */
264 return _B_FALSE;
265}
266
267static bool vboxSolarisSortNICList(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
268{
269 Bstr Iface1Str;
270 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
271
272 Bstr Iface2Str;
273 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
274
275 return Iface1Str < Iface2Str;
276}
277
278static bool vboxSolarisSameNIC(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
279{
280 Bstr Iface1Str;
281 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
282
283 Bstr Iface2Str;
284 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
285
286 return (Iface1Str == Iface2Str);
287}
288
289static int vboxSolarisAddPhysHostIface(di_node_t Node, di_minor_t Minor, void *pvHostNetworkInterfaceList)
290{
291 NOREF(Minor);
292
293 /*
294 * Skip aggregations.
295 */
296 if (!strcmp(di_driver_name(Node), "aggr"))
297 return DI_WALK_CONTINUE;
298
299 /*
300 * Skip softmacs.
301 */
302 if (!strcmp(di_driver_name(Node), "softmac"))
303 return DI_WALK_CONTINUE;
304
305 vboxSolarisAddHostIface(di_driver_name(Node), di_instance(Node), pvHostNetworkInterfaceList);
306 return DI_WALK_CONTINUE;
307}
308
309int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
310{
311
312 /*
313 * Use libdevinfo for determining all physical interfaces.
314 */
315 di_node_t Root;
316 Root = di_init("/", DINFOCACHE);
317 if (Root != DI_NODE_NIL)
318 {
319 di_walk_minor(Root, DDI_NT_NET, 0, &list, vboxSolarisAddPhysHostIface);
320 di_fini(Root);
321 }
322
323 /*
324 * Use libdlpi for determining all DLPI interfaces.
325 */
326 if (VBoxSolarisLibDlpiFound())
327 g_pfnLibDlpiWalk(vboxSolarisAddLinkHostIface, &list, 0);
328
329 /*
330 * This gets only the list of all plumbed logical interfaces.
331 * This is needed for zones which cannot access the device tree
332 * and in this case we just let them use the list of plumbed interfaces
333 * on the zone.
334 */
335 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
336 if (Sock > 0)
337 {
338 struct lifnum IfNum;
339 memset(&IfNum, 0, sizeof(IfNum));
340 IfNum.lifn_family = AF_INET;
341 int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
342 if (!rc)
343 {
344 struct lifreq Ifaces[24];
345 struct lifconf IfConfig;
346 memset(&IfConfig, 0, sizeof(IfConfig));
347 IfConfig.lifc_family = AF_INET;
348 IfConfig.lifc_len = sizeof(Ifaces);
349 IfConfig.lifc_buf = (caddr_t)&(Ifaces[0]);
350 rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
351 if (!rc)
352 {
353 for (int i = 0; i < IfNum.lifn_count; i++)
354 {
355 /*
356 * Skip loopback interfaces.
357 */
358 if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
359 continue;
360
361#if 0
362 rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
363 if (rc >= 0)
364 {
365 memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
366 sizeof(Info.IPAddress.au8));
367 // SIOCGLIFNETMASK
368 struct arpreq ArpReq;
369 memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
370
371 /*
372 * We might fail if the interface has not been assigned an IP address.
373 * That doesn't matter; as long as it's plumbed we can pick it up.
374 * But, if it has not acquired an IP address we cannot obtain it's MAC
375 * address this way, so we just use all zeros there.
376 */
377 rc = ioctl(Sock, SIOCGARP, &ArpReq);
378 if (rc >= 0)
379 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
380
381 char szNICDesc[LIFNAMSIZ + 256];
382 char *pszIface = Ifaces[i].lifr_name;
383 strcpy(szNICDesc, pszIface);
384
385 vboxSolarisAddLinkHostIface(pszIface, &list);
386 }
387#endif
388
389 char *pszIface = Ifaces[i].lifr_name;
390 vboxSolarisAddLinkHostIface(pszIface, &list);
391 }
392 }
393 }
394 close(Sock);
395 }
396
397 /*
398 * Weed out duplicates caused by dlpi_walk inconsistencies across Nevadas.
399 */
400 list.sort(vboxSolarisSortNICList);
401 list.unique(vboxSolarisSameNIC);
402
403 return VINF_SUCCESS;
404}
405
406#else
407int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
408{
409 return VERR_NOT_IMPLEMENTED;
410}
411#endif
412
413int NetIfGetConfigByName(PNETIFINFO pInfo)
414{
415 NOREF(pInfo);
416 return VERR_NOT_IMPLEMENTED;
417}
418
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