Home | History | Annotate | Line # | Download | only in kern
subr_tftproot.c revision 1.19
      1 /*	$NetBSD: subr_tftproot.c,v 1.19 2016/10/31 15:27:24 maxv 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.19 2016/10/31 15:27:24 maxv 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 extern void       mdattach(int);
     69 
     70 /*
     71  * Copied from <lib/libsa/tftp.h>
     72  */
     73 
     74 #define SEGSIZE         512             /* data segment size */
     75 
     76 /*
     77  * Packet types.
     78  */
     79 #define RRQ     01                      /* read request */
     80 #define WRQ     02                      /* write request */
     81 #define DATA    03                      /* data packet */
     82 #define ACK     04                      /* acknowledgement */
     83 #define ERROR   05                      /* error code */
     84 
     85 struct  tftphdr {
     86         short   th_opcode;              /* packet type */
     87         union {
     88                 unsigned short tu_block; /* block # */
     89                 short   tu_code;        /* error code */
     90                 char    tu_stuff[1];    /* request packet stuff */
     91         } th_u;
     92         char    th_data[1];             /* data or error string */
     93 } __packed;
     94 
     95 #define th_block        th_u.tu_block
     96 #define th_code         th_u.tu_code
     97 #define th_stuff        th_u.tu_stuff
     98 #define th_msg          th_data
     99 
    100 #define IPPORT_TFTP 69
    101 
    102 #ifdef TFTPROOT_DEBUG
    103 #define DPRINTF(x) printf x
    104 #else
    105 #define DPRINTF(x)
    106 #endif
    107 
    108 struct tftproot_handle {
    109 	struct nfs_diskless *trh_nd;
    110 	void *trh_base;
    111 	size_t trh_len;
    112 	unsigned short trh_block;
    113 	int trh_flags;
    114 };
    115 
    116 #define TRH_FINISHED	1
    117 
    118 int tftproot_dhcpboot(device_t);
    119 
    120 static int tftproot_getfile(struct tftproot_handle *, struct lwp *);
    121 static int tftproot_recv(struct mbuf **, void *);
    122 
    123 int
    124 tftproot_dhcpboot(device_t bootdv)
    125 {
    126 	struct nfs_diskless *nd = NULL;
    127 	struct ifnet *ifp = NULL;
    128 	struct lwp *l;
    129 	struct tftproot_handle trh;
    130 	device_t dv;
    131 	int error = -1;
    132 
    133 	if (rootspec != NULL) {
    134 		int s = pserialize_read_enter();
    135 		IFNET_READER_FOREACH(ifp)
    136 			if (strcmp(rootspec, ifp->if_xname) == 0)
    137 				break;
    138 		pserialize_read_exit(s);
    139 	}
    140 
    141 	if ((ifp == NULL) &&
    142 	    (bootdv != NULL && device_class(bootdv) == DV_IFNET)) {
    143 		int s = pserialize_read_enter();
    144 		IFNET_READER_FOREACH(ifp)
    145 			if (strcmp(device_xname(bootdv), ifp->if_xname) == 0)
    146 				break;
    147 		pserialize_read_exit(s);
    148 	}
    149 
    150 	if (ifp == NULL) {
    151 		DPRINTF(("%s():%d ifp is NULL\n", __func__, __LINE__));
    152 		goto out;
    153 	}
    154 
    155 	dv = device_find_by_xname(ifp->if_xname);
    156 
    157 	if ((dv == NULL) || (device_class(dv) != DV_IFNET)) {
    158 		DPRINTF(("%s():%d cannot find device for interface %s\n",
    159 		    __func__, __LINE__, ifp->if_xname));
    160 		goto out;
    161 	}
    162 
    163 	root_device = dv;
    164 
    165 	l = curlwp; /* XXX */
    166 
    167 	nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
    168 	nd->nd_ifp = ifp;
    169 	nd->nd_nomount = 1;
    170 
    171 	if ((error = nfs_boot_init(nd, l)) != 0) {
    172 		DPRINTF(("%s():%d nfs_boot_init returned %d\n",
    173 		    __func__, __LINE__, error));
    174 		goto out;
    175 	}
    176 
    177 	/*
    178 	 * Strip leading "tftp:"
    179 	 */
    180 #define PREFIX "tftp:"
    181 	if (strstr(nd->nd_bootfile, PREFIX) == nd->nd_bootfile)
    182 		(void)memmove(nd->nd_bootfile,
    183 			      nd->nd_bootfile + (sizeof(PREFIX) - 1),
    184 			      sizeof(nd->nd_bootfile) - sizeof(PREFIX));
    185 #undef PREFIX
    186 
    187 	printf("tftproot: bootfile=%s\n", nd->nd_bootfile);
    188 
    189 	memset(&trh, 0, sizeof(trh));
    190 	trh.trh_nd = nd;
    191 	trh.trh_block = 1;
    192 
    193 	if ((error = tftproot_getfile(&trh, l)) != 0) {
    194 		DPRINTF(("%s():%d tftproot_getfile returned %d\n",
    195 		    __func__, __LINE__, error));
    196 		goto out;
    197 	}
    198 
    199 	error = 0;
    200 
    201 out:
    202 	if (nd)
    203 		kmem_free(nd, sizeof(*nd));
    204 
    205 	return error;
    206 }
    207 
    208 static int
    209 tftproot_getfile(struct tftproot_handle *trh, struct lwp *l)
    210 {
    211 	struct socket *so = NULL;
    212 	struct mbuf *m_serv = NULL;
    213 	struct mbuf *m_outbuf = NULL;
    214 	struct sockaddr_in sin;
    215 	struct tftphdr *tftp;
    216 	size_t packetlen, namelen;
    217 	int error = -1;
    218 	const char octetstr[] = "octet";
    219 	size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data);
    220 	char *cp;
    221 
    222 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, l, NULL)) != 0) {
    223 		DPRINTF(("%s():%d socreate returned %d\n",
    224 		    __func__, __LINE__, error));
    225 		goto out;
    226 	}
    227 
    228 	/*
    229 	 * Set timeout
    230 	 */
    231 	if ((error = nfs_boot_setrecvtimo(so))) {
    232 		DPRINTF(("%s():%d SO_RCVTIMEO failed %d\n",
    233 		    __func__, __LINE__, error));
    234 		goto out;
    235 	}
    236 
    237 	/*
    238 	 * Set server address and port
    239 	 */
    240 	memcpy(&sin, &trh->trh_nd->nd_root.ndm_saddr, sizeof(sin));
    241 	sin.sin_port = htons(IPPORT_TFTP);
    242 
    243 	/*
    244 	 * Set send buffer, prepare the TFTP packet
    245 	 */
    246 	namelen = strlen(trh->trh_nd->nd_bootfile) + 1;
    247 	packetlen = sizeof(tftp->th_opcode) + namelen + sizeof(octetstr);
    248 	if (packetlen > MSIZE) {
    249 		DPRINTF(("%s():%d boot filename too long (%ld bytes)\n",
    250 		    __func__, __LINE__, (long)namelen));
    251 		goto out;
    252 	}
    253 
    254 	m_outbuf = m_gethdr(M_WAIT, MT_DATA);
    255 	m_clget(m_outbuf, M_WAIT);
    256 	m_outbuf->m_len = packetlen;
    257 	m_outbuf->m_pkthdr.len = packetlen;
    258 	m_reset_rcvif(m_outbuf);
    259 
    260 	tftp = mtod(m_outbuf, struct tftphdr *);
    261 	memset(tftp, 0, packetlen);
    262 
    263 	tftp->th_opcode = htons((short)RRQ);
    264 	cp = tftp->th_stuff;
    265 	(void)strncpy(cp,  trh->trh_nd->nd_bootfile, namelen);
    266 	cp += namelen;
    267 	(void)strncpy(cp, octetstr, sizeof(octetstr));
    268 
    269 	/*
    270 	 * Perform the file transfer
    271 	 */
    272 	printf("tftproot: download %s:%s ",
    273 	    inet_ntoa(sin.sin_addr), trh->trh_nd->nd_bootfile);
    274 
    275 	do {
    276 		/*
    277 		 * Show progress for every 200 blocks (100kB)
    278 		 */
    279 #ifndef TFTPROOT_PROGRESS
    280 #define TFTPROOT_PROGRESS 200
    281 #endif
    282 		if ((trh->trh_block % TFTPROOT_PROGRESS) == 0)
    283 			twiddle();
    284 
    285 		/*
    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
    289 		 */
    290 		if ((error = nfs_boot_sendrecv(so, &sin, NULL, m_outbuf,
    291 		    tftproot_recv, NULL, &m_serv, trh, l)) != 0) {
    292 			DPRINTF(("%s():%d sendrecv failed %d\n",
    293 			    __func__, __LINE__, error));
    294 			goto out;
    295 		}
    296 
    297 		/*
    298 		 * Accomodate the packet length for acks.
    299 		 * This is really needed only on first pass
    300 		 */
    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);
    305 
    306 
    307 		/*
    308 		 * Check for termination
    309 		 */
    310 		if (trh->trh_flags & TRH_FINISHED)
    311 			break;
    312 
    313 		trh->trh_block++;
    314 	} while (1/* CONSTCOND */);
    315 
    316 	printf("\n");
    317 
    318 	/*
    319 	 * Ack the last block. Ignore errors, as we already have the whole
    320 	 * file.
    321 	 */
    322 	if ((error = (*so->so_send)(so, mtod(m_serv, struct sockaddr *), NULL,
    323 	    m_outbuf, NULL, 0, l)) != 0)
    324 		DPRINTF(("%s():%d tftproot: sosend returned %d\n",
    325 		    __func__, __LINE__, error));
    326 
    327 	/* Freed by the protocol */
    328 	m_outbuf = NULL;
    329 
    330 	/*
    331 	 * And use it as the root ramdisk.
    332 	 */
    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);
    336 	mdattach(0);
    337 
    338 	error = 0;
    339 out:
    340 	if (m_serv)
    341 		m_freem(m_serv);
    342 
    343 	if (m_outbuf)
    344 		m_freem(m_outbuf);
    345 
    346 	if (so)
    347 		soclose(so);
    348 
    349 	return error;
    350 }
    351 
    352 static int
    353 tftproot_recv(struct mbuf **mp, void *ctx)
    354 {
    355 	struct tftproot_handle *trh = ctx;
    356 	struct tftphdr *tftp;
    357 	struct mbuf *m = *mp;
    358 	size_t newlen;
    359 	size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data);
    360 
    361 	/*
    362 	 * Check for short packet
    363 	 */
    364 	if (m->m_pkthdr.len < hdrlen) {
    365 		DPRINTF(("%s():%d short reply (%d bytes)\n",
    366 		    __func__, __LINE__, m->m_pkthdr.len));
    367 		return -1;
    368 	}
    369 
    370 	/*
    371 	 * Check for packet too large for being a TFTP packet
    372 	 */
    373 	if (m->m_pkthdr.len > hdrlen + SEGSIZE) {
    374 		DPRINTF(("%s():%d packet too big (%d bytes)\n",
    375 		    __func__, __LINE__, m->m_pkthdr.len));
    376 		return -1;
    377 	}
    378 
    379 
    380 	/*
    381 	 * Examine the TFTP header
    382 	 */
    383 	if (m->m_len > sizeof(*tftp)) {
    384 		if ((m = *mp = m_pullup(m, sizeof(*tftp))) == NULL) {
    385 			DPRINTF(("%s():%d m_pullup failed\n",
    386 			    __func__, __LINE__));
    387 			return -1;
    388 		}
    389 	}
    390 	tftp = mtod(m, struct tftphdr *);
    391 
    392 	/*
    393 	 * We handle data or error
    394 	 */
    395 	switch(ntohs(tftp->th_opcode)) {
    396 	case DATA:
    397 		break;
    398 
    399 	case ERROR: {
    400 		char errbuf[SEGSIZE + 1];
    401 		int i;
    402 
    403 		for (i = 0; i < SEGSIZE; i++) {
    404 			if ((tftp->th_data[i] < ' ') ||
    405 			    (tftp->th_data[i] > '~')) {
    406 				errbuf[i] = '\0';
    407 				break;
    408 			}
    409 			errbuf[i] = tftp->th_data[i];
    410 		}
    411 		errbuf[SEGSIZE] = '\0';
    412 
    413 		printf("tftproot: TFTP server returned error %d, %s\n",
    414 		    ntohs(tftp->th_code), errbuf);
    415 
    416 		return -1;
    417 		break;
    418 	}
    419 
    420 	default:
    421 		DPRINTF(("%s():%d unexpected tftp reply opcode %d\n",
    422 		    __func__, __LINE__, ntohs(tftp->th_opcode)));
    423 		return -1;
    424 		break;
    425 	}
    426 
    427 	/*
    428 	 * Check for last packet, which does not fill the whole space
    429 	 */
    430 	if (m->m_pkthdr.len < hdrlen + SEGSIZE) {
    431 		DPRINTF(("%s():%d last chunk (%d bytes)\n",
    432 		    __func__, __LINE__, m->m_pkthdr.len));
    433 		trh->trh_flags |= TRH_FINISHED;
    434 	}
    435 
    436 
    437 	if (ntohs(tftp->th_block) != trh->trh_block) {
    438 		DPRINTF(("%s():%d expected block %d, got block %d\n",
    439 		    __func__, __LINE__, trh->trh_block, ntohs(tftp->th_block)));
    440 		return -1;
    441 	}
    442 
    443 	/*
    444 	 * Grow the receiving buffer to accommodate new data
    445 	 */
    446 	newlen = trh->trh_len + (m->m_pkthdr.len - hdrlen);
    447 	if ((trh->trh_base = realloc(trh->trh_base,
    448 	    newlen, M_TEMP, M_WAITOK)) == NULL) {
    449 		DPRINTF(("%s():%d failed to realloc %ld bytes\n",
    450 		    __func__, __LINE__, (long)newlen));
    451 		return -1;
    452 	}
    453 
    454 	/*
    455 	 * Copy the data
    456 	 */
    457 	m_copydata(m, hdrlen, m->m_pkthdr.len - hdrlen,
    458 		   (char *)trh->trh_base + trh->trh_len);
    459 	trh->trh_len = newlen;
    460 
    461 	return 0;
    462 }
    463 
    464