Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 1 | /* |
| 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> |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 18 | #include <linux/ip.h> |
| 19 | #include <linux/ipv6.h> |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 20 | #include <linux/pkt_cls.h> |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 21 | #include <linux/tcp.h> |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 22 | |
| 23 | #include "bpf_helpers.h" |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 24 | #include "bpf_net_helpers.h" |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 25 | #include "netdbpf/bpf_shared.h" |
| 26 | |
Maciej Żenczykowski | 565b6fb | 2020-01-27 01:58:40 -0800 | [diff] [blame] | 27 | DEFINE_BPF_MAP_GRW(tether_ingress_map, HASH, TetherIngressKey, TetherIngressValue, 64, |
| 28 | AID_NETWORK_STACK) |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 29 | |
Maciej Żenczykowski | 0510b01 | 2020-02-12 05:03:05 -0800 | [diff] [blame] | 30 | // Tethering stats, indexed by upstream interface. |
Maciej Żenczykowski | b79d184 | 2020-03-20 07:11:43 +0000 | [diff] [blame] | 31 | DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, uint32_t, TetherStatsValue, IFACE_STATS_MAP_SIZE, |
| 32 | AID_NETWORK_STACK) |
Maciej Żenczykowski | 0510b01 | 2020-02-12 05:03:05 -0800 | [diff] [blame] | 33 | |
Maciej Żenczykowski | 8887cce | 2020-05-04 18:14:59 -0700 | [diff] [blame] | 34 | // Tethering data limit, indexed by upstream interface. |
| 35 | // (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif]) |
| 36 | DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, uint32_t, uint64_t, IFACE_STATS_MAP_SIZE, |
| 37 | AID_NETWORK_STACK) |
| 38 | |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 39 | static inline __always_inline int do_forward(struct __sk_buff* skb, bool is_ethernet) { |
| 40 | int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 41 | void* data = (void*)(long)skb->data; |
| 42 | const void* data_end = (void*)(long)skb->data_end; |
| 43 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 44 | struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data; |
| 45 | |
| 46 | // Must be meta-ethernet IPv6 frame |
| 47 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK; |
| 48 | |
| 49 | // Must have (ethernet and) ipv6 header |
| 50 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK; |
| 51 | |
| 52 | // Ethertype - if present - must be IPv6 |
| 53 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK; |
| 54 | |
| 55 | // IP version must be 6 |
| 56 | if (ip6->version != 6) return TC_ACT_OK; |
| 57 | |
| 58 | // Cannot decrement during forward if already zero or would be zero, |
| 59 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
| 60 | if (ip6->hop_limit <= 1) return TC_ACT_OK; |
| 61 | |
| 62 | TetherIngressKey k = { |
| 63 | .iif = skb->ifindex, |
| 64 | .neigh6 = ip6->daddr, |
| 65 | }; |
| 66 | |
| 67 | TetherIngressValue* v = bpf_tether_ingress_map_lookup_elem(&k); |
| 68 | |
| 69 | // If we don't find any offload information then simply let the core stack handle it... |
| 70 | if (!v) return TC_ACT_OK; |
| 71 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 72 | uint32_t stat_and_limit_k = skb->ifindex; |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 73 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 74 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 75 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 76 | // If we don't have anywhere to put stats, then abort... |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 77 | if (!stat_v) return TC_ACT_OK; |
| 78 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 79 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 80 | |
| 81 | // If we don't have a limit, then abort... |
| 82 | if (!limit_v) return TC_ACT_OK; |
| 83 | |
Maciej Żenczykowski | 1ec2453 | 2020-05-23 22:07:00 +0000 | [diff] [blame^] | 84 | // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort... |
| 85 | const int pmtu = v->pmtu; |
| 86 | if (pmtu < IPV6_MIN_MTU) return TC_ACT_OK; |
| 87 | |
| 88 | // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default |
| 89 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 90 | // undercount, which is still better then not accounting for this overhead at all. |
| 91 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 92 | // derived from this particular connection's mss (ie. from gro segment size). |
| 93 | // This would require a much newer kernel with newer ebpf accessors. |
| 94 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 95 | uint64_t packets = 1; |
| 96 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 1ec2453 | 2020-05-23 22:07:00 +0000 | [diff] [blame^] | 97 | if (bytes > pmtu) { |
| 98 | const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12; |
| 99 | const int mss = pmtu - tcp_overhead; |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 100 | const uint64_t payload = bytes - tcp_overhead; |
| 101 | packets = (payload + mss - 1) / mss; |
| 102 | bytes = tcp_overhead * packets + payload; |
| 103 | } |
| 104 | |
Maciej Żenczykowski | ac14cb5 | 2020-05-23 20:48:00 +0000 | [diff] [blame] | 105 | // Are we past the limit? If so, then abort... |
| 106 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 107 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 108 | // a packet we let the core stack deal with things. |
| 109 | // (The core stack needs to handle limits correctly anyway, |
| 110 | // since we don't offload all traffic in both directions) |
| 111 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK; |
| 112 | |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 113 | if (!is_ethernet) { |
| 114 | is_ethernet = true; |
| 115 | l2_header_size = sizeof(struct ethhdr); |
| 116 | // Try to inject an ethernet header, and simply return if we fail |
| 117 | if (bpf_skb_change_head(skb, l2_header_size, /*flags*/ 0)) { |
| 118 | __sync_fetch_and_add(&stat_v->rxErrors, 1); |
| 119 | return TC_ACT_OK; |
| 120 | } |
| 121 | |
| 122 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 123 | data = (void*)(long)skb->data; |
| 124 | data_end = (void*)(long)skb->data_end; |
| 125 | eth = data; |
| 126 | ip6 = (void*)(eth + 1); |
| 127 | |
| 128 | // I do not believe this can ever happen, but keep the verifier happy... |
| 129 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_SHOT; |
| 130 | }; |
| 131 | |
| 132 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 133 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 134 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 135 | uint8_t old_hl = ip6->hop_limit; |
| 136 | --ip6->hop_limit; |
| 137 | uint8_t new_hl = ip6->hop_limit; |
| 138 | |
| 139 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 140 | // (-ENOTSUPP) if it isn't. |
| 141 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 142 | |
| 143 | __sync_fetch_and_add(&stat_v->rxPackets, packets); |
| 144 | __sync_fetch_and_add(&stat_v->rxBytes, bytes); |
| 145 | |
| 146 | // Overwrite any mac header with the new one |
| 147 | *eth = v->macHeader; |
| 148 | |
| 149 | // Redirect to forwarded interface. |
| 150 | // |
| 151 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 152 | // The redirect actually happens after the ebpf program has already terminated, |
| 153 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 154 | // we can do about it here. |
| 155 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
| 156 | } |
| 157 | |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 158 | SEC("schedcls/ingress/tether_ether") |
| 159 | int sched_cls_ingress_tether_ether(struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 160 | return do_forward(skb, true); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Maciej Żenczykowski | 3c4a44c | 2020-04-22 04:11:43 +0000 | [diff] [blame] | 163 | // bpf_skb_change_head() is only present on 4.14+, |
| 164 | // |
| 165 | // Hence define a no-op stub for older kernels. |
| 166 | // |
| 167 | // Note: section names must be unique to prevent programs from |
| 168 | // appending to each other, so instead the bpf loader will strip |
| 169 | // everything past the final $ symbol when actually pinning |
| 170 | // the program into the filesystem. |
| 171 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$stub", AID_ROOT, AID_ROOT, |
| 172 | sched_cls_ingress_tether_rawip_stub, KVER_NONE, KVER(4, 14, 0)) |
| 173 | (struct __sk_buff* skb) { |
| 174 | return TC_ACT_OK; |
| 175 | } |
| 176 | |
| 177 | // and the real implementation for newer kernels |
| 178 | DEFINE_BPF_PROG_KVER("schedcls/ingress/tether_rawip$4_14", AID_ROOT, AID_ROOT, |
| 179 | sched_cls_ingress_tether_rawip_4_14, KVER(4, 14, 0)) |
Maciej Żenczykowski | 27d0d15 | 2020-04-16 05:07:39 +0000 | [diff] [blame] | 180 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 181 | return do_forward(skb, false); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Treehugger Robot | 2b9df0c | 2020-03-20 23:23:27 +0000 | [diff] [blame] | 184 | LICENSE("Apache 2.0"); |