Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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; |
| 18 | |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 19 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 20 | import android.net.INetd; |
| 21 | import android.net.InterfaceConfiguration; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 22 | import android.net.LinkAddress; |
| 23 | import android.net.LinkProperties; |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 24 | import android.net.NetworkInfo; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 25 | import android.net.RouteInfo; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 26 | import android.os.INetworkManagementService; |
| 27 | import android.os.RemoteException; |
| 28 | import android.util.Slog; |
| 29 | |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 30 | import com.android.internal.util.ArrayUtils; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 31 | import com.android.server.net.BaseNetworkObserver; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 32 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 33 | import java.net.Inet4Address; |
| 34 | import java.util.Objects; |
| 35 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 36 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 37 | * Class to manage a 464xlat CLAT daemon. Nat464Xlat is not thread safe and should be manipulated |
| 38 | * from a consistent and unique thread context. It is the responsibility of ConnectivityService to |
| 39 | * call into this class from its own Handler thread. |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 40 | * |
| 41 | * @hide |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 42 | */ |
| 43 | public class Nat464Xlat extends BaseNetworkObserver { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 44 | private static final String TAG = Nat464Xlat.class.getSimpleName(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 45 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 46 | // This must match the interface prefix in clatd.c. |
| 47 | private static final String CLAT_PREFIX = "v4-"; |
| 48 | |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 49 | // The network types on which we will start clatd, |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 50 | // allowing clat only on networks for which we can support IPv6-only. |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 51 | private static final int[] NETWORK_TYPES = { |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 52 | ConnectivityManager.TYPE_MOBILE, |
| 53 | ConnectivityManager.TYPE_WIFI, |
| 54 | ConnectivityManager.TYPE_ETHERNET, |
| 55 | }; |
| 56 | |
| 57 | // The network states in which running clatd is supported. |
| 58 | private static final NetworkInfo.State[] NETWORK_STATES = { |
| 59 | NetworkInfo.State.CONNECTED, |
| 60 | NetworkInfo.State.SUSPENDED, |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 61 | }; |
| 62 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 63 | private final INetd mNetd; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 64 | private final INetworkManagementService mNMService; |
| 65 | |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 66 | // The network we're running on, and its type. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 67 | private final NetworkAgentInfo mNetwork; |
| 68 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 69 | private enum State { |
| 70 | IDLE, // start() not called. Base iface and stacked iface names are null. |
| 71 | STARTING, // start() called. Base iface and stacked iface names are known. |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 72 | RUNNING, // start() called, and the stacked iface is known to be up. |
| 73 | STOPPING; // stop() called, this Nat464Xlat is still registered as a network observer for |
| 74 | // the stacked interface. |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 77 | private String mBaseIface; |
| 78 | private String mIface; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 79 | private State mState = State.IDLE; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 80 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 81 | public Nat464Xlat(NetworkAgentInfo nai, INetd netd, INetworkManagementService nmService) { |
| 82 | mNetd = netd; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 83 | mNMService = nmService; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 84 | mNetwork = nai; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 88 | * Determines whether a network requires clat. |
| 89 | * @param network the NetworkAgentInfo corresponding to the network. |
| 90 | * @return true if the network requires clat, false otherwise. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 91 | */ |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 92 | public static boolean requiresClat(NetworkAgentInfo nai) { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 93 | // TODO: migrate to NetworkCapabilities.TRANSPORT_*. |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 94 | final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType()); |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 95 | final boolean connected = ArrayUtils.contains(NETWORK_STATES, nai.networkInfo.getState()); |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 96 | // We only run clat on networks that don't have a native IPv4 address. |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 97 | final boolean hasIPv4Address = |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 98 | (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address(); |
Yuuki Habu | 8f54b61 | 2018-09-06 09:37:55 +0900 | [diff] [blame] | 99 | final boolean skip464xlat = |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 100 | (nai.netMisc() != null) && nai.netMisc().skip464xlat; |
Yuuki Habu | 8f54b61 | 2018-09-06 09:37:55 +0900 | [diff] [blame] | 101 | return supported && connected && !hasIPv4Address && !skip464xlat; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 104 | /** |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 105 | * @return true if clatd has been started and has not yet stopped. |
| 106 | * A true result corresponds to internal states STARTING and RUNNING. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 107 | */ |
| 108 | public boolean isStarted() { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 109 | return mState != State.IDLE; |
| 110 | } |
| 111 | |
| 112 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 113 | * @return true if clatd has been started but the stacked interface is not yet up. |
| 114 | */ |
| 115 | public boolean isStarting() { |
| 116 | return mState == State.STARTING; |
| 117 | } |
| 118 | |
| 119 | /** |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 120 | * @return true if clatd has been started and the stacked interface is up. |
| 121 | */ |
| 122 | public boolean isRunning() { |
| 123 | return mState == State.RUNNING; |
| 124 | } |
| 125 | |
| 126 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 127 | * @return true if clatd has been stopped. |
| 128 | */ |
| 129 | public boolean isStopping() { |
| 130 | return mState == State.STOPPING; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Start clatd, register this Nat464Xlat as a network observer for the stacked interface, |
| 135 | * and set internal state. |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 136 | */ |
| 137 | private void enterStartingState(String baseIface) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 138 | try { |
| 139 | mNMService.registerObserver(this); |
| 140 | } catch(RemoteException e) { |
| 141 | Slog.e(TAG, |
| 142 | "startClat: Can't register interface observer for clat on " + mNetwork.name()); |
| 143 | return; |
| 144 | } |
| 145 | try { |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 146 | mNetd.clatdStart(baseIface); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 147 | } catch(RemoteException|IllegalStateException e) { |
| 148 | Slog.e(TAG, "Error starting clatd on " + baseIface, e); |
| 149 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 150 | mIface = CLAT_PREFIX + baseIface; |
| 151 | mBaseIface = baseIface; |
| 152 | mState = State.STARTING; |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 155 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 156 | * Enter running state just after getting confirmation that the stacked interface is up, and |
| 157 | * turn ND offload off if on WiFi. |
| 158 | */ |
| 159 | private void enterRunningState() { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 160 | mState = State.RUNNING; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Stop clatd, and turn ND offload on if it had been turned off. |
| 165 | */ |
| 166 | private void enterStoppingState() { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 167 | try { |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 168 | mNetd.clatdStop(mBaseIface); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 169 | } catch(RemoteException|IllegalStateException e) { |
| 170 | Slog.e(TAG, "Error stopping clatd on " + mBaseIface, e); |
| 171 | } |
| 172 | |
| 173 | mState = State.STOPPING; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Unregister as a base observer for the stacked interface, and clear internal state. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 178 | */ |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 179 | private void enterIdleState() { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 180 | try { |
| 181 | mNMService.unregisterObserver(this); |
| 182 | } catch(RemoteException|IllegalStateException e) { |
| 183 | Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface, e); |
| 184 | } |
| 185 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 186 | mIface = null; |
| 187 | mBaseIface = null; |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 188 | mState = State.IDLE; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 192 | * Starts the clat daemon. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 193 | */ |
| 194 | public void start() { |
| 195 | if (isStarted()) { |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 196 | Slog.e(TAG, "startClat: already started"); |
| 197 | return; |
| 198 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 199 | |
| 200 | if (mNetwork.linkProperties == null) { |
| 201 | Slog.e(TAG, "startClat: Can't start clat with null LinkProperties"); |
| 202 | return; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 203 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 204 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 205 | String baseIface = mNetwork.linkProperties.getInterfaceName(); |
| 206 | if (baseIface == null) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 207 | Slog.e(TAG, "startClat: Can't start clat on null interface"); |
| 208 | return; |
| 209 | } |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 210 | // TODO: should we only do this if mNetd.clatdStart() succeeds? |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 211 | Slog.i(TAG, "Starting clatd on " + baseIface); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 212 | enterStartingState(baseIface); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 216 | * Stops the clat daemon. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 217 | */ |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 218 | public void stop() { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 219 | if (!isStarted()) { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 220 | return; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 221 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 222 | Slog.i(TAG, "Stopping clatd on " + mBaseIface); |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 223 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 224 | boolean wasStarting = isStarting(); |
| 225 | enterStoppingState(); |
| 226 | if (wasStarting) { |
| 227 | enterIdleState(); |
| 228 | } |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 231 | /** |
junyulai | cafa7c9 | 2018-06-05 16:10:04 +0800 | [diff] [blame] | 232 | * Copies the stacked clat link in oldLp, if any, to the passed LinkProperties. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 233 | * This is necessary because the LinkProperties in mNetwork come from the transport layer, which |
| 234 | * has no idea that 464xlat is running on top of it. |
| 235 | */ |
junyulai | cafa7c9 | 2018-06-05 16:10:04 +0800 | [diff] [blame] | 236 | public void fixupLinkProperties(LinkProperties oldLp, LinkProperties lp) { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 237 | if (!isRunning()) { |
| 238 | return; |
| 239 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 240 | if (lp == null || lp.getAllInterfaceNames().contains(mIface)) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | Slog.d(TAG, "clatd running, updating NAI for " + mIface); |
| 245 | for (LinkProperties stacked: oldLp.getStackedLinks()) { |
| 246 | if (Objects.equals(mIface, stacked.getInterfaceName())) { |
| 247 | lp.addStackedLink(stacked); |
| 248 | return; |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 253 | private LinkProperties makeLinkProperties(LinkAddress clatAddress) { |
| 254 | LinkProperties stacked = new LinkProperties(); |
| 255 | stacked.setInterfaceName(mIface); |
| 256 | |
| 257 | // Although the clat interface is a point-to-point tunnel, we don't |
| 258 | // point the route directly at the interface because some apps don't |
| 259 | // understand routes without gateways (see, e.g., http://b/9597256 |
| 260 | // http://b/9597516). Instead, set the next hop of the route to the |
| 261 | // clat IPv4 address itself (for those apps, it doesn't matter what |
| 262 | // the IP of the gateway is, only that there is one). |
| 263 | RouteInfo ipv4Default = new RouteInfo( |
| 264 | new LinkAddress(Inet4Address.ANY, 0), |
| 265 | clatAddress.getAddress(), mIface); |
| 266 | stacked.addRoute(ipv4Default); |
| 267 | stacked.addLinkAddress(clatAddress); |
| 268 | return stacked; |
| 269 | } |
| 270 | |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 271 | private LinkAddress getLinkAddress(String iface) { |
| 272 | try { |
| 273 | InterfaceConfiguration config = mNMService.getInterfaceConfig(iface); |
| 274 | return config.getLinkAddress(); |
| 275 | } catch(RemoteException|IllegalStateException e) { |
| 276 | Slog.e(TAG, "Error getting link properties: " + e); |
| 277 | return null; |
| 278 | } |
| 279 | } |
| 280 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 281 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 282 | * Adds stacked link on base link and transitions to RUNNING state. |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 283 | */ |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 284 | private void handleInterfaceLinkStateChanged(String iface, boolean up) { |
| 285 | if (!isStarting() || !up || !Objects.equals(mIface, iface)) { |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 288 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 289 | LinkAddress clatAddress = getLinkAddress(iface); |
| 290 | if (clatAddress == null) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 291 | Slog.e(TAG, "clatAddress was null for stacked iface " + iface); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 292 | return; |
| 293 | } |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 294 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 295 | Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s", |
| 296 | mIface, mIface, mBaseIface)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 297 | enterRunningState(); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 298 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 299 | lp.addStackedLink(makeLinkProperties(clatAddress)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 300 | mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 301 | } |
| 302 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 303 | /** |
| 304 | * Removes stacked link on base link and transitions to IDLE state. |
| 305 | */ |
| 306 | private void handleInterfaceRemoved(String iface) { |
| 307 | if (!Objects.equals(mIface, iface)) { |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 310 | if (!isRunning() && !isStopping()) { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | Slog.i(TAG, "interface " + iface + " removed"); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 315 | if (!isStopping()) { |
| 316 | // Ensure clatd is stopped if stop() has not been called: this likely means that clatd |
| 317 | // has crashed. |
| 318 | enterStoppingState(); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 319 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 320 | enterIdleState(); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 321 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 322 | lp.removeStackedLink(iface); |
| 323 | mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void interfaceLinkStateChanged(String iface, boolean up) { |
| 328 | mNetwork.handler().post(() -> { handleInterfaceLinkStateChanged(iface, up); }); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void interfaceRemoved(String iface) { |
| 333 | mNetwork.handler().post(() -> { handleInterfaceRemoved(iface); }); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 334 | } |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 335 | |
| 336 | @Override |
| 337 | public String toString() { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 338 | return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState; |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 339 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 340 | } |