Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 19 | #include <arpa/inet.h> |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <linux/if.h> |
Maciej Żenczykowski | 7330b02 | 2019-01-28 17:30:24 -0800 | [diff] [blame] | 22 | #include <linux/netlink.h> |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 23 | #include <linux/pkt_cls.h> |
Maciej Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 24 | #include <linux/pkt_sched.h> |
| 25 | #include <linux/rtnetlink.h> |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 31 | #define LOG_TAG "ClatUtils" |
| 32 | #include <log/log.h> |
| 33 | |
Maciej Żenczykowski | 7330b02 | 2019-01-28 17:30:24 -0800 | [diff] [blame] | 34 | #include "NetlinkCommands.h" |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 35 | #include "android-base/unique_fd.h" |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 36 | #include "bpf/BpfUtils.h" |
| 37 | #include "netdbpf/bpf_shared.h" |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 38 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace net { |
| 41 | |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 42 | int 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 Żenczykowski | 4e36f13 | 2019-12-15 13:20:15 -0800 | [diff] [blame] | 65 | int getClatEgressMapFd(void) { |
| 66 | const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0); |
| 67 | return (fd == -1) ? -errno : fd; |
| 68 | } |
| 69 | |
Maciej Żenczykowski | e870a87 | 2019-12-15 13:56:13 -0800 | [diff] [blame^] | 70 | int 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 Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 76 | int getClatIngressMapFd(void) { |
| 77 | const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0); |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 78 | return (fd == -1) ? -errno : fd; |
| 79 | } |
| 80 | |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 81 | int 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 Żenczykowski | 949d84a | 2019-01-28 17:22:30 -0800 | [diff] [blame] | 84 | return (fd == -1) ? -errno : fd; |
| 85 | } |
| 86 | |
Maciej Żenczykowski | 7330b02 | 2019-01-28 17:30:24 -0800 | [diff] [blame] | 87 | // TODO: use //system/netd/server/NetlinkCommands.cpp:openNetlinkSocket(protocol) |
| 88 | int 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 Żenczykowski | 992a51d | 2019-02-11 18:06:56 -0800 | [diff] [blame] | 121 | // TODO: merge with //system/netd/server/SockDiag.cpp:checkError(fd) |
| 122 | int 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 Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 155 | // 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 |
| 158 | int doTcQdiscClsact(int fd, int ifIndex, __u16 nlMsgType, __u16 nlMsgFlags) { |
| 159 | // 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 | |
| 208 | int tcQdiscAddDevClsact(int fd, int ifIndex) { |
| 209 | return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE); |
| 210 | } |
| 211 | |
| 212 | int tcQdiscReplaceDevClsact(int fd, int ifIndex) { |
| 213 | return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE); |
| 214 | } |
| 215 | |
| 216 | int tcQdiscDelDevClsact(int fd, int ifIndex) { |
| 217 | return doTcQdiscClsact(fd, ifIndex, RTM_DELQDISC, 0); |
| 218 | } |
| 219 | |
Maciej Żenczykowski | f7a9f88 | 2019-04-02 22:09:04 -0700 | [diff] [blame] | 220 | // tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 221 | int tcFilterAddDevBpf(int fd, int ifIndex, int bpfFd, bool ethernet) { |
| 222 | // The priority doesn't matter until we actually start attaching multiple |
Maciej Żenczykowski | f7a9f88 | 2019-04-02 22:09:04 -0700 | [diff] [blame] | 223 | // things to the same interface's ingress point. |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 224 | const int prio = 1; |
| 225 | |
| 226 | // This is the name of the filter we're attaching (ie. this is the 'bpf' |
| 227 | // packet classifier enabled by kernel config option CONFIG_NET_CLS_BPF. |
| 228 | // |
| 229 | // We go through some hoops in order to make this compile time constants |
| 230 | // so that we can define the struct further down the function with the |
| 231 | // field for this sized correctly already during the build. |
| 232 | #define BPF "bpf" |
| 233 | const char bpf[] = BPF; |
| 234 | // sizeof() includes the terminating NULL |
| 235 | #define ASCIIZ_LEN_BPF sizeof(bpf) |
| 236 | |
| 237 | // This is to replicate program name suffix used by 'tc' Linux cli |
| 238 | // when it attaches programs. |
| 239 | #define FSOBJ_SUFFIX ":[*fsobj]" |
| 240 | |
| 241 | // This macro expands (from header files) to: |
| 242 | // prog_clatd_schedcls_ingress_clat_rawip:[*fsobj] |
| 243 | // and is the name of the pinned ebpf program for ARPHRD_RAWIP interfaces. |
| 244 | // (also compatible with anything that has 0 size L2 header) |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 245 | #define NAME_RAWIP CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 246 | const char name_rawip[] = NAME_RAWIP; |
| 247 | |
| 248 | // This macro expands (from header files) to: |
| 249 | // prog_clatd_schedcls_ingress_clat_ether:[*fsobj] |
| 250 | // and is the name of the pinned ebpf program for ARPHRD_ETHER interfaces. |
| 251 | // (also compatible with anything that has standard ethernet header) |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 252 | #define NAME_ETHER CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 253 | const char name_ether[] = NAME_ETHER; |
| 254 | |
| 255 | // The actual name we'll use is determined at run time via 'ethernet' |
| 256 | // boolean. We need to compile time allocate enough space in the struct |
| 257 | // hence this macro magic to make sure we have enough space for either |
| 258 | // possibility. In practice both are actually the same size. |
| 259 | #define ASCIIZ_MAXLEN_NAME \ |
| 260 | ((sizeof(name_rawip) > sizeof(name_ether)) ? sizeof(name_rawip) : sizeof(name_ether)) |
| 261 | |
| 262 | // This is not a compile time constant and is used in strcpy below |
| 263 | #define NAME (ethernet ? NAME_ETHER : NAME_RAWIP) |
| 264 | |
| 265 | struct { |
| 266 | nlmsghdr n; |
| 267 | tcmsg t; |
| 268 | struct { |
| 269 | nlattr attr; |
| 270 | char str[NLMSG_ALIGN(ASCIIZ_LEN_BPF)]; |
| 271 | } kind; |
| 272 | struct { |
| 273 | nlattr attr; |
| 274 | struct { |
| 275 | nlattr attr; |
| 276 | __u32 u32; |
| 277 | } fd; |
| 278 | struct { |
| 279 | nlattr attr; |
| 280 | char str[NLMSG_ALIGN(ASCIIZ_MAXLEN_NAME)]; |
| 281 | } name; |
| 282 | struct { |
| 283 | nlattr attr; |
| 284 | __u32 u32; |
| 285 | } flags; |
| 286 | } options; |
| 287 | } req = { |
| 288 | .n = |
| 289 | { |
| 290 | .nlmsg_len = sizeof(req), |
| 291 | .nlmsg_type = RTM_NEWTFILTER, |
| 292 | .nlmsg_flags = NETLINK_REQUEST_FLAGS | NLM_F_EXCL | NLM_F_CREATE, |
| 293 | }, |
| 294 | .t = |
| 295 | { |
| 296 | .tcm_family = AF_UNSPEC, |
| 297 | .tcm_ifindex = ifIndex, |
| 298 | .tcm_handle = TC_H_UNSPEC, |
| 299 | .tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS), |
| 300 | .tcm_info = (prio << 16) | htons(ETH_P_IPV6), |
| 301 | }, |
| 302 | .kind = |
| 303 | { |
| 304 | .attr = |
| 305 | { |
| 306 | .nla_len = sizeof(req.kind), |
| 307 | .nla_type = TCA_KIND, |
| 308 | }, |
| 309 | .str = BPF, |
| 310 | }, |
| 311 | .options = |
| 312 | { |
| 313 | .attr = |
| 314 | { |
| 315 | .nla_len = sizeof(req.options), |
| 316 | .nla_type = TCA_OPTIONS, |
| 317 | }, |
| 318 | .fd = |
| 319 | { |
| 320 | .attr = |
| 321 | { |
| 322 | .nla_len = sizeof(req.options.fd), |
| 323 | .nla_type = TCA_BPF_FD, |
| 324 | }, |
| 325 | .u32 = static_cast<__u32>(bpfFd), |
| 326 | }, |
| 327 | .name = |
| 328 | { |
| 329 | .attr = |
| 330 | { |
| 331 | .nla_len = sizeof(req.options.name), |
| 332 | .nla_type = TCA_BPF_NAME, |
| 333 | }, |
| 334 | // Visible via 'tc filter show', but |
| 335 | // is overwritten by strcpy below |
| 336 | .str = "placeholder", |
| 337 | }, |
| 338 | .flags = |
| 339 | { |
| 340 | .attr = |
| 341 | { |
| 342 | .nla_len = sizeof(req.options.flags), |
| 343 | .nla_type = TCA_BPF_FLAGS, |
| 344 | }, |
| 345 | .u32 = TCA_BPF_FLAG_ACT_DIRECT, |
| 346 | }, |
| 347 | }, |
| 348 | }; |
| 349 | |
| 350 | strncpy(req.options.name.str, NAME, sizeof(req.options.name.str)); |
| 351 | |
| 352 | #undef NAME |
| 353 | #undef ASCIIZ_MAXLEN_NAME |
| 354 | #undef NAME_ETHER |
| 355 | #undef NAME_RAWIP |
| 356 | #undef NAME |
| 357 | #undef ASCIIZ_LEN_BPF |
| 358 | #undef BPF |
| 359 | |
| 360 | const int rv = send(fd, &req, sizeof(req), 0); |
| 361 | if (rv == -1) return -errno; |
| 362 | if (rv != sizeof(req)) return -EMSGSIZE; |
| 363 | |
| 364 | return processNetlinkResponse(fd); |
| 365 | } |
| 366 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 367 | } // namespace net |
| 368 | } // namespace android |