xref: /openbsd-src/usr.sbin/tcpdump/print-icmp.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*	$OpenBSD: print-icmp.c,v 1.15 2003/09/08 17:35:57 cedric Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
5  *	The Regents of the University of California.  All rights reserved.
6  *
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
18  * written permission.
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.
22  */
23 
24 #ifndef lint
25 static const char rcsid[] =
26     "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-icmp.c,v 1.15 2003/09/08 17:35:57 cedric Exp $ (LBL)";
27 #endif
28 
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/socket.h>
32 
33 struct mbuf;
34 struct rtentry;
35 #include <net/if.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_icmp.h>
42 #include <netinet/ip_var.h>
43 #include <netinet/udp.h>
44 #include <netinet/udp_var.h>
45 #include <netinet/tcp.h>
46 #include <netinet/tcpip.h>
47 
48 #include <stdio.h>
49 #include <string.h>
50 
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h"			/* must come after interface.h */
54 
55 /* rfc1700 */
56 #ifndef ICMP_UNREACH_NET_UNKNOWN
57 #define ICMP_UNREACH_NET_UNKNOWN	6	/* destination net unknown */
58 #endif
59 #ifndef ICMP_UNREACH_HOST_UNKNOWN
60 #define ICMP_UNREACH_HOST_UNKNOWN	7	/* destination host unknown */
61 #endif
62 #ifndef ICMP_UNREACH_ISOLATED
63 #define ICMP_UNREACH_ISOLATED		8	/* source host isolated */
64 #endif
65 #ifndef ICMP_UNREACH_NET_PROHIB
66 #define ICMP_UNREACH_NET_PROHIB		9	/* admin prohibited net */
67 #endif
68 #ifndef ICMP_UNREACH_HOST_PROHIB
69 #define ICMP_UNREACH_HOST_PROHIB	10	/* admin prohibited host */
70 #endif
71 #ifndef ICMP_UNREACH_TOSNET
72 #define ICMP_UNREACH_TOSNET		11	/* tos prohibited net */
73 #endif
74 #ifndef ICMP_UNREACH_TOSHOST
75 #define ICMP_UNREACH_TOSHOST		12	/* tos prohibited host */
76 #endif
77 
78 /* rfc1716 */
79 #ifndef ICMP_UNREACH_FILTER_PROHIB
80 #define ICMP_UNREACH_FILTER_PROHIB	13	/* admin prohibited filter */
81 #endif
82 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
83 #define ICMP_UNREACH_HOST_PRECEDENCE	14	/* host precedence violation */
84 #endif
85 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
86 #define ICMP_UNREACH_PRECEDENCE_CUTOFF	15	/* precedence cutoff */
87 #endif
88 
89 /* rfc1256 */
90 #ifndef ICMP_ROUTERADVERT
91 #define ICMP_ROUTERADVERT		9	/* router advertisement */
92 #endif
93 #ifndef ICMP_ROUTERSOLICIT
94 #define ICMP_ROUTERSOLICIT		10	/* router solicitation */
95 #endif
96 
97 /* Most of the icmp types */
98 static struct tok icmp2str[] = {
99 	{ ICMP_ECHOREPLY,		"echo reply" },
100 	{ ICMP_SOURCEQUENCH,		"source quench" },
101 	{ ICMP_ECHO,			"echo request" },
102 	{ ICMP_ROUTERSOLICIT,		"router solicitation" },
103 	{ ICMP_TSTAMP,			"time stamp request" },
104 	{ ICMP_TSTAMPREPLY,		"time stamp reply" },
105 	{ ICMP_IREQ,			"information request" },
106 	{ ICMP_IREQREPLY,		"information reply" },
107 	{ ICMP_MASKREQ,			"address mask request" },
108 	{ 0,				NULL }
109 };
110 
111 /* Formats for most of the ICMP_UNREACH codes */
112 static struct tok unreach2str[] = {
113 	{ ICMP_UNREACH_NET,		"net %s unreachable" },
114 	{ ICMP_UNREACH_HOST,		"host %s unreachable" },
115 	{ ICMP_UNREACH_SRCFAIL,
116 	    "%s unreachable - source route failed" },
117 	{ ICMP_UNREACH_NET_UNKNOWN,	"net %s unreachable - unknown" },
118 	{ ICMP_UNREACH_HOST_UNKNOWN,	"host %s unreachable - unknown" },
119 	{ ICMP_UNREACH_ISOLATED,
120 	    "%s unreachable - source host isolated" },
121 	{ ICMP_UNREACH_NET_PROHIB,
122 	    "net %s unreachable - admin prohibited" },
123 	{ ICMP_UNREACH_HOST_PROHIB,
124 	    "host %s unreachable - admin prohibited" },
125 	{ ICMP_UNREACH_TOSNET,
126 	    "net %s unreachable - tos prohibited" },
127 	{ ICMP_UNREACH_TOSHOST,
128 	    "host %s unreachable - tos prohibited" },
129 	{ ICMP_UNREACH_FILTER_PROHIB,
130 	    "host %s unreachable - admin prohibited filter" },
131 	{ ICMP_UNREACH_HOST_PRECEDENCE,
132 	    "host %s unreachable - host precedence violation" },
133 	{ ICMP_UNREACH_PRECEDENCE_CUTOFF,
134 	    "host %s unreachable - precedence cutoff" },
135 	{ 0,				NULL }
136 };
137 
138 /* Formats for the ICMP_REDIRECT codes */
139 static struct tok type2str[] = {
140 	{ ICMP_REDIRECT_NET,		"redirect %s to net %s" },
141 	{ ICMP_REDIRECT_HOST,		"redirect %s to host %s" },
142 	{ ICMP_REDIRECT_TOSNET,		"redirect-tos %s to net %s" },
143 	{ ICMP_REDIRECT_TOSHOST,	"redirect-tos %s to net %s" },
144 	{ 0,				NULL }
145 };
146 
147 /* rfc1191 */
148 struct mtu_discovery {
149 	short unused;
150 	short nexthopmtu;
151 };
152 
153 /* rfc1256 */
154 struct ih_rdiscovery {
155 	u_char ird_addrnum;
156 	u_char ird_addrsiz;
157 	u_short ird_lifetime;
158 };
159 
160 struct id_rdiscovery {
161 	u_int32_t ird_addr;
162 	u_int32_t ird_pref;
163 };
164 
165 void
166 icmp_print(register const u_char *bp, register const u_char *bp2)
167 {
168 	register const struct icmp *dp;
169 	register const struct ip *ip;
170 	register const char *str, *fmt;
171 	register const struct ip *oip;
172 	register const struct udphdr *ouh;
173 	register u_int hlen, dport, mtu;
174 	char buf[MAXHOSTNAMELEN+256];
175 	char buf2[MAXHOSTNAMELEN+256];
176 
177 	dp = (struct icmp *)bp;
178 	ip = (struct ip *)bp2;
179 	str = buf;
180 
181         (void)printf("%s > %s: ",
182 		ipaddr_string(&ip->ip_src),
183 		ipaddr_string(&ip->ip_dst));
184 
185 	TCHECK(dp->icmp_code);
186 	switch (dp->icmp_type) {
187 
188 	case ICMP_ECHOREPLY:
189 	case ICMP_ECHO:
190 		if (vflag) {
191 			TCHECK(dp->icmp_seq);
192 			(void)snprintf(buf, sizeof buf,
193 				       "echo %s (id:%04x seq:%d)",
194 				       (dp->icmp_type == ICMP_ECHO)?
195 				       "request": "reply",
196 				       ntohs(dp->icmp_id),
197 				       ntohs(dp->icmp_seq));
198 		} else
199 			str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
200 		break;
201 
202 	case ICMP_UNREACH:
203 		TCHECK(dp->icmp_ip.ip_dst);
204 		switch (dp->icmp_code) {
205 
206 		case ICMP_UNREACH_PROTOCOL:
207 			TCHECK(dp->icmp_ip.ip_p);
208 			(void)snprintf(buf, sizeof buf,
209 				       "%s protocol %d unreachable",
210 				       ipaddr_string(&dp->icmp_ip.ip_dst),
211 				       dp->icmp_ip.ip_p);
212 			break;
213 
214 		case ICMP_UNREACH_PORT:
215 			TCHECK(dp->icmp_ip.ip_p);
216 			oip = &dp->icmp_ip;
217 			hlen = oip->ip_hl * 4;
218 			ouh = (struct udphdr *)(((u_char *)oip) + hlen);
219 			dport = ntohs(ouh->uh_dport);
220 			switch (oip->ip_p) {
221 
222 			case IPPROTO_TCP:
223 				(void)snprintf(buf, sizeof buf,
224 					"%s tcp port %s unreachable",
225 					ipaddr_string(&oip->ip_dst),
226 					tcpport_string(dport));
227 				break;
228 
229 			case IPPROTO_UDP:
230 				(void)snprintf(buf, sizeof buf,
231 					"%s udp port %s unreachable",
232 					ipaddr_string(&oip->ip_dst),
233 					udpport_string(dport));
234 				break;
235 
236 			default:
237 				(void)snprintf(buf, sizeof buf,
238 					"%s protocol %d port %d unreachable",
239 					ipaddr_string(&oip->ip_dst),
240 					oip->ip_p, dport);
241 				break;
242 			}
243 			break;
244 
245 		case ICMP_UNREACH_NEEDFRAG:
246 			{
247 			register const struct mtu_discovery *mp;
248 
249 			mp = (struct mtu_discovery *)&dp->icmp_void;
250                         mtu = EXTRACT_16BITS(&mp->nexthopmtu);
251                         if (mtu)
252 			    (void)snprintf(buf, sizeof buf,
253 				"%s unreachable - need to frag (mtu %d)",
254 				ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
255                         else
256 			    (void)snprintf(buf, sizeof buf,
257 				"%s unreachable - need to frag",
258 				ipaddr_string(&dp->icmp_ip.ip_dst));
259 			}
260 			break;
261 
262 		default:
263 			fmt = tok2str(unreach2str, "#%d %%s unreachable",
264 			    dp->icmp_code);
265 			(void)snprintf(buf, sizeof buf, fmt,
266 			    ipaddr_string(&dp->icmp_ip.ip_dst));
267 			break;
268 		}
269 		break;
270 
271 	case ICMP_REDIRECT:
272 		TCHECK(dp->icmp_ip.ip_dst);
273 		fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
274 		    dp->icmp_code);
275 		(void)snprintf(buf, sizeof buf, fmt,
276 		    ipaddr_string(&dp->icmp_ip.ip_dst),
277 		    ipaddr_string(&dp->icmp_gwaddr));
278 		break;
279 
280 	case ICMP_ROUTERADVERT:
281 		{
282 		register const struct ih_rdiscovery *ihp;
283 		register const struct id_rdiscovery *idp;
284 		u_int lifetime, num, size;
285 
286 		(void)strlcpy(buf, "router advertisement", sizeof(buf));
287 
288 		ihp = (struct ih_rdiscovery *)&dp->icmp_void;
289 		TCHECK(*ihp);
290 		(void)strlcat(buf, " lifetime ", sizeof(buf));
291 		lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
292 		if (lifetime < 60)
293 			(void)snprintf(buf2, sizeof(buf2), "%u", lifetime);
294 		else if (lifetime < 60 * 60)
295 			(void)snprintf(buf2, sizeof(buf2), "%u:%02u",
296 			    lifetime / 60, lifetime % 60);
297 		else
298 			(void)snprintf(buf2, sizeof(buf2), "%u:%02u:%02u",
299 			    lifetime / 3600, (lifetime % 3600) / 60,
300 			    lifetime % 60);
301 		strlcat(buf, buf2, sizeof(buf));
302 
303 		num = ihp->ird_addrnum;
304 		(void)snprintf(buf2, sizeof(buf2), " %d:", num);
305 		strlcat(buf, buf2, sizeof(buf));
306 
307 		size = ihp->ird_addrsiz;
308 		if (size != 2) {
309 			(void)snprintf(buf2, sizeof(buf2), " [size %d]", size);
310 			strlcat(buf, buf2, sizeof(buf));
311 			break;
312 		}
313 		idp = (struct id_rdiscovery *)&dp->icmp_data;
314 		while (num-- > 0) {
315 			TCHECK(*idp);
316 			(void)snprintf(buf2, sizeof(buf2), " {%s %u}",
317 			    ipaddr_string(&idp->ird_addr),
318 			    EXTRACT_32BITS(&idp->ird_pref));
319 			strlcat(buf, buf2, sizeof(buf));
320 		}
321 		}
322 		break;
323 
324 	case ICMP_TIMXCEED:
325 		TCHECK(dp->icmp_ip.ip_dst);
326 		switch (dp->icmp_code) {
327 
328 		case ICMP_TIMXCEED_INTRANS:
329 			str = "time exceeded in-transit";
330 			break;
331 
332 		case ICMP_TIMXCEED_REASS:
333 			str = "ip reassembly time exceeded";
334 			break;
335 
336 		default:
337 			(void)snprintf(buf, sizeof buf,
338 				"time exceeded-#%d", dp->icmp_code);
339 			break;
340 		}
341 		break;
342 
343 	case ICMP_PARAMPROB:
344 		switch (dp->icmp_code) {
345 		case ICMP_PARAMPROB_OPTABSENT:
346 			str = "requested option absent";
347 			break;
348 		case ICMP_PARAMPROB_LENGTH:
349 			snprintf(buf, sizeof buf, "bad length %d", dp->icmp_pptr);
350 			break;
351 		default:
352 			TCHECK(dp->icmp_pptr);
353 			(void)snprintf(buf, sizeof buf,
354 				"parameter problem - octet %d",
355 				dp->icmp_pptr);
356 			break;
357 		}
358 		break;
359 
360 	case ICMP_MASKREPLY:
361 		TCHECK(dp->icmp_mask);
362 		(void)snprintf(buf, sizeof buf, "address mask is 0x%08x",
363 		    (u_int32_t)ntohl(dp->icmp_mask));
364 		break;
365 
366 	default:
367 		str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
368 		break;
369 	}
370         (void)printf("icmp: %s", str);
371 	return;
372 trunc:
373 	fputs("[|icmp]", stdout);
374 }
375