blob: 27426d7bded9cdfb79eccec050e04805002b26eb [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 java.net.Inet4Address;
20
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090021import android.net.InterfaceConfiguration;
Lorenzo Colitti853d7412016-03-03 17:17:29 +090022import android.net.ConnectivityManager;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090023import android.net.LinkAddress;
24import android.net.LinkProperties;
Paul Jensen3b759822014-05-13 11:44:01 -040025import android.net.NetworkAgent;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090026import android.net.RouteInfo;
27import android.os.Handler;
28import android.os.Message;
29import android.os.INetworkManagementService;
30import android.os.RemoteException;
31import android.util.Slog;
32
33import com.android.server.net.BaseNetworkObserver;
Lorenzo Colitti853d7412016-03-03 17:17:29 +090034import com.android.internal.util.ArrayUtils;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090035
36/**
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090037 * Class to manage a 464xlat CLAT daemon.
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
57 // ConnectivityService Handler for LinkProperties updates.
58 private final Handler mHandler;
59
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090060 // The network we're running on, and its type.
Lorenzo Colitti95439462014-10-09 13:44:48 +090061 private final NetworkAgentInfo mNetwork;
62
63 // Internal state variables.
64 //
65 // The possible states are:
66 // - Idle: start() not called. Everything is null.
67 // - Starting: start() called. Interfaces are non-null. isStarted() returns true.
68 // mIsRunning is false.
Lorenzo Colitti7f6c0d72014-11-10 19:17:35 -080069 // - Running: start() called, and interfaceLinkStateChanged() told us that mIface is up.
70 // mIsRunning is true.
Lorenzo Colitti95439462014-10-09 13:44:48 +090071 //
72 // Once mIface is non-null and isStarted() is true, methods called by ConnectivityService on
73 // its handler thread must not modify any internal state variables; they are only updated by the
74 // interface observers, called on the notification threads.
75 private String mBaseIface;
76 private String mIface;
77 private boolean mIsRunning;
78
Hugo Benichib577d652017-06-27 15:13:20 +090079 public Nat464Xlat(INetworkManagementService nmService, Handler handler, NetworkAgentInfo nai) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090080 mNMService = nmService;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090081 mHandler = handler;
Lorenzo Colitti95439462014-10-09 13:44:48 +090082 mNetwork = nai;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090083 }
84
85 /**
Paul Jensen3b759822014-05-13 11:44:01 -040086 * Determines whether a network requires clat.
87 * @param network the NetworkAgentInfo corresponding to the network.
88 * @return true if the network requires clat, false otherwise.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090089 */
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090090 public static boolean requiresClat(NetworkAgentInfo nai) {
Hugo Benichib577d652017-06-27 15:13:20 +090091 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090092 final int netType = nai.networkInfo.getType();
Hugo Benichib577d652017-06-27 15:13:20 +090093 final boolean supported = ArrayUtils.contains(NETWORK_TYPES, nai.networkInfo.getType());
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090094 final boolean connected = nai.networkInfo.isConnected();
Hugo Benichib577d652017-06-27 15:13:20 +090095 // We only run clat on networks that don't have a native IPv4 address.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090096 final boolean hasIPv4Address =
Hugo Benichib577d652017-06-27 15:13:20 +090097 (nai.linkProperties != null) && nai.linkProperties.hasIPv4Address();
98 return supported && connected && !hasIPv4Address;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090099 }
100
Lorenzo Colitti95439462014-10-09 13:44:48 +0900101 /**
102 * Determines whether clatd is started. Always true, except a) if start has not yet been called,
103 * or b) if our interface was removed.
104 */
105 public boolean isStarted() {
106 return mIface != null;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900107 }
108
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900109 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900110 * Clears internal state. Must not be called by ConnectivityService.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900111 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900112 private void clear() {
113 mIface = null;
114 mBaseIface = null;
115 mIsRunning = false;
116 }
117
118 /**
119 * Starts the clat daemon. Called by ConnectivityService on the handler thread.
120 */
121 public void start() {
122 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900123 Slog.e(TAG, "startClat: already started");
124 return;
125 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900126
127 if (mNetwork.linkProperties == null) {
128 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
129 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900130 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900131
132 try {
133 mNMService.registerObserver(this);
134 } catch(RemoteException e) {
135 Slog.e(TAG, "startClat: Can't register interface observer for clat on " + mNetwork);
136 return;
137 }
138
139 mBaseIface = mNetwork.linkProperties.getInterfaceName();
140 if (mBaseIface == null) {
141 Slog.e(TAG, "startClat: Can't start clat on null interface");
142 return;
143 }
144 mIface = CLAT_PREFIX + mBaseIface;
145 // From now on, isStarted() will return true.
146
147 Slog.i(TAG, "Starting clatd on " + mBaseIface);
148 try {
149 mNMService.startClatd(mBaseIface);
150 } catch(RemoteException|IllegalStateException e) {
151 Slog.e(TAG, "Error starting clatd: " + e);
152 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900153 }
154
155 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900156 * Stops the clat daemon. Called by ConnectivityService on the handler thread.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900157 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900158 public void stop() {
159 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900160 Slog.i(TAG, "Stopping clatd");
161 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900162 mNMService.stopClatd(mBaseIface);
163 } catch(RemoteException|IllegalStateException e) {
164 Slog.e(TAG, "Error stopping clatd: " + e);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900165 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900166 // When clatd stops and its interface is deleted, interfaceRemoved() will notify
167 // ConnectivityService and call clear().
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900168 } else {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900169 Slog.e(TAG, "clatd: already stopped");
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900170 }
171 }
172
Lorenzo Colitti95439462014-10-09 13:44:48 +0900173 private void updateConnectivityService(LinkProperties lp) {
174 Message msg = mHandler.obtainMessage(NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, lp);
175 msg.replyTo = mNetwork.messenger;
Paul Jensen3b759822014-05-13 11:44:01 -0400176 Slog.i(TAG, "sending message to ConnectivityService: " + msg);
177 msg.sendToTarget();
178 }
179
Lorenzo Colitti95439462014-10-09 13:44:48 +0900180 /**
181 * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork.
182 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
183 * has no idea that 464xlat is running on top of it.
184 */
185 public void fixupLinkProperties(LinkProperties oldLp) {
186 if (mNetwork.clatd != null &&
187 mIsRunning &&
188 mNetwork.linkProperties != null &&
189 !mNetwork.linkProperties.getAllInterfaceNames().contains(mIface)) {
190 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900191 for (LinkProperties stacked: oldLp.getStackedLinks()) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900192 if (mIface.equals(stacked.getInterfaceName())) {
193 mNetwork.linkProperties.addStackedLink(stacked);
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900194 break;
195 }
196 }
197 }
198 }
199
Lorenzo Colitti95439462014-10-09 13:44:48 +0900200 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
201 LinkProperties stacked = new LinkProperties();
202 stacked.setInterfaceName(mIface);
203
204 // Although the clat interface is a point-to-point tunnel, we don't
205 // point the route directly at the interface because some apps don't
206 // understand routes without gateways (see, e.g., http://b/9597256
207 // http://b/9597516). Instead, set the next hop of the route to the
208 // clat IPv4 address itself (for those apps, it doesn't matter what
209 // the IP of the gateway is, only that there is one).
210 RouteInfo ipv4Default = new RouteInfo(
211 new LinkAddress(Inet4Address.ANY, 0),
212 clatAddress.getAddress(), mIface);
213 stacked.addRoute(ipv4Default);
214 stacked.addLinkAddress(clatAddress);
215 return stacked;
216 }
217
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900218 private LinkAddress getLinkAddress(String iface) {
219 try {
220 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
221 return config.getLinkAddress();
222 } catch(RemoteException|IllegalStateException e) {
223 Slog.e(TAG, "Error getting link properties: " + e);
224 return null;
225 }
226 }
227
228 private void maybeSetIpv6NdOffload(String iface, boolean on) {
Hugo Benichib577d652017-06-27 15:13:20 +0900229 // TODO: migrate to NetworkCapabilities.TRANSPORT_*.
Lorenzo Colitti853d7412016-03-03 17:17:29 +0900230 if (mNetwork.networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900231 return;
232 }
233 try {
234 Slog.d(TAG, (on ? "En" : "Dis") + "abling ND offload on " + iface);
235 mNMService.setInterfaceIpv6NdOffload(iface, on);
236 } catch(RemoteException|IllegalStateException e) {
237 Slog.w(TAG, "Changing IPv6 ND offload on " + iface + "failed: " + e);
238 }
239 }
240
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900241 @Override
Lorenzo Colitti7f6c0d72014-11-10 19:17:35 -0800242 public void interfaceLinkStateChanged(String iface, boolean up) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900243 // Called by the InterfaceObserver on its own thread, so can race with stop().
Lorenzo Colitti7f6c0d72014-11-10 19:17:35 -0800244 if (isStarted() && up && mIface.equals(iface)) {
245 Slog.i(TAG, "interface " + iface + " is up, mIsRunning " + mIsRunning + "->true");
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900246
Lorenzo Colitti95439462014-10-09 13:44:48 +0900247 if (!mIsRunning) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900248 LinkAddress clatAddress = getLinkAddress(iface);
249 if (clatAddress == null) {
250 return;
251 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900252 mIsRunning = true;
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900253 maybeSetIpv6NdOffload(mBaseIface, false);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900254 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
255 lp.addStackedLink(makeLinkProperties(clatAddress));
256 Slog.i(TAG, "Adding stacked link " + mIface + " on top of " + mBaseIface);
257 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900258 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900259 }
260 }
261
262 @Override
263 public void interfaceRemoved(String iface) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900264 if (isStarted() && mIface.equals(iface)) {
265 Slog.i(TAG, "interface " + iface + " removed, mIsRunning " + mIsRunning + "->false");
266
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900267 if (mIsRunning) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900268 // The interface going away likely means clatd has crashed. Ask netd to stop it,
269 // because otherwise when we try to start it again on the same base interface netd
270 // will complain that it's already started.
271 //
272 // Note that this method can be called by the interface observer at the same time
273 // that ConnectivityService calls stop(). In this case, the second call to
274 // stopClatd() will just throw IllegalStateException, which we'll ignore.
275 try {
276 mNMService.unregisterObserver(this);
277 mNMService.stopClatd(mBaseIface);
278 } catch (RemoteException|IllegalStateException e) {
279 // Well, we tried.
280 }
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900281 maybeSetIpv6NdOffload(mBaseIface, true);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900282 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
283 lp.removeStackedLink(mIface);
284 clear();
285 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900286 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900287 }
288 }
Hugo Benichib577d652017-06-27 15:13:20 +0900289
290 @Override
291 public String toString() {
292 return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mIsRunning: " + mIsRunning;
293 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900294}