Debian Codegraph @

src/bpf/socket-bind.bpf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* The SPDX header above is actually correct in claiming this was
4 * LGPL-2.1-or-later, because it is. Since the kernel doesn't consider that
5 * compatible with GPL we will claim this to be GPL however, which should be
6 * fine given that LGPL-2.1-or-later downgrades to GPL if needed.
7 */
8
9 #include "socket-bind-api.bpf.h"
10 /* <linux/types.h> must precede <bpf/bpf_helpers.h> due to
11 * <bpf/bpf_helpers.h> does not depend from type header by design.
12 */
13 #include <linux/bpf.h>
14 #include <linux/types.h>
15 #include <netinet/in.h>
16 #include <stdbool.h>
17 #include <bpf/bpf_endian.h>
18 #include <bpf/bpf_helpers.h>
19
20 /*
21 * max_entries is set from user space with bpf_map__set_max_entries helper.
22 */
23 struct socket_bind_map_t {
24 __uint(type, BPF_MAP_TYPE_ARRAY);
25 __type(key, __u32);
26 __type(value, struct socket_bind_rule);
27 };
28
29 enum socket_bind_action {
30 SOCKET_BIND_DENY = 0,
31 SOCKET_BIND_ALLOW = 1,
32 };
33
34 struct socket_bind_map_t sd_bind_allow SEC(".maps");
35 struct socket_bind_map_t sd_bind_deny SEC(".maps");
36
37 static __always_inline bool match_af(
38 __u8 address_family, const struct socket_bind_rule *r) {
39 return r->address_family == AF_UNSPEC || address_family == r->address_family;
40 }
41
42 static __always_inline bool match_protocol(
43 __u32 protocol, const struct socket_bind_rule *r) {
44 return r->protocol == 0 || r->protocol == protocol;
45 }
46
47 static __always_inline bool match_user_port(
48 __u16 port, const struct socket_bind_rule *r) {
49 return r->nr_ports == 0 ||
50 (port >= r->port_min && port < r->port_min + (__u32) r->nr_ports);
51 }
52
53 static __always_inline bool match(
54 __u8 address_family,
55 __u32 protocol,
56 __u16 port,
57 const struct socket_bind_rule *r) {
58 if (r->address_family == SOCKET_BIND_RULE_AF_MATCH_NOTHING)
59 return false;
60
61 return match_af(address_family, r) &&
62 match_protocol(protocol, r) &&
63 match_user_port(port, r);
64 }
65
66 static __always_inline bool match_rules(
67 struct bpf_sock_addr *ctx,
68 struct socket_bind_map_t *rules) {
69 volatile __u32 user_port = ctx->user_port;
70 __u16 port = (__u16)bpf_ntohs(user_port);
71
72 for (__u32 i = 0; i < SOCKET_BIND_MAX_RULES; ++i) {
73 const __u32 key = i;
74 const struct socket_bind_rule *rule = bpf_map_lookup_elem(rules, &key);
75
76 /* Lookup returns NULL if iterator is advanced past the last
77 * element put in the map. */
78 if (!rule)
79 break;
80
81 if (match(ctx->user_family, ctx->protocol, port, rule))
82 return true;
83 }
84
85 return false;
86 }
87
88 static __always_inline int bind_socket(struct bpf_sock_addr *ctx) {
89 if (match_rules(ctx, &sd_bind_allow))
90 return SOCKET_BIND_ALLOW;
91
92 if (match_rules(ctx, &sd_bind_deny))
93 return SOCKET_BIND_DENY;
94
95 return SOCKET_BIND_ALLOW;
96 }
97
98 SEC("cgroup/bind4")
99 int sd_bind4(struct bpf_sock_addr *ctx) {
100 if (ctx->user_family != AF_INET || ctx->family != AF_INET)
101 return SOCKET_BIND_ALLOW;
102
103 return bind_socket(ctx);
104 }
105
106 SEC("cgroup/bind6")
107 int sd_bind6(struct bpf_sock_addr *ctx) {
108 if (ctx->user_family != AF_INET6 || ctx->family != AF_INET6)
109 return SOCKET_BIND_ALLOW;
110
111 return bind_socket(ctx);
112 }
113
114 char _license[] SEC("license") = "GPL";