[NFCT.TETHER.8] Prepare the upstream information for IPv4 offload rule

Add upstream interface index and its IPv4 address mapping

Required for building IPv4 forwarding rule when a conntrack event is
received.

Test: atest TetheringCoverageTests
Change-Id: I73f304777704f09481b80d18751e0bab5bab2edc
This commit is contained in:
Hungming Chen 2020-12-22 20:05:34 +08:00
commit 8784a60eab
2 changed files with 49 additions and 0 deletions

View file

@ -30,6 +30,7 @@ import static com.android.networkstack.tethering.TetheringConfiguration.DEFAULT_
import android.app.usage.NetworkStatsManager;
import android.net.INetd;
import android.net.LinkProperties;
import android.net.MacAddress;
import android.net.NetworkStats;
import android.net.NetworkStats.Entry;
@ -38,6 +39,7 @@ import android.net.ip.ConntrackMonitor;
import android.net.ip.ConntrackMonitor.ConntrackEventConsumer;
import android.net.ip.IpServer;
import android.net.netstats.provider.NetworkStatsProvider;
import android.net.util.InterfaceParams;
import android.net.util.SharedLog;
import android.net.util.TetheringUtils.ForwardedStats;
import android.os.ConditionVariable;
@ -58,7 +60,9 @@ import com.android.networkstack.tethering.apishim.common.BpfCoordinatorShim;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
@ -176,6 +180,11 @@ public class BpfCoordinator {
// Set for which downstream is monitoring the conntrack netlink message.
private final Set<IpServer> mMonitoringIpServers = new HashSet<>();
// Map of upstream interface IPv4 address to interface index.
// TODO: consider making the key to be unique because the upstream address is not unique. It
// is okay for now because there have only one upstream generally.
private final HashMap<Inet4Address, Integer> mIpv4UpstreamIndices = new HashMap<>();
// Runnable that used by scheduling next polling of stats.
private final Runnable mScheduledPollingTask = () -> {
updateForwardedStats();
@ -550,6 +559,39 @@ public class BpfCoordinator {
}
}
/**
* Call when UpstreamNetworkState may be changed.
* If upstream has ipv4 for tethering, update this new UpstreamNetworkState to map. The
* upstream interface index and its address mapping is prepared for building IPv4
* offload rule.
*
* TODO: Delete the unused upstream interface mapping.
* TODO: Support ether ip upstream interface.
*/
public void addUpstreamIfindexToMap(LinkProperties lp) {
if (!mPollingStarted) return;
// This will not work on a network that is using 464xlat because hasIpv4Address will not be
// true.
// TODO: need to consider 464xlat.
if (lp == null || !lp.hasIpv4Address()) return;
// Support raw ip upstream interface only.
final InterfaceParams params = InterfaceParams.getByName(lp.getInterfaceName());
if (params == null || params.hasMacAddress) return;
Collection<InetAddress> addresses = lp.getAddresses();
for (InetAddress addr: addresses) {
if (addr instanceof Inet4Address) {
Inet4Address i4addr = (Inet4Address) addr;
if (!i4addr.isAnyLocalAddress() && !i4addr.isLinkLocalAddress()
&& !i4addr.isLoopbackAddress() && !i4addr.isMulticastAddress()) {
mIpv4UpstreamIndices.put(i4addr, params.index);
}
}
}
}
/**
* Dump information.
* Block the function until all the data are dumped on the handler thread or timed-out. The

View file

@ -1636,6 +1636,13 @@ public class Tethering {
protected void handleNewUpstreamNetworkState(UpstreamNetworkState ns) {
mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns);
mOffload.updateUpstreamNetworkState(ns);
// TODO: Delete all related offload rules which are using this upstream.
if (ns != null) {
// Add upstream index to the map. The upstream interface index is required while
// the conntrack event builds the offload rules.
mBpfCoordinator.addUpstreamIfindexToMap(ns.linkProperties);
}
}
private void handleInterfaceServingStateActive(int mode, IpServer who) {