selftests/bpf: Fix erroneous bitmask operation
[platform/kernel/linux-rpi.git] / tools / testing / selftests / bpf / progs / xdp_redirect_multi_kern.c
1 // SPDX-License-Identifier: GPL-2.0
2 #define KBUILD_MODNAME "foo"
3 #include <string.h>
4 #include <linux/in.h>
5 #include <linux/if_ether.h>
6 #include <linux/if_packet.h>
7 #include <linux/ip.h>
8 #include <linux/ipv6.h>
9
10 #include <linux/bpf.h>
11 #include <bpf/bpf_helpers.h>
12 #include <bpf/bpf_endian.h>
13
14 /* One map use devmap, another one use devmap_hash for testing */
15 struct {
16         __uint(type, BPF_MAP_TYPE_DEVMAP);
17         __uint(key_size, sizeof(int));
18         __uint(value_size, sizeof(int));
19         __uint(max_entries, 1024);
20 } map_all SEC(".maps");
21
22 struct {
23         __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
24         __uint(key_size, sizeof(int));
25         __uint(value_size, sizeof(struct bpf_devmap_val));
26         __uint(max_entries, 128);
27 } map_egress SEC(".maps");
28
29 /* map to store egress interfaces mac addresses */
30 struct {
31         __uint(type, BPF_MAP_TYPE_HASH);
32         __type(key, __u32);
33         __type(value, __be64);
34         __uint(max_entries, 128);
35 } mac_map SEC(".maps");
36
37 SEC("xdp")
38 int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
39 {
40         void *data_end = (void *)(long)ctx->data_end;
41         void *data = (void *)(long)ctx->data;
42         int if_index = ctx->ingress_ifindex;
43         struct ethhdr *eth = data;
44         __u16 h_proto;
45         __u64 nh_off;
46
47         nh_off = sizeof(*eth);
48         if (data + nh_off > data_end)
49                 return XDP_DROP;
50
51         h_proto = eth->h_proto;
52
53         /* Using IPv4 for (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) testing */
54         if (h_proto == bpf_htons(ETH_P_IP))
55                 return bpf_redirect_map(&map_all, 0,
56                                         BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
57         /* Using IPv6 for none flag testing */
58         else if (h_proto == bpf_htons(ETH_P_IPV6))
59                 return bpf_redirect_map(&map_all, if_index, 0);
60         /* All others for BPF_F_BROADCAST testing */
61         else
62                 return bpf_redirect_map(&map_all, 0, BPF_F_BROADCAST);
63 }
64
65 /* The following 2 progs are for 2nd devmap prog testing */
66 SEC("xdp")
67 int xdp_redirect_map_all_prog(struct xdp_md *ctx)
68 {
69         return bpf_redirect_map(&map_egress, 0,
70                                 BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
71 }
72
73 SEC("xdp/devmap")
74 int xdp_devmap_prog(struct xdp_md *ctx)
75 {
76         void *data_end = (void *)(long)ctx->data_end;
77         void *data = (void *)(long)ctx->data;
78         __u32 key = ctx->egress_ifindex;
79         struct ethhdr *eth = data;
80         __u64 nh_off;
81         __be64 *mac;
82
83         nh_off = sizeof(*eth);
84         if (data + nh_off > data_end)
85                 return XDP_DROP;
86
87         mac = bpf_map_lookup_elem(&mac_map, &key);
88         if (mac)
89                 __builtin_memcpy(eth->h_source, mac, ETH_ALEN);
90
91         return XDP_PASS;
92 }
93
94 char _license[] SEC("license") = "GPL";