1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that: (1) source code distributions
12  * retain the above copyright notice and this paragraph in its entirety, (2)
13  * distributions including binary code include the above copyright notice and
14  * this paragraph in its entirety in the documentation or other materials
15  * provided with the distribution, and (3) all advertising materials mentioning
16  * features or use of this software display the following acknowledgement:
17  * ``This product includes software developed by the University of California,
18  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
19  * the University nor the names of its contributors may be used to endorse
20  * or promote products derived from this software without specific prior
21  * written permission.
22  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25  *
26  *
27  * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
28  */
29 
30 #include <sys/socket.h>
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <strings.h>
36 #include <libintl.h>
37 #include <errno.h>
38 
39 #include <netinet/in_systm.h>
40 #include <netinet/in.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_var.h>
43 #include <netinet/ip_icmp.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 
47 #include <arpa/inet.h>
48 #include <netdb.h>
49 
50 #include <libinetutil.h>
51 #include "traceroute.h"
52 
53 /*
54  * IPv4 source routing option.
55  * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
56  * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
57  */
58 struct ip_sourceroute {
59 	uint8_t ipsr_code;
60 	uint8_t ipsr_len;
61 	uint8_t ipsr_ptr;
62 	/* up to 9 IPv4 addresses */
63 	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
64 };
65 
66 int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
67 extern ushort_t in_cksum(ushort_t *, int);
68 extern char *inet_name(union any_in_addr *, int);
69 static char *pr_type(uchar_t);
70 void print_addr(uchar_t *, int, struct sockaddr *);
71 boolean_t print_icmp_other(uchar_t, uchar_t);
72 void send_probe(int, struct sockaddr *, struct ip *, int, int,
73     struct timeval *, int);
74 struct ip *set_buffers(int);
75 void set_IPv4opt_sourcerouting(int, union any_in_addr *, union any_in_addr *);
76 
77 /*
78  * prepares the buffer to be sent as an IP datagram
79  */
80 struct ip *
set_buffers(int plen)81 set_buffers(int plen)
82 {
83 	struct ip *outip;
84 	uchar_t *outp;		/* packet following the IP header (UDP/ICMP) */
85 	struct udphdr *outudp;
86 	struct icmp *outicmp;
87 	int optlen = 0;
88 
89 	outip = (struct ip *)malloc((size_t)plen);
90 	if (outip == NULL) {
91 		Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
92 		exit(EXIT_FAILURE);
93 	}
94 
95 	if (gw_count > 0) {
96 		/* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
97 		optlen = 8 + gw_count * sizeof (struct in_addr);
98 	}
99 
100 	(void) memset((char *)outip, 0, (size_t)plen);
101 	outp = (uchar_t *)(outip + 1);
102 
103 	outip->ip_v = IPVERSION;
104 	if (settos)
105 		outip->ip_tos = tos;
106 
107 	/*
108 	 * LBNL bug fixed: missing '- optlen' before, causing optlen
109 	 * added twice
110 	 *
111 	 * BSD bug: BSD touches the header fields 'len' and 'ip_off'
112 	 * even when HDRINCL is set. It applies htons() on these
113 	 * fields. It should send the header untouched when HDRINCL
114 	 * is set.
115 	 */
116 	outip->ip_len = htons(plen - optlen);
117 	outip->ip_off = htons(off);
118 	outip->ip_hl = (outp - (uchar_t *)outip) >> 2;
119 
120 	/* setup ICMP or UDP */
121 	if (useicmp) {
122 		outip->ip_p = IPPROTO_ICMP;
123 
124 		/* LINTED E_BAD_PTR_CAST_ALIGN */
125 		outicmp = (struct icmp *)outp;
126 		outicmp->icmp_type = ICMP_ECHO;
127 		outicmp->icmp_id = htons(ident);
128 	} else {
129 		outip->ip_p = IPPROTO_UDP;
130 
131 		/* LINTED E_BAD_PTR_CAST_ALIGN */
132 		outudp = (struct udphdr *)outp;
133 		outudp->uh_sport = htons(ident);
134 		outudp->uh_ulen =
135 		    htons((ushort_t)(plen - (sizeof (struct ip) + optlen)));
136 	}
137 
138 	return (outip);
139 }
140 
141 /*
142  * Setup the source routing for IPv4.
143  */
144 void
set_IPv4opt_sourcerouting(int sndsock,union any_in_addr * ip_addr,union any_in_addr * gwIPlist)145 set_IPv4opt_sourcerouting(int sndsock, union any_in_addr *ip_addr,
146     union any_in_addr *gwIPlist)
147 {
148 	struct protoent *pe;
149 	struct ip_sourceroute *srp;
150 	uchar_t optlist[MAX_IPOPTLEN];
151 	int i;
152 	int gwV4_count;
153 
154 	if ((pe = getprotobyname("ip")) == NULL) {
155 		Fprintf(stderr, "%s: unknown protocol ip\n", prog);
156 		exit(EXIT_FAILURE);
157 	}
158 
159 	gwV4_count = (gw_count < MAX_GWS) ? gw_count : MAX_GWS - 1;
160 	/* final hop */
161 	gwIPlist[gwV4_count].addr = ip_addr->addr;
162 
163 	/*
164 	 * the option length passed to setsockopt() needs to be a multiple of
165 	 * 32 bits. Therefore we need to use a 1-byte padding (source routing
166 	 * information takes 4x+3 bytes).
167 	 */
168 	optlist[0] = IPOPT_NOP;
169 
170 	srp = (struct ip_sourceroute *)&optlist[1];
171 	srp->ipsr_code = IPOPT_LSRR;
172 	/* 3 = 1 (code) + 1 (len) + 1 (ptr) */
173 	srp->ipsr_len = 3 + (gwV4_count + 1) * sizeof (gwIPlist[0].addr);
174 	srp->ipsr_ptr = IPOPT_MINOFF;
175 
176 	for (i = 0; i <= gwV4_count; i++) {
177 		(void) bcopy((char *)&gwIPlist[i].addr, &srp->ipsr_addrs[i],
178 		    sizeof (struct in_addr));
179 	}
180 
181 	if (setsockopt(sndsock, pe->p_proto, IP_OPTIONS, (const char *)optlist,
182 	    srp->ipsr_len + 1) < 0) {
183 		Fprintf(stderr, "%s: IP_OPTIONS: %s\n", prog, strerror(errno));
184 		exit(EXIT_FAILURE);
185 	}
186 }
187 
188 /*
189  * send a probe packet to the destination
190  */
191 void
send_probe(int sndsock,struct sockaddr * to,struct ip * outip,int seq,int ttl,struct timeval * tp,int packlen)192 send_probe(int sndsock, struct sockaddr *to, struct ip *outip,
193     int seq, int ttl, struct timeval *tp, int packlen)
194 {
195 	int cc;
196 	struct udpiphdr *ui;
197 	uchar_t *outp;		/* packet following the IP header (UDP/ICMP) */
198 	struct udphdr *outudp;
199 	struct icmp *outicmp;
200 	struct outdata *outdata;
201 	struct ip tip;
202 	int optlen = 0;
203 	int send_size;
204 
205 	/* initialize buffer pointers */
206 	outp = (uchar_t *)(outip + 1);
207 	/* LINTED E_BAD_PTR_CAST_ALIGN */
208 	outudp =  (struct udphdr *)outp;
209 	/* LINTED E_BAD_PTR_CAST_ALIGN */
210 	outicmp = (struct icmp *)outp;
211 	/* LINTED E_BAD_PTR_CAST_ALIGN */
212 	outdata = (struct outdata *)(outp + ICMP_MINLEN);
213 
214 	if (gw_count > 0) {
215 		/* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
216 		optlen = 8 + gw_count * sizeof (struct in_addr);
217 	}
218 
219 	if (raw_req) {
220 		send_size = packlen - optlen;
221 	} else if (useicmp) {
222 		send_size = packlen - optlen - sizeof (struct ip);
223 	} else {
224 		send_size = packlen - optlen - sizeof (struct ip) -
225 		    sizeof (struct udphdr);
226 	}
227 
228 	outip->ip_ttl = ttl;
229 	outip->ip_id = htons(ident + seq);
230 
231 	/*
232 	 * If a raw IPv4 packet is going to be sent, the Time to Live
233 	 * field in the packet was initialized above.  Otherwise, it is
234 	 * initialized here using the IPPROTO_IP level socket option.
235 	 */
236 	if (!raw_req) {
237 		if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, (char *)&ttl,
238 		    sizeof (ttl)) < 0) {
239 			Fprintf(stderr, "%s: IP_TTL: %s\n", prog,
240 			    strerror(errno));
241 			exit(EXIT_FAILURE);
242 		}
243 	}
244 
245 	/*
246 	 * In most cases, the kernel will recalculate the ip checksum.
247 	 * But we must do it anyway so that the udp checksum comes out
248 	 * right.
249 	 */
250 	if (docksum) {
251 		outip->ip_sum =
252 		    in_cksum((ushort_t *)outip, sizeof (*outip) + optlen);
253 		if (outip->ip_sum == 0)
254 			outip->ip_sum = 0xffff;
255 	}
256 
257 	/* Payload */
258 	outdata->seq = seq;
259 	outdata->ttl = ttl;
260 	outdata->tv = *tp;
261 
262 	if (useicmp) {
263 		outicmp->icmp_seq = htons(seq);
264 	} else {
265 		outudp->uh_dport  = htons((port + seq) % (MAX_PORT + 1));
266 	}
267 
268 	if (!raw_req)
269 		/* LINTED E_BAD_PTR_CAST_ALIGN */
270 		((struct sockaddr_in *)to)->sin_port = outudp->uh_dport;
271 
272 	/* (We can only do the checksum if we know our ip address) */
273 	if (docksum) {
274 		if (useicmp) {
275 			outicmp->icmp_cksum = 0;
276 			outicmp->icmp_cksum = in_cksum((ushort_t *)outicmp,
277 			    packlen - (sizeof (struct ip) + optlen));
278 			if (outicmp->icmp_cksum == 0)
279 				outicmp->icmp_cksum = 0xffff;
280 		} else {
281 			/* Checksum (must save and restore ip header) */
282 			tip = *outip;
283 			ui = (struct udpiphdr *)outip;
284 			ui->ui_next = 0;
285 			ui->ui_prev = 0;
286 			ui->ui_x1 = 0;
287 			ui->ui_len = outudp->uh_ulen;
288 			outudp->uh_sum = 0;
289 			outudp->uh_sum = in_cksum((ushort_t *)ui, packlen);
290 			if (outudp->uh_sum == 0)
291 				outudp->uh_sum = 0xffff;
292 			*outip = tip;
293 		}
294 	}
295 
296 	if (raw_req) {
297 		cc = sendto(sndsock, (char *)outip, send_size, 0, to,
298 		    sizeof (struct sockaddr_in));
299 	} else if (useicmp) {
300 		cc = sendto(sndsock, (char *)outicmp, send_size, 0, to,
301 		    sizeof (struct sockaddr_in));
302 	} else {
303 		cc = sendto(sndsock, (char *)outp, send_size, 0, to,
304 		    sizeof (struct sockaddr_in));
305 	}
306 
307 	if (cc < 0 || cc != send_size)  {
308 		if (cc < 0) {
309 			Fprintf(stderr, "%s: sendto: %s\n", prog,
310 			    strerror(errno));
311 		}
312 		Printf("%s: wrote %s %d chars, ret=%d\n",
313 		    prog, hostname, send_size, cc);
314 		(void) fflush(stdout);
315 	}
316 }
317 
318 /*
319  * Check out the reply packet to see if it's what we were expecting.
320  * Returns REPLY_GOT_TARGET if the reply comes from the target
321  *         REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED
322  *         REPLY_GOT_OTHER for other kinds of unreachables indicating none of
323  *	   the above two cases
324  *
325  * It also sets the icmp type and icmp code values
326  */
327 int
check_reply(struct msghdr * msg,int cc,int seq,uchar_t * type,uchar_t * code)328 check_reply(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code)
329 {
330 	uchar_t *buf = msg->msg_iov->iov_base;
331 	struct sockaddr_in *from_in = (struct sockaddr_in *)msg->msg_name;
332 	struct icmp *icp;
333 	int hlen;
334 	int save_cc = cc;
335 	struct ip *ip;
336 
337 	/* LINTED E_BAD_PTR_CAST_ALIGN */
338 	ip = (struct ip *)buf;
339 	hlen = ip->ip_hl << 2;
340 	if (cc < hlen + ICMP_MINLEN) {
341 		if (verbose) {
342 			Printf("packet too short (%d bytes) from %s\n",
343 			    cc, inet_ntoa(from_in->sin_addr));
344 		}
345 		return (REPLY_SHORT_PKT);
346 	}
347 	cc -= hlen;
348 	/* LINTED E_BAD_PTR_CAST_ALIGN */
349 	icp = (struct icmp *)(buf + hlen);
350 
351 	*type = icp->icmp_type;
352 	*code = icp->icmp_code;
353 
354 	/*
355 	 * traceroute interpretes only ICMP_TIMXCEED_INTRANS, ICMP_UNREACH and
356 	 * ICMP_ECHOREPLY, ignores others
357 	 */
358 	if ((*type == ICMP_TIMXCEED && *code == ICMP_TIMXCEED_INTRANS) ||
359 	    *type == ICMP_UNREACH || *type == ICMP_ECHOREPLY) {
360 		struct ip *hip;
361 		struct udphdr *up;
362 		struct icmp *hicmp;
363 
364 		cc -= ICMP_MINLEN;
365 		hip = &icp->icmp_ip;
366 		hlen = hip->ip_hl << 2;
367 		cc -= hlen;
368 		if (useicmp) {
369 			if (*type == ICMP_ECHOREPLY &&
370 			    icp->icmp_id == htons(ident) &&
371 			    icp->icmp_seq == htons(seq))
372 				return (REPLY_GOT_TARGET);
373 
374 			/* LINTED E_BAD_PTR_CAST_ALIGN */
375 			hicmp = (struct icmp *)((uchar_t *)hip + hlen);
376 
377 			if (ICMP_MINLEN <= cc &&
378 			    hip->ip_p == IPPROTO_ICMP &&
379 			    hicmp->icmp_id == htons(ident) &&
380 			    hicmp->icmp_seq == htons(seq)) {
381 				return ((*type == ICMP_TIMXCEED) ?
382 				    REPLY_GOT_GATEWAY : REPLY_GOT_OTHER);
383 			}
384 		} else {
385 			/* LINTED E_BAD_PTR_CAST_ALIGN */
386 			up = (struct udphdr *)((uchar_t *)hip + hlen);
387 			/*
388 			 * at least 4 bytes of UDP header is required for this
389 			 * check
390 			 */
391 			if (4 <= cc &&
392 			    hip->ip_p == IPPROTO_UDP &&
393 			    up->uh_sport == htons(ident) &&
394 			    up->uh_dport == htons((port + seq) %
395 				(MAX_PORT + 1))) {
396 				if (*type == ICMP_UNREACH &&
397 				    *code == ICMP_UNREACH_PORT) {
398 					return (REPLY_GOT_TARGET);
399 				} else if (*type == ICMP_TIMXCEED) {
400 					return (REPLY_GOT_GATEWAY);
401 				} else {
402 					return (REPLY_GOT_OTHER);
403 				}
404 			}
405 		}
406 	}
407 
408 	if (verbose) {
409 		int i, j;
410 		uchar_t *lp = (uchar_t *)ip;
411 
412 		cc = save_cc;
413 		Printf("\n%d bytes from %s to ", cc,
414 		    inet_ntoa(from_in->sin_addr));
415 		Printf("%s: icmp type %d (%s) code %d\n",
416 		    inet_ntoa(ip->ip_dst), *type, pr_type(*type), *code);
417 		for (i = 0; i < cc; i += 4) {
418 			Printf("%2d: x", i);
419 			for (j = 0; ((j < 4) && ((i + j) < cc)); j++)
420 				Printf("%2.2x", *lp++);
421 			(void) putchar('\n');
422 		}
423 	}
424 
425 	return (REPLY_SHORT_PKT);
426 }
427 
428 /*
429  * convert an ICMP "type" field to a printable string.
430  */
431 static char *
pr_type(uchar_t type)432 pr_type(uchar_t type)
433 {
434 	static struct icmptype_table ttab[] = {
435 		{ICMP_ECHOREPLY,	"Echo Reply"},
436 		{1,			"ICMP 1"},
437 		{2,			"ICMP 2"},
438 		{ICMP_UNREACH,		"Dest Unreachable"},
439 		{ICMP_SOURCEQUENCH,	"Source Quench"},
440 		{ICMP_REDIRECT,		"Redirect"},
441 		{6,			"ICMP 6"},
442 		{7,			"ICMP 7"},
443 		{ICMP_ECHO,		"Echo"},
444 		{ICMP_ROUTERADVERT,	"Router Advertisement"},
445 		{ICMP_ROUTERSOLICIT,	"Router Solicitation"},
446 		{ICMP_TIMXCEED,		"Time Exceeded"},
447 		{ICMP_PARAMPROB,	"Param Problem"},
448 		{ICMP_TSTAMP,		"Timestamp"},
449 		{ICMP_TSTAMPREPLY,	"Timestamp Reply"},
450 		{ICMP_IREQ,		"Info Request"},
451 		{ICMP_IREQREPLY,	"Info Reply"},
452 		{ICMP_MASKREQ,		"Netmask Request"},
453 		{ICMP_MASKREPLY,	"Netmask Reply"}
454 	};
455 	int i = 0;
456 
457 	for (i = 0; i < A_CNT(ttab); i++) {
458 		if (ttab[i].type == type)
459 			return (ttab[i].message);
460 	}
461 
462 	return ("OUT-OF-RANGE");
463 }
464 
465 /*
466  * print the IPv4 src address of the reply packet
467  */
468 void
print_addr(uchar_t * buf,int cc,struct sockaddr * from)469 print_addr(uchar_t *buf, int cc, struct sockaddr *from)
470 {
471 	/* LINTED E_BAD_PTR_CAST_ALIGN */
472 	struct sockaddr_in *from_in = (struct sockaddr_in *)from;
473 	struct ip *ip;
474 	union any_in_addr ip_addr;
475 
476 	ip_addr.addr = from_in->sin_addr;
477 
478 	/* LINTED E_BAD_PTR_CAST_ALIGN */
479 	ip = (struct ip *)buf;
480 
481 	if (nflag) {
482 		Printf(" %s", inet_ntoa(from_in->sin_addr));
483 	} else {
484 		Printf(" %s (%s)", inet_name(&ip_addr, AF_INET),
485 		    inet_ntoa(from_in->sin_addr));
486 	}
487 
488 	if (verbose)
489 		Printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst));
490 }
491 
492 /*
493  * ICMP messages which doesn't mean we got the target, or we got a gateway, are
494  * processed here. It returns _B_TRUE if it's some sort of 'unreachable'.
495  */
496 boolean_t
print_icmp_other(uchar_t type,uchar_t code)497 print_icmp_other(uchar_t type, uchar_t code)
498 {
499 	boolean_t unreach = _B_FALSE;
500 
501 	/*
502 	 * this function only prints '!*' for ICMP unreachable messages,
503 	 * ignores others.
504 	 */
505 	if (type != ICMP_UNREACH) {
506 		return (_B_FALSE);
507 	}
508 
509 	switch (code) {
510 	case ICMP_UNREACH_PORT:
511 		break;
512 
513 	case ICMP_UNREACH_NET_UNKNOWN:
514 	case ICMP_UNREACH_NET:
515 		unreach = _B_TRUE;
516 		Printf(" !N");
517 		break;
518 
519 	case ICMP_UNREACH_HOST_UNKNOWN:
520 	case ICMP_UNREACH_HOST:
521 		unreach = _B_TRUE;
522 		Printf(" !H");
523 		break;
524 
525 	case ICMP_UNREACH_PROTOCOL:
526 		Printf(" !P");
527 		break;
528 
529 	case ICMP_UNREACH_NEEDFRAG:
530 		unreach = _B_TRUE;
531 		Printf(" !F");
532 		break;
533 
534 	case ICMP_UNREACH_SRCFAIL:
535 		unreach = _B_TRUE;
536 		Printf(" !S");
537 		break;
538 
539 	case ICMP_UNREACH_FILTER_PROHIB:
540 	case ICMP_UNREACH_NET_PROHIB:
541 	case ICMP_UNREACH_HOST_PROHIB:
542 		unreach = _B_TRUE;
543 		Printf(" !X");
544 		break;
545 
546 	case ICMP_UNREACH_TOSNET:
547 	case ICMP_UNREACH_TOSHOST:
548 		unreach = _B_TRUE;
549 		Printf(" !T");
550 		break;
551 
552 	case ICMP_UNREACH_ISOLATED:
553 	case ICMP_UNREACH_HOST_PRECEDENCE:
554 	case ICMP_UNREACH_PRECEDENCE_CUTOFF:
555 		unreach = _B_TRUE;
556 		Printf(" !U");
557 		break;
558 
559 	default:
560 		unreach = _B_TRUE;
561 		Printf(" !<%d>", code);
562 		break;
563 	}
564 
565 	return (unreach);
566 }
567