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