tcp_debug.c revision 1541
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)tcp_debug.c	8.1 (Berkeley) 6/10/93
341541Srgrimes */
351541Srgrimes
361541Srgrimes#ifdef TCPDEBUG
371541Srgrimes/* load symbolic names */
381541Srgrimes#define PRUREQUESTS
391541Srgrimes#define TCPSTATES
401541Srgrimes#define	TCPTIMERS
411541Srgrimes#define	TANAMES
421541Srgrimes#endif
431541Srgrimes
441541Srgrimes#include <sys/param.h>
451541Srgrimes#include <sys/systm.h>
461541Srgrimes#include <sys/mbuf.h>
471541Srgrimes#include <sys/socket.h>
481541Srgrimes#include <sys/socketvar.h>
491541Srgrimes#include <sys/protosw.h>
501541Srgrimes#include <sys/errno.h>
511541Srgrimes
521541Srgrimes#include <net/route.h>
531541Srgrimes#include <net/if.h>
541541Srgrimes
551541Srgrimes#include <netinet/in.h>
561541Srgrimes#include <netinet/in_systm.h>
571541Srgrimes#include <netinet/ip.h>
581541Srgrimes#include <netinet/in_pcb.h>
591541Srgrimes#include <netinet/ip_var.h>
601541Srgrimes#include <netinet/tcp.h>
611541Srgrimes#include <netinet/tcp_fsm.h>
621541Srgrimes#include <netinet/tcp_seq.h>
631541Srgrimes#include <netinet/tcp_timer.h>
641541Srgrimes#include <netinet/tcp_var.h>
651541Srgrimes#include <netinet/tcpip.h>
661541Srgrimes#include <netinet/tcp_debug.h>
671541Srgrimes
681541Srgrimes#ifdef TCPDEBUG
691541Srgrimesint	tcpconsdebug = 0;
701541Srgrimes#endif
711541Srgrimes/*
721541Srgrimes * Tcp debug routines
731541Srgrimes */
741541Srgrimesvoid
751541Srgrimestcp_trace(act, ostate, tp, ti, req)
761541Srgrimes	short act, ostate;
771541Srgrimes	struct tcpcb *tp;
781541Srgrimes	struct tcpiphdr *ti;
791541Srgrimes	int req;
801541Srgrimes{
811541Srgrimes	tcp_seq seq, ack;
821541Srgrimes	int len, flags;
831541Srgrimes	struct tcp_debug *td = &tcp_debug[tcp_debx++];
841541Srgrimes
851541Srgrimes	if (tcp_debx == TCP_NDEBUG)
861541Srgrimes		tcp_debx = 0;
871541Srgrimes	td->td_time = iptime();
881541Srgrimes	td->td_act = act;
891541Srgrimes	td->td_ostate = ostate;
901541Srgrimes	td->td_tcb = (caddr_t)tp;
911541Srgrimes	if (tp)
921541Srgrimes		td->td_cb = *tp;
931541Srgrimes	else
941541Srgrimes		bzero((caddr_t)&td->td_cb, sizeof (*tp));
951541Srgrimes	if (ti)
961541Srgrimes		td->td_ti = *ti;
971541Srgrimes	else
981541Srgrimes		bzero((caddr_t)&td->td_ti, sizeof (*ti));
991541Srgrimes	td->td_req = req;
1001541Srgrimes#ifdef TCPDEBUG
1011541Srgrimes	if (tcpconsdebug == 0)
1021541Srgrimes		return;
1031541Srgrimes	if (tp)
1041541Srgrimes		printf("%x %s:", tp, tcpstates[ostate]);
1051541Srgrimes	else
1061541Srgrimes		printf("???????? ");
1071541Srgrimes	printf("%s ", tanames[act]);
1081541Srgrimes	switch (act) {
1091541Srgrimes
1101541Srgrimes	case TA_INPUT:
1111541Srgrimes	case TA_OUTPUT:
1121541Srgrimes	case TA_DROP:
1131541Srgrimes		if (ti == 0)
1141541Srgrimes			break;
1151541Srgrimes		seq = ti->ti_seq;
1161541Srgrimes		ack = ti->ti_ack;
1171541Srgrimes		len = ti->ti_len;
1181541Srgrimes		if (act == TA_OUTPUT) {
1191541Srgrimes			seq = ntohl(seq);
1201541Srgrimes			ack = ntohl(ack);
1211541Srgrimes			len = ntohs((u_short)len);
1221541Srgrimes		}
1231541Srgrimes		if (act == TA_OUTPUT)
1241541Srgrimes			len -= sizeof (struct tcphdr);
1251541Srgrimes		if (len)
1261541Srgrimes			printf("[%x..%x)", seq, seq+len);
1271541Srgrimes		else
1281541Srgrimes			printf("%x", seq);
1291541Srgrimes		printf("@%x, urp=%x", ack, ti->ti_urp);
1301541Srgrimes		flags = ti->ti_flags;
1311541Srgrimes		if (flags) {
1321541Srgrimes#ifndef lint
1331541Srgrimes			char *cp = "<";
1341541Srgrimes#define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
1351541Srgrimes			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
1361541Srgrimes#endif
1371541Srgrimes			printf(">");
1381541Srgrimes		}
1391541Srgrimes		break;
1401541Srgrimes
1411541Srgrimes	case TA_USER:
1421541Srgrimes		printf("%s", prurequests[req&0xff]);
1431541Srgrimes		if ((req & 0xff) == PRU_SLOWTIMO)
1441541Srgrimes			printf("<%s>", tcptimers[req>>8]);
1451541Srgrimes		break;
1461541Srgrimes	}
1471541Srgrimes	if (tp)
1481541Srgrimes		printf(" -> %s", tcpstates[tp->t_state]);
1491541Srgrimes	/* print out internal state of tp !?! */
1501541Srgrimes	printf("\n");
1511541Srgrimes	if (tp == 0)
1521541Srgrimes		return;
1531541Srgrimes	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
1541541Srgrimes	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
1551541Srgrimes	    tp->snd_max);
1561541Srgrimes	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
1571541Srgrimes	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
1581541Srgrimes#endif /* TCPDEBUG */
1591541Srgrimes}
160