xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_tftp.c (revision 410:51e39f6c6311)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*410Skcpoon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*410Skcpoon  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
27*410Skcpoon #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <arpa/tftp.h>
310Sstevel@tonic-gate #include "snoop.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate extern char *dlc_header;
340Sstevel@tonic-gate char *tftperror();
350Sstevel@tonic-gate char *show_type();
360Sstevel@tonic-gate 
37*410Skcpoon int
interpret_tftp(int flags,struct tftphdr * tftp,int fraglen)38*410Skcpoon interpret_tftp(int flags, struct tftphdr *tftp, int fraglen)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	char *name, *mode;
410Sstevel@tonic-gate 	extern int src_port, dst_port;
420Sstevel@tonic-gate 	int blocksize = fraglen - 4;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	switch (ntohs(tftp->th_opcode)) {
450Sstevel@tonic-gate 	case RRQ:
460Sstevel@tonic-gate 	case WRQ:
470Sstevel@tonic-gate 		add_transient(src_port, interpret_tftp);
480Sstevel@tonic-gate 		break;
490Sstevel@tonic-gate 	case ERROR:
500Sstevel@tonic-gate 		del_transient(src_port);
510Sstevel@tonic-gate 		break;
520Sstevel@tonic-gate 	}
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if (flags & F_SUM) {
550Sstevel@tonic-gate 		switch (ntohs(tftp->th_opcode)) {
560Sstevel@tonic-gate 		case RRQ:
57*410Skcpoon 			name = (char *)&tftp->th_stuff;
580Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
590Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
600Sstevel@tonic-gate 				"TFTP Read \"%s\" (%s)", name, mode);
610Sstevel@tonic-gate 			break;
620Sstevel@tonic-gate 		case WRQ:
63*410Skcpoon 			name = (char *)&tftp->th_stuff;
640Sstevel@tonic-gate 			mode = name + (strlen(name) + 1);
650Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
660Sstevel@tonic-gate 				"TFTP Write \"%s\" (%s)", name, mode);
670Sstevel@tonic-gate 			break;
680Sstevel@tonic-gate 		case DATA:
690Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
700Sstevel@tonic-gate 				"TFTP Data block %d (%d bytes)%s",
710Sstevel@tonic-gate 				ntohs(tftp->th_block),
720Sstevel@tonic-gate 				blocksize,
730Sstevel@tonic-gate 				blocksize < 512 ? " (last block)":"");
740Sstevel@tonic-gate 			break;
750Sstevel@tonic-gate 		case ACK:
760Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
770Sstevel@tonic-gate 				"TFTP Ack  block %d",
780Sstevel@tonic-gate 				ntohs(tftp->th_block));
790Sstevel@tonic-gate 			break;
800Sstevel@tonic-gate 		case ERROR:
810Sstevel@tonic-gate 			(void) sprintf(get_sum_line(),
820Sstevel@tonic-gate 				"TFTP Error: %s",
830Sstevel@tonic-gate 				tftperror(ntohs(tftp->th_code)));
840Sstevel@tonic-gate 			break;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (flags & F_DTAIL) {
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	show_header("TFTP:  ", "Trivial File Transfer Protocol", fraglen);
910Sstevel@tonic-gate 	show_space();
92*410Skcpoon 	(void) sprintf(get_line((char *)(uintptr_t)tftp->th_opcode -
93*410Skcpoon 		dlc_header, 2),
940Sstevel@tonic-gate 		"Opcode = %d (%s)",
950Sstevel@tonic-gate 		ntohs(tftp->th_opcode),
960Sstevel@tonic-gate 		show_type(ntohs(tftp->th_opcode)));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	switch (ntohs(tftp->th_opcode)) {
990Sstevel@tonic-gate 	case RRQ:
1000Sstevel@tonic-gate 	case WRQ:
101*410Skcpoon 		name = (char *)&tftp->th_stuff;
1020Sstevel@tonic-gate 		mode = name + (strlen(name) + 1);
1030Sstevel@tonic-gate 		(void) sprintf(
1040Sstevel@tonic-gate 			get_line(name - dlc_header, strlen(name) + 1),
1050Sstevel@tonic-gate 			"File name = \"%s\"",
1060Sstevel@tonic-gate 			name);
1070Sstevel@tonic-gate 		(void) sprintf(
1080Sstevel@tonic-gate 			get_line(mode - dlc_header, strlen(mode) + 1),
1090Sstevel@tonic-gate 			"Transfer mode = %s",
1100Sstevel@tonic-gate 			mode);
1110Sstevel@tonic-gate 		break;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	case DATA:
1140Sstevel@tonic-gate 		(void) sprintf(
115*410Skcpoon 			get_line((char *)(uintptr_t)tftp->th_block -
116*410Skcpoon 			dlc_header, 2),	"Data block = %d%s",
1170Sstevel@tonic-gate 			ntohs(tftp->th_block),
1180Sstevel@tonic-gate 			blocksize < 512 ? " (last block)":"");
119*410Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_data -
120*410Skcpoon 			dlc_header, blocksize),
1210Sstevel@tonic-gate 			"[ %d bytes of data ]",
1220Sstevel@tonic-gate 			blocksize);
1230Sstevel@tonic-gate 		break;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	case ACK:
126*410Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_block -
127*410Skcpoon 			dlc_header, 2),	"Acknowledge block = %d",
1280Sstevel@tonic-gate 			ntohs(tftp->th_block));
1290Sstevel@tonic-gate 		break;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	case ERROR:
132*410Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_code -
133*410Skcpoon 			dlc_header, 2),	"Error = %d (%s)",
1340Sstevel@tonic-gate 			ntohs(tftp->th_code),
1350Sstevel@tonic-gate 			tftperror(ntohs(tftp->th_code)));
136*410Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)tftp->th_data -
137*410Skcpoon 			dlc_header, strlen(tftp->th_data) + 1),
138*410Skcpoon 			"Error string = \"%s\"", tftp->th_data);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	return (fraglen);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate char *
show_type(t)1460Sstevel@tonic-gate show_type(t)
1470Sstevel@tonic-gate 	int t;
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	switch (t) {
1500Sstevel@tonic-gate 	case RRQ:	return ("read request");
1510Sstevel@tonic-gate 	case WRQ:	return ("write request");
1520Sstevel@tonic-gate 	case DATA:	return ("data packet");
1530Sstevel@tonic-gate 	case ACK:	return ("acknowledgement");
1540Sstevel@tonic-gate 	case ERROR:	return ("error");
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	return ("?");
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate char *
tftperror(code)1600Sstevel@tonic-gate tftperror(code)
1610Sstevel@tonic-gate     unsigned short code;
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	static char buf[128];
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	switch (code) {
1660Sstevel@tonic-gate 	case EUNDEF:	return ("not defined");
1670Sstevel@tonic-gate 	case ENOTFOUND:	return ("file not found");
1680Sstevel@tonic-gate 	case EACCESS:	return ("access violation");
1690Sstevel@tonic-gate 	case ENOSPACE:	return ("disk full or allocation exceeded");
1700Sstevel@tonic-gate 	case EBADOP:	return ("illegal TFTP operation");
1710Sstevel@tonic-gate 	case EBADID:	return ("unknown transfer ID");
1720Sstevel@tonic-gate 	case EEXISTS:	return ("file already exists");
1730Sstevel@tonic-gate 	case ENOUSER:	return ("no such user");
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	(void) sprintf(buf, "%d", code);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	return (buf);
1780Sstevel@tonic-gate }
179