root / slirp / tftp.c @ f68c781c
History | View | Annotate | Download (6.8 kB)
1 | c7f74643 | bellard | /*
|
---|---|---|---|
2 | c7f74643 | bellard | * tftp.c - a simple, read-only tftp server for qemu
|
3 | c7f74643 | bellard | *
|
4 | c7f74643 | bellard | * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
|
5 | c7f74643 | bellard | *
|
6 | c7f74643 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | c7f74643 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | c7f74643 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | c7f74643 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | c7f74643 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | c7f74643 | bellard | * furnished to do so, subject to the following conditions:
|
12 | c7f74643 | bellard | *
|
13 | c7f74643 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | c7f74643 | bellard | * all copies or substantial portions of the Software.
|
15 | c7f74643 | bellard | *
|
16 | c7f74643 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | c7f74643 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | c7f74643 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | c7f74643 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | c7f74643 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | c7f74643 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | c7f74643 | bellard | * THE SOFTWARE.
|
23 | c7f74643 | bellard | */
|
24 | c7f74643 | bellard | |
25 | c7f74643 | bellard | #include <slirp.h> |
26 | c7f74643 | bellard | |
27 | c7f74643 | bellard | struct tftp_session {
|
28 | a3504c87 | bellard | int in_use;
|
29 | a3504c87 | bellard | unsigned char filename[TFTP_FILENAME_MAX]; |
30 | a3504c87 | bellard | |
31 | a3504c87 | bellard | struct in_addr client_ip;
|
32 | a3504c87 | bellard | u_int16_t client_port; |
33 | a3504c87 | bellard | |
34 | a3504c87 | bellard | int timestamp;
|
35 | c7f74643 | bellard | }; |
36 | c7f74643 | bellard | |
37 | c7f74643 | bellard | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
38 | c7f74643 | bellard | |
39 | 9bf05444 | bellard | const char *tftp_prefix; |
40 | c7f74643 | bellard | |
41 | c7f74643 | bellard | static void tftp_session_update(struct tftp_session *spt) |
42 | c7f74643 | bellard | { |
43 | a3504c87 | bellard | spt->timestamp = curtime; |
44 | a3504c87 | bellard | spt->in_use = 1;
|
45 | c7f74643 | bellard | } |
46 | c7f74643 | bellard | |
47 | c7f74643 | bellard | static void tftp_session_terminate(struct tftp_session *spt) |
48 | c7f74643 | bellard | { |
49 | c7f74643 | bellard | spt->in_use = 0;
|
50 | c7f74643 | bellard | } |
51 | c7f74643 | bellard | |
52 | c7f74643 | bellard | static int tftp_session_allocate(struct tftp_t *tp) |
53 | c7f74643 | bellard | { |
54 | c7f74643 | bellard | struct tftp_session *spt;
|
55 | c7f74643 | bellard | int k;
|
56 | c7f74643 | bellard | |
57 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
58 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
59 | c7f74643 | bellard | |
60 | a3504c87 | bellard | if (!spt->in_use)
|
61 | a3504c87 | bellard | goto found;
|
62 | c7f74643 | bellard | |
63 | c7f74643 | bellard | /* sessions time out after 5 inactive seconds */
|
64 | a3504c87 | bellard | if ((int)(curtime - spt->timestamp) > 5000) |
65 | a3504c87 | bellard | goto found;
|
66 | c7f74643 | bellard | } |
67 | c7f74643 | bellard | |
68 | c7f74643 | bellard | return -1; |
69 | c7f74643 | bellard | |
70 | c7f74643 | bellard | found:
|
71 | c7f74643 | bellard | memset(spt, 0, sizeof(*spt)); |
72 | c7f74643 | bellard | memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
73 | c7f74643 | bellard | spt->client_port = tp->udp.uh_sport; |
74 | c7f74643 | bellard | |
75 | c7f74643 | bellard | tftp_session_update(spt); |
76 | c7f74643 | bellard | |
77 | c7f74643 | bellard | return k;
|
78 | c7f74643 | bellard | } |
79 | c7f74643 | bellard | |
80 | c7f74643 | bellard | static int tftp_session_find(struct tftp_t *tp) |
81 | c7f74643 | bellard | { |
82 | c7f74643 | bellard | struct tftp_session *spt;
|
83 | c7f74643 | bellard | int k;
|
84 | c7f74643 | bellard | |
85 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
86 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
87 | c7f74643 | bellard | |
88 | c7f74643 | bellard | if (spt->in_use) {
|
89 | c7f74643 | bellard | if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) { |
90 | c7f74643 | bellard | if (spt->client_port == tp->udp.uh_sport) {
|
91 | c7f74643 | bellard | return k;
|
92 | c7f74643 | bellard | } |
93 | c7f74643 | bellard | } |
94 | c7f74643 | bellard | } |
95 | c7f74643 | bellard | } |
96 | c7f74643 | bellard | |
97 | c7f74643 | bellard | return -1; |
98 | c7f74643 | bellard | } |
99 | c7f74643 | bellard | |
100 | c7f74643 | bellard | static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr, |
101 | c7f74643 | bellard | u_int8_t *buf, int len)
|
102 | c7f74643 | bellard | { |
103 | c7f74643 | bellard | int fd;
|
104 | c7f74643 | bellard | int bytes_read = 0; |
105 | c7f74643 | bellard | |
106 | 6d46bf8a | bellard | fd = open(spt->filename, O_RDONLY | O_BINARY); |
107 | c7f74643 | bellard | |
108 | c7f74643 | bellard | if (fd < 0) { |
109 | c7f74643 | bellard | return -1; |
110 | c7f74643 | bellard | } |
111 | c7f74643 | bellard | |
112 | c7f74643 | bellard | if (len) {
|
113 | c7f74643 | bellard | lseek(fd, block_nr * 512, SEEK_SET);
|
114 | c7f74643 | bellard | |
115 | c7f74643 | bellard | bytes_read = read(fd, buf, len); |
116 | c7f74643 | bellard | } |
117 | c7f74643 | bellard | |
118 | c7f74643 | bellard | close(fd); |
119 | c7f74643 | bellard | |
120 | c7f74643 | bellard | return bytes_read;
|
121 | c7f74643 | bellard | } |
122 | c7f74643 | bellard | |
123 | c7f74643 | bellard | static int tftp_send_error(struct tftp_session *spt, |
124 | c7f74643 | bellard | u_int16_t errorcode, const char *msg, |
125 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
126 | c7f74643 | bellard | { |
127 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
128 | c7f74643 | bellard | struct mbuf *m;
|
129 | c7f74643 | bellard | struct tftp_t *tp;
|
130 | c7f74643 | bellard | int nobytes;
|
131 | c7f74643 | bellard | |
132 | c7f74643 | bellard | m = m_get(); |
133 | c7f74643 | bellard | |
134 | c7f74643 | bellard | if (!m) {
|
135 | c7f74643 | bellard | return -1; |
136 | c7f74643 | bellard | } |
137 | c7f74643 | bellard | |
138 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
139 | c7f74643 | bellard | |
140 | c7f74643 | bellard | m->m_data += if_maxlinkhdr; |
141 | c7f74643 | bellard | tp = (void *)m->m_data;
|
142 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
143 | c7f74643 | bellard | |
144 | c7f74643 | bellard | tp->tp_op = htons(TFTP_ERROR); |
145 | c7f74643 | bellard | tp->x.tp_error.tp_error_code = htons(errorcode); |
146 | c7f74643 | bellard | strcpy(tp->x.tp_error.tp_msg, msg); |
147 | c7f74643 | bellard | |
148 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
149 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
150 | c7f74643 | bellard | |
151 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
152 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
153 | c7f74643 | bellard | |
154 | c7f74643 | bellard | nobytes = 2;
|
155 | c7f74643 | bellard | |
156 | c7f74643 | bellard | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - |
157 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
158 | c7f74643 | bellard | |
159 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
160 | c7f74643 | bellard | |
161 | c7f74643 | bellard | tftp_session_terminate(spt); |
162 | c7f74643 | bellard | |
163 | c7f74643 | bellard | return 0; |
164 | c7f74643 | bellard | } |
165 | c7f74643 | bellard | |
166 | c7f74643 | bellard | static int tftp_send_data(struct tftp_session *spt, |
167 | c7f74643 | bellard | u_int16_t block_nr, |
168 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
169 | c7f74643 | bellard | { |
170 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
171 | c7f74643 | bellard | struct mbuf *m;
|
172 | c7f74643 | bellard | struct tftp_t *tp;
|
173 | c7f74643 | bellard | int nobytes;
|
174 | c7f74643 | bellard | |
175 | c7f74643 | bellard | if (block_nr < 1) { |
176 | c7f74643 | bellard | return -1; |
177 | c7f74643 | bellard | } |
178 | c7f74643 | bellard | |
179 | c7f74643 | bellard | m = m_get(); |
180 | c7f74643 | bellard | |
181 | c7f74643 | bellard | if (!m) {
|
182 | c7f74643 | bellard | return -1; |
183 | c7f74643 | bellard | } |
184 | c7f74643 | bellard | |
185 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
186 | c7f74643 | bellard | |
187 | c7f74643 | bellard | m->m_data += if_maxlinkhdr; |
188 | c7f74643 | bellard | tp = (void *)m->m_data;
|
189 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
190 | c7f74643 | bellard | |
191 | c7f74643 | bellard | tp->tp_op = htons(TFTP_DATA); |
192 | c7f74643 | bellard | tp->x.tp_data.tp_block_nr = htons(block_nr); |
193 | c7f74643 | bellard | |
194 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
195 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
196 | c7f74643 | bellard | |
197 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
198 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
199 | c7f74643 | bellard | |
200 | c7f74643 | bellard | nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); |
201 | c7f74643 | bellard | |
202 | c7f74643 | bellard | if (nobytes < 0) { |
203 | c7f74643 | bellard | m_free(m); |
204 | c7f74643 | bellard | |
205 | c7f74643 | bellard | /* send "file not found" error back */
|
206 | c7f74643 | bellard | |
207 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
208 | c7f74643 | bellard | |
209 | c7f74643 | bellard | return -1; |
210 | c7f74643 | bellard | } |
211 | c7f74643 | bellard | |
212 | c7f74643 | bellard | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - |
213 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
214 | c7f74643 | bellard | |
215 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
216 | c7f74643 | bellard | |
217 | c7f74643 | bellard | if (nobytes == 512) { |
218 | c7f74643 | bellard | tftp_session_update(spt); |
219 | c7f74643 | bellard | } |
220 | c7f74643 | bellard | else {
|
221 | c7f74643 | bellard | tftp_session_terminate(spt); |
222 | c7f74643 | bellard | } |
223 | c7f74643 | bellard | |
224 | c7f74643 | bellard | return 0; |
225 | c7f74643 | bellard | } |
226 | c7f74643 | bellard | |
227 | c7f74643 | bellard | static void tftp_handle_rrq(struct tftp_t *tp, int pktlen) |
228 | c7f74643 | bellard | { |
229 | c7f74643 | bellard | struct tftp_session *spt;
|
230 | c7f74643 | bellard | int s, k, n;
|
231 | c7f74643 | bellard | u_int8_t *src, *dst; |
232 | c7f74643 | bellard | |
233 | c7f74643 | bellard | s = tftp_session_allocate(tp); |
234 | c7f74643 | bellard | |
235 | c7f74643 | bellard | if (s < 0) { |
236 | c7f74643 | bellard | return;
|
237 | c7f74643 | bellard | } |
238 | c7f74643 | bellard | |
239 | c7f74643 | bellard | spt = &tftp_sessions[s]; |
240 | c7f74643 | bellard | |
241 | c7f74643 | bellard | src = tp->x.tp_buf; |
242 | c7f74643 | bellard | dst = spt->filename; |
243 | c7f74643 | bellard | n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
244 | c7f74643 | bellard | |
245 | c7f74643 | bellard | /* get name */
|
246 | c7f74643 | bellard | |
247 | c7f74643 | bellard | for (k = 0; k < n; k++) { |
248 | c7f74643 | bellard | if (k < TFTP_FILENAME_MAX) {
|
249 | c7f74643 | bellard | dst[k] = src[k]; |
250 | c7f74643 | bellard | } |
251 | c7f74643 | bellard | else {
|
252 | c7f74643 | bellard | return;
|
253 | c7f74643 | bellard | } |
254 | c7f74643 | bellard | |
255 | c7f74643 | bellard | if (src[k] == '\0') { |
256 | c7f74643 | bellard | break;
|
257 | c7f74643 | bellard | } |
258 | c7f74643 | bellard | } |
259 | c7f74643 | bellard | |
260 | c7f74643 | bellard | if (k >= n) {
|
261 | c7f74643 | bellard | return;
|
262 | c7f74643 | bellard | } |
263 | c7f74643 | bellard | |
264 | c7f74643 | bellard | k++; |
265 | c7f74643 | bellard | |
266 | c7f74643 | bellard | /* check mode */
|
267 | c7f74643 | bellard | if ((n - k) < 6) { |
268 | c7f74643 | bellard | return;
|
269 | c7f74643 | bellard | } |
270 | c7f74643 | bellard | |
271 | c7f74643 | bellard | if (memcmp(&src[k], "octet\0", 6) != 0) { |
272 | c7f74643 | bellard | tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
273 | c7f74643 | bellard | return;
|
274 | c7f74643 | bellard | } |
275 | c7f74643 | bellard | |
276 | c7f74643 | bellard | /* do sanity checks on the filename */
|
277 | c7f74643 | bellard | |
278 | c7f74643 | bellard | if ((spt->filename[0] != '/') |
279 | c7f74643 | bellard | || (spt->filename[strlen(spt->filename) - 1] == '/') |
280 | c7f74643 | bellard | || strstr(spt->filename, "/../")) {
|
281 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
282 | c7f74643 | bellard | return;
|
283 | c7f74643 | bellard | } |
284 | c7f74643 | bellard | |
285 | c7f74643 | bellard | /* only allow exported prefixes */
|
286 | c7f74643 | bellard | |
287 | c7f74643 | bellard | if (!tftp_prefix
|
288 | c7f74643 | bellard | || (strncmp(spt->filename, tftp_prefix, strlen(tftp_prefix)) != 0)) {
|
289 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
290 | c7f74643 | bellard | return;
|
291 | c7f74643 | bellard | } |
292 | c7f74643 | bellard | |
293 | c7f74643 | bellard | /* check if the file exists */
|
294 | c7f74643 | bellard | |
295 | c7f74643 | bellard | if (tftp_read_data(spt, 0, spt->filename, 0) < 0) { |
296 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
297 | c7f74643 | bellard | return;
|
298 | c7f74643 | bellard | } |
299 | c7f74643 | bellard | |
300 | c7f74643 | bellard | tftp_send_data(spt, 1, tp);
|
301 | c7f74643 | bellard | } |
302 | c7f74643 | bellard | |
303 | c7f74643 | bellard | static void tftp_handle_ack(struct tftp_t *tp, int pktlen) |
304 | c7f74643 | bellard | { |
305 | c7f74643 | bellard | int s;
|
306 | c7f74643 | bellard | |
307 | c7f74643 | bellard | s = tftp_session_find(tp); |
308 | c7f74643 | bellard | |
309 | c7f74643 | bellard | if (s < 0) { |
310 | c7f74643 | bellard | return;
|
311 | c7f74643 | bellard | } |
312 | c7f74643 | bellard | |
313 | c7f74643 | bellard | if (tftp_send_data(&tftp_sessions[s],
|
314 | c7f74643 | bellard | ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
315 | c7f74643 | bellard | tp) < 0) {
|
316 | c7f74643 | bellard | return;
|
317 | c7f74643 | bellard | } |
318 | c7f74643 | bellard | } |
319 | c7f74643 | bellard | |
320 | c7f74643 | bellard | void tftp_input(struct mbuf *m) |
321 | c7f74643 | bellard | { |
322 | c7f74643 | bellard | struct tftp_t *tp = (struct tftp_t *)m->m_data; |
323 | c7f74643 | bellard | |
324 | c7f74643 | bellard | switch(ntohs(tp->tp_op)) {
|
325 | c7f74643 | bellard | case TFTP_RRQ:
|
326 | c7f74643 | bellard | tftp_handle_rrq(tp, m->m_len); |
327 | c7f74643 | bellard | break;
|
328 | c7f74643 | bellard | |
329 | c7f74643 | bellard | case TFTP_ACK:
|
330 | c7f74643 | bellard | tftp_handle_ack(tp, m->m_len); |
331 | c7f74643 | bellard | break;
|
332 | c7f74643 | bellard | } |
333 | c7f74643 | bellard | } |