2019-09-09 20:50:49 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package android.net.ip;
|
|
|
|
|
|
2019-12-06 15:24:53 +08:00
|
|
|
import static android.net.RouteInfo.RTN_UNICAST;
|
2023-03-15 06:45:04 +00:00
|
|
|
import static android.net.TetheringManager.CONNECTIVITY_SCOPE_GLOBAL;
|
|
|
|
|
import static android.net.TetheringManager.CONNECTIVITY_SCOPE_LOCAL;
|
2022-12-14 08:49:40 +00:00
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
|
|
|
|
|
import static android.net.TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
|
2020-02-27 20:27:18 +08:00
|
|
|
import static android.net.TetheringManager.TetheringRequest.checkStaticAddressConfiguration;
|
2019-09-09 20:50:49 +08:00
|
|
|
import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS;
|
|
|
|
|
import static android.net.util.NetworkConstants.asByte;
|
2020-03-09 15:38:45 +09:00
|
|
|
import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
2020-07-31 20:00:30 +09:00
|
|
|
import static com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTH;
|
2023-08-09 18:55:49 +09:00
|
|
|
import static com.android.net.module.util.NetworkStackConstants.RFC7421_PREFIX_LENGTH;
|
2024-01-22 11:09:55 +00:00
|
|
|
import static com.android.networkstack.tethering.TetheringConfiguration.USE_SYNC_SM;
|
2021-07-13 17:12:56 +08:00
|
|
|
import static com.android.networkstack.tethering.UpstreamNetworkState.isVcnInterface;
|
2021-11-04 11:26:03 +08:00
|
|
|
import static com.android.networkstack.tethering.util.PrefixUtils.asIpPrefix;
|
|
|
|
|
import static com.android.networkstack.tethering.util.TetheringMessageBase.BASE_IPSERVER;
|
2020-07-31 20:00:30 +09:00
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.net.INetd;
|
|
|
|
|
import android.net.INetworkStackStatusCallback;
|
|
|
|
|
import android.net.IpPrefix;
|
|
|
|
|
import android.net.LinkAddress;
|
|
|
|
|
import android.net.LinkProperties;
|
2020-02-13 09:16:19 +09:00
|
|
|
import android.net.MacAddress;
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.net.RouteInfo;
|
2023-08-25 12:50:37 +09:00
|
|
|
import android.net.RoutingCoordinatorManager;
|
2020-02-13 09:16:19 +09:00
|
|
|
import android.net.TetheredClient;
|
2019-12-25 19:40:32 +08:00
|
|
|
import android.net.TetheringManager;
|
2020-03-16 21:49:48 +08:00
|
|
|
import android.net.TetheringRequestParcel;
|
2020-02-13 09:16:19 +09:00
|
|
|
import android.net.dhcp.DhcpLeaseParcelable;
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.net.dhcp.DhcpServerCallbacks;
|
|
|
|
|
import android.net.dhcp.DhcpServingParamsParcel;
|
|
|
|
|
import android.net.dhcp.DhcpServingParamsParcelExt;
|
2020-04-03 17:01:33 +09:00
|
|
|
import android.net.dhcp.IDhcpEventCallbacks;
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.net.dhcp.IDhcpServer;
|
|
|
|
|
import android.net.ip.RouterAdvertisementDaemon.RaParams;
|
2020-02-14 01:06:35 +09:00
|
|
|
import android.os.Handler;
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.os.Message;
|
|
|
|
|
import android.os.RemoteException;
|
|
|
|
|
import android.os.ServiceSpecificException;
|
2023-11-08 14:50:15 +00:00
|
|
|
import android.util.ArraySet;
|
2019-09-09 20:50:49 +08:00
|
|
|
import android.util.Log;
|
|
|
|
|
import android.util.SparseArray;
|
|
|
|
|
|
2020-02-13 09:16:19 +09:00
|
|
|
import androidx.annotation.NonNull;
|
2020-04-09 10:13:44 +09:00
|
|
|
import androidx.annotation.Nullable;
|
2020-02-13 09:16:19 +09:00
|
|
|
|
2023-05-23 22:37:17 +08:00
|
|
|
import com.android.internal.annotations.VisibleForTesting;
|
2019-09-09 20:50:49 +08:00
|
|
|
import com.android.internal.util.MessageUtils;
|
|
|
|
|
import com.android.internal.util.State;
|
2021-08-12 13:56:43 +08:00
|
|
|
import com.android.modules.utils.build.SdkLevel;
|
2022-03-04 15:14:27 +01:00
|
|
|
import com.android.net.module.util.InterfaceParams;
|
2021-10-29 15:57:22 +09:00
|
|
|
import com.android.net.module.util.NetdUtils;
|
2023-08-25 12:50:37 +09:00
|
|
|
import com.android.net.module.util.SdkUtil.LateSdk;
|
2022-06-16 16:38:43 -07:00
|
|
|
import com.android.net.module.util.SharedLog;
|
2022-06-17 14:58:03 +09:00
|
|
|
import com.android.net.module.util.ip.InterfaceController;
|
|
|
|
|
import com.android.net.module.util.ip.IpNeighborMonitor;
|
|
|
|
|
import com.android.net.module.util.ip.IpNeighborMonitor.NeighborEvent;
|
2020-03-12 21:24:01 +08:00
|
|
|
import com.android.networkstack.tethering.BpfCoordinator;
|
2020-12-21 19:39:49 +08:00
|
|
|
import com.android.networkstack.tethering.BpfCoordinator.ClientInfo;
|
2023-08-15 07:13:00 +00:00
|
|
|
import com.android.networkstack.tethering.BpfCoordinator.Ipv6DownstreamRule;
|
2020-02-12 00:19:21 +08:00
|
|
|
import com.android.networkstack.tethering.PrivateAddressCoordinator;
|
2022-02-26 00:39:06 +08:00
|
|
|
import com.android.networkstack.tethering.TetheringConfiguration;
|
2022-03-14 18:04:33 +08:00
|
|
|
import com.android.networkstack.tethering.metrics.TetheringMetrics;
|
2021-11-04 11:26:03 +08:00
|
|
|
import com.android.networkstack.tethering.util.InterfaceSet;
|
|
|
|
|
import com.android.networkstack.tethering.util.PrefixUtils;
|
2022-12-21 06:58:23 +00:00
|
|
|
import com.android.networkstack.tethering.util.StateMachineShim;
|
|
|
|
|
import com.android.networkstack.tethering.util.SyncStateMachine.StateInfo;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
import java.net.Inet4Address;
|
|
|
|
|
import java.net.Inet6Address;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.util.ArrayList;
|
2020-02-13 09:16:19 +09:00
|
|
|
import java.util.Arrays;
|
2023-05-23 22:37:17 +08:00
|
|
|
import java.util.Collection;
|
2020-02-13 09:16:19 +09:00
|
|
|
import java.util.Collections;
|
2019-09-09 20:50:49 +08:00
|
|
|
import java.util.HashSet;
|
2020-02-13 09:16:19 +09:00
|
|
|
import java.util.List;
|
2019-09-09 20:50:49 +08:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides the interface to IP-layer serving functionality for a given network
|
|
|
|
|
* interface, e.g. for tethering or "local-only hotspot" mode.
|
|
|
|
|
*
|
|
|
|
|
* @hide
|
|
|
|
|
*/
|
2022-12-21 06:58:23 +00:00
|
|
|
public class IpServer extends StateMachineShim {
|
2019-09-09 20:50:49 +08:00
|
|
|
public static final int STATE_UNAVAILABLE = 0;
|
|
|
|
|
public static final int STATE_AVAILABLE = 1;
|
|
|
|
|
public static final int STATE_TETHERED = 2;
|
|
|
|
|
public static final int STATE_LOCAL_ONLY = 3;
|
|
|
|
|
|
|
|
|
|
/** Get string name of |state|.*/
|
|
|
|
|
public static String getStateString(int state) {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case STATE_UNAVAILABLE: return "UNAVAILABLE";
|
|
|
|
|
case STATE_AVAILABLE: return "AVAILABLE";
|
|
|
|
|
case STATE_TETHERED: return "TETHERED";
|
|
|
|
|
case STATE_LOCAL_ONLY: return "LOCAL_ONLY";
|
|
|
|
|
}
|
|
|
|
|
return "UNKNOWN: " + state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final byte DOUG_ADAMS = (byte) 42;
|
|
|
|
|
|
|
|
|
|
// TODO: have PanService use some visible version of this constant
|
2020-02-12 00:19:21 +08:00
|
|
|
private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1/24";
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
// TODO: have this configurable
|
|
|
|
|
private static final int DHCP_LEASE_TIME_SECS = 3600;
|
|
|
|
|
|
2023-08-16 13:19:33 +00:00
|
|
|
private static final int NO_UPSTREAM = 0;
|
2020-04-14 14:57:30 +09:00
|
|
|
private static final MacAddress NULL_MAC_ADDRESS = MacAddress.fromString("00:00:00:00:00:00");
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
private static final String TAG = "IpServer";
|
2024-04-02 03:24:57 +00:00
|
|
|
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
|
|
|
|
|
private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
|
2019-09-09 20:50:49 +08:00
|
|
|
private static final Class[] sMessageClasses = {
|
|
|
|
|
IpServer.class
|
|
|
|
|
};
|
|
|
|
|
private static final SparseArray<String> sMagicDecoderRing =
|
|
|
|
|
MessageUtils.findMessageNames(sMessageClasses);
|
|
|
|
|
|
|
|
|
|
/** IpServer callback. */
|
|
|
|
|
public static class Callback {
|
|
|
|
|
/**
|
|
|
|
|
* Notify that |who| has changed its tethering state.
|
|
|
|
|
*
|
|
|
|
|
* @param who the calling instance of IpServer
|
|
|
|
|
* @param state one of STATE_*
|
2019-12-25 19:40:32 +08:00
|
|
|
* @param lastError one of TetheringManager.TETHER_ERROR_*
|
2019-09-09 20:50:49 +08:00
|
|
|
*/
|
2019-12-16 20:15:20 +08:00
|
|
|
public void updateInterfaceState(IpServer who, int state, int lastError) { }
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify that |who| has new LinkProperties.
|
|
|
|
|
*
|
|
|
|
|
* @param who the calling instance of IpServer
|
|
|
|
|
* @param newLp the new LinkProperties to report
|
|
|
|
|
*/
|
2019-12-16 20:15:20 +08:00
|
|
|
public void updateLinkProperties(IpServer who, LinkProperties newLp) { }
|
2020-02-13 09:16:19 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify that the DHCP leases changed in one of the IpServers.
|
|
|
|
|
*/
|
|
|
|
|
public void dhcpLeasesChanged() { }
|
2020-02-12 00:19:21 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Request Tethering change.
|
|
|
|
|
*
|
|
|
|
|
* @param tetheringType the downstream type of this IpServer.
|
|
|
|
|
* @param enabled enable or disable tethering.
|
|
|
|
|
*/
|
|
|
|
|
public void requestEnableTethering(int tetheringType, boolean enabled) { }
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Capture IpServer dependencies, for injection. */
|
2019-12-16 20:15:20 +08:00
|
|
|
public abstract static class Dependencies {
|
2020-03-13 11:38:38 -07:00
|
|
|
/**
|
|
|
|
|
* Create a DadProxy instance to be used by IpServer.
|
|
|
|
|
* To support multiple tethered interfaces concurrently DAD Proxy
|
|
|
|
|
* needs to be supported per IpServer instead of per upstream.
|
|
|
|
|
*/
|
|
|
|
|
public DadProxy getDadProxy(Handler handler, InterfaceParams ifParams) {
|
|
|
|
|
return new DadProxy(handler, ifParams);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:06:35 +09:00
|
|
|
/** Create an IpNeighborMonitor to be used by this IpServer */
|
|
|
|
|
public IpNeighborMonitor getIpNeighborMonitor(Handler handler, SharedLog log,
|
|
|
|
|
IpNeighborMonitor.NeighborEventConsumer consumer) {
|
|
|
|
|
return new IpNeighborMonitor(handler, log, consumer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
/** Create a RouterAdvertisementDaemon instance to be used by IpServer.*/
|
|
|
|
|
public RouterAdvertisementDaemon getRouterAdvertisementDaemon(InterfaceParams ifParams) {
|
|
|
|
|
return new RouterAdvertisementDaemon(ifParams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Get |ifName|'s interface information.*/
|
|
|
|
|
public InterfaceParams getInterfaceParams(String ifName) {
|
|
|
|
|
return InterfaceParams.getByName(ifName);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 20:15:20 +08:00
|
|
|
/** Create a DhcpServer instance to be used by IpServer. */
|
|
|
|
|
public abstract void makeDhcpServer(String ifName, DhcpServingParamsParcel params,
|
|
|
|
|
DhcpServerCallbacks cb);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request from the user that it wants to tether
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_TETHER_REQUESTED = BASE_IPSERVER + 1;
|
2019-09-09 20:50:49 +08:00
|
|
|
// request from the user that it wants to untether
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_TETHER_UNREQUESTED = BASE_IPSERVER + 2;
|
2019-09-09 20:50:49 +08:00
|
|
|
// notification that this interface is down
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_INTERFACE_DOWN = BASE_IPSERVER + 3;
|
2020-07-29 12:05:04 +08:00
|
|
|
// notification from the {@link Tethering.TetherMainSM} that it had trouble enabling IP
|
|
|
|
|
// Forwarding
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_IP_FORWARDING_ENABLE_ERROR = BASE_IPSERVER + 4;
|
2020-07-29 12:05:04 +08:00
|
|
|
// notification from the {@link Tethering.TetherMainSM} SM that it had trouble disabling IP
|
|
|
|
|
// Forwarding
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_IP_FORWARDING_DISABLE_ERROR = BASE_IPSERVER + 5;
|
2020-07-29 12:05:04 +08:00
|
|
|
// notification from the {@link Tethering.TetherMainSM} SM that it had trouble starting
|
|
|
|
|
// tethering
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_START_TETHERING_ERROR = BASE_IPSERVER + 6;
|
2020-07-29 12:05:04 +08:00
|
|
|
// notification from the {@link Tethering.TetherMainSM} that it had trouble stopping tethering
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_STOP_TETHERING_ERROR = BASE_IPSERVER + 7;
|
2020-07-29 12:05:04 +08:00
|
|
|
// notification from the {@link Tethering.TetherMainSM} that it had trouble setting the DNS
|
|
|
|
|
// forwarders
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_SET_DNS_FORWARDERS_ERROR = BASE_IPSERVER + 8;
|
2019-09-09 20:50:49 +08:00
|
|
|
// the upstream connection has changed
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_TETHER_CONNECTION_CHANGED = BASE_IPSERVER + 9;
|
2019-09-09 20:50:49 +08:00
|
|
|
// new IPv6 tethering parameters need to be processed
|
2019-12-06 15:24:53 +08:00
|
|
|
public static final int CMD_IPV6_TETHER_UPDATE = BASE_IPSERVER + 10;
|
2020-02-14 01:06:35 +09:00
|
|
|
// new neighbor cache entry on our interface
|
|
|
|
|
public static final int CMD_NEIGHBOR_EVENT = BASE_IPSERVER + 11;
|
2020-04-09 10:13:44 +09:00
|
|
|
// request from DHCP server that it wants to have a new prefix
|
|
|
|
|
public static final int CMD_NEW_PREFIX_REQUEST = BASE_IPSERVER + 12;
|
2020-02-12 00:19:21 +08:00
|
|
|
// request from PrivateAddressCoordinator to restart tethering.
|
|
|
|
|
public static final int CMD_NOTIFY_PREFIX_CONFLICT = BASE_IPSERVER + 13;
|
2023-09-19 06:56:14 +00:00
|
|
|
public static final int CMD_SERVICE_FAILED_TO_START = BASE_IPSERVER + 14;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
private final State mInitialState;
|
|
|
|
|
private final State mLocalHotspotState;
|
|
|
|
|
private final State mTetheredState;
|
|
|
|
|
private final State mUnavailableState;
|
2020-02-12 00:19:21 +08:00
|
|
|
private final State mWaitingForRestartState;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
private final SharedLog mLog;
|
|
|
|
|
private final INetd mNetd;
|
2020-03-12 21:24:01 +08:00
|
|
|
@NonNull
|
|
|
|
|
private final BpfCoordinator mBpfCoordinator;
|
2023-08-25 12:50:37 +09:00
|
|
|
// Contains null if the connectivity module is unsupported, as the routing coordinator is not
|
|
|
|
|
// available. Must use LateSdk because MessageUtils enumerates fields in this class, so it
|
|
|
|
|
// must be able to find all classes at runtime.
|
|
|
|
|
@NonNull
|
|
|
|
|
private final LateSdk<RoutingCoordinatorManager> mRoutingCoordinator;
|
2019-09-09 20:50:49 +08:00
|
|
|
private final Callback mCallback;
|
|
|
|
|
private final InterfaceController mInterfaceCtrl;
|
2020-02-12 00:19:21 +08:00
|
|
|
private final PrivateAddressCoordinator mPrivateAddressCoordinator;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
private final String mIfaceName;
|
|
|
|
|
private final int mInterfaceType;
|
|
|
|
|
private final LinkProperties mLinkProperties;
|
|
|
|
|
private final boolean mUsingLegacyDhcp;
|
2022-02-25 23:14:58 +08:00
|
|
|
private final int mP2pLeasesSubnetPrefixLength;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
private final Dependencies mDeps;
|
|
|
|
|
|
|
|
|
|
private int mLastError;
|
|
|
|
|
private int mServingMode;
|
|
|
|
|
private InterfaceSet mUpstreamIfaceSet; // may change over time
|
2023-08-16 13:19:33 +00:00
|
|
|
// mInterfaceParams can't be final now because IpServer will be created when receives
|
|
|
|
|
// WIFI_AP_STATE_CHANGED broadcasts or when it detects that the wifi interface has come up.
|
|
|
|
|
// In the latter case, the interface is not fully initialized and the MAC address might not
|
|
|
|
|
// be correct (it will be set with a randomized MAC address later).
|
|
|
|
|
// TODO: Consider create the IpServer only when tethering want to enable it, then we can
|
|
|
|
|
// make mInterfaceParams final.
|
2019-09-09 20:50:49 +08:00
|
|
|
private InterfaceParams mInterfaceParams;
|
|
|
|
|
// TODO: De-duplicate this with mLinkProperties above. Currently, these link
|
|
|
|
|
// properties are those selected by the IPv6TetheringCoordinator and relayed
|
|
|
|
|
// to us. By comparison, mLinkProperties contains the addresses and directly
|
|
|
|
|
// connected routes that have been formed from these properties iff. we have
|
|
|
|
|
// succeeded in configuring them and are able to announce them within Router
|
|
|
|
|
// Advertisements (otherwise, we do not add them to mLinkProperties at all).
|
|
|
|
|
private LinkProperties mLastIPv6LinkProperties;
|
|
|
|
|
private RouterAdvertisementDaemon mRaDaemon;
|
2020-03-13 11:38:38 -07:00
|
|
|
private DadProxy mDadProxy;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
// To be accessed only on the handler thread
|
|
|
|
|
private int mDhcpServerStartIndex = 0;
|
|
|
|
|
private IDhcpServer mDhcpServer;
|
|
|
|
|
private RaParams mLastRaParams;
|
2020-03-16 21:49:48 +08:00
|
|
|
|
|
|
|
|
private LinkAddress mStaticIpv4ServerAddr;
|
|
|
|
|
private LinkAddress mStaticIpv4ClientAddr;
|
|
|
|
|
|
2020-02-13 09:16:19 +09:00
|
|
|
@NonNull
|
|
|
|
|
private List<TetheredClient> mDhcpLeases = Collections.emptyList();
|
2019-09-09 20:50:49 +08:00
|
|
|
|
2020-02-14 01:06:35 +09:00
|
|
|
private int mLastIPv6UpstreamIfindex = 0;
|
2023-08-17 14:34:31 +00:00
|
|
|
private boolean mUpstreamSupportsBpf = false;
|
2023-05-23 22:37:17 +08:00
|
|
|
@NonNull
|
|
|
|
|
private Set<IpPrefix> mLastIPv6UpstreamPrefixes = Collections.emptySet();
|
2020-02-14 01:06:35 +09:00
|
|
|
|
|
|
|
|
private class MyNeighborEventConsumer implements IpNeighborMonitor.NeighborEventConsumer {
|
|
|
|
|
public void accept(NeighborEvent e) {
|
|
|
|
|
sendMessage(CMD_NEIGHBOR_EVENT, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final IpNeighborMonitor mIpNeighborMonitor;
|
|
|
|
|
|
2020-02-12 00:19:21 +08:00
|
|
|
private LinkAddress mIpv4Address;
|
|
|
|
|
|
2022-03-14 18:04:33 +08:00
|
|
|
private final TetheringMetrics mTetheringMetrics;
|
2022-12-21 06:58:23 +00:00
|
|
|
private final Handler mHandler;
|
2022-03-14 18:04:33 +08:00
|
|
|
|
2020-05-12 19:15:24 +08:00
|
|
|
// TODO: Add a dependency object to pass the data members or variables from the tethering
|
|
|
|
|
// object. It helps to reduce the arguments of the constructor.
|
2019-09-09 20:50:49 +08:00
|
|
|
public IpServer(
|
2022-12-21 06:58:23 +00:00
|
|
|
String ifaceName, Handler handler, int interfaceType, SharedLog log,
|
2023-08-25 12:50:37 +09:00
|
|
|
INetd netd, @NonNull BpfCoordinator bpfCoordinator,
|
|
|
|
|
@Nullable LateSdk<RoutingCoordinatorManager> routingCoordinator, Callback callback,
|
2022-02-26 00:39:06 +08:00
|
|
|
TetheringConfiguration config, PrivateAddressCoordinator addressCoordinator,
|
2022-03-14 18:04:33 +08:00
|
|
|
TetheringMetrics tetheringMetrics, Dependencies deps) {
|
2024-01-22 11:09:55 +00:00
|
|
|
super(ifaceName, USE_SYNC_SM ? null : handler.getLooper());
|
2022-12-21 06:58:23 +00:00
|
|
|
mHandler = handler;
|
2019-09-09 20:50:49 +08:00
|
|
|
mLog = log.forSubComponent(ifaceName);
|
2020-01-07 14:43:17 +08:00
|
|
|
mNetd = netd;
|
2023-08-25 12:50:37 +09:00
|
|
|
mBpfCoordinator = bpfCoordinator;
|
|
|
|
|
mRoutingCoordinator = routingCoordinator;
|
2019-09-09 20:50:49 +08:00
|
|
|
mCallback = callback;
|
|
|
|
|
mInterfaceCtrl = new InterfaceController(ifaceName, mNetd, mLog);
|
|
|
|
|
mIfaceName = ifaceName;
|
|
|
|
|
mInterfaceType = interfaceType;
|
|
|
|
|
mLinkProperties = new LinkProperties();
|
2022-02-26 00:39:06 +08:00
|
|
|
mUsingLegacyDhcp = config.useLegacyDhcpServer();
|
2022-02-25 23:14:58 +08:00
|
|
|
mP2pLeasesSubnetPrefixLength = config.getP2pLeasesSubnetPrefixLength();
|
2020-02-12 00:19:21 +08:00
|
|
|
mPrivateAddressCoordinator = addressCoordinator;
|
2019-09-09 20:50:49 +08:00
|
|
|
mDeps = deps;
|
2022-03-14 18:04:33 +08:00
|
|
|
mTetheringMetrics = tetheringMetrics;
|
2019-09-09 20:50:49 +08:00
|
|
|
resetLinkProperties();
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_NO_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
mServingMode = STATE_AVAILABLE;
|
|
|
|
|
|
2020-02-14 01:06:35 +09:00
|
|
|
mIpNeighborMonitor = mDeps.getIpNeighborMonitor(getHandler(), mLog,
|
|
|
|
|
new MyNeighborEventConsumer());
|
2020-04-12 14:27:18 +08:00
|
|
|
|
2023-08-28 09:57:34 +00:00
|
|
|
// IP neighbor monitor monitors the neighbor events for adding/removing IPv6 downstream rule
|
|
|
|
|
// per client. If BPF offload is not supported, don't start listening for neighbor events.
|
|
|
|
|
if (mBpfCoordinator.isUsingBpfOffload() && !mIpNeighborMonitor.start()) {
|
2020-05-12 19:15:24 +08:00
|
|
|
mLog.e("Failed to create IpNeighborMonitor on " + mIfaceName);
|
2020-02-14 01:06:35 +09:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
mInitialState = new InitialState();
|
|
|
|
|
mLocalHotspotState = new LocalHotspotState();
|
|
|
|
|
mTetheredState = new TetheredState();
|
|
|
|
|
mUnavailableState = new UnavailableState();
|
2020-02-12 00:19:21 +08:00
|
|
|
mWaitingForRestartState = new WaitingForRestartState();
|
2022-12-21 06:58:23 +00:00
|
|
|
final ArrayList allStates = new ArrayList<StateInfo>();
|
|
|
|
|
allStates.add(new StateInfo(mInitialState, null));
|
|
|
|
|
allStates.add(new StateInfo(mLocalHotspotState, null));
|
|
|
|
|
allStates.add(new StateInfo(mTetheredState, null));
|
|
|
|
|
allStates.add(new StateInfo(mWaitingForRestartState, mTetheredState));
|
|
|
|
|
allStates.add(new StateInfo(mUnavailableState, null));
|
|
|
|
|
addAllStates(allStates);
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
|
2022-12-21 06:58:23 +00:00
|
|
|
private Handler getHandler() {
|
|
|
|
|
return mHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Start IpServer state machine. */
|
|
|
|
|
public void start() {
|
|
|
|
|
start(mInitialState);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Interface name which IpServer served.*/
|
|
|
|
|
public String interfaceName() {
|
|
|
|
|
return mIfaceName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-12-25 19:40:32 +08:00
|
|
|
* Tethering downstream type. It would be one of TetheringManager#TETHERING_*.
|
2019-09-09 20:50:49 +08:00
|
|
|
*/
|
|
|
|
|
public int interfaceType() {
|
|
|
|
|
return mInterfaceType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Last error from this IpServer. */
|
|
|
|
|
public int lastError() {
|
|
|
|
|
return mLastError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Serving mode is the current state of IpServer state machine. */
|
|
|
|
|
public int servingMode() {
|
|
|
|
|
return mServingMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The properties of the network link which IpServer is serving. */
|
|
|
|
|
public LinkProperties linkProperties() {
|
|
|
|
|
return new LinkProperties(mLinkProperties);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 00:19:21 +08:00
|
|
|
/** The address which IpServer is using. */
|
|
|
|
|
public LinkAddress getAddress() {
|
|
|
|
|
return mIpv4Address;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 09:16:19 +09:00
|
|
|
/**
|
|
|
|
|
* Get the latest list of DHCP leases that was reported. Must be called on the IpServer looper
|
|
|
|
|
* thread.
|
|
|
|
|
*/
|
|
|
|
|
public List<TetheredClient> getAllLeases() {
|
|
|
|
|
return Collections.unmodifiableList(mDhcpLeases);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 13:46:32 +00:00
|
|
|
/** Enable this IpServer. IpServer state machine will be tethered or localHotspot state. */
|
|
|
|
|
public void enable(final int requestedState, final TetheringRequestParcel request) {
|
|
|
|
|
sendMessage(CMD_TETHER_REQUESTED, requestedState, 0, request);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
/** Stop this IpServer. After this is called this IpServer should not be used any more. */
|
|
|
|
|
public void stop() {
|
|
|
|
|
sendMessage(CMD_INTERFACE_DOWN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tethering is canceled. IpServer state machine will be available and wait for
|
|
|
|
|
* next tethering request.
|
|
|
|
|
*/
|
|
|
|
|
public void unwanted() {
|
|
|
|
|
sendMessage(CMD_TETHER_UNREQUESTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Internals. */
|
|
|
|
|
|
2023-03-15 06:45:04 +00:00
|
|
|
private boolean startIPv4(int scope) {
|
|
|
|
|
return configureIPv4(true, scope);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience wrapper around INetworkStackStatusCallback to run callbacks on the IpServer
|
|
|
|
|
* handler.
|
|
|
|
|
*
|
|
|
|
|
* <p>Different instances of this class can be created for each call to IDhcpServer methods,
|
|
|
|
|
* with different implementations of the callback, to differentiate handling of success/error in
|
|
|
|
|
* each call.
|
|
|
|
|
*/
|
|
|
|
|
private abstract class OnHandlerStatusCallback extends INetworkStackStatusCallback.Stub {
|
|
|
|
|
@Override
|
|
|
|
|
public void onStatusAvailable(int statusCode) {
|
|
|
|
|
getHandler().post(() -> callback(statusCode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void callback(int statusCode);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getInterfaceVersion() {
|
|
|
|
|
return this.VERSION;
|
|
|
|
|
}
|
2020-01-23 14:55:57 +09:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getInterfaceHash() {
|
|
|
|
|
return this.HASH;
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DhcpServerCallbacksImpl extends DhcpServerCallbacks {
|
|
|
|
|
private final int mStartIndex;
|
|
|
|
|
|
|
|
|
|
private DhcpServerCallbacksImpl(int startIndex) {
|
|
|
|
|
mStartIndex = startIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDhcpServerCreated(int statusCode, IDhcpServer server) throws RemoteException {
|
|
|
|
|
getHandler().post(() -> {
|
|
|
|
|
// We are on the handler thread: mDhcpServerStartIndex can be read safely.
|
|
|
|
|
if (mStartIndex != mDhcpServerStartIndex) {
|
2020-07-22 21:28:48 +08:00
|
|
|
// This start request is obsolete. Explicitly stop the DHCP server to shut
|
|
|
|
|
// down its thread. When the |server| binder token goes out of scope, the
|
|
|
|
|
// garbage collector will finalize it, which causes the network stack process
|
|
|
|
|
// garbage collector to collect the server itself.
|
|
|
|
|
try {
|
|
|
|
|
server.stop(null);
|
|
|
|
|
} catch (RemoteException e) { }
|
2019-09-09 20:50:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (statusCode != STATUS_SUCCESS) {
|
|
|
|
|
mLog.e("Error obtaining DHCP server: " + statusCode);
|
|
|
|
|
handleError();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mDhcpServer = server;
|
|
|
|
|
try {
|
2020-02-13 09:16:19 +09:00
|
|
|
mDhcpServer.startWithCallbacks(new OnHandlerStatusCallback() {
|
2019-09-09 20:50:49 +08:00
|
|
|
@Override
|
|
|
|
|
public void callback(int startStatusCode) {
|
|
|
|
|
if (startStatusCode != STATUS_SUCCESS) {
|
|
|
|
|
mLog.e("Error starting DHCP server: " + startStatusCode);
|
|
|
|
|
handleError();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-09 10:13:44 +09:00
|
|
|
}, new DhcpEventCallback());
|
2019-09-09 20:50:49 +08:00
|
|
|
} catch (RemoteException e) {
|
2020-01-07 14:43:17 +08:00
|
|
|
throw new IllegalStateException(e);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleError() {
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_DHCPSERVER_ERROR;
|
2024-01-22 11:09:55 +00:00
|
|
|
if (USE_SYNC_SM) {
|
2022-12-21 16:39:23 +00:00
|
|
|
sendMessage(CMD_SERVICE_FAILED_TO_START, TETHER_ERROR_DHCPSERVER_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
sendMessageAtFrontOfQueueToAsyncSM(CMD_SERVICE_FAILED_TO_START,
|
|
|
|
|
TETHER_ERROR_DHCPSERVER_ERROR);
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 10:13:44 +09:00
|
|
|
private class DhcpEventCallback extends IDhcpEventCallbacks.Stub {
|
2020-02-13 09:16:19 +09:00
|
|
|
@Override
|
|
|
|
|
public void onLeasesChanged(List<DhcpLeaseParcelable> leaseParcelables) {
|
|
|
|
|
final ArrayList<TetheredClient> leases = new ArrayList<>();
|
|
|
|
|
for (DhcpLeaseParcelable lease : leaseParcelables) {
|
|
|
|
|
final LinkAddress address = new LinkAddress(
|
2020-03-09 15:38:45 +09:00
|
|
|
intToInet4AddressHTH(lease.netAddr), lease.prefixLength,
|
|
|
|
|
0 /* flags */, RT_SCOPE_UNIVERSE /* as per RFC6724#3.2 */,
|
|
|
|
|
lease.expTime /* deprecationTime */, lease.expTime /* expirationTime */);
|
2020-02-13 09:16:19 +09:00
|
|
|
|
|
|
|
|
final MacAddress macAddress;
|
|
|
|
|
try {
|
|
|
|
|
macAddress = MacAddress.fromBytes(lease.hwAddr);
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
Log.wtf(TAG, "Invalid address received from DhcpServer: "
|
|
|
|
|
+ Arrays.toString(lease.hwAddr));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo(
|
2020-03-09 15:38:45 +09:00
|
|
|
address, lease.hostname);
|
2020-02-13 09:16:19 +09:00
|
|
|
leases.add(new TetheredClient(
|
|
|
|
|
macAddress,
|
|
|
|
|
Collections.singletonList(addressInfo),
|
|
|
|
|
mInterfaceType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getHandler().post(() -> {
|
|
|
|
|
mDhcpLeases = leases;
|
|
|
|
|
mCallback.dhcpLeasesChanged();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 17:01:33 +09:00
|
|
|
@Override
|
2020-04-09 10:13:44 +09:00
|
|
|
public void onNewPrefixRequest(@NonNull final IpPrefix currentPrefix) {
|
|
|
|
|
Objects.requireNonNull(currentPrefix);
|
|
|
|
|
sendMessage(CMD_NEW_PREFIX_REQUEST, currentPrefix);
|
2020-04-03 17:01:33 +09:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 09:16:19 +09:00
|
|
|
@Override
|
|
|
|
|
public int getInterfaceVersion() {
|
|
|
|
|
return this.VERSION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getInterfaceHash() throws RemoteException {
|
|
|
|
|
return this.HASH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 10:13:44 +09:00
|
|
|
private RouteInfo getDirectConnectedRoute(@NonNull final LinkAddress ipv4Address) {
|
|
|
|
|
Objects.requireNonNull(ipv4Address);
|
|
|
|
|
return new RouteInfo(PrefixUtils.asIpPrefix(ipv4Address), null, mIfaceName, RTN_UNICAST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DhcpServingParamsParcel makeServingParams(@NonNull final Inet4Address defaultRouter,
|
|
|
|
|
@NonNull final Inet4Address dnsServer, @NonNull LinkAddress serverAddr,
|
|
|
|
|
@Nullable Inet4Address clientAddr) {
|
|
|
|
|
final boolean changePrefixOnDecline =
|
|
|
|
|
(mInterfaceType == TetheringManager.TETHERING_NCM && clientAddr == null);
|
2022-02-25 23:14:58 +08:00
|
|
|
final int subnetPrefixLength = mInterfaceType == TetheringManager.TETHERING_WIFI_P2P
|
|
|
|
|
? mP2pLeasesSubnetPrefixLength : 0 /* default value */;
|
|
|
|
|
|
2020-04-09 10:13:44 +09:00
|
|
|
return new DhcpServingParamsParcelExt()
|
|
|
|
|
.setDefaultRouters(defaultRouter)
|
|
|
|
|
.setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS)
|
|
|
|
|
.setDnsServers(dnsServer)
|
|
|
|
|
.setServerAddr(serverAddr)
|
|
|
|
|
.setMetered(true)
|
|
|
|
|
.setSingleClientAddr(clientAddr)
|
2022-02-25 23:14:58 +08:00
|
|
|
.setChangePrefixOnDecline(changePrefixOnDecline)
|
|
|
|
|
.setLeasesSubnetPrefixLength(subnetPrefixLength);
|
2020-04-09 10:13:44 +09:00
|
|
|
// TODO: also advertise link MTU
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 20:27:18 +08:00
|
|
|
private boolean startDhcp(final LinkAddress serverLinkAddr, final LinkAddress clientLinkAddr) {
|
2019-09-09 20:50:49 +08:00
|
|
|
if (mUsingLegacyDhcp) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-27 20:27:18 +08:00
|
|
|
|
|
|
|
|
final Inet4Address addr = (Inet4Address) serverLinkAddr.getAddress();
|
|
|
|
|
final Inet4Address clientAddr = clientLinkAddr == null ? null :
|
|
|
|
|
(Inet4Address) clientLinkAddr.getAddress();
|
|
|
|
|
|
2020-04-09 10:13:44 +09:00
|
|
|
final DhcpServingParamsParcel params = makeServingParams(addr /* defaultRouter */,
|
|
|
|
|
addr /* dnsServer */, serverLinkAddr, clientAddr);
|
2019-09-09 20:50:49 +08:00
|
|
|
mDhcpServerStartIndex++;
|
|
|
|
|
mDeps.makeDhcpServer(
|
|
|
|
|
mIfaceName, params, new DhcpServerCallbacksImpl(mDhcpServerStartIndex));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopDhcp() {
|
|
|
|
|
// Make all previous start requests obsolete so servers are not started later
|
|
|
|
|
mDhcpServerStartIndex++;
|
|
|
|
|
|
|
|
|
|
if (mDhcpServer != null) {
|
|
|
|
|
try {
|
|
|
|
|
mDhcpServer.stop(new OnHandlerStatusCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void callback(int statusCode) {
|
|
|
|
|
if (statusCode != STATUS_SUCCESS) {
|
|
|
|
|
mLog.e("Error stopping DHCP server: " + statusCode);
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_DHCPSERVER_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
// Not much more we can do here
|
|
|
|
|
}
|
2020-02-13 09:16:19 +09:00
|
|
|
mDhcpLeases.clear();
|
|
|
|
|
getHandler().post(mCallback::dhcpLeasesChanged);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mDhcpServer = null;
|
|
|
|
|
} catch (RemoteException e) {
|
2020-04-09 10:13:44 +09:00
|
|
|
mLog.e("Error stopping DHCP server", e);
|
2020-01-07 14:43:17 +08:00
|
|
|
// Not much more we can do here
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 20:27:18 +08:00
|
|
|
private boolean configureDhcp(boolean enable, final LinkAddress serverAddr,
|
|
|
|
|
final LinkAddress clientAddr) {
|
2019-09-09 20:50:49 +08:00
|
|
|
if (enable) {
|
2020-02-27 20:27:18 +08:00
|
|
|
return startDhcp(serverAddr, clientAddr);
|
2019-09-09 20:50:49 +08:00
|
|
|
} else {
|
|
|
|
|
stopDhcp();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopIPv4() {
|
2023-03-15 06:45:04 +00:00
|
|
|
configureIPv4(false /* enabled */, CONNECTIVITY_SCOPE_GLOBAL /* not used */);
|
2019-09-09 20:50:49 +08:00
|
|
|
// NOTE: All of configureIPv4() will be refactored out of existence
|
|
|
|
|
// into calls to InterfaceController, shared with startIPv4().
|
|
|
|
|
mInterfaceCtrl.clearIPv4Address();
|
2020-02-12 00:19:21 +08:00
|
|
|
mPrivateAddressCoordinator.releaseDownstream(this);
|
2021-02-08 17:15:35 +08:00
|
|
|
mBpfCoordinator.tetherOffloadClientClear(this);
|
2020-01-07 14:43:17 +08:00
|
|
|
mIpv4Address = null;
|
2020-03-16 21:49:48 +08:00
|
|
|
mStaticIpv4ServerAddr = null;
|
|
|
|
|
mStaticIpv4ClientAddr = null;
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 06:45:04 +00:00
|
|
|
private boolean configureIPv4(boolean enabled, int scope) {
|
2019-09-09 20:50:49 +08:00
|
|
|
if (VDBG) Log.d(TAG, "configureIPv4(" + enabled + ")");
|
|
|
|
|
|
2020-02-12 00:19:21 +08:00
|
|
|
if (enabled) {
|
2023-03-15 06:45:04 +00:00
|
|
|
mIpv4Address = requestIpv4Address(scope, true /* useLastAddress */);
|
2020-02-12 00:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mIpv4Address == null) {
|
|
|
|
|
mLog.e("No available ipv4 address");
|
2020-01-07 14:43:17 +08:00
|
|
|
return false;
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 23:58:05 +08:00
|
|
|
if (shouldNotConfigureBluetoothInterface()) {
|
|
|
|
|
// Interface was already configured elsewhere, only start DHCP.
|
2020-02-12 00:19:21 +08:00
|
|
|
return configureDhcp(enabled, mIpv4Address, null /* clientAddress */);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final IpPrefix ipv4Prefix = asIpPrefix(mIpv4Address);
|
|
|
|
|
|
2020-01-07 14:43:17 +08:00
|
|
|
final Boolean setIfaceUp;
|
2019-12-03 11:37:09 +08:00
|
|
|
if (mInterfaceType == TetheringManager.TETHERING_WIFI
|
2020-05-26 03:11:29 +09:00
|
|
|
|| mInterfaceType == TetheringManager.TETHERING_WIFI_P2P
|
2019-11-21 00:36:14 +02:00
|
|
|
|| mInterfaceType == TetheringManager.TETHERING_ETHERNET
|
|
|
|
|
|| mInterfaceType == TetheringManager.TETHERING_WIGIG) {
|
2020-05-26 03:11:29 +09:00
|
|
|
// The WiFi and Ethernet stack has ownership of the interface up/down state.
|
2020-01-07 14:43:17 +08:00
|
|
|
// It is unclear whether the Bluetooth or USB stacks will manage their own
|
|
|
|
|
// state.
|
|
|
|
|
setIfaceUp = null;
|
|
|
|
|
} else {
|
|
|
|
|
setIfaceUp = enabled;
|
|
|
|
|
}
|
|
|
|
|
if (!mInterfaceCtrl.setInterfaceConfiguration(mIpv4Address, setIfaceUp)) {
|
|
|
|
|
mLog.e("Error configuring interface");
|
|
|
|
|
if (!enabled) stopDhcp();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
if (enabled) {
|
2020-01-07 14:43:17 +08:00
|
|
|
mLinkProperties.addLinkAddress(mIpv4Address);
|
2020-04-09 10:13:44 +09:00
|
|
|
mLinkProperties.addRoute(getDirectConnectedRoute(mIpv4Address));
|
2019-09-09 20:50:49 +08:00
|
|
|
} else {
|
2020-01-07 14:43:17 +08:00
|
|
|
mLinkProperties.removeLinkAddress(mIpv4Address);
|
2020-04-09 10:13:44 +09:00
|
|
|
mLinkProperties.removeRoute(getDirectConnectedRoute(mIpv4Address));
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
2020-02-27 20:27:18 +08:00
|
|
|
return configureDhcp(enabled, mIpv4Address, mStaticIpv4ClientAddr);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 23:58:05 +08:00
|
|
|
private boolean shouldNotConfigureBluetoothInterface() {
|
|
|
|
|
// Before T, bluetooth tethering configures the interface elsewhere.
|
|
|
|
|
return (mInterfaceType == TetheringManager.TETHERING_BLUETOOTH) && !SdkLevel.isAtLeastT();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 06:45:04 +00:00
|
|
|
private LinkAddress requestIpv4Address(final int scope, final boolean useLastAddress) {
|
2020-02-12 00:19:21 +08:00
|
|
|
if (mStaticIpv4ServerAddr != null) return mStaticIpv4ServerAddr;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
2021-11-15 23:58:05 +08:00
|
|
|
if (shouldNotConfigureBluetoothInterface()) return new LinkAddress(BLUETOOTH_IFACE_ADDR);
|
2020-02-12 00:19:21 +08:00
|
|
|
|
2023-03-15 06:45:04 +00:00
|
|
|
return mPrivateAddressCoordinator.requestDownstreamAddress(this, scope, useLastAddress);
|
2020-04-09 10:13:44 +09:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
private boolean startIPv6() {
|
|
|
|
|
mInterfaceParams = mDeps.getInterfaceParams(mIfaceName);
|
|
|
|
|
if (mInterfaceParams == null) {
|
|
|
|
|
mLog.e("Failed to find InterfaceParams");
|
|
|
|
|
stopIPv6();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mRaDaemon = mDeps.getRouterAdvertisementDaemon(mInterfaceParams);
|
|
|
|
|
if (!mRaDaemon.start()) {
|
|
|
|
|
stopIPv6();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 13:56:43 +08:00
|
|
|
if (SdkLevel.isAtLeastS()) {
|
2020-03-13 11:38:38 -07:00
|
|
|
// DAD Proxy starts forwarding packets after IPv6 upstream is present.
|
|
|
|
|
mDadProxy = mDeps.getDadProxy(getHandler(), mInterfaceParams);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopIPv6() {
|
|
|
|
|
mInterfaceParams = null;
|
|
|
|
|
setRaParams(null);
|
|
|
|
|
|
|
|
|
|
if (mRaDaemon != null) {
|
|
|
|
|
mRaDaemon.stop();
|
|
|
|
|
mRaDaemon = null;
|
|
|
|
|
}
|
2020-03-13 11:38:38 -07:00
|
|
|
|
|
|
|
|
if (mDadProxy != null) {
|
|
|
|
|
mDadProxy.stop();
|
|
|
|
|
mDadProxy = null;
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IPv6TetheringCoordinator sends updates with carefully curated IPv6-only
|
|
|
|
|
// LinkProperties. These have extraneous data filtered out and only the
|
|
|
|
|
// necessary prefixes included (per its prefix distribution policy).
|
|
|
|
|
//
|
|
|
|
|
// TODO: Evaluate using a data structure than is more directly suited to
|
|
|
|
|
// communicating only the relevant information.
|
2020-05-21 17:38:28 +08:00
|
|
|
private void updateUpstreamIPv6LinkProperties(LinkProperties v6only, int ttlAdjustment) {
|
2019-09-09 20:50:49 +08:00
|
|
|
if (mRaDaemon == null) return;
|
|
|
|
|
|
|
|
|
|
// Avoid unnecessary work on spurious updates.
|
|
|
|
|
if (Objects.equals(mLastIPv6LinkProperties, v6only)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RaParams params = null;
|
2020-03-13 11:38:38 -07:00
|
|
|
String upstreamIface = null;
|
|
|
|
|
InterfaceParams upstreamIfaceParams = null;
|
2023-08-16 13:19:33 +00:00
|
|
|
int upstreamIfIndex = NO_UPSTREAM;
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
if (v6only != null) {
|
2020-03-13 11:38:38 -07:00
|
|
|
upstreamIface = v6only.getInterfaceName();
|
|
|
|
|
upstreamIfaceParams = mDeps.getInterfaceParams(upstreamIface);
|
|
|
|
|
if (upstreamIfaceParams != null) {
|
|
|
|
|
upstreamIfIndex = upstreamIfaceParams.index;
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
params = new RaParams();
|
2020-05-28 03:21:31 -07:00
|
|
|
params.mtu = v6only.getMtu();
|
2019-09-09 20:50:49 +08:00
|
|
|
params.hasDefaultRoute = v6only.hasIpv6DefaultRoute();
|
|
|
|
|
|
2020-05-21 17:38:28 +08:00
|
|
|
if (params.hasDefaultRoute) params.hopLimit = getHopLimit(upstreamIface, ttlAdjustment);
|
2019-09-09 20:50:49 +08:00
|
|
|
|
2023-05-23 22:37:17 +08:00
|
|
|
params.prefixes = getTetherableIpv6Prefixes(v6only);
|
|
|
|
|
for (IpPrefix prefix : params.prefixes) {
|
2019-09-09 20:50:49 +08:00
|
|
|
final Inet6Address dnsServer = getLocalDnsIpFor(prefix);
|
|
|
|
|
if (dnsServer != null) {
|
|
|
|
|
params.dnses.add(dnsServer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-14 01:06:35 +09:00
|
|
|
|
2021-02-09 23:12:37 +09:00
|
|
|
// Add upstream index to name mapping. See the comment of the interface mapping update in
|
|
|
|
|
// CMD_TETHER_CONNECTION_CHANGED. Adding the mapping update here to the avoid potential
|
|
|
|
|
// timing issue. It prevents that the IPv6 capability is updated later than
|
|
|
|
|
// CMD_TETHER_CONNECTION_CHANGED.
|
2023-08-16 13:19:33 +00:00
|
|
|
mBpfCoordinator.maybeAddUpstreamToLookupTable(upstreamIfIndex, upstreamIface);
|
2021-02-09 23:12:37 +09:00
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
// If v6only is null, we pass in null to setRaParams(), which handles
|
|
|
|
|
// deprecation of any existing RA data.
|
|
|
|
|
setRaParams(params);
|
2020-02-14 01:06:35 +09:00
|
|
|
|
2023-08-17 14:34:31 +00:00
|
|
|
// Not support BPF on virtual upstream interface
|
|
|
|
|
final boolean upstreamSupportsBpf = upstreamIface != null && !isVcnInterface(upstreamIface);
|
2023-05-23 22:37:17 +08:00
|
|
|
final Set<IpPrefix> upstreamPrefixes = params != null ? params.prefixes : Set.of();
|
|
|
|
|
updateIpv6ForwardingRules(mLastIPv6UpstreamIfindex, mLastIPv6UpstreamPrefixes,
|
|
|
|
|
upstreamIfIndex, upstreamPrefixes, upstreamSupportsBpf);
|
2023-08-17 14:34:31 +00:00
|
|
|
mLastIPv6LinkProperties = v6only;
|
2020-03-13 11:38:38 -07:00
|
|
|
mLastIPv6UpstreamIfindex = upstreamIfIndex;
|
2023-05-23 22:37:17 +08:00
|
|
|
mLastIPv6UpstreamPrefixes = upstreamPrefixes;
|
2023-08-17 14:34:31 +00:00
|
|
|
mUpstreamSupportsBpf = upstreamSupportsBpf;
|
2020-03-13 11:38:38 -07:00
|
|
|
if (mDadProxy != null) {
|
|
|
|
|
mDadProxy.setUpstreamIface(upstreamIfaceParams);
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 10:13:44 +09:00
|
|
|
private void removeRoutesFromLocalNetwork(@NonNull final List<RouteInfo> toBeRemoved) {
|
2021-10-29 15:57:22 +09:00
|
|
|
final int removalFailures = NetdUtils.removeRoutesFromLocalNetwork(
|
2020-04-09 10:13:44 +09:00
|
|
|
mNetd, toBeRemoved);
|
|
|
|
|
if (removalFailures > 0) {
|
|
|
|
|
mLog.e(String.format("Failed to remove %d IPv6 routes from local table.",
|
|
|
|
|
removalFailures));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (RouteInfo route : toBeRemoved) mLinkProperties.removeRoute(route);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 12:50:37 +09:00
|
|
|
private void addInterfaceToNetwork(final int netId, @NonNull final String ifaceName) {
|
2020-04-09 10:13:44 +09:00
|
|
|
try {
|
2023-11-15 11:51:42 +09:00
|
|
|
if (SdkLevel.isAtLeastS() && null != mRoutingCoordinator.value) {
|
2023-08-25 12:50:37 +09:00
|
|
|
// TODO : remove this call in favor of using the LocalNetworkConfiguration
|
|
|
|
|
// correctly, which will let ConnectivityService do it automatically.
|
|
|
|
|
mRoutingCoordinator.value.addInterfaceToNetwork(netId, ifaceName);
|
|
|
|
|
} else {
|
|
|
|
|
mNetd.networkAddInterface(netId, ifaceName);
|
2020-04-09 10:13:44 +09:00
|
|
|
}
|
|
|
|
|
} catch (ServiceSpecificException | RemoteException e) {
|
|
|
|
|
mLog.e("Failed to add " + mIfaceName + " to local table: ", e);
|
2023-10-06 18:31:08 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addInterfaceForward(@NonNull final String fromIface,
|
|
|
|
|
@NonNull final String toIface) throws ServiceSpecificException, RemoteException {
|
2023-11-15 11:51:42 +09:00
|
|
|
if (SdkLevel.isAtLeastS() && null != mRoutingCoordinator.value) {
|
2023-10-06 18:31:08 +09:00
|
|
|
mRoutingCoordinator.value.addInterfaceForward(fromIface, toIface);
|
|
|
|
|
} else {
|
|
|
|
|
mNetd.tetherAddForward(fromIface, toIface);
|
|
|
|
|
mNetd.ipfwdAddInterfaceForward(fromIface, toIface);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeInterfaceForward(@NonNull final String fromIface,
|
|
|
|
|
@NonNull final String toIface) {
|
2023-11-15 11:51:42 +09:00
|
|
|
if (SdkLevel.isAtLeastS() && null != mRoutingCoordinator.value) {
|
2023-10-06 18:31:08 +09:00
|
|
|
try {
|
|
|
|
|
mRoutingCoordinator.value.removeInterfaceForward(fromIface, toIface);
|
|
|
|
|
} catch (ServiceSpecificException e) {
|
|
|
|
|
mLog.e("Exception in removeInterfaceForward", e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
mNetd.ipfwdRemoveInterfaceForward(fromIface, toIface);
|
|
|
|
|
} catch (RemoteException | ServiceSpecificException e) {
|
|
|
|
|
mLog.e("Exception in ipfwdRemoveInterfaceForward", e);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
mNetd.tetherRemoveForward(fromIface, toIface);
|
|
|
|
|
} catch (RemoteException | ServiceSpecificException e) {
|
|
|
|
|
mLog.e("Exception in disableNat", e);
|
|
|
|
|
}
|
2020-04-09 10:13:44 +09:00
|
|
|
}
|
2023-08-25 12:50:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addRoutesToLocalNetwork(@NonNull final List<RouteInfo> toBeAdded) {
|
|
|
|
|
// It's safe to call addInterfaceToNetwork() even if
|
|
|
|
|
// the interface is already in the local_network.
|
|
|
|
|
addInterfaceToNetwork(INetd.LOCAL_NET_ID, mIfaceName);
|
|
|
|
|
try {
|
|
|
|
|
// Add routes from local network. Note that adding routes that
|
|
|
|
|
// already exist does not cause an error (EEXIST is silently ignored).
|
|
|
|
|
NetdUtils.addRoutesToLocalNetwork(mNetd, mIfaceName, toBeAdded);
|
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
|
mLog.e("Failed to add IPv4/v6 routes to local table: " + e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-09 10:13:44 +09:00
|
|
|
|
|
|
|
|
for (RouteInfo route : toBeAdded) mLinkProperties.addRoute(route);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
private void configureLocalIPv6Routes(
|
2023-11-08 14:50:15 +00:00
|
|
|
ArraySet<IpPrefix> deprecatedPrefixes, ArraySet<IpPrefix> newPrefixes) {
|
2019-09-09 20:50:49 +08:00
|
|
|
// [1] Remove the routes that are deprecated.
|
|
|
|
|
if (!deprecatedPrefixes.isEmpty()) {
|
2020-04-09 10:13:44 +09:00
|
|
|
removeRoutesFromLocalNetwork(getLocalRoutesFor(mIfaceName, deprecatedPrefixes));
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [2] Add only the routes that have not previously been added.
|
|
|
|
|
if (newPrefixes != null && !newPrefixes.isEmpty()) {
|
2023-11-08 14:50:15 +00:00
|
|
|
ArraySet<IpPrefix> addedPrefixes = new ArraySet<IpPrefix>(newPrefixes);
|
2019-09-09 20:50:49 +08:00
|
|
|
if (mLastRaParams != null) {
|
|
|
|
|
addedPrefixes.removeAll(mLastRaParams.prefixes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!addedPrefixes.isEmpty()) {
|
2020-04-09 10:13:44 +09:00
|
|
|
addRoutesToLocalNetwork(getLocalRoutesFor(mIfaceName, addedPrefixes));
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void configureLocalIPv6Dns(
|
2023-11-08 14:50:15 +00:00
|
|
|
ArraySet<Inet6Address> deprecatedDnses, ArraySet<Inet6Address> newDnses) {
|
2019-09-09 20:50:49 +08:00
|
|
|
// TODO: Is this really necessary? Can we not fail earlier if INetd cannot be located?
|
|
|
|
|
if (mNetd == null) {
|
|
|
|
|
if (newDnses != null) newDnses.clear();
|
|
|
|
|
mLog.e("No netd service instance available; not setting local IPv6 addresses");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [1] Remove deprecated local DNS IP addresses.
|
|
|
|
|
if (!deprecatedDnses.isEmpty()) {
|
|
|
|
|
for (Inet6Address dns : deprecatedDnses) {
|
|
|
|
|
if (!mInterfaceCtrl.removeAddress(dns, RFC7421_PREFIX_LENGTH)) {
|
|
|
|
|
mLog.e("Failed to remove local dns IP " + dns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLinkProperties.removeLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [2] Add only the local DNS IP addresses that have not previously been added.
|
|
|
|
|
if (newDnses != null && !newDnses.isEmpty()) {
|
2023-11-08 14:50:15 +00:00
|
|
|
final ArraySet<Inet6Address> addedDnses = new ArraySet<Inet6Address>(newDnses);
|
2019-09-09 20:50:49 +08:00
|
|
|
if (mLastRaParams != null) {
|
|
|
|
|
addedDnses.removeAll(mLastRaParams.dnses);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Inet6Address dns : addedDnses) {
|
|
|
|
|
if (!mInterfaceCtrl.addAddress(dns, RFC7421_PREFIX_LENGTH)) {
|
|
|
|
|
mLog.e("Failed to add local dns IP " + dns);
|
|
|
|
|
newDnses.remove(dns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLinkProperties.addLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
mNetd.tetherApplyDnsInterfaces();
|
|
|
|
|
} catch (ServiceSpecificException | RemoteException e) {
|
|
|
|
|
mLog.e("Failed to update local DNS caching server");
|
|
|
|
|
if (newDnses != null) newDnses.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 13:19:33 +00:00
|
|
|
private int getInterfaceIndexForRule(int ifindex, boolean supportsBpf) {
|
|
|
|
|
return supportsBpf ? ifindex : NO_UPSTREAM;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 22:37:17 +08:00
|
|
|
// Handles updates to IPv6 forwarding rules if the upstream or its prefixes change.
|
|
|
|
|
private void updateIpv6ForwardingRules(int prevUpstreamIfindex,
|
|
|
|
|
@NonNull Set<IpPrefix> prevUpstreamPrefixes, int upstreamIfindex,
|
|
|
|
|
@NonNull Set<IpPrefix> upstreamPrefixes, boolean upstreamSupportsBpf) {
|
2020-02-14 01:06:35 +09:00
|
|
|
// If the upstream interface has changed, remove all rules and re-add them with the new
|
2023-08-16 13:19:33 +00:00
|
|
|
// upstream interface. If upstream is a virtual network, treated as no upstream.
|
2023-05-23 22:37:17 +08:00
|
|
|
if (prevUpstreamIfindex != upstreamIfindex
|
|
|
|
|
|| !prevUpstreamPrefixes.equals(upstreamPrefixes)) {
|
2023-08-16 13:19:33 +00:00
|
|
|
mBpfCoordinator.updateAllIpv6Rules(this, this.mInterfaceParams,
|
2023-05-23 22:37:17 +08:00
|
|
|
getInterfaceIndexForRule(upstreamIfindex, upstreamSupportsBpf),
|
|
|
|
|
upstreamPrefixes);
|
2020-02-14 01:06:35 +09:00
|
|
|
}
|
2023-08-16 13:19:33 +00:00
|
|
|
}
|
2020-02-14 01:06:35 +09:00
|
|
|
|
2023-08-16 13:19:33 +00:00
|
|
|
// Handles updates to IPv6 downstream rules if a neighbor event is received.
|
|
|
|
|
private void addOrRemoveIpv6Downstream(NeighborEvent e) {
|
2020-02-21 20:21:14 +09:00
|
|
|
// mInterfaceParams must be non-null or the event would not have arrived.
|
2020-02-14 01:06:35 +09:00
|
|
|
if (e == null) return;
|
|
|
|
|
if (!(e.ip instanceof Inet6Address) || e.ip.isMulticastAddress()
|
|
|
|
|
|| e.ip.isLoopbackAddress() || e.ip.isLinkLocalAddress()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 14:57:30 +09:00
|
|
|
// When deleting rules, we still need to pass a non-null MAC, even though it's ignored.
|
2023-08-15 07:13:00 +00:00
|
|
|
// Do this here instead of in the Ipv6DownstreamRule constructor to ensure that we
|
|
|
|
|
// never add rules with a null MAC, only delete them.
|
2020-04-14 14:57:30 +09:00
|
|
|
MacAddress dstMac = e.isValid() ? e.macAddr : NULL_MAC_ADDRESS;
|
2023-08-16 13:19:33 +00:00
|
|
|
Ipv6DownstreamRule rule = new Ipv6DownstreamRule(
|
|
|
|
|
getInterfaceIndexForRule(mLastIPv6UpstreamIfindex, mUpstreamSupportsBpf),
|
|
|
|
|
mInterfaceParams.index, (Inet6Address) e.ip, mInterfaceParams.macAddr, dstMac);
|
2020-02-14 01:06:35 +09:00
|
|
|
if (e.isValid()) {
|
2023-08-28 09:57:34 +00:00
|
|
|
mBpfCoordinator.addIpv6DownstreamRule(this, rule);
|
2020-02-14 01:06:35 +09:00
|
|
|
} else {
|
2023-08-28 09:57:34 +00:00
|
|
|
mBpfCoordinator.removeIpv6DownstreamRule(this, rule);
|
2020-02-14 01:06:35 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 19:39:49 +08:00
|
|
|
// TODO: consider moving into BpfCoordinator.
|
|
|
|
|
private void updateClientInfoIpv4(NeighborEvent e) {
|
|
|
|
|
if (e == null) return;
|
|
|
|
|
if (!(e.ip instanceof Inet4Address) || e.ip.isMulticastAddress()
|
|
|
|
|
|| e.ip.isLoopbackAddress() || e.ip.isLinkLocalAddress()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When deleting clients, IpServer still need to pass a non-null MAC, even though it's
|
|
|
|
|
// ignored. Do this here instead of in the ClientInfo constructor to ensure that
|
|
|
|
|
// IpServer never add clients with a null MAC, only delete them.
|
|
|
|
|
final MacAddress clientMac = e.isValid() ? e.macAddr : NULL_MAC_ADDRESS;
|
|
|
|
|
final ClientInfo clientInfo = new ClientInfo(mInterfaceParams.index,
|
|
|
|
|
mInterfaceParams.macAddr, (Inet4Address) e.ip, clientMac);
|
|
|
|
|
if (e.isValid()) {
|
|
|
|
|
mBpfCoordinator.tetherOffloadClientAdd(this, clientInfo);
|
|
|
|
|
} else {
|
|
|
|
|
mBpfCoordinator.tetherOffloadClientRemove(this, clientInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:06:35 +09:00
|
|
|
private void handleNeighborEvent(NeighborEvent e) {
|
|
|
|
|
if (mInterfaceParams != null
|
|
|
|
|
&& mInterfaceParams.index == e.ifindex
|
|
|
|
|
&& mInterfaceParams.hasMacAddress) {
|
2023-08-16 13:19:33 +00:00
|
|
|
addOrRemoveIpv6Downstream(e);
|
2020-12-21 19:39:49 +08:00
|
|
|
updateClientInfoIpv4(e);
|
2020-02-14 01:06:35 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 17:38:28 +08:00
|
|
|
private byte getHopLimit(String upstreamIface, int adjustTTL) {
|
2019-09-09 20:50:49 +08:00
|
|
|
try {
|
|
|
|
|
int upstreamHopLimit = Integer.parseUnsignedInt(
|
|
|
|
|
mNetd.getProcSysNet(INetd.IPV6, INetd.CONF, upstreamIface, "hop_limit"));
|
2020-05-21 17:38:28 +08:00
|
|
|
upstreamHopLimit = upstreamHopLimit + adjustTTL;
|
2019-09-09 20:50:49 +08:00
|
|
|
// Cap the hop limit to 255.
|
|
|
|
|
return (byte) Integer.min(upstreamHopLimit, 255);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
mLog.e("Failed to find upstream interface hop limit", e);
|
|
|
|
|
}
|
|
|
|
|
return RaParams.DEFAULT_HOPLIMIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setRaParams(RaParams newParams) {
|
|
|
|
|
if (mRaDaemon != null) {
|
|
|
|
|
final RaParams deprecatedParams =
|
|
|
|
|
RaParams.getDeprecatedRaParams(mLastRaParams, newParams);
|
|
|
|
|
|
|
|
|
|
configureLocalIPv6Routes(deprecatedParams.prefixes,
|
|
|
|
|
(newParams != null) ? newParams.prefixes : null);
|
|
|
|
|
|
|
|
|
|
configureLocalIPv6Dns(deprecatedParams.dnses,
|
|
|
|
|
(newParams != null) ? newParams.dnses : null);
|
|
|
|
|
|
|
|
|
|
mRaDaemon.buildNewRa(deprecatedParams, newParams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLastRaParams = newParams;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 10:58:04 +08:00
|
|
|
private void maybeLogMessage(State state, int what) {
|
|
|
|
|
switch (what) {
|
|
|
|
|
// Suppress some CMD_* to avoid log flooding.
|
|
|
|
|
case CMD_IPV6_TETHER_UPDATE:
|
|
|
|
|
case CMD_NEIGHBOR_EVENT:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mLog.log(state.getName() + " got "
|
|
|
|
|
+ sMagicDecoderRing.get(what, Integer.toString(what)));
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendInterfaceState(int newInterfaceState) {
|
|
|
|
|
mServingMode = newInterfaceState;
|
|
|
|
|
mCallback.updateInterfaceState(this, newInterfaceState, mLastError);
|
|
|
|
|
sendLinkProperties();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendLinkProperties() {
|
|
|
|
|
mCallback.updateLinkProperties(this, new LinkProperties(mLinkProperties));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetLinkProperties() {
|
|
|
|
|
mLinkProperties.clear();
|
|
|
|
|
mLinkProperties.setInterfaceName(mIfaceName);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 21:49:48 +08:00
|
|
|
private void maybeConfigureStaticIp(final TetheringRequestParcel request) {
|
2020-02-27 20:27:18 +08:00
|
|
|
// Ignore static address configuration if they are invalid or null. In theory, static
|
|
|
|
|
// addresses should not be invalid here because TetheringManager do not allow caller to
|
|
|
|
|
// specify invalid static address configuration.
|
|
|
|
|
if (request == null || request.localIPv4Address == null
|
|
|
|
|
|| request.staticClientAddress == null || !checkStaticAddressConfiguration(
|
|
|
|
|
request.localIPv4Address, request.staticClientAddress)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-16 21:49:48 +08:00
|
|
|
|
|
|
|
|
mStaticIpv4ServerAddr = request.localIPv4Address;
|
|
|
|
|
mStaticIpv4ClientAddr = request.staticClientAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
class InitialState extends State {
|
|
|
|
|
@Override
|
|
|
|
|
public void enter() {
|
|
|
|
|
sendInterfaceState(STATE_AVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message message) {
|
2021-06-01 10:58:04 +08:00
|
|
|
maybeLogMessage(this, message.what);
|
2019-09-09 20:50:49 +08:00
|
|
|
switch (message.what) {
|
|
|
|
|
case CMD_TETHER_REQUESTED:
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_NO_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
switch (message.arg1) {
|
|
|
|
|
case STATE_LOCAL_ONLY:
|
2020-03-16 21:49:48 +08:00
|
|
|
maybeConfigureStaticIp((TetheringRequestParcel) message.obj);
|
2019-09-09 20:50:49 +08:00
|
|
|
transitionTo(mLocalHotspotState);
|
|
|
|
|
break;
|
|
|
|
|
case STATE_TETHERED:
|
2020-03-16 21:49:48 +08:00
|
|
|
maybeConfigureStaticIp((TetheringRequestParcel) message.obj);
|
2019-09-09 20:50:49 +08:00
|
|
|
transitionTo(mTetheredState);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mLog.e("Invalid tethering interface serving state specified.");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CMD_INTERFACE_DOWN:
|
|
|
|
|
transitionTo(mUnavailableState);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return NOT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
return HANDLED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 17:25:36 +08:00
|
|
|
private void startConntrackMonitoring() {
|
|
|
|
|
mBpfCoordinator.startMonitoring(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopConntrackMonitoring() {
|
|
|
|
|
mBpfCoordinator.stopMonitoring(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 08:50:38 +00:00
|
|
|
abstract class BaseServingState extends State {
|
|
|
|
|
private final int mDesiredInterfaceState;
|
|
|
|
|
|
|
|
|
|
BaseServingState(int interfaceState) {
|
|
|
|
|
mDesiredInterfaceState = interfaceState;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
@Override
|
|
|
|
|
public void enter() {
|
2020-10-15 17:25:36 +08:00
|
|
|
startConntrackMonitoring();
|
|
|
|
|
|
2023-05-19 08:50:38 +00:00
|
|
|
startServingInterface();
|
|
|
|
|
|
|
|
|
|
if (mLastError != TETHER_ERROR_NO_ERROR) {
|
2023-09-19 06:56:14 +00:00
|
|
|
// This will transition to InitialState right away, regardless of whether any
|
|
|
|
|
// message is already waiting in the StateMachine queue (including maybe some
|
|
|
|
|
// message to go to InitialState). InitialState will then process any pending
|
|
|
|
|
// message (and generally ignores them). It is difficult to know for sure whether
|
|
|
|
|
// this is correct in all cases, but this is equivalent to what IpServer was doing
|
|
|
|
|
// in previous versions of the mainline module.
|
2022-12-21 16:39:23 +00:00
|
|
|
// TODO : remove sendMessageAtFrontOfQueueToAsyncSM after migrating to the Sync
|
|
|
|
|
// StateMachine.
|
2024-01-22 11:09:55 +00:00
|
|
|
if (USE_SYNC_SM) {
|
2022-12-21 16:39:23 +00:00
|
|
|
sendSelfMessageToSyncSM(CMD_SERVICE_FAILED_TO_START, mLastError);
|
|
|
|
|
} else {
|
|
|
|
|
sendMessageAtFrontOfQueueToAsyncSM(CMD_SERVICE_FAILED_TO_START, mLastError);
|
|
|
|
|
}
|
2023-05-19 08:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DBG) Log.d(TAG, getStateString(mDesiredInterfaceState) + " serve " + mIfaceName);
|
|
|
|
|
sendInterfaceState(mDesiredInterfaceState);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 06:45:04 +00:00
|
|
|
private int getScope() {
|
|
|
|
|
if (mDesiredInterfaceState == STATE_TETHERED) {
|
|
|
|
|
return CONNECTIVITY_SCOPE_GLOBAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CONNECTIVITY_SCOPE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 08:50:38 +00:00
|
|
|
private void startServingInterface() {
|
2023-03-15 06:45:04 +00:00
|
|
|
if (!startIPv4(getScope())) {
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_IFACE_CFG_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2020-02-12 00:19:21 +08:00
|
|
|
NetdUtils.tetherInterface(mNetd, mIfaceName, asIpPrefix(mIpv4Address));
|
2020-02-15 11:35:00 +08:00
|
|
|
} catch (RemoteException | ServiceSpecificException | IllegalStateException e) {
|
2020-04-09 10:13:44 +09:00
|
|
|
mLog.e("Error Tethering", e);
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_TETHER_IFACE_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!startIPv6()) {
|
|
|
|
|
mLog.e("Failed to startIPv6");
|
|
|
|
|
// TODO: Make this a fatal error once Bluetooth IPv6 is sorted.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exit() {
|
|
|
|
|
// Note that at this point, we're leaving the tethered state. We can fail any
|
|
|
|
|
// of these operations, but it doesn't really change that we have to try them
|
|
|
|
|
// all in sequence.
|
|
|
|
|
stopIPv6();
|
|
|
|
|
|
|
|
|
|
try {
|
2020-01-07 14:43:17 +08:00
|
|
|
NetdUtils.untetherInterface(mNetd, mIfaceName);
|
|
|
|
|
} catch (RemoteException | ServiceSpecificException e) {
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_UNTETHER_IFACE_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
mLog.e("Failed to untether interface: " + e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopIPv4();
|
2020-10-15 17:25:36 +08:00
|
|
|
stopConntrackMonitoring();
|
2019-09-09 20:50:49 +08:00
|
|
|
|
|
|
|
|
resetLinkProperties();
|
2022-03-14 18:04:33 +08:00
|
|
|
|
|
|
|
|
mTetheringMetrics.updateErrorCode(mInterfaceType, mLastError);
|
|
|
|
|
mTetheringMetrics.sendReport(mInterfaceType);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message message) {
|
|
|
|
|
switch (message.what) {
|
|
|
|
|
case CMD_TETHER_UNREQUESTED:
|
|
|
|
|
transitionTo(mInitialState);
|
|
|
|
|
if (DBG) Log.d(TAG, "Untethered (unrequested)" + mIfaceName);
|
|
|
|
|
break;
|
|
|
|
|
case CMD_INTERFACE_DOWN:
|
|
|
|
|
transitionTo(mUnavailableState);
|
|
|
|
|
if (DBG) Log.d(TAG, "Untethered (ifdown)" + mIfaceName);
|
|
|
|
|
break;
|
|
|
|
|
case CMD_IPV6_TETHER_UPDATE:
|
2020-05-21 17:38:28 +08:00
|
|
|
updateUpstreamIPv6LinkProperties((LinkProperties) message.obj, message.arg1);
|
2019-09-09 20:50:49 +08:00
|
|
|
sendLinkProperties();
|
|
|
|
|
break;
|
|
|
|
|
case CMD_IP_FORWARDING_ENABLE_ERROR:
|
|
|
|
|
case CMD_IP_FORWARDING_DISABLE_ERROR:
|
|
|
|
|
case CMD_START_TETHERING_ERROR:
|
|
|
|
|
case CMD_STOP_TETHERING_ERROR:
|
|
|
|
|
case CMD_SET_DNS_FORWARDERS_ERROR:
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_INTERNAL_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
transitionTo(mInitialState);
|
|
|
|
|
break;
|
2020-04-09 10:13:44 +09:00
|
|
|
case CMD_NEW_PREFIX_REQUEST:
|
|
|
|
|
handleNewPrefixRequest((IpPrefix) message.obj);
|
|
|
|
|
break;
|
2020-02-12 00:19:21 +08:00
|
|
|
case CMD_NOTIFY_PREFIX_CONFLICT:
|
|
|
|
|
mLog.i("restart tethering: " + mInterfaceType);
|
|
|
|
|
mCallback.requestEnableTethering(mInterfaceType, false /* enabled */);
|
|
|
|
|
transitionTo(mWaitingForRestartState);
|
|
|
|
|
break;
|
2023-09-19 06:56:14 +00:00
|
|
|
case CMD_SERVICE_FAILED_TO_START:
|
|
|
|
|
mLog.e("start serving fail, error: " + message.arg1);
|
|
|
|
|
transitionTo(mInitialState);
|
2019-09-09 20:50:49 +08:00
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-05-19 08:50:38 +00:00
|
|
|
|
|
|
|
|
private void handleNewPrefixRequest(@NonNull final IpPrefix currentPrefix) {
|
|
|
|
|
if (!currentPrefix.contains(mIpv4Address.getAddress())
|
|
|
|
|
|| currentPrefix.getPrefixLength() != mIpv4Address.getPrefixLength()) {
|
|
|
|
|
Log.e(TAG, "Invalid prefix: " + currentPrefix);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final LinkAddress deprecatedLinkAddress = mIpv4Address;
|
2023-03-15 06:45:04 +00:00
|
|
|
mIpv4Address = requestIpv4Address(getScope(), false);
|
2023-05-19 08:50:38 +00:00
|
|
|
if (mIpv4Address == null) {
|
|
|
|
|
mLog.e("Fail to request a new downstream prefix");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final Inet4Address srvAddr = (Inet4Address) mIpv4Address.getAddress();
|
|
|
|
|
|
|
|
|
|
// Add new IPv4 address on the interface.
|
|
|
|
|
if (!mInterfaceCtrl.addAddress(srvAddr, currentPrefix.getPrefixLength())) {
|
|
|
|
|
mLog.e("Failed to add new IP " + srvAddr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove deprecated routes from local network.
|
|
|
|
|
removeRoutesFromLocalNetwork(
|
|
|
|
|
Collections.singletonList(getDirectConnectedRoute(deprecatedLinkAddress)));
|
|
|
|
|
mLinkProperties.removeLinkAddress(deprecatedLinkAddress);
|
|
|
|
|
|
|
|
|
|
// Add new routes to local network.
|
|
|
|
|
addRoutesToLocalNetwork(
|
|
|
|
|
Collections.singletonList(getDirectConnectedRoute(mIpv4Address)));
|
|
|
|
|
mLinkProperties.addLinkAddress(mIpv4Address);
|
|
|
|
|
|
|
|
|
|
// Update local DNS caching server with new IPv4 address, otherwise, dnsmasq doesn't
|
|
|
|
|
// listen on the interface configured with new IPv4 address, that results DNS validation
|
|
|
|
|
// failure of downstream client even if appropriate routes have been configured.
|
|
|
|
|
try {
|
|
|
|
|
mNetd.tetherApplyDnsInterfaces();
|
|
|
|
|
} catch (ServiceSpecificException | RemoteException e) {
|
|
|
|
|
mLog.e("Failed to update local DNS caching server");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendLinkProperties();
|
|
|
|
|
|
|
|
|
|
// Notify DHCP server that new prefix/route has been applied on IpServer.
|
|
|
|
|
final Inet4Address clientAddr = mStaticIpv4ClientAddr == null ? null :
|
|
|
|
|
(Inet4Address) mStaticIpv4ClientAddr.getAddress();
|
|
|
|
|
final DhcpServingParamsParcel params = makeServingParams(srvAddr /* defaultRouter */,
|
|
|
|
|
srvAddr /* dnsServer */, mIpv4Address /* serverLinkAddress */, clientAddr);
|
|
|
|
|
try {
|
|
|
|
|
mDhcpServer.updateParams(params, new OnHandlerStatusCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void callback(int statusCode) {
|
|
|
|
|
if (statusCode != STATUS_SUCCESS) {
|
|
|
|
|
mLog.e("Error updating DHCP serving params: " + statusCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
mLog.e("Error updating DHCP serving params", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handling errors in BaseServingState.enter() by transitioning is
|
|
|
|
|
// problematic because transitioning during a multi-state jump yields
|
|
|
|
|
// a Log.wtf(). Ultimately, there should be only one ServingState,
|
|
|
|
|
// and forwarding and NAT rules should be handled by a coordinating
|
|
|
|
|
// functional element outside of IpServer.
|
|
|
|
|
class LocalHotspotState extends BaseServingState {
|
2023-05-19 08:50:38 +00:00
|
|
|
LocalHotspotState() {
|
|
|
|
|
super(STATE_LOCAL_ONLY);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message message) {
|
|
|
|
|
if (super.processMessage(message)) return true;
|
|
|
|
|
|
2021-06-01 10:58:04 +08:00
|
|
|
maybeLogMessage(this, message.what);
|
2019-09-09 20:50:49 +08:00
|
|
|
switch (message.what) {
|
|
|
|
|
case CMD_TETHER_REQUESTED:
|
|
|
|
|
mLog.e("CMD_TETHER_REQUESTED while in local-only hotspot mode.");
|
|
|
|
|
break;
|
|
|
|
|
case CMD_TETHER_CONNECTION_CHANGED:
|
|
|
|
|
// Ignored in local hotspot state.
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handling errors in BaseServingState.enter() by transitioning is
|
|
|
|
|
// problematic because transitioning during a multi-state jump yields
|
|
|
|
|
// a Log.wtf(). Ultimately, there should be only one ServingState,
|
|
|
|
|
// and forwarding and NAT rules should be handled by a coordinating
|
|
|
|
|
// functional element outside of IpServer.
|
|
|
|
|
class TetheredState extends BaseServingState {
|
2023-05-19 08:50:38 +00:00
|
|
|
TetheredState() {
|
|
|
|
|
super(STATE_TETHERED);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exit() {
|
|
|
|
|
cleanupUpstream();
|
2023-08-16 13:19:33 +00:00
|
|
|
mBpfCoordinator.clearAllIpv6Rules(IpServer.this);
|
2019-09-09 20:50:49 +08:00
|
|
|
super.exit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-08 17:15:35 +08:00
|
|
|
// Note that IPv4 offload rules cleanup is implemented in BpfCoordinator while upstream
|
|
|
|
|
// state is null or changed because IPv4 and IPv6 tethering have different code flow
|
|
|
|
|
// and behaviour. While upstream is switching from offload supported interface to
|
|
|
|
|
// offload non-supportted interface, event CMD_TETHER_CONNECTION_CHANGED calls
|
|
|
|
|
// #cleanupUpstreamInterface but #cleanupUpstream because new UpstreamIfaceSet is not null.
|
|
|
|
|
// This case won't happen in IPv6 tethering because IPv6 tethering upstream state is
|
|
|
|
|
// reported by IPv6TetheringCoordinator. #cleanupUpstream is also called by unwirding
|
|
|
|
|
// adding NAT failure. In that case, the IPv4 offload rules are removed by #stopIPv4
|
|
|
|
|
// in the state machine. Once there is any case out whish is not covered by previous cases,
|
|
|
|
|
// probably consider clearing rules in #cleanupUpstream as well.
|
2019-09-09 20:50:49 +08:00
|
|
|
private void cleanupUpstream() {
|
|
|
|
|
if (mUpstreamIfaceSet == null) return;
|
|
|
|
|
|
|
|
|
|
for (String ifname : mUpstreamIfaceSet.ifnames) cleanupUpstreamInterface(ifname);
|
|
|
|
|
mUpstreamIfaceSet = null;
|
2023-08-16 13:19:33 +00:00
|
|
|
mBpfCoordinator.updateAllIpv6Rules(
|
2023-05-23 22:37:17 +08:00
|
|
|
IpServer.this, IpServer.this.mInterfaceParams, NO_UPSTREAM, Set.of());
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cleanupUpstreamInterface(String upstreamIface) {
|
|
|
|
|
// Note that we don't care about errors here.
|
|
|
|
|
// Sometimes interfaces are gone before we get
|
|
|
|
|
// to remove their rules, which generates errors.
|
|
|
|
|
// Just do the best we can.
|
2021-02-25 17:52:02 +08:00
|
|
|
mBpfCoordinator.maybeDetachProgram(mIfaceName, upstreamIface);
|
2023-10-06 18:31:08 +09:00
|
|
|
removeInterfaceForward(mIfaceName, upstreamIface);
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message message) {
|
|
|
|
|
if (super.processMessage(message)) return true;
|
|
|
|
|
|
2021-06-01 10:58:04 +08:00
|
|
|
maybeLogMessage(this, message.what);
|
2019-09-09 20:50:49 +08:00
|
|
|
switch (message.what) {
|
|
|
|
|
case CMD_TETHER_REQUESTED:
|
|
|
|
|
mLog.e("CMD_TETHER_REQUESTED while already tethering.");
|
|
|
|
|
break;
|
|
|
|
|
case CMD_TETHER_CONNECTION_CHANGED:
|
|
|
|
|
final InterfaceSet newUpstreamIfaceSet = (InterfaceSet) message.obj;
|
|
|
|
|
if (noChangeInUpstreamIfaceSet(newUpstreamIfaceSet)) {
|
|
|
|
|
if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newUpstreamIfaceSet == null) {
|
|
|
|
|
cleanupUpstream();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String removed : upstreamInterfacesRemoved(newUpstreamIfaceSet)) {
|
|
|
|
|
cleanupUpstreamInterface(removed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final Set<String> added = upstreamInterfacesAdd(newUpstreamIfaceSet);
|
|
|
|
|
// This makes the call to cleanupUpstream() in the error
|
|
|
|
|
// path for any interface neatly cleanup all the interfaces.
|
|
|
|
|
mUpstreamIfaceSet = newUpstreamIfaceSet;
|
|
|
|
|
|
|
|
|
|
for (String ifname : added) {
|
2021-02-09 23:12:37 +09:00
|
|
|
// Add upstream index to name mapping for the tether stats usage in the
|
|
|
|
|
// coordinator. Although this mapping could be added by both class
|
|
|
|
|
// Tethering and IpServer, adding mapping from IpServer guarantees that
|
|
|
|
|
// the mapping is added before adding forwarding rules. That is because
|
|
|
|
|
// there are different state machines in both classes. It is hard to
|
|
|
|
|
// guarantee the link property update order between multiple state machines.
|
|
|
|
|
// Note that both IPv4 and IPv6 interface may be added because
|
|
|
|
|
// Tethering::setUpstreamNetwork calls getTetheringInterfaces which merges
|
|
|
|
|
// IPv4 and IPv6 interface name (if any) into an InterfaceSet. The IPv6
|
|
|
|
|
// capability may be updated later. In that case, IPv6 interface mapping is
|
|
|
|
|
// updated in updateUpstreamIPv6LinkProperties.
|
|
|
|
|
if (!ifname.startsWith("v4-")) { // ignore clat interfaces
|
|
|
|
|
final InterfaceParams upstreamIfaceParams =
|
|
|
|
|
mDeps.getInterfaceParams(ifname);
|
|
|
|
|
if (upstreamIfaceParams != null) {
|
2023-08-16 13:19:33 +00:00
|
|
|
mBpfCoordinator.maybeAddUpstreamToLookupTable(
|
2021-02-09 23:12:37 +09:00
|
|
|
upstreamIfaceParams.index, ifname);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 17:52:02 +08:00
|
|
|
mBpfCoordinator.maybeAttachProgram(mIfaceName, ifname);
|
2019-09-09 20:50:49 +08:00
|
|
|
try {
|
2023-10-06 18:31:08 +09:00
|
|
|
addInterfaceForward(mIfaceName, ifname);
|
2020-01-07 14:43:17 +08:00
|
|
|
} catch (RemoteException | ServiceSpecificException e) {
|
2023-10-06 18:31:08 +09:00
|
|
|
mLog.e("Exception enabling iface forward", e);
|
2019-09-09 20:50:49 +08:00
|
|
|
cleanupUpstream();
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_ENABLE_FORWARDING_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
transitionTo(mInitialState);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2020-02-14 01:06:35 +09:00
|
|
|
case CMD_NEIGHBOR_EVENT:
|
|
|
|
|
handleNeighborEvent((NeighborEvent) message.obj);
|
|
|
|
|
break;
|
2019-09-09 20:50:49 +08:00
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean noChangeInUpstreamIfaceSet(InterfaceSet newIfaces) {
|
|
|
|
|
if (mUpstreamIfaceSet == null && newIfaces == null) return true;
|
|
|
|
|
if (mUpstreamIfaceSet != null && newIfaces != null) {
|
|
|
|
|
return mUpstreamIfaceSet.equals(newIfaces);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Set<String> upstreamInterfacesRemoved(InterfaceSet newIfaces) {
|
|
|
|
|
if (mUpstreamIfaceSet == null) return new HashSet<>();
|
|
|
|
|
|
|
|
|
|
final HashSet<String> removed = new HashSet<>(mUpstreamIfaceSet.ifnames);
|
|
|
|
|
removed.removeAll(newIfaces.ifnames);
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Set<String> upstreamInterfacesAdd(InterfaceSet newIfaces) {
|
|
|
|
|
final HashSet<String> added = new HashSet<>(newIfaces.ifnames);
|
|
|
|
|
if (mUpstreamIfaceSet != null) added.removeAll(mUpstreamIfaceSet.ifnames);
|
|
|
|
|
return added;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This state is terminal for the per interface state machine. At this
|
2020-07-29 12:05:04 +08:00
|
|
|
* point, the tethering main state machine should have removed this interface
|
2019-09-09 20:50:49 +08:00
|
|
|
* specific state machine from its list of possible recipients of
|
|
|
|
|
* tethering requests. The state machine itself will hang around until
|
|
|
|
|
* the garbage collector finds it.
|
|
|
|
|
*/
|
|
|
|
|
class UnavailableState extends State {
|
|
|
|
|
@Override
|
|
|
|
|
public void enter() {
|
2023-08-16 13:19:33 +00:00
|
|
|
// TODO: move mIpNeighborMonitor.stop() to TetheredState#exit, and trigger a neighbours
|
|
|
|
|
// dump after starting mIpNeighborMonitor.
|
2020-06-14 14:46:54 +08:00
|
|
|
mIpNeighborMonitor.stop();
|
2022-12-14 08:49:40 +00:00
|
|
|
mLastError = TETHER_ERROR_NO_ERROR;
|
2019-09-09 20:50:49 +08:00
|
|
|
sendInterfaceState(STATE_UNAVAILABLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 00:19:21 +08:00
|
|
|
class WaitingForRestartState extends State {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean processMessage(Message message) {
|
2021-06-01 10:58:04 +08:00
|
|
|
maybeLogMessage(this, message.what);
|
2020-02-12 00:19:21 +08:00
|
|
|
switch (message.what) {
|
|
|
|
|
case CMD_TETHER_UNREQUESTED:
|
|
|
|
|
transitionTo(mInitialState);
|
|
|
|
|
mLog.i("Untethered (unrequested) and restarting " + mIfaceName);
|
|
|
|
|
mCallback.requestEnableTethering(mInterfaceType, true /* enabled */);
|
|
|
|
|
break;
|
|
|
|
|
case CMD_INTERFACE_DOWN:
|
|
|
|
|
transitionTo(mUnavailableState);
|
2021-04-13 17:17:44 +09:00
|
|
|
mLog.i("Untethered (interface down) and restarting " + mIfaceName);
|
2020-02-12 00:19:21 +08:00
|
|
|
mCallback.requestEnableTethering(mInterfaceType, true /* enabled */);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:50:49 +08:00
|
|
|
// Accumulate routes representing "prefixes to be assigned to the local
|
|
|
|
|
// interface", for subsequent modification of local_network routing.
|
|
|
|
|
private static ArrayList<RouteInfo> getLocalRoutesFor(
|
2023-11-08 14:50:15 +00:00
|
|
|
String ifname, ArraySet<IpPrefix> prefixes) {
|
2019-09-09 20:50:49 +08:00
|
|
|
final ArrayList<RouteInfo> localRoutes = new ArrayList<RouteInfo>();
|
|
|
|
|
for (IpPrefix ipp : prefixes) {
|
2019-12-06 15:24:53 +08:00
|
|
|
localRoutes.add(new RouteInfo(ipp, null, ifname, RTN_UNICAST));
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|
|
|
|
|
return localRoutes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Given a prefix like 2001:db8::/64 return an address like 2001:db8::1.
|
|
|
|
|
private static Inet6Address getLocalDnsIpFor(IpPrefix localPrefix) {
|
|
|
|
|
final byte[] dnsBytes = localPrefix.getRawAddress();
|
|
|
|
|
dnsBytes[dnsBytes.length - 1] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1));
|
|
|
|
|
try {
|
|
|
|
|
return Inet6Address.getByAddress(null, dnsBytes, 0);
|
|
|
|
|
} catch (UnknownHostException e) {
|
2019-12-06 15:24:53 +08:00
|
|
|
Log.wtf(TAG, "Failed to construct Inet6Address from: " + localPrefix);
|
2019-09-09 20:50:49 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte getRandomSanitizedByte(byte dflt, byte... excluded) {
|
|
|
|
|
final byte random = (byte) (new Random()).nextInt();
|
|
|
|
|
for (int value : excluded) {
|
|
|
|
|
if (random == value) return dflt;
|
|
|
|
|
}
|
|
|
|
|
return random;
|
|
|
|
|
}
|
2023-05-23 22:37:17 +08:00
|
|
|
|
|
|
|
|
/** Get IPv6 prefixes from LinkProperties */
|
|
|
|
|
@NonNull
|
|
|
|
|
@VisibleForTesting
|
2023-11-08 14:50:15 +00:00
|
|
|
static ArraySet<IpPrefix> getTetherableIpv6Prefixes(@NonNull Collection<LinkAddress> addrs) {
|
|
|
|
|
final ArraySet<IpPrefix> prefixes = new ArraySet<>();
|
2023-05-23 22:37:17 +08:00
|
|
|
for (LinkAddress linkAddr : addrs) {
|
|
|
|
|
if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue;
|
|
|
|
|
prefixes.add(new IpPrefix(linkAddr.getAddress(), RFC7421_PREFIX_LENGTH));
|
|
|
|
|
}
|
|
|
|
|
return prefixes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
2023-11-08 14:50:15 +00:00
|
|
|
private ArraySet<IpPrefix> getTetherableIpv6Prefixes(@NonNull LinkProperties lp) {
|
2023-05-23 22:37:17 +08:00
|
|
|
return getTetherableIpv6Prefixes(lp.getLinkAddresses());
|
|
|
|
|
}
|
2019-09-09 20:50:49 +08:00
|
|
|
}
|