blob: 7c6811ab337884cfa48dfb7d1afb6ed9b70dc689 [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
17#include <linux/bpf.h>
18#include <linux/if.h>
19#include <linux/if_ether.h>
20#include <linux/in.h>
21#include <linux/in6.h>
22#include <linux/ip.h>
23#include <linux/ipv6.h>
24#include <linux/pkt_cls.h>
25#include <linux/swab.h>
26#include <stdbool.h>
27#include <stdint.h>
28
29// bionic kernel uapi linux/udp.h header is munged...
30#define __kernel_udphdr udphdr
31#include <linux/udp.h>
32
Maciej Żenczykowskif7699522022-05-24 15:56:03 -070033// The resulting .o needs to load on the Android T beta 3 bpfloader
34#define BPFLOADER_MIN_VER BPFLOADER_T_BETA3_VERSION
Maciej Żenczykowskiacebffb2022-05-16 16:05:15 -070035
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080036#include "bpf_helpers.h"
37#include "bpf_net_helpers.h"
Maciej Żenczykowski7b452a12022-12-08 13:10:29 +000038#include "clatd.h"
Maciej Żenczykowskice9108f2022-06-15 02:02:21 -070039#include "clat_mark.h"
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080040
Maciej Żenczykowskie0ddc472022-10-24 02:43:21 -070041// IP flags. (from kernel's include/net/ip.h)
42#define IP_CE 0x8000 // Flag: "Congestion" (really reserved 'evil bit')
43#define IP_DF 0x4000 // Flag: "Don't Fragment"
44#define IP_MF 0x2000 // Flag: "More Fragments"
45#define IP_OFFSET 0x1FFF // "Fragment Offset" part
46
47// from kernel's include/net/ipv6.h
48struct frag_hdr {
49 __u8 nexthdr;
50 __u8 reserved; // always zero
51 __be16 frag_off; // 13 bit offset, 2 bits zero, 1 bit "More Fragments"
52 __be32 identification;
53};
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080054
Maciej Żenczykowski9b239522023-01-03 14:59:54 +000055// constants for passing in to 'bool is_ethernet'
56static const bool RAWIP = false;
57static const bool ETHER = true;
58
59#define KVER_4_14 KVER(4, 14, 0)
60
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080061DEFINE_BPF_MAP_GRW(clat_ingress6_map, HASH, ClatIngress6Key, ClatIngress6Value, 16, AID_SYSTEM)
62
Maciej Żenczykowski9b239522023-01-03 14:59:54 +000063static inline __always_inline int nat64(struct __sk_buff* skb,
64 const bool is_ethernet,
65 const unsigned kver) {
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080066 // Require ethernet dst mac address to be our unicast address.
67 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE;
68
69 // Must be meta-ethernet IPv6 frame
70 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_PIPE;
71
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -070072 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
73
74 // Not clear if this is actually necessary considering we use DPA (Direct Packet Access),
75 // but we need to make sure we can read the IPv6 header reliably so that we can set
76 // skb->mark = 0xDeadC1a7 for packets we fail to offload.
Maciej Żenczykowski824fb292022-04-11 23:29:46 -070077 try_make_writable(skb, l2_header_size + sizeof(struct ipv6hdr));
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -070078
79 void* data = (void*)(long)skb->data;
80 const void* data_end = (void*)(long)skb->data_end;
81 const struct ethhdr* const eth = is_ethernet ? data : NULL; // used iff is_ethernet
82 const struct ipv6hdr* const ip6 = is_ethernet ? (void*)(eth + 1) : data;
83
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -080084 // Must have (ethernet and) ipv6 header
85 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_PIPE;
86
87 // Ethertype - if present - must be IPv6
88 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_PIPE;
89
90 // IP version must be 6
91 if (ip6->version != 6) return TC_ACT_PIPE;
92
93 // Maximum IPv6 payload length that can be translated to IPv4
Maciej Żenczykowskibdcb8962023-03-17 18:15:34 +000094 // Note: technically this check is too strict for an IPv6 fragment,
95 // which by virtue of stripping the extra 8 byte fragment extension header,
96 // could thus be 8 bytes larger and still fit in an ipv4 packet post
97 // translation. However... who ever heard of receiving ~64KB frags...
98 // fragments are kind of by definition smaller than ingress device mtu,
99 // and thus, on the internet, very very unlikely to exceed 1500 bytes.
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800100 if (ntohs(ip6->payload_len) > 0xFFFF - sizeof(struct iphdr)) return TC_ACT_PIPE;
101
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800102 ClatIngress6Key k = {
103 .iif = skb->ifindex,
104 .pfx96.in6_u.u6_addr32 =
105 {
106 ip6->saddr.in6_u.u6_addr32[0],
107 ip6->saddr.in6_u.u6_addr32[1],
108 ip6->saddr.in6_u.u6_addr32[2],
109 },
110 .local6 = ip6->daddr,
111 };
112
113 ClatIngress6Value* v = bpf_clat_ingress6_map_lookup_elem(&k);
114
115 if (!v) return TC_ACT_PIPE;
116
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700117 __u8 proto = ip6->nexthdr;
118 __be16 ip_id = 0;
119 __be16 frag_off = htons(IP_DF);
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000120 __u16 tot_len = ntohs(ip6->payload_len) + sizeof(struct iphdr); // cannot overflow, see above
121
122 if (proto == IPPROTO_FRAGMENT) {
123 // Fragment handling requires bpf_skb_adjust_room which is 4.14+
124 if (kver < KVER_4_14) return TC_ACT_PIPE;
125
126 // Must have (ethernet and) ipv6 header and ipv6 fragment extension header
127 if (data + l2_header_size + sizeof(*ip6) + sizeof(struct frag_hdr) > data_end)
128 return TC_ACT_PIPE;
129 const struct frag_hdr *frag = (const struct frag_hdr *)(ip6 + 1);
130 proto = frag->nexthdr;
131 // RFC6145: use bottom 16-bits of network endian 32-bit IPv6 ID field for 16-bit IPv4 field.
132 // this is equivalent to: ip_id = htons(ntohl(frag->identification));
133 ip_id = frag->identification >> 16;
134 // Conversion of 16-bit IPv6 frag offset to 16-bit IPv4 frag offset field.
135 // IPv6 is '13 bits of offset in multiples of 8' + 2 zero bits + more fragment bit
136 // IPv4 is zero bit + don't frag bit + more frag bit + '13 bits of offset in multiples of 8'
137 frag_off = ntohs(frag->frag_off);
138 frag_off = ((frag_off & 1) << 13) | (frag_off >> 3);
139 frag_off = htons(frag_off);
140 // Note that by construction tot_len is guaranteed to not underflow here
141 tot_len -= sizeof(struct frag_hdr);
142 // This is a badly formed IPv6 packet with less payload than the size of an IPv6 Frag EH
143 if (tot_len < sizeof(struct iphdr)) return TC_ACT_PIPE;
144 }
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700145
146 switch (proto) {
Hungming Chen6c0b1e82022-04-01 19:51:56 +0800147 case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6
148 case IPPROTO_UDP: // address means there is no need to update their checksums.
149 case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers,
150 case IPPROTO_ESP: // since there is never a checksum to update.
151 break;
152
153 default: // do not know how to handle anything else
154 // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING.
155 // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The
156 // duplicate one in ip6tables is not necessary.
157 skb->mark = CLAT_MARK;
158 return TC_ACT_PIPE;
159 }
160
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800161 struct ethhdr eth2; // used iff is_ethernet
162 if (is_ethernet) {
163 eth2 = *eth; // Copy over the ethernet header (src/dst mac)
164 eth2.h_proto = htons(ETH_P_IP); // But replace the ethertype
165 }
166
167 struct iphdr ip = {
168 .version = 4, // u4
169 .ihl = sizeof(struct iphdr) / sizeof(__u32), // u4
170 .tos = (ip6->priority << 4) + (ip6->flow_lbl[0] >> 4), // u8
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000171 .tot_len = htons(tot_len), // be16
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700172 .id = ip_id, // be16
173 .frag_off = frag_off, // be16
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800174 .ttl = ip6->hop_limit, // u8
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700175 .protocol = proto, // u8
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800176 .check = 0, // u16
Maciej Żenczykowski321321c2022-10-24 03:08:06 -0700177 .saddr = ip6->saddr.in6_u.u6_addr32[3], // be32
178 .daddr = v->local4.s_addr, // be32
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800179 };
180
181 // Calculate the IPv4 one's complement checksum of the IPv4 header.
182 __wsum sum4 = 0;
183 for (int i = 0; i < sizeof(ip) / sizeof(__u16); ++i) {
184 sum4 += ((__u16*)&ip)[i];
185 }
186 // Note that sum4 is guaranteed to be non-zero by virtue of ip.version == 4
187 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
188 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
189 ip.check = (__u16)~sum4; // sum4 cannot be zero, so this is never 0xFFFF
190
191 // Calculate the *negative* IPv6 16-bit one's complement checksum of the IPv6 header.
192 __wsum sum6 = 0;
193 // We'll end up with a non-zero sum due to ip6->version == 6 (which has '0' bits)
194 for (int i = 0; i < sizeof(*ip6) / sizeof(__u16); ++i) {
195 sum6 += ~((__u16*)ip6)[i]; // note the bitwise negation
196 }
197
198 // Note that there is no L4 checksum update: we are relying on the checksum neutrality
199 // of the ipv6 address chosen by netd's ClatdController.
200
201 // Packet mutations begin - point of no return, but if this first modification fails
202 // the packet is probably still pristine, so let clatd handle it.
Hungming Chen6c0b1e82022-04-01 19:51:56 +0800203 if (bpf_skb_change_proto(skb, htons(ETH_P_IP), 0)) {
204 // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING.
205 // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The
206 // duplicate one in ip6tables is not necessary.
207 skb->mark = CLAT_MARK;
208 return TC_ACT_PIPE;
209 }
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800210
211 // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet.
212 //
213 // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload,
214 // thus we need to subtract out the ipv6 header's sum, and add in the ipv4 header's sum.
215 // However, by construction of ip.check above the checksum of an ipv4 header is zero.
216 // Thus we only need to subtract the ipv6 header's sum, which is the same as adding
217 // in the sum of the bitwise negation of the ipv6 header.
218 //
219 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
220 // (-ENOTSUPP) if it isn't. So we just ignore the return code.
221 //
222 // if (skb->ip_summed == CHECKSUM_COMPLETE)
223 // return (skb->csum = csum_add(skb->csum, csum));
224 // else
225 // return -ENOTSUPP;
226 bpf_csum_update(skb, sum6);
227
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000228 // Technically 'kver < KVER_4_14' already implies 'frag_off == htons(IP_DF)' due to logic above,
229 // thus the initial 'kver >= KVER_4_14' check here is entirely superfluous.
230 //
231 // However, we *need* the compiler (when compiling the program for 4.9) to entirely
232 // optimize out the call to bpf_skb_adjust_room() bpf helper: it's not enough for it to emit
233 // an unreachable call to it, it must *not* emit it at all (otherwise the 4.9 kernel's
234 // bpf verifier will refuse to load a program with an unknown bpf helper call)
235 //
236 // This is easiest to achieve by being very explicit in the if clause,
237 // better safe than sorry...
238 //
239 // Note: we currently have no TreeHugger coverage for 4.9-T devices (there are no such
240 // Pixel or cuttlefish devices), so likely you won't notice for months if this breaks...
241 if (kver >= KVER_4_14 && frag_off != htons(IP_DF)) {
242 // If we're converting an IPv6 Fragment, we need to trim off 8 more bytes
243 // We're beyond recovery on error here... but hard to imagine how this could fail.
244 if (bpf_skb_adjust_room(skb, -(__s32)sizeof(struct frag_hdr), BPF_ADJ_ROOM_NET, /*flags*/0))
245 return TC_ACT_SHOT;
246 }
247
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800248 // bpf_skb_change_proto() invalidates all pointers - reload them.
249 data = (void*)(long)skb->data;
250 data_end = (void*)(long)skb->data_end;
251
252 // I cannot think of any valid way for this error condition to trigger, however I do
253 // believe the explicit check is required to keep the in kernel ebpf verifier happy.
254 if (data + l2_header_size + sizeof(struct iphdr) > data_end) return TC_ACT_SHOT;
255
256 if (is_ethernet) {
257 struct ethhdr* new_eth = data;
258
259 // Copy over the updated ethernet header
260 *new_eth = eth2;
261
262 // Copy over the new ipv4 header.
263 *(struct iphdr*)(new_eth + 1) = ip;
264 } else {
265 // Copy over the new ipv4 header without an ethernet header.
266 *(struct iphdr*)data = ip;
267 }
268
269 // Redirect, possibly back to same interface, so tcpdump sees packet twice.
270 if (v->oif) return bpf_redirect(v->oif, BPF_F_INGRESS);
271
272 // Just let it through, tcpdump will not see IPv4 packet.
273 return TC_ACT_PIPE;
274}
275
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000276DEFINE_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 -0800277(struct __sk_buff* skb) {
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000278 return nat64(skb, ETHER, KVER_4_14);
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800279}
280
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000281DEFINE_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 -0800282(struct __sk_buff* skb) {
Maciej Żenczykowski9b239522023-01-03 14:59:54 +0000283 return nat64(skb, ETHER, KVER_NONE);
284}
285
286DEFINE_BPF_PROG_KVER("schedcls/ingress6/clat_rawip$4_14", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip_4_14, KVER_4_14)
287(struct __sk_buff* skb) {
288 return nat64(skb, RAWIP, KVER_4_14);
289}
290
291DEFINE_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)
292(struct __sk_buff* skb) {
293 return nat64(skb, RAWIP, KVER_NONE);
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800294}
295
296DEFINE_BPF_MAP_GRW(clat_egress4_map, HASH, ClatEgress4Key, ClatEgress4Value, 16, AID_SYSTEM)
297
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800298DEFINE_BPF_PROG("schedcls/egress4/clat_rawip", AID_ROOT, AID_SYSTEM, sched_cls_egress4_clat_rawip)
299(struct __sk_buff* skb) {
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -0700300 // Must be meta-ethernet IPv4 frame
301 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_PIPE;
302
303 // Possibly not needed, but for consistency with nat64 up above
Maciej Żenczykowski824fb292022-04-11 23:29:46 -0700304 try_make_writable(skb, sizeof(struct iphdr));
Maciej Żenczykowski4b1e0f82022-04-07 16:53:02 -0700305
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800306 void* data = (void*)(long)skb->data;
307 const void* data_end = (void*)(long)skb->data_end;
308 const struct iphdr* const ip4 = data;
309
Maciej Żenczykowski849b3aa2022-01-20 20:58:34 -0800310 // Must have ipv4 header
311 if (data + sizeof(*ip4) > data_end) return TC_ACT_PIPE;
312
313 // IP version must be 4
314 if (ip4->version != 4) return TC_ACT_PIPE;
315
316 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
317 if (ip4->ihl != 5) return TC_ACT_PIPE;
318
319 // Calculate the IPv4 one's complement checksum of the IPv4 header.
320 __wsum sum4 = 0;
321 for (int i = 0; i < sizeof(*ip4) / sizeof(__u16); ++i) {
322 sum4 += ((__u16*)ip4)[i];
323 }
324 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
325 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
326 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
327 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
328 if (sum4 != 0xFFFF) return TC_ACT_PIPE;
329
330 // Minimum IPv4 total length is the size of the header
331 if (ntohs(ip4->tot_len) < sizeof(*ip4)) return TC_ACT_PIPE;
332
333 // We are incapable of dealing with IPv4 fragments
334 if (ip4->frag_off & ~htons(IP_DF)) return TC_ACT_PIPE;
335
336 switch (ip4->protocol) {
337 case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6
338 case IPPROTO_GRE: // address means there is no need to update their checksums.
339 case IPPROTO_ESP: // We do not need to bother looking at GRE/ESP headers,
340 break; // since there is never a checksum to update.
341
342 case IPPROTO_UDP: // See above comment, but must also have UDP header...
343 if (data + sizeof(*ip4) + sizeof(struct udphdr) > data_end) return TC_ACT_PIPE;
344 const struct udphdr* uh = (const struct udphdr*)(ip4 + 1);
345 // If IPv4/UDP checksum is 0 then fallback to clatd so it can calculate the
346 // checksum. Otherwise the network or more likely the NAT64 gateway might
347 // drop the packet because in most cases IPv6/UDP packets with a zero checksum
348 // are invalid. See RFC 6935. TODO: calculate checksum via bpf_csum_diff()
349 if (!uh->check) return TC_ACT_PIPE;
350 break;
351
352 default: // do not know how to handle anything else
353 return TC_ACT_PIPE;
354 }
355
356 ClatEgress4Key k = {
357 .iif = skb->ifindex,
358 .local4.s_addr = ip4->saddr,
359 };
360
361 ClatEgress4Value* v = bpf_clat_egress4_map_lookup_elem(&k);
362
363 if (!v) return TC_ACT_PIPE;
364
365 // Translating without redirecting doesn't make sense.
366 if (!v->oif) return TC_ACT_PIPE;
367
368 // This implementation is currently limited to rawip.
369 if (v->oifIsEthernet) return TC_ACT_PIPE;
370
371 struct ipv6hdr ip6 = {
372 .version = 6, // __u8:4
373 .priority = ip4->tos >> 4, // __u8:4
374 .flow_lbl = {(ip4->tos & 0xF) << 4, 0, 0}, // __u8[3]
375 .payload_len = htons(ntohs(ip4->tot_len) - 20), // __be16
376 .nexthdr = ip4->protocol, // __u8
377 .hop_limit = ip4->ttl, // __u8
378 .saddr = v->local6, // struct in6_addr
379 .daddr = v->pfx96, // struct in6_addr
380 };
381 ip6.daddr.in6_u.u6_addr32[3] = ip4->daddr;
382
383 // Calculate the IPv6 16-bit one's complement checksum of the IPv6 header.
384 __wsum sum6 = 0;
385 // We'll end up with a non-zero sum due to ip6.version == 6
386 for (int i = 0; i < sizeof(ip6) / sizeof(__u16); ++i) {
387 sum6 += ((__u16*)&ip6)[i];
388 }
389
390 // Note that there is no L4 checksum update: we are relying on the checksum neutrality
391 // of the ipv6 address chosen by netd's ClatdController.
392
393 // Packet mutations begin - point of no return, but if this first modification fails
394 // the packet is probably still pristine, so let clatd handle it.
395 if (bpf_skb_change_proto(skb, htons(ETH_P_IPV6), 0)) return TC_ACT_PIPE;
396
397 // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet.
398 //
399 // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload,
400 // thus we need to subtract out the ipv4 header's sum, and add in the ipv6 header's sum.
401 // However, we've already verified the ipv4 checksum is correct and thus 0.
402 // Thus we only need to add the ipv6 header's sum.
403 //
404 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
405 // (-ENOTSUPP) if it isn't. So we just ignore the return code (see above for more details).
406 bpf_csum_update(skb, sum6);
407
408 // bpf_skb_change_proto() invalidates all pointers - reload them.
409 data = (void*)(long)skb->data;
410 data_end = (void*)(long)skb->data_end;
411
412 // I cannot think of any valid way for this error condition to trigger, however I do
413 // believe the explicit check is required to keep the in kernel ebpf verifier happy.
414 if (data + sizeof(ip6) > data_end) return TC_ACT_SHOT;
415
416 // Copy over the new ipv6 header without an ethernet header.
417 *(struct ipv6hdr*)data = ip6;
418
419 // Redirect to non v4-* interface. Tcpdump only sees packet after this redirect.
420 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
421}
422
423LICENSE("Apache 2.0");
Maciej Żenczykowskic41e35d2022-08-04 13:58:46 +0000424CRITICAL("Connectivity");