blob: 53665f6ccb4648bb14ca2c7a44bd11aa78581d77 [file] [log] [blame]
Maciej Żenczykowskib70da762019-01-28 15:20:48 -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 Żenczykowskieec72082020-02-04 23:29:41 -080017#include "OffloadUtils.h"
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080018
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070019#include <arpa/inet.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080020#include <linux/if.h>
Hungming Chenec203382020-02-13 19:15:33 +080021#include <linux/if_arp.h>
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080022#include <linux/netlink.h>
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070023#include <linux/pkt_cls.h>
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080024#include <linux/pkt_sched.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080025#include <sys/ioctl.h>
26#include <sys/socket.h>
27#include <sys/types.h>
28#include <unistd.h>
29
Maciej Żenczykowskieec72082020-02-04 23:29:41 -080030#define LOG_TAG "OffloadUtils"
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080031#include <log/log.h>
32
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080033#include "NetlinkCommands.h"
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080034#include "android-base/unique_fd.h"
35
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080036namespace android {
37namespace net {
38
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -080039using std::max;
40
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080041int hardwareAddressType(const std::string& interface) {
42 base::unique_fd ufd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
43
44 if (ufd < 0) {
45 const int err = errno;
46 ALOGE("socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)");
47 return -err;
48 };
49
50 struct ifreq ifr = {};
51 // We use strncpy() instead of strlcpy() since kernel has to be able
52 // to handle non-zero terminated junk passed in by userspace anyway,
53 // and this way too long interface names (more than IFNAMSIZ-1 = 15
54 // characters plus terminating NULL) will not get truncated to 15
55 // characters and zero-terminated and thus potentially erroneously
56 // match a truncated interface if one were to exist.
57 strncpy(ifr.ifr_name, interface.c_str(), sizeof(ifr.ifr_name));
58
59 if (ioctl(ufd, SIOCGIFHWADDR, &ifr, sizeof(ifr))) return -errno;
60
61 return ifr.ifr_hwaddr.sa_family;
62}
63
Hungming Chenec203382020-02-13 19:15:33 +080064base::Result<bool> isEthernet(const std::string& interface) {
65 int rv = hardwareAddressType(interface);
66 if (rv < 0) {
67 errno = -rv;
68 return ErrnoErrorf("Get hardware address type of interface {} failed", interface);
69 }
70
71 switch (rv) {
72 case ARPHRD_ETHER:
73 return true;
74 case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519
75 case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet
76 return false;
77 default:
78 errno = EAFNOSUPPORT; // Address family not supported
79 return ErrnoErrorf("Unknown hardware address type {} on interface {}", rv, interface);
80 }
81}
82
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080083// TODO: use //system/netd/server/NetlinkCommands.cpp:openNetlinkSocket(protocol)
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080084// and //system/netd/server/SockDiag.cpp:checkError(fd)
85static int sendAndProcessNetlinkResponse(const void* req, int len) {
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080086 base::unique_fd fd(socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE));
87 if (fd == -1) {
88 const int err = errno;
89 ALOGE("socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)");
90 return -err;
91 }
92
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -080093 static constexpr int on = 1;
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080094 int rv = setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &on, sizeof(on));
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080095 if (rv) ALOGE("setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, %d)", on);
96
97 // this is needed to get sane strace netlink parsing, it allocates the pid
98 rv = bind(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
99 if (rv) {
100 const int err = errno;
101 ALOGE("bind(fd, {AF_NETLINK, 0, 0})");
102 return -err;
103 }
104
105 // we do not want to receive messages from anyone besides the kernel
106 rv = connect(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
107 if (rv) {
108 const int err = errno;
109 ALOGE("connect(fd, {AF_NETLINK, 0, 0})");
110 return -err;
111 }
112
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800113 rv = send(fd, req, len, 0);
Maciej Żenczykowski841ec722020-02-12 03:52:28 -0800114 if (rv == -1) return -errno;
115 if (rv != len) return -EMSGSIZE;
116
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -0800117 struct {
118 nlmsghdr h;
119 nlmsgerr e;
120 char buf[256];
121 } resp = {};
122
Maciej Żenczykowski841ec722020-02-12 03:52:28 -0800123 rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC);
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -0800124
125 if (rv == -1) {
126 const int err = errno;
127 ALOGE("recv() failed");
128 return -err;
129 }
130
131 if (rv < (int)NLMSG_SPACE(sizeof(struct nlmsgerr))) {
132 ALOGE("recv() returned short packet: %d", rv);
133 return -EMSGSIZE;
134 }
135
136 if (resp.h.nlmsg_len != (unsigned)rv) {
137 ALOGE("recv() returned invalid header length: %d != %d", resp.h.nlmsg_len, rv);
138 return -EBADMSG;
139 }
140
141 if (resp.h.nlmsg_type != NLMSG_ERROR) {
142 ALOGE("recv() did not return NLMSG_ERROR message: %d", resp.h.nlmsg_type);
143 return -EBADMSG;
144 }
145
146 return resp.e.error; // returns 0 on success
147}
148
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800149// ADD: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_EXCL|NLM_F_CREATE
150// REPLACE: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_CREATE|NLM_F_REPLACE
151// DEL: nlMsgType=RTM_DELQDISC nlMsgFlags=0
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800152int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags) {
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800153 // This is the name of the qdisc we are attaching.
154 // Some hoop jumping to make this compile time constant with known size,
155 // so that the structure declaration is well defined at compile time.
156#define CLSACT "clsact"
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800157 // sizeof() includes the terminating NULL
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800158 static constexpr size_t ASCIIZ_LEN_CLSACT = sizeof(CLSACT);
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800159
160 const struct {
161 nlmsghdr n;
162 tcmsg t;
163 struct {
164 nlattr attr;
165 char str[NLMSG_ALIGN(ASCIIZ_LEN_CLSACT)];
166 } kind;
167 } req = {
168 .n =
169 {
170 .nlmsg_len = sizeof(req),
171 .nlmsg_type = nlMsgType,
172 .nlmsg_flags = static_cast<__u16>(NETLINK_REQUEST_FLAGS | nlMsgFlags),
173 },
174 .t =
175 {
176 .tcm_family = AF_UNSPEC,
177 .tcm_ifindex = ifIndex,
178 .tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0),
179 .tcm_parent = TC_H_CLSACT,
180 },
181 .kind =
182 {
183 .attr =
184 {
185 .nla_len = NLA_HDRLEN + ASCIIZ_LEN_CLSACT,
186 .nla_type = TCA_KIND,
187 },
188 .str = CLSACT,
189 },
190 };
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800191#undef CLSACT
192
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800193 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800194}
195
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800196// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
197// direct-action
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800198int tcFilterAddDevBpf(int ifIndex, bool ingress, uint16_t prio, uint16_t proto, int bpfFd,
199 bool ethernet) {
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700200 // This is the name of the filter we're attaching (ie. this is the 'bpf'
201 // packet classifier enabled by kernel config option CONFIG_NET_CLS_BPF.
202 //
203 // We go through some hoops in order to make this compile time constants
204 // so that we can define the struct further down the function with the
205 // field for this sized correctly already during the build.
206#define BPF "bpf"
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700207 // sizeof() includes the terminating NULL
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800208 static constexpr size_t ASCIIZ_LEN_BPF = sizeof(BPF);
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700209
210 // This is to replicate program name suffix used by 'tc' Linux cli
211 // when it attaches programs.
212#define FSOBJ_SUFFIX ":[*fsobj]"
213
214 // This macro expands (from header files) to:
215 // prog_clatd_schedcls_ingress_clat_rawip:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800216 // and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700217 // (also compatible with anything that has 0 size L2 header)
Maciej Żenczykowski241e1d92020-02-16 17:02:50 -0800218 static constexpr char name_clat_rx_rawip[] = CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700219
220 // This macro expands (from header files) to:
221 // prog_clatd_schedcls_ingress_clat_ether:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800222 // and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700223 // (also compatible with anything that has standard ethernet header)
Maciej Żenczykowski241e1d92020-02-16 17:02:50 -0800224 static constexpr char name_clat_rx_ether[] = CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700225
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800226 // This macro expands (from header files) to:
227 // prog_clatd_schedcls_egress_clat_rawip:[*fsobj]
228 // and is the name of the pinned egress ebpf program for ARPHRD_RAWIP interfaces.
229 // (also compatible with anything that has 0 size L2 header)
Maciej Żenczykowski241e1d92020-02-16 17:02:50 -0800230 static constexpr char name_clat_tx_rawip[] = CLAT_EGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800231
232 // This macro expands (from header files) to:
233 // prog_clatd_schedcls_egress_clat_ether:[*fsobj]
234 // and is the name of the pinned egress ebpf program for ARPHRD_ETHER interfaces.
235 // (also compatible with anything that has standard ethernet header)
Maciej Żenczykowski241e1d92020-02-16 17:02:50 -0800236 static constexpr char name_clat_tx_ether[] = CLAT_EGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800237
Maciej Żenczykowski58efd782020-02-16 17:15:26 -0800238 // This macro expands (from header files) to:
239 // prog_offload_schedcls_ingress_tether_rawip:[*fsobj]
240 // and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces.
241 // (also compatible with anything that has 0 size L2 header)
242 static constexpr char name_tether_rawip[] = TETHER_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
243
244 // This macro expands (from header files) to:
245 // prog_offload_schedcls_ingress_tether_ether:[*fsobj]
246 // and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces.
247 // (also compatible with anything that has standard ethernet header)
248 static constexpr char name_tether_ether[] = TETHER_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
249
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800250#undef FSOBJ_SUFFIX
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800251
252 // The actual name we'll use is determined at run time via 'ethernet' and 'ingress'
253 // booleans. We need to compile time allocate enough space in the struct
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700254 // hence this macro magic to make sure we have enough space for either
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800255 // possibility. In practice some of these are actually the same size.
Maciej Żenczykowski58efd782020-02-16 17:15:26 -0800256 static constexpr size_t ASCIIZ_MAXLEN_NAME = max({
257 sizeof(name_clat_rx_rawip),
258 sizeof(name_clat_rx_ether),
259 sizeof(name_clat_tx_rawip),
260 sizeof(name_clat_tx_ether),
261 sizeof(name_tether_rawip),
262 sizeof(name_tether_ether),
263 });
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700264
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800265 // These are not compile time constants: 'name' is used in strncpy below
Maciej Żenczykowski241e1d92020-02-16 17:02:50 -0800266 const char* const name_clat_rx = ethernet ? name_clat_rx_ether : name_clat_rx_rawip;
267 const char* const name_clat_tx = ethernet ? name_clat_tx_ether : name_clat_tx_rawip;
Maciej Żenczykowski58efd782020-02-16 17:15:26 -0800268 const char* const name_clat = ingress ? name_clat_rx : name_clat_tx;
269 const char* const name_tether = ethernet ? name_tether_ether : name_tether_rawip;
270 const char* const name = (prio == PRIO_TETHER) ? name_tether : name_clat;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700271
272 struct {
273 nlmsghdr n;
274 tcmsg t;
275 struct {
276 nlattr attr;
277 char str[NLMSG_ALIGN(ASCIIZ_LEN_BPF)];
278 } kind;
279 struct {
280 nlattr attr;
281 struct {
282 nlattr attr;
283 __u32 u32;
284 } fd;
285 struct {
286 nlattr attr;
287 char str[NLMSG_ALIGN(ASCIIZ_MAXLEN_NAME)];
288 } name;
289 struct {
290 nlattr attr;
291 __u32 u32;
292 } flags;
293 } options;
294 } req = {
295 .n =
296 {
297 .nlmsg_len = sizeof(req),
298 .nlmsg_type = RTM_NEWTFILTER,
299 .nlmsg_flags = NETLINK_REQUEST_FLAGS | NLM_F_EXCL | NLM_F_CREATE,
300 },
301 .t =
302 {
303 .tcm_family = AF_UNSPEC,
304 .tcm_ifindex = ifIndex,
305 .tcm_handle = TC_H_UNSPEC,
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800306 .tcm_parent = TC_H_MAKE(TC_H_CLSACT,
307 ingress ? TC_H_MIN_INGRESS : TC_H_MIN_EGRESS),
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800308 .tcm_info = static_cast<__u32>((prio << 16) | htons(proto)),
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700309 },
310 .kind =
311 {
312 .attr =
313 {
314 .nla_len = sizeof(req.kind),
315 .nla_type = TCA_KIND,
316 },
317 .str = BPF,
318 },
319 .options =
320 {
321 .attr =
322 {
323 .nla_len = sizeof(req.options),
324 .nla_type = TCA_OPTIONS,
325 },
326 .fd =
327 {
328 .attr =
329 {
330 .nla_len = sizeof(req.options.fd),
331 .nla_type = TCA_BPF_FD,
332 },
333 .u32 = static_cast<__u32>(bpfFd),
334 },
335 .name =
336 {
337 .attr =
338 {
339 .nla_len = sizeof(req.options.name),
340 .nla_type = TCA_BPF_NAME,
341 },
342 // Visible via 'tc filter show', but
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800343 // is overwritten by strncpy below
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700344 .str = "placeholder",
345 },
346 .flags =
347 {
348 .attr =
349 {
350 .nla_len = sizeof(req.options.flags),
351 .nla_type = TCA_BPF_FLAGS,
352 },
353 .u32 = TCA_BPF_FLAG_ACT_DIRECT,
354 },
355 },
356 };
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700357#undef BPF
358
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800359 strncpy(req.options.name.str, name, sizeof(req.options.name.str));
360
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800361 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700362}
363
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800364// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800365int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto) {
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800366 const struct {
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800367 nlmsghdr n;
368 tcmsg t;
369 } req = {
370 .n =
371 {
372 .nlmsg_len = sizeof(req),
373 .nlmsg_type = RTM_DELTFILTER,
374 .nlmsg_flags = NETLINK_REQUEST_FLAGS,
375 },
376 .t =
377 {
378 .tcm_family = AF_UNSPEC,
379 .tcm_ifindex = ifIndex,
380 .tcm_handle = TC_H_UNSPEC,
381 .tcm_parent = TC_H_MAKE(TC_H_CLSACT,
382 ingress ? TC_H_MIN_INGRESS : TC_H_MIN_EGRESS),
383 .tcm_info = static_cast<__u32>((prio << 16) | htons(proto)),
384 },
385 };
386
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800387 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800388}
389
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800390} // namespace net
391} // namespace android