blob: e514ef17bbb6774f4e0a0e49dc2187b2a66a3849 [file] [log] [blame]
Hungming Chen56c632c2020-09-10 15:42:58 +08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <linux/if.h>
18#include <linux/ip.h>
19#include <linux/ipv6.h>
20#include <linux/pkt_cls.h>
21#include <linux/tcp.h>
22
23#include "bpf_helpers.h"
24#include "bpf_net_helpers.h"
25#include "netdbpf/bpf_shared.h"
26
Hungming Chen56c632c2020-09-10 15:42:58 +080027// Tethering stats, indexed by upstream interface.
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080028DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080029
30// Tethering data limit, indexed by upstream interface.
31// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080032DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK)
33
34// ----- IPv6 Support -----
35
36DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, TetherDownstream6Value, 64,
37 AID_NETWORK_STACK)
38
39DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value,
40 64, AID_NETWORK_STACK)
41
42DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, TetherUpstream6Value, 64,
43 AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080044
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -080045static inline __always_inline int do_forward6(struct __sk_buff* skb, const bool is_ethernet,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080046 const bool downstream) {
47 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
Hungming Chen56c632c2020-09-10 15:42:58 +080048 void* data = (void*)(long)skb->data;
49 const void* data_end = (void*)(long)skb->data_end;
50 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
51 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
52
Maciej Żenczykowski18552e82021-01-24 19:59:05 -080053 // Require ethernet dst mac address to be our unicast address.
54 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
55
Hungming Chen56c632c2020-09-10 15:42:58 +080056 // Must be meta-ethernet IPv6 frame
57 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
58
59 // Must have (ethernet and) ipv6 header
60 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
61
62 // Ethertype - if present - must be IPv6
63 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
64
65 // IP version must be 6
66 if (ip6->version != 6) return TC_ACT_OK;
67
68 // Cannot decrement during forward if already zero or would be zero,
69 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
70 if (ip6->hop_limit <= 1) return TC_ACT_OK;
71
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -080072 // If hardware offload is running and programming flows based on conntrack entries,
73 // try not to interfere with it.
74 if (ip6->nexthdr == IPPROTO_TCP) {
75 struct tcphdr* tcph = (void*)(ip6 + 1);
76
77 // Make sure we can get at the tcp header
78 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end) return TC_ACT_OK;
79
80 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
81 if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK;
82 }
83
Hungming Chen56c632c2020-09-10 15:42:58 +080084 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
85 __be32 src32 = ip6->saddr.s6_addr32[0];
86 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
87 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
88 return TC_ACT_OK;
89
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080090 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
91 __be32 dst32 = ip6->daddr.s6_addr32[0];
92 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
93 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
94 return TC_ACT_OK;
95
96 // In the upstream direction do not forward traffic within the same /64 subnet.
97 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
98 return TC_ACT_OK;
99
100 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +0800101 .iif = skb->ifindex,
102 .neigh6 = ip6->daddr,
103 };
104
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800105 TetherUpstream6Key ku = {
106 .iif = skb->ifindex,
107 };
108
109 TetherDownstream6Value* vd = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) : NULL;
110 TetherUpstream6Value* vu = downstream ? NULL : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800111
112 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800113 if (downstream && !vd) return TC_ACT_OK;
114 if (!downstream && !vu) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +0800115
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800116 uint32_t stat_and_limit_k = downstream ? skb->ifindex : vu->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800117
118 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
119
120 // If we don't have anywhere to put stats, then abort...
121 if (!stat_v) return TC_ACT_OK;
122
123 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
124
125 // If we don't have a limit, then abort...
126 if (!limit_v) return TC_ACT_OK;
127
128 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800129 const int pmtu = downstream ? vd->pmtu : vu->pmtu;
Hungming Chen56c632c2020-09-10 15:42:58 +0800130 if (pmtu < IPV6_MIN_MTU) return TC_ACT_OK;
131
132 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
133 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
134 // undercount, which is still better then not accounting for this overhead at all.
135 // Note: this really shouldn't be device/path mtu at all, but rather should be
136 // derived from this particular connection's mss (ie. from gro segment size).
137 // This would require a much newer kernel with newer ebpf accessors.
138 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
139 uint64_t packets = 1;
140 uint64_t bytes = skb->len;
141 if (bytes > pmtu) {
142 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
143 const int mss = pmtu - tcp_overhead;
144 const uint64_t payload = bytes - tcp_overhead;
145 packets = (payload + mss - 1) / mss;
146 bytes = tcp_overhead * packets + payload;
147 }
148
149 // Are we past the limit? If so, then abort...
150 // Note: will not overflow since u64 is 936 years even at 5Gbps.
151 // Do not drop here. Offload is just that, whenever we fail to handle
152 // a packet we let the core stack deal with things.
153 // (The core stack needs to handle limits correctly anyway,
154 // since we don't offload all traffic in both directions)
155 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
156
157 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800158 // Try to inject an ethernet header, and simply return if we fail.
159 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
160 // because this is easier and the kernel will strip extraneous ethernet header.
161 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
162 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800163 return TC_ACT_OK;
164 }
165
166 // bpf_skb_change_head() invalidates all pointers - reload them
167 data = (void*)(long)skb->data;
168 data_end = (void*)(long)skb->data_end;
169 eth = data;
170 ip6 = (void*)(eth + 1);
171
172 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800173 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
174 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800175 return TC_ACT_SHOT;
176 }
177 };
178
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800179 // At this point we always have an ethernet header - which will get stripped by the
180 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
181 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
182
Hungming Chen56c632c2020-09-10 15:42:58 +0800183 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
184 // thus corrections for it need to be done in 16-byte chunks at even offsets.
185 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
186 uint8_t old_hl = ip6->hop_limit;
187 --ip6->hop_limit;
188 uint8_t new_hl = ip6->hop_limit;
189
190 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
191 // (-ENOTSUPP) if it isn't.
192 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
193
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800194 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
195 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800196
197 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800198 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
199 *eth = downstream ? vd->macHeader : vu->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800200
201 // Redirect to forwarded interface.
202 //
203 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
204 // The redirect actually happens after the ebpf program has already terminated,
205 // and can fail for example for mtu reasons at that point in time, but there's nothing
206 // we can do about it here.
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800207 return bpf_redirect(downstream ? vd->oif : vu->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800208}
209
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800210DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800211 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800212(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800213 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800214}
215
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800216DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800217 sched_cls_tether_upstream6_ether)
218(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800219 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800220}
221
222// Note: section names must be unique to prevent programs from appending to each other,
223// so instead the bpf loader will strip everything past the final $ symbol when actually
224// pinning the program into the filesystem.
225//
226// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
227// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
228// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
229// (the first of those has already been upstreamed)
230//
231// 5.4 kernel support was only added to Android Common Kernel in R,
232// and thus a 5.4 kernel always supports this.
233//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800234// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800235DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800236 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800237(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800238 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800239}
240
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800241DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800242 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
243(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800244 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800245}
246
247// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800248DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14",
249 AID_ROOT, AID_NETWORK_STACK,
250 sched_cls_tether_downstream6_rawip_4_14,
251 KVER(4, 14, 0), KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800252(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800253 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800254}
255
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800256DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14",
257 AID_ROOT, AID_NETWORK_STACK,
258 sched_cls_tether_upstream6_rawip_4_14,
259 KVER(4, 14, 0), KVER(5, 4, 0))
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800260(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800261 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800262}
263
264// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800265// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
266// it at the same location this one would be pinned at and will thus skip loading this stub)
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800267DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800268 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800269(struct __sk_buff* skb) {
270 return TC_ACT_OK;
271}
272
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800273DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800274 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
275(struct __sk_buff* skb) {
276 return TC_ACT_OK;
277}
278
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800279// ----- IPv4 Support -----
280
281DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, TetherDownstream4Key, TetherDownstream4Value, 64,
282 AID_NETWORK_STACK)
283
284DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, TetherUpstream4Key, TetherUpstream4Value, 64,
285 AID_NETWORK_STACK)
286
287DEFINE_BPF_PROG("schedcls/tether_downstream4_ether", AID_ROOT, AID_NETWORK_STACK,
288 sched_cls_tether_downstream4_ether)
289(struct __sk_buff* skb) {
290 return TC_ACT_OK;
291}
292
293DEFINE_BPF_PROG("schedcls/tether_downstream4_rawip", AID_ROOT, AID_NETWORK_STACK,
294 sched_cls_tether_downstream4_rawip)
295(struct __sk_buff* skb) {
296 return TC_ACT_OK;
297}
298
299DEFINE_BPF_PROG("schedcls/tether_upstream4_ether", AID_ROOT, AID_NETWORK_STACK,
300 sched_cls_tether_upstream4_ether)
301(struct __sk_buff* skb) {
302 return TC_ACT_OK;
303}
304
305DEFINE_BPF_PROG("schedcls/tether_upstream4_rawip", AID_ROOT, AID_NETWORK_STACK,
306 sched_cls_tether_upstream4_rawip)
307(struct __sk_buff* skb) {
308 return TC_ACT_OK;
309}
310
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800311// ----- XDP Support -----
312
313#define DEFINE_XDP_PROG(str, func) \
314 DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx)
315
316DEFINE_XDP_PROG("xdp/tether_downstream_ether",
317 xdp_tether_downstream_ether) {
318 return XDP_PASS;
319}
320
321DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
322 xdp_tether_downstream_rawip) {
323 return XDP_PASS;
324}
325
326DEFINE_XDP_PROG("xdp/tether_upstream_ether",
327 xdp_tether_upstream_ether) {
328 return XDP_PASS;
329}
330
331DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
332 xdp_tether_upstream_rawip) {
333 return XDP_PASS;
334}
335
Hungming Chen56c632c2020-09-10 15:42:58 +0800336LICENSE("Apache 2.0");
337CRITICAL("netd");