From 5412a8c78243247acbb4a452e7bc76c1573625d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 30 May 2024 22:25:26 +0800 Subject: [PATCH] Improve iproute2 rules --- tun_linux.go | 83 +++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/tun_linux.go b/tun_linux.go index 886b32b..7ddfe82 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -629,78 +629,61 @@ func (t *NativeTun) rules() []*netlink.Rule { } if p4 { - if t.options.StrictRoute { - it = netlink.NewRule() - it.Priority = priority - it.Table = t.options.TableIndex - it.Family = unix.AF_INET - rules = append(rules, it) - } else { - it = netlink.NewRule() - it.Priority = priority - it.Invert = true - it.IifName = "lo" - it.Table = t.options.TableIndex - it.Family = unix.AF_INET - rules = append(rules, it) + it = netlink.NewRule() + it.Priority = priority + it.Invert = true + it.IifName = "lo" + it.Table = t.options.TableIndex + it.Family = unix.AF_INET + rules = append(rules, it) + it = netlink.NewRule() + it.Priority = priority + it.IifName = "lo" + it.Src = netip.PrefixFrom(netip.IPv4Unspecified(), 32) + it.Table = t.options.TableIndex + it.Family = unix.AF_INET + rules = append(rules, it) + + for _, address := range t.options.Inet4Address { it = netlink.NewRule() it.Priority = priority it.IifName = "lo" - it.Src = netip.PrefixFrom(netip.IPv4Unspecified(), 32) + it.Src = address.Masked() it.Table = t.options.TableIndex it.Family = unix.AF_INET rules = append(rules, it) - - for _, address := range t.options.Inet4Address { - it = netlink.NewRule() - it.Priority = priority - it.IifName = "lo" - it.Src = address.Masked() - it.Table = t.options.TableIndex - it.Family = unix.AF_INET - rules = append(rules, it) - } } priority++ } if p6 { - if !t.options.StrictRoute { - for _, address := range t.options.Inet6Address { - it = netlink.NewRule() - it.Priority = priority6 - it.IifName = "lo" - it.Src = address.Masked() - it.Table = t.options.TableIndex - it.Family = unix.AF_INET6 - rules = append(rules, it) - } - priority6++ - - it = netlink.NewRule() - it.Priority = priority6 - it.IifName = "lo" - it.Src = netip.PrefixFrom(netip.IPv6Unspecified(), 1) - it.Goto = nopPriority - it.Family = unix.AF_INET6 - rules = append(rules, it) - + for _, address := range t.options.Inet6Address { it = netlink.NewRule() it.Priority = priority6 it.IifName = "lo" - it.Src = netip.PrefixFrom(netip.AddrFrom16([16]byte{0: 128}), 1) - it.Goto = nopPriority + it.Src = address.Masked() + it.Table = t.options.TableIndex it.Family = unix.AF_INET6 rules = append(rules, it) - - priority6++ } + priority6++ it = netlink.NewRule() it.Priority = priority6 - it.Table = t.options.TableIndex + it.IifName = "lo" + it.Src = netip.PrefixFrom(netip.IPv6Unspecified(), 1) + it.Goto = nopPriority it.Family = unix.AF_INET6 rules = append(rules, it) + + it = netlink.NewRule() + it.Priority = priority6 + it.IifName = "lo" + it.Src = netip.PrefixFrom(netip.AddrFrom16([16]byte{0: 128}), 1) + it.Goto = nopPriority + it.Family = unix.AF_INET6 + rules = append(rules, it) + priority6++ } if p4 {