blob: 852de1e3daa91dc59805f6d7488525f4309bfce6 [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 Żenczykowskibca0c852021-01-19 01:22:17 -080045static inline __always_inline int do_forward(struct __sk_buff* skb, const bool is_ethernet,
46 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
53 // Must be meta-ethernet IPv6 frame
54 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
55
56 // Must have (ethernet and) ipv6 header
57 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
58
59 // Ethertype - if present - must be IPv6
60 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
61
62 // IP version must be 6
63 if (ip6->version != 6) return TC_ACT_OK;
64
65 // Cannot decrement during forward if already zero or would be zero,
66 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
67 if (ip6->hop_limit <= 1) return TC_ACT_OK;
68
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -080069 // If hardware offload is running and programming flows based on conntrack entries,
70 // try not to interfere with it.
71 if (ip6->nexthdr == IPPROTO_TCP) {
72 struct tcphdr* tcph = (void*)(ip6 + 1);
73
74 // Make sure we can get at the tcp header
75 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end) return TC_ACT_OK;
76
77 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
78 if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK;
79 }
80
Hungming Chen56c632c2020-09-10 15:42:58 +080081 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
82 __be32 src32 = ip6->saddr.s6_addr32[0];
83 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
84 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
85 return TC_ACT_OK;
86
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080087 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
88 __be32 dst32 = ip6->daddr.s6_addr32[0];
89 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
90 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
91 return TC_ACT_OK;
92
93 // In the upstream direction do not forward traffic within the same /64 subnet.
94 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
95 return TC_ACT_OK;
96
97 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +080098 .iif = skb->ifindex,
99 .neigh6 = ip6->daddr,
100 };
101
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800102 TetherUpstream6Key ku = {
103 .iif = skb->ifindex,
104 };
105
106 TetherDownstream6Value* vd = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) : NULL;
107 TetherUpstream6Value* vu = downstream ? NULL : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800108
109 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800110 if (downstream && !vd) return TC_ACT_OK;
111 if (!downstream && !vu) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +0800112
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800113 uint32_t stat_and_limit_k = downstream ? skb->ifindex : vu->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800114
115 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
116
117 // If we don't have anywhere to put stats, then abort...
118 if (!stat_v) return TC_ACT_OK;
119
120 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
121
122 // If we don't have a limit, then abort...
123 if (!limit_v) return TC_ACT_OK;
124
125 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800126 const int pmtu = downstream ? vd->pmtu : vu->pmtu;
Hungming Chen56c632c2020-09-10 15:42:58 +0800127 if (pmtu < IPV6_MIN_MTU) return TC_ACT_OK;
128
129 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
130 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
131 // undercount, which is still better then not accounting for this overhead at all.
132 // Note: this really shouldn't be device/path mtu at all, but rather should be
133 // derived from this particular connection's mss (ie. from gro segment size).
134 // This would require a much newer kernel with newer ebpf accessors.
135 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
136 uint64_t packets = 1;
137 uint64_t bytes = skb->len;
138 if (bytes > pmtu) {
139 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
140 const int mss = pmtu - tcp_overhead;
141 const uint64_t payload = bytes - tcp_overhead;
142 packets = (payload + mss - 1) / mss;
143 bytes = tcp_overhead * packets + payload;
144 }
145
146 // Are we past the limit? If so, then abort...
147 // Note: will not overflow since u64 is 936 years even at 5Gbps.
148 // Do not drop here. Offload is just that, whenever we fail to handle
149 // a packet we let the core stack deal with things.
150 // (The core stack needs to handle limits correctly anyway,
151 // since we don't offload all traffic in both directions)
152 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
153
154 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800155 // Try to inject an ethernet header, and simply return if we fail.
156 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
157 // because this is easier and the kernel will strip extraneous ethernet header.
158 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
159 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800160 return TC_ACT_OK;
161 }
162
163 // bpf_skb_change_head() invalidates all pointers - reload them
164 data = (void*)(long)skb->data;
165 data_end = (void*)(long)skb->data_end;
166 eth = data;
167 ip6 = (void*)(eth + 1);
168
169 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800170 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
171 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800172 return TC_ACT_SHOT;
173 }
174 };
175
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800176 // At this point we always have an ethernet header - which will get stripped by the
177 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
178 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
179
Hungming Chen56c632c2020-09-10 15:42:58 +0800180 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
181 // thus corrections for it need to be done in 16-byte chunks at even offsets.
182 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
183 uint8_t old_hl = ip6->hop_limit;
184 --ip6->hop_limit;
185 uint8_t new_hl = ip6->hop_limit;
186
187 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
188 // (-ENOTSUPP) if it isn't.
189 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
190
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800191 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
192 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800193
194 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800195 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
196 *eth = downstream ? vd->macHeader : vu->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800197
198 // Redirect to forwarded interface.
199 //
200 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
201 // The redirect actually happens after the ebpf program has already terminated,
202 // and can fail for example for mtu reasons at that point in time, but there's nothing
203 // we can do about it here.
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800204 return bpf_redirect(downstream ? vd->oif : vu->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800205}
206
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800207DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800208 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800209(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800210 return do_forward(skb, /* is_ethernet */ true, /* downstream */ true);
211}
212
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800213DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800214 sched_cls_tether_upstream6_ether)
215(struct __sk_buff* skb) {
216 return do_forward(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800217}
218
219// Note: section names must be unique to prevent programs from appending to each other,
220// so instead the bpf loader will strip everything past the final $ symbol when actually
221// pinning the program into the filesystem.
222//
223// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
224// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
225// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
226// (the first of those has already been upstreamed)
227//
228// 5.4 kernel support was only added to Android Common Kernel in R,
229// and thus a 5.4 kernel always supports this.
230//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800231// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800232DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800233 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800234(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800235 return do_forward(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800236}
237
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800238DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800239 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
240(struct __sk_buff* skb) {
241 return do_forward(skb, /* is_ethernet */ false, /* downstream */ false);
242}
243
244// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800245DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14",
246 AID_ROOT, AID_NETWORK_STACK,
247 sched_cls_tether_downstream6_rawip_4_14,
248 KVER(4, 14, 0), KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800249(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800250 return do_forward(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800251}
252
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800253DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14",
254 AID_ROOT, AID_NETWORK_STACK,
255 sched_cls_tether_upstream6_rawip_4_14,
256 KVER(4, 14, 0), KVER(5, 4, 0))
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800257(struct __sk_buff* skb) {
258 return do_forward(skb, /* is_ethernet */ false, /* downstream */ false);
259}
260
261// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800262// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
263// 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 -0800264DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800265 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800266(struct __sk_buff* skb) {
267 return TC_ACT_OK;
268}
269
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800270DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800271 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
272(struct __sk_buff* skb) {
273 return TC_ACT_OK;
274}
275
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800276// ----- IPv4 Support -----
277
278DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, TetherDownstream4Key, TetherDownstream4Value, 64,
279 AID_NETWORK_STACK)
280
281DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, TetherUpstream4Key, TetherUpstream4Value, 64,
282 AID_NETWORK_STACK)
283
284DEFINE_BPF_PROG("schedcls/tether_downstream4_ether", AID_ROOT, AID_NETWORK_STACK,
285 sched_cls_tether_downstream4_ether)
286(struct __sk_buff* skb) {
287 return TC_ACT_OK;
288}
289
290DEFINE_BPF_PROG("schedcls/tether_downstream4_rawip", AID_ROOT, AID_NETWORK_STACK,
291 sched_cls_tether_downstream4_rawip)
292(struct __sk_buff* skb) {
293 return TC_ACT_OK;
294}
295
296DEFINE_BPF_PROG("schedcls/tether_upstream4_ether", AID_ROOT, AID_NETWORK_STACK,
297 sched_cls_tether_upstream4_ether)
298(struct __sk_buff* skb) {
299 return TC_ACT_OK;
300}
301
302DEFINE_BPF_PROG("schedcls/tether_upstream4_rawip", AID_ROOT, AID_NETWORK_STACK,
303 sched_cls_tether_upstream4_rawip)
304(struct __sk_buff* skb) {
305 return TC_ACT_OK;
306}
307
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800308// ----- XDP Support -----
309
310#define DEFINE_XDP_PROG(str, func) \
311 DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx)
312
313DEFINE_XDP_PROG("xdp/tether_downstream_ether",
314 xdp_tether_downstream_ether) {
315 return XDP_PASS;
316}
317
318DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
319 xdp_tether_downstream_rawip) {
320 return XDP_PASS;
321}
322
323DEFINE_XDP_PROG("xdp/tether_upstream_ether",
324 xdp_tether_upstream_ether) {
325 return XDP_PASS;
326}
327
328DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
329 xdp_tether_upstream_rawip) {
330 return XDP_PASS;
331}
332
Hungming Chen56c632c2020-09-10 15:42:58 +0800333LICENSE("Apache 2.0");
334CRITICAL("netd");