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