Mitigator BPF API
Functions and types programs can use to filter packets.
mitigator_bpf.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2020 BIFIT <mitigator@bifit.com>
3  */
4 #pragma once
5 
6 #include <stdbool.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #define ABI_V1_ENTRYPOINT "filter_v1"
11 
12 #define SECTION(name) __attribute__((section(name), used))
13 
37 #define PROGRAM_DISPLAY_ID(id) \
38  SECTION("meta.display_id") \
39  static const char _mitigator_meta_program_id[40] = id;
40 
58 #define FILTER_V1 SECTION(ABI_V1_ENTRYPOINT)
59 
76 #define LOCAL static inline __attribute__((always_inline))
77 
103 #if defined(__clang__)
104 #define UNROLL _Pragma("unroll")
105 #else
106 #define UNROLL
107 #endif
108 
119 #define MAX_PAYLOAD_LENGTH 1536
120 
130 #define MAX_PARAMETERS_LENGTH 1024
131 
132 #ifdef __cplusplus
133 namespace mitigator {
134 #endif
135 
137 typedef void* Context;
138 
145 enum Result {
154 };
155 
161 typedef uint64_t Bool;
162 
168 typedef uint32_t Time;
169 
188 struct IpHeader {
189  uint32_t ip_hl : 4; /* 0 header length */
190  uint32_t ip_v : 4; /* version == 4 */
191  uint8_t ip_tos; /* 1 type of service */
192  uint16_t ip_len; /* 2-3 total length */
193  uint16_t ip_id; /* 4-5 packet ID */
194  uint16_t ip_off; /* 6-7 fragmentation offset */
195  uint8_t ip_ttl; /* 8 time to live */
196  uint8_t ip_p; /* 9 protocol ID */
197  uint16_t ip_sum; /* 10-11 header checksum */
198  uint32_t ip_src; /* 12-15 source address */
199  uint32_t ip_dst; /* 16-19 destination address */
200 };
201 
222 struct UdpHeader {
223  uint16_t uh_sport; /* 0-1 source port */
224  uint16_t uh_dport; /* 2-3 destination port */
225  uint16_t uh_ulen; /* 4-5 UDP length */
226  uint16_t uh_sum; /* 6-7 checksum */
227 };
228 
249 struct TcpHeader {
250  uint16_t th_sport; /* 0-1 source port */
251  uint16_t th_dport; /* 2-3 destination port */
252  uint32_t th_seq; /* 4-7 sequence number */
253  uint32_t th_ack; /* 8-11 acknowledgement number */
254  uint32_t th_flags2 : 4; /* 12 more flags */
255  uint32_t th_off : 4; /* data offset in words */
256  uint8_t th_flags; /* 13 flags */
257  uint16_t th_win; /* 14-15 window */
258  uint16_t th_sum; /* 16-17 checksum */
259  uint16_t th_urp; /* 18-19 urgent pointer */
260 };
261 
266 enum EtherType {
267  ETHER_TYPE_IP = 0x0800,
268  ETHER_TYPE_ARP = 0x0806,
269  ETHER_TYPE_8021Q = 0x8100,
270  ETHER_TYPE_IP6 = 0x86DD
271 };
272 
278 enum IpProto {
282 };
283 
312 enum TcpFlags {
313  TCP_FLAG_FIN = 0x01,
314  TCP_FLAG_SYN = 0x02,
315  TCP_FLAG_RST = 0x04,
316  TCP_FLAG_PUSH = 0x08,
317  TCP_FLAG_ACK = 0x10,
318  TCP_FLAG_URG = 0x20,
319  TCP_FLAG_ECE = 0x40,
320  TCP_FLAG_CWR = 0x80
321 };
322 
331 enum TcpOption {
339 };
340 
352 struct Flow {
353  uint32_t saddr;
354  uint32_t daddr;
355  uint16_t sport;
356  uint16_t dport;
357  uint32_t padding;
358 };
359 
365 void packet_flow(Context ctx, struct Flow* info);
366 
380 uint16_t packet_network_proto(Context ctx);
381 
397 void* packet_network_header(Context ctx);
398 
407 uint8_t packet_transport_proto(Context ctx);
408 
426 
439 void* packet_transport_payload(Context ctx, uint16_t* length);
440 
447 void set_packet_length(Context ctx, uint16_t length);
448 
458 void set_packet_offset(Context ctx, uint16_t offset);
459 
470 void set_packet_syncookie(Context ctx);
471 
486 void set_packet_mangled(Context ctx);
487 
493 void set_src_blacklisted(Context ctx, Time duration);
494 
500 void set_src_whitelisted(Context ctx, Time duration);
501 
510 typedef uint64_t TableKey;
511 
517 typedef uint64_t TableValue;
518 
528 struct TableRecord {
531  uint32_t padding;
532 };
533 
539 Bool table_find(Context ctx, TableKey key, struct TableRecord* record);
540 
548 Bool table_get(Context ctx, TableKey key, struct TableRecord* record);
549 
558 Bool table_put(Context ctx, TableKey key, TableValue value);
559 
561 uint64_t table_size(Context ctx);
562 
564 typedef uint32_t Cookie;
565 
575 
590 Bool syncookie_check(Context ctx, uint32_t seqnum_offset, uint32_t acknum_offset);
591 
616 Cookie cookie_make(Context ctx, const struct Flow* id);
617 
639 Bool cookie_check(Context ctx, const struct Flow* id, Cookie cookie);
640 
649 const void* parameters_get(Context ctx);
650 
665 uint32_t hash_crc32_u32(uint32_t value, uint32_t init);
666 
672 uint32_t hash_crc32_u64(uint64_t value, uint32_t init);
673 
704 uint32_t hash_crc32_data(const void* data, const void* end, uint32_t init);
705 
707 Time time_sec(Context ctx);
708 
710 uint64_t rand64(void);
711 
713 LOCAL uint16_t
714 bswap16(uint16_t value) {
715  return __builtin_bswap16(value);
716 }
717 
719 LOCAL uint32_t
720 bswap32(uint32_t value) {
721  return __builtin_bswap32(value);
722 }
723 
724 #ifdef __cplusplus
725 } // namespace mitigator
726 #endif
Cookie
uint32_t Cookie
Definition: mitigator_bpf.h:564
IP_PROTO_ICMP
@ IP_PROTO_ICMP
Definition: mitigator_bpf.h:279
cookie_check
Bool cookie_check(Context ctx, const struct Flow *id, Cookie cookie)
Check if a generic cookie matches the flow and has not expired.
TCP_OPT_WSCALE
@ TCP_OPT_WSCALE
Definition: mitigator_bpf.h:335
hash_crc32_u32
uint32_t hash_crc32_u32(uint32_t value, uint32_t init)
Compute CRC32 (Castagnoli) of a 32-bit value.
syncookie_make
Cookie syncookie_make(Context ctx)
Make a cookie value for use as a sequence number in a SYN+ACK packet.
table_find
Bool table_find(Context ctx, TableKey key, struct TableRecord *record)
Lookup value in the table by key.
RESULT_DROP
@ RESULT_DROP
Definition: mitigator_bpf.h:149
set_packet_syncookie
void set_packet_syncookie(Context ctx)
Convert response packet to an empty TCP SYN+ACK with syncookie.
bswap32
LOCAL uint32_t bswap32(uint32_t value)
Change byte order of a 32-bit value.
Definition: mitigator_bpf.h:720
LOCAL
#define LOCAL
Force the compiler to inline a local function.
Definition: mitigator_bpf.h:76
packet_transport_payload
void * packet_transport_payload(Context ctx, uint16_t *length)
Get packet transport payload for TCP or UDP.
TCP_OPT_MAXSEG
@ TCP_OPT_MAXSEG
Definition: mitigator_bpf.h:334
set_src_whitelisted
void set_src_whitelisted(Context ctx, Time duration)
Add packet source address to temporary whitelist.
Result
Result
Filter verdict.
Definition: mitigator_bpf.h:145
Time
uint32_t Time
Definition: mitigator_bpf.h:168
table_size
uint64_t table_size(Context ctx)
Get number of records in the table.
TCP_OPT_TIMESTAMPS
@ TCP_OPT_TIMESTAMPS
Definition: mitigator_bpf.h:338
packet_transport_proto
uint8_t packet_transport_proto(Context ctx)
Get packet transport protocol code, e.g. TCP, UDP, or ICMP.
hash_crc32_u64
uint32_t hash_crc32_u64(uint64_t value, uint32_t init)
Compute CRC32 (Castagnoli) of a 64-bit value.
TCP_OPT_NOP
@ TCP_OPT_NOP
Definition: mitigator_bpf.h:333
set_src_blacklisted
void set_src_blacklisted(Context ctx, Time duration)
Add packet source address to temporary blacklist.
Bool
uint64_t Bool
ABI-safe, eBPF-friendly boolean type.
Definition: mitigator_bpf.h:161
TcpFlags
TcpFlags
TCP flags.
Definition: mitigator_bpf.h:312
TcpOption
TcpOption
TCP option codes.
Definition: mitigator_bpf.h:331
TcpHeader
TCP header.
Definition: mitigator_bpf.h:249
RESULT_LIMIT
@ RESULT_LIMIT
Definition: mitigator_bpf.h:153
Flow::daddr
uint32_t daddr
Definition: mitigator_bpf.h:354
parameters_get
const void * parameters_get(Context ctx)
Get a pointer to read-only program parameters.
EtherType
EtherType
Ethernet frame type codes.
Definition: mitigator_bpf.h:266
RESULT_BACK
@ RESULT_BACK
Definition: mitigator_bpf.h:151
TCP_OPT_SACK
@ TCP_OPT_SACK
Definition: mitigator_bpf.h:337
Flow::dport
uint16_t dport
Definition: mitigator_bpf.h:356
ETHER_TYPE_8021Q
@ ETHER_TYPE_8021Q
Definition: mitigator_bpf.h:269
ETHER_TYPE_IP
@ ETHER_TYPE_IP
Definition: mitigator_bpf.h:267
Context
void * Context
Opaque filter context.
Definition: mitigator_bpf.h:137
packet_network_header
void * packet_network_header(Context ctx)
Get packet network header, e.g. IPv4 header.
table_get
Bool table_get(Context ctx, TableKey key, struct TableRecord *record)
Lookup value in the table by key and modify record update time.
TableValue
uint64_t TableValue
Definition: mitigator_bpf.h:517
syncookie_check
Bool syncookie_check(Context ctx, uint32_t seqnum_offset, uint32_t acknum_offset)
Check if packet is a TCP ACK carrying a SYN cookie.
hash_crc32_data
uint32_t hash_crc32_data(const void *data, const void *end, uint32_t init)
Compute CRC32 (Castagnoli) over [data; end).
table_put
Bool table_put(Context ctx, TableKey key, TableValue value)
Update value in the table, creating a new record if needed.
TableRecord::update_time
Time update_time
Definition: mitigator_bpf.h:530
ETHER_TYPE_IP6
@ ETHER_TYPE_IP6
Definition: mitigator_bpf.h:270
TableKey
uint64_t TableKey
Definition: mitigator_bpf.h:510
IpHeader
IPv4 header.
Definition: mitigator_bpf.h:188
TableRecord
Record in the program-wide table.
Definition: mitigator_bpf.h:528
TCP_OPT_EOL
@ TCP_OPT_EOL
Definition: mitigator_bpf.h:332
RESULT_PASS
@ RESULT_PASS
Definition: mitigator_bpf.h:147
time_sec
Time time_sec(Context ctx)
Get wallclock time in seconds.
set_packet_mangled
void set_packet_mangled(Context ctx)
Mark the packet as mangled by the program.
TableRecord::value
TableValue value
Definition: mitigator_bpf.h:529
IP_PROTO_TCP
@ IP_PROTO_TCP
Definition: mitigator_bpf.h:280
bswap16
LOCAL uint16_t bswap16(uint16_t value)
Change byte order of a 16-bit value.
Definition: mitigator_bpf.h:714
Flow::sport
uint16_t sport
Definition: mitigator_bpf.h:355
rand64
uint64_t rand64(void)
Generate a pseudo-random, non cryptographically-secure value.
set_packet_length
void set_packet_length(Context ctx, uint16_t length)
Set new packet transport payload length (max 1400).
ETHER_TYPE_ARP
@ ETHER_TYPE_ARP
Definition: mitigator_bpf.h:268
Flow::padding
uint32_t padding
Definition: mitigator_bpf.h:357
cookie_make
Cookie cookie_make(Context ctx, const struct Flow *id)
Make a generic cookie value based on random seed, current time, and flow fields that identify the cli...
UdpHeader
UDP header.
Definition: mitigator_bpf.h:222
Flow::saddr
uint32_t saddr
Definition: mitigator_bpf.h:353
packet_flow
void packet_flow(Context ctx, struct Flow *info)
Get packet flow information, including source and destination.
set_packet_offset
void set_packet_offset(Context ctx, uint16_t offset)
Set number of bytes to strip from the beginning of transport payload.
packet_transport_header
void * packet_transport_header(Context ctx)
Get packet transport header, e.g. TCP header.
packet_network_proto
uint16_t packet_network_proto(Context ctx)
Get packet network protocol code, e.g. IPv4.
TCP_OPT_SACK_PERM
@ TCP_OPT_SACK_PERM
Definition: mitigator_bpf.h:336
TableRecord::padding
uint32_t padding
Definition: mitigator_bpf.h:531
IpProto
IpProto
IPv4 and IPv6 transport protocol codes.
Definition: mitigator_bpf.h:278
Flow
Packet flow information.
Definition: mitigator_bpf.h:352
IP_PROTO_UDP
@ IP_PROTO_UDP
Definition: mitigator_bpf.h:281