2 * tftpd - tftp service, see /lib/rfc/rfc783 (now rfc1350 + 234[789])
18 Opsize= sizeof(short),
19 Blksize= sizeof(short),
20 Hdrsize= Opsize + Blksize,
32 Tftp_OACK = 6, /* option acknowledge */
34 Errnotdef = 0, /* see textual error instead */
42 Errbadopt = 8, /* really bad option value */
45 Maxsegsize = 65464, /* from rfc2348 */
48 * bandt (viaduct) tunnels use smaller mtu than ether's
49 * (1400 bytes for tcp mss of 1300 bytes).
53 * maximum size of block's data content, excludes hdrs,
54 * notably IP/UDP and TFTP, using worst-case (IPv6) sizes.
56 Bandtblksz = Bandtmtu - 40 - 8,
57 Bcavium = 1432, /* cavium's u-boot demands this size */
60 typedef struct Opt Opt;
63 int *valp; /* set to client's value if within bounds */
73 int blksize = Defsegsize; /* excluding 4-byte header */
74 int timeout = 5; /* seconds */
76 static Opt option[] = {
77 "timeout", &timeout, 1, 255,
78 /* see "hack" below */
79 "blksize", &blksize, 8, Maxsegsize,
80 "tsize", &tsize, 0, ~0UL >> 1,
83 void sendfile(int, char*, char*, int);
84 void recvfile(int, char*, char*);
85 void nak(int, int, char*);
86 void ack(int, ushort);
89 char* sunkernel(char*);
90 void remoteaddr(char*, char*, int);
96 char *dir = "/lib/tftpd";
99 char flog[] = "ipboot";
102 static char *opnames[] = {
104 [Tftp_WRITE] "write",
107 [Tftp_ERROR] "error",
114 fprint(2, "usage: %s [-dr] [-h homedir] [-s svc] [-x netmtpt]\n",
120 main(int argc, char **argv)
123 char adir[64], ldir[64];
127 setnetmtpt(net, sizeof net, nil);
133 dir = EARGF(usage());
139 svc = EARGF(usage());
142 setnetmtpt(net, sizeof net, EARGF(usage()));
148 snprint(buf, sizeof buf, "%s/", dir);
150 dirsllen = strlen(dirsl);
152 fmtinstall('E', eipfmt);
153 fmtinstall('I', eipfmt);
156 * setuser calls newns, and typical /lib/namespace files contain
157 * "cd /usr/$user", so call setuser before chdir.
161 sysfatal("can't get to directory %s: %r", dir);
164 switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
166 sysfatal("fork: %r");
173 snprint(buf, sizeof buf, "%s/udp!*!%s", net, svc);
174 cfd = announce(buf, adir);
176 sysfatal("announcing on %s: %r", buf);
177 syslog(dbg, flog, "tftpd started on %s dir %s", buf, adir);
180 lcfd = listen(adir, ldir);
182 sysfatal("listening on %s: %r", adir);
186 sysfatal("fork: %r");
188 dfd = accept(lcfd, ldir);
191 remoteaddr(ldir, raddr, sizeof(raddr));
193 syslog(0, flog, "tftp %d connection from %s dir %s",
206 handleopt(int fd, char *name, char *val)
211 for (op = option; op < option + nelem(option); op++)
212 if(cistrcmp(name, op->name) == 0) {
213 n = strtol(val, nil, 10);
214 if (n < op->min || n > op->max) {
215 nak(fd, Errbadopt, "option value out of range");
216 syslog(dbg, flog, "tftp bad option value from "
217 "client: %s %s", name, val);
218 sysfatal("bad option value from client: %s %s",
222 /* incoming 0 for tsize is uninteresting */
223 if(cistrcmp("tsize", op->name) != 0)
224 syslog(dbg, flog, "tftpd %d setting %s to client's %d",
245 /* copy word into bp iff it fits before ep, returns bytes to advance bp. */
247 emits(char *word, char *bp, char *ep)
251 len = strlen(word) + 1;
258 /* format number into bp iff it fits before ep. */
260 emitn(vlong n, char *bp, char *ep)
264 snprint(numb, sizeof numb, "%lld", n);
265 return emits(numb, bp, ep);
269 * send an OACK packet to respond to options. bail early with -1 on error.
270 * p is the packet containing the options.
272 * hack: bandt (viaducts) uses smaller mtu than ether's
273 * (1400 bytes for tcp mss of 1300 bytes),
274 * so offer at most bandt's mtu minus headers,
275 * to avoid failure of pxe booting via viaduct.
276 * there's an exception for the cavium's u-boot.
279 options(int fd, char *buf, int bufsz, char *file, ushort oper, char *p, int dlen)
281 int nmlen, vallen, olen, nopts;
291 for (; dlen > 0 && *p != '\0'; p = val + vallen, bp += olen) {
292 nmlen = strlen(p) + 1; /* include NUL */
297 if (dlen <= 0 || *val == '\0')
300 vallen = strlen(val) + 1;
307 op = handleopt(fd, p, val);
311 /* append OACK response to buf */
312 nmlen = emits(p, bp, ep); /* option name */
317 if (oper == Tftp_READ && cistrcmp(p, "tsize") == 0) {
318 size = filesize(file);
320 nak(fd, Errnotfound, "no such file");
321 syslog(dbg, flog, "tftpd tsize for "
322 "non-existent file %s", file);
324 // olen = emits("0", bp, ep);
328 olen = emitn(size, bp, ep);
329 syslog(dbg, flog, "tftpd %d %s tsize is %,lld",
331 } else if (oper == Tftp_READ && cistrcmp(p, "blksize") == 0 &&
332 blksize > Bandtblksz && blksize != Bcavium) {
333 *op->valp = blksize = Bandtblksz;
334 olen = emitn(blksize, bp, ep);
335 syslog(dbg, flog, "tftpd %d overriding blksize to %d",
338 olen = emits(val, bp, ep); /* use requested value */
341 return 0; /* no options actually seen */
346 *bp++ = '\0'; /* overkill */
349 if (write(fd, buf, bp - buf) < bp - buf) {
350 syslog(dbg, flog, "tftpd network write error on oack to %s: %r",
352 sysfatal("tftpd: network write error: %r");
355 syslog(dbg, flog, "tftpd oack: options to %s", raddr);
360 optlog(char *bytes, char *p, int dlen)
365 sprint(bp, "tftpd %d option bytes: ", dlen);
367 for (; dlen > 0; dlen--, p++)
370 syslog(dbg, flog, "%s", bytes);
374 * replace one occurrence of %[ICE] with ip, cfgpxe name, or ether mac, resp.
375 * we can't easily use $ because u-boot has stranger quoting rules than sh.
381 char *p, *newnm, *cur, *arpf, *ln, *remip, *bang;
385 p = strchr(file, '%');
386 if (p == nil || p[1] == '\0')
389 remip = strdup(raddr);
390 newnm = mallocz(strlen(file) + Maxpath, 1);
391 if (remip == nil || newnm == nil)
392 sysfatal("out of memory");
394 bang = strchr(remip, '!');
396 *bang = '\0'; /* remove !port */
398 memmove(newnm, file, p - file); /* copy up to % */
399 cur = newnm + strlen(newnm);
402 strcpy(cur, remip); /* remote's IP */
405 strcpy(cur, "/cfg/pxe/");
409 /* look up remote's IP in /net/arp to get mac. */
410 arpf = smprint("%s/arp", net);
411 arp = Bopen(arpf, OREAD);
415 /* read lines looking for remip in 3rd field of 4 */
416 while ((ln = Brdline(arp, '\n')) != nil) {
417 ln[Blinelen(arp)-1] = 0;
418 nf = tokenize(ln, fields, nelem(fields));
419 if (nf >= 4 && strcmp(fields[2], remip) == 0) {
420 strcpy(cur, fields[3]);
427 strcat(newnm, p + 2); /* tail following %x */
436 char *mode, *p, *file;
439 dlen = read(fd, bigbuf, sizeof(bigbuf)-1);
441 sysfatal("listen read: %r");
444 op = (bigbuf[0]<<8) | bigbuf[1];
446 mode = file = bigbuf + Opsize;
447 while(*mode != '\0' && dlen--)
454 file = mapname(file); /* we don't free the result; minor leak */
457 nak(fd, 0, "bad tftpmode");
459 syslog(dbg, flog, "tftpd %d bad mode %s for file %s from %s",
460 pid, mode, file, raddr);
464 if(op != Tftp_READ && op != Tftp_WRITE) {
465 nak(fd, Errbadop, "Illegal TFTP operation");
467 syslog(dbg, flog, "tftpd %d bad request %d (%s) %s", pid, op,
468 (op < nelem(opnames)? opnames[op]: "gok"), raddr);
473 if(file[0] == '#' || strncmp(file, "../", 3) == 0 ||
474 strstr(file, "/../") != nil ||
475 (file[0] == '/' && strncmp(file, dirsl, dirsllen) != 0)){
476 nak(fd, Errnoaccess, "Permission denied");
478 syslog(dbg, flog, "tftpd %d bad request %d from %s file %s",
479 pid, op, raddr, file);
485 * options are supposed to be negotiated, but the cavium board's
486 * u-boot really wants us to use a block size of 1432 bytes and won't
487 * take `no' for an answer.
489 p++; /* skip NUL after mode */
492 if(dlen > 0) { /* might have options */
496 optlog(bytes, p, dlen);
497 opts = options(fd, bytes, sizeof bytes, file, op, p, dlen);
502 sendfile(fd, file, mode, opts);
504 recvfile(fd, file, mode);
508 catcher(void *junk, char *msg)
512 if(strncmp(msg, "exit", 4) == 0)
518 awaitack(int fd, int block)
520 int ackblock, al, rxl;
524 for(rxl = 0; rxl < 10; rxl++) {
525 memset(ack, 0, Hdrsize);
527 al = read(fd, ack, sizeof(ack));
531 syslog(dbg, flog, "tftpd %d timed out "
532 "waiting for ack from %s", pid, raddr);
535 op = ack[0]<<8|ack[1];
536 if(op == Tftp_ERROR) {
538 syslog(dbg, flog, "tftpd %d got error "
539 "waiting for ack from %s", pid, raddr);
541 } else if(op != Tftp_ACK) {
542 syslog(dbg, flog, "tftpd %d rcvd %s op from %s", pid,
543 (op < nelem(opnames)? opnames[op]: "gok"),
547 ackblock = ack[2]<<8|ack[3];
549 syslog(dbg, flog, "tftpd %d read ack of %d bytes "
550 "for block %d", pid, al, ackblock);
551 if(ackblock == block)
552 return Ackok; /* for block just sent */
553 else if(ackblock == block + 1) /* intel pxe eof bug */
555 else if(ackblock == 0xffff)
558 /* ack is for some other block; ignore it, try again */
559 syslog(dbg, flog, "tftpd %d expected ack for block %d, "
560 "got %d", pid, block, ackblock);
566 sendfile(int fd, char *name, char *mode, int opts)
568 int file, block, ret, rexmit, n, txtry;
569 uchar buf[Maxsegsize+Hdrsize];
573 syslog(dbg, flog, "tftpd %d send file '%s' %s to %s",
574 pid, name, mode, raddr);
575 name = sunkernel(name);
577 nak(fd, 0, "not in our database");
583 file = open(name, OREAD);
585 errstr(errbuf, sizeof errbuf);
593 * if we sent an oack previously, wait for the client's ack or error.
594 * if we get no ack for our oack, it could be that we returned
595 * a tsize that the client can't handle, or it could be intel
596 * pxe just read-with-tsize to get size, couldn't be bothered to
597 * ack our oack and has just gone ahead and issued another read.
599 if(opts && awaitack(fd, 0) != Ackok)
602 for(txtry = 0; txtry < timeout;) {
603 if(rexmit == Ackok) {
609 n = read(file, buf+Hdrsize, blksize);
611 errstr(errbuf, sizeof errbuf);
618 syslog(dbg, flog, "tftpd %d rexmit %d %s:%d to %s",
619 pid, Hdrsize+n, name, block, raddr);
623 ret = write(fd, buf, Hdrsize+n);
624 if(ret < Hdrsize+n) {
626 "tftpd network write error on %s to %s: %r",
628 sysfatal("tftpd: network write error: %r");
631 syslog(dbg, flog, "tftpd %d sent block %d", pid, block);
633 rexmit = awaitack(fd, block);
634 if (rexmit == Ackerr)
636 if(ret != blksize+Hdrsize && rexmit == Ackok)
639 syslog(dbg, flog, "tftpd %d done sending file '%s' %s to %s",
640 pid, name, mode, raddr);
647 recvfile(int fd, char *name, char *mode)
649 ushort op, block, inblock;
650 uchar buf[Maxsegsize+8];
654 syslog(dbg, flog, "receive file '%s' %s from %s", name, mode, raddr);
656 file = create(name, OWRITE, 0666);
658 errstr(errbuf, sizeof errbuf);
660 syslog(dbg, flog, "can't create %s: %r", name);
670 n = read(fd, buf, blksize+8);
673 syslog(dbg, flog, "tftpd: network error reading %s: %r",
678 * NB: not `<='; just a header is legal and happens when
679 * file being read is a multiple of segment-size bytes long.
683 "tftpd: short read from network, reading %s",
687 op = buf[0]<<8|buf[1];
688 if(op == Tftp_ERROR) {
689 syslog(dbg, flog, "tftpd: tftp error reading %s", name);
694 inblock = buf[2]<<8|buf[3];
695 if(op == Tftp_DATA) {
696 if(inblock == block) {
697 ret = write(file, buf+Hdrsize, n);
699 errstr(errbuf, sizeof errbuf);
702 "tftpd: error writing %s: %s",
709 ack(fd, 0xffff); /* tell him to resend */
717 ack(int fd, ushort block)
727 n = write(fd, ack, 4);
729 sysfatal("network write: %r");
733 nak(int fd, int code, char *msg)
743 n = strlen(msg) + 4 + 1;
744 if(write(fd, buf, n) < n)
745 sysfatal("write nak: %r");
753 fd = open("#c/user", OWRITE);
754 if(fd < 0 || write(fd, "none", strlen("none")) < 0)
755 sysfatal("can't become none: %r");
757 if(newns("none", nil) < 0)
758 sysfatal("can't build namespace: %r");
762 lookup(char *sattr, char *sval, char *tattr, char *tval, int len)
774 sattr = ipattr(sval);
777 t = ndbipinfo(db, sattr, sval, attrs, 1);
780 strncpy(tval, t->val, len);
787 * for sun kernel boots, replace the requested file name with
788 * a one from our database. If the database doesn't specify a file,
792 sunkernel(char *name)
795 uchar v4[IPv4addrlen];
801 addr = strtoul(name, &suffix, 16);
802 if(suffix-name != 8 || (strcmp(suffix, "") != 0 && strcmp(suffix, ".SUN") != 0))
810 sprint(ipbuf, "%I", v6);
811 return lookup("ip", ipbuf, "bootf", buf, sizeof buf);
815 remoteaddr(char *dir, char *raddr, int len)
820 snprint(buf, sizeof(buf), "%s/remote", dir);
821 fd = open(buf, OREAD);
823 snprint(raddr, sizeof(raddr), "unknown");
826 n = read(fd, raddr, len-1);
829 snprint(raddr, sizeof(raddr), "unknown");