2 * TAP-Win32/TAP-Win64 -- A kernel driver to provide virtual tap
3 * device functionality on Windows.
5 * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
7 * This source code is Copyright (C) 2002-2009 OpenVPN Technologies, Inc.,
8 * and is released under the GPL version 2 (see below), however due
9 * to the extra costs of supporting Windows Vista, OpenVPN Solutions
10 * LLC reserves the right to change the terms of the TAP-Win32/TAP-Win64
11 * license for versions 9.1 and higher prior to the official release of
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program (see the file COPYING included with this
25 * distribution); if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 const char *g_LastErrorFilename
;
34 int g_LastErrorLineNumber
;
41 NewlineExists (const char *str
, int len
)
45 const char c
= *str
++;
55 MyDebugInit (unsigned int bufsiz
)
57 NdisZeroMemory (&g_Debug
, sizeof (g_Debug
));
58 g_Debug
.text
= (char *) MemAlloc (bufsiz
, FALSE
);
60 g_Debug
.capacity
= bufsiz
;
67 MemFree (g_Debug
.text
, g_Debug
.capacity
);
68 NdisZeroMemory (&g_Debug
, sizeof (g_Debug
));
72 MyDebugPrint (const unsigned char* format
, ...)
74 if (g_Debug
.text
&& g_Debug
.capacity
> 0 && CAN_WE_PRINT
)
77 ACQUIRE_MUTEX_ADAPTIVE (&g_Debug
.lock
, owned
);
80 const int remaining
= (int)g_Debug
.capacity
- (int)g_Debug
.out
;
88 va_start (args
, format
);
89 status
= RtlStringCchVPrintfExA (g_Debug
.text
+ g_Debug
.out
,
93 STRSAFE_NO_TRUNCATION
| STRSAFE_IGNORE_NULLS
,
98 if (status
== STATUS_SUCCESS
)
99 g_Debug
.out
= (unsigned int) (end
- g_Debug
.text
);
101 g_Debug
.error
= TRUE
;
104 g_Debug
.error
= TRUE
;
106 RELEASE_MUTEX (&g_Debug
.lock
);
109 g_Debug
.error
= TRUE
;
114 GetDebugLine (char *buf
, const int len
)
116 static const char *truncated
= "[OUTPUT TRUNCATED]\n";
119 NdisZeroMemory (buf
, len
);
121 if (g_Debug
.text
&& g_Debug
.capacity
> 0)
124 ACQUIRE_MUTEX_ADAPTIVE (&g_Debug
.lock
, owned
);
129 if (g_Debug
.error
|| NewlineExists (g_Debug
.text
+ g_Debug
.in
, (int)g_Debug
.out
- (int)g_Debug
.in
))
131 while (i
< (len
- 1) && g_Debug
.in
< g_Debug
.out
)
133 const char c
= g_Debug
.text
[g_Debug
.in
++];
144 if (g_Debug
.in
== g_Debug
.out
)
146 g_Debug
.in
= g_Debug
.out
= 0;
149 const unsigned int tlen
= strlen (truncated
);
150 if (tlen
< g_Debug
.capacity
)
152 NdisMoveMemory (g_Debug
.text
, truncated
, tlen
+1);
155 g_Debug
.error
= FALSE
;
162 RELEASE_MUTEX (&g_Debug
.lock
);
169 MyAssert (const unsigned char *file
, int line
)
171 DEBUGP (("MYASSERT failed %s/%d\n", file
, line
));
172 KeBugCheckEx (0x0F00BABA,
180 PrMac (const MACADDR mac
)
182 DEBUGP (("%x:%x:%x:%x:%x:%x",
183 mac
[0], mac
[1], mac
[2],
184 mac
[3], mac
[4], mac
[5]));
188 PrIP (IPADDR ip_addr
)
190 const unsigned char *ip
= (const unsigned char *) &ip_addr
;
192 DEBUGP (("%d.%d.%d.%d",
193 ip
[0], ip
[1], ip
[2], ip
[3]));
197 PrIPProto (int proto
)
215 DumpARP (const char *prefix
, const ARP_PACKET
*arp
)
217 DEBUGP (("%s ARP src=", prefix
));
218 PrMac (arp
->m_MAC_Source
);
220 PrMac (arp
->m_MAC_Destination
);
221 DEBUGP ((" OP=0x%04x",
222 (int)ntohs(arp
->m_ARP_Operation
)));
223 DEBUGP ((" M=0x%04x(%d)",
224 (int)ntohs(arp
->m_MAC_AddressType
),
225 (int)arp
->m_MAC_AddressSize
));
226 DEBUGP ((" P=0x%04x(%d)",
227 (int)ntohs(arp
->m_PROTO_AddressType
),
228 (int)arp
->m_PROTO_AddressSize
));
230 DEBUGP ((" MacSrc="));
231 PrMac (arp
->m_ARP_MAC_Source
);
232 DEBUGP ((" MacDest="));
233 PrMac (arp
->m_ARP_MAC_Destination
);
235 DEBUGP ((" IPSrc="));
236 PrIP (arp
->m_ARP_IP_Source
);
237 DEBUGP ((" IPDest="));
238 PrIP (arp
->m_ARP_IP_Destination
);
245 UCHAR payload
[DEFAULT_PACKET_LOOKAHEAD
];
249 DumpPacket2 (const char *prefix
,
250 const ETH_HEADER
*eth
,
251 const unsigned char *data
,
254 struct ethpayload
*ep
= (struct ethpayload
*) MemAlloc (sizeof (struct ethpayload
), TRUE
);
257 if (len
> DEFAULT_PACKET_LOOKAHEAD
)
258 len
= DEFAULT_PACKET_LOOKAHEAD
;
260 NdisMoveMemory (ep
->payload
, data
, len
);
261 DumpPacket (prefix
, (unsigned char *) ep
, sizeof (ETH_HEADER
) + len
);
262 MemFree (ep
, sizeof (struct ethpayload
));
267 DumpPacket (const char *prefix
,
268 const unsigned char *data
,
271 const ETH_HEADER
*eth
= (const ETH_HEADER
*) data
;
272 const IPHDR
*ip
= (const IPHDR
*) (data
+ sizeof (ETH_HEADER
));
274 if (len
< sizeof (ETH_HEADER
))
276 DEBUGP (("%s TRUNCATED PACKET LEN=%d\n", prefix
, len
));
281 if (len
>= sizeof (ARP_PACKET
) && eth
->proto
== htons (ETH_P_ARP
))
283 DumpARP (prefix
, (const ARP_PACKET
*) data
);
288 if (len
>= (sizeof (IPHDR
) + sizeof (ETH_HEADER
))
289 && eth
->proto
== htons (ETH_P_IP
)
290 && IPH_GET_VER (ip
->version_len
) == 4)
292 const int hlen
= IPH_GET_LEN (ip
->version_len
);
293 const int blen
= len
- sizeof (ETH_HEADER
);
296 DEBUGP (("%s IPv4 %s[%d]", prefix
, PrIPProto (ip
->protocol
), len
));
298 if (!(ntohs (ip
->tot_len
) == blen
&& hlen
<= blen
))
305 if (ip
->protocol
== IPPROTO_TCP
306 && blen
- hlen
>= (sizeof (TCPHDR
)))
308 const TCPHDR
*tcp
= (TCPHDR
*) (data
+ sizeof (ETH_HEADER
) + hlen
);
311 DEBUGP ((":%d", ntohs (tcp
->source
)));
314 DEBUGP ((":%d", ntohs (tcp
->dest
)));
319 else if ((ntohs (ip
->frag_off
) & IP_OFFMASK
) == 0
320 && ip
->protocol
== IPPROTO_UDP
321 && blen
- hlen
>= (sizeof (UDPHDR
)))
323 const UDPHDR
*udp
= (UDPHDR
*) (data
+ sizeof (ETH_HEADER
) + hlen
);
326 if ((udp
->dest
== htons (BOOTPC_PORT
) || udp
->dest
== htons (BOOTPS_PORT
))
327 && blen
- hlen
>= (sizeof (UDPHDR
) + sizeof (DHCP
)))
329 const DHCP
*dhcp
= (DHCP
*) (data
331 + sizeof (ETH_HEADER
)
335 - sizeof (ETH_HEADER
)
343 DumpDHCP (eth
, ip
, udp
, dhcp
, optlen
);
351 DEBUGP ((":%d", ntohs (udp
->source
)));
354 DEBUGP ((":%d", ntohs (udp
->dest
)));
361 DEBUGP ((" ipproto=%d ", ip
->protocol
));
372 DEBUGP (("%s ??? src=", prefix
));
376 DEBUGP ((" proto=0x%04x len=%d\n",
377 (int) ntohs(eth
->proto
),