import android.net.IConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
+import android.net.LinkProperties.CompareResult;
import android.net.MobileDataStateTracker;
import android.net.NetworkConfig;
import android.net.NetworkInfo;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
+import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.UnknownHostException;
public class ConnectivityService extends IConnectivityManager.Stub {
private static final boolean DBG = true;
+ private static final boolean VDBG = true;
private static final String TAG = "ConnectivityService";
// how long to wait before switching back to a radio's default network
private NetworkStateTracker mNetTrackers[];
/**
+ * The link properties that define the current links
+ */
+ private LinkProperties mCurrentLinkProperties[];
+
+ /**
* A per Net list of the PID's that requested access to the net
* used both as a refcount and for per-PID DNS selection
*/
private InetAddress mDefaultDns;
+ // this collection is used to refcount the added routes - if there are none left
+ // it's time to remove the route from the route table
+ private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
+
// used in DBG mode to track inet condition reports
private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
private ArrayList mInetLog;
}
RadioAttributes[] mRadioAttributes;
+ // the set of network types that can only be enabled by system/sig apps
+ List mProtectedNetworks;
+
public static synchronized ConnectivityService getInstance(Context context) {
if (sServiceInstance == null) {
sServiceInstance = new ConnectivityService(context);
mNetTrackers = new NetworkStateTracker[
ConnectivityManager.MAX_NETWORK_TYPE+1];
+ mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
mNetworkPreference = getPersistedNetworkPreference();
}
}
+ mProtectedNetworks = new ArrayList<Integer>();
+ int[] protectedNetworks = context.getResources().getIntArray(
+ com.android.internal.R.array.config_protectedNetworks);
+ for (int p : protectedNetworks) {
+ if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
+ mProtectedNetworks.add(p);
+ } else {
+ if (DBG) loge("Ignoring protectedNetwork " + p);
+ }
+ }
+
// high priority first
mPriorityList = new int[mNetworksDefined];
{
mNetConfigs[netType].radio);
continue;
}
+ mCurrentLinkProperties[netType] = null;
}
mTethering = new Tethering(mContext, mHandler.getLooper());
- mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
- !mTethering.isDunRequired()) &&
- (mTethering.getTetherableUsbRegexs().length != 0 ||
+ mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
mTethering.getTetherableWifiRegexs().length != 0 ||
mTethering.getTetherableBluetoothRegexs().length != 0) &&
- mTethering.getUpstreamIfaceRegexs().length != 0);
+ mTethering.getUpstreamIfaceTypes().length != 0);
if (DBG) {
mInetLog = new ArrayList();
usedNetworkType = networkType;
}
}
+
+ if (mProtectedNetworks.contains(usedNetworkType)) {
+ enforceConnectivityInternalPermission();
+ }
+
NetworkStateTracker network = mNetTrackers[usedNetworkType];
if (network != null) {
Integer currentPid = new Integer(getCallingPid());
*/
public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
enforceChangePermission();
+ if (mProtectedNetworks.contains(networkType)) {
+ enforceConnectivityInternalPermission();
+ }
+
if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
return false;
}
}
try {
InetAddress addr = InetAddress.getByAddress(hostAddress);
- return addHostRoute(tracker, addr, 0);
+ LinkProperties lp = tracker.getLinkProperties();
+ return addRoute(lp, RouteInfo.makeHostRoute(addr));
} catch (UnknownHostException e) {}
return false;
}
- /**
- * Ensure that a network route exists to deliver traffic to the specified
- * host via the mobile data network.
- * @param hostAddress the IP address of the host to which the route is desired,
- * in network byte order.
- * TODO - deprecate
- * @return {@code true} on success, {@code false} on failure
- */
- private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
- LinkProperties lp = nt.getLinkProperties();
- if ((lp == null) || (hostAddress == null)) return false;
+ private boolean addRoute(LinkProperties p, RouteInfo r) {
+ return modifyRoute(p.getInterfaceName(), p, r, 0, true);
+ }
- String interfaceName = lp.getInterfaceName();
- if (DBG) {
- log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
- cycleCount);
- }
- if (interfaceName == null) {
- if (DBG) loge("addHostRoute failed due to null interface name");
+ private boolean removeRoute(LinkProperties p, RouteInfo r) {
+ return modifyRoute(p.getInterfaceName(), p, r, 0, false);
+ }
+
+ private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
+ boolean doAdd) {
+ if ((ifaceName == null) || (lp == null) || (r == null)) return false;
+
+ if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
+ loge("Error adding route - too much recursion");
return false;
}
- RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
- InetAddress gatewayAddress = null;
- if (bestRoute != null) {
- gatewayAddress = bestRoute.getGateway();
- // if the best route is ourself, don't relf-reference, just add the host route
- if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
+ if (r.isHostRoute() == false) {
+ RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
+ if (bestRoute != null) {
+ if (bestRoute.getGateway().equals(r.getGateway())) {
+ // if there is no better route, add the implied hostroute for our gateway
+ bestRoute = RouteInfo.makeHostRoute(r.getGateway());
+ } else {
+ // if we will connect to our gateway through another route, add a direct
+ // route to it's gateway
+ bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
+ }
+ modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd);
+ }
}
- if (gatewayAddress != null) {
- if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
- loge("Error adding hostroute - too much recursion");
+ if (doAdd) {
+ if (DBG) log("Adding " + r + " for interface " + ifaceName);
+ mAddedRoutes.add(r);
+ try {
+ mNetd.addRoute(ifaceName, r);
+ } catch (Exception e) {
+ // never crash - catch them all
+ loge("Exception trying to add a route: " + e);
return false;
}
- if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
- }
-
- RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
-
- try {
- mNetd.addRoute(interfaceName, route);
- return true;
- } catch (Exception ex) {
- return false;
+ } else {
+ // if we remove this one and there are no more like it, then refcount==0 and
+ // we can remove it from the table
+ mAddedRoutes.remove(r);
+ if (mAddedRoutes.contains(r) == false) {
+ if (DBG) log("Removing " + r + " for interface " + ifaceName);
+ try {
+ mNetd.removeRoute(ifaceName, r);
+ } catch (Exception e) {
+ // never crash - catch them all
+ loge("Exception trying to remove a route: " + e);
+ return false;
+ }
+ } else {
+ if (DBG) log("not removing " + r + " as it's still in use");
+ }
}
- }
-
- // TODO support the removal of single host routes. Keep a ref count of them so we
- // aren't over-zealous
- private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
- return false;
+ return true;
}
/**
}
public void setDataDependency(int networkType, boolean met) {
- enforceChangePermission();
- if (DBG) {
- log("setDataDependency(" + networkType + ", " + met + ")");
- }
+ enforceConnectivityInternalPermission();
+
mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
(met ? ENABLED : DISABLED), networkType));
}
* right routing table entries exist.
*/
private void handleConnectivityChange(int netType, boolean doReset) {
+ int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
+
/*
* If a non-default network is enabled, add the host routes that
* will allow it's DNS servers to be accessed.
*/
handleDnsConfigurationChange(netType);
+ LinkProperties curLp = mCurrentLinkProperties[netType];
+ LinkProperties newLp = null;
+
if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
- if (mNetConfigs[netType].isDefault()) {
- handleApplyDefaultProxy(netType);
- addDefaultRoute(mNetTrackers[netType]);
- } else {
- addPrivateDnsRoutes(mNetTrackers[netType]);
+ newLp = mNetTrackers[netType].getLinkProperties();
+ if (VDBG) {
+ log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
+ " doReset=" + doReset + " resetMask=" + resetMask +
+ "\n curLp=" + curLp +
+ "\n newLp=" + newLp);
}
- /** Notify TetheringService if interface name has been changed. */
- if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
- Phone.REASON_LINK_PROPERTIES_CHANGED)) {
- handleTetherIfaceChange(netType);
+ if (curLp != null) {
+ if (curLp.isIdenticalInterfaceName(newLp)) {
+ CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
+ if ((car.removed.size() != 0) || (car.added.size() != 0)) {
+ for (LinkAddress linkAddr : car.removed) {
+ if (linkAddr.getAddress() instanceof Inet4Address) {
+ resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
+ }
+ if (linkAddr.getAddress() instanceof Inet6Address) {
+ resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
+ }
+ }
+ if (DBG) {
+ log("handleConnectivityChange: addresses changed" +
+ " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
+ "\n car=" + car);
+ }
+ } else {
+ if (DBG) {
+ log("handleConnectivityChange: address are the same reset per doReset" +
+ " linkProperty[" + netType + "]:" +
+ " resetMask=" + resetMask);
+ }
+ }
+ } else {
+ resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
+ log("handleConnectivityChange: interface not not equivalent reset both" +
+ " linkProperty[" + netType + "]:" +
+ " resetMask=" + resetMask);
+ }
}
- } else {
if (mNetConfigs[netType].isDefault()) {
- removeDefaultRoute(mNetTrackers[netType]);
- } else {
- removePrivateDnsRoutes(mNetTrackers[netType]);
+ handleApplyDefaultProxy(netType);
+ }
+ } else {
+ if (VDBG) {
+ log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
+ " doReset=" + doReset + " resetMask=" + resetMask +
+ "\n curLp=" + curLp +
+ "\n newLp= null");
}
}
+ mCurrentLinkProperties[netType] = newLp;
+ updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
- if (doReset) {
+ if (doReset || resetMask != 0) {
LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
if (linkProperties != null) {
String iface = linkProperties.getInterfaceName();
if (TextUtils.isEmpty(iface) == false) {
- if (DBG) log("resetConnections(" + iface + ")");
- NetworkUtils.resetConnections(iface);
+ if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
+ NetworkUtils.resetConnections(iface, resetMask);
}
}
}
- }
-
- private void addPrivateDnsRoutes(NetworkStateTracker nt) {
- boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
- LinkProperties p = nt.getLinkProperties();
- if (p == null) return;
- String interfaceName = p.getInterfaceName();
- if (DBG) {
- log("addPrivateDnsRoutes for " + nt +
- "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
- }
- if (interfaceName != null && !privateDnsRouteSet) {
- Collection<InetAddress> dnsList = p.getDnses();
- for (InetAddress dns : dnsList) {
- addHostRoute(nt, dns, 0);
+ // TODO: Temporary notifying upstread change to Tethering.
+ // @see bug/4455071
+ /** Notify TetheringService if interface name has been changed. */
+ if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
+ Phone.REASON_LINK_PROPERTIES_CHANGED)) {
+ if (isTetheringSupported()) {
+ mTethering.handleTetherIfaceChange();
}
- nt.privateDnsRouteSet(true);
}
}
- private void removePrivateDnsRoutes(NetworkStateTracker nt) {
- LinkProperties p = nt.getLinkProperties();
- if (p == null) return;
- String interfaceName = p.getInterfaceName();
- boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
- if (interfaceName != null && privateDnsRouteSet) {
- if (DBG) {
- log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
- " (" + interfaceName + ")");
- }
-
- Collection<InetAddress> dnsList = p.getDnses();
- for (InetAddress dns : dnsList) {
- if (DBG) log(" removing " + dns);
- RouteInfo route = RouteInfo.makeHostRoute(dns);
- try {
- mNetd.removeRoute(interfaceName, route);
- } catch (Exception ex) {
- loge("error (" + ex + ") removing dns route " + route);
+ /**
+ * Add and remove routes using the old properties (null if not previously connected),
+ * new properties (null if becoming disconnected). May even be double null, which
+ * is a noop.
+ * Uses isLinkDefault to determine if default routes should be set or conversely if
+ * host routes should be set to the dns servers
+ */
+ private void updateRoutes(LinkProperties newLp, LinkProperties curLp, boolean isLinkDefault) {
+ Collection<RouteInfo> routesToAdd = null;
+ CompareResult<InetAddress> dnsDiff = null;
+
+ if (curLp != null) {
+ // check for the delta between the current set and the new
+ CompareResult<RouteInfo> routeDiff = curLp.compareRoutes(newLp);
+ dnsDiff = curLp.compareDnses(newLp);
+
+ for (RouteInfo r : routeDiff.removed) {
+ if (isLinkDefault || ! r.isDefaultRoute()) {
+ removeRoute(curLp, r);
}
}
- nt.privateDnsRouteSet(false);
+ routesToAdd = routeDiff.added;
}
- }
-
- private void addDefaultRoute(NetworkStateTracker nt) {
- LinkProperties p = nt.getLinkProperties();
- if (p == null) return;
- String interfaceName = p.getInterfaceName();
- if (TextUtils.isEmpty(interfaceName)) return;
+ if (newLp != null) {
+ // if we didn't get a diff from cur -> new, then just use the new
+ if (routesToAdd == null) {
+ routesToAdd = newLp.getRoutes();
+ }
- for (RouteInfo route : p.getRoutes()) {
- //TODO - handle non-default routes
- if (route.isDefaultRoute()) {
- if (DBG) log("adding default route " + route);
- InetAddress gateway = route.getGateway();
- if (addHostRoute(nt, gateway, 0)) {
- try {
- mNetd.addRoute(interfaceName, route);
- } catch (Exception e) {
- loge("error adding default route " + route);
- continue;
- }
- if (DBG) {
- NetworkInfo networkInfo = nt.getNetworkInfo();
- log("addDefaultRoute for " + networkInfo.getTypeName() +
- " (" + interfaceName + "), GatewayAddr=" +
- gateway.getHostAddress());
- }
- } else {
- loge("error adding host route for default route " + route);
+ for (RouteInfo r : routesToAdd) {
+ if (isLinkDefault || ! r.isDefaultRoute()) {
+ addRoute(newLp, r);
}
}
}
- }
-
-
- public void removeDefaultRoute(NetworkStateTracker nt) {
- LinkProperties p = nt.getLinkProperties();
- if (p == null) return;
- String interfaceName = p.getInterfaceName();
- if (interfaceName == null) return;
-
- for (RouteInfo route : p.getRoutes()) {
- //TODO - handle non-default routes
- if (route.isDefaultRoute()) {
- try {
- mNetd.removeRoute(interfaceName, route);
- } catch (Exception ex) {
- loge("error (" + ex + ") removing default route " + route);
- continue;
+ if (!isLinkDefault) {
+ // handle DNS routes
+ Collection<InetAddress> dnsToAdd = null;
+ if (dnsDiff != null) {
+ dnsToAdd = dnsDiff.added;
+ for (InetAddress dnsAddress : dnsDiff.removed) {
+ removeRoute(curLp, RouteInfo.makeHostRoute(dnsAddress));
}
- if (DBG) {
- NetworkInfo networkInfo = nt.getNetworkInfo();
- log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
- interfaceName + ")");
+ }
+ if (newLp != null) {
+ if (dnsToAdd == null) {
+ dnsToAdd = newLp.getDnses();
+ }
+ for(InetAddress dnsAddress : dnsToAdd) {
+ addRoute(newLp, RouteInfo.makeHostRoute(dnsAddress));
}
}
}
}
+
/**
* Reads the network specific TCP buffer sizes from SystemProperties
* net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
break;
case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
info = (NetworkInfo) msg.obj;
- handleConnectivityChange(info.getType(), true);
+ // TODO: Temporary allowing network configuration
+ // change not resetting sockets.
+ // @see bug/4455071
+ handleConnectivityChange(info.getType(), false);
break;
case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
String causedBy = null;
}
}
- private void handleTetherIfaceChange(int type) {
- String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
-
- if (isTetheringSupported()) {
- mTethering.handleTetherIfaceChange(iface);
- }
- }
-
private void log(String s) {
Slog.d(TAG, s);
}