14edb46e9SPaul Traina /*
24644f044SBill Fenner * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina * The Regents of the University of California. All rights reserved.
44edb46e9SPaul Traina *
54edb46e9SPaul Traina * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina * or promote products derived from this software without specific prior
164edb46e9SPaul Traina * written permission.
174edb46e9SPaul Traina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina */
214edb46e9SPaul Traina
223340d773SGleb Smirnoff /* \summary: Trivial File Transfer Protocol (TFTP) printer */
233340d773SGleb Smirnoff
24*ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina
283340d773SGleb Smirnoff #include "netdissect.h"
295b0fe478SBruce M Simpson #include "extract.h"
303c602fabSXin LI
313c602fabSXin LI /*
323c602fabSXin LI * Trivial File Transfer Protocol (IEN-133)
333c602fabSXin LI */
343c602fabSXin LI
353c602fabSXin LI /*
363c602fabSXin LI * Packet types.
373c602fabSXin LI */
383c602fabSXin LI #define RRQ 01 /* read request */
393c602fabSXin LI #define WRQ 02 /* write request */
403c602fabSXin LI #define DATA 03 /* data packet */
413c602fabSXin LI #define ACK 04 /* acknowledgement */
423c602fabSXin LI #define TFTP_ERROR 05 /* error code */
433c602fabSXin LI #define OACK 06 /* option acknowledgement */
443c602fabSXin LI
453c602fabSXin LI /*
463c602fabSXin LI * Error codes.
473c602fabSXin LI */
483c602fabSXin LI #define EUNDEF 0 /* not defined */
493c602fabSXin LI #define ENOTFOUND 1 /* file not found */
503c602fabSXin LI #define EACCESS 2 /* access violation */
513c602fabSXin LI #define ENOSPACE 3 /* disk full or allocation exceeded */
523c602fabSXin LI #define EBADOP 4 /* illegal TFTP operation */
533c602fabSXin LI #define EBADID 5 /* unknown transfer ID */
543c602fabSXin LI #define EEXISTS 6 /* file already exists */
553c602fabSXin LI #define ENOUSER 7 /* no such user */
563c602fabSXin LI
574edb46e9SPaul Traina
584edb46e9SPaul Traina /* op code to string mapping */
593c602fabSXin LI static const struct tok op2str[] = {
604edb46e9SPaul Traina { RRQ, "RRQ" }, /* read request */
614edb46e9SPaul Traina { WRQ, "WRQ" }, /* write request */
624edb46e9SPaul Traina { DATA, "DATA" }, /* data packet */
634edb46e9SPaul Traina { ACK, "ACK" }, /* acknowledgement */
64a5779b6eSRui Paulo { TFTP_ERROR, "ERROR" }, /* error code */
65b5bfcb5dSMax Laier { OACK, "OACK" }, /* option acknowledgement */
664edb46e9SPaul Traina { 0, NULL }
674edb46e9SPaul Traina };
684edb46e9SPaul Traina
694edb46e9SPaul Traina /* error code to string mapping */
703c602fabSXin LI static const struct tok err2str[] = {
714edb46e9SPaul Traina { EUNDEF, "EUNDEF" }, /* not defined */
724edb46e9SPaul Traina { ENOTFOUND, "ENOTFOUND" }, /* file not found */
734edb46e9SPaul Traina { EACCESS, "EACCESS" }, /* access violation */
744edb46e9SPaul Traina { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
754edb46e9SPaul Traina { EBADOP, "EBADOP" }, /* illegal TFTP operation */
764edb46e9SPaul Traina { EBADID, "EBADID" }, /* unknown transfer ID */
774edb46e9SPaul Traina { EEXISTS, "EEXISTS" }, /* file already exists */
784edb46e9SPaul Traina { ENOUSER, "ENOUSER" }, /* no such user */
794edb46e9SPaul Traina { 0, NULL }
804edb46e9SPaul Traina };
814edb46e9SPaul Traina
824edb46e9SPaul Traina /*
834edb46e9SPaul Traina * Print trivial file transfer program requests
844edb46e9SPaul Traina */
854edb46e9SPaul Traina void
tftp_print(netdissect_options * ndo,const u_char * bp,u_int length)863c602fabSXin LI tftp_print(netdissect_options *ndo,
87*ee67461eSJoseph Mingrone const u_char *bp, u_int length)
884edb46e9SPaul Traina {
89*ee67461eSJoseph Mingrone const char *cp;
90*ee67461eSJoseph Mingrone u_int opcode;
913340d773SGleb Smirnoff u_int ui;
924edb46e9SPaul Traina
93*ee67461eSJoseph Mingrone ndo->ndo_protocol = "tftp";
94*ee67461eSJoseph Mingrone
95*ee67461eSJoseph Mingrone /* Print protocol */
96*ee67461eSJoseph Mingrone nd_print_protocol_caps(ndo);
974edb46e9SPaul Traina /* Print length */
98*ee67461eSJoseph Mingrone ND_PRINT(", length %u", length);
994edb46e9SPaul Traina
1004edb46e9SPaul Traina /* Print tftp request type */
1013340d773SGleb Smirnoff if (length < 2)
1023340d773SGleb Smirnoff goto trunc;
103*ee67461eSJoseph Mingrone opcode = GET_BE_U_2(bp);
104*ee67461eSJoseph Mingrone cp = tok2str(op2str, "tftp-#%u", opcode);
105*ee67461eSJoseph Mingrone ND_PRINT(", %s", cp);
1064edb46e9SPaul Traina /* Bail if bogus opcode */
1074edb46e9SPaul Traina if (*cp == 't')
1084edb46e9SPaul Traina return;
1090bff6a5aSEd Maste bp += 2;
1100bff6a5aSEd Maste length -= 2;
1114edb46e9SPaul Traina
1124edb46e9SPaul Traina switch (opcode) {
1134edb46e9SPaul Traina
1144edb46e9SPaul Traina case RRQ:
1154edb46e9SPaul Traina case WRQ:
1163340d773SGleb Smirnoff if (length == 0)
1174edb46e9SPaul Traina goto trunc;
118*ee67461eSJoseph Mingrone ND_PRINT(" ");
1193340d773SGleb Smirnoff /* Print filename */
120*ee67461eSJoseph Mingrone ND_PRINT("\"");
121*ee67461eSJoseph Mingrone ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
122*ee67461eSJoseph Mingrone ND_PRINT("\"");
1233340d773SGleb Smirnoff if (ui == 0)
1243340d773SGleb Smirnoff goto trunc;
1250bff6a5aSEd Maste bp += ui;
1263340d773SGleb Smirnoff length -= ui;
1273340d773SGleb Smirnoff
1283340d773SGleb Smirnoff /* Print the mode - RRQ and WRQ only */
1293340d773SGleb Smirnoff if (length == 0)
1303340d773SGleb Smirnoff goto trunc; /* no mode */
131*ee67461eSJoseph Mingrone ND_PRINT(" ");
132*ee67461eSJoseph Mingrone ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
1333340d773SGleb Smirnoff if (ui == 0)
1343340d773SGleb Smirnoff goto trunc;
1350bff6a5aSEd Maste bp += ui;
1363340d773SGleb Smirnoff length -= ui;
1373340d773SGleb Smirnoff
1383340d773SGleb Smirnoff /* Print options, if any */
1393340d773SGleb Smirnoff while (length != 0) {
140*ee67461eSJoseph Mingrone if (GET_U_1(bp) != '\0')
141*ee67461eSJoseph Mingrone ND_PRINT(" ");
142*ee67461eSJoseph Mingrone ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
1433340d773SGleb Smirnoff if (ui == 0)
1443340d773SGleb Smirnoff goto trunc;
1450bff6a5aSEd Maste bp += ui;
1463340d773SGleb Smirnoff length -= ui;
1473340d773SGleb Smirnoff }
1483340d773SGleb Smirnoff break;
1493340d773SGleb Smirnoff
1503340d773SGleb Smirnoff case OACK:
1513340d773SGleb Smirnoff /* Print options */
1523340d773SGleb Smirnoff while (length != 0) {
153*ee67461eSJoseph Mingrone if (GET_U_1(bp) != '\0')
154*ee67461eSJoseph Mingrone ND_PRINT(" ");
155*ee67461eSJoseph Mingrone ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
1563340d773SGleb Smirnoff if (ui == 0)
1573340d773SGleb Smirnoff goto trunc;
1580bff6a5aSEd Maste bp += ui;
1593340d773SGleb Smirnoff length -= ui;
1603340d773SGleb Smirnoff }
1614edb46e9SPaul Traina break;
1624edb46e9SPaul Traina
1634edb46e9SPaul Traina case ACK:
1644edb46e9SPaul Traina case DATA:
1653340d773SGleb Smirnoff if (length < 2)
1663340d773SGleb Smirnoff goto trunc; /* no block number */
167*ee67461eSJoseph Mingrone ND_PRINT(" block %u", GET_BE_U_2(bp));
1684edb46e9SPaul Traina break;
1694edb46e9SPaul Traina
170a5779b6eSRui Paulo case TFTP_ERROR:
1714edb46e9SPaul Traina /* Print error code string */
1723340d773SGleb Smirnoff if (length < 2)
1733340d773SGleb Smirnoff goto trunc; /* no error code */
174*ee67461eSJoseph Mingrone ND_PRINT(" %s", tok2str(err2str, "tftp-err-#%u \"",
175*ee67461eSJoseph Mingrone GET_BE_U_2(bp)));
1760bff6a5aSEd Maste bp += 2;
1773340d773SGleb Smirnoff length -= 2;
1784edb46e9SPaul Traina /* Print error message string */
1793340d773SGleb Smirnoff if (length == 0)
1803340d773SGleb Smirnoff goto trunc; /* no error message */
181*ee67461eSJoseph Mingrone ND_PRINT(" \"");
182*ee67461eSJoseph Mingrone ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
183*ee67461eSJoseph Mingrone ND_PRINT("\"");
1843340d773SGleb Smirnoff if (ui == 0)
1854edb46e9SPaul Traina goto trunc;
1864edb46e9SPaul Traina break;
1874edb46e9SPaul Traina
1884edb46e9SPaul Traina default:
1894edb46e9SPaul Traina /* We shouldn't get here */
190*ee67461eSJoseph Mingrone ND_PRINT("(unknown #%u)", opcode);
1914edb46e9SPaul Traina break;
1924edb46e9SPaul Traina }
1934edb46e9SPaul Traina return;
1944edb46e9SPaul Traina trunc:
195*ee67461eSJoseph Mingrone nd_print_trunc(ndo);
1964edb46e9SPaul Traina }
197