1 /* packet-zbee-nwk.c
2  * Dissector routines for the ZigBee Network Layer (NWK)
3  * By Owen Kirby <osk@exegin.com>
4  * Copyright 2009 Exegin Technologies Limited
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 /*  Include Files */
14 #include "config.h"
15 
16 
17 #include <epan/packet.h>
18 #include <epan/exceptions.h>
19 #include <epan/prefs.h>
20 #include <epan/addr_resolv.h>
21 #include <epan/address_types.h>
22 #include <epan/expert.h>
23 #include <epan/proto_data.h>
24 #include <epan/conversation_table.h>
25 #include <epan/conversation_filter.h>
26 #include <epan/tap.h>
27 #include <wsutil/bits_ctz.h>    /* for ws_ctz */
28 #include <wsutil/pint.h>
29 #include "packet-ieee802154.h"
30 #include "packet-zbee.h"
31 #include "packet-zbee-nwk.h"
32 #include "packet-zbee-aps.h"    /* for ZBEE_APS_CMD_KEY_LENGTH */
33 #include "packet-zbee-security.h"
34 
35 /*************************/
36 /* Function Declarations */
37 /*************************/
38 /* Dissector Routines */
39 static int         dissect_zbee_nwk        (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data);
40 static void        dissect_zbee_nwk_cmd    (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet* packet);
41 static int         dissect_zbee_beacon     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data);
42 static int         dissect_zbip_beacon     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data);
43 static int         dissect_zbee_ie         (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data);
44 static void        dissect_ieee802154_zigbee_rejoin(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, guint *offset);
45 static void        dissect_ieee802154_zigbee_txpower(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, guint *offset);
46 
47 
48 /* Command Dissector Helpers */
49 static guint       dissect_zbee_nwk_route_req  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
50                                                 zbee_nwk_packet * packet, guint offset);
51 static guint       dissect_zbee_nwk_route_rep  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, guint8 version);
52 static guint       dissect_zbee_nwk_status     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
53 static guint       dissect_zbee_nwk_leave      (tvbuff_t *tvb, proto_tree *tree, guint offset);
54 static guint       dissect_zbee_nwk_route_rec  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
55                                                 zbee_nwk_packet * packet, guint offset);
56 static guint       dissect_zbee_nwk_rejoin_req (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
57                                                 zbee_nwk_packet * packet, guint offset);
58 static guint       dissect_zbee_nwk_rejoin_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
59                                                 zbee_nwk_packet * packet, guint offset);
60 static guint       dissect_zbee_nwk_link_status(tvbuff_t *tvb, proto_tree *tree, guint offset);
61 static guint       dissect_zbee_nwk_report     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
62 static guint       dissect_zbee_nwk_update     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
63 static guint       dissect_zbee_nwk_ed_timeout_request(tvbuff_t *tvb, proto_tree *tree, guint offset);
64 static guint       dissect_zbee_nwk_ed_timeout_response(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
65 static guint       dissect_zbee_nwk_link_pwr_delta(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
66 static void        proto_init_zbee_nwk         (void);
67 static void        proto_cleanup_zbee_nwk(void);
68 void               proto_register_zbee_nwk(void);
69 void               proto_reg_handoff_zbee_nwk(void);
70 
71 /********************/
72 /* Global Variables */
73 /********************/
74 static int proto_zbee_nwk = -1;
75 static int proto_zbee_beacon = -1;
76 static int proto_zbip_beacon = -1;
77 static int proto_zbee_ie = -1;
78 static int hf_zbee_nwk_fcf = -1;
79 static int hf_zbee_nwk_frame_type = -1;
80 static int hf_zbee_nwk_proto_version = -1;
81 static int hf_zbee_nwk_discover_route = -1;
82 static int hf_zbee_nwk_multicast = -1;
83 static int hf_zbee_nwk_security = -1;
84 static int hf_zbee_nwk_source_route = -1;
85 static int hf_zbee_nwk_ext_dst = -1;
86 static int hf_zbee_nwk_ext_src = -1;
87 static int hf_zbee_nwk_end_device_initiator = -1;
88 static int hf_zbee_nwk_dst = -1;
89 static int hf_zbee_nwk_src = -1;
90 static int hf_zbee_nwk_addr = -1;
91 static int hf_zbee_nwk_radius = -1;
92 static int hf_zbee_nwk_seqno = -1;
93 static int hf_zbee_nwk_mcast = -1;
94 static int hf_zbee_nwk_mcast_mode = -1;
95 static int hf_zbee_nwk_mcast_radius = -1;
96 static int hf_zbee_nwk_mcast_max_radius = -1;
97 static int hf_zbee_nwk_dst64 = -1;
98 static int hf_zbee_nwk_src64 = -1;
99 static int hf_zbee_nwk_addr64 = -1;
100 static int hf_zbee_nwk_src64_origin = -1;
101 static int hf_zbee_nwk_relay_count = -1;
102 static int hf_zbee_nwk_relay_index = -1;
103 static int hf_zbee_nwk_relay = -1;
104 
105 static int hf_zbee_nwk_cmd_id = -1;
106 static int hf_zbee_nwk_cmd_addr = -1;
107 static int hf_zbee_nwk_cmd_route_id = -1;
108 static int hf_zbee_nwk_cmd_route_dest = -1;
109 static int hf_zbee_nwk_cmd_route_orig = -1;
110 static int hf_zbee_nwk_cmd_route_resp = -1;
111 static int hf_zbee_nwk_cmd_route_dest_ext = -1;
112 static int hf_zbee_nwk_cmd_route_orig_ext = -1;
113 static int hf_zbee_nwk_cmd_route_resp_ext = -1;
114 static int hf_zbee_nwk_cmd_route_cost = -1;
115 static int hf_zbee_nwk_cmd_route_options = -1;
116 static int hf_zbee_nwk_cmd_route_opt_repair = -1;
117 static int hf_zbee_nwk_cmd_route_opt_multicast = -1;
118 static int hf_zbee_nwk_cmd_route_opt_dest_ext = -1;
119 static int hf_zbee_nwk_cmd_route_opt_resp_ext = -1;
120 static int hf_zbee_nwk_cmd_route_opt_orig_ext = -1;
121 static int hf_zbee_nwk_cmd_route_opt_many_to_one = -1;
122 static int hf_zbee_nwk_cmd_nwk_status = -1;
123 static int hf_zbee_nwk_cmd_nwk_status_command_id = -1;
124 static int hf_zbee_nwk_cmd_leave_rejoin = -1;
125 static int hf_zbee_nwk_cmd_leave_request = -1;
126 static int hf_zbee_nwk_cmd_leave_children = -1;
127 static int hf_zbee_nwk_cmd_relay_count = -1;
128 static int hf_zbee_nwk_cmd_relay_device = -1;
129 static int hf_zbee_nwk_cmd_cinfo = -1;
130 static int hf_zbee_nwk_cmd_cinfo_alt_coord = -1;
131 static int hf_zbee_nwk_cmd_cinfo_type = -1;
132 static int hf_zbee_nwk_cmd_cinfo_power = -1;
133 static int hf_zbee_nwk_cmd_cinfo_idle_rx = -1;
134 static int hf_zbee_nwk_cmd_cinfo_security = -1;
135 static int hf_zbee_nwk_cmd_cinfo_alloc = -1;
136 static int hf_zbee_nwk_cmd_rejoin_status = -1;
137 static int hf_zbee_nwk_cmd_link_last = -1;
138 static int hf_zbee_nwk_cmd_link_first = -1;
139 static int hf_zbee_nwk_cmd_link_count = -1;
140 static int hf_zbee_nwk_cmd_link_address = -1;
141 static int hf_zbee_nwk_cmd_link_incoming_cost = -1;
142 static int hf_zbee_nwk_cmd_link_outgoing_cost = -1;
143 static int hf_zbee_nwk_cmd_report_type = -1;
144 static int hf_zbee_nwk_cmd_report_count = -1;
145 static int hf_zbee_nwk_cmd_update_type = -1;
146 static int hf_zbee_nwk_cmd_update_count = -1;
147 static int hf_zbee_nwk_cmd_update_id = -1;
148 static int hf_zbee_nwk_panid = -1;
149 static int hf_zbee_zboss_nwk_cmd_key = -1;
150 static int hf_zbee_nwk_cmd_epid = -1;
151 static int hf_zbee_nwk_cmd_end_device_timeout_request_enum = -1;
152 static int hf_zbee_nwk_cmd_end_device_configuration = -1;
153 static int hf_zbee_nwk_cmd_end_device_timeout_resp_status = -1;
154 static int hf_zbee_nwk_cmd_end_device_timeout_resp_parent_info = -1;
155 static int hf_zbee_nwk_cmd_prnt_info_mac_data_poll_keepalive_supported = -1;
156 static int hf_zbee_nwk_cmd_prnt_info_ed_to_req_keepalive_supported = -1;
157 static int hf_zbee_nwk_cmd_prnt_info_power_negotiation_supported = -1;
158 static int hf_zbee_nwk_cmd_link_pwr_list_count = -1;
159 static int hf_zbee_nwk_cmd_link_pwr_type = -1;
160 static int hf_zbee_nwk_cmd_link_pwr_device_address = -1;
161 static int hf_zbee_nwk_cmd_link_pwr_power_delta = -1;
162 
163 /*  ZigBee Beacons */
164 static int hf_zbee_beacon_protocol = -1;
165 static int hf_zbee_beacon_stack_profile = -1;
166 static int hf_zbee_beacon_version = -1;
167 static int hf_zbee_beacon_router_capacity = -1;
168 static int hf_zbee_beacon_depth = -1;
169 static int hf_zbee_beacon_end_device_capacity = -1;
170 static int hf_zbee_beacon_epid = -1;
171 static int hf_zbee_beacon_tx_offset = -1;
172 static int hf_zbee_beacon_update_id = -1;
173 
174 static int hf_zbip_beacon_allow_join = -1;
175 static int hf_zbip_beacon_router_capacity = -1;
176 static int hf_zbip_beacon_host_capacity = -1;
177 static int hf_zbip_beacon_unsecure = -1;
178 static int hf_zbip_beacon_network_id = -1;
179 
180 /* IEEE 802.15.4 IEs (Information Elements) */
181 static int hf_ieee802154_zigbee_ie = -1;
182 static int hf_ieee802154_zigbee_ie_id = -1;
183 static int hf_ieee802154_zigbee_ie_length = -1;
184 static int hf_ieee802154_zigbee_ie_tx_power = -1;
185 static int hf_ieee802154_zigbee_ie_source_addr = -1;
186 
187 static int hf_ieee802154_zigbee_rejoin_epid = -1;
188 static int hf_ieee802154_zigbee_rejoin_source_addr = -1;
189 
190 static gint ett_zbee_nwk = -1;
191 static gint ett_zbee_nwk_beacon = -1;
192 static gint ett_zbee_nwk_fcf = -1;
193 static gint ett_zbee_nwk_fcf_ext = -1;
194 static gint ett_zbee_nwk_mcast = -1;
195 static gint ett_zbee_nwk_route = -1;
196 static gint ett_zbee_nwk_cmd = -1;
197 static gint ett_zbee_nwk_cmd_options = -1;
198 static gint ett_zbee_nwk_cmd_cinfo = -1;
199 static gint ett_zbee_nwk_cmd_link = -1;
200 static gint ett_zbee_nwk_cmd_ed_to_rsp_prnt_info = -1;
201 static gint ett_zbee_nwk_cmd_link_pwr_struct = -1;
202 static gint ett_zbee_nwk_zigbee_ie_fields = -1;
203 static gint ett_zbee_nwk_ie_rejoin = -1;
204 static gint ett_zbee_nwk_header = -1;
205 static gint ett_zbee_nwk_header_ie = -1;
206 static gint ett_zbee_nwk_beacon_bitfield = -1;
207 
208 static expert_field ei_zbee_nwk_missing_payload = EI_INIT;
209 
210 static dissector_handle_t   aps_handle;
211 static dissector_handle_t   zbee_gp_handle;
212 
213 static int zbee_nwk_address_type = -1;
214 
215 static int zbee_nwk_tap = -1;
216 
217 /********************/
218 /* Field Names      */
219 /********************/
220 /* Frame Types */
221 static const value_string zbee_nwk_frame_types[] = {
222     { ZBEE_NWK_FCF_DATA,    "Data" },
223     { ZBEE_NWK_FCF_CMD,     "Command" },
224     { ZBEE_NWK_FCF_INTERPAN,"Interpan" },
225     { 0, NULL }
226 };
227 
228 /* Route Discovery Modes */
229 static const value_string zbee_nwk_discovery_modes[] = {
230     { ZBEE_NWK_FCF_DISCOVERY_SUPPRESS,  "Suppress" },
231     { ZBEE_NWK_FCF_DISCOVERY_ENABLE,    "Enable" },
232     { ZBEE_NWK_FCF_DISCOVERY_FORCE,     "Force" },
233     { 0, NULL }
234 };
235 
236 /* Command Names*/
237 static const value_string zbee_nwk_cmd_names[] = {
238     { ZBEE_NWK_CMD_ROUTE_REQ,           "Route Request" },
239     { ZBEE_NWK_CMD_ROUTE_REPLY,         "Route Reply" },
240     { ZBEE_NWK_CMD_NWK_STATUS,          "Network Status" },
241     { ZBEE_NWK_CMD_LEAVE,               "Leave" },
242     { ZBEE_NWK_CMD_ROUTE_RECORD,        "Route Record" },
243     { ZBEE_NWK_CMD_REJOIN_REQ,          "Rejoin Request" },
244     { ZBEE_NWK_CMD_REJOIN_RESP,         "Rejoin Response" },
245     { ZBEE_NWK_CMD_LINK_STATUS,         "Link Status" },
246     { ZBEE_NWK_CMD_NWK_REPORT,          "Network Report" },
247     { ZBEE_NWK_CMD_NWK_UPDATE,          "Network Update" },
248     { ZBEE_NWK_CMD_ED_TIMEOUT_REQUEST,  "End Device Timeout Request" },
249     { ZBEE_NWK_CMD_ED_TIMEOUT_RESPONSE, "End Device Timeout Response" },
250     { ZBEE_NWK_CMD_LINK_PWR_DELTA,      "Link Power Delta" },
251     { 0, NULL }
252 };
253 
254 /* Many-To-One Route Discovery Modes. */
255 static const value_string zbee_nwk_cmd_route_many_modes[] = {
256     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_NONE,  "Not Many-to-One" },
257     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_REC,   "With Source Routing" },
258     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_NOREC, "Without Source Routing" },
259     { 0, NULL }
260 };
261 
262 /* Rejoin Status Codes */
263 static const value_string zbee_nwk_rejoin_codes[] = {
264     { IEEE802154_CMD_ASRSP_AS_SUCCESS,      "Success" },
265     { IEEE802154_CMD_ASRSP_PAN_FULL,        "PAN Full" },
266     { IEEE802154_CMD_ASRSP_PAN_DENIED,      "PAN Access Denied" },
267     { 0, NULL }
268 };
269 
270 /* Network Report Types */
271 static const value_string zbee_nwk_report_types[] = {
272     { ZBEE_NWK_CMD_NWK_REPORT_ID_PAN_CONFLICT,  "PAN Identifier Conflict" },
273     { ZBEE_NWK_CMD_NWK_REPORT_ID_ZBOSS_KEY_TRACE,  "ZBOSS key trace" },
274     { 0, NULL }
275 };
276 
277 /* Network Update Types */
278 static const value_string zbee_nwk_update_types[] = {
279     { ZBEE_NWK_CMD_NWK_UPDATE_ID_PAN_UPDATE,  "PAN Identifier Update" },
280     { 0, NULL }
281 };
282 
283 /* Network Status Codes */
284 static const value_string zbee_nwk_status_codes[] = {
285     { ZBEE_NWK_STATUS_NO_ROUTE_AVAIL,       "No Route Available" },
286     { ZBEE_NWK_STATUS_TREE_LINK_FAIL,       "Tree Link Failure" },
287     { ZBEE_NWK_STATUS_NON_TREE_LINK_FAIL,   "Non-tree Link Failure" },
288     { ZBEE_NWK_STATUS_LOW_BATTERY,          "Low Battery" },
289     { ZBEE_NWK_STATUS_NO_ROUTING,           "No Routing Capacity" },
290     { ZBEE_NWK_STATUS_NO_INDIRECT,          "No Indirect Capacity" },
291     { ZBEE_NWK_STATUS_INDIRECT_EXPIRE,      "Indirect Transaction Expiry" },
292     { ZBEE_NWK_STATUS_DEVICE_UNAVAIL,       "Target Device Unavailable" },
293     { ZBEE_NWK_STATUS_ADDR_UNAVAIL,         "Target Address Unallocated" },
294     { ZBEE_NWK_STATUS_PARENT_LINK_FAIL,     "Parent Link Failure" },
295     { ZBEE_NWK_STATUS_VALIDATE_ROUTE,       "Validate Route" },
296     { ZBEE_NWK_STATUS_SOURCE_ROUTE_FAIL,    "Source Route Failure" },
297     { ZBEE_NWK_STATUS_MANY_TO_ONE_FAIL,     "Many-to-One Route Failure" },
298     { ZBEE_NWK_STATUS_ADDRESS_CONFLICT,     "Address Conflict" },
299     { ZBEE_NWK_STATUS_VERIFY_ADDRESS,       "Verify Address" },
300     { ZBEE_NWK_STATUS_PANID_UPDATE,         "PAN ID Update" },
301     { ZBEE_NWK_STATUS_ADDRESS_UPDATE,       "Network Address Update" },
302     { ZBEE_NWK_STATUS_BAD_FRAME_COUNTER,    "Bad Frame Counter" },
303     { ZBEE_NWK_STATUS_BAD_KEY_SEQNO,        "Bad Key Sequence Number" },
304     { 0, NULL }
305 };
306 
307 /* Stack Profile Values. */
308 static const value_string zbee_nwk_stack_profiles[] = {
309     { 0x00, "Network Specific" },
310     { 0x01, "ZigBee Home" },
311     { 0x02, "ZigBee PRO" },
312     { 0, NULL }
313 };
314 
315 
316 /* ED Requested Timeout Enumerated Values */
317 static const value_string zbee_nwk_end_device_timeout_request[] = {
318     { 0, "10 sec" },
319     { 1, "2 min" },
320     { 2, "4 min" },
321     { 3, "8 min" },
322     { 4, "16 min" },
323     { 5, "32 min" },
324     { 6, "64 min" },
325     { 7, "128 min" },
326     { 8, "256 min" },
327     { 9, "512 min" },
328     { 10, "1024 min" },
329     { 11, "2048 min" },
330     { 12, "4096 min" },
331     { 13, "8192 min" },
332     { 14, "16384 min" },
333     { 0, NULL }
334 };
335 
336 
337 /* End Device Timeout Response Status Codes */
338 static const value_string zbee_nwk_end_device_timeout_resp_status[] = {
339     { 0,      "Success" },
340     { 1,      "Incorrect value" },
341     { 0, NULL }
342 };
343 
344 /* Names of IEEE 802.15.4 IEs (Information Elements) for ZigBee */
345 static const value_string ieee802154_zigbee_ie_names[] = {
346     { ZBEE_ZIGBEE_IE_REJOIN,                    "Rejoin"   },
347     { ZBEE_ZIGBEE_IE_TX_POWER,                  "Tx Power" },
348     { ZBEE_ZIGBEE_IE_BEACON_PAYLOAD,            "Extended Beacon Payload" },
349     { 0, NULL }
350 };
351 
352 /* Stack Profile Values. */
353 static const value_string zbee_nwk_link_power_delta_types[] = {
354     { 0x00, "Notification" },
355     { 0x01, "Request" },
356     { 0x02, "Response" },
357     { 0x03, "Reserved" },
358     { 0, NULL }
359 };
360 
361 /* TODO: much of the following copied from ieee80154 dissector */
362 /*-------------------------------------
363  * Hash Tables and Lists
364  *-------------------------------------
365  */
366 ieee802154_map_tab_t zbee_nwk_map = { NULL, NULL };
367 GHashTable *zbee_table_nwk_keyring = NULL;
368 GHashTable *zbee_table_link_keyring = NULL;
369 
zbee_nwk_address_to_str(const address * addr,gchar * buf,int buf_len)370 static int zbee_nwk_address_to_str(const address* addr, gchar *buf, int buf_len)
371 {
372     guint16 zbee_nwk_addr = pletoh16(addr->data);
373 
374     if ((zbee_nwk_addr == ZBEE_BCAST_ALL) || (zbee_nwk_addr == ZBEE_BCAST_ACTIVE) || (zbee_nwk_addr == ZBEE_BCAST_ROUTERS)) {
375         return (int)g_strlcpy(buf, "Broadcast", buf_len) + 1;
376     }
377     else {
378         return g_snprintf(buf, buf_len, "0x%04x", zbee_nwk_addr) + 1;
379     }
380 }
381 
zbee_nwk_address_str_len(const address * addr _U_)382 static int zbee_nwk_address_str_len(const address* addr _U_)
383 {
384     return sizeof("Broadcast");
385 }
386 
zbee_nwk_address_len(void)387 static int zbee_nwk_address_len(void)
388 {
389     return sizeof(guint16);
390 }
391 
392 /**
393  *Extracts an integer sub-field from an int with a given mask
394  *
395 */
396 guint
zbee_get_bit_field(guint input,guint mask)397 zbee_get_bit_field(guint input, guint mask)
398 {
399     /* Sanity Check, don't want infinite loops. */
400     if (mask == 0) return 0;
401     /* Shift input and mask together. */
402     while (!(mask & 0x1)) {
403         input >>= 1;
404         mask >>=1;
405     } /* while */
406     return (input & mask);
407 } /* zbee_get_bit_field */
408 
409 /**
410  *Heuristic interpreter for the ZigBee network dissectors.
411  *
412  *@param tvb pointer to buffer containing raw packet.
413  *@param pinfo pointer to packet information fields
414  *@param tree pointer to data tree Wireshark uses to display packet.
415  *@return Boolean value, whether it handles the packet or not.
416 */
417 static gboolean
dissect_zbee_nwk_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)418 dissect_zbee_nwk_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
419 {
420     ieee802154_packet   *packet = (ieee802154_packet *)data;
421     guint16             fcf;
422     guint               ver;
423     guint               type;
424 
425     /* All ZigBee frames must always have a 16-bit source and destination address. */
426     if (packet == NULL) return FALSE;
427 
428     /* If the frame type and version are not sane, then it's probably not ZigBee. */
429     fcf = tvb_get_letohs(tvb, 0);
430     ver = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_VERSION);
431     type = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_FRAME_TYPE);
432     if ((ver < ZBEE_VERSION_2004) || (ver > ZBEE_VERSION_2007)) return FALSE;
433     if (!try_val_to_str(type, zbee_nwk_frame_types)) return FALSE;
434 
435     /* All interpan frames should originate from an extended address. */
436     if (type == ZBEE_NWK_FCF_INTERPAN) {
437         if (packet->src_addr_mode != IEEE802154_FCF_ADDR_EXT) return FALSE;
438     }
439     /* All other ZigBee frames must have 16-bit source and destination addresses. */
440     else {
441         if (packet->src_addr_mode != IEEE802154_FCF_ADDR_SHORT) return FALSE;
442         if (packet->dst_addr_mode != IEEE802154_FCF_ADDR_SHORT) return FALSE;
443     }
444 
445     /* Assume it's ZigBee */
446     dissect_zbee_nwk(tvb, pinfo, tree, packet);
447     return TRUE;
448 } /* dissect_zbee_heur */
449 
450 /**
451  *ZigBee NWK packet dissection routine for 2006, 2007 and Pro stack versions.
452  *
453  *@param tvb pointer to buffer containing raw packet.
454  *@param pinfo pointer to packet information fields.
455  *@param tree pointer to data tree Wireshark uses to display packet.
456  *@param data raw packet private data.
457 */
458 
459 static int
dissect_zbee_nwk_full(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)460 dissect_zbee_nwk_full(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
461 {
462     tvbuff_t            *payload_tvb = NULL;
463 
464     proto_item          *proto_root;
465     proto_item          *ti = NULL;
466     proto_tree          *nwk_tree;
467 
468     zbee_nwk_packet     packet;
469     ieee802154_packet   *ieee_packet;
470 
471     guint               offset = 0;
472     gchar               *src_addr, *dst_addr;
473 
474     guint16             fcf;
475 
476     ieee802154_short_addr   addr16;
477     ieee802154_map_rec     *map_rec;
478     ieee802154_hints_t     *ieee_hints;
479 
480     zbee_nwk_hints_t       *nwk_hints;
481     gboolean                unicast_src;
482 
483     static int * const fcf_flags_2007[] = {
484         &hf_zbee_nwk_frame_type,
485         &hf_zbee_nwk_proto_version,
486         &hf_zbee_nwk_discover_route,
487         &hf_zbee_nwk_multicast,
488         &hf_zbee_nwk_security,
489         &hf_zbee_nwk_source_route,
490         &hf_zbee_nwk_ext_dst,
491         &hf_zbee_nwk_ext_src,
492         &hf_zbee_nwk_end_device_initiator,
493         NULL
494     };
495 
496     static int * const fcf_flags[] = {
497         &hf_zbee_nwk_frame_type,
498         &hf_zbee_nwk_proto_version,
499         &hf_zbee_nwk_discover_route,
500         &hf_zbee_nwk_security,
501         NULL
502     };
503 
504     /* Reject the packet if data is NULL */
505     if (data == NULL)
506         return 0;
507     ieee_packet = (ieee802154_packet *)data;
508 
509     memset(&packet, 0, sizeof(packet));
510 
511     /* Set up hint structures */
512     if (!pinfo->fd->visited) {
513         /* Allocate frame data with hints for upper layers */
514         nwk_hints = wmem_new0(wmem_file_scope(), zbee_nwk_hints_t);
515         p_add_proto_data(wmem_file_scope(), pinfo, proto_zbee_nwk, 0, nwk_hints);
516     } else {
517         /* Retrieve existing structure */
518         nwk_hints = (zbee_nwk_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto_zbee_nwk, 0);
519     }
520 
521     ieee_hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
522             proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);
523 
524     /* Add ourself to the protocol column, clear the info column, and create the protocol tree. */
525     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZigBee");
526     col_clear(pinfo->cinfo, COL_INFO);
527     proto_root = proto_tree_add_item(tree, proto_zbee_nwk, tvb, offset, -1, ENC_NA);
528     nwk_tree = proto_item_add_subtree(proto_root, ett_zbee_nwk);
529 
530     /* Get and parse the FCF */
531     fcf = tvb_get_letohs(tvb, offset);
532     packet.type         = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_FRAME_TYPE);
533     packet.version      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_VERSION);
534     packet.discovery    = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_DISCOVER_ROUTE);
535     packet.security     = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_SECURITY);
536     packet.multicast    = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_MULTICAST);
537     packet.route        = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_SOURCE_ROUTE);
538     packet.ext_dst      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_EXT_DEST);
539     packet.ext_src      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_EXT_SOURCE);
540 
541     /* Display the FCF. */
542     if (packet.version >= ZBEE_VERSION_2007) {
543         ti = proto_tree_add_bitmask(nwk_tree, tvb, offset, hf_zbee_nwk_fcf, ett_zbee_nwk_fcf, fcf_flags_2007, ENC_LITTLE_ENDIAN);
544     } else {
545         ti = proto_tree_add_bitmask(nwk_tree, tvb, offset, hf_zbee_nwk_fcf, ett_zbee_nwk_fcf, fcf_flags, ENC_LITTLE_ENDIAN);
546     }
547     proto_item_append_text(ti, " %s", val_to_str_const(packet.type, zbee_nwk_frame_types, "Unknown"));
548     offset += 2;
549 
550     /* Add the frame type to the info column and protocol root. */
551     proto_item_append_text(proto_root, " %s", val_to_str_const(packet.type, zbee_nwk_frame_types, "Unknown Type"));
552     col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet.type, zbee_nwk_frame_types, "Reserved Frame Type"));
553 
554     if (packet.type != ZBEE_NWK_FCF_INTERPAN) {
555         /* Get the destination address. */
556         packet.dst = tvb_get_letohs(tvb, offset);
557 
558         set_address_tvb(&pinfo->net_dst, zbee_nwk_address_type, 2, tvb, offset);
559         copy_address_shallow(&pinfo->dst, &pinfo->net_dst);
560         dst_addr = address_to_str(pinfo->pool, &pinfo->dst);
561 
562         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_dst, tvb, offset, 2, packet.dst);
563         ti = proto_tree_add_uint(nwk_tree, hf_zbee_nwk_addr, tvb, offset, 2, packet.dst);
564         proto_item_set_generated(ti);
565         proto_item_set_hidden(ti);
566         offset += 2;
567 
568         proto_item_append_text(proto_root, ", Dst: %s", dst_addr);
569         col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: %s", dst_addr);
570 
571         /* Get the short nwk source address and pass it to upper layers */
572         packet.src = tvb_get_letohs(tvb, offset);
573 
574         set_address_tvb(&pinfo->net_src, zbee_nwk_address_type, 2, tvb, offset);
575         copy_address_shallow(&pinfo->src, &pinfo->net_src);
576         src_addr = address_to_str(pinfo->pool, &pinfo->src);
577 
578         if (nwk_hints)
579             nwk_hints->src = packet.src;
580         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_src, tvb, offset, 2, packet.src);
581         ti = proto_tree_add_uint(nwk_tree, hf_zbee_nwk_addr, tvb, offset, 2, packet.src);
582         proto_item_set_generated(ti);
583         proto_item_set_hidden(ti);
584         offset += 2;
585 
586         if (   (packet.src == ZBEE_BCAST_ALL)
587                || (packet.src == ZBEE_BCAST_ACTIVE)
588                || (packet.src == ZBEE_BCAST_ROUTERS)){
589             /* Source Broadcast doesn't make much sense. */
590             unicast_src = FALSE;
591         }
592         else {
593             unicast_src = TRUE;
594         }
595 
596         proto_item_append_text(proto_root, ", Src: %s", src_addr);
597         col_append_fstr(pinfo->cinfo, COL_INFO, ", Src: %s", src_addr);
598 
599         /* Get and display the radius. */
600         packet.radius = tvb_get_guint8(tvb, offset);
601         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_radius, tvb, offset, 1, packet.radius);
602         offset += 1;
603 
604         /* Get and display the sequence number. */
605         packet.seqno = tvb_get_guint8(tvb, offset);
606         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_seqno, tvb, offset, 1, packet.seqno);
607         offset += 1;
608 
609         /* Add the extended destination address (ZigBee 2006 and later). */
610         if ((packet.version >= ZBEE_VERSION_2007) && packet.ext_dst) {
611             packet.dst64 = tvb_get_letoh64(tvb, offset);
612             proto_tree_add_item(nwk_tree, hf_zbee_nwk_dst64, tvb, offset, 8, ENC_LITTLE_ENDIAN);
613             ti = proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_addr64, tvb, offset, 8, packet.dst64);
614             proto_item_set_generated(ti);
615             proto_item_set_hidden(ti);
616             offset += 8;
617         }
618 
619         /* Display the extended source address. (ZigBee 2006 and later). */
620         if (packet.version >= ZBEE_VERSION_2007) {
621             addr16.pan = ieee_packet->src_pan;
622 
623             if (packet.ext_src) {
624                 packet.src64 = tvb_get_letoh64(tvb, offset);
625                 proto_tree_add_item(nwk_tree, hf_zbee_nwk_src64, tvb, offset, 8, ENC_LITTLE_ENDIAN);
626                 ti = proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_addr64, tvb, offset, 8, packet.src64);
627                 proto_item_set_generated(ti);
628                 proto_item_set_hidden(ti);
629                 offset += 8;
630 
631                 if (!pinfo->fd->visited && nwk_hints) {
632                     /* Provide hints to upper layers */
633                     nwk_hints->src_pan = ieee_packet->src_pan;
634 
635                     /* Update nwk extended address hash table */
636                     if ( unicast_src ) {
637                         nwk_hints->map_rec = ieee802154_addr_update(&zbee_nwk_map,
638                                                                     packet.src, addr16.pan, packet.src64, pinfo->current_proto, pinfo->num);
639                     }
640                 }
641             }
642             else {
643                 /* See if extended source info was previously sniffed */
644                 if (!pinfo->fd->visited && nwk_hints) {
645                     nwk_hints->src_pan = ieee_packet->src_pan;
646                     addr16.addr = packet.src;
647 
648                     map_rec = (ieee802154_map_rec *) g_hash_table_lookup(zbee_nwk_map.short_table, &addr16);
649                     if (map_rec) {
650                         /* found a nwk mapping record */
651                         nwk_hints->map_rec = map_rec;
652                     }
653                     else {
654                         /* does ieee layer know? */
655                         map_rec = (ieee802154_map_rec *) g_hash_table_lookup(ieee_packet->short_table, &addr16);
656                         if (map_rec) nwk_hints->map_rec = map_rec;
657                     }
658                 } /* (!pinfo->fd->visited) */
659                 else {
660                     if (nwk_hints && nwk_hints->map_rec ) {
661                         /* Display inferred source address info */
662                         ti = proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_src64, tvb, offset, 0,
663                                                   nwk_hints->map_rec->addr64);
664                         proto_item_set_generated(ti);
665                         ti = proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_addr64, tvb, offset, 0, nwk_hints->map_rec->addr64);
666                         proto_item_set_generated(ti);
667                         proto_item_set_hidden(ti);
668 
669                         if ( nwk_hints->map_rec->start_fnum ) {
670                             ti = proto_tree_add_uint(nwk_tree, hf_zbee_nwk_src64_origin, tvb, 0, 0,
671                                                      nwk_hints->map_rec->start_fnum);
672                         }
673                         else {
674                             ti = proto_tree_add_uint_format_value(nwk_tree, hf_zbee_nwk_src64_origin, tvb, 0, 0, 0, "Pre-configured");
675                         }
676                         proto_item_set_generated(ti);
677                     }
678                 }
679             }
680 
681             /* If ieee layer didn't know its extended source address, and nwk layer does, fill it in */
682             if (!pinfo->fd->visited) {
683                 if ( (ieee_packet->src_addr_mode == IEEE802154_FCF_ADDR_SHORT) &&
684                      ieee_hints && !ieee_hints->map_rec ) {
685                     addr16.pan = ieee_packet->src_pan;
686                     addr16.addr = ieee_packet->src16;
687                     map_rec = (ieee802154_map_rec *) g_hash_table_lookup(zbee_nwk_map.short_table, &addr16);
688 
689                     if (map_rec) {
690                         /* found a ieee mapping record */
691                         ieee_hints->map_rec = map_rec;
692                     }
693                 }
694             } /* (!pinfo->fd->visited */
695         } /* (pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) */
696 
697         /* Add multicast control field (ZigBee 2006 and later). */
698         if ((packet.version >= ZBEE_VERSION_2007) && packet.multicast) {
699             static int * const multicast_flags[] = {
700                 &hf_zbee_nwk_mcast_mode,
701                 &hf_zbee_nwk_mcast_radius,
702                 &hf_zbee_nwk_mcast_max_radius,
703                 NULL
704             };
705 
706             guint8 mcast_control = tvb_get_guint8(tvb, offset);
707             packet.mcast_mode = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_MODE);
708             packet.mcast_radius = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_RADIUS);
709             packet.mcast_max_radius = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_MAX_RADIUS);
710 
711             proto_tree_add_bitmask(nwk_tree, tvb, offset, hf_zbee_nwk_mcast, ett_zbee_nwk_mcast, multicast_flags, ENC_NA);
712             offset += 1;
713         }
714 
715         /* Add the Source Route field. (ZigBee 2006 and later). */
716         if ((packet.version >= ZBEE_VERSION_2007) && packet.route) {
717             proto_tree *field_tree;
718             guint8      relay_count;
719             guint16     relay_addr;
720             guint       i;
721 
722             /* Create a subtree for the source route field. */
723             field_tree = proto_tree_add_subtree(nwk_tree, tvb, offset, 1, ett_zbee_nwk_route, &ti, "Source Route");
724 
725             /* Get and display the relay count. */
726             relay_count = tvb_get_guint8(tvb, offset);
727             proto_tree_add_uint(field_tree, hf_zbee_nwk_relay_count, tvb, offset, 1, relay_count);
728             proto_item_append_text(ti, ", Length: %d", relay_count);
729             offset += 1;
730 
731             /* Correct the length of the source route fields. */
732             proto_item_set_len(ti, 1 + relay_count*2);
733 
734             /* Get and display the relay index. */
735             proto_tree_add_item(field_tree, hf_zbee_nwk_relay_index, tvb, offset, 1, ENC_NA);
736             offset += 1;
737 
738             /* Get and display the relay list. */
739             for (i=0; i<relay_count; i++) {
740                 relay_addr = tvb_get_letohs(tvb, offset);
741                 proto_tree_add_uint_format(field_tree, hf_zbee_nwk_relay, tvb, offset, 2, relay_addr, "Relay %d: 0x%04x", i+1, relay_addr);
742                 offset += 2;
743             } /* for */
744         }
745     } /* if not interpan */
746 
747     /*
748      * Ensure that the payload exists. There are no valid ZigBee network
749      * packets that have no payload.
750      */
751     if (offset >= tvb_captured_length(tvb)) {
752         /* Non-existent or truncated payload. */
753         expert_add_info(pinfo, proto_root, &ei_zbee_nwk_missing_payload);
754         return tvb_captured_length(tvb);
755     }
756     /* Payload is encrypted, attempt security operations. */
757     else if (packet.security) {
758         payload_tvb = dissect_zbee_secure(tvb, pinfo, nwk_tree, offset);
759         if (payload_tvb == NULL) {
760             /* If Payload_tvb is NULL, then the security dissector cleaned up. */
761             return tvb_captured_length(tvb);
762         }
763     }
764     /* Plaintext payload. */
765     else {
766         payload_tvb = tvb_new_subset_remaining(tvb, offset);
767     }
768 
769     if (packet.type == ZBEE_NWK_FCF_CMD) {
770         /* Dissect the Network Command. */
771         dissect_zbee_nwk_cmd(payload_tvb, pinfo, nwk_tree, &packet);
772     }
773     else if (packet.type == ZBEE_NWK_FCF_DATA || packet.type == ZBEE_NWK_FCF_INTERPAN) {
774         /* Dissect the Network Payload (APS layer). */
775         call_dissector_with_data(aps_handle, payload_tvb, pinfo, tree, &packet);
776     }
777     else {
778         /* Invalid type. */
779         call_data_dissector(payload_tvb, pinfo, tree);
780     }
781 
782     tap_queue_packet(zbee_nwk_tap, pinfo, NULL);
783 
784     return tvb_captured_length(tvb);
785 } /* dissect_zbee_nwk */
786 
787 /**
788  *ZigBee packet dissection with proto version determination.
789  *
790  *@param tvb pointer to buffer containing raw packet.
791  *@param pinfo pointer to packet information fields.
792  *@param tree pointer to data tree Wireshark uses to display packet.
793  *@param data raw packet private data.
794 */
795 static int
dissect_zbee_nwk(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)796 dissect_zbee_nwk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
797 {
798     guint8 fcf0;
799     guint8 proto_version;
800 
801     fcf0 = tvb_get_guint8(tvb, 0);
802     proto_version = (fcf0 & ZBEE_NWK_FCF_VERSION) >> 2;
803     if (proto_version == ZBEE_VERSION_GREEN_POWER) {
804         call_dissector(zbee_gp_handle, tvb, pinfo, tree);
805     } else {
806         /* TODO: add check for FCF proto versions. */
807         dissect_zbee_nwk_full(tvb, pinfo, tree, data);
808     }
809     return tvb_captured_length(tvb);
810 }
811 
812 /**
813  *ZigBee Network command packet dissection routine for Wireshark.
814  *
815  *@param tvb pointer to buffer containing raw packet.
816  *@param pinfo pointer to packet information fields
817  *@param tree pointer to data tree Wireshark uses to display packet.
818 */
dissect_zbee_nwk_cmd(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,zbee_nwk_packet * packet)819 static void dissect_zbee_nwk_cmd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet* packet)
820 {
821     proto_tree  *cmd_tree;
822     proto_item  *cmd_root;
823 
824     guint       offset=0;
825     guint8      cmd_id = tvb_get_guint8(tvb, offset);
826 
827     /* Create a subtree for this command. */
828     cmd_tree = proto_tree_add_subtree_format(tree, tvb, offset, -1, ett_zbee_nwk_cmd, &cmd_root, "Command Frame: %s",
829                                     val_to_str_const(cmd_id, zbee_nwk_cmd_names, "Unknown"));
830 
831     /* Add the command ID. */
832     proto_tree_add_uint(cmd_tree, hf_zbee_nwk_cmd_id, tvb, offset, 1, cmd_id);
833     offset += 1;
834 
835     /* Add the command name to the info column. */
836     col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(cmd_id, zbee_nwk_cmd_names, "Unknown Command"));
837 
838 
839     /* Handle the command. */
840     switch(cmd_id){
841         case ZBEE_NWK_CMD_ROUTE_REQ:
842             /* Route Request Command. */
843             offset = dissect_zbee_nwk_route_req(tvb, pinfo, cmd_tree, packet, offset);
844             break;
845 
846         case ZBEE_NWK_CMD_ROUTE_REPLY:
847             /* Route Reply Command. */
848             offset = dissect_zbee_nwk_route_rep(tvb, pinfo, cmd_tree, offset, packet->version);
849             break;
850 
851         case ZBEE_NWK_CMD_NWK_STATUS:
852             /* Network Status Command. */
853             offset = dissect_zbee_nwk_status(tvb, pinfo, cmd_tree, offset);
854             break;
855 
856         case ZBEE_NWK_CMD_LEAVE:
857             /* Leave Command. */
858             offset = dissect_zbee_nwk_leave(tvb, cmd_tree, offset);
859             break;
860 
861         case ZBEE_NWK_CMD_ROUTE_RECORD:
862             /* Route Record Command. */
863             offset = dissect_zbee_nwk_route_rec(tvb, pinfo, cmd_tree, packet, offset);
864             break;
865 
866         case ZBEE_NWK_CMD_REJOIN_REQ:
867             /* Rejoin Request Command. */
868             offset = dissect_zbee_nwk_rejoin_req(tvb, pinfo, cmd_tree, packet, offset);
869             break;
870 
871         case ZBEE_NWK_CMD_REJOIN_RESP:
872             /* Rejoin Response Command. */
873             offset = dissect_zbee_nwk_rejoin_resp(tvb, pinfo, cmd_tree, packet, offset);
874             break;
875 
876         case ZBEE_NWK_CMD_LINK_STATUS:
877             /* Link Status Command. */
878             offset = dissect_zbee_nwk_link_status(tvb, cmd_tree, offset);
879             break;
880 
881         case ZBEE_NWK_CMD_NWK_REPORT:
882             /* Network Report Command. */
883             offset = dissect_zbee_nwk_report(tvb, pinfo, cmd_tree, offset);
884             break;
885 
886         case ZBEE_NWK_CMD_NWK_UPDATE:
887             /* Network Update Command. */
888             offset = dissect_zbee_nwk_update(tvb, pinfo, cmd_tree, offset);
889             break;
890 
891         case ZBEE_NWK_CMD_ED_TIMEOUT_REQUEST:
892             /* Network End Device Timeout Request Command. */
893             offset = dissect_zbee_nwk_ed_timeout_request(tvb, cmd_tree, offset);
894             break;
895 
896         case ZBEE_NWK_CMD_ED_TIMEOUT_RESPONSE:
897             /* Network End Device Timeout Response Command. */
898             offset = dissect_zbee_nwk_ed_timeout_response(tvb, pinfo, cmd_tree, offset);
899             break;
900 
901         case ZBEE_NWK_CMD_LINK_PWR_DELTA:
902             offset = dissect_zbee_nwk_link_pwr_delta(tvb, pinfo, cmd_tree, offset);
903             break;
904 
905         default:
906             /* Just break out and let the overflow handler deal with the payload. */
907             break;
908     } /* switch */
909 
910     /* There is excess data in the packet. */
911     if (offset < tvb_captured_length(tvb)) {
912         /* There are leftover bytes! */
913         tvbuff_t    *leftover_tvb   = tvb_new_subset_remaining(tvb, offset);
914         proto_tree  *root;
915 
916         /* Correct the length of the command tree. */
917         root = proto_tree_get_root(tree);
918         proto_item_set_len(cmd_root, offset);
919 
920         /* Dump the leftover to the data dissector. */
921         call_data_dissector(leftover_tvb, pinfo, root);
922     }
923 } /* dissect_zbee_nwk_cmd */
924 
925 /**
926  *Helper dissector for the Route Request command.
927  *
928  *@param tvb pointer to buffer containing raw packet.
929  *@param pinfo pointer to packet information fields
930  *@param tree pointer to the command subtree.
931  *@param packet pointer to the network packet struct.
932  *@param  offset into the tvb to begin dissection.
933  *@return offset after command dissection.
934 */
935 static guint
dissect_zbee_nwk_route_req(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,zbee_nwk_packet * packet,guint offset)936 dissect_zbee_nwk_route_req(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
937 {
938     guint8  route_options;
939     guint16 dest_addr;
940 
941     static int * const nwk_route_command_options_2007[] = {
942         &hf_zbee_nwk_cmd_route_opt_multicast,
943         &hf_zbee_nwk_cmd_route_opt_dest_ext,
944         &hf_zbee_nwk_cmd_route_opt_many_to_one,
945         NULL
946     };
947 
948     static int * const nwk_route_command_options[] = {
949         &hf_zbee_nwk_cmd_route_opt_repair,
950         NULL
951     };
952 
953     /* Get and display the route options field. */
954     route_options = tvb_get_guint8(tvb, offset);
955     if (packet->version >= ZBEE_VERSION_2007) {
956         proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_route_options, ett_zbee_nwk_cmd_options, nwk_route_command_options_2007, ENC_NA);
957     } else {
958         proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_route_options, ett_zbee_nwk_cmd_options, nwk_route_command_options, ENC_NA);
959     }
960     offset += 1;
961 
962     /* Get and display the route request ID. */
963     proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_id, tvb, offset, 1, ENC_NA);
964     offset += 1;
965 
966     /* Get and display the destination address. */
967     dest_addr = tvb_get_letohs(tvb, offset);
968     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_dest, tvb, offset, 2, dest_addr);
969     offset += 2;
970 
971     /* Get and display the path cost. */
972     proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_cost, tvb, offset, 1, ENC_NA);
973     offset += 1;
974 
975     /* Get and display the extended destination address. */
976     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_DEST_EXT) {
977         proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_dest_ext, tvb, offset, 8, ENC_LITTLE_ENDIAN);
978         offset += 8;
979     }
980 
981     /* Update the info column. */
982     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_MANY_MASK) {
983         col_clear(pinfo->cinfo, COL_INFO);
984         col_append_fstr(pinfo->cinfo, COL_INFO, "Many-to-One Route Request");
985     }
986     col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: 0x%04x, Src: 0x%04x", dest_addr, packet->src);
987 
988     /* Done */
989     return offset;
990 } /* dissect_zbee_nwk_route_req */
991 
992 /**
993  *Helper dissector for the Route Reply command.
994  *
995  *@param tvb pointer to buffer containing raw packet.
996  *@param pinfo pointer to packet information fields
997  *@param tree pointer to the command subtree.
998  *@param  offset into the tvb to begin dissection.
999  *@return offset after command dissection.
1000 */
1001 static guint
dissect_zbee_nwk_route_rep(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,guint offset,guint8 version)1002 dissect_zbee_nwk_route_rep(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, guint8 version)
1003 {
1004     guint8  route_options;
1005     guint16 orig_addr;
1006     guint16 resp_addr;
1007 
1008     static int * const nwk_route_command_options_2007[] = {
1009         &hf_zbee_nwk_cmd_route_opt_multicast,
1010         &hf_zbee_nwk_cmd_route_opt_resp_ext,
1011         &hf_zbee_nwk_cmd_route_opt_orig_ext,
1012         NULL
1013     };
1014 
1015     static int * const nwk_route_command_options[] = {
1016         &hf_zbee_nwk_cmd_route_opt_repair,
1017         NULL
1018     };
1019 
1020     /* Get and display the route options field. */
1021     route_options = tvb_get_guint8(tvb, offset);
1022     if (version >= ZBEE_VERSION_2007) {
1023         proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_route_options, ett_zbee_nwk_cmd_options, nwk_route_command_options_2007, ENC_NA);
1024     } else {
1025         proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_route_options, ett_zbee_nwk_cmd_options, nwk_route_command_options, ENC_NA);
1026     }
1027     offset += 1;
1028 
1029     /* Get and display the route request ID. */
1030     proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_id, tvb, offset, 1, ENC_NA);
1031     offset += 1;
1032 
1033     /* Get and display the originator address. */
1034     orig_addr = tvb_get_letohs(tvb, offset);
1035     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_orig, tvb, offset, 2, orig_addr);
1036     offset += 2;
1037 
1038     /* Get and display the responder address. */
1039     resp_addr = tvb_get_letohs(tvb, offset);
1040     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_resp, tvb, offset, 2, resp_addr);
1041     offset += 2;
1042 
1043     /* Get and display the path cost. */
1044     proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_cost, tvb, offset, 1, ENC_NA);
1045     offset += 1;
1046 
1047     /* Get and display the originator extended address. */
1048     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_ORIG_EXT) {
1049         proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_orig_ext, tvb, offset, 8, ENC_LITTLE_ENDIAN);
1050         offset += 8;
1051     }
1052 
1053     /* Get and display the responder extended address. */
1054     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_RESP_EXT) {
1055         proto_tree_add_item(tree, hf_zbee_nwk_cmd_route_resp_ext, tvb, offset, 8, ENC_LITTLE_ENDIAN);
1056         offset += 8;
1057     }
1058 
1059     /* Update the info column. */
1060     col_append_fstr(pinfo->cinfo, COL_INFO, ", Responder: 0x%04x, Originator: 0x%04x", resp_addr, orig_addr);
1061 
1062     /* Done */
1063     return offset;
1064 } /* dissect_zbee_nwk_route_rep */
1065 
1066 /**
1067  *Helper dissector for the Network Status command.
1068  *
1069  *@param tvb pointer to buffer containing raw packet.
1070  *@param pinfo pointer to packet information fields
1071  *@param tree pointer to the command subtree.
1072  *@param  offset into the tvb to begin dissection.
1073  *@return offset after command dissection.
1074 */
1075 static guint
dissect_zbee_nwk_status(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,guint offset)1076 dissect_zbee_nwk_status(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1077 {
1078     guint8  status_code;
1079     guint8  command_id;
1080     guint16 addr;
1081 
1082     /* Get and display the status code. */
1083     status_code = tvb_get_guint8(tvb, offset);
1084     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_nwk_status, tvb, offset, 1, status_code);
1085     offset += 1;
1086 
1087     /* Get and display the destination address. */
1088     addr = tvb_get_letohs(tvb, offset);
1089     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_dest, tvb, offset, 2, addr);
1090     offset += 2;
1091 
1092     /* Update the info column. */
1093     col_append_fstr(pinfo->cinfo, COL_INFO, ", 0x%04x: %s", addr, val_to_str_const(status_code, zbee_nwk_status_codes, "Unknown Status Code"));
1094 
1095     if (status_code == ZBEE_NWK_STATUS_UNKNOWN_COMMAND) {
1096         command_id = tvb_get_guint8(tvb, offset);
1097         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_nwk_status_command_id, tvb, offset, 1, command_id);
1098         col_append_fstr(pinfo->cinfo, COL_INFO, ", Unknown Command ID 0x%02x (%s)", command_id,
1099                 val_to_str_const(command_id, zbee_nwk_cmd_names, "Unknown ID"));
1100         offset++;
1101     }
1102 
1103     /* Done */
1104     return offset;
1105 } /* dissect_zbee_nwk_status */
1106 
1107 /**
1108  *Helper dissector for the Leave command.
1109  *
1110  *@param tvb pointer to buffer containing raw packet.
1111  *@param tree pointer to the command subtree.
1112  *@param  offset into the tvb to begin dissection.
1113  *@return offset after command dissection.
1114 */
1115 static guint
dissect_zbee_nwk_leave(tvbuff_t * tvb,proto_tree * tree,guint offset)1116 dissect_zbee_nwk_leave(tvbuff_t *tvb, proto_tree *tree, guint offset)
1117 {
1118     static int * const leave_options[] = {
1119         &hf_zbee_nwk_cmd_leave_rejoin,
1120         &hf_zbee_nwk_cmd_leave_request,
1121         &hf_zbee_nwk_cmd_leave_children,
1122         NULL
1123     };
1124 
1125     /* Get and display the leave options. */
1126     proto_tree_add_bitmask_list(tree, tvb, offset, 1, leave_options, ENC_NA);
1127     offset += 1;
1128 
1129     /* Done */
1130     return offset;
1131 } /* dissect_zbee_nwk_leave */
1132 
1133 /**
1134  *Helper dissector for the Route Record command.
1135  *
1136  *@param tvb pointer to buffer containing raw packet.
1137  *@param pinfo pointer to packet information fields
1138  *@param tree pointer to the command subtree.
1139  *@param packet pointer to the network packet struct.
1140  *@param  offset into the tvb to begin dissection.
1141  *@return offset after command dissection.
1142 */
1143 static guint
dissect_zbee_nwk_route_rec(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,zbee_nwk_packet * packet,guint offset)1144 dissect_zbee_nwk_route_rec(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
1145 {
1146     guint8  relay_count;
1147     guint16 relay_addr;
1148     guint   i;
1149 
1150     /* Get and display the relay count. */
1151     relay_count = tvb_get_guint8(tvb, offset);
1152     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_relay_count, tvb, offset, 1, relay_count);
1153     offset += 1;
1154 
1155     /* Get and display the relay addresses. */
1156     for (i=0; i<relay_count; i++) {
1157         relay_addr = tvb_get_letohs(tvb, offset);
1158         proto_tree_add_uint_format(tree, hf_zbee_nwk_cmd_relay_device, tvb, offset, 2, relay_addr,
1159                                    "Relay Device %d: 0x%04x", i+1, relay_addr);
1160         offset += 2;
1161     } /* for */
1162 
1163     /* Update the info column. */
1164     col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: 0x%04x", packet->dst);
1165 
1166 
1167     /* Done */
1168     return offset;
1169 } /* dissect_zbee_nwk_route_rec */
1170 
1171 /**
1172  *Helper dissector for the Rejoin Request command.
1173  *
1174  *@param tvb pointer to buffer containing raw packet.
1175  *@param pinfo pointer to packet information fields
1176  *@param tree pointer to the command subtree.
1177  *@param packet pointer to the network packet struct.
1178  *@param  offset into the tvb to begin dissection.
1179  *@return offset after command dissection.
1180 */
1181 static guint
dissect_zbee_nwk_rejoin_req(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,zbee_nwk_packet * packet,guint offset)1182 dissect_zbee_nwk_rejoin_req(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
1183 {
1184     static int * const capabilities[] = {
1185         &hf_zbee_nwk_cmd_cinfo_alt_coord,
1186         &hf_zbee_nwk_cmd_cinfo_type,
1187         &hf_zbee_nwk_cmd_cinfo_power,
1188         &hf_zbee_nwk_cmd_cinfo_idle_rx,
1189         &hf_zbee_nwk_cmd_cinfo_security,
1190         &hf_zbee_nwk_cmd_cinfo_alloc,
1191         NULL
1192     };
1193 
1194     proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_cinfo, ett_zbee_nwk_cmd_cinfo, capabilities, ENC_NA);
1195     offset += 1;
1196 
1197     /* Update the info column.*/
1198     col_append_fstr(pinfo->cinfo, COL_INFO, ", Device: 0x%04x", packet->src);
1199 
1200     /* Done */
1201     return offset;
1202 } /* dissect_zbee_nwk_rejoin_req */
1203 
1204 /**
1205  *Helper dissector for the Rejoin Response command.
1206  *
1207  *@param tvb pointer to buffer containing raw packet.
1208  *@param pinfo pointer to packet information fields
1209  *@param tree pointer to the command subtree.
1210  *@param packet pointer to the network packet struct.
1211  *@param  offset into the tvb to begin dissection.
1212  *@return offset after command dissection.
1213 */
1214 static guint
dissect_zbee_nwk_rejoin_resp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,zbee_nwk_packet * packet _U_,guint offset)1215 dissect_zbee_nwk_rejoin_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet _U_, guint offset)
1216 {
1217     guint8  status;
1218     guint16 new_address;
1219 
1220     /* Get and display the short address. */
1221     new_address = tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN);
1222     proto_tree_add_item(tree, hf_zbee_nwk_cmd_addr, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1223     offset += 2;
1224 
1225     /* Get and display the rejoin status. */
1226     status = tvb_get_guint8(tvb, offset);
1227     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_rejoin_status, tvb, offset, 1, status);
1228     offset += 1;
1229 
1230     /* Update the info column. */
1231     if (status == IEEE802154_CMD_ASRSP_AS_SUCCESS) {
1232         col_append_fstr(pinfo->cinfo, COL_INFO, ", New Address: 0x%04x", new_address);
1233     }
1234     else {
1235         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(status, zbee_nwk_rejoin_codes, "Unknown Rejoin Response"));
1236     }
1237 
1238     /* Done */
1239     return offset;
1240 } /* dissect_zbee_nwk_rejoin_resp */
1241 
1242 /**
1243  *Helper dissector for the Link Status command.
1244  *
1245  *@param tvb pointer to buffer containing raw packet.
1246  *@param tree pointer to the command subtree.
1247  *@param  offset into the tvb to begin dissection.
1248  *@return offset after command dissection.
1249 */
1250 static guint
dissect_zbee_nwk_link_status(tvbuff_t * tvb,proto_tree * tree,guint offset)1251 dissect_zbee_nwk_link_status(tvbuff_t *tvb, proto_tree *tree, guint offset)
1252 {
1253     guint8  options;
1254     int     i, link_count;
1255     proto_tree *subtree;
1256     static int * const link_options[] = {
1257         &hf_zbee_nwk_cmd_link_last,
1258         &hf_zbee_nwk_cmd_link_first,
1259         &hf_zbee_nwk_cmd_link_count,
1260         NULL
1261     };
1262 
1263     /* Get and Display the link status options. */
1264     options = tvb_get_guint8(tvb, offset);
1265     link_count = options & ZBEE_NWK_CMD_LINK_OPTION_COUNT_MASK;
1266     proto_tree_add_bitmask_list(tree, tvb, offset, 1, link_options, ENC_NA);
1267     offset += 1;
1268 
1269     /* Get and Display the link status list. */
1270     for (i=0; i<link_count; i++) {
1271         /* Get the address and link status. */
1272         subtree = proto_tree_add_subtree_format(tree, tvb, offset, 3, ett_zbee_nwk_cmd_link, NULL, "Link %d", i+1);
1273         proto_tree_add_item(subtree, hf_zbee_nwk_cmd_link_address, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1274         proto_tree_add_item(subtree, hf_zbee_nwk_cmd_link_incoming_cost, tvb, offset+2, 1, ENC_NA);
1275         proto_tree_add_item(subtree, hf_zbee_nwk_cmd_link_outgoing_cost, tvb, offset+2, 1, ENC_NA);
1276         offset += (2+1);
1277     } /* for */
1278 
1279     /* TODO: Update the info column. */
1280     return offset;
1281 } /* dissect_zbee_nwk_link_status */
1282 
1283 /**
1284  *Helper dissector for the End Device Timeout Request command.
1285  *
1286  *@param tvb pointer to buffer containing raw packet.
1287  *@param tree pointer to the command subtree.
1288  *@param  offset into the tvb to begin dissection.
1289  *@return offset after command dissection.
1290 */
1291 static guint
dissect_zbee_nwk_ed_timeout_request(tvbuff_t * tvb,proto_tree * tree,guint offset)1292 dissect_zbee_nwk_ed_timeout_request(tvbuff_t *tvb, proto_tree *tree, guint offset)
1293 {
1294     /* See 3.4.11 End Device Timeout Request Command */
1295 
1296     /* 3.4.11.3.1 Requested Timeout Field */
1297     proto_tree_add_item(tree, hf_zbee_nwk_cmd_end_device_timeout_request_enum, tvb, offset, 1, ENC_NA);
1298     offset++;
1299 
1300     /* 3.4.11.3.2 End Device Configuration Field */
1301     proto_tree_add_item(tree, hf_zbee_nwk_cmd_end_device_configuration, tvb, offset, 1, ENC_NA);
1302     offset++;
1303 
1304     return offset;
1305 } /* dissect_zbee_nwk_ed_timeout_request */
1306 
1307 /**
1308  *Helper dissector for the End Device Timeout Response command.
1309  *
1310  *@param tvb pointer to buffer containing raw packet.
1311  *@param tree pointer to the command subtree.
1312  *@param  offset into the tvb to begin dissection.
1313  *@return offset after command dissection.
1314 */
1315 static guint
dissect_zbee_nwk_ed_timeout_response(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,guint offset)1316 dissect_zbee_nwk_ed_timeout_response(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1317 {
1318     static int * const end_device_parent_info[] = {
1319         &hf_zbee_nwk_cmd_prnt_info_mac_data_poll_keepalive_supported,
1320         &hf_zbee_nwk_cmd_prnt_info_ed_to_req_keepalive_supported,
1321         &hf_zbee_nwk_cmd_prnt_info_power_negotiation_supported,
1322         NULL
1323     };
1324     guint status = tvb_get_guint8(tvb, offset);
1325     /* 3.4.12 End Device Timeout Response Command */
1326 
1327     /* status */
1328     proto_tree_add_item(tree, hf_zbee_nwk_cmd_end_device_timeout_resp_status, tvb, offset, 1, ENC_NA);
1329     offset++;
1330 
1331     /* Parent Information bitmask */
1332     proto_tree_add_bitmask(tree, tvb, offset, hf_zbee_nwk_cmd_end_device_timeout_resp_parent_info, ett_zbee_nwk_cmd_ed_to_rsp_prnt_info, end_device_parent_info, ENC_NA);
1333     offset++;
1334 
1335     proto_item_append_text(tree, ", %s", val_to_str_const(status, zbee_nwk_end_device_timeout_resp_status, "Unknown Status"));
1336     col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(status, zbee_nwk_end_device_timeout_resp_status, "Unknown Status"));
1337 
1338     return offset;
1339 } /* dissect_zbee_nwk_ed_timeout_response */
1340 
1341 /**
1342  *Helper dissector for the Link Power Delta command.
1343  *
1344  *@param tvb pointer to buffer containing raw packet.
1345  *@param tree pointer to the command subtree.
1346  *@param  offset into the tvb to begin dissection.
1347  *@return offset after command dissection.
1348 */
1349 static guint
dissect_zbee_nwk_link_pwr_delta(tvbuff_t * tvb,packet_info * pinfo _U_,proto_tree * tree,guint offset)1350 dissect_zbee_nwk_link_pwr_delta(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, guint offset)
1351 {
1352     int     i;
1353     int     count;
1354     gint    delta;
1355     guint8  type;
1356     guint16 addr;
1357     proto_tree *subtree;
1358 
1359     type = tvb_get_guint8(tvb, offset) & ZBEE_NWK_CMD_NWK_LINK_PWR_DELTA_TYPE_MASK;
1360     proto_tree_add_item(tree, hf_zbee_nwk_cmd_link_pwr_type, tvb, offset, 1, ENC_NA);
1361     offset++;
1362 
1363     count = tvb_get_guint8(tvb, offset);
1364     proto_tree_add_item(tree, hf_zbee_nwk_cmd_link_pwr_list_count, tvb, offset, 1, ENC_NA);
1365     offset++;
1366 
1367     proto_item_append_text(tree, ": %s, Count %d", val_to_str_const(type, zbee_nwk_link_power_delta_types, "Unknown"), count);
1368 
1369     for (i=0; i<count; i++) {
1370         subtree = proto_tree_add_subtree(tree, tvb, count, 3, ett_zbee_nwk_cmd_link_pwr_struct, NULL, "Power Delta Structure");
1371         addr = tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN);
1372         proto_tree_add_item(subtree, hf_zbee_nwk_cmd_link_pwr_device_address, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1373         offset += 2;
1374         delta = (char)tvb_get_guint8(tvb, offset);
1375         proto_tree_add_item(subtree, hf_zbee_nwk_cmd_link_pwr_power_delta, tvb, offset, 1, ENC_NA);
1376         offset++;
1377         proto_item_append_text(subtree, ": Device Address 0x%04X, Power Delta %d dBm", addr, delta);
1378     }
1379     return offset;
1380 }
1381 
1382 /**
1383  *Helper dissector for the Network Report command.
1384  *
1385  *@param tvb pointer to buffer containing raw packet.
1386  *@param pinfo pointer to packet information fields
1387  *@param tree pointer to the command subtree.
1388  *@param  offset into the tvb to begin dissection.
1389  *@return offset after command dissection.
1390 */
1391 static guint
dissect_zbee_nwk_report(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,guint offset)1392 dissect_zbee_nwk_report(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1393 {
1394     guint8  options;
1395     guint8  report_type;
1396     int     report_count;
1397     int     i;
1398 
1399     /* Get and display the command options field. */
1400     options = tvb_get_guint8(tvb, offset);
1401     report_count = options & ZBEE_NWK_CMD_NWK_REPORT_COUNT_MASK;
1402     report_type = options & ZBEE_NWK_CMD_NWK_REPORT_ID_MASK;
1403     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_report_type, tvb, offset, 1, report_type);
1404     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_report_count, tvb, offset, 1, report_count);
1405     offset += 1;
1406     report_type >>= ws_ctz(ZBEE_NWK_CMD_NWK_REPORT_ID_MASK);
1407 
1408     /* Get and display the epid. */
1409     proto_tree_add_item(tree, hf_zbee_nwk_cmd_epid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
1410     offset += 8;
1411 
1412     if (report_type == ZBEE_NWK_CMD_NWK_REPORT_ID_PAN_CONFLICT) {
1413 
1414         /* Report information contains a list of PANS with range of the sender. */
1415         for (i=0; i<report_count; i++) {
1416             proto_tree_add_item(tree, hf_zbee_nwk_panid, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1417             offset += 2;
1418         } /* for */
1419     }
1420     if (report_type == ZBEE_NWK_CMD_NWK_REPORT_ID_ZBOSS_KEY_TRACE) {
1421         guint8              key[ZBEE_APS_CMD_KEY_LENGTH];
1422 
1423         for (i=0; i<ZBEE_APS_CMD_KEY_LENGTH ; i++) {
1424             key[i] = tvb_get_guint8(tvb, offset+i);
1425         } /* for */
1426         proto_tree_add_item(tree, hf_zbee_zboss_nwk_cmd_key, tvb, offset, ZBEE_APS_CMD_KEY_LENGTH, ENC_NA);
1427         offset += ZBEE_APS_CMD_KEY_LENGTH;
1428         zbee_sec_add_key_to_keyring(pinfo, key);
1429     }
1430 
1431     /* Update the info column. */
1432     col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(report_type, zbee_nwk_report_types, "Unknown Report Type"));
1433 
1434     /* Done */
1435     return offset;
1436 } /* dissect_zbee_nwk_report */
1437 
1438 /**
1439  *Helper dissector for the Network Update command.
1440  *
1441  *@param tvb pointer to buffer containing raw packet.
1442  *@param pinfo pointer to packet information fields
1443  *@param tree pointer to the command subtree.
1444  *@param  offset into the tvb to begin dissection.
1445  *@return offset after command dissection.
1446 */
1447 static guint
dissect_zbee_nwk_update(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,guint offset)1448 dissect_zbee_nwk_update(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1449 {
1450     guint8  options;
1451     guint8  update_type;
1452     guint8  update_id;
1453     int     update_count;
1454     int     i;
1455 
1456     /* Get and display the command options field. */
1457     options = tvb_get_guint8(tvb, offset);
1458     update_count = options & ZBEE_NWK_CMD_NWK_UPDATE_COUNT_MASK;
1459     update_type = options & ZBEE_NWK_CMD_NWK_UPDATE_ID_MASK;
1460     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_type, tvb, offset, 1, update_type);
1461     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_count, tvb, offset, 1, update_count);
1462     offset += 1;
1463 
1464     /* Get and display the epid. */
1465     proto_tree_add_item(tree, hf_zbee_nwk_cmd_epid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
1466     offset += 8;
1467 
1468     /* Get and display the updateID. */
1469     update_id = tvb_get_guint8(tvb, offset);
1470     proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_id, tvb, offset, 1, update_id);
1471     offset += 1;
1472 
1473     if (update_type == ZBEE_NWK_CMD_NWK_UPDATE_ID_PAN_UPDATE) {
1474 
1475         /* Report information contains a list of PANS with range of the sender. */
1476         for (i=0; i<update_count; i++) {
1477             proto_tree_add_item(tree, hf_zbee_nwk_panid, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1478             offset += 2;
1479         } /* for */
1480     }
1481 
1482     /* Update the info column. */
1483     col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str_const(update_type, zbee_nwk_update_types, "Unknown Update Type"));
1484 
1485     /* Done */
1486     return offset;
1487 } /* dissect_zbee_nwk_update */
1488 
1489 /**
1490  *Heuristic interpreter for the ZigBee PRO beacon dissectors.
1491  *
1492  *@param tvb pointer to buffer containing raw packet.
1493  *@param pinfo pointer to packet information fields
1494  *@param tree pointer to data tree Wireshark uses to display packet.
1495  *@return Boolean value, whether it handles the packet or not.
1496 */
1497 static gboolean
dissect_zbee_beacon_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1498 dissect_zbee_beacon_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1499 {
1500     ieee802154_packet   *packet = (ieee802154_packet *)data;
1501 
1502     /* All ZigBee frames must always have a 16-bit source address. */
1503     if (!packet) return FALSE;
1504     if (packet->src_addr_mode != IEEE802154_FCF_ADDR_SHORT) return FALSE;
1505     if (tvb_captured_length(tvb) == 0) return FALSE;
1506 
1507     /* ZigBee beacons begin with a protocol identifier. */
1508     if (tvb_get_guint8(tvb, 0) != ZBEE_NWK_BEACON_PROTOCOL_ID) return FALSE;
1509     dissect_zbee_beacon(tvb, pinfo, tree, packet);
1510     return TRUE;
1511 } /* dissect_zbee_beacon_heur */
1512 
1513 /**
1514  *Dissector for Legacy ZigBee Beacon Payloads (prior to the Enhanced Beacon)
1515  *
1516  *@param tvb pointer to buffer containing raw packet.
1517  *@param pinfo pointer to packet information fields
1518  *@param tree pointer to data tree Wireshark uses to display packet.
1519 */
dissect_zbee_beacon(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)1520 static int dissect_zbee_beacon(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1521 {
1522 
1523     proto_item  *beacon_root;
1524     proto_tree  *beacon_tree;
1525     guint        offset = 0;
1526     guint8       version;
1527     guint32      profile;
1528 
1529     static int * const beacon_fields[] = {
1530         &hf_zbee_beacon_stack_profile,
1531         &hf_zbee_beacon_version,
1532         &hf_zbee_beacon_router_capacity,
1533         &hf_zbee_beacon_depth,
1534         &hf_zbee_beacon_end_device_capacity,
1535         NULL
1536     };
1537 
1538     /* Add ourself to the protocol column. */
1539     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZigBee");
1540     /* Create the tree for this beacon. */
1541     beacon_root = proto_tree_add_item(tree, proto_zbee_beacon, tvb, 0, -1, ENC_NA);
1542     beacon_tree = proto_item_add_subtree(beacon_root, ett_zbee_nwk_beacon);
1543 
1544     /* Get and display the protocol id, must be 0 on all ZigBee beacons. */
1545     proto_tree_add_item(beacon_tree, hf_zbee_beacon_protocol, tvb, offset, 1, ENC_NA);
1546     offset += 1;
1547 
1548     proto_tree_add_bitmask_text(beacon_tree, tvb, offset, 2, "Beacon: ", NULL , ett_zbee_nwk_beacon_bitfield, beacon_fields,
1549             ENC_LITTLE_ENDIAN, BMT_NO_INT|BMT_NO_TFS);
1550 
1551     /* Get and display the stack profile and protocol version. */
1552     version = (guint8)((tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN) & ZBEE_NWK_BEACON_PROTOCOL_VERSION) >> 4);
1553     profile = (guint32)(tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN) & ZBEE_NWK_BEACON_STACK_PROFILE);
1554     proto_item_append_text(beacon_root, ", %s", val_to_str_const(profile, zbee_nwk_stack_profiles, "Unknown Profile"));
1555     offset += 2;
1556 
1557     if (version >= ZBEE_VERSION_2007) {
1558         /* In ZigBee 2006 and later, the beacon contains an extended PAN ID. */
1559         proto_tree_add_item(beacon_tree, hf_zbee_beacon_epid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
1560         col_append_fstr(pinfo->cinfo, COL_INFO, ", EPID: %s", eui64_to_display(pinfo->pool,
1561                 tvb_get_guint64(tvb, offset, ENC_LITTLE_ENDIAN)));
1562         proto_item_append_text(beacon_root, ", EPID: %s", eui64_to_display(pinfo->pool,
1563                 tvb_get_guint64(tvb, offset, ENC_LITTLE_ENDIAN)));
1564         offset += 8;
1565 
1566         /*
1567          * In ZigBee 2006 the Tx-Offset is optional, while in the 2007 and
1568          * later versions, the Tx-Offset is a required value. Since both 2006 and
1569          * and 2007 versions have the same protocol version (2), we should treat
1570          * the Tx-Offset as well as the update ID as optional elements
1571          */
1572         if (tvb_bytes_exist(tvb, offset, 3)) {
1573             proto_tree_add_item(beacon_tree, hf_zbee_beacon_tx_offset, tvb, offset, 3, ENC_LITTLE_ENDIAN);
1574             offset += 3;
1575 
1576             /* Get and display the update ID. */
1577             if(tvb_captured_length_remaining(tvb, offset)) {
1578                 proto_tree_add_item(beacon_tree, hf_zbee_beacon_update_id, tvb, offset, 1, ENC_NA);
1579                 offset += 1;
1580             }
1581         }
1582     }
1583     else if (tvb_bytes_exist(tvb, offset, 3)) {
1584         /* In ZigBee 2004, the Tx-Offset is an optional value. */
1585         proto_tree_add_item(beacon_tree, hf_zbee_beacon_tx_offset, tvb, offset, 3, ENC_LITTLE_ENDIAN);
1586         offset += 3;
1587 
1588     }
1589 
1590     return offset;
1591 } /* dissect_zbee_beacon */
1592 
1593 /**
1594  *Heuristic interpreter for the ZigBee IP beacon dissectors.
1595  *
1596  *@param tvb pointer to buffer containing raw packet.
1597  *@param pinfo pointer to packet information fields
1598  *@param tree pointer to data tree Wireshark uses to display packet.
1599  *@return Boolean value, whether it handles the packet or not.
1600 */
1601 static gboolean
dissect_zbip_beacon_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1602 dissect_zbip_beacon_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1603 {
1604     ieee802154_packet   *packet = (ieee802154_packet *)data;
1605 
1606     /* All ZigBee frames must always have a 16-bit source address. */
1607     if (!packet) return FALSE;
1608     if (packet->src_addr_mode != IEEE802154_FCF_ADDR_SHORT) return FALSE;
1609     if (tvb_captured_length(tvb) == 0) return FALSE;
1610 
1611     /* ZigBee beacons begin with a protocol identifier. */
1612     if (tvb_get_guint8(tvb, 0) != ZBEE_IP_BEACON_PROTOCOL_ID) return FALSE;
1613     dissect_zbip_beacon(tvb, pinfo, tree, packet);
1614     return TRUE;
1615 } /* dissect_zbip_beacon_heur */
1616 
1617 /**
1618  *Dissector for ZigBee IP beacons.
1619  *
1620  *@param tvb pointer to buffer containing raw packet.
1621  *@param pinfo pointer to packet information fields
1622  *@param tree pointer to data tree Wireshark uses to display packet.
1623 */
dissect_zbip_beacon(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1624 static int dissect_zbip_beacon(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1625 {
1626     ieee802154_packet   *packet = (ieee802154_packet *)data;
1627 
1628     proto_item  *beacon_root;
1629     proto_tree  *beacon_tree;
1630     guint       offset = 0;
1631     guint8      proto_id;
1632     char        *ssid;
1633 
1634     /* Reject the packet if data is NULL */
1635     if (!packet) return 0;
1636 
1637     /* Add ourself to the protocol column. */
1638     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZigBee IP");
1639     /* Create the tree for this beacon. */
1640     beacon_root = proto_tree_add_item(tree, proto_zbip_beacon, tvb, 0, -1, ENC_NA);
1641     beacon_tree = proto_item_add_subtree(beacon_root, ett_zbee_nwk_beacon);
1642 
1643     /* Update the info column. */
1644     col_clear(pinfo->cinfo, COL_INFO);
1645     col_append_fstr(pinfo->cinfo, COL_INFO, "Beacon, Src: 0x%04x", packet->src16);
1646 
1647     /* Get and display the protocol id, must be 0x02 on all ZigBee beacons. */
1648     proto_id = tvb_get_guint8(tvb, offset);
1649     proto_tree_add_uint(beacon_tree, hf_zbee_beacon_protocol, tvb, offset, 1, proto_id);
1650     offset += 1;
1651 
1652     /* Get and display the beacon flags */
1653     proto_tree_add_item(beacon_tree, hf_zbip_beacon_allow_join, tvb, offset, 1, ENC_BIG_ENDIAN);
1654     proto_tree_add_item(beacon_tree, hf_zbip_beacon_router_capacity, tvb, offset, 1, ENC_BIG_ENDIAN);
1655     proto_tree_add_item(beacon_tree, hf_zbip_beacon_host_capacity, tvb, offset, 1, ENC_BIG_ENDIAN);
1656     proto_tree_add_item(beacon_tree, hf_zbip_beacon_unsecure, tvb, offset, 1, ENC_BIG_ENDIAN);
1657     offset += 1;
1658 
1659     /* Get and display the network ID. */
1660     proto_tree_add_item(beacon_tree, hf_zbip_beacon_network_id, tvb, offset, 16, ENC_ASCII|ENC_NA);
1661 
1662     ssid = tvb_get_string_enc(pinfo->pool, tvb, offset, 16, ENC_ASCII|ENC_NA);
1663     col_append_fstr(pinfo->cinfo, COL_INFO, ", SSID: %s", ssid);
1664     offset += 16;
1665 
1666     /* Check for leftover bytes. */
1667     if (offset < tvb_captured_length(tvb)) {
1668         /* TODO: There are TLV's to parse. */
1669         /* Bytes leftover! */
1670         tvbuff_t    *leftover_tvb   = tvb_new_subset_remaining(tvb, offset);
1671         proto_tree  *root;
1672 
1673         /* Correct the length of the beacon tree. */
1674         root = proto_tree_get_root(tree);
1675         proto_item_set_len(beacon_root, offset);
1676 
1677         /* Dump the leftover to the data dissector. */
1678         call_data_dissector(leftover_tvb, pinfo, root);
1679     }
1680     return tvb_captured_length(tvb);
1681 } /* dissect_zbip_beacon */
1682 
1683 /**
1684  *Subdissector command for ZigBee Specific IEs (Information Elements)
1685  *
1686  *@param tvb pointer to buffer containing raw packet.
1687  *@param pinfo pointer to packet information fields (unused).
1688  *@param tree pointer to command subtree.
1689  *@param data pointer to the length of the payload IE.
1690 */
1691 static int
dissect_zbee_ie(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1692 dissect_zbee_ie(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1693 {
1694     proto_tree *subtree;
1695     tvbuff_t   *ie_tvb;
1696     guint16     zigbee_ie;
1697     guint16     id;
1698     guint16     length;
1699     guint       pie_length;
1700     guint       offset = 0;
1701 
1702     static int * const fields[] = {
1703         &hf_ieee802154_zigbee_ie_id,
1704         &hf_ieee802154_zigbee_ie_length,
1705         NULL
1706     };
1707 
1708     pie_length = *(gint *)data;
1709 
1710     do {
1711         zigbee_ie =  tvb_get_letohs(tvb, offset);
1712         id        = (zigbee_ie & ZBEE_ZIGBEE_IE_ID_MASK) >> 6;
1713         length    =  zigbee_ie & ZBEE_ZIGBEE_IE_LENGTH_MASK;
1714 
1715         /* Create a subtree for this command frame. */
1716         subtree = proto_tree_add_subtree(tree, tvb, offset, 2+length, ett_zbee_nwk_header, NULL, "ZigBee IE");
1717         proto_item_append_text(subtree, ", %s, Length: %d", val_to_str_const(id, ieee802154_zigbee_ie_names, "Unknown"), length);
1718 
1719         proto_tree_add_bitmask(subtree, tvb, offset, hf_ieee802154_zigbee_ie,
1720                                ett_zbee_nwk_zigbee_ie_fields, fields, ENC_LITTLE_ENDIAN);
1721         offset += 2;
1722 
1723         switch (id) {
1724             case ZBEE_ZIGBEE_IE_REJOIN:
1725                 dissect_ieee802154_zigbee_rejoin(tvb, pinfo, subtree, &offset);
1726                 break;
1727 
1728             case ZBEE_ZIGBEE_IE_TX_POWER:
1729                 dissect_ieee802154_zigbee_txpower(tvb, pinfo, subtree, &offset);
1730                 break;
1731 
1732             case ZBEE_ZIGBEE_IE_BEACON_PAYLOAD:
1733                 ie_tvb = tvb_new_subset_length(tvb, offset, ZBEE_NWK_BEACON_LENGTH);
1734                 offset += dissect_zbee_beacon(ie_tvb, pinfo, subtree, NULL); /* Legacy ZigBee Beacon */
1735                 dissect_ieee802154_superframe(tvb, pinfo, subtree, &offset);
1736                 proto_tree_add_item(subtree, hf_ieee802154_zigbee_ie_source_addr, tvb, offset, 2, ENC_NA);
1737                 offset += 2;
1738                 break;
1739 
1740             default:
1741                 if (length > 0) { /* just use the data dissector */
1742                     call_data_dissector(tvb, pinfo, tree);
1743                     offset += length;
1744                 }
1745                 break;
1746         }
1747     } while (offset < pie_length);
1748 
1749     return tvb_captured_length(tvb);
1750 }
1751 
1752 /**
1753  *Subdissector for the ZigBee specific TX Power IE (information element)
1754  *
1755  *@param tvb pointer to buffer containing raw packet.
1756  *@param pinfo pointer to packet information fields (unused).
1757  *@param tree pointer to command subtree.
1758  *@param offset offset into the tvbuff to begin dissection.
1759 */
1760 static void
dissect_ieee802154_zigbee_txpower(tvbuff_t * tvb,packet_info * pinfo _U_,proto_tree * tree,guint * offset)1761 dissect_ieee802154_zigbee_txpower(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, guint *offset)
1762 {
1763     gint32 txpower;
1764 
1765     txpower = (char)tvb_get_guint8(tvb, *offset); /* tx power is a signed byte */
1766 
1767     proto_tree_add_item_ret_int(tree, hf_ieee802154_zigbee_ie_tx_power, tvb, *offset, 1, ENC_NA, &txpower);
1768     proto_item_append_text(tree, ", TX Power %d dBm", txpower);
1769 
1770     *offset += 1;
1771 }
1772 
1773 /**
1774  *Subdissector for the ZigBee specific Rejoin IE (information element)
1775  *
1776  *@param tvb pointer to buffer containing raw packet.
1777  *@param pinfo pointer to packet information fields (unused).
1778  *@param tree pointer to command subtree.
1779  *@param offset offset into the tvbuff to begin dissection.
1780 */
1781 static void
dissect_ieee802154_zigbee_rejoin(tvbuff_t * tvb,packet_info * pinfo _U_,proto_tree * tree,guint * offset)1782 dissect_ieee802154_zigbee_rejoin(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, guint *offset)
1783 {
1784     proto_tree *subtree;
1785 
1786     subtree = proto_tree_add_subtree(tree, tvb, *offset, 10, ett_zbee_nwk_ie_rejoin, NULL, "ZigBee Rejoin");
1787 
1788     proto_tree_add_item(subtree, hf_ieee802154_zigbee_rejoin_epid, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1789     proto_item_append_text(tree, ", EPID %s", eui64_to_display(pinfo->pool,
1790             tvb_get_guint64(tvb, *offset, ENC_LITTLE_ENDIAN)));
1791     *offset += 8;
1792 
1793     proto_tree_add_item(subtree, hf_ieee802154_zigbee_rejoin_source_addr, tvb, *offset, 2, ENC_LITTLE_ENDIAN);
1794     proto_item_append_text(tree, ", Src: 0x%04x",
1795             tvb_get_guint16(tvb, *offset, ENC_LITTLE_ENDIAN));
1796     *offset += 2;
1797 
1798 } /* dissect_ieee802154_zigbee_rejoin */
1799 
zbee_nwk_conv_get_filter_type(conv_item_t * conv,conv_filter_type_e filter)1800 static const char* zbee_nwk_conv_get_filter_type(conv_item_t* conv, conv_filter_type_e filter)
1801 {
1802     if ((filter == CONV_FT_SRC_ADDRESS) && (conv->src_address.type == zbee_nwk_address_type))
1803         return "zbee_nwk.src";
1804 
1805     if ((filter == CONV_FT_DST_ADDRESS) && (conv->dst_address.type == zbee_nwk_address_type))
1806         return "zbee_nwk.dst";
1807 
1808     if ((filter == CONV_FT_ANY_ADDRESS) && (conv->src_address.type == zbee_nwk_address_type))
1809         return "zbee_nwk.addr";
1810 
1811     return CONV_FILTER_INVALID;
1812 }
1813 
1814 static ct_dissector_info_t zbee_nwk_ct_dissector_info = {&zbee_nwk_conv_get_filter_type };
1815 
zbee_nwk_conversation_packet(void * pct,packet_info * pinfo,epan_dissect_t * edt _U_,const void * vip _U_)1816 static tap_packet_status zbee_nwk_conversation_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip _U_)
1817 {
1818     conv_hash_t *hash = (conv_hash_t*)pct;
1819 
1820     add_conversation_table_data(hash, &pinfo->net_src, &pinfo->net_dst, 0, 0, 1,
1821             pinfo->fd->pkt_len, &pinfo->rel_ts, &pinfo->abs_ts,
1822             &zbee_nwk_ct_dissector_info, ENDPOINT_NONE);
1823 
1824     return TAP_PACKET_REDRAW;
1825 }
1826 
zbee_nwk_host_get_filter_type(hostlist_talker_t * host,conv_filter_type_e filter)1827 static const char* zbee_nwk_host_get_filter_type(hostlist_talker_t* host, conv_filter_type_e filter)
1828 {
1829     if ((filter == CONV_FT_ANY_ADDRESS) && (host->myaddress.type == zbee_nwk_address_type))
1830         return "zbee_nwk.addr";
1831 
1832     return CONV_FILTER_INVALID;
1833 }
1834 
1835 static hostlist_dissector_info_t zbee_nwk_host_dissector_info = {&zbee_nwk_host_get_filter_type };
1836 
zbee_nwk_hostlist_packet(void * pit,packet_info * pinfo,epan_dissect_t * edt _U_,const void * vip _U_)1837 static tap_packet_status zbee_nwk_hostlist_packet(void *pit, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip _U_)
1838 {
1839     conv_hash_t *hash = (conv_hash_t*)pit;
1840 
1841     /* Take two "add" passes per packet, adding for each direction, ensures that all
1842      packets are counted properly (even if address is sending to itself)
1843      XXX - this could probably be done more efficiently inside hostlist_table */
1844     add_hostlist_table_data(hash, &pinfo->net_src, 0, TRUE, 1,
1845             pinfo->fd->pkt_len, &zbee_nwk_host_dissector_info, ENDPOINT_NONE);
1846     add_hostlist_table_data(hash, &pinfo->net_dst, 0, FALSE, 1,
1847             pinfo->fd->pkt_len, &zbee_nwk_host_dissector_info, ENDPOINT_NONE);
1848 
1849     return TAP_PACKET_REDRAW;
1850 }
1851 
zbee_nwk_filter_valid(packet_info * pinfo)1852 static gboolean zbee_nwk_filter_valid(packet_info *pinfo)
1853 {
1854     return proto_is_frame_protocol(pinfo->layers, "zbee_nwk");
1855 }
1856 
zbee_nwk_build_filter(packet_info * pinfo)1857 static gchar* zbee_nwk_build_filter(packet_info *pinfo)
1858 {
1859     return g_strdup_printf("zbee_nwk.addr eq %s and zbee_nwk.addr eq %s",
1860             address_to_str(pinfo->pool, &pinfo->net_src),
1861             address_to_str(pinfo->pool, &pinfo->net_dst));
1862 }
1863 
1864 /**
1865  *ZigBee protocol registration routine.
1866  *
1867 */
proto_register_zbee_nwk(void)1868 void proto_register_zbee_nwk(void)
1869 {
1870     static hf_register_info hf[] = {
1871 
1872             { &hf_zbee_nwk_fcf,
1873             { "Frame Control Field",             "zbee_nwk.fcf", FT_UINT16, BASE_HEX, NULL,
1874                 0x0, NULL, HFILL }},
1875 
1876             { &hf_zbee_nwk_frame_type,
1877             { "Frame Type",             "zbee_nwk.frame_type", FT_UINT16, BASE_HEX, VALS(zbee_nwk_frame_types),
1878                 ZBEE_NWK_FCF_FRAME_TYPE, NULL, HFILL }},
1879 
1880             { &hf_zbee_nwk_proto_version,
1881             { "Protocol Version",       "zbee_nwk.proto_version", FT_UINT16, BASE_DEC, NULL, ZBEE_NWK_FCF_VERSION,
1882                 NULL, HFILL }},
1883 
1884             { &hf_zbee_nwk_discover_route,
1885             { "Discover Route",         "zbee_nwk.discovery", FT_UINT16, BASE_HEX, VALS(zbee_nwk_discovery_modes),
1886                 ZBEE_NWK_FCF_DISCOVER_ROUTE,
1887                 "Determines how route discovery may be handled, if at all.", HFILL }},
1888 
1889             { &hf_zbee_nwk_multicast,
1890             { "Multicast",              "zbee_nwk.multicast", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_MULTICAST,
1891                 NULL, HFILL }},
1892 
1893             { &hf_zbee_nwk_security,
1894             { "Security",               "zbee_nwk.security", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_SECURITY,
1895                 "Whether or not security operations are performed on the network payload.", HFILL }},
1896 
1897             { &hf_zbee_nwk_source_route,
1898             { "Source Route",           "zbee_nwk.src_route", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_SOURCE_ROUTE,
1899                 NULL, HFILL }},
1900 
1901             { &hf_zbee_nwk_ext_dst,
1902             { "Destination",            "zbee_nwk.ext_dst", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_EXT_DEST,
1903                 NULL, HFILL }},
1904 
1905             { &hf_zbee_nwk_ext_src,
1906             { "Extended Source",        "zbee_nwk.ext_src", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_EXT_SOURCE,
1907                 NULL, HFILL }},
1908 
1909             { &hf_zbee_nwk_end_device_initiator,
1910             { "End Device Initiator",   "zbee_nwk.end_device_initiator", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_END_DEVICE_INITIATOR,
1911                 NULL, HFILL }},
1912 
1913             { &hf_zbee_nwk_dst,
1914             { "Destination",            "zbee_nwk.dst", FT_UINT16, BASE_HEX, NULL, 0x0,
1915                 NULL, HFILL }},
1916 
1917             { &hf_zbee_nwk_src,
1918             { "Source",                 "zbee_nwk.src", FT_UINT16, BASE_HEX, NULL, 0x0,
1919                 NULL, HFILL }},
1920 
1921             { &hf_zbee_nwk_addr,
1922             { "Address",                 "zbee_nwk.addr", FT_UINT16, BASE_HEX, NULL, 0x0,
1923                 NULL, HFILL }},
1924 
1925             { &hf_zbee_nwk_radius,
1926             { "Radius",                 "zbee_nwk.radius", FT_UINT8, BASE_DEC, NULL, 0x0,
1927                 "Number of hops remaining for a range-limited broadcast packet.", HFILL }},
1928 
1929             { &hf_zbee_nwk_seqno,
1930             { "Sequence Number",        "zbee_nwk.seqno", FT_UINT8, BASE_DEC, NULL, 0x0,
1931                 NULL, HFILL }},
1932 
1933             { &hf_zbee_nwk_mcast,
1934             { "Multicast Control Field",         "zbee_nwk.multicast.cf", FT_UINT8, BASE_HEX, NULL, 0x0,
1935                 NULL, HFILL }},
1936 
1937             { &hf_zbee_nwk_mcast_mode,
1938             { "Multicast Mode",         "zbee_nwk.multicast.mode", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_MCAST_MODE,
1939                 "Controls whether this packet is permitted to be routed through non-members of the multicast group.",
1940                 HFILL }},
1941 
1942             { &hf_zbee_nwk_mcast_radius,
1943             { "Non-Member Radius",      "zbee_nwk.multicast.radius", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_MCAST_RADIUS,
1944                 "Limits the range of multicast packets when being routed through non-members.", HFILL }},
1945 
1946             { &hf_zbee_nwk_mcast_max_radius,
1947             { "Max Non-Member Radius",  "zbee_nwk.multicast.max_radius", FT_UINT8, BASE_DEC, NULL,
1948                 ZBEE_NWK_MCAST_MAX_RADIUS, NULL, HFILL }},
1949 
1950             { &hf_zbee_nwk_dst64,
1951             { "Destination",   "zbee_nwk.dst64", FT_EUI64, BASE_NONE, NULL, 0x0,
1952                 NULL, HFILL }},
1953 
1954             { &hf_zbee_nwk_src64,
1955             { "Extended Source",        "zbee_nwk.src64", FT_EUI64, BASE_NONE, NULL, 0x0,
1956                 NULL, HFILL }},
1957 
1958             { &hf_zbee_nwk_addr64,
1959             { "Extended Address",        "zbee_nwk.addr64", FT_EUI64, BASE_NONE, NULL, 0x0,
1960                 NULL, HFILL }},
1961 
1962             { &hf_zbee_nwk_src64_origin,
1963             { "Origin",        "zbee_nwk.src64.origin", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1964                 NULL, HFILL }},
1965 
1966             { &hf_zbee_nwk_relay_count,
1967             { "Relay Count",            "zbee_nwk.relay.count", FT_UINT8, BASE_DEC, NULL, 0x0,
1968                 "Number of entries in the relay list.", HFILL }},
1969 
1970             { &hf_zbee_nwk_relay_index,
1971             { "Relay Index",            "zbee_nwk.relay.index", FT_UINT8, BASE_DEC, NULL, 0x0,
1972                 "Number of relays required to route to the source device.", HFILL }},
1973 
1974             { &hf_zbee_nwk_relay,
1975             { "Relay",            "zbee_nwk.relay", FT_UINT16, BASE_DEC, NULL, 0x0,
1976                 NULL, HFILL }},
1977 
1978             { &hf_zbee_nwk_cmd_id,
1979             { "Command Identifier",     "zbee_nwk.cmd.id", FT_UINT8, BASE_HEX, VALS(zbee_nwk_cmd_names), 0x0,
1980                 NULL, HFILL }},
1981 
1982             { &hf_zbee_nwk_cmd_addr,
1983             { "Address",                "zbee_nwk.cmd.addr", FT_UINT16, BASE_HEX, NULL, 0x0,
1984                 NULL, HFILL }},
1985 
1986             { &hf_zbee_nwk_cmd_route_id,
1987             { "Route ID",               "zbee_nwk.cmd.route.id", FT_UINT8, BASE_DEC, NULL, 0x0,
1988                 "A sequence number for routing commands.", HFILL }},
1989 
1990             { &hf_zbee_nwk_cmd_route_dest,
1991             { "Destination",            "zbee_nwk.cmd.route.dest", FT_UINT16, BASE_HEX, NULL, 0x0,
1992                 NULL, HFILL }},
1993 
1994             { &hf_zbee_nwk_cmd_route_orig,
1995             { "Originator",             "zbee_nwk.cmd.route.orig", FT_UINT16, BASE_HEX, NULL, 0x0,
1996                 NULL, HFILL }},
1997 
1998             { &hf_zbee_nwk_cmd_route_resp,
1999             { "Responder",              "zbee_nwk.cmd.route.resp", FT_UINT16, BASE_HEX, NULL, 0x0,
2000                 NULL, HFILL }},
2001 
2002             { &hf_zbee_nwk_cmd_route_dest_ext,
2003             { "Extended Destination",   "zbee_nwk.cmd.route.dest_ext", FT_EUI64, BASE_NONE, NULL, 0x0,
2004                 NULL, HFILL }},
2005 
2006             { &hf_zbee_nwk_cmd_route_orig_ext,
2007             { "Extended Originator",    "zbee_nwk.cmd.route.orig_ext", FT_EUI64, BASE_NONE, NULL, 0x0,
2008                 NULL, HFILL }},
2009 
2010             { &hf_zbee_nwk_cmd_route_resp_ext,
2011             { "Extended Responder",     "zbee_nwk.cmd.route.resp_ext", FT_EUI64, BASE_NONE, NULL, 0x0,
2012                 NULL, HFILL }},
2013 
2014             { &hf_zbee_nwk_cmd_route_cost,
2015             { "Path Cost",              "zbee_nwk.cmd.route.cost", FT_UINT8, BASE_DEC, NULL, 0x0,
2016                 "A value specifying the efficiency of this route.", HFILL }},
2017 
2018             { &hf_zbee_nwk_cmd_route_options,
2019             { "Command Options",           "zbee_nwk.cmd.route.opts", FT_UINT8, BASE_HEX, NULL, 0x0,
2020                 NULL, HFILL }},
2021 
2022             { &hf_zbee_nwk_cmd_route_opt_repair,
2023             { "Route Repair",           "zbee_nwk.cmd.route.opts.repair", FT_BOOLEAN, 8, NULL,
2024                 ZBEE_NWK_CMD_ROUTE_OPTION_REPAIR,
2025                 "Flag identifying whether the route request command was to repair a failed route.", HFILL }},
2026 
2027             { &hf_zbee_nwk_cmd_route_opt_multicast,
2028             { "Multicast",              "zbee_nwk.cmd.route.opts.mcast", FT_BOOLEAN, 8, NULL,
2029                 ZBEE_NWK_CMD_ROUTE_OPTION_MCAST,
2030                 "Flag identifying this as a multicast route request.", HFILL }},
2031 
2032             { &hf_zbee_nwk_cmd_route_opt_dest_ext,
2033             { "Extended Destination",   "zbee_nwk.cmd.route.opts.dest_ext", FT_BOOLEAN, 8, NULL,
2034                 ZBEE_NWK_CMD_ROUTE_OPTION_DEST_EXT, NULL, HFILL }},
2035 
2036             { &hf_zbee_nwk_cmd_route_opt_resp_ext,
2037             { "Extended Responder",   "zbee_nwk.cmd.route.opts.resp_ext", FT_BOOLEAN, 8, NULL,
2038                 ZBEE_NWK_CMD_ROUTE_OPTION_RESP_EXT, NULL, HFILL }},
2039 
2040             { &hf_zbee_nwk_cmd_route_opt_orig_ext,
2041             { "Extended Originator",    "zbee_nwk.cmd.route.opts.orig_ext", FT_BOOLEAN, 8, NULL,
2042                 ZBEE_NWK_CMD_ROUTE_OPTION_ORIG_EXT, NULL, HFILL }},
2043 
2044             { &hf_zbee_nwk_cmd_route_opt_many_to_one,
2045             { "Many-to-One Discovery",  "zbee_nwk.cmd.route.opts.many2one", FT_UINT8, BASE_HEX,
2046                 VALS(zbee_nwk_cmd_route_many_modes), ZBEE_NWK_CMD_ROUTE_OPTION_MANY_MASK,
2047                 NULL, HFILL }},
2048 
2049             { &hf_zbee_nwk_cmd_nwk_status,
2050             { "Status Code",            "zbee_nwk.cmd.status", FT_UINT8, BASE_HEX, VALS(zbee_nwk_status_codes), 0x0,
2051                 NULL, HFILL }},
2052 
2053             { &hf_zbee_nwk_cmd_nwk_status_command_id,
2054             { "Unknown Command ID",     "zbee_nwk.cmd.status.unknown_command_id", FT_UINT8, BASE_HEX,
2055                     VALS(zbee_nwk_cmd_names), 0x0, NULL, HFILL }},
2056 
2057             { &hf_zbee_nwk_cmd_leave_rejoin,
2058             { "Rejoin",                 "zbee_nwk.cmd.leave.rejoin", FT_BOOLEAN, 8, NULL,
2059                 ZBEE_NWK_CMD_LEAVE_OPTION_REJOIN, "Flag instructing the device to rejoin the network.", HFILL }},
2060 
2061             { &hf_zbee_nwk_cmd_leave_request,
2062             { "Request",                "zbee_nwk.cmd.leave.request", FT_BOOLEAN, 8, NULL,
2063                 ZBEE_NWK_CMD_LEAVE_OPTION_REQUEST,
2064                 "Flag identifying the direction of this command. 1=Request, 0=Indication", HFILL }},
2065 
2066             { &hf_zbee_nwk_cmd_leave_children,
2067             { "Remove Children",        "zbee_nwk.cmd.leave.children", FT_BOOLEAN, 8, NULL,
2068                 ZBEE_NWK_CMD_LEAVE_OPTION_CHILDREN,
2069                 "Flag instructing the device to remove its children in addition to itself.", HFILL }},
2070 
2071             { &hf_zbee_nwk_cmd_relay_count,
2072             { "Relay Count",            "zbee_nwk.cmd.relay_count", FT_UINT8, BASE_DEC, NULL, 0x0,
2073                 "Number of relays required to route to the destination.", HFILL }},
2074 
2075             { &hf_zbee_nwk_cmd_relay_device,
2076             { "Relay Device",            "zbee_nwk.cmd.relay_device", FT_UINT16, BASE_HEX, NULL, 0x0,
2077                 NULL, HFILL }},
2078 
2079             { &hf_zbee_nwk_cmd_cinfo,
2080             { "Capability Information",  "zbee_nwk.cmd.cinfo", FT_UINT8, BASE_HEX, NULL,
2081                 0x0, NULL, HFILL }},
2082 
2083             { &hf_zbee_nwk_cmd_cinfo_alt_coord,
2084             { "Alternate Coordinator",  "zbee_nwk.cmd.cinfo.alt_coord", FT_BOOLEAN, 8, NULL,
2085                 IEEE802154_CMD_CINFO_ALT_PAN_COORD,
2086                 "Indicates that the device is able to operate as a PAN coordinator.", HFILL }},
2087 
2088             { &hf_zbee_nwk_cmd_cinfo_type,
2089             { "Full-Function Device",   "zbee_nwk.cmd.cinfo.ffd", FT_BOOLEAN, 8, NULL,
2090                 IEEE802154_CMD_CINFO_DEVICE_TYPE, NULL, HFILL }},
2091 
2092             { &hf_zbee_nwk_cmd_cinfo_power,
2093             { "AC Power",               "zbee_nwk.cmd.cinfo.power", FT_BOOLEAN, 8, NULL,
2094                 IEEE802154_CMD_CINFO_POWER_SRC, "Indicates this device is using AC/Mains power.", HFILL }},
2095 
2096             { &hf_zbee_nwk_cmd_cinfo_idle_rx,
2097             { "Rx On When Idle",        "zbee_nwk.cmd.cinfo.on_idle", FT_BOOLEAN, 8, NULL,
2098                 IEEE802154_CMD_CINFO_IDLE_RX,
2099                 "Indicates the receiver is active when the device is idle.", HFILL }},
2100 
2101             { &hf_zbee_nwk_cmd_cinfo_security,
2102             { "Security Capability",    "zbee_nwk.cmd.cinfo.security", FT_BOOLEAN, 8, NULL,
2103                 IEEE802154_CMD_CINFO_SEC_CAPABLE,
2104                 "Indicates this device is capable of performing encryption/decryption.", HFILL }},
2105 
2106             { &hf_zbee_nwk_cmd_cinfo_alloc,
2107             { "Allocate Short Address", "zbee_nwk.cmd.cinfo.alloc", FT_BOOLEAN, 8, NULL,
2108                 IEEE802154_CMD_CINFO_ALLOC_ADDR,
2109                 "Flag requesting the parent to allocate a short address for this device.", HFILL }},
2110 
2111             { &hf_zbee_nwk_cmd_rejoin_status,
2112             { "Status",                 "zbee_nwk.cmd.rejoin_status", FT_UINT8, BASE_HEX,
2113                 VALS(zbee_nwk_rejoin_codes), 0x0, NULL, HFILL }},
2114 
2115             { &hf_zbee_nwk_cmd_link_last,
2116             { "Last Frame",             "zbee_nwk.cmd.link.last", FT_BOOLEAN, 8, NULL,
2117                 ZBEE_NWK_CMD_LINK_OPTION_LAST_FRAME,
2118                 "Flag indicating the last in a series of link status commands.", HFILL }},
2119 
2120             { &hf_zbee_nwk_cmd_link_first,
2121             { "First Frame",            "zbee_nwk.cmd.link.first", FT_BOOLEAN, 8, NULL,
2122                 ZBEE_NWK_CMD_LINK_OPTION_FIRST_FRAME,
2123                 "Flag indicating the first in a series of link status commands.", HFILL }},
2124 
2125             { &hf_zbee_nwk_cmd_link_count,
2126             { "Link Status Count",      "zbee_nwk.cmd.link.count", FT_UINT8, BASE_DEC, NULL,
2127                 ZBEE_NWK_CMD_LINK_OPTION_COUNT_MASK, NULL, HFILL }},
2128 
2129             { &hf_zbee_nwk_cmd_link_address,
2130             { "Address",      "zbee_nwk.cmd.link.address", FT_UINT16, BASE_HEX, NULL,
2131                 0x0, NULL, HFILL }},
2132 
2133             { &hf_zbee_nwk_cmd_link_incoming_cost,
2134             { "Incoming Cost",      "zbee_nwk.cmd.link.incoming_cost", FT_UINT8, BASE_DEC, NULL,
2135                 ZBEE_NWK_CMD_LINK_INCOMMING_COST_MASK, NULL, HFILL }},
2136 
2137             { &hf_zbee_nwk_cmd_link_outgoing_cost,
2138             { "Outgoing Cost",      "zbee_nwk.cmd.link.outgoing_cost", FT_UINT8, BASE_DEC, NULL,
2139                 ZBEE_NWK_CMD_LINK_OUTGOING_COST_MASK, NULL, HFILL }},
2140 
2141             { &hf_zbee_nwk_cmd_report_type,
2142             { "Report Type",            "zbee_nwk.cmd.report.type", FT_UINT8, BASE_HEX,
2143                 VALS(zbee_nwk_report_types), ZBEE_NWK_CMD_NWK_REPORT_ID_MASK, NULL, HFILL }},
2144 
2145             { &hf_zbee_nwk_cmd_report_count,
2146             { "Report Information Count",   "zbee_nwk.cmd.report.count", FT_UINT8, BASE_DEC, NULL,
2147                 ZBEE_NWK_CMD_NWK_REPORT_COUNT_MASK, NULL, HFILL }},
2148 
2149             { &hf_zbee_nwk_cmd_update_type,
2150             { "Update Type",            "zbee_nwk.cmd.update.type", FT_UINT8, BASE_HEX,
2151                 VALS(zbee_nwk_update_types), ZBEE_NWK_CMD_NWK_UPDATE_ID_MASK, NULL, HFILL }},
2152 
2153             { &hf_zbee_nwk_cmd_update_count,
2154             { "Update Information Count",   "zbee_nwk.cmd.update.count", FT_UINT8, BASE_DEC, NULL,
2155                 ZBEE_NWK_CMD_NWK_UPDATE_COUNT_MASK, NULL, HFILL }},
2156 
2157             { &hf_zbee_nwk_cmd_update_id,
2158             { "Update ID",              "zbee_nwk.cmd.update.id", FT_UINT8, BASE_DEC, NULL, 0x0,
2159                 NULL, HFILL }},
2160 
2161             { &hf_zbee_nwk_panid,
2162             { "PAN ID",        "zbee_nwk.panid", FT_UINT16, BASE_HEX, NULL, 0x0,
2163                 NULL, HFILL }},
2164 
2165             { &hf_zbee_zboss_nwk_cmd_key,
2166             { "ZBOSS Key",        "zbee_nwk.zboss_key", FT_BYTES, BASE_NONE, NULL, 0x0,
2167                 NULL, HFILL }},
2168 
2169             { &hf_zbee_nwk_cmd_epid,
2170             { "Extended PAN ID",        "zbee_nwk.cmd.epid", FT_EUI64, BASE_NONE, NULL, 0x0,
2171                 NULL, HFILL }},
2172 
2173             { &hf_zbee_nwk_cmd_end_device_timeout_request_enum,
2174             { "Requested Timeout Enumeration",        "zbee_nwk.cmd.ed_tmo_req", FT_UINT8, BASE_DEC,
2175               VALS(zbee_nwk_end_device_timeout_request), 0, NULL, HFILL }},
2176 
2177             { &hf_zbee_nwk_cmd_end_device_configuration,
2178             { "End Device Configuration",        "zbee_nwk.cmd.ed_config", FT_UINT8, BASE_HEX, NULL, 0x0,
2179                 NULL, HFILL }},
2180 
2181             { &hf_zbee_nwk_cmd_end_device_timeout_resp_status,
2182             { "Status",        "zbee_nwk.cmd.ed_tmo_rsp_status", FT_UINT8, BASE_DEC,
2183               VALS(zbee_nwk_end_device_timeout_resp_status), 0, NULL, HFILL }},
2184 
2185             { &hf_zbee_nwk_cmd_end_device_timeout_resp_parent_info,
2186             { "Parent Information",        "zbee_nwk.cmd.ed_prnt_info", FT_UINT8, BASE_HEX, NULL, 0x0,
2187                 NULL, HFILL }},
2188 
2189             { &hf_zbee_nwk_cmd_prnt_info_mac_data_poll_keepalive_supported,
2190             { "MAC Data Poll Keepalive",           "zbee_nwk.cmd.ed_prnt_info.mac_data_poll_keepalive", FT_BOOLEAN, 8, NULL,
2191               ZBEE_NWK_CMD_ED_TIMEO_RSP_PRNT_INFO_MAC_DATA_POLL_KEEPAL_SUPP,
2192               NULL, HFILL }},
2193 
2194             { &hf_zbee_nwk_cmd_prnt_info_ed_to_req_keepalive_supported,
2195             { "End Device Timeout Request Keepalive",           "zbee_nwk.cmd.ed_prnt_info.ed_tmo_req_keepalive", FT_BOOLEAN, 8, NULL,
2196               ZBEE_NWK_CMD_ED_TIMEO_RSP_PRNT_INFO_ED_TIMOU_REQ_KEEPAL_SUPP,
2197               NULL, HFILL }},
2198 
2199             { &hf_zbee_nwk_cmd_prnt_info_power_negotiation_supported,
2200             { "Power Negotiation Supported",           "zbee_nwk.cmd.power_negotiation_supported", FT_BOOLEAN, 8, NULL,
2201               ZBEE_NWK_CMD_ED_TIMEO_RSP_PRNT_INFO_PWR_NEG_SUPP,
2202               NULL, HFILL }},
2203 
2204             { &hf_zbee_nwk_cmd_link_pwr_type,
2205             { "Type",        "zbee_nwk.cmd.link_pwr_delta.type", FT_UINT8, BASE_HEX,
2206                 VALS(zbee_nwk_link_power_delta_types), ZBEE_NWK_CMD_NWK_LINK_PWR_DELTA_TYPE_MASK, NULL, HFILL }},
2207 
2208             { &hf_zbee_nwk_cmd_link_pwr_list_count,
2209             { "Structure Count",        "zbee_nwk.cmd.link_pwr_delta.list_count", FT_UINT8, BASE_DEC, NULL, 0x0,
2210               NULL, HFILL }},
2211 
2212             { &hf_zbee_nwk_cmd_link_pwr_device_address,
2213             { "Device Address",      "zbee_nwk.cmd.link_pwr_delta.address", FT_UINT16, BASE_HEX, NULL,
2214                 0x0, NULL, HFILL }},
2215 
2216             { &hf_zbee_nwk_cmd_link_pwr_power_delta,
2217             { "Power Delta",         "zbee_nwk.cmd.link_pwr_delta.power_delta", FT_INT8, BASE_DEC, NULL, 0x0,
2218                     NULL, HFILL }},
2219 
2220             { &hf_zbee_beacon_protocol,
2221             { "Protocol ID",            "zbee_beacon.protocol", FT_UINT8, BASE_DEC, NULL, 0x0,
2222                 NULL, HFILL }},
2223 
2224             { &hf_zbee_beacon_stack_profile,
2225             { "Stack Profile",          "zbee_beacon.profile", FT_UINT16, BASE_HEX,
2226                 VALS(zbee_nwk_stack_profiles), ZBEE_NWK_BEACON_STACK_PROFILE, NULL, HFILL }},
2227 
2228             { &hf_zbee_beacon_version,
2229             { "Protocol Version",       "zbee_beacon.version", FT_UINT16, BASE_DEC, NULL, ZBEE_NWK_BEACON_PROTOCOL_VERSION,
2230                 NULL, HFILL }},
2231 
2232             { &hf_zbee_beacon_router_capacity,
2233             { "Router Capacity", "zbee_beacon.router", FT_BOOLEAN, 16, NULL, ZBEE_NWK_BEACON_ROUTER_CAPACITY,
2234                 "Whether the device can accept join requests from routing capable devices.", HFILL }},
2235 
2236             { &hf_zbee_beacon_depth,
2237             { "Device Depth",           "zbee_beacon.depth", FT_UINT16, BASE_DEC, NULL, ZBEE_NWK_BEACON_NETWORK_DEPTH,
2238                 "The tree depth of the device, 0 indicates the network coordinator.", HFILL }},
2239 
2240             { &hf_zbee_beacon_end_device_capacity,
2241             { "End Device Capacity",        "zbee_beacon.end_dev", FT_BOOLEAN, 16, NULL, ZBEE_NWK_BEACON_END_DEVICE_CAPACITY,
2242                 "Whether the device can accept join requests from ZigBee end devices.", HFILL }},
2243 
2244             { &hf_zbee_beacon_epid,
2245             { "Extended PAN ID",        "zbee_beacon.ext_panid", FT_EUI64, BASE_NONE, NULL, 0x0,
2246                 "Extended PAN identifier.", HFILL }},
2247 
2248             { &hf_zbee_beacon_tx_offset,
2249             { "Tx Offset",              "zbee_beacon.tx_offset", FT_UINT24, BASE_DEC, NULL, 0x0,
2250                 "The time difference between a device and its parent's beacon.", HFILL }},
2251 
2252             { &hf_zbee_beacon_update_id,
2253             { "Update ID",              "zbee_beacon.update_id", FT_UINT8, BASE_DEC, NULL, 0x0,
2254                 NULL, HFILL }},
2255 
2256             { &hf_zbip_beacon_allow_join,
2257             { "Allow Join",             "zbip_beacon.allow_join", FT_BOOLEAN, 8, NULL, ZBEE_IP_BEACON_ALLOW_JOIN,
2258                 NULL, HFILL }},
2259 
2260             { &hf_zbip_beacon_router_capacity,
2261             { "Router Capacity",        "zbip_beacon.router", FT_BOOLEAN, 8, NULL, ZBEE_IP_BEACON_ROUTER_CAPACITY,
2262                 "Whether this device can accept new routers on the network.", HFILL }},
2263 
2264             { &hf_zbip_beacon_host_capacity,
2265             { "Host Capacity",        "zbip_beacon.host", FT_BOOLEAN, 8, NULL, ZBEE_IP_BEACON_HOST_CAPACITY,
2266                 "Whether this device can accept new host on the network.", HFILL }},
2267 
2268             { &hf_zbip_beacon_unsecure,
2269             { "Unsecure Network",     "zbip_beacon.unsecure", FT_BOOLEAN, 8, NULL, ZBEE_IP_BEACON_UNSECURE,
2270                 "Indicates that this network is not using link layer security.", HFILL }},
2271 
2272             { &hf_zbip_beacon_network_id,
2273             { "Network ID",           "zbip_beacon.network_id", FT_STRING, BASE_NONE, NULL, 0x0,
2274                 "A string that uniquely identifies this network.", HFILL }},
2275 
2276             { &hf_ieee802154_zigbee_ie,
2277             { "IE header",                       "zbee_nwk.zigbee_ie", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2278 
2279             { &hf_ieee802154_zigbee_ie_id,
2280             { "Id",                              "zbee_nwk.zigbee_ie.id", FT_UINT16, BASE_HEX, VALS(ieee802154_zigbee_ie_names),
2281                     ZBEE_ZIGBEE_IE_ID_MASK, NULL, HFILL }},
2282 
2283             { &hf_ieee802154_zigbee_ie_length,
2284             { "Length",                           "zbee_nwk.zigbee_ie.length", FT_UINT16, BASE_DEC, NULL,
2285                     ZBEE_ZIGBEE_IE_LENGTH_MASK, NULL, HFILL }},
2286 
2287             { &hf_ieee802154_zigbee_ie_tx_power,
2288             { "Tx Power (dBm)",                  "zbee_nwk.zigbee_ie.tx_power", FT_INT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2289 
2290             { &hf_ieee802154_zigbee_ie_source_addr,
2291             { "Source Address",                  "zbee_nwk.zigbee_ie.source_address", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2292 
2293 
2294             { &hf_ieee802154_zigbee_rejoin_epid,
2295             { "Extended PAN ID",                "zbee_nwk.zigbee_rejoin.ext_panid", FT_EUI64, BASE_NONE, NULL, 0x0,
2296                 "Extended PAN identifier", HFILL }},
2297 
2298             { &hf_ieee802154_zigbee_rejoin_source_addr,
2299             { "Source Address",                  "zbee_nwk.zigbee_rejoin.source_address", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2300 
2301 
2302     };
2303 
2304     /*  NWK Layer subtrees */
2305     static gint *ett[] = {
2306         &ett_zbee_nwk,
2307         &ett_zbee_nwk_beacon,
2308         &ett_zbee_nwk_fcf,
2309         &ett_zbee_nwk_fcf_ext,
2310         &ett_zbee_nwk_mcast,
2311         &ett_zbee_nwk_route,
2312         &ett_zbee_nwk_cmd,
2313         &ett_zbee_nwk_cmd_options,
2314         &ett_zbee_nwk_cmd_cinfo,
2315         &ett_zbee_nwk_cmd_link,
2316         &ett_zbee_nwk_cmd_ed_to_rsp_prnt_info,
2317         &ett_zbee_nwk_cmd_link_pwr_struct,
2318         &ett_zbee_nwk_zigbee_ie_fields,
2319         &ett_zbee_nwk_ie_rejoin,
2320         &ett_zbee_nwk_header,
2321         &ett_zbee_nwk_header_ie,
2322         &ett_zbee_nwk_beacon_bitfield,
2323     };
2324 
2325     static ei_register_info ei[] = {
2326         { &ei_zbee_nwk_missing_payload, { "zbee_nwk.missing_payload", PI_MALFORMED, PI_ERROR, "Missing Payload", EXPFILL }},
2327     };
2328 
2329     expert_module_t* expert_zbee_nwk;
2330 
2331     register_init_routine(proto_init_zbee_nwk);
2332     register_cleanup_routine(proto_cleanup_zbee_nwk);
2333 
2334     /* Register the protocol with Wireshark. */
2335     proto_zbee_nwk = proto_register_protocol("ZigBee Network Layer", "ZigBee", ZBEE_PROTOABBREV_NWK);
2336     proto_zbee_beacon = proto_register_protocol("ZigBee Beacon", "ZigBee Beacon", "zbee_beacon");
2337     proto_zbip_beacon = proto_register_protocol("ZigBee IP Beacon", "ZigBee IP Beacon", "zbip_beacon");
2338     proto_zbee_ie = proto_register_protocol("ZigBee IE", "ZigBee IE", "zbee_ie");
2339     proto_register_field_array(proto_zbee_nwk, hf, array_length(hf));
2340     proto_register_subtree_array(ett, array_length(ett));
2341 
2342     expert_zbee_nwk = expert_register_protocol(proto_zbee_nwk);
2343     expert_register_field_array(expert_zbee_nwk, ei, array_length(ei));
2344 
2345     /* Register the dissectors with Wireshark. */
2346     register_dissector(ZBEE_PROTOABBREV_NWK, dissect_zbee_nwk, proto_zbee_nwk);
2347     register_dissector("zbee_beacon", dissect_zbee_beacon, proto_zbee_beacon);
2348     register_dissector("zbip_beacon", dissect_zbip_beacon, proto_zbip_beacon);
2349     register_dissector("zbee_ie", dissect_zbee_ie, proto_zbee_ie);
2350 
2351     zbee_nwk_address_type = address_type_dissector_register("AT_ZIGBEE", "ZigBee 16-bit address",
2352             zbee_nwk_address_to_str, zbee_nwk_address_str_len, NULL, NULL, zbee_nwk_address_len, NULL, NULL);
2353 
2354     /* Register the Security dissector. */
2355     zbee_security_register(NULL, proto_zbee_nwk);
2356 
2357     zbee_nwk_tap = register_tap(ZBEE_PROTOABBREV_NWK);
2358 
2359     register_conversation_table(proto_zbee_nwk, TRUE, zbee_nwk_conversation_packet, zbee_nwk_hostlist_packet);
2360     register_conversation_filter(ZBEE_PROTOABBREV_NWK, "ZigBee Network Layer", zbee_nwk_filter_valid, zbee_nwk_build_filter);
2361 } /* proto_register_zbee_nwk */
2362 
2363 /**
2364  *Registers the zigbee dissector with Wireshark.
2365  *
2366 */
proto_reg_handoff_zbee_nwk(void)2367 void proto_reg_handoff_zbee_nwk(void)
2368 {
2369     /* Find the other dissectors we need. */
2370     aps_handle      = find_dissector_add_dependency(ZBEE_PROTOABBREV_APS, proto_zbee_nwk);
2371     zbee_gp_handle  = find_dissector_add_dependency(ZBEE_PROTOABBREV_NWK_GP, proto_zbee_nwk);
2372 
2373     /* Register our dissector with IEEE 802.15.4 */
2374     dissector_add_for_decode_as(IEEE802154_PROTOABBREV_WPAN_PANID, find_dissector(ZBEE_PROTOABBREV_NWK));
2375     heur_dissector_add(IEEE802154_PROTOABBREV_WPAN_BEACON, dissect_zbee_beacon_heur, "ZigBee Beacon", "zbee_wpan_beacon", proto_zbee_beacon, HEURISTIC_ENABLE);
2376     heur_dissector_add(IEEE802154_PROTOABBREV_WPAN_BEACON, dissect_zbip_beacon_heur, "ZigBee IP Beacon", "zbip_wpan_beacon", proto_zbip_beacon, HEURISTIC_ENABLE);
2377     heur_dissector_add(IEEE802154_PROTOABBREV_WPAN, dissect_zbee_nwk_heur, "ZigBee Network Layer over IEEE 802.15.4", "zbee_nwk_wpan", proto_zbee_nwk, HEURISTIC_ENABLE);
2378 } /* proto_reg_handoff_zbee */
2379 
free_keyring_key(gpointer key)2380 static void free_keyring_key(gpointer key)
2381 {
2382     g_free(key);
2383 }
2384 
free_keyring_val(gpointer a)2385 static void free_keyring_val(gpointer a)
2386 {
2387     GSList **slist = (GSList **)a;
2388     g_slist_free_full(*slist, g_free);
2389     g_free(slist);
2390 }
2391 
2392 /**
2393  *Init routine for the nwk dissector. Creates a
2394  *
2395 */
2396 static void
proto_init_zbee_nwk(void)2397 proto_init_zbee_nwk(void)
2398 {
2399     zbee_nwk_map.short_table = g_hash_table_new(ieee802154_short_addr_hash, ieee802154_short_addr_equal);
2400     zbee_nwk_map.long_table = g_hash_table_new(ieee802154_long_addr_hash, ieee802154_long_addr_equal);
2401     zbee_table_nwk_keyring  = g_hash_table_new_full(g_int_hash, g_int_equal, free_keyring_key, free_keyring_val);
2402 } /* proto_init_zbee_nwk */
2403 
2404 static void
proto_cleanup_zbee_nwk(void)2405 proto_cleanup_zbee_nwk(void)
2406 {
2407     g_hash_table_destroy(zbee_nwk_map.short_table);
2408     g_hash_table_destroy(zbee_nwk_map.long_table);
2409     g_hash_table_destroy(zbee_table_nwk_keyring);
2410 }
2411 
2412 /*
2413  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
2414  *
2415  * Local variables:
2416  * c-basic-offset: 4
2417  * tab-width: 8
2418  * indent-tabs-mode: nil
2419  * End:
2420  *
2421  * vi: set shiftwidth=4 tabstop=8 expandtab:
2422  * :indentSize=4:tabSize=8:noTabs=true:
2423  */
2424