1 /* $NetBSD: traceroute.c,v 1.81 2012/08/16 00:40:28 zafer Exp $ */
4 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
27 static const char rcsid
[] =
28 "@(#)Id: traceroute.c,v 1.68 2000/12/14 08:04:33 leres Exp (LBL)";
30 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997,\
32 The Regents of the University of California. All rights reserved.");
33 __RCSID("$NetBSD: traceroute.c,v 1.81 2012/08/16 00:40:28 zafer Exp $");
39 #include <net/gen/in.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <net/gen/ether.h>
43 #include <net/gen/eth_hdr.h>
44 #include <net/gen/eth_io.h>
45 #include <net/gen/ip_hdr.h>
46 #include <net/gen/ip_io.h>
47 #include <net/gen/udp.h>
48 #include <net/gen/udp_hdr.h>
49 #include <net/gen/udp_io.h>
52 * traceroute host - trace the route ip packets follow going to "host".
54 * Attempt to trace the route an ip packet would follow to some
55 * internet host. We find out intermediate hops by launching probe
56 * packets with a small ttl (time to live) then listening for an
57 * icmp "time exceeded" reply from a gateway. We start our probes
58 * with a ttl of one and increase by one until we get an icmp "port
59 * unreachable" (which means we got to "host") or hit a max (which
60 * defaults to 30 hops & can be changed with the -m flag). Three
61 * probes (change with -q flag) are sent at each ttl setting and a
62 * line is printed showing the ttl, address of the gateway and
63 * round trip time of each probe. If the probe answers come from
64 * different gateways, the address of each responding system will
65 * be printed. If there is no response within a 5 sec. timeout
66 * interval (changed with the -w flag), a "*" is printed for that
69 * Probe packets are UDP format. We don't want the destination
70 * host to process them so the destination port is set to an
71 * unlikely value (if some clod on the destination is using that
72 * value, it can be changed with the -p flag).
74 * A sample use might be:
76 * [yak 71]% traceroute nis.nsf.net.
77 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
78 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
79 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
80 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
81 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
82 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
83 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
84 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
85 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
86 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
87 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
88 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
90 * Note that lines 2 & 3 are the same. This is due to a buggy
91 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
92 * packets with a zero ttl.
94 * A more interesting example is:
96 * [yak 72]% traceroute allspice.lcs.mit.edu.
97 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
98 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
99 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
100 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
101 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
102 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
103 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
104 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
105 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
106 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
107 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
108 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
110 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
115 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
117 * (I start to see why I'm having so much trouble with mail to
118 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
119 * either don't send ICMP "time exceeded" messages or send them
120 * with a ttl too small to reach us. 14 - 17 are running the
121 * MIT C Gateway code that doesn't send "time exceeded"s. God
122 * only knows what's going on with 12.
124 * The silent gateway 12 in the above may be the result of a bug in
125 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
126 * sends an unreachable message using whatever ttl remains in the
127 * original datagram. Since, for gateways, the remaining ttl is
128 * zero, the icmp "time exceeded" is guaranteed to not make it back
129 * to us. The behavior of this bug is slightly more interesting
130 * when it appears on the destination system:
132 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
133 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
134 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
135 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
136 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
137 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
144 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
146 * Notice that there are 12 "gateways" (13 is the final
147 * destination) and exactly the last half of them are "missing".
148 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
149 * is using the ttl from our arriving datagram as the ttl in its
150 * icmp reply. So, the reply will time out on the return path
151 * (with no notice sent to anyone since icmp's aren't sent for
152 * icmp's) until we probe with a ttl that's at least twice the path
153 * length. I.e., rip is really only 7 hops away. A reply that
154 * returns with a ttl of 1 is a clue this problem exists.
155 * Traceroute prints a "!" after the time if the ttl is <= 1.
156 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
157 * non-standard (HPUX) software, expect to see this problem
158 * frequently and/or take care picking the target host of your
161 * Other possible annotations after the time are !H, !N, !P (got a host,
162 * network or protocol unreachable, respectively), !S or !F (source
163 * route failed or fragmentation needed -- neither of these should
164 * ever occur and the associated gateway is busted if you see one). If
165 * almost all the probes result in some kind of unreachable, traceroute
166 * will give up and exit.
170 * This program must be run by root or be setuid. (I suggest that
171 * you *don't* make it setuid -- casual use could result in a lot
172 * of unnecessary traffic on our poor, congested nets.)
174 * This program requires a kernel mod that does not appear in any
175 * system available from Berkeley: A raw ip socket using proto
176 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
177 * opposed to data to be wrapped in a ip datagram). See the README
178 * file that came with the source to this program for a description
179 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
180 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
181 * MODIFIED TO RUN THIS PROGRAM.
183 * The udp port usage may appear bizarre (well, ok, it is bizarre).
184 * The problem is that an icmp message only contains 8 bytes of
185 * data from the original datagram. 8 bytes is the size of a udp
186 * header so, if we want to associate replies with the original
187 * datagram, the necessary information must be encoded into the
188 * udp header (the ip id could be used but there's no way to
189 * interlock with the kernel's assignment of ip id's and, anyway,
190 * it would have taken a lot more kernel hacking to allow this
191 * code to set the ip id). So, to allow two or more users to
192 * use traceroute simultaneously, we use this task's pid as the
193 * source port (the high bit is set to move the port number out
194 * of the "likely" range). To keep track of which probe is being
195 * replied to (so times and/or hop counts don't get confused by a
196 * reply that was delayed in transit), we increment the destination
197 * port number before each probe.
199 * Don't use this as a coding example. I was trying to find a
200 * routing problem and this code sort-of popped out after 48 hours
201 * without sleep. I was amazed it ever compiled, much less ran.
203 * I stole the idea for this program from Steve Deering. Since
204 * the first release, I've learned that had I attended the right
205 * IETF working group meetings, I also could have stolen it from Guy
206 * Almes or Matt Mathis. I don't know (or care) who came up with
207 * the idea first. I envy the originators' perspicacity and I'm
208 * glad they didn't keep the idea a secret.
210 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
211 * enhancements to the original distribution.
213 * I've hacked up a round-trip-route version of this that works by
214 * sending a loose-source-routed udp datagram through the destination
215 * back to yourself. Unfortunately, SO many gateways botch source
216 * routing, the thing is almost worthless. Maybe one day...
218 * -- Van Jacobson (van@ee.lbl.gov)
219 * Tue Dec 20 03:50:13 PST 1988
222 #include <sys/param.h>
223 #include <sys/file.h>
224 #include <sys/ioctl.h>
225 #include <sys/socket.h>
226 #include <sys/time.h>
227 #include <sys/sysctl.h>
229 #include <netinet/in_systm.h>
230 #include <netinet/in.h>
231 #include <netinet/ip.h>
232 #include <netinet/ip_var.h>
233 #include <netinet/ip_icmp.h>
234 #include <netinet/udp.h>
235 #include <netinet/udp_var.h>
237 #include <arpa/inet.h>
253 #include <net/route.h>
254 #include <netipsec/ipsec.h>
258 #ifdef HAVE_OS_PROTO_H
259 #include "os-proto.h"
263 #ifndef ICMP_UNREACH_FILTER_PROHIB
264 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
266 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
267 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
269 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
270 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
273 #include "ifaddrlist.h"
275 #include "prog_ops.h"
277 /* Maximum number of gateways (include room for one noop) */
278 #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(u_int32_t)))
280 #ifndef MAXHOSTNAMELEN
281 #define MAXHOSTNAMELEN 64
284 #define Fprintf (void)fprintf
285 #define Printf (void)printf
287 /* Host name and address list */
294 /* Data section of the probe packet */
296 u_char seq
; /* sequence number of this packet */
297 u_char ttl
; /* ttl packet left with */
301 } tv
; /* time packet left */
305 * Support for ICMP extensions
307 * http://www.ietf.org/proceedings/01aug/I-D/draft-ietf-mpls-icmp-02.txt
309 #define ICMP_EXT_OFFSET 8 /* ICMP type, code, checksum, unused */ + \
310 128 /* original datagram */
311 #define ICMP_EXT_VERSION 2
313 * ICMP extensions, common header
315 struct icmp_ext_cmn_hdr
{
316 #if BYTE_ORDER == BIG_ENDIAN
317 unsigned char version
:4;
318 unsigned char reserved1
:4;
320 unsigned char reserved1
:4;
321 unsigned char version
:4;
323 unsigned char reserved2
;
324 unsigned short checksum
;
328 * ICMP extensions, object header
330 struct icmp_ext_obj_hdr
{
333 #define MPLS_STACK_ENTRY_CLASS 1
335 #define MPLS_STACK_ENTRY_C_TYPE 1
339 #if BYTE_ORDER == BIG_ENDIAN
352 #ifndef HAVE_ICMP_NEXTMTU
353 /* Path MTU Discovery (RFC1191) */
360 static u_char packet
[512]; /* last inbound (icmp) packet */
362 static struct ip
*outip
; /* last output (udp) packet */
363 static struct udphdr
*outudp
; /* last output (udp) packet */
364 static void *outmark
; /* packed location of struct outdata */
365 static struct outdata outsetup
; /* setup and copy for alignment */
367 static struct icmp
*outicmp
; /* last output (icmp) packet */
369 /* loose source route gateway list (including room for final destination) */
370 static u_int32_t gwlist
[NGATEWAYS
+ 1];
372 static int s
; /* receive (icmp) socket file descriptor */
373 static int sndsock
; /* send (udp/icmp) socket file descriptor */
375 static struct sockaddr whereto
; /* Who to try to reach */
376 static struct sockaddr wherefrom
; /* Who we are */
377 static int packlen
; /* total length of packet */
378 static int minpacket
; /* min ip packet size */
379 static int maxpacket
= 32 * 1024; /* max ip packet size */
380 static int printed_ttl
= 0;
381 static int pmtu
; /* Path MTU Discovery (RFC1191) */
382 static u_int pausemsecs
;
384 static const char *prog
;
386 static char *hostname
;
389 static const char devnull
[] = "/dev/null";
392 static int nprobes
= 3;
393 static int max_ttl
= 30;
394 static int first_ttl
= 1;
395 static u_int16_t ident
;
396 static in_port_t port
= 32768 + 666; /* start udp dest port # for probe packets */
398 static int options
; /* socket options */
400 static int waittime
= 5; /* time to wait for response (in seconds) */
401 static int nflag
; /* print addresses numerically */
403 static int Mflag
; /* show MPLS labels if any */
404 static int as_path
; /* print as numbers for each hop */
405 static char *as_server
= NULL
;
407 static int useicmp
= 0; /* use icmp echo instead of udp packets */
408 #ifdef CANT_HACK_CKSUM
409 static int doipcksum
= 0; /* don't calculate checksums */
411 static int doipcksum
= 1; /* calculate checksums */
413 static int optlen
; /* length of ip options */
415 static int mtus
[] = {
437 static int *mtuptr
= &mtus
[0];
438 static int mtudisc
= 0;
439 static int nextmtu
; /* from ICMP error, set by packet_ok(), might be 0 */
442 static double deltaT(struct timeval
*, struct timeval
*);
443 static void freehostinfo(struct hostinfo
*);
444 static void getaddr(u_int32_t
*, char *);
445 static struct hostinfo
*gethostinfo(char *);
446 static u_int16_t
in_cksum(u_int16_t
*, int);
447 static u_int16_t
in_cksum2(u_int16_t
, u_int16_t
*, int);
448 static char *inetname(struct in_addr
);
449 static int packet_ok(u_char
*, ssize_t
, struct sockaddr_in
*, int);
450 static const char *pr_type(u_char
);
451 static void print(u_char
*, int, struct sockaddr_in
*);
452 static void resize_packet(void);
453 static void dump_packet(void);
454 static void send_probe(int, int, struct timeval
*);
455 static void setsin(struct sockaddr_in
*, u_int32_t
);
456 static int str2val(const char *, const char *, int, int);
457 static void tvsub(struct timeval
*, struct timeval
*);
458 static void usage(void) __attribute__((__noreturn__
));
459 static ssize_t
wait_for_reply(int, struct sockaddr_in
*, const struct timeval
*);
460 static void decode_extensions(unsigned char *buf
, int ip_len
);
461 static void frag_err(void);
462 static int find_local_ip(struct sockaddr_in
*, struct sockaddr_in
*);
464 #ifdef IPSEC_POLICY_IPSEC
465 static int setpolicy(int, const char *);
470 main(int argc
, char **argv
)
475 struct sockaddr_in
*from
= (struct sockaddr_in
*)&wherefrom
;
476 struct sockaddr_in
*to
= (struct sockaddr_in
*)&whereto
;
481 int tos
= 0, settos
= 0, ttl_flag
= 0;
484 struct ifaddrlist
*al
, *al2
;
486 int mib
[4] = { CTL_NET
, PF_INET
, IPPROTO_IP
, IPCTL_DEFTTL
};
487 size_t size
= sizeof(max_ttl
);
489 setprogname(argv
[0]);
490 prog
= getprogname();
492 if (prog_init
&& prog_init() == -1)
493 err(1, "init failed");
496 /* Kernel takes care of it */
497 /* Insure the socket fds won't be 0, 1 or 2 */
498 if (open(devnull
, O_RDONLY
) < 0 ||
499 open(devnull
, O_RDONLY
) < 0 ||
500 open(devnull
, O_RDONLY
) < 0)
501 err(1, "Cannot open `%s'", devnull
);
503 if ((s
= prog_socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
)) < 0)
504 err(1, "icmp socket");
507 * XXX 'useicmp' will always be zero here. I think the HP-UX users
508 * running our traceroute code will forgive us.
512 sndsock
= prog_socket(AF_INET
, SOCK_RAW
, IPPROTO_UDP
);
514 sndsock
= prog_socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
);
517 sndsock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
518 useicmp
? IPPROTO_ICMP
: IPPROTO_UDP
);
521 err(1, "raw socket");
523 (void) prog_sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &max_ttl
, &size
,
527 while ((op
= getopt(argc
, argv
, "aA:dDFPIMnlrvxf:g:i:m:p:q:s:t:w:z:")) != -1)
548 first_ttl
= str2val(optarg
, "first ttl", 1, 255);
556 if (lsrr
>= NGATEWAYS
)
557 errx(1, "more than %d gateways", NGATEWAYS
);
558 getaddr(gwlist
+ lsrr
, optarg
);
575 max_ttl
= str2val(optarg
, "max ttl", 1, 255);
587 port
= (u_short
)str2val(optarg
, "port",
592 nprobes
= str2val(optarg
, "nprobes", 1, -1);
596 options
|= SO_DONTROUTE
;
601 * set the ip source address of the outbound
602 * probe (e.g., on a multi-homed host).
608 tos
= str2val(optarg
, "tos", 0, 255);
617 doipcksum
= (doipcksum
== 0);
621 waittime
= str2val(optarg
, "wait time",
626 pausemsecs
= str2val(optarg
, "pause msecs",
638 if (first_ttl
> max_ttl
)
639 errx(1, "first ttl (%d) may not be greater than max ttl (%d)",
643 warnx("ip checksums disabled");
646 optlen
= (lsrr
+ 1) * sizeof(gwlist
[0]);
647 minpacket
= sizeof(*outip
) + sizeof(struct outdata
) + optlen
;
649 minpacket
+= 8; /* XXX magic number */
651 minpacket
+= sizeof(*outudp
);
652 packlen
= minpacket
; /* minimum sized packet */
657 /* Process destination and optional packet size */
658 switch (argc
- optind
) {
661 packlen
= str2val(argv
[optind
+ 1],
662 "packet length", minpacket
, maxpacket
);
666 hostname
= argv
[optind
];
667 hi
= gethostinfo(hostname
);
668 setsin(to
, hi
->addrs
[0]);
670 warnx("%s has multiple addresses; using %s",
671 hostname
, inet_ntoa(to
->sin_addr
));
681 #ifdef HAVE_SETLINEBUF
684 setvbuf(stdout
, NULL
, _IOLBF
, 0);
687 outip
= malloc((unsigned)packlen
);
690 memset(outip
, 0, packlen
);
692 outip
->ip_v
= IPVERSION
;
695 #ifdef BYTESWAP_IP_HDR
696 outip
->ip_len
= htons(packlen
);
697 outip
->ip_off
= htons(off
);
699 outip
->ip_len
= packlen
;
702 outp
= (u_char
*)(outip
+ 1);
703 #ifdef HAVE_RAW_OPTIONS
711 gwlist
[lsrr
] = to
->sin_addr
.s_addr
;
713 outip
->ip_dst
.s_addr
= gwlist
[0];
715 /* force 4 byte alignment */
716 optlist
[0] = IPOPT_NOP
;
717 /* loose source route option */
718 optlist
[1] = IPOPT_LSRR
;
719 i
= lsrr
* sizeof(gwlist
[0]);
721 /* Pointer to LSRR addresses */
722 optlist
[3] = IPOPT_MINOFF
;
723 memcpy(optlist
+ 4, gwlist
+ 1, i
);
726 outip
->ip_dst
= to
->sin_addr
;
728 outip
->ip_hl
= (outp
- (u_char
*)outip
) >> 2;
729 ident
= htons(arc4random() & 0xffff) | 0x8000;
731 outip
->ip_p
= IPPROTO_ICMP
;
733 outicmp
= (struct icmp
*)outp
;
734 outicmp
->icmp_type
= ICMP_ECHO
;
735 outicmp
->icmp_id
= htons(ident
);
737 outmark
= outp
+ 8; /* XXX magic number */
739 outip
->ip_p
= IPPROTO_UDP
;
741 outudp
= (struct udphdr
*)outp
;
742 outudp
->uh_sport
= htons(ident
);
744 htons((u_int16_t
)(packlen
- (sizeof(*outip
) + optlen
)));
745 outmark
= outudp
+ 1;
748 if (options
& SO_DEBUG
)
749 (void)prog_setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
752 #ifdef IPSEC_POLICY_IPSEC
754 * do not raise error even if setsockopt fails, kernel may have ipsec
757 if (setpolicy(s
, "in bypass") < 0)
759 if (setpolicy(s
, "out bypass") < 0)
763 int level
= IPSEC_LEVEL_AVAIL
;
765 (void)prog_setsockopt(s
, IPPROTO_IP
, IP_ESP_TRANS_LEVEL
, &level
,
767 (void)prog_setsockopt(s
, IPPROTO_IP
, IP_ESP_NETWORK_LEVEL
, &level
,
769 #ifdef IP_AUTH_TRANS_LEVEL
770 (void)prog_setsockopt(s
, IPPROTO_IP
, IP_AUTH_TRANS_LEVEL
, &level
,
773 (void)prog_setsockopt(s
, IPPROTO_IP
, IP_AUTH_LEVEL
, &level
,
776 #ifdef IP_AUTH_NETWORK_LEVEL
777 (void)prog_setsockopt(s
, IPPROTO_IP
, IP_AUTH_NETWORK_LEVEL
, &level
,
781 #endif /*IPSEC_POLICY_IPSEC*/
785 #ifdef IPSEC_POLICY_IPSEC
787 * do not raise error even if setsockopt fails, kernel may have ipsec
790 if (setpolicy(sndsock
, "in bypass") < 0)
792 if (setpolicy(sndsock
, "out bypass") < 0)
796 int level
= IPSEC_LEVEL_BYPASS
;
798 (void)prog_setsockopt(sndsock
, IPPROTO_IP
, IP_ESP_TRANS_LEVEL
, &level
,
800 (void)prog_setsockopt(sndsock
, IPPROTO_IP
, IP_ESP_NETWORK_LEVEL
, &level
,
802 #ifdef IP_AUTH_TRANS_LEVEL
803 (void)prog_setsockopt(sndsock
, IPPROTO_IP
, IP_AUTH_TRANS_LEVEL
, &level
,
806 (void)prog_setsockopt(sndsock
, IPPROTO_IP
, IP_AUTH_LEVEL
, &level
,
809 #ifdef IP_AUTH_NETWORK_LEVEL
810 (void)prog_setsockopt(sndsock
, IPPROTO_IP
, IP_AUTH_NETWORK_LEVEL
, &level
,
814 #endif /*IPSEC_POLICY_IPSEC*/
817 #if defined(IP_OPTIONS) && !defined(HAVE_RAW_OPTIONS)
819 u_char optlist
[MAX_IPOPTLEN
];
822 gwlist
[lsrr
] = to
->sin_addr
.s_addr
;
825 /* force 4 byte alignment */
826 optlist
[0] = IPOPT_NOP
;
827 /* loose source route option */
828 optlist
[1] = IPOPT_LSRR
;
829 i
= lsrr
* sizeof(gwlist
[0]);
831 /* Pointer to LSRR addresses */
832 optlist
[3] = IPOPT_MINOFF
;
833 memcpy(optlist
+ 4, gwlist
, i
);
835 if ((prog_setsockopt(sndsock
, IPPROTO_IP
, IP_OPTIONS
, optlist
,
836 i
+ sizeof(gwlist
[0]))) < 0)
837 err(1, "IP_OPTIONS");
843 if (prog_setsockopt(sndsock
, SOL_SOCKET
, SO_SNDBUF
, (char *)&packlen
,
844 sizeof(packlen
)) < 0)
848 if (prog_setsockopt(sndsock
, IPPROTO_IP
, IP_HDRINCL
, (char *)&on
,
850 err(1, "IP_HDRINCL");
853 if (settos
&& prog_setsockopt(sndsock
, IPPROTO_IP
, IP_TOS
,
854 &tos
, sizeof(tos
)) < 0)
855 err(1, "setsockopt tos %d", tos
);
858 if (options
& SO_DEBUG
)
859 if (prog_setsockopt(sndsock
, SOL_SOCKET
, SO_DEBUG
, &on
,
861 err(1, "setsockopt debug %d", tos
);
862 if (options
& SO_DONTROUTE
)
863 if (prog_setsockopt(sndsock
, SOL_SOCKET
, SO_DONTROUTE
, &on
,
865 err(1, "setsockopt dontroute %d", tos
);
868 /* Get the interface address list */
869 n
= ifaddrlist(&al
, errbuf
, sizeof errbuf
);
872 errx(1, "ifaddrlist (%s)", errbuf
);
874 errx(1, "Can't find any network interfaces");
876 /* Look for a specific device */
877 if (device
!= NULL
) {
878 for (i
= n
; i
> 0; --i
, ++al2
)
879 if (strcmp(device
, al2
->device
) == 0)
882 errx(1, "Can't find interface %.32s", device
);
885 /* Determine our source address */
886 if (source
== NULL
) {
888 * If a device was specified, use the interface address.
889 * Otherwise, try to determine our source address.
890 * Warn if there are more than one.
892 setsin(from
, al2
->addr
);
893 if (n
> 1 && device
== NULL
&& !find_local_ip(from
, to
)) {
894 warnx("Multiple interfaces found; using %s @ %s",
895 inet_ntoa(from
->sin_addr
), al2
->device
);
898 hi
= gethostinfo(source
);
901 if (device
== NULL
) {
903 * Use the first interface found.
904 * Warn if there are more than one.
906 setsin(from
, hi
->addrs
[0]);
908 warnx("%s has multiple addresses; using %s",
909 source
, inet_ntoa(from
->sin_addr
));
912 * Make sure the source specified matches the
915 for (i
= hi
->n
, ap
= hi
->addrs
; i
> 0; --i
, ++ap
)
916 if (*ap
== al2
->addr
)
919 errx(1, "%s is not on interface %s",
926 /* Revert to non-privileged user after opening sockets */
931 * If not root, make sure source address matches a local interface.
932 * (The list of addresses produced by ifaddrlist() automatically
933 * excludes interfaces that are marked down and/or loopback.)
937 for (i
= n
; i
> 0; --i
, ++al2
)
938 if (from
->sin_addr
.s_addr
== al2
->addr
)
941 errx(1, "%s is not a valid local address "
942 "and you are not superuser.",
943 inet_ntoa(from
->sin_addr
));
946 outip
->ip_src
= from
->sin_addr
;
948 if (bind(sndsock
, (struct sockaddr
*)from
, sizeof(*from
)) < 0)
953 asn
= as_setup(as_server
);
955 warnx("as_setup failed, AS# lookups disabled");
956 (void)fflush(stderr
);
962 Fprintf(stderr
, "%s to %s (%s)",
963 prog
, hostname
, inet_ntoa(to
->sin_addr
));
965 Fprintf(stderr
, " from %s", source
);
966 Fprintf(stderr
, ", %d hops max, %d byte packets\n", max_ttl
, packlen
);
967 (void)fflush(stderr
);
969 for (ttl
= first_ttl
; ttl
<= max_ttl
; ++ttl
) {
970 u_int32_t lastaddr
= 0;
978 for (probe
= 0; probe
< nprobes
; ++probe
) {
980 struct timeval t1
, t2
;
982 if (sentfirst
&& pausemsecs
> 0)
983 usleep(pausemsecs
* 1000);
984 (void)gettimeofday(&t1
, NULL
);
985 if (!useicmp
&& htons(port
+ seq
+ 1) == 0)
987 send_probe(++seq
, ttl
, &t1
);
989 while ((cc
= wait_for_reply(s
, from
, &t1
)) != 0) {
990 (void)gettimeofday(&t2
, NULL
);
992 * Since we'll be receiving all ICMP
993 * messages to this host above, we may
994 * never end up with cc=0, so we need
995 * an additional termination check.
997 if (t2
.tv_sec
- t1
.tv_sec
> waittime
) {
1001 i
= packet_ok(packet
, cc
, from
, seq
);
1002 /* Skip short packet */
1006 from
->sin_addr
.s_addr
!= lastaddr
) {
1007 if (gotlastaddr
) printf("\n ");
1008 print(packet
, cc
, from
);
1009 lastaddr
= from
->sin_addr
.s_addr
;
1012 ip
= (struct ip
*)packet
;
1013 Printf(" %.3f ms", deltaT(&t1
, &t2
));
1015 Printf(" (ttl = %d)", ip
->ip_ttl
);
1018 if (ip
->ip_ttl
<= 1)
1025 /* time exceeded in transit */
1031 case ICMP_UNREACH_PORT
:
1033 if (ip
->ip_ttl
<= 1)
1039 case ICMP_UNREACH_NET
:
1044 case ICMP_UNREACH_HOST
:
1049 case ICMP_UNREACH_PROTOCOL
:
1054 case ICMP_UNREACH_NEEDFRAG
:
1060 Printf(" !F-%d", pmtu
);
1064 case ICMP_UNREACH_SRCFAIL
:
1069 case ICMP_UNREACH_FILTER_PROHIB
:
1074 case ICMP_UNREACH_HOST_PRECEDENCE
:
1079 case ICMP_UNREACH_PRECEDENCE_CUTOFF
:
1086 Printf(" !<%d>", code
);
1093 else if (cc
&& probe
== nprobes
- 1 && Mflag
)
1094 decode_extensions(packet
, cc
);
1095 (void)fflush(stdout
);
1099 (unreachable
> 0 && unreachable
>= ((nprobes
+ 1) / 2)))
1110 wait_for_reply(int sock
, struct sockaddr_in
*fromp
, const struct timeval
*tp
)
1112 struct pollfd set
[1];
1113 struct timeval now
, wait
;
1115 socklen_t fromlen
= sizeof(*fromp
);
1119 set
[0].events
= POLLIN
;
1121 wait
.tv_sec
= tp
->tv_sec
+ waittime
;
1122 wait
.tv_usec
= tp
->tv_usec
;
1123 (void)gettimeofday(&now
, NULL
);
1126 if (wait
.tv_sec
< 0) {
1131 retval
= prog_poll(set
, 1, wait
.tv_sec
* 1000 + wait
.tv_usec
/ 1000);
1133 /* If we continue, we probably just flood the remote host. */
1136 cc
= prog_recvfrom(sock
, (char *)packet
, sizeof(packet
), 0,
1137 (struct sockaddr
*)fromp
, &fromlen
);
1144 decode_extensions(unsigned char *buf
, int ip_len
)
1146 struct icmp_ext_cmn_hdr
*cmn_hdr
;
1147 struct icmp_ext_obj_hdr
*obj_hdr
;
1149 struct mpls_header mpls
;
1152 size_t datalen
, obj_len
;
1155 ip
= (struct ip
*)buf
;
1157 if (ip_len
< (int)((ip
->ip_hl
<< 2) + ICMP_EXT_OFFSET
+
1158 sizeof(struct icmp_ext_cmn_hdr
))) {
1160 * No support for ICMP extensions on this host
1166 * Move forward to the start of the ICMP extensions, if present
1168 buf
+= (ip
->ip_hl
<< 2) + ICMP_EXT_OFFSET
;
1169 cmn_hdr
= (struct icmp_ext_cmn_hdr
*)buf
;
1171 if (cmn_hdr
->version
!= ICMP_EXT_VERSION
) {
1178 datalen
= ip_len
- ((u_char
*)cmn_hdr
- (u_char
*)ip
);
1181 * Check the checksum, cmn_hdr->checksum == 0 means no checksum'ing
1184 * If the checksum is ok, we'll get 0, as the checksum is calculated
1185 * with the checksum field being 0'd.
1187 if (ntohs(cmn_hdr
->checksum
) &&
1188 in_cksum((u_short
*)cmn_hdr
, datalen
)) {
1193 buf
+= sizeof(*cmn_hdr
);
1194 datalen
-= sizeof(*cmn_hdr
);
1196 while (datalen
>= sizeof(struct icmp_ext_obj_hdr
)) {
1197 obj_hdr
= (struct icmp_ext_obj_hdr
*)buf
;
1198 obj_len
= ntohs(obj_hdr
->length
);
1201 * Sanity check the length field
1203 if (obj_len
> datalen
)
1209 * Move past the object header
1211 buf
+= sizeof(struct icmp_ext_obj_hdr
);
1212 obj_len
-= sizeof(struct icmp_ext_obj_hdr
);
1214 switch (obj_hdr
->class_num
) {
1215 case MPLS_STACK_ENTRY_CLASS
:
1216 switch (obj_hdr
->c_type
) {
1217 case MPLS_STACK_ENTRY_C_TYPE
:
1218 while (obj_len
>= sizeof(uint32_t)) {
1219 mpls
.mpls_h
= ntohl(*(uint32_t *)buf
);
1221 buf
+= sizeof(uint32_t);
1222 obj_len
-= sizeof(uint32_t);
1224 printf(" [MPLS: Label %d Exp %d]",
1225 mpls
.mpls
.label
, mpls
.mpls
.exp
);
1229 * Something went wrong, and we're at
1230 * a unknown offset into the packet,
1231 * ditch the rest of it.
1238 * Unknown object, skip past it
1240 buf
+= ntohs(obj_hdr
->length
) -
1241 sizeof(struct icmp_ext_obj_hdr
);
1248 * Unknown object, skip past it
1250 buf
+= ntohs(obj_hdr
->length
) -
1251 sizeof(struct icmp_ext_obj_hdr
);
1263 Fprintf(stderr
, "packet data:");
1266 for (p
= useicmp
? (u_char
*)outicmp
: (u_char
*)outudp
, i
= 0; i
<
1267 i
< packlen
- (sizeof(*outip
) + optlen
); i
++)
1269 for (p
= (u_char
*)outip
, i
= 0; i
< packlen
; i
++)
1273 Fprintf(stderr
, "\n ");
1274 Fprintf(stderr
, " %02x", *p
++);
1276 Fprintf(stderr
, "\n");
1280 send_probe(int seq
, int ttl
, struct timeval
*tp
)
1283 struct udpiphdr
* ui
, *oui
;
1284 int oldmtu
= packlen
;
1288 #ifdef BYTESWAP_IP_LEN
1289 outip
->ip_len
= htons(packlen
);
1291 outip
->ip_len
= packlen
;
1293 outip
->ip_ttl
= ttl
;
1295 outip
->ip_id
= htons(ident
+ seq
);
1299 * In most cases, the kernel will recalculate the ip checksum.
1300 * But we must do it anyway so that the udp checksum comes out
1305 in_cksum((u_int16_t
*)outip
, sizeof(*outip
) + optlen
);
1306 if (outip
->ip_sum
== 0)
1307 outip
->ip_sum
= 0xffff;
1313 outsetup
.tv
.tv32_sec
= htonl(tp
->tv_sec
);
1314 outsetup
.tv
.tv32_usec
= htonl(tp
->tv_usec
);
1315 memcpy(outmark
,&outsetup
,sizeof(outsetup
));
1318 outicmp
->icmp_seq
= htons(seq
);
1320 outudp
->uh_dport
= htons(port
+ seq
);
1323 /* Always calculate checksum for icmp packets */
1324 outicmp
->icmp_cksum
= 0;
1325 outicmp
->icmp_cksum
= in_cksum((u_short
*)outicmp
,
1326 packlen
- (sizeof(*outip
) + optlen
));
1327 if (outicmp
->icmp_cksum
== 0)
1328 outicmp
->icmp_cksum
= 0xffff;
1329 } else if (doipcksum
) {
1330 /* Checksum (we must save and restore ip header) */
1332 ui
= (struct udpiphdr
*)outip
;
1333 oui
= (struct udpiphdr
*)&tip
;
1334 /* Easier to zero and put back things that are ok */
1335 memset(ui
, 0, sizeof(ui
->ui_i
));
1336 ui
->ui_src
= oui
->ui_src
;
1337 ui
->ui_dst
= oui
->ui_dst
;
1338 ui
->ui_pr
= oui
->ui_pr
;
1339 ui
->ui_len
= outudp
->uh_ulen
;
1341 outudp
->uh_sum
= in_cksum((u_short
*)ui
, packlen
);
1342 if (outudp
->uh_sum
== 0)
1343 outudp
->uh_sum
= 0xffff;
1347 /* XXX undocumented debugging hack */
1349 const u_int16_t
*sp
;
1352 sp
= (u_int16_t
*)outip
;
1353 nshorts
= (u_int
)packlen
/ sizeof(u_int16_t
);
1355 Printf("[ %d bytes", packlen
);
1356 while (--nshorts
>= 0) {
1359 Printf(" %04x", ntohs(*sp
++));
1364 Printf(" %02x", *(const u_char
*)sp
);
1370 #if !defined(IP_HDRINCL) && defined(IP_TTL)
1371 if (prog_setsockopt(sndsock
, IPPROTO_IP
, IP_TTL
,
1372 (char *)&ttl
, sizeof(ttl
)) < 0)
1373 err(1, "setsockopt ttl %d", ttl
);
1377 nwio_ipopt_t ipopts
;
1378 memset(&ipopts
, 0, sizeof(ipopts
));
1379 ipopts
.nwio_flags
= NWIO_HDR_O_SPEC
;
1380 ipopts
.nwio_ttl
= ttl
;
1381 if(ioctl(sndsock
, NWIOSIPOPT
, &ipopts
) < 0) {
1382 err(1, "ttl ioctl");
1390 cc
= sendto(sndsock
, useicmp
? (char *)outicmp
: (char *)outudp
,
1391 packlen
- (sizeof(*outip
) + optlen
), 0, &whereto
, sizeof(whereto
));
1393 cc
+= sizeof(*outip
) + optlen
;
1395 cc
= prog_sendto(sndsock
, (char *)outip
,
1396 packlen
, 0, &whereto
, sizeof(whereto
));
1398 if (cc
< 0 || cc
!= packlen
) {
1401 * An errno of EMSGSIZE means we're writing too big a
1402 * datagram for the interface. We have to just
1403 * decrease the packet size until we find one that
1406 * XXX maybe we should try to read the outgoing if's
1409 if (errno
== EMSGSIZE
) {
1410 packlen
= *mtuptr
++;
1417 Printf("%s: wrote %s %d chars, ret=%d\n",
1418 prog
, hostname
, packlen
, cc
);
1419 (void)fflush(stdout
);
1421 if (oldmtu
!= packlen
) {
1422 Printf("message too big, "
1423 "trying new MTU = %d\n", packlen
);
1427 Printf("%2d ", ttl
);
1434 deltaT(struct timeval
*t1p
, struct timeval
*t2p
)
1438 dt
= (double)(t2p
->tv_sec
- t1p
->tv_sec
) * 1000.0 +
1439 (double)(t2p
->tv_usec
- t1p
->tv_usec
) / 1000.0;
1444 * Convert an ICMP "type" field to a printable string.
1449 static const char *ttab
[] = {
1450 "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable",
1451 "Source Quench", "Redirect", "ICMP 6", "ICMP 7",
1452 "Echo", "ICMP 9", "ICMP 10", "Time Exceeded",
1453 "Param Problem", "Timestamp", "Timestamp Reply", "Info Request",
1458 return "OUT-OF-RANGE";
1464 packet_ok(u_char
*buf
, ssize_t cc
, struct sockaddr_in
*from
, int seq
)
1472 ip
= (struct ip
*) buf
;
1473 hlen
= ip
->ip_hl
<< 2;
1474 if (cc
< hlen
+ ICMP_MINLEN
) {
1476 Printf("packet too short (%zd bytes) from %s\n", cc
,
1477 inet_ntoa(from
->sin_addr
));
1481 icp
= (struct icmp
*)(buf
+ hlen
);
1483 icp
= (struct icmp
*)buf
;
1485 type
= icp
->icmp_type
;
1486 code
= icp
->icmp_code
;
1487 /* Path MTU Discovery (RFC1191) */
1488 if (code
!= ICMP_UNREACH_NEEDFRAG
)
1491 #ifdef HAVE_ICMP_NEXTMTU
1492 pmtu
= ntohs(icp
->icmp_nextmtu
);
1494 pmtu
= ntohs(((struct my_pmtu
*)&icp
->icmp_void
)->ipm_nextmtu
);
1497 if ((type
== ICMP_TIMXCEED
&& code
== ICMP_TIMXCEED_INTRANS
) ||
1498 type
== ICMP_UNREACH
|| type
== ICMP_ECHOREPLY
) {
1503 hip
= &icp
->icmp_ip
;
1504 hlen
= hip
->ip_hl
<< 2;
1506 nextmtu
= ntohs(icp
->icmp_nextmtu
); /* for frag_err() */
1510 if (type
== ICMP_ECHOREPLY
&&
1511 icp
->icmp_id
== htons(ident
) &&
1512 icp
->icmp_seq
== htons(seq
))
1515 hicmp
= (struct icmp
*)((u_char
*)hip
+ hlen
);
1516 /* XXX 8 is a magic number */
1517 if (hlen
+ 8 <= cc
&&
1518 hip
->ip_p
== IPPROTO_ICMP
&&
1519 hicmp
->icmp_id
== htons(ident
) &&
1520 hicmp
->icmp_seq
== htons(seq
))
1521 return type
== ICMP_TIMXCEED
? -1 : code
+ 1;
1523 up
= (struct udphdr
*)((u_char
*)hip
+ hlen
);
1524 /* XXX 8 is a magic number */
1525 if (hlen
+ 12 <= cc
&&
1526 hip
->ip_p
== IPPROTO_UDP
&&
1527 up
->uh_sport
== htons(ident
) &&
1528 up
->uh_dport
== htons(port
+ seq
))
1529 return type
== ICMP_TIMXCEED
? -1 : code
+ 1;
1535 u_int32_t
*lp
= (u_int32_t
*)&icp
->icmp_ip
;
1537 Printf("\n%zd bytes from %s to ", cc
, inet_ntoa(from
->sin_addr
));
1538 Printf("%s: icmp type %d (%s) code %d\n",
1539 inet_ntoa(ip
->ip_dst
), type
, pr_type(type
), icp
->icmp_code
);
1540 for (i
= 4; i
< cc
; i
+= sizeof(*lp
))
1541 Printf("%2d: x%8.8x\n", i
, *lp
++);
1551 outicmp
->icmp_cksum
= 0;
1552 outicmp
->icmp_cksum
= in_cksum((u_int16_t
*)outicmp
,
1553 packlen
- (sizeof(*outip
) + optlen
));
1554 if (outicmp
->icmp_cksum
== 0)
1555 outicmp
->icmp_cksum
= 0xffff;
1558 htons((u_int16_t
)(packlen
- (sizeof(*outip
) + optlen
)));
1563 print(u_char
*buf
, int cc
, struct sockaddr_in
*from
)
1567 char addr
[INET_ADDRSTRLEN
];
1569 ip
= (struct ip
*) buf
;
1570 hlen
= ip
->ip_hl
<< 2;
1573 strlcpy(addr
, inet_ntoa(from
->sin_addr
), sizeof(addr
));
1576 Printf(" [AS%u]", as_lookup(asn
, addr
, AF_INET
));
1579 Printf(" %s", addr
);
1581 Printf(" %s (%s)", inetname(from
->sin_addr
), addr
);
1584 Printf(" %d bytes to %s", cc
, inet_ntoa (ip
->ip_dst
));
1588 in_cksum(u_int16_t
*addr
, int len
)
1591 return ~in_cksum2(0, addr
, len
);
1595 * Checksum routine for Internet Protocol family headers (C Version)
1598 in_cksum2(u_int16_t seed
, u_int16_t
*addr
, int len
)
1601 u_int16_t
*w
= addr
;
1609 * Our algorithm is simple, using a 32 bit accumulator (sum),
1610 * we add sequential 16 bit words to it, and at the end, fold
1611 * back all the carry bits from the top 16 bits into the lower
1619 /* mop up an odd byte, if necessary */
1621 answer
.b
[0] = *(u_char
*)w
;
1627 * add back carry outs from top 16 bits to low 16 bits
1629 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
1630 sum
+= (sum
>> 16); /* add carry */
1631 answer
.w
= sum
; /* truncate to 16 bits */
1636 * Subtract 2 timeval structs: out = out - in.
1637 * Out is assumed to be >= in.
1640 tvsub(struct timeval
*out
, struct timeval
*in
)
1643 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
1645 out
->tv_usec
+= 1000000;
1647 out
->tv_sec
-= in
->tv_sec
;
1651 * Construct an Internet address representation.
1652 * If the nflag has been supplied, give
1653 * numeric value, otherwise try for symbolic name.
1656 inetname(struct in_addr in
)
1660 static int first
= 1;
1661 static char domain
[MAXHOSTNAMELEN
+ 1], line
[MAXHOSTNAMELEN
+ 1];
1663 if (first
&& !nflag
) {
1666 if (gethostname(domain
, sizeof(domain
) - 1) < 0)
1669 cp
= strchr(domain
, '.');
1671 hp
= gethostbyname(domain
);
1673 cp
= strchr(hp
->h_name
, '.');
1679 (void)strlcpy(domain
, cp
, sizeof(domain
));
1683 if (!nflag
&& in
.s_addr
!= INADDR_ANY
) {
1684 hp
= gethostbyaddr((char *)&in
, sizeof(in
), AF_INET
);
1686 if ((cp
= strchr(hp
->h_name
, '.')) != NULL
&&
1687 strcmp(cp
+ 1, domain
) == 0)
1689 (void)strlcpy(line
, hp
->h_name
, sizeof(line
));
1693 return inet_ntoa(in
);
1696 static struct hostinfo
*
1697 gethostinfo(char *hname
)
1701 struct hostinfo
*hi
;
1704 struct in_addr addr
;
1706 hi
= calloc(1, sizeof(*hi
));
1709 if (inet_aton(hname
, &addr
) != 0) {
1710 hi
->name
= strdup(hname
);
1714 hi
->addrs
= calloc(1, sizeof(hi
->addrs
[0]));
1715 if (hi
->addrs
== NULL
)
1717 hi
->addrs
[0] = addr
.s_addr
;
1721 hp
= gethostbyname(hname
);
1723 errx(1, "unknown host %s", hname
);
1724 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= 4)
1725 errx(1, "bad host %s", hname
);
1726 hi
->name
= strdup(hp
->h_name
);
1729 for (n
= 0, p
= hp
->h_addr_list
; *p
!= NULL
; ++n
, ++p
)
1732 hi
->addrs
= calloc(n
, sizeof(hi
->addrs
[0]));
1733 if (hi
->addrs
== NULL
)
1735 for (ap
= hi
->addrs
, p
= hp
->h_addr_list
; *p
!= NULL
; ++ap
, ++p
)
1736 memcpy(ap
, *p
, sizeof(*ap
));
1741 freehostinfo(struct hostinfo
*hi
)
1743 if (hi
->name
!= NULL
) {
1752 getaddr(u_int32_t
*ap
, char *hname
)
1754 struct hostinfo
*hi
;
1756 hi
= gethostinfo(hname
);
1762 setsin(struct sockaddr_in
*sin
, u_int32_t addr
)
1765 memset(sin
, 0, sizeof(*sin
));
1766 #ifdef HAVE_SOCKADDR_SA_LEN
1767 sin
->sin_len
= sizeof(*sin
);
1769 sin
->sin_family
= AF_INET
;
1770 sin
->sin_addr
.s_addr
= addr
;
1773 /* String to value with optional min and max. Handles decimal and hex. */
1775 str2val(const char *str
, const char *what
, int mi
, int ma
)
1783 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X')) {
1785 val
= strtol(cp
, &ep
, 16);
1787 val
= strtol(str
, &ep
, 10);
1788 if (errno
|| str
[0] == '\0' || *ep
!= '\0')
1789 errx(1, "\"%s\" bad value for %s", str
, what
);
1790 if (val
< mi
&& mi
>= 0) {
1792 errx(1, "%s must be >= %d", what
, mi
);
1794 errx(1, "%s must be > %d", what
, mi
- 1);
1796 if (val
> ma
&& ma
>= 0)
1797 errx(1, "%s must be <= %d", what
, ma
);
1804 extern char version
[];
1806 Fprintf(stderr
, "Version %s\n", version
);
1807 Fprintf(stderr
, "Usage: %s [-adDFPIlMnrvx] [-g gateway] [-i iface] \
1808 [-f first_ttl]\n\t[-m max_ttl] [-p port] [-q nqueries] [-s src_addr] [-t tos]\n\t\
1809 [-w waittime] [-z pausemsecs] [-A as_server] host [packetlen]\n",
1815 * Received ICMP unreachable (fragmentation required and DF set).
1816 * If the ICMP error was from a "new" router, it'll contain the next-hop
1817 * MTU that we should use next. Otherwise we'll just keep going in the
1818 * mtus[] table, trying until we hit a valid MTU.
1827 if (nextmtu
> 0 && nextmtu
< packlen
) {
1828 Printf("\nfragmentation required and DF set, "
1829 "next hop MTU = %d\n",
1832 for (i
= 0; mtus
[i
] > 0; i
++) {
1833 if (mtus
[i
] < nextmtu
) {
1834 mtuptr
= &mtus
[i
]; /* next one to try */
1839 Printf("\nfragmentation required and DF set. ");
1841 Printf("\nBogus next hop MTU = %d > last MTU = %d. ",
1843 packlen
= *mtuptr
++;
1844 Printf("Trying new MTU = %d\n", packlen
);
1850 find_local_ip(struct sockaddr_in
*from
, struct sockaddr_in
*to
)
1853 struct sockaddr_in help
;
1856 sock
= prog_socket(AF_INET
, SOCK_DGRAM
, 0);
1857 if (sock
< 0) return 0;
1859 help
.sin_family
= AF_INET
;
1861 * At this point the port number doesn't matter
1862 * since it only has to be greater than zero.
1865 help
.sin_addr
.s_addr
= to
->sin_addr
.s_addr
;
1866 if (prog_connect(sock
, (struct sockaddr
*)&help
, sizeof(help
)) < 0) {
1867 (void)prog_close(sock
);
1871 help_len
= sizeof(help
);
1872 if (prog_getsockname(sock
, (struct sockaddr
*)&help
, &help_len
) < 0 ||
1873 help_len
!= sizeof(help
) ||
1874 help
.sin_addr
.s_addr
== INADDR_ANY
) {
1875 (void)prog_close(sock
);
1879 (void)prog_close(sock
);
1880 setsin(from
, help
.sin_addr
.s_addr
);
1885 #ifdef IPSEC_POLICY_IPSEC
1887 setpolicy(int so
, const char *policy
)
1891 buf
= ipsec_set_policy(policy
, strlen(policy
));
1893 warnx("%s", ipsec_strerror());
1896 (void)prog_setsockopt(so
, IPPROTO_IP
, IP_IPSEC_POLICY
,
1897 buf
, ipsec_get_policylen(buf
));