xref: /titanic_50/usr/src/common/net/dhcp/scan.c (revision d04ccbb3f3163ae5962a8b7465d9796bff6ca434)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d04ccbb3Scarlsonj  * Common Development and Distribution License (the "License").
6*d04ccbb3Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Routines used to extract/insert DHCP options. Must be kept MT SAFE,
267c478bd9Sstevel@tonic-gate  * as they are called from different threads.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include "dhcp_impl.h"
337c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #else
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static uint8_t	bootmagic[] = BOOTMAGIC;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Scan field for options.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate static void
field_scan(uint8_t * start,uint8_t * end,DHCP_OPT ** options,uint8_t last_option)457c478bd9Sstevel@tonic-gate field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
467c478bd9Sstevel@tonic-gate     uint8_t last_option)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	uint8_t		*current;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	while (start < end) {
517c478bd9Sstevel@tonic-gate 		if (*start == CD_PAD) {
527c478bd9Sstevel@tonic-gate 			start++;
537c478bd9Sstevel@tonic-gate 			continue;
547c478bd9Sstevel@tonic-gate 		}
557c478bd9Sstevel@tonic-gate 		if (*start == CD_END)
567c478bd9Sstevel@tonic-gate 			break;		/* done */
577c478bd9Sstevel@tonic-gate 		if (*start > last_option) {
587c478bd9Sstevel@tonic-gate 			if (++start < end)
597c478bd9Sstevel@tonic-gate 				start += *start + 1;
607c478bd9Sstevel@tonic-gate 			continue;	/* unrecognized option */
617c478bd9Sstevel@tonic-gate 		}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 		current = start;
647c478bd9Sstevel@tonic-gate 		if (++start < end)
657c478bd9Sstevel@tonic-gate 			start += *start + 1; /* advance to next option */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		/* all options besides CD_END and CD_PAD should have a len */
687c478bd9Sstevel@tonic-gate 		if ((current + 1) >= end)
697c478bd9Sstevel@tonic-gate 			continue;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		/* Ignores duplicate options. */
727c478bd9Sstevel@tonic-gate 		if (options[*current] == NULL) {
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 			options[*current] = (DHCP_OPT *)current;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 			/* verify that len won't go beyond end */
777c478bd9Sstevel@tonic-gate 			if ((current + options[*current]->len + 1) >= end) {
787c478bd9Sstevel@tonic-gate 				options[*current] = NULL;
797c478bd9Sstevel@tonic-gate 				continue;
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Scan Vendor field for options.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate static void
vendor_scan(PKT_LIST * pl)897c478bd9Sstevel@tonic-gate vendor_scan(PKT_LIST *pl)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	uint8_t	*start, *end, len;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_VENDOR_SPEC] == NULL)
947c478bd9Sstevel@tonic-gate 		return;
957c478bd9Sstevel@tonic-gate 	len = pl->opts[CD_VENDOR_SPEC]->len;
967c478bd9Sstevel@tonic-gate 	start = pl->opts[CD_VENDOR_SPEC]->value;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* verify that len won't go beyond the end of the packet */
997c478bd9Sstevel@tonic-gate 	if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
1007c478bd9Sstevel@tonic-gate 		return;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	end = start + len;
1037c478bd9Sstevel@tonic-gate 	field_scan(start, end, pl->vs, VS_OPTION_END);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Load opts table in PKT_LIST entry with PKT's options.
1087c478bd9Sstevel@tonic-gate  * Returns 0 if no fatal errors occur, otherwise...
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate int
dhcp_options_scan(PKT_LIST * pl,boolean_t scan_vendor)1117c478bd9Sstevel@tonic-gate dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	PKT 	*pkt = pl->pkt;
1147c478bd9Sstevel@tonic-gate 	uint_t	opt_size = pl->len - BASE_PKT_SIZE;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * bcmp() is used here instead of memcmp() since kernel/standalone
1187c478bd9Sstevel@tonic-gate 	 * doesn't have a memcmp().
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	if (pl->len < BASE_PKT_SIZE ||
1217c478bd9Sstevel@tonic-gate 	    bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
1227c478bd9Sstevel@tonic-gate 		pl->rfc1048 = 0;
1237c478bd9Sstevel@tonic-gate 		return (0);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	pl->rfc1048 = 1;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* check the options field */
1297c478bd9Sstevel@tonic-gate 	field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
1307c478bd9Sstevel@tonic-gate 	    DHCP_LAST_OPT);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * process vendor specific options. We look at the vendor options
1347c478bd9Sstevel@tonic-gate 	 * here, simply because a BOOTP server could fake DHCP vendor
1357c478bd9Sstevel@tonic-gate 	 * options. This increases our interoperability with BOOTP.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
1387c478bd9Sstevel@tonic-gate 		vendor_scan(pl);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_DHCP_TYPE] == NULL)
1417c478bd9Sstevel@tonic-gate 		return (0);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_DHCP_TYPE]->len != 1)
1447c478bd9Sstevel@tonic-gate 		return (DHCP_GARBLED_MSG_TYPE);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
1477c478bd9Sstevel@tonic-gate 	    *pl->opts[CD_DHCP_TYPE]->value > INFORM)
1487c478bd9Sstevel@tonic-gate 		return (DHCP_WRONG_MSG_TYPE);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_OPTION_OVERLOAD]) {
1517c478bd9Sstevel@tonic-gate 		if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
1527c478bd9Sstevel@tonic-gate 			pl->opts[CD_OPTION_OVERLOAD] = NULL;
1537c478bd9Sstevel@tonic-gate 			return (DHCP_BAD_OPT_OVLD);
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
1567c478bd9Sstevel@tonic-gate 		case 1:
1577c478bd9Sstevel@tonic-gate 			field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1587c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 2:
1617c478bd9Sstevel@tonic-gate 			field_scan(pkt->sname, &pkt->file[0], pl->opts,
1627c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		case 3:
1657c478bd9Sstevel@tonic-gate 			field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1667c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1677c478bd9Sstevel@tonic-gate 			field_scan(pkt->sname, &pkt->file[0], pl->opts,
1687c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		default:
1717c478bd9Sstevel@tonic-gate 			pl->opts[CD_OPTION_OVERLOAD] = NULL;
1727c478bd9Sstevel@tonic-gate 			return (DHCP_BAD_OPT_OVLD);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	return (0);
1767c478bd9Sstevel@tonic-gate }
177*d04ccbb3Scarlsonj 
178*d04ccbb3Scarlsonj /*
179*d04ccbb3Scarlsonj  * Locate a DHCPv6 option or suboption within a buffer.  DHCPv6 uses nested
180*d04ccbb3Scarlsonj  * options within options, and this function is designed to work with both
181*d04ccbb3Scarlsonj  * primary options and the suboptions contained within.
182*d04ccbb3Scarlsonj  *
183*d04ccbb3Scarlsonj  * The 'oldopt' is a previous option pointer, and is typically used to iterate
184*d04ccbb3Scarlsonj  * over options of the same code number.  The 'codenum' is in host byte order
185*d04ccbb3Scarlsonj  * for simplicity.  'retlenp' may be NULL, and if present gets the _entire_
186*d04ccbb3Scarlsonj  * option length (including header).
187*d04ccbb3Scarlsonj  *
188*d04ccbb3Scarlsonj  * Warning: the returned pointer has no particular alignment because DHCPv6
189*d04ccbb3Scarlsonj  * defines options without alignment.  The caller must deal with unaligned
190*d04ccbb3Scarlsonj  * pointers carefully.
191*d04ccbb3Scarlsonj  */
192*d04ccbb3Scarlsonj dhcpv6_option_t *
dhcpv6_find_option(const void * buffer,size_t buflen,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)193*d04ccbb3Scarlsonj dhcpv6_find_option(const void *buffer, size_t buflen,
194*d04ccbb3Scarlsonj     const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
195*d04ccbb3Scarlsonj {
196*d04ccbb3Scarlsonj 	const uchar_t *bp;
197*d04ccbb3Scarlsonj 	dhcpv6_option_t d6o;
198*d04ccbb3Scarlsonj 	uint_t olen;
199*d04ccbb3Scarlsonj 
200*d04ccbb3Scarlsonj 	codenum = htons(codenum);
201*d04ccbb3Scarlsonj 	bp = buffer;
202*d04ccbb3Scarlsonj 	while (buflen >= sizeof (dhcpv6_option_t)) {
203*d04ccbb3Scarlsonj 		(void) memcpy(&d6o, bp, sizeof (d6o));
204*d04ccbb3Scarlsonj 		olen = ntohs(d6o.d6o_len) + sizeof (d6o);
205*d04ccbb3Scarlsonj 		if (olen > buflen)
206*d04ccbb3Scarlsonj 			break;
207*d04ccbb3Scarlsonj 		if (d6o.d6o_code != codenum ||
208*d04ccbb3Scarlsonj 		    (oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
209*d04ccbb3Scarlsonj 			bp += olen;
210*d04ccbb3Scarlsonj 			buflen -= olen;
211*d04ccbb3Scarlsonj 			continue;
212*d04ccbb3Scarlsonj 		}
213*d04ccbb3Scarlsonj 		if (retlenp != NULL)
214*d04ccbb3Scarlsonj 			*retlenp = olen;
215*d04ccbb3Scarlsonj 		/* LINTED: alignment */
216*d04ccbb3Scarlsonj 		return ((dhcpv6_option_t *)bp);
217*d04ccbb3Scarlsonj 	}
218*d04ccbb3Scarlsonj 	return (NULL);
219*d04ccbb3Scarlsonj }
220*d04ccbb3Scarlsonj 
221*d04ccbb3Scarlsonj /*
222*d04ccbb3Scarlsonj  * Locate a DHCPv6 option within the top level of a PKT_LIST entry.  DHCPv6
223*d04ccbb3Scarlsonj  * uses nested options within options, and this function returns only the
224*d04ccbb3Scarlsonj  * primary options.  Use dhcpv6_find_option to traverse suboptions.
225*d04ccbb3Scarlsonj  *
226*d04ccbb3Scarlsonj  * See dhcpv6_find_option for usage details and warnings.
227*d04ccbb3Scarlsonj  */
228*d04ccbb3Scarlsonj dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST * plp,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)229*d04ccbb3Scarlsonj dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
230*d04ccbb3Scarlsonj     uint16_t codenum, uint_t *retlenp)
231*d04ccbb3Scarlsonj {
232*d04ccbb3Scarlsonj 	const dhcpv6_message_t *d6m;
233*d04ccbb3Scarlsonj 
234*d04ccbb3Scarlsonj 	if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
235*d04ccbb3Scarlsonj 		return (NULL);
236*d04ccbb3Scarlsonj 	d6m = (const dhcpv6_message_t *)plp->pkt;
237*d04ccbb3Scarlsonj 	return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
238*d04ccbb3Scarlsonj 	    codenum, retlenp));
239*d04ccbb3Scarlsonj }
240