1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/ipv6.h>
10 #include <linux/bpf.h>
11 #include <linux/types.h>
12 #include <linux/if_ether.h>
14 #include <bpf/bpf_endian.h>
15 #include <bpf/bpf_helpers.h>
16 #include "test_select_reuseport_common.h"
19 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
23 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
24 __uint(max_entries, 1);
27 } outer_map SEC(".maps");
30 __uint(type, BPF_MAP_TYPE_ARRAY);
31 __uint(max_entries, NR_RESULTS);
34 } result_map SEC(".maps");
37 __uint(type, BPF_MAP_TYPE_ARRAY);
38 __uint(max_entries, 1);
41 } tmp_index_ovr_map SEC(".maps");
44 __uint(type, BPF_MAP_TYPE_ARRAY);
45 __uint(max_entries, 1);
48 } linum_map SEC(".maps");
51 __uint(type, BPF_MAP_TYPE_ARRAY);
52 __uint(max_entries, 1);
54 __type(value, struct data_check);
55 } data_check_map SEC(".maps");
57 #define GOTO_DONE(_result) ({ \
64 int _select_by_skb_data(struct sk_reuseport_md *reuse_md)
66 __u32 linum, index = 0, flags = 0, index_zero = 0;
68 struct data_check data_check = {};
69 struct cmd *cmd, cmd_copy;
70 void *data, *data_end;
71 void *reuseport_array;
76 data = reuse_md->data;
77 data_end = reuse_md->data_end;
78 data_check.len = reuse_md->len;
79 data_check.eth_protocol = reuse_md->eth_protocol;
80 data_check.ip_protocol = reuse_md->ip_protocol;
81 data_check.hash = reuse_md->hash;
82 data_check.bind_inany = reuse_md->bind_inany;
83 if (data_check.eth_protocol == bpf_htons(ETH_P_IP)) {
84 if (bpf_skb_load_bytes_relative(reuse_md,
85 offsetof(struct iphdr, saddr),
86 data_check.skb_addrs, 8,
90 if (bpf_skb_load_bytes_relative(reuse_md,
91 offsetof(struct ipv6hdr, saddr),
92 data_check.skb_addrs, 32,
98 * The ip_protocol could be a compile time decision
99 * if the bpf_prog.o is dedicated to either TCP or
102 * Otherwise, reuse_md->ip_protocol or
103 * the protocol field in the iphdr can be used.
105 if (data_check.ip_protocol == IPPROTO_TCP) {
106 struct tcphdr *th = data;
108 if (th + 1 > data_end)
109 GOTO_DONE(DROP_MISC);
111 data_check.skb_ports[0] = th->source;
112 data_check.skb_ports[1] = th->dest;
115 /* The connection is being torn down at the end of a
116 * test. It can't contain a cmd, so return early.
120 if ((th->doff << 2) + sizeof(*cmd) > data_check.len)
121 GOTO_DONE(DROP_ERR_SKB_DATA);
122 if (bpf_skb_load_bytes(reuse_md, th->doff << 2, &cmd_copy,
124 GOTO_DONE(DROP_MISC);
126 } else if (data_check.ip_protocol == IPPROTO_UDP) {
127 struct udphdr *uh = data;
129 if (uh + 1 > data_end)
130 GOTO_DONE(DROP_MISC);
132 data_check.skb_ports[0] = uh->source;
133 data_check.skb_ports[1] = uh->dest;
135 if (sizeof(struct udphdr) + sizeof(*cmd) > data_check.len)
136 GOTO_DONE(DROP_ERR_SKB_DATA);
137 if (data + sizeof(struct udphdr) + sizeof(*cmd) > data_end) {
138 if (bpf_skb_load_bytes(reuse_md, sizeof(struct udphdr),
139 &cmd_copy, sizeof(cmd_copy)))
140 GOTO_DONE(DROP_MISC);
143 cmd = data + sizeof(struct udphdr);
146 GOTO_DONE(DROP_MISC);
149 reuseport_array = bpf_map_lookup_elem(&outer_map, &index_zero);
150 if (!reuseport_array)
151 GOTO_DONE(DROP_ERR_INNER_MAP);
153 index = cmd->reuseport_index;
154 index_ovr = bpf_map_lookup_elem(&tmp_index_ovr_map, &index_zero);
156 GOTO_DONE(DROP_MISC);
158 if (*index_ovr != -1) {
162 err = bpf_sk_select_reuseport(reuse_md, reuseport_array, &index,
167 if (cmd->pass_on_failure)
168 GOTO_DONE(PASS_ERR_SK_SELECT_REUSEPORT);
170 GOTO_DONE(DROP_ERR_SK_SELECT_REUSEPORT);
173 result_cnt = bpf_map_lookup_elem(&result_map, &result);
177 bpf_map_update_elem(&linum_map, &index_zero, &linum, BPF_ANY);
178 bpf_map_update_elem(&data_check_map, &index_zero, &data_check, BPF_ANY);
181 return result < PASS ? SK_DROP : SK_PASS;
184 char _license[] SEC("license") = "GPL";