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