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