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 java.net.Inet4Address; |
| 20 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 21 | import android.net.InterfaceConfiguration; |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 22 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 23 | import android.net.LinkAddress; |
| 24 | import android.net.LinkProperties; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 25 | import android.net.NetworkAgent; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 26 | import android.net.RouteInfo; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Message; |
| 29 | import android.os.INetworkManagementService; |
| 30 | import android.os.RemoteException; |
| 31 | import android.util.Slog; |
| 32 | |
| 33 | import com.android.server.net.BaseNetworkObserver; |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 34 | import com.android.internal.util.ArrayUtils; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 37 | * Class to manage a 464xlat CLAT daemon. |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 38 | * |
| 39 | * @hide |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 40 | */ |
| 41 | public class Nat464Xlat extends BaseNetworkObserver { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 42 | private static final String TAG = Nat464Xlat.class.getSimpleName(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 43 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 44 | // This must match the interface prefix in clatd.c. |
| 45 | private static final String CLAT_PREFIX = "v4-"; |
| 46 | |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 47 | // The network types we will start clatd on, |
| 48 | // allowing clat only on networks for which we can support IPv6-only. |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 49 | private static final int[] NETWORK_TYPES = { |
| 50 | ConnectivityManager.TYPE_MOBILE, |
| 51 | ConnectivityManager.TYPE_WIFI, |
| 52 | ConnectivityManager.TYPE_ETHERNET, |
| 53 | }; |
| 54 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 55 | private final INetworkManagementService mNMService; |
| 56 | |
| 57 | // ConnectivityService Handler for LinkProperties updates. |
| 58 | private final Handler mHandler; |
| 59 | |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 60 | // The network we're running on, and its type. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 61 | private final NetworkAgentInfo mNetwork; |
| 62 | |
| 63 | // Internal state variables. |
| 64 | // |
| 65 | // The possible states are: |
| 66 | // - Idle: start() not called. Everything is null. |
| 67 | // - Starting: start() called. Interfaces are non-null. isStarted() returns true. |
| 68 | // mIsRunning is false. |
Lorenzo Colitti | 7f6c0d7 | 2014-11-10 19:17:35 -0800 | [diff] [blame] | 69 | // - Running: start() called, and interfaceLinkStateChanged() told us that mIface is up. |
| 70 | // mIsRunning is true. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 71 | // |
| 72 | // Once mIface is non-null and isStarted() is true, methods called by ConnectivityService on |
| 73 | // its handler thread must not modify any internal state variables; they are only updated by the |
| 74 | // interface observers, called on the notification threads. |
| 75 | private String mBaseIface; |
| 76 | private String mIface; |
| 77 | private boolean mIsRunning; |
| 78 | |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 79 | public Nat464Xlat(INetworkManagementService nmService, Handler handler, NetworkAgentInfo nai) { |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 80 | mNMService = nmService; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 81 | mHandler = handler; |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 82 | mNetwork = nai; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 86 | * Determines whether a network requires clat. |
| 87 | * @param network the NetworkAgentInfo corresponding to the network. |
| 88 | * @return true if the network requires clat, false otherwise. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 89 | */ |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 90 | public static boolean requiresClat(NetworkAgentInfo nai) { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 91 | // TODO: migrate to NetworkCapabilities.TRANSPORT_*. |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 92 | final int netType = nai.networkInfo.getType(); |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 93 | final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType()); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 94 | final boolean connected = nai.networkInfo.isConnected(); |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 95 | // 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] | 96 | final boolean hasIPv4Address = |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 97 | (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address(); |
| 98 | return supported && connected && !hasIPv4Address; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 101 | /** |
| 102 | * Determines whether clatd is started. Always true, except a) if start has not yet been called, |
| 103 | * or b) if our interface was removed. |
| 104 | */ |
| 105 | public boolean isStarted() { |
| 106 | return mIface != null; |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 109 | /** |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 110 | * Clears internal state. Must not be called by ConnectivityService. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 111 | */ |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 112 | private void clear() { |
| 113 | mIface = null; |
| 114 | mBaseIface = null; |
| 115 | mIsRunning = false; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Starts the clat daemon. Called by ConnectivityService on the handler thread. |
| 120 | */ |
| 121 | public void start() { |
| 122 | if (isStarted()) { |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 123 | Slog.e(TAG, "startClat: already started"); |
| 124 | return; |
| 125 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 126 | |
| 127 | if (mNetwork.linkProperties == null) { |
| 128 | Slog.e(TAG, "startClat: Can't start clat with null LinkProperties"); |
| 129 | return; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 130 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 131 | |
| 132 | try { |
| 133 | mNMService.registerObserver(this); |
| 134 | } catch(RemoteException e) { |
| 135 | Slog.e(TAG, "startClat: Can't register interface observer for clat on " + mNetwork); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | mBaseIface = mNetwork.linkProperties.getInterfaceName(); |
| 140 | if (mBaseIface == null) { |
| 141 | Slog.e(TAG, "startClat: Can't start clat on null interface"); |
| 142 | return; |
| 143 | } |
| 144 | mIface = CLAT_PREFIX + mBaseIface; |
| 145 | // From now on, isStarted() will return true. |
| 146 | |
| 147 | Slog.i(TAG, "Starting clatd on " + mBaseIface); |
| 148 | try { |
| 149 | mNMService.startClatd(mBaseIface); |
| 150 | } catch(RemoteException|IllegalStateException e) { |
| 151 | Slog.e(TAG, "Error starting clatd: " + e); |
| 152 | } |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 156 | * Stops the clat daemon. Called by ConnectivityService on the handler thread. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 157 | */ |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 158 | public void stop() { |
| 159 | if (isStarted()) { |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 160 | Slog.i(TAG, "Stopping clatd"); |
| 161 | try { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 162 | mNMService.stopClatd(mBaseIface); |
| 163 | } catch(RemoteException|IllegalStateException e) { |
| 164 | Slog.e(TAG, "Error stopping clatd: " + e); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 165 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 166 | // When clatd stops and its interface is deleted, interfaceRemoved() will notify |
| 167 | // ConnectivityService and call clear(). |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 168 | } else { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 169 | Slog.e(TAG, "clatd: already stopped"); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 173 | private void updateConnectivityService(LinkProperties lp) { |
| 174 | Message msg = mHandler.obtainMessage(NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, lp); |
| 175 | msg.replyTo = mNetwork.messenger; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 176 | Slog.i(TAG, "sending message to ConnectivityService: " + msg); |
| 177 | msg.sendToTarget(); |
| 178 | } |
| 179 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 180 | /** |
| 181 | * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork. |
| 182 | * This is necessary because the LinkProperties in mNetwork come from the transport layer, which |
| 183 | * has no idea that 464xlat is running on top of it. |
| 184 | */ |
| 185 | public void fixupLinkProperties(LinkProperties oldLp) { |
| 186 | if (mNetwork.clatd != null && |
| 187 | mIsRunning && |
| 188 | mNetwork.linkProperties != null && |
| 189 | !mNetwork.linkProperties.getAllInterfaceNames().contains(mIface)) { |
| 190 | Slog.d(TAG, "clatd running, updating NAI for " + mIface); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 191 | for (LinkProperties stacked: oldLp.getStackedLinks()) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 192 | if (mIface.equals(stacked.getInterfaceName())) { |
| 193 | mNetwork.linkProperties.addStackedLink(stacked); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 200 | private LinkProperties makeLinkProperties(LinkAddress clatAddress) { |
| 201 | LinkProperties stacked = new LinkProperties(); |
| 202 | stacked.setInterfaceName(mIface); |
| 203 | |
| 204 | // Although the clat interface is a point-to-point tunnel, we don't |
| 205 | // point the route directly at the interface because some apps don't |
| 206 | // understand routes without gateways (see, e.g., http://b/9597256 |
| 207 | // http://b/9597516). Instead, set the next hop of the route to the |
| 208 | // clat IPv4 address itself (for those apps, it doesn't matter what |
| 209 | // the IP of the gateway is, only that there is one). |
| 210 | RouteInfo ipv4Default = new RouteInfo( |
| 211 | new LinkAddress(Inet4Address.ANY, 0), |
| 212 | clatAddress.getAddress(), mIface); |
| 213 | stacked.addRoute(ipv4Default); |
| 214 | stacked.addLinkAddress(clatAddress); |
| 215 | return stacked; |
| 216 | } |
| 217 | |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 218 | private LinkAddress getLinkAddress(String iface) { |
| 219 | try { |
| 220 | InterfaceConfiguration config = mNMService.getInterfaceConfig(iface); |
| 221 | return config.getLinkAddress(); |
| 222 | } catch(RemoteException|IllegalStateException e) { |
| 223 | Slog.e(TAG, "Error getting link properties: " + e); |
| 224 | return null; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | private void maybeSetIpv6NdOffload(String iface, boolean on) { |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 229 | // TODO: migrate to NetworkCapabilities.TRANSPORT_*. |
Lorenzo Colitti | 853d741 | 2016-03-03 17:17:29 +0900 | [diff] [blame] | 230 | if (mNetwork.networkInfo.getType() != ConnectivityManager.TYPE_WIFI) { |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | try { |
| 234 | Slog.d(TAG, (on ? "En" : "Dis") + "abling ND offload on " + iface); |
| 235 | mNMService.setInterfaceIpv6NdOffload(iface, on); |
| 236 | } catch(RemoteException|IllegalStateException e) { |
| 237 | Slog.w(TAG, "Changing IPv6 ND offload on " + iface + "failed: " + e); |
| 238 | } |
| 239 | } |
| 240 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 241 | @Override |
Lorenzo Colitti | 7f6c0d7 | 2014-11-10 19:17:35 -0800 | [diff] [blame] | 242 | public void interfaceLinkStateChanged(String iface, boolean up) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 243 | // Called by the InterfaceObserver on its own thread, so can race with stop(). |
Lorenzo Colitti | 7f6c0d7 | 2014-11-10 19:17:35 -0800 | [diff] [blame] | 244 | if (isStarted() && up && mIface.equals(iface)) { |
| 245 | Slog.i(TAG, "interface " + iface + " is up, mIsRunning " + mIsRunning + "->true"); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 246 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 247 | if (!mIsRunning) { |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 248 | LinkAddress clatAddress = getLinkAddress(iface); |
| 249 | if (clatAddress == null) { |
| 250 | return; |
| 251 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 252 | mIsRunning = true; |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 253 | maybeSetIpv6NdOffload(mBaseIface, false); |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 254 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 255 | lp.addStackedLink(makeLinkProperties(clatAddress)); |
| 256 | Slog.i(TAG, "Adding stacked link " + mIface + " on top of " + mBaseIface); |
| 257 | updateConnectivityService(lp); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 258 | } |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void interfaceRemoved(String iface) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 264 | if (isStarted() && mIface.equals(iface)) { |
| 265 | Slog.i(TAG, "interface " + iface + " removed, mIsRunning " + mIsRunning + "->false"); |
| 266 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 267 | if (mIsRunning) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 268 | // The interface going away likely means clatd has crashed. Ask netd to stop it, |
| 269 | // because otherwise when we try to start it again on the same base interface netd |
| 270 | // will complain that it's already started. |
| 271 | // |
| 272 | // Note that this method can be called by the interface observer at the same time |
| 273 | // that ConnectivityService calls stop(). In this case, the second call to |
| 274 | // stopClatd() will just throw IllegalStateException, which we'll ignore. |
| 275 | try { |
| 276 | mNMService.unregisterObserver(this); |
| 277 | mNMService.stopClatd(mBaseIface); |
| 278 | } catch (RemoteException|IllegalStateException e) { |
| 279 | // Well, we tried. |
| 280 | } |
Lorenzo Colitti | e21a26b | 2014-10-28 15:24:03 +0900 | [diff] [blame] | 281 | maybeSetIpv6NdOffload(mBaseIface, true); |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 282 | LinkProperties lp = new LinkProperties(mNetwork.linkProperties); |
| 283 | lp.removeStackedLink(mIface); |
| 284 | clear(); |
| 285 | updateConnectivityService(lp); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 286 | } |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 287 | } |
| 288 | } |
Hugo Benichi | b577d65 | 2017-06-27 15:13:20 +0900 | [diff] [blame^] | 289 | |
| 290 | @Override |
| 291 | public String toString() { |
| 292 | return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mIsRunning: " + mIsRunning; |
| 293 | } |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 294 | } |