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