2 * tftpd - tftp service, see /lib/rfc/rfc783 (now rfc1350 + 234[789])
16 Opsize= sizeof(short),
17 Blksize= sizeof(short),
18 Hdrsize= Opsize + Blksize,
30 Tftp_OACK = 6, /* option acknowledge */
32 Errnotdef = 0, /* see textual error instead */
40 Errbadopt = 8, /* really bad option value */
43 Maxsegsize = 65464, /* from rfc2348 */
46 * bandt (viaduct) tunnels use smaller mtu than ether's
47 * (1400 bytes for tcp mss of 1300 bytes).
51 * maximum size of block's data content, excludes hdrs,
52 * notably IP/UDP and TFTP, using worst-case (IPv6) sizes.
54 Bandtblksz = Bandtmtu - 40 - 8,
55 Bcavium = 1432, /* cavium's u-boot demands this size */
58 typedef struct Opt Opt;
61 int *valp; /* set to client's value if within bounds */
71 int blksize = Defsegsize; /* excluding 4-byte header */
72 int timeout = 5; /* seconds */
74 static Opt option[] = {
75 "timeout", &timeout, 1, 255,
76 /* see "hack" below */
77 "blksize", &blksize, 8, Maxsegsize,
78 "tsize", &tsize, 0, ~0UL >> 1,
81 void sendfile(int, char*, char*, int);
82 void recvfile(int, char*, char*);
83 void nak(int, int, char*);
84 void ack(int, ushort);
87 void remoteaddr(char*, char*, int);
97 char flog[] = "ipboot";
100 static char *opnames[] = {
102 [Tftp_WRITE] "write",
105 [Tftp_ERROR] "error",
112 fprint(2, "usage: %s [-dr] [-h homedir] [-s svc] [-x netmtpt]\n",
118 main(int argc, char **argv)
121 char adir[64], ldir[64];
125 setnetmtpt(net, sizeof net, nil);
131 homedir = EARGF(usage());
137 svc = EARGF(usage());
140 setnetmtpt(net, sizeof net, EARGF(usage()));
143 nsfile = EARGF(usage());
149 dirsllen = strlen(homedir);
150 while(dirsllen > 0 && homedir[dirsllen-1] == '/')
152 dirsl = smprint("%.*s/", utfnlen(homedir, dirsllen), homedir);
154 fmtinstall('E', eipfmt);
155 fmtinstall('I', eipfmt);
158 * setuser calls newns, and typical /lib/namespace files contain
159 * "cd /usr/$user", so call setuser before chdir.
162 if(chdir(homedir) < 0)
163 sysfatal("can't get to directory %s: %r", homedir);
166 switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
168 sysfatal("fork: %r");
175 snprint(buf, sizeof buf, "%s/udp!*!%s", net, svc);
176 cfd = announce(buf, adir);
178 sysfatal("announcing on %s: %r", buf);
179 syslog(dbg, flog, "tftpd started on %s dir %s", buf, adir);
182 lcfd = listen(adir, ldir);
184 sysfatal("listening on %s: %r", adir);
188 sysfatal("fork: %r");
190 dfd = accept(lcfd, ldir);
193 remoteaddr(ldir, raddr, sizeof(raddr));
195 syslog(0, flog, "tftp %d connection from %s dir %s",
208 handleopt(int fd, char *name, char *val)
213 for (op = option; op < option + nelem(option); op++)
214 if(cistrcmp(name, op->name) == 0) {
215 n = strtol(val, nil, 10);
216 if (n < op->min || n > op->max) {
217 nak(fd, Errbadopt, "option value out of range");
218 syslog(dbg, flog, "tftp bad option value from "
219 "client: %s %s", name, val);
220 sysfatal("bad option value from client: %s %s",
224 /* incoming 0 for tsize is uninteresting */
225 if(cistrcmp("tsize", op->name) != 0)
226 syslog(dbg, flog, "tftpd %d setting %s to client's %d",
247 /* copy word into bp iff it fits before ep, returns bytes to advance bp. */
249 emits(char *word, char *bp, char *ep)
253 len = strlen(word) + 1;
260 /* format number into bp iff it fits before ep. */
262 emitn(vlong n, char *bp, char *ep)
266 snprint(numb, sizeof numb, "%lld", n);
267 return emits(numb, bp, ep);
271 * send an OACK packet to respond to options. bail early with -1 on error.
272 * p is the packet containing the options.
274 * hack: bandt (viaducts) uses smaller mtu than ether's
275 * (1400 bytes for tcp mss of 1300 bytes),
276 * so offer at most bandt's mtu minus headers,
277 * to avoid failure of pxe booting via viaduct.
278 * there's an exception for the cavium's u-boot.
281 options(int fd, char *buf, int bufsz, char *file, ushort oper, char *p, int dlen)
283 int nmlen, vallen, olen, nopts;
293 for (; dlen > 0 && *p != '\0'; p = val + vallen, bp += olen) {
294 nmlen = strlen(p) + 1; /* include NUL */
299 if (dlen <= 0 || *val == '\0')
302 vallen = strlen(val) + 1;
308 op = handleopt(fd, p, val);
314 /* append OACK response to buf */
315 nmlen = emits(p, bp, ep); /* option name */
320 if (oper == Tftp_READ && cistrcmp(p, "tsize") == 0) {
321 size = filesize(file);
323 nak(fd, Errnotfound, "no such file");
324 syslog(dbg, flog, "tftpd tsize for "
325 "non-existent file %s", file);
327 // olen = emits("0", bp, ep);
331 olen = emitn(size, bp, ep);
332 syslog(dbg, flog, "tftpd %d %s tsize is %,lld",
334 } else if (oper == Tftp_READ && cistrcmp(p, "blksize") == 0 &&
335 blksize > Bandtblksz && blksize != Bcavium) {
336 *op->valp = blksize = Bandtblksz;
337 olen = emitn(blksize, bp, ep);
338 syslog(dbg, flog, "tftpd %d overriding blksize to %d",
341 olen = emits(val, bp, ep); /* use requested value */
344 return 0; /* no options actually seen */
346 if (write(fd, buf, bp - buf) < bp - buf) {
347 syslog(dbg, flog, "tftpd network write error on oack to %s: %r",
349 sysfatal("tftpd: network write error: %r");
352 syslog(dbg, flog, "tftpd oack: options to %s", raddr);
357 optlog(char *bytes, char *p, int dlen)
362 sprint(bp, "tftpd %d option bytes: ", dlen);
364 for (; dlen > 0; dlen--, p++)
367 syslog(dbg, flog, "%s", bytes);
371 * replace one occurrence of %[ICE] with ip, cfgpxe name, or ether mac, resp.
372 * we can't easily use $ because u-boot has stranger quoting rules than sh.
378 char *p, *newnm, *cur, *arpf, *ln, *remip, *bang;
382 p = strchr(file, '%');
383 if (p == nil || p[1] == '\0')
386 remip = strdup(raddr);
387 newnm = mallocz(strlen(file) + Maxpath, 1);
388 if (remip == nil || newnm == nil)
389 sysfatal("out of memory");
391 bang = strchr(remip, '!');
393 *bang = '\0'; /* remove !port */
395 memmove(newnm, file, p - file); /* copy up to % */
396 cur = newnm + strlen(newnm);
399 strcpy(cur, remip); /* remote's IP */
402 strcpy(cur, "/cfg/pxe/");
406 /* look up remote's IP in /net/arp to get mac. */
407 arpf = smprint("%s/arp", net);
408 arp = Bopen(arpf, OREAD);
412 /* read lines looking for remip in 3rd field of 4 */
413 while ((ln = Brdline(arp, '\n')) != nil) {
414 ln[Blinelen(arp)-1] = 0;
415 nf = tokenize(ln, fields, nelem(fields));
416 if (nf >= 4 && strcmp(fields[2], remip) == 0) {
417 strcpy(cur, fields[3]);
424 strcat(newnm, p + 2); /* tail following %x */
433 char *mode, *p, *file;
436 dlen = read(fd, bigbuf, sizeof(bigbuf)-1);
438 sysfatal("listen read: %r");
441 op = (bigbuf[0]<<8) | bigbuf[1];
443 mode = file = bigbuf + Opsize;
444 while(*mode != '\0' && dlen--)
451 file = mapname(file); /* we don't free the result; minor leak */
454 nak(fd, 0, "bad tftpmode");
456 syslog(dbg, flog, "tftpd %d bad mode %s for file %s from %s",
457 pid, mode, file, raddr);
461 if(op != Tftp_READ && op != Tftp_WRITE) {
462 nak(fd, Errbadop, "Illegal TFTP operation");
464 syslog(dbg, flog, "tftpd %d bad request %d (%s) %s", pid, op,
465 (op < nelem(opnames)? opnames[op]: "gok"), raddr);
470 if(file[0] == '#' || strncmp(file, "../", 3) == 0 ||
471 strstr(file, "/../") != nil ||
472 (file[0] == '/' && strncmp(file, dirsl, dirsllen) != 0)){
473 nak(fd, Errnoaccess, "Permission denied");
475 syslog(dbg, flog, "tftpd %d bad request %d from %s file %s",
476 pid, op, raddr, file);
482 * options are supposed to be negotiated, but the cavium board's
483 * u-boot really wants us to use a block size of 1432 bytes and won't
484 * take `no' for an answer.
486 p++; /* skip NUL after mode */
489 if(dlen > 0) { /* might have options */
493 optlog(bytes, p, dlen);
494 opts = options(fd, bytes, sizeof bytes, file, op, p, dlen);
499 sendfile(fd, file, mode, opts);
501 recvfile(fd, file, mode);
505 catcher(void *junk, char *msg)
509 if(strncmp(msg, "exit", 4) == 0)
515 awaitack(int fd, int block)
517 int ackblock, al, rxl;
521 for(rxl = 0; rxl < 10; rxl++) {
522 memset(ack, 0, Hdrsize);
524 al = read(fd, ack, sizeof(ack));
528 syslog(dbg, flog, "tftpd %d timed out "
529 "waiting for ack from %s", pid, raddr);
532 op = ack[0]<<8|ack[1];
533 if(op == Tftp_ERROR) {
535 syslog(dbg, flog, "tftpd %d got error "
536 "waiting for ack from %s", pid, raddr);
538 } else if(op != Tftp_ACK) {
539 syslog(dbg, flog, "tftpd %d rcvd %s op from %s", pid,
540 (op < nelem(opnames)? opnames[op]: "gok"),
544 ackblock = ack[2]<<8|ack[3];
546 syslog(dbg, flog, "tftpd %d read ack of %d bytes "
547 "for block %d", pid, al, ackblock);
548 if(ackblock == (block & 0xffff))
549 return Ackok; /* for block just sent */
550 else if(ackblock == (block + 1 & 0xffff)) /* intel pxe eof bug */
552 else if(ackblock == 0xffff)
555 /* ack is for some other block; ignore it, try again */
556 syslog(dbg, flog, "tftpd %d expected ack for block %d, "
557 "got %d", pid, block, ackblock);
563 sendfile(int fd, char *name, char *mode, int opts)
565 int file, block, ret, rexmit, n, txtry;
566 uchar buf[Maxsegsize+Hdrsize];
569 syslog(dbg, flog, "tftpd %d send file '%s' %s to %s",
570 pid, name, mode, raddr);
574 file = open(name, OREAD);
576 errstr(errbuf, sizeof errbuf);
584 * if we sent an oack previously, wait for the client's ack or error.
585 * if we get no ack for our oack, it could be that we returned
586 * a tsize that the client can't handle, or it could be intel
587 * pxe just read-with-tsize to get size, couldn't be bothered to
588 * ack our oack and has just gone ahead and issued another read.
590 if(opts && awaitack(fd, 0) != Ackok)
593 for(txtry = 0; txtry < timeout;) {
594 if(rexmit == Ackok) {
600 n = read(file, buf+Hdrsize, blksize);
602 errstr(errbuf, sizeof errbuf);
609 syslog(dbg, flog, "tftpd %d rexmit %d %s:%d to %s",
610 pid, Hdrsize+n, name, block, raddr);
614 ret = write(fd, buf, Hdrsize+n);
615 if(ret < Hdrsize+n) {
617 "tftpd network write error on %s to %s: %r",
619 sysfatal("tftpd: network write error: %r");
622 syslog(dbg, flog, "tftpd %d sent block %d", pid, block);
624 rexmit = awaitack(fd, block);
625 if (rexmit == Ackerr)
627 if(ret != blksize+Hdrsize && rexmit == Ackok)
630 syslog(dbg, flog, "tftpd %d done sending file '%s' %s to %s",
631 pid, name, mode, raddr);
638 recvfile(int fd, char *name, char *mode)
640 ushort op, block, inblock;
641 uchar buf[Maxsegsize+8];
645 syslog(dbg, flog, "receive file '%s' %s from %s", name, mode, raddr);
647 file = create(name, OWRITE, 0666);
649 errstr(errbuf, sizeof errbuf);
651 syslog(dbg, flog, "can't create %s: %s", name, errbuf);
661 n = read(fd, buf, blksize+8);
664 syslog(dbg, flog, "tftpd: network error reading %s: %r",
669 * NB: not `<='; just a header is legal and happens when
670 * file being read is a multiple of segment-size bytes long.
674 "tftpd: short read from network, reading %s",
678 op = buf[0]<<8|buf[1];
679 if(op == Tftp_ERROR) {
680 syslog(dbg, flog, "tftpd: tftp error reading %s", name);
685 inblock = buf[2]<<8|buf[3];
686 if(op == Tftp_DATA) {
687 if(inblock == block) {
688 ret = write(file, buf+Hdrsize, n);
690 errstr(errbuf, sizeof errbuf);
693 "tftpd: error writing %s: %s",
700 ack(fd, 0xffff); /* tell him to resend */
708 ack(int fd, ushort block)
718 n = write(fd, ack, 4);
720 sysfatal("network write: %r");
724 nak(int fd, int code, char *msg)
736 memmove(buf+4, msg, n - 5);
738 if(write(fd, buf, n) != n)
739 sysfatal("write nak: %r");
745 if(procsetuser("none") < 0)
746 sysfatal("can't become none: %r");
747 if(newns("none", nsfile) < 0)
748 sysfatal("can't build namespace: %r");
752 remoteaddr(char *dir, char *raddr, int len)
757 snprint(buf, sizeof(buf), "%s/remote", dir);
758 fd = open(buf, OREAD);
760 snprint(raddr, sizeof(raddr), "unknown");
763 n = read(fd, raddr, len-1);
766 snprint(raddr, sizeof(raddr), "unknown");