fix: No AddrPort presented for LAN

This commit is contained in:
mzz2017 2023-02-21 16:28:09 +08:00
parent 384b4131a3
commit ea340372b8
3 changed files with 11 additions and 10 deletions

View File

@ -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:])

View File

@ -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)) {

View File

@ -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)
}