1 /* $NetBSD: subr_tftproot.c,v 1.8 2009/03/18 16:00:21 cegger Exp $ */
4 * Copyright (c) 2007 Emmanuel Dreyfus, all rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
35 * Download the root RAMdisk through TFTP at root mount time
38 #include "opt_tftproot.h"
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: subr_tftproot.c,v 1.8 2009/03/18 16:00:21 cegger Exp $");
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/mount.h>
50 #include <sys/timevar.h>
51 #include <sys/socketvar.h>
57 #include <netinet/in.h>
59 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
63 #include <nfs/nfsmount.h>
64 #include <nfs/nfsdiskless.h>
65 #include <nfs/nfs_var.h>
67 extern void mdattach(int);
70 * Copied from <lib/libsa/tftp.h>
73 #define SEGSIZE 512 /* data segment size */
78 #define RRQ 01 /* read request */
79 #define WRQ 02 /* write request */
80 #define DATA 03 /* data packet */
81 #define ACK 04 /* acknowledgement */
82 #define ERROR 05 /* error code */
85 short th_opcode
; /* packet type */
87 unsigned short tu_block
; /* block # */
88 short tu_code
; /* error code */
89 char tu_stuff
[1]; /* request packet stuff */
91 char th_data
[1]; /* data or error string */
94 #define th_block th_u.tu_block
95 #define th_code th_u.tu_code
96 #define th_stuff th_u.tu_stuff
97 #define th_msg th_data
99 #define IPPORT_TFTP 69
101 #ifdef TFTPROOT_DEBUG
102 #define DPRINTF(x) printf x
107 struct tftproot_handle
{
108 struct nfs_diskless
*trh_nd
;
111 unsigned short trh_block
;
115 #define TRH_FINISHED 1
117 int tftproot_dhcpboot(struct device
*);
119 static int tftproot_getfile(struct tftproot_handle
*, struct lwp
*);
120 static int tftproot_recv(struct mbuf
*, void*);
123 tftproot_dhcpboot(struct device
*bootdv
)
125 struct nfs_diskless
*nd
= NULL
;
126 struct ifnet
*ifp
= NULL
;
128 struct tftproot_handle trh
;
132 if (rootspec
!= NULL
) {
134 if (strcmp(rootspec
, ifp
->if_xname
) == 0)
139 (bootdv
!= NULL
&& device_class(bootdv
) == DV_IFNET
)) {
141 if (strcmp(device_xname(bootdv
), ifp
->if_xname
) == 0)
146 DPRINTF(("%s():%d ifp is NULL\n", __func__
, __LINE__
));
150 dv
= device_find_by_xname(ifp
->if_xname
);
152 if ((dv
== NULL
) || (device_class(dv
) != DV_IFNET
)) {
153 DPRINTF(("%s():%d cannot find device for interface %s\n",
154 __func__
, __LINE__
, ifp
->if_xname
));
160 l
= curlwp
; /* XXX */
162 nd
= kmem_zalloc(sizeof(*nd
), KM_SLEEP
);
166 if ((error
= nfs_boot_init(nd
, l
)) != 0) {
167 DPRINTF(("%s():%d nfs_boot_init returned %d\n",
168 __func__
, __LINE__
, error
));
173 * Strip leading "tftp:"
175 #define PREFIX "tftp:"
176 if (strstr(nd
->nd_bootfile
, PREFIX
) == nd
->nd_bootfile
)
177 (void)memmove(nd
->nd_bootfile
,
178 nd
->nd_bootfile
+ (sizeof(PREFIX
) - 1),
179 sizeof(nd
->nd_bootfile
) - sizeof(PREFIX
));
182 printf("tftproot: bootfile=%s\n", nd
->nd_bootfile
);
184 memset(&trh
, 0, sizeof(trh
));
188 if ((error
= tftproot_getfile(&trh
, l
)) != 0) {
189 DPRINTF(("%s():%d tftproot_getfile returned %d\n",
190 __func__
, __LINE__
, error
));
198 kmem_free(nd
, sizeof(*nd
));
204 tftproot_getfile(struct tftproot_handle
*trh
, struct lwp
*l
)
206 struct socket
*so
= NULL
;
207 struct mbuf
*m_serv
= NULL
;
208 struct mbuf
*m_outbuf
= NULL
;
209 struct sockaddr_in
*sin
;
210 struct tftphdr
*tftp
;
211 size_t packetlen
, namelen
;
213 const char octetstr
[] = "octet";
214 size_t hdrlen
= sizeof(*tftp
) - sizeof(tftp
->th_data
);
216 /* struct device *dv; */
218 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0, l
, NULL
)) != 0) {
219 DPRINTF(("%s():%d socreate returned %d\n",
220 __func__
, __LINE__
, error
));
227 if ((error
= nfs_boot_setrecvtimo(so
))) {
228 DPRINTF(("%s():%d SO_RCVTIMEO failed %d\n",
229 __func__
, __LINE__
, error
));
234 * Set server address and port
236 m_serv
= m_get(M_WAIT
, MT_SONAME
);
237 m_serv
->m_len
= sizeof(*sin
);
238 sin
= mtod(m_serv
, struct sockaddr_in
*);
239 memcpy(sin
, &trh
->trh_nd
->nd_root
.ndm_saddr
, sizeof(*sin
));
240 sin
->sin_port
= htons(IPPORT_TFTP
);
243 * Set send buffer, prepare the TFTP packet
245 namelen
= strlen(trh
->trh_nd
->nd_bootfile
) + 1;
246 packetlen
= sizeof(tftp
->th_opcode
) + namelen
+ sizeof(octetstr
);
247 if (packetlen
> MSIZE
) {
248 DPRINTF(("%s():%d boot filename too long (%ld bytes)\n",
249 __func__
, __LINE__
, (long)namelen
));
253 m_outbuf
= m_gethdr(M_WAIT
, MT_DATA
);
254 m_clget(m_outbuf
, M_WAIT
);
255 m_outbuf
->m_len
= packetlen
;
256 m_outbuf
->m_pkthdr
.len
= packetlen
;
257 m_outbuf
->m_pkthdr
.rcvif
= NULL
;
259 tftp
= mtod(m_outbuf
, struct tftphdr
*);
260 memset(tftp
, 0, packetlen
);
262 tftp
->th_opcode
= htons((short)RRQ
);
264 (void)strncpy(cp
, trh
->trh_nd
->nd_bootfile
, namelen
);
266 (void)strncpy(cp
, octetstr
, sizeof(octetstr
));
269 * Perform the file transfer
271 sin
= (struct sockaddr_in
*)&trh
->trh_nd
->nd_root
.ndm_saddr
;
272 printf("tftproot: download %s:%s ",
273 inet_ntoa(sin
->sin_addr
), trh
->trh_nd
->nd_bootfile
);
277 * Show progress for every 200 blocks (100kB)
279 #ifndef TFTPROOT_PROGRESS
280 #define TFTPROOT_PROGRESS 200
282 if ((trh
->trh_block
% TFTPROOT_PROGRESS
) == 0)
286 * Send the packet and receive the answer.
287 * We get the sender address here, which should be
288 * the same server with a different port
290 if ((error
= nfs_boot_sendrecv(so
, m_serv
, NULL
, m_outbuf
,
291 tftproot_recv
, NULL
, &m_serv
, trh
, l
)) != 0) {
292 DPRINTF(("%s():%d sendrecv failed %d\n",
293 __func__
, __LINE__
, error
));
298 * Accomodate the packet length for acks.
299 * This is really needed only on first pass
301 m_outbuf
->m_len
= hdrlen
;
302 m_outbuf
->m_pkthdr
.len
= hdrlen
;
303 tftp
->th_opcode
= htons((short)ACK
);
304 tftp
->th_block
= htons(trh
->trh_block
);
308 * Check for termination
310 if (trh
->trh_flags
& TRH_FINISHED
)
314 } while (1/* CONSTCOND */);
319 * Ack the last block. so_send frees m_outbuf, therefore
320 * we do not want to free it ourselves.
321 * Ignore errors, as we already have the whole file.
323 if ((error
= (*so
->so_send
)(so
, m_serv
, NULL
,
324 m_outbuf
, NULL
, 0, l
)) != 0)
325 DPRINTF(("%s():%d tftproot: sosend returned %d\n",
326 __func__
, __LINE__
, error
));
331 * And use it as the root ramdisk.
333 DPRINTF(("%s():%d RAMdisk loaded: %ld@%p\n",
334 __func__
, __LINE__
, trh
->trh_len
, trh
->trh_base
));
335 md_root_setconf(trh
->trh_base
, trh
->trh_len
);
353 tftproot_recv(struct mbuf
*m
, void *ctx
)
355 struct tftproot_handle
*trh
= ctx
;
356 struct tftphdr
*tftp
;
358 size_t hdrlen
= sizeof(*tftp
) - sizeof(tftp
->th_data
);
361 * Check for short packet
363 if (m
->m_pkthdr
.len
< hdrlen
) {
364 DPRINTF(("%s():%d short reply (%d bytes)\n",
365 __func__
, __LINE__
, m
->m_pkthdr
.len
));
370 * Check for packet too large for being a TFTP packet
372 if (m
->m_pkthdr
.len
> hdrlen
+ SEGSIZE
) {
373 DPRINTF(("%s():%d packet too big (%d bytes)\n",
374 __func__
, __LINE__
, m
->m_pkthdr
.len
));
380 * Examine the TFTP header
382 if (m
->m_len
> sizeof(*tftp
)) {
383 if ((m
= m_pullup(m
, sizeof(*tftp
))) == NULL
) {
384 DPRINTF(("%s():%d m_pullup failed\n",
385 __func__
, __LINE__
));
389 tftp
= mtod(m
, struct tftphdr
*);
392 * We handle data or error
394 switch(ntohs(tftp
->th_opcode
)) {
399 char errbuf
[SEGSIZE
+ 1];
402 for (i
= 0; i
< SEGSIZE
; i
++) {
403 if ((tftp
->th_data
[i
] < ' ') ||
404 (tftp
->th_data
[i
] > '~')) {
408 errbuf
[i
] = tftp
->th_data
[i
];
410 errbuf
[SEGSIZE
] = '\0';
412 printf("tftproot: TFTP server returned error %d, %s\n",
413 ntohs(tftp
->th_code
), errbuf
);
420 DPRINTF(("%s():%d unexpected tftp reply opcode %d\n",
421 __func__
, __LINE__
, ntohs(tftp
->th_opcode
)));
427 * Check for last packet, which does not fill the whole space
429 if (m
->m_pkthdr
.len
< hdrlen
+ SEGSIZE
) {
430 DPRINTF(("%s():%d last chunk (%d bytes)\n",
431 __func__
, __LINE__
, m
->m_pkthdr
.len
));
432 trh
->trh_flags
|= TRH_FINISHED
;
436 if (ntohs(tftp
->th_block
) != trh
->trh_block
) {
437 DPRINTF(("%s():%d expected block %d, got block %d\n",
438 __func__
, __LINE__
, trh
->trh_block
, ntohs(tftp
->th_block
)));
443 * Grow the receiving buffer to accomodate new data
445 newlen
= trh
->trh_len
+ (m
->m_pkthdr
.len
- hdrlen
);
446 if ((trh
->trh_base
= realloc(trh
->trh_base
,
447 newlen
, M_TEMP
, M_WAITOK
)) == NULL
) {
448 DPRINTF(("%s():%d failed to realloc %ld bytes\n",
449 __func__
, __LINE__
, (long)newlen
));
456 m_copydata(m
, hdrlen
, m
->m_pkthdr
.len
- hdrlen
,
457 (char *)trh
->trh_base
+ trh
->trh_len
);
458 trh
->trh_len
= newlen
;