Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
| 19 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 20 | |
| 21 | import java.net.Inet4Address; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.net.IConnectivityManager; |
| 25 | import android.net.InterfaceConfiguration; |
| 26 | import android.net.LinkAddress; |
| 27 | import android.net.LinkProperties; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 28 | import android.net.NetworkAgent; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 29 | import android.net.NetworkUtils; |
| 30 | import android.net.RouteInfo; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 33 | import android.os.Messenger; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 34 | import android.os.INetworkManagementService; |
| 35 | import android.os.RemoteException; |
| 36 | import android.util.Slog; |
| 37 | |
| 38 | import com.android.server.net.BaseNetworkObserver; |
| 39 | |
| 40 | /** |
| 41 | * @hide |
| 42 | * |
| 43 | * Class to manage a 464xlat CLAT daemon. |
| 44 | */ |
| 45 | public class Nat464Xlat extends BaseNetworkObserver { |
| 46 | private Context mContext; |
| 47 | private INetworkManagementService mNMService; |
| 48 | private IConnectivityManager mConnService; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 49 | // Whether we started clatd and expect it to be running. |
| 50 | private boolean mIsStarted; |
| 51 | // Whether the clatd interface exists (i.e., clatd is running). |
| 52 | private boolean mIsRunning; |
| 53 | // The LinkProperties of the clat interface. |
| 54 | private LinkProperties mLP; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 55 | // Current LinkProperties of the network. Includes mLP as a stacked link when clat is active. |
| 56 | private LinkProperties mBaseLP; |
| 57 | // ConnectivityService Handler for LinkProperties updates. |
| 58 | private Handler mHandler; |
| 59 | // Marker to connote which network we're augmenting. |
| 60 | private Messenger mNetworkMessenger; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 61 | |
| 62 | // This must match the interface name in clatd.conf. |
| 63 | private static final String CLAT_INTERFACE_NAME = "clat4"; |
| 64 | |
| 65 | private static final String TAG = "Nat464Xlat"; |
| 66 | |
| 67 | public Nat464Xlat(Context context, INetworkManagementService nmService, |
| 68 | IConnectivityManager connService, Handler handler) { |
| 69 | mContext = context; |
| 70 | mNMService = nmService; |
| 71 | mConnService = connService; |
| 72 | mHandler = handler; |
| 73 | |
| 74 | mIsStarted = false; |
| 75 | mIsRunning = false; |
| 76 | mLP = new LinkProperties(); |
Lorenzo Colitti | 43b76df | 2014-05-16 12:10:32 -0700 | [diff] [blame] | 77 | |
| 78 | // If this is a runtime restart, it's possible that clatd is already |
| 79 | // running, but we don't know about it. If so, stop it. |
| 80 | try { |
| 81 | if (mNMService.isClatdStarted()) { |
| 82 | mNMService.stopClatd(); |
| 83 | } |
| 84 | } catch(RemoteException e) {} // Well, we tried. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 88 | * 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 Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 91 | */ |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame^] | 92 | public static boolean requiresClat(NetworkAgentInfo nai) { |
| 93 | final int netType = nai.networkInfo.getType(); |
| 94 | final boolean connected = nai.networkInfo.isConnected(); |
| 95 | final boolean hasIPv4Address = |
| 96 | (nai.linkProperties != null) ? nai.linkProperties.hasIPv4Address() : false; |
| 97 | Slog.d(TAG, "requiresClat: netType=" + netType + |
| 98 | ", connected=" + connected + |
| 99 | ", hasIPv4Address=" + hasIPv4Address); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 100 | // Only support clat on mobile for now. |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame^] | 101 | return netType == TYPE_MOBILE && connected && !hasIPv4Address; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame^] | 104 | public boolean isRunningClat(NetworkAgentInfo network) { |
| 105 | return mNetworkMessenger == network.messenger; |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 108 | /** |
| 109 | * Starts the clat daemon. |
| 110 | * @param lp The link properties of the interface to start clatd on. |
| 111 | */ |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 112 | public void startClat(NetworkAgentInfo network) { |
| 113 | if (mNetworkMessenger != null && mNetworkMessenger != network.messenger) { |
| 114 | Slog.e(TAG, "startClat: too many networks requesting clat"); |
| 115 | return; |
| 116 | } |
| 117 | mNetworkMessenger = network.messenger; |
| 118 | LinkProperties lp = network.linkProperties; |
| 119 | mBaseLP = new LinkProperties(lp); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 120 | if (mIsStarted) { |
| 121 | Slog.e(TAG, "startClat: already started"); |
| 122 | return; |
| 123 | } |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 124 | String iface = lp.getInterfaceName(); |
| 125 | Slog.i(TAG, "Starting clatd on " + iface + ", lp=" + lp); |
| 126 | try { |
| 127 | mNMService.startClatd(iface); |
| 128 | } catch(RemoteException e) { |
| 129 | Slog.e(TAG, "Error starting clat daemon: " + e); |
| 130 | } |
| 131 | mIsStarted = true; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Stops the clat daemon. |
| 136 | */ |
| 137 | public void stopClat() { |
| 138 | if (mIsStarted) { |
| 139 | Slog.i(TAG, "Stopping clatd"); |
| 140 | try { |
| 141 | mNMService.stopClatd(); |
| 142 | } catch(RemoteException e) { |
| 143 | Slog.e(TAG, "Error stopping clat daemon: " + e); |
| 144 | } |
| 145 | mIsStarted = false; |
| 146 | mIsRunning = false; |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 147 | mNetworkMessenger = null; |
| 148 | mBaseLP = null; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 149 | mLP.clear(); |
| 150 | } else { |
| 151 | Slog.e(TAG, "stopClat: already stopped"); |
| 152 | } |
| 153 | } |
| 154 | |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 155 | private void updateConnectivityService() { |
| 156 | Message msg = mHandler.obtainMessage( |
| 157 | NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, mBaseLP); |
| 158 | msg.replyTo = mNetworkMessenger; |
| 159 | Slog.i(TAG, "sending message to ConnectivityService: " + msg); |
| 160 | msg.sendToTarget(); |
| 161 | } |
| 162 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame^] | 163 | // Copies the stacked clat link in oldLp, if any, to the LinkProperties in nai. |
| 164 | public void fixupLinkProperties(NetworkAgentInfo nai, LinkProperties oldLp) { |
| 165 | if (isRunningClat(nai) && |
| 166 | nai.linkProperties != null && |
| 167 | !nai.linkProperties.getAllInterfaceNames().contains(CLAT_INTERFACE_NAME)) { |
| 168 | Slog.d(TAG, "clatd running, updating NAI for " + nai.linkProperties.getInterfaceName()); |
| 169 | for (LinkProperties stacked: oldLp.getStackedLinks()) { |
| 170 | if (CLAT_INTERFACE_NAME.equals(stacked.getInterfaceName())) { |
| 171 | nai.linkProperties.addStackedLink(stacked); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 178 | @Override |
| 179 | public void interfaceAdded(String iface) { |
| 180 | if (iface.equals(CLAT_INTERFACE_NAME)) { |
| 181 | Slog.i(TAG, "interface " + CLAT_INTERFACE_NAME + |
| 182 | " added, mIsRunning = " + mIsRunning + " -> true"); |
| 183 | mIsRunning = true; |
| 184 | |
Lorenzo Colitti | 861c3fc | 2013-06-17 11:10:27 -0700 | [diff] [blame] | 185 | // Create the LinkProperties for the clat interface by fetching the |
| 186 | // IPv4 address for the interface and adding an IPv4 default route, |
| 187 | // then stack the LinkProperties on top of the link it's running on. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 188 | try { |
| 189 | InterfaceConfiguration config = mNMService.getInterfaceConfig(iface); |
Lorenzo Colitti | 861c3fc | 2013-06-17 11:10:27 -0700 | [diff] [blame] | 190 | LinkAddress clatAddress = config.getLinkAddress(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 191 | mLP.clear(); |
| 192 | mLP.setInterfaceName(iface); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame^] | 193 | |
| 194 | // Although the clat interface is a point-to-point tunnel, we don't |
| 195 | // point the route directly at the interface because some apps don't |
| 196 | // understand routes without gateways (see, e.g., http://b/9597256 |
| 197 | // http://b/9597516). Instead, set the next hop of the route to the |
| 198 | // clat IPv4 address itself (for those apps, it doesn't matter what |
| 199 | // the IP of the gateway is, only that there is one). |
Lorenzo Colitti | 861c3fc | 2013-06-17 11:10:27 -0700 | [diff] [blame] | 200 | RouteInfo ipv4Default = new RouteInfo(new LinkAddress(Inet4Address.ANY, 0), |
| 201 | clatAddress.getAddress(), iface); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 202 | mLP.addRoute(ipv4Default); |
Lorenzo Colitti | 861c3fc | 2013-06-17 11:10:27 -0700 | [diff] [blame] | 203 | mLP.addLinkAddress(clatAddress); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 204 | mBaseLP.addStackedLink(mLP); |
| 205 | Slog.i(TAG, "Adding stacked link. tracker LP: " + mBaseLP); |
| 206 | updateConnectivityService(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 207 | } catch(RemoteException e) { |
| 208 | Slog.e(TAG, "Error getting link properties: " + e); |
| 209 | } |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void interfaceRemoved(String iface) { |
| 215 | if (iface == CLAT_INTERFACE_NAME) { |
| 216 | if (mIsRunning) { |
| 217 | NetworkUtils.resetConnections( |
| 218 | CLAT_INTERFACE_NAME, |
| 219 | NetworkUtils.RESET_IPV4_ADDRESSES); |
Lorenzo Colitti | 43b76df | 2014-05-16 12:10:32 -0700 | [diff] [blame] | 220 | mBaseLP.removeStackedLink(mLP); |
| 221 | updateConnectivityService(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 222 | } |
| 223 | Slog.i(TAG, "interface " + CLAT_INTERFACE_NAME + |
| 224 | " removed, mIsRunning = " + mIsRunning + " -> false"); |
| 225 | mIsRunning = false; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 226 | mLP.clear(); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 227 | Slog.i(TAG, "mLP = " + mLP); |
| 228 | } |
| 229 | } |
| 230 | }; |