[10663] | 1 | /* $Id: tstIntNet-1.cpp 103431 2024-02-19 12:06:37Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[10663] | 3 | * VBox - Testcase for internal networking, simple NetFlt trunk creation.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 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
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[1] | 32 | #include <VBox/intnet.h>
|
---|
[26574] | 33 | #include <VBox/intnetinline.h>
|
---|
[35346] | 34 | #include <VBox/vmm/pdmnetinline.h>
|
---|
[1] | 35 | #include <VBox/sup.h>
|
---|
[35346] | 36 | #include <VBox/vmm/vmm.h>
|
---|
[76474] | 37 | #include <iprt/errcore.h>
|
---|
[10663] | 38 | #include <iprt/initterm.h>
|
---|
| 39 | #include <iprt/alloc.h>
|
---|
| 40 | #include <iprt/path.h>
|
---|
[1] | 41 | #include <iprt/stream.h>
|
---|
[10663] | 42 | #include <iprt/string.h>
|
---|
[1] | 43 | #include <iprt/thread.h>
|
---|
[10663] | 44 | #include <iprt/param.h>
|
---|
[10747] | 45 | #include <iprt/getopt.h>
|
---|
[10751] | 46 | #include <iprt/rand.h>
|
---|
| 47 | #include <iprt/log.h>
|
---|
[31847] | 48 | #include <iprt/crc.h>
|
---|
[10978] | 49 | #include <iprt/net.h>
|
---|
[1] | 50 |
|
---|
[10756] | 51 | #include "../Pcap.h"
|
---|
[1] | 52 |
|
---|
[10756] | 53 |
|
---|
[57358] | 54 | /*********************************************************************************************************************************
|
---|
| 55 | * Global Variables *
|
---|
| 56 | *********************************************************************************************************************************/
|
---|
[10756] | 57 | static int g_cErrors = 0;
|
---|
| 58 | static uint64_t g_StartTS = 0;
|
---|
[10757] | 59 | static uint32_t g_DhcpXID = 0;
|
---|
| 60 | static bool g_fDhcpReply = false;
|
---|
[11684] | 61 | static bool g_fPingReply = false;
|
---|
[11045] | 62 | static uint32_t g_cOtherPkts = 0;
|
---|
| 63 | static uint32_t g_cArpPkts = 0;
|
---|
| 64 | static uint32_t g_cIpv4Pkts = 0;
|
---|
[16536] | 65 | static uint32_t g_cUdpPkts = 0;
|
---|
| 66 | static uint32_t g_cDhcpPkts = 0;
|
---|
| 67 | static uint32_t g_cTcpPkts = 0;
|
---|
[1] | 68 |
|
---|
| 69 |
|
---|
[11045] | 70 | /**
|
---|
| 71 | * Error reporting wrapper.
|
---|
| 72 | *
|
---|
| 73 | * @param pErrStrm The stream to write the error message to. Can be NULL.
|
---|
| 74 | * @param pszFormat The message format string.
|
---|
| 75 | * @param ... Format arguments.
|
---|
| 76 | */
|
---|
| 77 | static void tstIntNetError(PRTSTREAM pErrStrm, const char *pszFormat, ...)
|
---|
| 78 | {
|
---|
| 79 | if (!pErrStrm)
|
---|
| 80 | pErrStrm = g_pStdOut;
|
---|
[10756] | 81 |
|
---|
[11045] | 82 | va_list va;
|
---|
| 83 | va_start(va, pszFormat);
|
---|
| 84 | RTStrmPrintf(pErrStrm, "tstIntNet-1: ERROR - ");
|
---|
| 85 | RTStrmPrintfV(pErrStrm, pszFormat, va);
|
---|
| 86 | va_end(va);
|
---|
| 87 |
|
---|
| 88 | g_cErrors++;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
[10747] | 92 | /**
|
---|
[11045] | 93 | * Parses a frame an runs in thru the RTNet validation code so it gets
|
---|
| 94 | * some exercise.
|
---|
| 95 | *
|
---|
| 96 | * @param pvFrame Pointer to the ethernet frame.
|
---|
| 97 | * @param cbFrame The size of the ethernet frame.
|
---|
| 98 | * @param pErrStrm The error stream.
|
---|
| 99 | */
|
---|
[28025] | 100 | static void tstIntNetTestFrame(void const *pvFrame, size_t cbFrame, PRTSTREAM pErrStrm, bool fGso)
|
---|
[11045] | 101 | {
|
---|
| 102 | /*
|
---|
| 103 | * Ethernet header.
|
---|
| 104 | */
|
---|
| 105 | PCRTNETETHERHDR pEtherHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
| 106 | if (cbFrame <= sizeof(*pEtherHdr))
|
---|
| 107 | return tstIntNetError(pErrStrm, "cbFrame=%#x <= %#x (ether)\n", cbFrame, sizeof(*pEtherHdr));
|
---|
| 108 | ssize_t cbLeft = cbFrame - sizeof(*pEtherHdr);
|
---|
| 109 | uint8_t const *pbCur = (uint8_t const *)(pEtherHdr + 1);
|
---|
| 110 |
|
---|
| 111 | switch (RT_BE2H_U16(pEtherHdr->EtherType))
|
---|
| 112 | {
|
---|
| 113 | case RTNET_ETHERTYPE_ARP:
|
---|
| 114 | {
|
---|
| 115 | g_cArpPkts++;
|
---|
| 116 | break;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | case RTNET_ETHERTYPE_IPV4:
|
---|
| 120 | {
|
---|
| 121 | g_cIpv4Pkts++;
|
---|
| 122 |
|
---|
| 123 | PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)pbCur;
|
---|
[28025] | 124 | if (!RTNetIPv4IsHdrValid(pIpHdr, cbLeft, cbLeft, !fGso /*fChecksum*/))
|
---|
[11045] | 125 | return tstIntNetError(pErrStrm, "RTNetIPv4IsHdrValid failed\n");
|
---|
| 126 | pbCur += pIpHdr->ip_hl * 4;
|
---|
| 127 | cbLeft -= pIpHdr->ip_hl * 4;
|
---|
| 128 | AssertFatal(cbLeft >= 0);
|
---|
| 129 |
|
---|
| 130 | switch (pIpHdr->ip_p)
|
---|
| 131 | {
|
---|
| 132 | case RTNETIPV4_PROT_ICMP:
|
---|
| 133 | {
|
---|
[11051] | 134 | /** @todo ICMP? */
|
---|
[11045] | 135 | break;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | case RTNETIPV4_PROT_UDP:
|
---|
| 139 | {
|
---|
| 140 | g_cUdpPkts++;
|
---|
| 141 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)pbCur;
|
---|
[28025] | 142 | if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbLeft, !fGso /*fChecksum*/))
|
---|
[11045] | 143 | return tstIntNetError(pErrStrm, "RTNetIPv4IsUDPValid failed\n");
|
---|
| 144 | pbCur += sizeof(*pUdpHdr);
|
---|
| 145 | cbLeft -= sizeof(*pUdpHdr);
|
---|
| 146 |
|
---|
| 147 | if (RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS)
|
---|
| 148 | {
|
---|
| 149 | g_cDhcpPkts++;
|
---|
[11050] | 150 | PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)pbCur;
|
---|
| 151 | if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbLeft, NULL))
|
---|
| 152 | return tstIntNetError(pErrStrm, "RTNetIPv4IsDHCPValid failed\n");
|
---|
[11045] | 153 | }
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | case RTNETIPV4_PROT_TCP:
|
---|
| 158 | {
|
---|
| 159 | g_cTcpPkts++;
|
---|
| 160 | PCRTNETTCP pTcpHdr = (PCRTNETTCP)pbCur;
|
---|
[28025] | 161 | if (!RTNetIPv4IsTCPValid(pIpHdr, pTcpHdr, cbLeft, NULL, cbLeft, !fGso /*fChecksum*/))
|
---|
[11045] | 162 | return tstIntNetError(pErrStrm, "RTNetIPv4IsTCPValid failed\n");
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | //case RTNET_ETHERTYPE_IPV6:
|
---|
| 170 | default:
|
---|
| 171 | g_cOtherPkts++;
|
---|
| 172 | break;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
[10751] | 178 | * Transmits one frame after appending the CRC.
|
---|
| 179 | *
|
---|
| 180 | * @param hIf The interface handle.
|
---|
| 181 | * @param pSession The session.
|
---|
| 182 | * @param pBuf The shared interface buffer.
|
---|
| 183 | * @param pvFrame The frame without a crc.
|
---|
| 184 | * @param cbFrame The size of it.
|
---|
[10756] | 185 | * @param pFileRaw The file to write the raw data to (optional).
|
---|
[11045] | 186 | * @param pFileText The file to write a textual packet summary to (optional).
|
---|
[10751] | 187 | */
|
---|
[11045] | 188 | static void doXmitFrame(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, void *pvFrame, size_t cbFrame, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
|
---|
[10751] | 189 | {
|
---|
| 190 | /*
|
---|
[11045] | 191 | * Log it.
|
---|
[10751] | 192 | */
|
---|
[11045] | 193 | if (pFileText)
|
---|
| 194 | {
|
---|
| 195 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
| 196 | uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
|
---|
| 197 | RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x Send!\n",
|
---|
| 198 | NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
|
---|
| 199 | cbFrame, &pEthHdr->SrcMac, &pEthHdr->DstMac, RT_BE2H_U16(pEthHdr->EtherType));
|
---|
| 200 | }
|
---|
[10751] | 201 |
|
---|
| 202 | /*
|
---|
[11045] | 203 | * Run in thru the frame validator to test the RTNet code.
|
---|
| 204 | */
|
---|
[28025] | 205 | tstIntNetTestFrame(pvFrame, cbFrame, pFileText, false /*fGso*/);
|
---|
[11045] | 206 |
|
---|
| 207 | /*
|
---|
[10751] | 208 | * Write the frame and push the queue.
|
---|
| 209 | *
|
---|
| 210 | * Don't bother with dealing with overflows like DrvIntNet does, because
|
---|
| 211 | * it's not supposed to happen here in this testcase.
|
---|
| 212 | */
|
---|
[28714] | 213 | int rc = IntNetRingWriteFrame(&pBuf->Send, pvFrame, cbFrame);
|
---|
[10756] | 214 | if (RT_SUCCESS(rc))
|
---|
[10751] | 215 | {
|
---|
[10756] | 216 | if (pFileRaw)
|
---|
| 217 | PcapStreamFrame(pFileRaw, g_StartTS, pvFrame, cbFrame, 0xffff);
|
---|
| 218 | }
|
---|
| 219 | else
|
---|
| 220 | {
|
---|
[28714] | 221 | RTPrintf("tstIntNet-1: IntNetRingWriteFrame failed, %Rrc; cbFrame=%d pBuf->cbSend=%d\n", rc, cbFrame, pBuf->cbSend);
|
---|
[10751] | 222 | g_cErrors++;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | INTNETIFSENDREQ SendReq;
|
---|
| 226 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 227 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
| 228 | SendReq.pSession = pSession;
|
---|
| 229 | SendReq.hIf = hIf;
|
---|
[20864] | 230 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
[10751] | 231 | if (RT_FAILURE(rc))
|
---|
| 232 | {
|
---|
[20864] | 233 | RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_SEND,) failed, rc=%Rrc\n", rc);
|
---|
[10751] | 234 | g_cErrors++;
|
---|
| 235 | }
|
---|
[10756] | 236 |
|
---|
[10751] | 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | /**
|
---|
| 241 | * Does the transmit test.
|
---|
| 242 | *
|
---|
| 243 | * @param hIf The interface handle.
|
---|
| 244 | * @param pSession The session.
|
---|
| 245 | * @param pBuf The shared interface buffer.
|
---|
| 246 | * @param pSrcMac The mac address to use as source.
|
---|
[10756] | 247 | * @param pFileRaw The file to write the raw data to (optional).
|
---|
[11045] | 248 | * @param pFileText The file to write a textual packet summary to (optional).
|
---|
[10751] | 249 | */
|
---|
[11157] | 250 | static void doXmitTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCRTMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
|
---|
[10751] | 251 | {
|
---|
[10756] | 252 | uint8_t abFrame[4096];
|
---|
[10978] | 253 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)&abFrame[0];
|
---|
| 254 | PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
|
---|
| 255 | PRTNETUDP pUdpHdr = (PRTNETUDP) (pIpHdr + 1);
|
---|
| 256 | PRTNETDHCP pDhcpMsg = (PRTNETDHCP) (pUdpHdr + 1);
|
---|
[10756] | 257 |
|
---|
[10751] | 258 | /*
|
---|
| 259 | * Create a simple DHCP broadcast request.
|
---|
| 260 | */
|
---|
[10756] | 261 | memset(&abFrame, 0, sizeof(abFrame));
|
---|
[10751] | 262 |
|
---|
[10756] | 263 | pDhcpMsg->Op = 1; /* request */
|
---|
[10757] | 264 | pDhcpMsg->HType = 1; /* ethernet */
|
---|
[11157] | 265 | pDhcpMsg->HLen = sizeof(RTMAC);
|
---|
[10756] | 266 | pDhcpMsg->Hops = 0;
|
---|
[10757] | 267 | pDhcpMsg->XID = g_DhcpXID = RTRandU32();
|
---|
[10756] | 268 | pDhcpMsg->Secs = 0;
|
---|
| 269 | pDhcpMsg->Flags = 0; /* unicast */ //RT_H2BE_U16(0x8000); /* broadcast */
|
---|
[10978] | 270 | pDhcpMsg->CIAddr.u = 0;
|
---|
| 271 | pDhcpMsg->YIAddr.u = 0;
|
---|
| 272 | pDhcpMsg->SIAddr.u = 0;
|
---|
| 273 | pDhcpMsg->GIAddr.u = 0;
|
---|
[10756] | 274 | memset(&pDhcpMsg->CHAddr[0], '\0', sizeof(pDhcpMsg->CHAddr));
|
---|
| 275 | memcpy(&pDhcpMsg->CHAddr[0], pSrcMac, sizeof(*pSrcMac));
|
---|
| 276 | memset(&pDhcpMsg->SName[0], '\0', sizeof(pDhcpMsg->SName));
|
---|
| 277 | memset(&pDhcpMsg->File[0], '\0', sizeof(pDhcpMsg->File));
|
---|
| 278 | pDhcpMsg->abMagic[0] = 99;
|
---|
| 279 | pDhcpMsg->abMagic[1] = 130;
|
---|
| 280 | pDhcpMsg->abMagic[2] = 83;
|
---|
| 281 | pDhcpMsg->abMagic[3] = 99;
|
---|
| 282 |
|
---|
| 283 | pDhcpMsg->DhcpOpt = 53; /* DHCP Msssage Type option */
|
---|
| 284 | pDhcpMsg->DhcpLen = 1;
|
---|
| 285 | pDhcpMsg->DhcpReq = 1; /* DHCPDISCOVER */
|
---|
| 286 |
|
---|
| 287 | memset(&pDhcpMsg->abOptions[0], '\0', sizeof(pDhcpMsg->abOptions));
|
---|
| 288 | uint8_t *pbOpt = &pDhcpMsg->abOptions[0];
|
---|
| 289 |
|
---|
| 290 | *pbOpt++ = 116; /* DHCP Auto-Configure */
|
---|
| 291 | *pbOpt++ = 1;
|
---|
| 292 | *pbOpt++ = 1;
|
---|
| 293 |
|
---|
| 294 | *pbOpt++ = 61; /* Client identifier */
|
---|
| 295 | *pbOpt++ = 1 + sizeof(*pSrcMac);
|
---|
| 296 | *pbOpt++ = 1; /* hw type: ethernet */
|
---|
| 297 | memcpy(pbOpt, pSrcMac, sizeof(*pSrcMac));
|
---|
| 298 | pbOpt += sizeof(*pSrcMac);
|
---|
| 299 |
|
---|
| 300 | *pbOpt++ = 12; /* Host name */
|
---|
| 301 | *pbOpt++ = sizeof("tstIntNet-1") - 1;
|
---|
| 302 | memcpy(pbOpt, "tstIntNet-1", sizeof("tstIntNet-1") - 1);
|
---|
| 303 | pbOpt += sizeof("tstIntNet-1") - 1;
|
---|
| 304 |
|
---|
| 305 | *pbOpt = 0xff; /* the end */
|
---|
| 306 |
|
---|
| 307 | /* UDP */
|
---|
| 308 | pUdpHdr->uh_sport = RT_H2BE_U16(68); /* bootp */
|
---|
| 309 | pUdpHdr->uh_dport = RT_H2BE_U16(67); /* bootps */
|
---|
| 310 | pUdpHdr->uh_ulen = RT_H2BE_U16(sizeof(*pDhcpMsg) + sizeof(*pUdpHdr));
|
---|
| 311 | pUdpHdr->uh_sum = 0; /* pretend checksumming is disabled */
|
---|
| 312 |
|
---|
| 313 | /* IP */
|
---|
| 314 | pIpHdr->ip_v = 4;
|
---|
| 315 | pIpHdr->ip_hl = sizeof(*pIpHdr) / sizeof(uint32_t);
|
---|
| 316 | pIpHdr->ip_tos = 0;
|
---|
| 317 | pIpHdr->ip_len = RT_H2BE_U16(sizeof(*pDhcpMsg) + sizeof(*pUdpHdr) + sizeof(*pIpHdr));
|
---|
| 318 | pIpHdr->ip_id = (uint16_t)RTRandU32();
|
---|
| 319 | pIpHdr->ip_off = 0;
|
---|
| 320 | pIpHdr->ip_ttl = 255;
|
---|
| 321 | pIpHdr->ip_p = 0x11; /* UDP */
|
---|
| 322 | pIpHdr->ip_sum = 0;
|
---|
[10978] | 323 | pIpHdr->ip_src.u = 0;
|
---|
| 324 | pIpHdr->ip_dst.u = UINT32_C(0xffffffff); /* broadcast */
|
---|
[10983] | 325 | pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
|
---|
[10756] | 326 |
|
---|
| 327 | /* calc the UDP checksum. */
|
---|
[10983] | 328 | pUdpHdr->uh_sum = RTNetIPv4UDPChecksum(pIpHdr, pUdpHdr, pUdpHdr + 1);
|
---|
[10756] | 329 |
|
---|
| 330 | /* Ethernet */
|
---|
| 331 | memset(&pEthHdr->DstMac, 0xff, sizeof(pEthHdr->DstMac)); /* broadcast */
|
---|
| 332 | pEthHdr->SrcMac = *pSrcMac;
|
---|
[10978] | 333 | pEthHdr->EtherType = RT_H2BE_U16(RTNET_ETHERTYPE_IPV4); /* IP */
|
---|
[10756] | 334 |
|
---|
[11045] | 335 | doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pDhcpMsg + 1) - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
|
---|
[10751] | 336 | }
|
---|
| 337 |
|
---|
| 338 |
|
---|
[18457] | 339 | static uint16_t icmpChecksum(PRTNETICMPV4HDR pHdr, size_t cbHdr)
|
---|
[11684] | 340 | {
|
---|
[18457] | 341 | size_t cbLeft = cbHdr;
|
---|
[11684] | 342 | uint16_t *pbSrc = (uint16_t *)pHdr;
|
---|
| 343 | uint16_t oddByte = 0;
|
---|
| 344 | int cSum = 0;
|
---|
| 345 |
|
---|
| 346 | while (cbLeft > 1)
|
---|
| 347 | {
|
---|
| 348 | cSum += *pbSrc++;
|
---|
| 349 | cbLeft -= 2;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | if (cbLeft == 1)
|
---|
| 353 | {
|
---|
| 354 | *(uint16_t *)(&oddByte) = *(uint16_t *)pbSrc;
|
---|
| 355 | cSum += oddByte;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | cSum = (cSum >> 16) + (cSum & 0xffff);
|
---|
| 359 | cSum += (cSum >> 16);
|
---|
| 360 | uint16_t Result = ~cSum;
|
---|
| 361 | return Result;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 |
|
---|
[10751] | 365 | /**
|
---|
[11684] | 366 | * Does the rudimentary ping test with fixed destination and source IPs.
|
---|
| 367 | *
|
---|
| 368 | * @param hIf The interface handle.
|
---|
| 369 | * @param pSession The session.
|
---|
| 370 | * @param pBuf The shared interface buffer.
|
---|
| 371 | * @param pSrcMac The mac address to use as source.
|
---|
| 372 | * @param pFileRaw The file to write the raw data to (optional).
|
---|
| 373 | * @param pFileText The file to write a textual packet summary to (optional).
|
---|
| 374 | */
|
---|
| 375 | static void doPingTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCRTMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
|
---|
| 376 | {
|
---|
| 377 | uint8_t abFrame[4096];
|
---|
| 378 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)&abFrame[0];
|
---|
| 379 | PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
|
---|
| 380 | PRTNETICMPV4ECHO pIcmpEcho = (PRTNETICMPV4ECHO) (pIpHdr + 1);
|
---|
| 381 |
|
---|
| 382 | /*
|
---|
| 383 | * Create a simple ping request.
|
---|
| 384 | */
|
---|
| 385 | memset(&abFrame, 0, sizeof(abFrame));
|
---|
| 386 |
|
---|
| 387 | pIcmpEcho->Hdr.icmp_type = RTNETICMPV4_TYPE_ECHO_REQUEST;
|
---|
| 388 | pIcmpEcho->Hdr.icmp_code = 0;
|
---|
| 389 | pIcmpEcho->icmp_id = 0x06;
|
---|
| 390 | pIcmpEcho->icmp_seq = 0x05;
|
---|
| 391 | size_t cbPad = 56;
|
---|
| 392 | memset(&pIcmpEcho->icmp_data, '\0', cbPad);
|
---|
| 393 | pIcmpEcho->Hdr.icmp_cksum = icmpChecksum(&pIcmpEcho->Hdr, cbPad + 8);
|
---|
| 394 |
|
---|
| 395 | /* IP */
|
---|
| 396 | pIpHdr->ip_v = 4;
|
---|
| 397 | pIpHdr->ip_hl = sizeof(*pIpHdr) / sizeof(uint32_t);
|
---|
| 398 | pIpHdr->ip_tos = 0;
|
---|
[18457] | 399 | pIpHdr->ip_len = RT_H2BE_U16((uint16_t)(sizeof(*pIcmpEcho) + cbPad + sizeof(*pIpHdr)));
|
---|
[11684] | 400 | pIpHdr->ip_id = (uint16_t)RTRandU32();
|
---|
| 401 | pIpHdr->ip_off = 0;
|
---|
| 402 | pIpHdr->ip_ttl = 255;
|
---|
| 403 | pIpHdr->ip_p = 0x01; /*ICMP */
|
---|
| 404 | pIpHdr->ip_sum = 0;
|
---|
| 405 | pIpHdr->ip_src.u = UINT32_C(0x9701A8C0); /* 192.168.1.151 */
|
---|
| 406 | pIpHdr->ip_dst.u = UINT32_C(0xF9A344D0); /* 208.68.163.249 */
|
---|
| 407 | pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
|
---|
| 408 |
|
---|
| 409 | /* Ethernet */
|
---|
| 410 | memset(&pEthHdr->DstMac, 0xff, sizeof(pEthHdr->DstMac)); /* broadcast */
|
---|
| 411 |
|
---|
| 412 | pEthHdr->SrcMac = *pSrcMac;
|
---|
| 413 | #if 0 /* Enable with host's real Mac address for testing of the testcase. */
|
---|
| 414 | pEthHdr->SrcMac.au8[0] = 0x00;
|
---|
| 415 | pEthHdr->SrcMac.au8[1] = 0x1b;
|
---|
| 416 | pEthHdr->SrcMac.au8[2] = 0x24;
|
---|
| 417 | pEthHdr->SrcMac.au8[3] = 0xa0;
|
---|
| 418 | pEthHdr->SrcMac.au8[4] = 0x2f;
|
---|
| 419 | pEthHdr->SrcMac.au8[5] = 0xce;
|
---|
| 420 | #endif
|
---|
| 421 |
|
---|
| 422 | pEthHdr->EtherType = RT_H2BE_U16(RTNET_ETHERTYPE_IPV4); /* IP */
|
---|
| 423 |
|
---|
| 424 | doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pIcmpEcho + 1) + cbPad - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
[10747] | 429 | * Does packet sniffing for a given period of time.
|
---|
| 430 | *
|
---|
| 431 | * @param hIf The interface handle.
|
---|
| 432 | * @param pSession The session.
|
---|
| 433 | * @param pBuf The shared interface buffer.
|
---|
| 434 | * @param cMillies The time period, ms.
|
---|
| 435 | * @param pFileRaw The file to write the raw data to (optional).
|
---|
| 436 | * @param pFileText The file to write a textual packet summary to (optional).
|
---|
[10757] | 437 | * @param pSrcMac Out MAC address.
|
---|
[10747] | 438 | */
|
---|
| 439 | static void doPacketSniffing(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, uint32_t cMillies,
|
---|
[11157] | 440 | PRTSTREAM pFileRaw, PRTSTREAM pFileText, PCRTMAC pSrcMac)
|
---|
[10747] | 441 | {
|
---|
| 442 | /*
|
---|
| 443 | * The loop.
|
---|
| 444 | */
|
---|
| 445 | PINTNETRINGBUF pRingBuf = &pBuf->Recv;
|
---|
| 446 | for (;;)
|
---|
| 447 | {
|
---|
| 448 | /*
|
---|
| 449 | * Wait for a packet to become available.
|
---|
| 450 | */
|
---|
[10756] | 451 | uint64_t cElapsedMillies = (RTTimeNanoTS() - g_StartTS) / 1000000;
|
---|
[10747] | 452 | if (cElapsedMillies >= cMillies)
|
---|
| 453 | break;
|
---|
| 454 | INTNETIFWAITREQ WaitReq;
|
---|
| 455 | WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 456 | WaitReq.Hdr.cbReq = sizeof(WaitReq);
|
---|
| 457 | WaitReq.pSession = pSession;
|
---|
| 458 | WaitReq.hIf = hIf;
|
---|
| 459 | WaitReq.cMillies = cMillies - (uint32_t)cElapsedMillies;
|
---|
[20864] | 460 | int rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_WAIT, 0, &WaitReq.Hdr);
|
---|
[36470] | 461 | if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
|
---|
[10747] | 462 | break;
|
---|
| 463 | if (RT_FAILURE(rc))
|
---|
| 464 | {
|
---|
| 465 | g_cErrors++;
|
---|
| 466 | RTPrintf("tstIntNet-1: VMMR0_DO_INTNET_IF_WAIT returned %Rrc\n", rc);
|
---|
| 467 | break;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | /*
|
---|
| 471 | * Process the receive buffer.
|
---|
| 472 | */
|
---|
[26574] | 473 | PINTNETHDR pHdr;
|
---|
[28714] | 474 | while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)))
|
---|
[10747] | 475 | {
|
---|
[46904] | 476 | if (pHdr->u8Type == INTNETHDR_TYPE_FRAME)
|
---|
[10747] | 477 | {
|
---|
| 478 | size_t cbFrame = pHdr->cbFrame;
|
---|
[28714] | 479 | const void *pvFrame = IntNetHdrGetFramePtr(pHdr, pBuf);
|
---|
[26574] | 480 | uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
|
---|
[10747] | 481 |
|
---|
| 482 | if (pFileRaw)
|
---|
[10756] | 483 | PcapStreamFrame(pFileRaw, g_StartTS, pvFrame, cbFrame, 0xffff);
|
---|
[10747] | 484 |
|
---|
[10978] | 485 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
[10747] | 486 | if (pFileText)
|
---|
[10757] | 487 | RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n",
|
---|
[10747] | 488 | NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
|
---|
[11470] | 489 | cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
|
---|
[10757] | 490 | !memcmp(&pEthHdr->DstMac, pSrcMac, sizeof(*pSrcMac)) ? " Mine!" : "");
|
---|
[28025] | 491 | tstIntNetTestFrame(pvFrame, cbFrame, pFileText, false /*fGso*/);
|
---|
[10757] | 492 |
|
---|
| 493 | /* Loop for the DHCP reply. */
|
---|
| 494 | if ( cbFrame > 64
|
---|
[10978] | 495 | && RT_BE2H_U16(pEthHdr->EtherType) == 0x0800 /* EtherType == IP */)
|
---|
[10757] | 496 | {
|
---|
[10978] | 497 | PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)(pEthHdr + 1);
|
---|
| 498 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint32_t *)pIpHdr + pIpHdr->ip_hl);
|
---|
[10757] | 499 | if ( pIpHdr->ip_p == 0x11 /*UDP*/
|
---|
| 500 | && RT_BE2H_U16(pUdpHdr->uh_dport) == 68 /* bootp */
|
---|
| 501 | && RT_BE2H_U16(pUdpHdr->uh_sport) == 67 /* bootps */)
|
---|
| 502 | {
|
---|
[10978] | 503 | PCRTNETDHCP pDhcpMsg = (PCRTNETDHCP)(pUdpHdr + 1);
|
---|
[10757] | 504 | if ( pDhcpMsg->Op == 2 /* boot reply */
|
---|
| 505 | && pDhcpMsg->HType == 1 /* ethernet */
|
---|
[11157] | 506 | && pDhcpMsg->HLen == sizeof(RTMAC)
|
---|
[10757] | 507 | && (pDhcpMsg->XID == g_DhcpXID || !g_DhcpXID)
|
---|
| 508 | && !memcmp(&pDhcpMsg->CHAddr[0], pSrcMac, sizeof(*pSrcMac)))
|
---|
| 509 | {
|
---|
| 510 | g_fDhcpReply = true;
|
---|
| 511 | RTPrintf("tstIntNet-1: DHCP server reply! My IP: %d.%d.%d.%d\n",
|
---|
[10978] | 512 | pDhcpMsg->YIAddr.au8[0],
|
---|
| 513 | pDhcpMsg->YIAddr.au8[1],
|
---|
| 514 | pDhcpMsg->YIAddr.au8[2],
|
---|
| 515 | pDhcpMsg->YIAddr.au8[3]);
|
---|
[10757] | 516 | }
|
---|
| 517 | }
|
---|
[11684] | 518 | else if (pIpHdr->ip_p == 0x01) /* ICMP */
|
---|
| 519 | {
|
---|
| 520 | PRTNETICMPV4HDR pIcmpHdr = (PRTNETICMPV4HDR)(pIpHdr + 1);
|
---|
| 521 | PRTNETICMPV4ECHO pIcmpEcho = (PRTNETICMPV4ECHO)(pIpHdr + 1);
|
---|
| 522 | if ( pIcmpHdr->icmp_type == RTNETICMPV4_TYPE_ECHO_REPLY
|
---|
| 523 | && pIcmpEcho->icmp_seq == 0x05
|
---|
| 524 | && pIpHdr->ip_dst.u == UINT32_C(0x9701A8C0)
|
---|
| 525 | #if 0
|
---|
| 526 | /** Enable with the host's real Mac address for testing of the testcase.*/
|
---|
| 527 | && pEthHdr->DstMac.au8[0] == 0x00
|
---|
| 528 | && pEthHdr->DstMac.au8[1] == 0x1b
|
---|
| 529 | && pEthHdr->DstMac.au8[2] == 0x24
|
---|
| 530 | && pEthHdr->DstMac.au8[3] == 0xa0
|
---|
| 531 | && pEthHdr->DstMac.au8[4] == 0x2f
|
---|
| 532 | && pEthHdr->DstMac.au8[5] == 0xce
|
---|
| 533 | #else
|
---|
| 534 | && pEthHdr->DstMac.au16[0] == pSrcMac->au16[0]
|
---|
| 535 | && pEthHdr->DstMac.au16[1] == pSrcMac->au16[1]
|
---|
| 536 | && pEthHdr->DstMac.au16[2] == pSrcMac->au16[2]
|
---|
| 537 | #endif
|
---|
| 538 | )
|
---|
| 539 | {
|
---|
| 540 | g_fPingReply = true;
|
---|
| 541 | RTPrintf("tstIntNet-1: Ping reply! From %d.%d.%d.%d\n",
|
---|
| 542 | pIpHdr->ip_src.au8[0],
|
---|
| 543 | pIpHdr->ip_src.au8[1],
|
---|
| 544 | pIpHdr->ip_src.au8[2],
|
---|
| 545 | pIpHdr->ip_src.au8[3]);
|
---|
| 546 | }
|
---|
| 547 | else
|
---|
| 548 | RTPrintf("type=%d seq=%d dstmac=%.6Rhxs ip=%d.%d.%d.%d\n", pIcmpHdr->icmp_type, pIcmpEcho->icmp_seq,
|
---|
| 549 | &pEthHdr->DstMac, pIpHdr->ip_dst.au8[0], pIpHdr->ip_dst.au8[1], pIpHdr->ip_dst.au8[2], pIpHdr->ip_dst.au8[3]);
|
---|
| 550 | }
|
---|
[10757] | 551 | }
|
---|
[10747] | 552 | }
|
---|
[46904] | 553 | else if (pHdr->u8Type == INTNETHDR_TYPE_GSO)
|
---|
[28025] | 554 | {
|
---|
[28714] | 555 | PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, pBuf);
|
---|
[28025] | 556 | size_t cbFrame = pHdr->cbFrame;
|
---|
| 557 | if (PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(*pGso)))
|
---|
| 558 | {
|
---|
| 559 | const void *pvFrame = pGso + 1;
|
---|
| 560 | uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
|
---|
| 561 | cbFrame -= sizeof(pGso);
|
---|
| 562 |
|
---|
| 563 | if (pFileRaw)
|
---|
| 564 | PcapStreamGsoFrame(pFileRaw, g_StartTS, pGso, pvFrame, cbFrame, 0xffff);
|
---|
| 565 |
|
---|
| 566 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
| 567 | if (pFileText)
|
---|
| 568 | RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s [GSO]\n",
|
---|
| 569 | NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
|
---|
| 570 | cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
|
---|
| 571 | !memcmp(&pEthHdr->DstMac, pSrcMac, sizeof(*pSrcMac)) ? " Mine!" : "");
|
---|
| 572 | tstIntNetTestFrame(pvFrame, cbFrame, pFileText, true /*fGso*/);
|
---|
| 573 | }
|
---|
| 574 | else
|
---|
| 575 | {
|
---|
[56992] | 576 | RTPrintf("tstIntNet-1: Bad GSO frame: %.*Rhxs\n", sizeof(*pGso), pGso);
|
---|
[28025] | 577 | STAM_REL_COUNTER_INC(&pBuf->cStatBadFrames);
|
---|
| 578 | g_cErrors++;
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
[46904] | 581 | else if (pHdr->u8Type != INTNETHDR_TYPE_PADDING)
|
---|
[10747] | 582 | {
|
---|
[46904] | 583 | RTPrintf("tstIntNet-1: Unknown frame type %d\n", pHdr->u8Type);
|
---|
[26574] | 584 | STAM_REL_COUNTER_INC(&pBuf->cStatBadFrames);
|
---|
[10747] | 585 | g_cErrors++;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | /* Advance to the next frame. */
|
---|
[28714] | 589 | IntNetRingSkipFrame(pRingBuf);
|
---|
[10747] | 590 | }
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[10756] | 593 | uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
|
---|
[10747] | 594 | RTStrmPrintf(pFileText ? pFileText : g_pStdOut,
|
---|
| 595 | "%3RU64.%09u: stopped. cRecvs=%RU64 cbRecv=%RU64 cLost=%RU64 cOYs=%RU64 cNYs=%RU64\n",
|
---|
| 596 | NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
|
---|
[26574] | 597 | pBuf->Recv.cStatFrames.c,
|
---|
| 598 | pBuf->Recv.cbStatWritten.c,
|
---|
[10747] | 599 | pBuf->cStatLost.c,
|
---|
| 600 | pBuf->cStatYieldsOk.c,
|
---|
| 601 | pBuf->cStatYieldsNok.c
|
---|
| 602 | );
|
---|
[11045] | 603 | RTStrmPrintf(pFileText ? pFileText : g_pStdOut,
|
---|
| 604 | "%3RU64.%09u: cOtherPkts=%RU32 cArpPkts=%RU32 cIpv4Pkts=%RU32 cTcpPkts=%RU32 cUdpPkts=%RU32 cDhcpPkts=%RU32\n",
|
---|
| 605 | NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
|
---|
| 606 | g_cOtherPkts, g_cArpPkts, g_cIpv4Pkts, g_cTcpPkts, g_cUdpPkts, g_cDhcpPkts);
|
---|
[10747] | 607 | }
|
---|
| 608 |
|
---|
[48180] | 609 | #ifdef RT_OS_LINUX
|
---|
| 610 | #include <stdio.h>
|
---|
| 611 | #include <net/if.h>
|
---|
| 612 | #include <net/route.h>
|
---|
| 613 | /**
|
---|
| 614 | * Obtain the name of the interface used for default routing.
|
---|
| 615 | *
|
---|
| 616 | * NOTE: Copied from Main/src-server/linux/NetIf-linux.cpp
|
---|
| 617 | *
|
---|
| 618 | * @returns VBox status code.
|
---|
| 619 | *
|
---|
[74530] | 620 | * @param pszName The buffer where to put the name.
|
---|
| 621 | * @param cbName The buffer length.
|
---|
[48180] | 622 | */
|
---|
[74530] | 623 | static int getDefaultIfaceName(char *pszName, size_t cbName)
|
---|
[48180] | 624 | {
|
---|
| 625 | FILE *fp = fopen("/proc/net/route", "r");
|
---|
| 626 | char szBuf[1024];
|
---|
| 627 | char szIfName[17];
|
---|
| 628 | uint32_t uAddr;
|
---|
| 629 | uint32_t uGateway;
|
---|
| 630 | uint32_t uMask;
|
---|
| 631 | int iTmp;
|
---|
| 632 | unsigned uFlags;
|
---|
[10747] | 633 |
|
---|
[48180] | 634 | if (fp)
|
---|
| 635 | {
|
---|
| 636 | while (fgets(szBuf, sizeof(szBuf)-1, fp))
|
---|
| 637 | {
|
---|
| 638 | int n = sscanf(szBuf, "%16s %x %x %x %d %d %d %x %d %d %d\n",
|
---|
| 639 | szIfName, &uAddr, &uGateway, &uFlags, &iTmp, &iTmp, &iTmp,
|
---|
| 640 | &uMask, &iTmp, &iTmp, &iTmp);
|
---|
| 641 | if (n < 10 || !(uFlags & RTF_UP))
|
---|
| 642 | continue;
|
---|
| 643 |
|
---|
| 644 | if (uAddr == 0 && uMask == 0)
|
---|
| 645 | {
|
---|
| 646 | fclose(fp);
|
---|
[74530] | 647 | szIfName[sizeof(szIfName) - 1] = '\0';
|
---|
| 648 | return RTStrCopy(pszName, cbName, szIfName);
|
---|
[48180] | 649 | }
|
---|
| 650 | }
|
---|
| 651 | fclose(fp);
|
---|
| 652 | }
|
---|
| 653 | return VERR_INTERNAL_ERROR;
|
---|
| 654 | }
|
---|
| 655 | #endif /* RT_OS_LINUX */
|
---|
| 656 |
|
---|
| 657 |
|
---|
[51906] | 658 | /**
|
---|
| 659 | * Entry point.
|
---|
| 660 | */
|
---|
| 661 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
[1] | 662 | {
|
---|
[62996] | 663 | RT_NOREF(envp);
|
---|
| 664 |
|
---|
[1] | 665 | /*
|
---|
[10747] | 666 | * Init the runtime and parse the arguments.
|
---|
[1] | 667 | */
|
---|
[38636] | 668 | RTR3InitExe(argc, &argv, 0);
|
---|
[10747] | 669 |
|
---|
[17091] | 670 | static RTGETOPTDEF const s_aOptions[] =
|
---|
[10747] | 671 | {
|
---|
| 672 | { "--duration", 'd', RTGETOPT_REQ_UINT32 },
|
---|
| 673 | { "--file", 'f', RTGETOPT_REQ_STRING },
|
---|
[10806] | 674 | { "--interface", 'i', RTGETOPT_REQ_STRING },
|
---|
[11056] | 675 | { "--mac-sharing", 'm', RTGETOPT_REQ_NOTHING },
|
---|
[10764] | 676 | { "--network", 'n', RTGETOPT_REQ_STRING },
|
---|
[10747] | 677 | { "--promiscuous", 'p', RTGETOPT_REQ_NOTHING },
|
---|
| 678 | { "--recv-buffer", 'r', RTGETOPT_REQ_UINT32 },
|
---|
| 679 | { "--send-buffer", 's', RTGETOPT_REQ_UINT32 },
|
---|
| 680 | { "--sniffer", 'S', RTGETOPT_REQ_NOTHING },
|
---|
| 681 | { "--text-file", 't', RTGETOPT_REQ_STRING },
|
---|
| 682 | { "--xmit-test", 'x', RTGETOPT_REQ_NOTHING },
|
---|
[11684] | 683 | { "--ping-test", 'P', RTGETOPT_REQ_NOTHING },
|
---|
[10747] | 684 | };
|
---|
| 685 |
|
---|
| 686 | uint32_t cMillies = 1000;
|
---|
| 687 | PRTSTREAM pFileRaw = NULL;
|
---|
| 688 | #ifdef RT_OS_DARWIN
|
---|
| 689 | const char *pszIf = "en0";
|
---|
| 690 | #elif defined(RT_OS_LINUX)
|
---|
[48180] | 691 | char szIf[IFNAMSIZ+1] = "eth0"; /* Reasonable default */
|
---|
| 692 | /*
|
---|
| 693 | * Try to update the default interface by consulting the routing table.
|
---|
| 694 | * If we fail we still have our reasonable default.
|
---|
| 695 | */
|
---|
[74530] | 696 | getDefaultIfaceName(szIf, sizeof(szIf));
|
---|
[48180] | 697 | const char *pszIf = szIf;
|
---|
[11316] | 698 | #elif defined(RT_OS_SOLARIS)
|
---|
| 699 | const char* pszIf = "rge0";
|
---|
[10747] | 700 | #else
|
---|
| 701 | const char *pszIf = "em0";
|
---|
| 702 | #endif
|
---|
[11056] | 703 | bool fMacSharing = false;
|
---|
| 704 | const char *pszNetwork = "tstIntNet-1";
|
---|
[10747] | 705 | bool fPromiscuous = false;
|
---|
| 706 | uint32_t cbRecv = 0;
|
---|
| 707 | uint32_t cbSend = 0;
|
---|
| 708 | bool fSniffer = false;
|
---|
| 709 | PRTSTREAM pFileText = g_pStdOut;
|
---|
| 710 | bool fXmitTest = false;
|
---|
[11684] | 711 | bool fPingTest = false;
|
---|
[11157] | 712 | RTMAC SrcMac;
|
---|
[10751] | 713 | SrcMac.au8[0] = 0x08;
|
---|
| 714 | SrcMac.au8[1] = 0x03;
|
---|
| 715 | SrcMac.au8[2] = 0x86;
|
---|
| 716 | RTRandBytes(&SrcMac.au8[3], sizeof(SrcMac) - 3);
|
---|
[10747] | 717 |
|
---|
| 718 | int rc;
|
---|
| 719 | int ch;
|
---|
[17091] | 720 | RTGETOPTUNION Value;
|
---|
| 721 | RTGETOPTSTATE GetState;
|
---|
| 722 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
|
---|
| 723 | while ((ch = RTGetOpt(&GetState, &Value)))
|
---|
[10747] | 724 | switch (ch)
|
---|
| 725 | {
|
---|
| 726 | case 'd':
|
---|
| 727 | cMillies = Value.u32 * 1000;
|
---|
| 728 | if (cMillies / 1000 != Value.u32)
|
---|
| 729 | {
|
---|
| 730 | RTPrintf("tstIntNet-1: warning duration overflowed\n");
|
---|
| 731 | cMillies = UINT32_MAX - 1;
|
---|
| 732 | }
|
---|
| 733 | break;
|
---|
| 734 |
|
---|
| 735 | case 'f':
|
---|
| 736 | rc = RTStrmOpen(Value.psz, "w+b", &pFileRaw);
|
---|
| 737 | if (RT_FAILURE(rc))
|
---|
| 738 | {
|
---|
| 739 | RTPrintf("tstIntNet-1: Failed to creating \"%s\" for writing: %Rrc\n", Value.psz, rc);
|
---|
| 740 | return 1;
|
---|
| 741 | }
|
---|
| 742 | break;
|
---|
| 743 |
|
---|
| 744 | case 'i':
|
---|
| 745 | pszIf = Value.psz;
|
---|
| 746 | if (strlen(pszIf) >= INTNET_MAX_TRUNK_NAME)
|
---|
| 747 | {
|
---|
| 748 | RTPrintf("tstIntNet-1: Interface name is too long (max %d chars): %s\n", INTNET_MAX_TRUNK_NAME - 1, pszIf);
|
---|
| 749 | return 1;
|
---|
| 750 | }
|
---|
| 751 | break;
|
---|
| 752 |
|
---|
[11056] | 753 | case 'm':
|
---|
| 754 | fMacSharing = true;
|
---|
| 755 | break;
|
---|
| 756 |
|
---|
[10747] | 757 | case 'n':
|
---|
| 758 | pszNetwork = Value.psz;
|
---|
| 759 | if (strlen(pszNetwork) >= INTNET_MAX_NETWORK_NAME)
|
---|
| 760 | {
|
---|
| 761 | RTPrintf("tstIntNet-1: Network name is too long (max %d chars): %s\n", INTNET_MAX_NETWORK_NAME - 1, pszNetwork);
|
---|
| 762 | return 1;
|
---|
| 763 | }
|
---|
| 764 | break;
|
---|
| 765 |
|
---|
| 766 | case 'p':
|
---|
| 767 | fPromiscuous = true;
|
---|
| 768 | break;
|
---|
| 769 |
|
---|
| 770 | case 'r':
|
---|
| 771 | cbRecv = Value.u32;
|
---|
| 772 | break;
|
---|
| 773 |
|
---|
| 774 | case 's':
|
---|
| 775 | cbSend = Value.u32;
|
---|
| 776 | break;
|
---|
| 777 |
|
---|
| 778 | case 'S':
|
---|
| 779 | fSniffer = true;
|
---|
| 780 | break;
|
---|
| 781 |
|
---|
| 782 | case 't':
|
---|
| 783 | if (!*Value.psz)
|
---|
| 784 | pFileText = NULL;
|
---|
| 785 | else if (!strcmp(Value.psz, "-"))
|
---|
| 786 | pFileText = g_pStdOut;
|
---|
| 787 | else if (!strcmp(Value.psz, "!"))
|
---|
| 788 | pFileText = g_pStdErr;
|
---|
| 789 | else
|
---|
| 790 | {
|
---|
| 791 | rc = RTStrmOpen(Value.psz, "w", &pFileText);
|
---|
| 792 | if (RT_FAILURE(rc))
|
---|
| 793 | {
|
---|
| 794 | RTPrintf("tstIntNet-1: Failed to creating \"%s\" for writing: %Rrc\n", Value.psz, rc);
|
---|
| 795 | return 1;
|
---|
| 796 | }
|
---|
| 797 | }
|
---|
| 798 | break;
|
---|
| 799 |
|
---|
| 800 | case 'x':
|
---|
| 801 | fXmitTest = true;
|
---|
| 802 | break;
|
---|
| 803 |
|
---|
[11684] | 804 | case 'P':
|
---|
| 805 | fPingTest = true;
|
---|
| 806 | break;
|
---|
| 807 |
|
---|
[10747] | 808 | case 'h':
|
---|
[17772] | 809 | RTPrintf("syntax: tstIntNet-1 <options>\n"
|
---|
| 810 | "\n"
|
---|
| 811 | "Options:\n");
|
---|
| 812 | for (size_t i = 0; i < RT_ELEMENTS(s_aOptions); i++)
|
---|
| 813 | RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
|
---|
| 814 | RTPrintf("\n"
|
---|
| 815 | "Examples:\n"
|
---|
| 816 | " tstIntNet-1 -r 8192 -s 4096 -xS\n"
|
---|
| 817 | " tstIntNet-1 -n VBoxNetDhcp -r 4096 -s 4096 -i \"\" -xS\n");
|
---|
[10747] | 818 | return 1;
|
---|
[17772] | 819 |
|
---|
[26517] | 820 | case 'V':
|
---|
| 821 | RTPrintf("$Revision: 103431 $\n");
|
---|
| 822 | return 0;
|
---|
[10747] | 823 |
|
---|
| 824 | default:
|
---|
[26517] | 825 | return RTGetOptPrintError(ch, &Value);
|
---|
[10747] | 826 | }
|
---|
| 827 |
|
---|
[10663] | 828 | RTPrintf("tstIntNet-1: TESTING...\n");
|
---|
[1] | 829 |
|
---|
| 830 | /*
|
---|
[10663] | 831 | * Open the session, load ring-0 and issue the request.
|
---|
[1] | 832 | */
|
---|
[10663] | 833 | PSUPDRVSESSION pSession;
|
---|
[11794] | 834 | rc = SUPR3Init(&pSession);
|
---|
[10663] | 835 | if (RT_FAILURE(rc))
|
---|
[1] | 836 | {
|
---|
[11794] | 837 | RTPrintf("tstIntNet-1: SUPR3Init -> %Rrc\n", rc);
|
---|
[10663] | 838 | return 1;
|
---|
[1] | 839 | }
|
---|
| 840 |
|
---|
[103431] | 841 | static const char s_szVmmR0[] = "/../VMMR0.r0";
|
---|
| 842 | char szPath[RTPATH_MAX];
|
---|
| 843 | rc = RTPathExecDir(szPath, sizeof(szPath) - sizeof(s_szVmmR0));
|
---|
[10663] | 844 | if (RT_FAILURE(rc))
|
---|
[1] | 845 | {
|
---|
[19924] | 846 | RTPrintf("tstIntNet-1: RTPathExecDir -> %Rrc\n", rc);
|
---|
[10663] | 847 | return 1;
|
---|
[1] | 848 | }
|
---|
[103431] | 849 | memcpy(strchr(szPath, '\0'), s_szVmmR0, sizeof(s_szVmmR0));
|
---|
[1] | 850 |
|
---|
[41325] | 851 | char szAbsPath[RTPATH_MAX];
|
---|
| 852 | rc = RTPathAbs(szPath, szAbsPath, sizeof(szAbsPath));
|
---|
| 853 | if (RT_FAILURE(rc))
|
---|
| 854 | {
|
---|
| 855 | RTPrintf("tstIntNet-1: RTPathAbs -> %Rrc\n", rc);
|
---|
| 856 | return 1;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
[85506] | 859 | rc = SUPR3LoadVMM(szAbsPath, NULL);
|
---|
[10663] | 860 | if (RT_FAILURE(rc))
|
---|
[1] | 861 | {
|
---|
[41414] | 862 | RTPrintf("tstIntNet-1: SUPR3LoadVMM(\"%s\") -> %Rrc\n", szAbsPath, rc);
|
---|
[1] | 863 | return 1;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | /*
|
---|
[10663] | 867 | * Create the request, picking the network and trunk names from argv[2]
|
---|
| 868 | * and argv[1] if present.
|
---|
[1] | 869 | */
|
---|
[10663] | 870 | INTNETOPENREQ OpenReq;
|
---|
| 871 | OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 872 | OpenReq.Hdr.cbReq = sizeof(OpenReq);
|
---|
[10747] | 873 | OpenReq.pSession = pSession;
|
---|
[74530] | 874 | RTStrCopy(OpenReq.szNetwork, sizeof(OpenReq.szNetwork), pszNetwork);
|
---|
| 875 | RTStrCopy(OpenReq.szTrunk, sizeof(OpenReq.szTrunk), pszIf);
|
---|
[17404] | 876 | OpenReq.enmTrunkType = *pszIf ? kIntNetTrunkType_NetFlt : kIntNetTrunkType_WhateverNone;
|
---|
[11056] | 877 | OpenReq.fFlags = fMacSharing ? INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE : 0;
|
---|
[10747] | 878 | OpenReq.cbSend = cbSend;
|
---|
| 879 | OpenReq.cbRecv = cbRecv;
|
---|
[10663] | 880 | OpenReq.hIf = INTNET_HANDLE_INVALID;
|
---|
[1] | 881 |
|
---|
[10663] | 882 | /*
|
---|
| 883 | * Issue the request.
|
---|
| 884 | */
|
---|
| 885 | RTPrintf("tstIntNet-1: attempting to open/create network \"%s\" with NetFlt trunk \"%s\"...\n",
|
---|
| 886 | OpenReq.szNetwork, OpenReq.szTrunk);
|
---|
[10747] | 887 | RTStrmFlush(g_pStdOut);
|
---|
[20864] | 888 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_OPEN, 0, &OpenReq.Hdr);
|
---|
[10663] | 889 | if (RT_SUCCESS(rc))
|
---|
| 890 | {
|
---|
[10747] | 891 | RTPrintf("tstIntNet-1: successfully opened/created \"%s\" with NetFlt trunk \"%s\" - hIf=%#x\n",
|
---|
[10663] | 892 | OpenReq.szNetwork, OpenReq.szTrunk, OpenReq.hIf);
|
---|
[10747] | 893 | RTStrmFlush(g_pStdOut);
|
---|
[1] | 894 |
|
---|
[10747] | 895 | /*
|
---|
| 896 | * Get the ring-3 address of the shared interface buffer.
|
---|
| 897 | */
|
---|
[28711] | 898 | INTNETIFGETBUFFERPTRSREQ GetBufferPtrsReq;
|
---|
| 899 | GetBufferPtrsReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 900 | GetBufferPtrsReq.Hdr.cbReq = sizeof(GetBufferPtrsReq);
|
---|
| 901 | GetBufferPtrsReq.pSession = pSession;
|
---|
| 902 | GetBufferPtrsReq.hIf = OpenReq.hIf;
|
---|
| 903 | GetBufferPtrsReq.pRing3Buf = NULL;
|
---|
[28776] | 904 | GetBufferPtrsReq.pRing0Buf = NIL_RTR0PTR;
|
---|
[28711] | 905 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, 0, &GetBufferPtrsReq.Hdr);
|
---|
[10747] | 906 | if (RT_SUCCESS(rc))
|
---|
| 907 | {
|
---|
[28711] | 908 | PINTNETBUF pBuf = GetBufferPtrsReq.pRing3Buf;
|
---|
[10747] | 909 | RTPrintf("tstIntNet-1: pBuf=%p cbBuf=%d cbSend=%d cbRecv=%d\n",
|
---|
| 910 | pBuf, pBuf->cbBuf, pBuf->cbSend, pBuf->cbRecv);
|
---|
| 911 | RTStrmFlush(g_pStdOut);
|
---|
| 912 | if (fPromiscuous)
|
---|
| 913 | {
|
---|
| 914 | INTNETIFSETPROMISCUOUSMODEREQ PromiscReq;
|
---|
| 915 | PromiscReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 916 | PromiscReq.Hdr.cbReq = sizeof(PromiscReq);
|
---|
| 917 | PromiscReq.pSession = pSession;
|
---|
| 918 | PromiscReq.hIf = OpenReq.hIf;
|
---|
| 919 | PromiscReq.fPromiscuous = true;
|
---|
[20864] | 920 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, 0, &PromiscReq.Hdr);
|
---|
[10747] | 921 | if (RT_SUCCESS(rc))
|
---|
| 922 | RTPrintf("tstIntNet-1: interface in promiscuous mode\n");
|
---|
| 923 | }
|
---|
| 924 | if (RT_SUCCESS(rc))
|
---|
| 925 | {
|
---|
| 926 | /*
|
---|
[10846] | 927 | * Activate the interface.
|
---|
[10756] | 928 | */
|
---|
[10846] | 929 | INTNETIFSETACTIVEREQ ActiveReq;
|
---|
| 930 | ActiveReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
| 931 | ActiveReq.Hdr.cbReq = sizeof(ActiveReq);
|
---|
| 932 | ActiveReq.pSession = pSession;
|
---|
| 933 | ActiveReq.hIf = OpenReq.hIf;
|
---|
| 934 | ActiveReq.fActive = true;
|
---|
[20864] | 935 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SET_ACTIVE, 0, &ActiveReq.Hdr);
|
---|
[10846] | 936 | if (RT_SUCCESS(rc))
|
---|
| 937 | {
|
---|
| 938 | /*
|
---|
| 939 | * Start the stop watch, init the pcap file.
|
---|
| 940 | */
|
---|
| 941 | g_StartTS = RTTimeNanoTS();
|
---|
| 942 | if (pFileRaw)
|
---|
| 943 | PcapStreamHdr(pFileRaw, g_StartTS);
|
---|
[10756] | 944 |
|
---|
[10846] | 945 | /*
|
---|
| 946 | * Do the transmit test first and so we can sniff for the response.
|
---|
| 947 | */
|
---|
| 948 | if (fXmitTest)
|
---|
[11045] | 949 | doXmitTest(OpenReq.hIf, pSession, pBuf, &SrcMac, pFileRaw, pFileText);
|
---|
[10751] | 950 |
|
---|
[11684] | 951 | if (fPingTest)
|
---|
[11794] | 952 | doPingTest(OpenReq.hIf, pSession, pBuf, &SrcMac, pFileRaw, pFileText);
|
---|
| 953 |
|
---|
[10846] | 954 | /*
|
---|
| 955 | * Either enter sniffing mode or do a timeout thing.
|
---|
| 956 | */
|
---|
| 957 | if (fSniffer)
|
---|
[10757] | 958 | {
|
---|
[10846] | 959 | doPacketSniffing(OpenReq.hIf, pSession, pBuf, cMillies, pFileRaw, pFileText, &SrcMac);
|
---|
[11684] | 960 | if ( fXmitTest
|
---|
| 961 | && !g_fDhcpReply)
|
---|
[10846] | 962 | {
|
---|
[43876] | 963 | RTPrintf("tstIntNet-1: Error! The DHCP server didn't reply... (Perhaps you don't have one?)\n");
|
---|
[10846] | 964 | g_cErrors++;
|
---|
| 965 | }
|
---|
[11794] | 966 |
|
---|
[11684] | 967 | if ( fPingTest
|
---|
| 968 | && !g_fPingReply)
|
---|
| 969 | {
|
---|
[43876] | 970 | RTPrintf("tstIntNet-1: Error! No reply for ping request...\n");
|
---|
[11684] | 971 | g_cErrors++;
|
---|
| 972 | }
|
---|
[10757] | 973 | }
|
---|
[10846] | 974 | else
|
---|
| 975 | RTThreadSleep(cMillies);
|
---|
[10757] | 976 | }
|
---|
[10747] | 977 | else
|
---|
[10846] | 978 | {
|
---|
[20864] | 979 | RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
|
---|
[10846] | 980 | g_cErrors++;
|
---|
| 981 | }
|
---|
[10747] | 982 | }
|
---|
| 983 | else
|
---|
| 984 | {
|
---|
[20864] | 985 | RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
|
---|
[10747] | 986 | g_cErrors++;
|
---|
| 987 | }
|
---|
| 988 | }
|
---|
| 989 | else
|
---|
| 990 | {
|
---|
[28711] | 991 | RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS,) failed, rc=%Rrc\n", rc);
|
---|
[10747] | 992 | g_cErrors++;
|
---|
| 993 | }
|
---|
[1] | 994 | }
|
---|
| 995 | else
|
---|
| 996 | {
|
---|
[20864] | 997 | RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_OPEN,) failed, rc=%Rrc\n", rc);
|
---|
[1] | 998 | g_cErrors++;
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
[20864] | 1001 | SUPR3Term(false /*fForced*/);
|
---|
[1] | 1002 |
|
---|
[10747] | 1003 | /* close open files */
|
---|
| 1004 | if (pFileRaw)
|
---|
| 1005 | RTStrmClose(pFileRaw);
|
---|
| 1006 | if (pFileText && pFileText != g_pStdErr && pFileText != g_pStdOut)
|
---|
| 1007 | RTStrmClose(pFileText);
|
---|
| 1008 |
|
---|
[1] | 1009 | /*
|
---|
| 1010 | * Summary.
|
---|
| 1011 | */
|
---|
| 1012 | if (!g_cErrors)
|
---|
[10663] | 1013 | RTPrintf("tstIntNet-1: SUCCESS\n");
|
---|
[1] | 1014 | else
|
---|
[10663] | 1015 | RTPrintf("tstIntNet-1: FAILURE - %d errors\n", g_cErrors);
|
---|
[1] | 1016 |
|
---|
| 1017 | return !!g_cErrors;
|
---|
| 1018 | }
|
---|
| 1019 |
|
---|
[51906] | 1020 |
|
---|
| 1021 | #if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
|
---|
| 1022 | /**
|
---|
| 1023 | * Main entry point.
|
---|
| 1024 | */
|
---|
| 1025 | int main(int argc, char **argv, char **envp)
|
---|
| 1026 | {
|
---|
| 1027 | return TrustedMain(argc, argv, envp);
|
---|
| 1028 | }
|
---|
| 1029 | #endif
|
---|
| 1030 |
|
---|