1#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <fcntl.h>
5
6#include "dhcp.h"
7#include "config.h"
8
9#ifndef DEFAULT_LEASETIME
10#define DEFAULT_LEASETIME	3600	/* 1 hour */
11#endif
12
13#define REQUEST	(1 << 0)
14#define UINT8	(1 << 1)
15#define UINT16	(1 << 2)
16#define SINT16	(1 << 3)
17#define UINT32	(1 << 4)
18#define SINT32	(1 << 5)
19#define IPV4	(1 << 6)
20#define STRING	(1 << 7)
21#define PAIR	(1 << 8)
22#define ARRAY	(1 << 9)
23#define RFC3361	(1 << 10)
24#define RFC3397	(1 << 11)
25#define RFC3442 (1 << 12)
26
27struct dhcp_opt {
28	uint8_t option;
29	int type;
30	const char *var;
31};
32
33static const struct dhcp_opt const dhcp_opts[] = {
34	{ 1,	IPV4 | REQUEST,	"subnet_mask" },
35	{ 2,	UINT32,		"time_offset" },
36	{ 3,	IPV4 | ARRAY | REQUEST,	"routers" },
37	{ 4,	IPV4 | ARRAY,	"time_servers" },
38	{ 5,	IPV4 | ARRAY,	"ien116_name_servers" },
39	{ 6,	IPV4 | ARRAY,	"domain_name_servers" },
40	{ 7,	IPV4 | ARRAY,	"log_servers" },
41	{ 8,	IPV4 | ARRAY,	"cookie_servers" },
42	{ 9, 	IPV4 | ARRAY,	"lpr_servers" },
43	{ 10,	IPV4 | ARRAY,	"impress_servers" },
44	{ 11,	IPV4 | ARRAY,	"resource_location_servers" },
45	{ 12,	STRING,		"host_name" },
46	{ 13,	UINT16,		"boot_size" },
47	{ 14,	STRING,		"merit_dump" },
48	{ 15,	STRING,		"domain_name" },
49	{ 16,	IPV4,		"swap_server" },
50	{ 17,	STRING,		"root_path" },
51	{ 18,	STRING,		"extensions_path" },
52	{ 19,	UINT8,		"ip_forwarding" },
53	{ 20,	UINT8,		"non_local_source_routing" },
54	{ 21,	IPV4 | ARRAY,	"policy_filter" },
55	{ 22,	SINT16,		"max_dgram_reassembly" },
56	{ 23,	UINT16,		"default_ip_ttl" },
57	{ 24,	UINT32,		"path_mtu_aging_timeout" },
58	{ 25,	UINT16 | ARRAY,	"path_mtu_plateau_table" },
59	{ 26,	UINT16,		"interface_mtu" },
60	{ 27,	UINT8,		"all_subnets_local" },
61	{ 28,	IPV4 | REQUEST,	"broadcast_address" },
62	{ 29,	UINT8,		"perform_mask_discovery" },
63	{ 30,	UINT8,		"mask_supplier" },
64	{ 31,	UINT8,		"router_discovery" },
65	{ 32,	IPV4,		"router_solicitation_address" },
66	{ 33,	IPV4 | ARRAY | REQUEST,	"static_routes" },
67	{ 34,	UINT8,		"trailer_encapsulation" },
68	{ 35, 	UINT32,		"arp_cache_timeout" },
69	{ 36,	UINT16,		"ieee802_3_encapsulation" },
70	{ 37,	UINT8,		"default_tcp_ttl" },
71	{ 38,	UINT32,		"tcp_keepalive_interval" },
72	{ 39,	UINT8,		"tcp_keepalive_garbage" },
73	{ 40,	STRING,		"nis_domain" },
74	{ 41,	IPV4 | ARRAY,	"nis_servers" },
75	{ 42,	IPV4 | ARRAY,	"ntp_servers" },
76	{ 43,	STRING,		"vendor_encapsulated_options" },
77	{ 44,	IPV4 | ARRAY,	"netbios_name_servers" },
78	{ 45,	IPV4,		"netbios_dd_server" },
79	{ 46,	UINT8,		"netbios_node_type" },
80	{ 47,	STRING,		"netbios_scope" },
81	{ 48,	IPV4 | ARRAY,	"font_servers" },
82	{ 49,	IPV4 | ARRAY,	"x_display_manager" },
83	{ 50, 	IPV4,		"dhcp_requested_address" },
84	{ 51,	UINT32 | REQUEST,	"dhcp_lease_time" },
85	{ 52,	UINT8,		"dhcp_option_overload" },
86	{ 53,	UINT8,		"dhcp_message_type" },
87	{ 54,	IPV4,		"dhcp_server_identifier" },
88	{ 55,	UINT8 | ARRAY,	"dhcp_parameter_request_list" },
89	{ 56,	STRING,		"dhcp_message" },
90	{ 57,	UINT16,		"dhcp_max_message_size" },
91	{ 58,	UINT32 | REQUEST,	"dhcp_renewal_time" },
92	{ 59,	UINT32 | REQUEST,	"dhcp_rebinding_time" },
93	{ 64,	STRING,		"nisplus_domain" },
94	{ 65,	IPV4 | ARRAY,	"nisplus_servers" },
95	{ 66,	STRING,		"tftp_server_name" },
96	{ 67,	STRING,		"bootfile_name" },
97	{ 68,	IPV4 | ARRAY,	"mobile_ip_home_agent" },
98	{ 69,	IPV4 | ARRAY,	"smtp_server" },
99	{ 70,	IPV4 | ARRAY,	"pop_server" },
100	{ 71,	IPV4 | ARRAY,	"nntp_server" },
101	{ 72,	IPV4 | ARRAY,	"www_server" },
102	{ 73,	IPV4 | ARRAY,	"finger_server" },
103	{ 74,	IPV4 | ARRAY,	"irc_server" },
104	{ 75,	IPV4 | ARRAY,	"streettalk_server" },
105	{ 76,	IPV4 | ARRAY,	"streettalk_directory_assistance_server" },
106	{ 77,	STRING,		"user_class" },
107	{ 85,	IPV4 | ARRAY,	"nds_servers" },
108	{ 86,	STRING,		"nds_tree_name" },
109	{ 87,	STRING,		"nds_context" },
110	{ 88,	STRING | RFC3397,	"bcms_controller_names" },
111	{ 89,	IPV4 | ARRAY,	"bcms_controller_address" },
112	{ 91,	UINT32,		"client_last_transaction_time" },
113	{ 92,	IPV4 | ARRAY,	"associated_ip" },
114	{ 98,	STRING,		"uap_servers" },
115	{ 112,	IPV4 | ARRAY,	"netinfo_server_address" },
116	{ 113,	STRING,		"netinfo_server_tag" },
117	{ 114,	STRING,		"default_url" },
118	{ 118,	IPV4,		"subnet_selection" },
119	{ 119,	STRING | RFC3397,	"domain_search" },
120	{ 121,  RFC3442 | REQUEST,	"classless_static_routes" },
121	{ 249,  RFC3442,	"ms-classless_static_routes" },
122	{ 0, 0, NULL }
123};
124
125struct dhcp_message *
126get_lease_from_file(const char *leasefile)
127{
128	int fd;
129	struct dhcp_message *dhcp;
130	ssize_t bytes;
131
132	fd = open(leasefile, O_RDONLY);
133	if (fd == -1)
134		return NULL;
135	dhcp = malloc(sizeof(*dhcp));
136	memset(dhcp, 0, sizeof(*dhcp));
137	bytes = read(fd, dhcp, sizeof(*dhcp));
138	close(fd);
139	if (bytes < 0) {
140		free(dhcp);
141		dhcp = NULL;
142	}
143	return dhcp;
144}
145
146static uint8_t *dhcp_opt_buffer = NULL;
147
148static int
149valid_length(uint8_t option, int dl, int *type)
150{
151	const struct dhcp_opt *opt;
152	ssize_t sz;
153
154	if (dl == 0)
155		return -1;
156
157	for (opt = dhcp_opts; opt->option; opt++) {
158		if (opt->option != option)
159			continue;
160
161		if (type)
162			*type = opt->type;
163
164		if (opt->type == 0 || opt->type & STRING || opt->type & RFC3442)
165			return 0;
166
167		sz = 0;
168		if (opt->type & UINT32 || opt->type & IPV4)
169			sz = sizeof(uint32_t);
170		if (opt->type & UINT16)
171			sz = sizeof(uint16_t);
172		if (opt->type & UINT8)
173			sz = sizeof(uint8_t);
174		if (opt->type & IPV4 || opt->type & ARRAY)
175			return dl % sz;
176		return (dl == sz ? 0 : -1);
177	}
178
179	/* unknown option, so let it pass */
180	return 0;
181}
182
183static void
184free_option_buffer(void)
185{
186	free(dhcp_opt_buffer);
187}
188
189
190#define get_option_raw(dhcp, opt) get_option(dhcp, opt, NULL, NULL)
191static const uint8_t *
192get_option(const struct dhcp_message *dhcp, uint8_t opt, int *len, int *type)
193{
194	const uint8_t *p = dhcp->options;
195	const uint8_t *e = p + sizeof(dhcp->options);
196	uint8_t l, ol = 0;
197	uint8_t o = 0;
198	uint8_t overl = 0;
199	uint8_t *bp = NULL;
200	const uint8_t *op = NULL;
201	int bl = 0;
202
203	while (p < e) {
204		o = *p++;
205		if (o == opt) {
206			if (op) {
207				if (!dhcp_opt_buffer) {
208					dhcp_opt_buffer = malloc(sizeof(struct dhcp_message));
209					atexit(free_option_buffer);
210				}
211				if (!bp)
212					bp = dhcp_opt_buffer;
213				memcpy(bp, op, ol);
214				bp += ol;
215			}
216			ol = *p;
217			op = p + 1;
218			bl += ol;
219		}
220		switch (o) {
221		case DHO_PAD:
222			continue;
223		case DHO_END:
224			if (overl & 1) {
225				/* bit 1 set means parse boot file */
226				overl &= ~1;
227				p = dhcp->bootfile;
228				e = p + sizeof(dhcp->bootfile);
229			} else if (overl & 2) {
230				/* bit 2 set means parse server name */
231				overl &= ~2;
232				p = dhcp->servername;
233				e = p + sizeof(dhcp->servername);
234			} else
235				goto exit;
236			break;
237		case DHO_OPTIONSOVERLOADED:
238			/* Ensure we only get this option once */
239			if (!overl)
240				overl = p[1];
241			break;
242		}
243		l = *p++;
244		p += l;
245	}
246
247exit:
248	if (valid_length(o, bl, type) == -1) {
249		errno = EINVAL;
250		return NULL;
251	}
252	if (len)
253		*len = bl;
254	if (bp) {
255		memcpy(bp, op, ol);
256		return (const uint8_t *)&dhcp_opt_buffer;
257	}
258	if (op)
259		return op;
260	errno = ENOENT;
261	return NULL;
262}
263
264int
265get_option_addr32(uint32_t *a, const struct dhcp_message *dhcp, uint8_t option)
266{
267	const uint8_t *p = get_option_raw(dhcp, option);
268
269	if (!p)
270		return -1;
271	memcpy(a, p, sizeof(*a));
272	return 0;
273}
274
275int
276get_option_uint32(uint32_t *i, const struct dhcp_message *dhcp, uint8_t option)
277{
278	uint32_t a;
279
280	if (get_option_addr32(&a, dhcp, option) == -1)
281		return -1;
282
283	*i = ntohl(a);
284	return 0;
285}
286
287uint32_t
288get_netmask(uint32_t addr)
289{
290	uint32_t dst;
291
292	if (addr == 0)
293		return 0;
294
295	dst = htonl(addr);
296	if (IN_CLASSA(dst))
297		return ntohl(IN_CLASSA_NET);
298	if (IN_CLASSB (dst))
299		return ntohl(IN_CLASSB_NET);
300	if (IN_CLASSC (dst))
301		return ntohl(IN_CLASSC_NET);
302
303	return 0;
304}
305
306void showlease(struct dhcp_lease *lease)
307{
308    printf("addr:      %s\n", inet_ntoa(lease->addr));
309    printf("net:       %s\n", inet_ntoa(lease->net));
310    printf("leasetime: %d\n", lease->leasetime);
311    printf("renew:     %d\n", lease->renewaltime);
312    printf("rebind:    %d\n", lease->rebindtime);
313    printf("server:    %s\n", inet_ntoa(lease->server));
314}
315#define MAX_LEASETIME 2147460
316
317int
318main(int argc, char *argv[])
319{
320    struct dhcp_message *dhcp;
321    struct dhcp_lease *lease;
322    char leasefile[PATH_MAX];
323
324    if (argc < 2) {
325        fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
326        exit(1);
327    }
328    snprintf(leasefile, PATH_MAX, LEASEFILE, argv[1]);
329    if ((dhcp = get_lease_from_file(leasefile)) == NULL) {
330        fprintf(stderr, "Couldn't read lease file: %s\n", strerror(errno));
331        exit(1);
332    }
333    lease = malloc(sizeof(*lease));
334    lease->frominfo = 0;
335    lease->addr.s_addr = dhcp->yiaddr;
336
337    if (get_option_addr32(&lease->net.s_addr, dhcp, DHO_SUBNETMASK) == -1)
338        lease->net.s_addr = get_netmask(dhcp->yiaddr);
339    if (get_option_uint32(&lease->leasetime, dhcp, DHO_LEASETIME) != 0)
340        lease->leasetime = DEFAULT_LEASETIME;
341    get_option_addr32(&lease->server.s_addr, dhcp, DHO_SERVERID);
342    /* Dm: limit lease time value to avoid negative numbers when
343       converting to milliseconds */
344    if ((lease->leasetime != ~0U) && (lease->leasetime > MAX_LEASETIME))
345        lease->leasetime = MAX_LEASETIME;
346    if (get_option_uint32(&lease->renewaltime, dhcp, DHO_RENEWALTIME) != 0)
347        lease->renewaltime = 0;
348    if (get_option_uint32(&lease->rebindtime, dhcp, DHO_REBINDTIME) != 0)
349        lease->rebindtime = 0;
350    showlease(lease);
351    free(lease);
352    return 0;
353}
354