blob: 4e80ef4aac3e6a6fdae345919d012312c25c859f [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>
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080021#include <linux/netlink.h>
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070022#include <linux/pkt_cls.h>
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080023#include <linux/pkt_sched.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080024#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <sys/types.h>
27#include <unistd.h>
28
Maciej Żenczykowskieec72082020-02-04 23:29:41 -080029#define LOG_TAG "OffloadUtils"
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080030#include <log/log.h>
31
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080032#include "NetlinkCommands.h"
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080033#include "android-base/unique_fd.h"
34
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080035namespace android {
36namespace net {
37
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -080038using std::max;
39
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080040int hardwareAddressType(const std::string& interface) {
41 base::unique_fd ufd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
42
43 if (ufd < 0) {
44 const int err = errno;
45 ALOGE("socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)");
46 return -err;
47 };
48
49 struct ifreq ifr = {};
50 // We use strncpy() instead of strlcpy() since kernel has to be able
51 // to handle non-zero terminated junk passed in by userspace anyway,
52 // and this way too long interface names (more than IFNAMSIZ-1 = 15
53 // characters plus terminating NULL) will not get truncated to 15
54 // characters and zero-terminated and thus potentially erroneously
55 // match a truncated interface if one were to exist.
56 strncpy(ifr.ifr_name, interface.c_str(), sizeof(ifr.ifr_name));
57
58 if (ioctl(ufd, SIOCGIFHWADDR, &ifr, sizeof(ifr))) return -errno;
59
60 return ifr.ifr_hwaddr.sa_family;
61}
62
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080063// TODO: use //system/netd/server/NetlinkCommands.cpp:openNetlinkSocket(protocol)
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080064// and //system/netd/server/SockDiag.cpp:checkError(fd)
65static int sendAndProcessNetlinkResponse(const void* req, int len) {
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080066 base::unique_fd fd(socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE));
67 if (fd == -1) {
68 const int err = errno;
69 ALOGE("socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)");
70 return -err;
71 }
72
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -080073 static constexpr int on = 1;
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080074 int rv = setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &on, sizeof(on));
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080075 if (rv) ALOGE("setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, %d)", on);
76
77 // this is needed to get sane strace netlink parsing, it allocates the pid
78 rv = bind(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
79 if (rv) {
80 const int err = errno;
81 ALOGE("bind(fd, {AF_NETLINK, 0, 0})");
82 return -err;
83 }
84
85 // we do not want to receive messages from anyone besides the kernel
86 rv = connect(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
87 if (rv) {
88 const int err = errno;
89 ALOGE("connect(fd, {AF_NETLINK, 0, 0})");
90 return -err;
91 }
92
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080093 rv = send(fd, req, len, 0);
Maciej Żenczykowski841ec722020-02-12 03:52:28 -080094 if (rv == -1) return -errno;
95 if (rv != len) return -EMSGSIZE;
96
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -080097 struct {
98 nlmsghdr h;
99 nlmsgerr e;
100 char buf[256];
101 } resp = {};
102
Maciej Żenczykowski841ec722020-02-12 03:52:28 -0800103 rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC);
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -0800104
105 if (rv == -1) {
106 const int err = errno;
107 ALOGE("recv() failed");
108 return -err;
109 }
110
111 if (rv < (int)NLMSG_SPACE(sizeof(struct nlmsgerr))) {
112 ALOGE("recv() returned short packet: %d", rv);
113 return -EMSGSIZE;
114 }
115
116 if (resp.h.nlmsg_len != (unsigned)rv) {
117 ALOGE("recv() returned invalid header length: %d != %d", resp.h.nlmsg_len, rv);
118 return -EBADMSG;
119 }
120
121 if (resp.h.nlmsg_type != NLMSG_ERROR) {
122 ALOGE("recv() did not return NLMSG_ERROR message: %d", resp.h.nlmsg_type);
123 return -EBADMSG;
124 }
125
126 return resp.e.error; // returns 0 on success
127}
128
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800129// ADD: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_EXCL|NLM_F_CREATE
130// REPLACE: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_CREATE|NLM_F_REPLACE
131// DEL: nlMsgType=RTM_DELQDISC nlMsgFlags=0
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800132int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags) {
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800133 // This is the name of the qdisc we are attaching.
134 // Some hoop jumping to make this compile time constant with known size,
135 // so that the structure declaration is well defined at compile time.
136#define CLSACT "clsact"
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800137 // sizeof() includes the terminating NULL
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800138 static constexpr size_t ASCIIZ_LEN_CLSACT = sizeof(CLSACT);
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800139
140 const struct {
141 nlmsghdr n;
142 tcmsg t;
143 struct {
144 nlattr attr;
145 char str[NLMSG_ALIGN(ASCIIZ_LEN_CLSACT)];
146 } kind;
147 } req = {
148 .n =
149 {
150 .nlmsg_len = sizeof(req),
151 .nlmsg_type = nlMsgType,
152 .nlmsg_flags = static_cast<__u16>(NETLINK_REQUEST_FLAGS | nlMsgFlags),
153 },
154 .t =
155 {
156 .tcm_family = AF_UNSPEC,
157 .tcm_ifindex = ifIndex,
158 .tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0),
159 .tcm_parent = TC_H_CLSACT,
160 },
161 .kind =
162 {
163 .attr =
164 {
165 .nla_len = NLA_HDRLEN + ASCIIZ_LEN_CLSACT,
166 .nla_type = TCA_KIND,
167 },
168 .str = CLSACT,
169 },
170 };
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800171#undef CLSACT
172
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800173 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800174}
175
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800176// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
177// direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800178int tcFilterAddDevBpf(int ifIndex, int bpfFd, bool ethernet, bool ingress, bool ipv6) {
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700179 // The priority doesn't matter until we actually start attaching multiple
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800180 // things to the same interface's in/egress point.
181 const __u32 prio = 1;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700182
183 // This is the name of the filter we're attaching (ie. this is the 'bpf'
184 // packet classifier enabled by kernel config option CONFIG_NET_CLS_BPF.
185 //
186 // We go through some hoops in order to make this compile time constants
187 // so that we can define the struct further down the function with the
188 // field for this sized correctly already during the build.
189#define BPF "bpf"
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700190 // sizeof() includes the terminating NULL
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800191 static constexpr size_t ASCIIZ_LEN_BPF = sizeof(BPF);
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700192
193 // This is to replicate program name suffix used by 'tc' Linux cli
194 // when it attaches programs.
195#define FSOBJ_SUFFIX ":[*fsobj]"
196
197 // This macro expands (from header files) to:
198 // prog_clatd_schedcls_ingress_clat_rawip:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800199 // and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700200 // (also compatible with anything that has 0 size L2 header)
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800201 static constexpr char name_rx_rawip[] = CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700202
203 // This macro expands (from header files) to:
204 // prog_clatd_schedcls_ingress_clat_ether:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800205 // and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700206 // (also compatible with anything that has standard ethernet header)
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800207 static constexpr char name_rx_ether[] = CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700208
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800209 // This macro expands (from header files) to:
210 // prog_clatd_schedcls_egress_clat_rawip:[*fsobj]
211 // and is the name of the pinned egress ebpf program for ARPHRD_RAWIP interfaces.
212 // (also compatible with anything that has 0 size L2 header)
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800213 static constexpr char name_tx_rawip[] = CLAT_EGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800214
215 // This macro expands (from header files) to:
216 // prog_clatd_schedcls_egress_clat_ether:[*fsobj]
217 // and is the name of the pinned egress ebpf program for ARPHRD_ETHER interfaces.
218 // (also compatible with anything that has standard ethernet header)
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800219 static constexpr char name_tx_ether[] = CLAT_EGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
220
221#undef FSOBJ_SUFFIX
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800222
223 // The actual name we'll use is determined at run time via 'ethernet' and 'ingress'
224 // booleans. We need to compile time allocate enough space in the struct
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700225 // hence this macro magic to make sure we have enough space for either
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800226 // possibility. In practice some of these are actually the same size.
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800227 static constexpr size_t ASCIIZ_MAXLEN_NAME_RX =
228 max(sizeof(name_rx_rawip), sizeof(name_rx_ether));
229 static constexpr size_t ASCIIZ_MAXLEN_NAME_TX =
230 max(sizeof(name_tx_rawip), sizeof(name_tx_ether));
231 static constexpr size_t ASCIIZ_MAXLEN_NAME = max(ASCIIZ_MAXLEN_NAME_RX, ASCIIZ_MAXLEN_NAME_TX);
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700232
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800233 // These are not compile time constants: 'name' is used in strncpy below
234 const char* const name_rx = ethernet ? name_rx_ether : name_rx_rawip;
235 const char* const name_tx = ethernet ? name_tx_ether : name_tx_rawip;
236 const char* const name = ingress ? name_rx : name_tx;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700237
238 struct {
239 nlmsghdr n;
240 tcmsg t;
241 struct {
242 nlattr attr;
243 char str[NLMSG_ALIGN(ASCIIZ_LEN_BPF)];
244 } kind;
245 struct {
246 nlattr attr;
247 struct {
248 nlattr attr;
249 __u32 u32;
250 } fd;
251 struct {
252 nlattr attr;
253 char str[NLMSG_ALIGN(ASCIIZ_MAXLEN_NAME)];
254 } name;
255 struct {
256 nlattr attr;
257 __u32 u32;
258 } flags;
259 } options;
260 } req = {
261 .n =
262 {
263 .nlmsg_len = sizeof(req),
264 .nlmsg_type = RTM_NEWTFILTER,
265 .nlmsg_flags = NETLINK_REQUEST_FLAGS | NLM_F_EXCL | NLM_F_CREATE,
266 },
267 .t =
268 {
269 .tcm_family = AF_UNSPEC,
270 .tcm_ifindex = ifIndex,
271 .tcm_handle = TC_H_UNSPEC,
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800272 .tcm_parent = TC_H_MAKE(TC_H_CLSACT,
273 ingress ? TC_H_MIN_INGRESS : TC_H_MIN_EGRESS),
274 .tcm_info = (prio << 16) |
275 (__u32)(ipv6 ? htons(ETH_P_IPV6) : htons(ETH_P_IP)),
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700276 },
277 .kind =
278 {
279 .attr =
280 {
281 .nla_len = sizeof(req.kind),
282 .nla_type = TCA_KIND,
283 },
284 .str = BPF,
285 },
286 .options =
287 {
288 .attr =
289 {
290 .nla_len = sizeof(req.options),
291 .nla_type = TCA_OPTIONS,
292 },
293 .fd =
294 {
295 .attr =
296 {
297 .nla_len = sizeof(req.options.fd),
298 .nla_type = TCA_BPF_FD,
299 },
300 .u32 = static_cast<__u32>(bpfFd),
301 },
302 .name =
303 {
304 .attr =
305 {
306 .nla_len = sizeof(req.options.name),
307 .nla_type = TCA_BPF_NAME,
308 },
309 // Visible via 'tc filter show', but
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800310 // is overwritten by strncpy below
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700311 .str = "placeholder",
312 },
313 .flags =
314 {
315 .attr =
316 {
317 .nla_len = sizeof(req.options.flags),
318 .nla_type = TCA_BPF_FLAGS,
319 },
320 .u32 = TCA_BPF_FLAG_ACT_DIRECT,
321 },
322 },
323 };
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700324#undef BPF
325
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800326 strncpy(req.options.name.str, name, sizeof(req.options.name.str));
327
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800328 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700329}
330
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800331// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800332int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto) {
Maciej Żenczykowski77eabde2020-02-13 15:20:16 -0800333 const struct {
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800334 nlmsghdr n;
335 tcmsg t;
336 } req = {
337 .n =
338 {
339 .nlmsg_len = sizeof(req),
340 .nlmsg_type = RTM_DELTFILTER,
341 .nlmsg_flags = NETLINK_REQUEST_FLAGS,
342 },
343 .t =
344 {
345 .tcm_family = AF_UNSPEC,
346 .tcm_ifindex = ifIndex,
347 .tcm_handle = TC_H_UNSPEC,
348 .tcm_parent = TC_H_MAKE(TC_H_CLSACT,
349 ingress ? TC_H_MIN_INGRESS : TC_H_MIN_EGRESS),
350 .tcm_info = static_cast<__u32>((prio << 16) | htons(proto)),
351 },
352 };
353
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800354 return sendAndProcessNetlinkResponse(&req, sizeof(req));
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800355}
356
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800357} // namespace net
358} // namespace android