xref: /freebsd/contrib/tcpdump/print-sl.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
214edb46e9SPaul Traina 
223340d773SGleb Smirnoff /* \summary: Compressed Serial Line Internet Protocol printer */
233340d773SGleb Smirnoff 
24*ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina 
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina 
28*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
303340d773SGleb Smirnoff #include "extract.h"
314edb46e9SPaul Traina 
32943ee2b1SBill Fenner #include "ip.h"
33943ee2b1SBill Fenner #include "tcp.h"
34943ee2b1SBill Fenner #include "slcompress.h"
35943ee2b1SBill Fenner 
363c602fabSXin LI /*
373c602fabSXin LI  * definitions of the pseudo- link-level header attached to slip
383c602fabSXin LI  * packets grabbed by the packet filter (bpf) traffic monitor.
393c602fabSXin LI  */
403c602fabSXin LI #define SLIP_HDRLEN 16
413c602fabSXin LI 
423c602fabSXin LI #define SLX_DIR 0
433c602fabSXin LI #define SLX_CHDR 1
443c602fabSXin LI 
453c602fabSXin LI #define SLIPDIR_IN 0
463c602fabSXin LI #define SLIPDIR_OUT 1
473c602fabSXin LI 
483c602fabSXin LI 
494edb46e9SPaul Traina static u_int lastlen[2][256];
504edb46e9SPaul Traina static u_int lastconn = 255;
514edb46e9SPaul Traina 
52*ee67461eSJoseph Mingrone static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
53*ee67461eSJoseph Mingrone static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
544edb46e9SPaul Traina 
55*ee67461eSJoseph Mingrone void
sl_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)563c602fabSXin LI sl_if_print(netdissect_options *ndo,
573c602fabSXin LI             const struct pcap_pkthdr *h, const u_char *p)
584edb46e9SPaul Traina {
59*ee67461eSJoseph Mingrone 	u_int length = h->len;
60*ee67461eSJoseph Mingrone 	const struct ip *ip;
614edb46e9SPaul Traina 
62*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "slip";
63*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
64*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
654edb46e9SPaul Traina 
664edb46e9SPaul Traina 	length -= SLIP_HDRLEN;
674edb46e9SPaul Traina 
683340d773SGleb Smirnoff 	ip = (const struct ip *)(p + SLIP_HDRLEN);
694edb46e9SPaul Traina 
703c602fabSXin LI 	if (ndo->ndo_eflag)
71*ee67461eSJoseph Mingrone 		sliplink_print(ndo, p, ip, length);
723340d773SGleb Smirnoff 
73943ee2b1SBill Fenner 	switch (IP_V(ip)) {
74a88113a8SBill Fenner 	case 4:
753340d773SGleb Smirnoff 	        ip_print(ndo, (const u_char *)ip, length);
76a88113a8SBill Fenner 		break;
77a88113a8SBill Fenner 	case 6:
783340d773SGleb Smirnoff 		ip6_print(ndo, (const u_char *)ip, length);
79a88113a8SBill Fenner 		break;
80a88113a8SBill Fenner 	default:
81*ee67461eSJoseph Mingrone 		ND_PRINT("ip v%u", IP_V(ip));
82*ee67461eSJoseph Mingrone 	}
83a88113a8SBill Fenner }
844edb46e9SPaul Traina 
85*ee67461eSJoseph Mingrone void
sl_bsdos_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)863c602fabSXin LI sl_bsdos_if_print(netdissect_options *ndo,
873c602fabSXin LI                   const struct pcap_pkthdr *h, const u_char *p)
88699fc314SBill Fenner {
89*ee67461eSJoseph Mingrone 	u_int length = h->len;
90*ee67461eSJoseph Mingrone 	const struct ip *ip;
91699fc314SBill Fenner 
92*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "slip_bsdos";
93*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, SLIP_HDRLEN);
94*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += SLIP_HDRLEN;
95699fc314SBill Fenner 
96699fc314SBill Fenner 	length -= SLIP_HDRLEN;
97699fc314SBill Fenner 
983340d773SGleb Smirnoff 	ip = (const struct ip *)(p + SLIP_HDRLEN);
99699fc314SBill Fenner 
100699fc314SBill Fenner #ifdef notdef
1013c602fabSXin LI 	if (ndo->ndo_eflag)
1023c602fabSXin LI 		sliplink_print(ndo, p, ip, length);
103699fc314SBill Fenner #endif
104699fc314SBill Fenner 
1053340d773SGleb Smirnoff 	ip_print(ndo, (const u_char *)ip, length);
106699fc314SBill Fenner }
107699fc314SBill Fenner 
108*ee67461eSJoseph Mingrone static void
sliplink_print(netdissect_options * ndo,const u_char * p,const struct ip * ip,u_int length)1093c602fabSXin LI sliplink_print(netdissect_options *ndo,
110*ee67461eSJoseph Mingrone                const u_char *p, const struct ip *ip,
111*ee67461eSJoseph Mingrone                u_int length)
1124edb46e9SPaul Traina {
1134edb46e9SPaul Traina 	int dir;
1144edb46e9SPaul Traina 	u_int hlen;
1154edb46e9SPaul Traina 
116*ee67461eSJoseph Mingrone 	dir = GET_U_1(p + SLX_DIR);
1170bff6a5aSEd Maste 	switch (dir) {
1184edb46e9SPaul Traina 
1190bff6a5aSEd Maste 	case SLIPDIR_IN:
120*ee67461eSJoseph Mingrone 		ND_PRINT("I ");
1210bff6a5aSEd Maste 		break;
1220bff6a5aSEd Maste 
1230bff6a5aSEd Maste 	case SLIPDIR_OUT:
124*ee67461eSJoseph Mingrone 		ND_PRINT("O ");
1250bff6a5aSEd Maste 		break;
1260bff6a5aSEd Maste 
1270bff6a5aSEd Maste 	default:
128*ee67461eSJoseph Mingrone 		ND_PRINT("Invalid direction %d ", dir);
1290bff6a5aSEd Maste 		dir = -1;
1300bff6a5aSEd Maste 		break;
1310bff6a5aSEd Maste 	}
132*ee67461eSJoseph Mingrone 	switch (GET_U_1(p + SLX_CHDR) & 0xf0) {
1334edb46e9SPaul Traina 
1344edb46e9SPaul Traina 	case TYPE_IP:
135*ee67461eSJoseph Mingrone 		ND_PRINT("ip %u: ", length + SLIP_HDRLEN);
1364edb46e9SPaul Traina 		break;
1374edb46e9SPaul Traina 
1384edb46e9SPaul Traina 	case TYPE_UNCOMPRESSED_TCP:
1394edb46e9SPaul Traina 		/*
1402ebf6c05SBill Fenner 		 * The connection id is stored in the IP protocol field.
1414edb46e9SPaul Traina 		 * Get it from the link layer since sl_uncompress_tcp()
1424edb46e9SPaul Traina 		 * has restored the IP header copy to IPPROTO_TCP.
1434edb46e9SPaul Traina 		 */
144*ee67461eSJoseph Mingrone 		lastconn = GET_U_1(((const struct ip *)(p + SLX_CHDR))->ip_p);
145*ee67461eSJoseph Mingrone 		ND_PRINT("utcp %u: ", lastconn);
1460bff6a5aSEd Maste 		if (dir == -1) {
1470bff6a5aSEd Maste 			/* Direction is bogus, don't use it */
148*ee67461eSJoseph Mingrone 			return;
1490bff6a5aSEd Maste 		}
150*ee67461eSJoseph Mingrone 		ND_TCHECK_SIZE(ip);
151943ee2b1SBill Fenner 		hlen = IP_HL(ip);
152*ee67461eSJoseph Mingrone 		ND_TCHECK_SIZE((const struct tcphdr *)&((const int *)ip)[hlen]);
1533340d773SGleb Smirnoff 		hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
1544edb46e9SPaul Traina 		lastlen[dir][lastconn] = length - (hlen << 2);
1554edb46e9SPaul Traina 		break;
1564edb46e9SPaul Traina 
1574edb46e9SPaul Traina 	default:
1580bff6a5aSEd Maste 		if (dir == -1) {
1590bff6a5aSEd Maste 			/* Direction is bogus, don't use it */
160*ee67461eSJoseph Mingrone 			return;
1610bff6a5aSEd Maste 		}
162*ee67461eSJoseph Mingrone 		if (GET_U_1(p + SLX_CHDR) & TYPE_COMPRESSED_TCP) {
163*ee67461eSJoseph Mingrone 			compressed_sl_print(ndo, p + SLX_CHDR, ip, length, dir);
164*ee67461eSJoseph Mingrone 			ND_PRINT(": ");
1654edb46e9SPaul Traina 		} else
166*ee67461eSJoseph Mingrone 			ND_PRINT("slip-%u!: ", GET_U_1(p + SLX_CHDR));
1674edb46e9SPaul Traina 	}
1684edb46e9SPaul Traina }
1694edb46e9SPaul Traina 
1704edb46e9SPaul Traina static const u_char *
print_sl_change(netdissect_options * ndo,const char * str,const u_char * cp)1713c602fabSXin LI print_sl_change(netdissect_options *ndo,
172*ee67461eSJoseph Mingrone                 const char *str, const u_char *cp)
1734edb46e9SPaul Traina {
174*ee67461eSJoseph Mingrone 	u_int i;
1754edb46e9SPaul Traina 
176*ee67461eSJoseph Mingrone 	if ((i = GET_U_1(cp)) == 0) {
177*ee67461eSJoseph Mingrone 		cp++;
178*ee67461eSJoseph Mingrone 		i = GET_BE_U_2(cp);
1794edb46e9SPaul Traina 		cp += 2;
1804edb46e9SPaul Traina 	}
181*ee67461eSJoseph Mingrone 	ND_PRINT(" %s%u", str, i);
1824edb46e9SPaul Traina 	return (cp);
1834edb46e9SPaul Traina }
1844edb46e9SPaul Traina 
1854edb46e9SPaul Traina static const u_char *
print_sl_winchange(netdissect_options * ndo,const u_char * cp)1863c602fabSXin LI print_sl_winchange(netdissect_options *ndo,
187*ee67461eSJoseph Mingrone                    const u_char *cp)
1884edb46e9SPaul Traina {
189*ee67461eSJoseph Mingrone 	int16_t i;
1904edb46e9SPaul Traina 
191*ee67461eSJoseph Mingrone 	if ((i = GET_U_1(cp)) == 0) {
192*ee67461eSJoseph Mingrone 		cp++;
193*ee67461eSJoseph Mingrone 		i = GET_BE_S_2(cp);
1944edb46e9SPaul Traina 		cp += 2;
1954edb46e9SPaul Traina 	}
1964edb46e9SPaul Traina 	if (i >= 0)
197*ee67461eSJoseph Mingrone 		ND_PRINT(" W+%d", i);
1984edb46e9SPaul Traina 	else
199*ee67461eSJoseph Mingrone 		ND_PRINT(" W%d", i);
2004edb46e9SPaul Traina 	return (cp);
2014edb46e9SPaul Traina }
2024edb46e9SPaul Traina 
203*ee67461eSJoseph Mingrone static void
compressed_sl_print(netdissect_options * ndo,const u_char * chdr,const struct ip * ip,u_int length,int dir)2043c602fabSXin LI compressed_sl_print(netdissect_options *ndo,
2053c602fabSXin LI                     const u_char *chdr, const struct ip *ip,
2064edb46e9SPaul Traina                     u_int length, int dir)
2074edb46e9SPaul Traina {
208*ee67461eSJoseph Mingrone 	const u_char *cp = chdr;
209*ee67461eSJoseph Mingrone 	u_int flags, hlen;
2104edb46e9SPaul Traina 
211*ee67461eSJoseph Mingrone 	flags = GET_U_1(cp);
212*ee67461eSJoseph Mingrone 	cp++;
2134edb46e9SPaul Traina 	if (flags & NEW_C) {
214*ee67461eSJoseph Mingrone 		lastconn = GET_U_1(cp);
215*ee67461eSJoseph Mingrone 		cp++;
216*ee67461eSJoseph Mingrone 		ND_PRINT("ctcp %u", lastconn);
2174edb46e9SPaul Traina 	} else
218*ee67461eSJoseph Mingrone 		ND_PRINT("ctcp *");
2194edb46e9SPaul Traina 
2204edb46e9SPaul Traina 	/* skip tcp checksum */
2214edb46e9SPaul Traina 	cp += 2;
2224edb46e9SPaul Traina 
2234edb46e9SPaul Traina 	switch (flags & SPECIALS_MASK) {
2244edb46e9SPaul Traina 	case SPECIAL_I:
225*ee67461eSJoseph Mingrone 		ND_PRINT(" *SA+%u", lastlen[dir][lastconn]);
2264edb46e9SPaul Traina 		break;
2274edb46e9SPaul Traina 
2284edb46e9SPaul Traina 	case SPECIAL_D:
229*ee67461eSJoseph Mingrone 		ND_PRINT(" *S+%u", lastlen[dir][lastconn]);
2304edb46e9SPaul Traina 		break;
2314edb46e9SPaul Traina 
2324edb46e9SPaul Traina 	default:
2334edb46e9SPaul Traina 		if (flags & NEW_U)
2343c602fabSXin LI 			cp = print_sl_change(ndo, "U=", cp);
2354edb46e9SPaul Traina 		if (flags & NEW_W)
2363c602fabSXin LI 			cp = print_sl_winchange(ndo, cp);
2374edb46e9SPaul Traina 		if (flags & NEW_A)
2383c602fabSXin LI 			cp = print_sl_change(ndo, "A+", cp);
2394edb46e9SPaul Traina 		if (flags & NEW_S)
2403c602fabSXin LI 			cp = print_sl_change(ndo, "S+", cp);
2414edb46e9SPaul Traina 		break;
2424edb46e9SPaul Traina 	}
2434edb46e9SPaul Traina 	if (flags & NEW_I)
2443c602fabSXin LI 		cp = print_sl_change(ndo, "I+", cp);
2454edb46e9SPaul Traina 
2464edb46e9SPaul Traina 	/*
2474edb46e9SPaul Traina 	 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
2484edb46e9SPaul Traina 	 * 'cp - chdr' is the length of the compressed header.
2494edb46e9SPaul Traina 	 * 'length - hlen' is the amount of data in the packet.
2504edb46e9SPaul Traina 	 */
251*ee67461eSJoseph Mingrone 	ND_TCHECK_SIZE(ip);
252943ee2b1SBill Fenner 	hlen = IP_HL(ip);
253*ee67461eSJoseph Mingrone 	ND_TCHECK_SIZE((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
2543340d773SGleb Smirnoff 	hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
2554edb46e9SPaul Traina 	lastlen[dir][lastconn] = length - (hlen << 2);
256*ee67461eSJoseph Mingrone 	ND_PRINT(" %u (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));
2574edb46e9SPaul Traina }
258