]> The Tcpdump Group git mirrors - tcpdump/blob - print-nsh.c
Add the ndo_protocol field in the netdissect_options structure
[tcpdump] / print-nsh.c
1 /* 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
26 /* specification: draft-ietf-sfc-nsh-01 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "netdissect-stdinc.h"
33
34 #include "netdissect.h"
35 #include "extract.h"
36
37 static const char tstr[] = " [|NSH]";
38 static const struct tok nsh_flags [] = {
39 { 0x20, "O" },
40 { 0x10, "C" },
41 { 0, NULL }
42 };
43
44 #define NSH_BASE_HDR_LEN 4
45 #define NSH_SERVICE_PATH_HDR_LEN 4
46 #define NSH_HDR_WORD_SIZE 4U
47
48 void
49 nsh_print(netdissect_options *ndo, const u_char *bp, u_int len)
50 {
51 u_int n, vn;
52 uint8_t ver;
53 uint8_t flags;
54 u_int length;
55 uint8_t md_type;
56 uint8_t next_protocol;
57 uint32_t service_path_id;
58 uint8_t service_index;
59 uint32_t ctx;
60 uint16_t tlv_class;
61 uint8_t tlv_type;
62 uint8_t tlv_len;
63 u_int next_len;
64
65 ndo->ndo_protocol = "nsh";
66 /* print Base Header and Service Path Header */
67 if (len < NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN)
68 goto trunc;
69
70 ND_TCHECK_LEN(bp, NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN);
71
72 ver = (uint8_t)(EXTRACT_U_1(bp) >> 6);
73 flags = EXTRACT_U_1(bp);
74 bp += 1;
75 length = EXTRACT_U_1(bp);
76 bp += 1;
77 md_type = EXTRACT_U_1(bp);
78 bp += 1;
79 next_protocol = EXTRACT_U_1(bp);
80 bp += 1;
81 service_path_id = EXTRACT_BE_U_3(bp);
82 bp += 3;
83 service_index = EXTRACT_U_1(bp);
84 bp += 1;
85
86 ND_PRINT("NSH, ");
87 if (ndo->ndo_vflag > 1) {
88 ND_PRINT("ver %u, ", ver);
89 }
90 ND_PRINT("flags [%s], ", bittok2str_nosep(nsh_flags, "none", flags));
91 if (ndo->ndo_vflag > 2) {
92 ND_PRINT("length %u, ", length);
93 ND_PRINT("md type 0x%x, ", md_type);
94 }
95 if (ndo->ndo_vflag > 1) {
96 ND_PRINT("next-protocol 0x%x, ", next_protocol);
97 }
98 ND_PRINT("service-path-id 0x%06x, ", service_path_id);
99 ND_PRINT("service-index 0x%x", service_index);
100
101 /* Make sure we have all the headers */
102 if (len < length * NSH_HDR_WORD_SIZE)
103 goto trunc;
104
105 ND_TCHECK_LEN(bp, length * NSH_HDR_WORD_SIZE);
106
107 /*
108 * length includes the lengths of the Base and Service Path headers.
109 * That means it must be at least 2.
110 */
111 if (length < 2)
112 goto trunc;
113
114 /*
115 * Print, or skip, the Context Headers.
116 * (length - 2) is the length of those headers.
117 */
118 if (ndo->ndo_vflag > 2) {
119 if (md_type == 0x01) {
120 for (n = 0; n < length - 2; n++) {
121 ctx = EXTRACT_BE_U_4(bp);
122 bp += NSH_HDR_WORD_SIZE;
123 ND_PRINT("\n Context[%02u]: 0x%08x", n, ctx);
124 }
125 }
126 else if (md_type == 0x02) {
127 n = 0;
128 while (n < length - 2) {
129 tlv_class = EXTRACT_BE_U_2(bp);
130 bp += 2;
131 tlv_type = EXTRACT_U_1(bp);
132 bp += 1;
133 tlv_len = EXTRACT_U_1(bp);
134 bp += 1;
135
136 ND_PRINT("\n TLV Class %u, Type %u, Len %u",
137 tlv_class, tlv_type, tlv_len);
138
139 n += 1;
140
141 if (length - 2 < n + tlv_len) {
142 ND_PRINT(" ERROR: invalid-tlv-length");
143 return;
144 }
145
146 for (vn = 0; vn < tlv_len; vn++) {
147 ctx = EXTRACT_BE_U_4(bp);
148 bp += NSH_HDR_WORD_SIZE;
149 ND_PRINT("\n Value[%02u]: 0x%08x", vn, ctx);
150 }
151 n += tlv_len;
152 }
153 }
154 else {
155 ND_PRINT("ERROR: unknown-next-protocol");
156 return;
157 }
158 }
159 else {
160 bp += (length - 2) * NSH_HDR_WORD_SIZE;
161 }
162 ND_PRINT(ndo->ndo_vflag ? "\n " : ": ");
163
164 /* print Next Protocol */
165 next_len = len - length * NSH_HDR_WORD_SIZE;
166 switch (next_protocol) {
167 case 0x1:
168 ip_print(ndo, bp, next_len);
169 break;
170 case 0x2:
171 ip6_print(ndo, bp, next_len);
172 break;
173 case 0x3:
174 ether_print(ndo, bp, next_len, ndo->ndo_snapend - bp, NULL, NULL);
175 break;
176 default:
177 ND_PRINT("ERROR: unknown-next-protocol");
178 return;
179 }
180
181 return;
182
183 trunc:
184 ND_PRINT("%s", tstr);
185 }
186