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 | 565b6fb | 2020-01-27 01:58:40 -0800 | [diff] [blame] | 31 | DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, uint32_t, TetherStatsValue, 16, AID_NETWORK_STACK) |
Maciej Żenczykowski | 0510b01 | 2020-02-12 05:03:05 -0800 | [diff] [blame] | 32 | |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame^] | 33 | static inline __always_inline int do_forward(struct __sk_buff* skb, bool is_ethernet) { |
| 34 | int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 35 | void* data = (void*)(long)skb->data; |
| 36 | const void* data_end = (void*)(long)skb->data_end; |
| 37 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 38 | struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data; |
| 39 | |
| 40 | // Must be meta-ethernet IPv6 frame |
| 41 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK; |
| 42 | |
| 43 | // Must have (ethernet and) ipv6 header |
| 44 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK; |
| 45 | |
| 46 | // Ethertype - if present - must be IPv6 |
| 47 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK; |
| 48 | |
| 49 | // IP version must be 6 |
| 50 | if (ip6->version != 6) return TC_ACT_OK; |
| 51 | |
| 52 | // Cannot decrement during forward if already zero or would be zero, |
| 53 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
| 54 | if (ip6->hop_limit <= 1) return TC_ACT_OK; |
| 55 | |
| 56 | TetherIngressKey k = { |
| 57 | .iif = skb->ifindex, |
| 58 | .neigh6 = ip6->daddr, |
| 59 | }; |
| 60 | |
| 61 | TetherIngressValue* v = bpf_tether_ingress_map_lookup_elem(&k); |
| 62 | |
| 63 | // If we don't find any offload information then simply let the core stack handle it... |
| 64 | if (!v) return TC_ACT_OK; |
| 65 | |
| 66 | uint32_t stat_k = skb->ifindex; |
| 67 | |
| 68 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_k); |
| 69 | |
| 70 | // If we don't have anywhere to put stats, create an empty entry. |
| 71 | if (!stat_v) { |
| 72 | TetherStatsValue emptyStats = {}; |
| 73 | bpf_tether_stats_map_update_elem(&stat_k, &emptyStats, BPF_NOEXIST); |
| 74 | stat_v = bpf_tether_stats_map_lookup_elem(&stat_k); |
| 75 | } |
| 76 | |
| 77 | // If we *still* don't have anywhere to put stats, then abort... |
| 78 | if (!stat_v) return TC_ACT_OK; |
| 79 | |
| 80 | // This is approximate handling of tcp/ip overhead for incoming LRO/GRO packets: |
| 81 | // mtu of 1500 is not necessarily correct, but worst case we simply undercount, |
| 82 | // which is still better then not accounting for this overhead at all. |
| 83 | // Note: this really shouldn't be device mtu at all, but rather should be derived |
| 84 | // from this particular connection's mss - which requires a much newer kernel. |
| 85 | const int mtu = 1500; |
| 86 | uint64_t packets = 1; |
| 87 | uint64_t bytes = skb->len; |
| 88 | if (bytes > mtu) { |
| 89 | const bool is_ipv6 = (skb->protocol == htons(ETH_P_IPV6)); |
| 90 | const int ip_overhead = (is_ipv6 ? sizeof(struct ipv6hdr) : sizeof(struct iphdr)); |
| 91 | const int tcp_overhead = ip_overhead + sizeof(struct tcphdr) + 12; |
| 92 | const int mss = mtu - tcp_overhead; |
| 93 | const uint64_t payload = bytes - tcp_overhead; |
| 94 | packets = (payload + mss - 1) / mss; |
| 95 | bytes = tcp_overhead * packets + payload; |
| 96 | } |
| 97 | |
| 98 | if (!is_ethernet) { |
| 99 | is_ethernet = true; |
| 100 | l2_header_size = sizeof(struct ethhdr); |
| 101 | // Try to inject an ethernet header, and simply return if we fail |
| 102 | if (bpf_skb_change_head(skb, l2_header_size, /*flags*/ 0)) { |
| 103 | __sync_fetch_and_add(&stat_v->rxErrors, 1); |
| 104 | return TC_ACT_OK; |
| 105 | } |
| 106 | |
| 107 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 108 | data = (void*)(long)skb->data; |
| 109 | data_end = (void*)(long)skb->data_end; |
| 110 | eth = data; |
| 111 | ip6 = (void*)(eth + 1); |
| 112 | |
| 113 | // I do not believe this can ever happen, but keep the verifier happy... |
| 114 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_SHOT; |
| 115 | }; |
| 116 | |
| 117 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 118 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 119 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 120 | uint8_t old_hl = ip6->hop_limit; |
| 121 | --ip6->hop_limit; |
| 122 | uint8_t new_hl = ip6->hop_limit; |
| 123 | |
| 124 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 125 | // (-ENOTSUPP) if it isn't. |
| 126 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 127 | |
| 128 | __sync_fetch_and_add(&stat_v->rxPackets, packets); |
| 129 | __sync_fetch_and_add(&stat_v->rxBytes, bytes); |
| 130 | |
| 131 | // Overwrite any mac header with the new one |
| 132 | *eth = v->macHeader; |
| 133 | |
| 134 | // Redirect to forwarded interface. |
| 135 | // |
| 136 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 137 | // The redirect actually happens after the ebpf program has already terminated, |
| 138 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 139 | // we can do about it here. |
| 140 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
| 141 | } |
| 142 | |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 143 | SEC("schedcls/ingress/tether_ether") |
| 144 | int sched_cls_ingress_tether_ether(struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame^] | 145 | return do_forward(skb, true); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | SEC("schedcls/ingress/tether_rawip") |
| 149 | int sched_cls_ingress_tether_rawip(struct __sk_buff* skb) { |
Maciej Żenczykowski | a76543e | 2020-02-10 17:53:47 -0800 | [diff] [blame^] | 150 | return do_forward(skb, false); |
Hungming Chen | 41b2ae1 | 2020-02-04 15:09:37 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | char _license[] SEC("license") = "Apache 2.0"; |