Don't guard col_set_str (COL_PROTOCOL) with col_check
[obnox/wireshark/wip.git] / epan / dissectors / packet-netbios.c
1 /* packet-netbios.c
2  * Routines for NetBIOS protocol packet disassembly
3  * Jeff Foster <jfoste@woodward.com>
4  * Copyright 1999 Jeffrey C. Foster
5  *
6  * derived from the packet-nbns.c
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/llcsaps.h>
39 #include <epan/reassemble.h>
40 #include <epan/prefs.h>
41 #include "packet-netbios.h"
42
43 /* Netbios command numbers */
44 #define NB_ADD_GROUP            0x00
45 #define NB_ADD_NAME             0x01
46 #define NB_NAME_IN_CONFLICT     0x02
47 #define NB_STATUS_QUERY         0x03
48 #define NB_TERMINATE_TRACE_R    0x07
49 #define NB_DATAGRAM             0x08
50 #define NB_DATAGRAM_BCAST       0x09
51 #define NB_NAME_QUERY           0x0a
52 #define NB_ADD_NAME_RESP        0x0d
53 #define NB_NAME_RESP            0x0e
54 #define NB_STATUS_RESP          0x0f
55 #define NB_TERMINATE_TRACE_LR   0x13
56 #define NB_DATA_ACK             0x14
57 #define NB_DATA_FIRST_MIDDLE    0x15
58 #define NB_DATA_ONLY_LAST       0x16
59 #define NB_SESSION_CONFIRM      0x17
60 #define NB_SESSION_END          0x18
61 #define NB_SESSION_INIT         0x19
62 #define NB_NO_RECEIVE           0x1a
63 #define NB_RECEIVE_OUTSTANDING  0x1b
64 #define NB_RECEIVE_CONTINUE     0x1c
65 #define NB_KEEP_ALIVE           0x1f
66
67 /* Offsets of fields in the NetBIOS header. */
68 #define NB_LENGTH               0
69 #define NB_DELIMITER            2
70 #define NB_COMMAND              4
71 #define NB_FLAGS                5
72 #define NB_DATA1                5
73 #define NB_RESYNC               6
74 #define NB_DATA2                6
75 #define NB_CALL_NAME_TYPE       7
76 #define NB_XMIT_CORL            8
77 #define NB_RESP_CORL            10
78 #define NB_RMT_SES              12
79 #define NB_LOCAL_SES            13
80 #define NB_RECVER_NAME          12
81 #define NB_SENDER_NAME          28
82
83
84 static int proto_netbios = -1;
85 static int hf_netb_cmd = -1;
86 static int hf_netb_hdr_len = -1;
87 static int hf_netb_xmit_corrl = -1;
88 static int hf_netb_resp_corrl = -1;
89 static int hf_netb_call_name_type = -1;
90 static int hf_netb_ack = -1;
91 static int hf_netb_ack_with_data = -1;
92 static int hf_netb_ack_expected = -1;
93 static int hf_netb_recv_cont_req = -1;
94 static int hf_netb_send_no_ack = -1;
95 static int hf_netb_version = -1;
96 static int hf_netb_largest_frame = -1;
97 static int hf_netb_nb_name = -1;
98 static int hf_netb_nb_name_type = -1;
99 static int hf_netb_status_buffer_len = -1;
100 static int hf_netb_status = -1;
101 static int hf_netb_name_type = -1;
102 static int hf_netb_max_data_recv_size = -1;
103 static int hf_netb_termination_indicator = -1;
104 static int hf_netb_num_data_bytes_accepted = -1;
105 static int hf_netb_local_ses_no = -1;
106 static int hf_netb_remote_ses_no = -1;
107 static int hf_netb_data1 = -1;
108 static int hf_netb_data2 = -1;
109 static int hf_netb_fragments = -1;
110 static int hf_netb_fragment = -1;
111 static int hf_netb_fragment_overlap = -1;
112 static int hf_netb_fragment_overlap_conflict = -1;
113 static int hf_netb_fragment_multiple_tails = -1;
114 static int hf_netb_fragment_too_long_fragment = -1;
115 static int hf_netb_fragment_error = -1;
116
117 static gint ett_netb = -1;
118 static gint ett_netb_name = -1;
119 static gint ett_netb_flags = -1;
120 static gint ett_netb_status = -1;
121 static gint ett_netb_fragments = -1;
122 static gint ett_netb_fragment = -1;
123
124 static const fragment_items netbios_frag_items = {
125         &ett_netb_fragment,
126         &ett_netb_fragments,
127         &hf_netb_fragments,
128         &hf_netb_fragment,
129         &hf_netb_fragment_overlap,
130         &hf_netb_fragment_overlap_conflict,
131         &hf_netb_fragment_multiple_tails,
132         &hf_netb_fragment_too_long_fragment,
133         &hf_netb_fragment_error,
134         NULL,
135         "fragments"
136 };
137
138 static dissector_handle_t data_handle;
139
140 /* The strings for the station type, used by get_netbios_name function;
141    many of them came from the file "NetBIOS.txt" in the Zip archive at
142
143         http://www.net3group.com/ftp/browser.zip
144  */
145
146 static const value_string nb_name_type_vals[] = {
147         {0x00,  "Workstation/Redirector"},
148         {0x01,  "Browser"},
149         {0x02,  "Workstation/Redirector"},
150                 /* not sure what 0x02 is, I'm seeing alot of them however */
151                 /* i'm seeing them with workstation/redirection host
152                         announcements */
153         {0x03,  "Messenger service/Main name"},
154         {0x05,  "Forwarded name"},
155         {0x06,  "RAS Server service"},
156         {0x1b,  "Domain Master Browser"},
157         {0x1c,  "Domain Controllers"},
158         {0x1d,  "Local Master Browser"},
159         {0x1e,  "Browser Election Service"},
160         {0x1f,  "Net DDE Service"},
161         {0x20,  "Server service"},
162         {0x21,  "RAS client service"},
163         {0x22,  "Exchange Interchange (MSMail Connector)"},
164         {0x23,  "Exchange Store"},
165         {0x24,  "Exchange Directory"},
166         {0x2b,  "Lotus Notes Server service"},
167         {0x30,  "Modem sharing server service"},
168         {0x31,  "Modem sharing client service"},
169         {0x43,  "SMS Clients Remote Control"},
170         {0x44,  "SMS Administrators Remote Control Tool"},
171         {0x45,  "SMS Clients Remote Chat"},
172         {0x46,  "SMS Clients Remote Transfer"},
173         {0x4c,  "DEC Pathworks TCP/IP Service on Windows NT"},
174         {0x52,  "DEC Pathworks TCP/IP Service on Windows NT"},
175         {0x6a,  "Microsoft Exchange IMC"},
176         {0x87,  "Microsoft Exchange MTA"},
177         {0xbe,  "Network Monitor Agent"},
178         {0xbf,  "Network Monitor Analyzer"},
179         {0x00,  NULL}
180 };
181
182 /* Tables for reassembly of fragments. */
183 static GHashTable *netbios_fragment_table = NULL;
184 static GHashTable *netbios_reassembled_table = NULL;
185
186 /* defragmentation of NetBIOS Frame */
187 static gboolean netbios_defragment = TRUE;
188
189 /* See
190
191         http://www.s390.ibm.com/bookmgr-cgi/bookmgr.cmd/BOOKS/BK8P7001/CCONTENTS
192
193    and
194
195         http://ourworld.compuserve.com/homepages/TimothyDEvans/contents.htm
196
197    for information about the NetBIOS Frame Protocol (which is what this
198    module dissects). */
199
200 /* the strings for the command types  */
201
202 static const value_string cmd_vals[] = {
203         { NB_ADD_GROUP,                 "Add Group Name Query" },
204         { NB_ADD_NAME,                  "Add Name Query" },
205         { NB_NAME_IN_CONFLICT,          "Name In Conflict" },
206         { NB_STATUS_QUERY,              "Status Query" },
207         { NB_TERMINATE_TRACE_R,         "Terminate Trace" },
208         { NB_DATAGRAM,                  "Datagram" },
209         { NB_DATAGRAM_BCAST,            "Broadcast Datagram" },
210         { NB_NAME_QUERY,                "Name Query" },
211         { NB_ADD_NAME_RESP,             "Add Name Response" },
212         { NB_NAME_RESP,                 "Name Recognized" },
213         { NB_STATUS_RESP,               "Status Response" },
214         { NB_TERMINATE_TRACE_LR,        "Terminate Trace" },
215         { NB_DATA_ACK,                  "Data Ack" },
216         { NB_DATA_FIRST_MIDDLE,         "Data First Middle" },
217         { NB_DATA_ONLY_LAST,            "Data Only Last" },
218         { NB_SESSION_CONFIRM,           "Session Confirm" },
219         { NB_SESSION_END,               "Session End" },
220         { NB_SESSION_INIT,              "Session Initialize" },
221         { NB_NO_RECEIVE,                "No Receive" },
222         { NB_RECEIVE_OUTSTANDING,       "Receive Outstanding" },
223         { NB_RECEIVE_CONTINUE,          "Receive Continue" },
224         { NB_KEEP_ALIVE,                "Session Alive" },
225         { 0,                            NULL }
226 };
227
228 static const value_string name_types[] = {
229         { 0, "Unique name" },
230         { 1, "Group name" },
231         { 0, NULL }
232 };
233
234 static const true_false_string flags_allowed = {
235         "Allowed",
236         "Not allowed"
237 };
238
239 static const true_false_string netb_version_str = {
240         "2.00 or higher",
241         "1.xx"
242 };
243
244 static const value_string termination_indicator_vals[] = {
245         { 0x0000, "Normal session end" },
246         { 0x0001, "Abnormal session end" },
247         { 0,      NULL }
248 };
249
250 static const value_string status_vals[] = {
251         { 0, "Add name not in process" },
252         { 1, "Add name in process" },
253         { 0, NULL }
254 };
255
256 static const value_string max_frame_size_vals[] = {
257         { 0,    "516" },
258         { 1,    "1500" },
259         { 2,    "2052" },
260         { 3,    "4472" },
261         { 4,    "8144" },
262         { 5,    "11407" },
263         { 6,    "17800" },      /* 17800 in TR spec, 17749 in NBF spec */
264         { 7,    "65535" },
265         { 0,    NULL }
266 };
267
268
269 void capture_netbios(packet_counts *ld)
270 {
271         ld->netbios++;
272 }
273
274
275 int
276 process_netbios_name(const guchar *name_ptr, char *name_ret, int name_ret_len)
277 {
278         int i;
279         int name_type = *(name_ptr + NETBIOS_NAME_LEN - 1);
280         guchar name_char;
281         static const char hex_digits[16] = "0123456789abcdef";
282
283         for (i = 0; i < NETBIOS_NAME_LEN - 1; i++) {
284                 name_char = *name_ptr++;
285                 if (name_char >= ' ' && name_char <= '~') {
286                         if (--name_ret_len > 0)
287                                 *name_ret++ = name_char;
288                 } else {
289                         /* It's not printable; show it as <XX>, where
290                            XX is the value in hex. */
291                         if (--name_ret_len > 0)
292                                 *name_ret++ = '<';
293                         if (--name_ret_len > 0)
294                                 *name_ret++ = hex_digits[(name_char >> 4)];
295                         if (--name_ret_len > 0)
296                                 *name_ret++ = hex_digits[(name_char & 0x0F)];
297                         if (--name_ret_len > 0)
298                                 *name_ret++ = '>';
299                 }
300         }
301         *name_ret = '\0';
302
303         /* Remove trailing space characters from name. */
304
305         name_ret--;
306
307         for (i = 0; i < NETBIOS_NAME_LEN - 1; i++) {
308                 if (*name_ret != ' ') {
309                         *(name_ret + 1) = 0;
310                         break;
311                 }
312                 name_ret--;
313         }
314
315         return name_type;
316 }
317
318
319 int get_netbios_name( tvbuff_t *tvb, int offset, char *name_ret, int name_ret_len)
320
321 {/*  Extract the name string and name type.  Return the name string in  */
322  /* name_ret and return the name_type. */
323
324         return process_netbios_name( tvb_get_ptr( tvb, offset, NETBIOS_NAME_LEN ), name_ret, name_ret_len);
325 }
326
327
328 /*
329  * Get a string describing the type of a NetBIOS name.
330  */
331 const char *
332 netbios_name_type_descr(int name_type)
333 {
334         return val_to_str(name_type, nb_name_type_vals, "Unknown");
335 }
336
337 void netbios_add_name(const char* label, tvbuff_t *tvb, int offset,
338     proto_tree *tree)
339
340 {/* add a name field display tree. Display the name and station type in sub-tree */
341
342         proto_tree *field_tree;
343         proto_item *tf;
344         char  name_str[(NETBIOS_NAME_LEN - 1)*4 + 1];
345         int   name_type;
346         const char  *name_type_str;
347
348                                         /* decode the name field */
349         name_type = get_netbios_name( tvb, offset, name_str, (NETBIOS_NAME_LEN - 1)*4 + 1);
350         name_type_str = netbios_name_type_descr(name_type);
351         tf = proto_tree_add_text( tree, tvb, offset, NETBIOS_NAME_LEN,
352                 "%s: %s<%02x> (%s)", label, name_str, name_type, name_type_str);
353
354         field_tree = proto_item_add_subtree( tf, ett_netb_name);
355         proto_tree_add_string_format( field_tree, hf_netb_nb_name, tvb, offset,
356                 15, name_str, "%s", name_str);
357         proto_tree_add_uint_format( field_tree, hf_netb_nb_name_type, tvb, offset + 15, 1, name_type,
358             "0x%02x (%s)", name_type, name_type_str);
359 }
360
361
362 static void netbios_data_first_middle_flags( tvbuff_t *tvb, proto_tree *tree, int offset)
363
364 {
365         proto_tree *field_tree;
366         proto_item *tf;
367         guint flags = tvb_get_guint8( tvb, offset);
368
369                 /* decode the flag field for Data First Middle packet*/
370
371         tf = proto_tree_add_text(tree, tvb, offset, 1,
372                         "Flags: 0x%02x", flags);
373         field_tree = proto_item_add_subtree(tf, ett_netb_flags);
374
375         proto_tree_add_boolean( field_tree, hf_netb_ack, tvb, offset, 1, flags);
376
377         proto_tree_add_boolean( field_tree, hf_netb_ack_expected, tvb, offset, 1, flags);
378
379         proto_tree_add_boolean( field_tree, hf_netb_recv_cont_req, tvb, offset, 1, flags);
380 }
381
382 static void netbios_data_only_flags( tvbuff_t *tvb, proto_tree *tree,
383    int offset)
384 {
385         proto_tree *field_tree;
386         proto_item *tf;
387         guint flags = tvb_get_guint8( tvb, offset);
388
389                 /* decode the flag field for Data Only Last packet*/
390
391         tf = proto_tree_add_text(tree, tvb, offset, 1,
392                         "Flags: 0x%02x", flags);
393         field_tree = proto_item_add_subtree(tf, ett_netb_flags);
394
395         proto_tree_add_boolean( field_tree, hf_netb_ack, tvb, offset, 1, flags);
396
397         proto_tree_add_boolean( field_tree, hf_netb_ack_with_data, tvb, offset, 1, flags);
398
399         proto_tree_add_boolean( field_tree, hf_netb_ack_expected, tvb, offset, 1, flags);
400 }
401
402
403
404 static void netbios_add_ses_confirm_flags( tvbuff_t *tvb, proto_tree *tree,
405         int offset)
406 {
407         proto_tree *field_tree;
408         proto_item *tf;
409         guint flags = tvb_get_guint8( tvb, offset);
410
411                 /* decode the flag field for Session Confirm packet */
412         tf = proto_tree_add_text(tree, tvb, offset, 1,
413                         "Flags: 0x%02x", flags);
414         field_tree = proto_item_add_subtree( tf, ett_netb_flags);
415
416         proto_tree_add_boolean( field_tree, hf_netb_send_no_ack, tvb, offset, 1, flags);
417
418         proto_tree_add_boolean( field_tree, hf_netb_version, tvb, offset, 1, flags);
419 }
420
421
422 static void netbios_add_session_init_flags( tvbuff_t *tvb, proto_tree *tree,
423         int offset)
424 {
425         proto_tree *field_tree;
426         proto_item *tf;
427         guint flags = tvb_get_guint8( tvb, offset);
428                 /* decode the flag field for Session Init packet */
429
430         tf = proto_tree_add_text(tree, tvb, offset, 1,
431                         "Flags: 0x%02x", flags);
432         field_tree = proto_item_add_subtree(tf, ett_netb_flags);
433
434         proto_tree_add_boolean( field_tree, hf_netb_send_no_ack, tvb, offset, 1, flags);
435
436         proto_tree_add_uint( field_tree, hf_netb_largest_frame, tvb, offset, 1,
437                 flags);
438
439         proto_tree_add_boolean( field_tree, hf_netb_version, tvb, offset, 1, flags);
440 }
441
442
443 static void netbios_no_receive_flags( tvbuff_t *tvb, proto_tree *tree,
444     int offset)
445
446 {
447         proto_tree *field_tree;
448         proto_item *tf;
449         guint flags = tvb_get_guint8( tvb, offset);
450
451                 /* decode the flag field for No Receive packet*/
452
453         tf = proto_tree_add_text(tree, tvb, offset, 1,
454                         "Flags: 0x%02x", flags);
455
456         if (flags & 0x02) {
457                 field_tree = proto_item_add_subtree(tf, ett_netb_flags);
458                 proto_tree_add_text(field_tree, tvb, offset, 1, "%s",
459                     decode_boolean_bitfield(flags, 0x02, 8,
460                         "SEND.NO.ACK data not received", NULL));
461         }
462 }
463
464
465 /************************************************************************/
466 /*                                                                      */
467 /*  The routines to display the netbios field values in the tree        */
468 /*                                                                      */
469 /************************************************************************/
470
471
472 static void nb_xmit_corrl( tvbuff_t *tvb, int offset, proto_tree *tree)
473
474 {/* display the transmit correlator */
475
476         proto_tree_add_item( tree, hf_netb_xmit_corrl, tvb, offset + NB_XMIT_CORL,
477                 2, TRUE);
478 }
479
480
481 static void nb_resp_corrl( tvbuff_t *tvb, int offset, proto_tree *tree)
482
483 {/* display the response correlator */
484
485         proto_tree_add_item( tree, hf_netb_resp_corrl, tvb, offset + NB_RESP_CORL,
486                 2, TRUE);
487 }
488
489
490 static void nb_call_name_type( tvbuff_t *tvb, int offset, proto_tree *tree)
491
492 {/* display the call name type */
493
494         proto_tree_add_item( tree, hf_netb_call_name_type, tvb, offset + NB_CALL_NAME_TYPE,
495                 1, TRUE);
496
497 }
498
499
500 static guint8 nb_local_session( tvbuff_t *tvb, int offset, proto_tree *tree)
501
502 {/* add the local session to tree, and return its value */
503
504         guint8 local_session = tvb_get_guint8( tvb, offset + NB_LOCAL_SES);
505
506         proto_tree_add_uint( tree, hf_netb_local_ses_no, tvb, offset + NB_LOCAL_SES, 1,
507                 local_session);
508
509         return local_session;
510 }
511
512
513 static guint8 nb_remote_session( tvbuff_t *tvb, int offset, proto_tree *tree)
514
515 {/* add the remote session to tree, and return its value */
516
517         guint8 remote_session = tvb_get_guint8( tvb, offset + NB_RMT_SES);
518
519         proto_tree_add_uint( tree, hf_netb_remote_ses_no, tvb, offset + NB_RMT_SES, 1,
520                 remote_session);
521
522         return remote_session;
523 }
524
525
526 static void nb_data1(int hf, tvbuff_t *tvb, int offset, proto_tree *tree)
527
528 {/* add the DATA1 to tree with specified hf_ value */
529
530         proto_tree_add_item( tree, hf, tvb, offset + NB_DATA1, 1, TRUE);
531
532 }
533
534
535 static void nb_data2(int hf, tvbuff_t *tvb, int offset, proto_tree *tree)
536
537 {/* add the DATA2 to tree with specified hf_ value */
538
539         proto_tree_add_item( tree, hf, tvb, offset + NB_DATA2, 2, TRUE);
540
541 }
542
543
544 static void nb_resync_indicator( tvbuff_t *tvb, int offset, proto_tree *tree, const char *cmd_str)
545 {
546         guint16 resync_indicator = tvb_get_letohs( tvb, offset + NB_DATA2);
547
548
549         switch (resync_indicator) {
550
551         case 0x0000:
552                 proto_tree_add_text(tree, tvb, offset + NB_DATA2, 2,
553                     "Re-sync indicator: No re-sync");
554                 break;
555
556         case 0x0001:
557                 proto_tree_add_text(tree, tvb, offset + NB_DATA2, 2,
558                     "Re-sync indicator: First '%s' following 'Receive Outstanding'", cmd_str);
559                 break;
560
561         default:
562                 proto_tree_add_text(tree, tvb, offset + NB_DATA2, 2,
563                     "Re-sync indicator: 0x%04x", resync_indicator);
564                 break;
565         }
566 }
567
568 /************************************************************************/
569 /*                                                                      */
570 /*  The routines called by the top level to handle individual commands  */
571 /*                                                                      */
572 /************************************************************************/
573
574 static guint32
575 dissect_netb_unknown( tvbuff_t *tvb, int offset, proto_tree *tree)
576
577 {/* Handle any unknown commands, do nothing */
578
579         proto_tree_add_text(tree, tvb, offset + NB_COMMAND + 1, -1,
580             "Unknown NetBIOS command data");
581
582         return 0;
583 }
584
585
586 static guint32
587 dissect_netb_add_group_name( tvbuff_t *tvb, int offset, proto_tree *tree)
588
589 {/* Handle the ADD GROUP NAME QUERY command */
590
591         nb_resp_corrl( tvb, offset, tree);
592
593         netbios_add_name("Group name to add", tvb, offset + NB_SENDER_NAME,
594             tree);
595
596         return 0;
597 }
598
599
600 static guint32
601 dissect_netb_add_name( tvbuff_t *tvb, int offset, proto_tree *tree)
602
603 {/* Handle the ADD NAME QUERY command */
604
605         nb_resp_corrl( tvb, offset, tree);
606
607         netbios_add_name("Name to add", tvb, offset + NB_SENDER_NAME, tree);
608
609         return 0;
610 }
611
612
613 static guint32
614 dissect_netb_name_in_conflict( tvbuff_t *tvb, int offset, proto_tree *tree)
615
616 {/* Handle the NAME IN CONFLICT command */
617
618         netbios_add_name("Name In Conflict", tvb, offset + NB_RECVER_NAME,
619             tree);
620         netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME, tree);
621
622         return 0;
623 }
624
625
626 static guint32
627 dissect_netb_status_query( tvbuff_t *tvb, int offset, proto_tree *tree)
628
629 {/* Handle the STATUS QUERY command */
630
631         guint8 status_request = tvb_get_guint8( tvb, offset + NB_DATA1);
632
633         switch (status_request) {
634
635         case 0:
636                 proto_tree_add_text(tree, tvb, offset + NB_DATA1, 1,
637                     "Status request: NetBIOS 1.x or 2.0");
638                 break;
639
640         case 1:
641                 proto_tree_add_text(tree, tvb, offset + NB_DATA1, 1,
642                     "Status request: NetBIOS 2.1, initial status request");
643                 break;
644
645         default:
646                 proto_tree_add_text(tree, tvb, offset + NB_DATA1, 1,
647                     "Status request: NetBIOS 2.1, %u names received so far",
648                     status_request);
649                 break;
650         }
651         nb_data2( hf_netb_status_buffer_len, tvb, offset, tree);
652         nb_resp_corrl( tvb, offset, tree);
653         netbios_add_name("Receiver's Name", tvb, offset + NB_RECVER_NAME, tree);
654         netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME, tree);
655
656         return 0;
657 }
658
659
660 static guint32
661 dissect_netb_terminate_trace( tvbuff_t *tvb _U_, int offset _U_, proto_tree *tree _U_)
662
663 {/* Handle the TERMINATE TRACE command */
664
665         /*
666          * XXX - are any of the fields in this message significant?
667          * The IBM NetBIOS document shows them as "Reserved".
668          */
669
670         return 0;
671 }
672
673
674 static guchar zeroes[10];
675
676 static guint32
677 dissect_netb_datagram( tvbuff_t *tvb, int offset, proto_tree *tree)
678
679 {/* Handle the DATAGRAM command */
680
681         netbios_add_name("Receiver's Name", tvb, offset + NB_RECVER_NAME, tree);
682         /* Weird.  In some datagrams, this is 10 octets of 0, followed
683            by a MAC address.... */
684
685         if (memcmp( tvb_get_ptr( tvb,offset + NB_SENDER_NAME, 10), zeroes, 10) == 0) {
686                 proto_tree_add_text( tree, tvb, offset + NB_SENDER_NAME + 10, 6,
687                     "Sender's MAC Address: %s",
688                     ether_to_str( tvb_get_ptr( tvb,offset + NB_SENDER_NAME + 10, 6)));
689         } else {
690                 netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME,
691                     tree);
692         }
693
694         return 0;
695 }
696
697
698 static guint32
699 dissect_netb_datagram_bcast( tvbuff_t *tvb, int offset, proto_tree *tree)
700
701 {/* Handle the DATAGRAM BROADCAST command */
702
703         /* We assume the same weirdness can happen here.... */
704         if ( memcmp( tvb_get_ptr( tvb,offset + NB_SENDER_NAME + 10, 6), zeroes, 10) == 0) {
705                 proto_tree_add_text( tree, tvb, offset + NB_SENDER_NAME + 10, 6,
706                     "Sender's Node Address: %s",
707                     ether_to_str( tvb_get_ptr( tvb,offset + NB_SENDER_NAME + 10, 6)));
708         } else {
709                 netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME,
710                     tree);
711         }
712
713         return 0;
714 }
715
716
717 static guint32
718 dissect_netb_name_query( tvbuff_t *tvb, int offset, proto_tree *tree)
719
720 {/* Handle the NAME QUERY command */
721         guint8 local_session_number = tvb_get_guint8( tvb, offset + NB_DATA2);
722
723         if (local_session_number == 0) {
724                 proto_tree_add_text( tree, tvb, offset + NB_DATA2, 1,
725                     "Local Session No.: 0 (FIND.NAME request)");
726         } else {
727                 proto_tree_add_text( tree, tvb, offset + NB_DATA2, 1,
728                     "Local Session No.: 0x%02x", local_session_number);
729         }
730         nb_call_name_type( tvb, offset, tree);
731         nb_resp_corrl( tvb, offset, tree);
732         netbios_add_name("Query Name", tvb, offset + NB_RECVER_NAME, tree);
733         if (local_session_number != 0) {
734                 netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME,
735                     tree);
736         }
737
738         return 0;
739 }
740
741
742 static guint32
743 dissect_netb_add_name_resp( tvbuff_t *tvb, int offset, proto_tree *tree)
744
745 {/* Handle the ADD NAME RESPONSE command */
746
747         nb_data1( hf_netb_status, tvb, offset, tree);
748         nb_data2( hf_netb_name_type, tvb, offset, tree);
749         nb_xmit_corrl( tvb, offset, tree);
750         netbios_add_name("Name to be added", tvb, offset + NB_RECVER_NAME,
751             tree);
752         netbios_add_name("Name to be added", tvb, offset + NB_SENDER_NAME,
753             tree);
754
755         return 0;
756 }
757
758
759 static guint32
760 dissect_netb_name_resp( tvbuff_t *tvb, int offset, proto_tree *tree)
761
762 {/* Handle the NAME RECOGNIZED command */
763         guint8 local_session_number = tvb_get_guint8( tvb, offset + NB_DATA2);
764
765         switch (local_session_number) {
766
767         case 0x00:
768                 proto_tree_add_text( tree, tvb, offset + NB_DATA2, 1,
769                     "State of name: No LISTEN pending, or FIND.NAME response");
770                 break;
771
772         case 0xFF:
773                 proto_tree_add_text( tree, tvb, offset + NB_DATA2, 1,
774                     "State of name: LISTEN pending, but insufficient resources to establish session");
775                 break;
776
777         default:
778                 proto_tree_add_text( tree, tvb, offset + NB_DATA2, 1,
779                     "Local Session No.: 0x%02x", local_session_number);
780                 break;
781         }
782         nb_call_name_type( tvb, offset, tree);
783         nb_xmit_corrl( tvb, offset, tree);
784         if (local_session_number != 0x00 && local_session_number != 0xFF)
785                 nb_resp_corrl(tvb, offset, tree);
786         netbios_add_name("Receiver's Name", tvb, offset + NB_RECVER_NAME, tree);
787         if (local_session_number != 0x00 && local_session_number != 0xFF) {
788                 netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME,
789                     tree);
790         }
791
792         return 0;
793 }
794
795
796 static guint32
797 dissect_netb_status_resp( tvbuff_t *tvb, int offset, proto_tree *tree)
798
799 {/* Handle the STATUS RESPONSE command */
800         guint8 status_response = tvb_get_guint8( tvb, offset + NB_DATA1);
801         proto_item *td2;
802         proto_tree *data2_tree;
803         guint16 data2;
804
805         nb_call_name_type( tvb, offset, tree);
806         if (status_response == 0) {
807                 proto_tree_add_text(tree, tvb, offset + NB_DATA1, 1,
808                     "Status response: NetBIOS 1.x or 2.0");
809         } else {
810                 proto_tree_add_text(tree, tvb, offset + NB_DATA1, 1,
811                     "Status response: NetBIOS 2.1, %u names sent so far",
812                     status_response);
813         }
814         data2 = tvb_get_letohs( tvb, offset + NB_DATA2);
815
816         td2 = proto_tree_add_text(tree, tvb, offset + NB_DATA2, 2, "Status: 0x%04x",
817             data2);
818         data2_tree = proto_item_add_subtree(td2, ett_netb_status);
819         if (data2 & 0x8000) {
820                 proto_tree_add_text(data2_tree, tvb, offset, 2, "%s",
821                     decode_boolean_bitfield(data2, 0x8000, 8*2,
822                         "Data length exceeds maximum frame size", NULL));
823         }
824         if (data2 & 0x4000) {
825                 proto_tree_add_text(data2_tree, tvb, offset, 2, "%s",
826                     decode_boolean_bitfield(data2, 0x4000, 8*2,
827                         "Data length exceeds user's buffer", NULL));
828         }
829         proto_tree_add_text(data2_tree, tvb, offset, 2, "%s",
830             decode_numeric_bitfield(data2, 0x3FFF, 2*8,
831                         "Status data length = %u"));
832         nb_xmit_corrl( tvb, offset, tree);
833         netbios_add_name("Receiver's Name", tvb, offset + NB_RECVER_NAME, tree);
834         netbios_add_name("Sender's Name", tvb, offset + NB_SENDER_NAME,
835             tree);
836
837         return 0;
838 }
839
840
841 static guint32
842 dissect_netb_data_ack( tvbuff_t* tvb, int offset, proto_tree *tree)
843
844 {/* Handle the DATA ACK command */
845
846         nb_xmit_corrl( tvb, offset, tree);
847         nb_remote_session( tvb, offset, tree);
848         nb_local_session( tvb, offset, tree);
849
850         return 0;
851 }
852
853
854 static guint32
855 dissect_netb_data_first_middle( tvbuff_t *tvb, int offset, proto_tree *tree)
856
857 {/* Handle the DATA FIRST MIDDLE command */
858
859         guint8 remote_session, local_session;
860
861         /*
862          * This is the first frame, or the middle frame, of a fragmented
863          * packet.
864          *
865          * XXX - there are no sequence numbers, so we have to assume
866          * that fragments arrive in order with no duplicates.
867          * In fact, 802.2 LLC is supposed to handle that, so we
868          * might have to have the LLC dissector do so (but the TCP
869          * dissector doesn't currently handle out-of-order or duplicate
870          * data, either).
871          */
872
873         netbios_data_first_middle_flags( tvb, tree, offset + NB_FLAGS);
874
875         nb_resync_indicator( tvb, offset, tree, "DATA FIRST MIDDLE");
876         nb_xmit_corrl( tvb, offset, tree);
877         nb_resp_corrl( tvb, offset, tree);
878         remote_session = nb_remote_session( tvb, offset, tree);
879         local_session = nb_local_session( tvb, offset, tree);
880
881         /*
882          * Return a combination of the remote and local session numbers,
883          * for use when reassembling.
884          */
885         return (remote_session << 8) + local_session;
886 }
887
888
889 static guint32
890 dissect_netb_data_only_last( tvbuff_t *tvb, int offset, proto_tree *tree)
891
892 {/* Handle the DATA ONLY LAST command */
893
894         guint8 remote_session, local_session;
895
896         /*
897          * This is a complete packet, or the last frame of a fragmented
898          * packet.
899          */
900
901         netbios_data_only_flags( tvb, tree, offset + NB_FLAGS);
902
903         nb_resync_indicator( tvb, offset, tree, "DATA ONLY LAST");
904         nb_xmit_corrl( tvb, offset, tree);
905         nb_resp_corrl( tvb, offset, tree);
906         remote_session = nb_remote_session( tvb, offset, tree);
907         local_session = nb_local_session( tvb, offset, tree);
908
909         /*
910          * Return a combination of the remote and local session numbers,
911          * for use when reassembling.
912          */
913         return (remote_session << 8) + local_session;
914 }
915
916
917 static guint32
918 dissect_netb_session_confirm( tvbuff_t *tvb, int offset, proto_tree *tree)
919
920 {/* Handle the SESSION CONFIRM command */
921
922         netbios_add_ses_confirm_flags( tvb, tree, offset + NB_FLAGS);
923
924         nb_data2( hf_netb_max_data_recv_size, tvb, offset, tree);
925         nb_xmit_corrl( tvb, offset, tree);
926         nb_resp_corrl( tvb, offset, tree);
927         nb_remote_session( tvb, offset, tree);
928         nb_local_session( tvb, offset, tree);
929
930         return 0;
931 }
932
933
934 static guint32
935 dissect_netb_session_end( tvbuff_t *tvb, int offset, proto_tree *tree)
936
937 {/* Handle the SESSION END command */
938
939         nb_data2( hf_netb_termination_indicator, tvb, offset, tree);
940         nb_remote_session( tvb, offset, tree);
941         nb_local_session( tvb, offset, tree);
942
943         return 0;
944 }
945
946
947 static guint32
948 dissect_netb_session_init( tvbuff_t *tvb, int offset, proto_tree *tree)
949
950 {/* Handle the SESSION INITIALIZE command */
951
952         netbios_add_session_init_flags( tvb, tree, offset + NB_FLAGS);
953
954         nb_data2( hf_netb_max_data_recv_size, tvb, offset, tree);
955         nb_resp_corrl( tvb, offset, tree);
956         nb_xmit_corrl( tvb, offset, tree);
957         nb_remote_session( tvb, offset, tree);
958         nb_local_session( tvb, offset, tree);
959
960         return 0;
961 }
962
963 static guint32
964 dissect_netb_no_receive( tvbuff_t *tvb, int offset, proto_tree *tree)
965
966 {/* Handle the NO RECEIVE command */
967
968         netbios_no_receive_flags( tvb, tree, offset + NB_FLAGS);
969
970         nb_data2( hf_netb_num_data_bytes_accepted, tvb, offset, tree);
971         nb_remote_session( tvb, offset, tree);
972         nb_local_session( tvb, offset, tree);
973
974         return 0;
975 }
976
977
978 static guint32
979 dissect_netb_receive_outstanding( tvbuff_t *tvb, int offset, proto_tree *tree)
980
981 {/* Handle the RECEIVE OUTSTANDING command */
982
983         nb_data2( hf_netb_num_data_bytes_accepted, tvb, offset, tree);
984         nb_remote_session( tvb, offset, tree);
985         nb_local_session( tvb, offset, tree);
986
987         return 0;
988 }
989
990
991 static guint32
992 dissect_netb_receive_continue( tvbuff_t *tvb, int offset, proto_tree *tree)
993
994 {/* Handle the RECEIVE CONTINUE command */
995
996         nb_xmit_corrl( tvb, offset, tree);
997         nb_remote_session( tvb, offset, tree);
998         nb_local_session( tvb, offset, tree);
999
1000         return 0;
1001 }
1002
1003
1004 static guint32
1005 dissect_netb_session_alive( tvbuff_t *tvb, int offset, proto_tree *tree)
1006
1007 {/* Handle the SESSION ALIVE command */
1008
1009         /*
1010          * XXX - all the fields are claimed to be "Reserved", but
1011          * the session numbers appear to be non-zero in at least
1012          * one capture, and they do appear to match session numbers
1013          * in other messages, and I'd expect that you had to identify
1014          * sessions in this message in any case.
1015          *
1016          * We show only those fields.
1017          */
1018         nb_remote_session( tvb, offset, tree);
1019         nb_local_session( tvb, offset, tree);
1020
1021         return 0;
1022 }
1023
1024
1025 /************************************************************************/
1026 /*                                                                      */
1027 /*  The table routines called by the top level to handle commands       */
1028 /*                                                                      */
1029 /************************************************************************/
1030
1031
1032 static guint32 (*dissect_netb[])(tvbuff_t *, int, proto_tree *) = {
1033
1034   dissect_netb_add_group_name,  /* Add Group Name       0x00 */
1035   dissect_netb_add_name,        /* Add Name             0x01 */
1036   dissect_netb_name_in_conflict,/* Name In Conflict     0x02 */
1037   dissect_netb_status_query,    /* Status Query         0x03 */
1038   dissect_netb_unknown,         /* unknown              0x04 */
1039   dissect_netb_unknown,         /* unknown              0x05 */
1040   dissect_netb_unknown,         /* unknown              0x06 */
1041   dissect_netb_terminate_trace, /* Terminate Trace      0x07 */
1042   dissect_netb_datagram,        /* Datagram             0x08 */
1043   dissect_netb_datagram_bcast,  /* Datagram Broadcast   0x09 */
1044   dissect_netb_name_query,      /* Name Query           0x0A */
1045   dissect_netb_unknown,         /* unknown              0x0B */
1046   dissect_netb_unknown,         /* unknown              0x0C */
1047   dissect_netb_add_name_resp,   /* Add Name Response    0x0D */
1048   dissect_netb_name_resp,       /* Name Recognized      0x0E */
1049   dissect_netb_status_resp,     /* Status Response      0x0F */
1050   dissect_netb_unknown,         /* unknown              0x10 */
1051   dissect_netb_unknown,         /* unknown              0x11 */
1052   dissect_netb_unknown,         /* unknown              0x12 */
1053   dissect_netb_terminate_trace, /* Terminate Trace      0x13 */
1054   dissect_netb_data_ack,        /* Data Ack             0x14 */
1055   dissect_netb_data_first_middle,/* Data First Middle   0x15 */
1056   dissect_netb_data_only_last,  /* Data Only Last       0x16 */
1057   dissect_netb_session_confirm, /* Session Confirm      0x17 */
1058   dissect_netb_session_end,     /* Session End          0x18 */
1059   dissect_netb_session_init,    /* Session Initialize   0x19 */
1060   dissect_netb_no_receive,      /* No Receive           0x1A */
1061   dissect_netb_receive_outstanding,/* Receive Outstanding 0x1B */
1062   dissect_netb_receive_continue,/* Receive Continue     0x1C */
1063   dissect_netb_unknown,         /* unknown              0x1D */
1064   dissect_netb_unknown,         /* unknown              0x1E */
1065   dissect_netb_session_alive,   /* Session Alive        0x1f */
1066   dissect_netb_unknown,
1067 };
1068
1069 static heur_dissector_list_t netbios_heur_subdissector_list;
1070
1071 void
1072 dissect_netbios_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1073 {
1074         /*
1075          * Try the heuristic dissectors for NetBIOS; if none of them
1076          * accept the packet, dissect it as data.
1077          */
1078         if (!dissector_try_heuristic(netbios_heur_subdissector_list,
1079                                     tvb, pinfo, tree))
1080                 call_dissector(data_handle,tvb, pinfo, tree);
1081 }
1082
1083 static void
1084 dissect_netbios(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1085
1086 {
1087         proto_tree              *netb_tree = NULL;
1088         proto_item              *ti;
1089         guint16                 hdr_len, command;
1090         const char              *command_name;
1091         char                    name[(NETBIOS_NAME_LEN - 1)*4 + 1];
1092         int                     name_type;
1093         guint16                 session_id;
1094         gboolean                save_fragmented;
1095         int                     len;
1096         fragment_data           *fd_head;
1097         tvbuff_t                *next_tvb;
1098
1099         int offset = 0;
1100
1101                                         /* load the display labels      */
1102         col_set_str(pinfo->cinfo, COL_PROTOCOL, "NetBIOS");
1103
1104
1105 /* Find NetBIOS marker EFFF, this is done because I have seen an extra LLC */
1106 /* byte on our network. This only checks for one extra LLC byte. */
1107
1108         if ( 0xefff != tvb_get_letohs(tvb, 2)){
1109                 ++offset;
1110                 if ( 0xefff != tvb_get_letohs(tvb, 3)){
1111
1112                         if (check_col( pinfo->cinfo, COL_INFO))         /* print bad packet */
1113                                 col_set_str( pinfo->cinfo, COL_INFO, "Bad packet, no 0xEFFF marker");
1114
1115                         return;         /* this is an unknown packet, no marker */
1116                 }
1117         }
1118
1119
1120         hdr_len = tvb_get_letohs(tvb, offset + NB_LENGTH);
1121         command = tvb_get_guint8( tvb, offset + NB_COMMAND);
1122                                         /* limit command so no table overflows */
1123         command = MIN( command, sizeof( dissect_netb)/ sizeof(void *));
1124
1125         if (check_col( pinfo->cinfo, COL_INFO)) {              /* print command name */
1126                 command_name = val_to_str(command, cmd_vals, "Unknown (0x%02x)");
1127                 switch ( command ) {
1128                 case NB_NAME_QUERY:
1129                         name_type = get_netbios_name( tvb, offset + 12, name, (NETBIOS_NAME_LEN - 1)*4 + 1);
1130                         col_add_fstr( pinfo->cinfo, COL_INFO, "%s for %s<%02x>",
1131                             command_name, name, name_type);
1132                         break;
1133
1134                 case NB_NAME_RESP:
1135                 case NB_ADD_NAME:
1136                 case NB_ADD_GROUP:
1137                         name_type = get_netbios_name( tvb, offset + 28, name, (NETBIOS_NAME_LEN - 1)*4 + 1);
1138                         col_add_fstr( pinfo->cinfo, COL_INFO, "%s - %s<%02x>",
1139                             command_name, name, name_type);
1140                         break;
1141
1142                 default:
1143                         col_add_str( pinfo->cinfo, COL_INFO, 
1144                             command_name);
1145                         break;
1146                 }
1147         }
1148
1149         if ( tree) {
1150                 ti = proto_tree_add_item(tree, proto_netbios, tvb, 0, hdr_len, FALSE);
1151                 netb_tree = proto_item_add_subtree(ti, ett_netb);
1152
1153                 proto_tree_add_uint_format(netb_tree, hf_netb_hdr_len, tvb, offset, 2, hdr_len,
1154                         "Length: %d bytes", hdr_len);
1155
1156                 proto_tree_add_text(netb_tree, tvb, offset + 2, 2,
1157                         "Delimiter: EFFF (NetBIOS)");
1158
1159                 proto_tree_add_uint(netb_tree, hf_netb_cmd, tvb, offset + NB_COMMAND, 1, command);
1160         }
1161
1162                                         /* if command in table range */
1163         if ( command < sizeof( dissect_netb)/ sizeof(void *)) {
1164
1165                                         /* branch to handle commands */
1166                 session_id = (dissect_netb[ command])( tvb, offset, netb_tree);
1167
1168                 offset += hdr_len;                      /* move past header */
1169
1170                 save_fragmented = pinfo->fragmented;
1171
1172                 /*
1173                  * Process user data in frames that have it.
1174                  */
1175                 switch (command) {
1176
1177                 case NB_DATAGRAM:
1178                 case NB_DATAGRAM_BCAST:
1179                         /*
1180                          * No fragmentation here.
1181                          */
1182                         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1183                         dissect_netbios_payload(next_tvb, pinfo, tree);
1184                         break;
1185
1186                 case NB_DATA_FIRST_MIDDLE:
1187                 case NB_DATA_ONLY_LAST:
1188                         /*
1189                          * Possibly fragmented.
1190                          */
1191                         len = tvb_reported_length_remaining(tvb, offset);
1192                         if (netbios_defragment &&
1193                             tvb_bytes_exist(tvb, offset, len)) {
1194                                 fd_head = fragment_add_seq_next(tvb, offset,
1195                                     pinfo, session_id,
1196                                     netbios_fragment_table,
1197                                     netbios_reassembled_table,
1198                                     len, command == NB_DATA_FIRST_MIDDLE);
1199                                 if (fd_head != NULL) {
1200                                         if (fd_head->next != NULL) {
1201                                                 next_tvb = tvb_new_child_real_data(tvb, fd_head->data,
1202                                                     fd_head->len, fd_head->len);
1203                                                 add_new_data_source(pinfo,
1204                                                     next_tvb,
1205                                                     "Reassembled NetBIOS");
1206                                                 /* Show all fragments. */
1207                                                 if (tree) {
1208                                                         proto_item *frag_tree_item;
1209
1210                                                         show_fragment_seq_tree(fd_head,
1211                                                             &netbios_frag_items,
1212                                                             netb_tree, pinfo,
1213                                                             next_tvb, &frag_tree_item);
1214                                                 }
1215                                         } else {
1216                                                 next_tvb = tvb_new_subset(tvb,
1217                                                     offset, -1, -1);
1218                                         }
1219                                 } else {
1220                                         next_tvb = NULL;
1221                                 }
1222                         } else {
1223                                 /*
1224                                  * Dissect this, regardless of whether
1225                                  * it's NB_DATA_FIRST_MIDDLE or
1226                                  * NB_DATA_ONLY_LAST.
1227                                  *
1228                                  * XXX - it'd be nice to show
1229                                  * NB_DATA_FIRST_MIDDLE as a fragment
1230                                  * if it's not the first fragment (i.e.,
1231                                  * MIDDLE rather than FIRST), and show
1232                                  * NB_DATA_ONLY_LAST as a fragment if
1233                                  * it's part of a fragmented datagram
1234                                  * (i.e, LAST rather than ONLY), but
1235                                  * we'd have to do reassembly to
1236                                  * be able to determine that.
1237                                  */
1238                                 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1239                         }
1240                         if (next_tvb != NULL)
1241                                 dissect_netbios_payload(next_tvb, pinfo, tree);
1242                         else {
1243                                 next_tvb = tvb_new_subset (tvb, offset, -1, -1);
1244                                 call_dissector(data_handle, next_tvb, pinfo,
1245                                     tree);
1246                         }
1247                         break;
1248                 }
1249         }
1250 }
1251
1252 static void
1253 netbios_init(void)
1254 {
1255         /*
1256          * Initialize the fragment and reassembly tables.
1257          */
1258         fragment_table_init(&netbios_fragment_table);
1259         reassembled_table_init(&netbios_reassembled_table);
1260 }
1261
1262 void proto_register_netbios(void)
1263 {
1264         static gint *ett[] = {
1265                 &ett_netb,
1266                 &ett_netb_name,
1267                 &ett_netb_flags,
1268                 &ett_netb_status,
1269                 &ett_netb_fragments,
1270                 &ett_netb_fragment,
1271         };
1272
1273         static hf_register_info hf_netb[] = {
1274                 { &hf_netb_cmd,
1275                 { "Command", "netbios.command", FT_UINT8, BASE_HEX, VALS(cmd_vals), 0x0,
1276                         NULL, HFILL }},
1277
1278                 { &hf_netb_hdr_len,
1279                 { "Header Length", "netbios.hdr_len", FT_UINT16, BASE_DEC, NULL, 0x0,
1280                         NULL, HFILL }},
1281
1282                 { &hf_netb_xmit_corrl,
1283                 { "Transmit Correlator", "netbios.xmit_corrl", FT_UINT16, BASE_HEX, NULL, 0x0,
1284                         NULL, HFILL }},
1285
1286                 { &hf_netb_resp_corrl,
1287                 { "Response Correlator", "netbios.resp_corrl", FT_UINT16, BASE_HEX, NULL, 0x0,
1288                         NULL, HFILL }},
1289
1290                 { &hf_netb_call_name_type,
1291                 { "Caller's Name Type", "netbios.call_name_type", FT_UINT8, BASE_HEX, VALS(name_types), 0x0,
1292                         NULL, HFILL }},
1293
1294                 { &hf_netb_nb_name_type,
1295                 { "NetBIOS Name Type", "netbios.nb_name_type", FT_UINT8, BASE_HEX, VALS(nb_name_type_vals), 0x0,
1296                         NULL, HFILL }},
1297
1298                 { &hf_netb_nb_name,
1299                 { "NetBIOS Name", "netbios.nb_name", FT_STRING, BASE_NONE, NULL, 0x0,
1300                         NULL, HFILL }},
1301
1302                 { &hf_netb_ack,
1303                 { "Acknowledge", "netbios.ack", FT_BOOLEAN, 8, TFS( &tfs_set_notset), 0x08,
1304                         NULL, HFILL }},
1305
1306                 { &hf_netb_ack_with_data,
1307                 { "Acknowledge with data", "netbios.ack_with_data", FT_BOOLEAN, 8, TFS( &flags_allowed), 0x04,
1308                         NULL, HFILL }},
1309
1310                 { &hf_netb_ack_expected,
1311                 { "Acknowledge expected", "netbios.ack_expected", FT_BOOLEAN,  8,
1312                         TFS( &tfs_yes_no), 0x02, NULL, HFILL }},
1313
1314                 { &hf_netb_recv_cont_req,
1315                 { "RECEIVE_CONTINUE requested", "netbios.recv_cont_req", FT_BOOLEAN,  8,
1316                         TFS( &tfs_yes_no), 0x01, NULL, HFILL }},
1317
1318                 { &hf_netb_send_no_ack,
1319                 { "Handle SEND.NO.ACK", "netbios.send_no_ack", FT_BOOLEAN,  8,
1320                         TFS( &tfs_yes_no), 0x80, NULL, HFILL }},
1321
1322                 { &hf_netb_version,
1323                 { "NetBIOS Version", "netbios.version", FT_BOOLEAN,  8,
1324                         TFS( &netb_version_str), 0x01, NULL, HFILL }},
1325
1326                 { &hf_netb_largest_frame,
1327                 { "Largest Frame", "netbios.largest_frame", FT_UINT8, BASE_DEC, VALS(max_frame_size_vals), 0x0E,
1328                         NULL, HFILL }},
1329
1330                 { &hf_netb_status_buffer_len,
1331                 { "Length of status buffer", "netbios.status_buffer_len", FT_UINT16, BASE_DEC, NULL, 0x0,
1332                         NULL, HFILL }},
1333
1334                 { &hf_netb_status,
1335                 { "Status", "netbios.status", FT_UINT8, BASE_DEC, VALS(status_vals), 0x0,
1336                         NULL, HFILL }},
1337
1338                 { &hf_netb_name_type,
1339                 { "Name type", "netbios.name_type", FT_UINT16, BASE_DEC, VALS(name_types), 0x0,
1340                         NULL, HFILL }},
1341
1342                 { &hf_netb_max_data_recv_size,
1343                 { "Maximum data receive size", "netbios.max_data_recv_size", FT_UINT16, BASE_DEC, NULL, 0x0,
1344                         NULL, HFILL }},
1345
1346                 { &hf_netb_termination_indicator,
1347                 { "Termination indicator", "netbios.termination_indicator", FT_UINT16, BASE_HEX, VALS(termination_indicator_vals), 0x0,
1348                         NULL, HFILL }},
1349
1350                 { &hf_netb_num_data_bytes_accepted,
1351                 { "Number of data bytes accepted", "netbios.num_data_bytes_accepted", FT_UINT16, BASE_DEC, NULL, 0x0,
1352                         NULL, HFILL }},
1353
1354                 { &hf_netb_local_ses_no,
1355                 { "Local Session No.", "netbios.local_session", FT_UINT8, BASE_HEX, NULL, 0x0,
1356                         NULL, HFILL }},
1357
1358                 { &hf_netb_remote_ses_no,
1359                 { "Remote Session No.", "netbios.remote_session", FT_UINT8, BASE_HEX, NULL, 0x0,
1360                         NULL, HFILL }},
1361
1362                 { &hf_netb_data1,
1363                 { "DATA1 value", "netbios.data1", FT_UINT8, BASE_HEX, NULL, 0x0,
1364                         NULL, HFILL }},
1365
1366                 { &hf_netb_data2,
1367                 { "DATA2 value", "netbios.data2", FT_UINT16, BASE_HEX, NULL, 0x0,
1368                         NULL, HFILL }},
1369
1370                 { &hf_netb_fragment_overlap,
1371                 { "Fragment overlap",   "netbios.fragment.overlap", FT_BOOLEAN, BASE_NONE,
1372                         NULL, 0x0, "Fragment overlaps with other fragments", HFILL }},
1373
1374                 { &hf_netb_fragment_overlap_conflict,
1375                 { "Conflicting data in fragment overlap", "netbios.fragment.overlap.conflict",
1376                         FT_BOOLEAN, BASE_NONE,
1377                         NULL, 0x0, "Overlapping fragments contained conflicting data", HFILL }},
1378
1379                 { &hf_netb_fragment_multiple_tails,
1380                 { "Multiple tail fragments found", "netbios.fragment.multipletails",
1381                         FT_BOOLEAN, BASE_NONE,
1382                         NULL, 0x0, "Several tails were found when defragmenting the packet", HFILL }},
1383
1384                 { &hf_netb_fragment_too_long_fragment,
1385                 { "Fragment too long",  "netbios.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE,
1386                         NULL, 0x0, "Fragment contained data past end of packet", HFILL }},
1387
1388                 { &hf_netb_fragment_error,
1389                 {"Defragmentation error",       "netbios.fragment.error", FT_FRAMENUM, BASE_NONE,
1390                         NULL, 0x0, "Defragmentation error due to illegal fragments", HFILL }},
1391
1392                 { &hf_netb_fragment,
1393                 { "NetBIOS Fragment",           "netbios.fragment", FT_FRAMENUM, BASE_NONE,
1394                         NULL, 0x0, NULL, HFILL }},
1395
1396                 { &hf_netb_fragments,
1397                 { "NetBIOS Fragments",  "netbios.fragments", FT_NONE, BASE_NONE,
1398                         NULL, 0x0, NULL, HFILL }},
1399         };
1400         module_t *netbios_module;
1401
1402         proto_netbios = proto_register_protocol("NetBIOS", "NetBIOS", "netbios");
1403         proto_register_subtree_array(ett, array_length(ett));
1404         proto_register_field_array(proto_netbios, hf_netb, array_length(hf_netb));
1405
1406         register_heur_dissector_list("netbios", &netbios_heur_subdissector_list);
1407
1408         netbios_module = prefs_register_protocol(proto_netbios, NULL);
1409         prefs_register_bool_preference(netbios_module, "defragment",
1410             "Reassemble fragmented NetBIOS messages spanning multiple frames",
1411             "Whether the NetBIOS dissector should defragment messages spanning multiple frames",
1412             &netbios_defragment);
1413
1414         register_init_routine(netbios_init);
1415 }
1416
1417 void
1418 proto_reg_handoff_netbios(void)
1419 {
1420         dissector_handle_t netbios_handle;
1421
1422         netbios_handle = create_dissector_handle(dissect_netbios,
1423             proto_netbios);
1424         dissector_add("llc.dsap", SAP_NETBIOS, netbios_handle);
1425         data_handle = find_dissector("data");
1426 }