blob: 051782fc360f48330489f3801abd833fe8098252 [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
17#include "ClatUtils.h"
18
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070019#include <arpa/inet.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080020#include <errno.h>
21#include <linux/if.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>
25#include <linux/rtnetlink.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080026#include <sys/ioctl.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
30
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080031#define LOG_TAG "ClatUtils"
32#include <log/log.h>
33
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080034#include "NetlinkCommands.h"
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080035#include "android-base/unique_fd.h"
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070036#include "bpf/BpfUtils.h"
37#include "netdbpf/bpf_shared.h"
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080038
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080039namespace android {
40namespace net {
41
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080042int hardwareAddressType(const std::string& interface) {
43 base::unique_fd ufd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
44
45 if (ufd < 0) {
46 const int err = errno;
47 ALOGE("socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)");
48 return -err;
49 };
50
51 struct ifreq ifr = {};
52 // We use strncpy() instead of strlcpy() since kernel has to be able
53 // to handle non-zero terminated junk passed in by userspace anyway,
54 // and this way too long interface names (more than IFNAMSIZ-1 = 15
55 // characters plus terminating NULL) will not get truncated to 15
56 // characters and zero-terminated and thus potentially erroneously
57 // match a truncated interface if one were to exist.
58 strncpy(ifr.ifr_name, interface.c_str(), sizeof(ifr.ifr_name));
59
60 if (ioctl(ufd, SIOCGIFHWADDR, &ifr, sizeof(ifr))) return -errno;
61
62 return ifr.ifr_hwaddr.sa_family;
63}
64
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080065int getClatEgressMapFd(void) {
66 const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0);
67 return (fd == -1) ? -errno : fd;
68}
69
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080070int getClatEgressProgFd(bool with_ethernet_header) {
71 const int fd = bpf::bpfFdGet(
72 with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
73 return (fd == -1) ? -errno : fd;
74}
75
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -070076int getClatIngressMapFd(void) {
77 const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070078 return (fd == -1) ? -errno : fd;
79}
80
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -070081int getClatIngressProgFd(bool with_ethernet_header) {
82 const int fd = bpf::bpfFdGet(
83 with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0);
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080084 return (fd == -1) ? -errno : fd;
85}
86
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080087// TODO: use //system/netd/server/NetlinkCommands.cpp:openNetlinkSocket(protocol)
88int openNetlinkSocket(void) {
89 base::unique_fd fd(socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE));
90 if (fd == -1) {
91 const int err = errno;
92 ALOGE("socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)");
93 return -err;
94 }
95
96 int rv;
97
98 const int on = 1;
99 rv = setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &on, sizeof(on));
100 if (rv) ALOGE("setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, %d)", on);
101
102 // this is needed to get sane strace netlink parsing, it allocates the pid
103 rv = bind(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
104 if (rv) {
105 const int err = errno;
106 ALOGE("bind(fd, {AF_NETLINK, 0, 0})");
107 return -err;
108 }
109
110 // we do not want to receive messages from anyone besides the kernel
111 rv = connect(fd, (const struct sockaddr*)&KERNEL_NLADDR, sizeof(KERNEL_NLADDR));
112 if (rv) {
113 const int err = errno;
114 ALOGE("connect(fd, {AF_NETLINK, 0, 0})");
115 return -err;
116 }
117
118 return fd.release();
119}
120
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -0800121// TODO: merge with //system/netd/server/SockDiag.cpp:checkError(fd)
122int processNetlinkResponse(int fd) {
123 struct {
124 nlmsghdr h;
125 nlmsgerr e;
126 char buf[256];
127 } resp = {};
128
129 const int rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC);
130
131 if (rv == -1) {
132 const int err = errno;
133 ALOGE("recv() failed");
134 return -err;
135 }
136
137 if (rv < (int)NLMSG_SPACE(sizeof(struct nlmsgerr))) {
138 ALOGE("recv() returned short packet: %d", rv);
139 return -EMSGSIZE;
140 }
141
142 if (resp.h.nlmsg_len != (unsigned)rv) {
143 ALOGE("recv() returned invalid header length: %d != %d", resp.h.nlmsg_len, rv);
144 return -EBADMSG;
145 }
146
147 if (resp.h.nlmsg_type != NLMSG_ERROR) {
148 ALOGE("recv() did not return NLMSG_ERROR message: %d", resp.h.nlmsg_type);
149 return -EBADMSG;
150 }
151
152 return resp.e.error; // returns 0 on success
153}
154
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800155// ADD: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_EXCL|NLM_F_CREATE
156// REPLACE: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_CREATE|NLM_F_REPLACE
157// DEL: nlMsgType=RTM_DELQDISC nlMsgFlags=0
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800158static int doTcQdiscClsact(int fd, int ifIndex, __u16 nlMsgType, __u16 nlMsgFlags) {
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800159 // This is the name of the qdisc we are attaching.
160 // Some hoop jumping to make this compile time constant with known size,
161 // so that the structure declaration is well defined at compile time.
162#define CLSACT "clsact"
163 static const char clsact[] = CLSACT;
164 // sizeof() includes the terminating NULL
165#define ASCIIZ_LEN_CLSACT sizeof(clsact)
166
167 const struct {
168 nlmsghdr n;
169 tcmsg t;
170 struct {
171 nlattr attr;
172 char str[NLMSG_ALIGN(ASCIIZ_LEN_CLSACT)];
173 } kind;
174 } req = {
175 .n =
176 {
177 .nlmsg_len = sizeof(req),
178 .nlmsg_type = nlMsgType,
179 .nlmsg_flags = static_cast<__u16>(NETLINK_REQUEST_FLAGS | nlMsgFlags),
180 },
181 .t =
182 {
183 .tcm_family = AF_UNSPEC,
184 .tcm_ifindex = ifIndex,
185 .tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0),
186 .tcm_parent = TC_H_CLSACT,
187 },
188 .kind =
189 {
190 .attr =
191 {
192 .nla_len = NLA_HDRLEN + ASCIIZ_LEN_CLSACT,
193 .nla_type = TCA_KIND,
194 },
195 .str = CLSACT,
196 },
197 };
198#undef ASCIIZ_LEN_CLSACT
199#undef CLSACT
200
201 const int rv = send(fd, &req, sizeof(req), 0);
202 if (rv == -1) return -errno;
203 if (rv != sizeof(req)) return -EMSGSIZE;
204
205 return processNetlinkResponse(fd);
206}
207
208int tcQdiscAddDevClsact(int fd, int ifIndex) {
209 return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
210}
211
212int tcQdiscReplaceDevClsact(int fd, int ifIndex) {
213 return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
214}
215
216int tcQdiscDelDevClsact(int fd, int ifIndex) {
217 return doTcQdiscClsact(fd, ifIndex, RTM_DELQDISC, 0);
218}
219
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800220// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
221// direct-action
222static int tcFilterAddDevBpf(int fd, int ifIndex, int bpfFd, bool ethernet, bool ingress,
223 bool ipv6) {
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700224 // The priority doesn't matter until we actually start attaching multiple
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800225 // things to the same interface's in/egress point.
226 const __u32 prio = 1;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700227
228 // This is the name of the filter we're attaching (ie. this is the 'bpf'
229 // packet classifier enabled by kernel config option CONFIG_NET_CLS_BPF.
230 //
231 // We go through some hoops in order to make this compile time constants
232 // so that we can define the struct further down the function with the
233 // field for this sized correctly already during the build.
234#define BPF "bpf"
235 const char bpf[] = BPF;
236 // sizeof() includes the terminating NULL
237#define ASCIIZ_LEN_BPF sizeof(bpf)
238
239 // This is to replicate program name suffix used by 'tc' Linux cli
240 // when it attaches programs.
241#define FSOBJ_SUFFIX ":[*fsobj]"
242
243 // This macro expands (from header files) to:
244 // prog_clatd_schedcls_ingress_clat_rawip:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800245 // and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700246 // (also compatible with anything that has 0 size L2 header)
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800247#define NAME_RX_RAWIP CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX
248 const char name_rx_rawip[] = NAME_RX_RAWIP;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700249
250 // This macro expands (from header files) to:
251 // prog_clatd_schedcls_ingress_clat_ether:[*fsobj]
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800252 // and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces.
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700253 // (also compatible with anything that has standard ethernet header)
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800254#define NAME_RX_ETHER CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX
255 const char name_rx_ether[] = NAME_RX_ETHER;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700256
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800257 // This macro expands (from header files) to:
258 // prog_clatd_schedcls_egress_clat_rawip:[*fsobj]
259 // and is the name of the pinned egress ebpf program for ARPHRD_RAWIP interfaces.
260 // (also compatible with anything that has 0 size L2 header)
261#define NAME_TX_RAWIP CLAT_EGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX
262 const char name_tx_rawip[] = NAME_TX_RAWIP;
263
264 // This macro expands (from header files) to:
265 // prog_clatd_schedcls_egress_clat_ether:[*fsobj]
266 // and is the name of the pinned egress ebpf program for ARPHRD_ETHER interfaces.
267 // (also compatible with anything that has standard ethernet header)
268#define NAME_TX_ETHER CLAT_EGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX
269 const char name_tx_ether[] = NAME_TX_ETHER;
270
271 // The actual name we'll use is determined at run time via 'ethernet' and 'ingress'
272 // booleans. We need to compile time allocate enough space in the struct
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700273 // hence this macro magic to make sure we have enough space for either
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800274 // possibility. In practice some of these are actually the same size.
275#define ASCIIZ_MAXLEN_NAME_RX \
276 ((sizeof(name_rx_rawip) > sizeof(name_rx_ether)) ? sizeof(name_rx_rawip) \
277 : sizeof(name_rx_ether))
278#define ASCIIZ_MAXLEN_NAME_TX \
279 ((sizeof(name_tx_rawip) > sizeof(name_tx_ether)) ? sizeof(name_tx_rawip) \
280 : sizeof(name_tx_ether))
281#define ASCIIZ_MAXLEN_NAME \
282 ((ASCIIZ_MAXLEN_NAME_RX > ASCIIZ_MAXLEN_NAME_TX) ? ASCIIZ_MAXLEN_NAME_RX \
283 : ASCIIZ_MAXLEN_NAME_TX)
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700284
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800285 // These are not compile time constants: NAME is used in strncpy below
286#define NAME_RX (ethernet ? NAME_RX_ETHER : NAME_RX_RAWIP)
287#define NAME_TX (ethernet ? NAME_TX_ETHER : NAME_TX_RAWIP)
288#define NAME (ingress ? NAME_RX : NAME_TX)
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700289
290 struct {
291 nlmsghdr n;
292 tcmsg t;
293 struct {
294 nlattr attr;
295 char str[NLMSG_ALIGN(ASCIIZ_LEN_BPF)];
296 } kind;
297 struct {
298 nlattr attr;
299 struct {
300 nlattr attr;
301 __u32 u32;
302 } fd;
303 struct {
304 nlattr attr;
305 char str[NLMSG_ALIGN(ASCIIZ_MAXLEN_NAME)];
306 } name;
307 struct {
308 nlattr attr;
309 __u32 u32;
310 } flags;
311 } options;
312 } req = {
313 .n =
314 {
315 .nlmsg_len = sizeof(req),
316 .nlmsg_type = RTM_NEWTFILTER,
317 .nlmsg_flags = NETLINK_REQUEST_FLAGS | NLM_F_EXCL | NLM_F_CREATE,
318 },
319 .t =
320 {
321 .tcm_family = AF_UNSPEC,
322 .tcm_ifindex = ifIndex,
323 .tcm_handle = TC_H_UNSPEC,
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800324 .tcm_parent = TC_H_MAKE(TC_H_CLSACT,
325 ingress ? TC_H_MIN_INGRESS : TC_H_MIN_EGRESS),
326 .tcm_info = (prio << 16) |
327 (__u32)(ipv6 ? htons(ETH_P_IPV6) : htons(ETH_P_IP)),
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700328 },
329 .kind =
330 {
331 .attr =
332 {
333 .nla_len = sizeof(req.kind),
334 .nla_type = TCA_KIND,
335 },
336 .str = BPF,
337 },
338 .options =
339 {
340 .attr =
341 {
342 .nla_len = sizeof(req.options),
343 .nla_type = TCA_OPTIONS,
344 },
345 .fd =
346 {
347 .attr =
348 {
349 .nla_len = sizeof(req.options.fd),
350 .nla_type = TCA_BPF_FD,
351 },
352 .u32 = static_cast<__u32>(bpfFd),
353 },
354 .name =
355 {
356 .attr =
357 {
358 .nla_len = sizeof(req.options.name),
359 .nla_type = TCA_BPF_NAME,
360 },
361 // Visible via 'tc filter show', but
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800362 // is overwritten by strncpy below
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700363 .str = "placeholder",
364 },
365 .flags =
366 {
367 .attr =
368 {
369 .nla_len = sizeof(req.options.flags),
370 .nla_type = TCA_BPF_FLAGS,
371 },
372 .u32 = TCA_BPF_FLAG_ACT_DIRECT,
373 },
374 },
375 };
376
377 strncpy(req.options.name.str, NAME, sizeof(req.options.name.str));
378
379#undef NAME
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800380#undef NAME_TX
381#undef NAME_RX
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700382#undef ASCIIZ_MAXLEN_NAME
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800383#undef ASCIIZ_MAXLEN_NAME_TX
384#undef ASCIIZ_MAXLEN_NAME_RX
385#undef NAME_TX_ETHER
386#undef NAME_TX_RAWIP
387#undef NAME_RX_ETHER
388#undef NAME_RX_RAWIP
389#undef FSOBJ_SUFFIX
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700390#undef ASCIIZ_LEN_BPF
391#undef BPF
392
393 const int rv = send(fd, &req, sizeof(req), 0);
394 if (rv == -1) return -errno;
395 if (rv != sizeof(req)) return -EMSGSIZE;
396
397 return processNetlinkResponse(fd);
398}
399
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -0800400// tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
401int tcFilterAddDevIngressBpf(int fd, int ifIndex, int bpfFd, bool ethernet) {
402 return tcFilterAddDevBpf(fd, ifIndex, bpfFd, ethernet, /*ingress*/ true, /*ipv6*/ true);
403}
404
405// tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
406int tcFilterAddDevEgressBpf(int fd, int ifIndex, int bpfFd, bool ethernet) {
407 return tcFilterAddDevBpf(fd, ifIndex, bpfFd, ethernet, /*ingress*/ false, /*ipv6*/ false);
408}
409
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800410} // namespace net
411} // namespace android