/* * SPDX-License-Identifier: AGPL-3.0-only * Copyright (c) since 2023, mzz2017 */ package control import ( "bytes" "encoding/binary" "fmt" "github.com/v2rayA/dae/common" "github.com/v2rayA/dae/common/consts" internal "github.com/v2rayA/dae/pkg/ebpf_internal" "golang.org/x/sys/unix" "net/netip" "os" "syscall" ) func (c *ControlPlaneCore) RetrieveOutboundIndex(src, dst netip.AddrPort, l4proto uint8) (outboundIndex consts.OutboundIndex, err error) { srcIp6 := src.Addr().As16() dstIp6 := dst.Addr().As16() tuples := &bpfTuples{ Src: bpfIpPort{ Ip: common.Ipv6ByteSliceToUint32Array(srcIp6[:]), Port: internal.Htons(src.Port()), }, Dst: bpfIpPort{ Ip: common.Ipv6ByteSliceToUint32Array(dstIp6[:]), Port: internal.Htons(dst.Port()), }, L4proto: l4proto, } var _outboundIndex uint32 if err := c.bpf.RoutingTuplesMap.Lookup(tuples, &_outboundIndex); err != nil { return 0, fmt.Errorf("reading map: key [%v, %v, %v]: %w", src.String(), l4proto, dst.String(), err) } if _outboundIndex > uint32(consts.OutboundMax) { return 0, fmt.Errorf("bad outbound index") } return consts.OutboundIndex(_outboundIndex), nil } 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]) return netip.AddrPortFrom(netip.AddrFrom16(*(*[16]byte)(ip)), port) } } return netip.AddrPort{} } 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 } return fmt.Errorf("ipforward on %v is off: %v", ifname, path) } 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 }