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