/****************************************************************************
** File: icmpv6.c
**
** Author: Mike Borella
**
** Comments: Dump ICMPv6 information
**
** $Id: icmpv6.c,v 1.16 2002/02/27 14:42:15 mborella Exp $
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Library General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*****************************************************************************/
#include "ipv6.h"
#include "icmpv6.h"
#define HOLDER_SIZE 64
#define IPV6ADDR_SIZE 16
/*
* ICMPv6 type fields
*/
#define ICMPV6_TYPE_DESTUNREACHABLE 1
#define ICMPV6_TYPE_PACKETTOOBIG 2
#define ICMPV6_TYPE_TIMEEXCEEDED 3
#define ICMPV6_TYPE_PARAMETERPROBLEM 4
#define ICMPV6_TYPE_ECHOREQUEST 128
#define ICMPV6_TYPE_ECHOREPLY 129
#define ICMPV6_TYPE_GROUPMEMQUERY 130
#define ICMPV6_TYPE_GROUPMEMREPORT 131
#define ICMPV6_TYPE_GROUPMEMREDUCTION 132
#define ICMPV6_TYPE_ROUTERSOLICIT 133
#define ICMPV6_TYPE_ROUTERADVERT 134
#define ICMPV6_TYPE_NEIGHBORSOLICIT 135
#define ICMPV6_TYPE_NEIGHBORADVERT 136
#define ICMPV6_TYPE_REDIRECT 137
/*
* ICMPv6 destination unreachable code fields
*/
#define ICMPV6_DUCODE_NOROUTE 0
#define ICMPV6_DUCODE_ADMIN 1
#define ICMPV6_DUCODE_ADDRUNREACHABLE 2
#define ICMPV6_DUCODE_PORTUNREACHABLE 3
/*
* ICMPv6 time exceeded code fields
*/
#define ICMPV6_TECODE_HOPLIMIT 0
#define ICMPV6_TECODE_REASSEMBLY 1
/*
* ICMPv6 parameter problem code fields
*/
#define ICMPV6_PPCODE_BADHEADERFIELD 0
#define ICMPV6_PPCODE_BADNEXTHEADER 1
#define ICMPV6_PPCODE_BADIPV6OPTION 2
/*
* ICMPv6 options
*/
#define ICMPV6_OPTION_SRCLLADDR 1
#define ICMPV6_OPTION_DSTLLADDR 2
#define ICMPV6_OPTION_PREFIX 3
#define ICMPV6_OPTION_REDIRECTEDHDR 4
#define ICMPV6_OPTION_MTU 5
/*
* ICMPv6 option map
*/
strmap_t icmpv6_option_map [] =
{
{ ICMPV6_OPTION_SRCLLADDR, "source link-layer address" },
{ ICMPV6_OPTION_DSTLLADDR, "destination link-layer address" },
{ ICMPV6_OPTION_PREFIX, "prefix" },
{ ICMPV6_OPTION_REDIRECTEDHDR,"redirected header" },
{ ICMPV6_OPTION_MTU, "MTU" },
{ 0, "" }
};
/*
* ICMPv6 common header format
*/
typedef struct icmpv6_header
{
u_int8_t type;
u_int8_t code;
u_int16_t checksum;
} icmpv6_header_t;
/*
* ICMPv6 router advertisement structure
*/
typedef struct icmpv6_routeradvert
{
u_int8_t curr_hop_limit;
u_int8_t mo_reserved;
u_int16_t router_lifetime;
u_int32_t reachable_time;
u_int32_t retransmit_timer;
} icmpv6_routeradvert_t;
/*
* ICMPv6 prefix option
*/
typedef struct icmpv6_option_prefix
{
u_int8_t prefix_length;
u_int8_t la_reserved;
u_int32_t valid_lifetime;
u_int32_t preferred_lifetime;
u_int32_t reserved2;
u_int8_t prefix [16];
} icmpv6_option_prefix_t;
/*
* ICMPv6 type map
*/
strmap_t icmpv6_type_map [] =
{
{ ICMPV6_TYPE_DESTUNREACHABLE, "destination unreachable" },
{ ICMPV6_TYPE_PACKETTOOBIG, "packet too big" },
{ ICMPV6_TYPE_TIMEEXCEEDED, "time exceeded" },
{ ICMPV6_TYPE_PARAMETERPROBLEM, "parameter problem" },
{ ICMPV6_TYPE_ECHOREQUEST, "echo request" },
{ ICMPV6_TYPE_ECHOREPLY, "echo reply" },
{ ICMPV6_TYPE_GROUPMEMQUERY, "group membership query" },
{ ICMPV6_TYPE_GROUPMEMREPORT, "group membership report" },
{ ICMPV6_TYPE_GROUPMEMREDUCTION, "group membership reduction" },
{ ICMPV6_TYPE_ROUTERSOLICIT, "router solicitation" },
{ ICMPV6_TYPE_ROUTERADVERT, "router advertisement" },
{ ICMPV6_TYPE_NEIGHBORSOLICIT, "neighbor solicitation" },
{ ICMPV6_TYPE_NEIGHBORADVERT, "neighbor advertisement" },
{ ICMPV6_TYPE_REDIRECT, "redirect" },
{ 0, "" }
};
/*
* ICMPv6 destination unreachable code map
*/
strmap_t icmpv6_ducode_map [] =
{
{ ICMPV6_DUCODE_NOROUTE, "no route" },
{ ICMPV6_DUCODE_ADMIN, "administratively prohibited" },
{ ICMPV6_DUCODE_ADDRUNREACHABLE, "address unreachable" },
{ ICMPV6_DUCODE_PORTUNREACHABLE, "port unreachable" },
{ 0, "" }
};
extern struct arg_t *my_args;
/*----------------------------------------------------------------------------
**
** dump_icmpv6_options()
**
** Parse ICMPv6 options
**
**----------------------------------------------------------------------------
*/
void dump_icmpv6_options(packet_t * pkt)
{
u_int8_t type;
u_int8_t len;
u_int8_t real_len;
while(get_packet_apparentbytesleft(pkt) >= 2)
{
/* Get type and len */
if (get_packet_bytes((u_int8_t *) &type, pkt, 1) == 0)
return;
if (get_packet_bytes((u_int8_t *) &len, pkt, 1) == 0)
return;
/* Calculate the real length */
real_len = (8 * len) - 2;
/* Display type and code */
if (my_args->m)
{
display_minimal_strmap(type, icmpv6_option_map);
display_minimal_string(" ");
}
else
{
display_strmap("Type", type, icmpv6_option_map);
display(" Length", (u_int8_t *) &len, 1, DISP_DEC);
}
/* Get the value */
switch(type)
{
case ICMPV6_OPTION_SRCLLADDR:
case ICMPV6_OPTION_DSTLLADDR:
{
u_int8_t * value;
/* Allocate memory */
value = my_malloc(real_len);
/* Read and display the address */
if (get_packet_bytes(value, pkt, real_len) == 0)
return;
if (my_args->m)
{
display_minimal(value, real_len, DISP_HEXCOLONS);
display_minimal_string(" ");
}
else
display(" Address", value, real_len, DISP_HEXCOLONS);
/* Free memory */
my_free(value);
}
break;
case ICMPV6_OPTION_PREFIX:
{
/*
* I tried to read the rest of the prefix in using a structure
* and for some weird reason it was misaligned. This is a quick
* but ugly workaround.
*/
u_int8_t prefix_length;
u_int8_t la_reserved;
u_int8_t l_bit, a_bit, reserved1;
u_int32_t valid_lifetime;
u_int32_t preferred_lifetime;
u_int32_t reserved2;
u_int8_t prefix[16];
/* Read the prefix option */
if (get_packet_bytes((u_int8_t *) &prefix_length, pkt, 1) == 0)
return;
if (get_packet_bytes((u_int8_t *) &la_reserved, pkt, 1) == 0)
return;
if (get_packet_bytes((u_int8_t *) &valid_lifetime, pkt, 4) == 0)
return;
if (get_packet_bytes((u_int8_t *) &preferred_lifetime, pkt, 4) == 0)
return;
if (get_packet_bytes((u_int8_t *) &reserved2, pkt, 4) == 0)
return;
if (get_packet_bytes((u_int8_t *) &prefix, pkt, 16) == 0)
return;
/* Conversions */
valid_lifetime = ntohl(valid_lifetime);
preferred_lifetime = ntohl(preferred_lifetime);
reserved2 = ntohl(reserved2);
l_bit = la_reserved >> 7;
a_bit = (la_reserved >> 6) & 0x01;
reserved1 = la_reserved & 0x3f;
/* Display */
if (my_args->m)
{
display_minimal_ipv6((u_int8_t *) &prefix);
display_minimal_string(" ");
}
else
{
display(" Prefix length", (u_int8_t *) &prefix_length,
1, DISP_DEC);
display(" L (on-link) bit", (u_int8_t *) &l_bit, 1, DISP_DEC);
display(" A (auto config) bit", (u_int8_t *) &l_bit, 1,
DISP_DEC);
display(" Reserved", (u_int8_t *) &reserved1, 1, DISP_DEC);
display(" Valid lifetime",
(u_int8_t *) &valid_lifetime, 4, DISP_DEC);
display(" Preferred lifetime",
(u_int8_t *) &preferred_lifetime, 4, DISP_DEC);
display(" Reserved", (u_int8_t *) &reserved2, 4,
DISP_HEX);
display_ipv6(" Prefix", (u_int8_t *) &prefix);
}
}
break;
case ICMPV6_OPTION_REDIRECTEDHDR:
{
}
break;
case ICMPV6_OPTION_MTU:
{
u_int16_t reserved;
u_int32_t mtu;
/* Get the reserved and MTU fields */
if (get_packet_bytes((u_int8_t *) &reserved, pkt, 2) == 0)
return;
if (get_packet_bytes((u_int8_t *) &mtu, pkt, 4) == 0)
return;
/* Conversions */
reserved = ntohs(reserved);
mtu = ntohl(mtu);
/* Display */
if (my_args->m)
{
display_minimal((u_int8_t *) &mtu, 4, DISP_DEC);
display_minimal_string(" ");
}
else
{
display(" Reserved", (u_int8_t *) &reserved, 2, DISP_DEC);
display(" MTU", (u_int8_t *) &mtu, 4, DISP_DEC);
}
}
break;
default:
/* Just get the bytes and dump it as hex */
break;
} /* switch */
} /* while */
}
/*----------------------------------------------------------------------------
**
** dump_icmpv6()
**
** Parse ICMPv6 header and dump fields
**
**----------------------------------------------------------------------------
*/
void dump_icmpv6(packet_t *pkt)
{
icmpv6_header_t icmpv6;
char holder[HOLDER_SIZE];
u_int32_t parameter;
/* Set the layer */
set_layer(LAYER_NETWORK);
/*
* Stats accounting
*/
stats_update(STATS_ICMPV6);
/*
* Get the ICMPv6 header
*/
if (get_packet_bytes((u_int8_t *) &icmpv6, pkt, 4) == 0)
return;
/*
* Conversions
*/
icmpv6.checksum = ntohs(icmpv6.checksum);
/*
* Dump header
*/
if (my_args->m)
{
display_minimal_string("| ICMPv6 ");
display_minimal_string(map2str(icmpv6_type_map, icmpv6.type));
display_minimal_string(" ");
}
else
{
/* announcement */
display_header_banner("ICMPv6 Header");
/* print type */
snprintf(holder, HOLDER_SIZE, "%d (%s)", icmpv6.type,
map2str(icmpv6_type_map, icmpv6.type));
display_string("Type", holder);
/* based on the type, decide what to do with the code */
switch(icmpv6.type)
{
case ICMPV6_TYPE_DESTUNREACHABLE:
snprintf(holder, HOLDER_SIZE, "%d (%s)", icmpv6.code,
map2str(icmpv6_ducode_map, icmpv6.code));
display_string("Code", holder);
break;
default:
display("Code", (u_int8_t *) &icmpv6.code, 1, DISP_DEC);
}
display("Checksum", (u_int8_t *) &icmpv6.checksum, 2, DISP_DEC);
}
/*
* Special processing of the rest of the ICMP part
*/
switch(icmpv6.type)
{
case ICMPV6_TYPE_DESTUNREACHABLE:
case ICMPV6_TYPE_TIMEEXCEEDED:
{
/* skip unused 4 bytes */
if (skip_packet_bytes(pkt, 4) == 0)
break;
/* dump the contained IPv6 header */
if (get_packet_apparentbytesleft(pkt))
dump_ipv6(pkt);
}
break;
case ICMPV6_TYPE_PACKETTOOBIG:
{
/* next 4 bytes should be the mtu, get them, convert, display */
if (get_packet_bytes((u_int8_t *) ¶meter, pkt, 4) == 0)
return;
parameter = ntohl(parameter);
display("MTU", (u_int8_t *) ¶meter, 4, DISP_DEC);
/* dump the contained IPv6 header */
if (get_packet_apparentbytesleft(pkt))
dump_ipv6(pkt);
}
break;
case ICMPV6_TYPE_PARAMETERPROBLEM:
{
/* next 4 bytes should be a pointer, get them, convert, display */
if (get_packet_bytes((u_int8_t *) ¶meter, pkt, 4) == 0)
return;
parameter = ntohl(parameter);
display("Pointer", (u_int8_t *) ¶meter, 4, DISP_DEC);
/* dump the contained IPv6 header */
if (get_packet_apparentbytesleft(pkt))
dump_ipv6(pkt);
}
break;
case ICMPV6_TYPE_ECHOREQUEST:
case ICMPV6_TYPE_ECHOREPLY:
{
u_int16_t ping6_id, ping6_seqno;
u_int8_t bytes_left;
/* Next 4 bytes are an id followed by a seqno: get, convert, display */
if (get_packet_bytes((u_int8_t *) &ping6_id, pkt, 2) == 0)
return;
if (get_packet_bytes((u_int8_t *) &ping6_seqno, pkt, 2) == 0)
return;
/* Conversion */
ping6_id = ntohs(ping6_id);
ping6_seqno = ntohs(ping6_seqno);
/* Display */
if (my_args->m)
{
display_minimal((u_int8_t *) &ping6_seqno, 2, DISP_DEC);
}
else
{
display("Identifier", (u_int8_t *) &ping6_id, 2, DISP_DEC);
display("Sequence number", (u_int8_t *) &ping6_seqno, 2,
DISP_DEC);
}
/* Get and display the rest of the data */
bytes_left = get_packet_apparentbytesleft(pkt);
if (bytes_left <= 0)
break;
dump_hexbytes("Data", pkt, bytes_left, 0);
}
break;
case ICMPV6_TYPE_NEIGHBORSOLICIT:
case ICMPV6_TYPE_NEIGHBORADVERT:
{
u_int32_t reserved;
u_int32_t r_bit, s_bit;
u_int8_t v6addr[IPV6ADDR_SIZE];
/* Get the reserved bytes */
if (get_packet_bytes((u_int8_t *) &reserved, pkt, 4) == 0)
break;
/* Conversion */
reserved = ntohl(reserved);
/* If the message is an advert, pick out the R and S bits */
if (icmpv6.type == ICMPV6_TYPE_NEIGHBORADVERT)
{
r_bit = reserved >> 31;
s_bit = (reserved >> 30) & 0x0001;
reserved = reserved & 0x3fff;
}
/* Next 16 bytes are an IPv6 address */
if (get_packet_bytes((u_int8_t *) &v6addr, pkt, IPV6ADDR_SIZE) == 0)
break;
/* Display */
if (my_args->m)
{
display_minimal_ipv6((u_int8_t *) &v6addr);
display_minimal_string(" ");
}
else
{
if (icmpv6.type == ICMPV6_TYPE_NEIGHBORADVERT)
{
display("R (router) bit", (u_int8_t *) &r_bit, 4, DISP_DEC);
display("S (solicited) bit", (u_int8_t *) &s_bit, 4, DISP_DEC);
}
display("Reserved", (u_int8_t *) &reserved, 4, DISP_DEC);
display_ipv6("Target address", (u_int8_t *) &v6addr);
}
/* Parse options */
dump_icmpv6_options(pkt);
}
break;
case ICMPV6_TYPE_ROUTERSOLICIT:
{
u_int32_t reserved;
/* Get the reserved bytes */
if (get_packet_bytes((u_int8_t *) &reserved, pkt, 4) == 0)
break;
/* Conversion */
reserved = ntohl(reserved);
/* Display */
if (!my_args->m)
display("Reserved", (u_int8_t *) &reserved, 4, DISP_DEC);
/* Parse options */
dump_icmpv6_options(pkt);
}
break;
case ICMPV6_TYPE_ROUTERADVERT:
{
icmpv6_routeradvert_t adv;
u_int8_t m_bit, o_bit;
u_int8_t reserved;
/* Get the advert */
if (get_packet_bytes((u_int8_t *) &adv, pkt,
sizeof(icmpv6_routeradvert_t)) == 0)
break;
/* Conversions */
adv.router_lifetime = ntohs(adv.router_lifetime);
adv.reachable_time = ntohl(adv.reachable_time);
adv.retransmit_timer = ntohl(adv.retransmit_timer);
m_bit = adv.mo_reserved >> 7;
o_bit = (adv.mo_reserved >> 6) & 0x01;
reserved = adv.mo_reserved & 0x3f;
/* Display */
if (my_args->m)
{
display_minimal((u_int8_t *) &adv.router_lifetime, 2, DISP_DEC);
display_minimal_string(" ");
}
else
{
display("Current hop limit", (u_int8_t *) &adv.curr_hop_limit, 1,
DISP_DEC);
display("M (mngd config) bit", (u_int8_t *) &m_bit, 1,
DISP_DEC);
display("O (other config) bit", (u_int8_t *) &o_bit, 1, DISP_DEC);
display("Reserved", (u_int8_t *) &reserved, 1, DISP_DEC);
display("Router lifetime", (u_int8_t *) &adv.router_lifetime, 2,
DISP_DEC);
display("Reachable time", (u_int8_t *) &adv.reachable_time, 4,
DISP_DEC);
display("Retransmit timer", (u_int8_t *) &adv.retransmit_timer, 4,
DISP_DEC);
}
/* Parse options */
dump_icmpv6_options(pkt);
}
break;
default:
{
int bytes_left;
/* Get the remaining number of bytes */
bytes_left = get_packet_apparentbytesleft(pkt);
if (bytes_left <= 0)
break;
dump_hexbytes("Value", pkt, bytes_left, 1);
}
break;
}
/* dump the hex buffer */
hexbuffer_flush();
}