VirtualBox

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

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

#3282 HostNetIf API: Windows implementation is working and is enabled by default.

  • 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 15510 2008-12-15 15:32:45Z 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);
169 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
170 {
171 memcpy(Info.IPv6Address.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
172 sizeof(Info.IPv6Address.au8));
173 }
174 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
175 {
176 memcpy(Info.IPv6NetMask.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
177 sizeof(Info.IPv6NetMask.au8));
178 }
179 close(Sock);
180 }
181
182 /*
183 * Construct UUID with interface name and the MAC address if available.
184 */
185 RTUUID Uuid;
186 RTUuidClear(&Uuid);
187 memcpy(&Uuid, szNICInstance, RT_MIN(strlen(szNICInstance), sizeof(Uuid)));
188 Uuid.Gen.u8ClockSeqHiAndReserved = (Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
189 Uuid.Gen.u16TimeHiAndVersion = (Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
190 Uuid.Gen.au8Node[0] = Info.MACAddress.au8[0];
191 Uuid.Gen.au8Node[1] = Info.MACAddress.au8[1];
192 Uuid.Gen.au8Node[2] = Info.MACAddress.au8[2];
193 Uuid.Gen.au8Node[3] = Info.MACAddress.au8[3];
194 Uuid.Gen.au8Node[4] = Info.MACAddress.au8[4];
195 Uuid.Gen.au8Node[5] = Info.MACAddress.au8[5];
196 Info.Uuid = Uuid;
197 Info.enmType = NETIF_T_ETHERNET;
198
199 ComObjPtr<HostNetworkInterface> IfObj;
200 IfObj.createObject();
201 if (SUCCEEDED(IfObj->init(Bstr(szNICDesc), &Info)))
202 pList->push_back(IfObj);
203}
204
205static boolean_t vboxSolarisAddLinkHostIface(const char *pszIface, void *pvHostNetworkInterfaceList)
206{
207 /*
208 * Clip off the zone instance number from the interface name (if any).
209 */
210 char szIfaceName[128];
211 strcpy(szIfaceName, pszIface);
212 char *pszColon = (char *)memchr(szIfaceName, ':', sizeof(szIfaceName));
213 if (pszColon)
214 *pszColon = '\0';
215
216 /*
217 * Get the instance number from the interface name, then clip it off.
218 */
219 int cbInstance = 0;
220 int cbIface = strlen(szIfaceName);
221 const char *pszEnd = pszIface + cbIface - 1;
222 for (int i = 0; i < cbIface - 1; i++)
223 {
224 if (!RT_C_IS_DIGIT(*pszEnd))
225 break;
226 cbInstance++;
227 pszEnd--;
228 }
229
230 int Instance = atoi(pszEnd + 1);
231 strncpy(szIfaceName, pszIface, cbIface - cbInstance);
232 szIfaceName[cbIface - cbInstance] = '\0';
233
234 /*
235 * Add the interface.
236 */
237 vboxSolarisAddHostIface(szIfaceName, Instance, pvHostNetworkInterfaceList);
238
239 /*
240 * Continue walking...
241 */
242 return _B_FALSE;
243}
244
245static bool vboxSolarisSortNICList(const ComObjPtr <HostNetworkInterface> Iface1, const ComObjPtr <HostNetworkInterface> Iface2)
246{
247 Bstr Iface1Str;
248 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
249
250 Bstr Iface2Str;
251 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
252
253 return Iface1Str < Iface2Str;
254}
255
256static bool vboxSolarisSameNIC(const ComObjPtr <HostNetworkInterface> Iface1, const ComObjPtr <HostNetworkInterface> Iface2)
257{
258 Bstr Iface1Str;
259 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
260
261 Bstr Iface2Str;
262 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
263
264 return (Iface1Str == Iface2Str);
265}
266
267# ifdef VBOX_SOLARIS_NSL_RESOLVED
268static int vboxSolarisAddPhysHostIface(di_node_t Node, di_minor_t Minor, void *pvHostNetworkInterfaceList)
269{
270 /*
271 * Skip aggregations.
272 */
273 if (!strcmp(di_driver_name(Node), "aggr"))
274 return DI_WALK_CONTINUE;
275
276 /*
277 * Skip softmacs.
278 */
279 if (!strcmp(di_driver_name(Node), "softmac"))
280 return DI_WALK_CONTINUE;
281
282 vboxSolarisAddHostIface(di_driver_name(Node), di_instance(Node), pvHostNetworkInterfaceList);
283 return DI_WALK_CONTINUE;
284}
285# endif /* VBOX_SOLARIS_NSL_RESOLVED */
286
287int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
288{
289
290# ifdef VBOX_SOLARIS_NSL_RESOLVED
291
292 /*
293 * Use libdevinfo for determining all physical interfaces.
294 */
295 di_node_t Root;
296 Root = di_init("/", DINFOCACHE);
297 if (Root != DI_NODE_NIL)
298 {
299 di_walk_minor(Root, DDI_NT_NET, 0, &list, vboxSolarisAddPhysHostIface);
300 di_fini(Root);
301 }
302
303 /*
304 * Use libdlpi for determining all DLPI interfaces.
305 */
306 if (VBoxSolarisLibDlpiFound())
307 g_pfnLibDlpiWalk(vboxSolarisAddLinkHostIface, &list, 0);
308
309# endif /* VBOX_SOLARIS_NSL_RESOLVED */
310
311 /*
312 * This gets only the list of all plumbed logical interfaces.
313 * This is needed for zones which cannot access the device tree
314 * and in this case we just let them use the list of plumbed interfaces
315 * on the zone.
316 */
317 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
318 if (Sock > 0)
319 {
320 struct lifnum IfNum;
321 memset(&IfNum, 0, sizeof(IfNum));
322 IfNum.lifn_family = AF_INET;
323 int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
324 if (!rc)
325 {
326 struct lifreq Ifaces[24];
327 struct lifconf IfConfig;
328 memset(&IfConfig, 0, sizeof(IfConfig));
329 IfConfig.lifc_family = AF_INET;
330 IfConfig.lifc_len = sizeof(Ifaces);
331 IfConfig.lifc_buf = (caddr_t)&(Ifaces[0]);
332 rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
333 if (!rc)
334 {
335 for (int i = 0; i < IfNum.lifn_count; i++)
336 {
337 /*
338 * Skip loopback interfaces.
339 */
340 if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
341 continue;
342
343#if 0
344 rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
345 if (rc >= 0)
346 {
347 memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
348 sizeof(Info.IPAddress.au8));
349 // SIOCGLIFNETMASK
350 struct arpreq ArpReq;
351 memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
352
353 /*
354 * We might fail if the interface has not been assigned an IP address.
355 * That doesn't matter; as long as it's plumbed we can pick it up.
356 * But, if it has not acquired an IP address we cannot obtain it's MAC
357 * address this way, so we just use all zeros there.
358 */
359 rc = ioctl(Sock, SIOCGARP, &ArpReq);
360 if (rc >= 0)
361 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
362
363 char szNICDesc[LIFNAMSIZ + 256];
364 char *pszIface = Ifaces[i].lifr_name;
365 strcpy(szNICDesc, pszIface);
366
367 vboxSolarisAddLinkHostIface(pszIface, &list);
368 }
369#endif
370
371 char *pszIface = Ifaces[i].lifr_name;
372 vboxSolarisAddLinkHostIface(pszIface, &list);
373 }
374 }
375 }
376 close(Sock);
377 }
378
379 /*
380 * Weed out duplicates caused by dlpi_walk inconsistencies across Nevadas.
381 */
382 list.sort(vboxSolarisSortNICList);
383 list.unique(vboxSolarisSameNIC);
384
385 return VINF_SUCCESS;
386}
387#else
388int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
389{
390 return VERR_NOT_IMPLEMENTED;
391}
392#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