blob: cdb0d33efc4895c5a341ef1aa2941d60f137c991 [file] [log] [blame]
Maciej Żenczykowskib70da762019-01-28 15:20:48 -08001/*
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 Żenczykowski776200e2020-02-05 02:05:16 -080017#pragma once
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080018
Hungming Chenec203382020-02-13 19:15:33 +080019#include <android-base/result.h>
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080020#include <errno.h>
Maciej Żenczykowskif1247382020-02-05 00:44:14 -080021#include <linux/if_ether.h>
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080022#include <linux/rtnetlink.h>
Maciej Żenczykowskif1247382020-02-05 00:44:14 -080023
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080024#include <string>
25
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080026#include "bpf/BpfUtils.h"
27#include "netdbpf/bpf_shared.h"
28
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080029namespace android {
30namespace net {
31
Maciej Żenczykowskiad730892020-02-13 18:21:32 -080032// For better code clarity - do not change values - used for booleans like
33// with_ethernet_header or isEthernet.
34constexpr bool RAWIP = false;
35constexpr bool ETHER = true;
36
Maciej Żenczykowski86400032020-02-13 18:39:34 -080037// For better code clarity when used for 'bool ingress' parameter.
38constexpr bool EGRESS = false;
39constexpr bool INGRESS = true;
40
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -080041// The priority of clat/tether hooks - smaller is higher priority.
42constexpr uint16_t PRIO_CLAT = 1;
43constexpr uint16_t PRIO_TETHER = 2;
44
Hungming Chenec203382020-02-13 19:15:33 +080045// this returns an ARPHRD_* constant or a -errno
46int hardwareAddressType(const std::string& interface);
47
48base::Result<bool> isEthernet(const std::string& interface);
49
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080050inline int getClatEgressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080051 const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0);
52 return (fd == -1) ? -errno : fd;
53}
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080054
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080055inline int getClatEgressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080056 const int fd = bpf::bpfFdGet(
57 with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
58 return (fd == -1) ? -errno : fd;
59}
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080060
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080061inline int getClatIngressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080062 const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
63 return (fd == -1) ? -errno : fd;
64}
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070065
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080066inline int getClatIngressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080067 const int fd = bpf::bpfFdGet(
68 with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0);
69 return (fd == -1) ? -errno : fd;
70}
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080071
Maciej Żenczykowskifdd79292020-02-12 04:28:16 -080072inline int getTetherIngressMapFd(void) {
73 const int fd = bpf::bpfFdGet(TETHER_INGRESS_MAP_PATH, 0);
74 return (fd == -1) ? -errno : fd;
75}
76
Maciej Żenczykowski4acd4e82020-02-12 04:30:14 -080077inline int getTetherIngressProgFd(bool with_ethernet_header) {
78 const int fd = bpf::bpfFdGet(
79 with_ethernet_header ? TETHER_INGRESS_PROG_ETHER_PATH : TETHER_INGRESS_PROG_RAWIP_PATH,
80 0);
81 return (fd == -1) ? -errno : fd;
82}
83
Maciej Żenczykowski66ce72d2020-02-12 05:05:00 -080084inline int getTetherStatsMapFd(void) {
85 const int fd = bpf::bpfFdGet(TETHER_STATS_MAP_PATH, 0);
86 return (fd == -1) ? -errno : fd;
87}
88
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080089int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080090
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080091inline int tcQdiscAddDevClsact(int ifIndex) {
92 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080093}
94
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080095inline int tcQdiscReplaceDevClsact(int ifIndex) {
96 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080097}
98
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080099inline int tcQdiscDelDevClsact(int ifIndex) {
100 return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -0800101}
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800102
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800103// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
104// direct-action
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800105int tcFilterAddDevBpf(int ifIndex, bool ingress, uint16_t prio, uint16_t proto, int bpfFd,
106 bool ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800107
108// tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800109inline int tcFilterAddDevIngressClatIpv6(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800110 return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6, bpfFd, ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800111}
112
113// tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800114inline int tcFilterAddDevEgressClatIpv4(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800115 return tcFilterAddDevBpf(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP, bpfFd, ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800116}
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700117
Maciej Żenczykowski58efd782020-02-16 17:15:26 -0800118// tc filter add dev .. ingress prio 2 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
119inline int tcFilterAddDevIngressTether(int ifIndex, int bpfFd, bool ethernet) {
120 return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6, bpfFd, ethernet);
121}
122
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800123// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800124int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800125
126// tc filter del dev .. ingress prio 1 protocol ipv6
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800127inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800128 return tcFilterDelDev(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800129}
130
131// tc filter del dev .. egress prio 1 protocol ip
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800132inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800133 return tcFilterDelDev(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP);
134}
135
136// tc filter del dev .. ingress prio 2 protocol ipv6
137inline int tcFilterDelDevIngressTether(int ifIndex) {
138 return tcFilterDelDev(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800139}
140
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800141} // namespace net
142} // namespace android