2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 /* \summary: Internet Control Message Protocol (ICMP) printer */
26 #include "netdissect-stdinc.h"
31 #define ND_LONGJMP_FROM_TCHECK
32 #include "netdissect.h"
33 #include "addrtoname.h"
42 * Interface Control Message Protocol Definitions.
43 * Per RFC 792, September 1981.
47 * Structure of an icmp header.
50 nd_uint8_t icmp_type
; /* type of message, see below */
51 nd_uint8_t icmp_code
; /* type sub code */
52 nd_uint16_t icmp_cksum
; /* ones complement cksum of struct */
54 nd_uint8_t ih_pptr
; /* ICMP_PARAMPROB */
55 nd_ipv4 ih_gwaddr
; /* ICMP_REDIRECT */
60 struct ih_idseqx
{ /* RFC8335 */
67 #define icmp_pptr icmp_hun.ih_pptr
68 #define icmp_gwaddr icmp_hun.ih_gwaddr
69 #define icmp_id icmp_hun.ih_idseq.icd_id
70 #define icmp_seq icmp_hun.ih_idseq.icd_seq
71 #define icmp_xseq icmp_hun.ih_idseqx.icdx_seq
72 #define icmp_xinfo icmp_hun.ih_idseqx.icdx_info
73 #define icmp_void icmp_hun.ih_void
76 nd_uint32_t its_otime
;
77 nd_uint32_t its_rtime
;
78 nd_uint32_t its_ttime
;
82 /* options and then 64 bits of data */
87 #define icmp_otime icmp_dun.id_ts.its_otime
88 #define icmp_rtime icmp_dun.id_ts.its_rtime
89 #define icmp_ttime icmp_dun.id_ts.its_ttime
90 #define icmp_ip icmp_dun.id_ip.idi_ip
91 #define icmp_mask icmp_dun.id_mask
92 #define icmp_data icmp_dun.id_data
96 * Lower bounds on packet lengths for various types.
97 * For the error advice packets must first insure that the
98 * packet is large enough to contain the returned ip header.
99 * Only then can we do the check to see if 64 bits of packet
100 * data have been returned, since we need to check the returned
103 #define ICMP_MINLEN 8 /* abs minimum */
104 #define ICMP_EXTD_MINLEN (156 - sizeof (struct ip)) /* draft-bonica-internet-icmp-08 */
105 #define ICMP_TSLEN (8 + 3 * sizeof (uint32_t)) /* timestamp */
106 #define ICMP_MASKLEN 12 /* address mask */
107 #define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
108 #define ICMP_ADVLEN(p) (8 + (IP_HL(&(p)->icmp_ip) << 2) + 8)
109 /* N.B.: must separately check that ip_hl >= 5 */
112 * Definition of type and code field values.
114 #define ICMP_ECHOREPLY 0 /* echo reply */
115 #define ICMP_UNREACH 3 /* dest unreachable, codes: */
116 #define ICMP_UNREACH_NET 0 /* bad net */
117 #define ICMP_UNREACH_HOST 1 /* bad host */
118 #define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
119 #define ICMP_UNREACH_PORT 3 /* bad port */
120 #define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
121 #define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
122 #define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
123 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
124 #define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
125 #define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
126 #define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
127 #define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
128 #define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
129 #define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
130 #define ICMP_REDIRECT 5 /* shorter route, codes: */
131 #define ICMP_REDIRECT_NET 0 /* for network */
132 #define ICMP_REDIRECT_HOST 1 /* for host */
133 #define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
134 #define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
135 #define ICMP_ECHO 8 /* echo service */
136 #define ICMP_ROUTERADVERT 9 /* router advertisement */
137 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
138 #define ICMP_TIMXCEED 11 /* time exceeded, code: */
139 #define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
140 #define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
141 #define ICMP_PARAMPROB 12 /* ip header bad */
142 #define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
143 #define ICMP_TSTAMP 13 /* timestamp request */
144 #define ICMP_TSTAMPREPLY 14 /* timestamp reply */
145 #define ICMP_IREQ 15 /* information request */
146 #define ICMP_IREQREPLY 16 /* information reply */
147 #define ICMP_MASKREQ 17 /* address mask request */
148 #define ICMP_MASKREPLY 18 /* address mask reply */
150 #define ICMP_EXTENDED_ECHO_REQUEST 42 /* extended echo request */
151 #define ICMP_EXTENDED_ECHO_REPLY 43 /* extended echo reply */
152 #define ICMP_ECHO_X_MALFORMED_QUERY 1 /* malformed query */
153 #define ICMP_ECHO_X_NO_SUCH_INTERFACE 2 /* no such interface */
154 #define ICMP_ECHO_X_NO_SUCH_TABLE_ENTRY 3 /* no such table entry */
155 #define ICMP_ECHO_X_MULTIPLE_INTERFACES 4 /* multiple interfaces satisfy query */
157 #define ICMP_ERRTYPE(type) \
158 ((type) == ICMP_UNREACH || (type) == ICMP_SOURCEQUENCH || \
159 (type) == ICMP_REDIRECT || (type) == ICMP_TIMXCEED || \
160 (type) == ICMP_PARAMPROB)
161 #define ICMP_MULTIPART_EXT_TYPE(type) \
162 ((type) == ICMP_UNREACH || \
163 (type) == ICMP_TIMXCEED || \
164 (type) == ICMP_PARAMPROB)
165 #define ICMP_EXTENDED_ECHO_TYPE(type) \
166 ((type) == ICMP_EXTENDED_ECHO_REQUEST || \
167 (type) == ICMP_EXTENDED_ECHO_REPLY)
169 #ifndef ICMP_UNREACH_NET_UNKNOWN
170 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
172 #ifndef ICMP_UNREACH_HOST_UNKNOWN
173 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
175 #ifndef ICMP_UNREACH_ISOLATED
176 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
178 #ifndef ICMP_UNREACH_NET_PROHIB
179 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
181 #ifndef ICMP_UNREACH_HOST_PROHIB
182 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
184 #ifndef ICMP_UNREACH_TOSNET
185 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
187 #ifndef ICMP_UNREACH_TOSHOST
188 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
192 #ifndef ICMP_UNREACH_FILTER_PROHIB
193 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
195 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
196 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
198 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
199 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
202 /* Most of the icmp types */
203 static const struct tok icmp2str
[] = {
204 { ICMP_ECHOREPLY
, "echo reply" },
205 { ICMP_SOURCEQUENCH
, "source quench" },
206 { ICMP_ECHO
, "echo request" },
207 { ICMP_ROUTERSOLICIT
, "router solicitation" },
208 { ICMP_TSTAMP
, "time stamp request" },
209 { ICMP_TSTAMPREPLY
, "time stamp reply" },
210 { ICMP_IREQ
, "information request" },
211 { ICMP_IREQREPLY
, "information reply" },
212 { ICMP_MASKREQ
, "address mask request" },
213 { ICMP_EXTENDED_ECHO_REQUEST
, "extended echo request" },
214 { ICMP_EXTENDED_ECHO_REPLY
, "extended echo reply" },
218 static const struct tok icmp_extended_echo_reply_code_str
[] = {
220 { ICMP_ECHO_X_MALFORMED_QUERY
, "Malformed Query" },
221 { ICMP_ECHO_X_NO_SUCH_INTERFACE
, "No Such Interface" },
222 { ICMP_ECHO_X_NO_SUCH_TABLE_ENTRY
, "No Such Table Entry" },
223 { ICMP_ECHO_X_MULTIPLE_INTERFACES
, "Multiple Interfaces Satisfy Query" },
227 static const struct tok icmp_extended_echo_reply_state_str
[] = {
239 struct mtu_discovery
{
241 nd_uint16_t nexthopmtu
;
245 struct ih_rdiscovery
{
246 nd_uint8_t ird_addrnum
;
247 nd_uint8_t ird_addrsiz
;
248 nd_uint16_t ird_lifetime
;
251 struct id_rdiscovery
{
252 nd_uint32_t ird_addr
;
253 nd_uint32_t ird_pref
;
257 * RFC 4884 - Extended ICMP to Support Multi-Part Messages
259 * This is a general extension mechanism, based on the mechanism
260 * in draft-bonica-icmp-mpls-02 ICMP Extensions for MultiProtocol
263 * The Destination Unreachable, Time Exceeded
264 * and Parameter Problem messages are slightly changed as per
265 * the above RFC. A new Length field gets added to give
266 * the caller an idea about the length of the piggybacked
267 * IP packet before the extension header starts.
269 * The Length field represents length of the padded "original datagram"
270 * field measured in 32-bit words.
273 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
275 * | Type | Code | Checksum |
276 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
277 * | unused | Length | unused |
278 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
279 * | Internet Header + leading octets of original datagram |
283 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
287 nd_uint8_t icmp_type
;
288 nd_uint8_t icmp_code
;
289 nd_uint16_t icmp_checksum
;
290 nd_byte icmp_reserved
;
291 nd_uint8_t icmp_length
;
292 nd_byte icmp_reserved2
[2];
293 nd_byte icmp_ext_legacy_header
[128]; /* extension header starts 128 bytes after ICMP header */
294 nd_byte icmp_ext_version_res
[2];
295 nd_uint16_t icmp_ext_checksum
;
296 nd_byte icmp_ext_data
[1];
300 * Extract version from the first octet of icmp_ext_version_res.
302 #define ICMP_EXT_EXTRACT_VERSION(x) (((x)&0xf0)>>4)
307 #define ICMP_EXT_VERSION 2
310 * Extension object class numbers.
312 * Class 1 dates back to draft-bonica-icmp-mpls-02.
314 * Class 2 was used for an "Extended Payload Object Class", which
315 * contained bytes of the payload beyond the first 128 bytes, in
316 * draft-bonica-icmp-mpls-02; it was reassigned to an "Interface
317 * Information Object" in RFC 5837.
319 * Class 3 is defined by RFC8335.
323 #define MPLS_STACK_ENTRY_OBJECT_CLASS 1
325 #define INTERFACE_INFORMATION_OBJECT_CLASS 2
327 #define INTERFACE_IDENTIFICATION_OBJECT_CLASS 3
329 struct icmp_multipart_ext_object_header_t
{
331 nd_uint8_t class_num
;
335 static const struct tok icmp_multipart_ext_obj_values
[] = {
336 { MPLS_STACK_ENTRY_OBJECT_CLASS
, "MPLS Stack Entry Object" },
337 { INTERFACE_INFORMATION_OBJECT_CLASS
, "Interface Information Object" },
338 { INTERFACE_IDENTIFICATION_OBJECT_CLASS
, "Interface Identification Object" },
343 static const struct tok icmp_interface_information_role_values
[] = {
344 { 0, "Incoming IP Interface"},
345 { 1, "Sub-IP Component of Incoming IP Interface"},
346 { 2, "Outgoing IP Interface"},
352 Interface IP Address Sub-Object
354 +-------+-------+-------+-------+
356 +-------+-------+-------+-------+
359 struct icmp_interface_information_ipaddr_subobject_t
{
361 nd_uint16_t reserved
;
366 Interface Name Sub-Object
368 +--------+-----------................-----------------+
369 | length | interface name octets 1-63 |
370 +--------+-----------................-----------------+
372 struct icmp_interface_information_ifname_subobject_t
{
378 * Interface Identification IP Address Sub-Object
380 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
381 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382 * | AFI | Address Length| Reserved |
383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 struct icmp_interface_identification_ipaddr_subobject_t
{
394 const char *icmp_tstamp_print(u_int
);
396 /* print the milliseconds since midnight UTC */
398 icmp_tstamp_print(u_int tstamp
)
400 u_int msec
,sec
,min
,hrs
;
404 msec
= tstamp
% 1000;
406 min
= sec
/ 60; sec
-= min
* 60;
407 hrs
= min
/ 60; min
-= hrs
* 60;
408 snprintf(buf
, sizeof(buf
), "%02u:%02u:%02u.%03u",hrs
,min
,sec
,msec
);
413 print_icmp_multipart_ext_object(netdissect_options
*ndo
, const uint8_t *obj_tptr
)
415 u_int obj_tlen
, obj_class_num
, obj_ctype
;
416 const struct icmp_multipart_ext_object_header_t
*icmp_multipart_ext_object_header
;
418 icmp_multipart_ext_object_header
= (const struct icmp_multipart_ext_object_header_t
*)obj_tptr
;
419 obj_tlen
= GET_BE_U_2(icmp_multipart_ext_object_header
->length
);
420 obj_class_num
= GET_U_1(icmp_multipart_ext_object_header
->class_num
);
421 obj_ctype
= GET_U_1(icmp_multipart_ext_object_header
->ctype
);
422 obj_tptr
+= sizeof(struct icmp_multipart_ext_object_header_t
);
424 ND_PRINT("\n\t %s (%u), Class-Type: %u, length %u",
425 tok2str(icmp_multipart_ext_obj_values
,"unknown",obj_class_num
),
430 /* infinite loop protection */
431 if ((obj_class_num
== 0) ||
432 (obj_tlen
< sizeof(struct icmp_multipart_ext_object_header_t
))) {
435 obj_tlen
-= sizeof(struct icmp_multipart_ext_object_header_t
);
437 switch (obj_class_num
) {
438 case MPLS_STACK_ENTRY_OBJECT_CLASS
:
444 raw_label
= GET_BE_U_4(obj_tptr
);
445 ND_PRINT("\n\t label %u, tc %u", MPLS_LABEL(raw_label
), MPLS_TC(raw_label
));
446 if (MPLS_STACK(raw_label
))
448 ND_PRINT(", ttl %u", MPLS_TTL(raw_label
));
452 print_unknown_data(ndo
, obj_tptr
, "\n\t ", obj_tlen
);
456 case INTERFACE_INFORMATION_OBJECT_CLASS
:
459 Ctype in a INTERFACE_INFORMATION_OBJECT_CLASS object:
462 +-------+-------+-------+-------+-------+-------+-------+-------+
463 | Interface Role| Rsvd1 | Rsvd2 |ifIndex| IPAddr| name | MTU |
464 +-------+-------+-------+-------+-------+-------+-------+-------+
466 const uint8_t *offset
;
467 u_int interface_role
, if_index_flag
, ipaddr_flag
, name_flag
, mtu_flag
;
469 interface_role
= (obj_ctype
& 0xc0) >> 6;
470 if_index_flag
= (obj_ctype
& 0x8) >> 3;
471 ipaddr_flag
= (obj_ctype
& 0x4) >> 2;
472 name_flag
= (obj_ctype
& 0x2) >> 1;
473 mtu_flag
= (obj_ctype
& 0x1);
475 ND_PRINT("\n\t Interface Role: %s",
476 tok2str(icmp_interface_information_role_values
,
477 "an unknown interface role",interface_role
));
482 ND_PRINT("\n\t Interface Index: %u", GET_BE_U_4(offset
));
486 const struct icmp_interface_information_ipaddr_subobject_t
*ipaddr_subobj
;
488 ND_PRINT("\n\t IP Address sub-object: ");
489 ipaddr_subobj
= (const struct icmp_interface_information_ipaddr_subobject_t
*) offset
;
490 switch (GET_BE_U_2(ipaddr_subobj
->afi
)) {
492 ND_PRINT("%s", GET_IPADDR_STRING(ipaddr_subobj
->ip_addr
));
496 ND_PRINT("%s", GET_IP6ADDR_STRING(ipaddr_subobj
->ip_addr
));
500 ND_PRINT("Unknown Address Family Identifier");
506 uint8_t inft_name_length_field
;
507 const struct icmp_interface_information_ifname_subobject_t
*ifname_subobj
;
509 ifname_subobj
= (const struct icmp_interface_information_ifname_subobject_t
*) offset
;
510 inft_name_length_field
= GET_U_1(ifname_subobj
->length
);
511 ND_PRINT("\n\t Interface Name");
512 if (inft_name_length_field
== 0) {
513 ND_PRINT(" [length %u]", inft_name_length_field
);
514 nd_print_invalid(ndo
);
517 if (inft_name_length_field
% 4 != 0) {
518 ND_PRINT(" [length %u != N x 4]", inft_name_length_field
);
519 nd_print_invalid(ndo
);
520 offset
+= inft_name_length_field
;
523 if (inft_name_length_field
> 64) {
524 ND_PRINT(" [length %u > 64]", inft_name_length_field
);
525 nd_print_invalid(ndo
);
526 offset
+= inft_name_length_field
;
529 ND_PRINT(", length %u: ", inft_name_length_field
);
530 nd_printjnp(ndo
, ifname_subobj
->if_name
,
531 inft_name_length_field
- 1);
532 offset
+= inft_name_length_field
;
535 ND_PRINT("\n\t MTU: %u", GET_BE_U_4(offset
));
541 case INTERFACE_IDENTIFICATION_OBJECT_CLASS
:
544 ND_PRINT("\n\t Interface Name, length %d: ", obj_tlen
);
545 nd_printjnp(ndo
, obj_tptr
, obj_tlen
);
548 ND_PRINT("\n\t Interface Index: %u", GET_BE_U_4(obj_tptr
));
552 const struct icmp_interface_identification_ipaddr_subobject_t
*id_ipaddr_subobj
;
553 ND_PRINT("\n\t IP Address sub-object: ");
554 id_ipaddr_subobj
= (const struct icmp_interface_identification_ipaddr_subobject_t
*) obj_tptr
;
555 switch (GET_BE_U_2(id_ipaddr_subobj
->afi
)) {
557 if (GET_U_1(id_ipaddr_subobj
->addrlen
) != 4) {
558 ND_PRINT("[length %d != 4] ", GET_U_1(id_ipaddr_subobj
->addrlen
));
560 ND_PRINT("%s", GET_IPADDR_STRING(id_ipaddr_subobj
->ip_addr
));
563 if (GET_U_1(id_ipaddr_subobj
->addrlen
) != 16) {
564 ND_PRINT("[length %d != 16] ", GET_U_1(id_ipaddr_subobj
->addrlen
));
566 ND_PRINT("%s", GET_IP6ADDR_STRING(id_ipaddr_subobj
->ip_addr
));
569 ND_PRINT("Unknown Address Family Identifier");
575 print_unknown_data(ndo
, obj_tptr
, "\n\t ", obj_tlen
);
581 print_unknown_data(ndo
, obj_tptr
, "\n\t ", obj_tlen
);
584 return obj_tlen
+ sizeof(struct icmp_multipart_ext_object_header_t
);
588 icmp_print(netdissect_options
*ndo
, const u_char
*bp
, u_int plen
,
591 const struct icmp
*dp
;
592 uint8_t icmp_type
, icmp_code
;
593 const struct icmp_ext_t
*ext_dp
;
595 const uint8_t *obj_tptr
;
598 struct cksum_vec vec
[1];
600 ndo
->ndo_protocol
= "icmp";
601 dp
= (const struct icmp
*)bp
;
602 ext_dp
= (const struct icmp_ext_t
*)bp
;
605 icmp_type
= GET_U_1(dp
->icmp_type
);
606 icmp_code
= GET_U_1(dp
->icmp_code
);
611 (void)snprintf(buf
, sizeof(buf
), "echo %s, id %u, seq %u",
612 icmp_type
== ICMP_ECHO
?
614 GET_BE_U_2(dp
->icmp_id
),
615 GET_BE_U_2(dp
->icmp_seq
));
621 case ICMP_UNREACH_NET
:
622 (void)snprintf(buf
, sizeof(buf
),
623 "net %s unreachable",
624 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
627 case ICMP_UNREACH_HOST
:
628 (void)snprintf(buf
, sizeof(buf
),
629 "host %s unreachable",
630 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
633 case ICMP_UNREACH_PROTOCOL
:
634 (void)snprintf(buf
, sizeof(buf
),
635 "%s protocol %u unreachable",
636 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
637 GET_U_1(dp
->icmp_ip
.ip_p
));
640 case ICMP_UNREACH_PORT
:
642 const struct ip
*oip
;
643 const struct udphdr
*ouh
;
648 hlen
= IP_HL(oip
) * 4;
649 ouh
= (const struct udphdr
*)(((const u_char
*)oip
) + hlen
);
650 dport
= GET_BE_U_2(ouh
->uh_dport
);
651 ip_proto
= GET_U_1(oip
->ip_p
);
655 (void)snprintf(buf
, sizeof(buf
),
656 "%s tcp port %s unreachable",
657 GET_IPADDR_STRING(oip
->ip_dst
),
658 tcpport_string(ndo
, dport
));
662 (void)snprintf(buf
, sizeof(buf
),
663 "%s udp port %s unreachable",
664 GET_IPADDR_STRING(oip
->ip_dst
),
665 udpport_string(ndo
, dport
));
669 (void)snprintf(buf
, sizeof(buf
),
670 "%s protocol %u port %u unreachable",
671 GET_IPADDR_STRING(oip
->ip_dst
),
678 case ICMP_UNREACH_NEEDFRAG
:
680 const struct mtu_discovery
*mp
;
683 mp
= (const struct mtu_discovery
*)(const u_char
*)&dp
->icmp_void
;
684 mtu
= GET_BE_U_2(mp
->nexthopmtu
);
686 (void)snprintf(buf
, sizeof(buf
),
687 "%s unreachable - need to frag (mtu %u)",
688 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
), mtu
);
690 (void)snprintf(buf
, sizeof(buf
),
691 "%s unreachable - need to frag",
692 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
697 case ICMP_UNREACH_SRCFAIL
:
698 (void)snprintf(buf
, sizeof(buf
),
699 "%s unreachable - source route failed",
700 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
703 case ICMP_UNREACH_NET_UNKNOWN
:
704 (void)snprintf(buf
, sizeof(buf
),
705 "net %s unreachable - unknown",
706 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
709 case ICMP_UNREACH_HOST_UNKNOWN
:
710 (void)snprintf(buf
, sizeof(buf
),
711 "host %s unreachable - unknown",
712 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
715 case ICMP_UNREACH_ISOLATED
:
716 (void)snprintf(buf
, sizeof(buf
),
717 "%s unreachable - source host isolated",
718 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
721 case ICMP_UNREACH_NET_PROHIB
:
722 (void)snprintf(buf
, sizeof(buf
),
723 "net %s unreachable - admin prohibited",
724 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
727 case ICMP_UNREACH_HOST_PROHIB
:
728 (void)snprintf(buf
, sizeof(buf
),
729 "host %s unreachable - admin prohibited",
730 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
733 case ICMP_UNREACH_TOSNET
:
734 (void)snprintf(buf
, sizeof(buf
),
735 "net %s unreachable - tos prohibited",
736 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
739 case ICMP_UNREACH_TOSHOST
:
740 (void)snprintf(buf
, sizeof(buf
),
741 "host %s unreachable - tos prohibited",
742 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
745 case ICMP_UNREACH_FILTER_PROHIB
:
746 (void)snprintf(buf
, sizeof(buf
),
747 "host %s unreachable - admin prohibited filter",
748 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
751 case ICMP_UNREACH_HOST_PRECEDENCE
:
752 (void)snprintf(buf
, sizeof(buf
),
753 "host %s unreachable - host precedence violation",
754 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
757 case ICMP_UNREACH_PRECEDENCE_CUTOFF
:
758 (void)snprintf(buf
, sizeof(buf
),
759 "host %s unreachable - precedence cutoff",
760 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
));
764 (void)snprintf(buf
, sizeof(buf
),
765 "%s unreachable - #%u",
766 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
775 case ICMP_REDIRECT_NET
:
776 (void)snprintf(buf
, sizeof(buf
),
777 "redirect %s to net %s",
778 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
779 GET_IPADDR_STRING(dp
->icmp_gwaddr
));
782 case ICMP_REDIRECT_HOST
:
783 (void)snprintf(buf
, sizeof(buf
),
784 "redirect %s to host %s",
785 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
786 GET_IPADDR_STRING(dp
->icmp_gwaddr
));
789 case ICMP_REDIRECT_TOSNET
:
790 (void)snprintf(buf
, sizeof(buf
),
791 "redirect-tos %s to net %s",
792 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
793 GET_IPADDR_STRING(dp
->icmp_gwaddr
));
796 case ICMP_REDIRECT_TOSHOST
:
797 (void)snprintf(buf
, sizeof(buf
),
798 "redirect-tos %s to host %s",
799 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
800 GET_IPADDR_STRING(dp
->icmp_gwaddr
));
804 (void)snprintf(buf
, sizeof(buf
),
805 "redirect-#%u %s to %s", icmp_code
,
806 GET_IPADDR_STRING(dp
->icmp_ip
.ip_dst
),
807 GET_IPADDR_STRING(dp
->icmp_gwaddr
));
812 case ICMP_ROUTERADVERT
:
815 const struct ih_rdiscovery
*ihp
;
816 const struct id_rdiscovery
*idp
;
817 u_int lifetime
, num
, size
;
819 (void)snprintf(buf
, sizeof(buf
), "router advertisement");
820 cp
= buf
+ strlen(buf
);
822 ihp
= (const struct ih_rdiscovery
*)&dp
->icmp_void
;
823 (void)strncpy(cp
, " lifetime ", sizeof(buf
) - (cp
- buf
));
824 cp
= buf
+ strlen(buf
);
825 lifetime
= GET_BE_U_2(ihp
->ird_lifetime
);
827 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u",
829 } else if (lifetime
< 60 * 60) {
830 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u:%02u",
831 lifetime
/ 60, lifetime
% 60);
833 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
836 (lifetime
% 3600) / 60,
839 cp
= buf
+ strlen(buf
);
841 num
= GET_U_1(ihp
->ird_addrnum
);
842 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " %u:", num
);
843 cp
= buf
+ strlen(buf
);
845 size
= GET_U_1(ihp
->ird_addrsiz
);
847 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
851 idp
= (const struct id_rdiscovery
*)&dp
->icmp_data
;
853 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " {%s %u}",
854 GET_IPADDR_STRING(idp
->ird_addr
),
855 GET_BE_U_4(idp
->ird_pref
));
856 cp
= buf
+ strlen(buf
);
864 ND_TCHECK_4(dp
->icmp_ip
.ip_dst
);
867 case ICMP_TIMXCEED_INTRANS
:
868 str
= "time exceeded in-transit";
871 case ICMP_TIMXCEED_REASS
:
872 str
= "ip reassembly time exceeded";
876 (void)snprintf(buf
, sizeof(buf
), "time exceeded-#%u",
884 (void)snprintf(buf
, sizeof(buf
),
885 "parameter problem - code %u", icmp_code
);
887 (void)snprintf(buf
, sizeof(buf
),
888 "parameter problem - octet %u",
889 GET_U_1(dp
->icmp_pptr
));
894 (void)snprintf(buf
, sizeof(buf
), "address mask is 0x%08x",
895 GET_BE_U_4(dp
->icmp_mask
));
899 (void)snprintf(buf
, sizeof(buf
),
900 "time stamp query id %u seq %u",
901 GET_BE_U_2(dp
->icmp_id
),
902 GET_BE_U_2(dp
->icmp_seq
));
905 case ICMP_TSTAMPREPLY
:
906 (void)snprintf(buf
, sizeof(buf
),
907 "time stamp reply id %u seq %u: org %s",
908 GET_BE_U_2(dp
->icmp_id
),
909 GET_BE_U_2(dp
->icmp_seq
),
910 icmp_tstamp_print(GET_BE_U_4(dp
->icmp_otime
)));
912 (void)snprintf(buf
+strlen(buf
),sizeof(buf
)-strlen(buf
),", recv %s",
913 icmp_tstamp_print(GET_BE_U_4(dp
->icmp_rtime
)));
914 (void)snprintf(buf
+strlen(buf
),sizeof(buf
)-strlen(buf
),", xmit %s",
915 icmp_tstamp_print(GET_BE_U_4(dp
->icmp_ttime
)));
918 case ICMP_EXTENDED_ECHO_REQUEST
:
919 case ICMP_EXTENDED_ECHO_REPLY
:
920 /* brief info here due to limited buf; more info below */
921 (void)snprintf(buf
, sizeof(buf
), "extended echo %s, id %u, seq %u",
922 icmp_type
== ICMP_EXTENDED_ECHO_REQUEST
?
924 GET_BE_U_2(dp
->icmp_id
),
925 GET_U_1(dp
->icmp_xseq
));
929 str
= tok2str(icmp2str
, "type-#%u", icmp_type
);
932 ND_PRINT("ICMP %s, length %u", str
, plen
);
933 if (ndo
->ndo_vflag
&& !fragmented
) { /* don't attempt checksumming if this is a frag */
934 if (ND_TTEST_LEN(bp
, plen
)) {
937 vec
[0].ptr
= (const uint8_t *)(const void *)dp
;
939 sum
= in_cksum(vec
, 1);
941 uint16_t icmp_sum
= GET_BE_U_2(dp
->icmp_cksum
);
942 ND_PRINT(" (wrong icmp cksum %x (->%x)!)",
944 in_cksum_shouldbe(icmp_sum
, sum
));
950 * print the remnants of the IP packet.
951 * save the snaplength as this may get overridden in the IP printer.
953 if (ndo
->ndo_vflag
>= 1 && ICMP_ERRTYPE(icmp_type
)) {
955 const u_char
*snapend_save
;
959 ip
= (const struct ip
*)bp
;
960 snapend_save
= ndo
->ndo_snapend
;
962 * Update the snapend because extensions (MPLS, ...) may be
963 * present after the IP packet. In this case the current
964 * (outer) packet's snapend is not what ip_print() needs to
965 * decode an IP packet nested in the middle of an ICMP payload.
967 * This prevents that, in ip_print(), for the nested IP packet,
968 * the remaining length < remaining caplen.
970 ndo
->ndo_snapend
= ND_MIN(bp
+ GET_BE_U_2(ip
->ip_len
),
972 ip_print(ndo
, bp
, GET_BE_U_2(ip
->ip_len
));
973 ndo
->ndo_snapend
= snapend_save
;
976 /* ndo_protocol reassignment after ip_print() call */
977 ndo
->ndo_protocol
= "icmp";
980 * Attempt to decode multi-part message extensions (rfc4884) only for some ICMP types.
982 if (ndo
->ndo_vflag
>= 1 && plen
> ICMP_EXTD_MINLEN
&& ICMP_MULTIPART_EXT_TYPE(icmp_type
)) {
983 ND_TCHECK_SIZE(ext_dp
);
986 * Check first if the multi-part extension header shows a non-zero length.
987 * If the length field is not set then silently verify the checksum
988 * to check if an extension header is present. This is expedient,
989 * however not all implementations set the length field proper.
991 if (GET_U_1(ext_dp
->icmp_length
) == 0 &&
992 ND_TTEST_LEN(ext_dp
->icmp_ext_version_res
, plen
- ICMP_EXTD_MINLEN
)) {
993 vec
[0].ptr
= (const uint8_t *)(const void *)&ext_dp
->icmp_ext_version_res
;
994 vec
[0].len
= plen
- ICMP_EXTD_MINLEN
;
995 if (in_cksum(vec
, 1)) {
1000 ND_PRINT("\n\tICMP Multi-Part extension v%u",
1001 ICMP_EXT_EXTRACT_VERSION(*(ext_dp
->icmp_ext_version_res
)));
1004 * Sanity checking of the header.
1006 if (ICMP_EXT_EXTRACT_VERSION(*(ext_dp
->icmp_ext_version_res
)) !=
1008 ND_PRINT(" packet not supported");
1012 hlen
= plen
- ICMP_EXTD_MINLEN
;
1013 if (ND_TTEST_LEN(ext_dp
->icmp_ext_version_res
, hlen
)) {
1014 vec
[0].ptr
= (const uint8_t *)(const void *)&ext_dp
->icmp_ext_version_res
;
1016 ND_PRINT(", checksum 0x%04x (%scorrect), length %u",
1017 GET_BE_U_2(ext_dp
->icmp_ext_checksum
),
1018 in_cksum(vec
, 1) ? "in" : "",
1022 hlen
-= 4; /* subtract common header size */
1023 obj_tptr
= (const uint8_t *)ext_dp
->icmp_ext_data
;
1025 while (hlen
> sizeof(struct icmp_multipart_ext_object_header_t
)) {
1026 int obj_tlen
= print_icmp_multipart_ext_object(ndo
, obj_tptr
);
1028 /* malformed object */
1031 if (hlen
< (u_int
)obj_tlen
)
1034 obj_tptr
+= obj_tlen
;
1038 if (ndo
->ndo_vflag
>= 1 && ICMP_EXTENDED_ECHO_TYPE(icmp_type
)) {
1039 int xinfo
= GET_U_1(dp
->icmp_xinfo
);
1040 switch (icmp_type
) {
1041 case ICMP_EXTENDED_ECHO_REQUEST
:
1042 ND_PRINT("\n\t%s Interface", xinfo
& 1 ? "Local" : "Remote");
1043 if (ICMP_EXT_EXTRACT_VERSION(GET_U_1(dp
->icmp_data
)) != ICMP_EXT_VERSION
) {
1044 nd_print_invalid(ndo
);
1046 // A single extended object. The extended header is not
1047 // located at offset 128 in this case, so we can not use
1048 // icmp_ext_checksum.
1049 uint16_t sum
= GET_BE_U_2(dp
->icmp_data
+ 2);
1050 uint16_t len
= GET_BE_U_2(dp
->icmp_data
+ 4);
1051 // The checksum is over the extended header and the single
1054 vec
[0].ptr
= dp
->icmp_data
;
1056 if (ND_TTEST_LEN(vec
[0].ptr
, vec
[0].len
)) {
1057 ND_PRINT(", checksum 0x%04x (%scorrect), length %u",
1059 in_cksum(vec
, 1) ? "in" : "",
1062 print_icmp_multipart_ext_object(ndo
, dp
->icmp_data
+ 4);
1065 case ICMP_EXTENDED_ECHO_REPLY
:
1067 int state
= ( xinfo
& 0xe0 ) >> 5;
1068 ND_PRINT("\n\tCode %d (%s), State %d (%s), active %d ipv4 %d ipv6 %d",
1069 icmp_code
, tok2str(icmp_extended_echo_reply_code_str
, "Unknown", icmp_code
),
1070 state
, tok2str(icmp_extended_echo_reply_state_str
, "Unknown", state
),