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