00001 /* 00002 * Copyright (c) 2001, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the lwIP TCP/IP stack. 00030 * 00031 * Author: Adam Dunkels <adam@sics.se> 00032 * 00033 * $Id$ 00034 */ 00035 00036 #include <vfs/file.h> 00037 #include <ubixos/kpanic.h> 00038 00039 #include "netif/tcpdump.h" 00040 #include "net/ipv4/ip.h" 00041 #include "net/tcp.h" 00042 #include "net/udp.h" 00043 #include "net/ipv4/inet.h" 00044 00045 fileDescriptor *file = NULL; 00046 00047 void tcpdump_init(void) { 00048 char *fname; 00049 00050 fname = "tcpdump"; 00051 file = fopen(fname, "wb"); 00052 if(file == NULL) { 00053 kpanic("tcpdump_init: fopen\n"); 00054 } 00055 } 00056 00057 void tcpdump(struct pbuf *p) { 00058 /* 00059 struct ip_hdr *iphdr; 00060 struct tcp_hdr *tcphdr; 00061 struct udp_hdr *udphdr; 00062 char flags[5]; 00063 int i; 00064 int len; 00065 int offset; 00066 */ 00067 if (file == NULL) { 00068 return; 00069 } 00070 00071 /* 00072 iphdr = p->payload; 00073 switch(IPH_PROTO(iphdr)) { 00074 case IP_PROTO_TCP: 00075 tcphdr = (struct tcp_hdr *)((char *)iphdr + IP_HLEN); 00076 00077 pbuf_header(p, -IP_HLEN); 00078 if(inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src), 00079 (struct ip_addr *)&(iphdr->dest), 00080 IP_PROTO_TCP, p->tot_len) != 0) { 00081 DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n")); 00082 fprintf(file, "!chksum "); 00083 } 00084 00085 i = 0; 00086 if(TCPH_FLAGS(tcphdr) & TCP_SYN) { 00087 flags[i++] = 'S'; 00088 } 00089 if(TCPH_FLAGS(tcphdr) & TCP_PSH) { 00090 flags[i++] = 'P'; 00091 } 00092 if(TCPH_FLAGS(tcphdr) & TCP_FIN) { 00093 flags[i++] = 'F'; 00094 } 00095 if(TCPH_FLAGS(tcphdr) & TCP_RST) { 00096 flags[i++] = 'R'; 00097 } 00098 if(i == 0) { 00099 flags[i++] = '.'; 00100 } 00101 flags[i++] = 0; 00102 00103 00104 00105 fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ", 00106 (int)(ntohl(iphdr->src.addr) >> 24) & 0xff, 00107 (int)(ntohl(iphdr->src.addr) >> 16) & 0xff, 00108 (int)(ntohl(iphdr->src.addr) >> 8) & 0xff, 00109 (int)(ntohl(iphdr->src.addr) >> 0) & 0xff, 00110 ntohs(tcphdr->src), 00111 (int)(ntohl(iphdr->dest.addr) >> 24) & 0xff, 00112 (int)(ntohl(iphdr->dest.addr) >> 16) & 0xff, 00113 (int)(ntohl(iphdr->dest.addr) >> 8) & 0xff, 00114 (int)(ntohl(iphdr->dest.addr) >> 0) & 0xff, 00115 ntohs(tcphdr->dest)); 00116 offset = TCPH_OFFSET(tcphdr) >> 4; 00117 00118 len = ntohs(IPH_LEN(iphdr)) - offset * 4 - IP_HLEN; 00119 if(len != 0 || flags[0] != '.') { 00120 fprintf(file, "%s %lu:%lu(%u) ", 00121 flags, 00122 ntohl(tcphdr->seqno), 00123 ntohl(tcphdr->seqno) + len, 00124 len); 00125 } 00126 if(TCPH_FLAGS(tcphdr) & TCP_ACK) { 00127 fprintf(file, "ack %lu ", 00128 ntohl(tcphdr->ackno)); 00129 } 00130 fprintf(file, "wnd %u\n", 00131 ntohs(tcphdr->wnd)); 00132 00133 fflush(file); 00134 00135 pbuf_header(p, IP_HLEN); 00136 break; 00137 00138 case IP_PROTO_UDP: 00139 udphdr = (struct udp_hdr *)((char *)iphdr + IP_HLEN); 00140 00141 pbuf_header(p, -IP_HLEN); 00142 if(inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src), 00143 (struct ip_addr *)&(iphdr->dest), 00144 IP_PROTO_UDP, p->tot_len) != 0) { 00145 kprintf("tcpdump: IP checksum failed!\n"); 00146 fprintf(file, "!chksum "); 00147 } 00148 00149 fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ", 00150 (int)(ntohl(iphdr->src.addr) >> 24) & 0xff, 00151 (int)(ntohl(iphdr->src.addr) >> 16) & 0xff, 00152 (int)(ntohl(iphdr->src.addr) >> 8) & 0xff, 00153 (int)(ntohl(iphdr->src.addr) >> 0) & 0xff, 00154 ntohs(udphdr->src), 00155 (int)(ntohl(iphdr->dest.addr) >> 24) & 0xff, 00156 (int)(ntohl(iphdr->dest.addr) >> 16) & 0xff, 00157 (int)(ntohl(iphdr->dest.addr) >> 8) & 0xff, 00158 (int)(ntohl(iphdr->dest.addr) >> 0) & 0xff, 00159 ntohs(udphdr->dest)); 00160 fprintf(file, "U "); 00161 len = ntohs(IPH_LEN(iphdr)) - sizeof(struct udp_hdr) - IP_HLEN; 00162 fprintf(file, " %d\n", len); 00163 00164 fflush(file); 00165 00166 pbuf_header(p, IP_HLEN); 00167 break; 00168 00169 } 00170 */ 00171 } 00172 00173 /*** 00174 END 00175 ***/ 00176