Home | History | Annotate | Download | only in tcpdump
      1 /*
      2  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that: (1) source code distributions
      7  * retain the above copyright notice and this paragraph in its entirety, (2)
      8  * distributions including binary code include the above copyright notice and
      9  * this paragraph in its entirety in the documentation or other materials
     10  * provided with the distribution, and (3) all advertising materials mentioning
     11  * features or use of this software display the following acknowledgement:
     12  * ``This product includes software developed by the University of California,
     13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14  * the University nor the names of its contributors may be used to endorse
     15  * or promote products derived from this software without specific prior
     16  * written permission.
     17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20  */
     21 
     22 #define NETDISSECT_REWORKED
     23 #ifdef HAVE_CONFIG_H
     24 #include "config.h"
     25 #endif
     26 
     27 #include <tcpdump-stdinc.h>
     28 
     29 #include "interface.h"
     30 #include "extract.h"			/* must come after interface.h */
     31 
     32 #include "ip.h"
     33 #include "tcp.h"
     34 #include "slcompress.h"
     35 
     36 /*
     37  * definitions of the pseudo- link-level header attached to slip
     38  * packets grabbed by the packet filter (bpf) traffic monitor.
     39  */
     40 #define SLIP_HDRLEN 16
     41 
     42 #define SLX_DIR 0
     43 #define SLX_CHDR 1
     44 #define CHDR_LEN 15
     45 
     46 #define SLIPDIR_IN 0
     47 #define SLIPDIR_OUT 1
     48 
     49 static const char tstr[] = "[|slip]";
     50 
     51 static u_int lastlen[2][256];
     52 static u_int lastconn = 255;
     53 
     54 static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
     55 static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
     56 
     57 u_int
     58 sl_if_print(netdissect_options *ndo,
     59             const struct pcap_pkthdr *h, const u_char *p)
     60 {
     61 	register u_int caplen = h->caplen;
     62 	register u_int length = h->len;
     63 	register const struct ip *ip;
     64 
     65 	if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) {
     66 		ND_PRINT((ndo, "%s", tstr));
     67 		return (caplen);
     68 	}
     69 
     70 	length -= SLIP_HDRLEN;
     71 
     72 	ip = (struct ip *)(p + SLIP_HDRLEN);
     73 
     74 	if (ndo->ndo_eflag)
     75 		sliplink_print(ndo, p, ip, length);
     76 
     77 	switch (IP_V(ip)) {
     78 	case 4:
     79 	        ip_print(ndo, (u_char *)ip, length);
     80 		break;
     81 	case 6:
     82 		ip6_print(ndo, (u_char *)ip, length);
     83 		break;
     84 	default:
     85 		ND_PRINT((ndo, "ip v%d", IP_V(ip)));
     86 	}
     87 
     88 	return (SLIP_HDRLEN);
     89 }
     90 
     91 u_int
     92 sl_bsdos_if_print(netdissect_options *ndo,
     93                   const struct pcap_pkthdr *h, const u_char *p)
     94 {
     95 	register u_int caplen = h->caplen;
     96 	register u_int length = h->len;
     97 	register const struct ip *ip;
     98 
     99 	if (caplen < SLIP_HDRLEN) {
    100 		ND_PRINT((ndo, "%s", tstr));
    101 		return (caplen);
    102 	}
    103 
    104 	length -= SLIP_HDRLEN;
    105 
    106 	ip = (struct ip *)(p + SLIP_HDRLEN);
    107 
    108 #ifdef notdef
    109 	if (ndo->ndo_eflag)
    110 		sliplink_print(ndo, p, ip, length);
    111 #endif
    112 
    113 	ip_print(ndo, (u_char *)ip, length);
    114 
    115 	return (SLIP_HDRLEN);
    116 }
    117 
    118 static void
    119 sliplink_print(netdissect_options *ndo,
    120                register const u_char *p, register const struct ip *ip,
    121                register u_int length)
    122 {
    123 	int dir;
    124 	u_int hlen;
    125 
    126 	dir = p[SLX_DIR];
    127 	ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O "));
    128 
    129 	if (ndo->ndo_nflag) {
    130 		/* XXX just dump the header */
    131 		register int i;
    132 
    133 		for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
    134 			ND_PRINT((ndo, "%02x.", p[i]));
    135 		ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1]));
    136 		return;
    137 	}
    138 	switch (p[SLX_CHDR] & 0xf0) {
    139 
    140 	case TYPE_IP:
    141 		ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN));
    142 		break;
    143 
    144 	case TYPE_UNCOMPRESSED_TCP:
    145 		/*
    146 		 * The connection id is stored in the IP protocol field.
    147 		 * Get it from the link layer since sl_uncompress_tcp()
    148 		 * has restored the IP header copy to IPPROTO_TCP.
    149 		 */
    150 		lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
    151 		hlen = IP_HL(ip);
    152 		hlen += TH_OFF((struct tcphdr *)&((int *)ip)[hlen]);
    153 		lastlen[dir][lastconn] = length - (hlen << 2);
    154 		ND_PRINT((ndo, "utcp %d: ", lastconn));
    155 		break;
    156 
    157 	default:
    158 		if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
    159 			compressed_sl_print(ndo, &p[SLX_CHDR], ip,
    160 			    length, dir);
    161 			ND_PRINT((ndo, ": "));
    162 		} else
    163 			ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR]));
    164 	}
    165 }
    166 
    167 static const u_char *
    168 print_sl_change(netdissect_options *ndo,
    169                 const char *str, register const u_char *cp)
    170 {
    171 	register u_int i;
    172 
    173 	if ((i = *cp++) == 0) {
    174 		i = EXTRACT_16BITS(cp);
    175 		cp += 2;
    176 	}
    177 	ND_PRINT((ndo, " %s%d", str, i));
    178 	return (cp);
    179 }
    180 
    181 static const u_char *
    182 print_sl_winchange(netdissect_options *ndo,
    183                    register const u_char *cp)
    184 {
    185 	register short i;
    186 
    187 	if ((i = *cp++) == 0) {
    188 		i = EXTRACT_16BITS(cp);
    189 		cp += 2;
    190 	}
    191 	if (i >= 0)
    192 		ND_PRINT((ndo, " W+%d", i));
    193 	else
    194 		ND_PRINT((ndo, " W%d", i));
    195 	return (cp);
    196 }
    197 
    198 static void
    199 compressed_sl_print(netdissect_options *ndo,
    200                     const u_char *chdr, const struct ip *ip,
    201                     u_int length, int dir)
    202 {
    203 	register const u_char *cp = chdr;
    204 	register u_int flags, hlen;
    205 
    206 	flags = *cp++;
    207 	if (flags & NEW_C) {
    208 		lastconn = *cp++;
    209 		ND_PRINT((ndo, "ctcp %d", lastconn));
    210 	} else
    211 		ND_PRINT((ndo, "ctcp *"));
    212 
    213 	/* skip tcp checksum */
    214 	cp += 2;
    215 
    216 	switch (flags & SPECIALS_MASK) {
    217 	case SPECIAL_I:
    218 		ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn]));
    219 		break;
    220 
    221 	case SPECIAL_D:
    222 		ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn]));
    223 		break;
    224 
    225 	default:
    226 		if (flags & NEW_U)
    227 			cp = print_sl_change(ndo, "U=", cp);
    228 		if (flags & NEW_W)
    229 			cp = print_sl_winchange(ndo, cp);
    230 		if (flags & NEW_A)
    231 			cp = print_sl_change(ndo, "A+", cp);
    232 		if (flags & NEW_S)
    233 			cp = print_sl_change(ndo, "S+", cp);
    234 		break;
    235 	}
    236 	if (flags & NEW_I)
    237 		cp = print_sl_change(ndo, "I+", cp);
    238 
    239 	/*
    240 	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
    241 	 * 'cp - chdr' is the length of the compressed header.
    242 	 * 'length - hlen' is the amount of data in the packet.
    243 	 */
    244 	hlen = IP_HL(ip);
    245 	hlen += TH_OFF((struct tcphdr *)&((int32_t *)ip)[hlen]);
    246 	lastlen[dir][lastconn] = length - (hlen << 2);
    247 	ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr)));
    248 }
    249