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