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.21 1999/06/01 20:40:34 guy 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_item(nbns_tree, offset, len, "%s: type %s, class %s",
307 name, type_name, class_name);
308 q_tree = proto_tree_new();
309 proto_item_add_subtree(tq, q_tree, ETT_NBNS_QD);
311 proto_tree_add_item(q_tree, offset, name_len, "Name: %s", name);
314 proto_tree_add_item(q_tree, offset, 2, "Type: %s", type_name);
317 proto_tree_add_item(q_tree, offset, 2, "Class: %s", class_name);
320 return dptr - data_start;
324 nbns_add_nbns_flags(proto_tree *nbns_tree, int offset, u_short flags,
328 proto_tree *field_tree;
330 static const value_string rcode_vals[] = {
331 { RCODE_NOERROR, "No error" },
332 { RCODE_FMTERROR, "Request was invalidly formatted" },
333 { RCODE_SERVFAIL, "Server failure" },
334 { RCODE_NAMEERROR, "Requested name does not exist" },
335 { RCODE_NOTIMPL, "Request is not implemented" },
336 { RCODE_REFUSED, "Request was refused" },
337 { RCODE_ACTIVE, "Name is owned by another node" },
338 { RCODE_CONFLICT, "Name is in conflict" },
342 strcpy(buf, val_to_str(flags & F_OPCODE, opcode_vals,
343 "Unknown operation"));
344 if (flags & F_RESPONSE && !is_wack) {
345 strcat(buf, " response");
347 strcat(buf, val_to_str(flags & F_RCODE, rcode_vals,
350 tf = proto_tree_add_item(nbns_tree, offset, 2,
351 "Flags: 0x%04x (%s)", flags, buf);
352 field_tree = proto_tree_new();
353 proto_item_add_subtree(tf, field_tree, ETT_NBNS_FLAGS);
354 proto_tree_add_item(field_tree, offset, 2, "%s",
355 decode_boolean_bitfield(flags, F_RESPONSE,
356 2*8, "Response", "Query"));
357 proto_tree_add_item(field_tree, offset, 2, "%s",
358 decode_enumerated_bitfield(flags, F_OPCODE,
359 2*8, opcode_vals, "%s"));
360 if (flags & F_RESPONSE) {
361 proto_tree_add_item(field_tree, offset, 2,
363 decode_boolean_bitfield(flags, F_AUTHORITATIVE,
365 "Server is an authority for domain",
366 "Server isn't an authority for domain"));
368 proto_tree_add_item(field_tree, offset, 2, "%s",
369 decode_boolean_bitfield(flags, F_TRUNCATED,
371 "Message is truncated",
372 "Message is not truncated"));
373 proto_tree_add_item(field_tree, offset, 2, "%s",
374 decode_boolean_bitfield(flags, F_RECDESIRED,
376 "Do query recursively",
377 "Don't do query recursively"));
378 if (flags & F_RESPONSE) {
379 proto_tree_add_item(field_tree, offset, 2,
381 decode_boolean_bitfield(flags, F_RECAVAIL,
383 "Server can do recursive queries",
384 "Server can't do recursive queries"));
386 proto_tree_add_item(field_tree, offset, 2, "%s",
387 decode_boolean_bitfield(flags, F_BROADCAST,
390 "Not a broadcast packet"));
391 if (flags & F_RESPONSE && !is_wack) {
392 proto_tree_add_item(field_tree, offset, 2,
394 decode_enumerated_bitfield(flags, F_RCODE,
401 nbns_add_nb_flags(proto_tree *rr_tree, int offset, u_short flags)
404 proto_tree *field_tree;
406 static const value_string nb_flags_ont_vals[] = {
407 { NB_FLAGS_ONT_B_NODE, "B-node" },
408 { NB_FLAGS_ONT_P_NODE, "P-node" },
409 { NB_FLAGS_ONT_M_NODE, "M-node" },
410 { NB_FLAGS_ONT_H_NODE, "H-node" },
414 strcpy(buf, val_to_str(flags & NB_FLAGS_ONT, nb_flags_ont_vals,
417 if (flags & NB_FLAGS_G)
418 strcat(buf, "group");
420 strcat(buf, "unique");
421 tf = proto_tree_add_item(rr_tree, offset, 2, "Flags: 0x%x (%s)", flags,
423 field_tree = proto_tree_new();
424 proto_item_add_subtree(tf, field_tree, ETT_NBNS_NB_FLAGS);
425 proto_tree_add_item(field_tree, offset, 2, "%s",
426 decode_boolean_bitfield(flags, NB_FLAGS_G,
430 proto_tree_add_item(field_tree, offset, 2, "%s",
431 decode_enumerated_bitfield(flags, NB_FLAGS_ONT,
432 2*8, nb_flags_ont_vals, "%s"));
436 nbns_add_name_flags(proto_tree *rr_tree, int offset, u_short flags)
439 proto_item *field_tree;
441 static const value_string name_flags_ont_vals[] = {
442 { NAME_FLAGS_ONT_B_NODE, "B-node" },
443 { NAME_FLAGS_ONT_P_NODE, "P-node" },
444 { NAME_FLAGS_ONT_M_NODE, "M-node" },
448 strcpy(buf, val_to_str(flags & NAME_FLAGS_ONT, name_flags_ont_vals,
451 if (flags & NAME_FLAGS_G)
452 strcat(buf, "group");
454 strcat(buf, "unique");
455 if (flags & NAME_FLAGS_DRG)
456 strcat(buf, ", being deregistered");
457 if (flags & NAME_FLAGS_CNF)
458 strcat(buf, ", in conflict");
459 if (flags & NAME_FLAGS_ACT)
460 strcat(buf, ", active");
461 if (flags & NAME_FLAGS_PRM)
462 strcat(buf, ", permanent node name");
463 tf = proto_tree_add_item(rr_tree, offset, 2, "Name flags: 0x%x (%s)",
465 field_tree = proto_tree_new();
466 proto_item_add_subtree(tf, field_tree, ETT_NBNS_NAME_FLAGS);
467 proto_tree_add_item(field_tree, offset, 2, "%s",
468 decode_boolean_bitfield(flags, NAME_FLAGS_G,
472 proto_tree_add_item(field_tree, offset, 2, "%s",
473 decode_enumerated_bitfield(flags, NAME_FLAGS_ONT,
474 2*8, name_flags_ont_vals, "%s"));
475 proto_tree_add_item(field_tree, offset, 2, "%s",
476 decode_boolean_bitfield(flags, NAME_FLAGS_DRG,
478 "Name is being deregistered",
479 "Name is not being deregistered"));
480 proto_tree_add_item(field_tree, offset, 2, "%s",
481 decode_boolean_bitfield(flags, NAME_FLAGS_CNF,
483 "Name is in conflict",
484 "Name is not in conflict"));
485 proto_tree_add_item(field_tree, offset, 2, "%s",
486 decode_boolean_bitfield(flags, NAME_FLAGS_ACT,
489 "Name is not active"));
490 proto_tree_add_item(field_tree, offset, 2, "%s",
491 decode_boolean_bitfield(flags, NAME_FLAGS_PRM,
493 "Permanent node name",
494 "Not permanent node name"));
498 dissect_nbns_answer(const u_char *nbns_data_ptr, const u_char *pd, int offset,
499 proto_tree *nbns_tree, int opcode)
509 const u_char *data_start;
516 data_start = dptr = pd + offset;
518 len = get_nbns_name_type_class(nbns_data_ptr, pd, offset, name,
519 &name_len, &type, &class);
522 type_name = nbns_type_name(type);
523 class_name = dns_class_name(class);
528 data_len = pntohs(dptr);
532 case T_NB: /* "NB" record */
533 trr = proto_tree_add_item(nbns_tree, offset,
534 (dptr - data_start) + data_len,
535 "%s: type %s, class %s",
536 name, type_name, class_name);
537 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
538 name_len, type_name, class_name, ttl, data_len);
539 offset += (dptr - data_start);
540 while (data_len > 0) {
541 if (opcode == OPCODE_WACK) {
542 /* WACK response. This doesn't contain the
543 * same type of RR data as other T_NB
546 proto_tree_add_item(rr_tree, offset,
547 data_len, "(incomplete entry)");
550 flags = pntohs(dptr);
552 nbns_add_nbns_flags(rr_tree, offset, flags, 1);
557 proto_tree_add_item(rr_tree, offset,
558 data_len, "(incomplete entry)");
561 flags = pntohs(dptr);
563 nbns_add_nb_flags(rr_tree, offset, flags);
568 proto_tree_add_item(rr_tree, offset,
569 data_len, "(incomplete entry)");
572 proto_tree_add_item(rr_tree, offset, 4,
574 ip_to_str((guint8 *)dptr));
582 case T_NBSTAT: /* "NBSTAT" record */
585 char nbname[16+4+1]; /* 4 for [<last char>] */
588 trr = proto_tree_add_item(nbns_tree, offset,
589 (dptr - data_start) + data_len,
590 "%s: type %s, class %s",
591 name, type_name, class_name);
592 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
593 name_len, type_name, class_name, ttl, data_len);
594 offset += (dptr - data_start);
596 proto_tree_add_item(rr_tree, offset,
597 data_len, "(incomplete entry)");
602 proto_tree_add_item(rr_tree, offset, 2,
603 "Number of names: %u", num_names);
606 while (num_names != 0) {
608 proto_tree_add_item(rr_tree, offset,
609 data_len, "(incomplete entry)");
612 memcpy(nbname, dptr, 16);
614 canonicalize_netbios_name(nbname);
615 proto_tree_add_item(rr_tree, offset, 16,
621 proto_tree_add_item(rr_tree, offset,
622 data_len, "(incomplete entry)");
625 name_flags = pntohs(dptr);
627 nbns_add_name_flags(rr_tree, offset, name_flags);
635 proto_tree_add_item(rr_tree, offset,
636 data_len, "(incomplete entry)");
639 proto_tree_add_item(rr_tree, offset, 6,
641 ether_to_str((guint8 *)dptr));
647 proto_tree_add_item(rr_tree, offset,
648 data_len, "(incomplete entry)");
651 proto_tree_add_item(rr_tree, offset, 1,
652 "Jumpers: 0x%x", *dptr);
658 proto_tree_add_item(rr_tree, offset,
659 data_len, "(incomplete entry)");
662 proto_tree_add_item(rr_tree, offset, 1,
663 "Test result: 0x%x", *dptr);
669 proto_tree_add_item(rr_tree, offset,
670 data_len, "(incomplete entry)");
673 proto_tree_add_item(rr_tree, offset, 2,
674 "Version number: 0x%x", pntohs(dptr));
680 proto_tree_add_item(rr_tree, offset,
681 data_len, "(incomplete entry)");
684 proto_tree_add_item(rr_tree, offset, 2,
685 "Period of statistics: 0x%x", pntohs(dptr));
691 proto_tree_add_item(rr_tree, offset,
692 data_len, "(incomplete entry)");
695 proto_tree_add_item(rr_tree, offset, 2,
696 "Number of CRCs: %u", pntohs(dptr));
702 proto_tree_add_item(rr_tree, offset,
703 data_len, "(incomplete entry)");
706 proto_tree_add_item(rr_tree, offset, 2,
707 "Number of alignment errors: %u", pntohs(dptr));
713 proto_tree_add_item(rr_tree, offset,
714 data_len, "(incomplete entry)");
717 proto_tree_add_item(rr_tree, offset, 2,
718 "Number of collisions: %u", pntohs(dptr));
724 proto_tree_add_item(rr_tree, offset,
725 data_len, "(incomplete entry)");
728 proto_tree_add_item(rr_tree, offset, 2,
729 "Number of send aborts: %u", pntohs(dptr));
735 proto_tree_add_item(rr_tree, offset,
736 data_len, "(incomplete entry)");
739 proto_tree_add_item(rr_tree, offset, 4,
740 "Number of good sends: %u", pntohl(dptr));
746 proto_tree_add_item(rr_tree, offset,
747 data_len, "(incomplete entry)");
750 proto_tree_add_item(rr_tree, offset, 4,
751 "Number of good receives: %u", pntohl(dptr));
757 proto_tree_add_item(rr_tree, offset,
758 data_len, "(incomplete entry)");
761 proto_tree_add_item(rr_tree, offset, 2,
762 "Number of retransmits: %u", pntohs(dptr));
768 proto_tree_add_item(rr_tree, offset,
769 data_len, "(incomplete entry)");
772 proto_tree_add_item(rr_tree, offset, 2,
773 "Number of no resource conditions: %u", pntohs(dptr));
779 proto_tree_add_item(rr_tree, offset,
780 data_len, "(incomplete entry)");
783 proto_tree_add_item(rr_tree, offset, 2,
784 "Number of command blocks: %u", pntohs(dptr));
790 proto_tree_add_item(rr_tree, offset,
791 data_len, "(incomplete entry)");
794 proto_tree_add_item(rr_tree, offset, 2,
795 "Number of pending sessions: %u", pntohs(dptr));
801 proto_tree_add_item(rr_tree, offset,
802 data_len, "(incomplete entry)");
805 proto_tree_add_item(rr_tree, offset, 2,
806 "Max number of pending sessions: %u", pntohs(dptr));
810 proto_tree_add_item(rr_tree, offset, 2,
811 "Max total sessions possible: %u", pntohs(dptr));
817 proto_tree_add_item(rr_tree, offset,
818 data_len, "(incomplete entry)");
821 proto_tree_add_item(rr_tree, offset, 2,
822 "Session data packet size: %u", pntohs(dptr));
831 trr = proto_tree_add_item(nbns_tree, offset,
832 (dptr - data_start) + data_len,
833 "%s: type %s, class %s",
834 name, type_name, class_name);
835 rr_tree = add_rr_to_tree(trr, ETT_NBNS_RR, offset, name,
836 name_len, type_name, class_name, ttl, data_len);
837 offset += (dptr - data_start);
838 proto_tree_add_item(rr_tree, offset, data_len, "Data");
843 return dptr - data_start;
847 dissect_query_records(const u_char *nbns_data_ptr, int count, const u_char *pd,
848 int cur_off, proto_tree *nbns_tree)
855 ti = proto_tree_add_item(nbns_tree, start_off, 0, "Queries");
856 qatree = proto_tree_new();
857 proto_item_add_subtree(ti, qatree, ETT_NBNS_QRY);
859 cur_off += dissect_nbns_query(nbns_data_ptr, pd, cur_off, qatree);
860 proto_item_set_len(ti, cur_off - start_off);
862 return cur_off - start_off;
868 dissect_answer_records(const u_char *nbns_data_ptr, int count,
869 const u_char *pd, int cur_off, proto_tree *nbns_tree, int opcode, char *name)
876 ti = proto_tree_add_item(nbns_tree, start_off, 0, name);
877 qatree = proto_tree_new();
878 proto_item_add_subtree(ti, qatree, ETT_NBNS_ANS);
880 cur_off += dissect_nbns_answer(nbns_data_ptr, pd, cur_off,
882 proto_item_set_len(ti, cur_off - start_off);
883 return cur_off - start_off;
887 dissect_nbns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
889 const u_char *nbns_data_ptr;
890 proto_tree *nbns_tree;
892 guint16 id, flags, quest, ans, auth, add;
895 nbns_data_ptr = &pd[offset];
897 /* To do: check for runts, errs, etc. */
898 id = pntohs(&pd[offset + NBNS_ID]);
899 flags = pntohs(&pd[offset + NBNS_FLAGS]);
900 quest = pntohs(&pd[offset + NBNS_QUEST]);
901 ans = pntohs(&pd[offset + NBNS_ANS]);
902 auth = pntohs(&pd[offset + NBNS_AUTH]);
903 add = pntohs(&pd[offset + NBNS_ADD]);
905 if (check_col(fd, COL_PROTOCOL))
906 col_add_str(fd, COL_PROTOCOL, "NBNS (UDP)");
907 if (check_col(fd, COL_INFO)) {
908 col_add_fstr(fd, COL_INFO, "%s%s",
909 val_to_str(flags & F_OPCODE, opcode_vals,
910 "Unknown operation (%x)"),
911 (flags & F_RESPONSE) ? " response" : "");
915 ti = proto_tree_add_item(tree, offset, END_OF_FRAME,
916 "NetBIOS Name Service");
917 nbns_tree = proto_tree_new();
918 proto_item_add_subtree(ti, nbns_tree, ETT_NBNS);
920 proto_tree_add_item(nbns_tree, offset + NBNS_ID, 2,
921 "Transaction ID: 0x%04X", id);
923 nbns_add_nbns_flags(nbns_tree, offset + NBNS_FLAGS, flags, 0);
924 proto_tree_add_item(nbns_tree, offset + NBNS_QUEST, 2,
927 proto_tree_add_item(nbns_tree, offset + NBNS_ANS, 2,
930 proto_tree_add_item(nbns_tree, offset + NBNS_AUTH, 2,
933 proto_tree_add_item(nbns_tree, offset + NBNS_ADD, 2,
934 "Additional RRs: %d",
937 cur_off = offset + NBNS_HDRLEN;
940 cur_off += dissect_query_records(nbns_data_ptr,
941 quest, pd, cur_off, nbns_tree);
944 cur_off += dissect_answer_records(nbns_data_ptr,
945 ans, pd, cur_off, nbns_tree,
950 cur_off += dissect_answer_records(nbns_data_ptr,
951 auth, pd, cur_off, nbns_tree,
953 "Authoritative nameservers");
956 cur_off += dissect_answer_records(nbns_data_ptr,
957 add, pd, cur_off, nbns_tree,
959 "Additional records");
963 /* NetBIOS datagram packet, from RFC 1002, page 32 */
964 struct nbdgm_header {
975 /* For packets with data */
979 /* For error packets */
984 dissect_nbdgm(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
987 proto_tree *nbdgm_tree = NULL;
989 struct nbdgm_header header;
995 "Direct_unique datagram",
996 "Direct_group datagram",
997 "Broadcast datagram",
999 "Datagram query request",
1000 "Datagram positive query response",
1001 "Datagram negative query response"
1011 static value_string error_codes[] = {
1012 { 0x82, "Destination name not present" },
1013 { 0x83, "Invalid source name format" },
1014 { 0x84, "Invalid destination name format" },
1018 char *yesno[] = { "No", "Yes" };
1020 char name[MAXDNAME+4];
1023 header.msg_type = pd[offset];
1025 flags = pd[offset+1];
1026 header.flags.more = flags & 1;
1027 header.flags.first = (flags & 2) >> 1;
1028 header.flags.node_type = (flags & 12) >> 2;
1030 header.dgm_id = pntohs(&pd[offset+2]);
1031 memcpy(&header.src_ip, &pd[offset+4], 4);
1032 header.src_port = pntohs(&pd[offset+8]);
1034 if (header.msg_type == 0x10 ||
1035 header.msg_type == 0x11 || header.msg_type == 0x12) {
1036 header.dgm_length = pntohs(&pd[offset+10]);
1037 header.pkt_offset = pntohs(&pd[offset+12]);
1039 else if (header.msg_type == 0x13) {
1040 header.error_code = pntohs(&pd[offset+10]);
1043 message_index = header.msg_type - 0x0f;
1044 if (message_index < 1 || message_index > 8) {
1048 if (check_col(fd, COL_PROTOCOL))
1049 col_add_str(fd, COL_PROTOCOL, "NBDS (UDP)");
1050 if (check_col(fd, COL_INFO)) {
1051 col_add_fstr(fd, COL_INFO, "%s", message[message_index]);
1055 ti = proto_tree_add_item(tree, offset, header.dgm_length,
1056 "NetBIOS Datagram Service");
1057 nbdgm_tree = proto_tree_new();
1058 proto_item_add_subtree(ti, nbdgm_tree, ETT_NBDGM);
1060 proto_tree_add_item(nbdgm_tree, offset, 1, "Message Type: %s",
1061 message[message_index]);
1062 proto_tree_add_item(nbdgm_tree, offset+1, 1, "More fragments follow: %s",
1063 yesno[header.flags.more]);
1064 proto_tree_add_item(nbdgm_tree, offset+1, 1, "This is first fragment: %s",
1065 yesno[header.flags.first]);
1066 proto_tree_add_item(nbdgm_tree, offset+1, 1, "Node Type: %s",
1067 node[header.flags.node_type]);
1069 proto_tree_add_item(nbdgm_tree, offset+2, 2, "Datagram ID: 0x%04X",
1071 proto_tree_add_item(nbdgm_tree, offset+4, 4, "Source IP: %s",
1072 ip_to_str((guint8 *)&header.src_ip));
1073 proto_tree_add_item(nbdgm_tree, offset+8, 2, "Source Port: %d",
1080 if (header.msg_type == 0x10 ||
1081 header.msg_type == 0x11 || header.msg_type == 0x12) {
1084 proto_tree_add_item(nbdgm_tree, offset, 2,
1085 "Datagram length: %d bytes", header.dgm_length);
1086 proto_tree_add_item(nbdgm_tree, offset+2, 2,
1087 "Packet offset: %d bytes", header.pkt_offset);
1094 len = get_nbns_name(&pd[offset], pd, offset, name);
1097 proto_tree_add_item(nbdgm_tree, offset, len, "Source name: %s",
1103 /* Destination name */
1104 len = get_nbns_name(&pd[offset], pd, offset, name);
1107 proto_tree_add_item(nbdgm_tree, offset, len, "Destination name: %s",
1113 /* here we can pass the packet off to the next protocol */
1114 dissect_smb(pd, offset, fd, tree, max_data);
1116 else if (header.msg_type == 0x13) {
1118 proto_tree_add_item(nbdgm_tree, offset, 1, "Error code: %s",
1119 val_to_str(header.error_code, error_codes, "Unknown (0x%x)"));
1122 else if (header.msg_type == 0x14 ||
1123 header.msg_type == 0x15 || header.msg_type == 0x16) {
1124 /* Destination name */
1125 len = get_nbns_name(&pd[offset], pd, offset, name);
1128 proto_tree_add_item(nbdgm_tree, offset, len, "Destination name: %s",
1135 * NetBIOS Session Service message types.
1137 #define SESSION_MESSAGE 0x00
1138 #define SESSION_REQUEST 0x81
1139 #define POSITIVE_SESSION_RESPONSE 0x82
1140 #define NEGATIVE_SESSION_RESPONSE 0x83
1141 #define RETARGET_SESSION_RESPONSE 0x84
1142 #define SESSION_KEEP_ALIVE 0x85
1144 static const value_string message_types[] = {
1145 { SESSION_MESSAGE, "Session message" },
1146 { SESSION_REQUEST, "Session request" },
1147 { POSITIVE_SESSION_RESPONSE, "Positive session response" },
1148 { NEGATIVE_SESSION_RESPONSE, "Negative session response" },
1149 { RETARGET_SESSION_RESPONSE, "Retarget session response" },
1150 { SESSION_KEEP_ALIVE, "Session keep-alive" },
1155 * NetBIOS Session Service flags.
1157 #define NBSS_FLAGS_E 0x1
1159 static const value_string error_codes[] = {
1160 { 0x80, "Not listening on called name" },
1161 { 0x81, "Not listening for called name" },
1162 { 0x82, "Called name not present" },
1163 { 0x83, "Called name present, but insufficient resources" },
1164 { 0x8F, "Unspecified error" },
1169 * Dissect a single NBSS packet (there may be more than one in a given TCP
1170 * segment). Hmmm, in my experience, I have never seen more than one NBSS
1171 * in a single segment, since they mostly contain SMBs which are essentially
1172 * a request response type protocol (RJS). Also, a single session message
1173 * may be split over multiple segments.
1176 dissect_nbss_packet(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
1178 proto_tree *nbss_tree = NULL;
1180 proto_tree *field_tree;
1186 char name[MAXDNAME+4];
1188 msg_type = pd[offset];
1189 flags = pd[offset + 1];
1190 length = pntohs(&pd[offset + 2]);
1191 if (flags & NBSS_FLAGS_E)
1195 ti = proto_tree_add_item(tree, offset, length + 4,
1196 "NetBIOS Session Service");
1197 nbss_tree = proto_tree_new();
1198 proto_item_add_subtree(ti, nbss_tree, ETT_NBSS);
1200 proto_tree_add_item(nbss_tree, offset, 1, "Message Type: %s",
1201 val_to_str(msg_type, message_types, "Unknown (%x)"));
1207 tf = proto_tree_add_item(nbss_tree, offset, 1, "Flags: 0x%04x", flags);
1208 field_tree = proto_tree_new();
1209 proto_item_add_subtree(tf, field_tree, ETT_NBSS_FLAGS);
1210 proto_tree_add_item(field_tree, offset, 1, "%s",
1211 decode_boolean_bitfield(flags, NBSS_FLAGS_E,
1212 8, "Add 65536 to length", "Add 0 to length"));
1218 proto_tree_add_item(nbss_tree, offset, 2, "Length: %u", length);
1225 case SESSION_REQUEST:
1226 len = get_nbns_name(&pd[offset], pd, offset, name);
1228 proto_tree_add_item(nbss_tree, offset, len,
1229 "Called name: %s", name);
1232 len = get_nbns_name(&pd[offset], pd, offset, name);
1235 proto_tree_add_item(nbss_tree, offset, len,
1236 "Calling name: %s", name);
1240 case NEGATIVE_SESSION_RESPONSE:
1242 proto_tree_add_item(nbss_tree, offset, 1,
1244 val_to_str(pd[offset], error_codes, "Unknown (%x)"));
1247 case RETARGET_SESSION_RESPONSE:
1249 proto_tree_add_item(nbss_tree, offset, 4,
1250 "Retarget IP address: %s",
1251 ip_to_str((guint8 *)&pd[offset]));
1256 proto_tree_add_item(nbss_tree, offset, 2,
1257 "Retarget port: %u", pntohs(&pd[offset]));
1261 case SESSION_MESSAGE:
1263 * Here we can pass the packet off to the next protocol.
1266 dissect_smb(pd, offset, fd, tree, max_data - 4);
1275 dissect_nbss(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int max_data)
1282 msg_type = pd[offset];
1283 flags = pd[offset + 1];
1284 length = pntohs(&pd[offset + 2]);
1285 if (flags & NBSS_FLAGS_E)
1288 if (check_col(fd, COL_PROTOCOL))
1289 col_add_str(fd, COL_PROTOCOL, "NBSS (TCP)");
1290 if (check_col(fd, COL_INFO)) {
1291 col_add_fstr(fd, COL_INFO,
1292 val_to_str(msg_type, message_types, "Unknown (%x)"));
1295 while (max_data > 0) {
1296 len = dissect_nbss_packet(pd, offset, fd, tree, max_data);