1 // SPDX-License-Identifier: GPL-2.0 2 #include <string.h> 3 #include <netinet/in.h> 4 #include <netinet/tcp.h> 5 #include <linux/bpf.h> 6 #include "bpf_helpers.h" 7 8 char _license[] SEC("license") = "GPL"; 9 __u32 _version SEC("version") = 1; 10 11 #define SOL_CUSTOM 0xdeadbeef 12 13 struct sockopt_sk { 14 __u8 val; 15 }; 16 17 struct { 18 __uint(type, BPF_MAP_TYPE_SK_STORAGE); 19 __uint(map_flags, BPF_F_NO_PREALLOC); 20 __type(key, int); 21 __type(value, struct sockopt_sk); 22 } socket_storage_map SEC(".maps"); 23 24 SEC("cgroup/getsockopt") 25 int _getsockopt(struct bpf_sockopt *ctx) 26 { 27 __u8 *optval_end = ctx->optval_end; 28 __u8 *optval = ctx->optval; 29 struct sockopt_sk *storage; 30 31 if (ctx->level == SOL_IP && ctx->optname == IP_TOS) 32 /* Not interested in SOL_IP:IP_TOS; 33 * let next BPF program in the cgroup chain or kernel 34 * handle it. 35 */ 36 return 1; 37 38 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) { 39 /* Not interested in SOL_SOCKET:SO_SNDBUF; 40 * let next BPF program in the cgroup chain or kernel 41 * handle it. 42 */ 43 return 1; 44 } 45 46 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) { 47 /* Not interested in SOL_TCP:TCP_CONGESTION; 48 * let next BPF program in the cgroup chain or kernel 49 * handle it. 50 */ 51 return 1; 52 } 53 54 if (ctx->level != SOL_CUSTOM) 55 return 0; /* EPERM, deny everything except custom level */ 56 57 if (optval + 1 > optval_end) 58 return 0; /* EPERM, bounds check */ 59 60 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0, 61 BPF_SK_STORAGE_GET_F_CREATE); 62 if (!storage) 63 return 0; /* EPERM, couldn't get sk storage */ 64 65 if (!ctx->retval) 66 return 0; /* EPERM, kernel should not have handled 67 * SOL_CUSTOM, something is wrong! 68 */ 69 ctx->retval = 0; /* Reset system call return value to zero */ 70 71 optval[0] = storage->val; 72 ctx->optlen = 1; 73 74 return 1; 75 } 76 77 SEC("cgroup/setsockopt") 78 int _setsockopt(struct bpf_sockopt *ctx) 79 { 80 __u8 *optval_end = ctx->optval_end; 81 __u8 *optval = ctx->optval; 82 struct sockopt_sk *storage; 83 84 if (ctx->level == SOL_IP && ctx->optname == IP_TOS) 85 /* Not interested in SOL_IP:IP_TOS; 86 * let next BPF program in the cgroup chain or kernel 87 * handle it. 88 */ 89 return 1; 90 91 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) { 92 /* Overwrite SO_SNDBUF value */ 93 94 if (optval + sizeof(__u32) > optval_end) 95 return 0; /* EPERM, bounds check */ 96 97 *(__u32 *)optval = 0x55AA; 98 ctx->optlen = 4; 99 100 return 1; 101 } 102 103 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) { 104 /* Always use cubic */ 105 106 if (optval + 5 > optval_end) 107 return 0; /* EPERM, bounds check */ 108 109 memcpy(optval, "cubic", 5); 110 ctx->optlen = 5; 111 112 return 1; 113 } 114 115 if (ctx->level != SOL_CUSTOM) 116 return 0; /* EPERM, deny everything except custom level */ 117 118 if (optval + 1 > optval_end) 119 return 0; /* EPERM, bounds check */ 120 121 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0, 122 BPF_SK_STORAGE_GET_F_CREATE); 123 if (!storage) 124 return 0; /* EPERM, couldn't get sk storage */ 125 126 storage->val = optval[0]; 127 ctx->optlen = -1; /* BPF has consumed this option, don't call kernel 128 * setsockopt handler. 129 */ 130 131 return 1; 132 } 133