Add a bunch more NetBIOS name types.
[obnox/wireshark/wip.git] / packet-nbipx.c
1 /* packet-nbipx.c
2  * Routines for NetBIOS over IPX packet disassembly
3  * Gilbert Ramirez <gram@verdict.uthscsa.edu>
4  *
5  * $Id: packet-nbipx.c,v 1.12 1999/09/03 00:24:40 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 <glib.h>
36 #include "packet.h"
37 #include "packet-ipx.h" /* for ipxnet_to_string() */
38 #include "packet-netbios.h"
39
40 static int proto_nbipx = -1;
41
42 enum nbipx_protocol {
43         NETBIOS_NETWARE,
44         NETBIOS_NWLINK
45 };
46
47 static void
48 nbipx_ns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
49                 enum nbipx_protocol nbipx, int max_data);
50 static void
51 dissect_nbipx_dg(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
52                 int max_data);
53
54 /* There is no RFC or public specification of Netware or Microsoft
55  * NetBIOS over IPX packets. I have had to decode the protocol myself,
56  * so there are holes and perhaps errors in this code. (gram)
57  *
58  * A list of "NovelNetBIOS" packet types can be found at
59  *
60  *      http://www.protocols.com/pbook/novel.htm#NetBIOS
61  *
62  * and at least some of those packet types appear to match what's in
63  * some NBIPX packets.
64  *
65  * Note, however, that the offset of the packet type in an NBIPX packet
66  * *DEPENDS ON THE PACKET TYPE*; "Find name" and "Name recognized" have
67  * it at one offset, "Directed datagram" has it at another.  Does the
68  * NBIPX code base it on the length, or what?  Non-broadcast directed
69  * datagram packets have an IPX type of "IPX", just as "Find name" and
70  * "Name recognized" do....  For now, we base it on the length.
71  */
72 #define NBIPX_FIND_NAME         1
73 #define NBIPX_NAME_RECOGNIZED   2
74 #define NBIPX_CHECK_NAME        3
75 #define NBIPX_NAME_IN_USE       4
76 #define NBIPX_DEREGISTER_NAME   5
77 #define NBIPX_SESSION_DATA      6
78 #define NBIPX_SESSION_END       7
79 #define NBIPX_SESSION_END_ACK   8
80 #define NBIPX_STATUS_QUERY      9
81 #define NBIPX_STATUS_RESPONSE   10
82 #define NBIPX_DIRECTED_DATAGRAM 11
83
84 static const value_string nbipx_data_stream_type_vals[] = {
85         {NBIPX_FIND_NAME,               "Find name"},
86         {NBIPX_NAME_RECOGNIZED,         "Name recognized"},
87         {NBIPX_CHECK_NAME,              "Check name"},
88         {NBIPX_NAME_IN_USE,             "Name in use"},
89         {NBIPX_DEREGISTER_NAME,         "Deregister name"},
90         {NBIPX_SESSION_DATA,            "Session data"},
91         {NBIPX_SESSION_END,             "Session end"},
92         {NBIPX_SESSION_END_ACK,         "Session end ACK"},
93         {NBIPX_STATUS_QUERY,            "Status query"},
94         {NBIPX_STATUS_RESPONSE,         "Status response"},
95         {NBIPX_DIRECTED_DATAGRAM,       "Directed datagram"},
96         {0,                             NULL}
97 };
98
99 #define NWLINK_NAME_QUERY       1
100 #define NWLINK_SMB              2
101 #define NWLINK_NETBIOS_DATAGRAM 3
102
103 static const value_string nwlink_data_stream_type_vals[] = {
104         {NWLINK_NAME_QUERY,             "Name query"},
105         {NWLINK_SMB,                    "SMB"},
106         {NWLINK_NETBIOS_DATAGRAM,       "NetBIOS datagram"},
107         {0,                             NULL}
108 };
109
110 /* NetWare */
111 void
112 dissect_nbipx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
113 {
114         int     max_data = pi.captured_len - offset;
115
116         /*
117          * As said above, we look at the length of the packet to decide
118          * whether to treat it as a name-service packet or a datagram
119          * (the packet type would tell us, but it's at a *DIFFERENT
120          * LOCATION* in different types of packet...).
121          */
122         if (END_OF_FRAME == 50)
123                 nbipx_ns(pd, offset, fd, tree, NETBIOS_NETWARE, max_data);
124         else
125                 dissect_nbipx_dg(pd, offset, fd, tree, max_data);
126 }
127
128 void
129 dissect_nwlink_dg(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
130 {
131         int     max_data = pi.captured_len - offset;
132
133         nbipx_ns(pd, offset, fd, tree, NETBIOS_NWLINK, max_data);
134 }
135
136
137 static void
138 nbipx_ns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
139                 enum nbipx_protocol nbipx, int max_data)
140 {
141         proto_tree              *nbipx_tree;
142         proto_item              *ti;
143         int                     i;
144         guint8                  name_flags;
145         guint8                  packet_type;
146         int                     name_offset;
147         char                    name[(NETBIOS_NAME_LEN - 1)*4 + 1];
148         int                     name_type;
149         char                    node_name[(NETBIOS_NAME_LEN - 1)*4 + 1];
150         int                     node_name_type = 0;
151         int                     rtr_offset;
152         guint32                 router[8];
153
154         if (nbipx == NETBIOS_NETWARE) {
155                 name_offset = 34;
156         }
157         else {
158                 name_offset = 36;
159         }
160
161
162         name_flags = pd[offset+32];
163         packet_type = pd[offset+33];
164         name_type = get_netbios_name(pd, offset+name_offset, name);
165
166         if (nbipx == NETBIOS_NWLINK)
167                 node_name_type = get_netbios_name(pd, offset+52, node_name);
168
169         if (check_col(fd, COL_PROTOCOL)) {
170                 if (nbipx == NETBIOS_NETWARE) {
171                         col_add_str(fd, COL_PROTOCOL, "NBIPX");
172                 }
173                 else {
174                         col_add_str(fd, COL_PROTOCOL, "NWLink");
175                 }
176         }
177
178         if (check_col(fd, COL_INFO)) {
179                 if (nbipx == NETBIOS_NETWARE) {
180                         switch (packet_type) {
181                         case NBIPX_FIND_NAME:
182                         case NBIPX_NAME_RECOGNIZED:
183                         case NBIPX_CHECK_NAME:
184                         case NBIPX_NAME_IN_USE:
185                         case NBIPX_DEREGISTER_NAME:
186                                 col_add_fstr(fd, COL_INFO, "%s %s<%02x>",
187                                         val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"),
188                                         name, name_type);
189                                 break;
190
191                         default:
192                                 col_add_fstr(fd, COL_INFO, "%s",
193                                         val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"));
194                                 break;
195                         }
196                 }
197                 else {
198                         switch (packet_type) {
199                         case NWLINK_NAME_QUERY:
200                                 col_add_fstr(fd, COL_INFO, "Name Query for %s<%02x>",
201                                                 name, name_type);
202                                 break;
203
204                         case NWLINK_SMB:
205                                 /* Session? */
206                                 col_add_fstr(fd, COL_INFO, "SMB over NBIPX");
207                                 break;
208
209                         case NWLINK_NETBIOS_DATAGRAM:
210                                 /* Datagram? (Where did we see this?) */
211                                 col_add_fstr(fd, COL_INFO, "NetBIOS datagram over NBIPX");
212                                 break;
213                                 
214                         default:
215                                 col_add_str(fd, COL_INFO, "NetBIOS over IPX (NWLink)");
216                                 break;
217                         }
218                 }
219         }
220
221         if (tree) {
222                 ti = proto_tree_add_item(tree, proto_nbipx, offset, 68, NULL);
223                 nbipx_tree = proto_item_add_subtree(ti, ETT_NBIPX);
224
225                 if (nbipx == NETBIOS_NETWARE) {
226                         proto_tree_add_text(nbipx_tree, offset+33, 1,
227                                 "Packet Type: %s (%02X)",
228                                 val_to_str(packet_type, nbipx_data_stream_type_vals, "Unknown"),
229                                 packet_type);
230                 } else {
231                         proto_tree_add_text(nbipx_tree, offset+33, 1,
232                                 "Packet Type: %s (%02X)",
233                                 val_to_str(packet_type, nwlink_data_stream_type_vals, "Unknown"),
234                                 packet_type);
235                 }
236
237                 /* Eight routers are listed */
238                 for (i = 0; i < 8; i++) {
239                         rtr_offset = offset + (i << 2);
240                         memcpy(&router[i], &pd[rtr_offset], 4);
241                         if (router[i] != 0) {
242                                 proto_tree_add_text(nbipx_tree, rtr_offset, 4, "IPX Network: %s",
243                                                 ipxnet_to_string((guint8*)&router[i]));
244                         }
245                 }
246
247                 proto_tree_add_text(nbipx_tree, offset+32, 1, "Name Type: %02X",
248                                 name_flags);
249
250                 if (nbipx == NETBIOS_NETWARE) {
251                         netbios_add_name("Name", &pd[offset], offset,
252                                         name_offset, nbipx_tree);
253                 }
254                 else {
255                         netbios_add_name("Group name", &pd[offset], offset,
256                                         name_offset, nbipx_tree);
257                         netbios_add_name("Node name", &pd[offset], offset,
258                                         52, nbipx_tree);
259                 }
260         }
261
262         if (nbipx == NETBIOS_NWLINK) {
263                 switch (packet_type) {
264                         case NWLINK_SMB:
265                         case NWLINK_NETBIOS_DATAGRAM:
266                                 dissect_smb(pd, offset + 68, fd, tree, max_data - 68);
267                                 break;
268                                 
269                         default:
270                                 dissect_data(pd, offset + 68, fd, tree);
271                                 break;
272                 }
273         }
274 }
275
276 static void
277 dissect_nbipx_dg(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
278                 int max_data)
279 {
280         proto_tree                      *nbipx_tree;
281         proto_item                      *ti;
282
283         if (check_col(fd, COL_PROTOCOL))
284                 col_add_str(fd, COL_PROTOCOL, "NetBIOS");
285
286         if (check_col(fd, COL_INFO))
287                 col_add_fstr(fd, COL_INFO, "NetBIOS datagram over NBIPX");
288
289         if (tree) {
290                 ti = proto_tree_add_item(tree, proto_nbipx, offset, 68, NULL);
291                 nbipx_tree = proto_item_add_subtree(ti, ETT_NBIPX);
292
293                 proto_tree_add_text(nbipx_tree, offset+1, 1,
294                                 "Packet Type: %s (%02X)",
295                                 val_to_str(pd[offset+1], nbipx_data_stream_type_vals, "Unknown"),
296                                 pd[offset+1]);
297                 proto_tree_add_text(nbipx_tree, offset, 1,
298                     "Connection control: 0x%02x", pd[offset]);
299                 netbios_add_name("Receiver's Name", &pd[offset],
300                     offset, 2, nbipx_tree);
301                 netbios_add_name("Sender's Name", &pd[offset],
302                     offset, 2+16, nbipx_tree);
303
304                 dissect_smb(pd, offset+2+16+16, fd, tree, max_data - 2+16+16);
305         }
306 }
307
308 void
309 proto_register_nbipx(void)
310 {
311 /*        static hf_register_info hf[] = {
312                 { &variable,
313                 { "Name",           "nbipx.abbreviation", TYPE, VALS_POINTER }},
314         };*/
315
316         proto_nbipx = proto_register_protocol("NetBIOS over IPX", "nbipx");
317  /*       proto_register_field_array(proto_nbipx, hf, array_length(hf));*/
318 }