17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
232e3b6467Skcpoon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
242e3b6467Skcpoon  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <arpa/tftp.h>
297c478bd9Sstevel@tonic-gate #include "snoop.h"
307c478bd9Sstevel@tonic-gate 
31*5b8f338eSToomas Soome struct tftp_options {
32*5b8f338eSToomas Soome 	int blksize;
33*5b8f338eSToomas Soome 	int tsize;
34*5b8f338eSToomas Soome };
35*5b8f338eSToomas Soome 
367c478bd9Sstevel@tonic-gate extern char *dlc_header;
37*5b8f338eSToomas Soome static char *tftperror(unsigned short);
38*5b8f338eSToomas Soome static char *show_type(int);
39*5b8f338eSToomas Soome static char *tftp_parse_oack(char *, size_t, struct tftp_options *);
407c478bd9Sstevel@tonic-gate 
412e3b6467Skcpoon int
interpret_tftp(int flags,void * data,int fraglen)42*5b8f338eSToomas Soome interpret_tftp(int flags, void *data, int fraglen)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	char *name, *mode;
457c478bd9Sstevel@tonic-gate 	extern int src_port, dst_port;
467c478bd9Sstevel@tonic-gate 	int blocksize = fraglen - 4;
47*5b8f338eSToomas Soome 	struct tftp_options opts;
48*5b8f338eSToomas Soome 	struct ttable *tt;
49*5b8f338eSToomas Soome 	struct tftphdr *tftp = data;
50*5b8f338eSToomas Soome 
51*5b8f338eSToomas Soome 	opts.tsize = 0;
52*5b8f338eSToomas Soome 	opts.blksize = 512;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	switch (ntohs(tftp->th_opcode)) {
557c478bd9Sstevel@tonic-gate 	case RRQ:
567c478bd9Sstevel@tonic-gate 	case WRQ:
577c478bd9Sstevel@tonic-gate 		add_transient(src_port, interpret_tftp);
587c478bd9Sstevel@tonic-gate 		break;
597c478bd9Sstevel@tonic-gate 	case ERROR:
607c478bd9Sstevel@tonic-gate 		del_transient(src_port);
617c478bd9Sstevel@tonic-gate 		break;
62*5b8f338eSToomas Soome 	case OACK:
63*5b8f338eSToomas Soome 		tt = is_transient(dst_port);
64*5b8f338eSToomas Soome 		if (tt != NULL)
65*5b8f338eSToomas Soome 			tt->blksize = opts.blksize;
66*5b8f338eSToomas Soome 		break;
67*5b8f338eSToomas Soome 	case DATA:
68*5b8f338eSToomas Soome 		tt = is_transient(dst_port);
69*5b8f338eSToomas Soome 		if (tt != NULL)
70*5b8f338eSToomas Soome 			opts.blksize = tt->blksize;
71*5b8f338eSToomas Soome 		break;
72*5b8f338eSToomas Soome 	default:
73*5b8f338eSToomas Soome 		break;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
777c478bd9Sstevel@tonic-gate 		switch (ntohs(tftp->th_opcode)) {
787c478bd9Sstevel@tonic-gate 		case RRQ:
792e3b6467Skcpoon 			name = (char *)&tftp->th_stuff;
807c478bd9Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
817c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
82*5b8f338eSToomas Soome 			    "TFTP Read \"%s\" (%s)", name, mode);
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 		case WRQ:
852e3b6467Skcpoon 			name = (char *)&tftp->th_stuff;
867c478bd9Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
877c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
88*5b8f338eSToomas Soome 			    "TFTP Write \"%s\" (%s)", name, mode);
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 		case DATA:
917c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
92*5b8f338eSToomas Soome 			    "TFTP Data block %u (%d bytes)%s",
93*5b8f338eSToomas Soome 			    ntohs(tftp->th_block), blocksize,
94*5b8f338eSToomas Soome 			    blocksize < opts.blksize ? " (last block)":"");
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case ACK:
97*5b8f338eSToomas Soome 			(void) sprintf(get_sum_line(), "TFTP Ack block %d",
98*5b8f338eSToomas Soome 			    ntohs(tftp->th_block));
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		case ERROR:
101*5b8f338eSToomas Soome 			(void) sprintf(get_sum_line(), "TFTP Error: %s",
102*5b8f338eSToomas Soome 			    tftperror(ntohs(tftp->th_code)));
103*5b8f338eSToomas Soome 			break;
104*5b8f338eSToomas Soome 		case OACK:
105*5b8f338eSToomas Soome 			(void) sprintf(get_sum_line(), "TFTP OACK: %s",
106*5b8f338eSToomas Soome 			    tftp_parse_oack((char *)&tftp->th_stuff,
107*5b8f338eSToomas Soome 			    fraglen - sizeof (tftp->th_opcode), &opts));
108*5b8f338eSToomas Soome 			if (tt != NULL)
109*5b8f338eSToomas Soome 				tt->blksize = opts.blksize;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
115*5b8f338eSToomas Soome 		show_header("TFTP:  ", "Trivial File Transfer Protocol",
116*5b8f338eSToomas Soome 		    fraglen);
117*5b8f338eSToomas Soome 		show_space();
118*5b8f338eSToomas Soome 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_opcode -
119*5b8f338eSToomas Soome 		    dlc_header, 2), "Opcode = %d (%s)", ntohs(tftp->th_opcode),
120*5b8f338eSToomas Soome 		    show_type(ntohs(tftp->th_opcode)));
1217c478bd9Sstevel@tonic-gate 
122*5b8f338eSToomas Soome 		switch (ntohs(tftp->th_opcode)) {
123*5b8f338eSToomas Soome 		case RRQ:
124*5b8f338eSToomas Soome 		case WRQ:
125*5b8f338eSToomas Soome 			name = (char *)&tftp->th_stuff;
126*5b8f338eSToomas Soome 			mode = name + (strlen(name) + 1);
127*5b8f338eSToomas Soome 			(void) sprintf(
128*5b8f338eSToomas Soome 			    get_line(name - dlc_header, strlen(name) + 1),
129*5b8f338eSToomas Soome 			    "File name = \"%s\"", name);
130*5b8f338eSToomas Soome 			(void) sprintf(
131*5b8f338eSToomas Soome 			    get_line(mode - dlc_header, strlen(mode) + 1),
132*5b8f338eSToomas Soome 			    "Transfer mode = %s", mode);
133*5b8f338eSToomas Soome 			break;
1347c478bd9Sstevel@tonic-gate 
135*5b8f338eSToomas Soome 		case DATA:
136*5b8f338eSToomas Soome 			(void) sprintf(get_line(
137*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_block - dlc_header, 2),
138*5b8f338eSToomas Soome 			    "Data block = %d%s", ntohs(tftp->th_block),
139*5b8f338eSToomas Soome 			    blocksize < opts.blksize ? " (last block)" : "");
140*5b8f338eSToomas Soome 			(void) sprintf(get_line(
141*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_data - dlc_header,
142*5b8f338eSToomas Soome 			    blocksize), "[ %d bytes of data ]", blocksize);
143*5b8f338eSToomas Soome 			break;
1447c478bd9Sstevel@tonic-gate 
145*5b8f338eSToomas Soome 		case ACK:
146*5b8f338eSToomas Soome 			(void) sprintf(get_line(
147*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_block - dlc_header, 2),
148*5b8f338eSToomas Soome 			    "Acknowledge block = %d", ntohs(tftp->th_block));
149*5b8f338eSToomas Soome 			break;
1507c478bd9Sstevel@tonic-gate 
151*5b8f338eSToomas Soome 		case ERROR:
152*5b8f338eSToomas Soome 			(void) sprintf(get_line(
153*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_code - dlc_header, 2),
154*5b8f338eSToomas Soome 			    "Error = %d (%s)", ntohs(tftp->th_code),
155*5b8f338eSToomas Soome 			    tftperror(ntohs(tftp->th_code)));
156*5b8f338eSToomas Soome 			(void) sprintf(get_line(
157*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_data -
158*5b8f338eSToomas Soome 			    dlc_header, strlen(tftp->th_data) + 1),
159*5b8f338eSToomas Soome 			    "Error string = \"%s\"", tftp->th_data);
160*5b8f338eSToomas Soome 			break;
161*5b8f338eSToomas Soome 		case OACK:
162*5b8f338eSToomas Soome 			(void) sprintf(get_line(
163*5b8f338eSToomas Soome 			    (char *)(uintptr_t)tftp->th_code - dlc_header, 2),
164*5b8f338eSToomas Soome 			    "TFTP OACK: %s",
165*5b8f338eSToomas Soome 			    tftp_parse_oack((char *)&tftp->th_stuff,
166*5b8f338eSToomas Soome 			    fraglen - sizeof (tftp->th_opcode), &opts));
167*5b8f338eSToomas Soome 			if (tt != NULL)
168*5b8f338eSToomas Soome 				tt->blksize = opts.blksize;
169*5b8f338eSToomas Soome 			break;
170*5b8f338eSToomas Soome 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (fraglen);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
176*5b8f338eSToomas Soome static char *
show_type(int t)177*5b8f338eSToomas Soome show_type(int t)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	switch (t) {
1807c478bd9Sstevel@tonic-gate 	case RRQ:	return ("read request");
1817c478bd9Sstevel@tonic-gate 	case WRQ:	return ("write request");
1827c478bd9Sstevel@tonic-gate 	case DATA:	return ("data packet");
1837c478bd9Sstevel@tonic-gate 	case ACK:	return ("acknowledgement");
1847c478bd9Sstevel@tonic-gate 	case ERROR:	return ("error");
185*5b8f338eSToomas Soome 	case OACK:	return ("option acknowledgement");
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	return ("?");
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
190*5b8f338eSToomas Soome static char *
tftperror(unsigned short code)191*5b8f338eSToomas Soome tftperror(unsigned short code)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	static char buf[128];
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	switch (code) {
1967c478bd9Sstevel@tonic-gate 	case EUNDEF:	return ("not defined");
1977c478bd9Sstevel@tonic-gate 	case ENOTFOUND:	return ("file not found");
1987c478bd9Sstevel@tonic-gate 	case EACCESS:	return ("access violation");
1997c478bd9Sstevel@tonic-gate 	case ENOSPACE:	return ("disk full or allocation exceeded");
2007c478bd9Sstevel@tonic-gate 	case EBADOP:	return ("illegal TFTP operation");
2017c478bd9Sstevel@tonic-gate 	case EBADID:	return ("unknown transfer ID");
2027c478bd9Sstevel@tonic-gate 	case EEXISTS:	return ("file already exists");
2037c478bd9Sstevel@tonic-gate 	case ENOUSER:	return ("no such user");
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%d", code);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (buf);
2087c478bd9Sstevel@tonic-gate }
209*5b8f338eSToomas Soome 
210*5b8f338eSToomas Soome static char *
tftp_parse_oack(char * buf,size_t size,struct tftp_options * opts)211*5b8f338eSToomas Soome tftp_parse_oack(char *buf, size_t size, struct tftp_options *opts)
212*5b8f338eSToomas Soome {
213*5b8f338eSToomas Soome 	static char tftp_options[128];
214*5b8f338eSToomas Soome 	int i, idx;
215*5b8f338eSToomas Soome 
216*5b8f338eSToomas Soome 	tftp_options[0] = '\0';
217*5b8f338eSToomas Soome 	idx = 0;
218*5b8f338eSToomas Soome 
219*5b8f338eSToomas Soome 	while (size > 0 && idx < sizeof (tftp_options)) {
220*5b8f338eSToomas Soome 		if (idx > 0) {
221*5b8f338eSToomas Soome 			tftp_options[idx++] = ' ';
222*5b8f338eSToomas Soome 			tftp_options[idx] = '\0';
223*5b8f338eSToomas Soome 		}
224*5b8f338eSToomas Soome 
225*5b8f338eSToomas Soome 		/* get name */
226*5b8f338eSToomas Soome 		if (idx + strnlen(buf, size) + 1 > sizeof (tftp_options))
227*5b8f338eSToomas Soome 			break;
228*5b8f338eSToomas Soome 		for (i = 0; i < size; i++) {
229*5b8f338eSToomas Soome 			tftp_options[idx] = buf[i];
230*5b8f338eSToomas Soome 			if (tftp_options[idx] == '\0') {
231*5b8f338eSToomas Soome 				i++;
232*5b8f338eSToomas Soome 				break;
233*5b8f338eSToomas Soome 			}
234*5b8f338eSToomas Soome 			idx++;
235*5b8f338eSToomas Soome 		}
236*5b8f338eSToomas Soome 		size -= i;
237*5b8f338eSToomas Soome 		/*
238*5b8f338eSToomas Soome 		 * RFC 2348 requires this case in-sensitive.
239*5b8f338eSToomas Soome 		 */
240*5b8f338eSToomas Soome 		if (strcasecmp(buf, "blksize") == 0) {
241*5b8f338eSToomas Soome 			int blksize = strtol(buf + i, NULL, 0);
242*5b8f338eSToomas Soome 
243*5b8f338eSToomas Soome 			if (blksize >= 8)
244*5b8f338eSToomas Soome 				opts->blksize = blksize;
245*5b8f338eSToomas Soome 		}
246*5b8f338eSToomas Soome 		buf += i;
247*5b8f338eSToomas Soome 
248*5b8f338eSToomas Soome 		/* can we store separator? */
249*5b8f338eSToomas Soome 		if (idx + 3 > sizeof (tftp_options))
250*5b8f338eSToomas Soome 			break;
251*5b8f338eSToomas Soome 		strcat(tftp_options, ": ");
252*5b8f338eSToomas Soome 		idx += 2;
253*5b8f338eSToomas Soome 
254*5b8f338eSToomas Soome 		/* get value */
255*5b8f338eSToomas Soome 		if (idx + strnlen(buf, size) + 1 > sizeof (tftp_options))
256*5b8f338eSToomas Soome 			break;
257*5b8f338eSToomas Soome 
258*5b8f338eSToomas Soome 		for (i = 0; i < size; i++) {
259*5b8f338eSToomas Soome 			tftp_options[idx] = buf[i];
260*5b8f338eSToomas Soome 			if (tftp_options[idx] == '\0') {
261*5b8f338eSToomas Soome 				i++;
262*5b8f338eSToomas Soome 				break;
263*5b8f338eSToomas Soome 			}
264*5b8f338eSToomas Soome 			idx++;
265*5b8f338eSToomas Soome 		}
266*5b8f338eSToomas Soome 		size -= i;
267*5b8f338eSToomas Soome 		buf += i;
268*5b8f338eSToomas Soome 	}
269*5b8f338eSToomas Soome 	return (tftp_options);
270*5b8f338eSToomas Soome }
271