Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 /*
0004  * Copyright 2020 Google LLC.
0005  */
0006 
0007 #include <errno.h>
0008 #include <linux/bpf.h>
0009 #include <linux/if_ether.h>
0010 #include <linux/ip.h>
0011 #include <bpf/bpf_helpers.h>
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_ARRAY);
0015     __uint(max_entries, 1);
0016     __type(key, __u32);
0017     __type(value, __u32);
0018 } test_result SEC(".maps");
0019 
0020 SEC("cgroup_skb/egress")
0021 int load_bytes_relative(struct __sk_buff *skb)
0022 {
0023     struct ethhdr eth;
0024     struct iphdr iph;
0025 
0026     __u32 map_key = 0;
0027     __u32 test_passed = 0;
0028 
0029     /* MAC header is not set by the time cgroup_skb/egress triggers */
0030     if (bpf_skb_load_bytes_relative(skb, 0, &eth, sizeof(eth),
0031                     BPF_HDR_START_MAC) != -EFAULT)
0032         goto fail;
0033 
0034     if (bpf_skb_load_bytes_relative(skb, 0, &iph, sizeof(iph),
0035                     BPF_HDR_START_NET))
0036         goto fail;
0037 
0038     if (bpf_skb_load_bytes_relative(skb, 0xffff, &iph, sizeof(iph),
0039                     BPF_HDR_START_NET) != -EFAULT)
0040         goto fail;
0041 
0042     test_passed = 1;
0043 
0044 fail:
0045     bpf_map_update_elem(&test_result, &map_key, &test_passed, BPF_ANY);
0046 
0047     return 1;
0048 }