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