1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
30
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33
34 #include <netinet/in.h>
35 #include <arpa/tftp.h>
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42
43 #include "tftp-utils.h"
44 #include "tftp-io.h"
45
46 /*
47 * Default values, can be changed later via the TFTP Options
48 */
49 int timeoutpacket = TIMEOUT;
50 int timeoutnetwork = MAX_TIMEOUTS * TIMEOUT;
51 int maxtimeouts = MAX_TIMEOUTS;
52 uint16_t segsize = SEGSIZE;
53 uint16_t pktsize = SEGSIZE + 4;
54
55 int acting_as_client;
56
57
58 /*
59 * Set timeout values for packet reception. The idea is that you
60 * get 'maxtimeouts' of 5 seconds between 'timeoutpacket' (i.e. the
61 * first timeout) to 'timeoutnetwork' (i.e. the last timeout)
62 */
63 int
settimeouts(int _timeoutpacket,int _timeoutnetwork,int _maxtimeouts __unused)64 settimeouts(int _timeoutpacket, int _timeoutnetwork, int _maxtimeouts __unused)
65 {
66 int i;
67
68 /* We cannot do impossible things */
69 if (_timeoutpacket >= _timeoutnetwork)
70 return (0);
71
72 maxtimeouts = 0;
73 i = _timeoutpacket;
74 while (i < _timeoutnetwork || maxtimeouts < MIN_TIMEOUTS) {
75 maxtimeouts++;
76 i += 5;
77 }
78
79 timeoutpacket = _timeoutpacket;
80 timeoutnetwork = i;
81 return (1);
82 }
83
84 /* translate IPv4 mapped IPv6 address to IPv4 address */
85 void
unmappedaddr(struct sockaddr_in6 * sin6)86 unmappedaddr(struct sockaddr_in6 *sin6)
87 {
88 struct sockaddr_in *sin4;
89 u_int32_t addr;
90 int port;
91
92 if (sin6->sin6_family != AF_INET6 ||
93 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
94 return;
95 sin4 = (struct sockaddr_in *)sin6;
96 memcpy(&addr, &sin6->sin6_addr.s6_addr[12], sizeof(addr));
97 port = sin6->sin6_port;
98 memset(sin4, 0, sizeof(struct sockaddr_in));
99 sin4->sin_addr.s_addr = addr;
100 sin4->sin_port = port;
101 sin4->sin_family = AF_INET;
102 sin4->sin_len = sizeof(struct sockaddr_in);
103 }
104
105 /* Get a field from a \0 separated string */
106 ssize_t
get_field(int peer,char * buffer,ssize_t size)107 get_field(int peer, char *buffer, ssize_t size)
108 {
109 char *cp = buffer;
110
111 while (cp < buffer + size) {
112 if (*cp == '\0') break;
113 cp++;
114 }
115 if (*cp != '\0') {
116 tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
117 send_error(peer, EBADOP);
118 exit(1);
119 }
120 return (cp - buffer + 1);
121 }
122
123 /*
124 * Logging functions
125 */
126 static int _tftp_logtostdout = 1;
127
128 void
tftp_openlog(const char * ident,int logopt,int facility)129 tftp_openlog(const char *ident, int logopt, int facility)
130 {
131
132 _tftp_logtostdout = (ident == NULL);
133 if (_tftp_logtostdout == 0)
134 openlog(ident, logopt, facility);
135 }
136
137 void
tftp_closelog(void)138 tftp_closelog(void)
139 {
140
141 if (_tftp_logtostdout == 0)
142 closelog();
143 }
144
145 void
tftp_log(int priority,const char * message,...)146 tftp_log(int priority, const char *message, ...)
147 {
148 va_list ap;
149 char *s;
150
151 va_start(ap, message);
152 if (_tftp_logtostdout == 0) {
153 vasprintf(&s, message, ap);
154 syslog(priority, "%s", s);
155 } else {
156 vprintf(message, ap);
157 printf("\n");
158 }
159 va_end(ap);
160 }
161
162 /*
163 * Packet types
164 */
165 struct packettypes packettypes[] = {
166 { RRQ, "RRQ" },
167 { WRQ, "WRQ" },
168 { DATA, "DATA" },
169 { ACK, "ACK" },
170 { ERROR, "ERROR" },
171 { OACK, "OACK" },
172 { 0, NULL },
173 };
174
175 const char *
packettype(int type)176 packettype(int type)
177 {
178 static char failed[100];
179 int i = 0;
180
181 while (packettypes[i].name != NULL) {
182 if (packettypes[i].value == type)
183 break;
184 i++;
185 }
186 if (packettypes[i].name != NULL)
187 return packettypes[i].name;
188 sprintf(failed, "unknown (type: %d)", type);
189 return (failed);
190 }
191
192 /*
193 * Debugs
194 */
195 int debug = DEBUG_NONE;
196 struct debugs debugs[] = {
197 { DEBUG_PACKETS, "packet", "Packet debugging" },
198 { DEBUG_SIMPLE, "simple", "Simple debugging" },
199 { DEBUG_OPTIONS, "options", "Options debugging" },
200 { DEBUG_ACCESS, "access", "TCPd access debugging" },
201 { DEBUG_NONE, NULL, "No debugging" },
202 };
203 int packetdroppercentage = 0;
204
205 int
debug_find(char * s)206 debug_find(char *s)
207 {
208 int i = 0;
209
210 while (debugs[i].name != NULL) {
211 if (strcasecmp(debugs[i].name, s) == 0)
212 break;
213 i++;
214 }
215 return (debugs[i].value);
216 }
217
218 int
debug_finds(char * s)219 debug_finds(char *s)
220 {
221 int i = 0;
222 char *ps = s;
223
224 while (s != NULL) {
225 ps = strchr(s, ' ');
226 if (ps != NULL)
227 *ps = '\0';
228 i += debug_find(s);
229 if (ps != NULL)
230 *ps = ' ';
231 s = ps;
232 }
233 return (i);
234 }
235
236 const char *
debug_show(int d)237 debug_show(int d)
238 {
239 static char s[100];
240 size_t space = sizeof(s);
241 int i = 0;
242
243 s[0] = '\0';
244 while (debugs[i].name != NULL) {
245 if (d&debugs[i].value) {
246 if (s[0] != '\0')
247 strlcat(s, " ", space);
248 strlcat(s, debugs[i].name, space);
249 }
250 i++;
251 }
252 if (s[0] != '\0')
253 return (s);
254 return ("none");
255 }
256
257 /*
258 * RP_
259 */
260 struct rp_errors rp_errors[] = {
261 { RP_TIMEOUT, "Network timeout" },
262 { RP_TOOSMALL, "Not enough data bytes" },
263 { RP_WRONGSOURCE, "Invalid IP address of UDP port" },
264 { RP_ERROR, "Error packet" },
265 { RP_RECVFROM, "recvfrom() complained" },
266 { RP_TOOBIG, "Too many data bytes" },
267 { RP_NONE, NULL }
268 };
269
270 char *
rp_strerror(int error)271 rp_strerror(int error)
272 {
273 static char s[100];
274 size_t space = sizeof(s);
275 int i = 0;
276
277 while (rp_errors[i].desc != NULL) {
278 if (rp_errors[i].error == error) {
279 strlcpy(s, rp_errors[i].desc, space);
280 space -= strlen(rp_errors[i].desc);
281 }
282 i++;
283 }
284 if (s[0] == '\0')
285 sprintf(s, "unknown (error=%d)", error);
286 return (s);
287 }
288
289 /*
290 * Performance figures
291 */
292
293 void
stats_init(struct tftp_stats * ts)294 stats_init(struct tftp_stats *ts)
295 {
296
297 ts->amount = 0;
298 ts->rollovers = 0;
299 ts->retries = 0;
300 ts->blocks = 0;
301 ts->amount = 0;
302 gettimeofday(&(ts->tstart), NULL);
303 }
304
305 void
printstats(const char * direction,int verbose,struct tftp_stats * ts)306 printstats(const char *direction, int verbose, struct tftp_stats *ts)
307 {
308 double delta; /* compute delta in 1/10's second units */
309
310 delta = ((ts->tstop.tv_sec*10.)+(ts->tstop.tv_usec/100000)) -
311 ((ts->tstart.tv_sec*10.)+(ts->tstart.tv_usec/100000));
312 delta = delta/10.; /* back to seconds */
313
314 printf("%s %zu bytes during %.1f seconds in %u blocks",
315 direction, ts->amount, delta, ts->blocks);
316
317 if (ts->rollovers != 0)
318 printf(" with %d rollover%s",
319 ts->rollovers, ts->rollovers != 1 ? "s" : "");
320
321 if (verbose)
322 printf(" [%.0f bits/sec]", (ts->amount*8.)/delta);
323 putchar('\n');
324 }
325