xref: /netbsd-src/external/bsd/tcpdump/dist/print-tftp.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /*
2  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 /* \summary: Trivial File Transfer Protocol (TFTP) printer */
23 
24 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-tftp.c,v 1.9 2023/08/17 20:19:40 christos Exp $");
27 #endif
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 
33 #include "netdissect-stdinc.h"
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 
38 /*
39  * Trivial File Transfer Protocol (IEN-133)
40  */
41 
42 /*
43  * Packet types.
44  */
45 #define	RRQ	01			/* read request */
46 #define	WRQ	02			/* write request */
47 #define	DATA	03			/* data packet */
48 #define	ACK	04			/* acknowledgement */
49 #define	TFTP_ERROR	05			/* error code */
50 #define OACK	06			/* option acknowledgement */
51 
52 /*
53  * Error codes.
54  */
55 #define	EUNDEF		0		/* not defined */
56 #define	ENOTFOUND	1		/* file not found */
57 #define	EACCESS		2		/* access violation */
58 #define	ENOSPACE	3		/* disk full or allocation exceeded */
59 #define	EBADOP		4		/* illegal TFTP operation */
60 #define	EBADID		5		/* unknown transfer ID */
61 #define	EEXISTS		6		/* file already exists */
62 #define	ENOUSER		7		/* no such user */
63 
64 
65 /* op code to string mapping */
66 static const struct tok op2str[] = {
67 	{ RRQ,		"RRQ" },	/* read request */
68 	{ WRQ,		"WRQ" },	/* write request */
69 	{ DATA,		"DATA" },	/* data packet */
70 	{ ACK,		"ACK" },	/* acknowledgement */
71 	{ TFTP_ERROR,	"ERROR" },	/* error code */
72 	{ OACK,		"OACK" },	/* option acknowledgement */
73 	{ 0,		NULL }
74 };
75 
76 /* error code to string mapping */
77 static const struct tok err2str[] = {
78 	{ EUNDEF,	"EUNDEF" },	/* not defined */
79 	{ ENOTFOUND,	"ENOTFOUND" },	/* file not found */
80 	{ EACCESS,	"EACCESS" },	/* access violation */
81 	{ ENOSPACE,	"ENOSPACE" },	/* disk full or allocation exceeded */
82 	{ EBADOP,	"EBADOP" },	/* illegal TFTP operation */
83 	{ EBADID,	"EBADID" },	/* unknown transfer ID */
84 	{ EEXISTS,	"EEXISTS" },	/* file already exists */
85 	{ ENOUSER,	"ENOUSER" },	/* no such user */
86 	{ 0,		NULL }
87 };
88 
89 /*
90  * Print trivial file transfer program requests
91  */
92 void
93 tftp_print(netdissect_options *ndo,
94            const u_char *bp, u_int length)
95 {
96 	const char *cp;
97 	u_int opcode;
98 	u_int ui;
99 
100 	ndo->ndo_protocol = "tftp";
101 
102 	/* Print protocol */
103 	nd_print_protocol_caps(ndo);
104 	/* Print length */
105 	ND_PRINT(", length %u", length);
106 
107 	/* Print tftp request type */
108 	if (length < 2)
109 		goto trunc;
110 	opcode = GET_BE_U_2(bp);
111 	cp = tok2str(op2str, "tftp-#%u", opcode);
112 	ND_PRINT(", %s", cp);
113 	/* Bail if bogus opcode */
114 	if (*cp == 't')
115 		return;
116 	bp += 2;
117 	length -= 2;
118 
119 	switch (opcode) {
120 
121 	case RRQ:
122 	case WRQ:
123 		if (length == 0)
124 			goto trunc;
125 		ND_PRINT(" ");
126 		/* Print filename */
127 		ND_PRINT("\"");
128 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
129 		ND_PRINT("\"");
130 		if (ui == 0)
131 			goto trunc;
132 		bp += ui;
133 		length -= ui;
134 
135 		/* Print the mode - RRQ and WRQ only */
136 		if (length == 0)
137 			goto trunc;	/* no mode */
138 		ND_PRINT(" ");
139 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
140 		if (ui == 0)
141 			goto trunc;
142 		bp += ui;
143 		length -= ui;
144 
145 		/* Print options, if any */
146 		while (length != 0) {
147 			if (GET_U_1(bp) != '\0')
148 				ND_PRINT(" ");
149 			ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
150 			if (ui == 0)
151 				goto trunc;
152 			bp += ui;
153 			length -= ui;
154 		}
155 		break;
156 
157 	case OACK:
158 		/* Print options */
159 		while (length != 0) {
160 			if (GET_U_1(bp) != '\0')
161 				ND_PRINT(" ");
162 			ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
163 			if (ui == 0)
164 				goto trunc;
165 			bp += ui;
166 			length -= ui;
167 		}
168 		break;
169 
170 	case ACK:
171 	case DATA:
172 		if (length < 2)
173 			goto trunc;	/* no block number */
174 		ND_PRINT(" block %u", GET_BE_U_2(bp));
175 		break;
176 
177 	case TFTP_ERROR:
178 		/* Print error code string */
179 		if (length < 2)
180 			goto trunc;	/* no error code */
181 		ND_PRINT(" %s", tok2str(err2str, "tftp-err-#%u \"",
182 				       GET_BE_U_2(bp)));
183 		bp += 2;
184 		length -= 2;
185 		/* Print error message string */
186 		if (length == 0)
187 			goto trunc;	/* no error message */
188 		ND_PRINT(" \"");
189 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
190 		ND_PRINT("\"");
191 		if (ui == 0)
192 			goto trunc;
193 		break;
194 
195 	default:
196 		/* We shouldn't get here */
197 		ND_PRINT("(unknown #%u)", opcode);
198 		break;
199 	}
200 	return;
201 trunc:
202 	nd_print_trunc(ndo);
203 }
204