Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 17 | package android.net.ip; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 18 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 19 | import static android.net.InetAddresses.parseNumericAddress; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 20 | import static android.net.RouteInfo.RTN_UNICAST; |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 21 | import static android.net.TetheringManager.TetheringRequest.checkStaticAddressConfiguration; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 22 | import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 23 | import static android.net.shared.Inet4AddressUtils.intToInet4AddressHTH; |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 24 | import static android.net.util.NetworkConstants.FF; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 25 | import static android.net.util.NetworkConstants.RFC7421_PREFIX_LENGTH; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 26 | import static android.net.util.NetworkConstants.asByte; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 27 | import static android.net.util.TetheringMessageBase.BASE_IPSERVER; |
Automerger Merge Worker | 3d330ff | 2020-03-16 04:06:46 +0000 | [diff] [blame] | 28 | import static android.system.OsConstants.RT_SCOPE_UNIVERSE; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 29 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 30 | import android.net.INetd; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 31 | import android.net.INetworkStackStatusCallback; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 32 | import android.net.IpPrefix; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 33 | import android.net.LinkAddress; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 34 | import android.net.LinkProperties; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 35 | import android.net.MacAddress; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 36 | import android.net.RouteInfo; |
Lorenzo Colitti | 0fb584b | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 37 | import android.net.TetherOffloadRuleParcel; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 38 | import android.net.TetheredClient; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 39 | import android.net.TetheringManager; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 40 | import android.net.TetheringRequestParcel; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 41 | import android.net.dhcp.DhcpLeaseParcelable; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 42 | import android.net.dhcp.DhcpServerCallbacks; |
| 43 | import android.net.dhcp.DhcpServingParamsParcel; |
| 44 | import android.net.dhcp.DhcpServingParamsParcelExt; |
Xiao Ma | 3eab492 | 2020-04-06 11:24:48 +0000 | [diff] [blame] | 45 | import android.net.dhcp.IDhcpEventCallbacks; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 46 | import android.net.dhcp.IDhcpServer; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 47 | import android.net.ip.IpNeighborMonitor.NeighborEvent; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 48 | import android.net.ip.RouterAdvertisementDaemon.RaParams; |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 49 | import android.net.shared.NetdUtils; |
| 50 | import android.net.shared.RouteUtils; |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 51 | import android.net.util.InterfaceParams; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 52 | import android.net.util.InterfaceSet; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 53 | import android.net.util.SharedLog; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 54 | import android.os.Handler; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 55 | import android.os.Looper; |
| 56 | import android.os.Message; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 57 | import android.os.RemoteException; |
| 58 | import android.os.ServiceSpecificException; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 59 | import android.util.Log; |
| 60 | import android.util.SparseArray; |
| 61 | |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 62 | import androidx.annotation.NonNull; |
| 63 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 64 | import com.android.internal.util.MessageUtils; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 65 | import com.android.internal.util.State; |
| 66 | import com.android.internal.util.StateMachine; |
| 67 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 68 | import java.io.IOException; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 69 | import java.net.Inet4Address; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 70 | import java.net.Inet6Address; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 71 | import java.net.InetAddress; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 72 | import java.net.NetworkInterface; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 73 | import java.net.UnknownHostException; |
| 74 | import java.util.ArrayList; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 75 | import java.util.Arrays; |
| 76 | import java.util.Collections; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 77 | import java.util.HashSet; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 78 | import java.util.LinkedHashMap; |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 79 | import java.util.List; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 80 | import java.util.Objects; |
| 81 | import java.util.Random; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 82 | import java.util.Set; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 85 | * Provides the interface to IP-layer serving functionality for a given network |
| 86 | * interface, e.g. for tethering or "local-only hotspot" mode. |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 87 | * |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 88 | * @hide |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 89 | */ |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 90 | public class IpServer extends StateMachine { |
| 91 | public static final int STATE_UNAVAILABLE = 0; |
| 92 | public static final int STATE_AVAILABLE = 1; |
| 93 | public static final int STATE_TETHERED = 2; |
| 94 | public static final int STATE_LOCAL_ONLY = 3; |
| 95 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 96 | /** Get string name of |state|.*/ |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 97 | public static String getStateString(int state) { |
| 98 | switch (state) { |
| 99 | case STATE_UNAVAILABLE: return "UNAVAILABLE"; |
| 100 | case STATE_AVAILABLE: return "AVAILABLE"; |
| 101 | case STATE_TETHERED: return "TETHERED"; |
| 102 | case STATE_LOCAL_ONLY: return "LOCAL_ONLY"; |
| 103 | } |
| 104 | return "UNKNOWN: " + state; |
| 105 | } |
| 106 | |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 107 | private static final byte DOUG_ADAMS = (byte) 42; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 108 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 109 | private static final String USB_NEAR_IFACE_ADDR = "192.168.42.129"; |
| 110 | private static final int USB_PREFIX_LENGTH = 24; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 111 | private static final String WIFI_HOST_IFACE_ADDR = "192.168.43.1"; |
| 112 | private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 113 | private static final String WIFI_P2P_IFACE_ADDR = "192.168.49.1"; |
| 114 | private static final int WIFI_P2P_IFACE_PREFIX_LENGTH = 24; |
Remi NGUYEN VAN | f2f3f3e | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 115 | private static final String ETHERNET_IFACE_ADDR = "192.168.50.1"; |
| 116 | private static final int ETHERNET_IFACE_PREFIX_LENGTH = 24; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 117 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 118 | // TODO: have PanService use some visible version of this constant |
| 119 | private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1"; |
| 120 | private static final int BLUETOOTH_DHCP_PREFIX_LENGTH = 24; |
| 121 | |
| 122 | // TODO: have this configurable |
| 123 | private static final int DHCP_LEASE_TIME_SECS = 3600; |
| 124 | |
Lorenzo Colitti | 8bc1e54 | 2020-04-14 09:21:19 +0000 | [diff] [blame] | 125 | private static final MacAddress NULL_MAC_ADDRESS = MacAddress.fromString("00:00:00:00:00:00"); |
| 126 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 127 | private static final String TAG = "IpServer"; |
| 128 | private static final boolean DBG = false; |
| 129 | private static final boolean VDBG = false; |
| 130 | private static final Class[] sMessageClasses = { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 131 | IpServer.class |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 132 | }; |
| 133 | private static final SparseArray<String> sMagicDecoderRing = |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 134 | MessageUtils.findMessageNames(sMessageClasses); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 135 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 136 | /** IpServer callback. */ |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 137 | public static class Callback { |
| 138 | /** |
| 139 | * Notify that |who| has changed its tethering state. |
| 140 | * |
| 141 | * @param who the calling instance of IpServer |
| 142 | * @param state one of STATE_* |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 143 | * @param lastError one of TetheringManager.TETHER_ERROR_* |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 144 | */ |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 145 | public void updateInterfaceState(IpServer who, int state, int lastError) { } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Notify that |who| has new LinkProperties. |
| 149 | * |
| 150 | * @param who the calling instance of IpServer |
| 151 | * @param newLp the new LinkProperties to report |
| 152 | */ |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 153 | public void updateLinkProperties(IpServer who, LinkProperties newLp) { } |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Notify that the DHCP leases changed in one of the IpServers. |
| 157 | */ |
| 158 | public void dhcpLeasesChanged() { } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 159 | } |
| 160 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 161 | /** Capture IpServer dependencies, for injection. */ |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 162 | public abstract static class Dependencies { |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 163 | /** Create an IpNeighborMonitor to be used by this IpServer */ |
| 164 | public IpNeighborMonitor getIpNeighborMonitor(Handler handler, SharedLog log, |
| 165 | IpNeighborMonitor.NeighborEventConsumer consumer) { |
| 166 | return new IpNeighborMonitor(handler, log, consumer); |
| 167 | } |
| 168 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 169 | /** Create a RouterAdvertisementDaemon instance to be used by IpServer.*/ |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 170 | public RouterAdvertisementDaemon getRouterAdvertisementDaemon(InterfaceParams ifParams) { |
| 171 | return new RouterAdvertisementDaemon(ifParams); |
| 172 | } |
| 173 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 174 | /** Get |ifName|'s interface information.*/ |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 175 | public InterfaceParams getInterfaceParams(String ifName) { |
| 176 | return InterfaceParams.getByName(ifName); |
| 177 | } |
| 178 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 179 | /** Get |ifName|'s interface index. */ |
| 180 | public int getIfindex(String ifName) { |
| 181 | try { |
| 182 | return NetworkInterface.getByName(ifName).getIndex(); |
| 183 | } catch (IOException | NullPointerException e) { |
| 184 | Log.e(TAG, "Can't determine interface index for interface " + ifName); |
| 185 | return 0; |
| 186 | } |
| 187 | } |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 188 | /** Create a DhcpServer instance to be used by IpServer. */ |
| 189 | public abstract void makeDhcpServer(String ifName, DhcpServingParamsParcel params, |
| 190 | DhcpServerCallbacks cb); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 193 | // request from the user that it wants to tether |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 194 | public static final int CMD_TETHER_REQUESTED = BASE_IPSERVER + 1; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 195 | // request from the user that it wants to untether |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 196 | public static final int CMD_TETHER_UNREQUESTED = BASE_IPSERVER + 2; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 197 | // notification that this interface is down |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 198 | public static final int CMD_INTERFACE_DOWN = BASE_IPSERVER + 3; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 199 | // notification from the master SM that it had trouble enabling IP Forwarding |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 200 | public static final int CMD_IP_FORWARDING_ENABLE_ERROR = BASE_IPSERVER + 4; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 201 | // notification from the master SM that it had trouble disabling IP Forwarding |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 202 | public static final int CMD_IP_FORWARDING_DISABLE_ERROR = BASE_IPSERVER + 5; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 203 | // notification from the master SM that it had trouble starting tethering |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 204 | public static final int CMD_START_TETHERING_ERROR = BASE_IPSERVER + 6; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 205 | // notification from the master SM that it had trouble stopping tethering |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 206 | public static final int CMD_STOP_TETHERING_ERROR = BASE_IPSERVER + 7; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 207 | // notification from the master SM that it had trouble setting the DNS forwarders |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 208 | public static final int CMD_SET_DNS_FORWARDERS_ERROR = BASE_IPSERVER + 8; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 209 | // the upstream connection has changed |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 210 | public static final int CMD_TETHER_CONNECTION_CHANGED = BASE_IPSERVER + 9; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 211 | // new IPv6 tethering parameters need to be processed |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 212 | public static final int CMD_IPV6_TETHER_UPDATE = BASE_IPSERVER + 10; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 213 | // new neighbor cache entry on our interface |
| 214 | public static final int CMD_NEIGHBOR_EVENT = BASE_IPSERVER + 11; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 215 | |
| 216 | private final State mInitialState; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 217 | private final State mLocalHotspotState; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 218 | private final State mTetheredState; |
| 219 | private final State mUnavailableState; |
| 220 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 221 | private final SharedLog mLog; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 222 | private final INetd mNetd; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 223 | private final Callback mCallback; |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 224 | private final InterfaceController mInterfaceCtrl; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 225 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 226 | private final String mIfaceName; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 227 | private final int mInterfaceType; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 228 | private final LinkProperties mLinkProperties; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 229 | private final boolean mUsingLegacyDhcp; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 230 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 231 | private final Dependencies mDeps; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 232 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 233 | private int mLastError; |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 234 | private int mServingMode; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 235 | private InterfaceSet mUpstreamIfaceSet; // may change over time |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 236 | private InterfaceParams mInterfaceParams; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 237 | // TODO: De-duplicate this with mLinkProperties above. Currently, these link |
| 238 | // properties are those selected by the IPv6TetheringCoordinator and relayed |
| 239 | // to us. By comparison, mLinkProperties contains the addresses and directly |
| 240 | // connected routes that have been formed from these properties iff. we have |
| 241 | // succeeded in configuring them and are able to announce them within Router |
| 242 | // Advertisements (otherwise, we do not add them to mLinkProperties at all). |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 243 | private LinkProperties mLastIPv6LinkProperties; |
| 244 | private RouterAdvertisementDaemon mRaDaemon; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 245 | |
| 246 | // To be accessed only on the handler thread |
| 247 | private int mDhcpServerStartIndex = 0; |
| 248 | private IDhcpServer mDhcpServer; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 249 | private RaParams mLastRaParams; |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 250 | private LinkAddress mIpv4Address; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 251 | |
| 252 | private LinkAddress mStaticIpv4ServerAddr; |
| 253 | private LinkAddress mStaticIpv4ClientAddr; |
| 254 | |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 255 | @NonNull |
| 256 | private List<TetheredClient> mDhcpLeases = Collections.emptyList(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 257 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 258 | private int mLastIPv6UpstreamIfindex = 0; |
| 259 | |
| 260 | private class MyNeighborEventConsumer implements IpNeighborMonitor.NeighborEventConsumer { |
| 261 | public void accept(NeighborEvent e) { |
| 262 | sendMessage(CMD_NEIGHBOR_EVENT, e); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static class Ipv6ForwardingRule { |
| 267 | public final int upstreamIfindex; |
| 268 | public final int downstreamIfindex; |
| 269 | public final Inet6Address address; |
| 270 | public final MacAddress srcMac; |
| 271 | public final MacAddress dstMac; |
| 272 | |
| 273 | Ipv6ForwardingRule(int upstreamIfindex, int downstreamIfIndex, Inet6Address address, |
| 274 | MacAddress srcMac, MacAddress dstMac) { |
| 275 | this.upstreamIfindex = upstreamIfindex; |
| 276 | this.downstreamIfindex = downstreamIfIndex; |
| 277 | this.address = address; |
| 278 | this.srcMac = srcMac; |
| 279 | this.dstMac = dstMac; |
| 280 | } |
| 281 | |
| 282 | public Ipv6ForwardingRule onNewUpstream(int newUpstreamIfindex) { |
| 283 | return new Ipv6ForwardingRule(newUpstreamIfindex, downstreamIfindex, address, srcMac, |
| 284 | dstMac); |
| 285 | } |
Lorenzo Colitti | 0fb584b | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 286 | |
| 287 | // Don't manipulate TetherOffloadRuleParcel directly because implementing onNewUpstream() |
| 288 | // would be error-prone due to generated stable AIDL classes not having a copy constructor. |
| 289 | public TetherOffloadRuleParcel toTetherOffloadRuleParcel() { |
| 290 | final TetherOffloadRuleParcel parcel = new TetherOffloadRuleParcel(); |
| 291 | parcel.inputInterfaceIndex = upstreamIfindex; |
| 292 | parcel.outputInterfaceIndex = downstreamIfindex; |
| 293 | parcel.destination = address.getAddress(); |
| 294 | parcel.prefixLength = 128; |
| 295 | parcel.srcL2Address = srcMac.toByteArray(); |
| 296 | parcel.dstL2Address = dstMac.toByteArray(); |
| 297 | return parcel; |
| 298 | } |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 299 | } |
| 300 | private final LinkedHashMap<Inet6Address, Ipv6ForwardingRule> mIpv6ForwardingRules = |
| 301 | new LinkedHashMap<>(); |
| 302 | |
| 303 | private final IpNeighborMonitor mIpNeighborMonitor; |
| 304 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 305 | public IpServer( |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 306 | String ifaceName, Looper looper, int interfaceType, SharedLog log, |
junyulai | e3f4143 | 2019-12-03 14:34:13 +0800 | [diff] [blame] | 307 | INetd netd, Callback callback, boolean usingLegacyDhcp, Dependencies deps) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 308 | super(ifaceName, looper); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 309 | mLog = log.forSubComponent(ifaceName); |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 310 | mNetd = netd; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 311 | mCallback = callback; |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 312 | mInterfaceCtrl = new InterfaceController(ifaceName, mNetd, mLog); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 313 | mIfaceName = ifaceName; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 314 | mInterfaceType = interfaceType; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 315 | mLinkProperties = new LinkProperties(); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 316 | mUsingLegacyDhcp = usingLegacyDhcp; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 317 | mDeps = deps; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 318 | resetLinkProperties(); |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 319 | mLastError = TetheringManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 320 | mServingMode = STATE_AVAILABLE; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 321 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 322 | mIpNeighborMonitor = mDeps.getIpNeighborMonitor(getHandler(), mLog, |
| 323 | new MyNeighborEventConsumer()); |
| 324 | if (!mIpNeighborMonitor.start()) { |
| 325 | mLog.e("Failed to create IpNeighborMonitor on " + mIfaceName); |
| 326 | } |
| 327 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 328 | mInitialState = new InitialState(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 329 | mLocalHotspotState = new LocalHotspotState(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 330 | mTetheredState = new TetheredState(); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 331 | mUnavailableState = new UnavailableState(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 332 | addState(mInitialState); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 333 | addState(mLocalHotspotState); |
| 334 | addState(mTetheredState); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 335 | addState(mUnavailableState); |
| 336 | |
| 337 | setInitialState(mInitialState); |
| 338 | } |
| 339 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 340 | /** Interface name which IpServer served.*/ |
| 341 | public String interfaceName() { |
| 342 | return mIfaceName; |
| 343 | } |
Erik Kline | 218c226 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 344 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 345 | /** |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 346 | * Tethering downstream type. It would be one of TetheringManager#TETHERING_*. |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 347 | */ |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 348 | public int interfaceType() { |
| 349 | return mInterfaceType; |
| 350 | } |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 351 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 352 | /** Last error from this IpServer. */ |
| 353 | public int lastError() { |
| 354 | return mLastError; |
| 355 | } |
| 356 | |
| 357 | /** Serving mode is the current state of IpServer state machine. */ |
| 358 | public int servingMode() { |
| 359 | return mServingMode; |
| 360 | } |
| 361 | |
| 362 | /** The properties of the network link which IpServer is serving. */ |
| 363 | public LinkProperties linkProperties() { |
| 364 | return new LinkProperties(mLinkProperties); |
| 365 | } |
| 366 | |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 367 | /** |
| 368 | * Get the latest list of DHCP leases that was reported. Must be called on the IpServer looper |
| 369 | * thread. |
| 370 | */ |
| 371 | public List<TetheredClient> getAllLeases() { |
| 372 | return Collections.unmodifiableList(mDhcpLeases); |
| 373 | } |
| 374 | |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 375 | /** Stop this IpServer. After this is called this IpServer should not be used any more. */ |
| 376 | public void stop() { |
| 377 | sendMessage(CMD_INTERFACE_DOWN); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Tethering is canceled. IpServer state machine will be available and wait for |
| 382 | * next tethering request. |
| 383 | */ |
| 384 | public void unwanted() { |
| 385 | sendMessage(CMD_TETHER_UNREQUESTED); |
| 386 | } |
| 387 | |
| 388 | /** Internals. */ |
| 389 | |
| 390 | private boolean startIPv4() { |
| 391 | return configureIPv4(true); |
| 392 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 393 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 394 | /** |
| 395 | * Convenience wrapper around INetworkStackStatusCallback to run callbacks on the IpServer |
| 396 | * handler. |
| 397 | * |
| 398 | * <p>Different instances of this class can be created for each call to IDhcpServer methods, |
| 399 | * with different implementations of the callback, to differentiate handling of success/error in |
| 400 | * each call. |
| 401 | */ |
| 402 | private abstract class OnHandlerStatusCallback extends INetworkStackStatusCallback.Stub { |
| 403 | @Override |
| 404 | public void onStatusAvailable(int statusCode) { |
| 405 | getHandler().post(() -> callback(statusCode)); |
| 406 | } |
| 407 | |
| 408 | public abstract void callback(int statusCode); |
Remi NGUYEN VAN | 1232fe3 | 2019-04-05 15:15:48 +0900 | [diff] [blame] | 409 | |
| 410 | @Override |
| 411 | public int getInterfaceVersion() { |
| 412 | return this.VERSION; |
| 413 | } |
Paul Trautrim | e01ea25 | 2020-01-23 14:55:57 +0900 | [diff] [blame] | 414 | |
| 415 | @Override |
| 416 | public String getInterfaceHash() { |
| 417 | return this.HASH; |
| 418 | } |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | private class DhcpServerCallbacksImpl extends DhcpServerCallbacks { |
| 422 | private final int mStartIndex; |
| 423 | |
| 424 | private DhcpServerCallbacksImpl(int startIndex) { |
| 425 | mStartIndex = startIndex; |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void onDhcpServerCreated(int statusCode, IDhcpServer server) throws RemoteException { |
| 430 | getHandler().post(() -> { |
| 431 | // We are on the handler thread: mDhcpServerStartIndex can be read safely. |
| 432 | if (mStartIndex != mDhcpServerStartIndex) { |
| 433 | // This start request is obsolete. When the |server| binder token goes out of |
| 434 | // scope, the garbage collector will finalize it, which causes the network stack |
| 435 | // process garbage collector to collect the server itself. |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | if (statusCode != STATUS_SUCCESS) { |
| 440 | mLog.e("Error obtaining DHCP server: " + statusCode); |
| 441 | handleError(); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | mDhcpServer = server; |
| 446 | try { |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 447 | mDhcpServer.startWithCallbacks(new OnHandlerStatusCallback() { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 448 | @Override |
| 449 | public void callback(int startStatusCode) { |
| 450 | if (startStatusCode != STATUS_SUCCESS) { |
| 451 | mLog.e("Error starting DHCP server: " + startStatusCode); |
| 452 | handleError(); |
| 453 | } |
| 454 | } |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 455 | }, new DhcpLeaseCallback()); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 456 | } catch (RemoteException e) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 457 | throw new IllegalStateException(e); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 458 | } |
| 459 | }); |
| 460 | } |
| 461 | |
| 462 | private void handleError() { |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 463 | mLastError = TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 464 | transitionTo(mInitialState); |
| 465 | } |
| 466 | } |
| 467 | |
Xiao Ma | 3eab492 | 2020-04-06 11:24:48 +0000 | [diff] [blame] | 468 | private class DhcpLeaseCallback extends IDhcpEventCallbacks.Stub { |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 469 | @Override |
| 470 | public void onLeasesChanged(List<DhcpLeaseParcelable> leaseParcelables) { |
| 471 | final ArrayList<TetheredClient> leases = new ArrayList<>(); |
| 472 | for (DhcpLeaseParcelable lease : leaseParcelables) { |
| 473 | final LinkAddress address = new LinkAddress( |
Automerger Merge Worker | 3d330ff | 2020-03-16 04:06:46 +0000 | [diff] [blame] | 474 | intToInet4AddressHTH(lease.netAddr), lease.prefixLength, |
| 475 | 0 /* flags */, RT_SCOPE_UNIVERSE /* as per RFC6724#3.2 */, |
| 476 | lease.expTime /* deprecationTime */, lease.expTime /* expirationTime */); |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 477 | |
| 478 | final MacAddress macAddress; |
| 479 | try { |
| 480 | macAddress = MacAddress.fromBytes(lease.hwAddr); |
| 481 | } catch (IllegalArgumentException e) { |
| 482 | Log.wtf(TAG, "Invalid address received from DhcpServer: " |
| 483 | + Arrays.toString(lease.hwAddr)); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo( |
Automerger Merge Worker | 3d330ff | 2020-03-16 04:06:46 +0000 | [diff] [blame] | 488 | address, lease.hostname); |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 489 | leases.add(new TetheredClient( |
| 490 | macAddress, |
| 491 | Collections.singletonList(addressInfo), |
| 492 | mInterfaceType)); |
| 493 | } |
| 494 | |
| 495 | getHandler().post(() -> { |
| 496 | mDhcpLeases = leases; |
| 497 | mCallback.dhcpLeasesChanged(); |
| 498 | }); |
| 499 | } |
| 500 | |
| 501 | @Override |
Xiao Ma | 3eab492 | 2020-04-06 11:24:48 +0000 | [diff] [blame] | 502 | public void onNewPrefixRequest(IpPrefix currentPrefix) { |
| 503 | //TODO: add specific implementation. |
| 504 | } |
| 505 | |
| 506 | @Override |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 507 | public int getInterfaceVersion() { |
| 508 | return this.VERSION; |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public String getInterfaceHash() throws RemoteException { |
| 513 | return this.HASH; |
| 514 | } |
| 515 | } |
| 516 | |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 517 | private boolean startDhcp(final LinkAddress serverLinkAddr, final LinkAddress clientLinkAddr) { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 518 | if (mUsingLegacyDhcp) { |
| 519 | return true; |
| 520 | } |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 521 | |
| 522 | final Inet4Address addr = (Inet4Address) serverLinkAddr.getAddress(); |
| 523 | final int prefixLen = serverLinkAddr.getPrefixLength(); |
| 524 | final Inet4Address clientAddr = clientLinkAddr == null ? null : |
| 525 | (Inet4Address) clientLinkAddr.getAddress(); |
| 526 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 527 | final DhcpServingParamsParcel params; |
| 528 | params = new DhcpServingParamsParcelExt() |
| 529 | .setDefaultRouters(addr) |
| 530 | .setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS) |
| 531 | .setDnsServers(addr) |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 532 | .setServerAddr(serverLinkAddr) |
| 533 | .setMetered(true) |
| 534 | .setSingleClientAddr(clientAddr); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 535 | // TODO: also advertise link MTU |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 536 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 537 | mDhcpServerStartIndex++; |
| 538 | mDeps.makeDhcpServer( |
| 539 | mIfaceName, params, new DhcpServerCallbacksImpl(mDhcpServerStartIndex)); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
| 543 | private void stopDhcp() { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 544 | // Make all previous start requests obsolete so servers are not started later |
| 545 | mDhcpServerStartIndex++; |
| 546 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 547 | if (mDhcpServer != null) { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 548 | try { |
| 549 | mDhcpServer.stop(new OnHandlerStatusCallback() { |
| 550 | @Override |
| 551 | public void callback(int statusCode) { |
| 552 | if (statusCode != STATUS_SUCCESS) { |
| 553 | mLog.e("Error stopping DHCP server: " + statusCode); |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 554 | mLastError = TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 555 | // Not much more we can do here |
| 556 | } |
Remi NGUYEN VAN | db5594d | 2020-02-13 09:16:19 +0900 | [diff] [blame] | 557 | mDhcpLeases.clear(); |
| 558 | getHandler().post(mCallback::dhcpLeasesChanged); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 559 | } |
| 560 | }); |
| 561 | mDhcpServer = null; |
| 562 | } catch (RemoteException e) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 563 | mLog.e("Error stopping DHCP", e); |
| 564 | // Not much more we can do here |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 565 | } |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 569 | private boolean configureDhcp(boolean enable, final LinkAddress serverAddr, |
| 570 | final LinkAddress clientAddr) { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 571 | if (enable) { |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 572 | return startDhcp(serverAddr, clientAddr); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 573 | } else { |
| 574 | stopDhcp(); |
| 575 | return true; |
| 576 | } |
| 577 | } |
| 578 | |
Erik Kline | f8bba5b | 2018-05-18 16:09:24 +0900 | [diff] [blame] | 579 | private void stopIPv4() { |
| 580 | configureIPv4(false); |
| 581 | // NOTE: All of configureIPv4() will be refactored out of existence |
| 582 | // into calls to InterfaceController, shared with startIPv4(). |
| 583 | mInterfaceCtrl.clearIPv4Address(); |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 584 | mIpv4Address = null; |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 585 | mStaticIpv4ServerAddr = null; |
| 586 | mStaticIpv4ClientAddr = null; |
Erik Kline | f8bba5b | 2018-05-18 16:09:24 +0900 | [diff] [blame] | 587 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 588 | |
| 589 | private boolean configureIPv4(boolean enabled) { |
| 590 | if (VDBG) Log.d(TAG, "configureIPv4(" + enabled + ")"); |
| 591 | |
| 592 | // TODO: Replace this hard-coded information with dynamically selected |
| 593 | // config passed down to us by a higher layer IP-coordinating element. |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 594 | final Inet4Address srvAddr; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 595 | int prefixLen = 0; |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 596 | try { |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 597 | if (mStaticIpv4ServerAddr != null) { |
| 598 | srvAddr = (Inet4Address) mStaticIpv4ServerAddr.getAddress(); |
| 599 | prefixLen = mStaticIpv4ServerAddr.getPrefixLength(); |
| 600 | } else if (mInterfaceType == TetheringManager.TETHERING_USB |
Milim Lee | 58527ba | 2019-10-17 05:02:33 +0900 | [diff] [blame] | 601 | || mInterfaceType == TetheringManager.TETHERING_NCM) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 602 | srvAddr = (Inet4Address) parseNumericAddress(USB_NEAR_IFACE_ADDR); |
| 603 | prefixLen = USB_PREFIX_LENGTH; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 604 | } else if (mInterfaceType == TetheringManager.TETHERING_WIFI) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 605 | srvAddr = (Inet4Address) parseNumericAddress(getRandomWifiIPv4Address()); |
| 606 | prefixLen = WIFI_HOST_IFACE_PREFIX_LENGTH; |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 607 | } else if (mInterfaceType == TetheringManager.TETHERING_WIFI_P2P) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 608 | srvAddr = (Inet4Address) parseNumericAddress(WIFI_P2P_IFACE_ADDR); |
| 609 | prefixLen = WIFI_P2P_IFACE_PREFIX_LENGTH; |
Remi NGUYEN VAN | f2f3f3e | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 610 | } else if (mInterfaceType == TetheringManager.TETHERING_ETHERNET) { |
| 611 | // TODO: randomize address for tethering too, similarly to wifi |
| 612 | srvAddr = (Inet4Address) parseNumericAddress(ETHERNET_IFACE_ADDR); |
| 613 | prefixLen = ETHERNET_IFACE_PREFIX_LENGTH; |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 614 | } else { |
| 615 | // BT configures the interface elsewhere: only start DHCP. |
| 616 | // TODO: make all tethering types behave the same way, and delete the bluetooth |
| 617 | // code that calls into NetworkManagementService directly. |
| 618 | srvAddr = (Inet4Address) parseNumericAddress(BLUETOOTH_IFACE_ADDR); |
| 619 | mIpv4Address = new LinkAddress(srvAddr, BLUETOOTH_DHCP_PREFIX_LENGTH); |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 620 | return configureDhcp(enabled, mIpv4Address, null /* clientAddress */); |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 621 | } |
| 622 | mIpv4Address = new LinkAddress(srvAddr, prefixLen); |
| 623 | } catch (IllegalArgumentException e) { |
| 624 | mLog.e("Error selecting ipv4 address", e); |
| 625 | if (!enabled) stopDhcp(); |
| 626 | return false; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 627 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 628 | |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 629 | final Boolean setIfaceUp; |
Jimmy Chen | 151384d | 2019-12-03 11:37:09 +0800 | [diff] [blame] | 630 | if (mInterfaceType == TetheringManager.TETHERING_WIFI |
| 631 | || mInterfaceType == TetheringManager.TETHERING_WIFI_P2P) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 632 | // The WiFi stack has ownership of the interface up/down state. |
| 633 | // It is unclear whether the Bluetooth or USB stacks will manage their own |
| 634 | // state. |
| 635 | setIfaceUp = null; |
| 636 | } else { |
| 637 | setIfaceUp = enabled; |
| 638 | } |
| 639 | if (!mInterfaceCtrl.setInterfaceConfiguration(mIpv4Address, setIfaceUp)) { |
| 640 | mLog.e("Error configuring interface"); |
| 641 | if (!enabled) stopDhcp(); |
| 642 | return false; |
| 643 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 644 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 645 | // Directly-connected route. |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 646 | final IpPrefix ipv4Prefix = new IpPrefix(mIpv4Address.getAddress(), |
| 647 | mIpv4Address.getPrefixLength()); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 648 | final RouteInfo route = new RouteInfo(ipv4Prefix, null, null, RTN_UNICAST); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 649 | if (enabled) { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 650 | mLinkProperties.addLinkAddress(mIpv4Address); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 651 | mLinkProperties.addRoute(route); |
| 652 | } else { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 653 | mLinkProperties.removeLinkAddress(mIpv4Address); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 654 | mLinkProperties.removeRoute(route); |
| 655 | } |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 656 | |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 657 | return configureDhcp(enabled, mIpv4Address, mStaticIpv4ClientAddr); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 660 | private String getRandomWifiIPv4Address() { |
| 661 | try { |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 662 | byte[] bytes = parseNumericAddress(WIFI_HOST_IFACE_ADDR).getAddress(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 663 | bytes[3] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1), FF); |
| 664 | return InetAddress.getByAddress(bytes).getHostAddress(); |
| 665 | } catch (Exception e) { |
| 666 | return WIFI_HOST_IFACE_ADDR; |
| 667 | } |
| 668 | } |
| 669 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 670 | private boolean startIPv6() { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 671 | mInterfaceParams = mDeps.getInterfaceParams(mIfaceName); |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 672 | if (mInterfaceParams == null) { |
| 673 | mLog.e("Failed to find InterfaceParams"); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 674 | stopIPv6(); |
| 675 | return false; |
| 676 | } |
| 677 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 678 | mRaDaemon = mDeps.getRouterAdvertisementDaemon(mInterfaceParams); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 679 | if (!mRaDaemon.start()) { |
| 680 | stopIPv6(); |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | private void stopIPv6() { |
Erik Kline | 8bd00d5 | 2017-12-08 17:47:50 +0900 | [diff] [blame] | 688 | mInterfaceParams = null; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 689 | setRaParams(null); |
| 690 | |
| 691 | if (mRaDaemon != null) { |
| 692 | mRaDaemon.stop(); |
| 693 | mRaDaemon = null; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | // IPv6TetheringCoordinator sends updates with carefully curated IPv6-only |
| 698 | // LinkProperties. These have extraneous data filtered out and only the |
| 699 | // necessary prefixes included (per its prefix distribution policy). |
| 700 | // |
| 701 | // TODO: Evaluate using a data structure than is more directly suited to |
| 702 | // communicating only the relevant information. |
| 703 | private void updateUpstreamIPv6LinkProperties(LinkProperties v6only) { |
| 704 | if (mRaDaemon == null) return; |
| 705 | |
| 706 | // Avoid unnecessary work on spurious updates. |
| 707 | if (Objects.equals(mLastIPv6LinkProperties, v6only)) { |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | RaParams params = null; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 712 | int upstreamIfindex = 0; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 713 | |
| 714 | if (v6only != null) { |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 715 | final String upstreamIface = v6only.getInterfaceName(); |
| 716 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 717 | params = new RaParams(); |
Maciej Żenczykowski | 34d4361 | 2020-02-19 01:24:39 -0800 | [diff] [blame] | 718 | // We advertise an mtu lower by 16, which is the closest multiple of 8 >= 14, |
| 719 | // the ethernet header size. This makes kernel ebpf tethering offload happy. |
| 720 | // This hack should be reverted once we have the kernel fixed up. |
| 721 | // Note: this will automatically clamp to at least 1280 (ipv6 minimum mtu) |
| 722 | // see RouterAdvertisementDaemon.java putMtu() |
| 723 | params.mtu = v6only.getMtu() - 16; |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 724 | params.hasDefaultRoute = v6only.hasIpv6DefaultRoute(); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 725 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 726 | if (params.hasDefaultRoute) params.hopLimit = getHopLimit(upstreamIface); |
Erik Kline | 8e492f8 | 2018-09-14 16:25:54 +0900 | [diff] [blame] | 727 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 728 | for (LinkAddress linkAddr : v6only.getLinkAddresses()) { |
| 729 | if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue; |
| 730 | |
| 731 | final IpPrefix prefix = new IpPrefix( |
| 732 | linkAddr.getAddress(), linkAddr.getPrefixLength()); |
| 733 | params.prefixes.add(prefix); |
| 734 | |
| 735 | final Inet6Address dnsServer = getLocalDnsIpFor(prefix); |
| 736 | if (dnsServer != null) { |
| 737 | params.dnses.add(dnsServer); |
| 738 | } |
| 739 | } |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 740 | |
| 741 | upstreamIfindex = mDeps.getIfindex(upstreamIface); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 742 | } |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 743 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 744 | // If v6only is null, we pass in null to setRaParams(), which handles |
| 745 | // deprecation of any existing RA data. |
| 746 | |
| 747 | setRaParams(params); |
| 748 | mLastIPv6LinkProperties = v6only; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 749 | |
| 750 | updateIpv6ForwardingRules(mLastIPv6UpstreamIfindex, upstreamIfindex, null); |
| 751 | mLastIPv6UpstreamIfindex = upstreamIfindex; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 752 | } |
| 753 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 754 | private void configureLocalIPv6Routes( |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 755 | HashSet<IpPrefix> deprecatedPrefixes, HashSet<IpPrefix> newPrefixes) { |
| 756 | // [1] Remove the routes that are deprecated. |
| 757 | if (!deprecatedPrefixes.isEmpty()) { |
| 758 | final ArrayList<RouteInfo> toBeRemoved = |
| 759 | getLocalRoutesFor(mIfaceName, deprecatedPrefixes); |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 760 | // Remove routes from local network. |
| 761 | final int removalFailures = RouteUtils.removeRoutesFromLocalNetwork( |
| 762 | mNetd, toBeRemoved); |
| 763 | if (removalFailures > 0) { |
| 764 | mLog.e(String.format("Failed to remove %d IPv6 routes from local table.", |
| 765 | removalFailures)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 766 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 767 | |
| 768 | for (RouteInfo route : toBeRemoved) mLinkProperties.removeRoute(route); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | // [2] Add only the routes that have not previously been added. |
| 772 | if (newPrefixes != null && !newPrefixes.isEmpty()) { |
| 773 | HashSet<IpPrefix> addedPrefixes = (HashSet) newPrefixes.clone(); |
| 774 | if (mLastRaParams != null) { |
| 775 | addedPrefixes.removeAll(mLastRaParams.prefixes); |
| 776 | } |
| 777 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 778 | if (!addedPrefixes.isEmpty()) { |
| 779 | final ArrayList<RouteInfo> toBeAdded = |
| 780 | getLocalRoutesFor(mIfaceName, addedPrefixes); |
| 781 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 782 | // It's safe to call networkAddInterface() even if |
| 783 | // the interface is already in the local_network. |
| 784 | mNetd.networkAddInterface(INetd.LOCAL_NET_ID, mIfaceName); |
| 785 | try { |
| 786 | // Add routes from local network. Note that adding routes that |
| 787 | // already exist does not cause an error (EEXIST is silently ignored). |
| 788 | RouteUtils.addRoutesToLocalNetwork(mNetd, mIfaceName, toBeAdded); |
| 789 | } catch (IllegalStateException e) { |
| 790 | mLog.e("Failed to add IPv6 routes to local table: " + e); |
| 791 | } |
| 792 | } catch (ServiceSpecificException | RemoteException e) { |
| 793 | mLog.e("Failed to add " + mIfaceName + " to local table: ", e); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 794 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 795 | |
| 796 | for (RouteInfo route : toBeAdded) mLinkProperties.addRoute(route); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 801 | private void configureLocalIPv6Dns( |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 802 | HashSet<Inet6Address> deprecatedDnses, HashSet<Inet6Address> newDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 803 | // TODO: Is this really necessary? Can we not fail earlier if INetd cannot be located? |
| 804 | if (mNetd == null) { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 805 | if (newDnses != null) newDnses.clear(); |
| 806 | mLog.e("No netd service instance available; not setting local IPv6 addresses"); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | // [1] Remove deprecated local DNS IP addresses. |
| 811 | if (!deprecatedDnses.isEmpty()) { |
| 812 | for (Inet6Address dns : deprecatedDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 813 | if (!mInterfaceCtrl.removeAddress(dns, RFC7421_PREFIX_LENGTH)) { |
| 814 | mLog.e("Failed to remove local dns IP " + dns); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 815 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 816 | |
| 817 | mLinkProperties.removeLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | // [2] Add only the local DNS IP addresses that have not previously been added. |
| 822 | if (newDnses != null && !newDnses.isEmpty()) { |
| 823 | final HashSet<Inet6Address> addedDnses = (HashSet) newDnses.clone(); |
| 824 | if (mLastRaParams != null) { |
| 825 | addedDnses.removeAll(mLastRaParams.dnses); |
| 826 | } |
| 827 | |
| 828 | for (Inet6Address dns : addedDnses) { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 829 | if (!mInterfaceCtrl.addAddress(dns, RFC7421_PREFIX_LENGTH)) { |
| 830 | mLog.e("Failed to add local dns IP " + dns); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 831 | newDnses.remove(dns); |
| 832 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 833 | |
| 834 | mLinkProperties.addLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | try { |
Erik Kline | d3c0b5e | 2017-09-03 20:38:29 +0900 | [diff] [blame] | 839 | mNetd.tetherApplyDnsInterfaces(); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 840 | } catch (ServiceSpecificException | RemoteException e) { |
| 841 | mLog.e("Failed to update local DNS caching server"); |
| 842 | if (newDnses != null) newDnses.clear(); |
| 843 | } |
| 844 | } |
| 845 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 846 | private void addIpv6ForwardingRule(Ipv6ForwardingRule rule) { |
| 847 | try { |
Lorenzo Colitti | 0fb584b | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 848 | mNetd.tetherOffloadRuleAdd(rule.toTetherOffloadRuleParcel()); |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 849 | mIpv6ForwardingRules.put(rule.address, rule); |
| 850 | } catch (RemoteException | ServiceSpecificException e) { |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 851 | mLog.e("Could not add IPv6 downstream rule: ", e); |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | private void removeIpv6ForwardingRule(Ipv6ForwardingRule rule, boolean removeFromMap) { |
| 856 | try { |
Lorenzo Colitti | 0fb584b | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 857 | mNetd.tetherOffloadRuleRemove(rule.toTetherOffloadRuleParcel()); |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 858 | if (removeFromMap) { |
| 859 | mIpv6ForwardingRules.remove(rule.address); |
| 860 | } |
| 861 | } catch (RemoteException | ServiceSpecificException e) { |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 862 | mLog.e("Could not remove IPv6 downstream rule: ", e); |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 866 | private void clearIpv6ForwardingRules() { |
| 867 | for (Ipv6ForwardingRule rule : mIpv6ForwardingRules.values()) { |
| 868 | removeIpv6ForwardingRule(rule, false /*removeFromMap*/); |
| 869 | } |
| 870 | mIpv6ForwardingRules.clear(); |
| 871 | } |
| 872 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 873 | // Convenience method to replace a rule with the same rule on a new upstream interface. |
| 874 | // Allows replacing the rules in one iteration pass without ConcurrentModificationExceptions. |
| 875 | // Relies on the fact that rules are in a map indexed by IP address. |
| 876 | private void updateIpv6ForwardingRule(Ipv6ForwardingRule rule, int newIfindex) { |
| 877 | addIpv6ForwardingRule(rule.onNewUpstream(newIfindex)); |
| 878 | removeIpv6ForwardingRule(rule, false /*removeFromMap*/); |
| 879 | } |
| 880 | |
| 881 | // Handles all updates to IPv6 forwarding rules. These can currently change only if the upstream |
| 882 | // changes or if a neighbor event is received. |
| 883 | private void updateIpv6ForwardingRules(int prevUpstreamIfindex, int upstreamIfindex, |
| 884 | NeighborEvent e) { |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 885 | // If we no longer have an upstream, clear forwarding rules and do nothing else. |
| 886 | if (upstreamIfindex == 0) { |
| 887 | clearIpv6ForwardingRules(); |
| 888 | return; |
| 889 | } |
| 890 | |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 891 | // If the upstream interface has changed, remove all rules and re-add them with the new |
| 892 | // upstream interface. |
| 893 | if (prevUpstreamIfindex != upstreamIfindex) { |
| 894 | for (Ipv6ForwardingRule rule : mIpv6ForwardingRules.values()) { |
| 895 | updateIpv6ForwardingRule(rule, upstreamIfindex); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // If we're here to process a NeighborEvent, do so now. |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 900 | // mInterfaceParams must be non-null or the event would not have arrived. |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 901 | if (e == null) return; |
| 902 | if (!(e.ip instanceof Inet6Address) || e.ip.isMulticastAddress() |
| 903 | || e.ip.isLoopbackAddress() || e.ip.isLinkLocalAddress()) { |
| 904 | return; |
| 905 | } |
| 906 | |
Lorenzo Colitti | 8bc1e54 | 2020-04-14 09:21:19 +0000 | [diff] [blame] | 907 | // When deleting rules, we still need to pass a non-null MAC, even though it's ignored. |
| 908 | // Do this here instead of in the Ipv6ForwardingRule constructor to ensure that we never |
| 909 | // add rules with a null MAC, only delete them. |
| 910 | MacAddress dstMac = e.isValid() ? e.macAddr : NULL_MAC_ADDRESS; |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 911 | Ipv6ForwardingRule rule = new Ipv6ForwardingRule(upstreamIfindex, |
Lorenzo Colitti | 8bc1e54 | 2020-04-14 09:21:19 +0000 | [diff] [blame] | 912 | mInterfaceParams.index, (Inet6Address) e.ip, mInterfaceParams.macAddr, dstMac); |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 913 | if (e.isValid()) { |
| 914 | addIpv6ForwardingRule(rule); |
| 915 | } else { |
| 916 | removeIpv6ForwardingRule(rule, true /*removeFromMap*/); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | private void handleNeighborEvent(NeighborEvent e) { |
| 921 | if (mInterfaceParams != null |
| 922 | && mInterfaceParams.index == e.ifindex |
| 923 | && mInterfaceParams.hasMacAddress) { |
| 924 | updateIpv6ForwardingRules(mLastIPv6UpstreamIfindex, mLastIPv6UpstreamIfindex, e); |
| 925 | } |
| 926 | } |
| 927 | |
Erik Kline | 8e492f8 | 2018-09-14 16:25:54 +0900 | [diff] [blame] | 928 | private byte getHopLimit(String upstreamIface) { |
| 929 | try { |
| 930 | int upstreamHopLimit = Integer.parseUnsignedInt( |
| 931 | mNetd.getProcSysNet(INetd.IPV6, INetd.CONF, upstreamIface, "hop_limit")); |
| 932 | // Add one hop to account for this forwarding device |
| 933 | upstreamHopLimit++; |
| 934 | // Cap the hop limit to 255. |
| 935 | return (byte) Integer.min(upstreamHopLimit, 255); |
| 936 | } catch (Exception e) { |
| 937 | mLog.e("Failed to find upstream interface hop limit", e); |
| 938 | } |
| 939 | return RaParams.DEFAULT_HOPLIMIT; |
| 940 | } |
| 941 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 942 | private void setRaParams(RaParams newParams) { |
| 943 | if (mRaDaemon != null) { |
| 944 | final RaParams deprecatedParams = |
| 945 | RaParams.getDeprecatedRaParams(mLastRaParams, newParams); |
| 946 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 947 | configureLocalIPv6Routes(deprecatedParams.prefixes, |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 948 | (newParams != null) ? newParams.prefixes : null); |
| 949 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 950 | configureLocalIPv6Dns(deprecatedParams.dnses, |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 951 | (newParams != null) ? newParams.dnses : null); |
| 952 | |
| 953 | mRaDaemon.buildNewRa(deprecatedParams, newParams); |
| 954 | } |
| 955 | |
| 956 | mLastRaParams = newParams; |
| 957 | } |
| 958 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 959 | private void logMessage(State state, int what) { |
| 960 | mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what))); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 963 | private void sendInterfaceState(int newInterfaceState) { |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 964 | mServingMode = newInterfaceState; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 965 | mCallback.updateInterfaceState(this, newInterfaceState, mLastError); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 966 | sendLinkProperties(); |
| 967 | } |
| 968 | |
| 969 | private void sendLinkProperties() { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 970 | mCallback.updateLinkProperties(this, new LinkProperties(mLinkProperties)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 971 | } |
| 972 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 973 | private void resetLinkProperties() { |
| 974 | mLinkProperties.clear(); |
| 975 | mLinkProperties.setInterfaceName(mIfaceName); |
| 976 | } |
| 977 | |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 978 | private void maybeConfigureStaticIp(final TetheringRequestParcel request) { |
Treehugger Robot | 86d212b | 2020-03-30 04:23:55 +0000 | [diff] [blame] | 979 | // Ignore static address configuration if they are invalid or null. In theory, static |
| 980 | // addresses should not be invalid here because TetheringManager do not allow caller to |
| 981 | // specify invalid static address configuration. |
| 982 | if (request == null || request.localIPv4Address == null |
| 983 | || request.staticClientAddress == null || !checkStaticAddressConfiguration( |
| 984 | request.localIPv4Address, request.staticClientAddress)) { |
| 985 | return; |
| 986 | } |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 987 | |
| 988 | mStaticIpv4ServerAddr = request.localIPv4Address; |
| 989 | mStaticIpv4ClientAddr = request.staticClientAddress; |
| 990 | } |
| 991 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 992 | class InitialState extends State { |
| 993 | @Override |
| 994 | public void enter() { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 995 | sendInterfaceState(STATE_AVAILABLE); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public boolean processMessage(Message message) { |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1000 | logMessage(this, message.what); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1001 | switch (message.what) { |
| 1002 | case CMD_TETHER_REQUESTED: |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1003 | mLastError = TetheringManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1004 | switch (message.arg1) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1005 | case STATE_LOCAL_ONLY: |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 1006 | maybeConfigureStaticIp((TetheringRequestParcel) message.obj); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1007 | transitionTo(mLocalHotspotState); |
| 1008 | break; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1009 | case STATE_TETHERED: |
Automerger Merge Worker | 37a2275 | 2020-03-17 16:59:57 +0000 | [diff] [blame] | 1010 | maybeConfigureStaticIp((TetheringRequestParcel) message.obj); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1011 | transitionTo(mTetheredState); |
| 1012 | break; |
| 1013 | default: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1014 | mLog.e("Invalid tethering interface serving state specified."); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1015 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1016 | break; |
| 1017 | case CMD_INTERFACE_DOWN: |
| 1018 | transitionTo(mUnavailableState); |
| 1019 | break; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1020 | case CMD_IPV6_TETHER_UPDATE: |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1021 | updateUpstreamIPv6LinkProperties((LinkProperties) message.obj); |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1022 | break; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1023 | default: |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1024 | return NOT_HANDLED; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1025 | } |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1026 | return HANDLED; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1030 | class BaseServingState extends State { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1031 | @Override |
| 1032 | public void enter() { |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1033 | if (!startIPv4()) { |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1034 | mLastError = TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1035 | return; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1036 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1037 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1038 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1039 | final IpPrefix ipv4Prefix = new IpPrefix(mIpv4Address.getAddress(), |
| 1040 | mIpv4Address.getPrefixLength()); |
| 1041 | NetdUtils.tetherInterface(mNetd, mIfaceName, ipv4Prefix); |
markchien | d72cf2c | 2020-02-15 11:35:00 +0800 | [diff] [blame] | 1042 | } catch (RemoteException | ServiceSpecificException | IllegalStateException e) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1043 | mLog.e("Error Tethering: " + e); |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1044 | mLastError = TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1045 | return; |
| 1046 | } |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1047 | |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1048 | if (!startIPv6()) { |
| 1049 | mLog.e("Failed to startIPv6"); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1050 | // TODO: Make this a fatal error once Bluetooth IPv6 is sorted. |
| 1051 | return; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1052 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 1055 | @Override |
| 1056 | public void exit() { |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 1057 | // Note that at this point, we're leaving the tethered state. We can fail any |
| 1058 | // of these operations, but it doesn't really change that we have to try them |
| 1059 | // all in sequence. |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1060 | stopIPv6(); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 1061 | |
| 1062 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1063 | NetdUtils.untetherInterface(mNetd, mIfaceName); |
| 1064 | } catch (RemoteException | ServiceSpecificException e) { |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1065 | mLastError = TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1066 | mLog.e("Failed to untether interface: " + e); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1069 | stopIPv4(); |
| 1070 | |
| 1071 | resetLinkProperties(); |
Christopher Wiley | e10bfc0 | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1074 | @Override |
| 1075 | public boolean processMessage(Message message) { |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1076 | logMessage(this, message.what); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1077 | switch (message.what) { |
| 1078 | case CMD_TETHER_UNREQUESTED: |
| 1079 | transitionTo(mInitialState); |
| 1080 | if (DBG) Log.d(TAG, "Untethered (unrequested)" + mIfaceName); |
| 1081 | break; |
| 1082 | case CMD_INTERFACE_DOWN: |
| 1083 | transitionTo(mUnavailableState); |
| 1084 | if (DBG) Log.d(TAG, "Untethered (ifdown)" + mIfaceName); |
| 1085 | break; |
| 1086 | case CMD_IPV6_TETHER_UPDATE: |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1087 | updateUpstreamIPv6LinkProperties((LinkProperties) message.obj); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1088 | sendLinkProperties(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1089 | break; |
| 1090 | case CMD_IP_FORWARDING_ENABLE_ERROR: |
| 1091 | case CMD_IP_FORWARDING_DISABLE_ERROR: |
| 1092 | case CMD_START_TETHERING_ERROR: |
| 1093 | case CMD_STOP_TETHERING_ERROR: |
| 1094 | case CMD_SET_DNS_FORWARDERS_ERROR: |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 1095 | mLastError = TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1096 | transitionTo(mInitialState); |
| 1097 | break; |
| 1098 | default: |
| 1099 | return false; |
| 1100 | } |
| 1101 | return true; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1105 | // Handling errors in BaseServingState.enter() by transitioning is |
| 1106 | // problematic because transitioning during a multi-state jump yields |
| 1107 | // a Log.wtf(). Ultimately, there should be only one ServingState, |
| 1108 | // and forwarding and NAT rules should be handled by a coordinating |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1109 | // functional element outside of IpServer. |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1110 | class LocalHotspotState extends BaseServingState { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1111 | @Override |
| 1112 | public void enter() { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1113 | super.enter(); |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1114 | if (mLastError != TetheringManager.TETHER_ERROR_NO_ERROR) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1115 | transitionTo(mInitialState); |
| 1116 | } |
| 1117 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1118 | if (DBG) Log.d(TAG, "Local hotspot " + mIfaceName); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1119 | sendInterfaceState(STATE_LOCAL_ONLY); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | @Override |
| 1123 | public boolean processMessage(Message message) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1124 | if (super.processMessage(message)) return true; |
| 1125 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1126 | logMessage(this, message.what); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1127 | switch (message.what) { |
| 1128 | case CMD_TETHER_REQUESTED: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1129 | mLog.e("CMD_TETHER_REQUESTED while in local-only hotspot mode."); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1130 | break; |
| 1131 | case CMD_TETHER_CONNECTION_CHANGED: |
| 1132 | // Ignored in local hotspot state. |
| 1133 | break; |
| 1134 | default: |
| 1135 | return false; |
| 1136 | } |
| 1137 | return true; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1141 | // Handling errors in BaseServingState.enter() by transitioning is |
| 1142 | // problematic because transitioning during a multi-state jump yields |
| 1143 | // a Log.wtf(). Ultimately, there should be only one ServingState, |
| 1144 | // and forwarding and NAT rules should be handled by a coordinating |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1145 | // functional element outside of IpServer. |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1146 | class TetheredState extends BaseServingState { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1147 | @Override |
| 1148 | public void enter() { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1149 | super.enter(); |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1150 | if (mLastError != TetheringManager.TETHER_ERROR_NO_ERROR) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1151 | transitionTo(mInitialState); |
| 1152 | } |
| 1153 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1154 | if (DBG) Log.d(TAG, "Tethered " + mIfaceName); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1155 | sendInterfaceState(STATE_TETHERED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | @Override |
| 1159 | public void exit() { |
| 1160 | cleanupUpstream(); |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1161 | super.exit(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1162 | } |
| 1163 | |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1164 | private void cleanupUpstream() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1165 | if (mUpstreamIfaceSet == null) return; |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 1166 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1167 | for (String ifname : mUpstreamIfaceSet.ifnames) cleanupUpstreamInterface(ifname); |
| 1168 | mUpstreamIfaceSet = null; |
Automerger Merge Worker | 43c671d | 2020-02-25 04:04:44 +0000 | [diff] [blame] | 1169 | clearIpv6ForwardingRules(); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | private void cleanupUpstreamInterface(String upstreamIface) { |
| 1173 | // Note that we don't care about errors here. |
| 1174 | // Sometimes interfaces are gone before we get |
| 1175 | // to remove their rules, which generates errors. |
| 1176 | // Just do the best we can. |
| 1177 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1178 | mNetd.ipfwdRemoveInterfaceForward(mIfaceName, upstreamIface); |
| 1179 | } catch (RemoteException | ServiceSpecificException e) { |
| 1180 | mLog.e("Exception in ipfwdRemoveInterfaceForward: " + e.toString()); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 1181 | } |
| 1182 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1183 | mNetd.tetherRemoveForward(mIfaceName, upstreamIface); |
| 1184 | } catch (RemoteException | ServiceSpecificException e) { |
| 1185 | mLog.e("Exception in disableNat: " + e.toString()); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 1186 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | @Override |
| 1190 | public boolean processMessage(Message message) { |
Erik Kline | f79a34d | 2017-04-13 22:54:34 +0900 | [diff] [blame] | 1191 | if (super.processMessage(message)) return true; |
| 1192 | |
Erik Kline | b4df3f4 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1193 | logMessage(this, message.what); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1194 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1195 | case CMD_TETHER_REQUESTED: |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1196 | mLog.e("CMD_TETHER_REQUESTED while already tethering."); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1197 | break; |
| 1198 | case CMD_TETHER_CONNECTION_CHANGED: |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1199 | final InterfaceSet newUpstreamIfaceSet = (InterfaceSet) message.obj; |
| 1200 | if (noChangeInUpstreamIfaceSet(newUpstreamIfaceSet)) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1201 | if (VDBG) Log.d(TAG, "Connection changed noop - dropping"); |
| 1202 | break; |
| 1203 | } |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1204 | |
| 1205 | if (newUpstreamIfaceSet == null) { |
| 1206 | cleanupUpstream(); |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | for (String removed : upstreamInterfacesRemoved(newUpstreamIfaceSet)) { |
| 1211 | cleanupUpstreamInterface(removed); |
| 1212 | } |
| 1213 | |
| 1214 | final Set<String> added = upstreamInterfacesAdd(newUpstreamIfaceSet); |
| 1215 | // This makes the call to cleanupUpstream() in the error |
| 1216 | // path for any interface neatly cleanup all the interfaces. |
| 1217 | mUpstreamIfaceSet = newUpstreamIfaceSet; |
| 1218 | |
| 1219 | for (String ifname : added) { |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1220 | try { |
markchien | 92b8e7f | 2020-01-07 14:43:17 +0800 | [diff] [blame] | 1221 | mNetd.tetherAddForward(mIfaceName, ifname); |
| 1222 | mNetd.ipfwdAddInterfaceForward(mIfaceName, ifname); |
| 1223 | } catch (RemoteException | ServiceSpecificException e) { |
| 1224 | mLog.e("Exception enabling NAT: " + e.toString()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1225 | cleanupUpstream(); |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 1226 | mLastError = TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1227 | transitionTo(mInitialState); |
| 1228 | return true; |
| 1229 | } |
| 1230 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1231 | break; |
Lorenzo Colitti | fb19f49 | 2020-02-14 01:06:35 +0900 | [diff] [blame] | 1232 | case CMD_NEIGHBOR_EVENT: |
| 1233 | handleNeighborEvent((NeighborEvent) message.obj); |
| 1234 | break; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1235 | default: |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1236 | return false; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1237 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1238 | return true; |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1239 | } |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 1240 | |
| 1241 | private boolean noChangeInUpstreamIfaceSet(InterfaceSet newIfaces) { |
| 1242 | if (mUpstreamIfaceSet == null && newIfaces == null) return true; |
| 1243 | if (mUpstreamIfaceSet != null && newIfaces != null) { |
| 1244 | return mUpstreamIfaceSet.equals(newIfaces); |
| 1245 | } |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
| 1249 | private Set<String> upstreamInterfacesRemoved(InterfaceSet newIfaces) { |
| 1250 | if (mUpstreamIfaceSet == null) return new HashSet<>(); |
| 1251 | |
| 1252 | final HashSet<String> removed = new HashSet<>(mUpstreamIfaceSet.ifnames); |
| 1253 | removed.removeAll(newIfaces.ifnames); |
| 1254 | return removed; |
| 1255 | } |
| 1256 | |
| 1257 | private Set<String> upstreamInterfacesAdd(InterfaceSet newIfaces) { |
| 1258 | final HashSet<String> added = new HashSet<>(newIfaces.ifnames); |
| 1259 | if (mUpstreamIfaceSet != null) added.removeAll(mUpstreamIfaceSet.ifnames); |
| 1260 | return added; |
| 1261 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Christopher Wiley | 9ad83ab | 2016-05-20 17:51:27 -0700 | [diff] [blame] | 1264 | /** |
| 1265 | * This state is terminal for the per interface state machine. At this |
| 1266 | * point, the master state machine should have removed this interface |
| 1267 | * specific state machine from its list of possible recipients of |
| 1268 | * tethering requests. The state machine itself will hang around until |
| 1269 | * the garbage collector finds it. |
| 1270 | */ |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1271 | class UnavailableState extends State { |
| 1272 | @Override |
| 1273 | public void enter() { |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 1274 | mLastError = TetheringManager.TETHER_ERROR_NO_ERROR; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 1275 | sendInterfaceState(STATE_UNAVAILABLE); |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1276 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1277 | } |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1278 | |
| 1279 | // Accumulate routes representing "prefixes to be assigned to the local |
| 1280 | // interface", for subsequent modification of local_network routing. |
| 1281 | private static ArrayList<RouteInfo> getLocalRoutesFor( |
| 1282 | String ifname, HashSet<IpPrefix> prefixes) { |
| 1283 | final ArrayList<RouteInfo> localRoutes = new ArrayList<RouteInfo>(); |
| 1284 | for (IpPrefix ipp : prefixes) { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 1285 | localRoutes.add(new RouteInfo(ipp, null, ifname, RTN_UNICAST)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1286 | } |
| 1287 | return localRoutes; |
| 1288 | } |
| 1289 | |
| 1290 | // Given a prefix like 2001:db8::/64 return an address like 2001:db8::1. |
| 1291 | private static Inet6Address getLocalDnsIpFor(IpPrefix localPrefix) { |
| 1292 | final byte[] dnsBytes = localPrefix.getRawAddress(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 1293 | dnsBytes[dnsBytes.length - 1] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1)); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1294 | try { |
| 1295 | return Inet6Address.getByAddress(null, dnsBytes, 0); |
| 1296 | } catch (UnknownHostException e) { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 1297 | Log.wtf(TAG, "Failed to construct Inet6Address from: " + localPrefix); |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1298 | return null; |
| 1299 | } |
| 1300 | } |
| 1301 | |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 1302 | private static byte getRandomSanitizedByte(byte dflt, byte... excluded) { |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1303 | final byte random = (byte) (new Random()).nextInt(); |
Erik Kline | 0f27eed | 2018-05-29 19:24:43 +0900 | [diff] [blame] | 1304 | for (int value : excluded) { |
| 1305 | if (random == value) return dflt; |
| 1306 | } |
| 1307 | return random; |
Erik Kline | 94ae4c9 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1308 | } |
Christopher Wiley | d289666 | 2016-05-19 11:54:54 -0700 | [diff] [blame] | 1309 | } |