~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/sockopt_inherit.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/bpf.h>
  3 #include <bpf/bpf_helpers.h>
  4 
  5 char _license[] SEC("license") = "GPL";
  6 
  7 #define SOL_CUSTOM                      0xdeadbeef
  8 #define CUSTOM_INHERIT1                 0
  9 #define CUSTOM_INHERIT2                 1
 10 #define CUSTOM_LISTENER                 2
 11 
 12 __s32 page_size = 0;
 13 
 14 struct sockopt_inherit {
 15         __u8 val;
 16 };
 17 
 18 struct {
 19         __uint(type, BPF_MAP_TYPE_SK_STORAGE);
 20         __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
 21         __type(key, int);
 22         __type(value, struct sockopt_inherit);
 23 } cloned1_map SEC(".maps");
 24 
 25 struct {
 26         __uint(type, BPF_MAP_TYPE_SK_STORAGE);
 27         __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
 28         __type(key, int);
 29         __type(value, struct sockopt_inherit);
 30 } cloned2_map SEC(".maps");
 31 
 32 struct {
 33         __uint(type, BPF_MAP_TYPE_SK_STORAGE);
 34         __uint(map_flags, BPF_F_NO_PREALLOC);
 35         __type(key, int);
 36         __type(value, struct sockopt_inherit);
 37 } listener_only_map SEC(".maps");
 38 
 39 static __inline struct sockopt_inherit *get_storage(struct bpf_sockopt *ctx)
 40 {
 41         if (ctx->optname == CUSTOM_INHERIT1)
 42                 return bpf_sk_storage_get(&cloned1_map, ctx->sk, 0,
 43                                           BPF_SK_STORAGE_GET_F_CREATE);
 44         else if (ctx->optname == CUSTOM_INHERIT2)
 45                 return bpf_sk_storage_get(&cloned2_map, ctx->sk, 0,
 46                                           BPF_SK_STORAGE_GET_F_CREATE);
 47         else
 48                 return bpf_sk_storage_get(&listener_only_map, ctx->sk, 0,
 49                                           BPF_SK_STORAGE_GET_F_CREATE);
 50 }
 51 
 52 SEC("cgroup/getsockopt")
 53 int _getsockopt(struct bpf_sockopt *ctx)
 54 {
 55         __u8 *optval_end = ctx->optval_end;
 56         struct sockopt_inherit *storage;
 57         __u8 *optval = ctx->optval;
 58 
 59         if (ctx->level != SOL_CUSTOM)
 60                 goto out; /* only interested in SOL_CUSTOM */
 61 
 62         if (optval + 1 > optval_end)
 63                 return 0; /* EPERM, bounds check */
 64 
 65         storage = get_storage(ctx);
 66         if (!storage)
 67                 return 0; /* EPERM, couldn't get sk storage */
 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 out:
 77         /* optval larger than PAGE_SIZE use kernel's buffer. */
 78         if (ctx->optlen > page_size)
 79                 ctx->optlen = 0;
 80         return 1;
 81 }
 82 
 83 SEC("cgroup/setsockopt")
 84 int _setsockopt(struct bpf_sockopt *ctx)
 85 {
 86         __u8 *optval_end = ctx->optval_end;
 87         struct sockopt_inherit *storage;
 88         __u8 *optval = ctx->optval;
 89 
 90         if (ctx->level != SOL_CUSTOM)
 91                 goto out; /* only interested in SOL_CUSTOM */
 92 
 93         if (optval + 1 > optval_end)
 94                 return 0; /* EPERM, bounds check */
 95 
 96         storage = get_storage(ctx);
 97         if (!storage)
 98                 return 0; /* EPERM, couldn't get sk storage */
 99 
100         storage->val = optval[0];
101         ctx->optlen = -1;
102 
103         return 1;
104 
105 out:
106         /* optval larger than PAGE_SIZE use kernel's buffer. */
107         if (ctx->optlen > page_size)
108                 ctx->optlen = 0;
109         return 1;
110 }
111 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php