]>
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 */
32 #include "netdissect-stdinc.h"
34 #define ND_LONGJMP_FROM_TCHECK
35 #include "netdissect.h"
38 static const struct tok nsh_flags
[] = {
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 #define NSH_BASE_HDR_LEN 4
51 #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)
58 #define NSH_SERVICE_PATH_HDR_LEN 4
59 #define NSH_HDR_WORD_SIZE 4U
65 static const struct tok md_str
[] = {
66 { MD_RSV
, "reserved" },
69 { MD_EXP
, "experimental" },
80 static const struct tok np_str
[] = {
83 { NP_ETH
, "Ethernet" },
86 { NP_EXP1
, "Experiment 1" },
87 { NP_EXP2
, "Experiment 2" },
92 nsh_print(netdissect_options
*ndo
, const u_char
*bp
, u_int len
)
95 u_int ver
, length
, md_type
;
96 uint8_t next_protocol
;
97 u_char past_headers
= 0;
100 ndo
->ndo_protocol
= "nsh";
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | Service Path Header |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 * ~ Context Header(s) ~
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 /* print Base Header and Service Path Header */
116 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
);
122 basehdr
= GET_BE_U_4(bp
);
124 ver
= NSH_VER(basehdr
);
125 length
= NSH_LENGTH(basehdr
);
126 md_type
= NSH_MD_TYPE(basehdr
);
127 next_protocol
= NSH_NEXT_PROT(basehdr
);
130 if (ndo
->ndo_vflag
> 1) {
131 ND_PRINT("ver %u, ", ver
);
135 ND_PRINT("flags [%s], ",
136 bittok2str_nosep(nsh_flags
, "none", NSH_FLAGS(basehdr
)));
137 if (ndo
->ndo_vflag
> 2) {
138 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
));
142 if (ndo
->ndo_vflag
> 1) {
143 ND_PRINT("next-protocol %s, ",
144 tok2str(np_str
, "unknown (0x%02x)", next_protocol
));
147 /* Make sure we have all the headers */
148 if (len
< length
* NSH_HDR_WORD_SIZE
) {
149 ND_PRINT(" (too many headers for packet length %u)", len
);
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 ND_PRINT("service-path-id 0x%06x, ", GET_BE_U_3(bp
));
163 ND_PRINT("service-index 0x%x", GET_U_1(bp
));
167 * length includes the lengths of the Base and Service Path headers.
168 * That means it must be at least 2.
171 ND_PRINT(" (less than two headers)");
176 * Print, or skip, the Context Headers.
177 * (length - 2) is the length of those headers.
179 if (ndo
->ndo_vflag
> 2) {
182 if (md_type
== MD_TYPE1
) {
184 ND_PRINT(" (invalid length for the MD type)");
187 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
;
192 } else if (md_type
== MD_TYPE2
) {
194 while (n
< length
- 2) {
196 uint8_t tlv_type
, tlv_len
, tlv_len_padded
;
198 tlv_class
= GET_BE_U_2(bp
);
200 tlv_type
= GET_U_1(bp
);
202 tlv_len
= GET_U_1(bp
) & 0x7f;
204 tlv_len_padded
= roundup2(tlv_len
, NSH_HDR_WORD_SIZE
);
206 ND_PRINT("\n TLV Class %u, Type %u, Len %u",
207 tlv_class
, tlv_type
, tlv_len
);
211 if (length
- 2 < n
+ tlv_len_padded
/ NSH_HDR_WORD_SIZE
) {
212 ND_PRINT(" (length too big)");
217 const char *sep
= "0x";
220 ND_PRINT("\n Value: ");
221 for (vn
= 0; vn
< tlv_len
; vn
++) {
222 ND_PRINT("%s%02x", sep
, GET_U_1(bp
));
226 /* Cover any TLV padding. */
227 ND_TCHECK_LEN(bp
, tlv_len_padded
- tlv_len
);
228 bp
+= tlv_len_padded
- tlv_len
;
229 n
+= tlv_len_padded
/ NSH_HDR_WORD_SIZE
;
235 if (! past_headers
) {
236 ND_TCHECK_LEN(bp
, (length
- 2) * NSH_HDR_WORD_SIZE
);
237 bp
+= (length
- 2) * NSH_HDR_WORD_SIZE
;
239 ND_PRINT(ndo
->ndo_vflag
? "\n " : ": ");
241 /* print Next Protocol */
242 next_len
= len
- length
* NSH_HDR_WORD_SIZE
;
243 switch (next_protocol
) {
245 ip_print(ndo
, bp
, next_len
);
248 ip6_print(ndo
, bp
, next_len
);
251 ether_print(ndo
, bp
, next_len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
254 ND_PRINT("ERROR: unknown-next-protocol");
261 nd_print_invalid(ndo
);