]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
style pedant. buffer length check cleanup will be next.
[tcpdump] / print-llc.c
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Code by Matt Thomas, Digital Equipment Corporation
22 * with an awful lot of hacking by Jeffrey Mogul, DECWRL
23 */
24
25 #ifndef lint
26 static const char rcsid[] =
27 "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.29 2000-06-10 20:57:56 assar Exp $";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/param.h>
35 #include <sys/time.h>
36
37 #include <netinet/in.h>
38
39 #include <ctype.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "extract.h" /* must come after interface.h */
47
48 #include "llc.h"
49
50 static struct tok cmd2str[] = {
51 { LLC_UI, "ui" },
52 { LLC_TEST, "test" },
53 { LLC_XID, "xid" },
54 { LLC_UA, "ua" },
55 { LLC_DISC, "disc" },
56 { LLC_DM, "dm" },
57 { LLC_SABME, "sabme" },
58 { LLC_FRMR, "frmr" },
59 { 0, NULL }
60 };
61
62 /*
63 * Returns non-zero IFF it succeeds in printing the header
64 */
65 int
66 llc_print(const u_char *p, u_int length, u_int caplen,
67 const u_char *esrc, const u_char *edst)
68 {
69 struct llc llc;
70 register u_short et;
71 register int ret;
72
73 if (caplen < 3) {
74 (void)printf("[|llc]");
75 default_print((u_char *)p, caplen);
76 return(0);
77 }
78
79 /* Watch out for possible alignment problems */
80 memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
81
82 if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
83 ipx_print(p, length);
84 return (1);
85 }
86
87 /* Cisco Discovery Protocol - SNAP & ether type 0x2000 */
88 if(llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP &&
89 llc.llcui == LLC_UI &&
90 llc.ethertype[0] == 0x20 && llc.ethertype[1] == 0x00 ) {
91 cdp_print( p, length, caplen, esrc, edst);
92 return (1);
93 }
94
95 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
96 stp_print(p, length);
97 return (1);
98 }
99 if (llc.ssap == 0xf0 && llc.dsap == 0xf0) {
100 /*
101 * we don't actually have a full netbeui parser yet, but the
102 * smb parser can handle many smb-in-netbeui packets, which
103 * is very useful, so we call that
104 */
105 netbeui_print(p + 2, p + min(caplen, length));
106 return (1);
107 }
108 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
109 && llc.llcui == LLC_UI) {
110 isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
111 return (1);
112 }
113
114 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
115 && llc.llcui == LLC_UI) {
116 if (caplen < sizeof(llc)) {
117 (void)printf("[|llc-snap]");
118 default_print((u_char *)p, caplen);
119 return (0);
120 }
121 if (vflag)
122 (void)printf("snap %s ", protoid_string(llc.llcpi));
123
124 caplen -= sizeof(llc);
125 length -= sizeof(llc);
126 p += sizeof(llc);
127
128 /* This is an encapsulated Ethernet packet */
129 et = EXTRACT_16BITS(&llc.ethertype[0]);
130 ret = ether_encap_print(et, p, length, caplen);
131 if (ret)
132 return (ret);
133 }
134
135 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
136 if (eflag)
137 (void)printf("%s ", llcsap_string(llc.dsap));
138 else
139 (void)printf("%s > %s %s ",
140 etheraddr_string(esrc),
141 etheraddr_string(edst),
142 llcsap_string(llc.dsap));
143 } else {
144 if (eflag)
145 (void)printf("%s > %s ",
146 llcsap_string(llc.ssap & ~LLC_GSAP),
147 llcsap_string(llc.dsap));
148 else
149 (void)printf("%s %s > %s %s ",
150 etheraddr_string(esrc),
151 llcsap_string(llc.ssap & ~LLC_GSAP),
152 etheraddr_string(edst),
153 llcsap_string(llc.dsap));
154 }
155
156 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
157 const char *m;
158 char f;
159 m = tok2str(cmd2str, "%02x", LLC_U_CMD(llc.llcu));
160 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
161 case 0: f = 'C'; break;
162 case LLC_GSAP: f = 'R'; break;
163 case LLC_U_POLL: f = 'P'; break;
164 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
165 default: f = '?'; break;
166 }
167
168 printf("%s/%c", m, f);
169
170 p += 3;
171 length -= 3;
172 caplen -= 3;
173
174 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
175 if (*p == LLC_XID_FI) {
176 printf(": %02x %02x", p[1], p[2]);
177 p += 3;
178 length -= 3;
179 caplen -= 3;
180 }
181 }
182
183 if (!strcmp(m,"ui") && f=='C') {
184 /*
185 * we don't have a proper ipx decoder yet, but there
186 * is a partial one in the smb code
187 */
188 ipx_netbios_print(p,p+min(caplen,length));
189 }
190
191 } else {
192 char f;
193 llc.llcis = ntohs(llc.llcis);
194 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
195 case 0: f = 'C'; break;
196 case LLC_GSAP: f = 'R'; break;
197 case LLC_U_POLL: f = 'P'; break;
198 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
199 default: f = '?'; break;
200 }
201
202 if ((llc.llcu & LLC_S_FMT) == LLC_S_FMT) {
203 static char *llc_s[] = { "rr", "rej", "rnr", "03" };
204 (void)printf("%s (r=%d,%c)",
205 llc_s[LLC_S_CMD(llc.llcis)],
206 LLC_IS_NR(llc.llcis),
207 f);
208 } else {
209 (void)printf("I (s=%d,r=%d,%c)",
210 LLC_I_NS(llc.llcis),
211 LLC_IS_NR(llc.llcis),
212 f);
213 }
214 p += 4;
215 length -= 4;
216 caplen -= 4;
217 }
218 (void)printf(" len=%d", length);
219 return(1);
220 }