dae/component/control/control_plane.go

446 lines
14 KiB
Go
Raw Normal View History

2023-01-23 18:54:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
2023-01-28 12:56:06 +07:00
* Copyright (c) since 2022, v2rayA Organization <team@v2raya.org>
2023-01-23 18:54:21 +07:00
*/
package control
import (
"context"
"encoding/hex"
2023-01-23 18:54:21 +07:00
"errors"
"fmt"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/rlimit"
2023-01-29 12:38:15 +07:00
"github.com/mzz2017/softwind/pool"
"github.com/sirupsen/logrus"
2023-01-23 19:01:24 +07:00
"github.com/v2rayA/dae/common"
"github.com/v2rayA/dae/common/consts"
"github.com/v2rayA/dae/component/outbound"
"github.com/v2rayA/dae/component/outbound/dialer"
"github.com/v2rayA/dae/component/routing"
2023-01-28 00:50:21 +07:00
"github.com/v2rayA/dae/config"
"github.com/v2rayA/dae/pkg/config_parser"
2023-01-30 22:21:00 +07:00
internal "github.com/v2rayA/dae/pkg/ebpf_internal"
2023-01-23 18:54:21 +07:00
"golang.org/x/sys/unix"
"net"
"net/netip"
"os"
"path/filepath"
"reflect"
2023-01-23 18:54:21 +07:00
"strconv"
"strings"
"sync"
"syscall"
2023-01-28 14:47:43 +07:00
"time"
2023-01-23 18:54:21 +07:00
)
type ControlPlane struct {
*ControlPlaneCore
deferFuncs []func() error
2023-01-23 18:54:21 +07:00
// TODO: add mutex?
outbounds []*outbound.DialerGroup
outboundName2Id map[string]uint8
SimulatedLpmTries [][]netip.Prefix
SimulatedDomainSet []DomainSet
Final string
// mutex protects the dnsCache.
2023-01-27 01:10:27 +07:00
mutex sync.Mutex
dnsCache map[string]*dnsCache
2023-01-24 16:15:27 +07:00
dnsUpstream netip.AddrPort
2023-01-23 18:54:21 +07:00
}
2023-01-28 00:50:21 +07:00
func NewControlPlane(
log *logrus.Logger,
nodes []string,
groups []config.Group,
routingA *config.Routing,
dnsUpstream string,
checkUrl string,
2023-01-28 14:47:43 +07:00
checkInterval time.Duration,
lanInterface []string,
wanInterface []string,
2023-01-28 00:50:21 +07:00
) (c *ControlPlane, err error) {
kernelVersion, e := internal.KernelVersion()
if e != nil {
return nil, fmt.Errorf("failed to get kernel version: %w", e)
}
2023-02-04 10:38:01 +07:00
// Must judge version from high to low to reduce the number of user upgrading kernel.
if kernelVersion.Less(consts.ChecksumFeatureVersion) {
return nil, fmt.Errorf("your kernel version %v does not support checksum related features; expect >=%v; upgrade your kernel and try again",
kernelVersion.String(),
2023-02-04 10:38:01 +07:00
consts.ChecksumFeatureVersion.String())
}
if len(wanInterface) > 0 && kernelVersion.Less(consts.CgSocketCookieFeatureVersion) {
return nil, fmt.Errorf("your kernel version %v does not support bind to WAN; expect >=%v; remove wan_interface in config file and try again",
kernelVersion.String(),
consts.CgSocketCookieFeatureVersion.String())
}
2023-02-06 13:01:02 +07:00
if len(lanInterface) > 0 && kernelVersion.Less(consts.SkAssignFeatureVersion) {
return nil, fmt.Errorf("your kernel version %v does not support bind to LAN; expect >=%v; remove lan_interface in config file and try again",
2023-02-06 13:01:02 +07:00
kernelVersion.String(),
consts.SkAssignFeatureVersion.String())
}
2023-02-04 10:38:01 +07:00
if kernelVersion.Less(consts.BasicFeatureVersion) {
return nil, fmt.Errorf("your kernel version %v does not satisfy basic requirement; expect >=%v",
c.kernelVersion.String(),
consts.BasicFeatureVersion.String())
2023-02-04 10:38:01 +07:00
}
2023-01-23 18:54:21 +07:00
// Allow the current process to lock memory for eBPF resources.
2023-01-28 00:50:21 +07:00
if err = rlimit.RemoveMemlock(); err != nil {
2023-01-23 18:54:21 +07:00
return nil, fmt.Errorf("rlimit.RemoveMemlock:%v", err)
}
pinPath := filepath.Join(consts.BpfPinRoot, consts.AppName)
os.MkdirAll(pinPath, 0755)
2023-01-27 01:10:27 +07:00
2023-01-23 18:54:21 +07:00
// Load pre-compiled programs and maps into the kernel.
log.Infof("Loading eBPF programs and maps into the kernel")
2023-01-23 18:54:21 +07:00
var bpf bpfObjects
var ProgramOptions ebpf.ProgramOptions
2023-02-04 17:18:06 +07:00
if log.Level == logrus.PanicLevel {
ProgramOptions = ebpf.ProgramOptions{
LogLevel: ebpf.LogLevelBranch | ebpf.LogLevelStats,
//LogLevel: ebpf.LogLevelInstruction | ebpf.LogLevelStats,
}
}
// Trick. Replace the beams with rotten timbers to reduce the loading.
var obj interface{} = &bpf // Bind to both LAN and WAN.
if len(lanInterface) > 0 && len(wanInterface) == 0 {
// Only bind LAN.
2023-02-01 09:59:57 +07:00
obj = &bpfObjectsLan{}
} else if len(lanInterface) == 0 && len(wanInterface) > 0 {
// Only bind to WAN.
// Trick. Replace the beams with rotten timbers.
obj = &bpfObjectsWan{}
}
2023-01-27 01:10:27 +07:00
retryLoadBpf:
if err = loadBpfObjects(obj, &ebpf.CollectionOptions{
2023-01-23 18:54:21 +07:00
Maps: ebpf.MapOptions{
PinPath: pinPath,
},
Programs: ProgramOptions,
2023-01-23 18:54:21 +07:00
}); err != nil {
if errors.Is(err, ebpf.ErrMapIncompatible) {
2023-01-27 01:10:27 +07:00
// Map property is incompatible. Remove the old map and try again.
2023-01-23 18:54:21 +07:00
prefix := "use pinned map "
_, after, ok := strings.Cut(err.Error(), prefix)
if !ok {
2023-01-23 18:54:21 +07:00
return nil, fmt.Errorf("loading objects: bad format: %w", err)
}
mapName, _, _ := strings.Cut(after, ":")
2023-01-23 18:54:21 +07:00
_ = os.Remove(filepath.Join(pinPath, mapName))
2023-01-29 10:19:58 +07:00
log.Infof("Incompatible new map format with existing map %v detected; removed the old one.", mapName)
2023-01-27 01:10:27 +07:00
goto retryLoadBpf
2023-01-23 18:54:21 +07:00
}
2023-01-29 10:19:58 +07:00
// Get detailed log from ebpf.internal.(*VerifierError)
if log.Level == logrus.FatalLevel {
if v := reflect.Indirect(reflect.ValueOf(errors.Unwrap(errors.Unwrap(err)))); v.Kind() == reflect.Struct {
if _log := v.FieldByName("Log"); _log.IsValid() {
if strSlice, ok := _log.Interface().([]string); ok {
log.Fatalln(strings.Join(strSlice, "\n"))
}
}
}
}
return nil, fmt.Errorf("loading objects: %w", err)
2023-01-23 18:54:21 +07:00
}
2023-02-01 09:59:57 +07:00
if _, ok := obj.(*bpfObjects); !ok {
// Reverse takeover.
2023-02-01 09:59:57 +07:00
AssignBpfObjects(&bpf, obj)
}
2023-01-27 01:10:27 +07:00
// Write params.
2023-01-28 00:50:21 +07:00
if err = bpf.ParamMap.Update(consts.DisableL4TxChecksumKey, consts.DisableL4ChecksumPolicy_SetZero, ebpf.UpdateAny); err != nil {
2023-01-23 18:54:21 +07:00
return nil, err
}
2023-01-28 00:50:21 +07:00
if err = bpf.ParamMap.Update(consts.DisableL4RxChecksumKey, consts.DisableL4ChecksumPolicy_SetZero, ebpf.UpdateAny); err != nil {
2023-01-23 18:54:21 +07:00
return nil, err
}
// Write tproxy (control plane) PID.
if err = bpf.ParamMap.Update(consts.ControlPlaneOidKey, uint32(os.Getpid()), ebpf.UpdateAny); err != nil {
return nil, err
}
// Write ip_proto to hdr_size map for IPv6 extension extraction.
2023-01-28 10:47:02 +07:00
if err = bpf.IpprotoHdrsizeMap.Update(uint32(unix.IPPROTO_HOPOPTS), int32(-1), ebpf.UpdateAny); err != nil {
return nil, err
}
if err = bpf.IpprotoHdrsizeMap.Update(uint32(unix.IPPROTO_ROUTING), int32(-1), ebpf.UpdateAny); err != nil {
return nil, err
}
if err = bpf.IpprotoHdrsizeMap.Update(uint32(unix.IPPROTO_FRAGMENT), int32(4), ebpf.UpdateAny); err != nil {
return nil, err
}
2023-01-30 14:50:55 +07:00
if err = bpf.IpprotoHdrsizeMap.Update(uint32(unix.IPPROTO_TCP), int32(-2), ebpf.UpdateAny); err != nil {
2023-01-28 10:47:02 +07:00
return nil, err
}
2023-01-30 14:50:55 +07:00
if err = bpf.IpprotoHdrsizeMap.Update(uint32(unix.IPPROTO_UDP), int32(-2), ebpf.UpdateAny); err != nil {
2023-01-28 10:47:02 +07:00
return nil, err
}
2023-01-28 14:47:43 +07:00
core := &ControlPlaneCore{
log: log,
deferFuncs: []func() error{bpf.Close},
bpf: &bpf,
kernelVersion: &kernelVersion,
}
defer func() {
if err != nil {
_ = core.Close()
}
}()
// Bind to links. Binding should be advance of dialerGroups to avoid un-routable old connection.
for _, ifname := range lanInterface {
if err = core.BindLan(ifname); err != nil {
return nil, fmt.Errorf("BindLan: %v: %w", ifname, err)
}
}
for _, ifname := range wanInterface {
if err = core.BindWan(ifname); err != nil {
return nil, fmt.Errorf("BindWan: %v: %w", ifname, err)
}
}
2023-01-27 01:10:27 +07:00
// DialerGroups (outbounds).
2023-01-28 00:50:21 +07:00
option := &dialer.GlobalOption{
2023-01-28 14:47:43 +07:00
Log: log,
CheckUrl: checkUrl,
CheckInterval: checkInterval,
2023-01-28 00:50:21 +07:00
}
2023-01-23 18:54:21 +07:00
outbounds := []*outbound.DialerGroup{
2023-01-28 00:50:21 +07:00
outbound.NewDialerGroup(option, consts.OutboundDirect.String(),
[]*dialer.Dialer{dialer.NewDirectDialer(option, true)},
2023-01-23 18:54:21 +07:00
outbound.DialerSelectionPolicy{
Policy: consts.DialerSelectionPolicy_Fixed,
FixedIndex: 0,
}),
2023-01-28 00:50:21 +07:00
outbound.NewDialerGroup(option, consts.OutboundBlock.String(),
[]*dialer.Dialer{dialer.NewBlockDialer(option)},
2023-01-23 18:54:21 +07:00
outbound.DialerSelectionPolicy{
2023-01-27 01:10:27 +07:00
Policy: consts.DialerSelectionPolicy_Fixed,
FixedIndex: 0,
2023-01-23 18:54:21 +07:00
}),
}
2023-01-28 00:50:21 +07:00
// Filter out groups.
dialerSet := outbound.NewDialerSetFromLinks(option, nodes)
for _, group := range groups {
2023-01-28 14:47:43 +07:00
// Parse policy.
policy, err := outbound.NewDialerSelectionPolicyFromGroupParam(&group.Param)
if err != nil {
return nil, fmt.Errorf("failed to create group %v: %w", group.Name, err)
}
// Filter nodes.
2023-01-28 00:50:21 +07:00
dialers, err := dialerSet.Filter(group.Param.Filter)
if err != nil {
return nil, fmt.Errorf(`failed to create group "%v": %w`, group.Name, err)
}
2023-01-28 14:47:43 +07:00
// Convert node links to dialers.
log.Infof(`Group "%v" node list:`, group.Name)
for _, d := range dialers {
log.Infoln("\t" + d.Name())
d.ActiveCheck()
}
if len(dialers) == 0 {
log.Infoln("\t<Empty>")
2023-01-28 00:50:21 +07:00
}
2023-01-28 14:47:43 +07:00
// Create dialer group and append it to outbounds.
2023-01-28 00:50:21 +07:00
dialerGroup := outbound.NewDialerGroup(option, group.Name, dialers, *policy)
outbounds = append(outbounds, dialerGroup)
}
2023-01-23 18:54:21 +07:00
// Generate outboundName2Id from outbounds.
if len(outbounds) > 0xff {
return nil, fmt.Errorf("too many outbounds")
}
outboundName2Id := make(map[string]uint8)
for i, o := range outbounds {
outboundName2Id[o.Name] = uint8(i)
}
builder := NewRoutingMatcherBuilder(outboundName2Id, &bpf)
// Routing.
2023-01-28 00:50:21 +07:00
var rules []*config_parser.RoutingRule
if rules, err = routing.ApplyRulesOptimizers(routingA.Rules,
&routing.RefineFunctionParamKeyOptimizer{},
&routing.DatReaderOptimizer{Logger: log},
&routing.MergeAndSortRulesOptimizer{},
&routing.DeduplicateParamsOptimizer{},
); err != nil {
2023-02-01 13:08:01 +07:00
return nil, fmt.Errorf("ApplyRulesOptimizers error:\n%w", err)
}
2023-01-29 10:19:58 +07:00
if log.IsLevelEnabled(logrus.DebugLevel) {
var debugBuilder strings.Builder
for _, rule := range rules {
2023-01-28 00:50:21 +07:00
debugBuilder.WriteString(rule.String(true) + "\n")
}
2023-01-29 10:19:58 +07:00
log.Debugf("RoutingA:\n%vfinal: %v\n", debugBuilder.String(), routingA.Final)
}
if err = routing.ApplyMatcherBuilder(log, builder, rules, routingA.Final); err != nil {
2023-01-23 18:54:21 +07:00
return nil, fmt.Errorf("ApplyMatcherBuilder: %w", err)
}
2023-01-28 00:50:21 +07:00
if err = builder.Build(); err != nil {
2023-01-23 18:54:21 +07:00
return nil, fmt.Errorf("RoutingMatcherBuilder.Build: %w", err)
}
2023-01-27 01:10:27 +07:00
// DNS upstream.
2023-02-06 15:22:07 +07:00
var dnsAddrPort netip.AddrPort
if dnsUpstream != "" {
dnsAddrPort, err = netip.ParseAddrPort(dnsUpstream)
if err != nil {
return nil, fmt.Errorf("failed to parse DNS upstream: \"%v\": %w", dnsUpstream, err)
}
dnsAddr16 := dnsAddrPort.Addr().As16()
if err = bpf.DnsUpstreamMap.Update(consts.ZeroKey, bpfIpPort{
Ip: common.Ipv6ByteSliceToUint32Array(dnsAddr16[:]),
Port: internal.Htons(dnsAddrPort.Port()),
}, ebpf.UpdateAny); err != nil {
return nil, err
}
} else {
if err = bpf.DnsUpstreamMap.Update(consts.ZeroKey, bpfIpPort{
Ip: [4]uint32{},
// Zero port indicates no element, because bpf_map_lookup_elem cannot return 0 for map_type_array.
Port: 0,
}, ebpf.UpdateAny); err != nil {
return nil, err
}
2023-01-27 01:10:27 +07:00
}
2023-01-23 18:54:21 +07:00
return &ControlPlane{
ControlPlaneCore: core,
deferFuncs: nil,
2023-01-23 18:54:21 +07:00
outbounds: outbounds,
outboundName2Id: outboundName2Id,
SimulatedLpmTries: builder.SimulatedLpmTries,
SimulatedDomainSet: builder.SimulatedDomainSet,
2023-01-28 00:50:21 +07:00
Final: routingA.Final,
2023-01-23 18:54:21 +07:00
mutex: sync.Mutex{},
dnsCache: make(map[string]*dnsCache),
2023-01-28 00:50:21 +07:00
dnsUpstream: dnsAddrPort,
2023-01-23 18:54:21 +07:00
}, nil
}
func (c *ControlPlane) ListenAndServe(port uint16) (err error) {
// Listen.
var listenConfig = net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
return dialer.TproxyControl(c)
},
}
tcpListener, err := listenConfig.Listen(context.TODO(), "tcp", "[::1]:"+strconv.Itoa(int(port)))
2023-01-23 18:54:21 +07:00
if err != nil {
return fmt.Errorf("listenTCP: %w", err)
}
defer tcpListener.Close()
packetConn, err := listenConfig.ListenPacket(context.TODO(), "udp", "[::1]:"+strconv.Itoa(int(port)))
2023-01-23 18:54:21 +07:00
if err != nil {
return fmt.Errorf("listenUDP: %w", err)
}
defer packetConn.Close()
udpConn := packetConn.(*net.UDPConn)
2023-01-23 18:54:21 +07:00
/// Serve.
// TCP socket.
tcpFile, err := tcpListener.(*net.TCPListener).File()
if err != nil {
return fmt.Errorf("failed to retrieve copy of the underlying TCP connection file")
}
c.deferFuncs = append(c.deferFuncs, func() error {
return tcpFile.Close()
})
if err := c.bpf.ListenSocketMap.Update(consts.ZeroKey, uint64(tcpFile.Fd()), ebpf.UpdateAny); err != nil {
return err
}
// UDP socket.
udpFile, err := udpConn.File()
if err != nil {
return fmt.Errorf("failed to retrieve copy of the underlying UDP connection file")
}
c.deferFuncs = append(c.deferFuncs, func() error {
return udpFile.Close()
})
if err := c.bpf.ListenSocketMap.Update(consts.OneKey, uint64(udpFile.Fd()), ebpf.UpdateAny); err != nil {
return err
}
// Port.
2023-01-30 22:21:00 +07:00
if err := c.bpf.ParamMap.Update(consts.BigEndianTproxyPortKey, uint32(internal.Htons(port)), ebpf.UpdateAny); err != nil {
2023-01-23 18:54:21 +07:00
return err
}
ctx, cancel := context.WithCancel(context.Background())
c.deferFuncs = append(c.deferFuncs, func() error {
cancel()
return nil
})
go func() {
defer cancel()
for {
lconn, err := tcpListener.Accept()
2023-01-23 18:54:21 +07:00
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
c.log.Errorf("Error when accept: %v", err)
}
break
}
go func() {
if err := c.handleConn(lconn); err != nil {
c.log.Warnln("handleConn:", err)
}
}()
}
}()
go func() {
defer cancel()
for {
var buf [65535]byte
var oob [120]byte // Size for original dest
n, oobn, _, src, err := udpConn.ReadMsgUDPAddrPort(buf[:], oob[:])
2023-01-23 18:54:21 +07:00
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
c.log.Errorf("ReadFromUDPAddrPort: %v, %v", src.String(), err)
2023-01-23 18:54:21 +07:00
}
break
}
dst := RetrieveOriginalDest(oob[:oobn])
if !dst.IsValid() {
c.log.WithFields(logrus.Fields{
"source": src.String(),
"oob": hex.EncodeToString(oob[:oobn]),
}).Warnf("Failed to retrieve original dest")
2023-01-23 18:54:21 +07:00
continue
}
newBuf := pool.Get(n)
copy(newBuf, buf[:n])
go func(data []byte, src, dst netip.AddrPort) {
if e := c.handlePkt(newBuf, src, dst); e != nil {
2023-01-23 18:54:21 +07:00
c.log.Warnln("handlePkt:", e)
}
pool.Put(newBuf)
}(newBuf, src, dst)
2023-01-23 18:54:21 +07:00
}
}()
<-ctx.Done()
return nil
}
func (c *ControlPlane) Close() (err error) {
// Invoke defer funcs in reverse order.
for i := len(c.deferFuncs) - 1; i >= 0; i-- {
if e := c.deferFuncs[i](); e != nil {
// Combine errors.
if err != nil {
err = fmt.Errorf("%w; %v", err, e)
} else {
err = e
}
}
}
return c.ControlPlaneCore.Close()
2023-01-23 18:54:21 +07:00
}