VirtualBox

source: vbox/trunk/include/VBox/VBoxPktDmp.h@ 72101

Last change on this file since 72101 was 69107, checked in by vboxsync, 7 years ago

include/VBox/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: VBoxPktDmp.h 69107 2017-10-17 10:53:48Z vboxsync $ */
2/** @file
3 * VBoxPktDmp.h - Dump Ethernet frame into debug log.
4 */
5
6/*
7 * Copyright (C) 2016-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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
27#ifndef ___VBox_vboxpktdmp_h
28#define ___VBox_vboxpktdmp_h
29
30#include <iprt/net.h>
31#include <iprt/log.h>
32#if defined(LOG_ENABLED) && !defined(VBOX_DEVICE_STRUCT_TESTCASE)
33# include <iprt/asm.h>
34#endif
35
36
37DECLINLINE(const char *) vboxEthTypeStr(uint16_t uType)
38{
39 switch (uType)
40 {
41 case RTNET_ETHERTYPE_IPV4: return "IP";
42 case RTNET_ETHERTYPE_IPV6: return "IPv6";
43 case RTNET_ETHERTYPE_ARP: return "ARP";
44 }
45 return "unknown";
46}
47
48
49DECLHIDDEN(void) vboxEthPacketDump(const char *pcszInstance, const char *pcszText, const uint8_t *pcPacket, uint32_t cb)
50{
51#if defined(LOG_ENABLED) && !defined(VBOX_DEVICE_STRUCT_TESTCASE)
52 AssertReturnVoid(cb >= 14);
53
54 const uint8_t *pHdr = pcPacket;
55 const uint8_t *pEnd = pcPacket + cb;
56 AssertReturnVoid(pEnd - pHdr >= 14);
57 uint16_t uEthType = RT_N2H_U16(*(uint16_t*)(pHdr+12));
58 Log2(("%s: %s (%d bytes), %RTmac => %RTmac, EthType=%s(0x%x)\n", pcszInstance,
59 pcszText, cb, pHdr+6, pHdr, vboxEthTypeStr(uEthType), uEthType));
60 pHdr += sizeof(RTNETETHERHDR);
61 if (uEthType == RTNET_ETHERTYPE_VLAN)
62 {
63 AssertReturnVoid(pEnd - pHdr >= 4);
64 uEthType = RT_N2H_U16(*(uint16_t*)(pHdr+2));
65 Log2((" + VLAN: id=%d EthType=%s(0x%x)\n", RT_N2H_U16(*(uint16_t*)(pHdr)) & 0xFFF,
66 vboxEthTypeStr(uEthType), uEthType));
67 pHdr += 2 * sizeof(uint16_t);
68 }
69 uint8_t uProto = 0xFF;
70 switch (uEthType)
71 {
72 case RTNET_ETHERTYPE_IPV6:
73 AssertReturnVoid(pEnd - pHdr >= 40);
74 uProto = pHdr[6];
75 Log2((" + IPv6: %RTnaipv6 => %RTnaipv6\n", pHdr+8, pHdr+24));
76 pHdr += 40;
77 break;
78 case RTNET_ETHERTYPE_IPV4:
79 AssertReturnVoid(pEnd - pHdr >= 20);
80 uProto = pHdr[9];
81 Log2((" + IP: %RTnaipv4 => %RTnaipv4\n", *(uint32_t*)(pHdr+12), *(uint32_t*)(pHdr+16)));
82 pHdr += (pHdr[0] & 0xF) * 4;
83 break;
84 case RTNET_ETHERTYPE_ARP:
85 AssertReturnVoid(pEnd - pHdr >= 28);
86 AssertReturnVoid(RT_N2H_U16(*(uint16_t*)(pHdr+2)) == RTNET_ETHERTYPE_IPV4);
87 switch (RT_N2H_U16(*(uint16_t*)(pHdr+6)))
88 {
89 case 1: /* ARP request */
90 Log2((" + ARP-REQ: who-has %RTnaipv4 tell %RTnaipv4\n",
91 *(uint32_t*)(pHdr+24), *(uint32_t*)(pHdr+14)));
92 break;
93 case 2: /* ARP reply */
94 Log2((" + ARP-RPL: %RTnaipv4 is-at %RTmac\n",
95 *(uint32_t*)(pHdr+14), pHdr+8));
96 break;
97 default:
98 Log2((" + ARP: unknown op %d\n", RT_N2H_U16(*(uint16_t*)(pHdr+6))));
99 break;
100 }
101 break;
102 /* There is no default case as uProto is initialized with 0xFF */
103 }
104 while (uProto != 0xFF)
105 {
106 switch (uProto)
107 {
108 case 0: /* IPv6 Hop-by-Hop option*/
109 case 60: /* IPv6 Destination option*/
110 case 43: /* IPv6 Routing option */
111 case 44: /* IPv6 Fragment option */
112 Log2((" + IPv6 option (%d): <not implemented>\n", uProto));
113 uProto = pHdr[0];
114 pHdr += pHdr[1] * 8 + 8; /* Skip to the next extension/protocol */
115 break;
116 case 51: /* IPv6 IPsec AH */
117 Log2((" + IPv6 IPsec AH: <not implemented>\n"));
118 uProto = pHdr[0];
119 pHdr += (pHdr[1] + 2) * 4; /* Skip to the next extension/protocol */
120 break;
121 case 50: /* IPv6 IPsec ESP */
122 /* Cannot decode IPsec, fall through */
123 Log2((" + IPv6 IPsec ESP: <not implemented>\n"));
124 uProto = 0xFF;
125 break;
126 case 59: /* No Next Header */
127 Log2((" + IPv6 No Next Header\n"));
128 uProto = 0xFF;
129 break;
130 case 58: /* IPv6-ICMP */
131 switch (pHdr[0])
132 {
133 case 1: Log2((" + IPv6-ICMP: destination unreachable, code %d\n", pHdr[1])); break;
134 case 128: Log2((" + IPv6-ICMP: echo request\n")); break;
135 case 129: Log2((" + IPv6-ICMP: echo reply\n")); break;
136 default: Log2((" + IPv6-ICMP: unknown type %d, code %d\n", pHdr[0], pHdr[1])); break;
137 }
138 uProto = 0xFF;
139 break;
140 case 1: /* ICMP */
141 switch (pHdr[0])
142 {
143 case 0: Log2((" + ICMP: echo reply\n")); break;
144 case 8: Log2((" + ICMP: echo request\n")); break;
145 case 3: Log2((" + ICMP: destination unreachable, code %d\n", pHdr[1])); break;
146 default: Log2((" + ICMP: unknown type %d, code %d\n", pHdr[0], pHdr[1])); break;
147 }
148 uProto = 0xFF;
149 break;
150 case 6: /* TCP */
151 Log2((" + TCP: src=%d dst=%d seq=%x ack=%x\n",
152 RT_N2H_U16(*(uint16_t*)(pHdr)), RT_N2H_U16(*(uint16_t*)(pHdr+2)),
153 RT_N2H_U32(*(uint32_t*)(pHdr+4)), RT_N2H_U32(*(uint32_t*)(pHdr+8))));
154 uProto = 0xFF;
155 break;
156 case 17: /* UDP */
157 Log2((" + UDP: src=%d dst=%d\n",
158 RT_N2H_U16(*(uint16_t*)(pHdr)), RT_N2H_U16(*(uint16_t*)(pHdr+2))));
159 uProto = 0xFF;
160 break;
161 default:
162 Log2((" + Unknown: proto=0x%x\n", uProto));
163 uProto = 0xFF;
164 break;
165 }
166 }
167 Log3(("%.*Rhxd\n", cb, pcPacket));
168#else
169 RT_NOREF4(pcszInstance, pcszText, pcPacket, cb);
170#endif
171}
172
173#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette