blob: 2caace0038855e509c9dc947fa839bfcc1a30886 [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
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080019#include <errno.h>
Maciej Żenczykowskif1247382020-02-05 00:44:14 -080020#include <linux/if_ether.h>
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080021#include <linux/rtnetlink.h>
Maciej Żenczykowskif1247382020-02-05 00:44:14 -080022
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080023#include <string>
24
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080025#include "bpf/BpfUtils.h"
26#include "netdbpf/bpf_shared.h"
27
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080028namespace android {
29namespace net {
30
Maciej Żenczykowskiad730892020-02-13 18:21:32 -080031// this returns an ARPHRD_* constant or a -errno
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080032int hardwareAddressType(const std::string& interface);
33
Maciej Żenczykowskiad730892020-02-13 18:21:32 -080034// For better code clarity - do not change values - used for booleans like
35// with_ethernet_header or isEthernet.
36constexpr bool RAWIP = false;
37constexpr bool ETHER = true;
38
Maciej Żenczykowski86400032020-02-13 18:39:34 -080039// For better code clarity when used for 'bool ingress' parameter.
40constexpr bool EGRESS = false;
41constexpr bool INGRESS = true;
42
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080043inline int getClatEgressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080044 const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0);
45 return (fd == -1) ? -errno : fd;
46}
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080047
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080048inline int getClatEgressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080049 const int fd = bpf::bpfFdGet(
50 with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
51 return (fd == -1) ? -errno : fd;
52}
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080053
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080054inline int getClatIngressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080055 const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
56 return (fd == -1) ? -errno : fd;
57}
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070058
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080059inline int getClatIngressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080060 const int fd = bpf::bpfFdGet(
61 with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0);
62 return (fd == -1) ? -errno : fd;
63}
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080064
Maciej Żenczykowskifdd79292020-02-12 04:28:16 -080065inline int getTetherIngressMapFd(void) {
66 const int fd = bpf::bpfFdGet(TETHER_INGRESS_MAP_PATH, 0);
67 return (fd == -1) ? -errno : fd;
68}
69
Maciej Żenczykowski4acd4e82020-02-12 04:30:14 -080070inline int getTetherIngressProgFd(bool with_ethernet_header) {
71 const int fd = bpf::bpfFdGet(
72 with_ethernet_header ? TETHER_INGRESS_PROG_ETHER_PATH : TETHER_INGRESS_PROG_RAWIP_PATH,
73 0);
74 return (fd == -1) ? -errno : fd;
75}
76
Maciej Żenczykowski66ce72d2020-02-12 05:05:00 -080077inline int getTetherStatsMapFd(void) {
78 const int fd = bpf::bpfFdGet(TETHER_STATS_MAP_PATH, 0);
79 return (fd == -1) ? -errno : fd;
80}
81
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080082int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080083
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080084inline int tcQdiscAddDevClsact(int ifIndex) {
85 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080086}
87
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080088inline int tcQdiscReplaceDevClsact(int ifIndex) {
89 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080090}
91
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080092inline int tcQdiscDelDevClsact(int ifIndex) {
93 return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080094}
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080095
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -080096// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
97// direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080098int tcFilterAddDevBpf(int ifIndex, int bpfFd, bool ethernet, bool ingress, bool ipv6);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -080099
100// tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800101inline int tcFilterAddDevIngressBpf(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800102 return tcFilterAddDevBpf(ifIndex, bpfFd, ethernet, INGRESS, /*ipv6*/ true);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800103}
104
105// tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800106inline int tcFilterAddDevEgressBpf(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800107 return tcFilterAddDevBpf(ifIndex, bpfFd, ethernet, EGRESS, /*ipv6*/ false);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800108}
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700109
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800110// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800111int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800112
113// tc filter del dev .. ingress prio 1 protocol ipv6
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800114inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800115 return tcFilterDelDev(ifIndex, INGRESS, /*prio*/ 1, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800116}
117
118// tc filter del dev .. egress prio 1 protocol ip
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800119inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800120 return tcFilterDelDev(ifIndex, EGRESS, /*prio*/ 1, ETH_P_IP);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800121}
122
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800123} // namespace net
124} // namespace android