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