Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.22 KB | None | 0 0
  1. #!/bin/sh
  2. ##MODIFIED VERSION of FreshJR_QOS v1.9 for MedWatt released 07/17/2017
  3. ##Modified version has bandwidth limiter enabled for LAN IP's 192.168.1.32 - 192.168.1.47
  4.  
  5. ##Script Tested on ASUS AC-68U, FW380.67_beta4, using Adaptive QOS with Manual Bandwidth Settings
  6. ##Script Changes Unidentified Packets QOS destination from "Default" (Category7) Traffic Container into user definable (in WebUI) "Others" Traffic Container.
  7. ##Script Changes Maximum Allowed Bandwidth of Default Traffic Container (Category7). This container is now repurposed to act as bandwith limiter for defined clients.
  8. ##Script Changes Minimum Guarenteed Bandwidth per QOS category from 128Kbit into user defined percentages upload and download.
  9. ##Script supports custom QOS rules
  10. ## Included active custom rule moves any TCP/UDP traffic on ports 500 & 4500 into VOIP traffic Container. (Wifi Calling)
  11. ## Included active custom rule moves any TCP/UDP traffic on ports 16384 - 16415 into VOIP traffic Container. (Facetime)
  12. ## Rest of rules are commented out but are present to act as custom rule templates. See comments next to rule for details
  13.  
  14. cru a FreshJR_QOS "0 0 * * * /jffs/scripts/FreshJR_QOS" ## schedules a daily check to see if modifcation is still persistant
  15.  
  16. if [ "$(nvram get qos_enable)" = "1" ] && [ "$(nvram get qos_type)" = "1" ] ; then
  17. logger "Adaptive QOS: Modification Script Started"
  18. sleep 30
  19.  
  20. if [ -e "/usr/sbin/realtc" ] ; then
  21. tc="realtc"
  22. else
  23. tc="tc"
  24. fi
  25.  
  26. #################### Variables Setup #####################
  27.  
  28. #DO NOT ADD OR REMOVE SPACES WHILE CHANGING VARIBLES, ADDITIONAL/MISSING SPACES WILL CAUSE FAILURE
  29. #DO NOT ADD OR REMOVE SPACES WHILE CHANGING VARIBLES, ADDITIONAL/MISSING SPACES WILL CAUSE FAILURE
  30.  
  31. #Percent of download speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  32. NetControl_DownBandPercent=5 #This value can be adjust as desired
  33. VoIP_DownBandPercent=20 #This value can be adjust as desired
  34. Gaming_DownBandPercent=15 #This value can be adjust as desired
  35. Others_DownBandPercent=10 #This value can be adjust as desired #Note: New destination for all unidentified traffic per script default
  36. WebSurfing_DownBandPercent=10 #This value can be adjust as desired
  37. Video_DownBandPercent=30 #This value can be adjust as desired
  38. FileTransfer_DownBandPercent=5 #This value can be adjust as desired
  39.  
  40. #Percent of upload speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  41. NetControl_UpBandPercent=5 #This value can be adjust as desired
  42. VoIP_UpBandPercent=20 #This value can be adjust as desired
  43. Gaming_UpBandPercent=15 #This value can be adjust as desired
  44. Others_UpBandPercent=30 #This value can be adjust as desired #Note: New destination for all unidentified traffic per script default
  45. WebSurfing_UpBandPercent=10 #This value can be adjust as desired
  46. Video_UpBandPercent=10 #This value can be adjust as desired
  47. FileTransfer_UpBandPercent=5 #This value can be adjust as desired
  48.  
  49. #Upload/download GUARENTEED and MAX percents for bandwith limiter
  50. Default_DownBandPercent=5 #This value can be adjust as desired #Guarented shared bandwidth for bandwidth limited clients #Note: Originally destination for unidentified traffic but repurposed for bandwith limited client traffic
  51. Default_UpBandPercent=5 #This value can be adjust as desired #Guarented shared bandwidth for bandwidth limited clients #Note: Originally destination for unidentified traffic but repurposed for bandwith limited client traffic
  52. Default_DownCeilPercent=75 #This value can be adjust as desired #Max shared bandwidth for bandwidth limiter (100 = no limiting) #Note: Has to be higher than Default_DownBandPercent
  53. Default_UpCeilPercent=75 #This value can be adjust as desired #Max shared bandwidth for bandwidth limiter (100 = no limiting) #Note: Has to be higher than Default_UpBandPercent
  54.  
  55. while read -r line; #reads all QOS rules, used to read user order of VOIP contianers
  56. do
  57. flowid="$( echo -n ${line} | sed -n -e 's/.*flowid //p' | tail -c 1)" #check if individual rule line output has valid flowID which would then correspond to an individual rules Traffic Container / Catagory / Flowid.
  58. if [ "${flowid}" != "" ] ; then #if valid flowID is found, read next line.
  59. read line
  60. mark="$(echo ${line} | sed -n -e 's/.*mark \([a-zA-z0-9]* [a-zA-z0-9]*\).*/\1/p')" #This line reads which individual QOS traffic rule / mark corresponds to the Traffic Container / Catagory / Flowid read in the previous line
  61.  
  62.  
  63. if [ "${mark}" = "0x80060000 0x803f0000" ] ; then #VOIP
  64. eval "Cat${flowid}DownBandPercent=${VoIP_DownBandPercent}"
  65. eval "Cat${flowid}UpBandPercent=${VoIP_UpBandPercent}"
  66. VOIP="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  67. fi
  68.  
  69. if [ "${mark}" = "0x80080000 0x803f0000" ] ; then #Gaming
  70. eval "Cat${flowid}DownBandPercent=${Gaming_DownBandPercent}"
  71. eval "Cat${flowid}UpBandPercent=${Gaming_UpBandPercent}"
  72. Gaming="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  73. fi
  74.  
  75. if [ "${mark}" = "0x800a0000 0x803f0000" ] ; then #Others
  76. eval "Cat${flowid}DownBandPercent=${Others_DownBandPercent}"
  77. eval "Cat${flowid}UpBandPercent=${Others_UpBandPercent}"
  78. Others="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  79. fi
  80.  
  81. if [ "${mark}" = "0x800d0000 0x803f0000" ] ; then #Web Surfing
  82. eval "Cat${flowid}DownBandPercent=${WebSurfing_DownBandPercent}"
  83. eval "Cat${flowid}UpBandPercent=${WebSurfing_UpBandPercent}"
  84. Web="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  85. fi
  86.  
  87. if [ "${mark}" = "0x80040000 0x803f0000" ] ; then #Streaming
  88. eval "Cat${flowid}DownBandPercent=${Video_DownBandPercent}"
  89. eval "Cat${flowid}UpBandPercent=${Video_UpBandPercent}"
  90. Streaming="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  91. fi
  92.  
  93. if [ "${mark}" = "0x80030000 0x803f0000" ] ; then #Downloads
  94. eval "Cat${flowid}DownBandPercent=${FileTransfer_DownBandPercent}"
  95. eval "Cat${flowid}UpBandPercent=${FileTransfer_UpBandPercent}"
  96. Downloads="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  97. fi
  98.  
  99. if [ "${mark}" = "0x80000000 0x8000ffff" ] ; then #Default (Unidentified traffic)
  100. Default="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  101. fi
  102.  
  103. fi
  104. done <<EOF
  105. $(${tc} filter show dev br0 | grep -o "flowid.*" -A1 | sed '/^--$/d')
  106. EOF
  107.  
  108. Cat0DownBandPercent=${NetControl_DownBandPercent}
  109. Cat0UpBandPercent=${NetControl_UpBandPercent}
  110.  
  111. Cat7DownBandPercent=${Default_DownBandPercent}
  112. Cat7DownCeilPercent=${Default_DownCeilPercent}
  113. Cat7UpBandPercent=${Default_UpBandPercent}
  114. Cat7UpCeilPercent=${Default_UpCeilPercent}
  115.  
  116.  
  117. ############################### Unidentified Traffic Priority & Custom Rules ##########################
  118.  
  119.  
  120. if [ "${Default}" = "1:17" ] ; then
  121. logger "Adaptive QOS: Changing container for Unidentified Traffic & Applying Custom Rules"
  122.  
  123. VOIP_mark="0x40060001" #Note these marks are same as filter match/mask combo but have a 1 at the end. That trailing one prevents the filters from being marked unidentified
  124. Gaming_mark="0x40080001"
  125. Others_mark="0x400a0001"
  126. Web_mark="0x400d0001"
  127. Streaming_mark="0x40040001"
  128. Downloads_mark="0x40030001"
  129. BandwidthLimiter_mark="0x403b0001"
  130.  
  131. ${tc} filter del dev br0 parent 1: prio 1
  132. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 500 0xffff flowid ${VOIP} #Custom Rule 1 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 500 goes to "VOIP" Traffic Container) --Wifi Calling
  133. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 4500 0xffff flowid ${VOIP} #Custom Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 4500 goes to "VOIP" Traffic Container)--Wifi Calling
  134. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 16384 0xffe0 flowid ${VOIP} #Custom Rule 3 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination ports 16384 - 16415 go to "VOIP" Traffic Container) --Facetime
  135. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dst 192.168.1.32/28 flowid ${Default} #Template Rule 1 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination ip 192.168.1.100 goes to "Default" Traffic Container) --Destination catagory is Bandwidth Limited
  136. #${tc} filter add dev br0 protocol all prio 1 u32 match u32 0xCCDDEEFF 0xffffffff at -16 flowid {VOIP} #Template Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination MAC Address AA:BB:CC:DD:EE:FF goes to "VOIP" Traffic Container) **RULE USES LAST 8 MAC DIGITS
  137. #${tc} filter add dev br0 protocol all prio 1 u32 match ip src 75.75.75.75/32 flowid ${Streaming} #Template Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ WAN source ip 75.75.75.75 goes to "Streaming" Traffic Container) (/32 CIDR mask defines only one ip, if IP range is desired see SNB forum post for guide)
  138. #${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 1234 0xffff flowid ${Downloads} #Template Rule 3 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 1234 goes to "Downloads" Traffic Container) (0xFFFF port mask defines only one port, if port range is desired see SNB forum post for guide)
  139. ${tc} filter add dev br0 protocol all prio 2 u32 match mark 0x80000000 0x8000ffff flowid ${Others} #DO NOT DELETE/DISABLE Reroute incomming unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container
  140.  
  141. ${tc} filter del dev eth0 parent 1: prio 1
  142. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 500 0xffff flowid ${VOIP} #Custom Rule 1 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 500 goes to "VOIP" Traffic Container) --Wifi Calling
  143. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 4500 0xffff flowid ${VOIP} #Custom Rule 2 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 4500 goes to "VOIP" Traffic Container) --Wifi Calling
  144. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 16384 0xffe0 flowid ${VOIP} #Custom Rule 3 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source ports 16384 - 16415 go to "VOIP" Traffic Container) --Facetime
  145. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip src 192.168.1.100/32 flowid ${VOIP} #Template Rule 1 -->NOT WORKING<-- (All outgoing traffic w/ LAN source ip 192.168.1.123 goes to "VOIP" Traffic Container) **Reason this does not work is because you can only apply filters to egress traffic. The egress source IP of outgoing traffic is your Public WAN IP, not your Local LAN IP. So filter will not match on local IP.
  146. #${tc} filter add dev eth0 protocol all prio 1 u32 match u16 0xEEFF 0xffff at -8 flowid {VOIP} #Template Rule 2 -->NOT WORKING<-- (All outgoing traffic w/ LAN source MAC Address AA:BB:CC:DD:EE:FF goes to "VOIP" Traffic Container) **RULE USES LAST 4 MAC DIGITS **Reason this does not work is because you can only apply filters to egress traffic. The egress source MAC ADDRESS of outgoing traffic is your Router MAC ADDRESS, not your client MAC ADDRESS. So filter will not match on local MAC ADDRESS.
  147. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip dst 75.75.75.75/32 flowid ${Streaming} #Template Rule 3 --OUTGOING TRAFFIC-- (All outgoing traffic w/ WAN destination ip 75.75.75.75 goes to "Streaming" Traffic Container) (/32 CIDR mask defines only one ip, if IP range is desired see SNB forum post for guide)
  148. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 1234 0xffff flowid ${Downloads} #Template Rule 4 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 1234 go to "Downloads" Traffic Container) (0xFFFF port mask defines only one port, if port range is desired see SNB forum post for guide)
  149. ${tc} filter add dev eth0 protocol all prio 1 u32 match mark 0x403b0000 0x403f0000 flowid ${Default} #Required rule if using BandwidthLimiter_mark variable! --Destination catagory is Bandwidth Limited
  150. iptables -D POSTROUTING -t mangle -o eth0 -s 192.168.1.32/28 -j MARK --set-mark ${BandwidthLimiter_mark} #Template Rule 1 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  151. iptables -A POSTROUTING -t mangle -o eth0 -s 192.168.1.32/28 -j MARK --set-mark ${BandwidthLimiter_mark} #Template Rule 1 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  152. #iptables -D POSTROUTING -t mangle -o eth0 -m mac --mac-source AA:BB:CC:DD:EE:FF -j MARK --set-mark ${VOIP_mark} #Template Rule 2 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  153. #iptables -A POSTROUTING -t mangle -o eth0 -m mac --mac-source AA:BB:CC:DD:EE:FF -j MARK --set-mark ${VOIP_mark} #Template Rule 2 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  154. ${tc} filter add dev eth0 protocol all prio 2 u32 match mark 0x40000000 0x4000ffff flowid ${Others} #DO NOT DELETE/DISABLE Reroute outgoing unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container
  155. else
  156. logger "Adaptive QOS: No change required for Unidentified Traffic Container or Custom Rules"
  157. fi
  158.  
  159. ######################## Minimum Alotted Bandwidth Per QOS Catagory ##########################
  160.  
  161.  
  162. DownCeil="$(printf "%.0f" $(nvram get qos_ibw))" #Maximum download rate defined in WebUI
  163. UpCeil="$(printf "%.0f" $(nvram get qos_obw))"
  164.  
  165. DownRate0="$(expr ${DownCeil} \* ${Cat0DownBandPercent} / 100)"Kbit #New rates that correspond to user defined percentages above
  166. DownRate1="$(expr ${DownCeil} \* ${Cat1DownBandPercent} / 100)"Kbit
  167. DownRate2="$(expr ${DownCeil} \* ${Cat2DownBandPercent} / 100)"Kbit
  168. DownRate3="$(expr ${DownCeil} \* ${Cat3DownBandPercent} / 100)"Kbit
  169. DownRate4="$(expr ${DownCeil} \* ${Cat4DownBandPercent} / 100)"Kbit
  170. DownRate5="$(expr ${DownCeil} \* ${Cat5DownBandPercent} / 100)"Kbit
  171. DownRate6="$(expr ${DownCeil} \* ${Cat6DownBandPercent} / 100)"Kbit
  172. DownRate7="$(expr ${DownCeil} \* ${Cat7DownBandPercent} / 100)"Kbit
  173.  
  174. DownCeil7="$(expr ${DownCeil} \* ${Cat7DownCeilPercent} / 100)"
  175.  
  176. UpRate0="$(expr ${UpCeil} \* ${Cat0UpBandPercent} / 100)"Kbit
  177. UpRate1="$(expr ${UpCeil} \* ${Cat1UpBandPercent} / 100)"Kbit
  178. UpRate2="$(expr ${UpCeil} \* ${Cat2UpBandPercent} / 100)"Kbit
  179. UpRate3="$(expr ${UpCeil} \* ${Cat3UpBandPercent} / 100)"Kbit
  180. UpRate4="$(expr ${UpCeil} \* ${Cat4UpBandPercent} / 100)"Kbit
  181. UpRate5="$(expr ${UpCeil} \* ${Cat5UpBandPercent} / 100)"Kbit
  182. UpRate6="$(expr ${UpCeil} \* ${Cat6UpBandPercent} / 100)"Kbit
  183. UpRate7="$(expr ${UpCeil} \* ${Cat7UpBandPercent} / 100)"Kbit
  184.  
  185. UpCeil7="$(expr ${UpCeil} \* ${Cat7UpCeilPercent} / 100)"
  186.  
  187. CurrentDownRate1="$(${tc} class show dev br0 | grep -w "1:11" | tr ' ' '\n' | grep "rate" -A1 | tail -n 1)"
  188. if [ "${CurrentDownRate1}" != "${DownRate1}" ] ; then
  189. logger "Adaptive QOS: Changing minimum alloted bandwidth per QOS category to user defined percentages"
  190. ${tc} class change dev br0 parent 1:1 classid 1:10 htb prio 0 rate ${DownRate0} ceil ${DownCeil}Kbit
  191. ${tc} class change dev br0 parent 1:1 classid 1:11 htb prio 1 rate ${DownRate1} ceil ${DownCeil}Kbit
  192. ${tc} class change dev br0 parent 1:1 classid 1:12 htb prio 2 rate ${DownRate2} ceil ${DownCeil}Kbit
  193. ${tc} class change dev br0 parent 1:1 classid 1:13 htb prio 3 rate ${DownRate3} ceil ${DownCeil}Kbit
  194. ${tc} class change dev br0 parent 1:1 classid 1:14 htb prio 4 rate ${DownRate4} ceil ${DownCeil}Kbit
  195. ${tc} class change dev br0 parent 1:1 classid 1:15 htb prio 5 rate ${DownRate5} ceil ${DownCeil}Kbit
  196. ${tc} class change dev br0 parent 1:1 classid 1:16 htb prio 6 rate ${DownRate6} ceil ${DownCeil}Kbit
  197. ${tc} class change dev br0 parent 1:1 classid 1:17 htb prio 7 rate ${DownRate7} ceil ${DownCeil7}Kbit
  198.  
  199. ${tc} class change dev eth0 parent 1:1 classid 1:10 htb prio 0 rate ${UpRate0} ceil ${UpCeil}Kbit
  200. ${tc} class change dev eth0 parent 1:1 classid 1:11 htb prio 1 rate ${UpRate1} ceil ${UpCeil}Kbit
  201. ${tc} class change dev eth0 parent 1:1 classid 1:12 htb prio 2 rate ${UpRate2} ceil ${UpCeil}Kbit
  202. ${tc} class change dev eth0 parent 1:1 classid 1:13 htb prio 3 rate ${UpRate3} ceil ${UpCeil}Kbit
  203. ${tc} class change dev eth0 parent 1:1 classid 1:14 htb prio 4 rate ${UpRate4} ceil ${UpCeil}Kbit
  204. ${tc} class change dev eth0 parent 1:1 classid 1:15 htb prio 5 rate ${UpRate5} ceil ${UpCeil}Kbit
  205. ${tc} class change dev eth0 parent 1:1 classid 1:16 htb prio 6 rate ${UpRate6} ceil ${UpCeil}Kbit
  206. ${tc} class change dev eth0 parent 1:1 classid 1:17 htb prio 7 rate ${UpRate7} ceil ${UpCeil7}Kbit
  207.  
  208. else
  209. logger "Adaptive QOS: No change required for QOS category bandwidth percentages"
  210. fi
  211. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement