2 * Routines for NetBIOS Name Service, Datagram Service, and Session Service
3 * packet disassembly (the name dates back to when it had only NBNS)
4 * Gilbert Ramirez <gram@verdict.uthscsa.edu>
5 * Much stuff added by Guy Harris <guy@netapp.com>
7 * $Id: packet-nbns.c,v 1.22 1999/07/07 22:51:47 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>
41 #include "packet-dns.h"
44 /* Packet structure taken from RFC 1002. See also RFC 1001.
45 * Opcode, flags, and rcode treated as "flags", similarly to DNS,
46 * to make it easier to lift the dissection code from "packet-dns.c". */
48 /* Offsets of fields in the NBNS header. */
56 /* Length of NBNS header. */
57 #define NBNS_HDRLEN 12
60 #define T_NB 32 /* NetBIOS name service RR */
61 #define T_NBSTAT 33 /* NetBIOS node status RR */
63 /* Bit fields in the flags */
64 #define F_RESPONSE (1<<15) /* packet is response */
65 #define F_OPCODE (0xF<<11) /* query opcode */
66 #define F_AUTHORITATIVE (1<<10) /* response is authoritative */
67 #define F_TRUNCATED (1<<9) /* response is truncated */
68 #define F_RECDESIRED (1<<8) /* recursion desired */
69 #define F_RECAVAIL (1<<7) /* recursion available */
70 #define F_BROADCAST (1<<4) /* broadcast/multicast packet */
71 #define F_RCODE (0xF<<0) /* reply code */
74 #define OPCODE_QUERY (0<<11) /* standard query */
75 #define OPCODE_REGISTRATION (5<<11) /* registration */
76 #define OPCODE_RELEASE (6<<11) /* release name */
77 #define OPCODE_WACK (7<<11) /* wait for acknowledgement */
78 #define OPCODE_REFRESH (8<<11) /* refresh registration */
79 #define OPCODE_REFRESHALT (9<<11) /* refresh registration (alternate opcode) */
80 #define OPCODE_MHREGISTRATION (15<<11) /* multi-homed registration */
83 #define RCODE_NOERROR (0<<0)
84 #define RCODE_FMTERROR (1<<0)
85 #define RCODE_SERVFAIL (2<<0)
86 #define RCODE_NAMEERROR (3<<0)
87 #define RCODE_NOTIMPL (4<<0)
88 #define RCODE_REFUSED (5<<0)
89 #define RCODE_ACTIVE (6<<0)
90 #define RCODE_CONFLICT (7<<0)
92 /* Values for the "NB_FLAGS" field of RR data. From RFC 1001 and 1002,
93 * except for NB_FLAGS_ONT_H_NODE, which was discovered by looking at
95 #define NB_FLAGS_ONT (3<<(15-2)) /* bits for node type */
96 #define NB_FLAGS_ONT_B_NODE (0<<(15-2)) /* B-mode node */
97 #define NB_FLAGS_ONT_P_NODE (1<<(15-2)) /* P-mode node */
98 #define NB_FLAGS_ONT_M_NODE (2<<(15-2)) /* M-mode node */
99 #define NB_FLAGS_ONT_H_NODE (3<<(15-2)) /* H-mode node */
101 #define NB_FLAGS_G (1<<(15-0)) /* group name */
103 /* Values for the "NAME_FLAGS" field of a NODE_NAME entry in T_NBSTAT
104 * RR data. From RFC 1001 and 1002, except for NAME_FLAGS_ONT_H_NODE,
105 * which was discovered by looking at packet traces. */
106 #define NAME_FLAGS_PRM (1<<(15-6)) /* name is permanent node name */
108 #define NAME_FLAGS_ACT (1<<(15-5)) /* name is active */
110 #define NAME_FLAGS_CNF (1<<(15-4)) /* name is in conflict */
112 #define NAME_FLAGS_DRG (1<<(15-3)) /* name is being deregistered */
114 #define NAME_FLAGS_ONT (3<<(15-2)) /* bits for node type */
115 #define NAME_FLAGS_ONT_B_NODE (0<<(15-2)) /* B-mode node */
116 #define NAME_FLAGS_ONT_P_NODE (1<<(15-2)) /* P-mode node */
117 #define NAME_FLAGS_ONT_M_NODE (2<<(15-2)) /* M-mode node */
119 #define NAME_FLAGS_G (1<<(15-0)) /* group name */
121 static const value_string opcode_vals[] = {
122 { OPCODE_QUERY, "Name query" },
123 { OPCODE_REGISTRATION, "Registration" },
124 { OPCODE_RELEASE, "Release" },
125 { OPCODE_WACK, "Wait for acknowledgment" },
126 { OPCODE_REFRESH, "Refresh" },
127 { OPCODE_REFRESHALT, "Refresh (alternate opcode)" },
128 { OPCODE_MHREGISTRATION, "Multi-homed registration" },
133 nbns_type_name (int type)
145 /* "Canonicalize" a 16-character NetBIOS name by:
147 * removing and saving the last byte;
149 * stripping trailing blanks;
151 * appending the trailing byte, as a hex number, in square brackets. */
153 canonicalize_netbios_name(char *nbname)
158 /* Get the last character of the name, as it's a special number
159 * indicating the type of the name, rather than part of the name
161 pnbname = nbname + 15; /* point to the 16th character */
162 lastchar = *(unsigned char *)pnbname;
164 /* Now strip off any trailing blanks used to pad it to
166 while (pnbname > &nbname[0]) {
167 if (*(pnbname - 1) != ' ')
168 break; /* found non-blank character */
169 pnbname--; /* blank - skip over it */
172 /* Replace the last character with its hex value, in square
173 * brackets, to make it easier to tell what it is. */
174 sprintf(pnbname, "[%02X]", lastchar);
180 get_nbns_name(const u_char *nbns_data_ptr, const u_char *pd,
181 int offset, char *name_ret)
185 char nbname[MAXDNAME+4]; /* 4 for [<last char>] */
186 char *pname, *pnbname, cname, cnbname;
188 name_len = get_dns_name(nbns_data_ptr, pd + offset, name, sizeof(name));
190 /* OK, now undo the first-level encoding. */
192 pnbname = &nbname[0];
194 /* Every two characters of the first level-encoded name
195 * turn into one character in the decoded name. */
198 break; /* no more characters */
200 break; /* scope ID follows */
201 if (cname < 'A' || cname > 'Z') {
204 "Illegal NetBIOS name (character not between A and Z in first-level encoding)");
208 cnbname = cname << 4;
212 if (cname == '\0' || cname == '.') {
213 /* No more characters in the name - but we're in
214 * the middle of a pair. Not legal. */
216 "Illegal NetBIOS name (odd number of bytes)");
219 if (cname < 'A' || cname > 'Z') {
222 "Illegal NetBIOS name (character not between A and Z in first-level encoding)");
229 /* Store the character. */
230 *pnbname++ = cnbname;
233 /* NetBIOS names are supposed to be exactly 16 bytes long. */
234 if (pnbname - nbname == 16) {
235 /* This one is; canonicalize its name. */
236 pnbname = canonicalize_netbios_name(nbname);
238 sprintf(nbname, "Illegal NetBIOS name (%ld bytes long)",
239 (long)(pnbname - nbname));
243 /* We have a scope ID, starting at "pname"; append that to
244 * the decoded host name. */
245 strcpy(pnbname, pname);
247 /* Terminate the decoded host name. */
252 strcpy (name_ret, nbname);
258 get_nbns_name_type_class(const u_char *nbns_data_ptr, const u_char *pd,
259 int offset, char *name_ret, int *name_len_ret, int *type_ret,
266 name_len = get_nbns_name(nbns_data_ptr, pd, offset, name_ret);
269 type = pntohs(&pd[offset]);
271 class = pntohs(&pd[offset]);
275 *name_len_ret = name_len;
282 dissect_nbns_query(const u_char *nbns_data_ptr, const u_char *pd, int offset,
283 proto_tree *nbns_tree)
293 const u_char *data_start;
297 data_start = dptr = pd + offset;
299 len = get_nbns_name_type_class(nbns_data_ptr, pd, offset, name,
300 &name_len, &type, &class);
303 type_name = nbns_type_name(type);
304 class_name = dns_class_name(class);
306 tq = proto_tree_add_text(nbns_tree, offset, len, "%s: type %s, class %s",
307 name, type_name, class_name);
308 q_tree = proto_item_add_subtree(tq, ETT_NBNS_QD);
310 proto_tree_add_text(q_tree, offset, name_len, "Name: %s", name);
313 proto_tree_add_text(q_tree, offset, 2, "Type: %s", type_name);
316 proto_tree_add_text(q_tree, offset, 2, "Class: %s", class_name);
319 return dptr - data_start;
323 nbns_add_nbns_flags(proto_tree *nbns_tree, int offset, u_short flags,
327 proto_tree *field_tree;
329 static const value_string rcode_vals[] = {
330 { RCODE_NOERROR, "No error" },
331 { RCODE_FMTERROR, "Request was invalidly formatted" },
332 { RCODE_SERVFAIL, "Server failure" },
333 { RCODE_NAMEERROR, "Requested name does not exist" },
334 { RCODE_NOTIMPL, "Request is not implemented" },
335 { RCODE_REFUSED, "Request was refused" },
336 { RCODE_ACTIVE, "Name is owned by another node" },
337 { RCODE_CONFLICT, "Name is in conflict" },
341 strcpy(buf, val_to_str(flags & F_OPCODE, opcode_vals,
342 "Unknown operation"));
343 if (flags & F_RESPONSE && !is_wack) {
344 strcat(buf, " response");
346 strcat(buf, val_to_str(flags & F_RCODE, rcode_vals,
349 tf = proto_tree_add_text(nbns_tree, offset, 2,
350 "Flags: 0x%04x (%s)", flags, buf);
351 field_tree = proto_item_add_subtree(tf, ETT_NBNS_FLAGS);
352 proto_tree_add_text(field_tree, offset, 2, "%s",
353 decode_boolean_bitfield(flags, F_RESPONSE,
354 2*8, "Response", "Query"));
355 proto_tree_add_text(field_tree, offset, 2, "%s",
356 decode_enumerated_bitfield(flags, F_OPCODE,
357 2*8, opcode_vals, "%s"));
358 if (flags & F_RESPONSE) {
359 proto_tree_add_text(field_tree, offset, 2,
361 decode_boolean_bitfield(flags, F_AUTHORITATIVE,
363 "Server is an authority for domain",
364 "Server isn't an authority for domain"));
366 proto_tree_add_text(field_tree, offset, 2, "%s",
367 decode_boolean_bitfield(flags, F_TRUNCATED,
369 "Message is truncated",
370 "Message is not truncated"));
371 proto_tree_add_text(field_tree, offset, 2, "%s",
372 decode_boolean_bitfield(flags, F_RECDESIRED,
374 "Do query recursively",
375 "Don't do query recursively"));
376 if (flags & F_RESPONSE) {
377 proto_tree_add_text(field_tree, offset, 2,
379 decode_boolean_bitfield(flags, F_RECAVAIL,
381 "Server can do recursive queries",
382 "Server can't do recursive queries"));
384 proto_tree_add_text(field_tree, offset, 2, "%s",
385 decode_boolean_bitfield(flags, F_BROADCAST,
388 "Not a broadcast packet"));
389 if (flags & F_RESPONSE && !is_wack) {
390 proto_tree_add_text(field_tree, offset, 2,
392 decode_enumerated_bitfield(flags, F_RCODE,
399 nbns_add_nb_flags(proto_tree *rr_tree, int offset, u_short flags)
402 proto_tree *field_tree;
404 static const value_string nb_flags_ont_vals[] = {
405 { NB_FLAGS_ONT_B_NODE, "B-node" },
406 { NB_FLAGS_ONT_P_NODE, "P-node" },
407 { NB_FLAGS_ONT_M_NODE, "M-node" },
408 { NB_FLAGS_ONT_H_NODE, "H-node" },
412 strcpy(buf, val_to_str(flags & NB_FLAGS_ONT, nb_flags_ont_vals,
415 if (flags & NB_FLAGS_G)
416 strcat(buf, "group");
418 strcat(buf, "unique");
419 tf = proto_tree_add_text(rr_tree, offset, 2, "Flags: 0x%x (%s)", flags,
421 field_tree = proto_item_add_subtree(tf, ETT_NBNS_NB_FLAGS);
422 proto_tree_add_text(field_tree, offset, 2, "%s",
423 decode_boolean_bitfield(flags, NB_FLAGS_G,
427 proto_tree_add_text(field_tree, offset, 2, "%s",
428 decode_enumerated_bitfield(flags, NB_FLAGS_ONT,
429 2*8, nb_flags_ont_vals, "%s"));
433 nbns_add_name_flags(proto_tree *rr_tree, int offset, u_short flags)
436 proto_item *field_tree;
438 static const value_string name_flags_ont_vals[] = {
439 { NAME_FLAGS_ONT_B_NODE, "B-node" },
440 { NAME_FLAGS_ONT_P_NODE, "P-node" },
441 { NAME_FLAGS_ONT_M_NODE, "M-node" },
445 strcpy(buf, val_to_str(flags & NAME_FLAGS_ONT, name_flags_ont_vals,
448 if (flags & NAME_FLAGS_G)
449 strcat(buf, "group");
451 strcat(buf, "unique");
452 if (flags & NAME_FLAGS_DRG)
453 strcat(buf, ", being deregistered");
454 if (flags & NAME_FLAGS_CNF)
455 strcat(buf, ", in conflict");
456 if (flags & NAME_FLAGS_ACT)
457 strcat(buf, ", active");
458 if (flags & NAME_FLAGS_PRM)
459 strcat(buf, ", permanent node name");
460 tf = proto_tree_add_text(rr_tree, offset, 2, "Name flags: 0x%x (%s)",
462 field_tree = proto_item_add_subtree(tf, ETT_NBNS_NAME_FLAGS);
463 proto_tree_add_text(field_tree, offset, 2, "%s",
464 decode_boolean_bitfield(flags, NAME_FLAGS_G,
468 proto_tree_add_text(field_tree, offset, 2, "%s",
469 decode_enumerated_bitfield(flags, NAME_FLAGS_ONT,
470 2*8, name_flags_ont_vals, "%s"));
471 proto_tree_add_text(field_tree, offset, 2, "%s",
472 decode_boolean_bitfield(flags, NAME_FLAGS_DRG,
474 "Name is being deregistered",
475 "Name is not being deregistered"));
476 proto_tree_add_text(field_tree, offset, 2, "%s",
477 decode_boolean_bitfield(flags, NAME_FLAGS_CNF,
479 "Name is in conflict",
480 "Name is not in conflict"));
481 proto_tree_add_text(field_tree, offset, 2, "%s",
482 decode_boolean_bitfield(flags, NAME_FLAGS_ACT,
485 "Name is not active"));
486 proto_tree_add_text(field_tree, offset, 2, "%s",
487 decode_boolean_bitfield(flags, NAME_FLAGS_PRM,
489 "Permanent node name",
490 "Not permanent node name"));
494 dissect_nbns_answer(const u_char *nbns_data_ptr, const u_char *pd, int offset,
495 proto_tree *nbns_tree, int opcode)
505 const u_char *data_start;
512 data_start = dptr = pd + offset;
514 len = get_nbns_name_type_class(nbns_data_ptr, pd, offset, name,
515 &name_len, &type, &class);
518 type_name = nbns_type_name(type);
519 class_name = dns_class_name(class);
524 data_len = pntohs(dptr);
528 case T_NB: /* "NB" record */
529 trr = proto_tree_add_text(nbns_tree, offset,
530 (dptr - data_start) + data_len,
531 "%s: type %s, class %s",
532 name, type_name, class_name);
533 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
534 name_len, type_name, class_name, ttl, data_len);
535 offset += (dptr - data_start);
536 while (data_len > 0) {
537 if (opcode == OPCODE_WACK) {
538 /* WACK response. This doesn't contain the
539 * same type of RR data as other T_NB
542 proto_tree_add_text(rr_tree, offset,
543 data_len, "(incomplete entry)");
546 flags = pntohs(dptr);
548 nbns_add_nbns_flags(rr_tree, offset, flags, 1);
553 proto_tree_add_text(rr_tree, offset,
554 data_len, "(incomplete entry)");
557 flags = pntohs(dptr);
559 nbns_add_nb_flags(rr_tree, offset, flags);
564 proto_tree_add_text(rr_tree, offset,
565 data_len, "(incomplete entry)");
568 proto_tree_add_text(rr_tree, offset, 4,
570 ip_to_str((guint8 *)dptr));
578 case T_NBSTAT: /* "NBSTAT" record */
581 char nbname[16+4+1]; /* 4 for [<last char>] */
584 trr = proto_tree_add_text(nbns_tree, offset,
585 (dptr - data_start) + data_len,
586 "%s: type %s, class %s",
587 name, type_name, class_name);
588 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
589 name_len, type_name, class_name, ttl, data_len);
590 offset += (dptr - data_start);
592 proto_tree_add_text(rr_tree, offset,
593 data_len, "(incomplete entry)");
598 proto_tree_add_text(rr_tree, offset, 2,
599 "Number of names: %u", num_names);
602 while (num_names != 0) {
604 proto_tree_add_text(rr_tree, offset,
605 data_len, "(incomplete entry)");
608 memcpy(nbname, dptr, 16);
610 canonicalize_netbios_name(nbname);
611 proto_tree_add_text(rr_tree, offset, 16,
617 proto_tree_add_text(rr_tree, offset,
618 data_len, "(incomplete entry)");
621 name_flags = pntohs(dptr);
623 nbns_add_name_flags(rr_tree, offset, name_flags);
631 proto_tree_add_text(rr_tree, offset,
632 data_len, "(incomplete entry)");
635 proto_tree_add_text(rr_tree, offset, 6,
637 ether_to_str((guint8 *)dptr));
643 proto_tree_add_text(rr_tree, offset,
644 data_len, "(incomplete entry)");
647 proto_tree_add_text(rr_tree, offset, 1,
648 "Jumpers: 0x%x", *dptr);
654 proto_tree_add_text(rr_tree, offset,
655 data_len, "(incomplete entry)");
658 proto_tree_add_text(rr_tree, offset, 1,
659 "Test result: 0x%x", *dptr);
665 proto_tree_add_text(rr_tree, offset,
666 data_len, "(incomplete entry)");
669 proto_tree_add_text(rr_tree, offset, 2,
670 "Version number: 0x%x", pntohs(dptr));
676 proto_tree_add_text(rr_tree, offset,
677 data_len, "(incomplete entry)");
680 proto_tree_add_text(rr_tree, offset, 2,
681 "Period of statistics: 0x%x", pntohs(dptr));
687 proto_tree_add_text(rr_tree, offset,
688 data_len, "(incomplete entry)");
691 proto_tree_add_text(rr_tree, offset, 2,
692 "Number of CRCs: %u", pntohs(dptr));
698 proto_tree_add_text(rr_tree, offset,
699 data_len, "(incomplete entry)");
702 proto_tree_add_text(rr_tree, offset, 2,
703 "Number of alignment errors: %u", pntohs(dptr));
709 proto_tree_add_text(rr_tree, offset,
710 data_len, "(incomplete entry)");
713 proto_tree_add_text(rr_tree, offset, 2,
714 "Number of collisions: %u", pntohs(dptr));
720 proto_tree_add_text(rr_tree, offset,
721 data_len, "(incomplete entry)");
724 proto_tree_add_text(rr_tree, offset, 2,
725 "Number of send aborts: %u", pntohs(dptr));
731 proto_tree_add_text(rr_tree, offset,
732 data_len, "(incomplete entry)");
735 proto_tree_add_text(rr_tree, offset, 4,
736 "Number of good sends: %u", pntohl(dptr));
742 proto_tree_add_text(rr_tree, offset,
743 data_len, "(incomplete entry)");
746 proto_tree_add_text(rr_tree, offset, 4,
747 "Number of good receives: %u", pntohl(dptr));
753 proto_tree_add_text(rr_tree, offset,
754 data_len, "(incomplete entry)");
757 proto_tree_add_text(rr_tree, offset, 2,
758 "Number of retransmits: %u", pntohs(dptr));
764 proto_tree_add_text(rr_tree, offset,
765 data_len, "(incomplete entry)");
768 proto_tree_add_text(rr_tree, offset, 2,
769 "Number of no resource conditions: %u", pntohs(dptr));
775 proto_tree_add_text(rr_tree, offset,
776 data_len, "(incomplete entry)");
779 proto_tree_add_text(rr_tree, offset, 2,
780 "Number of command blocks: %u", pntohs(dptr));
786 proto_tree_add_text(rr_tree, offset,
787 data_len, "(incomplete entry)");
790 proto_tree_add_text(rr_tree, offset, 2,
791 "Number of pending sessions: %u", pntohs(dptr));
797 proto_tree_add_text(rr_tree, offset,
798 data_len, "(incomplete entry)");
801 proto_tree_add_text(rr_tree, offset, 2,
802 "Max number of pending sessions: %u", pntohs(dptr));
806 proto_tree_add_text(rr_tree, offset, 2,
807 "Max total sessions possible: %u", pntohs(dptr));
813 proto_tree_add_text(rr_tree, offset,
814 data_len, "(incomplete entry)");
817 proto_tree_add_text(rr_tree, offset, 2,
818 "Session data packet size: %u", pntohs(dptr));
827 trr = proto_tree_add_text(nbns_tree, offset,
828 (dptr - data_start) + data_len,
829 "%s: type %s, class %s",
830 name, type_name, class_name);
831 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
832 name_len, type_name, class_name, ttl, data_len);
833 offset += (dptr - data_start);
834 proto_tree_add_text(rr_tree, offset, data_len, "Data");
839 return dptr - data_start;
843 dissect_query_records(const u_char *nbns_data_ptr, int count, const u_char *pd,
844 int cur_off, proto_tree *nbns_tree)
851 ti = proto_tree_add_text(nbns_tree, start_off, 0, "Queries");
852 qatree = proto_item_add_subtree(ti, ETT_NBNS_QRY);
854 cur_off += dissect_nbns_query(nbns_data_ptr, pd, cur_off, qatree);
855 proto_item_set_len(ti, cur_off - start_off);
857 return cur_off - start_off;
863 dissect_answer_records(const u_char *nbns_data_ptr, int count,
864 const u_char *pd, int cur_off, proto_tree *nbns_tree, int opcode, char *name)
871 ti = proto_tree_add_text(nbns_tree, start_off, 0, name);
872 qatree = proto_item_add_subtree(ti, ETT_NBNS_ANS);
874 cur_off += dissect_nbns_answer(nbns_data_ptr, pd, cur_off,
876 proto_item_set_len(ti, cur_off - start_off);
877 return cur_off - start_off;
881 dissect_nbns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
883 const u_char *nbns_data_ptr;
884 proto_tree *nbns_tree;
886 guint16 id, flags, quest, ans, auth, add;
889 nbns_data_ptr = &pd[offset];
891 /* To do: check for runts, errs, etc. */
892 id = pntohs(&pd[offset + NBNS_ID]);
893 flags = pntohs(&pd[offset + NBNS_FLAGS]);
894 quest = pntohs(&pd[offset + NBNS_QUEST]);
895 ans = pntohs(&pd[offset + NBNS_ANS]);
896 auth = pntohs(&pd[offset + NBNS_AUTH]);
897 add = pntohs(&pd[offset + NBNS_ADD]);
899 if (check_col(fd, COL_PROTOCOL))
900 col_add_str(fd, COL_PROTOCOL, "NBNS (UDP)");
901 if (check_col(fd, COL_INFO)) {
902 col_add_fstr(fd, COL_INFO, "%s%s",
903 val_to_str(flags & F_OPCODE, opcode_vals,
904 "Unknown operation (%x)"),
905 (flags & F_RESPONSE) ? " response" : "");
909 ti = proto_tree_add_text(tree, offset, END_OF_FRAME,
910 "NetBIOS Name Service");
911 nbns_tree = proto_item_add_subtree(ti, ETT_NBNS);
913 proto_tree_add_text(nbns_tree, offset + NBNS_ID, 2,
914 "Transaction ID: 0x%04X", id);
916 nbns_add_nbns_flags(nbns_tree, offset + NBNS_FLAGS, flags, 0);
917 proto_tree_add_text(nbns_tree, offset + NBNS_QUEST, 2,
920 proto_tree_add_text(nbns_tree, offset + NBNS_ANS, 2,
923 proto_tree_add_text(nbns_tree, offset + NBNS_AUTH, 2,
926 proto_tree_add_text(nbns_tree, offset + NBNS_ADD, 2,
927 "Additional RRs: %d",
930 cur_off = offset + NBNS_HDRLEN;
933 cur_off += dissect_query_records(nbns_data_ptr,
934 quest, pd, cur_off, nbns_tree);
937 cur_off += dissect_answer_records(nbns_data_ptr,
938 ans, pd, cur_off, nbns_tree,
943 cur_off += dissect_answer_records(nbns_data_ptr,
944 auth, pd, cur_off, nbns_tree,
946 "Authoritative nameservers");
949 cur_off += dissect_answer_records(nbns_data_ptr,
950 add, pd, cur_off, nbns_tree,
952 "Additional records");
956 /* NetBIOS datagram packet, from RFC 1002, page 32 */
957 struct nbdgm_header {
968 /* For packets with data */
972 /* For error packets */
977 dissect_nbdgm(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
980 proto_tree *nbdgm_tree = NULL;
982 struct nbdgm_header header;
988 "Direct_unique datagram",
989 "Direct_group datagram",
990 "Broadcast datagram",
992 "Datagram query request",
993 "Datagram positive query response",
994 "Datagram negative query response"
1004 static value_string error_codes[] = {
1005 { 0x82, "Destination name not present" },
1006 { 0x83, "Invalid source name format" },
1007 { 0x84, "Invalid destination name format" },
1011 char *yesno[] = { "No", "Yes" };
1013 char name[MAXDNAME+4];
1016 header.msg_type = pd[offset];
1018 flags = pd[offset+1];
1019 header.flags.more = flags & 1;
1020 header.flags.first = (flags & 2) >> 1;
1021 header.flags.node_type = (flags & 12) >> 2;
1023 header.dgm_id = pntohs(&pd[offset+2]);
1024 memcpy(&header.src_ip, &pd[offset+4], 4);
1025 header.src_port = pntohs(&pd[offset+8]);
1027 if (header.msg_type == 0x10 ||
1028 header.msg_type == 0x11 || header.msg_type == 0x12) {
1029 header.dgm_length = pntohs(&pd[offset+10]);
1030 header.pkt_offset = pntohs(&pd[offset+12]);
1032 else if (header.msg_type == 0x13) {
1033 header.error_code = pntohs(&pd[offset+10]);
1036 message_index = header.msg_type - 0x0f;
1037 if (message_index < 1 || message_index > 8) {
1041 if (check_col(fd, COL_PROTOCOL))
1042 col_add_str(fd, COL_PROTOCOL, "NBDS (UDP)");
1043 if (check_col(fd, COL_INFO)) {
1044 col_add_fstr(fd, COL_INFO, "%s", message[message_index]);
1048 ti = proto_tree_add_text(tree, offset, header.dgm_length,
1049 "NetBIOS Datagram Service");
1050 nbdgm_tree = proto_item_add_subtree(ti, ETT_NBDGM);
1052 proto_tree_add_text(nbdgm_tree, offset, 1, "Message Type: %s",
1053 message[message_index]);
1054 proto_tree_add_text(nbdgm_tree, offset+1, 1, "More fragments follow: %s",
1055 yesno[header.flags.more]);
1056 proto_tree_add_text(nbdgm_tree, offset+1, 1, "This is first fragment: %s",
1057 yesno[header.flags.first]);
1058 proto_tree_add_text(nbdgm_tree, offset+1, 1, "Node Type: %s",
1059 node[header.flags.node_type]);
1061 proto_tree_add_text(nbdgm_tree, offset+2, 2, "Datagram ID: 0x%04X",
1063 proto_tree_add_text(nbdgm_tree, offset+4, 4, "Source IP: %s",
1064 ip_to_str((guint8 *)&header.src_ip));
1065 proto_tree_add_text(nbdgm_tree, offset+8, 2, "Source Port: %d",
1072 if (header.msg_type == 0x10 ||
1073 header.msg_type == 0x11 || header.msg_type == 0x12) {
1076 proto_tree_add_text(nbdgm_tree, offset, 2,
1077 "Datagram length: %d bytes", header.dgm_length);
1078 proto_tree_add_text(nbdgm_tree, offset+2, 2,
1079 "Packet offset: %d bytes", header.pkt_offset);
1086 len = get_nbns_name(&pd[offset], pd, offset, name);
1089 proto_tree_add_text(nbdgm_tree, offset, len, "Source name: %s",
1095 /* Destination name */
1096 len = get_nbns_name(&pd[offset], pd, offset, name);
1099 proto_tree_add_text(nbdgm_tree, offset, len, "Destination name: %s",
1105 /* here we can pass the packet off to the next protocol */
1106 dissect_smb(pd, offset, fd, tree, max_data);
1108 else if (header.msg_type == 0x13) {
1110 proto_tree_add_text(nbdgm_tree, offset, 1, "Error code: %s",
1111 val_to_str(header.error_code, error_codes, "Unknown (0x%x)"));
1114 else if (header.msg_type == 0x14 ||
1115 header.msg_type == 0x15 || header.msg_type == 0x16) {
1116 /* Destination name */
1117 len = get_nbns_name(&pd[offset], pd, offset, name);
1120 proto_tree_add_text(nbdgm_tree, offset, len, "Destination name: %s",
1127 * NetBIOS Session Service message types.
1129 #define SESSION_MESSAGE 0x00
1130 #define SESSION_REQUEST 0x81
1131 #define POSITIVE_SESSION_RESPONSE 0x82
1132 #define NEGATIVE_SESSION_RESPONSE 0x83
1133 #define RETARGET_SESSION_RESPONSE 0x84
1134 #define SESSION_KEEP_ALIVE 0x85
1136 static const value_string message_types[] = {
1137 { SESSION_MESSAGE, "Session message" },
1138 { SESSION_REQUEST, "Session request" },
1139 { POSITIVE_SESSION_RESPONSE, "Positive session response" },
1140 { NEGATIVE_SESSION_RESPONSE, "Negative session response" },
1141 { RETARGET_SESSION_RESPONSE, "Retarget session response" },
1142 { SESSION_KEEP_ALIVE, "Session keep-alive" },
1147 * NetBIOS Session Service flags.
1149 #define NBSS_FLAGS_E 0x1
1151 static const value_string error_codes[] = {
1152 { 0x80, "Not listening on called name" },
1153 { 0x81, "Not listening for called name" },
1154 { 0x82, "Called name not present" },
1155 { 0x83, "Called name present, but insufficient resources" },
1156 { 0x8F, "Unspecified error" },
1161 * Dissect a single NBSS packet (there may be more than one in a given TCP
1162 * segment). Hmmm, in my experience, I have never seen more than one NBSS
1163 * in a single segment, since they mostly contain SMBs which are essentially
1164 * a request response type protocol (RJS). Also, a single session message
1165 * may be split over multiple segments.
1168 dissect_nbss_packet(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
1170 proto_tree *nbss_tree = NULL;
1172 proto_tree *field_tree;
1178 char name[MAXDNAME+4];
1180 msg_type = pd[offset];
1181 flags = pd[offset + 1];
1182 length = pntohs(&pd[offset + 2]);
1183 if (flags & NBSS_FLAGS_E)
1187 ti = proto_tree_add_text(tree, offset, length + 4,
1188 "NetBIOS Session Service");
1189 nbss_tree = proto_item_add_subtree(ti, ETT_NBSS);
1191 proto_tree_add_text(nbss_tree, offset, 1, "Message Type: %s",
1192 val_to_str(msg_type, message_types, "Unknown (%x)"));
1198 tf = proto_tree_add_text(nbss_tree, offset, 1, "Flags: 0x%04x", flags);
1199 field_tree = proto_item_add_subtree(tf, ETT_NBSS_FLAGS);
1200 proto_tree_add_text(field_tree, offset, 1, "%s",
1201 decode_boolean_bitfield(flags, NBSS_FLAGS_E,
1202 8, "Add 65536 to length", "Add 0 to length"));
1208 proto_tree_add_text(nbss_tree, offset, 2, "Length: %u", length);
1215 case SESSION_REQUEST:
1216 len = get_nbns_name(&pd[offset], pd, offset, name);
1218 proto_tree_add_text(nbss_tree, offset, len,
1219 "Called name: %s", name);
1222 len = get_nbns_name(&pd[offset], pd, offset, name);
1225 proto_tree_add_text(nbss_tree, offset, len,
1226 "Calling name: %s", name);
1230 case NEGATIVE_SESSION_RESPONSE:
1232 proto_tree_add_text(nbss_tree, offset, 1,
1234 val_to_str(pd[offset], error_codes, "Unknown (%x)"));
1237 case RETARGET_SESSION_RESPONSE:
1239 proto_tree_add_text(nbss_tree, offset, 4,
1240 "Retarget IP address: %s",
1241 ip_to_str((guint8 *)&pd[offset]));
1246 proto_tree_add_text(nbss_tree, offset, 2,
1247 "Retarget port: %u", pntohs(&pd[offset]));
1251 case SESSION_MESSAGE:
1253 * Here we can pass the packet off to the next protocol.
1256 dissect_smb(pd, offset, fd, tree, max_data - 4);
1265 dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
1272 msg_type = pd[offset];
1273 flags = pd[offset + 1];
1274 length = pntohs(&pd[offset + 2]);
1275 if (flags & NBSS_FLAGS_E)
1278 if (check_col(fd, COL_PROTOCOL))
1279 col_add_str(fd, COL_PROTOCOL, "NBSS (TCP)");
1280 if (check_col(fd, COL_INFO)) {
1281 col_add_fstr(fd, COL_INFO,
1282 val_to_str(msg_type, message_types, "Unknown (%x)"));
1285 while (max_data > 0) {
1286 len = dissect_nbss_packet(pd, offset, fd, tree, max_data);