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