[15372] | 1 | /* $Id: NetIf-linux.cpp 98288 2023-01-24 15:32:43Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * Main - NetIfList, Linux implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
[15372] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[15372] | 26 | */
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 |
|
---|
[57358] | 30 | /*********************************************************************************************************************************
|
---|
| 31 | * Header Files *
|
---|
| 32 | *********************************************************************************************************************************/
|
---|
[76592] | 33 | #define LOG_GROUP LOG_GROUP_MAIN_HOST
|
---|
[15372] | 34 |
|
---|
[76474] | 35 | #include <iprt/errcore.h>
|
---|
[15442] | 36 | #include <list>
|
---|
[15462] | 37 | #include <sys/ioctl.h>
|
---|
[68026] | 38 | #include <sys/socket.h>
|
---|
| 39 | #include <linux/wireless.h>
|
---|
[15462] | 40 | #include <net/if_arp.h>
|
---|
[20481] | 41 | #include <net/route.h>
|
---|
[15462] | 42 | #include <netinet/in.h>
|
---|
[15463] | 43 | #include <stdio.h>
|
---|
| 44 | #include <unistd.h>
|
---|
[15464] | 45 | #include <iprt/asm.h>
|
---|
[15442] | 46 |
|
---|
| 47 | #include "HostNetworkInterfaceImpl.h"
|
---|
[15372] | 48 | #include "netif.h"
|
---|
[76592] | 49 | #include "LoggingNew.h"
|
---|
[15372] | 50 |
|
---|
[48180] | 51 | /**
|
---|
| 52 | * Obtain the name of the interface used for default routing.
|
---|
| 53 | *
|
---|
| 54 | * NOTE: There is a copy in Devices/Network/testcase/tstIntNet-1.cpp.
|
---|
| 55 | *
|
---|
| 56 | * @returns VBox status code.
|
---|
| 57 | *
|
---|
[74530] | 58 | * @param pszName The buffer where to put the name.
|
---|
| 59 | * @param cbName Size of of the destination buffer.
|
---|
[48180] | 60 | */
|
---|
[74530] | 61 | static int getDefaultIfaceName(char *pszName, size_t cbName)
|
---|
[20481] | 62 | {
|
---|
| 63 | FILE *fp = fopen("/proc/net/route", "r");
|
---|
| 64 | char szBuf[1024];
|
---|
| 65 | char szIfName[17];
|
---|
[48180] | 66 | uint32_t uAddr;
|
---|
| 67 | uint32_t uGateway;
|
---|
| 68 | uint32_t uMask;
|
---|
[20481] | 69 | int iTmp;
|
---|
[31040] | 70 | unsigned uFlags;
|
---|
[20481] | 71 |
|
---|
[23203] | 72 | if (fp)
|
---|
[20481] | 73 | {
|
---|
[23203] | 74 | while (fgets(szBuf, sizeof(szBuf)-1, fp))
|
---|
| 75 | {
|
---|
[48180] | 76 | int n = sscanf(szBuf, "%16s %x %x %x %d %d %d %x %d %d %d\n",
|
---|
| 77 | szIfName, &uAddr, &uGateway, &uFlags, &iTmp, &iTmp, &iTmp,
|
---|
| 78 | &uMask, &iTmp, &iTmp, &iTmp);
|
---|
[31040] | 79 | if (n < 10 || !(uFlags & RTF_UP))
|
---|
[23203] | 80 | continue;
|
---|
[20481] | 81 |
|
---|
[48180] | 82 | if (uAddr == 0 && uMask == 0)
|
---|
[23203] | 83 | {
|
---|
| 84 | fclose(fp);
|
---|
[74530] | 85 | szIfName[sizeof(szIfName) - 1] = '\0';
|
---|
| 86 | return RTStrCopy(pszName, cbName, szIfName);
|
---|
[23203] | 87 | }
|
---|
[20481] | 88 | }
|
---|
[33516] | 89 | fclose(fp);
|
---|
[20481] | 90 | }
|
---|
| 91 | return VERR_INTERNAL_ERROR;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[44742] | 94 | static uint32_t getInterfaceSpeed(const char *pszName)
|
---|
| 95 | {
|
---|
| 96 | /*
|
---|
| 97 | * I wish I could do simple ioctl here, but older kernels require root
|
---|
| 98 | * privileges for any ethtool commands.
|
---|
| 99 | */
|
---|
| 100 | char szBuf[256];
|
---|
| 101 | uint32_t uSpeed = 0;
|
---|
| 102 | /* First, we try to retrieve the speed via sysfs. */
|
---|
| 103 | RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/speed", pszName);
|
---|
| 104 | FILE *fp = fopen(szBuf, "r");
|
---|
| 105 | if (fp)
|
---|
| 106 | {
|
---|
| 107 | if (fscanf(fp, "%u", &uSpeed) != 1)
|
---|
| 108 | uSpeed = 0;
|
---|
| 109 | fclose(fp);
|
---|
| 110 | }
|
---|
| 111 | if (uSpeed == 10)
|
---|
| 112 | {
|
---|
| 113 | /* Check the cable is plugged in at all */
|
---|
| 114 | unsigned uCarrier = 0;
|
---|
| 115 | RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/carrier", pszName);
|
---|
| 116 | fp = fopen(szBuf, "r");
|
---|
| 117 | if (fp)
|
---|
| 118 | {
|
---|
| 119 | if (fscanf(fp, "%u", &uCarrier) != 1 || uCarrier == 0)
|
---|
| 120 | uSpeed = 0;
|
---|
| 121 | fclose(fp);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[48955] | 124 |
|
---|
[44742] | 125 | if (uSpeed == 0)
|
---|
| 126 | {
|
---|
| 127 | /* Failed to get speed via sysfs, go to plan B. */
|
---|
[98288] | 128 | int vrc = NetIfAdpCtlOut(pszName, "speed", szBuf, sizeof(szBuf));
|
---|
| 129 | if (RT_SUCCESS(vrc))
|
---|
[44742] | 130 | uSpeed = RTStrToUInt32(szBuf);
|
---|
| 131 | }
|
---|
| 132 | return uSpeed;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[15936] | 135 | static int getInterfaceInfo(int iSocket, const char *pszName, PNETIFINFO pInfo)
|
---|
| 136 | {
|
---|
[24592] | 137 | // Zeroing out pInfo is a bad idea as it should contain both short and long names at
|
---|
[33540] | 138 | // this point. So make sure the structure is cleared by the caller if necessary!
|
---|
[24592] | 139 | // memset(pInfo, 0, sizeof(*pInfo));
|
---|
[15936] | 140 | struct ifreq Req;
|
---|
[47117] | 141 | RT_ZERO(Req);
|
---|
| 142 | RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pszName);
|
---|
[15936] | 143 | if (ioctl(iSocket, SIOCGIFHWADDR, &Req) >= 0)
|
---|
| 144 | {
|
---|
| 145 | switch (Req.ifr_hwaddr.sa_family)
|
---|
| 146 | {
|
---|
| 147 | case ARPHRD_ETHER:
|
---|
[17275] | 148 | pInfo->enmMediumType = NETIF_T_ETHERNET;
|
---|
[15936] | 149 | break;
|
---|
| 150 | default:
|
---|
[17275] | 151 | pInfo->enmMediumType = NETIF_T_UNKNOWN;
|
---|
[15936] | 152 | break;
|
---|
| 153 | }
|
---|
[16251] | 154 | /* Generate UUID from name and MAC address. */
|
---|
[15936] | 155 | RTUUID uuid;
|
---|
[16251] | 156 | RTUuidClear(&uuid);
|
---|
| 157 | memcpy(&uuid, Req.ifr_name, RT_MIN(sizeof(Req.ifr_name), sizeof(uuid)));
|
---|
[62363] | 158 | uuid.Gen.u8ClockSeqHiAndReserved = (uint8_t)((uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80);
|
---|
| 159 | uuid.Gen.u16TimeHiAndVersion = (uint16_t)((uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000);
|
---|
[15936] | 160 | memcpy(uuid.Gen.au8Node, &Req.ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node));
|
---|
| 161 | pInfo->Uuid = uuid;
|
---|
[16251] | 162 |
|
---|
[15936] | 163 | memcpy(&pInfo->MACAddress, Req.ifr_hwaddr.sa_data, sizeof(pInfo->MACAddress));
|
---|
| 164 |
|
---|
| 165 | if (ioctl(iSocket, SIOCGIFADDR, &Req) >= 0)
|
---|
| 166 | memcpy(pInfo->IPAddress.au8,
|
---|
| 167 | &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr,
|
---|
| 168 | sizeof(pInfo->IPAddress.au8));
|
---|
| 169 |
|
---|
| 170 | if (ioctl(iSocket, SIOCGIFNETMASK, &Req) >= 0)
|
---|
| 171 | memcpy(pInfo->IPNetMask.au8,
|
---|
| 172 | &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr,
|
---|
| 173 | sizeof(pInfo->IPNetMask.au8));
|
---|
| 174 |
|
---|
| 175 | if (ioctl(iSocket, SIOCGIFFLAGS, &Req) >= 0)
|
---|
| 176 | pInfo->enmStatus = Req.ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
|
---|
| 177 |
|
---|
[68026] | 178 | struct iwreq WRq;
|
---|
| 179 | RT_ZERO(WRq);
|
---|
| 180 | RTStrCopy(WRq.ifr_name, sizeof(WRq.ifr_name), pszName);
|
---|
| 181 | pInfo->fWireless = ioctl(iSocket, SIOCGIWNAME, &WRq) >= 0;
|
---|
| 182 |
|
---|
[15936] | 183 | FILE *fp = fopen("/proc/net/if_inet6", "r");
|
---|
| 184 | if (fp)
|
---|
| 185 | {
|
---|
| 186 | RTNETADDRIPV6 IPv6Address;
|
---|
| 187 | unsigned uIndex, uLength, uScope, uTmp;
|
---|
| 188 | char szName[30];
|
---|
| 189 | for (;;)
|
---|
| 190 | {
|
---|
[47117] | 191 | RT_ZERO(szName);
|
---|
[15936] | 192 | int n = fscanf(fp,
|
---|
| 193 | "%08x%08x%08x%08x"
|
---|
| 194 | " %02x %02x %02x %02x %20s\n",
|
---|
| 195 | &IPv6Address.au32[0], &IPv6Address.au32[1],
|
---|
| 196 | &IPv6Address.au32[2], &IPv6Address.au32[3],
|
---|
| 197 | &uIndex, &uLength, &uScope, &uTmp, szName);
|
---|
| 198 | if (n == EOF)
|
---|
| 199 | break;
|
---|
| 200 | if (n != 9 || uLength > 128)
|
---|
| 201 | {
|
---|
| 202 | Log(("getInterfaceInfo: Error while reading /proc/net/if_inet6, n=%d uLength=%u\n",
|
---|
| 203 | n, uLength));
|
---|
| 204 | break;
|
---|
| 205 | }
|
---|
| 206 | if (!strcmp(Req.ifr_name, szName))
|
---|
| 207 | {
|
---|
| 208 | pInfo->IPv6Address.au32[0] = htonl(IPv6Address.au32[0]);
|
---|
| 209 | pInfo->IPv6Address.au32[1] = htonl(IPv6Address.au32[1]);
|
---|
| 210 | pInfo->IPv6Address.au32[2] = htonl(IPv6Address.au32[2]);
|
---|
| 211 | pInfo->IPv6Address.au32[3] = htonl(IPv6Address.au32[3]);
|
---|
[67432] | 212 | RTNetPrefixToMaskIPv6(uLength, &pInfo->IPv6NetMask);
|
---|
[15936] | 213 | }
|
---|
| 214 | }
|
---|
| 215 | fclose(fp);
|
---|
| 216 | }
|
---|
[43507] | 217 | /*
|
---|
[43668] | 218 | * Don't even try to get speed for non-Ethernet interfaces, it only
|
---|
| 219 | * produces errors.
|
---|
[43507] | 220 | */
|
---|
[43668] | 221 | if (pInfo->enmMediumType == NETIF_T_ETHERNET)
|
---|
[44742] | 222 | pInfo->uSpeedMbits = getInterfaceSpeed(pszName);
|
---|
| 223 | else
|
---|
| 224 | pInfo->uSpeedMbits = 0;
|
---|
[15936] | 225 | }
|
---|
| 226 | return VINF_SUCCESS;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[21878] | 229 | int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
|
---|
[15372] | 230 | {
|
---|
[20481] | 231 | char szDefaultIface[256];
|
---|
[98288] | 232 | int vrc = getDefaultIfaceName(szDefaultIface, sizeof(szDefaultIface));
|
---|
| 233 | if (RT_FAILURE(vrc))
|
---|
[20481] | 234 | {
|
---|
| 235 | Log(("NetIfList: Failed to find default interface.\n"));
|
---|
[74530] | 236 | szDefaultIface[0] = '\0';
|
---|
[20481] | 237 | }
|
---|
[15462] | 238 | int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 239 | if (sock >= 0)
|
---|
| 240 | {
|
---|
[15936] | 241 | FILE *fp = fopen("/proc/net/dev", "r");
|
---|
| 242 | if (fp)
|
---|
[15462] | 243 | {
|
---|
[15936] | 244 | char buf[256];
|
---|
| 245 | while (fgets(buf, sizeof(buf), fp))
|
---|
[15462] | 246 | {
|
---|
[15936] | 247 | char *pszEndOfName = strchr(buf, ':');
|
---|
| 248 | if (!pszEndOfName)
|
---|
| 249 | continue;
|
---|
| 250 | *pszEndOfName = 0;
|
---|
[62363] | 251 | size_t iFirstNonWS = strspn(buf, " ");
|
---|
| 252 | char *pszName = buf + iFirstNonWS;
|
---|
[15936] | 253 | NETIFINFO Info;
|
---|
[24592] | 254 | RT_ZERO(Info);
|
---|
[98288] | 255 | vrc = getInterfaceInfo(sock, pszName, &Info);
|
---|
| 256 | if (RT_FAILURE(vrc))
|
---|
[15936] | 257 | break;
|
---|
[17275] | 258 | if (Info.enmMediumType == NETIF_T_ETHERNET)
|
---|
[15462] | 259 | {
|
---|
[15936] | 260 | ComObjPtr<HostNetworkInterface> IfObj;
|
---|
| 261 | IfObj.createObject();
|
---|
[17834] | 262 |
|
---|
| 263 | HostNetworkInterfaceType_T enmType;
|
---|
[48016] | 264 | if (strncmp(pszName, RT_STR_TUPLE("vboxnet")))
|
---|
[17834] | 265 | enmType = HostNetworkInterfaceType_Bridged;
|
---|
| 266 | else
|
---|
| 267 | enmType = HostNetworkInterfaceType_HostOnly;
|
---|
| 268 |
|
---|
[77436] | 269 | if (SUCCEEDED(IfObj->init(pszName, enmType, &Info)))
|
---|
[20481] | 270 | {
|
---|
| 271 | if (strcmp(pszName, szDefaultIface) == 0)
|
---|
| 272 | list.push_front(IfObj);
|
---|
| 273 | else
|
---|
| 274 | list.push_back(IfObj);
|
---|
| 275 | }
|
---|
[15936] | 276 | }
|
---|
[15462] | 277 |
|
---|
| 278 | }
|
---|
[15936] | 279 | fclose(fp);
|
---|
[15462] | 280 | }
|
---|
| 281 | close(sock);
|
---|
| 282 | }
|
---|
[19233] | 283 | else
|
---|
[98288] | 284 | vrc = VERR_INTERNAL_ERROR;
|
---|
[15936] | 285 |
|
---|
[98288] | 286 | return vrc;
|
---|
[15372] | 287 | }
|
---|
[19233] | 288 |
|
---|
[19433] | 289 | int NetIfGetConfigByName(PNETIFINFO pInfo)
|
---|
[19233] | 290 | {
|
---|
| 291 | int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 292 | if (sock < 0)
|
---|
| 293 | return VERR_NOT_IMPLEMENTED;
|
---|
[98288] | 294 | int vrc = getInterfaceInfo(sock, pInfo->szShortName, pInfo);
|
---|
[19233] | 295 | close(sock);
|
---|
[98288] | 296 | return vrc;
|
---|
[19233] | 297 | }
|
---|
[44742] | 298 |
|
---|
| 299 | /**
|
---|
| 300 | * Retrieve the physical link speed in megabits per second. If the interface is
|
---|
| 301 | * not up or otherwise unavailable the zero speed is returned.
|
---|
| 302 | *
|
---|
| 303 | * @returns VBox status code.
|
---|
| 304 | *
|
---|
| 305 | * @param pcszIfName Interface name.
|
---|
| 306 | * @param puMbits Where to store the link speed.
|
---|
| 307 | */
|
---|
| 308 | int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
|
---|
| 309 | {
|
---|
| 310 | int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 311 | if (sock < 0)
|
---|
| 312 | return VERR_OUT_OF_RESOURCES;
|
---|
| 313 | struct ifreq Req;
|
---|
[47117] | 314 | RT_ZERO(Req);
|
---|
| 315 | RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
|
---|
[44742] | 316 | if (ioctl(sock, SIOCGIFHWADDR, &Req) >= 0)
|
---|
| 317 | {
|
---|
| 318 | if (ioctl(sock, SIOCGIFFLAGS, &Req) >= 0)
|
---|
| 319 | if (Req.ifr_flags & IFF_UP)
|
---|
| 320 | {
|
---|
[46345] | 321 | close(sock);
|
---|
[44742] | 322 | *puMbits = getInterfaceSpeed(pcszIfName);
|
---|
| 323 | return VINF_SUCCESS;
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
[46345] | 326 | close(sock);
|
---|
[44742] | 327 | *puMbits = 0;
|
---|
| 328 | return VWRN_NOT_FOUND;
|
---|
| 329 | }
|
---|