2023-02-06 12:56:43 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-06 12:56:43 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2023-02-10 10:55:00 +07:00
|
|
|
"bytes"
|
2023-02-06 12:56:43 +07:00
|
|
|
"encoding/binary"
|
2023-02-21 15:10:44 +07:00
|
|
|
"encoding/hex"
|
2023-02-06 12:56:43 +07:00
|
|
|
"fmt"
|
2023-04-23 12:27:29 +07:00
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
2023-03-14 14:01:55 +07:00
|
|
|
"github.com/daeuniverse/dae/common"
|
|
|
|
"github.com/daeuniverse/dae/common/consts"
|
2023-02-06 12:56:43 +07:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2023-04-02 11:02:57 +07:00
|
|
|
func (c *ControlPlane) Route(src, dst netip.AddrPort, domain string, l4proto consts.L4ProtoType, routingResult *bpfRoutingResult) (outboundIndex consts.OutboundIndex, mark uint32, must bool, err error) {
|
2023-02-25 01:38:21 +07:00
|
|
|
var ipVersion consts.IpVersionType
|
|
|
|
if dst.Addr().Is4() || dst.Addr().Is4In6() {
|
|
|
|
ipVersion = consts.IpVersion_4
|
|
|
|
} else {
|
|
|
|
ipVersion = consts.IpVersion_6
|
|
|
|
}
|
|
|
|
bSrc := src.Addr().As16()
|
|
|
|
bDst := dst.Addr().As16()
|
2023-04-02 11:02:57 +07:00
|
|
|
if outboundIndex, mark, must, err = c.routingMatcher.Match(
|
2023-02-25 01:38:21 +07:00
|
|
|
bSrc[:],
|
|
|
|
bDst[:],
|
|
|
|
src.Port(),
|
|
|
|
dst.Port(),
|
|
|
|
ipVersion,
|
|
|
|
l4proto,
|
|
|
|
domain,
|
|
|
|
routingResult.Pname,
|
2023-08-20 22:43:33 +07:00
|
|
|
routingResult.Dscp,
|
2023-02-25 01:38:21 +07:00
|
|
|
append([]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, routingResult.Mac[:]...),
|
|
|
|
); err != nil {
|
2023-04-02 11:02:57 +07:00
|
|
|
return 0, 0, false, err
|
2023-02-25 01:38:21 +07:00
|
|
|
}
|
|
|
|
|
2023-04-02 11:02:57 +07:00
|
|
|
return outboundIndex, mark, false, nil
|
2023-02-25 01:38:21 +07:00
|
|
|
}
|
|
|
|
|
2023-02-27 12:29:42 +07:00
|
|
|
func (c *controlPlaneCore) RetrieveRoutingResult(src, dst netip.AddrPort, l4proto uint8) (result *bpfRoutingResult, err error) {
|
2023-02-06 12:56:43 +07:00
|
|
|
srcIp6 := src.Addr().As16()
|
|
|
|
dstIp6 := dst.Addr().As16()
|
|
|
|
|
2023-08-20 22:43:33 +07:00
|
|
|
tuples := &bpfTuplesKey{
|
2023-02-13 02:41:59 +07:00
|
|
|
Sip: struct{ U6Addr8 [16]uint8 }{U6Addr8: srcIp6},
|
2023-02-21 15:10:44 +07:00
|
|
|
Sport: common.Htons(src.Port()),
|
2023-02-13 02:41:59 +07:00
|
|
|
Dip: struct{ U6Addr8 [16]uint8 }{U6Addr8: dstIp6},
|
2023-02-21 15:10:44 +07:00
|
|
|
Dport: common.Htons(dst.Port()),
|
2023-02-06 12:56:43 +07:00
|
|
|
L4proto: l4proto,
|
|
|
|
}
|
2023-02-06 17:34:34 +07:00
|
|
|
|
2023-02-20 17:06:54 +07:00
|
|
|
var routingResult bpfRoutingResult
|
2023-02-21 15:28:09 +07:00
|
|
|
if err := c.bpf.RoutingTuplesMap.Lookup(tuples, &routingResult); err != nil {
|
2023-02-20 17:06:54 +07:00
|
|
|
return nil, fmt.Errorf("reading map: key [%v, %v, %v]: %w", src.String(), l4proto, dst.String(), err)
|
2023-02-06 17:34:34 +07:00
|
|
|
}
|
2023-02-20 17:06:54 +07:00
|
|
|
return &routingResult, nil
|
2023-02-06 12:56:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func RetrieveOriginalDest(oob []byte) netip.AddrPort {
|
|
|
|
msgs, err := syscall.ParseSocketControlMessage(oob)
|
|
|
|
if err != nil {
|
|
|
|
return netip.AddrPort{}
|
|
|
|
}
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
|
|
|
|
ip := msg.Data[4:8]
|
|
|
|
port := binary.BigEndian.Uint16(msg.Data[2:4])
|
|
|
|
return netip.AddrPortFrom(netip.AddrFrom4(*(*[4]byte)(ip)), port)
|
|
|
|
} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == unix.IPV6_RECVORIGDSTADDR {
|
|
|
|
ip := msg.Data[8:24]
|
|
|
|
port := binary.BigEndian.Uint16(msg.Data[2:4])
|
2023-02-07 20:11:12 +07:00
|
|
|
return netip.AddrPortFrom(netip.AddrFrom16(*(*[16]byte)(ip)), port)
|
2023-02-06 12:56:43 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return netip.AddrPort{}
|
|
|
|
}
|
2023-02-10 10:55:00 +07:00
|
|
|
|
|
|
|
func checkIpforward(ifname string, ipversion consts.IpVersionStr) error {
|
|
|
|
path := fmt.Sprintf("/proc/sys/net/ipv%v/conf/%v/forwarding", ipversion, ifname)
|
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if bytes.Equal(bytes.TrimSpace(b), []byte("1")) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-26 18:44:25 +07:00
|
|
|
return fmt.Errorf("ipforward on %v is off: %v; see docs of dae for help", ifname, path)
|
2023-02-10 10:55:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckIpforward(ifname string) error {
|
|
|
|
if err := checkIpforward(ifname, consts.IpVersionStr_4); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkIpforward(ifname, consts.IpVersionStr_6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-20 17:06:54 +07:00
|
|
|
|
2023-03-25 16:16:24 +07:00
|
|
|
func setForwarding(ifname string, ipversion consts.IpVersionStr, val string) error {
|
|
|
|
path := fmt.Sprintf("/proc/sys/net/ipv%v/conf/%v/forwarding", ipversion, ifname)
|
|
|
|
err := os.WriteFile(path, []byte(val), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetIpv4forward(val string) error {
|
|
|
|
err := os.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte(val), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetForwarding(ifname string, val string) {
|
|
|
|
_ = setForwarding(ifname, consts.IpVersionStr_4, val)
|
|
|
|
_ = setForwarding(ifname, consts.IpVersionStr_6, val)
|
|
|
|
}
|
|
|
|
|
2023-02-26 04:07:42 +07:00
|
|
|
func checkSendRedirects(ifname string, ipversion consts.IpVersionStr) error {
|
|
|
|
path := fmt.Sprintf("/proc/sys/net/ipv%v/conf/%v/send_redirects", ipversion, ifname)
|
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if bytes.Equal(bytes.TrimSpace(b), []byte("0")) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-26 18:44:25 +07:00
|
|
|
return fmt.Errorf("send_directs on %v is on: %v; see docs of dae for help", ifname, path)
|
2023-02-26 04:07:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckSendRedirects(ifname string) error {
|
|
|
|
if err := checkSendRedirects(ifname, consts.IpVersionStr_4); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-25 16:16:24 +07:00
|
|
|
func setSendRedirects(ifname string, ipversion consts.IpVersionStr, val string) error {
|
|
|
|
path := fmt.Sprintf("/proc/sys/net/ipv%v/conf/%v/send_redirects", ipversion, ifname)
|
|
|
|
err := os.WriteFile(path, []byte(val), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSendRedirects(ifname string, val string) {
|
|
|
|
_ = setSendRedirects(ifname, consts.IpVersionStr_4, val)
|
|
|
|
}
|
|
|
|
|
2023-02-21 15:10:44 +07:00
|
|
|
func ProcessName2String(pname []uint8) string {
|
|
|
|
return string(bytes.TrimRight(pname[:], string([]byte{0})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Mac2String(mac []uint8) string {
|
|
|
|
ori := []byte(hex.EncodeToString(mac))
|
|
|
|
// Insert ":".
|
2023-02-21 15:28:09 +07:00
|
|
|
b := make([]byte, len(ori)/2*3-1)
|
2023-02-21 15:10:44 +07:00
|
|
|
for i, j := 0, 0; i < len(ori); i, j = i+2, j+3 {
|
|
|
|
copy(b[j:j+2], ori[i:i+2])
|
2023-02-21 15:28:09 +07:00
|
|
|
if j+2 < len(b) {
|
|
|
|
b[j+2] = ':'
|
|
|
|
}
|
2023-02-21 15:10:44 +07:00
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|