Skip to content

Commit b5f3fec

Browse files
committed
ping: Fix linux route rules
1 parent 4fb5702 commit b5f3fec

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

redirect_nftables.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,26 @@ func (r *autoRedirect) setupNFTables() error {
143143
}
144144
}
145145
chainPreRoutingUDP := nft.AddChain(&nftables.Chain{
146-
Name: "prerouting_udp",
146+
Name: "prerouting_udp_icmp",
147147
Table: table,
148148
Hooknum: nftables.ChainHookPrerouting,
149149
Priority: nftables.ChainPriorityRef(*nftables.ChainPriorityNATDest + 2),
150150
Type: nftables.ChainTypeFilter,
151151
})
152+
ipProto := &nftables.Set{
153+
Table: table,
154+
Anonymous: true,
155+
Constant: true,
156+
KeyType: nftables.TypeInetProto,
157+
}
158+
err = nft.AddSet(ipProto, []nftables.SetElement{
159+
{Key: []byte{unix.IPPROTO_UDP}},
160+
{Key: []byte{unix.IPPROTO_ICMP}},
161+
{Key: []byte{unix.IPPROTO_ICMPV6}},
162+
})
163+
if err != nil {
164+
return err
165+
}
152166
nft.AddRule(&nftables.Rule{
153167
Table: table,
154168
Chain: chainPreRoutingUDP,
@@ -157,10 +171,11 @@ func (r *autoRedirect) setupNFTables() error {
157171
Key: expr.MetaKeyL4PROTO,
158172
Register: 1,
159173
},
160-
&expr.Cmp{
161-
Op: expr.CmpOpNeq,
162-
Register: 1,
163-
Data: []byte{unix.IPPROTO_UDP},
174+
&expr.Lookup{
175+
SourceRegister: 1,
176+
SetID: ipProto.ID,
177+
SetName: ipProto.Name,
178+
Invert: true,
164179
},
165180
&expr.Verdict{
166181
Kind: expr.VerdictReturn,

tun_linux.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -816,14 +816,6 @@ func (t *NativeTun) rules() []*netlink.Rule {
816816
it.Family = unix.AF_INET
817817
rules = append(rules, it)
818818
}
819-
if p4 && !t.options.StrictRoute {
820-
it = netlink.NewRule()
821-
it.Priority = priority
822-
it.IPProto = syscall.IPPROTO_ICMP
823-
it.Goto = nopPriority
824-
it.Family = unix.AF_INET
825-
rules = append(rules, it)
826-
}
827819
if p6 {
828820
it = netlink.NewRule()
829821
it.Priority = priority6
@@ -834,16 +826,6 @@ func (t *NativeTun) rules() []*netlink.Rule {
834826
it.Family = unix.AF_INET6
835827
rules = append(rules, it)
836828
}
837-
838-
if p6 && !t.options.StrictRoute {
839-
it = netlink.NewRule()
840-
it.Priority = priority6
841-
it.IPProto = syscall.IPPROTO_ICMPV6
842-
it.Goto = nopPriority
843-
it.Family = unix.AF_INET6
844-
rules = append(rules, it)
845-
priority6++
846-
}
847829
}
848830
if p4 {
849831
it = netlink.NewRule()

0 commit comments

Comments
 (0)