]> The Tcpdump Group git mirrors - tcpdump/blob - print-smb.c
Print truncations with nd_print_trunc() instead of tstr[] strings
[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 = EXTRACT_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 = EXTRACT_LE_U_2(w + 9 * 2);
187 param = buf + EXTRACT_LE_U_2(w + 10 * 2);
188 dcnt = EXTRACT_LE_U_2(w + 11 * 2);
189 data = buf + EXTRACT_LE_U_2(w + 12 * 2);
190 fn = smbfindint(EXTRACT_LE_U_2(w + 14 * 2), trans2_fns);
191 } else {
192 if (EXTRACT_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 = EXTRACT_LE_U_2(w + 3 * 2);
199 param = buf + EXTRACT_LE_U_2(w + 4 * 2);
200 dcnt = EXTRACT_LE_U_2(w + 6 * 2);
201 data = buf + EXTRACT_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 (EXTRACT_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 = EXTRACT_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 = EXTRACT_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 = EXTRACT_LE_U_2(w + 9 * 2);
348 param = buf + EXTRACT_LE_U_2(w + 10 * 2);
349 datalen = EXTRACT_LE_U_2(w + 11 * 2);
350 data = buf + EXTRACT_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 = EXTRACT_LE_U_2(w + 3 * 2);
358 param = buf + EXTRACT_LE_U_2(w + 4 * 2);
359 datalen = EXTRACT_LE_U_2(w + 6 * 2);
360 data = buf + EXTRACT_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 * EXTRACT_U_1(words), maxbuf),
369 unicodestr);
370
371 ND_TCHECK_2(data1);
372 bcc = EXTRACT_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 = EXTRACT_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, PTR_DIFF(maxbuf, words + 1)));
423
424 ND_TCHECK_2(data);
425 bcc = EXTRACT_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 + EXTRACT_LE_U_2(data),
430 maxbuf), unicodestr);
431 else
432 smb_data_print(ndo, data + 2,
433 min(EXTRACT_LE_U_2(data), PTR_DIFF(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 = EXTRACT_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, PTR_DIFF(maxbuf, words + 1)));
468
469 ND_TCHECK_2(data);
470 bcc = EXTRACT_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 + EXTRACT_LE_U_2(data),
475 maxbuf), unicodestr);
476 else
477 smb_data_print(ndo, data + 2,
478 min(EXTRACT_LE_U_2(data), PTR_DIFF(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 = EXTRACT_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 (EXTRACT_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 = EXTRACT_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 + EXTRACT_LE_U_2(data),
516 maxbuf), unicodestr);
517 else
518 smb_data_print(ndo, data + 2,
519 min(EXTRACT_LE_U_2(data), PTR_DIFF(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 ND_TCHECK_1(buf + 9);
813 request = (EXTRACT_U_1(buf + 9) & 0x80) ? 0 : 1;
814 startbuf = buf;
815
816 command = EXTRACT_U_1(buf + 4);
817
818 fn = smbfind(command, smb_fns);
819
820 if (ndo->ndo_vflag > 1)
821 ND_PRINT("\n");
822
823 ND_PRINT("SMB PACKET: %s (%s)\n", fn->name, request ? "REQUEST" : "REPLY");
824
825 if (ndo->ndo_vflag < 2)
826 return;
827
828 ND_TCHECK_2(buf + 10);
829 flags2 = EXTRACT_LE_U_2(buf + 10);
830 unicodestr = flags2 & 0x8000;
831 nterrcodes = flags2 & 0x4000;
832
833 /* print out the header */
834 smb_fdata(ndo, buf, fmt_smbheader, buf + 33, unicodestr);
835
836 if (nterrcodes) {
837 nterror = EXTRACT_LE_U_4(buf + 5);
838 if (nterror)
839 ND_PRINT("NTError = %s\n", nt_errstr(nterror));
840 } else {
841 if (EXTRACT_U_1(buf + 5))
842 ND_PRINT("SMBError = %s\n", smb_errstr(EXTRACT_U_1(buf + 5),
843 EXTRACT_LE_U_2(buf + 7)));
844 }
845
846 smboffset = 32;
847
848 for (;;) {
849 const char *f1, *f2;
850 int wct;
851 u_int bcc;
852 u_int newsmboffset;
853
854 words = buf + smboffset;
855 ND_TCHECK_1(words);
856 wct = EXTRACT_U_1(words);
857 data = words + 1 + wct * 2;
858 maxwords = min(data, maxbuf);
859
860 if (request) {
861 f1 = fn->descript.req_f1;
862 f2 = fn->descript.req_f2;
863 } else {
864 f1 = fn->descript.rep_f1;
865 f2 = fn->descript.rep_f2;
866 }
867
868 if (fn->descript.fn)
869 (*fn->descript.fn)(ndo, words, data, buf, maxbuf);
870 else {
871 if (wct) {
872 if (f1)
873 smb_fdata(ndo, words + 1, f1, words + 1 + wct * 2, unicodestr);
874 else {
875 u_int i;
876 u_int v;
877
878 for (i = 0; words + 1 + 2 * i < maxwords; i++) {
879 ND_TCHECK_2(words + 1 + 2 * i);
880 v = EXTRACT_LE_U_2(words + 1 + 2 * i);
881 ND_PRINT("smb_vwv[%u]=%u (0x%X)\n", i, v, v);
882 }
883 }
884 }
885
886 ND_TCHECK_2(data);
887 bcc = EXTRACT_LE_U_2(data);
888 ND_PRINT("smb_bcc=%u\n", bcc);
889 if (f2) {
890 if (bcc > 0)
891 smb_fdata(ndo, data + 2, f2, data + 2 + bcc, unicodestr);
892 } else {
893 if (bcc > 0) {
894 ND_PRINT("smb_buf[]=\n");
895 smb_data_print(ndo, data + 2, min(bcc, PTR_DIFF(maxbuf, data + 2)));
896 }
897 }
898 }
899
900 if ((fn->flags & FLG_CHAIN) == 0)
901 break;
902 if (wct == 0)
903 break;
904 ND_TCHECK_1(words + 1);
905 command = EXTRACT_U_1(words + 1);
906 if (command == 0xFF)
907 break;
908 ND_TCHECK_2(words + 3);
909 newsmboffset = EXTRACT_LE_U_2(words + 3);
910
911 fn = smbfind(command, smb_fns);
912
913 ND_PRINT("\nSMB PACKET: %s (%s) (CHAINED)\n",
914 fn->name, request ? "REQUEST" : "REPLY");
915 if (newsmboffset <= smboffset) {
916 ND_PRINT("Bad andX offset: %u <= %u\n", newsmboffset, smboffset);
917 break;
918 }
919 smboffset = newsmboffset;
920 }
921
922 ND_PRINT("\n");
923 return;
924 trunc:
925 nd_print_trunc(ndo);
926 }
927
928
929 /*
930 * print a NBT packet received across tcp on port 139
931 */
932 void
933 nbt_tcp_print(netdissect_options *ndo,
934 const u_char *data, u_int length)
935 {
936 u_int caplen;
937 u_int type;
938 u_int nbt_len;
939 const u_char *maxbuf;
940
941 ndo->ndo_protocol = "nbt_tcp";
942 if (length < 4)
943 goto trunc;
944 if (ndo->ndo_snapend < data)
945 goto trunc;
946 caplen = ndo->ndo_snapend - data;
947 if (caplen < 4)
948 goto trunc;
949 maxbuf = data + caplen;
950 ND_TCHECK_1(data);
951 type = EXTRACT_U_1(data);
952 ND_TCHECK_2(data + 2);
953 nbt_len = EXTRACT_BE_U_2(data + 2);
954 length -= 4;
955 caplen -= 4;
956
957 startbuf = data;
958
959 if (ndo->ndo_vflag < 2) {
960 ND_PRINT(" NBT Session Packet: ");
961 switch (type) {
962 case 0x00:
963 ND_PRINT("Session Message");
964 break;
965
966 case 0x81:
967 ND_PRINT("Session Request");
968 break;
969
970 case 0x82:
971 ND_PRINT("Session Granted");
972 break;
973
974 case 0x83:
975 {
976 u_int ecode;
977
978 if (nbt_len < 4)
979 goto trunc;
980 if (length < 4)
981 goto trunc;
982 if (caplen < 4)
983 goto trunc;
984 ecode = EXTRACT_U_1(data + 4);
985
986 ND_PRINT("Session Reject, ");
987 switch (ecode) {
988 case 0x80:
989 ND_PRINT("Not listening on called name");
990 break;
991 case 0x81:
992 ND_PRINT("Not listening for calling name");
993 break;
994 case 0x82:
995 ND_PRINT("Called name not present");
996 break;
997 case 0x83:
998 ND_PRINT("Called name present, but insufficient resources");
999 break;
1000 default:
1001 ND_PRINT("Unspecified error 0x%X", ecode);
1002 break;
1003 }
1004 }
1005 break;
1006
1007 case 0x85:
1008 ND_PRINT("Session Keepalive");
1009 break;
1010
1011 default:
1012 data = smb_fdata(ndo, data, "Unknown packet type [rB]", maxbuf, 0);
1013 break;
1014 }
1015 } else {
1016 ND_PRINT("\n>>> NBT Session Packet\n");
1017 switch (type) {
1018 case 0x00:
1019 data = smb_fdata(ndo, data, "[P1]NBT Session Message\nFlags=[B]\nLength=[ru]\n",
1020 data + 4, 0);
1021 if (data == NULL)
1022 break;
1023 if (nbt_len >= 4 && caplen >= 4 && memcmp(data,"\377SMB",4) == 0) {
1024 if (nbt_len > caplen) {
1025 if (nbt_len > length)
1026 ND_PRINT("WARNING: Packet is continued in later TCP segments\n");
1027 else
1028 ND_PRINT("WARNING: Short packet. Try increasing the snap length by %u\n",
1029 nbt_len - caplen);
1030 }
1031 print_smb(ndo, data, maxbuf > data + nbt_len ? data + nbt_len : maxbuf);
1032 } else
1033 ND_PRINT("Session packet:(raw data or continuation?)\n");
1034 break;
1035
1036 case 0x81:
1037 data = smb_fdata(ndo, data,
1038 "[P1]NBT Session Request\nFlags=[B]\nLength=[ru]\nDestination=[n1]\nSource=[n1]\n",
1039 maxbuf, 0);
1040 break;
1041
1042 case 0x82:
1043 data = smb_fdata(ndo, data, "[P1]NBT Session Granted\nFlags=[B]\nLength=[ru]\n", maxbuf, 0);
1044 break;
1045
1046 case 0x83:
1047 {
1048 const u_char *origdata;
1049 u_int ecode;
1050
1051 origdata = data;
1052 data = smb_fdata(ndo, data, "[P1]NBT SessionReject\nFlags=[B]\nLength=[ru]\nReason=[B]\n",
1053 maxbuf, 0);
1054 if (data == NULL)
1055 break;
1056 if (nbt_len >= 1 && caplen >= 1) {
1057 ecode = EXTRACT_U_1(origdata + 4);
1058 switch (ecode) {
1059 case 0x80:
1060 ND_PRINT("Not listening on called name\n");
1061 break;
1062 case 0x81:
1063 ND_PRINT("Not listening for calling name\n");
1064 break;
1065 case 0x82:
1066 ND_PRINT("Called name not present\n");
1067 break;
1068 case 0x83:
1069 ND_PRINT("Called name present, but insufficient resources\n");
1070 break;
1071 default:
1072 ND_PRINT("Unspecified error 0x%X\n", ecode);
1073 break;
1074 }
1075 }
1076 }
1077 break;
1078
1079 case 0x85:
1080 data = smb_fdata(ndo, data, "[P1]NBT Session Keepalive\nFlags=[B]\nLength=[ru]\n", maxbuf, 0);
1081 break;
1082
1083 default:
1084 data = smb_fdata(ndo, data, "NBT - Unknown packet type\nType=[B]\n", maxbuf, 0);
1085 break;
1086 }
1087 ND_PRINT("\n");
1088 }
1089 return;
1090 trunc:
1091 nd_print_trunc(ndo);
1092 }
1093
1094 static const struct tok opcode_str[] = {
1095 { 0, "QUERY" },
1096 { 5, "REGISTRATION" },
1097 { 6, "RELEASE" },
1098 { 7, "WACK" },
1099 { 8, "REFRESH(8)" },
1100 { 9, "REFRESH" },
1101 { 15, "MULTIHOMED REGISTRATION" },
1102 { 0, NULL }
1103 };
1104
1105 /*
1106 * print a NBT packet received across udp on port 137
1107 */
1108 void
1109 nbt_udp137_print(netdissect_options *ndo,
1110 const u_char *data, u_int length)
1111 {
1112 const u_char *maxbuf = data + length;
1113 u_int name_trn_id, response, opcode, nm_flags, rcode;
1114 u_int qdcount, ancount, nscount, arcount;
1115 const u_char *p;
1116 u_int total, i;
1117
1118 ndo->ndo_protocol = "nbt_udp137";
1119 ND_TCHECK_2(data + 10);
1120 name_trn_id = EXTRACT_BE_U_2(data);
1121 response = (EXTRACT_U_1(data + 2) >> 7);
1122 opcode = (EXTRACT_U_1(data + 2) >> 3) & 0xF;
1123 nm_flags = ((EXTRACT_U_1(data + 2) & 0x7) << 4) + (EXTRACT_U_1(data + 3) >> 4);
1124 rcode = EXTRACT_U_1(data + 3) & 0xF;
1125 qdcount = EXTRACT_BE_U_2(data + 4);
1126 ancount = EXTRACT_BE_U_2(data + 6);
1127 nscount = EXTRACT_BE_U_2(data + 8);
1128 arcount = EXTRACT_BE_U_2(data + 10);
1129 startbuf = data;
1130
1131 if (maxbuf <= data)
1132 return;
1133
1134 if (ndo->ndo_vflag > 1)
1135 ND_PRINT("\n>>> ");
1136
1137 ND_PRINT("NBT UDP PACKET(137): %s", tok2str(opcode_str, "OPUNKNOWN", opcode));
1138 if (response) {
1139 ND_PRINT("; %s", rcode ? "NEGATIVE" : "POSITIVE");
1140 }
1141 ND_PRINT("; %s; %s", response ? "RESPONSE" : "REQUEST",
1142 (nm_flags & 1) ? "BROADCAST" : "UNICAST");
1143
1144 if (ndo->ndo_vflag < 2)
1145 return;
1146
1147 ND_PRINT("\nTrnID=0x%X\nOpCode=%u\nNmFlags=0x%X\nRcode=%u\nQueryCount=%u\nAnswerCount=%u\nAuthorityCount=%u\nAddressRecCount=%u\n",
1148 name_trn_id, opcode, nm_flags, rcode, qdcount, ancount, nscount,
1149 arcount);
1150
1151 p = data + 12;
1152
1153 total = ancount + nscount + arcount;
1154
1155 if (qdcount > 100 || total > 100) {
1156 ND_PRINT("Corrupt packet??\n");
1157 return;
1158 }
1159
1160 if (qdcount) {
1161 ND_PRINT("QuestionRecords:\n");
1162 for (i = 0; i < qdcount; i++) {
1163 p = smb_fdata(ndo, p,
1164 "|Name=[n1]\nQuestionType=[rw]\nQuestionClass=[rw]\n#",
1165 maxbuf, 0);
1166 if (p == NULL)
1167 goto out;
1168 }
1169 }
1170
1171 if (total) {
1172 ND_PRINT("\nResourceRecords:\n");
1173 for (i = 0; i < total; i++) {
1174 u_int rdlen;
1175 u_int restype;
1176
1177 p = smb_fdata(ndo, p, "Name=[n1]\n#", maxbuf, 0);
1178 if (p == NULL)
1179 goto out;
1180 ND_TCHECK_2(p);
1181 restype = EXTRACT_BE_U_2(p);
1182 p = smb_fdata(ndo, p, "ResType=[rw]\nResClass=[rw]\nTTL=[rU]\n", p + 8, 0);
1183 if (p == NULL)
1184 goto out;
1185 ND_TCHECK_2(p);
1186 rdlen = EXTRACT_BE_U_2(p);
1187 ND_PRINT("ResourceLength=%u\nResourceData=\n", rdlen);
1188 p += 2;
1189 if (rdlen == 6) {
1190 p = smb_fdata(ndo, p, "AddrType=[rw]\nAddress=[b.b.b.b]\n", p + rdlen, 0);
1191 if (p == NULL)
1192 goto out;
1193 } else {
1194 if (restype == 0x21) {
1195 u_int numnames;
1196
1197 ND_TCHECK_1(p);
1198 numnames = EXTRACT_U_1(p);
1199 p = smb_fdata(ndo, p, "NumNames=[B]\n", p + 1, 0);
1200 if (p == NULL)
1201 goto out;
1202 while (numnames--) {
1203 p = smb_fdata(ndo, p, "Name=[n2]\t#", maxbuf, 0);
1204 if (p == NULL)
1205 goto out;
1206 ND_TCHECK_1(p);
1207 if (p >= maxbuf)
1208 goto out;
1209 if (EXTRACT_U_1(p) & 0x80)
1210 ND_PRINT("<GROUP> ");
1211 switch (EXTRACT_U_1(p) & 0x60) {
1212 case 0x00: ND_PRINT("B "); break;
1213 case 0x20: ND_PRINT("P "); break;
1214 case 0x40: ND_PRINT("M "); break;
1215 case 0x60: ND_PRINT("_ "); break;
1216 }
1217 if (EXTRACT_U_1(p) & 0x10)
1218 ND_PRINT("<DEREGISTERING> ");
1219 if (EXTRACT_U_1(p) & 0x08)
1220 ND_PRINT("<CONFLICT> ");
1221 if (EXTRACT_U_1(p) & 0x04)
1222 ND_PRINT("<ACTIVE> ");
1223 if (EXTRACT_U_1(p) & 0x02)
1224 ND_PRINT("<PERMANENT> ");
1225 ND_PRINT("\n");
1226 p += 2;
1227 }
1228 } else {
1229 if (p >= maxbuf)
1230 goto out;
1231 smb_data_print(ndo, p, min(rdlen, length - (p - data)));
1232 p += rdlen;
1233 }
1234 }
1235 }
1236 }
1237
1238 if (p < maxbuf)
1239 smb_fdata(ndo, p, "AdditionalData:\n", maxbuf, 0);
1240
1241 out:
1242 ND_PRINT("\n");
1243 return;
1244 trunc:
1245 nd_print_trunc(ndo);
1246 }
1247
1248 /*
1249 * Print an SMB-over-TCP packet received across tcp on port 445
1250 */
1251 void
1252 smb_tcp_print(netdissect_options *ndo,
1253 const u_char * data, u_int length)
1254 {
1255 u_int caplen;
1256 u_int smb_len;
1257 const u_char *maxbuf;
1258
1259 ndo->ndo_protocol = "smb_tcp";
1260 if (length < 4)
1261 goto trunc;
1262 if (ndo->ndo_snapend < data)
1263 goto trunc;
1264 caplen = ndo->ndo_snapend - data;
1265 if (caplen < 4)
1266 goto trunc;
1267 maxbuf = data + caplen;
1268 smb_len = EXTRACT_BE_U_3(data + 1);
1269 length -= 4;
1270 caplen -= 4;
1271
1272 startbuf = data;
1273 data += 4;
1274
1275 if (smb_len >= 4 && caplen >= 4 && memcmp(data,"\377SMB",4) == 0) {
1276 if (smb_len > caplen) {
1277 if (smb_len > length)
1278 ND_PRINT(" WARNING: Packet is continued in later TCP segments\n");
1279 else
1280 ND_PRINT(" WARNING: Short packet. Try increasing the snap length by %u\n",
1281 smb_len - caplen);
1282 } else
1283 ND_PRINT(" ");
1284 print_smb(ndo, data, maxbuf > data + smb_len ? data + smb_len : maxbuf);
1285 } else
1286 ND_PRINT(" SMB-over-TCP packet:(raw data or continuation?)\n");
1287 return;
1288 trunc:
1289 nd_print_trunc(ndo);
1290 }
1291
1292 /*
1293 * print a NBT packet received across udp on port 138
1294 */
1295 void
1296 nbt_udp138_print(netdissect_options *ndo,
1297 const u_char *data, u_int length)
1298 {
1299 const u_char *maxbuf = data + length;
1300
1301 ndo->ndo_protocol = "nbt_udp138";
1302 if (maxbuf > ndo->ndo_snapend)
1303 maxbuf = ndo->ndo_snapend;
1304 if (maxbuf <= data)
1305 return;
1306 startbuf = data;
1307
1308 if (ndo->ndo_vflag < 2) {
1309 ND_PRINT("NBT UDP PACKET(138)");
1310 return;
1311 }
1312
1313 data = smb_fdata(ndo, data,
1314 "\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#",
1315 maxbuf, 0);
1316
1317 if (data != NULL) {
1318 /* If there isn't enough data for "\377SMB", don't check for it. */
1319 if ((data + 3) >= maxbuf)
1320 goto out;
1321
1322 if (memcmp(data, "\377SMB",4) == 0)
1323 print_smb(ndo, data, maxbuf);
1324 }
1325 out:
1326 ND_PRINT("\n");
1327 }
1328
1329
1330 /*
1331 print netbeui frames
1332 */
1333 static struct nbf_strings {
1334 const char *name;
1335 const char *nonverbose;
1336 const char *verbose;
1337 } nbf_strings[0x20] = {
1338 { "Add Group Name Query", ", [P23]Name to add=[n2]#",
1339 "[P5]ResponseCorrelator=[w]\n[P16]Name to add=[n2]\n" },
1340 { "Add Name Query", ", [P23]Name to add=[n2]#",
1341 "[P5]ResponseCorrelator=[w]\n[P16]Name to add=[n2]\n" },
1342 { "Name In Conflict", NULL, NULL },
1343 { "Status Query", NULL, NULL },
1344 { NULL, NULL, NULL }, /* not used */
1345 { NULL, NULL, NULL }, /* not used */
1346 { NULL, NULL, NULL }, /* not used */
1347 { "Terminate Trace", NULL, NULL },
1348 { "Datagram", NULL,
1349 "[P7]Destination=[n2]\nSource=[n2]\n" },
1350 { "Broadcast Datagram", NULL,
1351 "[P7]Destination=[n2]\nSource=[n2]\n" },
1352 { "Name Query", ", [P7]Name=[n2]#",
1353 "[P1]SessionNumber=[B]\nNameType=[B][P2]\nResponseCorrelator=[w]\nName=[n2]\nName of sender=[n2]\n" },
1354 { NULL, NULL, NULL }, /* not used */
1355 { NULL, NULL, NULL }, /* not used */
1356 { "Add Name Response", ", [P1]GroupName=[w] [P4]Destination=[n2] Source=[n2]#",
1357 "AddNameInProcess=[B]\nGroupName=[w]\nTransmitCorrelator=[w][P2]\nDestination=[n2]\nSource=[n2]\n" },
1358 { "Name Recognized", NULL,
1359 "[P1]Data2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nDestination=[n2]\nSource=[n2]\n" },
1360 { "Status Response", NULL, NULL },
1361 { NULL, NULL, NULL }, /* not used */
1362 { NULL, NULL, NULL }, /* not used */
1363 { NULL, NULL, NULL }, /* not used */
1364 { "Terminate Trace", NULL, NULL },
1365 { "Data Ack", NULL,
1366 "[P3]TransmitCorrelator=[w][P2]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1367 { "Data First/Middle", NULL,
1368 "Flags=[{RECEIVE_CONTINUE|NO_ACK||PIGGYBACK_ACK_INCLUDED|}]\nResyncIndicator=[w][P2]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1369 { "Data Only/Last", NULL,
1370 "Flags=[{|NO_ACK|PIGGYBACK_ACK_ALLOWED|PIGGYBACK_ACK_INCLUDED|}]\nResyncIndicator=[w][P2]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1371 { "Session Confirm", NULL,
1372 "Data1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1373 { "Session End", NULL,
1374 "[P1]Data2=[w][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1375 { "Session Initialize", NULL,
1376 "Data1=[B]\nData2=[w]\nTransmitCorrelator=[w]\nResponseCorelator=[w]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1377 { "No Receive", NULL,
1378 "Flags=[{|SEND_NO_ACK}]\nDataBytesAccepted=[b][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1379 { "Receive Outstanding", NULL,
1380 "[P1]DataBytesAccepted=[b][P4]\nRemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1381 { "Receive Continue", NULL,
1382 "[P2]TransmitCorrelator=[w]\n[P2]RemoteSessionNumber=[B]\nLocalSessionNumber=[B]\n" },
1383 { NULL, NULL, NULL }, /* not used */
1384 { NULL, NULL, NULL }, /* not used */
1385 { "Session Alive", NULL, NULL }
1386 };
1387
1388 void
1389 netbeui_print(netdissect_options *ndo,
1390 u_short control, const u_char *data, u_int length)
1391 {
1392 const u_char *maxbuf = data + length;
1393 u_int len;
1394 u_int command;
1395 const u_char *data2;
1396 int is_truncated = 0;
1397
1398 ndo->ndo_protocol = "netbeui";
1399 if (maxbuf > ndo->ndo_snapend)
1400 maxbuf = ndo->ndo_snapend;
1401 ND_TCHECK_1(data + 4);
1402 len = EXTRACT_LE_U_2(data);
1403 command = EXTRACT_U_1(data + 4);
1404 data2 = data + len;
1405 if (data2 >= maxbuf) {
1406 data2 = maxbuf;
1407 is_truncated = 1;
1408 }
1409
1410 startbuf = data;
1411
1412 if (ndo->ndo_vflag < 2) {
1413 ND_PRINT("NBF Packet: ");
1414 data = smb_fdata(ndo, data, "[P5]#", maxbuf, 0);
1415 } else {
1416 ND_PRINT("\n>>> NBF Packet\nType=0x%X ", control);
1417 data = smb_fdata(ndo, data, "Length=[u] Signature=[w] Command=[B]\n#", maxbuf, 0);
1418 }
1419 if (data == NULL)
1420 goto out;
1421
1422 if (command > 0x1f || nbf_strings[command].name == NULL) {
1423 if (ndo->ndo_vflag < 2)
1424 data = smb_fdata(ndo, data, "Unknown NBF Command#", data2, 0);
1425 else
1426 data = smb_fdata(ndo, data, "Unknown NBF Command\n", data2, 0);
1427 } else {
1428 if (ndo->ndo_vflag < 2) {
1429 ND_PRINT("%s", nbf_strings[command].name);
1430 if (nbf_strings[command].nonverbose != NULL)
1431 data = smb_fdata(ndo, data, nbf_strings[command].nonverbose, data2, 0);
1432 } else {
1433 ND_PRINT("%s:\n", nbf_strings[command].name);
1434 if (nbf_strings[command].verbose != NULL)
1435 data = smb_fdata(ndo, data, nbf_strings[command].verbose, data2, 0);
1436 else
1437 ND_PRINT("\n");
1438 }
1439 }
1440
1441 if (ndo->ndo_vflag < 2)
1442 return;
1443
1444 if (data == NULL)
1445 goto out;
1446
1447 if (is_truncated) {
1448 /* data2 was past the end of the buffer */
1449 goto out;
1450 }
1451
1452 /* If this isn't a command that would contain an SMB message, quit. */
1453 if (command != 0x08 && command != 0x09 && command != 0x15 &&
1454 command != 0x16)
1455 goto out;
1456
1457 /* If there isn't enough data for "\377SMB", don't look for it. */
1458 if ((data2 + 3) >= maxbuf)
1459 goto out;
1460
1461 if (memcmp(data2, "\377SMB",4) == 0)
1462 print_smb(ndo, data2, maxbuf);
1463 else {
1464 u_int i;
1465 for (i = 0; i < 128; i++) {
1466 if ((data2 + i + 3) >= maxbuf)
1467 break;
1468 if (memcmp(data2 + i, "\377SMB", 4) == 0) {
1469 ND_PRINT("found SMB packet at %u\n", i);
1470 print_smb(ndo, data2 + i, maxbuf);
1471 break;
1472 }
1473 }
1474 }
1475
1476 out:
1477 ND_PRINT("\n");
1478 return;
1479 trunc:
1480 nd_print_trunc(ndo);
1481 }
1482
1483
1484 /*
1485 * print IPX-Netbios frames
1486 */
1487 void
1488 ipx_netbios_print(netdissect_options *ndo,
1489 const u_char *data, u_int length)
1490 {
1491 /*
1492 * this is a hack till I work out how to parse the rest of the
1493 * NetBIOS-over-IPX stuff
1494 */
1495 u_int i;
1496 const u_char *maxbuf;
1497
1498 ndo->ndo_protocol = "ipx_netbios";
1499 maxbuf = data + length;
1500 /* Don't go past the end of the captured data in the packet. */
1501 if (maxbuf > ndo->ndo_snapend)
1502 maxbuf = ndo->ndo_snapend;
1503 startbuf = data;
1504 for (i = 0; i < 128; i++) {
1505 if ((data + i + 4) > maxbuf)
1506 break;
1507 if (memcmp(data + i, "\377SMB", 4) == 0) {
1508 smb_fdata(ndo, data, "\n>>> IPX transport ", data + i, 0);
1509 print_smb(ndo, data + i, maxbuf);
1510 ND_PRINT("\n");
1511 break;
1512 }
1513 }
1514 if (i == 128)
1515 smb_fdata(ndo, data, "\n>>> Unknown IPX ", maxbuf, 0);
1516 }