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