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 | |
Maciej Żenczykowski | 776200e | 2020-02-05 02:05:16 -0800 | [diff] [blame] | 17 | #pragma once |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 18 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Maciej Żenczykowski | f124738 | 2020-02-05 00:44:14 -0800 | [diff] [blame] | 20 | #include <linux/if_ether.h> |
Maciej Żenczykowski | b3e69d6 | 2020-02-05 02:44:16 -0800 | [diff] [blame^] | 21 | #include <linux/rtnetlink.h> |
Maciej Żenczykowski | f124738 | 2020-02-05 00:44:14 -0800 | [diff] [blame] | 22 | |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 25 | #include "bpf/BpfUtils.h" |
| 26 | #include "netdbpf/bpf_shared.h" |
| 27 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
| 30 | |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 31 | int hardwareAddressType(const std::string& interface); |
| 32 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 33 | static inline int getClatEgressMapFd(void) { |
| 34 | const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0); |
| 35 | return (fd == -1) ? -errno : fd; |
| 36 | } |
Maciej Żenczykowski | 4e36f13 | 2019-12-15 13:20:15 -0800 | [diff] [blame] | 37 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 38 | static inline int getClatEgressProgFd(bool with_ethernet_header) { |
| 39 | const int fd = bpf::bpfFdGet( |
| 40 | with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0); |
| 41 | return (fd == -1) ? -errno : fd; |
| 42 | } |
Maciej Żenczykowski | e870a87 | 2019-12-15 13:56:13 -0800 | [diff] [blame] | 43 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 44 | static inline int getClatIngressMapFd(void) { |
| 45 | const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0); |
| 46 | return (fd == -1) ? -errno : fd; |
| 47 | } |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 48 | |
Maciej Żenczykowski | e5b6b4d | 2020-02-05 02:00:38 -0800 | [diff] [blame] | 49 | static inline int getClatIngressProgFd(bool with_ethernet_header) { |
| 50 | const int fd = bpf::bpfFdGet( |
| 51 | with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0); |
| 52 | return (fd == -1) ? -errno : fd; |
| 53 | } |
Maciej Żenczykowski | 949d84a | 2019-01-28 17:22:30 -0800 | [diff] [blame] | 54 | |
Maciej Żenczykowski | 7330b02 | 2019-01-28 17:30:24 -0800 | [diff] [blame] | 55 | int openNetlinkSocket(void); |
| 56 | |
Maciej Żenczykowski | 992a51d | 2019-02-11 18:06:56 -0800 | [diff] [blame] | 57 | int processNetlinkResponse(int fd); |
| 58 | |
Maciej Żenczykowski | b3e69d6 | 2020-02-05 02:44:16 -0800 | [diff] [blame^] | 59 | int doTcQdiscClsact(int fd, int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags); |
| 60 | |
| 61 | static inline int tcQdiscAddDevClsact(int fd, int ifIndex) { |
| 62 | return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE); |
| 63 | } |
| 64 | |
| 65 | static inline int tcQdiscReplaceDevClsact(int fd, int ifIndex) { |
| 66 | return doTcQdiscClsact(fd, ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE); |
| 67 | } |
| 68 | |
| 69 | static inline int tcQdiscDelDevClsact(int fd, int ifIndex) { |
| 70 | return doTcQdiscClsact(fd, ifIndex, RTM_DELQDISC, 0); |
| 71 | } |
Maciej Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 72 | |
Maciej Żenczykowski | b140a3a | 2019-12-15 11:53:46 -0800 | [diff] [blame] | 73 | int tcFilterAddDevIngressBpf(int fd, int ifIndex, int bpfFd, bool ethernet); |
Maciej Żenczykowski | a06943c | 2019-12-15 11:57:42 -0800 | [diff] [blame] | 74 | int tcFilterAddDevEgressBpf(int fd, int ifIndex, int bpfFd, bool ethernet); |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 75 | |
Maciej Żenczykowski | f124738 | 2020-02-05 00:44:14 -0800 | [diff] [blame] | 76 | // tc filter del dev .. in/egress prio .. protocol .. |
| 77 | int tcFilterDelDev(int fd, int ifIndex, bool ingress, uint16_t prio, uint16_t proto); |
| 78 | |
| 79 | // tc filter del dev .. ingress prio 1 protocol ipv6 |
| 80 | static inline int tcFilterDelDevIngressClatIpv6(int fd, int ifIndex) { |
| 81 | return tcFilterDelDev(fd, ifIndex, /*ingress*/ true, /*prio*/ 1, ETH_P_IPV6); |
| 82 | } |
| 83 | |
| 84 | // tc filter del dev .. egress prio 1 protocol ip |
| 85 | static inline int tcFilterDelDevEgressClatIpv4(int fd, int ifIndex) { |
| 86 | return tcFilterDelDev(fd, ifIndex, /*ingress*/ false, /*prio*/ 1, ETH_P_IP); |
| 87 | } |
| 88 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 89 | } // namespace net |
| 90 | } // namespace android |