summaryrefslogtreecommitdiff
path: root/sbin/dhcpleased/engine.c
blob: 40574ef1aa2fa5d6893e833242c3c26b1951d427 (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
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
/*	$OpenBSD: engine.c,v 1.27 2021/09/15 15:18:23 florian Exp $	*/

/*
 * Copyright (c) 2017, 2021 Florian Obser <florian@openbsd.org>
 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/uio.h>

#include <net/if.h>
#include <net/route.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <pwd.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vis.h>

#include "checksum.h"
#include "log.h"
#include "dhcpleased.h"
#include "engine.h"

/*
 * RFC 2131 4.1 p23 has "SHOULD be 4 seconds", we are a bit more aggressive,
 * networks are faster these days.
 */
#define	START_EXP_BACKOFF	 1
#define	MAX_EXP_BACKOFF_SLOW	64 /* RFC 2131 4.1 p23 */
#define	MAX_EXP_BACKOFF_FAST	 2
#define	MINIMUM(a, b)		(((a) < (b)) ? (a) : (b))

enum if_state {
	IF_DOWN,
	IF_INIT,
	/* IF_SELECTING, */
	IF_REQUESTING,
	IF_BOUND,
	IF_RENEWING,
	IF_REBINDING,
	/* IF_INIT_REBOOT, */
	IF_REBOOTING,
};

const char* if_state_name[] = {
	"Down",
	"Init",
	/* "Selecting", */
	"Requesting",
	"Bound",
	"Renewing",
	"Rebinding",
	/* "Init-Reboot", */
	"Rebooting",
};

struct dhcpleased_iface {
	LIST_ENTRY(dhcpleased_iface)	 entries;
	enum if_state			 state;
	struct event			 timer;
	struct timeval			 timo;
	uint32_t			 if_index;
	int				 rdomain;
	int				 running;
	struct ether_addr		 hw_address;
	int				 link_state;
	uint32_t			 cur_mtu;
	uint32_t			 xid;
	struct timespec			 request_time;
	struct in_addr			 server_identifier;
	struct in_addr			 dhcp_server; /* for unicast */
	struct in_addr			 requested_ip;
	struct in_addr			 mask;
	struct in_addr			 siaddr;
	char				 file[4 * DHCP_FILE_LEN + 1];
	char				 hostname[4 * 255 + 1];
	char				 domainname[4 * 255 + 1];
	struct dhcp_route		 prev_routes[MAX_DHCP_ROUTES];
	int				 prev_routes_len;
	struct dhcp_route		 routes[MAX_DHCP_ROUTES];
	int				 routes_len;
	struct in_addr			 nameservers[MAX_RDNS_COUNT];
	uint32_t			 lease_time;
	uint32_t			 renewal_time;
	uint32_t			 rebinding_time;
};

LIST_HEAD(, dhcpleased_iface) dhcpleased_interfaces;

__dead void		 engine_shutdown(void);
void			 engine_sig_handler(int sig, short, void *);
void			 engine_dispatch_frontend(int, short, void *);
void			 engine_dispatch_main(int, short, void *);
#ifndef	SMALL
void			 send_interface_info(struct dhcpleased_iface *, pid_t);
void			 engine_showinfo_ctl(struct imsg *, uint32_t);
#endif	/* SMALL */
void			 engine_update_iface(struct imsg_ifinfo *);
struct dhcpleased_iface	*get_dhcpleased_iface_by_id(uint32_t);
void			 remove_dhcpleased_iface(uint32_t);
void			 parse_dhcp(struct dhcpleased_iface *,
			     struct imsg_dhcp *);
void			 state_transition(struct dhcpleased_iface *, enum
			     if_state);
void			 iface_timeout(int, short, void *);
void			 request_dhcp_discover(struct dhcpleased_iface *);
void			 request_dhcp_request(struct dhcpleased_iface *);
void			 log_lease(struct dhcpleased_iface *, int);
void			 log_rdns(struct dhcpleased_iface *, int);
void			 send_configure_interface(struct dhcpleased_iface *);
void			 send_rdns_proposal(struct dhcpleased_iface *);
void			 send_deconfigure_interface(struct dhcpleased_iface *);
void			 send_rdns_withdraw(struct dhcpleased_iface *);
void			 send_routes_withdraw(struct dhcpleased_iface *);
void			 parse_lease(struct dhcpleased_iface *,
			     struct imsg_ifinfo *);
int			 engine_imsg_compose_main(int, pid_t, void *, uint16_t);
void			 log_dhcp_hdr(struct dhcp_hdr *);
const char		*dhcp_message_type2str(uint8_t);

#ifndef SMALL
struct dhcpleased_conf	*engine_conf;
#endif /* SMALL */

static struct imsgev	*iev_frontend;
static struct imsgev	*iev_main;
int64_t			 proposal_id;

void
engine_sig_handler(int sig, short event, void *arg)
{
	/*
	 * Normal signal handler rules don't apply because libevent
	 * decouples for us.
	 */

	switch (sig) {
	case SIGINT:
	case SIGTERM:
		engine_shutdown();
	default:
		fatalx("unexpected signal");
	}
}

void
engine(int debug, int verbose)
{
	struct event		 ev_sigint, ev_sigterm;
	struct passwd		*pw;

#ifndef SMALL
	engine_conf = config_new_empty();
#endif /* SMALL */

	log_init(debug, LOG_DAEMON);
	log_setverbose(verbose);

	if ((pw = getpwnam(DHCPLEASED_USER)) == NULL)
		fatal("getpwnam");

	if (chdir("/") == -1)
		fatal("chdir(\"/\")");

	if (unveil("/", "") == -1)
		fatal("unveil /");
	if (unveil(NULL, NULL) == -1)
		fatal("unveil");

	setproctitle("%s", "engine");
	log_procinit("engine");

	if (setgroups(1, &pw->pw_gid) ||
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
		fatal("can't drop privileges");

	if (pledge("stdio recvfd", NULL) == -1)
		fatal("pledge");

	event_init();

	/* Setup signal handler(s). */
	signal_set(&ev_sigint, SIGINT, engine_sig_handler, NULL);
	signal_set(&ev_sigterm, SIGTERM, engine_sig_handler, NULL);
	signal_add(&ev_sigint, NULL);
	signal_add(&ev_sigterm, NULL);
	signal(SIGPIPE, SIG_IGN);
	signal(SIGHUP, SIG_IGN);

	/* Setup pipe and event handler to the main process. */
	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
		fatal(NULL);

	imsg_init(&iev_main->ibuf, 3);
	iev_main->handler = engine_dispatch_main;

	/* Setup event handlers. */
	iev_main->events = EV_READ;
	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
	    iev_main->handler, iev_main);
	event_add(&iev_main->ev, NULL);

	LIST_INIT(&dhcpleased_interfaces);

	event_dispatch();

	engine_shutdown();
}

__dead void
engine_shutdown(void)
{
	/* Close pipes. */
	msgbuf_clear(&iev_frontend->ibuf.w);
	close(iev_frontend->ibuf.fd);
	msgbuf_clear(&iev_main->ibuf.w);
	close(iev_main->ibuf.fd);

	free(iev_frontend);
	free(iev_main);

	log_info("engine exiting");
	exit(0);
}

int
engine_imsg_compose_frontend(int type, pid_t pid, void *data,
    uint16_t datalen)
{
	return (imsg_compose_event(iev_frontend, type, 0, pid, -1,
	    data, datalen));
}

int
engine_imsg_compose_main(int type, pid_t pid, void *data,
    uint16_t datalen)
{
	return (imsg_compose_event(iev_main, type, 0, pid, -1,
	    data, datalen));
}

void
engine_dispatch_frontend(int fd, short event, void *bula)
{
	struct imsgev			*iev = bula;
	struct imsgbuf			*ibuf = &iev->ibuf;
	struct imsg			 imsg;
	struct dhcpleased_iface		*iface;
	ssize_t				 n;
	int				 shut = 0;
#ifndef	SMALL
	int				 verbose;
#endif	/* SMALL */
	uint32_t			 if_index;

	if (event & EV_READ) {
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
			fatal("imsg_read error");
		if (n == 0)	/* Connection closed. */
			shut = 1;
	}
	if (event & EV_WRITE) {
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
			fatal("msgbuf_write");
		if (n == 0)	/* Connection closed. */
			shut = 1;
	}

	for (;;) {
		if ((n = imsg_get(ibuf, &imsg)) == -1)
			fatal("%s: imsg_get error", __func__);
		if (n == 0)	/* No more messages. */
			break;

		switch (imsg.hdr.type) {
#ifndef	SMALL
		case IMSG_CTL_LOG_VERBOSE:
			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
				fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: "
				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
			memcpy(&verbose, imsg.data, sizeof(verbose));
			log_setverbose(verbose);
			break;
		case IMSG_CTL_SHOW_INTERFACE_INFO:
			if (IMSG_DATA_SIZE(imsg) != sizeof(if_index))
				fatalx("%s: IMSG_CTL_SHOW_INTERFACE_INFO wrong "
				    "length: %lu", __func__,
				    IMSG_DATA_SIZE(imsg));
			memcpy(&if_index, imsg.data, sizeof(if_index));
			engine_showinfo_ctl(&imsg, if_index);
			break;
		case IMSG_REQUEST_REBOOT:
			if (IMSG_DATA_SIZE(imsg) != sizeof(if_index))
				fatalx("%s: IMSG_CTL_SEND_DISCOVER wrong "
				    "length: %lu", __func__,
				    IMSG_DATA_SIZE(imsg));
			memcpy(&if_index, imsg.data, sizeof(if_index));
			iface = get_dhcpleased_iface_by_id(if_index);
			if (iface != NULL) {
				switch (iface->state) {
				case IF_DOWN:
					break;
				case IF_INIT:
				case IF_REQUESTING:
					state_transition(iface, iface->state);
					break;
				case IF_RENEWING:
				case IF_REBINDING:
				case IF_REBOOTING:
				case IF_BOUND:
					state_transition(iface, IF_REBOOTING);
					break;
				}
			}
			break;
#endif	/* SMALL */
		case IMSG_REMOVE_IF:
			if (IMSG_DATA_SIZE(imsg) != sizeof(if_index))
				fatalx("%s: IMSG_REMOVE_IF wrong length: %lu",
				    __func__, IMSG_DATA_SIZE(imsg));
			memcpy(&if_index, imsg.data, sizeof(if_index));
			remove_dhcpleased_iface(if_index);
			break;
		case IMSG_DHCP: {
			struct imsg_dhcp	imsg_dhcp;
			if (IMSG_DATA_SIZE(imsg) != sizeof(imsg_dhcp))
				fatalx("%s: IMSG_DHCP wrong length: %lu",
				    __func__, IMSG_DATA_SIZE(imsg));
			memcpy(&imsg_dhcp, imsg.data, sizeof(imsg_dhcp));
			iface = get_dhcpleased_iface_by_id(imsg_dhcp.if_index);
			if (iface != NULL)
				parse_dhcp(iface, &imsg_dhcp);
			break;
		}
		case IMSG_REPROPOSE_RDNS:
			LIST_FOREACH (iface, &dhcpleased_interfaces, entries)
				send_rdns_proposal(iface);
			break;
		default:
			log_debug("%s: unexpected imsg %d", __func__,
			    imsg.hdr.type);
			break;
		}
		imsg_free(&imsg);
	}
	if (!shut)
		imsg_event_add(iev);
	else {
		/* This pipe is dead. Remove its event handler. */
		event_del(&iev->ev);
		event_loopexit(NULL);
	}
}

void
engine_dispatch_main(int fd, short event, void *bula)
{
#ifndef SMALL
	static struct dhcpleased_conf	*nconf;
	static struct iface_conf	*iface_conf;
#endif /* SMALL */
	struct imsg			 imsg;
	struct imsgev			*iev = bula;
	struct imsgbuf			*ibuf = &iev->ibuf;
	struct imsg_ifinfo		 imsg_ifinfo;
	ssize_t				 n;
	int				 shut = 0;

	if (event & EV_READ) {
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
			fatal("imsg_read error");
		if (n == 0)	/* Connection closed. */
			shut = 1;
	}
	if (event & EV_WRITE) {
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
			fatal("msgbuf_write");
		if (n == 0)	/* Connection closed. */
			shut = 1;
	}

	for (;;) {
		if ((n = imsg_get(ibuf, &imsg)) == -1)
			fatal("%s: imsg_get error", __func__);
		if (n == 0)	/* No more messages. */
			break;

		switch (imsg.hdr.type) {
		case IMSG_SOCKET_IPC:
			/*
			 * Setup pipe and event handler to the frontend
			 * process.
			 */
			if (iev_frontend)
				fatalx("%s: received unexpected imsg fd "
				    "to engine", __func__);

			if ((fd = imsg.fd) == -1)
				fatalx("%s: expected to receive imsg fd to "
				   "engine but didn't receive any", __func__);

			iev_frontend = malloc(sizeof(struct imsgev));
			if (iev_frontend == NULL)
				fatal(NULL);

			imsg_init(&iev_frontend->ibuf, fd);
			iev_frontend->handler = engine_dispatch_frontend;
			iev_frontend->events = EV_READ;

			event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
			iev_frontend->events, iev_frontend->handler,
			    iev_frontend);
			event_add(&iev_frontend->ev, NULL);

			if (pledge("stdio", NULL) == -1)
				fatal("pledge");

			break;
		case IMSG_UPDATE_IF:
			if (IMSG_DATA_SIZE(imsg) != sizeof(imsg_ifinfo))
				fatalx("%s: IMSG_UPDATE_IF wrong length: %lu",
				    __func__, IMSG_DATA_SIZE(imsg));
			memcpy(&imsg_ifinfo, imsg.data, sizeof(imsg_ifinfo));
			engine_update_iface(&imsg_ifinfo);
			break;
#ifndef SMALL
		case IMSG_RECONF_CONF:
			if (nconf != NULL)
				fatalx("%s: IMSG_RECONF_CONF already in "
				    "progress", __func__);
			if ((nconf = malloc(sizeof(struct dhcpleased_conf))) ==
			    NULL)
				fatal(NULL);
			SIMPLEQ_INIT(&nconf->iface_list);
			break;
		case IMSG_RECONF_IFACE:
			if (IMSG_DATA_SIZE(imsg) != sizeof(struct
			    iface_conf))
				fatalx("%s: IMSG_RECONF_IFACE wrong length: "
				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
			if ((iface_conf = malloc(sizeof(struct iface_conf)))
			    == NULL)
				fatal(NULL);
			memcpy(iface_conf, imsg.data, sizeof(struct
			    iface_conf));
			iface_conf->vc_id = NULL;
			iface_conf->vc_id_len = 0;
			iface_conf->c_id = NULL;
			iface_conf->c_id_len = 0;
			SIMPLEQ_INSERT_TAIL(&nconf->iface_list,
			    iface_conf, entry);
			break;
		case IMSG_RECONF_VC_ID:
			if (iface_conf == NULL)
				fatal("IMSG_RECONF_VC_ID without "
				    "IMSG_RECONF_IFACE");
			if (IMSG_DATA_SIZE(imsg) > 255 + 2)
				fatalx("%s: IMSG_RECONF_VC_ID wrong length: "
				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
			if ((iface_conf->vc_id = malloc(IMSG_DATA_SIZE(imsg)))
			    == NULL)
				fatal(NULL);
			memcpy(iface_conf->vc_id, imsg.data,
			    IMSG_DATA_SIZE(imsg));
			iface_conf->vc_id_len = IMSG_DATA_SIZE(imsg);
			break;
		case IMSG_RECONF_C_ID:
			if (iface_conf == NULL)
				fatal("IMSG_RECONF_C_ID without "
				    "IMSG_RECONF_IFACE");
			if (IMSG_DATA_SIZE(imsg) > 255 + 2)
				fatalx("%s: IMSG_RECONF_C_ID wrong length: "
				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
			if ((iface_conf->c_id = malloc(IMSG_DATA_SIZE(imsg)))
			    == NULL)
				fatal(NULL);
			memcpy(iface_conf->c_id, imsg.data,
			    IMSG_DATA_SIZE(imsg));
			iface_conf->c_id_len = IMSG_DATA_SIZE(imsg);
			break;
		case IMSG_RECONF_END: {
			struct dhcpleased_iface	*iface;
			int			*ifaces;
			int			 i, if_index;
			char			*if_name;
			char			 ifnamebuf[IF_NAMESIZE];

			if (nconf == NULL)
				fatalx("%s: IMSG_RECONF_END without "
				    "IMSG_RECONF_CONF", __func__);
			ifaces = changed_ifaces(engine_conf, nconf);
			merge_config(engine_conf, nconf);
			nconf = NULL;
			for (i = 0; ifaces[i] != 0; i++) {
				if_index = ifaces[i];
				if_name = if_indextoname(if_index, ifnamebuf);
				iface = get_dhcpleased_iface_by_id(if_index);
				if (if_name == NULL || iface == NULL)
					continue;
				iface_conf = find_iface_conf(
				    &engine_conf->iface_list, if_name);
				if (iface_conf == NULL)
					continue;
				if (iface_conf->ignore & IGN_DNS)
					send_rdns_withdraw(iface);
				if (iface_conf->ignore & IGN_ROUTES)
					send_routes_withdraw(iface);
			}
			free(ifaces);
			break;
		}
#endif /* SMALL */
		default:
			log_debug("%s: unexpected imsg %d", __func__,
			    imsg.hdr.type);
			break;
		}
		imsg_free(&imsg);
	}
	if (!shut)
		imsg_event_add(iev);
	else {
		/* This pipe is dead. Remove its event handler. */
		event_del(&iev->ev);
		event_loopexit(NULL);
	}
}

#ifndef	SMALL
void
send_interface_info(struct dhcpleased_iface *iface, pid_t pid)
{
	struct ctl_engine_info	 cei;

	memset(&cei, 0, sizeof(cei));
	cei.if_index = iface->if_index;
	cei.running = iface->running;
	cei.link_state = iface->link_state;
	strlcpy(cei.state, if_state_name[iface->state], sizeof(cei.state));
	memcpy(&cei.request_time, &iface->request_time,
	    sizeof(cei.request_time));
	cei.server_identifier.s_addr = iface->server_identifier.s_addr;
	cei.dhcp_server.s_addr = iface->dhcp_server.s_addr;
	cei.requested_ip.s_addr = iface->requested_ip.s_addr;
	cei.mask.s_addr = iface->mask.s_addr;
	cei.routes_len = iface->routes_len;
	memcpy(cei.routes, iface->routes, sizeof(cei.routes));
	memcpy(cei.nameservers, iface->nameservers, sizeof(cei.nameservers));
	cei.lease_time = iface->lease_time;
	cei.renewal_time = iface->renewal_time;
	cei.rebinding_time = iface->rebinding_time;
	engine_imsg_compose_frontend(IMSG_CTL_SHOW_INTERFACE_INFO, pid, &cei,
	    sizeof(cei));
}

void
engine_showinfo_ctl(struct imsg *imsg, uint32_t if_index)
{
	struct dhcpleased_iface			*iface;

	switch (imsg->hdr.type) {
	case IMSG_CTL_SHOW_INTERFACE_INFO:
		if ((iface = get_dhcpleased_iface_by_id(if_index)) != NULL)
			send_interface_info(iface, imsg->hdr.pid);
		else
			engine_imsg_compose_frontend(IMSG_CTL_END,
			    imsg->hdr.pid, NULL, 0);
		break;
	default:
		log_debug("%s: error handling imsg", __func__);
		break;
	}
}
#endif	/* SMALL */

void
engine_update_iface(struct imsg_ifinfo *imsg_ifinfo)
{
	struct dhcpleased_iface	*iface;
	int			 need_refresh = 0;

	iface = get_dhcpleased_iface_by_id(imsg_ifinfo->if_index);

	if (iface == NULL) {
		if ((iface = calloc(1, sizeof(*iface))) == NULL)
			fatal("calloc");
		iface->state = IF_DOWN;
		iface->timo.tv_usec = arc4random_uniform(1000000);
		evtimer_set(&iface->timer, iface_timeout, iface);
		iface->if_index = imsg_ifinfo->if_index;
		iface->rdomain = imsg_ifinfo->rdomain;
		iface->running = imsg_ifinfo->running;
		iface->link_state = imsg_ifinfo->link_state;
		iface->requested_ip.s_addr = INADDR_ANY;
		memcpy(&iface->hw_address, &imsg_ifinfo->hw_address,
		    sizeof(struct ether_addr));
		LIST_INSERT_HEAD(&dhcpleased_interfaces, iface, entries);
		need_refresh = 1;
	} else {
		if (memcmp(&iface->hw_address, &imsg_ifinfo->hw_address,
		    sizeof(struct ether_addr)) != 0) {
			memcpy(&iface->hw_address, &imsg_ifinfo->hw_address,
			    sizeof(struct ether_addr));
			need_refresh = 1;
		}
		if (imsg_ifinfo->rdomain != iface->rdomain) {
			iface->rdomain = imsg_ifinfo->rdomain;
			need_refresh = 1;
		}
		if (imsg_ifinfo->running != iface->running) {
			iface->running = imsg_ifinfo->running;
			need_refresh = 1;
		}

		if (imsg_ifinfo->link_state != iface->link_state) {
			iface->link_state = imsg_ifinfo->link_state;
			need_refresh = 1;
		}
	}

	if (!need_refresh)
		return;

	if (iface->running && LINK_STATE_IS_UP(iface->link_state)) {
		if (iface->requested_ip.s_addr == INADDR_ANY)
			parse_lease(iface, imsg_ifinfo);

		if (iface->requested_ip.s_addr == INADDR_ANY)
			state_transition(iface, IF_INIT);
		else
			state_transition(iface, IF_REBOOTING);
	} else
		state_transition(iface, IF_DOWN);
}
struct dhcpleased_iface*
get_dhcpleased_iface_by_id(uint32_t if_index)
{
	struct dhcpleased_iface	*iface;
	LIST_FOREACH (iface, &dhcpleased_interfaces, entries) {
		if (iface->if_index == if_index)
			return (iface);
	}

	return (NULL);
}

void
remove_dhcpleased_iface(uint32_t if_index)
{
	struct dhcpleased_iface	*iface;

	iface = get_dhcpleased_iface_by_id(if_index);

	if (iface == NULL)
		return;

	send_rdns_withdraw(iface);
	send_deconfigure_interface(iface);
	LIST_REMOVE(iface, entries);
	evtimer_del(&iface->timer);
	free(iface);
}

void
parse_dhcp(struct dhcpleased_iface *iface, struct imsg_dhcp *dhcp)
{
	static uint8_t		 cookie[] = DHCP_COOKIE;
	static struct ether_addr bcast_mac;
#ifndef SMALL
	struct iface_conf	*iface_conf;
#endif /* SMALL */
	struct ether_header	*eh;
	struct ether_addr	 ether_src, ether_dst;
	struct ip		*ip;
	struct udphdr		*udp;
	struct dhcp_hdr		*dhcp_hdr;
	struct in_addr		 server_identifier, subnet_mask;
	struct in_addr		 nameservers[MAX_RDNS_COUNT];
	struct dhcp_route	 routes[MAX_DHCP_ROUTES];
	size_t			 rem, i;
	uint32_t		 sum, usum, lease_time = 0, renewal_time = 0;
	uint32_t		 rebinding_time = 0;
	uint8_t			*p, dho = DHO_PAD, dho_len;
	uint8_t			 dhcp_message_type = 0;
	int			 routes_len = 0, routers = 0, csr = 0;
	char			 from[sizeof("xx:xx:xx:xx:xx:xx")];
	char			 to[sizeof("xx:xx:xx:xx:xx:xx")];
	char			 hbuf_src[INET_ADDRSTRLEN];
	char			 hbuf_dst[INET_ADDRSTRLEN];
	char			 hbuf[INET_ADDRSTRLEN];
	char			 domainname[4 * 255 + 1];
	char			 hostname[4 * 255 + 1];
	char			 ifnamebuf[IF_NAMESIZE], *if_name;

	if (bcast_mac.ether_addr_octet[0] == 0)
		memset(bcast_mac.ether_addr_octet, 0xff, ETHER_ADDR_LEN);

	if_name = if_indextoname(iface->if_index, ifnamebuf);

#ifndef SMALL
	iface_conf = find_iface_conf(&engine_conf->iface_list, if_name);
#endif /* SMALL*/

	memset(hbuf_src, 0, sizeof(hbuf_src));
	memset(hbuf_dst, 0, sizeof(hbuf_dst));

	p = dhcp->packet;
	rem = dhcp->len;

	if (rem < sizeof(*eh)) {
		log_warnx("%s: message too short", __func__);
		return;
	}
	eh = (struct ether_header *)p;
	memcpy(ether_src.ether_addr_octet, eh->ether_shost,
	    sizeof(ether_src.ether_addr_octet));
	strlcpy(from, ether_ntoa(&ether_src), sizeof(from));
	memcpy(ether_dst.ether_addr_octet, eh->ether_dhost,
	    sizeof(ether_dst.ether_addr_octet));
	strlcpy(to, ether_ntoa(&ether_dst), sizeof(to));
	p += sizeof(*eh);
	rem -= sizeof(*eh);

	if (memcmp(&ether_dst, &iface->hw_address, sizeof(ether_dst)) != 0 &&
	    memcmp(&ether_dst, &bcast_mac, sizeof(ether_dst)) != 0)
		return ; /* silently ignore packet not for us */

	if (rem < sizeof(*ip))
		goto too_short;

	if (log_getverbose() > 1)
		log_debug("%s, from: %s, to: %s", __func__, from, to);

	ip = (struct ip *)p;

	if (rem < (size_t)ip->ip_hl << 2)
		goto too_short;

	if (wrapsum(checksum((uint8_t *)ip, ip->ip_hl << 2, 0)) != 0) {
		log_warnx("%s: bad IP checksum", __func__);
		return;
	}
	if (rem < ntohs(ip->ip_len))
		goto too_short;

	p += ip->ip_hl << 2;
	rem -= ip->ip_hl << 2;

	if (inet_ntop(AF_INET, &ip->ip_src, hbuf_src, sizeof(hbuf_src)) == NULL)
		hbuf_src[0] = '\0';
	if (inet_ntop(AF_INET, &ip->ip_dst, hbuf_dst, sizeof(hbuf_dst)) == NULL)
		hbuf_dst[0] = '\0';

#ifndef SMALL
	if (iface_conf != NULL) {
		for (i = 0; (int)i < iface_conf->ignore_servers_len; i++) {
			if (iface_conf->ignore_servers[i].s_addr ==
			    ip->ip_src.s_addr) {
				log_debug("ignoring server %s", hbuf_src);
				return;
			}
		}
	}
#endif /* SMALL */

	if (rem < sizeof(*udp))
		goto too_short;

	udp = (struct udphdr *)p;
	if (rem < ntohs(udp->uh_ulen))
		goto too_short;

	if (rem > ntohs(udp->uh_ulen)) {
		if (log_getverbose() > 1) {
			log_debug("%s: accepting packet with %lu bytes of data"
			    " after udp payload", __func__, rem -
			    ntohs(udp->uh_ulen));
		}
		rem = ntohs(udp->uh_ulen);
	}

	p += sizeof(*udp);
	rem -= sizeof(*udp);

	usum = udp->uh_sum;
	udp->uh_sum = 0;

	sum = wrapsum(checksum((uint8_t *)udp, sizeof(*udp), checksum(p, rem,
	    checksum((uint8_t *)&ip->ip_src, 2 * sizeof(ip->ip_src),
	    IPPROTO_UDP + ntohs(udp->uh_ulen)))));

	if (usum != 0 && usum != sum) {
		log_warnx("%s: bad UDP checksum", __func__);
		return;
	}

	if (log_getverbose() > 1) {
		log_debug("%s: %s:%d -> %s:%d", __func__, hbuf_src,
		    ntohs(udp->uh_sport), hbuf_dst, ntohs(udp->uh_dport));
	}

	if (ntohs(udp->uh_sport) != SERVER_PORT ||
	    ntohs(udp->uh_dport) != CLIENT_PORT) {
		log_warnx("%s: invalid ports used %s:%d -> %s:%d", __func__,
		    hbuf_src, ntohs(udp->uh_sport),
		    hbuf_dst, ntohs(udp->uh_dport));
		return;
	}
	if (rem < sizeof(*dhcp_hdr))
		goto too_short;

	dhcp_hdr = (struct dhcp_hdr *)p;
	p += sizeof(*dhcp_hdr);
	rem -= sizeof(*dhcp_hdr);

	dhcp_hdr->sname[DHCP_SNAME_LEN -1 ] = '\0'; /* ensure it's a string */
	dhcp_hdr->file[DHCP_FILE_LEN -1 ] = '\0'; /* ensure it's a string */

	if (log_getverbose() > 1)
		log_dhcp_hdr(dhcp_hdr);

	if (dhcp_hdr->op != DHCP_BOOTREPLY) {
		log_warnx("%s: ignorning non-reply packet", __func__);
		return;
	}

	if (dhcp_hdr->xid != iface->xid)
		return; /* silently ignore wrong xid */

	if (rem < sizeof(cookie))
		goto too_short;

	if (memcmp(p, cookie, sizeof(cookie)) != 0) {
		log_warnx("%s: no dhcp cookie in packet from %s", __func__,
		    from);
		return;
	}
	p += sizeof(cookie);
	rem -= sizeof(cookie);

	memset(&server_identifier, 0, sizeof(server_identifier));
	memset(&subnet_mask, 0, sizeof(subnet_mask));
	memset(&routes, 0, sizeof(routes));
	memset(&nameservers, 0, sizeof(nameservers));

	while (rem > 0 && dho != DHO_END) {
		dho = *p;
		p += 1;
		rem -= 1;
		/* only DHO_END and DHO_PAD are 1 byte long without length */
		if (dho == DHO_PAD || dho == DHO_END)
			dho_len = 0;
		else {
			if (rem == 0)
				goto too_short; /* missing option length */
			dho_len = *p;
			p += 1;
			rem -= 1;
			if (rem < dho_len)
				goto too_short;
		}

		switch (dho) {
		case DHO_PAD:
			if (log_getverbose() > 1)
				log_debug("DHO_PAD");
			break;
		case DHO_END:
			if (log_getverbose() > 1)
				log_debug("DHO_END");
			break;
		case DHO_DHCP_MESSAGE_TYPE:
			if (dho_len != 1)
				goto wrong_length;
			dhcp_message_type = *p;
			if (log_getverbose() > 1) {
				log_debug("DHO_DHCP_MESSAGE_TYPE: %s",
				    dhcp_message_type2str(dhcp_message_type));
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DHCP_SERVER_IDENTIFIER:
			if (dho_len != sizeof(server_identifier))
				goto wrong_length;
			memcpy(&server_identifier, p,
			    sizeof(server_identifier));
			if (log_getverbose() > 1) {
				log_debug("DHO_DHCP_SERVER_IDENTIFIER: %s",
				    inet_ntop(AF_INET, &server_identifier,
				    hbuf, sizeof(hbuf)));
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DHCP_LEASE_TIME:
			if (dho_len != sizeof(lease_time))
				goto wrong_length;
			memcpy(&lease_time, p, sizeof(lease_time));
			lease_time = ntohl(lease_time);
			if (log_getverbose() > 1) {
				log_debug("DHO_DHCP_LEASE_TIME %us",
				    lease_time);
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_SUBNET_MASK:
			if (dho_len != sizeof(subnet_mask))
				goto wrong_length;
			memcpy(&subnet_mask, p, sizeof(subnet_mask));
			if (log_getverbose() > 1) {
				log_debug("DHO_SUBNET_MASK: %s",
				    inet_ntop(AF_INET, &subnet_mask, hbuf,
				    sizeof(hbuf)));
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_ROUTERS:
			if (dho_len < sizeof(routes[routes_len].gw))
				goto wrong_length;
			if (dho_len % sizeof(routes[routes_len].gw) != 0)
				goto wrong_length;

			/*
			 * Ignore routers option if classless static routes
			 * are present (RFC3442).
			 */
			if (!csr) {
				routers = 1;
				while (routes_len < MAX_DHCP_ROUTES &&
				    dho_len > 0) {
					memcpy(&routes[routes_len].gw, p,
					    sizeof(routes[routes_len].gw));
					if (log_getverbose() > 1) {
						log_debug("DHO_ROUTER: %s",
						    inet_ntop(AF_INET,
						    &routes[routes_len].gw,
						    hbuf, sizeof(hbuf)));
					}
					p += sizeof(routes[routes_len].gw);
					rem -= sizeof(routes[routes_len].gw);
					dho_len -=
					    sizeof(routes[routes_len].gw);
					routes_len++;
				}
			}
			if (dho_len != 0) {
				/* ignore > MAX_DHCP_ROUTES routes */
				p += dho_len;
				rem -= dho_len;
			}
			break;
		case DHO_DOMAIN_NAME_SERVERS:
			if (dho_len < sizeof(nameservers[0]))
				goto wrong_length;
			if (dho_len % sizeof(nameservers[0]) != 0)
				goto wrong_length;
			/* we limit ourself to 8 nameservers for proposals */
			memcpy(&nameservers, p, MINIMUM(sizeof(nameservers),
			    dho_len));
			if (log_getverbose() > 1) {
				for (i = 0; i < MINIMUM(sizeof(nameservers),
				    dho_len / sizeof(nameservers[0])); i++) {
					log_debug("DHO_DOMAIN_NAME_SERVERS: %s "
					    "(%lu/%lu)", inet_ntop(AF_INET,
					    &nameservers[i], hbuf,
					    sizeof(hbuf)), i + 1,
					    dho_len / sizeof(nameservers[0]));
				}
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_HOST_NAME:
			if ( dho_len < 1)
				goto wrong_length;
			strvisx(hostname, p, dho_len, VIS_SAFE);
			if (log_getverbose() > 1)
				log_debug("DHO_HOST_NAME: %s", hostname);
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DOMAIN_NAME:
			if ( dho_len < 1)
				goto wrong_length;
			strvisx(domainname, p, dho_len, VIS_SAFE);
			if (log_getverbose() > 1)
				log_debug("DHO_DOMAIN_NAME: %s", domainname);
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DHCP_RENEWAL_TIME:
			if (dho_len != sizeof(renewal_time))
				goto wrong_length;
			memcpy(&renewal_time, p, sizeof(renewal_time));
			renewal_time = ntohl(renewal_time);
			if (log_getverbose() > 1) {
				log_debug("DHO_DHCP_RENEWAL_TIME %us",
				    renewal_time);
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DHCP_REBINDING_TIME:
			if (dho_len != sizeof(rebinding_time))
				goto wrong_length;
			memcpy(&rebinding_time, p, sizeof(rebinding_time));
			rebinding_time = ntohl(rebinding_time);
			if (log_getverbose() > 1) {
				log_debug("DHO_DHCP_REBINDING_TIME %us",
				    rebinding_time);
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_DHCP_CLIENT_IDENTIFIER:
			/* the server is supposed to echo this back to us */
#ifndef SMALL
			if (iface_conf != NULL && iface_conf->c_id_len > 0) {
				if (dho_len != iface_conf->c_id[1]) {
					log_warnx("wrong "
					    "DHO_DHCP_CLIENT_IDENTIFIER");
					return;
				}
				if (memcmp(p, &iface_conf->c_id[2], dho_len) !=
				    0) {
					log_warnx("wrong "
					    "DHO_DHCP_CLIENT_IDENTIFIER");
					return;
				}
			} else
#endif /* SMALL */
			{
				if (dho_len != 1 + sizeof(iface->hw_address))
					goto wrong_length;
				if (*p != HTYPE_ETHER) {
					log_warnx("DHO_DHCP_CLIENT_IDENTIFIER: "
					    "wrong type");
					return;
				}
				if (memcmp(p + 1, &iface->hw_address,
				    sizeof(iface->hw_address)) != 0) {
					log_warnx("wrong "
					    "DHO_DHCP_CLIENT_IDENTIFIER");
					return;
				}
			}
			p += dho_len;
			rem -= dho_len;
			break;
		case DHO_CLASSLESS_STATIC_ROUTES: {
			int	prefixlen, compressed_prefixlen;

			csr = 1;
			if (routers) {
				/*
				 * Ignore routers option if classless static
				 * routes are present (RFC3442).
				 */
				routers = 0;
				routes_len = 0;
			}
			while (routes_len < MAX_DHCP_ROUTES && dho_len > 0) {
				prefixlen = *p;
				p += 1;
				rem -= 1;
				dho_len -= 1;

				if (prefixlen < 0 || prefixlen > 32) {
					log_warnx("%s: invalid prefixlen: %d",
					    __func__, prefixlen);
					return;
				}

				if (prefixlen > 0)
					routes[routes_len].mask.s_addr =
					    htonl(0xffffffff << (32 -
						prefixlen));
				else
					routes[routes_len].mask.s_addr =
					    INADDR_ANY;

				compressed_prefixlen = (prefixlen + 7) / 8;
				if (dho_len < compressed_prefixlen)
					goto wrong_length;

				memcpy(&routes[routes_len].dst, p,
				    compressed_prefixlen);
				p += compressed_prefixlen;
				rem -= compressed_prefixlen;
				dho_len -= compressed_prefixlen;

				if (dho_len < sizeof(routes[routes_len].gw))
					goto wrong_length;

				memcpy(&routes[routes_len].gw, p,
				    sizeof(routes[routes_len].gw));
				p += sizeof(routes[routes_len].gw);
				rem -= sizeof(routes[routes_len].gw);
				dho_len -= sizeof(routes[routes_len].gw);

				routes_len++;
			}

			if (dho_len != 0) {
				/* ignore > MAX_DHCP_ROUTES routes */
				p += dho_len;
				rem -= dho_len;
			}
			break;
		}
		default:
			if (log_getverbose() > 1)
				log_debug("DHO_%u, len: %u", dho, dho_len);
			p += dho_len;
			rem -= dho_len;
		}

	}
	while (rem != 0) {
		if (*p != DHO_PAD)
			break;
		p++;
		rem--;
	}
	if (rem != 0)
		log_warnx("%s: %lu bytes garbage data from %s", __func__, rem,
		    from);

	log_debug("%s on %s from %s/%s to %s/%s",
	    dhcp_message_type2str(dhcp_message_type), if_name == NULL ? "?" :
	    if_name, from, hbuf_src, to, hbuf_dst);

	switch (dhcp_message_type) {
	case DHCPOFFER:
		if (iface->state != IF_INIT) {
			log_debug("ignoring unexpected DHCPOFFER");
			return;
		}
		if (server_identifier.s_addr == INADDR_ANY &&
		    dhcp_hdr->yiaddr.s_addr == INADDR_ANY) {
			log_warnx("%s: did not receive server identifier or "
			    "offered IP address", __func__);
			return;
		}
		iface->server_identifier.s_addr = server_identifier.s_addr;
		iface->requested_ip.s_addr = dhcp_hdr->yiaddr.s_addr;
		state_transition(iface, IF_REQUESTING);
		break;
	case DHCPACK:
		switch (iface->state) {
		case IF_REQUESTING:
		case IF_RENEWING:
		case IF_REBINDING:
		case IF_REBOOTING:
			break;
		default:
			log_debug("ignoring unexpected DHCPACK");
			return;
		}
		if (server_identifier.s_addr == INADDR_ANY &&
		    dhcp_hdr->yiaddr.s_addr == INADDR_ANY) {
			log_warnx("%s: did not receive server identifier or "
			    "offered IP address", __func__);
			return;
		}
		if (lease_time == 0) {
			log_warnx("%s no lease time from %s", __func__, from);
			return;
		}
		if (subnet_mask.s_addr == INADDR_ANY) {
			log_warnx("%s: no subnetmask received from %s",
			    __func__, from);
			return;
		}

		/* Defaults if we didn't receive renewal or rebinding time. */
		if (renewal_time == 0)
			renewal_time = lease_time / 2;
		if (rebinding_time == 0)
			rebinding_time = lease_time - (lease_time / 8);

		/* RFC 2131 4.4.5 */
		/* Ignore invalid T1/T2 options */
		if (renewal_time >= rebinding_time) {
			log_warnx("%s: renewal_time(%u) >= rebinding_time(%u) "
			    "from %s: using defaults",
			    __func__, renewal_time, rebinding_time, from);
			renewal_time = rebinding_time = 0;
		} else if (rebinding_time >= lease_time) {
			log_warnx("%s: rebinding_time(%u) >= lease_time(%u) "
			    "from %s: using defaults",
			    __func__, rebinding_time, lease_time, from);
			renewal_time = rebinding_time = 0;
		}

		/* Defaults if we received wrong renewal or rebinding time. */
		if (renewal_time == 0)
			renewal_time = lease_time / 2;
		if (rebinding_time == 0)
			rebinding_time = lease_time - (lease_time / 8);

		clock_gettime(CLOCK_MONOTONIC, &iface->request_time);
		iface->server_identifier.s_addr = server_identifier.s_addr;
		iface->requested_ip.s_addr = dhcp_hdr->yiaddr.s_addr;
		iface->mask.s_addr = subnet_mask.s_addr;
#ifndef SMALL
		if (iface_conf != NULL && iface_conf->ignore & IGN_ROUTES) {
			iface->routes_len = 0;
			memset(iface->routes, 0, sizeof(iface->routes));
		} else
#endif /* SMALL */
		{
			iface->prev_routes_len = iface->routes_len;
			memcpy(iface->prev_routes, iface->routes,
			    sizeof(iface->prev_routes));
			iface->routes_len = routes_len;
			memcpy(iface->routes, routes, sizeof(iface->routes));
		}
		iface->lease_time = lease_time;
		iface->renewal_time = renewal_time;
		iface->rebinding_time = rebinding_time;

#ifndef SMALL
		if (iface_conf != NULL && iface_conf->ignore & IGN_DNS) {
			memset(iface->nameservers, 0,
			    sizeof(iface->nameservers));
		} else
#endif /* SMALL */
		{
			memcpy(iface->nameservers, nameservers,
			    sizeof(iface->nameservers));
		}

		iface->siaddr.s_addr = dhcp_hdr->siaddr.s_addr;

		/* we made sure this is a string futher up */
		strnvis(iface->file, dhcp_hdr->file, sizeof(iface->file),
		    VIS_SAFE);

		strlcpy(iface->domainname, domainname,
		    sizeof(iface->domainname));
		strlcpy(iface->hostname, hostname, sizeof(iface->hostname));
		state_transition(iface, IF_BOUND);
		break;
	case DHCPNAK:
		switch (iface->state) {
		case IF_REQUESTING:
		case IF_RENEWING:
		case IF_REBINDING:
		case IF_REBOOTING:
			break;
		default:
			log_debug("ignoring unexpected DHCPNAK");
			return;
		}

		state_transition(iface, IF_INIT);
		break;
	default:
		log_warnx("%s: unimplemented message type %d", __func__,
		    dhcp_message_type);
		break;
	}
	return;
 too_short:
	log_warnx("%s: message from %s too short", __func__, from);
	return;
 wrong_length:
	log_warnx("%s: received option %d with wrong length: %d", __func__,
	    dho, dho_len);
	return;
}

/* XXX check valid transitions */
void
state_transition(struct dhcpleased_iface *iface, enum if_state new_state)
{
	enum if_state	 old_state = iface->state;
	struct timespec	 now, res;
	char		 ifnamebuf[IF_NAMESIZE], *if_name;

	iface->state = new_state;
	switch (new_state) {
	case IF_DOWN:
		if (iface->requested_ip.s_addr == INADDR_ANY) {
			/* nothing to do until iface comes up */
			iface->timo.tv_sec = -1;
			break;
		}
		if (old_state == IF_DOWN) {
			/* nameservers already withdrawn when if went down */
			send_deconfigure_interface(iface);
			/* nothing more to do until iface comes back */
			iface->timo.tv_sec = -1;
		} else {
			send_rdns_withdraw(iface);
			clock_gettime(CLOCK_MONOTONIC, &now);
			timespecsub(&now, &iface->request_time, &res);
			iface->timo.tv_sec = iface->lease_time - res.tv_sec;
			if (iface->timo.tv_sec < 0)
				iface->timo.tv_sec = 0; /* deconfigure now */
		}
		break;
	case IF_INIT:
		switch (old_state) {
		case IF_INIT:
			if (iface->timo.tv_sec < MAX_EXP_BACKOFF_SLOW)
				iface->timo.tv_sec *= 2;
			break;
		case IF_REQUESTING:
		case IF_RENEWING:
		case IF_REBINDING:
		case IF_REBOOTING:
			/* lease expired, got DHCPNAK or timeout: delete IP */
			send_rdns_withdraw(iface);
			send_deconfigure_interface(iface);
			/* fall through */
		case IF_DOWN:
			iface->timo.tv_sec = START_EXP_BACKOFF;
			break;
		case IF_BOUND:
			fatal("invalid transition Bound -> Init");
			break;
		}
		request_dhcp_discover(iface);
		break;
	case IF_REBOOTING:
		if (old_state == IF_REBOOTING)
			iface->timo.tv_sec *= 2;
		else {
			/* make sure we send broadcast */
			iface->dhcp_server.s_addr = INADDR_ANY;
			iface->timo.tv_sec = START_EXP_BACKOFF;
		}
		request_dhcp_request(iface);
		break;
	case IF_REQUESTING:
		if (old_state == IF_REQUESTING)
			iface->timo.tv_sec *= 2;
		else
			iface->timo.tv_sec = START_EXP_BACKOFF;
		request_dhcp_request(iface);
		break;
	case IF_BOUND:
		iface->timo.tv_sec = iface->renewal_time;
		if (old_state == IF_REQUESTING || old_state == IF_REBOOTING) {
			send_configure_interface(iface);
			send_rdns_proposal(iface);
		}
		break;
	case IF_RENEWING:
		if (old_state == IF_BOUND) {
			iface->dhcp_server.s_addr =
			    iface->server_identifier.s_addr;
			iface->server_identifier.s_addr = INADDR_ANY;

			iface->timo.tv_sec = (iface->rebinding_time -
			    iface->renewal_time) / 2; /* RFC 2131 4.4.5 */
		} else
			iface->timo.tv_sec /= 2;

		if (iface->timo.tv_sec < 60)
			iface->timo.tv_sec = 60;
		request_dhcp_request(iface);
		break;
	case IF_REBINDING:
		if (old_state == IF_RENEWING) {
			iface->dhcp_server.s_addr = INADDR_ANY;
			iface->timo.tv_sec = (iface->lease_time -
			    iface->renewal_time) / 2; /* RFC 2131 4.4.5 */
		} else
			iface->timo.tv_sec /= 2;
		request_dhcp_request(iface);
		break;
	}

	if_name = if_indextoname(iface->if_index, ifnamebuf);
	log_debug("%s[%s] %s -> %s, timo: %lld", __func__, if_name == NULL ?
	    "?" : if_name, if_state_name[old_state], if_state_name[new_state],
	    iface->timo.tv_sec);

	if (iface->timo.tv_sec == -1) {
		if (evtimer_pending(&iface->timer, NULL))
			evtimer_del(&iface->timer);
	} else
		evtimer_add(&iface->timer, &iface->timo);
}

void
iface_timeout(int fd, short events, void *arg)
{
	struct dhcpleased_iface	*iface = (struct dhcpleased_iface *)arg;
	struct timespec		 now, res;

	log_debug("%s[%d]: %s", __func__, iface->if_index,
	    if_state_name[iface->state]);

	switch (iface->state) {
	case IF_DOWN:
		state_transition(iface, IF_DOWN);
		break;
	case IF_INIT:
		state_transition(iface, IF_INIT);
		break;
	case IF_REBOOTING:
		if (iface->timo.tv_sec >= MAX_EXP_BACKOFF_FAST)
			state_transition(iface, IF_INIT);
		else
			state_transition(iface, IF_REBOOTING);
		break;
	case IF_REQUESTING:
		if (iface->timo.tv_sec >= MAX_EXP_BACKOFF_SLOW)
			state_transition(iface, IF_INIT);
		else
			state_transition(iface, IF_REQUESTING);
		break;
	case IF_BOUND:
		state_transition(iface, IF_RENEWING);
		break;
	case IF_RENEWING:
		clock_gettime(CLOCK_MONOTONIC, &now);
		timespecsub(&now, &iface->request_time, &res);
		log_debug("%s: res.tv_sec: %lld, rebinding_time: %u", __func__,
		    res.tv_sec, iface->rebinding_time);
		if (res.tv_sec > iface->rebinding_time)
			state_transition(iface, IF_REBINDING);
		else
			state_transition(iface, IF_RENEWING);
		break;
	case IF_REBINDING:
		clock_gettime(CLOCK_MONOTONIC, &now);
		timespecsub(&now, &iface->request_time, &res);
		log_debug("%s: res.tv_sec: %lld, lease_time: %u", __func__,
		    res.tv_sec, iface->lease_time);
		if (res.tv_sec > iface->lease_time)
			state_transition(iface, IF_INIT);
		else
			state_transition(iface, IF_REBINDING);
		break;
	}
}

void
request_dhcp_discover(struct dhcpleased_iface *iface)
{
	struct imsg_req_discover	 imsg_req_discover;

	imsg_req_discover.if_index = iface->if_index;
	imsg_req_discover.xid = iface->xid = arc4random();
	engine_imsg_compose_frontend(IMSG_SEND_DISCOVER, 0, &imsg_req_discover,
	    sizeof(imsg_req_discover));
}

void
request_dhcp_request(struct dhcpleased_iface *iface)
{
	struct imsg_req_request	 imsg_req_request;

	iface->xid = arc4random();
	imsg_req_request.if_index = iface->if_index;
	imsg_req_request.xid = iface->xid;
	imsg_req_request.server_identifier.s_addr =
	    iface->server_identifier.s_addr;
	imsg_req_request.requested_ip.s_addr = iface->requested_ip.s_addr;
	imsg_req_request.dhcp_server.s_addr =  iface->dhcp_server.s_addr;
	engine_imsg_compose_frontend(IMSG_SEND_REQUEST, 0, &imsg_req_request,
	    sizeof(imsg_req_request));
}

void
log_lease(struct dhcpleased_iface *iface, int deconfigure)
{
	char	 hbuf_lease[INET_ADDRSTRLEN], hbuf_server[INET_ADDRSTRLEN];
	char	 ifnamebuf[IF_NAMESIZE], *if_name;

	if_name = if_indextoname(iface->if_index, ifnamebuf);
	inet_ntop(AF_INET, &iface->requested_ip, hbuf_lease,
	    sizeof(hbuf_lease));
	inet_ntop(AF_INET, &iface->server_identifier, hbuf_server,
	    sizeof(hbuf_server));


	if (deconfigure)
		log_info("deleting %s from %s (lease from %s)", hbuf_lease,
		    if_name == NULL ? "?" : if_name, hbuf_server);
	else
		log_info("adding %s to %s (lease from %s)", hbuf_lease,
		    if_name == NULL ? "?" : if_name, hbuf_server);
}

void
send_configure_interface(struct dhcpleased_iface *iface)
{
	struct imsg_configure_interface	 imsg;
	int				 i, j, found;

	log_lease(iface, 0);

	memset(&imsg, 0, sizeof(imsg));
	imsg.if_index = iface->if_index;
	imsg.rdomain = iface->rdomain;
	imsg.addr.s_addr = iface->requested_ip.s_addr;
	imsg.mask.s_addr = iface->mask.s_addr;
	imsg.siaddr.s_addr = iface->siaddr.s_addr;
	strlcpy(imsg.file, iface->file, sizeof(imsg.file));
	strlcpy(imsg.domainname, iface->domainname, sizeof(imsg.domainname));
	strlcpy(imsg.hostname, iface->hostname, sizeof(imsg.hostname));
	for (i = 0; i < iface->prev_routes_len; i++) {
		found = 0;
		for (j = 0; j < iface->routes_len; j++) {
			if (memcmp(&iface->prev_routes[i], &iface->routes[j],
			    sizeof(struct dhcp_route)) == 0) {
				found = 1;
				break;
			}
		}
		if (!found)
			imsg.routes[imsg.routes_len++] = iface->prev_routes[i];
	}
	if (imsg.routes_len > 0)
		engine_imsg_compose_main(IMSG_WITHDRAW_ROUTES, 0, &imsg,
		    sizeof(imsg));
	imsg.routes_len = iface->routes_len;
	memcpy(imsg.routes, iface->routes, sizeof(imsg.routes));
	engine_imsg_compose_main(IMSG_CONFIGURE_INTERFACE, 0, &imsg,
	    sizeof(imsg));
}

void
send_deconfigure_interface(struct dhcpleased_iface *iface)
{
	struct imsg_configure_interface	 imsg;

	if (iface->requested_ip.s_addr == INADDR_ANY)
		return;

	log_lease(iface, 1);

	imsg.if_index = iface->if_index;
	imsg.rdomain = iface->rdomain;
	imsg.addr.s_addr = iface->requested_ip.s_addr;
	imsg.mask.s_addr = iface->mask.s_addr;
	imsg.siaddr.s_addr = iface->siaddr.s_addr;
	strlcpy(imsg.file, iface->file, sizeof(imsg.file));
	strlcpy(imsg.domainname, iface->domainname, sizeof(imsg.domainname));
	strlcpy(imsg.hostname, iface->hostname, sizeof(imsg.hostname));
	imsg.routes_len = iface->routes_len;
	memcpy(imsg.routes, iface->routes, sizeof(imsg.routes));
	engine_imsg_compose_main(IMSG_DECONFIGURE_INTERFACE, 0, &imsg,
	    sizeof(imsg));

	iface->server_identifier.s_addr = INADDR_ANY;
	iface->dhcp_server.s_addr = INADDR_ANY;
	iface->requested_ip.s_addr = INADDR_ANY;
	iface->mask.s_addr = INADDR_ANY;
	iface->routes_len = 0;
	memset(iface->routes, 0, sizeof(iface->routes));
}

void
send_routes_withdraw(struct dhcpleased_iface *iface)
{
	struct imsg_configure_interface	 imsg;

	if (iface->requested_ip.s_addr == INADDR_ANY || iface->routes_len == 0)
		return;

	imsg.if_index = iface->if_index;
	imsg.rdomain = iface->rdomain;
	imsg.addr.s_addr = iface->requested_ip.s_addr;
	imsg.mask.s_addr = iface->mask.s_addr;
	imsg.siaddr.s_addr = iface->siaddr.s_addr;
	strlcpy(imsg.file, iface->file, sizeof(imsg.file));
	strlcpy(imsg.domainname, iface->domainname, sizeof(imsg.domainname));
	strlcpy(imsg.hostname, iface->hostname, sizeof(imsg.hostname));
	imsg.routes_len = iface->routes_len;
	memcpy(imsg.routes, iface->routes, sizeof(imsg.routes));
	engine_imsg_compose_main(IMSG_WITHDRAW_ROUTES, 0, &imsg,
	    sizeof(imsg));
}

void
log_rdns(struct dhcpleased_iface *iface, int withdraw)
{
	int	 i;
	char	 hbuf_rdns[INET_ADDRSTRLEN], hbuf_server[INET_ADDRSTRLEN];
	char	 ifnamebuf[IF_NAMESIZE], *if_name, *rdns_buf = NULL, *tmp_buf;

	if_name = if_indextoname(iface->if_index, ifnamebuf);

	inet_ntop(AF_INET, &iface->server_identifier, hbuf_server,
	    sizeof(hbuf_server));

	for (i = 0; i < MAX_RDNS_COUNT && iface->nameservers[i].s_addr !=
		 INADDR_ANY; i++) {
		inet_ntop(AF_INET, &iface->nameservers[i], hbuf_rdns,
		    sizeof(hbuf_rdns));
		tmp_buf = rdns_buf;
		if (asprintf(&rdns_buf, "%s %s", tmp_buf ? tmp_buf : "",
		    hbuf_rdns) < 0) {
			rdns_buf = NULL;
			break;
		}
		free(tmp_buf);
	}

	if (rdns_buf != NULL) {
		if (withdraw) {
			log_info("deleting nameservers%s (lease from %s on %s)",
			    rdns_buf, hbuf_server, if_name == NULL ? "?" :
			    if_name);
		} else {
			log_info("adding nameservers%s (lease from %s on %s)",
			    rdns_buf, hbuf_server, if_name == NULL ? "?" :
			    if_name);
		}
		free(rdns_buf);
	}
}

void
send_rdns_proposal(struct dhcpleased_iface *iface)
{
	struct imsg_propose_rdns	 imsg;

	log_rdns(iface, 0);

	memset(&imsg, 0, sizeof(imsg));

	imsg.if_index = iface->if_index;
	imsg.rdomain = iface->rdomain;
	for (imsg.rdns_count = 0; imsg.rdns_count < MAX_RDNS_COUNT &&
		 iface->nameservers[imsg.rdns_count].s_addr != INADDR_ANY;
	     imsg.rdns_count++)
		;
	memcpy(imsg.rdns, iface->nameservers, sizeof(imsg.rdns));
	engine_imsg_compose_main(IMSG_PROPOSE_RDNS, 0, &imsg, sizeof(imsg));
}

void
send_rdns_withdraw(struct dhcpleased_iface *iface)
{
	struct imsg_propose_rdns	 imsg;

	log_rdns(iface, 1);

	memset(&imsg, 0, sizeof(imsg));

	imsg.if_index = iface->if_index;
	imsg.rdomain = iface->rdomain;
	engine_imsg_compose_main(IMSG_WITHDRAW_RDNS, 0, &imsg, sizeof(imsg));
	memset(iface->nameservers, 0, sizeof(iface->nameservers));
}

void
parse_lease(struct dhcpleased_iface *iface, struct imsg_ifinfo *imsg_ifinfo)
{
	char	*p, *p1;

	/* make sure this is a string */
	imsg_ifinfo->lease[sizeof(imsg_ifinfo->lease) - 1] = '\0';

	iface->requested_ip.s_addr = INADDR_ANY;

	if ((p = strstr(imsg_ifinfo->lease, LEASE_IP_PREFIX)) == NULL)
		return;

	p += sizeof(LEASE_IP_PREFIX) - 1;
	if ((p1 = strchr(p, '\n')) == NULL)
		return;
	*p1 = '\0';

	if (inet_pton(AF_INET, p, &iface->requested_ip) != 1)
		iface->requested_ip.s_addr = INADDR_ANY;
}

void
log_dhcp_hdr(struct dhcp_hdr *dhcp_hdr)
{
#ifndef	SMALL
	char	 hbuf[INET_ADDRSTRLEN];

	log_debug("dhcp_hdr op: %s (%d)", dhcp_hdr->op == DHCP_BOOTREQUEST ?
	    "Boot Request" : dhcp_hdr->op == DHCP_BOOTREPLY ? "Boot Reply" :
	    "Unknown", dhcp_hdr->op);
	log_debug("dhcp_hdr htype: %s (%d)", dhcp_hdr->htype == 1 ? "Ethernet":
	    "Unknown", dhcp_hdr->htype);
	log_debug("dhcp_hdr hlen: %d", dhcp_hdr->hlen);
	log_debug("dhcp_hdr hops: %d", dhcp_hdr->hops);
	log_debug("dhcp_hdr xid: 0x%x", dhcp_hdr->xid);
	log_debug("dhcp_hdr secs: %u", dhcp_hdr->secs);
	log_debug("dhcp_hdr flags: 0x%x", dhcp_hdr->flags);
	log_debug("dhcp_hdr ciaddr: %s", inet_ntop(AF_INET, &dhcp_hdr->ciaddr,
	    hbuf, sizeof(hbuf)));
	log_debug("dhcp_hdr yiaddr: %s", inet_ntop(AF_INET, &dhcp_hdr->yiaddr,
	    hbuf, sizeof(hbuf)));
	log_debug("dhcp_hdr siaddr: %s", inet_ntop(AF_INET, &dhcp_hdr->siaddr,
	    hbuf, sizeof(hbuf)));
	log_debug("dhcp_hdr giaddr: %s", inet_ntop(AF_INET, &dhcp_hdr->giaddr,
	    hbuf, sizeof(hbuf)));
	log_debug("dhcp_hdr chaddr: %02x:%02x:%02x:%02x:%02x:%02x "
	    "(%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x)",
	    dhcp_hdr->chaddr[0], dhcp_hdr->chaddr[1], dhcp_hdr->chaddr[2],
	    dhcp_hdr->chaddr[3], dhcp_hdr->chaddr[4], dhcp_hdr->chaddr[5],
	    dhcp_hdr->chaddr[6], dhcp_hdr->chaddr[7], dhcp_hdr->chaddr[8],
	    dhcp_hdr->chaddr[9], dhcp_hdr->chaddr[10], dhcp_hdr->chaddr[11],
	    dhcp_hdr->chaddr[12], dhcp_hdr->chaddr[13], dhcp_hdr->chaddr[14],
	    dhcp_hdr->chaddr[15]);
	/* ignore sname and file, if we ever print it use strvis(3) */
#endif
}

const char *
dhcp_message_type2str(uint8_t dhcp_message_type)
{
	switch (dhcp_message_type) {
	case DHCPDISCOVER:
		return "DHCPDISCOVER";
	case DHCPOFFER:
		return "DHCPOFFER";
	case DHCPREQUEST:
		return "DHCPREQUEST";
	case DHCPDECLINE:
		return "DHCPDECLINE";
	case DHCPACK:
		return "DHCPACK";
	case DHCPNAK:
		return "DHCPNAK";
	case DHCPRELEASE:
		return "DHCPRELEASE";
	case DHCPINFORM:
		return "DHCPINFORM";
	default:
		return "Unknown";
	}
}