2003-01-18 18:18:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "netif/tcpdump.h"
|
|
|
|
#include "lwip/ip.h"
|
2010-02-21 11:35:37 +00:00
|
|
|
#include "lwip/tcp_impl.h"
|
2003-01-18 18:18:02 +00:00
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/inet.h"
|
2007-11-25 14:09:16 +00:00
|
|
|
#include "lwip/inet_chksum.h"
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2010-02-21 11:26:15 +00:00
|
|
|
#ifndef TCPDUMP_DEBUG
|
|
|
|
#define TCPDUMP_DEBUG LWIP_DBG_OFF
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
static FILE *file = NULL;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
tcpdump_init(void)
|
|
|
|
{
|
2003-06-19 10:47:44 +00:00
|
|
|
#define TCPDUMP_FNAME "/tmp/tcpdump"
|
|
|
|
file = fopen(TCPDUMP_FNAME, "w");
|
2003-05-01 13:27:52 +00:00
|
|
|
if (file == NULL) {
|
2003-06-19 10:47:44 +00:00
|
|
|
perror("tcpdump_init: cannot open \""TCPDUMP_FNAME"\" for writing");
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
2003-06-19 10:47:44 +00:00
|
|
|
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: file %s\n", TCPDUMP_FNAME));
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
tcpdump(struct pbuf *p)
|
|
|
|
{
|
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
struct tcp_hdr *tcphdr;
|
2008-05-30 11:58:40 +00:00
|
|
|
#if LWIP_UDP
|
2003-01-18 18:18:02 +00:00
|
|
|
struct udp_hdr *udphdr;
|
2008-05-30 11:58:40 +00:00
|
|
|
#endif
|
2003-01-18 18:18:02 +00:00
|
|
|
char flags[5];
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
int offset;
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (file == NULL) {
|
2003-01-18 18:18:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
#ifdef IPv4
|
|
|
|
iphdr = (struct ip_hdr *)p->payload;
|
2003-05-01 13:27:52 +00:00
|
|
|
switch (IPH_PROTO(iphdr)) {
|
2010-03-26 14:20:39 +00:00
|
|
|
#if LWIP_TCP
|
|
|
|
case IP_PROTO_TCP:
|
2003-01-18 18:18:02 +00:00
|
|
|
tcphdr = (struct tcp_hdr *)((char *)iphdr + IP_HLEN);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pbuf_header(p, -IP_HLEN);
|
2011-07-29 12:38:53 +00:00
|
|
|
if (inet_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
|
|
|
|
(ip_addr_t *)&(iphdr->src),
|
|
|
|
(ip_addr_t *)&(iphdr->dest)) != 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
/* fprintf(file, "chksum 0x%lx ", tcphdr->chksum);
|
|
|
|
tcphdr->chksum = 0;
|
2010-02-09 13:19:01 +00:00
|
|
|
fprintf(file, "should be 0x%lx ", inet_chksum_pseudo(p, (ip_addr_t *)&(iphdr->src),
|
|
|
|
(ip_addr_t *)&(iphdr->dest),
|
2003-01-18 18:18:02 +00:00
|
|
|
IP_PROTO_TCP, p->tot_len));*/
|
|
|
|
fprintf(file, "!chksum ");
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
i = 0;
|
2003-05-01 13:27:52 +00:00
|
|
|
if (TCPH_FLAGS(tcphdr) & TCP_SYN) {
|
2003-01-18 18:18:02 +00:00
|
|
|
flags[i++] = 'S';
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (TCPH_FLAGS(tcphdr) & TCP_PSH) {
|
2003-01-18 18:18:02 +00:00
|
|
|
flags[i++] = 'P';
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (TCPH_FLAGS(tcphdr) & TCP_FIN) {
|
2003-01-18 18:18:02 +00:00
|
|
|
flags[i++] = 'F';
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (TCPH_FLAGS(tcphdr) & TCP_RST) {
|
2003-01-18 18:18:02 +00:00
|
|
|
flags[i++] = 'R';
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (i == 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
flags[i++] = '.';
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
flags[i++] = 0;
|
|
|
|
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ",
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 24) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 16) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 8) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 0) & 0xff,
|
|
|
|
ntohs(tcphdr->src),
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 24) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 16) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 8) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 0) & 0xff,
|
|
|
|
ntohs(tcphdr->dest));
|
|
|
|
offset = TCPH_OFFSET(tcphdr) >> 4;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
len = ntohs(IPH_LEN(iphdr)) - offset * 4 - IP_HLEN;
|
2003-05-01 13:27:52 +00:00
|
|
|
if (len != 0 || flags[0] != '.') {
|
2008-09-30 13:48:07 +00:00
|
|
|
fprintf(file, "%s %u:%u(%u) ",
|
2003-01-18 18:18:02 +00:00
|
|
|
flags,
|
|
|
|
ntohl(tcphdr->seqno),
|
|
|
|
ntohl(tcphdr->seqno) + len,
|
|
|
|
len);
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (TCPH_FLAGS(tcphdr) & TCP_ACK) {
|
2008-09-30 13:48:07 +00:00
|
|
|
fprintf(file, "ack %u ",
|
2003-01-18 18:18:02 +00:00
|
|
|
ntohl(tcphdr->ackno));
|
|
|
|
}
|
|
|
|
fprintf(file, "wnd %u\n",
|
|
|
|
ntohs(tcphdr->wnd));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
fflush(file);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pbuf_header(p, IP_HLEN);
|
|
|
|
break;
|
2010-03-26 14:20:39 +00:00
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
|
2008-05-30 11:58:40 +00:00
|
|
|
#if LWIP_UDP
|
2010-03-26 14:20:39 +00:00
|
|
|
case IP_PROTO_UDP:
|
2003-01-18 18:18:02 +00:00
|
|
|
udphdr = (struct udp_hdr *)((char *)iphdr + IP_HLEN);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pbuf_header(p, -IP_HLEN);
|
2011-07-29 12:38:53 +00:00
|
|
|
if (inet_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
|
|
|
|
(ip_addr_t *)&(iphdr->src),
|
|
|
|
(ip_addr_t *)&(iphdr->dest)) != 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
/* fprintf(file, "chksum 0x%lx ", tcphdr->chksum);
|
|
|
|
tcphdr->chksum = 0;
|
2010-02-09 13:19:01 +00:00
|
|
|
fprintf(file, "should be 0x%lx ", inet_chksum_pseudo(p, (ip_addr_t *)&(iphdr->src),
|
|
|
|
(ip_addr_t *)&(iphdr->dest),
|
2003-01-18 18:18:02 +00:00
|
|
|
IP_PROTO_TCP, p->tot_len));*/
|
|
|
|
fprintf(file, "!chksum ");
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ",
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 24) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 16) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 8) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->src.addr) >> 0) & 0xff,
|
|
|
|
ntohs(udphdr->src),
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 24) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 16) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 8) & 0xff,
|
|
|
|
(int)(ntohl(iphdr->dest.addr) >> 0) & 0xff,
|
|
|
|
ntohs(udphdr->dest));
|
|
|
|
fprintf(file, "U ");
|
|
|
|
len = ntohs(IPH_LEN(iphdr)) - sizeof(struct udp_hdr) - IP_HLEN;
|
|
|
|
fprintf(file, " %d\n", len);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
fflush(file);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
pbuf_header(p, IP_HLEN);
|
|
|
|
break;
|
2008-05-30 11:58:40 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2010-03-26 14:20:39 +00:00
|
|
|
default:
|
|
|
|
LWIP_DEBUGF(TCPDUMP_DEBUG, ("unhandled IP protocol: %d\n", (int)IPH_PROTO(iphdr)));
|
|
|
|
break;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* IPv4 */
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|