blob: 12a63cd6370c17454490b18b8d77d88955dfce5b [file] [log] [blame]
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001/* Copyright (c) 2015, bugyo
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright notice,
9 * this list of conditions and the following disclaimer in the documentation
10 * and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24/* \summary: Network Service Header (NSH) printer */
25
Elliott Hughes820eced2021-08-20 18:00:50 -070026/* specification: RFC 8300 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -070027
28#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070029#include <config.h>
Elliott Hughese2e3bd12017-05-15 10:59:29 -070030#endif
31
Elliott Hughes820eced2021-08-20 18:00:50 -070032#include "netdissect-stdinc.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -070033
Elliott Hughes820eced2021-08-20 18:00:50 -070034#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070035#include "netdissect.h"
36#include "extract.h"
37
Elliott Hughese2e3bd12017-05-15 10:59:29 -070038static const struct tok nsh_flags [] = {
Elliott Hughes820eced2021-08-20 18:00:50 -070039 { 0x2, "O" },
Elliott Hughese2e3bd12017-05-15 10:59:29 -070040 { 0, NULL }
41};
42
Elliott Hughes820eced2021-08-20 18:00:50 -070043/*
44 * 0 1 2 3
45 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * |Ver|O|U| TTL | Length |U|U|U|U|MD Type| Next Protocol |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 */
Elliott Hughese2e3bd12017-05-15 10:59:29 -070050#define NSH_BASE_HDR_LEN 4
Elliott Hughes820eced2021-08-20 18:00:50 -070051#define NSH_VER(x) (((x) & 0xc0000000) >> 30)
52#define NSH_FLAGS(x) (((x) & 0x30000000) >> 28)
53#define NSH_TTL(x) (((x) & 0x0fc00000) >> 22)
54#define NSH_LENGTH(x) (((x) & 0x003f0000) >> 16)
55#define NSH_MD_TYPE(x) (((x) & 0x00000f00) >> 8)
56#define NSH_NEXT_PROT(x) (((x) & 0x000000ff) >> 0)
57
Elliott Hughese2e3bd12017-05-15 10:59:29 -070058#define NSH_SERVICE_PATH_HDR_LEN 4
59#define NSH_HDR_WORD_SIZE 4U
60
Elliott Hughes820eced2021-08-20 18:00:50 -070061#define MD_RSV 0x00
62#define MD_TYPE1 0x01
63#define MD_TYPE2 0x02
64#define MD_EXP 0x0F
65static const struct tok md_str[] = {
66 { MD_RSV, "reserved" },
67 { MD_TYPE1, "1" },
68 { MD_TYPE2, "2" },
69 { MD_EXP, "experimental" },
70 { 0, NULL }
71};
72
73#define NP_IPV4 0x01
74#define NP_IPV6 0x02
75#define NP_ETH 0x03
76#define NP_NSH 0x04
77#define NP_MPLS 0x05
78#define NP_EXP1 0xFE
79#define NP_EXP2 0xFF
80static const struct tok np_str[] = {
81 { NP_IPV4, "IPv4" },
82 { NP_IPV6, "IPv6" },
83 { NP_ETH, "Ethernet" },
84 { NP_NSH, "NSH" },
85 { NP_MPLS, "MPLS" },
86 { NP_EXP1, "Experiment 1" },
87 { NP_EXP2, "Experiment 2" },
88 { 0, NULL }
89};
90
Elliott Hughese2e3bd12017-05-15 10:59:29 -070091void
92nsh_print(netdissect_options *ndo, const u_char *bp, u_int len)
93{
Elliott Hughes820eced2021-08-20 18:00:50 -070094 uint32_t basehdr;
95 u_int ver, length, md_type;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070096 uint8_t next_protocol;
Elliott Hughes820eced2021-08-20 18:00:50 -070097 u_char past_headers = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070098 u_int next_len;
99
Elliott Hughes820eced2021-08-20 18:00:50 -0700100 ndo->ndo_protocol = "nsh";
101 /*
102 * 0 1 2 3
103 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Base Header |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | Service Path Header |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | |
110 * ~ Context Header(s) ~
111 * | |
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 */
114
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700115 /* print Base Header and Service Path Header */
Elliott Hughes820eced2021-08-20 18:00:50 -0700116 if (len < NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN) {
117 ND_PRINT(" (packet length %u < %u)",
118 len, NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN);
119 goto invalid;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700120 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700121
122 basehdr = GET_BE_U_4(bp);
123 bp += 4;
124 ver = NSH_VER(basehdr);
125 length = NSH_LENGTH(basehdr);
126 md_type = NSH_MD_TYPE(basehdr);
127 next_protocol = NSH_NEXT_PROT(basehdr);
128
129 ND_PRINT("NSH, ");
130 if (ndo->ndo_vflag > 1) {
131 ND_PRINT("ver %u, ", ver);
132 }
133 if (ver != 0)
134 return;
135 ND_PRINT("flags [%s], ",
136 bittok2str_nosep(nsh_flags, "none", NSH_FLAGS(basehdr)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700137 if (ndo->ndo_vflag > 2) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700138 ND_PRINT("TTL %u, ", NSH_TTL(basehdr));
139 ND_PRINT("length %u, ", length);
140 ND_PRINT("md type %s, ", tok2str(md_str, "unknown (0x%02x)", md_type));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700141 }
142 if (ndo->ndo_vflag > 1) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700143 ND_PRINT("next-protocol %s, ",
144 tok2str(np_str, "unknown (0x%02x)", next_protocol));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700145 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700146
147 /* Make sure we have all the headers */
Elliott Hughes820eced2021-08-20 18:00:50 -0700148 if (len < length * NSH_HDR_WORD_SIZE) {
149 ND_PRINT(" (too many headers for packet length %u)", len);
150 goto invalid;
151 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700152
Elliott Hughes820eced2021-08-20 18:00:50 -0700153 /*
154 * 0 1 2 3
155 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
156 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 * | Service Path Identifier (SPI) | Service Index |
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 *
160 */
161 ND_PRINT("service-path-id 0x%06x, ", GET_BE_U_3(bp));
162 bp += 3;
163 ND_PRINT("service-index 0x%x", GET_U_1(bp));
164 bp += 1;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700165
166 /*
167 * length includes the lengths of the Base and Service Path headers.
168 * That means it must be at least 2.
169 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700170 if (length < 2) {
171 ND_PRINT(" (less than two headers)");
172 goto invalid;
173 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700174
175 /*
176 * Print, or skip, the Context Headers.
177 * (length - 2) is the length of those headers.
178 */
179 if (ndo->ndo_vflag > 2) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700180 u_int n;
181
182 if (md_type == MD_TYPE1) {
183 if (length != 6) {
184 ND_PRINT(" (invalid length for the MD type)");
185 goto invalid;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700186 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700187 for (n = 0; n < length - 2; n++) {
188 ND_PRINT("\n Context[%02u]: 0x%08x", n, GET_BE_U_4(bp));
189 bp += NSH_HDR_WORD_SIZE;
190 }
191 past_headers = 1;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700192 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700193 else if (md_type == MD_TYPE2) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700194 n = 0;
195 while (n < length - 2) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700196 uint16_t tlv_class;
197 uint8_t tlv_type, tlv_len, tlv_len_padded;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700198
Elliott Hughes820eced2021-08-20 18:00:50 -0700199 tlv_class = GET_BE_U_2(bp);
200 bp += 2;
201 tlv_type = GET_U_1(bp);
202 bp += 1;
203 tlv_len = GET_U_1(bp) & 0x7f;
204 bp += 1;
205 tlv_len_padded = roundup2(tlv_len, NSH_HDR_WORD_SIZE);
206
207 ND_PRINT("\n TLV Class %u, Type %u, Len %u",
208 tlv_class, tlv_type, tlv_len);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700209
210 n += 1;
211
Elliott Hughes820eced2021-08-20 18:00:50 -0700212 if (length - 2 < n + tlv_len_padded / NSH_HDR_WORD_SIZE) {
213 ND_PRINT(" (length too big)");
214 goto invalid;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700215 }
216
Elliott Hughes820eced2021-08-20 18:00:50 -0700217 if (tlv_len) {
218 const char *sep = "0x";
219 u_int vn;
220
221 ND_PRINT("\n Value: ");
222 for (vn = 0; vn < tlv_len; vn++) {
223 ND_PRINT("%s%02x", sep, GET_U_1(bp));
224 bp += 1;
225 sep = ":";
226 }
227 /* Cover any TLV padding. */
228 ND_TCHECK_LEN(bp, tlv_len_padded - tlv_len);
229 bp += tlv_len_padded - tlv_len;
230 n += tlv_len_padded / NSH_HDR_WORD_SIZE;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700231 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700232 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700233 past_headers = 1;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700234 }
235 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700236 if (! past_headers) {
237 ND_TCHECK_LEN(bp, (length - 2) * NSH_HDR_WORD_SIZE);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700238 bp += (length - 2) * NSH_HDR_WORD_SIZE;
239 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700240 ND_PRINT(ndo->ndo_vflag ? "\n " : ": ");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700241
242 /* print Next Protocol */
243 next_len = len - length * NSH_HDR_WORD_SIZE;
244 switch (next_protocol) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700245 case NP_IPV4:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700246 ip_print(ndo, bp, next_len);
247 break;
Elliott Hughes820eced2021-08-20 18:00:50 -0700248 case NP_IPV6:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700249 ip6_print(ndo, bp, next_len);
250 break;
Elliott Hughes820eced2021-08-20 18:00:50 -0700251 case NP_ETH:
252 ether_print(ndo, bp, next_len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700253 break;
254 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700255 ND_PRINT("ERROR: unknown-next-protocol");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700256 return;
257 }
258
259 return;
260
Elliott Hughes820eced2021-08-20 18:00:50 -0700261invalid:
262 nd_print_invalid(ndo);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700263}
264