]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/tap.go
43207c19e7cc032771d05eb27c40453fd4a4de13
[govpn.git] / src / govpn / tap.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package govpn
20
21 import (
22         "io"
23
24         "golang.org/x/crypto/poly1305"
25 )
26
27 const (
28         EtherSize = 14
29 )
30
31 type TAP struct {
32         Name string
33         Sink chan []byte
34         dev  io.ReadWriter
35         buf0 []byte
36         buf1 []byte
37         bufZ bool
38 }
39
40 var (
41         taps = make(map[string]*TAP)
42 )
43
44 // Return maximal acceptable TAP interface MTU. This is daemon's MTU
45 // minus nonce, MAC, pad and Ethernet header sizes.
46 func TAPMaxMTU() int {
47         return MTU - poly1305.TagSize - NonceSize - 1 - EtherSize
48 }
49
50 func NewTAP(ifaceName string) (*TAP, error) {
51         maxIfacePktSize := TAPMaxMTU() + EtherSize
52         tapRaw, err := newTAPer(ifaceName)
53         if err != nil {
54                 return nil, err
55         }
56         tap := TAP{
57                 Name: ifaceName,
58                 dev:  tapRaw,
59                 buf0: make([]byte, maxIfacePktSize),
60                 buf1: make([]byte, maxIfacePktSize),
61                 Sink: make(chan []byte),
62         }
63         go func() {
64                 var n int
65                 var err error
66                 var buf []byte
67                 for {
68                         if tap.bufZ {
69                                 buf = tap.buf0
70                         } else {
71                                 buf = tap.buf1
72                         }
73                         tap.bufZ = !tap.bufZ
74                         n, err = tap.dev.Read(buf)
75                         if err != nil {
76                                 panic("Reading TAP:" + err.Error())
77                         }
78                         tap.Sink <- buf[:n]
79                 }
80         }()
81         return &tap, nil
82 }
83
84 func (t *TAP) Write(data []byte) (n int, err error) {
85         return t.dev.Write(data)
86 }
87
88 func TAPListen(ifaceName string) (*TAP, error) {
89         tap, exists := taps[ifaceName]
90         if exists {
91                 return tap, nil
92         }
93         tap, err := NewTAP(ifaceName)
94         if err != nil {
95                 return nil, err
96         }
97         taps[ifaceName] = tap
98         return tap, nil
99 }