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