xref: /freebsd/contrib/tcpdump/print-chdlc.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /* maybe it should be merged into print-ppp.c */
2 /*
3  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that: (1) source code distributions
8  * retain the above copyright notice and this paragraph in its entirety, (2)
9  * distributions including binary code include the above copyright notice and
10  * this paragraph in its entirety in the documentation or other materials
11  * provided with the distribution, and (3) all advertising materials mentioning
12  * features or use of this software display the following acknowledgement:
13  * ``This product includes software developed by the University of California,
14  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15  * the University nor the names of its contributors may be used to endorse
16  * or promote products derived from this software without specific prior
17  * written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22 
23 #ifndef lint
24 static const char rcsid[] =
25     "@(#) $Header: /tcpdump/master/tcpdump/print-chdlc.c,v 1.13 2001/09/17 21:57:57 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 
35 #include <netinet/in.h>
36 
37 #include <ctype.h>
38 #include <netdb.h>
39 #include <pcap.h>
40 #include <stdio.h>
41 
42 #include "interface.h"
43 #include "addrtoname.h"
44 #include "ethertype.h"
45 #include "extract.h"
46 #include "ppp.h"
47 #include "chdlc.h"
48 
49 static void chdlc_slarp_print(const u_char *, u_int);
50 
51 /* Standard CHDLC printer */
52 void
53 chdlc_if_print(u_char *user, const struct pcap_pkthdr *h,
54 	     register const u_char *p)
55 {
56 	register u_int length = h->len;
57 	register u_int caplen = h->caplen;
58 
59 	++infodelay;
60 	ts_print(&h->ts);
61 
62 	/*
63 	 * Some printers want to get back at the link level addresses,
64 	 * and/or check that they're not walking off the end of the packet.
65 	 * Rather than pass them all the way down, we set these globals.
66 	 */
67 	packetp = p;
68 	snapend = p + caplen;
69 
70 	chdlc_print(p, length, caplen);
71 
72 	putchar('\n');
73 	--infodelay;
74 	if (infoprint)
75 		info(0);
76 }
77 
78 void
79 chdlc_print(register const u_char *p, u_int length, u_int caplen)
80 {
81 	const struct ip *ip;
82 	u_int proto;
83 
84 	if (caplen < CHDLC_HDRLEN) {
85 		printf("[|chdlc]");
86 		return;
87 	}
88 
89 	proto = EXTRACT_16BITS(&p[2]);
90 	if (eflag) {
91 		switch (p[0]) {
92 		case CHDLC_UNICAST:
93 			printf("unicast ");
94 			break;
95 		case CHDLC_BCAST:
96 			printf("bcast ");
97 			break;
98 		default:
99 			printf("0x%02x ", p[0]);
100 			break;
101 		}
102 		printf("%d %04x: ", length, proto);
103 	}
104 
105 	length -= CHDLC_HDRLEN;
106 	ip = (const struct ip *)(p + CHDLC_HDRLEN);
107 	switch (proto) {
108 	case ETHERTYPE_IP:
109 		ip_print((const u_char *)ip, length);
110 		break;
111 #ifdef INET6
112 	case ETHERTYPE_IPV6:
113 		ip6_print((const u_char *)ip, length);
114 		break;
115 #endif
116 	case CHDLC_TYPE_SLARP:
117 		chdlc_slarp_print((const u_char *)ip, length);
118 		break;
119 #if 0
120 	case CHDLC_TYPE_CDP:
121 		chdlc_cdp_print((const u_char *)ip, length);
122 		break;
123 #endif
124 	}
125 	if (xflag)
126 		default_print((const u_char *)ip, caplen - CHDLC_HDRLEN);
127 }
128 
129 struct cisco_slarp {
130 	u_int32_t code;
131 #define SLARP_REQUEST	0
132 #define SLARP_REPLY	1
133 #define SLARP_KEEPALIVE	2
134 	union {
135 		struct {
136 			struct in_addr addr;
137 			struct in_addr mask;
138 			u_int16_t unused[3];
139 		} addr;
140 		struct {
141 			u_int32_t myseq;
142 			u_int32_t yourseq;
143 			u_int16_t rel;
144 			u_int16_t t1;
145 			u_int16_t t2;
146 		} keep;
147 	} un;
148 };
149 
150 #define SLARP_LEN	18
151 
152 static void
153 chdlc_slarp_print(const u_char *cp, u_int length)
154 {
155 	const struct cisco_slarp *slarp;
156 
157 	if (length < SLARP_LEN) {
158 		printf("[|slarp]");
159 		return;
160 	}
161 
162 	slarp = (const struct cisco_slarp *)cp;
163 	switch (ntohl(slarp->code)) {
164 	case SLARP_REQUEST:
165 		printf("slarp-request");
166 		break;
167 	case SLARP_REPLY:
168 		printf("slarp-reply %s/%s",
169 			ipaddr_string(&slarp->un.addr.addr),
170 			ipaddr_string(&slarp->un.addr.mask));
171 		break;
172 	case SLARP_KEEPALIVE:
173 		printf("slarp-keepalive my=0x%x your=0x%x ",
174 			(u_int32_t)ntohl(slarp->un.keep.myseq),
175 			(u_int32_t)ntohl(slarp->un.keep.yourseq));
176 		printf("reliability=0x%04x t1=%d.%d",
177 			ntohs(slarp->un.keep.rel), ntohs(slarp->un.keep.t1),
178 			ntohs(slarp->un.keep.t2));
179 		break;
180 	default:
181 		printf("slarp-0x%x unknown", (u_int32_t)ntohl(slarp->code));
182 		break;
183 	}
184 
185 	if (SLARP_LEN < length && vflag)
186 		printf("(trailing junk: %d bytes)", length - SLARP_LEN);
187 }
188