xref: /freebsd/contrib/tcpdump/print-chdlc.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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.28.2.3 2004/03/24 00:46:03 guy 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 	const struct ip *ip;
52 	u_int proto;
53 
54 	if (caplen < CHDLC_HDRLEN) {
55 		printf("[|chdlc]");
56 		return (caplen);
57 	}
58 
59 	proto = EXTRACT_16BITS(&p[2]);
60 	if (eflag) {
61 		switch (p[0]) {
62 		case CHDLC_UNICAST:
63 			printf("unicast ");
64 			break;
65 		case CHDLC_BCAST:
66 			printf("bcast ");
67 			break;
68 		default:
69 			printf("0x%02x ", p[0]);
70 			break;
71 		}
72 		printf("%d %04x: ", length, proto);
73 	}
74 
75 	length -= CHDLC_HDRLEN;
76 	ip = (const struct ip *)(p + CHDLC_HDRLEN);
77 	switch (proto) {
78 	case ETHERTYPE_IP:
79 		ip_print((const u_char *)ip, length);
80 		break;
81 #ifdef INET6
82 	case ETHERTYPE_IPV6:
83 		ip6_print((const u_char *)ip, length);
84 		break;
85 #endif
86 	case CHDLC_TYPE_SLARP:
87 		chdlc_slarp_print((const u_char *)ip, length);
88 		break;
89 #if 0
90 	case CHDLC_TYPE_CDP:
91 		chdlc_cdp_print((const u_char *)ip, length);
92 		break;
93 #endif
94         case ETHERTYPE_MPLS:
95         case ETHERTYPE_MPLS_MULTI:
96                 mpls_print((const u_char *)(ip), length);
97 		break;
98         case ETHERTYPE_ISO:
99                 /* is the fudge byte set ? lets verify by spotting ISO headers */
100                 if (*(p+CHDLC_HDRLEN+1) == 0x81 ||
101                     *(p+CHDLC_HDRLEN+1) == 0x82 ||
102                     *(p+CHDLC_HDRLEN+1) == 0x83)
103                     isoclns_print(p+CHDLC_HDRLEN+1, length-1, length-1);
104                 else
105                     isoclns_print(p+CHDLC_HDRLEN, length, length);
106                 break;
107 	default:
108                 printf("unknown CHDLC protocol (0x%04x)", proto);
109                 break;
110 	}
111 
112 	return (CHDLC_HDRLEN);
113 }
114 
115 struct cisco_slarp {
116 	u_int32_t code;
117 #define SLARP_REQUEST	0
118 #define SLARP_REPLY	1
119 #define SLARP_KEEPALIVE	2
120 	union {
121 		struct {
122 			struct in_addr addr;
123 			struct in_addr mask;
124 			u_int16_t unused[3];
125 		} addr;
126 		struct {
127 			u_int32_t myseq;
128 			u_int32_t yourseq;
129 			u_int16_t rel;
130 			u_int16_t t1;
131 			u_int16_t t2;
132 		} keep;
133 	} un;
134 };
135 
136 #define SLARP_LEN	18
137 
138 static void
139 chdlc_slarp_print(const u_char *cp, u_int length)
140 {
141 	const struct cisco_slarp *slarp;
142 
143 	if (length < SLARP_LEN)
144 		goto trunc;
145 
146 	slarp = (const struct cisco_slarp *)cp;
147 	TCHECK(*slarp);
148         printf("SLARP (length: %u), ",length);
149 	switch (EXTRACT_32BITS(&slarp->code)) {
150 	case SLARP_REQUEST:
151 		printf("request");
152                 /* ok we do not know it - but lets at least dump it */
153                 print_unknown_data(cp+4,"\n\t",length-4);
154 		break;
155 	case SLARP_REPLY:
156 		printf("reply %s/%s",
157 			ipaddr_string(&slarp->un.addr.addr),
158 			ipaddr_string(&slarp->un.addr.mask));
159 		break;
160 	case SLARP_KEEPALIVE:
161 		printf("keepalive: mineseen=0x%08x, yourseen=0x%08x",
162 			EXTRACT_32BITS(&slarp->un.keep.myseq),
163 			EXTRACT_32BITS(&slarp->un.keep.yourseq));
164 		printf(", reliability=0x%04x, t1=%d.%d",
165 			EXTRACT_16BITS(&slarp->un.keep.rel),
166 			EXTRACT_16BITS(&slarp->un.keep.t1),
167 			EXTRACT_16BITS(&slarp->un.keep.t2));
168 		break;
169 	default:
170 		printf("0x%02x unknown", EXTRACT_32BITS(&slarp->code));
171                 if (vflag <= 1)
172                     print_unknown_data(cp+4,"\n\t",length-4);
173 		break;
174 	}
175 
176 	if (SLARP_LEN < length && vflag)
177 		printf(", (trailing junk: %d bytes)", length - SLARP_LEN);
178         if (vflag > 1)
179             print_unknown_data(cp+4,"\n\t",length-4);
180 	return;
181 
182 trunc:
183 	printf("[|slarp]");
184 }
185