]> The Tcpdump Group git mirrors - tcpdump/blob - print-vxlan-gpe.c
GRE, VXLAN: Fix output printing with s/printf/ND_PRINT/
[tcpdump] / print-vxlan-gpe.c
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 /* \summary: Generic Protocol Extension for VXLAN (VXLAN GPE) printer */
25
26 /* specification: draft-ietf-nvo3-vxlan-gpe-12 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "netdissect-stdinc.h"
33
34 #define ND_LONGJMP_FROM_TCHECK
35 #include "netdissect.h"
36 #include "extract.h"
37
38 #define VXLAN_GPE_VER 0x30 /* GPE */
39 #define VXLAN_GPE_SHIFT 4
40 #define VXLAN_GPE_VER_0 0x0
41 #define VXLAN_GPE_I 0x08 /* Instance Bit */
42 #define VXLAN_GPE_P 0x04 /* GPE Next Protocol */
43 #define VXLAN_GPE_B 0x02 /* GPE BUM Traffic */
44 #define VXLAN_GPE_O 0x01 /* GPE OAM Flag */
45
46 static const struct tok vxlan_gpe_flags [] = {
47 { VXLAN_GPE_I, "I" },
48 { VXLAN_GPE_P, "P" },
49 { VXLAN_GPE_B, "B" },
50 { VXLAN_GPE_O, "O" },
51 { 0, NULL }
52 };
53
54 #define VXLAN_GPE_PROTO_RESERVED 0x00
55 #define VXLAN_GPE_PROTO_IPV4 0x01
56 #define VXLAN_GPE_PROTO_IPV6 0x02
57 #define VXLAN_GPE_PROTO_ETHERNET 0x03
58 #define VXLAN_GPE_PROTO_NSH 0x04
59
60 #define VXLAN_GPE_HDR_LEN 8
61
62 /*
63 * VXLAN GPE header, draft-ietf-nvo3-vxlan-gpe-12
64 * Generic Protocol Extension for VXLAN
65 *
66 * 0 1 2 3
67 * 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
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * |R|R|Ver|I|P|B|O| Reserved |Next Protocol |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * | VXLAN Network Identifier (VNI) | Reserved |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 */
74
75 void
76 vxlan_gpe_print(netdissect_options *ndo, const u_char *bp, u_int len)
77 {
78 uint8_t flags, ver;
79 uint8_t next_protocol;
80
81 /*
82 * XXX - OpenBSD has a single dissector for VXLAN and VXLAN-GPE,
83 * using the flag bits to distinguish between them.
84 *
85 * draft-ietf-nvo3-vxlan-gpe-12, the final VXLAN-GPE draft, says
86 * that VXLAN-GPE uses port 4790, rather than VXLAN's port 4789,
87 * and that the P flag bit must be set for VXLAN-GPE packets,
88 * indicating that the header includes a "next protocol" field,
89 * and that if a packet with the P it not set is received on
90 * port 4790, "the "Next Protocol" field must be set to zero and
91 * the payload MUST be ETHERNET(L2) as defined by [RFC7348]."
92 */
93 ndo->ndo_protocol = "vxlan_gpe";
94 ND_PRINT("VXLAN-GPE, ");
95 if (len < VXLAN_GPE_HDR_LEN) {
96 ND_PRINT(" (len %u < %u)", len, VXLAN_GPE_HDR_LEN);
97 goto invalid;
98 }
99
100 flags = GET_U_1(bp);
101 bp += 1;
102 len -= 1;
103 ver = (flags & VXLAN_GPE_VER) >> VXLAN_GPE_SHIFT;
104 if (ver != VXLAN_GPE_VER_0) {
105 ND_PRINT("unknown version %u", ver);
106 goto invalid;
107 }
108 ND_PRINT("flags [%s], ",
109 bittok2str_nosep(vxlan_gpe_flags, "none", flags));
110
111 /* Reserved */
112 bp += 2;
113 len -= 2;
114
115 /*
116 * If the VXLAN_GPE_P flag bit isn't set, that means this is a VXLAN
117 * packet, not a VXLAN-GPE packet, and thus has no "next protocol"
118 * field; the payload is Ethernet.
119 */
120 if (flags & VXLAN_GPE_P)
121 next_protocol = GET_U_1(bp);
122 else
123 next_protocol = VXLAN_GPE_PROTO_ETHERNET;
124 bp += 1;
125 len -= 1;
126
127 /*
128 * Both RFC 7348 and draft-ietf-nvo3-vxlan-gpe-12 say that the I flag
129 * MUST be set.
130 */
131 if (flags & VXLAN_GPE_I)
132 ND_PRINT("vni %u", GET_BE_U_3(bp));
133 else
134 ND_PRINT("ERROR: I flag not set");
135 bp += 3;
136 len -= 3;
137
138 if (flags & VXLAN_GPE_B)
139 ND_PRINT(", BUM");
140
141 if (flags & VXLAN_GPE_O) {
142 ND_PRINT(", OAM (proto 0x%x, len %u)", next_protocol, len);
143 return;
144 }
145
146 /* Reserved */
147 ND_TCHECK_1(bp);
148 bp += 1;
149 len -= 1;
150
151 ND_PRINT(ndo->ndo_vflag ? "\n " : ": ");
152
153 switch (next_protocol) {
154 case VXLAN_GPE_PROTO_IPV4:
155 ip_print(ndo, bp, len);
156 break;
157 case VXLAN_GPE_PROTO_IPV6:
158 ip6_print(ndo, bp, len);
159 break;
160 case VXLAN_GPE_PROTO_ETHERNET:
161 ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
162 break;
163 case VXLAN_GPE_PROTO_NSH:
164 nsh_print(ndo, bp, len);
165 break;
166 /*
167 * OpenBSD supports 0x05 for MPLS, which was in earlier drafts
168 * of VXLAN GPE, but not in the final -12 draft.
169 */
170 default:
171 ND_PRINT("ERROR: unknown-next-protocol");
172 goto invalid;
173 }
174
175 return;
176
177 invalid:
178 nd_print_invalid(ndo);
179 }
180