xref: /freebsd/contrib/tcpdump/print-cip.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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 #define NETDISSECT_REWORKED
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <string.h>
29 
30 #include <tcpdump-stdinc.h>
31 
32 #include "interface.h"
33 #include "addrtoname.h"
34 
35 #define RFC1483LLC_LEN	8
36 
37 static const unsigned char rfcllc[] = {
38 	0xaa,	/* DSAP: non-ISO */
39 	0xaa,	/* SSAP: non-ISO */
40 	0x03,	/* Ctrl: Unnumbered Information Command PDU */
41 	0x00,	/* OUI: EtherType */
42 	0x00,
43 	0x00 };
44 
45 static inline void
46 cip_print(netdissect_options *ndo, int length)
47 {
48 	/*
49 	 * There is no MAC-layer header, so just print the length.
50 	 */
51 	ND_PRINT((ndo, "%d: ", length));
52 }
53 
54 /*
55  * This is the top level routine of the printer.  'p' points
56  * to the LLC/SNAP or raw header of the packet, 'h->ts' is the timestamp,
57  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
58  * is the number of bytes actually captured.
59  */
60 u_int
61 cip_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
62 {
63 	u_int caplen = h->caplen;
64 	u_int length = h->len;
65 	u_short extracted_ethertype;
66 
67 	if (memcmp(rfcllc, p, sizeof(rfcllc))==0 && caplen < RFC1483LLC_LEN) {
68 		ND_PRINT((ndo, "[|cip]"));
69 		return (0);
70 	}
71 
72 	if (ndo->ndo_eflag)
73 		cip_print(ndo, length);
74 
75 	if (memcmp(rfcllc, p, sizeof(rfcllc)) == 0) {
76 		/*
77 		 * LLC header is present.  Try to print it & higher layers.
78 		 */
79 		if (llc_print(ndo, p, length, caplen, NULL, NULL,
80 		    &extracted_ethertype) == 0) {
81 			/* ether_type not known, print raw packet */
82 			if (!ndo->ndo_eflag)
83 				cip_print(ndo, length);
84 			if (extracted_ethertype) {
85 				ND_PRINT((ndo, "(LLC %s) ",
86 			       etherproto_string(htons(extracted_ethertype))));
87 			}
88 			if (!ndo->ndo_suppress_default_print)
89 				ND_DEFAULTPRINT(p, caplen);
90 		}
91 	} else {
92 		/*
93 		 * LLC header is absent; treat it as just IP.
94 		 */
95 		ip_print(ndo, p, length);
96 	}
97 
98 	return (0);
99 }
100 
101 
102 /*
103  * Local Variables:
104  * c-style: whitesmith
105  * c-basic-offset: 8
106  * End:
107  */
108