xref: /freebsd/contrib/tcpdump/print-cip.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 #ifndef lint
24 static const char rcsid[] =
25     "@(#) $Header: /tcpdump/master/tcpdump/print-cip.c,v 1.2 1999/11/21 09:36:49 fenner Exp $ (LBL)";
26 #endif
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <net/if.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcpip.h>
48 
49 #include <stdio.h>
50 #include <pcap.h>
51 
52 #include "interface.h"
53 #include "addrtoname.h"
54 
55 const u_char *packetp;
56 const u_char *snapend;
57 
58 #define RFC1483LLC_LEN 8
59 
60 static unsigned char rfcllc[] = {
61   0xaa,   /* DSAP: non-ISO */
62   0xaa,   /* SSAP: non-ISO */
63   0x03,   /* Ctrl: Unnumbered Information Command PDU */
64   0x00,   /* OUI: EtherType */
65   0x00,
66   0x00 };
67 
68 static inline void
69 cip_print(register const u_char *bp, int length)
70 {
71   int i;
72 
73   if (memcmp(rfcllc, bp, sizeof(rfcllc))) {
74     if (qflag) {
75       for(i=0;i<RFC1483LLC_LEN;i++)
76 	(void)printf("%2.2x ",bp[i]);
77     } else {
78       for(i=0;i<RFC1483LLC_LEN-2;i++)
79 	(void)printf("%2.2x ",bp[i]);
80       etherproto_string(((u_short*)bp)[3]);
81     }
82   } else {
83     if (qflag)
84       (void)printf("(null encapsulation)");
85     else {
86       (void)printf("(null encap)");
87       etherproto_string(ETHERTYPE_IP);
88     }
89   }
90 }
91 
92 /*
93  * This is the top level routine of the printer.  'p' is the points
94  * to the raw header of the packet, 'tvp' is the timestamp,
95  * 'length' is the length of the packet off the wire, and 'caplen'
96  * is the number of bytes actually captured.
97  */
98 void
99 cip_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
100 {
101 	int caplen = h->caplen;
102 	int length = h->len;
103 	u_short ether_type;
104 	u_short extracted_ethertype;
105 	u_short *bp;
106 
107 	ts_print(&h->ts);
108 
109 	if (memcmp(rfcllc, p, sizeof(rfcllc))==0 && caplen < RFC1483LLC_LEN) {
110 		printf("[|cip]");
111 		goto out;
112 	}
113 
114 	if (eflag)
115 	  cip_print(p, length);
116 
117 	/*
118 	 * Some printers want to get back at the ethernet addresses,
119 	 * and/or check that they're not walking off the end of the packet.
120 	 * Rather than pass them all the way down, we set these globals.
121 	 */
122 	packetp = p;
123 	snapend = p + caplen;
124 
125 	if (memcmp(rfcllc, p, sizeof(rfcllc))==0) {
126 	  length -= RFC1483LLC_LEN;
127 	  caplen -= RFC1483LLC_LEN;
128 	  bp = (u_short*)p;
129 	  p += RFC1483LLC_LEN;
130 	  ether_type = ntohs(bp[3]);
131 	} else
132 	  ether_type = ETHERTYPE_IP;
133 
134 	/*
135 	 * Is it (gag) an 802.3 encapsulation?
136 	 */
137 	extracted_ethertype = 0;
138 	if (ether_type < ETHERMTU) {
139 		/* Try to print the LLC-layer header & higher layers */
140 		if (llc_print(p, length, caplen, NULL, NULL)==0) {
141 			/* ether_type not known, print raw packet */
142 			if (!eflag)
143 				cip_print((u_char *)bp, length);
144 			if (extracted_ethertype) {
145 				printf("(LLC %s) ",
146 			       etherproto_string(htons(extracted_ethertype)));
147 			}
148 			if (!xflag && !qflag)
149 				default_print(p, caplen);
150 		}
151 	} else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
152 		/* ether_type not known, print raw packet */
153 		if (!eflag)
154 			cip_print((u_char *)bp, length + RFC1483LLC_LEN);
155 		if (!xflag && !qflag)
156 			default_print(p, caplen);
157 	}
158 	if (xflag)
159 		default_print(p, caplen);
160  out:
161 	putchar('\n');
162 }
163