From ea340372b8dbecceb2dcca21f9305d357ff346bc Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Tue, 21 Feb 2023 16:28:09 +0800 Subject: [PATCH] fix: No AddrPort presented for LAN --- control/control_plane.go | 3 ++- control/kern/tproxy.c | 10 ++++------ control/utils.go | 8 +++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/control/control_plane.go b/control/control_plane.go index f6aff95..313f8ff 100644 --- a/control/control_plane.go +++ b/control/control_plane.go @@ -533,9 +533,10 @@ func (c *ControlPlane) ListenAndServe(port uint16) (err error) { routingResult, err := c.core.RetrieveRoutingResult(src, pktDst, unix.IPPROTO_UDP) if err != nil { // WAN. Old method. + lastErr := err addrHdr, dataOffset, err := ParseAddrHdr(data) if err != nil { - c.log.Warnf("No AddrPort presented: %v", err) + c.log.Warnf("No AddrPort presented: %v, %v", lastErr, err) return } copy(data, data[dataOffset:]) diff --git a/control/kern/tproxy.c b/control/kern/tproxy.c index bae3936..8b2c75f 100644 --- a/control/kern/tproxy.c +++ b/control/kern/tproxy.c @@ -158,7 +158,7 @@ struct { // side does not care it (full-cone). __type(value, struct dst_routing_result); // Original target. __uint(max_entries, MAX_DST_MAPPING_NUM); - /// NOTICE: It MUST be pinned. + /// NOTICE: It MUST be pinned, or connection may break. __uint(pinning, LIBBPF_PIN_BY_NAME); } tcp_dst_map SEC(".maps"); // This map is only for old method (redirect mode in WAN). @@ -187,7 +187,6 @@ struct { __type(key, __u32); __type(value, struct lpm_key); __uint(max_entries, 3); - __uint(pinning, LIBBPF_PIN_BY_NAME); } lpm_key_map SEC(".maps"); // h_sport, h_dport: @@ -196,7 +195,6 @@ struct { __type(key, __u32); __type(value, __u16); __uint(max_entries, 2); - __uint(pinning, LIBBPF_PIN_BY_NAME); } h_port_map SEC(".maps"); // l4proto, ipversion: @@ -205,7 +203,6 @@ struct { __type(key, __u32); __type(value, __u32); __uint(max_entries, 2); - __uint(pinning, LIBBPF_PIN_BY_NAME); } l4proto_ipversion_map SEC(".maps"); // IPPROTO to hdr_size @@ -1763,8 +1760,9 @@ int tproxy_wan_egress(struct __sk_buff *skb) { if ((new_hdr.routing_result.outbound == OUTBOUND_DIRECT || new_hdr.routing_result.outbound == OUTBOUND_MUST_DIRECT) && - new_hdr.routing_result.mark == 0 // If mark is not zero, we should re-route it, so we - // send it to control plane in WAN. + new_hdr.routing_result.mark == + 0 // If mark is not zero, we should re-route it, so we + // send it to control plane in WAN. ) { return TC_ACT_OK; } else if (unlikely(new_hdr.routing_result.outbound == OUTBOUND_BLOCK)) { diff --git a/control/utils.go b/control/utils.go index 5755d4e..6ea0a71 100644 --- a/control/utils.go +++ b/control/utils.go @@ -32,7 +32,7 @@ func (c *ControlPlaneCore) RetrieveRoutingResult(src, dst netip.AddrPort, l4prot } var routingResult bpfRoutingResult - if err := c.bpf.RoutingTuplesMap.LookupAndDelete(tuples, &routingResult); err != nil { + if err := c.bpf.RoutingTuplesMap.Lookup(tuples, &routingResult); err != nil { return nil, fmt.Errorf("reading map: key [%v, %v, %v]: %w", src.String(), l4proto, dst.String(), err) } return &routingResult, nil @@ -97,10 +97,12 @@ func ProcessName2String(pname []uint8) string { func Mac2String(mac []uint8) string { ori := []byte(hex.EncodeToString(mac)) // Insert ":". - b := make([]byte, len(ori)/2*3) + b := make([]byte, len(ori)/2*3-1) for i, j := 0, 0; i < len(ori); i, j = i+2, j+3 { copy(b[j:j+2], ori[i:i+2]) - b[j+2] = ':' + if j+2 < len(b) { + b[j+2] = ':' + } } return string(b) }