xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_tftp.c (revision 2e3b64671f0fdac42d7fb21a8fa7e3ce9fce3359)
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 /*
23*2e3b6467Skcpoon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*2e3b6467Skcpoon  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*2e3b6467Skcpoon #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <arpa/tftp.h>
317c478bd9Sstevel@tonic-gate #include "snoop.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate extern char *dlc_header;
347c478bd9Sstevel@tonic-gate char *tftperror();
357c478bd9Sstevel@tonic-gate char *show_type();
367c478bd9Sstevel@tonic-gate 
37*2e3b6467Skcpoon int
38*2e3b6467Skcpoon interpret_tftp(int flags, struct tftphdr *tftp, int fraglen)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	char *name, *mode;
417c478bd9Sstevel@tonic-gate 	extern int src_port, dst_port;
427c478bd9Sstevel@tonic-gate 	int blocksize = fraglen - 4;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	switch (ntohs(tftp->th_opcode)) {
457c478bd9Sstevel@tonic-gate 	case RRQ:
467c478bd9Sstevel@tonic-gate 	case WRQ:
477c478bd9Sstevel@tonic-gate 		add_transient(src_port, interpret_tftp);
487c478bd9Sstevel@tonic-gate 		break;
497c478bd9Sstevel@tonic-gate 	case ERROR:
507c478bd9Sstevel@tonic-gate 		del_transient(src_port);
517c478bd9Sstevel@tonic-gate 		break;
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
557c478bd9Sstevel@tonic-gate 		switch (ntohs(tftp->th_opcode)) {
567c478bd9Sstevel@tonic-gate 		case RRQ:
577c478bd9Sstevel@tonic-gate 			name = (char *)&tftp->th_stuff;
587c478bd9Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
597c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
607c478bd9Sstevel@tonic-gate 				"TFTP Read \"%s\" (%s)", name, mode);
617c478bd9Sstevel@tonic-gate 			break;
627c478bd9Sstevel@tonic-gate 		case WRQ:
637c478bd9Sstevel@tonic-gate 			name = (char *)&tftp->th_stuff;
647c478bd9Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
657c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
667c478bd9Sstevel@tonic-gate 				"TFTP Write \"%s\" (%s)", name, mode);
677c478bd9Sstevel@tonic-gate 			break;
687c478bd9Sstevel@tonic-gate 		case DATA:
697c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
707c478bd9Sstevel@tonic-gate 				"TFTP Data block %d (%d bytes)%s",
717c478bd9Sstevel@tonic-gate 				ntohs(tftp->th_block),
727c478bd9Sstevel@tonic-gate 				blocksize,
737c478bd9Sstevel@tonic-gate 				blocksize < 512 ? " (last block)":"");
747c478bd9Sstevel@tonic-gate 			break;
757c478bd9Sstevel@tonic-gate 		case ACK:
767c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
777c478bd9Sstevel@tonic-gate 				"TFTP Ack  block %d",
787c478bd9Sstevel@tonic-gate 				ntohs(tftp->th_block));
797c478bd9Sstevel@tonic-gate 			break;
807c478bd9Sstevel@tonic-gate 		case ERROR:
817c478bd9Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
827c478bd9Sstevel@tonic-gate 				"TFTP Error: %s",
837c478bd9Sstevel@tonic-gate 				tftperror(ntohs(tftp->th_code)));
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	show_header("TFTP:  ", "Trivial File Transfer Protocol", fraglen);
917c478bd9Sstevel@tonic-gate 	show_space();
92*2e3b6467Skcpoon 	(void) sprintf(get_line((char *)(uintptr_t)tftp->th_opcode -
93*2e3b6467Skcpoon 		dlc_header, 2),
947c478bd9Sstevel@tonic-gate 		"Opcode = %d (%s)",
957c478bd9Sstevel@tonic-gate 		ntohs(tftp->th_opcode),
967c478bd9Sstevel@tonic-gate 		show_type(ntohs(tftp->th_opcode)));
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	switch (ntohs(tftp->th_opcode)) {
997c478bd9Sstevel@tonic-gate 	case RRQ:
1007c478bd9Sstevel@tonic-gate 	case WRQ:
1017c478bd9Sstevel@tonic-gate 		name = (char *)&tftp->th_stuff;
1027c478bd9Sstevel@tonic-gate 		mode = name + (strlen(name) + 1);
1037c478bd9Sstevel@tonic-gate 		(void) sprintf(
1047c478bd9Sstevel@tonic-gate 			get_line(name - dlc_header, strlen(name) + 1),
1057c478bd9Sstevel@tonic-gate 			"File name = \"%s\"",
1067c478bd9Sstevel@tonic-gate 			name);
1077c478bd9Sstevel@tonic-gate 		(void) sprintf(
1087c478bd9Sstevel@tonic-gate 			get_line(mode - dlc_header, strlen(mode) + 1),
1097c478bd9Sstevel@tonic-gate 			"Transfer mode = %s",
1107c478bd9Sstevel@tonic-gate 			mode);
1117c478bd9Sstevel@tonic-gate 		break;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	case DATA:
1147c478bd9Sstevel@tonic-gate 		(void) sprintf(
115*2e3b6467Skcpoon 			get_line((char *)(uintptr_t)tftp->th_block -
116*2e3b6467Skcpoon 			dlc_header, 2),	"Data block = %d%s",
1177c478bd9Sstevel@tonic-gate 			ntohs(tftp->th_block),
1187c478bd9Sstevel@tonic-gate 			blocksize < 512 ? " (last block)":"");
119*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_data -
120*2e3b6467Skcpoon 			dlc_header, blocksize),
1217c478bd9Sstevel@tonic-gate 			"[ %d bytes of data ]",
1227c478bd9Sstevel@tonic-gate 			blocksize);
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	case ACK:
126*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_block -
127*2e3b6467Skcpoon 			dlc_header, 2),	"Acknowledge block = %d",
1287c478bd9Sstevel@tonic-gate 			ntohs(tftp->th_block));
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	case ERROR:
132*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_code -
133*2e3b6467Skcpoon 			dlc_header, 2),	"Error = %d (%s)",
1347c478bd9Sstevel@tonic-gate 			ntohs(tftp->th_code),
1357c478bd9Sstevel@tonic-gate 			tftperror(ntohs(tftp->th_code)));
136*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_data -
137*2e3b6467Skcpoon 			dlc_header, strlen(tftp->th_data) + 1),
138*2e3b6467Skcpoon 			"Error string = \"%s\"", tftp->th_data);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return (fraglen);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate char *
1467c478bd9Sstevel@tonic-gate show_type(t)
1477c478bd9Sstevel@tonic-gate 	int t;
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	switch (t) {
1507c478bd9Sstevel@tonic-gate 	case RRQ:	return ("read request");
1517c478bd9Sstevel@tonic-gate 	case WRQ:	return ("write request");
1527c478bd9Sstevel@tonic-gate 	case DATA:	return ("data packet");
1537c478bd9Sstevel@tonic-gate 	case ACK:	return ("acknowledgement");
1547c478bd9Sstevel@tonic-gate 	case ERROR:	return ("error");
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	return ("?");
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate char *
1607c478bd9Sstevel@tonic-gate tftperror(code)
1617c478bd9Sstevel@tonic-gate     unsigned short code;
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	static char buf[128];
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	switch (code) {
1667c478bd9Sstevel@tonic-gate 	case EUNDEF:	return ("not defined");
1677c478bd9Sstevel@tonic-gate 	case ENOTFOUND:	return ("file not found");
1687c478bd9Sstevel@tonic-gate 	case EACCESS:	return ("access violation");
1697c478bd9Sstevel@tonic-gate 	case ENOSPACE:	return ("disk full or allocation exceeded");
1707c478bd9Sstevel@tonic-gate 	case EBADOP:	return ("illegal TFTP operation");
1717c478bd9Sstevel@tonic-gate 	case EBADID:	return ("unknown transfer ID");
1727c478bd9Sstevel@tonic-gate 	case EEXISTS:	return ("file already exists");
1737c478bd9Sstevel@tonic-gate 	case ENOUSER:	return ("no such user");
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%d", code);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (buf);
1787c478bd9Sstevel@tonic-gate }
179