2021-05-06 23:47:09 +03:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
// This file contains APIs for hooks and helpers that are
|
|
|
|
// exposed by netebpfext.sys for use by eBPF programs.
|
|
|
|
|
|
|
|
// XDP hook. We use "struct xdp_md" for cross-platform compatibility.
|
|
|
|
typedef struct xdp_md
|
|
|
|
{
|
2021-07-07 20:39:59 +03:00
|
|
|
void* data; ///< Pointer to start of packet data.
|
|
|
|
void* data_end; ///< Pointer to end of packet data.
|
|
|
|
uint64_t data_meta; ///< Packet metadata.
|
2021-05-06 23:47:09 +03:00
|
|
|
|
|
|
|
/* size: 12, cachelines: 1, members: 3 */
|
|
|
|
/* last cacheline: 12 bytes */
|
|
|
|
} xdp_md_t;
|
|
|
|
|
|
|
|
typedef enum _xdp_action
|
|
|
|
{
|
2021-06-30 19:22:40 +03:00
|
|
|
XDP_PASS = 1, ///< Allow the packet to pass.
|
2021-09-10 08:34:03 +03:00
|
|
|
XDP_DROP, ///< Drop the packet.
|
|
|
|
XDP_TX ///< Bounce the received packet back out the same NIC it arrived on.
|
2021-05-06 23:47:09 +03:00
|
|
|
} xdp_action_t;
|
|
|
|
|
2021-07-02 00:27:24 +03:00
|
|
|
/**
|
|
|
|
* @brief Handle an incoming packet as early as possible.
|
|
|
|
*
|
|
|
|
* Program type: \ref EBPF_PROGRAM_TYPE_XDP
|
|
|
|
*
|
|
|
|
* @param[in] context Packet metadata.
|
|
|
|
* @retval XDP_PASS Allow the packet to pass.
|
|
|
|
* @retval XDP_DROP Drop the packet.
|
|
|
|
*/
|
2021-07-02 02:21:33 +03:00
|
|
|
typedef xdp_action_t
|
|
|
|
xdp_hook_t(xdp_md_t* context);
|
2021-07-02 00:27:24 +03:00
|
|
|
|
2021-09-14 03:46:23 +03:00
|
|
|
// XDP helper functions.
|
|
|
|
#define XDP_EXT_HELPER_FN_BASE 0xFFFF
|
|
|
|
|
|
|
|
#ifndef __doxygen
|
|
|
|
#define EBPF_HELPER(return_type, name, args) typedef return_type(*name##_t) args
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adjust XDP context data pointer.
|
|
|
|
*
|
|
|
|
* @param[in] ctx XDP context.
|
|
|
|
* @param[in] delta Number of bytes to move the data pointer by.
|
|
|
|
*
|
|
|
|
* @retval 0 The operation was successful.
|
|
|
|
* @retval <0 A failure occured.
|
|
|
|
*/
|
|
|
|
EBPF_HELPER(int, bpf_xdp_adjust_head, (xdp_md_t * ctx, int delta));
|
|
|
|
#ifndef __doxygen
|
|
|
|
#define bpf_xdp_adjust_head ((bpf_xdp_adjust_head_t)XDP_EXT_HELPER_FN_BASE + 1)
|
|
|
|
#endif
|
|
|
|
|
2021-09-21 18:46:35 +03:00
|
|
|
/**
|
|
|
|
* @brief Compute checksum difference for replacing parts of a network packet buffer.
|
|
|
|
*
|
|
|
|
* @param[in] from Pointer to raw buffer that will be replaced.
|
|
|
|
* @param[in] from_size Length of the "from" buffer. Must be a multiple of 4.
|
|
|
|
* @param[in] to Pointer to the raw buffer that will replace the previous buffer.
|
|
|
|
* @param[in] to_size Length of the "to" buffer. Must be a multiple of 4.
|
|
|
|
*
|
|
|
|
* @returns The checksum delta on success, or <0 on failure.
|
|
|
|
*/
|
|
|
|
EBPF_HELPER(int, bpf_csum_diff, (void* from, int from_size, void* to, int to_size, int seed));
|
|
|
|
#ifndef __doxygen
|
|
|
|
#define bpf_csum_diff ((bpf_csum_diff_t)XDP_EXT_HELPER_FN_BASE + 2)
|
|
|
|
#endif
|
|
|
|
|
2021-05-06 23:47:09 +03:00
|
|
|
// BIND hook
|
|
|
|
|
|
|
|
typedef enum _bind_operation
|
|
|
|
{
|
2021-06-30 19:22:40 +03:00
|
|
|
BIND_OPERATION_BIND, ///< Entry to bind.
|
|
|
|
BIND_OPERATION_POST_BIND, ///< After port allocation.
|
|
|
|
BIND_OPERATION_UNBIND, ///< Release port.
|
2021-05-06 23:47:09 +03:00
|
|
|
} bind_operation_t;
|
|
|
|
|
2021-07-07 20:39:59 +03:00
|
|
|
typedef struct _bind_md
|
|
|
|
{
|
|
|
|
uint8_t* app_id_start; ///< Pointer to start of App ID.
|
|
|
|
uint8_t* app_id_end; ///< Pointer to end of App ID.
|
|
|
|
uint64_t process_id; ///< Process ID.
|
|
|
|
uint8_t socket_address[16]; ///< Socket address to bind to.
|
|
|
|
uint8_t socket_address_length; ///< Length in bytes of the socket address.
|
|
|
|
bind_operation_t operation; ///< Operation to do.
|
|
|
|
uint8_t protocol; ///< Protocol number (e.g., IPPROTO_TCP).
|
|
|
|
} bind_md_t;
|
|
|
|
|
2021-05-06 23:47:09 +03:00
|
|
|
typedef enum _bind_action
|
|
|
|
{
|
2021-06-30 19:22:40 +03:00
|
|
|
BIND_PERMIT, ///< Permit the bind operation.
|
|
|
|
BIND_DENY, ///< Deny the bind operation.
|
|
|
|
BIND_REDIRECT, ///< Change the bind endpoint.
|
2021-05-06 23:47:09 +03:00
|
|
|
} bind_action_t;
|
2021-07-02 00:27:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle a socket bind() request.
|
|
|
|
*
|
|
|
|
* Program type: \ref EBPF_PROGRAM_TYPE_BIND
|
|
|
|
*
|
|
|
|
* @param[in] context Socket metadata.
|
|
|
|
* @retval BIND_PERMIT Permit the bind operation.
|
|
|
|
* @retval BIND_DENY Deny the bind operation.
|
|
|
|
* @retval BIND_REDIRECT Change the bind endpoint.
|
|
|
|
*/
|
2021-07-02 02:21:33 +03:00
|
|
|
typedef bind_action_t
|
2021-09-21 18:46:35 +03:00
|
|
|
bind_hook_t(bind_md_t* context);
|