2017-08-11 14:18:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-24 19:35:25 +00:00
|
|
|
"errors"
|
2017-11-14 15:27:53 +00:00
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
"golang.org/x/net/ipv6"
|
2017-08-11 14:18:20 +00:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2017-11-18 22:34:02 +00:00
|
|
|
/* A Bind handles listening on a port for both IPv6 and IPv4 UDP traffic
|
|
|
|
*/
|
|
|
|
type Bind interface {
|
2017-10-08 20:03:32 +00:00
|
|
|
SetMark(value uint32) error
|
2017-11-18 22:34:02 +00:00
|
|
|
ReceiveIPv6(buff []byte) (int, Endpoint, error)
|
|
|
|
ReceiveIPv4(buff []byte) (int, Endpoint, error)
|
|
|
|
Send(buff []byte, end Endpoint) error
|
2017-10-08 20:03:32 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2017-11-17 16:25:45 +00:00
|
|
|
/* An Endpoint maintains the source/destination caching for a peer
|
|
|
|
*
|
2017-11-18 22:34:02 +00:00
|
|
|
* dst : the remote address of a peer ("endpoint" in uapi terminology)
|
2017-11-17 16:25:45 +00:00
|
|
|
* src : the local address from which datagrams originate going to the peer
|
|
|
|
*/
|
2017-11-18 22:34:02 +00:00
|
|
|
type Endpoint interface {
|
2017-11-17 16:25:45 +00:00
|
|
|
ClearSrc() // clears the source address
|
|
|
|
SrcToString() string // returns the local source address (ip:port)
|
|
|
|
DstToString() string // returns the destination address (ip:port)
|
|
|
|
DstToBytes() []byte // used for mac2 cookie calculations
|
|
|
|
DstIP() net.IP
|
|
|
|
SrcIP() net.IP
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:35:25 +00:00
|
|
|
func parseEndpoint(s string) (*net.UDPAddr, error) {
|
|
|
|
|
|
|
|
// ensure that the host is an IP address
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ip := net.ParseIP(host); ip == nil {
|
|
|
|
return nil, errors.New("Failed to parse IP address: " + host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse address and port
|
|
|
|
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addr, err
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:43:55 +00:00
|
|
|
/* Must hold device and net lock
|
|
|
|
*/
|
2017-11-19 12:35:17 +00:00
|
|
|
func unsafeCloseBind(device *Device) error {
|
2017-11-11 22:26:44 +00:00
|
|
|
var err error
|
2017-11-11 14:43:55 +00:00
|
|
|
netc := &device.net
|
|
|
|
if netc.bind != nil {
|
2017-11-11 22:26:44 +00:00
|
|
|
err = netc.bind.Close()
|
2017-11-11 14:43:55 +00:00
|
|
|
netc.bind = nil
|
|
|
|
}
|
2017-11-11 22:26:44 +00:00
|
|
|
return err
|
2017-11-11 14:43:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:52:32 +00:00
|
|
|
func (device *Device) BindUpdate() error {
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
device.net.mutex.Lock()
|
|
|
|
defer device.net.mutex.Unlock()
|
|
|
|
|
|
|
|
device.peers.mutex.Lock()
|
|
|
|
defer device.peers.mutex.Unlock()
|
2017-08-11 14:18:20 +00:00
|
|
|
|
2017-10-07 20:35:23 +00:00
|
|
|
// close existing sockets
|
2017-08-11 14:18:20 +00:00
|
|
|
|
2017-11-19 12:35:17 +00:00
|
|
|
if err := unsafeCloseBind(device); err != nil {
|
2017-11-11 14:43:55 +00:00
|
|
|
return err
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:35:23 +00:00
|
|
|
// open new sockets
|
2017-08-11 14:18:20 +00:00
|
|
|
|
2017-12-29 16:42:09 +00:00
|
|
|
if device.isUp.Get() {
|
2017-08-17 10:58:18 +00:00
|
|
|
|
2017-10-08 20:03:32 +00:00
|
|
|
// bind to new port
|
|
|
|
|
|
|
|
var err error
|
2018-02-02 15:40:14 +00:00
|
|
|
netc := &device.net
|
2017-11-18 23:21:58 +00:00
|
|
|
netc.bind, netc.port, err = CreateBind(netc.port)
|
2017-10-08 20:03:32 +00:00
|
|
|
if err != nil {
|
2017-11-11 14:43:55 +00:00
|
|
|
netc.bind = nil
|
2017-10-08 20:03:32 +00:00
|
|
|
return err
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|
2017-08-17 10:58:18 +00:00
|
|
|
|
2017-10-08 20:03:32 +00:00
|
|
|
// set mark
|
|
|
|
|
|
|
|
err = netc.bind.SetMark(netc.fwmark)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-22 15:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 19:33:47 +00:00
|
|
|
// clear cached source addresses
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
for _, peer := range device.peers.keyMap {
|
2017-10-16 19:33:47 +00:00
|
|
|
peer.mutex.Lock()
|
2018-02-02 15:40:14 +00:00
|
|
|
defer peer.mutex.Unlock()
|
2017-11-18 22:34:02 +00:00
|
|
|
if peer.endpoint != nil {
|
|
|
|
peer.endpoint.ClearSrc()
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
}
|
2017-11-11 14:43:55 +00:00
|
|
|
|
2018-01-26 21:52:32 +00:00
|
|
|
// start receiving routines
|
2017-11-11 14:43:55 +00:00
|
|
|
|
2017-11-29 17:46:31 +00:00
|
|
|
go device.RoutineReceiveIncoming(ipv4.Version, netc.bind)
|
|
|
|
go device.RoutineReceiveIncoming(ipv6.Version, netc.bind)
|
2017-11-14 15:27:53 +00:00
|
|
|
|
2017-11-11 22:26:44 +00:00
|
|
|
device.log.Debug.Println("UDP bind has been updated")
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 10:58:18 +00:00
|
|
|
return nil
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:52:32 +00:00
|
|
|
func (device *Device) BindClose() error {
|
2017-11-11 14:43:55 +00:00
|
|
|
device.net.mutex.Lock()
|
2017-11-19 12:35:17 +00:00
|
|
|
err := unsafeCloseBind(device)
|
2017-11-11 14:43:55 +00:00
|
|
|
device.net.mutex.Unlock()
|
|
|
|
return err
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|