xref: /netbsd-src/external/bsd/tcpdump/dist/print-sl.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  */
210f74e101Schristos 
2211b3aaa1Schristos #include <sys/cdefs.h>
230f74e101Schristos #ifndef lint
24*26ba0b50Schristos __RCSID("$NetBSD: print-sl.c,v 1.11 2024/09/02 16:15:33 christos Exp $");
250f74e101Schristos #endif
260f74e101Schristos 
27dc860a36Sspz /* \summary: Compressed Serial Line Internet Protocol printer */
28dc860a36Sspz 
29c74ad251Schristos #include <config.h>
300f74e101Schristos 
31c74ad251Schristos #include "netdissect-stdinc.h"
320f74e101Schristos 
33c74ad251Schristos #define ND_LONGJMP_FROM_TCHECK
34fdccd7e4Schristos #include "netdissect.h"
35fdccd7e4Schristos #include "extract.h"
360f74e101Schristos 
370f74e101Schristos #include "ip.h"
380f74e101Schristos #include "tcp.h"
390f74e101Schristos #include "slcompress.h"
400f74e101Schristos 
41b3a00663Schristos /*
42b3a00663Schristos  * definitions of the pseudo- link-level header attached to slip
43b3a00663Schristos  * packets grabbed by the packet filter (bpf) traffic monitor.
44b3a00663Schristos  */
45b3a00663Schristos #define SLIP_HDRLEN 16
46b3a00663Schristos 
47b3a00663Schristos #define SLX_DIR 0
48b3a00663Schristos #define SLX_CHDR 1
49b3a00663Schristos 
50b3a00663Schristos #define SLIPDIR_IN 0
51b3a00663Schristos #define SLIPDIR_OUT 1
52b3a00663Schristos 
53b3a00663Schristos 
540f74e101Schristos static u_int lastlen[2][256];
550f74e101Schristos static u_int lastconn = 255;
560f74e101Schristos 
57c74ad251Schristos static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
58c74ad251Schristos static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
590f74e101Schristos 
60c74ad251Schristos void
61b3a00663Schristos sl_if_print(netdissect_options *ndo,
62b3a00663Schristos             const struct pcap_pkthdr *h, const u_char *p)
630f74e101Schristos {
64c74ad251Schristos 	u_int length = h->len;
65c74ad251Schristos 	const struct ip *ip;
660f74e101Schristos 
67c74ad251Schristos 	ndo->ndo_protocol = "slip";
68c74ad251Schristos 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
69c74ad251Schristos 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
700f74e101Schristos 
710f74e101Schristos 	length -= SLIP_HDRLEN;
720f74e101Schristos 
73fdccd7e4Schristos 	ip = (const struct ip *)(p + SLIP_HDRLEN);
740f74e101Schristos 
75b3a00663Schristos 	if (ndo->ndo_eflag)
76c74ad251Schristos 		sliplink_print(ndo, p, ip, length);
77dc860a36Sspz 
780f74e101Schristos 	switch (IP_V(ip)) {
790f74e101Schristos 	case 4:
80fdccd7e4Schristos 	        ip_print(ndo, (const u_char *)ip, length);
810f74e101Schristos 		break;
820f74e101Schristos 	case 6:
83fdccd7e4Schristos 		ip6_print(ndo, (const u_char *)ip, length);
840f74e101Schristos 		break;
850f74e101Schristos 	default:
86c74ad251Schristos 		ND_PRINT("ip v%u", IP_V(ip));
87c74ad251Schristos 	}
880f74e101Schristos }
890f74e101Schristos 
90c74ad251Schristos void
91b3a00663Schristos sl_bsdos_if_print(netdissect_options *ndo,
92b3a00663Schristos                   const struct pcap_pkthdr *h, const u_char *p)
930f74e101Schristos {
94c74ad251Schristos 	u_int length = h->len;
95c74ad251Schristos 	const struct ip *ip;
960f74e101Schristos 
97c74ad251Schristos 	ndo->ndo_protocol = "slip_bsdos";
98c74ad251Schristos 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
99c74ad251Schristos 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
1000f74e101Schristos 
1010f74e101Schristos 	length -= SLIP_HDRLEN;
1020f74e101Schristos 
103fdccd7e4Schristos 	ip = (const struct ip *)(p + SLIP_HDRLEN);
1040f74e101Schristos 
1050f74e101Schristos #ifdef notdef
106b3a00663Schristos 	if (ndo->ndo_eflag)
107b3a00663Schristos 		sliplink_print(ndo, p, ip, length);
1080f74e101Schristos #endif
1090f74e101Schristos 
110fdccd7e4Schristos 	ip_print(ndo, (const u_char *)ip, length);
1110f74e101Schristos }
1120f74e101Schristos 
113c74ad251Schristos static void
114b3a00663Schristos sliplink_print(netdissect_options *ndo,
115c74ad251Schristos                const u_char *p, const struct ip *ip,
116c74ad251Schristos                u_int length)
1170f74e101Schristos {
1180f74e101Schristos 	int dir;
1190f74e101Schristos 	u_int hlen;
1200f74e101Schristos 
121c74ad251Schristos 	dir = GET_U_1(p + SLX_DIR);
12272c96ff3Schristos 	switch (dir) {
1230f74e101Schristos 
12472c96ff3Schristos 	case SLIPDIR_IN:
125c74ad251Schristos 		ND_PRINT("I ");
12672c96ff3Schristos 		break;
12772c96ff3Schristos 
12872c96ff3Schristos 	case SLIPDIR_OUT:
129c74ad251Schristos 		ND_PRINT("O ");
13072c96ff3Schristos 		break;
13172c96ff3Schristos 
13272c96ff3Schristos 	default:
133c74ad251Schristos 		ND_PRINT("Invalid direction %d ", dir);
13472c96ff3Schristos 		dir = -1;
13572c96ff3Schristos 		break;
13672c96ff3Schristos 	}
137c74ad251Schristos 	switch (GET_U_1(p + SLX_CHDR) & 0xf0) {
1380f74e101Schristos 
1390f74e101Schristos 	case TYPE_IP:
140c74ad251Schristos 		ND_PRINT("ip %u: ", length + SLIP_HDRLEN);
1410f74e101Schristos 		break;
1420f74e101Schristos 
1430f74e101Schristos 	case TYPE_UNCOMPRESSED_TCP:
1440f74e101Schristos 		/*
1450f74e101Schristos 		 * The connection id is stored in the IP protocol field.
1460f74e101Schristos 		 * Get it from the link layer since sl_uncompress_tcp()
1470f74e101Schristos 		 * has restored the IP header copy to IPPROTO_TCP.
1480f74e101Schristos 		 */
149c74ad251Schristos 		lastconn = GET_U_1(((const struct ip *)(p + SLX_CHDR))->ip_p);
150c74ad251Schristos 		ND_PRINT("utcp %u: ", lastconn);
15172c96ff3Schristos 		if (dir == -1) {
15272c96ff3Schristos 			/* Direction is bogus, don't use it */
153c74ad251Schristos 			return;
15472c96ff3Schristos 		}
155c74ad251Schristos 		ND_TCHECK_SIZE(ip);
1560f74e101Schristos 		hlen = IP_HL(ip);
157c74ad251Schristos 		ND_TCHECK_SIZE((const struct tcphdr *)&((const int *)ip)[hlen]);
158fdccd7e4Schristos 		hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
1590f74e101Schristos 		lastlen[dir][lastconn] = length - (hlen << 2);
1600f74e101Schristos 		break;
1610f74e101Schristos 
1620f74e101Schristos 	default:
16372c96ff3Schristos 		if (dir == -1) {
16472c96ff3Schristos 			/* Direction is bogus, don't use it */
165c74ad251Schristos 			return;
16672c96ff3Schristos 		}
167c74ad251Schristos 		if (GET_U_1(p + SLX_CHDR) & TYPE_COMPRESSED_TCP) {
168c74ad251Schristos 			compressed_sl_print(ndo, p + SLX_CHDR, ip, length, dir);
169c74ad251Schristos 			ND_PRINT(": ");
1700f74e101Schristos 		} else
171c74ad251Schristos 			ND_PRINT("slip-%u!: ", GET_U_1(p + SLX_CHDR));
1720f74e101Schristos 	}
1730f74e101Schristos }
1740f74e101Schristos 
1750f74e101Schristos static const u_char *
176b3a00663Schristos print_sl_change(netdissect_options *ndo,
177c74ad251Schristos                 const char *str, const u_char *cp)
1780f74e101Schristos {
179c74ad251Schristos 	u_int i;
1800f74e101Schristos 
181c74ad251Schristos 	if ((i = GET_U_1(cp)) == 0) {
182c74ad251Schristos 		cp++;
183c74ad251Schristos 		i = GET_BE_U_2(cp);
1840f74e101Schristos 		cp += 2;
1850f74e101Schristos 	}
186c74ad251Schristos 	ND_PRINT(" %s%u", str, i);
1870f74e101Schristos 	return (cp);
1880f74e101Schristos }
1890f74e101Schristos 
1900f74e101Schristos static const u_char *
191b3a00663Schristos print_sl_winchange(netdissect_options *ndo,
192c74ad251Schristos                    const u_char *cp)
1930f74e101Schristos {
194c74ad251Schristos 	int16_t i;
1950f74e101Schristos 
196c74ad251Schristos 	if ((i = GET_U_1(cp)) == 0) {
197c74ad251Schristos 		cp++;
198c74ad251Schristos 		i = GET_BE_S_2(cp);
1990f74e101Schristos 		cp += 2;
2000f74e101Schristos 	}
2010f74e101Schristos 	if (i >= 0)
202c74ad251Schristos 		ND_PRINT(" W+%d", i);
2030f74e101Schristos 	else
204c74ad251Schristos 		ND_PRINT(" W%d", i);
2050f74e101Schristos 	return (cp);
2060f74e101Schristos }
2070f74e101Schristos 
208c74ad251Schristos static void
209b3a00663Schristos compressed_sl_print(netdissect_options *ndo,
210b3a00663Schristos                     const u_char *chdr, const struct ip *ip,
2110f74e101Schristos                     u_int length, int dir)
2120f74e101Schristos {
213c74ad251Schristos 	const u_char *cp = chdr;
214c74ad251Schristos 	u_int flags, hlen;
2150f74e101Schristos 
216c74ad251Schristos 	flags = GET_U_1(cp);
217c74ad251Schristos 	cp++;
2180f74e101Schristos 	if (flags & NEW_C) {
219c74ad251Schristos 		lastconn = GET_U_1(cp);
220c74ad251Schristos 		cp++;
221c74ad251Schristos 		ND_PRINT("ctcp %u", lastconn);
2220f74e101Schristos 	} else
223c74ad251Schristos 		ND_PRINT("ctcp *");
2240f74e101Schristos 
2250f74e101Schristos 	/* skip tcp checksum */
2260f74e101Schristos 	cp += 2;
2270f74e101Schristos 
2280f74e101Schristos 	switch (flags & SPECIALS_MASK) {
2290f74e101Schristos 	case SPECIAL_I:
230c74ad251Schristos 		ND_PRINT(" *SA+%u", lastlen[dir][lastconn]);
2310f74e101Schristos 		break;
2320f74e101Schristos 
2330f74e101Schristos 	case SPECIAL_D:
234c74ad251Schristos 		ND_PRINT(" *S+%u", lastlen[dir][lastconn]);
2350f74e101Schristos 		break;
2360f74e101Schristos 
2370f74e101Schristos 	default:
2380f74e101Schristos 		if (flags & NEW_U)
239b3a00663Schristos 			cp = print_sl_change(ndo, "U=", cp);
2400f74e101Schristos 		if (flags & NEW_W)
241b3a00663Schristos 			cp = print_sl_winchange(ndo, cp);
2420f74e101Schristos 		if (flags & NEW_A)
243b3a00663Schristos 			cp = print_sl_change(ndo, "A+", cp);
2440f74e101Schristos 		if (flags & NEW_S)
245b3a00663Schristos 			cp = print_sl_change(ndo, "S+", cp);
2460f74e101Schristos 		break;
2470f74e101Schristos 	}
2480f74e101Schristos 	if (flags & NEW_I)
249b3a00663Schristos 		cp = print_sl_change(ndo, "I+", cp);
2500f74e101Schristos 
2510f74e101Schristos 	/*
2520f74e101Schristos 	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
2530f74e101Schristos 	 * 'cp - chdr' is the length of the compressed header.
2540f74e101Schristos 	 * 'length - hlen' is the amount of data in the packet.
2550f74e101Schristos 	 */
256c74ad251Schristos 	ND_TCHECK_SIZE(ip);
2570f74e101Schristos 	hlen = IP_HL(ip);
258c74ad251Schristos 	ND_TCHECK_SIZE((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
259fdccd7e4Schristos 	hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
2600f74e101Schristos 	lastlen[dir][lastconn] = length - (hlen << 2);
261c74ad251Schristos 	ND_PRINT(" %u (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
2620f74e101Schristos }
263