2 * Routines for Network Service Header
5 * Author: Vanson Lim <vlim@cisco.com>
6 * (c) Copyright 2020, Cisco Systems Inc.
8 * draft-ietf-sfc-nsh-01
9 * Author: Chidambaram Arunachalam <carunach@cisco.com>
10 * Copyright 2016, ciscoSystems Inc.
12 * (c) Copyright 2016, Sumit Kumar Jha <sjha3@ncsu.edu>
13 * Support for VXLAN GPE encapsulation
15 * Wireshark - Network traffic analyzer
16 * By Gerald Combs <gerald@wireshark.org>
17 * Copyright 1998 Gerald Combs
19 * SPDX-License-Identifier: GPL-2.0-or-later
23 #include <epan/packet.h>
24 #include <epan/etypes.h>
25 #include <epan/expert.h>
26 #include <epan/ipproto.h>
27 #include "packet-nsh.h"
28 #include "packet-vxlan.h"
33 #define MD_MAX_VERSION 0
36 void proto_reg_handoff_nsh(void);
37 void proto_register_nsh(void);
39 static dissector_handle_t nsh_handle
;
41 static const value_string nsh_next_protocols
[] = {
45 { NSH_ETHERNET
, "Ethernet" },
48 { NSH_EXPERIMENT_1
, "Experiment 1" },
49 { NSH_EXPERIMENT_2
, "Experiment 2" },
55 static int hf_nsh_version
;
56 static int hf_nsh_oam
;
57 static int hf_nsh_critical_metadata
;
58 static int hf_nsh_ttl
;
59 static int hf_nsh_length
;
60 static int hf_nsh_md_type
;
61 static int hf_nsh_next_proto
;
62 static int hf_nsh_service_pathID
;
63 static int hf_nsh_service_index
;
64 static int hf_nsh_context_header
;
65 static int hf_nsh_metadata_class
;
66 static int hf_nsh_metadata_type
;
67 static int hf_nsh_metadata_unassignedbit
;
68 static int hf_nsh_metadata_length
;
69 static int hf_nsh_metadata
;
71 static expert_field ei_nsh_length_invalid
;
75 static dissector_table_t subdissector_table
;
78 *Dissect Fixed Length Context headers
82 dissect_nsh_md_type_1(tvbuff_t
*tvb
, proto_tree
*nsh_tree
, int offset
)
84 proto_tree_add_item(nsh_tree
, hf_nsh_context_header
, tvb
, offset
, 4, ENC_NA
);
85 proto_tree_add_item(nsh_tree
, hf_nsh_context_header
, tvb
, offset
+ 4, 4, ENC_NA
);
86 proto_tree_add_item(nsh_tree
, hf_nsh_context_header
, tvb
, offset
+ 8, 4, ENC_NA
);
87 proto_tree_add_item(nsh_tree
, hf_nsh_context_header
, tvb
, offset
+ 12, 4, ENC_NA
);
91 *Dissect Variable Length Context headers
96 dissect_nsh_md_type_2(tvbuff_t
*tvb
, proto_tree
*nsh_tree
, int offset
, int nsh_bytes_len
)
99 uint32_t type2_metadata_len
= 0;
102 while (offset
< nsh_bytes_len
) {
104 proto_tree_add_item(nsh_tree
, hf_nsh_metadata_class
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
105 proto_tree_add_item(nsh_tree
, hf_nsh_metadata_type
, tvb
, offset
+ 2, 1, ENC_BIG_ENDIAN
);
107 /* Bit 24 is unassigned */
108 proto_tree_add_item(nsh_tree
, hf_nsh_metadata_unassignedbit
, tvb
, offset
+ 3, 1, ENC_BIG_ENDIAN
);
110 /* Bits 25-31 represent variable length metadata byte count */
111 proto_tree_add_item_ret_uint(nsh_tree
, hf_nsh_metadata_length
, tvb
, offset
+ 3, 1, ENC_BIG_ENDIAN
, &type2_metadata_len
);
113 if (type2_metadata_len
> 0)
114 proto_tree_add_item(nsh_tree
, hf_nsh_metadata
, tvb
, offset
+ 4, type2_metadata_len
, ENC_NA
);
116 pad_len
= (type2_metadata_len
% 4) ? (4 - (type2_metadata_len
% 4)) : 0;
117 offset
= offset
+ 4 + type2_metadata_len
+ pad_len
;
126 *Dissect Network Service Header
131 dissect_nsh(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
136 uint32_t nsh_bytes_len
;
137 int nsh_next_proto
= -1;
138 proto_item
*length_pi
;
141 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "NSH");
142 col_set_str(pinfo
->cinfo
, COL_INFO
, "Network Service Header");
145 proto_tree
*nsh_tree
;
147 ti
= proto_tree_add_item(tree
, proto_nsh
, tvb
, offset
, 2, ENC_NA
);
148 nsh_tree
= proto_item_add_subtree(ti
, ett_nsh
);
152 proto_tree_add_item(nsh_tree
, hf_nsh_version
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
153 proto_tree_add_item(nsh_tree
, hf_nsh_oam
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
154 proto_tree_add_item(nsh_tree
, hf_nsh_critical_metadata
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
157 /*NSH Time to live Bits 4 - 9*/
158 proto_tree_add_item(nsh_tree
, hf_nsh_ttl
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
159 length_pi
= proto_tree_add_item_ret_uint(nsh_tree
, hf_nsh_length
, tvb
, offset
, 2, ENC_BIG_ENDIAN
, &nsh_bytes_len
);
161 proto_item_set_len(ti
, nsh_bytes_len
);
164 md_type
= tvb_get_uint8(tvb
, offset
+ 2);
165 proto_tree_add_item(nsh_tree
, hf_nsh_md_type
, tvb
, offset
+ 2, 1, ENC_BIG_ENDIAN
);
167 nsh_next_proto
= tvb_get_uint8(tvb
, offset
+ 3);
168 proto_tree_add_item(nsh_tree
, hf_nsh_next_proto
, tvb
, offset
+ 3, 1, ENC_BIG_ENDIAN
);
170 /*NSH Service Path Header */
172 proto_tree_add_item(nsh_tree
, hf_nsh_service_pathID
, tvb
, offset
, 3, ENC_BIG_ENDIAN
);
173 proto_tree_add_item(nsh_tree
, hf_nsh_service_index
, tvb
, offset
+ 3, 1, ENC_BIG_ENDIAN
);
175 /* Decode Context Headers */
180 /* The Length MUST be of value 0x6 for MD Type equal to 0x1 */
181 if (nsh_bytes_len
!= 4 * 6) {
182 expert_add_info_format(pinfo
, length_pi
, &ei_nsh_length_invalid
,
183 "Length MUST be of value 0x6 for MD Type equal to 0x1");
184 nsh_bytes_len
= 4 * 6;
186 dissect_nsh_md_type_1(tvb
, nsh_tree
, offset
);
191 /* The Length MUST be of value 0x2 or greater for MD Type equal to 0x2 */
192 if (nsh_bytes_len
< 4 * 2) {
193 expert_add_info_format(pinfo
, length_pi
, &ei_nsh_length_invalid
,
194 "Length MUST be of value 0x2 or greater for MD Type equal to 0x2");
195 nsh_bytes_len
= 4 * 2;
197 /* MD Type 2 indicates ZERO or more Variable Length Context headers*/
198 if (nsh_bytes_len
> 8)
199 dissect_nsh_md_type_2(tvb
, nsh_tree
, offset
, nsh_bytes_len
);
204 * Unknown type, but assume presence of at least the NSH
205 * Base Header (32 bits, 4 bytes).
207 if (nsh_bytes_len
< 4) {
208 expert_add_info_format(pinfo
, length_pi
, &ei_nsh_length_invalid
,
209 "Length must be at least 0x1 for NSH Base Header");
216 /*Decode next protocol payload */
218 if (tvb_captured_length_remaining(tvb
, nsh_bytes_len
) > 0) {
219 next_tvb
= tvb_new_subset_remaining(tvb
, nsh_bytes_len
);
220 if (!dissector_try_uint(subdissector_table
, nsh_next_proto
, next_tvb
, pinfo
, tree
)) {
221 call_data_dissector(next_tvb
, pinfo
, tree
);
225 return tvb_captured_length(tvb
);
230 dissect_nsh_heur(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
)
232 const int tvb_length
= tvb_captured_length(tvb
);
233 if (tvb_length
< 8) return false;
235 const uint8_t version
= tvb_get_uint8(tvb
, 0) >> 6;
236 const uint8_t length
= tvb_get_uint8(tvb
, 1) & 0x3F;
237 const uint8_t md_type
= tvb_get_uint8(tvb
, 2) & 0x0F;
238 const uint8_t proto
= tvb_get_uint8(tvb
, 3);
240 if (version
> MD_MAX_VERSION
) return false;
241 if (md_type
!= 1 && md_type
!= 2) return false;
242 if (md_type
== 1 && length
!= 6) return false;
243 if (md_type
== 2 && length
< 2) return false;
244 if (length
* 4 > tvb_length
) return false;
245 if (proto
== 0) return false;
246 if (proto
> NSH_MAX_PROTOCOL
) return false;
248 // Note: md_type = 0x0 and md_type = 0xf are strictly speaking also valid.
249 // For the heuristic to work as good as possible, it is best to restrict
250 // as much as possible and only allow md_type 1 and 2.
252 dissect_nsh(tvb
, pinfo
, tree
, data
);
257 proto_register_nsh(void)
259 expert_module_t
*expert_nsh
;
261 static hf_register_info nsh_info
[] = {
263 /* Network Service Header fields */
265 { "Version", "nsh.version",
266 FT_UINT16
, BASE_DEC_HEX
, NULL
, 0xC000,
271 { "O Bit", "nsh.Obit",
272 FT_UINT16
, BASE_DEC
, NULL
, 0x2000,
277 { &hf_nsh_critical_metadata
,
278 { "C Bit", "nsh.CBit",
279 FT_UINT16
, BASE_DEC
, NULL
, 0x1000,
280 "Critical Metadata Bit", HFILL
}
285 { "Time to live", "nsh.ttl",
286 FT_UINT16
, BASE_HEX
, NULL
, 0x0FC0,
287 "Maximum SFF hops for an SFP, this field is used for service-plane loop detection", HFILL
}
292 { "Length", "nsh.length",
293 FT_UINT16
, BASE_DEC_HEX
, NULL
, 0x003F,
294 "Total length, in 4-byte words, of NSH including Base, Service Path headers and optional variable TLVs", HFILL
}
299 { "MD Type", "nsh.mdtype",
300 FT_UINT8
, BASE_DEC_HEX
, NULL
, 0x00,
301 "Metadata Type defines the format of the metadata being carried", HFILL
}
305 { &hf_nsh_next_proto
,
306 { "Next Protocol", "nsh.nextproto",
307 FT_UINT8
, BASE_DEC_HEX
, VALS(nsh_next_protocols
), 0x00,
308 "Protocol type of the original packet", HFILL
}
312 { &hf_nsh_service_pathID
,
314 FT_UINT24
, BASE_DEC_HEX
, NULL
, 0x00,
315 "Service Path Identifier", HFILL
}
319 { &hf_nsh_service_index
,
321 FT_UINT8
, BASE_DEC_HEX
, NULL
, 0x00,
322 "Service Index", HFILL
}
327 { &hf_nsh_context_header
,
328 { "Context Header", "nsh.contextheader",
329 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
330 "Mandatory Context Header", HFILL
}
334 { &hf_nsh_metadata_class
,
335 { "TLV Class", "nsh.metadataclass",
336 FT_UINT16
, BASE_DEC_HEX
, NULL
, 0x00,
337 "TLV class describes the scope of the metadata type field", HFILL
}
341 { &hf_nsh_metadata_type
,
342 { "Type", "nsh.metadatatype",
343 FT_UINT8
, BASE_DEC_HEX
, NULL
, 0x00,
344 "Type of metadata", HFILL
}
348 { &hf_nsh_metadata_unassignedbit
,
349 { "Unassigned Bit", "nsh.metadataunassignedbit",
350 FT_UINT8
, BASE_HEX
, NULL
, 0x80,
351 "Unassigned Bit within Variable Length Metadata header", HFILL
}
355 { &hf_nsh_metadata_length
,
356 { "Length", "nsh.metadatalen",
357 FT_UINT8
, BASE_HEX
, NULL
, 0x7F,
358 "Length of the variable metadata in bytes", HFILL
}
363 { "Variable Metadata", "nsh.metadata",
364 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
365 "Variable length metadata", HFILL
}
371 static int *ett
[] = {
375 static ei_register_info ei
[] = {
376 { &ei_nsh_length_invalid
, { "nsh.length.invalid", PI_PROTOCOL
, PI_WARN
, "Invalid total length", EXPFILL
}},
379 proto_nsh
= proto_register_protocol("Network Service Header", "NSH", "nsh");
380 proto_register_field_array(proto_nsh
, nsh_info
, array_length(nsh_info
));
381 proto_register_subtree_array(ett
, array_length(ett
));
383 expert_nsh
= expert_register_protocol(proto_nsh
);
384 expert_register_field_array(expert_nsh
, ei
, array_length(ei
));
386 subdissector_table
= register_dissector_table("nsh.next_proto", "NSH Next Protocol", proto_nsh
, FT_UINT32
, BASE_DEC
);
388 nsh_handle
= register_dissector("nsh", dissect_nsh
, proto_nsh
);
392 proto_reg_handoff_nsh(void)
394 dissector_add_uint("ethertype", ETHERTYPE_NSH
, nsh_handle
);
395 dissector_add_uint("gre.proto", ETHERTYPE_NSH
, nsh_handle
);
396 dissector_add_uint("vxlan.next_proto", VXLAN_NSH
, nsh_handle
);
397 dissector_add_uint("nsh.next_proto", NSH_NSH
, nsh_handle
);
398 dissector_add_uint("ip.proto", IP_PROTO_NSH
, nsh_handle
);
400 heur_dissector_add("gtp.tpdu", dissect_nsh_heur
, "NSH over GTP", "nsh_gtp.tpdu", proto_nsh
, HEURISTIC_ENABLE
);
404 * Editor modelines - https://www.wireshark.org/tools/modelines.html
409 * indent-tabs-mode: nil
412 * vi: set shiftwidth=4 tabstop=8 expandtab:
413 * :indentSize=4:tabSize=8:noTabs=true: