Move 3 ASN1 dissectors to 'clean' group; move 1 PIDL dissector to 'dirty' group.
[metze/wireshark/wip.git] / epan / dissectors / packet-opsi.c
1 /* packet-opsi.c
2  * Routines for OPSI protocol dissection
3  * Copyright 2004, Laurent Rabret (France Telecom R&D) <laurent.rabret@i.hate.spams.org>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include <epan/packet.h>
31 #include <epan/dissectors/packet-tcp.h>
32 #include <epan/prefs.h>
33
34 /* TCP destination port dedicated to the OPSI protocol */
35 #define TCP_PORT_OPSI           4002
36
37 /* Information position in OPSI header */
38 #define MAJOR_VERSION_OFFSET    0
39 #define MINOR_VERSION_OFFSET    1
40 #define CODE_OFFSET             2
41 #define HOOK_ID_OFFSET          3
42 #define PACKET_LENGTH_OFFSET    4
43 #define SESSION_OFFSET          6
44
45 #define HEADER_LENGTH           8
46
47
48 /* Valid OPSI code values */
49 #define DISCOVER_REQUEST        1
50 #define DISCOVER_RESPONSE       2
51 #define SERVICE_REQUEST         3
52 #define SERVICE_ACCEPT          4
53 #define SERVICE_REJECT          5
54 #define TERMINATE_REQUEST       6
55
56 /* Internal structure to dissect attributes */
57 typedef struct {
58         guint16         attribute_type;         /* attribute code */
59         const char      *tree_text;             /* text for fold out */
60         gint            *tree_id;               /* id for add_item */
61         int*            hf_type_attribute;      /* id for seach option */
62         void            (*dissect)(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
63 } opsi_attribute_handle_t;
64
65
66 /* Attributes codes */
67 #define USER_NAME_ATTRIBUTE             1
68 #define USER_PASSWD_ATTRIBUTE           2
69 #define CHAP_PASSWD_ATTRIBUTE           3
70 #define NAS_IP_ADDRESS_ATTRIBUTE        4
71 #define NAS_PORT_ATTRIBUTE              5
72 #define SERVICE_TYPE_ATTRIBUTE          6
73 #define FRAMED_PROTOCOL_ATTRIBUTE       7
74 #define FRAMED_ADDRESS_ATTRIBUTE        8
75 #define FRAMED_NETMASK_ATTRIBUTE        9
76 #define FRAMED_ROUTING_ATTRIBUTE        10
77 #define FRAMED_FILTER_ATTRIBUTE         11
78 #define FRAMED_MTU_ATTRIBUTE            12
79 #define FRAMED_COMPRESSION_ATTRIBUTE    13
80 #define CALLED_STATION_ID_ATTRIBUTE     30
81 #define CALLING_STATION_ID_ATTRIBUTE    31
82 #define NAS_IDENTIFIER                  32
83 #define ACCOUNTING_40_ATTRIBUTE         40
84 #define ACCOUNTING_41_ATTRIBUTE         41
85 #define ACCOUNTING_42_ATTRIBUTE         42
86 #define ACCOUNTING_43_ATTRIBUTE         43
87 #define ACCOUNTING_SESSION_ID_ATTRIBUTE 44
88 #define ACCOUNTING_45_ATTRIBUTE         45
89 #define ACCOUNTING_46_ATTRIBUTE         46
90 #define ACCOUNTING_47_ATTRIBUTE         47
91 #define ACCOUNTING_48_ATTRIBUTE         48
92 #define ACCOUNTING_49_ATTRIBUTE         49
93 #define ACCOUNTING_50_ATTRIBUTE         50
94 #define ACCOUNTING_51_ATTRIBUTE         51
95 #define ACCOUNTING_52_ATTRIBUTE         52
96 #define ACCOUNTING_53_ATTRIBUTE         53
97 #define ACCOUNTING_54_ATTRIBUTE         54
98 #define ACCOUNTING_55_ATTRIBUTE         55
99 #define ACCOUNTING_56_ATTRIBUTE         56
100 #define ACCOUNTING_57_ATTRIBUTE         57
101 #define ACCOUNTING_58_ATTRIBUTE         58
102 #define ACCOUNTING_59_ATTRIBUTE         59
103 #define CHAP_CHALLENGE_ATTRIBUTE        60
104 #define NAS_PORT_TYPE_ATTRIBUTE         61
105 #define DESIGNATION_NUMBER_ATTRIBUTE    77
106 #define NAS_PORT_ID_ATTRIBUTE           87
107
108 #define SMC_AAAID_ATTRIBUTE             651
109 #define SMC_VPNID_ATTRIBUTE             652
110 #define SMC_VPNNAME_ATTRIBUTE           653
111 #define SMC_RANID_ATTRIBUTE             654
112 #define SMC_RANIP_ATTRIBUTE             655
113 #define SMC_RANNAME_ATTRIBUTE           656
114 #define SMC_POPID_ATTRIBUTE             657
115 #define SMC_POPNAME_ATTRIBUTE           658
116 #define SMC_SMCID_ATTRIBUTE             659
117 #define SMC_RECEIVE_TIME_ATTRIBUTE      660
118 #define SMC_STAT_TIME_ATTRIBUTE         661
119
120 #define OPSI_FLAGS_ATTRIBUTE            674
121 #define OPSI_APPLICATION_NAME_ATTRIBUTE 675
122
123 /*
124  * Published API functions.  NOTE, "local" API functions
125  * only valid from the packet-opsi file.
126  */
127 static void decode_string_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
128 static void decode_ipv4_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
129 static void decode_longint_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
130 static void decode_value_string_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
131 static void decode_time_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length);
132 /******* *******/
133
134 /* Initialize the protocol and registered fields */
135 static int proto_opsi                   = -1;
136 static int hf_opsi_major_version        = -1;
137 static int hf_opsi_minor_version        = -1;
138 static int hf_opsi_opcode               = -1;
139 static int hf_opsi_hook_id              = -1;
140 static int hf_opsi_length               = -1;
141 static int hf_opsi_session_id           = -1;
142 static int hf_user_name_att             = -1;
143 static int hf_password_att              = -1;
144 static int hf_chap_password_att         = -1;
145 static int hf_nas_ip_add_att            = -1;
146 static int hf_nas_port_att              = -1;
147 static int hf_service_type_att          = -1;
148 static int hf_framed_protocol_att       = -1;
149 static int hf_framed_address_att        = -1;
150 static int hf_framed_netmask_att        = -1;
151 static int hf_framed_routing_att        = -1;
152 static int hf_framed_filter_att         = -1;
153 static int hf_framed_mtu_att            = -1;
154 static int hf_framed_compression_att    = -1;
155 static int hf_called_station_att        = -1;
156 static int hf_calling_station_att       = -1;
157 static int hf_nas_identifier_att        = -1;
158 static int hf_accounting_att            = -1;
159 static int hf_acct_session_id_att       = -1;
160 static int hf_chap_challenge_att        = -1;
161 static int hf_nas_port_type_att         = -1;
162 static int hf_designation_num_att       = -1;
163 static int hf_nas_port_id_att           = -1;
164 static int hf_smc_aaa_id_att            = -1;
165 static int hf_smc_vpn_id_att            = -1;
166 static int hf_smc_vpn_name_att          = -1;
167 static int hf_smc_ran_id_att            = -1;
168 static int hf_smc_ran_ip_att            = -1;
169 static int hf_smc_ran_name_att          = -1;
170 static int hf_smc_pop_id_att            = -1;
171 static int hf_smc_pop_name_att          = -1;
172 static int hf_smc_id_att                = -1;
173 static int hf_smc_receive_time_att      = -1;
174 static int hf_smc_stat_time_att         = -1;
175 static int hf_opsi_flags_att            = -1;
176 static int hf_opsi_application_name_att = -1;
177
178 /* Initialize the subtree pointers */
179 static gint ett_opsi                    = -1;
180 static gint ett_opsi_user_name          = -1;
181 static gint ett_opsi_user_password      = -1;
182 static gint ett_opsi_chap_password      = -1;
183 static gint ett_opsi_nas_ip_address     = -1;
184 static gint ett_opsi_nas_port           = -1;
185 static gint ett_opsi_service_type       = -1;
186 static gint ett_opsi_framed_protocol    = -1;
187 static gint ett_opsi_framed_address     = -1;
188 static gint ett_opsi_framed_netmask     = -1;
189 static gint ett_opsi_framed_routing     = -1;
190 static gint ett_opsi_framed_filter      = -1;
191 static gint ett_opsi_framed_mtu         = -1;
192 static gint ett_opsi_framed_compression = -1;
193 static gint ett_opsi_called_station_id  = -1;
194 static gint ett_opsi_calling_station_id = -1;
195 static gint ett_opsi_nas_identifier     = -1;
196 static gint ett_opsi_accounting         = -1;
197 static gint ett_opsi_acct_session_id    = -1;
198 static gint ett_opsi_chap_challenge     = -1;
199 static gint ett_opsi_nas_port_type      = -1;
200 static gint ett_opsi_designation_number = -1;
201 static gint ett_opsi_nas_port_id        = -1;
202 static gint ett_opsi_smc_aaa_id         = -1;
203 static gint ett_opsi_smc_vpn_id         = -1;
204 static gint ett_opsi_smc_vpn_name       = -1;
205 static gint ett_opsi_smc_ran_id         = -1;
206 static gint ett_opsi_smc_ran_ip         = -1;
207 static gint ett_opsi_smc_ran_name       = -1;
208 static gint ett_opsi_smc_pop_id         = -1;
209 static gint ett_opsi_smc_pop_name       = -1;
210 static gint ett_opsi_smc_id             = -1;
211 static gint ett_opsi_smc_receive_time   = -1;
212 static gint ett_opsi_smc_stat_time      = -1;
213 static gint ett_opsi_flags              = -1;
214 static gint ett_opsi_application_name   = -1;
215
216
217 /* Code mapping */
218 static const value_string opsi_opcode[] = {
219                 { DISCOVER_REQUEST,     "Discover Request" },
220                 { DISCOVER_RESPONSE,    "Discover Response" },
221                 { SERVICE_REQUEST,      "Service Request" },
222                 { SERVICE_ACCEPT,       "Service Accept" },
223                 { SERVICE_REJECT,       "Service Reject" },
224                 { TERMINATE_REQUEST,    "Terminate Request" },
225                 { 0,                    NULL }
226         };
227
228 static const value_string opsi_service_type_code[] = {
229                 { 1, "Login" },
230                 { 2, "Framed" },
231                 { 3, "Callback Login" },
232                 { 4, "Callback Framed" },
233                 { 5, "Outbound" },
234                 { 6, "Administrative" },
235                 { 7, "NAS Prompt" },
236                 { 8, "Authenticate Only" },
237                 { 9, "Callback NAS Prompt" },
238                 { 0,                    NULL }
239         };
240
241 static const value_string opsi_framed_protocol_code[] = {
242                 { 1,    "PPP" },
243                 { 2,    "SLIP" },
244                 { 3,    "AppleTalk Remote Access Protocol (ARAP)" },
245                 { 4,    "Gandalf proprietary SingleLink/MultiLink protocol" },
246                 { 5,    "Xylogics proprietary IPX/SLIP" },
247                 { 255,  "Ascend ARA" },
248                 { 256,  "MPP" },
249                 { 257,  "EURAW" },
250                 { 258,  "EUUI" },
251                 { 259,  "X25" },
252                 { 260,  "COMB" },
253                 { 261,  "FR" },
254                 { 262,  "MP" },
255                 { 263,  "FR-CIR"},
256                 { 0,                    NULL }
257         };
258
259 static const value_string opsi_framed_routing_code[] = {
260                 { 0,    "None" },
261                 { 1,    "Broadcast" },
262                 { 2,    "Listen" },
263                 { 3,    "Broadcast-Listen" },
264                 { 4,    "Broadcast V2" },
265                 { 5,    "Listen V2" },
266                 { 6,    "Broadcast-Listen V2" },
267                 { 0,    NULL },
268         };
269
270 static const value_string opsi_framed_compression_code[] = {
271                 { 0,    "None" },
272                 { 1,    "Van Jacobsen TCP/IP" },
273                 { 2,    "IPX header compression" },
274                 { 0,    NULL }
275         };
276
277 static const value_string opsi_nas_port_type_code[] = {
278                 { 0, "Async" },
279                 { 1, "Sync" },
280                 { 2, "ISDN Sync" },
281                 { 3, "ISDN Async V.120" },
282                 { 4, "ISDN Async V.110" },
283                 { 5, "Virtual" },
284                 { 6, "PIAFS" },
285                 { 7, "HDLC Clear Channel" },
286                 { 8, "X.25" },
287                 { 9, "X.75" },
288                 { 10, "G.3 Fax" },
289                 { 11, "SDSL - Symmetric DSL" },
290                 { 12, "ADSL-CAP - Asymmetric DSL, Carrierless Amplitude Phase Modulation" },
291                 { 13, "ADSL-DMT - Asymmetric DSL, Discrete Multi-Tone" },
292                 { 14, "IDSL - ISDN Digital Subscriber Line" },
293                 { 15, "Ethernet" },
294                 { 16, "xDSL - Digital Subscriber Line of unknown type" },
295                 { 17, "Cable" },
296                 { 18, "Wireless - Other" },
297                 { 19, "Wireless - IEEE 802.11" },
298                 { 201,"Voice over IP" },
299                 { 0,                    NULL }
300         };
301
302
303 /* Structure used to decode OPSI frame attributes       */
304 /* CAUTION : it is compulsory to sort this array        */
305 /* (first argument of the opsi_attribute_handle_t)      */
306 /* in ascending order                                   */
307 /*                                                      */
308 static opsi_attribute_handle_t opsi_attributes[] = {
309         { USER_NAME_ATTRIBUTE,          /* 1 */
310         "User name attribute", &ett_opsi_user_name, &hf_user_name_att, decode_string_attribute },
311         { USER_PASSWD_ATTRIBUTE,        /* 2 */
312         "User password attribute" , &ett_opsi_user_password, &hf_password_att, decode_string_attribute },
313         { CHAP_PASSWD_ATTRIBUTE,        /* 3 */
314         "CHAP password attribute", &ett_opsi_chap_password, &hf_chap_password_att, decode_string_attribute },
315         { NAS_IP_ADDRESS_ATTRIBUTE,     /* 4 */
316         "NAS IP address attribute", &ett_opsi_nas_ip_address, &hf_nas_ip_add_att, decode_ipv4_attribute },
317         {NAS_PORT_ATTRIBUTE,            /* 5 */
318         "NAS port attribute", &ett_opsi_nas_port, &hf_nas_port_att, decode_longint_attribute },
319         {SERVICE_TYPE_ATTRIBUTE,        /* 6 */
320         "Service type attribute", &ett_opsi_service_type, &hf_service_type_att, decode_value_string_attribute },
321         {FRAMED_PROTOCOL_ATTRIBUTE,     /* 7 */
322         "Framed protocol attribute", &ett_opsi_framed_protocol, &hf_framed_protocol_att, decode_value_string_attribute },
323         {FRAMED_ADDRESS_ATTRIBUTE,      /* 8 */
324         "Framed address attribute", &ett_opsi_framed_address, &hf_framed_address_att, decode_ipv4_attribute },
325         {FRAMED_NETMASK_ATTRIBUTE,      /* 9 */
326         "Framed netmask attribute", &ett_opsi_framed_netmask, &hf_framed_netmask_att, decode_ipv4_attribute },
327         {FRAMED_ROUTING_ATTRIBUTE,      /* 10 */
328         "Framed routing attribute", &ett_opsi_framed_routing, &hf_framed_routing_att, decode_value_string_attribute },
329         {FRAMED_FILTER_ATTRIBUTE,       /* 11 */
330         "Framed filter attribute", &ett_opsi_framed_filter, &hf_framed_filter_att, decode_string_attribute },
331         {FRAMED_MTU_ATTRIBUTE,          /* 12 */
332         "Framed MTU attribute", &ett_opsi_framed_mtu, &hf_framed_mtu_att, decode_longint_attribute },
333         {FRAMED_COMPRESSION_ATTRIBUTE,  /* 13 */
334         "Framed compression attribute", &ett_opsi_framed_compression, &hf_framed_compression_att, decode_value_string_attribute },
335         {CALLED_STATION_ID_ATTRIBUTE,   /* 30 */
336         "Called station ID attribute", &ett_opsi_called_station_id, &hf_called_station_att, decode_string_attribute },
337         {CALLING_STATION_ID_ATTRIBUTE,  /* 31 */
338         "Calling station ID attribute", &ett_opsi_calling_station_id, &hf_calling_station_att, decode_string_attribute },
339         {NAS_IDENTIFIER,                /* 32 */
340         "NAS Identifier attribute", &ett_opsi_nas_identifier, &hf_nas_identifier_att, decode_string_attribute },
341         {ACCOUNTING_40_ATTRIBUTE,       /* 40 */
342         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
343         {ACCOUNTING_41_ATTRIBUTE,       /* 41 */
344         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
345         {ACCOUNTING_42_ATTRIBUTE,       /* 42 */
346         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
347         {ACCOUNTING_43_ATTRIBUTE,       /* 43 */
348         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
349         {ACCOUNTING_SESSION_ID_ATTRIBUTE,       /* 44 */
350         "Accounting session ID attribute", &ett_opsi_acct_session_id, &hf_acct_session_id_att, decode_string_attribute },
351         {ACCOUNTING_45_ATTRIBUTE,       /* 45 */
352         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
353         {ACCOUNTING_46_ATTRIBUTE,       /* 46 */
354         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
355         {ACCOUNTING_47_ATTRIBUTE,       /* 47 */
356         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
357         {ACCOUNTING_48_ATTRIBUTE,       /* 48 */
358         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
359         {ACCOUNTING_49_ATTRIBUTE,       /* 49 */
360         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
361         {ACCOUNTING_50_ATTRIBUTE,       /* 50 */
362         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
363         {ACCOUNTING_51_ATTRIBUTE,       /* 51 */
364         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
365         {ACCOUNTING_52_ATTRIBUTE,       /* 52 */
366         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
367         {ACCOUNTING_53_ATTRIBUTE,       /* 53 */
368         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
369         {ACCOUNTING_54_ATTRIBUTE,       /* 54 */
370         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
371         {ACCOUNTING_55_ATTRIBUTE,       /* 55 */
372         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
373         {ACCOUNTING_56_ATTRIBUTE,       /* 56 */
374         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
375         {ACCOUNTING_57_ATTRIBUTE,       /* 57 */
376         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
377         {ACCOUNTING_58_ATTRIBUTE,       /* 58 */
378         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
379         {ACCOUNTING_59_ATTRIBUTE,       /* 59 */
380         "Accounting attribute", &ett_opsi_accounting, &hf_accounting_att, decode_string_attribute },
381         {CHAP_CHALLENGE_ATTRIBUTE,      /* 60 */
382         "CHAP challenge",       &ett_opsi_chap_challenge, &hf_chap_challenge_att, decode_string_attribute },
383         {NAS_PORT_TYPE_ATTRIBUTE,       /* 61 */
384         "NAS port type attribute", &ett_opsi_nas_port_type, &hf_nas_port_type_att, decode_value_string_attribute },
385         {DESIGNATION_NUMBER_ATTRIBUTE,  /* 77 */
386         "Designation number attribute", &ett_opsi_designation_number, &hf_designation_num_att, decode_string_attribute },
387         {NAS_PORT_ID_ATTRIBUTE,         /* 87 */
388         "NAS port ID attribute", &ett_opsi_nas_port_id, &hf_nas_port_id_att, decode_string_attribute },
389         {SMC_AAAID_ATTRIBUTE,           /* 651 */
390         "SMC AAA ID attribute", &ett_opsi_smc_aaa_id, &hf_smc_aaa_id_att, decode_longint_attribute },
391         {SMC_VPNID_ATTRIBUTE,           /* 652 */
392         "SMC VPN ID attribute", &ett_opsi_smc_vpn_id, &hf_smc_vpn_id_att, decode_longint_attribute },
393         {SMC_VPNNAME_ATTRIBUTE,         /* 653 */
394         "SMC VPN name attribute", &ett_opsi_smc_vpn_name, &hf_smc_vpn_name_att, decode_string_attribute },
395         {SMC_RANID_ATTRIBUTE,           /* 654 */
396         "SMC RAN ID attribute", &ett_opsi_smc_ran_id, &hf_smc_ran_id_att, decode_longint_attribute },
397         {SMC_RANIP_ATTRIBUTE,           /* 655 */
398         "SMC RAN IP attribute", &ett_opsi_smc_ran_ip, &hf_smc_ran_ip_att, decode_ipv4_attribute },
399         {SMC_RANNAME_ATTRIBUTE,         /* 656 */
400         "SMC RAN name attribute", &ett_opsi_smc_ran_name, &hf_smc_ran_name_att, decode_string_attribute },
401         {SMC_POPID_ATTRIBUTE,           /* 657 */
402         "SMC POP ID attribute", &ett_opsi_smc_pop_id, &hf_smc_pop_id_att, decode_longint_attribute },
403         {SMC_POPNAME_ATTRIBUTE,         /* 658 */
404         "SMC POP name attribute", &ett_opsi_smc_pop_name, &hf_smc_pop_name_att, decode_string_attribute },
405         {SMC_SMCID_ATTRIBUTE,           /* 659 */
406         "SMC ID attribute", &ett_opsi_smc_id, &hf_smc_id_att, decode_longint_attribute },
407         {SMC_RECEIVE_TIME_ATTRIBUTE,    /* 660 */
408         "SMC receive time attribute", &ett_opsi_smc_receive_time, &hf_smc_receive_time_att, decode_time_attribute },
409         {SMC_STAT_TIME_ATTRIBUTE,       /* 661 */
410         "SMC stat time attribute", &ett_opsi_smc_stat_time, &hf_smc_stat_time_att, decode_longint_attribute },
411         {OPSI_FLAGS_ATTRIBUTE,          /* 674 */
412         "OPSI flags attribute", &ett_opsi_flags, &hf_opsi_flags_att, decode_longint_attribute },
413         {OPSI_APPLICATION_NAME_ATTRIBUTE,/* 675 */
414         "OPSI application name attribute", &ett_opsi_application_name, &hf_opsi_application_name_att, decode_string_attribute },
415
416 };
417 #define OPSI_ATTRIBUTES_COUNT (sizeof(opsi_attributes)/sizeof(opsi_attribute_handle_t))
418
419 /* Desegmentation of OPSI (over TCP) */
420 static gboolean opsi_desegment = TRUE;
421 /* To check if we must create or update the information column  */
422 static gboolean opsi_first;
423
424 static void
425 decode_string_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length)
426 {
427         guint8* pbuffer;
428         if (length < 4) {
429                 proto_tree_add_text(tree, tvb, offset, length, "Too short attribute!");
430                 return;
431         }
432
433         pbuffer=tvb_get_ephemeral_string(tvb, offset+4, length-4);
434         proto_tree_add_string(tree, *hfValue, tvb, offset+4, length-4, pbuffer);
435 }
436
437
438 static void
439 decode_ipv4_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length)
440 {
441         guint32 ip_address;
442         if (length < 8) {
443                 proto_tree_add_text(tree, tvb, offset, length, "Too short attribute!");
444                 return;
445         }
446         ip_address = tvb_get_ipv4(tvb, offset+4);
447         proto_tree_add_ipv4(tree, *hfValue, tvb, offset+4, 4, ip_address);
448 }
449
450 static void
451 decode_longint_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length)
452 {
453         if (length < 8) {
454                 proto_tree_add_text(tree, tvb, offset, length, "Too short attribute!");
455                 return;
456         }
457         proto_tree_add_uint(tree, *hfValue, tvb, offset+4, 4, tvb_get_ntohl(tvb, offset+4));
458 }
459
460 static void
461 decode_value_string_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length)
462 {
463         if (length < 8) {
464                 proto_tree_add_text(tree, tvb, offset, length, "Too short attribute!");
465                 return;
466         }
467         proto_tree_add_item(tree, *hfValue, tvb, offset+4, 4, ENC_BIG_ENDIAN);
468 }
469
470 static void
471 decode_time_attribute(tvbuff_t *tvb, proto_tree *tree, int* hfValue, int offset, int length)
472 {
473         nstime_t ns;
474
475         if (length < 8) {
476                 proto_tree_add_text(tree, tvb, offset, length, "Too short attribute!");
477                 return;
478         }
479       ns.secs  = tvb_get_ntohl(tvb, offset+4);
480       ns.nsecs = 0;
481       proto_tree_add_time(tree, *hfValue, tvb, offset+4, 4, &ns);
482 }
483
484 /****************************************************************************/
485 /********** End of attribute decoding ***************************************/
486 /****************************************************************************/
487
488 /* To find the correct size of the PDU. Needed by the desegmentation feature*/
489 static guint
490 get_opsi_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
491 {
492   /*
493    * Get the length of the OPSI packet.
494    * We are guaranteed there're enough chars in tvb in order to
495    * extract the length value. No exception thrown !!
496    */
497   return tvb_get_ntohs(tvb, offset + 4);
498 }
499
500 static int
501 get_opsi_attribute_index(int min, int max, int attribute_type)
502 {
503         int middle, at;
504
505         middle = (min+max)/2;
506         at = opsi_attributes[middle].attribute_type;
507         if (at == attribute_type) return middle;
508         if (attribute_type > at) {
509                 return (middle == max) ? -1 : get_opsi_attribute_index(middle+1, max, attribute_type);
510         }
511         return (middle == min) ? -1 : get_opsi_attribute_index(min, middle-1, attribute_type);
512 }
513
514
515 static void
516 dissect_attributes(tvbuff_t *tvb, proto_tree *opsi_tree, int offset, int length)
517 {
518         int i;
519         int attribute_type;
520         int attribute_length;
521         proto_item *ti;
522         proto_tree *ntree = NULL;
523
524         while (length >= 4) {
525                 attribute_type          = tvb_get_ntohs(tvb, offset);
526                 attribute_length        = tvb_get_ntohs(tvb, offset+2);
527                 if (attribute_length > length) break;
528                 /* We perform a standard log(n) lookup */
529                 i = get_opsi_attribute_index(0, OPSI_ATTRIBUTES_COUNT-1, attribute_type);
530                 if (i == -1) {
531                         proto_tree_add_text(opsi_tree, tvb, offset, attribute_length, "Unknown attribute (%d)", attribute_type);
532                 }
533                 else {
534                         ti = proto_tree_add_text(opsi_tree, tvb, offset, attribute_length, "%s (%d)", opsi_attributes[i].tree_text, attribute_type);
535                         ntree = proto_item_add_subtree(ti, *opsi_attributes[i].tree_id);
536                         proto_tree_add_text(ntree, tvb, offset+2, 2, "Length (%d)", attribute_length);
537                         opsi_attributes[i].dissect(tvb, ntree, opsi_attributes[i].hf_type_attribute, offset, attribute_length);
538                 }
539                 if (attribute_length < 4) {
540                         /* Length must be at least 4, for the type and length. */
541                         break;
542                 }
543                 offset += attribute_length;
544                 length -= attribute_length;
545         }
546         if (length) {
547                 proto_tree_add_text(opsi_tree, tvb, offset, -1, "Short frame");
548         }
549 }
550
551 static void
552 dissect_opsi_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
553 {
554         proto_item *ti;
555         proto_tree *opsi_tree;
556
557         if (opsi_first == TRUE) {
558                 opsi_first = FALSE;
559
560                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "OPSI");
561
562                 if (check_col(pinfo->cinfo, COL_INFO)) {
563                         col_clear(pinfo->cinfo, COL_INFO);
564                         if (tvb_length(tvb) < CODE_OFFSET+1) {
565                                 col_set_str(pinfo->cinfo, COL_INFO, "Open Policy Service Interface");
566                         }
567                         else {
568                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Open Policy Service Interface, %s",
569                                         val_to_str(tvb_get_guint8(tvb, CODE_OFFSET), opsi_opcode,
570                                      "<Unknown opcode %d>"));
571                         }
572                 }
573         }
574         else if (check_col(pinfo->cinfo, COL_INFO) && (tvb_length(tvb) > CODE_OFFSET)) {
575                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s",
576                           val_to_str(tvb_get_guint8(tvb, CODE_OFFSET), opsi_opcode,
577                                      "<Unknown opcode %d>"));
578         }
579
580         if (tree) {
581                 ti = proto_tree_add_item(tree, proto_opsi, tvb, 0, -1, ENC_NA);
582                 opsi_tree = proto_item_add_subtree(ti, ett_opsi);
583                 if (tvb_length(tvb) < 8 ) {
584                         proto_tree_add_text(opsi_tree, tvb, 0, -1, "Too short OPSI packet!");
585                         return;
586                 }
587
588                 proto_tree_add_item(opsi_tree, hf_opsi_major_version, tvb, MAJOR_VERSION_OFFSET, 1, ENC_BIG_ENDIAN);
589                 proto_tree_add_item(opsi_tree, hf_opsi_minor_version, tvb, MINOR_VERSION_OFFSET, 1, ENC_BIG_ENDIAN);
590                 proto_tree_add_item(opsi_tree, hf_opsi_opcode, tvb, CODE_OFFSET, 1, ENC_BIG_ENDIAN);
591                 proto_tree_add_item(opsi_tree, hf_opsi_hook_id, tvb, HOOK_ID_OFFSET, 1, ENC_BIG_ENDIAN);
592                 proto_tree_add_item(opsi_tree, hf_opsi_length, tvb, PACKET_LENGTH_OFFSET, 2, ENC_BIG_ENDIAN);
593                 proto_tree_add_item(opsi_tree, hf_opsi_session_id, tvb, SESSION_OFFSET, 2, ENC_BIG_ENDIAN);
594
595                 dissect_attributes(tvb, opsi_tree, HEADER_LENGTH, MIN(((int)tvb_length(tvb)-HEADER_LENGTH), (tvb_get_ntohs(tvb, PACKET_LENGTH_OFFSET)-HEADER_LENGTH)));
596         }
597 }
598
599
600 static void
601 dissect_opsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
602 {
603         opsi_first = TRUE;
604         /* we need at least 6 bytes to get the payload size ! */
605         tcp_dissect_pdus(tvb, pinfo, tree, opsi_desegment, 6, get_opsi_pdu_len,
606                 dissect_opsi_pdu);
607 }
608
609
610 void
611 proto_register_opsi(void)
612 {
613
614 /* Setup list of header fields  See Section 1.6.1 for details*/
615         static hf_register_info hf[] = {
616                 { &hf_opsi_major_version,
617                         { "Major version",           "opsi.major",
618                         FT_UINT8, BASE_DEC, NULL, 0x0,
619                         NULL, HFILL }
620                 },
621                 { &hf_opsi_minor_version,
622                         { "Minor version",           "opsi.minor",
623                         FT_UINT8, BASE_DEC, NULL, 0x0,
624                         NULL, HFILL }
625                 },
626                 { &hf_opsi_opcode,
627                         { "Operation code",             "opsi.opcode",
628                         FT_UINT8, BASE_DEC, VALS(opsi_opcode), 0x0,
629                         NULL, HFILL }
630                 },
631                 { &hf_opsi_hook_id,
632                         { "Hook ID",                    "opsi.hook",
633                         FT_UINT8, BASE_DEC, NULL, 0x0,
634                         NULL, HFILL }
635                 },
636                 { &hf_opsi_length,
637                         { "Message length",             "opsi.length",
638                         FT_UINT16, BASE_DEC, NULL, 0x0,
639                         NULL, HFILL }
640                 },
641                 { &hf_opsi_session_id,
642                         { "Session ID",                 "opsi.session_id",
643                         FT_UINT16, BASE_DEC, NULL, 0x0,
644                         NULL, HFILL }
645                 },
646                 { &hf_user_name_att,
647                         { "User name",                  "opsi.attr.user_name",
648                         FT_STRING, BASE_NONE, NULL, 0x00,
649                         NULL, HFILL }
650                 },
651                 { &hf_password_att,
652                         { "User password",              "opsi.attr.password",
653                         FT_STRING, BASE_NONE, NULL, 0x00,
654                         NULL, HFILL }
655                 },
656                 { &hf_chap_password_att,
657                         { "CHAP password attribute",    "opsi.attr.chap_password",
658                         FT_STRING, BASE_NONE, NULL, 0x00,
659                         NULL, HFILL }
660                 },
661                 { &hf_nas_ip_add_att,
662                         { "NAS IP address",             "opsi.attr.nas_ip_addr",
663                         FT_IPv4, BASE_NONE, NULL, 0x00,
664                         NULL, HFILL }
665                 },
666                 { &hf_nas_port_att,
667                         { "NAS port",                   "opsi.attr.nas_port",
668                         FT_UINT32, BASE_HEX, NULL, 0x00,
669                         NULL, HFILL }
670                 },
671                 { &hf_service_type_att,
672                         { "Service type",               "opsi.attr.service_type",
673                         FT_UINT32, BASE_DEC, VALS(opsi_service_type_code), 0x0,
674                         NULL, HFILL }
675                 },
676                 { &hf_framed_protocol_att,
677                         { "Framed protocol",            "opsi.attr.framed_protocol",
678                         FT_UINT32, BASE_DEC, VALS(opsi_framed_protocol_code), 0x0,
679                         NULL, HFILL }
680                 },
681                 { &hf_framed_address_att,
682                         { "Framed address",             "opsi.attr.framed_address",
683                         FT_IPv4, BASE_NONE, NULL, 0x00,
684                         NULL, HFILL }
685                 },
686                 { &hf_framed_netmask_att,
687                         { "Framed netmask",             "opsi.attr.framed_netmask",
688                         FT_IPv4, BASE_NONE, NULL, 0x00,
689                         NULL, HFILL }
690                 },
691                 { &hf_framed_routing_att,
692                         { "Framed routing",             "opsi.attr.framed_routing",
693                         FT_UINT32, BASE_DEC, VALS(opsi_framed_routing_code), 0x0,
694                         NULL, HFILL }
695                 },
696                 { &hf_framed_filter_att,
697                         { "Framed filter",              "opsi.attr.framed_filter",
698                         FT_STRING, BASE_NONE, NULL, 0x00,
699                         NULL, HFILL }
700                 },
701                 { &hf_framed_mtu_att,
702                         { "Framed MTU",         "opsi.attr.framed_mtu",
703                         FT_UINT32, BASE_DEC, NULL, 0x00,
704                         NULL, HFILL }
705                 },
706                 { &hf_framed_compression_att,
707                         { "Framed compression",         "opsi.attr.framed_compression",
708                         FT_UINT32, BASE_DEC, VALS(opsi_framed_compression_code), 0x0,
709                         NULL, HFILL }
710                 },
711                 { &hf_called_station_att,
712                         { "Called station ID",          "opsi.attr.called_station_id",
713                         FT_STRING, BASE_NONE, NULL, 0x00,
714                         NULL, HFILL }
715                 },
716                 { &hf_calling_station_att,
717                         { "Calling station ID",         "opsi.attr.calling_station_id",
718                         FT_STRING, BASE_NONE, NULL, 0x00,
719                         NULL, HFILL }
720                 },
721                 { &hf_nas_identifier_att,
722                         { "NAS ID",                     "opsi.attr.nas_id",
723                         FT_STRING, BASE_NONE, NULL, 0x00,
724                         NULL, HFILL }
725                 },
726                 { &hf_accounting_att,
727                         { "Accounting",                 "opsi.attr.accounting",
728                         FT_STRING, BASE_NONE, NULL, 0x00,
729                         NULL, HFILL }
730                 },
731                 { &hf_acct_session_id_att,
732                         { "Accounting session ID",      "opsi.attr.acct.session_id",
733                         FT_STRING, BASE_NONE, NULL, 0x00,
734                         NULL, HFILL }
735                 },
736                 { &hf_chap_challenge_att,
737                         { "CHAP challenge",             "opsi.attr.chap_challenge",
738                         FT_STRING, BASE_NONE, NULL, 0x00,
739                         NULL, HFILL }
740                 },
741                 { &hf_nas_port_type_att,
742                         { "NAS port type",              "opsi.attr.nas_port_type",
743                         FT_UINT32, BASE_DEC, VALS(opsi_nas_port_type_code), 0x0,
744                         NULL, HFILL }
745                 },
746                 { &hf_designation_num_att,
747                         { "Designation number",         "opsi.attr.designation_number",
748                         FT_STRING, BASE_NONE, NULL, 0x00,
749                         NULL, HFILL }
750                 },
751                 { &hf_nas_port_id_att,
752                         { "NAS port ID",                "opsi.attr.nas_port_id",
753                         FT_STRING, BASE_NONE, NULL, 0x00,
754                         NULL, HFILL }
755                 },
756                 { &hf_smc_aaa_id_att,
757                         { "SMC AAA ID",                 "opsi.attr.smc_aaa_id",
758                         FT_UINT32, BASE_DEC, NULL, 0x00,
759                         NULL, HFILL }
760                 },
761                 { &hf_smc_vpn_id_att,
762                         { "SMC VPN ID",                 "opsi.attr.smc_vpn_id",
763                         FT_UINT32, BASE_DEC, NULL, 0x00,
764                         NULL, HFILL }
765                 },
766                 { &hf_smc_vpn_name_att,
767                         { "SMC VPN name",               "opsi.attr.smc_vpn_name",
768                         FT_STRING, BASE_NONE, NULL, 0x00,
769                         NULL, HFILL }
770                 },
771                 { &hf_smc_ran_id_att,
772                         { "SMC RAN ID",                 "opsi.attr.smc_ran_id",
773                         FT_UINT32, BASE_DEC, NULL, 0x00,
774                         NULL, HFILL }
775                 },
776                 { &hf_smc_ran_ip_att,
777                         { "SMC RAN IP address",         "opsi.attr.smc_ran_ip",
778                         FT_IPv4, BASE_NONE, NULL, 0x00,
779                         NULL, HFILL }
780                 },
781                 { &hf_smc_ran_name_att,
782                         { "SMC RAN name",               "opsi.attr.smc_ran_name",
783                         FT_STRING, BASE_NONE, NULL, 0x00,
784                         NULL, HFILL }
785                 },
786                 { &hf_smc_pop_id_att,
787                         { "SMC POP id",                 "opsi.attr.smc_pop_id",
788                         FT_UINT32, BASE_DEC, NULL, 0x00,
789                         NULL, HFILL }
790                 },
791                 { &hf_smc_pop_name_att,
792                         { "SMC POP name",               "opsi.attr.smc_pop_name",
793                         FT_STRING, BASE_NONE, NULL, 0x00,
794                         NULL, HFILL }
795                 },
796                 { &hf_smc_id_att,
797                         { "SMC ID",                     "opsi.attr.smc_id",
798                         FT_UINT32, BASE_DEC, NULL, 0x00,
799                         NULL, HFILL }
800                 },
801                 { &hf_smc_receive_time_att,
802                         { "SMC receive time",           "opsi.attr.smc_receive_time",
803                         FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
804                         NULL, HFILL }
805                 },
806                 { &hf_smc_stat_time_att,
807                         { "SMC stat time",              "opsi.attr.smc_stat_time",
808                         FT_UINT32, BASE_DEC, NULL, 0x00,
809                         NULL, HFILL }
810                 },
811                 { &hf_opsi_flags_att,
812                         { "OPSI flags",                 "opsi.attr.flags",
813                         FT_UINT32, BASE_DEC, NULL, 0x00,
814                         NULL, HFILL }
815                 },
816                 { &hf_opsi_application_name_att,
817                         { "OPSI application name",      "opsi.attr.application_name",
818                         FT_STRING, BASE_NONE, NULL, 0x00,
819                         NULL, HFILL }
820                 },
821         };
822
823 /* Setup protocol subtree array */
824         static gint *ett[] = {
825                 &ett_opsi,
826                 &ett_opsi_user_name,
827                 &ett_opsi_user_password,
828                 &ett_opsi_chap_password,
829                 &ett_opsi_nas_ip_address,
830                 &ett_opsi_nas_port,
831                 &ett_opsi_service_type,
832                 &ett_opsi_framed_protocol,
833                 &ett_opsi_framed_address,
834                 &ett_opsi_framed_netmask,
835                 &ett_opsi_framed_routing,
836                 &ett_opsi_framed_filter,
837                 &ett_opsi_framed_mtu,
838                 &ett_opsi_framed_compression,
839                 &ett_opsi_called_station_id,
840                 &ett_opsi_calling_station_id,
841                 &ett_opsi_nas_identifier,
842                 &ett_opsi_accounting,
843                 &ett_opsi_acct_session_id,
844                 &ett_opsi_chap_challenge,
845                 &ett_opsi_nas_port_type,
846                 &ett_opsi_designation_number,
847                 &ett_opsi_nas_port_id,
848                 &ett_opsi_smc_aaa_id,
849                 &ett_opsi_smc_vpn_id,
850                 &ett_opsi_smc_vpn_name,
851                 &ett_opsi_smc_ran_id,
852                 &ett_opsi_smc_ran_ip,
853                 &ett_opsi_smc_ran_name,
854                 &ett_opsi_smc_pop_id,
855                 &ett_opsi_smc_pop_name,
856                 &ett_opsi_smc_id,
857                 &ett_opsi_smc_receive_time,
858                 &ett_opsi_smc_stat_time,
859                 &ett_opsi_flags,
860                 &ett_opsi_application_name,
861         };
862
863 /* For desegmentation / reassembly */
864         module_t *opsi_module;
865
866 /* Register the protocol name and description */
867         proto_opsi = proto_register_protocol("Open Policy Service Interface",
868             "OPSI", "opsi");
869
870 /* Required function calls to register the header fields and subtrees used */
871         proto_register_field_array(proto_opsi, hf, array_length(hf));
872         proto_register_subtree_array(ett, array_length(ett));
873
874 /* We activate the desegmentation / reassembly feature */
875         opsi_module = prefs_register_protocol(proto_opsi, NULL);
876         prefs_register_bool_preference(opsi_module, "desegment_opsi_messages",
877                 "Reassemble OPSI messages spanning multiple TCP segments",
878                 "Whether the OPSI dissector should desegment all messages spanning multiple TCP segments",
879                 &opsi_desegment);
880 }
881
882
883 void
884 proto_reg_handoff_opsi(void)
885 {
886         dissector_handle_t opsi_handle;
887         opsi_handle = create_dissector_handle(dissect_opsi, proto_opsi);
888         dissector_add_uint("tcp.port", TCP_PORT_OPSI, opsi_handle);
889 }