blob: e8a694ba87d939373c3307cffafdded737b33b49 [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>
21
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080022#include <string>
23
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080024#include "bpf/BpfUtils.h"
25#include "netdbpf/bpf_shared.h"
26
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080027namespace android {
28namespace net {
29
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080030int hardwareAddressType(const std::string& interface);
31
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080032static inline int getClatEgressMapFd(void) {
33 const int fd = bpf::bpfFdGet(CLAT_EGRESS_MAP_PATH, 0);
34 return (fd == -1) ? -errno : fd;
35}
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080036
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080037static inline int getClatEgressProgFd(bool with_ethernet_header) {
38 const int fd = bpf::bpfFdGet(
39 with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
40 return (fd == -1) ? -errno : fd;
41}
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080042
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080043static inline int getClatIngressMapFd(void) {
44 const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
45 return (fd == -1) ? -errno : fd;
46}
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070047
Maciej Żenczykowskie5b6b4d2020-02-05 02:00:38 -080048static inline int getClatIngressProgFd(bool with_ethernet_header) {
49 const int fd = bpf::bpfFdGet(
50 with_ethernet_header ? CLAT_INGRESS_PROG_ETHER_PATH : CLAT_INGRESS_PROG_RAWIP_PATH, 0);
51 return (fd == -1) ? -errno : fd;
52}
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -080053
Maciej Żenczykowski7330b022019-01-28 17:30:24 -080054int openNetlinkSocket(void);
55
Maciej Żenczykowski992a51d2019-02-11 18:06:56 -080056int processNetlinkResponse(int fd);
57
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -080058int tcQdiscAddDevClsact(int fd, int ifIndex);
59int tcQdiscReplaceDevClsact(int fd, int ifIndex);
60int tcQdiscDelDevClsact(int fd, int ifIndex);
61
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -080062int tcFilterAddDevIngressBpf(int fd, int ifIndex, int bpfFd, bool ethernet);
Maciej Żenczykowskia06943c2019-12-15 11:57:42 -080063int tcFilterAddDevEgressBpf(int fd, int ifIndex, int bpfFd, bool ethernet);
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070064
Maciej Żenczykowskif1247382020-02-05 00:44:14 -080065// tc filter del dev .. in/egress prio .. protocol ..
66int tcFilterDelDev(int fd, int ifIndex, bool ingress, uint16_t prio, uint16_t proto);
67
68// tc filter del dev .. ingress prio 1 protocol ipv6
69static inline int tcFilterDelDevIngressClatIpv6(int fd, int ifIndex) {
70 return tcFilterDelDev(fd, ifIndex, /*ingress*/ true, /*prio*/ 1, ETH_P_IPV6);
71}
72
73// tc filter del dev .. egress prio 1 protocol ip
74static inline int tcFilterDelDevEgressClatIpv4(int fd, int ifIndex) {
75 return tcFilterDelDev(fd, ifIndex, /*ingress*/ false, /*prio*/ 1, ETH_P_IP);
76}
77
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080078} // namespace net
79} // namespace android