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