Skip to content

Commit bd1ebe4

Browse files
authored
Merge branch 'nmap:master' into fix_nonadmin_npcaphelper
2 parents f7c77b5 + b64d13a commit bd1ebe4

17 files changed

+540
-442
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## Unreleased changes
2+
3+
* Added complete capture support for 802.1q VLAN packets. The 802.1q frame tag
4+
is usually stripped by NDIS before Npcap encounters it, but Npcap will now
5+
restore it from the packet metadata if the VLAN ID is not 0. Filtering with the
6+
"vlan" keyword is also now supported on live captures. The deprecated
7+
`/vlan_support` installer option has no impact on this feature.
8+
9+
* Npcap now supports the `BPF_MOD` and `BPF_XOR` instructions. Previously, pcap
10+
filter expressions using the `%` and `^` operators would result in
11+
less-efficient user-mode filtering.
12+
13+
* The Npcap driver now supports a range of NDIS versions depending on what is
14+
available at runtime. This will allow us to support newer features like URO
15+
and hardware timestamping when available, without needing to have separate
16+
builds for each NDIS version at install time.
17+
118
## Npcap 1.80 [2024-09-16]
219

320
* On ARM64, Npcap now installs ARM64X pure-forwarder DLLs so that Npcap can be used by x86_64

Common/Packet32.h

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117

118118
#include <sal.h>
119119
#include <winsock2.h>
120+
#include "npcap-bpf.h"
121+
#include "npcap-defs.h"
120122

121123
#ifdef HAVE_AIRPCAP_API
122124
#include <airpcap.h>
@@ -186,93 +188,6 @@ typedef struct NetType
186188
}NetType;
187189

188190

189-
//some definitions stolen from libpcap
190-
191-
#ifndef BPF_MAJOR_VERSION
192-
193-
/*!
194-
\brief A BPF pseudo-assembly program.
195-
196-
The program will be injected in the kernel by the PacketSetBPF() function and applied to every incoming packet.
197-
*/
198-
struct bpf_program
199-
{
200-
UINT bf_len; ///< Indicates the number of instructions of the program, i.e. the number of struct bpf_insn that will follow.
201-
_Field_size_full_(bf_len)
202-
struct bpf_insn* bf_insns; ///< A pointer to the first instruction of the program.
203-
};
204-
205-
/*!
206-
\brief A single BPF pseudo-instruction.
207-
208-
bpf_insn contains a single instruction for the BPF register-machine. It is used to send a filter program to the driver.
209-
*/
210-
struct bpf_insn
211-
{
212-
USHORT code; ///< Instruction type and addressing mode.
213-
UCHAR jt; ///< Jump if true
214-
UCHAR jf; ///< Jump if false
215-
int k; ///< Generic field used for various purposes.
216-
};
217-
218-
/*!
219-
\brief Structure that contains a couple of statistics values on the current capture.
220-
221-
It is used by packet.dll to return statistics about a capture session.
222-
*/
223-
struct bpf_stat
224-
{
225-
UINT bs_recv; ///< Number of packets that the driver received from the network adapter
226-
///< from the beginning of the current capture. This value includes the packets
227-
///< lost by the driver.
228-
UINT bs_drop; ///< number of packets that the driver lost from the beginning of a capture.
229-
///< Basically, a packet is lost when the the buffer of the driver is full.
230-
///< In this situation the packet cannot be stored and the driver rejects it.
231-
UINT ps_ifdrop; ///< drops by interface. XXX not yet supported
232-
UINT bs_capt; ///< number of packets that pass the filter, find place in the kernel buffer and
233-
///< thus reach the application.
234-
};
235-
236-
/*!
237-
\brief Packet header.
238-
239-
This structure defines the header associated with every packet delivered to the application.
240-
*/
241-
struct bpf_hdr
242-
{
243-
struct timeval bh_tstamp; ///< The timestamp associated with the captured packet.
244-
///< It is stored in a TimeVal structure.
245-
UINT bh_caplen; ///< Length of captured portion. The captured portion <b>can be different</b>
246-
///< from the original packet, because it is possible (with a proper filter)
247-
///< to instruct the driver to capture only a portion of the packets.
248-
UINT bh_datalen; ///< Original length of packet
249-
USHORT bh_hdrlen; ///< Length of bpf header (this struct plus alignment padding). In some cases,
250-
///< a padding could be added between the end of this structure and the packet
251-
///< data for performance reasons. This filed can be used to retrieve the actual data
252-
///< of the packet.
253-
};
254-
255-
/*!
256-
\brief Dump packet header.
257-
258-
This structure defines the header associated with the packets in a buffer to be used with PacketSendPackets().
259-
It is simpler than the bpf_hdr, because it corresponds to the header associated by WinPcap and libpcap to a
260-
packet in a dump file. This makes straightforward sending WinPcap dump files to the network.
261-
*/
262-
struct dump_bpf_hdr
263-
{
264-
struct timeval ts; ///< Time stamp of the packet
265-
UINT caplen; ///< Length of captured portion. The captured portion can smaller than the
266-
///< the original packet, because it is possible (with a proper filter) to
267-
///< instruct the driver to capture only a portion of the packets.
268-
UINT len; ///< Length of the original packet (off wire).
269-
};
270-
271-
272-
#endif
273-
274-
struct bpf_stat;
275-
276191
#define DOSNAMEPREFIX TEXT("Packet_") ///< Prefix added to the adapters device names to create the WinPcap devices
277192
#define MAX_LINK_NAME_LENGTH 64 //< Maximum length of the devices symbolic links
278193
#define NMAX_PACKET 65535
@@ -359,26 +274,6 @@ typedef struct _PACKET
359274
PACKET_WINPCAP_DEPRECATED BOOLEAN bIoComplete; ///< \deprecated Still present for compatibility with old applications.
360275
} PACKET, * LPPACKET;
361276

362-
/*!
363-
\brief Structure containing an OID request.
364-
365-
It is used by the PacketRequest() function to send an OID to the interface card driver.
366-
It can be used, for example, to retrieve the status of the error counters on the adapter, its MAC address,
367-
the list of the multicast groups defined on it, and so on.
368-
*/
369-
struct _PACKET_OID_DATA
370-
{
371-
ULONG Oid; ///< OID code. See the Microsoft DDK documentation or the file ntddndis.h
372-
///< for a complete list of valid codes.
373-
ULONG Length; ///< Length of the data field
374-
_Field_size_full_(Length)
375-
UCHAR Data[1]; ///< variable-length field that contains the information passed to or received
376-
///< from the adapter.
377-
};
378-
typedef struct _PACKET_OID_DATA PACKET_OID_DATA, * PPACKET_OID_DATA;
379-
380-
#define PACKET_OID_DATA_LENGTH(_DataLength) \
381-
(FIELD_OFFSET(PACKET_OID_DATA, Data) + _DataLength)
382277

383278
#ifdef __cplusplus
384279
extern "C"

Common/npcap-bpf.h

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/***********************IMPORTANT NPCAP LICENSE TERMS***********************
2+
*
3+
* Npcap (https://npcap.com) is a Windows packet sniffing driver and library and
4+
* is copyright (c) 2013-2023 by Nmap Software LLC ("The Nmap Project"). All
5+
* rights reserved.
6+
*
7+
* Even though Npcap source code is publicly available for review, it is not
8+
* open source software and may not be redistributed or used in other software
9+
* without special permission from the Nmap Project. The standard (free) version
10+
* is usually limited to installation on five systems. For more details, see the
11+
* LICENSE file included with Npcap and also available at
12+
* https://github.com/nmap/npcap/blob/master/LICENSE. This header file
13+
* summarizes a few important aspects of the Npcap license, but is not a
14+
* substitute for that full Npcap license agreement.
15+
*
16+
* We fund the Npcap project by selling two types of commercial licenses to a
17+
* special Npcap OEM edition:
18+
*
19+
* 1) The Npcap OEM Redistribution License allows companies distribute Npcap OEM
20+
* within their products. Licensees generally use the Npcap OEM silent
21+
* installer, ensuring a seamless experience for end users. Licensees may choose
22+
* between a perpetual unlimited license or a quarterly term license, along with
23+
* options for commercial support and updates. Prices and details:
24+
* https://npcap.com/oem/redist.html
25+
*
26+
* 2) The Npcap OEM Internal-Use License is for organizations that wish to use
27+
* Npcap OEM internally, without redistribution outside their organization. This
28+
* allows them to bypass the 5-system usage cap of the Npcap free edition. It
29+
* includes commercial support and update options, and provides the extra Npcap
30+
* OEM features such as the silent installer for automated deployment. Prices
31+
* and details: https://npcap.com/oem/internal.html
32+
*
33+
* Both of these licenses include updates and support as well as a warranty.
34+
* Npcap OEM also includes a silent installer for unattended installation.
35+
* Further details about Npcap OEM are available from https://npcap.com/oem/,
36+
* and you are also welcome to contact us at [email protected] to ask any questions
37+
* or set up a license for your organization.
38+
*
39+
* Free and open source software producers are also welcome to contact us for
40+
* redistribution requests. However, we normally recommend that such authors
41+
* instead ask your users to download and install Npcap themselves. It will be
42+
* free for them if they need 5 or fewer copies.
43+
*
44+
* If the Nmap Project (directly or through one of our commercial licensing
45+
* customers) has granted you additional rights to Npcap or Npcap OEM, those
46+
* additional rights take precedence where they conflict with the terms of the
47+
* license agreement.
48+
*
49+
* Since the Npcap source code is available for download and review, users
50+
* sometimes contribute code patches to fix bugs or add new features. By sending
51+
* these changes to the Nmap Project (including through direct email or our
52+
* mailing lists or submitting pull requests through our source code
53+
* repository), it is understood unless you specify otherwise that you are
54+
* offering the Nmap Project the unlimited, non-exclusive right to reuse,
55+
* modify, and relicense your code contribution so that we may (but are not
56+
* obligated to) incorporate it into Npcap. If you wish to specify special
57+
* license conditions or restrictions on your contributions, just say so when
58+
* you send them.
59+
*
60+
* This software is distributed in the hope that it will be useful, but WITHOUT
61+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
62+
* FOR A PARTICULAR PURPOSE. Warranty rights and commercial support are
63+
* available for the OEM Edition described above.
64+
*
65+
* Other copyright notices and attribution may appear below this license header.
66+
* We have kept those for attribution purposes, but any license terms granted by
67+
* those notices apply only to their original work, and not to any changes made
68+
* by the Nmap Project or to this entire file.
69+
*
70+
***************************************************************************/
71+
/* Portions of this file
72+
* Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
73+
* The Regents of the University of California. All rights reserved.
74+
*
75+
* This code is derived from the Stanford/CMU enet packet filter,
76+
* (net/enet.c) distributed as part of 4.3BSD, and code contributed
77+
* to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
78+
* Berkeley Laboratory.
79+
*
80+
* Redistribution and use in source and binary forms, with or without
81+
* modification, are permitted provided that the following conditions
82+
* are met:
83+
* 1. Redistributions of source code must retain the above copyright
84+
* notice, this list of conditions and the following disclaimer.
85+
* 2. Redistributions in binary form must reproduce the above copyright
86+
* notice, this list of conditions and the following disclaimer in the
87+
* documentation and/or other materials provided with the distribution.
88+
* 3. All advertising materials mentioning features or use of this software
89+
* must display the following acknowledgement:
90+
* This product includes software developed by the University of
91+
* California, Berkeley and its contributors.
92+
* 4. Neither the name of the University nor the names of its contributors
93+
* may be used to endorse or promote products derived from this software
94+
* without specific prior written permission.
95+
*
96+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
97+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
100+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
101+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
102+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106+
* SUCH DAMAGE.
107+
*
108+
*/
109+
#ifndef NPCAP_BPF_H
110+
#define NPCAP_BPF_H
111+
112+
/* These are defined by libpcap's bpf.h, so if that has already been included,
113+
* we want to avoid redefining them here.
114+
*/
115+
#ifndef lib_pcap_bpf_h
116+
117+
#ifndef _NTDDK_
118+
#include <winsock2.h>
119+
#endif
120+
121+
/*!
122+
\brief A BPF pseudo-assembly program.
123+
124+
The program will be injected in the kernel by the PacketSetBPF() function and applied to every incoming packet.
125+
*/
126+
struct bpf_program
127+
{
128+
UINT bf_len; ///< Indicates the number of instructions of the program, i.e. the number of struct bpf_insn that will follow.
129+
#ifdef _Field_size_full_ /* SAL annotation */
130+
_Field_size_full_(bf_len)
131+
#endif
132+
struct bpf_insn* bf_insns; ///< A pointer to the first instruction of the program.
133+
};
134+
135+
/*!
136+
\brief A single BPF pseudo-instruction.
137+
138+
bpf_insn contains a single instruction for the BPF register-machine. It is used to send a filter program to the driver.
139+
*/
140+
struct bpf_insn
141+
{
142+
USHORT code; ///< Instruction type and addressing mode.
143+
UCHAR jt; ///< Jump if true
144+
UCHAR jf; ///< Jump if false
145+
ULONG k; ///< Generic field used for various purposes.
146+
};
147+
148+
/*!
149+
\brief Structure that contains a couple of statistics values on the current capture.
150+
151+
It is used by packet.dll to return statistics about a capture session.
152+
*/
153+
struct bpf_stat
154+
{
155+
UINT bs_recv; ///< Number of packets that the driver received from the network adapter
156+
///< from the beginning of the current capture. This value includes the packets
157+
///< lost by the driver.
158+
UINT bs_drop; ///< number of packets that the driver lost from the beginning of a capture.
159+
///< Basically, a packet is lost when the the buffer of the driver is full.
160+
///< In this situation the packet cannot be stored and the driver rejects it.
161+
UINT ps_ifdrop; ///< drops by interface. XXX not yet supported
162+
UINT bs_capt; ///< number of packets that pass the filter, find place in the kernel buffer and
163+
///< thus reach the application.
164+
};
165+
166+
/* Current version number of filter architecture. */
167+
#define BPF_MAJOR_VERSION 1
168+
#define BPF_MINOR_VERSION 1
169+
170+
/*!
171+
\brief Packet header.
172+
173+
This structure defines the header associated with every packet delivered to the application.
174+
*/
175+
struct bpf_hdr
176+
{
177+
struct timeval bh_tstamp; ///< The timestamp associated with the captured packet.
178+
///< It is stored in a TimeVal structure.
179+
UINT bh_caplen; ///< Length of captured portion. The captured portion <b>can be different</b>
180+
///< from the original packet, because it is possible (with a proper filter)
181+
///< to instruct the driver to capture only a portion of the packets.
182+
UINT bh_datalen; ///< Original length of packet
183+
USHORT bh_hdrlen; ///< Length of bpf header (this struct plus alignment padding). In some cases,
184+
///< a padding could be added between the end of this structure and the packet
185+
///< data for performance reasons. This filed can be used to retrieve the actual data
186+
///< of the packet.
187+
};
188+
189+
/*!
190+
\brief Dump packet header.
191+
192+
This structure defines the header associated with the packets in a buffer to be used with PacketSendPackets().
193+
It is simpler than the bpf_hdr, because it corresponds to the header in the pcap-savefile(5) format.
194+
This makes straightforward sending WinPcap dump files to the network.
195+
*/
196+
struct dump_bpf_hdr
197+
{
198+
struct timeval ts; ///< Time stamp of the packet
199+
UINT caplen; ///< Length of captured portion. The captured portion can smaller than the
200+
///< the original packet, because it is possible (with a proper filter) to
201+
///< instruct the driver to capture only a portion of the packets.
202+
UINT len; ///< Length of the original packet (off wire).
203+
};
204+
205+
#endif /* lib_pcap_bpf_h */
206+
207+
/* BPF extensions */
208+
/* Special offsets to mimic Linux kernel's BPF extensions.
209+
* The names are taken directly from Linux in order to allow libpcap's
210+
* gencode.c to use the same code for both, but the values are different.
211+
*/
212+
/* The base offset for these extensions */
213+
#define SKF_AD_OFF (-0x1000)
214+
/* The extensions are numbered in the order they were added.
215+
* Since they are treated like offsets, we space them by 4 to avoid the
216+
* appearance of reading overlapped memory segments.
217+
* User can issue BIOCGETINFO(NPF_GETINFO_BPFEXT) to retrieve the value of
218+
* SKF_AD_MAX, and any extension less than or equal to that value will be
219+
* supported.
220+
*/
221+
/* Halfword (2 bytes) representing the 802.1q header. */
222+
#define SKF_AD_VLAN_TAG 0
223+
/* Boolean: is there VLAN metadata present? Currently, we cannot distinguish VLAN
224+
* 0 and priority class 0 (both defaults) from the case of no VLAN tag present,
225+
* so this will return false in that case. */
226+
#define SKF_AD_VLAN_TAG_PRESENT 4
227+
#define SKF_AD_MAX 4
228+
229+
#endif /* NPCAP_BPF_H */

0 commit comments

Comments
 (0)