Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.server.connectivity.tethering; |
| 18 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 19 | import static android.net.NetworkUtils.numericToInetAddress; |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 20 | import static android.net.util.NetworkConstants.asByte; |
| 21 | import static android.net.util.NetworkConstants.FF; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 22 | import static android.net.util.NetworkConstants.RFC7421_PREFIX_LENGTH; |
| 23 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 25 | import android.net.INetd; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 26 | import android.net.INetworkStatsService; |
| 27 | import android.net.InterfaceConfiguration; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 28 | import android.net.IpPrefix; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 29 | import android.net.LinkAddress; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 30 | import android.net.LinkProperties; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 31 | import android.net.RouteInfo; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 32 | import android.net.dhcp.DhcpServer; |
| 33 | import android.net.dhcp.DhcpServingParams; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 34 | import android.net.ip.InterfaceController; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 35 | import android.net.ip.RouterAdvertisementDaemon; |
| 36 | import android.net.ip.RouterAdvertisementDaemon.RaParams; |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 37 | import android.net.util.InterfaceParams; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 38 | import android.net.util.InterfaceSet; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 39 | import android.net.util.SharedLog; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 40 | import android.os.INetworkManagementService; |
| 41 | import android.os.Looper; |
| 42 | import android.os.Message; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 43 | import android.os.RemoteException; |
| 44 | import android.os.ServiceSpecificException; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 46 | import android.util.Slog; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 47 | import android.util.SparseArray; |
| 48 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 49 | import com.android.internal.util.MessageUtils; |
| 50 | import com.android.internal.util.Protocol; |
| 51 | import com.android.internal.util.State; |
| 52 | import com.android.internal.util.StateMachine; |
| 53 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 54 | import java.net.Inet4Address; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 55 | import java.net.Inet6Address; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 56 | import java.net.InetAddress; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 57 | import java.net.UnknownHostException; |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.HashSet; |
| 60 | import java.util.Objects; |
| 61 | import java.util.Random; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 62 | import java.util.Set; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 65 | * Provides the interface to IP-layer serving functionality for a given network |
| 66 | * interface, e.g. for tethering or "local-only hotspot" mode. |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 67 | * |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 68 | * @hide |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 69 | */ |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 70 | public class TetherInterfaceStateMachine extends StateMachine { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 71 | private static final IpPrefix LINK_LOCAL_PREFIX = new IpPrefix("fe80::/64"); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 72 | private static final byte DOUG_ADAMS = (byte) 42; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 73 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 74 | private static final String USB_NEAR_IFACE_ADDR = "192.168.42.129"; |
| 75 | private static final int USB_PREFIX_LENGTH = 24; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 76 | private static final String WIFI_HOST_IFACE_ADDR = "192.168.43.1"; |
| 77 | private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 78 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 79 | // TODO: have PanService use some visible version of this constant |
| 80 | private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1"; |
| 81 | private static final int BLUETOOTH_DHCP_PREFIX_LENGTH = 24; |
| 82 | |
| 83 | // TODO: have this configurable |
| 84 | private static final int DHCP_LEASE_TIME_SECS = 3600; |
| 85 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 86 | private final static String TAG = "TetherInterfaceSM"; |
| 87 | private final static boolean DBG = false; |
| 88 | private final static boolean VDBG = false; |
| 89 | private static final Class[] messageClasses = { |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 90 | TetherInterfaceStateMachine.class |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 91 | }; |
| 92 | private static final SparseArray<String> sMagicDecoderRing = |
| 93 | MessageUtils.findMessageNames(messageClasses); |
| 94 | |
| 95 | private static final int BASE_IFACE = Protocol.BASE_TETHERING + 100; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 96 | // request from the user that it wants to tether |
| 97 | public static final int CMD_TETHER_REQUESTED = BASE_IFACE + 2; |
| 98 | // request from the user that it wants to untether |
| 99 | public static final int CMD_TETHER_UNREQUESTED = BASE_IFACE + 3; |
| 100 | // notification that this interface is down |
| 101 | public static final int CMD_INTERFACE_DOWN = BASE_IFACE + 4; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 102 | // notification from the master SM that it had trouble enabling IP Forwarding |
| 103 | public static final int CMD_IP_FORWARDING_ENABLE_ERROR = BASE_IFACE + 7; |
| 104 | // notification from the master SM that it had trouble disabling IP Forwarding |
| 105 | public static final int CMD_IP_FORWARDING_DISABLE_ERROR = BASE_IFACE + 8; |
| 106 | // notification from the master SM that it had trouble starting tethering |
| 107 | public static final int CMD_START_TETHERING_ERROR = BASE_IFACE + 9; |
| 108 | // notification from the master SM that it had trouble stopping tethering |
| 109 | public static final int CMD_STOP_TETHERING_ERROR = BASE_IFACE + 10; |
| 110 | // notification from the master SM that it had trouble setting the DNS forwarders |
| 111 | public static final int CMD_SET_DNS_FORWARDERS_ERROR = BASE_IFACE + 11; |
| 112 | // the upstream connection has changed |
| 113 | public static final int CMD_TETHER_CONNECTION_CHANGED = BASE_IFACE + 12; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 114 | // new IPv6 tethering parameters need to be processed |
| 115 | public static final int CMD_IPV6_TETHER_UPDATE = BASE_IFACE + 13; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 116 | |
| 117 | private final State mInitialState; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 118 | private final State mLocalHotspotState; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 119 | private final State mTetheredState; |
| 120 | private final State mUnavailableState; |
| 121 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 122 | private final SharedLog mLog; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 123 | private final INetworkManagementService mNMService; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 124 | private final INetd mNetd; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 125 | private final INetworkStatsService mStatsService; |
| 126 | private final IControlsTethering mTetherController; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 127 | private final InterfaceController mInterfaceCtrl; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 128 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 129 | private final String mIfaceName; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 130 | private final int mInterfaceType; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 131 | private final LinkProperties mLinkProperties; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 132 | private final boolean mUsingLegacyDhcp; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 133 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 134 | private final TetheringDependencies mDeps; |
| 135 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 136 | private int mLastError; |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 137 | private int mServingMode; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 138 | private InterfaceSet mUpstreamIfaceSet; // may change over time |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 139 | private InterfaceParams mInterfaceParams; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 140 | // TODO: De-duplicate this with mLinkProperties above. Currently, these link |
| 141 | // properties are those selected by the IPv6TetheringCoordinator and relayed |
| 142 | // to us. By comparison, mLinkProperties contains the addresses and directly |
| 143 | // connected routes that have been formed from these properties iff. we have |
| 144 | // succeeded in configuring them and are able to announce them within Router |
| 145 | // Advertisements (otherwise, we do not add them to mLinkProperties at all). |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 146 | private LinkProperties mLastIPv6LinkProperties; |
| 147 | private RouterAdvertisementDaemon mRaDaemon; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 148 | private DhcpServer mDhcpServer; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 149 | private RaParams mLastRaParams; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 150 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 151 | public TetherInterfaceStateMachine( |
| 152 | String ifaceName, Looper looper, int interfaceType, SharedLog log, |
| 153 | INetworkManagementService nMService, INetworkStatsService statsService, |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 154 | IControlsTethering tetherController, boolean usingLegacyDhcp, |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 155 | TetheringDependencies deps) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 156 | super(ifaceName, looper); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 157 | mLog = log.forSubComponent(ifaceName); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 158 | mNMService = nMService; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 159 | mNetd = deps.getNetdService(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 160 | mStatsService = statsService; |
| 161 | mTetherController = tetherController; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 162 | mInterfaceCtrl = new InterfaceController(ifaceName, nMService, mNetd, mLog); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 163 | mIfaceName = ifaceName; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 164 | mInterfaceType = interfaceType; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 165 | mLinkProperties = new LinkProperties(); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 166 | mUsingLegacyDhcp = usingLegacyDhcp; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 167 | mDeps = deps; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 168 | resetLinkProperties(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 169 | mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 170 | mServingMode = IControlsTethering.STATE_AVAILABLE; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 171 | |
| 172 | mInitialState = new InitialState(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 173 | mLocalHotspotState = new LocalHotspotState(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 174 | mTetheredState = new TetheredState(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 175 | mUnavailableState = new UnavailableState(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 176 | addState(mInitialState); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 177 | addState(mLocalHotspotState); |
| 178 | addState(mTetheredState); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 179 | addState(mUnavailableState); |
| 180 | |
| 181 | setInitialState(mInitialState); |
| 182 | } |
| 183 | |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 184 | public String interfaceName() { return mIfaceName; } |
| 185 | |
| 186 | public int interfaceType() { return mInterfaceType; } |
| 187 | |
| 188 | public int lastError() { return mLastError; } |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 189 | |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 190 | public int servingMode() { return mServingMode; } |
| 191 | |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 192 | public LinkProperties linkProperties() { return new LinkProperties(mLinkProperties); } |
| 193 | |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 194 | public void stop() { sendMessage(CMD_INTERFACE_DOWN); } |
| 195 | |
Erik Kline | 218c226 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 196 | public void unwanted() { sendMessage(CMD_TETHER_UNREQUESTED); } |
| 197 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 198 | /** |
| 199 | * Internals. |
| 200 | */ |
| 201 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 202 | private boolean startIPv4() { return configureIPv4(true); } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 203 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 204 | private boolean startDhcp(Inet4Address addr, int prefixLen) { |
| 205 | if (mUsingLegacyDhcp) { |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | final InterfaceParams ifaceParams = mDeps.getInterfaceParams(mIfaceName); |
| 210 | if (ifaceParams == null) { |
| 211 | Log.e(TAG, "Failed to find interface params for DHCPv4"); |
| 212 | return false; |
| 213 | } |
| 214 | final DhcpServingParams params; |
| 215 | try { |
| 216 | params = new DhcpServingParams.Builder() |
| 217 | .setDefaultRouters(addr) |
| 218 | .setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS) |
| 219 | .setDnsServers(addr) |
| 220 | .setServerAddr(new LinkAddress(addr, prefixLen)) |
Remi NGUYEN VAN | 6557a24 | 2018-07-13 17:42:39 +0900 | [diff] [blame] | 221 | .setMetered(true) |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 222 | .build(); |
| 223 | // TODO: also advertise link MTU |
| 224 | } catch (DhcpServingParams.InvalidParameterException e) { |
| 225 | Log.e(TAG, "Invalid DHCP parameters", e); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | mDhcpServer = mDeps.makeDhcpServer(getHandler().getLooper(), ifaceParams, params, |
| 230 | mLog.forSubComponent("DHCP")); |
| 231 | mDhcpServer.start(); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | private void stopDhcp() { |
| 236 | if (mDhcpServer != null) { |
| 237 | mDhcpServer.stop(); |
| 238 | mDhcpServer = null; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private boolean configureDhcp(boolean enable, Inet4Address addr, int prefixLen) { |
| 243 | if (enable) { |
| 244 | return startDhcp(addr, prefixLen); |
| 245 | } else { |
| 246 | stopDhcp(); |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | |
Erik Kline | f8bba5b | 2018-05-18 16:09:24 +0900 | [diff] [blame] | 251 | private void stopIPv4() { |
| 252 | configureIPv4(false); |
| 253 | // NOTE: All of configureIPv4() will be refactored out of existence |
| 254 | // into calls to InterfaceController, shared with startIPv4(). |
| 255 | mInterfaceCtrl.clearIPv4Address(); |
| 256 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 257 | |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 258 | // TODO: Refactor this in terms of calls to InterfaceController. |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 259 | private boolean configureIPv4(boolean enabled) { |
| 260 | if (VDBG) Log.d(TAG, "configureIPv4(" + enabled + ")"); |
| 261 | |
| 262 | // TODO: Replace this hard-coded information with dynamically selected |
| 263 | // config passed down to us by a higher layer IP-coordinating element. |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 264 | String ipAsString = null; |
| 265 | int prefixLen = 0; |
| 266 | if (mInterfaceType == ConnectivityManager.TETHERING_USB) { |
| 267 | ipAsString = USB_NEAR_IFACE_ADDR; |
| 268 | prefixLen = USB_PREFIX_LENGTH; |
| 269 | } else if (mInterfaceType == ConnectivityManager.TETHERING_WIFI) { |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 270 | ipAsString = getRandomWifiIPv4Address(); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 271 | prefixLen = WIFI_HOST_IFACE_PREFIX_LENGTH; |
| 272 | } else { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 273 | // BT configures the interface elsewhere: only start DHCP. |
| 274 | final Inet4Address srvAddr = (Inet4Address) numericToInetAddress(BLUETOOTH_IFACE_ADDR); |
| 275 | return configureDhcp(enabled, srvAddr, BLUETOOTH_DHCP_PREFIX_LENGTH); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 276 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 277 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 278 | final LinkAddress linkAddr; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 279 | try { |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 280 | final InterfaceConfiguration ifcg = mNMService.getInterfaceConfig(mIfaceName); |
| 281 | if (ifcg == null) { |
| 282 | mLog.e("Received null interface config"); |
| 283 | return false; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 284 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 285 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 286 | InetAddress addr = numericToInetAddress(ipAsString); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 287 | linkAddr = new LinkAddress(addr, prefixLen); |
| 288 | ifcg.setLinkAddress(linkAddr); |
| 289 | if (mInterfaceType == ConnectivityManager.TETHERING_WIFI) { |
| 290 | // The WiFi stack has ownership of the interface up/down state. |
| 291 | // It is unclear whether the Bluetooth or USB stacks will manage their own |
| 292 | // state. |
| 293 | ifcg.ignoreInterfaceUpDownStatus(); |
| 294 | } else { |
| 295 | if (enabled) { |
| 296 | ifcg.setInterfaceUp(); |
| 297 | } else { |
| 298 | ifcg.setInterfaceDown(); |
| 299 | } |
| 300 | } |
| 301 | ifcg.clearFlag("running"); |
| 302 | mNMService.setInterfaceConfig(mIfaceName, ifcg); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 303 | |
| 304 | if (!configureDhcp(enabled, (Inet4Address) addr, prefixLen)) { |
| 305 | return false; |
| 306 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 307 | } catch (Exception e) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 308 | mLog.e("Error configuring interface " + e); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 312 | // Directly-connected route. |
| 313 | final RouteInfo route = new RouteInfo(linkAddr); |
| 314 | if (enabled) { |
| 315 | mLinkProperties.addLinkAddress(linkAddr); |
| 316 | mLinkProperties.addRoute(route); |
| 317 | } else { |
| 318 | mLinkProperties.removeLinkAddress(linkAddr); |
| 319 | mLinkProperties.removeRoute(route); |
| 320 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 324 | private String getRandomWifiIPv4Address() { |
| 325 | try { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 326 | byte[] bytes = numericToInetAddress(WIFI_HOST_IFACE_ADDR).getAddress(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 327 | bytes[3] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1), FF); |
| 328 | return InetAddress.getByAddress(bytes).getHostAddress(); |
| 329 | } catch (Exception e) { |
| 330 | return WIFI_HOST_IFACE_ADDR; |
| 331 | } |
| 332 | } |
| 333 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 334 | private boolean startIPv6() { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 335 | mInterfaceParams = mDeps.getInterfaceParams(mIfaceName); |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 336 | if (mInterfaceParams == null) { |
| 337 | mLog.e("Failed to find InterfaceParams"); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 338 | stopIPv6(); |
| 339 | return false; |
| 340 | } |
| 341 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 342 | mRaDaemon = mDeps.getRouterAdvertisementDaemon(mInterfaceParams); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 343 | if (!mRaDaemon.start()) { |
| 344 | stopIPv6(); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | private void stopIPv6() { |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 352 | mInterfaceParams = null; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 353 | setRaParams(null); |
| 354 | |
| 355 | if (mRaDaemon != null) { |
| 356 | mRaDaemon.stop(); |
| 357 | mRaDaemon = null; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // IPv6TetheringCoordinator sends updates with carefully curated IPv6-only |
| 362 | // LinkProperties. These have extraneous data filtered out and only the |
| 363 | // necessary prefixes included (per its prefix distribution policy). |
| 364 | // |
| 365 | // TODO: Evaluate using a data structure than is more directly suited to |
| 366 | // communicating only the relevant information. |
| 367 | private void updateUpstreamIPv6LinkProperties(LinkProperties v6only) { |
| 368 | if (mRaDaemon == null) return; |
| 369 | |
| 370 | // Avoid unnecessary work on spurious updates. |
| 371 | if (Objects.equals(mLastIPv6LinkProperties, v6only)) { |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | RaParams params = null; |
| 376 | |
| 377 | if (v6only != null) { |
| 378 | params = new RaParams(); |
| 379 | params.mtu = v6only.getMtu(); |
| 380 | params.hasDefaultRoute = v6only.hasIPv6DefaultRoute(); |
| 381 | |
| 382 | for (LinkAddress linkAddr : v6only.getLinkAddresses()) { |
| 383 | if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue; |
| 384 | |
| 385 | final IpPrefix prefix = new IpPrefix( |
| 386 | linkAddr.getAddress(), linkAddr.getPrefixLength()); |
| 387 | params.prefixes.add(prefix); |
| 388 | |
| 389 | final Inet6Address dnsServer = getLocalDnsIpFor(prefix); |
| 390 | if (dnsServer != null) { |
| 391 | params.dnses.add(dnsServer); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | // If v6only is null, we pass in null to setRaParams(), which handles |
| 396 | // deprecation of any existing RA data. |
| 397 | |
| 398 | setRaParams(params); |
| 399 | mLastIPv6LinkProperties = v6only; |
| 400 | } |
| 401 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 402 | private void configureLocalIPv6Routes( |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 403 | HashSet<IpPrefix> deprecatedPrefixes, HashSet<IpPrefix> newPrefixes) { |
| 404 | // [1] Remove the routes that are deprecated. |
| 405 | if (!deprecatedPrefixes.isEmpty()) { |
| 406 | final ArrayList<RouteInfo> toBeRemoved = |
| 407 | getLocalRoutesFor(mIfaceName, deprecatedPrefixes); |
| 408 | try { |
| 409 | final int removalFailures = mNMService.removeRoutesFromLocalNetwork(toBeRemoved); |
| 410 | if (removalFailures > 0) { |
| 411 | mLog.e(String.format("Failed to remove %d IPv6 routes from local table.", |
| 412 | removalFailures)); |
| 413 | } |
| 414 | } catch (RemoteException e) { |
| 415 | mLog.e("Failed to remove IPv6 routes from local table: " + e); |
| 416 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 417 | |
| 418 | for (RouteInfo route : toBeRemoved) mLinkProperties.removeRoute(route); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // [2] Add only the routes that have not previously been added. |
| 422 | if (newPrefixes != null && !newPrefixes.isEmpty()) { |
| 423 | HashSet<IpPrefix> addedPrefixes = (HashSet) newPrefixes.clone(); |
| 424 | if (mLastRaParams != null) { |
| 425 | addedPrefixes.removeAll(mLastRaParams.prefixes); |
| 426 | } |
| 427 | |
| 428 | if (mLastRaParams == null || mLastRaParams.prefixes.isEmpty()) { |
| 429 | // We need to be able to send unicast RAs, and clients might |
| 430 | // like to ping the default router's link-local address. Note |
| 431 | // that we never remove the link-local route from the network |
| 432 | // until Tethering disables tethering on the interface. We |
| 433 | // only need to add the link-local prefix once, but in the |
| 434 | // event we add it more than once netd silently ignores EEXIST. |
| 435 | addedPrefixes.add(LINK_LOCAL_PREFIX); |
| 436 | } |
| 437 | |
| 438 | if (!addedPrefixes.isEmpty()) { |
| 439 | final ArrayList<RouteInfo> toBeAdded = |
| 440 | getLocalRoutesFor(mIfaceName, addedPrefixes); |
| 441 | try { |
| 442 | // It's safe to call addInterfaceToLocalNetwork() even if |
| 443 | // the interface is already in the local_network. Note also |
| 444 | // that adding routes that already exist does not cause an |
| 445 | // error (EEXIST is silently ignored). |
| 446 | mNMService.addInterfaceToLocalNetwork(mIfaceName, toBeAdded); |
Sehee Park | a1874cc | 2018-06-19 20:07:37 +0900 | [diff] [blame] | 447 | } catch (Exception e) { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 448 | mLog.e("Failed to add IPv6 routes to local table: " + e); |
| 449 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 450 | |
| 451 | for (RouteInfo route : toBeAdded) mLinkProperties.addRoute(route); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 456 | private void configureLocalIPv6Dns( |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 457 | HashSet<Inet6Address> deprecatedDnses, HashSet<Inet6Address> newDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 458 | // TODO: Is this really necessary? Can we not fail earlier if INetd cannot be located? |
| 459 | if (mNetd == null) { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 460 | if (newDnses != null) newDnses.clear(); |
| 461 | mLog.e("No netd service instance available; not setting local IPv6 addresses"); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | // [1] Remove deprecated local DNS IP addresses. |
| 466 | if (!deprecatedDnses.isEmpty()) { |
| 467 | for (Inet6Address dns : deprecatedDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 468 | if (!mInterfaceCtrl.removeAddress(dns, RFC7421_PREFIX_LENGTH)) { |
| 469 | mLog.e("Failed to remove local dns IP " + dns); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 470 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 471 | |
| 472 | mLinkProperties.removeLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | // [2] Add only the local DNS IP addresses that have not previously been added. |
| 477 | if (newDnses != null && !newDnses.isEmpty()) { |
| 478 | final HashSet<Inet6Address> addedDnses = (HashSet) newDnses.clone(); |
| 479 | if (mLastRaParams != null) { |
| 480 | addedDnses.removeAll(mLastRaParams.dnses); |
| 481 | } |
| 482 | |
| 483 | for (Inet6Address dns : addedDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 484 | if (!mInterfaceCtrl.addAddress(dns, RFC7421_PREFIX_LENGTH)) { |
| 485 | mLog.e("Failed to add local dns IP " + dns); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 486 | newDnses.remove(dns); |
| 487 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 488 | |
| 489 | mLinkProperties.addLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
| 493 | try { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 494 | mNetd.tetherApplyDnsInterfaces(); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 495 | } catch (ServiceSpecificException | RemoteException e) { |
| 496 | mLog.e("Failed to update local DNS caching server"); |
| 497 | if (newDnses != null) newDnses.clear(); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | private void setRaParams(RaParams newParams) { |
| 502 | if (mRaDaemon != null) { |
| 503 | final RaParams deprecatedParams = |
| 504 | RaParams.getDeprecatedRaParams(mLastRaParams, newParams); |
| 505 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 506 | configureLocalIPv6Routes(deprecatedParams.prefixes, |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 507 | (newParams != null) ? newParams.prefixes : null); |
| 508 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 509 | configureLocalIPv6Dns(deprecatedParams.dnses, |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 510 | (newParams != null) ? newParams.dnses : null); |
| 511 | |
| 512 | mRaDaemon.buildNewRa(deprecatedParams, newParams); |
| 513 | } |
| 514 | |
| 515 | mLastRaParams = newParams; |
| 516 | } |
| 517 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 518 | private void logMessage(State state, int what) { |
| 519 | mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what))); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 522 | private void sendInterfaceState(int newInterfaceState) { |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 523 | mServingMode = newInterfaceState; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 524 | mTetherController.updateInterfaceState( |
| 525 | TetherInterfaceStateMachine.this, newInterfaceState, mLastError); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 526 | sendLinkProperties(); |
| 527 | } |
| 528 | |
| 529 | private void sendLinkProperties() { |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 530 | mTetherController.updateLinkProperties( |
| 531 | TetherInterfaceStateMachine.this, new LinkProperties(mLinkProperties)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 532 | } |
| 533 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 534 | private void resetLinkProperties() { |
| 535 | mLinkProperties.clear(); |
| 536 | mLinkProperties.setInterfaceName(mIfaceName); |
| 537 | } |
| 538 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 539 | class InitialState extends State { |
| 540 | @Override |
| 541 | public void enter() { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 542 | sendInterfaceState(IControlsTethering.STATE_AVAILABLE); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public boolean processMessage(Message message) { |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 547 | logMessage(this, message.what); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 548 | switch (message.what) { |
| 549 | case CMD_TETHER_REQUESTED: |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 550 | mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 551 | switch (message.arg1) { |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 552 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 553 | transitionTo(mLocalHotspotState); |
| 554 | break; |
| 555 | case IControlsTethering.STATE_TETHERED: |
| 556 | transitionTo(mTetheredState); |
| 557 | break; |
| 558 | default: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 559 | mLog.e("Invalid tethering interface serving state specified."); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 560 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 561 | break; |
| 562 | case CMD_INTERFACE_DOWN: |
| 563 | transitionTo(mUnavailableState); |
| 564 | break; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 565 | case CMD_IPV6_TETHER_UPDATE: |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 566 | updateUpstreamIPv6LinkProperties((LinkProperties) message.obj); |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 567 | break; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 568 | default: |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 569 | return NOT_HANDLED; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 570 | } |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 571 | return HANDLED; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 575 | class BaseServingState extends State { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 576 | @Override |
| 577 | public void enter() { |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 578 | if (!startIPv4()) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 579 | mLastError = ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 580 | return; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 581 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 582 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 583 | try { |
| 584 | mNMService.tetherInterface(mIfaceName); |
| 585 | } catch (Exception e) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 586 | mLog.e("Error Tethering: " + e); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 587 | mLastError = ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 590 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 591 | if (!startIPv6()) { |
| 592 | mLog.e("Failed to startIPv6"); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 593 | // TODO: Make this a fatal error once Bluetooth IPv6 is sorted. |
| 594 | return; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 595 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 598 | @Override |
| 599 | public void exit() { |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 600 | // Note that at this point, we're leaving the tethered state. We can fail any |
| 601 | // of these operations, but it doesn't really change that we have to try them |
| 602 | // all in sequence. |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 603 | stopIPv6(); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 604 | |
| 605 | try { |
| 606 | mNMService.untetherInterface(mIfaceName); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 607 | } catch (Exception e) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 608 | mLastError = ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 609 | mLog.e("Failed to untether interface: " + e); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 612 | stopIPv4(); |
| 613 | |
| 614 | resetLinkProperties(); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 617 | @Override |
| 618 | public boolean processMessage(Message message) { |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 619 | logMessage(this, message.what); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 620 | switch (message.what) { |
| 621 | case CMD_TETHER_UNREQUESTED: |
| 622 | transitionTo(mInitialState); |
| 623 | if (DBG) Log.d(TAG, "Untethered (unrequested)" + mIfaceName); |
| 624 | break; |
| 625 | case CMD_INTERFACE_DOWN: |
| 626 | transitionTo(mUnavailableState); |
| 627 | if (DBG) Log.d(TAG, "Untethered (ifdown)" + mIfaceName); |
| 628 | break; |
| 629 | case CMD_IPV6_TETHER_UPDATE: |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 630 | updateUpstreamIPv6LinkProperties((LinkProperties) message.obj); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 631 | sendLinkProperties(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 632 | break; |
| 633 | case CMD_IP_FORWARDING_ENABLE_ERROR: |
| 634 | case CMD_IP_FORWARDING_DISABLE_ERROR: |
| 635 | case CMD_START_TETHERING_ERROR: |
| 636 | case CMD_STOP_TETHERING_ERROR: |
| 637 | case CMD_SET_DNS_FORWARDERS_ERROR: |
| 638 | mLastError = ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 639 | transitionTo(mInitialState); |
| 640 | break; |
| 641 | default: |
| 642 | return false; |
| 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | } |
| 647 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 648 | // Handling errors in BaseServingState.enter() by transitioning is |
| 649 | // problematic because transitioning during a multi-state jump yields |
| 650 | // a Log.wtf(). Ultimately, there should be only one ServingState, |
| 651 | // and forwarding and NAT rules should be handled by a coordinating |
| 652 | // functional element outside of TetherInterfaceStateMachine. |
| 653 | class LocalHotspotState extends BaseServingState { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 654 | @Override |
| 655 | public void enter() { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 656 | super.enter(); |
| 657 | if (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 658 | transitionTo(mInitialState); |
| 659 | } |
| 660 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 661 | if (DBG) Log.d(TAG, "Local hotspot " + mIfaceName); |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 662 | sendInterfaceState(IControlsTethering.STATE_LOCAL_ONLY); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | @Override |
| 666 | public boolean processMessage(Message message) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 667 | if (super.processMessage(message)) return true; |
| 668 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 669 | logMessage(this, message.what); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 670 | switch (message.what) { |
| 671 | case CMD_TETHER_REQUESTED: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 672 | mLog.e("CMD_TETHER_REQUESTED while in local-only hotspot mode."); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 673 | break; |
| 674 | case CMD_TETHER_CONNECTION_CHANGED: |
| 675 | // Ignored in local hotspot state. |
| 676 | break; |
| 677 | default: |
| 678 | return false; |
| 679 | } |
| 680 | return true; |
| 681 | } |
| 682 | } |
| 683 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 684 | // Handling errors in BaseServingState.enter() by transitioning is |
| 685 | // problematic because transitioning during a multi-state jump yields |
| 686 | // a Log.wtf(). Ultimately, there should be only one ServingState, |
| 687 | // and forwarding and NAT rules should be handled by a coordinating |
| 688 | // functional element outside of TetherInterfaceStateMachine. |
| 689 | class TetheredState extends BaseServingState { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 690 | @Override |
| 691 | public void enter() { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 692 | super.enter(); |
| 693 | if (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 694 | transitionTo(mInitialState); |
| 695 | } |
| 696 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 697 | if (DBG) Log.d(TAG, "Tethered " + mIfaceName); |
| 698 | sendInterfaceState(IControlsTethering.STATE_TETHERED); |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public void exit() { |
| 703 | cleanupUpstream(); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 704 | super.exit(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 705 | } |
| 706 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 707 | private void cleanupUpstream() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 708 | if (mUpstreamIfaceSet == null) return; |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 709 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 710 | for (String ifname : mUpstreamIfaceSet.ifnames) cleanupUpstreamInterface(ifname); |
| 711 | mUpstreamIfaceSet = null; |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | private void cleanupUpstreamInterface(String upstreamIface) { |
| 715 | // Note that we don't care about errors here. |
| 716 | // Sometimes interfaces are gone before we get |
| 717 | // to remove their rules, which generates errors. |
| 718 | // Just do the best we can. |
| 719 | try { |
| 720 | // About to tear down NAT; gather remaining statistics. |
| 721 | mStatsService.forceUpdate(); |
| 722 | } catch (Exception e) { |
| 723 | if (VDBG) Log.e(TAG, "Exception in forceUpdate: " + e.toString()); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 724 | } |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 725 | try { |
| 726 | mNMService.stopInterfaceForwarding(mIfaceName, upstreamIface); |
| 727 | } catch (Exception e) { |
| 728 | if (VDBG) Log.e(TAG, "Exception in removeInterfaceForward: " + e.toString()); |
| 729 | } |
| 730 | try { |
| 731 | mNMService.disableNat(mIfaceName, upstreamIface); |
| 732 | } catch (Exception e) { |
| 733 | if (VDBG) Log.e(TAG, "Exception in disableNat: " + e.toString()); |
| 734 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public boolean processMessage(Message message) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 739 | if (super.processMessage(message)) return true; |
| 740 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 741 | logMessage(this, message.what); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 742 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 743 | case CMD_TETHER_REQUESTED: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 744 | mLog.e("CMD_TETHER_REQUESTED while already tethering."); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 745 | break; |
| 746 | case CMD_TETHER_CONNECTION_CHANGED: |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 747 | final InterfaceSet newUpstreamIfaceSet = (InterfaceSet) message.obj; |
| 748 | if (noChangeInUpstreamIfaceSet(newUpstreamIfaceSet)) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 749 | if (VDBG) Log.d(TAG, "Connection changed noop - dropping"); |
| 750 | break; |
| 751 | } |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 752 | |
| 753 | if (newUpstreamIfaceSet == null) { |
| 754 | cleanupUpstream(); |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | for (String removed : upstreamInterfacesRemoved(newUpstreamIfaceSet)) { |
| 759 | cleanupUpstreamInterface(removed); |
| 760 | } |
| 761 | |
| 762 | final Set<String> added = upstreamInterfacesAdd(newUpstreamIfaceSet); |
| 763 | // This makes the call to cleanupUpstream() in the error |
| 764 | // path for any interface neatly cleanup all the interfaces. |
| 765 | mUpstreamIfaceSet = newUpstreamIfaceSet; |
| 766 | |
| 767 | for (String ifname : added) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 768 | try { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 769 | mNMService.enableNat(mIfaceName, ifname); |
| 770 | mNMService.startInterfaceForwarding(mIfaceName, ifname); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 771 | } catch (Exception e) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 772 | mLog.e("Exception enabling NAT: " + e); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 773 | cleanupUpstream(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 774 | mLastError = ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 775 | transitionTo(mInitialState); |
| 776 | return true; |
| 777 | } |
| 778 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 779 | break; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 780 | default: |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 781 | return false; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 782 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 783 | return true; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 784 | } |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 785 | |
| 786 | private boolean noChangeInUpstreamIfaceSet(InterfaceSet newIfaces) { |
| 787 | if (mUpstreamIfaceSet == null && newIfaces == null) return true; |
| 788 | if (mUpstreamIfaceSet != null && newIfaces != null) { |
| 789 | return mUpstreamIfaceSet.equals(newIfaces); |
| 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | |
| 794 | private Set<String> upstreamInterfacesRemoved(InterfaceSet newIfaces) { |
| 795 | if (mUpstreamIfaceSet == null) return new HashSet<>(); |
| 796 | |
| 797 | final HashSet<String> removed = new HashSet<>(mUpstreamIfaceSet.ifnames); |
| 798 | removed.removeAll(newIfaces.ifnames); |
| 799 | return removed; |
| 800 | } |
| 801 | |
| 802 | private Set<String> upstreamInterfacesAdd(InterfaceSet newIfaces) { |
| 803 | final HashSet<String> added = new HashSet<>(newIfaces.ifnames); |
| 804 | if (mUpstreamIfaceSet != null) added.removeAll(mUpstreamIfaceSet.ifnames); |
| 805 | return added; |
| 806 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Christopher Wiley | 9ad83ab | 2016-05-20 17:51:27 -0700 | [diff] [blame] | 809 | /** |
| 810 | * This state is terminal for the per interface state machine. At this |
| 811 | * point, the master state machine should have removed this interface |
| 812 | * specific state machine from its list of possible recipients of |
| 813 | * tethering requests. The state machine itself will hang around until |
| 814 | * the garbage collector finds it. |
| 815 | */ |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 816 | class UnavailableState extends State { |
| 817 | @Override |
| 818 | public void enter() { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 819 | mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 820 | sendInterfaceState(IControlsTethering.STATE_UNAVAILABLE); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 821 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 822 | } |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 823 | |
| 824 | // Accumulate routes representing "prefixes to be assigned to the local |
| 825 | // interface", for subsequent modification of local_network routing. |
| 826 | private static ArrayList<RouteInfo> getLocalRoutesFor( |
| 827 | String ifname, HashSet<IpPrefix> prefixes) { |
| 828 | final ArrayList<RouteInfo> localRoutes = new ArrayList<RouteInfo>(); |
| 829 | for (IpPrefix ipp : prefixes) { |
| 830 | localRoutes.add(new RouteInfo(ipp, null, ifname)); |
| 831 | } |
| 832 | return localRoutes; |
| 833 | } |
| 834 | |
| 835 | // Given a prefix like 2001:db8::/64 return an address like 2001:db8::1. |
| 836 | private static Inet6Address getLocalDnsIpFor(IpPrefix localPrefix) { |
| 837 | final byte[] dnsBytes = localPrefix.getRawAddress(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 838 | dnsBytes[dnsBytes.length - 1] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 839 | try { |
| 840 | return Inet6Address.getByAddress(null, dnsBytes, 0); |
| 841 | } catch (UnknownHostException e) { |
| 842 | Slog.wtf(TAG, "Failed to construct Inet6Address from: " + localPrefix); |
| 843 | return null; |
| 844 | } |
| 845 | } |
| 846 | |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 847 | private static byte getRandomSanitizedByte(byte dflt, byte... excluded) { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 848 | final byte random = (byte) (new Random()).nextInt(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 849 | for (int value : excluded) { |
| 850 | if (random == value) return dflt; |
| 851 | } |
| 852 | return random; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 853 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 854 | } |