xref: /netbsd-src/external/bsd/tcpdump/dist/print-geneve.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*
2  * Copyright (c) 2014 VMware, Inc. All Rights Reserved.
3  *
4  * Jesse Gross <jesse@nicira.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that: (1) source code
8  * distributions retain the above copyright notice and this paragraph
9  * in its entirety, and (2) distributions including binary code include
10  * the above copyright notice and this paragraph in its entirety in
11  * the documentation or other materials provided with the distribution.
12  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
13  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
14  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15  * FOR A PARTICULAR PURPOSE.
16  */
17 
18 #include <sys/cdefs.h>
19 #ifndef lint
20 __RCSID("$NetBSD: print-geneve.c,v 1.3 2017/02/05 04:05:05 spz Exp $");
21 #endif
22 
23 /* \summary: Generic Network Virtualization Encapsulation (Geneve) printer */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <netdissect-stdinc.h>
30 
31 #include "netdissect.h"
32 #include "extract.h"
33 #include "ethertype.h"
34 
35 /*
36  * Geneve header, draft-ietf-nvo3-geneve
37  *
38  *    0                   1                   2                   3
39  *    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
40  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *    |Ver|  Opt Len  |O|C|    Rsvd.  |          Protocol Type        |
42  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  *    |        Virtual Network Identifier (VNI)       |    Reserved   |
44  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  *    |                    Variable Length Options                    |
46  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47  *
48  * Options:
49  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50  *    |          Option Class         |      Type     |R|R|R| Length  |
51  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52  *    |                      Variable Option Data                     |
53  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54  */
55 
56 #define VER_SHIFT 6
57 #define HDR_OPTS_LEN_MASK 0x3F
58 
59 #define FLAG_OAM      (1 << 7)
60 #define FLAG_CRITICAL (1 << 6)
61 #define FLAG_R1       (1 << 5)
62 #define FLAG_R2       (1 << 4)
63 #define FLAG_R3       (1 << 3)
64 #define FLAG_R4       (1 << 2)
65 #define FLAG_R5       (1 << 1)
66 #define FLAG_R6       (1 << 0)
67 
68 #define OPT_TYPE_CRITICAL (1 << 7)
69 #define OPT_LEN_MASK 0x1F
70 
71 static const struct tok geneve_flag_values[] = {
72         { FLAG_OAM, "O" },
73         { FLAG_CRITICAL, "C" },
74         { FLAG_R1, "R1" },
75         { FLAG_R2, "R2" },
76         { FLAG_R3, "R3" },
77         { FLAG_R4, "R4" },
78         { FLAG_R5, "R5" },
79         { FLAG_R6, "R6" },
80         { 0, NULL }
81 };
82 
83 static const char *
84 format_opt_class(uint16_t opt_class)
85 {
86     switch (opt_class) {
87     case 0x0100:
88         return "Linux";
89     case 0x0101:
90         return "Open vSwitch";
91     case 0x0102:
92         return "Open Virtual Networking (OVN)";
93     case 0x0103:
94         return "In-band Network Telemetry (INT)";
95     case 0x0104:
96         return "VMware";
97     default:
98         if (opt_class <= 0x00ff)
99             return "Standard";
100         else if (opt_class >= 0xfff0)
101             return "Experimental";
102     }
103 
104     return "Unknown";
105 }
106 
107 static void
108 geneve_opts_print(netdissect_options *ndo, const u_char *bp, u_int len)
109 {
110     const char *sep = "";
111 
112     while (len > 0) {
113         uint16_t opt_class;
114         uint8_t opt_type;
115         uint8_t opt_len;
116 
117         ND_PRINT((ndo, "%s", sep));
118         sep = ", ";
119 
120         opt_class = EXTRACT_16BITS(bp);
121         opt_type = *(bp + 2);
122         opt_len = 4 + ((*(bp + 3) & OPT_LEN_MASK) * 4);
123 
124         ND_PRINT((ndo, "class %s (0x%x) type 0x%x%s len %u",
125                   format_opt_class(opt_class), opt_class, opt_type,
126                   opt_type & OPT_TYPE_CRITICAL ? "(C)" : "", opt_len));
127 
128         if (opt_len > len) {
129             ND_PRINT((ndo, " [bad length]"));
130             return;
131         }
132 
133         if (ndo->ndo_vflag > 1 && opt_len > 4) {
134             const uint32_t *data = (const uint32_t *)(bp + 4);
135             int i;
136 
137             ND_PRINT((ndo, " data"));
138 
139             for (i = 4; i < opt_len; i += 4) {
140                 ND_PRINT((ndo, " %08x", EXTRACT_32BITS(data)));
141                 data++;
142             }
143         }
144 
145         bp += opt_len;
146         len -= opt_len;
147     }
148 }
149 
150 void
151 geneve_print(netdissect_options *ndo, const u_char *bp, u_int len)
152 {
153     uint8_t ver_opt;
154     u_int version;
155     uint8_t flags;
156     uint16_t prot;
157     uint32_t vni;
158     uint8_t reserved;
159     u_int opts_len;
160 
161     ND_PRINT((ndo, "Geneve"));
162 
163     ND_TCHECK2(*bp, 8);
164 
165     ver_opt = *bp;
166     bp += 1;
167     len -= 1;
168 
169     version = ver_opt >> VER_SHIFT;
170     if (version != 0) {
171         ND_PRINT((ndo, " ERROR: unknown-version %u", version));
172         return;
173     }
174 
175     flags = *bp;
176     bp += 1;
177     len -= 1;
178 
179     prot = EXTRACT_16BITS(bp);
180     bp += 2;
181     len -= 2;
182 
183     vni = EXTRACT_24BITS(bp);
184     bp += 3;
185     len -= 3;
186 
187     reserved = *bp;
188     bp += 1;
189     len -= 1;
190 
191     ND_PRINT((ndo, ", Flags [%s]",
192               bittok2str_nosep(geneve_flag_values, "none", flags)));
193     ND_PRINT((ndo, ", vni 0x%x", vni));
194 
195     if (reserved)
196         ND_PRINT((ndo, ", rsvd 0x%x", reserved));
197 
198     if (ndo->ndo_eflag)
199         ND_PRINT((ndo, ", proto %s (0x%04x)",
200                   tok2str(ethertype_values, "unknown", prot), prot));
201 
202     opts_len = (ver_opt & HDR_OPTS_LEN_MASK) * 4;
203 
204     if (len < opts_len) {
205         ND_PRINT((ndo, " truncated-geneve - %u bytes missing",
206                   opts_len - len));
207         return;
208     }
209 
210     ND_TCHECK2(*bp, opts_len);
211 
212     if (opts_len > 0) {
213         ND_PRINT((ndo, ", options ["));
214 
215         if (ndo->ndo_vflag)
216             geneve_opts_print(ndo, bp, opts_len);
217         else
218             ND_PRINT((ndo, "%u bytes", opts_len));
219 
220         ND_PRINT((ndo, "]"));
221     }
222 
223     bp += opts_len;
224     len -= opts_len;
225 
226     if (ndo->ndo_vflag < 1)
227         ND_PRINT((ndo, ": "));
228     else
229         ND_PRINT((ndo, "\n\t"));
230 
231     if (ethertype_print(ndo, prot, bp, len, ndo->ndo_snapend - bp, NULL, NULL) == 0) {
232         if (prot == ETHERTYPE_TEB)
233             ether_print(ndo, bp, len, ndo->ndo_snapend - bp, NULL, NULL);
234         else
235             ND_PRINT((ndo, "geneve-proto-0x%x", prot));
236     }
237 
238     return;
239 
240 trunc:
241     ND_PRINT((ndo, " [|geneve]"));
242 }
243