blob: ee0caf3cfe4f3ab4e97564bf6a5500d6dc04b220 [file] [log] [blame]
Elliott Hughes892a68b2015-10-19 14:43:53 -07001/*
Elliott Hughes892a68b2015-10-19 14:43:53 -07002 * Copyright (c) 2014 The TCPDUMP project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
Elliott Hughese2e3bd12017-05-15 10:59:29 -070028/* \summary: Loopback Protocol printer */
29
30/*
31 * originally defined as the Ethernet Configuration Testing Protocol.
Elliott Hughes820eced2021-08-20 18:00:50 -070032 * specification: https://www.mit.edu/people/jhawk/ctp.pdf
Elliott Hughese2e3bd12017-05-15 10:59:29 -070033 */
34
Elliott Hughes892a68b2015-10-19 14:43:53 -070035#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070036#include <config.h>
Elliott Hughes892a68b2015-10-19 14:43:53 -070037#endif
38
Elliott Hughes820eced2021-08-20 18:00:50 -070039#include "netdissect-stdinc.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070040
Elliott Hughes820eced2021-08-20 18:00:50 -070041#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070042#include "netdissect.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070043#include "extract.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070044#include "addrtoname.h"
45
Elliott Hughes892a68b2015-10-19 14:43:53 -070046
47#define LOOPBACK_REPLY 1
48#define LOOPBACK_FWDDATA 2
49
50static const struct tok fcode_str[] = {
51 { LOOPBACK_REPLY, "Reply" },
52 { LOOPBACK_FWDDATA, "Forward Data" },
53 { 0, NULL }
54};
55
56static void
Elliott Hughes820eced2021-08-20 18:00:50 -070057loopback_message_print(netdissect_options *ndo,
58 const u_char *cp, u_int len)
Elliott Hughes892a68b2015-10-19 14:43:53 -070059{
Elliott Hughes892a68b2015-10-19 14:43:53 -070060 uint16_t function;
61
62 if (len < 2)
Elliott Hughese2e3bd12017-05-15 10:59:29 -070063 goto invalid;
Elliott Hughes892a68b2015-10-19 14:43:53 -070064 /* function */
Elliott Hughes820eced2021-08-20 18:00:50 -070065 function = GET_LE_U_2(cp);
Elliott Hughes892a68b2015-10-19 14:43:53 -070066 cp += 2;
Elliott Hughes820eced2021-08-20 18:00:50 -070067 len -= 2;
68 ND_PRINT(", %s", tok2str(fcode_str, " invalid (%u)", function));
Elliott Hughes892a68b2015-10-19 14:43:53 -070069
70 switch (function) {
71 case LOOPBACK_REPLY:
Elliott Hughes820eced2021-08-20 18:00:50 -070072 if (len < 2)
Elliott Hughese2e3bd12017-05-15 10:59:29 -070073 goto invalid;
Elliott Hughes892a68b2015-10-19 14:43:53 -070074 /* receipt number */
Elliott Hughes820eced2021-08-20 18:00:50 -070075 ND_PRINT(", receipt number %u", GET_LE_U_2(cp));
Elliott Hughes892a68b2015-10-19 14:43:53 -070076 cp += 2;
Elliott Hughes820eced2021-08-20 18:00:50 -070077 len -= 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -070078 /* data */
Elliott Hughes820eced2021-08-20 18:00:50 -070079 ND_PRINT(", data (%u octets)", len);
80 ND_TCHECK_LEN(cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -070081 break;
82 case LOOPBACK_FWDDATA:
Elliott Hughes820eced2021-08-20 18:00:50 -070083 if (len < MAC_ADDR_LEN)
Elliott Hughese2e3bd12017-05-15 10:59:29 -070084 goto invalid;
Elliott Hughes892a68b2015-10-19 14:43:53 -070085 /* forwarding address */
Elliott Hughes820eced2021-08-20 18:00:50 -070086 ND_PRINT(", forwarding address %s", GET_ETHERADDR_STRING(cp));
87 cp += MAC_ADDR_LEN;
88 len -= MAC_ADDR_LEN;
Elliott Hughes892a68b2015-10-19 14:43:53 -070089 /* data */
Elliott Hughes820eced2021-08-20 18:00:50 -070090 ND_PRINT(", data (%u octets)", len);
91 ND_TCHECK_LEN(cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -070092 break;
93 default:
Elliott Hughes820eced2021-08-20 18:00:50 -070094 ND_TCHECK_LEN(cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -070095 break;
96 }
97 return;
98
Elliott Hughese2e3bd12017-05-15 10:59:29 -070099invalid:
Elliott Hughes820eced2021-08-20 18:00:50 -0700100 nd_print_invalid(ndo);
101 ND_TCHECK_LEN(cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700102}
103
104void
Elliott Hughes820eced2021-08-20 18:00:50 -0700105loopback_print(netdissect_options *ndo,
106 const u_char *cp, u_int len)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700107{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108 uint16_t skipCount;
109
Elliott Hughes820eced2021-08-20 18:00:50 -0700110 ndo->ndo_protocol = "loopback";
111 ND_PRINT("Loopback");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700112 if (len < 2)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700113 goto invalid;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700114 /* skipCount */
Elliott Hughes820eced2021-08-20 18:00:50 -0700115 skipCount = GET_LE_U_2(cp);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700116 cp += 2;
Elliott Hughes820eced2021-08-20 18:00:50 -0700117 len -= 2;
118 ND_PRINT(", skipCount %u", skipCount);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700119 if (skipCount % 8)
Elliott Hughes820eced2021-08-20 18:00:50 -0700120 ND_PRINT(" (bogus)");
121 if (skipCount > len)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700122 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -0700123 /* the octets to skip */
124 ND_TCHECK_LEN(cp, skipCount);
125 cp += skipCount;
126 len -= skipCount;
127 /* the first message to decode */
128 loopback_message_print(ndo, cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129 return;
130
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700131invalid:
Elliott Hughes820eced2021-08-20 18:00:50 -0700132 nd_print_invalid(ndo);
133 ND_TCHECK_LEN(cp, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700134}
135