blob: bdbbc5e8d806ec07649e7ec97584fb31c1f6db8a [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
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -080027DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, TetherDownstream6Value, 64,
Hungming Chen56c632c2020-09-10 15:42:58 +080028 AID_NETWORK_STACK)
29
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080030DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, TetherUpstream6Value, 64,
31 AID_NETWORK_STACK)
32
Hungming Chen56c632c2020-09-10 15:42:58 +080033// Tethering stats, indexed by upstream interface.
34DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, uint32_t, TetherStatsValue, 16, AID_NETWORK_STACK)
35
36// Tethering data limit, indexed by upstream interface.
37// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
38DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, uint32_t, uint64_t, 16, AID_NETWORK_STACK)
39
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080040static inline __always_inline int do_forward(struct __sk_buff* skb, const bool is_ethernet,
41 const bool downstream) {
42 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
Hungming Chen56c632c2020-09-10 15:42:58 +080043 void* data = (void*)(long)skb->data;
44 const void* data_end = (void*)(long)skb->data_end;
45 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
46 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
47
48 // Must be meta-ethernet IPv6 frame
49 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
50
51 // Must have (ethernet and) ipv6 header
52 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
53
54 // Ethertype - if present - must be IPv6
55 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
56
57 // IP version must be 6
58 if (ip6->version != 6) return TC_ACT_OK;
59
60 // Cannot decrement during forward if already zero or would be zero,
61 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
62 if (ip6->hop_limit <= 1) return TC_ACT_OK;
63
64 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
65 __be32 src32 = ip6->saddr.s6_addr32[0];
66 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
67 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
68 return TC_ACT_OK;
69
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080070 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
71 __be32 dst32 = ip6->daddr.s6_addr32[0];
72 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
73 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
74 return TC_ACT_OK;
75
76 // In the upstream direction do not forward traffic within the same /64 subnet.
77 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
78 return TC_ACT_OK;
79
80 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +080081 .iif = skb->ifindex,
82 .neigh6 = ip6->daddr,
83 };
84
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080085 TetherUpstream6Key ku = {
86 .iif = skb->ifindex,
87 };
88
89 TetherDownstream6Value* vd = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) : NULL;
90 TetherUpstream6Value* vu = downstream ? NULL : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +080091
92 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080093 if (downstream && !vd) return TC_ACT_OK;
94 if (!downstream && !vu) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +080095
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080096 uint32_t stat_and_limit_k = downstream ? skb->ifindex : vu->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +080097
98 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
99
100 // If we don't have anywhere to put stats, then abort...
101 if (!stat_v) return TC_ACT_OK;
102
103 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
104
105 // If we don't have a limit, then abort...
106 if (!limit_v) return TC_ACT_OK;
107
108 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800109 const int pmtu = downstream ? vd->pmtu : vu->pmtu;
Hungming Chen56c632c2020-09-10 15:42:58 +0800110 if (pmtu < IPV6_MIN_MTU) return TC_ACT_OK;
111
112 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
113 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
114 // undercount, which is still better then not accounting for this overhead at all.
115 // Note: this really shouldn't be device/path mtu at all, but rather should be
116 // derived from this particular connection's mss (ie. from gro segment size).
117 // This would require a much newer kernel with newer ebpf accessors.
118 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
119 uint64_t packets = 1;
120 uint64_t bytes = skb->len;
121 if (bytes > pmtu) {
122 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
123 const int mss = pmtu - tcp_overhead;
124 const uint64_t payload = bytes - tcp_overhead;
125 packets = (payload + mss - 1) / mss;
126 bytes = tcp_overhead * packets + payload;
127 }
128
129 // Are we past the limit? If so, then abort...
130 // Note: will not overflow since u64 is 936 years even at 5Gbps.
131 // Do not drop here. Offload is just that, whenever we fail to handle
132 // a packet we let the core stack deal with things.
133 // (The core stack needs to handle limits correctly anyway,
134 // since we don't offload all traffic in both directions)
135 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
136
137 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800138 // Try to inject an ethernet header, and simply return if we fail.
139 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
140 // because this is easier and the kernel will strip extraneous ethernet header.
141 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
142 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800143 return TC_ACT_OK;
144 }
145
146 // bpf_skb_change_head() invalidates all pointers - reload them
147 data = (void*)(long)skb->data;
148 data_end = (void*)(long)skb->data_end;
149 eth = data;
150 ip6 = (void*)(eth + 1);
151
152 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800153 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
154 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800155 return TC_ACT_SHOT;
156 }
157 };
158
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800159 // At this point we always have an ethernet header - which will get stripped by the
160 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
161 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
162
Hungming Chen56c632c2020-09-10 15:42:58 +0800163 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
164 // thus corrections for it need to be done in 16-byte chunks at even offsets.
165 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
166 uint8_t old_hl = ip6->hop_limit;
167 --ip6->hop_limit;
168 uint8_t new_hl = ip6->hop_limit;
169
170 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
171 // (-ENOTSUPP) if it isn't.
172 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
173
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800174 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
175 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800176
177 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800178 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
179 *eth = downstream ? vd->macHeader : vu->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800180
181 // Redirect to forwarded interface.
182 //
183 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
184 // The redirect actually happens after the ebpf program has already terminated,
185 // and can fail for example for mtu reasons at that point in time, but there's nothing
186 // we can do about it here.
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800187 return bpf_redirect(downstream ? vd->oif : vu->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800188}
189
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800190DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_ROOT,
191 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800192(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800193 return do_forward(skb, /* is_ethernet */ true, /* downstream */ true);
194}
195
196DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_ROOT,
197 sched_cls_tether_upstream6_ether)
198(struct __sk_buff* skb) {
199 return do_forward(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800200}
201
202// Note: section names must be unique to prevent programs from appending to each other,
203// so instead the bpf loader will strip everything past the final $ symbol when actually
204// pinning the program into the filesystem.
205//
206// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
207// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
208// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
209// (the first of those has already been upstreamed)
210//
211// 5.4 kernel support was only added to Android Common Kernel in R,
212// and thus a 5.4 kernel always supports this.
213//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800214// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800215DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_ROOT,
216 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800217(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800218 return do_forward(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800219}
220
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800221DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_ROOT,
222 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
223(struct __sk_buff* skb) {
224 return do_forward(skb, /* is_ethernet */ false, /* downstream */ false);
225}
226
227// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800228DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14", AID_ROOT, AID_ROOT,
229 sched_cls_tether_downstream6_rawip_4_14, KVER(4, 14, 0),
Hungming Chen56c632c2020-09-10 15:42:58 +0800230 KVER(5, 4, 0))
231(struct __sk_buff* skb) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800232 return do_forward(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800233}
234
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800235DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14", AID_ROOT, AID_ROOT,
236 sched_cls_tether_upstream6_rawip_4_14, KVER(4, 14, 0),
237 KVER(5, 4, 0))
238(struct __sk_buff* skb) {
239 return do_forward(skb, /* is_ethernet */ false, /* downstream */ false);
240}
241
242// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800243// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
244// it at the same location this one would be pinned at and will thus skip loading this stub)
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800245DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_ROOT,
246 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800247(struct __sk_buff* skb) {
248 return TC_ACT_OK;
249}
250
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800251DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_ROOT,
252 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
253(struct __sk_buff* skb) {
254 return TC_ACT_OK;
255}
256
Hungming Chen56c632c2020-09-10 15:42:58 +0800257LICENSE("Apache 2.0");
258CRITICAL("netd");