eBPF for Windows
ebpf_nethooks.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation
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 // XDP_TEST hook. We use "struct xdp_md" for cross-platform compatibility.
10 typedef struct xdp_md_
11 {
12  void* data;
13  void* data_end;
14  uint64_t data_meta;
15  uint32_t ingress_ifindex;
16 
17  /* size: 26, cachelines: 1, members: 4 */
18  /* last cacheline: 26 bytes */
20 
21 typedef enum _xdp_action
22 {
23  XDP_PASS = 1,
25  XDP_TX
27 
38 typedef xdp_action_t
39 xdp_hook_t(xdp_md_t* context);
40 
41 // XDP_TEST helper functions.
42 #define XDP_EXT_HELPER_FN_BASE 0xFFFF
43 
44 #ifndef __doxygen
45 #define EBPF_HELPER(return_type, name, args) typedef return_type(*const name##_t) args
46 #endif
47 
48 typedef enum
49 {
52 
62 EBPF_HELPER(int, bpf_xdp_adjust_head, (xdp_md_t * ctx, int delta));
63 #ifndef __doxygen
64 #define bpf_xdp_adjust_head ((bpf_xdp_adjust_head_t)BPF_FUNC_xdp_adjust_head)
65 #endif
66 
67 // BIND hook
68 
69 typedef enum _bind_operation
70 {
75 
76 typedef struct _bind_md
77 {
78  uint8_t* app_id_start;
79  uint8_t* app_id_end;
80  uint64_t process_id;
81  uint8_t socket_address[16];
84  uint8_t protocol;
86 
87 typedef enum _bind_action
88 {
93 
104 typedef bind_action_t
106 
107 //
108 // CGROUP_SOCK_ADDR.
109 //
110 
111 #define BPF_SOCK_ADDR_VERDICT_REJECT 0
112 #define BPF_SOCK_ADDR_VERDICT_PROCEED 1
113 
114 #ifdef _MSC_VER
115 #pragma warning(push)
116 #pragma warning(disable : 4201)
117 #endif
121 typedef struct bpf_sock_addr
122 {
123  uint32_t family;
124  struct
125  {
130  union
131  {
132  uint32_t msg_src_ip4;
133  uint32_t msg_src_ip6[4];
134  };
135  uint16_t msg_src_port;
136  };
137  struct
138  {
139  /* @brief Destination IP address in network byte order.
140  * Local for egress, remote for ingress.
141  */
142  union
143  {
144  uint32_t user_ip4;
145  uint32_t user_ip6[4];
146  };
147  uint16_t user_port;
148  };
149  uint32_t protocol;
150  uint32_t compartment_id;
151  uint64_t interface_luid;
153 
154 #define SOCK_ADDR_EXT_HELPER_FN_BASE 0xFFFF
155 
156 typedef enum
157 {
161 
173 EBPF_HELPER(uint64_t, bpf_sock_addr_get_current_pid_tgid, (bpf_sock_addr_t * ctx));
174 #ifndef __doxygen
175 #define bpf_sock_addr_get_current_pid_tgid \
176  ((bpf_sock_addr_get_current_pid_tgid_t)BPF_FUNC_sock_addr_get_current_pid_tgid)
177 #endif
178 
190 EBPF_HELPER(int, bpf_sock_addr_set_redirect_context, (bpf_sock_addr_t * ctx, void* data, uint32_t data_size));
191 #ifndef __doxygen
192 #define bpf_sock_addr_set_redirect_context \
193  ((bpf_sock_addr_set_redirect_context_t)BPF_FUNC_sock_addr_set_redirect_context)
194 #endif
195 
213 typedef int
215 
216 typedef enum _bpf_sock_op_type
217 {
225 
226 typedef struct _bpf_sock_ops
227 {
229  uint32_t family;
230  struct
231  {
232  union
233  {
234  uint32_t local_ip4;
235  uint32_t local_ip6[4];
236  };
237  uint32_t local_port;
238  };
239  struct
240  {
241  union
242  {
243  uint32_t remote_ip4;
244  uint32_t remote_ip6[4];
245  };
246  uint32_t remote_port;
247  };
248  uint8_t protocol;
249  uint32_t compartment_id;
250  uint64_t interface_luid;
252 
265 typedef int
267 
268 #ifdef _MSC_VER
269 #pragma warning(pop)
270 #endif
enum _bind_operation bind_operation_t
#define SOCK_ADDR_EXT_HELPER_FN_BASE
Definition: ebpf_nethooks.h:154
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:157
@ BPF_FUNC_sock_addr_set_redirect_context
Definition: ebpf_nethooks.h:159
@ BPF_FUNC_sock_addr_get_current_pid_tgid
Definition: ebpf_nethooks.h:158
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:214
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:266
_xdp_action
Definition: ebpf_nethooks.h:22
@ XDP_TX
Bounce the received packet back out the same NIC it arrived on.
Definition: ebpf_nethooks.h:25
@ XDP_PASS
Allow the packet to pass.
Definition: ebpf_nethooks.h:23
@ XDP_DROP
Drop the packet.
Definition: ebpf_nethooks.h:24
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:70
@ BIND_OPERATION_BIND
Entry to bind.
Definition: ebpf_nethooks.h:71
@ BIND_OPERATION_UNBIND
Release port.
Definition: ebpf_nethooks.h:73
@ BIND_OPERATION_POST_BIND
After port allocation.
Definition: ebpf_nethooks.h:72
xdp_action_t xdp_hook_t(xdp_md_t *context)
Handle an incoming packet as early as possible.
Definition: ebpf_nethooks.h:39
enum _xdp_action xdp_action_t
struct xdp_md_ xdp_md_t
_bind_action
Definition: ebpf_nethooks.h:88
@ BIND_REDIRECT
Change the bind endpoint.
Definition: ebpf_nethooks.h:91
@ BIND_PERMIT
Permit the bind operation.
Definition: ebpf_nethooks.h:89
@ BIND_DENY
Deny the bind operation.
Definition: ebpf_nethooks.h:90
_bpf_sock_op_type
Definition: ebpf_nethooks.h:217
@ BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
Indicates when a passive (inbound) connection is established.
Definition: ebpf_nethooks.h:221
@ BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB
Indicates when an active (outbound) connection is established.
Definition: ebpf_nethooks.h:219
@ BPF_SOCK_OPS_CONNECTION_DELETED_CB
Indicates when a connection is deleted.
Definition: ebpf_nethooks.h:223
bind_action_t bind_hook_t(bind_md_t *context)
Handle an AF_INET socket bind() request.
Definition: ebpf_nethooks.h:105
struct bpf_sock_addr bpf_sock_addr_t
Data structure used as context for BPF_PROG_TYPE_CGROUP_SOCK_ADDR program type.
ebpf_nethook_helper_id_t
Definition: ebpf_nethooks.h:49
@ BPF_FUNC_xdp_adjust_head
Definition: ebpf_nethooks.h:50
#define XDP_EXT_HELPER_FN_BASE
Definition: ebpf_nethooks.h:42
enum _bind_action bind_action_t
struct _bind_md bind_md_t
struct _bpf_sock_ops bpf_sock_ops_t
int bpf_xdp_adjust_head(xdp_md_t *ctx, int delta)
Adjust XDP_TEST context data pointer.
Definition: ebpf_nethooks.h:77
uint8_t protocol
Protocol number (e.g., IPPROTO_TCP).
Definition: ebpf_nethooks.h:84
bind_operation_t operation
Operation to do.
Definition: ebpf_nethooks.h:83
uint8_t * app_id_end
Pointer to end of App ID.
Definition: ebpf_nethooks.h:79
uint8_t socket_address[16]
Socket address to bind to.
Definition: ebpf_nethooks.h:81
uint64_t process_id
Process ID.
Definition: ebpf_nethooks.h:80
uint8_t * app_id_start
Pointer to start of App ID.
Definition: ebpf_nethooks.h:78
uint8_t socket_address_length
Length in bytes of the socket address.
Definition: ebpf_nethooks.h:82
Definition: ebpf_nethooks.h:227
uint32_t local_ip4
Definition: ebpf_nethooks.h:234
uint32_t family
IP address family.
Definition: ebpf_nethooks.h:229
uint32_t remote_ip4
Definition: ebpf_nethooks.h:243
uint32_t compartment_id
Network compartment Id.
Definition: ebpf_nethooks.h:249
uint8_t protocol
IP protocol.
Definition: ebpf_nethooks.h:248
uint32_t remote_port
Definition: ebpf_nethooks.h:246
uint64_t interface_luid
Interface LUID.
Definition: ebpf_nethooks.h:250
uint32_t local_ip6[4]
Definition: ebpf_nethooks.h:235
bpf_sock_op_type_t op
Definition: ebpf_nethooks.h:228
uint32_t remote_ip6[4]
Definition: ebpf_nethooks.h:244
uint32_t local_port
Definition: ebpf_nethooks.h:237
Data structure used as context for BPF_PROG_TYPE_CGROUP_SOCK_ADDR program type.
Definition: ebpf_nethooks.h:122
uint32_t protocol
IP protocol.
Definition: ebpf_nethooks.h:149
uint16_t user_port
Destination port in network byte order.
Definition: ebpf_nethooks.h:147
uint16_t msg_src_port
Source port in network byte order.
Definition: ebpf_nethooks.h:135
uint32_t user_ip4
Definition: ebpf_nethooks.h:144
uint32_t user_ip6[4]
Definition: ebpf_nethooks.h:145
uint32_t compartment_id
Network compartment Id.
Definition: ebpf_nethooks.h:150
uint32_t msg_src_ip6[4]
Definition: ebpf_nethooks.h:133
uint64_t interface_luid
Interface LUID.
Definition: ebpf_nethooks.h:151
uint32_t msg_src_ip4
Definition: ebpf_nethooks.h:132
uint32_t family
IP address family.
Definition: ebpf_nethooks.h:123
Definition: ebpf_nethooks.h:11
void * data_end
Pointer to end of packet data.
Definition: ebpf_nethooks.h:13
uint64_t data_meta
Packet metadata.
Definition: ebpf_nethooks.h:14
uint32_t ingress_ifindex
Ingress interface index.
Definition: ebpf_nethooks.h:15
void * data
Pointer to start of packet data.
Definition: ebpf_nethooks.h:12