Home | History | Annotate | Line # | Download | only in dist
print-icmp.c revision 1.1.1.9
      1      1.1  christos /*
      2      1.1  christos  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
      3      1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4      1.1  christos  *
      5      1.1  christos  * Redistribution and use in source and binary forms, with or without
      6      1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7      1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8      1.1  christos  * distributions including binary code include the above copyright notice and
      9      1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10      1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11      1.1  christos  * features or use of this software display the following acknowledgement:
     12      1.1  christos  * ``This product includes software developed by the University of California,
     13      1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14      1.1  christos  * the University nor the names of its contributors may be used to endorse
     15      1.1  christos  * or promote products derived from this software without specific prior
     16      1.1  christos  * written permission.
     17      1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18      1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19      1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20      1.1  christos  */
     21      1.1  christos 
     22  1.1.1.7       spz /* \summary: Internet Control Message Protocol (ICMP) printer */
     23  1.1.1.7       spz 
     24      1.1  christos #ifdef HAVE_CONFIG_H
     25      1.1  christos #include "config.h"
     26      1.1  christos #endif
     27      1.1  christos 
     28  1.1.1.6  christos #include <netdissect-stdinc.h>
     29      1.1  christos 
     30      1.1  christos #include <stdio.h>
     31      1.1  christos #include <string.h>
     32      1.1  christos 
     33  1.1.1.6  christos #include "netdissect.h"
     34      1.1  christos #include "addrtoname.h"
     35  1.1.1.6  christos #include "extract.h"
     36      1.1  christos 
     37      1.1  christos #include "ip.h"
     38      1.1  christos #include "udp.h"
     39      1.1  christos #include "ipproto.h"
     40      1.1  christos #include "mpls.h"
     41      1.1  christos 
     42      1.1  christos /*
     43      1.1  christos  * Interface Control Message Protocol Definitions.
     44      1.1  christos  * Per RFC 792, September 1981.
     45      1.1  christos  */
     46      1.1  christos 
     47      1.1  christos /*
     48      1.1  christos  * Structure of an icmp header.
     49      1.1  christos  */
     50      1.1  christos struct icmp {
     51  1.1.1.4  christos 	uint8_t  icmp_type;		/* type of message, see below */
     52  1.1.1.4  christos 	uint8_t  icmp_code;		/* type sub code */
     53  1.1.1.4  christos 	uint16_t icmp_cksum;		/* ones complement cksum of struct */
     54      1.1  christos 	union {
     55  1.1.1.4  christos 		uint8_t ih_pptr;			/* ICMP_PARAMPROB */
     56      1.1  christos 		struct in_addr ih_gwaddr;	/* ICMP_REDIRECT */
     57      1.1  christos 		struct ih_idseq {
     58  1.1.1.4  christos 			uint16_t icd_id;
     59  1.1.1.4  christos 			uint16_t icd_seq;
     60      1.1  christos 		} ih_idseq;
     61  1.1.1.4  christos 		uint32_t ih_void;
     62      1.1  christos 	} icmp_hun;
     63      1.1  christos #define	icmp_pptr	icmp_hun.ih_pptr
     64      1.1  christos #define	icmp_gwaddr	icmp_hun.ih_gwaddr
     65      1.1  christos #define	icmp_id		icmp_hun.ih_idseq.icd_id
     66      1.1  christos #define	icmp_seq	icmp_hun.ih_idseq.icd_seq
     67      1.1  christos #define	icmp_void	icmp_hun.ih_void
     68      1.1  christos 	union {
     69      1.1  christos 		struct id_ts {
     70  1.1.1.4  christos 			uint32_t its_otime;
     71  1.1.1.4  christos 			uint32_t its_rtime;
     72  1.1.1.4  christos 			uint32_t its_ttime;
     73      1.1  christos 		} id_ts;
     74      1.1  christos 		struct id_ip  {
     75      1.1  christos 			struct ip idi_ip;
     76      1.1  christos 			/* options and then 64 bits of data */
     77      1.1  christos 		} id_ip;
     78  1.1.1.4  christos 		uint32_t id_mask;
     79  1.1.1.4  christos 		uint8_t id_data[1];
     80      1.1  christos 	} icmp_dun;
     81      1.1  christos #define	icmp_otime	icmp_dun.id_ts.its_otime
     82      1.1  christos #define	icmp_rtime	icmp_dun.id_ts.its_rtime
     83      1.1  christos #define	icmp_ttime	icmp_dun.id_ts.its_ttime
     84      1.1  christos #define	icmp_ip		icmp_dun.id_ip.idi_ip
     85      1.1  christos #define	icmp_mask	icmp_dun.id_mask
     86      1.1  christos #define	icmp_data	icmp_dun.id_data
     87      1.1  christos };
     88      1.1  christos 
     89  1.1.1.4  christos #define ICMP_MPLS_EXT_EXTRACT_VERSION(x) (((x)&0xf0)>>4)
     90      1.1  christos #define ICMP_MPLS_EXT_VERSION 2
     91      1.1  christos 
     92      1.1  christos /*
     93      1.1  christos  * Lower bounds on packet lengths for various types.
     94      1.1  christos  * For the error advice packets must first insure that the
     95      1.1  christos  * packet is large enought to contain the returned ip header.
     96      1.1  christos  * Only then can we do the check to see if 64 bits of packet
     97      1.1  christos  * data have been returned, since we need to check the returned
     98      1.1  christos  * ip header length.
     99      1.1  christos  */
    100      1.1  christos #define	ICMP_MINLEN	8				/* abs minimum */
    101      1.1  christos #define ICMP_EXTD_MINLEN (156 - sizeof (struct ip))     /* draft-bonica-internet-icmp-08 */
    102  1.1.1.4  christos #define	ICMP_TSLEN	(8 + 3 * sizeof (uint32_t))	/* timestamp */
    103      1.1  christos #define	ICMP_MASKLEN	12				/* address mask */
    104      1.1  christos #define	ICMP_ADVLENMIN	(8 + sizeof (struct ip) + 8)	/* min */
    105      1.1  christos #define	ICMP_ADVLEN(p)	(8 + (IP_HL(&(p)->icmp_ip) << 2) + 8)
    106      1.1  christos 	/* N.B.: must separately check that ip_hl >= 5 */
    107      1.1  christos 
    108      1.1  christos /*
    109      1.1  christos  * Definition of type and code field values.
    110      1.1  christos  */
    111      1.1  christos #define	ICMP_ECHOREPLY		0		/* echo reply */
    112      1.1  christos #define	ICMP_UNREACH		3		/* dest unreachable, codes: */
    113      1.1  christos #define		ICMP_UNREACH_NET	0		/* bad net */
    114      1.1  christos #define		ICMP_UNREACH_HOST	1		/* bad host */
    115      1.1  christos #define		ICMP_UNREACH_PROTOCOL	2		/* bad protocol */
    116      1.1  christos #define		ICMP_UNREACH_PORT	3		/* bad port */
    117      1.1  christos #define		ICMP_UNREACH_NEEDFRAG	4		/* IP_DF caused drop */
    118      1.1  christos #define		ICMP_UNREACH_SRCFAIL	5		/* src route failed */
    119      1.1  christos #define		ICMP_UNREACH_NET_UNKNOWN 6		/* unknown net */
    120      1.1  christos #define		ICMP_UNREACH_HOST_UNKNOWN 7		/* unknown host */
    121      1.1  christos #define		ICMP_UNREACH_ISOLATED	8		/* src host isolated */
    122      1.1  christos #define		ICMP_UNREACH_NET_PROHIB	9		/* prohibited access */
    123      1.1  christos #define		ICMP_UNREACH_HOST_PROHIB 10		/* ditto */
    124      1.1  christos #define		ICMP_UNREACH_TOSNET	11		/* bad tos for net */
    125      1.1  christos #define		ICMP_UNREACH_TOSHOST	12		/* bad tos for host */
    126      1.1  christos #define	ICMP_SOURCEQUENCH	4		/* packet lost, slow down */
    127      1.1  christos #define	ICMP_REDIRECT		5		/* shorter route, codes: */
    128      1.1  christos #define		ICMP_REDIRECT_NET	0		/* for network */
    129      1.1  christos #define		ICMP_REDIRECT_HOST	1		/* for host */
    130      1.1  christos #define		ICMP_REDIRECT_TOSNET	2		/* for tos and net */
    131      1.1  christos #define		ICMP_REDIRECT_TOSHOST	3		/* for tos and host */
    132      1.1  christos #define	ICMP_ECHO		8		/* echo service */
    133      1.1  christos #define	ICMP_ROUTERADVERT	9		/* router advertisement */
    134      1.1  christos #define	ICMP_ROUTERSOLICIT	10		/* router solicitation */
    135      1.1  christos #define	ICMP_TIMXCEED		11		/* time exceeded, code: */
    136      1.1  christos #define		ICMP_TIMXCEED_INTRANS	0		/* ttl==0 in transit */
    137      1.1  christos #define		ICMP_TIMXCEED_REASS	1		/* ttl==0 in reass */
    138      1.1  christos #define	ICMP_PARAMPROB		12		/* ip header bad */
    139      1.1  christos #define		ICMP_PARAMPROB_OPTABSENT 1		/* req. opt. absent */
    140      1.1  christos #define	ICMP_TSTAMP		13		/* timestamp request */
    141      1.1  christos #define	ICMP_TSTAMPREPLY	14		/* timestamp reply */
    142      1.1  christos #define	ICMP_IREQ		15		/* information request */
    143      1.1  christos #define	ICMP_IREQREPLY		16		/* information reply */
    144      1.1  christos #define	ICMP_MASKREQ		17		/* address mask request */
    145      1.1  christos #define	ICMP_MASKREPLY		18		/* address mask reply */
    146      1.1  christos 
    147      1.1  christos #define	ICMP_MAXTYPE		18
    148      1.1  christos 
    149  1.1.1.5  christos #define ICMP_ERRTYPE(type) \
    150  1.1.1.5  christos 	((type) == ICMP_UNREACH || (type) == ICMP_SOURCEQUENCH || \
    151  1.1.1.5  christos 	(type) == ICMP_REDIRECT || (type) == ICMP_TIMXCEED || \
    152  1.1.1.5  christos 	(type) == ICMP_PARAMPROB)
    153      1.1  christos #define	ICMP_MPLS_EXT_TYPE(type) \
    154      1.1  christos 	((type) == ICMP_UNREACH || \
    155      1.1  christos          (type) == ICMP_TIMXCEED || \
    156      1.1  christos          (type) == ICMP_PARAMPROB)
    157      1.1  christos /* rfc1700 */
    158      1.1  christos #ifndef ICMP_UNREACH_NET_UNKNOWN
    159      1.1  christos #define ICMP_UNREACH_NET_UNKNOWN	6	/* destination net unknown */
    160      1.1  christos #endif
    161      1.1  christos #ifndef ICMP_UNREACH_HOST_UNKNOWN
    162      1.1  christos #define ICMP_UNREACH_HOST_UNKNOWN	7	/* destination host unknown */
    163      1.1  christos #endif
    164      1.1  christos #ifndef ICMP_UNREACH_ISOLATED
    165      1.1  christos #define ICMP_UNREACH_ISOLATED		8	/* source host isolated */
    166      1.1  christos #endif
    167      1.1  christos #ifndef ICMP_UNREACH_NET_PROHIB
    168      1.1  christos #define ICMP_UNREACH_NET_PROHIB		9	/* admin prohibited net */
    169      1.1  christos #endif
    170      1.1  christos #ifndef ICMP_UNREACH_HOST_PROHIB
    171      1.1  christos #define ICMP_UNREACH_HOST_PROHIB	10	/* admin prohibited host */
    172      1.1  christos #endif
    173      1.1  christos #ifndef ICMP_UNREACH_TOSNET
    174      1.1  christos #define ICMP_UNREACH_TOSNET		11	/* tos prohibited net */
    175      1.1  christos #endif
    176      1.1  christos #ifndef ICMP_UNREACH_TOSHOST
    177      1.1  christos #define ICMP_UNREACH_TOSHOST		12	/* tos prohibited host */
    178      1.1  christos #endif
    179      1.1  christos 
    180      1.1  christos /* rfc1716 */
    181      1.1  christos #ifndef ICMP_UNREACH_FILTER_PROHIB
    182      1.1  christos #define ICMP_UNREACH_FILTER_PROHIB	13	/* admin prohibited filter */
    183      1.1  christos #endif
    184      1.1  christos #ifndef ICMP_UNREACH_HOST_PRECEDENCE
    185      1.1  christos #define ICMP_UNREACH_HOST_PRECEDENCE	14	/* host precedence violation */
    186      1.1  christos #endif
    187      1.1  christos #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
    188      1.1  christos #define ICMP_UNREACH_PRECEDENCE_CUTOFF	15	/* precedence cutoff */
    189      1.1  christos #endif
    190      1.1  christos 
    191      1.1  christos /* Most of the icmp types */
    192  1.1.1.3  christos static const struct tok icmp2str[] = {
    193      1.1  christos 	{ ICMP_ECHOREPLY,		"echo reply" },
    194      1.1  christos 	{ ICMP_SOURCEQUENCH,		"source quench" },
    195      1.1  christos 	{ ICMP_ECHO,			"echo request" },
    196      1.1  christos 	{ ICMP_ROUTERSOLICIT,		"router solicitation" },
    197      1.1  christos 	{ ICMP_TSTAMP,			"time stamp request" },
    198      1.1  christos 	{ ICMP_TSTAMPREPLY,		"time stamp reply" },
    199      1.1  christos 	{ ICMP_IREQ,			"information request" },
    200      1.1  christos 	{ ICMP_IREQREPLY,		"information reply" },
    201      1.1  christos 	{ ICMP_MASKREQ,			"address mask request" },
    202      1.1  christos 	{ 0,				NULL }
    203      1.1  christos };
    204      1.1  christos 
    205      1.1  christos /* Formats for most of the ICMP_UNREACH codes */
    206  1.1.1.3  christos static const struct tok unreach2str[] = {
    207      1.1  christos 	{ ICMP_UNREACH_NET,		"net %s unreachable" },
    208      1.1  christos 	{ ICMP_UNREACH_HOST,		"host %s unreachable" },
    209      1.1  christos 	{ ICMP_UNREACH_SRCFAIL,
    210      1.1  christos 	    "%s unreachable - source route failed" },
    211      1.1  christos 	{ ICMP_UNREACH_NET_UNKNOWN,	"net %s unreachable - unknown" },
    212      1.1  christos 	{ ICMP_UNREACH_HOST_UNKNOWN,	"host %s unreachable - unknown" },
    213      1.1  christos 	{ ICMP_UNREACH_ISOLATED,
    214      1.1  christos 	    "%s unreachable - source host isolated" },
    215      1.1  christos 	{ ICMP_UNREACH_NET_PROHIB,
    216      1.1  christos 	    "net %s unreachable - admin prohibited" },
    217      1.1  christos 	{ ICMP_UNREACH_HOST_PROHIB,
    218      1.1  christos 	    "host %s unreachable - admin prohibited" },
    219      1.1  christos 	{ ICMP_UNREACH_TOSNET,
    220      1.1  christos 	    "net %s unreachable - tos prohibited" },
    221      1.1  christos 	{ ICMP_UNREACH_TOSHOST,
    222      1.1  christos 	    "host %s unreachable - tos prohibited" },
    223      1.1  christos 	{ ICMP_UNREACH_FILTER_PROHIB,
    224      1.1  christos 	    "host %s unreachable - admin prohibited filter" },
    225      1.1  christos 	{ ICMP_UNREACH_HOST_PRECEDENCE,
    226      1.1  christos 	    "host %s unreachable - host precedence violation" },
    227      1.1  christos 	{ ICMP_UNREACH_PRECEDENCE_CUTOFF,
    228      1.1  christos 	    "host %s unreachable - precedence cutoff" },
    229      1.1  christos 	{ 0,				NULL }
    230      1.1  christos };
    231      1.1  christos 
    232      1.1  christos /* Formats for the ICMP_REDIRECT codes */
    233  1.1.1.3  christos static const struct tok type2str[] = {
    234      1.1  christos 	{ ICMP_REDIRECT_NET,		"redirect %s to net %s" },
    235      1.1  christos 	{ ICMP_REDIRECT_HOST,		"redirect %s to host %s" },
    236      1.1  christos 	{ ICMP_REDIRECT_TOSNET,		"redirect-tos %s to net %s" },
    237      1.1  christos 	{ ICMP_REDIRECT_TOSHOST,	"redirect-tos %s to host %s" },
    238      1.1  christos 	{ 0,				NULL }
    239      1.1  christos };
    240      1.1  christos 
    241      1.1  christos /* rfc1191 */
    242      1.1  christos struct mtu_discovery {
    243  1.1.1.4  christos 	uint16_t unused;
    244  1.1.1.4  christos 	uint16_t nexthopmtu;
    245      1.1  christos };
    246      1.1  christos 
    247      1.1  christos /* rfc1256 */
    248      1.1  christos struct ih_rdiscovery {
    249  1.1.1.4  christos 	uint8_t ird_addrnum;
    250  1.1.1.4  christos 	uint8_t ird_addrsiz;
    251  1.1.1.4  christos 	uint16_t ird_lifetime;
    252      1.1  christos };
    253      1.1  christos 
    254      1.1  christos struct id_rdiscovery {
    255  1.1.1.4  christos 	uint32_t ird_addr;
    256  1.1.1.4  christos 	uint32_t ird_pref;
    257      1.1  christos };
    258      1.1  christos 
    259      1.1  christos /*
    260      1.1  christos  * draft-bonica-internet-icmp-08
    261      1.1  christos  *
    262      1.1  christos  * The Destination Unreachable, Time Exceeded
    263      1.1  christos  * and Parameter Problem messages are slighly changed as per
    264      1.1  christos  * the above draft. A new Length field gets added to give
    265      1.1  christos  * the caller an idea about the length of the piggypacked
    266      1.1  christos  * IP packet before the MPLS extension header starts.
    267      1.1  christos  *
    268      1.1  christos  * The Length field represents length of the padded "original datagram"
    269      1.1  christos  * field  measured in 32-bit words.
    270      1.1  christos  *
    271      1.1  christos  * 0                   1                   2                   3
    272      1.1  christos  * 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
    273      1.1  christos  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    274      1.1  christos  * |     Type      |     Code      |          Checksum             |
    275      1.1  christos  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    276      1.1  christos  * |     unused    |    Length     |          unused               |
    277      1.1  christos  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    278      1.1  christos  * |      Internet Header + leading octets of original datagram    |
    279      1.1  christos  * |                                                               |
    280      1.1  christos  * |                           //                                  |
    281      1.1  christos  * |                                                               |
    282      1.1  christos  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    283      1.1  christos  */
    284      1.1  christos 
    285      1.1  christos struct icmp_ext_t {
    286  1.1.1.4  christos     uint8_t icmp_type;
    287  1.1.1.4  christos     uint8_t icmp_code;
    288  1.1.1.4  christos     uint8_t icmp_checksum[2];
    289  1.1.1.4  christos     uint8_t icmp_reserved;
    290  1.1.1.4  christos     uint8_t icmp_length;
    291  1.1.1.4  christos     uint8_t icmp_reserved2[2];
    292  1.1.1.4  christos     uint8_t icmp_ext_legacy_header[128]; /* extension header starts 128 bytes after ICMP header */
    293  1.1.1.4  christos     uint8_t icmp_ext_version_res[2];
    294  1.1.1.4  christos     uint8_t icmp_ext_checksum[2];
    295  1.1.1.4  christos     uint8_t icmp_ext_data[1];
    296      1.1  christos };
    297      1.1  christos 
    298      1.1  christos struct icmp_mpls_ext_object_header_t {
    299  1.1.1.4  christos     uint8_t length[2];
    300  1.1.1.4  christos     uint8_t class_num;
    301  1.1.1.4  christos     uint8_t ctype;
    302      1.1  christos };
    303      1.1  christos 
    304      1.1  christos static const struct tok icmp_mpls_ext_obj_values[] = {
    305      1.1  christos     { 1, "MPLS Stack Entry" },
    306      1.1  christos     { 2, "Extended Payload" },
    307      1.1  christos     { 0, NULL}
    308      1.1  christos };
    309      1.1  christos 
    310      1.1  christos /* prototypes */
    311      1.1  christos const char *icmp_tstamp_print(u_int);
    312      1.1  christos 
    313      1.1  christos /* print the milliseconds since midnight UTC */
    314      1.1  christos const char *
    315  1.1.1.5  christos icmp_tstamp_print(u_int tstamp)
    316  1.1.1.5  christos {
    317      1.1  christos     u_int msec,sec,min,hrs;
    318      1.1  christos 
    319      1.1  christos     static char buf[64];
    320      1.1  christos 
    321      1.1  christos     msec = tstamp % 1000;
    322      1.1  christos     sec = tstamp / 1000;
    323      1.1  christos     min = sec / 60; sec -= min * 60;
    324      1.1  christos     hrs = min / 60; min -= hrs * 60;
    325      1.1  christos     snprintf(buf, sizeof(buf), "%02u:%02u:%02u.%03u",hrs,min,sec,msec);
    326      1.1  christos     return buf;
    327      1.1  christos }
    328  1.1.1.4  christos 
    329      1.1  christos void
    330  1.1.1.4  christos icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *bp2,
    331  1.1.1.4  christos            int fragmented)
    332      1.1  christos {
    333      1.1  christos 	char *cp;
    334      1.1  christos 	const struct icmp *dp;
    335      1.1  christos         const struct icmp_ext_t *ext_dp;
    336      1.1  christos 	const struct ip *ip;
    337      1.1  christos 	const char *str, *fmt;
    338      1.1  christos 	const struct ip *oip;
    339      1.1  christos 	const struct udphdr *ouh;
    340  1.1.1.4  christos         const uint8_t *obj_tptr;
    341  1.1.1.4  christos         uint32_t raw_label;
    342      1.1  christos         const u_char *snapend_save;
    343      1.1  christos 	const struct icmp_mpls_ext_object_header_t *icmp_mpls_ext_object_header;
    344      1.1  christos 	u_int hlen, dport, mtu, obj_tlen, obj_class_num, obj_ctype;
    345      1.1  christos 	char buf[MAXHOSTNAMELEN + 100];
    346  1.1.1.2  christos 	struct cksum_vec vec[1];
    347      1.1  christos 
    348  1.1.1.6  christos 	dp = (const struct icmp *)bp;
    349  1.1.1.6  christos         ext_dp = (const struct icmp_ext_t *)bp;
    350  1.1.1.6  christos 	ip = (const struct ip *)bp2;
    351      1.1  christos 	str = buf;
    352      1.1  christos 
    353  1.1.1.4  christos 	ND_TCHECK(dp->icmp_code);
    354      1.1  christos 	switch (dp->icmp_type) {
    355      1.1  christos 
    356      1.1  christos 	case ICMP_ECHO:
    357      1.1  christos 	case ICMP_ECHOREPLY:
    358  1.1.1.4  christos 		ND_TCHECK(dp->icmp_seq);
    359      1.1  christos 		(void)snprintf(buf, sizeof(buf), "echo %s, id %u, seq %u",
    360      1.1  christos                                dp->icmp_type == ICMP_ECHO ?
    361      1.1  christos                                "request" : "reply",
    362      1.1  christos                                EXTRACT_16BITS(&dp->icmp_id),
    363      1.1  christos                                EXTRACT_16BITS(&dp->icmp_seq));
    364      1.1  christos 		break;
    365      1.1  christos 
    366      1.1  christos 	case ICMP_UNREACH:
    367  1.1.1.4  christos 		ND_TCHECK(dp->icmp_ip.ip_dst);
    368      1.1  christos 		switch (dp->icmp_code) {
    369      1.1  christos 
    370      1.1  christos 		case ICMP_UNREACH_PROTOCOL:
    371  1.1.1.4  christos 			ND_TCHECK(dp->icmp_ip.ip_p);
    372      1.1  christos 			(void)snprintf(buf, sizeof(buf),
    373      1.1  christos 			    "%s protocol %d unreachable",
    374  1.1.1.4  christos 			    ipaddr_string(ndo, &dp->icmp_ip.ip_dst),
    375      1.1  christos 			    dp->icmp_ip.ip_p);
    376      1.1  christos 			break;
    377      1.1  christos 
    378      1.1  christos 		case ICMP_UNREACH_PORT:
    379  1.1.1.4  christos 			ND_TCHECK(dp->icmp_ip.ip_p);
    380      1.1  christos 			oip = &dp->icmp_ip;
    381      1.1  christos 			hlen = IP_HL(oip) * 4;
    382  1.1.1.6  christos 			ouh = (const struct udphdr *)(((const u_char *)oip) + hlen);
    383  1.1.1.4  christos 			ND_TCHECK(ouh->uh_dport);
    384      1.1  christos 			dport = EXTRACT_16BITS(&ouh->uh_dport);
    385      1.1  christos 			switch (oip->ip_p) {
    386      1.1  christos 
    387      1.1  christos 			case IPPROTO_TCP:
    388      1.1  christos 				(void)snprintf(buf, sizeof(buf),
    389      1.1  christos 					"%s tcp port %s unreachable",
    390  1.1.1.4  christos 					ipaddr_string(ndo, &oip->ip_dst),
    391  1.1.1.6  christos 					tcpport_string(ndo, dport));
    392      1.1  christos 				break;
    393      1.1  christos 
    394      1.1  christos 			case IPPROTO_UDP:
    395      1.1  christos 				(void)snprintf(buf, sizeof(buf),
    396      1.1  christos 					"%s udp port %s unreachable",
    397  1.1.1.4  christos 					ipaddr_string(ndo, &oip->ip_dst),
    398  1.1.1.6  christos 					udpport_string(ndo, dport));
    399      1.1  christos 				break;
    400      1.1  christos 
    401      1.1  christos 			default:
    402      1.1  christos 				(void)snprintf(buf, sizeof(buf),
    403  1.1.1.9  christos 					"%s protocol %u port %u unreachable",
    404  1.1.1.4  christos 					ipaddr_string(ndo, &oip->ip_dst),
    405      1.1  christos 					oip->ip_p, dport);
    406      1.1  christos 				break;
    407      1.1  christos 			}
    408      1.1  christos 			break;
    409      1.1  christos 
    410      1.1  christos 		case ICMP_UNREACH_NEEDFRAG:
    411      1.1  christos 		    {
    412      1.1  christos 			register const struct mtu_discovery *mp;
    413  1.1.1.6  christos 			mp = (const struct mtu_discovery *)(const u_char *)&dp->icmp_void;
    414      1.1  christos 			mtu = EXTRACT_16BITS(&mp->nexthopmtu);
    415      1.1  christos 			if (mtu) {
    416      1.1  christos 				(void)snprintf(buf, sizeof(buf),
    417      1.1  christos 				    "%s unreachable - need to frag (mtu %d)",
    418  1.1.1.4  christos 				    ipaddr_string(ndo, &dp->icmp_ip.ip_dst), mtu);
    419      1.1  christos 			} else {
    420      1.1  christos 				(void)snprintf(buf, sizeof(buf),
    421      1.1  christos 				    "%s unreachable - need to frag",
    422  1.1.1.4  christos 				    ipaddr_string(ndo, &dp->icmp_ip.ip_dst));
    423      1.1  christos 			}
    424      1.1  christos 		    }
    425      1.1  christos 			break;
    426      1.1  christos 
    427      1.1  christos 		default:
    428      1.1  christos 			fmt = tok2str(unreach2str, "#%d %%s unreachable",
    429      1.1  christos 			    dp->icmp_code);
    430      1.1  christos 			(void)snprintf(buf, sizeof(buf), fmt,
    431  1.1.1.4  christos 			    ipaddr_string(ndo, &dp->icmp_ip.ip_dst));
    432      1.1  christos 			break;
    433      1.1  christos 		}
    434      1.1  christos 		break;
    435      1.1  christos 
    436      1.1  christos 	case ICMP_REDIRECT:
    437  1.1.1.4  christos 		ND_TCHECK(dp->icmp_ip.ip_dst);
    438      1.1  christos 		fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
    439      1.1  christos 		    dp->icmp_code);
    440      1.1  christos 		(void)snprintf(buf, sizeof(buf), fmt,
    441  1.1.1.4  christos 		    ipaddr_string(ndo, &dp->icmp_ip.ip_dst),
    442  1.1.1.4  christos 		    ipaddr_string(ndo, &dp->icmp_gwaddr));
    443      1.1  christos 		break;
    444      1.1  christos 
    445      1.1  christos 	case ICMP_ROUTERADVERT:
    446      1.1  christos 	    {
    447      1.1  christos 		register const struct ih_rdiscovery *ihp;
    448      1.1  christos 		register const struct id_rdiscovery *idp;
    449      1.1  christos 		u_int lifetime, num, size;
    450      1.1  christos 
    451      1.1  christos 		(void)snprintf(buf, sizeof(buf), "router advertisement");
    452      1.1  christos 		cp = buf + strlen(buf);
    453      1.1  christos 
    454  1.1.1.6  christos 		ihp = (const struct ih_rdiscovery *)&dp->icmp_void;
    455  1.1.1.4  christos 		ND_TCHECK(*ihp);
    456      1.1  christos 		(void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
    457      1.1  christos 		cp = buf + strlen(buf);
    458      1.1  christos 		lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
    459      1.1  christos 		if (lifetime < 60) {
    460      1.1  christos 			(void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
    461      1.1  christos 			    lifetime);
    462      1.1  christos 		} else if (lifetime < 60 * 60) {
    463      1.1  christos 			(void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
    464      1.1  christos 			    lifetime / 60, lifetime % 60);
    465      1.1  christos 		} else {
    466      1.1  christos 			(void)snprintf(cp, sizeof(buf) - (cp - buf),
    467      1.1  christos 			    "%u:%02u:%02u",
    468      1.1  christos 			    lifetime / 3600,
    469      1.1  christos 			    (lifetime % 3600) / 60,
    470      1.1  christos 			    lifetime % 60);
    471      1.1  christos 		}
    472      1.1  christos 		cp = buf + strlen(buf);
    473      1.1  christos 
    474      1.1  christos 		num = ihp->ird_addrnum;
    475      1.1  christos 		(void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
    476      1.1  christos 		cp = buf + strlen(buf);
    477      1.1  christos 
    478      1.1  christos 		size = ihp->ird_addrsiz;
    479      1.1  christos 		if (size != 2) {
    480      1.1  christos 			(void)snprintf(cp, sizeof(buf) - (cp - buf),
    481      1.1  christos 			    " [size %d]", size);
    482      1.1  christos 			break;
    483      1.1  christos 		}
    484  1.1.1.6  christos 		idp = (const struct id_rdiscovery *)&dp->icmp_data;
    485      1.1  christos 		while (num-- > 0) {
    486  1.1.1.4  christos 			ND_TCHECK(*idp);
    487      1.1  christos 			(void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
    488  1.1.1.4  christos 			    ipaddr_string(ndo, &idp->ird_addr),
    489      1.1  christos 			    EXTRACT_32BITS(&idp->ird_pref));
    490      1.1  christos 			cp = buf + strlen(buf);
    491      1.1  christos 			++idp;
    492      1.1  christos 		}
    493      1.1  christos 	    }
    494      1.1  christos 		break;
    495      1.1  christos 
    496      1.1  christos 	case ICMP_TIMXCEED:
    497  1.1.1.4  christos 		ND_TCHECK(dp->icmp_ip.ip_dst);
    498      1.1  christos 		switch (dp->icmp_code) {
    499      1.1  christos 
    500      1.1  christos 		case ICMP_TIMXCEED_INTRANS:
    501      1.1  christos 			str = "time exceeded in-transit";
    502      1.1  christos 			break;
    503      1.1  christos 
    504      1.1  christos 		case ICMP_TIMXCEED_REASS:
    505      1.1  christos 			str = "ip reassembly time exceeded";
    506      1.1  christos 			break;
    507      1.1  christos 
    508      1.1  christos 		default:
    509  1.1.1.9  christos 			(void)snprintf(buf, sizeof(buf), "time exceeded-#%u",
    510      1.1  christos 			    dp->icmp_code);
    511      1.1  christos 			break;
    512      1.1  christos 		}
    513      1.1  christos 		break;
    514      1.1  christos 
    515      1.1  christos 	case ICMP_PARAMPROB:
    516      1.1  christos 		if (dp->icmp_code)
    517      1.1  christos 			(void)snprintf(buf, sizeof(buf),
    518  1.1.1.9  christos 			    "parameter problem - code %u", dp->icmp_code);
    519      1.1  christos 		else {
    520  1.1.1.4  christos 			ND_TCHECK(dp->icmp_pptr);
    521      1.1  christos 			(void)snprintf(buf, sizeof(buf),
    522  1.1.1.9  christos 			    "parameter problem - octet %u", dp->icmp_pptr);
    523      1.1  christos 		}
    524      1.1  christos 		break;
    525      1.1  christos 
    526      1.1  christos 	case ICMP_MASKREPLY:
    527  1.1.1.4  christos 		ND_TCHECK(dp->icmp_mask);
    528      1.1  christos 		(void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
    529      1.1  christos 		    EXTRACT_32BITS(&dp->icmp_mask));
    530      1.1  christos 		break;
    531      1.1  christos 
    532      1.1  christos 	case ICMP_TSTAMP:
    533  1.1.1.4  christos 		ND_TCHECK(dp->icmp_seq);
    534      1.1  christos 		(void)snprintf(buf, sizeof(buf),
    535      1.1  christos 		    "time stamp query id %u seq %u",
    536      1.1  christos 		    EXTRACT_16BITS(&dp->icmp_id),
    537      1.1  christos 		    EXTRACT_16BITS(&dp->icmp_seq));
    538      1.1  christos 		break;
    539      1.1  christos 
    540      1.1  christos 	case ICMP_TSTAMPREPLY:
    541  1.1.1.4  christos 		ND_TCHECK(dp->icmp_ttime);
    542      1.1  christos 		(void)snprintf(buf, sizeof(buf),
    543      1.1  christos 		    "time stamp reply id %u seq %u: org %s",
    544      1.1  christos                                EXTRACT_16BITS(&dp->icmp_id),
    545      1.1  christos                                EXTRACT_16BITS(&dp->icmp_seq),
    546      1.1  christos                                icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_otime)));
    547      1.1  christos 
    548      1.1  christos                 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", recv %s",
    549      1.1  christos                          icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_rtime)));
    550      1.1  christos                 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", xmit %s",
    551      1.1  christos                          icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_ttime)));
    552      1.1  christos                 break;
    553      1.1  christos 
    554      1.1  christos 	default:
    555      1.1  christos 		str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
    556      1.1  christos 		break;
    557      1.1  christos 	}
    558  1.1.1.4  christos 	ND_PRINT((ndo, "ICMP %s, length %u", str, plen));
    559  1.1.1.4  christos 	if (ndo->ndo_vflag && !fragmented) { /* don't attempt checksumming if this is a frag */
    560  1.1.1.4  christos 		if (ND_TTEST2(*bp, plen)) {
    561  1.1.1.9  christos 			uint16_t sum;
    562  1.1.1.9  christos 
    563  1.1.1.6  christos 			vec[0].ptr = (const uint8_t *)(const void *)dp;
    564  1.1.1.2  christos 			vec[0].len = plen;
    565  1.1.1.2  christos 			sum = in_cksum(vec, 1);
    566      1.1  christos 			if (sum != 0) {
    567  1.1.1.9  christos 				uint16_t icmp_sum;
    568  1.1.1.9  christos 				ND_TCHECK_16BITS(&dp->icmp_cksum);
    569      1.1  christos 				icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
    570  1.1.1.4  christos 				ND_PRINT((ndo, " (wrong icmp cksum %x (->%x)!)",
    571      1.1  christos 					     icmp_sum,
    572  1.1.1.4  christos 					     in_cksum_shouldbe(icmp_sum, sum)));
    573      1.1  christos 			}
    574      1.1  christos 		}
    575      1.1  christos 	}
    576      1.1  christos 
    577      1.1  christos         /*
    578      1.1  christos          * print the remnants of the IP packet.
    579      1.1  christos          * save the snaplength as this may get overidden in the IP printer.
    580      1.1  christos          */
    581  1.1.1.5  christos 	if (ndo->ndo_vflag >= 1 && ICMP_ERRTYPE(dp->icmp_type)) {
    582      1.1  christos 		bp += 8;
    583  1.1.1.4  christos 		ND_PRINT((ndo, "\n\t"));
    584  1.1.1.6  christos 		ip = (const struct ip *)bp;
    585  1.1.1.4  christos 		ndo->ndo_snaplen = ndo->ndo_snapend - bp;
    586  1.1.1.4  christos                 snapend_save = ndo->ndo_snapend;
    587  1.1.1.8  christos 		ND_TCHECK_16BITS(&ip->ip_len);
    588  1.1.1.4  christos 		ip_print(ndo, bp, EXTRACT_16BITS(&ip->ip_len));
    589  1.1.1.4  christos                 ndo->ndo_snapend = snapend_save;
    590      1.1  christos 	}
    591      1.1  christos 
    592      1.1  christos         /*
    593      1.1  christos          * Attempt to decode the MPLS extensions only for some ICMP types.
    594      1.1  christos          */
    595  1.1.1.4  christos         if (ndo->ndo_vflag >= 1 && plen > ICMP_EXTD_MINLEN && ICMP_MPLS_EXT_TYPE(dp->icmp_type)) {
    596      1.1  christos 
    597  1.1.1.4  christos             ND_TCHECK(*ext_dp);
    598      1.1  christos 
    599      1.1  christos             /*
    600      1.1  christos              * Check first if the mpls extension header shows a non-zero length.
    601      1.1  christos              * If the length field is not set then silently verify the checksum
    602      1.1  christos              * to check if an extension header is present. This is expedient,
    603      1.1  christos              * however not all implementations set the length field proper.
    604      1.1  christos              */
    605  1.1.1.8  christos             if (!ext_dp->icmp_length &&
    606  1.1.1.8  christos                 ND_TTEST2(ext_dp->icmp_ext_version_res, plen - ICMP_EXTD_MINLEN)) {
    607  1.1.1.6  christos                 vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
    608  1.1.1.2  christos                 vec[0].len = plen - ICMP_EXTD_MINLEN;
    609  1.1.1.2  christos                 if (in_cksum(vec, 1)) {
    610  1.1.1.2  christos                     return;
    611  1.1.1.2  christos                 }
    612      1.1  christos             }
    613      1.1  christos 
    614  1.1.1.4  christos             ND_PRINT((ndo, "\n\tMPLS extension v%u",
    615  1.1.1.4  christos                    ICMP_MPLS_EXT_EXTRACT_VERSION(*(ext_dp->icmp_ext_version_res))));
    616  1.1.1.4  christos 
    617      1.1  christos             /*
    618      1.1  christos              * Sanity checking of the header.
    619      1.1  christos              */
    620      1.1  christos             if (ICMP_MPLS_EXT_EXTRACT_VERSION(*(ext_dp->icmp_ext_version_res)) !=
    621      1.1  christos                 ICMP_MPLS_EXT_VERSION) {
    622  1.1.1.4  christos                 ND_PRINT((ndo, " packet not supported"));
    623      1.1  christos                 return;
    624      1.1  christos             }
    625      1.1  christos 
    626      1.1  christos             hlen = plen - ICMP_EXTD_MINLEN;
    627  1.1.1.8  christos             if (ND_TTEST2(ext_dp->icmp_ext_version_res, hlen)) {
    628  1.1.1.8  christos                 vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
    629  1.1.1.8  christos                 vec[0].len = hlen;
    630  1.1.1.8  christos                 ND_PRINT((ndo, ", checksum 0x%04x (%scorrect), length %u",
    631  1.1.1.8  christos                        EXTRACT_16BITS(ext_dp->icmp_ext_checksum),
    632  1.1.1.8  christos                        in_cksum(vec, 1) ? "in" : "",
    633  1.1.1.8  christos                        hlen));
    634  1.1.1.8  christos             }
    635      1.1  christos 
    636      1.1  christos             hlen -= 4; /* subtract common header size */
    637  1.1.1.6  christos             obj_tptr = (const uint8_t *)ext_dp->icmp_ext_data;
    638      1.1  christos 
    639      1.1  christos             while (hlen > sizeof(struct icmp_mpls_ext_object_header_t)) {
    640      1.1  christos 
    641  1.1.1.6  christos                 icmp_mpls_ext_object_header = (const struct icmp_mpls_ext_object_header_t *)obj_tptr;
    642  1.1.1.4  christos                 ND_TCHECK(*icmp_mpls_ext_object_header);
    643      1.1  christos                 obj_tlen = EXTRACT_16BITS(icmp_mpls_ext_object_header->length);
    644      1.1  christos                 obj_class_num = icmp_mpls_ext_object_header->class_num;
    645      1.1  christos                 obj_ctype = icmp_mpls_ext_object_header->ctype;
    646      1.1  christos                 obj_tptr += sizeof(struct icmp_mpls_ext_object_header_t);
    647      1.1  christos 
    648  1.1.1.4  christos                 ND_PRINT((ndo, "\n\t  %s Object (%u), Class-Type: %u, length %u",
    649      1.1  christos                        tok2str(icmp_mpls_ext_obj_values,"unknown",obj_class_num),
    650      1.1  christos                        obj_class_num,
    651      1.1  christos                        obj_ctype,
    652  1.1.1.4  christos                        obj_tlen));
    653      1.1  christos 
    654      1.1  christos                 hlen-=sizeof(struct icmp_mpls_ext_object_header_t); /* length field includes tlv header */
    655      1.1  christos 
    656  1.1.1.4  christos                 /* infinite loop protection */
    657      1.1  christos                 if ((obj_class_num == 0) ||
    658      1.1  christos                     (obj_tlen < sizeof(struct icmp_mpls_ext_object_header_t))) {
    659      1.1  christos                     return;
    660      1.1  christos                 }
    661      1.1  christos                 obj_tlen-=sizeof(struct icmp_mpls_ext_object_header_t);
    662      1.1  christos 
    663      1.1  christos                 switch (obj_class_num) {
    664      1.1  christos                 case 1:
    665      1.1  christos                     switch(obj_ctype) {
    666      1.1  christos                     case 1:
    667  1.1.1.4  christos                         ND_TCHECK2(*obj_tptr, 4);
    668      1.1  christos                         raw_label = EXTRACT_32BITS(obj_tptr);
    669  1.1.1.4  christos                         ND_PRINT((ndo, "\n\t    label %u, exp %u", MPLS_LABEL(raw_label), MPLS_EXP(raw_label)));
    670      1.1  christos                         if (MPLS_STACK(raw_label))
    671  1.1.1.4  christos                             ND_PRINT((ndo, ", [S]"));
    672  1.1.1.4  christos                         ND_PRINT((ndo, ", ttl %u", MPLS_TTL(raw_label)));
    673      1.1  christos                         break;
    674      1.1  christos                     default:
    675  1.1.1.4  christos                         print_unknown_data(ndo, obj_tptr, "\n\t    ", obj_tlen);
    676      1.1  christos                     }
    677      1.1  christos                     break;
    678      1.1  christos 
    679      1.1  christos                /*
    680      1.1  christos                 *  FIXME those are the defined objects that lack a decoder
    681      1.1  christos                 *  you are welcome to contribute code ;-)
    682      1.1  christos                 */
    683      1.1  christos                 case 2:
    684      1.1  christos                 default:
    685  1.1.1.4  christos                     print_unknown_data(ndo, obj_tptr, "\n\t    ", obj_tlen);
    686      1.1  christos                     break;
    687      1.1  christos                 }
    688      1.1  christos                 if (hlen < obj_tlen)
    689      1.1  christos                     break;
    690      1.1  christos                 hlen -= obj_tlen;
    691      1.1  christos                 obj_tptr += obj_tlen;
    692      1.1  christos             }
    693      1.1  christos         }
    694      1.1  christos 
    695      1.1  christos 	return;
    696      1.1  christos trunc:
    697  1.1.1.4  christos 	ND_PRINT((ndo, "[|icmp]"));
    698      1.1  christos }
    699  1.1.1.4  christos /*
    700  1.1.1.4  christos  * Local Variables:
    701  1.1.1.4  christos  * c-style: whitesmith
    702  1.1.1.4  christos  * c-basic-offset: 8
    703  1.1.1.4  christos  * End:
    704  1.1.1.4  christos  */
    705