xref: /netbsd-src/external/bsd/tcpdump/dist/print-nsh.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
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 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-nsh.c,v 1.5 2024/09/02 16:15:32 christos Exp $");
27 #endif
28 
29 /* \summary: Network Service Header (NSH) printer */
30 
31 /* specification: RFC 8300 */
32 
33 #include <config.h>
34 
35 #include "netdissect-stdinc.h"
36 
37 #define ND_LONGJMP_FROM_TCHECK
38 #include "netdissect.h"
39 #include "extract.h"
40 
41 static const struct tok nsh_flags [] = {
42     { 0x2, "O" },
43     { 0, NULL }
44 };
45 
46 /*
47  *    0                   1                   2                   3
48  *    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
49  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50  *   |Ver|O|U|    TTL    |   Length  |U|U|U|U|MD Type| Next Protocol |
51  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52  */
53 #define NSH_BASE_HDR_LEN 4
54 #define NSH_VER(x)       (((x) & 0xc0000000) >> 30)
55 #define NSH_FLAGS(x)     (((x) & 0x30000000) >> 28)
56 #define NSH_TTL(x)       (((x) & 0x0fc00000) >> 22)
57 #define NSH_LENGTH(x)    (((x) & 0x003f0000) >> 16)
58 #define NSH_MD_TYPE(x)   (((x) & 0x00000f00) >>  8)
59 #define NSH_NEXT_PROT(x) (((x) & 0x000000ff) >>  0)
60 
61 #define NSH_SERVICE_PATH_HDR_LEN 4
62 #define NSH_HDR_WORD_SIZE 4U
63 
64 #define MD_RSV   0x00
65 #define MD_TYPE1 0x01
66 #define MD_TYPE2 0x02
67 #define MD_EXP   0x0F
68 static const struct tok md_str[] = {
69     { MD_RSV,   "reserved"     },
70     { MD_TYPE1, "1"            },
71     { MD_TYPE2, "2"            },
72     { MD_EXP,   "experimental" },
73     { 0, NULL }
74 };
75 
76 #define NP_IPV4 0x01
77 #define NP_IPV6 0x02
78 #define NP_ETH  0x03
79 #define NP_NSH  0x04
80 #define NP_MPLS 0x05
81 #define NP_EXP1 0xFE
82 #define NP_EXP2 0xFF
83 static const struct tok np_str[] = {
84     { NP_IPV4, "IPv4"         },
85     { NP_IPV6, "IPv6"         },
86     { NP_ETH,  "Ethernet"     },
87     { NP_NSH,  "NSH"          },
88     { NP_MPLS, "MPLS"         },
89     { NP_EXP1, "Experiment 1" },
90     { NP_EXP2, "Experiment 2" },
91     { 0, NULL }
92 };
93 
94 void
95 nsh_print(netdissect_options *ndo, const u_char *bp, u_int len)
96 {
97     uint32_t basehdr;
98     u_int ver, length, md_type;
99     uint8_t next_protocol;
100     u_char past_headers = 0;
101     u_int next_len;
102 
103     ndo->ndo_protocol = "nsh";
104     /*
105      *    0                   1                   2                   3
106      *    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
107      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108      *   |                Base Header                                    |
109      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110      *   |                Service Path Header                            |
111      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112      *   |                                                               |
113      *   ~                Context Header(s)                              ~
114      *   |                                                               |
115      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116      */
117 
118     /* print Base Header and Service Path Header */
119     if (len < NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN) {
120         ND_PRINT(" (packet length %u < %u)",
121                  len, NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN);
122         goto invalid;
123     }
124 
125     basehdr = GET_BE_U_4(bp);
126     bp += 4;
127     ver = NSH_VER(basehdr);
128     length = NSH_LENGTH(basehdr);
129     md_type = NSH_MD_TYPE(basehdr);
130     next_protocol = NSH_NEXT_PROT(basehdr);
131 
132     ND_PRINT("NSH, ");
133     if (ndo->ndo_vflag > 1) {
134         ND_PRINT("ver %u, ", ver);
135     }
136     if (ver != 0)
137         return;
138     ND_PRINT("flags [%s], ",
139              bittok2str_nosep(nsh_flags, "none", NSH_FLAGS(basehdr)));
140     if (ndo->ndo_vflag > 2) {
141         ND_PRINT("TTL %u, ", NSH_TTL(basehdr));
142         ND_PRINT("length %u, ", length);
143         ND_PRINT("md type %s, ", tok2str(md_str, "unknown (0x%02x)", md_type));
144     }
145     if (ndo->ndo_vflag > 1) {
146         ND_PRINT("next-protocol %s, ",
147                  tok2str(np_str, "unknown (0x%02x)", next_protocol));
148     }
149 
150     /* Make sure we have all the headers */
151     if (len < length * NSH_HDR_WORD_SIZE) {
152         ND_PRINT(" (too many headers for packet length %u)", len);
153         goto invalid;
154     }
155 
156     /*
157      *    0                   1                   2                   3
158      *    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
159      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160      *   |          Service Path Identifier (SPI)        | Service Index |
161      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162      *
163      */
164     ND_PRINT("service-path-id 0x%06x, ", GET_BE_U_3(bp));
165     bp += 3;
166     ND_PRINT("service-index 0x%x", GET_U_1(bp));
167     bp += 1;
168 
169     /*
170      * length includes the lengths of the Base and Service Path headers.
171      * That means it must be at least 2.
172      */
173     if (length < 2) {
174         ND_PRINT(" (less than two headers)");
175         goto invalid;
176     }
177 
178     /*
179      * Print, or skip, the Context Headers.
180      * (length - 2) is the length of those headers.
181      */
182     if (ndo->ndo_vflag > 2) {
183         u_int n;
184 
185         if (md_type == MD_TYPE1) {
186             if (length != 6) {
187                 ND_PRINT(" (length for the MD type)");
188                 goto invalid;
189             }
190             for (n = 0; n < length - 2; n++) {
191                 ND_PRINT("\n        Context[%02u]: 0x%08x", n, GET_BE_U_4(bp));
192                 bp += NSH_HDR_WORD_SIZE;
193             }
194             past_headers = 1;
195         } else if (md_type == MD_TYPE2) {
196             n = 0;
197             while (n < length - 2) {
198                 uint16_t tlv_class;
199                 uint8_t tlv_type, tlv_len, tlv_len_padded;
200 
201                 tlv_class = GET_BE_U_2(bp);
202                 bp += 2;
203                 tlv_type  = GET_U_1(bp);
204                 bp += 1;
205                 tlv_len   = GET_U_1(bp) & 0x7f;
206                 bp += 1;
207                 tlv_len_padded = roundup2(tlv_len, NSH_HDR_WORD_SIZE);
208 
209                 ND_PRINT("\n        TLV Class %u, Type %u, Len %u",
210                           tlv_class, tlv_type, tlv_len);
211 
212                 n += 1;
213 
214                 if (length - 2 < n + tlv_len_padded / NSH_HDR_WORD_SIZE) {
215                     ND_PRINT(" (length too big)");
216                     goto invalid;
217                 }
218 
219                 if (tlv_len) {
220                     const char *sep = "0x";
221                     u_int vn;
222 
223                     ND_PRINT("\n            Value: ");
224                     for (vn = 0; vn < tlv_len; vn++) {
225                         ND_PRINT("%s%02x", sep, GET_U_1(bp));
226                         bp += 1;
227                         sep = ":";
228                     }
229                     /* Cover any TLV padding. */
230                     ND_TCHECK_LEN(bp, tlv_len_padded - tlv_len);
231                     bp += tlv_len_padded - tlv_len;
232                     n += tlv_len_padded / NSH_HDR_WORD_SIZE;
233                 }
234             }
235             past_headers = 1;
236         }
237     }
238     if (! past_headers) {
239         ND_TCHECK_LEN(bp, (length - 2) * NSH_HDR_WORD_SIZE);
240         bp += (length - 2) * NSH_HDR_WORD_SIZE;
241     }
242     ND_PRINT(ndo->ndo_vflag ? "\n    " : ": ");
243 
244     /* print Next Protocol */
245     next_len = len - length * NSH_HDR_WORD_SIZE;
246     switch (next_protocol) {
247     case NP_IPV4:
248         ip_print(ndo, bp, next_len);
249         break;
250     case NP_IPV6:
251         ip6_print(ndo, bp, next_len);
252         break;
253     case NP_ETH:
254         ether_print(ndo, bp, next_len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
255         break;
256     default:
257         ND_PRINT("ERROR: unknown-next-protocol");
258         return;
259     }
260 
261     return;
262 
263 invalid:
264     nd_print_invalid(ndo);
265 }
266 
267