mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-05 01:54:09 +08:00
96a8eb1eee
Some of the sample files are causing issues when they are loaded with tc and cls_bpf, meaning tc bails out while trying to parse the resulting ELF file as program/map/etc sections are not present, which can be easily spotted with readelf(1). Currently, BPF samples are including some of the kernel headers and mid term we should change them to refrain from this, really. When dynamic debugging is enabled, we bail out due to undeclared KBUILD_MODNAME, which is easily overlooked in the build as clang spills this along with other noisy warnings from various header includes, and llc still generates an ELF file with mentioned characteristics. For just playing around with BPF examples, this can be a bit of a hurdle to take. Just add a fake KBUILD_MODNAME as a band-aid to fix the issue, same is done in xdp*_kern samples already. Fixes:65d472fb00
("samples/bpf: add 'pointer to packet' tests") Fixes:6afb1e28b8
("samples/bpf: Add tunnel set/get tests.") Fixes:a3f7461734
("cgroup: bpf: Add an example to do cgroup checking in BPF") Reported-by: Chandrasekar Kannan <ckannan@console.to> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
43 lines
1.2 KiB
C
43 lines
1.2 KiB
C
/* Copyright (c) 2016 Facebook
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
* License as published by the Free Software Foundation.
|
|
*/
|
|
#define KBUILD_MODNAME "foo"
|
|
#include <linux/ip.h>
|
|
#include <linux/ipv6.h>
|
|
#include <linux/in.h>
|
|
#include <linux/tcp.h>
|
|
#include <linux/udp.h>
|
|
#include <uapi/linux/bpf.h>
|
|
#include "bpf_helpers.h"
|
|
|
|
#define DEFAULT_PKTGEN_UDP_PORT 9
|
|
#define IP_MF 0x2000
|
|
#define IP_OFFSET 0x1FFF
|
|
|
|
static inline int ip_is_fragment(struct __sk_buff *ctx, __u64 nhoff)
|
|
{
|
|
return load_half(ctx, nhoff + offsetof(struct iphdr, frag_off))
|
|
& (IP_MF | IP_OFFSET);
|
|
}
|
|
|
|
SEC("ldabs")
|
|
int handle_ingress(struct __sk_buff *skb)
|
|
{
|
|
__u64 troff = ETH_HLEN + sizeof(struct iphdr);
|
|
|
|
if (load_half(skb, offsetof(struct ethhdr, h_proto)) != ETH_P_IP)
|
|
return 0;
|
|
if (load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol)) != IPPROTO_UDP ||
|
|
load_byte(skb, ETH_HLEN) != 0x45)
|
|
return 0;
|
|
if (ip_is_fragment(skb, ETH_HLEN))
|
|
return 0;
|
|
if (load_half(skb, troff + offsetof(struct udphdr, dest)) == DEFAULT_PKTGEN_UDP_PORT)
|
|
return TC_ACT_SHOT;
|
|
return 0;
|
|
}
|
|
char _license[] SEC("license") = "GPL";
|