Fix up fwmark handling

This commit is contained in:
Jason A. Donenfeld 2017-09-21 03:09:57 +02:00
parent 47a21c8bb0
commit c545d63bb9
4 changed files with 30 additions and 20 deletions

View file

@ -6,6 +6,6 @@ import (
"net" "net"
) )
func setMark(conn *net.UDPConn, value int) error { func setMark(conn *net.UDPConn, value uint32) error {
return nil return nil
} }

View file

@ -5,8 +5,8 @@ import (
"net" "net"
) )
func setMark(conn *net.UDPConn, value int) error { func setMark(conn *net.UDPConn, value uint32) error {
if conn == nil || value == 0 { if conn == nil {
return nil return nil
} }
@ -19,6 +19,6 @@ func setMark(conn *net.UDPConn, value int) error {
int(file.Fd()), int(file.Fd()),
unix.SOL_SOCKET, unix.SOL_SOCKET,
unix.SO_MARK, unix.SO_MARK,
value, int(value),
) )
} }

View file

@ -24,7 +24,7 @@ type Device struct {
mutex sync.RWMutex mutex sync.RWMutex
addr *net.UDPAddr // UDP source address addr *net.UDPAddr // UDP source address
conn *net.UDPConn // UDP "connection" conn *net.UDPConn // UDP "connection"
fwmark int fwmark uint32
} }
mutex sync.RWMutex mutex sync.RWMutex
privateKey NoisePrivateKey privateKey NoisePrivateKey

View file

@ -42,6 +42,9 @@ func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
if device.net.addr != nil { if device.net.addr != nil {
send(fmt.Sprintf("listen_port=%d", device.net.addr.Port)) send(fmt.Sprintf("listen_port=%d", device.net.addr.Port))
} }
if device.net.fwmark != 0 {
send(fmt.Sprintf("fwmark=%d", device.net.fwmark))
}
for _, peer := range device.peers { for _, peer := range device.peers {
func() { func() {
@ -158,25 +161,32 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
// TODO: Clear source address of all peers // TODO: Clear source address of all peers
case "fwmark": case "fwmark":
fwmark, err := strconv.ParseInt(value, 10, 32) var fwmark uint64 = 0
if err != nil { if value != "" {
logError.Println("Invalid fwmark", err) var err error
return &IPCError{Code: ipcErrorInvalid} fwmark, err = strconv.ParseUint(value, 10, 32)
if err != nil {
logError.Println("Invalid fwmark", err)
return &IPCError{Code: ipcErrorInvalid}
}
} }
device.net.mutex.Lock() device.net.mutex.Lock()
device.net.fwmark = int(fwmark) if fwmark > 0 || device.net.fwmark > 0 {
err = setMark( device.net.fwmark = uint32(fwmark)
device.net.conn, err := setMark(
device.net.fwmark, device.net.conn,
) device.net.fwmark,
device.net.mutex.Unlock() )
if err != nil { if err != nil {
logError.Println("Failed to set fwmark:", err) logError.Println("Failed to set fwmark:", err)
return &IPCError{Code: ipcErrorIO} device.net.mutex.Unlock()
} return &IPCError{Code: ipcErrorIO}
}
// TODO: Clear source address of all peers // TODO: Clear source address of all peers
}
device.net.mutex.Unlock()
case "public_key": case "public_key":