subr_tftproot.c revision 1.21 1 /* $NetBSD: subr_tftproot.c,v 1.21 2018/02/08 09:05:20 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Download the root RAMdisk through TFTP at root mount time
36 */
37
38 #include "opt_tftproot.h"
39 #include "opt_md.h"
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: subr_tftproot.c,v 1.21 2018/02/08 09:05:20 dholland Exp $");
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/lwp.h>
49 #include <sys/kmem.h>
50 #include <sys/mbuf.h>
51 #include <sys/timevar.h>
52 #include <sys/socketvar.h>
53
54 #include <net/if.h>
55
56 #include <dev/md.h>
57
58 #include <netinet/in.h>
59
60 #include <nfs/rpcv2.h>
61
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfs.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/nfsdiskless.h>
66 #include <nfs/nfs_var.h>
67
68 /*
69 * Copied from <lib/libsa/tftp.h>
70 */
71
72 #define SEGSIZE 512 /* data segment size */
73
74 /*
75 * Packet types.
76 */
77 #define RRQ 01 /* read request */
78 #define WRQ 02 /* write request */
79 #define DATA 03 /* data packet */
80 #define ACK 04 /* acknowledgement */
81 #define ERROR 05 /* error code */
82
83 struct tftphdr {
84 short th_opcode; /* packet type */
85 union {
86 unsigned short tu_block; /* block # */
87 short tu_code; /* error code */
88 char tu_stuff[1]; /* request packet stuff */
89 } th_u;
90 char th_data[1]; /* data or error string */
91 } __packed;
92
93 #define th_block th_u.tu_block
94 #define th_code th_u.tu_code
95 #define th_stuff th_u.tu_stuff
96 #define th_msg th_data
97
98 #define IPPORT_TFTP 69
99
100 #ifdef TFTPROOT_DEBUG
101 #define DPRINTF(x) printf x
102 #else
103 #define DPRINTF(x)
104 #endif
105
106 struct tftproot_handle {
107 struct nfs_diskless *trh_nd;
108 void *trh_base;
109 size_t trh_len;
110 unsigned short trh_block;
111 int trh_flags;
112 };
113
114 #define TRH_FINISHED 1
115
116 int tftproot_dhcpboot(device_t);
117
118 static int tftproot_getfile(struct tftproot_handle *, struct lwp *);
119 static int tftproot_recv(struct mbuf **, void *);
120
121 int
122 tftproot_dhcpboot(device_t bootdv)
123 {
124 struct nfs_diskless *nd = NULL;
125 struct ifnet *ifp = NULL;
126 struct lwp *l;
127 struct tftproot_handle trh;
128 device_t dv;
129 int error = -1;
130
131 if (rootspec != NULL) {
132 int s = pserialize_read_enter();
133 IFNET_READER_FOREACH(ifp)
134 if (strcmp(rootspec, ifp->if_xname) == 0)
135 break;
136 pserialize_read_exit(s);
137 }
138
139 if ((ifp == NULL) &&
140 (bootdv != NULL && device_class(bootdv) == DV_IFNET)) {
141 int s = pserialize_read_enter();
142 IFNET_READER_FOREACH(ifp)
143 if (strcmp(device_xname(bootdv), ifp->if_xname) == 0)
144 break;
145 pserialize_read_exit(s);
146 }
147
148 if (ifp == NULL) {
149 DPRINTF(("%s():%d ifp is NULL\n", __func__, __LINE__));
150 goto out;
151 }
152
153 dv = device_find_by_xname(ifp->if_xname);
154
155 if ((dv == NULL) || (device_class(dv) != DV_IFNET)) {
156 DPRINTF(("%s():%d cannot find device for interface %s\n",
157 __func__, __LINE__, ifp->if_xname));
158 goto out;
159 }
160
161 root_device = dv;
162
163 l = curlwp; /* XXX */
164
165 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
166 nd->nd_ifp = ifp;
167 nd->nd_nomount = 1;
168
169 if ((error = nfs_boot_init(nd, l)) != 0) {
170 DPRINTF(("%s():%d nfs_boot_init returned %d\n",
171 __func__, __LINE__, error));
172 goto out;
173 }
174
175 /*
176 * Strip leading "tftp:"
177 */
178 #define PREFIX "tftp:"
179 if (strstr(nd->nd_bootfile, PREFIX) == nd->nd_bootfile)
180 (void)memmove(nd->nd_bootfile,
181 nd->nd_bootfile + (sizeof(PREFIX) - 1),
182 sizeof(nd->nd_bootfile) - sizeof(PREFIX));
183 #undef PREFIX
184
185 printf("tftproot: bootfile=%s\n", nd->nd_bootfile);
186
187 memset(&trh, 0, sizeof(trh));
188 trh.trh_nd = nd;
189 trh.trh_block = 1;
190
191 if ((error = tftproot_getfile(&trh, l)) != 0) {
192 DPRINTF(("%s():%d tftproot_getfile returned %d\n",
193 __func__, __LINE__, error));
194 goto out;
195 }
196
197 error = 0;
198
199 out:
200 if (nd)
201 kmem_free(nd, sizeof(*nd));
202
203 return error;
204 }
205
206 static int
207 tftproot_getfile(struct tftproot_handle *trh, struct lwp *l)
208 {
209 struct socket *so = NULL;
210 struct mbuf *m_serv = NULL;
211 struct mbuf *m_outbuf = NULL;
212 struct sockaddr_in sin;
213 struct tftphdr *tftp;
214 size_t packetlen, namelen;
215 int error = -1;
216 const char octetstr[] = "octet";
217 size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data);
218 char *cp;
219
220 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, l, NULL)) != 0) {
221 DPRINTF(("%s():%d socreate returned %d\n",
222 __func__, __LINE__, error));
223 goto out;
224 }
225
226 /*
227 * Set timeout
228 */
229 if ((error = nfs_boot_setrecvtimo(so))) {
230 DPRINTF(("%s():%d SO_RCVTIMEO failed %d\n",
231 __func__, __LINE__, error));
232 goto out;
233 }
234
235 /*
236 * Set server address and port
237 */
238 memcpy(&sin, &trh->trh_nd->nd_root.ndm_saddr, sizeof(sin));
239 sin.sin_port = htons(IPPORT_TFTP);
240
241 /*
242 * Set send buffer, prepare the TFTP packet
243 */
244 namelen = strlen(trh->trh_nd->nd_bootfile) + 1;
245 packetlen = sizeof(tftp->th_opcode) + namelen + sizeof(octetstr);
246 if (packetlen > MSIZE) {
247 DPRINTF(("%s():%d boot filename too long (%ld bytes)\n",
248 __func__, __LINE__, (long)namelen));
249 goto out;
250 }
251
252 m_outbuf = m_gethdr(M_WAIT, MT_DATA);
253 m_clget(m_outbuf, M_WAIT);
254 m_outbuf->m_len = packetlen;
255 m_outbuf->m_pkthdr.len = packetlen;
256 m_reset_rcvif(m_outbuf);
257
258 tftp = mtod(m_outbuf, struct tftphdr *);
259 memset(tftp, 0, packetlen);
260
261 tftp->th_opcode = htons((short)RRQ);
262 cp = tftp->th_stuff;
263 (void)strncpy(cp, trh->trh_nd->nd_bootfile, namelen);
264 cp += namelen;
265 (void)strncpy(cp, octetstr, sizeof(octetstr));
266
267 /*
268 * Perform the file transfer
269 */
270 printf("tftproot: download %s:%s ",
271 inet_ntoa(sin.sin_addr), trh->trh_nd->nd_bootfile);
272
273 do {
274 /*
275 * Show progress for every 200 blocks (100kB)
276 */
277 #ifndef TFTPROOT_PROGRESS
278 #define TFTPROOT_PROGRESS 200
279 #endif
280 if ((trh->trh_block % TFTPROOT_PROGRESS) == 0)
281 twiddle();
282
283 /*
284 * Send the packet and receive the answer.
285 * We get the sender address here, which should be
286 * the same server with a different port
287 */
288 if ((error = nfs_boot_sendrecv(so, &sin, NULL, m_outbuf,
289 tftproot_recv, NULL, &m_serv, trh, l)) != 0) {
290 DPRINTF(("%s():%d sendrecv failed %d\n",
291 __func__, __LINE__, error));
292 goto out;
293 }
294
295 /*
296 * Accommodate the packet length for acks.
297 * This is really needed only on first pass
298 */
299 m_outbuf->m_len = hdrlen;
300 m_outbuf->m_pkthdr.len = hdrlen;
301 tftp->th_opcode = htons((short)ACK);
302 tftp->th_block = htons(trh->trh_block);
303
304
305 /*
306 * Check for termination
307 */
308 if (trh->trh_flags & TRH_FINISHED)
309 break;
310
311 trh->trh_block++;
312 } while (1/* CONSTCOND */);
313
314 printf("\n");
315
316 /*
317 * Ack the last block. Ignore errors, as we already have the whole
318 * file.
319 */
320 if ((error = (*so->so_send)(so, mtod(m_serv, struct sockaddr *), NULL,
321 m_outbuf, NULL, 0, l)) != 0)
322 DPRINTF(("%s():%d tftproot: sosend returned %d\n",
323 __func__, __LINE__, error));
324
325 /* Freed by the protocol */
326 m_outbuf = NULL;
327
328 /*
329 * And use it as the root ramdisk.
330 */
331 DPRINTF(("%s():%d RAMdisk loaded: %ld@%p\n",
332 __func__, __LINE__, trh->trh_len, trh->trh_base));
333 md_root_setconf(trh->trh_base, trh->trh_len);
334
335 error = 0;
336 out:
337 if (m_serv)
338 m_freem(m_serv);
339
340 if (m_outbuf)
341 m_freem(m_outbuf);
342
343 if (so)
344 soclose(so);
345
346 return error;
347 }
348
349 static int
350 tftproot_recv(struct mbuf **mp, void *ctx)
351 {
352 struct tftproot_handle *trh = ctx;
353 struct tftphdr *tftp;
354 struct mbuf *m = *mp;
355 size_t newlen;
356 size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data);
357
358 /*
359 * Check for short packet
360 */
361 if (m->m_pkthdr.len < hdrlen) {
362 DPRINTF(("%s():%d short reply (%d bytes)\n",
363 __func__, __LINE__, m->m_pkthdr.len));
364 return -1;
365 }
366
367 /*
368 * Check for packet too large for being a TFTP packet
369 */
370 if (m->m_pkthdr.len > hdrlen + SEGSIZE) {
371 DPRINTF(("%s():%d packet too big (%d bytes)\n",
372 __func__, __LINE__, m->m_pkthdr.len));
373 return -1;
374 }
375
376
377 /*
378 * Examine the TFTP header
379 */
380 if (m->m_len > sizeof(*tftp)) {
381 if ((m = *mp = m_pullup(m, sizeof(*tftp))) == NULL) {
382 DPRINTF(("%s():%d m_pullup failed\n",
383 __func__, __LINE__));
384 return -1;
385 }
386 }
387 tftp = mtod(m, struct tftphdr *);
388
389 /*
390 * We handle data or error
391 */
392 switch(ntohs(tftp->th_opcode)) {
393 case DATA:
394 break;
395
396 case ERROR: {
397 char errbuf[SEGSIZE + 1];
398 int i;
399
400 for (i = 0; i < SEGSIZE; i++) {
401 if ((tftp->th_data[i] < ' ') ||
402 (tftp->th_data[i] > '~')) {
403 errbuf[i] = '\0';
404 break;
405 }
406 errbuf[i] = tftp->th_data[i];
407 }
408 errbuf[SEGSIZE] = '\0';
409
410 printf("tftproot: TFTP server returned error %d, %s\n",
411 ntohs(tftp->th_code), errbuf);
412
413 return -1;
414 break;
415 }
416
417 default:
418 DPRINTF(("%s():%d unexpected tftp reply opcode %d\n",
419 __func__, __LINE__, ntohs(tftp->th_opcode)));
420 return -1;
421 break;
422 }
423
424 /*
425 * Check for last packet, which does not fill the whole space
426 */
427 if (m->m_pkthdr.len < hdrlen + SEGSIZE) {
428 DPRINTF(("%s():%d last chunk (%d bytes)\n",
429 __func__, __LINE__, m->m_pkthdr.len));
430 trh->trh_flags |= TRH_FINISHED;
431 }
432
433
434 if (ntohs(tftp->th_block) != trh->trh_block) {
435 DPRINTF(("%s():%d expected block %d, got block %d\n",
436 __func__, __LINE__, trh->trh_block, ntohs(tftp->th_block)));
437 return -1;
438 }
439
440 /*
441 * Grow the receiving buffer to accommodate new data
442 */
443 newlen = trh->trh_len + (m->m_pkthdr.len - hdrlen);
444 if ((trh->trh_base = realloc(trh->trh_base,
445 newlen, M_TEMP, M_WAITOK)) == NULL) {
446 DPRINTF(("%s():%d failed to realloc %ld bytes\n",
447 __func__, __LINE__, (long)newlen));
448 return -1;
449 }
450
451 /*
452 * Copy the data
453 */
454 m_copydata(m, hdrlen, m->m_pkthdr.len - hdrlen,
455 (char *)trh->trh_base + trh->trh_len);
456 trh->trh_len = newlen;
457
458 return 0;
459 }
460
461