diff --git a/bpf_progs/clat_mark.h b/bpf_progs/clat_mark.h new file mode 100644 index 0000000000..874d6ae3c4 --- /dev/null +++ b/bpf_progs/clat_mark.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +/* -=-=-=-=-= WARNING -=-=-=-=-=- + * + * DO *NOT* *EVER* CHANGE THIS CONSTANT + * + * This is aidl::android::net::INetd::CLAT_MARK but we can't use that from + * pure C code (ie. the eBPF clat program). + * + * It must match the iptables rules setup by netd on Android T. + * + * This mark value is used by the eBPF clatd program to mark ingress non-offloaded clat + * packets for later dropping in ip6tables bw_raw_PREROUTING. + * They need to be dropped *after* the clat daemon (via receive on an AF_PACKET socket) + * sees them and thus cannot be dropped from the bpf program itself. + */ +static const uint32_t CLAT_MARK = 0xDEADC1A7; diff --git a/bpf_progs/clatd.c b/bpf_progs/clatd.c index c5b855510f..66e9616904 100644 --- a/bpf_progs/clatd.c +++ b/bpf_progs/clatd.c @@ -36,16 +36,11 @@ #include "bpf_helpers.h" #include "bpf_net_helpers.h" #include "bpf_shared.h" +#include "clat_mark.h" // From kernel:include/net/ip.h #define IP_DF 0x4000 // Flag: "Don't Fragment" -// Used for iptables drops ingress clat packet. Beware of clat mark change may break the device -// which is using the old clat mark in netd platform code. The reason is that the clat mark is a -// mainline constant since T+ but netd iptable rules (ex: bandwidth control, firewall, and so on) -// are set in stone. -#define CLAT_MARK 0xdeadc1a7 - DEFINE_BPF_MAP_GRW(clat_ingress6_map, HASH, ClatIngress6Key, ClatIngress6Value, 16, AID_SYSTEM) static inline __always_inline int nat64(struct __sk_buff* skb, bool is_ethernet) { diff --git a/service/native/include/Common.h b/service/native/include/Common.h index 3f28991bea..03f449a56c 100644 --- a/service/native/include/Common.h +++ b/service/native/include/Common.h @@ -17,9 +17,12 @@ #pragma once // TODO: deduplicate with the constants in NetdConstants.h. #include +#include "clat_mark.h" using aidl::android::net::INetd; +static_assert(INetd::CLAT_MARK == CLAT_MARK, "must be 0xDEADC1A7"); + enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY }; // ALLOWLIST means the firewall denies all by default, uids must be explicitly ALLOWed