You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
10 KiB

  1. #!/bin/sh
  2. mwan3_get_iface_id()
  3. {
  4. let iface_count++
  5. [ "$1" == "$INTERFACE" ] && iface_id=$iface_count
  6. }
  7. mwan3_get_route_args()
  8. {
  9. route_args=$(ip -4 route list dev $DEVICE default | head -1 | sed '/.*via \([^ ]*\) .*$/!d;s//\1/;q' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
  10. [ -n "$route_args" ] && route_args="via $route_args"
  11. route_args="nexthop $route_args dev $DEVICE"
  12. }
  13. mwan3_set_general_iptables()
  14. {
  15. if ! iptables -S mwan3_ifaces -t mangle &> /dev/null; then
  16. iptables -N mwan3_ifaces -t mangle
  17. fi
  18. if ! iptables -S mwan3_rules -t mangle &> /dev/null; then
  19. iptables -N mwan3_rules -t mangle
  20. fi
  21. if ! iptables -S mwan3_connected -t mangle &> /dev/null; then
  22. iptables -N mwan3_connected -t mangle
  23. fi
  24. if ! iptables -S mwan3_hook -t mangle &> /dev/null; then
  25. iptables -N mwan3_hook -t mangle
  26. iptables -A mwan3_hook -t mangle -j CONNMARK --restore-mark --nfmask 0xff00 --ctmask 0xff00
  27. iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_ifaces
  28. iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_connected
  29. iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_rules
  30. iptables -A mwan3_hook -t mangle -j CONNMARK --save-mark --nfmask 0xff00 --ctmask 0xff00
  31. fi
  32. if ! iptables -S mwan3_track_hook -t mangle &> /dev/null; then
  33. iptables -N mwan3_track_hook -t mangle
  34. fi
  35. if ! iptables -S PREROUTING -t mangle | grep mwan3_hook &> /dev/null; then
  36. iptables -A PREROUTING -t mangle -j mwan3_hook
  37. fi
  38. if ! iptables -S OUTPUT -t mangle | grep mwan3_hook &> /dev/null; then
  39. iptables -A OUTPUT -t mangle -j mwan3_hook
  40. fi
  41. if ! iptables -S OUTPUT -t mangle | grep mwan3_track_hook &> /dev/null; then
  42. iptables -A OUTPUT -t mangle -j mwan3_track_hook
  43. fi
  44. iptables -F mwan3_rules -t mangle
  45. }
  46. mwan3_set_connected_iptables()
  47. {
  48. local connected_networks
  49. if iptables -S mwan3_connected -t mangle &> /dev/null; then
  50. iptables -F mwan3_connected -t mangle
  51. for connected_networks in $(ip -4 route | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
  52. iptables -A mwan3_connected -t mangle -d $connected_networks -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
  53. done
  54. iptables -I mwan3_connected -t mangle -d 224.0.0.0/3 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
  55. iptables -I mwan3_connected -t mangle -d 127.0.0.0/8 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
  56. fi
  57. }
  58. mwan3_set_iface_iptables()
  59. {
  60. local local_net local_nets
  61. local_net=$(ip -4 route list dev $DEVICE scope link | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
  62. if ! iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
  63. iptables -N mwan3_iface_$INTERFACE -t mangle
  64. fi
  65. iptables -F mwan3_iface_$INTERFACE -t mangle
  66. iptables -D mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE &> /dev/null
  67. if [ $ACTION == "ifup" ]; then
  68. if [ -n "$local_net" ]; then
  69. for local_nets in $local_net ; do
  70. if [ $ACTION == "ifup" ]; then
  71. iptables -I mwan3_iface_$INTERFACE -t mangle -s $local_net -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark 0xff00/0xff00
  72. fi
  73. done
  74. fi
  75. iptables -A mwan3_iface_$INTERFACE -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark $(($iface_id*256))/0xff00
  76. iptables -A mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE
  77. fi
  78. if [ $ACTION == "ifdown" ]; then
  79. iptables -X mwan3_iface_$INTERFACE -t mangle
  80. fi
  81. }
  82. mwan3_set_iface_route()
  83. {
  84. ip -4 route flush table $iface_id
  85. [ $ACTION == "ifup" ] && ip -4 route add table $iface_id default $route_args
  86. }
  87. mwan3_set_iface_rules()
  88. {
  89. while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+1000)):'"')" ]; do
  90. ip -4 rule del pref $(($iface_id+1000))
  91. done
  92. while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+2000)):'"')" ]; do
  93. ip -4 rule del pref $(($iface_id+2000))
  94. done
  95. while [ -n "$(ip -4 rule list | awk '$1 == "2254:"')" ]; do
  96. ip -4 rule del pref 2254
  97. done
  98. [ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+1000)) iif $DEVICE lookup main
  99. [ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+2000)) fwmark $(($iface_id*256))/0xff00 lookup $iface_id
  100. ip rule add pref 2254 fwmark 0xfe00/0xff00 unreachable
  101. }
  102. mwan3_track()
  103. {
  104. local track_ip track_ips reliability count timeout interval down up
  105. mwan3_list_track_ips()
  106. {
  107. track_ips="$1 $track_ips"
  108. }
  109. config_list_foreach $INTERFACE track_ip mwan3_list_track_ips
  110. if [ -n "$track_ips" ]; then
  111. config_get reliability $INTERFACE reliability 1
  112. config_get count $INTERFACE count 1
  113. config_get timeout $INTERFACE timeout 4
  114. config_get interval $INTERFACE interval 10
  115. config_get down $INTERFACE down 5
  116. config_get up $INTERFACE up 5
  117. if ! iptables -S mwan3_track_$INTERFACE -t mangle &> /dev/null; then
  118. iptables -N mwan3_track_$INTERFACE -t mangle
  119. iptables -A mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE
  120. fi
  121. iptables -F mwan3_track_$INTERFACE -t mangle
  122. for track_ip in $track_ips; do
  123. iptables -A mwan3_track_$INTERFACE -t mangle -d $track_ip -j MARK --set-xmark 0xff00/0xff00
  124. done
  125. [ -x /usr/sbin/mwan3track ] && /usr/sbin/mwan3track $INTERFACE $DEVICE $reliability $count $timeout $interval $down $up $track_ips &
  126. else
  127. iptables -D mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE &> /dev/null
  128. iptables -F mwan3_track_$INTERFACE -t mangle &> /dev/null
  129. iptables -X mwan3_track_$INTERFACE -t mangle &> /dev/null
  130. fi
  131. }
  132. mwan3_set_policy()
  133. {
  134. local iface_count iface_id metric probability weight
  135. config_get INTERFACE $1 interface
  136. config_get metric $1 metric 1
  137. config_get weight $1 weight 1
  138. [ -n "$INTERFACE" ] || return 0
  139. config_foreach mwan3_get_iface_id interface
  140. [ -n "$iface_id" ] || return 0
  141. if iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
  142. if [ "$metric" -lt "$lowest_metric" ]; then
  143. total_weight=$weight
  144. iptables -F mwan3_policy_$policy -t mangle
  145. iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE $weight $weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
  146. lowest_metric=$metric
  147. elif [ "$metric" -eq "$lowest_metric" ]; then
  148. total_weight=$(($total_weight+$weight))
  149. probability=$(($weight*1000/$total_weight))
  150. if [ "$probability" -lt 10 ]; then
  151. probability="0.00$probability"
  152. elif [ $probability -lt 100 ]; then
  153. probability="0.0$probability"
  154. elif [ $probability -lt 1000 ]; then
  155. probability="0.$probability"
  156. else
  157. probability="1"
  158. fi
  159. probability="-m statistic --mode random --probability $probability"
  160. iptables -I mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 $probability -m comment --comment "$INTERFACE $weight $total_weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
  161. fi
  162. fi
  163. }
  164. mwan3_set_policies_iptables()
  165. {
  166. local lowest_metric policy total_weight
  167. policy=$1
  168. if [ "$policy" != $(echo "$policy" | cut -c1-15) ]; then
  169. logger -t mwan3 -p warn "Policy $policy exceeds max of 15 chars. Not setting policy" && return 0
  170. fi
  171. if ! iptables -S mwan3_policy_$policy -t mangle &> /dev/null; then
  172. iptables -N mwan3_policy_$policy -t mangle
  173. fi
  174. iptables -F mwan3_policy_$policy -t mangle
  175. iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "unreachable" -j MARK --set-xmark 0xfe00/0xff00
  176. lowest_metric=256
  177. total_weight=0
  178. config_list_foreach $policy use_member mwan3_set_policy
  179. iptables -X $policy -t mangle &> /dev/null
  180. }
  181. mwan3_set_user_rules_iptables()
  182. {
  183. local proto src_ip src_port dest_ip dest_port use_policy
  184. config_get proto $1 proto all
  185. config_get src_ip $1 src_ip 0.0.0.0/0
  186. config_get src_port $1 src_port 0:65535
  187. config_get dest_ip $1 dest_ip 0.0.0.0/0
  188. config_get dest_port $1 dest_port 0:65535
  189. config_get use_policy $1 use_policy
  190. if [ -n "$use_policy" ]; then
  191. if [ "$use_policy" == "default" ]; then
  192. use_policy="MARK --set-xmark 0xff00/0xff00"
  193. elif [ "$use_policy" == "unreachable" ]; then
  194. use_policy="MARK --set-xmark 0xfe00/0xff00"
  195. else
  196. use_policy="mwan3_policy_$use_policy"
  197. fi
  198. case $proto in
  199. tcp|udp)
  200. iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m multiport --sports $src_port -m multiport --dports $dest_port -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
  201. ;;
  202. *)
  203. iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
  204. ;;
  205. esac
  206. fi
  207. }
  208. mwan3_ifupdown()
  209. {
  210. local counter enabled iface_count iface_id route_args wan_metric
  211. [ -n "$DEVICE" ] || exit 0
  212. [ -n "$INTERFACE" ] || exit 0
  213. [ "$(uci get -P /var/state mwan3.$INTERFACE 2> /dev/null)" == "interface" ] || return 0
  214. config_load mwan3
  215. config_get enabled $INTERFACE enabled 0
  216. counter=0
  217. if [ $ACTION == "ifup" ]; then
  218. [ "$enabled" -eq 1 ] || exit 0
  219. while [ -z "$(ip -4 route list dev $DEVICE default | head -1)" -a "$counter" -lt 10 ]; do
  220. sleep 1
  221. let counter++
  222. if [ "$counter" -ge 10 ]; then
  223. logger -t mwan3 -p warn "Could not find gateway for interface $INTERFACE ($DEVICE)" && exit 0
  224. fi
  225. done
  226. mwan3_get_route_args
  227. fi
  228. while [ "$(pgrep -f -o hotplug-call)" -ne $$ -a "$counter" -lt 60 ]; do
  229. sleep 1
  230. let counter++
  231. if [ "$counter" -ge 60 ]; then
  232. logger -t mwan3 -p warn "Timeout waiting for older hotplug processes to finish. $ACTION interface $INTERFACE ($DEVICE) aborted" && exit 0
  233. fi
  234. done
  235. config_foreach mwan3_get_iface_id interface
  236. [ -n "$iface_id" ] || exit 0
  237. [ "$iface_count" -le 250 ] || exit 0
  238. unset iface_count
  239. unset counter
  240. logger -t mwan3 -p notice "$ACTION interface $INTERFACE ($DEVICE)"
  241. mwan3_set_general_iptables
  242. mwan3_set_iface_iptables
  243. mwan3_set_iface_route
  244. mwan3_set_iface_rules
  245. [ $ACTION == "ifup" ] && mwan3_track
  246. config_foreach mwan3_set_policies_iptables policy
  247. config_foreach mwan3_set_user_rules_iptables rule
  248. }
  249. case "$ACTION" in
  250. ifup|ifdown)
  251. mwan3_ifupdown
  252. mwan3_set_connected_iptables
  253. ;;
  254. esac