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