--- freeradius-server-2.1.6.orig/src/lib/dhcp.c	2009-05-18 13:13:55.000000000 +0200
+++ freeradius-server-2.1.6/src/lib/dhcp.c	2009-06-05 16:50:35.000000000 +0200
@@ -122,6 +122,8 @@
 	socklen_t	        sizeof_dst;
 	RADIUS_PACKET		*packet;
 	int port;
+	ssize_t total;
+	uint8_t *p;
 
 	packet = rad_alloc(0);
 	if (!packet) return NULL;
@@ -185,38 +187,37 @@
 	packet->id = ntohl(magic);
 
 	/*
-	 *	Check that it's a known packet type.
+	 *	Loop over the options to find DHCP-Message-Type, opt. 53
 	 */
-	if ((packet->data[240] != 53) ||
-	    (packet->data[241] != 1) ||
-	    (packet->data[242] == 0) ||
-	    (packet->data[242] > 8)) {
-		fprintf(stderr, "Unknown, or badly formatted DHCP packet\n");
-		rad_free(&packet);
-		return NULL;
-	}
+	p = packet->data + 240;
+	total = packet->data_len - 240;
 
-	/*
-	 *	Create a unique vector from the MAC address and the
-	 *	DHCP opcode.  This is a hack for the RADIUS
-	 *	infrastructure in the rest of the server.
-	 *
-	 *	Note: packet->data[2] == 6, which is smaller than
-	 *	sizeof(packet->vector)
-	 *
-	 *	FIXME:  Look for client-identifier in packet,
-	 *      and use that, too?
-	 */
-	memset(packet->vector, 0, sizeof(packet->vector));
-	memcpy(packet->vector, packet->data + 28, packet->data[2]);
-	packet->vector[packet->data[2]] = packet->data[242];
+	while (total > 0) {
 
-	/*
-	 *	FIXME: for DISCOVER / REQUEST: src_port == dst_port + 1
-	 *	FIXME: for OFFER / ACK       : src_port = dst_port - 1
-	 */
+		if (*p == 0) break;
+		if (*p == 255) break; /* end of options signifier */
+
+		if (*p == 53) {
+
+			packet->code = PW_DHCP_OFFSET | p[2];
+
+			if ((packet->code == 1024) ||
+			    (packet->code > 1024 + 8)) {
+				fprintf(stderr, "Unknown, or badly formatted DHCP packet\n");
+				rad_free(&packet);
+				return NULL;
+			}
+
+			memset(packet->vector, 0, sizeof(packet->vector));
+			memcpy(packet->vector, packet->data + 28, packet->data[2]);
+			packet->vector[packet->data[2]] = p[2];
+		}
 
-	packet->code = PW_DHCP_OFFSET | packet->data[242];
+		total -= 2;
+		total -= p[1];
+		p += p[1];
+		p += 2;
+	}
 
 	/*
 	 *	Unique keys are xid, client mac, and client ID?
