blob: fbb5318f16527b9617b091e90249e1b152725459 [file] [log] [blame]
Hungming Chen41b2ae12020-02-04 15:09:37 +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>
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080018#include <linux/ip.h>
19#include <linux/ipv6.h>
Hungming Chen41b2ae12020-02-04 15:09:37 +080020#include <linux/pkt_cls.h>
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080021#include <linux/tcp.h>
Hungming Chen41b2ae12020-02-04 15:09:37 +080022
23#include "bpf_helpers.h"
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080024#include "bpf_net_helpers.h"
Hungming Chen41b2ae12020-02-04 15:09:37 +080025#include "netdbpf/bpf_shared.h"
26
Maciej Żenczykowski565b6fb2020-01-27 01:58:40 -080027DEFINE_BPF_MAP_GRW(tether_ingress_map, HASH, TetherIngressKey, TetherIngressValue, 64,
28 AID_NETWORK_STACK)
Hungming Chen41b2ae12020-02-04 15:09:37 +080029
Maciej Żenczykowski0510b012020-02-12 05:03:05 -080030// Tethering stats, indexed by upstream interface.
Maciej Żenczykowskib79d1842020-03-20 07:11:43 +000031DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, uint32_t, TetherStatsValue, IFACE_STATS_MAP_SIZE,
32 AID_NETWORK_STACK)
Maciej Żenczykowski0510b012020-02-12 05:03:05 -080033
Maciej Żenczykowski8887cce2020-05-04 18:14:59 -070034// Tethering data limit, indexed by upstream interface.
35// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
36DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, uint32_t, uint64_t, IFACE_STATS_MAP_SIZE,
37 AID_NETWORK_STACK)
38
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080039static 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 Żenczykowskiac14cb52020-05-23 20:48:00 +000072 uint32_t stat_and_limit_k = skb->ifindex;
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080073
Maciej Żenczykowskiac14cb52020-05-23 20:48:00 +000074 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080075
Maciej Żenczykowskiac14cb52020-05-23 20:48:00 +000076 // If we don't have anywhere to put stats, then abort...
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -080077 if (!stat_v) return TC_ACT_OK;
78
Maciej Żenczykowskiac14cb52020-05-23 20:48:00 +000079 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 Żenczykowskia76543e2020-02-10 17:53:47 -080084 // This is approximate handling of tcp/ip overhead for incoming LRO/GRO packets:
85 // mtu of 1500 is not necessarily correct, but worst case we simply undercount,
86 // which is still better then not accounting for this overhead at all.
87 // Note: this really shouldn't be device mtu at all, but rather should be derived
88 // from this particular connection's mss - which requires a much newer kernel.
89 const int mtu = 1500;
90 uint64_t packets = 1;
91 uint64_t bytes = skb->len;
92 if (bytes > mtu) {
93 const bool is_ipv6 = (skb->protocol == htons(ETH_P_IPV6));
94 const int ip_overhead = (is_ipv6 ? sizeof(struct ipv6hdr) : sizeof(struct iphdr));
95 const int tcp_overhead = ip_overhead + sizeof(struct tcphdr) + 12;
96 const int mss = mtu - tcp_overhead;
97 const uint64_t payload = bytes - tcp_overhead;
98 packets = (payload + mss - 1) / mss;
99 bytes = tcp_overhead * packets + payload;
100 }
101
Maciej Żenczykowskiac14cb52020-05-23 20:48:00 +0000102 // Are we past the limit? If so, then abort...
103 // Note: will not overflow since u64 is 936 years even at 5Gbps.
104 // Do not drop here. Offload is just that, whenever we fail to handle
105 // a packet we let the core stack deal with things.
106 // (The core stack needs to handle limits correctly anyway,
107 // since we don't offload all traffic in both directions)
108 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
109
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -0800110 if (!is_ethernet) {
111 is_ethernet = true;
112 l2_header_size = sizeof(struct ethhdr);
113 // Try to inject an ethernet header, and simply return if we fail
114 if (bpf_skb_change_head(skb, l2_header_size, /*flags*/ 0)) {
115 __sync_fetch_and_add(&stat_v->rxErrors, 1);
116 return TC_ACT_OK;
117 }
118
119 // bpf_skb_change_head() invalidates all pointers - reload them
120 data = (void*)(long)skb->data;
121 data_end = (void*)(long)skb->data_end;
122 eth = data;
123 ip6 = (void*)(eth + 1);
124
125 // I do not believe this can ever happen, but keep the verifier happy...
126 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_SHOT;
127 };
128
129 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
130 // thus corrections for it need to be done in 16-byte chunks at even offsets.
131 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
132 uint8_t old_hl = ip6->hop_limit;
133 --ip6->hop_limit;
134 uint8_t new_hl = ip6->hop_limit;
135
136 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
137 // (-ENOTSUPP) if it isn't.
138 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
139
140 __sync_fetch_and_add(&stat_v->rxPackets, packets);
141 __sync_fetch_and_add(&stat_v->rxBytes, bytes);
142
143 // Overwrite any mac header with the new one
144 *eth = v->macHeader;
145
146 // Redirect to forwarded interface.
147 //
148 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
149 // The redirect actually happens after the ebpf program has already terminated,
150 // and can fail for example for mtu reasons at that point in time, but there's nothing
151 // we can do about it here.
152 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
153}
154
Hungming Chen41b2ae12020-02-04 15:09:37 +0800155SEC("schedcls/ingress/tether_ether")
156int sched_cls_ingress_tether_ether(struct __sk_buff* skb) {
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -0800157 return do_forward(skb, true);
Hungming Chen41b2ae12020-02-04 15:09:37 +0800158}
159
Maciej Żenczykowski3c4a44c2020-04-22 04:11:43 +0000160// bpf_skb_change_head() is only present on 4.14+,
161//
162// Hence define a no-op stub for older kernels.
163//
164// Note: section names must be unique to prevent programs from
165// appending to each other, so instead the bpf loader will strip
166// everything past the final $ symbol when actually pinning
167// the program into the filesystem.
168DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$stub", AID_ROOT, AID_ROOT,
169 sched_cls_ingress_tether_rawip_stub, KVER_NONE, KVER(4, 14, 0))
170(struct __sk_buff* skb) {
171 return TC_ACT_OK;
172}
173
174// and the real implementation for newer kernels
175DEFINE_BPF_PROG_KVER("schedcls/ingress/tether_rawip$4_14", AID_ROOT, AID_ROOT,
176 sched_cls_ingress_tether_rawip_4_14, KVER(4, 14, 0))
Maciej Żenczykowski27d0d152020-04-16 05:07:39 +0000177(struct __sk_buff* skb) {
Maciej Żenczykowskia76543e2020-02-10 17:53:47 -0800178 return do_forward(skb, false);
Hungming Chen41b2ae12020-02-04 15:09:37 +0800179}
180
Treehugger Robot2b9df0c2020-03-20 23:23:27 +0000181LICENSE("Apache 2.0");