xref: /freebsd/contrib/tcpdump/print-chdlc.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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.11 2000/10/09 01:53:19 guy 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 "ppp.h"
46 #include "chdlc.h"
47 
48 static void chdlc_slarp_print(const u_char *, u_int);
49 
50 /* Standard CHDLC printer */
51 void
52 chdlc_if_print(u_char *user, const struct pcap_pkthdr *h,
53 	     register const u_char *p)
54 {
55 	register u_int length = h->len;
56 	register u_int caplen = h->caplen;
57 	const struct ip *ip;
58 	u_int proto;
59 
60 	ts_print(&h->ts);
61 
62 	if (caplen < CHDLC_HDRLEN) {
63 		printf("[|chdlc]");
64 		goto out;
65 	}
66 
67 	/*
68 	 * Some printers want to get back at the link level addresses,
69 	 * and/or check that they're not walking off the end of the packet.
70 	 * Rather than pass them all the way down, we set these globals.
71 	 */
72 	proto = ntohs(*(u_short *)&p[2]);
73 	packetp = p;
74 	snapend = p + caplen;
75 
76 	if (eflag) {
77 		switch (p[0]) {
78 		case CHDLC_UNICAST:
79 			printf("unicast ");
80 			break;
81 		case CHDLC_BCAST:
82 			printf("bcast ");
83 			break;
84 		default:
85 			printf("0x%02x ", p[0]);
86 			break;
87 		}
88 		printf("%d %04x: ", length, proto);
89 	}
90 
91 	length -= CHDLC_HDRLEN;
92 	ip = (struct ip *)(p + CHDLC_HDRLEN);
93 	switch (proto) {
94 	case ETHERTYPE_IP:
95 		ip_print((const u_char *)ip, length);
96 		break;
97 #ifdef INET6
98 	case ETHERTYPE_IPV6:
99 		ip6_print((const u_char *)ip, length);
100 		break;
101 #endif
102 	case CHDLC_TYPE_SLARP:
103 		chdlc_slarp_print((const u_char *)ip, length);
104 		break;
105 #if 0
106 	case CHDLC_TYPE_CDP:
107 		chdlc_cdp_print((const u_char *)ip, length);
108 		break;
109 #endif
110 	}
111 	if (xflag)
112 		default_print((const u_char *)ip, caplen - CHDLC_HDRLEN);
113 out:
114 	putchar('\n');
115 }
116 
117 struct cisco_slarp {
118 	u_int32_t code;
119 #define SLARP_REQUEST	0
120 #define SLARP_REPLY	1
121 #define SLARP_KEEPALIVE	2
122 	union {
123 		struct {
124 			struct in_addr addr;
125 			struct in_addr mask;
126 			u_int16_t unused[3];
127 		} addr;
128 		struct {
129 			u_int32_t myseq;
130 			u_int32_t yourseq;
131 			u_int16_t rel;
132 			u_int16_t t1;
133 			u_int16_t t2;
134 		} keep;
135 	} un;
136 };
137 
138 #define SLARP_LEN	18
139 
140 static void
141 chdlc_slarp_print(const u_char *cp, u_int length)
142 {
143 	struct cisco_slarp *slarp;
144 
145 	if (length < SLARP_LEN) {
146 		printf("[|slarp]");
147 		return;
148 	}
149 
150 	slarp = (struct cisco_slarp *)cp;
151 	switch (ntohl(slarp->code)) {
152 	case SLARP_REQUEST:
153 		printf("slarp-request");
154 		break;
155 	case SLARP_REPLY:
156 		printf("slarp-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("slarp-keepalive my=0x%x your=0x%x ",
162 			(u_int32_t)ntohl(slarp->un.keep.myseq),
163 			(u_int32_t)ntohl(slarp->un.keep.yourseq));
164 		printf("reliability=0x%04x t1=%d.%d",
165 			ntohs(slarp->un.keep.rel), ntohs(slarp->un.keep.t1),
166 			ntohs(slarp->un.keep.t2));
167 		break;
168 	default:
169 		printf("slarp-0x%x unknown", (u_int32_t)ntohl(slarp->code));
170 		break;
171 	}
172 
173 	if (SLARP_LEN < length && vflag)
174 		printf("(trailing junk: %d bytes)", length - SLARP_LEN);
175 }
176