]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/server/common.go
9a5554bda2d0429906edb456ab04069635e4b93d
[govpn.git] / src / cypherpunks.ru / govpn / server / common.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 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 server
20
21 import (
22         "github.com/pkg/errors"
23
24         "cypherpunks.ru/govpn"
25 )
26
27 const logFuncPrefix = "govpn/server."
28
29 var (
30         errMisconfiguredTap = errors.New("No PreUp and no Iface, can't create interface")
31         errPreUpNoTap       = errors.New("PreUp didn't returned an interface, and Iface is unset")
32 )
33
34 func (s *Server) callUp(peer *govpn.Peer, proto govpn.Protocol) (*govpn.TAP, error) {
35         var (
36                 tap           *govpn.TAP
37                 conf          = s.confs.Get(*peer.ID)
38                 err           error
39                 isConfigIface = len(conf.Iface) != 0
40                 fields        = s.LogFields()
41         )
42         fields["func"] = logFuncPrefix + "Server.callUp"
43
44         if !isConfigIface && conf.PreUp == nil {
45                 return nil, errors.Wrapf(
46                         errMisconfiguredTap,
47                         "interface:%q, PreUp:%q",
48                         conf.Iface,
49                         conf.PreUp,
50                 )
51         }
52
53         if conf.PreUp != nil {
54                 s.logger.WithFields(fields).Debug("PreUp defined, execute it")
55                 tap, err = conf.PreUp(govpn.PeerContext{
56                         RemoteAddress: peer.Addr,
57                         Protocol:      proto,
58                         Config:        *conf,
59                 })
60                 if err != nil {
61                         return nil, errors.Wrap(err, "conf.PreUp")
62                 }
63                 s.logger.WithFields(fields).WithField("tap", tap).Debug("PreUp finished")
64         } else {
65                 s.logger.WithFields(fields).Debug("No PreUp defined, skip")
66         }
67
68         if tap == nil {
69                 s.logger.WithFields(fields).Debug("PreUp didn't returned an interface, create one")
70                 if !isConfigIface {
71                         return nil, errors.Wrapf(errPreUpNoTap, "interface:%q tap:%q", conf.Iface, tap)
72                 }
73
74                 if tap, err = govpn.TAPListen(conf.Iface, peer.MTU); err != nil {
75                         return nil, errors.Wrap(err, "govpn.TAPListen")
76                 }
77         }
78
79         conf.Iface = tap.Name
80
81         if conf.Up == nil {
82                 s.logger.WithFields(fields).Debug("Got interface, no Up")
83                 return tap, nil
84         }
85         s.logger.WithFields(fields).Debug("Got interface, execute Up")
86
87         err = conf.Up(govpn.PeerContext{
88                 RemoteAddress: peer.Addr,
89                 Protocol:      proto,
90                 Config:        *conf,
91         })
92         if err != nil {
93                 return nil, errors.Wrap(err, "conf.Up")
94         }
95         s.logger.WithFields(fields).Debug("Got interface, Up executed")
96         return tap, nil
97 }
98
99 func (s *Server) callDown(ps *PeerState) error {
100         fields := s.LogFields()
101         fields["func"] = logFuncPrefix + "Server.callDown"
102
103         conf := s.confs.Get(*ps.peer.ID)
104         if conf == nil {
105                 s.logger.WithFields(fields).Error("Couldn't get configuration")
106                 return nil
107         }
108         if conf.Down == nil {
109                 s.logger.WithFields(fields).Debug("No Down, skip")
110                 return nil
111         }
112         s.logger.WithFields(fields).Debug("Execute Down")
113         err := conf.Down(govpn.PeerContext{
114                 RemoteAddress: ps.peer.Addr,
115                 Config:        *conf,
116                 Protocol:      ps.peer.Protocol,
117         })
118         s.logger.WithFields(fields).Debug("Down executed")
119         return errors.Wrap(err, "peer.Down")
120 }