]> The Tcpdump Group git mirrors - tcpdump/blob - print-zep.c
Merge branch 'master' into fix_udp_frag_badlen
[tcpdump] / print-zep.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 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: ZigBee Encapsulation Protocol (ZEP) 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
32 #include "extract.h"
33
34 /* From wireshark packet-zep.c:
35 *
36 ***********************************************************************
37 *
38 * ZEP Packets must be received in the following format:
39 *
40 * |UDP Header| ZEP Header |IEEE 802.15.4 Packet|
41 * | 8 bytes | 16/32 bytes | <= 127 bytes |
42 *
43 ***********************************************************************
44 *
45 * ZEP v1 Header will have the following format:
46 *
47 * |Preamble|Version|Channel ID|Device ID|CRC/LQI Mode|LQI Val|Reserved|Length|
48 * |2 bytes |1 byte | 1 byte | 2 bytes | 1 byte |1 byte |7 bytes |1 byte|
49 *
50 * ZEP v2 Header will have the following format (if type=1/Data):
51 * |Prmbl|Ver |Type |ChnlID|DevID|C/L Mode|LQI|NTP TS|Seq#|Res |Len|
52 * | 2 | 1 | 1 | 1 | 2 | 1 | 1 | 8 | 4 | 10 | 1 |
53 *
54 * ZEP v2 Header will have the following format (if type=2/Ack):
55 * |Preamble|Version| Type |Sequence#|
56 * |2 bytes |1 byte |1 byte| 4 bytes |
57 *------------------------------------------------------------
58 */
59
60 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
61 #define JAN_1970 2208988800U
62
63 /* Print timestamp */
64 static void zep_print_ts(netdissect_options *ndo, const u_char *p)
65 {
66 int32_t i;
67 uint32_t uf;
68 uint32_t f;
69 float ff;
70
71 i = GET_BE_U_4(p);
72 uf = GET_BE_U_4(p + 4);
73 ff = (float) uf;
74 if (ff < 0.0) /* some compilers are buggy */
75 ff += FMAXINT;
76 ff = (float) (ff / FMAXINT); /* shift radix point by 32 bits */
77 f = (uint32_t) (ff * 1000000000.0); /* treat fraction as parts per
78 billion */
79 ND_PRINT("%u.%09d", i, f);
80
81 #ifdef HAVE_STRFTIME
82 /*
83 * print the time in human-readable format.
84 */
85 if (i) {
86 time_t seconds = i - JAN_1970;
87 struct tm *tm;
88 char time_buf[128];
89
90 tm = localtime(&seconds);
91 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
92 ND_PRINT(" (%s)", time_buf);
93 }
94 #endif
95 }
96
97 /*
98 * Main function to print packets.
99 */
100
101 void
102 zep_print(netdissect_options *ndo,
103 const u_char *bp, u_int len)
104 {
105 uint8_t version, inner_len;
106 uint32_t seq_no;
107
108 ndo->ndo_protocol = "zep";
109
110 nd_print_protocol_caps(ndo);
111
112 ND_TCHECK_LEN(bp, 8);
113
114 if (GET_U_1(bp) != 'E' || GET_U_1(bp + 1) != 'X')
115 goto trunc;
116
117 version = GET_U_1(bp + 2);
118 ND_PRINT("v%d ", version);
119
120 if (version == 1) {
121 /* ZEP v1 packet. */
122 ND_TCHECK_LEN(bp, 16);
123 ND_PRINT("Channel ID %d, Device ID 0x%04x, ",
124 GET_U_1(bp + 3), GET_BE_U_2(bp + 4));
125 if (GET_U_1(bp + 6))
126 ND_PRINT("CRC, ");
127 else
128 ND_PRINT("LQI %d, ", GET_U_1(bp + 7));
129 inner_len = GET_U_1(bp + 15);
130 ND_PRINT("inner len = %d", inner_len);
131
132 bp += 16;
133 len -= 16;
134 } else {
135 /* ZEP v2 packet. */
136 if (GET_U_1(bp + 3) == 2) {
137 /* ZEP v2 ack. */
138 seq_no = GET_BE_U_4(bp + 4);
139 ND_PRINT("ACK, seq# = %d", seq_no);
140 inner_len = 0;
141 bp += 8;
142 len -= 8;
143 } else {
144 /* ZEP v2 data, or some other. */
145 ND_TCHECK_LEN(bp, 32);
146
147 ND_PRINT("Type %d, Channel ID %d, Device ID 0x%04x, ",
148 GET_U_1(bp + 3), GET_U_1(bp + 4),
149 GET_BE_U_2(bp + 5));
150 if (GET_U_1(bp + 7))
151 ND_PRINT("CRC, ");
152 else
153 ND_PRINT("LQI %d, ", GET_U_1(bp + 8));
154
155 zep_print_ts(ndo, bp + 9);
156 seq_no = GET_BE_U_4(bp + 17);
157 inner_len = GET_U_1(bp + 31);
158 ND_PRINT(", seq# = %d, inner len = %d",
159 seq_no, inner_len);
160 bp += 32;
161 len -= 32;
162 }
163 }
164
165 if (inner_len != 0) {
166 /* Call 802.15.4 dissector. */
167 ND_PRINT("\n\t");
168 if (ieee802_15_4_print(ndo, bp, inner_len)) {
169 bp += len;
170 len = 0;
171 }
172 }
173
174 if (!ndo->ndo_suppress_default_print)
175 ND_DEFAULTPRINT(bp, len);
176
177 return;
178 trunc:
179 nd_print_trunc(ndo);
180 }