2 * tftpd - tftp service, see /lib/rfc/rfc783 (now rfc1350 + 234[789])
17 Opsize= sizeof(short),
18 Blksize= sizeof(short),
19 Hdrsize= Opsize + Blksize,
31 Tftp_OACK = 6, /* option acknowledge */
33 Errnotdef = 0, /* see textual error instead */
41 Errbadopt = 8, /* really bad option value */
44 Maxsegsize = 65464, /* from rfc2348 */
47 * bandt (viaduct) tunnels use smaller mtu than ether's
48 * (1400 bytes for tcp mss of 1300 bytes).
52 * maximum size of block's data content, excludes hdrs,
53 * notably IP/UDP and TFTP, using worst-case (IPv6) sizes.
55 Bandtblksz = Bandtmtu - 40 - 8,
56 Bcavium = 1432, /* cavium's u-boot demands this size */
59 typedef struct Opt Opt;
62 int *valp; /* set to client's value if within bounds */
72 int blksize = Defsegsize; /* excluding 4-byte header */
73 int timeout = 5; /* seconds */
75 static Opt option[] = {
76 "timeout", &timeout, 1, 255,
77 /* see "hack" below */
78 "blksize", &blksize, 8, Maxsegsize,
79 "tsize", &tsize, 0, ~0UL >> 1,
82 void sendfile(int, char*, char*, int);
83 void recvfile(int, char*, char*);
84 void nak(int, int, char*);
85 void ack(int, ushort);
88 char* sunkernel(char*);
89 void remoteaddr(char*, char*, int);
95 char *dir = "/lib/tftpd";
98 char flog[] = "ipboot";
101 static char *opnames[] = {
103 [Tftp_WRITE] "write",
106 [Tftp_ERROR] "error",
113 fprint(2, "usage: %s [-dr] [-h homedir] [-s svc] [-x netmtpt]\n",
119 main(int argc, char **argv)
122 char adir[64], ldir[64];
126 setnetmtpt(net, sizeof net, nil);
132 dir = EARGF(usage());
138 svc = EARGF(usage());
141 setnetmtpt(net, sizeof net, EARGF(usage()));
147 snprint(buf, sizeof buf, "%s/", dir);
149 dirsllen = strlen(dirsl);
151 fmtinstall('E', eipfmt);
152 fmtinstall('I', eipfmt);
155 * setuser calls newns, and typical /lib/namespace files contain
156 * "cd /usr/$user", so call setuser before chdir.
160 sysfatal("can't get to directory %s: %r", dir);
163 switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
165 sysfatal("fork: %r");
172 snprint(buf, sizeof buf, "%s/udp!*!%s", net, svc);
173 cfd = announce(buf, adir);
175 sysfatal("announcing on %s: %r", buf);
176 syslog(dbg, flog, "tftpd started on %s dir %s", buf, adir);
179 lcfd = listen(adir, ldir);
181 sysfatal("listening on %s: %r", adir);
185 sysfatal("fork: %r");
187 dfd = accept(lcfd, ldir);
190 remoteaddr(ldir, raddr, sizeof(raddr));
192 syslog(0, flog, "tftp %d connection from %s dir %s",
205 handleopt(int fd, char *name, char *val)
210 for (op = option; op < option + nelem(option); op++)
211 if(cistrcmp(name, op->name) == 0) {
212 n = strtol(val, nil, 10);
213 if (n < op->min || n > op->max) {
214 nak(fd, Errbadopt, "option value out of range");
215 syslog(dbg, flog, "tftp bad option value from "
216 "client: %s %s", name, val);
217 sysfatal("bad option value from client: %s %s",
221 /* incoming 0 for tsize is uninteresting */
222 if(cistrcmp("tsize", op->name) != 0)
223 syslog(dbg, flog, "tftpd %d setting %s to client's %d",
244 /* copy word into bp iff it fits before ep, returns bytes to advance bp. */
246 emits(char *word, char *bp, char *ep)
250 len = strlen(word) + 1;
257 /* format number into bp iff it fits before ep. */
259 emitn(vlong n, char *bp, char *ep)
263 snprint(numb, sizeof numb, "%lld", n);
264 return emits(numb, bp, ep);
268 * send an OACK packet to respond to options. bail early with -1 on error.
269 * p is the packet containing the options.
271 * hack: bandt (viaducts) uses smaller mtu than ether's
272 * (1400 bytes for tcp mss of 1300 bytes),
273 * so offer at most bandt's mtu minus headers,
274 * to avoid failure of pxe booting via viaduct.
275 * there's an exception for the cavium's u-boot.
278 options(int fd, char *buf, int bufsz, char *file, ushort oper, char *p, int dlen)
280 int nmlen, vallen, olen, nopts;
290 for (; dlen > 0 && *p != '\0'; p = val + vallen, bp += olen) {
291 nmlen = strlen(p) + 1; /* include NUL */
296 if (dlen <= 0 || *val == '\0')
299 vallen = strlen(val) + 1;
306 op = handleopt(fd, p, val);
310 /* append OACK response to buf */
311 nmlen = emits(p, bp, ep); /* option name */
316 if (oper == Tftp_READ && cistrcmp(p, "tsize") == 0) {
317 size = filesize(file);
319 nak(fd, Errnotfound, "no such file");
320 syslog(dbg, flog, "tftpd tsize for "
321 "non-existent file %s", file);
323 // olen = emits("0", bp, ep);
327 olen = emitn(size, bp, ep);
328 syslog(dbg, flog, "tftpd %d %s tsize is %,lld",
330 } else if (oper == Tftp_READ && cistrcmp(p, "blksize") == 0 &&
331 blksize > Bandtblksz && blksize != Bcavium) {
332 *op->valp = blksize = Bandtblksz;
333 olen = emitn(blksize, bp, ep);
334 syslog(dbg, flog, "tftpd %d overriding blksize to %d",
337 olen = emits(val, bp, ep); /* use requested value */
340 return 0; /* no options actually seen */
342 if (write(fd, buf, bp - buf) < bp - buf) {
343 syslog(dbg, flog, "tftpd network write error on oack to %s: %r",
345 sysfatal("tftpd: network write error: %r");
348 syslog(dbg, flog, "tftpd oack: options to %s", raddr);
353 optlog(char *bytes, char *p, int dlen)
358 sprint(bp, "tftpd %d option bytes: ", dlen);
360 for (; dlen > 0; dlen--, p++)
363 syslog(dbg, flog, "%s", bytes);
367 * replace one occurrence of %[ICE] with ip, cfgpxe name, or ether mac, resp.
368 * we can't easily use $ because u-boot has stranger quoting rules than sh.
374 char *p, *newnm, *cur, *arpf, *ln, *remip, *bang;
378 p = strchr(file, '%');
379 if (p == nil || p[1] == '\0')
382 remip = strdup(raddr);
383 newnm = mallocz(strlen(file) + Maxpath, 1);
384 if (remip == nil || newnm == nil)
385 sysfatal("out of memory");
387 bang = strchr(remip, '!');
389 *bang = '\0'; /* remove !port */
391 memmove(newnm, file, p - file); /* copy up to % */
392 cur = newnm + strlen(newnm);
395 strcpy(cur, remip); /* remote's IP */
398 strcpy(cur, "/cfg/pxe/");
402 /* look up remote's IP in /net/arp to get mac. */
403 arpf = smprint("%s/arp", net);
404 arp = Bopen(arpf, OREAD);
408 /* read lines looking for remip in 3rd field of 4 */
409 while ((ln = Brdline(arp, '\n')) != nil) {
410 ln[Blinelen(arp)-1] = 0;
411 nf = tokenize(ln, fields, nelem(fields));
412 if (nf >= 4 && strcmp(fields[2], remip) == 0) {
413 strcpy(cur, fields[3]);
420 strcat(newnm, p + 2); /* tail following %x */
429 char *mode, *p, *file;
432 dlen = read(fd, bigbuf, sizeof(bigbuf)-1);
434 sysfatal("listen read: %r");
437 op = (bigbuf[0]<<8) | bigbuf[1];
439 mode = file = bigbuf + Opsize;
440 while(*mode != '\0' && dlen--)
447 file = mapname(file); /* we don't free the result; minor leak */
450 nak(fd, 0, "bad tftpmode");
452 syslog(dbg, flog, "tftpd %d bad mode %s for file %s from %s",
453 pid, mode, file, raddr);
457 if(op != Tftp_READ && op != Tftp_WRITE) {
458 nak(fd, Errbadop, "Illegal TFTP operation");
460 syslog(dbg, flog, "tftpd %d bad request %d (%s) %s", pid, op,
461 (op < nelem(opnames)? opnames[op]: "gok"), raddr);
466 if(file[0] == '#' || strncmp(file, "../", 3) == 0 ||
467 strstr(file, "/../") != nil ||
468 (file[0] == '/' && strncmp(file, dirsl, dirsllen) != 0)){
469 nak(fd, Errnoaccess, "Permission denied");
471 syslog(dbg, flog, "tftpd %d bad request %d from %s file %s",
472 pid, op, raddr, file);
478 * options are supposed to be negotiated, but the cavium board's
479 * u-boot really wants us to use a block size of 1432 bytes and won't
480 * take `no' for an answer.
482 p++; /* skip NUL after mode */
485 if(dlen > 0) { /* might have options */
489 optlog(bytes, p, dlen);
490 opts = options(fd, bytes, sizeof bytes, file, op, p, dlen);
495 sendfile(fd, file, mode, opts);
497 recvfile(fd, file, mode);
501 catcher(void *junk, char *msg)
505 if(strncmp(msg, "exit", 4) == 0)
511 awaitack(int fd, int block)
513 int ackblock, al, rxl;
517 for(rxl = 0; rxl < 10; rxl++) {
518 memset(ack, 0, Hdrsize);
520 al = read(fd, ack, sizeof(ack));
524 syslog(dbg, flog, "tftpd %d timed out "
525 "waiting for ack from %s", pid, raddr);
528 op = ack[0]<<8|ack[1];
529 if(op == Tftp_ERROR) {
531 syslog(dbg, flog, "tftpd %d got error "
532 "waiting for ack from %s", pid, raddr);
534 } else if(op != Tftp_ACK) {
535 syslog(dbg, flog, "tftpd %d rcvd %s op from %s", pid,
536 (op < nelem(opnames)? opnames[op]: "gok"),
540 ackblock = ack[2]<<8|ack[3];
542 syslog(dbg, flog, "tftpd %d read ack of %d bytes "
543 "for block %d", pid, al, ackblock);
544 if(ackblock == block)
545 return Ackok; /* for block just sent */
546 else if(ackblock == block + 1) /* intel pxe eof bug */
548 else if(ackblock == 0xffff)
551 /* ack is for some other block; ignore it, try again */
552 syslog(dbg, flog, "tftpd %d expected ack for block %d, "
553 "got %d", pid, block, ackblock);
559 sendfile(int fd, char *name, char *mode, int opts)
561 int file, block, ret, rexmit, n, txtry;
562 uchar buf[Maxsegsize+Hdrsize];
566 syslog(dbg, flog, "tftpd %d send file '%s' %s to %s",
567 pid, name, mode, raddr);
568 name = sunkernel(name);
570 nak(fd, 0, "not in our database");
576 file = open(name, OREAD);
578 errstr(errbuf, sizeof errbuf);
586 * if we sent an oack previously, wait for the client's ack or error.
587 * if we get no ack for our oack, it could be that we returned
588 * a tsize that the client can't handle, or it could be intel
589 * pxe just read-with-tsize to get size, couldn't be bothered to
590 * ack our oack and has just gone ahead and issued another read.
592 if(opts && awaitack(fd, 0) != Ackok)
595 for(txtry = 0; txtry < timeout;) {
596 if(rexmit == Ackok) {
602 n = read(file, buf+Hdrsize, blksize);
604 errstr(errbuf, sizeof errbuf);
611 syslog(dbg, flog, "tftpd %d rexmit %d %s:%d to %s",
612 pid, Hdrsize+n, name, block, raddr);
616 ret = write(fd, buf, Hdrsize+n);
617 if(ret < Hdrsize+n) {
619 "tftpd network write error on %s to %s: %r",
621 sysfatal("tftpd: network write error: %r");
624 syslog(dbg, flog, "tftpd %d sent block %d", pid, block);
626 rexmit = awaitack(fd, block);
627 if (rexmit == Ackerr)
629 if(ret != blksize+Hdrsize && rexmit == Ackok)
632 syslog(dbg, flog, "tftpd %d done sending file '%s' %s to %s",
633 pid, name, mode, raddr);
640 recvfile(int fd, char *name, char *mode)
642 ushort op, block, inblock;
643 uchar buf[Maxsegsize+8];
647 syslog(dbg, flog, "receive file '%s' %s from %s", name, mode, raddr);
649 file = create(name, OWRITE, 0666);
651 errstr(errbuf, sizeof errbuf);
653 syslog(dbg, flog, "can't create %s: %s", name, errbuf);
663 n = read(fd, buf, blksize+8);
666 syslog(dbg, flog, "tftpd: network error reading %s: %r",
671 * NB: not `<='; just a header is legal and happens when
672 * file being read is a multiple of segment-size bytes long.
676 "tftpd: short read from network, reading %s",
680 op = buf[0]<<8|buf[1];
681 if(op == Tftp_ERROR) {
682 syslog(dbg, flog, "tftpd: tftp error reading %s", name);
687 inblock = buf[2]<<8|buf[3];
688 if(op == Tftp_DATA) {
689 if(inblock == block) {
690 ret = write(file, buf+Hdrsize, n);
692 errstr(errbuf, sizeof errbuf);
695 "tftpd: error writing %s: %s",
702 ack(fd, 0xffff); /* tell him to resend */
710 ack(int fd, ushort block)
720 n = write(fd, ack, 4);
722 sysfatal("network write: %r");
726 nak(int fd, int code, char *msg)
738 memmove(buf+4, msg, n - 5);
740 if(write(fd, buf, n) != n)
741 sysfatal("write nak: %r");
749 fd = open("#c/user", OWRITE);
750 if(fd < 0 || write(fd, "none", strlen("none")) < 0)
751 sysfatal("can't become none: %r");
753 if(newns("none", nil) < 0)
754 sysfatal("can't build namespace: %r");
758 lookup(char *sattr, char *sval, char *tattr, char *tval, int len)
770 sattr = ipattr(sval);
773 t = ndbipinfo(db, sattr, sval, attrs, 1);
776 strncpy(tval, t->val, len);
783 * for sun kernel boots, replace the requested file name with
784 * a one from our database. If the database doesn't specify a file,
788 sunkernel(char *name)
791 uchar v4[IPv4addrlen];
797 addr = strtoul(name, &suffix, 16);
798 if(suffix-name != 8 || (strcmp(suffix, "") != 0 && strcmp(suffix, ".SUN") != 0))
806 sprint(ipbuf, "%I", v6);
807 return lookup("ip", ipbuf, "bootf", buf, sizeof buf);
811 remoteaddr(char *dir, char *raddr, int len)
816 snprint(buf, sizeof(buf), "%s/remote", dir);
817 fd = open(buf, OREAD);
819 snprint(raddr, sizeof(raddr), "unknown");
822 n = read(fd, raddr, len-1);
825 snprint(raddr, sizeof(raddr), "unknown");