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 | |
| 72 | uint32_t stat_k = skb->ifindex; |
| 73 | |
| 74 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_k); |
| 75 | |
| 76 | // If we don't have anywhere to put stats, create an empty entry. |
| 77 | if (!stat_v) { |
| 78 | TetherStatsValue emptyStats = {}; |
| 79 | bpf_tether_stats_map_update_elem(&stat_k, &emptyStats, BPF_NOEXIST); |
| 80 | stat_v = bpf_tether_stats_map_lookup_elem(&stat_k); |
| 81 | } |
| 82 | |
| 83 | // If we *still* don't have anywhere to put stats, then abort... |
| 84 | if (!stat_v) return TC_ACT_OK; |
| 85 | |
| 86 | // This is approximate handling of tcp/ip overhead for incoming LRO/GRO packets: |
| 87 | // mtu of 1500 is not necessarily correct, but worst case we simply undercount, |
| 88 | // which is still better then not accounting for this overhead at all. |
| 89 | // Note: this really shouldn't be device mtu at all, but rather should be derived |
| 90 | // from this particular connection's mss - which requires a much newer kernel. |
| 91 | const int mtu = 1500; |
| 92 | uint64_t packets = 1; |
| 93 | uint64_t bytes = skb->len; |
| 94 | if (bytes > mtu) { |
| 95 | const bool is_ipv6 = (skb->protocol == htons(ETH_P_IPV6)); |
| 96 | const int ip_overhead = (is_ipv6 ? sizeof(struct ipv6hdr) : sizeof(struct iphdr)); |
| 97 | const int tcp_overhead = ip_overhead + sizeof(struct tcphdr) + 12; |
| 98 | const int mss = mtu - tcp_overhead; |
| 99 | const uint64_t payload = bytes - tcp_overhead; |
| 100 | packets = (payload + mss - 1) / mss; |
| 101 | bytes = tcp_overhead * packets + payload; |
| 102 | } |
| 103 | |
| 104 | if (!is_ethernet) { |
| 105 | is_ethernet = true; |
| 106 | l2_header_size = sizeof(struct ethhdr); |
| 107 | // Try to inject an ethernet header, and simply return if we fail |
| 108 | if (bpf_skb_change_head(skb, l2_header_size, /*flags*/ 0)) { |
| 109 | __sync_fetch_and_add(&stat_v->rxErrors, 1); |
| 110 | return TC_ACT_OK; |
| 111 | } |
| 112 | |
| 113 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 114 | data = (void*)(long)skb->data; |
| 115 | data_end = (void*)(long)skb->data_end; |
| 116 | eth = data; |
| 117 | ip6 = (void*)(eth + 1); |
| 118 | |
| 119 | // I do not believe this can ever happen, but keep the verifier happy... |
| 120 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_SHOT; |
| 121 | }; |
| 122 | |
| 123 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 124 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 125 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 126 | uint8_t old_hl = ip6->hop_limit; |
| 127 | --ip6->hop_limit; |
| 128 | uint8_t new_hl = ip6->hop_limit; |
| 129 | |
| 130 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 131 | // (-ENOTSUPP) if it isn't. |
| 132 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 133 | |
| 134 | __sync_fetch_and_add(&stat_v->rxPackets, packets); |
| 135 | __sync_fetch_and_add(&stat_v->rxBytes, bytes); |
| 136 | |
| 137 | // Overwrite any mac header with the new one |
| 138 | *eth = v->macHeader; |
| 139 | |
| 140 | // Redirect to forwarded interface. |
| 141 | // |
| 142 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 143 | // The redirect actually happens after the ebpf program has already terminated, |
| 144 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 145 | // we can do about it here. |
| 146 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
| 147 | } |
| 148 | |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 149 | SEC("schedcls/ingress/tether_ether") |
| 150 | int sched_cls_ingress_tether_ether(struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 151 | return do_forward(skb, true); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Maciej Żenczykowski | 3c4a44c | 2020-04-22 04:11:43 +0000 | [diff] [blame] | 154 | // bpf_skb_change_head() is only present on 4.14+, |
| 155 | // |
| 156 | // Hence define a no-op stub for older kernels. |
| 157 | // |
| 158 | // Note: section names must be unique to prevent programs from |
| 159 | // appending to each other, so instead the bpf loader will strip |
| 160 | // everything past the final $ symbol when actually pinning |
| 161 | // the program into the filesystem. |
| 162 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$stub", AID_ROOT, AID_ROOT, |
| 163 | sched_cls_ingress_tether_rawip_stub, KVER_NONE, KVER(4, 14, 0)) |
| 164 | (struct __sk_buff* skb) { |
| 165 | return TC_ACT_OK; |
| 166 | } |
| 167 | |
| 168 | // and the real implementation for newer kernels |
| 169 | DEFINE_BPF_PROG_KVER("schedcls/ingress/tether_rawip$4_14", AID_ROOT, AID_ROOT, |
| 170 | sched_cls_ingress_tether_rawip_4_14, KVER(4, 14, 0)) |
Maciej Żenczykowski | 27d0d15 | 2020-04-16 05:07:39 +0000 | [diff] [blame] | 171 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame] | 172 | return do_forward(skb, false); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Treehugger Robot | 2b9df0c | 2020-03-20 23:23:27 +0000 | [diff] [blame] | 175 | LICENSE("Apache 2.0"); |