blob: fceacba4239c9aaa01a17174980052653edd4597 [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 Colitti13c9fde2013-03-15 04:22:37 +090019import android.net.InterfaceConfiguration;
Lorenzo Colitti853d7412016-03-03 17:17:29 +090020import android.net.ConnectivityManager;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090021import android.net.LinkAddress;
22import android.net.LinkProperties;
Erik Kline3c182162017-09-21 17:28:10 +090023import android.net.NetworkInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090024import android.net.RouteInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090025import android.os.INetworkManagementService;
26import android.os.RemoteException;
27import android.util.Slog;
28
Hugo Benichi50d46a42017-08-31 14:29:51 +000029import com.android.internal.util.ArrayUtils;
Hugo Benichief502882017-09-01 01:23:32 +000030import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090031
Hugo Benichi4f6f1392017-06-29 14:04:13 +090032import java.net.Inet4Address;
33import java.util.Objects;
34
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090035/**
Hugo Benichief502882017-09-01 01:23:32 +000036 * 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 Benichib577d652017-06-27 15:13:20 +090039 *
40 * @hide
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090041 */
42public class Nat464Xlat extends BaseNetworkObserver {
Hugo Benichib577d652017-06-27 15:13:20 +090043 private static final String TAG = Nat464Xlat.class.getSimpleName();
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090044
Lorenzo Colitti95439462014-10-09 13:44:48 +090045 // This must match the interface prefix in clatd.c.
46 private static final String CLAT_PREFIX = "v4-";
47
Erik Kline3c182162017-09-21 17:28:10 +090048 // The network types on which we will start clatd,
Hugo Benichib577d652017-06-27 15:13:20 +090049 // allowing clat only on networks for which we can support IPv6-only.
Lorenzo Colitti853d7412016-03-03 17:17:29 +090050 private static final int[] NETWORK_TYPES = {
Erik Kline3c182162017-09-21 17:28:10 +090051 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 Colitti853d7412016-03-03 17:17:29 +090060 };
61
Lorenzo Colitti95439462014-10-09 13:44:48 +090062 private final INetworkManagementService mNMService;
63
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090064 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090065 private final NetworkAgentInfo mNetwork;
66
Hugo Benichi4f6f1392017-06-29 14:04:13 +090067 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 Benichief502882017-09-01 01:23:32 +000070 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 Benichi4f6f1392017-06-29 14:04:13 +090073 }
74
Lorenzo Colitti95439462014-10-09 13:44:48 +090075 private String mBaseIface;
76 private String mIface;
Hugo Benichief502882017-09-01 01:23:32 +000077 private State mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +090078
Hugo Benichief502882017-09-01 01:23:32 +000079 public Nat464Xlat(INetworkManagementService nmService, NetworkAgentInfo nai) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090080 mNMService = nmService;
Lorenzo Colitti95439462014-10-09 13:44:48 +090081 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090082 }
83
84 /**
Paul Jensen3b759822014-05-13 11:44:01 -040085 * 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 Colitti13c9fde2013-03-15 04:22:37 +090088 */
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090089 public static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +090090 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Hugo Benichib577d652017-06-27 15:13:20 +090091 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Erik Kline3c182162017-09-21 17:28:10 +090092 final boolean connected = ArrayUtils.contains(NETWORK_STATES, nai.networkInfo.getState());
Hugo Benichib577d652017-06-27 15:13:20 +090093 // We only run clat on networks that don't have a native IPv4 address.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090094 final boolean hasIPv4Address =
Hugo Benichib577d652017-06-27 15:13:20 +090095 (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address();
96 return supported && connected && !hasIPv4Address;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090097 }
98
Lorenzo Colitti95439462014-10-09 13:44:48 +090099 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900100 * @return true if clatd has been started and has not yet stopped.
101 * A true result corresponds to internal states STARTING and RUNNING.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900102 */
103 public boolean isStarted() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900104 return mState != State.IDLE;
105 }
106
107 /**
Hugo Benichief502882017-09-01 01:23:32 +0000108 * @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 Benichi4f6f1392017-06-29 14:04:13 +0900115 * @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 Benichief502882017-09-01 01:23:32 +0000122 * @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 Benichi4f6f1392017-06-29 14:04:13 +0900131 */
132 private void enterStartingState(String baseIface) {
Hugo Benichief502882017-09-01 01:23:32 +0000133 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 Benichi4f6f1392017-06-29 14:04:13 +0900145 mIface = CLAT_PREFIX + baseIface;
146 mBaseIface = baseIface;
147 mState = State.STARTING;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900148 }
149
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900150 /**
Hugo Benichief502882017-09-01 01:23:32 +0000151 * 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() {
Hugo Benichief502882017-09-01 01:23:32 +0000155 mState = State.RUNNING;
156 }
157
158 /**
159 * Stop clatd, and turn ND offload on if it had been turned off.
160 */
161 private void enterStoppingState() {
Hugo Benichief502882017-09-01 01:23:32 +0000162 try {
163 mNMService.stopClatd(mBaseIface);
164 } catch(RemoteException|IllegalStateException e) {
165 Slog.e(TAG, "Error stopping clatd on " + mBaseIface, e);
166 }
167
168 mState = State.STOPPING;
169 }
170
171 /**
172 * Unregister as a base observer for the stacked interface, and clear internal state.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900173 */
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900174 private void enterIdleState() {
Hugo Benichief502882017-09-01 01:23:32 +0000175 try {
176 mNMService.unregisterObserver(this);
177 } catch(RemoteException|IllegalStateException e) {
178 Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface, e);
179 }
180
Lorenzo Colitti95439462014-10-09 13:44:48 +0900181 mIface = null;
182 mBaseIface = null;
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900183 mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +0900184 }
185
186 /**
Hugo Benichief502882017-09-01 01:23:32 +0000187 * Starts the clat daemon.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900188 */
189 public void start() {
190 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900191 Slog.e(TAG, "startClat: already started");
192 return;
193 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900194
195 if (mNetwork.linkProperties == null) {
196 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
197 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900198 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900199
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900200 String baseIface = mNetwork.linkProperties.getInterfaceName();
201 if (baseIface == null) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900202 Slog.e(TAG, "startClat: Can't start clat on null interface");
203 return;
204 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900205 // TODO: should we only do this if mNMService.startClatd() succeeds?
Hugo Benichief502882017-09-01 01:23:32 +0000206 Slog.i(TAG, "Starting clatd on " + baseIface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900207 enterStartingState(baseIface);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900208 }
209
210 /**
Hugo Benichief502882017-09-01 01:23:32 +0000211 * Stops the clat daemon.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900212 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900213 public void stop() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900214 if (!isStarted()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900215 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900216 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900217 Slog.i(TAG, "Stopping clatd on " + mBaseIface);
Hugo Benichi50d46a42017-08-31 14:29:51 +0000218
Hugo Benichief502882017-09-01 01:23:32 +0000219 boolean wasStarting = isStarting();
220 enterStoppingState();
221 if (wasStarting) {
222 enterIdleState();
223 }
Paul Jensen3b759822014-05-13 11:44:01 -0400224 }
225
Lorenzo Colitti95439462014-10-09 13:44:48 +0900226 /**
227 * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork.
228 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
229 * has no idea that 464xlat is running on top of it.
230 */
231 public void fixupLinkProperties(LinkProperties oldLp) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900232 if (!isRunning()) {
233 return;
234 }
235 LinkProperties lp = mNetwork.linkProperties;
236 if (lp == null || lp.getAllInterfaceNames().contains(mIface)) {
237 return;
238 }
239
240 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
241 for (LinkProperties stacked: oldLp.getStackedLinks()) {
242 if (Objects.equals(mIface, stacked.getInterfaceName())) {
243 lp.addStackedLink(stacked);
244 return;
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900245 }
246 }
247 }
248
Lorenzo Colitti95439462014-10-09 13:44:48 +0900249 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
250 LinkProperties stacked = new LinkProperties();
251 stacked.setInterfaceName(mIface);
252
253 // Although the clat interface is a point-to-point tunnel, we don't
254 // point the route directly at the interface because some apps don't
255 // understand routes without gateways (see, e.g., http://b/9597256
256 // http://b/9597516). Instead, set the next hop of the route to the
257 // clat IPv4 address itself (for those apps, it doesn't matter what
258 // the IP of the gateway is, only that there is one).
259 RouteInfo ipv4Default = new RouteInfo(
260 new LinkAddress(Inet4Address.ANY, 0),
261 clatAddress.getAddress(), mIface);
262 stacked.addRoute(ipv4Default);
263 stacked.addLinkAddress(clatAddress);
264 return stacked;
265 }
266
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900267 private LinkAddress getLinkAddress(String iface) {
268 try {
269 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
270 return config.getLinkAddress();
271 } catch(RemoteException|IllegalStateException e) {
272 Slog.e(TAG, "Error getting link properties: " + e);
273 return null;
274 }
275 }
276
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900277 /**
Hugo Benichief502882017-09-01 01:23:32 +0000278 * Adds stacked link on base link and transitions to RUNNING state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900279 */
Hugo Benichief502882017-09-01 01:23:32 +0000280 private void handleInterfaceLinkStateChanged(String iface, boolean up) {
281 if (!isStarting() || !up || !Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000282 return;
283 }
Hugo Benichief502882017-09-01 01:23:32 +0000284
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900285 LinkAddress clatAddress = getLinkAddress(iface);
286 if (clatAddress == null) {
Hugo Benichief502882017-09-01 01:23:32 +0000287 Slog.e(TAG, "clatAddress was null for stacked iface " + iface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900288 return;
289 }
Hugo Benichief502882017-09-01 01:23:32 +0000290
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900291 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
292 mIface, mIface, mBaseIface));
Hugo Benichief502882017-09-01 01:23:32 +0000293 enterRunningState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900294 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
295 lp.addStackedLink(makeLinkProperties(clatAddress));
Hugo Benichief502882017-09-01 01:23:32 +0000296 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900297 }
298
Hugo Benichief502882017-09-01 01:23:32 +0000299 /**
300 * Removes stacked link on base link and transitions to IDLE state.
301 */
302 private void handleInterfaceRemoved(String iface) {
303 if (!Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000304 return;
305 }
Hugo Benichief502882017-09-01 01:23:32 +0000306 if (!isRunning() && !isStopping()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900307 return;
308 }
309
310 Slog.i(TAG, "interface " + iface + " removed");
Hugo Benichief502882017-09-01 01:23:32 +0000311 if (!isStopping()) {
312 // Ensure clatd is stopped if stop() has not been called: this likely means that clatd
313 // has crashed.
314 enterStoppingState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900315 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900316 enterIdleState();
Hugo Benichief502882017-09-01 01:23:32 +0000317 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
318 lp.removeStackedLink(iface);
319 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
320 }
321
322 @Override
323 public void interfaceLinkStateChanged(String iface, boolean up) {
324 mNetwork.handler().post(() -> { handleInterfaceLinkStateChanged(iface, up); });
325 }
326
327 @Override
328 public void interfaceRemoved(String iface) {
329 mNetwork.handler().post(() -> { handleInterfaceRemoved(iface); });
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900330 }
Hugo Benichib577d652017-06-27 15:13:20 +0900331
332 @Override
333 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900334 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900335 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900336}