summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdhcpagent/common/dhcp_hostconf.c
blob: 49fa6a8f38920ead29a67e1184bd62de25729542 (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
/*
 * 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.
 */

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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netinet/dhcp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>			/* memcpy */
#include <fcntl.h>
#include <limits.h>

#include "dhcp_hostconf.h"

static void		relativize_time(DHCP_OPT *, time_t, time_t);
static void		relativize_v6(uint32_t *, time_t, time_t);

/*
 * ifname_to_hostconf(): converts an interface name into a hostconf file for
 *			 that interface
 *
 *   input: const char *: the interface name
 *	    boolean_t: B_TRUE if using DHCPv6
 *  output: char *: the hostconf filename
 *    note: uses an internal static buffer (not threadsafe)
 */

char *
ifname_to_hostconf(const char *ifname, boolean_t isv6)
{
	static char filename[sizeof (DHCP_HOSTCONF_TMPL6) + LIFNAMSIZ];

	(void) snprintf(filename, sizeof (filename), "%s%s%s",
	    DHCP_HOSTCONF_PREFIX, ifname,
	    isv6 ? DHCP_HOSTCONF_SUFFIX6 : DHCP_HOSTCONF_SUFFIX);

	return (filename);
}

/*
 * remove_hostconf(): removes an interface.dhc file
 *
 *   input: const char *: the interface name
 *	    boolean_t: B_TRUE if using DHCPv6
 *  output: int: 0 if the file is removed, -1 if it can't be removed
 *          (errno is set)
 */

int
remove_hostconf(const char *ifname, boolean_t isv6)
{
	return (unlink(ifname_to_hostconf(ifname, isv6)));
}

/*
 * read_hostconf(): reads the contents of an <if>.dhc file into a PKT_LIST
 *
 *   input: const char *: the interface name
 *	    PKT_LIST **: a pointer to a PKT_LIST * to store the info in
 *	    uint_t: the length of the list of PKT_LISTs
 *	    boolean_t: B_TRUE if using DHCPv6
 *  output: int: >0 if the file is read and loaded into the PKT_LIST *
 *	    successfully, -1 otherwise (errno is set)
 *    note: the PKT and PKT_LISTs are dynamically allocated here
 */

int
read_hostconf(const char *ifname, PKT_LIST **plpp, uint_t plplen,
    boolean_t isv6)
{
	PKT_LIST	*plp = NULL;
	PKT		*pkt = NULL;
	int		fd;
	time_t		orig_time, current_time = time(NULL);
	uint32_t	lease;
	uint32_t	magic;
	int		pcnt = 0;
	int		retval;

	fd = open(ifname_to_hostconf(ifname, isv6), O_RDONLY);
	if (fd == -1)
		return (-1);

	if (read(fd, &magic, sizeof (magic)) != sizeof (magic))
		goto failure;

	if (magic != (isv6 ? DHCP_HOSTCONF_MAGIC6 : DHCP_HOSTCONF_MAGIC))
		goto failure;

	if (read(fd, &orig_time, sizeof (orig_time)) != sizeof (orig_time))
		goto failure;

	/*
	 * read the packet back in from disk, and for v4, run it through
	 * dhcp_options_scan(). note that we use calloc() because
	 * dhcp_options_scan() relies on the structure being zeroed.
	 */

	for (pcnt = 0; pcnt < plplen; pcnt++) {

		plp = NULL;
		pkt = NULL;

		if ((plp = calloc(1, sizeof (PKT_LIST))) == NULL)
			goto failure;

		retval = read(fd, &plp->len, sizeof (plp->len));
		if (retval == 0 && pcnt != 0) {
			/*
			 * Reached end of file on a boundary, but after
			 * we've read at least one packet, so we consider
			 * this successful, allowing us to use files from
			 * older versions of the agent happily.
			 */
			free(plp);
			break;
		} else if (retval != sizeof (plp->len))
			goto failure;

		if ((pkt = malloc(plp->len)) == NULL)
			goto failure;

		if (read(fd, pkt, plp->len) != plp->len)
			goto failure;

		plp->pkt = pkt;

		plpp[pcnt] = plp;

		if (!isv6 && dhcp_options_scan(plp, B_TRUE) != 0)
			goto failure;

		/*
		 * First packet used to validate that we're interested,
		 * the rest are presumed to be historical reference and
		 * are not relativized
		 */
		if (pcnt == 0)
			continue;

		if (isv6) {
			dhcpv6_option_t	d6o;
			dhcpv6_ia_na_t	d6in;
			dhcpv6_iaaddr_t	d6ia;
			uchar_t		*opts, *optmax, *subomax;

			/*
			 * Loop over contents of the packet to find the address
			 * options.
			 */
			opts = (uchar_t *)pkt + sizeof (dhcpv6_message_t);
			optmax = (uchar_t *)pkt + plp->len;
			while (opts + sizeof (d6o) <= optmax) {

				/*
				 * Extract option header and make sure option
				 * is intact.
				 */
				(void) memcpy(&d6o, opts, sizeof (d6o));
				d6o.d6o_code = ntohs(d6o.d6o_code);
				d6o.d6o_len = ntohs(d6o.d6o_len);
				subomax = opts + sizeof (d6o) + d6o.d6o_len;
				if (subomax > optmax)
					break;

				/*
				 * If this isn't an option that contains
				 * address or prefix leases, then skip over it.
				 */
				if (d6o.d6o_code != DHCPV6_OPT_IA_NA &&
				    d6o.d6o_code != DHCPV6_OPT_IA_TA &&
				    d6o.d6o_code != DHCPV6_OPT_IA_PD) {
					opts = subomax;
					continue;
				}

				/*
				 * Handle the option first.
				 */
				if (d6o.d6o_code == DHCPV6_OPT_IA_TA) {
					/* no timers in this structure */
					opts += sizeof (dhcpv6_ia_ta_t);
				} else {
					/* both na and pd */
					if (opts + sizeof (d6in) > subomax) {
						opts = subomax;
						continue;
					}
					(void) memcpy(&d6in, opts,
					    sizeof (d6in));
					relativize_v6(&d6in.d6in_t1, orig_time,
					    current_time);
					relativize_v6(&d6in.d6in_t2, orig_time,
					    current_time);
					(void) memcpy(opts, &d6in,
					    sizeof (d6in));
					opts += sizeof (d6in);
				}

				/*
				 * Now handle each suboption (address) inside.
				 */
				while (opts + sizeof (d6o) <= subomax) {
					/*
					 * Verify the suboption header first.
					 */
					(void) memcpy(&d6o, opts,
					    sizeof (d6o));
					d6o.d6o_code = ntohs(d6o.d6o_code);
					d6o.d6o_len = ntohs(d6o.d6o_len);
					if (opts + sizeof (d6o) + d6o.d6o_len >
					    subomax)
						break;
					if (d6o.d6o_code != DHCPV6_OPT_IAADDR) {
						opts += sizeof (d6o) +
						    d6o.d6o_len;
						continue;
					}

					/*
					 * Now process the contents.
					 */
					if (opts + sizeof (d6ia) > subomax)
						break;
					(void) memcpy(&d6ia, opts,
					    sizeof (d6ia));
					relativize_v6(&d6ia.d6ia_preflife,
					    orig_time, current_time);
					relativize_v6(&d6ia.d6ia_vallife,
					    orig_time, current_time);
					(void) memcpy(opts, &d6ia,
					    sizeof (d6ia));
					opts += sizeof (d6o) + d6o.d6o_len;
				}
				opts = subomax;
			}
		} else {

			/*
			 * make sure the IPv4 DHCP lease is still valid.
			 */

			if (plp->opts[CD_LEASE_TIME] != NULL &&
			    plp->opts[CD_LEASE_TIME]->len ==
			    sizeof (lease_t)) {

				(void) memcpy(&lease,
				    plp->opts[CD_LEASE_TIME]->value,
				    sizeof (lease_t));

				lease = ntohl(lease);
				if ((lease != DHCP_PERM) &&
				    (orig_time + lease) <= current_time)
					goto failure;
			}

			relativize_time(plp->opts[CD_T1_TIME], orig_time,
			    current_time);
			relativize_time(plp->opts[CD_T2_TIME], orig_time,
			    current_time);
			relativize_time(plp->opts[CD_LEASE_TIME], orig_time,
			    current_time);
		}
	}

	(void) close(fd);
	return (pcnt);

failure:
	free(pkt);
	free(plp);
	while (pcnt-- > 0) {
		free(plpp[pcnt]->pkt);
		free(plpp[pcnt]);
	}
	(void) close(fd);
	return (-1);
}

/*
 * write_hostconf(): writes the contents of a PKT_LIST into an <if>.dhc file
 *
 *   input: const char *: the interface name
 *	    PKT_LIST **: a list of pointers to PKT_LIST to write
 *	    uint_t: length of the list of PKT_LIST pointers
 *	    time_t: a starting time to treat the relative lease times
 *		    in the first packet as relative to
 *	    boolean_t: B_TRUE if using DHCPv6
 *  output: int: 0 if the file is written successfully, -1 otherwise
 *	    (errno is set)
 */

int
write_hostconf(
    const char *ifname,
    PKT_LIST *pl[],
    uint_t pllen,
    time_t relative_to,
    boolean_t isv6)
{
	int		fd;
	struct iovec	iov[IOV_MAX];
	int		retval;
	uint32_t	magic;
	ssize_t		explen = 0; /* Expected length of write */
	int		i, iovlen = 0;

	fd = open(ifname_to_hostconf(ifname, isv6), O_WRONLY|O_CREAT|O_TRUNC,
	    0600);
	if (fd == -1)
		return (-1);

	/*
	 * first write our magic number, then the relative time of the
	 * leases, then for each packet we write the length of the packet
	 * followed by the packet.  we will then use the relative time in
	 * read_hostconf() to recalculate the lease times for the first packet.
	 */

	magic = isv6 ? DHCP_HOSTCONF_MAGIC6 : DHCP_HOSTCONF_MAGIC;
	iov[iovlen].iov_base = (caddr_t)&magic;
	explen += iov[iovlen++].iov_len  = sizeof (magic);
	iov[iovlen].iov_base = (caddr_t)&relative_to;
	explen += iov[iovlen++].iov_len  = sizeof (relative_to);
	for (i = 0; i < pllen && iovlen < (IOV_MAX - 1); i++) {
		iov[iovlen].iov_base = (caddr_t)&pl[i]->len;
		explen += iov[iovlen++].iov_len  = sizeof (pl[i]->len);
		iov[iovlen].iov_base = (caddr_t)pl[i]->pkt;
		explen += iov[iovlen++].iov_len  = pl[i]->len;
	}

	retval = writev(fd, iov, iovlen);

	(void) close(fd);

	if (retval != explen)
		return (-1);

	return (0);
}

/*
 * relativize_time(): re-relativizes a time in a DHCP option
 *
 *   input: DHCP_OPT *: the DHCP option parameter to convert
 *	    time_t: the time the leases in the packet are currently relative to
 *	    time_t: the current time which leases will become relative to
 *  output: void
 */

static void
relativize_time(DHCP_OPT *option, time_t orig_time, time_t current_time)
{
	uint32_t	pkt_time;
	time_t		time_diff = current_time - orig_time;

	if (option == NULL || option->len != sizeof (lease_t))
		return;

	(void) memcpy(&pkt_time, option->value, option->len);
	if (ntohl(pkt_time) != DHCP_PERM)
		pkt_time = htonl(ntohl(pkt_time) - time_diff);

	(void) memcpy(option->value, &pkt_time, option->len);
}

/*
 * relativize_v6(): re-relativizes a time in a DHCPv6 option
 *
 *   input: uint32_t *: the time value to convert
 *	    time_t: the time the leases in the packet are currently relative to
 *	    time_t: the current time which leases will become relative to
 *  output: void
 */

static void
relativize_v6(uint32_t *val, time_t orig_time, time_t current_time)
{
	uint32_t	hval;
	time_t		time_diff = current_time - orig_time;

	hval = ntohl(*val);
	if (hval != DHCPV6_INFTIME) {
		if (hval < time_diff)
			*val = 0;
		else
			*val = htonl(hval - time_diff);
	}
}