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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
|
/*
* Copyright (C) 2015 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.dhcp;
import static android.net.dhcp.DhcpPacket.DHCP_BROADCAST_ADDRESS;
import static android.net.dhcp.DhcpPacket.DHCP_DNS_SERVER;
import static android.net.dhcp.DhcpPacket.DHCP_DOMAIN_NAME;
import static android.net.dhcp.DhcpPacket.DHCP_LEASE_TIME;
import static android.net.dhcp.DhcpPacket.DHCP_MTU;
import static android.net.dhcp.DhcpPacket.DHCP_REBINDING_TIME;
import static android.net.dhcp.DhcpPacket.DHCP_RENEWAL_TIME;
import static android.net.dhcp.DhcpPacket.DHCP_ROUTER;
import static android.net.dhcp.DhcpPacket.DHCP_SUBNET_MASK;
import static android.net.dhcp.DhcpPacket.DHCP_VENDOR_INFO;
import static android.net.dhcp.DhcpPacket.INADDR_ANY;
import static android.net.dhcp.DhcpPacket.INADDR_BROADCAST;
import static android.net.dhcp.DhcpPacket.INFINITE_LEASE;
import static android.net.util.NetworkStackUtils.closeSocketQuietly;
import static android.net.util.SocketUtils.makePacketSocketAddress;
import static android.system.OsConstants.AF_INET;
import static android.system.OsConstants.AF_PACKET;
import static android.system.OsConstants.ETH_P_IP;
import static android.system.OsConstants.IPPROTO_UDP;
import static android.system.OsConstants.SOCK_DGRAM;
import static android.system.OsConstants.SOCK_RAW;
import static android.system.OsConstants.SOL_SOCKET;
import static android.system.OsConstants.SO_BROADCAST;
import static android.system.OsConstants.SO_RCVBUF;
import static android.system.OsConstants.SO_REUSEADDR;
import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ANY;
import android.annotation.NonNull;
import android.content.Context;
import android.net.DhcpResults;
import android.net.InetAddresses;
import android.net.NetworkStackIpMemoryStore;
import android.net.TrafficStats;
import android.net.ip.IpClient;
import android.net.ipmemorystore.NetworkAttributes;
import android.net.ipmemorystore.OnNetworkAttributesRetrievedListener;
import android.net.ipmemorystore.OnStatusListener;
import android.net.metrics.DhcpClientEvent;
import android.net.metrics.DhcpErrorEvent;
import android.net.metrics.IpConnectivityLog;
import android.net.util.InterfaceParams;
import android.net.util.NetworkStackUtils;
import android.net.util.SocketUtils;
import android.os.Message;
import android.os.SystemClock;
import android.system.ErrnoException;
import android.system.Os;
import android.util.EventLog;
import android.util.Log;
import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.HexDump;
import com.android.internal.util.MessageUtils;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.internal.util.TrafficStatsConstants;
import com.android.internal.util.WakeupMessage;
import com.android.networkstack.R;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Random;
/**
* A DHCPv4 client.
*
* Written to behave similarly to the DhcpStateMachine + dhcpcd 5.5.6 combination used in Android
* 5.1 and below, as configured on Nexus 6. The interface is the same as DhcpStateMachine.
*
* TODO:
*
* - Exponential backoff when receiving NAKs (not specified by the RFC, but current behaviour).
* - Support persisting lease state and support INIT-REBOOT. Android 5.1 does this, but it does not
* do so correctly: instead of requesting the lease last obtained on a particular network (e.g., a
* given SSID), it requests the last-leased IP address on the same interface, causing a delay if
* the server NAKs or a timeout if it doesn't.
*
* Known differences from current behaviour:
*
* - Does not request the "static routes" option.
* - Does not support BOOTP servers. DHCP has been around since 1993, should be everywhere now.
* - Requests the "broadcast" option, but does nothing with it.
* - Rejects invalid subnet masks such as 255.255.255.1 (current code treats that as 255.255.255.0).
*
* @hide
*/
public class DhcpClient extends StateMachine {
private static final String TAG = "DhcpClient";
private static final boolean DBG = true;
private static final boolean STATE_DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final boolean MSG_DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final boolean PACKET_DBG = Log.isLoggable(TAG, Log.DEBUG);
// Metrics events: must be kept in sync with server-side aggregation code.
/** Represents transitions from DhcpInitState to DhcpBoundState */
private static final String EVENT_INITIAL_BOUND = "InitialBoundState";
/** Represents transitions from and to DhcpBoundState via DhcpRenewingState */
private static final String EVENT_RENEWING_BOUND = "RenewingBoundState";
// Timers and timeouts.
private static final int SECONDS = 1000;
private static final int FIRST_TIMEOUT_MS = 2 * SECONDS;
private static final int MAX_TIMEOUT_MS = 128 * SECONDS;
private static final int IPMEMORYSTORE_TIMEOUT_MS = 1 * SECONDS;
// This is not strictly needed, since the client is asynchronous and implements exponential
// backoff. It's maintained for backwards compatibility with the previous DHCP code, which was
// a blocking operation with a 30-second timeout. We pick 36 seconds so we can send packets at
// t=0, t=2, t=6, t=14, t=30, allowing for 10% jitter.
private static final int DHCP_TIMEOUT_MS = 36 * SECONDS;
// DhcpClient uses IpClient's handler.
private static final int PUBLIC_BASE = IpClient.DHCPCLIENT_CMD_BASE;
// Below constants are picked up by MessageUtils and exempt from ProGuard optimization.
/* Commands from controller to start/stop DHCP */
public static final int CMD_START_DHCP = PUBLIC_BASE + 1;
public static final int CMD_STOP_DHCP = PUBLIC_BASE + 2;
/* Notification from DHCP state machine prior to DHCP discovery/renewal */
public static final int CMD_PRE_DHCP_ACTION = PUBLIC_BASE + 3;
/* Notification from DHCP state machine post DHCP discovery/renewal. Indicates
* success/failure */
public static final int CMD_POST_DHCP_ACTION = PUBLIC_BASE + 4;
/* Notification from DHCP state machine before quitting */
public static final int CMD_ON_QUIT = PUBLIC_BASE + 5;
/* Command from controller to indicate DHCP discovery/renewal can continue
* after pre DHCP action is complete */
public static final int CMD_PRE_DHCP_ACTION_COMPLETE = PUBLIC_BASE + 6;
/* Command and event notification to/from IpManager requesting the setting
* (or clearing) of an IPv4 LinkAddress.
*/
public static final int CMD_CLEAR_LINKADDRESS = PUBLIC_BASE + 7;
public static final int CMD_CONFIGURE_LINKADDRESS = PUBLIC_BASE + 8;
public static final int EVENT_LINKADDRESS_CONFIGURED = PUBLIC_BASE + 9;
/* Message.arg1 arguments to CMD_POST_DHCP_ACTION notification */
public static final int DHCP_SUCCESS = 1;
public static final int DHCP_FAILURE = 2;
// Internal messages.
private static final int PRIVATE_BASE = IpClient.DHCPCLIENT_CMD_BASE + 100;
private static final int CMD_KICK = PRIVATE_BASE + 1;
private static final int CMD_RECEIVED_PACKET = PRIVATE_BASE + 2;
private static final int CMD_TIMEOUT = PRIVATE_BASE + 3;
private static final int CMD_RENEW_DHCP = PRIVATE_BASE + 4;
private static final int CMD_REBIND_DHCP = PRIVATE_BASE + 5;
private static final int CMD_EXPIRE_DHCP = PRIVATE_BASE + 6;
private static final int EVENT_CONFIGURATION_TIMEOUT = PRIVATE_BASE + 7;
private static final int EVENT_CONFIGURATION_OBTAINED = PRIVATE_BASE + 8;
private static final int EVENT_CONFIGURATION_INVALID = PRIVATE_BASE + 9;
// constant to represent this DHCP lease has been expired.
@VisibleForTesting
public static final long EXPIRED_LEASE = 1L;
// For message logging.
private static final Class[] sMessageClasses = { DhcpClient.class };
private static final SparseArray<String> sMessageNames =
MessageUtils.findMessageNames(sMessageClasses);
// DHCP parameters that we request.
/* package */ static final byte[] REQUESTED_PARAMS = new byte[] {
DHCP_SUBNET_MASK,
DHCP_ROUTER,
DHCP_DNS_SERVER,
DHCP_DOMAIN_NAME,
DHCP_MTU,
DHCP_BROADCAST_ADDRESS, // TODO: currently ignored.
DHCP_LEASE_TIME,
DHCP_RENEWAL_TIME,
DHCP_REBINDING_TIME,
DHCP_VENDOR_INFO,
};
// DHCP flag that means "yes, we support unicast."
private static final boolean DO_UNICAST = false;
// System services / libraries we use.
private final Context mContext;
private final Random mRandom;
private final IpConnectivityLog mMetricsLog = new IpConnectivityLog();
// Sockets.
// - We use a packet socket to receive, because servers send us packets bound for IP addresses
// which we have not yet configured, and the kernel protocol stack drops these.
// - We use a UDP socket to send, so the kernel handles ARP and routing for us (DHCP servers can
// be off-link as well as on-link).
private FileDescriptor mPacketSock;
private FileDescriptor mUdpSock;
private ReceiveThread mReceiveThread;
// State variables.
private final StateMachine mController;
private final WakeupMessage mKickAlarm;
private final WakeupMessage mTimeoutAlarm;
private final WakeupMessage mRenewAlarm;
private final WakeupMessage mRebindAlarm;
private final WakeupMessage mExpiryAlarm;
private final String mIfaceName;
private boolean mRegisteredForPreDhcpNotification;
private InterfaceParams mIface;
// TODO: MacAddress-ify more of this class hierarchy.
private byte[] mHwAddr;
private SocketAddress mInterfaceBroadcastAddr;
private int mTransactionId;
private long mTransactionStartMillis;
private DhcpResults mDhcpLease;
private long mDhcpLeaseExpiry;
private DhcpResults mOffer;
private String mL2Key;
private Inet4Address mLastAssignedIpv4Address;
private long mLastAssignedIpv4AddressExpiry;
private Dependencies mDependencies;
@NonNull
private final NetworkStackIpMemoryStore mIpMemoryStore;
// Milliseconds SystemClock timestamps used to record transition times to DhcpBoundState.
private long mLastInitEnterTime;
private long mLastBoundExitTime;
// States.
private State mStoppedState = new StoppedState();
private State mDhcpState = new DhcpState();
private State mDhcpInitState = new DhcpInitState();
private State mDhcpSelectingState = new DhcpSelectingState();
private State mDhcpRequestingState = new DhcpRequestingState();
private State mDhcpHaveLeaseState = new DhcpHaveLeaseState();
private State mConfiguringInterfaceState = new ConfiguringInterfaceState();
private State mDhcpBoundState = new DhcpBoundState();
private State mDhcpRenewingState = new DhcpRenewingState();
private State mDhcpRebindingState = new DhcpRebindingState();
private State mDhcpInitRebootState = new DhcpInitRebootState();
private State mDhcpRebootingState = new DhcpRebootingState();
private State mObtainingConfigurationState = new ObtainingConfigurationState();
private State mWaitBeforeStartState = new WaitBeforeStartState(mDhcpInitState);
private State mWaitBeforeRenewalState = new WaitBeforeRenewalState(mDhcpRenewingState);
private State mWaitBeforeObtainingConfigurationState =
new WaitBeforeObtainingConfigurationState(mObtainingConfigurationState);
private WakeupMessage makeWakeupMessage(String cmdName, int cmd) {
cmdName = DhcpClient.class.getSimpleName() + "." + mIfaceName + "." + cmdName;
return new WakeupMessage(mContext, getHandler(), cmdName, cmd);
}
/**
* Encapsulates DhcpClient depencencies that's used for unit testing and
* integration testing.
*/
public static class Dependencies {
private final NetworkStackIpMemoryStore mNetworkStackIpMemoryStore;
public Dependencies(NetworkStackIpMemoryStore store) {
mNetworkStackIpMemoryStore = store;
}
/**
* Get a IpMemoryStore instance.
*/
public NetworkStackIpMemoryStore getIpMemoryStore() {
return mNetworkStackIpMemoryStore;
}
/**
* Get the value of DHCP related experiment flags.
*/
public boolean getBooleanDeviceConfig(final String nameSpace, final String flagName) {
return NetworkStackUtils.getDeviceConfigPropertyBoolean(nameSpace, flagName,
false /* default value */);
}
}
// TODO: Take an InterfaceParams instance instead of an interface name String.
private DhcpClient(Context context, StateMachine controller, String iface,
Dependencies deps) {
super(TAG, controller.getHandler());
mDependencies = deps;
mContext = context;
mController = controller;
mIfaceName = iface;
mIpMemoryStore = deps.getIpMemoryStore();
// CHECKSTYLE:OFF IndentationCheck
addState(mStoppedState);
addState(mDhcpState);
addState(mDhcpInitState, mDhcpState);
addState(mWaitBeforeStartState, mDhcpState);
addState(mWaitBeforeObtainingConfigurationState, mDhcpState);
addState(mObtainingConfigurationState, mDhcpState);
addState(mDhcpSelectingState, mDhcpState);
addState(mDhcpRequestingState, mDhcpState);
addState(mDhcpHaveLeaseState, mDhcpState);
addState(mConfiguringInterfaceState, mDhcpHaveLeaseState);
addState(mDhcpBoundState, mDhcpHaveLeaseState);
addState(mWaitBeforeRenewalState, mDhcpHaveLeaseState);
addState(mDhcpRenewingState, mDhcpHaveLeaseState);
addState(mDhcpRebindingState, mDhcpHaveLeaseState);
addState(mDhcpInitRebootState, mDhcpState);
addState(mDhcpRebootingState, mDhcpState);
// CHECKSTYLE:ON IndentationCheck
setInitialState(mStoppedState);
mRandom = new Random();
// Used to schedule packet retransmissions.
mKickAlarm = makeWakeupMessage("KICK", CMD_KICK);
// Used to time out PacketRetransmittingStates.
mTimeoutAlarm = makeWakeupMessage("TIMEOUT", CMD_TIMEOUT);
// Used to schedule DHCP reacquisition.
mRenewAlarm = makeWakeupMessage("RENEW", CMD_RENEW_DHCP);
mRebindAlarm = makeWakeupMessage("REBIND", CMD_REBIND_DHCP);
mExpiryAlarm = makeWakeupMessage("EXPIRY", CMD_EXPIRE_DHCP);
}
public void registerForPreDhcpNotification() {
mRegisteredForPreDhcpNotification = true;
}
public static DhcpClient makeDhcpClient(
Context context, StateMachine controller, InterfaceParams ifParams,
Dependencies deps) {
DhcpClient client = new DhcpClient(context, controller, ifParams.name, deps);
client.mIface = ifParams;
client.start();
return client;
}
/**
* check whether or not to support caching the last lease info and INIT-REBOOT state.
*
*/
public boolean isDhcpLeaseCacheEnabled() {
return mDependencies.getBooleanDeviceConfig(NetworkStackUtils.NAMESPACE_CONNECTIVITY,
NetworkStackUtils.DHCP_INIT_REBOOT_ENABLED);
}
/**
* check whether or not to support DHCP Rapid Commit option.
*/
public boolean isDhcpRapidCommitEnabled() {
return mDependencies.getBooleanDeviceConfig(NetworkStackUtils.NAMESPACE_CONNECTIVITY,
NetworkStackUtils.DHCP_RAPID_COMMIT_ENABLED);
}
private void confirmDhcpLease(DhcpPacket packet, DhcpResults results) {
setDhcpLeaseExpiry(packet);
acceptDhcpResults(results, "Confirmed");
}
private boolean initInterface() {
if (mIface == null) mIface = InterfaceParams.getByName(mIfaceName);
if (mIface == null) {
Log.e(TAG, "Can't determine InterfaceParams for " + mIfaceName);
return false;
}
mHwAddr = mIface.macAddr.toByteArray();
mInterfaceBroadcastAddr = makePacketSocketAddress(mIface.index, DhcpPacket.ETHER_BROADCAST);
return true;
}
private void startNewTransaction() {
mTransactionId = mRandom.nextInt();
mTransactionStartMillis = SystemClock.elapsedRealtime();
}
private boolean initSockets() {
return initPacketSocket() && initUdpSocket();
}
private boolean initPacketSocket() {
try {
mPacketSock = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IP);
SocketAddress addr = makePacketSocketAddress(ETH_P_IP, mIface.index);
Os.bind(mPacketSock, addr);
NetworkStackUtils.attachDhcpFilter(mPacketSock);
} catch(SocketException|ErrnoException e) {
Log.e(TAG, "Error creating packet socket", e);
return false;
}
return true;
}
private boolean initUdpSocket() {
final int oldTag = TrafficStats.getAndSetThreadStatsTag(
TrafficStatsConstants.TAG_SYSTEM_DHCP);
try {
mUdpSock = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SocketUtils.bindSocketToInterface(mUdpSock, mIfaceName);
Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_REUSEADDR, 1);
Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_BROADCAST, 1);
Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_RCVBUF, 0);
Os.bind(mUdpSock, IPV4_ADDR_ANY, DhcpPacket.DHCP_CLIENT);
} catch(SocketException|ErrnoException e) {
Log.e(TAG, "Error creating UDP socket", e);
return false;
} finally {
TrafficStats.setThreadStatsTag(oldTag);
}
return true;
}
private boolean connectUdpSock(Inet4Address to) {
try {
Os.connect(mUdpSock, to, DhcpPacket.DHCP_SERVER);
return true;
} catch (SocketException|ErrnoException e) {
Log.e(TAG, "Error connecting UDP socket", e);
return false;
}
}
private void closeSockets() {
closeSocketQuietly(mUdpSock);
closeSocketQuietly(mPacketSock);
}
class ReceiveThread extends Thread {
private final byte[] mPacket = new byte[DhcpPacket.MAX_LENGTH];
private volatile boolean mStopped = false;
public void halt() {
mStopped = true;
closeSockets(); // Interrupts the read() call the thread is blocked in.
}
@Override
public void run() {
if (DBG) Log.d(TAG, "Receive thread started");
while (!mStopped) {
int length = 0; // Or compiler can't tell it's initialized if a parse error occurs.
try {
length = Os.read(mPacketSock, mPacket, 0, mPacket.length);
DhcpPacket packet = null;
packet = DhcpPacket.decodeFullPacket(mPacket, length, DhcpPacket.ENCAP_L2);
if (DBG) Log.d(TAG, "Received packet: " + packet);
sendMessage(CMD_RECEIVED_PACKET, packet);
} catch (IOException|ErrnoException e) {
if (!mStopped) {
Log.e(TAG, "Read error", e);
logError(DhcpErrorEvent.RECEIVE_ERROR);
}
} catch (DhcpPacket.ParseException e) {
Log.e(TAG, "Can't parse packet: " + e.getMessage());
if (PACKET_DBG) {
Log.d(TAG, HexDump.dumpHexString(mPacket, 0, length));
}
if (e.errorCode == DhcpErrorEvent.DHCP_NO_COOKIE) {
int snetTagId = 0x534e4554;
String bugId = "31850211";
int uid = -1;
String data = DhcpPacket.ParseException.class.getName();
EventLog.writeEvent(snetTagId, bugId, uid, data);
}
logError(e.errorCode);
}
}
if (DBG) Log.d(TAG, "Receive thread stopped");
}
}
private short getSecs() {
return (short) ((SystemClock.elapsedRealtime() - mTransactionStartMillis) / 1000);
}
private boolean transmitPacket(ByteBuffer buf, String description, int encap, Inet4Address to) {
try {
if (encap == DhcpPacket.ENCAP_L2) {
if (DBG) Log.d(TAG, "Broadcasting " + description);
Os.sendto(mPacketSock, buf.array(), 0, buf.limit(), 0, mInterfaceBroadcastAddr);
} else if (encap == DhcpPacket.ENCAP_BOOTP && to.equals(INADDR_BROADCAST)) {
if (DBG) Log.d(TAG, "Broadcasting " + description);
// We only send L3-encapped broadcasts in DhcpRebindingState,
// where we have an IP address and an unconnected UDP socket.
//
// N.B.: We only need this codepath because DhcpRequestPacket
// hardcodes the source IP address to 0.0.0.0. We could reuse
// the packet socket if this ever changes.
Os.sendto(mUdpSock, buf, 0, to, DhcpPacket.DHCP_SERVER);
} else {
// It's safe to call getpeername here, because we only send unicast packets if we
// have an IP address, and we connect the UDP socket in DhcpBoundState#enter.
if (DBG) Log.d(TAG, String.format("Unicasting %s to %s",
description, Os.getpeername(mUdpSock)));
Os.write(mUdpSock, buf);
}
} catch(ErrnoException|IOException e) {
Log.e(TAG, "Can't send packet: ", e);
return false;
}
return true;
}
private boolean sendDiscoverPacket() {
ByteBuffer packet = DhcpPacket.buildDiscoverPacket(
DhcpPacket.ENCAP_L2, mTransactionId, getSecs(), mHwAddr,
DO_UNICAST, REQUESTED_PARAMS, isDhcpRapidCommitEnabled());
return transmitPacket(packet, "DHCPDISCOVER", DhcpPacket.ENCAP_L2, INADDR_BROADCAST);
}
private boolean sendRequestPacket(
Inet4Address clientAddress, Inet4Address requestedAddress,
Inet4Address serverAddress, Inet4Address to) {
// TODO: should we use the transaction ID from the server?
final int encap = INADDR_ANY.equals(clientAddress)
? DhcpPacket.ENCAP_L2 : DhcpPacket.ENCAP_BOOTP;
ByteBuffer packet = DhcpPacket.buildRequestPacket(
encap, mTransactionId, getSecs(), clientAddress,
DO_UNICAST, mHwAddr, requestedAddress,
serverAddress, REQUESTED_PARAMS, null);
String serverStr = (serverAddress != null) ? serverAddress.getHostAddress() : null;
String description = "DHCPREQUEST ciaddr=" + clientAddress.getHostAddress() +
" request=" + requestedAddress.getHostAddress() +
" serverid=" + serverStr;
return transmitPacket(packet, description, encap, to);
}
private void scheduleLeaseTimers() {
if (mDhcpLeaseExpiry == 0) {
Log.d(TAG, "Infinite lease, no timer scheduling needed");
return;
}
final long now = SystemClock.elapsedRealtime();
// TODO: consider getting the renew and rebind timers from T1 and T2.
// See also:
// https://tools.ietf.org/html/rfc2131#section-4.4.5
// https://tools.ietf.org/html/rfc1533#section-9.9
// https://tools.ietf.org/html/rfc1533#section-9.10
final long remainingDelay = mDhcpLeaseExpiry - now;
final long renewDelay = remainingDelay / 2;
final long rebindDelay = remainingDelay * 7 / 8;
mRenewAlarm.schedule(now + renewDelay);
mRebindAlarm.schedule(now + rebindDelay);
mExpiryAlarm.schedule(now + remainingDelay);
Log.d(TAG, "Scheduling renewal in " + (renewDelay / 1000) + "s");
Log.d(TAG, "Scheduling rebind in " + (rebindDelay / 1000) + "s");
Log.d(TAG, "Scheduling expiry in " + (remainingDelay / 1000) + "s");
}
private void setLeaseExpiredToIpMemoryStore() {
final String l2Key = mL2Key;
if (l2Key == null) return;
final NetworkAttributes.Builder na = new NetworkAttributes.Builder();
// TODO: clear out the address and lease instead of storing an expired lease
na.setAssignedV4AddressExpiry(EXPIRED_LEASE);
final OnStatusListener listener = status -> {
if (!status.isSuccess()) Log.e(TAG, "Failed to set lease expiry, status: " + status);
};
mIpMemoryStore.storeNetworkAttributes(l2Key, na.build(), listener);
}
private void maybeSaveLeaseToIpMemoryStore() {
final String l2Key = mL2Key;
if (l2Key == null || mDhcpLease == null || mDhcpLease.ipAddress == null) return;
final NetworkAttributes.Builder na = new NetworkAttributes.Builder();
na.setAssignedV4Address((Inet4Address) mDhcpLease.ipAddress.getAddress());
na.setAssignedV4AddressExpiry((mDhcpLease.leaseDuration == INFINITE_LEASE)
? Long.MAX_VALUE
: mDhcpLease.leaseDuration * 1000 + System.currentTimeMillis());
na.setDnsAddresses(mDhcpLease.dnsServers);
na.setMtu(mDhcpLease.mtu);
final OnStatusListener listener = status -> {
if (!status.isSuccess()) Log.e(TAG, "Failed to store network attrs, status: " + status);
};
mIpMemoryStore.storeNetworkAttributes(l2Key, na.build(), listener);
}
private void notifySuccess() {
if (isDhcpLeaseCacheEnabled()) {
maybeSaveLeaseToIpMemoryStore();
}
mController.sendMessage(
CMD_POST_DHCP_ACTION, DHCP_SUCCESS, 0, new DhcpResults(mDhcpLease));
}
private void notifyFailure() {
if (isDhcpLeaseCacheEnabled()) {
setLeaseExpiredToIpMemoryStore();
}
mController.sendMessage(CMD_POST_DHCP_ACTION, DHCP_FAILURE, 0, null);
}
private void acceptDhcpResults(DhcpResults results, String msg) {
mDhcpLease = results;
if (mDhcpLease.dnsServers.isEmpty()) {
// supplement customized dns servers
String[] dnsServersList =
mContext.getResources().getStringArray(R.array.config_default_dns_servers);
for (final String dnsServer : dnsServersList) {
try {
mDhcpLease.dnsServers.add(InetAddresses.parseNumericAddress(dnsServer));
} catch (IllegalArgumentException e) {
Log.e(TAG, "Invalid default DNS server: " + dnsServer, e);
}
}
}
mOffer = null;
Log.d(TAG, msg + " lease: " + mDhcpLease);
notifySuccess();
}
private void clearDhcpState() {
mDhcpLease = null;
mDhcpLeaseExpiry = 0;
mOffer = null;
}
/**
* Quit the DhcpStateMachine.
*
* @hide
*/
public void doQuit() {
Log.d(TAG, "doQuit");
quit();
}
@Override
protected void onQuitting() {
Log.d(TAG, "onQuitting");
mController.sendMessage(CMD_ON_QUIT);
}
abstract class LoggingState extends State {
private long mEnterTimeMs;
@Override
public void enter() {
if (STATE_DBG) Log.d(TAG, "Entering state " + getName());
mEnterTimeMs = SystemClock.elapsedRealtime();
}
@Override
public void exit() {
long durationMs = SystemClock.elapsedRealtime() - mEnterTimeMs;
logState(getName(), (int) durationMs);
}
private String messageName(int what) {
return sMessageNames.get(what, Integer.toString(what));
}
private String messageToString(Message message) {
long now = SystemClock.uptimeMillis();
return new StringBuilder(" ")
.append(message.getWhen() - now)
.append(messageName(message.what))
.append(" ").append(message.arg1)
.append(" ").append(message.arg2)
.append(" ").append(message.obj)
.toString();
}
@Override
public boolean processMessage(Message message) {
if (MSG_DBG) {
Log.d(TAG, getName() + messageToString(message));
}
return NOT_HANDLED;
}
@Override
public String getName() {
// All DhcpClient's states are inner classes with a well defined name.
// Use getSimpleName() and avoid super's getName() creating new String instances.
return getClass().getSimpleName();
}
}
// Sends CMD_PRE_DHCP_ACTION to the controller, waits for the controller to respond with
// CMD_PRE_DHCP_ACTION_COMPLETE, and then transitions to mOtherState.
abstract class WaitBeforeOtherState extends LoggingState {
protected State mOtherState;
@Override
public void enter() {
super.enter();
mController.sendMessage(CMD_PRE_DHCP_ACTION);
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case CMD_PRE_DHCP_ACTION_COMPLETE:
transitionTo(mOtherState);
return HANDLED;
default:
return NOT_HANDLED;
}
}
}
class StoppedState extends State {
@Override
public boolean processMessage(Message message) {
switch (message.what) {
case CMD_START_DHCP:
mL2Key = (String) message.obj;
if (mRegisteredForPreDhcpNotification) {
transitionTo(isDhcpLeaseCacheEnabled()
? mWaitBeforeObtainingConfigurationState : mWaitBeforeStartState);
} else {
transitionTo(isDhcpLeaseCacheEnabled()
? mObtainingConfigurationState : mDhcpInitState);
}
return HANDLED;
default:
return NOT_HANDLED;
}
}
}
class WaitBeforeStartState extends WaitBeforeOtherState {
WaitBeforeStartState(State otherState) {
super();
mOtherState = otherState;
}
}
class WaitBeforeRenewalState extends WaitBeforeOtherState {
WaitBeforeRenewalState(State otherState) {
super();
mOtherState = otherState;
}
}
class WaitBeforeObtainingConfigurationState extends WaitBeforeOtherState {
WaitBeforeObtainingConfigurationState(State otherState) {
super();
mOtherState = otherState;
}
}
class DhcpState extends State {
@Override
public void enter() {
clearDhcpState();
if (initInterface() && initSockets()) {
mReceiveThread = new ReceiveThread();
mReceiveThread.start();
} else {
notifyFailure();
transitionTo(mStoppedState);
}
}
@Override
public void exit() {
if (mReceiveThread != null) {
mReceiveThread.halt(); // Also closes sockets.
mReceiveThread = null;
}
clearDhcpState();
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case CMD_STOP_DHCP:
transitionTo(mStoppedState);
return HANDLED;
default:
return NOT_HANDLED;
}
}
}
public boolean isValidPacket(DhcpPacket packet) {
// TODO: check checksum.
int xid = packet.getTransactionId();
if (xid != mTransactionId) {
Log.d(TAG, "Unexpected transaction ID " + xid + ", expected " + mTransactionId);
return false;
}
if (!Arrays.equals(packet.getClientMac(), mHwAddr)) {
Log.d(TAG, "MAC addr mismatch: got " +
HexDump.toHexString(packet.getClientMac()) + ", expected " +
HexDump.toHexString(packet.getClientMac()));
return false;
}
return true;
}
public void setDhcpLeaseExpiry(DhcpPacket packet) {
long leaseTimeMillis = packet.getLeaseTimeMillis();
mDhcpLeaseExpiry =
(leaseTimeMillis > 0) ? SystemClock.elapsedRealtime() + leaseTimeMillis : 0;
}
/**
* Retransmits packets using jittered exponential backoff with an optional timeout. Packet
* transmission is triggered by CMD_KICK, which is sent by an AlarmManager alarm. If a subclass
* sets mTimeout to a positive value, then timeout() is called by an AlarmManager alarm mTimeout
* milliseconds after entering the state. Kicks and timeouts are cancelled when leaving the
* state.
*
* Concrete subclasses must implement sendPacket, which is called when the alarm fires and a
* packet needs to be transmitted, and receivePacket, which is triggered by CMD_RECEIVED_PACKET
* sent by the receive thread. They may also set mTimeout and implement timeout.
*/
abstract class PacketRetransmittingState extends LoggingState {
private int mTimer;
protected int mTimeout = 0;
@Override
public void enter() {
super.enter();
initTimer();
maybeInitTimeout();
sendMessage(CMD_KICK);
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case CMD_KICK:
sendPacket();
scheduleKick();
return HANDLED;
case CMD_RECEIVED_PACKET:
receivePacket((DhcpPacket) message.obj);
return HANDLED;
case CMD_TIMEOUT:
timeout();
return HANDLED;
default:
return NOT_HANDLED;
}
}
@Override
public void exit() {
super.exit();
mKickAlarm.cancel();
mTimeoutAlarm.cancel();
}
abstract protected boolean sendPacket();
abstract protected void receivePacket(DhcpPacket packet);
protected void timeout() {}
protected void initTimer() {
mTimer = FIRST_TIMEOUT_MS;
}
protected int jitterTimer(int baseTimer) {
int maxJitter = baseTimer / 10;
int jitter = mRandom.nextInt(2 * maxJitter) - maxJitter;
return baseTimer + jitter;
}
protected void scheduleKick() {
long now = SystemClock.elapsedRealtime();
long timeout = jitterTimer(mTimer);
long alarmTime = now + timeout;
mKickAlarm.schedule(alarmTime);
mTimer *= 2;
if (mTimer > MAX_TIMEOUT_MS) {
mTimer = MAX_TIMEOUT_MS;
}
}
protected void maybeInitTimeout() {
if (mTimeout > 0) {
long alarmTime = SystemClock.elapsedRealtime() + mTimeout;
mTimeoutAlarm.schedule(alarmTime);
}
}
}
class ObtainingConfigurationState extends LoggingState {
@Override
public void enter() {
super.enter();
// Set a timeout for retrieving network attributes operation
sendMessageDelayed(EVENT_CONFIGURATION_TIMEOUT, IPMEMORYSTORE_TIMEOUT_MS);
final OnNetworkAttributesRetrievedListener listener = (status, l2Key, attributes) -> {
if (null == attributes || null == attributes.assignedV4Address) {
if (!status.isSuccess()) {
Log.e(TAG, "Error retrieving network attributes: " + status);
}
sendMessage(EVENT_CONFIGURATION_INVALID);
return;
}
sendMessage(EVENT_CONFIGURATION_OBTAINED, attributes);
};
mIpMemoryStore.retrieveNetworkAttributes(mL2Key, listener);
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case EVENT_CONFIGURATION_INVALID:
case EVENT_CONFIGURATION_TIMEOUT:
transitionTo(mDhcpInitState);
return HANDLED;
case EVENT_CONFIGURATION_OBTAINED:
final long currentTime = System.currentTimeMillis();
NetworkAttributes attributes = (NetworkAttributes) message.obj;
if (DBG) {
Log.d(TAG, "l2key: " + mL2Key
+ " lease address: " + attributes.assignedV4Address
+ " lease expiry: " + attributes.assignedV4AddressExpiry
+ " current time: " + currentTime);
}
if (currentTime >= attributes.assignedV4AddressExpiry) {
// Lease has expired.
transitionTo(mDhcpInitState);
return HANDLED;
}
mLastAssignedIpv4Address = attributes.assignedV4Address;
mLastAssignedIpv4AddressExpiry = attributes.assignedV4AddressExpiry;
transitionTo(mDhcpInitRebootState);
return HANDLED;
default:
deferMessage(message);
return HANDLED;
}
}
@Override
public void exit() {
removeMessages(EVENT_CONFIGURATION_INVALID);
removeMessages(EVENT_CONFIGURATION_TIMEOUT);
removeMessages(EVENT_CONFIGURATION_OBTAINED);
}
}
class DhcpInitState extends PacketRetransmittingState {
public DhcpInitState() {
super();
}
@Override
public void enter() {
super.enter();
startNewTransaction();
mLastInitEnterTime = SystemClock.elapsedRealtime();
}
protected boolean sendPacket() {
return sendDiscoverPacket();
}
protected void receivePacket(DhcpPacket packet) {
if (!isValidPacket(packet)) return;
// 1. received the DHCPOFFER packet, process it by following RFC2131.
// 2. received the DHCPACK packet from DHCP Servers who support Rapid
// Commit option, process it by following RFC4039.
if (packet instanceof DhcpOfferPacket) {
mOffer = packet.toDhcpResults();
if (mOffer != null) {
Log.d(TAG, "Got pending lease: " + mOffer);
transitionTo(mDhcpRequestingState);
}
} else if (packet instanceof DhcpAckPacket) {
// If received DHCPACK packet w/o Rapid Commit option in this state,
// just drop it and wait for the next DHCPOFFER packet or DHCPACK w/
// Rapid Commit option.
if (!isDhcpRapidCommitEnabled() || !packet.mRapidCommit) return;
final DhcpResults results = packet.toDhcpResults();
if (results != null) {
confirmDhcpLease(packet, results);
transitionTo(mConfiguringInterfaceState);
}
}
}
}
// Not implemented. We request the first offer we receive.
class DhcpSelectingState extends LoggingState {
}
class DhcpRequestingState extends PacketRetransmittingState {
public DhcpRequestingState() {
mTimeout = DHCP_TIMEOUT_MS / 2;
}
protected boolean sendPacket() {
return sendRequestPacket(
INADDR_ANY, // ciaddr
(Inet4Address) mOffer.ipAddress.getAddress(), // DHCP_REQUESTED_IP
(Inet4Address) mOffer.serverAddress, // DHCP_SERVER_IDENTIFIER
INADDR_BROADCAST); // packet destination address
}
protected void receivePacket(DhcpPacket packet) {
if (!isValidPacket(packet)) return;
if ((packet instanceof DhcpAckPacket)) {
DhcpResults results = packet.toDhcpResults();
if (results != null) {
confirmDhcpLease(packet, results);
transitionTo(mConfiguringInterfaceState);
}
} else if (packet instanceof DhcpNakPacket) {
// TODO: Wait a while before returning into INIT state.
Log.d(TAG, "Received NAK, returning to INIT");
mOffer = null;
transitionTo(mDhcpInitState);
}
}
@Override
protected void timeout() {
// After sending REQUESTs unsuccessfully for a while, go back to init.
transitionTo(mDhcpInitState);
}
}
class DhcpHaveLeaseState extends State {
@Override
public boolean processMessage(Message message) {
switch (message.what) {
case CMD_EXPIRE_DHCP:
Log.d(TAG, "Lease expired!");
notifyFailure();
transitionTo(mDhcpInitState);
return HANDLED;
default:
return NOT_HANDLED;
}
}
@Override
public void exit() {
// Clear any extant alarms.
mRenewAlarm.cancel();
mRebindAlarm.cancel();
mExpiryAlarm.cancel();
clearDhcpState();
// Tell IpManager to clear the IPv4 address. There is no need to
// wait for confirmation since any subsequent packets are sent from
// INADDR_ANY anyway (DISCOVER, REQUEST).
mController.sendMessage(CMD_CLEAR_LINKADDRESS);
}
}
class ConfiguringInterfaceState extends LoggingState {
@Override
public void enter() {
super.enter();
mController.sendMessage(CMD_CONFIGURE_LINKADDRESS, mDhcpLease.ipAddress);
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case EVENT_LINKADDRESS_CONFIGURED:
transitionTo(mDhcpBoundState);
return HANDLED;
default:
return NOT_HANDLED;
}
}
}
class DhcpBoundState extends LoggingState {
@Override
public void enter() {
super.enter();
if (mDhcpLease.serverAddress != null && !connectUdpSock(mDhcpLease.serverAddress)) {
// There's likely no point in going into DhcpInitState here, we'll probably
// just repeat the transaction, get the same IP address as before, and fail.
//
// NOTE: It is observed that connectUdpSock() basically never fails, due to
// SO_BINDTODEVICE. Examining the local socket address shows it will happily
// return an IPv4 address from another interface, or even return "0.0.0.0".
//
// TODO: Consider deleting this check, following testing on several kernels.
notifyFailure();
transitionTo(mStoppedState);
}
scheduleLeaseTimers();
logTimeToBoundState();
}
@Override
public void exit() {
super.exit();
mLastBoundExitTime = SystemClock.elapsedRealtime();
}
@Override
public boolean processMessage(Message message) {
super.processMessage(message);
switch (message.what) {
case CMD_RENEW_DHCP:
if (mRegisteredForPreDhcpNotification) {
transitionTo(mWaitBeforeRenewalState);
} else {
transitionTo(mDhcpRenewingState);
}
return HANDLED;
default:
return NOT_HANDLED;
}
}
private void logTimeToBoundState() {
long now = SystemClock.elapsedRealtime();
if (mLastBoundExitTime > mLastInitEnterTime) {
logState(EVENT_RENEWING_BOUND, (int) (now - mLastBoundExitTime));
} else {
logState(EVENT_INITIAL_BOUND, (int) (now - mLastInitEnterTime));
}
}
}
abstract class DhcpReacquiringState extends PacketRetransmittingState {
protected String mLeaseMsg;
@Override
public void enter() {
super.enter();
startNewTransaction();
}
abstract protected Inet4Address packetDestination();
protected boolean sendPacket() {
return sendRequestPacket(
(Inet4Address) mDhcpLease.ipAddress.getAddress(), // ciaddr
INADDR_ANY, // DHCP_REQUESTED_IP
null, // DHCP_SERVER_IDENTIFIER
packetDestination()); // packet destination address
}
protected void receivePacket(DhcpPacket packet) {
if (!isValidPacket(packet)) return;
if ((packet instanceof DhcpAckPacket)) {
final DhcpResults results = packet.toDhcpResults();
if (results != null) {
if (!mDhcpLease.ipAddress.equals(results.ipAddress)) {
Log.d(TAG, "Renewed lease not for our current IP address!");
notifyFailure();
transitionTo(mDhcpInitState);
}
setDhcpLeaseExpiry(packet);
// Updating our notion of DhcpResults here only causes the
// DNS servers and routes to be updated in LinkProperties
// in IpManager and by any overridden relevant handlers of
// the registered IpManager.Callback. IP address changes
// are not supported here.
acceptDhcpResults(results, mLeaseMsg);
transitionTo(mDhcpBoundState);
}
} else if (packet instanceof DhcpNakPacket) {
Log.d(TAG, "Received NAK, returning to INIT");
notifyFailure();
transitionTo(mDhcpInitState);
}
}
}
class DhcpRenewingState extends DhcpReacquiringState {
public DhcpRenewingState() {
mLeaseMsg = "Renewed";
}
@Override
public boolean processMessage(Message message) {
if (super.processMessage(message) == HANDLED) {
return HANDLED;
}
switch (message.what) {
case CMD_REBIND_DHCP:
transitionTo(mDhcpRebindingState);
return HANDLED;
default:
return NOT_HANDLED;
}
}
@Override
protected Inet4Address packetDestination() {
// Not specifying a SERVER_IDENTIFIER option is a violation of RFC 2131, but...
// http://b/25343517 . Try to make things work anyway by using broadcast renews.
return (mDhcpLease.serverAddress != null) ?
mDhcpLease.serverAddress : INADDR_BROADCAST;
}
}
class DhcpRebindingState extends DhcpReacquiringState {
public DhcpRebindingState() {
mLeaseMsg = "Rebound";
}
@Override
public void enter() {
super.enter();
// We need to broadcast and possibly reconnect the socket to a
// completely different server.
closeSocketQuietly(mUdpSock);
if (!initUdpSocket()) {
Log.e(TAG, "Failed to recreate UDP socket");
transitionTo(mDhcpInitState);
}
}
@Override
protected Inet4Address packetDestination() {
return INADDR_BROADCAST;
}
}
class DhcpInitRebootState extends DhcpRequestingState {
@Override
public void enter() {
super.enter();
startNewTransaction();
}
// RFC 2131 4.3.2 describes generated DHCPREQUEST message during
// INIT-REBOOT state:
// 'server identifier' MUST NOT be filled in, 'requested IP address'
// option MUST be filled in with client's notion of its previously
// assigned address. 'ciaddr' MUST be zero. The client is seeking to
// verify a previously allocated, cached configuration. Server SHOULD
// send a DHCPNAK message to the client if the 'requested IP address'
// is incorrect, or is on the wrong network.
@Override
protected boolean sendPacket() {
return sendRequestPacket(
INADDR_ANY, // ciaddr
mLastAssignedIpv4Address, // DHCP_REQUESTED_IP
null, // DHCP_SERVER_IDENTIFIER
INADDR_BROADCAST); // packet destination address
}
@Override
public void exit() {
mLastAssignedIpv4Address = null;
mLastAssignedIpv4AddressExpiry = 0;
}
}
class DhcpRebootingState extends LoggingState {
}
private void logError(int errorCode) {
mMetricsLog.log(mIfaceName, new DhcpErrorEvent(errorCode));
}
private void logState(String name, int durationMs) {
final DhcpClientEvent event = new DhcpClientEvent.Builder()
.setMsg(name)
.setDurationMs(durationMs)
.build();
mMetricsLog.log(mIfaceName, event);
}
}
|