]>
The Tcpdump Group git mirrors - tcpdump/blob - print-nsh.c
1 /* Copyright (c) 2015, bugyo
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.
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.
24 /* \summary: Network Service Header (NSH) printer */
26 /* specification: RFC 8300 */
30 #include "netdissect-stdinc.h"
32 #define ND_LONGJMP_FROM_TCHECK
33 #include "netdissect.h"
36 static const struct tok nsh_flags
[] = {
43 * 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
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * |Ver|O|U| TTL | Length |U|U|U|U|MD Type| Next Protocol |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 #define NSH_BASE_HDR_LEN 4
49 #define NSH_VER(x) (((x) & 0xc0000000) >> 30)
50 #define NSH_FLAGS(x) (((x) & 0x30000000) >> 28)
51 #define NSH_TTL(x) (((x) & 0x0fc00000) >> 22)
52 #define NSH_LENGTH(x) (((x) & 0x003f0000) >> 16)
53 #define NSH_MD_TYPE(x) (((x) & 0x00000f00) >> 8)
54 #define NSH_NEXT_PROT(x) (((x) & 0x000000ff) >> 0)
56 #define NSH_SERVICE_PATH_HDR_LEN 4
57 #define NSH_HDR_WORD_SIZE 4U
63 static const struct tok md_str
[] = {
64 { MD_RSV
, "reserved" },
67 { MD_EXP
, "experimental" },
78 static const struct tok np_str
[] = {
81 { NP_ETH
, "Ethernet" },
84 { NP_EXP1
, "Experiment 1" },
85 { NP_EXP2
, "Experiment 2" },
90 nsh_print(netdissect_options
*ndo
, const u_char
*bp
, u_int len
)
93 u_int ver
, length
, md_type
;
94 uint8_t next_protocol
;
95 u_char past_headers
= 0;
98 ndo
->ndo_protocol
= "nsh";
101 * 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
102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Service Path Header |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * ~ Context Header(s) ~
110 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 /* print Base Header and Service Path Header */
114 if (len
< NSH_BASE_HDR_LEN
+ NSH_SERVICE_PATH_HDR_LEN
) {
115 ND_PRINT(" (packet length %u < %u)",
116 len
, NSH_BASE_HDR_LEN
+ NSH_SERVICE_PATH_HDR_LEN
);
120 basehdr
= GET_BE_U_4(bp
);
122 ver
= NSH_VER(basehdr
);
123 length
= NSH_LENGTH(basehdr
);
124 md_type
= NSH_MD_TYPE(basehdr
);
125 next_protocol
= NSH_NEXT_PROT(basehdr
);
128 if (ndo
->ndo_vflag
> 1) {
129 ND_PRINT("ver %u, ", ver
);
133 ND_PRINT("flags [%s], ",
134 bittok2str_nosep(nsh_flags
, "none", NSH_FLAGS(basehdr
)));
135 if (ndo
->ndo_vflag
> 2) {
136 ND_PRINT("TTL %u, ", NSH_TTL(basehdr
));
137 ND_PRINT("length %u, ", length
);
138 ND_PRINT("md type %s, ", tok2str(md_str
, "unknown (0x%02x)", md_type
));
140 if (ndo
->ndo_vflag
> 1) {
141 ND_PRINT("next-protocol %s, ",
142 tok2str(np_str
, "unknown (0x%02x)", next_protocol
));
145 /* Make sure we have all the headers */
146 if (len
< length
* NSH_HDR_WORD_SIZE
) {
147 ND_PRINT(" (too many headers for packet length %u)", len
);
153 * 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
154 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155 * | Service Path Identifier (SPI) | Service Index |
156 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 ND_PRINT("service-path-id 0x%06x, ", GET_BE_U_3(bp
));
161 ND_PRINT("service-index 0x%x", GET_U_1(bp
));
165 * length includes the lengths of the Base and Service Path headers.
166 * That means it must be at least 2.
169 ND_PRINT(" (less than two headers)");
174 * Print, or skip, the Context Headers.
175 * (length - 2) is the length of those headers.
177 if (ndo
->ndo_vflag
> 2) {
180 if (md_type
== MD_TYPE1
) {
182 ND_PRINT(" (length for the MD type)");
185 for (n
= 0; n
< length
- 2; n
++) {
186 ND_PRINT("\n Context[%02u]: 0x%08x", n
, GET_BE_U_4(bp
));
187 bp
+= NSH_HDR_WORD_SIZE
;
190 } else if (md_type
== MD_TYPE2
) {
192 while (n
< length
- 2) {
194 uint8_t tlv_type
, tlv_len
, tlv_len_padded
;
196 tlv_class
= GET_BE_U_2(bp
);
198 tlv_type
= GET_U_1(bp
);
200 tlv_len
= GET_U_1(bp
) & 0x7f;
202 tlv_len_padded
= roundup2(tlv_len
, NSH_HDR_WORD_SIZE
);
204 ND_PRINT("\n TLV Class %u, Type %u, Len %u",
205 tlv_class
, tlv_type
, tlv_len
);
209 if (length
- 2 < n
+ tlv_len_padded
/ NSH_HDR_WORD_SIZE
) {
210 ND_PRINT(" (length too big)");
215 const char *sep
= "0x";
218 ND_PRINT("\n Value: ");
219 for (vn
= 0; vn
< tlv_len
; vn
++) {
220 ND_PRINT("%s%02x", sep
, GET_U_1(bp
));
224 /* Cover any TLV padding. */
225 ND_TCHECK_LEN(bp
, tlv_len_padded
- tlv_len
);
226 bp
+= tlv_len_padded
- tlv_len
;
227 n
+= tlv_len_padded
/ NSH_HDR_WORD_SIZE
;
233 if (! past_headers
) {
234 ND_TCHECK_LEN(bp
, (length
- 2) * NSH_HDR_WORD_SIZE
);
235 bp
+= (length
- 2) * NSH_HDR_WORD_SIZE
;
237 ND_PRINT(ndo
->ndo_vflag
? "\n " : ": ");
239 /* print Next Protocol */
240 next_len
= len
- length
* NSH_HDR_WORD_SIZE
;
241 switch (next_protocol
) {
243 ip_print(ndo
, bp
, next_len
);
246 ip6_print(ndo
, bp
, next_len
);
249 ether_print(ndo
, bp
, next_len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
252 ND_PRINT("ERROR: unknown-next-protocol");
259 nd_print_invalid(ndo
);