1 /* $NetBSD: subr_tftproot.c,v 1.16 2015/05/21 02:04:22 rtr 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.16 2015/05/21 02:04:22 rtr 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 IFNET_FOREACH(ifp) 135 if (strcmp(rootspec, ifp->if_xname) == 0) 136 break; 137 } 138 139 if ((ifp == NULL) && 140 (bootdv != NULL && device_class(bootdv) == DV_IFNET)) { 141 IFNET_FOREACH(ifp) 142 if (strcmp(device_xname(bootdv), ifp->if_xname) == 0) 143 break; 144 } 145 146 if (ifp == NULL) { 147 DPRINTF(("%s():%d ifp is NULL\n", __func__, __LINE__)); 148 goto out; 149 } 150 151 dv = device_find_by_xname(ifp->if_xname); 152 153 if ((dv == NULL) || (device_class(dv) != DV_IFNET)) { 154 DPRINTF(("%s():%d cannot find device for interface %s\n", 155 __func__, __LINE__, ifp->if_xname)); 156 goto out; 157 } 158 159 root_device = dv; 160 161 l = curlwp; /* XXX */ 162 163 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP); 164 nd->nd_ifp = ifp; 165 nd->nd_nomount = 1; 166 167 if ((error = nfs_boot_init(nd, l)) != 0) { 168 DPRINTF(("%s():%d nfs_boot_init returned %d\n", 169 __func__, __LINE__, error)); 170 goto out; 171 } 172 173 /* 174 * Strip leading "tftp:" 175 */ 176 #define PREFIX "tftp:" 177 if (strstr(nd->nd_bootfile, PREFIX) == nd->nd_bootfile) 178 (void)memmove(nd->nd_bootfile, 179 nd->nd_bootfile + (sizeof(PREFIX) - 1), 180 sizeof(nd->nd_bootfile) - sizeof(PREFIX)); 181 #undef PREFIX 182 183 printf("tftproot: bootfile=%s\n", nd->nd_bootfile); 184 185 memset(&trh, 0, sizeof(trh)); 186 trh.trh_nd = nd; 187 trh.trh_block = 1; 188 189 if ((error = tftproot_getfile(&trh, l)) != 0) { 190 DPRINTF(("%s():%d tftproot_getfile returned %d\n", 191 __func__, __LINE__, error)); 192 goto out; 193 } 194 195 error = 0; 196 197 out: 198 if (nd) 199 kmem_free(nd, sizeof(*nd)); 200 201 return error; 202 } 203 204 static int 205 tftproot_getfile(struct tftproot_handle *trh, struct lwp *l) 206 { 207 struct socket *so = NULL; 208 struct mbuf *m_serv = NULL; 209 struct mbuf *m_outbuf = NULL; 210 struct sockaddr_in sin; 211 struct tftphdr *tftp; 212 size_t packetlen, namelen; 213 int error = -1; 214 const char octetstr[] = "octet"; 215 size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data); 216 char *cp; 217 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)); 221 goto out; 222 } 223 224 /* 225 * Set timeout 226 */ 227 if ((error = nfs_boot_setrecvtimo(so))) { 228 DPRINTF(("%s():%d SO_RCVTIMEO failed %d\n", 229 __func__, __LINE__, error)); 230 goto out; 231 } 232 233 /* 234 * Set server address and port 235 */ 236 memcpy(&sin, &trh->trh_nd->nd_root.ndm_saddr, sizeof(sin)); 237 sin.sin_port = htons(IPPORT_TFTP); 238 239 /* 240 * Set send buffer, prepare the TFTP packet 241 */ 242 namelen = strlen(trh->trh_nd->nd_bootfile) + 1; 243 packetlen = sizeof(tftp->th_opcode) + namelen + sizeof(octetstr); 244 if (packetlen > MSIZE) { 245 DPRINTF(("%s():%d boot filename too long (%ld bytes)\n", 246 __func__, __LINE__, (long)namelen)); 247 goto out; 248 } 249 250 m_outbuf = m_gethdr(M_WAIT, MT_DATA); 251 m_clget(m_outbuf, M_WAIT); 252 m_outbuf->m_len = packetlen; 253 m_outbuf->m_pkthdr.len = packetlen; 254 m_outbuf->m_pkthdr.rcvif = NULL; 255 256 tftp = mtod(m_outbuf, struct tftphdr *); 257 memset(tftp, 0, packetlen); 258 259 tftp->th_opcode = htons((short)RRQ); 260 cp = tftp->th_stuff; 261 (void)strncpy(cp, trh->trh_nd->nd_bootfile, namelen); 262 cp += namelen; 263 (void)strncpy(cp, octetstr, sizeof(octetstr)); 264 265 /* 266 * Perform the file transfer 267 */ 268 printf("tftproot: download %s:%s ", 269 inet_ntoa(sin.sin_addr), trh->trh_nd->nd_bootfile); 270 271 do { 272 /* 273 * Show progress for every 200 blocks (100kB) 274 */ 275 #ifndef TFTPROOT_PROGRESS 276 #define TFTPROOT_PROGRESS 200 277 #endif 278 if ((trh->trh_block % TFTPROOT_PROGRESS) == 0) 279 twiddle(); 280 281 /* 282 * Send the packet and receive the answer. 283 * We get the sender address here, which should be 284 * the same server with a different port 285 */ 286 if ((error = nfs_boot_sendrecv(so, &sin, NULL, m_outbuf, 287 tftproot_recv, NULL, &m_serv, trh, l)) != 0) { 288 DPRINTF(("%s():%d sendrecv failed %d\n", 289 __func__, __LINE__, error)); 290 goto out; 291 } 292 293 /* 294 * Accomodate the packet length for acks. 295 * This is really needed only on first pass 296 */ 297 m_outbuf->m_len = hdrlen; 298 m_outbuf->m_pkthdr.len = hdrlen; 299 tftp->th_opcode = htons((short)ACK); 300 tftp->th_block = htons(trh->trh_block); 301 302 303 /* 304 * Check for termination 305 */ 306 if (trh->trh_flags & TRH_FINISHED) 307 break; 308 309 trh->trh_block++; 310 } while (1/* CONSTCOND */); 311 312 printf("\n"); 313 314 /* 315 * Ack the last block. so_send frees m_outbuf, therefore 316 * we do not want to free it ourselves. 317 * Ignore errors, as we already have the whole file. 318 */ 319 if ((error = (*so->so_send)(so, mtod(m_serv, struct sockaddr *), NULL, 320 m_outbuf, NULL, 0, l)) != 0) 321 DPRINTF(("%s():%d tftproot: sosend returned %d\n", 322 __func__, __LINE__, error)); 323 else 324 m_outbuf = NULL; 325 326 /* 327 * And use it as the root ramdisk. 328 */ 329 DPRINTF(("%s():%d RAMdisk loaded: %ld@%p\n", 330 __func__, __LINE__, trh->trh_len, trh->trh_base)); 331 md_root_setconf(trh->trh_base, trh->trh_len); 332 mdattach(0); 333 334 error = 0; 335 out: 336 if (m_serv) 337 m_freem(m_serv); 338 339 if (m_outbuf) 340 m_freem(m_outbuf); 341 342 if (so) 343 soclose(so); 344 345 return error; 346 } 347 348 static int 349 tftproot_recv(struct mbuf **mp, void *ctx) 350 { 351 struct tftproot_handle *trh = ctx; 352 struct tftphdr *tftp; 353 struct mbuf *m = *mp; 354 size_t newlen; 355 size_t hdrlen = sizeof(*tftp) - sizeof(tftp->th_data); 356 357 /* 358 * Check for short packet 359 */ 360 if (m->m_pkthdr.len < hdrlen) { 361 DPRINTF(("%s():%d short reply (%d bytes)\n", 362 __func__, __LINE__, m->m_pkthdr.len)); 363 return -1; 364 } 365 366 /* 367 * Check for packet too large for being a TFTP packet 368 */ 369 if (m->m_pkthdr.len > hdrlen + SEGSIZE) { 370 DPRINTF(("%s():%d packet too big (%d bytes)\n", 371 __func__, __LINE__, m->m_pkthdr.len)); 372 return -1; 373 } 374 375 376 /* 377 * Examine the TFTP header 378 */ 379 if (m->m_len > sizeof(*tftp)) { 380 if ((m = *mp = m_pullup(m, sizeof(*tftp))) == NULL) { 381 DPRINTF(("%s():%d m_pullup failed\n", 382 __func__, __LINE__)); 383 return -1; 384 } 385 } 386 tftp = mtod(m, struct tftphdr *); 387 388 /* 389 * We handle data or error 390 */ 391 switch(ntohs(tftp->th_opcode)) { 392 case DATA: 393 break; 394 395 case ERROR: { 396 char errbuf[SEGSIZE + 1]; 397 int i; 398 399 for (i = 0; i < SEGSIZE; i++) { 400 if ((tftp->th_data[i] < ' ') || 401 (tftp->th_data[i] > '~')) { 402 errbuf[i] = '\0'; 403 break; 404 } 405 errbuf[i] = tftp->th_data[i]; 406 } 407 errbuf[SEGSIZE] = '\0'; 408 409 printf("tftproot: TFTP server returned error %d, %s\n", 410 ntohs(tftp->th_code), errbuf); 411 412 return -1; 413 break; 414 } 415 416 default: 417 DPRINTF(("%s():%d unexpected tftp reply opcode %d\n", 418 __func__, __LINE__, ntohs(tftp->th_opcode))); 419 return -1; 420 break; 421 } 422 423 /* 424 * Check for last packet, which does not fill the whole space 425 */ 426 if (m->m_pkthdr.len < hdrlen + SEGSIZE) { 427 DPRINTF(("%s():%d last chunk (%d bytes)\n", 428 __func__, __LINE__, m->m_pkthdr.len)); 429 trh->trh_flags |= TRH_FINISHED; 430 } 431 432 433 if (ntohs(tftp->th_block) != trh->trh_block) { 434 DPRINTF(("%s():%d expected block %d, got block %d\n", 435 __func__, __LINE__, trh->trh_block, ntohs(tftp->th_block))); 436 return -1; 437 } 438 439 /* 440 * Grow the receiving buffer to accommodate new data 441 */ 442 newlen = trh->trh_len + (m->m_pkthdr.len - hdrlen); 443 if ((trh->trh_base = realloc(trh->trh_base, 444 newlen, M_TEMP, M_WAITOK)) == NULL) { 445 DPRINTF(("%s():%d failed to realloc %ld bytes\n", 446 __func__, __LINE__, (long)newlen)); 447 return -1; 448 } 449 450 /* 451 * Copy the data 452 */ 453 m_copydata(m, hdrlen, m->m_pkthdr.len - hdrlen, 454 (char *)trh->trh_base + trh->trh_len); 455 trh->trh_len = newlen; 456 457 return 0; 458 } 459 460