gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/xdp/cmd/bpf/tunnel_host.ebpf.c (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // clang-format off
    16  // Contains types needed by later headers.
    17  #include <linux/types.h>
    18  // clang-format on
    19  #include <bpf/bpf_endian.h>
    20  #include <bpf/bpf_helpers.h>
    21  #include <linux/bpf.h>
    22  #include <linux/if_ether.h>
    23  #include <linux/ip.h>
    24  #include <netinet/in.h>
    25  #include <netinet/tcp.h>
    26  
    27  #define section(secname) __attribute__((section(secname), used))
    28  
    29  char __license[] section("license") = "Apache-2.0";
    30  
    31  // Note: bpf_helpers.h includes a struct definition for bpf_map_def in some, but
    32  // not all, environments. Define our own equivalent struct to avoid issues with
    33  // multiple declarations.
    34  struct gvisor_bpf_map_def {
    35    unsigned int type;
    36    unsigned int key_size;
    37    unsigned int value_size;
    38    unsigned int max_entries;
    39    unsigned int map_flags;
    40  };
    41  
    42  struct gvisor_bpf_map_def section("maps") dev_map = {
    43      .type = BPF_MAP_TYPE_DEVMAP,
    44      .key_size = sizeof(__u32),
    45      .value_size = sizeof(__u32),
    46      .max_entries = 1,
    47  };
    48  
    49  // Redirect almost all incoming traffic to go out another device. Certain
    50  // packets are allowed through to the Linux network stack:
    51  //
    52  //   - SSH (IPv4 TCP port 22) traffic.
    53  //   - Some obviously broken packets.
    54  section("xdp") int xdp_prog(struct xdp_md *ctx) {
    55    void *cursor = (void *)(long)ctx->data;
    56    void *data_end = (void *)(long)ctx->data_end;
    57  
    58    // Ensure there's space for an ethernet header.
    59    struct ethhdr *eth = cursor;
    60    if ((void *)(eth + 1) > data_end) {
    61      return XDP_PASS;
    62    }
    63    cursor += sizeof(*eth);
    64  
    65    // Send all non-IPv4 traffic to the socket.
    66    if (eth->h_proto != bpf_htons(ETH_P_IP)) {
    67      return bpf_redirect_map(&dev_map, ctx->rx_queue_index, XDP_PASS);
    68    }
    69  
    70    // IP packets get inspected to allow SSH traffic to the host.
    71    struct iphdr *ip = cursor;
    72    if ((void *)(ip + 1) > data_end) {
    73      return XDP_PASS;
    74    }
    75    cursor += sizeof(*ip);
    76  
    77    if (ip->protocol != IPPROTO_TCP) {
    78      return bpf_redirect_map(&dev_map, ctx->rx_queue_index, XDP_PASS);
    79    }
    80    struct tcphdr *tcp = cursor;
    81    if ((void *)(tcp + 1) > data_end) {
    82      return XDP_PASS;
    83    }
    84  
    85    // Allow port 22 traffic for SSH debugging.
    86    if (tcp->th_dport == bpf_htons(22)) {
    87      return XDP_PASS;
    88    }
    89  
    90    return bpf_redirect_map(&dev_map, ctx->rx_queue_index, XDP_PASS);
    91  }