Skip to content

Commit ac5b3a5

Browse files
bonsaivikingguyharris
authored andcommitted
Npcap: support live capture "vlan" filter
Uses PacketGetInfo and NPF_GETINFO_BPFEXT to detect support of the relevant BPF extension. Npcap SDK 1.15 will contain definitions for SKF_AD_VLAN_TAG and other relevant constants.
1 parent bc02377 commit ac5b3a5

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ include_directories(
609609
include(CheckFunctionExists)
610610
include(CMakePushCheckState)
611611
include(CheckSymbolExists)
612+
include(CheckIncludeFile)
612613

613614
if(WIN32)
614615

@@ -626,8 +627,11 @@ if(WIN32)
626627
#
627628
cmake_push_check_state()
628629
set(CMAKE_REQUIRED_LIBRARIES ${Packet_LIBRARIES})
630+
set(CMAKE_REQUIRED_INCLUDES ${Packet_INCLUDE_DIRS})
629631
check_function_exists(PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER)
630632
check_function_exists(PacketGetTimestampModes HAVE_PACKET_GET_TIMESTAMP_MODES)
633+
check_function_exists(PacketGetInfo HAVE_PACKET_GET_INFO)
634+
check_include_file(npcap-bpf.h HAVE_NPCAP_BPF_H)
631635
cmake_pop_check_state()
632636
endif(Packet_FOUND)
633637

@@ -694,7 +698,6 @@ endif()
694698
# Detect available platform features
695699
###################################################################
696700

697-
include(CheckIncludeFile)
698701
include(CheckIncludeFiles)
699702
include(CheckStructHasMember)
700703
include(CheckTypeSize)

cmakeconfig.h.in

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@
9090
/* Define to 1 if Packet32 API (Npcap driver) is available */
9191
#cmakedefine HAVE_PACKET32 1
9292

93-
/* Define to 1 if Npcap's version.h is available */
93+
/* Define to 1 if Npcap BPF extension definitions are available */
94+
#cmakedefine HAVE_NPCAP_BPF_H 1
95+
96+
/* Define to 1 if NPcap's version.h is available */
9497
#cmakedefine HAVE_VERSION_H 1
9598

9699
/* Define to 1 if you have a POSIX-style `strerror_r' function. */
@@ -200,6 +203,9 @@
200203
/* Define to 1 if you have the `PacketIsLoopbackAdapter' function. */
201204
#cmakedefine HAVE_PACKET_IS_LOOPBACK_ADAPTER 1
202205

206+
/* Define to 1 if you have the `PacketGetInfo' function. */
207+
#cmakedefine HAVE_PACKET_GET_INFO 1
208+
203209
/* IPv6 */
204210
#cmakedefine INET6 1
205211

gencode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
#endif
6161

6262
#ifdef _WIN32
63+
#ifdef HAVE_NPCAP_BPF_H
64+
/* Defines BPF extensions for Npcap */
65+
#include <npcap-bpf.h>
66+
#endif
6367
#ifdef INET6
6468
#if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
6569
/* IPv6 address */

pcap-npf.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,10 @@ pcap_activate_npf(pcap_t *p)
830830
int status = 0;
831831
struct bpf_insn total_insn;
832832
struct bpf_program total_prog;
833+
#ifdef HAVE_PACKET_GET_INFO
834+
char oid_data_buf[PACKET_OID_DATA_LENGTH(sizeof(ULONG))] = {0};
835+
PACKET_OID_DATA *oid_data_arg = (PACKET_OID_DATA *)oid_data_buf;
836+
#endif
833837

834838
if (p->opt.rfmon) {
835839
/*
@@ -1086,6 +1090,23 @@ pcap_activate_npf(pcap_t *p)
10861090
}
10871091
#endif /* HAVE_PACKET_GET_TIMESTAMP_MODES */
10881092

1093+
#if defined(HAVE_PACKET_GET_INFO) && defined(NPF_GETINFO_BPFEXT) && defined(SKF_AD_VLAN_TAG_PRESENT)
1094+
1095+
/* Can we generate special code for VLAN checks? */
1096+
oid_data_arg->Oid = NPF_GETINFO_BPFEXT;
1097+
oid_data_arg->Length = sizeof(ULONG);
1098+
if (PacketGetInfo(pw->adapter, oid_data_arg)) {
1099+
if (*((ULONG *)oid_data_arg->Data) >= SKF_AD_VLAN_TAG_PRESENT) {
1100+
/* Yes, we can. Request that we do so. */
1101+
p->bpf_codegen_flags |= BPF_SPECIAL_VLAN_HANDLING;
1102+
}
1103+
}
1104+
else {
1105+
pcapint_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1106+
GetLastError(), "Error calling PacketGetInfo");
1107+
}
1108+
#endif /* HAVE_PACKET_GET_INFO */
1109+
10891110
/*
10901111
* Turn a negative snapshot value (invalid), a snapshot value of
10911112
* 0 (unspecified), or a value bigger than the normal maximum

0 commit comments

Comments
 (0)