2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 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"
|
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"
|
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
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
2019-05-14 07:09:52 +00:00
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
2017-05-30 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IPCError struct {
|
2019-01-03 18:04:00 +00:00
|
|
|
int64
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) Error() string {
|
2019-01-03 18:04:00 +00:00
|
|
|
return fmt.Sprintf("IPC error: %d", s.int64)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 01:49:27 +00:00
|
|
|
func (s IPCError) ErrorCode() int64 {
|
2019-01-03 18:04:00 +00:00
|
|
|
return s.int64
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 16:27:02 +00:00
|
|
|
func (device *Device) IpcGetOperation(socket *bufio.Writer) error {
|
2017-06-28 21:45:45 +00:00
|
|
|
lines := make([]string, 0, 100)
|
|
|
|
send := func(line string) {
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
func() {
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// 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() {
|
|
|
|
send("private_key=" + device.staticIdentity.privateKey.ToHex())
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.port != 0 {
|
|
|
|
send(fmt.Sprintf("listen_port=%d", device.net.port))
|
|
|
|
}
|
|
|
|
|
|
|
|
if device.net.fwmark != 0 {
|
|
|
|
send(fmt.Sprintf("fwmark=%d", device.net.fwmark))
|
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// serialize each peer state
|
|
|
|
|
|
|
|
for _, peer := range device.peers.keyMap {
|
2019-01-03 18:04:00 +00:00
|
|
|
peer.RLock()
|
|
|
|
defer peer.RUnlock()
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-06-28 21:45:45 +00:00
|
|
|
send("public_key=" + peer.handshake.remoteStatic.ToHex())
|
|
|
|
send("preshared_key=" + peer.handshake.presharedKey.ToHex())
|
2018-09-03 05:04:47 +00:00
|
|
|
send("protocol_version=1")
|
2017-11-18 22:34:02 +00:00
|
|
|
if peer.endpoint != nil {
|
|
|
|
send("endpoint=" + peer.endpoint.DstToString())
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
2017-07-18 13:22:56 +00:00
|
|
|
|
|
|
|
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))
|
2018-05-20 01:26:46 +00:00
|
|
|
send(fmt.Sprintf("tx_bytes=%d", atomic.LoadUint64(&peer.stats.txBytes)))
|
|
|
|
send(fmt.Sprintf("rx_bytes=%d", atomic.LoadUint64(&peer.stats.rxBytes)))
|
2018-04-18 05:24:33 +00:00
|
|
|
send(fmt.Sprintf("persistent_keepalive_interval=%d", peer.persistentKeepaliveInterval))
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
for _, ip := range device.allowedips.EntriesForPeer(peer) {
|
2017-06-28 21:45:45 +00:00
|
|
|
send("allowed_ip=" + ip.String())
|
|
|
|
}
|
|
|
|
|
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)
|
2017-06-28 21:45:45 +00:00
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
_, err := socket.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorIO}
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 16:27:02 +00:00
|
|
|
func (device *Device) IpcSetOperation(socket *bufio.Reader) error {
|
2017-05-30 20:36:49 +00:00
|
|
|
scanner := bufio.NewScanner(socket)
|
2017-07-17 14:16:18 +00:00
|
|
|
logError := device.log.Error
|
|
|
|
logDebug := device.log.Debug
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-06-29 12:39:21 +00:00
|
|
|
var peer *Peer
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2017-08-07 13:25:04 +00:00
|
|
|
dummy := false
|
2019-09-28 18:12:46 +00:00
|
|
|
createdNewPeer := false
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig := true
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
// parse line
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
line := scanner.Text()
|
2017-06-29 12:39:21 +00:00
|
|
|
if line == "" {
|
|
|
|
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 {
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorProtocol}
|
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
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
/* device configuration */
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
if deviceConfig {
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
switch key {
|
|
|
|
case "private_key":
|
|
|
|
var sk NoisePrivateKey
|
2020-02-04 17:03:31 +00:00
|
|
|
err := sk.FromMaybeZeroHex(value)
|
2017-09-26 12:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to set private_key:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println("UAPI: Updating private key")
|
2017-09-26 12:26:12 +00:00
|
|
|
device.SetPrivateKey(sk)
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
case "listen_port":
|
2018-01-13 08:00:37 +00:00
|
|
|
|
|
|
|
// parse port number
|
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
port, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
2017-10-27 08:43:37 +00:00
|
|
|
logError.Println("Failed to parse listen_port:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-07-23 14:21:08 +00:00
|
|
|
}
|
2018-01-13 08:00:37 +00:00
|
|
|
|
|
|
|
// update port and rebind
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
logDebug.Println("UAPI: Updating listen port")
|
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.net.Lock()
|
2017-10-16 19:33:47 +00:00
|
|
|
device.net.port = uint16(port)
|
2019-01-03 18:04:00 +00:00
|
|
|
device.net.Unlock()
|
2018-01-13 08:00:37 +00:00
|
|
|
|
2018-01-26 21:52:32 +00:00
|
|
|
if err := device.BindUpdate(); err != nil {
|
2017-08-11 14:18:20 +00:00
|
|
|
logError.Println("Failed to set listen_port:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorPortInUse}
|
2017-08-11 14:18:20 +00:00
|
|
|
}
|
2017-08-07 13:25:04 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
case "fwmark":
|
2017-11-17 16:25:45 +00:00
|
|
|
|
|
|
|
// 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 12:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
logError.Println("Invalid fwmark", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-08-22 15:22:45 +00:00
|
|
|
}
|
2017-11-17 16:25:45 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
logDebug.Println("UAPI: Updating fwmark")
|
|
|
|
|
2018-02-18 19:49:03 +00:00
|
|
|
if err := device.BindSetMark(uint32(fwmark)); err != nil {
|
2018-01-26 21:52:32 +00:00
|
|
|
logError.Println("Failed to update fwmark:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorPortInUse}
|
2018-01-26 21:52:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
case "public_key":
|
|
|
|
// switch to peer configuration
|
2018-02-02 15:40:14 +00:00
|
|
|
logDebug.Println("UAPI: Transition to peer configuration")
|
2017-08-04 14:15:53 +00:00
|
|
|
deviceConfig = false
|
2017-07-17 14:16:18 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
case "replace_peers":
|
|
|
|
if value != "true" {
|
|
|
|
logError.Println("Failed to set replace_peers, invalid value:", value)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
logDebug.Println("UAPI: Removing all peers")
|
2017-08-04 14:15:53 +00:00
|
|
|
device.RemoveAllPeers()
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
default:
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Invalid UAPI device key:", key)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* peer configuration */
|
|
|
|
|
|
|
|
if !deviceConfig {
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
switch key {
|
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
case "public_key":
|
2018-02-02 15:40:14 +00:00
|
|
|
var publicKey NoisePublicKey
|
|
|
|
err := publicKey.FromHex(value)
|
2017-08-04 14:15:53 +00:00
|
|
|
if err != nil {
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Failed to get peer by public key:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
// ignore peer with public key of device
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2019-01-03 18:04:00 +00:00
|
|
|
device.staticIdentity.RLock()
|
2018-05-13 21:14:43 +00:00
|
|
|
dummy = device.staticIdentity.publicKey.Equals(publicKey)
|
2019-01-03 18:04:00 +00:00
|
|
|
device.staticIdentity.RUnlock()
|
2017-08-07 13:25:04 +00:00
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
if dummy {
|
2017-08-07 13:25:04 +00:00
|
|
|
peer = &Peer{}
|
2018-05-14 01:29:21 +00:00
|
|
|
} else {
|
|
|
|
peer = device.LookupPeer(publicKey)
|
2018-02-02 15:40:14 +00:00
|
|
|
}
|
2017-08-07 13:25:04 +00:00
|
|
|
|
2019-09-28 18:12:46 +00:00
|
|
|
createdNewPeer = peer == nil
|
|
|
|
if createdNewPeer {
|
2018-02-02 15:40:14 +00:00
|
|
|
peer, err = device.NewPeer(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to create new peer:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-08-07 13:25:04 +00:00
|
|
|
}
|
2019-08-05 14:57:41 +00:00
|
|
|
if peer == nil {
|
|
|
|
dummy = true
|
|
|
|
peer = &Peer{}
|
|
|
|
} else {
|
|
|
|
logDebug.Println(peer, "- UAPI: Created")
|
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 18:12:46 +00:00
|
|
|
case "update_only":
|
|
|
|
|
|
|
|
// allow disabling of creation
|
|
|
|
|
|
|
|
if value != "true" {
|
|
|
|
logError.Println("Failed to set update only, invalid value:", value)
|
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
|
|
|
}
|
|
|
|
if createdNewPeer && !dummy {
|
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
peer = &Peer{}
|
|
|
|
dummy = true
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
case "remove":
|
2017-10-16 19:33:47 +00:00
|
|
|
|
|
|
|
// remove currently selected peer from device
|
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
if value != "true" {
|
|
|
|
logError.Println("Failed to set remove, invalid value:", value)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
2017-08-07 13:25:04 +00:00
|
|
|
if !dummy {
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Removing")
|
2017-08-07 13:25:04 +00:00
|
|
|
device.RemovePeer(peer.handshake.remoteStatic)
|
|
|
|
}
|
|
|
|
peer = &Peer{}
|
|
|
|
dummy = true
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
case "preshared_key":
|
2017-10-16 19:33:47 +00:00
|
|
|
|
|
|
|
// update PSK
|
|
|
|
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Updating preshared key")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
|
|
|
peer.handshake.mutex.Lock()
|
2017-08-07 13:25:04 +00:00
|
|
|
err := peer.handshake.presharedKey.FromHex(value)
|
2018-02-02 15:40:14 +00:00
|
|
|
peer.handshake.mutex.Unlock()
|
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
if err != nil {
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Failed to set preshared key:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-06-01 19:31:30 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
case "endpoint":
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2017-11-18 22:34:02 +00:00
|
|
|
// set endpoint destination
|
|
|
|
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Updating endpoint")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-11-18 22:34:02 +00:00
|
|
|
err := func() error {
|
2019-01-03 18:04:00 +00:00
|
|
|
peer.Lock()
|
|
|
|
defer peer.Unlock()
|
2019-11-07 16:13:05 +00:00
|
|
|
endpoint, err := conn.CreateEndpoint(value)
|
2017-11-18 23:21:58 +00:00
|
|
|
if err != nil {
|
2017-11-18 22:34:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
peer.endpoint = endpoint
|
|
|
|
return nil
|
|
|
|
}()
|
2017-10-16 19:33:47 +00:00
|
|
|
|
2017-06-30 12:41:08 +00:00
|
|
|
if err != nil {
|
2019-03-19 06:34:04 +00:00
|
|
|
logError.Println("Failed to set endpoint:", err, ":", value)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-06-01 19:31:30 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
case "persistent_keepalive_interval":
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2018-05-07 20:27:03 +00:00
|
|
|
// update persistent keepalive interval
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2018-11-05 04:46:27 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Updating persistent keepalive interval")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
secs, err := strconv.ParseUint(value, 10, 16)
|
|
|
|
if err != nil {
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Failed to set persistent keepalive interval:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-06-04 19:48:15 +00:00
|
|
|
}
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2018-04-18 05:24:33 +00:00
|
|
|
old := peer.persistentKeepaliveInterval
|
|
|
|
peer.persistentKeepaliveInterval = uint16(secs)
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2018-05-07 20:27:03 +00:00
|
|
|
// send immediate keepalive if we're turning it on and before it wasn't on
|
2017-08-04 14:15:53 +00:00
|
|
|
|
|
|
|
if old == 0 && secs != 0 {
|
|
|
|
if err != nil {
|
|
|
|
logError.Println("Failed to get tun device status:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorIO}
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
2017-12-29 16:42:09 +00:00
|
|
|
if device.isUp.Get() && !dummy {
|
2018-05-07 20:27:03 +00:00
|
|
|
peer.SendKeepalive()
|
2017-08-04 14:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
case "replace_allowed_ips":
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Removing all allowedips")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
if value != "true" {
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Failed to replace allowedips, invalid value:", value)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-06-04 19:48:15 +00:00
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
|
|
|
if dummy {
|
|
|
|
continue
|
2017-08-07 13:25:04 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2018-05-13 21:14:43 +00:00
|
|
|
device.allowedips.RemoveByPeer(peer)
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
case "allowed_ip":
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2018-05-21 01:38:50 +00:00
|
|
|
logDebug.Println(peer, "- UAPI: Adding allowedip")
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
_, network, err := net.ParseCIDR(value)
|
|
|
|
if err != nil {
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Failed to set allowed ip:", err)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-06-04 19:48:15 +00:00
|
|
|
}
|
2018-02-02 15:40:14 +00:00
|
|
|
|
|
|
|
if dummy {
|
|
|
|
continue
|
2017-08-07 13:25:04 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2018-02-02 15:40:14 +00:00
|
|
|
ones, _ := network.Mask.Size()
|
2018-05-13 21:14:43 +00:00
|
|
|
device.allowedips.Insert(network.IP, uint(ones), peer)
|
2018-02-02 15:40:14 +00:00
|
|
|
|
2018-09-03 05:04:47 +00:00
|
|
|
case "protocol_version":
|
|
|
|
|
|
|
|
if value != "1" {
|
|
|
|
logError.Println("Invalid protocol version:", value)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2018-09-03 05:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
default:
|
2018-05-21 01:38:50 +00:00
|
|
|
logError.Println("Invalid UAPI peer key:", key)
|
2019-03-03 03:04:41 +00:00
|
|
|
return &IPCError{ipc.IpcErrorInvalid}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
func (device *Device) IpcHandle(socket net.Conn) {
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
// create buffered read/writer
|
|
|
|
|
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
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
defer buffered.Flush()
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
op, err := buffered.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
// handle operation
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
var status *IPCError
|
|
|
|
|
|
|
|
switch op {
|
2017-07-17 14:16:18 +00:00
|
|
|
case "set=1\n":
|
2020-04-01 16:27:02 +00:00
|
|
|
err = device.IpcSetOperation(buffered.Reader)
|
|
|
|
if err != nil && !errors.As(err, &status) {
|
|
|
|
// should never happen
|
|
|
|
device.log.Error.Println("Invalid UAPI error:", err)
|
|
|
|
status = &IPCError{1}
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-07-17 14:16:18 +00:00
|
|
|
case "get=1\n":
|
2020-04-01 16:27:02 +00:00
|
|
|
err = device.IpcGetOperation(buffered.Writer)
|
|
|
|
if err != nil && !errors.As(err, &status) {
|
|
|
|
// should never happen
|
|
|
|
device.log.Error.Println("Invalid UAPI error:", err)
|
|
|
|
status = &IPCError{1}
|
|
|
|
}
|
2017-07-17 14:16:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
device.log.Error.Println("Invalid UAPI operation:", op)
|
2017-08-04 14:15:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// write status
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-08-04 14:15:53 +00: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 14:16:18 +00:00
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|