summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/sbin/dhcpagent/states.c
blob: c26e6c07b25ea2618b77ab433b2e4119f16f72a2 (plain)
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
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * This module contains core functions for managing DHCP state machine
 * instances.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdlib.h>
#include <search.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/arp.h>
#include <arpa/inet.h>
#include <dhcpmsg.h>
#include <dhcpagent_util.h>
#include <dhcp_stable.h>

#include "agent.h"
#include "states.h"
#include "interface.h"
#include "defaults.h"
#include "script_handler.h"

static uint_t global_smach_count;

static uchar_t *global_duid;
static size_t global_duidlen;

/*
 * iaid_retry(): attempt to write LIF IAID again
 *
 *   input: iu_tq_t *: ignored
 *	    void *: pointer to LIF
 *  output: none
 */

/* ARGSUSED */
static void
iaid_retry(iu_tq_t *tqp, void *arg)
{
	dhcp_lif_t *lif = arg;

	if (write_stable_iaid(lif->lif_name, lif->lif_iaid) == -1) {
		if (errno != EROFS) {
			dhcpmsg(MSG_ERR,
			    "iaid_retry: unable to write out IAID for %s",
			    lif->lif_name);
			release_lif(lif);
		} else {
			lif->lif_iaid_id = iu_schedule_timer(tq, 60,
			    iaid_retry, lif);
		}
	} else {
		release_lif(lif);
	}
}

/*
 * insert_smach(): Create a state machine instance on a given logical
 *		   interface.  The state machine holds the caller's LIF
 *		   reference on success, and frees it on failure.
 *
 *   input: dhcp_lif_t *: logical interface name
 *	    int *: set to DHCP_IPC_E_* if creation fails
 *  output: dhcp_smach_t *: state machine instance
 */

dhcp_smach_t *
insert_smach(dhcp_lif_t *lif, int *error)
{
	dhcp_smach_t *dsmp, *alt_primary;
	boolean_t isv6;
	const char *prl;

	if ((dsmp = calloc(1, sizeof (*dsmp))) == NULL) {
		dhcpmsg(MSG_ERR, "cannot allocate state machine entry for %s",
		    lif->lif_name);
		remove_lif(lif);
		release_lif(lif);
		*error = DHCP_IPC_E_MEMORY;
		return (NULL);
	}
	dsmp->dsm_name = lif->lif_name;
	dsmp->dsm_lif = lif;
	dsmp->dsm_hold_count = 1;
	dsmp->dsm_state = INIT;
	dsmp->dsm_dflags = DHCP_IF_REMOVED;	/* until added to list */
	isv6 = lif->lif_pif->pif_isv6;

	/*
	 * Now that we have a controlling LIF, we need to assign an IAID to
	 * that LIF.
	 */
	if (lif->lif_iaid == 0 &&
	    (lif->lif_iaid = read_stable_iaid(lif->lif_name)) == 0) {
		static uint32_t iaidctr = 0x80000000u;

		/*
		 * If this is a logical interface, then use an arbitrary seed
		 * value.  Otherwise, use the ifIndex.
		 */
		lif->lif_iaid = make_stable_iaid(lif->lif_name,
		    strchr(lif->lif_name, ':') != NULL ? iaidctr++ :
		    lif->lif_pif->pif_index);
		dhcpmsg(MSG_INFO,
		    "insert_smach: manufactured IAID %u for v%d %s",
		    lif->lif_iaid, isv6 ? 6 : 4, lif->lif_name);
		hold_lif(lif);
		iaid_retry(NULL, lif);
	}

	if (isv6) {
		dsmp->dsm_dflags |= DHCP_IF_V6;
		dsmp->dsm_server = ipv6_all_dhcp_relay_and_servers;

		/*
		 * With DHCPv6, we do all of our I/O using the common
		 * v6_sock_fd.  There's no need for per-interface file
		 * descriptors because we have IPV6_PKTINFO.
		 */
	} else {
		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_BROADCAST),
		    &dsmp->dsm_server);

		/*
		 * With IPv4 DHCP, we start off doing our I/O via DLPI, so open
		 * that up now.
		 */
		if (!open_dlpi_pif(lif->lif_pif)) {
			dhcpmsg(MSG_ERR, "unable to open DLPI for %s",
			    lif->lif_name);
			/* This will also dispose of the LIF */
			release_smach(dsmp);
			*error = DHCP_IPC_E_SOCKET;
			return (NULL);
		}
		dsmp->dsm_using_dlpi = B_TRUE;
	}
	dsmp->dsm_retrans_timer		= -1;
	dsmp->dsm_offer_timer		= -1;
	dsmp->dsm_neg_hrtime		= gethrtime();
	dsmp->dsm_script_fd		= -1;
	dsmp->dsm_script_pid		= -1;
	dsmp->dsm_script_helper_pid	= -1;
	dsmp->dsm_script_event_id	= -1;
	dsmp->dsm_start_timer		= -1;

	ipc_action_init(&dsmp->dsm_ia);

	/*
	 * initialize the parameter request list, if there is one.
	 */

	prl = df_get_string(dsmp->dsm_name, isv6, DF_PARAM_REQUEST_LIST);
	if (prl == NULL) {
		dsmp->dsm_prl = NULL;
	} else {
		int i;

		for (dsmp->dsm_prllen = 1, i = 0; prl[i] != '\0'; i++) {
			if (prl[i] == ',')
				dsmp->dsm_prllen++;
		}

		dsmp->dsm_prl = malloc(dsmp->dsm_prllen *
		    sizeof (*dsmp->dsm_prl));
		if (dsmp->dsm_prl == NULL) {
			dhcpmsg(MSG_WARNING, "insert_smach: cannot allocate "
			    "parameter request list for %s (continuing)",
			    dsmp->dsm_name);
		} else {
			for (i = 0; i < dsmp->dsm_prllen; prl++, i++) {
				dsmp->dsm_prl[i] = strtoul(prl, NULL, 0);
				while (*prl != ',' && *prl != '\0')
					prl++;
				if (*prl == '\0')
					break;
			}
		}
	}

	dsmp->dsm_offer_wait = df_get_int(dsmp->dsm_name, isv6,
	    DF_OFFER_WAIT);

	/*
	 * If there is no primary of this type, and there is one of the other,
	 * then make this one primary if it's on the same named PIF.
	 */
	if (primary_smach(isv6) == NULL &&
	    (alt_primary = primary_smach(!isv6)) != NULL) {
		if (strcmp(lif->lif_pif->pif_name,
		    alt_primary->dsm_lif->lif_pif->pif_name) == 0) {
			dhcpmsg(MSG_DEBUG,
			    "insert_smach: making %s primary for v%d",
			    dsmp->dsm_name, isv6 ? 6 : 4);
			dsmp->dsm_dflags |= DHCP_IF_PRIMARY;
		}
	}

	/*
	 * We now have at least one state machine running, so cancel any
	 * running inactivity timer.
	 */
	if (inactivity_id != -1 &&
	    iu_cancel_timer(tq, inactivity_id, NULL) == 1)
		inactivity_id = -1;

	dsmp->dsm_dflags &= ~DHCP_IF_REMOVED;
	insque(dsmp, &lif->lif_smachs);
	global_smach_count++;
	dhcpmsg(MSG_DEBUG2, "insert_smach: inserted %s", dsmp->dsm_name);

	return (dsmp);
}

/*
 * hold_smach(): acquires a hold on a state machine
 *
 *   input: dhcp_smach_t *: the state machine to acquire a hold on
 *  output: void
 */

void
hold_smach(dhcp_smach_t *dsmp)
{
	dsmp->dsm_hold_count++;

	dhcpmsg(MSG_DEBUG2, "hold_smach: hold count on %s: %d",
	    dsmp->dsm_name, dsmp->dsm_hold_count);
}

/*
 * free_smach(): frees the memory occupied by a state machine
 *
 *   input: dhcp_smach_t *: the DHCP state machine to free
 *  output: void
 */

static void
free_smach(dhcp_smach_t *dsmp)
{
	dhcpmsg(MSG_DEBUG, "free_smach: freeing state machine %s",
	    dsmp->dsm_name);

	deprecate_leases(dsmp);
	remove_lif(dsmp->dsm_lif);
	release_lif(dsmp->dsm_lif);
	free_pkt_list(&dsmp->dsm_recv_pkt_list);
	if (dsmp->dsm_ack != dsmp->dsm_orig_ack)
		free_pkt_entry(dsmp->dsm_orig_ack);
	free_pkt_entry(dsmp->dsm_ack);
	free(dsmp->dsm_send_pkt.pkt);
	free(dsmp->dsm_cid);
	free(dsmp->dsm_prl);
	free(dsmp->dsm_routers);
	free(dsmp->dsm_reqhost);
	free(dsmp);

	/* no big deal if this fails */
	if (global_smach_count == 0 && inactivity_id == -1) {
		inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
		    inactivity_shutdown, NULL);
	}
}

/*
 * release_smach(): releases a hold previously acquired on a state machine.
 *		    If the hold count reaches 0, the state machine is freed.
 *
 *   input: dhcp_smach_t *: the state machine entry to release the hold on
 *  output: void
 */

void
release_smach(dhcp_smach_t *dsmp)
{
	if (dsmp->dsm_hold_count == 0) {
		dhcpmsg(MSG_CRIT, "release_smach: extraneous release");
		return;
	}

	if (dsmp->dsm_hold_count == 1 &&
	    !(dsmp->dsm_dflags & DHCP_IF_REMOVED)) {
		dhcpmsg(MSG_CRIT, "release_smach: missing removal");
		return;
	}

	if (--dsmp->dsm_hold_count == 0) {
		free_smach(dsmp);
	} else {
		dhcpmsg(MSG_DEBUG2, "release_smach: hold count on %s: %d",
		    dsmp->dsm_name, dsmp->dsm_hold_count);
	}
}

/*
 * next_smach(): state machine iterator function
 *
 *   input: dhcp_smach_t *: current state machine (or NULL for list start)
 *          boolean_t: B_TRUE if DHCPv6, B_FALSE otherwise
 *  output: dhcp_smach_t *: next state machine in list
 */

dhcp_smach_t *
next_smach(dhcp_smach_t *dsmp, boolean_t isv6)
{
	dhcp_lif_t *lif;
	dhcp_pif_t *pif;

	if (dsmp != NULL) {
		if (dsmp->dsm_next != NULL)
			return (dsmp->dsm_next);

		if ((lif = dsmp->dsm_lif) != NULL)
			lif = lif->lif_next;
		for (; lif != NULL; lif = lif->lif_next) {
			if (lif->lif_smachs != NULL)
				return (lif->lif_smachs);
		}

		if ((pif = dsmp->dsm_lif->lif_pif) != NULL)
			pif = pif->pif_next;
	} else {
		pif = isv6 ? v6root : v4root;
	}
	for (; pif != NULL; pif = pif->pif_next) {
		for (lif = pif->pif_lifs; lif != NULL; lif = lif->lif_next) {
			if (lif->lif_smachs != NULL)
				return (lif->lif_smachs);
		}
	}
	return (NULL);
}

/*
 * primary_smach(): loop through all state machines of the given type (v4 or
 *		    v6) in the system, and locate the one that's primary.
 *
 *   input: boolean_t: B_TRUE for IPv6
 *  output: dhcp_smach_t *: the primary state machine
 */

dhcp_smach_t *
primary_smach(boolean_t isv6)
{
	dhcp_smach_t *dsmp;

	for (dsmp = next_smach(NULL, isv6); dsmp != NULL;
	    dsmp = next_smach(dsmp, isv6)) {
		if (dsmp->dsm_dflags & DHCP_IF_PRIMARY)
			break;
	}
	return (dsmp);
}

/*
 * make_primary(): designate a given state machine as being the primary
 *		   instance on the primary interface.  Note that the user often
 *		   thinks in terms of a primary "interface" (rather than just
 *		   an instance), so we go to lengths here to keep v4 and v6 in
 *		   sync.
 *
 *   input: dhcp_smach_t *: the primary state machine
 *  output: none
 */

void
make_primary(dhcp_smach_t *dsmp)
{
	dhcp_smach_t *old_primary, *alt_primary;
	dhcp_pif_t *pif;

	if ((old_primary = primary_smach(dsmp->dsm_isv6)) != NULL)
		old_primary->dsm_dflags &= ~DHCP_IF_PRIMARY;
	dsmp->dsm_dflags |= DHCP_IF_PRIMARY;

	/*
	 * Find the primary for the other protocol.
	 */
	alt_primary = primary_smach(!dsmp->dsm_isv6);

	/*
	 * If it's on a different interface, then cancel that.  If it's on the
	 * same interface, then we're done.
	 */
	if (alt_primary != NULL) {
		if (strcmp(alt_primary->dsm_lif->lif_pif->pif_name,
		    dsmp->dsm_lif->lif_pif->pif_name) == 0)
			return;
		alt_primary->dsm_dflags &= ~DHCP_IF_PRIMARY;
	}

	/*
	 * We need a new primary for the other protocol.  If the PIF exists,
	 * there must be at least one state machine.  Just choose the first for
	 * consistency with insert_smach().
	 */
	if ((pif = lookup_pif_by_name(dsmp->dsm_lif->lif_pif->pif_name,
	    !dsmp->dsm_isv6)) != NULL) {
		pif->pif_lifs->lif_smachs->dsm_dflags |= DHCP_IF_PRIMARY;
	}
}

/*
 * lookup_smach(): finds a state machine by name and type; used for dispatching
 *		   user commands.
 *
 *   input: const char *: the name of the state machine
 *          boolean_t: B_TRUE if DHCPv6, B_FALSE otherwise
 *  output: dhcp_smach_t *: the state machine found
 */

dhcp_smach_t *
lookup_smach(const char *smname, boolean_t isv6)
{
	dhcp_smach_t *dsmp;

	for (dsmp = next_smach(NULL, isv6); dsmp != NULL;
	    dsmp = next_smach(dsmp, isv6)) {
		if (strcmp(dsmp->dsm_name, smname) == 0)
			break;
	}
	return (dsmp);
}

/*
 * lookup_smach_by_uindex(): iterate through running state machines by
 *			     truncated interface index.
 *
 *   input: uint16_t: the interface index (truncated)
 *	    dhcp_smach_t *: the previous state machine, or NULL for start
 *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 *  output: dhcp_smach_t *: next state machine, or NULL at end of list
 */

dhcp_smach_t *
lookup_smach_by_uindex(uint16_t ifindex, dhcp_smach_t *dsmp, boolean_t isv6)
{
	dhcp_pif_t *pif;
	dhcp_lif_t *lif;

	/*
	 * If the user gives us a state machine, then check that the next one
	 * available is on the same physical interface.  If so, then go ahead
	 * and return that.
	 */
	if (dsmp != NULL) {
		pif = dsmp->dsm_lif->lif_pif;
		if ((dsmp = next_smach(dsmp, isv6)) == NULL)
			return (NULL);
		if (pif == dsmp->dsm_lif->lif_pif)
			return (dsmp);
	} else {
		/* Otherwise, start at the beginning of the list */
		pif = NULL;
	}

	/*
	 * Find the next physical interface with the same truncated interface
	 * index, and return the first state machine on that.  If there are no
	 * more physical interfaces that match, then we're done.
	 */
	do {
		pif = lookup_pif_by_uindex(ifindex, pif, isv6);
		if (pif == NULL)
			return (NULL);
		for (lif = pif->pif_lifs; lif != NULL; lif = lif->lif_next) {
			if ((dsmp = lif->lif_smachs) != NULL)
				break;
		}
	} while (dsmp == NULL);
	return (dsmp);
}

/*
 * lookup_smach_by_xid(): iterate through running state machines by transaction
 *			  id.  Transaction ID zero means "all state machines."
 *
 *   input: uint32_t: the transaction id to look up
 *	    dhcp_smach_t *: the previous state machine, or NULL for start
 *	    boolean_t: B_TRUE if DHCPv6, B_FALSE otherwise
 *  output: dhcp_smach_t *: next state machine, or NULL at end of list
 */

dhcp_smach_t *
lookup_smach_by_xid(uint32_t xid, dhcp_smach_t *dsmp, boolean_t isv6)
{
	for (dsmp = next_smach(dsmp, isv6); dsmp != NULL;
	    dsmp = next_smach(dsmp, isv6)) {
		if (xid == 0 ||
		    pkt_get_xid(dsmp->dsm_send_pkt.pkt, isv6) == xid)
			break;
	}

	return (dsmp);
}

/*
 * lookup_smach_by_event(): find a state machine busy with a particular event
 *			    ID.  This is used only for error handling.
 *
 *   input: iu_event_id_t: the event id to look up
 *  output: dhcp_smach_t *: matching state machine, or NULL if none
 */

dhcp_smach_t *
lookup_smach_by_event(iu_event_id_t eid)
{
	dhcp_smach_t *dsmp;
	boolean_t isv6 = B_FALSE;

	for (;;) {
		for (dsmp = next_smach(NULL, isv6); dsmp != NULL;
		    dsmp = next_smach(dsmp, isv6)) {
			if ((dsmp->dsm_dflags & DHCP_IF_BUSY) &&
			    eid == dsmp->dsm_ia.ia_eid)
				return (dsmp);
		}
		if (isv6)
			break;
		isv6 = B_TRUE;
	}

	return (dsmp);
}

/*
 * cancel_offer_timer(): stop the offer polling timer on a given state machine
 *
 *   input: dhcp_smach_t *: state machine on which to stop polling for offers
 *  output: none
 */

void
cancel_offer_timer(dhcp_smach_t *dsmp)
{
	int retval;

	if (dsmp->dsm_offer_timer != -1) {
		retval = iu_cancel_timer(tq, dsmp->dsm_offer_timer, NULL);
		dsmp->dsm_offer_timer = -1;
		if (retval == 1)
			release_smach(dsmp);
	}
}

/*
 * cancel_smach_timers(): stop all of the timers related to a given state
 *			  machine, including lease and LIF expiry.
 *
 *   input: dhcp_smach_t *: state machine to cancel
 *  output: none
 *    note: this function assumes that the iu timer functions are synchronous
 *	    and thus don't require any protection or ordering on cancellation.
 */

static void
cancel_smach_timers(dhcp_smach_t *dsmp)
{
	dhcp_lease_t *dlp;
	dhcp_lif_t *lif;
	uint_t nlifs;

	for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlp->dl_next) {
		cancel_lease_timers(dlp);
		lif = dlp->dl_lifs;
		nlifs = dlp->dl_nlifs;
		for (; nlifs > 0; nlifs--, lif = lif->lif_next)
			cancel_lif_timers(lif);
	}

	cancel_offer_timer(dsmp);
	stop_pkt_retransmission(dsmp);
	if (dsmp->dsm_start_timer != -1) {
		(void) iu_cancel_timer(tq, dsmp->dsm_start_timer, NULL);
		dsmp->dsm_start_timer = -1;
		release_smach(dsmp);
	}
}

/*
 * remove_smach(): removes a given state machine from the system.  marks it
 *		   for being freed (but may not actually free it).
 *
 *   input: dhcp_smach_t *: the state machine to remove
 *  output: void
 */

void
remove_smach(dhcp_smach_t *dsmp)
{
	if (dsmp->dsm_dflags & DHCP_IF_REMOVED)
		return;

	dhcpmsg(MSG_DEBUG2, "remove_smach: removing %s", dsmp->dsm_name);
	dsmp->dsm_dflags |= DHCP_IF_REMOVED;
	remque(dsmp);
	global_smach_count--;

	/*
	 * if we have long term timers, cancel them so that state machine
	 * resources can be reclaimed in a reasonable amount of time.
	 */
	cancel_smach_timers(dsmp);

	/* Drop the hold that the LIF's state machine list had on us */
	release_smach(dsmp);
}

/*
 * finished_smach(): we're finished with a given state machine; remove it from
 *		     the system and tell the user (who may have initiated the
 *		     removal process).  Note that we remove it from the system
 *		     first to allow back-to-back drop and create invocations.
 *
 *   input: dhcp_smach_t *: the state machine to remove
 *	    int: error for IPC
 *  output: void
 */

void
finished_smach(dhcp_smach_t *dsmp, int error)
{
	hold_smach(dsmp);
	remove_smach(dsmp);
	if (dsmp->dsm_ia.ia_fd != -1)
		ipc_action_finish(dsmp, error);
	else
		(void) async_cancel(dsmp);
	release_smach(dsmp);
}

/*
 * set_smach_state(): changes state and updates I/O
 *
 *   input: dhcp_smach_t *: the state machine to change
 *	    DHCPSTATE: the new state
 *  output: boolean_t: B_TRUE on success, B_FALSE on failure
 */

boolean_t
set_smach_state(dhcp_smach_t *dsmp, DHCPSTATE state)
{
	if (dsmp->dsm_state != state) {
		boolean_t is_bound;

		dhcpmsg(MSG_DEBUG,
		    "set_smach_state: changing from %s to %s on %s",
		    dhcp_state_to_string(dsmp->dsm_state),
		    dhcp_state_to_string(state), dsmp->dsm_name);

		if (!dsmp->dsm_isv6) {
			/*
			 * When we're in a bound state for IPv4, we receive our
			 * packets through our LIF.  Otherwise, we receive them
			 * through DLPI.  Make sure the right one is connected.
			 * For IPv6, no such change is necessary.
			 */
			is_bound = (state == BOUND || state == REBINDING ||
			    state == RENEWING || state == RELEASING ||
			    state == INFORM_SENT || state == INFORMATION);
			if (dsmp->dsm_using_dlpi && is_bound) {
				if (!open_ip_lif(dsmp->dsm_lif))
					return (B_FALSE);
				dsmp->dsm_using_dlpi = B_FALSE;
				close_dlpi_pif(dsmp->dsm_lif->lif_pif);
			}
			if (!dsmp->dsm_using_dlpi && !is_bound) {
				if (!open_dlpi_pif(dsmp->dsm_lif->lif_pif))
					return (B_FALSE);
				dsmp->dsm_using_dlpi = B_TRUE;
				close_ip_lif(dsmp->dsm_lif);
			}
		}

		dsmp->dsm_state = state;
	}
	return (B_TRUE);
}

/*
 * duid_retry(): attempt to write DUID again
 *
 *   input: iu_tq_t *: ignored
 *	    void *: ignored
 *  output: none
 */

/* ARGSUSED */
static void
duid_retry(iu_tq_t *tqp, void *arg)
{
	if (write_stable_duid(global_duid, global_duidlen) == -1) {
		if (errno != EROFS) {
			dhcpmsg(MSG_ERR,
			    "duid_retry: unable to write out DUID");
		} else {
			(void) iu_schedule_timer(tq, 60, duid_retry, NULL);
		}
	}
}

/*
 * get_smach_cid(): gets the client ID for a given state machine.
 *
 *   input: dhcp_smach_t *: the state machine to set up
 *  output: int: DHCP_IPC_SUCCESS or one of DHCP_IPC_E_* on failure.
 */

int
get_smach_cid(dhcp_smach_t *dsmp)
{
	uchar_t *client_id;
	uint_t client_id_len;
	dhcp_lif_t *lif = dsmp->dsm_lif;
	dhcp_pif_t *pif = lif->lif_pif;
	const char *value;
	size_t slen;

	/*
	 * Look in defaults file for the client-id.  If present, this takes
	 * precedence over all other forms of ID.
	 */

	dhcpmsg(MSG_DEBUG, "get_smach_cid: getting default client-id "
	    "property on %s", dsmp->dsm_name);
	value = df_get_string(dsmp->dsm_name, pif->pif_isv6, DF_CLIENT_ID);
	if (value != NULL) {
		/*
		 * The Client ID string can have one of three basic forms:
		 *	<decimal>,<data...>
		 *	0x<hex...>
		 *	<string...>
		 *
		 * The first form is an RFC 3315 DUID.  This is legal for both
		 * IPv4 DHCP and DHCPv6.  For IPv4, an RFC 4361 Client ID is
		 * constructed from this value.
		 *
		 * The second and third forms are legal for IPv4 only.  This is
		 * a raw Client ID, in hex or ASCII string format.
		 */

		if (isdigit(*value) &&
		    value[strspn(value, "0123456789")] == ',') {
			char *cp;
			ulong_t duidtype;
			ulong_t subtype;

			errno = 0;
			duidtype = strtoul(value, &cp, 0);
			if (value == cp || errno != 0 || *cp != ',' ||
			    duidtype > 65535) {
				dhcpmsg(MSG_ERR, "get_smach_cid: cannot parse "
				    "DUID type in %s", value);
				goto no_specified_id;
			}
			value = cp + 1;
			switch (duidtype) {
			case DHCPV6_DUID_LL:
			case DHCPV6_DUID_LLT: {
				int num;
				char chr;

				errno = 0;
				subtype = strtoul(value, &cp, 0);
				if (value == cp || errno != 0 || *cp != ',' ||
				    subtype > 65535) {
					dhcpmsg(MSG_ERR, "get_smach_cid: "
					    "cannot parse MAC type in %s",
					    value);
					goto no_specified_id;
				}
				value = cp + 1;
				client_id_len = pif->pif_isv6 ? 1 : 5;
				for (; *cp != '\0'; cp++) {
					if (*cp == ':')
						client_id_len++;
					else if (!isxdigit(*cp))
						break;
				}
				if (duidtype == DHCPV6_DUID_LL) {
					duid_llt_t *dllt;
					time_t now;

					client_id_len += sizeof (*dllt);
					dllt = malloc(client_id_len);
					if (dllt == NULL)
						goto alloc_failure;
					dsmp->dsm_cid = (uchar_t *)dllt;
					dllt->dllt_dutype = htons(duidtype);
					dllt->dllt_hwtype = htons(subtype);
					now = time(NULL) - DUID_TIME_BASE;
					dllt->dllt_time = htonl(now);
					cp = (char *)(dllt + 1);
				} else {
					duid_ll_t *dll;

					client_id_len += sizeof (*dll);
					dll = malloc(client_id_len);
					if (dll == NULL)
						goto alloc_failure;
					dsmp->dsm_cid = (uchar_t *)dll;
					dll->dll_dutype = htons(duidtype);
					dll->dll_hwtype = htons(subtype);
					cp = (char *)(dll + 1);
				}
				num = 0;
				while ((chr = *value) != '\0') {
					if (isdigit(chr)) {
						num = (num << 4) + chr - '0';
					} else if (isxdigit(chr)) {
						num = (num << 4) + 10 + chr -
						    (isupper(chr) ? 'A' : 'a');
					} else if (chr == ':') {
						*cp++ = num;
						num = 0;
					} else {
						break;
					}
				}
				break;
			}
			case DHCPV6_DUID_EN: {
				duid_en_t *den;

				errno = 0;
				subtype = strtoul(value, &cp, 0);
				if (value == cp || errno != 0 || *cp != ',') {
					dhcpmsg(MSG_ERR, "get_smach_cid: "
					    "cannot parse enterprise in %s",
					    value);
					goto no_specified_id;
				}
				value = cp + 1;
				slen = strlen(value);
				client_id_len = (slen + 1) / 2;
				den = malloc(sizeof (*den) + client_id_len);
				if (den == NULL)
					goto alloc_failure;
				den->den_dutype = htons(duidtype);
				DHCPV6_SET_ENTNUM(den, subtype);
				if (hexascii_to_octet(value, slen, den + 1,
				    &client_id_len) != 0) {
					dhcpmsg(MSG_ERROR, "get_smach_cid: "
					    "cannot parse hex string in %s",
					    value);
					free(den);
					goto no_specified_id;
				}
				dsmp->dsm_cid = (uchar_t *)den;
				break;
			}
			default:
				slen = strlen(value);
				client_id_len = (slen + 1) / 2;
				cp = malloc(client_id_len);
				if (cp == NULL)
					goto alloc_failure;
				if (hexascii_to_octet(value, slen, cp,
				    &client_id_len) != 0) {
					dhcpmsg(MSG_ERROR, "get_smach_cid: "
					    "cannot parse hex string in %s",
					    value);
					free(cp);
					goto no_specified_id;
				}
				dsmp->dsm_cid = (uchar_t *)cp;
				break;
			}
			dsmp->dsm_cidlen = client_id_len;
			if (!pif->pif_isv6) {
				(void) memmove(dsmp->dsm_cid + 5,
				    dsmp->dsm_cid, client_id_len - 5);
				dsmp->dsm_cid[0] = 255;
				dsmp->dsm_cid[1] = lif->lif_iaid >> 24;
				dsmp->dsm_cid[2] = lif->lif_iaid >> 16;
				dsmp->dsm_cid[3] = lif->lif_iaid >> 8;
				dsmp->dsm_cid[4] = lif->lif_iaid;
			}
			return (DHCP_IPC_SUCCESS);
		}

		if (pif->pif_isv6) {
			dhcpmsg(MSG_ERROR,
			    "get_smach_cid: client ID for %s invalid: %s",
			    dsmp->dsm_name, value);
		} else if (strncasecmp("0x", value, 2) == 0 &&
		    value[2] != '\0') {
			/* skip past the 0x and convert the value to binary */
			value += 2;
			slen = strlen(value);
			client_id_len = (slen + 1) / 2;
			dsmp->dsm_cid = malloc(client_id_len);
			if (dsmp->dsm_cid == NULL)
				goto alloc_failure;
			if (hexascii_to_octet(value, slen, dsmp->dsm_cid,
			    &client_id_len) == 0) {
				dsmp->dsm_cidlen = client_id_len;
				return (DHCP_IPC_SUCCESS);
			}
			dhcpmsg(MSG_WARNING, "get_smach_cid: cannot convert "
			    "hex value for Client ID on %s", dsmp->dsm_name);
		} else {
			client_id_len = strlen(value);
			dsmp->dsm_cid = malloc(client_id_len);
			if (dsmp->dsm_cid == NULL)
				goto alloc_failure;
			(void) memcpy(dsmp->dsm_cid, value, client_id_len);
			return (DHCP_IPC_SUCCESS);
		}
	}
no_specified_id:

	/*
	 * There was either no user-specified Client ID value, or we were
	 * unable to parse it.  We need to determine if a Client ID is required
	 * and, if so, generate one.
	 *
	 * If it's IPv4 and not a logical interface, then we need to preserve
	 * backward-compatibility by avoiding new-fangled DUID/IAID
	 * construction.
	 */
	if (!pif->pif_isv6 && strchr(dsmp->dsm_name, ':') == NULL) {
		if (pif->pif_hwtype == ARPHRD_IB) {
			/*
			 * This comes from the DHCP over IPoIB specification.
			 * In the absence of an user specified client id, IPoIB
			 * automatically uses the required format, with the
			 * unique 4 octet value set to 0 (since IPoIB driver
			 * allows only a single interface on a port with a
			 * specific GID to belong to an IP subnet (PSARC
			 * 2001/289, FWARC 2002/702).
			 *
			 *   Type  Client-Identifier
			 * +-----+-----+-----+-----+-----+----....----+
			 * |  0  |  0 (4 octets)   |   GID (16 octets)|
			 * +-----+-----+-----+-----+-----+----....----+
			 */
			dsmp->dsm_cidlen = 1 + 4 + 16;
			dsmp->dsm_cid = client_id = malloc(dsmp->dsm_cidlen);
			if (dsmp->dsm_cid == NULL)
				goto alloc_failure;

			/*
			 * Pick the GID from the mac address. The format
			 * of the hardware address is:
			 * +-----+-----+-----+-----+----....----+
			 * | QPN (4 octets)  |   GID (16 octets)|
			 * +-----+-----+-----+-----+----....----+
			 */
			(void) memcpy(client_id + 5, pif->pif_hwaddr + 4,
			    pif->pif_hwlen - 4);
			(void) memset(client_id, 0, 5);
		}
		return (DHCP_IPC_SUCCESS);
	}

	/*
	 * Now check for a saved DUID.  If there is one, then use it.  If there
	 * isn't, then generate a new one.  For IPv4, we need to construct the
	 * RFC 4361 Client ID with this value and the LIF's IAID.
	 */
	if (global_duid == NULL &&
	    (global_duid = read_stable_duid(&global_duidlen)) == NULL) {
		global_duid = make_stable_duid(pif->pif_name, &global_duidlen);
		if (global_duid == NULL)
			goto alloc_failure;
		duid_retry(NULL, NULL);
	}

	if (pif->pif_isv6) {
		dsmp->dsm_cid = malloc(global_duidlen);
		if (dsmp->dsm_cid == NULL)
			goto alloc_failure;
		(void) memcpy(dsmp->dsm_cid, global_duid, global_duidlen);
		dsmp->dsm_cidlen = global_duidlen;
	} else {
		dsmp->dsm_cid = malloc(5 + global_duidlen);
		if (dsmp->dsm_cid == NULL)
			goto alloc_failure;
		dsmp->dsm_cid[0] = 255;
		dsmp->dsm_cid[1] = lif->lif_iaid >> 24;
		dsmp->dsm_cid[2] = lif->lif_iaid >> 16;
		dsmp->dsm_cid[3] = lif->lif_iaid >> 8;
		dsmp->dsm_cid[4] = lif->lif_iaid;
		(void) memcpy(dsmp->dsm_cid + 5, global_duid, global_duidlen);
		dsmp->dsm_cidlen = 5 + global_duidlen;
	}

	return (DHCP_IPC_SUCCESS);

alloc_failure:
	dhcpmsg(MSG_ERR, "get_smach_cid: cannot allocate Client Id for %s",
	    dsmp->dsm_name);
	return (DHCP_IPC_E_MEMORY);
}

/*
 * smach_count(): returns the number of state machines running
 *
 *   input: void
 *  output: uint_t: the number of state machines
 */

uint_t
smach_count(void)
{
	return (global_smach_count);
}

/*
 * discard_default_routes(): removes a state machine's default routes alone.
 *
 *   input: dhcp_smach_t *: the state machine whose default routes need to be
 *			    discarded
 *  output: void
 */

void
discard_default_routes(dhcp_smach_t *dsmp)
{
	free(dsmp->dsm_routers);
	dsmp->dsm_routers = NULL;
	dsmp->dsm_nrouters = 0;
}

/*
 * remove_default_routes(): removes a state machine's default routes from the
 *			    kernel and from the state machine.
 *
 *   input: dhcp_smach_t *: the state machine whose default routes need to be
 *			    removed
 *  output: void
 */

void
remove_default_routes(dhcp_smach_t *dsmp)
{
	int idx;
	uint32_t ifindex;

	if (dsmp->dsm_routers != NULL) {
		ifindex = dsmp->dsm_lif->lif_pif->pif_index;
		for (idx = dsmp->dsm_nrouters - 1; idx >= 0; idx--) {
			if (del_default_route(ifindex,
			    &dsmp->dsm_routers[idx])) {
				dhcpmsg(MSG_DEBUG, "remove_default_routes: "
				    "removed %s from %s",
				    inet_ntoa(dsmp->dsm_routers[idx]),
				    dsmp->dsm_name);
			} else {
				dhcpmsg(MSG_INFO, "remove_default_routes: "
				    "unable to remove %s from %s",
				    inet_ntoa(dsmp->dsm_routers[idx]),
				    dsmp->dsm_name);
			}
		}
		discard_default_routes(dsmp);
	}
}

/*
 * reset_smach(): resets a state machine to its initial state
 *
 *   input: dhcp_smach_t *: the state machine to reset
 *  output: void
 */

void
reset_smach(dhcp_smach_t *dsmp)
{
	dsmp->dsm_dflags &= ~DHCP_IF_FAILED;

	remove_default_routes(dsmp);

	free_pkt_list(&dsmp->dsm_recv_pkt_list);

	if (dsmp->dsm_orig_ack != dsmp->dsm_ack)
		free_pkt_entry(dsmp->dsm_orig_ack);

	free_pkt_entry(dsmp->dsm_ack);

	dsmp->dsm_ack = dsmp->dsm_orig_ack = NULL;

	cancel_smach_timers(dsmp);

	(void) set_smach_state(dsmp, INIT);
	if (dsmp->dsm_isv6) {
		dsmp->dsm_server = ipv6_all_dhcp_relay_and_servers;
	} else {
		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_BROADCAST),
		    &dsmp->dsm_server);
	}
	dsmp->dsm_neg_hrtime		= gethrtime();
	dsmp->dsm_script_fd		= -1;
	dsmp->dsm_script_pid		= -1;
	dsmp->dsm_script_helper_pid	= -1;
	dsmp->dsm_script_callback	= NULL;
	dsmp->dsm_callback_arg		= NULL;
	dsmp->dsm_script_event_id	= -1;
	free(dsmp->dsm_reqhost);
	dsmp->dsm_reqhost		= NULL;
}

/*
 * refresh_smach(): refreshes a given state machine, as though awakened from
 *		    hibernation or by lower layer "link up."
 *
 *   input: dhcp_smach_t *: state machine to refresh
 *  output: void
 */

void
refresh_smach(dhcp_smach_t *dsmp)
{
	if (dsmp->dsm_state == BOUND || dsmp->dsm_state == RENEWING ||
	    dsmp->dsm_state == REBINDING || dsmp->dsm_state == INFORMATION) {
		dhcpmsg(MSG_WARNING, "refreshing state on %s", dsmp->dsm_name);
		cancel_smach_timers(dsmp);
		if (dsmp->dsm_state == INFORMATION)
			dhcp_inform(dsmp);
		else
			dhcp_init_reboot(dsmp);
	}
}

/*
 * refresh_smachs(): refreshes all finite leases under DHCP control
 *
 *   input: iu_eh_t *: unused
 *	    int: unused
 *	    void *: unused
 *  output: void
 */

/* ARGSUSED */
void
refresh_smachs(iu_eh_t *eh, int sig, void *arg)
{
	boolean_t isv6 = B_FALSE;
	dhcp_smach_t *dsmp;

	for (;;) {
		for (dsmp = next_smach(NULL, isv6); dsmp != NULL;
		    dsmp = next_smach(dsmp, isv6)) {
			refresh_smach(dsmp);
		}
		if (isv6)
			break;
		isv6 = B_TRUE;
	}
}

/*
 * nuke_smach_list(): delete the state machine list.  For use when the
 *		      dhcpagent is exiting.
 *
 *   input: none
 *  output: none
 */

void
nuke_smach_list(void)
{
	boolean_t isv6 = B_FALSE;
	dhcp_smach_t *dsmp, *dsmp_next;

	for (;;) {
		for (dsmp = next_smach(NULL, isv6); dsmp != NULL;
		    dsmp = dsmp_next) {
			int	status;
			const char *drop = isv6 ? EVENT_DROP6 : EVENT_DROP;
			const char *release = isv6 ? EVENT_RELEASE6 :
			    EVENT_RELEASE;

			dsmp_next = next_smach(dsmp, isv6);

			cancel_smach_timers(dsmp);
			if (dsmp->dsm_script_pid != -1) {
				/*
				 * Stop a script if it is not for DROP or
				 * RELEASE
				 */
				if (strcmp(dsmp->dsm_script_event, drop) == 0 ||
				    strcmp(dsmp->dsm_script_event, release) ==
				    0) {
					continue;
				}
				script_stop(dsmp);
			}

			/*
			 * If the script is started by script_start, dhcp_drop
			 * and dhcp_release should and will only be called
			 * after the script exits.
			 */
			if (df_get_bool(dsmp->dsm_name, isv6,
			    DF_RELEASE_ON_SIGTERM)) {
				if (script_start(dsmp, release, dhcp_release,
				    "DHCP agent is exiting", &status) == 1) {
					continue;
				}
				if (status == 1)
					continue;
			}
			(void) script_start(dsmp, drop, dhcp_drop, NULL, NULL);
		}
		if (isv6)
			break;
		isv6 = B_TRUE;
	}
}

/*
 * insert_lease(): Create a lease structure on a given state machine.  The
 *		   lease holds a reference to the state machine.
 *
 *   input: dhcp_smach_t *: state machine
 *  output: dhcp_lease_t *: newly-created lease
 */

dhcp_lease_t *
insert_lease(dhcp_smach_t *dsmp)
{
	dhcp_lease_t *dlp;

	if ((dlp = calloc(1, sizeof (*dlp))) == NULL)
		return (NULL);
	dlp->dl_smach = dsmp;
	dlp->dl_hold_count = 1;
	init_timer(&dlp->dl_t1, 0);
	init_timer(&dlp->dl_t2, 0);
	insque(dlp, &dsmp->dsm_leases);
	dhcpmsg(MSG_DEBUG2, "insert_lease: new lease for %s", dsmp->dsm_name);
	return (dlp);
}

/*
 * hold_lease(): acquires a hold on a lease
 *
 *   input: dhcp_lease_t *: the lease to acquire a hold on
 *  output: void
 */

void
hold_lease(dhcp_lease_t *dlp)
{
	dlp->dl_hold_count++;

	dhcpmsg(MSG_DEBUG2, "hold_lease: hold count on lease for %s: %d",
	    dlp->dl_smach->dsm_name, dlp->dl_hold_count);
}

/*
 * release_lease(): releases a hold previously acquired on a lease.
 *		    If the hold count reaches 0, the lease is freed.
 *
 *   input: dhcp_lease_t *: the lease to release the hold on
 *  output: void
 */

void
release_lease(dhcp_lease_t *dlp)
{
	if (dlp->dl_hold_count == 0) {
		dhcpmsg(MSG_CRIT, "release_lease: extraneous release");
		return;
	}

	if (dlp->dl_hold_count == 1 && !dlp->dl_removed) {
		dhcpmsg(MSG_CRIT, "release_lease: missing removal");
		return;
	}

	if (--dlp->dl_hold_count == 0) {
		dhcpmsg(MSG_DEBUG,
		    "release_lease: freeing lease on state machine %s",
		    dlp->dl_smach->dsm_name);
		free(dlp);
	} else {
		dhcpmsg(MSG_DEBUG2,
		    "release_lease: hold count on lease for %s: %d",
		    dlp->dl_smach->dsm_name, dlp->dl_hold_count);
	}
}

/*
 * remove_lease(): removes a given lease from the state machine and drops the
 *		   state machine's hold on the lease.
 *
 *   input: dhcp_lease_t *: the lease to remove
 *  output: void
 */

void
remove_lease(dhcp_lease_t *dlp)
{
	if (dlp->dl_removed) {
		dhcpmsg(MSG_CRIT, "remove_lease: extraneous removal");
	} else {
		dhcp_lif_t *lif, *lifnext;
		uint_t nlifs;

		dhcpmsg(MSG_DEBUG,
		    "remove_lease: removed lease from state machine %s",
		    dlp->dl_smach->dsm_name);
		dlp->dl_removed = B_TRUE;
		remque(dlp);

		cancel_lease_timers(dlp);

		lif = dlp->dl_lifs;
		nlifs = dlp->dl_nlifs;
		for (; nlifs > 0; nlifs--, lif = lifnext) {
			lifnext = lif->lif_next;
			unplumb_lif(lif);
		}

		release_lease(dlp);
	}
}

/*
 * cancel_lease_timer(): cancels a lease-related timer
 *
 *   input: dhcp_lease_t *: the lease to operate on
 *	    dhcp_timer_t *: the timer to cancel
 *  output: void
 */

static void
cancel_lease_timer(dhcp_lease_t *dlp, dhcp_timer_t *dt)
{
	if (dt->dt_id == -1)
		return;
	if (cancel_timer(dt)) {
		release_lease(dlp);
	} else {
		dhcpmsg(MSG_WARNING,
		    "cancel_lease_timer: cannot cancel timer");
	}
}

/*
 * cancel_lease_timers(): cancels an lease's pending timers
 *
 *   input: dhcp_lease_t *: the lease to operate on
 *  output: void
 */

void
cancel_lease_timers(dhcp_lease_t *dlp)
{
	cancel_lease_timer(dlp, &dlp->dl_t1);
	cancel_lease_timer(dlp, &dlp->dl_t2);
}

/*
 * schedule_lease_timer(): schedules a lease-related timer
 *
 *   input: dhcp_lease_t *: the lease to operate on
 *	    dhcp_timer_t *: the timer to schedule
 *	    iu_tq_callback_t *: the callback to call upon firing
 *  output: boolean_t: B_TRUE if the timer was scheduled successfully
 */

boolean_t
schedule_lease_timer(dhcp_lease_t *dlp, dhcp_timer_t *dt,
    iu_tq_callback_t *expire)
{
	/*
	 * If there's a timer running, cancel it and release its lease
	 * reference.
	 */
	if (dt->dt_id != -1) {
		if (!cancel_timer(dt))
			return (B_FALSE);
		release_lease(dlp);
	}

	if (schedule_timer(dt, expire, dlp)) {
		hold_lease(dlp);
		return (B_TRUE);
	} else {
		dhcpmsg(MSG_WARNING,
		    "schedule_lease_timer: cannot schedule timer");
		return (B_FALSE);
	}
}

/*
 * deprecate_leases(): remove all of the leases from a given state machine
 *
 *   input: dhcp_smach_t *: the state machine
 *  output: none
 */

void
deprecate_leases(dhcp_smach_t *dsmp)
{
	dhcp_lease_t *dlp;

	/*
	 * note that due to infelicities in the routing code, any default
	 * routes must be removed prior to canonizing or deprecating the LIF.
	 */

	remove_default_routes(dsmp);

	while ((dlp = dsmp->dsm_leases) != NULL)
		remove_lease(dlp);
}

/*
 * verify_smach(): if the state machine is in a bound state, then verify the
 *		   standing of the configured interfaces.  Abandon those that
 *		   the user has modified.  If we end up with no valid leases,
 *		   then just terminate the state machine.
 *
 *   input: dhcp_smach_t *: the state machine
 *  output: boolean_t: B_TRUE if the state machine is still valid.
 *    note: assumes caller holds a state machine reference; as with most
 *	    callback functions.
 */

boolean_t
verify_smach(dhcp_smach_t *dsmp)
{
	dhcp_lease_t *dlp, *dlpn;

	if (dsmp->dsm_dflags & DHCP_IF_REMOVED) {
		release_smach(dsmp);
		return (B_FALSE);
	}

	if (!dsmp->dsm_isv6) {
		/*
		 * If this is DHCPv4, then verify the main LIF.
		 */
		if (!verify_lif(dsmp->dsm_lif))
			goto smach_terminate;
	}

	/*
	 * If we're not in one of the bound states, then there are no LIFs to
	 * verify here.
	 */
	if (dsmp->dsm_state != BOUND &&
	    dsmp->dsm_state != RENEWING &&
	    dsmp->dsm_state != REBINDING) {
		release_smach(dsmp);
		return (B_TRUE);
	}

	for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlpn) {
		dhcp_lif_t *lif, *lifnext;
		uint_t nlifs;

		dlpn = dlp->dl_next;
		lif = dlp->dl_lifs;
		nlifs = dlp->dl_nlifs;
		for (; nlifs > 0; lif = lifnext, nlifs--) {
			lifnext = lif->lif_next;
			if (!verify_lif(lif)) {
				/*
				 * User has manipulated the interface.  Even
				 * if we plumbed it, we must now disown it.
				 */
				lif->lif_plumbed = B_FALSE;
				remove_lif(lif);
			}
		}
		if (dlp->dl_nlifs == 0)
			remove_lease(dlp);
	}

	/*
	 * If there are leases left, then everything's ok.
	 */
	if (dsmp->dsm_leases != NULL) {
		release_smach(dsmp);
		return (B_TRUE);
	}

smach_terminate:
	finished_smach(dsmp, DHCP_IPC_E_UNKIF);
	release_smach(dsmp);

	return (B_FALSE);
}