xref: /netbsd-src/external/bsd/tcpdump/dist/print-cip.c (revision f4748aaa01faf324805f9747191535eb6600f82c)
1 /*
2  * Marko Kiiskila carnil@cs.tut.fi
3  *
4  * Tampere University of Technology - Telecommunications Laboratory
5  *
6  * Permission to use, copy, modify and distribute this
7  * software and its documentation is hereby granted,
8  * provided that both the copyright notice and this
9  * permission notice appear in all copies of the software,
10  * derivative works or modified versions, and any portions
11  * thereof, that both notices appear in supporting
12  * documentation, and that the use of this software is
13  * acknowledged in any publications resulting from using
14  * the software.
15  *
16  * TUT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION AND DISCLAIMS ANY LIABILITY OF ANY KIND FOR
18  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS
19  * SOFTWARE.
20  *
21  */
22 
23 #include <sys/cdefs.h>
24 #ifndef lint
25 __RCSID("$NetBSD: print-cip.c,v 1.6 2017/02/05 04:05:05 spz Exp $");
26 #endif
27 
28 /* \summary: Classical-IP over ATM printer */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include <string.h>
35 
36 #include <netdissect-stdinc.h>
37 
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 
41 static const unsigned char rfcllc[] = {
42 	0xaa,	/* DSAP: non-ISO */
43 	0xaa,	/* SSAP: non-ISO */
44 	0x03,	/* Ctrl: Unnumbered Information Command PDU */
45 	0x00,	/* OUI: EtherType */
46 	0x00,
47 	0x00 };
48 
49 static inline void
50 cip_print(netdissect_options *ndo, u_int length)
51 {
52 	/*
53 	 * There is no MAC-layer header, so just print the length.
54 	 */
55 	ND_PRINT((ndo, "%u: ", length));
56 }
57 
58 /*
59  * This is the top level routine of the printer.  'p' points
60  * to the LLC/SNAP or raw header of the packet, 'h->ts' is the timestamp,
61  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
62  * is the number of bytes actually captured.
63  */
64 u_int
65 cip_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
66 {
67 	u_int caplen = h->caplen;
68 	u_int length = h->len;
69 	size_t cmplen;
70 	int llc_hdrlen;
71 
72 	cmplen = sizeof(rfcllc);
73 	if (cmplen > caplen)
74 		cmplen = caplen;
75 	if (cmplen > length)
76 		cmplen = length;
77 
78 	if (ndo->ndo_eflag)
79 		cip_print(ndo, length);
80 
81 	if (cmplen == 0) {
82 		ND_PRINT((ndo, "[|cip]"));
83 		return 0;
84 	}
85 	if (memcmp(rfcllc, p, cmplen) == 0) {
86 		/*
87 		 * LLC header is present.  Try to print it & higher layers.
88 		 */
89 		llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
90 		if (llc_hdrlen < 0) {
91 			/* packet type not known, print raw packet */
92 			if (!ndo->ndo_suppress_default_print)
93 				ND_DEFAULTPRINT(p, caplen);
94 			llc_hdrlen = -llc_hdrlen;
95 		}
96 	} else {
97 		/*
98 		 * LLC header is absent; treat it as just IP.
99 		 */
100 		llc_hdrlen = 0;
101 		ip_print(ndo, p, length);
102 	}
103 
104 	return (llc_hdrlen);
105 }
106 
107 
108 /*
109  * Local Variables:
110  * c-style: whitesmith
111  * c-basic-offset: 8
112  * End:
113  */
114