1 /* $OpenBSD: print-sl.c,v 1.22 2021/12/01 18:28:46 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #include <sys/time.h>
25 #include <sys/file.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28
29 #include <net/if.h>
30
31 #include <netinet/in.h>
32 #include <netinet/ip.h>
33 #include <netinet/if_ether.h>
34 #include <netinet/udp.h>
35 #include <netinet/tcp.h>
36
37 #include <net/slcompress.h>
38
39 #include <ctype.h>
40 #include <netdb.h>
41 #include <pcap.h>
42 #include <stdio.h>
43 #include <limits.h>
44
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "extract.h" /* must come after interface.h */
48
49 static u_int lastlen[2][256];
50 static u_int lastconn = 255;
51
52 static void sliplink_print(const u_char *, const struct ip *, u_int);
53 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int);
54
55 /*
56 * Definitions of the pseudo-link-level header attached to slip
57 * packets grabbed by the packet filter (bpf) traffic monitor.
58 */
59 #define SLIP_HDRLEN 16 /* BPF SLIP header length */
60
61 /* Offsets into BPF SLIP header. */
62 #define SLX_DIR 0 /* direction; see below */
63 #define SLX_CHDR 1 /* compressed header data */
64 #define CHDR_LEN 15 /* length of compressed header data */
65
66 #define SLIPDIR_IN 0 /* incoming */
67 #define SLIPDIR_OUT 1 /* outgoing */
68
69 /* XXX needs more hacking to work right */
70
71 void
sl_if_print(u_char * user,const struct pcap_pkthdr * h,const u_char * p)72 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
73 {
74 u_int caplen = h->caplen;
75 u_int length = h->len;
76 const struct ip *ip;
77
78 ts_print(&h->ts);
79
80 if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) {
81 printf("[|slip]");
82 goto out;
83 }
84 /*
85 * Some printers want to get back at the link level addresses,
86 * and/or check that they're not walking off the end of the packet.
87 * Rather than pass them all the way down, we set these globals.
88 */
89 packetp = p;
90 snapend = p + caplen;
91
92 length -= SLIP_HDRLEN;
93
94 ip = (struct ip *)(p + SLIP_HDRLEN);
95
96 if (eflag)
97 sliplink_print(p, ip, length);
98
99 switch (ip->ip_v) {
100 case 4:
101 ip_print((u_char *)ip, length);
102 break;
103 case 6:
104 ip6_print((u_char *)ip, length);
105 break;
106 default:
107 printf ("ip v%d", ip->ip_v);
108 }
109
110 if (xflag)
111 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
112 out:
113 putchar('\n');
114 }
115
116
117 void
sl_bsdos_if_print(u_char * user,const struct pcap_pkthdr * h,const u_char * p)118 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
119 {
120 u_int caplen = h->caplen;
121 u_int length = h->len;
122 const struct ip *ip;
123
124 ts_print(&h->ts);
125
126 if (caplen < SLIP_HDRLEN) {
127 printf("[|slip]");
128 goto out;
129 }
130 /*
131 * Some printers want to get back at the link level addresses,
132 * and/or check that they're not walking off the end of the packet.
133 * Rather than pass them all the way down, we set these globals.
134 */
135 packetp = p;
136 snapend = p + caplen;
137
138 length -= SLIP_HDRLEN;
139
140 ip = (struct ip *)(p + SLIP_HDRLEN);
141
142 #ifdef notdef
143 if (eflag)
144 sliplink_print(p, ip, length);
145 #endif
146
147 ip_print((u_char *)ip, length);
148
149 if (xflag)
150 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
151 out:
152 putchar('\n');
153 }
154
155 static void
sliplink_print(const u_char * p,const struct ip * ip,u_int length)156 sliplink_print(const u_char *p, const struct ip *ip, u_int length)
157 {
158 int dir;
159 u_int hlen;
160
161 dir = p[SLX_DIR];
162 putchar(dir == SLIPDIR_IN ? 'I' : 'O');
163 putchar(' ');
164
165 if (nflag) {
166 /* XXX just dump the header */
167 int i;
168
169 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
170 printf("%02x.", p[i]);
171 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
172 return;
173 }
174 switch (p[SLX_CHDR] & 0xf0) {
175
176 case TYPE_IP:
177 printf("ip %d: ", length + SLIP_HDRLEN);
178 break;
179
180 case TYPE_UNCOMPRESSED_TCP:
181 /*
182 * The connection id is stored in the IP protocol field.
183 * Get it from the link layer since sl_uncompress_tcp()
184 * has restored the IP header copy to IPPROTO_TCP.
185 */
186 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
187 hlen = ip->ip_hl;
188 hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
189 lastlen[dir][lastconn] = length - (hlen << 2);
190 printf("utcp %d: ", lastconn);
191 break;
192
193 default:
194 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
195 compressed_sl_print(&p[SLX_CHDR], ip,
196 length, dir);
197 printf(": ");
198 } else
199 printf("slip-%d!: ", p[SLX_CHDR]);
200 }
201 }
202
203 static const u_char *
print_sl_change(const char * str,const u_char * cp)204 print_sl_change(const char *str, const u_char *cp)
205 {
206 u_int i;
207
208 if ((i = *cp++) == 0) {
209 i = EXTRACT_16BITS(cp);
210 cp += 2;
211 }
212 printf(" %s%d", str, i);
213 return (cp);
214 }
215
216 static const u_char *
print_sl_winchange(const u_char * cp)217 print_sl_winchange(const u_char *cp)
218 {
219 short i;
220
221 if ((i = *cp++) == 0) {
222 i = EXTRACT_16BITS(cp);
223 cp += 2;
224 }
225 if (i >= 0)
226 printf(" W+%d", i);
227 else
228 printf(" W%d", i);
229 return (cp);
230 }
231
232 static void
compressed_sl_print(const u_char * chdr,const struct ip * ip,u_int length,int dir)233 compressed_sl_print(const u_char *chdr, const struct ip *ip,
234 u_int length, int dir)
235 {
236 const u_char *cp = chdr;
237 u_int flags, hlen;
238
239 flags = *cp++;
240 if (flags & NEW_C) {
241 lastconn = *cp++;
242 printf("ctcp %d", lastconn);
243 } else
244 printf("ctcp *");
245
246 /* skip tcp checksum */
247 cp += 2;
248
249 switch (flags & SPECIALS_MASK) {
250 case SPECIAL_I:
251 printf(" *SA+%d", lastlen[dir][lastconn]);
252 break;
253
254 case SPECIAL_D:
255 printf(" *S+%d", lastlen[dir][lastconn]);
256 break;
257
258 default:
259 if (flags & NEW_U)
260 cp = print_sl_change("U=", cp);
261 if (flags & NEW_W)
262 cp = print_sl_winchange(cp);
263 if (flags & NEW_A)
264 cp = print_sl_change("A+", cp);
265 if (flags & NEW_S)
266 cp = print_sl_change("S+", cp);
267 break;
268 }
269 if (flags & NEW_I)
270 cp = print_sl_change("I+", cp);
271
272 /*
273 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
274 * 'cp - chdr' is the length of the compressed header.
275 * 'length - hlen' is the amount of data in the packet.
276 */
277 hlen = ip->ip_hl;
278 hlen += ((struct tcphdr *)&((int32_t *)ip)[hlen])->th_off;
279 lastlen[dir][lastconn] = length - (hlen << 2);
280 printf(" %d (%d)", lastlen[dir][lastconn], (int)(cp - chdr));
281 }
282