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
|
/*
* 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.
*
* Routines used to extract/insert DHCP options. Must be kept MT SAFE,
* as they are called from different threads.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include "dhcp_impl.h"
#if defined(_KERNEL) && !defined(_BOOT)
#include <sys/sunddi.h>
#else
#include <strings.h>
#endif /* _KERNEL && !_BOOT */
static uint8_t bootmagic[] = BOOTMAGIC;
/*
* Scan field for options.
*/
static void
field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
uint8_t last_option)
{
uint8_t *current;
while (start < end) {
if (*start == CD_PAD) {
start++;
continue;
}
if (*start == CD_END)
break; /* done */
if (*start > last_option) {
if (++start < end)
start += *start + 1;
continue; /* unrecognized option */
}
current = start;
if (++start < end)
start += *start + 1; /* advance to next option */
/* all options besides CD_END and CD_PAD should have a len */
if ((current + 1) >= end)
continue;
/* Ignores duplicate options. */
if (options[*current] == NULL) {
options[*current] = (DHCP_OPT *)current;
/* verify that len won't go beyond end */
if ((current + options[*current]->len + 1) >= end) {
options[*current] = NULL;
continue;
}
}
}
}
/*
* Scan Vendor field for options.
*/
static void
vendor_scan(PKT_LIST *pl)
{
uint8_t *start, *end, len;
if (pl->opts[CD_VENDOR_SPEC] == NULL)
return;
len = pl->opts[CD_VENDOR_SPEC]->len;
start = pl->opts[CD_VENDOR_SPEC]->value;
/* verify that len won't go beyond the end of the packet */
if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
return;
end = start + len;
field_scan(start, end, pl->vs, VS_OPTION_END);
}
/*
* Load opts table in PKT_LIST entry with PKT's options.
* Returns 0 if no fatal errors occur, otherwise...
*/
int
dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
{
PKT *pkt = pl->pkt;
uint_t opt_size = pl->len - BASE_PKT_SIZE;
/*
* bcmp() is used here instead of memcmp() since kernel/standalone
* doesn't have a memcmp().
*/
if (pl->len < BASE_PKT_SIZE ||
bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
pl->rfc1048 = 0;
return (0);
}
pl->rfc1048 = 1;
/* check the options field */
field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
DHCP_LAST_OPT);
/*
* process vendor specific options. We look at the vendor options
* here, simply because a BOOTP server could fake DHCP vendor
* options. This increases our interoperability with BOOTP.
*/
if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
vendor_scan(pl);
if (pl->opts[CD_DHCP_TYPE] == NULL)
return (0);
if (pl->opts[CD_DHCP_TYPE]->len != 1)
return (DHCP_GARBLED_MSG_TYPE);
if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
*pl->opts[CD_DHCP_TYPE]->value > INFORM)
return (DHCP_WRONG_MSG_TYPE);
if (pl->opts[CD_OPTION_OVERLOAD]) {
if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
case 1:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
break;
case 2:
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
case 3:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
default:
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
}
return (0);
}
/*
* Locate a DHCPv6 option or suboption within a buffer. DHCPv6 uses nested
* options within options, and this function is designed to work with both
* primary options and the suboptions contained within.
*
* The 'oldopt' is a previous option pointer, and is typically used to iterate
* over options of the same code number. The 'codenum' is in host byte order
* for simplicity. 'retlenp' may be NULL, and if present gets the _entire_
* option length (including header).
*
* Warning: the returned pointer has no particular alignment because DHCPv6
* defines options without alignment. The caller must deal with unaligned
* pointers carefully.
*/
dhcpv6_option_t *
dhcpv6_find_option(const void *buffer, size_t buflen,
const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
{
const uchar_t *bp;
dhcpv6_option_t d6o;
uint_t olen;
codenum = htons(codenum);
bp = buffer;
while (buflen >= sizeof (dhcpv6_option_t)) {
(void) memcpy(&d6o, bp, sizeof (d6o));
olen = ntohs(d6o.d6o_len) + sizeof (d6o);
if (olen > buflen)
break;
if (d6o.d6o_code != codenum ||
(oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
bp += olen;
buflen -= olen;
continue;
}
if (retlenp != NULL)
*retlenp = olen;
/* LINTED: alignment */
return ((dhcpv6_option_t *)bp);
}
return (NULL);
}
/*
* Locate a DHCPv6 option within the top level of a PKT_LIST entry. DHCPv6
* uses nested options within options, and this function returns only the
* primary options. Use dhcpv6_find_option to traverse suboptions.
*
* See dhcpv6_find_option for usage details and warnings.
*/
dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
uint16_t codenum, uint_t *retlenp)
{
const dhcpv6_message_t *d6m;
if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
return (NULL);
d6m = (const dhcpv6_message_t *)plp->pkt;
return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
codenum, retlenp));
}
|