xref: /dflybsd-src/contrib/tcpdump/print-sl.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  */
2141c99275SPeter Avalos 
22411677aeSAaron LI /* \summary: Compressed Serial Line Internet Protocol printer */
2341c99275SPeter Avalos 
2441c99275SPeter Avalos #ifdef HAVE_CONFIG_H
25*ed775ee7SAntonio Huete Jimenez #include <config.h>
2641c99275SPeter Avalos #endif
2741c99275SPeter Avalos 
28*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
2941c99275SPeter Avalos 
30*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
31411677aeSAaron LI #include "netdissect.h"
32411677aeSAaron LI #include "extract.h"
3341c99275SPeter Avalos 
3441c99275SPeter Avalos #include "ip.h"
3541c99275SPeter Avalos #include "tcp.h"
3641c99275SPeter Avalos #include "slcompress.h"
3741c99275SPeter Avalos 
38411677aeSAaron LI /*
39411677aeSAaron LI  * definitions of the pseudo- link-level header attached to slip
40411677aeSAaron LI  * packets grabbed by the packet filter (bpf) traffic monitor.
41411677aeSAaron LI  */
42411677aeSAaron LI #define SLIP_HDRLEN 16
43411677aeSAaron LI 
44411677aeSAaron LI #define SLX_DIR 0
45411677aeSAaron LI #define SLX_CHDR 1
46411677aeSAaron LI 
47411677aeSAaron LI #define SLIPDIR_IN 0
48411677aeSAaron LI #define SLIPDIR_OUT 1
49411677aeSAaron LI 
50411677aeSAaron LI 
5141c99275SPeter Avalos static u_int lastlen[2][256];
5241c99275SPeter Avalos static u_int lastconn = 255;
5341c99275SPeter Avalos 
54*ed775ee7SAntonio Huete Jimenez static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
55*ed775ee7SAntonio Huete Jimenez static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
5641c99275SPeter Avalos 
57*ed775ee7SAntonio Huete Jimenez void
sl_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)58411677aeSAaron LI sl_if_print(netdissect_options *ndo,
59411677aeSAaron LI             const struct pcap_pkthdr *h, const u_char *p)
6041c99275SPeter Avalos {
61*ed775ee7SAntonio Huete Jimenez 	u_int length = h->len;
62*ed775ee7SAntonio Huete Jimenez 	const struct ip *ip;
6341c99275SPeter Avalos 
64*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "slip";
65*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
66*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
6741c99275SPeter Avalos 
6841c99275SPeter Avalos 	length -= SLIP_HDRLEN;
6941c99275SPeter Avalos 
70411677aeSAaron LI 	ip = (const struct ip *)(p + SLIP_HDRLEN);
7141c99275SPeter Avalos 
72411677aeSAaron LI 	if (ndo->ndo_eflag)
73*ed775ee7SAntonio Huete Jimenez 		sliplink_print(ndo, p, ip, length);
7441c99275SPeter Avalos 
7541c99275SPeter Avalos 	switch (IP_V(ip)) {
7641c99275SPeter Avalos 	case 4:
77411677aeSAaron LI 	        ip_print(ndo, (const u_char *)ip, length);
7841c99275SPeter Avalos 		break;
7941c99275SPeter Avalos 	case 6:
80411677aeSAaron LI 		ip6_print(ndo, (const u_char *)ip, length);
8141c99275SPeter Avalos 		break;
8241c99275SPeter Avalos 	default:
83*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("ip v%u", IP_V(ip));
84*ed775ee7SAntonio Huete Jimenez 	}
8541c99275SPeter Avalos }
8641c99275SPeter Avalos 
87*ed775ee7SAntonio Huete Jimenez void
sl_bsdos_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)88411677aeSAaron LI sl_bsdos_if_print(netdissect_options *ndo,
89411677aeSAaron LI                   const struct pcap_pkthdr *h, const u_char *p)
9041c99275SPeter Avalos {
91*ed775ee7SAntonio Huete Jimenez 	u_int length = h->len;
92*ed775ee7SAntonio Huete Jimenez 	const struct ip *ip;
9341c99275SPeter Avalos 
94*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "slip_bsdos";
95*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
96*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
9741c99275SPeter Avalos 
9841c99275SPeter Avalos 	length -= SLIP_HDRLEN;
9941c99275SPeter Avalos 
100411677aeSAaron LI 	ip = (const struct ip *)(p + SLIP_HDRLEN);
10141c99275SPeter Avalos 
10241c99275SPeter Avalos #ifdef notdef
103411677aeSAaron LI 	if (ndo->ndo_eflag)
104411677aeSAaron LI 		sliplink_print(ndo, p, ip, length);
10541c99275SPeter Avalos #endif
10641c99275SPeter Avalos 
107411677aeSAaron LI 	ip_print(ndo, (const u_char *)ip, length);
10841c99275SPeter Avalos }
10941c99275SPeter Avalos 
110*ed775ee7SAntonio Huete Jimenez static void
sliplink_print(netdissect_options * ndo,const u_char * p,const struct ip * ip,u_int length)111411677aeSAaron LI sliplink_print(netdissect_options *ndo,
112*ed775ee7SAntonio Huete Jimenez                const u_char *p, const struct ip *ip,
113*ed775ee7SAntonio Huete Jimenez                u_int length)
11441c99275SPeter Avalos {
11541c99275SPeter Avalos 	int dir;
11641c99275SPeter Avalos 	u_int hlen;
11741c99275SPeter Avalos 
118*ed775ee7SAntonio Huete Jimenez 	dir = GET_U_1(p + SLX_DIR);
119411677aeSAaron LI 	switch (dir) {
12041c99275SPeter Avalos 
121411677aeSAaron LI 	case SLIPDIR_IN:
122*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("I ");
123411677aeSAaron LI 		break;
124411677aeSAaron LI 
125411677aeSAaron LI 	case SLIPDIR_OUT:
126*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("O ");
127411677aeSAaron LI 		break;
128411677aeSAaron LI 
129411677aeSAaron LI 	default:
130*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("Invalid direction %d ", dir);
131411677aeSAaron LI 		dir = -1;
132411677aeSAaron LI 		break;
133411677aeSAaron LI 	}
134*ed775ee7SAntonio Huete Jimenez 	switch (GET_U_1(p + SLX_CHDR) & 0xf0) {
13541c99275SPeter Avalos 
13641c99275SPeter Avalos 	case TYPE_IP:
137*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("ip %u: ", length + SLIP_HDRLEN);
13841c99275SPeter Avalos 		break;
13941c99275SPeter Avalos 
14041c99275SPeter Avalos 	case TYPE_UNCOMPRESSED_TCP:
14141c99275SPeter Avalos 		/*
14241c99275SPeter Avalos 		 * The connection id is stored in the IP protocol field.
14341c99275SPeter Avalos 		 * Get it from the link layer since sl_uncompress_tcp()
14441c99275SPeter Avalos 		 * has restored the IP header copy to IPPROTO_TCP.
14541c99275SPeter Avalos 		 */
146*ed775ee7SAntonio Huete Jimenez 		lastconn = GET_U_1(((const struct ip *)(p + SLX_CHDR))->ip_p);
147*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("utcp %u: ", lastconn);
148411677aeSAaron LI 		if (dir == -1) {
149411677aeSAaron LI 			/* Direction is bogus, don't use it */
150*ed775ee7SAntonio Huete Jimenez 			return;
151411677aeSAaron LI 		}
152*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_SIZE(ip);
15341c99275SPeter Avalos 		hlen = IP_HL(ip);
154*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_SIZE((const struct tcphdr *)&((const int *)ip)[hlen]);
155411677aeSAaron LI 		hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
15641c99275SPeter Avalos 		lastlen[dir][lastconn] = length - (hlen << 2);
15741c99275SPeter Avalos 		break;
15841c99275SPeter Avalos 
15941c99275SPeter Avalos 	default:
160411677aeSAaron LI 		if (dir == -1) {
161411677aeSAaron LI 			/* Direction is bogus, don't use it */
162*ed775ee7SAntonio Huete Jimenez 			return;
16341c99275SPeter Avalos 		}
164*ed775ee7SAntonio Huete Jimenez 		if (GET_U_1(p + SLX_CHDR) & TYPE_COMPRESSED_TCP) {
165*ed775ee7SAntonio Huete Jimenez 			compressed_sl_print(ndo, p + SLX_CHDR, ip, length, dir);
166*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(": ");
167411677aeSAaron LI 		} else
168*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("slip-%u!: ", GET_U_1(p + SLX_CHDR));
169411677aeSAaron LI 	}
17041c99275SPeter Avalos }
17141c99275SPeter Avalos 
17241c99275SPeter Avalos static const u_char *
print_sl_change(netdissect_options * ndo,const char * str,const u_char * cp)173411677aeSAaron LI print_sl_change(netdissect_options *ndo,
174*ed775ee7SAntonio Huete Jimenez                 const char *str, const u_char *cp)
17541c99275SPeter Avalos {
176*ed775ee7SAntonio Huete Jimenez 	u_int i;
17741c99275SPeter Avalos 
178*ed775ee7SAntonio Huete Jimenez 	if ((i = GET_U_1(cp)) == 0) {
179*ed775ee7SAntonio Huete Jimenez 		cp++;
180*ed775ee7SAntonio Huete Jimenez 		i = GET_BE_U_2(cp);
18141c99275SPeter Avalos 		cp += 2;
18241c99275SPeter Avalos 	}
183*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(" %s%u", str, i);
18441c99275SPeter Avalos 	return (cp);
18541c99275SPeter Avalos }
18641c99275SPeter Avalos 
18741c99275SPeter Avalos static const u_char *
print_sl_winchange(netdissect_options * ndo,const u_char * cp)188411677aeSAaron LI print_sl_winchange(netdissect_options *ndo,
189*ed775ee7SAntonio Huete Jimenez                    const u_char *cp)
19041c99275SPeter Avalos {
191*ed775ee7SAntonio Huete Jimenez 	int16_t i;
19241c99275SPeter Avalos 
193*ed775ee7SAntonio Huete Jimenez 	if ((i = GET_U_1(cp)) == 0) {
194*ed775ee7SAntonio Huete Jimenez 		cp++;
195*ed775ee7SAntonio Huete Jimenez 		i = GET_BE_S_2(cp);
19641c99275SPeter Avalos 		cp += 2;
19741c99275SPeter Avalos 	}
19841c99275SPeter Avalos 	if (i >= 0)
199*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" W+%d", i);
20041c99275SPeter Avalos 	else
201*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" W%d", i);
20241c99275SPeter Avalos 	return (cp);
20341c99275SPeter Avalos }
20441c99275SPeter Avalos 
205*ed775ee7SAntonio Huete Jimenez static void
compressed_sl_print(netdissect_options * ndo,const u_char * chdr,const struct ip * ip,u_int length,int dir)206411677aeSAaron LI compressed_sl_print(netdissect_options *ndo,
207411677aeSAaron LI                     const u_char *chdr, const struct ip *ip,
20841c99275SPeter Avalos                     u_int length, int dir)
20941c99275SPeter Avalos {
210*ed775ee7SAntonio Huete Jimenez 	const u_char *cp = chdr;
211*ed775ee7SAntonio Huete Jimenez 	u_int flags, hlen;
21241c99275SPeter Avalos 
213*ed775ee7SAntonio Huete Jimenez 	flags = GET_U_1(cp);
214*ed775ee7SAntonio Huete Jimenez 	cp++;
21541c99275SPeter Avalos 	if (flags & NEW_C) {
216*ed775ee7SAntonio Huete Jimenez 		lastconn = GET_U_1(cp);
217*ed775ee7SAntonio Huete Jimenez 		cp++;
218*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("ctcp %u", lastconn);
21941c99275SPeter Avalos 	} else
220*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("ctcp *");
22141c99275SPeter Avalos 
22241c99275SPeter Avalos 	/* skip tcp checksum */
22341c99275SPeter Avalos 	cp += 2;
22441c99275SPeter Avalos 
22541c99275SPeter Avalos 	switch (flags & SPECIALS_MASK) {
22641c99275SPeter Avalos 	case SPECIAL_I:
227*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" *SA+%u", lastlen[dir][lastconn]);
22841c99275SPeter Avalos 		break;
22941c99275SPeter Avalos 
23041c99275SPeter Avalos 	case SPECIAL_D:
231*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" *S+%u", lastlen[dir][lastconn]);
23241c99275SPeter Avalos 		break;
23341c99275SPeter Avalos 
23441c99275SPeter Avalos 	default:
23541c99275SPeter Avalos 		if (flags & NEW_U)
236411677aeSAaron LI 			cp = print_sl_change(ndo, "U=", cp);
23741c99275SPeter Avalos 		if (flags & NEW_W)
238411677aeSAaron LI 			cp = print_sl_winchange(ndo, cp);
23941c99275SPeter Avalos 		if (flags & NEW_A)
240411677aeSAaron LI 			cp = print_sl_change(ndo, "A+", cp);
24141c99275SPeter Avalos 		if (flags & NEW_S)
242411677aeSAaron LI 			cp = print_sl_change(ndo, "S+", cp);
24341c99275SPeter Avalos 		break;
24441c99275SPeter Avalos 	}
24541c99275SPeter Avalos 	if (flags & NEW_I)
246411677aeSAaron LI 		cp = print_sl_change(ndo, "I+", cp);
24741c99275SPeter Avalos 
24841c99275SPeter Avalos 	/*
24941c99275SPeter Avalos 	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
25041c99275SPeter Avalos 	 * 'cp - chdr' is the length of the compressed header.
25141c99275SPeter Avalos 	 * 'length - hlen' is the amount of data in the packet.
25241c99275SPeter Avalos 	 */
253*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(ip);
25441c99275SPeter Avalos 	hlen = IP_HL(ip);
255*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
256411677aeSAaron LI 	hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
25741c99275SPeter Avalos 	lastlen[dir][lastconn] = length - (hlen << 2);
258*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(" %u (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
25941c99275SPeter Avalos }
260