You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/ebpf/filter.c

116 lines
2.8 KiB
C

/* Copyright (C) 2018 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <stddef.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/in6.h>
#include <linux/ipv6.h>
#include <linux/filter.h>
#include <bpf/bpf_helpers.h>
#include "llvm_bpfload.h"
#define DEBUG 0
#define LINUX_VERSION_CODE 263682
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, 32768);
} ipv4_drop SEC(".maps");
struct vlan_hdr {
__u16 h_vlan_TCI;
__u16 h_vlan_encapsulated_proto;
};
static __always_inline int ipv4_filter(struct __sk_buff *skb)
{
__u32 nhoff;
__u32 *value;
__u32 ip = 0;
nhoff = skb->cb[0];
ip = load_word(skb, nhoff + offsetof(struct iphdr, saddr));
value = bpf_map_lookup_elem(&ipv4_drop, &ip);
if (value) {
#if DEBUG
char fmt[] = "Found value for saddr: %u\n";
bpf_trace_printk(fmt, sizeof(fmt), value);
#endif
*value = *value + 1;
return 0;
}
ip = load_word(skb, nhoff + offsetof(struct iphdr, daddr));
value = bpf_map_lookup_elem(&ipv4_drop, &ip);
if (value) {
#if DEBUG
char fmt[] = "Found value for daddr: %u\n";
bpf_trace_printk(fmt, sizeof(fmt), value);
#endif
*value = *value + 1;
return 0;
}
#if DEBUG
char fmt[] = "Nothing so ok\n";
bpf_trace_printk(fmt, sizeof(fmt));
#endif
return -1;
}
static __always_inline int ipv6_filter(struct __sk_buff *skb)
{
return -1;
}
int SEC("filter") hashfilter(struct __sk_buff *skb)
{
__u32 nhoff = ETH_HLEN;
__u16 proto = load_half(skb, offsetof(struct ethhdr, h_proto));
if (proto == ETH_P_8021AD || proto == ETH_P_8021Q) {
proto = load_half(skb, nhoff + offsetof(struct vlan_hdr,
h_vlan_encapsulated_proto));
nhoff += sizeof(struct vlan_hdr);
}
skb->cb[0] = nhoff;
switch (proto) {
case ETH_P_IP:
return ipv4_filter(skb);
case ETH_P_IPV6:
return ipv6_filter(skb);
default:
break;
}
return -1;
}
char __license[] SEC("license") = "GPL";
ebpf: take clang -target bpf include issue of stdint.h into account This patch prepares code before enabling the clang -target bpf. The clang compiler does not like #include <stdint.h> when using '-target bpf' it will fail with: fatal error: 'gnu/stubs-32.h' file not found This is because using clang -target bpf, then clang will have '__bpf__' defined instead of '__x86_64__' hence the gnu/stubs-32.h include attempt as /usr/include/gnu/stubs.h contains, on x86_64: #if !defined __x86_64__ # include <gnu/stubs-32.h> #endif #if defined __x86_64__ && defined __LP64__ # include <gnu/stubs-64.h> #endif #if defined __x86_64__ && defined __ILP32__ # include <gnu/stubs-x32.h> #endif This can be worked around by installing the 32-bit version of glibc-devel.i686 on your distribution. But the BPF programs does not really need to include stdint.h, if converting: uint64_t -> __u64 uint32_t -> __u32 uint16_t -> __u16 uint8_t -> __u8 This patch does this type syntax conversion. The build of a ebpf files had an issue for system like Debian because they don't have a asm/types.h in the include path if the architecture is not defined which is the case due to target bpf. This results in: clang-5.0 -Wall -Iinclude -O2 \ -D__KERNEL__ -D__ASM_SYSREG_H \ -target bpf -S -emit-llvm vlan_filter.c -o vlan_filter.ll In file included from vlan_filter.c:19: In file included from include/linux/bpf.h:11: /usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found #include <asm/types.h> ^~~~~~~~~~~~~ 1 error generated. Makefile:523: recipe for target 'vlan_filter.bpf' failed This patch fixes the issue by adding a include path setting the architecture to the one of the builder. Signed-off-by: Jesper Dangaard Brouer <netoptimizer@brouer.com> Sidned-off-by: Eric Leblond <eric@regit.org>
7 years ago
__u32 __version SEC("version") = LINUX_VERSION_CODE;