2 * Routines for NetBIOS-over-TCP packet disassembly (the name dates back
3 * to when it had only NBNS)
4 * Gilbert Ramirez <gram@xiexie.org>
5 * Much stuff added by Guy Harris <guy@alum.mit.edu>
7 * $Id: packet-nbns.c,v 1.38 2000/03/12 04:47:42 gram Exp $
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@zing.org>
11 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
42 #include "packet-dns.h"
43 #include "packet-netbios.h"
44 #include "packet-smb.h"
46 static int proto_nbns = -1;
47 static int hf_nbns_response = -1;
48 static int hf_nbns_query = -1;
49 static int hf_nbns_transaction_id = -1;
50 static int hf_nbns_count_questions = -1;
51 static int hf_nbns_count_answers = -1;
52 static int hf_nbns_count_auth_rr = -1;
53 static int hf_nbns_count_add_rr = -1;
55 static gint ett_nbns = -1;
56 static gint ett_nbns_qd = -1;
57 static gint ett_nbns_flags = -1;
58 static gint ett_nbns_nb_flags = -1;
59 static gint ett_nbns_name_flags = -1;
60 static gint ett_nbns_rr = -1;
61 static gint ett_nbns_qry = -1;
62 static gint ett_nbns_ans = -1;
64 static int proto_nbdgm = -1;
65 static int hf_nbdgm_type = -1;
66 static int hf_nbdgm_fragment = -1;
67 static int hf_nbdgm_first = -1;
68 static int hf_nbdgm_node_type = -1;
69 static int hf_nbdgm_datagram_id = -1;
70 static int hf_nbdgm_src_ip = -1;
71 static int hf_nbdgm_src_port = -1;
73 static gint ett_nbdgm = -1;
75 static int proto_nbss = -1;
76 static int hf_nbss_type = -1;
77 static int hf_nbss_flags = -1;
79 static gint ett_nbss = -1;
80 static gint ett_nbss_flags = -1;
82 /* Packet structure taken from RFC 1002. See also RFC 1001.
83 * Opcode, flags, and rcode treated as "flags", similarly to DNS,
84 * to make it easier to lift the dissection code from "packet-dns.c". */
86 /* Offsets of fields in the NBNS header. */
94 /* Length of NBNS header. */
95 #define NBNS_HDRLEN 12
98 #define T_NB 32 /* NetBIOS name service RR */
99 #define T_NBSTAT 33 /* NetBIOS node status RR */
101 /* Bit fields in the flags */
102 #define F_RESPONSE (1<<15) /* packet is response */
103 #define F_OPCODE (0xF<<11) /* query opcode */
104 #define F_AUTHORITATIVE (1<<10) /* response is authoritative */
105 #define F_TRUNCATED (1<<9) /* response is truncated */
106 #define F_RECDESIRED (1<<8) /* recursion desired */
107 #define F_RECAVAIL (1<<7) /* recursion available */
108 #define F_BROADCAST (1<<4) /* broadcast/multicast packet */
109 #define F_RCODE (0xF<<0) /* reply code */
112 #define OPCODE_QUERY (0<<11) /* standard query */
113 #define OPCODE_REGISTRATION (5<<11) /* registration */
114 #define OPCODE_RELEASE (6<<11) /* release name */
115 #define OPCODE_WACK (7<<11) /* wait for acknowledgement */
116 #define OPCODE_REFRESH (8<<11) /* refresh registration */
117 #define OPCODE_REFRESHALT (9<<11) /* refresh registration (alternate opcode) */
118 #define OPCODE_MHREGISTRATION (15<<11) /* multi-homed registration */
121 #define RCODE_NOERROR (0<<0)
122 #define RCODE_FMTERROR (1<<0)
123 #define RCODE_SERVFAIL (2<<0)
124 #define RCODE_NAMEERROR (3<<0)
125 #define RCODE_NOTIMPL (4<<0)
126 #define RCODE_REFUSED (5<<0)
127 #define RCODE_ACTIVE (6<<0)
128 #define RCODE_CONFLICT (7<<0)
130 /* Values for the "NB_FLAGS" field of RR data. From RFC 1001 and 1002,
131 * except for NB_FLAGS_ONT_H_NODE, which was discovered by looking at
133 #define NB_FLAGS_ONT (3<<(15-2)) /* bits for node type */
134 #define NB_FLAGS_ONT_B_NODE (0<<(15-2)) /* B-mode node */
135 #define NB_FLAGS_ONT_P_NODE (1<<(15-2)) /* P-mode node */
136 #define NB_FLAGS_ONT_M_NODE (2<<(15-2)) /* M-mode node */
137 #define NB_FLAGS_ONT_H_NODE (3<<(15-2)) /* H-mode node */
139 #define NB_FLAGS_G (1<<(15-0)) /* group name */
141 /* Values for the "NAME_FLAGS" field of a NODE_NAME entry in T_NBSTAT
142 * RR data. From RFC 1001 and 1002, except for NAME_FLAGS_ONT_H_NODE,
143 * which was discovered by looking at packet traces. */
144 #define NAME_FLAGS_PRM (1<<(15-6)) /* name is permanent node name */
146 #define NAME_FLAGS_ACT (1<<(15-5)) /* name is active */
148 #define NAME_FLAGS_CNF (1<<(15-4)) /* name is in conflict */
150 #define NAME_FLAGS_DRG (1<<(15-3)) /* name is being deregistered */
152 #define NAME_FLAGS_ONT (3<<(15-2)) /* bits for node type */
153 #define NAME_FLAGS_ONT_B_NODE (0<<(15-2)) /* B-mode node */
154 #define NAME_FLAGS_ONT_P_NODE (1<<(15-2)) /* P-mode node */
155 #define NAME_FLAGS_ONT_M_NODE (2<<(15-2)) /* M-mode node */
157 #define NAME_FLAGS_G (1<<(15-0)) /* group name */
159 static const value_string opcode_vals[] = {
160 { OPCODE_QUERY, "Name query" },
161 { OPCODE_REGISTRATION, "Registration" },
162 { OPCODE_RELEASE, "Release" },
163 { OPCODE_WACK, "Wait for acknowledgment" },
164 { OPCODE_REFRESH, "Refresh" },
165 { OPCODE_REFRESHALT, "Refresh (alternate opcode)" },
166 { OPCODE_MHREGISTRATION, "Multi-homed registration" },
171 nbns_type_name (int type)
184 get_nbns_name(const u_char *pd, int offset, int nbns_data_offset,
185 char *name_ret, int *name_type_ret)
189 char nbname[NETBIOS_NAME_LEN];
190 char *pname, *pnbname, cname, cnbname;
193 name_len = get_dns_name(pd, offset, nbns_data_offset, name,
196 /* OK, now undo the first-level encoding. */
198 pnbname = &nbname[0];
200 /* Every two characters of the first level-encoded name
201 * turn into one character in the decoded name. */
204 break; /* no more characters */
206 break; /* scope ID follows */
207 if (cname < 'A' || cname > 'Z') {
210 "Illegal NetBIOS name (character not between A and Z in first-level encoding)");
214 cnbname = cname << 4;
218 if (cname == '\0' || cname == '.') {
219 /* No more characters in the name - but we're in
220 * the middle of a pair. Not legal. */
222 "Illegal NetBIOS name (odd number of bytes)");
225 if (cname < 'A' || cname > 'Z') {
228 "Illegal NetBIOS name (character not between A and Z in first-level encoding)");
235 /* Do we have room to store the character? */
236 if (pnbname < &nbname[NETBIOS_NAME_LEN]) {
237 /* Yes - store the character. */
241 /* We bump the pointer even if it's past the end of the
242 name, so we keep track of how long the name is. */
246 /* NetBIOS names are supposed to be exactly 16 bytes long. */
247 if (pnbname - nbname != NETBIOS_NAME_LEN) {
249 sprintf(nbname, "Illegal NetBIOS name (%ld bytes long)",
250 (long)(pnbname - nbname));
254 /* This one is; make its name printable. */
255 name_type = process_netbios_name(nbname, name_ret);
256 name_ret += strlen(name_ret);
257 sprintf(name_ret, "<%02x>", name_type);
260 /* We have a scope ID, starting at "pname"; append that to
261 * the decoded host name. */
262 strcpy(name_ret, pname);
264 if (name_type_ret != NULL)
265 *name_type_ret = name_type;
269 if (name_type_ret != NULL)
271 strcpy (name_ret, nbname);
277 get_nbns_name_type_class(const u_char *pd, int offset, int nbns_data_offset,
278 char *name_ret, int *name_len_ret, int *name_type_ret, int *type_ret,
285 name_len = get_nbns_name(pd, offset, nbns_data_offset, name_ret,
289 if (!BYTES_ARE_IN_FRAME(offset, 2)) {
290 /* We ran past the end of the captured data in the packet. */
293 type = pntohs(&pd[offset]);
296 if (!BYTES_ARE_IN_FRAME(offset, 2)) {
297 /* We ran past the end of the captured data in the packet. */
300 class = pntohs(&pd[offset]);
304 *name_len_ret = name_len;
310 add_name_and_type(proto_tree *tree, int offset, int len, char *tag,
311 char *name, int name_type)
313 if (name_type != -1) {
314 proto_tree_add_text(tree, offset, len, "%s: %s (%s)",
315 tag, name, netbios_name_type_descr(name_type));
317 proto_tree_add_text(tree, offset, len, "%s: %s",
323 dissect_nbns_query(const u_char *pd, int offset, int nbns_data_offset,
324 frame_data *fd, proto_tree *nbns_tree)
327 char name[(NETBIOS_NAME_LEN - 1)*4 + MAXDNAME];
335 const u_char *data_start;
339 data_start = dptr = pd + offset;
341 len = get_nbns_name_type_class(pd, offset, nbns_data_offset, name,
342 &name_len, &name_type, &type, &class);
344 /* We ran past the end of the data in the packet. */
349 type_name = nbns_type_name(type);
350 class_name = dns_class_name(class);
353 col_append_fstr(fd, COL_INFO, " %s %s", type_name, name);
354 if (nbns_tree != NULL) {
355 tq = proto_tree_add_text(nbns_tree, offset, len,
356 "%s: type %s, class %s", name, type_name, class_name);
357 q_tree = proto_item_add_subtree(tq, ett_nbns_qd);
359 add_name_and_type(q_tree, offset, name_len, "Name", name,
363 proto_tree_add_text(q_tree, offset, 2, "Type: %s", type_name);
366 proto_tree_add_text(q_tree, offset, 2, "Class: %s", class_name);
370 return dptr - data_start;
374 nbns_add_nbns_flags(proto_tree *nbns_tree, int offset, u_short flags,
378 proto_tree *field_tree;
380 static const value_string rcode_vals[] = {
381 { RCODE_NOERROR, "No error" },
382 { RCODE_FMTERROR, "Request was invalidly formatted" },
383 { RCODE_SERVFAIL, "Server failure" },
384 { RCODE_NAMEERROR, "Requested name does not exist" },
385 { RCODE_NOTIMPL, "Request is not implemented" },
386 { RCODE_REFUSED, "Request was refused" },
387 { RCODE_ACTIVE, "Name is owned by another node" },
388 { RCODE_CONFLICT, "Name is in conflict" },
392 strcpy(buf, val_to_str(flags & F_OPCODE, opcode_vals,
393 "Unknown operation"));
394 if (flags & F_RESPONSE && !is_wack) {
395 strcat(buf, " response");
397 strcat(buf, val_to_str(flags & F_RCODE, rcode_vals,
400 tf = proto_tree_add_text(nbns_tree, offset, 2,
401 "Flags: 0x%04x (%s)", flags, buf);
402 field_tree = proto_item_add_subtree(tf, ett_nbns_flags);
403 proto_tree_add_text(field_tree, offset, 2, "%s",
404 decode_boolean_bitfield(flags, F_RESPONSE,
405 2*8, "Response", "Query"));
406 proto_tree_add_text(field_tree, offset, 2, "%s",
407 decode_enumerated_bitfield(flags, F_OPCODE,
408 2*8, opcode_vals, "%s"));
409 if (flags & F_RESPONSE) {
410 proto_tree_add_text(field_tree, offset, 2,
412 decode_boolean_bitfield(flags, F_AUTHORITATIVE,
414 "Server is an authority for domain",
415 "Server isn't an authority for domain"));
417 proto_tree_add_text(field_tree, offset, 2, "%s",
418 decode_boolean_bitfield(flags, F_TRUNCATED,
420 "Message is truncated",
421 "Message is not truncated"));
422 proto_tree_add_text(field_tree, offset, 2, "%s",
423 decode_boolean_bitfield(flags, F_RECDESIRED,
425 "Do query recursively",
426 "Don't do query recursively"));
427 if (flags & F_RESPONSE) {
428 proto_tree_add_text(field_tree, offset, 2,
430 decode_boolean_bitfield(flags, F_RECAVAIL,
432 "Server can do recursive queries",
433 "Server can't do recursive queries"));
435 proto_tree_add_text(field_tree, offset, 2, "%s",
436 decode_boolean_bitfield(flags, F_BROADCAST,
439 "Not a broadcast packet"));
440 if (flags & F_RESPONSE && !is_wack) {
441 proto_tree_add_text(field_tree, offset, 2,
443 decode_enumerated_bitfield(flags, F_RCODE,
450 nbns_add_nb_flags(proto_tree *rr_tree, int offset, u_short flags)
453 proto_tree *field_tree;
455 static const value_string nb_flags_ont_vals[] = {
456 { NB_FLAGS_ONT_B_NODE, "B-node" },
457 { NB_FLAGS_ONT_P_NODE, "P-node" },
458 { NB_FLAGS_ONT_M_NODE, "M-node" },
459 { NB_FLAGS_ONT_H_NODE, "H-node" },
463 strcpy(buf, val_to_str(flags & NB_FLAGS_ONT, nb_flags_ont_vals,
466 if (flags & NB_FLAGS_G)
467 strcat(buf, "group");
469 strcat(buf, "unique");
470 tf = proto_tree_add_text(rr_tree, offset, 2, "Flags: 0x%x (%s)", flags,
472 field_tree = proto_item_add_subtree(tf, ett_nbns_nb_flags);
473 proto_tree_add_text(field_tree, offset, 2, "%s",
474 decode_boolean_bitfield(flags, NB_FLAGS_G,
478 proto_tree_add_text(field_tree, offset, 2, "%s",
479 decode_enumerated_bitfield(flags, NB_FLAGS_ONT,
480 2*8, nb_flags_ont_vals, "%s"));
484 nbns_add_name_flags(proto_tree *rr_tree, int offset, u_short flags)
487 proto_item *field_tree;
489 static const value_string name_flags_ont_vals[] = {
490 { NAME_FLAGS_ONT_B_NODE, "B-node" },
491 { NAME_FLAGS_ONT_P_NODE, "P-node" },
492 { NAME_FLAGS_ONT_M_NODE, "M-node" },
496 strcpy(buf, val_to_str(flags & NAME_FLAGS_ONT, name_flags_ont_vals,
499 if (flags & NAME_FLAGS_G)
500 strcat(buf, "group");
502 strcat(buf, "unique");
503 if (flags & NAME_FLAGS_DRG)
504 strcat(buf, ", being deregistered");
505 if (flags & NAME_FLAGS_CNF)
506 strcat(buf, ", in conflict");
507 if (flags & NAME_FLAGS_ACT)
508 strcat(buf, ", active");
509 if (flags & NAME_FLAGS_PRM)
510 strcat(buf, ", permanent node name");
511 tf = proto_tree_add_text(rr_tree, offset, 2, "Name flags: 0x%x (%s)",
513 field_tree = proto_item_add_subtree(tf, ett_nbns_name_flags);
514 proto_tree_add_text(field_tree, offset, 2, "%s",
515 decode_boolean_bitfield(flags, NAME_FLAGS_G,
519 proto_tree_add_text(field_tree, offset, 2, "%s",
520 decode_enumerated_bitfield(flags, NAME_FLAGS_ONT,
521 2*8, name_flags_ont_vals, "%s"));
522 proto_tree_add_text(field_tree, offset, 2, "%s",
523 decode_boolean_bitfield(flags, NAME_FLAGS_DRG,
525 "Name is being deregistered",
526 "Name is not being deregistered"));
527 proto_tree_add_text(field_tree, offset, 2, "%s",
528 decode_boolean_bitfield(flags, NAME_FLAGS_CNF,
530 "Name is in conflict",
531 "Name is not in conflict"));
532 proto_tree_add_text(field_tree, offset, 2, "%s",
533 decode_boolean_bitfield(flags, NAME_FLAGS_ACT,
536 "Name is not active"));
537 proto_tree_add_text(field_tree, offset, 2, "%s",
538 decode_boolean_bitfield(flags, NAME_FLAGS_PRM,
540 "Permanent node name",
541 "Not permanent node name"));
545 dissect_nbns_answer(const u_char *pd, int offset, int nbns_data_offset,
546 frame_data *fd, proto_tree *nbns_tree, int opcode)
549 char name[(NETBIOS_NAME_LEN - 1)*4 + MAXDNAME + 64];
558 const u_char *data_start;
564 char name_str[(NETBIOS_NAME_LEN - 1)*4 + 1];
566 char nbname[16+4+1]; /* 4 for [<last char>] */
569 data_start = dptr = pd + offset;
572 len = get_nbns_name_type_class(pd, offset, nbns_data_offset, name,
573 &name_len, &name_type, &type, &class);
575 /* We ran past the end of the data in the packet. */
581 type_name = nbns_type_name(type);
582 class_name = dns_class_name(class);
584 if (!BYTES_ARE_IN_FRAME(cur_offset, 4)) {
585 /* We ran past the end of the captured data in the packet. */
592 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
593 /* We ran past the end of the captured data in the packet. */
596 data_len = pntohs(dptr);
601 case T_NB: /* "NB" record */
603 if (opcode != OPCODE_WACK) {
604 col_append_fstr(fd, COL_INFO, " %s %s",
605 type_name, ip_to_str((guint8 *)(dptr + 2)));
608 if (nbns_tree == NULL)
610 trr = proto_tree_add_text(nbns_tree, offset,
611 (dptr - data_start) + data_len,
612 "%s: type %s, class %s",
613 name, type_name, class_name);
615 strcat(name, netbios_name_type_descr(name_type));
617 rr_tree = add_rr_to_tree(trr, ett_nbns_rr, offset, name,
618 name_len, type_name, class_name, ttl, data_len);
619 while (data_len > 0) {
620 if (opcode == OPCODE_WACK) {
621 /* WACK response. This doesn't contain the
622 * same type of RR data as other T_NB
624 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
625 /* We ran past the end of the captured
626 data in the packet. */
630 proto_tree_add_text(rr_tree, cur_offset,
631 data_len, "(incomplete entry)");
634 flags = pntohs(dptr);
636 nbns_add_nbns_flags(rr_tree, cur_offset,
641 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
642 /* We ran past the end of the captured
643 data in the packet. */
647 proto_tree_add_text(rr_tree, cur_offset,
648 data_len, "(incomplete entry)");
651 flags = pntohs(dptr);
653 nbns_add_nb_flags(rr_tree, cur_offset, flags);
657 if (!BYTES_ARE_IN_FRAME(cur_offset, 4)) {
658 /* We ran past the end of the captured
659 data in the packet. */
663 proto_tree_add_text(rr_tree, cur_offset,
664 data_len, "(incomplete entry)");
667 proto_tree_add_text(rr_tree, cur_offset, 4,
669 ip_to_str((guint8 *)dptr));
677 case T_NBSTAT: /* "NBSTAT" record */
679 col_append_fstr(fd, COL_INFO, " %s", type_name);
680 if (nbns_tree == NULL)
682 trr = proto_tree_add_text(nbns_tree, offset,
683 (dptr - data_start) + data_len,
684 "%s: type %s, class %s",
685 name, type_name, class_name);
686 rr_tree = add_rr_to_tree(trr, ett_nbns_rr, offset, name,
687 name_len, type_name, class_name, ttl, data_len);
688 if (!BYTES_ARE_IN_FRAME(cur_offset, 1)) {
689 /* We ran past the end of the captured
690 data in the packet. */
694 proto_tree_add_text(rr_tree, cur_offset,
695 data_len, "(incomplete entry)");
700 proto_tree_add_text(rr_tree, cur_offset, 2,
701 "Number of names: %u", num_names);
704 while (num_names != 0) {
705 if (!BYTES_ARE_IN_FRAME(cur_offset, NETBIOS_NAME_LEN)) {
706 /* We ran past the end of the captured
707 data in the packet. */
710 if (data_len < NETBIOS_NAME_LEN) {
711 proto_tree_add_text(rr_tree, cur_offset,
712 data_len, "(incomplete entry)");
715 memcpy(nbname, dptr, NETBIOS_NAME_LEN);
716 dptr += NETBIOS_NAME_LEN;
717 name_type = process_netbios_name(nbname,
719 proto_tree_add_text(rr_tree, cur_offset,
720 NETBIOS_NAME_LEN, "Name: %s<%02x> (%s)",
722 netbios_name_type_descr(name_type));
723 cur_offset += NETBIOS_NAME_LEN;
724 data_len -= NETBIOS_NAME_LEN;
726 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
727 /* We ran past the end of the captured
728 data in the packet. */
732 proto_tree_add_text(rr_tree, cur_offset,
733 data_len, "(incomplete entry)");
736 name_flags = pntohs(dptr);
738 nbns_add_name_flags(rr_tree, cur_offset, name_flags);
745 if (!BYTES_ARE_IN_FRAME(cur_offset, 6)) {
746 /* We ran past the end of the captured
747 data in the packet. */
751 proto_tree_add_text(rr_tree, cur_offset,
752 data_len, "(incomplete entry)");
755 proto_tree_add_text(rr_tree, cur_offset, 6,
757 ether_to_str((guint8 *)dptr));
762 if (!BYTES_ARE_IN_FRAME(cur_offset, 1)) {
763 /* We ran past the end of the captured
764 data in the packet. */
768 proto_tree_add_text(rr_tree, cur_offset,
769 data_len, "(incomplete entry)");
772 proto_tree_add_text(rr_tree, cur_offset, 1,
773 "Jumpers: 0x%x", *dptr);
778 if (!BYTES_ARE_IN_FRAME(cur_offset, 1)) {
779 /* We ran past the end of the captured
780 data in the packet. */
784 proto_tree_add_text(rr_tree, cur_offset,
785 data_len, "(incomplete entry)");
788 proto_tree_add_text(rr_tree, cur_offset, 1,
789 "Test result: 0x%x", *dptr);
794 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
795 /* We ran past the end of the captured
796 data in the packet. */
800 proto_tree_add_text(rr_tree, cur_offset,
801 data_len, "(incomplete entry)");
804 proto_tree_add_text(rr_tree, cur_offset, 2,
805 "Version number: 0x%x", pntohs(dptr));
810 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
811 /* We ran past the end of the captured
812 data in the packet. */
816 proto_tree_add_text(rr_tree, cur_offset,
817 data_len, "(incomplete entry)");
820 proto_tree_add_text(rr_tree, cur_offset, 2,
821 "Period of statistics: 0x%x", pntohs(dptr));
826 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
827 /* We ran past the end of the captured
828 data in the packet. */
832 proto_tree_add_text(rr_tree, cur_offset,
833 data_len, "(incomplete entry)");
836 proto_tree_add_text(rr_tree, cur_offset, 2,
837 "Number of CRCs: %u", pntohs(dptr));
842 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
843 /* We ran past the end of the captured
844 data in the packet. */
848 proto_tree_add_text(rr_tree, cur_offset,
849 data_len, "(incomplete entry)");
852 proto_tree_add_text(rr_tree, cur_offset, 2,
853 "Number of alignment errors: %u", pntohs(dptr));
858 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
859 /* We ran past the end of the captured
860 data in the packet. */
864 proto_tree_add_text(rr_tree, cur_offset,
865 data_len, "(incomplete entry)");
868 proto_tree_add_text(rr_tree, cur_offset, 2,
869 "Number of collisions: %u", pntohs(dptr));
874 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
875 /* We ran past the end of the captured
876 data in the packet. */
880 proto_tree_add_text(rr_tree, cur_offset,
881 data_len, "(incomplete entry)");
884 proto_tree_add_text(rr_tree, cur_offset, 2,
885 "Number of send aborts: %u", pntohs(dptr));
890 if (!BYTES_ARE_IN_FRAME(cur_offset, 4)) {
891 /* We ran past the end of the captured
892 data in the packet. */
896 proto_tree_add_text(rr_tree, cur_offset,
897 data_len, "(incomplete entry)");
900 proto_tree_add_text(rr_tree, cur_offset, 4,
901 "Number of good sends: %u", pntohl(dptr));
906 if (!BYTES_ARE_IN_FRAME(cur_offset, 4)) {
907 /* We ran past the end of the captured
908 data in the packet. */
912 proto_tree_add_text(rr_tree, cur_offset,
913 data_len, "(incomplete entry)");
916 proto_tree_add_text(rr_tree, cur_offset, 4,
917 "Number of good receives: %u", pntohl(dptr));
922 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
923 /* We ran past the end of the captured
924 data in the packet. */
928 proto_tree_add_text(rr_tree, cur_offset,
929 data_len, "(incomplete entry)");
932 proto_tree_add_text(rr_tree, cur_offset, 2,
933 "Number of retransmits: %u", pntohs(dptr));
938 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
939 /* We ran past the end of the captured
940 data in the packet. */
944 proto_tree_add_text(rr_tree, cur_offset,
945 data_len, "(incomplete entry)");
948 proto_tree_add_text(rr_tree, cur_offset, 2,
949 "Number of no resource conditions: %u", pntohs(dptr));
954 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
955 /* We ran past the end of the captured
956 data in the packet. */
960 proto_tree_add_text(rr_tree, cur_offset,
961 data_len, "(incomplete entry)");
964 proto_tree_add_text(rr_tree, cur_offset, 2,
965 "Number of command blocks: %u", pntohs(dptr));
970 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
971 /* We ran past the end of the captured
972 data in the packet. */
976 proto_tree_add_text(rr_tree, cur_offset,
977 data_len, "(incomplete entry)");
980 proto_tree_add_text(rr_tree, cur_offset, 2,
981 "Number of pending sessions: %u", pntohs(dptr));
986 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
987 /* We ran past the end of the captured
988 data in the packet. */
992 proto_tree_add_text(rr_tree, cur_offset,
993 data_len, "(incomplete entry)");
996 proto_tree_add_text(rr_tree, cur_offset, 2,
997 "Max number of pending sessions: %u", pntohs(dptr));
1001 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
1002 /* We ran past the end of the captured
1003 data in the packet. */
1007 proto_tree_add_text(rr_tree, cur_offset,
1008 data_len, "(incomplete entry)");
1011 proto_tree_add_text(rr_tree, cur_offset, 2,
1012 "Max total sessions possible: %u", pntohs(dptr));
1017 if (!BYTES_ARE_IN_FRAME(cur_offset, 2)) {
1018 /* We ran past the end of the captured
1019 data in the packet. */
1023 proto_tree_add_text(rr_tree, cur_offset,
1024 data_len, "(incomplete entry)");
1027 proto_tree_add_text(rr_tree, cur_offset, 2,
1028 "Session data packet size: %u", pntohs(dptr));
1037 col_append_fstr(fd, COL_INFO, " %s", type_name);
1038 if (nbns_tree == NULL)
1040 trr = proto_tree_add_text(nbns_tree, offset,
1041 (dptr - data_start) + data_len,
1042 "%s: type %s, class %s",
1043 name, type_name, class_name);
1044 rr_tree = add_rr_to_tree(trr, ett_nbns_rr, offset, name,
1045 name_len, type_name, class_name, ttl, data_len);
1046 proto_tree_add_text(rr_tree, cur_offset, data_len, "Data");
1051 return dptr - data_start;
1055 dissect_query_records(const u_char *pd, int cur_off, int nbns_data_offset,
1056 int count, frame_data *fd, proto_tree *nbns_tree)
1058 int start_off, add_off;
1059 proto_tree *qatree = NULL;
1060 proto_item *ti = NULL;
1062 start_off = cur_off;
1063 if (nbns_tree != NULL) {
1064 ti = proto_tree_add_text(nbns_tree, start_off, 0, "Queries");
1065 qatree = proto_item_add_subtree(ti, ett_nbns_qry);
1067 while (count-- > 0) {
1068 add_off = dissect_nbns_query(pd, cur_off, nbns_data_offset,
1071 /* We ran past the end of the captured data in the
1078 proto_item_set_len(ti, cur_off - start_off);
1080 return cur_off - start_off;
1086 dissect_answer_records(const u_char *pd, int cur_off, int nbns_data_offset,
1087 int count, frame_data *fd, proto_tree *nbns_tree, int opcode, char *name)
1089 int start_off, add_off;
1090 proto_tree *qatree = NULL;
1091 proto_item *ti = NULL;
1093 start_off = cur_off;
1094 if (nbns_tree != NULL) {
1095 ti = proto_tree_add_text(nbns_tree, start_off, 0, name);
1096 qatree = proto_item_add_subtree(ti, ett_nbns_ans);
1098 while (count-- > 0) {
1099 add_off = dissect_nbns_answer(pd, cur_off, nbns_data_offset,
1100 fd, qatree, opcode);
1102 /* We ran past the end of the captured data in the
1109 proto_item_set_len(ti, cur_off - start_off);
1110 return cur_off - start_off;
1114 dissect_nbns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
1116 int nbns_data_offset;
1117 proto_tree *nbns_tree = NULL;
1119 guint16 id, flags, quest, ans, auth, add;
1122 nbns_data_offset = offset;
1124 if (check_col(fd, COL_PROTOCOL))
1125 col_add_str(fd, COL_PROTOCOL, "NBNS");
1127 if (pi.captured_len < NBNS_HDRLEN) {
1128 col_add_str(fd, COL_INFO, "Short NBNS packet");
1129 dissect_data(pd, offset, fd, tree);
1133 /* To do: check for runts, errs, etc. */
1134 id = pntohs(&pd[offset + NBNS_ID]);
1135 flags = pntohs(&pd[offset + NBNS_FLAGS]);
1136 quest = pntohs(&pd[offset + NBNS_QUEST]);
1137 ans = pntohs(&pd[offset + NBNS_ANS]);
1138 auth = pntohs(&pd[offset + NBNS_AUTH]);
1139 add = pntohs(&pd[offset + NBNS_ADD]);
1141 if (check_col(fd, COL_INFO)) {
1142 col_add_fstr(fd, COL_INFO, "%s%s",
1143 val_to_str(flags & F_OPCODE, opcode_vals,
1144 "Unknown operation (%x)"),
1145 (flags & F_RESPONSE) ? " response" : "");
1147 /* Set "fd" to NULL; we pass a NULL "fd" to the query and
1148 answer dissectors, as a way of saying that they shouldn't
1149 add stuff to the COL_INFO column (a call to
1150 "check_col(fd, COL_INFO)" is more expensive than a check
1151 that a pointer isn't NULL). */
1156 ti = proto_tree_add_item(tree, proto_nbns, offset, END_OF_FRAME, NULL);
1157 nbns_tree = proto_item_add_subtree(ti, ett_nbns);
1159 if (flags & F_RESPONSE) {
1160 proto_tree_add_item_hidden(nbns_tree, hf_nbns_response,
1163 proto_tree_add_item_hidden(nbns_tree, hf_nbns_query,
1167 proto_tree_add_item(nbns_tree, hf_nbns_transaction_id,
1168 offset + NBNS_ID, 2, id);
1170 nbns_add_nbns_flags(nbns_tree, offset + NBNS_FLAGS, flags, 0);
1171 proto_tree_add_item(nbns_tree, hf_nbns_count_questions,
1172 offset + NBNS_QUEST, 2, quest);
1173 proto_tree_add_item(nbns_tree, hf_nbns_count_answers,
1174 offset + NBNS_ANS, 2, ans);
1175 proto_tree_add_item(nbns_tree, hf_nbns_count_auth_rr,
1176 offset + NBNS_AUTH, 2, auth);
1177 proto_tree_add_item(nbns_tree, hf_nbns_count_add_rr,
1178 offset + NBNS_ADD, 2, add);
1181 cur_off = offset + NBNS_HDRLEN;
1184 /* If this is a response, don't add information about the
1185 queries to the summary, just add information about the
1187 cur_off += dissect_query_records(pd, cur_off,
1188 nbns_data_offset, quest,
1189 (!(flags & F_RESPONSE) ? fd : NULL), nbns_tree);
1193 /* If this is a request, don't add information about the
1194 answers to the summary, just add information about the
1196 cur_off += dissect_answer_records(pd, cur_off,
1197 nbns_data_offset, ans,
1198 ((flags & F_RESPONSE) ? fd : NULL), nbns_tree,
1199 flags & F_OPCODE, "Answers");
1203 /* Don't add information about the authoritative name
1204 servers, or the additional records, to the summary. */
1206 cur_off += dissect_answer_records(pd, cur_off,
1208 auth, NULL, nbns_tree,
1210 "Authoritative nameservers");
1213 cur_off += dissect_answer_records(pd, cur_off,
1215 add, NULL, nbns_tree,
1217 "Additional records");
1221 /* NetBIOS datagram packet, from RFC 1002, page 32 */
1222 struct nbdgm_header {
1233 /* For packets with data */
1237 /* For error packets */
1242 dissect_nbdgm(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
1244 proto_tree *nbdgm_tree = NULL;
1246 struct nbdgm_header header;
1249 int max_data = pi.captured_len - offset;
1253 "Direct_unique datagram",
1254 "Direct_group datagram",
1255 "Broadcast datagram",
1257 "Datagram query request",
1258 "Datagram positive query response",
1259 "Datagram negative query response"
1269 static value_string error_codes[] = {
1270 { 0x82, "Destination name not present" },
1271 { 0x83, "Invalid source name format" },
1272 { 0x84, "Invalid destination name format" },
1276 char *yesno[] = { "No", "Yes" };
1278 char name[(NETBIOS_NAME_LEN - 1)*4 + MAXDNAME];
1282 header.msg_type = pd[offset];
1284 flags = pd[offset+1];
1285 header.flags.more = flags & 1;
1286 header.flags.first = (flags & 2) >> 1;
1287 header.flags.node_type = (flags & 12) >> 2;
1289 header.dgm_id = pntohs(&pd[offset+2]);
1290 memcpy(&header.src_ip, &pd[offset+4], 4);
1291 header.src_port = pntohs(&pd[offset+8]);
1293 if (header.msg_type == 0x10 ||
1294 header.msg_type == 0x11 || header.msg_type == 0x12) {
1295 header.dgm_length = pntohs(&pd[offset+10]);
1296 header.pkt_offset = pntohs(&pd[offset+12]);
1298 else if (header.msg_type == 0x13) {
1299 header.error_code = pntohs(&pd[offset+10]);
1302 message_index = header.msg_type - 0x0f;
1303 if (message_index < 1 || message_index > 8) {
1307 if (check_col(fd, COL_PROTOCOL))
1308 col_add_str(fd, COL_PROTOCOL, "NBDS");
1309 if (check_col(fd, COL_INFO)) {
1310 col_add_fstr(fd, COL_INFO, "%s", message[message_index]);
1314 ti = proto_tree_add_item(tree, proto_nbdgm, offset, header.dgm_length, NULL);
1315 nbdgm_tree = proto_item_add_subtree(ti, ett_nbdgm);
1317 proto_tree_add_uint_format(nbdgm_tree, hf_nbdgm_type,
1321 message[message_index]);
1322 proto_tree_add_boolean_format(nbdgm_tree, hf_nbdgm_fragment,
1325 "More fragments follow: %s",
1326 yesno[header.flags.more]);
1327 proto_tree_add_boolean_format(nbdgm_tree, hf_nbdgm_first,
1330 "This is first fragment: %s",
1331 yesno[header.flags.first]);
1332 proto_tree_add_uint_format(nbdgm_tree, hf_nbdgm_node_type,
1334 header.flags.node_type,
1336 node[header.flags.node_type]);
1338 proto_tree_add_item(nbdgm_tree, hf_nbdgm_datagram_id,
1339 offset+2, 2, header.dgm_id);
1340 proto_tree_add_item(nbdgm_tree, hf_nbdgm_src_ip,
1341 offset+4, 4, header.src_ip);
1342 proto_tree_add_item(nbdgm_tree, hf_nbdgm_src_port,
1343 offset+8, 2, header.src_port);
1350 if (header.msg_type == 0x10 ||
1351 header.msg_type == 0x11 || header.msg_type == 0x12) {
1354 proto_tree_add_text(nbdgm_tree, offset, 2,
1355 "Datagram length: %d bytes", header.dgm_length);
1356 proto_tree_add_text(nbdgm_tree, offset+2, 2,
1357 "Packet offset: %d bytes", header.pkt_offset);
1364 len = get_nbns_name(pd, offset, offset, name, &name_type);
1367 add_name_and_type(nbdgm_tree, offset, len,
1368 "Source name", name, name_type);
1373 /* Destination name */
1374 len = get_nbns_name(pd, offset, offset, name, &name_type);
1377 add_name_and_type(nbdgm_tree, offset, len,
1378 "Destination name", name, name_type);
1383 /* here we can pass the packet off to the next protocol */
1384 dissect_smb(pd, offset, fd, tree, max_data);
1386 else if (header.msg_type == 0x13) {
1388 proto_tree_add_text(nbdgm_tree, offset, 1, "Error code: %s",
1389 val_to_str(header.error_code, error_codes, "Unknown (0x%x)"));
1392 else if (header.msg_type == 0x14 ||
1393 header.msg_type == 0x15 || header.msg_type == 0x16) {
1394 /* Destination name */
1395 len = get_nbns_name(pd, offset, offset, name, &name_type);
1398 add_name_and_type(nbdgm_tree, offset, len,
1399 "Destination name", name, name_type);
1405 * NetBIOS Session Service message types.
1407 #define SESSION_MESSAGE 0x00
1408 #define SESSION_REQUEST 0x81
1409 #define POSITIVE_SESSION_RESPONSE 0x82
1410 #define NEGATIVE_SESSION_RESPONSE 0x83
1411 #define RETARGET_SESSION_RESPONSE 0x84
1412 #define SESSION_KEEP_ALIVE 0x85
1414 static const value_string message_types[] = {
1415 { SESSION_MESSAGE, "Session message" },
1416 { SESSION_REQUEST, "Session request" },
1417 { POSITIVE_SESSION_RESPONSE, "Positive session response" },
1418 { NEGATIVE_SESSION_RESPONSE, "Negative session response" },
1419 { RETARGET_SESSION_RESPONSE, "Retarget session response" },
1420 { SESSION_KEEP_ALIVE, "Session keep-alive" },
1425 * NetBIOS Session Service flags.
1427 #define NBSS_FLAGS_E 0x1
1429 static const value_string error_codes[] = {
1430 { 0x80, "Not listening on called name" },
1431 { 0x81, "Not listening for called name" },
1432 { 0x82, "Called name not present" },
1433 { 0x83, "Called name present, but insufficient resources" },
1434 { 0x8F, "Unspecified error" },
1439 * Dissect a single NBSS packet (there may be more than one in a given TCP
1440 * segment). Hmmm, in my experience, I have never seen more than one NBSS
1441 * in a single segment, since they mostly contain SMBs which are essentially
1442 * a request response type protocol (RJS). Also, a single session message
1443 * may be split over multiple segments.
1446 dissect_nbss_packet(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
1448 proto_tree *nbss_tree = NULL;
1450 proto_tree *field_tree;
1456 char name[(NETBIOS_NAME_LEN - 1)*4 + MAXDNAME];
1459 msg_type = pd[offset];
1460 flags = pd[offset + 1];
1461 length = pntohs(&pd[offset + 2]);
1462 if (flags & NBSS_FLAGS_E)
1466 ti = proto_tree_add_item(tree, proto_nbss, offset, length + 4, NULL);
1467 nbss_tree = proto_item_add_subtree(ti, ett_nbss);
1469 proto_tree_add_uint_format(nbss_tree, hf_nbss_type,
1473 val_to_str(msg_type, message_types,
1480 tf = proto_tree_add_item(nbss_tree, hf_nbss_flags, offset, 1, flags);
1481 field_tree = proto_item_add_subtree(tf, ett_nbss_flags);
1482 proto_tree_add_text(field_tree, offset, 1, "%s",
1483 decode_boolean_bitfield(flags, NBSS_FLAGS_E,
1484 8, "Add 65536 to length", "Add 0 to length"));
1490 proto_tree_add_text(nbss_tree, offset, 2, "Length: %u", length);
1497 case SESSION_REQUEST:
1498 len = get_nbns_name(pd, offset, offset, name, &name_type);
1500 add_name_and_type(nbss_tree, offset, len,
1501 "Called name", name, name_type);
1504 len = get_nbns_name(pd, offset, offset, name, &name_type);
1507 add_name_and_type(nbss_tree, offset, len,
1508 "Calling name", name, name_type);
1512 case NEGATIVE_SESSION_RESPONSE:
1514 proto_tree_add_text(nbss_tree, offset, 1,
1516 val_to_str(pd[offset], error_codes, "Unknown (%x)"));
1519 case RETARGET_SESSION_RESPONSE:
1521 proto_tree_add_text(nbss_tree, offset, 4,
1522 "Retarget IP address: %s",
1523 ip_to_str((guint8 *)&pd[offset]));
1528 proto_tree_add_text(nbss_tree, offset, 2,
1529 "Retarget port: %u", pntohs(&pd[offset]));
1533 case SESSION_MESSAGE:
1535 * Here we can pass the packet off to the next protocol.
1538 dissect_smb(pd, offset, fd, tree, max_data - 4);
1547 dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
1555 msg_type = pd[offset];
1556 flags = pd[offset + 1];
1557 length = pntohs(&pd[offset + 2]);
1558 if (flags & NBSS_FLAGS_E)
1562 * XXX - we should set this based on both "pi.captured_len"
1565 max_data = pi.captured_len - offset;
1567 /* Hmmm, it may be a continuation message ... */
1571 if (((msg_type != SESSION_REQUEST) &&
1572 (msg_type != POSITIVE_SESSION_RESPONSE) &&
1573 (msg_type != NEGATIVE_SESSION_RESPONSE) &&
1574 (msg_type != RETARGET_SESSION_RESPONSE) &&
1575 (msg_type != SESSION_MESSAGE)) ||
1576 ((msg_type == SESSION_MESSAGE) &&
1577 (memcmp(pd + offset + 4, "\377SMB", 4) != 0))) {
1579 if (check_col(fd, COL_PROTOCOL))
1580 col_add_str(fd, COL_PROTOCOL, "NBSS");
1581 if (check_col(fd, COL_INFO)) {
1582 col_add_fstr(fd, COL_INFO, "NBSS Continuation Message");
1586 proto_tree_add_text(tree, offset, max_data, "Continuation data");
1592 if (check_col(fd, COL_PROTOCOL))
1593 col_add_str(fd, COL_PROTOCOL, "NBSS");
1594 if (check_col(fd, COL_INFO)) {
1595 col_add_fstr(fd, COL_INFO,
1596 val_to_str(msg_type, message_types, "Unknown (%x)"));
1599 while (max_data > 0) {
1600 len = dissect_nbss_packet(pd, offset, fd, tree, max_data);
1608 proto_register_nbt(void)
1611 static hf_register_info hf_nbns[] = {
1612 { &hf_nbns_response,
1613 { "Response", "nbns.response",
1614 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1615 "TRUE if NBNS response" }},
1617 { "Query", "nbns.query",
1618 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1619 "TRUE if NBNS query" }},
1620 { &hf_nbns_transaction_id,
1621 { "Transaction ID", "nbns.id",
1622 FT_UINT16, BASE_HEX, NULL, 0x0,
1623 "Identification of transaction" }},
1624 { &hf_nbns_count_questions,
1625 { "Questions", "nbns.count.queries",
1626 FT_UINT16, BASE_DEC, NULL, 0x0,
1627 "Number of queries in packet" }},
1628 { &hf_nbns_count_answers,
1629 { "Answer RRs", "nbns.count.answers",
1630 FT_UINT16, BASE_DEC, NULL, 0x0,
1631 "Number of answers in packet" }},
1632 { &hf_nbns_count_auth_rr,
1633 { "Authority RRs", "nbns.count.auth_rr",
1634 FT_UINT16, BASE_DEC, NULL, 0x0,
1635 "Number of authoritative records in packet" }},
1636 { &hf_nbns_count_add_rr,
1637 { "Additional RRs", "nbns.count.add_rr",
1638 FT_UINT16, BASE_DEC, NULL, 0x0,
1639 "Number of additional records in packet" }}
1642 static hf_register_info hf_nbdgm[] = {
1644 { "Message Type", "nbdgm.type",
1645 FT_UINT8, BASE_DEC, NULL, 0x0,
1646 "NBDGM message type" }},
1647 { &hf_nbdgm_fragment,
1648 { "Fragmented", "nbdgm.next",
1649 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1650 "TRUE if more fragments follow" }},
1652 { "First fragment", "nbdgm.first",
1653 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1654 "TRUE if first fragment" }},
1655 { &hf_nbdgm_node_type,
1656 { "Node Type", "nbdgm.node_type",
1657 FT_UINT8, BASE_DEC, NULL, 0x0,
1659 { &hf_nbdgm_datagram_id,
1660 { "Datagram ID", "nbdgm.dgram_id",
1661 FT_UINT16, BASE_HEX, NULL, 0x0,
1662 "Datagram identifier" }},
1664 { "Source IP", "nbdgm.src.ip",
1665 FT_IPv4, BASE_NONE, NULL, 0x0,
1666 "Source IPv4 address" }},
1667 { &hf_nbdgm_src_port,
1668 { "Source Port", "nbdgm.src.port",
1669 FT_UINT16, BASE_DEC, NULL, 0x0,
1673 static hf_register_info hf_nbss[] = {
1675 { "Message Type", "nbss.type",
1676 FT_UINT8, BASE_DEC, NULL, 0x0,
1677 "NBSS message type" }},
1679 { "Flags", "nbss.flags",
1680 FT_UINT8, BASE_HEX, NULL, 0x0,
1681 "NBSS message flags" }}
1683 static gint *ett[] = {
1688 &ett_nbns_name_flags,
1697 proto_nbns = proto_register_protocol("NetBIOS Name Service", "nbns");
1698 proto_register_field_array(proto_nbns, hf_nbns, array_length(hf_nbns));
1700 proto_nbdgm = proto_register_protocol("NetBIOS Datagram Service", "nbdgm");
1701 proto_register_field_array(proto_nbdgm, hf_nbdgm, array_length(hf_nbdgm));
1703 proto_nbss = proto_register_protocol("NetBIOS Session Service", "nbss");
1704 proto_register_field_array(proto_nbss, hf_nbss, array_length(hf_nbss));
1706 proto_register_subtree_array(ett, array_length(ett));