]> Git Repo - qemu.git/blob - slirp/tftp.c
slirp: replace remaining qemu headers dependency
[qemu.git] / slirp / tftp.c
1 /*
2  * tftp.c - a simple, read-only tftp server for qemu
3  *
4  * Copyright (c) 2004 Magnus Damm <[email protected]>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "slirp.h"
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 static inline int tftp_session_in_use(struct tftp_session *spt)
32 {
33     return (spt->slirp != NULL);
34 }
35
36 static inline void tftp_session_update(struct tftp_session *spt)
37 {
38     spt->timestamp = curtime;
39 }
40
41 static void tftp_session_terminate(struct tftp_session *spt)
42 {
43     if (spt->fd >= 0) {
44         close(spt->fd);
45         spt->fd = -1;
46     }
47     g_free(spt->filename);
48     spt->slirp = NULL;
49 }
50
51 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
52                                  struct tftp_t *tp)
53 {
54   struct tftp_session *spt;
55   int k;
56
57   for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
58     spt = &slirp->tftp_sessions[k];
59
60     if (!tftp_session_in_use(spt))
61         goto found;
62
63     /* sessions time out after 5 inactive seconds */
64     if ((int)(curtime - spt->timestamp) > 5000) {
65         tftp_session_terminate(spt);
66         goto found;
67     }
68   }
69
70   return -1;
71
72  found:
73   memset(spt, 0, sizeof(*spt));
74   memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
75   spt->fd = -1;
76   spt->block_size = 512;
77   spt->client_port = tp->udp.uh_sport;
78   spt->slirp = slirp;
79
80   tftp_session_update(spt);
81
82   return k;
83 }
84
85 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
86                              struct tftp_t *tp)
87 {
88   struct tftp_session *spt;
89   int k;
90
91   for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
92     spt = &slirp->tftp_sessions[k];
93
94     if (tftp_session_in_use(spt)) {
95       if (sockaddr_equal(&spt->client_addr, srcsas)) {
96         if (spt->client_port == tp->udp.uh_sport) {
97           return k;
98         }
99       }
100     }
101   }
102
103   return -1;
104 }
105
106 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
107                           uint8_t *buf, int len)
108 {
109     int bytes_read = 0;
110
111     if (spt->fd < 0) {
112         spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
113     }
114
115     if (spt->fd < 0) {
116         return -1;
117     }
118
119     if (len) {
120         lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
121
122         bytes_read = read(spt->fd, buf, len);
123     }
124
125     return bytes_read;
126 }
127
128 static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
129                                           struct mbuf *m)
130 {
131     struct tftp_t *tp;
132
133     memset(m->m_data, 0, m->m_size);
134
135     m->m_data += IF_MAXLINKHDR;
136     if (spt->client_addr.ss_family == AF_INET6) {
137         m->m_data += sizeof(struct ip6);
138     } else {
139         m->m_data += sizeof(struct ip);
140     }
141     tp = (void *)m->m_data;
142     m->m_data += sizeof(struct udphdr);
143
144     return tp;
145 }
146
147 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
148                             struct tftp_t *recv_tp)
149 {
150     if (spt->client_addr.ss_family == AF_INET6) {
151         struct sockaddr_in6 sa6, da6;
152
153         sa6.sin6_addr = spt->slirp->vhost_addr6;
154         sa6.sin6_port = recv_tp->udp.uh_dport;
155         da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
156         da6.sin6_port = spt->client_port;
157
158         udp6_output(NULL, m, &sa6, &da6);
159     } else {
160         struct sockaddr_in sa4, da4;
161
162         sa4.sin_addr = spt->slirp->vhost_addr;
163         sa4.sin_port = recv_tp->udp.uh_dport;
164         da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
165         da4.sin_port = spt->client_port;
166
167         udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
168     }
169 }
170
171 static int tftp_send_oack(struct tftp_session *spt,
172                           const char *keys[], uint32_t values[], int nb,
173                           struct tftp_t *recv_tp)
174 {
175     struct mbuf *m;
176     struct tftp_t *tp;
177     int i, n = 0;
178
179     m = m_get(spt->slirp);
180
181     if (!m)
182         return -1;
183
184     tp = tftp_prep_mbuf_data(spt, m);
185
186     tp->tp_op = htons(TFTP_OACK);
187     for (i = 0; i < nb; i++) {
188         n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
189                       keys[i]) + 1;
190         n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
191                       values[i]) + 1;
192     }
193
194     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n
195                - sizeof(struct udphdr);
196     tftp_udp_output(spt, m, recv_tp);
197
198     return 0;
199 }
200
201 static void tftp_send_error(struct tftp_session *spt,
202                             uint16_t errorcode, const char *msg,
203                             struct tftp_t *recv_tp)
204 {
205   struct mbuf *m;
206   struct tftp_t *tp;
207
208   DEBUG_TFTP("tftp error msg: %s", msg);
209
210   m = m_get(spt->slirp);
211
212   if (!m) {
213     goto out;
214   }
215
216   tp = tftp_prep_mbuf_data(spt, m);
217
218   tp->tp_op = htons(TFTP_ERROR);
219   tp->x.tp_error.tp_error_code = htons(errorcode);
220   slirp_pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
221
222   m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 + strlen(msg)
223              - sizeof(struct udphdr);
224   tftp_udp_output(spt, m, recv_tp);
225
226 out:
227   tftp_session_terminate(spt);
228 }
229
230 static void tftp_send_next_block(struct tftp_session *spt,
231                                  struct tftp_t *recv_tp)
232 {
233   struct mbuf *m;
234   struct tftp_t *tp;
235   int nobytes;
236
237   m = m_get(spt->slirp);
238
239   if (!m) {
240     return;
241   }
242
243   tp = tftp_prep_mbuf_data(spt, m);
244
245   tp->tp_op = htons(TFTP_DATA);
246   tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
247
248   nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
249                            spt->block_size);
250
251   if (nobytes < 0) {
252     m_free(m);
253
254     /* send "file not found" error back */
255
256     tftp_send_error(spt, 1, "File not found", tp);
257
258     return;
259   }
260
261   m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes)
262              - sizeof(struct udphdr);
263   tftp_udp_output(spt, m, recv_tp);
264
265   if (nobytes == spt->block_size) {
266     tftp_session_update(spt);
267   }
268   else {
269     tftp_session_terminate(spt);
270   }
271
272   spt->block_nr++;
273 }
274
275 static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
276                             struct tftp_t *tp, int pktlen)
277 {
278   struct tftp_session *spt;
279   int s, k;
280   size_t prefix_len;
281   char *req_fname;
282   const char *option_name[2];
283   uint32_t option_value[2];
284   int nb_options = 0;
285
286   /* check if a session already exists and if so terminate it */
287   s = tftp_session_find(slirp, srcsas, tp);
288   if (s >= 0) {
289     tftp_session_terminate(&slirp->tftp_sessions[s]);
290   }
291
292   s = tftp_session_allocate(slirp, srcsas, tp);
293
294   if (s < 0) {
295     return;
296   }
297
298   spt = &slirp->tftp_sessions[s];
299
300   /* unspecified prefix means service disabled */
301   if (!slirp->tftp_prefix) {
302       tftp_send_error(spt, 2, "Access violation", tp);
303       return;
304   }
305
306   /* skip header fields */
307   k = 0;
308   pktlen -= offsetof(struct tftp_t, x.tp_buf);
309
310   /* prepend tftp_prefix */
311   prefix_len = strlen(slirp->tftp_prefix);
312   spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
313   memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
314   spt->filename[prefix_len] = '/';
315
316   /* get name */
317   req_fname = spt->filename + prefix_len + 1;
318
319   while (1) {
320     if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
321       tftp_send_error(spt, 2, "Access violation", tp);
322       return;
323     }
324     req_fname[k] = tp->x.tp_buf[k];
325     if (req_fname[k++] == '\0') {
326       break;
327     }
328   }
329
330   DEBUG_TFTP("tftp rrq file: %s", req_fname);
331
332   /* check mode */
333   if ((pktlen - k) < 6) {
334     tftp_send_error(spt, 2, "Access violation", tp);
335     return;
336   }
337
338   if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
339       tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
340       return;
341   }
342
343   k += 6; /* skipping octet */
344
345   /* do sanity checks on the filename */
346   if (!strncmp(req_fname, "../", 3) ||
347       req_fname[strlen(req_fname) - 1] == '/' ||
348       strstr(req_fname, "/../")) {
349       tftp_send_error(spt, 2, "Access violation", tp);
350       return;
351   }
352
353   /* check if the file exists */
354   if (tftp_read_data(spt, 0, NULL, 0) < 0) {
355       tftp_send_error(spt, 1, "File not found", tp);
356       return;
357   }
358
359   if (tp->x.tp_buf[pktlen - 1] != 0) {
360       tftp_send_error(spt, 2, "Access violation", tp);
361       return;
362   }
363
364   while (k < pktlen && nb_options < G_N_ELEMENTS(option_name)) {
365       const char *key, *value;
366
367       key = &tp->x.tp_buf[k];
368       k += strlen(key) + 1;
369
370       if (k >= pktlen) {
371           tftp_send_error(spt, 2, "Access violation", tp);
372           return;
373       }
374
375       value = &tp->x.tp_buf[k];
376       k += strlen(value) + 1;
377
378       if (strcasecmp(key, "tsize") == 0) {
379           int tsize = atoi(value);
380           struct stat stat_p;
381
382           if (tsize == 0) {
383               if (stat(spt->filename, &stat_p) == 0)
384                   tsize = stat_p.st_size;
385               else {
386                   tftp_send_error(spt, 1, "File not found", tp);
387                   return;
388               }
389           }
390
391           option_name[nb_options] = "tsize";
392           option_value[nb_options] = tsize;
393           nb_options++;
394       } else if (strcasecmp(key, "blksize") == 0) {
395           int blksize = atoi(value);
396
397           /* Accept blksize up to our maximum size */
398           if (blksize > 0) {
399               spt->block_size = MIN(blksize, TFTP_BLOCKSIZE_MAX);
400               option_name[nb_options] = "blksize";
401               option_value[nb_options] = spt->block_size;
402               nb_options++;
403           }
404       }
405   }
406
407   if (nb_options > 0) {
408       assert(nb_options <= G_N_ELEMENTS(option_name));
409       tftp_send_oack(spt, option_name, option_value, nb_options, tp);
410       return;
411   }
412
413   spt->block_nr = 0;
414   tftp_send_next_block(spt, tp);
415 }
416
417 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
418                             struct tftp_t *tp, int pktlen)
419 {
420   int s;
421
422   s = tftp_session_find(slirp, srcsas, tp);
423
424   if (s < 0) {
425     return;
426   }
427
428   tftp_send_next_block(&slirp->tftp_sessions[s], tp);
429 }
430
431 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
432                               struct tftp_t *tp, int pktlen)
433 {
434   int s;
435
436   s = tftp_session_find(slirp, srcsas, tp);
437
438   if (s < 0) {
439     return;
440   }
441
442   tftp_session_terminate(&slirp->tftp_sessions[s]);
443 }
444
445 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
446 {
447   struct tftp_t *tp = (struct tftp_t *)m->m_data;
448
449   switch(ntohs(tp->tp_op)) {
450   case TFTP_RRQ:
451     tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
452     break;
453
454   case TFTP_ACK:
455     tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
456     break;
457
458   case TFTP_ERROR:
459     tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
460     break;
461   }
462 }
This page took 0.051682 seconds and 4 git commands to generate.