blob: e7193e463d53d178c4339af7a0fee4c832fb7f6e [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 Żenczykowski8887cce2020-05-04 18:14:59 -070089inline int getTetherLimitMapFd(void) {
90 const int fd = bpf::bpfFdGet(TETHER_LIMIT_MAP_PATH, 0);
91 return (fd == -1) ? -errno : fd;
92}
93
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080094int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080095
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080096inline int tcQdiscAddDevClsact(int ifIndex) {
97 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080098}
99
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800100inline int tcQdiscReplaceDevClsact(int ifIndex) {
101 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -0800102}
103
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800104inline int tcQdiscDelDevClsact(int ifIndex) {
105 return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -0800106}
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800107
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800108// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
109// direct-action
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800110int tcFilterAddDevBpf(int ifIndex, bool ingress, uint16_t prio, uint16_t proto, int bpfFd,
111 bool ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800112
113// tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800114inline int tcFilterAddDevIngressClatIpv6(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800115 return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6, bpfFd, ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800116}
117
118// tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800119inline int tcFilterAddDevEgressClatIpv4(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowskie9207f62020-02-13 18:58:51 -0800120 return tcFilterAddDevBpf(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP, bpfFd, ethernet);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800121}
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700122
Maciej Żenczykowski58efd782020-02-16 17:15:26 -0800123// tc filter add dev .. ingress prio 2 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
124inline int tcFilterAddDevIngressTether(int ifIndex, int bpfFd, bool ethernet) {
125 return tcFilterAddDevBpf(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6, bpfFd, ethernet);
126}
127
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800128// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800129int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800130
131// tc filter del dev .. ingress prio 1 protocol ipv6
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800132inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800133 return tcFilterDelDev(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800134}
135
136// tc filter del dev .. egress prio 1 protocol ip
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800137inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800138 return tcFilterDelDev(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP);
139}
140
141// tc filter del dev .. ingress prio 2 protocol ipv6
142inline int tcFilterDelDevIngressTether(int ifIndex) {
143 return tcFilterDelDev(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800144}
145
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800146} // namespace net
147} // namespace android