10#define ENTRYPOINT_SECTION "filter_v2"
12#define SECTION(name) __attribute__((section(name), used))
15#define STATIC_ASSERT(x) static_assert(x, "")
17#define STATIC_ASSERT(x) _Static_assert(x, "")
43#define PROGRAM_DISPLAY_ID(id) \
44 SECTION("meta.display_id") \
45 static const char _mitigator_meta_program_id[] = id;
64#define ENTRYPOINT SECTION(ENTRYPOINT_SECTION)
82#define LOCAL static inline __attribute__((always_inline))
98#define PACKED __attribute__((packed))
125#if defined(__clang__)
126#define UNROLL _Pragma("unroll")
141#define MAX_PAYLOAD_LENGTH 1536
152#define MAX_PARAMETERS_LENGTH 1024
240 ETHER_TYPE_IP = 0x0800,
241 ETHER_TYPE_ARP = 0x0806,
242 ETHER_TYPE_8021Q = 0x8100,
243 ETHER_TYPE_IP6 = 0x86DD
329 IP_PROTO_ICMPV6 = 58,
384 uint32_t th_flags2 : 4;
424 TCP_FLAG_PUSH = 0x08,
444 TCP_OPT_SACK_PERM = 4,
446 TCP_OPT_TIMESTAMPS = 8
469 ICMP_DEST_UNREACHABLE = 3,
470 ICMP_SOURCE_QUENCH = 4,
473 ICMP_TIME_EXCEEDED = 11,
474 ICMP_PARAM_PROBLEM = 12,
475 ICMP_TIMESTAMP_REQUEST = 13,
476 ICMP_TIMESTAMP_REPLY = 14,
477 ICMP_INFO_REQUEST = 15,
478 ICMP_INFO_REPLY = 16,
483 ICMP6_DEST_UNREACHABLE = 1,
484 ICMP6_PKT_TOO_BIG = 2,
485 ICMP6_TIME_EXCEEDED = 3,
486 ICMP6_PARAM_PROBLEM = 4,
487 ICMP6_ECHO_REQUEST = 128,
488 ICMP6_ECHO_REPLY = 129,
489 ICMP6_ROUTER_SOL = 133,
490 ICMP6_ROUTER_ADV = 134,
491 ICMP6_NEIGHBOR_SOL = 135,
492 ICMP6_NEIGHBOR_ADV = 136,
768#define TABLE_EX_KEY_SIZE 16
771#define TABLE_EX_VALUE_SIZE 8
783 void* value,
void* value_end);
795 void* value,
void* value_end);
808 const void* value,
const void* value_end);
907 uint32_t acknum_offset);
1005 return __builtin_bswap16(value);
1011 return __builtin_bswap32(value);
1017 return __builtin_bswap64(value);
1021STATIC_ASSERT(
sizeof(
struct VlanHeader) == 4);
1022STATIC_ASSERT(
sizeof(
struct IpHeader) == 20);
1023STATIC_ASSERT(
sizeof(
struct Ip6Addr) == 16);
1024STATIC_ASSERT(
sizeof(
struct Ip6Header) == 40);
1025STATIC_ASSERT(
sizeof(
struct TcpHeader) == 20);
1026STATIC_ASSERT(
sizeof(
struct UdpHeader) == 8);
1027STATIC_ASSERT(
sizeof(
struct IcmpHeader) == 4);
EtherType
Ethernet frame type codes.
Definition mitigator_bpf.h:239
uint8_t packet_transport_proto(Context ctx)
Get packet transport protocol code, e.g. TCP, UDP, or ICMP.
struct TableExResult table_ex_get(Context ctx, const void *key, const void *key_end, void *value, void *value_end)
Lookup value in the extended table by key and modify record update time.
void * packet_ether_header(Context ctx)
Get packet Ethernet header.
uint64_t TableValue
Definition mitigator_bpf.h:695
void * Context
Opaque filter context.
Definition mitigator_bpf.h:160
LOCAL uint16_t bswap16(uint16_t value)
Change byte order of a 16-bit value.
Definition mitigator_bpf.h:1004
uint64_t table_ex_size(Context ctx)
Get number of records in the extended table.
IcmpType
ICMPv4 types.
Definition mitigator_bpf.h:467
void bloom_reset(Context ctx)
Reset bloom filter to the initial state.
uint32_t hash_crc32_data(const void *data, const void *end, uint32_t init)
Compute CRC32 (Castagnoli) over [data; end).
uint32_t Time
Definition mitigator_bpf.h:196
void set_packet_offset(Context ctx, uint16_t offset)
Set number of bytes to strip from the beginning of transport payload.
uint16_t packet_network_proto(Context ctx)
Get packet network protocol code, e.g. IPv4.
Result
Filter verdict.
Definition mitigator_bpf.h:167
@ RESULT_LIMIT
Definition mitigator_bpf.h:175
@ RESULT_DROP
Definition mitigator_bpf.h:171
@ RESULT_SORB
Definition mitigator_bpf.h:181
@ RESULT_PASS
Definition mitigator_bpf.h:169
@ RESULT_BACK
Definition mitigator_bpf.h:173
void * packet_transport_payload(Context ctx, uint16_t *length)
Get packet transport payload for TCP, UDP, or ICMP.
#define LOCAL
Force the compiler to inline a local function.
Definition mitigator_bpf.h:82
Bool table_get(Context ctx, TableKey key, struct TableRecord *record)
Lookup value in the basic table by key and modify record update time.
uint32_t hash_crc32_u64(uint64_t value, uint32_t init)
Compute CRC32 (Castagnoli) of a 64-bit value.
TcpOption
TCP option codes.
Definition mitigator_bpf.h:439
void * packet_network_header(Context ctx)
Get packet network header, e.g. IPv4 header.
IpProto
IPv4 and IPv6 transport protocol codes.
Definition mitigator_bpf.h:324
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...
TcpFlags
TCP flags.
Definition mitigator_bpf.h:420
Cookie syncookie_make(Context ctx)
Make a cookie value for use as a sequence number in a SYN+ACK packet.
LOCAL uint64_t bswap64(uint64_t value)
Change byte order of a 64-bit value.
Definition mitigator_bpf.h:1016
void set_packet_length(Context ctx, uint16_t length)
Set new packet transport payload length (max 1400).
uint64_t table_size(Context ctx)
Get number of records in the table.
uint32_t IpAddr
IPv4 address.
Definition mitigator_bpf.h:247
Bool table_ex_put(Context ctx, const void *key, const void *key_end, const void *value, const void *value_end)
Update value in the extended table, creating a new record if needed.
void * packet_transport_header(Context ctx)
Get packet transport header, e.g. TCP header.
Bool isn_syncookie_check(Context ctx, uint32_t seqnum_offset, uint32_t acknum_offset)
Check if packet is a TCP ACK carrying an ISN SYN cookie.
struct TableExResult table_ex_find(Context ctx, const void *key, const void *key_end, void *value, void *value_end)
Lookup value in the extended table by key.
LOCAL uint32_t bswap32(uint32_t value)
Change byte order of a 32-bit value.
Definition mitigator_bpf.h:1010
uint64_t rand64(void)
Generate a pseudo-random, non cryptographically-secure value.
uint32_t hash_crc32_u32(uint32_t value, uint32_t init)
Compute CRC32 (Castagnoli) of a 32-bit value.
Icmp6Type
ICMPv6 types.
Definition mitigator_bpf.h:482
Bool syncookie_check(Context ctx, uint32_t seqnum_offset, uint32_t acknum_offset)
Check if packet is a TCP ACK carrying a SYN cookie.
Bool cookie_check(Context ctx, const struct Flow *id, Cookie cookie)
Check if a generic cookie matches the flow and has not expired.
void set_packet_isn_syncookie(Context ctx)
Convert response packet to an empty TCP SYN+ACK with ISN syncookie.
Bool learning_check(Context ctx)
Check if CMON has learned session for current packet.
void set_src_blacklisted(Context ctx, Time duration)
Add packet source address to temporary blacklist.
Bool table_put(Context ctx, TableKey key, TableValue value)
Update value in the basic table, creating a new record if needed.
const void * parameters_get(Context ctx)
Get a pointer to read-only program parameters.
Bool bloom_check(Context ctx, uint64_t hash)
Check if hash value is stored in the bloom filter.
void bloom_add(Context ctx, uint64_t hash)
Add hash value to the bloom filter.
Time time_sec(Context ctx)
Get system time in seconds.
uint64_t Bool
ABI-safe, EBPF-friendly boolean type.
Definition mitigator_bpf.h:189
void isn_send_ack_packet(Context ctx)
Send ACK packet with ISN syncookie.
Bool table_find(Context ctx, TableKey key, struct TableRecord *record)
Lookup value in the basic table by key.
void set_src_whitelisted(Context ctx, Time duration)
Add packet source address to temporary whitelist.
uint64_t TableKey
Definition mitigator_bpf.h:688
void set_packet_mangled(Context ctx)
Mark the packet as mangled by the program.
void set_packet_syncookie(Context ctx)
Convert response packet to an empty TCP SYN+ACK with syncookie.
void packet_flow(Context ctx, struct Flow *info)
Get packet flow information, including source and destination.
uint32_t Cookie
Definition mitigator_bpf.h:814
Ethernet address (MAC address).
Definition mitigator_bpf.h:209
Packet flow information.
Definition mitigator_bpf.h:515
IPv6 address representation.
Definition mitigator_bpf.h:287
Result of a lookup in the extended table.
Definition mitigator_bpf.h:761
Record in the program-wide table.
Definition mitigator_bpf.h:709
Network address, either IPv4 or IPv6.
Definition mitigator_bpf.h:496