1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import static com.android.net.module.util.NetworkStackConstants.ARP_REPLY;
import static com.android.net.module.util.NetworkStackConstants.ARP_REQUEST;
import static com.android.net.module.util.NetworkStackConstants.ETHER_ADDR_LEN;
import static com.android.net.module.util.NetworkStackConstants.ETHER_BROADCAST;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import android.net.dhcp.DhcpAckPacket;
import android.net.dhcp.DhcpOfferPacket;
import android.net.dhcp.DhcpPacket;
import android.util.ArrayMap;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.networkstack.arp.ArpPacket;
import com.android.testutils.TapPacketReader;
import java.net.Inet4Address;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
/**
* A class simulate tethered client. When caller create TetheringTester, it would connect to
* tethering module that do the dhcp and slaac to obtain ipv4 and ipv6 address. Then caller can
* send/receive packets by this class.
*/
public final class TetheringTester {
private static final String TAG = TetheringTester.class.getSimpleName();
private static final int PACKET_READ_TIMEOUT_MS = 100;
private static final int DHCP_DISCOVER_ATTEMPTS = 10;
private static final byte[] DHCP_REQUESTED_PARAMS = new byte[] {
DhcpPacket.DHCP_SUBNET_MASK,
DhcpPacket.DHCP_ROUTER,
DhcpPacket.DHCP_DNS_SERVER,
DhcpPacket.DHCP_LEASE_TIME,
};
public static final String DHCP_HOSTNAME = "testhostname";
private final ArrayMap<MacAddress, TetheredDevice> mTetheredDevices;
private final TapPacketReader mDownstreamReader;
public TetheringTester(TapPacketReader downstream) {
if (downstream == null) fail("Downstream reader could not be NULL");
mDownstreamReader = downstream;
mTetheredDevices = new ArrayMap<>();
}
public TetheredDevice createTetheredDevice(MacAddress macAddr) throws Exception {
if (mTetheredDevices.get(macAddr) != null) {
fail("Tethered device already created");
}
TetheredDevice tethered = new TetheredDevice(macAddr);
mTetheredDevices.put(macAddr, tethered);
return tethered;
}
public class TetheredDevice {
public final MacAddress macAddr;
public final MacAddress routerMacAddr;
public final Inet4Address ipv4Addr;
private TetheredDevice(MacAddress mac) throws Exception {
macAddr = mac;
DhcpResults dhcpResults = runDhcp(macAddr.toByteArray());
ipv4Addr = (Inet4Address) dhcpResults.ipAddress.getAddress();
routerMacAddr = getRouterMacAddressFromArp(ipv4Addr, macAddr,
dhcpResults.serverAddress);
}
}
/** Simulate dhcp client to obtain ipv4 address. */
public DhcpResults runDhcp(byte[] clientMacAddr)
throws Exception {
// We have to retransmit DHCP requests because IpServer declares itself to be ready before
// its DhcpServer is actually started. TODO: fix this race and remove this loop.
DhcpPacket offerPacket = null;
for (int i = 0; i < DHCP_DISCOVER_ATTEMPTS; i++) {
Log.d(TAG, "Sending DHCP discover");
sendDhcpDiscover(clientMacAddr);
offerPacket = getNextDhcpPacket();
if (offerPacket instanceof DhcpOfferPacket) break;
}
if (!(offerPacket instanceof DhcpOfferPacket)) {
throw new TimeoutException("No DHCPOFFER received on interface within timeout");
}
sendDhcpRequest(offerPacket, clientMacAddr);
DhcpPacket ackPacket = getNextDhcpPacket();
if (!(ackPacket instanceof DhcpAckPacket)) {
throw new TimeoutException("No DHCPACK received on interface within timeout");
}
return ackPacket.toDhcpResults();
}
private void sendDhcpDiscover(byte[] macAddress) throws Exception {
ByteBuffer packet = DhcpPacket.buildDiscoverPacket(DhcpPacket.ENCAP_L2,
new Random().nextInt() /* transactionId */, (short) 0 /* secs */,
macAddress, false /* unicast */, DHCP_REQUESTED_PARAMS,
false /* rapid commit */, DHCP_HOSTNAME);
mDownstreamReader.sendResponse(packet);
}
private void sendDhcpRequest(DhcpPacket offerPacket, byte[] macAddress)
throws Exception {
DhcpResults results = offerPacket.toDhcpResults();
Inet4Address clientIp = (Inet4Address) results.ipAddress.getAddress();
Inet4Address serverIdentifier = results.serverAddress;
ByteBuffer packet = DhcpPacket.buildRequestPacket(DhcpPacket.ENCAP_L2,
0 /* transactionId */, (short) 0 /* secs */, DhcpPacket.INADDR_ANY /* clientIp */,
false /* broadcast */, macAddress, clientIp /* requestedIpAddress */,
serverIdentifier, DHCP_REQUESTED_PARAMS, DHCP_HOSTNAME);
mDownstreamReader.sendResponse(packet);
}
private DhcpPacket getNextDhcpPacket() throws Exception {
final byte[] packet = getNextMatchedPacket((p) -> {
// Test whether this is DHCP packet.
try {
DhcpPacket.decodeFullPacket(p, p.length, DhcpPacket.ENCAP_L2);
} catch (DhcpPacket.ParseException e) {
// Not a DHCP packet.
return false;
}
return true;
});
return packet == null ? null :
DhcpPacket.decodeFullPacket(packet, packet.length, DhcpPacket.ENCAP_L2);
}
@Nullable
private ArpPacket parseArpPacket(final byte[] packet) {
try {
return ArpPacket.parseArpPacket(packet, packet.length);
} catch (ArpPacket.ParseException e) {
return null;
}
}
private void maybeReplyArp(byte[] packet) {
ByteBuffer buf = ByteBuffer.wrap(packet);
final ArpPacket arpPacket = parseArpPacket(packet);
if (arpPacket == null || arpPacket.opCode != ARP_REQUEST) return;
for (int i = 0; i < mTetheredDevices.size(); i++) {
TetheredDevice tethered = mTetheredDevices.valueAt(i);
if (!arpPacket.targetIp.equals(tethered.ipv4Addr)) continue;
final ByteBuffer arpReply = ArpPacket.buildArpPacket(
arpPacket.senderHwAddress.toByteArray() /* dst */,
tethered.macAddr.toByteArray() /* srcMac */,
arpPacket.senderIp.getAddress() /* target IP */,
arpPacket.senderHwAddress.toByteArray() /* target HW address */,
tethered.ipv4Addr.getAddress() /* sender IP */,
(short) ARP_REPLY);
try {
sendPacket(arpReply);
} catch (Exception e) {
fail("Failed to reply ARP for " + tethered.ipv4Addr);
}
return;
}
}
private MacAddress getRouterMacAddressFromArp(final Inet4Address tetherIp,
final MacAddress tetherMac, final Inet4Address routerIp) throws Exception {
final ByteBuffer arpProbe = ArpPacket.buildArpPacket(ETHER_BROADCAST /* dst */,
tetherMac.toByteArray() /* srcMac */, routerIp.getAddress() /* target IP */,
new byte[ETHER_ADDR_LEN] /* target HW address */,
tetherIp.getAddress() /* sender IP */, (short) ARP_REQUEST);
sendPacket(arpProbe);
final byte[] packet = getNextMatchedPacket((p) -> {
final ArpPacket arpPacket = parseArpPacket(p);
if (arpPacket == null || arpPacket.opCode != ARP_REPLY) return false;
return arpPacket.targetIp.equals(tetherIp);
});
if (packet != null) {
Log.d(TAG, "Get Mac address from ARP");
final ArpPacket arpReply = ArpPacket.parseArpPacket(packet, packet.length);
return arpReply.senderHwAddress;
}
fail("Could not get ARP packet");
return null;
}
public void sendPacket(ByteBuffer packet) throws Exception {
mDownstreamReader.sendResponse(packet);
}
public byte[] getNextMatchedPacket(Predicate<byte[]> filter) {
byte[] packet;
while ((packet = mDownstreamReader.poll(PACKET_READ_TIMEOUT_MS)) != null) {
if (filter.test(packet)) return packet;
maybeReplyArp(packet);
}
return null;
}
public void verifyUpload(final RemoteResponder dst, final ByteBuffer packet,
final Predicate<byte[]> filter) throws Exception {
sendPacket(packet);
assertNotNull("Upload fail", dst.getNextMatchedPacket(filter));
}
public static class RemoteResponder {
final TapPacketReader mUpstreamReader;
public RemoteResponder(TapPacketReader reader) {
mUpstreamReader = reader;
}
public void sendPacket(ByteBuffer packet) throws Exception {
mUpstreamReader.sendResponse(packet);
}
public byte[] getNextMatchedPacket(Predicate<byte[]> filter) throws Exception {
return mUpstreamReader.poll(PACKET_READ_TIMEOUT_MS, filter);
}
public void verifyDownload(final TetheringTester dst, final ByteBuffer packet,
final Predicate<byte[]> filter) throws Exception {
sendPacket(packet);
assertNotNull("Download fail", dst.getNextMatchedPacket(filter));
}
}
}
|