2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2021-01-28 16:52:15 +00:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-01-26 19:11:58 +00:00
|
|
|
"bytes"
|
2020-04-01 16:27:02 +00:00
|
|
|
"errors"
|
2017-05-30 20:36:49 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-06-01 19:31:30 +00:00
|
|
|
"net"
|
2017-06-04 19:48:15 +00:00
|
|
|
"strconv"
|
2017-06-29 12:39:21 +00:00
|
|
|
"strings"
|
2021-01-26 19:11:58 +00:00
|
|
|
"sync"
|
2017-07-17 14:16:18 +00:00
|
|
|
"sync/atomic"
|
2017-07-18 13:22:56 +00:00
|
|
|
"time"
|
2019-05-14 07:09:52 +00:00
|
|
|
|
2021-11-05 00:52:54 +00:00
|
|
|
"golang.zx2c4.com/go118/netip"
|
2019-05-14 07:09:52 +00:00
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
2017-05-30 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IPCError struct {
|
2021-01-15 21:24:38 +00:00
|
|
|
code int64 // error code
|
|
|
|
err error // underlying/wrapped error
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) Error() string {
|
2021-01-15 21:24:38 +00:00
|
|
|
return fmt.Sprintf("IPC error %d: %v", s.code, s.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s IPCError) Unwrap() error {
|
|
|
|
return s.err
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) ErrorCode() int64 {
|
2021-01-15 21:24:38 +00:00
|
|
|
return s.code
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipcErrorf(code int64, msg string, args ...interface{}) *IPCError {
|
|
|
|
return &IPCError{code: code, err: fmt.Errorf(msg, args...)}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:11:58 +00:00
|
|
|
var byteBufferPool = &sync.Pool{
|
|
|
|
New: func() interface{} { return new(bytes.Buffer) },
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// IpcGetOperation implements the WireGuard configuration protocol "get" operation.
|
|
|
|
// See https://www.wireguard.com/xplatform/#configuration-protocol for details.
|
2020-12-22 18:08:25 +00:00
|
|
|
func (device *Device) IpcGetOperation(w io.Writer) error {
|
2021-01-28 14:26:22 +00:00
|
|
|
device.ipcMutex.RLock()
|
|
|
|
defer device.ipcMutex.RUnlock()
|
|
|
|
|
2021-01-26 19:11:58 +00:00
|
|
|
buf := byteBufferPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer byteBufferPool.Put(buf)
|
|
|
|
sendf := func(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(buf, format, args...)
|
|
|
|
buf.WriteByte('\n')
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
2021-01-27 23:49:31 +00:00
|
|
|
keyf := func(prefix string, key *[32]byte) {
|
|
|
|
buf.Grow(len(key)*2 + 2 + len(prefix))
|
|
|
|
buf.WriteString(prefix)
|
|
|
|
buf.WriteByte('=')
|
|
|
|
const hex = "0123456789abcdef"
|
|
|
|
for i := 0; i < len(key); i++ {
|
|
|
|
buf.WriteByte(hex[key[i]>>4])
|
|
|
|
buf.WriteByte(hex[key[i]&0xf])
|
|
|
|
}
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
func() {
|
|
|
|
// lock required resources
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.net.RLock()
|
|
|
|
defer device.net.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.staticIdentity.RLock()
|
|
|
|
defer device.staticIdentity.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.peers.RLock()
|
|
|
|
defer device.peers.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
|
|
|
// serialize device related values
|
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
if !device.staticIdentity.privateKey.IsZero() {
|
2021-01-27 23:49:31 +00:00
|
|
|
keyf("private_key", (*[32]byte)(&device.staticIdentity.privateKey))
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.port != 0 {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("listen_port=%d", device.net.port)
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.fwmark != 0 {
|
2021-01-26 19:11:58 +00:00
|
|
|
sendf("fwmark=%d", device.net.fwmark)
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
for _, peer := range device.peers.keyMap {
|
2021-11-18 23:37:24 +00:00
|
|
|
// Serialize peer state.
|
|
|
|
// Do the work in an anonymous function so that we can use defer.
|
|
|
|
func() {
|
|
|
|
peer.RLock()
|
|
|
|
defer peer.RUnlock()
|
|
|
|
|
|
|
|
keyf("public_key", (*[32]byte)(&peer.handshake.remoteStatic))
|
|
|
|
keyf("preshared_key", (*[32]byte)(&peer.handshake.presharedKey))
|
|
|
|
sendf("protocol_version=1")
|
|
|
|
if peer.endpoint != nil {
|
|
|
|
sendf("endpoint=%s", peer.endpoint.DstToString())
|
|
|
|
}
|
|
|
|
|
|
|
|
nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)
|
|
|
|
secs := nano / time.Second.Nanoseconds()
|
|
|
|
nano %= time.Second.Nanoseconds()
|
|
|
|
|
|
|
|
sendf("last_handshake_time_sec=%d", secs)
|
|
|
|
sendf("last_handshake_time_nsec=%d", nano)
|
|
|
|
sendf("tx_bytes=%d", atomic.LoadUint64(&peer.stats.txBytes))
|
|
|
|
sendf("rx_bytes=%d", atomic.LoadUint64(&peer.stats.rxBytes))
|
|
|
|
sendf("persistent_keepalive_interval=%d", atomic.LoadUint32(&peer.persistentKeepaliveInterval))
|
|
|
|
|
|
|
|
device.allowedips.EntriesForPeer(peer, func(prefix netip.Prefix) bool {
|
|
|
|
sendf("allowed_ip=%s", prefix.String())
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}()
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-07-17 14:16:18 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// send lines (does not require resource locks)
|
2021-01-26 19:11:58 +00:00
|
|
|
if _, err := w.Write(buf.Bytes()); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorIO, "failed to write output: %w", err)
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// IpcSetOperation implements the WireGuard configuration protocol "set" operation.
|
|
|
|
// See https://www.wireguard.com/xplatform/#configuration-protocol for details.
|
2021-01-15 21:24:38 +00:00
|
|
|
func (device *Device) IpcSetOperation(r io.Reader) (err error) {
|
2021-01-28 14:26:22 +00:00
|
|
|
device.ipcMutex.Lock()
|
|
|
|
defer device.ipcMutex.Unlock()
|
2021-01-25 17:35:35 +00:00
|
|
|
|
2021-01-15 21:24:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Errorf("%v", err)
|
2021-01-15 21:24:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer := new(ipcSetPeer)
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig := true
|
|
|
|
|
2021-01-15 21:24:38 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
2017-05-30 20:36:49 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2017-06-29 12:39:21 +00:00
|
|
|
if line == "" {
|
2021-01-15 22:32:34 +00:00
|
|
|
// Blank line means terminate operation.
|
2021-11-29 17:31:54 +00:00
|
|
|
peer.handlePostConfig()
|
2017-06-29 12:39:21 +00:00
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-29 12:39:21 +00:00
|
|
|
parts := strings.Split(line, "=")
|
|
|
|
if len(parts) != 2 {
|
2021-01-15 21:24:38 +00:00
|
|
|
return ipcErrorf(ipc.IpcErrorProtocol, "failed to parse line %q, found %d =-separated parts, want 2", line, len(parts))
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-29 12:39:21 +00:00
|
|
|
key := parts[0]
|
|
|
|
value := parts[1]
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if key == "public_key" {
|
|
|
|
if deviceConfig {
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig = false
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2021-01-27 17:13:53 +00:00
|
|
|
peer.handlePostConfig()
|
2021-01-15 22:32:34 +00:00
|
|
|
// Load/create the peer we are now configuring.
|
|
|
|
err := device.handlePublicKeyLine(peer, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
var err error
|
|
|
|
if deviceConfig {
|
|
|
|
err = device.handleDeviceLine(key, value)
|
|
|
|
} else {
|
|
|
|
err = device.handlePeerLine(peer, key, value)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 17:13:53 +00:00
|
|
|
peer.handlePostConfig()
|
2019-09-28 18:12:46 +00:00
|
|
|
|
2021-01-25 17:21:43 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorIO, "failed to read input: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-15 22:32:34 +00:00
|
|
|
}
|
2019-09-28 18:12:46 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handleDeviceLine(key, value string) error {
|
|
|
|
switch key {
|
|
|
|
case "private_key":
|
|
|
|
var sk NoisePrivateKey
|
|
|
|
err := sk.FromMaybeZeroHex(value)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set private_key: %w", err)
|
|
|
|
}
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("UAPI: Updating private key")
|
2021-01-15 22:32:34 +00:00
|
|
|
device.SetPrivateKey(sk)
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "listen_port":
|
|
|
|
port, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to parse listen_port: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// update port and rebind
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("UAPI: Updating listen port")
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
device.net.Lock()
|
|
|
|
device.net.port = uint16(port)
|
|
|
|
device.net.Unlock()
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if err := device.BindUpdate(); err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorPortInUse, "failed to set listen_port: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "fwmark":
|
2021-01-25 17:27:06 +00:00
|
|
|
mark, err := strconv.ParseUint(value, 10, 32)
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid fwmark: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("UAPI: Updating fwmark")
|
2021-01-25 17:27:06 +00:00
|
|
|
if err := device.BindSetMark(uint32(mark)); err != nil {
|
2021-01-15 22:32:34 +00:00
|
|
|
return ipcErrorf(ipc.IpcErrorPortInUse, "failed to update fwmark: %w", err)
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "replace_peers":
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set replace_peers, invalid value: %v", value)
|
|
|
|
}
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("UAPI: Removing all peers")
|
2021-01-15 22:32:34 +00:00
|
|
|
device.RemoveAllPeers()
|
2017-11-18 22:34:02 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
default:
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI device key: %v", key)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// An ipcSetPeer is the current state of an IPC set operation on a peer.
|
|
|
|
type ipcSetPeer struct {
|
|
|
|
*Peer // Peer is the current peer being operated on
|
|
|
|
dummy bool // dummy reports whether this peer is a temporary, placeholder peer
|
|
|
|
created bool // new reports whether this is a newly created peer
|
2021-11-11 02:12:37 +00:00
|
|
|
pkaOn bool // pkaOn reports whether the peer had the persistent keepalive turn on
|
2021-01-15 22:32:34 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-27 17:13:53 +00:00
|
|
|
func (peer *ipcSetPeer) handlePostConfig() {
|
2021-11-16 19:27:44 +00:00
|
|
|
if peer.Peer == nil || peer.dummy {
|
2021-11-11 02:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-16 20:13:55 +00:00
|
|
|
if peer.created {
|
|
|
|
peer.disableRoaming = peer.device.net.brokenRoaming && peer.endpoint != nil
|
|
|
|
}
|
2021-11-16 19:27:44 +00:00
|
|
|
if peer.device.isUp() {
|
2021-11-11 02:12:37 +00:00
|
|
|
peer.Start()
|
|
|
|
if peer.pkaOn {
|
|
|
|
peer.SendKeepalive()
|
|
|
|
}
|
2021-01-27 17:13:53 +00:00
|
|
|
peer.SendStagedPackets()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handlePublicKeyLine(peer *ipcSetPeer, value string) error {
|
|
|
|
// Load/create the peer we are configuring.
|
|
|
|
var publicKey NoisePublicKey
|
|
|
|
err := publicKey.FromHex(value)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to get peer by public key: %w", err)
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// Ignore peer with the same public key as this device.
|
|
|
|
device.staticIdentity.RLock()
|
|
|
|
peer.dummy = device.staticIdentity.publicKey.Equals(publicKey)
|
|
|
|
device.staticIdentity.RUnlock()
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if peer.dummy {
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
} else {
|
|
|
|
peer.Peer = device.LookupPeer(publicKey)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer.created = peer.Peer == nil
|
|
|
|
if peer.created {
|
|
|
|
peer.Peer, err = device.NewPeer(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to create new peer: %w", err)
|
|
|
|
}
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Created", peer.Peer)
|
2021-01-15 22:32:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error {
|
|
|
|
switch key {
|
|
|
|
case "update_only":
|
|
|
|
// allow disabling of creation
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set update only, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if peer.created && !peer.dummy {
|
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
peer.dummy = true
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "remove":
|
|
|
|
// remove currently selected peer from device
|
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set remove, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if !peer.dummy {
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Removing", peer.Peer)
|
2021-01-15 22:32:34 +00:00
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
}
|
|
|
|
peer.Peer = &Peer{}
|
|
|
|
peer.dummy = true
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "preshared_key":
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Updating preshared key", peer.Peer)
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
peer.handshake.mutex.Lock()
|
|
|
|
err := peer.handshake.presharedKey.FromHex(value)
|
|
|
|
peer.handshake.mutex.Unlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set preshared key: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "endpoint":
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Updating endpoint", peer.Peer)
|
2021-02-22 01:01:50 +00:00
|
|
|
endpoint, err := device.net.bind.ParseEndpoint(value)
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set endpoint %v: %w", value, err)
|
|
|
|
}
|
2021-01-25 17:32:09 +00:00
|
|
|
peer.Lock()
|
|
|
|
defer peer.Unlock()
|
|
|
|
peer.endpoint = endpoint
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "persistent_keepalive_interval":
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Updating persistent keepalive interval", peer.Peer)
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
secs, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set persistent keepalive interval: %w", err)
|
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
old := atomic.SwapUint32(&peer.persistentKeepaliveInterval, uint32(secs))
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
// Send immediate keepalive if we're turning it on and before it wasn't on.
|
2021-11-11 02:12:37 +00:00
|
|
|
peer.pkaOn = old == 0 && secs != 0
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "replace_allowed_ips":
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Removing all allowedips", peer.Peer)
|
2021-01-15 22:32:34 +00:00
|
|
|
if value != "true" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to replace allowedips, invalid value: %v", value)
|
|
|
|
}
|
|
|
|
if peer.dummy {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
device.allowedips.RemoveByPeer(peer.Peer)
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "allowed_ip":
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Verbosef("%v - UAPI: Adding allowedip", peer.Peer)
|
2021-11-05 00:52:54 +00:00
|
|
|
prefix, err := netip.ParsePrefix(value)
|
2021-01-15 22:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set allowed ip: %w", err)
|
|
|
|
}
|
|
|
|
if peer.dummy {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-05 00:52:54 +00:00
|
|
|
device.allowedips.Insert(prefix, peer.Peer)
|
2018-09-03 05:04:47 +00:00
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
case "protocol_version":
|
|
|
|
if value != "1" {
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid protocol version: %v", value)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2021-01-15 22:32:34 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI peer key: %v", key)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:32:34 +00:00
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 13:30:57 +00:00
|
|
|
func (device *Device) IpcGet() (string, error) {
|
2020-12-22 18:08:25 +00:00
|
|
|
buf := new(strings.Builder)
|
|
|
|
if err := device.IpcGetOperation(buf); err != nil {
|
2020-12-22 13:30:57 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) IpcSet(uapiConf string) error {
|
2020-12-22 18:08:25 +00:00
|
|
|
return device.IpcSetOperation(strings.NewReader(uapiConf))
|
2020-12-22 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
func (device *Device) IpcHandle(socket net.Conn) {
|
2017-07-17 14:16:18 +00:00
|
|
|
defer socket.Close()
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
buffered := func(s io.ReadWriter) *bufio.ReadWriter {
|
|
|
|
reader := bufio.NewReader(s)
|
|
|
|
writer := bufio.NewWriter(s)
|
|
|
|
return bufio.NewReadWriter(reader, writer)
|
|
|
|
}(socket)
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
for {
|
|
|
|
op, err := buffered.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
// handle operation
|
|
|
|
switch op {
|
|
|
|
case "set=1\n":
|
|
|
|
err = device.IpcSetOperation(buffered.Reader)
|
|
|
|
case "get=1\n":
|
2021-01-26 18:35:25 +00:00
|
|
|
var nextByte byte
|
|
|
|
nextByte, err = buffered.ReadByte()
|
2021-01-25 18:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if nextByte != '\n' {
|
2021-01-26 22:05:48 +00:00
|
|
|
err = ipcErrorf(ipc.IpcErrorInvalid, "trailing character in UAPI get: %q", nextByte)
|
2021-01-25 18:00:43 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
err = device.IpcGetOperation(buffered.Writer)
|
|
|
|
default:
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Errorf("invalid UAPI operation: %v", op)
|
2021-01-25 18:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2021-01-25 18:00:43 +00:00
|
|
|
// write status
|
|
|
|
var status *IPCError
|
|
|
|
if err != nil && !errors.As(err, &status) {
|
|
|
|
// shouldn't happen
|
|
|
|
status = ipcErrorf(ipc.IpcErrorUnknown, "other UAPI error: %w", err)
|
|
|
|
}
|
|
|
|
if status != nil {
|
2021-01-26 22:05:48 +00:00
|
|
|
device.log.Errorf("%v", status)
|
2021-01-25 18:00:43 +00:00
|
|
|
fmt.Fprintf(buffered, "errno=%d\n\n", status.ErrorCode())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(buffered, "errno=0\n\n")
|
|
|
|
}
|
|
|
|
buffered.Flush()
|
2017-07-17 14:16:18 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|