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 |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 158 | static int doTcQdiscClsact(int fd, int ifIndex, __u16 nlMsgType, __u16 nlMsgFlags) { |
Maciej Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 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 | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 220 | // tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/... |
| 221 | // direct-action |
| 222 | static int tcFilterAddDevBpf(int fd, int ifIndex, int bpfFd, bool ethernet, bool ingress, |
| 223 | bool ipv6) { |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 224 | // The priority doesn't matter until we actually start attaching multiple |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 225 | // things to the same interface's in/egress point. |
| 226 | const __u32 prio = 1; |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 227 | |
| 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 Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 245 | // and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces. |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 246 | // (also compatible with anything that has 0 size L2 header) |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 247 | #define NAME_RX_RAWIP CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX |
| 248 | const char name_rx_rawip[] = NAME_RX_RAWIP; |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 249 | |
| 250 | // This macro expands (from header files) to: |
| 251 | // prog_clatd_schedcls_ingress_clat_ether:[*fsobj] |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 252 | // and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces. |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 253 | // (also compatible with anything that has standard ethernet header) |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 254 | #define NAME_RX_ETHER CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX |
| 255 | const char name_rx_ether[] = NAME_RX_ETHER; |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 256 | |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 257 | // 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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 273 | // hence this macro magic to make sure we have enough space for either |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 274 | // 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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 284 | |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 285 | // 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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 289 | |
| 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 Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 324 | .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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 328 | }, |
| 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 Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 362 | // is overwritten by strncpy below |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 363 | .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 Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 380 | #undef NAME_TX |
| 381 | #undef NAME_RX |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 382 | #undef ASCIIZ_MAXLEN_NAME |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 383 | #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 Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 390 | #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 Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame^] | 400 | // tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action |
| 401 | int 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 |
| 406 | int tcFilterAddDevEgressBpf(int fd, int ifIndex, int bpfFd, bool ethernet) { |
| 407 | return tcFilterAddDevBpf(fd, ifIndex, bpfFd, ethernet, /*ingress*/ false, /*ipv6*/ false); |
| 408 | } |
| 409 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 410 | } // namespace net |
| 411 | } // namespace android |