xref: /freebsd/contrib/tcpdump/print-vxlan.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that: (1) source code
4  * distributions retain the above copyright notice and this paragraph
5  * in its entirety, and (2) distributions including binary code include
6  * the above copyright notice and this paragraph in its entirety in
7  * the documentation or other materials provided with the distribution.
8  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11  * FOR A PARTICULAR PURPOSE.
12  *
13  * Original code by Francesco Fondelli (francesco dot fondelli, gmail dot com)
14  */
15 
16 /* \summary: Virtual eXtensible Local Area Network (VXLAN) printer */
17 
18 /* specification: RFC 7348 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include "netdissect-stdinc.h"
25 
26 #define ND_LONGJMP_FROM_TCHECK
27 #include "netdissect.h"
28 #include "extract.h"
29 
30 static const struct tok vxlan_flags [] = {
31     { 0x08, "I" },
32     { 0, NULL }
33 };
34 #define VXLAN_HDR_LEN 8
35 
36 /*
37  * VXLAN header, RFC7348
38  *               Virtual eXtensible Local Area Network (VXLAN): A Framework
39  *               for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks
40  *
41  *     0                   1                   2                   3
42  *     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
43  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  *    |R|R|R|R|I|R|R|R|            Reserved                           |
45  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46  *    |                VXLAN Network Identifier (VNI) |   Reserved    |
47  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48  */
49 
50 void
51 vxlan_print(netdissect_options *ndo, const u_char *bp, u_int len)
52 {
53     uint8_t flags;
54     uint32_t vni;
55 
56     ndo->ndo_protocol = "vxlan";
57     nd_print_protocol_caps(ndo);
58     if (len < VXLAN_HDR_LEN)
59         goto invalid;
60 
61     flags = GET_U_1(bp);
62     bp += 1;
63     ND_PRINT(", flags [%s] (0x%02x), ",
64              bittok2str_nosep(vxlan_flags, "invalid", flags), flags);
65 
66     /* 1st Reserved */
67     bp += 3;
68 
69     vni = GET_BE_U_3(bp);
70     bp += 3;
71     ND_PRINT("vni %u\n", vni);
72 
73     /* 2nd Reserved */
74     ND_TCHECK_1(bp);
75     bp += 1;
76 
77     ether_print(ndo, bp, len - VXLAN_HDR_LEN, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
78 
79     return;
80 
81 invalid:
82     nd_print_invalid(ndo);
83 }
84