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