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 #pragma once
18 
19 #include <android-base/result.h>
20 #include <errno.h>
21 #include <linux/if_ether.h>
22 #include <linux/rtnetlink.h>
23 
24 #include <string>
25 
26 #include "bpf/BpfUtils.h"
27 #include "netdbpf/bpf_shared.h"
28 
29 namespace android {
30 namespace net {
31 
32 // For better code clarity - do not change values - used for booleans like
33 // with_ethernet_header or isEthernet.
34 constexpr bool RAWIP = false;
35 constexpr bool ETHER = true;
36 
37 // For better code clarity when used for 'bool ingress' parameter.
38 constexpr bool EGRESS = false;
39 constexpr bool INGRESS = true;
40 
41 // The priority of clat/tether hooks - smaller is higher priority.
42 constexpr uint16_t PRIO_CLAT = 1;
43 constexpr uint16_t PRIO_TETHER = 2;
44 
45 // this returns an ARPHRD_* constant or a -errno
46 int hardwareAddressType(const std::string& interface);
47 
48 base::Result<bool> isEthernet(const std::string& interface);
49 
getClatEgressMapFd(void)50 inline int getClatEgressMapFd(void) {
51     const int fd = bpf::mapRetrieveRW(CLAT_EGRESS_MAP_PATH);
52     return (fd == -1) ? -errno : fd;
53 }
54 
getClatEgressProgFd(bool with_ethernet_header)55 inline int getClatEgressProgFd(bool with_ethernet_header) {
56     const int fd = bpf::retrieveProgram(with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH
57                                                              : CLAT_EGRESS_PROG_RAWIP_PATH);
58     return (fd == -1) ? -errno : fd;
59 }
60 
getClatIngressMapFd(void)61 inline int getClatIngressMapFd(void) {
62     const int fd = bpf::mapRetrieveRW(CLAT_INGRESS_MAP_PATH);
63     return (fd == -1) ? -errno : fd;
64 }
65 
getClatIngressProgFd(bool with_ethernet_header)66 inline int getClatIngressProgFd(bool with_ethernet_header) {
67     const int fd = bpf::retrieveProgram(with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH
68                                                              : CLAT_INGRESS_PROG_RAWIP_PATH);
69     return (fd == -1) ? -errno : fd;
70 }
71 
getTetherIngressMapFd(void)72 inline int getTetherIngressMapFd(void) {
73     const int fd = bpf::mapRetrieveRW(TETHER_INGRESS_MAP_PATH);
74     return (fd == -1) ? -errno : fd;
75 }
76 
getTetherIngressProgFd(bool with_ethernet_header)77 inline int getTetherIngressProgFd(bool with_ethernet_header) {
78     const int fd = bpf::retrieveProgram(with_ethernet_header ? TETHER_INGRESS_PROG_ETHER_PATH
79                                                              : TETHER_INGRESS_PROG_RAWIP_PATH);
80     return (fd == -1) ? -errno : fd;
81 }
82 
getTetherStatsMapFd(void)83 inline int getTetherStatsMapFd(void) {
84     const int fd = bpf::mapRetrieveRW(TETHER_STATS_MAP_PATH);
85     return (fd == -1) ? -errno : fd;
86 }
87 
getTetherLimitMapFd(void)88 inline int getTetherLimitMapFd(void) {
89     const int fd = bpf::mapRetrieveRW(TETHER_LIMIT_MAP_PATH);
90     return (fd == -1) ? -errno : fd;
91 }
92 
93 int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
94 
tcQdiscAddDevClsact(int ifIndex)95 inline int tcQdiscAddDevClsact(int ifIndex) {
96     return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
97 }
98 
tcQdiscReplaceDevClsact(int ifIndex)99 inline int tcQdiscReplaceDevClsact(int ifIndex) {
100     return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
101 }
102 
tcQdiscDelDevClsact(int ifIndex)103 inline int tcQdiscDelDevClsact(int ifIndex) {
104     return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
105 }
106 
107 // tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
108 // direct-action
109 int tcFilterAddDevBpf(int ifIndex, bool ingress, uint16_t prio, uint16_t proto, int bpfFd,
110                       bool ethernet);
111 
112 // tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
tcFilterAddDevIngressClatIpv6(int ifIndex,int bpfFd,bool ethernet)113 inline int tcFilterAddDevIngressClatIpv6(int ifIndex, int bpfFd, bool ethernet) {
114     return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6, bpfFd, ethernet);
115 }
116 
117 // tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
tcFilterAddDevEgressClatIpv4(int ifIndex,int bpfFd,bool ethernet)118 inline int tcFilterAddDevEgressClatIpv4(int ifIndex, int bpfFd, bool ethernet) {
119     return tcFilterAddDevBpf(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP, bpfFd, ethernet);
120 }
121 
122 // tc filter add dev .. ingress prio 2 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
tcFilterAddDevIngressTether(int ifIndex,int bpfFd,bool ethernet)123 inline int tcFilterAddDevIngressTether(int ifIndex, int bpfFd, bool ethernet) {
124     return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6, bpfFd, ethernet);
125 }
126 
127 // tc filter del dev .. in/egress prio .. protocol ..
128 int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
129 
130 // tc filter del dev .. ingress prio 1 protocol ipv6
tcFilterDelDevIngressClatIpv6(int ifIndex)131 inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
132     return tcFilterDelDev(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6);
133 }
134 
135 // tc filter del dev .. egress prio 1 protocol ip
tcFilterDelDevEgressClatIpv4(int ifIndex)136 inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
137     return tcFilterDelDev(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP);
138 }
139 
140 // tc filter del dev .. ingress prio 2 protocol ipv6
tcFilterDelDevIngressTether(int ifIndex)141 inline int tcFilterDelDevIngressTether(int ifIndex) {
142     return tcFilterDelDev(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6);
143 }
144 
145 }  // namespace net
146 }  // namespace android
147