• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/if_link.h>
23 #include <linux/rtnetlink.h>
24 
25 #include <string>
26 
27 #include "bpf/BpfUtils.h"
28 #include "netdbpf/bpf_shared.h"
29 
30 namespace android {
31 namespace net {
32 
33 // For better code clarity - do not change values - used for booleans like
34 // with_ethernet_header or isEthernet.
35 constexpr bool RAWIP = false;
36 constexpr bool ETHER = true;
37 
38 // For better code clarity when used for 'bool ingress' parameter.
39 constexpr bool EGRESS = false;
40 constexpr bool INGRESS = true;
41 
42 // this returns an ARPHRD_* constant or a -errno
43 int hardwareAddressType(const std::string& interface);
44 
45 // return MTU or -errno
46 int deviceMTU(const std::string& interface);
47 
48 base::Result<bool> isEthernet(const std::string& interface);
49 
getClatEgress4MapFd(void)50 inline int getClatEgress4MapFd(void) {
51     const int fd = bpf::mapRetrieveRW(CLAT_EGRESS4_MAP_PATH);
52     return (fd == -1) ? -errno : fd;
53 }
54 
getClatEgress4ProgFd(bool with_ethernet_header)55 inline int getClatEgress4ProgFd(bool with_ethernet_header) {
56     const int fd = bpf::retrieveProgram(with_ethernet_header ? CLAT_EGRESS4_PROG_ETHER_PATH
57                                                              : CLAT_EGRESS4_PROG_RAWIP_PATH);
58     return (fd == -1) ? -errno : fd;
59 }
60 
getClatIngress6MapFd(void)61 inline int getClatIngress6MapFd(void) {
62     const int fd = bpf::mapRetrieveRW(CLAT_INGRESS6_MAP_PATH);
63     return (fd == -1) ? -errno : fd;
64 }
65 
getClatIngress6ProgFd(bool with_ethernet_header)66 inline int getClatIngress6ProgFd(bool with_ethernet_header) {
67     const int fd = bpf::retrieveProgram(with_ethernet_header ? CLAT_INGRESS6_PROG_ETHER_PATH
68                                                              : CLAT_INGRESS6_PROG_RAWIP_PATH);
69     return (fd == -1) ? -errno : fd;
70 }
71 
72 int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
73 
tcQdiscAddDevClsact(int ifIndex)74 inline int tcQdiscAddDevClsact(int ifIndex) {
75     return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
76 }
77 
tcQdiscReplaceDevClsact(int ifIndex)78 inline int tcQdiscReplaceDevClsact(int ifIndex) {
79     return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
80 }
81 
tcQdiscDelDevClsact(int ifIndex)82 inline int tcQdiscDelDevClsact(int ifIndex) {
83     return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
84 }
85 
86 // tc filter add dev .. in/egress prio 4 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
87 // direct-action
88 int tcFilterAddDevBpf(int ifIndex, bool ingress, uint16_t proto, int bpfFd, bool ethernet);
89 
90 // tc filter add dev .. ingress prio 4 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
tcFilterAddDevIngressClatIpv6(int ifIndex,int bpfFd,bool ethernet)91 inline int tcFilterAddDevIngressClatIpv6(int ifIndex, int bpfFd, bool ethernet) {
92     return tcFilterAddDevBpf(ifIndex, INGRESS, ETH_P_IPV6, bpfFd, ethernet);
93 }
94 
95 // tc filter add dev .. egress prio 4 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
tcFilterAddDevEgressClatIpv4(int ifIndex,int bpfFd,bool ethernet)96 inline int tcFilterAddDevEgressClatIpv4(int ifIndex, int bpfFd, bool ethernet) {
97     return tcFilterAddDevBpf(ifIndex, EGRESS, ETH_P_IP, bpfFd, ethernet);
98 }
99 
100 // tc filter del dev .. in/egress prio .. protocol ..
101 int tcFilterDelDev(int ifIndex, bool ingress, uint16_t proto);
102 
103 // tc filter del dev .. ingress prio 4 protocol ipv6
tcFilterDelDevIngressClatIpv6(int ifIndex)104 inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
105     return tcFilterDelDev(ifIndex, INGRESS, ETH_P_IPV6);
106 }
107 
108 // tc filter del dev .. egress prio 4 protocol ip
tcFilterDelDevEgressClatIpv4(int ifIndex)109 inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
110     return tcFilterDelDev(ifIndex, EGRESS, ETH_P_IP);
111 }
112 
113 }  // namespace net
114 }  // namespace android
115