]> The Tcpdump Group git mirrors - tcpdump/blob - print-chdlc.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[tcpdump] / print-chdlc.c
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 /* \summary: Cisco HDLC printer */
23
24 #include <config.h>
25
26 #include "netdissect-stdinc.h"
27
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "addrtoname.h"
31 #include "ethertype.h"
32 #include "extract.h"
33 #include "chdlc.h"
34 #include "nlpid.h"
35
36 static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
37
38 static const struct tok chdlc_cast_values[] = {
39 { CHDLC_UNICAST, "unicast" },
40 { CHDLC_BCAST, "bcast" },
41 { 0, NULL}
42 };
43
44
45 /* Standard CHDLC printer */
46 void
47 chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
48 {
49 ndo->ndo_protocol = "chdlc";
50 ND_TCHECK_LEN(p, CHDLC_HDRLEN);
51 ndo->ndo_ll_hdr_len += CHDLC_HDRLEN;
52 chdlc_print(ndo, p, h->len);
53 }
54
55 void
56 chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
57 {
58 u_int proto;
59
60 ndo->ndo_protocol = "chdlc";
61 ND_ICHECK_U(length, <, CHDLC_HDRLEN);
62 proto = GET_BE_U_2(p + 2);
63 if (ndo->ndo_eflag) {
64 ND_PRINT("%s, ethertype %s (0x%04x), length %u: ",
65 tok2str(chdlc_cast_values, "0x%02x", GET_U_1(p)),
66 tok2str(ethertype_values, "Unknown", proto),
67 proto,
68 length);
69 }
70
71 length -= CHDLC_HDRLEN;
72 p += CHDLC_HDRLEN;
73
74 switch (proto) {
75 case ETHERTYPE_IP:
76 ip_print(ndo, p, length);
77 break;
78 case ETHERTYPE_IPV6:
79 ip6_print(ndo, p, length);
80 break;
81 case CHDLC_TYPE_SLARP:
82 chdlc_slarp_print(ndo, p, length);
83 break;
84 case ETHERTYPE_MPLS:
85 case ETHERTYPE_MPLS_MULTI:
86 mpls_print(ndo, p, length);
87 break;
88 case ETHERTYPE_ISO:
89 /* is the fudge byte set ? lets verify by spotting ISO headers */
90 ND_ICHECK_U(length, <, 2);
91 if (GET_U_1(p + 1) == NLPID_CLNP ||
92 GET_U_1(p + 1) == NLPID_ESIS ||
93 GET_U_1(p + 1) == NLPID_ISIS)
94 isoclns_print(ndo, p + 1, length - 1);
95 else
96 isoclns_print(ndo, p, length);
97 break;
98 default:
99 if (!ndo->ndo_eflag)
100 ND_PRINT("unknown CHDLC protocol (0x%04x)", proto);
101 break;
102 }
103
104 return;
105
106 invalid:
107 nd_print_invalid(ndo);
108 }
109
110 /*
111 * The fixed-length portion of a SLARP packet.
112 */
113 struct cisco_slarp {
114 nd_uint32_t code;
115 #define SLARP_REQUEST 0
116 #define SLARP_REPLY 1
117 #define SLARP_KEEPALIVE 2
118 union {
119 struct {
120 uint8_t addr[4];
121 uint8_t mask[4];
122 } addr;
123 struct {
124 nd_uint32_t myseq;
125 nd_uint32_t yourseq;
126 nd_uint16_t rel;
127 } keep;
128 } un;
129 };
130
131 #define SLARP_MIN_LEN 14
132 #define SLARP_MAX_LEN 18
133
134 static void
135 chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
136 {
137 const struct cisco_slarp *slarp;
138 u_int sec,min,hrs,days;
139
140 ndo->ndo_protocol = "chdlc_slarp";
141 ND_PRINT("SLARP");
142 ND_ICHECK_U(length, <, SLARP_MIN_LEN);
143 ND_PRINT(" (length: %u), ",length);
144
145 slarp = (const struct cisco_slarp *)cp;
146 ND_TCHECK_LEN(slarp, SLARP_MIN_LEN);
147 switch (GET_BE_U_4(slarp->code)) {
148 case SLARP_REQUEST:
149 ND_PRINT("request");
150 /*
151 * At least according to William "Chops" Westfield's
152 * message in
153 *
154 * https://web.archive.org/web/20190725151313/www.nethelp.no/net/cisco-hdlc.txt
155 *
156 * the address and mask aren't used in requests -
157 * they're just zero.
158 */
159 break;
160 case SLARP_REPLY:
161 ND_PRINT("reply %s/%s",
162 GET_IPADDR_STRING(slarp->un.addr.addr),
163 GET_IPADDR_STRING(slarp->un.addr.mask));
164 break;
165 case SLARP_KEEPALIVE:
166 ND_PRINT("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
167 GET_BE_U_4(slarp->un.keep.myseq),
168 GET_BE_U_4(slarp->un.keep.yourseq),
169 GET_BE_U_2(slarp->un.keep.rel));
170
171 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
172 cp += SLARP_MIN_LEN;
173 sec = GET_BE_U_4(cp) / 1000;
174 min = sec / 60; sec -= min * 60;
175 hrs = min / 60; min -= hrs * 60;
176 days = hrs / 24; hrs -= days * 24;
177 ND_PRINT(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
178 }
179 break;
180 default:
181 ND_PRINT("0x%02x unknown", GET_BE_U_4(slarp->code));
182 if (ndo->ndo_vflag <= 1)
183 print_unknown_data(ndo,cp+4,"\n\t",length-4);
184 break;
185 }
186
187 if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
188 ND_PRINT(", (trailing junk: %u bytes)", length - SLARP_MAX_LEN);
189 if (ndo->ndo_vflag > 1)
190 print_unknown_data(ndo,cp+4,"\n\t",length-4);
191 return;
192
193 invalid:
194 nd_print_invalid(ndo);
195 }