Remove check_col() guard
[metze/wireshark/wip.git] / epan / dissectors / packet-zbee-nwk.c
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  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /*  Include Files */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif /* HAVEHCONFIG_H */
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <gmodule.h>
36 #include <epan/proto.h>
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <epan/addr_resolv.h>
40 #include <epan/expert.h>
41 #include <epan/value_string.h>
42
43 #include "packet-zbee.h"
44 #include "packet-zbee-security.h"
45 #include "packet-zbee-nwk.h"
46
47 /*************************/
48 /* Function Declarations */
49 /*************************/
50 /* Dissector Routines */
51 static void        dissect_zbee_nwk        (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
52 static void        dissect_zbee_nwk_cmd    (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
53 static void        dissect_zbee_beacon     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
54
55 /* Command Dissector Helpers */
56 static guint       dissect_zbee_nwk_route_req  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset);
57 static guint       dissect_zbee_nwk_route_rep  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
58 static guint       dissect_zbee_nwk_status     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
59 static guint       dissect_zbee_nwk_leave      (tvbuff_t *tvb, proto_tree *tree, guint offset);
60 static guint       dissect_zbee_nwk_route_rec  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset);
61 static guint       dissect_zbee_nwk_rejoin_req (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset);
62 static guint       dissect_zbee_nwk_rejoin_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset);
63 static guint       dissect_zbee_nwk_link_status(tvbuff_t *tvb, proto_tree *tree, guint offset);
64 static guint       dissect_zbee_nwk_report     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
65 static guint       dissect_zbee_nwk_update     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset);
66
67
68 /********************/
69 /* Global Variables */
70 /********************/
71 static int proto_zbee_nwk = -1;
72 static int hf_zbee_nwk_frame_type = -1;
73 static int hf_zbee_nwk_proto_version = -1;
74 static int hf_zbee_nwk_discover_route = -1;
75 static int hf_zbee_nwk_multicast = -1;
76 static int hf_zbee_nwk_security = -1;
77 static int hf_zbee_nwk_source_route = -1;
78 static int hf_zbee_nwk_ext_dst = -1;
79 static int hf_zbee_nwk_ext_src = -1;
80 static int hf_zbee_nwk_dst = -1;
81 static int hf_zbee_nwk_src = -1;
82 static int hf_zbee_nwk_radius = -1;
83 static int hf_zbee_nwk_seqno = -1;
84 static int hf_zbee_nwk_mcast_mode = -1;
85 static int hf_zbee_nwk_mcast_radius = -1;
86 static int hf_zbee_nwk_mcast_max_radius = -1;
87 static int hf_zbee_nwk_dst64 = -1;
88 static int hf_zbee_nwk_src64 = -1;
89 static int hf_zbee_nwk_relay_count = -1;
90 static int hf_zbee_nwk_relay_index = -1;
91
92 static int hf_zbee_nwk_cmd_id = -1;
93 static int hf_zbee_nwk_cmd_addr = -1;
94 static int hf_zbee_nwk_cmd_route_id = -1;
95 static int hf_zbee_nwk_cmd_route_dest = -1;
96 static int hf_zbee_nwk_cmd_route_orig = -1;
97 static int hf_zbee_nwk_cmd_route_resp = -1;
98 static int hf_zbee_nwk_cmd_route_dest_ext = -1;
99 static int hf_zbee_nwk_cmd_route_orig_ext = -1;
100 static int hf_zbee_nwk_cmd_route_resp_ext = -1;
101 static int hf_zbee_nwk_cmd_route_cost = -1;
102 static int hf_zbee_nwk_cmd_route_opt_repair = -1;
103 static int hf_zbee_nwk_cmd_route_opt_multicast = -1;
104 static int hf_zbee_nwk_cmd_route_opt_dest_ext = -1;
105 static int hf_zbee_nwk_cmd_route_opt_resp_ext = -1;
106 static int hf_zbee_nwk_cmd_route_opt_orig_ext = -1;
107 static int hf_zbee_nwk_cmd_route_opt_many_to_one = -1;
108 static int hf_zbee_nwk_cmd_nwk_status = -1;
109 static int hf_zbee_nwk_cmd_leave_rejoin = -1;
110 static int hf_zbee_nwk_cmd_leave_request = -1;
111 static int hf_zbee_nwk_cmd_leave_children = -1;
112 static int hf_zbee_nwk_cmd_relay_count = -1;
113 static int hf_zbee_nwk_cmd_cinfo_alt_coord = -1;
114 static int hf_zbee_nwk_cmd_cinfo_type = -1;
115 static int hf_zbee_nwk_cmd_cinfo_power = -1;
116 static int hf_zbee_nwk_cmd_cinfo_idle_rx = -1;
117 static int hf_zbee_nwk_cmd_cinfo_security = -1;
118 static int hf_zbee_nwk_cmd_cinfo_alloc = -1;
119 static int hf_zbee_nwk_cmd_rejoin_status = -1;
120 static int hf_zbee_nwk_cmd_link_last = -1;
121 static int hf_zbee_nwk_cmd_link_first = -1;
122 static int hf_zbee_nwk_cmd_link_count = -1;
123 static int hf_zbee_nwk_cmd_report_type = -1;
124 static int hf_zbee_nwk_cmd_report_count = -1;
125 static int hf_zbee_nwk_cmd_update_type = -1;
126 static int hf_zbee_nwk_cmd_update_count = -1;
127 static int hf_zbee_nwk_cmd_update_id = -1;
128 static int hf_zbee_nwk_cmd_epid = -1;
129
130 /*  ZigBee Beacons */
131 static int hf_zbee_beacon_protocol = -1;
132 static int hf_zbee_beacon_stack_profile = -1;
133 static int hf_zbee_beacon_version = -1;
134 static int hf_zbee_beacon_router_capacity = -1;
135 static int hf_zbee_beacon_depth = -1;
136 static int hf_zbee_beacon_end_device_capacity = -1;
137 static int hf_zbee_beacon_epid = -1;
138 static int hf_zbee_beacon_tx_offset = -1;
139 static int hf_zbee_beacon_update_id = -1;
140
141 static gint ett_zbee_nwk = -1;
142 static gint ett_zbee_beacon = -1;
143 static gint ett_zbee_nwk_fcf = -1;
144 static gint ett_zbee_nwk_mcast = -1;
145 static gint ett_zbee_nwk_route = -1;
146 static gint ett_zbee_nwk_cmd = -1;
147 static gint ett_zbee_nwk_cmd_options = -1;
148 static gint ett_zbee_nwk_cmd_cinfo = -1;
149
150 static dissector_handle_t   data_handle;
151 static dissector_handle_t   aps_handle;
152
153 /********************/
154 /* Field Names      */
155 /********************/
156 /* Frame Types */
157 static const value_string zbee_nwk_frame_types[] = {
158     { ZBEE_NWK_FCF_DATA,    "Data" },
159     { ZBEE_NWK_FCF_CMD,     "Command" },
160     { 0, NULL }
161 };
162
163 /* Route Discovery Modes */
164 static const value_string zbee_nwk_discovery_modes[] = {
165     { ZBEE_NWK_FCF_DISCOVERY_SUPPRESS,  "Suppress" },
166     { ZBEE_NWK_FCF_DISCOVERY_ENABLE,    "Enable" },
167     { ZBEE_NWK_FCF_DISCOVERY_FORCE,     "Force" },
168     { 0, NULL }
169 };
170
171 /* Command Names*/
172 static const value_string zbee_nwk_cmd_names[] = {
173     { ZBEE_NWK_CMD_ROUTE_REQ,       "Route Request" },
174     { ZBEE_NWK_CMD_ROUTE_REPLY,     "Route Reply" },
175     { ZBEE_NWK_CMD_NWK_STATUS,      "Network Status" },
176     { ZBEE_NWK_CMD_LEAVE,           "Leave" },
177     { ZBEE_NWK_CMD_ROUTE_RECORD,    "Route Record" },
178     { ZBEE_NWK_CMD_REJOIN_REQ,      "Rejoin Request" },
179     { ZBEE_NWK_CMD_REJOIN_RESP,     "Rejoin Response" },
180     { ZBEE_NWK_CMD_LINK_STATUS,     "Link Status" },
181     { ZBEE_NWK_CMD_NWK_REPORT,      "Network Report" },
182     { ZBEE_NWK_CMD_NWK_UPDATE,      "Network Update" },
183     { 0, NULL }
184 };
185
186 /* Many-To-One Route Discovery Modes. */
187 static const value_string zbee_nwk_cmd_route_many_modes[] = {
188     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_NONE,  "Not Many-to-One" },
189     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_REC,   "With Source Routing" },
190     { ZBEE_NWK_CMD_ROUTE_OPTION_MANY_NOREC, "Without Source Routing" },
191     { 0, NULL }
192 };
193
194 /* Rejoin Status Codes */
195 static const value_string zbee_nwk_rejoin_codes[] = {
196     { IEEE802154_CMD_ASRSP_AS_SUCCESS,      "Success" },
197     { IEEE802154_CMD_ASRSP_PAN_FULL,        "PAN Full" },
198     { IEEE802154_CMD_ASRSP_PAN_DENIED,      "PAN Access Denied" },
199     { 0, NULL }
200 };
201
202 /* Network Report Types */
203 static const value_string zbee_nwk_report_types[] = {
204     { ZBEE_NWK_CMD_NWK_REPORT_ID_PAN_CONFLICT,  "PAN Identifier Conflict" },
205     { 0, NULL }
206 };
207
208 /* Network Update Types */
209 static const value_string zbee_nwk_update_types[] = {
210     { ZBEE_NWK_CMD_NWK_UPDATE_ID_PAN_UPDATE,  "PAN Identifier Update" },
211     { 0, NULL }
212 };
213
214 /* Network Status Codes */
215 static const value_string zbee_nwk_status_codes[] = {
216     { ZBEE_NWK_STATUS_NO_ROUTE_AVAIL,       "No Route Available" },
217     { ZBEE_NWK_STATUS_TREE_LINK_FAIL,       "Tree Link Failure" },
218     { ZBEE_NWK_STATUS_NON_TREE_LINK_FAIL,   "Non-tree Link Failure" },
219     { ZBEE_NWK_STATUS_LOW_BATTERY,          "Low Battery" },
220     { ZBEE_NWK_STATUS_NO_ROUTING,           "No Routing Capacity" },
221     { ZBEE_NWK_STATUS_NO_INDIRECT,          "No Indirect Capacity" },
222     { ZBEE_NWK_STATUS_INDIRECT_EXPIRE,      "Indirect Transaction Expiry" },
223     { ZBEE_NWK_STATUS_DEVICE_UNAVAIL,       "Target Device Unavailable" },
224     { ZBEE_NWK_STATUS_ADDR_UNAVAIL,         "Target Address Unallocated" },
225     { ZBEE_NWK_STATUS_PARENT_LINK_FAIL,     "Parent Link Failure" },
226     { ZBEE_NWK_STATUS_VALIDATE_ROUTE,       "Validate Route" },
227     { ZBEE_NWK_STATUS_SOURCE_ROUTE_FAIL,    "Source Route Failure" },
228     { ZBEE_NWK_STATUS_MANY_TO_ONE_FAIL,     "Many-to-One Route Failure" },
229     { ZBEE_NWK_STATUS_ADDRESS_CONFLICT,     "Address Conflict" },
230     { ZBEE_NWK_STATUS_VERIFY_ADDRESS,       "Verify Address" },
231     { ZBEE_NWK_STATUS_PANID_UPDATE,         "PAN ID Update" },
232     { ZBEE_NWK_STATUS_ADDRESS_UPDATE,       "Network Address Update" },
233     { ZBEE_NWK_STATUS_BAD_FRAME_COUNTER,    "Bad Frame Counter" },
234     { ZBEE_NWK_STATUS_BAD_KEY_SEQNO,        "Bad Key Sequence Number" },
235     { 0, NULL }
236 };
237
238 /* Stack Profile Values. */
239 static const value_string zbee_nwk_stack_profiles[] = {
240     { 0x00, "Network Specific" },
241     { 0x01, "ZigBee Home" },
242     { 0x02, "ZigBee PRO" },
243     { 0, NULL }
244 };
245
246 /*FUNCTION:------------------------------------------------------
247  *  NAME
248  *      proto_tree_add_eui64
249  *  DESCRIPTION
250  *      Helper function to display an EUI-64 address to the tree.
251  *  PARAMETERS
252  *      proto_tree  *tree
253  *      int         hfindex
254  *      tvbuff_t    *tvb
255  *      gint        start
256  *      gint        length
257  *      guint64     value;
258  *  RETURNS
259  *      proto_item *
260  *---------------------------------------------------------------
261  */
262 proto_item *
263 proto_tree_add_eui64(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start, gint length, gint64 value)
264 {
265     header_field_info *hf = proto_registrar_get_nth(hfindex);
266     return proto_tree_add_uint64_format(tree, hfindex, tvb, start, length, value, "%s: %s (%s)", hf->name, print_eui64_oui(value), print_eui64(value));
267 }
268
269 /*FUNCTION:------------------------------------------------------
270  *  NAME
271  *      zbee_get_bit_field
272  *  DESCRIPTION
273  *      Extracts an integer sub-field from an int with a given mask
274  *      if the mask is 0, this will return 0, if the mask is non-
275  *      continuous the output is undefined.
276  *  PARAMETERS
277  *      guint       input
278  *      guint       mask
279  *  RETURNS
280  *      guint
281  *---------------------------------------------------------------
282  */
283 guint
284 zbee_get_bit_field(guint input, guint mask)
285 {
286     /* Sanity Check, don't want infinite loops. */
287     if (mask == 0) return 0;
288     /* Shift input and mask together. */
289     while (!(mask & 0x1)) {
290         input >>= 1;
291         mask >>=1;
292     } /* while */
293     return (input & mask);
294 } /* zbee_get_bit_field */
295
296 /*FUNCTION:------------------------------------------------------
297  *  NAME
298  *      dissect_zbee_nwk_heur
299  *  DESCRIPTION
300  *      Heuristic interpreter for the ZigBee network dissectors.
301  *  PARAMETERS
302  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
303  *      packet_into *pinfo  - pointer to packet information fields
304  *      proto_tree *tree    - pointer to data tree Wireshark uses to display packet.
305  *  RETURNS
306  *      Boolean value, whether it handles the packet or not.
307  *---------------------------------------------------------------
308  */
309 static gboolean
310 dissect_zbee_nwk_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
311 {
312     ieee802154_packet   *packet = pinfo->private_data;
313
314     /* All ZigBee frames must always have a 16-bit source address. */
315     if (packet->src_addr_mode != IEEE802154_FCF_ADDR_SHORT) {
316         return FALSE;
317     }
318     /* ZigBee MAC frames must always contain a 16-bit destination address. */
319     if ( (packet->frame_type == IEEE802154_FCF_DATA) &&
320          (packet->dst_addr_mode == IEEE802154_FCF_ADDR_SHORT) ) {
321         dissect_zbee_nwk(tvb, pinfo, tree);
322         return TRUE;
323     }
324     /* ZigBee MAC Beacons must have the first byte (protocol ID) equal to the
325      * ZigBee protocol ID. */
326     if ( (packet->frame_type == IEEE802154_FCF_BEACON) &&
327          (tvb_get_guint8(tvb, 0) == ZBEE_NWK_BEACON_PROCOL_ID) ) {
328         dissect_zbee_beacon(tvb, pinfo, tree);
329         return TRUE;
330     }
331     /* If we get this far, then this packet did not meet the requirements for
332      * a ZigBee frame.
333      */
334     return FALSE;
335 } /* dissect_zbee_heur */
336
337 /*FUNCTION:------------------------------------------------------
338  *  NAME
339  *      dissect_zbee_nwk
340  *  DESCRIPTION
341  *      ZigBee packet dissection routine for Wireshark.
342  *  PARAMETERS
343  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
344  *      packet_into *pinfo  - pointer to packet information fields
345  *      proto_tree *tree    - pointer to data tree Wireshark uses to display packet.
346  *  RETURNS
347  *      void
348  *---------------------------------------------------------------
349  */
350 static void
351 dissect_zbee_nwk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
352 {
353     tvbuff_t            *payload_tvb = NULL;
354
355     proto_item          *proto_root = NULL;
356     proto_item          *ti = NULL;
357     proto_tree          *nwk_tree = NULL;
358     proto_tree          *field_tree = NULL;
359
360     zbee_nwk_packet     packet;
361
362     guint               offset = 0;
363     gchar               *src_addr = ep_alloc(32);
364     gchar               *dst_addr = ep_alloc(32);
365
366     guint16             fcf;
367     memset(&packet, 0, sizeof(packet));
368
369     /* Add ourself to the protocol column, clear the info column, and create the protocol tree. */
370     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZigBee");
371     col_clear(pinfo->cinfo, COL_INFO);
372     if (tree) {
373         proto_root = proto_tree_add_protocol_format(tree, proto_zbee_nwk, tvb, offset, tvb_length(tvb), "ZigBee Network Layer");
374         nwk_tree = proto_item_add_subtree(proto_root, ett_zbee_nwk);
375     }
376
377     /* Get and parse the FCF */
378     fcf = tvb_get_letohs(tvb, offset);
379     packet.type         = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_FRAME_TYPE);
380     packet.version      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_VERSION);
381     packet.discovery    = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_DISCOVER_ROUTE);
382     packet.security     = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_SECURITY);
383     packet.multicast    = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_MULTICAST);
384     packet.route        = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_SOURCE_ROUTE);
385     packet.ext_dst      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_EXT_DEST);
386     packet.ext_src      = zbee_get_bit_field(fcf, ZBEE_NWK_FCF_EXT_SOURCE);
387     pinfo->zbee_stack_vers = packet.version;
388
389     /* Display the FCF. */
390     if (tree) {
391         /* Create a subtree for the FCF. */
392         ti = proto_tree_add_text(nwk_tree, tvb, offset, sizeof(guint16), "Frame Control Field: %s (0x%04x)",
393                 val_to_str(packet.type, zbee_nwk_frame_types, "Unknown"), fcf);
394         field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_fcf);
395         proto_tree_add_uint(field_tree, hf_zbee_nwk_frame_type, tvb, offset, sizeof(guint8), fcf & ZBEE_NWK_FCF_FRAME_TYPE);
396
397         /*  Add the rest of the fcf fields to the subtree */
398         proto_tree_add_uint(field_tree, hf_zbee_nwk_proto_version, tvb, offset, sizeof(guint8), fcf & ZBEE_NWK_FCF_VERSION);
399         proto_tree_add_uint(field_tree, hf_zbee_nwk_discover_route, tvb, offset, sizeof(guint8), fcf & ZBEE_NWK_FCF_DISCOVER_ROUTE);
400         if (pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) {
401             proto_tree_add_boolean(field_tree, hf_zbee_nwk_multicast, tvb, offset+sizeof(guint8), sizeof(guint8), fcf & ZBEE_NWK_FCF_MULTICAST);
402         }
403         proto_tree_add_boolean(field_tree, hf_zbee_nwk_security, tvb, offset+sizeof(guint8), sizeof(guint8), fcf & ZBEE_NWK_FCF_SECURITY);
404         if (pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) {
405             proto_tree_add_boolean(field_tree, hf_zbee_nwk_source_route, tvb, offset+sizeof(guint8), sizeof(guint8), fcf & ZBEE_NWK_FCF_SOURCE_ROUTE);
406             proto_tree_add_boolean(field_tree, hf_zbee_nwk_ext_dst, tvb, offset+sizeof(guint8), sizeof(guint8), fcf & ZBEE_NWK_FCF_EXT_DEST);
407             proto_tree_add_boolean(field_tree, hf_zbee_nwk_ext_src, tvb, offset+sizeof(guint8), sizeof(guint8), fcf & ZBEE_NWK_FCF_EXT_SOURCE);
408         }
409     }
410     offset += sizeof(guint16);
411
412     /* Add the frame type to the info column and protocol root. */
413     if (tree) {
414         proto_item_append_text(proto_root, " %s", val_to_str(packet.type, zbee_nwk_frame_types, "Unknown Type"));
415     }
416     if (check_col(pinfo->cinfo, COL_INFO)) {
417         col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet.type, zbee_nwk_frame_types, "Reserved Frame Type"));
418     }
419
420     /* Get the destination address. */
421     packet.dst = tvb_get_letohs(tvb, offset);
422     if (tree) {
423         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_dst, tvb, offset, sizeof(guint16), packet.dst);
424     }
425     offset += sizeof(guint16);
426
427     /* Display the destination address. */
428     if (   (packet.dst == ZBEE_BCAST_ALL)
429         || (packet.dst == ZBEE_BCAST_ACTIVE)
430         || (packet.dst == ZBEE_BCAST_ROUTERS)){
431         g_snprintf(dst_addr, 32, "Broadcast");
432     }
433     else g_snprintf(dst_addr, 32, "0x%04x", packet.dst);
434     SET_ADDRESS(&pinfo->dst, AT_STRINGZ, (int)strlen(dst_addr)+1, dst_addr);
435     SET_ADDRESS(&pinfo->net_dst, AT_STRINGZ, (int)strlen(dst_addr)+1, dst_addr);
436     if (tree) {
437         proto_item_append_text(proto_root, ", Dst: %s", dst_addr);
438     }
439     if (check_col(pinfo->cinfo, COL_INFO) && (packet.type == ZBEE_NWK_FCF_DATA)) {
440         col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: %s", dst_addr);
441     }
442
443     /* Get the source address. */
444     packet.src = tvb_get_letohs(tvb, offset);
445     if(tree){
446         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_src, tvb, offset, sizeof(guint16), packet.src);
447     }
448     offset += sizeof(guint16);
449
450     /* Display the source address. */
451     if (   (packet.src == ZBEE_BCAST_ALL)
452         || (packet.src == ZBEE_BCAST_ACTIVE)
453         || (packet.src == ZBEE_BCAST_ROUTERS)){
454         /* Source Broadcast doesn't make much sense. */
455         g_snprintf(src_addr, 32, "Broadcast");
456     }
457     else g_snprintf(src_addr, 32, "0x%04x", packet.src);
458     SET_ADDRESS(&pinfo->src, AT_STRINGZ, (int)strlen(src_addr)+1, src_addr);
459     SET_ADDRESS(&pinfo->net_src, AT_STRINGZ, (int)strlen(src_addr)+1, src_addr);
460     if (tree) {
461         proto_item_append_text(proto_root, ", Src: %s", src_addr);
462     }
463     if (check_col(pinfo->cinfo, COL_INFO) && (packet.type == ZBEE_NWK_FCF_DATA)){
464         col_append_fstr(pinfo->cinfo, COL_INFO, ", Src: %s", src_addr);
465     }
466
467     /* Get and display the radius. */
468     packet.radius = tvb_get_guint8(tvb, offset);
469     if (tree) {
470         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_radius, tvb, offset, sizeof(guint8), packet.radius);
471     }
472     offset += sizeof(guint8);
473
474     /* Get and display the sequence number. */
475     packet.seqno = tvb_get_guint8(tvb, offset);
476     if (tree) {
477         proto_tree_add_uint(nwk_tree, hf_zbee_nwk_seqno, tvb, offset, sizeof(guint8), packet.seqno);
478     }
479     offset += sizeof(guint8);
480
481     /* Add Multicast control field. (ZigBee 2006 and later). */
482     if ((pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) && packet.multicast) {
483         guint8  mcast_control = tvb_get_guint8(tvb, offset);
484
485         packet.mcast_mode       = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_MODE);
486         packet.mcast_radius     = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_RADIUS);
487         packet.mcast_max_radius = zbee_get_bit_field(mcast_control, ZBEE_NWK_MCAST_MAX_RADIUS);
488         if (tree) {
489             /* Create a subtree for the multicast control field. */
490             ti = proto_tree_add_text(nwk_tree, tvb, offset, sizeof(guint8), "Multicast Control Field");
491             field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_mcast);
492             /* Add the fields. */
493             ti = proto_tree_add_uint(field_tree, hf_zbee_nwk_mcast_mode, tvb, offset, sizeof(guint8), mcast_control & ZBEE_NWK_MCAST_MODE);
494             proto_tree_add_uint(field_tree, hf_zbee_nwk_mcast_radius, tvb, offset, sizeof(guint8), mcast_control & ZBEE_NWK_MCAST_RADIUS);
495             proto_tree_add_uint(field_tree, hf_zbee_nwk_mcast_max_radius, tvb, offset, sizeof(guint8), mcast_control & ZBEE_NWK_MCAST_MAX_RADIUS);
496         }
497         offset += sizeof(guint8);
498     }
499
500     /* Add the extended destination address (ZigBee 2006 and later). */
501     if ((pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) && packet.ext_dst) {
502         packet.dst64 = tvb_get_letoh64(tvb, offset);
503         if (tree) {
504             proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_dst64, tvb, offset, sizeof(guint64), packet.dst64);
505         }
506         offset += sizeof(guint64);
507     }
508
509     /* Add the extended source address. (ZigBee 2006 and later). */
510     if ((pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) && packet.ext_src) {
511         packet.src64 = tvb_get_letoh64(tvb, offset);
512         if (tree) {
513             proto_tree_add_eui64(nwk_tree, hf_zbee_nwk_src64, tvb, offset, sizeof(guint64), packet.src64);
514         }
515         offset += sizeof(guint64);
516     }
517
518     /* Add the Source Route field. (ZigBee 2006 and later). */
519     if ((pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) && packet.route) {
520         guint8  relay_count;
521         guint8  relay_index;
522         guint16 relay_addr;
523         guint   i;
524
525         if (tree) {
526             /* Create a subtree for the source route field. */
527             ti = proto_tree_add_text(nwk_tree, tvb, offset, sizeof(guint8), "Source Route");
528             field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_route);
529         }
530
531         /* Get and display the relay count. */
532         relay_count = tvb_get_guint8(tvb, offset);
533         if (tree) {
534             proto_tree_add_uint(field_tree, hf_zbee_nwk_relay_count, tvb, offset, sizeof(guint8), relay_count);
535             proto_item_append_text(ti, ", Length: %d", relay_count);
536         }
537         offset += sizeof(guint8);
538
539         if (tree) {
540             /* Correct the length of the source route fields. */
541             proto_item_set_len(ti, sizeof(guint8) + relay_count*sizeof(guint16));
542         }
543
544         /* Get and display the relay index. */
545         relay_index = tvb_get_guint8(tvb, offset);
546         if (tree) {
547             proto_tree_add_uint(field_tree, hf_zbee_nwk_relay_index, tvb, offset, sizeof(guint8), relay_index);
548         }
549         offset += sizeof(guint8);
550
551         /* Get and display the relay list. */
552         for (i=0; i<relay_count; i++) {
553             relay_addr = tvb_get_letohs(tvb, offset);
554             if (tree) {
555                 proto_tree_add_text(field_tree, tvb, offset, sizeof(guint16), "Relay %d: 0x%04x", i+1, relay_addr);
556             }
557             offset += sizeof(guint16);
558         } /* for */
559     }
560
561     /*
562      * Link the packet structure into the private data pointer so the
563      * APS layer can retrieve the network source address.
564      *
565      * BUGBUG: Ideally, the APS layer could just pull this out of the
566      * pinfo structure. But there is no suitable address type to use
567      * for ZigBee's 16-bit short address.
568      */
569     pinfo->private_data = (void *)&packet;
570
571     /*
572      * Ensure that the payload exists. There are no valid ZigBee network
573      * packets that have no payload.
574      */
575     if (offset >= tvb_length(tvb)) {
576         /* Non-existent or truncated payload. */
577         expert_add_info_format(pinfo, proto_root, PI_MALFORMED, PI_ERROR, "Missing Payload");
578         THROW(BoundsError);
579     }
580     /* Payload is encrypted, attempt security operations. */
581     else if (packet.security) {
582         payload_tvb = dissect_zbee_secure(tvb, pinfo, nwk_tree, offset, 0);
583         if (payload_tvb == NULL) {
584             /* If Payload_tvb is NULL, then the security dissector cleaned up. */
585             return;
586         }
587     }
588     /* Plaintext payload. */
589     else {
590         payload_tvb = tvb_new_subset_remaining(tvb, offset);
591     }
592
593     if (packet.type == ZBEE_NWK_FCF_CMD) {
594         /* Dissect the Network Command. */
595         dissect_zbee_nwk_cmd(payload_tvb, pinfo, nwk_tree);
596     }
597     else if (packet.type == ZBEE_NWK_FCF_DATA) {
598         /* Dissect the Network Payload (APS layer). */
599         call_dissector(aps_handle, payload_tvb, pinfo, tree);
600     }
601     else {
602         /* Invalid type. */
603         call_dissector(data_handle, payload_tvb, pinfo, tree);
604     }
605 } /* dissect_zbee_nwk */
606
607 /*FUNCTION:------------------------------------------------------
608  *  NAME
609  *      dissect_zbee_nwk_cmd
610  *  DESCRIPTION
611  *      ZigBee Network command packet dissection routine for Wireshark.
612  *          note: this dissector differs from others in that is shouldn't be
613  *                  passed the main tree pointer, but the nwk tree instead.
614  *  PARAMETERS
615  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
616  *      packet_into *pinfo  - pointer to packet information fields
617  *      proto_tree *tree    - pointer to data tree Wireshark uses to display packet.
618  *  RETURNS
619  *      void
620  *---------------------------------------------------------------
621  */
622 static void dissect_zbee_nwk_cmd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
623 {
624     proto_tree  *cmd_tree = NULL;
625     proto_item  *cmd_root = NULL;
626
627     zbee_nwk_packet *packet = pinfo->private_data;
628
629     guint       offset=0;
630     guint8      cmd_id = tvb_get_guint8(tvb, offset);
631
632     /* Create a subtree for this command. */
633     if (tree) {
634         cmd_root = proto_tree_add_text(tree, tvb, offset, tvb_length(tvb), "Command Frame: %s", val_to_str(cmd_id, zbee_nwk_cmd_names, "Unknown"));
635         cmd_tree = proto_item_add_subtree(cmd_root, ett_zbee_nwk_cmd);
636
637         /* Add the command ID. */
638         proto_tree_add_uint(cmd_tree, hf_zbee_nwk_cmd_id, tvb, offset, sizeof(guint8), cmd_id);
639     }
640     offset += sizeof(guint8);
641
642     /* Add the command name to the info column. */
643     if (check_col(pinfo->cinfo, COL_INFO)) {
644         col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(cmd_id, zbee_nwk_cmd_names, "Unknown Command"));
645     }
646
647     /* Handle the command. */
648     switch(cmd_id){
649         case ZBEE_NWK_CMD_ROUTE_REQ:
650             /* Route Request Command. */
651             offset = dissect_zbee_nwk_route_req(tvb, pinfo, cmd_tree, packet, offset);
652             break;
653
654         case ZBEE_NWK_CMD_ROUTE_REPLY:
655             /* Route Reply Command. */
656             offset = dissect_zbee_nwk_route_rep(tvb, pinfo, cmd_tree, offset);
657             break;
658
659         case ZBEE_NWK_CMD_NWK_STATUS:
660             /* Network Status Command. */
661             offset = dissect_zbee_nwk_status(tvb, pinfo, cmd_tree, offset);
662             break;
663
664         case ZBEE_NWK_CMD_LEAVE:
665             /* Leave Command. */
666             offset = dissect_zbee_nwk_leave(tvb, cmd_tree, offset);
667             break;
668
669         case ZBEE_NWK_CMD_ROUTE_RECORD:
670             /* Route Record Command. */
671             offset = dissect_zbee_nwk_route_rec(tvb, pinfo, cmd_tree, packet, offset);
672             break;
673
674         case ZBEE_NWK_CMD_REJOIN_REQ:
675             /* Rejoin Request Command. */
676             offset = dissect_zbee_nwk_rejoin_req(tvb, pinfo, cmd_tree, packet, offset);
677             break;
678
679         case ZBEE_NWK_CMD_REJOIN_RESP:
680             /* Rejoin Response Command. */
681             offset = dissect_zbee_nwk_rejoin_resp(tvb, pinfo, cmd_tree, packet, offset);
682             break;
683
684         case ZBEE_NWK_CMD_LINK_STATUS:
685             /* Link Status Command. */
686             offset = dissect_zbee_nwk_link_status(tvb, cmd_tree, offset);
687             break;
688
689         case ZBEE_NWK_CMD_NWK_REPORT:
690             /* Network Report Command. */
691             offset = dissect_zbee_nwk_report(tvb, pinfo, cmd_tree, offset);
692             break;
693
694         case ZBEE_NWK_CMD_NWK_UPDATE:
695             /* Network Update Command. */
696             offset = dissect_zbee_nwk_update(tvb, pinfo, cmd_tree, offset);
697             break;
698
699         default:
700             /* Just break out and let the overflow handler deal with the payload. */
701             break;
702     } /* switch */
703
704     /* There is excess data in the packet. */
705     if (offset < tvb_length(tvb)) {
706         /* There are leftover bytes! */
707         guint       leftover_len    = tvb_length(tvb) - offset;
708         tvbuff_t    *leftover_tvb   = tvb_new_subset(tvb, offset, leftover_len, leftover_len);
709         proto_tree  *root           = NULL;
710
711         /* Correct the length of the command tree. */
712         if (tree) {
713             root = proto_tree_get_root(tree);
714             proto_item_set_len(cmd_root, offset);
715         }
716
717         /* Dump the leftover to the data dissector. */
718         call_dissector(data_handle, leftover_tvb, pinfo, root);
719     }
720 } /* dissect_zbee_nwk_cmd */
721
722 /*FUNCTION:------------------------------------------------------
723  *  NAME
724  *      dissect_zbee_nwk_route_req
725  *  DESCRIPTION
726  *      Helper dissector for the Route Request command.
727  *  PARAMETERS
728  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
729  *      packet_into *pinfo  - pointer to packet information fields
730  *      proto_tree *tree    - pointer to the command subtree.
731  *      zbee_nwk_packet *packet - pointer to the network packet struct.
732  *      offset              - offset into the tvb to begin dissection.
733  *  RETURNS
734  *      guint               - offset after command dissection.
735  *---------------------------------------------------------------
736  */
737 static guint
738 dissect_zbee_nwk_route_req(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
739 {
740     proto_tree  *field_tree;
741     proto_item  *ti;
742
743     guint8  route_options;
744     guint8  route_id;
745     guint16 dest_addr;
746     guint64 dest_ext_addr;
747     guint8  path_cost;
748
749     /* Get and display the route options field. */
750     route_options = tvb_get_guint8(tvb, offset);
751     if (tree) {
752         /* Create a subtree for the command options. */
753         ti = proto_tree_add_text(tree, tvb, offset, sizeof(guint8), "Command Options (0x%02x)", route_options);
754         field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_cmd_options);
755
756         if (pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) {
757             proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_route_opt_multicast, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_MCAST);
758             proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_route_opt_dest_ext, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_DEST_EXT);
759             proto_tree_add_uint(field_tree, hf_zbee_nwk_cmd_route_opt_many_to_one, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_MANY_MASK);
760         }
761         else {
762             proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_route_opt_repair, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_REPAIR);
763         }
764     }
765     offset += sizeof(guint8);
766
767     /* Get and display the route request ID. */
768     route_id = tvb_get_guint8(tvb, offset);
769     if (tree) {
770         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_id, tvb, offset, sizeof(guint8), route_id);
771     }
772     offset += sizeof(guint8);
773
774     /* Get and display the destination address. */
775     dest_addr = tvb_get_letohs(tvb, offset);
776     if (tree) {
777         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_dest, tvb, offset, sizeof(guint16), dest_addr);
778     }
779     offset += sizeof(guint16);
780
781     /* Get and display the path cost. */
782     path_cost = tvb_get_guint8(tvb, offset);
783     if (tree) {
784         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_cost, tvb, offset, sizeof(guint8), path_cost);
785     }
786     offset += sizeof(guint8);
787
788     /* Get and display the extended destination address. */
789     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_DEST_EXT) {
790         dest_ext_addr = tvb_get_letoh64(tvb, offset);
791         if (tree) {
792             proto_tree_add_eui64(tree, hf_zbee_nwk_cmd_route_dest_ext, tvb, offset, sizeof(guint64), dest_ext_addr);
793         }
794         offset += sizeof(guint64);
795     }
796
797     /* Update the info column. */
798     if (check_col(pinfo->cinfo, COL_INFO)) {
799         col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: 0x%04x, Src: 0x%04x", dest_addr, packet->src);
800     }
801
802     /* Done */
803     return offset;
804 } /* dissect_zbee_nwk_route_req */
805
806 /*FUNCTION:------------------------------------------------------
807  *  NAME
808  *      dissect_zbee_nwk_route_rep
809  *  DESCRIPTION
810  *      Helper dissector for the Route Reply command.
811  *  PARAMETERS
812  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
813  *      packet_into *pinfo  - pointer to packet information fields
814  *      proto_tree *tree    - pointer to the command subtree.
815  *      offset              - offset into the tvb to begin dissection.
816  *  RETURNS
817  *      guint               - offset after command dissection.
818  *---------------------------------------------------------------
819  */
820 static guint
821 dissect_zbee_nwk_route_rep(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
822 {
823     proto_tree  *field_tree;
824     proto_item  *ti;
825
826     guint8  route_options;
827     guint8  route_id;
828     guint16 orig_addr;
829     guint16 resp_addr;
830     guint64 orig_ext_addr;
831     guint64 resp_ext_addr;
832     guint8  path_cost;
833
834     /* Get and display the route options field. */
835     route_options = tvb_get_guint8(tvb, offset);
836     if (tree) {
837         /* Create a subtree for the command options. */
838         ti = proto_tree_add_text(tree, tvb, offset, sizeof(guint8), "Command Options (0x%02x)", route_options);
839         field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_cmd_options);
840
841         if (pinfo->zbee_stack_vers >= ZBEE_VERSION_2007) {
842             proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_route_opt_multicast, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_MCAST);
843             proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_route_opt_resp_ext, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_RESP_EXT);
844             proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_route_opt_orig_ext, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_ORIG_EXT);
845         }
846         else {
847             proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_route_opt_repair, tvb, offset, sizeof(guint8), route_options & ZBEE_NWK_CMD_ROUTE_OPTION_REPAIR);
848         }
849     }
850     offset += sizeof(guint8);
851
852     /* Get and display the route request ID. */
853     route_id = tvb_get_guint8(tvb, offset);
854     if (tree) {
855         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_id, tvb, offset, sizeof(guint8), route_id);
856     }
857     offset += sizeof(guint8);
858
859     /* Get and display the originator address. */
860     orig_addr = tvb_get_letohs(tvb, offset);
861     if (tree) {
862         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_orig, tvb, offset, sizeof(guint16), orig_addr);
863     }
864     offset += sizeof(guint16);
865
866     /* Get and display the responder address. */
867     resp_addr = tvb_get_letohs(tvb, offset);
868     if (tree) {
869         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_resp, tvb, offset, sizeof(guint16), resp_addr);
870     }
871     offset += sizeof(guint16);
872
873     /* Get and display the path cost. */
874     path_cost = tvb_get_guint8(tvb, offset);
875     if (tree) {
876         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_cost, tvb, offset, sizeof(guint8), path_cost);
877     }
878     offset += sizeof(guint8);
879
880     /* Get and display the originator extended address. */
881     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_ORIG_EXT) {
882         orig_ext_addr = tvb_get_letoh64(tvb, offset);
883         if (tree) {
884             proto_tree_add_eui64(tree, hf_zbee_nwk_cmd_route_orig_ext, tvb, offset, sizeof(guint64), orig_ext_addr);
885         }
886         offset += sizeof(guint64);
887     }
888
889     /* Get and display the responder extended address. */
890     if (route_options & ZBEE_NWK_CMD_ROUTE_OPTION_RESP_EXT) {
891         resp_ext_addr = tvb_get_letoh64(tvb, offset);
892         if (tree) {
893             proto_tree_add_eui64(tree, hf_zbee_nwk_cmd_route_resp_ext, tvb, offset, sizeof(guint64), resp_ext_addr);
894         }
895         offset += sizeof(guint64);
896     }
897
898     /* Update the info column. */
899     if (check_col(pinfo->cinfo, COL_INFO)) {
900         col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: 0x%04x, Src: 0x%04x", resp_addr, orig_addr);
901     }
902
903     /* Done */
904     return offset;
905 } /* dissect_zbee_nwk_route_rep */
906
907 /*FUNCTION:------------------------------------------------------
908  *  NAME
909  *      dissect_zbee_nwk_status
910  *  DESCRIPTION
911  *      Helper dissector for the Network Status command.
912  *  PARAMETERS
913  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
914  *      packet_into *pinfo  - pointer to packet information fields
915  *      proto_tree *tree    - pointer to the command subtree.
916  *      offset              - offset into the tvb to begin dissection.
917  *  RETURNS
918  *      guint               - offset after command dissection.
919  *---------------------------------------------------------------
920  */
921 static guint
922 dissect_zbee_nwk_status(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
923 {
924     guint8  status_code;
925     guint16 addr;
926
927     /* Get and display the status code. */
928     status_code = tvb_get_guint8(tvb, offset);
929     if (tree) {
930         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_nwk_status, tvb, offset, sizeof(guint8), status_code);
931     }
932     offset += sizeof(guint8);
933
934     /* Get and display the destination address. */
935     addr = tvb_get_letohs(tvb, offset);
936     if (tree) {
937         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_route_dest, tvb, offset, sizeof(guint16), addr);
938     }
939     offset += sizeof(guint16);
940
941     /* Update the info column. */
942     if (check_col(pinfo->cinfo, COL_INFO)) {
943         col_append_fstr(pinfo->cinfo, COL_INFO, ", 0x%04x: %s", addr, val_to_str(status_code, zbee_nwk_status_codes, "Unknown Status Code"));
944     }
945
946     /* Done */
947     return offset;
948 } /* dissect_zbee_nwk_status */
949
950 /*FUNCTION:------------------------------------------------------
951  *  NAME
952  *      dissect_zbee_nwk_leave
953  *  DESCRIPTION
954  *      Helper dissector for the Leave command.
955  *  PARAMETERS
956  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
957  *      proto_tree *tree    - pointer to the command subtree.
958  *      offset              - offset into the tvb to begin dissection.
959  *  RETURNS
960  *      guint               - offset after command dissection.
961  *---------------------------------------------------------------
962  */
963 static guint
964 dissect_zbee_nwk_leave(tvbuff_t *tvb, proto_tree *tree, guint offset)
965 {
966     guint8  leave_options;
967
968     /* Get and display the leave options. */
969     leave_options = tvb_get_guint8(tvb, offset);
970     if (tree) {
971         proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_leave_rejoin, tvb, offset, sizeof(guint8), leave_options & ZBEE_NWK_CMD_LEAVE_OPTION_REJOIN);
972         proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_leave_request, tvb, offset, sizeof(guint8), leave_options & ZBEE_NWK_CMD_LEAVE_OPTION_REQUEST);
973         proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_leave_children, tvb, offset, sizeof(guint8), leave_options & ZBEE_NWK_CMD_LEAVE_OPTION_CHILDREN);
974     }
975     offset += sizeof(guint8);
976
977     /* Done */
978     return offset;
979 } /* dissect_zbee_nwk_leave */
980
981 /*FUNCTION:------------------------------------------------------
982  *  NAME
983  *      dissect_zbee_nwk_route_rec
984  *  DESCRIPTION
985  *      Helper dissector for the Reoute Record command.
986  *  PARAMETERS
987  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
988  *      packet_into *pinfo  - pointer to packet information fields
989  *      proto_tree *tree    - pointer to the command subtree.
990  *      zbee_nwk_packet *packet - pointer to the network packet struct.
991  *      offset              - offset into the tvb to begin dissection.
992  *  RETURNS
993  *      guint               - offset after command dissection.
994  *---------------------------------------------------------------
995  */
996 static guint
997 dissect_zbee_nwk_route_rec(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
998 {
999     guint8  relay_count;
1000     guint16 relay_addr;
1001     guint   i;
1002
1003     /* Get and display the relay count. */
1004     relay_count = tvb_get_guint8(tvb, offset);
1005     if (tree) {
1006         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_relay_count, tvb, offset, sizeof(guint8), relay_count);
1007     }
1008     offset += sizeof(guint8);
1009
1010     /* Get and display the relay addresses. */
1011     for (i=0; i<relay_count; i++) {
1012         relay_addr = tvb_get_letohs(tvb, offset);
1013         if (tree) {
1014             proto_tree_add_text(tree, tvb, offset, sizeof(guint16), "Relay Device %d: 0x%04x", i+1, relay_addr);
1015         }
1016         offset += sizeof(guint16);
1017     } /* for */
1018
1019     /* Update the info column. */
1020     if (check_col(pinfo->cinfo, COL_INFO)) {
1021         col_append_fstr(pinfo->cinfo, COL_INFO, ", Dst: 0x%04x", packet->dst);
1022     }
1023
1024     /* Done */
1025     return offset;
1026 } /* dissect_zbee_nwk_route_rec */
1027
1028 /*FUNCTION:------------------------------------------------------
1029  *  NAME
1030  *      dissect_zbee_nwk_rejoin_req
1031  *  DESCRIPTION
1032  *      Helper dissector for the Rejoin Request command.
1033  *  PARAMETERS
1034  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1035  *      packet_into *pinfo  - pointer to packet information fields
1036  *      proto_tree *tree    - pointer to the command subtree.
1037  *      zbee_nwk_packet *packet - pointer to the network packet struct.
1038  *      offset              - offset into the tvb to begin dissection.
1039  *  RETURNS
1040  *      guint               - offset after command dissection.
1041  *---------------------------------------------------------------
1042  */
1043 static guint
1044 dissect_zbee_nwk_rejoin_req(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
1045 {
1046     proto_tree  *field_tree;
1047     proto_item  *ti;
1048
1049     guint8  capabilities;
1050
1051     /* Get and dispaly the capabilities information. */
1052     capabilities = tvb_get_guint8(tvb, offset);
1053     if (tree) {
1054         /* Create a subtree for the capability information. */
1055         ti = proto_tree_add_text(tree, tvb, offset, sizeof(guint8), "Capability Information");
1056         field_tree = proto_item_add_subtree(ti, ett_zbee_nwk_cmd_cinfo);
1057
1058         /* Add the capability info flags. */
1059         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_alt_coord, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_ALT_COORD);
1060         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_type, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_FFD);
1061         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_power, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_POWER);
1062         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_idle_rx, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_IDLE_RX);
1063         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_security, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_SECURITY);
1064         proto_tree_add_boolean(field_tree, hf_zbee_nwk_cmd_cinfo_alloc, tvb, offset, sizeof(guint8), capabilities & ZBEE_CINFO_ALLOC);
1065     }
1066     offset += sizeof(guint8);
1067
1068     /* Update the info column.*/
1069     if (check_col(pinfo->cinfo, COL_INFO)) {
1070         col_append_fstr(pinfo->cinfo, COL_INFO, ", Device: 0x%04x", packet->src);
1071     }
1072
1073     /* Done */
1074     return offset;
1075 } /* dissect_zbee_nwk_rejoin_req */
1076
1077 /*FUNCTION:------------------------------------------------------
1078  *  NAME
1079  *      dissect_zbee_nwk_rejoin_resp
1080  *  DESCRIPTION
1081  *      Helper dissector for the Rejoin Response command.
1082  *  PARAMETERS
1083  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1084  *      packet_into *pinfo  - pointer to packet information fields
1085  *      proto_tree *tree    - pointer to the command subtree.
1086  *      zbee_nwk_packet *packet - pointer to the network packet struct.
1087  *      offset              - offset into the tvb to begin dissection.
1088  *  RETURNS
1089  *      guint               - offset after command dissection.
1090  *---------------------------------------------------------------
1091  */
1092 static guint
1093 dissect_zbee_nwk_rejoin_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, zbee_nwk_packet * packet, guint offset)
1094 {
1095     guint16 addr;
1096     guint8  status;
1097
1098     /* Get and display the short address. */
1099     addr = tvb_get_letohs(tvb, offset);
1100     if (tree) {
1101         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_addr, tvb, offset, sizeof(guint16), addr);
1102     }
1103     offset += sizeof(guint16);
1104
1105     /* Get and display the rejoin status. */
1106     status = tvb_get_guint8(tvb, offset);
1107     if (tree) {
1108         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_rejoin_status, tvb, offset, sizeof(guint8), status);
1109     }
1110     offset += sizeof(guint8);
1111
1112     /* Update the info column. */
1113     if (check_col(pinfo->cinfo, COL_INFO)) {
1114         if (status == IEEE802154_CMD_ASRSP_AS_SUCCESS) {
1115            col_append_fstr(pinfo->cinfo, COL_INFO, ", Address: 0x%04x", packet->src);
1116         }
1117         else {
1118             col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str(status, zbee_nwk_rejoin_codes, "Unknown Rejoin Response"));
1119         }
1120     }
1121
1122     /* Done */
1123     return offset;
1124 } /* dissect_zbee_nwk_rejoin_resp */
1125
1126 /*FUNCTION:------------------------------------------------------
1127  *  NAME
1128  *      dissect_zbee_nwk_link_status
1129  *  DESCRIPTION
1130  *      Helper dissector for the Link Status command.
1131  *  PARAMETERS
1132  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1133  *      proto_tree *tree    - pointer to the command subtree.
1134  *      offset              - offset into the tvb to begin dissection.
1135  *  RETURNS
1136  *      guint               - offset after command dissection.
1137  *---------------------------------------------------------------
1138  */
1139 static guint
1140 dissect_zbee_nwk_link_status(tvbuff_t *tvb, proto_tree *tree, guint offset)
1141 {
1142     guint8  options;
1143     guint16 addr;
1144     int     i, link_count;
1145
1146     /* Get and Display the link status options. */
1147     options = tvb_get_guint8(tvb, offset);
1148     link_count = options & ZBEE_NWK_CMD_LINK_OPTION_COUNT_MASK;
1149     if (tree) {
1150         proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_link_last, tvb, offset, sizeof(guint8), options & ZBEE_NWK_CMD_LINK_OPTION_LAST_FRAME);
1151         proto_tree_add_boolean(tree, hf_zbee_nwk_cmd_link_first, tvb, offset, sizeof(guint8), options & ZBEE_NWK_CMD_LINK_OPTION_FIRST_FRAME);
1152         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_link_count, tvb, offset, sizeof(guint8), link_count);
1153     }
1154     offset += sizeof(guint8);
1155
1156     /* Get and Display the link status list. */
1157     for (i=0; i<link_count; i++) {
1158         /* Get the address and link status. */
1159         addr = tvb_get_letohs(tvb, offset);
1160         options = tvb_get_guint8(tvb, offset+sizeof(guint16));
1161         if (tree) {
1162             proto_tree_add_text(tree, tvb, offset, sizeof(guint16)+sizeof(guint8), "0x%04x, Incoming Cost: %d Outgoing Cost: %d", addr, options & ZBEE_NWK_CMD_LINK_INCOMMING_COST_MASK, (options & ZBEE_NWK_CMD_LINK_OUTGOING_COST_MASK)>>4);
1163         }
1164         offset += (sizeof(guint16)+sizeof(guint8));
1165     } /* for */
1166
1167     /* TODO: Update the info column. */
1168     return offset;
1169 } /* dissect_zbee_nwk_link_status */
1170
1171 /*FUNCTION:------------------------------------------------------
1172  *  NAME
1173  *      dissect_zbee_nwk_report
1174  *  DESCRIPTION
1175  *      Helper dissector for the Network Report command.
1176  *  PARAMETERS
1177  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1178  *      packet_into *pinfo  - pointer to packet information fields
1179  *      proto_tree *tree    - pointer to the command subtree.
1180  *      offset              - offset into the tvb to begin dissection.
1181  *  RETURNS
1182  *      guint               - offset after command dissection.
1183  *---------------------------------------------------------------
1184  */
1185 static guint
1186 dissect_zbee_nwk_report(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1187 {
1188     guint8  options;
1189     guint8  report_type;
1190     guint64 epid;
1191     int     report_count;
1192     int     i;
1193
1194     /* Get and display the command options field. */
1195     options = tvb_get_guint8(tvb, offset);
1196     report_count = options & ZBEE_NWK_CMD_NWK_REPORT_COUNT_MASK;
1197     report_type = options & ZBEE_NWK_CMD_NWK_REPORT_ID_MASK;
1198     if (tree) {
1199         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_report_type, tvb, offset, sizeof(guint8), report_type);
1200         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_report_count, tvb, offset, sizeof(guint8), report_count);
1201     }
1202     offset += sizeof(guint8);
1203
1204     /* Get and display the epid. */
1205     epid = tvb_get_letoh64(tvb, offset);
1206     if (tree) {
1207         proto_tree_add_uint64_format_value(tree, hf_zbee_nwk_cmd_epid, tvb, offset, sizeof(guint64), epid, "%s", print_eui64(epid));
1208     }
1209     offset += sizeof(guint64);
1210
1211     if (report_type == ZBEE_NWK_CMD_NWK_REPORT_ID_PAN_CONFLICT) {
1212         guint16 panId;
1213
1214         /* Report information contains a list of PANS with range of the sender. */
1215         for (i=0; i<report_count; i++) {
1216             panId = tvb_get_letohs(tvb, offset);
1217             if (tree) {
1218                 proto_tree_add_text(tree, tvb, offset, sizeof(guint16), "PANID: 0x%04x", panId);
1219             }
1220             offset += sizeof(guint16);
1221         } /* for */
1222     }
1223
1224     /* Update the info column. */
1225     if (check_col(pinfo->cinfo, COL_INFO)) {
1226         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str(report_type, zbee_nwk_report_types, "Unknown Report Type"));
1227     }
1228
1229     /* Done */
1230     return offset;
1231 } /* dissect_zbee_nwk_report */
1232
1233 /*FUNCTION:------------------------------------------------------
1234  *  NAME
1235  *      dissect_zbee_nwk_update
1236  *  DESCRIPTION
1237  *      Helper dissector for the Network Update command.
1238  *  PARAMETERS
1239  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1240  *      packet_into *pinfo  - pointer to packet information fields
1241  *      proto_tree *tree    - pointer to the command subtree.
1242  *      offset              - offset into the tvb to begin dissection.
1243  *  RETURNS
1244  *      guint               - offset after command dissection.
1245  *---------------------------------------------------------------
1246  */
1247 static guint
1248 dissect_zbee_nwk_update(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
1249 {
1250     guint8  options;
1251     guint8  update_type;
1252     guint8  update_id;
1253     guint64 epid;
1254     int     update_count;
1255     int     i;
1256
1257     /* Get and display the command options field. */
1258     options = tvb_get_guint8(tvb, offset);
1259     update_count = options & ZBEE_NWK_CMD_NWK_UPDATE_COUNT_MASK;
1260     update_type = options & ZBEE_NWK_CMD_NWK_UPDATE_ID_MASK;
1261     if (tree) {
1262         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_type, tvb, offset, sizeof(guint8), update_type);
1263         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_count, tvb, offset, sizeof(guint8), update_count);
1264     }
1265     offset += sizeof(guint8);
1266
1267     /* Get and display the epid. */
1268     epid = tvb_get_letoh64(tvb, offset);
1269     if (tree) {
1270         proto_tree_add_uint64_format_value(tree, hf_zbee_nwk_cmd_epid, tvb, offset, sizeof(guint64), epid, "%s", print_eui64(epid));
1271     }
1272     offset += sizeof(guint64);
1273
1274     /* Get and display the updateID. */
1275     update_id = tvb_get_guint8(tvb, offset);
1276     if (tree) {
1277         proto_tree_add_uint(tree, hf_zbee_nwk_cmd_update_id, tvb, offset, sizeof(guint8), update_id);
1278     }
1279     offset += sizeof(guint8);
1280
1281     if (update_type == ZBEE_NWK_CMD_NWK_UPDATE_ID_PAN_UPDATE) {
1282         guint16 panId;
1283
1284         /* Report information contains a list of PANS with range of the sender. */
1285         for (i=0; i<update_count; i++) {
1286             panId = tvb_get_letohs(tvb, offset);
1287             if (tree) {
1288                 proto_tree_add_text(tree, tvb, offset, sizeof(guint16), "PANID: 0x%04x", panId);
1289             }
1290             offset += sizeof(guint16);
1291         } /* for */
1292     }
1293
1294     /* Update the info column. */
1295     if (check_col(pinfo->cinfo, COL_INFO)) {
1296         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str(update_type, zbee_nwk_update_types, "Unknown Update Type"));
1297     }
1298
1299     /* Done */
1300     return offset;
1301 } /* dissect_zbee_nwk_update */
1302
1303 /*FUNCTION:------------------------------------------------------
1304  *  NAME
1305  *      dissect_zbee_beacon
1306  *  DESCRIPTION
1307  *      Dissector for ZigBee network beacons.
1308  *  PARAMETERS
1309  *      tvbuff_t *tvb       - pointer to buffer containing raw packet.
1310  *      packet_into *pinfo  - pointer to packet information fields
1311  *      proto_tree *tree    - pointer to data tree Wireshark uses to display packet.
1312  *  RETURNS
1313  *      void
1314  *---------------------------------------------------------------
1315  */
1316 static void dissect_zbee_beacon(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1317 {
1318     ieee802154_packet   *packet = pinfo->private_data;
1319
1320     proto_item  *beacon_root = NULL;
1321     proto_tree  *beacon_tree = NULL;
1322     guint       offset = 0;
1323
1324     guint8      temp;
1325     guint8      version;
1326     guint64     epid;
1327     guint32     tx_offset;
1328
1329     /* Add ourself to the protocol column. */
1330     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ZigBee");
1331     /* Create the tree for this beacon. */
1332     if (tree) {
1333         beacon_root = proto_tree_add_protocol_format(tree, proto_zbee_nwk, tvb, 0, tvb_length(tvb), "ZigBee Beacon");
1334         beacon_tree = proto_item_add_subtree(beacon_root, ett_zbee_beacon);
1335     }
1336
1337     /* Update the info column. */
1338     if(check_col(pinfo->cinfo, COL_INFO)) {
1339         col_clear(pinfo->cinfo, COL_INFO);
1340         col_append_fstr(pinfo->cinfo, COL_INFO, "Beacon, Src: 0x%04x", packet->src.addr16);
1341     }
1342
1343     /* Get and display the protocol id, must be 0 on all ZigBee beacons. */
1344     temp = tvb_get_guint8(tvb, offset);
1345     if (tree) {
1346         proto_tree_add_uint(beacon_tree, hf_zbee_beacon_protocol, tvb, offset, sizeof(guint8), temp);
1347     }
1348     offset += sizeof(guint8);
1349
1350     /* Get and display the stack profile and protocol version. */
1351     temp = tvb_get_guint8(tvb, offset);
1352     pinfo->zbee_stack_vers = version = zbee_get_bit_field(temp, ZBEE_NWK_BEACON_PROTOCOL_VERSION);
1353     if (tree) {
1354         proto_tree_add_uint(beacon_tree, hf_zbee_beacon_stack_profile, tvb, offset, sizeof(guint8), zbee_get_bit_field(temp, ZBEE_NWK_BEACON_STACK_PROFILE));
1355         proto_tree_add_uint(beacon_tree, hf_zbee_beacon_version, tvb, offset, sizeof(guint8), version);
1356     }
1357     offset += sizeof(guint8);
1358
1359     /* Get and display the security level and flags. */
1360     temp        = tvb_get_guint8(tvb, offset);
1361     if (tree) {
1362         proto_tree_add_boolean(beacon_tree, hf_zbee_beacon_router_capacity, tvb, offset, sizeof(guint8), zbee_get_bit_field(temp, ZBEE_NWK_BEACON_ROUTER_CAPACITY));
1363         proto_tree_add_uint(beacon_tree, hf_zbee_beacon_depth, tvb, offset, sizeof(guint8), zbee_get_bit_field(temp, ZBEE_NWK_BEACON_NETWORK_DEPTH));
1364         proto_tree_add_boolean(beacon_tree, hf_zbee_beacon_end_device_capacity, tvb, offset, sizeof(guint8), zbee_get_bit_field(temp, ZBEE_NWK_BEACON_END_DEVICE_CAPACITY));
1365     }
1366     offset += sizeof(guint8);
1367
1368     if (version >= ZBEE_VERSION_2007) {
1369         /* In ZigBee 2006 and later, the beacon contains an extended PAN ID. */
1370         epid = tvb_get_letoh64(tvb, offset);
1371         if (tree) {
1372             proto_tree_add_uint64_format_value(beacon_tree, hf_zbee_beacon_epid, tvb, offset, sizeof(guint64), epid, "%s", print_eui64(epid));
1373         }
1374         offset += sizeof(guint64);
1375
1376         /* Update the Info Column with the EPID. */
1377         if (check_col(pinfo->cinfo, COL_INFO)) {
1378             col_append_fstr(pinfo->cinfo, COL_INFO, ", EPID: %s", print_eui64(epid));
1379         }
1380
1381         /* In ZigBee 2007 and layer, the Tx-Offset is a required value. */
1382         tx_offset = tvb_get_letoh24(tvb, offset);
1383         if (tree) {
1384             proto_tree_add_uint(beacon_tree, hf_zbee_beacon_tx_offset, tvb, offset, 3, tx_offset);
1385         }
1386         offset += 3;
1387
1388         /* Get and display the update ID. */
1389         temp = tvb_get_guint8(tvb, offset);
1390         if (tree) {
1391             proto_tree_add_uint(beacon_tree, hf_zbee_beacon_update_id, tvb, offset, sizeof(guint8), temp);
1392         }
1393         offset += sizeof(guint8);
1394     }
1395     else if (tvb_bytes_exist(tvb, offset, 3)) {
1396         /* In ZigBee 2004, the Tx-Offset is an optional value. */
1397         tx_offset = tvb_get_letoh24(tvb, offset);
1398         if (tree) {
1399             proto_tree_add_uint(beacon_tree, hf_zbee_beacon_tx_offset, tvb, offset, 3, tx_offset);
1400         }
1401         offset += 3;
1402
1403         /* Update the info column with the PAN ID. */
1404         if (check_col(pinfo->cinfo, COL_INFO)) {
1405             col_append_fstr(pinfo->cinfo, COL_INFO, ", PAN: 0x%04x", packet->src_pan);
1406         }
1407     }
1408
1409     /* Check for leftover bytes. */
1410     if (offset < tvb_length(tvb)) {
1411         /* Bytes leftover! */
1412         guint       leftover_len    = tvb_length(tvb) - offset;
1413         tvbuff_t    *leftover_tvb   = tvb_new_subset(tvb, offset, leftover_len, leftover_len);
1414         proto_tree  *root           = NULL;
1415
1416         /* Correct the length of the beacon tree. */
1417         if (tree) {
1418             root = proto_tree_get_root(tree);
1419             proto_item_set_len(beacon_root, offset);
1420         }
1421
1422         /* Dump the leftover to the data dissector. */
1423         call_dissector(data_handle, leftover_tvb, pinfo, root);
1424     }
1425 } /* dissect_zbee_beacon */
1426
1427 /*FUNCTION:------------------------------------------------------
1428  *  NAME
1429  *      proto_register_zbee_nwk
1430  *  DESCRIPTION
1431  *      ZigBee protocol registration routine.
1432  *  PARAMETERS
1433  *      none
1434  *  RETURNS
1435  *      void
1436  *---------------------------------------------------------------
1437  */
1438 void proto_register_zbee_nwk(void)
1439 {
1440     static hf_register_info hf[] = {
1441
1442             { &hf_zbee_nwk_frame_type,
1443             { "Frame Type",             "zbee.nwk.frame_type", FT_UINT16, BASE_HEX, VALS(zbee_nwk_frame_types), ZBEE_NWK_FCF_FRAME_TYPE,
1444                 NULL, HFILL }},
1445
1446             { &hf_zbee_nwk_proto_version,
1447             { "Protocol Version",       "zbee.nwk.proto_version", FT_UINT16, BASE_DEC, NULL, ZBEE_NWK_FCF_VERSION,
1448                 NULL, HFILL }},
1449
1450             { &hf_zbee_nwk_discover_route,
1451             { "Discover Route",         "zbee.nwk.discovery", FT_UINT16, BASE_HEX, VALS(zbee_nwk_discovery_modes), ZBEE_NWK_FCF_DISCOVER_ROUTE,
1452                 "Determines how route discovery may be handled, if at all.", HFILL }},
1453
1454             { &hf_zbee_nwk_multicast,
1455             { "Multicast",              "zbee.nwk.multicast", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_MULTICAST,
1456                 NULL, HFILL }},
1457
1458             { &hf_zbee_nwk_security,
1459             { "Security",               "zbee.nwk.security", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_SECURITY,
1460                 "Whether or not security operations are performed on the network payload.", HFILL }},
1461
1462             { &hf_zbee_nwk_source_route,
1463             { "Source Route",           "zbee.nwk.src_route", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_SOURCE_ROUTE,
1464                 NULL, HFILL }},
1465
1466             { &hf_zbee_nwk_ext_dst,
1467             { "Extended Destination",   "zbee.nwk.ext_dst", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_EXT_DEST,
1468                 NULL, HFILL }},
1469
1470             { &hf_zbee_nwk_ext_src,
1471             { "Extended Source",        "zbee.nwk.ext_src", FT_BOOLEAN, 16, NULL, ZBEE_NWK_FCF_EXT_SOURCE,
1472                 NULL, HFILL }},
1473
1474             { &hf_zbee_nwk_dst,
1475             { "Destination",            "zbee.nwk.dst", FT_UINT16, BASE_HEX, NULL, 0x0,
1476                 NULL, HFILL }},
1477
1478             { &hf_zbee_nwk_src,
1479             { "Source",                 "zbee.nwk.src", FT_UINT16, BASE_HEX, NULL, 0x0,
1480                 NULL, HFILL }},
1481
1482             { &hf_zbee_nwk_radius,
1483             { "Radius",                 "zbee.nwk.radius", FT_UINT8, BASE_DEC, NULL, 0x0,
1484                 "Number of hops remaining for a range-limited broadcast packet.", HFILL }},
1485
1486             { &hf_zbee_nwk_seqno,
1487             { "Sequence Number",        "zbee.nwk.seqno", FT_UINT8, BASE_DEC, NULL, 0x0,
1488                 NULL, HFILL }},
1489
1490             { &hf_zbee_nwk_mcast_mode,
1491             { "Multicast Mode",         "zbee.nwk.multicast.mode", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_MCAST_MODE,
1492                 "Controls whether this packet is permitted to be routed through non-members of the multicast group.", HFILL }},
1493
1494             { &hf_zbee_nwk_mcast_radius,
1495             { "Non-Member Radius",      "zbee.nwk.multicast.radius", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_MCAST_RADIUS,
1496                 "Limits the range of multicast packets when being routed through non-members.", HFILL }},
1497
1498             { &hf_zbee_nwk_mcast_max_radius,
1499             { "Max Non-Member Radius",  "zbee.nwk.multicast.max_radius", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_MCAST_MAX_RADIUS,
1500                 NULL, HFILL }},
1501
1502             { &hf_zbee_nwk_dst64,
1503             { "Extended Destination",   "zbee.nwk.dst64", FT_UINT64, BASE_HEX, NULL, 0x0,
1504                 NULL, HFILL }},
1505
1506             { &hf_zbee_nwk_src64,
1507             { "Extended Source",        "zbee.nwk.scr64", FT_UINT64, BASE_HEX, NULL, 0x0,
1508                 NULL, HFILL }},
1509
1510             { &hf_zbee_nwk_relay_count,
1511             { "Relay Count",            "zbee.nwk.relay.count", FT_UINT8, BASE_DEC, NULL, 0x0,
1512                 "Number of entries in the relay list.", HFILL }},
1513
1514             { &hf_zbee_nwk_relay_index,
1515             { "Relay Index",            "zbee.nwk.relay.index", FT_UINT8, BASE_DEC, NULL, 0x0,
1516                 "Number of relays required to route to the source device.", HFILL }},
1517
1518             { &hf_zbee_nwk_cmd_id,
1519             { "Command Identifier",     "zbee.nwk.cmd.id", FT_UINT8, BASE_HEX, VALS(zbee_nwk_cmd_names), 0x0,
1520                 NULL, HFILL }},
1521
1522             { &hf_zbee_nwk_cmd_addr,
1523             { "Address",                "zbee.nwk.cmd.addr", FT_UINT16, BASE_HEX, NULL, 0x0,
1524                 NULL, HFILL }},
1525
1526             { &hf_zbee_nwk_cmd_route_id,
1527             { "Route ID",               "zbee.nwk.cmd.route.id", FT_UINT8, BASE_DEC, NULL, 0x0,
1528                 "A sequence number for routing commands.", HFILL }},
1529
1530             { &hf_zbee_nwk_cmd_route_dest,
1531             { "Destination",            "zbee.nwk.cmd.route.dest", FT_UINT16, BASE_HEX, NULL, 0x0,
1532                 NULL, HFILL }},
1533
1534             { &hf_zbee_nwk_cmd_route_orig,
1535             { "Originator",             "zbee.nwk.cmd.route.orig", FT_UINT16, BASE_HEX, NULL, 0x0,
1536                 NULL, HFILL }},
1537
1538             { &hf_zbee_nwk_cmd_route_resp,
1539             { "Responder",              "zbee.nwk.cmd.route.resp", FT_UINT16, BASE_HEX, NULL, 0x0,
1540                 NULL, HFILL }},
1541
1542             { &hf_zbee_nwk_cmd_route_dest_ext,
1543             { "Extended Destination",   "zbee.nwk.cmd.route.dest_ext", FT_UINT64, BASE_HEX, NULL, 0x0,
1544                 NULL, HFILL }},
1545
1546             { &hf_zbee_nwk_cmd_route_orig_ext,
1547             { "Extended Originator",    "zbee.nwk.cmd.route.orig_ext", FT_UINT64, BASE_HEX, NULL, 0x0,
1548                 NULL, HFILL }},
1549
1550             { &hf_zbee_nwk_cmd_route_resp_ext,
1551             { "Extended Responder",     "zbee.nwk.cmd.route.resp_ext", FT_UINT64, BASE_HEX, NULL, 0x0,
1552                 NULL, HFILL }},
1553
1554             { &hf_zbee_nwk_cmd_route_cost,
1555             { "Path Cost",              "zbee.nwk.cmd.route.cost", FT_UINT8, BASE_DEC, NULL, 0x0,
1556                 "A value specifying the efficiency of this route.", HFILL }},
1557
1558             { &hf_zbee_nwk_cmd_route_opt_repair,
1559             { "Route Repair",           "zbee.nwk.cmd.route.opts.repair", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_ROUTE_OPTION_REPAIR,
1560                 "Flag identifying whether the route request command was to repair a failed route.", HFILL }},
1561
1562             { &hf_zbee_nwk_cmd_route_opt_multicast,
1563             { "Multicast",              "zbee.nwk.cmd.route.opts.mcast", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_ROUTE_OPTION_MCAST,
1564                 "Flag identifying this as a multicast route request.", HFILL }},
1565
1566             { &hf_zbee_nwk_cmd_route_opt_dest_ext,
1567             { "Extended Destination",   "zbee.nwk.cmd.route.opts.dest_ext", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_ROUTE_OPTION_DEST_EXT,
1568                 NULL, HFILL }},
1569
1570             { &hf_zbee_nwk_cmd_route_opt_resp_ext,
1571             { "Extended Responder",   "zbee.nwk.cmd.route.opts.resp_ext", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_ROUTE_OPTION_RESP_EXT,
1572                 NULL, HFILL }},
1573
1574             { &hf_zbee_nwk_cmd_route_opt_orig_ext,
1575             { "Extended Originator",    "zbee.nwk.cmd.route.opts.orig_ext", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_ROUTE_OPTION_ORIG_EXT,
1576                 NULL, HFILL }},
1577
1578             { &hf_zbee_nwk_cmd_route_opt_many_to_one,
1579             { "Many-to-One Discovery",  "zbee.nwk.cmd.route.opts.many2one", FT_UINT8, BASE_HEX, VALS(zbee_nwk_cmd_route_many_modes), ZBEE_NWK_CMD_ROUTE_OPTION_MANY_MASK,
1580                 NULL, HFILL }},
1581
1582             { &hf_zbee_nwk_cmd_nwk_status,
1583             { "Status Code",            "zbee.nwk.cmd.status", FT_UINT8, BASE_HEX, VALS(zbee_nwk_status_codes), 0x0,
1584                 NULL, HFILL }},
1585
1586             { &hf_zbee_nwk_cmd_leave_rejoin,
1587             { "Rejoin",                 "zbee.nwk.cmd.leave.rejoin", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_LEAVE_OPTION_REJOIN,
1588                 "Flag instructing the device to rejoin the network.", HFILL }},
1589
1590             { &hf_zbee_nwk_cmd_leave_request,
1591             { "Request",                "zbee.nwk.cmd.leave.request", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_LEAVE_OPTION_REQUEST,
1592                 "Flag identifying the direction of this command. 1=Request, 0=Indication", HFILL }},
1593
1594             { &hf_zbee_nwk_cmd_leave_children,
1595             { "Remove Children",        "zbee.nwk.cmd.leave.children", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_LEAVE_OPTION_CHILDREN,
1596                 "Flag instructing the device to remove its children in addition to itself.", HFILL }},
1597
1598             { &hf_zbee_nwk_cmd_relay_count,
1599             { "Relay Count",            "zbee.nwk.cmd.relay_count", FT_UINT8, BASE_DEC, NULL, 0x0,
1600                 "Number of relays required to route to the destination.", HFILL }},
1601
1602             { &hf_zbee_nwk_cmd_cinfo_alt_coord,
1603             { "Alternate Coordinator",  "zbee.nwk.cmd.cinfo.alt_coord", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_ALT_PAN_COORD,
1604                 "Indicates that the device is able to operate as a PAN coordinator.", HFILL }},
1605
1606             { &hf_zbee_nwk_cmd_cinfo_type,
1607             { "Full-Function Device",   "zbee.nwk.cmd.cinfo.ffd", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_DEVICE_TYPE,
1608                 NULL, HFILL }},
1609
1610             { &hf_zbee_nwk_cmd_cinfo_power,
1611             { "AC Power",               "zbee.nwk.cmd.cinfo.power", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_POWER_SRC,
1612                 "Indicates this device is using AC/Mains power.", HFILL }},
1613
1614             { &hf_zbee_nwk_cmd_cinfo_idle_rx,
1615             { "Rx On When Idle",        "zbee.nwk.cmd.cinfo.power", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_IDLE_RX,
1616                 "Indicates the receiver is active when the device is idle.", HFILL }},
1617
1618             { &hf_zbee_nwk_cmd_cinfo_security,
1619             { "Security Capability",    "zbee.nwk.cmd.cinfo.security", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_SEC_CAPABLE,
1620                 "Indicates this device is capable of performing encryption/decryption.", HFILL }},
1621
1622             { &hf_zbee_nwk_cmd_cinfo_alloc,
1623             { "Allocate Short Address", "zbee.nwk.cmd.cinfo.alloc", FT_BOOLEAN, 8, NULL, IEEE802154_CMD_CINFO_ALLOC_ADDR,
1624                 "Flag requesting the parent to allocate a short address for this device.", HFILL }},
1625
1626             { &hf_zbee_nwk_cmd_rejoin_status,
1627             { "Status",                 "zbee.nwk.cmd.rejoin_status", FT_UINT8, BASE_HEX, VALS(zbee_nwk_rejoin_codes), 0x0,
1628                 NULL, HFILL }},
1629
1630             { &hf_zbee_nwk_cmd_link_last,
1631             { "Last Frame",             "zbee.nwk.cmd.link.last", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_LINK_OPTION_LAST_FRAME,
1632                 "Flag indicating the last in a series of link status commands.", HFILL }},
1633
1634             { &hf_zbee_nwk_cmd_link_first,
1635             { "First Frame",            "zbee.nwk.cmd.link.first", FT_BOOLEAN, 8, NULL, ZBEE_NWK_CMD_LINK_OPTION_FIRST_FRAME,
1636                 "Flag indicating the first in a series of link status commands.", HFILL }},
1637
1638             { &hf_zbee_nwk_cmd_link_count,
1639             { "Link Status Count",      "zbee.nwk.cmd.link.count", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_CMD_LINK_OPTION_COUNT_MASK,
1640                 NULL, HFILL }},
1641
1642             { &hf_zbee_nwk_cmd_report_type,
1643             { "Report Type",            "zbee.nwk.cmd.report.type", FT_UINT8, BASE_HEX, VALS(zbee_nwk_report_types), ZBEE_NWK_CMD_NWK_REPORT_ID_MASK,
1644                 NULL, HFILL }},
1645
1646             { &hf_zbee_nwk_cmd_report_count,
1647             { "Report Information Count",   "zbee.nwk.cmd.report.count", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_CMD_NWK_REPORT_COUNT_MASK,
1648                 NULL, HFILL }},
1649
1650             { &hf_zbee_nwk_cmd_update_type,
1651             { "Update Type",            "zbee.nwk.cmd.update.type", FT_UINT8, BASE_HEX, VALS(zbee_nwk_update_types), ZBEE_NWK_CMD_NWK_UPDATE_ID_MASK,
1652                 NULL, HFILL }},
1653
1654             { &hf_zbee_nwk_cmd_update_count,
1655             { "Update Information Count",   "zbee.nwk.cmd.update.count", FT_UINT8, BASE_DEC, NULL, ZBEE_NWK_CMD_NWK_UPDATE_COUNT_MASK,
1656                 NULL, HFILL }},
1657
1658             { &hf_zbee_nwk_cmd_update_id,
1659             { "Update ID",              "zbee.nwk.cmd.update.id", FT_UINT8, BASE_DEC, NULL, 0x0,
1660                 NULL, HFILL }},
1661
1662             { &hf_zbee_nwk_cmd_epid,
1663             { "Extended PAN ID",        "zbee.nwk.cmd.epid", FT_UINT64, BASE_HEX, NULL, 0x0,
1664                 NULL, HFILL }},
1665
1666             { &hf_zbee_beacon_protocol,
1667             { "Protocol ID",            "zbee.beacon.protocol", FT_UINT8, BASE_DEC, NULL, 0x0,
1668                 NULL, HFILL }},
1669
1670             { &hf_zbee_beacon_stack_profile,
1671             { "Stack Profile",          "zbee.beacon.profile", FT_UINT8, BASE_HEX, VALS(zbee_nwk_stack_profiles), 0x0,
1672                 NULL, HFILL }},
1673
1674             { &hf_zbee_beacon_version,
1675             { "Protocol Version",       "zbee.beacon.version", FT_UINT8, BASE_DEC, NULL, 0x0,
1676                 NULL, HFILL }},
1677
1678             { &hf_zbee_beacon_router_capacity,
1679             { "Router Capacity", "zbee.beacon.router", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1680                 "Whether the device can accept join requests from routing capable devices.", HFILL }},
1681
1682             { &hf_zbee_beacon_depth,
1683             { "Device Depth",           "zbee.beacon.depth", FT_UINT8, BASE_DEC, NULL, 0x0,
1684                 "The tree depth of the device, 0 indicates the network coordinator.", HFILL }},
1685
1686             { &hf_zbee_beacon_end_device_capacity,
1687             { "End Device Capacity",        "zbee.beacon.end_dev", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1688                 "Whether the device can accept join requests from ZigBee end devices.", HFILL }},
1689
1690             { &hf_zbee_beacon_epid,
1691             { "Extended PAN ID",        "zbee.beacon.ext_panid", FT_UINT64, BASE_HEX, NULL, 0x0,
1692                 "Extended PAN identifier.", HFILL }},
1693
1694             { &hf_zbee_beacon_tx_offset,
1695             { "Tx Offset",              "zbee.beacon.tx_offset", FT_UINT32, BASE_DEC, NULL, 0x0,
1696                 "The time difference between a device and its parent's beacon.", HFILL }},
1697
1698             { &hf_zbee_beacon_update_id,
1699             { "Update ID",              "zbee.beacon.update_id", FT_UINT8, BASE_DEC, NULL, 0x0,
1700                 NULL, HFILL }}
1701     };
1702
1703     /*  NWK Layer subtrees */
1704     static gint *ett[] = {
1705         &ett_zbee_nwk,
1706         &ett_zbee_beacon,
1707         &ett_zbee_nwk_fcf,
1708         &ett_zbee_nwk_mcast,
1709         &ett_zbee_nwk_route,
1710         &ett_zbee_nwk_cmd,
1711         &ett_zbee_nwk_cmd_options,
1712         &ett_zbee_nwk_cmd_cinfo
1713     };
1714
1715     /* Register the protocol with Wireshark. */
1716     proto_zbee_nwk = proto_register_protocol("ZigBee Network Layer", "ZigBee NWK", "zbee.nwk");
1717     proto_register_field_array(proto_zbee_nwk, hf, array_length(hf));
1718     proto_register_subtree_array(ett, array_length(ett));
1719
1720     /* Register the dissectors with Wireshark. */
1721     register_dissector("zbee.nwk", dissect_zbee_nwk, proto_zbee_nwk);
1722     register_dissector("zbee.beacon", dissect_zbee_beacon, proto_zbee_nwk);
1723
1724     /* Register the Security dissector. */
1725     zbee_security_register(NULL, proto_zbee_nwk);
1726 } /* proto_register_zbee */
1727
1728 /*FUNCTION:------------------------------------------------------
1729  *  NAME
1730  *      proto_reg_handoff_zbee_nwk
1731  *  DESCRIPTION
1732  *      Registers the zigbee dissector with Wireshark.
1733  *  PARAMETERS
1734  *      none
1735  *  RETURNS
1736  *      void
1737  *---------------------------------------------------------------
1738  */
1739 void proto_reg_handoff_zbee_nwk(void)
1740 {
1741     /* Find the other dissectors we need. */
1742     data_handle     = find_dissector("data");
1743     aps_handle      = find_dissector("zbee.aps");
1744
1745     /* Register our dissector with IEEE 802.15.4 */
1746     heur_dissector_add("wpan", dissect_zbee_nwk_heur, proto_zbee_nwk);
1747
1748     /* Handoff the ZigBee security dissector code. */
1749     zbee_security_handoff();
1750 } /* proto_reg_handoff_zbee */