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