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