blob: bb09d0de36e52709a1055075193cab93ac05d86a [file] [log] [blame]
markchien74a4fa92019-09-09 20:50:49 +08001/*
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
17package android.net.ip;
18
markchien6cf0e552019-12-06 15:24:53 +080019import static android.net.RouteInfo.RTN_UNICAST;
Mark49649c92023-03-15 06:45:04 +000020import static android.net.TetheringManager.CONNECTIVITY_SCOPE_GLOBAL;
21import static android.net.TetheringManager.CONNECTIVITY_SCOPE_LOCAL;
Mark74461fc2022-12-14 08:49:40 +000022import static android.net.TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR;
23import static android.net.TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR;
24import static android.net.TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR;
25import static android.net.TetheringManager.TETHER_ERROR_INTERNAL_ERROR;
26import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
27import static android.net.TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR;
28import static android.net.TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
markchien245352e2020-02-27 20:27:18 +080029import static android.net.TetheringManager.TetheringRequest.checkStaticAddressConfiguration;
markchien74a4fa92019-09-09 20:50:49 +080030import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS;
markchien74a4fa92019-09-09 20:50:49 +080031import static android.net.util.NetworkConstants.asByte;
Remi NGUYEN VANdfdf7502020-03-09 15:38:45 +090032import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
markchien74a4fa92019-09-09 20:50:49 +080033
Chalard Jean78701642020-07-31 20:00:30 +090034import static com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTH;
Xiao Ma1f993302023-08-09 18:55:49 +090035import static com.android.net.module.util.NetworkStackConstants.RFC7421_PREFIX_LENGTH;
markchien7dc1b4d2021-07-13 17:12:56 +080036import static com.android.networkstack.tethering.UpstreamNetworkState.isVcnInterface;
markchiend02f9af2021-11-04 11:26:03 +080037import static com.android.networkstack.tethering.util.PrefixUtils.asIpPrefix;
38import static com.android.networkstack.tethering.util.TetheringMessageBase.BASE_IPSERVER;
Chalard Jean78701642020-07-31 20:00:30 +090039
markchien74a4fa92019-09-09 20:50:49 +080040import android.net.INetd;
41import android.net.INetworkStackStatusCallback;
markchien74a4fa92019-09-09 20:50:49 +080042import android.net.IpPrefix;
43import android.net.LinkAddress;
44import android.net.LinkProperties;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090045import android.net.MacAddress;
markchien74a4fa92019-09-09 20:50:49 +080046import android.net.RouteInfo;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090047import android.net.TetheredClient;
markchien9b4d7572019-12-25 19:40:32 +080048import android.net.TetheringManager;
markchienf053e4b2020-03-16 21:49:48 +080049import android.net.TetheringRequestParcel;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090050import android.net.dhcp.DhcpLeaseParcelable;
markchien74a4fa92019-09-09 20:50:49 +080051import android.net.dhcp.DhcpServerCallbacks;
52import android.net.dhcp.DhcpServingParamsParcel;
53import android.net.dhcp.DhcpServingParamsParcelExt;
Xiao Ma49889dd2020-04-03 17:01:33 +090054import android.net.dhcp.IDhcpEventCallbacks;
markchien74a4fa92019-09-09 20:50:49 +080055import android.net.dhcp.IDhcpServer;
56import android.net.ip.RouterAdvertisementDaemon.RaParams;
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +090057import android.os.Handler;
markchien74a4fa92019-09-09 20:50:49 +080058import android.os.Looper;
59import android.os.Message;
60import android.os.RemoteException;
61import android.os.ServiceSpecificException;
62import android.util.Log;
markchien74a4fa92019-09-09 20:50:49 +080063import android.util.SparseArray;
64
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090065import androidx.annotation.NonNull;
Xiao Ma4455d6b2020-04-09 10:13:44 +090066import androidx.annotation.Nullable;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090067
markchien74a4fa92019-09-09 20:50:49 +080068import com.android.internal.util.MessageUtils;
markchien74a4fa92019-09-09 20:50:49 +080069import com.android.internal.util.State;
70import com.android.internal.util.StateMachine;
markchien932df542021-08-12 13:56:43 +080071import com.android.modules.utils.build.SdkLevel;
Patrick Rohr9f371f02022-03-04 15:14:27 +010072import com.android.net.module.util.InterfaceParams;
Chalard Jeanadcec9e2021-10-29 15:57:22 +090073import com.android.net.module.util.NetdUtils;
Patrick Rohrb873e252022-06-16 16:38:43 -070074import com.android.net.module.util.SharedLog;
Xiao Mac81c0662022-06-17 14:58:03 +090075import com.android.net.module.util.ip.InterfaceController;
76import com.android.net.module.util.ip.IpNeighborMonitor;
77import com.android.net.module.util.ip.IpNeighborMonitor.NeighborEvent;
Hungming Chen68f1c2a2020-03-12 21:24:01 +080078import com.android.networkstack.tethering.BpfCoordinator;
Hungming Chend71c06e2020-12-21 19:39:49 +080079import com.android.networkstack.tethering.BpfCoordinator.ClientInfo;
KH Shi59ad35e2023-08-15 07:13:00 +000080import com.android.networkstack.tethering.BpfCoordinator.Ipv6DownstreamRule;
markchienc9daba32020-02-12 00:19:21 +080081import com.android.networkstack.tethering.PrivateAddressCoordinator;
markchienb961d3d2022-02-26 00:39:06 +080082import com.android.networkstack.tethering.TetheringConfiguration;
Wayne Ma6cd440f2022-03-14 18:04:33 +080083import com.android.networkstack.tethering.metrics.TetheringMetrics;
markchiend02f9af2021-11-04 11:26:03 +080084import com.android.networkstack.tethering.util.InterfaceSet;
85import com.android.networkstack.tethering.util.PrefixUtils;
markchien74a4fa92019-09-09 20:50:49 +080086
87import java.net.Inet4Address;
88import java.net.Inet6Address;
markchien74a4fa92019-09-09 20:50:49 +080089import java.net.UnknownHostException;
90import java.util.ArrayList;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090091import java.util.Arrays;
92import java.util.Collections;
markchien74a4fa92019-09-09 20:50:49 +080093import java.util.HashSet;
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +090094import java.util.List;
markchien74a4fa92019-09-09 20:50:49 +080095import java.util.Objects;
96import java.util.Random;
97import java.util.Set;
98
99/**
100 * Provides the interface to IP-layer serving functionality for a given network
101 * interface, e.g. for tethering or "local-only hotspot" mode.
102 *
103 * @hide
104 */
105public class IpServer extends StateMachine {
106 public static final int STATE_UNAVAILABLE = 0;
107 public static final int STATE_AVAILABLE = 1;
108 public static final int STATE_TETHERED = 2;
109 public static final int STATE_LOCAL_ONLY = 3;
110
111 /** Get string name of |state|.*/
112 public static String getStateString(int state) {
113 switch (state) {
114 case STATE_UNAVAILABLE: return "UNAVAILABLE";
115 case STATE_AVAILABLE: return "AVAILABLE";
116 case STATE_TETHERED: return "TETHERED";
117 case STATE_LOCAL_ONLY: return "LOCAL_ONLY";
118 }
119 return "UNKNOWN: " + state;
120 }
121
122 private static final byte DOUG_ADAMS = (byte) 42;
123
markchien74a4fa92019-09-09 20:50:49 +0800124 // TODO: have PanService use some visible version of this constant
markchienc9daba32020-02-12 00:19:21 +0800125 private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1/24";
markchien74a4fa92019-09-09 20:50:49 +0800126
127 // TODO: have this configurable
128 private static final int DHCP_LEASE_TIME_SECS = 3600;
129
Lorenzo Colitti330a9b92020-04-14 14:57:30 +0900130 private static final MacAddress NULL_MAC_ADDRESS = MacAddress.fromString("00:00:00:00:00:00");
131
markchien74a4fa92019-09-09 20:50:49 +0800132 private static final String TAG = "IpServer";
133 private static final boolean DBG = false;
134 private static final boolean VDBG = false;
135 private static final Class[] sMessageClasses = {
136 IpServer.class
137 };
138 private static final SparseArray<String> sMagicDecoderRing =
139 MessageUtils.findMessageNames(sMessageClasses);
140
141 /** IpServer callback. */
142 public static class Callback {
143 /**
144 * Notify that |who| has changed its tethering state.
145 *
146 * @param who the calling instance of IpServer
147 * @param state one of STATE_*
markchien9b4d7572019-12-25 19:40:32 +0800148 * @param lastError one of TetheringManager.TETHER_ERROR_*
markchien74a4fa92019-09-09 20:50:49 +0800149 */
markchien9d353822019-12-16 20:15:20 +0800150 public void updateInterfaceState(IpServer who, int state, int lastError) { }
markchien74a4fa92019-09-09 20:50:49 +0800151
152 /**
153 * Notify that |who| has new LinkProperties.
154 *
155 * @param who the calling instance of IpServer
156 * @param newLp the new LinkProperties to report
157 */
markchien9d353822019-12-16 20:15:20 +0800158 public void updateLinkProperties(IpServer who, LinkProperties newLp) { }
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900159
160 /**
161 * Notify that the DHCP leases changed in one of the IpServers.
162 */
163 public void dhcpLeasesChanged() { }
markchienc9daba32020-02-12 00:19:21 +0800164
165 /**
166 * Request Tethering change.
167 *
168 * @param tetheringType the downstream type of this IpServer.
169 * @param enabled enable or disable tethering.
170 */
171 public void requestEnableTethering(int tetheringType, boolean enabled) { }
markchien74a4fa92019-09-09 20:50:49 +0800172 }
173
174 /** Capture IpServer dependencies, for injection. */
markchien9d353822019-12-16 20:15:20 +0800175 public abstract static class Dependencies {
Tyler Wear90e40632020-03-13 11:38:38 -0700176 /**
177 * Create a DadProxy instance to be used by IpServer.
178 * To support multiple tethered interfaces concurrently DAD Proxy
179 * needs to be supported per IpServer instead of per upstream.
180 */
181 public DadProxy getDadProxy(Handler handler, InterfaceParams ifParams) {
182 return new DadProxy(handler, ifParams);
183 }
184
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900185 /** Create an IpNeighborMonitor to be used by this IpServer */
186 public IpNeighborMonitor getIpNeighborMonitor(Handler handler, SharedLog log,
187 IpNeighborMonitor.NeighborEventConsumer consumer) {
188 return new IpNeighborMonitor(handler, log, consumer);
189 }
190
markchien74a4fa92019-09-09 20:50:49 +0800191 /** Create a RouterAdvertisementDaemon instance to be used by IpServer.*/
192 public RouterAdvertisementDaemon getRouterAdvertisementDaemon(InterfaceParams ifParams) {
193 return new RouterAdvertisementDaemon(ifParams);
194 }
195
196 /** Get |ifName|'s interface information.*/
197 public InterfaceParams getInterfaceParams(String ifName) {
198 return InterfaceParams.getByName(ifName);
199 }
200
markchien9d353822019-12-16 20:15:20 +0800201 /** Create a DhcpServer instance to be used by IpServer. */
202 public abstract void makeDhcpServer(String ifName, DhcpServingParamsParcel params,
203 DhcpServerCallbacks cb);
markchien74a4fa92019-09-09 20:50:49 +0800204 }
205
markchien74a4fa92019-09-09 20:50:49 +0800206 // request from the user that it wants to tether
markchien6cf0e552019-12-06 15:24:53 +0800207 public static final int CMD_TETHER_REQUESTED = BASE_IPSERVER + 1;
markchien74a4fa92019-09-09 20:50:49 +0800208 // request from the user that it wants to untether
markchien6cf0e552019-12-06 15:24:53 +0800209 public static final int CMD_TETHER_UNREQUESTED = BASE_IPSERVER + 2;
markchien74a4fa92019-09-09 20:50:49 +0800210 // notification that this interface is down
markchien6cf0e552019-12-06 15:24:53 +0800211 public static final int CMD_INTERFACE_DOWN = BASE_IPSERVER + 3;
Chiachang Wang14aaefc2020-07-29 12:05:04 +0800212 // notification from the {@link Tethering.TetherMainSM} that it had trouble enabling IP
213 // Forwarding
markchien6cf0e552019-12-06 15:24:53 +0800214 public static final int CMD_IP_FORWARDING_ENABLE_ERROR = BASE_IPSERVER + 4;
Chiachang Wang14aaefc2020-07-29 12:05:04 +0800215 // notification from the {@link Tethering.TetherMainSM} SM that it had trouble disabling IP
216 // Forwarding
markchien6cf0e552019-12-06 15:24:53 +0800217 public static final int CMD_IP_FORWARDING_DISABLE_ERROR = BASE_IPSERVER + 5;
Chiachang Wang14aaefc2020-07-29 12:05:04 +0800218 // notification from the {@link Tethering.TetherMainSM} SM that it had trouble starting
219 // tethering
markchien6cf0e552019-12-06 15:24:53 +0800220 public static final int CMD_START_TETHERING_ERROR = BASE_IPSERVER + 6;
Chiachang Wang14aaefc2020-07-29 12:05:04 +0800221 // notification from the {@link Tethering.TetherMainSM} that it had trouble stopping tethering
markchien6cf0e552019-12-06 15:24:53 +0800222 public static final int CMD_STOP_TETHERING_ERROR = BASE_IPSERVER + 7;
Chiachang Wang14aaefc2020-07-29 12:05:04 +0800223 // notification from the {@link Tethering.TetherMainSM} that it had trouble setting the DNS
224 // forwarders
markchien6cf0e552019-12-06 15:24:53 +0800225 public static final int CMD_SET_DNS_FORWARDERS_ERROR = BASE_IPSERVER + 8;
markchien74a4fa92019-09-09 20:50:49 +0800226 // the upstream connection has changed
markchien6cf0e552019-12-06 15:24:53 +0800227 public static final int CMD_TETHER_CONNECTION_CHANGED = BASE_IPSERVER + 9;
markchien74a4fa92019-09-09 20:50:49 +0800228 // new IPv6 tethering parameters need to be processed
markchien6cf0e552019-12-06 15:24:53 +0800229 public static final int CMD_IPV6_TETHER_UPDATE = BASE_IPSERVER + 10;
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900230 // new neighbor cache entry on our interface
231 public static final int CMD_NEIGHBOR_EVENT = BASE_IPSERVER + 11;
Xiao Ma4455d6b2020-04-09 10:13:44 +0900232 // request from DHCP server that it wants to have a new prefix
233 public static final int CMD_NEW_PREFIX_REQUEST = BASE_IPSERVER + 12;
markchienc9daba32020-02-12 00:19:21 +0800234 // request from PrivateAddressCoordinator to restart tethering.
235 public static final int CMD_NOTIFY_PREFIX_CONFLICT = BASE_IPSERVER + 13;
markchien74a4fa92019-09-09 20:50:49 +0800236
237 private final State mInitialState;
238 private final State mLocalHotspotState;
239 private final State mTetheredState;
240 private final State mUnavailableState;
markchienc9daba32020-02-12 00:19:21 +0800241 private final State mWaitingForRestartState;
markchien74a4fa92019-09-09 20:50:49 +0800242
243 private final SharedLog mLog;
markchien74a4fa92019-09-09 20:50:49 +0800244 private final INetd mNetd;
Hungming Chen68f1c2a2020-03-12 21:24:01 +0800245 @NonNull
246 private final BpfCoordinator mBpfCoordinator;
markchien74a4fa92019-09-09 20:50:49 +0800247 private final Callback mCallback;
248 private final InterfaceController mInterfaceCtrl;
markchienc9daba32020-02-12 00:19:21 +0800249 private final PrivateAddressCoordinator mPrivateAddressCoordinator;
markchien74a4fa92019-09-09 20:50:49 +0800250
251 private final String mIfaceName;
252 private final int mInterfaceType;
253 private final LinkProperties mLinkProperties;
254 private final boolean mUsingLegacyDhcp;
Hungming Chen3d8fa882020-04-12 14:27:18 +0800255 private final boolean mUsingBpfOffload;
markchienfb65dfe2022-02-25 23:14:58 +0800256 private final int mP2pLeasesSubnetPrefixLength;
markchien74a4fa92019-09-09 20:50:49 +0800257
258 private final Dependencies mDeps;
259
260 private int mLastError;
261 private int mServingMode;
262 private InterfaceSet mUpstreamIfaceSet; // may change over time
263 private InterfaceParams mInterfaceParams;
264 // TODO: De-duplicate this with mLinkProperties above. Currently, these link
265 // properties are those selected by the IPv6TetheringCoordinator and relayed
266 // to us. By comparison, mLinkProperties contains the addresses and directly
267 // connected routes that have been formed from these properties iff. we have
268 // succeeded in configuring them and are able to announce them within Router
269 // Advertisements (otherwise, we do not add them to mLinkProperties at all).
270 private LinkProperties mLastIPv6LinkProperties;
271 private RouterAdvertisementDaemon mRaDaemon;
Tyler Wear90e40632020-03-13 11:38:38 -0700272 private DadProxy mDadProxy;
markchien74a4fa92019-09-09 20:50:49 +0800273
274 // To be accessed only on the handler thread
275 private int mDhcpServerStartIndex = 0;
276 private IDhcpServer mDhcpServer;
277 private RaParams mLastRaParams;
markchienf053e4b2020-03-16 21:49:48 +0800278
279 private LinkAddress mStaticIpv4ServerAddr;
280 private LinkAddress mStaticIpv4ClientAddr;
281
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900282 @NonNull
283 private List<TetheredClient> mDhcpLeases = Collections.emptyList();
markchien74a4fa92019-09-09 20:50:49 +0800284
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900285 private int mLastIPv6UpstreamIfindex = 0;
286
287 private class MyNeighborEventConsumer implements IpNeighborMonitor.NeighborEventConsumer {
288 public void accept(NeighborEvent e) {
289 sendMessage(CMD_NEIGHBOR_EVENT, e);
290 }
291 }
292
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900293 private final IpNeighborMonitor mIpNeighborMonitor;
294
markchienc9daba32020-02-12 00:19:21 +0800295 private LinkAddress mIpv4Address;
296
Wayne Ma6cd440f2022-03-14 18:04:33 +0800297 private final TetheringMetrics mTetheringMetrics;
298
Hungming Chen5bc3af92020-05-12 19:15:24 +0800299 // TODO: Add a dependency object to pass the data members or variables from the tethering
300 // object. It helps to reduce the arguments of the constructor.
markchien74a4fa92019-09-09 20:50:49 +0800301 public IpServer(
302 String ifaceName, Looper looper, int interfaceType, SharedLog log,
Hungming Chen68f1c2a2020-03-12 21:24:01 +0800303 INetd netd, @NonNull BpfCoordinator coordinator, Callback callback,
markchienb961d3d2022-02-26 00:39:06 +0800304 TetheringConfiguration config, PrivateAddressCoordinator addressCoordinator,
Wayne Ma6cd440f2022-03-14 18:04:33 +0800305 TetheringMetrics tetheringMetrics, Dependencies deps) {
markchien74a4fa92019-09-09 20:50:49 +0800306 super(ifaceName, looper);
307 mLog = log.forSubComponent(ifaceName);
markchien12c5bb82020-01-07 14:43:17 +0800308 mNetd = netd;
Hungming Chen68f1c2a2020-03-12 21:24:01 +0800309 mBpfCoordinator = coordinator;
markchien74a4fa92019-09-09 20:50:49 +0800310 mCallback = callback;
311 mInterfaceCtrl = new InterfaceController(ifaceName, mNetd, mLog);
312 mIfaceName = ifaceName;
313 mInterfaceType = interfaceType;
314 mLinkProperties = new LinkProperties();
markchienb961d3d2022-02-26 00:39:06 +0800315 mUsingLegacyDhcp = config.useLegacyDhcpServer();
316 mUsingBpfOffload = config.isBpfOffloadEnabled();
markchienfb65dfe2022-02-25 23:14:58 +0800317 mP2pLeasesSubnetPrefixLength = config.getP2pLeasesSubnetPrefixLength();
markchienc9daba32020-02-12 00:19:21 +0800318 mPrivateAddressCoordinator = addressCoordinator;
markchien74a4fa92019-09-09 20:50:49 +0800319 mDeps = deps;
Wayne Ma6cd440f2022-03-14 18:04:33 +0800320 mTetheringMetrics = tetheringMetrics;
markchien74a4fa92019-09-09 20:50:49 +0800321 resetLinkProperties();
Mark74461fc2022-12-14 08:49:40 +0000322 mLastError = TETHER_ERROR_NO_ERROR;
markchien74a4fa92019-09-09 20:50:49 +0800323 mServingMode = STATE_AVAILABLE;
324
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900325 mIpNeighborMonitor = mDeps.getIpNeighborMonitor(getHandler(), mLog,
326 new MyNeighborEventConsumer());
Hungming Chen3d8fa882020-04-12 14:27:18 +0800327
Hungming Chen5bc3af92020-05-12 19:15:24 +0800328 // IP neighbor monitor monitors the neighbor events for adding/removing offload
Hungming Chen3d8fa882020-04-12 14:27:18 +0800329 // forwarding rules per client. If BPF offload is not supported, don't start listening
KH Shi59ad35e2023-08-15 07:13:00 +0000330 // for neighbor events. See updateIpv6ForwardingRules, addIpv6DownstreamRule,
331 // removeIpv6DownstreamRule.
Hungming Chen5bc3af92020-05-12 19:15:24 +0800332 if (mUsingBpfOffload && !mIpNeighborMonitor.start()) {
333 mLog.e("Failed to create IpNeighborMonitor on " + mIfaceName);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900334 }
335
markchien74a4fa92019-09-09 20:50:49 +0800336 mInitialState = new InitialState();
337 mLocalHotspotState = new LocalHotspotState();
338 mTetheredState = new TetheredState();
339 mUnavailableState = new UnavailableState();
markchienc9daba32020-02-12 00:19:21 +0800340 mWaitingForRestartState = new WaitingForRestartState();
markchien74a4fa92019-09-09 20:50:49 +0800341 addState(mInitialState);
342 addState(mLocalHotspotState);
343 addState(mTetheredState);
markchienc9daba32020-02-12 00:19:21 +0800344 addState(mWaitingForRestartState, mTetheredState);
markchien74a4fa92019-09-09 20:50:49 +0800345 addState(mUnavailableState);
346
347 setInitialState(mInitialState);
348 }
349
350 /** Interface name which IpServer served.*/
351 public String interfaceName() {
352 return mIfaceName;
353 }
354
355 /**
markchien9b4d7572019-12-25 19:40:32 +0800356 * Tethering downstream type. It would be one of TetheringManager#TETHERING_*.
markchien74a4fa92019-09-09 20:50:49 +0800357 */
358 public int interfaceType() {
359 return mInterfaceType;
360 }
361
362 /** Last error from this IpServer. */
363 public int lastError() {
364 return mLastError;
365 }
366
367 /** Serving mode is the current state of IpServer state machine. */
368 public int servingMode() {
369 return mServingMode;
370 }
371
372 /** The properties of the network link which IpServer is serving. */
373 public LinkProperties linkProperties() {
374 return new LinkProperties(mLinkProperties);
375 }
376
markchienc9daba32020-02-12 00:19:21 +0800377 /** The address which IpServer is using. */
378 public LinkAddress getAddress() {
379 return mIpv4Address;
380 }
381
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900382 /**
383 * Get the latest list of DHCP leases that was reported. Must be called on the IpServer looper
384 * thread.
385 */
386 public List<TetheredClient> getAllLeases() {
387 return Collections.unmodifiableList(mDhcpLeases);
388 }
389
Marka5424582022-12-01 13:46:32 +0000390 /** Enable this IpServer. IpServer state machine will be tethered or localHotspot state. */
391 public void enable(final int requestedState, final TetheringRequestParcel request) {
392 sendMessage(CMD_TETHER_REQUESTED, requestedState, 0, request);
393 }
394
markchien74a4fa92019-09-09 20:50:49 +0800395 /** Stop this IpServer. After this is called this IpServer should not be used any more. */
396 public void stop() {
397 sendMessage(CMD_INTERFACE_DOWN);
398 }
399
400 /**
401 * Tethering is canceled. IpServer state machine will be available and wait for
402 * next tethering request.
403 */
404 public void unwanted() {
405 sendMessage(CMD_TETHER_UNREQUESTED);
406 }
407
408 /** Internals. */
409
Mark49649c92023-03-15 06:45:04 +0000410 private boolean startIPv4(int scope) {
411 return configureIPv4(true, scope);
markchien74a4fa92019-09-09 20:50:49 +0800412 }
413
414 /**
415 * Convenience wrapper around INetworkStackStatusCallback to run callbacks on the IpServer
416 * handler.
417 *
418 * <p>Different instances of this class can be created for each call to IDhcpServer methods,
419 * with different implementations of the callback, to differentiate handling of success/error in
420 * each call.
421 */
422 private abstract class OnHandlerStatusCallback extends INetworkStackStatusCallback.Stub {
423 @Override
424 public void onStatusAvailable(int statusCode) {
425 getHandler().post(() -> callback(statusCode));
426 }
427
428 public abstract void callback(int statusCode);
429
430 @Override
431 public int getInterfaceVersion() {
432 return this.VERSION;
433 }
Paul Trautrimbbfcd542020-01-23 14:55:57 +0900434
435 @Override
436 public String getInterfaceHash() {
437 return this.HASH;
438 }
markchien74a4fa92019-09-09 20:50:49 +0800439 }
440
441 private class DhcpServerCallbacksImpl extends DhcpServerCallbacks {
442 private final int mStartIndex;
443
444 private DhcpServerCallbacksImpl(int startIndex) {
445 mStartIndex = startIndex;
446 }
447
448 @Override
449 public void onDhcpServerCreated(int statusCode, IDhcpServer server) throws RemoteException {
450 getHandler().post(() -> {
451 // We are on the handler thread: mDhcpServerStartIndex can be read safely.
452 if (mStartIndex != mDhcpServerStartIndex) {
markchienaf2670f2020-07-22 21:28:48 +0800453 // This start request is obsolete. Explicitly stop the DHCP server to shut
454 // down its thread. When the |server| binder token goes out of scope, the
455 // garbage collector will finalize it, which causes the network stack process
456 // garbage collector to collect the server itself.
457 try {
458 server.stop(null);
459 } catch (RemoteException e) { }
markchien74a4fa92019-09-09 20:50:49 +0800460 return;
461 }
462
463 if (statusCode != STATUS_SUCCESS) {
464 mLog.e("Error obtaining DHCP server: " + statusCode);
465 handleError();
466 return;
467 }
468
469 mDhcpServer = server;
470 try {
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900471 mDhcpServer.startWithCallbacks(new OnHandlerStatusCallback() {
markchien74a4fa92019-09-09 20:50:49 +0800472 @Override
473 public void callback(int startStatusCode) {
474 if (startStatusCode != STATUS_SUCCESS) {
475 mLog.e("Error starting DHCP server: " + startStatusCode);
476 handleError();
477 }
478 }
Xiao Ma4455d6b2020-04-09 10:13:44 +0900479 }, new DhcpEventCallback());
markchien74a4fa92019-09-09 20:50:49 +0800480 } catch (RemoteException e) {
markchien12c5bb82020-01-07 14:43:17 +0800481 throw new IllegalStateException(e);
markchien74a4fa92019-09-09 20:50:49 +0800482 }
483 });
484 }
485
486 private void handleError() {
Mark74461fc2022-12-14 08:49:40 +0000487 mLastError = TETHER_ERROR_DHCPSERVER_ERROR;
markchien74a4fa92019-09-09 20:50:49 +0800488 transitionTo(mInitialState);
489 }
490 }
491
Xiao Ma4455d6b2020-04-09 10:13:44 +0900492 private class DhcpEventCallback extends IDhcpEventCallbacks.Stub {
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900493 @Override
494 public void onLeasesChanged(List<DhcpLeaseParcelable> leaseParcelables) {
495 final ArrayList<TetheredClient> leases = new ArrayList<>();
496 for (DhcpLeaseParcelable lease : leaseParcelables) {
497 final LinkAddress address = new LinkAddress(
Remi NGUYEN VANdfdf7502020-03-09 15:38:45 +0900498 intToInet4AddressHTH(lease.netAddr), lease.prefixLength,
499 0 /* flags */, RT_SCOPE_UNIVERSE /* as per RFC6724#3.2 */,
500 lease.expTime /* deprecationTime */, lease.expTime /* expirationTime */);
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900501
502 final MacAddress macAddress;
503 try {
504 macAddress = MacAddress.fromBytes(lease.hwAddr);
505 } catch (IllegalArgumentException e) {
506 Log.wtf(TAG, "Invalid address received from DhcpServer: "
507 + Arrays.toString(lease.hwAddr));
508 return;
509 }
510
511 final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo(
Remi NGUYEN VANdfdf7502020-03-09 15:38:45 +0900512 address, lease.hostname);
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900513 leases.add(new TetheredClient(
514 macAddress,
515 Collections.singletonList(addressInfo),
516 mInterfaceType));
517 }
518
519 getHandler().post(() -> {
520 mDhcpLeases = leases;
521 mCallback.dhcpLeasesChanged();
522 });
523 }
524
525 @Override
Xiao Ma4455d6b2020-04-09 10:13:44 +0900526 public void onNewPrefixRequest(@NonNull final IpPrefix currentPrefix) {
527 Objects.requireNonNull(currentPrefix);
528 sendMessage(CMD_NEW_PREFIX_REQUEST, currentPrefix);
Xiao Ma49889dd2020-04-03 17:01:33 +0900529 }
530
531 @Override
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900532 public int getInterfaceVersion() {
533 return this.VERSION;
534 }
535
536 @Override
537 public String getInterfaceHash() throws RemoteException {
538 return this.HASH;
539 }
540 }
541
Xiao Ma4455d6b2020-04-09 10:13:44 +0900542 private RouteInfo getDirectConnectedRoute(@NonNull final LinkAddress ipv4Address) {
543 Objects.requireNonNull(ipv4Address);
544 return new RouteInfo(PrefixUtils.asIpPrefix(ipv4Address), null, mIfaceName, RTN_UNICAST);
545 }
546
547 private DhcpServingParamsParcel makeServingParams(@NonNull final Inet4Address defaultRouter,
548 @NonNull final Inet4Address dnsServer, @NonNull LinkAddress serverAddr,
549 @Nullable Inet4Address clientAddr) {
550 final boolean changePrefixOnDecline =
551 (mInterfaceType == TetheringManager.TETHERING_NCM && clientAddr == null);
markchienfb65dfe2022-02-25 23:14:58 +0800552 final int subnetPrefixLength = mInterfaceType == TetheringManager.TETHERING_WIFI_P2P
553 ? mP2pLeasesSubnetPrefixLength : 0 /* default value */;
554
Xiao Ma4455d6b2020-04-09 10:13:44 +0900555 return new DhcpServingParamsParcelExt()
556 .setDefaultRouters(defaultRouter)
557 .setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS)
558 .setDnsServers(dnsServer)
559 .setServerAddr(serverAddr)
560 .setMetered(true)
561 .setSingleClientAddr(clientAddr)
markchienfb65dfe2022-02-25 23:14:58 +0800562 .setChangePrefixOnDecline(changePrefixOnDecline)
563 .setLeasesSubnetPrefixLength(subnetPrefixLength);
Xiao Ma4455d6b2020-04-09 10:13:44 +0900564 // TODO: also advertise link MTU
565 }
566
markchien245352e2020-02-27 20:27:18 +0800567 private boolean startDhcp(final LinkAddress serverLinkAddr, final LinkAddress clientLinkAddr) {
markchien74a4fa92019-09-09 20:50:49 +0800568 if (mUsingLegacyDhcp) {
569 return true;
570 }
markchien245352e2020-02-27 20:27:18 +0800571
572 final Inet4Address addr = (Inet4Address) serverLinkAddr.getAddress();
markchien245352e2020-02-27 20:27:18 +0800573 final Inet4Address clientAddr = clientLinkAddr == null ? null :
574 (Inet4Address) clientLinkAddr.getAddress();
575
Xiao Ma4455d6b2020-04-09 10:13:44 +0900576 final DhcpServingParamsParcel params = makeServingParams(addr /* defaultRouter */,
577 addr /* dnsServer */, serverLinkAddr, clientAddr);
markchien74a4fa92019-09-09 20:50:49 +0800578 mDhcpServerStartIndex++;
579 mDeps.makeDhcpServer(
580 mIfaceName, params, new DhcpServerCallbacksImpl(mDhcpServerStartIndex));
581 return true;
582 }
583
584 private void stopDhcp() {
585 // Make all previous start requests obsolete so servers are not started later
586 mDhcpServerStartIndex++;
587
588 if (mDhcpServer != null) {
589 try {
590 mDhcpServer.stop(new OnHandlerStatusCallback() {
591 @Override
592 public void callback(int statusCode) {
593 if (statusCode != STATUS_SUCCESS) {
594 mLog.e("Error stopping DHCP server: " + statusCode);
Mark74461fc2022-12-14 08:49:40 +0000595 mLastError = TETHER_ERROR_DHCPSERVER_ERROR;
markchien74a4fa92019-09-09 20:50:49 +0800596 // Not much more we can do here
597 }
Remi NGUYEN VANb9379a52020-02-13 09:16:19 +0900598 mDhcpLeases.clear();
599 getHandler().post(mCallback::dhcpLeasesChanged);
markchien74a4fa92019-09-09 20:50:49 +0800600 }
601 });
602 mDhcpServer = null;
603 } catch (RemoteException e) {
Xiao Ma4455d6b2020-04-09 10:13:44 +0900604 mLog.e("Error stopping DHCP server", e);
markchien12c5bb82020-01-07 14:43:17 +0800605 // Not much more we can do here
markchien74a4fa92019-09-09 20:50:49 +0800606 }
607 }
608 }
609
markchien245352e2020-02-27 20:27:18 +0800610 private boolean configureDhcp(boolean enable, final LinkAddress serverAddr,
611 final LinkAddress clientAddr) {
markchien74a4fa92019-09-09 20:50:49 +0800612 if (enable) {
markchien245352e2020-02-27 20:27:18 +0800613 return startDhcp(serverAddr, clientAddr);
markchien74a4fa92019-09-09 20:50:49 +0800614 } else {
615 stopDhcp();
616 return true;
617 }
618 }
619
620 private void stopIPv4() {
Mark49649c92023-03-15 06:45:04 +0000621 configureIPv4(false /* enabled */, CONNECTIVITY_SCOPE_GLOBAL /* not used */);
markchien74a4fa92019-09-09 20:50:49 +0800622 // NOTE: All of configureIPv4() will be refactored out of existence
623 // into calls to InterfaceController, shared with startIPv4().
624 mInterfaceCtrl.clearIPv4Address();
markchienc9daba32020-02-12 00:19:21 +0800625 mPrivateAddressCoordinator.releaseDownstream(this);
Hungming Chena6e78692021-02-08 17:15:35 +0800626 mBpfCoordinator.tetherOffloadClientClear(this);
markchien12c5bb82020-01-07 14:43:17 +0800627 mIpv4Address = null;
markchienf053e4b2020-03-16 21:49:48 +0800628 mStaticIpv4ServerAddr = null;
629 mStaticIpv4ClientAddr = null;
markchien74a4fa92019-09-09 20:50:49 +0800630 }
631
Mark49649c92023-03-15 06:45:04 +0000632 private boolean configureIPv4(boolean enabled, int scope) {
markchien74a4fa92019-09-09 20:50:49 +0800633 if (VDBG) Log.d(TAG, "configureIPv4(" + enabled + ")");
634
markchienc9daba32020-02-12 00:19:21 +0800635 if (enabled) {
Mark49649c92023-03-15 06:45:04 +0000636 mIpv4Address = requestIpv4Address(scope, true /* useLastAddress */);
markchienc9daba32020-02-12 00:19:21 +0800637 }
638
639 if (mIpv4Address == null) {
640 mLog.e("No available ipv4 address");
markchien12c5bb82020-01-07 14:43:17 +0800641 return false;
markchien74a4fa92019-09-09 20:50:49 +0800642 }
643
markchien69681d62021-11-15 23:58:05 +0800644 if (shouldNotConfigureBluetoothInterface()) {
645 // Interface was already configured elsewhere, only start DHCP.
markchienc9daba32020-02-12 00:19:21 +0800646 return configureDhcp(enabled, mIpv4Address, null /* clientAddress */);
647 }
648
649 final IpPrefix ipv4Prefix = asIpPrefix(mIpv4Address);
650
markchien12c5bb82020-01-07 14:43:17 +0800651 final Boolean setIfaceUp;
Jimmy Chenea902f62019-12-03 11:37:09 +0800652 if (mInterfaceType == TetheringManager.TETHERING_WIFI
Milim Lee98078162020-05-26 03:11:29 +0900653 || mInterfaceType == TetheringManager.TETHERING_WIFI_P2P
Dedy Lansky6896f612019-11-21 00:36:14 +0200654 || mInterfaceType == TetheringManager.TETHERING_ETHERNET
655 || mInterfaceType == TetheringManager.TETHERING_WIGIG) {
Milim Lee98078162020-05-26 03:11:29 +0900656 // The WiFi and Ethernet stack has ownership of the interface up/down state.
markchien12c5bb82020-01-07 14:43:17 +0800657 // It is unclear whether the Bluetooth or USB stacks will manage their own
658 // state.
659 setIfaceUp = null;
660 } else {
661 setIfaceUp = enabled;
662 }
663 if (!mInterfaceCtrl.setInterfaceConfiguration(mIpv4Address, setIfaceUp)) {
664 mLog.e("Error configuring interface");
665 if (!enabled) stopDhcp();
666 return false;
667 }
markchien74a4fa92019-09-09 20:50:49 +0800668
markchien74a4fa92019-09-09 20:50:49 +0800669 if (enabled) {
markchien12c5bb82020-01-07 14:43:17 +0800670 mLinkProperties.addLinkAddress(mIpv4Address);
Xiao Ma4455d6b2020-04-09 10:13:44 +0900671 mLinkProperties.addRoute(getDirectConnectedRoute(mIpv4Address));
markchien74a4fa92019-09-09 20:50:49 +0800672 } else {
markchien12c5bb82020-01-07 14:43:17 +0800673 mLinkProperties.removeLinkAddress(mIpv4Address);
Xiao Ma4455d6b2020-04-09 10:13:44 +0900674 mLinkProperties.removeRoute(getDirectConnectedRoute(mIpv4Address));
markchien74a4fa92019-09-09 20:50:49 +0800675 }
markchien245352e2020-02-27 20:27:18 +0800676 return configureDhcp(enabled, mIpv4Address, mStaticIpv4ClientAddr);
markchien74a4fa92019-09-09 20:50:49 +0800677 }
678
markchien69681d62021-11-15 23:58:05 +0800679 private boolean shouldNotConfigureBluetoothInterface() {
680 // Before T, bluetooth tethering configures the interface elsewhere.
681 return (mInterfaceType == TetheringManager.TETHERING_BLUETOOTH) && !SdkLevel.isAtLeastT();
682 }
683
Mark49649c92023-03-15 06:45:04 +0000684 private LinkAddress requestIpv4Address(final int scope, final boolean useLastAddress) {
markchienc9daba32020-02-12 00:19:21 +0800685 if (mStaticIpv4ServerAddr != null) return mStaticIpv4ServerAddr;
markchien74a4fa92019-09-09 20:50:49 +0800686
markchien69681d62021-11-15 23:58:05 +0800687 if (shouldNotConfigureBluetoothInterface()) return new LinkAddress(BLUETOOTH_IFACE_ADDR);
markchienc9daba32020-02-12 00:19:21 +0800688
Mark49649c92023-03-15 06:45:04 +0000689 return mPrivateAddressCoordinator.requestDownstreamAddress(this, scope, useLastAddress);
Xiao Ma4455d6b2020-04-09 10:13:44 +0900690 }
691
markchien74a4fa92019-09-09 20:50:49 +0800692 private boolean startIPv6() {
693 mInterfaceParams = mDeps.getInterfaceParams(mIfaceName);
694 if (mInterfaceParams == null) {
695 mLog.e("Failed to find InterfaceParams");
696 stopIPv6();
697 return false;
698 }
699
700 mRaDaemon = mDeps.getRouterAdvertisementDaemon(mInterfaceParams);
701 if (!mRaDaemon.start()) {
702 stopIPv6();
703 return false;
704 }
705
markchien932df542021-08-12 13:56:43 +0800706 if (SdkLevel.isAtLeastS()) {
Tyler Wear90e40632020-03-13 11:38:38 -0700707 // DAD Proxy starts forwarding packets after IPv6 upstream is present.
708 mDadProxy = mDeps.getDadProxy(getHandler(), mInterfaceParams);
709 }
710
markchien74a4fa92019-09-09 20:50:49 +0800711 return true;
712 }
713
714 private void stopIPv6() {
715 mInterfaceParams = null;
716 setRaParams(null);
717
718 if (mRaDaemon != null) {
719 mRaDaemon.stop();
720 mRaDaemon = null;
721 }
Tyler Wear90e40632020-03-13 11:38:38 -0700722
723 if (mDadProxy != null) {
724 mDadProxy.stop();
725 mDadProxy = null;
726 }
markchien74a4fa92019-09-09 20:50:49 +0800727 }
728
729 // IPv6TetheringCoordinator sends updates with carefully curated IPv6-only
730 // LinkProperties. These have extraneous data filtered out and only the
731 // necessary prefixes included (per its prefix distribution policy).
732 //
733 // TODO: Evaluate using a data structure than is more directly suited to
734 // communicating only the relevant information.
markchiend63c4f32020-05-21 17:38:28 +0800735 private void updateUpstreamIPv6LinkProperties(LinkProperties v6only, int ttlAdjustment) {
markchien74a4fa92019-09-09 20:50:49 +0800736 if (mRaDaemon == null) return;
737
738 // Avoid unnecessary work on spurious updates.
739 if (Objects.equals(mLastIPv6LinkProperties, v6only)) {
740 return;
741 }
742
743 RaParams params = null;
Tyler Wear90e40632020-03-13 11:38:38 -0700744 String upstreamIface = null;
745 InterfaceParams upstreamIfaceParams = null;
746 int upstreamIfIndex = 0;
markchien74a4fa92019-09-09 20:50:49 +0800747
748 if (v6only != null) {
Tyler Wear90e40632020-03-13 11:38:38 -0700749 upstreamIface = v6only.getInterfaceName();
750 upstreamIfaceParams = mDeps.getInterfaceParams(upstreamIface);
751 if (upstreamIfaceParams != null) {
752 upstreamIfIndex = upstreamIfaceParams.index;
753 }
markchien74a4fa92019-09-09 20:50:49 +0800754 params = new RaParams();
Maciej Żenczykowskif99d2a12020-05-28 03:21:31 -0700755 params.mtu = v6only.getMtu();
markchien74a4fa92019-09-09 20:50:49 +0800756 params.hasDefaultRoute = v6only.hasIpv6DefaultRoute();
757
markchiend63c4f32020-05-21 17:38:28 +0800758 if (params.hasDefaultRoute) params.hopLimit = getHopLimit(upstreamIface, ttlAdjustment);
markchien74a4fa92019-09-09 20:50:49 +0800759
760 for (LinkAddress linkAddr : v6only.getLinkAddresses()) {
761 if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue;
762
763 final IpPrefix prefix = new IpPrefix(
764 linkAddr.getAddress(), linkAddr.getPrefixLength());
765 params.prefixes.add(prefix);
766
767 final Inet6Address dnsServer = getLocalDnsIpFor(prefix);
768 if (dnsServer != null) {
769 params.dnses.add(dnsServer);
770 }
771 }
772 }
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900773
Lorenzo Colittidc6715c2021-02-09 23:12:37 +0900774 // Add upstream index to name mapping. See the comment of the interface mapping update in
775 // CMD_TETHER_CONNECTION_CHANGED. Adding the mapping update here to the avoid potential
776 // timing issue. It prevents that the IPv6 capability is updated later than
777 // CMD_TETHER_CONNECTION_CHANGED.
778 mBpfCoordinator.addUpstreamNameToLookupTable(upstreamIfIndex, upstreamIface);
779
markchien74a4fa92019-09-09 20:50:49 +0800780 // If v6only is null, we pass in null to setRaParams(), which handles
781 // deprecation of any existing RA data.
782
783 setRaParams(params);
markchien7dc1b4d2021-07-13 17:12:56 +0800784 // Be aware that updateIpv6ForwardingRules use mLastIPv6LinkProperties, so this line should
785 // be eariler than updateIpv6ForwardingRules.
786 // TODO: avoid this dependencies and move this logic into BpfCoordinator.
markchien74a4fa92019-09-09 20:50:49 +0800787 mLastIPv6LinkProperties = v6only;
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900788
Tyler Wear90e40632020-03-13 11:38:38 -0700789 updateIpv6ForwardingRules(mLastIPv6UpstreamIfindex, upstreamIfIndex, null);
790 mLastIPv6UpstreamIfindex = upstreamIfIndex;
791 if (mDadProxy != null) {
792 mDadProxy.setUpstreamIface(upstreamIfaceParams);
793 }
markchien74a4fa92019-09-09 20:50:49 +0800794 }
795
Xiao Ma4455d6b2020-04-09 10:13:44 +0900796 private void removeRoutesFromLocalNetwork(@NonNull final List<RouteInfo> toBeRemoved) {
Chalard Jeanadcec9e2021-10-29 15:57:22 +0900797 final int removalFailures = NetdUtils.removeRoutesFromLocalNetwork(
Xiao Ma4455d6b2020-04-09 10:13:44 +0900798 mNetd, toBeRemoved);
799 if (removalFailures > 0) {
800 mLog.e(String.format("Failed to remove %d IPv6 routes from local table.",
801 removalFailures));
802 }
803
804 for (RouteInfo route : toBeRemoved) mLinkProperties.removeRoute(route);
805 }
806
807 private void addRoutesToLocalNetwork(@NonNull final List<RouteInfo> toBeAdded) {
808 try {
809 // It's safe to call networkAddInterface() even if
810 // the interface is already in the local_network.
811 mNetd.networkAddInterface(INetd.LOCAL_NET_ID, mIfaceName);
812 try {
813 // Add routes from local network. Note that adding routes that
814 // already exist does not cause an error (EEXIST is silently ignored).
Chalard Jeanadcec9e2021-10-29 15:57:22 +0900815 NetdUtils.addRoutesToLocalNetwork(mNetd, mIfaceName, toBeAdded);
Xiao Ma4455d6b2020-04-09 10:13:44 +0900816 } catch (IllegalStateException e) {
817 mLog.e("Failed to add IPv4/v6 routes to local table: " + e);
818 return;
819 }
820 } catch (ServiceSpecificException | RemoteException e) {
821 mLog.e("Failed to add " + mIfaceName + " to local table: ", e);
822 return;
823 }
824
825 for (RouteInfo route : toBeAdded) mLinkProperties.addRoute(route);
826 }
827
markchien74a4fa92019-09-09 20:50:49 +0800828 private void configureLocalIPv6Routes(
829 HashSet<IpPrefix> deprecatedPrefixes, HashSet<IpPrefix> newPrefixes) {
830 // [1] Remove the routes that are deprecated.
831 if (!deprecatedPrefixes.isEmpty()) {
Xiao Ma4455d6b2020-04-09 10:13:44 +0900832 removeRoutesFromLocalNetwork(getLocalRoutesFor(mIfaceName, deprecatedPrefixes));
markchien74a4fa92019-09-09 20:50:49 +0800833 }
834
835 // [2] Add only the routes that have not previously been added.
836 if (newPrefixes != null && !newPrefixes.isEmpty()) {
837 HashSet<IpPrefix> addedPrefixes = (HashSet) newPrefixes.clone();
838 if (mLastRaParams != null) {
839 addedPrefixes.removeAll(mLastRaParams.prefixes);
840 }
841
842 if (!addedPrefixes.isEmpty()) {
Xiao Ma4455d6b2020-04-09 10:13:44 +0900843 addRoutesToLocalNetwork(getLocalRoutesFor(mIfaceName, addedPrefixes));
markchien74a4fa92019-09-09 20:50:49 +0800844 }
845 }
846 }
847
848 private void configureLocalIPv6Dns(
849 HashSet<Inet6Address> deprecatedDnses, HashSet<Inet6Address> newDnses) {
850 // TODO: Is this really necessary? Can we not fail earlier if INetd cannot be located?
851 if (mNetd == null) {
852 if (newDnses != null) newDnses.clear();
853 mLog.e("No netd service instance available; not setting local IPv6 addresses");
854 return;
855 }
856
857 // [1] Remove deprecated local DNS IP addresses.
858 if (!deprecatedDnses.isEmpty()) {
859 for (Inet6Address dns : deprecatedDnses) {
860 if (!mInterfaceCtrl.removeAddress(dns, RFC7421_PREFIX_LENGTH)) {
861 mLog.e("Failed to remove local dns IP " + dns);
862 }
863
864 mLinkProperties.removeLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH));
865 }
866 }
867
868 // [2] Add only the local DNS IP addresses that have not previously been added.
869 if (newDnses != null && !newDnses.isEmpty()) {
870 final HashSet<Inet6Address> addedDnses = (HashSet) newDnses.clone();
871 if (mLastRaParams != null) {
872 addedDnses.removeAll(mLastRaParams.dnses);
873 }
874
875 for (Inet6Address dns : addedDnses) {
876 if (!mInterfaceCtrl.addAddress(dns, RFC7421_PREFIX_LENGTH)) {
877 mLog.e("Failed to add local dns IP " + dns);
878 newDnses.remove(dns);
879 }
880
881 mLinkProperties.addLinkAddress(new LinkAddress(dns, RFC7421_PREFIX_LENGTH));
882 }
883 }
884
885 try {
886 mNetd.tetherApplyDnsInterfaces();
887 } catch (ServiceSpecificException | RemoteException e) {
888 mLog.e("Failed to update local DNS caching server");
889 if (newDnses != null) newDnses.clear();
890 }
891 }
892
KH Shi59ad35e2023-08-15 07:13:00 +0000893 private void addIpv6DownstreamRule(Ipv6DownstreamRule rule) {
Hungming Chen3d8fa882020-04-12 14:27:18 +0800894 // Theoretically, we don't need this check because IP neighbor monitor doesn't start if BPF
895 // offload is disabled. Add this check just in case.
896 // TODO: Perhaps remove this protection check.
897 if (!mUsingBpfOffload) return;
898
KH Shi59ad35e2023-08-15 07:13:00 +0000899 mBpfCoordinator.addIpv6DownstreamRule(this, rule);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900900 }
901
KH Shi59ad35e2023-08-15 07:13:00 +0000902 private void removeIpv6DownstreamRule(Ipv6DownstreamRule rule) {
Hungming Chen3d8fa882020-04-12 14:27:18 +0800903 // TODO: Perhaps remove this protection check.
KH Shi59ad35e2023-08-15 07:13:00 +0000904 // See the related comment in #addIpv6DownstreamRule.
Hungming Chen3d8fa882020-04-12 14:27:18 +0800905 if (!mUsingBpfOffload) return;
906
KH Shi59ad35e2023-08-15 07:13:00 +0000907 mBpfCoordinator.removeIpv6DownstreamRule(this, rule);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900908 }
909
Lorenzo Colittic61fc082020-02-21 20:21:14 +0900910 private void clearIpv6ForwardingRules() {
Hungming Chen269c0882020-05-06 14:57:35 +0800911 if (!mUsingBpfOffload) return;
912
913 mBpfCoordinator.tetherOffloadRuleClear(this);
Lorenzo Colittic61fc082020-02-21 20:21:14 +0900914 }
915
Hungming Chen269c0882020-05-06 14:57:35 +0800916 private void updateIpv6ForwardingRule(int newIfindex) {
917 // TODO: Perhaps remove this protection check.
KH Shi59ad35e2023-08-15 07:13:00 +0000918 // See the related comment in #addIpv6DownstreamRule.
Hungming Chen269c0882020-05-06 14:57:35 +0800919 if (!mUsingBpfOffload) return;
920
921 mBpfCoordinator.tetherOffloadRuleUpdate(this, newIfindex);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900922 }
923
markchien7dc1b4d2021-07-13 17:12:56 +0800924 private boolean isIpv6VcnNetworkInterface() {
925 if (mLastIPv6LinkProperties == null) return false;
926
927 return isVcnInterface(mLastIPv6LinkProperties.getInterfaceName());
928 }
929
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900930 // Handles all updates to IPv6 forwarding rules. These can currently change only if the upstream
931 // changes or if a neighbor event is received.
932 private void updateIpv6ForwardingRules(int prevUpstreamIfindex, int upstreamIfindex,
933 NeighborEvent e) {
markchien7dc1b4d2021-07-13 17:12:56 +0800934 // If no longer have an upstream or it is virtual network, clear forwarding rules and do
935 // nothing else.
936 // TODO: Rather than always clear rules, ensure whether ipv6 ever enable first.
937 if (upstreamIfindex == 0 || isIpv6VcnNetworkInterface()) {
Lorenzo Colittic61fc082020-02-21 20:21:14 +0900938 clearIpv6ForwardingRules();
939 return;
940 }
941
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900942 // If the upstream interface has changed, remove all rules and re-add them with the new
943 // upstream interface.
944 if (prevUpstreamIfindex != upstreamIfindex) {
Hungming Chen269c0882020-05-06 14:57:35 +0800945 updateIpv6ForwardingRule(upstreamIfindex);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900946 }
947
948 // If we're here to process a NeighborEvent, do so now.
Lorenzo Colittic61fc082020-02-21 20:21:14 +0900949 // mInterfaceParams must be non-null or the event would not have arrived.
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900950 if (e == null) return;
951 if (!(e.ip instanceof Inet6Address) || e.ip.isMulticastAddress()
952 || e.ip.isLoopbackAddress() || e.ip.isLinkLocalAddress()) {
953 return;
954 }
955
Lorenzo Colitti330a9b92020-04-14 14:57:30 +0900956 // When deleting rules, we still need to pass a non-null MAC, even though it's ignored.
KH Shi59ad35e2023-08-15 07:13:00 +0000957 // Do this here instead of in the Ipv6DownstreamRule constructor to ensure that we
958 // never add rules with a null MAC, only delete them.
Lorenzo Colitti330a9b92020-04-14 14:57:30 +0900959 MacAddress dstMac = e.isValid() ? e.macAddr : NULL_MAC_ADDRESS;
KH Shi59ad35e2023-08-15 07:13:00 +0000960 Ipv6DownstreamRule rule = new Ipv6DownstreamRule(upstreamIfindex, mInterfaceParams.index,
961 (Inet6Address) e.ip, mInterfaceParams.macAddr, dstMac);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900962 if (e.isValid()) {
KH Shi59ad35e2023-08-15 07:13:00 +0000963 addIpv6DownstreamRule(rule);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900964 } else {
KH Shi59ad35e2023-08-15 07:13:00 +0000965 removeIpv6DownstreamRule(rule);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900966 }
967 }
968
Hungming Chend71c06e2020-12-21 19:39:49 +0800969 // TODO: consider moving into BpfCoordinator.
970 private void updateClientInfoIpv4(NeighborEvent e) {
971 // TODO: Perhaps remove this protection check.
KH Shi59ad35e2023-08-15 07:13:00 +0000972 // See the related comment in #addIpv6DownstreamRule.
Hungming Chend71c06e2020-12-21 19:39:49 +0800973 if (!mUsingBpfOffload) return;
974
975 if (e == null) return;
976 if (!(e.ip instanceof Inet4Address) || e.ip.isMulticastAddress()
977 || e.ip.isLoopbackAddress() || e.ip.isLinkLocalAddress()) {
978 return;
979 }
980
981 // When deleting clients, IpServer still need to pass a non-null MAC, even though it's
982 // ignored. Do this here instead of in the ClientInfo constructor to ensure that
983 // IpServer never add clients with a null MAC, only delete them.
984 final MacAddress clientMac = e.isValid() ? e.macAddr : NULL_MAC_ADDRESS;
985 final ClientInfo clientInfo = new ClientInfo(mInterfaceParams.index,
986 mInterfaceParams.macAddr, (Inet4Address) e.ip, clientMac);
987 if (e.isValid()) {
988 mBpfCoordinator.tetherOffloadClientAdd(this, clientInfo);
989 } else {
Hungming Chend71c06e2020-12-21 19:39:49 +0800990 mBpfCoordinator.tetherOffloadClientRemove(this, clientInfo);
991 }
992 }
993
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +0900994 private void handleNeighborEvent(NeighborEvent e) {
995 if (mInterfaceParams != null
996 && mInterfaceParams.index == e.ifindex
997 && mInterfaceParams.hasMacAddress) {
998 updateIpv6ForwardingRules(mLastIPv6UpstreamIfindex, mLastIPv6UpstreamIfindex, e);
Hungming Chend71c06e2020-12-21 19:39:49 +0800999 updateClientInfoIpv4(e);
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +09001000 }
1001 }
1002
markchiend63c4f32020-05-21 17:38:28 +08001003 private byte getHopLimit(String upstreamIface, int adjustTTL) {
markchien74a4fa92019-09-09 20:50:49 +08001004 try {
1005 int upstreamHopLimit = Integer.parseUnsignedInt(
1006 mNetd.getProcSysNet(INetd.IPV6, INetd.CONF, upstreamIface, "hop_limit"));
markchiend63c4f32020-05-21 17:38:28 +08001007 upstreamHopLimit = upstreamHopLimit + adjustTTL;
markchien74a4fa92019-09-09 20:50:49 +08001008 // Cap the hop limit to 255.
1009 return (byte) Integer.min(upstreamHopLimit, 255);
1010 } catch (Exception e) {
1011 mLog.e("Failed to find upstream interface hop limit", e);
1012 }
1013 return RaParams.DEFAULT_HOPLIMIT;
1014 }
1015
1016 private void setRaParams(RaParams newParams) {
1017 if (mRaDaemon != null) {
1018 final RaParams deprecatedParams =
1019 RaParams.getDeprecatedRaParams(mLastRaParams, newParams);
1020
1021 configureLocalIPv6Routes(deprecatedParams.prefixes,
1022 (newParams != null) ? newParams.prefixes : null);
1023
1024 configureLocalIPv6Dns(deprecatedParams.dnses,
1025 (newParams != null) ? newParams.dnses : null);
1026
1027 mRaDaemon.buildNewRa(deprecatedParams, newParams);
1028 }
1029
1030 mLastRaParams = newParams;
1031 }
1032
markchien21021ef2021-06-01 10:58:04 +08001033 private void maybeLogMessage(State state, int what) {
1034 switch (what) {
1035 // Suppress some CMD_* to avoid log flooding.
1036 case CMD_IPV6_TETHER_UPDATE:
1037 case CMD_NEIGHBOR_EVENT:
1038 break;
1039 default:
1040 mLog.log(state.getName() + " got "
1041 + sMagicDecoderRing.get(what, Integer.toString(what)));
1042 }
markchien74a4fa92019-09-09 20:50:49 +08001043 }
1044
1045 private void sendInterfaceState(int newInterfaceState) {
1046 mServingMode = newInterfaceState;
1047 mCallback.updateInterfaceState(this, newInterfaceState, mLastError);
1048 sendLinkProperties();
1049 }
1050
1051 private void sendLinkProperties() {
1052 mCallback.updateLinkProperties(this, new LinkProperties(mLinkProperties));
1053 }
1054
1055 private void resetLinkProperties() {
1056 mLinkProperties.clear();
1057 mLinkProperties.setInterfaceName(mIfaceName);
1058 }
1059
markchienf053e4b2020-03-16 21:49:48 +08001060 private void maybeConfigureStaticIp(final TetheringRequestParcel request) {
markchien245352e2020-02-27 20:27:18 +08001061 // Ignore static address configuration if they are invalid or null. In theory, static
1062 // addresses should not be invalid here because TetheringManager do not allow caller to
1063 // specify invalid static address configuration.
1064 if (request == null || request.localIPv4Address == null
1065 || request.staticClientAddress == null || !checkStaticAddressConfiguration(
1066 request.localIPv4Address, request.staticClientAddress)) {
1067 return;
1068 }
markchienf053e4b2020-03-16 21:49:48 +08001069
1070 mStaticIpv4ServerAddr = request.localIPv4Address;
1071 mStaticIpv4ClientAddr = request.staticClientAddress;
1072 }
1073
markchien74a4fa92019-09-09 20:50:49 +08001074 class InitialState extends State {
1075 @Override
1076 public void enter() {
1077 sendInterfaceState(STATE_AVAILABLE);
1078 }
1079
1080 @Override
1081 public boolean processMessage(Message message) {
markchien21021ef2021-06-01 10:58:04 +08001082 maybeLogMessage(this, message.what);
markchien74a4fa92019-09-09 20:50:49 +08001083 switch (message.what) {
1084 case CMD_TETHER_REQUESTED:
Mark74461fc2022-12-14 08:49:40 +00001085 mLastError = TETHER_ERROR_NO_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001086 switch (message.arg1) {
1087 case STATE_LOCAL_ONLY:
markchienf053e4b2020-03-16 21:49:48 +08001088 maybeConfigureStaticIp((TetheringRequestParcel) message.obj);
markchien74a4fa92019-09-09 20:50:49 +08001089 transitionTo(mLocalHotspotState);
1090 break;
1091 case STATE_TETHERED:
markchienf053e4b2020-03-16 21:49:48 +08001092 maybeConfigureStaticIp((TetheringRequestParcel) message.obj);
markchien74a4fa92019-09-09 20:50:49 +08001093 transitionTo(mTetheredState);
1094 break;
1095 default:
1096 mLog.e("Invalid tethering interface serving state specified.");
1097 }
1098 break;
1099 case CMD_INTERFACE_DOWN:
1100 transitionTo(mUnavailableState);
1101 break;
markchien74a4fa92019-09-09 20:50:49 +08001102 default:
1103 return NOT_HANDLED;
1104 }
1105 return HANDLED;
1106 }
1107 }
1108
Hungming Chen46c30b12020-10-15 17:25:36 +08001109 private void startConntrackMonitoring() {
1110 mBpfCoordinator.startMonitoring(this);
1111 }
1112
1113 private void stopConntrackMonitoring() {
1114 mBpfCoordinator.stopMonitoring(this);
1115 }
1116
Mark3ec851e2023-05-19 08:50:38 +00001117 abstract class BaseServingState extends State {
1118 private final int mDesiredInterfaceState;
1119
1120 BaseServingState(int interfaceState) {
1121 mDesiredInterfaceState = interfaceState;
1122 }
1123
markchien74a4fa92019-09-09 20:50:49 +08001124 @Override
1125 public void enter() {
Hungming Chen46c30b12020-10-15 17:25:36 +08001126 startConntrackMonitoring();
1127
Mark3ec851e2023-05-19 08:50:38 +00001128 startServingInterface();
1129
1130 if (mLastError != TETHER_ERROR_NO_ERROR) {
1131 transitionTo(mInitialState);
1132 }
1133
1134 if (DBG) Log.d(TAG, getStateString(mDesiredInterfaceState) + " serve " + mIfaceName);
1135 sendInterfaceState(mDesiredInterfaceState);
1136 }
1137
Mark49649c92023-03-15 06:45:04 +00001138 private int getScope() {
1139 if (mDesiredInterfaceState == STATE_TETHERED) {
1140 return CONNECTIVITY_SCOPE_GLOBAL;
1141 }
1142
1143 return CONNECTIVITY_SCOPE_LOCAL;
1144 }
1145
Mark3ec851e2023-05-19 08:50:38 +00001146 private void startServingInterface() {
Mark49649c92023-03-15 06:45:04 +00001147 if (!startIPv4(getScope())) {
Mark74461fc2022-12-14 08:49:40 +00001148 mLastError = TETHER_ERROR_IFACE_CFG_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001149 return;
1150 }
1151
1152 try {
markchienc9daba32020-02-12 00:19:21 +08001153 NetdUtils.tetherInterface(mNetd, mIfaceName, asIpPrefix(mIpv4Address));
markchien6c2b7cc2020-02-15 11:35:00 +08001154 } catch (RemoteException | ServiceSpecificException | IllegalStateException e) {
Xiao Ma4455d6b2020-04-09 10:13:44 +09001155 mLog.e("Error Tethering", e);
Mark74461fc2022-12-14 08:49:40 +00001156 mLastError = TETHER_ERROR_TETHER_IFACE_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001157 return;
1158 }
1159
1160 if (!startIPv6()) {
1161 mLog.e("Failed to startIPv6");
1162 // TODO: Make this a fatal error once Bluetooth IPv6 is sorted.
1163 return;
1164 }
1165 }
1166
1167 @Override
1168 public void exit() {
1169 // Note that at this point, we're leaving the tethered state. We can fail any
1170 // of these operations, but it doesn't really change that we have to try them
1171 // all in sequence.
1172 stopIPv6();
1173
1174 try {
markchien12c5bb82020-01-07 14:43:17 +08001175 NetdUtils.untetherInterface(mNetd, mIfaceName);
1176 } catch (RemoteException | ServiceSpecificException e) {
Mark74461fc2022-12-14 08:49:40 +00001177 mLastError = TETHER_ERROR_UNTETHER_IFACE_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001178 mLog.e("Failed to untether interface: " + e);
1179 }
1180
1181 stopIPv4();
Hungming Chen46c30b12020-10-15 17:25:36 +08001182 stopConntrackMonitoring();
markchien74a4fa92019-09-09 20:50:49 +08001183
1184 resetLinkProperties();
Wayne Ma6cd440f2022-03-14 18:04:33 +08001185
1186 mTetheringMetrics.updateErrorCode(mInterfaceType, mLastError);
1187 mTetheringMetrics.sendReport(mInterfaceType);
markchien74a4fa92019-09-09 20:50:49 +08001188 }
1189
1190 @Override
1191 public boolean processMessage(Message message) {
markchien74a4fa92019-09-09 20:50:49 +08001192 switch (message.what) {
1193 case CMD_TETHER_UNREQUESTED:
1194 transitionTo(mInitialState);
1195 if (DBG) Log.d(TAG, "Untethered (unrequested)" + mIfaceName);
1196 break;
1197 case CMD_INTERFACE_DOWN:
1198 transitionTo(mUnavailableState);
1199 if (DBG) Log.d(TAG, "Untethered (ifdown)" + mIfaceName);
1200 break;
1201 case CMD_IPV6_TETHER_UPDATE:
markchiend63c4f32020-05-21 17:38:28 +08001202 updateUpstreamIPv6LinkProperties((LinkProperties) message.obj, message.arg1);
markchien74a4fa92019-09-09 20:50:49 +08001203 sendLinkProperties();
1204 break;
1205 case CMD_IP_FORWARDING_ENABLE_ERROR:
1206 case CMD_IP_FORWARDING_DISABLE_ERROR:
1207 case CMD_START_TETHERING_ERROR:
1208 case CMD_STOP_TETHERING_ERROR:
1209 case CMD_SET_DNS_FORWARDERS_ERROR:
Mark74461fc2022-12-14 08:49:40 +00001210 mLastError = TETHER_ERROR_INTERNAL_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001211 transitionTo(mInitialState);
1212 break;
Xiao Ma4455d6b2020-04-09 10:13:44 +09001213 case CMD_NEW_PREFIX_REQUEST:
1214 handleNewPrefixRequest((IpPrefix) message.obj);
1215 break;
markchienc9daba32020-02-12 00:19:21 +08001216 case CMD_NOTIFY_PREFIX_CONFLICT:
1217 mLog.i("restart tethering: " + mInterfaceType);
1218 mCallback.requestEnableTethering(mInterfaceType, false /* enabled */);
1219 transitionTo(mWaitingForRestartState);
1220 break;
markchien74a4fa92019-09-09 20:50:49 +08001221 default:
1222 return false;
1223 }
1224 return true;
1225 }
Mark3ec851e2023-05-19 08:50:38 +00001226
1227 private void handleNewPrefixRequest(@NonNull final IpPrefix currentPrefix) {
1228 if (!currentPrefix.contains(mIpv4Address.getAddress())
1229 || currentPrefix.getPrefixLength() != mIpv4Address.getPrefixLength()) {
1230 Log.e(TAG, "Invalid prefix: " + currentPrefix);
1231 return;
1232 }
1233
1234 final LinkAddress deprecatedLinkAddress = mIpv4Address;
Mark49649c92023-03-15 06:45:04 +00001235 mIpv4Address = requestIpv4Address(getScope(), false);
Mark3ec851e2023-05-19 08:50:38 +00001236 if (mIpv4Address == null) {
1237 mLog.e("Fail to request a new downstream prefix");
1238 return;
1239 }
1240 final Inet4Address srvAddr = (Inet4Address) mIpv4Address.getAddress();
1241
1242 // Add new IPv4 address on the interface.
1243 if (!mInterfaceCtrl.addAddress(srvAddr, currentPrefix.getPrefixLength())) {
1244 mLog.e("Failed to add new IP " + srvAddr);
1245 return;
1246 }
1247
1248 // Remove deprecated routes from local network.
1249 removeRoutesFromLocalNetwork(
1250 Collections.singletonList(getDirectConnectedRoute(deprecatedLinkAddress)));
1251 mLinkProperties.removeLinkAddress(deprecatedLinkAddress);
1252
1253 // Add new routes to local network.
1254 addRoutesToLocalNetwork(
1255 Collections.singletonList(getDirectConnectedRoute(mIpv4Address)));
1256 mLinkProperties.addLinkAddress(mIpv4Address);
1257
1258 // Update local DNS caching server with new IPv4 address, otherwise, dnsmasq doesn't
1259 // listen on the interface configured with new IPv4 address, that results DNS validation
1260 // failure of downstream client even if appropriate routes have been configured.
1261 try {
1262 mNetd.tetherApplyDnsInterfaces();
1263 } catch (ServiceSpecificException | RemoteException e) {
1264 mLog.e("Failed to update local DNS caching server");
1265 return;
1266 }
1267 sendLinkProperties();
1268
1269 // Notify DHCP server that new prefix/route has been applied on IpServer.
1270 final Inet4Address clientAddr = mStaticIpv4ClientAddr == null ? null :
1271 (Inet4Address) mStaticIpv4ClientAddr.getAddress();
1272 final DhcpServingParamsParcel params = makeServingParams(srvAddr /* defaultRouter */,
1273 srvAddr /* dnsServer */, mIpv4Address /* serverLinkAddress */, clientAddr);
1274 try {
1275 mDhcpServer.updateParams(params, new OnHandlerStatusCallback() {
1276 @Override
1277 public void callback(int statusCode) {
1278 if (statusCode != STATUS_SUCCESS) {
1279 mLog.e("Error updating DHCP serving params: " + statusCode);
1280 }
1281 }
1282 });
1283 } catch (RemoteException e) {
1284 mLog.e("Error updating DHCP serving params", e);
1285 }
1286 }
markchien74a4fa92019-09-09 20:50:49 +08001287 }
1288
1289 // Handling errors in BaseServingState.enter() by transitioning is
1290 // problematic because transitioning during a multi-state jump yields
1291 // a Log.wtf(). Ultimately, there should be only one ServingState,
1292 // and forwarding and NAT rules should be handled by a coordinating
1293 // functional element outside of IpServer.
1294 class LocalHotspotState extends BaseServingState {
Mark3ec851e2023-05-19 08:50:38 +00001295 LocalHotspotState() {
1296 super(STATE_LOCAL_ONLY);
markchien74a4fa92019-09-09 20:50:49 +08001297 }
1298
1299 @Override
1300 public boolean processMessage(Message message) {
1301 if (super.processMessage(message)) return true;
1302
markchien21021ef2021-06-01 10:58:04 +08001303 maybeLogMessage(this, message.what);
markchien74a4fa92019-09-09 20:50:49 +08001304 switch (message.what) {
1305 case CMD_TETHER_REQUESTED:
1306 mLog.e("CMD_TETHER_REQUESTED while in local-only hotspot mode.");
1307 break;
1308 case CMD_TETHER_CONNECTION_CHANGED:
1309 // Ignored in local hotspot state.
1310 break;
1311 default:
1312 return false;
1313 }
1314 return true;
1315 }
1316 }
1317
1318 // Handling errors in BaseServingState.enter() by transitioning is
1319 // problematic because transitioning during a multi-state jump yields
1320 // a Log.wtf(). Ultimately, there should be only one ServingState,
1321 // and forwarding and NAT rules should be handled by a coordinating
1322 // functional element outside of IpServer.
1323 class TetheredState extends BaseServingState {
Mark3ec851e2023-05-19 08:50:38 +00001324 TetheredState() {
1325 super(STATE_TETHERED);
markchien74a4fa92019-09-09 20:50:49 +08001326 }
1327
1328 @Override
1329 public void exit() {
1330 cleanupUpstream();
1331 super.exit();
1332 }
1333
Hungming Chena6e78692021-02-08 17:15:35 +08001334 // Note that IPv4 offload rules cleanup is implemented in BpfCoordinator while upstream
1335 // state is null or changed because IPv4 and IPv6 tethering have different code flow
1336 // and behaviour. While upstream is switching from offload supported interface to
1337 // offload non-supportted interface, event CMD_TETHER_CONNECTION_CHANGED calls
1338 // #cleanupUpstreamInterface but #cleanupUpstream because new UpstreamIfaceSet is not null.
1339 // This case won't happen in IPv6 tethering because IPv6 tethering upstream state is
1340 // reported by IPv6TetheringCoordinator. #cleanupUpstream is also called by unwirding
1341 // adding NAT failure. In that case, the IPv4 offload rules are removed by #stopIPv4
1342 // in the state machine. Once there is any case out whish is not covered by previous cases,
1343 // probably consider clearing rules in #cleanupUpstream as well.
markchien74a4fa92019-09-09 20:50:49 +08001344 private void cleanupUpstream() {
1345 if (mUpstreamIfaceSet == null) return;
1346
1347 for (String ifname : mUpstreamIfaceSet.ifnames) cleanupUpstreamInterface(ifname);
1348 mUpstreamIfaceSet = null;
Lorenzo Colittic61fc082020-02-21 20:21:14 +09001349 clearIpv6ForwardingRules();
markchien74a4fa92019-09-09 20:50:49 +08001350 }
1351
1352 private void cleanupUpstreamInterface(String upstreamIface) {
1353 // Note that we don't care about errors here.
1354 // Sometimes interfaces are gone before we get
1355 // to remove their rules, which generates errors.
1356 // Just do the best we can.
Hungming Chen3dbd4a12021-02-25 17:52:02 +08001357 mBpfCoordinator.maybeDetachProgram(mIfaceName, upstreamIface);
markchien74a4fa92019-09-09 20:50:49 +08001358 try {
markchien12c5bb82020-01-07 14:43:17 +08001359 mNetd.ipfwdRemoveInterfaceForward(mIfaceName, upstreamIface);
1360 } catch (RemoteException | ServiceSpecificException e) {
1361 mLog.e("Exception in ipfwdRemoveInterfaceForward: " + e.toString());
markchien74a4fa92019-09-09 20:50:49 +08001362 }
1363 try {
markchien12c5bb82020-01-07 14:43:17 +08001364 mNetd.tetherRemoveForward(mIfaceName, upstreamIface);
1365 } catch (RemoteException | ServiceSpecificException e) {
1366 mLog.e("Exception in disableNat: " + e.toString());
markchien74a4fa92019-09-09 20:50:49 +08001367 }
1368 }
1369
1370 @Override
1371 public boolean processMessage(Message message) {
1372 if (super.processMessage(message)) return true;
1373
markchien21021ef2021-06-01 10:58:04 +08001374 maybeLogMessage(this, message.what);
markchien74a4fa92019-09-09 20:50:49 +08001375 switch (message.what) {
1376 case CMD_TETHER_REQUESTED:
1377 mLog.e("CMD_TETHER_REQUESTED while already tethering.");
1378 break;
1379 case CMD_TETHER_CONNECTION_CHANGED:
1380 final InterfaceSet newUpstreamIfaceSet = (InterfaceSet) message.obj;
1381 if (noChangeInUpstreamIfaceSet(newUpstreamIfaceSet)) {
1382 if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
1383 break;
1384 }
1385
1386 if (newUpstreamIfaceSet == null) {
1387 cleanupUpstream();
1388 break;
1389 }
1390
1391 for (String removed : upstreamInterfacesRemoved(newUpstreamIfaceSet)) {
1392 cleanupUpstreamInterface(removed);
1393 }
1394
1395 final Set<String> added = upstreamInterfacesAdd(newUpstreamIfaceSet);
1396 // This makes the call to cleanupUpstream() in the error
1397 // path for any interface neatly cleanup all the interfaces.
1398 mUpstreamIfaceSet = newUpstreamIfaceSet;
1399
1400 for (String ifname : added) {
Lorenzo Colittidc6715c2021-02-09 23:12:37 +09001401 // Add upstream index to name mapping for the tether stats usage in the
1402 // coordinator. Although this mapping could be added by both class
1403 // Tethering and IpServer, adding mapping from IpServer guarantees that
1404 // the mapping is added before adding forwarding rules. That is because
1405 // there are different state machines in both classes. It is hard to
1406 // guarantee the link property update order between multiple state machines.
1407 // Note that both IPv4 and IPv6 interface may be added because
1408 // Tethering::setUpstreamNetwork calls getTetheringInterfaces which merges
1409 // IPv4 and IPv6 interface name (if any) into an InterfaceSet. The IPv6
1410 // capability may be updated later. In that case, IPv6 interface mapping is
1411 // updated in updateUpstreamIPv6LinkProperties.
1412 if (!ifname.startsWith("v4-")) { // ignore clat interfaces
1413 final InterfaceParams upstreamIfaceParams =
1414 mDeps.getInterfaceParams(ifname);
1415 if (upstreamIfaceParams != null) {
1416 mBpfCoordinator.addUpstreamNameToLookupTable(
1417 upstreamIfaceParams.index, ifname);
1418 }
1419 }
1420
Hungming Chen3dbd4a12021-02-25 17:52:02 +08001421 mBpfCoordinator.maybeAttachProgram(mIfaceName, ifname);
markchien74a4fa92019-09-09 20:50:49 +08001422 try {
markchien12c5bb82020-01-07 14:43:17 +08001423 mNetd.tetherAddForward(mIfaceName, ifname);
1424 mNetd.ipfwdAddInterfaceForward(mIfaceName, ifname);
1425 } catch (RemoteException | ServiceSpecificException e) {
1426 mLog.e("Exception enabling NAT: " + e.toString());
markchien74a4fa92019-09-09 20:50:49 +08001427 cleanupUpstream();
Mark74461fc2022-12-14 08:49:40 +00001428 mLastError = TETHER_ERROR_ENABLE_FORWARDING_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001429 transitionTo(mInitialState);
1430 return true;
1431 }
1432 }
1433 break;
Lorenzo Colitti5e15d7b2020-02-14 01:06:35 +09001434 case CMD_NEIGHBOR_EVENT:
1435 handleNeighborEvent((NeighborEvent) message.obj);
1436 break;
markchien74a4fa92019-09-09 20:50:49 +08001437 default:
1438 return false;
1439 }
1440 return true;
1441 }
1442
1443 private boolean noChangeInUpstreamIfaceSet(InterfaceSet newIfaces) {
1444 if (mUpstreamIfaceSet == null && newIfaces == null) return true;
1445 if (mUpstreamIfaceSet != null && newIfaces != null) {
1446 return mUpstreamIfaceSet.equals(newIfaces);
1447 }
1448 return false;
1449 }
1450
1451 private Set<String> upstreamInterfacesRemoved(InterfaceSet newIfaces) {
1452 if (mUpstreamIfaceSet == null) return new HashSet<>();
1453
1454 final HashSet<String> removed = new HashSet<>(mUpstreamIfaceSet.ifnames);
1455 removed.removeAll(newIfaces.ifnames);
1456 return removed;
1457 }
1458
1459 private Set<String> upstreamInterfacesAdd(InterfaceSet newIfaces) {
1460 final HashSet<String> added = new HashSet<>(newIfaces.ifnames);
1461 if (mUpstreamIfaceSet != null) added.removeAll(mUpstreamIfaceSet.ifnames);
1462 return added;
1463 }
1464 }
1465
1466 /**
1467 * This state is terminal for the per interface state machine. At this
Chiachang Wang14aaefc2020-07-29 12:05:04 +08001468 * point, the tethering main state machine should have removed this interface
markchien74a4fa92019-09-09 20:50:49 +08001469 * specific state machine from its list of possible recipients of
1470 * tethering requests. The state machine itself will hang around until
1471 * the garbage collector finds it.
1472 */
1473 class UnavailableState extends State {
1474 @Override
1475 public void enter() {
h.zhangd244bd02020-06-14 14:46:54 +08001476 mIpNeighborMonitor.stop();
Mark74461fc2022-12-14 08:49:40 +00001477 mLastError = TETHER_ERROR_NO_ERROR;
markchien74a4fa92019-09-09 20:50:49 +08001478 sendInterfaceState(STATE_UNAVAILABLE);
1479 }
1480 }
1481
markchienc9daba32020-02-12 00:19:21 +08001482 class WaitingForRestartState extends State {
1483 @Override
1484 public boolean processMessage(Message message) {
markchien21021ef2021-06-01 10:58:04 +08001485 maybeLogMessage(this, message.what);
markchienc9daba32020-02-12 00:19:21 +08001486 switch (message.what) {
1487 case CMD_TETHER_UNREQUESTED:
1488 transitionTo(mInitialState);
1489 mLog.i("Untethered (unrequested) and restarting " + mIfaceName);
1490 mCallback.requestEnableTethering(mInterfaceType, true /* enabled */);
1491 break;
1492 case CMD_INTERFACE_DOWN:
1493 transitionTo(mUnavailableState);
Lorenzo Colitti8a36c292021-04-13 17:17:44 +09001494 mLog.i("Untethered (interface down) and restarting " + mIfaceName);
markchienc9daba32020-02-12 00:19:21 +08001495 mCallback.requestEnableTethering(mInterfaceType, true /* enabled */);
1496 break;
1497 default:
1498 return false;
1499 }
1500 return true;
1501 }
1502 }
1503
markchien74a4fa92019-09-09 20:50:49 +08001504 // Accumulate routes representing "prefixes to be assigned to the local
1505 // interface", for subsequent modification of local_network routing.
1506 private static ArrayList<RouteInfo> getLocalRoutesFor(
1507 String ifname, HashSet<IpPrefix> prefixes) {
1508 final ArrayList<RouteInfo> localRoutes = new ArrayList<RouteInfo>();
1509 for (IpPrefix ipp : prefixes) {
markchien6cf0e552019-12-06 15:24:53 +08001510 localRoutes.add(new RouteInfo(ipp, null, ifname, RTN_UNICAST));
markchien74a4fa92019-09-09 20:50:49 +08001511 }
1512 return localRoutes;
1513 }
1514
1515 // Given a prefix like 2001:db8::/64 return an address like 2001:db8::1.
1516 private static Inet6Address getLocalDnsIpFor(IpPrefix localPrefix) {
1517 final byte[] dnsBytes = localPrefix.getRawAddress();
1518 dnsBytes[dnsBytes.length - 1] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1));
1519 try {
1520 return Inet6Address.getByAddress(null, dnsBytes, 0);
1521 } catch (UnknownHostException e) {
markchien6cf0e552019-12-06 15:24:53 +08001522 Log.wtf(TAG, "Failed to construct Inet6Address from: " + localPrefix);
markchien74a4fa92019-09-09 20:50:49 +08001523 return null;
1524 }
1525 }
1526
1527 private static byte getRandomSanitizedByte(byte dflt, byte... excluded) {
1528 final byte random = (byte) (new Random()).nextInt();
1529 for (int value : excluded) {
1530 if (random == value) return dflt;
1531 }
1532 return random;
1533 }
1534}