3 * Report MTU on path to specified host
5 * (c) 2008 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_icmp.h>
50 #include <netinet/udp.h>
54 #include <sys/ioctl.h>
56 #include <mLib/alloc.h>
57 #include <mLib/bits.h>
58 #include <mLib/dstr.h>
60 #include <mLib/mdwopt.h>
61 #include <mLib/quis.h>
62 #include <mLib/report.h>
65 /*----- Static variables --------------------------------------------------*/
67 static unsigned char buf[65536];
71 /*----- Utility functions -------------------------------------------------*/
73 /* Step a value according to a simple LFSR. */
75 do (q) = ((q) & 0x8000) ? ((q) << 1) ^ POLY : ((q) << 1); while (0)
77 /* Fill buffer with a constant but pseudorandom string. Uses a simple
80 static void fillbuffer(unsigned char *p, size_t sz)
82 unsigned int y = 0xbc20;
83 const unsigned char *l = p + sz;
88 for (i = 0; i < 8; i++) STEP(y);
92 /* Convert a string to floating point. */
93 static double s2f(const char *s, const char *what)
100 if (errno || *q) die(EXIT_FAILURE, "bad %s", what);
104 /* Convert a floating-point value into a struct timeval. */
105 static void f2tv(struct timeval *tv, double t)
106 { tv->tv_sec = t; tv->tv_usec = (t - tv->tv_sec)*MILLION; }
108 /*----- Main algorithm skeleton -------------------------------------------*/
111 unsigned f; /* Various flags */
112 #define F_VERBOSE 1u /* Give a running commentary */
113 double retx; /* Initial retransmit interval */
114 double regr; /* Retransmit growth factor */
115 double timeout; /* Retransmission timeout */
116 int seqoff; /* Offset to write sequence number */
117 const struct probe_ops *pops; /* Probe algorithm description */
118 struct sockaddr_in sin; /* Destination address */
122 const struct param *pp;
128 const struct probe_ops *next;
130 int (*setup)(void *, int, const struct param *);
131 void (*finish)(void *);
132 void (*selprep)(void *, int *, fd_set *);
133 int (*xmit)(void *, int);
134 int (*selproc)(void *, fd_set *, struct probestate *);
145 /* or a positive MTU upper-bound */
148 /* Add a file descriptor FD to the set `fd_in', updating `*maxfd'. */
150 do { FD_SET(fd, fd_in); if (*maxfd < fd) *maxfd = fd; } while (0)
152 /* Check whether a buffer contains a packet from our current probe. */
153 static int mypacketp(struct probestate *ps,
154 const unsigned char *p, size_t sz)
156 const struct param *pp = ps->pp;
158 return (sz >= pp->seqoff + 2 && LOAD16(p + pp->seqoff) == ps->q);
161 /* See whether MTU is an acceptable MTU value. Return an appropriate
162 * RC_... code or a new suggested MTU.
164 static int probe(struct probestate *ps, void *st, int mtu)
166 const struct param *pp = ps->pp;
168 struct timeval tv, now, when, done;
169 double timer = pp->retx;
172 /* Set up the first retransmit and give-up timers. */
173 gettimeofday(&now, 0);
174 f2tv(&tv, pp->timeout); TV_ADD(&done, &now, &tv);
175 f2tv(&tv, timer); TV_ADD(&when, &now, &tv);
176 if (TV_CMP(&when, >, &done)) when = done;
178 /* Send the initial probe. */
179 if (pp->f & F_VERBOSE)
180 moan("sending probe of size %d (seq = %04x)", mtu, ps->q);
182 STORE16(buf + pp->seqoff, ps->q);
183 if ((rc = pp->pops->xmit(st, mtu)) != RC_OK) return (rc);
187 /* Wait for something interesting to happen. */
188 maxfd = 0; FD_ZERO(&fd_in);
189 pp->pops->selprep(st, &maxfd, &fd_in);
190 TV_SUB(&tv, &when, &now);
191 if (select(maxfd + 1, &fd_in, 0, 0, &tv) < 0) return (RC_FAIL);
192 gettimeofday(&now, 0);
194 /* See whether the probe method has any answers for us. */
195 if ((rc = pp->pops->selproc(st, &fd_in, ps)) != RC_OK) return (rc);
197 /* If we've waited too long, give up. If we should retransmit, do
200 if (TV_CMP(&now, >, &done))
202 else if (TV_CMP(&now, >, &when)) {
203 if (pp->f & F_VERBOSE) moan("re-sending probe of size %d", mtu);
204 if ((rc = pp->pops->xmit(st, mtu)) != RC_OK) return (rc);
206 timer *= pp->regr; f2tv(&tv, timer); TV_ADD(&when, &when, &tv);
207 } while (TV_CMP(&when, <, &now));
208 if (TV_CMP(&when, >, &done)) when = done;
213 /* Discover the path MTU to the destination address. */
214 static int pathmtu(const struct param *pp)
220 struct probestate ps;
222 /* Build and connect a UDP socket. We'll need this to know the local port
223 * number to use if nothing else. Set other stuff up.
225 if ((sk = socket(PF_INET, SOCK_DGRAM, 0)) < 0) goto fail_0;
226 if (connect(sk, (struct sockaddr *)&pp->sin, sizeof(pp->sin))) goto fail_1;
227 st = xmalloc(pp->pops->statesz);
228 if ((mtu = pp->pops->setup(st, sk, pp)) < 0) goto fail_2;
229 ps.pp = pp; ps.q = rand() & 0xffff;
232 /* And now we do a thing which is sort of like a binary search, except that
233 * we also take explicit clues as establishing a new upper bound, and we
234 * try to hug that initially.
237 assert(lo <= mtu && mtu <= hi);
238 if (pp->f & F_VERBOSE) moan("probe: %d <= %d <= %d", lo, mtu, hi);
239 rc = probe(&ps, st, mtu);
243 if (pp->f & F_VERBOSE) moan("probe failed");
247 /* If we've not seen a dropped packet before then we don't know what
248 * this means yet -- in particular, we don't know which bit of the
249 * network is swallowing packets. Send a minimum-size probe. If
250 * that doesn't come back then assume that the remote host is
251 * swallowing our packets. If it does, then we assume that dropped
252 * packets are a result of ICMP fragmentation-needed reports being
253 * lost or suppressed.
255 if (pp->f & F_VERBOSE) moan("gave up: black hole detected");
257 if (pp->f & F_VERBOSE) moan("sending minimum-size probe");
258 switch (probe(&ps, st, lo)) {
262 if (pp->f & F_VERBOSE) {
263 moan("no reply from min-size probe: "
264 "assume black hole at target");
269 if (pp->f & F_VERBOSE) {
270 moan("reply from min-size probe OK: "
271 "assume black hole in network");
276 if (pp->f & F_VERBOSE)
277 moan("unexpected return code from probe");
283 if (droppy) goto higher; else goto lower;
288 if (pp->f & F_VERBOSE)
289 moan("probe returned: remote host is not a black hole");
293 if (pp->f & F_VERBOSE) moan("probe returned: found correct MTU");
298 /* Now we must make a new guess, between lo and hi. We know that lo
299 * is good; but we're not so sure about hi here. We know that hi >
300 * lo, so this will find an approximate midpoint, greater than lo and
303 if (pp->f & F_VERBOSE) moan("probe returned: guessing higher");
304 mtu += (hi - lo + 1)/2;
309 /* If this didn't work, and we're already at the bottom of our
310 * possible range, then something has gone horribly wrong.
315 if (pp->f & F_VERBOSE) moan("error returned: found correct MTU");
320 /* We must make a new guess, between lo and hi. We're probably
321 * fairly sure that lo will succeed, since either it's the minimum
322 * MTU or we've tested it already; but we're not quite sure about hi,
323 * so we want to aim high.
325 if (pp->f & F_VERBOSE) moan("error returned: guessing lower");
326 mtu -= (hi - lo + 1)/2;
330 if (pp->f & F_VERBOSE) moan("error returned with new MTU estimate");
337 /* Clean up and return our result. */
338 pp->pops->finish(st);
344 pp->pops->finish(st);
353 /*----- Doing it the hard way ---------------------------------------------*/
355 #if defined(linux) || defined(__OpenBSD__)
360 # define sane_htons htons
361 # define sane_htonl htonl
367 static int rawicmp = -1, rawudp = -1, rawerr = 0;
369 #define IPCK_INIT 0xffff
371 /* Compute an IP checksum over some data. This is a restartable interface:
372 * initialize A to `IPCK_INIT' for the first call.
374 static unsigned ipcksum(const void *buf, size_t n, unsigned a)
376 unsigned long aa = a ^ 0xffff;
377 const unsigned char *p = buf, *l = p + n;
379 while (p < l - 1) { aa += LOAD16_B(p); p += 2; }
380 if (p < l) { aa += (unsigned)(*p) << 8; }
381 do aa = (aa & 0xffff) + (aa >> 16); while (aa >= 0x10000);
382 return (aa == 0xffff ? aa : aa ^ 0xffff);
385 /* TCP/UDP pseudoheader structure. */
387 struct in_addr ph_src, ph_dst;
393 struct sockaddr_in me, sin;
394 int sk, rawicmp, rawudp;
398 static int raw_setup(void *stv, int sk, const struct param *pp)
400 struct raw_state *st = stv;
403 struct ifaddrs *ifa, *ifaa, *ifap;
406 /* If we couldn't acquire raw sockets, we fail here. */
407 if (rawerr) { errno = rawerr; goto fail_0; }
408 st->rawicmp = rawicmp; st->rawudp = rawudp; st->sk = sk;
410 /* Initialize the sequence number. */
411 st->q = rand() & 0xffff;
413 /* Snaffle the local and remote address and port number. */
416 if (getsockname(sk, (struct sockaddr *)&st->me, &sz))
419 /* There isn't a portable way to force the DF flag onto a packet through
420 * UDP, or even through raw IP, unless we write the entire IP header
421 * ourselves. This is somewhat annoying, especially since we have an
422 * uphill struggle keeping track of which systems randomly expect which
423 * header fields to be presented in host byte order. Oh, well.
426 if (setsockopt(rawudp, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i))) goto fail_0;
428 /* Find an upper bound on the MTU. Do two passes over the interface
429 * list. If we can find matches for our local address then use the
430 * highest one of those; otherwise do a second pass and simply take the
431 * highest MTU of any network interface.
433 if (getifaddrs(&ifaa)) goto fail_0;
434 for (i = 0; i < 2; i++) {
435 for (ifap = 0, ifa = ifaa; ifa; ifa = ifa->ifa_next) {
436 if (!(ifa->ifa_flags & IFF_UP) || !ifa->ifa_addr ||
437 ifa->ifa_addr->sa_family != AF_INET ||
439 ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr !=
440 st->me.sin_addr.s_addr) ||
441 (i == 1 && ifap && strcmp(ifap->ifa_name, ifa->ifa_name) == 0) ||
442 strlen(ifa->ifa_name) >= sizeof(ifr.ifr_name))
445 strcpy(ifr.ifr_name, ifa->ifa_name);
446 if (ioctl(sk, SIOCGIFMTU, &ifr)) goto fail_1;
447 if (mtu < ifr.ifr_mtu) mtu = ifr.ifr_mtu;
451 if (mtu < 0) { errno = ENOTCONN; goto fail_1; }
463 static void raw_finish(void *stv) { ; }
465 static void raw_selprep(void *stv, int *maxfd, fd_set *fd_in)
466 { struct raw_state *st = stv; ADDFD(st->sk); ADDFD(st->rawicmp); }
468 static int raw_xmit(void *stv, int mtu)
470 struct raw_state *st = stv;
471 unsigned char b[65536], *p;
477 /* Build the IP header. */
480 ip->ip_hl = sizeof(*ip)/4;
481 ip->ip_tos = IPTOS_RELIABILITY;
482 ip->ip_len = sane_htons(mtu);
483 STEP(st->q); ip->ip_id = htons(st->q);
484 ip->ip_off = sane_htons(0 | IP_DF);
486 ip->ip_p = IPPROTO_UDP;
488 ip->ip_src = st->me.sin_addr;
489 ip->ip_dst = st->sin.sin_addr;
491 /* Build a UDP packet in the output buffer. */
492 udp = (struct udphdr *)(ip + 1);
493 udp->uh_sport = st->me.sin_port;
494 udp->uh_dport = st->sin.sin_port;
495 udp->uh_ulen = htons(mtu - sizeof(*ip));
498 /* Copy the payload. */
499 p = (unsigned char *)(udp + 1);
500 memcpy(p, buf, mtu - (p - b));
502 /* Calculate the UDP checksum. */
503 ph.ph_src = ip->ip_src;
504 ph.ph_dst = ip->ip_dst;
506 ph.ph_p = IPPROTO_UDP;
507 ph.ph_len = udp->uh_ulen;
509 ck = ipcksum(&ph, sizeof(ph), ck);
510 ck = ipcksum(udp, mtu - sizeof(*ip), ck);
511 udp->uh_sum = htons(ck);
513 /* Send the whole thing off. If we're too big for the interface then we
514 * might need to trim immediately.
516 if (sendto(st->rawudp, b, mtu, 0,
517 (struct sockaddr *)&st->sin, sizeof(st->sin)) < 0) {
518 if (errno == EMSGSIZE) return (RC_LOWER);
529 static int raw_selproc(void *stv, fd_set *fd_in, struct probestate *ps)
531 struct raw_state *st = stv;
532 unsigned char b[65536];
538 /* An ICMP packet: see what's inside. */
539 if (FD_ISSET(st->rawicmp, fd_in)) {
540 if ((n = read(st->rawicmp, b, sizeof(b))) < 0) goto fail_0;
543 if (n < sizeof(*ip) || n < sizeof(4*ip->ip_hl) ||
544 ip->ip_v != 4 || ip->ip_p != IPPROTO_ICMP)
546 n -= sizeof(4*ip->ip_hl);
548 icmp = (struct icmp *)(b + 4*ip->ip_hl);
549 if (n < sizeof(*icmp) || icmp->icmp_type != ICMP_UNREACH)
551 n -= offsetof(struct icmp, icmp_ip);
554 if (n < sizeof(*ip) ||
555 ip->ip_p != IPPROTO_UDP || ip->ip_hl != sizeof(*ip)/4 ||
556 ip->ip_id != htons(st->q) ||
557 ip->ip_src.s_addr != st->me.sin_addr.s_addr ||
558 ip->ip_dst.s_addr != st->sin.sin_addr.s_addr)
562 udp = (struct udphdr *)(ip + 1);
563 if (n < sizeof(udp) || udp->uh_sport != st->me.sin_port ||
564 udp->uh_dport != st->sin.sin_port)
568 if (icmp->icmp_code == ICMP_UNREACH_PORT) return (RC_HIGHER);
569 else if (icmp->icmp_code != ICMP_UNREACH_NEEDFRAG) goto skip_icmp;
570 else if (icmp->icmp_nextmtu) return (htons(icmp->icmp_nextmtu));
571 else return (RC_LOWER);
575 /* If we got a reply to the current probe then we're good. If we got an
576 * error, or the packet's sequence number is wrong, then ignore it.
578 if (FD_ISSET(st->sk, fd_in)) {
579 if ((n = read(st->sk, b, sizeof(b))) < 0) return (RC_OK);
580 else if (mypacketp(ps, b, n)) return (RC_HIGHER);
590 static const struct probe_ops raw_ops = {
591 "raw", OPS_CHAIN, sizeof(struct raw_state),
592 raw_setup, raw_finish,
593 raw_selprep, raw_xmit, raw_selproc
597 #define OPS_CHAIN &raw_ops
599 /*----- Doing the job on Linux --------------------------------------------*/
604 # define IP_MTU 14 /* Blech! */
611 static int linux_setup(void *stv, int sk, const struct param *pp)
613 struct linux_state *st = stv;
617 /* Snaffle the UDP socket. */
620 /* Turn on kernel path-MTU discovery and force DF on. */
621 i = IP_PMTUDISC_PROBE;
622 if (setsockopt(st->sk, IPPROTO_IP, IP_MTU_DISCOVER, &i, sizeof(i)))
625 /* Read the initial MTU guess back and report it. */
627 if (getsockopt(st->sk, IPPROTO_IP, IP_MTU, &mtu, &sz))
634 static void linux_finish(void *stv) { ; }
636 static void linux_selprep(void *stv, int *maxfd, fd_set *fd_in)
637 { struct linux_state *st = stv; ADDFD(st->sk); }
639 static int linux_xmit(void *stv, int mtu)
641 struct linux_state *st = stv;
643 /* Write the packet. */
644 if (write(st->sk, buf, mtu - 28) >= 0) return (RC_OK);
645 else if (errno == EMSGSIZE) return (RC_LOWER);
646 else return (RC_FAIL);
649 static int linux_selproc(void *stv, fd_set *fd_in, struct probestate *ps)
651 struct linux_state *st = stv;
655 unsigned char b[65536];
657 /* Read an answer. If it looks like the right kind of error then report a
658 * success. This is potentially wrong, since we can't tell whether an
659 * error was delayed from an earlier probe. However, we never return
660 * RC_LOWER from this method, so the packet sizes ought to be monotonically
661 * decreasing and this won't cause trouble. Otherwise update from the
662 * kernel's idea of the right MTU.
664 if (FD_ISSET(st->sk, fd_in)) {
665 n = read(st->sk, &buf, sizeof(buf));
667 mypacketp(ps, b, n) :
668 errno == ECONNREFUSED || errno == EHOSTUNREACH)
671 if (getsockopt(st->sk, IPPROTO_IP, IP_MTU, &mtu, &sz))
678 static const struct probe_ops linux_ops = {
679 "linux", OPS_CHAIN, sizeof(struct linux_state),
680 linux_setup, linux_finish,
681 linux_selprep, linux_xmit, linux_selproc
685 #define OPS_CHAIN &linux_ops
689 /*----- Help options ------------------------------------------------------*/
691 static const struct probe_ops *probe_ops = OPS_CHAIN;
693 static void version(FILE *fp)
694 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
696 static void usage(FILE *fp)
698 pquis(fp, "Usage: $ [-v] [-H HEADER] [-m METHOD]\n\
699 [-r SECS] [-g FACTOR] [-t SECS] HOST [PORT]\n");
702 static void help(FILE *fp)
704 const struct probe_ops *ops;
713 -h, --help Show this help text.\n\
714 -V, --version Show version number.\n\
715 -u, --usage Show brief usage message.\n\
717 -g, --growth=FACTOR Growth factor for retransmit interval.\n\
718 -m, --method=METHOD Use METHOD to probe for MTU.\n\
719 -r, --retransmit=SECS Retransmit if no reply after SEC.\n\
720 -t, --timeout=SECS Give up expecting a reply after SECS.\n\
721 -v, --verbose Write a running commentary to stderr.\n\
722 -H, --header=HEX Packet header, in hexadecimal.\n\
726 for (ops = probe_ops; ops; ops = ops->next)
727 printf("\t%s\n", ops->name);
730 /*----- Main code ---------------------------------------------------------*/
732 int main(int argc, char *argv[])
734 struct param pp = { 0, 0.333, 3.0, 8.0, 0, OPS_CHAIN };
747 if ((rawicmp = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0 ||
748 (rawudp = socket(PF_INET, SOCK_RAW, IPPROTO_UDP)) < 0)
750 if (setuid(getuid()))
754 fillbuffer(buf, sizeof(buf));
755 pp.sin.sin_port = htons(7);
758 static const struct option opts[] = {
759 { "help", 0, 0, 'h' },
760 { "version", 0, 0, 'V' },
761 { "usage", 0, 0, 'u' },
762 { "header", OPTF_ARGREQ, 0, 'H' },
763 { "growth", OPTF_ARGREQ, 0, 'g' },
764 { "method", OPTF_ARGREQ, 0, 'm' },
765 { "retransmit", OPTF_ARGREQ, 0, 'r' },
766 { "timeout", OPTF_ARGREQ, 0, 't' },
767 { "verbose", 0, 0, 'v' },
771 i = mdwopt(argc, argv, "hVu" "H:g:m:r:t:v", opts, 0, 0, 0);
774 case 'h': help(stdout); exit(0);
775 case 'V': version(stdout); exit(0);
776 case 'u': usage(stdout); exit(0);
781 hex_decode(&hc, optarg, strlen(optarg), &d);
782 hex_decode(&hc, 0, 0, &d);
783 sz = d.len < 532 ? d.len : 532;
784 memcpy(buf, d.buf, sz);
788 case 'g': pp.regr = s2f(optarg, "retransmit growth factor"); break;
789 case 'r': pp.retx = s2f(optarg, "retransmit interval"); break;
790 case 't': pp.timeout = s2f(optarg, "timeout"); break;
793 for (pp.pops = OPS_CHAIN; pp.pops; pp.pops = pp.pops->next)
794 if (strcmp(pp.pops->name, optarg) == 0) goto found_alg;
795 die(EXIT_FAILURE, "unknown probe algorithm `%s'", optarg);
799 case 'v': pp.f |= F_VERBOSE; break;
806 argv += optind; argc -= optind;
807 if ((f & f_bogus) || 1 > argc || argc > 2) {
812 if ((h = gethostbyname(*argv)) == 0)
813 die(EXIT_FAILURE, "unknown host `%s': %s", *argv, hstrerror(h_errno));
814 if (h->h_addrtype != AF_INET)
815 die(EXIT_FAILURE, "unsupported address family for host `%s'", *argv);
816 memcpy(&pp.sin.sin_addr, h->h_addr, sizeof(struct in_addr));
821 u = strtoul(*argv, &q, 0);
823 pp.sin.sin_port = htons(u);
824 else if ((s = getservbyname(*argv, "udp")) == 0)
825 die(EXIT_FAILURE, "unknown UDP service `%s'", *argv);
827 pp.sin.sin_port = s->s_port;
830 pp.sin.sin_family = AF_INET;
833 die(EXIT_FAILURE, "failed to discover MTU: %s", strerror(errno));
835 if (ferror(stdout) || fflush(stdout) || fclose(stdout))
836 die(EXIT_FAILURE, "failed to write result: %s", strerror(errno));
840 /*----- That's all, folks -------------------------------------------------*/