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: draft-ietf-sfc-nsh-01 */
32 #include <netdissect-stdinc.h>
34 #include "netdissect.h"
37 static const char tstr
[] = " [|NSH]";
38 static const struct tok nsh_flags
[] = {
44 #define NSH_BASE_HDR_LEN 4
45 #define NSH_SERVICE_PATH_HDR_LEN 4
46 #define NSH_HDR_WORD_SIZE 4U
49 nsh_print(netdissect_options
*ndo
, const u_char
*bp
, u_int len
)
56 uint8_t next_protocol
;
57 uint32_t service_path_id
;
58 uint8_t service_index
;
65 /* print Base Header and Service Path Header */
66 if (len
< NSH_BASE_HDR_LEN
+ NSH_SERVICE_PATH_HDR_LEN
)
69 ND_TCHECK2(*bp
, NSH_BASE_HDR_LEN
+ NSH_SERVICE_PATH_HDR_LEN
);
71 ver
= (uint8_t)(*bp
>> 6);
80 service_path_id
= EXTRACT_24BITS(bp
);
85 ND_PRINT((ndo
, "NSH, "));
86 if (ndo
->ndo_vflag
> 1) {
87 ND_PRINT((ndo
, "ver %d, ", ver
));
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
));
94 if (ndo
->ndo_vflag
> 1) {
95 ND_PRINT((ndo
, "next-protocol 0x%x, ", next_protocol
));
97 ND_PRINT((ndo
, "service-path-id 0x%06x, ", service_path_id
));
98 ND_PRINT((ndo
, "service-index 0x%x", service_index
));
100 /* Make sure we have all the headers */
101 if (len
< length
* NSH_HDR_WORD_SIZE
)
104 ND_TCHECK2(*bp
, length
* NSH_HDR_WORD_SIZE
);
107 * length includes the lengths of the Base and Service Path headers.
108 * That means it must be at least 2.
114 * Print, or skip, the Context Headers.
115 * (length - 2) is the length of those headers.
117 if (ndo
->ndo_vflag
> 2) {
118 if (md_type
== 0x01) {
119 for (n
= 0; n
< length
- 2; n
++) {
120 ctx
= EXTRACT_32BITS(bp
);
121 bp
+= NSH_HDR_WORD_SIZE
;
122 ND_PRINT((ndo
, "\n Context[%02d]: 0x%08x", n
, ctx
));
125 else if (md_type
== 0x02) {
127 while (n
< length
- 2) {
128 tlv_class
= EXTRACT_16BITS(bp
);
135 ND_PRINT((ndo
, "\n TLV Class %d, Type %d, Len %d",
136 tlv_class
, tlv_type
, tlv_len
));
140 if (length
- 2 < n
+ tlv_len
) {
141 ND_PRINT((ndo
, " ERROR: invalid-tlv-length"));
145 for (vn
= 0; vn
< tlv_len
; vn
++) {
146 ctx
= EXTRACT_32BITS(bp
);
147 bp
+= NSH_HDR_WORD_SIZE
;
148 ND_PRINT((ndo
, "\n Value[%02d]: 0x%08x", vn
, ctx
));
154 ND_PRINT((ndo
, "ERROR: unknown-next-protocol"));
159 bp
+= (length
- 2) * NSH_HDR_WORD_SIZE
;
161 ND_PRINT((ndo
, ndo
->ndo_vflag
? "\n " : ": "));
163 /* print Next Protocol */
164 next_len
= len
- length
* NSH_HDR_WORD_SIZE
;
165 switch (next_protocol
) {
167 ip_print(ndo
, bp
, next_len
);
170 ip6_print(ndo
, bp
, next_len
);
173 ether_print(ndo
, bp
, next_len
, ndo
->ndo_snapend
- bp
, NULL
, NULL
);
176 ND_PRINT((ndo
, "ERROR: unknown-next-protocol"));
183 ND_PRINT((ndo
, "%s", tstr
));