linux/tools/testing/selftests/bpf/progs/test_select_reuseport_kern.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018 Facebook */
   3
   4#include <stdlib.h>
   5#include <linux/in.h>
   6#include <linux/ip.h>
   7#include <linux/ipv6.h>
   8#include <linux/tcp.h>
   9#include <linux/udp.h>
  10#include <linux/bpf.h>
  11#include <linux/types.h>
  12#include <linux/if_ether.h>
  13
  14#include <bpf/bpf_endian.h>
  15#include <bpf/bpf_helpers.h>
  16#include "test_select_reuseport_common.h"
  17
  18int _version SEC("version") = 1;
  19
  20#ifndef offsetof
  21#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  22#endif
  23
  24struct {
  25        __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
  26        __uint(max_entries, 1);
  27        __uint(key_size, sizeof(__u32));
  28        __uint(value_size, sizeof(__u32));
  29} outer_map SEC(".maps");
  30
  31struct {
  32        __uint(type, BPF_MAP_TYPE_ARRAY);
  33        __uint(max_entries, NR_RESULTS);
  34        __type(key, __u32);
  35        __type(value, __u32);
  36} result_map SEC(".maps");
  37
  38struct {
  39        __uint(type, BPF_MAP_TYPE_ARRAY);
  40        __uint(max_entries, 1);
  41        __type(key, __u32);
  42        __type(value, int);
  43} tmp_index_ovr_map SEC(".maps");
  44
  45struct {
  46        __uint(type, BPF_MAP_TYPE_ARRAY);
  47        __uint(max_entries, 1);
  48        __type(key, __u32);
  49        __type(value, __u32);
  50} linum_map SEC(".maps");
  51
  52struct {
  53        __uint(type, BPF_MAP_TYPE_ARRAY);
  54        __uint(max_entries, 1);
  55        __type(key, __u32);
  56        __type(value, struct data_check);
  57} data_check_map SEC(".maps");
  58
  59#define GOTO_DONE(_result) ({                   \
  60        result = (_result);                     \
  61        linum = __LINE__;                       \
  62        goto done;                              \
  63})
  64
  65SEC("sk_reuseport")
  66int _select_by_skb_data(struct sk_reuseport_md *reuse_md)
  67{
  68        __u32 linum, index = 0, flags = 0, index_zero = 0;
  69        __u32 *result_cnt, *linum_value;
  70        struct data_check data_check = {};
  71        struct cmd *cmd, cmd_copy;
  72        void *data, *data_end;
  73        void *reuseport_array;
  74        enum result result;
  75        int *index_ovr;
  76        int err;
  77
  78        data = reuse_md->data;
  79        data_end = reuse_md->data_end;
  80        data_check.len = reuse_md->len;
  81        data_check.eth_protocol = reuse_md->eth_protocol;
  82        data_check.ip_protocol = reuse_md->ip_protocol;
  83        data_check.hash = reuse_md->hash;
  84        data_check.bind_inany = reuse_md->bind_inany;
  85        if (data_check.eth_protocol == bpf_htons(ETH_P_IP)) {
  86                if (bpf_skb_load_bytes_relative(reuse_md,
  87                                                offsetof(struct iphdr, saddr),
  88                                                data_check.skb_addrs, 8,
  89                                                BPF_HDR_START_NET))
  90                        GOTO_DONE(DROP_MISC);
  91        } else {
  92                if (bpf_skb_load_bytes_relative(reuse_md,
  93                                                offsetof(struct ipv6hdr, saddr),
  94                                                data_check.skb_addrs, 32,
  95                                                BPF_HDR_START_NET))
  96                        GOTO_DONE(DROP_MISC);
  97        }
  98
  99        /*
 100         * The ip_protocol could be a compile time decision
 101         * if the bpf_prog.o is dedicated to either TCP or
 102         * UDP.
 103         *
 104         * Otherwise, reuse_md->ip_protocol or
 105         * the protocol field in the iphdr can be used.
 106         */
 107        if (data_check.ip_protocol == IPPROTO_TCP) {
 108                struct tcphdr *th = data;
 109
 110                if (th + 1 > data_end)
 111                        GOTO_DONE(DROP_MISC);
 112
 113                data_check.skb_ports[0] = th->source;
 114                data_check.skb_ports[1] = th->dest;
 115
 116                if (th->fin)
 117                        /* The connection is being torn down at the end of a
 118                         * test. It can't contain a cmd, so return early.
 119                         */
 120                        return SK_PASS;
 121
 122                if ((th->doff << 2) + sizeof(*cmd) > data_check.len)
 123                        GOTO_DONE(DROP_ERR_SKB_DATA);
 124                if (bpf_skb_load_bytes(reuse_md, th->doff << 2, &cmd_copy,
 125                                       sizeof(cmd_copy)))
 126                        GOTO_DONE(DROP_MISC);
 127                cmd = &cmd_copy;
 128        } else if (data_check.ip_protocol == IPPROTO_UDP) {
 129                struct udphdr *uh = data;
 130
 131                if (uh + 1 > data_end)
 132                        GOTO_DONE(DROP_MISC);
 133
 134                data_check.skb_ports[0] = uh->source;
 135                data_check.skb_ports[1] = uh->dest;
 136
 137                if (sizeof(struct udphdr) + sizeof(*cmd) > data_check.len)
 138                        GOTO_DONE(DROP_ERR_SKB_DATA);
 139                if (data + sizeof(struct udphdr) + sizeof(*cmd) > data_end) {
 140                        if (bpf_skb_load_bytes(reuse_md, sizeof(struct udphdr),
 141                                               &cmd_copy, sizeof(cmd_copy)))
 142                                GOTO_DONE(DROP_MISC);
 143                        cmd = &cmd_copy;
 144                } else {
 145                        cmd = data + sizeof(struct udphdr);
 146                }
 147        } else {
 148                GOTO_DONE(DROP_MISC);
 149        }
 150
 151        reuseport_array = bpf_map_lookup_elem(&outer_map, &index_zero);
 152        if (!reuseport_array)
 153                GOTO_DONE(DROP_ERR_INNER_MAP);
 154
 155        index = cmd->reuseport_index;
 156        index_ovr = bpf_map_lookup_elem(&tmp_index_ovr_map, &index_zero);
 157        if (!index_ovr)
 158                GOTO_DONE(DROP_MISC);
 159
 160        if (*index_ovr != -1) {
 161                index = *index_ovr;
 162                *index_ovr = -1;
 163        }
 164        err = bpf_sk_select_reuseport(reuse_md, reuseport_array, &index,
 165                                      flags);
 166        if (!err)
 167                GOTO_DONE(PASS);
 168
 169        if (cmd->pass_on_failure)
 170                GOTO_DONE(PASS_ERR_SK_SELECT_REUSEPORT);
 171        else
 172                GOTO_DONE(DROP_ERR_SK_SELECT_REUSEPORT);
 173
 174done:
 175        result_cnt = bpf_map_lookup_elem(&result_map, &result);
 176        if (!result_cnt)
 177                return SK_DROP;
 178
 179        bpf_map_update_elem(&linum_map, &index_zero, &linum, BPF_ANY);
 180        bpf_map_update_elem(&data_check_map, &index_zero, &data_check, BPF_ANY);
 181
 182        (*result_cnt)++;
 183        return result < PASS ? SK_DROP : SK_PASS;
 184}
 185
 186char _license[] SEC("license") = "GPL";
 187