xref: /netbsd-src/external/bsd/tcpdump/dist/print-vxlan-gpe.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
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-vxlan-gpe.c,v 1.3 2017/02/05 04:05:05 spz Exp $");
27 #endif
28 
29 /* \summary: Generic Protocol Extension for VXLAN (VXLAN GPE) printer */
30 
31 /* specification: draft-ietf-nvo3-vxlan-gpe-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[] = " [|VXLAN-GPE]";
43 static const struct tok vxlan_gpe_flags [] = {
44     { 0x08, "I" },
45     { 0x04, "P" },
46     { 0x01, "O" },
47     { 0, NULL }
48 };
49 
50 #define VXLAN_GPE_HDR_LEN 8
51 
52 /*
53  * VXLAN GPE header, draft-ietf-nvo3-vxlan-gpe-01
54  *                   Generic Protocol Extension for VXLAN
55  *
56  *     0                   1                   2                   3
57  *     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
58  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59  *    |R|R|Ver|I|P|R|O|       Reserved                |Next Protocol  |
60  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61  *    |                VXLAN Network Identifier (VNI) |   Reserved    |
62  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  */
64 
65 void
66 vxlan_gpe_print(netdissect_options *ndo, const u_char *bp, u_int len)
67 {
68     uint8_t flags;
69     uint8_t next_protocol;
70     uint32_t vni;
71 
72     if (len < VXLAN_GPE_HDR_LEN)
73         goto trunc;
74 
75     ND_TCHECK2(*bp, VXLAN_GPE_HDR_LEN);
76 
77     flags = *bp;
78     bp += 3;
79 
80     next_protocol = *bp;
81     bp += 1;
82 
83     vni = EXTRACT_24BITS(bp);
84     bp += 4;
85 
86     ND_PRINT((ndo, "VXLAN-GPE, "));
87     ND_PRINT((ndo, "flags [%s], ",
88               bittok2str_nosep(vxlan_gpe_flags, "none", flags)));
89     ND_PRINT((ndo, "vni %u", vni));
90     ND_PRINT((ndo, ndo->ndo_vflag ? "\n    " : ": "));
91 
92     switch (next_protocol) {
93     case 0x1:
94         ip_print(ndo, bp, len - 8);
95         break;
96     case 0x2:
97         ip6_print(ndo, bp, len - 8);
98         break;
99     case 0x3:
100         ether_print(ndo, bp, len - 8, ndo->ndo_snapend - bp, NULL, NULL);
101         break;
102     case 0x4:
103         nsh_print(ndo, bp, len - 8);
104         break;
105     case 0x5:
106         mpls_print(ndo, bp, len - 8);
107         break;
108     default:
109         ND_PRINT((ndo, "ERROR: unknown-next-protocol"));
110         return;
111     }
112 
113 	return;
114 
115 trunc:
116 	ND_PRINT((ndo, "%s", tstr));
117 }
118 
119