blob: 9d9b1cfdf6e2d61d609589604f18ebc5499f0ded [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;
21import android.net.InterfaceConfiguration;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090022import android.net.LinkAddress;
23import android.net.LinkProperties;
Erik Kline3c182162017-09-21 17:28:10 +090024import android.net.NetworkInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090025import android.net.RouteInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090026import android.os.INetworkManagementService;
27import android.os.RemoteException;
28import android.util.Slog;
29
Hugo Benichi50d46a42017-08-31 14:29:51 +000030import com.android.internal.util.ArrayUtils;
Hugo Benichief502882017-09-01 01:23:32 +000031import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090032
Hugo Benichi4f6f1392017-06-29 14:04:13 +090033import java.net.Inet4Address;
34import java.util.Objects;
35
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090036/**
Hugo Benichief502882017-09-01 01:23:32 +000037 * Class to manage a 464xlat CLAT daemon. Nat464Xlat is not thread safe and should be manipulated
38 * from a consistent and unique thread context. It is the responsibility of ConnectivityService to
39 * call into this class from its own Handler thread.
Hugo Benichib577d652017-06-27 15:13:20 +090040 *
41 * @hide
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090042 */
43public class Nat464Xlat extends BaseNetworkObserver {
Hugo Benichib577d652017-06-27 15:13:20 +090044 private static final String TAG = Nat464Xlat.class.getSimpleName();
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090045
Lorenzo Colitti95439462014-10-09 13:44:48 +090046 // This must match the interface prefix in clatd.c.
47 private static final String CLAT_PREFIX = "v4-";
48
Erik Kline3c182162017-09-21 17:28:10 +090049 // The network types on which we will start clatd,
Hugo Benichib577d652017-06-27 15:13:20 +090050 // allowing clat only on networks for which we can support IPv6-only.
Lorenzo Colitti853d7412016-03-03 17:17:29 +090051 private static final int[] NETWORK_TYPES = {
Erik Kline3c182162017-09-21 17:28:10 +090052 ConnectivityManager.TYPE_MOBILE,
53 ConnectivityManager.TYPE_WIFI,
54 ConnectivityManager.TYPE_ETHERNET,
55 };
56
57 // The network states in which running clatd is supported.
58 private static final NetworkInfo.State[] NETWORK_STATES = {
59 NetworkInfo.State.CONNECTED,
60 NetworkInfo.State.SUSPENDED,
Lorenzo Colitti853d7412016-03-03 17:17:29 +090061 };
62
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090063 private final INetd mNetd;
Lorenzo Colitti95439462014-10-09 13:44:48 +090064 private final INetworkManagementService mNMService;
65
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090066 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090067 private final NetworkAgentInfo mNetwork;
68
Hugo Benichi4f6f1392017-06-29 14:04:13 +090069 private enum State {
70 IDLE, // start() not called. Base iface and stacked iface names are null.
71 STARTING, // start() called. Base iface and stacked iface names are known.
Hugo Benichief502882017-09-01 01:23:32 +000072 RUNNING, // start() called, and the stacked iface is known to be up.
73 STOPPING; // stop() called, this Nat464Xlat is still registered as a network observer for
74 // the stacked interface.
Hugo Benichi4f6f1392017-06-29 14:04:13 +090075 }
76
Lorenzo Colitti95439462014-10-09 13:44:48 +090077 private String mBaseIface;
78 private String mIface;
Hugo Benichief502882017-09-01 01:23:32 +000079 private State mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +090080
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090081 public Nat464Xlat(NetworkAgentInfo nai, INetd netd, INetworkManagementService nmService) {
82 mNetd = netd;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090083 mNMService = nmService;
Lorenzo Colitti95439462014-10-09 13:44:48 +090084 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090085 }
86
87 /**
Paul Jensen3b759822014-05-13 11:44:01 -040088 * Determines whether a network requires clat.
89 * @param network the NetworkAgentInfo corresponding to the network.
90 * @return true if the network requires clat, false otherwise.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090091 */
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090092 public static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +090093 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Hugo Benichib577d652017-06-27 15:13:20 +090094 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Erik Kline3c182162017-09-21 17:28:10 +090095 final boolean connected = ArrayUtils.contains(NETWORK_STATES, nai.networkInfo.getState());
Hugo Benichib577d652017-06-27 15:13:20 +090096 // We only run clat on networks that don't have a native IPv4 address.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090097 final boolean hasIPv4Address =
Hugo Benichib577d652017-06-27 15:13:20 +090098 (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address();
Yuuki Habu8f54b612018-09-06 09:37:55 +090099 final boolean skip464xlat =
soma, kawata88b8f632018-10-23 21:10:02 +0900100 (nai.netMisc() != null) && nai.netMisc().skip464xlat;
Yuuki Habu8f54b612018-09-06 09:37:55 +0900101 return supported && connected && !hasIPv4Address && !skip464xlat;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900102 }
103
Lorenzo Colitti95439462014-10-09 13:44:48 +0900104 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900105 * @return true if clatd has been started and has not yet stopped.
106 * A true result corresponds to internal states STARTING and RUNNING.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900107 */
108 public boolean isStarted() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900109 return mState != State.IDLE;
110 }
111
112 /**
Hugo Benichief502882017-09-01 01:23:32 +0000113 * @return true if clatd has been started but the stacked interface is not yet up.
114 */
115 public boolean isStarting() {
116 return mState == State.STARTING;
117 }
118
119 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900120 * @return true if clatd has been started and the stacked interface is up.
121 */
122 public boolean isRunning() {
123 return mState == State.RUNNING;
124 }
125
126 /**
Hugo Benichief502882017-09-01 01:23:32 +0000127 * @return true if clatd has been stopped.
128 */
129 public boolean isStopping() {
130 return mState == State.STOPPING;
131 }
132
133 /**
134 * Start clatd, register this Nat464Xlat as a network observer for the stacked interface,
135 * and set internal state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900136 */
137 private void enterStartingState(String baseIface) {
Hugo Benichief502882017-09-01 01:23:32 +0000138 try {
139 mNMService.registerObserver(this);
140 } catch(RemoteException e) {
141 Slog.e(TAG,
142 "startClat: Can't register interface observer for clat on " + mNetwork.name());
143 return;
144 }
145 try {
Lorenzo Colitti9307ca22019-01-12 01:54:23 +0900146 mNetd.clatdStart(baseIface);
Hugo Benichief502882017-09-01 01:23:32 +0000147 } catch(RemoteException|IllegalStateException e) {
148 Slog.e(TAG, "Error starting clatd on " + baseIface, e);
149 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900150 mIface = CLAT_PREFIX + baseIface;
151 mBaseIface = baseIface;
152 mState = State.STARTING;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900153 }
154
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900155 /**
Hugo Benichief502882017-09-01 01:23:32 +0000156 * Enter running state just after getting confirmation that the stacked interface is up, and
157 * turn ND offload off if on WiFi.
158 */
159 private void enterRunningState() {
Hugo Benichief502882017-09-01 01:23:32 +0000160 mState = State.RUNNING;
161 }
162
163 /**
164 * Stop clatd, and turn ND offload on if it had been turned off.
165 */
166 private void enterStoppingState() {
Hugo Benichief502882017-09-01 01:23:32 +0000167 try {
Lorenzo Colitti9307ca22019-01-12 01:54:23 +0900168 mNetd.clatdStop(mBaseIface);
Hugo Benichief502882017-09-01 01:23:32 +0000169 } 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 }
Lorenzo Colitti9307ca22019-01-12 01:54:23 +0900210 // TODO: should we only do this if mNetd.clatdStart() 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 /**
junyulaicafa7c92018-06-05 16:10:04 +0800232 * Copies the stacked clat link in oldLp, if any, to the passed LinkProperties.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900233 * 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 */
junyulaicafa7c92018-06-05 16:10:04 +0800236 public void fixupLinkProperties(LinkProperties oldLp, LinkProperties lp) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900237 if (!isRunning()) {
238 return;
239 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900240 if (lp == null || lp.getAllInterfaceNames().contains(mIface)) {
241 return;
242 }
243
244 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
245 for (LinkProperties stacked: oldLp.getStackedLinks()) {
246 if (Objects.equals(mIface, stacked.getInterfaceName())) {
247 lp.addStackedLink(stacked);
248 return;
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900249 }
250 }
251 }
252
Lorenzo Colitti95439462014-10-09 13:44:48 +0900253 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
254 LinkProperties stacked = new LinkProperties();
255 stacked.setInterfaceName(mIface);
256
257 // Although the clat interface is a point-to-point tunnel, we don't
258 // point the route directly at the interface because some apps don't
259 // understand routes without gateways (see, e.g., http://b/9597256
260 // http://b/9597516). Instead, set the next hop of the route to the
261 // clat IPv4 address itself (for those apps, it doesn't matter what
262 // the IP of the gateway is, only that there is one).
263 RouteInfo ipv4Default = new RouteInfo(
264 new LinkAddress(Inet4Address.ANY, 0),
265 clatAddress.getAddress(), mIface);
266 stacked.addRoute(ipv4Default);
267 stacked.addLinkAddress(clatAddress);
268 return stacked;
269 }
270
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900271 private LinkAddress getLinkAddress(String iface) {
272 try {
273 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
274 return config.getLinkAddress();
275 } catch(RemoteException|IllegalStateException e) {
276 Slog.e(TAG, "Error getting link properties: " + e);
277 return null;
278 }
279 }
280
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900281 /**
Hugo Benichief502882017-09-01 01:23:32 +0000282 * Adds stacked link on base link and transitions to RUNNING state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900283 */
Hugo Benichief502882017-09-01 01:23:32 +0000284 private void handleInterfaceLinkStateChanged(String iface, boolean up) {
285 if (!isStarting() || !up || !Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000286 return;
287 }
Hugo Benichief502882017-09-01 01:23:32 +0000288
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900289 LinkAddress clatAddress = getLinkAddress(iface);
290 if (clatAddress == null) {
Hugo Benichief502882017-09-01 01:23:32 +0000291 Slog.e(TAG, "clatAddress was null for stacked iface " + iface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900292 return;
293 }
Hugo Benichief502882017-09-01 01:23:32 +0000294
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900295 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
296 mIface, mIface, mBaseIface));
Hugo Benichief502882017-09-01 01:23:32 +0000297 enterRunningState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900298 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
299 lp.addStackedLink(makeLinkProperties(clatAddress));
Hugo Benichief502882017-09-01 01:23:32 +0000300 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900301 }
302
Hugo Benichief502882017-09-01 01:23:32 +0000303 /**
304 * Removes stacked link on base link and transitions to IDLE state.
305 */
306 private void handleInterfaceRemoved(String iface) {
307 if (!Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000308 return;
309 }
Hugo Benichief502882017-09-01 01:23:32 +0000310 if (!isRunning() && !isStopping()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900311 return;
312 }
313
314 Slog.i(TAG, "interface " + iface + " removed");
Hugo Benichief502882017-09-01 01:23:32 +0000315 if (!isStopping()) {
316 // Ensure clatd is stopped if stop() has not been called: this likely means that clatd
317 // has crashed.
318 enterStoppingState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900319 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900320 enterIdleState();
Hugo Benichief502882017-09-01 01:23:32 +0000321 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
322 lp.removeStackedLink(iface);
323 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
324 }
325
326 @Override
327 public void interfaceLinkStateChanged(String iface, boolean up) {
328 mNetwork.handler().post(() -> { handleInterfaceLinkStateChanged(iface, up); });
329 }
330
331 @Override
332 public void interfaceRemoved(String iface) {
333 mNetwork.handler().post(() -> { handleInterfaceRemoved(iface); });
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900334 }
Hugo Benichib577d652017-06-27 15:13:20 +0900335
336 @Override
337 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900338 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900339 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900340}