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