Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <netinet/in.h>
0003 #include <linux/bpf.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 char _license[] SEC("license") = "GPL";
0007 
0008 SEC("cgroup/getsockopt")
0009 int _getsockopt_child(struct bpf_sockopt *ctx)
0010 {
0011     __u8 *optval_end = ctx->optval_end;
0012     __u8 *optval = ctx->optval;
0013 
0014     if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
0015         return 1;
0016 
0017     if (optval + 1 > optval_end)
0018         return 0; /* EPERM, bounds check */
0019 
0020     if (optval[0] != 0x80)
0021         return 0; /* EPERM, unexpected optval from the kernel */
0022 
0023     ctx->retval = 0; /* Reset system call return value to zero */
0024 
0025     optval[0] = 0x90;
0026     ctx->optlen = 1;
0027 
0028     return 1;
0029 }
0030 
0031 SEC("cgroup/getsockopt")
0032 int _getsockopt_parent(struct bpf_sockopt *ctx)
0033 {
0034     __u8 *optval_end = ctx->optval_end;
0035     __u8 *optval = ctx->optval;
0036 
0037     if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
0038         return 1;
0039 
0040     if (optval + 1 > optval_end)
0041         return 0; /* EPERM, bounds check */
0042 
0043     if (optval[0] != 0x90)
0044         return 0; /* EPERM, unexpected optval from the kernel */
0045 
0046     ctx->retval = 0; /* Reset system call return value to zero */
0047 
0048     optval[0] = 0xA0;
0049     ctx->optlen = 1;
0050 
0051     return 1;
0052 }
0053 
0054 SEC("cgroup/setsockopt")
0055 int _setsockopt(struct bpf_sockopt *ctx)
0056 {
0057     __u8 *optval_end = ctx->optval_end;
0058     __u8 *optval = ctx->optval;
0059 
0060     if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
0061         return 1;
0062 
0063     if (optval + 1 > optval_end)
0064         return 0; /* EPERM, bounds check */
0065 
0066     optval[0] += 0x10;
0067     ctx->optlen = 1;
0068 
0069     return 1;
0070 }