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