wireguard-go/src/uapi.go

373 lines
8.1 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"fmt"
"io"
2017-06-01 21:31:30 +02:00
"net"
"strconv"
"strings"
2017-07-17 16:16:18 +02:00
"sync/atomic"
"time"
)
type IPCError struct {
Code int64
}
func (s *IPCError) Error() string {
return fmt.Sprintf("IPC error: %d", s.Code)
}
func (s *IPCError) ErrorCode() int64 {
return s.Code
}
2017-07-17 16:16:18 +02:00
func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
// create lines
2017-07-17 16:16:18 +02:00
device.mutex.RLock()
device.net.mutex.RLock()
2017-07-17 16:16:18 +02:00
lines := make([]string, 0, 100)
send := func(line string) {
lines = append(lines, line)
}
if !device.privateKey.IsZero() {
send("private_key=" + device.privateKey.ToHex())
}
if device.net.port != 0 {
send(fmt.Sprintf("listen_port=%d", device.net.port))
}
2017-09-21 03:09:57 +02:00
if device.net.fwmark != 0 {
send(fmt.Sprintf("fwmark=%d", device.net.fwmark))
}
for _, peer := range device.peers {
func() {
peer.mutex.RLock()
defer peer.mutex.RUnlock()
send("public_key=" + peer.handshake.remoteStatic.ToHex())
send("preshared_key=" + peer.handshake.presharedKey.ToHex())
if peer.endpoint.set {
send("endpoint=" + peer.endpoint.value.DstToString())
}
nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)
secs := nano / time.Second.Nanoseconds()
nano %= time.Second.Nanoseconds()
send(fmt.Sprintf("last_handshake_time_sec=%d", secs))
send(fmt.Sprintf("last_handshake_time_nsec=%d", nano))
send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes))
send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes))
2017-07-17 16:16:18 +02:00
send(fmt.Sprintf("persistent_keepalive_interval=%d",
atomic.LoadUint64(&peer.persistentKeepaliveInterval),
))
2017-08-04 16:15:53 +02:00
for _, ip := range device.routingTable.AllowedIPs(peer) {
send("allowed_ip=" + ip.String())
}
}()
}
device.net.mutex.RUnlock()
2017-07-17 16:16:18 +02:00
device.mutex.RUnlock()
// send lines
for _, line := range lines {
_, err := socket.WriteString(line + "\n")
if err != nil {
2017-07-17 16:16:18 +02:00
return &IPCError{
Code: ipcErrorIO,
}
}
}
return nil
}
func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
scanner := bufio.NewScanner(socket)
logInfo := device.log.Info
2017-07-17 16:16:18 +02:00
logError := device.log.Error
logDebug := device.log.Debug
var peer *Peer
2017-08-04 16:15:53 +02:00
dummy := false
2017-08-04 16:15:53 +02:00
deviceConfig := true
for scanner.Scan() {
2017-07-17 16:16:18 +02:00
// parse line
line := scanner.Text()
if line == "" {
return nil
}
parts := strings.Split(line, "=")
if len(parts) != 2 {
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorProtocol}
}
key := parts[0]
value := parts[1]
2017-08-04 16:15:53 +02:00
/* device configuration */
2017-08-04 16:15:53 +02:00
if deviceConfig {
2017-08-04 16:15:53 +02:00
switch key {
case "private_key":
var sk NoisePrivateKey
2017-09-26 14:26:12 +02:00
err := sk.FromHex(value)
if err != nil {
logError.Println("Failed to set private_key:", err)
return &IPCError{Code: ipcErrorInvalid}
}
2017-09-26 14:26:12 +02:00
device.SetPrivateKey(sk)
2017-08-04 16:15:53 +02:00
case "listen_port":
port, err := strconv.ParseUint(value, 10, 16)
if err != nil {
logError.Println("Failed to parse listen_port:", err)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
}
device.net.port = uint16(port)
if err := UpdateUDPListener(device); err != nil {
logError.Println("Failed to set listen_port:", err)
2017-09-26 15:15:27 +02:00
return &IPCError{Code: ipcErrorPortInUse}
}
2017-08-04 16:15:53 +02:00
case "fwmark":
// parse fwmark field
fwmark, err := func() (uint32, error) {
if value == "" {
return 0, nil
}
mark, err := strconv.ParseUint(value, 10, 32)
return uint32(mark), err
}()
2017-09-26 14:26:12 +02:00
if err != nil {
logError.Println("Invalid fwmark", err)
return &IPCError{Code: ipcErrorInvalid}
2017-08-22 17:22:45 +02:00
}
2017-08-22 17:22:45 +02:00
device.net.mutex.Lock()
device.net.fwmark = uint32(fwmark)
2017-09-21 03:09:57 +02:00
device.net.mutex.Unlock()
2017-08-04 16:15:53 +02:00
case "public_key":
// switch to peer configuration
deviceConfig = false
2017-07-17 16:16:18 +02:00
2017-08-04 16:15:53 +02:00
case "replace_peers":
if value != "true" {
logError.Println("Failed to set replace_peers, invalid value:", value)
return &IPCError{Code: ipcErrorInvalid}
}
device.RemoveAllPeers()
2017-08-04 16:15:53 +02:00
default:
logError.Println("Invalid UAPI key (device configuration):", key)
return &IPCError{Code: ipcErrorInvalid}
}
2017-08-04 16:15:53 +02:00
}
/* peer configuration */
if !deviceConfig {
switch key {
2017-08-04 16:15:53 +02:00
case "public_key":
var pubKey NoisePublicKey
err := pubKey.FromHex(value)
if err != nil {
logError.Println("Failed to get peer by public_key:", err)
return &IPCError{Code: ipcErrorInvalid}
}
// check if public key of peer equal to device
device.mutex.RLock()
if device.publicKey.Equals(pubKey) {
// create dummy instance (not added to device)
peer = &Peer{}
dummy = true
2017-08-04 16:15:53 +02:00
device.mutex.RUnlock()
logInfo.Println("Ignoring peer with public key of device")
} else {
// find peer referenced
2017-08-04 16:15:53 +02:00
peer, _ = device.peers[pubKey]
device.mutex.RUnlock()
if peer == nil {
peer, err = device.NewPeer(pubKey)
if err != nil {
logError.Println("Failed to create new peer:", err)
return &IPCError{Code: ipcErrorInvalid}
}
}
signalSend(peer.signal.handshakeReset)
dummy = false
2017-08-04 16:15:53 +02:00
}
case "remove":
// remove currently selected peer from device
2017-08-04 16:15:53 +02:00
if value != "true" {
logError.Println("Failed to set remove, invalid value:", value)
return &IPCError{Code: ipcErrorInvalid}
}
if !dummy {
logDebug.Println("Removing", peer.String())
device.RemovePeer(peer.handshake.remoteStatic)
}
peer = &Peer{}
dummy = true
case "preshared_key":
// update PSK
peer.mutex.Lock()
err := peer.handshake.presharedKey.FromHex(value)
peer.mutex.Unlock()
2017-06-01 21:31:30 +02:00
if err != nil {
2017-07-17 16:16:18 +02:00
logError.Println("Failed to set preshared_key:", err)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
2017-06-01 21:31:30 +02:00
}
case "endpoint":
// set endpoint destination and reset handshake timer
peer.mutex.Lock()
err := peer.endpoint.value.SetDst(value)
peer.endpoint.set = (err == nil)
peer.mutex.Unlock()
if err != nil {
2017-07-17 16:16:18 +02:00
logError.Println("Failed to set endpoint:", value)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
2017-06-01 21:31:30 +02:00
}
signalSend(peer.signal.handshakeReset)
case "persistent_keepalive_interval":
2017-08-04 16:15:53 +02:00
// update keep-alive interval
secs, err := strconv.ParseUint(value, 10, 16)
if err != nil {
2017-07-17 16:16:18 +02:00
logError.Println("Failed to set persistent_keepalive_interval:", err)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
}
2017-08-04 16:15:53 +02:00
old := atomic.SwapUint64(
2017-07-17 16:16:18 +02:00
&peer.persistentKeepaliveInterval,
2017-08-04 16:15:53 +02:00
secs,
2017-07-17 16:16:18 +02:00
)
2017-08-04 16:15:53 +02:00
// send immediate keep-alive
if old == 0 && secs != 0 {
if err != nil {
logError.Println("Failed to get tun device status:", err)
return &IPCError{Code: ipcErrorIO}
}
if device.tun.isUp.Get() && !dummy {
2017-08-04 16:15:53 +02:00
peer.SendKeepAlive()
}
}
case "replace_allowed_ips":
2017-08-04 16:15:53 +02:00
if value != "true" {
2017-07-17 16:16:18 +02:00
logError.Println("Failed to set replace_allowed_ips, invalid value:", value)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
}
if !dummy {
device.routingTable.RemovePeer(peer)
}
case "allowed_ip":
_, network, err := net.ParseCIDR(value)
if err != nil {
2017-07-17 16:16:18 +02:00
logError.Println("Failed to set allowed_ip:", err)
2017-08-04 16:15:53 +02:00
return &IPCError{Code: ipcErrorInvalid}
}
ones, _ := network.Mask.Size()
if !dummy {
device.routingTable.Insert(network.IP, uint(ones), peer)
}
default:
2017-08-04 16:15:53 +02:00
logError.Println("Invalid UAPI key (peer configuration):", key)
return &IPCError{Code: ipcErrorInvalid}
}
}
}
return nil
}
func ipcHandle(device *Device, socket net.Conn) {
2017-08-04 16:15:53 +02:00
// create buffered read/writer
2017-07-17 16:16:18 +02:00
defer socket.Close()
2017-07-17 16:16:18 +02:00
buffered := func(s io.ReadWriter) *bufio.ReadWriter {
reader := bufio.NewReader(s)
writer := bufio.NewWriter(s)
return bufio.NewReadWriter(reader, writer)
}(socket)
2017-07-17 16:16:18 +02:00
defer buffered.Flush()
2017-07-17 16:16:18 +02:00
op, err := buffered.ReadString('\n')
if err != nil {
return
}
2017-08-04 16:15:53 +02:00
// handle operation
2017-08-04 16:15:53 +02:00
var status *IPCError
switch op {
2017-07-17 16:16:18 +02:00
case "set=1\n":
device.log.Debug.Println("Config, set operation")
2017-08-04 16:15:53 +02:00
status = ipcSetOperation(device, buffered)
2017-07-17 16:16:18 +02:00
case "get=1\n":
device.log.Debug.Println("Config, get operation")
2017-08-04 16:15:53 +02:00
status = ipcGetOperation(device, buffered)
2017-07-17 16:16:18 +02:00
default:
device.log.Error.Println("Invalid UAPI operation:", op)
2017-08-04 16:15:53 +02:00
return
}
// write status
2017-08-04 16:15:53 +02:00
if status != nil {
device.log.Error.Println(status)
fmt.Fprintf(buffered, "errno=%d\n\n", status.ErrorCode())
} else {
fmt.Fprintf(buffered, "errno=0\n\n")
2017-07-17 16:16:18 +02:00
}
}