linux/tools/testing/selftests/bpf/progs/sendmsg6_prog.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (c) 2018 Facebook
   3
   4#include <linux/stddef.h>
   5#include <linux/bpf.h>
   6#include <sys/socket.h>
   7
   8#include <bpf/bpf_helpers.h>
   9#include <bpf/bpf_endian.h>
  10
  11#include <bpf_sockopt_helpers.h>
  12
  13#define SRC_REWRITE_IP6_0       0
  14#define SRC_REWRITE_IP6_1       0
  15#define SRC_REWRITE_IP6_2       0
  16#define SRC_REWRITE_IP6_3       6
  17
  18#define DST_REWRITE_IP6_0       0
  19#define DST_REWRITE_IP6_1       0
  20#define DST_REWRITE_IP6_2       0
  21#define DST_REWRITE_IP6_3       1
  22
  23#define DST_REWRITE_PORT6       6666
  24
  25int _version SEC("version") = 1;
  26
  27SEC("cgroup/sendmsg6")
  28int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
  29{
  30        if (ctx->type != SOCK_DGRAM)
  31                return 0;
  32
  33        if (!get_set_sk_priority(ctx))
  34                return 0;
  35
  36        /* Rewrite source. */
  37        if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
  38            ctx->msg_src_ip6[3] == bpf_htonl(0)) {
  39                ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
  40                ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
  41                ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
  42                ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
  43        } else {
  44                /* Unexpected source. Reject sendmsg. */
  45                return 0;
  46        }
  47
  48        /* Rewrite destination. */
  49        if (ctx->user_ip6[0] == bpf_htonl(0xFACEB00C)) {
  50                ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_0);
  51                ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_1);
  52                ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
  53                ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_3);
  54
  55                ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
  56        } else {
  57                /* Unexpected destination. Reject sendmsg. */
  58                return 0;
  59        }
  60
  61        return 1;
  62}
  63
  64char _license[] SEC("license") = "GPL";
  65