blob: 39fc6964b8a26eac38b2a155ce892d0127a0de25 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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.
The Android Open Source Project2949f582009-03-03 19:30:46 -080020 */
21
Elliott Hughese2e3bd12017-05-15 10:59:29 -070022/* \summary: Trivial File Transfer Protocol (TFTP) printer */
23
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070025#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080026#endif
27
Elliott Hughes820eced2021-08-20 18:00:50 -070028#include "netdissect-stdinc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080029
Elliott Hughese2e3bd12017-05-15 10:59:29 -070030#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#include "extract.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070032
33/*
34 * Trivial File Transfer Protocol (IEN-133)
35 */
36
37/*
38 * Packet types.
39 */
40#define RRQ 01 /* read request */
41#define WRQ 02 /* write request */
42#define DATA 03 /* data packet */
43#define ACK 04 /* acknowledgement */
44#define TFTP_ERROR 05 /* error code */
45#define OACK 06 /* option acknowledgement */
46
Elliott Hughes892a68b2015-10-19 14:43:53 -070047/*
48 * Error codes.
49 */
50#define EUNDEF 0 /* not defined */
51#define ENOTFOUND 1 /* file not found */
52#define EACCESS 2 /* access violation */
53#define ENOSPACE 3 /* disk full or allocation exceeded */
54#define EBADOP 4 /* illegal TFTP operation */
55#define EBADID 5 /* unknown transfer ID */
56#define EEXISTS 6 /* file already exists */
57#define ENOUSER 7 /* no such user */
58
The Android Open Source Project2949f582009-03-03 19:30:46 -080059
60/* op code to string mapping */
JP Abgrall53f17a92014-02-12 14:02:41 -080061static const struct tok op2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080062 { RRQ, "RRQ" }, /* read request */
63 { WRQ, "WRQ" }, /* write request */
64 { DATA, "DATA" }, /* data packet */
65 { ACK, "ACK" }, /* acknowledgement */
JP Abgrall53f17a92014-02-12 14:02:41 -080066 { TFTP_ERROR, "ERROR" }, /* error code */
The Android Open Source Project2949f582009-03-03 19:30:46 -080067 { OACK, "OACK" }, /* option acknowledgement */
68 { 0, NULL }
69};
70
71/* error code to string mapping */
JP Abgrall53f17a92014-02-12 14:02:41 -080072static const struct tok err2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080073 { EUNDEF, "EUNDEF" }, /* not defined */
74 { ENOTFOUND, "ENOTFOUND" }, /* file not found */
75 { EACCESS, "EACCESS" }, /* access violation */
76 { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
77 { EBADOP, "EBADOP" }, /* illegal TFTP operation */
78 { EBADID, "EBADID" }, /* unknown transfer ID */
79 { EEXISTS, "EEXISTS" }, /* file already exists */
80 { ENOUSER, "ENOUSER" }, /* no such user */
81 { 0, NULL }
82};
83
84/*
85 * Print trivial file transfer program requests
86 */
87void
Elliott Hughes892a68b2015-10-19 14:43:53 -070088tftp_print(netdissect_options *ndo,
Elliott Hughes820eced2021-08-20 18:00:50 -070089 const u_char *bp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -080090{
Elliott Hughes820eced2021-08-20 18:00:50 -070091 const char *cp;
92 u_int opcode;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070093 u_int ui;
The Android Open Source Project2949f582009-03-03 19:30:46 -080094
Elliott Hughes820eced2021-08-20 18:00:50 -070095 ndo->ndo_protocol = "tftp";
96
97 /* Print protocol */
98 nd_print_protocol_caps(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -080099 /* Print length */
Elliott Hughes820eced2021-08-20 18:00:50 -0700100 ND_PRINT(", length %u", length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800101
102 /* Print tftp request type */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700103 if (length < 2)
104 goto trunc;
Elliott Hughes820eced2021-08-20 18:00:50 -0700105 opcode = GET_BE_U_2(bp);
106 cp = tok2str(op2str, "tftp-#%u", opcode);
107 ND_PRINT(", %s", cp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800108 /* Bail if bogus opcode */
109 if (*cp == 't')
110 return;
Elliott Hughescec480a2017-12-19 16:54:57 -0800111 bp += 2;
112 length -= 2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800113
114 switch (opcode) {
115
116 case RRQ:
117 case WRQ:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700118 if (length == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800119 goto trunc;
Elliott Hughes820eced2021-08-20 18:00:50 -0700120 ND_PRINT(" ");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700121 /* Print filename */
Elliott Hughes820eced2021-08-20 18:00:50 -0700122 ND_PRINT("\"");
123 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
124 ND_PRINT("\"");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700125 if (ui == 0)
126 goto trunc;
Elliott Hughescec480a2017-12-19 16:54:57 -0800127 bp += ui;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700128 length -= ui;
129
130 /* Print the mode - RRQ and WRQ only */
131 if (length == 0)
132 goto trunc; /* no mode */
Elliott Hughes820eced2021-08-20 18:00:50 -0700133 ND_PRINT(" ");
134 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700135 if (ui == 0)
136 goto trunc;
Elliott Hughescec480a2017-12-19 16:54:57 -0800137 bp += ui;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700138 length -= ui;
139
140 /* Print options, if any */
141 while (length != 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700142 if (GET_U_1(bp) != '\0')
143 ND_PRINT(" ");
144 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700145 if (ui == 0)
146 goto trunc;
Elliott Hughescec480a2017-12-19 16:54:57 -0800147 bp += ui;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700148 length -= ui;
149 }
150 break;
151
152 case OACK:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700153 /* Print options */
154 while (length != 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700155 if (GET_U_1(bp) != '\0')
156 ND_PRINT(" ");
157 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700158 if (ui == 0)
159 goto trunc;
Elliott Hughescec480a2017-12-19 16:54:57 -0800160 bp += ui;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700161 length -= ui;
162 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800163 break;
164
165 case ACK:
166 case DATA:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700167 if (length < 2)
168 goto trunc; /* no block number */
Elliott Hughes820eced2021-08-20 18:00:50 -0700169 ND_PRINT(" block %u", GET_BE_U_2(bp));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800170 break;
171
JP Abgrall53f17a92014-02-12 14:02:41 -0800172 case TFTP_ERROR:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800173 /* Print error code string */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700174 if (length < 2)
175 goto trunc; /* no error code */
Elliott Hughes820eced2021-08-20 18:00:50 -0700176 ND_PRINT(" %s", tok2str(err2str, "tftp-err-#%u \"",
177 GET_BE_U_2(bp)));
Elliott Hughescec480a2017-12-19 16:54:57 -0800178 bp += 2;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700179 length -= 2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800180 /* Print error message string */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700181 if (length == 0)
182 goto trunc; /* no error message */
Elliott Hughes820eced2021-08-20 18:00:50 -0700183 ND_PRINT(" \"");
184 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
185 ND_PRINT("\"");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700186 if (ui == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187 goto trunc;
188 break;
189
190 default:
191 /* We shouldn't get here */
Elliott Hughes820eced2021-08-20 18:00:50 -0700192 ND_PRINT("(unknown #%u)", opcode);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800193 break;
194 }
195 return;
196trunc:
Elliott Hughes820eced2021-08-20 18:00:50 -0700197 nd_print_trunc(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800198}