1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*
* author: Darren Bounds <dbounds@intrusense.com>
* copyright: Copyright (C) 2002 by Darren Bounds
* license: This software is under GPL version 2 of license
*
* 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 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
*
* 4g8 official page at http://.net
*/
#include "icmpv4_hdr.h"
void
icmpv4_hdr(u_int8_t *packet, u_int16_t diff)
{
u_int8_t *s_addr, *d_addr;
u_int8_t *icmp_t, *icmp_c;
struct libnet_icmpv4_hdr *icmphdr;
#ifdef DEBUG
fprintf(stdout, "DEBUG: icmpv4_hdr()\n");
#endif
#ifndef icmp_iphdr
#define icmp_iphdr icmphdr->dun.ip.idi_ip
#endif
icmphdr = (struct libnet_icmpv4_hdr *)(packet + IPV4_H + hdr_len);
s_addr = (u_int8_t *)malloc(sizeof(s_addr));
d_addr = (u_int8_t *)malloc(sizeof(d_addr));
icmp_t = retrieve_icmp_type(icmphdr->icmp_type);
icmp_c = retrieve_icmp_code(icmphdr->icmp_type, icmphdr->icmp_code);
fprintf(stdout, "ICMP header: Type: %s(%d) ", icmp_t, icmphdr->icmp_type);
switch(icmphdr->icmp_type)
{
case ICMP_REDIRECT:
fprintf(stdout, "Code: %s(%d) Gateway: %s ",
icmp_c, icmphdr->icmp_code,
libnet_addr2name4(ntohl(icmphdr->hun.gateway), 0));
s_addr = libnet_addr2name4(icmp_iphdr.ip_src.s_addr, 0);
d_addr = libnet_addr2name4(icmp_iphdr.ip_dst.s_addr, 0);
fprintf(stdout, "\n");
fprintf(stdout, "\t Original IP header:\n");
fprintf(stdout, "\t Src Address: %s Dst Address: %s\n", s_addr, d_addr);
fprintf(stdout, "\t TTL: %d ID: %d TOS: 0x%X Len: %d ",
icmp_iphdr.ip_ttl,
ntohs(icmp_iphdr.ip_id),
icmp_iphdr.ip_tos,
ntohs(icmp_iphdr.ip_len));
if(ntohs(icmp_iphdr.ip_off) & IP_DF)
fprintf(stdout, "(DF)");
break;
case ICMP_UNREACH:
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
if(icmphdr->icmp_code == ICMP_UNREACH_NEEDFRAG)
fprintf(stdout, "MTU: %d Pad: %d ", ntohs(icmphdr->hun.frag.mtu), ntohs(icmphdr->hun.frag.pad));
s_addr = libnet_addr2name4(icmp_iphdr.ip_src.s_addr, 0);
d_addr = libnet_addr2name4(icmp_iphdr.ip_dst.s_addr, 0);
fprintf(stdout, "\n");
fprintf(stdout, "\t Original IP header:\n");
fprintf(stdout, "\t Src Address: %s Dst Address: %s\n", s_addr, d_addr);
fprintf(stdout, "\t TTL: %d ID: %d TOS: 0x%X Len: %d ",
icmp_iphdr.ip_ttl,
ntohs(icmp_iphdr.ip_id),
icmp_iphdr.ip_tos,
ntohs(icmp_iphdr.ip_len));
if(ntohs(icmp_iphdr.ip_off) & IP_DF)
fprintf(stdout, "(DF)");
if(libnet_build_ipv4(
ntohs(icmp_iphdr.ip_len),
icmp_iphdr.ip_tos,
ntohs(icmp_iphdr.ip_id),
icmp_iphdr.ip_off,
icmp_iphdr.ip_ttl,
icmp_iphdr.ip_p,
icmp_iphdr.ip_sum,
icmp_iphdr.ip_src.s_addr,
icmp_iphdr.ip_dst.s_addr,
(payload_len == 0) ? NULL : packet + 10,
payload_len,
pkt_d,
0) == -1)
{
fatal_error("Unable to build original IP header: %s", libnet_geterror(pkt_d));
}
if(libnet_build_icmpv4_unreach(
icmphdr->icmp_type,
icmphdr->icmp_code,
icmphdr->icmp_sum,
NULL,
0,
pkt_d,
0) == -1)
{
fatal_error("Unable to build ICMPv4 unreach header: %s", libnet_geterror(pkt_d));
}
break;
case ICMP_TIMXCEED: case ICMP_PARAMPROB:
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
s_addr = libnet_addr2name4(icmp_iphdr.ip_src.s_addr, 0);
d_addr = libnet_addr2name4(icmp_iphdr.ip_dst.s_addr, 0);
fprintf(stdout, "\n");
fprintf(stdout, "\t Original IP header:\n");
fprintf(stdout, "\t Src Address: %s Dst Address: %s\n", s_addr, d_addr);
fprintf(stdout, "\t TTL: %d ID: %d TOS: 0x%X Len: %d ",
icmp_iphdr.ip_ttl,
ntohs(icmp_iphdr.ip_id),
icmp_iphdr.ip_tos,
ntohs(icmp_iphdr.ip_len));
if(ntohs(icmp_iphdr.ip_off) & IP_DF)
fprintf(stdout, "(DF)");
break;
case ICMP_TSTAMPREPLY:
if(icmphdr->icmp_code > 0)
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
fprintf(stdout, "ID: %d Seqn: %d\n", icmphdr->icmp_id, icmphdr->icmp_seq);
fprintf(stdout, "\t Original: %lu Received: %lu Transmit: %lu",
(u_long)ntohl(icmphdr->icmp_otime),
(u_long)ntohl(icmphdr->icmp_rtime),
(u_long)ntohl(icmphdr->icmp_ttime));
break;
case ICMP_MASKREPLY:
if(icmphdr->icmp_code > 0)
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
fprintf(stdout, "ID: %d Seqn: %d\n", icmphdr->icmp_id, icmphdr->icmp_seq);
fprintf(stdout, "\t Mask: %s ", libnet_addr2name4(ntohl(icmphdr->dun.mask), 0));
if(libnet_build_icmpv4_mask(
icmphdr->icmp_type,
icmphdr->icmp_code,
icmphdr->icmp_sum,
icmphdr->icmp_id,
icmphdr->icmp_seq,
ntohl(icmphdr->dun.mask),
(payload_len == 0) ? NULL : packet,
payload_len,
pkt_d,
0) == -1)
{
fatal_error("Unable to build ICMPv4 mask header: %s", libnet_geterror(pkt_d));
}
break;
case ICMP_ECHOREPLY: case ICMP_ECHO:
if(icmphdr->icmp_code > 0)
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
fprintf(stdout, "ID: %d Seqn: %d", icmphdr->icmp_id, icmphdr->icmp_seq);
payload_len = diff - 8;
if(libnet_build_icmpv4_echo(
icmphdr->icmp_type,
icmphdr->icmp_code,
icmphdr->icmp_sum,
icmphdr->icmp_id,
icmphdr->icmp_seq,
(payload_len == 0) ? NULL : packet + hdr_len + IPV4_H + 8,
payload_len,
pkt_d,
0) == -1)
{
fatal_error("Unable to build ICMPv4 echo header: %s", libnet_geterror(pkt_d));
}
break;
case ICMP_MASKREQ: case ICMP_TSTAMP:
if(icmphdr->icmp_code > 0)
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
fprintf(stdout, "ID: %d Seqn: %d", icmphdr->icmp_id, icmphdr->icmp_seq);
if(libnet_build_icmpv4_mask(
icmphdr->icmp_type,
icmphdr->icmp_code,
icmphdr->icmp_sum,
icmphdr->icmp_id,
icmphdr->icmp_seq,
ntohl(icmphdr->dun.mask),
(payload_len == 0) ? NULL : packet,
payload_len,
pkt_d,
0) == -1)
{
fatal_error("Unable to build ICMPv4 mask header: %s", libnet_geterror(pkt_d));
}
break;
case ICMP_TRACEROUTE:
if(icmphdr->icmp_code > 0)
fprintf(stdout, "Code: %s(%d) ", icmp_c, icmphdr->icmp_code);
break;
}
fprintf(stdout, "\n");
return;
}
|