blob: c145ca304672c13152742280d94f674820845a8a [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
19import static android.net.ConnectivityManager.TYPE_MOBILE;
Lorenzo Colittie21a26b2014-10-28 15:24:03 +090020import static android.net.ConnectivityManager.TYPE_WIFI;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090021
22import java.net.Inet4Address;
23
24import android.content.Context;
25import android.net.IConnectivityManager;
26import android.net.InterfaceConfiguration;
27import android.net.LinkAddress;
28import android.net.LinkProperties;
Paul Jensen3b759822014-05-13 11:44:01 -040029import android.net.NetworkAgent;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090030import android.net.NetworkUtils;
31import android.net.RouteInfo;
32import android.os.Handler;
33import android.os.Message;
Paul Jensen3b759822014-05-13 11:44:01 -040034import android.os.Messenger;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090035import android.os.INetworkManagementService;
36import android.os.RemoteException;
37import android.util.Slog;
38
39import com.android.server.net.BaseNetworkObserver;
40
41/**
42 * @hide
43 *
44 * Class to manage a 464xlat CLAT daemon.
45 */
46public class Nat464Xlat extends BaseNetworkObserver {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090047 private static final String TAG = "Nat464Xlat";
48
Lorenzo Colitti95439462014-10-09 13:44:48 +090049 // This must match the interface prefix in clatd.c.
50 private static final String CLAT_PREFIX = "v4-";
51
52 private final INetworkManagementService mNMService;
53
54 // ConnectivityService Handler for LinkProperties updates.
55 private final Handler mHandler;
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
60 // Internal state variables.
61 //
62 // The possible states are:
63 // - Idle: start() not called. Everything is null.
64 // - Starting: start() called. Interfaces are non-null. isStarted() returns true.
65 // mIsRunning is false.
66 // - Running: start() called, and interfaceAdded() told us that mIface is up. Clat IP address
67 // is non-null. mIsRunning is true.
68 //
69 // Once mIface is non-null and isStarted() is true, methods called by ConnectivityService on
70 // its handler thread must not modify any internal state variables; they are only updated by the
71 // interface observers, called on the notification threads.
72 private String mBaseIface;
73 private String mIface;
74 private boolean mIsRunning;
75
76 public Nat464Xlat(
77 Context context, INetworkManagementService nmService,
78 Handler handler, NetworkAgentInfo nai) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090079 mNMService = nmService;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090080 mHandler = handler;
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) {
90 final int netType = nai.networkInfo.getType();
91 final boolean connected = nai.networkInfo.isConnected();
92 final boolean hasIPv4Address =
93 (nai.linkProperties != null) ? nai.linkProperties.hasIPv4Address() : false;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090094 // Only support clat on mobile for now.
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +090095 return netType == TYPE_MOBILE && connected && !hasIPv4Address;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090096 }
97
Lorenzo Colitti95439462014-10-09 13:44:48 +090098 /**
99 * Determines whether clatd is started. Always true, except a) if start has not yet been called,
100 * or b) if our interface was removed.
101 */
102 public boolean isStarted() {
103 return mIface != null;
Lorenzo Colittid2ef1e52013-03-28 14:13:43 +0900104 }
105
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900106 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900107 * Clears internal state. Must not be called by ConnectivityService.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900108 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900109 private void clear() {
110 mIface = null;
111 mBaseIface = null;
112 mIsRunning = false;
113 }
114
115 /**
116 * Starts the clat daemon. Called by ConnectivityService on the handler thread.
117 */
118 public void start() {
119 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900120 Slog.e(TAG, "startClat: already started");
121 return;
122 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900123
124 if (mNetwork.linkProperties == null) {
125 Slog.e(TAG, "startClat: Can't start clat with null LinkProperties");
126 return;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900127 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900128
129 try {
130 mNMService.registerObserver(this);
131 } catch(RemoteException e) {
132 Slog.e(TAG, "startClat: Can't register interface observer for clat on " + mNetwork);
133 return;
134 }
135
136 mBaseIface = mNetwork.linkProperties.getInterfaceName();
137 if (mBaseIface == null) {
138 Slog.e(TAG, "startClat: Can't start clat on null interface");
139 return;
140 }
141 mIface = CLAT_PREFIX + mBaseIface;
142 // From now on, isStarted() will return true.
143
144 Slog.i(TAG, "Starting clatd on " + mBaseIface);
145 try {
146 mNMService.startClatd(mBaseIface);
147 } catch(RemoteException|IllegalStateException e) {
148 Slog.e(TAG, "Error starting clatd: " + e);
149 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900150 }
151
152 /**
Lorenzo Colitti95439462014-10-09 13:44:48 +0900153 * Stops the clat daemon. Called by ConnectivityService on the handler thread.
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900154 */
Lorenzo Colitti95439462014-10-09 13:44:48 +0900155 public void stop() {
156 if (isStarted()) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900157 Slog.i(TAG, "Stopping clatd");
158 try {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900159 mNMService.stopClatd(mBaseIface);
160 } catch(RemoteException|IllegalStateException e) {
161 Slog.e(TAG, "Error stopping clatd: " + e);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900162 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900163 // When clatd stops and its interface is deleted, interfaceRemoved() will notify
164 // ConnectivityService and call clear().
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900165 } else {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900166 Slog.e(TAG, "clatd: already stopped");
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900167 }
168 }
169
Lorenzo Colitti95439462014-10-09 13:44:48 +0900170 private void updateConnectivityService(LinkProperties lp) {
171 Message msg = mHandler.obtainMessage(NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, lp);
172 msg.replyTo = mNetwork.messenger;
Paul Jensen3b759822014-05-13 11:44:01 -0400173 Slog.i(TAG, "sending message to ConnectivityService: " + msg);
174 msg.sendToTarget();
175 }
176
Lorenzo Colitti95439462014-10-09 13:44:48 +0900177 /**
178 * Copies the stacked clat link in oldLp, if any, to the LinkProperties in mNetwork.
179 * This is necessary because the LinkProperties in mNetwork come from the transport layer, which
180 * has no idea that 464xlat is running on top of it.
181 */
182 public void fixupLinkProperties(LinkProperties oldLp) {
183 if (mNetwork.clatd != null &&
184 mIsRunning &&
185 mNetwork.linkProperties != null &&
186 !mNetwork.linkProperties.getAllInterfaceNames().contains(mIface)) {
187 Slog.d(TAG, "clatd running, updating NAI for " + mIface);
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900188 for (LinkProperties stacked: oldLp.getStackedLinks()) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900189 if (mIface.equals(stacked.getInterfaceName())) {
190 mNetwork.linkProperties.addStackedLink(stacked);
Lorenzo Colitti1df5fa52014-09-20 13:47:47 +0900191 break;
192 }
193 }
194 }
195 }
196
Lorenzo Colitti95439462014-10-09 13:44:48 +0900197 private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
198 LinkProperties stacked = new LinkProperties();
199 stacked.setInterfaceName(mIface);
200
201 // Although the clat interface is a point-to-point tunnel, we don't
202 // point the route directly at the interface because some apps don't
203 // understand routes without gateways (see, e.g., http://b/9597256
204 // http://b/9597516). Instead, set the next hop of the route to the
205 // clat IPv4 address itself (for those apps, it doesn't matter what
206 // the IP of the gateway is, only that there is one).
207 RouteInfo ipv4Default = new RouteInfo(
208 new LinkAddress(Inet4Address.ANY, 0),
209 clatAddress.getAddress(), mIface);
210 stacked.addRoute(ipv4Default);
211 stacked.addLinkAddress(clatAddress);
212 return stacked;
213 }
214
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900215 private LinkAddress getLinkAddress(String iface) {
216 try {
217 InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
218 return config.getLinkAddress();
219 } catch(RemoteException|IllegalStateException e) {
220 Slog.e(TAG, "Error getting link properties: " + e);
221 return null;
222 }
223 }
224
225 private void maybeSetIpv6NdOffload(String iface, boolean on) {
226 if (mNetwork.networkInfo.getType() != TYPE_WIFI) {
227 return;
228 }
229 try {
230 Slog.d(TAG, (on ? "En" : "Dis") + "abling ND offload on " + iface);
231 mNMService.setInterfaceIpv6NdOffload(iface, on);
232 } catch(RemoteException|IllegalStateException e) {
233 Slog.w(TAG, "Changing IPv6 ND offload on " + iface + "failed: " + e);
234 }
235 }
236
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900237 @Override
238 public void interfaceAdded(String iface) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900239 // Called by the InterfaceObserver on its own thread, so can race with stop().
240 if (isStarted() && mIface.equals(iface)) {
241 Slog.i(TAG, "interface " + iface + " added, mIsRunning " + mIsRunning + "->true");
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900242
Lorenzo Colitti95439462014-10-09 13:44:48 +0900243 if (!mIsRunning) {
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900244 LinkAddress clatAddress = getLinkAddress(iface);
245 if (clatAddress == null) {
246 return;
247 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900248 mIsRunning = true;
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900249 maybeSetIpv6NdOffload(mBaseIface, false);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900250 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
251 lp.addStackedLink(makeLinkProperties(clatAddress));
252 Slog.i(TAG, "Adding stacked link " + mIface + " on top of " + mBaseIface);
253 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900254 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900255 }
256 }
257
258 @Override
259 public void interfaceRemoved(String iface) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900260 if (isStarted() && mIface.equals(iface)) {
261 Slog.i(TAG, "interface " + iface + " removed, mIsRunning " + mIsRunning + "->false");
262
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900263 if (mIsRunning) {
Lorenzo Colitti95439462014-10-09 13:44:48 +0900264 // The interface going away likely means clatd has crashed. Ask netd to stop it,
265 // because otherwise when we try to start it again on the same base interface netd
266 // will complain that it's already started.
267 //
268 // Note that this method can be called by the interface observer at the same time
269 // that ConnectivityService calls stop(). In this case, the second call to
270 // stopClatd() will just throw IllegalStateException, which we'll ignore.
271 try {
272 mNMService.unregisterObserver(this);
273 mNMService.stopClatd(mBaseIface);
274 } catch (RemoteException|IllegalStateException e) {
275 // Well, we tried.
276 }
Lorenzo Colittie21a26b2014-10-28 15:24:03 +0900277 maybeSetIpv6NdOffload(mBaseIface, true);
Lorenzo Colitti95439462014-10-09 13:44:48 +0900278 LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
279 lp.removeStackedLink(mIface);
280 clear();
281 updateConnectivityService(lp);
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900282 }
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +0900283 }
284 }
Lorenzo Colitti95439462014-10-09 13:44:48 +0900285}