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