1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
5 #include "bpf_tracing_net.h"
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
10 #include "bpf_kfuncs.h"
11 #include "crypto_common.h"
13 unsigned char key[256] = {};
14 u16 udp_test_port = 7777;
15 u32 authsize, key_len;
20 static int skb_dynptr_validate(struct __sk_buff *skb, struct bpf_dynptr *psrc)
26 if (skb->protocol != __bpf_constant_htons(ETH_P_IPV6))
29 if (bpf_skb_load_bytes(skb, ETH_HLEN, &ip6h, sizeof(ip6h)))
32 if (ip6h.nexthdr != IPPROTO_UDP)
35 if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(ip6h), &udph, sizeof(udph)))
38 if (udph.dest != __bpf_htons(udp_test_port))
41 offset = ETH_HLEN + sizeof(ip6h) + sizeof(udph);
42 if (skb->len < offset + 16)
45 /* let's make sure that 16 bytes of payload are in the linear part of skb */
46 bpf_skb_pull_data(skb, offset + 16);
47 bpf_dynptr_from_skb(skb, 0, psrc);
48 bpf_dynptr_adjust(psrc, offset, offset + 16);
54 int skb_crypto_setup(void *ctx)
56 struct bpf_crypto_params params = {
61 struct bpf_crypto_ctx *cctx;
71 __builtin_memcpy(¶ms.algo, algo, sizeof(algo));
72 __builtin_memcpy(¶ms.key, key, sizeof(key));
73 cctx = bpf_crypto_ctx_create(¶ms, sizeof(params), &err);
80 err = crypto_ctx_insert(cctx);
81 if (err && err != -EEXIST)
88 int decrypt_sanity(struct __sk_buff *skb)
90 struct __crypto_ctx_value *v;
91 struct bpf_crypto_ctx *ctx;
92 struct bpf_dynptr psrc, pdst;
95 err = skb_dynptr_validate(skb, &psrc);
101 v = crypto_ctx_value_lookup();
113 /* dst is a global variable to make testing part easier to check. In real
114 * production code, a percpu map should be used to store the result.
116 bpf_dynptr_from_mem(dst, sizeof(dst), 0, &pdst);
118 status = bpf_crypto_decrypt(ctx, &psrc, &pdst, NULL);
124 int encrypt_sanity(struct __sk_buff *skb)
126 struct __crypto_ctx_value *v;
127 struct bpf_crypto_ctx *ctx;
128 struct bpf_dynptr psrc, pdst;
133 err = skb_dynptr_validate(skb, &psrc);
139 v = crypto_ctx_value_lookup();
151 /* dst is a global variable to make testing part easier to check. In real
152 * production code, a percpu map should be used to store the result.
154 bpf_dynptr_from_mem(dst, sizeof(dst), 0, &pdst);
156 status = bpf_crypto_encrypt(ctx, &psrc, &pdst, NULL);
161 char __license[] SEC("license") = "GPL";