Home | History | Annotate | Line # | Download | only in dist
      1 /*
      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.
     20  */
     21 
     22 /* \summary: Trivial File Transfer Protocol (TFTP) printer */
     23 
     24 #include <sys/cdefs.h>
     25 #ifndef lint
     26 __RCSID("$NetBSD: print-tftp.c,v 1.10 2024/09/02 16:15:33 christos Exp $");
     27 #endif
     28 
     29 #include <config.h>
     30 
     31 #include "netdissect-stdinc.h"
     32 
     33 #include "netdissect.h"
     34 #include "extract.h"
     35 
     36 /*
     37  * Trivial File Transfer Protocol (IEN-133)
     38  */
     39 
     40 /*
     41  * Packet types.
     42  */
     43 #define	RRQ	01			/* read request */
     44 #define	WRQ	02			/* write request */
     45 #define	DATA	03			/* data packet */
     46 #define	ACK	04			/* acknowledgement */
     47 #define	TFTP_ERROR	05			/* error code */
     48 #define OACK	06			/* option acknowledgement */
     49 
     50 /*
     51  * Error codes.
     52  */
     53 #define	EUNDEF		0		/* not defined */
     54 #define	ENOTFOUND	1		/* file not found */
     55 #define	EACCESS		2		/* access violation */
     56 #define	ENOSPACE	3		/* disk full or allocation exceeded */
     57 #define	EBADOP		4		/* illegal TFTP operation */
     58 #define	EBADID		5		/* unknown transfer ID */
     59 #define	EEXISTS		6		/* file already exists */
     60 #define	ENOUSER		7		/* no such user */
     61 
     62 
     63 /* op code to string mapping */
     64 static const struct tok op2str[] = {
     65 	{ RRQ,		"RRQ" },	/* read request */
     66 	{ WRQ,		"WRQ" },	/* write request */
     67 	{ DATA,		"DATA" },	/* data packet */
     68 	{ ACK,		"ACK" },	/* acknowledgement */
     69 	{ TFTP_ERROR,	"ERROR" },	/* error code */
     70 	{ OACK,		"OACK" },	/* option acknowledgement */
     71 	{ 0,		NULL }
     72 };
     73 
     74 /* error code to string mapping */
     75 static const struct tok err2str[] = {
     76 	{ EUNDEF,	"EUNDEF" },	/* not defined */
     77 	{ ENOTFOUND,	"ENOTFOUND" },	/* file not found */
     78 	{ EACCESS,	"EACCESS" },	/* access violation */
     79 	{ ENOSPACE,	"ENOSPACE" },	/* disk full or allocation exceeded */
     80 	{ EBADOP,	"EBADOP" },	/* illegal TFTP operation */
     81 	{ EBADID,	"EBADID" },	/* unknown transfer ID */
     82 	{ EEXISTS,	"EEXISTS" },	/* file already exists */
     83 	{ ENOUSER,	"ENOUSER" },	/* no such user */
     84 	{ 0,		NULL }
     85 };
     86 
     87 /*
     88  * Print trivial file transfer program requests
     89  */
     90 void
     91 tftp_print(netdissect_options *ndo,
     92            const u_char *bp, u_int length)
     93 {
     94 	const char *cp;
     95 	u_int opcode;
     96 	u_int ui;
     97 
     98 	ndo->ndo_protocol = "tftp";
     99 
    100 	/* Print protocol */
    101 	nd_print_protocol_caps(ndo);
    102 	/* Print length */
    103 	ND_PRINT(", length %u", length);
    104 
    105 	/* Print tftp request type */
    106 	if (length < 2)
    107 		goto trunc;
    108 	opcode = GET_BE_U_2(bp);
    109 	cp = tok2str(op2str, "tftp-#%u", opcode);
    110 	ND_PRINT(", %s", cp);
    111 	/* Bail if bogus opcode */
    112 	if (*cp == 't')
    113 		return;
    114 	bp += 2;
    115 	length -= 2;
    116 
    117 	switch (opcode) {
    118 
    119 	case RRQ:
    120 	case WRQ:
    121 		if (length == 0)
    122 			goto trunc;
    123 		ND_PRINT(" ");
    124 		/* Print filename */
    125 		ND_PRINT("\"");
    126 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
    127 		ND_PRINT("\"");
    128 		if (ui == 0)
    129 			goto trunc;
    130 		bp += ui;
    131 		length -= ui;
    132 
    133 		/* Print the mode - RRQ and WRQ only */
    134 		if (length == 0)
    135 			goto trunc;	/* no mode */
    136 		ND_PRINT(" ");
    137 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
    138 		if (ui == 0)
    139 			goto trunc;
    140 		bp += ui;
    141 		length -= ui;
    142 
    143 		/* Print options, if any */
    144 		while (length != 0) {
    145 			if (GET_U_1(bp) != '\0')
    146 				ND_PRINT(" ");
    147 			ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
    148 			if (ui == 0)
    149 				goto trunc;
    150 			bp += ui;
    151 			length -= ui;
    152 		}
    153 		break;
    154 
    155 	case OACK:
    156 		/* Print options */
    157 		while (length != 0) {
    158 			if (GET_U_1(bp) != '\0')
    159 				ND_PRINT(" ");
    160 			ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
    161 			if (ui == 0)
    162 				goto trunc;
    163 			bp += ui;
    164 			length -= ui;
    165 		}
    166 		break;
    167 
    168 	case ACK:
    169 	case DATA:
    170 		if (length < 2)
    171 			goto trunc;	/* no block number */
    172 		ND_PRINT(" block %u", GET_BE_U_2(bp));
    173 		break;
    174 
    175 	case TFTP_ERROR:
    176 		/* Print error code string */
    177 		if (length < 2)
    178 			goto trunc;	/* no error code */
    179 		ND_PRINT(" %s", tok2str(err2str, "tftp-err-#%u \"",
    180 				       GET_BE_U_2(bp)));
    181 		bp += 2;
    182 		length -= 2;
    183 		/* Print error message string */
    184 		if (length == 0)
    185 			goto trunc;	/* no error message */
    186 		ND_PRINT(" \"");
    187 		ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
    188 		ND_PRINT("\"");
    189 		if (ui == 0)
    190 			goto trunc;
    191 		break;
    192 
    193 	default:
    194 		/* We shouldn't get here */
    195 		ND_PRINT("(unknown #%u)", opcode);
    196 		break;
    197 	}
    198 	return;
    199 trunc:
    200 	nd_print_trunc(ndo);
    201 }
    202