Commit Graph
104 Commits
Author SHA1 Message Date
basil00 29c6fda167 Improve & expand WinDivert test suite.
- Include tests for random*.
- Include latency timings in microseconds.
- More tests.
2018-11-19 07:21:54 +08:00
basil00 edbe5ea942 Optimize based on event expressions.
E.g., if "event == CONNECT" then do not install
a callout for LISTEN, etc.
2018-11-15 07:58:44 +08:00
basil00 67ad628a1f Add pseudo-random pkt matching to WinDivert (#156)
- Adds 3 new "pseudo-random" filter expressions:
  * random8 (8bit)
  * random16 (16bit)
  * random32 (32bit)
  Not really "random", but these values are based
  on a hash of the packet headers & timestamp.
  These are useful for traffic sampling, e.g.:
  "random8 < 100" matches ~39% of all packets.
- Add new WinDivertHelperHashPacket() helper
  function.  The algorithm is an xxHash variant.
2018-11-14 09:40:17 +08:00
basil00 312050accb Add a new WinDivertShutdown() function (#156).
This function allows the user application to
"shutdown" a handle in anticipation of a
call to WinDivertClose().  When a handle has
been shutdown, new packets will no longer be
queued.  However, existing queued packets can
still be read by WinDivertRecv().  When the
queue is emptied, WinDivertRecv() will fail
with ERROR_NO_DATA.

The WinDivertShutdown() function is analogous
to the shutdown() socket function.

This change also simplifies events.
2018-11-11 09:44:24 +08:00
basil00 0a74b5528c Update driver and inf version information. 2018-11-09 07:29:05 +08:00
basil00 084c78e4af Code cleanup & improve const correctness in API. 2018-11-09 07:21:56 +08:00
basil00 33a461eef6 Add packet/payload matching to WinDivert (#156).
Adds the follow terms to the filter language:
- packet[idx]: 8bit packet value at idx.
- packet16[idx]: 16bit packet value at idx.
- packet32[idx]: 32bit packet value at idx.
- PROTO.Payload[idx]: 8bit payload value at idx.
- PROTO.Payload16[idx]: 16bit payload value at
  idx.
- PROTO.Payload32[idx]: 32bit payload value at
  idx.
where PROTO is {tcp,udp}.

16 and 32bit values assume network byte ordering.

The index may be:
- An undecorated integer, in which case the
  packet or payload is treated as a 8/16/32bit
  value array, similar to C arrays.
- A 'b' decorated integer, (e.g, packet32[17b]),
  in which case the integer is interpreted as a
  byte offset.
- A negative (un)decorated integer, in which
  case indexing begins at the *end* of the
  packet or payload.  E.g., packet32[-1] is the
  last 32bits of the packet.

If the index is out-of-bounds, then the test
is deemed to be equivalent to "false".
2018-11-07 07:54:32 +08:00
basil00 1ccf486132 Calculate full checksums for reinjected packets. 2018-11-04 08:47:39 +08:00
basil00 4f7ff88664 Add batched recv/send to WinDivert (#156).
This change allows multiple packets to be
received/sent at once, reducing overheads.  To
exploit this feature, applications need to use
the WinDivertRecvEx()/WinDivertSendEx()
functions with the new addrLen parameter.  The
passthru example has been modified to use
batching.

The DEBUG flag has been retired since it
conflicts with batching, and was not very
useful anyway.
2018-11-03 08:40:34 +08:00
basil00 3966ec1287 Add a WINDIVERT_FLAG_PARTIAL flag.
This suppresses ERROR_INSUFFICIENT_BUFFER errors
if the passed buffer is too small.  Applications
can set this flag if they want to do partial
reads.
2018-10-30 08:56:43 +08:00
basil00 fc868feb3e Add type-safe get-fixed-val routines.
This prevents possible bsods when values are
missing.  Also add missing windivert_shared.c
file.
2018-10-30 05:36:44 +08:00
basil00 de4245f95e Event & REFLECT layer fixes.
- Make event bitwise-OR-able.
- Simplify event names.
- Fix REFLECT serialization.
  (setting AutomaticSerialization=TRUE does not
   work.)
- Preallocate REFLECT memory.
2018-10-29 05:40:03 +08:00
basil00 316765aa92 Fix impostor packet checksums. 2018-10-27 06:52:14 +08:00
basil00 86db54b400 Move pseduo checksum calculation inside driver.
This means WinDivertSend() no longer needs to
modify the packet and address buffers.  See #134.
2018-10-26 10:38:35 +08:00
basil00 34e8f96910 Consistent naming for DATA structs. 2018-10-26 06:57:40 +08:00
basil00 5aa4fc3c46 Enable NX flag for MmGetSystemAddressForMdlSafe.
See discussion in #128.
2018-10-26 06:43:49 +08:00
basil00 fd75e6d42a Add a new SOCKET layer to WinDivert (see #156).
Adds a new SOCKET layer for monitoring socket
events, such as BIND, LISTEN, CONNECT,
and ACCEPT.  These events occur before the
flow is established (and unlike the FLOW layer)
it is possible to block/drop SOCKET events.

Otherwise, this layer is very similar to the
FLOW layer, so the PID is available and injection
is not possible.  This commit should be
considered to be a WIP.

Other changes:
- New socketdump.exe sample.  Prints socket
  events.
- Improve IPv6 address parsing.
- New helper functions:
  * WinDivertHelperFormatIPv4Address
  * WinDivertHelperFormatIPv6Address
- Fix REFLECT bugs.
- The network 5-tuple can now be accessed at
  the NETWORK layer.
- Various cleanups.
2018-10-25 10:25:41 +08:00
basil00 2ab1ddf1d2 Make it possible to filter based on events.
E.g., "event == FLOW_ESTABLISHED".
2018-10-22 08:38:12 +08:00
basil00 f0dd1224b1 Add a new REFLECT layer to WinDivert (see #156).
Adds a new REFLECT layer for monitoring WinDivert
handles.  This includes:
- three new REFLECT events: ESTABLISHED, OPEN and
  CLOSE;
- modifying the ADDRESS for REFLECT data:
  open time, process-id, layer, flags, and
  priority of the opened handle; and
- allowing WinDivertRecv() to read a
  representation of the opened filter.

This change also includes a new "object"
representation for WinDivert filter strings.
The API has been updated as follows:
- WinDivertHelperCompileFilter (replaces
  CheckFilter) compiles filter strings into the
  object form; and
- WinDivertHelperFormatFilter can "decompile" an
  object back into a human-readable filter
  string.

Other:
- New NO_INSTALL flag.
- New windivertctl.exe sample program.
2018-10-21 10:12:19 +08:00
basil00 6250568974 Add a new FLOW layer into WinDivert.
Adds a new WINDIVERT_LAYER_FLOW layer that can:
- Track when flows are established
- Track when existing flows are deleted
2018-09-30 09:07:16 +08:00
basil00 1543e958e6 Dual-license WinDivert under the LGPLv3 and GPLv2.
This allows projects that are strictly "GPLv2
only" to use WinDivert without licensing issues.
See the FSF's compatibility matrix here:
https://www.gnu.org/licenses/gpl-faq.en.html

This change does not affect existing projects
that can continue to use the LGPLv3 as before.

Also, bump the WinDivert version to 1.4.1.
2018-04-04 18:41:45 +08:00
basil00 bef2240d9e Do not thread the impostor flag.
Only packets directly injected by other drivers
are considered impostor.  This is still sufficient
to prevent infinite loops.
2018-01-13 16:27:20 +08:00
basil00 53845fa8a9 Fix #107
This issue was caused by an uninitialized variable
inside the driver.  It sometimes resulted in a
junk error code returned for the "false" filter.
2018-01-13 08:08:37 +08:00
basil00 21f1e1a71e Revert WinDivert back to inline packet processing.
This hardens WinDivert against other drivers that
attempt to modify packets after they were blocked
& absorbed by WinDivert, leading to BSODs.  This
is also a fix for issue #110.
2018-01-08 08:11:10 +08:00
basil00 fb68612176 Cleanup protocol header definitions.
Now use a single version.
Also prepare for 1.4.0-rc release.
2017-11-21 09:16:06 +08:00
basil00 dbb6b0fe60 Invert the checksum flags.
Zeroing the addr will default to full checksums.
2017-11-14 20:20:18 +08:00
basil00 80b31815b2 Implement "impostor" packets.
WinDivert will now mark any packet injected by
another driver as an "impostor", meaning that it
did not originate from the network.  Changes are:
- User programs may filter impostor packets.
- WinDivertSend() automatically decrements the TTL
  for imposter packets, see #41.
2017-11-13 09:38:06 +08:00
basil00 aea3a3a858 Add support for pseudo IP/TCP/UDP checksums.
Most NIC cards support checksum offloading,
meaning that it is not necessary to calculate the
full IP/TCP/UDP checksums for injected packets.
WINDIVERT_ADDRESS has been extended to include 3
extra flags that indicate if the packet has full
or pseudo checksums.  This is a WIP.
2017-11-09 22:11:13 +08:00
basil00 bbf6a34aa6 Add Loopback and Timestamp fields to addresses.
- Loopback=1 for loopback packets, 0 otherwise.
- Timestamp is the time when packet captured.
2017-11-05 12:12:19 +08:00
basil00 9ae77eb7a5 Loopback packets are now considered outbound only
Workaround for #17
2017-11-01 20:47:45 +08:00
basil00 4c4f586f4d Add WINDIVERT_FLAG_DEBUG for WinDivertOpen() 2017-10-31 10:53:06 +08:00
basil00 d9fbc813f4 Refactor timeout logic. 2017-10-24 20:22:47 +08:00
basil00 b2fd7eb891 Bump version to 1.4 2017-10-22 21:07:06 +08:00
basil00 4ae5101d56 Reinject queued packets on WinDivertClose() 2017-10-22 09:40:50 +08:00
basil00 65f366820a Fix context access without lock. 2017-10-21 17:53:15 +08:00
basil00 4d68983d25 Ensure that the work queue length is bounded. 2017-10-20 08:17:27 +08:00
basil00 c4575b7059 Prevent block-clone-inject infinite loops.
- A partial fix for #41
- Decrements the TTL for reinjected packets.
- If (TTL==0), WinDivertRecv() will fail with:
  ERROR_HOST_UNREACHABLE = 1232
  which is better than looping.
2017-10-18 15:47:50 +08:00
basil00 f5f3559ae9 Various improvements
- Support NonPagedPoolNx for Windows 8, 10.
- More aggressive context locking
- Set MAXWORKERS=1 (seems to perform best).
2017-10-16 22:56:38 +08:00
basil00 d778639cbe Optimizations & bug fixes.
- Immediately reinject in SNIFF mode.
- Use a NET_BUFFER pool for cloned packets.
- Fix bug caused by pool mix up for cloned packets.
- Copy NET_BUFFER_LIST info for non-matching packets.
- Keep reference to original NET_BUFFER_LIST after cloning.
  (might not be necessary, but some sample drivers do).
2017-10-13 06:47:15 +08:00
basil00 11b9a74341 Queued packets now use the PagedPool.
This is possible since IRQL=PASSIVE_LEVEL.
2017-10-11 20:44:01 +08:00
basil00 2e1bfa8ca5 WinDivert driver overhaul.
This is a major update designed to modernize the WinDivert driver, including
optimizations, design improvements and bug fixes.  The new version has not
been fully tested and should be considered **UNSTABLE**.

- Most of the packet processing is now (almost) fully out-of-band.  This is a
  good since the classify function runs at DISPATCH_LEVEL.  The driver will
  still try to match at least one packet before moving the work out-of-band.
- Re-injected non-matching packets are now clones rather than copies.
- Queued packets are still copied.  This is because the driver should avoid
  keeping a reference to the original packet for very long.  Since we do not
  trust the user application to handle the packet in a timely fashion, it is
  better to copy rather than keep a reference.  That said, the driver now
  implements an optimization where it will service a read request immediately
  if possible (saving 1 packet copy).
- SNIFF mode also now works differently.  Previously, SNIFF mode would not
  block the original packet.  However, this is problematic under the new
  design since WinDivert cannot permit the packet and retain a reference to it
  at the same time.  The new version will block & absorb the original packet
  and re-inject a clone out-of-band.
- Packet time management has been replaced.  Previously, a timer was used to
  periodically wake up a function that would sweep away expired packets.  The
  new version explicitly timestamps every packet, and expired packets are
  cleaned up by the read service routine.
- The context->filter is now deallocated in the destroy callback to avoid
  possible a race condition with the callout function.  It is unclear if this
  is really necessary, however.
2017-10-10 23:14:37 +08:00
basil00 910a2e3050 Add new WINDIVERT_PARAM_QUEUE_SIZE parameter.
Controls the number of bytes used by the packet queue.
2017-10-01 15:33:58 +08:00
basil00 e69cc09cce Update WinDivert sublayer GUIDs for version 1.3. 2017-09-24 16:24:37 +08:00
basil00 7b7d92cd26 Bump version to WinDivert 1.3.0
This prevents the old 1.2 driver from being used by new 1.3 software.

Also, clean up the driver code.
2017-09-24 15:48:38 +08:00
basil00 200da2fba1 Complete WinDivertSend() IO requests immediately.
Currently, WinDivert waits until injection completes before completing the
corresponding IO request.  However, packet injection may take an arbitrarily
long time, such as waiting for a user-mode application.

The new version completes the IO request immediately, provided the call to the
corresponding WFP packet injection function did not return an error, thus
eliminating the problem.  The disadvantage is that some packet injection
errors may no longer be detected.
2017-09-23 20:09:04 +08:00
basil00 c13a27bb4f Update CHANGELOG and VERSION 2017-09-23 16:54:58 +08:00
basil00 ea95889a30 Possible fix for issue #90. 2017-09-12 20:18:41 +08:00
Mike Neuman 504132080a Bugfix: If the user's app exits (or crashes) before calling CloseHandle()
on the WinDivert handle, the driver's cleanup function cannot make FWPM
calls. The cleanup function detects this FWPM error and returns
without ever calling the Fwps cleanup functions (which are independent
of fwpm).

As a result, the driver will not unregister the callouts, which leaves
the Windows kernel confused. You can reproduce the problem by having
a user app close uncleanly, then "sc stop windivert1.2", then try to
re-run the app. You'll get "file not found" when StartService() is called.
2017-08-15 09:32:44 -07:00
basil00 cd1bfff998 Bump copyright year. 2016-01-19 11:15:48 +08:00
basil00 657285bef6 CHANGELOG tweaks & remove old .inf file. 2015-07-26 11:06:32 +08:00