tcpdump  4.99.1
About: tcpdump is a tool for network monitoring and data acquisition.
  Fossies Dox: tcpdump-4.99.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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