lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

mwan3 (3613B)


      1 #!/bin/sh
      2 
      3 [ -x /usr/bin/ip ] || exit 4
      4 [ -x /usr/sbin/ipset ] || exit 5
      5 [ -x /usr/sbin/iptables ] || exit 6
      6 [ -x /usr/sbin/ip6tables ] || exit 7
      7 [ -x /usr/bin/logger ] || exit 8
      8 
      9 . /lib/functions.sh
     10 . /lib/functions/network.sh
     11 . /lib/mwan3/mwan3.sh
     12 
     13 help()
     14 {
     15 	cat <<EOF
     16 Syntax: mwan3 [command]
     17 
     18 Available commands:
     19 	start           Load iptables rules, ip rules and ip routes
     20 	stop            Unload iptables rules, ip rules and ip routes
     21 	restart         Reload iptables rules, ip rules and ip routes
     22 	ifup <iface>    Load rules and routes for specific interface
     23 	ifdown <iface>  Unload rules and routes for specific interface
     24 	interfaces      Show interfaces status
     25 	policies        Show currently active policy
     26 	connected       Show directly connected networks
     27 	rules           Show active rules
     28 	status          Show all status
     29 
     30 EOF
     31 }
     32 
     33 ifdown()
     34 {
     35 	if [ -z "$1" ]; then
     36 		echo "Error: Expecting interface. Usage: mwan3 ifdown <interface>" && exit 0
     37 	fi
     38 
     39 	if [ -n "$2" ]; then
     40 		echo "Error: Too many arguments. Usage: mwan3 ifdown <interface>" && exit 0
     41 	fi
     42 
     43 	ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
     44 
     45 	if [ -e /var/run/mwan3track-$1.pid ] ; then
     46 		kill $(cat /var/run/mwan3track-$1.pid)
     47 		rm /var/run/mwan3track-$1.pid
     48 	fi
     49 }
     50 
     51 ifup()
     52 {
     53 	local device enabled
     54 
     55 	config_load mwan3
     56 
     57 	if [ -z "$1" ]; then
     58 		echo "Expecting interface. Usage: mwan3 ifup <interface>" && exit 0
     59 	fi
     60 
     61 	if [ -n "$2" ]; then
     62 		echo "Too many arguments. Usage: mwan3 ifup <interface>" && exit 0
     63 	fi
     64 
     65 	config_get enabled "$1" enabled 0
     66 
     67 	device=$(uci -p /var/state get network.$1.ifname) &> /dev/null
     68 
     69 	if [ -n "$device" ] ; then
     70 		[ "$enabled" -eq 1 ] && ACTION=ifup INTERFACE=$1 DEVICE=$device /sbin/hotplug-call iface
     71 	fi
     72 }
     73 
     74 interfaces()
     75 {
     76 	config_load mwan3
     77 
     78 	echo "Interface status:"
     79 	config_foreach mwan3_report_iface_status interface
     80 	echo -e
     81 }
     82 
     83 policies()
     84 {
     85 	echo "Current ipv4 policies:"
     86 	mwan3_report_policies_v4
     87 	echo -e
     88 	echo "Current ipv6 policies:"
     89 	mwan3_report_policies_v6
     90 	echo -e
     91 }
     92 
     93 connected()
     94 {
     95 	echo "Directly connected ipv4 networks:"
     96 	mwan3_report_connected_v4
     97 	echo -e
     98 	echo "Directly connected ipv6 networks:"
     99 	mwan3_report_connected_v6
    100 	echo -e
    101 }
    102 
    103 rules()
    104 {
    105 	echo "Active ipv4 user rules:"
    106 	mwan3_report_rules_v4
    107 	echo -e
    108 	echo "Active ipv6 user rules:"
    109 	mwan3_report_rules_v6
    110 	echo -e
    111 }
    112 
    113 status()
    114 {
    115 	interfaces
    116 	policies
    117 	connected
    118 	rules
    119 }
    120 
    121 start()
    122 {
    123 	config_load mwan3
    124 	config_foreach ifup interface
    125 }
    126 
    127 stop()
    128 {
    129 	local ipset route rule table IP IPT
    130 
    131 	killall mwan3track &> /dev/null
    132 	rm /var/run/mwan3track-* &> /dev/null
    133 
    134 	for IP in "$IP4" "$IP6"; do
    135 
    136 		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
    137 			$IP route flush table $route &> /dev/null
    138 		done
    139 
    140 		for rule in $($IP rule list | egrep '^[1-2][0-9]{3}\:' | cut -d ':' -f 1); do
    141 			$IP rule del pref $rule &> /dev/null
    142 		done
    143 	done
    144 
    145 	for IPT in "$IPT4" "$IPT6"; do
    146 
    147 		$IPT -D PREROUTING -j mwan3_hook &> /dev/null
    148 		$IPT -D OUTPUT -j mwan3_hook &> /dev/null
    149 
    150 		for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
    151 			$IPT -F $table &> /dev/null
    152 		done
    153 
    154 		for table in $($IPT -S | awk '{print $2}' | grep mwan3 | sort -u); do
    155 			$IPT -X $table &> /dev/null
    156 		done
    157 	done
    158 
    159 	for ipset in $($IPS -n list | grep mwan3_); do
    160 		$IPS -q destroy $ipset
    161 	done
    162 
    163 	for ipset in $($IPS -n list | grep mwan3 | grep -E '_v4|_v6'); do
    164 		$IPS -q destroy $ipset
    165 	done
    166 }
    167 
    168 restart() {
    169 	stop
    170 	start
    171 }
    172 
    173 case "$1" in
    174 	ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart)
    175 		$*
    176 	;;
    177 	*)
    178 		help
    179 	;;
    180 esac
    181 
    182 exit 0