nrelease - fix/improve livecd
[dragonfly.git] / contrib / tcpdump / print-sl.c
CommitLineData
c8cf0f94
PA
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
411677ae 22/* \summary: Compressed Serial Line Internet Protocol printer */
c8cf0f94
PA
23
24#ifdef HAVE_CONFIG_H
ed775ee7 25#include <config.h>
c8cf0f94
PA
26#endif
27
ed775ee7 28#include "netdissect-stdinc.h"
c8cf0f94 29
ed775ee7 30#define ND_LONGJMP_FROM_TCHECK
411677ae
AL
31#include "netdissect.h"
32#include "extract.h"
c8cf0f94
PA
33
34#include "ip.h"
35#include "tcp.h"
c8cf0f94
PA
36#include "slcompress.h"
37
411677ae
AL
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
411677ae
AL
46
47#define SLIPDIR_IN 0
48#define SLIPDIR_OUT 1
49
411677ae 50
c8cf0f94
PA
51static u_int lastlen[2][256];
52static u_int lastconn = 255;
53
ed775ee7
AHJ
54static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
55static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
c8cf0f94 56
ed775ee7 57void
411677ae
AL
58sl_if_print(netdissect_options *ndo,
59 const struct pcap_pkthdr *h, const u_char *p)
c8cf0f94 60{
ed775ee7
AHJ
61 u_int length = h->len;
62 const struct ip *ip;
c8cf0f94 63
ed775ee7
AHJ
64 ndo->ndo_protocol = "slip";
65 ND_TCHECK_LEN(p, SLIP_HDRLEN);
66 ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
c8cf0f94
PA
67
68 length -= SLIP_HDRLEN;
69
411677ae
AL
70 ip = (const struct ip *)(p + SLIP_HDRLEN);
71
72 if (ndo->ndo_eflag)
ed775ee7 73 sliplink_print(ndo, p, ip, length);
c8cf0f94
PA
74
75 switch (IP_V(ip)) {
76 case 4:
411677ae 77 ip_print(ndo, (const u_char *)ip, length);
c8cf0f94 78 break;
c8cf0f94 79 case 6:
411677ae 80 ip6_print(ndo, (const u_char *)ip, length);
c8cf0f94 81 break;
c8cf0f94 82 default:
ed775ee7 83 ND_PRINT("ip v%u", IP_V(ip));
c8cf0f94 84 }
c8cf0f94
PA
85}
86
ed775ee7 87void
411677ae
AL
88sl_bsdos_if_print(netdissect_options *ndo,
89 const struct pcap_pkthdr *h, const u_char *p)
c8cf0f94 90{
ed775ee7
AHJ
91 u_int length = h->len;
92 const struct ip *ip;
c8cf0f94 93
ed775ee7
AHJ
94 ndo->ndo_protocol = "slip_bsdos";
95 ND_TCHECK_LEN(p, SLIP_HDRLEN);
96 ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
c8cf0f94
PA
97
98 length -= SLIP_HDRLEN;
99
411677ae 100 ip = (const struct ip *)(p + SLIP_HDRLEN);
c8cf0f94
PA
101
102#ifdef notdef
411677ae
AL
103 if (ndo->ndo_eflag)
104 sliplink_print(ndo, p, ip, length);
c8cf0f94
PA
105#endif
106
411677ae 107 ip_print(ndo, (const u_char *)ip, length);
c8cf0f94
PA
108}
109
ed775ee7 110static void
411677ae 111sliplink_print(netdissect_options *ndo,
ed775ee7
AHJ
112 const u_char *p, const struct ip *ip,
113 u_int length)
c8cf0f94
PA
114{
115 int dir;
116 u_int hlen;
117
ed775ee7 118 dir = GET_U_1(p + SLX_DIR);
411677ae
AL
119 switch (dir) {
120
121 case SLIPDIR_IN:
ed775ee7 122 ND_PRINT("I ");
411677ae 123 break;
c8cf0f94 124
411677ae 125 case SLIPDIR_OUT:
ed775ee7 126 ND_PRINT("O ");
411677ae
AL
127 break;
128
129 default:
ed775ee7 130 ND_PRINT("Invalid direction %d ", dir);
411677ae
AL
131 dir = -1;
132 break;
133 }
ed775ee7 134 switch (GET_U_1(p + SLX_CHDR) & 0xf0) {
c8cf0f94
PA
135
136 case TYPE_IP:
ed775ee7 137 ND_PRINT("ip %u: ", length + SLIP_HDRLEN);
c8cf0f94
PA
138 break;
139
140 case TYPE_UNCOMPRESSED_TCP:
141 /*
142 * The connection id is stored in the IP protocol field.
143 * Get it from the link layer since sl_uncompress_tcp()
144 * has restored the IP header copy to IPPROTO_TCP.
145 */
ed775ee7
AHJ
146 lastconn = GET_U_1(((const struct ip *)(p + SLX_CHDR))->ip_p);
147 ND_PRINT("utcp %u: ", lastconn);
411677ae
AL
148 if (dir == -1) {
149 /* Direction is bogus, don't use it */
ed775ee7 150 return;
411677ae 151 }
ed775ee7 152 ND_TCHECK_SIZE(ip);
c8cf0f94 153 hlen = IP_HL(ip);
ed775ee7 154 ND_TCHECK_SIZE((const struct tcphdr *)&((const int *)ip)[hlen]);
411677ae 155 hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
c8cf0f94 156 lastlen[dir][lastconn] = length - (hlen << 2);
c8cf0f94
PA
157 break;
158
159 default:
411677ae
AL
160 if (dir == -1) {
161 /* Direction is bogus, don't use it */
ed775ee7 162 return;
411677ae 163 }
ed775ee7
AHJ
164 if (GET_U_1(p + SLX_CHDR) & TYPE_COMPRESSED_TCP) {
165 compressed_sl_print(ndo, p + SLX_CHDR, ip, length, dir);
166 ND_PRINT(": ");
c8cf0f94 167 } else
ed775ee7 168 ND_PRINT("slip-%u!: ", GET_U_1(p + SLX_CHDR));
c8cf0f94
PA
169 }
170}
171
172static const u_char *
411677ae 173print_sl_change(netdissect_options *ndo,
ed775ee7 174 const char *str, const u_char *cp)
c8cf0f94 175{
ed775ee7 176 u_int i;
c8cf0f94 177
ed775ee7
AHJ
178 if ((i = GET_U_1(cp)) == 0) {
179 cp++;
180 i = GET_BE_U_2(cp);
c8cf0f94
PA
181 cp += 2;
182 }
ed775ee7 183 ND_PRINT(" %s%u", str, i);
c8cf0f94
PA
184 return (cp);
185}
186
187static const u_char *
411677ae 188print_sl_winchange(netdissect_options *ndo,
ed775ee7 189 const u_char *cp)
c8cf0f94 190{
ed775ee7 191 int16_t i;
c8cf0f94 192
ed775ee7
AHJ
193 if ((i = GET_U_1(cp)) == 0) {
194 cp++;
195 i = GET_BE_S_2(cp);
c8cf0f94
PA
196 cp += 2;
197 }
198 if (i >= 0)
ed775ee7 199 ND_PRINT(" W+%d", i);
c8cf0f94 200 else
ed775ee7 201 ND_PRINT(" W%d", i);
c8cf0f94
PA
202 return (cp);
203}
204
ed775ee7 205static void
411677ae
AL
206compressed_sl_print(netdissect_options *ndo,
207 const u_char *chdr, const struct ip *ip,
208 u_int length, int dir)
c8cf0f94 209{
ed775ee7
AHJ
210 const u_char *cp = chdr;
211 u_int flags, hlen;
c8cf0f94 212
ed775ee7
AHJ
213 flags = GET_U_1(cp);
214 cp++;
c8cf0f94 215 if (flags & NEW_C) {
ed775ee7
AHJ
216 lastconn = GET_U_1(cp);
217 cp++;
218 ND_PRINT("ctcp %u", lastconn);
c8cf0f94 219 } else
ed775ee7 220 ND_PRINT("ctcp *");
c8cf0f94
PA
221
222 /* skip tcp checksum */
223 cp += 2;
224
225 switch (flags & SPECIALS_MASK) {
226 case SPECIAL_I:
ed775ee7 227 ND_PRINT(" *SA+%u", lastlen[dir][lastconn]);
c8cf0f94
PA
228 break;
229
230 case SPECIAL_D:
ed775ee7 231 ND_PRINT(" *S+%u", lastlen[dir][lastconn]);
c8cf0f94
PA
232 break;
233
234 default:
235 if (flags & NEW_U)
411677ae 236 cp = print_sl_change(ndo, "U=", cp);
c8cf0f94 237 if (flags & NEW_W)
411677ae 238 cp = print_sl_winchange(ndo, cp);
c8cf0f94 239 if (flags & NEW_A)
411677ae 240 cp = print_sl_change(ndo, "A+", cp);
c8cf0f94 241 if (flags & NEW_S)
411677ae 242 cp = print_sl_change(ndo, "S+", cp);
c8cf0f94
PA
243 break;
244 }
245 if (flags & NEW_I)
411677ae 246 cp = print_sl_change(ndo, "I+", cp);
c8cf0f94
PA
247
248 /*
249 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
250 * 'cp - chdr' is the length of the compressed header.
251 * 'length - hlen' is the amount of data in the packet.
252 */
ed775ee7 253 ND_TCHECK_SIZE(ip);
c8cf0f94 254 hlen = IP_HL(ip);
ed775ee7 255 ND_TCHECK_SIZE((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
411677ae 256 hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
c8cf0f94 257 lastlen[dir][lastconn] = length - (hlen << 2);
ed775ee7 258 ND_PRINT(" %u (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
c8cf0f94 259}