blob: 2646d7669d794e8e38225da02099ad7227131ee6 [file] [log] [blame]
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +09001/*
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
17package com.android.server.connectivity;
18
Lorenzo Colitti853d7412016-03-03 17:17:29 +090019import android.net.ConnectivityManager;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090020import android.net.INetd;
Lorenzo Colitti6998fa82019-01-08 10:04:25 +090021import android.net.InetAddresses;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090022import android.net.InterfaceConfiguration;
Lorenzo Colitti6998fa82019-01-08 10:04:25 +090023import android.net.IpPrefix;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090024import android.net.LinkAddress;
25import android.net.LinkProperties;
Erik Kline3c182162017-09-21 17:28:10 +090026import android.net.NetworkInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090027import android.net.RouteInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090028import android.os.INetworkManagementService;
29import android.os.RemoteException;
Lorenzo Colittid593e292019-02-19 13:21:56 +090030import android.os.ServiceSpecificException;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090031import android.util.Slog;
32
Lorenzo Colittid593e292019-02-19 13:21:56 +090033import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi50d46a42017-08-31 14:29:51 +000034import com.android.internal.util.ArrayUtils;
Hugo Benichief502882017-09-01 01:23:32 +000035import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090036
Hugo Benichi4f6f1392017-06-29 14:04:13 +090037import java.net.Inet4Address;
Lorenzo Colitti6998fa82019-01-08 10:04:25 +090038import java.net.Inet6Address;
Hugo Benichi4f6f1392017-06-29 14:04:13 +090039import java.util.Objects;
40
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090041/**
Hugo Benichief502882017-09-01 01:23:32 +000042 * Class to manage a 464xlat CLAT daemon. Nat464Xlat is not thread safe and should be manipulated
43 * from a consistent and unique thread context. It is the responsibility of ConnectivityService to
44 * call into this class from its own Handler thread.
Hugo Benichib577d652017-06-27 15:13:20 +090045 *
46 * @hide
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090047 */
48public class Nat464Xlat extends BaseNetworkObserver {
Hugo Benichib577d652017-06-27 15:13:20 +090049 private static final String TAG = Nat464Xlat.class.getSimpleName();
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090050
Lorenzo Colitti95439462014-10-09 13:44:48 +090051 // This must match the interface prefix in clatd.c.
52 private static final String CLAT_PREFIX = "v4-";
53
Erik Kline3c182162017-09-21 17:28:10 +090054 // The network types on which we will start clatd,
Hugo Benichib577d652017-06-27 15:13:20 +090055 // allowing clat only on networks for which we can support IPv6-only.
Lorenzo Colitti853d7412016-03-03 17:17:29 +090056 private static final int[] NETWORK_TYPES = {
Erik Kline3c182162017-09-21 17:28:10 +090057 ConnectivityManager.TYPE_MOBILE,
58 ConnectivityManager.TYPE_WIFI,
59 ConnectivityManager.TYPE_ETHERNET,
60 };
61
62 // The network states in which running clatd is supported.
63 private static final NetworkInfo.State[] NETWORK_STATES = {
64 NetworkInfo.State.CONNECTED,
65 NetworkInfo.State.SUSPENDED,
Lorenzo Colitti853d7412016-03-03 17:17:29 +090066 };
67
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090068 private final INetd mNetd;
Lorenzo Colitti95439462014-10-09 13:44:48 +090069 private final INetworkManagementService mNMService;
70
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090071 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090072 private final NetworkAgentInfo mNetwork;
73
Hugo Benichi4f6f1392017-06-29 14:04:13 +090074 private enum State {
Lorenzo Colittid593e292019-02-19 13:21:56 +090075 IDLE, // start() not called. Base iface and stacked iface names are null.
76 DISCOVERING, // same as IDLE, except prefix discovery in progress.
77 STARTING, // start() called. Base iface and stacked iface names are known.
78 RUNNING, // start() called, and the stacked iface is known to be up.
Hugo Benichi4f6f1392017-06-29 14:04:13 +090079 }
80
Lorenzo Colitti6998fa82019-01-08 10:04:25 +090081 private IpPrefix mNat64Prefix;
Lorenzo Colitti95439462014-10-09 13:44:48 +090082 private String mBaseIface;
83 private String mIface;
Lorenzo Colitti6998fa82019-01-08 10:04:25 +090084 private Inet6Address mIPv6Address;
Hugo Benichief502882017-09-01 01:23:32 +000085 private State mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +090086
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090087 public Nat464Xlat(NetworkAgentInfo nai, INetd netd, INetworkManagementService nmService) {
88 mNetd = netd;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090089 mNMService = nmService;
Lorenzo Colitti95439462014-10-09 13:44:48 +090090 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090091 }
92
93 /**
Lorenzo Colittid593e292019-02-19 13:21:56 +090094 * Whether to attempt 464xlat on this network. This is true for an IPv6-only network that is
95 * currently connected and where the NetworkAgent has not disabled 464xlat. It is the signal to
96 * enable NAT64 prefix discovery.
97 *
Paul Jensen3b759822014-05-13 11:44:01 -040098 * @param network the NetworkAgentInfo corresponding to the network.
99 * @return true if the network requires clat, false otherwise.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900100 */
Lorenzo Colittid593e292019-02-19 13:21:56 +0900101 @VisibleForTesting
102 protected static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +0900103 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Hugo Benichib577d652017-06-27 15:13:20 +0900104 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Erik Kline3c182162017-09-21 17:28:10 +0900105 final boolean connected = ArrayUtils.contains(NETWORK_STATES, nai.networkInfo.getState());
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900106
Lorenzo Colittid593e292019-02-19 13:21:56 +0900107 // Only run clat on networks that have a global IPv6 address and don't have a native IPv4
108 // address.
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900109 LinkProperties lp = nai.linkProperties;
Lorenzo Colittid593e292019-02-19 13:21:56 +0900110 final boolean isIpv6OnlyNetwork = (lp != null) && lp.hasGlobalIPv6Address()
111 && !lp.hasIPv4Address();
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900112
113 // If the network tells us it doesn't use clat, respect that.
114 final boolean skip464xlat = (nai.netMisc() != null) && nai.netMisc().skip464xlat;
115
Lorenzo Colittid593e292019-02-19 13:21:56 +0900116 return supported && connected && isIpv6OnlyNetwork && !skip464xlat;
117 }
118
119 /**
120 * Whether the clat demon should be started on this network now. This is true if requiresClat is
121 * true and a NAT64 prefix has been discovered.
122 *
123 * @param nai the NetworkAgentInfo corresponding to the network.
124 * @return true if the network should start clat, false otherwise.
125 */
126 @VisibleForTesting
127 protected static boolean shouldStartClat(NetworkAgentInfo nai) {
128 LinkProperties lp = nai.linkProperties;
129 return requiresClat(nai) && lp != null && lp.getNat64Prefix() != null;
130 }
131
132 /**
133 * @return true if we have started prefix discovery and not yet stopped it (regardless of
134 * whether it is still running or has succeeded).
135 * A true result corresponds to internal states DISCOVERING, STARTING and RUNNING.
136 */
137 public boolean isPrefixDiscoveryStarted() {
138 return mState == State.DISCOVERING || isStarted();
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900139 }
140
Lorenzo Colitti95439462014-10-09 13:44:48 +0900141 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900142 * @return true if clatd has been started and has not yet stopped.
143 * A true result corresponds to internal states STARTING and RUNNING.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900144 */
145 public boolean isStarted() {
Lorenzo Colittid593e292019-02-19 13:21:56 +0900146 return (mState == State.STARTING || mState == State.RUNNING);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900147 }
148
149 /**
Hugo Benichief502882017-09-01 01:23:32 +0000150 * @return true if clatd has been started but the stacked interface is not yet up.
151 */
152 public boolean isStarting() {
153 return mState == State.STARTING;
154 }
155
156 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900157 * @return true if clatd has been started and the stacked interface is up.
158 */
159 public boolean isRunning() {
160 return mState == State.RUNNING;
161 }
162
163 /**
Hugo Benichief502882017-09-01 01:23:32 +0000164 * Start clatd, register this Nat464Xlat as a network observer for the stacked interface,
165 * and set internal state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900166 */
167 private void enterStartingState(String baseIface) {
Hugo Benichief502882017-09-01 01:23:32 +0000168 try {
169 mNMService.registerObserver(this);
Lorenzo Colittidf595632019-01-08 14:43:37 +0900170 } catch (RemoteException e) {
171 Slog.e(TAG, "Can't register interface observer for clat on " + mNetwork.name());
Hugo Benichief502882017-09-01 01:23:32 +0000172 return;
173 }
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900174
175 String addrStr = null;
Hugo Benichief502882017-09-01 01:23:32 +0000176 try {
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900177 addrStr = mNetd.clatdStart(baseIface, mNat64Prefix.toString());
Lorenzo Colittib313e0c2019-02-27 10:35:10 +0900178 } catch (RemoteException | ServiceSpecificException e) {
179 Slog.e(TAG, "Error starting clatd on " + baseIface + ": " + e);
Hugo Benichief502882017-09-01 01:23:32 +0000180 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900181 mIface = CLAT_PREFIX + baseIface;
182 mBaseIface = baseIface;
183 mState = State.STARTING;
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900184 try {
185 mIPv6Address = (Inet6Address) InetAddresses.parseNumericAddress(addrStr);
186 } catch (ClassCastException | IllegalArgumentException | NullPointerException e) {
187 Slog.e(TAG, "Invalid IPv6 address " + addrStr);
188 }
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900189 }
190
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900191 /**
Hugo Benichief502882017-09-01 01:23:32 +0000192 * Enter running state just after getting confirmation that the stacked interface is up, and
193 * turn ND offload off if on WiFi.
194 */
195 private void enterRunningState() {
Hugo Benichief502882017-09-01 01:23:32 +0000196 mState = State.RUNNING;
197 }
198
199 /**
Hugo Benichief502882017-09-01 01:23:32 +0000200 * Unregister as a base observer for the stacked interface, and clear internal state.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900201 */
Lorenzo Colittid593e292019-02-19 13:21:56 +0900202 private void leaveStartedState() {
Hugo Benichief502882017-09-01 01:23:32 +0000203 try {
204 mNMService.unregisterObserver(this);
Lorenzo Colittidf595632019-01-08 14:43:37 +0900205 } catch (RemoteException | IllegalStateException e) {
Lorenzo Colittib313e0c2019-02-27 10:35:10 +0900206 Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface + ": " + e);
Hugo Benichief502882017-09-01 01:23:32 +0000207 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900208 mIface = null;
209 mBaseIface = null;
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900210 mState = State.IDLE;
Lorenzo Colittid593e292019-02-19 13:21:56 +0900211 if (requiresClat(mNetwork)) {
212 mState = State.DISCOVERING;
213 } else {
214 stopPrefixDiscovery();
215 mState = State.IDLE;
216 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900217 }
218
Lorenzo Colittid593e292019-02-19 13:21:56 +0900219 @VisibleForTesting
220 protected void start() {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900221 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900222 Slog.e(TAG, "startClat: already started");
223 return;
224 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900225
226 if (mNetwork.linkProperties == null) {
227 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
228 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900229 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900230
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900231 String baseIface = mNetwork.linkProperties.getInterfaceName();
232 if (baseIface == null) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900233 Slog.e(TAG, "startClat: Can't start clat on null interface");
234 return;
235 }
Lorenzo Colitti9307ca22019-01-12 01:54:23 +0900236 // TODO: should we only do this if mNetd.clatdStart() succeeds?
Hugo Benichief502882017-09-01 01:23:32 +0000237 Slog.i(TAG, "Starting clatd on " + baseIface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900238 enterStartingState(baseIface);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900239 }
240
Lorenzo Colittid593e292019-02-19 13:21:56 +0900241 @VisibleForTesting
242 protected void stop() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900243 if (!isStarted()) {
Lorenzo Colittidf595632019-01-08 14:43:37 +0900244 Slog.e(TAG, "stopClat: already stopped");
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900245 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900246 }
Hugo Benichi50d46a42017-08-31 14:29:51 +0000247
Lorenzo Colittidf595632019-01-08 14:43:37 +0900248 Slog.i(TAG, "Stopping clatd on " + mBaseIface);
249 try {
250 mNetd.clatdStop(mBaseIface);
Lorenzo Colittib313e0c2019-02-27 10:35:10 +0900251 } catch (RemoteException | ServiceSpecificException e) {
252 Slog.e(TAG, "Error stopping clatd on " + mBaseIface + ": " + e);
Lorenzo Colittidf595632019-01-08 14:43:37 +0900253 }
254
255 String iface = mIface;
256 boolean wasRunning = isRunning();
257
Lorenzo Colittid593e292019-02-19 13:21:56 +0900258 // Change state before updating LinkProperties. handleUpdateLinkProperties ends up calling
259 // fixupLinkProperties, and if at that time the state is still RUNNING, fixupLinkProperties
260 // would wrongly inform ConnectivityService that there is still a stacked interface.
261 leaveStartedState();
Lorenzo Colittidf595632019-01-08 14:43:37 +0900262
263 if (wasRunning) {
264 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
265 lp.removeStackedLink(iface);
266 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Hugo Benichief502882017-09-01 01:23:32 +0000267 }
Paul Jensen3b759822014-05-13 11:44:01 -0400268 }
269
Lorenzo Colittid593e292019-02-19 13:21:56 +0900270 private void startPrefixDiscovery() {
271 try {
272 mNetd.resolverStartPrefix64Discovery(getNetId());
273 mState = State.DISCOVERING;
274 } catch (RemoteException | ServiceSpecificException e) {
Lorenzo Colittib313e0c2019-02-27 10:35:10 +0900275 Slog.e(TAG, "Error starting prefix discovery on netId " + getNetId() + ": " + e);
Lorenzo Colittid593e292019-02-19 13:21:56 +0900276 }
277 }
278
279 private void stopPrefixDiscovery() {
280 try {
281 mNetd.resolverStopPrefix64Discovery(getNetId());
282 } catch (RemoteException | ServiceSpecificException e) {
Lorenzo Colittib313e0c2019-02-27 10:35:10 +0900283 Slog.e(TAG, "Error stopping prefix discovery on netId " + getNetId() + ": " + e);
Lorenzo Colittid593e292019-02-19 13:21:56 +0900284 }
285 }
286
287 /**
288 * Starts/stops NAT64 prefix discovery and clatd as necessary.
289 */
290 public void update() {
291 // TODO: turn this class into a proper StateMachine. // http://b/126113090
292 if (requiresClat(mNetwork)) {
293 if (!isPrefixDiscoveryStarted()) {
294 startPrefixDiscovery();
295 } else if (shouldStartClat(mNetwork)) {
296 // NAT64 prefix detected. Start clatd.
297 // TODO: support the NAT64 prefix changing after it's been discovered. There is no
298 // need to support this at the moment because it cannot happen without changes to
299 // the Dns64Configuration code in netd.
300 start();
301 } else {
302 // NAT64 prefix removed. Stop clatd and go back into DISCOVERING state.
303 stop();
304 }
305 } else {
306 // Network no longer requires clat. Stop clat and prefix discovery.
307 if (isStarted()) {
308 stop();
309 } else if (isPrefixDiscoveryStarted()) {
310 leaveStartedState();
311 }
312 }
313 }
314
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900315 public void setNat64Prefix(IpPrefix nat64Prefix) {
316 mNat64Prefix = nat64Prefix;
317 }
318
Lorenzo Colitti95439462014-10-09 13:44:48 +0900319 /**
junyulaicafa7c92018-06-05 16:10:04 +0800320 * Copies the stacked clat link in oldLp, if any, to the passed LinkProperties.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900321 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
322 * has no idea that 464xlat is running on top of it.
323 */
junyulaicafa7c92018-06-05 16:10:04 +0800324 public void fixupLinkProperties(LinkProperties oldLp, LinkProperties lp) {
Lorenzo Colitti6998fa82019-01-08 10:04:25 +0900325 lp.setNat64Prefix(mNat64Prefix);
326
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900327 if (!isRunning()) {
328 return;
329 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900330 if (lp == null || lp.getAllInterfaceNames().contains(mIface)) {
331 return;
332 }
333
334 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
335 for (LinkProperties stacked: oldLp.getStackedLinks()) {
336 if (Objects.equals(mIface, stacked.getInterfaceName())) {
337 lp.addStackedLink(stacked);
338 return;
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900339 }
340 }
341 }
342
Lorenzo Colitti95439462014-10-09 13:44:48 +0900343 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
344 LinkProperties stacked = new LinkProperties();
345 stacked.setInterfaceName(mIface);
346
347 // Although the clat interface is a point-to-point tunnel, we don't
348 // point the route directly at the interface because some apps don't
349 // understand routes without gateways (see, e.g., http://b/9597256
350 // http://b/9597516). Instead, set the next hop of the route to the
351 // clat IPv4 address itself (for those apps, it doesn't matter what
352 // the IP of the gateway is, only that there is one).
353 RouteInfo ipv4Default = new RouteInfo(
354 new LinkAddress(Inet4Address.ANY, 0),
355 clatAddress.getAddress(), mIface);
356 stacked.addRoute(ipv4Default);
357 stacked.addLinkAddress(clatAddress);
358 return stacked;
359 }
360
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900361 private LinkAddress getLinkAddress(String iface) {
362 try {
363 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
364 return config.getLinkAddress();
Lorenzo Colittidf595632019-01-08 14:43:37 +0900365 } catch (RemoteException | IllegalStateException e) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900366 Slog.e(TAG, "Error getting link properties: " + e);
367 return null;
368 }
369 }
370
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900371 /**
Hugo Benichief502882017-09-01 01:23:32 +0000372 * Adds stacked link on base link and transitions to RUNNING state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900373 */
Hugo Benichief502882017-09-01 01:23:32 +0000374 private void handleInterfaceLinkStateChanged(String iface, boolean up) {
Lorenzo Colittid593e292019-02-19 13:21:56 +0900375 // TODO: if we call start(), then stop(), then start() again, and the
376 // interfaceLinkStateChanged notification for the first start is delayed past the first
377 // stop, then the code becomes out of sync with system state and will behave incorrectly.
378 //
379 // This is not trivial to fix because:
380 // 1. It is not guaranteed that start() will eventually result in the interface coming up,
381 // because there could be an error starting clat (e.g., if the interface goes down before
382 // the packet socket can be bound).
383 // 2. If start is called multiple times, there is nothing in the interfaceLinkStateChanged
384 // notification that says which start() call the interface was created by.
385 //
386 // Once this code is converted to StateMachine, it will be possible to use deferMessage to
387 // ensure it stays in STARTING state until the interfaceLinkStateChanged notification fires,
388 // and possibly use a timeout (or provide some guarantees at the lower layer) to address #1.
Hugo Benichief502882017-09-01 01:23:32 +0000389 if (!isStarting() || !up || !Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000390 return;
391 }
Hugo Benichief502882017-09-01 01:23:32 +0000392
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900393 LinkAddress clatAddress = getLinkAddress(iface);
394 if (clatAddress == null) {
Hugo Benichief502882017-09-01 01:23:32 +0000395 Slog.e(TAG, "clatAddress was null for stacked iface " + iface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900396 return;
397 }
Hugo Benichief502882017-09-01 01:23:32 +0000398
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900399 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
400 mIface, mIface, mBaseIface));
Hugo Benichief502882017-09-01 01:23:32 +0000401 enterRunningState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900402 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
403 lp.addStackedLink(makeLinkProperties(clatAddress));
Hugo Benichief502882017-09-01 01:23:32 +0000404 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900405 }
406
Hugo Benichief502882017-09-01 01:23:32 +0000407 /**
408 * Removes stacked link on base link and transitions to IDLE state.
409 */
410 private void handleInterfaceRemoved(String iface) {
411 if (!Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000412 return;
413 }
Lorenzo Colittidf595632019-01-08 14:43:37 +0900414 if (!isRunning()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900415 return;
416 }
417
418 Slog.i(TAG, "interface " + iface + " removed");
Lorenzo Colittidf595632019-01-08 14:43:37 +0900419 // If we're running, and the interface was removed, then we didn't call stop(), and it's
420 // likely that clatd crashed. Ensure we call stop() so we can start clatd again. Calling
421 // stop() will also update LinkProperties, and if clatd crashed, the LinkProperties update
422 // will cause ConnectivityService to call start() again.
423 stop();
Hugo Benichief502882017-09-01 01:23:32 +0000424 }
425
426 @Override
427 public void interfaceLinkStateChanged(String iface, boolean up) {
428 mNetwork.handler().post(() -> { handleInterfaceLinkStateChanged(iface, up); });
429 }
430
431 @Override
432 public void interfaceRemoved(String iface) {
433 mNetwork.handler().post(() -> { handleInterfaceRemoved(iface); });
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900434 }
Hugo Benichib577d652017-06-27 15:13:20 +0900435
436 @Override
437 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900438 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900439 }
Lorenzo Colittid593e292019-02-19 13:21:56 +0900440
441 @VisibleForTesting
442 protected int getNetId() {
443 return mNetwork.network.netId;
444 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900445}