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; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 21 | import android.net.InetAddresses; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 22 | import android.net.InterfaceConfiguration; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 23 | import android.net.IpPrefix; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 24 | import android.net.LinkAddress; |
| 25 | import android.net.LinkProperties; |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 26 | import android.net.NetworkInfo; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 27 | import android.net.RouteInfo; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 28 | import android.os.INetworkManagementService; |
| 29 | import android.os.RemoteException; |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 30 | import android.os.ServiceSpecificException; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 31 | import android.util.Slog; |
| 32 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 33 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 34 | import com.android.internal.util.ArrayUtils; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 35 | import com.android.server.net.BaseNetworkObserver; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 36 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 37 | import java.net.Inet4Address; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 38 | import java.net.Inet6Address; |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 39 | import java.util.Objects; |
| 40 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 41 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 42 | * 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 Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 45 | * |
| 46 | * @hide |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 47 | */ |
| 48 | public class Nat464Xlat extends BaseNetworkObserver { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 49 | private static final String TAG = Nat464Xlat.class.getSimpleName(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 50 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 51 | // This must match the interface prefix in clatd.c. |
| 52 | private static final String CLAT_PREFIX = "v4-"; |
| 53 | |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 54 | // The network types on which we will start clatd, |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 55 | // allowing clat only on networks for which we can support IPv6-only. |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 56 | private static final int[] NETWORK_TYPES = { |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 57 | 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 Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 66 | }; |
| 67 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 68 | private final INetd mNetd; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 69 | private final INetworkManagementService mNMService; |
| 70 | |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 71 | // The network we're running on, and its type. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 72 | private final NetworkAgentInfo mNetwork; |
| 73 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 74 | private enum State { |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 75 | 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 Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 81 | private IpPrefix mNat64Prefix; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 82 | private String mBaseIface; |
| 83 | private String mIface; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 84 | private Inet6Address mIPv6Address; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 85 | private State mState = State.IDLE; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 86 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 87 | public Nat464Xlat(NetworkAgentInfo nai, INetd netd, INetworkManagementService nmService) { |
| 88 | mNetd = netd; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 89 | mNMService = nmService; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 90 | mNetwork = nai; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 94 | * 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 Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 98 | * @param network the NetworkAgentInfo corresponding to the network. |
| 99 | * @return true if the network requires clat, false otherwise. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 100 | */ |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 101 | @VisibleForTesting |
| 102 | protected static boolean requiresClat(NetworkAgentInfo nai) { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 103 | // TODO: migrate to NetworkCapabilities.TRANSPORT_*. |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 104 | final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType()); |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 105 | final boolean connected = ArrayUtils.contains(NETWORK_STATES, nai.networkInfo.getState()); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 106 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 107 | // Only run clat on networks that have a global IPv6 address and don't have a native IPv4 |
| 108 | // address. |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 109 | LinkProperties lp = nai.linkProperties; |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 110 | final boolean isIpv6OnlyNetwork = (lp != null) && lp.hasGlobalIPv6Address() |
| 111 | && !lp.hasIPv4Address(); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 112 | |
| 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 Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 116 | 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 Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 141 | /** |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 142 | * @return true if clatd has been started and has not yet stopped. |
| 143 | * A true result corresponds to internal states STARTING and RUNNING. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 144 | */ |
| 145 | public boolean isStarted() { |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 146 | return (mState == State.STARTING || mState == State.RUNNING); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 150 | * @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 Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 157 | * @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 Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 164 | * Start clatd, register this Nat464Xlat as a network observer for the stacked interface, |
| 165 | * and set internal state. |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 166 | */ |
| 167 | private void enterStartingState(String baseIface) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 168 | try { |
| 169 | mNMService.registerObserver(this); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 170 | } catch (RemoteException e) { |
| 171 | Slog.e(TAG, "Can't register interface observer for clat on " + mNetwork.name()); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 172 | return; |
| 173 | } |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 174 | |
| 175 | String addrStr = null; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 176 | try { |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 177 | addrStr = mNetd.clatdStart(baseIface, mNat64Prefix.toString()); |
Lorenzo Colitti | b313e0c | 2019-02-27 10:35:10 +0900 | [diff] [blame] | 178 | } catch (RemoteException | ServiceSpecificException e) { |
| 179 | Slog.e(TAG, "Error starting clatd on " + baseIface + ": " + e); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 180 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 181 | mIface = CLAT_PREFIX + baseIface; |
| 182 | mBaseIface = baseIface; |
| 183 | mState = State.STARTING; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 184 | try { |
| 185 | mIPv6Address = (Inet6Address) InetAddresses.parseNumericAddress(addrStr); |
| 186 | } catch (ClassCastException | IllegalArgumentException | NullPointerException e) { |
| 187 | Slog.e(TAG, "Invalid IPv6 address " + addrStr); |
| 188 | } |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 191 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 192 | * 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 Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 196 | mState = State.RUNNING; |
| 197 | } |
| 198 | |
| 199 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 200 | * 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] | 201 | */ |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 202 | private void leaveStartedState() { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 203 | try { |
| 204 | mNMService.unregisterObserver(this); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 205 | } catch (RemoteException | IllegalStateException e) { |
Lorenzo Colitti | b313e0c | 2019-02-27 10:35:10 +0900 | [diff] [blame] | 206 | Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface + ": " + e); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 207 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 208 | mIface = null; |
| 209 | mBaseIface = null; |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 210 | mState = State.IDLE; |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 211 | if (requiresClat(mNetwork)) { |
| 212 | mState = State.DISCOVERING; |
| 213 | } else { |
| 214 | stopPrefixDiscovery(); |
| 215 | mState = State.IDLE; |
| 216 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 219 | @VisibleForTesting |
| 220 | protected void start() { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 221 | if (isStarted()) { |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 222 | Slog.e(TAG, "startClat: already started"); |
| 223 | return; |
| 224 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 225 | |
| 226 | if (mNetwork.linkProperties == null) { |
| 227 | Slog.e(TAG, "startClat: Can't start clat with null LinkProperties"); |
| 228 | return; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 229 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 230 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 231 | String baseIface = mNetwork.linkProperties.getInterfaceName(); |
| 232 | if (baseIface == null) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 233 | Slog.e(TAG, "startClat: Can't start clat on null interface"); |
| 234 | return; |
| 235 | } |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 236 | // TODO: should we only do this if mNetd.clatdStart() succeeds? |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 237 | Slog.i(TAG, "Starting clatd on " + baseIface); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 238 | enterStartingState(baseIface); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 241 | @VisibleForTesting |
| 242 | protected void stop() { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 243 | if (!isStarted()) { |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 244 | Slog.e(TAG, "stopClat: already stopped"); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 245 | return; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 246 | } |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 247 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 248 | Slog.i(TAG, "Stopping clatd on " + mBaseIface); |
| 249 | try { |
| 250 | mNetd.clatdStop(mBaseIface); |
Lorenzo Colitti | b313e0c | 2019-02-27 10:35:10 +0900 | [diff] [blame] | 251 | } catch (RemoteException | ServiceSpecificException e) { |
| 252 | Slog.e(TAG, "Error stopping clatd on " + mBaseIface + ": " + e); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | String iface = mIface; |
| 256 | boolean wasRunning = isRunning(); |
| 257 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 258 | // 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 Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 262 | |
| 263 | if (wasRunning) { |
| 264 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 265 | lp.removeStackedLink(iface); |
| 266 | mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 267 | } |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 270 | private void startPrefixDiscovery() { |
| 271 | try { |
| 272 | mNetd.resolverStartPrefix64Discovery(getNetId()); |
| 273 | mState = State.DISCOVERING; |
| 274 | } catch (RemoteException | ServiceSpecificException e) { |
Lorenzo Colitti | b313e0c | 2019-02-27 10:35:10 +0900 | [diff] [blame] | 275 | Slog.e(TAG, "Error starting prefix discovery on netId " + getNetId() + ": " + e); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | private void stopPrefixDiscovery() { |
| 280 | try { |
| 281 | mNetd.resolverStopPrefix64Discovery(getNetId()); |
| 282 | } catch (RemoteException | ServiceSpecificException e) { |
Lorenzo Colitti | b313e0c | 2019-02-27 10:35:10 +0900 | [diff] [blame] | 283 | Slog.e(TAG, "Error stopping prefix discovery on netId " + getNetId() + ": " + e); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 284 | } |
| 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 Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 315 | public void setNat64Prefix(IpPrefix nat64Prefix) { |
| 316 | mNat64Prefix = nat64Prefix; |
| 317 | } |
| 318 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 319 | /** |
junyulai | cafa7c9 | 2018-06-05 16:10:04 +0800 | [diff] [blame] | 320 | * 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] | 321 | * 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 | */ |
junyulai | cafa7c9 | 2018-06-05 16:10:04 +0800 | [diff] [blame] | 324 | public void fixupLinkProperties(LinkProperties oldLp, LinkProperties lp) { |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 325 | lp.setNat64Prefix(mNat64Prefix); |
| 326 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 327 | if (!isRunning()) { |
| 328 | return; |
| 329 | } |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 330 | 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 Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 343 | 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 Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 361 | private LinkAddress getLinkAddress(String iface) { |
| 362 | try { |
| 363 | InterfaceConfiguration config = mNMService.getInterfaceConfig(iface); |
| 364 | return config.getLinkAddress(); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 365 | } catch (RemoteException | IllegalStateException e) { |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 366 | Slog.e(TAG, "Error getting link properties: " + e); |
| 367 | return null; |
| 368 | } |
| 369 | } |
| 370 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 371 | /** |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 372 | * Adds stacked link on base link and transitions to RUNNING state. |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 373 | */ |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 374 | private void handleInterfaceLinkStateChanged(String iface, boolean up) { |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 375 | // 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 Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 389 | if (!isStarting() || !up || !Objects.equals(mIface, iface)) { |
Hugo Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 390 | return; |
| 391 | } |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 392 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 393 | LinkAddress clatAddress = getLinkAddress(iface); |
| 394 | if (clatAddress == null) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 395 | Slog.e(TAG, "clatAddress was null for stacked iface " + iface); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 396 | return; |
| 397 | } |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 398 | |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 399 | Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s", |
| 400 | mIface, mIface, mBaseIface)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 401 | enterRunningState(); |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 402 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 403 | lp.addStackedLink(makeLinkProperties(clatAddress)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 404 | mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 405 | } |
| 406 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 407 | /** |
| 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 Benichi | 50d46a4 | 2017-08-31 14:29:51 +0000 | [diff] [blame] | 412 | return; |
| 413 | } |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 414 | if (!isRunning()) { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | Slog.i(TAG, "interface " + iface + " removed"); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 419 | // 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 Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 424 | } |
| 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 Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 434 | } |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 435 | |
| 436 | @Override |
| 437 | public String toString() { |
Hugo Benichi | 4f6f139 | 2017-06-29 14:04:13 +0900 | [diff] [blame] | 438 | return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState; |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame] | 439 | } |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 440 | |
| 441 | @VisibleForTesting |
| 442 | protected int getNetId() { |
| 443 | return mNetwork.network.netId; |
| 444 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 445 | } |