eBPF for Windows
ebpf_nethooks.h
Go to the documentation of this file.
1 // Copyright (c) eBPF for Windows contributors
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 #include <stdint.h>
5 
6 // This file contains APIs for hooks and helpers that are
7 // exposed by netebpfext.sys for use by eBPF programs.
8 
9 #ifndef __doxygen
10 #define EBPF_HELPER(return_type, name, args) typedef return_type(*const name##_t) args
11 #endif
12 
13 // BIND hook
14 
15 typedef enum _bind_operation
16 {
21 
22 typedef struct _bind_md
23 {
24  uint8_t* app_id_start;
25  uint8_t* app_id_end;
26  uint64_t process_id;
27  uint8_t socket_address[16];
30  uint8_t protocol;
32 
33 typedef enum _bind_action
34 {
39 
50 typedef bind_action_t
52 
53 //
54 // CGROUP_SOCK_ADDR.
55 //
56 
57 #define BPF_SOCK_ADDR_VERDICT_REJECT 0
58 #define BPF_SOCK_ADDR_VERDICT_PROCEED 1
59 
60 #ifdef _MSC_VER
61 #pragma warning(push)
62 #pragma warning(disable : 4201)
63 #endif
67 typedef struct bpf_sock_addr
68 {
69  uint32_t family;
70  struct
71  {
76  union
77  {
78  uint32_t msg_src_ip4;
79  uint32_t msg_src_ip6[4];
80  };
81  uint16_t msg_src_port;
82  };
83  struct
84  {
85  /* @brief Destination IP address in network byte order.
86  * Local for egress, remote for ingress.
87  */
88  union
89  {
90  uint32_t user_ip4;
91  uint32_t user_ip6[4];
92  };
93  uint16_t user_port;
94  };
95  uint32_t protocol;
96  uint32_t compartment_id;
97  uint64_t interface_luid;
99 
100 #define SOCK_ADDR_EXT_HELPER_FN_BASE 0xFFFF
101 
102 typedef enum
103 {
107 
121 EBPF_HELPER(uint64_t, bpf_sock_addr_get_current_pid_tgid, (bpf_sock_addr_t * ctx));
122 #ifndef __doxygen
123 #define bpf_sock_addr_get_current_pid_tgid \
124  ((bpf_sock_addr_get_current_pid_tgid_t)BPF_FUNC_sock_addr_get_current_pid_tgid)
125 #endif
126 
138 EBPF_HELPER(int, bpf_sock_addr_set_redirect_context, (bpf_sock_addr_t * ctx, void* data, uint32_t data_size));
139 #ifndef __doxygen
140 #define bpf_sock_addr_set_redirect_context \
141  ((bpf_sock_addr_set_redirect_context_t)BPF_FUNC_sock_addr_set_redirect_context)
142 #endif
143 
161 typedef int
163 
164 typedef enum _bpf_sock_op_type
165 {
173 
174 typedef struct _bpf_sock_ops
175 {
177  uint32_t family;
178  struct
179  {
180  union
181  {
182  uint32_t local_ip4;
183  uint32_t local_ip6[4];
184  };
185  uint32_t local_port;
186  };
187  struct
188  {
189  union
190  {
191  uint32_t remote_ip4;
192  uint32_t remote_ip6[4];
193  };
194  uint32_t remote_port;
195  };
196  uint8_t protocol;
197  uint32_t compartment_id;
198  uint64_t interface_luid;
200 
213 typedef int
215 
216 #ifdef _MSC_VER
217 #pragma warning(pop)
218 #endif
enum _bind_operation bind_operation_t
#define SOCK_ADDR_EXT_HELPER_FN_BASE
Definition: ebpf_nethooks.h:100
uint64_t bpf_sock_addr_get_current_pid_tgid(bpf_sock_addr_t *ctx)
Get current pid and tgid (sock_addr specific only).
ebpf_sock_addr_helper_id_t
Definition: ebpf_nethooks.h:103
@ BPF_FUNC_sock_addr_set_redirect_context
Definition: ebpf_nethooks.h:105
@ BPF_FUNC_sock_addr_get_current_pid_tgid
Definition: ebpf_nethooks.h:104
enum _bpf_sock_op_type bpf_sock_op_type_t
int sock_addr_hook_t(bpf_sock_addr_t *context)
Handle socket operation. Currently supports ingress/egress connection initialization.
Definition: ebpf_nethooks.h:162
int sock_ops_hook_t(bpf_sock_ops_t *context)
Handle socket event notification. Currently notifies ingress/egress connection establishment and tear...
Definition: ebpf_nethooks.h:214
int bpf_sock_addr_set_redirect_context(bpf_sock_addr_t *ctx, void *data, uint32_t data_size)
Set a context for consumption by a user-mode application (sock_addr specific only)....
_bind_operation
Definition: ebpf_nethooks.h:16
@ BIND_OPERATION_BIND
Entry to bind.
Definition: ebpf_nethooks.h:17
@ BIND_OPERATION_UNBIND
Release port.
Definition: ebpf_nethooks.h:19
@ BIND_OPERATION_POST_BIND
After port allocation.
Definition: ebpf_nethooks.h:18
_bind_action
Definition: ebpf_nethooks.h:34
@ BIND_REDIRECT
Change the bind endpoint.
Definition: ebpf_nethooks.h:37
@ BIND_PERMIT
Permit the bind operation.
Definition: ebpf_nethooks.h:35
@ BIND_DENY
Deny the bind operation.
Definition: ebpf_nethooks.h:36
_bpf_sock_op_type
Definition: ebpf_nethooks.h:165
@ BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
Indicates when a passive (inbound) connection is established.
Definition: ebpf_nethooks.h:169
@ BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB
Indicates when an active (outbound) connection is established.
Definition: ebpf_nethooks.h:167
@ BPF_SOCK_OPS_CONNECTION_DELETED_CB
Indicates when a connection is deleted.
Definition: ebpf_nethooks.h:171
bind_action_t bind_hook_t(bind_md_t *context)
Handle an AF_INET socket bind() request.
Definition: ebpf_nethooks.h:51
struct bpf_sock_addr bpf_sock_addr_t
Data structure used as context for BPF_PROG_TYPE_CGROUP_SOCK_ADDR program type.
enum _bind_action bind_action_t
struct _bind_md bind_md_t
struct _bpf_sock_ops bpf_sock_ops_t
Definition: ebpf_nethooks.h:23
uint8_t protocol
Protocol number (e.g., IPPROTO_TCP).
Definition: ebpf_nethooks.h:30
bind_operation_t operation
Operation to do.
Definition: ebpf_nethooks.h:29
uint8_t * app_id_end
Pointer to end of App ID.
Definition: ebpf_nethooks.h:25
uint8_t socket_address[16]
Socket address to bind to.
Definition: ebpf_nethooks.h:27
uint64_t process_id
Process ID.
Definition: ebpf_nethooks.h:26
uint8_t * app_id_start
Pointer to start of App ID.
Definition: ebpf_nethooks.h:24
uint8_t socket_address_length
Length in bytes of the socket address.
Definition: ebpf_nethooks.h:28
Definition: ebpf_nethooks.h:175
uint32_t local_ip4
Definition: ebpf_nethooks.h:182
uint32_t family
IP address family.
Definition: ebpf_nethooks.h:177
uint32_t remote_ip4
Definition: ebpf_nethooks.h:191
uint32_t compartment_id
Network compartment Id.
Definition: ebpf_nethooks.h:197
uint8_t protocol
IP protocol.
Definition: ebpf_nethooks.h:196
uint32_t remote_port
Definition: ebpf_nethooks.h:194
uint64_t interface_luid
Interface LUID.
Definition: ebpf_nethooks.h:198
uint32_t local_ip6[4]
Definition: ebpf_nethooks.h:183
bpf_sock_op_type_t op
Definition: ebpf_nethooks.h:176
uint32_t remote_ip6[4]
Definition: ebpf_nethooks.h:192
uint32_t local_port
Definition: ebpf_nethooks.h:185
Data structure used as context for BPF_PROG_TYPE_CGROUP_SOCK_ADDR program type.
Definition: ebpf_nethooks.h:68
uint32_t protocol
IP protocol.
Definition: ebpf_nethooks.h:95
uint16_t user_port
Destination port in network byte order.
Definition: ebpf_nethooks.h:93
uint16_t msg_src_port
Source port in network byte order.
Definition: ebpf_nethooks.h:81
uint32_t user_ip4
Definition: ebpf_nethooks.h:90
uint32_t user_ip6[4]
Definition: ebpf_nethooks.h:91
uint32_t compartment_id
Network compartment Id.
Definition: ebpf_nethooks.h:96
uint32_t msg_src_ip6[4]
Definition: ebpf_nethooks.h:79
uint64_t interface_luid
Interface LUID.
Definition: ebpf_nethooks.h:97
uint32_t msg_src_ip4
Definition: ebpf_nethooks.h:78
uint32_t family
IP address family.
Definition: ebpf_nethooks.h:69