1/*	$NetBSD: bootp.c,v 1.37 2009/07/10 12:16:31 roy Exp $	*/
2
3/*
4 * Copyright (c) 1992 Regents of the University of California.
5 * All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Lawrence Berkeley Laboratory and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp  (LBL)
40 */
41
42#include <sys/param.h>
43#include <netinet/in.h>
44#include <netinet/in_systm.h>
45
46#ifdef _STANDALONE
47#include <lib/libkern/libkern.h>
48#else
49#include <string.h>
50#endif
51
52#include "stand.h"
53#include "net.h"
54#include "bootp.h"
55
56struct in_addr servip;
57#ifdef SUPPORT_LINUX
58char linuxcmdline[256];
59#ifndef TAG_LINUX_CMDLINE
60#define TAG_LINUX_CMDLINE 123
61#endif
62#endif
63
64static n_long	nmask, smask;
65
66static satime_t	bot;
67
68static	char vm_rfc1048[4] = VM_RFC1048;
69#ifdef BOOTP_VEND_CMU
70static	char vm_cmu[4] = VM_CMU;
71#endif
72
73/* Local forwards */
74static	ssize_t bootpsend(struct iodesc *, void *, size_t);
75static	ssize_t bootprecv(struct iodesc *, void *, size_t, saseconds_t);
76static	int vend_rfc1048(u_char *, u_int);
77#ifdef BOOTP_VEND_CMU
78static	void vend_cmu(u_char *);
79#endif
80
81#ifdef SUPPORT_DHCP
82static char expected_dhcpmsgtype = -1, dhcp_ok;
83struct in_addr dhcp_serverip;
84#endif
85
86/*
87 * Boot programs can patch this at run-time to change the behavior
88 * of bootp/dhcp.
89 */
90int bootp_flags;
91
92static void
93bootp_addvend(u_char *area)
94{
95#ifdef SUPPORT_DHCP
96	char vci[64];
97	int vcilen;
98
99	*area++ = TAG_PARAM_REQ;
100	*area++ = 6;
101	*area++ = TAG_SUBNET_MASK;
102	*area++ = TAG_GATEWAY;
103	*area++ = TAG_HOSTNAME;
104	*area++ = TAG_DOMAINNAME;
105	*area++ = TAG_ROOTPATH;
106	*area++ = TAG_SWAPSERVER;
107
108	/* Insert a NetBSD Vendor Class Identifier option. */
109	sprintf(vci, "NetBSD:%s:libsa", MACHINE);
110	vcilen = strlen(vci);
111	*area++ = TAG_CLASSID;
112	*area++ = vcilen;
113	(void)memcpy(area, vci, vcilen);
114	area += vcilen;
115#endif
116	*area = TAG_END;
117}
118
119/* Fetch required bootp information */
120void
121bootp(int sock)
122{
123	struct iodesc *d;
124	struct bootp *bp;
125	struct {
126		u_char header[UDP_TOTAL_HEADER_SIZE];
127		struct bootp wbootp;
128	} wbuf;
129	struct {
130		u_char header[UDP_TOTAL_HEADER_SIZE];
131		struct bootp rbootp;
132	} rbuf;
133	unsigned int index;
134
135#ifdef BOOTP_DEBUG
136 	if (debug)
137		printf("bootp: socket=%d\n", sock);
138#endif
139	if (!bot)
140		bot = getsecs();
141
142	if (!(d = socktodesc(sock))) {
143		printf("bootp: bad socket. %d\n", sock);
144		return;
145	}
146#ifdef BOOTP_DEBUG
147 	if (debug)
148		printf("bootp: d=%lx\n", (long)d);
149#endif
150
151	bp = &wbuf.wbootp;
152	(void)memset(bp, 0, sizeof(*bp));
153
154	bp->bp_op = BOOTREQUEST;
155	bp->bp_htype = 1;		/* 10Mb Ethernet (48 bits) */
156	bp->bp_hlen = 6;
157	bp->bp_xid = htonl(d->xid);
158	MACPY(d->myea, bp->bp_chaddr);
159	(void)strncpy((char *)bp->bp_file, bootfile, sizeof(bp->bp_file));
160	(void)memcpy(bp->bp_vend, vm_rfc1048, sizeof(vm_rfc1048));
161	index = 4;
162#ifdef SUPPORT_DHCP
163	bp->bp_vend[index++] = TAG_DHCP_MSGTYPE;
164	bp->bp_vend[index++] = 1;
165	bp->bp_vend[index++] = DHCPDISCOVER;
166#endif
167	bootp_addvend(&bp->bp_vend[index]);
168
169	d->myip.s_addr = INADDR_ANY;
170	d->myport = htons(IPPORT_BOOTPC);
171	d->destip.s_addr = INADDR_BROADCAST;
172	d->destport = htons(IPPORT_BOOTPS);
173
174#ifdef SUPPORT_DHCP
175	expected_dhcpmsgtype = DHCPOFFER;
176	dhcp_ok = 0;
177#endif
178
179	if (sendrecv(d,
180		    bootpsend, bp, sizeof(*bp),
181		    bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
182	   == -1) {
183		printf("bootp: no reply\n");
184		return;
185	}
186
187#ifdef SUPPORT_DHCP
188	if (dhcp_ok) {
189		u_int32_t leasetime;
190		index = 6;
191		bp->bp_vend[index++] = DHCPREQUEST;
192		bp->bp_vend[index++] = TAG_REQ_ADDR;
193		bp->bp_vend[index++] = 4;
194		(void)memcpy(&bp->bp_vend[9], &rbuf.rbootp.bp_yiaddr, 4);
195		index += 4;
196		bp->bp_vend[index++] = TAG_SERVERID;
197		bp->bp_vend[index++] = 4;
198		(void)memcpy(&bp->bp_vend[index], &dhcp_serverip.s_addr, 4);
199		index += 4;
200		bp->bp_vend[index++] = TAG_LEASETIME;
201		bp->bp_vend[index++] = 4;
202		leasetime = htonl(300);
203		(void)memcpy(&bp->bp_vend[index], &leasetime, 4);
204		index += 4;
205		bootp_addvend(&bp->bp_vend[index]);
206
207		expected_dhcpmsgtype = DHCPACK;
208
209		if (sendrecv(d,
210			    bootpsend, bp, sizeof(*bp),
211			    bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
212		   == -1) {
213			printf("DHCPREQUEST failed\n");
214			return;
215		}
216	}
217#endif
218
219	myip = d->myip = rbuf.rbootp.bp_yiaddr;
220	servip = rbuf.rbootp.bp_siaddr;
221	if (rootip.s_addr == INADDR_ANY)
222		rootip = servip;
223	(void)memcpy(bootfile, rbuf.rbootp.bp_file, sizeof(bootfile));
224	bootfile[sizeof(bootfile) - 1] = '\0';
225
226	if (IN_CLASSA(myip.s_addr))
227		nmask = IN_CLASSA_NET;
228	else if (IN_CLASSB(myip.s_addr))
229		nmask = IN_CLASSB_NET;
230	else
231		nmask = IN_CLASSC_NET;
232#ifdef BOOTP_DEBUG
233	if (debug)
234		printf("'native netmask' is %s\n", intoa(nmask));
235#endif
236
237	/* Get subnet (or natural net) mask */
238	netmask = nmask;
239	if (smask)
240		netmask = smask;
241#ifdef BOOTP_DEBUG
242	if (debug)
243		printf("mask: %s\n", intoa(netmask));
244#endif
245
246	/* We need a gateway if root is on a different net */
247	if (!SAMENET(myip, rootip, netmask)) {
248#ifdef BOOTP_DEBUG
249		if (debug)
250			printf("need gateway for root ip\n");
251#endif
252	}
253
254	/* Toss gateway if on a different net */
255	if (!SAMENET(myip, gateip, netmask)) {
256#ifdef BOOTP_DEBUG
257		if (debug)
258			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
259#endif
260		gateip.s_addr = 0;
261	}
262
263#ifdef BOOTP_DEBUG
264	if (debug) {
265		printf("client addr: %s\n", inet_ntoa(myip));
266		if (smask)
267			printf("subnet mask: %s\n", intoa(smask));
268		if (gateip.s_addr != 0)
269			printf("net gateway: %s\n", inet_ntoa(gateip));
270		printf("server addr: %s\n", inet_ntoa(rootip));
271		if (rootpath[0] != '\0')
272			printf("server path: %s\n", rootpath);
273		if (bootfile[0] != '\0')
274			printf("file name: %s\n", bootfile);
275	}
276#endif
277
278	/* Bump xid so next request will be unique. */
279	++d->xid;
280}
281
282/* Transmit a bootp request */
283static ssize_t
284bootpsend(struct iodesc *d, void *pkt, size_t len)
285{
286	struct bootp *bp;
287
288#ifdef BOOTP_DEBUG
289	if (debug)
290		printf("bootpsend: d=%lx called.\n", (long)d);
291#endif
292
293	bp = pkt;
294	bp->bp_secs = htons((u_short)(getsecs() - bot));
295
296#ifdef BOOTP_DEBUG
297	if (debug)
298		printf("bootpsend: calling sendudp\n");
299#endif
300
301	return sendudp(d, pkt, len);
302}
303
304static ssize_t
305bootprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
306{
307	ssize_t n;
308	struct bootp *bp;
309
310#ifdef BOOTP_DEBUGx
311	if (debug)
312		printf("bootp_recvoffer: called\n");
313#endif
314
315	n = readudp(d, pkt, len, tleft);
316	if (n == -1 || (size_t)n < sizeof(struct bootp) - BOOTP_VENDSIZE)
317		goto bad;
318
319	bp = (struct bootp *)pkt;
320
321#ifdef BOOTP_DEBUG
322	if (debug)
323		printf("bootprecv: checked.  bp = 0x%lx, n = %d\n",
324		    (long)bp, (int)n);
325#endif
326	if (bp->bp_xid != htonl(d->xid)) {
327#ifdef BOOTP_DEBUG
328		if (debug) {
329			printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
330			    d->xid, ntohl(bp->bp_xid));
331		}
332#endif
333		goto bad;
334	}
335
336	/* protect against bogus addresses sent by DHCP servers */
337	if (bp->bp_yiaddr.s_addr == INADDR_ANY ||
338	    bp->bp_yiaddr.s_addr == INADDR_BROADCAST)
339		goto bad;
340
341#ifdef BOOTP_DEBUG
342	if (debug)
343		printf("bootprecv: got one!\n");
344#endif
345
346	/* Suck out vendor info */
347	if (memcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
348		if (vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
349			goto bad;
350	}
351#ifdef BOOTP_VEND_CMU
352	else if (memcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
353		vend_cmu(bp->bp_vend);
354#endif
355	else
356		printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
357
358	return n;
359bad:
360	errno = 0;
361	return -1;
362}
363
364static int
365vend_rfc1048(u_char *cp, u_int len)
366{
367	u_char *ep;
368	int size;
369	u_char tag;
370
371#ifdef BOOTP_DEBUG
372	if (debug)
373		printf("vend_rfc1048 bootp info. len=%d\n", len);
374#endif
375	ep = cp + len;
376
377	/* Step over magic cookie */
378	cp += sizeof(int);
379
380	while (cp < ep) {
381		tag = *cp++;
382		size = *cp++;
383		if (tag == TAG_END)
384			break;
385
386		if (tag == TAG_SUBNET_MASK && size >= sizeof(smask)) {
387			(void)memcpy(&smask, cp, sizeof(smask));
388		}
389		if (tag == TAG_GATEWAY && size >= sizeof(gateip.s_addr)) {
390			(void)memcpy(&gateip.s_addr, cp, sizeof(gateip.s_addr));
391		}
392		if (tag == TAG_SWAPSERVER && size >= sizeof(rootip.s_addr)) {
393			/* let it override bp_siaddr */
394			(void)memcpy(&rootip.s_addr, cp, sizeof(rootip.s_addr));
395		}
396	        if (tag == TAG_ROOTPATH && size < sizeof(rootpath)) {
397			strncpy(rootpath, (char *)cp, sizeof(rootpath));
398			rootpath[size] = '\0';
399		}
400		if (tag == TAG_HOSTNAME && size < sizeof(hostname)) {
401			strncpy(hostname, (char *)cp, sizeof(hostname));
402			hostname[size] = '\0';
403		}
404#ifdef SUPPORT_DHCP
405		if (tag == TAG_DHCP_MSGTYPE) {
406			if (*cp != expected_dhcpmsgtype)
407				return -1;
408			dhcp_ok = 1;
409		}
410		if (tag == TAG_SERVERID &&
411		    size >= sizeof(dhcp_serverip.s_addr))
412		{
413			(void)memcpy(&dhcp_serverip.s_addr, cp,
414			      sizeof(dhcp_serverip.s_addr));
415		}
416#endif
417#ifdef SUPPORT_LINUX
418		if (tag == TAG_LINUX_CMDLINE && size < sizeof(linuxcmdline)) {
419			strncpy(linuxcmdline, (char *)cp, sizeof(linuxcmdline));
420			linuxcmdline[size] = '\0';
421		}
422#endif
423		cp += size;
424	}
425	return 0;
426}
427
428#ifdef BOOTP_VEND_CMU
429static void
430vend_cmu(u_char *cp)
431{
432	struct cmu_vend *vp;
433
434#ifdef BOOTP_DEBUG
435	if (debug)
436		printf("vend_cmu bootp info.\n");
437#endif
438	vp = (struct cmu_vend *)cp;
439
440	if (vp->v_smask.s_addr != 0) {
441		smask = vp->v_smask.s_addr;
442	}
443	if (vp->v_dgate.s_addr != 0) {
444		gateip = vp->v_dgate;
445	}
446}
447#endif
448