2 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/tftp.h>
35 #include <arpa/inet.h>
48 #include "tftp-file.h"
50 #include "tftp-utils.h"
51 #include "tftp-options.h"
53 struct sockaddr_storage peer_sock;
54 struct sockaddr_storage me_sock;
56 static int send_packet(int peer, uint16_t block, char *pkt, int size);
58 static struct errmsg {
62 { EUNDEF, "Undefined error code" },
63 { ENOTFOUND, "File not found" },
64 { EACCESS, "Access violation" },
65 { ENOSPACE, "Disk full or allocation exceeded" },
66 { EBADOP, "Illegal TFTP operation" },
67 { EBADID, "Unknown transfer ID" },
68 { EEXISTS, "File already exists" },
69 { ENOUSER, "No such user" },
70 { EOPTNEG, "Option negotiation" },
74 #define DROPPACKET(s) \
75 if (packetdroppercentage != 0 && \
76 random()%100 < packetdroppercentage) { \
77 tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s); \
80 #define DROPPACKETn(s,n) \
81 if (packetdroppercentage != 0 && \
82 random()%100 < packetdroppercentage) { \
83 tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s); \
95 for (pe = errmsgs; pe->e_code >= 0; pe++)
96 if (pe->e_code == error)
98 snprintf(ebuf, sizeof(ebuf), "error %d", error);
103 send_packet(int peer, uint16_t block, char *pkt, int size)
108 for (i = 0; i < 12 ; i++) {
109 DROPPACKETn("send_packet", 0);
111 if (sendto(peer, pkt, size, 0, (struct sockaddr *)&peer_sock,
112 peer_sock.ss_len) == size) {
115 "%s block %d, attempt %d successful",
116 packettype(ntohs(((struct tftphdr *)
117 (pkt))->th_opcode)), block, i);
121 "%s block %d, attempt %d failed (Error %d: %s)",
122 packettype(ntohs(((struct tftphdr *)(pkt))->th_opcode)),
123 block, i, errno, strerror(errno));
128 tftp_log(LOG_ERR, "send_packet: %s", strerror(errno));
133 * Send an ERROR packet (error message).
134 * Error code passed in is one of the
135 * standard TFTP codes, or a UNIX errno
139 send_error(int peer, int error)
144 char buf[MAXPKTSIZE];
146 if (debug&DEBUG_PACKETS)
147 tftp_log(LOG_DEBUG, "Sending ERROR %d", error);
149 DROPPACKET("send_error");
151 tp = (struct tftphdr *)buf;
152 tp->th_opcode = htons((u_short)ERROR);
153 tp->th_code = htons((u_short)error);
154 for (pe = errmsgs; pe->e_code >= 0; pe++)
155 if (pe->e_code == error)
157 if (pe->e_code < 0) {
158 pe->e_msg = strerror(error - 100);
159 tp->th_code = EUNDEF; /* set 'undef' errorcode */
161 strcpy(tp->th_msg, pe->e_msg);
162 length = strlen(pe->e_msg);
163 tp->th_msg[length] = '\0';
166 if (debug&DEBUG_PACKETS)
167 tftp_log(LOG_DEBUG, "Sending ERROR %d: %s", error, tp->th_msg);
169 if (sendto(peer, buf, length, 0,
170 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != length)
171 tftp_log(LOG_ERR, "send_error: %s", strerror(errno));
175 * Send an WRQ packet (write request).
178 send_wrq(int peer, char *filename, char *mode)
183 char buf[MAXPKTSIZE];
186 if (debug&DEBUG_PACKETS)
187 tftp_log(LOG_DEBUG, "Sending WRQ: filename: '%s', mode '%s'",
191 DROPPACKETn("send_wrq", 1);
193 tp = (struct tftphdr *)buf;
194 tp->th_opcode = htons((u_short)WRQ);
195 size = offsetof(struct tftphdr, th_stuff);
198 strlcpy(bp, filename, sizeof(buf) - size);
199 bp += strlen(filename);
202 size += strlen(filename) + 1;
204 strlcpy(bp, mode, sizeof(buf) - size);
208 size += strlen(mode) + 1;
210 if (options_rfc_enabled)
211 size += make_options(peer, bp, sizeof(buf) - size);
213 n = sendto(peer, buf, size, 0,
214 (struct sockaddr *)&peer_sock, peer_sock.ss_len);
216 tftp_log(LOG_ERR, "send_wrq: %s", strerror(errno));
223 * Send an RRQ packet (write request).
226 send_rrq(int peer, char *filename, char *mode)
231 char buf[MAXPKTSIZE];
234 if (debug&DEBUG_PACKETS)
235 tftp_log(LOG_DEBUG, "Sending RRQ: filename: '%s', mode '%s'",
239 DROPPACKETn("send_rrq", 1);
241 tp = (struct tftphdr *)buf;
242 tp->th_opcode = htons((u_short)RRQ);
243 size = offsetof(struct tftphdr, th_stuff);
246 strlcpy(bp, filename, sizeof(buf) - size);
247 bp += strlen(filename);
250 size += strlen(filename) + 1;
252 strlcpy(bp, mode, sizeof(buf) - size);
256 size += strlen(mode) + 1;
258 if (options_rfc_enabled) {
259 options[OPT_TSIZE].o_request = strdup("0");
260 size += make_options(peer, bp, sizeof(buf) - size);
263 n = sendto(peer, buf, size, 0,
264 (struct sockaddr *)&peer_sock, peer_sock.ss_len);
266 tftp_log(LOG_ERR, "send_rrq: %d %s", n, strerror(errno));
273 * Send an OACK packet (option acknowledgement).
281 char buf[MAXPKTSIZE];
283 if (debug&DEBUG_PACKETS)
284 tftp_log(LOG_DEBUG, "Sending OACK");
286 DROPPACKETn("send_oack", 0);
289 * Send back an options acknowledgement (only the ones with
292 tp = (struct tftphdr *)buf;
294 size = sizeof(buf) - 2;
295 tp->th_opcode = htons((u_short)OACK);
296 for (i = 0; options[i].o_type != NULL; i++) {
297 if (options[i].o_reply != NULL) {
298 n = snprintf(bp, size, "%s%c%s", options[i].o_type,
299 0, options[i].o_reply);
303 tftp_log(LOG_ERR, "oack: buffer overflow");
310 if (sendto(peer, buf, size, 0,
311 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != size) {
312 tftp_log(LOG_INFO, "send_oack: %s", strerror(errno));
320 * Send an ACK packet (acknowledgement).
323 send_ack(int fp, uint16_t block)
327 char buf[MAXPKTSIZE];
329 if (debug&DEBUG_PACKETS)
330 tftp_log(LOG_DEBUG, "Sending ACK for block %d", block);
332 DROPPACKETn("send_ack", 0);
334 tp = (struct tftphdr *)buf;
335 size = sizeof(buf) - 2;
336 tp->th_opcode = htons((u_short)ACK);
337 tp->th_block = htons((u_short)block);
340 if (sendto(fp, buf, size, 0,
341 (struct sockaddr *)&peer_sock, peer_sock.ss_len) != size) {
342 tftp_log(LOG_INFO, "send_ack: %s", strerror(errno));
353 send_data(int peer, uint16_t block, char *data, int size)
355 char buf[MAXPKTSIZE];
359 if (debug&DEBUG_PACKETS)
360 tftp_log(LOG_DEBUG, "Sending DATA packet %d of %d bytes",
363 DROPPACKETn("send_data", 0);
365 pkt = (struct tftphdr *)buf;
367 pkt->th_opcode = htons((u_short)DATA);
368 pkt->th_block = htons((u_short)block);
369 memcpy(pkt->th_data, data, size);
371 n = send_packet(peer, block, (char *)pkt, size + 4);
379 static jmp_buf timeoutbuf;
382 timeout(int sig __unused)
385 /* tftp_log(LOG_DEBUG, "Timeout\n"); Inside a signal handler... */
386 longjmp(timeoutbuf, 1);
390 receive_packet(int peer, char *data, int size, struct sockaddr_storage *from,
394 struct sockaddr_storage from_local;
395 struct sockaddr_storage *pfrom;
398 static int timed_out;
400 if (debug&DEBUG_PACKETS)
402 "Waiting %d seconds for packet", timeoutpacket);
404 pkt = (struct tftphdr *)data;
406 signal(SIGALRM, timeout);
407 timed_out = setjmp(timeoutbuf);
410 if (timed_out != 0) {
411 tftp_log(LOG_ERR, "receive_packet: timeout");
416 pfrom = (from == NULL) ? &from_local : from;
417 fromlen = sizeof(*pfrom);
418 n = recvfrom(peer, data, size, 0, (struct sockaddr *)pfrom, &fromlen);
422 DROPPACKETn("receive_packet", RP_TIMEOUT);
425 tftp_log(LOG_ERR, "receive_packet: timeout");
430 /* No idea what could have happened if it isn't a timeout */
431 tftp_log(LOG_ERR, "receive_packet: %s", strerror(errno));
432 return (RP_RECVFROM);
436 "receive_packet: packet too small (%d bytes)", n);
437 return (RP_TOOSMALL);
440 pkt->th_opcode = ntohs((u_short)pkt->th_opcode);
441 if (pkt->th_opcode == DATA ||
442 pkt->th_opcode == ACK)
443 pkt->th_block = ntohs((u_short)pkt->th_block);
445 if (pkt->th_opcode == DATA && n > pktsize) {
446 tftp_log(LOG_ERR, "receive_packet: packet too big");
450 if (((struct sockaddr_in *)(pfrom))->sin_addr.s_addr !=
451 ((struct sockaddr_in *)(&peer_sock))->sin_addr.s_addr) {
453 "receive_packet: received packet from wrong source");
454 return (RP_WRONGSOURCE);
457 if (pkt->th_opcode == ERROR) {
458 tftp_log(pkt->th_code == EUNDEF ? LOG_DEBUG : LOG_ERR,
459 "Got ERROR packet: %s", pkt->th_msg);
463 if (debug&DEBUG_PACKETS)
464 tftp_log(LOG_DEBUG, "Received %d bytes in a %s packet",
465 n, packettype(pkt->th_opcode));