blob: 2248b0c58ea0bddbaa1c925e8c0c92165a6b947d [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 Żenczykowski830a15e2020-02-13 18:47:25 -080043// The priority of clat/tether hooks - smaller is higher priority.
44constexpr uint16_t PRIO_CLAT = 1;
45constexpr uint16_t PRIO_TETHER = 2;
46
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080047inline int getClatEgressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080048 const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0);
49 return (fd == -1) ? -errno : fd;
50}
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080051
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080052inline int getClatEgressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080053 const int fd = bpf::bpfFdGet(
54 with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
55 return (fd == -1) ? -errno : fd;
56}
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080057
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080058inline int getClatIngressMapFd(void) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080059 const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
60 return (fd == -1) ? -errno : fd;
61}
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070062
Maciej Żenczykowskibfcd6a52020-02-12 04:05:58 -080063inline int getClatIngressProgFd(bool with_ethernet_header) {
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080064 const int fd = bpf::bpfFdGet(
65 with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0);
66 return (fd == -1) ? -errno : fd;
67}
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080068
Maciej Żenczykowskifdd79292020-02-12 04:28:16 -080069inline int getTetherIngressMapFd(void) {
70 const int fd = bpf::bpfFdGet(TETHER_INGRESS_MAP_PATH, 0);
71 return (fd == -1) ? -errno : fd;
72}
73
Maciej Żenczykowski4acd4e82020-02-12 04:30:14 -080074inline int getTetherIngressProgFd(bool with_ethernet_header) {
75 const int fd = bpf::bpfFdGet(
76 with_ethernet_header ? TETHER_INGRESS_PROG_ETHER_PATH : TETHER_INGRESS_PROG_RAWIP_PATH,
77 0);
78 return (fd == -1) ? -errno : fd;
79}
80
Maciej Żenczykowski66ce72d2020-02-12 05:05:00 -080081inline int getTetherStatsMapFd(void) {
82 const int fd = bpf::bpfFdGet(TETHER_STATS_MAP_PATH, 0);
83 return (fd == -1) ? -errno : fd;
84}
85
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080086int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags);
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080087
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080088inline int tcQdiscAddDevClsact(int ifIndex) {
89 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_EXCL | NLM_F_CREATE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080090}
91
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080092inline int tcQdiscReplaceDevClsact(int ifIndex) {
93 return doTcQdiscClsact(ifIndex, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_REPLACE);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080094}
95
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080096inline int tcQdiscDelDevClsact(int ifIndex) {
97 return doTcQdiscClsact(ifIndex, RTM_DELQDISC, 0);
Maciej Żenczykowskib3e69d62020-02-05 02:44:16 -080098}
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080099
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800100// tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
101// direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800102int tcFilterAddDevBpf(int ifIndex, int bpfFd, bool ethernet, bool ingress, bool ipv6);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800103
104// tc filter add dev .. ingress prio 1 protocol ipv6 bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800105inline int tcFilterAddDevIngressBpf(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800106 return tcFilterAddDevBpf(ifIndex, bpfFd, ethernet, INGRESS, /*ipv6*/ true);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800107}
108
109// tc filter add dev .. egress prio 1 protocol ip bpf object-pinned /sys/fs/bpf/... direct-action
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800110inline int tcFilterAddDevEgressBpf(int ifIndex, int bpfFd, bool ethernet) {
Maciej Żenczykowski86400032020-02-13 18:39:34 -0800111 return tcFilterAddDevBpf(ifIndex, bpfFd, ethernet, EGRESS, /*ipv6*/ false);
Maciej Żenczykowski74f8c9d2020-02-05 02:59:20 -0800112}
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700113
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800114// tc filter del dev .. in/egress prio .. protocol ..
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800115int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800116
117// tc filter del dev .. ingress prio 1 protocol ipv6
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800118inline int tcFilterDelDevIngressClatIpv6(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800119 return tcFilterDelDev(ifIndex, INGRESS, PRIO_CLAT, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800120}
121
122// tc filter del dev .. egress prio 1 protocol ip
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800123inline int tcFilterDelDevEgressClatIpv4(int ifIndex) {
Maciej Żenczykowski830a15e2020-02-13 18:47:25 -0800124 return tcFilterDelDev(ifIndex, EGRESS, PRIO_CLAT, ETH_P_IP);
125}
126
127// tc filter del dev .. ingress prio 2 protocol ipv6
128inline int tcFilterDelDevIngressTether(int ifIndex) {
129 return tcFilterDelDev(ifIndex, INGRESS, PRIO_TETHER, ETH_P_IPV6);
Maciej Żenczykowskif1247382020-02-05 00:44:14 -0800130}
131
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800132} // namespace net
133} // namespace android