blob: 1dc727c108e6e3dc449a55e15aca87dad858c731 [file] [log] [blame]
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -08001/*
2 * Copyright (C) 2019 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
Maciej Żenczykowski11141da2024-03-15 18:21:33 -070017// The resulting .o needs to load on Android T+
Maciej Żenczykowski4e4f8722024-06-15 06:38:08 -070018#define BPFLOADER_MIN_VER BPFLOADER_MAINLINE_T_VERSION
Maciej Żenczykowskiacebffb2022-05-16 16:05:15 -070019
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080020#include "bpf_net_helpers.h"
Maciej Żenczykowski7b452a12022-12-08 13:10:29 +000021#include "clatd.h"
Maciej Żenczykowskice9108f2022-06-15 02:02:21 -070022#include "clat_mark.h"
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080023
Maciej Żenczykowskie0ddc472022-10-24 02:43:21 -070024// IP flags. (from kernel's include/net/ip.h)
25#define IP_CE 0x8000 // Flag: "Congestion" (really reserved 'evil bit')
26#define IP_DF 0x4000 // Flag: "Don't Fragment"
27#define IP_MF 0x2000 // Flag: "More Fragments"
28#define IP_OFFSET 0x1FFF // "Fragment Offset" part
29
30// from kernel's include/net/ipv6.h
31struct frag_hdr {
32 __u8 nexthdr;
33 __u8 reserved; // always zero
34 __be16 frag_off; // 13 bit offset, 2 bits zero, 1 bit "More Fragments"
35 __be32 identification;
36};
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080037
38DEFINE_BPF_MAP_GRW(clat_ingress6_map, HASH, ClatIngress6Key, ClatIngress6Value, 16, AID_SYSTEM)
39
Maciej Żenczykowski9b239522023-01-03 14:59:54 +000040static inline __always_inline int nat64(struct __sk_buff* skb,
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -070041 const struct rawip_bool rawip,
Maciej Żenczykowski3a645682023-10-06 15:11:01 -070042 const struct kver_uint kver) {
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -070043 const bool is_ethernet = !rawip.rawip;
44
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080045 // Require ethernet dst mac address to be our unicast address.
46 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE;
47
48 // Must be meta-ethernet IPv6 frame
49 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_PIPE;
50
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -070051 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
52
53 // Not clear if this is actually necessary considering we use DPA (Direct Packet Access),
54 // but we need to make sure we can read the IPv6 header reliably so that we can set
55 // skb->mark = 0xDeadC1a7 for packets we fail to offload.
Maciej Żenczykowski824fb292022-04-11 23:29:46 -070056 try_make_writable(skb, l2_header_size + sizeof(struct ipv6hdr));
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -070057
58 void* data = (void*)(long)skb->data;
59 const void* data_end = (void*)(long)skb->data_end;
60 const struct ethhdr* const eth = is_ethernet ? data : NULL; // used iff is_ethernet
61 const struct ipv6hdr* const ip6 = is_ethernet ? (void*)(eth + 1) : data;
62
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080063 // Must have (ethernet and) ipv6 header
64 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_PIPE;
65
66 // Ethertype - if present - must be IPv6
67 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_PIPE;
68
69 // IP version must be 6
70 if (ip6->version != 6) return TC_ACT_PIPE;
71
72 // Maximum IPv6 payload length that can be translated to IPv4
Maciej Żenczykowskibdcb8962023-03-17 18:15:34 +000073 // Note: technically this check is too strict for an IPv6 fragment,
74 // which by virtue of stripping the extra 8 byte fragment extension header,
75 // could thus be 8 bytes larger and still fit in an ipv4 packet post
76 // translation. However... who ever heard of receiving ~64KB frags...
77 // fragments are kind of by definition smaller than ingress device mtu,
78 // and thus, on the internet, very very unlikely to exceed 1500 bytes.
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080079 if (ntohs(ip6->payload_len) > 0xFFFF - sizeof(struct iphdr)) return TC_ACT_PIPE;
80
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080081 ClatIngress6Key k = {
82 .iif = skb->ifindex,
83 .pfx96.in6_u.u6_addr32 =
84 {
85 ip6->saddr.in6_u.u6_addr32[0],
86 ip6->saddr.in6_u.u6_addr32[1],
87 ip6->saddr.in6_u.u6_addr32[2],
88 },
89 .local6 = ip6->daddr,
90 };
91
92 ClatIngress6Value* v = bpf_clat_ingress6_map_lookup_elem(&k);
93
94 if (!v) return TC_ACT_PIPE;
95
Maciej Żenczykowski321321c2022-10-24 03:08:06 -070096 __u8 proto = ip6->nexthdr;
97 __be16 ip_id = 0;
98 __be16 frag_off = htons(IP_DF);
Maciej Żenczykowski9b239522023-01-03 14:59:54 +000099 __u16 tot_len = ntohs(ip6->payload_len) + sizeof(struct iphdr); // cannot overflow, see above
100
101 if (proto == IPPROTO_FRAGMENT) {
102 // Fragment handling requires bpf_skb_adjust_room which is 4.14+
Maciej Żenczykowski3a645682023-10-06 15:11:01 -0700103 if (!KVER_IS_AT_LEAST(kver, 4, 14, 0)) return TC_ACT_PIPE;
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000104
105 // Must have (ethernet and) ipv6 header and ipv6 fragment extension header
106 if (data + l2_header_size + sizeof(*ip6) + sizeof(struct frag_hdr) > data_end)
107 return TC_ACT_PIPE;
108 const struct frag_hdr *frag = (const struct frag_hdr *)(ip6 + 1);
109 proto = frag->nexthdr;
110 // RFC6145: use bottom 16-bits of network endian 32-bit IPv6 ID field for 16-bit IPv4 field.
111 // this is equivalent to: ip_id = htons(ntohl(frag->identification));
112 ip_id = frag->identification >> 16;
113 // Conversion of 16-bit IPv6 frag offset to 16-bit IPv4 frag offset field.
114 // IPv6 is '13 bits of offset in multiples of 8' + 2 zero bits + more fragment bit
115 // IPv4 is zero bit + don't frag bit + more frag bit + '13 bits of offset in multiples of 8'
116 frag_off = ntohs(frag->frag_off);
117 frag_off = ((frag_off & 1) << 13) | (frag_off >> 3);
118 frag_off = htons(frag_off);
119 // Note that by construction tot_len is guaranteed to not underflow here
120 tot_len -= sizeof(struct frag_hdr);
121 // This is a badly formed IPv6 packet with less payload than the size of an IPv6 Frag EH
122 if (tot_len < sizeof(struct iphdr)) return TC_ACT_PIPE;
123 }
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700124
125 switch (proto) {
Maciej Żenczykowski43d31bb2023-07-25 16:16:46 +0000126 case IPPROTO_TCP: // For TCP, UDP & UDPLITE the checksum neutrality of the chosen
127 case IPPROTO_UDP: // IPv6 address means there is no need to update their checksums.
128 case IPPROTO_UDPLITE: //
129 case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers,
130 case IPPROTO_ESP: // since there is never a checksum to update.
Hungming Chen6c0b1e82022-04-01 19:51:56 +0800131 break;
132
133 default: // do not know how to handle anything else
134 // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING.
135 // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The
136 // duplicate one in ip6tables is not necessary.
137 skb->mark = CLAT_MARK;
138 return TC_ACT_PIPE;
139 }
140
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800141 struct ethhdr eth2; // used iff is_ethernet
142 if (is_ethernet) {
143 eth2 = *eth; // Copy over the ethernet header (src/dst mac)
144 eth2.h_proto = htons(ETH_P_IP); // But replace the ethertype
145 }
146
147 struct iphdr ip = {
148 .version = 4, // u4
149 .ihl = sizeof(struct iphdr) / sizeof(__u32), // u4
150 .tos = (ip6->priority << 4) + (ip6->flow_lbl[0] >> 4), // u8
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000151 .tot_len = htons(tot_len), // be16
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700152 .id = ip_id, // be16
153 .frag_off = frag_off, // be16
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800154 .ttl = ip6->hop_limit, // u8
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700155 .protocol = proto, // u8
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800156 .check = 0, // u16
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700157 .saddr = ip6->saddr.in6_u.u6_addr32[3], // be32
158 .daddr = v->local4.s_addr, // be32
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800159 };
160
161 // Calculate the IPv4 one's complement checksum of the IPv4 header.
162 __wsum sum4 = 0;
Maciej Żenczykowskic11dfd82024-07-24 17:54:41 -0700163 for (unsigned i = 0; i < sizeof(ip) / sizeof(__u16); ++i) {
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800164 sum4 += ((__u16*)&ip)[i];
165 }
166 // Note that sum4 is guaranteed to be non-zero by virtue of ip.version == 4
167 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
168 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
169 ip.check = (__u16)~sum4; // sum4 cannot be zero, so this is never 0xFFFF
170
171 // Calculate the *negative* IPv6 16-bit one's complement checksum of the IPv6 header.
172 __wsum sum6 = 0;
173 // We'll end up with a non-zero sum due to ip6->version == 6 (which has '0' bits)
Maciej Żenczykowskic11dfd82024-07-24 17:54:41 -0700174 for (unsigned i = 0; i < sizeof(*ip6) / sizeof(__u16); ++i) {
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800175 sum6 += ~((__u16*)ip6)[i]; // note the bitwise negation
176 }
177
178 // Note that there is no L4 checksum update: we are relying on the checksum neutrality
179 // of the ipv6 address chosen by netd's ClatdController.
180
181 // Packet mutations begin - point of no return, but if this first modification fails
182 // the packet is probably still pristine, so let clatd handle it.
Hungming Chen6c0b1e82022-04-01 19:51:56 +0800183 if (bpf_skb_change_proto(skb, htons(ETH_P_IP), 0)) {
184 // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING.
185 // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The
186 // duplicate one in ip6tables is not necessary.
187 skb->mark = CLAT_MARK;
188 return TC_ACT_PIPE;
189 }
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800190
191 // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet.
192 //
193 // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload,
194 // thus we need to subtract out the ipv6 header's sum, and add in the ipv4 header's sum.
195 // However, by construction of ip.check above the checksum of an ipv4 header is zero.
196 // Thus we only need to subtract the ipv6 header's sum, which is the same as adding
197 // in the sum of the bitwise negation of the ipv6 header.
198 //
199 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
200 // (-ENOTSUPP) if it isn't. So we just ignore the return code.
201 //
202 // if (skb->ip_summed == CHECKSUM_COMPLETE)
203 // return (skb->csum = csum_add(skb->csum, csum));
204 // else
205 // return -ENOTSUPP;
206 bpf_csum_update(skb, sum6);
207
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000208 // Technically 'kver < KVER_4_14' already implies 'frag_off == htons(IP_DF)' due to logic above,
209 // thus the initial 'kver >= KVER_4_14' check here is entirely superfluous.
210 //
211 // However, we *need* the compiler (when compiling the program for 4.9) to entirely
212 // optimize out the call to bpf_skb_adjust_room() bpf helper: it's not enough for it to emit
213 // an unreachable call to it, it must *not* emit it at all (otherwise the 4.9 kernel's
214 // bpf verifier will refuse to load a program with an unknown bpf helper call)
215 //
216 // This is easiest to achieve by being very explicit in the if clause,
217 // better safe than sorry...
218 //
219 // Note: we currently have no TreeHugger coverage for 4.9-T devices (there are no such
220 // Pixel or cuttlefish devices), so likely you won't notice for months if this breaks...
Maciej Żenczykowski3a645682023-10-06 15:11:01 -0700221 if (KVER_IS_AT_LEAST(kver, 4, 14, 0) && frag_off != htons(IP_DF)) {
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000222 // If we're converting an IPv6 Fragment, we need to trim off 8 more bytes
223 // We're beyond recovery on error here... but hard to imagine how this could fail.
224 if (bpf_skb_adjust_room(skb, -(__s32)sizeof(struct frag_hdr), BPF_ADJ_ROOM_NET, /*flags*/0))
225 return TC_ACT_SHOT;
226 }
227
Maciej Żenczykowski66da6a92023-09-08 05:08:25 +0000228 try_make_writable(skb, l2_header_size + sizeof(struct iphdr));
229
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800230 // bpf_skb_change_proto() invalidates all pointers - reload them.
231 data = (void*)(long)skb->data;
232 data_end = (void*)(long)skb->data_end;
233
234 // I cannot think of any valid way for this error condition to trigger, however I do
235 // believe the explicit check is required to keep the in kernel ebpf verifier happy.
236 if (data + l2_header_size + sizeof(struct iphdr) > data_end) return TC_ACT_SHOT;
237
238 if (is_ethernet) {
239 struct ethhdr* new_eth = data;
240
241 // Copy over the updated ethernet header
242 *new_eth = eth2;
243
244 // Copy over the new ipv4 header.
245 *(struct iphdr*)(new_eth + 1) = ip;
246 } else {
247 // Copy over the new ipv4 header without an ethernet header.
248 *(struct iphdr*)data = ip;
249 }
250
Maciej Żenczykowski8b3f9d92024-03-13 03:39:36 +0000251 // Count successfully translated packet
252 __sync_fetch_and_add(&v->packets, 1);
253 __sync_fetch_and_add(&v->bytes, skb->len - l2_header_size);
254
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800255 // Redirect, possibly back to same interface, so tcpdump sees packet twice.
256 if (v->oif) return bpf_redirect(v->oif, BPF_F_INGRESS);
257
258 // Just let it through, tcpdump will not see IPv4 packet.
259 return TC_ACT_PIPE;
260}
261
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000262DEFINE_BPF_PROG_KVER("schedcls/ingress6/clat_ether$4_14", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_ether_4_14, KVER_4_14)
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800263(struct __sk_buff* skb) {
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000264 return nat64(skb, ETHER, KVER_4_14);
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800265}
266
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000267DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress6/clat_ether$4_9", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_ether_4_9, KVER_NONE, KVER_4_14)
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800268(struct __sk_buff* skb) {
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000269 return nat64(skb, ETHER, KVER_NONE);
270}
271
272DEFINE_BPF_PROG_KVER("schedcls/ingress6/clat_rawip$4_14", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip_4_14, KVER_4_14)
273(struct __sk_buff* skb) {
274 return nat64(skb, RAWIP, KVER_4_14);
275}
276
277DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress6/clat_rawip$4_9", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip_4_9, KVER_NONE, KVER_4_14)
278(struct __sk_buff* skb) {
279 return nat64(skb, RAWIP, KVER_NONE);
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800280}
281
282DEFINE_BPF_MAP_GRW(clat_egress4_map, HASH, ClatEgress4Key, ClatEgress4Value, 16, AID_SYSTEM)
283
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800284DEFINE_BPF_PROG("schedcls/egress4/clat_rawip", AID_ROOT, AID_SYSTEM, sched_cls_egress4_clat_rawip)
285(struct __sk_buff* skb) {
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -0700286 // Must be meta-ethernet IPv4 frame
287 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_PIPE;
288
289 // Possibly not needed, but for consistency with nat64 up above
Maciej Żenczykowski824fb292022-04-11 23:29:46 -0700290 try_make_writable(skb, sizeof(struct iphdr));
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -0700291
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800292 void* data = (void*)(long)skb->data;
293 const void* data_end = (void*)(long)skb->data_end;
294 const struct iphdr* const ip4 = data;
295
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800296 // Must have ipv4 header
297 if (data + sizeof(*ip4) > data_end) return TC_ACT_PIPE;
298
299 // IP version must be 4
300 if (ip4->version != 4) return TC_ACT_PIPE;
301
302 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
303 if (ip4->ihl != 5) return TC_ACT_PIPE;
304
305 // Calculate the IPv4 one's complement checksum of the IPv4 header.
306 __wsum sum4 = 0;
Maciej Żenczykowskic11dfd82024-07-24 17:54:41 -0700307 for (unsigned i = 0; i < sizeof(*ip4) / sizeof(__u16); ++i) {
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800308 sum4 += ((__u16*)ip4)[i];
309 }
310 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
311 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
312 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
313 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
314 if (sum4 != 0xFFFF) return TC_ACT_PIPE;
315
316 // Minimum IPv4 total length is the size of the header
317 if (ntohs(ip4->tot_len) < sizeof(*ip4)) return TC_ACT_PIPE;
318
319 // We are incapable of dealing with IPv4 fragments
320 if (ip4->frag_off & ~htons(IP_DF)) return TC_ACT_PIPE;
321
322 switch (ip4->protocol) {
Maciej Żenczykowski43d31bb2023-07-25 16:16:46 +0000323 case IPPROTO_TCP: // For TCP, UDP & UDPLITE the checksum neutrality of the chosen
324 case IPPROTO_UDPLITE: // IPv6 address means there is no need to update their checksums.
325 case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers,
326 case IPPROTO_ESP: // since there is never a checksum to update.
327 break;
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800328
Maciej Żenczykowski43d31bb2023-07-25 16:16:46 +0000329 case IPPROTO_UDP: // See above comment, but must also have UDP header...
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800330 if (data + sizeof(*ip4) + sizeof(struct udphdr) > data_end) return TC_ACT_PIPE;
331 const struct udphdr* uh = (const struct udphdr*)(ip4 + 1);
332 // If IPv4/UDP checksum is 0 then fallback to clatd so it can calculate the
333 // checksum. Otherwise the network or more likely the NAT64 gateway might
334 // drop the packet because in most cases IPv6/UDP packets with a zero checksum
335 // are invalid. See RFC 6935. TODO: calculate checksum via bpf_csum_diff()
336 if (!uh->check) return TC_ACT_PIPE;
337 break;
338
339 default: // do not know how to handle anything else
340 return TC_ACT_PIPE;
341 }
342
343 ClatEgress4Key k = {
344 .iif = skb->ifindex,
345 .local4.s_addr = ip4->saddr,
346 };
347
348 ClatEgress4Value* v = bpf_clat_egress4_map_lookup_elem(&k);
349
350 if (!v) return TC_ACT_PIPE;
351
352 // Translating without redirecting doesn't make sense.
353 if (!v->oif) return TC_ACT_PIPE;
354
355 // This implementation is currently limited to rawip.
356 if (v->oifIsEthernet) return TC_ACT_PIPE;
357
358 struct ipv6hdr ip6 = {
359 .version = 6, // __u8:4
360 .priority = ip4->tos >> 4, // __u8:4
361 .flow_lbl = {(ip4->tos & 0xF) << 4, 0, 0}, // __u8[3]
362 .payload_len = htons(ntohs(ip4->tot_len) - 20), // __be16
363 .nexthdr = ip4->protocol, // __u8
364 .hop_limit = ip4->ttl, // __u8
365 .saddr = v->local6, // struct in6_addr
366 .daddr = v->pfx96, // struct in6_addr
367 };
368 ip6.daddr.in6_u.u6_addr32[3] = ip4->daddr;
369
370 // Calculate the IPv6 16-bit one's complement checksum of the IPv6 header.
371 __wsum sum6 = 0;
372 // We'll end up with a non-zero sum due to ip6.version == 6
Maciej Żenczykowskic11dfd82024-07-24 17:54:41 -0700373 for (unsigned i = 0; i < sizeof(ip6) / sizeof(__u16); ++i) {
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800374 sum6 += ((__u16*)&ip6)[i];
375 }
376
377 // Note that there is no L4 checksum update: we are relying on the checksum neutrality
378 // of the ipv6 address chosen by netd's ClatdController.
379
380 // Packet mutations begin - point of no return, but if this first modification fails
381 // the packet is probably still pristine, so let clatd handle it.
382 if (bpf_skb_change_proto(skb, htons(ETH_P_IPV6), 0)) return TC_ACT_PIPE;
383
384 // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet.
385 //
386 // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload,
387 // thus we need to subtract out the ipv4 header's sum, and add in the ipv6 header's sum.
388 // However, we've already verified the ipv4 checksum is correct and thus 0.
389 // Thus we only need to add the ipv6 header's sum.
390 //
391 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
392 // (-ENOTSUPP) if it isn't. So we just ignore the return code (see above for more details).
393 bpf_csum_update(skb, sum6);
394
395 // bpf_skb_change_proto() invalidates all pointers - reload them.
396 data = (void*)(long)skb->data;
397 data_end = (void*)(long)skb->data_end;
398
399 // I cannot think of any valid way for this error condition to trigger, however I do
400 // believe the explicit check is required to keep the in kernel ebpf verifier happy.
401 if (data + sizeof(ip6) > data_end) return TC_ACT_SHOT;
402
403 // Copy over the new ipv6 header without an ethernet header.
404 *(struct ipv6hdr*)data = ip6;
405
Maciej Żenczykowski8b3f9d92024-03-13 03:39:36 +0000406 // Count successfully translated packet
407 __sync_fetch_and_add(&v->packets, 1);
408 __sync_fetch_and_add(&v->bytes, skb->len);
409
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800410 // Redirect to non v4-* interface. Tcpdump only sees packet after this redirect.
411 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
412}
413
414LICENSE("Apache 2.0");
Maciej Żenczykowskic41e35d2022-08-04 13:58:46 +0000415CRITICAL("Connectivity");
Maciej Żenczykowskide1342a2023-06-09 05:45:57 +0000416DISABLE_BTF_ON_USER_BUILDS();