blob: f8d23d4c8d51a360fe26ccd6fb162486464e13e2 [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;
Paul Jensen3b759822014-05-13 11:44:01 -040023import android.net.NetworkAgent;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090024import android.net.RouteInfo;
25import android.os.Handler;
26import android.os.Message;
27import android.os.INetworkManagementService;
28import android.os.RemoteException;
29import android.util.Slog;
30
31import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti853d7412016-03-03 17:17:29 +090032import com.android.internal.util.ArrayUtils;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090033
Hugo Benichi4f6f1392017-06-29 14:04:13 +090034import java.net.Inet4Address;
35import java.util.Objects;
36
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090037/**
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090038 * Class to manage a 464xlat CLAT daemon.
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
Hugo Benichib577d652017-06-27 15:13:20 +090048 // The network types we will start clatd on,
49 // 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 = {
51 ConnectivityManager.TYPE_MOBILE,
52 ConnectivityManager.TYPE_WIFI,
53 ConnectivityManager.TYPE_ETHERNET,
54 };
55
Lorenzo Colitti95439462014-10-09 13:44:48 +090056 private final INetworkManagementService mNMService;
57
58 // ConnectivityService Handler for LinkProperties updates.
59 private final Handler mHandler;
60
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090061 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090062 private final NetworkAgentInfo mNetwork;
63
Hugo Benichi4f6f1392017-06-29 14:04:13 +090064 private enum State {
65 IDLE, // start() not called. Base iface and stacked iface names are null.
66 STARTING, // start() called. Base iface and stacked iface names are known.
67 RUNNING; // start() called, and the stacked iface is known to be up.
68 }
69
Lorenzo Colitti95439462014-10-09 13:44:48 +090070 // Once mIface is non-null and isStarted() is true, methods called by ConnectivityService on
71 // its handler thread must not modify any internal state variables; they are only updated by the
72 // interface observers, called on the notification threads.
73 private String mBaseIface;
74 private String mIface;
Hugo Benichi4f6f1392017-06-29 14:04:13 +090075 private volatile State mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +090076
Hugo Benichib577d652017-06-27 15:13:20 +090077 public Nat464Xlat(INetworkManagementService nmService, Handler handler, NetworkAgentInfo nai) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090078 mNMService = nmService;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090079 mHandler = handler;
Lorenzo Colitti95439462014-10-09 13:44:48 +090080 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090081 }
82
83 /**
Paul Jensen3b759822014-05-13 11:44:01 -040084 * Determines whether a network requires clat.
85 * @param network the NetworkAgentInfo corresponding to the network.
86 * @return true if the network requires clat, false otherwise.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090087 */
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090088 public static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +090089 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090090 final int netType = nai.networkInfo.getType();
Hugo Benichib577d652017-06-27 15:13:20 +090091 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090092 final boolean connected = nai.networkInfo.isConnected();
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 /**
108 * @return true if clatd has been started and the stacked interface is up.
109 */
110 public boolean isRunning() {
111 return mState == State.RUNNING;
112 }
113
114 /**
115 * Sets internal state.
116 */
117 private void enterStartingState(String baseIface) {
118 mIface = CLAT_PREFIX + baseIface;
119 mBaseIface = baseIface;
120 mState = State.STARTING;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900121 }
122
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900123 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900124 * Clears internal state. Must not be called by ConnectivityService.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900125 */
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900126 private void enterIdleState() {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900127 mIface = null;
128 mBaseIface = null;
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900129 mState = State.IDLE;
Lorenzo Colitti95439462014-10-09 13:44:48 +0900130 }
131
132 /**
133 * Starts the clat daemon. Called by ConnectivityService on the handler thread.
134 */
135 public void start() {
136 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900137 Slog.e(TAG, "startClat: already started");
138 return;
139 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900140
141 if (mNetwork.linkProperties == null) {
142 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
143 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900144 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900145
146 try {
147 mNMService.registerObserver(this);
148 } catch(RemoteException e) {
149 Slog.e(TAG, "startClat: Can't register interface observer for clat on " + mNetwork);
150 return;
151 }
152
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900153 String baseIface = mNetwork.linkProperties.getInterfaceName();
154 if (baseIface == null) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900155 Slog.e(TAG, "startClat: Can't start clat on null interface");
156 return;
157 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900158 // TODO: should we only do this if mNMService.startClatd() succeeds?
159 enterStartingState(baseIface);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900160
161 Slog.i(TAG, "Starting clatd on " + mBaseIface);
162 try {
163 mNMService.startClatd(mBaseIface);
164 } catch(RemoteException|IllegalStateException e) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900165 Slog.e(TAG, "Error starting clatd on " + mBaseIface, e);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900166 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900167 }
168
169 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900170 * Stops the clat daemon. Called by ConnectivityService on the handler thread.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900171 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900172 public void stop() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900173 if (!isStarted()) {
174 Slog.e(TAG, "stopClat: already stopped or not started");
175 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900176 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900177
178 Slog.i(TAG, "Stopping clatd on " + mBaseIface);
179 try {
180 mNMService.stopClatd(mBaseIface);
181 } catch(RemoteException|IllegalStateException e) {
182 Slog.e(TAG, "Error stopping clatd on " + mBaseIface, e);
183 }
184 // When clatd stops and its interface is deleted, interfaceRemoved() will notify
185 // ConnectivityService and call enterIdleState().
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900186 }
187
Lorenzo Colitti95439462014-10-09 13:44:48 +0900188 private void updateConnectivityService(LinkProperties lp) {
189 Message msg = mHandler.obtainMessage(NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, lp);
190 msg.replyTo = mNetwork.messenger;
Paul Jensen3b759822014-05-13 11:44:01 -0400191 Slog.i(TAG, "sending message to ConnectivityService: " + msg);
192 msg.sendToTarget();
193 }
194
Lorenzo Colitti95439462014-10-09 13:44:48 +0900195 /**
196 * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork.
197 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
198 * has no idea that 464xlat is running on top of it.
199 */
200 public void fixupLinkProperties(LinkProperties oldLp) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900201 if (!isRunning()) {
202 return;
203 }
204 LinkProperties lp = mNetwork.linkProperties;
205 if (lp == null || lp.getAllInterfaceNames().contains(mIface)) {
206 return;
207 }
208
209 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
210 for (LinkProperties stacked: oldLp.getStackedLinks()) {
211 if (Objects.equals(mIface, stacked.getInterfaceName())) {
212 lp.addStackedLink(stacked);
213 return;
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900214 }
215 }
216 }
217
Lorenzo Colitti95439462014-10-09 13:44:48 +0900218 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
219 LinkProperties stacked = new LinkProperties();
220 stacked.setInterfaceName(mIface);
221
222 // Although the clat interface is a point-to-point tunnel, we don't
223 // point the route directly at the interface because some apps don't
224 // understand routes without gateways (see, e.g., http://b/9597256
225 // http://b/9597516). Instead, set the next hop of the route to the
226 // clat IPv4 address itself (for those apps, it doesn't matter what
227 // the IP of the gateway is, only that there is one).
228 RouteInfo ipv4Default = new RouteInfo(
229 new LinkAddress(Inet4Address.ANY, 0),
230 clatAddress.getAddress(), mIface);
231 stacked.addRoute(ipv4Default);
232 stacked.addLinkAddress(clatAddress);
233 return stacked;
234 }
235
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900236 private LinkAddress getLinkAddress(String iface) {
237 try {
238 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
239 return config.getLinkAddress();
240 } catch(RemoteException|IllegalStateException e) {
241 Slog.e(TAG, "Error getting link properties: " + e);
242 return null;
243 }
244 }
245
246 private void maybeSetIpv6NdOffload(String iface, boolean on) {
Hugo Benichib577d652017-06-27 15:13:20 +0900247 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti853d7412016-03-03 17:17:29 +0900248 if (mNetwork.networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900249 return;
250 }
251 try {
252 Slog.d(TAG, (on ? "En" : "Dis") + "abling ND offload on " + iface);
253 mNMService.setInterfaceIpv6NdOffload(iface, on);
254 } catch(RemoteException|IllegalStateException e) {
255 Slog.w(TAG, "Changing IPv6 ND offload on " + iface + "failed: " + e);
256 }
257 }
258
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900259 /**
260 * Adds stacked link on base link and transitions to Running state
261 * This is called by the InterfaceObserver on its own thread, so can race with stop().
262 */
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900263 @Override
Lorenzo Colitti7f6c0d72014-11-10 19:17:35 -0800264 public void interfaceLinkStateChanged(String iface, boolean up) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900265 if (!isStarted() || !up || !Objects.equals(mIface, iface)) {
266 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900267 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900268 if (isRunning()) {
269 return;
270 }
271 LinkAddress clatAddress = getLinkAddress(iface);
272 if (clatAddress == null) {
273 return;
274 }
275 mState = State.RUNNING;
276 Slog.i(TAG, String.format("interface %s is up, adding stacked link %s on top of %s",
277 mIface, mIface, mBaseIface));
278
279 maybeSetIpv6NdOffload(mBaseIface, false);
280 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
281 lp.addStackedLink(makeLinkProperties(clatAddress));
282 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900283 }
284
285 @Override
286 public void interfaceRemoved(String iface) {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900287 if (!isStarted() || !Objects.equals(mIface, iface)) {
288 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900289 }
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900290 if (!isRunning()) {
291 return;
292 }
293
294 Slog.i(TAG, "interface " + iface + " removed");
295 // The interface going away likely means clatd has crashed. Ask netd to stop it,
296 // because otherwise when we try to start it again on the same base interface netd
297 // will complain that it's already started.
298 //
299 // Note that this method can be called by the interface observer at the same time
300 // that ConnectivityService calls stop(). In this case, the second call to
301 // stopClatd() will just throw IllegalStateException, which we'll ignore.
302 try {
303 mNMService.unregisterObserver(this);
304 mNMService.stopClatd(mBaseIface);
305 } catch (RemoteException|IllegalStateException e) {
306 // Well, we tried.
307 }
308 maybeSetIpv6NdOffload(mBaseIface, true);
309 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
310 lp.removeStackedLink(mIface);
311 enterIdleState();
312 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900313 }
Hugo Benichib577d652017-06-27 15:13:20 +0900314
315 @Override
316 public String toString() {
Hugo Benichi4f6f1392017-06-29 14:04:13 +0900317 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;
Hugo Benichib577d652017-06-27 15:13:20 +0900318 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900319}