xref: /freebsd/contrib/tcpdump/print-chdlc.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*
2  * Copyright (c) 1990, 1991, 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 
22 #ifndef lint
23 static const char rcsid[] _U_ =
24     "@(#) $Header: /tcpdump/master/tcpdump/print-chdlc.c,v 1.32.2.8 2005/08/23 10:29:42 hannes Exp $ (LBL)";
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <tcpdump-stdinc.h>
32 
33 #include <pcap.h>
34 #include <stdio.h>
35 
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ethertype.h"
39 #include "extract.h"
40 #include "ppp.h"
41 #include "chdlc.h"
42 
43 static void chdlc_slarp_print(const u_char *, u_int);
44 
45 /* Standard CHDLC printer */
46 u_int
47 chdlc_if_print(const struct pcap_pkthdr *h, register const u_char *p)
48 {
49 	register u_int length = h->len;
50 	register u_int caplen = h->caplen;
51 
52 	if (caplen < CHDLC_HDRLEN) {
53 		printf("[|chdlc]");
54 		return (caplen);
55 	}
56         return (chdlc_print(p,length));
57 }
58 
59 u_int
60 chdlc_print(register const u_char *p, u_int length) {
61 	u_int proto;
62 	const struct ip *ip;
63 
64 	proto = EXTRACT_16BITS(&p[2]);
65 	if (eflag) {
66 		switch (p[0]) {
67 		case CHDLC_UNICAST:
68 			printf("unicast ");
69 			break;
70 		case CHDLC_BCAST:
71 			printf("bcast ");
72 			break;
73 		default:
74 			printf("0x%02x ", p[0]);
75 			break;
76 		}
77 		printf("%d %04x: ", length, proto);
78 	}
79 
80 	length -= CHDLC_HDRLEN;
81 	ip = (const struct ip *)(p + CHDLC_HDRLEN);
82 	switch (proto) {
83 	case ETHERTYPE_IP:
84 		ip_print(gndo, (const u_char *)ip, length);
85 		break;
86 #ifdef INET6
87 	case ETHERTYPE_IPV6:
88 		ip6_print((const u_char *)ip, length);
89 		break;
90 #endif
91 	case CHDLC_TYPE_SLARP:
92 		chdlc_slarp_print((const u_char *)ip, length);
93 		break;
94 #if 0
95 	case CHDLC_TYPE_CDP:
96 		chdlc_cdp_print((const u_char *)ip, length);
97 		break;
98 #endif
99         case ETHERTYPE_MPLS:
100         case ETHERTYPE_MPLS_MULTI:
101                 mpls_print((const u_char *)(ip), length);
102 		break;
103         case ETHERTYPE_ISO:
104                 /* is the fudge byte set ? lets verify by spotting ISO headers */
105                 if (*(p+CHDLC_HDRLEN+1) == 0x81 ||
106                     *(p+CHDLC_HDRLEN+1) == 0x82 ||
107                     *(p+CHDLC_HDRLEN+1) == 0x83)
108                     isoclns_print(p+CHDLC_HDRLEN+1, length-1, length-1);
109                 else
110                     isoclns_print(p+CHDLC_HDRLEN, length, length);
111                 break;
112 	default:
113                 printf("unknown CHDLC protocol (0x%04x)", proto);
114                 break;
115 	}
116 
117 	return (CHDLC_HDRLEN);
118 }
119 
120 /*
121  * The fixed-length portion of a SLARP packet.
122  */
123 struct cisco_slarp {
124 	u_int8_t code[4];
125 #define SLARP_REQUEST	0
126 #define SLARP_REPLY	1
127 #define SLARP_KEEPALIVE	2
128 	union {
129 		struct {
130 			u_int8_t addr[4];
131 			u_int8_t mask[4];
132 		} addr;
133 		struct {
134 			u_int8_t myseq[4];
135 			u_int8_t yourseq[4];
136 			u_int8_t rel[2];
137 		} keep;
138 	} un;
139 };
140 
141 #define SLARP_MIN_LEN	14
142 #define SLARP_MAX_LEN	18
143 
144 static void
145 chdlc_slarp_print(const u_char *cp, u_int length)
146 {
147 	const struct cisco_slarp *slarp;
148         u_int sec,min,hrs,days;
149 
150         printf("SLARP (length: %u), ",length);
151 	if (length < SLARP_MIN_LEN)
152 		goto trunc;
153 
154 	slarp = (const struct cisco_slarp *)cp;
155 	TCHECK2(*slarp, SLARP_MIN_LEN);
156 	switch (EXTRACT_32BITS(&slarp->code)) {
157 	case SLARP_REQUEST:
158 		printf("request");
159 		/*
160 		 * At least according to William "Chops" Westfield's
161 		 * message in
162 		 *
163 		 *	http://www.nethelp.no/net/cisco-hdlc.txt
164 		 *
165 		 * the address and mask aren't used in requests -
166 		 * they're just zero.
167 		 */
168 		break;
169 	case SLARP_REPLY:
170 		printf("reply %s/%s",
171 			ipaddr_string(&slarp->un.addr.addr),
172 			ipaddr_string(&slarp->un.addr.mask));
173 		break;
174 	case SLARP_KEEPALIVE:
175 		printf("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
176                        EXTRACT_32BITS(&slarp->un.keep.myseq),
177                        EXTRACT_32BITS(&slarp->un.keep.yourseq),
178                        EXTRACT_16BITS(&slarp->un.keep.rel));
179 
180                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
181                         cp += SLARP_MIN_LEN;
182                         if (!TTEST2(*cp, 4))
183                                 goto trunc;
184                         sec = EXTRACT_32BITS(cp) / 1000;
185                         min = sec / 60; sec -= min * 60;
186                         hrs = min / 60; min -= hrs * 60;
187                         days = hrs / 24; hrs -= days * 24;
188                         printf(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
189                 }
190 		break;
191 	default:
192 		printf("0x%02x unknown", EXTRACT_32BITS(&slarp->code));
193                 if (vflag <= 1)
194                     print_unknown_data(cp+4,"\n\t",length-4);
195 		break;
196 	}
197 
198 	if (SLARP_MAX_LEN < length && vflag)
199 		printf(", (trailing junk: %d bytes)", length - SLARP_MAX_LEN);
200         if (vflag > 1)
201             print_unknown_data(cp+4,"\n\t",length-4);
202 	return;
203 
204 trunc:
205 	printf("[|slarp]");
206 }
207 
208 
209 /*
210  * Local Variables:
211  * c-style: whitesmith
212  * c-basic-offset: 8
213  * End:
214  */
215