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