Fix up the "ethereal-dev" address to refer to "ethereal.com" rather than
[obnox/wireshark/wip.git] / packet-nbipx.c
1 /* packet-nbipx.c
2  * Routines for NetBIOS over IPX packet disassembly
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-nbipx.c,v 1.34 2001/01/09 06:31:38 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include <glib.h>
37 #include "packet.h"
38 #include "packet-ipx.h"
39 #include "packet-netbios.h"
40 #include "packet-smb.h"
41
42 static int proto_nbipx = -1;
43
44 static gint ett_nbipx = -1;
45 static gint ett_nbipx_conn_ctrl = -1;
46 static gint ett_nbipx_name_type_flags = -1;
47
48 enum nbipx_protocol {
49         NETBIOS_NETWARE,
50         NETBIOS_NWLINK
51 };
52
53 static void
54 dissect_nbipx_ns(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
55
56 static void
57 dissect_nbipx_dg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
58
59 /* There is no RFC or public specification of Netware or Microsoft
60  * NetBIOS over IPX packets. I have had to decode the protocol myself,
61  * so there are holes and perhaps errors in this code. (gram)
62  *
63  * A list of "NovelNetBIOS" packet types can be found at
64  *
65  *      http://www.protocols.com/pbook/novel.htm#NetBIOS
66  *
67  * and at least some of those packet types appear to match what's in
68  * some NBIPX packets.
69  *
70  * Note, however, that the offset of the packet type in an NBIPX packet
71  * *DEPENDS ON THE PACKET TYPE*; "Find name" and "Name recognized" have
72  * it at one offset, "Directed datagram" has it at another.  Does the
73  * NBIPX code base it on the length, or what?  Non-broadcast directed
74  * datagram packets have an IPX type of "IPX", just as "Find name" and
75  * "Name recognized" do....  For now, we base it on the length.
76  */
77 #define NBIPX_FIND_NAME         1
78 #define NBIPX_NAME_RECOGNIZED   2
79 #define NBIPX_CHECK_NAME        3
80 #define NBIPX_NAME_IN_USE       4
81 #define NBIPX_DEREGISTER_NAME   5
82 #define NBIPX_SESSION_DATA      6
83 #define NBIPX_SESSION_END       7
84 #define NBIPX_SESSION_END_ACK   8
85 #define NBIPX_STATUS_QUERY      9
86 #define NBIPX_STATUS_RESPONSE   10
87 #define NBIPX_DIRECTED_DATAGRAM 11
88
89 static const value_string nbipx_data_stream_type_vals[] = {
90         {NBIPX_FIND_NAME,               "Find name"},
91         {NBIPX_NAME_RECOGNIZED,         "Name recognized"},
92         {NBIPX_CHECK_NAME,              "Check name"},
93         {NBIPX_NAME_IN_USE,             "Name in use"},
94         {NBIPX_DEREGISTER_NAME,         "Deregister name"},
95         {NBIPX_SESSION_DATA,            "Session data"},
96         {NBIPX_SESSION_END,             "Session end"},
97         {NBIPX_SESSION_END_ACK,         "Session end ACK"},
98         {NBIPX_STATUS_QUERY,            "Status query"},
99         {NBIPX_STATUS_RESPONSE,         "Status response"},
100         {NBIPX_DIRECTED_DATAGRAM,       "Directed datagram"},
101         {0,                             NULL}
102 };
103
104 #define NWLINK_NAME_QUERY       1
105 #define NWLINK_SMB              2
106 #define NWLINK_NETBIOS_DATAGRAM 3
107
108 static const value_string nwlink_data_stream_type_vals[] = {
109         {NWLINK_NAME_QUERY,             "Name query"},
110         {NWLINK_SMB,                    "SMB"},
111         {NWLINK_NETBIOS_DATAGRAM,       "NetBIOS datagram"},
112         {0,                             NULL}
113 };
114
115 /* NetWare */
116 static void
117 dissect_nbipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
118 {
119         CHECK_DISPLAY_AS_DATA(proto_nbipx, tvb, pinfo, tree);
120
121         pinfo->current_proto = "NBIPX";
122
123         if (check_col(pinfo->fd, COL_PROTOCOL))
124                 col_set_str(pinfo->fd, COL_PROTOCOL, "NBIPX");
125         if (check_col(pinfo->fd, COL_INFO))
126                 col_clear(pinfo->fd, COL_INFO);
127
128         /*
129          * As said above, we look at the length of the packet to decide
130          * whether to treat it as a name-service packet or a datagram
131          * (the packet type would tell us, but it's at a *DIFFERENT
132          * LOCATION* in different types of packet...).
133          */
134         if (tvb_reported_length(tvb) == 50)
135                 dissect_nbipx_ns(tvb, pinfo, tree);
136         else
137                 dissect_nbipx_dg(tvb, pinfo, tree);
138 }
139
140 static void
141 add_routers(proto_tree *tree, tvbuff_t *tvb, int offset)
142 {
143         int             i;
144         int             rtr_offset;
145         guint32         router;
146
147         /* Eight routers are listed */
148         for (i = 0; i < 8; i++) {
149                 rtr_offset = offset + (i << 2);
150                 tvb_memcpy(tvb, (guint8 *)&router, rtr_offset, 4);
151                 if (router != 0) {
152                         proto_tree_add_text(tree, tvb, rtr_offset, 4,
153                             "IPX Network: %s",
154                             ipxnet_to_string((guint8*)&router));
155                 }
156         }
157 }
158
159 static void
160 dissect_nbipx_ns(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
161 {
162         proto_tree              *nbipx_tree;
163         proto_item              *ti;
164         int                     offset = 0;
165         guint8                  packet_type;
166         guint8                  name_type_flag;
167         proto_tree              *name_type_flag_tree;
168         proto_item              *tf;
169         char                    name[(NETBIOS_NAME_LEN - 1)*4 + 1];
170         int                     name_type;
171
172         name_type_flag = tvb_get_guint8(tvb, offset+32);
173         packet_type = tvb_get_guint8(tvb, offset+33);
174         name_type = get_netbios_name(tvb, offset+34, name);
175
176         if (check_col(pinfo->fd, COL_INFO)) {
177                 switch (packet_type) {
178                 case NBIPX_FIND_NAME:
179                 case NBIPX_NAME_RECOGNIZED:
180                 case NBIPX_CHECK_NAME:
181                 case NBIPX_NAME_IN_USE:
182                 case NBIPX_DEREGISTER_NAME:
183                         col_add_fstr(pinfo->fd, COL_INFO, "%s %s<%02x>",
184                                 val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"),
185                                 name, name_type);
186                         break;
187
188                 default:
189                         col_add_fstr(pinfo->fd, COL_INFO, "%s",
190                                 val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"));
191                         break;
192                 }
193         }
194
195         if (tree) {
196                 ti = proto_tree_add_item(tree, proto_nbipx, tvb, offset, 50,
197                     FALSE);
198                 nbipx_tree = proto_item_add_subtree(ti, ett_nbipx);
199
200                 add_routers(nbipx_tree, tvb, offset);
201
202                 tf = proto_tree_add_text(nbipx_tree, tvb, offset+32, 1,
203                         "Name type flag: 0x%02x", name_type_flag);
204                 name_type_flag_tree = proto_item_add_subtree(tf,
205                                 ett_nbipx_name_type_flags);
206                 proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
207                     1, "%s",
208                     decode_boolean_bitfield(name_type_flag, 0x80, 8,
209                       "Group name", "Unique name"));
210                 proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
211                     1, "%s",
212                     decode_boolean_bitfield(name_type_flag, 0x40, 8,
213                       "Name in use", "Name not used"));
214                 proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
215                     1, "%s",
216                     decode_boolean_bitfield(name_type_flag, 0x04, 8,
217                       "Name registered", "Name not registered"));
218                 proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
219                     1, "%s",
220                     decode_boolean_bitfield(name_type_flag, 0x02, 8,
221                       "Name duplicated", "Name not duplicated"));
222                 proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
223                     1, "%s",
224                     decode_boolean_bitfield(name_type_flag, 0x01, 8,
225                       "Name deregistered", "Name not deregistered"));
226
227                 proto_tree_add_text(nbipx_tree, tvb, offset+33, 1,
228                         "Packet Type: %s (%02X)",
229                         val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"),
230                         packet_type);
231
232                 netbios_add_name("Name", tvb, offset + 34, nbipx_tree);
233         }
234 }
235
236 static void
237 dissect_nbipx_dg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
238 {
239         proto_tree                      *nbipx_tree;
240         proto_item                      *ti;
241         int                             offset = 0;
242         guint8                          conn_control;
243         proto_tree                      *cc_tree;
244         guint8                          packet_type;
245         tvbuff_t                        *next_tvb;
246         const guint8                    *next_pd;
247         int                             next_offset;
248
249         if (check_col(pinfo->fd, COL_INFO))
250                 col_add_fstr(pinfo->fd, COL_INFO, "NetBIOS datagram over NBIPX");
251
252         if (tree) {
253                 ti = proto_tree_add_item(tree, proto_nbipx, tvb, offset,
254                     2+NETBIOS_NAME_LEN+NETBIOS_NAME_LEN, FALSE);
255                 nbipx_tree = proto_item_add_subtree(ti, ett_nbipx);
256
257                 conn_control = tvb_get_guint8(tvb, offset);
258                 ti = proto_tree_add_text(nbipx_tree, tvb, offset, 1,
259                     "Connection control: 0x%02x", conn_control);
260                 cc_tree = proto_item_add_subtree(ti, ett_nbipx_conn_ctrl);
261                 proto_tree_add_text(cc_tree, tvb, offset, 1, "%s",
262                       decode_boolean_bitfield(conn_control, 0x80, 8,
263                               "System packet", "Non-system packet"));
264                 proto_tree_add_text(cc_tree, tvb, offset, 1, "%s",
265                       decode_boolean_bitfield(conn_control, 0x40, 8,
266                               "Send acknowledge", "No send acknowledge"));
267                 proto_tree_add_text(cc_tree, tvb, offset, 1, "%s",
268                       decode_boolean_bitfield(conn_control, 0x20, 8,
269                               "Attention", "No attention"));
270                 proto_tree_add_text(cc_tree, tvb, offset, 1, "%s",
271                       decode_boolean_bitfield(conn_control, 0x10, 8,
272                               "End of message", "No end of message"));
273                 proto_tree_add_text(cc_tree, tvb, offset, 1, "%s",
274                       decode_boolean_bitfield(conn_control, 0x08, 8,
275                               "Resend", "No resend"));
276                 offset += 1;
277
278                 packet_type = tvb_get_guint8(tvb, offset);
279                 proto_tree_add_text(nbipx_tree, tvb, offset, 1,
280                                 "Packet Type: %s (%02X)",
281                                 val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"),
282                                 packet_type);
283                 offset += 1;
284
285                 if (!netbios_add_name("Receiver's Name", tvb, offset,
286                     nbipx_tree))
287                         return;
288                 offset += NETBIOS_NAME_LEN;
289
290                 if (!netbios_add_name("Sender's Name", tvb, offset,
291                     nbipx_tree))
292                         return;
293                 offset += NETBIOS_NAME_LEN;
294
295                 if (tvb_offset_exists(tvb, offset)) {
296                         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
297                         tvb_compat(next_tvb, &next_pd, &next_offset);
298                         dissect_smb(next_pd, next_offset, pinfo->fd, tree,
299                             tvb_length(next_tvb));
300                 }
301         }
302 }
303
304 static void
305 dissect_nwlink_dg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
306 {
307         proto_tree      *nbipx_tree;
308         proto_item      *ti;
309         int             offset = 0;
310         guint8          packet_type;
311         guint8          name_type_flag;
312         proto_tree      *name_type_flag_tree;
313         proto_item      *tf;
314         char            name[(NETBIOS_NAME_LEN - 1)*4 + 1];
315         int             name_type;
316         char            node_name[(NETBIOS_NAME_LEN - 1)*4 + 1];
317         int             node_name_type = 0;
318         tvbuff_t        *next_tvb;
319         const guint8    *next_pd;
320         int             next_offset;
321
322         name_type_flag = tvb_get_guint8(tvb, offset+32);
323         packet_type = tvb_get_guint8(tvb, offset+33);
324         name_type = get_netbios_name(tvb, offset+36, name);
325         node_name_type = get_netbios_name(tvb, offset+52, node_name);
326
327         if (check_col(pinfo->fd, COL_PROTOCOL))
328                 col_set_str(pinfo->fd, COL_PROTOCOL, "NWLink");
329
330         if (check_col(pinfo->fd, COL_INFO)) {
331                 /*
332                  * XXX - Microsoft Network Monitor thinks that the octet
333                  * at 32 is a packet type, e.g. "mailslot write" for
334                  * browser announcements, and that the octet at 33 is a
335                  * name type, in the sense of the 16th byte of a
336                  * NetBIOS name.
337                  *
338                  * A name type of 2 shows up in a "host announcement",
339                  * and a name type of 3 shows up in a "local master
340                  * annoumcement", so maybe that field really *is* a
341                  * name type - the fact that it's not associated with
342                  * any of the NetBIOS names in the packet nonwithstanding.
343                  *
344                  * I haven't seen any packets with the name type octet
345                  * being anything other than 2 or 3, so I don't know
346                  * whether those are name service operations; however,
347                  * given that NWLink, unlike socket-0x0455 NBIPX,
348                  * has separate sockets for name queries and datagrams,
349                  * it may be that this really is a name type, and that
350                  * these are all datagrams, not name queries.
351                  */
352                 switch (packet_type) {
353                 case NWLINK_NAME_QUERY:
354                         col_add_fstr(pinfo->fd, COL_INFO, "Name Query for %s<%02x>",
355                                         name, name_type);
356                         break;
357
358                 case NWLINK_SMB:
359                         /* Session? */
360                         col_add_fstr(pinfo->fd, COL_INFO, "SMB over NBIPX");
361                         break;
362
363                 case NWLINK_NETBIOS_DATAGRAM:
364                         /* Datagram? (Where did we see this?) */
365                         col_add_fstr(pinfo->fd, COL_INFO, "NetBIOS datagram over NBIPX");
366                         break;
367                                 
368                 default:
369                         col_set_str(pinfo->fd, COL_INFO, "NetBIOS over IPX (NWLink)");
370                         break;
371                 }
372         }
373
374         if (tree) {
375                 ti = proto_tree_add_item(tree, proto_nbipx, tvb, offset, 68, FALSE);
376                 nbipx_tree = proto_item_add_subtree(ti, ett_nbipx);
377
378                 add_routers(nbipx_tree, tvb, offset);
379
380                 /*
381                  * XXX - is "packet_type" really a packet type?  See
382                  * above.
383                  */
384                 if (packet_type != NWLINK_SMB &&
385                       packet_type != NWLINK_NETBIOS_DATAGRAM) {
386                         tf = proto_tree_add_text(nbipx_tree, tvb, offset+32, 1,
387                                 "Name type flag: 0x%02x",
388                                 name_type_flag);
389                         name_type_flag_tree = proto_item_add_subtree(tf,
390                                         ett_nbipx_name_type_flags);
391                         proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
392                             1, "%s",
393                             decode_boolean_bitfield(name_type_flag, 0x80, 8,
394                               "Group name", "Unique name"));
395                         proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
396                             1, "%s",
397                             decode_boolean_bitfield(name_type_flag, 0x40, 8,
398                               "Name in use", "Name not used"));
399                         proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
400                             1, "%s",
401                             decode_boolean_bitfield(name_type_flag, 0x04, 8,
402                               "Name registered", "Name not registered"));
403                         proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
404                             1, "%s",
405                             decode_boolean_bitfield(name_type_flag, 0x02, 8,
406                               "Name duplicated", "Name not duplicated"));
407                         proto_tree_add_text(name_type_flag_tree, tvb, offset+32,
408                             1, "%s",
409                             decode_boolean_bitfield(name_type_flag, 0x01, 8,
410                               "Name deregistered", "Name not deregistered"));
411
412                         if (!netbios_add_name("Group name", tvb, offset+36,
413                             nbipx_tree))
414                                 return;
415                         if (!netbios_add_name("Node name", tvb, offset+52,
416                             nbipx_tree))
417                                 return;
418                         proto_tree_add_text(nbipx_tree, tvb, offset+33, 1,
419                             "Packet Type: %s (%02X)",
420                             val_to_str(packet_type, nwlink_data_stream_type_vals, "Unknown"),
421                             packet_type);
422                 } else {
423                         proto_tree_add_text(nbipx_tree, tvb, offset+32, 1,
424                             "Packet type: 0x%02x", name_type_flag);
425                         proto_tree_add_text(nbipx_tree, tvb, offset+33, 1,
426                             "Name Type: %s (0x%02x)",
427                             netbios_name_type_descr(packet_type),
428                             packet_type);
429                         proto_tree_add_text(nbipx_tree, tvb, offset+34, 2,
430                             "Message ID: 0x%04x",
431                             tvb_get_letohs(tvb, offset+34));
432                         if (!netbios_add_name("Requested name", tvb, offset+36,
433                             nbipx_tree))
434                                 return;
435                         if (!netbios_add_name("Source name", tvb, offset+52,
436                             nbipx_tree))
437                                 return;
438                 }
439         }
440
441         offset += 68;
442
443         if (tvb_offset_exists(tvb, offset)) {
444                 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
445
446                 switch (packet_type) {
447                 case NWLINK_SMB:
448                 case NWLINK_NETBIOS_DATAGRAM:
449                         tvb_compat(next_tvb, &next_pd, &next_offset);
450                         dissect_smb(next_pd, next_offset, pinfo->fd, tree,
451                             tvb_length(next_tvb));
452                         break;
453                                 
454                 default:
455                         dissect_data(next_tvb, 0, pinfo, tree);
456                         break;
457                 }
458         }
459 }
460
461 void
462 proto_register_nbipx(void)
463 {
464 /*        static hf_register_info hf[] = {
465                 { &variable,
466                 { "Name",           "nbipx.abbreviation", TYPE, VALS_POINTER }},
467         };*/
468         static gint *ett[] = {
469                 &ett_nbipx,
470                 &ett_nbipx_conn_ctrl,
471                 &ett_nbipx_name_type_flags,
472         };
473
474         proto_nbipx = proto_register_protocol("NetBIOS over IPX",
475             "NBIPX", "nbipx");
476  /*       proto_register_field_array(proto_nbipx, hf, array_length(hf));*/
477         proto_register_subtree_array(ett, array_length(ett));
478
479         register_dissector("nbipx", dissect_nbipx, proto_nbipx);
480 }
481
482 void
483 proto_reg_handoff_nbipx(void)
484 {
485         dissector_add("ipx.socket", IPX_SOCKET_NWLINK_SMB_DGRAM,
486             dissect_nwlink_dg, proto_nbipx);
487 }