├── .travis.yml ├── LICENSE.md ├── README.md ├── fuzz.go ├── vxlan.go └── vxlan_test.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.5 4 | - tip 5 | before_install: 6 | - go get github.com/axw/gocov/gocov 7 | - go get github.com/mattn/goveralls 8 | - go get golang.org/x/tools/cmd/cover 9 | before_script: 10 | - go get -d ./... 11 | script: 12 | - go test -v ./... 13 | - if ! $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN; then echo "Coveralls not available."; fi 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | =========== 3 | 4 | Copyright (C) 2016 Matt Layher 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vxlan [![Build Status](https://travis-ci.org/mdlayher/vxlan.svg?branch=master)](https://travis-ci.org/mdlayher/vxlan) [![Coverage Status](https://coveralls.io/repos/mdlayher/vxlan/badge.svg?branch=master)](https://coveralls.io/r/mdlayher/vxlan?branch=master) [![GoDoc](http://godoc.org/github.com/mdlayher/vxlan?status.svg)](http://godoc.org/github.com/mdlayher/vxlan) [![Report Card](http://goreportcard.com/badge/mdlayher/vxlan)](http://goreportcard.com/report/mdlayher/vxlan) 2 | ===== 3 | 4 | Package `vxlan` implements marshaling and unmarshaling of Virtual eXtensible 5 | Local Area Network (VXLAN) frames, as described in RFC 7348. MIT Licensed. 6 | -------------------------------------------------------------------------------- /fuzz.go: -------------------------------------------------------------------------------- 1 | // +build gofuzz 2 | 3 | package vxlan 4 | 5 | func Fuzz(data []byte) int { 6 | f := new(Frame) 7 | if err := f.UnmarshalBinary(data); err != nil { 8 | return 0 9 | } 10 | 11 | if _, err := f.MarshalBinary(); err != nil { 12 | panic(err) 13 | } 14 | 15 | return 1 16 | } 17 | -------------------------------------------------------------------------------- /vxlan.go: -------------------------------------------------------------------------------- 1 | // Package vxlan implements marshaling and unmarshaling of Virtual eXtensible 2 | // Local Area Network (VXLAN) frames, as described in RFC 7348. 3 | package vxlan 4 | 5 | import ( 6 | "encoding/binary" 7 | "errors" 8 | "io" 9 | 10 | "github.com/mdlayher/ethernet" 11 | ) 12 | 13 | const ( 14 | // MaxVNI is the maximum possible value for a VNI: the maximum value 15 | // of a 24-bit integer. 16 | MaxVNI = (1 << 24) - 1 17 | ) 18 | 19 | var ( 20 | // ErrInvalidFrame is returned when the reserved I bit is not set in 21 | // a byte slice when Frame.UnmarshalBinary is called. 22 | ErrInvalidFrame = errors.New("invalid Frame") 23 | 24 | // ErrInvalidVNI is returned when a Frame contains an invalid VNI, 25 | // and Frame.MarshalBinary is called. 26 | ErrInvalidVNI = errors.New("invalid VNI") 27 | ) 28 | 29 | // A VNI is a 24-bit Virtual Network Identifier. It is used to designate a 30 | // VXLAN overlay network. Use its Valid method to determine if a VNI contains 31 | // a valid value. 32 | type VNI uint32 33 | 34 | // Valid determines if a VNI is a valid, 24-bit integer. 35 | func (v VNI) Valid() bool { 36 | return v <= MaxVNI 37 | } 38 | 39 | // A Frame is an Virtual eXtensible Local Area Network (VXLAN) frame, as 40 | // described in RFC 7348, Section 5. 41 | // 42 | // It contains a VNI used to designate an overlay network, and an embedded 43 | // Ethernet frame which transports an arbitrary payload within the overlay 44 | // network. 45 | type Frame struct { 46 | VNI VNI 47 | Ethernet *ethernet.Frame 48 | } 49 | 50 | // MarshalBinary allocates a byte slice and marshals a Frame into binary form. 51 | // 52 | // If a VNI value is invalid, ErrInvalidVNI will be returned. 53 | func (f *Frame) MarshalBinary() ([]byte, error) { 54 | if !f.VNI.Valid() { 55 | return nil, ErrInvalidVNI 56 | } 57 | 58 | efb, err := f.Ethernet.MarshalFCS() 59 | if err != nil { 60 | return nil, err 61 | } 62 | 63 | b := make([]byte, 8) 64 | 65 | // I flag is always set to 1, all others are reserved 66 | b[0] |= 1 << 3 67 | 68 | binary.BigEndian.PutUint32(b[3:], uint32(f.VNI)) 69 | 70 | // Ethernet frame bytes accompany VXLAN frame 71 | return append(b, efb...), nil 72 | } 73 | 74 | // UnmarshalBinary allocates a byte slice and marshals a Frame into binary form. 75 | // 76 | // If a VNI value is invalid, ErrInvalidVNI will be returned. 77 | func (f *Frame) UnmarshalBinary(b []byte) error { 78 | // Need at least VXLAN frame and empty Ethernet frame 79 | if len(b) < 18 { 80 | return io.ErrUnexpectedEOF 81 | } 82 | 83 | // I flag must be set to 1. 84 | if (b[0] >> 3) != 1 { 85 | return ErrInvalidFrame 86 | } 87 | 88 | f.VNI = VNI(binary.BigEndian.Uint32(b[3:])) 89 | 90 | ef := new(ethernet.Frame) 91 | if err := ef.UnmarshalFCS(b[8:]); err != nil { 92 | return err 93 | } 94 | f.Ethernet = ef 95 | 96 | return nil 97 | } 98 | -------------------------------------------------------------------------------- /vxlan_test.go: -------------------------------------------------------------------------------- 1 | package vxlan 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "testing" 7 | 8 | "github.com/mdlayher/ethernet" 9 | ) 10 | 11 | func TestFrameMarshalBinary(t *testing.T) { 12 | var tests = []struct { 13 | desc string 14 | f *Frame 15 | b []byte 16 | err error 17 | }{ 18 | { 19 | desc: "Frame with VNI value too large", 20 | f: &Frame{ 21 | VNI: MaxVNI + 1, 22 | }, 23 | err: ErrInvalidVNI, 24 | }, 25 | { 26 | desc: "Frame with Ethernet frame with VLAN too large", 27 | f: &Frame{ 28 | Ethernet: ðernet.Frame{ 29 | VLAN: []*ethernet.VLAN{{ 30 | ID: ethernet.VLANMax + 1, 31 | }}, 32 | }, 33 | }, 34 | err: ethernet.ErrInvalidVLAN, 35 | }, 36 | { 37 | desc: "Frame with VNI 1", 38 | f: &Frame{ 39 | VNI: 1, 40 | Ethernet: ðernet.Frame{}, 41 | }, 42 | b: append([]byte{ 43 | 0x08, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x01, 0x00, 45 | }, ethernetFrame(t)...), 46 | }, 47 | { 48 | desc: "Frame with VNI Max", 49 | f: &Frame{ 50 | VNI: MaxVNI, 51 | Ethernet: ðernet.Frame{}, 52 | }, 53 | b: append([]byte{ 54 | 0x08, 0x00, 0x00, 0x00, 55 | 0xff, 0xff, 0xff, 0x00, 56 | }, ethernetFrame(t)...), 57 | }, 58 | } 59 | 60 | for i, tt := range tests { 61 | t.Logf("[%02d] test %q", i, tt.desc) 62 | 63 | b, err := tt.f.MarshalBinary() 64 | if err != nil { 65 | if want, got := tt.err, err; want != got { 66 | t.Fatalf("unexpected error: %v != %v", 67 | want, got) 68 | } 69 | 70 | continue 71 | } 72 | 73 | if want, got := tt.b, b; !bytes.Equal(want, got) { 74 | t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v", 75 | want, got) 76 | } 77 | } 78 | } 79 | 80 | func TestFrameUnmarshalBinary(t *testing.T) { 81 | var tests = []struct { 82 | desc string 83 | b []byte 84 | f *Frame 85 | err error 86 | }{ 87 | { 88 | desc: "Frame too short", 89 | b: []byte{0x00}, 90 | err: io.ErrUnexpectedEOF, 91 | }, 92 | { 93 | desc: "Frame with I flag not set", 94 | b: append([]byte{ 95 | 0x00, 0x00, 0x00, 0x00, 96 | 0x00, 0x00, 0x01, 0x00, 97 | }, ethernetFrame(t)...), 98 | err: ErrInvalidFrame, 99 | }, 100 | { 101 | desc: "Frame with invalid Ethernet frame check sequence", 102 | b: func() []byte { 103 | b := append([]byte{ 104 | 0x08, 0x00, 0x00, 0x00, 105 | 0x00, 0x00, 0x01, 0x00, 106 | }, ethernetFrame(t)...) 107 | 108 | // Break FCS 109 | b[len(b)-1] = 0x00 110 | return b 111 | }(), 112 | err: ethernet.ErrInvalidFCS, 113 | }, 114 | { 115 | desc: "Frame with VNI 1", 116 | b: append([]byte{ 117 | 0x08, 0x00, 0x00, 0x00, 118 | 0x00, 0x00, 0x01, 0x00, 119 | }, ethernetFrame(t)...), 120 | f: &Frame{ 121 | VNI: 1, 122 | Ethernet: ðernet.Frame{}, 123 | }, 124 | }, 125 | { 126 | desc: "Frame with VNI Max", 127 | b: append([]byte{ 128 | 0x08, 0x00, 0x00, 0x00, 129 | 0xff, 0xff, 0xff, 0x00, 130 | }, ethernetFrame(t)...), 131 | f: &Frame{ 132 | VNI: MaxVNI, 133 | Ethernet: ðernet.Frame{}, 134 | }, 135 | }, 136 | } 137 | 138 | for i, tt := range tests { 139 | t.Logf("[%02d] test %q", i, tt.desc) 140 | 141 | f := new(Frame) 142 | if err := f.UnmarshalBinary(tt.b); err != nil { 143 | if want, got := tt.err, err; want != got { 144 | t.Fatalf("unexpected error: %v != %v", 145 | want, got) 146 | } 147 | 148 | continue 149 | } 150 | 151 | fb, err := f.MarshalBinary() 152 | if err != nil { 153 | t.Fatalf("unexpected error: %v", err) 154 | } 155 | 156 | if want, got := tt.b, fb; !bytes.Equal(want, got) { 157 | t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v", 158 | want, got) 159 | } 160 | } 161 | } 162 | 163 | func ethernetFrame(t *testing.T) []byte { 164 | f := new(ethernet.Frame) 165 | fb, err := f.MarshalFCS() 166 | if err != nil { 167 | t.Fatalf("failed to marshal Ethernet frame: %v", err) 168 | } 169 | 170 | return fb 171 | } 172 | --------------------------------------------------------------------------------