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