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$");
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
36 #include <netinet/in.h>
37 #include <arpa/tftp.h>
45 #include "tftp-utils.h"
47 #include "tftp-options.h"
53 struct options options[] = {
54 { "tsize", NULL, NULL, NULL /* option_tsize */, 1 },
55 { "timeout", NULL, NULL, option_timeout, 1 },
56 { "blksize", NULL, NULL, option_blksize, 1 },
57 { "blksize2", NULL, NULL, option_blksize2, 0 },
58 { "rollover", NULL, NULL, option_rollover, 0 },
59 { "windowsize", NULL, NULL, option_windowsize, 1 },
60 { NULL, NULL, NULL, NULL, 0 }
63 /* By default allow them */
64 int options_rfc_enabled = 1;
65 int options_extra_enabled = 1;
68 * Rules for the option handlers:
69 * - If there is no o_request, there will be no processing.
72 * - Logging is done as warnings.
73 * - The handler exit()s if there is a serious problem with the
74 * values submitted in the option.
77 * - Logging is done as errors. After all, the server shouldn't
79 * - The handler returns if there is a serious problem with the
80 * values submitted in the option.
81 * - Sending the EBADOP packets is done by the handler.
85 option_tsize(int peer __unused, struct tftphdr *tp __unused, int mode,
89 if (options[OPT_TSIZE].o_request == NULL)
93 asprintf(&options[OPT_TSIZE].o_reply,
94 "%ju", stbuf->st_size);
96 /* XXX Allows writes of all sizes. */
97 options[OPT_TSIZE].o_reply =
98 strdup(options[OPT_TSIZE].o_request);
103 option_timeout(int peer)
107 if (options[OPT_TIMEOUT].o_request == NULL)
110 to = atoi(options[OPT_TIMEOUT].o_request);
111 if (to < TIMEOUT_MIN || to > TIMEOUT_MAX) {
112 tftp_log(acting_as_client ? LOG_ERR : LOG_WARNING,
113 "Received bad value for timeout. "
114 "Should be between %d and %d, received %d",
115 TIMEOUT_MIN, TIMEOUT_MAX, to);
116 send_error(peer, EBADOP);
117 if (acting_as_client)
122 options[OPT_TIMEOUT].o_reply =
123 strdup(options[OPT_TIMEOUT].o_request);
125 settimeouts(timeoutpacket, timeoutnetwork, maxtimeouts);
127 if (debug&DEBUG_OPTIONS)
128 tftp_log(LOG_DEBUG, "Setting timeout to '%s'",
129 options[OPT_TIMEOUT].o_reply);
135 option_rollover(int peer)
138 if (options[OPT_ROLLOVER].o_request == NULL)
141 if (strcmp(options[OPT_ROLLOVER].o_request, "0") != 0
142 && strcmp(options[OPT_ROLLOVER].o_request, "1") != 0) {
143 tftp_log(acting_as_client ? LOG_ERR : LOG_WARNING,
144 "Bad value for rollover, "
145 "should be either 0 or 1, received '%s', "
147 options[OPT_ROLLOVER].o_request);
148 if (acting_as_client) {
149 send_error(peer, EBADOP);
154 options[OPT_ROLLOVER].o_reply =
155 strdup(options[OPT_ROLLOVER].o_request);
157 if (debug&DEBUG_OPTIONS)
158 tftp_log(LOG_DEBUG, "Setting rollover to '%s'",
159 options[OPT_ROLLOVER].o_reply);
165 option_blksize(int peer)
170 if (options[OPT_BLKSIZE].o_request == NULL)
173 /* maximum size of an UDP packet according to the system */
174 len = sizeof(maxdgram);
175 if (sysctlbyname("net.inet.udp.maxdgram",
176 &maxdgram, &len, NULL, 0) < 0) {
177 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram");
178 return (acting_as_client ? 1 : 0);
181 int size = atoi(options[OPT_BLKSIZE].o_request);
182 if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
183 if (acting_as_client) {
185 "Invalid blocksize (%d bytes), aborting",
187 send_error(peer, EBADOP);
190 tftp_log(LOG_WARNING,
191 "Invalid blocksize (%d bytes), ignoring request",
197 if (size > (int)maxdgram) {
198 if (acting_as_client) {
200 "Invalid blocksize (%d bytes), "
201 "net.inet.udp.maxdgram sysctl limits it to "
202 "%ld bytes.\n", size, maxdgram);
203 send_error(peer, EBADOP);
206 tftp_log(LOG_WARNING,
207 "Invalid blocksize (%d bytes), "
208 "net.inet.udp.maxdgram sysctl limits it to "
209 "%ld bytes.\n", size, maxdgram);
211 /* No reason to return */
215 asprintf(&options[OPT_BLKSIZE].o_reply, "%d", size);
218 if (debug&DEBUG_OPTIONS)
219 tftp_log(LOG_DEBUG, "Setting blksize to '%s'",
220 options[OPT_BLKSIZE].o_reply);
226 option_blksize2(int peer __unused)
233 8, 16, 32, 64, 128, 256, 512, 1024,
234 2048, 4096, 8192, 16384, 32768, 0
237 if (options[OPT_BLKSIZE2].o_request == NULL)
240 /* maximum size of an UDP packet according to the system */
241 len = sizeof(maxdgram);
242 if (sysctlbyname("net.inet.udp.maxdgram",
243 &maxdgram, &len, NULL, 0) < 0) {
244 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram");
245 return (acting_as_client ? 1 : 0);
248 size = atoi(options[OPT_BLKSIZE2].o_request);
249 for (i = 0; sizes[i] != 0; i++) {
250 if (size == sizes[i]) break;
254 "Invalid blocksize2 (%d bytes), ignoring request", size);
255 return (acting_as_client ? 1 : 0);
258 if (size > (int)maxdgram) {
259 for (i = 0; sizes[i+1] != 0; i++) {
260 if ((int)maxdgram < sizes[i+1]) break;
263 "Invalid blocksize2 (%d bytes), net.inet.udp.maxdgram "
264 "sysctl limits it to %ld bytes.\n", size, maxdgram);
266 /* No need to return */
269 asprintf(&options[OPT_BLKSIZE2].o_reply, "%d", size);
272 if (debug&DEBUG_OPTIONS)
273 tftp_log(LOG_DEBUG, "Setting blksize2 to '%s'",
274 options[OPT_BLKSIZE2].o_reply);
280 option_windowsize(int peer)
284 if (options[OPT_WINDOWSIZE].o_request == NULL)
287 size = atoi(options[OPT_WINDOWSIZE].o_request);
288 if (size < WINDOWSIZE_MIN || size > WINDOWSIZE_MAX) {
289 if (acting_as_client) {
291 "Invalid windowsize (%d blocks), aborting",
293 send_error(peer, EBADOP);
296 tftp_log(LOG_WARNING,
297 "Invalid windowsize (%d blocks), ignoring request",
303 /* XXX: Should force a windowsize of 1 for non-seekable files. */
304 asprintf(&options[OPT_WINDOWSIZE].o_reply, "%d", size);
307 if (debug&DEBUG_OPTIONS)
308 tftp_log(LOG_DEBUG, "Setting windowsize to '%s'",
309 options[OPT_WINDOWSIZE].o_reply);
315 * Append the available options to the header
318 make_options(int peer __unused, char *buffer, uint16_t size) {
323 uint16_t returnsize = 0;
325 if (!options_rfc_enabled) return (0);
327 for (i = 0; options[i].o_type != NULL; i++) {
328 if (options[i].rfc == 0 && !options_extra_enabled)
331 option = options[i].o_type;
332 if (acting_as_client)
333 value = options[i].o_request;
335 value = options[i].o_reply;
339 length = strlen(value) + strlen(option) + 2;
340 if (size <= length) {
342 "Running out of option space for "
343 "option '%s' with value '%s': "
344 "needed %d bytes, got %d bytes",
345 option, value, size, length);
349 sprintf(buffer, "%s%c%s%c", option, '\000', value, '\000');
352 returnsize += length;
359 * Parse the received options in the header
362 parse_options(int peer, char *buffer, uint16_t size)
364 int i, options_failed;
365 char *c, *cp, *option, *value;
367 if (!options_rfc_enabled) return (0);
369 /* Parse the options */
374 i = get_field(peer, cp, size);
378 i = get_field(peer, cp, size);
381 /* We are at the end */
382 if (*option == '\0') break;
384 if (debug&DEBUG_OPTIONS)
386 "option: '%s' value: '%s'", option, value);
388 for (c = option; *c; c++)
391 for (i = 0; options[i].o_type != NULL; i++) {
392 if (strcmp(option, options[i].o_type) == 0) {
393 if (!acting_as_client)
394 options[i].o_request = value;
395 if (!options_extra_enabled && !options[i].rfc) {
397 "Option '%s' with value '%s' found "
398 "but it is not an RFC option",
402 if (options[i].o_handler)
404 (options[i].o_handler)(peer);
408 if (options[i].o_type == NULL)
409 tftp_log(LOG_WARNING,
410 "Unknown option: '%s'", option);
412 size -= strlen(option) + strlen(value) + 2;
415 return (options_failed);
419 * Set some default values in the options
425 options[OPT_ROLLOVER].o_request = strdup("0");