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