blob: e6585ad194ec8d5d4be7b951c4ad793cfd8696f6 [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;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090023import android.net.RouteInfo;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090024import android.os.INetworkManagementService;
25import android.os.RemoteException;
26import android.util.Slog;
27
Hugo Benichi50d46a42017-08-31 14:29:51 +000028import com.android.internal.util.ArrayUtils;
Hugo Benichief502882017-09-01 01:23:32 +000029import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090030
Hugo Benichi4f6f1392017-06-29 14:04:13 +090031import java.net.Inet4Address;
32import java.util.Objects;
33
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090034/**
Hugo Benichief502882017-09-01 01:23:32 +000035 * Class to manage a 464xlat CLAT daemon. Nat464Xlat is not thread safe and should be manipulated
36 * from a consistent and unique thread context. It is the responsibility of ConnectivityService to
37 * call into this class from its own Handler thread.
Hugo Benichib577d652017-06-27 15:13:20 +090038 *
39 * @hide
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090040 */
41public class Nat464Xlat extends BaseNetworkObserver {
Hugo Benichib577d652017-06-27 15:13:20 +090042 private static final String TAG = Nat464Xlat.class.getSimpleName();
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090043
Lorenzo Colitti95439462014-10-09 13:44:48 +090044 // This must match the interface prefix in clatd.c.
45 private static final String CLAT_PREFIX = "v4-";
46
Hugo Benichib577d652017-06-27 15:13:20 +090047 // The network types we will start clatd on,
48 // allowing clat only on networks for which we can support IPv6-only.
Lorenzo Colitti853d7412016-03-03 17:17:29 +090049 private static final int[] NETWORK_TYPES = {
50 ConnectivityManager.TYPE_MOBILE,
51 ConnectivityManager.TYPE_WIFI,
52 ConnectivityManager.TYPE_ETHERNET,
53 };
54
Lorenzo Colitti95439462014-10-09 13:44:48 +090055 private final INetworkManagementService mNMService;
56
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090057 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090058 private final NetworkAgentInfo mNetwork;
59
Hugo Benichi4f6f1392017-06-29 14:04:13 +090060 private enum State {
61 IDLE, // start() not called. Base iface and stacked iface names are null.
62 STARTING, // start() called. Base iface and stacked iface names are known.
Hugo Benichief502882017-09-01 01:23:32 +000063 RUNNING, // start() called, and the stacked iface is known to be up.
64 STOPPING; // stop() called, this Nat464Xlat is still registered as a network observer for
65 // the stacked interface.
Hugo Benichi4f6f1392017-06-29 14:04:13 +090066 }
67
Lorenzo Colitti95439462014-10-09 13:44:48 +090068 private String mBaseIface;
69 private String mIface;
Hugo Benichief502882017-09-01 01:23:32 +000070 private State mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +090071
Hugo Benichief502882017-09-01 01:23:32 +000072 public Nat464Xlat(INetworkManagementService nmService, NetworkAgentInfo nai) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090073 mNMService = nmService;
Lorenzo Colitti95439462014-10-09 13:44:48 +090074 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090075 }
76
77 /**
Paul Jensen3b759822014-05-13 11:44:01 -040078 * Determines whether a network requires clat.
79 * @param network the NetworkAgentInfo corresponding to the network.
80 * @return true if the network requires clat, false otherwise.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090081 */
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090082 public static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +090083 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090084 final int netType = nai.networkInfo.getType();
Hugo Benichib577d652017-06-27 15:13:20 +090085 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Hugo Benichief502882017-09-01 01:23:32 +000086 // TODO: this should also consider if the network is in SUSPENDED state to avoid stopping
87 // clatd in SUSPENDED state.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090088 final boolean connected = nai.networkInfo.isConnected();
Hugo Benichib577d652017-06-27 15:13:20 +090089 // We only run clat on networks that don't have a native IPv4 address.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090090 final boolean hasIPv4Address =
Hugo Benichib577d652017-06-27 15:13:20 +090091 (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address();
92 return supported && connected && !hasIPv4Address;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090093 }
94
Lorenzo Colitti95439462014-10-09 13:44:48 +090095 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +090096 * @return true if clatd has been started and has not yet stopped.
97 * A true result corresponds to internal states STARTING and RUNNING.
Lorenzo Colitti95439462014-10-09 13:44:48 +090098 */
99 public boolean isStarted() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900100 return mState != State.IDLE;
101 }
102
103 /**
Hugo Benichief502882017-09-01 01:23:32 +0000104 * @return true if clatd has been started but the stacked interface is not yet up.
105 */
106 public boolean isStarting() {
107 return mState == State.STARTING;
108 }
109
110 /**
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900111 * @return true if clatd has been started and the stacked interface is up.
112 */
113 public boolean isRunning() {
114 return mState == State.RUNNING;
115 }
116
117 /**
Hugo Benichief502882017-09-01 01:23:32 +0000118 * @return true if clatd has been stopped.
119 */
120 public boolean isStopping() {
121 return mState == State.STOPPING;
122 }
123
124 /**
125 * Start clatd, register this Nat464Xlat as a network observer for the stacked interface,
126 * and set internal state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900127 */
128 private void enterStartingState(String baseIface) {
Hugo Benichief502882017-09-01 01:23:32 +0000129 try {
130 mNMService.registerObserver(this);
131 } catch(RemoteException e) {
132 Slog.e(TAG,
133 "startClat: Can't register interface observer for clat on " + mNetwork.name());
134 return;
135 }
136 try {
137 mNMService.startClatd(baseIface);
138 } catch(RemoteException|IllegalStateException e) {
139 Slog.e(TAG, "Error starting clatd on " + baseIface, e);
140 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900141 mIface = CLAT_PREFIX + baseIface;
142 mBaseIface = baseIface;
143 mState = State.STARTING;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900144 }
145
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900146 /**
Hugo Benichief502882017-09-01 01:23:32 +0000147 * Enter running state just after getting confirmation that the stacked interface is up, and
148 * turn ND offload off if on WiFi.
149 */
150 private void enterRunningState() {
151 maybeSetIpv6NdOffload(mBaseIface, false);
152 mState = State.RUNNING;
153 }
154
155 /**
156 * Stop clatd, and turn ND offload on if it had been turned off.
157 */
158 private void enterStoppingState() {
159 if (isRunning()) {
160 maybeSetIpv6NdOffload(mBaseIface, true);
161 }
162
163 try {
164 mNMService.stopClatd(mBaseIface);
165 } catch(RemoteException|IllegalStateException e) {
166 Slog.e(TAG, "Error stopping clatd on " + mBaseIface, e);
167 }
168
169 mState = State.STOPPING;
170 }
171
172 /**
173 * Unregister as a base observer for the stacked interface, and clear internal state.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900174 */
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900175 private void enterIdleState() {
Hugo Benichief502882017-09-01 01:23:32 +0000176 try {
177 mNMService.unregisterObserver(this);
178 } catch(RemoteException|IllegalStateException e) {
179 Slog.e(TAG, "Error unregistering clatd observer on " + mBaseIface, e);
180 }
181
Lorenzo Colitti95439462014-10-09 13:44:48 +0900182 mIface = null;
183 mBaseIface = null;
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900184 mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +0900185 }
186
187 /**
Hugo Benichief502882017-09-01 01:23:32 +0000188 * Starts the clat daemon.
Lorenzo Colitti95439462014-10-09 13:44:48 +0900189 */
190 public void start() {
191 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900192 Slog.e(TAG, "startClat: already started");
193 return;
194 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900195
196 if (mNetwork.linkProperties == null) {
197 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
198 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900199 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900200
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900201 String baseIface = mNetwork.linkProperties.getInterfaceName();
202 if (baseIface == null) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900203 Slog.e(TAG, "startClat: Can't start clat on null interface");
204 return;
205 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900206 // TODO: should we only do this if mNMService.startClatd() succeeds?
Hugo Benichief502882017-09-01 01:23:32 +0000207 Slog.i(TAG, "Starting clatd on " + baseIface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900208 enterStartingState(baseIface);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900209 }
210
211 /**
Hugo Benichief502882017-09-01 01:23:32 +0000212 * Stops the clat daemon.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900213 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900214 public void stop() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900215 if (!isStarted()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900216 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900217 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900218 Slog.i(TAG, "Stopping clatd on " + mBaseIface);
Hugo Benichi50d46a42017-08-31 14:29:51 +0000219
Hugo Benichief502882017-09-01 01:23:32 +0000220 boolean wasStarting = isStarting();
221 enterStoppingState();
222 if (wasStarting) {
223 enterIdleState();
224 }
Paul Jensen3b759822014-05-13 11:44:01 -0400225 }
226
Lorenzo Colitti95439462014-10-09 13:44:48 +0900227 /**
228 * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork.
229 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
230 * has no idea that 464xlat is running on top of it.
231 */
232 public void fixupLinkProperties(LinkProperties oldLp) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900233 if (!isRunning()) {
234 return;
235 }
236 LinkProperties lp = mNetwork.linkProperties;
237 if (lp == null || lp.getAllInterfaceNames().contains(mIface)) {
238 return;
239 }
240
241 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
242 for (LinkProperties stacked: oldLp.getStackedLinks()) {
243 if (Objects.equals(mIface, stacked.getInterfaceName())) {
244 lp.addStackedLink(stacked);
245 return;
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900246 }
247 }
248 }
249
Lorenzo Colitti95439462014-10-09 13:44:48 +0900250 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
251 LinkProperties stacked = new LinkProperties();
252 stacked.setInterfaceName(mIface);
253
254 // Although the clat interface is a point-to-point tunnel, we don't
255 // point the route directly at the interface because some apps don't
256 // understand routes without gateways (see, e.g., http://b/9597256
257 // http://b/9597516). Instead, set the next hop of the route to the
258 // clat IPv4 address itself (for those apps, it doesn't matter what
259 // the IP of the gateway is, only that there is one).
260 RouteInfo ipv4Default = new RouteInfo(
261 new LinkAddress(Inet4Address.ANY, 0),
262 clatAddress.getAddress(), mIface);
263 stacked.addRoute(ipv4Default);
264 stacked.addLinkAddress(clatAddress);
265 return stacked;
266 }
267
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900268 private LinkAddress getLinkAddress(String iface) {
269 try {
270 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
271 return config.getLinkAddress();
272 } catch(RemoteException|IllegalStateException e) {
273 Slog.e(TAG, "Error getting link properties: " + e);
274 return null;
275 }
276 }
277
278 private void maybeSetIpv6NdOffload(String iface, boolean on) {
Hugo Benichib577d652017-06-27 15:13:20 +0900279 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti853d7412016-03-03 17:17:29 +0900280 if (mNetwork.networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900281 return;
282 }
283 try {
284 Slog.d(TAG, (on ? "En" : "Dis") + "abling ND offload on " + iface);
285 mNMService.setInterfaceIpv6NdOffload(iface, on);
286 } catch(RemoteException|IllegalStateException e) {
287 Slog.w(TAG, "Changing IPv6 ND offload on " + iface + "failed: " + e);
288 }
289 }
290
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900291 /**
Hugo Benichief502882017-09-01 01:23:32 +0000292 * Adds stacked link on base link and transitions to RUNNING state.
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900293 */
Hugo Benichief502882017-09-01 01:23:32 +0000294 private void handleInterfaceLinkStateChanged(String iface, boolean up) {
295 if (!isStarting() || !up || !Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000296 return;
297 }
Hugo Benichief502882017-09-01 01:23:32 +0000298
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900299 LinkAddress clatAddress = getLinkAddress(iface);
300 if (clatAddress == null) {
Hugo Benichief502882017-09-01 01:23:32 +0000301 Slog.e(TAG, "clatAddress was null for stacked iface " + iface);
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900302 return;
303 }
Hugo Benichief502882017-09-01 01:23:32 +0000304
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900305 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
306 mIface, mIface, mBaseIface));
Hugo Benichief502882017-09-01 01:23:32 +0000307 enterRunningState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900308 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
309 lp.addStackedLink(makeLinkProperties(clatAddress));
Hugo Benichief502882017-09-01 01:23:32 +0000310 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900311 }
312
Hugo Benichief502882017-09-01 01:23:32 +0000313 /**
314 * Removes stacked link on base link and transitions to IDLE state.
315 */
316 private void handleInterfaceRemoved(String iface) {
317 if (!Objects.equals(mIface, iface)) {
Hugo Benichi50d46a42017-08-31 14:29:51 +0000318 return;
319 }
Hugo Benichief502882017-09-01 01:23:32 +0000320 if (!isRunning() && !isStopping()) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900321 return;
322 }
323
324 Slog.i(TAG, "interface " + iface + " removed");
Hugo Benichief502882017-09-01 01:23:32 +0000325 if (!isStopping()) {
326 // Ensure clatd is stopped if stop() has not been called: this likely means that clatd
327 // has crashed.
328 enterStoppingState();
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900329 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900330 enterIdleState();
Hugo Benichief502882017-09-01 01:23:32 +0000331 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
332 lp.removeStackedLink(iface);
333 mNetwork.connService().handleUpdateLinkProperties(mNetwork, lp);
334 }
335
336 @Override
337 public void interfaceLinkStateChanged(String iface, boolean up) {
338 mNetwork.handler().post(() -> { handleInterfaceLinkStateChanged(iface, up); });
339 }
340
341 @Override
342 public void interfaceRemoved(String iface) {
343 mNetwork.handler().post(() -> { handleInterfaceRemoved(iface); });
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900344 }
Hugo Benichib577d652017-06-27 15:13:20 +0900345
346 @Override
347 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900348 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900349 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900350}