2 * Copyright (c) 2017 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 #define KBUILD_MODNAME "foo"
9 #include <linux/ptrace.h>
10 #include <linux/version.h>
11 #include <uapi/linux/bpf.h>
12 #include <uapi/linux/in6.h>
13 #include <bpf/bpf_helpers.h>
14 #include "bpf_legacy.h"
15 #include <bpf/bpf_tracing.h>
17 #define MAX_NR_PORTS 65536
20 struct bpf_map_def_legacy
SEC("maps") port_a
= {
21 .type
= BPF_MAP_TYPE_ARRAY
,
22 .key_size
= sizeof(u32
),
23 .value_size
= sizeof(int),
24 .max_entries
= MAX_NR_PORTS
,
28 struct bpf_map_def_legacy
SEC("maps") port_h
= {
29 .type
= BPF_MAP_TYPE_HASH
,
30 .key_size
= sizeof(u32
),
31 .value_size
= sizeof(int),
36 struct bpf_map_def_legacy
SEC("maps") reg_result_h
= {
37 .type
= BPF_MAP_TYPE_HASH
,
38 .key_size
= sizeof(u32
),
39 .value_size
= sizeof(int),
44 struct bpf_map_def_legacy
SEC("maps") inline_result_h
= {
45 .type
= BPF_MAP_TYPE_HASH
,
46 .key_size
= sizeof(u32
),
47 .value_size
= sizeof(int),
51 /* map #4 */ /* Test case #0 */
52 struct bpf_map_def_legacy
SEC("maps") a_of_port_a
= {
53 .type
= BPF_MAP_TYPE_ARRAY_OF_MAPS
,
54 .key_size
= sizeof(u32
),
55 .inner_map_idx
= 0, /* map_fd[0] is port_a */
56 .max_entries
= MAX_NR_PORTS
,
59 /* map #5 */ /* Test case #1 */
60 struct bpf_map_def_legacy
SEC("maps") h_of_port_a
= {
61 .type
= BPF_MAP_TYPE_HASH_OF_MAPS
,
62 .key_size
= sizeof(u32
),
63 .inner_map_idx
= 0, /* map_fd[0] is port_a */
67 /* map #6 */ /* Test case #2 */
68 struct bpf_map_def_legacy
SEC("maps") h_of_port_h
= {
69 .type
= BPF_MAP_TYPE_HASH_OF_MAPS
,
70 .key_size
= sizeof(u32
),
71 .inner_map_idx
= 1, /* map_fd[1] is port_h */
75 static __always_inline
int do_reg_lookup(void *inner_map
, u32 port
)
79 result
= bpf_map_lookup_elem(inner_map
, &port
);
80 return result
? *result
: -ENOENT
;
83 static __always_inline
int do_inline_array_lookup(void *inner_map
, u32 port
)
87 if (inner_map
!= &port_a
)
90 result
= bpf_map_lookup_elem(&port_a
, &port
);
91 return result
? *result
: -ENOENT
;
94 static __always_inline
int do_inline_hash_lookup(void *inner_map
, u32 port
)
98 if (inner_map
!= &port_h
)
101 result
= bpf_map_lookup_elem(&port_h
, &port
);
102 return result
? *result
: -ENOENT
;
105 SEC("kprobe/sys_connect")
106 int trace_sys_connect(struct pt_regs
*ctx
)
108 struct sockaddr_in6
*in6
;
109 u16 test_case
, port
, dst6
[8];
110 int addrlen
, ret
, inline_ret
, ret_key
= 0;
112 void *outer_map
, *inner_map
;
113 bool inline_hash
= false;
115 in6
= (struct sockaddr_in6
*)PT_REGS_PARM2(ctx
);
116 addrlen
= (int)PT_REGS_PARM3(ctx
);
118 if (addrlen
!= sizeof(*in6
))
121 ret
= bpf_probe_read_user(dst6
, sizeof(dst6
), &in6
->sin6_addr
);
127 if (dst6
[0] != 0xdead || dst6
[1] != 0xbeef)
132 ret
= bpf_probe_read_user(&port
, sizeof(port
), &in6
->sin6_port
);
141 if (test_case
== 0) {
142 outer_map
= &a_of_port_a
;
143 } else if (test_case
== 1) {
144 outer_map
= &h_of_port_a
;
145 } else if (test_case
== 2) {
146 outer_map
= &h_of_port_h
;
153 inner_map
= bpf_map_lookup_elem(outer_map
, &port_key
);
160 ret
= do_reg_lookup(inner_map
, port_key
);
162 if (test_case
== 0 || test_case
== 1)
163 inline_ret
= do_inline_array_lookup(inner_map
, port_key
);
165 inline_ret
= do_inline_hash_lookup(inner_map
, port_key
);
168 bpf_map_update_elem(®_result_h
, &ret_key
, &ret
, BPF_ANY
);
169 bpf_map_update_elem(&inline_result_h
, &ret_key
, &inline_ret
, BPF_ANY
);
174 char _license
[] SEC("license") = "GPL";
175 u32 _version
SEC("version") = LINUX_VERSION_CODE
;