blob: 6512ef426aa76d656c12e3ece864f208d5d32e51 [file] [log] [blame]
Lorenzo Colitti734b14e2021-02-05 23:56:09 +09001/*
2 * Copyright (C) 2021 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#pragma once
18
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090019#include <linux/if.h>
20#include <linux/if_ether.h>
21#include <linux/in.h>
22#include <linux/in6.h>
23
Lorenzo Colitti734b14e2021-02-05 23:56:09 +090024// Common definitions for BPF code in the tethering mainline module.
25// These definitions are available to:
26// - The BPF programs in Tethering/bpf_progs/
Ken Chenbb57fa92021-10-22 00:49:13 +080027// - JNI code that depends on the bpf_connectivity_headers library.
Lorenzo Colittib81584d2021-02-06 00:00:58 +090028
Maciej Żenczykowskib82bf652022-08-10 19:28:16 +000029#define BPF_TETHER_ERRORS \
30 ERR(INVALID_IPV4_VERSION) \
31 ERR(INVALID_IPV6_VERSION) \
32 ERR(LOW_TTL) \
33 ERR(INVALID_TCP_HEADER) \
34 ERR(TCP_CONTROL_PACKET) \
35 ERR(NON_GLOBAL_SRC) \
36 ERR(NON_GLOBAL_DST) \
37 ERR(LOCAL_SRC_DST) \
38 ERR(NO_STATS_ENTRY) \
39 ERR(NO_LIMIT_ENTRY) \
40 ERR(BELOW_IPV4_MTU) \
41 ERR(BELOW_IPV6_MTU) \
42 ERR(LIMIT_REACHED) \
43 ERR(CHANGE_HEAD_FAILED) \
44 ERR(TOO_SHORT) \
45 ERR(HAS_IP_OPTIONS) \
46 ERR(IS_IP_FRAG) \
47 ERR(CHECKSUM) \
48 ERR(NON_TCP_UDP) \
49 ERR(NON_TCP) \
50 ERR(SHORT_L4_HEADER) \
51 ERR(SHORT_TCP_HEADER) \
52 ERR(SHORT_UDP_HEADER) \
53 ERR(UDP_CSUM_ZERO) \
54 ERR(TRUNCATED_IPV4) \
Lorenzo Colittib81584d2021-02-06 00:00:58 +090055 ERR(_MAX)
56
57#define ERR(x) BPF_TETHER_ERR_ ##x,
58enum {
59 BPF_TETHER_ERRORS
60};
61#undef ERR
62
63#define ERR(x) #x,
64static const char *bpf_tether_errors[] = {
65 BPF_TETHER_ERRORS
66};
67#undef ERR
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090068
69// This header file is shared by eBPF kernel programs (C) and netd (C++) and
70// some of the maps are also accessed directly from Java mainline module code.
71//
72// Hence: explicitly pad all relevant structures and assert that their size
73// is the sum of the sizes of their fields.
74#define STRUCT_SIZE(name, size) _Static_assert(sizeof(name) == (size), "Incorrect struct size.")
75
76
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090077typedef uint32_t TetherStatsKey; // upstream ifindex
78
79typedef struct {
80 uint64_t rxPackets;
81 uint64_t rxBytes;
82 uint64_t rxErrors;
83 uint64_t txPackets;
84 uint64_t txBytes;
85 uint64_t txErrors;
86} TetherStatsValue;
87STRUCT_SIZE(TetherStatsValue, 6 * 8); // 48
88
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090089typedef uint32_t TetherLimitKey; // upstream ifindex
90typedef uint64_t TetherLimitValue; // in bytes
91
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090092// For now tethering offload only needs to support downstreams that use 6-byte MAC addresses,
93// because all downstream types that are currently supported (WiFi, USB, Bluetooth and
94// Ethernet) have 6-byte MAC addresses.
95
96typedef struct {
Hungming Chen9b8c6062021-03-18 18:35:54 +080097 uint32_t iif; // The input interface index
98 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
99 uint8_t zero[2]; // zero pad for 8 byte alignment
100 struct in6_addr neigh6; // The destination IPv6 address
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900101} TetherDownstream6Key;
Hungming Chen9b8c6062021-03-18 18:35:54 +0800102STRUCT_SIZE(TetherDownstream6Key, 4 + 6 + 2 + 16); // 28
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900103
104typedef struct {
105 uint32_t oif; // The output interface to redirect to
106 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
107 uint16_t pmtu; // The maximum L3 output path/route mtu
108} Tether6Value;
109STRUCT_SIZE(Tether6Value, 4 + 14 + 2); // 20
110
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900111typedef struct {
112 uint32_t iif; // The input interface index
113 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
114 uint16_t l4Proto; // IPPROTO_TCP/UDP/...
115 struct in6_addr src6; // source &
116 struct in6_addr dst6; // destination IPv6 addresses
117 __be16 srcPort; // source &
118 __be16 dstPort; // destination tcp/udp/... ports
119} TetherDownstream64Key;
120STRUCT_SIZE(TetherDownstream64Key, 4 + 6 + 2 + 16 + 16 + 2 + 2); // 48
121
122typedef struct {
123 uint32_t oif; // The output interface to redirect to
124 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
125 uint16_t pmtu; // The maximum L3 output path/route mtu
126 struct in_addr src4; // source &
127 struct in_addr dst4; // destination IPv4 addresses
128 __be16 srcPort; // source &
129 __be16 outPort; // destination tcp/udp/... ports
130 uint64_t lastUsed; // Kernel updates on each use with bpf_ktime_get_boot_ns()
131} TetherDownstream64Value;
132STRUCT_SIZE(TetherDownstream64Value, 4 + 14 + 2 + 4 + 4 + 2 + 2 + 8); // 40
133
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900134typedef struct {
Hungming Chen9b8c6062021-03-18 18:35:54 +0800135 uint32_t iif; // The input interface index
136 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
137 uint8_t zero[2]; // zero pad for 8 byte alignment
138 // TODO: extend this to include src ip /64 subnet
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900139} TetherUpstream6Key;
Hungming Chen9b8c6062021-03-18 18:35:54 +0800140STRUCT_SIZE(TetherUpstream6Key, 12);
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900141
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900142typedef struct {
143 uint32_t iif; // The input interface index
144 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
145 uint16_t l4Proto; // IPPROTO_TCP/UDP/...
146 struct in_addr src4; // source &
147 struct in_addr dst4; // destination IPv4 addresses
148 __be16 srcPort; // source &
149 __be16 dstPort; // destination TCP/UDP/... ports
150} Tether4Key;
151STRUCT_SIZE(Tether4Key, 4 + 6 + 2 + 4 + 4 + 2 + 2); // 24
152
153typedef struct {
154 uint32_t oif; // The output interface to redirect to
155 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
156 uint16_t pmtu; // Maximum L3 output path/route mtu
157 struct in6_addr src46; // source & (always IPv4 mapped for downstream)
158 struct in6_addr dst46; // destination IP addresses (may be IPv4 mapped or IPv6 for upstream)
159 __be16 srcPort; // source &
160 __be16 dstPort; // destination tcp/udp/... ports
161 uint64_t last_used; // Kernel updates on each use with bpf_ktime_get_boot_ns()
162} Tether4Value;
163STRUCT_SIZE(Tether4Value, 4 + 14 + 2 + 16 + 16 + 2 + 2 + 8); // 64
164
Lorenzo Colitti56be03e2021-02-24 00:10:44 +0900165#undef STRUCT_SIZE