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