blob: fbbdf0051266f5cb57344157cfdf7367af35e9fb [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() {
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 Colitti13c9fde2013-03-15 04:22:37 +0900178 */
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900179 private void enterIdleState() {
Hugo Benichief502882017-09-01 01:23:32 +0000180 try {
181 mNMService.unregisterObserver(this);
182 } catch(RemoteException|IllegalStateException e) {
183 Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface, e);
184 }
185
Lorenzo Colitti95439462014-10-09 13:44:48 +0900186 mIface = null;
187 mBaseIface = null;
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900188 mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +0900189 }
190
191 /**
Hugo Benichief502882017-09-01 01:23:32 +0000192 * Starts the clat daemon.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900193 */
194 public void start() {
195 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900196 Slog.e(TAG, "startClat: already started");
197 return;
198 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900199
200 if (mNetwork.linkProperties == null) {
201 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
202 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900203 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900204
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900205 String baseIface = mNetwork.linkProperties.getInterfaceName();
206 if (baseIface == null) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900207 Slog.e(TAG, "startClat: Can't start clat on null interface");
208 return;
209 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900210 // TODO: should we only do this if mNMService.startClatd() succeeds?
Hugo Benichief502882017-09-01 01:23:32 +0000211 Slog.i(TAG, "Starting clatd on " + baseIface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900212 enterStartingState(baseIface);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900213 }
214
215 /**
Hugo Benichief502882017-09-01 01:23:32 +0000216 * Stops the clat daemon.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900217 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900218 public void stop() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900219 if (!isStarted()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900220 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900221 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900222 Slog.i(TAG, "Stopping clatd on " + mBaseIface);
Hugo Benichi50d46a42017-08-31 14:29:51 +0000223
Hugo Benichief502882017-09-01 01:23:32 +0000224 boolean wasStarting = isStarting();
225 enterStoppingState();
226 if (wasStarting) {
227 enterIdleState();
228 }
Paul Jensen3b759822014-05-13 11:44:01 -0400229 }
230
Lorenzo Colitti95439462014-10-09 13:44:48 +0900231 /**
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 Benichi4f6f1392017-06-29 14:04:13 +0900237 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 Colitti1df5fa52014-09-20 13:47:47 +0900250 }
251 }
252 }
253
Lorenzo Colitti95439462014-10-09 13:44:48 +0900254 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 Colittie21a26b2014-10-28 15:24:03 +0900272 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 Benichib577d652017-06-27 15:13:20 +0900283 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti853d7412016-03-03 17:17:29 +0900284 if (mNetwork.networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900285 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 Benichi4f6f1392017-06-29 14:04:13 +0900295 /**
Hugo Benichief502882017-09-01 01:23:32 +0000296 * Adds stacked link on base link and transitions to RUNNING state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900297 */
Hugo Benichief502882017-09-01 01:23:32 +0000298 private void handleInterfaceLinkStateChanged(String iface, boolean up) {
299 if (!isStarting() || !up || !Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000300 return;
301 }
Hugo Benichief502882017-09-01 01:23:32 +0000302
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900303 LinkAddress clatAddress = getLinkAddress(iface);
304 if (clatAddress == null) {
Hugo Benichief502882017-09-01 01:23:32 +0000305 Slog.e(TAG, "clatAddress was null for stacked iface " + iface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900306 return;
307 }
Hugo Benichief502882017-09-01 01:23:32 +0000308
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900309 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
310 mIface, mIface, mBaseIface));
Hugo Benichief502882017-09-01 01:23:32 +0000311 enterRunningState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900312 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
313 lp.addStackedLink(makeLinkProperties(clatAddress));
Hugo Benichief502882017-09-01 01:23:32 +0000314 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900315 }
316
Hugo Benichief502882017-09-01 01:23:32 +0000317 /**
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 Benichi50d46a42017-08-31 14:29:51 +0000322 return;
323 }
Hugo Benichief502882017-09-01 01:23:32 +0000324 if (!isRunning() && !isStopping()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900325 return;
326 }
327
328 Slog.i(TAG, "interface " + iface + " removed");
Hugo Benichief502882017-09-01 01:23:32 +0000329 if (!isStopping()) {
330 // Ensure clatd is stopped if stop() has not been called: this likely means that clatd
331 // has crashed.
332 enterStoppingState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900333 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900334 enterIdleState();
Hugo Benichief502882017-09-01 01:23:32 +0000335 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 Colitti13c9fde2013-03-15 04:22:37 +0900348 }
Hugo Benichib577d652017-06-27 15:13:20 +0900349
350 @Override
351 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900352 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900353 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900354}