1 // SPDX-License-Identifier: GPL-2.0 2 #include <netinet/in.h> 3 #include <linux/bpf.h> 4 #include <bpf/bpf_helpers.h> 5 6 char _license[] SEC("license") = "GPL"; 7 8 SEC("cgroup/getsockopt") 9 int _getsockopt_child(struct bpf_sockopt *ctx) 10 { 11 __u8 *optval_end = ctx->optval_end; 12 __u8 *optval = ctx->optval; 13 14 if (ctx->level != SOL_IP || ctx->optname != IP_TOS) 15 return 1; 16 17 if (optval + 1 > optval_end) 18 return 0; /* EPERM, bounds check */ 19 20 if (optval[0] != 0x80) 21 return 0; /* EPERM, unexpected optval from the kernel */ 22 23 ctx->retval = 0; /* Reset system call return value to zero */ 24 25 optval[0] = 0x90; 26 ctx->optlen = 1; 27 28 return 1; 29 } 30 31 SEC("cgroup/getsockopt") 32 int _getsockopt_parent(struct bpf_sockopt *ctx) 33 { 34 __u8 *optval_end = ctx->optval_end; 35 __u8 *optval = ctx->optval; 36 37 if (ctx->level != SOL_IP || ctx->optname != IP_TOS) 38 return 1; 39 40 if (optval + 1 > optval_end) 41 return 0; /* EPERM, bounds check */ 42 43 if (optval[0] != 0x90) 44 return 0; /* EPERM, unexpected optval from the kernel */ 45 46 ctx->retval = 0; /* Reset system call return value to zero */ 47 48 optval[0] = 0xA0; 49 ctx->optlen = 1; 50 51 return 1; 52 } 53 54 SEC("cgroup/setsockopt") 55 int _setsockopt(struct bpf_sockopt *ctx) 56 { 57 __u8 *optval_end = ctx->optval_end; 58 __u8 *optval = ctx->optval; 59 60 if (ctx->level != SOL_IP || ctx->optname != IP_TOS) 61 return 1; 62 63 if (optval + 1 > optval_end) 64 return 0; /* EPERM, bounds check */ 65 66 optval[0] += 0x10; 67 ctx->optlen = 1; 68 69 return 1; 70 } 71