nfs_bootdhcp.c revision 1.5 1 1.5 scottr /* $NetBSD: nfs_bootdhcp.c,v 1.5 1998/01/12 21:27:14 scottr Exp $ */
2 1.1 gwr
3 1.1 gwr /*-
4 1.1 gwr * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gwr * by Adam Glass and Gordon W. Ross.
9 1.1 gwr *
10 1.1 gwr * Redistribution and use in source and binary forms, with or without
11 1.1 gwr * modification, are permitted provided that the following conditions
12 1.1 gwr * are met:
13 1.1 gwr * 1. Redistributions of source code must retain the above copyright
14 1.1 gwr * notice, this list of conditions and the following disclaimer.
15 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gwr * notice, this list of conditions and the following disclaimer in the
17 1.1 gwr * documentation and/or other materials provided with the distribution.
18 1.1 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 gwr * must display the following acknowledgement:
20 1.1 gwr * This product includes software developed by the NetBSD
21 1.1 gwr * Foundation, Inc. and its contributors.
22 1.1 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 gwr * contributors may be used to endorse or promote products derived
24 1.1 gwr * from this software without specific prior written permission.
25 1.1 gwr *
26 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 gwr */
38 1.1 gwr
39 1.1 gwr /*
40 1.1 gwr * Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
41 1.1 gwr *
42 1.1 gwr * History:
43 1.1 gwr *
44 1.1 gwr * Tor Egge developed the initial version of this code based on
45 1.1 gwr * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
46 1.1 gwr * submitted that work to NetBSD as bugreport "kern/2351" on
47 1.1 gwr * 29 Apr 1996.
48 1.1 gwr *
49 1.1 gwr * Gordon Ross reorganized Tor's version into this form and
50 1.1 gwr * integrated it into the NetBSD sources during Aug 1997.
51 1.1 gwr */
52 1.1 gwr
53 1.5 scottr #include "opt_nfs_boot.h"
54 1.5 scottr
55 1.1 gwr #include <sys/param.h>
56 1.1 gwr #include <sys/systm.h>
57 1.1 gwr #include <sys/kernel.h>
58 1.1 gwr #include <sys/conf.h>
59 1.1 gwr #include <sys/device.h>
60 1.1 gwr #include <sys/ioctl.h>
61 1.1 gwr #include <sys/proc.h>
62 1.1 gwr #include <sys/mount.h>
63 1.1 gwr #include <sys/mbuf.h>
64 1.1 gwr #include <sys/reboot.h>
65 1.1 gwr #include <sys/socket.h>
66 1.1 gwr #include <sys/socketvar.h>
67 1.1 gwr
68 1.1 gwr #include <net/if.h>
69 1.3 drochner #include <net/if_types.h>
70 1.1 gwr #include <net/if_arp.h> /* ARPHRD_ETHER, etc. */
71 1.1 gwr #include <net/if_dl.h>
72 1.1 gwr #include <net/if_ether.h>
73 1.1 gwr #include <net/route.h>
74 1.1 gwr
75 1.1 gwr #include <netinet/in.h>
76 1.1 gwr #include <netinet/if_inarp.h>
77 1.1 gwr
78 1.1 gwr #include <nfs/nfsproto.h>
79 1.1 gwr /* #include <nfs/nfs.h> */
80 1.1 gwr #include <nfs/nfsdiskless.h>
81 1.1 gwr
82 1.1 gwr /*
83 1.1 gwr * There are two implementations of NFS diskless boot.
84 1.1 gwr * This implementation uses BOOTP (RFC951, RFC1048), and
85 1.1 gwr * the other uses Sun RPC/bootparams (nfs_bootparam.c).
86 1.1 gwr *
87 1.1 gwr * This method gets everything it needs with one BOOTP
88 1.1 gwr * request and reply. Note that this actually uses only
89 1.1 gwr * the old BOOTP functionality subset of DHCP. It is not
90 1.1 gwr * clear that DHCP provides any advantage over BOOTP for
91 1.1 gwr * diskless boot. DHCP allows the server to assign an IP
92 1.1 gwr * address without any a-priori knowledge of the client,
93 1.1 gwr * but we require that the server has a-priori knowledge
94 1.1 gwr * of the client so it can export our (unique) NFS root.
95 1.1 gwr * Given that the server needs a-priori knowledge about
96 1.1 gwr * the client anyway, it might as well assign a fixed IP
97 1.1 gwr * address for the client and support BOOTP.
98 1.1 gwr *
99 1.1 gwr * On the other hand, disk-FULL clients may use DHCP, but
100 1.1 gwr * in that case the DHCP client should be user-mode code,
101 1.1 gwr * and has no bearing on the code below. -gwr
102 1.1 gwr */
103 1.1 gwr
104 1.1 gwr /* Begin stuff from bootp.h */
105 1.1 gwr /* Definitions from RFC951 */
106 1.1 gwr #define BP_CHADDR_LEN 16
107 1.1 gwr #define BP_SNAME_LEN 64
108 1.1 gwr #define BP_FILE_LEN 128
109 1.1 gwr #define BP_VEND_LEN 64
110 1.1 gwr struct bootp {
111 1.1 gwr u_int8_t bp_op; /* packet opcode type */
112 1.1 gwr u_int8_t bp_htype; /* hardware addr type */
113 1.1 gwr u_int8_t bp_hlen; /* hardware addr length */
114 1.1 gwr u_int8_t bp_hops; /* gateway hops */
115 1.1 gwr u_int32_t bp_xid; /* transaction ID */
116 1.1 gwr u_int16_t bp_secs; /* seconds since boot began */
117 1.1 gwr u_int16_t bp_flags; /* RFC1532 broadcast, etc. */
118 1.1 gwr struct in_addr bp_ciaddr; /* client IP address */
119 1.1 gwr struct in_addr bp_yiaddr; /* 'your' IP address */
120 1.1 gwr struct in_addr bp_siaddr; /* server IP address */
121 1.1 gwr struct in_addr bp_giaddr; /* gateway IP address */
122 1.1 gwr u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
123 1.1 gwr char bp_sname[BP_SNAME_LEN]; /* server host name */
124 1.1 gwr char bp_file[BP_FILE_LEN]; /* boot file name */
125 1.1 gwr u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */
126 1.1 gwr /*
127 1.1 gwr * Note that BOOTP packets are allowed to be longer
128 1.1 gwr * (see RFC 1532 sect. 2.1) and common practice is to
129 1.1 gwr * allow the option data in bp_vend to extend into the
130 1.1 gwr * additional space provided in longer packets.
131 1.1 gwr */
132 1.1 gwr };
133 1.1 gwr
134 1.1 gwr #define IPPORT_BOOTPS 67
135 1.1 gwr #define IPPORT_BOOTPC 68
136 1.1 gwr
137 1.1 gwr #define BOOTREQUEST 1
138 1.1 gwr #define BOOTREPLY 2
139 1.1 gwr
140 1.1 gwr /*
141 1.1 gwr * Is this available from the sockaddr_dl somehow?
142 1.1 gwr * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
143 1.1 gwr * The interface has ->if_type but not the ARP fmt.
144 1.1 gwr */
145 1.1 gwr #define HTYPE_ETHERNET 1
146 1.1 gwr
147 1.1 gwr /*
148 1.1 gwr * Vendor magic cookie (v_magic) for RFC1048
149 1.1 gwr */
150 1.1 gwr static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
151 1.1 gwr
152 1.1 gwr /*
153 1.1 gwr * Tag values used to specify what information is being supplied in
154 1.1 gwr * the vendor (options) data area of the packet.
155 1.1 gwr */
156 1.1 gwr /* RFC 1048 */
157 1.1 gwr #define TAG_END ((unsigned char) 255)
158 1.1 gwr #define TAG_PAD ((unsigned char) 0)
159 1.1 gwr #define TAG_SUBNET_MASK ((unsigned char) 1)
160 1.1 gwr #define TAG_TIME_OFFSET ((unsigned char) 2)
161 1.1 gwr #define TAG_GATEWAY ((unsigned char) 3)
162 1.1 gwr #define TAG_TIME_SERVER ((unsigned char) 4)
163 1.1 gwr #define TAG_NAME_SERVER ((unsigned char) 5)
164 1.1 gwr #define TAG_DOMAIN_SERVER ((unsigned char) 6)
165 1.1 gwr #define TAG_LOG_SERVER ((unsigned char) 7)
166 1.1 gwr #define TAG_COOKIE_SERVER ((unsigned char) 8)
167 1.1 gwr #define TAG_LPR_SERVER ((unsigned char) 9)
168 1.1 gwr #define TAG_IMPRESS_SERVER ((unsigned char) 10)
169 1.1 gwr #define TAG_RLP_SERVER ((unsigned char) 11)
170 1.1 gwr #define TAG_HOST_NAME ((unsigned char) 12)
171 1.1 gwr #define TAG_BOOT_SIZE ((unsigned char) 13)
172 1.1 gwr /* RFC 1395 */
173 1.1 gwr #define TAG_DUMP_FILE ((unsigned char) 14)
174 1.1 gwr #define TAG_DOMAIN_NAME ((unsigned char) 15)
175 1.1 gwr #define TAG_SWAP_SERVER ((unsigned char) 16)
176 1.1 gwr #define TAG_ROOT_PATH ((unsigned char) 17)
177 1.1 gwr /* End of stuff from bootp.h */
178 1.1 gwr
179 1.2 drochner #ifdef NFS_BOOT_DHCP
180 1.2 drochner #define TAG_REQ_ADDR ((unsigned char) 50)
181 1.2 drochner #define TAG_LEASETIME ((unsigned char) 51)
182 1.2 drochner #define TAG_OVERLOAD ((unsigned char) 52)
183 1.2 drochner #define TAG_DHCP_MSGTYPE ((unsigned char) 53)
184 1.2 drochner #define TAG_SERVERID ((unsigned char) 54)
185 1.2 drochner #define TAG_PARAM_REQ ((unsigned char) 55)
186 1.2 drochner #define TAG_MSG ((unsigned char) 56)
187 1.2 drochner #define TAG_MAXSIZE ((unsigned char) 57)
188 1.2 drochner #define TAG_T1 ((unsigned char) 58)
189 1.2 drochner #define TAG_T2 ((unsigned char) 59)
190 1.2 drochner #define TAG_CLASSID ((unsigned char) 60)
191 1.2 drochner #define TAG_CLIENTID ((unsigned char) 61)
192 1.2 drochner #endif
193 1.2 drochner
194 1.2 drochner #ifdef NFS_BOOT_DHCP
195 1.2 drochner #define DHCPDISCOVER 1
196 1.2 drochner #define DHCPOFFER 2
197 1.2 drochner #define DHCPREQUEST 3
198 1.2 drochner #define DHCPDECLINE 4
199 1.2 drochner #define DHCPACK 5
200 1.2 drochner #define DHCPNAK 6
201 1.2 drochner #define DHCPRELEASE 7
202 1.2 drochner #endif
203 1.1 gwr
204 1.2 drochner #ifdef NFS_BOOT_DHCP
205 1.2 drochner #define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
206 1.2 drochner #else
207 1.1 gwr /*
208 1.1 gwr * The "extended" size is somewhat arbitrary, but is
209 1.1 gwr * constrained by the maximum message size specified
210 1.1 gwr * by RFC1533 (567 total). This value increases the
211 1.1 gwr * space for options from 64 bytes to 256 bytes.
212 1.1 gwr */
213 1.2 drochner #define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
214 1.2 drochner #endif
215 1.1 gwr #define BOOTP_SIZE_MIN (sizeof(struct bootp))
216 1.1 gwr
217 1.1 gwr /* Convenience macro */
218 1.1 gwr #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
219 1.1 gwr
220 1.1 gwr static int bootpc_call __P((struct socket *, struct ifnet *,
221 1.1 gwr struct nfs_diskless *, struct proc *));
222 1.2 drochner static void bootp_extract __P((struct bootp *, int, struct nfs_diskless *));
223 1.1 gwr
224 1.1 gwr /* #define DEBUG XXX */
225 1.1 gwr
226 1.1 gwr #ifdef DEBUG
227 1.1 gwr #define DPRINT(s) printf("nfs_boot: %s\n", s)
228 1.1 gwr #else
229 1.1 gwr #define DPRINT(s) (void)0
230 1.1 gwr #endif
231 1.1 gwr
232 1.1 gwr
233 1.1 gwr /*
234 1.1 gwr * Get our boot parameters using BOOTP.
235 1.1 gwr */
236 1.1 gwr int
237 1.1 gwr nfs_bootdhcp(ifp, nd, procp)
238 1.1 gwr struct ifnet *ifp;
239 1.1 gwr struct nfs_diskless *nd;
240 1.1 gwr struct proc *procp;
241 1.1 gwr {
242 1.1 gwr struct ifaliasreq iareq;
243 1.1 gwr struct socket *so;
244 1.1 gwr struct sockaddr_in *sin;
245 1.1 gwr int error;
246 1.1 gwr
247 1.1 gwr /*
248 1.1 gwr * Get a socket to use for various things in here.
249 1.1 gwr * After this, use "goto out" to cleanup and return.
250 1.1 gwr */
251 1.1 gwr error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
252 1.1 gwr if (error) {
253 1.1 gwr printf("nfs_boot: socreate, error=%d\n", error);
254 1.1 gwr return (error);
255 1.1 gwr }
256 1.1 gwr
257 1.1 gwr /*
258 1.1 gwr * Do enough of ifconfig(8) so that the chosen interface
259 1.1 gwr * can talk to the servers. Use address zero for now.
260 1.1 gwr */
261 1.1 gwr bzero(&iareq, sizeof(iareq));
262 1.1 gwr bcopy(ifp->if_xname, iareq.ifra_name, IFNAMSIZ);
263 1.1 gwr /* Set the I/F address */
264 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_addr;
265 1.1 gwr sin->sin_len = sizeof(*sin);
266 1.1 gwr sin->sin_family = AF_INET;
267 1.1 gwr sin->sin_addr.s_addr = INADDR_ANY;
268 1.1 gwr /* Leave subnetmask unspecified (len=0) */
269 1.1 gwr /* Set the broadcast addr. */
270 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
271 1.1 gwr sin->sin_len = sizeof(*sin);
272 1.1 gwr sin->sin_family = AF_INET;
273 1.1 gwr sin->sin_addr.s_addr = INADDR_BROADCAST;
274 1.1 gwr error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
275 1.1 gwr if (error) {
276 1.1 gwr printf("nfs_boot: set ifaddr zero, error=%d\n", error);
277 1.1 gwr goto out;
278 1.1 gwr }
279 1.1 gwr
280 1.1 gwr /* This function call does the real send/recv work. */
281 1.1 gwr error = bootpc_call(so, ifp, nd, procp);
282 1.1 gwr /* Get rid of the temporary (zero) IP address. */
283 1.2 drochner /*
284 1.2 drochner * XXX SIOCDIFADDR takes a "struct ifreq", which is
285 1.2 drochner * an exact subset of "struct ifaliasreq".
286 1.2 drochner */
287 1.1 gwr (void) ifioctl(so, SIOCDIFADDR, (caddr_t)&iareq, procp);
288 1.1 gwr /* NOW we can test the error from bootpc_call. */
289 1.1 gwr if (error)
290 1.1 gwr goto out;
291 1.1 gwr
292 1.1 gwr /*
293 1.1 gwr * Do ifconfig with our real IP address and mask.
294 1.1 gwr */
295 1.1 gwr /* I/F address */
296 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_addr;
297 1.1 gwr sin->sin_addr = nd->nd_myip;
298 1.1 gwr /* subnetmask */
299 1.1 gwr if (nd->nd_mask.s_addr) {
300 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_mask;
301 1.1 gwr sin->sin_len = sizeof(*sin);
302 1.1 gwr sin->sin_family = AF_INET;
303 1.1 gwr sin->sin_addr = nd->nd_mask;
304 1.1 gwr }
305 1.1 gwr /* Let ifioctl() default the broadcast address. */
306 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
307 1.1 gwr sin->sin_len = 0;
308 1.1 gwr sin->sin_family = 0;
309 1.1 gwr sin->sin_addr.s_addr = 0;
310 1.1 gwr error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
311 1.1 gwr if (error) {
312 1.1 gwr printf("nfs_boot: set ifaddr real, error=%d\n", error);
313 1.1 gwr goto out;
314 1.1 gwr }
315 1.1 gwr
316 1.1 gwr out:
317 1.1 gwr soclose(so);
318 1.1 gwr return (error);
319 1.1 gwr }
320 1.1 gwr
321 1.2 drochner struct bootpcontext {
322 1.2 drochner int xid;
323 1.2 drochner u_char *haddr;
324 1.2 drochner u_char halen;
325 1.2 drochner struct bootp *replybuf;
326 1.2 drochner int replylen;
327 1.2 drochner #ifdef NFS_BOOT_DHCP
328 1.2 drochner char expected_dhcpmsgtype, dhcp_ok;
329 1.2 drochner struct in_addr dhcp_serverip;
330 1.2 drochner #endif
331 1.2 drochner };
332 1.2 drochner
333 1.2 drochner static int bootpset __P((struct mbuf*, void*, int));
334 1.2 drochner static int bootpcheck __P((struct mbuf*, void*));
335 1.2 drochner
336 1.2 drochner static int bootpset(m, context, waited)
337 1.2 drochner struct mbuf *m;
338 1.2 drochner void *context;
339 1.2 drochner int waited;
340 1.2 drochner {
341 1.2 drochner struct bootp *bootp;
342 1.2 drochner
343 1.2 drochner /* we know it's contigous (in 1 mbuf cluster) */
344 1.2 drochner bootp = mtod(m, struct bootp*);
345 1.2 drochner
346 1.2 drochner bootp->bp_secs = htons(waited);
347 1.2 drochner
348 1.2 drochner return(0);
349 1.2 drochner }
350 1.2 drochner
351 1.2 drochner static int bootpcheck(m, context)
352 1.2 drochner struct mbuf *m;
353 1.2 drochner void *context;
354 1.2 drochner {
355 1.2 drochner struct bootp *bootp;
356 1.2 drochner struct bootpcontext *bpc = context;
357 1.2 drochner u_int tag, len;
358 1.2 drochner u_char *p, *limit;
359 1.2 drochner
360 1.2 drochner /*
361 1.2 drochner * Is this a valid reply?
362 1.2 drochner */
363 1.2 drochner if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
364 1.2 drochner DPRINT("short packet");
365 1.2 drochner return(-1);
366 1.2 drochner }
367 1.2 drochner if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
368 1.2 drochner DPRINT("long packet");
369 1.2 drochner return(-1);
370 1.2 drochner }
371 1.2 drochner
372 1.2 drochner /*
373 1.2 drochner * don't make first checks more expensive than necessary
374 1.2 drochner */
375 1.2 drochner #define ofs(what, elem) ((int)&(((what *)0)->elem))
376 1.2 drochner if (m->m_len < ofs(struct bootp, bp_secs)) {
377 1.2 drochner m = m_pullup(m, ofs(struct bootp, bp_secs));
378 1.2 drochner if (m == NULL)
379 1.2 drochner return(-1);
380 1.2 drochner }
381 1.2 drochner #undef ofs
382 1.2 drochner bootp = mtod(m, struct bootp*);
383 1.2 drochner
384 1.2 drochner if (bootp->bp_op != BOOTREPLY) {
385 1.2 drochner DPRINT("not reply");
386 1.2 drochner return(-1);
387 1.2 drochner }
388 1.2 drochner if (bootp->bp_hlen != bpc->halen) {
389 1.2 drochner DPRINT("bad hwa_len");
390 1.2 drochner return(-1);
391 1.2 drochner }
392 1.2 drochner if (bcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
393 1.2 drochner DPRINT("wrong hwaddr");
394 1.2 drochner return(-1);
395 1.2 drochner }
396 1.2 drochner if (bootp->bp_xid != bpc->xid) {
397 1.2 drochner DPRINT("wrong xid");
398 1.2 drochner return(-1);
399 1.2 drochner }
400 1.2 drochner
401 1.2 drochner /*
402 1.2 drochner * OK, it's worth to look deeper.
403 1.2 drochner * We copy the mbuf into a flat buffer here because
404 1.2 drochner * m_pullup() is a bit limited for this purpose
405 1.2 drochner * (doesn't allocate a cluster if necessary).
406 1.2 drochner */
407 1.2 drochner bpc->replylen = m->m_pkthdr.len;
408 1.2 drochner m_copydata(m, 0, bpc->replylen, (caddr_t)bpc->replybuf);
409 1.2 drochner bootp = bpc->replybuf;
410 1.2 drochner
411 1.2 drochner /*
412 1.2 drochner * Check the vendor data.
413 1.2 drochner */
414 1.2 drochner if (bcmp(bootp->bp_vend, vm_rfc1048, 4)) {
415 1.2 drochner printf("nfs_boot: reply missing options\n");
416 1.2 drochner return(-1);
417 1.2 drochner }
418 1.2 drochner p = &bootp->bp_vend[4];
419 1.2 drochner limit = ((char*)bootp) + bpc->replylen;
420 1.2 drochner while (p < limit) {
421 1.2 drochner tag = *p++;
422 1.2 drochner if (tag == TAG_END)
423 1.2 drochner break;
424 1.2 drochner if (tag == TAG_PAD)
425 1.2 drochner continue;
426 1.2 drochner len = *p++;
427 1.2 drochner if ((p + len) > limit) {
428 1.2 drochner printf("nfs_boot: option %d too long\n", tag);
429 1.2 drochner return(-1);
430 1.2 drochner }
431 1.2 drochner switch (tag) {
432 1.2 drochner #ifdef NFS_BOOT_DHCP
433 1.2 drochner case TAG_DHCP_MSGTYPE:
434 1.2 drochner if (*p != bpc->expected_dhcpmsgtype)
435 1.2 drochner return(-1);
436 1.2 drochner bpc->dhcp_ok = 1;
437 1.2 drochner break;
438 1.2 drochner case TAG_SERVERID:
439 1.2 drochner bcopy(p, &bpc->dhcp_serverip.s_addr,
440 1.2 drochner sizeof(bpc->dhcp_serverip.s_addr));
441 1.2 drochner break;
442 1.2 drochner #endif
443 1.2 drochner default:
444 1.2 drochner break;
445 1.2 drochner }
446 1.2 drochner p += len;
447 1.2 drochner }
448 1.2 drochner return(0);
449 1.2 drochner }
450 1.2 drochner
451 1.1 gwr static int
452 1.1 gwr bootpc_call(so, ifp, nd, procp)
453 1.1 gwr struct socket *so;
454 1.1 gwr struct ifnet *ifp;
455 1.1 gwr struct nfs_diskless *nd;
456 1.1 gwr struct proc *procp;
457 1.1 gwr {
458 1.1 gwr static u_int32_t xid = ~0xFF;
459 1.2 drochner struct bootp *bootp; /* request */
460 1.1 gwr struct mbuf *m, *nam;
461 1.1 gwr struct sockaddr_in *sin;
462 1.2 drochner int error;
463 1.1 gwr u_char *haddr;
464 1.1 gwr u_char hafmt, halen;
465 1.2 drochner struct bootpcontext bpc;
466 1.1 gwr
467 1.1 gwr /*
468 1.1 gwr * Initialize to NULL anything that will hold an allocation,
469 1.1 gwr * and free each at the end if not null.
470 1.1 gwr */
471 1.2 drochner bpc.replybuf = NULL;
472 1.1 gwr m = nam = NULL;
473 1.1 gwr
474 1.1 gwr /* Record our H/W (Ethernet) address. */
475 1.1 gwr { struct sockaddr_dl *sdl = ifp->if_sadl;
476 1.3 drochner switch (sdl->sdl_type) {
477 1.3 drochner case IFT_ETHER:
478 1.3 drochner case IFT_FDDI:
479 1.3 drochner hafmt = HTYPE_ETHERNET;
480 1.3 drochner break;
481 1.3 drochner default:
482 1.3 drochner printf("bootp: unsupported interface type %d\n",
483 1.3 drochner sdl->sdl_type);
484 1.3 drochner error = EINVAL;
485 1.3 drochner goto out;
486 1.3 drochner }
487 1.1 gwr halen = sdl->sdl_alen;
488 1.1 gwr haddr = (unsigned char *)LLADDR(sdl);
489 1.1 gwr }
490 1.1 gwr
491 1.1 gwr /*
492 1.1 gwr * Skip the route table when sending on this socket.
493 1.1 gwr * If this is not done, ip_output finds the loopback
494 1.1 gwr * interface (why?) and then fails because broadcast
495 1.1 gwr * is not supported on that interface...
496 1.1 gwr */
497 1.1 gwr { int32_t *opt;
498 1.1 gwr m = m_get(M_WAIT, MT_SOOPTS);
499 1.1 gwr opt = mtod(m, int32_t *);
500 1.1 gwr m->m_len = sizeof(*opt);
501 1.1 gwr *opt = 1;
502 1.1 gwr error = sosetopt(so, SOL_SOCKET, SO_DONTROUTE, m);
503 1.1 gwr m = NULL; /* was consumed */
504 1.1 gwr }
505 1.1 gwr if (error) {
506 1.1 gwr DPRINT("SO_DONTROUTE");
507 1.1 gwr goto out;
508 1.1 gwr }
509 1.1 gwr
510 1.1 gwr /* Enable broadcast. */
511 1.2 drochner if ((error = nfs_boot_enbroadcast(so))) {
512 1.1 gwr DPRINT("SO_BROADCAST");
513 1.1 gwr goto out;
514 1.1 gwr }
515 1.1 gwr
516 1.1 gwr /* Set the receive timeout for the socket. */
517 1.2 drochner if ((error = nfs_boot_setrecvtimo(so))) {
518 1.1 gwr DPRINT("SO_RCVTIMEO");
519 1.1 gwr goto out;
520 1.1 gwr }
521 1.1 gwr
522 1.1 gwr /*
523 1.1 gwr * Bind the local endpoint to a bootp client port.
524 1.1 gwr */
525 1.2 drochner if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC))) {
526 1.1 gwr DPRINT("bind failed\n");
527 1.1 gwr goto out;
528 1.1 gwr }
529 1.1 gwr
530 1.1 gwr /*
531 1.1 gwr * Setup socket address for the server.
532 1.1 gwr */
533 1.1 gwr nam = m_get(M_WAIT, MT_SONAME);
534 1.1 gwr sin = mtod(nam, struct sockaddr_in *);
535 1.1 gwr sin->sin_len = nam->m_len = sizeof(*sin);
536 1.1 gwr sin->sin_family = AF_INET;
537 1.1 gwr sin->sin_addr.s_addr = INADDR_BROADCAST;
538 1.1 gwr sin->sin_port = htons(IPPORT_BOOTPS);
539 1.1 gwr
540 1.1 gwr /*
541 1.2 drochner * Allocate buffer used for request
542 1.1 gwr */
543 1.2 drochner m = m_gethdr(M_WAIT, MT_DATA);
544 1.2 drochner MCLGET(m, M_WAIT);
545 1.2 drochner bootp = mtod(m, struct bootp*);
546 1.2 drochner m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
547 1.2 drochner m->m_pkthdr.rcvif = NULL;
548 1.1 gwr
549 1.1 gwr /*
550 1.2 drochner * Build the BOOTP reqest message.
551 1.1 gwr * Note: xid is host order! (opaque to server)
552 1.1 gwr */
553 1.1 gwr bzero((caddr_t)bootp, BOOTP_SIZE_MAX);
554 1.1 gwr bootp->bp_op = BOOTREQUEST;
555 1.1 gwr bootp->bp_htype = hafmt;
556 1.1 gwr bootp->bp_hlen = halen; /* Hardware address length */
557 1.1 gwr bootp->bp_xid = ++xid;
558 1.1 gwr bcopy(haddr, bootp->bp_chaddr, halen);
559 1.1 gwr /* Fill-in the vendor data. */
560 1.1 gwr bcopy(vm_rfc1048, bootp->bp_vend, 4);
561 1.2 drochner #ifdef NFS_BOOT_DHCP
562 1.2 drochner bootp->bp_vend[4] = TAG_DHCP_MSGTYPE;
563 1.2 drochner bootp->bp_vend[5] = 1;
564 1.2 drochner bootp->bp_vend[6] = DHCPDISCOVER;
565 1.2 drochner bootp->bp_vend[7] = TAG_END;
566 1.2 drochner #else
567 1.1 gwr bootp->bp_vend[4] = TAG_END;
568 1.2 drochner #endif
569 1.2 drochner
570 1.2 drochner bpc.xid = xid;
571 1.2 drochner bpc.haddr = haddr;
572 1.2 drochner bpc.halen = halen;
573 1.2 drochner bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
574 1.2 drochner if (bpc.replybuf == NULL)
575 1.2 drochner panic("nfs_boot: malloc reply buf");
576 1.2 drochner #ifdef NFS_BOOT_DHCP
577 1.2 drochner bpc.expected_dhcpmsgtype = DHCPOFFER;
578 1.2 drochner bpc.dhcp_ok = 0;
579 1.2 drochner #endif
580 1.1 gwr
581 1.2 drochner error = nfs_boot_sendrecv(so, nam, bootpset, m,
582 1.2 drochner bootpcheck, 0, 0, &bpc);
583 1.2 drochner if (error)
584 1.1 gwr goto out;
585 1.2 drochner
586 1.2 drochner #ifdef NFS_BOOT_DHCP
587 1.2 drochner if (bpc.dhcp_ok) {
588 1.2 drochner u_int32_t leasetime;
589 1.2 drochner bootp->bp_vend[6] = DHCPREQUEST;
590 1.2 drochner bootp->bp_vend[7] = TAG_REQ_ADDR;
591 1.2 drochner bootp->bp_vend[8] = 4;
592 1.2 drochner bcopy(&bpc.replybuf->bp_yiaddr, &bootp->bp_vend[9], 4);
593 1.2 drochner bootp->bp_vend[13] = TAG_SERVERID;
594 1.2 drochner bootp->bp_vend[14] = 4;
595 1.2 drochner bcopy(&bpc.dhcp_serverip.s_addr, &bootp->bp_vend[15], 4);
596 1.2 drochner bootp->bp_vend[19] = TAG_LEASETIME;
597 1.2 drochner bootp->bp_vend[20] = 4;
598 1.2 drochner leasetime = htonl(300);
599 1.2 drochner bcopy(&leasetime, &bootp->bp_vend[21], 4);
600 1.2 drochner bootp->bp_vend[25] = TAG_END;
601 1.2 drochner
602 1.2 drochner bpc.expected_dhcpmsgtype = DHCPACK;
603 1.2 drochner
604 1.2 drochner error = nfs_boot_sendrecv(so, nam, bootpset, m,
605 1.2 drochner bootpcheck, 0, 0, &bpc);
606 1.2 drochner if (error)
607 1.2 drochner goto out;
608 1.1 gwr }
609 1.2 drochner #endif
610 1.1 gwr
611 1.1 gwr /*
612 1.2 drochner * bootpcheck() has copied the receive mbuf into
613 1.2 drochner * the buffer at bpc.replybuf.
614 1.1 gwr */
615 1.2 drochner bootp_extract(bpc.replybuf, bpc.replylen, nd);
616 1.2 drochner
617 1.2 drochner out:
618 1.2 drochner if (bpc.replybuf)
619 1.2 drochner free(bpc.replybuf, M_DEVBUF);
620 1.2 drochner if (m)
621 1.2 drochner m_freem(m);
622 1.2 drochner if (nam)
623 1.2 drochner m_freem(nam);
624 1.2 drochner return (error);
625 1.2 drochner }
626 1.1 gwr
627 1.2 drochner static void bootp_extract(bootp, replylen, nd)
628 1.2 drochner struct bootp *bootp;
629 1.2 drochner int replylen;
630 1.2 drochner struct nfs_diskless *nd;
631 1.2 drochner {
632 1.2 drochner struct sockaddr_in *sin;
633 1.2 drochner struct in_addr netmask;
634 1.2 drochner struct in_addr gateway;
635 1.2 drochner struct in_addr rootserver;
636 1.2 drochner char *myname; /* my hostname */
637 1.2 drochner char *mydomain; /* my domainname */
638 1.2 drochner char *rootpath;
639 1.2 drochner int mynamelen;
640 1.2 drochner int mydomainlen;
641 1.2 drochner int rootpathlen;
642 1.2 drochner u_int tag, len;
643 1.2 drochner u_char *p, *limit;
644 1.1 gwr
645 1.2 drochner /* Default these to "unspecified". */
646 1.2 drochner netmask.s_addr = 0;
647 1.2 drochner gateway.s_addr = 0;
648 1.2 drochner mydomain = myname = rootpath = NULL;
649 1.2 drochner mydomainlen = mynamelen = rootpathlen = 0;
650 1.2 drochner /* default root server to bootp next-server */
651 1.2 drochner rootserver = bootp->bp_siaddr;
652 1.2 drochner
653 1.2 drochner p = &bootp->bp_vend[4];
654 1.2 drochner limit = ((char*)bootp) + replylen;
655 1.2 drochner while (p < limit) {
656 1.2 drochner tag = *p++;
657 1.2 drochner if (tag == TAG_END)
658 1.2 drochner break;
659 1.2 drochner if (tag == TAG_PAD)
660 1.1 gwr continue;
661 1.2 drochner len = *p++;
662 1.2 drochner if ((p + len) > limit) {
663 1.2 drochner printf("nfs_boot: option %d too long\n", tag);
664 1.2 drochner break;
665 1.2 drochner }
666 1.2 drochner switch (tag) {
667 1.2 drochner case TAG_SUBNET_MASK:
668 1.2 drochner bcopy(p, &netmask, 4);
669 1.2 drochner break;
670 1.2 drochner case TAG_GATEWAY:
671 1.2 drochner /* Routers */
672 1.2 drochner bcopy(p, &gateway, 4);
673 1.2 drochner break;
674 1.2 drochner case TAG_HOST_NAME:
675 1.2 drochner if (len >= sizeof(hostname)) {
676 1.2 drochner printf("nfs_boot: host name >=%d bytes",
677 1.2 drochner sizeof(hostname));
678 1.1 gwr break;
679 1.2 drochner }
680 1.2 drochner myname = p;
681 1.2 drochner mynamelen = len;
682 1.2 drochner break;
683 1.2 drochner case TAG_DOMAIN_NAME:
684 1.2 drochner if (len >= sizeof(domainname)) {
685 1.2 drochner printf("nfs_boot: domain name >=%d bytes",
686 1.2 drochner sizeof(domainname));
687 1.1 gwr break;
688 1.1 gwr }
689 1.2 drochner mydomain = p;
690 1.2 drochner mydomainlen = len;
691 1.2 drochner break;
692 1.2 drochner case TAG_ROOT_PATH:
693 1.2 drochner /* Leave some room for the server name. */
694 1.2 drochner if (len >= (MNAMELEN-10)) {
695 1.2 drochner printf("nfs_boot: rootpath >=%d bytes",
696 1.2 drochner (MNAMELEN-10));
697 1.1 gwr break;
698 1.1 gwr }
699 1.2 drochner rootpath = p;
700 1.2 drochner rootpathlen = len;
701 1.2 drochner break;
702 1.2 drochner case TAG_SWAP_SERVER:
703 1.2 drochner /* override NFS server address */
704 1.2 drochner bcopy(p, &rootserver, 4);
705 1.2 drochner break;
706 1.2 drochner default:
707 1.2 drochner break;
708 1.1 gwr }
709 1.2 drochner p += len;
710 1.1 gwr }
711 1.1 gwr
712 1.1 gwr /*
713 1.1 gwr * Store and print network config info.
714 1.1 gwr */
715 1.1 gwr if (myname) {
716 1.1 gwr myname[mynamelen] = '\0';
717 1.1 gwr strncpy(hostname, myname, sizeof(hostname));
718 1.1 gwr hostnamelen = mynamelen;
719 1.1 gwr printf("nfs_boot: my_name=%s\n", hostname);
720 1.1 gwr }
721 1.1 gwr if (mydomain) {
722 1.1 gwr mydomain[mydomainlen] = '\0';
723 1.1 gwr strncpy(domainname, mydomain, sizeof(domainname));
724 1.1 gwr domainnamelen = mydomainlen;
725 1.1 gwr printf("nfs_boot: my_domain=%s\n", domainname);
726 1.1 gwr }
727 1.1 gwr nd->nd_myip = bootp->bp_yiaddr;
728 1.1 gwr if (nd->nd_myip.s_addr)
729 1.1 gwr printf("nfs_boot: my_addr=0x%x\n", INTOHL(nd->nd_myip));
730 1.1 gwr nd->nd_mask = netmask;
731 1.1 gwr if (nd->nd_mask.s_addr)
732 1.1 gwr printf("nfs_boot: my_mask=0x%x\n", INTOHL(nd->nd_mask));
733 1.1 gwr nd->nd_gwip = gateway;
734 1.1 gwr if (nd->nd_gwip.s_addr)
735 1.1 gwr printf("nfs_boot: gateway=0x%x\n", INTOHL(nd->nd_gwip));
736 1.1 gwr
737 1.1 gwr /*
738 1.1 gwr * Store the information about our NFS root mount.
739 1.1 gwr * The caller will print it, so be silent here.
740 1.1 gwr */
741 1.1 gwr {
742 1.1 gwr struct nfs_dlmount *ndm = &nd->nd_root;
743 1.1 gwr
744 1.1 gwr /* Server IP address. */
745 1.1 gwr sin = (struct sockaddr_in *) &ndm->ndm_saddr;
746 1.1 gwr bzero((caddr_t)sin, sizeof(*sin));
747 1.1 gwr sin->sin_len = sizeof(*sin);
748 1.1 gwr sin->sin_family = AF_INET;
749 1.2 drochner sin->sin_addr = rootserver;
750 1.1 gwr /* Server name. */
751 1.2 drochner if (!bcmp(&rootserver, &bootp->bp_siaddr,
752 1.2 drochner sizeof(struct in_addr))) {
753 1.2 drochner /* standard root server, we have the name */
754 1.2 drochner strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
755 1.2 drochner } else {
756 1.1 gwr /* Show the server IP address numerically. */
757 1.1 gwr sprintf(ndm->ndm_host, "0x%8x",
758 1.2 drochner INTOHL(rootserver));
759 1.1 gwr }
760 1.2 drochner len = strlen(ndm->ndm_host);
761 1.2 drochner if (rootpath &&
762 1.2 drochner len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host)) {
763 1.2 drochner ndm->ndm_host[len++] = ':';
764 1.2 drochner strncpy(ndm->ndm_host + len,
765 1.2 drochner rootpath, rootpathlen);
766 1.2 drochner ndm->ndm_host[len + rootpathlen] = '\0';
767 1.2 drochner } /* else: upper layer will handle error */
768 1.1 gwr }
769 1.1 gwr }
770