print-sl.c revision 1.4
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#include <sys/cdefs.h>
23#ifndef lint
24__RCSID("$NetBSD: print-sl.c,v 1.4 2014/11/20 03:05:03 christos Exp $");
25#endif
26
27#define NETDISSECT_REWORKED
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <tcpdump-stdinc.h>
33
34#include "interface.h"
35#include "extract.h"			/* must come after interface.h */
36
37#include "ip.h"
38#include "tcp.h"
39#include "slcompress.h"
40
41/*
42 * definitions of the pseudo- link-level header attached to slip
43 * packets grabbed by the packet filter (bpf) traffic monitor.
44 */
45#define SLIP_HDRLEN 16
46
47#define SLX_DIR 0
48#define SLX_CHDR 1
49#define CHDR_LEN 15
50
51#define SLIPDIR_IN 0
52#define SLIPDIR_OUT 1
53
54static const char tstr[] = "[|slip]";
55
56static u_int lastlen[2][256];
57static u_int lastconn = 255;
58
59static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
60static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
61
62u_int
63sl_if_print(netdissect_options *ndo,
64            const struct pcap_pkthdr *h, const u_char *p)
65{
66	register u_int caplen = h->caplen;
67	register u_int length = h->len;
68	register const struct ip *ip;
69
70	if (caplen < SLIP_HDRLEN) {
71		ND_PRINT((ndo, "%s", tstr));
72		return (caplen);
73	}
74
75	length -= SLIP_HDRLEN;
76
77	ip = (struct ip *)(p + SLIP_HDRLEN);
78
79	if (ndo->ndo_eflag)
80		sliplink_print(ndo, p, ip, length);
81
82	switch (IP_V(ip)) {
83	case 4:
84	        ip_print(ndo, (u_char *)ip, length);
85		break;
86#ifdef INET6
87	case 6:
88		ip6_print(ndo, (u_char *)ip, length);
89		break;
90#endif
91	default:
92		ND_PRINT((ndo, "ip v%d", IP_V(ip)));
93	}
94
95	return (SLIP_HDRLEN);
96}
97
98u_int
99sl_bsdos_if_print(netdissect_options *ndo,
100                  const struct pcap_pkthdr *h, const u_char *p)
101{
102	register u_int caplen = h->caplen;
103	register u_int length = h->len;
104	register const struct ip *ip;
105
106	if (caplen < SLIP_HDRLEN) {
107		ND_PRINT((ndo, "%s", tstr));
108		return (caplen);
109	}
110
111	length -= SLIP_HDRLEN;
112
113	ip = (struct ip *)(p + SLIP_HDRLEN);
114
115#ifdef notdef
116	if (ndo->ndo_eflag)
117		sliplink_print(ndo, p, ip, length);
118#endif
119
120	ip_print(ndo, (u_char *)ip, length);
121
122	return (SLIP_HDRLEN);
123}
124
125static void
126sliplink_print(netdissect_options *ndo,
127               register const u_char *p, register const struct ip *ip,
128               register u_int length)
129{
130	int dir;
131	u_int hlen;
132
133	dir = p[SLX_DIR];
134	ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O "));
135
136	if (ndo->ndo_nflag) {
137		/* XXX just dump the header */
138		register int i;
139
140		for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
141			ND_PRINT((ndo, "%02x.", p[i]));
142		ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1]));
143		return;
144	}
145	switch (p[SLX_CHDR] & 0xf0) {
146
147	case TYPE_IP:
148		ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN));
149		break;
150
151	case TYPE_UNCOMPRESSED_TCP:
152		/*
153		 * The connection id is stored in the IP protocol field.
154		 * Get it from the link layer since sl_uncompress_tcp()
155		 * has restored the IP header copy to IPPROTO_TCP.
156		 */
157		lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
158		hlen = IP_HL(ip);
159		hlen += TH_OFF((struct tcphdr *)&((int *)ip)[hlen]);
160		lastlen[dir][lastconn] = length - (hlen << 2);
161		ND_PRINT((ndo, "utcp %d: ", lastconn));
162		break;
163
164	default:
165		if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
166			compressed_sl_print(ndo, &p[SLX_CHDR], ip,
167			    length, dir);
168			ND_PRINT((ndo, ": "));
169		} else
170			ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR]));
171	}
172}
173
174static const u_char *
175print_sl_change(netdissect_options *ndo,
176                const char *str, register const u_char *cp)
177{
178	register u_int i;
179
180	if ((i = *cp++) == 0) {
181		i = EXTRACT_16BITS(cp);
182		cp += 2;
183	}
184	ND_PRINT((ndo, " %s%d", str, i));
185	return (cp);
186}
187
188static const u_char *
189print_sl_winchange(netdissect_options *ndo,
190                   register const u_char *cp)
191{
192	register short i;
193
194	if ((i = *cp++) == 0) {
195		i = EXTRACT_16BITS(cp);
196		cp += 2;
197	}
198	if (i >= 0)
199		ND_PRINT((ndo, " W+%d", i));
200	else
201		ND_PRINT((ndo, " W%d", i));
202	return (cp);
203}
204
205static void
206compressed_sl_print(netdissect_options *ndo,
207                    const u_char *chdr, const struct ip *ip,
208                    u_int length, int dir)
209{
210	register const u_char *cp = chdr;
211	register u_int flags, hlen;
212
213	flags = *cp++;
214	if (flags & NEW_C) {
215		lastconn = *cp++;
216		ND_PRINT((ndo, "ctcp %d", lastconn));
217	} else
218		ND_PRINT((ndo, "ctcp *"));
219
220	/* skip tcp checksum */
221	cp += 2;
222
223	switch (flags & SPECIALS_MASK) {
224	case SPECIAL_I:
225		ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn]));
226		break;
227
228	case SPECIAL_D:
229		ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn]));
230		break;
231
232	default:
233		if (flags & NEW_U)
234			cp = print_sl_change(ndo, "U=", cp);
235		if (flags & NEW_W)
236			cp = print_sl_winchange(ndo, cp);
237		if (flags & NEW_A)
238			cp = print_sl_change(ndo, "A+", cp);
239		if (flags & NEW_S)
240			cp = print_sl_change(ndo, "S+", cp);
241		break;
242	}
243	if (flags & NEW_I)
244		cp = print_sl_change(ndo, "I+", cp);
245
246	/*
247	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
248	 * 'cp - chdr' is the length of the compressed header.
249	 * 'length - hlen' is the amount of data in the packet.
250	 */
251	hlen = IP_HL(ip);
252	hlen += TH_OFF((struct tcphdr *)&((int32_t *)ip)[hlen]);
253	lastlen[dir][lastconn] = length - (hlen << 2);
254	ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr)));
255}
256