10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3431Scarlsonj * Common Development and Distribution License (the "License").
6*3431Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Routines used to extract/insert DHCP options. Must be kept MT SAFE,
260Sstevel@tonic-gate * as they are called from different threads.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include "dhcp_impl.h"
330Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #else
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #endif /* _KERNEL && !_BOOT */
380Sstevel@tonic-gate
390Sstevel@tonic-gate static uint8_t bootmagic[] = BOOTMAGIC;
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * Scan field for options.
430Sstevel@tonic-gate */
440Sstevel@tonic-gate static void
field_scan(uint8_t * start,uint8_t * end,DHCP_OPT ** options,uint8_t last_option)450Sstevel@tonic-gate field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
460Sstevel@tonic-gate uint8_t last_option)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate uint8_t *current;
490Sstevel@tonic-gate
500Sstevel@tonic-gate while (start < end) {
510Sstevel@tonic-gate if (*start == CD_PAD) {
520Sstevel@tonic-gate start++;
530Sstevel@tonic-gate continue;
540Sstevel@tonic-gate }
550Sstevel@tonic-gate if (*start == CD_END)
560Sstevel@tonic-gate break; /* done */
570Sstevel@tonic-gate if (*start > last_option) {
580Sstevel@tonic-gate if (++start < end)
590Sstevel@tonic-gate start += *start + 1;
600Sstevel@tonic-gate continue; /* unrecognized option */
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate current = start;
640Sstevel@tonic-gate if (++start < end)
650Sstevel@tonic-gate start += *start + 1; /* advance to next option */
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* all options besides CD_END and CD_PAD should have a len */
680Sstevel@tonic-gate if ((current + 1) >= end)
690Sstevel@tonic-gate continue;
700Sstevel@tonic-gate
710Sstevel@tonic-gate /* Ignores duplicate options. */
720Sstevel@tonic-gate if (options[*current] == NULL) {
730Sstevel@tonic-gate
740Sstevel@tonic-gate options[*current] = (DHCP_OPT *)current;
750Sstevel@tonic-gate
760Sstevel@tonic-gate /* verify that len won't go beyond end */
770Sstevel@tonic-gate if ((current + options[*current]->len + 1) >= end) {
780Sstevel@tonic-gate options[*current] = NULL;
790Sstevel@tonic-gate continue;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate }
820Sstevel@tonic-gate }
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Scan Vendor field for options.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate static void
vendor_scan(PKT_LIST * pl)890Sstevel@tonic-gate vendor_scan(PKT_LIST *pl)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate uint8_t *start, *end, len;
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (pl->opts[CD_VENDOR_SPEC] == NULL)
940Sstevel@tonic-gate return;
950Sstevel@tonic-gate len = pl->opts[CD_VENDOR_SPEC]->len;
960Sstevel@tonic-gate start = pl->opts[CD_VENDOR_SPEC]->value;
970Sstevel@tonic-gate
980Sstevel@tonic-gate /* verify that len won't go beyond the end of the packet */
990Sstevel@tonic-gate if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
1000Sstevel@tonic-gate return;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate end = start + len;
1030Sstevel@tonic-gate field_scan(start, end, pl->vs, VS_OPTION_END);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate * Load opts table in PKT_LIST entry with PKT's options.
1080Sstevel@tonic-gate * Returns 0 if no fatal errors occur, otherwise...
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate int
dhcp_options_scan(PKT_LIST * pl,boolean_t scan_vendor)1110Sstevel@tonic-gate dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate PKT *pkt = pl->pkt;
1140Sstevel@tonic-gate uint_t opt_size = pl->len - BASE_PKT_SIZE;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * bcmp() is used here instead of memcmp() since kernel/standalone
1180Sstevel@tonic-gate * doesn't have a memcmp().
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate if (pl->len < BASE_PKT_SIZE ||
1210Sstevel@tonic-gate bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
1220Sstevel@tonic-gate pl->rfc1048 = 0;
1230Sstevel@tonic-gate return (0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate pl->rfc1048 = 1;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /* check the options field */
1290Sstevel@tonic-gate field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
1300Sstevel@tonic-gate DHCP_LAST_OPT);
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * process vendor specific options. We look at the vendor options
1340Sstevel@tonic-gate * here, simply because a BOOTP server could fake DHCP vendor
1350Sstevel@tonic-gate * options. This increases our interoperability with BOOTP.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
1380Sstevel@tonic-gate vendor_scan(pl);
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if (pl->opts[CD_DHCP_TYPE] == NULL)
1410Sstevel@tonic-gate return (0);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if (pl->opts[CD_DHCP_TYPE]->len != 1)
1440Sstevel@tonic-gate return (DHCP_GARBLED_MSG_TYPE);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
1470Sstevel@tonic-gate *pl->opts[CD_DHCP_TYPE]->value > INFORM)
1480Sstevel@tonic-gate return (DHCP_WRONG_MSG_TYPE);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (pl->opts[CD_OPTION_OVERLOAD]) {
1510Sstevel@tonic-gate if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
1520Sstevel@tonic-gate pl->opts[CD_OPTION_OVERLOAD] = NULL;
1530Sstevel@tonic-gate return (DHCP_BAD_OPT_OVLD);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
1560Sstevel@tonic-gate case 1:
1570Sstevel@tonic-gate field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1580Sstevel@tonic-gate DHCP_LAST_OPT);
1590Sstevel@tonic-gate break;
1600Sstevel@tonic-gate case 2:
1610Sstevel@tonic-gate field_scan(pkt->sname, &pkt->file[0], pl->opts,
1620Sstevel@tonic-gate DHCP_LAST_OPT);
1630Sstevel@tonic-gate break;
1640Sstevel@tonic-gate case 3:
1650Sstevel@tonic-gate field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1660Sstevel@tonic-gate DHCP_LAST_OPT);
1670Sstevel@tonic-gate field_scan(pkt->sname, &pkt->file[0], pl->opts,
1680Sstevel@tonic-gate DHCP_LAST_OPT);
1690Sstevel@tonic-gate break;
1700Sstevel@tonic-gate default:
1710Sstevel@tonic-gate pl->opts[CD_OPTION_OVERLOAD] = NULL;
1720Sstevel@tonic-gate return (DHCP_BAD_OPT_OVLD);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate return (0);
1760Sstevel@tonic-gate }
177*3431Scarlsonj
178*3431Scarlsonj /*
179*3431Scarlsonj * Locate a DHCPv6 option or suboption within a buffer. DHCPv6 uses nested
180*3431Scarlsonj * options within options, and this function is designed to work with both
181*3431Scarlsonj * primary options and the suboptions contained within.
182*3431Scarlsonj *
183*3431Scarlsonj * The 'oldopt' is a previous option pointer, and is typically used to iterate
184*3431Scarlsonj * over options of the same code number. The 'codenum' is in host byte order
185*3431Scarlsonj * for simplicity. 'retlenp' may be NULL, and if present gets the _entire_
186*3431Scarlsonj * option length (including header).
187*3431Scarlsonj *
188*3431Scarlsonj * Warning: the returned pointer has no particular alignment because DHCPv6
189*3431Scarlsonj * defines options without alignment. The caller must deal with unaligned
190*3431Scarlsonj * pointers carefully.
191*3431Scarlsonj */
192*3431Scarlsonj dhcpv6_option_t *
dhcpv6_find_option(const void * buffer,size_t buflen,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)193*3431Scarlsonj dhcpv6_find_option(const void *buffer, size_t buflen,
194*3431Scarlsonj const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
195*3431Scarlsonj {
196*3431Scarlsonj const uchar_t *bp;
197*3431Scarlsonj dhcpv6_option_t d6o;
198*3431Scarlsonj uint_t olen;
199*3431Scarlsonj
200*3431Scarlsonj codenum = htons(codenum);
201*3431Scarlsonj bp = buffer;
202*3431Scarlsonj while (buflen >= sizeof (dhcpv6_option_t)) {
203*3431Scarlsonj (void) memcpy(&d6o, bp, sizeof (d6o));
204*3431Scarlsonj olen = ntohs(d6o.d6o_len) + sizeof (d6o);
205*3431Scarlsonj if (olen > buflen)
206*3431Scarlsonj break;
207*3431Scarlsonj if (d6o.d6o_code != codenum ||
208*3431Scarlsonj (oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
209*3431Scarlsonj bp += olen;
210*3431Scarlsonj buflen -= olen;
211*3431Scarlsonj continue;
212*3431Scarlsonj }
213*3431Scarlsonj if (retlenp != NULL)
214*3431Scarlsonj *retlenp = olen;
215*3431Scarlsonj /* LINTED: alignment */
216*3431Scarlsonj return ((dhcpv6_option_t *)bp);
217*3431Scarlsonj }
218*3431Scarlsonj return (NULL);
219*3431Scarlsonj }
220*3431Scarlsonj
221*3431Scarlsonj /*
222*3431Scarlsonj * Locate a DHCPv6 option within the top level of a PKT_LIST entry. DHCPv6
223*3431Scarlsonj * uses nested options within options, and this function returns only the
224*3431Scarlsonj * primary options. Use dhcpv6_find_option to traverse suboptions.
225*3431Scarlsonj *
226*3431Scarlsonj * See dhcpv6_find_option for usage details and warnings.
227*3431Scarlsonj */
228*3431Scarlsonj dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST * plp,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)229*3431Scarlsonj dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
230*3431Scarlsonj uint16_t codenum, uint_t *retlenp)
231*3431Scarlsonj {
232*3431Scarlsonj const dhcpv6_message_t *d6m;
233*3431Scarlsonj
234*3431Scarlsonj if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
235*3431Scarlsonj return (NULL);
236*3431Scarlsonj d6m = (const dhcpv6_message_t *)plp->pkt;
237*3431Scarlsonj return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
238*3431Scarlsonj codenum, retlenp));
239*3431Scarlsonj }
240