xref: /illumos-gate/usr/src/common/net/dhcp/scan.c (revision c40a6cd7)
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 #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include "dhcp_impl.h"
317c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #else
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static uint8_t	bootmagic[] = BOOTMAGIC;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Scan field for options.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate static void
field_scan(uint8_t * start,uint8_t * end,DHCP_OPT ** options,uint8_t last_option)437c478bd9Sstevel@tonic-gate field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
447c478bd9Sstevel@tonic-gate     uint8_t last_option)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	uint8_t		*current;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	while (start < end) {
497c478bd9Sstevel@tonic-gate 		if (*start == CD_PAD) {
507c478bd9Sstevel@tonic-gate 			start++;
517c478bd9Sstevel@tonic-gate 			continue;
527c478bd9Sstevel@tonic-gate 		}
537c478bd9Sstevel@tonic-gate 		if (*start == CD_END)
547c478bd9Sstevel@tonic-gate 			break;		/* done */
557c478bd9Sstevel@tonic-gate 		if (*start > last_option) {
567c478bd9Sstevel@tonic-gate 			if (++start < end)
577c478bd9Sstevel@tonic-gate 				start += *start + 1;
587c478bd9Sstevel@tonic-gate 			continue;	/* unrecognized option */
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 		current = start;
627c478bd9Sstevel@tonic-gate 		if (++start < end)
637c478bd9Sstevel@tonic-gate 			start += *start + 1; /* advance to next option */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		/* all options besides CD_END and CD_PAD should have a len */
667c478bd9Sstevel@tonic-gate 		if ((current + 1) >= end)
677c478bd9Sstevel@tonic-gate 			continue;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		/* Ignores duplicate options. */
707c478bd9Sstevel@tonic-gate 		if (options[*current] == NULL) {
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 			options[*current] = (DHCP_OPT *)current;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 			/* verify that len won't go beyond end */
757c478bd9Sstevel@tonic-gate 			if ((current + options[*current]->len + 1) >= end) {
767c478bd9Sstevel@tonic-gate 				options[*current] = NULL;
777c478bd9Sstevel@tonic-gate 				continue;
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Scan Vendor field for options.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate static void
vendor_scan(PKT_LIST * pl)877c478bd9Sstevel@tonic-gate vendor_scan(PKT_LIST *pl)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	uint8_t	*start, *end, len;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_VENDOR_SPEC] == NULL)
927c478bd9Sstevel@tonic-gate 		return;
937c478bd9Sstevel@tonic-gate 	len = pl->opts[CD_VENDOR_SPEC]->len;
947c478bd9Sstevel@tonic-gate 	start = pl->opts[CD_VENDOR_SPEC]->value;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/* verify that len won't go beyond the end of the packet */
977c478bd9Sstevel@tonic-gate 	if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
987c478bd9Sstevel@tonic-gate 		return;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	end = start + len;
1017c478bd9Sstevel@tonic-gate 	field_scan(start, end, pl->vs, VS_OPTION_END);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Load opts table in PKT_LIST entry with PKT's options.
1067c478bd9Sstevel@tonic-gate  * Returns 0 if no fatal errors occur, otherwise...
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate int
dhcp_options_scan(PKT_LIST * pl,boolean_t scan_vendor)1097c478bd9Sstevel@tonic-gate dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	PKT 	*pkt = pl->pkt;
1127c478bd9Sstevel@tonic-gate 	uint_t	opt_size = pl->len - BASE_PKT_SIZE;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * bcmp() is used here instead of memcmp() since kernel/standalone
1167c478bd9Sstevel@tonic-gate 	 * doesn't have a memcmp().
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	if (pl->len < BASE_PKT_SIZE ||
1197c478bd9Sstevel@tonic-gate 	    bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
1207c478bd9Sstevel@tonic-gate 		pl->rfc1048 = 0;
1217c478bd9Sstevel@tonic-gate 		return (0);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	pl->rfc1048 = 1;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* check the options field */
1277c478bd9Sstevel@tonic-gate 	field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
1287c478bd9Sstevel@tonic-gate 	    DHCP_LAST_OPT);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * process vendor specific options. We look at the vendor options
1327c478bd9Sstevel@tonic-gate 	 * here, simply because a BOOTP server could fake DHCP vendor
1337c478bd9Sstevel@tonic-gate 	 * options. This increases our interoperability with BOOTP.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
1367c478bd9Sstevel@tonic-gate 		vendor_scan(pl);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_DHCP_TYPE] == NULL)
1397c478bd9Sstevel@tonic-gate 		return (0);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_DHCP_TYPE]->len != 1)
1427c478bd9Sstevel@tonic-gate 		return (DHCP_GARBLED_MSG_TYPE);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
1457c478bd9Sstevel@tonic-gate 	    *pl->opts[CD_DHCP_TYPE]->value > INFORM)
1467c478bd9Sstevel@tonic-gate 		return (DHCP_WRONG_MSG_TYPE);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (pl->opts[CD_OPTION_OVERLOAD]) {
1497c478bd9Sstevel@tonic-gate 		if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
1507c478bd9Sstevel@tonic-gate 			pl->opts[CD_OPTION_OVERLOAD] = NULL;
1517c478bd9Sstevel@tonic-gate 			return (DHCP_BAD_OPT_OVLD);
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
1547c478bd9Sstevel@tonic-gate 		case 1:
1557c478bd9Sstevel@tonic-gate 			field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1567c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case 2:
1597c478bd9Sstevel@tonic-gate 			field_scan(pkt->sname, &pkt->file[0], pl->opts,
1607c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case 3:
1637c478bd9Sstevel@tonic-gate 			field_scan(pkt->file, &pkt->cookie[0], pl->opts,
1647c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1657c478bd9Sstevel@tonic-gate 			field_scan(pkt->sname, &pkt->file[0], pl->opts,
1667c478bd9Sstevel@tonic-gate 			    DHCP_LAST_OPT);
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		default:
1697c478bd9Sstevel@tonic-gate 			pl->opts[CD_OPTION_OVERLOAD] = NULL;
1707c478bd9Sstevel@tonic-gate 			return (DHCP_BAD_OPT_OVLD);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
175*d04ccbb3Scarlsonj 
176*d04ccbb3Scarlsonj /*
177*d04ccbb3Scarlsonj  * Locate a DHCPv6 option or suboption within a buffer.  DHCPv6 uses nested
178*d04ccbb3Scarlsonj  * options within options, and this function is designed to work with both
179*d04ccbb3Scarlsonj  * primary options and the suboptions contained within.
180*d04ccbb3Scarlsonj  *
181*d04ccbb3Scarlsonj  * The 'oldopt' is a previous option pointer, and is typically used to iterate
182*d04ccbb3Scarlsonj  * over options of the same code number.  The 'codenum' is in host byte order
183*d04ccbb3Scarlsonj  * for simplicity.  'retlenp' may be NULL, and if present gets the _entire_
184*d04ccbb3Scarlsonj  * option length (including header).
185*d04ccbb3Scarlsonj  *
186*d04ccbb3Scarlsonj  * Warning: the returned pointer has no particular alignment because DHCPv6
187*d04ccbb3Scarlsonj  * defines options without alignment.  The caller must deal with unaligned
188*d04ccbb3Scarlsonj  * pointers carefully.
189*d04ccbb3Scarlsonj  */
190*d04ccbb3Scarlsonj dhcpv6_option_t *
dhcpv6_find_option(const void * buffer,size_t buflen,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)191*d04ccbb3Scarlsonj dhcpv6_find_option(const void *buffer, size_t buflen,
192*d04ccbb3Scarlsonj     const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
193*d04ccbb3Scarlsonj {
194*d04ccbb3Scarlsonj 	const uchar_t *bp;
195*d04ccbb3Scarlsonj 	dhcpv6_option_t d6o;
196*d04ccbb3Scarlsonj 	uint_t olen;
197*d04ccbb3Scarlsonj 
198*d04ccbb3Scarlsonj 	codenum = htons(codenum);
199*d04ccbb3Scarlsonj 	bp = buffer;
200*d04ccbb3Scarlsonj 	while (buflen >= sizeof (dhcpv6_option_t)) {
201*d04ccbb3Scarlsonj 		(void) memcpy(&d6o, bp, sizeof (d6o));
202*d04ccbb3Scarlsonj 		olen = ntohs(d6o.d6o_len) + sizeof (d6o);
203*d04ccbb3Scarlsonj 		if (olen > buflen)
204*d04ccbb3Scarlsonj 			break;
205*d04ccbb3Scarlsonj 		if (d6o.d6o_code != codenum ||
206*d04ccbb3Scarlsonj 		    (oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
207*d04ccbb3Scarlsonj 			bp += olen;
208*d04ccbb3Scarlsonj 			buflen -= olen;
209*d04ccbb3Scarlsonj 			continue;
210*d04ccbb3Scarlsonj 		}
211*d04ccbb3Scarlsonj 		if (retlenp != NULL)
212*d04ccbb3Scarlsonj 			*retlenp = olen;
213*d04ccbb3Scarlsonj 		/* LINTED: alignment */
214*d04ccbb3Scarlsonj 		return ((dhcpv6_option_t *)bp);
215*d04ccbb3Scarlsonj 	}
216*d04ccbb3Scarlsonj 	return (NULL);
217*d04ccbb3Scarlsonj }
218*d04ccbb3Scarlsonj 
219*d04ccbb3Scarlsonj /*
220*d04ccbb3Scarlsonj  * Locate a DHCPv6 option within the top level of a PKT_LIST entry.  DHCPv6
221*d04ccbb3Scarlsonj  * uses nested options within options, and this function returns only the
222*d04ccbb3Scarlsonj  * primary options.  Use dhcpv6_find_option to traverse suboptions.
223*d04ccbb3Scarlsonj  *
224*d04ccbb3Scarlsonj  * See dhcpv6_find_option for usage details and warnings.
225*d04ccbb3Scarlsonj  */
226*d04ccbb3Scarlsonj dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST * plp,const dhcpv6_option_t * oldopt,uint16_t codenum,uint_t * retlenp)227*d04ccbb3Scarlsonj dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
228*d04ccbb3Scarlsonj     uint16_t codenum, uint_t *retlenp)
229*d04ccbb3Scarlsonj {
230*d04ccbb3Scarlsonj 	const dhcpv6_message_t *d6m;
231*d04ccbb3Scarlsonj 
232*d04ccbb3Scarlsonj 	if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
233*d04ccbb3Scarlsonj 		return (NULL);
234*d04ccbb3Scarlsonj 	d6m = (const dhcpv6_message_t *)plp->pkt;
235*d04ccbb3Scarlsonj 	return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
236*d04ccbb3Scarlsonj 	    codenum, retlenp));
237*d04ccbb3Scarlsonj }
238