xref: /netbsd-src/external/bsd/tcpdump/dist/print-smb.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*
2  * Copyright (C) Andrew Tridgell 1995-1999
3  *
4  * This software may be distributed either under the terms of the
5  * BSD-style license that accompanies tcpdump or the GNU GPL version 2
6  * or later
7  */
8 
9 #include <sys/cdefs.h>
10 #ifndef lint
11 __RCSID("$NetBSD: print-smb.c,v 1.8 2019/10/01 16:06:16 christos Exp $");
12 #endif
13 
14 /* \summary: SMB/CIFS printer */
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include <netdissect-stdinc.h>
21 
22 #include <string.h>
23 
24 #include "netdissect.h"
25 #include "extract.h"
26 #include "smb.h"
27 
28 static const char tstr[] = "[|SMB]";
29 
30 static int request = 0;
31 static int unicodestr = 0;
32 
33 const u_char *startbuf = NULL;
34 
35 struct smbdescript {
36     const char *req_f1;
37     const char *req_f2;
38     const char *rep_f1;
39     const char *rep_f2;
40     void (*fn)(netdissect_options *, const u_char *, const u_char *, const u_char *, const u_char *);
41 };
42 
43 struct smbdescriptint {
44     const char *req_f1;
45     const char *req_f2;
46     const char *rep_f1;
47     const char *rep_f2;
48     void (*fn)(netdissect_options *, const u_char *, const u_char *, int, int);
49 };
50 
51 struct smbfns
52 {
53     int id;
54     const char *name;
55     int flags;
56     struct smbdescript descript;
57 };
58 
59 struct smbfnsint
60 {
61     int id;
62     const char *name;
63     int flags;
64     struct smbdescriptint descript;
65 };
66 
67 #define DEFDESCRIPT	{ NULL, NULL, NULL, NULL, NULL }
68 
69 #define FLG_CHAIN	(1 << 0)
70 
71 static const struct smbfns *
72 smbfind(int id, const struct smbfns *list)
73 {
74     int sindex;
75 
76     for (sindex = 0; list[sindex].name; sindex++)
77 	if (list[sindex].id == id)
78 	    return(&list[sindex]);
79 
80     return(&list[0]);
81 }
82 
83 static const struct smbfnsint *
84 smbfindint(int id, const struct smbfnsint *list)
85 {
86     int sindex;
87 
88     for (sindex = 0; list[sindex].name; sindex++)
89 	if (list[sindex].id == id)
90 	    return(&list[sindex]);
91 
92     return(&list[0]);
93 }
94 
95 static void
96 trans2_findfirst(netdissect_options *ndo,
97                  const u_char *param, const u_char *data, int pcnt, int dcnt)
98 {
99     const char *fmt;
100 
101     if (request)
102 	fmt = "Attribute=[A]\nSearchCount=[d]\nFlags=[w]\nLevel=[dP4]\nFile=[S]\n";
103     else
104 	fmt = "Handle=[w]\nCount=[d]\nEOS=[w]\nEoffset=[d]\nLastNameOfs=[w]\n";
105 
106     smb_fdata(ndo, param, fmt, param + pcnt, unicodestr);
107     if (dcnt) {
108 	ND_PRINT((ndo, "data:\n"));
109 	smb_print_data(ndo, data, dcnt);
110     }
111 }
112 
113 static void
114 trans2_qfsinfo(netdissect_options *ndo,
115                const u_char *param, const u_char *data, int pcnt, int dcnt)
116 {
117     static int level = 0;
118     const char *fmt="";
119 
120     if (request) {
121 	ND_TCHECK2(*param, 2);
122 	level = EXTRACT_LE_16BITS(param);
123 	fmt = "InfoLevel=[d]\n";
124 	smb_fdata(ndo, param, fmt, param + pcnt, unicodestr);
125     } else {
126 	switch (level) {
127 	case 1:
128 	    fmt = "idFileSystem=[W]\nSectorUnit=[D]\nUnit=[D]\nAvail=[D]\nSectorSize=[d]\n";
129 	    break;
130 	case 2:
131 	    fmt = "CreationTime=[T2]VolNameLength=[lb]\nVolumeLabel=[c]\n";
132 	    break;
133 	case 0x105:
134 	    fmt = "Capabilities=[W]\nMaxFileLen=[D]\nVolNameLen=[lD]\nVolume=[C]\n";
135 	    break;
136 	default:
137 	    fmt = "UnknownLevel\n";
138 	    break;
139 	}
140 	smb_fdata(ndo, data, fmt, data + dcnt, unicodestr);
141     }
142     if (dcnt) {
143 	ND_PRINT((ndo, "data:\n"));
144 	smb_print_data(ndo, data, dcnt);
145     }
146     return;
147 trunc:
148     ND_PRINT((ndo, "%s", tstr));
149 }
150 
151 static const struct smbfnsint trans2_fns[] = {
152     { 0, "TRANSACT2_OPEN", 0,
153 	{ "Flags2=[w]\nMode=[w]\nSearchAttrib=[A]\nAttrib=[A]\nTime=[T2]\nOFun=[w]\nSize=[D]\nRes=([w, w, w, w, w])\nPath=[S]",
154 	  NULL,
155 	  "Handle=[d]\nAttrib=[A]\nTime=[T2]\nSize=[D]\nAccess=[w]\nType=[w]\nState=[w]\nAction=[w]\nInode=[W]\nOffErr=[d]\n|EALength=[d]\n",
156 	  NULL, NULL }},
157     { 1, "TRANSACT2_FINDFIRST", 0,
158 	{ NULL, NULL, NULL, NULL, trans2_findfirst }},
159     { 2, "TRANSACT2_FINDNEXT", 0, DEFDESCRIPT },
160     { 3, "TRANSACT2_QFSINFO", 0,
161 	{ NULL, NULL, NULL, NULL, trans2_qfsinfo }},
162     { 4, "TRANSACT2_SETFSINFO", 0, DEFDESCRIPT },
163     { 5, "TRANSACT2_QPATHINFO", 0, DEFDESCRIPT },
164     { 6, "TRANSACT2_SETPATHINFO", 0, DEFDESCRIPT },
165     { 7, "TRANSACT2_QFILEINFO", 0, DEFDESCRIPT },
166     { 8, "TRANSACT2_SETFILEINFO", 0, DEFDESCRIPT },
167     { 9, "TRANSACT2_FSCTL", 0, DEFDESCRIPT },
168     { 10, "TRANSACT2_IOCTL", 0, DEFDESCRIPT },
169     { 11, "TRANSACT2_FINDNOTIFYFIRST", 0, DEFDESCRIPT },
170     { 12, "TRANSACT2_FINDNOTIFYNEXT", 0, DEFDESCRIPT },
171     { 13, "TRANSACT2_MKDIR", 0, DEFDESCRIPT },
172     { -1, NULL, 0, DEFDESCRIPT }
173 };
174 
175 
176 static void
177 print_trans2(netdissect_options *ndo,
178              const u_char *words, const u_char *dat, const u_char *buf, const u_char *maxbuf)
179 {
180     u_int bcc;
181     static const struct smbfnsint *fn = &trans2_fns[0];
182     const u_char *data, *param;
183     const u_char *w = words + 1;
184     const char *f1 = NULL, *f2 = NULL;
185     int pcnt, dcnt;
186 
187     ND_TCHECK(words[0]);
188     if (request) {
189 	ND_TCHECK2(w[14 * 2], 2);
190 	pcnt = EXTRACT_LE_16BITS(w + 9 * 2);
191 	param = buf + EXTRACT_LE_16BITS(w + 10 * 2);
192 	dcnt = EXTRACT_LE_16BITS(w + 11 * 2);
193 	data = buf + EXTRACT_LE_16BITS(w + 12 * 2);
194 	fn = smbfindint(EXTRACT_LE_16BITS(w + 14 * 2), trans2_fns);
195     } else {
196 	if (words[0] == 0) {
197 	    ND_PRINT((ndo, "%s\n", fn->name));
198 	    ND_PRINT((ndo, "Trans2Interim\n"));
199 	    return;
200 	}
201 	ND_TCHECK2(w[7 * 2], 2);
202 	pcnt = EXTRACT_LE_16BITS(w + 3 * 2);
203 	param = buf + EXTRACT_LE_16BITS(w + 4 * 2);
204 	dcnt = EXTRACT_LE_16BITS(w + 6 * 2);
205 	data = buf + EXTRACT_LE_16BITS(w + 7 * 2);
206     }
207 
208     ND_PRINT((ndo, "%s param_length=%d data_length=%d\n", fn->name, pcnt, dcnt));
209 
210     if (request) {
211 	if (words[0] == 8) {
212 	    smb_fdata(ndo, words + 1,
213 		"Trans2Secondary\nTotParam=[d]\nTotData=[d]\nParamCnt=[d]\nParamOff=[d]\nParamDisp=[d]\nDataCnt=[d]\nDataOff=[d]\nDataDisp=[d]\nHandle=[d]\n",
214 		maxbuf, unicodestr);
215 	    return;
216 	} else {
217 	    smb_fdata(ndo, words + 1,
218 		"TotParam=[d]\nTotData=[d]\nMaxParam=[d]\nMaxData=[d]\nMaxSetup=[b][P1]\nFlags=[w]\nTimeOut=[D]\nRes1=[w]\nParamCnt=[d]\nParamOff=[d]\nDataCnt=[d]\nDataOff=[d]\nSetupCnt=[b][P1]\n",
219 		words + 1 + 14 * 2, unicodestr);
220 	}
221 	f1 = fn->descript.req_f1;
222 	f2 = fn->descript.req_f2;
223     } else {
224 	smb_fdata(ndo, words + 1,
225 	    "TotParam=[d]\nTotData=[d]\nRes1=[w]\nParamCnt=[d]\nParamOff=[d]\nParamDisp[d]\nDataCnt=[d]\nDataOff=[d]\nDataDisp=[d]\nSetupCnt=[b][P1]\n",
226 	    words + 1 + 10 * 2, unicodestr);
227 	f1 = fn->descript.rep_f1;
228 	f2 = fn->descript.rep_f2;
229     }
230 
231     ND_TCHECK2(*dat, 2);
232     bcc = EXTRACT_LE_16BITS(dat);
233     ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
234     if (fn->descript.fn)
235 	(*fn->descript.fn)(ndo, param, data, pcnt, dcnt);
236     else {
237 	smb_fdata(ndo, param, f1 ? f1 : "Parameters=\n", param + pcnt, unicodestr);
238 	smb_fdata(ndo, data, f2 ? f2 : "Data=\n", data + dcnt, unicodestr);
239     }
240     return;
241 trunc:
242     ND_PRINT((ndo, "%s", tstr));
243 }
244 
245 static void
246 print_browse(netdissect_options *ndo,
247              const u_char *param, int paramlen, const u_char *data, int datalen)
248 {
249     const u_char *maxbuf = data + datalen;
250     int command;
251 
252     ND_TCHECK(data[0]);
253     command = data[0];
254 
255     smb_fdata(ndo, param, "BROWSE PACKET\n|Param ", param+paramlen, unicodestr);
256 
257     switch (command) {
258     case 0xF:
259 	data = smb_fdata(ndo, data,
260 	    "BROWSE PACKET:\nType=[B] (LocalMasterAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nElectionVersion=[w]\nBrowserConstant=[w]\n",
261 	    maxbuf, unicodestr);
262 	break;
263 
264     case 0x1:
265 	data = smb_fdata(ndo, data,
266 	    "BROWSE PACKET:\nType=[B] (HostAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nElectionVersion=[w]\nBrowserConstant=[w]\n",
267 	    maxbuf, unicodestr);
268 	break;
269 
270     case 0x2:
271 	data = smb_fdata(ndo, data,
272 	    "BROWSE PACKET:\nType=[B] (AnnouncementRequest)\nFlags=[B]\nReplySystemName=[S]\n",
273 	    maxbuf, unicodestr);
274 	break;
275 
276     case 0xc:
277 	data = smb_fdata(ndo, data,
278 	    "BROWSE PACKET:\nType=[B] (WorkgroupAnnouncement)\nUpdateCount=[w]\nRes1=[B]\nAnnounceInterval=[d]\nName=[n2]\nMajorVersion=[B]\nMinorVersion=[B]\nServerType=[W]\nCommentPointer=[W]\nServerName=[S]\n",
279 	    maxbuf, unicodestr);
280 	break;
281 
282     case 0x8:
283 	data = smb_fdata(ndo, data,
284 	    "BROWSE PACKET:\nType=[B] (ElectionFrame)\nElectionVersion=[B]\nOSSummary=[W]\nUptime=[(W, W)]\nServerName=[S]\n",
285 	    maxbuf, unicodestr);
286 	break;
287 
288     case 0xb:
289 	data = smb_fdata(ndo, data,
290 	    "BROWSE PACKET:\nType=[B] (BecomeBackupBrowser)\nName=[S]\n",
291 	    maxbuf, unicodestr);
292 	break;
293 
294     case 0x9:
295 	data = smb_fdata(ndo, data,
296 	    "BROWSE PACKET:\nType=[B] (GetBackupList)\nListCount?=[B]\nToken=[W]\n",
297 	    maxbuf, unicodestr);
298 	break;
299 
300     case 0xa:
301 	data = smb_fdata(ndo, data,
302 	    "BROWSE PACKET:\nType=[B] (BackupListResponse)\nServerCount?=[B]\nToken=[W]\n*Name=[S]\n",
303 	    maxbuf, unicodestr);
304 	break;
305 
306     case 0xd:
307 	data = smb_fdata(ndo, data,
308 	    "BROWSE PACKET:\nType=[B] (MasterAnnouncement)\nMasterName=[S]\n",
309 	    maxbuf, unicodestr);
310 	break;
311 
312     case 0xe:
313 	data = smb_fdata(ndo, data,
314 	    "BROWSE PACKET:\nType=[B] (ResetBrowser)\nOptions=[B]\n", maxbuf, unicodestr);
315 	break;
316 
317     default:
318 	data = smb_fdata(ndo, data, "Unknown Browser Frame ", maxbuf, unicodestr);
319 	break;
320     }
321     return;
322 trunc:
323     ND_PRINT((ndo, "%s", tstr));
324 }
325 
326 
327 static void
328 print_ipc(netdissect_options *ndo,
329           const u_char *param, int paramlen, const u_char *data, int datalen)
330 {
331     if (paramlen)
332 	smb_fdata(ndo, param, "Command=[w]\nStr1=[S]\nStr2=[S]\n", param + paramlen,
333 	    unicodestr);
334     if (datalen)
335 	smb_fdata(ndo, data, "IPC ", data + datalen, unicodestr);
336 }
337 
338 
339 static void
340 print_trans(netdissect_options *ndo,
341             const u_char *words, const u_char *data1, const u_char *buf, const u_char *maxbuf)
342 {
343     u_int bcc;
344     const char *f1, *f2, *f3, *f4;
345     const u_char *data, *param;
346     const u_char *w = words + 1;
347     int datalen, paramlen;
348 
349     if (request) {
350 	ND_TCHECK2(w[12 * 2], 2);
351 	paramlen = EXTRACT_LE_16BITS(w + 9 * 2);
352 	param = buf + EXTRACT_LE_16BITS(w + 10 * 2);
353 	datalen = EXTRACT_LE_16BITS(w + 11 * 2);
354 	data = buf + EXTRACT_LE_16BITS(w + 12 * 2);
355 	f1 = "TotParamCnt=[d] \nTotDataCnt=[d] \nMaxParmCnt=[d] \nMaxDataCnt=[d]\nMaxSCnt=[d] \nTransFlags=[w] \nRes1=[w] \nRes2=[w] \nRes3=[w]\nParamCnt=[d] \nParamOff=[d] \nDataCnt=[d] \nDataOff=[d] \nSUCnt=[d]\n";
356 	f2 = "|Name=[S]\n";
357 	f3 = "|Param ";
358 	f4 = "|Data ";
359     } else {
360 	ND_TCHECK2(w[7 * 2], 2);
361 	paramlen = EXTRACT_LE_16BITS(w + 3 * 2);
362 	param = buf + EXTRACT_LE_16BITS(w + 4 * 2);
363 	datalen = EXTRACT_LE_16BITS(w + 6 * 2);
364 	data = buf + EXTRACT_LE_16BITS(w + 7 * 2);
365 	f1 = "TotParamCnt=[d] \nTotDataCnt=[d] \nRes1=[d]\nParamCnt=[d] \nParamOff=[d] \nRes2=[d] \nDataCnt=[d] \nDataOff=[d] \nRes3=[d]\nLsetup=[d]\n";
366 	f2 = "|Unknown ";
367 	f3 = "|Param ";
368 	f4 = "|Data ";
369     }
370 
371     smb_fdata(ndo, words + 1, f1, min(words + 1 + 2 * words[0], maxbuf),
372         unicodestr);
373 
374     ND_TCHECK2(*data1, 2);
375     bcc = EXTRACT_LE_16BITS(data1);
376     ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
377     if (bcc > 0) {
378 	smb_fdata(ndo, data1 + 2, f2, maxbuf - (paramlen + datalen), unicodestr);
379 #define MAILSLOT_BROWSE_STR "\\MAILSLOT\\BROWSE"
380 	ND_TCHECK2(*(data1 + 2), strlen(MAILSLOT_BROWSE_STR) + 1);
381 	if (strcmp((const char *)(data1 + 2), MAILSLOT_BROWSE_STR) == 0) {
382 	    print_browse(ndo, param, paramlen, data, datalen);
383 	    return;
384 	}
385 #undef MAILSLOT_BROWSE_STR
386 
387 #define PIPE_LANMAN_STR "\\PIPE\\LANMAN"
388 	ND_TCHECK2(*(data1 + 2), strlen(PIPE_LANMAN_STR) + 1);
389 	if (strcmp((const char *)(data1 + 2), PIPE_LANMAN_STR) == 0) {
390 	    print_ipc(ndo, param, paramlen, data, datalen);
391 	    return;
392 	}
393 #undef PIPE_LANMAN_STR
394 
395 	if (paramlen)
396 	    smb_fdata(ndo, param, f3, min(param + paramlen, maxbuf), unicodestr);
397 	if (datalen)
398 	    smb_fdata(ndo, data, f4, min(data + datalen, maxbuf), unicodestr);
399     }
400     return;
401 trunc:
402     ND_PRINT((ndo, "%s", tstr));
403 }
404 
405 
406 static void
407 print_negprot(netdissect_options *ndo,
408               const u_char *words, const u_char *data, const u_char *buf _U_, const u_char *maxbuf)
409 {
410     u_int wct, bcc;
411     const char *f1 = NULL, *f2 = NULL;
412 
413     ND_TCHECK(words[0]);
414     wct = words[0];
415     if (request)
416 	f2 = "*|Dialect=[Y]\n";
417     else {
418 	if (wct == 1)
419 	    f1 = "Core Protocol\nDialectIndex=[d]";
420 	else if (wct == 17)
421 	    f1 = "NT1 Protocol\nDialectIndex=[d]\nSecMode=[B]\nMaxMux=[d]\nNumVcs=[d]\nMaxBuffer=[D]\nRawSize=[D]\nSessionKey=[W]\nCapabilities=[W]\nServerTime=[T3]TimeZone=[d]\nCryptKey=";
422 	else if (wct == 13)
423 	    f1 = "Coreplus/Lanman1/Lanman2 Protocol\nDialectIndex=[d]\nSecMode=[w]\nMaxXMit=[d]\nMaxMux=[d]\nMaxVcs=[d]\nBlkMode=[w]\nSessionKey=[W]\nServerTime=[T1]TimeZone=[d]\nRes=[W]\nCryptKey=";
424     }
425 
426     if (f1)
427 	smb_fdata(ndo, words + 1, f1, min(words + 1 + wct * 2, maxbuf),
428 	    unicodestr);
429     else
430 	smb_print_data(ndo, words + 1, min(wct * 2, PTR_DIFF(maxbuf, words + 1)));
431 
432     ND_TCHECK2(*data, 2);
433     bcc = EXTRACT_LE_16BITS(data);
434     ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
435     if (bcc > 0) {
436 	if (f2)
437 	    smb_fdata(ndo, data + 2, f2, min(data + 2 + EXTRACT_LE_16BITS(data),
438 		maxbuf), unicodestr);
439 	else
440 	    smb_print_data(ndo, data + 2, min(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));
441     }
442     return;
443 trunc:
444     ND_PRINT((ndo, "%s", tstr));
445 }
446 
447 static void
448 print_sesssetup(netdissect_options *ndo,
449                 const u_char *words, const u_char *data, const u_char *buf _U_, const u_char *maxbuf)
450 {
451     u_int wct, bcc;
452     const char *f1 = NULL, *f2 = NULL;
453 
454     ND_TCHECK(words[0]);
455     wct = words[0];
456     if (request) {
457 	if (wct == 10)
458 	    f1 = "Com2=[w]\nOff2=[d]\nBufSize=[d]\nMpxMax=[d]\nVcNum=[d]\nSessionKey=[W]\nPassLen=[d]\nCryptLen=[d]\nCryptOff=[d]\nPass&Name=\n";
459 	else
460 	    f1 = "Com2=[B]\nRes1=[B]\nOff2=[d]\nMaxBuffer=[d]\nMaxMpx=[d]\nVcNumber=[d]\nSessionKey=[W]\nCaseInsensitivePasswordLength=[d]\nCaseSensitivePasswordLength=[d]\nRes=[W]\nCapabilities=[W]\nPass1&Pass2&Account&Domain&OS&LanMan=\n";
461     } else {
462 	if (wct == 3) {
463 	    f1 = "Com2=[w]\nOff2=[d]\nAction=[w]\n";
464 	} else if (wct == 13) {
465 	    f1 = "Com2=[B]\nRes=[B]\nOff2=[d]\nAction=[w]\n";
466 	    f2 = "NativeOS=[S]\nNativeLanMan=[S]\nPrimaryDomain=[S]\n";
467 	}
468     }
469 
470     if (f1)
471 	smb_fdata(ndo, words + 1, f1, min(words + 1 + wct * 2, maxbuf),
472 	    unicodestr);
473     else
474 	smb_print_data(ndo, words + 1, min(wct * 2, PTR_DIFF(maxbuf, words + 1)));
475 
476     ND_TCHECK2(*data, 2);
477     bcc = EXTRACT_LE_16BITS(data);
478     ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
479     if (bcc > 0) {
480 	if (f2)
481 	    smb_fdata(ndo, data + 2, f2, min(data + 2 + EXTRACT_LE_16BITS(data),
482 		maxbuf), unicodestr);
483 	else
484 	    smb_print_data(ndo, data + 2, min(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));
485     }
486     return;
487 trunc:
488     ND_PRINT((ndo, "%s", tstr));
489 }
490 
491 static void
492 print_lockingandx(netdissect_options *ndo,
493                   const u_char *words, const u_char *data, const u_char *buf _U_, const u_char *maxbuf)
494 {
495     u_int wct, bcc;
496     const u_char *maxwords;
497     const char *f1 = NULL, *f2 = NULL;
498 
499     ND_TCHECK(words[0]);
500     wct = words[0];
501     if (request) {
502 	f1 = "Com2=[w]\nOff2=[d]\nHandle=[d]\nLockType=[w]\nTimeOut=[D]\nUnlockCount=[d]\nLockCount=[d]\n";
503 	ND_TCHECK(words[7]);
504 	if (words[7] & 0x10)
505 	    f2 = "*Process=[d]\n[P2]Offset=[M]\nLength=[M]\n";
506 	else
507 	    f2 = "*Process=[d]\nOffset=[D]\nLength=[D]\n";
508     } else {
509 	f1 = "Com2=[w]\nOff2=[d]\n";
510     }
511 
512     maxwords = min(words + 1 + wct * 2, maxbuf);
513     if (wct)
514 	smb_fdata(ndo, words + 1, f1, maxwords, unicodestr);
515 
516     ND_TCHECK2(*data, 2);
517     bcc = EXTRACT_LE_16BITS(data);
518     ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
519     if (bcc > 0) {
520 	if (f2)
521 	    smb_fdata(ndo, data + 2, f2, min(data + 2 + EXTRACT_LE_16BITS(data),
522 		maxbuf), unicodestr);
523 	else
524 	    smb_print_data(ndo, data + 2, min(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));
525     }
526     return;
527 trunc:
528     ND_PRINT((ndo, "%s", tstr));
529 }
530 
531 
532 static const struct smbfns smb_fns[] = {
533     { -1, "SMBunknown", 0, DEFDESCRIPT },
534 
535     { SMBtcon, "SMBtcon", 0,
536 	{ NULL, "Path=[Z]\nPassword=[Z]\nDevice=[Z]\n",
537 	  "MaxXmit=[d]\nTreeId=[d]\n", NULL,
538 	  NULL } },
539 
540     { SMBtdis, "SMBtdis", 0, DEFDESCRIPT },
541     { SMBexit,  "SMBexit", 0, DEFDESCRIPT },
542     { SMBioctl, "SMBioctl", 0, DEFDESCRIPT },
543 
544     { SMBecho, "SMBecho", 0,
545 	{ "ReverbCount=[d]\n", NULL,
546 	  "SequenceNum=[d]\n", NULL,
547 	  NULL } },
548 
549     { SMBulogoffX, "SMBulogoffX", FLG_CHAIN, DEFDESCRIPT },
550 
551     { SMBgetatr, "SMBgetatr", 0,
552 	{ NULL, "Path=[Z]\n",
553 	  "Attribute=[A]\nTime=[T2]Size=[D]\nRes=([w,w,w,w,w])\n", NULL,
554 	  NULL } },
555 
556     { SMBsetatr, "SMBsetatr", 0,
557 	{ "Attribute=[A]\nTime=[T2]Res=([w,w,w,w,w])\n", "Path=[Z]\n",
558 	  NULL, NULL, NULL } },
559 
560     { SMBchkpth, "SMBchkpth", 0,
561        { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
562 
563     { SMBsearch, "SMBsearch", 0,
564 	{ "Count=[d]\nAttrib=[A]\n",
565 	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\n",
566 	  "Count=[d]\n",
567 	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
568 	  NULL } },
569 
570     { SMBopen, "SMBopen", 0,
571 	{ "Mode=[w]\nAttribute=[A]\n", "Path=[Z]\n",
572 	  "Handle=[d]\nOAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\n",
573 	  NULL, NULL } },
574 
575     { SMBcreate, "SMBcreate", 0,
576 	{ "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },
577 
578     { SMBmknew, "SMBmknew", 0,
579 	{ "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },
580 
581     { SMBunlink, "SMBunlink", 0,
582 	{ "Attrib=[A]\n", "Path=[Z]\n", NULL, NULL, NULL } },
583 
584     { SMBread, "SMBread", 0,
585 	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
586 	  "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },
587 
588     { SMBwrite, "SMBwrite", 0,
589 	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
590 	  "Count=[d]\n", NULL, NULL } },
591 
592     { SMBclose, "SMBclose", 0,
593 	{ "Handle=[d]\nTime=[T2]", NULL, NULL, NULL, NULL } },
594 
595     { SMBmkdir, "SMBmkdir", 0,
596 	{ NULL, "Path=[Z]\n", NULL, NULL, NULL } },
597 
598     { SMBrmdir, "SMBrmdir", 0,
599 	{ NULL, "Path=[Z]\n", NULL, NULL, NULL } },
600 
601     { SMBdskattr, "SMBdskattr", 0,
602 	{ NULL, NULL,
603 	  "TotalUnits=[d]\nBlocksPerUnit=[d]\nBlockSize=[d]\nFreeUnits=[d]\nMedia=[w]\n",
604 	  NULL, NULL } },
605 
606     { SMBmv, "SMBmv", 0,
607 	{ "Attrib=[A]\n", "OldPath=[Z]\nNewPath=[Z]\n", NULL, NULL, NULL } },
608 
609     /*
610      * this is a Pathworks specific call, allowing the
611      * changing of the root path
612      */
613     { pSETDIR, "SMBsetdir", 0, { NULL, "Path=[Z]\n", NULL, NULL, NULL } },
614 
615     { SMBlseek, "SMBlseek", 0,
616 	{ "Handle=[d]\nMode=[w]\nOffset=[D]\n", "Offset=[D]\n", NULL, NULL, NULL } },
617 
618     { SMBflush, "SMBflush", 0, { "Handle=[d]\n", NULL, NULL, NULL, NULL } },
619 
620     { SMBsplopen, "SMBsplopen", 0,
621 	{ "SetupLen=[d]\nMode=[w]\n", "Ident=[Z]\n", "Handle=[d]\n",
622 	  NULL, NULL } },
623 
624     { SMBsplclose, "SMBsplclose", 0,
625 	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },
626 
627     { SMBsplretq, "SMBsplretq", 0,
628 	{ "MaxCount=[d]\nStartIndex=[d]\n", NULL,
629 	  "Count=[d]\nIndex=[d]\n",
630 	  "*Time=[T2]Status=[B]\nJobID=[d]\nSize=[D]\nRes=[B]Name=[s16]\n",
631 	  NULL } },
632 
633     { SMBsplwr, "SMBsplwr", 0,
634 	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },
635 
636     { SMBlock, "SMBlock", 0,
637 	{ "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },
638 
639     { SMBunlock, "SMBunlock", 0,
640 	{ "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },
641 
642     /* CORE+ PROTOCOL FOLLOWS */
643 
644     { SMBreadbraw, "SMBreadbraw", 0,
645 	{ "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[d]\n",
646 	  NULL, NULL, NULL, NULL } },
647 
648     { SMBwritebraw, "SMBwritebraw", 0,
649 	{ "Handle=[d]\nTotalCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\n|DataSize=[d]\nDataOff=[d]\n",
650 	  NULL, "WriteRawAck", NULL, NULL } },
651 
652     { SMBwritec, "SMBwritec", 0,
653 	{ NULL, NULL, "Count=[d]\n", NULL, NULL } },
654 
655     { SMBwriteclose, "SMBwriteclose", 0,
656 	{ "Handle=[d]\nCount=[d]\nOffset=[D]\nTime=[T2]Res=([w,w,w,w,w,w])",
657 	  NULL, "Count=[d]\n", NULL, NULL } },
658 
659     { SMBlockread, "SMBlockread", 0,
660 	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
661 	  "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },
662 
663     { SMBwriteunlock, "SMBwriteunlock", 0,
664 	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,
665 	  "Count=[d]\n", NULL, NULL } },
666 
667     { SMBreadBmpx, "SMBreadBmpx", 0,
668 	{ "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[w]\n",
669 	  NULL,
670 	  "Offset=[D]\nTotCount=[d]\nRemaining=[d]\nRes=([w,w])\nDataSize=[d]\nDataOff=[d]\n",
671 	  NULL, NULL } },
672 
673     { SMBwriteBmpx, "SMBwriteBmpx", 0,
674 	{ "Handle=[d]\nTotCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\nDataSize=[d]\nDataOff=[d]\n", NULL,
675 	  "Remaining=[d]\n", NULL, NULL } },
676 
677     { SMBwriteBs, "SMBwriteBs", 0,
678 	{ "Handle=[d]\nTotCount=[d]\nOffset=[D]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\n",
679 	  NULL, "Count=[d]\n", NULL, NULL } },
680 
681     { SMBsetattrE, "SMBsetattrE", 0,
682 	{ "Handle=[d]\nCreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]", NULL,
683 	  NULL, NULL, NULL } },
684 
685     { SMBgetattrE, "SMBgetattrE", 0,
686 	{ "Handle=[d]\n", NULL,
687 	  "CreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]Size=[D]\nAllocSize=[D]\nAttribute=[A]\n",
688 	  NULL, NULL } },
689 
690     { SMBtranss, "SMBtranss", 0, DEFDESCRIPT },
691     { SMBioctls, "SMBioctls", 0, DEFDESCRIPT },
692 
693     { SMBcopy, "SMBcopy", 0,
694 	{ "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",
695 	  "CopyCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },
696 
697     { SMBmove, "SMBmove", 0,
698 	{ "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",
699 	  "MoveCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },
700 
701     { SMBopenX, "SMBopenX", FLG_CHAIN,
702 	{ "Com2=[w]\nOff2=[d]\nFlags=[w]\nMode=[w]\nSearchAttrib=[A]\nAttrib=[A]\nTime=[T2]OFun=[w]\nSize=[D]\nTimeOut=[D]\nRes=[W]\n",
703 	  "Path=[S]\n",
704 	  "Com2=[w]\nOff2=[d]\nHandle=[d]\nAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\nType=[w]\nState=[w]\nAction=[w]\nFileID=[W]\nRes=[w]\n",
705 	  NULL, NULL } },
706 
707     { SMBreadX, "SMBreadX", FLG_CHAIN,
708 	{ "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nCountLeft=[d]\n",
709 	  NULL,
710 	  "Com2=[w]\nOff2=[d]\nRemaining=[d]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\nRes=([w,w,w,w])\n",
711 	  NULL, NULL } },
712 
713     { SMBwriteX, "SMBwriteX", FLG_CHAIN,
714 	{ "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nCountLeft=[d]\nRes=[w]\nDataSize=[d]\nDataOff=[d]\n",
715 	  NULL,
716 	  "Com2=[w]\nOff2=[d]\nCount=[d]\nRemaining=[d]\nRes=[W]\n",
717 	  NULL, NULL } },
718 
719     { SMBffirst, "SMBffirst", 0,
720 	{ "Count=[d]\nAttrib=[A]\n",
721 	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
722 	  "Count=[d]\n",
723 	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
724 	  NULL } },
725 
726     { SMBfunique, "SMBfunique", 0,
727 	{ "Count=[d]\nAttrib=[A]\n",
728 	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
729 	  "Count=[d]\n",
730 	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
731 	  NULL } },
732 
733     { SMBfclose, "SMBfclose", 0,
734 	{ "Count=[d]\nAttrib=[A]\n",
735 	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",
736 	  "Count=[d]\n",
737 	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",
738 	  NULL } },
739 
740     { SMBfindnclose, "SMBfindnclose", 0,
741 	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },
742 
743     { SMBfindclose, "SMBfindclose", 0,
744 	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },
745 
746     { SMBsends, "SMBsends", 0,
747 	{ NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },
748 
749     { SMBsendstrt, "SMBsendstrt", 0,
750 	{ NULL, "Source=[Z]\nDest=[Z]\n", "GroupID=[d]\n", NULL, NULL } },
751 
752     { SMBsendend, "SMBsendend", 0,
753 	{ "GroupID=[d]\n", NULL, NULL, NULL, NULL } },
754 
755     { SMBsendtxt, "SMBsendtxt", 0,
756 	{ "GroupID=[d]\n", NULL, NULL, NULL, NULL } },
757 
758     { SMBsendb, "SMBsendb", 0,
759 	{ NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },
760 
761     { SMBfwdname, "SMBfwdname", 0, DEFDESCRIPT },
762     { SMBcancelf, "SMBcancelf", 0, DEFDESCRIPT },
763     { SMBgetmac, "SMBgetmac", 0, DEFDESCRIPT },
764 
765     { SMBnegprot, "SMBnegprot", 0,
766 	{ NULL, NULL, NULL, NULL, print_negprot } },
767 
768     { SMBsesssetupX, "SMBsesssetupX", FLG_CHAIN,
769 	{ NULL, NULL, NULL, NULL, print_sesssetup } },
770 
771     { SMBtconX, "SMBtconX", FLG_CHAIN,
772 	{ "Com2=[w]\nOff2=[d]\nFlags=[w]\nPassLen=[d]\nPasswd&Path&Device=\n",
773 	  NULL, "Com2=[w]\nOff2=[d]\n", "ServiceType=[R]\n", NULL } },
774 
775     { SMBlockingX, "SMBlockingX", FLG_CHAIN,
776 	{ NULL, NULL, NULL, NULL, print_lockingandx } },
777 
778     { SMBtrans2, "SMBtrans2", 0, { NULL, NULL, NULL, NULL, print_trans2 } },
779 
780     { SMBtranss2, "SMBtranss2", 0, DEFDESCRIPT },
781     { SMBctemp, "SMBctemp", 0, DEFDESCRIPT },
782     { SMBreadBs, "SMBreadBs", 0, DEFDESCRIPT },
783     { SMBtrans, "SMBtrans", 0, { NULL, NULL, NULL, NULL, print_trans } },
784 
785     { SMBnttrans, "SMBnttrans", 0, DEFDESCRIPT },
786     { SMBnttranss, "SMBnttranss", 0, DEFDESCRIPT },
787 
788     { SMBntcreateX, "SMBntcreateX", FLG_CHAIN,
789 	{ "Com2=[w]\nOff2=[d]\nRes=[b]\nNameLen=[ld]\nFlags=[W]\nRootDirectoryFid=[D]\nAccessMask=[W]\nAllocationSize=[L]\nExtFileAttributes=[W]\nShareAccess=[W]\nCreateDisposition=[W]\nCreateOptions=[W]\nImpersonationLevel=[W]\nSecurityFlags=[b]\n",
790 	  "Path=[C]\n",
791 	  "Com2=[w]\nOff2=[d]\nOplockLevel=[b]\nFid=[d]\nCreateAction=[W]\nCreateTime=[T3]LastAccessTime=[T3]LastWriteTime=[T3]ChangeTime=[T3]ExtFileAttributes=[W]\nAllocationSize=[L]\nEndOfFile=[L]\nFileType=[w]\nDeviceState=[w]\nDirectory=[b]\n",
792 	  NULL, NULL } },
793 
794     { SMBntcancel, "SMBntcancel", 0, DEFDESCRIPT },
795 
796     { -1, NULL, 0, DEFDESCRIPT }
797 };
798 
799 
800 /*
801  * print a SMB message
802  */
803 static void
804 print_smb(netdissect_options *ndo,
805           const u_char *buf, const u_char *maxbuf)
806 {
807     uint16_t flags2;
808     int nterrcodes;
809     int command;
810     uint32_t nterror;
811     const u_char *words, *maxwords, *data;
812     const struct smbfns *fn;
813     const char *fmt_smbheader =
814         "[P4]SMB Command   =  [B]\nError class   =  [BP1]\nError code    =  [d]\nFlags1        =  [B]\nFlags2        =  [B][P13]\nTree ID       =  [d]\nProc ID       =  [d]\nUID           =  [d]\nMID           =  [d]\nWord Count    =  [b]\n";
815     int smboffset;
816 
817     ND_TCHECK(buf[9]);
818     request = (buf[9] & 0x80) ? 0 : 1;
819     startbuf = buf;
820 
821     command = buf[4];
822 
823     fn = smbfind(command, smb_fns);
824 
825     if (ndo->ndo_vflag > 1)
826 	ND_PRINT((ndo, "\n"));
827 
828     ND_PRINT((ndo, "SMB PACKET: %s (%s)\n", fn->name, request ? "REQUEST" : "REPLY"));
829 
830     if (ndo->ndo_vflag < 2)
831 	return;
832 
833     ND_TCHECK_16BITS(&buf[10]);
834     flags2 = EXTRACT_LE_16BITS(&buf[10]);
835     unicodestr = flags2 & 0x8000;
836     nterrcodes = flags2 & 0x4000;
837 
838     /* print out the header */
839     smb_fdata(ndo, buf, fmt_smbheader, buf + 33, unicodestr);
840 
841     if (nterrcodes) {
842     	nterror = EXTRACT_LE_32BITS(&buf[5]);
843 	if (nterror)
844 	    ND_PRINT((ndo, "NTError = %s\n", nt_errstr(nterror)));
845     } else {
846 	if (buf[5])
847 	    ND_PRINT((ndo, "SMBError = %s\n", smb_errstr(buf[5], EXTRACT_LE_16BITS(&buf[7]))));
848     }
849 
850     smboffset = 32;
851 
852     for (;;) {
853 	const char *f1, *f2;
854 	int wct;
855 	u_int bcc;
856 	int newsmboffset;
857 
858 	words = buf + smboffset;
859 	ND_TCHECK(words[0]);
860 	wct = words[0];
861 	data = words + 1 + wct * 2;
862 	maxwords = min(data, maxbuf);
863 
864 	if (request) {
865 	    f1 = fn->descript.req_f1;
866 	    f2 = fn->descript.req_f2;
867 	} else {
868 	    f1 = fn->descript.rep_f1;
869 	    f2 = fn->descript.rep_f2;
870 	}
871 
872 	if (fn->descript.fn)
873 	    (*fn->descript.fn)(ndo, words, data, buf, maxbuf);
874 	else {
875 	    if (wct) {
876 		if (f1)
877 		    smb_fdata(ndo, words + 1, f1, words + 1 + wct * 2, unicodestr);
878 		else {
879 		    int i;
880 		    int v;
881 
882 		    for (i = 0; &words[1 + 2 * i] < maxwords; i++) {
883 			ND_TCHECK2(words[1 + 2 * i], 2);
884 			v = EXTRACT_LE_16BITS(words + 1 + 2 * i);
885 			ND_PRINT((ndo, "smb_vwv[%d]=%d (0x%X)\n", i, v, v));
886 		    }
887 		}
888 	    }
889 
890 	    ND_TCHECK2(*data, 2);
891 	    bcc = EXTRACT_LE_16BITS(data);
892 	    ND_PRINT((ndo, "smb_bcc=%u\n", bcc));
893 	    if (f2) {
894 		if (bcc > 0)
895 		    smb_fdata(ndo, data + 2, f2, data + 2 + bcc, unicodestr);
896 	    } else {
897 		if (bcc > 0) {
898 		    ND_PRINT((ndo, "smb_buf[]=\n"));
899 		    smb_print_data(ndo, data + 2, min(bcc, PTR_DIFF(maxbuf, data + 2)));
900 		}
901 	    }
902 	}
903 
904 	if ((fn->flags & FLG_CHAIN) == 0)
905 	    break;
906 	if (wct == 0)
907 	    break;
908 	ND_TCHECK(words[1]);
909 	command = words[1];
910 	if (command == 0xFF)
911 	    break;
912 	ND_TCHECK2(words[3], 2);
913 	newsmboffset = EXTRACT_LE_16BITS(words + 3);
914 
915 	fn = smbfind(command, smb_fns);
916 
917 	ND_PRINT((ndo, "\nSMB PACKET: %s (%s) (CHAINED)\n",
918 	    fn->name, request ? "REQUEST" : "REPLY"));
919 	if (newsmboffset <= smboffset) {
920 	    ND_PRINT((ndo, "Bad andX offset: %u <= %u\n", newsmboffset, smboffset));
921 	    break;
922 	}
923 	smboffset = newsmboffset;
924     }
925 
926     ND_PRINT((ndo, "\n"));
927     return;
928 trunc:
929     ND_PRINT((ndo, "%s", tstr));
930 }
931 
932 
933 /*
934  * print a NBT packet received across tcp on port 139
935  */
936 void
937 nbt_tcp_print(netdissect_options *ndo,
938               const u_char *data, int length)
939 {
940     int caplen;
941     int type;
942     u_int nbt_len;
943     const u_char *maxbuf;
944 
945     if (length < 4)
946 	goto trunc;
947     if (ndo->ndo_snapend < data)
948 	goto trunc;
949     caplen = ndo->ndo_snapend - data;
950     if (caplen < 4)
951 	goto trunc;
952     maxbuf = data + caplen;
953     ND_TCHECK_8BITS(data);
954     type = data[0];
955     ND_TCHECK_16BITS(data + 2);
956     nbt_len = EXTRACT_16BITS(data + 2);
957     length -= 4;
958     caplen -= 4;
959 
960     startbuf = data;
961 
962     if (ndo->ndo_vflag < 2) {
963 	ND_PRINT((ndo, " NBT Session Packet: "));
964 	switch (type) {
965 	case 0x00:
966 	    ND_PRINT((ndo, "Session Message"));
967 	    break;
968 
969 	case 0x81:
970 	    ND_PRINT((ndo, "Session Request"));
971 	    break;
972 
973 	case 0x82:
974 	    ND_PRINT((ndo, "Session Granted"));
975 	    break;
976 
977 	case 0x83:
978 	  {
979 	    int ecode;
980 
981 	    if (nbt_len < 4)
982 		goto trunc;
983 	    if (length < 4)
984 		goto trunc;
985 	    if (caplen < 4)
986 		goto trunc;
987 	    ecode = data[4];
988 
989 	    ND_PRINT((ndo, "Session Reject, "));
990 	    switch (ecode) {
991 	    case 0x80:
992 		ND_PRINT((ndo, "Not listening on called name"));
993 		break;
994 	    case 0x81:
995 		ND_PRINT((ndo, "Not listening for calling name"));
996 		break;
997 	    case 0x82:
998 		ND_PRINT((ndo, "Called name not present"));
999 		break;
1000 	    case 0x83:
1001 		ND_PRINT((ndo, "Called name present, but insufficient resources"));
1002 		break;
1003 	    default:
1004 		ND_PRINT((ndo, "Unspecified error 0x%X", ecode));
1005 		break;
1006 	    }
1007 	  }
1008 	    break;
1009 
1010 	case 0x85:
1011 	    ND_PRINT((ndo, "Session Keepalive"));
1012 	    break;
1013 
1014 	default:
1015 	    data = smb_fdata(ndo, data, "Unknown packet type [rB]", maxbuf, 0);
1016 	    break;
1017 	}
1018     } else {
1019 	ND_PRINT((ndo, "\n>>> NBT Session Packet\n"));
1020 	switch (type) {
1021 	case 0x00:
1022 	    data = smb_fdata(ndo, data, "[P1]NBT Session Message\nFlags=[B]\nLength=[rd]\n",
1023 		data + 4, 0);
1024 	    if (data == NULL)
1025 		break;
1026 	    if (nbt_len >= 4 && caplen >= 4 && memcmp(data,"\377SMB",4) == 0) {
1027 		if ((int)nbt_len > caplen) {
1028 		    if ((int)nbt_len > length)
1029 			ND_PRINT((ndo, "WARNING: Packet is continued in later TCP segments\n"));
1030 		    else
1031 			ND_PRINT((ndo, "WARNING: Short packet. Try increasing the snap length by %d\n",
1032 			    nbt_len - caplen));
1033 		}
1034 		print_smb(ndo, data, maxbuf > data + nbt_len ? data + nbt_len : maxbuf);
1035 	    } else
1036 		ND_PRINT((ndo, "Session packet:(raw data or continuation?)\n"));
1037 	    break;
1038 
1039 	case 0x81:
1040 	    data = smb_fdata(ndo, data,
1041 		"[P1]NBT Session Request\nFlags=[B]\nLength=[rd]\nDestination=[n1]\nSource=[n1]\n",
1042 		maxbuf, 0);
1043 	    break;
1044 
1045 	case 0x82:
1046 	    data = smb_fdata(ndo, data, "[P1]NBT Session Granted\nFlags=[B]\nLength=[rd]\n", maxbuf, 0);
1047 	    break;
1048 
1049 	case 0x83:
1050 	  {
1051 	    const u_char *origdata;
1052 	    int ecode;
1053 
1054 	    origdata = data;
1055 	    data = smb_fdata(ndo, data, "[P1]NBT SessionReject\nFlags=[B]\nLength=[rd]\nReason=[B]\n",
1056 		maxbuf, 0);
1057 	    if (data == NULL)
1058 		break;
1059 	    if (nbt_len >= 1 && caplen >= 1) {
1060 		ecode = origdata[4];
1061 		switch (ecode) {
1062 		case 0x80:
1063 		    ND_PRINT((ndo, "Not listening on called name\n"));
1064 		    break;
1065 		case 0x81:
1066 		    ND_PRINT((ndo, "Not listening for calling name\n"));
1067 		    break;
1068 		case 0x82:
1069 		    ND_PRINT((ndo, "Called name not present\n"));
1070 		    break;
1071 		case 0x83:
1072 		    ND_PRINT((ndo, "Called name present, but insufficient resources\n"));
1073 		    break;
1074 		default:
1075 		    ND_PRINT((ndo, "Unspecified error 0x%X\n", ecode));
1076 		    break;
1077 		}
1078 	    }
1079 	  }
1080 	    break;
1081 
1082 	case 0x85:
1083 	    data = smb_fdata(ndo, data, "[P1]NBT Session Keepalive\nFlags=[B]\nLength=[rd]\n", maxbuf, 0);
1084 	    break;
1085 
1086 	default:
1087 	    data = smb_fdata(ndo, data, "NBT - Unknown packet type\nType=[B]\n", maxbuf, 0);
1088 	    break;
1089 	}
1090 	ND_PRINT((ndo, "\n"));
1091     }
1092     return;
1093 trunc:
1094     ND_PRINT((ndo, "%s", tstr));
1095 }
1096 
1097 static const struct tok opcode_str[] = {
1098 	{ 0,  "QUERY"                   },
1099 	{ 5,  "REGISTRATION"            },
1100 	{ 6,  "RELEASE"                 },
1101 	{ 7,  "WACK"                    },
1102 	{ 8,  "REFRESH(8)"              },
1103 	{ 9,  "REFRESH"                 },
1104 	{ 15, "MULTIHOMED REGISTRATION" },
1105 	{ 0, NULL }
1106 };
1107 
1108 /*
1109  * print a NBT packet received across udp on port 137
1110  */
1111 void
1112 nbt_udp137_print(netdissect_options *ndo,
1113                  const u_char *data, int length)
1114 {
1115     const u_char *maxbuf = data + length;
1116     int name_trn_id, response, opcode, nm_flags, rcode;
1117     int qdcount, ancount, nscount, arcount;
1118     const u_char *p;
1119     int total, i;
1120 
1121     ND_TCHECK2(data[10], 2);
1122     name_trn_id = EXTRACT_16BITS(data);
1123     response = (data[2] >> 7);
1124     opcode = (data[2] >> 3) & 0xF;
1125     nm_flags = ((data[2] & 0x7) << 4) + (data[3] >> 4);
1126     rcode = data[3] & 0xF;
1127     qdcount = EXTRACT_16BITS(data + 4);
1128     ancount = EXTRACT_16BITS(data + 6);
1129     nscount = EXTRACT_16BITS(data + 8);
1130     arcount = EXTRACT_16BITS(data + 10);
1131     startbuf = data;
1132 
1133     if (maxbuf <= data)
1134 	return;
1135 
1136     if (ndo->ndo_vflag > 1)
1137 	ND_PRINT((ndo, "\n>>> "));
1138 
1139     ND_PRINT((ndo, "NBT UDP PACKET(137): %s", tok2str(opcode_str, "OPUNKNOWN", opcode)));
1140     if (response) {
1141         ND_PRINT((ndo, "; %s", rcode ? "NEGATIVE" : "POSITIVE"));
1142     }
1143     ND_PRINT((ndo, "; %s; %s", response ? "RESPONSE" : "REQUEST",
1144               (nm_flags & 1) ? "BROADCAST" : "UNICAST"));
1145 
1146     if (ndo->ndo_vflag < 2)
1147 	return;
1148 
1149     ND_PRINT((ndo, "\nTrnID=0x%X\nOpCode=%d\nNmFlags=0x%X\nRcode=%d\nQueryCount=%d\nAnswerCount=%d\nAuthorityCount=%d\nAddressRecCount=%d\n",
1150 	name_trn_id, opcode, nm_flags, rcode, qdcount, ancount, nscount,
1151 	arcount));
1152 
1153     p = data + 12;
1154 
1155     total = ancount + nscount + arcount;
1156 
1157     if (qdcount > 100 || total > 100) {
1158 	ND_PRINT((ndo, "Corrupt packet??\n"));
1159 	return;
1160     }
1161 
1162     if (qdcount) {
1163 	ND_PRINT((ndo, "QuestionRecords:\n"));
1164 	for (i = 0; i < qdcount; i++) {
1165 	    p = smb_fdata(ndo, p,
1166 		"|Name=[n1]\nQuestionType=[rw]\nQuestionClass=[rw]\n#",
1167 		maxbuf, 0);
1168 	    if (p == NULL)
1169 		goto out;
1170 	}
1171     }
1172 
1173     if (total) {
1174 	ND_PRINT((ndo, "\nResourceRecords:\n"));
1175 	for (i = 0; i < total; i++) {
1176 	    int rdlen;
1177 	    int restype;
1178 
1179 	    p = smb_fdata(ndo, p, "Name=[n1]\n#", maxbuf, 0);
1180 	    if (p == NULL)
1181 		goto out;
1182 	    ND_TCHECK_16BITS(p);
1183 	    restype = EXTRACT_16BITS(p);
1184 	    p = smb_fdata(ndo, p, "ResType=[rw]\nResClass=[rw]\nTTL=[rD]\n", p + 8, 0);
1185 	    if (p == NULL)
1186 		goto out;
1187 	    ND_TCHECK_16BITS(p);
1188 	    rdlen = EXTRACT_16BITS(p);
1189 	    ND_PRINT((ndo, "ResourceLength=%d\nResourceData=\n", rdlen));
1190 	    p += 2;
1191 	    if (rdlen == 6) {
1192 		p = smb_fdata(ndo, p, "AddrType=[rw]\nAddress=[b.b.b.b]\n", p + rdlen, 0);
1193 		if (p == NULL)
1194 		    goto out;
1195 	    } else {
1196 		if (restype == 0x21) {
1197 		    int numnames;
1198 
1199 		    ND_TCHECK(*p);
1200 		    numnames = p[0];
1201 		    p = smb_fdata(ndo, p, "NumNames=[B]\n", p + 1, 0);
1202 		    if (p == NULL)
1203 			goto out;
1204 		    while (numnames--) {
1205 			p = smb_fdata(ndo, p, "Name=[n2]\t#", maxbuf, 0);
1206 			if (p == NULL)
1207 			    goto out;
1208 			ND_TCHECK(*p);
1209 			if (p[0] & 0x80)
1210 			    ND_PRINT((ndo, "<GROUP> "));
1211 			switch (p[0] & 0x60) {
1212 			case 0x00: ND_PRINT((ndo, "B ")); break;
1213 			case 0x20: ND_PRINT((ndo, "P ")); break;
1214 			case 0x40: ND_PRINT((ndo, "M ")); break;
1215 			case 0x60: ND_PRINT((ndo, "_ ")); break;
1216 			}
1217 			if (p[0] & 0x10)
1218 			    ND_PRINT((ndo, "<DEREGISTERING> "));
1219 			if (p[0] & 0x08)
1220 			    ND_PRINT((ndo, "<CONFLICT> "));
1221 			if (p[0] & 0x04)
1222 			    ND_PRINT((ndo, "<ACTIVE> "));
1223 			if (p[0] & 0x02)
1224 			    ND_PRINT((ndo, "<PERMANENT> "));
1225 			ND_PRINT((ndo, "\n"));
1226 			p += 2;
1227 		    }
1228 		} else {
1229 		    smb_print_data(ndo, p, min(rdlen, length - (p - data)));
1230 		    p += rdlen;
1231 		}
1232 	    }
1233 	}
1234     }
1235 
1236     if (p < maxbuf)
1237 	smb_fdata(ndo, p, "AdditionalData:\n", maxbuf, 0);
1238 
1239 out:
1240     ND_PRINT((ndo, "\n"));
1241     return;
1242 trunc:
1243     ND_PRINT((ndo, "%s", tstr));
1244 }
1245 
1246 /*
1247  * Print an SMB-over-TCP packet received across tcp on port 445
1248  */
1249 void
1250 smb_tcp_print(netdissect_options *ndo,
1251               const u_char * data, int length)
1252 {
1253     int caplen;
1254     u_int smb_len;
1255     const u_char *maxbuf;
1256 
1257     if (length < 4)
1258 	goto trunc;
1259     if (ndo->ndo_snapend < data)
1260 	goto trunc;
1261     caplen = ndo->ndo_snapend - data;
1262     if (caplen < 4)
1263 	goto trunc;
1264     maxbuf = data + caplen;
1265     smb_len = EXTRACT_24BITS(data + 1);
1266     length -= 4;
1267     caplen -= 4;
1268 
1269     startbuf = data;
1270     data += 4;
1271 
1272     if (smb_len >= 4 && caplen >= 4 && memcmp(data,"\377SMB",4) == 0) {
1273 	if ((int)smb_len > caplen) {
1274 	    if ((int)smb_len > length)
1275 		ND_PRINT((ndo, " WARNING: Packet is continued in later TCP segments\n"));
1276 	    else
1277 		ND_PRINT((ndo, " WARNING: Short packet. Try increasing the snap length by %d\n",
1278 		    smb_len - caplen));
1279 	} else
1280 	    ND_PRINT((ndo, " "));
1281 	print_smb(ndo, data, maxbuf > data + smb_len ? data + smb_len : maxbuf);
1282     } else
1283 	ND_PRINT((ndo, " SMB-over-TCP packet:(raw data or continuation?)\n"));
1284     return;
1285 trunc:
1286     ND_PRINT((ndo, "%s", tstr));
1287 }
1288 
1289 /*
1290  * print a NBT packet received across udp on port 138
1291  */
1292 void
1293 nbt_udp138_print(netdissect_options *ndo,
1294                  const u_char *data, int length)
1295 {
1296     const u_char *maxbuf = data + length;
1297 
1298     if (maxbuf > ndo->ndo_snapend)
1299 	maxbuf = ndo->ndo_snapend;
1300     if (maxbuf <= data)
1301 	return;
1302     startbuf = data;
1303 
1304     if (ndo->ndo_vflag < 2) {
1305 	ND_PRINT((ndo, "NBT UDP PACKET(138)"));
1306 	return;
1307     }
1308 
1309     data = smb_fdata(ndo, data,
1310 	"\n>>> NBT UDP PACKET(138) Res=[rw] ID=[rw] IP=[b.b.b.b] Port=[rd] Length=[rd] Res2=[rw]\nSourceName=[n1]\nDestName=[n1]\n#",
1311 	maxbuf, 0);
1312 
1313     if (data != NULL) {
1314 	/* If there isn't enough data for "\377SMB", don't check for it. */
1315 	if (&data[3] >= maxbuf)
1316 	    goto out;
1317 
1318 	if (memcmp(data, "\377SMB",4) == 0)
1319 	    print_smb(ndo, data, maxbuf);
1320     }
1321 out:
1322     ND_PRINT((ndo, "\n"));
1323 }
1324 
1325 
1326 /*
1327    print netbeui frames
1328 */
1329 static struct nbf_strings {
1330 	const char	*name;
1331 	const char	*nonverbose;
1332 	const char	*verbose;
1333 } nbf_strings[0x20] = {
1334 	{ "Add Group Name Query", ", [P23]Name to add=[n2]#",
1335 	  "[P5]ResponseCorrelator=[w]\n[P16]Name to add=[n2]\n" },
1336 	{ "Add Name Query", ", [P23]Name to add=[n2]#",
1337 	  "[P5]ResponseCorrelator=[w]\n[P16]Name to add=[n2]\n" },
1338 	{ "Name In Conflict", NULL, NULL },
1339 	{ "Status Query", NULL, NULL },
1340 	{ NULL, NULL, NULL },	/* not used */
1341 	{ NULL, NULL, NULL },	/* not used */
1342 	{ NULL, NULL, NULL },	/* not used */
1343 	{ "Terminate Trace", NULL, NULL },
1344 	{ "Datagram", NULL,
1345 	  "[P7]Destination=[n2]\nSource=[n2]\n" },
1346 	{ "Broadcast Datagram", NULL,
1347 	  "[P7]Destination=[n2]\nSource=[n2]\n" },
1348 	{ "Name Query", ", [P7]Name=[n2]#",
1349 	  "[P1]SessionNumber=[B]\nNameType=[B][P2]\nResponseCorrelator=[w]\nName=[n2]\nName of sender=[n2]\n" },
1350 	{ NULL, NULL, NULL },	/* not used */
1351 	{ NULL, NULL, NULL },	/* not used */
1352 	{ "Add Name Response", ", [P1]GroupName=[w] [P4]Destination=[n2] Source=[n2]#",
1353 	  "AddNameInProcess=[B]\nGroupName=[w]\nTransmitCorrelator=[w][P2]\nDestination=[n2]\nSource=[n2]\n" },
1354 	{ "Name Recognized", NULL,
1355 	  "[P1]Data2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nDestination=[n2]\nSource=[n2]\n" },
1356 	{ "Status Response", NULL, NULL },
1357 	{ NULL, NULL, NULL },	/* not used */
1358 	{ NULL, NULL, NULL },	/* not used */
1359 	{ NULL, NULL, NULL },	/* not used */
1360 	{ "Terminate Trace", NULL, NULL },
1361 	{ "Data Ack", NULL,
1362 	  "[P3]TransmitCorrelator=[w][P2]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1363 	{ "Data First/Middle", NULL,
1364 	  "Flags=[{RECEIVE_CONTINUE|NO_ACK||PIGGYBACK_ACK_INCLUDED|}]\nResyncIndicator=[w][P2]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1365 	{ "Data Only/Last", NULL,
1366 	  "Flags=[{|NO_ACK|PIGGYBACK_ACK_ALLOWED|PIGGYBACK_ACK_INCLUDED|}]\nResyncIndicator=[w][P2]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1367 	{ "Session Confirm", NULL,
1368 	  "Data1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1369 	{ "Session End", NULL,
1370 	  "[P1]Data2=[w][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1371 	{ "Session Initialize", NULL,
1372 	  "Data1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1373 	{ "No Receive", NULL,
1374 	  "Flags=[{|SEND_NO_ACK}]\nDataBytesAccepted=[b][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1375 	{ "Receive Outstanding", NULL,
1376 	  "[P1]DataBytesAccepted=[b][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1377 	{ "Receive Continue", NULL,
1378 	  "[P2]TransmitCorrelator=[w]\n[P2]RemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1379 	{ NULL, NULL, NULL },	/* not used */
1380 	{ NULL, NULL, NULL },	/* not used */
1381 	{ "Session Alive", NULL, NULL }
1382 };
1383 
1384 void
1385 netbeui_print(netdissect_options *ndo,
1386               u_short control, const u_char *data, int length)
1387 {
1388     const u_char *maxbuf = data + length;
1389     int len;
1390     int command;
1391     const u_char *data2;
1392     int is_truncated = 0;
1393 
1394     if (maxbuf > ndo->ndo_snapend)
1395 	maxbuf = ndo->ndo_snapend;
1396     ND_TCHECK(data[4]);
1397     len = EXTRACT_LE_16BITS(data);
1398     command = data[4];
1399     data2 = data + len;
1400     if (data2 >= maxbuf) {
1401 	data2 = maxbuf;
1402 	is_truncated = 1;
1403     }
1404 
1405     startbuf = data;
1406 
1407     if (ndo->ndo_vflag < 2) {
1408 	ND_PRINT((ndo, "NBF Packet: "));
1409 	data = smb_fdata(ndo, data, "[P5]#", maxbuf, 0);
1410     } else {
1411 	ND_PRINT((ndo, "\n>>> NBF Packet\nType=0x%X ", control));
1412 	data = smb_fdata(ndo, data, "Length=[d] Signature=[w] Command=[B]\n#", maxbuf, 0);
1413     }
1414     if (data == NULL)
1415 	goto out;
1416 
1417     if (command > 0x1f || nbf_strings[command].name == NULL) {
1418 	if (ndo->ndo_vflag < 2)
1419 	    data = smb_fdata(ndo, data, "Unknown NBF Command#", data2, 0);
1420 	else
1421 	    data = smb_fdata(ndo, data, "Unknown NBF Command\n", data2, 0);
1422     } else {
1423 	if (ndo->ndo_vflag < 2) {
1424 	    ND_PRINT((ndo, "%s", nbf_strings[command].name));
1425 	    if (nbf_strings[command].nonverbose != NULL)
1426 		data = smb_fdata(ndo, data, nbf_strings[command].nonverbose, data2, 0);
1427 	} else {
1428 	    ND_PRINT((ndo, "%s:\n", nbf_strings[command].name));
1429 	    if (nbf_strings[command].verbose != NULL)
1430 		data = smb_fdata(ndo, data, nbf_strings[command].verbose, data2, 0);
1431 	    else
1432 		ND_PRINT((ndo, "\n"));
1433 	}
1434     }
1435 
1436     if (ndo->ndo_vflag < 2)
1437 	return;
1438 
1439     if (data == NULL)
1440 	goto out;
1441 
1442     if (is_truncated) {
1443 	/* data2 was past the end of the buffer */
1444 	goto out;
1445     }
1446 
1447     /* If this isn't a command that would contain an SMB message, quit. */
1448     if (command != 0x08 && command != 0x09 && command != 0x15 &&
1449         command != 0x16)
1450 	goto out;
1451 
1452     /* If there isn't enough data for "\377SMB", don't look for it. */
1453     if (&data2[3] >= maxbuf)
1454 	goto out;
1455 
1456     if (memcmp(data2, "\377SMB",4) == 0)
1457 	print_smb(ndo, data2, maxbuf);
1458     else {
1459 	int i;
1460 	for (i = 0; i < 128; i++) {
1461 	    if (&data2[i + 3] >= maxbuf)
1462 		break;
1463 	    if (memcmp(&data2[i], "\377SMB", 4) == 0) {
1464 		ND_PRINT((ndo, "found SMB packet at %d\n", i));
1465 		print_smb(ndo, &data2[i], maxbuf);
1466 		break;
1467 	    }
1468 	}
1469     }
1470 
1471 out:
1472     ND_PRINT((ndo, "\n"));
1473     return;
1474 trunc:
1475     ND_PRINT((ndo, "%s", tstr));
1476 }
1477 
1478 
1479 /*
1480  * print IPX-Netbios frames
1481  */
1482 void
1483 ipx_netbios_print(netdissect_options *ndo,
1484                   const u_char *data, u_int length)
1485 {
1486     /*
1487      * this is a hack till I work out how to parse the rest of the
1488      * NetBIOS-over-IPX stuff
1489      */
1490     int i;
1491     const u_char *maxbuf;
1492 
1493     maxbuf = data + length;
1494     /* Don't go past the end of the captured data in the packet. */
1495     if (maxbuf > ndo->ndo_snapend)
1496 	maxbuf = ndo->ndo_snapend;
1497     startbuf = data;
1498     for (i = 0; i < 128; i++) {
1499 	if (&data[i + 4] > maxbuf)
1500 	    break;
1501 	if (memcmp(&data[i], "\377SMB", 4) == 0) {
1502 	    smb_fdata(ndo, data, "\n>>> IPX transport ", &data[i], 0);
1503 	    print_smb(ndo, &data[i], maxbuf);
1504 	    ND_PRINT((ndo, "\n"));
1505 	    break;
1506 	}
1507     }
1508     if (i == 128)
1509 	smb_fdata(ndo, data, "\n>>> Unknown IPX ", maxbuf, 0);
1510 }
1511