2024-05-04 22:55:56 +03:00
|
|
|
// Copyright (c) eBPF for Windows contributors
|
2021-06-10 22:02:00 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-03-10 01:54:03 +03:00
|
|
|
#pragma once
|
2021-02-09 04:16:52 +03:00
|
|
|
|
2023-02-07 21:32:19 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Header file for structures/prototypes of the driver.
|
|
|
|
*/
|
2021-09-14 00:46:43 +03:00
|
|
|
|
|
|
|
#include "ebpf_nethooks.h"
|
2022-02-26 04:53:12 +03:00
|
|
|
#include "ebpf_program_attach_type_guids.h"
|
2023-02-07 21:32:19 +03:00
|
|
|
#include "ebpf_program_types.h"
|
2023-09-05 20:57:21 +03:00
|
|
|
#include "ebpf_shared_framework.h"
|
2021-09-14 00:46:43 +03:00
|
|
|
#include "ebpf_windows.h"
|
2022-02-26 04:53:12 +03:00
|
|
|
#include "net_ebpf_ext_hook_provider.h"
|
|
|
|
#include "net_ebpf_ext_prog_info_provider.h"
|
2021-09-14 03:46:23 +03:00
|
|
|
#include "net_ebpf_ext_program_info.h"
|
2022-07-01 01:08:02 +03:00
|
|
|
#include "net_ebpf_ext_tracelog.h"
|
2023-02-07 21:32:19 +03:00
|
|
|
#include "netebpfext_platform.h"
|
|
|
|
|
|
|
|
#include <guiddef.h>
|
|
|
|
#include <netioapi.h>
|
|
|
|
#include <netiodef.h>
|
2021-09-14 03:46:23 +03:00
|
|
|
|
2021-09-21 18:46:35 +03:00
|
|
|
#define NET_EBPF_EXTENSION_POOL_TAG 'Nfbe'
|
2021-09-14 00:46:43 +03:00
|
|
|
#define NET_EBPF_EXTENSION_NPI_PROVIDER_VERSION 0
|
|
|
|
|
2022-11-29 23:49:42 +03:00
|
|
|
CONST IN6_ADDR DECLSPEC_SELECTANY in6addr_v4mappedprefix = IN6ADDR_V4MAPPEDPREFIX_INIT;
|
|
|
|
|
2022-04-12 21:37:44 +03:00
|
|
|
#define htonl(x) _byteswap_ulong(x)
|
|
|
|
#define htons(x) _byteswap_ushort(x)
|
2022-11-29 23:49:42 +03:00
|
|
|
#define ntohl(x) _byteswap_ulong(x)
|
|
|
|
#define ntohs(x) _byteswap_ushort(x)
|
2022-04-21 02:48:20 +03:00
|
|
|
struct _net_ebpf_extension_hook_client;
|
|
|
|
|
|
|
|
typedef struct _wfp_ale_layer_fields
|
|
|
|
{
|
|
|
|
uint16_t local_ip_address_field;
|
|
|
|
uint16_t local_port_field;
|
|
|
|
uint16_t remote_ip_address_field;
|
|
|
|
uint16_t remote_port_field;
|
|
|
|
uint16_t protocol_field;
|
|
|
|
uint32_t direction_field;
|
|
|
|
uint16_t compartment_id_field;
|
2022-06-17 19:31:48 +03:00
|
|
|
uint16_t interface_luid_field;
|
2023-01-31 02:44:30 +03:00
|
|
|
uint16_t user_id_field;
|
2023-02-22 04:34:21 +03:00
|
|
|
uint16_t flags_field;
|
2022-04-21 02:48:20 +03:00
|
|
|
} wfp_ale_layer_fields_t;
|
|
|
|
|
2022-03-15 01:16:11 +03:00
|
|
|
typedef struct _net_ebpf_extension_wfp_filter_parameters
|
|
|
|
{
|
|
|
|
const GUID* layer_guid; ///< GUID of WFP layer to which this filter is associated.
|
2022-11-29 23:49:42 +03:00
|
|
|
const GUID* sublayer_guid; ///< GUID of the WFP sublayer to which this filter is associated.
|
2022-03-15 01:16:11 +03:00
|
|
|
const GUID* callout_guid; ///< GUID of WFP callout to which this filter is associated.
|
|
|
|
const wchar_t* name; ///< Display name of filter.
|
|
|
|
const wchar_t* description; ///< Description of filter.
|
|
|
|
} net_ebpf_extension_wfp_filter_parameters_t;
|
|
|
|
|
2022-11-29 23:49:42 +03:00
|
|
|
typedef struct _net_ebpf_ext_sublayer_info
|
|
|
|
{
|
|
|
|
const GUID* sublayer_guid;
|
|
|
|
const wchar_t* name;
|
|
|
|
const wchar_t* description;
|
|
|
|
const uint32_t flags;
|
|
|
|
const uint16_t weight;
|
|
|
|
} net_ebpf_ext_sublayer_info_t;
|
|
|
|
|
|
|
|
typedef struct _net_ebpf_extension_wfp_filter_parameters_array
|
|
|
|
{
|
|
|
|
ebpf_attach_type_t* attach_type;
|
|
|
|
uint32_t count;
|
|
|
|
net_ebpf_extension_wfp_filter_parameters_t* filter_parameters;
|
|
|
|
} net_ebpf_extension_wfp_filter_parameters_array_t;
|
|
|
|
|
2022-04-21 02:48:20 +03:00
|
|
|
/**
|
|
|
|
* "Base class" for all WFP filter contexts used by net ebpf extension hooks.
|
|
|
|
*/
|
2024-03-08 19:38:54 +03:00
|
|
|
|
|
|
|
typedef enum _net_ebpf_ext_wfp_filter_state
|
|
|
|
{
|
|
|
|
NET_EBPF_EXT_WFP_FILTER_ADDED = 1,
|
|
|
|
NET_EBPF_EXT_WFP_FILTER_DELETED = 2,
|
|
|
|
|
|
|
|
} net_ebpf_ext_wfp_filter_state_t;
|
|
|
|
|
|
|
|
typedef struct _net_ebpf_ext_wfp_filter_id
|
|
|
|
{
|
|
|
|
wchar_t* name;
|
|
|
|
uint64_t id;
|
|
|
|
net_ebpf_ext_wfp_filter_state_t state;
|
|
|
|
} net_ebpf_ext_wfp_filter_id_t;
|
|
|
|
|
2022-04-21 02:48:20 +03:00
|
|
|
typedef struct _net_ebpf_extension_wfp_filter_context
|
|
|
|
{
|
|
|
|
volatile long reference_count; ///< Reference count.
|
|
|
|
const struct _net_ebpf_extension_hook_client* client_context; ///< Pointer to hook NPI client.
|
2024-03-08 19:38:54 +03:00
|
|
|
|
|
|
|
net_ebpf_ext_wfp_filter_id_t* filter_ids; ///< Array of WFP filter Ids.
|
|
|
|
uint32_t filter_ids_count; ///< Number of WFP filter Ids.
|
|
|
|
|
|
|
|
bool client_detached : 1; ///< True if client has detached.
|
2022-04-21 02:48:20 +03:00
|
|
|
} net_ebpf_extension_wfp_filter_context_t;
|
|
|
|
|
2023-11-30 03:07:44 +03:00
|
|
|
#define REFERENCE_FILTER_CONTEXT(filter_context) \
|
|
|
|
if ((filter_context) != NULL) { \
|
|
|
|
InterlockedIncrement(&(filter_context)->reference_count); \
|
|
|
|
}
|
2022-04-21 02:48:20 +03:00
|
|
|
|
2023-11-30 03:07:44 +03:00
|
|
|
#define DEREFERENCE_FILTER_CONTEXT(filter_context) \
|
|
|
|
if ((filter_context) != NULL) { \
|
|
|
|
if (InterlockedDecrement(&(filter_context)->reference_count) == 0) { \
|
|
|
|
net_ebpf_extension_hook_client_leave_rundown( \
|
|
|
|
(net_ebpf_extension_hook_client_t*)(filter_context)->client_context); \
|
2024-03-08 19:38:54 +03:00
|
|
|
if ((filter_context)->filter_ids != NULL) { \
|
|
|
|
ExFreePool((filter_context)->filter_ids); \
|
|
|
|
} \
|
2023-11-30 03:07:44 +03:00
|
|
|
ExFreePool((filter_context)); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2022-04-21 02:48:20 +03:00
|
|
|
/**
|
|
|
|
* @brief This function allocates and initializes a net ebpf extension WFP filter context. This should be invoked when
|
|
|
|
* the hook client is being attached.
|
|
|
|
*
|
|
|
|
* @param[in] filter_context_size Size in bytes of the filter context.
|
|
|
|
* @param[in] client_context Pointer to hook client being attached. This would be associated with the filter context.
|
|
|
|
* @param[out] filter_context Pointer to created filter_context.
|
|
|
|
*
|
|
|
|
* @retval EBPF_SUCCESS The filter context was created successfully.
|
|
|
|
* @retval EBPF_NO_MEMORY Out of memory.
|
|
|
|
*/
|
2022-11-19 00:36:24 +03:00
|
|
|
_Must_inspect_result_ ebpf_result_t
|
2022-04-21 02:48:20 +03:00
|
|
|
net_ebpf_extension_wfp_filter_context_create(
|
|
|
|
size_t filter_context_size,
|
|
|
|
_In_ const struct _net_ebpf_extension_hook_client* client_context,
|
|
|
|
_Outptr_ net_ebpf_extension_wfp_filter_context_t** filter_context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function cleans up the input ebpf extension WFP filter context. This should be invoked when the hook
|
|
|
|
* client is being detached.
|
|
|
|
*
|
|
|
|
* @param[out] filter_context Pointer to filter_context to clean up.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
net_ebpf_extension_wfp_filter_context_cleanup(_Frees_ptr_ net_ebpf_extension_wfp_filter_context_t* filter_context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Structure for WFP flow Id parameters.
|
|
|
|
*/
|
|
|
|
typedef struct _net_ebpf_extension_flow_context_parameters
|
|
|
|
{
|
|
|
|
uint64_t flow_id; ///< WFP flow Id.
|
|
|
|
uint16_t layer_id; ///< WFP layer Id that this flow is associated to.
|
|
|
|
uint32_t callout_id; ///< WFP callout Id that this flow is associated to.
|
|
|
|
} net_ebpf_extension_flow_context_parameters_t;
|
|
|
|
|
|
|
|
typedef enum _net_ebpf_extension_hook_id
|
|
|
|
{
|
|
|
|
EBPF_HOOK_OUTBOUND_L2 = 0,
|
|
|
|
EBPF_HOOK_INBOUND_L2,
|
|
|
|
EBPF_HOOK_ALE_RESOURCE_ALLOC_V4,
|
|
|
|
EBPF_HOOK_ALE_RESOURCE_ALLOC_V6,
|
|
|
|
EBPF_HOOK_ALE_RESOURCE_RELEASE_V4,
|
|
|
|
EBPF_HOOK_ALE_RESOURCE_RELEASE_V6, // 5
|
|
|
|
EBPF_HOOK_ALE_AUTH_CONNECT_V4,
|
|
|
|
EBPF_HOOK_ALE_AUTH_CONNECT_V6,
|
2022-11-29 23:49:42 +03:00
|
|
|
EBPF_HOOK_ALE_CONNECT_REDIRECT_V4,
|
|
|
|
EBPF_HOOK_ALE_CONNECT_REDIRECT_V6,
|
|
|
|
EBPF_HOOK_ALE_AUTH_RECV_ACCEPT_V4, // 10
|
2022-04-21 02:48:20 +03:00
|
|
|
EBPF_HOOK_ALE_AUTH_RECV_ACCEPT_V6,
|
2022-11-29 23:49:42 +03:00
|
|
|
EBPF_HOOK_ALE_FLOW_ESTABLISHED_V4,
|
2022-04-21 02:48:20 +03:00
|
|
|
EBPF_HOOK_ALE_FLOW_ESTABLISHED_V6
|
|
|
|
} net_ebpf_extension_hook_id_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper function to return the eBPF network extension hook Id for the input WFP layer Id.
|
|
|
|
*
|
|
|
|
* @param[in] wfp_layer_id WFP layer Id.
|
|
|
|
*
|
|
|
|
* @returns eBPF network extension hook Id for the input WFP layer Id.
|
|
|
|
*/
|
|
|
|
net_ebpf_extension_hook_id_t
|
|
|
|
net_ebpf_extension_get_hook_id_from_wfp_layer_id(uint16_t wfp_layer_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper function to return the assigned Id for the WFP callout corresponding to the eBPF hook.
|
|
|
|
*
|
|
|
|
* @param[in] hook_id eBPF network extension hook id.
|
|
|
|
*
|
|
|
|
* @returns assigned Id for the WFP callout corresponding to the eBPF hook.
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
net_ebpf_extension_get_callout_id_for_hook(net_ebpf_extension_hook_id_t hook_id);
|
|
|
|
|
2022-03-15 01:16:11 +03:00
|
|
|
/**
|
|
|
|
* @brief Add WFP filters with specified conditions at specified layers.
|
|
|
|
*
|
|
|
|
* @param[in] filter_count Count of filters to be added.
|
2022-11-29 23:49:42 +03:00
|
|
|
* @param[in] parameters Filter parameters.
|
2022-03-15 01:16:11 +03:00
|
|
|
* @param[in] condition_count Count of filter conditions.
|
|
|
|
* @param[in] conditions Common filter conditions to be applied to each filter.
|
2022-12-16 02:58:33 +03:00
|
|
|
* @param[in, out] filter_context Caller supplied context to be associated with the WFP filter.
|
2022-03-15 01:16:11 +03:00
|
|
|
* @param[out] filter_ids Output buffer where the added filter IDs are stored.
|
|
|
|
*
|
|
|
|
* @retval EBPF_SUCCESS The operation completed successfully.
|
|
|
|
* @retval EBPF_INVALID_ARGUMENT One or more arguments are invalid.
|
|
|
|
*/
|
2022-11-19 00:36:24 +03:00
|
|
|
_Must_inspect_result_ ebpf_result_t
|
2022-03-15 01:16:11 +03:00
|
|
|
net_ebpf_extension_add_wfp_filters(
|
|
|
|
uint32_t filter_count,
|
|
|
|
_In_count_(filter_count) const net_ebpf_extension_wfp_filter_parameters_t* parameters,
|
|
|
|
uint32_t condition_count,
|
|
|
|
_In_opt_count_(condition_count) const FWPM_FILTER_CONDITION* conditions,
|
2022-12-16 02:58:33 +03:00
|
|
|
_Inout_ net_ebpf_extension_wfp_filter_context_t* filter_context,
|
2024-03-08 19:38:54 +03:00
|
|
|
_Outptr_result_buffer_maybenull_(filter_count) net_ebpf_ext_wfp_filter_id_t** filter_ids);
|
2022-03-15 01:16:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deletes WFP filters with specified filter IDs.
|
|
|
|
*
|
|
|
|
* @param[in] filter_count Count of filters to be added.
|
|
|
|
* @param[in] filter_ids ID of the filter being deleted.
|
|
|
|
*/
|
|
|
|
void
|
2024-03-08 19:38:54 +03:00
|
|
|
net_ebpf_extension_delete_wfp_filters(
|
|
|
|
uint32_t filter_count, _Frees_ptr_ _In_count_(filter_count) net_ebpf_ext_wfp_filter_id_t* filter_ids);
|
2022-03-15 01:16:11 +03:00
|
|
|
|
2023-04-08 01:16:45 +03:00
|
|
|
// eBPF WFP Provider GUID.
|
|
|
|
// ddb851f5-841a-4b77-8a46-bb7063e9f162
|
|
|
|
DEFINE_GUID(EBPF_WFP_PROVIDER, 0xddb851f5, 0x841a, 0x4b77, 0x8a, 0x46, 0xbb, 0x70, 0x63, 0xe9, 0xf1, 0x62);
|
|
|
|
|
2022-11-29 23:49:42 +03:00
|
|
|
// Default eBPF WFP Sublayer GUID.
|
2022-03-15 01:16:11 +03:00
|
|
|
// 7c7b3fb9-3331-436a-98e1-b901df457fff
|
2022-11-29 23:49:42 +03:00
|
|
|
DEFINE_GUID(EBPF_DEFAULT_SUBLAYER, 0x7c7b3fb9, 0x3331, 0x436a, 0x98, 0xe1, 0xb9, 0x01, 0xdf, 0x45, 0x7f, 0xff);
|
2022-03-15 01:16:11 +03:00
|
|
|
|
2021-09-21 18:46:35 +03:00
|
|
|
// Globals.
|
|
|
|
extern NDIS_HANDLE _net_ebpf_ext_nbl_pool_handle;
|
|
|
|
extern NDIS_HANDLE _net_ebpf_ext_ndis_handle;
|
|
|
|
extern HANDLE _net_ebpf_ext_l2_injection_handle;
|
2022-02-26 04:53:12 +03:00
|
|
|
extern DEVICE_OBJECT* _net_ebpf_ext_driver_device_object;
|
2021-09-21 18:46:35 +03:00
|
|
|
|
2021-01-14 23:48:48 +03:00
|
|
|
//
|
2021-09-14 00:46:43 +03:00
|
|
|
// Shared function prototypes.
|
2021-01-14 23:48:48 +03:00
|
|
|
//
|
|
|
|
|
2021-09-21 18:46:35 +03:00
|
|
|
/**
|
|
|
|
* @brief Initialize global NDIS handles.
|
|
|
|
*
|
|
|
|
* @param[in] driver_object The driver object to associate the NDIS generic object handle with.
|
|
|
|
* @retval STATUS_SUCCESS NDIS handles initialized successfully.
|
|
|
|
* @retval STATUS_INSUFFICIENT_RESOURCES Failed to initialize NDIS handles due to insufficient resources.
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
net_ebpf_ext_initialize_ndis_handles(_In_ const DRIVER_OBJECT* driver_object);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Uninitialize global NDIS handles.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
net_ebpf_ext_uninitialize_ndis_handles();
|
|
|
|
|
2021-04-28 06:39:20 +03:00
|
|
|
/**
|
|
|
|
* @brief Register for the WFP callouts used to power hooks.
|
|
|
|
*
|
|
|
|
* @param[in] device_object Device object used by this driver.
|
|
|
|
* @retval STATUS_SUCCESS Operation succeeded.
|
|
|
|
* @retval FWP_E_* A Windows Filtering Platform (WFP) specific error.
|
|
|
|
*/
|
2021-01-14 23:48:48 +03:00
|
|
|
NTSTATUS
|
2022-03-15 01:16:11 +03:00
|
|
|
net_ebpf_extension_initialize_wfp_components(_Inout_ void* device_object);
|
2021-01-14 23:48:48 +03:00
|
|
|
|
2021-04-28 06:39:20 +03:00
|
|
|
/**
|
|
|
|
* @brief Unregister the WFP callouts.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2022-03-15 01:16:11 +03:00
|
|
|
net_ebpf_extension_uninitialize_wfp_components(void);
|
2021-04-28 06:39:20 +03:00
|
|
|
|
|
|
|
/**
|
2021-09-14 00:46:43 +03:00
|
|
|
* @brief Register network extension NPI providers with eBPF core.
|
2021-04-28 06:39:20 +03:00
|
|
|
*
|
|
|
|
* @retval STATUS_SUCCESS Operation succeeded.
|
|
|
|
* @retval STATUS_UNSUCCESSFUL Operation failed.
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
2021-05-05 00:31:12 +03:00
|
|
|
net_ebpf_ext_register_providers();
|
2021-04-28 06:39:20 +03:00
|
|
|
|
|
|
|
/**
|
2021-09-14 00:46:43 +03:00
|
|
|
* @brief Unregister network extension NPI providers from eBPF core.
|
2021-05-04 01:17:10 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2022-06-17 19:31:48 +03:00
|
|
|
net_ebpf_ext_unregister_providers();
|
2022-11-29 23:49:42 +03:00
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
net_ebpf_ext_filter_change_notify(
|
|
|
|
FWPS_CALLOUT_NOTIFY_TYPE callout_notification_type, _In_ const GUID* filter_key, _Inout_ FWPS_FILTER* filter);
|