Added fwmark code

This commit is contained in:
Mathias Hall-Andersen 2017-08-25 14:53:23 +02:00
parent 4986cfe78b
commit eafa3df606
6 changed files with 75 additions and 21 deletions

11
src/conn_default.go Normal file
View File

@ -0,0 +1,11 @@
// +build !linux
package main
import (
"net"
)
func setFwmark(conn *net.UDPConn, value int) error {
return nil
}

24
src/conn_linux.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"golang.org/x/sys/unix"
"net"
)
func setMark(conn *net.UDPConn, value int) error {
if conn == nil || value == 0 {
return nil
}
file, err := conn.File()
if err != nil {
return err
}
return unix.SetsockoptInt(
int(file.Fd()),
unix.SOL_SOCKET,
unix.SO_MARK,
value,
)
}

View File

@ -30,12 +30,14 @@ const (
QueueOutboundSize = 1024 QueueOutboundSize = 1024
QueueInboundSize = 1024 QueueInboundSize = 1024
QueueHandshakeSize = 1024 QueueHandshakeSize = 1024
MinMessageSize = MessageTransportSize // size of keep-alive MaxSegmentSize = (1 << 16) - 1 // largest possible UDP datagram
MaxMessageSize = ((1 << 16) - 1) + MessageTransportHeaderSize MinMessageSize = MessageKeepaliveSize // minimum size of transport message (keepalive)
MaxPeers = 1 << 16 MaxMessageSize = MaxSegmentSize // maximum size of transport message
MaxContentSize = MaxSegmentSize - MessageTransportSize // maximum size of transport message content
) )
const ( const (
UnderLoadQueueSize = QueueHandshakeSize / 8 UnderLoadQueueSize = QueueHandshakeSize / 8
UnderLoadAfterTime = time.Second // how long does the device remain under load after detected UnderLoadAfterTime = time.Second // how long does the device remain under load after detected
MaxPeers = 1 << 16 // maximum number of configured peers
) )

View File

@ -32,12 +32,13 @@ const (
) )
const ( const (
MessageInitiationSize = 148 MessageInitiationSize = 148 // size of handshake initation message
MessageResponseSize = 92 MessageResponseSize = 92 // size of response message
MessageCookieReplySize = 64 MessageCookieReplySize = 64 // size of cookie reply message
MessageTransportHeaderSize = 16 MessageTransportHeaderSize = 16 // size of data preceeding content in transport message
MessageTransportSize = MessageTransportHeaderSize + poly1305.TagSize // size of empty transport MessageTransportSize = MessageTransportHeaderSize + poly1305.TagSize // size of empty transport
MessageKeepaliveSize = MessageTransportSize MessageKeepaliveSize = MessageTransportSize // size of keepalive
MessageHandshakeSize = MessageInitiationSize // size of largest handshake releated message
) )
const ( const (

View File

@ -54,6 +54,26 @@ func (device *Device) addToInboundQueue(
} }
} }
func (device *Device) addToDecryptionQueue(
queue chan *QueueInboundElement,
element *QueueInboundElement,
) {
for {
select {
case queue <- element:
return
default:
select {
case old := <-queue:
// drop & release to potential consumer
old.Drop()
old.mutex.Unlock()
default:
}
}
}
}
func (device *Device) addToHandshakeQueue( func (device *Device) addToHandshakeQueue(
queue chan QueueHandshakeElement, queue chan QueueHandshakeElement,
element QueueHandshakeElement, element QueueHandshakeElement,
@ -167,7 +187,7 @@ func (device *Device) RoutineReceiveIncomming() {
// add to decryption queues // add to decryption queues
device.addToInboundQueue(device.queue.decryption, elem) device.addToDecryptionQueue(device.queue.decryption, elem)
device.addToInboundQueue(peer.queue.inbound, elem) device.addToInboundQueue(peer.queue.inbound, elem)
buffer = device.GetMessageBuffer() buffer = device.GetMessageBuffer()
continue continue
@ -218,7 +238,6 @@ func (device *Device) RoutineDecryption() {
// check if dropped // check if dropped
if elem.IsDropped() { if elem.IsDropped() {
elem.mutex.Unlock() // TODO: Make consistent with send
continue continue
} }
@ -256,7 +275,7 @@ func (device *Device) RoutineHandshake() {
logDebug := device.log.Debug logDebug := device.log.Debug
logDebug.Println("Routine, handshake routine, started for device") logDebug.Println("Routine, handshake routine, started for device")
var temp [256]byte var temp [MessageHandshakeSize]byte
var elem QueueHandshakeElement var elem QueueHandshakeElement
for { for {

View File

@ -101,6 +101,7 @@ func addToEncryptionQueue(
default: default:
select { select {
case old := <-queue: case old := <-queue:
// drop & release to potential consumer
old.Drop() old.Drop()
old.mutex.Unlock() old.mutex.Unlock()
default: default:
@ -137,19 +138,16 @@ func (peer *Peer) SendBuffer(buffer []byte) (int, error) {
*/ */
func (device *Device) RoutineReadFromTUN() { func (device *Device) RoutineReadFromTUN() {
var elem *QueueOutboundElement elem := device.NewOutboundElement()
logDebug := device.log.Debug logDebug := device.log.Debug
logError := device.log.Error logError := device.log.Error
logDebug.Println("Routine, TUN Reader: started") logDebug.Println("Routine, TUN Reader started")
for { for {
// read packet
if elem == nil { // read packet
elem = device.NewOutboundElement()
}
elem.packet = elem.buffer[MessageTransportHeaderSize:] elem.packet = elem.buffer[MessageTransportHeaderSize:]
size, err := device.tun.device.Read(elem.packet) size, err := device.tun.device.Read(elem.packet)
@ -159,7 +157,7 @@ func (device *Device) RoutineReadFromTUN() {
return return
} }
if size == 0 { if size == 0 || size > MaxContentSize {
continue continue
} }
@ -191,7 +189,7 @@ func (device *Device) RoutineReadFromTUN() {
continue continue
} }
// check if known endpoint // check if known endpoint (drop early)
peer.mutex.RLock() peer.mutex.RLock()
if peer.endpoint == nil { if peer.endpoint == nil {
@ -205,8 +203,7 @@ func (device *Device) RoutineReadFromTUN() {
signalSend(peer.signal.handshakeReset) signalSend(peer.signal.handshakeReset)
addToOutboundQueue(peer.queue.nonce, elem) addToOutboundQueue(peer.queue.nonce, elem)
elem = nil elem = device.NewOutboundElement()
} }
} }