2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/tftp.h>
37 #include <arpa/inet.h>
50 #include "tftp-file.h"
52 #include "tftp-utils.h"
53 #include "tftp-options.h"
55 struct sockaddr_storage peer_sock;
56 struct sockaddr_storage me_sock;
58 static int send_packet(int peer, uint16_t block, char *pkt, int size);
60 static struct errmsg {
64 { EUNDEF, "Undefined error code" },
65 { ENOTFOUND, "File not found" },
66 { EACCESS, "Access violation" },
67 { ENOSPACE, "Disk full or allocation exceeded" },
68 { EBADOP, "Illegal TFTP operation" },
69 { EBADID, "Unknown transfer ID" },
70 { EEXISTS, "File already exists" },
71 { ENOUSER, "No such user" },
72 { EOPTNEG, "Option negotiation" },
76 #define DROPPACKET(s) \
77 if (packetdroppercentage != 0 && \
78 random()%100 < packetdroppercentage) { \
79 tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s); \
82 #define DROPPACKETn(s,n) \
83 if (packetdroppercentage != 0 && \
84 random()%100 < packetdroppercentage) { \
85 tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s); \
97 for (pe = errmsgs; pe->e_code >= 0; pe++)
98 if (pe->e_code == error)
100 snprintf(ebuf, sizeof(ebuf), "error %d", error);
105 send_packet(int peer, uint16_t block, char *pkt, int size)
110 for (i = 0; i < 12 ; i++) {
111 DROPPACKETn("send_packet", 0);
113 if (sendto(peer, pkt, size, 0, (struct sockaddr *)&peer_sock,
114 peer_sock.ss_len) == size) {
117 "%s block %d, attempt %d successful",
118 packettype(ntohs(((struct tftphdr *)
119 (pkt))->th_opcode)), block, i);
123 "%s block %d, attempt %d failed (Error %d: %s)",
124 packettype(ntohs(((struct tftphdr *)(pkt))->th_opcode)),
125 block, i, errno, strerror(errno));
130 tftp_log(LOG_ERR, "send_packet: %s", strerror(errno));
135 * Send an ERROR packet (error message).
136 * Error code passed in is one of the
137 * standard TFTP codes, or a UNIX errno
141 send_error(int peer, int error)
146 char buf[MAXPKTSIZE];
148 if (debug&DEBUG_PACKETS)
149 tftp_log(LOG_DEBUG, "Sending ERROR %d", error);
151 DROPPACKET("send_error");
153 tp = (struct tftphdr *)buf;
154 tp->th_opcode = htons((u_short)ERROR);
155 tp->th_code = htons((u_short)error);
156 for (pe = errmsgs; pe->e_code >= 0; pe++)
157 if (pe->e_code == error)
159 if (pe->e_code < 0) {
160 pe->e_msg = strerror(error - 100);
161 tp->th_code = EUNDEF; /* set 'undef' errorcode */
163 strcpy(tp->th_msg, pe->e_msg);
164 length = strlen(pe->e_msg);
165 tp->th_msg[length] = '\0';
168 if (debug&DEBUG_PACKETS)
169 tftp_log(LOG_DEBUG, "Sending ERROR %d: %s", error, tp->th_msg);
171 if (sendto(peer, buf, length, 0,
172 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != length)
173 tftp_log(LOG_ERR, "send_error: %s", strerror(errno));
177 * Send an WRQ packet (write request).
180 send_wrq(int peer, char *filename, char *mode)
185 char buf[MAXPKTSIZE];
188 if (debug&DEBUG_PACKETS)
189 tftp_log(LOG_DEBUG, "Sending WRQ: filename: '%s', mode '%s'",
193 DROPPACKETn("send_wrq", 0);
195 tp = (struct tftphdr *)buf;
196 tp->th_opcode = htons((u_short)WRQ);
197 size = offsetof(struct tftphdr, th_stuff);
200 strlcpy(bp, filename, sizeof(buf) - size);
201 bp += strlen(filename);
204 size += strlen(filename) + 1;
206 strlcpy(bp, mode, sizeof(buf) - size);
210 size += strlen(mode) + 1;
212 if (options_rfc_enabled)
213 size += make_options(peer, bp, sizeof(buf) - size);
215 n = sendto(peer, buf, size, 0,
216 (struct sockaddr *)&peer_sock, peer_sock.ss_len);
218 tftp_log(LOG_ERR, "send_wrq: %s", strerror(errno));
225 * Send an RRQ packet (write request).
228 send_rrq(int peer, char *filename, char *mode)
233 char buf[MAXPKTSIZE];
236 if (debug&DEBUG_PACKETS)
237 tftp_log(LOG_DEBUG, "Sending RRQ: filename: '%s', mode '%s'",
241 DROPPACKETn("send_rrq", 0);
243 tp = (struct tftphdr *)buf;
244 tp->th_opcode = htons((u_short)RRQ);
245 size = offsetof(struct tftphdr, th_stuff);
248 strlcpy(bp, filename, sizeof(buf) - size);
249 bp += strlen(filename);
252 size += strlen(filename) + 1;
254 strlcpy(bp, mode, sizeof(buf) - size);
258 size += strlen(mode) + 1;
260 if (options_rfc_enabled) {
261 options[OPT_TSIZE].o_request = strdup("0");
262 size += make_options(peer, bp, sizeof(buf) - size);
265 n = sendto(peer, buf, size, 0,
266 (struct sockaddr *)&peer_sock, peer_sock.ss_len);
268 tftp_log(LOG_ERR, "send_rrq: %d %s", n, strerror(errno));
275 * Send an OACK packet (option acknowledgement).
283 char buf[MAXPKTSIZE];
285 if (debug&DEBUG_PACKETS)
286 tftp_log(LOG_DEBUG, "Sending OACK");
288 DROPPACKETn("send_oack", 0);
291 * Send back an options acknowledgement (only the ones with
294 tp = (struct tftphdr *)buf;
296 size = sizeof(buf) - 2;
297 tp->th_opcode = htons((u_short)OACK);
298 for (i = 0; options[i].o_type != NULL; i++) {
299 if (options[i].o_reply != NULL) {
300 n = snprintf(bp, size, "%s%c%s", options[i].o_type,
301 0, options[i].o_reply);
305 tftp_log(LOG_ERR, "oack: buffer overflow");
312 if (sendto(peer, buf, size, 0,
313 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != size) {
314 tftp_log(LOG_INFO, "send_oack: %s", strerror(errno));
322 * Send an ACK packet (acknowledgement).
325 send_ack(int fp, uint16_t block)
329 char buf[MAXPKTSIZE];
331 if (debug&DEBUG_PACKETS)
332 tftp_log(LOG_DEBUG, "Sending ACK for block %d", block);
334 DROPPACKETn("send_ack", 0);
336 tp = (struct tftphdr *)buf;
337 size = sizeof(buf) - 2;
338 tp->th_opcode = htons((u_short)ACK);
339 tp->th_block = htons((u_short)block);
342 if (sendto(fp, buf, size, 0,
343 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != size) {
344 tftp_log(LOG_INFO, "send_ack: %s", strerror(errno));
355 send_data(int peer, uint16_t block, char *data, int size)
357 char buf[MAXPKTSIZE];
361 if (debug&DEBUG_PACKETS)
362 tftp_log(LOG_DEBUG, "Sending DATA packet %d of %d bytes",
365 DROPPACKETn("send_data", 0);
367 pkt = (struct tftphdr *)buf;
369 pkt->th_opcode = htons((u_short)DATA);
370 pkt->th_block = htons((u_short)block);
371 memcpy(pkt->th_data, data, size);
373 n = send_packet(peer, block, (char *)pkt, size + 4);
381 static jmp_buf timeoutbuf;
384 timeout(int sig __unused)
387 /* tftp_log(LOG_DEBUG, "Timeout\n"); Inside a signal handler... */
388 longjmp(timeoutbuf, 1);
392 receive_packet(int peer, char *data, int size, struct sockaddr_storage *from,
396 struct sockaddr_storage from_local;
397 struct sockaddr_storage *pfrom;
400 static int timed_out;
402 if (debug&DEBUG_PACKETS)
404 "Waiting %d seconds for packet", timeoutpacket);
406 pkt = (struct tftphdr *)data;
408 signal(SIGALRM, timeout);
409 timed_out = setjmp(timeoutbuf);
412 if (timed_out != 0) {
413 tftp_log(LOG_ERR, "receive_packet: timeout");
418 pfrom = (from == NULL) ? &from_local : from;
419 fromlen = sizeof(*pfrom);
420 n = recvfrom(peer, data, size, 0, (struct sockaddr *)pfrom, &fromlen);
424 DROPPACKETn("receive_packet", RP_TIMEOUT);
427 tftp_log(LOG_ERR, "receive_packet: timeout");
432 /* No idea what could have happened if it isn't a timeout */
433 tftp_log(LOG_ERR, "receive_packet: %s", strerror(errno));
434 return (RP_RECVFROM);
438 "receive_packet: packet too small (%d bytes)", n);
439 return (RP_TOOSMALL);
442 pkt->th_opcode = ntohs((u_short)pkt->th_opcode);
443 if (pkt->th_opcode == DATA ||
444 pkt->th_opcode == ACK)
445 pkt->th_block = ntohs((u_short)pkt->th_block);
447 if (pkt->th_opcode == DATA && n > pktsize) {
448 tftp_log(LOG_ERR, "receive_packet: packet too big");
452 if (((struct sockaddr_in *)(pfrom))->sin_addr.s_addr !=
453 ((struct sockaddr_in *)(&peer_sock))->sin_addr.s_addr) {
455 "receive_packet: received packet from wrong source");
456 return (RP_WRONGSOURCE);
459 if (pkt->th_opcode == ERROR) {
460 tftp_log(pkt->th_code == EUNDEF ? LOG_DEBUG : LOG_ERR,
461 "Got ERROR packet: %s", pkt->th_msg);
465 if (debug&DEBUG_PACKETS)
466 tftp_log(LOG_DEBUG, "Received %d bytes in a %s packet",
467 n, packettype(pkt->th_opcode));