xref: /freebsd-13-stable/libexec/tftpd/tftp-transfer.c (revision 3d497e17ebd33fe0f58d773e35ab994d750258d6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2008 Edwin Groothuis. 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  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include <sys/socket.h>
34 
35 #include <netinet/in.h>
36 #include <arpa/tftp.h>
37 
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 
44 #include "tftp-file.h"
45 #include "tftp-io.h"
46 #include "tftp-utils.h"
47 #include "tftp-options.h"
48 #include "tftp-transfer.h"
49 
50 struct block_data {
51 	off_t offset;
52 	uint16_t block;
53 	int size;
54 };
55 
56 /*
57  * Send a file via the TFTP data session.
58  */
59 void
tftp_send(int peer,uint16_t * block,struct tftp_stats * ts)60 tftp_send(int peer, uint16_t *block, struct tftp_stats *ts)
61 {
62 	struct tftphdr *rp;
63 	int size, n_data, n_ack, sendtry, acktry;
64 	u_int i, j;
65 	uint16_t oldblock, windowblock;
66 	char sendbuffer[MAXPKTSIZE];
67 	char recvbuffer[MAXPKTSIZE];
68 	struct block_data window[WINDOWSIZE_MAX];
69 
70 	rp = (struct tftphdr *)recvbuffer;
71 	*block = 1;
72 	ts->amount = 0;
73 	windowblock = 0;
74 	acktry = 0;
75 	do {
76 read_block:
77 		if (debug&DEBUG_SIMPLE)
78 			tftp_log(LOG_DEBUG, "Sending block %d (window block %d)",
79 			    *block, windowblock);
80 
81 		window[windowblock].offset = tell_file();
82 		window[windowblock].block = *block;
83 		size = read_file(sendbuffer, segsize);
84 		if (size < 0) {
85 			tftp_log(LOG_ERR, "read_file returned %d", size);
86 			send_error(peer, errno + 100);
87 			goto abort;
88 		}
89 		window[windowblock].size = size;
90 		windowblock++;
91 
92 		for (sendtry = 0; ; sendtry++) {
93 			n_data = send_data(peer, *block, sendbuffer, size);
94 			if (n_data == 0)
95 				break;
96 
97 			if (sendtry == maxtimeouts) {
98 				tftp_log(LOG_ERR,
99 				    "Cannot send DATA packet #%d, "
100 				    "giving up", *block);
101 				return;
102 			}
103 			tftp_log(LOG_ERR,
104 			    "Cannot send DATA packet #%d, trying again",
105 			    *block);
106 		}
107 
108 		/* Only check for ACK for last block in window. */
109 		if (windowblock == windowsize || size != segsize) {
110 			n_ack = receive_packet(peer, recvbuffer,
111 			    MAXPKTSIZE, NULL, timeoutpacket);
112 			if (n_ack < 0) {
113 				if (n_ack == RP_TIMEOUT) {
114 					if (acktry == maxtimeouts) {
115 						tftp_log(LOG_ERR,
116 						    "Timeout #%d send ACK %d "
117 						    "giving up", acktry, *block);
118 						return;
119 					}
120 					tftp_log(LOG_WARNING,
121 					    "Timeout #%d on ACK %d",
122 					    acktry, *block);
123 
124 					acktry++;
125 					ts->retries++;
126 					if (seek_file(window[0].offset) != 0) {
127 						tftp_log(LOG_ERR,
128 						    "seek_file failed: %s",
129 						    strerror(errno));
130 						send_error(peer, errno + 100);
131 						goto abort;
132 					}
133 					*block = window[0].block;
134 					windowblock = 0;
135 					goto read_block;
136 				}
137 
138 				/* Either read failure or ERROR packet */
139 				if (debug&DEBUG_SIMPLE)
140 					tftp_log(LOG_ERR, "Aborting: %s",
141 					    rp_strerror(n_ack));
142 				goto abort;
143 			}
144 			if (rp->th_opcode == ACK) {
145 				/*
146 				 * Look for the ACKed block in our open
147 				 * window.
148 				 */
149 				for (i = 0; i < windowblock; i++) {
150 					if (rp->th_block == window[i].block)
151 						break;
152 				}
153 
154 				if (i == windowblock) {
155 					/* Did not recognize ACK. */
156 					if (debug&DEBUG_SIMPLE)
157 						tftp_log(LOG_DEBUG,
158 						    "ACK %d out of window",
159 						    rp->th_block);
160 
161 					/* Re-synchronize with the other side */
162 					(void) synchnet(peer);
163 
164 					/* Resend the current window. */
165 					ts->retries++;
166 					if (seek_file(window[0].offset) != 0) {
167 						tftp_log(LOG_ERR,
168 						    "seek_file failed: %s",
169 						    strerror(errno));
170 						send_error(peer, errno + 100);
171 						goto abort;
172 					}
173 					*block = window[0].block;
174 					windowblock = 0;
175 					goto read_block;
176 				}
177 
178 				/* ACKed at least some data. */
179 				acktry = 0;
180 				for (j = 0; j <= i; j++) {
181 					if (debug&DEBUG_SIMPLE)
182 						tftp_log(LOG_DEBUG,
183 						    "ACKed block %d",
184 						    window[j].block);
185 					ts->blocks++;
186 					ts->amount += window[j].size;
187 				}
188 
189 				/*
190 				 * Partial ACK.  Rewind state to first
191 				 * un-ACKed block.
192 				 */
193 				if (i + 1 != windowblock) {
194 					if (debug&DEBUG_SIMPLE)
195 						tftp_log(LOG_DEBUG,
196 						    "Partial ACK");
197 					if (seek_file(window[i + 1].offset) !=
198 					    0) {
199 						tftp_log(LOG_ERR,
200 						    "seek_file failed: %s",
201 						    strerror(errno));
202 						send_error(peer, errno + 100);
203 						goto abort;
204 					}
205 					*block = window[i + 1].block;
206 					windowblock = 0;
207 					ts->retries++;
208 					goto read_block;
209 				}
210 
211 				windowblock = 0;
212 			}
213 
214 		}
215 		oldblock = *block;
216 		(*block)++;
217 		if (oldblock > *block) {
218 			if (options[OPT_ROLLOVER].o_request == NULL) {
219 				/*
220 				 * "rollover" option not specified in
221 				 * tftp client.  Default to rolling block
222 				 * counter to 0.
223 				 */
224 				*block = 0;
225 			} else {
226 				*block = atoi(options[OPT_ROLLOVER].o_request);
227 			}
228 
229 			ts->rollovers++;
230 		}
231 		gettimeofday(&(ts->tstop), NULL);
232 	} while (size == segsize);
233 abort:
234 	return;
235 }
236 
237 /*
238  * Receive a file via the TFTP data session.
239  *
240  * - It could be that the first block has already arrived while
241  *   trying to figure out if we were receiving options or not. In
242  *   that case it is passed to this function.
243  */
244 void
tftp_receive(int peer,uint16_t * block,struct tftp_stats * ts,struct tftphdr * firstblock,size_t fb_size)245 tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts,
246     struct tftphdr *firstblock, size_t fb_size)
247 {
248 	struct tftphdr *rp;
249 	uint16_t oldblock, windowstart;
250 	int n_data, n_ack, writesize, i, retry, windowblock;
251 	char recvbuffer[MAXPKTSIZE];
252 
253 	ts->amount = 0;
254 	windowblock = 0;
255 
256 	if (firstblock != NULL) {
257 		writesize = write_file(firstblock->th_data, fb_size);
258 		ts->amount += writesize;
259 		ts->blocks++;
260 		windowblock++;
261 		if (windowsize == 1 || fb_size != segsize) {
262 			for (i = 0; ; i++) {
263 				n_ack = send_ack(peer, *block);
264 				if (n_ack > 0) {
265 					if (i == maxtimeouts) {
266 						tftp_log(LOG_ERR,
267 						    "Cannot send ACK packet #%d, "
268 						    "giving up", *block);
269 						return;
270 					}
271 					tftp_log(LOG_ERR,
272 					    "Cannot send ACK packet #%d, trying again",
273 					    *block);
274 					continue;
275 				}
276 
277 				break;
278 			}
279 		}
280 
281 		if (fb_size != segsize) {
282 			write_close();
283 			gettimeofday(&(ts->tstop), NULL);
284 			return;
285 		}
286 	}
287 
288 	rp = (struct tftphdr *)recvbuffer;
289 	do {
290 		oldblock = *block;
291 		(*block)++;
292 		if (oldblock > *block) {
293 			if (options[OPT_ROLLOVER].o_request == NULL) {
294 				/*
295 				 * "rollover" option not specified in
296 				 * tftp client.  Default to rolling block
297 				 * counter to 0.
298 				 */
299 				*block = 0;
300 			} else {
301 				*block = atoi(options[OPT_ROLLOVER].o_request);
302 			}
303 
304 			ts->rollovers++;
305 		}
306 
307 		for (retry = 0; ; retry++) {
308 			if (debug&DEBUG_SIMPLE)
309 				tftp_log(LOG_DEBUG,
310 				    "Receiving DATA block %d (window block %d)",
311 				    *block, windowblock);
312 
313 			n_data = receive_packet(peer, recvbuffer,
314 			    MAXPKTSIZE, NULL, timeoutpacket);
315 			if (n_data < 0) {
316 				if (retry == maxtimeouts) {
317 					tftp_log(LOG_ERR,
318 					    "Timeout #%d on DATA block %d, "
319 					    "giving up", retry, *block);
320 					return;
321 				}
322 				if (n_data == RP_TIMEOUT) {
323 					tftp_log(LOG_WARNING,
324 					    "Timeout #%d on DATA block %d",
325 					    retry, *block);
326 					send_ack(peer, oldblock);
327 					windowblock = 0;
328 					continue;
329 				}
330 
331 				/* Either read failure or ERROR packet */
332 				if (debug&DEBUG_SIMPLE)
333 					tftp_log(LOG_DEBUG, "Aborting: %s",
334 					    rp_strerror(n_data));
335 				goto abort;
336 			}
337 			if (rp->th_opcode == DATA) {
338 				ts->blocks++;
339 
340 				if (rp->th_block == *block)
341 					break;
342 
343 				/*
344 				 * Ignore duplicate blocks within the
345 				 * window.
346 				 *
347 				 * This does not handle duplicate
348 				 * blocks during a rollover as
349 				 * gracefully, but that should still
350 				 * recover eventually.
351 				 */
352 				if (*block > windowsize)
353 					windowstart = *block - windowsize;
354 				else
355 					windowstart = 0;
356 				if (rp->th_block > windowstart &&
357 				    rp->th_block < *block) {
358 					if (debug&DEBUG_SIMPLE)
359 						tftp_log(LOG_DEBUG,
360 					    "Ignoring duplicate DATA block %d",
361 						    rp->th_block);
362 					windowblock++;
363 					retry = 0;
364 					continue;
365 				}
366 
367 				tftp_log(LOG_WARNING,
368 				    "Expected DATA block %d, got block %d",
369 				    *block, rp->th_block);
370 
371 				/* Re-synchronize with the other side */
372 				(void) synchnet(peer);
373 
374 				tftp_log(LOG_INFO, "Trying to sync");
375 				*block = oldblock;
376 				ts->retries++;
377 				goto send_ack;	/* rexmit */
378 
379 			} else {
380 				tftp_log(LOG_WARNING,
381 				    "Expected DATA block, got %s block",
382 				    packettype(rp->th_opcode));
383 			}
384 		}
385 
386 		if (n_data > 0) {
387 			writesize = write_file(rp->th_data, n_data);
388 			ts->amount += writesize;
389 			if (writesize <= 0) {
390 				tftp_log(LOG_ERR,
391 				    "write_file returned %d", writesize);
392 				if (writesize < 0)
393 					send_error(peer, errno + 100);
394 				else
395 					send_error(peer, ENOSPACE);
396 				goto abort;
397 			}
398 		}
399 		if (n_data != segsize)
400 			write_close();
401 		windowblock++;
402 
403 		/* Only send ACKs for the last block in the window. */
404 		if (windowblock < windowsize && n_data == segsize)
405 			continue;
406 send_ack:
407 		for (i = 0; ; i++) {
408 			n_ack = send_ack(peer, *block);
409 			if (n_ack > 0) {
410 
411 				if (i == maxtimeouts) {
412 					tftp_log(LOG_ERR,
413 					    "Cannot send ACK packet #%d, "
414 					    "giving up", *block);
415 					return;
416 				}
417 
418 				tftp_log(LOG_ERR,
419 				    "Cannot send ACK packet #%d, trying again",
420 				    *block);
421 				continue;
422 			}
423 
424 			if (debug&DEBUG_SIMPLE)
425 				tftp_log(LOG_DEBUG, "Sent ACK for %d", *block);
426 			windowblock = 0;
427 			break;
428 		}
429 		gettimeofday(&(ts->tstop), NULL);
430 	} while (n_data == segsize);
431 
432 	/* Don't do late packet management for the client implementation */
433 	if (acting_as_client)
434 		return;
435 
436 	for (i = 0; ; i++) {
437 		n_data = receive_packet(peer, (char *)rp, pktsize,
438 		    NULL, timeoutpacket);
439 		if (n_data <= 0)
440 			break;
441 		if (n_data > 0 &&
442 		    rp->th_opcode == DATA &&	/* and got a data block */
443 		    *block == rp->th_block)	/* then my last ack was lost */
444 			send_ack(peer, *block);	/* resend final ack */
445 	}
446 
447 abort:
448 	return;
449 }
450