Add an argument to abs_time_to_str() and abs_time_secs_to_str()
[obnox/wireshark/wip.git] / epan / dissectors / packet-dnp.c
1 /* packet-dnp.c
2  * Routines for DNP dissection
3  * Copyright 2003, 2006, 2007, Graham Bloice <graham.bloice@trihedral.com>
4  *
5  * DNP3.0 Application Layer Object dissection added by Chris Bontje (chrisbontje@shaw.ca)
6  * Copyright 2005
7  *
8  * Major updates: tcp and application layer defragmentation, more object dissections by Graham Bloice
9  *
10  * $Id$
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald@wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39 #include <glib.h>
40 #include <time.h>
41
42 #include <epan/packet.h>
43 #include <epan/prefs.h>
44 #include <epan/reassemble.h>
45 #include <epan/emem.h>
46 #include <epan/dissectors/packet-tcp.h>
47 #include <epan/conversation.h>
48 #include <epan/expert.h>
49
50 /*
51  * See
52  *
53  * http://www.dnp.org/
54  *
55  * although note that you have to join the DNP organization to get to
56  * see the protocol specs online - otherwise, you have to buy a
57  * dead-tree version.
58  *
59  * ...Application Layer Notes...
60  *
61  * Application Layer Decoding based on information available in
62  * DNP3 Basic 4 Documentation Set, specifically the document:
63  * "DNP V3.00 Application Layer" v0.03 P009-0PD.APP & Technical Bulletins
64  *
65  * ---------------------------------------------------------------------------
66  *
67  * Several command codes were missing, causing the dissector to abort decoding
68  * on valid packets.  Those commands have been added.
69  * 
70  * The semantics of Variation 0 have been cleaned up.  Variation 0 is the 
71  * "Default Variation".  It is used only in Master -> Slave read commands
72  * to request the data in whatever variation the Slave is configured to use by 
73  * default. Decoder strings have been added to the Binary Output and 
74  * Analog Output objects (10 and 40) so that group read commands will 
75  * decode properly.
76  *
77  * Roy M. Silvernail <roy@rant-central.com> 01/05/2009
78  *
79  */
80
81 /***************************************************************************/
82 /* DNP 3.0 Constants */
83 /***************************************************************************/
84 #define DNP_HDR_LEN     10
85 #define TCP_PORT_DNP    20000
86 #define UDP_PORT_DNP    20000
87
88 /***************************************************************************/
89 /* Datalink and Transport Layer Bit-Masks */
90 /***************************************************************************/
91 #define DNP3_CTL_DIR    0x80
92 #define DNP3_CTL_PRM    0x40
93 #define DNP3_CTL_FCB    0x20
94 #define DNP3_CTL_FCV    0x10
95 #define DNP3_CTL_RES    0x20
96 #define DNP3_CTL_DFC    0x10
97 #define DNP3_CTL_FUNC   0x0f
98
99 #define DNP3_TR_FIR   0x40
100 #define DNP3_TR_FIN   0x80
101 #define DNP3_TR_SEQ   0x3f
102
103 #define AL_MAX_CHUNK_SIZE 16
104
105 /***************************************************************************/
106 /* Data Link Function codes */
107 /***************************************************************************/
108 /* Primary to Secondary */
109 #define DL_FUNC_RESET_LINK  0x00
110 #define DL_FUNC_RESET_PROC  0x01
111 #define DL_FUNC_TEST_LINK   0x02
112 #define DL_FUNC_USER_DATA   0x03
113 #define DL_FUNC_UNC_DATA    0x04
114 #define DL_FUNC_LINK_STAT   0x09
115
116 /* Secondary to Primary */
117 #define DL_FUNC_ACK         0x00
118 #define DL_FUNC_NACK        0x01
119 #define DL_FUNC_STAT_LINK   0x0B
120 #define DL_FUNC_NO_FUNC     0x0E
121 #define DL_FUNC_NOT_IMPL    0x0F
122
123 /***************************************************************************/
124 /* Application Layer Bit-Masks */
125 /***************************************************************************/
126 #define DNP3_AL_UNS   0x10
127 #define DNP3_AL_CON   0x20
128 #define DNP3_AL_FIN   0x40
129 #define DNP3_AL_FIR   0x80
130 #define DNP3_AL_SEQ   0x0f
131 #define DNP3_AL_FUNC  0xff
132
133 /***************************************************************************/
134 /* Application Layer Function codes */
135 /***************************************************************************/
136 #define AL_FUNC_CONFIRM    0x00    /* 00  - Confirm */
137 #define AL_FUNC_READ       0x01    /* 01  - Read */
138 #define AL_FUNC_WRITE      0x02    /* 02  - Write */
139 #define AL_FUNC_SELECT     0x03    /* 03  - Select */
140 #define AL_FUNC_OPERATE    0x04    /* 04  - Operate */
141 #define AL_FUNC_DIROP      0x05    /* 05  - Direct Operate */
142 #define AL_FUNC_DIROPNACK  0x06    /* 06  - Direct Operate No ACK */
143 #define AL_FUNC_FRZ        0x07    /* 07  - Immediate Freeze */
144 #define AL_FUNC_FRZNACK    0x08    /* 08  - Immediate Freeze No ACK */
145 #define AL_FUNC_FRZCLR     0x09    /* 09  - Freeze and Clear */
146 #define AL_FUNC_FRZCLRNACK 0x0A    /* 10  - Freeze and Clear No ACK */
147 #define AL_FUNC_FRZT       0x0B    /* 11  - Freeze With Time */
148 #define AL_FUNC_FRZTNACK   0x0C    /* 12  - Freeze With Time No ACK */
149 #define AL_FUNC_COLDRST    0x0D    /* 13  - Cold Restart */
150 #define AL_FUNC_WARMRST    0x0E    /* 14  - Warm Restart */
151 #define AL_FUNC_INITDATA   0x0F    /* 15  - Initialize Data */
152 #define AL_FUNC_INITAPP    0x10    /* 16  - Initialize Application */
153 #define AL_FUNC_STARTAPP   0x11    /* 17  - Start Application */
154 #define AL_FUNC_STOPAPP    0x12    /* 18  - Stop Application */
155 #define AL_FUNC_SAVECFG    0x13    /* 19  - Save Configuration */
156 #define AL_FUNC_ENSPMSG    0x14    /* 20  - Enable Spontaneous Msg */
157 #define AL_FUNC_DISSPMSG   0x15    /* 21  - Disable Spontaneous Msg */
158 #define AL_FUNC_ASSIGNCL   0x16    /* 22  - Assign Classes */
159 #define AL_FUNC_DELAYMST   0x17    /* 23  - Delay Measurement */
160 #define AL_FUNC_RECCT      0x18    /* 24  - Record Current Time */
161 #define AL_FUNC_OPENFILE   0x19    /* 25  - Open File */
162 #define AL_FUNC_CLOSEFILE  0x1A    /* 26  - Close File */
163 #define AL_FUNC_DELETEFILE 0x1B    /* 27  - Delete File */
164 #define AL_FUNC_GETFILEINF 0x1C    /* 28  - Get File Info */
165 #define AL_FUNC_AUTHFILE   0x1D    /* 29  - Authenticate File */
166 #define AL_FUNC_ABORTFILE  0x1E    /* 30  - Abort File */
167 #define AL_FUNC_ACTCNF     0x1F    /* 31  - Activate Config */
168 #define AL_FUNC_AUTHREQ    0x20    /* 32  - Authentication Request */
169 #define AL_FUNC_AUTHERR    0x21    /* 33  - Authentication Error */
170 #define AL_FUNC_RESPON     0x81    /* 129 - Response */
171 #define AL_FUNC_UNSOLI     0x82    /* 130 - Unsolicited Response */
172 #define AL_FUNC_AUTHRESP   0x83    /* 131 - Authentication Response */
173
174 /***************************************************************************/
175 /* Application Layer Internal Indication (IIN) bits */
176 /* 2 Bytes, message formatting: [First Octet] | [Second Octet] */
177 /***************************************************************************/
178 /* Octet 1 */
179 #define AL_IIN_BMSG        0x0100   /* Bit 0 - Broadcast message rx'd */
180 #define AL_IIN_CLS1D       0x0200   /* Bit 1 - Class 1 Data Available */
181 #define AL_IIN_CLS2D       0x0400   /* Bit 2 - Class 2 Data Available */
182 #define AL_IIN_CLS3D       0x0800   /* Bit 3 - Class 3 Data Available */
183 #define AL_IIN_TSR         0x1000   /* Bit 4 - Time Sync Req'd from Master */
184 #define AL_IIN_DOL         0x2000   /* Bit 5 - Digital Outputs in Local Mode */
185 #define AL_IIN_DT          0x4000   /* Bit 6 - Device Trouble */
186 #define AL_IIN_RST         0x8000   /* Bit 7 - Device Restart */
187
188 /* Octet 2 */
189                         /* 0x0001      Bit 0 - Reserved */
190 #define AL_IIN_OBJU        0x0002   /* Bit 1 - Requested Objects Unknown */
191 #define AL_IIN_PIOOR       0x0004   /* Bit 2 - Parameters Invalid or Out of Range */
192 #define AL_IIN_EBO         0x0008   /* Bit 3 - Event Buffer Overflow */
193 #define AL_IIN_OAE         0x0010   /* Bit 4 - Operation Already Executing */
194 #define AL_IIN_CC          0x0020   /* Bit 5 - Device Configuration Corrupt */
195                         /* 0x0040      Bit 6 - Reserved */
196                         /* 0x0080      Bit 7 - Reserved */
197
198 /***************************************************************************/
199 /* Application Layer Data Object Qualifier */
200 /***************************************************************************/
201 /* Bit-Masks */
202 #define AL_OBJQ_INDEX      0x70     /* x111xxxx Masks Index from Qualifier */
203 #define AL_OBJQ_CODE       0x0F     /* xxxx1111 Masks Code from Qualifier */
204
205 /* Index Size (3-bits x111xxxx)            */
206 /* When Qualifier Code != 11               */
207 #define AL_OBJQL_IDX_NI    0x00    /* Objects are Packed with no index */
208 #define AL_OBJQL_IDX_1O    0x01    /* Objects are prefixed w/ 1-octet index */
209 #define AL_OBJQL_IDX_2O    0x02    /* Objects are prefixed w/ 2-octet index */
210 #define AL_OBJQL_IDX_4O    0x03    /* Objects are prefixed w/ 4-octet index */
211 #define AL_OBJQL_IDX_1OS   0x04    /* Objects are prefixed w/ 1-octet object size */
212 #define AL_OBJQL_IDX_2OS   0x05    /* Objects are prefixed w/ 2-octet object size */
213 #define AL_OBJQL_IDX_4OS   0x06    /* Objects are prefixed w/ 4-octet object size */
214
215 /* When Qualifier Code == 11 */
216 #define AL_OBJQL_IDX11_1OIS    0x01    /* 1 octet identifier size */
217 #define AL_OBJQL_IDX11_2OIS    0x02    /* 2 octet identifier size */
218 #define AL_OBJQL_IDX11_4OIS    0x03    /* 4 octet identifier size */
219
220 /* Qualifier Code (4-bits) */
221 /* 4-bits ( xxxx1111 ) */
222 #define AL_OBJQL_CODE_SSI8     0x00    /* 00 8-bit Start and Stop Indices in Range Field */
223 #define AL_OBJQL_CODE_SSI16    0x01    /* 01 16-bit Start and Stop Indices in Range Field */
224 #define AL_OBJQL_CODE_SSI32    0x02    /* 02 32-bit Start and Stop Indices in Range Field */
225 #define AL_OBJQL_CODE_AA8      0x03    /* 03 8-bit Absolute Address in Range Field */
226 #define AL_OBJQL_CODE_AA16     0x04    /* 04 16-bit Absolute Address in Range Field */
227 #define AL_OBJQL_CODE_AA32     0x05    /* 05 32-bit Absolute Address in Range Field */
228 #define AL_OBJQL_CODE_R0       0x06    /* 06 Length of Range field is 0 (no range field) */
229 #define AL_OBJQL_CODE_SF8      0x07    /* 07 8-bit Single Field Quantity */
230 #define AL_OBJQL_CODE_SF16     0x08    /* 08 16-bit Single Field Quantity */
231 #define AL_OBJQL_CODE_SF32     0x09    /* 09 32-bit Single Field Quantity */
232                            /*  0x0A       10 Reserved  */
233 #define AL_OBJQL_CODE_FF       0x0B    /* 11 Free-format Qualifier */
234                            /*  0x0C       12 Reserved  */
235                            /*  0x0D       13 Reserved  */
236                            /*  0x0E       14 Reserved  */
237                            /*  0x0F       15 Reserved  */
238
239 /***************************************************************************/
240 /* Application Layer Data Object Definitions                               */
241 /***************************************************************************/
242 /* Binary Input Objects */
243 #define AL_OBJ_BI_ALL      0x0100   /* 01 00 Binary Input Default Variation */
244 #define AL_OBJ_BI_1BIT     0x0101   /* 01 01 Single-bit Binary Input */
245 #define AL_OBJ_BI_STAT     0x0102   /* 01 02 Binary Input With Status */
246 #define AL_OBJ_BIC_ALL     0x0200   /* 02 00 Binary Input Change Default Variation */
247 #define AL_OBJ_BIC_NOTIME  0x0201   /* 02 01 Binary Input Change Without Time */
248 #define AL_OBJ_BIC_TIME    0x0202   /* 02 02 Binary Input Change With Time */
249 #define AL_OBJ_BIC_RTIME   0x0203   /* 02 03 Binary Input Change With Relative Time */
250
251 /* Double-bit Input Objects */
252 #define AL_OBJ_2BI_ALL     0x0300   /* 03 00 Double-bit Input Default Variation */
253 #define AL_OBJ_2BI_NF      0x0301   /* 03 01 Double-bit Input No Flags */
254 #define AL_OBJ_2BI_STAT    0x0302   /* 03 02 Double-bit Input With Status */
255 #define AL_OBJ_2BIC_NOTIME 0x0401   /* 04 01 Double-bit Input Change Without Time */
256 #define AL_OBJ_2BIC_TIME   0x0402   /* 04 02 Double-bit Input Change With Time */
257 #define AL_OBJ_2BIC_RTIME  0x0403   /* 04 03 Double-bit Input Change With Relative Time */
258
259 /* Binary Input Quality Flags */
260 #define AL_OBJ_BI_FLAG0    0x0001   /* Point Online (0=Offline; 1=Online) */
261 #define AL_OBJ_BI_FLAG1    0x0002   /* Restart (0=Normal; 1=Restart) */
262 #define AL_OBJ_BI_FLAG2    0x0004   /* Comms Lost (0=Normal; 1=Lost) */
263 #define AL_OBJ_BI_FLAG3    0x0008   /* Remote Force (0=Normal; 1=Forced) */
264 #define AL_OBJ_BI_FLAG4    0x0010   /* Local Force (0=Normal; 1=Forced) */
265 #define AL_OBJ_BI_FLAG5    0x0020   /* Chatter Filter (0=Normal; 1=Filter On) */
266 #define AL_OBJ_BI_FLAG6    0x0040   /* Double-bit LSB (0=Off; 1=On) */
267 #define AL_OBJ_BI_FLAG7    0x0080   /* Point State (0=Off; 1=On) or Double-bit MSB */
268
269 /***************************************************************************/
270 /* Binary Output Objects */
271 #define AL_OBJ_BO_ALL      0x0A00   /* 10 00 Binary Output Default Variation */
272 #define AL_OBJ_BO          0x0A01   /* 10 01 Binary Output */
273 #define AL_OBJ_BO_STAT     0x0A02   /* 10 02 Binary Output Status */
274 #define AL_OBJ_CTLOP_BLK   0x0C01   /* 12 01 Control Relay Output Block */
275                         /* 0x0C02      12 02 Pattern Control Block */
276                         /* 0x0C03      12 03 Pattern Mask */
277
278 #define AL_OBJCTLC_CODE    0x0F    /* Bit-Mask xxxx1111 for Control Code 'Code' */
279 #define AL_OBJCTLC_MISC    0x30    /* Bit-Mask xx11xxxx for Control Code Misc Values */
280 #define AL_OBJCTLC_TC      0xC0    /* Bit-Mask 11xxxxxx for Control Code 'Trip/Close' */
281
282 #define AL_OBJCTLC_CODE0   0x00    /* xxxx0000 NUL Operation; only process R attribute */
283 #define AL_OBJCTLC_CODE1   0x01    /* xxxx0001 Pulse On ^On-Time -> vOff-Time, remain off */
284 #define AL_OBJCTLC_CODE2   0x02    /* xxxx0010 Pulse Off vOff-Time -> ^On-Time, remain on */
285 #define AL_OBJCTLC_CODE3   0x03    /* xxxx0011 Latch On */
286 #define AL_OBJCTLC_CODE4   0x04    /* xxxx0100 Latch Off */
287                         /* 0x05-0x15  Reserved */
288
289 #define AL_OBJCTLC_QUEUE   0x10    /* xxx1xxxx for Control Code 'Queue' */
290 #define AL_OBJCTLC_CLEAR   0x20    /* xx1xxxxx for Control Code 'Clear' */
291
292 #define AL_OBJCTLC_TC0     0x00    /* 00xxxxxx NUL */
293 #define AL_OBJCTLC_TC1     0x40    /* 01xxxxxx Close */
294 #define AL_OBJCTLC_TC2     0x80    /* 10xxxxxx Trip */
295
296 #define AL_OBJCTL_STAT0    0x00    /* Request Accepted, Initiated or Queued */
297 #define AL_OBJCTL_STAT1    0x01    /* Request Not Accepted; Arm-timer expired */
298 #define AL_OBJCTL_STAT2    0x02    /* Request Not Accepted; No 'SELECT' rx'd */
299 #define AL_OBJCTL_STAT3    0x03    /* Request Not Accepted; Format errors in ctrl request */
300 #define AL_OBJCTL_STAT4    0x04    /* Control Operation Not Supported for this point */
301 #define AL_OBJCTL_STAT5    0x05    /* Request Not Accepted; Ctrl Queue full or pt. active */
302 #define AL_OBJCTL_STAT6    0x06    /* Request Not Accepted; Ctrl HW Problems */
303 #define AL_OBJCTL_STAT7    0x07    /* Request Not Accepted; Local/Remote switch in Local*/
304 #define AL_OBJCTL_STAT8    0x08    /* Request Not Accepted; Too many operations requested */
305 #define AL_OBJCTL_STAT9    0x09    /* Request Not Accepted; Insufficient authorization */
306 #define AL_OBJCTL_STAT10   0x0A    /* Request Not Accepted; Local automation proc active */
307
308 /* Binary Output Quality Flags */
309 #define AL_OBJ_BO_FLAG0    0x0001   /* Point Online (0=Offline; 1=Online) */
310 #define AL_OBJ_BO_FLAG1    0x0002   /* Restart (0=Normal; 1=Restart) */
311 #define AL_OBJ_BO_FLAG2    0x0004   /* Comms Lost (0=Normal; 1=Lost) */
312 #define AL_OBJ_BO_FLAG3    0x0008   /* Remote Force (0=Normal; 1=Forced) */
313 #define AL_OBJ_BO_FLAG4    0x0010   /* Local Force (0=Normal; 1=Forced) */
314 #define AL_OBJ_BO_FLAG5    0x0020   /* Reserved */
315 #define AL_OBJ_BO_FLAG6    0x0040   /* Reserved */
316 #define AL_OBJ_BO_FLAG7    0x0080   /* Point State (0=Off; 1=On) */
317
318 /***************************************************************************/
319 /* Counter Objects */
320 #define AL_OBJ_CTR_ALL     0x1400   /* 20 00 Binary Counter Default Variation */
321 #define AL_OBJ_CTR_32      0x1401   /* 20 01 32-Bit Binary Counter */
322 #define AL_OBJ_CTR_16      0x1402   /* 20 02 16-Bit Binary Counter */
323 #define AL_OBJ_DCTR_32     0x1403   /* 20 03 32-Bit Delta Counter */
324 #define AL_OBJ_DCTR_16     0x1404   /* 20 04 16-Bit Delta Counter */
325 #define AL_OBJ_CTR_32NF    0x1405   /* 20 05 32-Bit Binary Counter Without Flag */
326 #define AL_OBJ_CTR_16NF    0x1406   /* 20 06 16-Bit Binary Counter Without Flag */
327 #define AL_OBJ_DCTR_32NF   0x1407   /* 20 07 32-Bit Delta Counter Without Flag */
328 #define AL_OBJ_DCTR_16NF   0x1408   /* 20 08 16-Bit Delta Counter Without Flag */
329 #define AL_OBJ_FCTR_ALL    0x1500   /* 21 00 Frozen Binary Counter Default Variation */
330 #define AL_OBJ_FCTR_32     0x1501   /* 21 01 32-Bit Frozen Counter */
331 #define AL_OBJ_FCTR_16     0x1502   /* 21 02 16-Bit Frozen Counter */
332 #define AL_OBJ_FDCTR_32    0x1503   /* 21 03 32-Bit Frozen Delta Counter */
333 #define AL_OBJ_FDCTR_16    0x1504   /* 21 04 16-Bit Frozen Delta Counter */
334 #define AL_OBJ_FCTR_32T    0x1505   /* 21 05 32-Bit Frozen Counter w/ Time of Freeze */
335 #define AL_OBJ_FCTR_16T    0x1506   /* 21 06 16-Bit Frozen Counter w/ Time of Freeze */
336 #define AL_OBJ_FDCTR_32T   0x1507   /* 21 07 32-Bit Frozen Delta Counter w/ Time of Freeze */
337 #define AL_OBJ_FDCTR_16T   0x1508   /* 21 08 16-Bit Frozen Delta Counter w/ Time of Freeze */
338 #define AL_OBJ_FCTR_32NF   0x1509   /* 21 09 32-Bit Frozen Counter Without Flag */
339 #define AL_OBJ_FCTR_16NF   0x1510   /* 21 10 16-Bit Frozen Counter Without Flag */
340 #define AL_OBJ_FDCTR_32NF  0x1511   /* 21 11 32-Bit Frozen Delta Counter Without Flag */
341 #define AL_OBJ_FDCTR_16NF  0x1512   /* 21 12 16-Bit Frozen Delta Counter Without Flag */
342 #define AL_OBJ_CTRC_ALL    0x1600   /* 22 00 Counter Change Event Default Variation */
343 #define AL_OBJ_CTRC_32     0x1601   /* 22 01 32-Bit Counter Change Event w/o Time */
344 #define AL_OBJ_CTRC_16     0x1602   /* 22 02 16-Bit Counter Change Event w/o Time */
345 #define AL_OBJ_DCTRC_32    0x1603   /* 22 03 32-Bit Delta Counter Change Event w/o Time */
346 #define AL_OBJ_DCTRC_16    0x1604   /* 22 04 16-Bit Delta Counter Change Event w/o Time */
347 #define AL_OBJ_CTRC_32T    0x1605   /* 22 05 32-Bit Counter Change Event with Time */
348 #define AL_OBJ_CTRC_16T    0x1606   /* 22 06 16-Bit Counter Change Event with Time */
349 #define AL_OBJ_DCTRC_32T   0x1607   /* 22 07 32-Bit Delta Counter Change Event with Time */
350 #define AL_OBJ_DCTRC_16T   0x1608   /* 22 08 16-Bit Delta Counter Change Event with Time */
351 #define AL_OBJ_FCTRC_ALL   0x1700   /* 21 00 Frozen Binary Counter Change Event Default Variation */
352 #define AL_OBJ_FCTRC_32    0x1701   /* 21 01 32-Bit Frozen Counter Change Event */
353 #define AL_OBJ_FCTRC_16    0x1702   /* 21 02 16-Bit Frozen Counter Change Event */
354 #define AL_OBJ_FDCTRC_32   0x1703   /* 21 03 32-Bit Frozen Delta Counter Change Event */
355 #define AL_OBJ_FDCTRC_16   0x1704   /* 21 04 16-Bit Frozen Delta Counter Change Event */
356 #define AL_OBJ_FCTRC_32T   0x1705   /* 21 05 32-Bit Frozen Counter Change Event w/ Time of Freeze */
357 #define AL_OBJ_FCTRC_16T   0x1706   /* 21 06 16-Bit Frozen Counter Change Event w/ Time of Freeze */
358 #define AL_OBJ_FDCTRC_32T  0x1707   /* 21 07 32-Bit Frozen Delta Counter Change Event w/ Time of Freeze */
359 #define AL_OBJ_FDCTRC_16T  0x1708   /* 21 08 16-Bit Frozen Delta Counter Change Event w/ Time of Freeze */
360
361 /* Counter Quality Flags */
362 #define AL_OBJ_CTR_FLAG0   0x0001   /* Point Online (0=Offline; 1=Online) */
363 #define AL_OBJ_CTR_FLAG1   0x0002   /* Restart (0=Normal; 1=Restart) */
364 #define AL_OBJ_CTR_FLAG2   0x0004   /* Comms Lost (0=Normal; 1=Lost) */
365 #define AL_OBJ_CTR_FLAG3   0x0008   /* Remote Force (0=Normal; 1=Forced) */
366 #define AL_OBJ_CTR_FLAG4   0x0010   /* Local Force (0=Normal; 1=Forced) */
367 #define AL_OBJ_CTR_FLAG5   0x0020   /* Roll-over (0=Normal; 1=Roll-Over) */
368 #define AL_OBJ_CTR_FLAG6   0x0040   /* Discontinuity (0=Normal; 1=Discontinuity) */
369 #define AL_OBJ_CTR_FLAG7   0x0080   /* Reserved */
370
371 /***************************************************************************/
372 /* Analog Input Objects */
373 #define AL_OBJ_AI_ALL      0x1E00   /* 30 00 Analog Input Default Variation */
374 #define AL_OBJ_AI_32       0x1E01   /* 30 01 32-Bit Analog Input */
375 #define AL_OBJ_AI_16       0x1E02   /* 30 02 16-Bit Analog Input */
376 #define AL_OBJ_AI_32NF     0x1E03   /* 30 03 32-Bit Analog Input Without Flag */
377 #define AL_OBJ_AI_16NF     0x1E04   /* 30 04 16-Bit Analog Input Without Flag */
378 #define AL_OBJ_AI_FLT      0x1E05   /* 30 05 32-Bit Floating Point Input */
379 #define AL_OBJ_AI_DBL      0x1E06   /* 30 06 64-Bit Floating Point Input */
380                         /* 0x1F01      31 01 32-Bit Frozen Analog Input */
381                         /* 0x1F02      31 02 16-Bit Frozen Analog Input */
382                         /* 0x1F03      31 03 32-Bit Frozen Analog Input w/ Time of Freeze */
383                         /* 0x1F04      31 04 16-Bit Frozen Analog Input w/ Time of Freeze */
384                         /* 0x1F05      31 05 32-Bit Frozen Analog Input Without Flag */
385                         /* 0x1F06      31 06 16-Bit Frozen Analog Input Without Flag */
386 #define AL_OBJ_AIF_FLT     0x1F07   /* 31 07 32-Bit Frozen Floating Point Input */
387 #define AL_OBJ_AIF_DBL     0x1F08   /* 31 08 64-Bit Frozen Floating Point Input */
388 #define AL_OBJ_AIC_ALL     0x2000   /* 32 00 Analog Input Change Default Variation */
389 #define AL_OBJ_AIC_32NT    0x2001   /* 32 01 32-Bit Analog Change Event w/o Time */
390 #define AL_OBJ_AIC_16NT    0x2002   /* 32 02 16-Bit Analog Change Event w/o Time */
391 #define AL_OBJ_AIC_32T     0x2003   /* 32 03 32-Bit Analog Change Event w/ Time */
392 #define AL_OBJ_AIC_16T     0x2004   /* 32 04 16-Bit Analog Change Event w/ Time */
393 #define AL_OBJ_AIC_FLTNT   0x2005   /* 32 05 32-Bit Floating Point Change Event w/o Time*/
394 #define AL_OBJ_AIC_DBLNT   0x2006   /* 32 06 64-Bit Floating Point Change Event w/o Time*/
395 #define AL_OBJ_AIC_FLTT    0x2007   /* 32 07 32-Bit Floating Point Change Event w/ Time*/
396 #define AL_OBJ_AIC_DBLT    0x2008   /* 32 08 64-Bit Floating Point Change Event w/ Time*/
397                         /* 0x2101      33 01 32-Bit Frozen Analog Event w/o Time */
398                         /* 0x2102      33 02 16-Bit Frozen Analog Event w/o Time */
399                         /* 0x2103      33 03 32-Bit Frozen Analog Event w/ Time */
400                         /* 0x2104      33 04 16-Bit Frozen Analog Event w/ Time */
401 #define AL_OBJ_AIFC_FLTNT  0x2105   /* 33 05 32-Bit Floating Point Frozen Change Event w/o Time*/
402 #define AL_OBJ_AIFC_DBLNT  0x2106   /* 33 06 64-Bit Floating Point Frozen Change Event w/o Time*/
403 #define AL_OBJ_AIFC_FLTT   0x2107   /* 33 07 32-Bit Floating Point Frozen Change Event w/ Time*/
404 #define AL_OBJ_AIFC_DBLT   0x2108   /* 33 08 64-Bit Floating Point Frozen Change Event w/ Time*/
405
406
407 /* Analog Input Quality Flags */
408 #define AL_OBJ_AI_FLAG0    0x0001   /* Point Online (0=Offline; 1=Online) */
409 #define AL_OBJ_AI_FLAG1    0x0002   /* Restart (0=Normal; 1=Restart) */
410 #define AL_OBJ_AI_FLAG2    0x0004   /* Comms Lost (0=Normal; 1=Lost) */
411 #define AL_OBJ_AI_FLAG3    0x0008   /* Remote Force (0=Normal; 1=Forced) */
412 #define AL_OBJ_AI_FLAG4    0x0010   /* Local Force (0=Normal; 1=Forced) */
413 #define AL_OBJ_AI_FLAG5    0x0020   /* Over-Range (0=Normal; 1=Over-Range) */
414 #define AL_OBJ_AI_FLAG6    0x0040   /* Reference Check (0=Normal; 1=Error) */
415 #define AL_OBJ_AI_FLAG7    0x0080   /* Reserved */
416
417 /***************************************************************************/
418 /* Analog Output Objects */
419 #define AL_OBJ_AO_ALL      0x2800   /* 40 00 Analog Output Default Variation */
420 #define AL_OBJ_AO_32       0x2801   /* 40 01 32-Bit Analog Output Status */
421 #define AL_OBJ_AO_16       0x2802   /* 40 02 16-Bit Analog Output Status */
422 #define AL_OBJ_AO_FLT      0x2803   /* 40 03 32-Bit Floating Point Output Status */
423 #define AL_OBJ_AO_DBL      0x2804   /* 40 04 64-Bit Floating Point Output Status */
424 #define AL_OBJ_AO_32OPB    0x2901   /* 41 01 32-Bit Analog Output Block */
425 #define AL_OBJ_AO_16OPB    0x2902   /* 41 02 16-Bit Analog Output Block */
426 #define AL_OBJ_AO_FLTOPB   0x2903   /* 41 03 32-Bit Floating Point Output Block */
427 #define AL_OBJ_AO_DBLOPB   0x2904   /* 41 04 64-Bit Floating Point Output Block */
428
429 /* Analog Output Quality Flags */
430 #define AL_OBJ_AO_FLAG0    0x0001   /* Point Online (0=Offline; 1=Online) */
431 #define AL_OBJ_AO_FLAG1    0x0002   /* Restart (0=Normal; 1=Restart) */
432 #define AL_OBJ_AO_FLAG2    0x0004   /* Comms Lost (0=Normal; 1=Lost) */
433 #define AL_OBJ_AO_FLAG3    0x0008   /* Remote Force (0=Normal; 1=Forced) */
434 #define AL_OBJ_AO_FLAG4    0x0010   /* Local Force (0=Normal; 1=Forced) */
435 #define AL_OBJ_AO_FLAG5    0x0020   /* Reserved */
436 #define AL_OBJ_AO_FLAG6    0x0040   /* Reserved */
437 #define AL_OBJ_AO_FLAG7    0x0080   /* Reserved */
438
439 /***************************************************************************/
440 /* Time Objects */
441 #define AL_OBJ_TD_ALL      0x3200   /* 50 00 Time and Date Default Variation */
442 #define AL_OBJ_TD          0x3201   /* 50 01 Time and Date */
443 #define AL_OBJ_TDI         0x3202   /* 50 02 Time and Date w/ Interval */
444 #define AL_OBJ_TDR         0x3203   /* 50 03 Last Recorded Time and Date */
445 #define AL_OBJ_TDCTO       0x3301   /* 51 01 Time and Date CTO */
446 #define AL_OBJ_UTDCTO      0x3302   /* 51 02 Unsynchronized Time and Date CTO */
447 #define AL_OBJ_TDELAYC     0x3401   /* 52 01 Time Delay Coarse */
448 #define AL_OBJ_TDELAYF     0x3402   /* 52 02 Time Delay Fine */
449
450 /***************************************************************************/
451 /* Class Data Objects */
452 #define AL_OBJ_CLASS0      0x3C01   /* 60 01 Class 0 Data */
453 #define AL_OBJ_CLASS1      0x3C02   /* 60 02 Class 1 Data */
454 #define AL_OBJ_CLASS2      0x3C03   /* 60 03 Class 2 Data */
455 #define AL_OBJ_CLASS3      0x3C04   /* 60 04 Class 3 Data */
456
457 /***************************************************************************/
458 /* Device Objects */
459 #define AL_OBJ_IIN         0x5001   /* 80 01 Internal Indications */
460
461 /***************************************************************************/
462 /* Octet String Objects */
463 #define AL_OBJ_OCT         0x6E00   /* 110 xx Octet string */
464
465 /***************************************************************************/
466 /* End of Application Layer Data Object Definitions */
467 /***************************************************************************/
468
469 /* Initialize the protocol and registered fields */
470 static int proto_dnp3 = -1;
471 static int hf_dnp3_start = -1;
472 static int hf_dnp3_len = -1;
473 static int hf_dnp3_ctl = -1;
474 static int hf_dnp3_ctl_prifunc = -1;
475 static int hf_dnp3_ctl_secfunc = -1;
476 static int hf_dnp3_ctl_dir = -1;
477 static int hf_dnp3_ctl_prm = -1;
478 static int hf_dnp3_ctl_fcb = -1;
479 static int hf_dnp3_ctl_fcv = -1;
480 static int hf_dnp3_ctl_dfc = -1;
481 static int hf_dnp3_dst = -1;
482 static int hf_dnp3_src = -1;
483 static int hf_dnp_hdr_CRC = -1;
484 static int hf_dnp_hdr_CRC_bad = -1;
485 static int hf_dnp3_tr_ctl = -1;
486 static int hf_dnp3_tr_fin = -1;
487 static int hf_dnp3_tr_fir = -1;
488 static int hf_dnp3_tr_seq = -1;
489 static int hf_dnp3_al_ctl = -1;
490 static int hf_dnp3_al_fir = -1;
491 static int hf_dnp3_al_fin = -1;
492 static int hf_dnp3_al_con = -1;
493 static int hf_dnp3_al_uns = -1;
494 static int hf_dnp3_al_seq = -1;
495 static int hf_dnp3_al_func = -1;
496 /* Added for Application Layer Decoding */
497 static int hf_dnp3_al_iin = -1;
498 static int hf_dnp3_al_iin_bmsg = -1;
499 static int hf_dnp3_al_iin_cls1d = -1;
500 static int hf_dnp3_al_iin_cls2d = -1;
501 static int hf_dnp3_al_iin_cls3d = -1;
502 static int hf_dnp3_al_iin_tsr = -1;
503 static int hf_dnp3_al_iin_dol = -1;
504 static int hf_dnp3_al_iin_dt = -1;
505 static int hf_dnp3_al_iin_rst = -1;
506 static int hf_dnp3_al_iin_obju = -1;
507 static int hf_dnp3_al_iin_pioor = -1;
508 static int hf_dnp3_al_iin_ebo = -1;
509 static int hf_dnp3_al_iin_oae = -1;
510 static int hf_dnp3_al_iin_cc = -1;
511 static int hf_dnp3_al_obj = -1;
512 static int hf_dnp3_al_objq_index = -1;
513 static int hf_dnp3_al_objq_code = -1;
514 static int hf_dnp3_al_range_start8 = -1;
515 static int hf_dnp3_al_range_stop8 = -1;
516 static int hf_dnp3_al_range_start16 = -1;
517 static int hf_dnp3_al_range_stop16 = -1;
518 static int hf_dnp3_al_range_start32 = -1;
519 static int hf_dnp3_al_range_stop32 = -1;
520 static int hf_dnp3_al_range_abs8 = -1;
521 static int hf_dnp3_al_range_abs16 = -1;
522 static int hf_dnp3_al_range_abs32 = -1;
523 static int hf_dnp3_al_range_quant8 = -1;
524 static int hf_dnp3_al_range_quant16 = -1;
525 static int hf_dnp3_al_range_quant32 = -1;
526 static int hf_dnp3_al_index8 = -1;
527 static int hf_dnp3_al_index16 = -1;
528 static int hf_dnp3_al_index32 = -1;
529
530 /*static int hf_dnp3_al_objq = -1;
531   static int hf_dnp3_al_nobj = -1; */
532 static int hf_dnp3_al_ptnum = -1;
533 static int hf_dnp3_al_biq_b0 = -1;
534 static int hf_dnp3_al_biq_b1 = -1;
535 static int hf_dnp3_al_biq_b2 = -1;
536 static int hf_dnp3_al_biq_b3 = -1;
537 static int hf_dnp3_al_biq_b4 = -1;
538 static int hf_dnp3_al_biq_b5 = -1;
539 static int hf_dnp3_al_biq_b6 = -1;
540 static int hf_dnp3_al_biq_b7 = -1;
541 static int hf_dnp3_al_boq_b0 = -1;
542 static int hf_dnp3_al_boq_b1 = -1;
543 static int hf_dnp3_al_boq_b2 = -1;
544 static int hf_dnp3_al_boq_b3 = -1;
545 static int hf_dnp3_al_boq_b4 = -1;
546 static int hf_dnp3_al_boq_b5 = -1;
547 static int hf_dnp3_al_boq_b6 = -1;
548 static int hf_dnp3_al_boq_b7 = -1;
549 static int hf_dnp3_al_ctrq_b0 = -1;
550 static int hf_dnp3_al_ctrq_b1 = -1;
551 static int hf_dnp3_al_ctrq_b2 = -1;
552 static int hf_dnp3_al_ctrq_b3 = -1;
553 static int hf_dnp3_al_ctrq_b4 = -1;
554 static int hf_dnp3_al_ctrq_b5 = -1;
555 static int hf_dnp3_al_ctrq_b6 = -1;
556 static int hf_dnp3_al_ctrq_b7 = -1;
557 static int hf_dnp3_al_aiq_b0 = -1;
558 static int hf_dnp3_al_aiq_b1 = -1;
559 static int hf_dnp3_al_aiq_b2 = -1;
560 static int hf_dnp3_al_aiq_b3 = -1;
561 static int hf_dnp3_al_aiq_b4 = -1;
562 static int hf_dnp3_al_aiq_b5 = -1;
563 static int hf_dnp3_al_aiq_b6 = -1;
564 static int hf_dnp3_al_aiq_b7 = -1;
565 static int hf_dnp3_al_aoq_b0 = -1;
566 static int hf_dnp3_al_aoq_b1 = -1;
567 static int hf_dnp3_al_aoq_b2 = -1;
568 static int hf_dnp3_al_aoq_b3 = -1;
569 static int hf_dnp3_al_aoq_b4 = -1;
570 static int hf_dnp3_al_aoq_b5 = -1;
571 static int hf_dnp3_al_aoq_b6 = -1;
572 static int hf_dnp3_al_aoq_b7 = -1;
573 static int hf_dnp3_al_timestamp = -1;
574 static int hf_dnp3_al_rel_timestamp = -1;
575 static int hf_dnp3_al_ana16 = -1;
576 static int hf_dnp3_al_ana32 = -1;
577 static int hf_dnp3_al_anaflt = -1;
578 static int hf_dnp3_al_anadbl = -1;
579 static int hf_dnp3_al_bit = -1;
580 static int hf_dnp3_al_2bit = -1;
581 static int hf_dnp3_al_cnt16 = -1;
582 static int hf_dnp3_al_cnt32 = -1;
583 static int hf_dnp3_al_ctrlstatus = -1;
584 static int hf_dnp3_al_anaout16 = -1;
585 static int hf_dnp3_al_anaout32 = -1;
586 static int hf_dnp3_al_anaoutflt = -1;
587 static int hf_dnp3_al_anaoutdbl = -1;
588
589 /***************************************************************************/
590 /* Value String Look-Ups */
591 /***************************************************************************/
592 static const value_string dnp3_ctl_func_pri_vals[] = {
593   { DL_FUNC_RESET_LINK, "Reset of Remote Link" },
594   { DL_FUNC_RESET_PROC, "Reset of User Process" },
595   { DL_FUNC_TEST_LINK,  "Test Function For Link" },
596   { DL_FUNC_USER_DATA,  "User Data" },
597   { DL_FUNC_UNC_DATA,   "Unconfirmed User Data" },
598   { DL_FUNC_LINK_STAT,  "Request Link Status" },
599   { 0, NULL }
600 };
601
602 static const value_string dnp3_ctl_func_sec_vals[] = {
603   { DL_FUNC_ACK,        "ACK" },
604   { DL_FUNC_NACK,       "NACK" },
605   { DL_FUNC_STAT_LINK,  "Status of Link" },
606   { DL_FUNC_NO_FUNC,    "Link Service Not Functioning" },
607   { DL_FUNC_NOT_IMPL,   "Link Service Not Used or Implemented" },
608   { 0,  NULL }
609 };
610
611 static const value_string dnp3_ctl_flags_pri_vals[] _U_ = {
612   { DNP3_CTL_DIR, "DIR" },
613   { DNP3_CTL_PRM, "PRM" },
614   { DNP3_CTL_FCB, "FCB" },
615   { DNP3_CTL_FCV, "FCV" },
616   { 0,  NULL }
617 };
618
619 static const value_string dnp3_ctl_flags_sec_vals[] _U_ = {
620   { DNP3_CTL_DIR, "DIR" },
621   { DNP3_CTL_PRM, "PRM" },
622   { DNP3_CTL_RES, "RES" },
623   { DNP3_CTL_DFC, "DFC" },
624   { 0,  NULL }
625 };
626
627 static const value_string dnp3_tr_flags_vals[] _U_ = {
628   { DNP3_TR_FIN,  "FIN" },
629   { DNP3_TR_FIR,  "FIR" },
630   { 0,  NULL }
631 };
632
633 static const value_string dnp3_al_flags_vals[] _U_ = {
634   { DNP3_AL_FIR,  "FIR" },
635   { DNP3_AL_FIN,  "FIN" },
636   { DNP3_AL_CON,  "CON" },
637   { 0,  NULL }
638 };
639
640 /* Application Layer Function Code Values */
641 static const value_string dnp3_al_func_vals[] = {
642   { AL_FUNC_CONFIRM,    "Confirm" },
643   { AL_FUNC_READ,       "Read" },
644   { AL_FUNC_WRITE,      "Write" },
645   { AL_FUNC_SELECT,     "Select" },
646   { AL_FUNC_OPERATE,    "Operate" },
647   { AL_FUNC_DIROP,      "Direct Operate" },
648   { AL_FUNC_DIROPNACK,  "Direct Operate No Ack" },
649   { AL_FUNC_FRZ,        "Immediate Freeze" },
650   { AL_FUNC_FRZNACK,    "Immediate Freeze No Ack" },
651   { AL_FUNC_FRZCLR,     "Freeze and Clear" },
652   { AL_FUNC_FRZCLRNACK, "Freeze and Clear No ACK" },
653   { AL_FUNC_FRZT,       "Freeze With Time" },
654   { AL_FUNC_FRZTNACK,   "Freeze With Time No ACK" },
655   { AL_FUNC_COLDRST,    "Cold Restart" },
656   { AL_FUNC_WARMRST,    "Warm Restart" },
657   { AL_FUNC_INITDATA,   "Initialize Data" },
658   { AL_FUNC_INITAPP,    "Initialize Application" },
659   { AL_FUNC_STARTAPP,   "Start Application" },
660   { AL_FUNC_STOPAPP,    "Stop Application" },
661   { AL_FUNC_SAVECFG,    "Save Configuration" },
662   { AL_FUNC_ENSPMSG,    "Enable Spontaneous Messages" },
663   { AL_FUNC_DISSPMSG,   "Disable Spontaneous Messages" },
664   { AL_FUNC_ASSIGNCL,   "Assign Classes" },
665   { AL_FUNC_DELAYMST,   "Delay Measurement" },
666   { AL_FUNC_RECCT,      "Record Current Time" },
667   { AL_FUNC_OPENFILE,   "Open File" },
668   { AL_FUNC_CLOSEFILE,  "Close File" },
669   { AL_FUNC_DELETEFILE, "Delete File" },
670   { AL_FUNC_GETFILEINF, "Get File Info" },
671   { AL_FUNC_AUTHFILE,   "Authenticate File" },
672   { AL_FUNC_ABORTFILE,  "Abort File" },
673   { AL_FUNC_ACTCNF,     "Activate Config" },
674   { AL_FUNC_AUTHREQ,    "Authentication Request" },
675   { AL_FUNC_AUTHERR,    "Authentication Error" },
676   { AL_FUNC_RESPON,     "Response" },
677   { AL_FUNC_UNSOLI,     "Unsolicited Response" },
678   { AL_FUNC_AUTHRESP,   "Authentication Response" },
679   { 0, NULL }
680 };
681
682 /* Application Layer Internal Indication (IIN) bit Values */
683 static const value_string dnp3_al_iin_vals[] _U_ = {
684   { AL_IIN_BMSG,    "Broadcast message Rx'd" },
685   { AL_IIN_CLS1D,   "Class 1 Data Available" },
686   { AL_IIN_CLS2D,   "Class 2 Data Available" },
687   { AL_IIN_CLS3D,   "Class 3 Data Available" },
688   { AL_IIN_TSR,     "Time Sync Required from Master" },
689   { AL_IIN_DOL,     "Digital Outputs in Local Mode" },
690   { AL_IIN_DT,      "Device Trouble" },
691   { AL_IIN_RST,     "Device Restart" },
692   { AL_IIN_OBJU,    "Requested Objects Unknown" },
693   { AL_IIN_PIOOR,   "Parameters Invalid or Out of Range" },
694   { AL_IIN_EBO,     "Event Buffer Overflow" },
695   { AL_IIN_OAE,     "Operation Already Executing" },
696   { AL_IIN_CC,      "Device Configuration Corrupt" },
697   { 0, NULL }
698 };
699
700 /* Application Layer Object Qualifier Index Values When Qualifier Code != 11 */
701 static const value_string dnp3_al_objq_index_vals[] = {
702   { AL_OBJQL_IDX_NI,    "None" },
703   { AL_OBJQL_IDX_1O,    "1-Octet Indexing" },
704   { AL_OBJQL_IDX_2O,    "2-Octet Indexing" },
705   { AL_OBJQL_IDX_4O,    "4-Octet Indexing" },
706   { AL_OBJQL_IDX_1OS,   "1-Octet Object Size" },
707   { AL_OBJQL_IDX_2OS,   "2-Octet Object Size" },
708   { AL_OBJQL_IDX_4OS,   "4-Octet Object Size" },
709   { 0, NULL }
710 };
711
712 /* Application Layer Object Qualifier Code Values */
713 static const value_string dnp3_al_objq_code_vals[] = {
714   { AL_OBJQL_CODE_SSI8,     "8-bit Start and Stop Indices" },
715   { AL_OBJQL_CODE_SSI16,    "16-bit Start and Stop Indices" },
716   { AL_OBJQL_CODE_SSI32,    "32-bit Start and Stop Indices" },
717   { AL_OBJQL_CODE_AA8,      "8-bit Absolute Address in Range Field" },
718   { AL_OBJQL_CODE_AA16,     "16-bit Absolute Address in Range Field" },
719   { AL_OBJQL_CODE_AA32,     "32-bit Absolute Address in Range Field" },
720   { AL_OBJQL_CODE_R0,       "No Range Field" },
721   { AL_OBJQL_CODE_SF8,      "8-bit Single Field Quantity" },
722   { AL_OBJQL_CODE_SF16,     "16-bit Single Field Quantity" },
723   { AL_OBJQL_CODE_SF32,     "32-bit Single Field Quantity" },
724   { AL_OBJQL_CODE_FF,       "Free-format Qualifier" },
725   { 0, NULL }
726 };
727
728 /* Application Layer Data Object Values */
729 static const value_string dnp3_al_obj_vals[] = {
730   { AL_OBJ_BI_ALL,     "Binary Input Default Variation (Obj:01, Var:Default)" },
731   { AL_OBJ_BI_1BIT,    "Single-Bit Binary Input (Obj:01, Var:01)" },
732   { AL_OBJ_BI_STAT,    "Binary Input With Status (Obj:01, Var:02)" },
733   { AL_OBJ_BIC_ALL,    "Binary Input Change Default Variation (Obj:02, Var:Default)" },
734   { AL_OBJ_BIC_NOTIME, "Binary Input Change Without Time (Obj:02, Var:01)" },
735   { AL_OBJ_BIC_TIME,   "Binary Input Change With Time (Obj:02, Var:02)" },
736   { AL_OBJ_BIC_RTIME,  "Binary Input Change With Relative Time (Obj:02, Var:03)" },
737   { AL_OBJ_2BI_ALL,    "Double-bit Input Default Variation (Obj:03, Var:Default)" },
738   { AL_OBJ_2BI_NF,     "Double-bit Input No Flags (Obj:03, Var:01)" },
739   { AL_OBJ_2BI_STAT,   "Double-bit Input With Status (Obj:03, Var:02)" },
740   { AL_OBJ_2BIC_NOTIME, "Double-bit Input Change Without Time (Obj:04, Var:01)" },
741   { AL_OBJ_2BIC_TIME,  "Double-bit Input Change With Time (Obj:04, Var:02)" },
742   { AL_OBJ_2BIC_RTIME, "Double-bit Input Change With Relative Time (Obj:04, Var:03)" },
743   { AL_OBJ_BO_ALL,     "Binary Output Default Variation (Obj:10, Var:Default)" },
744   { AL_OBJ_BO,         "Binary Output (Obj:10, Var:01)" },
745   { AL_OBJ_BO_STAT,    "Binary Output Status (Obj:10, Var:02)" },
746   { AL_OBJ_CTLOP_BLK,  "Control Relay Output Block (Obj:12, Var:01)" },
747   { AL_OBJ_CTR_ALL,    "Binary Counter Default Variation (Obj:20, Var:Default)" },
748   { AL_OBJ_CTR_32,     "32-Bit Binary Counter (Obj:20, Var:01)" },
749   { AL_OBJ_CTR_16,     "16-Bit Binary Counter (Obj:20, Var:02)" },
750   { AL_OBJ_DCTR_32,    "32-Bit Binary Delta Counter (Obj:20, Var:03)" },
751   { AL_OBJ_DCTR_16,    "16-Bit Binary Delta Counter (Obj:20, Var:04)" },
752   { AL_OBJ_CTR_32NF,   "32-Bit Binary Counter Without Flag (Obj:20, Var:05)" },
753   { AL_OBJ_CTR_16NF,   "16-Bit Binary Counter Without Flag (Obj:20, Var:06)" },
754   { AL_OBJ_DCTR_32NF,  "32-Bit Binary Delta Counter Without Flag (Obj:20, Var:07)" },
755   { AL_OBJ_DCTR_16NF,  "16-Bit Binary Delta Counter Without Flag (Obj:20, Var:08)" },
756   { AL_OBJ_FCTR_ALL,   "Frozen Binary Counter Default Variation (Obj:21, Var:Default)" },
757   { AL_OBJ_FCTR_32,    "32-Bit Frozen Binary Counter (Obj:21, Var:01)" },
758   { AL_OBJ_FCTR_16,    "16-Bit Frozen Binary Counter (Obj:21, Var:02)" },
759   { AL_OBJ_FDCTR_32,   "32-Bit Frozen Binary Delta Counter (Obj:21, Var:03)" },
760   { AL_OBJ_FDCTR_16,   "16-Bit Frozen Binary Delta Counter (Obj:21, Var:04)" },
761   { AL_OBJ_FCTR_32T,   "32-Bit Frozen Binary Counter (Obj:21, Var:01)" },
762   { AL_OBJ_FCTR_16T,   "16-Bit Frozen Binary Counter (Obj:21, Var:02)" },
763   { AL_OBJ_FDCTR_32T,  "32-Bit Frozen Binary Delta Counter (Obj:21, Var:03)" },
764   { AL_OBJ_FDCTR_16T,  "16-Bit Frozen Binary Delta Counter (Obj:21, Var:04)" },
765   { AL_OBJ_FCTR_32NF,  "32-Bit Frozen Binary Counter Without Flag (Obj:21, Var:05)" },
766   { AL_OBJ_FCTR_16NF,  "16-Bit Frozen Binary Counter Without Flag (Obj:21, Var:06)" },
767   { AL_OBJ_FDCTR_32NF, "32-Bit Frozen Binary Delta Counter Without Flag (Obj:21, Var:07)" },
768   { AL_OBJ_FDCTR_16NF, "16-Bit Frozen Binary Delta Counter Without Flag (Obj:21, Var:08)" },
769   { AL_OBJ_CTRC_ALL,   "Binary Counter Change Default Variation (Obj:22, Var:Default)" },
770   { AL_OBJ_CTRC_32,    "32-Bit Counter Change Event w/o Time (Obj:22, Var:01)" },
771   { AL_OBJ_CTRC_16,    "16-Bit Counter Change Event w/o Time (Obj:22, Var:02)" },
772   { AL_OBJ_DCTRC_32,   "32-Bit Delta Counter Change Event w/o Time (Obj:22, Var:03)" },
773   { AL_OBJ_DCTRC_16,   "16-Bit Delta Counter Change Event w/o Time (Obj:22, Var:04)" },
774   { AL_OBJ_CTRC_32T,   "32-Bit Counter Change Event with Time (Obj:22, Var:05)" },
775   { AL_OBJ_CTRC_16T,   "16-Bit Counter Change Event with Time (Obj:22, Var:06)" },
776   { AL_OBJ_DCTRC_32T,  "32-Bit Delta Counter Change Event with Time (Obj:22, Var:07)" },
777   { AL_OBJ_DCTRC_16T,  "16-Bit Delta Counter Change Event with Time (Obj:22, Var:08)" },
778   { AL_OBJ_FCTRC_ALL,  "Frozen Binary Counter Change Default Variation (Obj:23, Var:Default)" },
779   { AL_OBJ_FCTRC_32,   "32-Bit Frozen Counter Change Event w/o Time (Obj:23, Var:01)" },
780   { AL_OBJ_FCTRC_16,   "16-Bit Frozen Counter Change Event w/o Time (Obj:23, Var:02)" },
781   { AL_OBJ_FDCTRC_32,  "32-Bit Frozen Delta Counter Change Event w/o Time (Obj:23, Var:03)" },
782   { AL_OBJ_FDCTRC_16,  "16-Bit Frozen Delta Counter Change Event w/o Time (Obj:23, Var:04)" },
783   { AL_OBJ_FCTRC_32T,  "32-Bit Frozen Counter Change Event with Time (Obj:23, Var:05)" },
784   { AL_OBJ_FCTRC_16T,  "16-Bit Frozen Counter Change Event with Time (Obj:23, Var:06)" },
785   { AL_OBJ_FDCTRC_32T, "32-Bit Frozen Delta Counter Change Event with Time (Obj:23, Var:07)" },
786   { AL_OBJ_FDCTRC_16T, "16-Bit Frozen Delta Counter Change Event with Time (Obj:23, Var:08)" },
787   { AL_OBJ_AI_ALL,     "Analog Input Default Variation (Obj:30, Var:Default)" },
788   { AL_OBJ_AI_32,      "32-Bit Analog Input (Obj:30, Var:01)" },
789   { AL_OBJ_AI_16,      "16-Bit Analog Input (Obj:30, Var:02)" },
790   { AL_OBJ_AI_32NF,    "32-Bit Analog Input Without Flag (Obj:30, Var:03)" },
791   { AL_OBJ_AI_16NF,    "16-Bit Analog Input Without Flag (Obj:30, Var:04)" },
792   { AL_OBJ_AI_FLT,     "32-Bit Floating Point Input (Obj:30, Var:05)" },
793   { AL_OBJ_AI_DBL,     "64-Bit Floating Point Input (Obj:30, Var:06)" },
794   { AL_OBJ_AIF_FLT,    "32-Bit Frozen Floating Point Input (Obj:31, Var:07)" },
795   { AL_OBJ_AIF_DBL,    "64-Bit Frozen Floating Point Input (Obj:31, Var:08)" },
796   { AL_OBJ_AIC_ALL,    "Analog Input Change Default Variation (Obj:32, Var:Default)" },
797   { AL_OBJ_AIC_32NT,   "32-Bit Analog Change Event w/o Time (Obj:32, Var:01)" },
798   { AL_OBJ_AIC_16NT,   "16-Bit Analog Change Event w/o Time (Obj:32, Var:02)" },
799   { AL_OBJ_AIC_32T,    "32-Bit Analog Change Event with Time (Obj:32, Var:03)" },
800   { AL_OBJ_AIC_16T,    "16-Bit Analog Change Event with Time (Obj:32, Var:04)" },
801   { AL_OBJ_AIC_FLTNT,  "32-Bit Floating Point Change Event w/o Time (Obj:32, Var:05)" },
802   { AL_OBJ_AIC_DBLNT,  "64-Bit Floating Point Change Event w/o Time (Obj:32, Var:06)" },
803   { AL_OBJ_AIC_FLTT,   "32-Bit Floating Point Change Event w/ Time (Obj:32, Var:07)" },
804   { AL_OBJ_AIC_DBLT,   "64-Bit Floating Point Change Event w/ Time (Obj:32, Var:08)" },
805   { AL_OBJ_AIFC_FLTNT, "32-Bit Floating Point Frozen Change Event w/o Time (Obj:33, Var:05)" },
806   { AL_OBJ_AIFC_DBLNT, "64-Bit Floating Point Frozen Change Event w/o Time (Obj:33, Var:06)" },
807   { AL_OBJ_AIFC_FLTT,  "32-Bit Floating Point Frozen Change Event w/ Time (Obj:33, Var:07)" },
808   { AL_OBJ_AIFC_DBLT,  "64-Bit Floating Point Frozen Change Event w/ Time (Obj:33, Var:08)" },
809   { AL_OBJ_AO_ALL,     "Analog Output Default Variation (Obj:40, Var:Default)" },
810   { AL_OBJ_AO_32,      "32-Bit Analog Output Status (Obj:40, Var:01)" },
811   { AL_OBJ_AO_16,      "16-Bit Analog Output Status (Obj:40, Var:02)" },
812   { AL_OBJ_AO_FLT,     "32-Bit Floating Point Output Status (Obj:40, Var:03)" },
813   { AL_OBJ_AO_DBL,     "64-Bit Floating Point Output Status (Obj:40, Var:04)" },
814   { AL_OBJ_AO_32OPB,   "32-Bit Analog Output Block (Obj:41, Var:01)" },
815   { AL_OBJ_AO_16OPB,   "16-Bit Analog Output Block (Obj:41, Var:02)" },
816   { AL_OBJ_AO_FLTOPB,  "32-Bit Floating Point Output Block (Obj:41, Var:03)" },
817   { AL_OBJ_AO_DBLOPB,  "64-Bit Floating Point Output Block (Obj:41, Var:04)" },
818   { AL_OBJ_TD_ALL,     "Time and Date Default Variations (Obj:50, Var:Default)" },
819   { AL_OBJ_TD,         "Time and Date (Obj:50, Var:01)" },
820   { AL_OBJ_TDI,        "Time and Date w/Interval (Obj:50, Var:02)" },
821   { AL_OBJ_TDR,        "Last Recorded Time and Date (Obj:50, Var:03)" },
822   { AL_OBJ_TDCTO,      "Time and Date CTO (Obj:51, Var:01)" },
823   { AL_OBJ_TDELAYF,    "Time Delay - Fine (Obj:52, Var:02)" },
824   { AL_OBJ_CLASS0,     "Class 0 Data (Obj:60, Var:01)" },
825   { AL_OBJ_CLASS1,     "Class 1 Data (Obj:60, Var:02)" },
826   { AL_OBJ_CLASS2,     "Class 2 Data (Obj:60, Var:03)" },
827   { AL_OBJ_CLASS3,     "Class 3 Data (Obj:60, Var:04)" },
828   { AL_OBJ_IIN,        "Internal Indications (Obj:80, Var:01)" },
829   { AL_OBJ_OCT,        "Octet String (Obj:110)" },
830   { 0, NULL }
831 };
832
833 /* Application Layer Control Code 'Code' Values */
834 static const value_string dnp3_al_ctlc_code_vals[] = {
835   { AL_OBJCTLC_CODE0,     "NUL Operation" },
836   { AL_OBJCTLC_CODE1,     "Pulse On" },
837   { AL_OBJCTLC_CODE2,     "Pulse Off" },
838   { AL_OBJCTLC_CODE3,     "Latch On" },
839   { AL_OBJCTLC_CODE4,     "Latch Off" },
840   { 0, NULL }
841 };
842
843 /* Application Layer Control Code 'Misc' Values */
844 static const value_string dnp3_al_ctlc_misc_vals[] = {
845   { AL_OBJCTLC_QUEUE,     "Queue" },
846   { AL_OBJCTLC_CLEAR,     "Clear" },
847   { 0, NULL }
848 };
849
850 /* Application Layer Control Code 'Trip/Close' Values */
851 static const value_string dnp3_al_ctlc_tc_vals[] = {
852   { AL_OBJCTLC_TC0,     "NUL" },
853   { AL_OBJCTLC_TC1,     "Close" },
854   { AL_OBJCTLC_TC2,     "Trip" },
855   { 0, NULL }
856 };
857
858 /* Application Layer Control Status Values */
859 static const value_string dnp3_al_ctl_status_vals[] = {
860   { AL_OBJCTL_STAT0,     "Req. Accepted/Init/Queued" },
861   { AL_OBJCTL_STAT1,     "Req. Not Accepted; Arm-Timer Expired" },
862   { AL_OBJCTL_STAT2,     "Req. Not Accepted; No 'SELECT' Received" },
863   { AL_OBJCTL_STAT3,     "Req. Not Accepted; Format Err. in Ctl Req." },
864   { AL_OBJCTL_STAT4,     "Ctl Oper. Not Supported For This Point" },
865   { AL_OBJCTL_STAT5,     "Req. Not Accepted; Ctrl Queue Full/Point Active" },
866   { AL_OBJCTL_STAT6,     "Req. Not Accepted; Ctrl Hardware Problems" },
867   { AL_OBJCTL_STAT7,     "Req. Not Accepted; Local/Remote switch in Local" },
868   { AL_OBJCTL_STAT8,     "Req. Not Accepted; Too many operations" },
869   { AL_OBJCTL_STAT9,     "Req. Not Accepted; Insufficient authorization" },
870   { AL_OBJCTL_STAT10,    "Req. Not Accepted; Local automation proc active" },
871   { 0, NULL }
872 };
873
874 /* Application Layer Binary Input Quality Flag Values */
875 static const value_string dnp3_al_biflag_vals[] _U_ = {
876   { AL_OBJ_BI_FLAG0, "Online" },
877   { AL_OBJ_BI_FLAG1, "Restart" },
878   { AL_OBJ_BI_FLAG2, "Comm Fail" },
879   { AL_OBJ_BI_FLAG3, "Remote Forced" },
880   { AL_OBJ_BI_FLAG4, "Locally Forced" },
881   { AL_OBJ_BI_FLAG5, "Chatter Filter" },
882   { 0, NULL }
883 };
884
885 /* Application Layer Counter Quality Flag Values */
886 static const value_string dnp3_al_ctrflag_vals[] _U_ = {
887   { AL_OBJ_CTR_FLAG0, "Online" },
888   { AL_OBJ_CTR_FLAG1, "Restart" },
889   { AL_OBJ_CTR_FLAG2, "Comm Fail" },
890   { AL_OBJ_CTR_FLAG3, "Remote Forced" },
891   { AL_OBJ_CTR_FLAG4, "Locally Forced" },
892   { AL_OBJ_CTR_FLAG5, "Roll-Over" },
893   { AL_OBJ_CTR_FLAG6, "Discontinuity" },
894   { 0, NULL }
895 };
896
897 /* Application Layer Analog Input Quality Flag Values */
898 static const value_string dnp3_al_aiflag_vals[] _U_ = {
899   { AL_OBJ_AI_FLAG0, "Online" },
900   { AL_OBJ_AI_FLAG1, "Restart" },
901   { AL_OBJ_AI_FLAG2, "Comm Fail" },
902   { AL_OBJ_AI_FLAG3, "Remote Forced" },
903   { AL_OBJ_AI_FLAG4, "Locally Forced" },
904   { AL_OBJ_AI_FLAG5, "Over-Range" },
905   { AL_OBJ_AI_FLAG6, "Ref. Error" },
906   { 0, NULL }
907 };
908
909 /* Initialize the subtree pointers */
910 static gint ett_dnp3 = -1;
911 static gint ett_dnp3_dl = -1;
912 static gint ett_dnp3_dl_ctl = -1;
913 static gint ett_dnp3_tr_ctl = -1;
914 static gint ett_dnp3_al_data = -1;
915 static gint ett_dnp3_al = -1;
916 static gint ett_dnp3_al_ctl = -1;
917
918 /* Added for Application Layer Decoding */
919 static gint ett_dnp3_al_iin = -1;
920 static gint ett_dnp3_al_obj = -1;
921 static gint ett_dnp3_al_obj_qualifier = -1;
922 static gint ett_dnp3_al_obj_range = -1;
923 static gint ett_dnp3_al_objdet = -1;
924 static gint ett_dnp3_al_obj_quality = -1;
925 static gint ett_dnp3_al_obj_point = -1;
926
927 /* Tables for reassembly of fragments. */
928 static GHashTable *al_fragment_table = NULL;
929 static GHashTable *al_reassembled_table = NULL;
930
931 /* ************************************************************************* */
932 /*                   Header values for reassembly                            */
933 /* ************************************************************************* */
934 static int   hf_dnp3_fragment  = -1;
935 static int   hf_dnp3_fragments = -1;
936 static int   hf_dnp3_fragment_overlap = -1;
937 static int   hf_dnp3_fragment_overlap_conflict = -1;
938 static int   hf_dnp3_fragment_multiple_tails = -1;
939 static int   hf_dnp3_fragment_too_long_fragment = -1;
940 static int   hf_dnp3_fragment_error = -1;
941 static int   hf_dnp3_fragment_reassembled_in = -1;
942 static gint ett_dnp3_fragment  = -1;
943 static gint ett_dnp3_fragments = -1;
944
945 static const fragment_items dnp3_frag_items = {
946   &ett_dnp3_fragment,
947   &ett_dnp3_fragments,
948   &hf_dnp3_fragments,
949   &hf_dnp3_fragment,
950   &hf_dnp3_fragment_overlap,
951   &hf_dnp3_fragment_overlap_conflict,
952   &hf_dnp3_fragment_multiple_tails,
953   &hf_dnp3_fragment_too_long_fragment,
954   &hf_dnp3_fragment_error,
955   &hf_dnp3_fragment_reassembled_in,
956   "DNP 3.0 fragments"
957 };
958
959 /* Conversation stuff, used for tracking application message fragments */
960 /* the number of entries in the memory chunk array */
961 #define dnp3_conv_init_count 50
962
963 /* Conversation structure */
964 typedef struct {
965   guint conv_seq_number;
966 } dnp3_conv_t;
967
968 /* The conversation sequence number */
969 static guint seq_number = 0;
970
971 /* desegmentation of DNP3 over TCP */
972 static gboolean dnp3_desegment = TRUE;
973
974 /* Enum for different quality type fields */
975 enum QUALITY_TYPE {
976   BIN_IN,
977   BIN_OUT,
978   ANA_IN,
979   ANA_OUT,
980   COUNTER
981 };
982
983 /*****************************************************************/
984 /*                                                               */
985 /* CRC LOOKUP TABLE                                              */
986 /* ================                                              */
987 /* The following CRC lookup table was generated automagically    */
988 /* by the Rocksoft^tm Model CRC Algorithm Table Generation       */
989 /* Program V1.0 using the following model parameters:            */
990 /*                                                               */
991 /*    Width   : 2 bytes.                                         */
992 /*    Poly    : 0x3D65                                           */
993 /*    Reverse : TRUE.                                            */
994 /*                                                               */
995 /* For more information on the Rocksoft^tm Model CRC Algorithm,  */
996 /* see the document titled "A Painless Guide to CRC Error        */
997 /* Detection Algorithms" by Ross Williams                        */
998 /* (ross@guest.adelaide.edu.au.). This document is likely to be  */
999 /* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft".        */
1000 /*                                                               */
1001 /*****************************************************************/
1002
1003 static guint16 crctable[256] =
1004 {
1005  0x0000, 0x365E, 0x6CBC, 0x5AE2, 0xD978, 0xEF26, 0xB5C4, 0x839A,
1006  0xFF89, 0xC9D7, 0x9335, 0xA56B, 0x26F1, 0x10AF, 0x4A4D, 0x7C13,
1007  0xB26B, 0x8435, 0xDED7, 0xE889, 0x6B13, 0x5D4D, 0x07AF, 0x31F1,
1008  0x4DE2, 0x7BBC, 0x215E, 0x1700, 0x949A, 0xA2C4, 0xF826, 0xCE78,
1009  0x29AF, 0x1FF1, 0x4513, 0x734D, 0xF0D7, 0xC689, 0x9C6B, 0xAA35,
1010  0xD626, 0xE078, 0xBA9A, 0x8CC4, 0x0F5E, 0x3900, 0x63E2, 0x55BC,
1011  0x9BC4, 0xAD9A, 0xF778, 0xC126, 0x42BC, 0x74E2, 0x2E00, 0x185E,
1012  0x644D, 0x5213, 0x08F1, 0x3EAF, 0xBD35, 0x8B6B, 0xD189, 0xE7D7,
1013  0x535E, 0x6500, 0x3FE2, 0x09BC, 0x8A26, 0xBC78, 0xE69A, 0xD0C4,
1014  0xACD7, 0x9A89, 0xC06B, 0xF635, 0x75AF, 0x43F1, 0x1913, 0x2F4D,
1015  0xE135, 0xD76B, 0x8D89, 0xBBD7, 0x384D, 0x0E13, 0x54F1, 0x62AF,
1016  0x1EBC, 0x28E2, 0x7200, 0x445E, 0xC7C4, 0xF19A, 0xAB78, 0x9D26,
1017  0x7AF1, 0x4CAF, 0x164D, 0x2013, 0xA389, 0x95D7, 0xCF35, 0xF96B,
1018  0x8578, 0xB326, 0xE9C4, 0xDF9A, 0x5C00, 0x6A5E, 0x30BC, 0x06E2,
1019  0xC89A, 0xFEC4, 0xA426, 0x9278, 0x11E2, 0x27BC, 0x7D5E, 0x4B00,
1020  0x3713, 0x014D, 0x5BAF, 0x6DF1, 0xEE6B, 0xD835, 0x82D7, 0xB489,
1021  0xA6BC, 0x90E2, 0xCA00, 0xFC5E, 0x7FC4, 0x499A, 0x1378, 0x2526,
1022  0x5935, 0x6F6B, 0x3589, 0x03D7, 0x804D, 0xB613, 0xECF1, 0xDAAF,
1023  0x14D7, 0x2289, 0x786B, 0x4E35, 0xCDAF, 0xFBF1, 0xA113, 0x974D,
1024  0xEB5E, 0xDD00, 0x87E2, 0xB1BC, 0x3226, 0x0478, 0x5E9A, 0x68C4,
1025  0x8F13, 0xB94D, 0xE3AF, 0xD5F1, 0x566B, 0x6035, 0x3AD7, 0x0C89,
1026  0x709A, 0x46C4, 0x1C26, 0x2A78, 0xA9E2, 0x9FBC, 0xC55E, 0xF300,
1027  0x3D78, 0x0B26, 0x51C4, 0x679A, 0xE400, 0xD25E, 0x88BC, 0xBEE2,
1028  0xC2F1, 0xF4AF, 0xAE4D, 0x9813, 0x1B89, 0x2DD7, 0x7735, 0x416B,
1029  0xF5E2, 0xC3BC, 0x995E, 0xAF00, 0x2C9A, 0x1AC4, 0x4026, 0x7678,
1030  0x0A6B, 0x3C35, 0x66D7, 0x5089, 0xD313, 0xE54D, 0xBFAF, 0x89F1,
1031  0x4789, 0x71D7, 0x2B35, 0x1D6B, 0x9EF1, 0xA8AF, 0xF24D, 0xC413,
1032  0xB800, 0x8E5E, 0xD4BC, 0xE2E2, 0x6178, 0x5726, 0x0DC4, 0x3B9A,
1033  0xDC4D, 0xEA13, 0xB0F1, 0x86AF, 0x0535, 0x336B, 0x6989, 0x5FD7,
1034  0x23C4, 0x159A, 0x4F78, 0x7926, 0xFABC, 0xCCE2, 0x9600, 0xA05E,
1035  0x6E26, 0x5878, 0x029A, 0x34C4, 0xB75E, 0x8100, 0xDBE2, 0xEDBC,
1036  0x91AF, 0xA7F1, 0xFD13, 0xCB4D, 0x48D7, 0x7E89, 0x246B, 0x1235
1037 };
1038
1039 /*****************************************************************/
1040 /*                   End of CRC Lookup Table                     */
1041 /*****************************************************************/
1042
1043 /* calculates crc given a buffer of characters and a length of buffer */
1044 static guint16
1045 calculateCRC(const void *buf, guint len) {
1046   guint16 crc = 0;
1047   const guint8 *p = (const guint8 *)buf;
1048   while(len-- > 0)
1049     crc = crctable[(crc ^ *p++) & 0xff] ^ (crc >> 8);
1050   return ~crc;
1051 }
1052
1053 /*****************************************************************/
1054 /*  Adds text to item, with trailing "," if required             */
1055 /*****************************************************************/
1056 static gboolean
1057 add_item_text(proto_item *item, const gchar *text, gboolean comma_needed)
1058 {
1059   if (comma_needed) {
1060     proto_item_append_text(item, ", ");
1061   }
1062   proto_item_append_text(item, "%s", text);
1063   return TRUE;
1064 }
1065
1066 /*****************************************************************/
1067 /*  Application Layer Process Internal Indications (IIN)         */
1068 /*****************************************************************/
1069 static void
1070 dnp3_al_process_iin(tvbuff_t *tvb, int offset, proto_tree *al_tree)
1071 {
1072
1073   guint16       al_iin;
1074   proto_item    *tiin;
1075   proto_tree    *iin_tree = NULL;
1076   gboolean      comma_needed = FALSE;
1077
1078   al_iin = tvb_get_ntohs(tvb, offset);
1079
1080   tiin = proto_tree_add_uint_format(al_tree, hf_dnp3_al_iin, tvb, offset, 2, al_iin,
1081         "Internal Indications: ");
1082   if (al_iin & AL_IIN_RST)    comma_needed = add_item_text(tiin, "Device Restart", comma_needed);
1083   if (al_iin & AL_IIN_DOL)    comma_needed = add_item_text(tiin, "Digital Outputs in Local", comma_needed);
1084   if (al_iin & AL_IIN_DT)     comma_needed = add_item_text(tiin, "Device Trouble", comma_needed);
1085   if (al_iin & AL_IIN_TSR)    comma_needed = add_item_text(tiin, "Time Sync Required", comma_needed);
1086   if (al_iin & AL_IIN_CLS3D)  comma_needed = add_item_text(tiin, "Class 3 Data Available", comma_needed);
1087   if (al_iin & AL_IIN_CLS2D)  comma_needed = add_item_text(tiin, "Class 2 Data Available", comma_needed);
1088   if (al_iin & AL_IIN_CLS1D)  comma_needed = add_item_text(tiin, "Class 1 Data Available", comma_needed);
1089   if (al_iin & AL_IIN_BMSG)   comma_needed = add_item_text(tiin, "Broadcast Message Rx'd", comma_needed);
1090   if (al_iin & AL_IIN_CC)     comma_needed = add_item_text(tiin, "Device Configuration Corrupt", comma_needed);
1091   if (al_iin & AL_IIN_OAE)    comma_needed = add_item_text(tiin, "Operation Already Executing", comma_needed);
1092   if (al_iin & AL_IIN_EBO)    comma_needed = add_item_text(tiin, "Event Buffer Overflow", comma_needed);
1093   if (al_iin & AL_IIN_PIOOR)  comma_needed = add_item_text(tiin, "Parameters Invalid or Out of Range", comma_needed);
1094   if (al_iin & AL_IIN_OBJU)   comma_needed = add_item_text(tiin, "Requested Objects Unknown", comma_needed);
1095   proto_item_append_text(tiin, " (0x%04x)", al_iin);
1096
1097   iin_tree = proto_item_add_subtree(tiin, ett_dnp3_al_iin);
1098   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_rst, tvb, offset, 2, FALSE);
1099   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_dt, tvb, offset, 2, FALSE);
1100   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_dol, tvb, offset, 2, FALSE);
1101   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_tsr, tvb, offset, 2, FALSE);
1102   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_cls3d, tvb, offset, 2, FALSE);
1103   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_cls2d, tvb, offset, 2, FALSE);
1104   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_cls1d, tvb, offset, 2, FALSE);
1105   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_bmsg, tvb, offset, 2, FALSE);
1106   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_cc, tvb, offset, 2, FALSE);
1107   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_oae, tvb, offset, 2, FALSE);
1108   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_ebo, tvb, offset, 2, FALSE);
1109   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_pioor, tvb, offset, 2, FALSE);
1110   proto_tree_add_item(iin_tree, hf_dnp3_al_iin_obju, tvb, offset, 2, FALSE);
1111 }
1112
1113 /*****************************************************************/
1114 /* Function to determine Application Layer Object Index size and */
1115 /* Point address.                                                */
1116 /*****************************************************************/
1117 static int
1118 dnp3_al_obj_procindex(tvbuff_t *tvb, int offset, guint8 al_objq_index, guint32 *al_ptaddr, proto_tree *item_tree)
1119 {
1120   int indexbytes = 0;
1121   proto_item *index_item;
1122
1123   switch (al_objq_index)
1124   {
1125     case AL_OBJQL_IDX_NI:        /* No Index */
1126       indexbytes = 0;
1127       index_item = proto_tree_add_text(item_tree, tvb, offset, 0, "Point Index: %u", *al_ptaddr);
1128       PROTO_ITEM_SET_GENERATED(index_item);
1129       break;
1130     case AL_OBJQL_IDX_1O:
1131       *al_ptaddr = tvb_get_guint8(tvb, offset);
1132       proto_tree_add_item(item_tree, hf_dnp3_al_index8, tvb, offset, 1, TRUE);
1133       indexbytes = 1;
1134       break;
1135     case AL_OBJQL_IDX_2O:
1136       *al_ptaddr = tvb_get_letohs(tvb, offset);
1137       proto_tree_add_item(item_tree, hf_dnp3_al_index16, tvb, offset, 2, TRUE);
1138       indexbytes = 2;
1139       break;
1140     case AL_OBJQL_IDX_4O:
1141       *al_ptaddr = tvb_get_letohl(tvb, offset);
1142       proto_tree_add_item(item_tree, hf_dnp3_al_index32, tvb, offset, 4, TRUE);
1143       indexbytes = 4;
1144       break;
1145   }
1146   return indexbytes;
1147 }
1148
1149 /*****************************************************************/
1150 /* Function to add the same string to two separate tree items    */
1151 /*****************************************************************/
1152 static void
1153 dnp3_append_2item_text(proto_item *item1, proto_item *item2, const gchar *text)
1154 {
1155   proto_item_append_text(item1, "%s", text);
1156   proto_item_append_text(item2, "%s", text);
1157 }
1158
1159 /*****************************************************************/
1160 /* Function to Determine Application Layer Point Quality Flags & */
1161 /* add Point Quality Flag Sub-Tree                               */
1162 /*****************************************************************/
1163 static void
1164 dnp3_al_obj_quality(tvbuff_t *tvb, int offset, guint8 al_ptflags, proto_tree *point_tree, proto_item *point_item, enum QUALITY_TYPE type)
1165 {
1166
1167   proto_tree  *quality_tree = NULL;
1168   proto_item  *quality_item;
1169   int         hf0 = 0, hf1 = 0, hf2 = 0, hf3 = 0, hf4 = 0, hf5 = 0, hf6 = 0, hf7 = 0;
1170
1171   /* Common code */
1172   proto_item_append_text(point_item, " (Quality: ");
1173   quality_item = proto_tree_add_text(point_tree, tvb, offset, 1, "Quality: ");
1174   quality_tree = proto_item_add_subtree(quality_item, ett_dnp3_al_obj_quality);
1175
1176   if (al_ptflags & AL_OBJ_BI_FLAG0) {
1177     dnp3_append_2item_text(point_item, quality_item, "Online");
1178   }
1179   else {
1180     dnp3_append_2item_text(point_item, quality_item, "Offline");
1181   }
1182   if (al_ptflags & AL_OBJ_BI_FLAG1) dnp3_append_2item_text(point_item, quality_item, ", Restart");
1183   if (al_ptflags & AL_OBJ_BI_FLAG2) dnp3_append_2item_text(point_item, quality_item, ", Comm Fail");
1184   if (al_ptflags & AL_OBJ_BI_FLAG3) dnp3_append_2item_text(point_item, quality_item, ", Remote Force");
1185   if (al_ptflags & AL_OBJ_BI_FLAG4) dnp3_append_2item_text(point_item, quality_item, ", Local Force");
1186
1187   switch (type) {
1188     case BIN_IN: /* Binary Input Quality flags */
1189       if (al_ptflags & AL_OBJ_BI_FLAG5) dnp3_append_2item_text(point_item, quality_item, ", Chatter Filter");
1190
1191       hf0 = hf_dnp3_al_biq_b0;
1192       hf1 = hf_dnp3_al_biq_b1;
1193       hf2 = hf_dnp3_al_biq_b2;
1194       hf3 = hf_dnp3_al_biq_b3;
1195       hf4 = hf_dnp3_al_biq_b4;
1196       hf5 = hf_dnp3_al_biq_b5;
1197       hf6 = hf_dnp3_al_biq_b6;
1198       hf7 = hf_dnp3_al_biq_b7;
1199       break;
1200
1201     case BIN_OUT: /* Binary Output Quality flags */
1202       hf0 = hf_dnp3_al_boq_b0;
1203       hf1 = hf_dnp3_al_boq_b1;
1204       hf2 = hf_dnp3_al_boq_b2;
1205       hf3 = hf_dnp3_al_boq_b3;
1206       hf4 = hf_dnp3_al_boq_b4;
1207       hf5 = hf_dnp3_al_boq_b5;
1208       hf6 = hf_dnp3_al_boq_b6;
1209       hf7 = hf_dnp3_al_boq_b7;
1210       break;
1211
1212     case ANA_IN: /* Analog Input Quality flags */
1213       if (al_ptflags & AL_OBJ_AI_FLAG5) dnp3_append_2item_text(point_item, quality_item, ", Over-Range");
1214       if (al_ptflags & AL_OBJ_AI_FLAG6) dnp3_append_2item_text(point_item, quality_item, ", Reference Check");
1215
1216       hf0 = hf_dnp3_al_aiq_b0;
1217       hf1 = hf_dnp3_al_aiq_b1;
1218       hf2 = hf_dnp3_al_aiq_b2;
1219       hf3 = hf_dnp3_al_aiq_b3;
1220       hf4 = hf_dnp3_al_aiq_b4;
1221       hf5 = hf_dnp3_al_aiq_b5;
1222       hf6 = hf_dnp3_al_aiq_b6;
1223       hf7 = hf_dnp3_al_aiq_b7;
1224       break;
1225
1226     case ANA_OUT: /* Analog Output Quality flags */
1227       hf0 = hf_dnp3_al_aoq_b0;
1228       hf1 = hf_dnp3_al_aoq_b1;
1229       hf2 = hf_dnp3_al_aoq_b2;
1230       hf3 = hf_dnp3_al_aoq_b3;
1231       hf4 = hf_dnp3_al_aoq_b4;
1232       hf5 = hf_dnp3_al_aoq_b5;
1233       hf6 = hf_dnp3_al_aoq_b6;
1234       hf7 = hf_dnp3_al_aoq_b7;
1235       break;
1236
1237     case COUNTER: /* Counter Quality flags */
1238       if (al_ptflags & AL_OBJ_CTR_FLAG5) dnp3_append_2item_text(point_item, quality_item, ", Roll-over");
1239       if (al_ptflags & AL_OBJ_CTR_FLAG6) dnp3_append_2item_text(point_item, quality_item, ", Discontinuity");
1240
1241       hf0 = hf_dnp3_al_ctrq_b0;
1242       hf1 = hf_dnp3_al_ctrq_b1;
1243       hf2 = hf_dnp3_al_ctrq_b2;
1244       hf3 = hf_dnp3_al_ctrq_b3;
1245       hf4 = hf_dnp3_al_ctrq_b4;
1246       hf5 = hf_dnp3_al_ctrq_b5;
1247       hf6 = hf_dnp3_al_ctrq_b6;
1248       hf7 = hf_dnp3_al_ctrq_b7;
1249       break;
1250   }
1251
1252   if (quality_tree != NULL) {
1253     proto_tree_add_item(quality_tree, hf7, tvb, offset, 1, TRUE);
1254     proto_tree_add_item(quality_tree, hf6, tvb, offset, 1, TRUE);
1255     proto_tree_add_item(quality_tree, hf5, tvb, offset, 1, TRUE);
1256     proto_tree_add_item(quality_tree, hf4, tvb, offset, 1, TRUE);
1257     proto_tree_add_item(quality_tree, hf3, tvb, offset, 1, TRUE);
1258     proto_tree_add_item(quality_tree, hf2, tvb, offset, 1, TRUE);
1259     proto_tree_add_item(quality_tree, hf1, tvb, offset, 1, TRUE);
1260     proto_tree_add_item(quality_tree, hf0, tvb, offset, 1, TRUE);
1261   }
1262   proto_item_append_text(point_item, ")");
1263 }
1264
1265 /**********************************************************************/
1266 /* Function to convert DNP3 timestamp to nstime_t value               */
1267 /**********************************************************************/
1268 /* 48-bit Time Format                                                 */
1269 /* MSB      FF       EE       DD       CC       BB       AA      LSB  */
1270 /*       ffffffff eeeeeeee dddddddd cccccccc bbbbbbbb aaaaaaaa        */
1271 /*       47    40 39    32 31    24 23    16 15     8 7      0        */
1272 /*                                                                    */
1273 /* Value is ms since 00:00 on 1/1/1970                                */
1274 /**********************************************************************/
1275 static void
1276 dnp3_al_get_timestamp(nstime_t *timestamp, tvbuff_t *tvb, int data_pos)
1277 {
1278
1279   guint32    hi, lo;
1280   guint64    time_ms;
1281
1282   lo = tvb_get_letohs(tvb, data_pos);
1283   hi = tvb_get_letohl(tvb, data_pos + 2);
1284
1285   time_ms = (guint64)hi * 0x10000 + lo;
1286
1287   timestamp->secs = (long)(time_ms / 1000);
1288   timestamp->nsecs = (long)(time_ms % 1000) * 1000000;
1289 }
1290
1291 /*****************************************************************/
1292 /*  Desc:    Application Layer Process Object Details            */
1293 /*  Returns: New offset pointer into tvb                         */
1294 /*****************************************************************/
1295 static int
1296 dnp3_al_process_object(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_tree *robj_tree, gboolean header_only, guint16 *al_objtype)
1297 {
1298
1299   guint8        al_2bit, al_objq, al_objq_index, al_objq_code, al_ptflags, al_ctlobj_code, al_oct_len=0,
1300                 al_ctlobj_code_c, al_ctlobj_code_m, al_ctlobj_code_tc, al_ctlobj_count, al_bi_val, bitindex=0;
1301   guint16       al_obj, al_val16=0, al_ctlobj_stat, al_relms;
1302   guint32       al_val32, al_ptaddr=0, al_ctlobj_on, al_ctlobj_off;
1303   nstime_t      al_cto, al_reltime, al_abstime;
1304   gboolean      al_bit;
1305   guint         data_pos;
1306   gfloat        al_valflt;
1307   gdouble       al_valdbl;
1308   int           item_num, num_items=0;
1309   int           orig_offset, start_offset, rangebytes=0, indexbytes=0;
1310   proto_item    *object_item = NULL, *point_item = NULL, *qualifier_item = NULL, *range_item = NULL;
1311   proto_tree    *object_tree = NULL, *point_tree, *qualifier_tree, *range_tree;
1312   const gchar   *ctl_code_str, *ctl_misc_str, *ctl_tc_str, *ctl_status_str;
1313
1314   orig_offset = offset;
1315
1316   /* Application Layer Objects in this Message */
1317   *al_objtype =
1318   al_obj = tvb_get_ntohs(tvb, offset);
1319
1320   /* Special handling for Octet string objects as the variation is the length of the string */
1321   if ((al_obj & 0xFF00) == AL_OBJ_OCT) {
1322     al_oct_len = al_obj & 0xFF;
1323     al_obj = AL_OBJ_OCT;
1324   }
1325
1326   /* Create Data Objects Detail Tree */
1327   object_item = proto_tree_add_uint_format(robj_tree, hf_dnp3_al_obj, tvb, offset, 2, al_obj,
1328      "Object(s): %s (0x%04x)", val_to_str(al_obj, dnp3_al_obj_vals, "Unknown Object - Abort Decoding..."), al_obj);
1329   object_tree = proto_item_add_subtree(object_item, ett_dnp3_al_obj);
1330
1331   offset += 2;
1332
1333   /* Object Qualifier */
1334   al_objq = tvb_get_guint8(tvb, offset);
1335   al_objq_index = al_objq & AL_OBJQ_INDEX;
1336   al_objq_index = al_objq_index >> 4;
1337   al_objq_code = al_objq & AL_OBJQ_CODE;
1338
1339   qualifier_item = proto_tree_add_text(object_tree, tvb, offset, 1, "Qualifier Field, Prefix: %s, Code: %s",
1340     val_to_str(al_objq_index, dnp3_al_objq_index_vals, "Unknown Index Type"),
1341     val_to_str(al_objq_code, dnp3_al_objq_code_vals, "Unknown Code Type"));
1342   qualifier_tree = proto_item_add_subtree(qualifier_item, ett_dnp3_al_obj_qualifier);
1343   proto_tree_add_item(qualifier_tree, hf_dnp3_al_objq_index, tvb, offset, 1, FALSE);
1344   proto_tree_add_item(qualifier_tree, hf_dnp3_al_objq_code, tvb, offset, 1, FALSE);
1345
1346   offset += 1;
1347
1348   /* Create (possibly synthesized) number of items and range field tree */
1349   range_item = proto_tree_add_text(object_tree, tvb, offset, 0, "Number of Items: ");
1350   range_tree = proto_item_add_subtree(range_item, ett_dnp3_al_obj_range);
1351
1352   switch (al_objq_code)
1353   {
1354     case AL_OBJQL_CODE_SSI8:           /* 8-bit Start and Stop Indices in Range Field */
1355       num_items = ( tvb_get_guint8(tvb, offset+1) - tvb_get_guint8(tvb, offset) + 1);
1356       PROTO_ITEM_SET_GENERATED(range_item);
1357       al_ptaddr = tvb_get_guint8(tvb, offset);
1358       proto_tree_add_item(range_tree, hf_dnp3_al_range_start8, tvb, offset, 1, TRUE);
1359       proto_tree_add_item(range_tree, hf_dnp3_al_range_stop8, tvb, offset + 1, 1, TRUE);
1360       rangebytes = 2;
1361       break;
1362     case AL_OBJQL_CODE_SSI16:          /* 16-bit Start and Stop Indices in Range Field */
1363       num_items = ( tvb_get_letohs(tvb, offset+2) - tvb_get_letohs(tvb, (offset)) + 1);
1364       PROTO_ITEM_SET_GENERATED(range_item);
1365       al_ptaddr = tvb_get_letohs(tvb, offset);
1366       proto_tree_add_item(range_tree, hf_dnp3_al_range_start16, tvb, offset, 2, TRUE);
1367       proto_tree_add_item(range_tree, hf_dnp3_al_range_stop16, tvb, offset + 2, 2, TRUE);
1368       rangebytes = 4;
1369       break;
1370     case AL_OBJQL_CODE_SSI32:          /* 32-bit Start and Stop Indices in Range Field */
1371       num_items = ( tvb_get_letohl(tvb, offset+4) - tvb_get_letohl(tvb, offset) + 1);
1372       PROTO_ITEM_SET_GENERATED(range_item);
1373       al_ptaddr = tvb_get_letohl(tvb, offset);
1374       proto_tree_add_item(range_tree, hf_dnp3_al_range_start32, tvb, offset, 4, TRUE);
1375       proto_tree_add_item(range_tree, hf_dnp3_al_range_stop32, tvb, offset + 4, 4, TRUE);
1376       rangebytes = 8;
1377       break;
1378     case AL_OBJQL_CODE_AA8:            /* 8-bit Absolute Address in Range Field */
1379       num_items = 1;
1380       PROTO_ITEM_SET_GENERATED(range_item);
1381       al_ptaddr = tvb_get_guint8(tvb, offset);
1382       proto_tree_add_item(range_tree, hf_dnp3_al_range_abs8, tvb, offset, 1, TRUE);
1383       rangebytes = 1;
1384       break;
1385     case AL_OBJQL_CODE_AA16:           /* 16-bit Absolute Address in Range Field */
1386       num_items = 1;
1387       PROTO_ITEM_SET_GENERATED(range_item);
1388       al_ptaddr = tvb_get_letohs(tvb, offset);
1389       proto_tree_add_item(range_tree, hf_dnp3_al_range_abs16, tvb, offset, 2, TRUE);
1390       rangebytes = 2;
1391       break;
1392     case AL_OBJQL_CODE_AA32:           /* 32-bit Absolute Address in Range Field */
1393       num_items = 1;
1394       PROTO_ITEM_SET_GENERATED(range_item);
1395       al_ptaddr = tvb_get_letohl(tvb, offset);
1396       proto_tree_add_item(range_tree, hf_dnp3_al_range_abs32, tvb, offset, 4, TRUE);
1397       rangebytes = 4;
1398       break;
1399     case AL_OBJQL_CODE_SF8:            /* 8-bit Single Field Quantity in Range Field */
1400       num_items = tvb_get_guint8(tvb, offset);
1401       proto_tree_add_item(range_tree, hf_dnp3_al_range_quant8, tvb, offset, 1, TRUE);
1402       rangebytes = 1;
1403       proto_item_set_len(range_item, rangebytes);
1404       break;
1405     case AL_OBJQL_CODE_SF16:           /* 16-bit Single Field Quantity in Range Field */
1406       num_items = tvb_get_letohs(tvb, offset);
1407       proto_tree_add_item(range_tree, hf_dnp3_al_range_quant16, tvb, offset, 2, TRUE);
1408       rangebytes = 2;
1409       proto_item_set_len(range_item, rangebytes);
1410       break;
1411     case AL_OBJQL_CODE_SF32:           /* 32-bit Single Field Quantity in Range Field */
1412       num_items = tvb_get_letohl(tvb, offset);
1413       proto_tree_add_item(range_tree, hf_dnp3_al_range_quant32, tvb, offset, 4, TRUE);
1414       rangebytes = 4;
1415       proto_item_set_len(range_item, rangebytes);
1416       break;
1417   }
1418   if (num_items > 0) {
1419     proto_item_append_text(object_item, ", %d point%s", num_items, plurality(num_items, "", "s"));
1420   }
1421   proto_item_append_text(range_item, "%d", num_items);
1422
1423   if (num_items < 0) {
1424     proto_item_append_text(range_item, " (bogus)");
1425     expert_add_info_format(pinfo, range_item, PI_MALFORMED, PI_ERROR, "Negative number of items");
1426     return tvb_length(tvb);
1427   }
1428
1429
1430   offset += rangebytes;
1431
1432   bitindex = 0; /* Temp variable for cycling through points when object values are encoded into
1433             bits; primarily objects 0x0101, 0x0301 & 0x1001 */
1434
1435   /* Only process the point information for replies or items with point index lists */
1436   if (!header_only || al_objq_index > 0) {
1437     start_offset = offset;
1438     for (item_num = 0; item_num < num_items; item_num++)
1439     {
1440       /* Create Point item and Process Index */
1441       point_item = proto_tree_add_text(object_tree, tvb, offset, 0, "Point Number");
1442       point_tree = proto_item_add_subtree(point_item, ett_dnp3_al_obj_point);
1443
1444       data_pos = offset;
1445       indexbytes = dnp3_al_obj_procindex(tvb, offset, al_objq_index, &al_ptaddr, point_tree);
1446       proto_item_append_text(point_item, " %u", al_ptaddr);
1447       data_pos += indexbytes;
1448
1449       if (!header_only) {
1450         switch (al_obj)
1451         {
1452
1453           case AL_OBJ_BI_ALL:      /* Binary Input Default Variation (Obj:01, Var:Default) */
1454           case AL_OBJ_BIC_ALL:     /* Binary Input Change Default Variation (Obj:02, Var:Default) */
1455           case AL_OBJ_2BI_ALL:     /* Double-bit Input Default Variation (Obj:03, Var:Default) */
1456           case AL_OBJ_CTR_ALL:     /* Binary Counter Default Variation (Obj:20, Var:Default) */
1457           case AL_OBJ_CTRC_ALL:    /* Binary Counter Change Default Variation (Obj:22 Var:Default) */
1458           case AL_OBJ_AI_ALL:      /* Analog Input Default Variation (Obj:30, Var:Default) */
1459           case AL_OBJ_AIC_ALL:     /* Analog Input Change Default Variation (Obj:32 Var:Default) */
1460
1461             offset = data_pos;
1462             break;
1463
1464           case AL_OBJ_BI_1BIT:    /* Single-Bit Binary Input (Obj:01, Var:01) */
1465           case AL_OBJ_BO:         /* Binary Output (Obj:10, Var:01) */
1466
1467             /* Reset bit index if we've gone onto the next byte */
1468             if (bitindex > 7)
1469             {
1470               bitindex = 0;
1471               offset += (indexbytes + 1);
1472             }
1473
1474             /* Extract the bit from the packed byte */
1475             al_bi_val = tvb_get_guint8(tvb, offset);
1476             al_bit = (al_bi_val & (1 << bitindex)) > 0;
1477
1478             proto_item_append_text(point_item, ", Value: %u", al_bit);
1479             proto_tree_add_boolean(point_tree, hf_dnp3_al_bit, tvb, offset, 1, al_bit);
1480             proto_item_set_len(point_item, indexbytes + 1);
1481
1482             /* If we've read the last item, then move the offset past this byte */
1483             if (item_num == (num_items-1))
1484             {
1485               offset += (indexbytes + 1);
1486             }
1487
1488             break;
1489
1490           case AL_OBJ_2BI_NF:    /* Double-bit Input No Flags (Obj:03, Var:01) */
1491
1492             if (bitindex > 3)
1493             {
1494               bitindex = 0;
1495               offset += (indexbytes + 1);
1496             }
1497
1498             /* Extract the Double-bit from the packed byte */
1499             al_bi_val = tvb_get_guint8(tvb, offset);
1500             al_2bit = ((al_bi_val >> (bitindex << 1)) & 3);
1501
1502             proto_item_append_text(point_item, ", Value: %u", al_2bit);
1503             proto_tree_add_uint(point_tree, hf_dnp3_al_2bit, tvb, offset, 1, al_2bit);
1504             proto_item_set_len(point_item, indexbytes + 1);
1505
1506             /* If we've read the last item, then move the offset past this byte */
1507             if (item_num == (num_items-1))
1508             {
1509               offset += (indexbytes + 1);
1510             }
1511
1512             break;
1513
1514
1515           case AL_OBJ_BI_STAT:    /* Binary Input With Status (Obj:01, Var:02) */
1516           case AL_OBJ_BIC_NOTIME: /* Binary Input Change Without Time (Obj:02, Var:01) */
1517           case AL_OBJ_BO_STAT:    /* Binary Output Status (Obj:10, Var:02) */
1518
1519             /* Get Point Flags */
1520             al_ptflags = tvb_get_guint8(tvb, data_pos);
1521
1522             switch (al_obj) {
1523               case AL_OBJ_BI_STAT:
1524               case AL_OBJ_BIC_NOTIME:
1525                 dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, BIN_IN);
1526                 break;
1527               case AL_OBJ_BO_STAT:
1528                 dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, BIN_OUT);
1529                 break;
1530             }
1531             data_pos += 1;
1532
1533             al_bit = (al_ptflags & AL_OBJ_BI_FLAG7) > 0;
1534             proto_item_append_text(point_item, ", Value: %u", al_bit);
1535
1536             proto_item_set_len(point_item, data_pos - offset);
1537
1538             offset = data_pos;
1539             break;
1540
1541           case AL_OBJ_2BI_STAT:    /* Double-bit Input With Status (Obj:03, Var:02) */
1542           case AL_OBJ_2BIC_NOTIME: /* Double-bit Input Change Without Time (Obj:04, Var:01) */
1543
1544             /* Get Point Flags */
1545             al_ptflags = tvb_get_guint8(tvb, data_pos);
1546             dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, BIN_IN);
1547             data_pos += 1;
1548
1549             al_2bit = (al_ptflags >> 6) & 3;
1550             proto_item_append_text(point_item, ", Value: %u", al_2bit);
1551             proto_item_set_len(point_item, data_pos - offset);
1552
1553             offset = data_pos;
1554             break;
1555
1556           case AL_OBJ_BIC_TIME:   /* Binary Input Change w/ Time (Obj:02, Var:02)  */
1557
1558             /* Get Point Flags */
1559             al_ptflags = tvb_get_guint8(tvb, data_pos);
1560             dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, BIN_IN);
1561             data_pos += 1;
1562
1563
1564             /* Get timestamp */
1565             dnp3_al_get_timestamp(&al_abstime, tvb, data_pos);
1566             proto_tree_add_time(point_tree, hf_dnp3_al_timestamp, tvb, data_pos, 6, &al_abstime);
1567             data_pos += 6;
1568
1569             al_bit = (al_ptflags & AL_OBJ_BI_FLAG7) >> 7; /* bit shift 1xxxxxxx -> xxxxxxx1 */
1570             proto_item_append_text(point_item, ", Value: %u, Timestamp: %s", al_bit, abs_time_to_str(&al_abstime, FALSE));
1571             proto_item_set_len(point_item, data_pos - offset);
1572
1573             offset = data_pos;
1574             break;
1575
1576           case AL_OBJ_2BIC_TIME:   /* Double-bit Input Change w/ Time (Obj:04, Var:02)  */
1577
1578             /* Get Point Flags */
1579             al_ptflags = tvb_get_guint8(tvb, data_pos);
1580             dnp3_al_obj_quality(tvb, (offset+indexbytes), al_ptflags, point_tree, point_item, BIN_IN);
1581             data_pos += 1;
1582
1583
1584             /* Get timestamp */
1585             dnp3_al_get_timestamp(&al_abstime, tvb, data_pos);
1586             proto_tree_add_time(point_tree, hf_dnp3_al_timestamp, tvb, data_pos, 6, &al_abstime);
1587             data_pos += 6;
1588
1589             al_2bit = (al_ptflags >> 6) & 3; /* bit shift 11xxxxxx -> 00000011 */
1590             proto_item_append_text(point_item, ", Value: %u, Timestamp: %s", al_2bit, abs_time_to_str(&al_abstime, FALSE));
1591             proto_item_set_len(point_item, data_pos - offset);
1592
1593             offset = data_pos;
1594             break;
1595
1596           case AL_OBJ_BIC_RTIME:   /* Binary Input Change w/ Relative Time (Obj:02, Var:03)  */
1597
1598             /* Get Point Flags */
1599             al_ptflags = tvb_get_guint8(tvb, data_pos);
1600             dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, BIN_IN);
1601             data_pos += 1;
1602
1603             /* Get relative time, and convert to ns_time */
1604             al_relms = tvb_get_letohs(tvb, data_pos);
1605             al_reltime.secs = al_relms / 1000;
1606             al_reltime.nsecs = (al_relms % 1000) * 1000;
1607             /* Now add to CTO time */
1608             nstime_sum(&al_abstime, &al_cto, &al_reltime);
1609             proto_tree_add_time(point_tree, hf_dnp3_al_rel_timestamp, tvb, data_pos, 2, &al_reltime);
1610             data_pos += 2;
1611
1612             al_bit = (al_ptflags & AL_OBJ_BI_FLAG7) >> 7; /* bit shift 1xxxxxxx -> xxxxxxx1 */
1613             proto_item_append_text(point_item, ", Value: %u, Timestamp: %s", al_bit, abs_time_to_str(&al_abstime, FALSE));
1614             proto_item_set_len(point_item, data_pos - offset);
1615
1616             offset = data_pos;
1617             break;
1618
1619           case AL_OBJ_CTLOP_BLK:  /* Control Relay Output Block (Obj:12, Var:01) */
1620
1621             al_ctlobj_code = tvb_get_guint8(tvb, data_pos);
1622             data_pos += 1;
1623
1624             /* Bit-Mask xxxx1111 for Control Code 'Code' */
1625             al_ctlobj_code_c = al_ctlobj_code & AL_OBJCTLC_CODE;
1626             ctl_code_str = val_to_str(al_ctlobj_code_c, dnp3_al_ctlc_code_vals, "Ctrl Code Invalid (0x%02x)");
1627
1628             /* Bit-Mask xx11xxxx for Control Code Misc Values */
1629             al_ctlobj_code_m = al_ctlobj_code & AL_OBJCTLC_MISC;
1630             ctl_misc_str = val_to_str(al_ctlobj_code_m, dnp3_al_ctlc_misc_vals, "");
1631
1632             /* Bit-Mask 11xxxxxx for Control Code 'Trip/Close' */
1633             al_ctlobj_code_tc = al_ctlobj_code & AL_OBJCTLC_TC;
1634             ctl_tc_str = val_to_str(al_ctlobj_code_tc, dnp3_al_ctlc_tc_vals, "");
1635
1636             /* Get "Count" Field */
1637             al_ctlobj_count = tvb_get_guint8(tvb, data_pos);
1638             data_pos += 1;
1639
1640             /* Get "On Time" Field */
1641             al_ctlobj_on = tvb_get_letohl(tvb, data_pos);
1642             data_pos += 4;
1643
1644             /* Get "Off Time" Field */
1645             al_ctlobj_off = tvb_get_letohl(tvb, data_pos);
1646             data_pos += 4;
1647
1648             al_ctlobj_stat = tvb_get_guint8(tvb, data_pos);
1649             proto_tree_add_item(point_item, hf_dnp3_al_ctrlstatus, tvb, data_pos, 1, TRUE);
1650             ctl_status_str = val_to_str(al_ctlobj_stat, dnp3_al_ctl_status_vals, "Invalid Status (0x%02x)");
1651             data_pos += 1;
1652
1653             proto_item_append_text(point_item, ", Control Code: [%s,%s,%s (0x%02x)]",
1654                  ctl_code_str, ctl_misc_str, ctl_tc_str, al_ctlobj_code);
1655
1656             proto_tree_add_text(point_tree, tvb, data_pos - 11, 11,
1657                "  [Count: %u] [On-Time: %u] [Off-Time: %u] [Status: %s (0x%02x)]",
1658                    al_ctlobj_count, al_ctlobj_on, al_ctlobj_off, ctl_status_str, al_ctlobj_stat);
1659
1660             proto_item_set_len(point_item, data_pos - offset);
1661
1662             offset = data_pos;
1663             break;
1664
1665           case AL_OBJ_AO_32OPB:   /* 32-Bit Analog Output Block (Obj:41, Var:01) */
1666           case AL_OBJ_AO_16OPB:   /* 16-Bit Analog Output Block (Obj:41, Var:02) */
1667           case AL_OBJ_AO_FLTOPB:  /* 32-Bit Floating Point Output Block (Obj:41, Var:03) */
1668           case AL_OBJ_AO_DBLOPB:  /* 64-Bit Floating Point Output Block (Obj:41, Var:04) */
1669
1670             switch (al_obj)
1671             {
1672               case AL_OBJ_AO_32OPB:
1673                 al_val32 = tvb_get_letohl(tvb, data_pos);
1674                 proto_item_append_text(point_item, ", Value: %u", al_val32);
1675                 proto_tree_add_item(point_tree, hf_dnp3_al_anaout32, tvb, data_pos, 4, TRUE);
1676                 data_pos += 4;
1677                 break;
1678               case AL_OBJ_AO_16OPB:
1679                 al_val32 = tvb_get_letohs(tvb, data_pos);
1680                 proto_item_append_text(point_item, ", Value: %u", al_val32);
1681                 proto_tree_add_item(point_tree, hf_dnp3_al_anaout16, tvb, data_pos, 2, TRUE);
1682                 data_pos += 2;
1683                 break;
1684               case AL_OBJ_AO_FLTOPB:
1685                 al_valflt = tvb_get_letohieee_float(tvb, data_pos);
1686                 proto_item_append_text(point_item, ", Value: %g", al_valflt);
1687                 proto_tree_add_item(point_tree, hf_dnp3_al_anaoutflt, tvb, data_pos, 4, TRUE);
1688                 data_pos += 4;
1689                 break;
1690               case AL_OBJ_AO_DBLOPB:
1691                 al_valdbl = tvb_get_letohieee_double(tvb, data_pos);
1692                 proto_item_append_text(point_item, ", Value: %g", al_valdbl);
1693                 proto_tree_add_item(point_tree, hf_dnp3_al_anaoutdbl, tvb, data_pos, 8, TRUE);
1694                 data_pos += 8;
1695                 break;
1696             }
1697
1698             /* Get control status */
1699             al_ctlobj_stat = tvb_get_guint8(tvb, data_pos);
1700             ctl_status_str = val_to_str(al_ctlobj_stat, dnp3_al_ctl_status_vals, "Invalid Status (0x%02x)");
1701             proto_item_append_text(point_item, " [Status: %s (0x%02x)]", ctl_status_str, al_ctlobj_stat);
1702             proto_tree_add_item(point_tree, hf_dnp3_al_ctrlstatus, tvb, data_pos, 1, TRUE);
1703             data_pos += 1;
1704
1705             proto_item_set_len(point_item, data_pos - offset);
1706
1707             offset = data_pos;
1708             break;
1709
1710           case AL_OBJ_CTR_32:     /* 32-Bit Binary Counter (Obj:20, Var:01) */
1711           case AL_OBJ_CTR_16:     /* 16-Bit Binary Counter (Obj:20, Var:02) */
1712           case AL_OBJ_DCTR_32:    /* 32-Bit Binary Delta Counter (Obj:20, Var:03) */
1713           case AL_OBJ_DCTR_16:    /* 16-Bit Binary Delta Counter (Obj:20, Var:04) */
1714           case AL_OBJ_CTR_32NF:   /* 32-Bit Binary Counter Without Flag (Obj:20, Var:05) */
1715           case AL_OBJ_CTR_16NF:   /* 16-Bit Binary Counter Without Flag (Obj:20, Var:06) */
1716           case AL_OBJ_DCTR_32NF:  /* 32-Bit Binary Delta Counter Without Flag (Obj:20, Var:07) */
1717           case AL_OBJ_DCTR_16NF:  /* 16-Bit Binary Delta Counter Without Flag (Obj:20, Var:08) */
1718           case AL_OBJ_FCTR_32:    /* 32-Bit Frozen Counter (Obj:21, Var:01) */
1719           case AL_OBJ_FCTR_16:    /* 16-Bit Frozen Counter (Obj:21, Var:02) */
1720           case AL_OBJ_FDCTR_32:   /* 21 03 32-Bit Frozen Delta Counter */
1721           case AL_OBJ_FDCTR_16:   /* 21 04 16-Bit Frozen Delta Counter */
1722           case AL_OBJ_FCTR_32T:   /* 21 05 32-Bit Frozen Counter w/ Time of Freeze */
1723           case AL_OBJ_FCTR_16T:   /* 21 06 16-Bit Frozen Counter w/ Time of Freeze */
1724           case AL_OBJ_FDCTR_32T:  /* 21 07 32-Bit Frozen Delta Counter w/ Time of Freeze */
1725           case AL_OBJ_FDCTR_16T:  /* 21 08 16-Bit Frozen Delta Counter w/ Time of Freeze */
1726           case AL_OBJ_FCTR_32NF:  /* 21 09 32-Bit Frozen Counter Without Flag */
1727           case AL_OBJ_FCTR_16NF:  /* 21 10 16-Bit Frozen Counter Without Flag */
1728           case AL_OBJ_FDCTR_32NF: /* 21 11 32-Bit Frozen Delta Counter Without Flag */
1729           case AL_OBJ_FDCTR_16NF: /* 21 12 16-Bit Frozen Delta Counter Without Flag */
1730           case AL_OBJ_CTRC_32:    /* 32-Bit Counter Change Event w/o Time (Obj:22, Var:01) */
1731           case AL_OBJ_CTRC_16:    /* 16-Bit Counter Change Event w/o Time (Obj:22, Var:02) */
1732           case AL_OBJ_DCTRC_32:   /* 32-Bit Delta Counter Change Event w/o Time (Obj:22, Var:03) */
1733           case AL_OBJ_DCTRC_16:   /* 16-Bit Delta Counter Change Event w/o Time (Obj:22, Var:04) */
1734           case AL_OBJ_CTRC_32T:   /* 32-Bit Counter Change Event with Time (Obj:22, Var:05) */
1735           case AL_OBJ_CTRC_16T:   /* 16-Bit Counter Change Event with Time (Obj:22, Var:06) */
1736           case AL_OBJ_DCTRC_32T:  /* 32-Bit Delta Counter Change Event with Time (Obj:22, Var:07) */
1737           case AL_OBJ_DCTRC_16T:  /* 16-Bit Delta Counter Change Event with Time (Obj:22, Var:08) */
1738           case AL_OBJ_FCTRC_32:   /* 21 01 32-Bit Frozen Counter Change Event */
1739           case AL_OBJ_FCTRC_16:   /* 21 02 16-Bit Frozen Counter Change Event */
1740           case AL_OBJ_FDCTRC_32:  /* 21 03 32-Bit Frozen Delta Counter Change Event */
1741           case AL_OBJ_FDCTRC_16:  /* 21 04 16-Bit Frozen Delta Counter Change Event */
1742           case AL_OBJ_FCTRC_32T:  /* 21 05 32-Bit Frozen Counter Change Event w/ Time of Freeze */
1743           case AL_OBJ_FCTRC_16T:  /* 21 06 16-Bit Frozen Counter Change Event w/ Time of Freeze */
1744           case AL_OBJ_FDCTRC_32T: /* 21 07 32-Bit Frozen Delta Counter Change Event w/ Time of Freeze */
1745           case AL_OBJ_FDCTRC_16T: /* 21 08 16-Bit Frozen Delta Counter Change Event w/ Time of Freeze */
1746
1747             /* Get Point Flags for those types that have them, it's easier to block out those that don't have flags */
1748             switch (al_obj)
1749             {
1750               case AL_OBJ_CTR_32NF:
1751               case AL_OBJ_CTR_16NF:
1752               case AL_OBJ_DCTR_32NF:
1753               case AL_OBJ_DCTR_16NF:
1754               case AL_OBJ_FCTR_32NF:
1755               case AL_OBJ_FCTR_16NF:
1756               case AL_OBJ_FDCTR_32NF:
1757               case AL_OBJ_FDCTR_16NF:
1758                 break;
1759
1760               default:
1761                 al_ptflags = tvb_get_guint8(tvb, data_pos);
1762                 dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, COUNTER);
1763                 data_pos += 1;
1764                 break;
1765             }
1766
1767             /* Get Counter values */
1768             switch (al_obj)
1769             {
1770               case AL_OBJ_CTR_32:
1771               case AL_OBJ_DCTR_32:
1772               case AL_OBJ_CTR_32NF:
1773               case AL_OBJ_DCTR_32NF:
1774               case AL_OBJ_FCTR_32:
1775               case AL_OBJ_FDCTR_32:
1776               case AL_OBJ_FCTR_32T:
1777               case AL_OBJ_FDCTR_32T:
1778               case AL_OBJ_FCTR_32NF:
1779               case AL_OBJ_FDCTR_32NF:
1780               case AL_OBJ_CTRC_32:
1781               case AL_OBJ_DCTRC_32:
1782               case AL_OBJ_CTRC_32T:
1783               case AL_OBJ_DCTRC_32T:
1784               case AL_OBJ_FCTRC_32:
1785               case AL_OBJ_FDCTRC_32:
1786               case AL_OBJ_FCTRC_32T:
1787               case AL_OBJ_FDCTRC_32T:
1788
1789                 al_val32 = tvb_get_letohl(tvb, data_pos);
1790                 proto_item_append_text(point_item, ", Count: %u", al_val32);
1791                 proto_tree_add_item(point_tree, hf_dnp3_al_cnt32, tvb, data_pos, 4, TRUE);
1792                 data_pos += 4;
1793                 break;
1794
1795               case AL_OBJ_CTR_16:
1796               case AL_OBJ_DCTR_16:
1797               case AL_OBJ_CTR_16NF:
1798               case AL_OBJ_DCTR_16NF:
1799               case AL_OBJ_FCTR_16:
1800               case AL_OBJ_FDCTR_16:
1801               case AL_OBJ_FCTR_16T:
1802               case AL_OBJ_FDCTR_16T:
1803               case AL_OBJ_FCTR_16NF:
1804               case AL_OBJ_FDCTR_16NF:
1805               case AL_OBJ_CTRC_16:
1806               case AL_OBJ_DCTRC_16:
1807               case AL_OBJ_CTRC_16T:
1808               case AL_OBJ_DCTRC_16T:
1809               case AL_OBJ_FCTRC_16:
1810               case AL_OBJ_FDCTRC_16:
1811               case AL_OBJ_FCTRC_16T:
1812               case AL_OBJ_FDCTRC_16T:
1813
1814                 al_val16 = tvb_get_letohs(tvb, data_pos);
1815                 proto_item_append_text(point_item, ", Count: %u", al_val16);
1816                 proto_tree_add_item(point_tree, hf_dnp3_al_cnt16, tvb, data_pos, 2, TRUE);
1817                 data_pos += 2;
1818                 break;
1819             }
1820
1821             /* Get the time for those points that have it */
1822             switch (al_obj)
1823             {
1824               case AL_OBJ_FCTR_32T:
1825               case AL_OBJ_FCTR_16T:
1826               case AL_OBJ_FDCTR_32T:
1827               case AL_OBJ_FDCTR_16T:
1828               case AL_OBJ_CTRC_32T:
1829               case AL_OBJ_CTRC_16T:
1830               case AL_OBJ_DCTRC_32T:
1831               case AL_OBJ_DCTRC_16T:
1832               case AL_OBJ_FCTRC_32T:
1833               case AL_OBJ_FCTRC_16T:
1834               case AL_OBJ_FDCTRC_32T:
1835               case AL_OBJ_FDCTRC_16T:
1836                 dnp3_al_get_timestamp(&al_abstime, tvb, data_pos);
1837                 proto_item_append_text(point_item, ", Timestamp: %s", abs_time_to_str(&al_abstime, FALSE));
1838                 proto_tree_add_time(point_tree, hf_dnp3_al_timestamp, tvb, data_pos, 6, &al_abstime);
1839                 data_pos += 6;
1840                 break;
1841             }
1842
1843             proto_item_set_len(point_item, data_pos - offset);
1844             offset = data_pos;
1845
1846             break;
1847
1848           case AL_OBJ_AI_32:        /* 32-Bit Analog Input (Obj:30, Var:01) */
1849           case AL_OBJ_AI_16:        /* 16-Bit Analog Input (Obj:30, Var:02) */
1850           case AL_OBJ_AI_32NF:      /* 32-Bit Analog Input Without Flag (Obj:30, Var:03) */
1851           case AL_OBJ_AI_16NF:      /* 16-Bit Analog Input Without Flag (Obj:30, Var:04) */
1852           case AL_OBJ_AI_FLT:       /* 32-Bit Floating Point Input (Obj:30, Var:05) */
1853           case AL_OBJ_AI_DBL:       /* 64-Bit Floating Point Input (Obj:30, Var:06) */
1854           case AL_OBJ_AIF_FLT:      /* 32-Bit Frozen Floating Point Input (Obj:31, Var:07) */
1855           case AL_OBJ_AIF_DBL:      /* 64-Bit Frozen Floating Point Input (Obj:31, Var:08) */
1856           case AL_OBJ_AIC_32NT:     /* 32-Bit Analog Change Event w/o Time (Obj:32, Var:01) */
1857           case AL_OBJ_AIC_16NT:     /* 16-Bit Analog Change Event w/o Time (Obj:32, Var:02) */
1858           case AL_OBJ_AIC_32T:      /* 32-Bit Analog Change Event with Time (Obj:32, Var:03) */
1859           case AL_OBJ_AIC_16T:      /* 16-Bit Analog Change Event with Time (Obj:32, Var:04) */
1860           case AL_OBJ_AIC_FLTNT:    /* 32-Bit Floating Point Change Event w/o Time (Obj:32, Var:05) */
1861           case AL_OBJ_AIC_DBLNT:    /* 64-Bit Floating Point Change Event w/o Time (Obj:32, Var:06) */
1862           case AL_OBJ_AIC_FLTT:     /* 32-Bit Floating Point Change Event w/ Time (Obj:32, Var:07) */
1863           case AL_OBJ_AIC_DBLT:     /* 64-Bit Floating Point Change Event w/ Time (Obj:32, Var:08) */
1864           case AL_OBJ_AIFC_FLTNT:   /* 32-Bit Floating Point Frozen Change Event w/o Time (Obj:33, Var:05) */
1865           case AL_OBJ_AIFC_DBLNT:   /* 64-Bit Floating Point Frozen Change Event w/o Time (Obj:33, Var:06) */
1866           case AL_OBJ_AIFC_FLTT:    /* 32-Bit Floating Point Frozen Change Event w/ Time (Obj:33, Var:07) */
1867           case AL_OBJ_AIFC_DBLT:    /* 64-Bit Floating Point Frozen Change Event w/ Time (Obj:33, Var:08) */
1868
1869             /* Get Point Flags for those types that have them */
1870             switch (al_obj)
1871             {
1872               case AL_OBJ_AI_32NF:
1873               case AL_OBJ_AI_16NF:
1874                 break;
1875
1876               default:
1877                 al_ptflags = tvb_get_guint8(tvb, data_pos);
1878                 dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, ANA_IN);
1879                 data_pos += 1;
1880                 break;
1881             }
1882
1883             switch (al_obj)
1884             {
1885               case AL_OBJ_AI_32:
1886               case AL_OBJ_AI_32NF:
1887               case AL_OBJ_AIC_32NT:
1888               case AL_OBJ_AIC_32T:
1889
1890                 al_val32 = tvb_get_letohl(tvb, data_pos);
1891                 proto_item_append_text(point_item, ", Value: %u", al_val32);
1892                 proto_tree_add_item(point_tree, hf_dnp3_al_ana32, tvb, data_pos, 4, TRUE);
1893                 data_pos += 4;
1894                 break;
1895
1896               case AL_OBJ_AI_16:
1897               case AL_OBJ_AI_16NF:
1898               case AL_OBJ_AIC_16NT:
1899               case AL_OBJ_AIC_16T:
1900
1901                 al_val16 = tvb_get_letohs(tvb, data_pos);
1902                 proto_item_append_text(point_item, ", Value: %u", al_val16);
1903                 proto_tree_add_item(point_tree, hf_dnp3_al_ana16, tvb, data_pos, 2, TRUE);
1904                 data_pos += 2;
1905                 break;
1906
1907               case AL_OBJ_AI_FLT:
1908               case AL_OBJ_AIF_FLT:
1909               case AL_OBJ_AIC_FLTNT:
1910               case AL_OBJ_AIC_FLTT:
1911               case AL_OBJ_AIFC_FLTNT:
1912               case AL_OBJ_AIFC_FLTT:
1913
1914                 al_valflt = tvb_get_letohieee_float(tvb, data_pos);
1915                 proto_item_append_text(point_item, ", Value: %g", al_valflt);
1916                 proto_tree_add_item(point_tree, hf_dnp3_al_anaflt, tvb, data_pos, 4, TRUE);
1917                 data_pos += 4;
1918                 break;
1919
1920               case AL_OBJ_AI_DBL:
1921               case AL_OBJ_AIF_DBL:
1922               case AL_OBJ_AIC_DBLNT:
1923               case AL_OBJ_AIC_DBLT:
1924               case AL_OBJ_AIFC_DBLNT:
1925               case AL_OBJ_AIFC_DBLT:
1926
1927                 al_valdbl = tvb_get_letohieee_double(tvb, data_pos);
1928                 proto_item_append_text(point_item, ", Value: %g", al_valdbl);
1929                 proto_tree_add_item(point_tree, hf_dnp3_al_anadbl, tvb, data_pos, 8, TRUE);
1930                 data_pos += 8;
1931                 break;
1932             }
1933
1934             /* Get timestamp */
1935             switch (al_obj)
1936             {
1937               case AL_OBJ_AIC_32T:
1938               case AL_OBJ_AIC_16T:
1939               case AL_OBJ_AIC_FLTT:
1940               case AL_OBJ_AIC_DBLT:
1941               case AL_OBJ_AIFC_FLTT:
1942               case AL_OBJ_AIFC_DBLT:
1943                 dnp3_al_get_timestamp(&al_abstime, tvb, data_pos);
1944                 proto_item_append_text(point_item, ", Timestamp: %s", abs_time_to_str(&al_abstime, FALSE));
1945                 proto_tree_add_time(point_tree, hf_dnp3_al_timestamp, tvb, data_pos, 6, &al_abstime);
1946                 data_pos += 6;
1947                 break;
1948             }
1949
1950             proto_item_set_len(point_item, data_pos - offset);
1951
1952             offset = data_pos;
1953             break;
1954
1955           case AL_OBJ_AO_32:     /* 32-Bit Analog Output Status (Obj:40, Var:01) */
1956           case AL_OBJ_AO_16:     /* 16-Bit Analog Output Status (Obj:40, Var:02) */
1957           case AL_OBJ_AO_FLT:    /* 32-Bit Floating Point Output Status (Obj:40, Var:03) */
1958           case AL_OBJ_AO_DBL:    /* 64-Bit Floating Point Output Status (Obj:40, Var:04) */
1959
1960             /* Get Point Flags */
1961             al_ptflags = tvb_get_guint8(tvb, data_pos);
1962             dnp3_al_obj_quality(tvb, data_pos, al_ptflags, point_tree, point_item, ANA_OUT);
1963             data_pos += 1;
1964
1965             switch (al_obj)
1966             {
1967               case AL_OBJ_AO_32:     /* 32-Bit Analog Output Status (Obj:40, Var:01) */
1968
1969                 al_val32 = tvb_get_letohl(tvb, data_pos);
1970                 proto_item_append_text(point_item, ", Value: %u", al_val32);
1971                 proto_tree_add_item(point_tree, hf_dnp3_al_anaout32, tvb, data_pos, 4, TRUE);
1972                 data_pos += 4;
1973                 break;
1974
1975               case AL_OBJ_AO_16:     /* 16-Bit Analog Output Status (Obj:40, Var:02) */
1976
1977                 al_val16 = tvb_get_letohs(tvb, data_pos);
1978                 proto_item_append_text(point_item, ", Value: %u", al_val16);
1979                 proto_tree_add_item(point_tree, hf_dnp3_al_anaout16, tvb, data_pos, 2, TRUE);
1980                 data_pos += 2;
1981                 break;
1982
1983               case AL_OBJ_AO_FLT:     /* 32-Bit Floating Point Output Status (Obj:40, Var:03) */
1984
1985                 al_valflt = tvb_get_letohieee_float(tvb, data_pos);
1986                 proto_item_append_text(point_item, ", Value: %g", al_valflt);
1987                 proto_tree_add_item(point_tree, hf_dnp3_al_anaoutflt, tvb, data_pos, 4, TRUE);
1988                 data_pos += 4;
1989                 break;
1990
1991               case AL_OBJ_AO_DBL:     /* 64-Bit Floating Point Output Status (Obj:40, Var:04) */
1992
1993                 al_valdbl = tvb_get_letohieee_double(tvb, data_pos);
1994                 proto_item_append_text(point_item, ", Value: %g", al_valdbl);
1995                 proto_tree_add_item(point_tree, hf_dnp3_al_anaoutdbl, tvb, data_pos, 8, TRUE);
1996                 data_pos += 8;
1997                 break;
1998             }
1999
2000             proto_item_set_len(point_item, data_pos - offset);
2001             offset = data_pos;
2002
2003             break;
2004
2005           case AL_OBJ_TD:    /* Time and Date (Obj:50, Var:01) */
2006           case AL_OBJ_TDCTO: /* Time and Date CTO (Obj:51, Var:01) */
2007
2008             dnp3_al_get_timestamp(&al_cto, tvb, data_pos);
2009             proto_tree_add_time(object_tree, hf_dnp3_al_timestamp, tvb, data_pos, 6, &al_cto);
2010             data_pos += 6;
2011             proto_item_set_len(point_item, data_pos - offset);
2012
2013             offset = data_pos;
2014             break;
2015
2016           case AL_OBJ_TDELAYF: /* Time Delay - Fine (Obj:52, Var:02) */
2017
2018             al_val16 = tvb_get_letohs(tvb, data_pos);
2019             proto_tree_add_text(object_tree, tvb, data_pos, 2, "Time Delay: %u ms", al_val16);
2020             data_pos += 2;
2021             proto_item_set_len(point_item, data_pos - offset);
2022
2023             offset = data_pos;
2024             break;
2025
2026           case AL_OBJ_CLASS0:  /* Class Data Objects */
2027           case AL_OBJ_CLASS1:
2028           case AL_OBJ_CLASS2:
2029           case AL_OBJ_CLASS3:
2030
2031             /* No data here */
2032             offset = data_pos;
2033             break;
2034
2035           case AL_OBJ_IIN:     /* IIN Data Object */
2036
2037             /* Single byte of data here */
2038             proto_tree_add_text(object_tree, tvb, data_pos, 1, "Value: %u", tvb_get_guint8(tvb, data_pos));
2039             data_pos += 1;
2040             proto_item_set_len(point_item, data_pos - offset);
2041
2042             offset = data_pos;
2043             break;
2044
2045           case AL_OBJ_OCT:    /* Octet string */
2046
2047             /* read the number of bytes defined by the variation */
2048             if (al_oct_len > 0) {
2049               proto_tree_add_text(object_tree, tvb, data_pos, al_oct_len, "Octet String (%u bytes)", al_oct_len);
2050               data_pos += al_oct_len;
2051               proto_item_set_len(point_item, data_pos - offset);
2052             }
2053
2054             offset = data_pos;
2055             break;
2056
2057           default:             /* In case of unknown object */
2058
2059             proto_tree_add_text(object_tree, tvb, offset, tvb_reported_length_remaining(tvb, offset),
2060               "Unknown Data Chunk, %u Bytes", tvb_reported_length_remaining(tvb, offset));
2061             offset = tvb_length(tvb); /* Finish decoding if unknown object is encountered... */
2062             break;
2063         }
2064         /* Increment the bit index for next time */
2065         bitindex++;
2066
2067         /* And increment the point address, may be overwritten by an index value */
2068         al_ptaddr++;
2069       }
2070       if (start_offset > offset) {
2071         expert_add_info_format(pinfo, point_item, PI_MALFORMED, PI_ERROR, "Invalid length");
2072         offset = tvb_length(tvb); /* Finish decoding if unknown object is encountered... */
2073       }
2074     }
2075   }
2076   proto_item_set_len(object_item, offset - orig_offset);
2077
2078   return offset;
2079 }
2080
2081 /*****************************************************************/
2082 /* Application Layer Dissector */
2083 /*****************************************************************/
2084 static int
2085 dissect_dnp3_al(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2086 {
2087   guint8        al_ctl, al_seq, al_func, al_class = 0, i;
2088   guint16       bytes, obj_type;
2089   gboolean      al_fir, al_fin, al_con, al_uns;
2090   guint         data_len = 0, offset = 0;
2091   proto_item   *ti = NULL, *tc, *t_robj;
2092   proto_tree   *al_tree = NULL, *field_tree = NULL, *robj_tree = NULL;
2093   const gchar  *func_code_str;
2094
2095   data_len = tvb_length(tvb);
2096
2097   /* Handle the control byte and function code */
2098   al_ctl = tvb_get_guint8(tvb, offset);
2099   al_seq = al_ctl & DNP3_AL_SEQ;
2100   al_fir = al_ctl & DNP3_AL_FIR;
2101   al_fin = al_ctl & DNP3_AL_FIN;
2102   al_con = al_ctl & DNP3_AL_CON;
2103   al_uns = al_ctl & DNP3_AL_UNS;
2104   al_func = tvb_get_guint8(tvb, (offset+1));
2105   func_code_str = val_to_str(al_func, dnp3_al_func_vals, "Unknown function (0x%02x)");
2106
2107   if (check_col(pinfo->cinfo, COL_INFO))
2108     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s", func_code_str);
2109     col_set_fence(pinfo->cinfo, COL_INFO);
2110
2111   /* format up the text representation */
2112   ti = proto_tree_add_text(tree, tvb, offset, data_len, "Application Layer: (");
2113   if (al_ctl & DNP3_AL_FIR)  proto_item_append_text(ti, "FIR, ");
2114   if (al_ctl & DNP3_AL_FIN)  proto_item_append_text(ti, "FIN, ");
2115   if (al_ctl & DNP3_AL_CON)  proto_item_append_text(ti, "CON, ");
2116   proto_item_append_text(ti, "Sequence %u, %s)", al_seq, func_code_str);
2117
2118   /* Add the al tree branch */
2119   al_tree = proto_item_add_subtree(ti, ett_dnp3_al);
2120
2121   /* Application Layer control byte subtree */
2122   tc = proto_tree_add_uint_format(al_tree, hf_dnp3_al_ctl, tvb, offset, 1, al_ctl,
2123       "Control: 0x%02x (", al_ctl);
2124   if (al_ctl & DNP3_AL_FIR)  proto_item_append_text(tc, "FIR, ");
2125   if (al_ctl & DNP3_AL_FIN)  proto_item_append_text(tc, "FIN, ");
2126   if (al_ctl & DNP3_AL_CON)  proto_item_append_text(tc, "CON, ");
2127   if (al_ctl & DNP3_AL_UNS)  proto_item_append_text(tc, "UNS, ");
2128   proto_item_append_text(tc, "Sequence %u)", al_seq);
2129
2130   field_tree = proto_item_add_subtree(tc, ett_dnp3_al_ctl);
2131   proto_tree_add_boolean(field_tree, hf_dnp3_al_fir, tvb, offset, 1, al_ctl);
2132   proto_tree_add_boolean(field_tree, hf_dnp3_al_fin, tvb, offset, 1, al_ctl);
2133   proto_tree_add_boolean(field_tree, hf_dnp3_al_con, tvb, offset, 1, al_ctl);
2134   proto_tree_add_boolean(field_tree, hf_dnp3_al_uns, tvb, offset, 1, al_ctl);
2135   proto_tree_add_item(field_tree, hf_dnp3_al_seq, tvb, offset, 1, FALSE);
2136   offset += 1;
2137
2138 #if 0
2139   /* If this packet is NOT the final Application Layer Message, exit and continue
2140      processing the remaining data in the fragment. */
2141   if (!al_fin)
2142   {
2143   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "Buffering User Data Until Final Frame is Received..");
2144   return 1;
2145   }
2146 #endif
2147
2148   /* Application Layer Function Code Byte  */
2149   proto_tree_add_uint_format(al_tree, hf_dnp3_al_func, tvb, offset, 1, al_func,
2150     "Function Code: %s (0x%02x)", func_code_str, al_func);
2151   offset += 1;
2152
2153   switch (al_func) {
2154
2155   case AL_FUNC_READ:     /* Read Function Code 0x01 */
2156
2157   /* Create Read Request Data Objects Tree */
2158   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "READ Request Data Objects");
2159   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2160
2161   /* Process Data Object Details */
2162   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2163     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, TRUE, &obj_type);
2164
2165     /* Update class type for each object that was a class read */
2166     switch(obj_type) {
2167       case AL_OBJ_CLASS0:
2168       case AL_OBJ_CLASS1:
2169       case AL_OBJ_CLASS2:
2170       case AL_OBJ_CLASS3:
2171         al_class |= (1 << ((obj_type & 0x0f) - 1));
2172         break;
2173       default:
2174         break;
2175     }
2176   }
2177
2178   /* Update the col info if there were class reads */
2179   if (check_col(pinfo->cinfo, COL_INFO) && (al_class > 0)) {
2180     col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "Class ");
2181     for (i = 0; i < 4; i++) {
2182       if (al_class & (1 << i)) {
2183         col_append_fstr(pinfo->cinfo, COL_INFO, "%u", i);
2184       }
2185     }
2186   }
2187
2188   break;
2189
2190   case AL_FUNC_WRITE:     /* Write Function Code 0x02 */
2191
2192   /* Create Write Request Data Objects Tree */
2193   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "WRITE Request Data Objects");
2194   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2195
2196   /* Process Data Object Details */
2197   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2198     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2199   }
2200
2201   break;
2202
2203   case AL_FUNC_SELECT:     /* Select Function Code 0x03 */
2204
2205   /* Create Select Request Data Objects Tree */
2206   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "SELECT Request Data Objects");
2207   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2208
2209   /* Process Data Object Details */
2210   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2211     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2212   }
2213
2214   break;
2215
2216   case AL_FUNC_OPERATE:    /* Operate Function Code 0x04 */
2217              /* Functionally identical to 'SELECT' Function Code */
2218
2219   /* Create Operate Request Data Objects Tree */
2220   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "OPERATE Request Data Objects");
2221   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2222
2223   /* Process Data Object Details */
2224   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2225     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2226   }
2227
2228   break;
2229
2230   case AL_FUNC_DIROP:     /* Direct Operate Function Code 0x05 */
2231             /* Functionally identical to 'SELECT' Function Code */
2232
2233   /* Create Direct Operate Request Data Objects Tree */
2234   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "DIRECT OPERATE Request Data Objects");
2235   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2236
2237   /* Process Data Object Details */
2238   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2239     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2240   }
2241
2242   break;
2243
2244   case AL_FUNC_ENSPMSG:   /* Enable Spontaneous Messages Function Code 0x14 */
2245
2246   /* Create Enable Spontaneous Messages Data Objects Tree */
2247   t_robj = proto_tree_add_text(al_tree, tvb, offset, -1, "Enable Spontaneous Msg's Data Objects");
2248   robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2249
2250   /* Process Data Object Details */
2251   while (offset <= (data_len-2))  {  /* 2 octet object code + CRC32 */
2252     offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2253   }
2254
2255   break;
2256
2257   case AL_FUNC_DELAYMST:  /* Delay Measurement Function Code 0x17 */
2258
2259   break;
2260
2261   case AL_FUNC_RESPON:   /* Response Function Code 0x81 */
2262   case AL_FUNC_UNSOLI:   /* Unsolicited Response Function Code 0x82 */
2263
2264   /* Application Layer IIN bits req'd if message is a response */
2265   dnp3_al_process_iin(tvb, offset, al_tree);
2266   offset += 2;
2267
2268   /* Ensure there is actual data remaining in the message.
2269      A response will not contain data following the IIN bits,
2270      if there is none available */
2271   bytes = tvb_reported_length_remaining(tvb, offset);
2272   if (bytes > 0)
2273   {
2274     /* Create Response Data Objects Tree */
2275     t_robj = proto_tree_add_text(al_tree, tvb, offset, -1,"RESPONSE Data Objects");
2276     robj_tree = proto_item_add_subtree(t_robj, ett_dnp3_al_objdet);
2277
2278     /* Process Data Object Details */
2279     while (offset <= (data_len-2)) {  /* 2 octet object code + CRC32 */
2280       offset = dnp3_al_process_object(tvb, pinfo, offset, robj_tree, FALSE, &obj_type);
2281     }
2282
2283     break;
2284   }
2285
2286   default:    /* Unknown Function */
2287
2288   break;
2289   }
2290
2291   return 0;
2292 }
2293
2294 /*****************************************************************/
2295 /* Data Link and Transport layer dissector */
2296 /*****************************************************************/
2297 static void
2298 dissect_dnp3_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2299 {
2300
2301 /* Set up structures needed to add the protocol subtree and manage it */
2302     proto_item   *ti = NULL, *tdl, *tc, *al_chunks, *hidden_item;
2303     proto_tree   *dnp3_tree = NULL, *dl_tree = NULL, *tr_tree = NULL, *field_tree = NULL, *al_tree = NULL;
2304     int           offset = 0, temp_offset = 0, al_result = 0;
2305     gboolean      dl_prm, tr_fir, tr_fin;
2306     guint8        dl_len, dl_ctl, dl_func, tr_ctl, tr_seq;
2307     const gchar  *func_code_str;
2308     guint16       dl_dst, dl_src, dl_crc, calc_dl_crc;
2309     guint8       *tmp = NULL, *tmp_ptr;
2310     guint8        data_len;
2311     int           data_offset;
2312     gboolean      crc_OK = FALSE;
2313     tvbuff_t     *al_tvb = NULL, *next_tvb;
2314     guint         i;
2315     guint         conv_seq_number;
2316     gboolean      save_fragmented;
2317     fragment_data *frag_msg;
2318     gboolean      update_col_info = TRUE;
2319     conversation_t *conversation;
2320     dnp3_conv_t  *conv_data_ptr;
2321
2322
2323
2324 /* Make entries in Protocol column and Info column on summary display */
2325   col_set_str(pinfo->cinfo, COL_PROTOCOL, "DNP 3.0");
2326
2327   col_clear(pinfo->cinfo, COL_INFO);
2328
2329   /* Skip "0x0564" header bytes */
2330   temp_offset += 2;
2331
2332   dl_len = tvb_get_guint8(tvb, temp_offset);
2333   temp_offset += 1;
2334
2335   dl_ctl = tvb_get_guint8(tvb, temp_offset);
2336   temp_offset += 1;
2337
2338   dl_dst = tvb_get_letohs(tvb, temp_offset);
2339   temp_offset += 2;
2340
2341   dl_src = tvb_get_letohs(tvb, temp_offset);
2342
2343   dl_func = dl_ctl & DNP3_CTL_FUNC;
2344   dl_prm = dl_ctl & DNP3_CTL_PRM;
2345   func_code_str = val_to_str(dl_func, dl_prm ? dnp3_ctl_func_pri_vals : dnp3_ctl_func_sec_vals,
2346            "Unknown function (0x%02x)");
2347
2348   if (check_col(pinfo->cinfo, COL_INFO))
2349     col_append_fstr(pinfo->cinfo, COL_INFO, "len=%u, from %u to %u, %s",
2350             dl_len, dl_src, dl_dst, func_code_str);
2351
2352   /* create display subtree for the protocol */
2353   ti = proto_tree_add_item(tree, proto_dnp3, tvb, offset, -1, FALSE);
2354   dnp3_tree = proto_item_add_subtree(ti, ett_dnp3);
2355
2356   /* Create Subtree for Data Link Layer */
2357   tdl = proto_tree_add_text(dnp3_tree, tvb, offset, DNP_HDR_LEN,
2358         "Data Link Layer, Len: %u, From: %u, To: %u, ", dl_len, dl_src, dl_dst);
2359   if (dl_prm) {
2360     if (dl_ctl & DNP3_CTL_DIR) proto_item_append_text(tdl, "DIR, ");
2361     if (dl_ctl & DNP3_CTL_PRM) proto_item_append_text(tdl, "PRM, ");
2362     if (dl_ctl & DNP3_CTL_FCB) proto_item_append_text(tdl, "FCB, ");
2363     if (dl_ctl & DNP3_CTL_FCV) proto_item_append_text(tdl, "FCV, ");
2364   }
2365   else {
2366     if (dl_ctl & DNP3_CTL_DIR) proto_item_append_text(tdl, "DIR, ");
2367     if (dl_ctl & DNP3_CTL_PRM) proto_item_append_text(tdl, "PRM, ");
2368     if (dl_ctl & DNP3_CTL_RES) proto_item_append_text(tdl, "RES, ");
2369     if (dl_ctl & DNP3_CTL_DFC) proto_item_append_text(tdl, "DFC, ");
2370   }
2371   proto_item_append_text(tdl, "%s", func_code_str);
2372   dl_tree = proto_item_add_subtree(tdl, ett_dnp3_dl);
2373
2374   /* start bytes */
2375   proto_tree_add_item(dl_tree, hf_dnp3_start, tvb, offset, 2, FALSE);
2376   offset += 2;
2377
2378   /* add length field */
2379   proto_tree_add_item(dl_tree, hf_dnp3_len, tvb, offset, 1, FALSE);
2380   offset += 1;
2381
2382   /* Add Control Byte Subtree */
2383   tc = proto_tree_add_uint_format(dl_tree, hf_dnp3_ctl, tvb, offset, 1, dl_ctl,
2384           "Control: 0x%02x (", dl_ctl);
2385   /* Add Text to Control Byte Subtree Header */
2386   if (dl_prm) {
2387     if (dl_ctl & DNP3_CTL_DIR) proto_item_append_text(tc, "DIR, ");
2388     if (dl_ctl & DNP3_CTL_PRM) proto_item_append_text(tc, "PRM, ");
2389     if (dl_ctl & DNP3_CTL_FCB) proto_item_append_text(tc, "FCB, ");
2390     if (dl_ctl & DNP3_CTL_FCV) proto_item_append_text(tc, "FCV, ");
2391   }
2392   else {
2393     if (dl_ctl & DNP3_CTL_DIR) proto_item_append_text(tc, "DIR, ");
2394     if (dl_ctl & DNP3_CTL_PRM) proto_item_append_text(tc, "PRM, ");
2395     if (dl_ctl & DNP3_CTL_RES) proto_item_append_text(tc, "RES, ");
2396     if (dl_ctl & DNP3_CTL_DFC) proto_item_append_text(tc, "DFC, ");
2397   }
2398   proto_item_append_text(tc, "%s)", func_code_str );
2399   field_tree = proto_item_add_subtree(tc, ett_dnp3_dl_ctl);
2400
2401   /* Add Control Byte Subtree Items */
2402   if (dl_prm) {
2403     proto_tree_add_item(field_tree, hf_dnp3_ctl_dir, tvb, offset, 1, TRUE);
2404     proto_tree_add_item(field_tree, hf_dnp3_ctl_prm, tvb, offset, 1, TRUE);
2405     proto_tree_add_item(field_tree, hf_dnp3_ctl_fcb, tvb, offset, 1, TRUE);
2406     proto_tree_add_item(field_tree, hf_dnp3_ctl_fcv, tvb, offset, 1, TRUE);
2407     proto_tree_add_item(field_tree, hf_dnp3_ctl_prifunc, tvb, offset, 1, FALSE);
2408   }
2409   else {
2410     proto_tree_add_item(field_tree, hf_dnp3_ctl_dir, tvb, offset, 1, TRUE);
2411     proto_tree_add_item(field_tree, hf_dnp3_ctl_prm, tvb, offset, 1, TRUE);
2412     proto_tree_add_item(field_tree, hf_dnp3_ctl_dfc, tvb, offset, 1, TRUE);
2413     proto_tree_add_item(field_tree, hf_dnp3_ctl_secfunc, tvb, offset, 1, FALSE);
2414   }
2415     offset += 1;
2416
2417   /* add destination and source addresses */
2418   proto_tree_add_item(dl_tree, hf_dnp3_dst, tvb, offset, 2, TRUE);
2419   offset += 2;
2420   proto_tree_add_item(dl_tree, hf_dnp3_src, tvb, offset, 2, TRUE);
2421   offset += 2;
2422
2423   /* and header CRC */
2424   dl_crc = tvb_get_letohs(tvb, offset);
2425   calc_dl_crc = calculateCRC(tvb_get_ptr(tvb, 0, DNP_HDR_LEN - 2), DNP_HDR_LEN - 2);
2426   if (dl_crc == calc_dl_crc)
2427     proto_tree_add_uint_format(dl_tree, hf_dnp_hdr_CRC, tvb, offset, 2,
2428                                dl_crc, "CRC: 0x%04x [correct]", dl_crc);
2429   else
2430   {
2431     hidden_item = proto_tree_add_boolean(dl_tree, hf_dnp_hdr_CRC_bad, tvb,
2432                                          offset, 2, TRUE);
2433     PROTO_ITEM_SET_HIDDEN(hidden_item);
2434     proto_tree_add_uint_format(dl_tree, hf_dnp_hdr_CRC, tvb, offset, 2,
2435                                dl_crc, "CRC: 0x%04x [incorrect, should be 0x%04x]",
2436                                dl_crc, calc_dl_crc);
2437   }
2438   offset += 2;
2439
2440   /* If the DataLink function is 'Request Link Status' or 'Status of Link',
2441      or 'Reset Link' we don't expect any Transport or Application Layer Data
2442      NOTE: This code should probably check what DOES have TR or AL data */
2443   if ((dl_func != DL_FUNC_LINK_STAT) && (dl_func != DL_FUNC_STAT_LINK) &&
2444       (dl_func != DL_FUNC_RESET_LINK) && (dl_func != DL_FUNC_ACK))
2445   {
2446
2447     /* get the transport layer byte */
2448     tr_ctl = tvb_get_guint8(tvb, offset);
2449     tr_seq = tr_ctl & DNP3_TR_SEQ;
2450     tr_fir = tr_ctl & DNP3_TR_FIR;
2451     tr_fin = tr_ctl & DNP3_TR_FIN;
2452
2453     /* Add Transport Layer Tree */
2454     tc = proto_tree_add_uint_format(dnp3_tree, hf_dnp3_tr_ctl, tvb, offset, 1, tr_ctl,
2455             "Transport Layer: 0x%02x (", tr_ctl);
2456     if (tr_fir) proto_item_append_text(tc, "FIR, ");
2457     if (tr_fin) proto_item_append_text(tc, "FIN, ");
2458     proto_item_append_text(tc, "Sequence %u)", tr_seq);
2459
2460     tr_tree = proto_item_add_subtree(tc, ett_dnp3_tr_ctl);
2461     proto_tree_add_boolean(tr_tree, hf_dnp3_tr_fin, tvb, offset, 1, tr_ctl);
2462     proto_tree_add_boolean(tr_tree, hf_dnp3_tr_fir, tvb, offset, 1, tr_ctl);
2463     proto_tree_add_item(tr_tree, hf_dnp3_tr_seq, tvb, offset, 1, FALSE);
2464
2465     /* Allocate AL chunk tree */
2466     al_chunks = proto_tree_add_text(tr_tree, tvb, offset + 1, -1, "Application data chunks");
2467     al_tree = proto_item_add_subtree(al_chunks, ett_dnp3_al_data);
2468
2469     /* extract the application layer data, validating the CRCs */
2470
2471     /* XXX - check for dl_len <= 5 */
2472     data_len = dl_len - 5;
2473     tmp = g_malloc(data_len);
2474     tmp_ptr = tmp;
2475     i = 0;
2476     data_offset = 1;  /* skip the transport layer byte when assembling chunks */
2477     while(data_len > 0)
2478     {
2479       guint8 chk_size;
2480       const guint8 *chk_ptr;
2481       guint16 calc_crc, act_crc;
2482
2483       chk_size = MIN(data_len, AL_MAX_CHUNK_SIZE);
2484       chk_ptr = tvb_get_ptr(tvb, offset, chk_size);
2485       memcpy(tmp_ptr, chk_ptr + data_offset, chk_size - data_offset);
2486       calc_crc = calculateCRC(chk_ptr, chk_size);
2487       offset += chk_size;
2488       tmp_ptr += chk_size - data_offset;
2489       act_crc = tvb_get_letohs(tvb, offset);
2490       offset += 2;
2491       crc_OK = calc_crc == act_crc;
2492       if (crc_OK)
2493       {
2494         proto_tree_add_text(al_tree, tvb, offset - (chk_size + 2), chk_size + 2,
2495                             "Application Chunk %u Len: %u CRC 0x%04x",
2496                             i, chk_size, act_crc);
2497         data_len -= chk_size;
2498       }
2499       else
2500       {
2501         proto_tree_add_text(al_tree, tvb, offset - (chk_size + 2), chk_size + 2,
2502                             "Application Chunk %u Len: %u Bad CRC got 0x%04x expected 0x%04x",
2503                             i, chk_size, act_crc, calc_crc);
2504         data_len = 0;
2505         break;
2506       }
2507       i++;
2508       data_offset = 0;  /* copy all of the rest of the chunks */
2509     }
2510
2511     /* if all crc OK, set up new tvb */
2512     if (crc_OK)
2513     {
2514       al_tvb = tvb_new_child_real_data(tvb, tmp, (guint) (tmp_ptr-tmp), (gint) (tmp_ptr-tmp));
2515       tvb_set_free_cb(al_tvb, g_free);
2516
2517       /* Check for fragmented packet */
2518       save_fragmented = pinfo->fragmented;
2519       if (! (tr_fir && tr_fin))
2520       {
2521         /* A fragmented packet */
2522         pinfo->fragmented = TRUE;
2523
2524         /* Look up the conversation to get the fragment reassembly id */
2525         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2526           pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2527
2528         if (conversation == NULL) {
2529           /* No conversation yet, so make one */
2530           conversation = conversation_new(pinfo->fd->num,  &pinfo->src, &pinfo->dst, pinfo->ptype,
2531             pinfo->srcport, pinfo->destport, 0);
2532         }
2533
2534         conv_data_ptr = (dnp3_conv_t*)conversation_get_proto_data(conversation, proto_dnp3);
2535
2536         if (conv_data_ptr == NULL) {
2537           /* New data structure required */
2538           conv_data_ptr = se_alloc(sizeof(dnp3_conv_t));
2539
2540           /*** Increment static global fragment reassembly id ***/
2541           conv_data_ptr->conv_seq_number = seq_number++;
2542
2543           conversation_add_proto_data(conversation, proto_dnp3, (void *)conv_data_ptr);
2544         }
2545         conv_seq_number = conv_data_ptr->conv_seq_number;
2546
2547         /*
2548         * Add the frame to
2549         * whatever reassembly is in progress, if any, and see
2550         * if it's done.
2551         */
2552
2553         frag_msg = fragment_add_seq_next(al_tvb, 0, pinfo, conv_seq_number,
2554             al_fragment_table,
2555             al_reassembled_table,
2556             tvb_reported_length(al_tvb), /* As this is a constructed tvb, all of it is ok */
2557             !tr_fin);
2558
2559         next_tvb = process_reassembled_data(al_tvb, 0, pinfo,
2560             "Reassembled DNP 3.0 Application Layer message", frag_msg, &dnp3_frag_items,
2561             &update_col_info, tr_tree);
2562
2563         if (next_tvb) { /* Reassembled */
2564           /* We have the complete payload */
2565           if (check_col (pinfo->cinfo, COL_INFO))
2566             col_set_str(pinfo->cinfo, COL_INFO, "Reassembled Application Layer");
2567             col_set_fence(pinfo->cinfo, COL_INFO);
2568         }
2569         else
2570         {
2571           /* We don't have the complete reassembled payload. */
2572           if (check_col (pinfo->cinfo, COL_INFO))
2573             col_add_fstr (pinfo->cinfo, COL_INFO,
2574                 "Application Layer fragment %u", tr_seq);
2575             col_set_fence(pinfo->cinfo, COL_INFO);
2576         }
2577
2578       }
2579       else
2580       {
2581         /* No reassembly required */
2582         next_tvb = al_tvb;
2583         add_new_data_source(pinfo, next_tvb, "DNP 3.0 Application Layer message");
2584         col_clear(pinfo->cinfo, COL_INFO);
2585       }
2586       pinfo->fragmented = save_fragmented;
2587     }
2588     else
2589     {
2590       /* CRC error - throw away the data. */
2591       next_tvb = NULL;
2592       g_free(tmp);
2593       proto_tree_add_text(dnp3_tree, tvb, 11, -1, "CRC failed, %u chunks", i);
2594     }
2595
2596     if (next_tvb)
2597     {
2598       al_result = dissect_dnp3_al(next_tvb, pinfo, dnp3_tree);
2599     }
2600   }
2601 }
2602
2603 static guint
2604 get_dnp3_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
2605 {
2606   guint16 message_len;  /* need 16 bits as total can exceed 255 */
2607   guint16 data_crc;     /* No. of user data CRC bytes */
2608   message_len = tvb_get_guint8(tvb, offset + 2);
2609
2610   /* Add in 2 bytes for header start octets,
2611             1 byte for len itself,
2612             2 bytes for header CRC
2613             data CRC bytes (2 bytes per 16 bytes of data
2614   */
2615
2616   data_crc = (guint16)(ceil((message_len - 5) / 16.0)) * 2;
2617   message_len += 2 + 1 + 2 + data_crc;
2618   return message_len;
2619 }
2620
2621 static int
2622 dissect_dnp3_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2623 {
2624   gint length = tvb_length(tvb);
2625
2626   /* Check for a dnp packet.  It should begin with 0x0564 */
2627   if(length < DNP_HDR_LEN || tvb_get_ntohs(tvb, 0) != 0x0564) {
2628     /* Not a DNP 3.0 packet, just happened to use the same port */
2629     return 0;
2630   }
2631
2632   tcp_dissect_pdus(tvb, pinfo, tree, TRUE, DNP_HDR_LEN,
2633                    get_dnp3_message_len, dissect_dnp3_message);
2634
2635   return tvb_length(tvb);
2636 }
2637
2638 static int
2639 dissect_dnp3_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2640 {
2641   gint length = tvb_length(tvb);
2642   /* Check for a dnp packet.  It should begin with 0x0564 */
2643   if(length < DNP_HDR_LEN || tvb_get_ntohs(tvb, 0) != 0x0564) {
2644     /* Not a DNP 3.0 packet, just happened to use the same port */
2645     return 0;
2646   }
2647
2648   dissect_dnp3_message(tvb, pinfo, tree);
2649   return length;
2650 }
2651
2652 static void
2653 al_defragment_init(void)
2654 {
2655   fragment_table_init(&al_fragment_table);
2656   reassembled_table_init(&al_reassembled_table);
2657 }
2658
2659 /* Register the protocol with Wireshark */
2660
2661 void
2662 proto_register_dnp3(void)
2663 {
2664
2665 /* Setup list of header fields */
2666   static hf_register_info hf[] = {
2667     { &hf_dnp3_start,
2668     { "Start Bytes", "dnp3.start", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2669
2670     { &hf_dnp3_len,
2671     { "Length", "dnp3.len", FT_UINT8, BASE_DEC, NULL, 0x0, "Frame Data Length", HFILL }},
2672
2673     { &hf_dnp3_ctl,
2674     { "Control", "dnp3.ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "Frame Control Byte", HFILL }},
2675
2676     { &hf_dnp3_ctl_prifunc,
2677     { "Control Function Code", "dnp3.ctl.prifunc", FT_UINT8, BASE_DEC,
2678       VALS(dnp3_ctl_func_pri_vals), DNP3_CTL_FUNC, "Frame Control Function Code", HFILL }},
2679
2680     { &hf_dnp3_ctl_secfunc,
2681     { "Control Function Code", "dnp3.ctl.secfunc", FT_UINT8, BASE_DEC,
2682       VALS(dnp3_ctl_func_sec_vals), DNP3_CTL_FUNC, "Frame Control Function Code", HFILL }},
2683
2684     { &hf_dnp3_ctl_dir,
2685     { "Direction", "dnp3.ctl.dir", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_CTL_DIR, NULL, HFILL }},
2686
2687     { &hf_dnp3_ctl_prm,
2688     { "Primary", "dnp3.ctl.prm", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_CTL_PRM, NULL, HFILL }},
2689
2690     { &hf_dnp3_ctl_fcb,
2691     { "Frame Count Bit", "dnp3.ctl.fcb", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_CTL_FCB, NULL, HFILL }},
2692
2693     { &hf_dnp3_ctl_fcv,
2694     { "Frame Count Valid", "dnp3.ctl.fcv", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_CTL_FCV, NULL, HFILL }},
2695
2696     { &hf_dnp3_ctl_dfc,
2697     { "Data Flow Control", "dnp3.ctl.dfc", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_CTL_DFC, NULL, HFILL }},
2698
2699     { &hf_dnp3_dst,
2700     { "Destination", "dnp3.dst", FT_UINT16, BASE_DEC, NULL, 0x0, "Destination Address", HFILL }},
2701
2702     { &hf_dnp3_src,
2703     { "Source", "dnp3.src", FT_UINT16, BASE_DEC, NULL, 0x0, "Source Address", HFILL }},
2704
2705     { &hf_dnp_hdr_CRC,
2706     { "CRC", "dnp3.hdr.CRC", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2707
2708     { &hf_dnp_hdr_CRC_bad,
2709     { "Bad CRC", "dnp3.hdr.CRC_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2710
2711     { &hf_dnp3_tr_ctl,
2712     { "Transport Control", "dnp3.tr.ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "Transport Layer Control Byte", HFILL }},
2713
2714     { &hf_dnp3_tr_fin,
2715     { "Final", "dnp3.tr.fin", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_TR_FIN, NULL, HFILL }},
2716
2717     { &hf_dnp3_tr_fir,
2718     { "First", "dnp3.tr.fir", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_TR_FIR, NULL, HFILL }},
2719
2720     { &hf_dnp3_tr_seq,
2721     { "Sequence", "dnp3.tr.seq", FT_UINT8, BASE_DEC, NULL, DNP3_TR_SEQ, "Frame Sequence Number", HFILL }},
2722
2723     { &hf_dnp3_al_ctl,
2724     { "Application Control", "dnp3.al.ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "Application Layer Control Byte", HFILL }},
2725
2726     { &hf_dnp3_al_fir,
2727     { "First", "dnp3.al.fir", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_AL_FIR, NULL, HFILL }},
2728
2729     { &hf_dnp3_al_fin,
2730     { "Final", "dnp3.al.fin", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_AL_FIN, NULL, HFILL }},
2731
2732     { &hf_dnp3_al_con,
2733     { "Confirm", "dnp3.al.con", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_AL_CON, NULL, HFILL }},
2734
2735     { &hf_dnp3_al_uns,
2736     { "Unsolicited", "dnp3.al.uns", FT_BOOLEAN, 8, TFS(&tfs_set_notset), DNP3_AL_UNS, NULL, HFILL }},
2737
2738     { &hf_dnp3_al_seq,
2739     { "Sequence", "dnp3.al.seq", FT_UINT8, BASE_DEC, NULL, DNP3_AL_SEQ, "Frame Sequence Number", HFILL }},
2740
2741     { &hf_dnp3_al_func,
2742     { "Application Layer Function Code", "dnp3.al.func", FT_UINT8, BASE_DEC,
2743       VALS(dnp3_al_func_vals), DNP3_AL_FUNC, "Application Function Code", HFILL }},
2744
2745     { &hf_dnp3_al_iin,
2746     { "Application Layer IIN bits", "dnp3.al.iin", FT_UINT16, BASE_DEC, NULL, 0x0, "Application Layer IIN", HFILL }},
2747
2748     { &hf_dnp3_al_iin_bmsg,
2749     { "Broadcast Msg Rx", "dnp3.al.iin.bmsg", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_BMSG, NULL, HFILL }},
2750
2751     { &hf_dnp3_al_iin_cls1d,
2752     { "Class 1 Data Available", "dnp3.al.iin.cls1d", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_CLS1D, NULL, HFILL }},
2753
2754     { &hf_dnp3_al_iin_cls2d,
2755     { "Class 2 Data Available", "dnp3.al.iin.cls2d", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_CLS2D, NULL, HFILL }},
2756
2757     { &hf_dnp3_al_iin_cls3d,
2758     { "Class 3 Data Available", "dnp3.al.iin.cls3d", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_CLS3D, NULL, HFILL }},
2759
2760     { &hf_dnp3_al_iin_tsr,
2761     { "Time Sync Required", "dnp3.al.iin.tsr", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_TSR, NULL, HFILL }},
2762
2763     { &hf_dnp3_al_iin_dol,
2764     { "Digital Outputs in Local", "dnp3.al.iin.dol", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_DOL, NULL, HFILL }},
2765
2766     { &hf_dnp3_al_iin_dt,
2767     { "Device Trouble", "dnp3.al.iin.dt", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_DT, NULL, HFILL }},
2768
2769     { &hf_dnp3_al_iin_rst,
2770     { "Device Restart", "dnp3.al.iin.rst", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_RST, NULL, HFILL }},
2771
2772     { &hf_dnp3_al_iin_obju,
2773     { "Requested Objects Unknown", "dnp3.al.iin.obju", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_OBJU, NULL, HFILL }},
2774
2775     { &hf_dnp3_al_iin_pioor,
2776     { "Parameters Invalid or Out of Range", "dnp3.al.iin.pioor", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_PIOOR, NULL, HFILL }},
2777
2778     { &hf_dnp3_al_iin_ebo,
2779     { "Event Buffer Overflow", "dnp3.al.iin.ebo", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_EBO, NULL, HFILL }},
2780
2781     { &hf_dnp3_al_iin_oae,
2782     { "Operation Already Executing", "dnp3.al.iin.oae", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_OAE, NULL, HFILL }},
2783
2784     { &hf_dnp3_al_iin_cc,
2785     { "Configuration Corrupt", "dnp3.al.iin.cc", FT_BOOLEAN, 16, TFS(&tfs_set_notset), AL_IIN_CC, NULL, HFILL }},
2786
2787     { &hf_dnp3_al_obj,
2788     { "Object", "dnp3.al.obj", FT_UINT16, BASE_HEX, VALS(dnp3_al_obj_vals), 0x0, "Application Layer Object", HFILL }},
2789
2790     { &hf_dnp3_al_objq_index,
2791     { "Index Prefix", "dnp3.al.objq.index", FT_UINT8, BASE_DEC, VALS(dnp3_al_objq_index_vals), AL_OBJQ_INDEX, "Object Index Prefixing", HFILL }},
2792
2793     { &hf_dnp3_al_objq_code,
2794     { "Qualifier Code", "dnp3.al.objq.code", FT_UINT8, BASE_DEC, VALS(dnp3_al_objq_code_vals), AL_OBJQ_CODE, "Object Qualifier Code", HFILL }},
2795
2796     { &hf_dnp3_al_range_start8,
2797     { "Start (8 bit)", "dnp3.al.range.start", FT_UINT8, BASE_DEC, NULL, 0x0, "Object Start Index", HFILL }},
2798
2799     { &hf_dnp3_al_range_stop8,
2800     { "Stop (8 bit)", "dnp3.al.range.stop", FT_UINT8, BASE_DEC, NULL, 0x0, "Object Stop Index", HFILL }},
2801
2802     { &hf_dnp3_al_range_start16,
2803     { "Start (16 bit)", "dnp3.al.range.start", FT_UINT16, BASE_DEC, NULL, 0x0, "Object Start Index", HFILL }},
2804
2805     { &hf_dnp3_al_range_stop16,
2806     { "Stop (16 bit)", "dnp3.al.range.stop", FT_UINT16, BASE_DEC, NULL, 0x0, "Object Stop Index", HFILL }},
2807
2808     { &hf_dnp3_al_range_start32,
2809     { "Start (32 bit)", "dnp3.al.range.start", FT_UINT32, BASE_DEC, NULL, 0x0, "Object Start Index", HFILL }},
2810
2811     { &hf_dnp3_al_range_stop32,
2812     { "Stop (32 bit)", "dnp3.al.range.stop", FT_UINT32, BASE_DEC, NULL, 0x0, "Object Stop Index", HFILL }},
2813
2814     { &hf_dnp3_al_range_abs8,
2815     { "Address (8 bit)", "dnp3.al.range.abs", FT_UINT8, BASE_DEC, NULL, 0x0, "Object Absolute Address", HFILL }},
2816
2817     { &hf_dnp3_al_range_abs16,
2818     { "Address (16 bit)", "dnp3.al.range.abs", FT_UINT16, BASE_DEC, NULL, 0x0, "Object Absolute Address", HFILL }},
2819
2820     { &hf_dnp3_al_range_abs32,
2821     { "Address (32 bit)", "dnp3.al.range.abs", FT_UINT32, BASE_DEC, NULL, 0x0, "Object Absolute Address", HFILL }},
2822
2823     { &hf_dnp3_al_range_quant8,
2824     { "Quantity (8 bit)", "dnp3.al.range.quantity", FT_UINT8, BASE_DEC, NULL, 0x0, "Object Quantity", HFILL }},
2825
2826     { &hf_dnp3_al_range_quant16,
2827     { "Quantity (16 bit)", "dnp3.al.range.quantity", FT_UINT16, BASE_DEC, NULL, 0x0, "Object Quantity", HFILL }},
2828
2829     { &hf_dnp3_al_range_quant32,
2830     { "Quantity (32 bit)", "dnp3.al.range.quantity", FT_UINT32, BASE_DEC, NULL, 0x0, "Object Quantity", HFILL }},
2831
2832     { &hf_dnp3_al_index8,
2833     { "Index (8 bit)", "dnp3.al.index", FT_UINT8, BASE_DEC, NULL, 0x0, "Object Index", HFILL }},
2834
2835     { &hf_dnp3_al_index16,
2836     { "Index (16 bit)", "dnp3.al.index", FT_UINT16, BASE_DEC, NULL, 0x0, "Object Index", HFILL }},
2837
2838     { &hf_dnp3_al_index32,
2839     { "Index (32 bit)", "dnp3.al.index", FT_UINT32, BASE_DEC, NULL, 0x0, "Object Index", HFILL }},
2840
2841     { &hf_dnp3_al_ptnum,
2842     { "Object Point Number", "dnp3.al.ptnum", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2843
2844     { &hf_dnp3_al_bit,
2845     { "Value (bit)", "dnp3.al.bit", FT_BOOLEAN, 8, TFS(&tfs_on_off), 0x1, "Digital Value (1 bit)", HFILL }},
2846
2847     { &hf_dnp3_al_2bit,
2848     { "Value (two bit)", "dnp3.al.2bit", FT_UINT8, BASE_DEC, NULL, 0x0, "Digital Value (2 bit)", HFILL }},
2849
2850     { &hf_dnp3_al_ana16,
2851     { "Value (16 bit)", "dnp3.al.ana", FT_UINT16, BASE_DEC, NULL, 0x0, "Analog Value (16 bit)", HFILL }},
2852
2853     { &hf_dnp3_al_ana32,
2854     { "Value (32 bit)", "dnp3.al.ana", FT_UINT32, BASE_DEC, NULL, 0x0, "Analog Value (32 bit)", HFILL }},
2855
2856     { &hf_dnp3_al_anaflt,
2857     { "Value (float)", "dnp3.al.ana", FT_FLOAT, BASE_NONE, NULL, 0x0, "Analog Value (float)", HFILL }},
2858
2859     { &hf_dnp3_al_anadbl,
2860     { "Value (double)", "dnp3.al.ana", FT_DOUBLE, BASE_NONE, NULL, 0x0, "Analog Value (double)", HFILL }},
2861
2862     { &hf_dnp3_al_anaout16,
2863     { "Output Value (16 bit)", "dnp3.al.anaout", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2864
2865     { &hf_dnp3_al_anaout32,
2866     { "Output Value (32 bit)", "dnp3.al.anaout", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2867
2868     { &hf_dnp3_al_anaoutflt,
2869     { "Output Value (float)", "dnp3.al.anaout", FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2870
2871     { &hf_dnp3_al_anaoutdbl,
2872     { "Output (double)", "dnp3.al.anaout", FT_DOUBLE, BASE_NONE, NULL, 0x0, "Output Value (double)", HFILL }},
2873
2874     { &hf_dnp3_al_cnt16,
2875     { "Counter (16 bit)", "dnp3.al.cnt", FT_UINT16, BASE_DEC, NULL, 0x0, "Counter Value (16 bit)", HFILL }},
2876
2877     { &hf_dnp3_al_cnt32,
2878     { "Counter (32 bit)", "dnp3.al.cnt", FT_UINT32, BASE_DEC, NULL, 0x0, "Counter Value (32 bit)", HFILL }},
2879
2880     { &hf_dnp3_al_ctrlstatus,
2881     { "Control Status", "dnp3.al.ctrlstatus", FT_UINT8, BASE_DEC, dnp3_al_ctl_status_vals, 0xff, NULL, HFILL }},
2882
2883     { &hf_dnp3_al_biq_b0,
2884     { "Online", "dnp3.al.biq.b0", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG0, NULL, HFILL }},
2885
2886     { &hf_dnp3_al_biq_b1,
2887     { "Restart", "dnp3.al.biq.b1", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG1, NULL, HFILL }},
2888
2889     { &hf_dnp3_al_biq_b2,
2890     { "Comm Fail", "dnp3.al.biq.b2", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG2, NULL, HFILL }},
2891
2892     { &hf_dnp3_al_biq_b3,
2893     { "Remote Force", "dnp3.al.biq.b3", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG3, NULL, HFILL }},
2894
2895     { &hf_dnp3_al_biq_b4,
2896     { "Local Force", "dnp3.al.biq.b4", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG4, NULL, HFILL }},
2897
2898     { &hf_dnp3_al_biq_b5,
2899     { "Chatter Filter", "dnp3.al.biq.b5", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG5, NULL, HFILL }},
2900
2901     { &hf_dnp3_al_biq_b6,
2902     { "Reserved", "dnp3.al.biq.b6", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG6, NULL, HFILL }},
2903
2904     { &hf_dnp3_al_biq_b7,
2905     { "Point Value", "dnp3.al.biq.b7", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BI_FLAG7, NULL, HFILL }},
2906
2907     { &hf_dnp3_al_boq_b0,
2908     { "Online", "dnp3.al.boq.b0", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG0, NULL, HFILL }},
2909
2910     { &hf_dnp3_al_boq_b1,
2911     { "Restart", "dnp3.al.boq.b1", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG1, NULL, HFILL }},
2912
2913     { &hf_dnp3_al_boq_b2,
2914     { "Comm Fail", "dnp3.al.boq.b2", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG2, NULL, HFILL }},
2915
2916     { &hf_dnp3_al_boq_b3,
2917     { "Remote Force", "dnp3.al.boq.b3", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG3, NULL, HFILL }},
2918
2919     { &hf_dnp3_al_boq_b4,
2920     { "Local Force", "dnp3.al.boq.b4", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG4, NULL, HFILL }},
2921
2922     { &hf_dnp3_al_boq_b5,
2923     { "Reserved", "dnp3.al.boq.b5", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG5, NULL, HFILL }},
2924
2925     { &hf_dnp3_al_boq_b6,
2926     { "Reserved", "dnp3.al.boq.b6", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG6, NULL, HFILL }},
2927
2928     { &hf_dnp3_al_boq_b7,
2929     { "Point Value", "dnp3.al.boq.b7", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_BO_FLAG7, NULL, HFILL }},
2930
2931     { &hf_dnp3_al_ctrq_b0,
2932     { "Online", "dnp3.al.ctrq.b0", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG0, NULL, HFILL }},
2933
2934     { &hf_dnp3_al_ctrq_b1,
2935     { "Restart", "dnp3.al.ctrq.b1", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG1, NULL, HFILL }},
2936
2937     { &hf_dnp3_al_ctrq_b2,
2938     { "Comm Fail", "dnp3.al.ctrq.b2", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG2, NULL, HFILL }},
2939
2940     { &hf_dnp3_al_ctrq_b3,
2941     { "Remote Force", "dnp3.al.ctrq.b3", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG3, NULL, HFILL }},
2942
2943     { &hf_dnp3_al_ctrq_b4,
2944     { "Local Force", "dnp3.al.ctrq.b4", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG4, NULL, HFILL }},
2945
2946     { &hf_dnp3_al_ctrq_b5,
2947     { "Roll-Over", "dnp3.al.ctrq.b5", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG5, NULL, HFILL }},
2948
2949     { &hf_dnp3_al_ctrq_b6,
2950     { "Discontinuity", "dnp3.al.ctrq.b6", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG6, NULL, HFILL }},
2951
2952     { &hf_dnp3_al_ctrq_b7,
2953     { "Reserved", "dnp3.al.ctrq.b7", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_CTR_FLAG7, NULL, HFILL }},
2954
2955     { &hf_dnp3_al_aiq_b0,
2956     { "Online", "dnp3.al.aiq.b0", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG0, NULL, HFILL }},
2957
2958     { &hf_dnp3_al_aiq_b1,
2959     { "Restart", "dnp3.al.aiq.b1", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG1, NULL, HFILL }},
2960
2961     { &hf_dnp3_al_aiq_b2,
2962     { "Comm Fail", "dnp3.al.aiq.b2", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG2, NULL, HFILL }},
2963
2964     { &hf_dnp3_al_aiq_b3,
2965     { "Remote Force", "dnp3.al.aiq.b3", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG3, NULL, HFILL }},
2966
2967     { &hf_dnp3_al_aiq_b4,
2968     { "Local Force", "dnp3.al.aiq.b4", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG4, NULL, HFILL }},
2969
2970     { &hf_dnp3_al_aiq_b5,
2971     { "Over-Range", "dnp3.al.aiq.b5", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG5, NULL, HFILL }},
2972
2973     { &hf_dnp3_al_aiq_b6,
2974     { "Reference Check", "dnp3.al.aiq.b6", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG6, NULL, HFILL }},
2975
2976     { &hf_dnp3_al_aiq_b7,
2977     { "Reserved", "dnp3.al.aiq.b7", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AI_FLAG7, NULL, HFILL }},
2978
2979     { &hf_dnp3_al_aoq_b0,
2980     { "Online", "dnp3.al.aoq.b0", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG0, NULL, HFILL }},
2981
2982     { &hf_dnp3_al_aoq_b1,
2983     { "Restart", "dnp3.al.aoq.b1", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG1, NULL, HFILL }},
2984
2985     { &hf_dnp3_al_aoq_b2,
2986     { "Comm Fail", "dnp3.al.aoq.b2", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG2, NULL, HFILL }},
2987
2988     { &hf_dnp3_al_aoq_b3,
2989     { "Remote Force", "dnp3.al.aoq.b3", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG3, NULL, HFILL }},
2990
2991     { &hf_dnp3_al_aoq_b4,
2992     { "Local Force", "dnp3.al.aoq.b4", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG4, NULL, HFILL }},
2993
2994     { &hf_dnp3_al_aoq_b5,
2995     { "Reserved", "dnp3.al.aoq.b5", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG5, NULL, HFILL }},
2996
2997     { &hf_dnp3_al_aoq_b6,
2998     { "Reserved", "dnp3.al.aoq.b6", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG6, NULL, HFILL }},
2999
3000     { &hf_dnp3_al_aoq_b7,
3001     { "Reserved", "dnp3.al.aoq.b7", FT_BOOLEAN, 8, TFS(&tfs_set_notset), AL_OBJ_AO_FLAG7, NULL, HFILL }},
3002
3003     { &hf_dnp3_al_timestamp,
3004     { "Timestamp", "dnp3.al.timestamp", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0, "Object Timestamp", HFILL }},
3005
3006     { &hf_dnp3_al_rel_timestamp,
3007     { "Relative Timestamp", "dnp3.al.reltimestamp", FT_RELATIVE_TIME, BASE_NONE, NULL, 0, "Object Relative Timestamp", HFILL }},
3008
3009     { &hf_dnp3_fragment,
3010     { "DNP 3.0 AL Fragment", "dnp3.al.fragment", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "DNP 3.0 Application Layer Fragment", HFILL }},
3011
3012     { &hf_dnp3_fragments,
3013     { "DNP 3.0 AL Fragments", "dnp3.al.fragments", FT_NONE, BASE_NONE, NULL, 0x0, "DNP 3.0 Application Layer Fragments", HFILL }},
3014
3015     { &hf_dnp3_fragment_overlap,
3016     { "Fragment overlap", "dnp3.al.fragment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "Fragment overlaps with other fragments", HFILL }},
3017
3018     { &hf_dnp3_fragment_overlap_conflict,
3019     { "Conflicting data in fragment overlap", "dnp3.al.fragment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3020       "Overlapping fragments contained conflicting data", HFILL }},
3021
3022     { &hf_dnp3_fragment_multiple_tails,
3023     { "Multiple tail fragments found", "dnp3.al.fragment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3024       "Several tails were found when defragmenting the packet", HFILL }},
3025
3026     { &hf_dnp3_fragment_too_long_fragment,
3027     { "Fragment too long", "dnp3.al.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3028       "Fragment contained data past end of packet", HFILL }},
3029
3030     { &hf_dnp3_fragment_error,
3031     { "Defragmentation error", "dnp3.al.fragment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
3032       "Defragmentation error due to illegal fragments", HFILL }},
3033
3034     { &hf_dnp3_fragment_reassembled_in,
3035     { "Reassembled PDU In Frame", "dnp3.al.fragment.reassembled_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
3036       "This PDU is reassembled in this frame", HFILL }}
3037   };
3038
3039 /* Setup protocol subtree array */
3040   static gint *ett[] = {
3041     &ett_dnp3,
3042     &ett_dnp3_dl,
3043     &ett_dnp3_dl_ctl,
3044     &ett_dnp3_tr_ctl,
3045     &ett_dnp3_al_data,
3046     &ett_dnp3_al,
3047     &ett_dnp3_al_ctl,
3048     &ett_dnp3_al_iin,
3049     &ett_dnp3_al_obj,
3050     &ett_dnp3_al_obj_qualifier,
3051     &ett_dnp3_al_obj_range,
3052     &ett_dnp3_al_objdet,
3053     &ett_dnp3_al_obj_quality,
3054     &ett_dnp3_al_obj_point,
3055     &ett_dnp3_fragment,
3056     &ett_dnp3_fragments
3057   };
3058   module_t *dnp3_module;
3059
3060 /* Register protocol init routine */
3061   register_init_routine(&al_defragment_init);
3062
3063 /* Register the protocol name and description */
3064   proto_dnp3 = proto_register_protocol("Distributed Network Protocol 3.0",
3065                    "DNP 3.0", "dnp3");
3066
3067 /* Required function calls to register the header fields and subtrees used */
3068   proto_register_field_array(proto_dnp3, hf, array_length(hf));
3069   proto_register_subtree_array(ett, array_length(ett));
3070
3071   dnp3_module = prefs_register_protocol(proto_dnp3, NULL);
3072   prefs_register_bool_preference(dnp3_module, "desegment",
3073     "Reassemble DNP3 messages spanning multiple TCP segments",
3074     "Whether the DNP3 dissector should reassemble messages spanning multiple TCP segments."
3075     " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3076     &dnp3_desegment);
3077 }
3078
3079
3080 /* If this dissector uses sub-dissector registration add a registration routine.
3081    This format is required because a script is used to find these routines and
3082    create the code that calls these routines.
3083 */
3084 void
3085 proto_reg_handoff_dnp3(void)
3086 {
3087   dissector_handle_t dnp3_tcp_handle;
3088   dissector_handle_t dnp3_udp_handle;
3089
3090   dnp3_tcp_handle = new_create_dissector_handle(dissect_dnp3_tcp, proto_dnp3);
3091   dnp3_udp_handle = new_create_dissector_handle(dissect_dnp3_udp, proto_dnp3);
3092   dissector_add("tcp.port", TCP_PORT_DNP, dnp3_tcp_handle);
3093   dissector_add("udp.port", UDP_PORT_DNP, dnp3_udp_handle);
3094 }
3095
3096 /*
3097  * Editor modelines
3098  *
3099  * Local Variables:
3100  * c-basic-offset: 2
3101  * tab-width: 8
3102  * indent-tabs-mode: nil
3103  * End:
3104  *
3105  * ex: set shiftwidth=2 tabstop=8 expandtab
3106  * :indentSize=2:tabSize=8:noTabs=true:
3107  */
3108