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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Show ICMP packets incoming (and detect bombs)
* modified from ping
* (c) 1994/1995 - Laurent Demailly - <dl@hplyot.obspm.fr>
* as is, no warranty,... see LICENSE for copying,...
*/
/* note : the original bsd code was *very* buggy !!!
it should be ok, now */
#include <string.h>
#include <stddef.h>
#include "defs.h"
#ifndef ANSI_OFFSETOF
#ifndef offsetof
# define offsetof(t,m) (int)((&((t *)0L)->m))
#endif
#endif
char to_hex(a)
int a;
{
return ((char)(a <= 9 ? a + '0' : (a -10) + 'A'));
}
int pr_pack(buf, cc, from)
char *buf; /* ptr to start of IP header */
int cc; /* total size of received packet */
struct sockaddr_in *from; /* address of sender */
{
int iphdrlen,doipdecoding=1;
int iplen;
struct ip *ip; /* ptr to IP header */
register struct icmp *icp; /* ptr to ICMP header */
struct tcphdr *tp; /* ptr to TCP header */
time_t t;
char *pr_type(),*pr_subtype(),*strtime;
struct hostent *hostent=NULL;
struct servent *servent=NULL;
static char prbuf[1024]={'\0'}; /* provide enough room for even the longest hosts*/
/*
* We have to look at the IP header, to get its length.
* We also verify that what follows the IP header contains at
* least an ICMP header (8 bytes minimum).
*/
ip = (struct ip *) buf;
iphdrlen = ip->ip_hl << 2; /* convert # 16-bit words to #bytes */
if (cc < (iphdrlen + ICMP_MINLEN)) {
sprintf(prbuf,"packet too short (%d bytes) from %s", cc,
inet_ntoa(from->sin_addr));
if (syslogdoutput) {
syslog(LOG_WARNING,"%s",prbuf);
} else {
puts(prbuf);
fflush(stdout);
}
return -1;
}
iplen = cc;
cc -= iphdrlen;
icp = (struct icmp *)(buf + iphdrlen);
switch (icp->icmp_type)
{
case ICMP_ECHO :
case ICMP_ECHOREPLY :
doipdecoding=0;
if (verbose<2) break;
case ICMP_SOURCEQUENCH :
case ICMP_TIMXCEED :
case ICMP_REDIRECT :
if (!verbose) break;
default :
if (!nonamequery) {
hostent=gethostbyaddr((char*)&(from->sin_addr.s_addr),
sizeof (struct in_addr),
AF_INET);
}
if (!syslogdoutput) {
t=time((time_t *)NULL); strtime=ctime(&t);
strtime+=4; /* skip day name */
strtime[15]=0; /* keep MMM DD HH:MM:SS */
printf("%s ",strtime);
}
sprintf(prbuf,hostent?"ICMP_%s%s < %s [%s]":"ICMP_%s%s < %s",
pr_type(icp->icmp_type),
icp->icmp_type==ICMP_UNREACH?pr_subtype(icp->icmp_code):"",
inet_ntoa(from->sin_addr),
hostent?hostent->h_name:NULL
);
if ( doipdecoding &&
( cc >= offsetof(struct icmp,icmp_ip.ip_src)+sizeof(icp->icmp_ip.ip_dst) ) )
{
if (showsrcip)
{ /* icp->icmp_ip.ip_src.s_addr == local host, show it
only if requested (might be usefull for host with several
interfaces */
if (!nonamequery) {
hostent=gethostbyaddr((char*)&(icp->icmp_ip.ip_src.s_addr),
sizeof (struct in_addr),
AF_INET);
}
sprintf(prbuf+strlen(prbuf),hostent?" - %s [%s]":" - %s",
inet_ntoa(icp->icmp_ip.ip_src),
hostent?hostent->h_name:NULL);
}
if (cc>=offsetof(struct icmp,icmp_ip.ip_dst)+sizeof(icp->icmp_ip.ip_dst))
{
if (!nonamequery) {
hostent=gethostbyaddr((char*)&(icp->icmp_ip.ip_dst.s_addr),
sizeof (struct in_addr),
AF_INET);
}
sprintf(prbuf+strlen(prbuf),hostent?" > %s [%s]":" > %s",
inet_ntoa(icp->icmp_ip.ip_dst),
hostent?hostent->h_name:NULL);
tp = (struct tcphdr *)((char *)&(icp->icmp_dun)+sizeof(struct ip)) ;
#if defined(__GLIBC__) && (__GLIBC__ >= 2)
if (cc>=offsetof(struct icmp,icmp_dun)+sizeof(struct ip)+offsetof(struct tcphdr,seq)+sizeof(tp->seq))
{
if (noportquery) {
sprintf(prbuf+strlen(prbuf)," sp=%d dp=%d seq=0x%8.8x",
ntohs(tp->source),ntohs(tp->dest),
ntohl(tp->seq));
} else {
if ((servent=getservbyport(ntohs(tp->source),NULL)))
sprintf(prbuf+strlen(prbuf)," sp=%d [%s]",
ntohs(tp->source),servent->s_name);
else
sprintf(prbuf+strlen(prbuf)," sp=%d",tp->source);
if ((servent=getservbyport(ntohs(tp->dest),NULL)))
sprintf(prbuf+strlen(prbuf)," dp=%d [%s] seq=0x%8.8x",
ntohs(tp->dest),servent->s_name,
ntohl(tp->seq));
else
sprintf(prbuf+strlen(prbuf)," dp=%d seq=0x%8.8x",
ntohs(tp->dest),ntohl(tp->seq));
}
}
#else
if (cc>=offsetof(struct icmp,icmp_dun)+sizeof(struct ip)+offsetof(struct tcphdr,th_seq)+sizeof(tp->th_seq))
{
if (noportquery) {
sprintf(prbuf+strlen(prbuf)," sp=%d dp=%d seq=0x%8.8x",
ntohs(tp->th_sport),ntohs(tp->th_dport),
ntohl(tp->th_seq));
} else {
if ((servent=getservbyport(ntohs(tp->th_sport),NULL)))
sprintf(prbuf+strlen(prbuf)," sp=%d [%s]",
ntohs(tp->th_sport),servent->s_name);
else
sprintf(prbuf+strlen(prbuf)," sp=%d",tp->th_sport);
if ((servent=getservbyport(ntohs(tp->th_dport),NULL)))
sprintf(prbuf+strlen(prbuf)," dp=%d [%s] seq=0x%8.8x",
ntohs(tp->th_dport),servent->s_name,
ntohl(tp->th_seq));
else
sprintf(prbuf+strlen(prbuf)," dp=%d seq=0x%8.8x",
ntohs(tp->th_dport),ntohl(tp->th_seq));
}
}
#endif
}
}
sprintf(prbuf+strlen(prbuf)," sz=%d(+%d)",cc,iphdrlen);
if (syslogdoutput) {
syslog(LOG_NOTICE,"%s",prbuf);
} else {
puts(prbuf);
fflush(stdout);
if (verbose>2) { /* hexa dump adapted from a file dump by dl (me!) */
/* certainly not the smartest around, but it works !*/
static char h[] = " ";
static char a[] = " ";
int i,j,b,n, flagNEof;
unsigned char *pbuf=(unsigned char *)buf;
n = 0;
flagNEof = 1;
while (flagNEof) {
i = j = 0;
while (i < 16 && (flagNEof = iplen--)) {
b= (int)(*(pbuf++));
h[j++] = to_hex(b >> 4);
h[j++] = to_hex(b & 0x0F);
j += i % 2 + ((i == 7) << 1);
a[i++] = (b > 31 && b < 127) ? b : '.';
}
if (i==0) break;
while (i < 16) {
h[j++] = ' ';
h[j++] = ' ';
j += i % 2 + ((i == 7) << 1);
a[i++] = ' ';
}
printf("%04X : %s %s\n", n, h, a);
n += 16;
}
}
}
}
return 0;
}
/*
* Convert an ICMP "type" field to a printable string.
* This is called for ICMP packets that are received that are not
* ICMP_ECHOREPLY packets.
*/
char *
pr_type(t)
register int t;
{
static char *ttab[] = {
"Echo_Reply",
"1",
"2",
"Dest_Unreachable",
"Source_Quench",
"Redirect",
"Alternate Host Address",
"7",
"Echo",
"RouterAdvert",
"Router_Solicit",
"Time_Exceeded",
"Parameter_Problem",
"Timestamp",
"Timestamp_Reply",
"Info_Request",
"Info_Reply",
"Mask_Request",
"Mask_Reply",
"19 (Reserved for Security)",
/* 20-29: reserved for robustness experiment */
"20","21","22","23","24","25","26","27","28","29",
"Traceroute",
"Datagram Convers. Err.",
"Mobile Host Redir",
"IPv6 Where-Are-You",
"IPv6 I-Am-Here",
"Mobile Registration Request",
"Mobile Registration Reply",
"Domain Name Req",
"Domain Name Reply",
"SKIP",
"Photuris",
"41 (ICMP messages utilized by experimental mobility protocols such as Seamoby)"
};
if (t < 0 || t > 18) {
static char buf[80];
sprintf(buf,"OUT_OF_RANGE(%d)",t);
return(buf);
}
return(ttab[t]);
}
/*
* Convert an ICMP UNREACH sub-"type" field to a printable string.
*/
char *
pr_subtype(t)
register int t;
{
static char *ttab[] = {
"Net",
"Host",
"Protocol",
"Port",
"Frag",
"Source",
"DestNet",
"DestHost",
"Isolated",
"AuthNet",
"AuthHost",
"NetSvc",
"HostSvc",
"Filtered",
"PrecdViolation",
"PrecdCut"
};
static char buf[80];
if (t < 0 || t > 15) {
sprintf(buf,"[OUT_OF_RANGE(%d)]",t);
} else {
sprintf(buf,"[%s]",ttab[t]);
}
return(buf);
}
|