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

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

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ 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 
  3 #include "vmlinux.h"
  4 
  5 #include <bpf/bpf_helpers.h>
  6 
  7 #define AF_INET6 10
  8 
  9 struct {
 10         __uint(type, BPF_MAP_TYPE_SK_STORAGE);
 11         __uint(map_flags, BPF_F_NO_PREALLOC);
 12         __type(key, int);
 13         __type(value, int);
 14 } sockops_netns_cookies SEC(".maps");
 15 
 16 struct {
 17         __uint(type, BPF_MAP_TYPE_SK_STORAGE);
 18         __uint(map_flags, BPF_F_NO_PREALLOC);
 19         __type(key, int);
 20         __type(value, int);
 21 } sk_msg_netns_cookies SEC(".maps");
 22 
 23 struct {
 24         __uint(type, BPF_MAP_TYPE_SOCKMAP);
 25         __uint(max_entries, 2);
 26         __type(key, __u32);
 27         __type(value, __u64);
 28 } sock_map SEC(".maps");
 29 
 30 SEC("sockops")
 31 int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
 32 {
 33         struct bpf_sock *sk = ctx->sk;
 34         int *cookie;
 35         __u32 key = 0;
 36 
 37         if (ctx->family != AF_INET6)
 38                 return 1;
 39 
 40         if (!sk)
 41                 return 1;
 42 
 43         switch (ctx->op) {
 44         case BPF_SOCK_OPS_TCP_CONNECT_CB:
 45                 cookie = bpf_sk_storage_get(&sockops_netns_cookies, sk, 0,
 46                                             BPF_SK_STORAGE_GET_F_CREATE);
 47                 if (!cookie)
 48                         return 1;
 49 
 50                 *cookie = bpf_get_netns_cookie(ctx);
 51                 break;
 52         case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
 53                 bpf_sock_map_update(ctx, &sock_map, &key, BPF_NOEXIST);
 54                 break;
 55         default:
 56                 break;
 57         }
 58 
 59         return 1;
 60 }
 61 
 62 SEC("sk_msg")
 63 int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
 64 {
 65         struct bpf_sock *sk = msg->sk;
 66         int *cookie;
 67 
 68         if (msg->family != AF_INET6)
 69                 return 1;
 70 
 71         if (!sk)
 72                 return 1;
 73 
 74         cookie = bpf_sk_storage_get(&sk_msg_netns_cookies, sk, 0,
 75                                     BPF_SK_STORAGE_GET_F_CREATE);
 76         if (!cookie)
 77                 return 1;
 78 
 79         *cookie = bpf_get_netns_cookie(msg);
 80 
 81         return 1;
 82 }
 83 
 84 char _license[] SEC("license") = "GPL";
 85 

~ [ 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