#!/bin/sh
|
|
|
|
[ -x /usr/bin/ip ] || exit 4
|
|
[ -x /usr/sbin/ipset ] || exit 5
|
|
[ -x /usr/sbin/iptables ] || exit 6
|
|
[ -x /usr/sbin/ip6tables ] || exit 7
|
|
[ -x /usr/bin/logger ] || exit 8
|
|
|
|
. /lib/functions.sh
|
|
. /lib/functions/network.sh
|
|
. /lib/mwan3/mwan3.sh
|
|
|
|
help()
|
|
{
|
|
cat <<EOF
|
|
Syntax: mwan3 [command]
|
|
|
|
Available commands:
|
|
start Load iptables rules, ip rules and ip routes
|
|
stop Unload iptables rules, ip rules and ip routes
|
|
restart Reload iptables rules, ip rules and ip routes
|
|
ifup <iface> Load rules and routes for specific interface
|
|
ifdown <iface> Unload rules and routes for specific interface
|
|
interfaces Show interfaces status
|
|
policies Show currently active policy
|
|
connected Show directly connected networks
|
|
rules Show active rules
|
|
status Show all status
|
|
|
|
EOF
|
|
}
|
|
|
|
ifdown()
|
|
{
|
|
if [ -z "$1" ]; then
|
|
echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>" && exit 0
|
|
fi
|
|
|
|
if [ -n "$2" ]; then
|
|
echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>" && exit 0
|
|
fi
|
|
|
|
ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
|
|
|
|
if [ -e /var/run/mwan3track-$1.pid ] ; then
|
|
kill $(cat /var/run/mwan3track-$1.pid)
|
|
rm /var/run/mwan3track-$1.pid
|
|
fi
|
|
}
|
|
|
|
ifup()
|
|
{
|
|
local device enabled
|
|
|
|
config_load mwan3
|
|
|
|
if [ -z "$1" ]; then
|
|
echo "Expecting interface. Usage: mwan3 ifup <interface>" && exit 0
|
|
fi
|
|
|
|
if [ -n "$2" ]; then
|
|
echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
|
|
fi
|
|
|
|
config_get enabled "$1" enabled 0
|
|
|
|
device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
|
|
|
|
if [ -n "$device" ] ; then
|
|
[ "$enabled" -eq 1 ] && ACTION=ifup INTERFACE=$1 DEVICE=$device /sbin/hotplug-call iface
|
|
fi
|
|
}
|
|
|
|
interfaces()
|
|
{
|
|
config_load mwan3
|
|
|
|
echo "Interface status:"
|
|
config_foreach mwan3_report_iface_status interface
|
|
echo -e
|
|
}
|
|
|
|
policies()
|
|
{
|
|
echo "Current ipv4 policies:"
|
|
mwan3_report_policies_v4
|
|
echo -e
|
|
echo "Current ipv6 policies:"
|
|
mwan3_report_policies_v6
|
|
echo -e
|
|
}
|
|
|
|
connected()
|
|
{
|
|
echo "Directly connected ipv4 networks:"
|
|
mwan3_report_connected_v4
|
|
echo -e
|
|
echo "Directly connected ipv6 networks:"
|
|
mwan3_report_connected_v6
|
|
echo -e
|
|
}
|
|
|
|
rules()
|
|
{
|
|
echo "Active ipv4 user rules:"
|
|
mwan3_report_rules_v4
|
|
echo -e
|
|
echo "Active ipv6 user rules:"
|
|
mwan3_report_rules_v6
|
|
echo -e
|
|
}
|
|
|
|
status()
|
|
{
|
|
interfaces
|
|
policies
|
|
connected
|
|
rules
|
|
}
|
|
|
|
start()
|
|
{
|
|
config_load mwan3
|
|
config_foreach ifup interface
|
|
}
|
|
|
|
stop()
|
|
{
|
|
local ipset route rule table IP IPT
|
|
|
|
killall mwan3track &> /dev/null
|
|
rm /var/run/mwan3track-* &> /dev/null
|
|
|
|
for IP in "$IP4" "$IP6"; do
|
|
|
|
for route in $($IP route list table all | sed 's/.*table \([^ ]*\) .*/\1/' | awk '{print $1}' | awk '{for(i=1;i<=NF;i++) if($i+0>0) if($i+0<255) {print;break}}'); do
|
|
$IP route flush table $route &> /dev/null
|
|
done
|
|
|
|
for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
|
|
$IP rule del pref $rule &> /dev/null
|
|
done
|
|
done
|
|
|
|
for IPT in "$IPT4" "$IPT6"; do
|
|
|
|
$IPT -D PREROUTING -j mwan3_hook &> /dev/null
|
|
$IPT -D OUTPUT -j mwan3_hook &> /dev/null
|
|
|
|
for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
|
|
$IPT -F $table &> /dev/null
|
|
done
|
|
|
|
for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
|
|
$IPT -X $table &> /dev/null
|
|
done
|
|
done
|
|
|
|
for ipset in $($IPS -n list | grep mwan3_); do
|
|
$IPS -q destroy $ipset
|
|
done
|
|
|
|
for ipset in $($IPS -n list | grep mwan3 | grep -E '_v4|_v6'); do
|
|
$IPS -q destroy $ipset
|
|
done
|
|
}
|
|
|
|
restart() {
|
|
stop
|
|
start
|
|
}
|
|
|
|
case "$1" in
|
|
ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
|
|
$*
|
|
;;
|
|
*)
|
|
help
|
|
;;
|
|
esac
|
|
|
|
exit 0
|