@@ -99,7 +99,7 @@ del_if() {
9999 local table
100100 [[ $HAVE_SET_DNS -eq 0 ]] || unset_dns
101101 [[ $HAVE_SET_FIREWALL -eq 0 ]] || remove_firewall
102- if [[ -z $TABLE || $TABLE == auto ]] && get_fwmark table && [[ $( wg show " $INTERFACE " allowed-ips ) =~ /0( \ | $' \n ' | $) ]] ; then
102+ if [[ -z $TABLE || $TABLE == auto ]] && get_fwmark table; then
103103 while [[ $( ip -4 rule show 2> /dev/null) == * " lookup $table " * ]]; do
104104 cmd ip -4 rule delete table $table
105105 done
@@ -171,7 +171,7 @@ add_route() {
171171
172172 if [[ -n $TABLE && $TABLE != auto ]]; then
173173 cmd ip $proto route add " $1 " dev " $INTERFACE " table " $TABLE "
174- elif [[ $1 == * /0 ]]; then
174+ elif [[ $WG_QUICK_ROUTING_MODE == " improved " || $ 1 == * /0 ]]; then
175175 add_default " $1 "
176176 else
177177 [[ -n $( ip $proto route show dev " $INTERFACE " match " $1 " 2> /dev/null) ]] || cmd ip $proto route add " $1 " dev " $INTERFACE "
@@ -209,40 +209,45 @@ remove_firewall() {
209209}
210210
211211HAVE_SET_FIREWALL=0
212+ # Add a route using "Improved Rule-based Routing"
212213add_default () {
214+ local proto=-4 iptables=iptables pf=ip
215+ [[ $1 == * :* ]] && proto=-6 iptables=ip6tables pf=ip6
216+
213217 local table line
214218 if ! get_fwmark table; then
215219 table=51820
216220 while [[ -n $( ip -4 route show table $table 2> /dev/null) || -n $( ip -6 route show table $table 2> /dev/null) ]]; do
217221 (( table++ ))
218222 done
219223 cmd wg set " $INTERFACE " fwmark $table
224+
225+ cmd ip $proto rule add not fwmark $table table $table
226+ cmd ip $proto rule add table main suppress_prefixlength 0
227+
228+ local marker=" -m comment --comment \" wg-quick(8) rule for $INTERFACE \" " restore=$' *raw\n ' nftable=" wg-quick-$INTERFACE " nftcmd
229+ printf -v nftcmd ' %sadd table %s %s\n' " $nftcmd " " $pf " " $nftable "
230+ printf -v nftcmd ' %sadd chain %s %s preraw { type filter hook prerouting priority -300; }\n' " $nftcmd " " $pf " " $nftable "
231+ printf -v nftcmd ' %sadd chain %s %s premangle { type filter hook prerouting priority -150; }\n' " $nftcmd " " $pf " " $nftable "
232+ printf -v nftcmd ' %sadd chain %s %s postmangle { type filter hook postrouting priority -150; }\n' " $nftcmd " " $pf " " $nftable "
233+ while read -r line; do
234+ [[ $line =~ .* inet6? \ ([0-9a-f:.]+)/[0-9]+.* ]] || continue
235+ printf -v restore ' %s-I PREROUTING ! -i %s -d %s -m addrtype ! --src-type LOCAL -j DROP %s\n' " $restore " " $INTERFACE " " ${BASH_REMATCH[1]} " " $marker "
236+ printf -v nftcmd ' %sadd rule %s %s preraw iifname != "%s" %s daddr %s fib saddr type != local drop\n' " $nftcmd " " $pf " " $nftable " " $INTERFACE " " $pf " " ${BASH_REMATCH[1]} "
237+ done < <( ip -o $proto addr show dev " $INTERFACE " 2> /dev/null)
238+ printf -v restore ' %sCOMMIT\n*mangle\n-I POSTROUTING -m mark --mark %d -p udp -j CONNMARK --save-mark %s\n-I PREROUTING -p udp -j CONNMARK --restore-mark %s\nCOMMIT\n' " $restore " $table " $marker " " $marker "
239+ printf -v nftcmd ' %sadd rule %s %s postmangle meta l4proto udp mark %d ct mark set mark \n' " $nftcmd " " $pf " " $nftable " $table
240+ printf -v nftcmd ' %sadd rule %s %s premangle meta l4proto udp meta mark set ct mark \n' " $nftcmd " " $pf " " $nftable "
241+ [[ $proto == -4 ]] && cmd sysctl -q net.ipv4.conf.all.src_valid_mark=1
242+ if type -p nft > /dev/null; then
243+ cmd nft -f <( echo -n " $nftcmd " )
244+ else
245+ echo -n " $restore " | cmd $iptables -restore -n
246+ fi
220247 fi
221- local proto=-4 iptables=iptables pf=ip
222- [[ $1 == * :* ]] && proto=-6 iptables=ip6tables pf=ip6
223- cmd ip $proto rule add not fwmark $table table $table
224- cmd ip $proto rule add table main suppress_prefixlength 0
248+
225249 cmd ip $proto route add " $1 " dev " $INTERFACE " table $table
226250
227- local marker=" -m comment --comment \" wg-quick(8) rule for $INTERFACE \" " restore=$' *raw\n ' nftable=" wg-quick-$INTERFACE " nftcmd
228- printf -v nftcmd ' %sadd table %s %s\n' " $nftcmd " " $pf " " $nftable "
229- printf -v nftcmd ' %sadd chain %s %s preraw { type filter hook prerouting priority -300; }\n' " $nftcmd " " $pf " " $nftable "
230- printf -v nftcmd ' %sadd chain %s %s premangle { type filter hook prerouting priority -150; }\n' " $nftcmd " " $pf " " $nftable "
231- printf -v nftcmd ' %sadd chain %s %s postmangle { type filter hook postrouting priority -150; }\n' " $nftcmd " " $pf " " $nftable "
232- while read -r line; do
233- [[ $line =~ .* inet6? \ ([0-9a-f:.]+)/[0-9]+.* ]] || continue
234- printf -v restore ' %s-I PREROUTING ! -i %s -d %s -m addrtype ! --src-type LOCAL -j DROP %s\n' " $restore " " $INTERFACE " " ${BASH_REMATCH[1]} " " $marker "
235- printf -v nftcmd ' %sadd rule %s %s preraw iifname != "%s" %s daddr %s fib saddr type != local drop\n' " $nftcmd " " $pf " " $nftable " " $INTERFACE " " $pf " " ${BASH_REMATCH[1]} "
236- done < <( ip -o $proto addr show dev " $INTERFACE " 2> /dev/null)
237- printf -v restore ' %sCOMMIT\n*mangle\n-I POSTROUTING -m mark --mark %d -p udp -j CONNMARK --save-mark %s\n-I PREROUTING -p udp -j CONNMARK --restore-mark %s\nCOMMIT\n' " $restore " $table " $marker " " $marker "
238- printf -v nftcmd ' %sadd rule %s %s postmangle meta l4proto udp mark %d ct mark set mark \n' " $nftcmd " " $pf " " $nftable " $table
239- printf -v nftcmd ' %sadd rule %s %s premangle meta l4proto udp meta mark set ct mark \n' " $nftcmd " " $pf " " $nftable "
240- [[ $proto == -4 ]] && cmd sysctl -q net.ipv4.conf.all.src_valid_mark=1
241- if type -p nft > /dev/null; then
242- cmd nft -f <( echo -n " $nftcmd " )
243- else
244- echo -n " $restore " | cmd $iptables -restore -n
245- fi
246251 HAVE_SET_FIREWALL=1
247252 return 0
248253}
0 commit comments